1
2
3
4
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.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/memblock.h>
22#include <linux/printk.h>
23#include <asm/tlbflush.h>
24
25#include <trace/events/block.h>
26#include "blk.h"
27
28#define POOL_SIZE 64
29#define ISA_POOL_SIZE 16
30
31static struct bio_set bounce_bio_set, bounce_bio_split;
32static mempool_t page_pool, isa_page_pool;
33
34#if defined(CONFIG_HIGHMEM)
35static __init int init_emergency_pool(void)
36{
37 int ret;
38#if defined(CONFIG_HIGHMEM) && !defined(CONFIG_MEMORY_HOTPLUG)
39 if (max_pfn <= max_low_pfn)
40 return 0;
41#endif
42
43 ret = mempool_init_page_pool(&page_pool, POOL_SIZE, 0);
44 BUG_ON(ret);
45 pr_info("pool size: %d pages\n", POOL_SIZE);
46
47 ret = bioset_init(&bounce_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
48 BUG_ON(ret);
49 if (bioset_integrity_create(&bounce_bio_set, BIO_POOL_SIZE))
50 BUG_ON(1);
51
52 ret = bioset_init(&bounce_bio_split, BIO_POOL_SIZE, 0, 0);
53 BUG_ON(ret);
54
55 return 0;
56}
57
58__initcall(init_emergency_pool);
59#endif
60
61#ifdef CONFIG_HIGHMEM
62
63
64
65static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
66{
67 unsigned char *vto;
68
69 vto = kmap_atomic(to->bv_page);
70 memcpy(vto + to->bv_offset, vfrom, to->bv_len);
71 kunmap_atomic(vto);
72}
73
74#else
75
76#define bounce_copy_vec(to, vfrom) \
77 memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
78
79#endif
80
81
82
83
84static void *mempool_alloc_pages_isa(gfp_t gfp_mask, void *data)
85{
86 return mempool_alloc_pages(gfp_mask | GFP_DMA, data);
87}
88
89
90
91
92
93int init_emergency_isa_pool(void)
94{
95 int ret;
96
97 if (mempool_initialized(&isa_page_pool))
98 return 0;
99
100 ret = mempool_init(&isa_page_pool, ISA_POOL_SIZE, mempool_alloc_pages_isa,
101 mempool_free_pages, (void *) 0);
102 BUG_ON(ret);
103
104 pr_info("isa pool size: %d pages\n", ISA_POOL_SIZE);
105 return 0;
106}
107
108
109
110
111
112
113static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
114{
115 unsigned char *vfrom;
116 struct bio_vec tovec, fromvec;
117 struct bvec_iter iter;
118
119
120
121
122
123 struct bvec_iter from_iter = BVEC_ITER_ALL_INIT;
124
125 bio_for_each_segment(tovec, to, iter) {
126 fromvec = bio_iter_iovec(from, from_iter);
127 if (tovec.bv_page != fromvec.bv_page) {
128
129
130
131
132
133 vfrom = page_address(fromvec.bv_page) +
134 tovec.bv_offset;
135
136 bounce_copy_vec(&tovec, vfrom);
137 flush_dcache_page(tovec.bv_page);
138 }
139 bio_advance_iter(from, &from_iter, tovec.bv_len);
140 }
141}
142
143static void bounce_end_io(struct bio *bio, mempool_t *pool)
144{
145 struct bio *bio_orig = bio->bi_private;
146 struct bio_vec *bvec, orig_vec;
147 int i;
148 struct bvec_iter orig_iter = bio_orig->bi_iter;
149
150
151
152
153 bio_for_each_segment_all(bvec, bio, i) {
154 orig_vec = bio_iter_iovec(bio_orig, orig_iter);
155 if (bvec->bv_page != orig_vec.bv_page) {
156 dec_zone_page_state(bvec->bv_page, NR_BOUNCE);
157 mempool_free(bvec->bv_page, pool);
158 }
159 bio_advance_iter(bio_orig, &orig_iter, orig_vec.bv_len);
160 }
161
162 bio_orig->bi_status = bio->bi_status;
163 bio_endio(bio_orig);
164 bio_put(bio);
165}
166
167static void bounce_end_io_write(struct bio *bio)
168{
169 bounce_end_io(bio, &page_pool);
170}
171
172static void bounce_end_io_write_isa(struct bio *bio)
173{
174
175 bounce_end_io(bio, &isa_page_pool);
176}
177
178static void __bounce_end_io_read(struct bio *bio, mempool_t *pool)
179{
180 struct bio *bio_orig = bio->bi_private;
181
182 if (!bio->bi_status)
183 copy_to_high_bio_irq(bio_orig, bio);
184
185 bounce_end_io(bio, pool);
186}
187
188static void bounce_end_io_read(struct bio *bio)
189{
190 __bounce_end_io_read(bio, &page_pool);
191}
192
193static void bounce_end_io_read_isa(struct bio *bio)
194{
195 __bounce_end_io_read(bio, &isa_page_pool);
196}
197
198static struct bio *bounce_clone_bio(struct bio *bio_src, gfp_t gfp_mask,
199 struct bio_set *bs)
200{
201 struct bvec_iter iter;
202 struct bio_vec bv;
203 struct bio *bio;
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227 bio = bio_alloc_bioset(gfp_mask, bio_segments(bio_src), bs);
228 if (!bio)
229 return NULL;
230 bio->bi_disk = bio_src->bi_disk;
231 bio->bi_opf = bio_src->bi_opf;
232 bio->bi_ioprio = bio_src->bi_ioprio;
233 bio->bi_write_hint = bio_src->bi_write_hint;
234 bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector;
235 bio->bi_iter.bi_size = bio_src->bi_iter.bi_size;
236
237 switch (bio_op(bio)) {
238 case REQ_OP_DISCARD:
239 case REQ_OP_SECURE_ERASE:
240 case REQ_OP_WRITE_ZEROES:
241 break;
242 case REQ_OP_WRITE_SAME:
243 bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0];
244 break;
245 default:
246 bio_for_each_segment(bv, bio_src, iter)
247 bio->bi_io_vec[bio->bi_vcnt++] = bv;
248 break;
249 }
250
251 if (bio_integrity(bio_src)) {
252 int ret;
253
254 ret = bio_integrity_clone(bio, bio_src, gfp_mask);
255 if (ret < 0) {
256 bio_put(bio);
257 return NULL;
258 }
259 }
260
261 bio_clone_blkg_association(bio, bio_src);
262 blkcg_bio_issue_init(bio);
263
264 return bio;
265}
266
267static void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig,
268 mempool_t *pool)
269{
270 struct bio *bio;
271 int rw = bio_data_dir(*bio_orig);
272 struct bio_vec *to, from;
273 struct bvec_iter iter;
274 unsigned i = 0;
275 bool bounce = false;
276 int sectors = 0;
277 bool passthrough = bio_is_passthrough(*bio_orig);
278
279 bio_for_each_segment(from, *bio_orig, iter) {
280 if (i++ < BIO_MAX_PAGES)
281 sectors += from.bv_len >> 9;
282 if (page_to_pfn(from.bv_page) > q->limits.bounce_pfn)
283 bounce = true;
284 }
285 if (!bounce)
286 return;
287
288 if (!passthrough && sectors < bio_sectors(*bio_orig)) {
289 bio = bio_split(*bio_orig, sectors, GFP_NOIO, &bounce_bio_split);
290 bio_chain(bio, *bio_orig);
291 generic_make_request(*bio_orig);
292 *bio_orig = bio;
293 }
294 bio = bounce_clone_bio(*bio_orig, GFP_NOIO, passthrough ? NULL :
295 &bounce_bio_set);
296
297 bio_for_each_segment_all(to, bio, i) {
298 struct page *page = to->bv_page;
299
300 if (page_to_pfn(page) <= q->limits.bounce_pfn)
301 continue;
302
303 to->bv_page = mempool_alloc(pool, q->bounce_gfp);
304 inc_zone_page_state(to->bv_page, NR_BOUNCE);
305
306 if (rw == WRITE) {
307 char *vto, *vfrom;
308
309 flush_dcache_page(page);
310
311 vto = page_address(to->bv_page) + to->bv_offset;
312 vfrom = kmap_atomic(page) + to->bv_offset;
313 memcpy(vto, vfrom, to->bv_len);
314 kunmap_atomic(vfrom);
315 }
316 }
317
318 trace_block_bio_bounce(q, *bio_orig);
319
320 bio->bi_flags |= (1 << BIO_BOUNCED);
321
322 if (pool == &page_pool) {
323 bio->bi_end_io = bounce_end_io_write;
324 if (rw == READ)
325 bio->bi_end_io = bounce_end_io_read;
326 } else {
327 bio->bi_end_io = bounce_end_io_write_isa;
328 if (rw == READ)
329 bio->bi_end_io = bounce_end_io_read_isa;
330 }
331
332 bio->bi_private = *bio_orig;
333 *bio_orig = bio;
334}
335
336void blk_queue_bounce(struct request_queue *q, struct bio **bio_orig)
337{
338 mempool_t *pool;
339
340
341
342
343 if (!bio_has_data(*bio_orig))
344 return;
345
346
347
348
349
350
351 if (!(q->bounce_gfp & GFP_DMA)) {
352 if (q->limits.bounce_pfn >= blk_max_pfn)
353 return;
354 pool = &page_pool;
355 } else {
356 BUG_ON(!mempool_initialized(&isa_page_pool));
357 pool = &isa_page_pool;
358 }
359
360
361
362
363 __blk_queue_bounce(q, bio_orig, pool);
364}
365