1 /* bounce buffer handling for block devices
3 * - Split from highmem.c
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
24 #include <trace/events/block.h>
28 #define ISA_POOL_SIZE 16
30 static struct bio_set
*bounce_bio_set
, *bounce_bio_split
;
31 static mempool_t
*page_pool
, *isa_page_pool
;
33 #if defined(CONFIG_HIGHMEM) || defined(CONFIG_NEED_BOUNCE_POOL)
34 static __init
int init_emergency_pool(void)
36 #if defined(CONFIG_HIGHMEM) && !defined(CONFIG_MEMORY_HOTPLUG)
37 if (max_pfn
<= max_low_pfn
)
41 page_pool
= mempool_create_page_pool(POOL_SIZE
, 0);
43 pr_info("pool size: %d pages\n", POOL_SIZE
);
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
))
50 bounce_bio_split
= bioset_create(BIO_POOL_SIZE
, 0, 0);
51 BUG_ON(!bounce_bio_split
);
56 __initcall(init_emergency_pool
);
61 * highmem version, map in to vec
63 static void bounce_copy_vec(struct bio_vec
*to
, unsigned char *vfrom
)
68 local_irq_save(flags
);
69 vto
= kmap_atomic(to
->bv_page
);
70 memcpy(vto
+ to
->bv_offset
, vfrom
, to
->bv_len
);
72 local_irq_restore(flags
);
75 #else /* CONFIG_HIGHMEM */
77 #define bounce_copy_vec(to, vfrom) \
78 memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
80 #endif /* CONFIG_HIGHMEM */
83 * allocate pages in the DMA region for the ISA pool
85 static void *mempool_alloc_pages_isa(gfp_t gfp_mask
, void *data
)
87 return mempool_alloc_pages(gfp_mask
| GFP_DMA
, data
);
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.
94 int init_emergency_isa_pool(void)
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
);
103 pr_info("isa pool size: %d pages\n", ISA_POOL_SIZE
);
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
112 static void copy_to_high_bio_irq(struct bio
*to
, struct bio
*from
)
114 unsigned char *vfrom
;
115 struct bio_vec tovec
, *fromvec
= from
->bi_io_vec
;
116 struct bvec_iter iter
;
118 bio_for_each_segment(tovec
, to
, iter
) {
119 if (tovec
.bv_page
!= fromvec
->bv_page
) {
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
125 vfrom
= page_address(fromvec
->bv_page
) +
128 bounce_copy_vec(&tovec
, vfrom
);
129 flush_dcache_page(tovec
.bv_page
);
136 static void bounce_end_io(struct bio
*bio
, mempool_t
*pool
)
138 struct bio
*bio_orig
= bio
->bi_private
;
139 struct bio_vec
*bvec
, *org_vec
;
141 int start
= bio_orig
->bi_iter
.bi_idx
;
144 * free up bounce indirect pages used
146 bio_for_each_segment_all(bvec
, bio
, i
) {
147 org_vec
= bio_orig
->bi_io_vec
+ i
+ start
;
149 if (bvec
->bv_page
== org_vec
->bv_page
)
152 dec_zone_page_state(bvec
->bv_page
, NR_BOUNCE
);
153 mempool_free(bvec
->bv_page
, pool
);
156 bio_orig
->bi_status
= bio
->bi_status
;
161 static void bounce_end_io_write(struct bio
*bio
)
163 bounce_end_io(bio
, page_pool
);
166 static void bounce_end_io_write_isa(struct bio
*bio
)
169 bounce_end_io(bio
, isa_page_pool
);
172 static void __bounce_end_io_read(struct bio
*bio
, mempool_t
*pool
)
174 struct bio
*bio_orig
= bio
->bi_private
;
177 copy_to_high_bio_irq(bio_orig
, bio
);
179 bounce_end_io(bio
, pool
);
182 static void bounce_end_io_read(struct bio
*bio
)
184 __bounce_end_io_read(bio
, page_pool
);
187 static void bounce_end_io_read_isa(struct bio
*bio
)
189 __bounce_end_io_read(bio
, isa_page_pool
);
192 static void __blk_queue_bounce(struct request_queue
*q
, struct bio
**bio_orig
,
196 int rw
= bio_data_dir(*bio_orig
);
197 struct bio_vec
*to
, from
;
198 struct bvec_iter iter
;
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
)
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
);
218 bio
= bio_clone_bioset(*bio_orig
, GFP_NOIO
, bounce_bio_set
);
220 bio_for_each_segment_all(to
, bio
, i
) {
221 struct page
*page
= to
->bv_page
;
223 if (page_to_pfn(page
) <= q
->limits
.bounce_pfn
)
226 to
->bv_page
= mempool_alloc(pool
, q
->bounce_gfp
);
227 inc_zone_page_state(to
->bv_page
, NR_BOUNCE
);
232 flush_dcache_page(page
);
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
);
241 trace_block_bio_bounce(q
, *bio_orig
);
243 bio
->bi_flags
|= (1 << BIO_BOUNCED
);
245 if (pool
== page_pool
) {
246 bio
->bi_end_io
= bounce_end_io_write
;
248 bio
->bi_end_io
= bounce_end_io_read
;
250 bio
->bi_end_io
= bounce_end_io_write_isa
;
252 bio
->bi_end_io
= bounce_end_io_read_isa
;
255 bio
->bi_private
= *bio_orig
;
259 void blk_queue_bounce(struct request_queue
*q
, struct bio
**bio_orig
)
264 * Data-less bio, nothing to bounce
266 if (!bio_has_data(*bio_orig
))
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
274 if (!(q
->bounce_gfp
& GFP_DMA
)) {
275 if (q
->limits
.bounce_pfn
>= blk_max_pfn
)
279 BUG_ON(!isa_page_pool
);
280 pool
= isa_page_pool
;
286 __blk_queue_bounce(q
, bio_orig
, pool
);