]> git.proxmox.com Git - pve-kernel.git/blob - patches/kernel/0012-block-bio_iov_iter_get_pages-pin-more-pages-for-mult.patch
644b5640856f10b153169273171d601485795d23
[pve-kernel.git] / patches / kernel / 0012-block-bio_iov_iter_get_pages-pin-more-pages-for-mult.patch
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Martin Wilck <mwilck@suse.com>
3 Date: Tue, 9 Oct 2018 17:04:42 +0100
4 Subject: [PATCH] block: bio_iov_iter_get_pages: pin more pages for
5 multi-segment IOs
6
7 Buglink: https://bugs.launchpad.net/bugs/1796542
8
9 bio_iov_iter_get_pages() currently only adds pages for the next non-zero
10 segment from the iov_iter to the bio. That's suboptimal for callers,
11 which typically try to pin as many pages as fit into the bio. This patch
12 converts the current bio_iov_iter_get_pages() into a static helper, and
13 introduces a new helper that allocates as many pages as
14
15 1) fit into the bio,
16 2) are present in the iov_iter,
17 3) and can be pinned by MM.
18
19 Error is returned only if zero pages could be pinned. Because of 3), a
20 zero return value doesn't necessarily mean all pages have been pinned.
21 Callers that have to pin every page in the iov_iter must still call this
22 function in a loop (this is currently the case).
23
24 This change matters most for __blkdev_direct_IO_simple(), which calls
25 bio_iov_iter_get_pages() only once. If it obtains less pages than
26 requested, it returns a "short write" or "short read", and
27 __generic_file_write_iter() falls back to buffered writes, which may
28 lead to data corruption.
29
30 Fixes: 72ecad22d9f1 ("block: support a full bio worth of IO for simplified bdev direct-io")
31 Reviewed-by: Christoph Hellwig <hch@lst.de>
32 Signed-off-by: Martin Wilck <mwilck@suse.com>
33 Signed-off-by: Jens Axboe <axboe@kernel.dk>
34 (cherry picked from commit 17d51b10d7773e4618bcac64648f30f12d4078fb)
35 Signed-off-by: Colin Ian King <colin.king@canonical.com>
36 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
37 ---
38 block/bio.c | 35 ++++++++++++++++++++++++++++++++---
39 1 file changed, 32 insertions(+), 3 deletions(-)
40
41 diff --git a/block/bio.c b/block/bio.c
42 index d76372a6a5fe..415c65b9c590 100644
43 --- a/block/bio.c
44 +++ b/block/bio.c
45 @@ -902,14 +902,16 @@ int bio_add_page(struct bio *bio, struct page *page,
46 EXPORT_SYMBOL(bio_add_page);
47
48 /**
49 - * bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
50 + * __bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
51 * @bio: bio to add pages to
52 * @iter: iov iterator describing the region to be mapped
53 *
54 - * Pins as many pages from *iter and appends them to @bio's bvec array. The
55 + * Pins pages from *iter and appends them to @bio's bvec array. The
56 * pages will have to be released using put_page() when done.
57 + * For multi-segment *iter, this function only adds pages from the
58 + * the next non-empty segment of the iov iterator.
59 */
60 -int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
61 +static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
62 {
63 unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt, idx;
64 struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
65 @@ -946,6 +948,33 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
66 iov_iter_advance(iter, size);
67 return 0;
68 }
69 +
70 +/**
71 + * bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
72 + * @bio: bio to add pages to
73 + * @iter: iov iterator describing the region to be mapped
74 + *
75 + * Pins pages from *iter and appends them to @bio's bvec array. The
76 + * pages will have to be released using put_page() when done.
77 + * The function tries, but does not guarantee, to pin as many pages as
78 + * fit into the bio, or are requested in *iter, whatever is smaller.
79 + * If MM encounters an error pinning the requested pages, it stops.
80 + * Error is returned only if 0 pages could be pinned.
81 + */
82 +int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
83 +{
84 + unsigned short orig_vcnt = bio->bi_vcnt;
85 +
86 + do {
87 + int ret = __bio_iov_iter_get_pages(bio, iter);
88 +
89 + if (unlikely(ret))
90 + return bio->bi_vcnt > orig_vcnt ? 0 : ret;
91 +
92 + } while (iov_iter_count(iter) && !bio_full(bio));
93 +
94 + return 0;
95 +}
96 EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages);
97
98 static void submit_bio_wait_endio(struct bio *bio)