* [PATCH] block: optimize I/O merge hot path with unlikely() hints
@ 2026-06-06 2:42 Steven Feng
2026-06-08 14:34 ` Jens Axboe
2026-06-09 7:38 ` Christoph Hellwig
0 siblings, 2 replies; 4+ messages in thread
From: Steven Feng @ 2026-06-06 2:42 UTC (permalink / raw)
To: Jens Axboe; +Cc: linux-block, linux-kernel, Steven Feng
Remove redundant '== false' comparisons and add unlikely() branch
prediction hints in block I/O merge path functions.
These functions (ll_new_hw_segment, ll_merge_requests_fn, and
blk_rq_merge_ok) are executed on every I/O request merge attempt,
making them critical hot paths. Data integrity check failures are
rare events, so marking these conditions as unlikely() helps the
CPU optimize the common case by improving branch prediction.
Changes:
- Replace 'func() == false' with 'unlikely(!func())' for better
code style and branch prediction
This micro-optimization reduces branch misprediction penalties in
high-frequency I/O merge paths.
Signed-off-by: Steven Feng <steven@joint-cloud.com>
---
block/blk-merge.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/block/blk-merge.c b/block/blk-merge.c
index fcf09325b22e..65347d1646a1 100644
--- a/block/blk-merge.c
+++ b/block/blk-merge.c
@@ -547,7 +547,7 @@ static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
if (!blk_cgroup_mergeable(req, bio))
goto no_merge;
- if (blk_integrity_merge_bio(req->q, req, bio) == false)
+ if (unlikely(!blk_integrity_merge_bio(req->q, req, bio)))
goto no_merge;
/* discard request merge won't add new segment */
@@ -649,7 +649,7 @@ static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
if (!blk_cgroup_mergeable(req, next->bio))
return 0;
- if (blk_integrity_merge_rq(q, req, next) == false)
+ if (unlikely(!blk_integrity_merge_rq(q, req, next)))
return 0;
if (!bio_crypt_ctx_merge_rq(req, next))
@@ -905,7 +905,7 @@ bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
if (!blk_cgroup_mergeable(rq, bio))
return false;
- if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
+ if (unlikely(!blk_integrity_merge_bio(rq->q, rq, bio)))
return false;
if (!bio_crypt_rq_ctx_compatible(rq, bio))
return false;
@@ -915,7 +915,7 @@ bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
return false;
if (rq->bio->bi_ioprio != bio->bi_ioprio)
return false;
- if (blk_atomic_write_mergeable_rq_bio(rq, bio) == false)
+ if (unlikely(!blk_atomic_write_mergeable_rq_bio(rq, bio)))
return false;
return true;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] block: optimize I/O merge hot path with unlikely() hints
2026-06-06 2:42 [PATCH] block: optimize I/O merge hot path with unlikely() hints Steven Feng
@ 2026-06-08 14:34 ` Jens Axboe
2026-06-09 7:38 ` Christoph Hellwig
1 sibling, 0 replies; 4+ messages in thread
From: Jens Axboe @ 2026-06-08 14:34 UTC (permalink / raw)
To: Steven Feng; +Cc: linux-block, linux-kernel
On Sat, 06 Jun 2026 10:42:18 +0800, Steven Feng wrote:
> Remove redundant '== false' comparisons and add unlikely() branch
> prediction hints in block I/O merge path functions.
>
> These functions (ll_new_hw_segment, ll_merge_requests_fn, and
> blk_rq_merge_ok) are executed on every I/O request merge attempt,
> making them critical hot paths. Data integrity check failures are
> rare events, so marking these conditions as unlikely() helps the
> CPU optimize the common case by improving branch prediction.
>
> [...]
Applied, thanks!
[1/1] block: optimize I/O merge hot path with unlikely() hints
commit: 7ed4aab1381f3439f45032eb860f89d9da5e45c2
Best regards,
--
Jens Axboe
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] block: optimize I/O merge hot path with unlikely() hints
2026-06-06 2:42 [PATCH] block: optimize I/O merge hot path with unlikely() hints Steven Feng
2026-06-08 14:34 ` Jens Axboe
@ 2026-06-09 7:38 ` Christoph Hellwig
1 sibling, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2026-06-09 7:38 UTC (permalink / raw)
To: Steven Feng; +Cc: Jens Axboe, linux-block, linux-kernel
On Sat, Jun 06, 2026 at 10:42:18AM +0800, Steven Feng wrote:
> Remove redundant '== false' comparisons and add unlikely() branch
> prediction hints in block I/O merge path functions.
>
> These functions (ll_new_hw_segment, ll_merge_requests_fn, and
> blk_rq_merge_ok) are executed on every I/O request merge attempt,
> making them critical hot paths. Data integrity check failures are
> rare events, so marking these conditions as unlikely() helps the
> CPU optimize the common case by improving branch prediction.
Umm, these are not failures. Just conditions to not merge because
of this, and not merging, both because of these conditions and others,
it the most common case for most workloads.
With your patch the object file size of blk-merge.o increases slightly
for me on x86_64:
text data bss dec hex filename
12299 577 0 12876 324c block/blk-merge.o.old
12331 577 0 12908 326c block/blk-merge.o
and looking at the assembly this is due to worse code generation
because it now splits different error returns.
What kind of optimization are you attempting and how did you measure
the results of this "optimizatіon"?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] block: optimize I/O merge hot path with unlikely() hints
[not found] ` <ZmUjB3aF8K1xYzHr@lst.de>
@ 2026-06-09 8:24 ` Steven Feng
0 siblings, 0 replies; 4+ messages in thread
From: Steven Feng @ 2026-06-09 8:24 UTC (permalink / raw)
To: Christoph Hellwig; +Cc: Jens Axboe, linux-block, linux-kernel, Steven Feng
On Tue, Jun 09, 2026 at 12:38:47AM -0700, Christoph Hellwig wrote:
> Umm, these are not failures. Just conditions to not merge because
> of this, and not merging, both because of these conditions and others,
> it the most common case for most workloads.
You're absolutely right. I misunderstood the workload characteristics.
I incorrectly assumed these were rare failure cases rather than common
merge rejection conditions.
> With your patch the object file size of blk-merge.o increases slightly
> for me on x86_64:
>
> What kind of optimization are you attempting and how did you measure
> the results of this "optimizatіon"?
I apologize - I did not properly measure the impact. I was focused on
code style cleanup (removing '== false' comparisons) and incorrectly
added unlikely() hints based on a flawed assumption about branch
frequency.
Given the increased code size and my incorrect understanding of the
workload, please disregard this patch.
Thank you for the detailed feedback. I'll be more careful to profile
and measure before claiming performance improvements in the future.
Steven
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-06-09 8:24 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-06 2:42 [PATCH] block: optimize I/O merge hot path with unlikely() hints Steven Feng
2026-06-08 14:34 ` Jens Axboe
2026-06-09 7:38 ` Christoph Hellwig
[not found] <20260606024218.12345-1-steven@joint-cloud.com>
[not found] ` <ZmUjB3aF8K1xYzHr@lst.de>
2026-06-09 8:24 ` Steven Feng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox