Skip to contentSkip to conversation

block: try to make aligned bio in case of big chunk IO

PATCH Superseded Ming Lei 3 participants 6 messages Nov 7, 2023 – Nov 8, 2023 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 block: try to make aligned bio in case of big chunk IO

In case of big chunk sequential IO, bio's size is often not aligned with this queue's max request size because of multipage bvec, then small sized bio can be made by bio split, so try to align bio with max io size if it isn't the last one.

Ed Tsai reported this way improves 64MB read/write by > 15%~25% in Antutu V10 Storage Test.

---
 block/bio.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
block/bio.c +57 -0
index 816d412c06e9..749b6283dab9 100644
@@ -1294,6 +1294,47 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 	return ret;
 }
 
+/* should only be called before submission */
+static void bio_shrink(struct bio *bio, unsigned bytes)
+{
+	unsigned int size = bio->bi_iter.bi_size;
+	int idx;
+
+	if (bytes >= size)
+		return;
+
+	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
+
+	idx = bio->bi_vcnt - 1;
+	bio->bi_iter.bi_size -= bytes;
+	while (bytes > 0) {
+		struct bio_vec *bv = &bio->bi_io_vec[idx];
+		unsigned int len = min_t(unsigned, bv->bv_len, bytes);
+
+		bytes -= len;
+		bv->bv_len -= len;
+		if (!bv->bv_len) {
+			bio_release_page(bio, bv->bv_page);
+			idx--;
+		}
+	}
+	WARN_ON_ONCE(idx < 0);
+	bio->bi_vcnt = idx + 1;
+}
+
+static unsigned bio_align_with_io_size(struct bio *bio)
+{
+	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
+	unsigned int size = bio->bi_iter.bi_size;
+	unsigned int trim = size & ((queue_max_sectors(q) << 9) - 1);
+
+	if (trim && trim != size) {
+		bio_shrink(bio, trim);
+		return trim;
+	}
+	return 0;
+}
+
 /**
  * bio_iov_iter_get_pages - add user or kernel pages to a bio
  * @bio: bio to add pages to
@@ -1333,6 +1374,22 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 		ret = __bio_iov_iter_get_pages(bio, iter);
 	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
 
+
+	/*
+	 * If we still have data and bio is full, this bio size may not be
+	 * aligned with max io size, small bio can be caused by split, try
+	 * to avoid this situation by aligning bio with max io size.
+	 *
+	 * Big chunk of sequential IO workload can benefit from this way.
+	 */
+	if (!ret && iov_iter_count(iter) && bio->bi_bdev &&
+			bio_op(bio) != REQ_OP_ZONE_APPEND) {
+		unsigned trim = bio_align_with_io_size(bio);
+
+		if (trim)
+			iov_iter_revert(iter, trim);
+	}
+
 	return bio->bi_vcnt ? 0 : ret;
 }
 EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages);
Signature · 1 line
2.41.0
#1 lore raw
Quoted 4 lines · Ming Lei

In case of big chunk sequential IO, bio's size is often not aligned with this queue's max request size because of multipage bvec, then small sized bio can be made by bio split, so try to align bio with max io size if it isn't the last one.

I have a hard time parsing this long sentence.

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

Quoted 10 lines
block/bio.c +10 -0
+	/*
+	 * If we still have data and bio is full, this bio size may not be
+	 * aligned with max io size, small bio can be caused by split, try
+	 * to avoid this situation by aligning bio with max io size.
+	 *
+	 * Big chunk of sequential IO workload can benefit from this way.
+	 */
+	if (!ret && iov_iter_count(iter) && bio->bi_bdev &&
+			bio_op(bio) != REQ_OP_ZONE_APPEND) {
+		unsigned trim = bio_align_with_io_size(bio);

Besides the fact that bi_bdev should always be set, this really does thing backwards. Instead of rewding things which is really expensive don't add it in the first place.

Acked-by on v1 1/1
#5 lore raw
In reply to Ming Lei
Quoted 101 lines · Ming Lei

In case of big chunk sequential IO, bio's size is often not aligned with
this queue's max request size because of multipage bvec, then small sized
bio can be made by bio split, so try to align bio with max io size if it isn't the last one.

Ed Tsai reported this way improves 64MB read/write by > 15%~25% in Antutu V10 Storage Test.

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

---
 block/bio.c | 57
+++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
block/bio.c +41 -0
index 816d412c06e9..749b6283dab9 100644
@@ -1294,6 +1294,47 @@ static int __bio_iov_iter_get_pages(struct bio
 *bio, struct iov_iter *iter)
 	return ret;
 }
 
