From: Yang Erkun <yangerkun@huawei.com>
To: axboe@kernel.dk, josef@toxicpanda.com, xiubli@redhat.com,
prasanna.kalever@redhat.com, ming.lei@redhat.com, yukuai@fygo.io,
nilay@linux.ibm.com
Cc: linux-block@vger.kernel.org, nbd@other.debian.org,
yangerkun@huawei.com, wangkefeng.wang@huawei.com,
leijitang@huawei.com, yi.zhang@huawei.com,
chengzhihao1@huawei.com, houtao1@huawei.com, wozizhi@huawei.com
Subject: [RFC PATCH 3/5] nbd: publish devices atomically
Date: Sat, 7 Nov 2026 14:46:27 +0800 [thread overview]
Message-ID: <20261107064629.4093489-4-yangerkun@huawei.com> (raw)
In-Reply-To: <20261107064629.4093489-1-yangerkun@huawei.com>
nbd_dev_add() inserts the device into nbd_index_idr as soon as it has
an index, but sets nbd->refs and nbd_total_devices only after add_disk(),
the latter without nbd_index_mutex. With parallel_ops enabled in a later
patch, a status walk can then report the half-initialized device while
nbd_total_devices does not account for it yet, and overflow the reply
(-EMSGSIZE).
Set nbd->refs and nbd_total_devices together under nbd_index_mutex, and
skip devices with a zero refcount in populate_nbd_status(). Then
nbd_total_devices is an upper bound on the devices status reports, and a
reply sized from it can't overflow.
Assisted-by: LLM
Signed-off-by: Yang Erkun <yangerkun@huawei.com>
---
drivers/block/nbd.c | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 1448ee63fab5..8c2cb312f209 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -2029,10 +2029,15 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs,
goto out_free_work;
/*
- * Now publish the device.
+ * Now publish the device. nbd->refs and nbd_total_devices are
+ * updated together under nbd_index_mutex, so a concurrent idr
+ * lookup either sees a fully initialised device or one that it has
+ * to skip because its refcount is still zero.
*/
+ mutex_lock(&nbd_index_mutex);
refcount_set(&nbd->refs, refs);
nbd_total_devices++;
+ mutex_unlock(&nbd_index_mutex);
return nbd;
out_free_work:
@@ -2537,6 +2542,16 @@ static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
u8 connected = 0;
int ret;
+ /*
+ * nbd_dev_add() inserts the device into the idr before it is fully
+ * initialised and only publishes it (sets nbd->refs) after
+ * add_disk(); skip a device that is not published yet or is already
+ * going away, so that the walk never reports a device that
+ * nbd_total_devices does not account for.
+ */
+ if (!refcount_read(&nbd->refs))
+ return 0;
+
/* This is a little racey, but for status it's ok. The
* reason we don't take a ref here is because we can't
* take a ref in the index == -1 case as we would need
--
2.52.0
next prev parent reply other threads:[~2026-11-07 6:54 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-07 6:46 [RFC PATCH 0/5] nbd: allow parallel genetlink operations Yang Erkun
2026-11-07 6:46 ` [RFC PATCH 1/5] nbd: validate the backend identifier under config_lock Yang Erkun
2026-11-07 6:46 ` [RFC PATCH 2/5] nbd: decrement nbd_total_devices when a device is removed Yang Erkun
2026-11-07 6:46 ` Yang Erkun [this message]
2026-11-07 6:46 ` [RFC PATCH 4/5] nbd: hold config_lock while updating nr_hw_queues Yang Erkun
2026-11-07 6:46 ` [RFC PATCH 5/5] nbd: allow genetlink operations to run in parallel Yang Erkun
2026-11-17 4:36 ` [RFC PATCH 0/5] nbd: allow parallel genetlink operations yangerkun
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=20261107064629.4093489-4-yangerkun@huawei.com \
--to=yangerkun@huawei.com \
--cc=axboe@kernel.dk \
--cc=chengzhihao1@huawei.com \
--cc=houtao1@huawei.com \
--cc=josef@toxicpanda.com \
--cc=leijitang@huawei.com \
--cc=linux-block@vger.kernel.org \
--cc=ming.lei@redhat.com \
--cc=nbd@other.debian.org \
--cc=nilay@linux.ibm.com \
--cc=prasanna.kalever@redhat.com \
--cc=wangkefeng.wang@huawei.com \
--cc=wozizhi@huawei.com \
--cc=xiubli@redhat.com \
--cc=yi.zhang@huawei.com \
--cc=yukuai@fygo.io \
/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