linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch()
@ 2022-06-05 14:58 wuchi
  2022-06-17 10:08 ` chi wu
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: wuchi @ 2022-06-05 14:58 UTC (permalink / raw)
  To: axboe, mwilck, andriy.shevchenko; +Cc: linux-block, linux-kernel

1. Getting next index before continue branch.
2. Checking free bits when setting the target bits. Otherwise,
it may reuse the busying bits.

Signed-off-by: wuchi <wuchi.zero@gmail.com>
---
 lib/sbitmap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/sbitmap.c b/lib/sbitmap.c
index ae4fd4de9ebe..29eb0484215a 100644
--- a/lib/sbitmap.c
+++ b/lib/sbitmap.c
@@ -528,7 +528,7 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
 
 		sbitmap_deferred_clear(map);
 		if (map->word == (1UL << (map_depth - 1)) - 1)
-			continue;
+			goto next;
 
 		nr = find_first_zero_bit(&map->word, map_depth);
 		if (nr + nr_tags <= map_depth) {
@@ -539,6 +539,8 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
 			get_mask = ((1UL << map_tags) - 1) << nr;
 			do {
 				val = READ_ONCE(map->word);
+				if ((val & ~get_mask) != val)
+					goto next;
 				ret = atomic_long_cmpxchg(ptr, val, get_mask | val);
 			} while (ret != val);
 			get_mask = (get_mask & ~ret) >> nr;
@@ -549,6 +551,7 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
 				return get_mask;
 			}
 		}
+next:
 		/* Jump to next index. */
 		if (++index >= sb->map_nr)
 			index = 0;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch()
  2022-06-05 14:58 [PATCH] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch() wuchi
@ 2022-06-17 10:08 ` chi wu
  2022-06-24 23:47 ` Martin Wilck
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: chi wu @ 2022-06-17 10:08 UTC (permalink / raw)
  To: axboe, mwilck, andriy.shevchenko; +Cc: linux-block, linux-kernel

Ping....

wuchi <wuchi.zero@gmail.com> 于2022年6月5日周日 22:58写道:
>
> 1. Getting next index before continue branch.
> 2. Checking free bits when setting the target bits. Otherwise,
> it may reuse the busying bits.
>
> Signed-off-by: wuchi <wuchi.zero@gmail.com>
> ---
>  lib/sbitmap.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/lib/sbitmap.c b/lib/sbitmap.c
> index ae4fd4de9ebe..29eb0484215a 100644
> --- a/lib/sbitmap.c
> +++ b/lib/sbitmap.c
> @@ -528,7 +528,7 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
>
>                 sbitmap_deferred_clear(map);
>                 if (map->word == (1UL << (map_depth - 1)) - 1)
> -                       continue;
> +                       goto next;
>
>                 nr = find_first_zero_bit(&map->word, map_depth);
>                 if (nr + nr_tags <= map_depth) {
> @@ -539,6 +539,8 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
>                         get_mask = ((1UL << map_tags) - 1) << nr;
>                         do {
>                                 val = READ_ONCE(map->word);
> +                               if ((val & ~get_mask) != val)
> +                                       goto next;
>                                 ret = atomic_long_cmpxchg(ptr, val, get_mask | val);
>                         } while (ret != val);
>                         get_mask = (get_mask & ~ret) >> nr;
> @@ -549,6 +551,7 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
>                                 return get_mask;
>                         }
>                 }
> +next:
>                 /* Jump to next index. */
>                 if (++index >= sb->map_nr)
>                         index = 0;
> --
> 2.20.1
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch()
  2022-06-05 14:58 [PATCH] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch() wuchi
  2022-06-17 10:08 ` chi wu
