Skip to contentSkip to conversation

blk-mq: use array manage hctx map instead of xarray

PATCH Applied Fengnan Chang 3 participants 9 messages Nov 28, 2025 – Dec 1, 2025 Block layer

lore
Share & tools
lore
#0 lore raw

v3 cover blk-mq: use array manage hctx map instead of xarray

After commit 4e5cc99e1e48 ("blk-mq: manage hctx map via xarray"), we use an xarray instead of array to store hctx, but in poll mode, each time in blk_mq_poll, we need use xa_load to find corresponding hctx, this introduce some costs. In my test, xa_load may cost 3.8% cpu.

After revert previous change, eliminates the overhead of xa_load and can result in a 3% performance improvement.

potentital use-after-free on q->queue_hw_ctx can be fixed by use rcu to avoid, same as Yu Kuai did in [1].

[1] https://lore.kernel.org/all/[email protected]/

v3:
fix build error and part sparse warnings, not all sparse warnings, because the queue is freezed in __blk_mq_update_nr_hw_queues, only need protect 'queue_hw_ctx' through rcu where it can be accessed without grabbing 'q_usage_counter'.

v2:
1. modify synchronize_rcu() to synchronize_rcu_expedited()
2. use rcu_dereference(q->queue_hw_ctx)[id] in queue_hctx to better read.

Fengnan Chang (2):
blk-mq: use array manage hctx map instead of xarray
blk-mq: fix potential uaf for 'queue_hw_ctx'

 block/blk-mq-tag.c     |  2 +-
 block/blk-mq.c         | 63 ++++++++++++++++++++++++++++--------------
 block/blk-mq.h         |  2 +-
 include/linux/blk-mq.h | 14 +++++++++-
 include/linux/blkdev.h |  2 +-
 5 files changed, 58 insertions(+), 25 deletions(-)

base-commit: 4941a17751c99e17422be743c02c923ad706f888

Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

PATCH v3 1/2 blk-mq: use array manage hctx map instead of xarray

After commit 4e5cc99e1e48 ("blk-mq: manage hctx map via xarray"), we use an xarray instead of array to store hctx, but in poll mode, each time in blk_mq_poll, we need use xa_load to find corresponding hctx, this introduce some costs. In my test, xa_load may cost 3.8% cpu.

This patch revert previous change, eliminates the overhead of xa_load and can result in a 3% performance improvement.

---
 block/blk-mq-tag.c     |  2 +-
 block/blk-mq.c         | 58 +++++++++++++++++++++++++++---------------
 block/blk-mq.h         |  2 +-
 include/linux/blk-mq.h |  3 ++-
 include/linux/blkdev.h |  2 +-
 5 files changed, 42 insertions(+), 25 deletions(-)
block/blk-mq-tag.c +1 -1
index 5b664dbdf655..33946cdb5716 100644
@@ -499,7 +499,7 @@ void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
 	int srcu_idx;
 
 	/*
-	 * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and hctx_table
+	 * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and queue_hw_ctx
 	 * while the queue is frozen. So we can use q_usage_counter to avoid
 	 * racing with it.
 	 */
block/blk-mq.c +37 -21

127 lines not on this page · raw message

block/blk-mq.h +1 -1
index c4fccdeb5441..80a3f0c2bce7 100644
@@ -84,7 +84,7 @@ static inline struct blk_mq_hw_ctx *blk_mq_map_queue_type(struct request_queue *
 							  enum hctx_type type,
 							  unsigned int cpu)
 {
-	return xa_load(&q->hctx_table, q->tag_set->map[type].mq_map[cpu]);
+	return q->queue_hw_ctx[q->tag_set->map[type].mq_map[cpu]];
 }
 
 static inline enum hctx_type blk_mq_get_hctx_type(blk_opf_t opf)
include/linux/blk-mq.h +2 -1
index b25d12545f46..0795f29dd65d 100644
@@ -1000,7 +1000,8 @@ static inline void *blk_mq_rq_to_pdu(struct request *rq)
 }
 
 #define queue_for_each_hw_ctx(q, hctx, i)				\
