xref: /openbmc/linux/block/bounce.c (revision 3c892a09)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2719c555fSJens Axboe /* bounce buffer handling for block devices
3719c555fSJens Axboe  *
4719c555fSJens Axboe  * - Split from highmem.c
5719c555fSJens Axboe  */
6719c555fSJens Axboe 
7b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8b1de0d13SMitchel Humpherys 
9719c555fSJens Axboe #include <linux/mm.h>
10719c555fSJens Axboe #include <linux/export.h>
11719c555fSJens Axboe #include <linux/swap.h>
12719c555fSJens Axboe #include <linux/gfp.h>
13719c555fSJens Axboe #include <linux/bio.h>
14719c555fSJens Axboe #include <linux/pagemap.h>
15719c555fSJens Axboe #include <linux/mempool.h>
16719c555fSJens Axboe #include <linux/blkdev.h>
1766114cadSTejun Heo #include <linux/backing-dev.h>
18719c555fSJens Axboe #include <linux/init.h>
19719c555fSJens Axboe #include <linux/hash.h>
20719c555fSJens Axboe #include <linux/highmem.h>
21719c555fSJens Axboe #include <linux/bootmem.h>
22b1de0d13SMitchel Humpherys #include <linux/printk.h>
23719c555fSJens Axboe #include <asm/tlbflush.h>
24719c555fSJens Axboe 
25719c555fSJens Axboe #include <trace/events/block.h>
263bce016aSChristoph Hellwig #include "blk.h"
27719c555fSJens Axboe 
28719c555fSJens Axboe #define POOL_SIZE	64
29719c555fSJens Axboe #define ISA_POOL_SIZE	16
30719c555fSJens Axboe 
31e0fc443aSBart Van Assche static struct bio_set *bounce_bio_set, *bounce_bio_split;
32719c555fSJens Axboe static mempool_t *page_pool, *isa_page_pool;
33719c555fSJens Axboe 
34719c555fSJens Axboe #if defined(CONFIG_HIGHMEM) || defined(CONFIG_NEED_BOUNCE_POOL)
35719c555fSJens Axboe static __init int init_emergency_pool(void)
36719c555fSJens Axboe {
37719c555fSJens Axboe #if defined(CONFIG_HIGHMEM) && !defined(CONFIG_MEMORY_HOTPLUG)
38719c555fSJens Axboe 	if (max_pfn <= max_low_pfn)
39719c555fSJens Axboe 		return 0;
40719c555fSJens Axboe #endif
41719c555fSJens Axboe 
42719c555fSJens Axboe 	page_pool = mempool_create_page_pool(POOL_SIZE, 0);
43719c555fSJens Axboe 	BUG_ON(!page_pool);
44b1de0d13SMitchel Humpherys 	pr_info("pool size: %d pages\n", POOL_SIZE);
45719c555fSJens Axboe 
46a8821f3fSNeilBrown 	bounce_bio_set = bioset_create(BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
47a8821f3fSNeilBrown 	BUG_ON(!bounce_bio_set);
48a8821f3fSNeilBrown 	if (bioset_integrity_create(bounce_bio_set, BIO_POOL_SIZE))
49a8821f3fSNeilBrown 		BUG_ON(1);
50a8821f3fSNeilBrown 
51a8821f3fSNeilBrown 	bounce_bio_split = bioset_create(BIO_POOL_SIZE, 0, 0);
52a8821f3fSNeilBrown 	BUG_ON(!bounce_bio_split);
53a8821f3fSNeilBrown 
54719c555fSJens Axboe 	return 0;
55719c555fSJens Axboe }
56719c555fSJens Axboe 
57719c555fSJens Axboe __initcall(init_emergency_pool);
58719c555fSJens Axboe #endif
59719c555fSJens Axboe 
60719c555fSJens Axboe #ifdef CONFIG_HIGHMEM
61719c555fSJens Axboe /*
62719c555fSJens Axboe  * highmem version, map in to vec
63719c555fSJens Axboe  */
64719c555fSJens Axboe static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
65719c555fSJens Axboe {
66719c555fSJens Axboe 	unsigned long flags;
67719c555fSJens Axboe 	unsigned char *vto;
68719c555fSJens Axboe 
69719c555fSJens Axboe 	local_irq_save(flags);
70719c555fSJens Axboe 	vto = kmap_atomic(to->bv_page);
71719c555fSJens Axboe 	memcpy(vto + to->bv_offset, vfrom, to->bv_len);
72719c555fSJens Axboe 	kunmap_atomic(vto);
73719c555fSJens Axboe 	local_irq_restore(flags);
74719c555fSJens Axboe }
75719c555fSJens Axboe 
76719c555fSJens Axboe #else /* CONFIG_HIGHMEM */
77719c555fSJens Axboe 
78719c555fSJens Axboe #define bounce_copy_vec(to, vfrom)	\
79719c555fSJens Axboe 	memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
80719c555fSJens Axboe 
81719c555fSJens Axboe #endif /* CONFIG_HIGHMEM */
82719c555fSJens Axboe 
83719c555fSJens Axboe /*
84719c555fSJens Axboe  * allocate pages in the DMA region for the ISA pool
85719c555fSJens Axboe  */
86719c555fSJens Axboe static void *mempool_alloc_pages_isa(gfp_t gfp_mask, void *data)
87719c555fSJens Axboe {
88719c555fSJens Axboe 	return mempool_alloc_pages(gfp_mask | GFP_DMA, data);
89719c555fSJens Axboe }
90719c555fSJens Axboe 
91719c555fSJens Axboe /*
92719c555fSJens Axboe  * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
93719c555fSJens Axboe  * as the max address, so check if the pool has already been created.
94719c555fSJens Axboe  */
95719c555fSJens Axboe int init_emergency_isa_pool(void)
96719c555fSJens Axboe {
97719c555fSJens Axboe 	if (isa_page_pool)
98719c555fSJens Axboe 		return 0;
99719c555fSJens Axboe 
100719c555fSJens Axboe 	isa_page_pool = mempool_create(ISA_POOL_SIZE, mempool_alloc_pages_isa,
101719c555fSJens Axboe 				       mempool_free_pages, (void *) 0);
102719c555fSJens Axboe 	BUG_ON(!isa_page_pool);
103719c555fSJens Axboe 
104b1de0d13SMitchel Humpherys 	pr_info("isa pool size: %d pages\n", ISA_POOL_SIZE);
105719c555fSJens Axboe 	return 0;
106719c555fSJens Axboe }
107719c555fSJens Axboe 
108719c555fSJens Axboe /*
109719c555fSJens Axboe  * Simple bounce buffer support for highmem pages. Depending on the
110719c555fSJens Axboe  * queue gfp mask set, *to may or may not be a highmem page. kmap it
111719c555fSJens Axboe  * always, it will do the Right Thing
112719c555fSJens Axboe  */
113719c555fSJens Axboe static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
114719c555fSJens Axboe {
115719c555fSJens Axboe 	unsigned char *vfrom;
1163c892a09SMing Lei 	struct bio_vec tovec, fromvec;
117719c555fSJens Axboe 	struct bvec_iter iter;
1183c892a09SMing Lei 	/*
1193c892a09SMing Lei 	 * The bio of @from is created by bounce, so we can iterate
1203c892a09SMing Lei 	 * its bvec from start to end, but the @from->bi_iter can't be
1213c892a09SMing Lei 	 * trusted because it might be changed by splitting.
1223c892a09SMing Lei 	 */
1233c892a09SMing Lei 	struct bvec_iter from_iter = BVEC_ITER_ALL_INIT;
124719c555fSJens Axboe 
125719c555fSJens Axboe 	bio_for_each_segment(tovec, to, iter) {
1263c892a09SMing Lei 		fromvec = bio_iter_iovec(from, from_iter);
1273c892a09SMing Lei 		if (tovec.bv_page != fromvec.bv_page) {
128719c555fSJens Axboe 			/*
129719c555fSJens Axboe 			 * fromvec->bv_offset and fromvec->bv_len might have
130719c555fSJens Axboe 			 * been modified by the block layer, so use the original
131719c555fSJens Axboe 			 * copy, bounce_copy_vec already uses tovec->bv_len
132719c555fSJens Axboe 			 */
1333c892a09SMing Lei 			vfrom = page_address(fromvec.bv_page) +
134719c555fSJens Axboe 				tovec.bv_offset;
135719c555fSJens Axboe 
136719c555fSJens Axboe 			bounce_copy_vec(&tovec, vfrom);
137719c555fSJens Axboe 			flush_dcache_page(tovec.bv_page);
138719c555fSJens Axboe 		}
1393c892a09SMing Lei 		bio_advance_iter(from, &from_iter, tovec.bv_len);
140719c555fSJens Axboe 	}
141719c555fSJens Axboe }
142719c555fSJens Axboe 
1434246a0b6SChristoph Hellwig static void bounce_end_io(struct bio *bio, mempool_t *pool)
144719c555fSJens Axboe {
145719c555fSJens Axboe 	struct bio *bio_orig = bio->bi_private;
1467891f05cSMing Lei 	struct bio_vec *bvec, orig_vec;
147719c555fSJens Axboe 	int i;
1487891f05cSMing Lei 	struct bvec_iter orig_iter = bio_orig->bi_iter;
149719c555fSJens Axboe 
150719c555fSJens Axboe 	/*
151719c555fSJens Axboe 	 * free up bounce indirect pages used
152719c555fSJens Axboe 	 */
153719c555fSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
1547891f05cSMing Lei 		orig_vec = bio_iter_iovec(bio_orig, orig_iter);
1557891f05cSMing Lei 		if (bvec->bv_page != orig_vec.bv_page) {
156719c555fSJens Axboe 			dec_zone_page_state(bvec->bv_page, NR_BOUNCE);
157719c555fSJens Axboe 			mempool_free(bvec->bv_page, pool);
158719c555fSJens Axboe 		}
1597891f05cSMing Lei 		bio_advance_iter(bio_orig, &orig_iter, orig_vec.bv_len);
1607891f05cSMing Lei 	}
161719c555fSJens Axboe 
1624e4cbee9SChristoph Hellwig 	bio_orig->bi_status = bio->bi_status;
1634246a0b6SChristoph Hellwig 	bio_endio(bio_orig);
164719c555fSJens Axboe 	bio_put(bio);
165719c555fSJens Axboe }
166719c555fSJens Axboe 
1674246a0b6SChristoph Hellwig static void bounce_end_io_write(struct bio *bio)
168719c555fSJens Axboe {
1694246a0b6SChristoph Hellwig 	bounce_end_io(bio, page_pool);
170719c555fSJens Axboe }
171719c555fSJens Axboe 
1724246a0b6SChristoph Hellwig static void bounce_end_io_write_isa(struct bio *bio)
173719c555fSJens Axboe {
174719c555fSJens Axboe 
1754246a0b6SChristoph Hellwig 	bounce_end_io(bio, isa_page_pool);
176719c555fSJens Axboe }
177719c555fSJens Axboe 
1784246a0b6SChristoph Hellwig static void __bounce_end_io_read(struct bio *bio, mempool_t *pool)
179719c555fSJens Axboe {
180719c555fSJens Axboe 	struct bio *bio_orig = bio->bi_private;
181719c555fSJens Axboe 
1824e4cbee9SChristoph Hellwig 	if (!bio->bi_status)
183719c555fSJens Axboe 		copy_to_high_bio_irq(bio_orig, bio);
184719c555fSJens Axboe 
1854246a0b6SChristoph Hellwig 	bounce_end_io(bio, pool);
186719c555fSJens Axboe }
187719c555fSJens Axboe 
1884246a0b6SChristoph Hellwig static void bounce_end_io_read(struct bio *bio)
189719c555fSJens Axboe {
1904246a0b6SChristoph Hellwig 	__bounce_end_io_read(bio, page_pool);
191719c555fSJens Axboe }
192719c555fSJens Axboe 
1934246a0b6SChristoph Hellwig static void bounce_end_io_read_isa(struct bio *bio)
194719c555fSJens Axboe {
1954246a0b6SChristoph Hellwig 	__bounce_end_io_read(bio, isa_page_pool);
196719c555fSJens Axboe }
197719c555fSJens Axboe 
198719c555fSJens Axboe static void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig,
199a3ad0a9dSJan Kara 			       mempool_t *pool)
200719c555fSJens Axboe {
201719c555fSJens Axboe 	struct bio *bio;
202719c555fSJens Axboe 	int rw = bio_data_dir(*bio_orig);
203719c555fSJens Axboe 	struct bio_vec *to, from;
204719c555fSJens Axboe 	struct bvec_iter iter;
205a8821f3fSNeilBrown 	unsigned i = 0;
206a8821f3fSNeilBrown 	bool bounce = false;
207a8821f3fSNeilBrown 	int sectors = 0;
208719c555fSJens Axboe 
209a8821f3fSNeilBrown 	bio_for_each_segment(from, *bio_orig, iter) {
210a8821f3fSNeilBrown 		if (i++ < BIO_MAX_PAGES)
211a8821f3fSNeilBrown 			sectors += from.bv_len >> 9;
2121c4bc3abSChristoph Hellwig 		if (page_to_pfn(from.bv_page) > q->limits.bounce_pfn)
213a8821f3fSNeilBrown 			bounce = true;
214a8821f3fSNeilBrown 	}
215a8821f3fSNeilBrown 	if (!bounce)
216719c555fSJens Axboe 		return;
217a8821f3fSNeilBrown 
218a8821f3fSNeilBrown 	if (sectors < bio_sectors(*bio_orig)) {
219a8821f3fSNeilBrown 		bio = bio_split(*bio_orig, sectors, GFP_NOIO, bounce_bio_split);
220a8821f3fSNeilBrown 		bio_chain(bio, *bio_orig);
221a8821f3fSNeilBrown 		generic_make_request(*bio_orig);
222a8821f3fSNeilBrown 		*bio_orig = bio;
223a8821f3fSNeilBrown 	}
224a8821f3fSNeilBrown 	bio = bio_clone_bioset(*bio_orig, GFP_NOIO, bounce_bio_set);
225719c555fSJens Axboe 
226719c555fSJens Axboe 	bio_for_each_segment_all(to, bio, i) {
227719c555fSJens Axboe 		struct page *page = to->bv_page;
228719c555fSJens Axboe 
2291c4bc3abSChristoph Hellwig 		if (page_to_pfn(page) <= q->limits.bounce_pfn)
230719c555fSJens Axboe 			continue;
231719c555fSJens Axboe 
232719c555fSJens Axboe 		to->bv_page = mempool_alloc(pool, q->bounce_gfp);
233393a3397SWang YanQing 		inc_zone_page_state(to->bv_page, NR_BOUNCE);
234719c555fSJens Axboe 
235719c555fSJens Axboe 		if (rw == WRITE) {
236719c555fSJens Axboe 			char *vto, *vfrom;
237719c555fSJens Axboe 
238719c555fSJens Axboe 			flush_dcache_page(page);
239719c555fSJens Axboe 
240719c555fSJens Axboe 			vto = page_address(to->bv_page) + to->bv_offset;
241719c555fSJens Axboe 			vfrom = kmap_atomic(page) + to->bv_offset;
242719c555fSJens Axboe 			memcpy(vto, vfrom, to->bv_len);
243719c555fSJens Axboe 			kunmap_atomic(vfrom);
244719c555fSJens Axboe 		}
245719c555fSJens Axboe 	}
246719c555fSJens Axboe 
247719c555fSJens Axboe 	trace_block_bio_bounce(q, *bio_orig);
248719c555fSJens Axboe 
249719c555fSJens Axboe 	bio->bi_flags |= (1 << BIO_BOUNCED);
250719c555fSJens Axboe 
251719c555fSJens Axboe 	if (pool == page_pool) {
252719c555fSJens Axboe 		bio->bi_end_io = bounce_end_io_write;
253719c555fSJens Axboe 		if (rw == READ)
254719c555fSJens Axboe 			bio->bi_end_io = bounce_end_io_read;
255719c555fSJens Axboe 	} else {
256719c555fSJens Axboe 		bio->bi_end_io = bounce_end_io_write_isa;
257719c555fSJens Axboe 		if (rw == READ)
258719c555fSJens Axboe 			bio->bi_end_io = bounce_end_io_read_isa;
259719c555fSJens Axboe 	}
260719c555fSJens Axboe 
261719c555fSJens Axboe 	bio->bi_private = *bio_orig;
262719c555fSJens Axboe 	*bio_orig = bio;
263719c555fSJens Axboe }
264719c555fSJens Axboe 
265719c555fSJens Axboe void blk_queue_bounce(struct request_queue *q, struct bio **bio_orig)
266719c555fSJens Axboe {
267719c555fSJens Axboe 	mempool_t *pool;
268719c555fSJens Axboe 
269719c555fSJens Axboe 	/*
270719c555fSJens Axboe 	 * Data-less bio, nothing to bounce
271719c555fSJens Axboe 	 */
272719c555fSJens Axboe 	if (!bio_has_data(*bio_orig))
273719c555fSJens Axboe 		return;
274719c555fSJens Axboe 
275719c555fSJens Axboe 	/*
276719c555fSJens Axboe 	 * for non-isa bounce case, just check if the bounce pfn is equal
277719c555fSJens Axboe 	 * to or bigger than the highest pfn in the system -- in that case,
278719c555fSJens Axboe 	 * don't waste time iterating over bio segments
279719c555fSJens Axboe 	 */
280719c555fSJens Axboe 	if (!(q->bounce_gfp & GFP_DMA)) {
2811c4bc3abSChristoph Hellwig 		if (q->limits.bounce_pfn >= blk_max_pfn)
282719c555fSJens Axboe 			return;
283719c555fSJens Axboe 		pool = page_pool;
284719c555fSJens Axboe 	} else {
285719c555fSJens Axboe 		BUG_ON(!isa_page_pool);
286719c555fSJens Axboe 		pool = isa_page_pool;
287719c555fSJens Axboe 	}
288719c555fSJens Axboe 
289719c555fSJens Axboe 	/*
290719c555fSJens Axboe 	 * slow path
291719c555fSJens Axboe 	 */
292a3ad0a9dSJan Kara 	__blk_queue_bounce(q, bio_orig, pool);
293719c555fSJens Axboe }
294