From: Jens Axboe <axboe@kernel.dk>
To: Peiyang He <peiyang_he@smail.nju.edu.cn>
Cc: ming.lei@redhat.com, fengnanchang@gmail.com,
linux-block@vger.kernel.org, stable@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] blk-mq: fix null-ptr-deref in blk_mq_run_hw_queues
Date: Wed, 2 Dec 2026 15:48:58 -0700 [thread overview]
Message-ID: <fcd94d7a-4514-4b46-b070-2d7011f8a52a@kernel.dk> (raw)
In-Reply-To: <F5E00FBB97BC76D9+20261202151340.2228227-1-peiyang_he@smail.nju.edu.cn>
On 12/2/26 8:13 AM, Peiyang He wrote:
> blk_mq_run_hw_queues() iterates over q->queue_hw_ctx via
> queue_for_each_hw_ctx() without synchronizing against concurrent
> __blk_mq_update_nr_hw_queues(). While the queue is frozen,
> __blk_mq_realloc_hw_ctxs() can set entries in q->queue_hw_ctx to NULL
> when shrinking nr_hw_queues or when hctx reallocation fails. A
> concurrent caller such as blk_mq_unquiesce_queue() can then get a NULL
> hctx from queue_hctx() and dereference hctx->state in
> blk_mq_hctx_stopped(), causing a NULL pointer dereference.
>
> Acquire q->q_usage_counter via percpu_ref_tryget() in
> blk_mq_run_hw_queues() and release it with blk_queue_exit(), similar
> to blk_mq_queue_tag_busy_iter(). This serializes the hctx iteration
> with queue freezing and hctx reallocation. If the queue has been fully
> frozen and q_usage_counter has reached zero, blk_mq_run_hw_queues()
> returns early without accessing q->queue_hw_ctx.
>
> Fixes: d0c98769ee7d ("blk-mq: use array manage hctx map instead of xarray")
> Cc: stable@vger.kernel.org
> Signed-off-by: Peiyang He <peiyang_he@smail.nju.edu.cn>
> ---
> Found by our research tool based on Syzkaller when fuzzing the nbd driver;
> we manually write the PoC, confirm this bug exists in the latest Linux
> and the fix is valid:
>
> BUG: KASAN: null-ptr-deref in instrument_atomic_read include/linux/instrumented.h:82 [inline]
> BUG: KASAN: null-ptr-deref in _test_bit include/asm-generic/bitops/instrumented-non-atomic.h:141 [inline]
> BUG: KASAN: null-ptr-deref in blk_mq_hctx_stopped block/blk-mq.h:242 [inline]
> BUG: KASAN: null-ptr-deref in blk_mq_run_hw_queues+0x1bd/0x390 block/blk-mq.c:2426
> Read of size 8 at addr 0000000000000050 by task syz.0.132/9244
>
> CPU: 1 UID: 0 PID: 9244 Comm: syz.0.132 Not tainted 7.1.0 #14 PREEMPT(full)
> Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
> Call Trace:
> <TASK>
> dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
> kasan_report+0x117/0x150 mm/kasan/report.c:595
> check_region_inline mm/kasan/generic.c:-1 [inline]
> kasan_check_range+0x3c7/0x3f0 mm/kasan/generic.c:200
> instrument_atomic_read include/linux/instrumented.h:82 [inline]
> _test_bit include/asm-generic/bitops/instrumented-non-atomic.h:141 [inline]
> blk_mq_hctx_stopped block/blk-mq.h:242 [inline]
> blk_mq_run_hw_queues+0x1bd/0x390 block/blk-mq.c:2426
> nbd_clear_que drivers/block/nbd.c:1054 [inline]
> nbd_clear_sock+0xb6/0x170 drivers/block/nbd.c:1428
> nbd_clear_sock_ioctl drivers/block/nbd.c:1575 [inline]
> __nbd_ioctl drivers/block/nbd.c:1603 [inline]
> nbd_ioctl+0x341/0xe60 drivers/block/nbd.c:1663
> blkdev_ioctl+0x5e8/0x750 block/ioctl.c:797
> vfs_ioctl fs/ioctl.c:51 [inline]
> __do_sys_ioctl fs/ioctl.c:597 [inline]
> __se_sys_ioctl+0x101/0x170 fs/ioctl.c:583
> do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
> do_syscall_64+0x15f/0x560 arch/x86/entry/syscall_64.c:94
> entry_SYSCALL_64_after_hwframe+0x77/0x7f
> RIP: 0033:0x7f5c289a6dcd
> Code: 04 25 28 00 00 00 48 89 45 c8 31 c0 48 8d 45 10 c7 45 b0 10 00 00 00 48 89 45 b8 48 8d 45 d0 48 89 45 c0 b8 10 00 00 00 0f 05 <89> c2 3d 00 f0 ff ff 77 1a 48 8b 45 c8 64 48 2b 04 25 28 00 00 00
> RSP: 002b:00007f5c273f70a0 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
> RAX: ffffffffffffffda RBX: 00007f5c2979ffe0 RCX: 00007f5c289a6dcd
> RDX: 0000000000000000 RSI: 000000000000ab04 RDI: 0000000000000008
> RBP: 00007f5c273f70f0 R08: 0000000000000000 R09: 00007f5c273f76c0
> R10: 0000000000000000 R11: 0000000000000246 R12: 00007f5c273f76c0
> R13: ffffffffffffffb0 R14: 000000000000000e R15: 00007f5c2979fdd0
> </TASK>
> ==================================================================
>
> block/blk-mq.c | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> diff --git a/block/blk-mq.c b/block/blk-mq.c
> index 54ccfc43c27c..bf129a6a77b5 100644
> --- a/block/blk-mq.c
> +++ b/block/blk-mq.c
> @@ -2412,6 +2412,14 @@ void blk_mq_run_hw_queues(struct request_queue *q, bool async)
> struct blk_mq_hw_ctx *hctx, *sq_hctx;
> unsigned long i;
>
> + /*
> + * __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.
> + */
> + if (!percpu_ref_tryget(&q->q_usage_counter))
> + return;
> +
> sq_hctx = NULL;
> if (blk_queue_sq_sched(q))
> sq_hctx = blk_mq_get_sq_hctx(q);
> @@ -2427,6 +2435,7 @@ void blk_mq_run_hw_queues(struct request_queue *q, bool async)
> !list_empty_careful(&hctx->dispatch))
> blk_mq_run_hw_queue(hctx, async);
> }
> + blk_queue_exit(q);
> }
> EXPORT_SYMBOL(blk_mq_run_hw_queues);
That's a hard pass, adding overhead to a fast path for a corner case in one
driver. Yes we use this in tag iteration, but that's not at all a fast path.
Please rethink this approach.
--
Jens Axboe
prev parent reply other threads:[~2026-12-02 22:49 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 15:13 [PATCH] blk-mq: fix null-ptr-deref in blk_mq_run_hw_queues Peiyang He
2026-12-02 17:41 ` Bart Van Assche
2026-12-02 22:48 ` Jens Axboe [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=fcd94d7a-4514-4b46-b070-2d7011f8a52a@kernel.dk \
--to=axboe@kernel.dk \
--cc=fengnanchang@gmail.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ming.lei@redhat.com \
--cc=peiyang_he@smail.nju.edu.cn \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox