Skip to content

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

Closed
ghost wants to merge 3 commits into
linus-master_basefrom
series/1025620=>linus-master
Closed

ghost wants to merge 3 commits into
linus-master_basefrom
series/1025620=>linus-master

Conversation

@ghost

@ghost ghost commented Nov 20, 2025

Copy link
Copy Markdown

Pull request for series with
subject: blk-mq: use array manage hctx map instead of xarray
version: 1
url: https://patchwork.kernel.org/project/linux-block/list/?series=1025620

@ghost

ghost commented Nov 20, 2025

Copy link
Copy Markdown
Author

@ghost
ghost force-pushed the linus-master_base branch from 4458758 to 6f43942 Compare November 21, 2025 09:45
@ghost

ghost commented Nov 21, 2025

Copy link
Copy Markdown
Author

Fengnan Chang added 2 commits November 21, 2025 18:54
After commit 4e5cc99 ("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]/

Signed-off-by: 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]/

Signed-off-by: Yu Kuai <[email protected]>
Signed-off-by: Fengnan Chang <[email protected]>
@ghost
ghost force-pushed the series/1025620=>linus-master branch from 5e5954e to 3689420 Compare November 21, 2025 09:54
@ghost
ghost force-pushed the linus-master_base branch from 6f43942 to ec9caac Compare December 8, 2025 05:15
@ghost

ghost commented Dec 8, 2025

Copy link
Copy Markdown
Author

Upstream branch: c2f2b01
series: https://patchwork.kernel.org/project/linux-block/list/?series=1028582
version: 3

Pull request is NOT updated. Failed to apply https://patchwork.kernel.org/project/linux-block/list/?series=1028582
error message:

Cmd('git') failed due to: exit code(128)
  cmdline: git am --3way
  stdout: 'Applying: blk-mq: use array manage hctx map instead of xarray
Using index info to reconstruct a base tree...
M	block/blk-mq-tag.c
M	block/blk-mq.c
M	block/blk-mq.h
M	include/linux/blk-mq.h
M	include/linux/blkdev.h
Falling back to patching base and 3-way merge...
Auto-merging include/linux/blkdev.h
CONFLICT (content): Merge conflict in include/linux/blkdev.h
Auto-merging include/linux/blk-mq.h
CONFLICT (content): Merge conflict in include/linux/blk-mq.h
Auto-merging block/blk-mq.h
CONFLICT (content): Merge conflict in block/blk-mq.h
Auto-merging block/blk-mq.c
CONFLICT (content): Merge conflict in block/blk-mq.c
Patch failed at 0001 blk-mq: use array manage hctx map instead of xarray'
  stderr: 'error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"'

conflict:

diff --cc block/blk-mq.c
index 4e96bb246247,eed12fab3484..000000000000
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@@ -4535,12 -4524,7 +4535,16 @@@ static void __blk_mq_realloc_hw_ctxs(st
  		if (hctxs)
  			memcpy(new_hctxs, hctxs, q->nr_hw_queues *
  			       sizeof(*hctxs));
++<<<<<<< HEAD
 +		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();
++=======
+ 		q->queue_hw_ctx = new_hctxs;
++>>>>>>> blk-mq: use array manage hctx map instead of xarray
  		kfree(hctxs);
  		hctxs = new_hctxs;
  	}
diff --cc block/blk-mq.h
index aa15d31aaae9,80a3f0c2bce7..000000000000
--- a/block/blk-mq.h
+++ b/block/blk-mq.h
@@@ -84,7 -84,7 +84,11 @@@ static inline struct blk_mq_hw_ctx *blk
  							  enum hctx_type type,
  							  unsigned int cpu)
  {
++<<<<<<< HEAD
 +	return queue_hctx((q), (q->tag_set->map[type].mq_map[cpu]));
++=======
+ 	return q->queue_hw_ctx[q->tag_set->map[type].mq_map[cpu]];
++>>>>>>> blk-mq: use array manage hctx map instead of xarray
  }
  
  static inline enum hctx_type blk_mq_get_hctx_type(blk_opf_t opf)
