Skip to contentSkip to conversation

blk-mq: per-ctx tag caching

PATCH Superseded Jens Axboe 2 participants 8 messages Dec 30, 2019 – Dec 31, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

cover blk-mq: per-ctx tag caching

This is something I've been toying with yesterday, and got it cleaned up a bit just now. It's still very much a testing thing, and there's a few debug checks in there and items that need improving.

Anyway, the idea here is that we can reduce the cost of getting a tag for a new request, if we don't get them piecemeal. Add a per-ctx tag cache, and grab batches of tags if it's empty. If it's not empty, we can just find a free bit there.

/sys/kernel/debug/block/<dev>/<hctx>/<cpu>/tag_hit holds some stats associated with this, so you can check how it's doing.

I've seen nice improvements with this in testing.

Signature · 1 line
Jens Axboe
author maintainer
#1 lore raw

PATCH 1/4 sbitmap: remove cleared bitmask

This is in preparation for doing something better, which doesn't need us to maintain two sets of bitmaps.

---
 include/linux/sbitmap.h | 25 +-----------
 lib/sbitmap.c           | 88 +++++------------------------------------
 2 files changed, 10 insertions(+), 103 deletions(-)
include/linux/sbitmap.h +1 -24
index e40d019c3d9d..7cdd82e0e0dd 100644
@@ -27,16 +27,6 @@ struct sbitmap_word {
 	 * @word: word holding free bits
 	 */
 	unsigned long word ____cacheline_aligned_in_smp;
-
-	/**
-	 * @cleared: word holding cleared bits
-	 */
-	unsigned long cleared ____cacheline_aligned_in_smp;
-
-	/**
-	 * @swap_lock: Held while swapping word <-> cleared
-	 */
-	spinlock_t swap_lock;
 } ____cacheline_aligned_in_smp;
 
 /**
@@ -251,7 +241,7 @@ static inline void __sbitmap_for_each_set(struct sbitmap *sb,
 					   sb->depth - scanned);
 
 		scanned += depth;
-		word = sb->map[index].word & ~sb->map[index].cleared;
+		word = sb->map[index].word;
 		if (!word)
 			goto next;
 
@@ -307,19 +297,6 @@ static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr)
 	clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
 }
 
-/*
- * This one is special, since it doesn't actually clear the bit, rather it
- * sets the corresponding bit in the ->cleared mask instead. Paired with
- * the caller doing sbitmap_deferred_clear() if a given index is full, which
- * will clear the previously freed entries in the corresponding ->word.
- */
-static inline void sbitmap_deferred_clear_bit(struct sbitmap *sb, unsigned int bitnr)
-{
-	unsigned long *addr = &sb->map[SB_NR_TO_INDEX(sb, bitnr)].cleared;
-
-	set_bit(SB_NR_TO_BIT(sb, bitnr), addr);
-}
-
 static inline void sbitmap_clear_bit_unlock(struct sbitmap *sb,
 					    unsigned int bitnr)
 {
lib/sbitmap.c +9 -79

161 lines not on this page · raw message

Signature · 1 line
2.24.1
author maintainer
#2 lore raw

PATCH 2/4 sbitmap: mask out top bits that can't be used

In reply to Jens Axboe

If the tag depth isn't a multiple of the bits_per_word we selected, we'll have dead bits at the top. Ensure that they are set. This doesn't matter for the bit finding as we limit it to the depth of the individual map, but it'll matter for when we try and grab batches of tags off one map.

---
 lib/sbitmap.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
lib/sbitmap.c +7 -1
index c13a9623e9b5..a6c6c104b063 100644
@@ -46,7 +46,13 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
 		return -ENOMEM;
 
 	for (i = 0; i < sb->map_nr; i++) {
-		sb->map[i].depth = min(depth, bits_per_word);
+		if (depth >= bits_per_word) {
+			sb->map[i].depth = bits_per_word;
+		} else {
+			sb->map[i].depth = depth;
+			/* mask off top unused bits, can never get allocated */
+			sb->map[i].word = ~((1UL << depth) - 1);
+		}
 		depth -= sb->map[i].depth;
 	}
 	return 0;
Signature · 1 line
2.24.1
author maintainer
#3 lore raw

PATCH 3/4 sbitmap: add batch tag retrieval

In reply to Jens Axboe

This allows retrieving a batch of tags by the caller, instead of getting them one at the time.

---
 include/linux/sbitmap.h | 23 ++++++++++
 lib/sbitmap.c           | 97 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+)
