Skip to content

Commit 9672b0d

Browse files
committed
sbitmap: add __sbitmap_queue_get_batch()
The block layer tag allocation batching still calls into sbitmap to get each tag, but we can improve on that. Add __sbitmap_queue_get_batch(), which returns a mask of tags all at once, along with an offset for those tags. An example return would be 0xff, where bits 0..7 are set, with tag_offset == 128. The valid tags in this case would be 128..135. A batch is specific to an individual sbitmap_map, hence it cannot be larger than that. The requested number of tags is automatically reduced to the max that can be satisfied with a single map. On failure, 0 is returned. Caller should fall back to single tag allocation at that point/ Signed-off-by: Jens Axboe <[email protected]>
1 parent 8971a3b commit 9672b0d

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

include/linux/sbitmap.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,19 @@ void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth);
426426
*/
427427
int __sbitmap_queue_get(struct sbitmap_queue *sbq);
428428

429+
/**
430+
* __sbitmap_queue_get_batch() - Try to allocate a batch of free bits
431+
* @sbq: Bitmap queue to allocate from.
432+
* @nr_tags: number of tags requested
433+
* @offset: offset to add to returned bits
434+
*
435+
* Return: Mask of allocated tags, 0 if none are found. Each tag allocated is
436+
* a bit in the mask returned, and the caller must add @offset to the value to
437+
* get the absolute tag value.
438+
*/
439+
unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
440+
unsigned int *offset);
441+
429442
/**
430443
* __sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct
431444
* sbitmap_queue, limiting the depth used from each word, with preemption

lib/sbitmap.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,57 @@ int __sbitmap_queue_get(struct sbitmap_queue *sbq)
489489
}
490490
EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
491491

492+
unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
493+
unsigned int *offset)
494+
{
495+
struct sbitmap *sb = &sbq->sb;
496+
unsigned int hint, depth;
497+
unsigned long index, nr;
498+
int i;
499+
500+
if (unlikely(sb->round_robin))
501+
return 0;
502+
503+
depth = READ_ONCE(sb->depth);
504+
hint = update_alloc_hint_before_get(sb, depth);
505+
506+
index = SB_NR_TO_INDEX(sb, hint);
507+
508+
for (i = 0; i < sb->map_nr; i++) {
509+
struct sbitmap_word *map = &sb->map[index];
510+
unsigned long get_mask;
511+
512+
sbitmap_deferred_clear(map);
513+
if (map->word == (1UL << (map->depth - 1)) - 1)
514+
continue;
515+
516+
nr = find_first_zero_bit(&map->word, map->depth);
517+
if (nr + nr_tags <= map->depth) {
518+
atomic_long_t *ptr = (atomic_long_t *) &map->word;
519+
int map_tags = min_t(int, nr_tags, map->depth);
520+
unsigned long val, ret;
521+
522+
get_mask = ((1UL << map_tags) - 1) << nr;
523+
do {
524+
val = READ_ONCE(map->word);
525+
ret = atomic_long_cmpxchg(ptr, val, get_mask | val);
526+
} while (ret != val);
527+
get_mask = (get_mask & ~ret) >> nr;
528+
if (get_mask) {
529+
*offset = nr + (index << sb->shift);
530+
update_alloc_hint_after_get(sb, depth, hint,
531+
*offset + map_tags - 1);
532+
return get_mask;
533+
}
534+
}
535+
/* Jump to next index. */
536+
if (++index >= sb->map_nr)
537+
index = 0;
538+
}
539+
540+
return 0;
541+
}
542+
492543
int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
493544
unsigned int shallow_depth)
494545
{

0 commit comments

Comments
 (0)