@ 2022-06-24 23:47 ` Martin Wilck
  2022-06-25 16:59 ` Jens Axboe
  2022-06-25 17:02 ` Jens Axboe
  3 siblings, 0 replies; 5+ messages in thread
From: Martin Wilck @ 2022-06-24 23:47 UTC (permalink / raw)
  To: wuchi, axboe, andriy.shevchenko; +Cc: linux-block, linux-kernel

On Sun, 2022-06-05 at 22:58 +0800, wuchi wrote:
> 1. Getting next index before continue branch.
> 2. Checking free bits when setting the target bits. Otherwise,
> it may reuse the busying bits.
> 
> Signed-off-by: wuchi <wuchi.zero@gmail.com>

Reviewed-by: Martin Wilck <mwilck@suse.com>

> ---
>  lib/sbitmap.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/sbitmap.c b/lib/sbitmap.c
> index ae4fd4de9ebe..29eb0484215a 100644
> --- a/lib/sbitmap.c
> +++ b/lib/sbitmap.c
> @@ -528,7 +528,7 @@ unsigned long __sbitmap_queue_get_batch(struct
> sbitmap_queue *sbq, int nr_tags,
>  
>                 sbitmap_deferred_clear(map);
>                 if (map->word == (1UL << (map_depth - 1)) - 1)
> -                       continue;
> +                       goto next;
>  
>                 nr = find_first_zero_bit(&map->word, map_depth);
>                 if (nr + nr_tags <= map_depth) {
> @@ -539,6 +539,8 @@ unsigned long __sbitmap_queue_get_batch(struct
> sbitmap_queue *sbq, int nr_tags,
>                         get_mask = ((1UL << map_tags) - 1) << nr;
>                         do {
>                                 val = READ_ONCE(map->word);
> +                               if ((val & ~get_mask) != val)
> +                                       goto next;
>                                 ret = atomic_long_cmpxchg(ptr, val,
> get_mask | val);
>                         } while (ret != val);
>                         get_mask = (get_mask & ~ret) >> nr;
> @@ -549,6 +551,7 @@ unsigned long __sbitmap_queue_get_batch(struct
> sbitmap_queue *sbq, int nr_tags,
>                                 return get_mask;
>                         }
>                 }
> +next:
>                 /* Jump to next index. */
>                 if (++index >= sb->map_nr)
>                         index = 0;


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch()
  2022-06-05 14:58 [PATCH] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch() wuchi
  2022-06-17 10:08 ` chi wu
  2022-06-24 23:47 ` Martin Wilck
@ 2022-06-25 16:59 ` Jens Axboe
  2022-06-25 17:02 ` Jens Axboe
  3 siblings, 0 replies; 5+ messages in thread
From: Jens Axboe @ 2022-06-25 16:59 UTC (permalink / raw)
  To: wuchi, mwilck, andriy.shevchenko; +Cc: linux-block, linux-kernel

On 6/5/22 8:58 AM, wuchi wrote:
> 1. Getting next index before continue branch.
> 2. Checking free bits when setting the target bits. Otherwise,
> it may reuse the busying bits.

Applied with:

Fixes: 9672b0d43782 ("sbitmap: add __sbitmap_queue_get_batch()")

added as well.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch()
  2022-06-05 14:58 [PATCH] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch() wuchi
                   ` (2 preceding siblings ...)
  2022-06-25 16:59 ` Jens Axboe
@ 2022-06-25 17:02 ` Jens Axboe
  3 siblings, 0 replies; 5+ messages in thread
From: Jens Axboe @ 2022-06-25 17:02 UTC (permalink / raw)
  To: wuchi.zero, mwilck, andriy.shevchenko; +Cc: linux-kernel, linux-block

On Sun, 5 Jun 2022 22:58:35 +0800, wuchi wrote:
> 1. Getting next index before continue branch.
> 2. Checking free bits when setting the target bits. Otherwise,
> it may reuse the busying bits.
> 
> 

Applied, thanks!

[1/1] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch()
      (no commit info)

Best regards,
-- 
Jens Axboe



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-06-25 17:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-05 14:58 [PATCH] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch() wuchi
2022-06-17 10:08 ` chi wu
2022-06-24 23:47 ` Martin Wilck
2022-06-25 16:59 ` Jens Axboe
2022-06-25 17:02 ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).