diff --cc include/linux/blk-mq.h
index eb7254b3dddd,0795f29dd65d..000000000000
--- a/include/linux/blk-mq.h
+++ b/include/linux/blk-mq.h
@@@ -1015,20 -999,9 +1015,24 @@@ static inline void *blk_mq_rq_to_pdu(st
  	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 &&			\
++<<<<<<< HEAD
 +	     ({ hctx = queue_hctx((q), i); 1; }); (i)++)
++=======
+ 	     ({ hctx = (q)->queue_hw_ctx[i]; 1; }); (i)++)
++>>>>>>> blk-mq: use array manage hctx map instead of xarray
  
  #define hctx_for_each_ctx(hctx, ctx, i)					\
  	for ((i) = 0; (i) < (hctx)->nr_ctx &&				\
diff --cc include/linux/blkdev.h
index 72e34acd439c,56328080ca09..000000000000
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@@ -503,7 -493,7 +503,11 @@@ struct request_queue 
  
  	/* hw dispatch queues */
  	unsigned int		nr_hw_queues;
++<<<<<<< HEAD
 +	struct blk_mq_hw_ctx * __rcu *queue_hw_ctx;
++=======
+ 	struct blk_mq_hw_ctx	**queue_hw_ctx;
++>>>>>>> blk-mq: use array manage hctx map instead of xarray
  
  	struct percpu_ref	q_usage_counter;
  	struct lock_class_key	io_lock_cls_key;

@ghost

ghost commented Dec 28, 2025

Copy link
Copy Markdown
Author

At least one diff in series https://patchwork.kernel.org/project/linux-block/list/?series=1025620 irrelevant now for [{'archived': False, 'project': 241}] search patterns

@ghost ghost closed this Dec 28, 2025
@ghost
ghost deleted the series/1025620=>linus-master branch January 9, 2026 05:03
ghost pushed a commit that referenced this pull request Aug 2, 2026
…exception()

The trace_kvm_inj_exception tracepoint takes as arguments the exception
vector, whether the exception has an error code (and subsequently, the
error code), and whether it is being reinjected.  Because '0' is a valid
error code, KVM uses __print_symbolic() to format the error code as a
string to avoid printing the error code entirely if the exception doesn't
have an error code (see commit 21d4c57 ("KVM: x86: Print error code
in exception injection tracepoint iff valid").

KVM's abuse of __print_symbolic() was all fine and dandy, until commit
754e38d ("tracing: Use explicit array size instead of sentinel
elements in symbol printing") reworked the printing to avoid terminating
the arrays with NULL/0 values, and missed KVM's clever use of not-quite
empty array of symbols.

  BUG: kernel NULL pointer dereference, address: 0000000000000000
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page
  PGD 0 P4D 0
  Oops: Oops: 0000 [#1] SMP
  CPU: 20 UID: 0 PID: 791 Comm: less Not tainted 7.2.0-rc2 #401 PREEMPT
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
  RIP: 0010:strlen+0x0/0x20
  Call Trace:
   <TASK>
   trace_seq_puts+0x18/0x80
   trace_print_symbols_seq+0x68/0xa0
   trace_raw_output_kvm_inj_exception+0x64/0xf0 [kvm]
   s_show+0x47/0x110
   seq_read_iter+0x2a5/0x4c0
   seq_read+0xfd/0x130
   vfs_read+0xb6/0x330
   ? vfs_write+0x2f2/0x3f0
   ksys_read+0x61/0xd0
   do_syscall_64+0xb7/0x570
   entry_SYSCALL_64_after_hwframe+0x4b/0x53
  RIP: 0033:0x7ff283714862
   </TASK>

Simply drop the dummy array entirely, so that __print_symbolic() generates
a truly empty array.

Signed-off-by: Daniel Paziyski <[email protected]>
Fixes: 754e38d ("tracing: Use explicit array size instead of sentinel elements in symbol printing")
Reviewed-by: Thomas Weißschuh <[email protected]>
Link: https://patch.msgid.link/[email protected]
[sean: massage changelog, add splat, add Fixes, cc stable]
Signed-off-by: Sean Christopherson <[email protected]>
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant