Linux block layer
 help / color / mirror / Atom feed
* [PATCH] block: badblocks: Remove excess comparisons of p[hi - 1]
@ 2026-09-13 17:25 Sean Young
  0 siblings, 0 replies; only message in thread
From: Sean Young @ 2026-09-13 17:25 UTC (permalink / raw)
  To: linux-kernel, Jens Axboe; +Cc: Sean Young, linux-block

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-09-13 17:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-13 17:25 [PATCH] block: badblocks: Remove excess comparisons of p[hi - 1] Sean Young

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox