Skip to contentSkip to conversation

fix block layer bounce buffering for block size > PAGE_SIZE

PATCH In review Christoph Hellwig 3 participants 7 messages Apr 30 – May 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi all,

this series has two fixes that make the new block layer bounce buffering code work for the block size > PAGE_SIZE case.

Diffstat:

 block/bio.c          |   27 +++++++++++++++------------
 fs/iomap/direct-io.c |    2 +-
 include/linux/bio.h  |    3 ++-
 3 files changed, 18 insertions(+), 14 deletions(-)
#1 lore raw

PATCH 1/2 block: pass a minsize argument to bio_iov_iter_bounce

When bouncing for block size > PAGE_SIZE file systems, the bio needs to be big enough to fit an entire block.

---
 block/bio.c          | 23 +++++++++++++----------
 fs/iomap/direct-io.c |  2 +-
 include/linux/bio.h  |  3 ++-
 3 files changed, 16 insertions(+), 12 deletions(-)
block/bio.c +13 -10
index b8972dba68a0..f3e5d8bea08c 100644
@@ -1279,11 +1279,12 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter,
 	return bio_iov_iter_align_down(bio, iter, len_align_mask);
 }
 
-static struct folio *folio_alloc_greedy(gfp_t gfp, size_t *size)
+static struct folio *folio_alloc_greedy(gfp_t gfp, size_t *size,
+		size_t minsize)
 {
 	struct folio *folio;
 
-	while (*size > PAGE_SIZE) {
+	while (*size > minsize) {
 		folio = folio_alloc(gfp | __GFP_NORETRY, get_order(*size));
 		if (folio)
 			return folio;
@@ -1307,7 +1308,7 @@ static void bio_free_folios(struct bio *bio)
 }
 
 static int bio_iov_iter_bounce_write(struct bio *bio, struct iov_iter *iter,
-		size_t maxlen)
+		size_t maxlen, size_t minsize)
 {
 	size_t total_len = min(maxlen, iov_iter_count(iter));
 
@@ -1322,13 +1323,13 @@ static int bio_iov_iter_bounce_write(struct bio *bio, struct iov_iter *iter,
 		size_t this_len = min(total_len, SZ_1M);
 		struct folio *folio;
 
-		if (this_len > PAGE_SIZE * 2)
+		if (this_len > minsize * 2)
 			this_len = rounddown_pow_of_two(this_len);
 
 		if (bio->bi_iter.bi_size > BIO_MAX_SIZE - this_len)
 			break;
 
-		folio = folio_alloc_greedy(GFP_KERNEL, &this_len);
+		folio = folio_alloc_greedy(GFP_KERNEL, &this_len, minsize);
 		if (!folio)
 			break;
 		bio_add_folio_nofail(bio, folio, this_len, 0);
@@ -1348,12 +1349,12 @@ static int bio_iov_iter_bounce_write(struct bio *bio, struct iov_iter *iter,
 }
 
 static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter,
-		size_t maxlen)
+		size_t maxlen, size_t minsize)
 {
 	size_t len = min3(iov_iter_count(iter), maxlen, SZ_1M);
 	struct folio *folio;
 
-	folio = folio_alloc_greedy(GFP_KERNEL, &len);
+	folio = folio_alloc_greedy(GFP_KERNEL, &len, minsize);
 	if (!folio)
 		return -ENOMEM;
 
@@ -1390,6 +1391,7 @@ static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter,
  * @bio:	bio to send
  * @iter:	iter to read from / write into
  * @maxlen:	maximum size to bounce
+ * @minsize:	minimum folio allocation size
  *
  * Helper for direct I/O implementations that need to bounce buffer because
  * we need to checksum the data or perform other operations that require
@@ -1397,11 +1399,12 @@ static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter,
  * copies the data into it.  Needs to be paired with bio_iov_iter_unbounce()
  * called on completion.
  */
-int bio_iov_iter_bounce(struct bio *bio, struct iov_iter *iter, size_t maxlen)
+int bio_iov_iter_bounce(struct bio *bio, struct iov_iter *iter, size_t maxlen,
+			size_t minsize)
 {
 	if (op_is_write(bio_op(bio)))
-		return bio_iov_iter_bounce_write(bio, iter, maxlen);
-	return bio_iov_iter_bounce_read(bio, iter, maxlen);
+		return bio_iov_iter_bounce_write(bio, iter, maxlen, minsize);
+	return bio_iov_iter_bounce_read(bio, iter, maxlen, minsize);
 }
 
 static void bvec_unpin(struct bio_vec *bv, bool mark_dirty)
fs/iomap/direct-io.c +1 -1
index b0a6549b3848..b36ee619cdcd 100644
@@ -355,7 +355,7 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter,
 
 	if (dio->flags & IOMAP_DIO_BOUNCE)
 		ret = bio_iov_iter_bounce(bio, dio->submit.iter,
-				iomap_max_bio_size(&iter->iomap));
+				iomap_max_bio_size(&iter->iomap), alignment);
 	else
 		ret = bio_iov_iter_get_pages(bio, dio->submit.iter,
 					     alignment - 1);
include/linux/bio.h +2 -1
index 97d747320b35..dc17780d6c1e 100644
@@ -475,7 +475,8 @@ void __bio_release_pages(struct bio *bio, bool mark_dirty);
 extern void bio_set_pages_dirty(struct bio *bio);
 extern void bio_check_pages_dirty(struct bio *bio);
 
-int bio_iov_iter_bounce(struct bio *bio, struct iov_iter *iter, size_t maxlen);
+int bio_iov_iter_bounce(struct bio *bio, struct iov_iter *iter, size_t maxlen,
+		size_t minsize);
 void bio_iov_iter_unbounce(struct bio *bio, bool is_error, bool mark_dirty);
 
 extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/2] block: pass a minsize argument to bio_iov_iter_bounce