+/* should only be called before submission */
+static void bio_shrink(struct bio *bio, unsigned bytes)
+{
+	unsigned int size = bio->bi_iter.bi_size;
+	int idx;
+
+	if (bytes >= size)
+		return;
+
+	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
+
+	idx = bio->bi_vcnt - 1;
+	bio->bi_iter.bi_size -= bytes;
+	while (bytes > 0) {
+		struct bio_vec *bv = &bio->bi_io_vec[idx];
+		unsigned int len = min_t(unsigned, bv->bv_len, bytes);
+
+		bytes -= len;
+		bv->bv_len -= len;
+		if (!bv->bv_len) {
+			bio_release_page(bio, bv->bv_page);
+			idx--;
+		}
+	}
+	WARN_ON_ONCE(idx < 0);
+	bio->bi_vcnt = idx + 1;
+}
+
+static unsigned bio_align_with_io_size(struct bio *bio)
+{
+	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
+	unsigned int size = bio->bi_iter.bi_size;
+	unsigned int trim = size & ((queue_max_sectors(q) << 9) - 1);
+
+	if (trim && trim != size) {
+		bio_shrink(bio, trim);
+		return trim;
+	}
+	return 0;
+}
+
 /**
  * bio_iov_iter_get_pages - add user or kernel pages to a bio

* @bio: bio to add pages to

block/bio.c +3 -0
@@ -1333,6 +1374,22 @@ int bio_iov_iter_get_pages(struct bio *bio,
 struct iov_iter *iter)
 		ret = __bio_iov_iter_get_pages(bio, iter);
 	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
 
+
+	/*
+	 * If we still have data and bio is full, this bio size may not

be

block/bio.c +1 -0
+	 * aligned with max io size, small bio can be caused by split,

try

block/bio.c +3 -0
+	 * to avoid this situation by aligning bio with max io size.
+	 *
+	 * Big chunk of sequential IO workload can benefit from this

way.

block/bio.c +9 -0
+	 */
+	if (!ret && iov_iter_count(iter) && bio->bi_bdev &&
+			bio_op(bio) != REQ_OP_ZONE_APPEND) {
+		unsigned trim = bio_align_with_io_size(bio);
+
+		if (trim)
+			iov_iter_revert(iter, trim);
+	}
+
 	return bio->bi_vcnt ? 0 : ret;
 }
 EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages);
Signature · 1 line
2.41.0

Thanks. This looks good to me.

--
Best Regards,
Ed Tsai

author
#2 lore raw
In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Ming Lei

In case of big chunk sequential IO, bio's size is often not aligned with this queue's max request size because of multipage bvec, then small sized bio can be made by bio split, so try to align bio with max io size if it isn't the last one.

I have a hard time parsing this long sentence.

It covers the reasons(multipage bvec, bio split, big sequential IO) and solution( align bio), or any suggestion to simplify it further?

Quoted 14 lines

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

