Linux block layer
 help / color / mirror / Atom feed
From: Sean Young <sean@mess.org>
To: linux-kernel@vger.kernel.org, Jens Axboe <axboe@kernel.dk>
Cc: Sean Young <sean@mess.org>, linux-block@vger.kernel.org
Subject: [PATCH] block: badblocks: Remove excess comparisons of p[hi - 1]
Date: Sun, 13 Sep 2026 18:25:55 +0100	[thread overview]
Message-ID: <20260913172557.62857-1-sean@mess.org> (raw)

The binary search in prev_badblocks() is correct, but it is doing
excessive comparisons. Once p[hi - 1] is compared we do not have to
compare it again.

Also remove the if after the loop since the condition is always true;
s is always greater or equal too p[lo]; on function entry, if p[lo]) > s
is true we exit early, and then lo is set to mid if p[mid] < s.

The first issue was found with an LLM, the second on code inspection.

Signed-off-by: Sean Young <sean@mess.org>
---
 block/badblocks.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/block/badblocks.c b/block/badblocks.c
index ece64e76fe8f..143b488b7fe6 100644
--- a/block/badblocks.c
+++ b/block/badblocks.c
@@ -496,14 +496,14 @@ static int prev_badblocks(struct badblocks *bb, struct badblocks_context *bad,
 	}
 
 	lo = 0;
-	hi = bb->count;
+	hi = bb->count - 1;
 	p = bb->page;
 
 	/* The following bisect search might be unnecessary */
 	if (BB_OFFSET(p[lo]) > s)
 		return -1;
-	if (BB_OFFSET(p[hi - 1]) <= s)
-		return hi - 1;
+	if (BB_OFFSET(p[hi]) <= s)
+		return hi;
 
 	/* Do bisect search in bad table */
 	while (hi - lo > 1) {
@@ -521,8 +521,7 @@ static int prev_badblocks(struct badblocks *bb, struct badblocks_context *bad,
 			hi = mid;
 	}
 
-	if (BB_OFFSET(p[lo]) <= s)
-		ret = lo;
+	ret = lo;
 out:
 	return ret;
 }
-- 
2.55.0


                 reply	other threads:[~2026-09-13 17:26 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=20260913172557.62857-1-sean@mess.org \
    --to=sean@mess.org \
    --cc=axboe@kernel.dk \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.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