1 // SPDX-License-Identifier: GPL-2.0
2 /* bounce buffer handling for block devices
3 *
4 * - Split from highmem.c
5 */
6
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9 #include <linux/mm.h>
10 #include <linux/export.h>
11 #include <linux/swap.h>
12 #include <linux/gfp.h>
13 #include <linux/bio-integrity.h>
14 #include <linux/pagemap.h>
15 #include <linux/mempool.h>
16 #include <linux/blkdev.h>
17 #include <linux/backing-dev.h>
18 #include <linux/init.h>
19 #include <linux/hash.h>
20 #include <linux/highmem.h>
21 #include <linux/printk.h>
22 #include <asm/tlbflush.h>
23
24 #include <trace/events/block.h>
25 #include "blk.h"
26 #include "blk-cgroup.h"
27
28 #define POOL_SIZE 64
29 #define ISA_POOL_SIZE 16
30
31 static struct bio_set bounce_bio_set, bounce_bio_split;
32 static mempool_t page_pool;
33
init_bounce_bioset(void)34 static void init_bounce_bioset(void)
35 {
36 static bool bounce_bs_setup;
37 int ret;
38
39 if (bounce_bs_setup)
40 return;
41
42 ret = bioset_init(&bounce_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
43 BUG_ON(ret);
44
45 ret = bioset_init(&bounce_bio_split, BIO_POOL_SIZE, 0, 0);
46 BUG_ON(ret);
47 bounce_bs_setup = true;
48 }
49
init_emergency_pool(void)50 static __init int init_emergency_pool(void)
51 {
52 int ret;
53
54 #ifndef CONFIG_MEMORY_HOTPLUG
55 if (max_pfn <= max_low_pfn)
56 return 0;
57 #endif
58
59 ret = mempool_init_page_pool(&page_pool, POOL_SIZE, 0);
60 BUG_ON(ret);
61 pr_info("pool size: %d pages\n", POOL_SIZE);
62
63 init_bounce_bioset();
64 return 0;
65 }
66
67 __initcall(init_emergency_pool);
68
69 /*
70 * Simple bounce buffer support for highmem pages. Depending on the
71 * queue gfp mask set, *to may or may not be a highmem page. kmap it
72 * always, it will do the Right Thing
73 */
copy_to_high_bio_irq(struct bio * to,struct bio * from)74 static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
75 {
76 struct bio_vec tovec, fromvec;
77 struct bvec_iter iter;
78 /*
79 * The bio of @from is created by bounce, so we can iterate
80 * its bvec from start to end, but the @from->bi_iter can't be
81 * trusted because it might be changed by splitting.
82 */
83 struct bvec_iter from_iter = BVEC_ITER_ALL_INIT;
84
85 bio_for_each_segment(tovec, to, iter) {
86 fromvec = bio_iter_iovec(from, from_iter);
87 if (tovec.bv_page != fromvec.bv_page) {
88 /*
89 * fromvec->bv_offset and fromvec->bv_len might have
90 * been modified by the block layer, so use the original
91 * copy, bounce_copy_vec already uses tovec->bv_len
92 */
93 memcpy_to_bvec(&tovec, page_address(fromvec.bv_page) +
94 tovec.bv_offset);
95 }
96 bio_advance_iter(from, &from_iter, tovec.bv_len);
97 }
98 }
99
bounce_end_io(struct bio * bio)100 static void bounce_end_io(struct bio *bio)
101 {
102 struct bio *bio_orig = bio->bi_private;
103 struct bio_vec *bvec, orig_vec;
104 struct bvec_iter orig_iter = bio_orig->bi_iter;
105 struct bvec_iter_all iter_all;
106
107 /*
108 * free up bounce indirect pages used
109 */
110 bio_for_each_segment_all(bvec, bio, iter_all) {
111 orig_vec = bio_iter_iovec(bio_orig, orig_iter);
112 if (bvec->bv_page != orig_vec.bv_page) {
113 dec_zone_page_state(bvec->bv_page, NR_BOUNCE);
114 mempool_free(bvec->bv_page, &page_pool);
115 }
116 bio_advance_iter(bio_orig, &orig_iter, orig_vec.bv_len);
117 }
118
119 bio_orig->bi_status = bio->bi_status;
120 bio_endio(bio_orig);
121 bio_put(bio);
122 }
123
bounce_end_io_write(struct bio * bio)124 static void bounce_end_io_write(struct bio *bio)
125 {
126 bounce_end_io(bio);
127 }
128
bounce_end_io_read(struct bio * bio)129 static void bounce_end_io_read(struct bio *bio)
130 {
131 struct bio *bio_orig = bio->bi_private;
132
133 if (!bio->bi_status)
134 copy_to_high_bio_irq(bio_orig, bio);
135
136 bounce_end_io(bio);
137 }
138
bounce_clone_bio(struct bio * bio_src)139 static struct bio *bounce_clone_bio(struct bio *bio_src)
140 {
141 struct bvec_iter iter;
142 struct bio_vec bv;
143 struct bio *bio;
144
145 /*
146 * Pre immutable biovecs, __bio_clone() used to just do a memcpy from
147 * bio_src->bi_io_vec to bio->bi_io_vec.
148 *
149 * We can't do that anymore, because:
150 *
151 * - The point of cloning the biovec is to produce a bio with a biovec
152 * the caller can modify: bi_idx and bi_bvec_done should be 0.
153 *
154 * - The original bio could've had more than BIO_MAX_VECS biovecs; if
155 * we tried to clone the whole thing bio_alloc_bioset() would fail.
156 * But the clone should succeed as long as the number of biovecs we
157 * actually need to allocate is fewer than BIO_MAX_VECS.
158 *
159 * - Lastly, bi_vcnt should not be looked at or relied upon by code
160 * that does not own the bio - reason being drivers don't use it for
161 * iterating over the biovec anymore, so expecting it to be kept up
162 * to date (i.e. for clones that share the parent biovec) is just
163 * asking for trouble and would force extra work.
164 */
165 bio = bio_alloc_bioset(bio_src->bi_bdev, bio_segments(bio_src),
166 bio_src->bi_opf, GFP_NOIO, &bounce_bio_set);
167 if (bio_flagged(bio_src, BIO_REMAPPED))
168 bio_set_flag(bio, BIO_REMAPPED);
169 bio->bi_ioprio = bio_src->bi_ioprio;
170 bio->bi_write_hint = bio_src->bi_write_hint;
171 bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector;
172 bio->bi_iter.bi_size = bio_src->bi_iter.bi_size;
173
174 switch (bio_op(bio)) {
175 case REQ_OP_DISCARD:
176 case REQ_OP_SECURE_ERASE:
177 case REQ_OP_WRITE_ZEROES:
178 break;
179 default:
180 bio_for_each_segment(bv, bio_src, iter)
181 bio->bi_io_vec[bio->bi_vcnt++] = bv;
182 break;
183 }
184
185 if (bio_crypt_clone(bio, bio_src, GFP_NOIO) < 0)
186 goto err_put;
187
188 if (bio_integrity(bio_src) &&
189 bio_integrity_clone(bio, bio_src, GFP_NOIO) < 0)
190 goto err_put;
191
192 bio_clone_blkg_association(bio, bio_src);
193
194 return bio;
195
196 err_put:
197 bio_put(bio);
198 return NULL;
199 }
200
__blk_queue_bounce(struct bio * bio_orig,struct request_queue * q)201 struct bio *__blk_queue_bounce(struct bio *bio_orig, struct request_queue *q)
202 {
203 struct bio *bio;
204 int rw = bio_data_dir(bio_orig);
205 struct bio_vec *to, from;
206 struct bvec_iter iter;
207 unsigned i = 0, bytes = 0;
208 bool bounce = false;
209 int sectors;
210
211 bio_for_each_segment(from, bio_orig, iter) {
212 if (i++ < BIO_MAX_VECS)
213 bytes += from.bv_len;
214 if (PageHighMem(from.bv_page))
215 bounce = true;
216 }
217 if (!bounce)
218 return bio_orig;
219
220 /*
221 * Individual bvecs might not be logical block aligned. Round down
222 * the split size so that each bio is properly block size aligned,
223 * even if we do not use the full hardware limits.
224 */
225 sectors = ALIGN_DOWN(bytes, queue_logical_block_size(q)) >>
226 SECTOR_SHIFT;
227 if (sectors < bio_sectors(bio_orig)) {
228 bio = bio_split(bio_orig, sectors, GFP_NOIO, &bounce_bio_split);
229 bio_chain(bio, bio_orig);
230 submit_bio_noacct(bio_orig);
231 bio_orig = bio;
232 }
233 bio = bounce_clone_bio(bio_orig);
234
235 /*
236 * Bvec table can't be updated by bio_for_each_segment_all(),
237 * so retrieve bvec from the table directly. This way is safe
238 * because the 'bio' is single-page bvec.
239 */
240 for (i = 0, to = bio->bi_io_vec; i < bio->bi_vcnt; to++, i++) {
241 struct page *bounce_page;
242
243 if (!PageHighMem(to->bv_page))
244 continue;
245
246 bounce_page = mempool_alloc(&page_pool, GFP_NOIO);
247 inc_zone_page_state(bounce_page, NR_BOUNCE);
248
249 if (rw == WRITE) {
250 flush_dcache_page(to->bv_page);
251 memcpy_from_bvec(page_address(bounce_page), to);
252 }
253 to->bv_page = bounce_page;
254 }
255
256 trace_block_bio_bounce(bio_orig);
257
258 bio->bi_flags |= (1 << BIO_BOUNCED);
259
260 if (rw == READ)
261 bio->bi_end_io = bounce_end_io_read;
262 else
263 bio->bi_end_io = bounce_end_io_write;
264
265 bio->bi_private = bio_orig;
266 return bio;
267 }
268