Skip to contentSkip to conversation

block: consider discard merge last

PATCH Applied Keith Busch 3 participants 3 messages Nov 14, 2025 – Nov 17, 2025 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: consider discard merge last

From: Keith Busch <[email protected]>

If the next discard range is contiguous with the current range being considered, it's cheaper to expand the current range than to append an additional bio.

---
 block/blk-merge.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
block/blk-merge.c +6 -6
index d3115d7469df0..db08bc9060916 100644
@@ -712,10 +712,10 @@ static void blk_account_io_merge_request(struct request *req)
 static enum elv_merge blk_try_req_merge(struct request *req,
 					struct request *next)
 {
-	if (blk_discard_mergable(req))
-		return ELEVATOR_DISCARD_MERGE;
-	else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next))
+	if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next))
 		return ELEVATOR_BACK_MERGE;
+	else if (blk_discard_mergable(req))
+		return ELEVATOR_DISCARD_MERGE;
 
 	return ELEVATOR_NO_MERGE;
 }
@@ -903,12 +903,12 @@ bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
 
 enum elv_merge blk_try_merge(struct request *rq, struct bio *bio)
 {
-	if (blk_discard_mergable(rq))
-		return ELEVATOR_DISCARD_MERGE;
-	else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
+	if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
 		return ELEVATOR_BACK_MERGE;
 	else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
 		return ELEVATOR_FRONT_MERGE;
+	else if (blk_discard_mergable(rq))
+		return ELEVATOR_DISCARD_MERGE;
 	return ELEVATOR_NO_MERGE;
 }
 
Signature · 1 line
2.47.3
maintainer Applied
#2 lore raw
In reply to Keith Busch
Quoted 5 lines · Keith Busch

If the next discard range is contiguous with the current range being considered, it's cheaper to expand the current range than to append an additional bio.

Applied, thanks!

[1/1] block: consider discard merge last

      commit: 2516c246d01c23a5f5310e9ac78d9f8aad9b1d0e

Best regards,

Signature · 1 line
Jens Axboe