include/linux/sbitmap.h +23 -0
index 7cdd82e0e0dd..d7560f57cd6f 100644
@@ -366,6 +366,29 @@ static inline void sbitmap_queue_free(struct sbitmap_queue *sbq)
  */
 void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth);
 
+/**
+ * __sbitmap_queue_get_batch() - Try to allocate a batch of free tags from a
+ * &struct sbitmap_queue with preemption already disabled.
+ * @sbq: Bitmap queue to allocate from.
+ * @offset: tag offset
+ * @mask: mask of free tags
+ *
+ * Return: Zero if successful, -1 otherwise.
+ */
+int __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, unsigned int *offset,
+			      unsigned long *mask);
+
+/**
+ * __sbitmap_queue_clear_batch() - Free a batch a tags
+ * @sbq: Bitmap queue to allocate from.
+ * @offset: tag offset
+ * @mask: mask of free tags
+ *
+ * Return: Zero if successful, -1 otherwise.
+ */
+void __sbitmap_queue_clear_batch(struct sbitmap_queue *sbq, unsigned int offset,
+				 unsigned long mask);
+
 /**
  * __sbitmap_queue_get() - Try to allocate a free bit from a &struct
  * sbitmap_queue with preemption already disabled.
lib/sbitmap.c +97 -0

109 lines not on this page · raw message

Signature · 1 line
2.24.1
author maintainer
#4 lore raw

PATCH 4/4 blk-mq: allocate tags in batches

In reply to Jens Axboe

Instead of grabbing tags one by one, grab a batch and store the local cache in the software queue. Then subsequent tag allocations can just grab free tags from there, without having to hit the shared tag map.

We flush these batches out if we run out of tags on the hardware queue. The intent here is this should rarely happen.

This works very well in practice, with anywhere from 40-60 batch counts seen regularly in testing.

---
 block/blk-mq-debugfs.c |  18 +++++++
 block/blk-mq-tag.c     | 105 ++++++++++++++++++++++++++++++++++++++++-
 block/blk-mq.c         |  13 ++++-
 block/blk-mq.h         |   5 ++
 4 files changed, 139 insertions(+), 2 deletions(-)
block/blk-mq-debugfs.c +18 -0
index b3f2ba483992..fcd6f7ce80cc 100644
@@ -659,6 +659,23 @@ CTX_RQ_SEQ_OPS(default, HCTX_TYPE_DEFAULT);
 CTX_RQ_SEQ_OPS(read, HCTX_TYPE_READ);
 CTX_RQ_SEQ_OPS(poll, HCTX_TYPE_POLL);
 
+static ssize_t ctx_tag_hit_write(void *data, const char __user *buf,
+				    size_t count, loff_t *ppos)
+{
+	struct blk_mq_ctx *ctx = data;
+
+	ctx->tag_hit = ctx->tag_refill = 0;
+	return count;
+}
+
+static int ctx_tag_hit_show(void *data, struct seq_file *m)
+{
+	struct blk_mq_ctx *ctx = data;
+
+	seq_printf(m, "hit=%lu refills=%lu, tags=%lx, tag_offset=%u\n", ctx->tag_hit, ctx->tag_refill, ctx->tags, ctx->tag_offset);
+	return 0;
+}
+
 static int ctx_dispatched_show(void *data, struct seq_file *m)
 {
 	struct blk_mq_ctx *ctx = data;
@@ -800,6 +817,7 @@ static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
 	{"read_rq_list", 0400, .seq_ops = &ctx_read_rq_list_seq_ops},
 	{"poll_rq_list", 0400, .seq_ops = &ctx_poll_rq_list_seq_ops},
 	{"dispatched", 0600, ctx_dispatched_show, ctx_dispatched_write},
+	{"tag_hit", 0600, ctx_tag_hit_show, ctx_tag_hit_write},
 	{"merged", 0600, ctx_merged_show, ctx_merged_write},
 	{"completed", 0600, ctx_completed_show, ctx_completed_write},
 	{},
block/blk-mq-tag.c +104 -1

124 lines not on this page · raw message

block/blk-mq.c +12 -1
index 3c71d52b6401..9eeade7736eb 100644
@@ -276,6 +276,9 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
 	struct request *rq = tags->static_rqs[tag];
 	req_flags_t rq_flags = 0;
 
+	if (WARN_ON_ONCE(!rq))
+		printk("no rq for tag %u\n", tag);
+
 	if (data->flags & BLK_MQ_REQ_INTERNAL) {
 		rq->tag = -1;
 		rq->internal_tag = tag;
@@ -2358,6 +2361,9 @@ static void blk_mq_init_cpu_queues(struct request_queue *q,
 		struct blk_mq_hw_ctx *hctx;
 		int k;
 
+		__ctx->tags = 0;
+		__ctx->tag_offset = -1;
+
 		__ctx->cpu = i;
 		spin_lock_init(&__ctx->lock);
 		for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
@@ -2447,6 +2453,8 @@ static void blk_mq_map_swqueue(struct request_queue *q)
 			}
 
 			hctx = blk_mq_map_queue_type(q, j, i);
+			ctx->tags = 0;
+			ctx->tag_offset = -1;
 			ctx->hctxs[j] = hctx;
 			/*
 			 * If the CPU is already set in the mask, then we've
@@ -3224,8 +3232,11 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
 	if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
 		return;
 
-	list_for_each_entry(q, &set->tag_list, tag_set_list)
+	list_for_each_entry(q, &set->tag_list, tag_set_list) {
+		blk_mq_tag_queue_flush_batches(q);
 		blk_mq_freeze_queue(q);
+	}
+
 	/*
 	 * Switch IO scheduler to 'none', cleaning up the data associated
 	 * with the previous scheduler. We will switch back once we are done
block/blk-mq.h +5 -0
index eaaca8fc1c28..7a3198f62f6e 100644
@@ -18,7 +18,9 @@ struct blk_mq_ctxs {
 struct blk_mq_ctx {
 	struct {
 		spinlock_t		lock;
+		unsigned int		tag_offset;
 		struct list_head	rq_lists[HCTX_MAX_TYPES];
+		unsigned long		tags;
 	} ____cacheline_aligned_in_smp;
 
 	unsigned int		cpu;
@@ -32,6 +34,8 @@ struct blk_mq_ctx {
 	/* incremented at completion time */
 	unsigned long		____cacheline_aligned_in_smp rq_completed[2];
 
