Commit efe1f3a1 authored by Ming Lei's avatar Ming Lei Committed by Martin K. Petersen

scsi: sbitmap: Maintain allocation round_robin in sbitmap

Currently the allocation round_robin info is maintained by sbitmap_queue.

However, bit allocation really belongs to sbitmap. Move it there.

Link: https://lore.kernel.org/r/20210122023317.687987-3-ming.lei@redhat.com
Cc: Omar Sandoval <osandov@fb.com>
Cc: Kashyap Desai <kashyap.desai@broadcom.com>
Cc: Sumanesh Samanta <sumanesh.samanta@broadcom.com>
Cc: Ewan D. Milne <emilne@redhat.com>
Cc: Hannes Reinecke <hare@suse.de>
Cc: virtualization@lists.linux-foundation.org
Tested-by: default avatarSumanesh Samanta <sumanesh.samanta@broadcom.com>
Reviewed-by: default avatarHannes Reinecke <hare@suse.de>
Signed-off-by: default avatarMing Lei <ming.lei@redhat.com>
Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
parent 4ec59179
...@@ -2702,7 +2702,7 @@ blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set, ...@@ -2702,7 +2702,7 @@ blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
goto free_cpumask; goto free_cpumask;
if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
gfp, node)) gfp, node, false))
goto free_ctxs; goto free_ctxs;
hctx->nr_ctx = 0; hctx->nr_ctx = 0;
......
...@@ -478,7 +478,8 @@ static int kyber_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) ...@@ -478,7 +478,8 @@ static int kyber_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
for (i = 0; i < KYBER_NUM_DOMAINS; i++) { for (i = 0; i < KYBER_NUM_DOMAINS; i++) {
if (sbitmap_init_node(&khd->kcq_map[i], hctx->nr_ctx, if (sbitmap_init_node(&khd->kcq_map[i], hctx->nr_ctx,
ilog2(8), GFP_KERNEL, hctx->numa_node)) { ilog2(8), GFP_KERNEL, hctx->numa_node,
false)) {
while (--i >= 0) while (--i >= 0)
sbitmap_free(&khd->kcq_map[i]); sbitmap_free(&khd->kcq_map[i]);
goto err_kcqs; goto err_kcqs;
......
...@@ -614,7 +614,7 @@ vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg, ...@@ -614,7 +614,7 @@ vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
return ERR_PTR(-EIO); return ERR_PTR(-EIO);
} }
tag = sbitmap_get(&svq->scsi_tags, 0, false); tag = sbitmap_get(&svq->scsi_tags, 0);
if (tag < 0) { if (tag < 0) {
pr_err("Unable to obtain tag for vhost_scsi_cmd\n"); pr_err("Unable to obtain tag for vhost_scsi_cmd\n");
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
...@@ -1512,7 +1512,7 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds) ...@@ -1512,7 +1512,7 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
return 0; return 0;
if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL, if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL,
NUMA_NO_NODE)) NUMA_NO_NODE, false))
return -ENOMEM; return -ENOMEM;
svq->max_cmds = max_cmds; svq->max_cmds = max_cmds;
......
...@@ -56,6 +56,11 @@ struct sbitmap { ...@@ -56,6 +56,11 @@ struct sbitmap {
*/ */
unsigned int map_nr; unsigned int map_nr;
/**
* @round_robin: Allocate bits in strict round-robin order.
*/
bool round_robin;
/** /**
* @map: Allocated bitmap. * @map: Allocated bitmap.
*/ */
...@@ -124,11 +129,6 @@ struct sbitmap_queue { ...@@ -124,11 +129,6 @@ struct sbitmap_queue {
*/ */
atomic_t ws_active; atomic_t ws_active;
/**
* @round_robin: Allocate bits in strict round-robin order.
*/
bool round_robin;
/** /**
* @min_shallow_depth: The minimum shallow depth which may be passed to * @min_shallow_depth: The minimum shallow depth which may be passed to
* sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow(). * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow().
...@@ -144,11 +144,14 @@ struct sbitmap_queue { ...@@ -144,11 +144,14 @@ struct sbitmap_queue {
* given, a good default is chosen. * given, a good default is chosen.
* @flags: Allocation flags. * @flags: Allocation flags.
* @node: Memory node to allocate on. * @node: Memory node to allocate on.
* @round_robin: If true, be stricter about allocation order; always allocate
* starting from the last allocated bit. This is less efficient
* than the default behavior (false).
* *
* Return: Zero on success or negative errno on failure. * Return: Zero on success or negative errno on failure.
*/ */
int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
gfp_t flags, int node); gfp_t flags, int node, bool round_robin);
/** /**
* sbitmap_free() - Free memory used by a &struct sbitmap. * sbitmap_free() - Free memory used by a &struct sbitmap.
...@@ -174,15 +177,12 @@ void sbitmap_resize(struct sbitmap *sb, unsigned int depth); ...@@ -174,15 +177,12 @@ void sbitmap_resize(struct sbitmap *sb, unsigned int depth);
* sbitmap_get() - Try to allocate a free bit from a &struct sbitmap. * sbitmap_get() - Try to allocate a free bit from a &struct sbitmap.
* @sb: Bitmap to allocate from. * @sb: Bitmap to allocate from.
* @alloc_hint: Hint for where to start searching for a free bit. * @alloc_hint: Hint for where to start searching for a free bit.
* @round_robin: If true, be stricter about allocation order; always allocate
* starting from the last allocated bit. This is less efficient
* than the default behavior (false).
* *
* This operation provides acquire barrier semantics if it succeeds. * This operation provides acquire barrier semantics if it succeeds.
* *
* Return: Non-negative allocated bit number if successful, -1 otherwise. * Return: Non-negative allocated bit number if successful, -1 otherwise.
*/ */
int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin); int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint);
/** /**
* sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap, * sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap,
......
...@@ -33,7 +33,7 @@ static inline bool sbitmap_deferred_clear(struct sbitmap_word *map) ...@@ -33,7 +33,7 @@ static inline bool sbitmap_deferred_clear(struct sbitmap_word *map)
} }
int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
gfp_t flags, int node) gfp_t flags, int node, bool round_robin)
{ {
unsigned int bits_per_word; unsigned int bits_per_word;
unsigned int i; unsigned int i;
...@@ -58,6 +58,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, ...@@ -58,6 +58,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
sb->shift = shift; sb->shift = shift;
sb->depth = depth; sb->depth = depth;
sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
sb->round_robin = round_robin;
if (depth == 0) { if (depth == 0) {
sb->map = NULL; sb->map = NULL;
...@@ -129,14 +130,14 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth, ...@@ -129,14 +130,14 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
} }
static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index, static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index,
unsigned int alloc_hint, bool round_robin) unsigned int alloc_hint)
{ {
struct sbitmap_word *map = &sb->map[index]; struct sbitmap_word *map = &sb->map[index];
int nr; int nr;
do { do {
nr = __sbitmap_get_word(&map->word, map->depth, alloc_hint, nr = __sbitmap_get_word(&map->word, map->depth, alloc_hint,
!round_robin); !sb->round_robin);
if (nr != -1) if (nr != -1)
break; break;
if (!sbitmap_deferred_clear(map)) if (!sbitmap_deferred_clear(map))
...@@ -146,7 +147,7 @@ static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index, ...@@ -146,7 +147,7 @@ static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index,
return nr; return nr;
} }
int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin) int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint)
{ {
unsigned int i, index; unsigned int i, index;
int nr = -1; int nr = -1;
...@@ -158,14 +159,13 @@ int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin) ...@@ -158,14 +159,13 @@ int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
* alloc_hint to find the right word index. No point in looping * alloc_hint to find the right word index. No point in looping
* twice in find_next_zero_bit() for that case. * twice in find_next_zero_bit() for that case.
*/ */
if (round_robin) if (sb->round_robin)
alloc_hint = SB_NR_TO_BIT(sb, alloc_hint); alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
else else
alloc_hint = 0; alloc_hint = 0;
for (i = 0; i < sb->map_nr; i++) { for (i = 0; i < sb->map_nr; i++) {
nr = sbitmap_find_bit_in_index(sb, index, alloc_hint, nr = sbitmap_find_bit_in_index(sb, index, alloc_hint);
round_robin);
if (nr != -1) { if (nr != -1) {
nr += index << sb->shift; nr += index << sb->shift;
break; break;
...@@ -350,7 +350,8 @@ int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth, ...@@ -350,7 +350,8 @@ int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
int ret; int ret;
int i; int i;
ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node); ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node,
round_robin);
if (ret) if (ret)
return ret; return ret;
...@@ -382,7 +383,6 @@ int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth, ...@@ -382,7 +383,6 @@ int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch); atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
} }
sbq->round_robin = round_robin;
return 0; return 0;
} }
EXPORT_SYMBOL_GPL(sbitmap_queue_init_node); EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
...@@ -424,12 +424,12 @@ int __sbitmap_queue_get(struct sbitmap_queue *sbq) ...@@ -424,12 +424,12 @@ int __sbitmap_queue_get(struct sbitmap_queue *sbq)
hint = depth ? prandom_u32() % depth : 0; hint = depth ? prandom_u32() % depth : 0;
this_cpu_write(*sbq->alloc_hint, hint); this_cpu_write(*sbq->alloc_hint, hint);
} }
nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin); nr = sbitmap_get(&sbq->sb, hint);
if (nr == -1) { if (nr == -1) {
/* If the map is full, a hint won't do us much good. */ /* If the map is full, a hint won't do us much good. */
this_cpu_write(*sbq->alloc_hint, 0); this_cpu_write(*sbq->alloc_hint, 0);
} else if (nr == hint || unlikely(sbq->round_robin)) { } else if (nr == hint || unlikely(sbq->sb.round_robin)) {
/* Only update the hint if we used it. */ /* Only update the hint if we used it. */
hint = nr + 1; hint = nr + 1;
if (hint >= depth - 1) if (hint >= depth - 1)
...@@ -460,7 +460,7 @@ int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq, ...@@ -460,7 +460,7 @@ int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
if (nr == -1) { if (nr == -1) {
/* If the map is full, a hint won't do us much good. */ /* If the map is full, a hint won't do us much good. */
this_cpu_write(*sbq->alloc_hint, 0); this_cpu_write(*sbq->alloc_hint, 0);
} else if (nr == hint || unlikely(sbq->round_robin)) { } else if (nr == hint || unlikely(sbq->sb.round_robin)) {
/* Only update the hint if we used it. */ /* Only update the hint if we used it. */
hint = nr + 1; hint = nr + 1;
if (hint >= depth - 1) if (hint >= depth - 1)
...@@ -576,7 +576,7 @@ void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, ...@@ -576,7 +576,7 @@ void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
smp_mb__after_atomic(); smp_mb__after_atomic();
sbitmap_queue_wake_up(sbq); sbitmap_queue_wake_up(sbq);
if (likely(!sbq->round_robin && nr < sbq->sb.depth)) if (likely(!sbq->sb.round_robin && nr < sbq->sb.depth))
*per_cpu_ptr(sbq->alloc_hint, cpu) = nr; *per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
} }
EXPORT_SYMBOL_GPL(sbitmap_queue_clear); EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
...@@ -633,7 +633,7 @@ void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m) ...@@ -633,7 +633,7 @@ void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
} }
seq_puts(m, "}\n"); seq_puts(m, "}\n");
seq_printf(m, "round_robin=%d\n", sbq->round_robin); seq_printf(m, "round_robin=%d\n", sbq->sb.round_robin);
seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth); seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
} }
EXPORT_SYMBOL_GPL(sbitmap_queue_show); EXPORT_SYMBOL_GPL(sbitmap_queue_show);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment