xref: /openbmc/linux/block/bounce.c (revision b2441318)
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;
116719c555fSJens Axboe 	struct bio_vec tovec, *fromvec = from->bi_io_vec;
117719c555fSJens Axboe 	struct bvec_iter iter;
118719c555fSJens Axboe 
119719c555fSJens Axboe 	bio_for_each_segment(tovec, to, iter) {
120719c555fSJens Axboe 		if (tovec.bv_page != fromvec->bv_page) {
121719c555fSJens Axboe 			/*
122719c555fSJens Axboe 			 * fromvec->bv_offset and fromvec->bv_len might have
123719c555fSJens Axboe 			 * been modified by the block layer, so use the original
124719c555fSJens Axboe 			 * copy, bounce_copy_vec already uses tovec->bv_len
125719c555fSJens Axboe 			 */
126719c555fSJens Axboe 			vfrom = page_address(fromvec->bv_page) +
127719c555fSJens Axboe 				tovec.bv_offset;
128719c555fSJens Axboe 
129719c555fSJens Axboe 			bounce_copy_vec(&tovec, vfrom);
130719c555fSJens Axboe 			flush_dcache_page(tovec.bv_page);
131719c555fSJens Axboe 		}
132719c555fSJens Axboe 
133719c555fSJens Axboe 		fromvec++;
134719c555fSJens Axboe 	}
135719c555fSJens Axboe }
136719c555fSJens Axboe 
1374246a0b6SChristoph Hellwig static void bounce_end_io(struct bio *bio, mempool_t *pool)
138719c555fSJens Axboe {
139719c555fSJens Axboe 	struct bio *bio_orig = bio->bi_private;
140719c555fSJens Axboe 	struct bio_vec *bvec, *org_vec;
141719c555fSJens Axboe 	int i;
14299451879SMing Lei 	int start = bio_orig->bi_iter.bi_idx;
143719c555fSJens Axboe 
144719c555fSJens Axboe 	/*
145719c555fSJens Axboe 	 * free up bounce indirect pages used
146719c555fSJens Axboe 	 */
147719c555fSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
14899451879SMing Lei 		org_vec = bio_orig->bi_io_vec + i + start;
14999451879SMing Lei 
150719c555fSJens Axboe 		if (bvec->bv_page == org_vec->bv_page)
151719c555fSJens Axboe 			continue;
152719c555fSJens Axboe 
153719c555fSJens Axboe 		dec_zone_page_state(bvec->bv_page, NR_BOUNCE);
154719c555fSJens Axboe 		mempool_free(bvec->bv_page, pool);
155719c555fSJens Axboe 	}
156719c555fSJens Axboe 
1574e4cbee9SChristoph Hellwig 	bio_orig->bi_status = bio->bi_status;
1584246a0b6SChristoph Hellwig 	bio_endio(bio_orig);
159719c555fSJens Axboe 	bio_put(bio);
160719c555fSJens Axboe }
161719c555fSJens Axboe 
1624246a0b6SChristoph Hellwig static void bounce_end_io_write(struct bio *bio)
163719c555fSJens Axboe {
1644246a0b6SChristoph Hellwig 	bounce_end_io(bio, page_pool);
165719c555fSJens Axboe }
166719c555fSJens Axboe 
1674246a0b6SChristoph Hellwig static void bounce_end_io_write_isa(struct bio *bio)
168719c555fSJens Axboe {
169719c555fSJens Axboe 
1704246a0b6SChristoph Hellwig 	bounce_end_io(bio, isa_page_pool);
171719c555fSJens Axboe }
172719c555fSJens Axboe 
1734246a0b6SChristoph Hellwig static void __bounce_end_io_read(struct bio *bio, mempool_t *pool)
174719c555fSJens Axboe {
175719c555fSJens Axboe 	struct bio *bio_orig = bio->bi_private;
176719c555fSJens Axboe 
1774e4cbee9SChristoph Hellwig 	if (!bio->bi_status)
178719c555fSJens Axboe 		copy_to_high_bio_irq(bio_orig, bio);
179719c555fSJens Axboe 
1804246a0b6SChristoph Hellwig 	bounce_end_io(bio, pool);
181719c555fSJens Axboe }
182719c555fSJens Axboe 
1834246a0b6SChristoph Hellwig static void bounce_end_io_read(struct bio *bio)
184719c555fSJens Axboe {
1854246a0b6SChristoph Hellwig 	__bounce_end_io_read(bio, page_pool);
186719c555fSJens Axboe }
187719c555fSJens Axboe 
1884246a0b6SChristoph Hellwig static void bounce_end_io_read_isa(struct bio *bio)
189719c555fSJens Axboe {
1904246a0b6SChristoph Hellwig 	__bounce_end_io_read(bio, isa_page_pool);
191719c555fSJens Axboe }
192719c555fSJens Axboe 
193719c555fSJens Axboe static void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig,
194a3ad0a9dSJan Kara 			       mempool_t *pool)
195719c555fSJens Axboe {
196719c555fSJens Axboe 	struct bio *bio;
197719c555fSJens Axboe 	int rw = bio_data_dir(*bio_orig);
198719c555fSJens Axboe 	struct bio_vec *to, from;
199719c555fSJens Axboe 	struct bvec_iter iter;
200a8821f3fSNeilBrown 	unsigned i = 0;
201a8821f3fSNeilBrown 	bool bounce = false;
202a8821f3fSNeilBrown 	int sectors = 0;
203719c555fSJens Axboe 
204a8821f3fSNeilBrown 	bio_for_each_segment(from, *bio_orig, iter) {
205a8821f3fSNeilBrown 		if (i++ < BIO_MAX_PAGES)
206a8821f3fSNeilBrown 			sectors += from.bv_len >> 9;
2071c4bc3abSChristoph Hellwig 		if (page_to_pfn(from.bv_page) > q->limits.bounce_pfn)
208a8821f3fSNeilBrown 			bounce = true;
209a8821f3fSNeilBrown 	}
210a8821f3fSNeilBrown 	if (!bounce)
211719c555fSJens Axboe 		return;
212a8821f3fSNeilBrown 
213a8821f3fSNeilBrown 	if (sectors < bio_sectors(*bio_orig)) {
214a8821f3fSNeilBrown 		bio = bio_split(*bio_orig, sectors, GFP_NOIO, bounce_bio_split);
215a8821f3fSNeilBrown 		bio_chain(bio, *bio_orig);
216a8821f3fSNeilBrown 		generic_make_request(*bio_orig);
217a8821f3fSNeilBrown 		*bio_orig = bio;
218a8821f3fSNeilBrown 	}
219a8821f3fSNeilBrown 	bio = bio_clone_bioset(*bio_orig, GFP_NOIO, bounce_bio_set);
220719c555fSJens Axboe 
221719c555fSJens Axboe 	bio_for_each_segment_all(to, bio, i) {
222719c555fSJens Axboe 		struct page *page = to->bv_page;
223719c555fSJens Axboe 
2241c4bc3abSChristoph Hellwig 		if (page_to_pfn(page) <= q->limits.bounce_pfn)
225719c555fSJens Axboe 			continue;
226719c555fSJens Axboe 
227719c555fSJens Axboe 		to->bv_page = mempool_alloc(pool, q->bounce_gfp);
228393a3397SWang YanQing 		inc_zone_page_state(to->bv_page, NR_BOUNCE);
229719c555fSJens Axboe 
230719c555fSJens Axboe 		if (rw == WRITE) {
231719c555fSJens Axboe 			char *vto, *vfrom;
232719c555fSJens Axboe 
233719c555fSJens Axboe 			flush_dcache_page(page);
234719c555fSJens Axboe 
235719c555fSJens Axboe 			vto = page_address(to->bv_page) + to->bv_offset;
236719c555fSJens Axboe 			vfrom = kmap_atomic(page) + to->bv_offset;
237719c555fSJens Axboe 			memcpy(vto, vfrom, to->bv_len);
238719c555fSJens Axboe 			kunmap_atomic(vfrom);
239719c555fSJens Axboe 		}
240719c555fSJens Axboe 	}
241719c555fSJens Axboe 
242719c555fSJens Axboe 	trace_block_bio_bounce(q, *bio_orig);
243719c555fSJens Axboe 
244719c555fSJens Axboe 	bio->bi_flags |= (1 << BIO_BOUNCED);
245719c555fSJens Axboe 
246719c555fSJens Axboe 	if (pool == page_pool) {
247719c555fSJens Axboe 		bio->bi_end_io = bounce_end_io_write;
248719c555fSJens Axboe 		if (rw == READ)
249719c555fSJens Axboe 			bio->bi_end_io = bounce_end_io_read;
250719c555fSJens Axboe 	} else {
251719c555fSJens Axboe 		bio->bi_end_io = bounce_end_io_write_isa;
252719c555fSJens Axboe 		if (rw == READ)
253719c555fSJens Axboe 			bio->bi_end_io = bounce_end_io_read_isa;
254719c555fSJens Axboe 	}
255719c555fSJens Axboe 
256719c555fSJens Axboe 	bio->bi_private = *bio_orig;
257719c555fSJens Axboe 	*bio_orig = bio;
258719c555fSJens Axboe }
259719c555fSJens Axboe 
260719c555fSJens Axboe void blk_queue_bounce(struct request_queue *q, struct bio **bio_orig)
261719c555fSJens Axboe {
262719c555fSJens Axboe 	mempool_t *pool;
263719c555fSJens Axboe 
264719c555fSJens Axboe 	/*
265719c555fSJens Axboe 	 * Data-less bio, nothing to bounce
266719c555fSJens Axboe 	 */
267719c555fSJens Axboe 	if (!bio_has_data(*bio_orig))
268719c555fSJens Axboe 		return;
269719c555fSJens Axboe 
270719c555fSJens Axboe 	/*
271719c555fSJens Axboe 	 * for non-isa bounce case, just check if the bounce pfn is equal
272719c555fSJens Axboe 	 * to or bigger than the highest pfn in the system -- in that case,
273719c555fSJens Axboe 	 * don't waste time iterating over bio segments
274719c555fSJens Axboe 	 */
275719c555fSJens Axboe 	if (!(q->bounce_gfp & GFP_DMA)) {
2761c4bc3abSChristoph Hellwig 		if (q->limits.bounce_pfn >= blk_max_pfn)
277719c555fSJens Axboe 			return;
278719c555fSJens Axboe 		pool = page_pool;
279719c555fSJens Axboe 	} else {
280719c555fSJens Axboe 		BUG_ON(!isa_page_pool);
281719c555fSJens Axboe 		pool = isa_page_pool;
282719c555fSJens Axboe 	}
283719c555fSJens Axboe 
284719c555fSJens Axboe 	/*
285719c555fSJens Axboe 	 * slow path
286719c555fSJens Axboe 	 */
287a3ad0a9dSJan Kara 	__blk_queue_bounce(q, bio_orig, pool);
288719c555fSJens Axboe }
289