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