rationalize the flow in bio_add_page and friends v2
Hi Jens,
when reviewing v2 of Jinyoung's "Change the integrity configuration method in block" series I noticed that someone made a complete mess of the bio_add_page flow, so this untangles this to make the code better reusable for adding integrity payloads. (I'll also have a word with younger me when I get the chance about this..)
Changes since v1:
- rebased against the current for-6.6/block tree
Diffstat:
bio.c | 124 ++++++++++++++++++++++++++++-------------------------------------- 1 file changed, 53 insertions(+), 71 deletions(-)
PATCH 1/8 block: tidy up the bio full checks in bio_add_hw_page
bio_add_hw_page already checks if the number of bytes trying to be added
even fit into max_hw_sectors limit of the queue. Remove the call tobio_full and just do a check for the smaller of the number of segments in the bio and the queue max segments limit, and do this cheap check before the more expensive gap to previous check.
--- block/bio.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
block/bio.c +4 -6
@@ -1014,6 +1014,10 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio,if (bio_try_merge_hw_seg (q, bio, page, len, offset, same_page))return len; +if (bio->bi_vcnt >= +min (bio->bi_max_vecs,queue_max_segments (q))) +return 0 ; +/* * If the queue doesn't support SG gaps and adding this segment * would create a gap, disallow it.
@@ -1023,12 +1027,6 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio,return 0 ; } -if (bio_full (bio, len)) -return 0 ; - -if (bio->bi_vcnt >=queue_max_segments (q)) -return 0 ; -bvec_set_page (&bio->bi_io_vec[bio->bi_vcnt], page, len, offset); bio->bi_vcnt++; bio->bi_iter.bi_size += len;
Signature · 1 line
2.39.2
RE: [PATCH 1/8] block: tidy up the bio full checks in bio_add_hw_page
Looks good to me,
PATCH 2/8 block: use SECTOR_SHIFT bio_add_hw_page
In reply to Christoph HellwigUse the SECTOR_SHIFT magic constant instead of the magic number.
--- block/bio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/bio.c +1 -1
@@ -1007,7 +1007,7 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio,if (WARN_ON_ONCE (bio_flagged (bio,BIO_CLONED )))return 0 ; -if (((bio->bi_iter.bi_size + len) >>9 ) > max_sectors) +if (((bio->bi_iter.bi_size + len) >>SECTOR_SHIFT ) > max_sectors)return 0 ;if (bio->bi_vcnt >0 ) {
Signature · 1 line
2.39.2
PATCH 3/8 block: move the BIO_CLONED checks out of __bio_try_merge_page
In reply to Christoph Hellwig__bio_try_merge_page is a way too low-level helper to assert that the bio is not cloned. Move the check into bio_add_page and bio_iov_iter_get_pages instead, which are the high level entry points that should enforce this variant. bio_add_hw_page already this check, coverig the third (indirect) caller of __bio_try_merge_page.
--- block/bio.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
block/bio.c +6 -3
@@ -945,9 +945,6 @@ static inline bool page_is_mergeable(const struct bio_vec *bv,static bool __bio_try_merge_page (struct bio *bio,struct page *page,unsigned int len,unsigned int off,bool *same_page) { -if (WARN_ON_ONCE (bio_flagged (bio,BIO_CLONED ))) -return false; -if (bio->bi_vcnt >0 ) {struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt -1 ];
@@ -1127,6 +1124,9 @@ int bio_add_page(struct bio *bio, struct page *page, {bool same_page = false; +if (WARN_ON_ONCE (bio_flagged (bio,BIO_CLONED ))) +return 0 ; +if (!__bio_try_merge_page (bio, page, len, offset, &same_page)) {if (bio_full (bio, len))return 0 ;
@@ -1335,6 +1335,9 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) {int ret =0 ; +if (WARN_ON_ONCE (bio_flagged (bio,BIO_CLONED ))) +return -EIO ; +if (iov_iter_is_bvec (iter)) {bio_iov_bvec_set (bio, iter);iov_iter_advance (iter, bio->bi_iter.bi_size);
Signature · 1 line
2.39.2
PATCH 4/8 block: move the bi_vcnt check out of __bio_try_merge_page
In reply to Christoph HellwigMove the bi_vcnt out of __bio_try_merge_page and into the two callers that don't already have it in preparation for additional changes to __bio_try_merge_page.
--- block/bio.c | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-)
block/bio.c +22 -23
@@ -945,20 +945,17 @@ static inline bool page_is_mergeable(const struct bio_vec *bv,static bool __bio_try_merge_page (struct bio *bio,struct page *page,unsigned int len,unsigned int off,bool *same_page) { -if (bio->bi_vcnt >0 ) { -struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt -1 ]; - -if (page_is_mergeable (bv, page, len, off, same_page)) { -if (bio->bi_iter.bi_size >UINT_MAX - len) { - *same_page = false; -return false; - } - bv->bv_len += len; - bio->bi_iter.bi_size += len; -return true; - } +struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt -1 ]; + +if (!page_is_mergeable (bv, page, len, off, same_page)) +return false; +if (bio->bi_iter.bi_size >UINT_MAX - len) { + *same_page = false; +return false; } -return false; + bv->bv_len += len; + bio->bi_iter.bi_size += len; +return true; }/*
@@ -1127,11 +1124,13 @@ int bio_add_page(struct bio *bio, struct page *page,if (WARN_ON_ONCE (bio_flagged (bio,BIO_CLONED )))return 0 ; -if (!__bio_try_merge_page (bio, page, len, offset, &same_page)) { -if (bio_full (bio, len)) -return 0 ; -__bio_add_page (bio, page, len, offset); - } +if (bio->bi_vcnt >0 && +__bio_try_merge_page (bio, page, len, offset, &same_page)) +return len; + +if (bio_full (bio, len)) +return 0 ; +__bio_add_page (bio, page, len, offset);return len; }EXPORT_SYMBOL (bio_add_page);
@@ -1205,13 +1204,13 @@ static int bio_iov_add_page(struct bio *bio, struct page *page, {bool same_page = false; -if (!__bio_try_merge_page (bio, page, len, offset, &same_page)) { -__bio_add_page (bio, page, len, offset); +if (bio->bi_vcnt >0 && +__bio_try_merge_page (bio, page, len, offset, &same_page)) { +if (same_page) +bio_release_page (bio, page);return 0 ; } - -if (same_page) -bio_release_page (bio, page); +__bio_add_page (bio, page, len, offset);return 0 ; }
Signature · 1 line
2.39.2
PATCH 5/8 block: move the bi_size overflow check in __bio_try_merge_page
In reply to Christoph HellwigChecking for availability in bi_size in a function that attempts to merge into an existing segment is a bit odd, as the limit also applies when adding a new segment. This code works fine as we always call __bio_try_merge_page, but contributes to sub-optimal calling conventions and doesn't lead to clear code.
Move it to two of the callers instead, the third one already has a more strict check that includes max_hw_segments anyway.
--- block/bio.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
block/bio.c +5 -4
@@ -949,10 +949,6 @@ static bool __bio_try_merge_page(struct bio *bio, struct page *page,if (!page_is_mergeable (bv, page, len, off, same_page))return false; -if (bio->bi_iter.bi_size >UINT_MAX - len) { - *same_page = false; -return false; - } bv->bv_len += len; bio->bi_iter.bi_size += len;return true;
@@ -1123,6 +1119,8 @@ int bio_add_page(struct bio *bio, struct page *page,if (WARN_ON_ONCE (bio_flagged (bio,BIO_CLONED )))return 0 ; +if (bio->bi_iter.bi_size >UINT_MAX - len) +return 0 ;if (bio->bi_vcnt >0 &&__bio_try_merge_page (bio, page, len, offset, &same_page))
@@ -1204,6 +1202,9 @@ static int bio_iov_add_page(struct bio *bio, struct page *page, {bool same_page = false; +if (WARN_ON_ONCE (bio->bi_iter.bi_size >UINT_MAX - len)) +return -EIO ; +if (bio->bi_vcnt >0 &&__bio_try_merge_page (bio, page, len, offset, &same_page)) {if (same_page)
Signature · 1 line
2.39.2
PATCH 6/8 block: downgrade a bio_full call in bio_add_page
In reply to Christoph Hellwigbio_add_page already checks that there is space in bi_size a little earlier. So after we failed to add to an existing segment, just check that there is another one available instead of duplicating the bi_size check.
--- block/bio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/bio.c +1 -1
@@ -1126,7 +1126,7 @@ int bio_add_page(struct bio *bio, struct page *page, __bio_try_merge_page(bio, page, len, offset, &same_page))return len; -if (bio_full (bio, len)) +if (bio->bi_vcnt >= bio->bi_max_vecs)return 0 ;__bio_add_page (bio, page, len, offset);return len;
Signature · 1 line
2.39.2
PATCH 7/8 block: move the bi_size update out of __bio_try_merge_page
In reply to Christoph HellwigThe update of bi_size is the only thing in __bio_try_merge_page that needs a bio. Move it to the callers, and merge __bio_try_merge_page and page_is_mergeable into a single bvec_try_merge_page that only takes the current bvec instead of a full bio. This will allow reusing this function for supporting multi-page integrity payload bvecs.
--- block/bio.c | 57 +++++++++++++++++++---------------------------------- 1 file changed, 20 insertions(+), 37 deletions(-)
block/bio.c +20 -37
@@ -903,9 +903,8 @@ static inline bool bio_full(struct bio *bio, unsigned len)return false; } -static inline bool page_is_mergeable (const struct bio_vec *bv, -struct page *page,unsigned int len,unsigned int off, -bool *same_page) +static bool bvec_try_merge_page (struct bio_vec *bv,struct page *page, +unsigned int len,unsigned int off,bool *same_page) {size_t bv_end = bv->bv_offset + bv->bv_len;phys_addr_t vec_end_addr =page_to_phys (bv->bv_page) + bv_end -1 ;
@@ -919,38 +918,14 @@ static inline bool page_is_mergeable(const struct bio_vec *bv,return false; *same_page = ((vec_end_addr &PAGE_MASK ) == page_addr); -if (*same_page) -return true; -else if (IS_ENABLED (CONFIG_KMSAN )) -return false; -return (bv->bv_page + bv_end /PAGE_SIZE ) == (page + off /PAGE_SIZE ); -} - -/** -* __bio_try_merge_page - try appending data to an existing bvec. -* @bio: destination bio -* @page: start page to add -* @len: length of the data to add -* @off: offset of the data relative to @page -* @same_page: return if the segment has been merged inside the same page -* -* Try to add the data at @page + @off to the last bvec of @bio. This is a -* useful optimisation for file systems with a block size smaller than the -* page size. -* -* Warn if (@len, @off) crosses pages in case that @same_page is true. -* -* Return %true on success or %false on failure. -*/ -static bool __bio_try_merge_page (struct bio *bio,struct page *page, -unsigned int len,unsigned int off,bool *same_page) -{ -struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt -1 ]; +if (!*same_page) { +if (IS_ENABLED (CONFIG_KMSAN )) +return false; +if (bv->bv_page + bv_end /PAGE_SIZE != page + off /PAGE_SIZE ) +return false; + } -if (!page_is_mergeable (bv, page, len, off, same_page)) -return false; bv->bv_len += len; - bio->bi_iter.bi_size += len;return true; }
@@ -972,7 +947,7 @@ static bool bio_try_merge_hw_seg(struct request_queue *q, struct bio *bio,return false;if (bv->bv_len + len >queue_max_segment_size (q))return false; -return __bio_try_merge_page (bio, page, len, offset, same_page); +return bvec_try_merge_page (bv, page, len, offset, same_page); }/**
@@ -1001,8 +976,11 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio,return 0 ;if (bio->bi_vcnt >0 ) { -if (bio_try_merge_hw_seg (q, bio, page, len, offset, same_page)) +if (bio_try_merge_hw_seg (q, bio, page, len, offset, + same_page)) { + bio->bi_iter.bi_size += len;return len; + }if (bio->bi_vcnt >=min (bio->bi_max_vecs,queue_max_segments (q)))
@@ -1123,8 +1101,11 @@ int bio_add_page(struct bio *bio, struct page *page,return 0 ;if (bio->bi_vcnt >0 && -__bio_try_merge_page (bio, page, len, offset, &same_page)) +bvec_try_merge_page (&bio->bi_io_vec[bio->bi_vcnt -1 ], + page, len, offset, &same_page)) { + bio->bi_iter.bi_size += len;return len; + }if (bio->bi_vcnt >= bio->bi_max_vecs)return 0 ;
@@ -1206,7 +1187,9 @@ static int bio_iov_add_page(struct bio *bio, struct page *page,return -EIO ;if (bio->bi_vcnt >0 && -__bio_try_merge_page (bio, page, len, offset, &same_page)) { +bvec_try_merge_page (&bio->bi_io_vec[bio->bi_vcnt -1 ], + page, len, offset, &same_page)) { + bio->bi_iter.bi_size += len;if (same_page)bio_release_page (bio, page);return 0 ;
Signature · 1 line
2.39.2
PATCH 8/8 block: don't pass a bio to bio_try_merge_hw_seg
In reply to Christoph HellwigThere is no good reason to pass the bio to bio_try_merge_hw_seg. Just pass the current bvec and rename the function to bvec_try_merge_hw_page. This will allow reusing this function for supporting multi-page integrity payload bvecs.
--- block/bio.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
block/bio.c +7 -9
@@ -934,11 +934,10 @@ static bool bvec_try_merge_page(struct bio_vec *bv, struct page *page,* size limit. This is not for normal read/write bios, but for passthrough * or Zone Append operations that we can't split. */ -static bool bio_try_merge_hw_seg (struct request_queue *q,struct bio *bio, -struct page *page,unsigned len, -unsigned offset,bool *same_page) +static bool bvec_try_merge_hw_page (struct request_queue *q,struct bio_vec *bv, +struct page *page,unsigned len,unsigned offset, +bool *same_page) { -struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt -1 ];unsigned long mask =queue_segment_boundary (q);phys_addr_t addr1 =page_to_phys (bv->bv_page) + bv->bv_offset;phys_addr_t addr2 =page_to_phys (page) + offset + len -1 ;
@@ -967,8 +966,6 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio, struct page *page, unsigned int len, unsigned int offset, unsigned int max_sectors, bool *same_page) { - struct bio_vec *bvec; - if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED))) return 0;
@@ -976,7 +973,9 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio,return 0 ;if (bio->bi_vcnt >0 ) { -if (bio_try_merge_hw_seg (q, bio, page, len, offset, +struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt -1 ]; + +if (bvec_try_merge_hw_page (q, bv, page, len, offset, same_page)) { bio->bi_iter.bi_size += len;return len;
@@ -990,8 +989,7 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio,* If the queue doesn't support SG gaps and adding this segment * would create a gap, disallow it. */ - bvec = &bio->bi_io_vec[bio->bi_vcnt -1 ]; -if (bvec_gap_to_prev (&q->limits, bvec, offset)) +if (bvec_gap_to_prev (&q->limits, bv, offset))return 0 ; }
Signature · 1 line
2.39.2
Quoted 10 lines · Christoph Hellwig
when reviewing v2 of Jinyoung's "Change the integrity configuration method in block" series I noticed that someone made a complete mess of the
bio_add_pageflow, so this untangles this to make the code better reusable for adding integrity payloads. (I'll also have a word with younger me when I get the chance about this..)Changes since v1:
- rebased against the current for-6.6/block tree[...]
Applied, thanks!
[1/8] block: tidy up the bio full checks in bio_add_hw_page
commit: cd1d83e24e689f25de7e34bea697971750138d5f
[2/8] block: use SECTOR_SHIFT bio_add_hw_page
commit: 6850b2dd5c25f27f7b74414553f047d4c12dd66c
[3/8] block: move the BIO_CLONED checks out of __bio_try_merge_page
commit: 939e1a370330841b2c0292a483d7b38f3ee45f88
[4/8] block: move the bi_vcnt check out of __bio_try_merge_page
commit: 0eca8b6f97ac705c5806f7d062207379094fb114
[5/8] block: move the bi_size overflow check in __bio_try_merge_page
commit: 613699050a49760f1d70c74f71bd0b013ca3c356
[6/8] block: downgrade a bio_full call in bio_add_page
commit: 80232b520314214d846eb0a65faef8b51b702fa7
[7/8] block: move the bi_size update out of __bio_try_merge_page
commit: 858c708d9efb7e8e5c6320793b778cc17cf8368a
[8/8] block: don't pass a bio to bio_try_merge_hw_seg
commit: ae42f0b3bf65912e122fc2e8d5f6d94b51156dbaBest regards,
Signature · 1 line
Jens Axboe