mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Cheng Lingfei <chenglingfei@foxmail.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
	 syzbot+b6fd00508cc05a5c003c@syzkaller.appspotmail.com,
	 Cheng Lingfei <chenglingfei@foxmail.com>
Subject: [PATCH] null_blk: fix sleeping in atomic context with shared_tags and memory_backed
Date: Tue, 25 Aug 2026 14:16:51 +0800	[thread overview]
Message-ID: <tencent_F7A3786EE31A7EC420F9C05E74BA446FB109@qq.com> (raw)

When a null_blk device is configured with both shared_tags and
memory_backed (or blocking), the zone lock uses a mutex which may sleep.
However, the global shared tag_set only checks the g_blocking module
parameter to decide whether to set BLK_MQ_F_BLOCKING, ignoring the
per-device dev->blocking flag. Without BLK_MQ_F_BLOCKING, blk-mq uses
rcu_read_lock() instead of srcu_read_lock() to protect the dispatch
path, making it illegal to sleep.

This leads to a "sleeping function called from invalid context" BUG when
I/O is submitted during add_disk() partition scanning, as the call chain
enters null_lock_zone() -> mutex_lock() inside an RCU read-side critical
section.

Fix this by disabling shared_tags in null_validate_conf() when the
device needs blocking but the global tag_set was not initialized with
BLK_MQ_F_BLOCKING (i.e., g_blocking is false). The device falls back to
a per-device tag_set which correctly propagates dev->blocking to
BLK_MQ_F_BLOCKING, allowing blk-mq to use SRCU and permit sleeping in
the dispatch path.

Users who need both shared_tags and memory_backed simultaneously should
load the module with the blocking=1 parameter so the global tag_set is
initialized with BLK_MQ_F_BLOCKING from the start.

Reported-by: syzbot+b6fd00508cc05a5c003c@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=b6fd00508cc05a5c003c
Tested-by: syzbot+b6fd00508cc05a5c003c@syzkaller.appspotmail.com
Signed-off-by: Cheng Lingfei <chenglingfei@foxmail.com>
---
 drivers/block/null_blk/main.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/block/null_blk/main.c b/drivers/block/null_blk/main.c
index f8c0fd57e041..0dc7fd106db8 100644
--- a/drivers/block/null_blk/main.c
+++ b/drivers/block/null_blk/main.c
@@ -1912,6 +1912,12 @@ static int null_validate_conf(struct nullb_device *dev)
 		dev->blocking = true;
 	else /* cache is meaningless */
 		dev->cache_size = 0;
+
+	if (dev->shared_tags && dev->blocking && !g_blocking) {
+		pr_info("shared_tags disabled: memory_backed/blocking device requires per-device tag set for BLK_MQ_F_BLOCKING\n");
+		dev->shared_tags = false;
+	}
+
 	dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024,
 						dev->cache_size);
 	dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps);

---
base-commit: 2f1baf1fc8929e6c48370be543ad028ac7ad4131
change-id: 20260825-b4-null-blk-sleep-fix-64a1a6323e97

Best regards,
--  
Cheng Lingfei <chenglingfei@foxmail.com>


                 reply	other threads:[~2026-08-25  6:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=tencent_F7A3786EE31A7EC420F9C05E74BA446FB109@qq.com \
    --to=chenglingfei@foxmail.com \
    --cc=axboe@kernel.dk \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=syzbot+b6fd00508cc05a5c003c@syzkaller.appspotmail.com \
    /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

all inboxes | Powered by JetHome®