Skip to contentSkip to conversation

block: cleanup and optimize bvec_gap_to_prev

PATCH In review Christoph Hellwig 3 participants 3 messages Oct 10, 2025 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: cleanup and optimize bvec_gap_to_prev

Micro-optimize the code in bvec_gap_to_prev using two tricks:

- OR the offset and previous length + offset so that there is no need for a branch to check both with separate logical and checks.
- Always OR in the virtual boundary mask, as the instruction for that is cheaper than the branch to check for a 0 value.

This together then removes the need for the separate __-handler as skipping the branch for the 0 check is gone.

The first optimization was stolen from Keith Busch's virtual boundary series.

---
 block/blk-merge.c |  2 +-
 block/blk.h       | 12 ++----------
 2 files changed, 3 insertions(+), 11 deletions(-)
block/blk-merge.c +1 -1
index 37864c5d287e..002c59479824 100644
@@ -81,7 +81,7 @@ static inline bool bio_will_gap(struct request_queue *q,
 	bio_get_first_bvec(next, &nb);
 	if (biovec_phys_mergeable(q, &pb, &nb))
 		return false;
-	return __bvec_gap_to_prev(&q->limits, &pb, nb.bv_offset);
+	return bvec_gap_to_prev(&q->limits, &pb, nb.bv_offset);
 }
 
 static inline bool req_gap_back_merge(struct request *req, struct bio *bio)
block/blk.h +2 -10
index 170794632135..d95788caa129 100644
@@ -140,13 +140,6 @@ static inline bool biovec_phys_mergeable(struct request_queue *q,
 	return true;
 }
 
-static inline bool __bvec_gap_to_prev(const struct queue_limits *lim,
-		struct bio_vec *bprv, unsigned int offset)
-{
-	return (offset & lim->virt_boundary_mask) ||
-		((bprv->bv_offset + bprv->bv_len) & lim->virt_boundary_mask);
-}
-
 /*
  * Check if adding a bio_vec after bprv with offset would create a gap in
  * the SG list. Most drivers don't care about this, but some do.
@@ -154,9 +147,8 @@ static inline bool __bvec_gap_to_prev(const struct queue_limits *lim,
 static inline bool bvec_gap_to_prev(const struct queue_limits *lim,
 		struct bio_vec *bprv, unsigned int offset)
 {
-	if (!lim->virt_boundary_mask)
-		return false;
-	return __bvec_gap_to_prev(lim, bprv, offset);
+	return (offset | (bprv->bv_offset + bprv->bv_len)) &
+		lim->virt_boundary_mask;
 }
 
 static inline bool rq_mergeable(struct request *rq)
Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Christoph Hellwig

Micro-optimize the code in bvec_gap_to_prev using two tricks:

- OR the offset and previous length + offset so that there is no need for a branch to check both with separate logical and checks.
- Always OR in the virtual boundary mask, as the instruction for that is cheaper than the branch to check for a 0 value.

This together then removes the need for the separate __-handler as skipping the branch for the 0 check is gone.

Looks good