Linux block layer
 help / color / mirror / Atom feed
From: Yang Erkun <yangerkun@huawei.com>
To: josef@toxicpanda.com, axboe@kernel.dk, hch@lst.de, yukuai@kernel.org
Cc: yi.zhang@huawei.com, chengzhihao1@huawei.com,
	echo.chenlin@huawei.com, leo.lilong@huaweicloud.com,
	wangkefeng.wang@huawei.com, yangerkun@huawei.com,
	linux-block@vger.kernel.org, nbd@other.debian.org
Subject: [PATCH v3 4/6] nbd: set nr_hw_queues at device creation to skip queue freeze
Date: Mon, 13 Jul 2026 14:56:42 +0800	[thread overview]
Message-ID: <20260713065644.1637594-5-yangerkun@huawei.com> (raw)
In-Reply-To: <20260713065644.1637594-1-yangerkun@huawei.com>

There still be queue freeze call when nbd_start_device invoking
blk_mq_update_nr_hw_queues. For netlink path, we can obtain the actual
number of connections before calling nbd_dev_add in nbd_genl_connect,
which can helps remove this queue freeze.

However, nbd devices created with a fixed nbds_max may still require
this freezing because the real connection count is unknown.

Signed-off-by: Yang Erkun <yangerkun@huawei.com>
---
 drivers/block/nbd.c | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 0755b7046ed4..400f638e832e 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1930,7 +1930,8 @@ static const struct blk_mq_ops nbd_mq_ops = {
 	.timeout	= nbd_xmit_timeout,
 };
 
-static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
+static struct nbd_device *nbd_dev_add(int index, unsigned int refs,
+				       int nr_hw_queues)
 {
 	struct queue_limits lim = {
 		.max_hw_sectors		= 65536,
@@ -1947,7 +1948,7 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
 		goto out;
 
 	nbd->tag_set.ops = &nbd_mq_ops;
-	nbd->tag_set.nr_hw_queues = 1;
+	nbd->tag_set.nr_hw_queues = nr_hw_queues;
 	nbd->tag_set.queue_depth = 128;
 	nbd->tag_set.numa_node = NUMA_NO_NODE;
 	nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
@@ -2070,6 +2071,35 @@ static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
 	[NBD_SOCK_FD]			=	{ .type = NLA_U32 },
 };
 
+/*
+ * Count the number of socket FDs in the NBD_ATTR_SOCKETS netlink attribute.
+ * This is used to determine the correct nr_hw_queues before creating the
+ * nbd device, so that blk_mq_update_nr_hw_queues (and its RCU grace period
+ * overhead) can be avoided entirely.
+ */
+static int nbd_genl_count_sockets(struct genl_info *info)
+{
+	struct nlattr *attr;
+	int rem, count = 0;
+
+	if (!info->attrs[NBD_ATTR_SOCKETS])
+		return 0;
+
+	nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS], rem) {
+		struct nlattr *socks[NBD_SOCK_MAX + 1];
+
+		if (nla_type(attr) != NBD_SOCK_ITEM)
+			continue;
+		if (nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
+						  attr, nbd_sock_policy,
+						  info->extack) != 0)
+			continue;
+		if (socks[NBD_SOCK_FD])
+			count++;
+	}
+	return count;
+}
+
 /* We don't use this right now since we don't parse the incoming list, but we
  * still want it here so userspace knows what to expect.
  */
@@ -2101,6 +2131,7 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
 	struct nbd_device *nbd;
 	struct nbd_config *config;
 	int index = -1;
+	int num_connections = nbd_genl_count_sockets(info);
 	int ret;
 	bool put_dev = false;
 
@@ -2148,7 +2179,7 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
 	mutex_unlock(&nbd_index_mutex);
 
 	if (!nbd) {
-		nbd = nbd_dev_add(index, 2);
+		nbd = nbd_dev_add(index, 2, num_connections);
 		if (IS_ERR(nbd)) {
 			pr_err("failed to add new device\n");
 			return PTR_ERR(nbd);
@@ -2715,7 +2746,7 @@ static int __init nbd_init(void)
 	nbd_dbg_init();
 
 	for (i = 0; i < nbds_max; i++)
-		nbd_dev_add(i, 1);
+		nbd_dev_add(i, 1, 1);
 	return 0;
 }
 
-- 
2.52.0


  parent reply	other threads:[~2026-07-13  7:05 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-13  6:56 [PATCH v3 0/6] nbd: eliminate queue freeze/unfreeze overhead in connection setup Yang Erkun
2026-07-13  6:56 ` [PATCH v3 1/6] nbd: simplify find_fallback() by removing redundant logic Yang Erkun
2026-07-13  6:56 ` [PATCH v3 2/6] nbd: disallow NBD_SET_SOCK on an active device Yang Erkun
2026-07-22  3:03   ` yu kuai
2026-07-22  6:12     ` yangerkun
2026-07-13  6:56 ` [PATCH v3 3/6] nbd: remove queue freeze in nbd_add_socket Yang Erkun
2026-07-22  3:17   ` yu kuai
2026-07-22  6:35     ` yu kuai
2026-07-22  8:45       ` yangerkun
2026-07-22  9:20         ` yangerkun
2026-07-13  6:56 ` Yang Erkun [this message]
2026-07-22  3:38   ` [PATCH v3 4/6] nbd: set nr_hw_queues at device creation to skip queue freeze yu kuai
2026-07-23  1:09     ` yangerkun
2026-07-13  6:56 ` [PATCH v3 5/6] nbd: skip queue freeze when setting size at device startup Yang Erkun
2026-07-22  3:49   ` yu kuai
2026-07-23  1:22     ` yangerkun
2026-07-13  6:56 ` [PATCH v3 6/6] nbd: add nr_hw_queues module parameter for pre-created devices Yang Erkun
2026-07-22  3:53   ` yu kuai
2026-07-23  1:24     ` yangerkun
2026-07-21  8:41 ` [PATCH v3 0/6] nbd: eliminate queue freeze/unfreeze overhead in connection setup 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=20260713065644.1637594-5-yangerkun@huawei.com \
    --to=yangerkun@huawei.com \
    --cc=axboe@kernel.dk \
    --cc=chengzhihao1@huawei.com \
    --cc=echo.chenlin@huawei.com \
    --cc=hch@lst.de \
    --cc=josef@toxicpanda.com \
    --cc=leo.lilong@huaweicloud.com \
    --cc=linux-block@vger.kernel.org \
    --cc=nbd@other.debian.org \
    --cc=wangkefeng.wang@huawei.com \
    --cc=yi.zhang@huawei.com \
    --cc=yukuai@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