-	xa_for_each(&(q)->hctx_table, (i), (hctx))
+	for ((i) = 0; (i) < (q)->nr_hw_queues &&			\
+	     ({ hctx = (q)->queue_hw_ctx[i]; 1; }); (i)++)
 
 #define hctx_for_each_ctx(hctx, ctx, i)					\
 	for ((i) = 0; (i) < (hctx)->nr_ctx &&				\
include/linux/blkdev.h +1 -1
index 70b671a9a7f7..56328080ca09 100644
@@ -493,7 +493,7 @@ struct request_queue {
 
 	/* hw dispatch queues */
 	unsigned int		nr_hw_queues;
-	struct xarray		hctx_table;
+	struct blk_mq_hw_ctx	**queue_hw_ctx;
 
 	struct percpu_ref	q_usage_counter;
 	struct lock_class_key	io_lock_cls_key;
Signature · 1 line
2.39.5 (Apple Git-154)
#2 lore raw

PATCH v3 2/2 blk-mq: fix potential uaf for 'queue_hw_ctx'

In reply to Fengnan Chang

This is just apply Kuai's patch in [1] with mirror changes.

blk_mq_realloc_hw_ctxs() will free the 'queue_hw_ctx'(e.g. undate submit_queues through configfs for null_blk), while it might still be used from other context(e.g. switch elevator to none):

t1 t2
elevator_switch
blk_mq_unquiesce_queue
blk_mq_run_hw_queues
queue_for_each_hw_ctx

    // assembly code for hctx = (q)->queue_hw_ctx[i]
    mov    0x48(%rbp),%rdx -> read old queue_hw_ctx

					__blk_mq_update_nr_hw_queues
					 blk_mq_realloc_hw_ctxs
					  hctxs = q->queue_hw_ctx
					  q->queue_hw_ctx = new_hctxs
					  kfree(hctxs)
    movslq %ebx,%rax
    mov    (%rdx,%rax,8),%rdi ->uaf

This problem was found by code review, and I comfirmed that the concurrent scenario do exist(specifically 'q->queue_hw_ctx' can be changed during blk_mq_run_hw_queues()), however, the uaf problem hasn't been repoduced yet without hacking the kernel.

Sicne the queue is freezed in __blk_mq_update_nr_hw_queues(), fix the problem by protecting 'queue_hw_ctx' through rcu where it can be accessed without grabbing 'q_usage_counter'.

[1] https://lore.kernel.org/all/[email protected]/

---
 block/blk-mq.c         |  7 ++++++-
 include/linux/blk-mq.h | 13 ++++++++++++-
 include/linux/blkdev.h |  2 +-
 3 files changed, 19 insertions(+), 3 deletions(-)
block/blk-mq.c +6 -1
index eed12fab3484..0b8b72194003 100644
@@ -4524,7 +4524,12 @@ static void __blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
 		if (hctxs)
 			memcpy(new_hctxs, hctxs, q->nr_hw_queues *
 			       sizeof(*hctxs));
-		q->queue_hw_ctx = new_hctxs;
+		rcu_assign_pointer(q->queue_hw_ctx, new_hctxs);
+		/*
+		 * Make sure reading the old queue_hw_ctx from other
+		 * context concurrently won't trigger uaf.
+		 */
+		synchronize_rcu_expedited();
 		kfree(hctxs);
 		hctxs = new_hctxs;
 	}
include/linux/blk-mq.h +12 -1
index 0795f29dd65d..c16875b35521 100644
@@ -999,9 +999,20 @@ static inline void *blk_mq_rq_to_pdu(struct request *rq)
 	return rq + 1;
 }
 
