xref: /linux/block/bounce.c (revision 74ce1896c6c65b2f8cccbf59162d542988835835)
1 /* bounce buffer handling for block devices
2  *
3  * - Split from highmem.c
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/mm.h>
9 #include <linux/export.h>
10 #include <linux/swap.h>
11 #include <linux/gfp.h>
12 #include <linux/bio.h>
13 #include <linux/pagemap.h>
14 #include <linux/mempool.h>
15 #include <linux/blkdev.h>
16 #include <linux/backing-dev.h>
17 #include <linux/init.h>
18 #include <linux/hash.h>
19 #include <linux/highmem.h>
20 #include <linux/bootmem.h>
21 #include <linux/printk.h>
22 #include <asm/tlbflush.h>
23 
24 #include <trace/events/block.h>
25 #include "blk.h"
26 
27 #define POOL_SIZE	64
28 #define ISA_POOL_SIZE	16
29 
30 static struct bio_set *bounce_bio_set, *bounce_bio_split;
31 static mempool_t *page_pool, *isa_page_pool;
32 
33 #if defined(CONFIG_HIGHMEM) || defined(CONFIG_NEED_BOUNCE_POOL)
34 static __init int init_emergency_pool(void)
35 {
36 #if defined(CONFIG_HIGHMEM) && !defined(CONFIG_MEMORY_HOTPLUG)
37 	if (max_pfn <= max_low_pfn)
38 		return 0;
39 #endif
40 
41 	page_pool = mempool_create_page_pool(POOL_SIZE, 0);
42 	BUG_ON(!page_pool);
43 	pr_info("pool size: %d pages\n", POOL_SIZE);
44 
45 	bounce_bio_set = bioset_create(BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
46 	BUG_ON(!bounce_bio_set);
47 	if (bioset_integrity_create(bounce_bio_set, BIO_POOL_SIZE))
48 		BUG_ON(1);
49 
50 	bounce_bio_split = bioset_create(BIO_POOL_SIZE, 0, 0);
51 	BUG_ON(!bounce_bio_split);
52 
53 	return 0;
54 }
55 
56 __initcall(init_emergency_pool);
57 #endif
58 
59 #ifdef CONFIG_HIGHMEM
60 /*
61  * highmem version, map in to vec
62  */
63 static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
64 {
65 	unsigned long flags;
66 	unsigned char *vto;
67 
68 	local_irq_save(flags);
69 	vto = kmap_atomic(to->bv_page);
70 	memcpy(vto + to->bv_offset, vfrom, to->bv_len);
71 	kunmap_atomic(vto);
72 	local_irq_restore(flags);
73 }
74 
75 #else /* CONFIG_HIGHMEM */
76 
77 #define bounce_copy_vec(to, vfrom)	\
78 	memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
79 
80 #endif /* CONFIG_HIGHMEM */
81 
82 /*
83  * allocate pages in the DMA region for the ISA pool
84  */
85 static void *mempool_alloc_pages_isa(gfp_t gfp_mask, void *data)
86 {
87 	return mempool_alloc_pages(gfp_mask | GFP_DMA, data);
88 }
89 
90 /*
91  * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
92  * as the max address, so check if the pool has already been created.
93  */
94 int init_emergency_isa_pool(void)
95 {
96 	if (isa_page_pool)
97 		return 0;
98 
99 	isa_page_pool = mempool_create(ISA_POOL_SIZE, mempool_alloc_pages_isa,
100 				       mempool_free_pages, (void *) 0);
101 	BUG_ON(!isa_page_pool);
102 
103 	pr_info("isa pool size: %d pages\n", ISA_POOL_SIZE);
104 	return 0;
105 }
106 
107 /*
108  * Simple bounce buffer support for highmem pages. Depending on the
109  * queue gfp mask set, *to may or may not be a highmem page. kmap it
110  * always, it will do the Right Thing
111  */
112 static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
113 {
114 	unsigned char *vfrom;
115 	struct bio_vec tovec, *fromvec = from->bi_io_vec;
116 	struct bvec_iter iter;
117 
118 	bio_for_each_segment(tovec, to, iter) {
119 		if (tovec.bv_page != fromvec->bv_page) {
120 			/*
121 			 * fromvec->bv_offset and fromvec->bv_len might have
122 			 * been modified by the block layer, so use the original
123 			 * copy, bounce_copy_vec already uses tovec->bv_len
124 			 */
125 			vfrom = page_address(fromvec->bv_page) +
126 				tovec.bv_offset;
127 
128 			bounce_copy_vec(&tovec, vfrom);
129 			flush_dcache_page(tovec.bv_page);
130 		}
131 
132 		fromvec++;
133 	}
134 }
135 
136 static void bounce_end_io(struct bio *bio, mempool_t *pool)
137 {
138 	struct bio *bio_orig = bio->bi_private;
139 	struct bio_vec *bvec, *org_vec;
140 	int i;
141 	int start = bio_orig->bi_iter.bi_idx;
142 
143 	/*
144 	 * free up bounce indirect pages used
145 	 */
146 	bio_for_each_segment_all(bvec, bio, i) {
147 		org_vec = bio_orig->bi_io_vec + i + start;
148 
149 		if (bvec->bv_page == org_vec->bv_page)
150 			continue;
151 
152 		dec_zone_page_state(bvec->bv_page, NR_BOUNCE);
153 		mempool_free(bvec->bv_page, pool);
154 	}
155 
156 	bio_orig->bi_status = bio->bi_status;
157 	bio_endio(bio_orig);
158 	bio_put(bio);
159 }
160 
161 static void bounce_end_io_write(struct bio *bio)
162 {
163 	bounce_end_io(bio, page_pool);
164 }
165 
166 static void bounce_end_io_write_isa(struct bio *bio)
167 {
168 
169 	bounce_end_io(bio, isa_page_pool);
170 }
171 
172 static void __bounce_end_io_read(struct bio *bio, mempool_t *pool)
173 {
174 	struct bio *bio_orig = bio->bi_private;
175 
176 	if (!bio->bi_status)
177 		copy_to_high_bio_irq(bio_orig, bio);
178 
179 	bounce_end_io(bio, pool);
180 }
181 
182 static void bounce_end_io_read(struct bio *bio)
183 {
184 	__bounce_end_io_read(bio, page_pool);
185 }
186 
187 static void bounce_end_io_read_isa(struct bio *bio)
188 {
189 	__bounce_end_io_read(bio, isa_page_pool);
190 }
191 
192 static void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig,
193 			       mempool_t *pool)
194 {
195 	struct bio *bio;
196 	int rw = bio_data_dir(*bio_orig);
197 	struct bio_vec *to, from;
198 	struct bvec_iter iter;
199 	unsigned i = 0;
200 	bool bounce = false;
201 	int sectors = 0;
202 
203 	bio_for_each_segment(from, *bio_orig, iter) {
204 		if (i++ < BIO_MAX_PAGES)
205 			sectors += from.bv_len >> 9;
206 		if (page_to_pfn(from.bv_page) > q->limits.bounce_pfn)
207 			bounce = true;
208 	}
209 	if (!bounce)
210 		return;
211 
212 	if (sectors < bio_sectors(*bio_orig)) {
213 		bio = bio_split(*bio_orig, sectors, GFP_NOIO, bounce_bio_split);
214 		bio_chain(bio, *bio_orig);
215 		generic_make_request(*bio_orig);
216 		*bio_orig = bio;
217 	}
218 	bio = bio_clone_bioset(*bio_orig, GFP_NOIO, bounce_bio_set);
219 
220 	bio_for_each_segment_all(to, bio, i) {
221 		struct page *page = to->bv_page;
222 
223 		if (page_to_pfn(page) <= q->limits.bounce_pfn)
224 			continue;
225 
226 		to->bv_page = mempool_alloc(pool, q->bounce_gfp);
227 		inc_zone_page_state(to->bv_page, NR_BOUNCE);
228 
229 		if (rw == WRITE) {
230 			char *vto, *vfrom;
231 
232 			flush_dcache_page(page);
233 
234 			vto = page_address(to->bv_page) + to->bv_offset;
235 			vfrom = kmap_atomic(page) + to->bv_offset;
236 			memcpy(vto, vfrom, to->bv_len);
237 			kunmap_atomic(vfrom);
238 		}
239 	}
240 
241 	trace_block_bio_bounce(q, *bio_orig);
242 
243 	bio->bi_flags |= (1 << BIO_BOUNCED);
244 
245 	if (pool == page_pool) {
246 		bio->bi_end_io = bounce_end_io_write;
247 		if (rw == READ)
248 			bio->bi_end_io = bounce_end_io_read;
249 	} else {
250 		bio->bi_end_io = bounce_end_io_write_isa;
251 		if (rw == READ)
252 			bio->bi_end_io = bounce_end_io_read_isa;
253 	}
254 
255 	bio->bi_private = *bio_orig;
256 	*bio_orig = bio;
257 }
258 
259 void blk_queue_bounce(struct request_queue *q, struct bio **bio_orig)
260 {
261 	mempool_t *pool;
262 
263 	/*
264 	 * Data-less bio, nothing to bounce
265 	 */
266 	if (!bio_has_data(*bio_orig))
267 		return;
268 
269 	/*
270 	 * for non-isa bounce case, just check if the bounce pfn is equal
271 	 * to or bigger than the highest pfn in the system -- in that case,
272 	 * don't waste time iterating over bio segments
273 	 */
274 	if (!(q->bounce_gfp & GFP_DMA)) {
275 		if (q->limits.bounce_pfn >= blk_max_pfn)
276 			return;
277 		pool = page_pool;
278 	} else {
279 		BUG_ON(!isa_page_pool);
280 		pool = isa_page_pool;
281 	}
282 
283 	/*
284 	 * slow path
285 	 */
286 	__blk_queue_bounce(q, bio_orig, pool);
287 }
288