+	unsigned long		tag_hit, tag_refill;
+
 	struct request_queue	*queue;
 	struct blk_mq_ctxs      *ctxs;
 	struct kobject		kobj;
@@ -60,6 +64,7 @@ struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
 					unsigned int reserved_tags);
 int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
 		     unsigned int hctx_idx, unsigned int depth);
+void blk_mq_tag_queue_flush_batches(struct request_queue *q);
 
 /*
  * Internal helpers for request insertion into sw queues
Signature · 1 line
2.24.1
#5 lore raw

Re: [PATCH 4/4] blk-mq: allocate tags in batches

Hi Jens,

Quoted 108 lines · Jens Axboe

Instead of grabbing tags one by one, grab a batch and store the local cache in the software queue. Then subsequent tag allocations can just grab free tags from there, without having to hit the shared tag map.

We flush these batches out if we run out of tags on the hardware queue. The intent here is this should rarely happen.

This works very well in practice, with anywhere from 40-60 batch counts seen regularly in testing.

---
 block/blk-mq-debugfs.c |  18 +++++++
 block/blk-mq-tag.c     | 105 ++++++++++++++++++++++++++++++++++++++++-
 block/blk-mq.c         |  13 ++++-
 block/blk-mq.h         |   5 ++
 4 files changed, 139 insertions(+), 2 deletions(-)
block/blk-mq-debugfs.c +18 -0
index b3f2ba483992..fcd6f7ce80cc 100644
@@ -659,6 +659,23 @@ CTX_RQ_SEQ_OPS(default, HCTX_TYPE_DEFAULT);
 CTX_RQ_SEQ_OPS(read, HCTX_TYPE_READ);
 CTX_RQ_SEQ_OPS(poll, HCTX_TYPE_POLL);
 
+static ssize_t ctx_tag_hit_write(void *data, const char __user *buf,
+				    size_t count, loff_t *ppos)
+{
+	struct blk_mq_ctx *ctx = data;
+
+	ctx->tag_hit = ctx->tag_refill = 0;
+	return count;
+}
+
+static int ctx_tag_hit_show(void *data, struct seq_file *m)
+{
+	struct blk_mq_ctx *ctx = data;
+
+	seq_printf(m, "hit=%lu refills=%lu, tags=%lx, tag_offset=%u\n", ctx->tag_hit, ctx->tag_refill, ctx->tags, ctx->tag_offset);
+	return 0;
+}
+
 static int ctx_dispatched_show(void *data, struct seq_file *m)
 {
 	struct blk_mq_ctx *ctx = data;
@@ -800,6 +817,7 @@ static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
 	{"read_rq_list", 0400, .seq_ops = &ctx_read_rq_list_seq_ops},
 	{"poll_rq_list", 0400, .seq_ops = &ctx_poll_rq_list_seq_ops},
 	{"dispatched", 0600, ctx_dispatched_show, ctx_dispatched_write},
+	{"tag_hit", 0600, ctx_tag_hit_show, ctx_tag_hit_write},
 	{"merged", 0600, ctx_merged_show, ctx_merged_write},
 	{"completed", 0600, ctx_completed_show, ctx_completed_write},
 	{},
block/blk-mq-tag.c +46 -0
index fbacde454718..3b5269bc4e36 100644
@@ -99,6 +99,101 @@ static int __blk_mq_get_tag(struct blk_mq_alloc_data *data,
 		return __sbitmap_queue_get(bt);
 }
 
+static void blk_mq_tag_flush_batches(struct blk_mq_hw_ctx *hctx)
+{
+	struct sbitmap_queue *bt = &hctx->tags->bitmap_tags;
+	struct blk_mq_ctx *ctx;
+	unsigned int i;
+
+	/*
+	 * we could potentially add a ctx map for this, but probably not
+	 * worth it. better to ensure that we rarely (or never) get into
+	 * the need to flush ctx tag batches.
+	 */
+	hctx_for_each_ctx(hctx, ctx, i) {
+		unsigned long mask;
+		unsigned int offset;
+
+		if (!ctx->tags)
+			continue;
+
+		spin_lock(&ctx->lock);
+		mask = ctx->tags;
+		offset = ctx->tag_offset;
+		ctx->tags = 0;
+		ctx->tag_offset = -1;
+		spin_unlock(&ctx->lock);
+
+		if (mask)
+			__sbitmap_queue_clear_batch(bt, offset, mask);
+	}
+}
+
+void blk_mq_tag_queue_flush_batches(struct request_queue *q)
+{
+	struct blk_mq_hw_ctx *hctx;
+	int i;
+
+	queue_for_each_hw_ctx(q, hctx, i)
+		blk_mq_tag_flush_batches(hctx);
+}
+
+static int blk_mq_get_tag_batch(struct blk_mq_alloc_data *data)
+{
+	struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
+	struct sbitmap_queue *bt = &tags->bitmap_tags;
+	struct blk_mq_ctx *ctx = data->ctx;
+	int tag, cpu;
+

block/blk-mq-tag.c static int __blk_mq_get_tag(struct blk_mq_alloc_data *data, · line 157

Quoted 10 lines
block/blk-mq-tag.c +10 -0
+	if (!ctx)
+		return -1;
+
+	preempt_disable();
+
+	/* bad luck if we got preempted coming in here, should be rare */
+	cpu = smp_processor_id();
+	if (unlikely(ctx->cpu != cpu)) {
+		ctx = data->ctx = __blk_mq_get_ctx(data->q, cpu);
+		data->hctx = blk_mq_map_queue(data->q, data->cmd_flags, ctx);

When blk_mq_get_tag_batch is called for getting driver tag, rq->mq_hctx has been bound to the current request in blk_mq_rq_ctx_init(), so looks we shouldn't change hctx here.

Quoted 118 lines
block/blk-mq-tag.c +48 -1
+		tags = blk_mq_tags_from_data(data);
+		bt = &tags->bitmap_tags;
+	}
+
+	spin_lock(&ctx->lock);
+
+	if (WARN_ON_ONCE(ctx->tag_offset != -1 && !ctx->tags)) {
+		printk("hit=%lu, refill=%lun", ctx->tag_hit,  ctx->tag_refill);
+		ctx->tag_offset = -1;
+	}
+
+	/* if offset is != -1, we have a tag cache. grab first free one */
+	if (ctx->tag_offset != -1) {
+get_tag:
+		ctx->tag_hit++;
+
+		WARN_ON_ONCE(!ctx->tags);
+
+		tag = __ffs(ctx->tags);
+		__clear_bit(tag, &ctx->tags);
+		tag += ctx->tag_offset;
+		if (!ctx->tags)
+			ctx->tag_offset = -1;
+out:
+		spin_unlock(&ctx->lock);
+		preempt_enable();
+		return tag;
+	}
+
+	/* no current tag cache, attempt to refill a batch */
+	if (!__sbitmap_queue_get_batch(bt, &ctx->tag_offset, &ctx->tags)) {
+		ctx->tag_refill++;
+		goto get_tag;
+	}
+
+	tag = -1;
+	goto out;
+}
+
 unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
 {
 	struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
@@ -116,8 +211,13 @@ unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
 		bt = &tags->breserved_tags;
 		tag_offset = 0;
 	} else {
-		bt = &tags->bitmap_tags;
 		tag_offset = tags->nr_reserved_tags;
+
+		tag = blk_mq_get_tag_batch(data);
+		if (tag != -1)
+			goto found_tag;
+
+		bt = &tags->bitmap_tags;
 	}
 
 	tag = __blk_mq_get_tag(data, bt);
@@ -146,6 +246,9 @@ unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
 		if (tag != -1)
 			break;
 
+		if (!(data->flags & BLK_MQ_REQ_RESERVED))
+			blk_mq_tag_flush_batches(data->hctx);
+
 		sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE);
 
 		tag = __blk_mq_get_tag(data, bt);