block/bio.c int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter, · line 1290

Quoted 11 lines · Christoph Hellwig
block/bio.c +3 -2
-static struct folio *folio_alloc_greedy(gfp_t gfp, size_t *size)
+static struct folio *folio_alloc_greedy(gfp_t gfp, size_t *size,
+		size_t minsize)
 {
 	struct folio *folio;
 
-	while (*size > PAGE_SIZE) {
+	while (*size > minsize) {
 		folio = folio_alloc(gfp | __GFP_NORETRY, get_order(*size));
 		if (folio)
 			return folio;

Seems a bit inefficient. How about something like this:

static struct folio *folio_alloc_greedy(gfp_t gfp, size_t *size, size_t minsize)

{
        unsigned int order = get_order(*size);
        unsigned int minorder = get_order(minsize);
        struct folio *folio;

        while (order > minorder) {
                folio = folio_alloc(gfp | __GFP_NORETRY, order);
                if (folio)
                        return folio;
                order--;
                *size = 1UL << order;
        }

        return folio_alloc(gfp, order);
}
#3 lore raw

Re: [PATCH 1/2] block: pass a minsize argument to bio_iov_iter_bounce

Quoted 1 line · Matthew Wilcox

Seems a bit inefficient. How about something like this:

Works maybe a bit more efficiently, but also is more code and harder to read. So unless it actualy makes a difference in a relevant way, why bother?

#4 lore raw

Re: [PATCH 1/2] block: pass a minsize argument to bio_iov_iter_bounce

In reply to Christoph Hellwig
Quoted 2 lines · Christoph Hellwig

When bouncing for block size > PAGE_SIZE file systems, the bio needs to be big enough to fit an entire block.

We set the alignment to fs_block_size when IOMAP_DIO_FSBLOCK_ALIGNED is set. And we always set IOMAP_DIO_FSBLOCK_ALIGNED in btrfs for dio reads/writes. IIUC, it was added to support bs > PS block sizes in btrfs. But in XFS, we set it only for CoW inodes (xfs_is_always_cow_inode is true).

The commit message seems to indicate this is needed for all filesystems that support bs > PS configurations. Am I missing something here?

Otherwise the patch looks good to me.

Quoted 8 lines
---
 block/bio.c          | 23 +++++++++++++----------
 fs/iomap/direct-io.c |  2 +-
 include/linux/bio.h  |  3 ++-
 3 files changed, 16 insertions(+), 12 deletions(-)
Signature · 1 line
Pankaj
#5 lore raw

Re: [PATCH 1/2] block: pass a minsize argument to bio_iov_iter_bounce

Quoted 8 lines · Pankaj Raghav (Samsung)
Quoted 2 lines · Christoph Hellwig

When bouncing for block size > PAGE_SIZE file systems, the bio needs to be big enough to fit an entire block.

We set the alignment to fs_block_size when IOMAP_DIO_FSBLOCK_ALIGNED is set. And we always set IOMAP_DIO_FSBLOCK_ALIGNED in btrfs for dio reads/writes. IIUC, it was added to support bs > PS block sizes in btrfs. But in XFS, we set it only for CoW inodes (xfs_is_always_cow_inode is true).

Yes, I ran into this with zoned XFS on large block size.

Quoted 3 lines

The commit message seems to indicate this is needed for all filesystems that support bs > PS configurations. Am I missing something here?

No, the commit message could be a bit more exact.

#6 lore raw

PATCH 2/2 block: align down bounces bios

In reply to Christoph Hellwig

Just like for the extract user pages path, we need to align down the size to the supported boundary.

---
 block/bio.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/bio.c +2 -2
index f3e5d8bea08c..5f10900b3f42 100644
@@ -1345,7 +1345,7 @@ static int bio_iov_iter_bounce_write(struct bio *bio, struct iov_iter *iter,
 
 	if (!bio->bi_iter.bi_size)
 		return -ENOMEM;
-	return 0;
+	return bio_iov_iter_align_down(bio, iter, minsize - 1);
 }
 
 static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter,
@@ -1383,7 +1383,7 @@ static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter,
 	bvec_set_folio(&bio->bi_io_vec[0], folio, bio->bi_iter.bi_size, 0);
 	if (iov_iter_extract_will_pin(iter))
 		bio_set_flag(bio, BIO_PAGE_PINNED);
-	return 0;
+	return bio_iov_iter_align_down(bio, iter, minsize - 1);
 }
 
 /**
Signature · 1 line
2.53.0