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, linux-block@vger.kernel.org,
	nbd@other.debian.org
Subject: [PATCH v2 1/5] nbd: simplify find_fallback() by removing redundant logic
Date: Thu, 25 Jun 2026 16:44:54 +0800	[thread overview]
Message-ID: <20260625084458.4171890-2-yangerkun@huawei.com> (raw)
In-Reply-To: <20260625084458.4171890-1-yangerkun@huawei.com>

From: Long Li <leo.lilong@huawei.com>

The second conditional checking nsock->fallback_index validity is the
logical inverse of the first, so drop it and let execution fall through
naturally. Consolidate the two identical dev_err_ratelimited() + return
paths into a single no_fallback label to reduce duplication.

Signed-off-by: Long Li <leo.lilong@huawei.com>
---
 drivers/block/nbd.c | 37 ++++++++++++++-----------------------
 1 file changed, 14 insertions(+), 23 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 3a585a0c882a..dcba3042862a 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1061,40 +1061,31 @@ static int find_fallback(struct nbd_device *nbd, int index)
 	int new_index = -1;
 	struct nbd_sock *nsock = config->socks[index];
 	int fallback = nsock->fallback_index;
+	int i;
 
 	if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
 		return new_index;
 
-	if (config->num_connections <= 1) {
-		dev_err_ratelimited(disk_to_dev(nbd->disk),
-				    "Dead connection, failed to find a fallback\n");
-		return new_index;
-	}
+	if (config->num_connections <= 1)
+		goto no_fallback;
 
 	if (fallback >= 0 && fallback < config->num_connections &&
 	    !config->socks[fallback]->dead)
 		return fallback;
 
-	if (nsock->fallback_index < 0 ||
-	    nsock->fallback_index >= config->num_connections ||
-	    config->socks[nsock->fallback_index]->dead) {
-		int i;
-		for (i = 0; i < config->num_connections; i++) {
-			if (i == index)
-				continue;
-			if (!config->socks[i]->dead) {
-				new_index = i;
-				break;
-			}
-		}
-		nsock->fallback_index = new_index;
-		if (new_index < 0) {
-			dev_err_ratelimited(disk_to_dev(nbd->disk),
-					    "Dead connection, failed to find a fallback\n");
-			return new_index;
+	for (i = 0; i < config->num_connections; i++) {
+		if (i != index && !config->socks[i]->dead) {
+			new_index = i;
+			break;
 		}
 	}
-	new_index = nsock->fallback_index;
+	nsock->fallback_index = new_index;
+	if (new_index >= 0)
+		return new_index;
+
+no_fallback:
+	dev_err_ratelimited(disk_to_dev(nbd->disk),
+			    "Dead connection, failed to find a fallback\n");
 	return new_index;
 }
 
-- 
2.52.0


  reply	other threads:[~2026-06-25  8:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-25  8:44 [PATCH v2 0/5] nbd: eliminate queue freeze/unfreeze overhead in connection setup Yang Erkun
2026-06-25  8:44 ` Yang Erkun [this message]
2026-06-25  8:44 ` [PATCH v2 2/5] nbd: replace socks pointer array with xarray Yang Erkun
2026-06-25  8:44 ` [PATCH v2 3/5] nbd: remove redundant num_connections boundary checks Yang Erkun
2026-06-25  8:44 ` [PATCH v2 4/5] nbd: remove queue freeze in nbd_add_socket Yang Erkun
2026-06-25  8:44 ` [PATCH v2 5/5] nbd: set nr_hw_queues at device creation to skip queue freeze Yang Erkun

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=20260625084458.4171890-2-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