+static inline struct blk_mq_hw_ctx *queue_hctx(struct request_queue *q, int id)
+{
+	struct blk_mq_hw_ctx *hctx;
+
+	rcu_read_lock();
+	hctx = rcu_dereference(q->queue_hw_ctx)[id];
+	rcu_read_unlock();
+
+	return hctx;
+}
+
 #define queue_for_each_hw_ctx(q, hctx, i)				\
 	for ((i) = 0; (i) < (q)->nr_hw_queues &&			\
-	     ({ hctx = (q)->queue_hw_ctx[i]; 1; }); (i)++)
+	     ({ hctx = queue_hctx((q), i); 1; }); (i)++)
 
 #define hctx_for_each_ctx(hctx, ctx, i)					\
 	for ((i) = 0; (i) < (hctx)->nr_ctx &&				\
include/linux/blkdev.h +1 -1
index 56328080ca09..e25d9802e08b 100644
@@ -493,7 +493,7 @@ struct request_queue {
 
 	/* hw dispatch queues */
 	unsigned int		nr_hw_queues;
-	struct blk_mq_hw_ctx	**queue_hw_ctx;
+	struct blk_mq_hw_ctx * __rcu *queue_hw_ctx;
 
 	struct percpu_ref	q_usage_counter;
 	struct lock_class_key	io_lock_cls_key;
Signature · 1 line
2.39.5 (Apple Git-154)
maintainer
#3 lore raw

Re: [PATCH v3 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx'

Quoted 8 lines · Fengnan Chang
include/linux/blk-mq.h +0 -0
index 0795f29dd65d..c16875b35521 100644
@@ -999,9 +999,20 @@ static inline void *blk_mq_rq_to_pdu(struct request *rq)
 	return rq + 1;
 }
 

include/linux/blk-mq.h static inline void *blk_mq_rq_to_pdu(struct request *rq) · line 1011

Quoted 10 lines
include/linux/blk-mq.h +10 -0
+static inline struct blk_mq_hw_ctx *queue_hctx(struct request_queue *q, int id)
+{
+	struct blk_mq_hw_ctx *hctx;
+
+	rcu_read_lock();
+	hctx = rcu_dereference(q->queue_hw_ctx)[id];
+	rcu_read_unlock();
+
+	return hctx;
+}

Should eg blk_mq_map_queue_type() use this helper now too?

Note: I've applied this, so anything beyond this v3 should be an incremental against the current tree.

Signature · 1 line
Jens Axboe
#4 lore raw

Re: [PATCH v3 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx'

Quoted 10 lines · Jens Axboe
Quoted 8 lines · Fengnan Chang
include/linux/blk-mq.h +0 -0
index 0795f29dd65d..c16875b35521 100644
@@ -999,9 +999,20 @@ static inline void *blk_mq_rq_to_pdu(struct request *rq)
      return rq + 1;
 }
 
Quoted 12 lines

include/linux/blk-mq.h static inline void *blk_mq_rq_to_pdu(struct request *rq) · line 1011

Quoted 10 lines
include/linux/blk-mq.h +10 -0
+static inline struct blk_mq_hw_ctx *queue_hctx(struct request_queue *q, int id)
+{
+     struct blk_mq_hw_ctx *hctx;
+
+     rcu_read_lock();
+     hctx = rcu_dereference(q->queue_hw_ctx)[id];
+     rcu_read_unlock();
+
+     return hctx;
+}

Should eg blk_mq_map_queue_type() use this helper now too?

You are right, now some caller of blk_mq_map_queue_type now didn't grab 'q_usage_counter', such as blk_mq_cpu_mapped_to_hctx. Also checked all other functions, no more missed cases.

New patch:
https://lore.kernel.org/linux-block/[email protected]/T/#u

Thanks.

Quoted 6 lines

Note: I've applied this, so anything beyond this v3 should be an incremental against the current tree.

--
Jens Axboe

#5 lore raw
In reply to Fengnan Chang

Hi,

Quoted 10 lines · Fengnan Chang

After commit 4e5cc99e1e48 ("blk-mq: manage hctx map via xarray"), we use an xarray instead of array to store hctx, but in poll mode, each time in blk_mq_poll, we need use xa_load to find corresponding hctx, this introduce some costs. In my test, xa_load may cost 3.8% cpu.

After revert previous change, eliminates the overhead of xa_load and can result in a 3% performance improvement.

potentital use-after-free on q->queue_hw_ctx can be fixed by use rcu to avoid, same as Yu Kuai did in [1].

Hope I'm not too late for the party. I'm not against for this set, just wonder have we considered changing to store hctx directly in bio for blk-mq devices, are we strongly against to increase bio size?

Quoted 26 lines

[1] https://lore.kernel.org/all/[email protected]/

v3:
fix build error and part sparse warnings, not all sparse warnings, because the queue is freezed in __blk_mq_update_nr_hw_queues, only need protect 'queue_hw_ctx' through rcu where it can be accessed without grabbing 'q_usage_counter'.

v2:
1. modify synchronize_rcu() to synchronize_rcu_expedited()
2. use rcu_dereference(q->queue_hw_ctx)[id] in queue_hctx to better read.

Fengnan Chang (2):
blk-mq: use array manage hctx map instead of xarray
blk-mq: fix potential uaf for 'queue_hw_ctx'

  block/blk-mq-tag.c     |  2 +-
  block/blk-mq.c         | 63 ++++++++++++++++++++++++++++--------------
  block/blk-mq.h         |  2 +-
  include/linux/blk-mq.h | 14 +++++++++-
  include/linux/blkdev.h |  2 +-
  5 files changed, 58 insertions(+), 25 deletions(-)

base-commit: 4941a17751c99e17422be743c02c923ad706f888

Signature · 2 lines
Thanks,
Kuai
#6 lore raw
Quoted 12 lines · Yu Kuai

Hi,

Quoted 8 lines · Fengnan Chang

After commit 4e5cc99e1e48 ("blk-mq: manage hctx map via xarray"), we use an xarray instead of array to store hctx, but in poll mode, each time in blk_mq_poll, we need use xa_load to find corresponding hctx, this introduce some costs. In my test, xa_load may cost 3.8% cpu.

After revert previous change, eliminates the overhead of xa_load and can result in a 3% performance improvement.

Quoted 6 lines
Quoted 2 lines

potentital use-after-free on q->queue_hw_ctx can be fixed by use rcu to avoid, same as Yu Kuai did in [1].

Hope I'm not too late for the party. I'm not against for this set, just wonder have we considered changing to store hctx directly in bio for blk-mq devices, are we strongly against to increase bio size?

I've thought about that put the hctx in the bio, it's a better way, but increasing the size of the bio, which is now exactly 128 bytes.

Quoted 31 lines
Quoted 26 lines

[1] https://lore.kernel.org/all/[email protected]/

v3:
fix build error and part sparse warnings, not all sparse warnings, because the queue is freezed in __blk_mq_update_nr_hw_queues, only need protect 'queue_hw_ctx' through rcu where it can be accessed without grabbing 'q_usage_counter'.

v2:
1. modify synchronize_rcu() to synchronize_rcu_expedited()
2. use rcu_dereference(q->queue_hw_ctx)[id] in queue_hctx to better read.

Fengnan Chang (2):
blk-mq: use array manage hctx map instead of xarray
blk-mq: fix potential uaf for 'queue_hw_ctx'

  block/blk-mq-tag.c     |  2 +-
  block/blk-mq.c         | 63 ++++++++++++++++++++++++++++--------------
  block/blk-mq.h         |  2 +-
  include/linux/blk-mq.h | 14 +++++++++-
  include/linux/blkdev.h |  2 +-
  5 files changed, 58 insertions(+), 25 deletions(-)
Quoted 1 line

base-commit: 4941a17751c99e17422be743c02c923ad706f888

--
Thanks,
Kuai

maintainer
#7 lore raw
Quoted 16 lines · fengnan chang
Quoted 15 lines · Yu Kuai

Hi,

Quoted 10 lines · Fengnan Chang

After commit 4e5cc99e1e48 ("blk-mq: manage hctx map via xarray"), we use an xarray instead of array to store hctx, but in poll mode, each time in blk_mq_poll, we need use xa_load to find corresponding hctx, this introduce some costs. In my test, xa_load may cost 3.8% cpu.

After revert previous change, eliminates the overhead of xa_load and can result in a 3% performance improvement.

potentital use-after-free on q->queue_hw_ctx can be fixed by use rcu to avoid, same as Yu Kuai did in [1].

Quoted 6 lines
Quoted 3 lines

Hope I'm not too late for the party. I'm not against for this set, just wonder have we considered changing to store hctx directly in bio for blk-mq devices, are we strongly against to increase bio size?

I've thought about that put the hctx in the bio, it's a better way, but increasing the size of the bio, which is now exactly 128 bytes.

Don't think it's worth it for polled IO.

Signature · 1 line
Jens Axboe
maintainer Applied
#8 lore raw
In reply to Fengnan Chang
Quoted 9 lines · Fengnan Chang

After commit 4e5cc99e1e48 ("blk-mq: manage hctx map via xarray"), we use an xarray instead of array to store hctx, but in poll mode, each time in blk_mq_poll, we need use xa_load to find corresponding hctx, this introduce some costs. In my test, xa_load may cost 3.8% cpu.

After revert previous change, eliminates the overhead of xa_load and can result in a 3% performance improvement.

[...]

Applied, thanks!

[1/2] blk-mq: use array manage hctx map instead of xarray

      commit: d0c98769ee7d5db8d699a270690639cde1766cd4
[2/2] blk-mq: fix potential uaf for 'queue_hw_ctx'
      commit: 89e1fb7ceffd898505ad7fa57acec0585bfaa2cc

Best regards,

Signature · 1 line
Jens Axboe