sbitmap: fix sbitmap_for_each_set()
authorOmar Sandoval <osandov@fb.com>
Mon, 3 Dec 2018 22:45:43 +0000 (14:45 -0800)
committerJens Axboe <axboe@kernel.dk>
Tue, 4 Dec 2018 00:03:58 +0000 (17:03 -0700)
We need to ignore bits in the cleared mask when iterating over all set
bits.

Fixes: ea86ea2cdced ("sbitmap: ammortize cost of clearing bits")
Reported-by: Jens Axboe@kernel.dk>
Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
include/linux/sbitmap.h

index 92806a2..03f50fc 100644 (file)
@@ -265,12 +265,14 @@ static inline void __sbitmap_for_each_set(struct sbitmap *sb,
        nr = SB_NR_TO_BIT(sb, start);
 
        while (scanned < sb->depth) {
-               struct sbitmap_word *word = &sb->map[index];
-               unsigned int depth = min_t(unsigned int, word->depth - nr,
+               unsigned long word;
+               unsigned int depth = min_t(unsigned int,
+                                          sb->map[index].depth - nr,
                                           sb->depth - scanned);
 
                scanned += depth;
-               if (!word->word)
+               word = sb->map[index].word & ~sb->map[index].cleared;
+               if (!word)
                        goto next;
 
                /*
@@ -280,7 +282,7 @@ static inline void __sbitmap_for_each_set(struct sbitmap *sb,
                 */
                depth += nr;
                while (1) {
-                       nr = find_next_bit(&word->word, depth, nr);
+                       nr = find_next_bit(&word, depth, nr);
                        if (nr >= depth)
                                break;
                        if (!fn(sb, (index << sb->shift) + nr, data))