Skip to contentSkip to conversation

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

PATCH Superseded Fengnan Chang 4 participants 8 messages Nov 20, 2025 – Nov 21, 2025 Block layer

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

From: Fengnan Chang <[email protected]>

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.

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]/

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         | 13 ++++++++-
 include/linux/blk-mq.h |  3 +-
 include/linux/blkdev.h |  2 +-
 5 files changed, 58 insertions(+), 25 deletions(-)

base-commit: 4a0c9b3391999818e2c5b93719699b255be1f682
prerequisite-patch-id: dc4aa50f50259c7ba5a6a5439a6beecfa89897fa
prerequisite-patch-id: 8ffb2499d24e08d652438fd9dac815b8d4c0d8b3

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

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

From: Fengnan Chang <[email protected]>

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.

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

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

---
 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
Quoted 2 lines · Fengnan Chang

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

Does this mean that this patch triggers a use-after-free? If so, please include the fix for the use-after-free in this patch.

Thanks,

Bart.

#3 lore raw
Quoted 8 lines

From: "Bart Van Assche"<[email protected]>
Date:  Fri, Nov 21, 2025, 00:41
Subject:  Re: [PATCH 1/2] blk-mq: use array manage hctx map instead of xarray
To: "Fengnan Chang"<[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>
Cc: "Fengnan Chang"<[email protected]>
On 11/19/25 7:16 PM, Fengnan Chang wrote:

Quoted 8 lines
Quoted 3 lines

use-after-free on q->queue_hw_ctx can be fixed by use rcu to avoid in

next patch, same as Yu Kuai did in [1],


Does this mean that this patch triggers a use-after-free? If so, please

include the fix for the use-after-free in this patch.

No, this won't tigger use-after-free,  just potentially risky. "The uaf problem hasn't been repoduced yet without hacking the kernel."

Quoted 6 lines


Thanks,


Bart.

#4 lore raw

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

In reply to Fengnan Chang

From: Fengnan Chang <[email protected]>

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

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 ++++++-
 block/blk-mq.h         | 11 +++++++++++
 include/linux/blk-mq.h |  2 +-
 include/linux/blkdev.h |  2 +-
 4 files changed, 19 insertions(+), 3 deletions(-)
block/blk-mq.c +6 -1
index eed12fab3484..82195f22befd 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();
 		kfree(hctxs);
 		hctxs = new_hctxs;
 	}
block/blk-mq.h +11 -0
index 80a3f0c2bce7..ccd8c08524a4 100644
@@ -87,6 +87,17 @@ static inline struct blk_mq_hw_ctx *blk_mq_map_queue_type(struct request_queue *
 	return q->queue_hw_ctx[q->tag_set->map[type].mq_map[cpu]];
 }
 
+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;
+}
+
 static inline enum hctx_type blk_mq_get_hctx_type(blk_opf_t opf)
 {
 	enum hctx_type type = HCTX_TYPE_DEFAULT;
include/linux/blk-mq.h +1 -1
index 0795f29dd65d..484baf91fd91 100644
@@ -1001,7 +1001,7 @@ static inline void *blk_mq_rq_to_pdu(struct request *rq)
 
 #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..f50f2d5eeb55 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
#5 lore raw

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

Quoted 7 lines
block/blk-mq.c +0 -0
index eed12fab3484..82195f22befd 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 *

block/blk-mq.c static void __blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, · line 4534

Quoted 10 lines
block/blk-mq.c +6 -1
 			       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();
 		kfree(hctxs);
 		hctxs = new_hctxs;

Might make sense to use the expedited version here, to avoid odd ball cases that end up doing this for tons of devices.

Quoted 8 lines
block/blk-mq.h +0 -0
index 80a3f0c2bce7..ccd8c08524a4 100644
@@ -87,6 +87,17 @@ static inline struct blk_mq_hw_ctx *blk_mq_map_queue_type(struct request_queue *
 	return q->queue_hw_ctx[q->tag_set->map[type].mq_map[cpu]];
 }
 

block/blk-mq.h static inline struct blk_mq_hw_ctx *blk_mq_map_queue_type(struct request_queue * · line 99

Quoted 10 lines
block/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;
+}

I think that'd read a lot better if the type was **hctx and you just return *hctx instead.

Signature · 1 line
Jens Axboe
#6 lore raw

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

Quoted 78 lines

From: "Jens Axboe"<[email protected]>
Date:  Thu, Nov 20, 2025, 23:01
Subject:  Re: [PATCH 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx'
To: "Fengnan Chang"<[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>
Cc: "Fengnan Chang"<[email protected]>

Quoted 26 lines
block/blk-mq.c +0 -0

index eed12fab3484..82195f22befd 100644

--- a/block/blk-mq.c

+++ b/block/blk-mq.c

block/blk-mq.c +0 -0
@@ -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.

Quoted 7 lines

+                 */

+                synchronize_rcu();

                 kfree(hctxs);

                 hctxs = new_hctxs;


Might make sense to use the expedited version here, to avoid odd ball

cases that end up doing this for tons of devices.

Quoted 31 lines
block/blk-mq.h +0 -0

index 80a3f0c2bce7..ccd8c08524a4 100644

--- a/block/blk-mq.h

+++ b/block/blk-mq.h

block/blk-mq.h +0 -0
@@ -87,6 +87,17 @@ static inline struct blk_mq_hw_ctx *blk_mq_map_queue_type(struct request_queue *
 
          return q->queue_hw_ctx[q->tag_set->map[type].mq_map[cpu]];
 
  }
 

block/blk-mq.h +8 -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();
 
+
Quoted 8 lines
Quoted 3 lines

+        return hctx;

+}


I think that'd read a lot better if the type was **hctx and you just

return *hctx instead.

Do you means like this ?
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;
}

It's seems not right, queue_hw_ctx is protected, after unlock to access may cause UAF.
How about  rcu_dereference(q->queue_hw_ctx)[id]; ?

Quoted 5 lines


--

Jens Axboe

maintainer
#7 lore raw
In reply to Fengnan Chang
Quoted 7 lines · Fengnan Chang

From: Fengnan Chang <[email protected]>

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.

Quoted 7 lines

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

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]/

Thanks for looking into this, it is indeed not ideal currently after that change. I've noticed the same. Just a few minor notes on the patches, please send a v2.

Signature · 1 line
Jens Axboe