block/blk-mq.c +12 -1
index 3c71d52b6401..9eeade7736eb 100644
@@ -276,6 +276,9 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
 	struct request *rq = tags->static_rqs[tag];
 	req_flags_t rq_flags = 0;
 
+	if (WARN_ON_ONCE(!rq))
+		printk("no rq for tag %u\n", tag);
+
 	if (data->flags & BLK_MQ_REQ_INTERNAL) {
 		rq->tag = -1;
 		rq->internal_tag = tag;
@@ -2358,6 +2361,9 @@ static void blk_mq_init_cpu_queues(struct request_queue *q,
 		struct blk_mq_hw_ctx *hctx;
 		int k;
 
+		__ctx->tags = 0;
+		__ctx->tag_offset = -1;
+
 		__ctx->cpu = i;
 		spin_lock_init(&__ctx->lock);
 		for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
@@ -2447,6 +2453,8 @@ static void blk_mq_map_swqueue(struct request_queue *q)
 			}
 
 			hctx = blk_mq_map_queue_type(q, j, i);
+			ctx->tags = 0;
+			ctx->tag_offset = -1;
 			ctx->hctxs[j] = hctx;
 			/*
 			 * If the CPU is already set in the mask, then we've
@@ -3224,8 +3232,11 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
 	if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
 		return;
 
-	list_for_each_entry(q, &set->tag_list, tag_set_list)
+	list_for_each_entry(q, &set->tag_list, tag_set_list) {
+		blk_mq_tag_queue_flush_batches(q);
 		blk_mq_freeze_queue(q);
+	}
+
 	/*
 	 * Switch IO scheduler to 'none', cleaning up the data associated
 	 * with the previous scheduler. We will switch back once we are done
block/blk-mq.h +0 -0
index eaaca8fc1c28..7a3198f62f6e 100644

block/blk-mq.h struct blk_mq_ctxs { · line 23

Quoted 6 lines
block/blk-mq.h +2 -0
@@ -18,7 +18,9 @@ struct blk_mq_ctxs {
 struct blk_mq_ctx {
 	struct {
 		spinlock_t		lock;
+		unsigned int		tag_offset;
 		struct list_head	rq_lists[HCTX_MAX_TYPES];
+		unsigned long		tags;

blk_mq_get_tag_batch() can be called for getting both scheduler and driver tag, but the above two seems shared for both, is that one issue?

thanks,
Ming

author maintainer
#6 lore raw

Re: [PATCH 4/4] blk-mq: allocate tags in batches

Quoted 13 lines · Ming Lei
Quoted 13 lines
block/blk-mq-tag.c +13 -0
+static int blk_mq_get_tag_batch(struct blk_mq_alloc_data *data)
+{
+	struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
+	struct sbitmap_queue *bt = &tags->bitmap_tags;
+	struct blk_mq_ctx *ctx = data->ctx;
+	int tag, cpu;
+
+	if (!ctx)
+		return -1;
+
+	preempt_disable();
+
+	/* bad luck if we got preempted coming in here, should be rare */
Quoted 8 lines

block/blk-mq-tag.c static int __blk_mq_get_tag(struct blk_mq_alloc_data *data, · line 157

Quoted 4 lines
block/blk-mq-tag.c +4 -0
+	cpu = smp_processor_id();
+	if (unlikely(ctx->cpu != cpu)) {
+		ctx = data->ctx = __blk_mq_get_ctx(data->q, cpu);
+		data->hctx = blk_mq_map_queue(data->q, data->cmd_flags, ctx);

When blk_mq_get_tag_batch is called for getting driver tag, rq->mq_hctx has been bound to the current request in blk_mq_rq_ctx_init(), so looks we shouldn't change hctx here.

->ctx is also NULL for that case, so it gets skipped. Probably needs a comment...

I'll need to check if all cases are covered, the batched tags should be exclusive to the non-reserved, regular tags. Or just the scheduler tags, if a scheduler is attached. Not sure it makes a lot of sense to handle the scheduler case as well (and have two sets of batches, driver tags and scheduler tags), but if it does, we could extend it for that and just bail if we're not on the right ctx anymore. In all honesty, I wrote that above code without ever checking that it happens. It most certainly could, but I'd consider it a very low risk. So might be better to simply just return -1 for that case and ignore it.

Signature · 1 line
Jens Axboe
author maintainer
#7 lore raw

Re: [PATCH 4/4] blk-mq: allocate tags in batches

Quoted 26 lines · Jens Axboe
Quoted 13 lines · Ming Lei
Quoted 13 lines
block/blk-mq-tag.c +13 -0
+static int blk_mq_get_tag_batch(struct blk_mq_alloc_data *data)
+{
+	struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
+	struct sbitmap_queue *bt = &tags->bitmap_tags;
+	struct blk_mq_ctx *ctx = data->ctx;
+	int tag, cpu;
+
+	if (!ctx)
+		return -1;
+
+	preempt_disable();
+
+	/* bad luck if we got preempted coming in here, should be rare */
Quoted 8 lines

block/blk-mq-tag.c static int __blk_mq_get_tag(struct blk_mq_alloc_data *data, · line 157

Quoted 4 lines
block/blk-mq-tag.c +4 -0
+	cpu = smp_processor_id();
+	if (unlikely(ctx->cpu != cpu)) {
+		ctx = data->ctx = __blk_mq_get_ctx(data->q, cpu);
+		data->hctx = blk_mq_map_queue(data->q, data->cmd_flags, ctx);

When blk_mq_get_tag_batch is called for getting driver tag, rq->mq_hctx has been bound to the current request in blk_mq_rq_ctx_init(), so looks we shouldn't change hctx here.

->ctx is also NULL for that case, so it gets skipped. Probably needs a comment...

Quoted 9 lines

I'll need to check if all cases are covered, the batched tags should be exclusive to the non-reserved, regular tags. Or just the scheduler tags, if a scheduler is attached. Not sure it makes a lot of sense to handle the scheduler case as well (and have two sets of batches, driver tags and scheduler tags), but if it does, we could extend it for that and just bail if we're not on the right ctx anymore. In all honesty, I wrote that above code without ever checking that it happens. It most certainly could, but I'd consider it a very low risk. So might be better to simply just return -1 for that case and ignore it.

Something like this should it clearer and get rid of that special rare case of having been preempted on to a different CPU since we got the ctx assignment.

We could also just not care, I added the lock grab later on since we need it for flushing out batches. But we don't really need it on the allocation side if we just have preemption disabled, and we don't allocate requests from irq context. The lock is cheap enough since we're in that cacheline anyway, and it should never be contended.

https://git.kernel.dk/cgit/linux-block/commit/?h=for-5.6/block-test&id=d33686a37ca1e902271588b40e80d4b5f82640ce

Signature · 1 line
Jens Axboe