diff mbox series

[2/2] block: share more code for bio addition helpers

Message ID 20241105155235.460088-3-hch@lst.de (mailing list archive)
State New
Headers show
Series [1/2] block: remove bio_add_hw_folio | expand

Commit Message

Christoph Hellwig Nov. 5, 2024, 3:52 p.m. UTC
__bio_iov_iter_get_pages currently open codes adding pages to the bio,
which duplicates a lot of code from bio_add_page.  Add a lower level
bio_do_add_page helpers that passes down the same_page output argument
so that __bio_iov_iter_get_pages can reuse the code and also check
for the error return from it - while no error should happen due to
how the bvec space is used for the pin_user_space result I'd rather
be safe than sorry and actually check for these impossible errors.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 block/bio.c | 66 ++++++++++++++++++++++++-----------------------------
 1 file changed, 30 insertions(+), 36 deletions(-)

Comments

Kundan Kumar Nov. 6, 2024, 10:03 a.m. UTC | #1
On 05/11/24 04:52PM, Christoph Hellwig wrote:
>__bio_iov_iter_get_pages currently open codes adding pages to the bio,
>which duplicates a lot of code from bio_add_page.  Add a lower level
>bio_do_add_page helpers that passes down the same_page output argument
>so that __bio_iov_iter_get_pages can reuse the code and also check
>for the error return from it - while no error should happen due to
>how the bvec space is used for the pin_user_space result I'd rather
>be safe than sorry and actually check for these impossible errors.
>
>Signed-off-by: Christoph Hellwig <hch@lst.de>
>---
> block/bio.c | 66 ++++++++++++++++++++++++-----------------------------
> 1 file changed, 30 insertions(+), 36 deletions(-)
>
>diff --git a/block/bio.c b/block/bio.c
>index 1f6ac44b4881..bc3bca5f0686 100644
>--- a/block/bio.c
>+++ b/block/bio.c
>@@ -1063,20 +1063,9 @@ void __bio_add_page(struct bio *bio, struct page *page,
> }
> EXPORT_SYMBOL_GPL(__bio_add_page);
>
>-/**
>- *	bio_add_page	-	attempt to add page(s) to bio
>- *	@bio: destination bio
>- *	@page: start page to add
>- *	@len: vec entry length, may cross pages
>- *	@offset: vec entry offset relative to @page, may cross pages
>- *
>- *	Attempt to add page(s) to the bio_vec maplist. This will only fail
>- *	if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
>- */
>-int bio_add_page(struct bio *bio, struct page *page,
>-		 unsigned int len, unsigned int offset)
>+static int bio_do_add_page(struct bio *bio, struct page *page,
>+		 unsigned int len, unsigned int offset, bool *same_page)

As we are passing length within a folio, values will reach near UINT_MAX.
It will be better to make len and offset as size_t, also to add a check like :
if (len > UINT_MAX || offset > UINT_MAX)
		return 0; 

> {
>-	bool same_page = false;

nit: extra line got added
diff mbox series

Patch

diff --git a/block/bio.c b/block/bio.c
index 1f6ac44b4881..bc3bca5f0686 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -1063,20 +1063,9 @@  void __bio_add_page(struct bio *bio, struct page *page,
 }
 EXPORT_SYMBOL_GPL(__bio_add_page);
 
-/**
- *	bio_add_page	-	attempt to add page(s) to bio
- *	@bio: destination bio
- *	@page: start page to add
- *	@len: vec entry length, may cross pages
- *	@offset: vec entry offset relative to @page, may cross pages
- *
- *	Attempt to add page(s) to the bio_vec maplist. This will only fail
- *	if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
- */
-int bio_add_page(struct bio *bio, struct page *page,
-		 unsigned int len, unsigned int offset)
+static int bio_do_add_page(struct bio *bio, struct page *page,
+		 unsigned int len, unsigned int offset, bool *same_page)
 {
-	bool same_page = false;
 
 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
 		return 0;
@@ -1085,7 +1074,7 @@  int bio_add_page(struct bio *bio, struct page *page,
 
 	if (bio->bi_vcnt > 0 &&
 	    bvec_try_merge_page(&bio->bi_io_vec[bio->bi_vcnt - 1],
-				page, len, offset, &same_page)) {
+				page, len, offset, same_page)) {
 		bio->bi_iter.bi_size += len;
 		return len;
 	}
@@ -1095,6 +1084,24 @@  int bio_add_page(struct bio *bio, struct page *page,
 	__bio_add_page(bio, page, len, offset);
 	return len;
 }
+
+/**
+ * bio_add_page	- attempt to add page(s) to bio
+ * @bio: destination bio
+ * @page: start page to add
+ * @len: vec entry length, may cross pages
+ * @offset: vec entry offset relative to @page, may cross pages
+ *
+ * Attempt to add page(s) to the bio_vec maplist.  Will only fail if the
+ * bio is full, or it is incorrectly used on a cloned bio.
+ */
+int bio_add_page(struct bio *bio, struct page *page,
+		 unsigned int len, unsigned int offset)
+{
+	bool same_page = false;
+
+	return bio_do_add_page(bio, page, len, offset, &same_page);
+}
 EXPORT_SYMBOL(bio_add_page);
 
 void bio_add_folio_nofail(struct bio *bio, struct folio *folio, size_t len,
@@ -1159,27 +1166,6 @@  void bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
 	bio_set_flag(bio, BIO_CLONED);
 }
 
-static int bio_iov_add_folio(struct bio *bio, struct folio *folio, size_t len,
-			     size_t offset)
-{
-	bool same_page = false;
-
-	if (WARN_ON_ONCE(bio->bi_iter.bi_size > UINT_MAX - len))
-		return -EIO;
-
-	if (bio->bi_vcnt > 0 &&
-	    bvec_try_merge_page(&bio->bi_io_vec[bio->bi_vcnt - 1],
-				folio_page(folio, 0), len, offset,
-				&same_page)) {
-		bio->bi_iter.bi_size += len;
-		if (same_page && bio_flagged(bio, BIO_PAGE_PINNED))
-			unpin_user_folio(folio, 1);
-		return 0;
-	}
-	bio_add_folio_nofail(bio, folio, len, offset);
-	return 0;
-}
-
 static unsigned int get_contig_folio_len(unsigned int *num_pages,
 					 struct page **pages, unsigned int i,
 					 struct folio *folio, size_t left,
@@ -1274,6 +1260,7 @@  static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 	for (left = size, i = 0; left > 0; left -= len, i += num_pages) {
 		struct page *page = pages[i];
 		struct folio *folio = page_folio(page);
+		bool same_page = false;
 
 		folio_offset = ((size_t)folio_page_idx(folio, page) <<
 			       PAGE_SHIFT) + offset;
@@ -1286,7 +1273,14 @@  static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 			len = get_contig_folio_len(&num_pages, pages, i,
 						   folio, left, offset);
 
-		bio_iov_add_folio(bio, folio, len, folio_offset);
+		if (bio_do_add_page(bio, folio_page(folio, 0), len,
+				folio_offset, &same_page) != len) {
+			ret = -EINVAL;
+			break;
+		}
+
+		if (same_page && bio_flagged(bio, BIO_PAGE_PINNED))
+			unpin_user_folio(folio, 1);
 		offset = 0;
 	}