Skip to content

Commit 7930d0a

Browse files
Ming Leiaxboe
authored andcommitted
sbitmap: introduce __sbitmap_for_each_set()
For blk-mq, we need to be able to iterate software queues starting from any queue in a round robin fashion, so introduce this helper. Reviewed-by: Omar Sandoval <osandov@fb.com> Cc: Omar Sandoval <osandov@fb.com> Signed-off-by: Ming Lei <ming.lei@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent caf8eb0 commit 7930d0a

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

include/linux/sbitmap.h

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -211,46 +211,76 @@ bool sbitmap_any_bit_set(const struct sbitmap *sb);
211211
*/
212212
bool sbitmap_any_bit_clear(const struct sbitmap *sb);
213213

214+
#define SB_NR_TO_INDEX(sb, bitnr) ((bitnr) >> (sb)->shift)
215+
#define SB_NR_TO_BIT(sb, bitnr) ((bitnr) & ((1U << (sb)->shift) - 1U))
216+
214217
typedef bool (*sb_for_each_fn)(struct sbitmap *, unsigned int, void *);
215218

216219
/**
217-
* sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
220+
* __sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
221+
* @start: Where to start the iteration.
218222
* @sb: Bitmap to iterate over.
219223
* @fn: Callback. Should return true to continue or false to break early.
220224
* @data: Pointer to pass to callback.
221225
*
222226
* This is inline even though it's non-trivial so that the function calls to the
223227
* callback will hopefully get optimized away.
224228
*/
225-
static inline void sbitmap_for_each_set(struct sbitmap *sb, sb_for_each_fn fn,
226-
void *data)
229+
static inline void __sbitmap_for_each_set(struct sbitmap *sb,
230+
unsigned int start,
231+
sb_for_each_fn fn, void *data)
227232
{
228-
unsigned int i;
233+
unsigned int index;
234+
unsigned int nr;
235+
unsigned int scanned = 0;
229236

230-
for (i = 0; i < sb->map_nr; i++) {
231-
struct sbitmap_word *word = &sb->map[i];
232-
unsigned int off, nr;
237+
if (start >= sb->depth)
238+
start = 0;
239+
index = SB_NR_TO_INDEX(sb, start);
240+
nr = SB_NR_TO_BIT(sb, start);
233241

234-
if (!word->word)
235-
continue;
242+
while (scanned < sb->depth) {
243+
struct sbitmap_word *word = &sb->map[index];
244+
unsigned int depth = min_t(unsigned int, word->depth - nr,
245+
sb->depth - scanned);
236246

237-
nr = 0;
238-
off = i << sb->shift;
247+
scanned += depth;
248+
if (!word->word)
249+
goto next;
250+
251+
/*
252+
* On the first iteration of the outer loop, we need to add the
253+
* bit offset back to the size of the word for find_next_bit().
254+
* On all other iterations, nr is zero, so this is a noop.
255+
*/
256+
depth += nr;
239257
while (1) {
240-
nr = find_next_bit(&word->word, word->depth, nr);
241-
if (nr >= word->depth)
258+
nr = find_next_bit(&word->word, depth, nr);
259+
if (nr >= depth)
242260
break;
243-
244-
if (!fn(sb, off + nr, data))
261+
if (!fn(sb, (index << sb->shift) + nr, data))
245262
return;
246263

247264
nr++;
248265
}
266+
next:
267+
nr = 0;
268+
if (++index >= sb->map_nr)
269+
index = 0;
249270
}
250271
}
251272

252-
#define SB_NR_TO_INDEX(sb, bitnr) ((bitnr) >> (sb)->shift)
253-
#define SB_NR_TO_BIT(sb, bitnr) ((bitnr) & ((1U << (sb)->shift) - 1U))
273+
/**
274+
* sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
275+
* @sb: Bitmap to iterate over.
276+
* @fn: Callback. Should return true to continue or false to break early.
277+
* @data: Pointer to pass to callback.
278+
*/
279+
static inline void sbitmap_for_each_set(struct sbitmap *sb, sb_for_each_fn fn,
280+
void *data)
281+
{
282+
__sbitmap_for_each_set(sb, 0, fn, data);
283+
}
254284

255285
static inline unsigned long *__sbitmap_word(struct sbitmap *sb,
256286
unsigned int bitnr)

0 commit comments

Comments
 (0)