bio: limit bio max size

bio size can grow up to 4GB when muli-page bvec is enabled.
but sometimes it would lead to inefficient behaviors.
in case of large chunk direct I/O, - 32MB chunk read in user space -
all pages for 32MB would be merged to a bio structure if the pages
physical addresses are contiguous. it makes some delay to submit
until merge complete. bio max size should be limited to a proper size.

When 32MB chunk read with direct I/O option is coming from userspace,
kernel behavior is below now in do_direct_IO() loop. it's timeline.

| bio merge for 32MB. total 8,192 pages are merged.
| total elapsed time is over 2ms.
|------------------ ... ----------------------->|
| 8,192 pages merged a bio.
| at this time, first bio submit is done.
| 1 bio is split to 32 read request and issue.
|--------------->
|--------------->
|--------------->
......
|--------------->
|--------------->|
total 19ms elapsed to complete 32MB read done from device. |

If bio max size is limited with 1MB, behavior is changed below.

| bio merge for 1MB. 256 pages are merged for each bio.
| total 32 bio will be made.
| total elapsed time is over 2ms. it's same.
| but, first bio submit timing is fast. about 100us.
|--->|--->|--->|---> ... -->|--->|--->|--->|--->|
| 256 pages merged a bio.
| at this time, first bio submit is done.
| and 1 read request is issued for 1 bio.
|--------------->
|--------------->
|--------------->
......
|--------------->
|--------------->|
total 17ms elapsed to complete 32MB read done from device. |

As a result, read request issue timing is faster if bio max size is limited.
Current kernel behavior with multipage bvec, super large bio can be created.
And it lead to delay first I/O request issue.

Signed-off-by: Changheun Lee <nanich.lee@samsung.com>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Link: https://lore.kernel.org/r/20210503095203.29076-1-nanich.lee@samsung.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by Changheun Lee and committed by Jens Axboe cd2c7545 c646790a

+21 -3
+11 -2
block/bio.c
··· 255 255 } 256 256 EXPORT_SYMBOL(bio_init); 257 257 258 + unsigned int bio_max_size(struct bio *bio) 259 + { 260 + struct block_device *bdev = bio->bi_bdev; 261 + 262 + return bdev ? bdev->bd_disk->queue->limits.bio_max_bytes : UINT_MAX; 263 + } 264 + 258 265 /** 259 266 * bio_reset - reinitialize a bio 260 267 * @bio: bio to reset ··· 873 866 struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1]; 874 867 875 868 if (page_is_mergeable(bv, page, len, off, same_page)) { 876 - if (bio->bi_iter.bi_size > UINT_MAX - len) { 869 + if (bio->bi_iter.bi_size > bio_max_size(bio) - len) { 877 870 *same_page = false; 878 871 return false; 879 872 } ··· 1002 995 { 1003 996 unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt; 1004 997 unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt; 998 + unsigned int bytes_left = bio_max_size(bio) - bio->bi_iter.bi_size; 1005 999 struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt; 1006 1000 struct page **pages = (struct page **)bv; 1007 1001 bool same_page = false; ··· 1018 1010 BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2); 1019 1011 pages += entries_left * (PAGE_PTRS_PER_BVEC - 1); 1020 1012 1021 - size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset); 1013 + size = iov_iter_get_pages(iter, pages, bytes_left, nr_pages, 1014 + &offset); 1022 1015 if (unlikely(size <= 0)) 1023 1016 return size ? size : -EFAULT; 1024 1017
+5
block/blk-settings.c
··· 31 31 */ 32 32 void blk_set_default_limits(struct queue_limits *lim) 33 33 { 34 + lim->bio_max_bytes = UINT_MAX; 34 35 lim->max_segments = BLK_MAX_SEGMENTS; 35 36 lim->max_discard_segments = 1; 36 37 lim->max_integrity_segments = 0; ··· 139 138 max_sectors = round_down(max_sectors, 140 139 limits->logical_block_size >> SECTOR_SHIFT); 141 140 limits->max_sectors = max_sectors; 141 + 142 + if (check_shl_overflow(max_sectors, SECTOR_SHIFT, 143 + &limits->bio_max_bytes)) 144 + limits->bio_max_bytes = UINT_MAX; 142 145 143 146 q->backing_dev_info->io_pages = max_sectors >> (PAGE_SHIFT - 9); 144 147 }
+3 -1
include/linux/bio.h
··· 106 106 return NULL; 107 107 } 108 108 109 + extern unsigned int bio_max_size(struct bio *bio); 110 + 109 111 /** 110 112 * bio_full - check if the bio is full 111 113 * @bio: bio to check ··· 121 119 if (bio->bi_vcnt >= bio->bi_max_vecs) 122 120 return true; 123 121 124 - if (bio->bi_iter.bi_size > UINT_MAX - len) 122 + if (bio->bi_iter.bi_size > bio_max_size(bio) - len) 125 123 return true; 126 124 127 125 return false;
+2
include/linux/blkdev.h
··· 327 327 }; 328 328 329 329 struct queue_limits { 330 + unsigned int bio_max_bytes; 331 + 330 332 enum blk_bounce bounce; 331 333 unsigned long seg_boundary_mask; 332 334 unsigned long virt_boundary_mask;