Quoted 11 lines
block/bio.c +10 -0
+	/*
+	 * If we still have data and bio is full, this bio size may not be
+	 * aligned with max io size, small bio can be caused by split, try
+	 * to avoid this situation by aligning bio with max io size.
+	 *
+	 * Big chunk of sequential IO workload can benefit from this way.
+	 */
+	if (!ret && iov_iter_count(iter) && bio->bi_bdev &&
+			bio_op(bio) != REQ_OP_ZONE_APPEND) {
+		unsigned trim = bio_align_with_io_size(bio);

Besides the fact that bi_bdev should always be set, this really does thing backwards.

Looks it is true after your patch passes bdev to bio_alloc*(), but bio_add_page() is just fine without bio->bi_bdev.

Also this way follows the check for aligning with block size.

But seems safe to remove the check.

Quoted 2 lines

Instead of rewding things which is really
expensive don't add it in the first place.

The rewind may not be avoided because iov_iter_extract_pages() can often return less pages than requested. Also rewind is only done after the bio becomes full(at least 1MB bytes are added) and there is still data left, so it shouldn't be too expensive.

I will provide 'size' hint to iov_iter_extract_pages() to try to make it aligned from the beginning in next version, but rewind may not be avoided.

Thanks,
Ming

#4 lore raw
In reply to Christoph Hellwig
Quoted 12 lines · Christoph Hellwig

you have verified the sender or the content.

Quoted 1 line · Ming Lei

In case of big chunk sequential IO, bio's size is often not aligned

with

Quoted 1 line

this queue's max request size because of multipage bvec, then small

sized

Quoted 1 line

bio can be made by bio split, so try to align bio with max io size

if

Quoted 1 line

it isn't the last one.

I have a hard time parsing this long sentence.

Quoted 15 lines

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

Quoted 10 lines
block/bio.c +10 -0
+/*
+ * If we still have data and bio is full, this bio size may not be
+ * aligned with max io size, small bio can be caused by split, try
+ * to avoid this situation by aligning bio with max io size.
+ *
+ * Big chunk of sequential IO workload can benefit from this way.
+ */
+if (!ret && iov_iter_count(iter) && bio->bi_bdev &&
+bio_op(bio) != REQ_OP_ZONE_APPEND) {
+unsigned trim = bio_align_with_io_size(bio);

Besides the fact that bi_bdev should always be set, this really does thing backwards. Instead of rewding things which is really expensive don't add it in the first place.

We cannot predict the number of pages that can be filled in a bio as it depends on the physical layout of memory buffer. The only option is to limit the bio to the queue limit.

Even if I fill the bio with a large amount of pages, it will still be split based on the queue limit at block layer. Therefore, I believe it is appropriate to limit the bio size in this case.

We can enforce this limitation before extracting the page, which would elimiate the need for iov_iter_revert.

--
Best Regards,
Ed Tsai

#3 lore raw
In reply to Ming Lei
Quoted 53 lines · Ming Lei
Quoted 2 lines · Christoph Hellwig
Quoted 1 line · Ming Lei

In case of big chunk sequential IO, bio's size is often not

aligned with

Quoted 1 line
Quoted 1 line

this queue's max request size because of multipage bvec, then

small sized

Quoted 1 line
Quoted 1 line

bio can be made by bio split, so try to align bio with max io

size if

Quoted 3 lines
Quoted 1 line

it isn't the last one.

I have a hard time parsing this long sentence.

It covers the reasons(multipage bvec, bio split, big sequential IO) and solution(
align bio), or any suggestion to simplify it further?

Quoted 3 lines
Quoted 3 lines

+/*
+ * If we still have data and bio is full, this bio size may not

be

Quoted 1 line
Quoted 1 line

+ * aligned with max io size, small bio can be caused by split,

try

Quoted 3 lines
Quoted 3 lines

+ * to avoid this situation by aligning bio with max io size.
+ *
+ * Big chunk of sequential IO workload can benefit from this

way.

Quoted 6 lines

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

Quoted 4 lines

+ */

block/bio.c +3 -0
+if (!ret && iov_iter_count(iter) && bio->bi_bdev &&
+bio_op(bio) != REQ_OP_ZONE_APPEND) {
+unsigned trim = bio_align_with_io_size(bio);

Besides the fact that bi_bdev should always be set, this really

does

Quoted 1 line

thing backwards.

Looks it is true after your patch passes bdev to bio_alloc*(), but bio_add_page() is just fine without bio->bi_bdev.

Also this way follows the check for aligning with block size.

But seems safe to remove the check.

Quoted 2 lines

Instead of rewding things which is really
expensive don't add it in the first place.

The rewind may not be avoided because iov_iter_extract_pages() can often
return less pages than requested. Also rewind is only done after the bio
becomes full(at least 1MB bytes are added) and there is still data left,
so it shouldn't be too expensive.

Quoted 7 lines

I will provide 'size' hint to iov_iter_extract_pages() to try to make it aligned from the beginning in next version, but rewind may not be avoided.

Thanks,
Ming

We cannot predict the number of pages that can be filled in a bio as it depends on the physical layout of memory buffer. The only option is to limit the bio to the queue limit.

Even if I fill the bio with a large amount of pages, it will still be split based on the queue limit at block layer. Therefore, I believe it is appropriate to limit the bio size in this case.

We can enforce this limitation before extracting the page, which would elimiate the need for iov_iter_revert.

Hi Christoph, what stage do you think would be better to impose the limitaion?

--
Best Regards,
Ed Tsai

Quoted 1 line