Skip to contentSkip to conversation

rationalize the flow in bio_add_page and friends

PATCH In review Christoph Hellwig 4 participants 23 messages May 12, 2023 – May 20, 2023 Block layer

lore
Share & tools
lore
#0 lore raw

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..)

Diffstat:

 bio.c |  123 ++++++++++++++++++++++++++++--------------------------------------
 1 file changed, 53 insertions(+), 70 deletions(-)
#1 lore raw

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 to
bio_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
index 043944fd46ebbc..1528ca0f3df6dc 100644
@@ -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
#2 lore raw

RE: [PATCH 1/8] block: tidy up the bio full checks in bio_add_hw_page

Hi, Christoph.

Quoted 21 lines
block/bio.c +0 -0
@@ -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.

block/bio.c +0 -0
@@ -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;

block/bio.c +0 -4
-
-        if (bio->bi_vcnt >= queue_max_segments(q))
-                return 0;
-
Quoted 5 lines

        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

If the wrong bi_max_vecs is set in bio_init(e.g. bi_max_vec = 0), I think we need a code to defend against page_add.
If modified in this way, it may be added to bvec normally and cause inconsistency in the information of bio.

Best Regards,
Jinyoung.

#3 lore raw

PATCH 2/8 block: use SECTOR_SHIFT bio_add_hw_page

In reply to Christoph Hellwig

Use 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
index 1528ca0f3df6dc..d020065e613cc8 100644
@@ -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
#6 lore raw

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
index d020065e613cc8..c7bf20a779ebed 100644
@@ -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;
@@ -1328,6 +1328,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
#9 lore raw

PATCH 4/8 block: move the bi_vcnt check out of __bio_try_merge_page

In reply to Christoph Hellwig

Move 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 | 44 ++++++++++++++++++++++----------------------
 1 file changed, 22 insertions(+), 22 deletions(-)
block/bio.c +22 -22
index c7bf20a779ebed..5d2c95e05b1a52 100644
@@ -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);
@@ -1198,13 +1197,14 @@ 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)
+			put_page(page);
 		return 0;
 	}
 
-	if (same_page)
-		put_page(page);
+	__bio_add_page(bio, page, len, offset);
 	return 0;
 }
 
Signature · 1 line
2.39.2
#12 lore raw

PATCH 5/8 block: move the bi_size overflow check in __bio_try_merge_page

In reply to Christoph Hellwig

Checking 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
index 5d2c95e05b1a52..93e6bca3c2239f 100644
@@ -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))
@@ -1197,6 +1195,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
#15 lore raw

PATCH 6/8 block: downgrade a bio_full call in bio_add_page

In reply to Christoph Hellwig

bio_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
index 93e6bca3c2239f..89b1475de0c370 100644
@@ -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
#17 lore raw

PATCH 7/8 block: move the bi_size update out of __bio_try_merge_page

In reply to Christoph Hellwig

The 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
index 89b1475de0c370..106009707ca1c5 100644
@@ -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;
@@ -1199,7 +1180,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)
 			put_page(page);
 		return 0;
Signature · 1 line
2.39.2
#19 lore raw

PATCH 8/8 block: don't pass a bio to bio_try_merge_hw_seg

In reply to Christoph Hellwig

There 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
index 106009707ca1c5..79e8aa600ddbe2 100644
@@ -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
Reviewed-by on v1 8/8
#20 lore raw

RE: [PATCH 8/8] block: don't pass a bio to bio_try_merge_hw_seg

block/bio.c static bool bvec_try_merge_page(struct bio_vec *bv, struct page *page, · line 940

Quoted 11 lines
block/bio.c +3 -3
@@ -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)
 {

There is a static because there is no external call based on the current change point. So, I will modify it when using this function later.
Thank you for organizing the functions that can be used for common use.

Looks good to me,

maintainer
#21 lore raw
In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig

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..)

Diffstat:

 bio.c |  123 ++++++++++++++++++++++++++++--------------------------------------
 1 file changed, 53 insertions(+), 70 deletions(-)

Nice cleanups and generated code reduction too.

Signature · 1 line
Jens Axboe
maintainer Applied
#22 lore raw
In reply to Christoph Hellwig
Quoted 11 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_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..)

Diffstat:

 bio.c |  123 ++++++++++++++++++++++++++++--------------------------------------
 1 file changed, 53 insertions(+), 70 deletions(-)

[...]

Applied, thanks!

[1/8] block: tidy up the bio full checks in bio_add_hw_page
(no commit info)
[2/8] block: use SECTOR_SHIFT bio_add_hw_page
(no commit info)
[3/8] block: move the BIO_CLONED checks out of __bio_try_merge_page
(no commit info)
[4/8] block: move the bi_vcnt check out of __bio_try_merge_page
(no commit info)
[5/8] block: move the bi_size overflow check in __bio_try_merge_page
(no commit info)
[6/8] block: downgrade a bio_full call in bio_add_page
(no commit info)
[7/8] block: move the bi_size update out of __bio_try_merge_page
(no commit info)
[8/8] block: don't pass a bio to bio_try_merge_hw_seg
(no commit info)

Best regards,

Signature · 1 line
Jens Axboe