blk-mq: use array manage hctx map instead of xarray
v2 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.
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]/
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: 4941a17751c99e17422be743c02c923ad706f888
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v2 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.
potential 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
@@ -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
@@ -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
@@ -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
@@ -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)
PATCH v2 2/2 blk-mq: fix potential uaf for 'queue_hw_ctx'
In reply to Fengnan ChangFrom: Fengnan Chang <[email protected]>
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 t2elevator_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 ->uafThis 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
@@ -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; }
block/blk-mq.h +11 -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]]; } +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
@@ -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
@@ -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)
Re: [PATCH v2 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx'
Quoted 70 lines · Fengnan Chang
From: Fengnan Chang <[email protected]>
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. undatesubmit_queuesthrough configfs fornull_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 ->uafThis 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]/
Signed-off-by Yu Kuai <[email protected]>Signed-off-by Fengnan Chang <[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@@ -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; }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 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; +}
If hctx is retrieved from old table, uaf will be triggered on hctx dereference after returning from queue_hctx().
So rcu read lock should be held anywhere for hctx deference.
Thanks,
Ming
Re: [PATCH v2 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx'
Quoted 86 lines · Ming Lei
Quoted 70 lines · Fengnan Chang
From: Fengnan Chang <[email protected]>
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. undatesubmit_queuesthrough configfs fornull_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 ->uafThis 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]/
Signed-off-by Yu Kuai <[email protected]>Signed-off-by Fengnan Chang <[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@@ -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; }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 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; +}If
hctxis retrieved from old table, uaf will be triggered onhctxdereference after returning fromqueue_hctx().
Quoted 1 line
So rcu read lock should be held anywhere for
hctxdeference.
oops, the rcu read lock only protects q->queue_hw_ctx, so this way is fine, sorry for the noise!
Thanks.
Ming
kernel test robot
build botRe: [PATCH v2 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx'
In reply to Fengnan Changkernel test robot noticed the following build errors:
Hi Fengnan,
kernel test robot noticed the following build errors:
[auto build test ERROR on 4941a17751c99e17422be743c02c923ad706f888]
url: https://github.com/intel-lab-lkp/linux/commits/Fengnan-Chang/blk-mq-use-array-manage-hctx-map-instead-of-xarray/20251127-094243 base: 4941a17751c99e17422be743c02c923ad706f888 patch link: https://lore.kernel.org/r/20251127013908.66118-3-fengnanchang%40gmail.com patch subject: [PATCH v2 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx' config: loongarch-allmodconfig (https://download.01.org/0day-ci/archive/20251128/[email protected]/config) compiler: clang version 19.1.7 (https://github.com/llvm/llvm-project cd708029e0b2869e80abe31ddb175f7c35361f90) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251128/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> drivers/block/rnbd/rnbd-clt.c:1324:2: error: call to undeclared function 'queue_hctx'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
1324 | queue_for_each_hw_ctx(dev->queue, hctx, i) {
| ^
include/linux/blk-mq.h:1004:17: note: expanded from macro 'queue_for_each_hw_ctx'
1004 | ({ hctx = queue_hctx((q), i); 1; }); (i)++)
| ^
>> drivers/block/rnbd/rnbd-clt.c:1324:2: error: incompatible integer to pointer conversion assigning to 'struct blk_mq_hw_ctx *' from 'int' [-Wint-conversion]
1324 | queue_for_each_hw_ctx(dev->queue, hctx, i) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/blk-mq.h:1004:15: note: expanded from macro 'queue_for_each_hw_ctx'
1004 | ({ hctx = queue_hctx((q), i); 1; }); (i)++)
| ^ ~~~~~~~~~~~~~~~~~~2 errors generated.
vim +/queue_hctx +1324 drivers/block/rnbd/rnbd-clt.c
f7a7a5c228d45e Jack Wang 2020-05-11 1317
f7a7a5c228d45e Jack Wang 2020-05-11 1318 static void rnbd_init_mq_hw_queues(struct rnbd_clt_dev *dev)
f7a7a5c228d45e Jack Wang 2020-05-11 1319 {
4f481208749a22 Ming Lei 2022-03-08 1320 unsigned long i;
f7a7a5c228d45e Jack Wang 2020-05-11 1321 struct blk_mq_hw_ctx *hctx;
f7a7a5c228d45e Jack Wang 2020-05-11 1322 struct rnbd_queue *q;
f7a7a5c228d45e Jack Wang 2020-05-11 1323
f7a7a5c228d45e Jack Wang 2020-05-11 @1324 queue_for_each_hw_ctx(dev->queue, hctx, i) {
f7a7a5c228d45e Jack Wang 2020-05-11 1325 q = &dev->hw_queues[i];
f7a7a5c228d45e Jack Wang 2020-05-11 1326 rnbd_init_hw_queue(dev, q, hctx);
f7a7a5c228d45e Jack Wang 2020-05-11 1327 hctx->driver_data = q;
f7a7a5c228d45e Jack Wang 2020-05-11 1328 }
f7a7a5c228d45e Jack Wang 2020-05-11 1329 }f7a7a5c228d45e Jack Wang 2020-05-11 1330
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v2 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx'
In reply to Fengnan Changkernel test robot noticed the following build warnings:
Hi Fengnan,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 4941a17751c99e17422be743c02c923ad706f888]
url: https://github.com/intel-lab-lkp/linux/commits/Fengnan-Chang/blk-mq-use-array-manage-hctx-map-instead-of-xarray/20251127-094243 base: 4941a17751c99e17422be743c02c923ad706f888 patch link: https://lore.kernel.org/r/20251127013908.66118-3-fengnanchang%40gmail.com patch subject: [PATCH v2 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx' config: arm64-randconfig-r131-20251128 (https://download.01.org/0day-ci/archive/20251128/[email protected]/config) compiler: aarch64-linux-gcc (GCC) 11.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251128/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
block/blk-mq-sysfs.c: note: in included file:
>> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu *
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu **
--
block/blk-mq-sched.c: note: in included file:
>> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu *
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu **
--
block/blk-mq-tag.c: note: in included file:
>> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu *
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu **
--
block/blk-mq-debugfs.c: note: in included file:
>> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu *
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu **
--
block/kyber-iosched.c: note: in included file (through block/elevator.h):
>> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu *
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu **
--
block/blk-mq.c:726:19: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct blk_mq_hw_ctx *hctx @@ got struct blk_mq_hw_ctx [noderef] __rcu * @@
block/blk-mq.c:726:19: sparse: expected struct blk_mq_hw_ctx *hctx
block/blk-mq.c:726:19: sparse: got struct blk_mq_hw_ctx [noderef] __rcu *
block/blk-mq.c:4514:41: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct blk_mq_hw_ctx **hctxs @@ got struct blk_mq_hw_ctx [noderef] __rcu **queue_hw_ctx @@
block/blk-mq.c:4514:41: sparse: expected struct blk_mq_hw_ctx **hctxs
block/blk-mq.c:4514:41: sparse: got struct blk_mq_hw_ctx [noderef] __rcu **queue_hw_ctx
block/blk-mq.c:4527:17: sparse: sparse: incompatible types in comparison expression (different address spaces):
block/blk-mq.c:4527:17: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu *
block/blk-mq.c:4527:17: sparse: struct blk_mq_hw_ctx [noderef] __rcu **
>> block/blk-mq.c:5192:48: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected struct blk_mq_hw_ctx *hctx @@ got struct blk_mq_hw_ctx [noderef] __rcu * @@ block/blk-mq.c:5192:48: sparse: expected struct blk_mq_hw_ctx *hctx block/blk-mq.c:5192:48: sparse: got struct blk_mq_hw_ctx [noderef] __rcu * block/blk-mq.c: note: in included file: >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces): block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu * block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu ** >> block/blk-mq.h:95:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu *[noderef] __rcu *
block/blk-mq.h:95:16: sparse: struct blk_mq_hw_ctx [noderef] __rcu **
vim +95 block/blk-mq.h
89
90 static inline struct blk_mq_hw_ctx *queue_hctx(struct request_queue *q, int id)
91 {
92 struct blk_mq_hw_ctx *hctx;
93
94 rcu_read_lock();
> 95 hctx = rcu_dereference(q->queue_hw_ctx)[id];
96 rcu_read_unlock();
97
98 return hctx;
99 }100
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v2 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx'
In reply to Fengnan Changkernel test robot noticed the following build warnings:
Hi Fengnan,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 4941a17751c99e17422be743c02c923ad706f888]
url: https://github.com/intel-lab-lkp/linux/commits/Fengnan-Chang/blk-mq-use-array-manage-hctx-map-instead-of-xarray/20251127-094243 base: 4941a17751c99e17422be743c02c923ad706f888 patch link: https://lore.kernel.org/r/20251127013908.66118-3-fengnanchang%40gmail.com patch subject: [PATCH v2 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx' config: arm-randconfig-002-20251128 (https://download.01.org/0day-ci/archive/20251128/[email protected]/config) compiler: arm-linux-gnueabi-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251128/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from drivers/block/rnbd/rnbd-clt.h:16,
from drivers/block/rnbd/rnbd-clt.c:19:
drivers/block/rnbd/rnbd-clt.c: In function 'rnbd_init_mq_hw_queues':
include/linux/blk-mq.h:1004:17: error: implicit declaration of function 'queue_hctx'; did you mean 'queue_work'? [-Werror=implicit-function-declaration]
({ hctx = queue_hctx((q), i); 1; }); (i)++)
^~~~~~~~~~
drivers/block/rnbd/rnbd-clt.c:1324:2: note: in expansion of macro 'queue_for_each_hw_ctx'
queue_for_each_hw_ctx(dev->queue, hctx, i) {
^~~~~~~~~~~~~~~~~~~~~
>> include/linux/blk-mq.h:1004:15: warning: assignment to 'struct blk_mq_hw_ctx *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
({ hctx = queue_hctx((q), i); 1; }); (i)++)
^
drivers/block/rnbd/rnbd-clt.c:1324:2: note: in expansion of macro 'queue_for_each_hw_ctx'
queue_for_each_hw_ctx(dev->queue, hctx, i) {
^~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors
--
In file included from rnbd-clt.h:16,
from rnbd-clt.c:19:
rnbd-clt.c: In function 'rnbd_init_mq_hw_queues':
include/linux/blk-mq.h:1004:17: error: implicit declaration of function 'queue_hctx'; did you mean 'queue_work'? [-Werror=implicit-function-declaration]
({ hctx = queue_hctx((q), i); 1; }); (i)++)
^~~~~~~~~~
rnbd-clt.c:1324:2: note: in expansion of macro 'queue_for_each_hw_ctx'
queue_for_each_hw_ctx(dev->queue, hctx, i) {
^~~~~~~~~~~~~~~~~~~~~
>> include/linux/blk-mq.h:1004:15: warning: assignment to 'struct blk_mq_hw_ctx *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
({ hctx = queue_hctx((q), i); 1; }); (i)++)
^
rnbd-clt.c:1324:2: note: in expansion of macro 'queue_for_each_hw_ctx'
queue_for_each_hw_ctx(dev->queue, hctx, i) {
^~~~~~~~~~~~~~~~~~~~~cc1: some warnings being treated as errors
vim +1004 include/linux/blk-mq.h
1001
1002 #define queue_for_each_hw_ctx(q, hctx, i) \
1003 for ((i) = 0; (i) < (q)->nr_hw_queues && \
> 1004 ({ hctx = queue_hctx((q), i); 1; }); (i)++)1005
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v2 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx'
In reply to Fengnan Changkernel test robot noticed the following build errors:
Hi Fengnan,
kernel test robot noticed the following build errors:
[auto build test ERROR on 4941a17751c99e17422be743c02c923ad706f888]
url: https://github.com/intel-lab-lkp/linux/commits/Fengnan-Chang/blk-mq-use-array-manage-hctx-map-instead-of-xarray/20251127-094243 base: 4941a17751c99e17422be743c02c923ad706f888 patch link: https://lore.kernel.org/r/20251127013908.66118-3-fengnanchang%40gmail.com patch subject: [PATCH v2 2/2] blk-mq: fix potential uaf for 'queue_hw_ctx' config: um-allyesconfig (https://download.01.org/0day-ci/archive/20251128/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251128/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from drivers/block/rnbd/rnbd-clt.h:16,
from drivers/block/rnbd/rnbd-clt.c:19:
drivers/block/rnbd/rnbd-clt.c: In function 'rnbd_init_mq_hw_queues':
>> include/linux/blk-mq.h:1004:24: error: implicit declaration of function 'queue_hctx' [-Wimplicit-function-declaration]
1004 | ({ hctx = queue_hctx((q), i); 1; }); (i)++)
| ^~~~~~~~~~
drivers/block/rnbd/rnbd-clt.c:1324:9: note: in expansion of macro 'queue_for_each_hw_ctx'
1324 | queue_for_each_hw_ctx(dev->queue, hctx, i) {
| ^~~~~~~~~~~~~~~~~~~~~
>> include/linux/blk-mq.h:1004:22: error: assignment to 'struct blk_mq_hw_ctx *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
1004 | ({ hctx = queue_hctx((q), i); 1; }); (i)++)
| ^
drivers/block/rnbd/rnbd-clt.c:1324:9: note: in expansion of macro 'queue_for_each_hw_ctx'
1324 | queue_for_each_hw_ctx(dev->queue, hctx, i) {
| ^~~~~~~~~~~~~~~~~~~~~vim +/queue_hctx +1004 include/linux/blk-mq.h
1001
1002 #define queue_for_each_hw_ctx(q, hctx, i) \
1003 for ((i) = 0; (i) < (q)->nr_hw_queues && \
> 1004 ({ hctx = queue_hctx((q), i); 1; }); (i)++)1005
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Jens Axboe
maintainerQuoted 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 inblk_mq_poll, we need usexa_loadto find corresponding hctx, this introduce some costs. In my test,xa_loadmay cost 3.8% cpu.
Quoted 7 lines
After revert previous change, eliminates the overhead of
xa_loadand can result in a 3% performance improvement.potentital use-after-free on
q->queue_hw_ctxcan be fixed by use rcu to avoid, same as Yu Kuai did in [1].
What changed in v2? Neither this cover letter nor the patches have any mention of that.
Signature · 1 line
Jens Axboe
Quoted 12 lines · Jens Axboe
Quoted 10 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 inblk_mq_poll, we need usexa_loadto find corresponding hctx, this introduce some costs. In my test,xa_loadmay cost 3.8% cpu.After revert previous change, eliminates the overhead of
xa_loadand can result in a 3% performance improvement.
Quoted 7 lines
Quoted 4 lines
potentital use-after-free on
q->queue_hw_ctxcan be fixed by use rcu to avoid, same as Yu Kuai did in [1].What changed in v2? Neither this cover letter nor the patches have any mention of that.
Sorry for miss change description. Two change in V2:
1. modify synchronize_rcu() to synchronize_rcu_expedited()
2. use rcu_dereference(q->queue_hw_ctx)[id] in queue_hctx to better read.
Quoted 4 lines
--
Jens Axboe
Jens Axboe
maintainerQuoted 27 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 inblk_mq_poll, we need usexa_loadto find corresponding hctx, this introduce some costs. In my test,xa_loadmay cost 3.8% cpu.After revert previous change, eliminates the overhead of
xa_loadand can result in a 3% performance improvement.potentital use-after-free on
q->queue_hw_ctxcan 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(-)
Quoted 1 line
base-commit:
4941a17751c99e17422be743c02c923ad706f888
Adding correct Yu address, was also wrong for v1 and somehow not corrected?
Signature · 1 line
Jens Axboe
Quoted 29 lines · Jens Axboe
Quoted 27 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 inblk_mq_poll, we need usexa_loadto find corresponding hctx, this introduce some costs. In my test,xa_loadmay cost 3.8% cpu.After revert previous change, eliminates the overhead of
xa_loadand can result in a 3% performance improvement.potentital use-after-free on
q->queue_hw_ctxcan 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(-)
Quoted 4 lines
Quoted 1 line
base-commit:
4941a17751c99e17422be743c02c923ad706f888Adding correct Yu address, was also wrong for v1 and somehow not corrected?
I didn't notice this, the error message was blocked in the spam folder.
Quoted 4 lines
--
Jens Axboe