Skip to contentSkip to conversation

lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch()

PATCH Applied wuchi 3 participants 5 messages Jun 5, 2022 – Jun 25, 2022

Subsystems (2)
lore
Share & tools
lore

wuchi

author
#0 lore raw

PATCH 1/1 lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch()

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

---
 lib/sbitmap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/sbitmap.c +4 -1
index ae4fd4de9ebe..29eb0484215a 100644
@@ -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;
Signature · 1 line
2.20.1
author
#1 lore raw

Ping....

Quoted 43 lines · wuchi <[email protected]> 于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.

---
 lib/sbitmap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/sbitmap.c +4 -2
index ae4fd4de9ebe..29eb0484215a 100644
@@ -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

Reviewed-by on v1 1/1
#2 lore raw
In reply to wuchi
Quoted 5 lines · wuchi

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

Quoted 38 lines

---
 lib/sbitmap.c | 5 ++++-

 1 file changed, 4 insertions(+), 1 deletion(-)
lib/sbitmap.c +0 -0
index ae4fd4de9ebe..29eb0484215a 100644
@@ -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) {

lib/sbitmap.c +2 -0
@@ -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;

lib/sbitmap.c +1 -0
@@ -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;

maintainer
#3 lore raw
In reply to wuchi
Quoted 3 lines · wuchi

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.

Signature · 1 line
Jens Axboe
maintainer Applied
#4 lore raw
In reply to wuchi
Quoted 5 lines · wuchi

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,

Signature · 1 line
Jens Axboe