Skip to contentSkip to conversation

block: account for bi_bvec_done in bio_may_need_split()

PATCH Applied Ming Lei 2 participants 2 messages Jan 10 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 block: account for bi_bvec_done in bio_may_need_split()

When checking if a bio fits in a single segment, bio_may_need_split() compares bi_size against the current bvec's bv_len. However, for partially consumed bvecs (bi_bvec_done > 0), such as in cloned or split bios, the remaining bytes in the current bvec is actually (bv_len - bi_bvec_done), not bv_len.

This could cause bio_may_need_split() to incorrectly return false, leading to nr_phys_segments being set to 1 when the bio actually spans multiple segments. This triggers the WARN_ON in __blk_rq_map_sg() when the actual mapped segments exceed the expected count.

Fix by subtracting bi_bvec_done from bv_len in the comparison.

Close: https://lore.kernel.org/linux-block/[email protected]/

Fixes ee623c892aa5 ("block: use bvec iterator helper for bio_may_need_split()")
Cc Nitesh Shetty <[email protected]>
---
 block/blk.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk.h +1 -1
index 98f4dfd4ec75..980eef1f5690 100644
@@ -380,7 +380,7 @@ static inline bool bio_may_need_split(struct bio *bio,
 		return true;
 
 	bv = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
-	if (bio->bi_iter.bi_size > bv->bv_len)
+	if (bio->bi_iter.bi_size > bv->bv_len - bio->bi_iter.bi_bvec_done)
 		return true;
 	return bv->bv_len + bv->bv_offset > lim->max_fast_segment_size;
 }
Signature · 1 line
2.47.1
maintainer Applied
#1 lore raw
Quoted 6 lines · Ming Lei

When checking if a bio fits in a single segment, bio_may_need_split() compares bi_size against the current bvec's bv_len. However, for partially consumed bvecs (bi_bvec_done > 0), such as in cloned or split bios, the remaining bytes in the current bvec is actually (bv_len - bi_bvec_done), not bv_len.

Quoted 6 lines

This could cause bio_may_need_split() to incorrectly return false, leading to nr_phys_segments being set to 1 when the bio actually spans multiple segments. This triggers the WARN_ON in __blk_rq_map_sg() when the actual mapped segments exceed the expected count.

[...]

Applied, thanks!

[1/1] block: account for bi_bvec_done in bio_may_need_split()

      commit: 5461a44ce37a507d8ed7e03cc65eefc52cb5a8f5

Best regards,

Signature · 1 line
Jens Axboe