From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796251743; b=K/LxdaKyuGGFUJEoZjbTvYvVwu+U2Ghx09KQO/wqghbkNeOY7/qDItAeEtD6q8jTes/8 SRa3TZN+tlUYdrqJ/KjdqSVzBryLnch4AHEA7NRCGlJuZ2bAYNYCqnet+SoJiVMnynldn xuuk5jJLpLYBYtrxu0ipQ++kcZHookKAPPjtN8uaGxnJfALTeHt0WX5PKsGuwRdWkDxq9 WdYBInPT+tkrMSQ0UcuiBU+DXc5UJqCG2KYks4rbuTn3QTHZlVKTiBFT7kQ4YwK4yz0m8 3ajhmltPWQYMr7CzXxdTPolAdh1CJud2RkVmSdoE7QCdr2GUvL2ms6S2Kvd3j3/uu5A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796251743; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References: Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=Cgw0Gtf5y4mfh1CQiaytjGiGngspBHhIaPR2/eEUoW0=; b=fkp6yMDFQstO7frdL9VhKgSe1THK1hfvXzjrzZkQXVXUu9g28sz/42S09d5iVFdQWUWf 4A3Ro8s5FVEcIi76wI1QGASXF8D90PnW21pwNJHfK7ZlQWYAbmmK+WV3uLxSb5LrLIhaT QHHNmP74nRFP2FcPoJ/7ROXSO0cLKlSCRnsQaZld9QCUsOMcnBxt2YMkmf3qO1NvMrgUw SMefjU43Dqdv57/WZHXqZEUihgFwt9H3oP0v+oOu9Eca/WhcXGFCyNxLS/QqwKp64meDx LBXYJKujO60Nk9gM1cyh8tNFXThAjhn49ogVDqRA1eai/kxEZMPTo2++tESk+mgwkhA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass header.d=kernel.dk header.i=@kernel.dk header.a=rsa-sha256 header.s=google header.b=VmIhIN1a; arc=none smtp.remote-ip=74.125.82.173 Received: from mail-dy1-f173.google.com (mail-dy1-f173.google.com [74.125.82.173]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 554D2606A322 for ; Wed, 2 Dec 2026 22:49:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 554D2606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.dk header.i=@kernel.dk header.a=rsa-sha256 header.s=google header.b=VmIhIN1a Received: by mail-dy1-f173.google.com with SMTP id 5a478bee46e88-385d8a00598so23575338eec.1 for ; Wed, 02 Dec 2026 14:49:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796251741; x=1796856541; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Cgw0Gtf5y4mfh1CQiaytjGiGngspBHhIaPR2/eEUoW0=; b=VmIhIN1aag9eil9XwgLUoPYmIU9hVbl1musS1Q+0kS1zZLgM4BciSObVc84yYoqlFF yRvHQcCBSmefC0DKLwWHpmb+SADU5PbXat3shd9J1cWijUqjWBeZXkymd77t9EP/s2/a b3cMqUEEEQkn7zxtJ6wWwlfZAYnTNDIV7iWSki/GsmAsvQVbuuiTtmhT9fO0qmt/ED0O iiD/DOLKhAuEd9wari7HkB+WfJGux1SBuYksqEjhzEpc8oiLdZalA2F/hny7o5K2M1T/ QvfBYDnK5T8Q5Ej3ZT6gDlXSRh81I3weBAZnAG7KssJ9PL1E+Xmt3VA2iq+nywtwoanU IMvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796251741; x=1796856541; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Cgw0Gtf5y4mfh1CQiaytjGiGngspBHhIaPR2/eEUoW0=; b=Mhn34prQkWU2acNUaOkk9zoMynn2nSpQbcA8xnUaYcGdkgSp7zhUbhio5COXeR9Tsp rZ2C8/rcKfR3lcRYvWVGvWTwjNqheuFPenIBgb8f7aJOV6cgDy/W+k3rWySpW72xpZqx DIGrs3qW1tQrlZ5WvJWHWuBXqxXSqOnqEH/GtqgcTiwOMRNPf7GphjT+YmBYmQb9PMdQ GyJZ0zyYovF/fpo8KeCgbKoWbpFHXpwB05zu8XzTslUYw5kKaG9dA4mCRL3hwlK8ZCRM T377a0fmKpCGQyDT+fbngFeZ4X5EGKBU2E+X2WmH8CHtAFm4/2yeFbdWTkPGSC7YZzsd +4Qw== X-Forwarded-Encrypted: i=1; AL+hErFyfL3SG6Cyh1gpjqgR5iNg805zYzmmOhjV02V91pizg/w6dlt3n1RPeN3r//JmYLuraKSwn9c5LyTtWrQ=@vger.kernel.org X-Gm-Message-State: APuU4Z8rJGxpghtOtyqSUn/YAhPp9DtnpeljR3YMzrEfFhexX6Ua4rcL MXnD6fyShExUDCEGrmikNtRssxRxGwc0f1vvYwC8B0Z+O3ZcNzTJ57Mm84W0EWocqQc= X-Gm-Gg: AZFdTZfWCwSwoR7Ve+E0yAqa+Xuy1YZ1htBaI0LAOdaazmexcq7TsZaybCm1gWFDBgW u+9zzHfaNTiUtfR+Vg9VaMVrVYTyUtfJpHgkypgudythzdORVTDIlbYs/pTOMxuAdy/kIU1xPUf iS324BBuql+p7T3g47OR7ZCmbfp637vjAWwakB6H2AGpwgUFWOO3QYrmRx7Xqz9rSkgVwsXVQSn jDNnqdfGolLp2er/0iA2KImKpNmJvpK9j5CS/B+ehBdCLQFuvOIFdETUaCdNjUdrtMoU3LWe3cB hUpPYpnXDwPT51IQO2/6DugpvydkO5NCHgnKzhf3L1U+xa+zTlRe3kePWJxPo4uJoB5Swk66D51 frlxMilIpVrb2zZPFz/4cwkPjFPxdDMRV8RF/DNzY0zqhbc8F6SrlbTQm6w4D9829r4sTM8/Zu2 eaIOJwyA+6qA0/XvP0uS+MiecKp5BY99ju4ERWBt3O582ZL6+7CVSWQ450Ppu1CkSnNuGnJ0rOW NZ6xqWhNTgLMSFWqgVMdw5j6Idrqo1Fv0hAw5io/XCY7M/cdjqqUpTbasP3OKeJQhjpG5X3n7lI af6GGNLxSwBx5j6njVjoC1zCsP0snjaxI8wRQMtv2qyMNE24ObtXzJT9Z+4JEXDdSe9NPlkKUqN q2C/KDZ32wEUWMDYVZe+jlmQHmOUyhA6Z+9yWHB9WMP96ng2Y8HN6/xG5gyUyRUmoJ9LB7A== X-Received: by 2002:a05:7300:7316:b0:386:c5d3:7300 with SMTP id 5a478bee46e88-386c5d37603mr38265763eec.12.1796251740794; Wed, 02 Dec 2026 14:49:00 -0800 (PST) Received: from [192.168.1.142] ([198.8.77.135]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385672c6f31sm119520673eec.15.2026.12.02.14.48.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 02 Dec 2026 14:48:59 -0800 (PST) Message-ID: Date: Wed, 2 Dec 2026 15:48:58 -0700 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] blk-mq: fix null-ptr-deref in blk_mq_run_hw_queues To: Peiyang He Cc: ming.lei@redhat.com, fengnanchang@gmail.com, linux-block@vger.kernel.org, stable@vger.kernel.org, linux-kernel@vger.kernel.org References: Content-Language: en-US From: Jens Axboe In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 > --- > 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: > > 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 > > ================================================================== > > 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