1 // SPDX-License-Identifier: GPL-2.0 2 /* bounce buffer handling for block devices 3 * 4 * - Split from highmem.c 5 */ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9 #include <linux/mm.h> 10 #include <linux/export.h> 11 #include <linux/swap.h> 12 #include <linux/gfp.h> 13 #include <linux/bio.h> 14 #include <linux/pagemap.h> 15 #include <linux/mempool.h> 16 #include <linux/blkdev.h> 17 #include <linux/backing-dev.h> 18 #include <linux/init.h> 19 #include <linux/hash.h> 20 #include <linux/highmem.h> 21 #include <linux/memblock.h> 22 #include <linux/printk.h> 23 #include <asm/tlbflush.h> 24 25 #include <trace/events/block.h> 26 #include "blk.h" 27 28 #define POOL_SIZE 64 29 #define ISA_POOL_SIZE 16 30 31 static struct bio_set bounce_bio_set, bounce_bio_split; 32 static mempool_t page_pool; 33 34 static void init_bounce_bioset(void) 35 { 36 static bool bounce_bs_setup; 37 int ret; 38 39 if (bounce_bs_setup) 40 return; 41 42 ret = bioset_init(&bounce_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); 43 BUG_ON(ret); 44 if (bioset_integrity_create(&bounce_bio_set, BIO_POOL_SIZE)) 45 BUG_ON(1); 46 47 ret = bioset_init(&bounce_bio_split, BIO_POOL_SIZE, 0, 0); 48 BUG_ON(ret); 49 bounce_bs_setup = true; 50 } 51 52 #if defined(CONFIG_HIGHMEM) 53 static __init int init_emergency_pool(void) 54 { 55 int ret; 56 #if defined(CONFIG_HIGHMEM) && !defined(CONFIG_MEMORY_HOTPLUG) 57 if (max_pfn <= max_low_pfn) 58 return 0; 59 #endif 60 61 ret = mempool_init_page_pool(&page_pool, POOL_SIZE, 0); 62 BUG_ON(ret); 63 pr_info("pool size: %d pages\n", POOL_SIZE); 64 65 init_bounce_bioset(); 66 return 0; 67 } 68 69 __initcall(init_emergency_pool); 70 #endif 71 72 #ifdef CONFIG_HIGHMEM 73 /* 74 * highmem version, map in to vec 75 */ 76 static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom) 77 { 78 unsigned char *vto; 79 80 vto = kmap_atomic(to->bv_page); 81 memcpy(vto + to->bv_offset, vfrom, to->bv_len); 82 kunmap_atomic(vto); 83 } 84 85 #else /* CONFIG_HIGHMEM */ 86 87 #define bounce_copy_vec(to, vfrom) \ 88 memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len) 89 90 #endif /* CONFIG_HIGHMEM */ 91 92 /* 93 * Simple bounce buffer support for highmem pages. Depending on the 94 * queue gfp mask set, *to may or may not be a highmem page. kmap it 95 * always, it will do the Right Thing 96 */ 97 static void copy_to_high_bio_irq(struct bio *to, struct bio *from) 98 { 99 unsigned char *vfrom; 100 struct bio_vec tovec, fromvec; 101 struct bvec_iter iter; 102 /* 103 * The bio of @from is created by bounce, so we can iterate 104 * its bvec from start to end, but the @from->bi_iter can't be 105 * trusted because it might be changed by splitting. 106 */ 107 struct bvec_iter from_iter = BVEC_ITER_ALL_INIT; 108 109 bio_for_each_segment(tovec, to, iter) { 110 fromvec = bio_iter_iovec(from, from_iter); 111 if (tovec.bv_page != fromvec.bv_page) { 112 /* 113 * fromvec->bv_offset and fromvec->bv_len might have 114 * been modified by the block layer, so use the original 115 * copy, bounce_copy_vec already uses tovec->bv_len 116 */ 117 vfrom = page_address(fromvec.bv_page) + 118 tovec.bv_offset; 119 120 bounce_copy_vec(&tovec, vfrom); 121 flush_dcache_page(tovec.bv_page); 122 } 123 bio_advance_iter(from, &from_iter, tovec.bv_len); 124 } 125 } 126 127 static void bounce_end_io(struct bio *bio) 128 { 129 struct bio *bio_orig = bio->bi_private; 130 struct bio_vec *bvec, orig_vec; 131 struct bvec_iter orig_iter = bio_orig->bi_iter; 132 struct bvec_iter_all iter_all; 133 134 /* 135 * free up bounce indirect pages used 136 */ 137 bio_for_each_segment_all(bvec, bio, iter_all) { 138 orig_vec = bio_iter_iovec(bio_orig, orig_iter); 139 if (bvec->bv_page != orig_vec.bv_page) { 140 dec_zone_page_state(bvec->bv_page, NR_BOUNCE); 141 mempool_free(bvec->bv_page, &page_pool); 142 } 143 bio_advance_iter(bio_orig, &orig_iter, orig_vec.bv_len); 144 } 145 146 bio_orig->bi_status = bio->bi_status; 147 bio_endio(bio_orig); 148 bio_put(bio); 149 } 150 151 static void bounce_end_io_write(struct bio *bio) 152 { 153 bounce_end_io(bio); 154 } 155 156 static void bounce_end_io_read(struct bio *bio) 157 { 158 struct bio *bio_orig = bio->bi_private; 159 160 if (!bio->bi_status) 161 copy_to_high_bio_irq(bio_orig, bio); 162 163 bounce_end_io(bio); 164 } 165 166 static struct bio *bounce_clone_bio(struct bio *bio_src) 167 { 168 struct bvec_iter iter; 169 struct bio_vec bv; 170 struct bio *bio; 171 172 /* 173 * Pre immutable biovecs, __bio_clone() used to just do a memcpy from 174 * bio_src->bi_io_vec to bio->bi_io_vec. 175 * 176 * We can't do that anymore, because: 177 * 178 * - The point of cloning the biovec is to produce a bio with a biovec 179 * the caller can modify: bi_idx and bi_bvec_done should be 0. 180 * 181 * - The original bio could've had more than BIO_MAX_VECS biovecs; if 182 * we tried to clone the whole thing bio_alloc_bioset() would fail. 183 * But the clone should succeed as long as the number of biovecs we 184 * actually need to allocate is fewer than BIO_MAX_VECS. 185 * 186 * - Lastly, bi_vcnt should not be looked at or relied upon by code 187 * that does not own the bio - reason being drivers don't use it for 188 * iterating over the biovec anymore, so expecting it to be kept up 189 * to date (i.e. for clones that share the parent biovec) is just 190 * asking for trouble and would force extra work on 191 * __bio_clone_fast() anyways. 192 */ 193 if (bio_is_passthrough(bio_src)) 194 bio = bio_kmalloc(GFP_NOIO | __GFP_NOFAIL, 195 bio_segments(bio_src)); 196 else 197 bio = bio_alloc_bioset(GFP_NOIO, bio_segments(bio_src), 198 &bounce_bio_set); 199 bio->bi_bdev = bio_src->bi_bdev; 200 if (bio_flagged(bio_src, BIO_REMAPPED)) 201 bio_set_flag(bio, BIO_REMAPPED); 202 bio->bi_opf = bio_src->bi_opf; 203 bio->bi_ioprio = bio_src->bi_ioprio; 204 bio->bi_write_hint = bio_src->bi_write_hint; 205 bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector; 206 bio->bi_iter.bi_size = bio_src->bi_iter.bi_size; 207 208 switch (bio_op(bio)) { 209 case REQ_OP_DISCARD: 210 case REQ_OP_SECURE_ERASE: 211 case REQ_OP_WRITE_ZEROES: 212 break; 213 case REQ_OP_WRITE_SAME: 214 bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0]; 215 break; 216 default: 217 bio_for_each_segment(bv, bio_src, iter) 218 bio->bi_io_vec[bio->bi_vcnt++] = bv; 219 break; 220 } 221 222 if (bio_crypt_clone(bio, bio_src, GFP_NOIO) < 0) 223 goto err_put; 224 225 if (bio_integrity(bio_src) && 226 bio_integrity_clone(bio, bio_src, GFP_NOIO) < 0) 227 goto err_put; 228 229 bio_clone_blkg_association(bio, bio_src); 230 blkcg_bio_issue_init(bio); 231 232 return bio; 233 234 err_put: 235 bio_put(bio); 236 return NULL; 237 } 238 239 240 void blk_queue_bounce(struct request_queue *q, struct bio **bio_orig) 241 { 242 struct bio *bio; 243 int rw = bio_data_dir(*bio_orig); 244 struct bio_vec *to, from; 245 struct bvec_iter iter; 246 unsigned i = 0; 247 bool bounce = false; 248 int sectors = 0; 249 250 /* 251 * Data-less bio, nothing to bounce 252 */ 253 if (!bio_has_data(*bio_orig)) 254 return; 255 256 /* 257 * Just check if the bounce pfn is equal to or bigger than the highest 258 * pfn in the system -- in that case, don't waste time iterating over 259 * bio segments 260 */ 261 if (q->limits.bounce_pfn >= blk_max_pfn) 262 return; 263 264 bio_for_each_segment(from, *bio_orig, iter) { 265 if (i++ < BIO_MAX_VECS) 266 sectors += from.bv_len >> 9; 267 if (page_to_pfn(from.bv_page) > q->limits.bounce_pfn) 268 bounce = true; 269 } 270 if (!bounce) 271 return; 272 273 if (!bio_is_passthrough(*bio_orig) && 274 sectors < bio_sectors(*bio_orig)) { 275 bio = bio_split(*bio_orig, sectors, GFP_NOIO, &bounce_bio_split); 276 bio_chain(bio, *bio_orig); 277 submit_bio_noacct(*bio_orig); 278 *bio_orig = bio; 279 } 280 bio = bounce_clone_bio(*bio_orig); 281 282 /* 283 * Bvec table can't be updated by bio_for_each_segment_all(), 284 * so retrieve bvec from the table directly. This way is safe 285 * because the 'bio' is single-page bvec. 286 */ 287 for (i = 0, to = bio->bi_io_vec; i < bio->bi_vcnt; to++, i++) { 288 struct page *page = to->bv_page; 289 290 if (page_to_pfn(page) <= q->limits.bounce_pfn) 291 continue; 292 293 to->bv_page = mempool_alloc(&page_pool, GFP_NOIO); 294 inc_zone_page_state(to->bv_page, NR_BOUNCE); 295 296 if (rw == WRITE) { 297 char *vto, *vfrom; 298 299 flush_dcache_page(page); 300 301 vto = page_address(to->bv_page) + to->bv_offset; 302 vfrom = kmap_atomic(page) + to->bv_offset; 303 memcpy(vto, vfrom, to->bv_len); 304 kunmap_atomic(vfrom); 305 } 306 } 307 308 trace_block_bio_bounce(*bio_orig); 309 310 bio->bi_flags |= (1 << BIO_BOUNCED); 311 312 if (rw == READ) 313 bio->bi_end_io = bounce_end_io_read; 314 else 315 bio->bi_end_io = bounce_end_io_write; 316 317 bio->bi_private = *bio_orig; 318 *bio_orig = bio; 319 } 320