Skip to content

Commit 17d51b1

Browse files
mwilckaxboe
authored andcommitted
block: bio_iov_iter_get_pages: pin more pages for multi-segment IOs
bio_iov_iter_get_pages() currently only adds pages for the next non-zero segment from the iov_iter to the bio. That's suboptimal for callers, which typically try to pin as many pages as fit into the bio. This patch converts the current bio_iov_iter_get_pages() into a static helper, and introduces a new helper that allocates as many pages as 1) fit into the bio, 2) are present in the iov_iter, 3) and can be pinned by MM. Error is returned only if zero pages could be pinned. Because of 3), a zero return value doesn't necessarily mean all pages have been pinned. Callers that have to pin every page in the iov_iter must still call this function in a loop (this is currently the case). This change matters most for __blkdev_direct_IO_simple(), which calls bio_iov_iter_get_pages() only once. If it obtains less pages than requested, it returns a "short write" or "short read", and __generic_file_write_iter() falls back to buffered writes, which may lead to data corruption. Fixes: 72ecad2 ("block: support a full bio worth of IO for simplified bdev direct-io") Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Martin Wilck <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 9362dd1 commit 17d51b1

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

block/bio.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,14 +903,16 @@ int bio_add_page(struct bio *bio, struct page *page,
903903
EXPORT_SYMBOL(bio_add_page);
904904

905905
/**
906-
* bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
906+
* __bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
907907
* @bio: bio to add pages to
908908
* @iter: iov iterator describing the region to be mapped
909909
*
910-
* Pins as many pages from *iter and appends them to @bio's bvec array. The
910+
* Pins pages from *iter and appends them to @bio's bvec array. The
911911
* pages will have to be released using put_page() when done.
912+
* For multi-segment *iter, this function only adds pages from the
913+
* the next non-empty segment of the iov iterator.
912914
*/
913-
int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
915+
static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
914916
{
915917
unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt, idx;
916918
struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
@@ -947,6 +949,33 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
947949
iov_iter_advance(iter, size);
948950
return 0;
949951
}
952+
953+
/**
954+
* bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
955+
* @bio: bio to add pages to
956+
* @iter: iov iterator describing the region to be mapped
957+
*
958+
* Pins pages from *iter and appends them to @bio's bvec array. The
959+
* pages will have to be released using put_page() when done.
960+
* The function tries, but does not guarantee, to pin as many pages as
961+
* fit into the bio, or are requested in *iter, whatever is smaller.
962+
* If MM encounters an error pinning the requested pages, it stops.
963+
* Error is returned only if 0 pages could be pinned.
964+
*/
965+
int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
966+
{
967+
unsigned short orig_vcnt = bio->bi_vcnt;
968+
969+
do {
970+
int ret = __bio_iov_iter_get_pages(bio, iter);
971+
972+
if (unlikely(ret))
973+
return bio->bi_vcnt > orig_vcnt ? 0 : ret;
974+
975+
} while (iov_iter_count(iter) && !bio_full(bio));
976+
977+
return 0;
978+
}
950979
EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages);
951980

952981
static void submit_bio_wait_endio(struct bio *bio)

0 commit comments

Comments
 (0)