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> 2157c8a661SMike Rapoport #include <linux/memblock.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 31338aa96dSKent Overstreet static struct bio_set bounce_bio_set, bounce_bio_split; 32*ce288e05SChristoph Hellwig static mempool_t page_pool; 33719c555fSJens Axboe 3452990a5fSJens Axboe static void init_bounce_bioset(void) 3552990a5fSJens Axboe { 3652990a5fSJens Axboe static bool bounce_bs_setup; 3752990a5fSJens Axboe int ret; 3852990a5fSJens Axboe 3952990a5fSJens Axboe if (bounce_bs_setup) 4052990a5fSJens Axboe return; 4152990a5fSJens Axboe 4252990a5fSJens Axboe ret = bioset_init(&bounce_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); 4352990a5fSJens Axboe BUG_ON(ret); 4452990a5fSJens Axboe if (bioset_integrity_create(&bounce_bio_set, BIO_POOL_SIZE)) 4552990a5fSJens Axboe BUG_ON(1); 4652990a5fSJens Axboe 4752990a5fSJens Axboe ret = bioset_init(&bounce_bio_split, BIO_POOL_SIZE, 0, 0); 4852990a5fSJens Axboe BUG_ON(ret); 4952990a5fSJens Axboe bounce_bs_setup = true; 5052990a5fSJens Axboe } 5152990a5fSJens Axboe 52a687a533SArnd Bergmann #if defined(CONFIG_HIGHMEM) 53719c555fSJens Axboe static __init int init_emergency_pool(void) 54719c555fSJens Axboe { 55338aa96dSKent Overstreet int ret; 56719c555fSJens Axboe #if defined(CONFIG_HIGHMEM) && !defined(CONFIG_MEMORY_HOTPLUG) 57719c555fSJens Axboe if (max_pfn <= max_low_pfn) 58719c555fSJens Axboe return 0; 59719c555fSJens Axboe #endif 60719c555fSJens Axboe 61338aa96dSKent Overstreet ret = mempool_init_page_pool(&page_pool, POOL_SIZE, 0); 62338aa96dSKent Overstreet BUG_ON(ret); 63b1de0d13SMitchel Humpherys pr_info("pool size: %d pages\n", POOL_SIZE); 64719c555fSJens Axboe 6552990a5fSJens Axboe init_bounce_bioset(); 66719c555fSJens Axboe return 0; 67719c555fSJens Axboe } 68719c555fSJens Axboe 69719c555fSJens Axboe __initcall(init_emergency_pool); 70719c555fSJens Axboe #endif 71719c555fSJens Axboe 72719c555fSJens Axboe #ifdef CONFIG_HIGHMEM 73719c555fSJens Axboe /* 74719c555fSJens Axboe * highmem version, map in to vec 75719c555fSJens Axboe */ 76719c555fSJens Axboe static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom) 77719c555fSJens Axboe { 78719c555fSJens Axboe unsigned char *vto; 79719c555fSJens Axboe 80719c555fSJens Axboe vto = kmap_atomic(to->bv_page); 81719c555fSJens Axboe memcpy(vto + to->bv_offset, vfrom, to->bv_len); 82719c555fSJens Axboe kunmap_atomic(vto); 83719c555fSJens Axboe } 84719c555fSJens Axboe 85719c555fSJens Axboe #else /* CONFIG_HIGHMEM */ 86719c555fSJens Axboe 87719c555fSJens Axboe #define bounce_copy_vec(to, vfrom) \ 88719c555fSJens Axboe memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len) 89719c555fSJens Axboe 90719c555fSJens Axboe #endif /* CONFIG_HIGHMEM */ 91719c555fSJens Axboe 92719c555fSJens Axboe /* 93719c555fSJens Axboe * Simple bounce buffer support for highmem pages. Depending on the 94719c555fSJens Axboe * queue gfp mask set, *to may or may not be a highmem page. kmap it 95719c555fSJens Axboe * always, it will do the Right Thing 96719c555fSJens Axboe */ 97719c555fSJens Axboe static void copy_to_high_bio_irq(struct bio *to, struct bio *from) 98719c555fSJens Axboe { 99719c555fSJens Axboe unsigned char *vfrom; 1003c892a09SMing Lei struct bio_vec tovec, fromvec; 101719c555fSJens Axboe struct bvec_iter iter; 1023c892a09SMing Lei /* 1033c892a09SMing Lei * The bio of @from is created by bounce, so we can iterate 1043c892a09SMing Lei * its bvec from start to end, but the @from->bi_iter can't be 1053c892a09SMing Lei * trusted because it might be changed by splitting. 1063c892a09SMing Lei */ 1073c892a09SMing Lei struct bvec_iter from_iter = BVEC_ITER_ALL_INIT; 108719c555fSJens Axboe 109719c555fSJens Axboe bio_for_each_segment(tovec, to, iter) { 1103c892a09SMing Lei fromvec = bio_iter_iovec(from, from_iter); 1113c892a09SMing Lei if (tovec.bv_page != fromvec.bv_page) { 112719c555fSJens Axboe /* 113719c555fSJens Axboe * fromvec->bv_offset and fromvec->bv_len might have 114719c555fSJens Axboe * been modified by the block layer, so use the original 115719c555fSJens Axboe * copy, bounce_copy_vec already uses tovec->bv_len 116719c555fSJens Axboe */ 1173c892a09SMing Lei vfrom = page_address(fromvec.bv_page) + 118719c555fSJens Axboe tovec.bv_offset; 119719c555fSJens Axboe 120719c555fSJens Axboe bounce_copy_vec(&tovec, vfrom); 121719c555fSJens Axboe flush_dcache_page(tovec.bv_page); 122719c555fSJens Axboe } 1233c892a09SMing Lei bio_advance_iter(from, &from_iter, tovec.bv_len); 124719c555fSJens Axboe } 125719c555fSJens Axboe } 126719c555fSJens Axboe 127*ce288e05SChristoph Hellwig static void bounce_end_io(struct bio *bio) 128719c555fSJens Axboe { 129719c555fSJens Axboe struct bio *bio_orig = bio->bi_private; 1307891f05cSMing Lei struct bio_vec *bvec, orig_vec; 1317891f05cSMing Lei struct bvec_iter orig_iter = bio_orig->bi_iter; 1326dc4f100SMing Lei struct bvec_iter_all iter_all; 133719c555fSJens Axboe 134719c555fSJens Axboe /* 135719c555fSJens Axboe * free up bounce indirect pages used 136719c555fSJens Axboe */ 1372b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 1387891f05cSMing Lei orig_vec = bio_iter_iovec(bio_orig, orig_iter); 1397891f05cSMing Lei if (bvec->bv_page != orig_vec.bv_page) { 140719c555fSJens Axboe dec_zone_page_state(bvec->bv_page, NR_BOUNCE); 141*ce288e05SChristoph Hellwig mempool_free(bvec->bv_page, &page_pool); 142719c555fSJens Axboe } 1437891f05cSMing Lei bio_advance_iter(bio_orig, &orig_iter, orig_vec.bv_len); 1447891f05cSMing Lei } 145719c555fSJens Axboe 1464e4cbee9SChristoph Hellwig bio_orig->bi_status = bio->bi_status; 1474246a0b6SChristoph Hellwig bio_endio(bio_orig); 148719c555fSJens Axboe bio_put(bio); 149719c555fSJens Axboe } 150719c555fSJens Axboe 1514246a0b6SChristoph Hellwig static void bounce_end_io_write(struct bio *bio) 152719c555fSJens Axboe { 153*ce288e05SChristoph Hellwig bounce_end_io(bio); 154719c555fSJens Axboe } 155719c555fSJens Axboe 156*ce288e05SChristoph Hellwig static void bounce_end_io_read(struct bio *bio) 157719c555fSJens Axboe { 158719c555fSJens Axboe struct bio *bio_orig = bio->bi_private; 159719c555fSJens Axboe 1604e4cbee9SChristoph Hellwig if (!bio->bi_status) 161719c555fSJens Axboe copy_to_high_bio_irq(bio_orig, bio); 162719c555fSJens Axboe 163*ce288e05SChristoph Hellwig bounce_end_io(bio); 164719c555fSJens Axboe } 165719c555fSJens Axboe 166ebfe4183SChristoph Hellwig static struct bio *bounce_clone_bio(struct bio *bio_src) 167c55183c9SChristoph Hellwig { 168c55183c9SChristoph Hellwig struct bvec_iter iter; 169c55183c9SChristoph Hellwig struct bio_vec bv; 170c55183c9SChristoph Hellwig struct bio *bio; 171c55183c9SChristoph Hellwig 172c55183c9SChristoph Hellwig /* 173c55183c9SChristoph Hellwig * Pre immutable biovecs, __bio_clone() used to just do a memcpy from 174c55183c9SChristoph Hellwig * bio_src->bi_io_vec to bio->bi_io_vec. 175c55183c9SChristoph Hellwig * 176c55183c9SChristoph Hellwig * We can't do that anymore, because: 177c55183c9SChristoph Hellwig * 178c55183c9SChristoph Hellwig * - The point of cloning the biovec is to produce a bio with a biovec 179c55183c9SChristoph Hellwig * the caller can modify: bi_idx and bi_bvec_done should be 0. 180c55183c9SChristoph Hellwig * 181a8affc03SChristoph Hellwig * - The original bio could've had more than BIO_MAX_VECS biovecs; if 182c55183c9SChristoph Hellwig * we tried to clone the whole thing bio_alloc_bioset() would fail. 183c55183c9SChristoph Hellwig * But the clone should succeed as long as the number of biovecs we 184a8affc03SChristoph Hellwig * actually need to allocate is fewer than BIO_MAX_VECS. 185c55183c9SChristoph Hellwig * 186c55183c9SChristoph Hellwig * - Lastly, bi_vcnt should not be looked at or relied upon by code 187c55183c9SChristoph Hellwig * that does not own the bio - reason being drivers don't use it for 188c55183c9SChristoph Hellwig * iterating over the biovec anymore, so expecting it to be kept up 189c55183c9SChristoph Hellwig * to date (i.e. for clones that share the parent biovec) is just 190c55183c9SChristoph Hellwig * asking for trouble and would force extra work on 191c55183c9SChristoph Hellwig * __bio_clone_fast() anyways. 192c55183c9SChristoph Hellwig */ 193b90994c6SChristoph Hellwig if (bio_is_passthrough(bio_src)) 19447dc096aSChristoph Hellwig bio = bio_kmalloc(GFP_NOIO | __GFP_NOFAIL, 19547dc096aSChristoph Hellwig bio_segments(bio_src)); 196b90994c6SChristoph Hellwig else 197ebfe4183SChristoph Hellwig bio = bio_alloc_bioset(GFP_NOIO, bio_segments(bio_src), 198b90994c6SChristoph Hellwig &bounce_bio_set); 199309dca30SChristoph Hellwig bio->bi_bdev = bio_src->bi_bdev; 20046bbf653SChristoph Hellwig if (bio_flagged(bio_src, BIO_REMAPPED)) 20146bbf653SChristoph Hellwig bio_set_flag(bio, BIO_REMAPPED); 202c55183c9SChristoph Hellwig bio->bi_opf = bio_src->bi_opf; 203ca474b73SHannes Reinecke bio->bi_ioprio = bio_src->bi_ioprio; 204c55183c9SChristoph Hellwig bio->bi_write_hint = bio_src->bi_write_hint; 205c55183c9SChristoph Hellwig bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector; 206c55183c9SChristoph Hellwig bio->bi_iter.bi_size = bio_src->bi_iter.bi_size; 207c55183c9SChristoph Hellwig 208c55183c9SChristoph Hellwig switch (bio_op(bio)) { 209c55183c9SChristoph Hellwig case REQ_OP_DISCARD: 210c55183c9SChristoph Hellwig case REQ_OP_SECURE_ERASE: 211c55183c9SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 212c55183c9SChristoph Hellwig break; 213c55183c9SChristoph Hellwig case REQ_OP_WRITE_SAME: 214c55183c9SChristoph Hellwig bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0]; 215c55183c9SChristoph Hellwig break; 216c55183c9SChristoph Hellwig default: 217c55183c9SChristoph Hellwig bio_for_each_segment(bv, bio_src, iter) 218c55183c9SChristoph Hellwig bio->bi_io_vec[bio->bi_vcnt++] = bv; 219c55183c9SChristoph Hellwig break; 220c55183c9SChristoph Hellwig } 221c55183c9SChristoph Hellwig 222ebfe4183SChristoph Hellwig if (bio_crypt_clone(bio, bio_src, GFP_NOIO) < 0) 22307560151SEric Biggers goto err_put; 224a892c8d5SSatya Tangirala 22507560151SEric Biggers if (bio_integrity(bio_src) && 226ebfe4183SChristoph Hellwig bio_integrity_clone(bio, bio_src, GFP_NOIO) < 0) 22707560151SEric Biggers goto err_put; 228c55183c9SChristoph Hellwig 229db6638d7SDennis Zhou bio_clone_blkg_association(bio, bio_src); 230e439bedfSDennis Zhou blkcg_bio_issue_init(bio); 2315bf9a1f3SDennis Zhou (Facebook) 232c55183c9SChristoph Hellwig return bio; 23307560151SEric Biggers 23407560151SEric Biggers err_put: 23507560151SEric Biggers bio_put(bio); 23607560151SEric Biggers return NULL; 237c55183c9SChristoph Hellwig } 238c55183c9SChristoph Hellwig 239*ce288e05SChristoph Hellwig 240*ce288e05SChristoph Hellwig void blk_queue_bounce(struct request_queue *q, struct bio **bio_orig) 241719c555fSJens Axboe { 242719c555fSJens Axboe struct bio *bio; 243719c555fSJens Axboe int rw = bio_data_dir(*bio_orig); 244719c555fSJens Axboe struct bio_vec *to, from; 245719c555fSJens Axboe struct bvec_iter iter; 246a8821f3fSNeilBrown unsigned i = 0; 247a8821f3fSNeilBrown bool bounce = false; 248a8821f3fSNeilBrown int sectors = 0; 249719c555fSJens Axboe 250*ce288e05SChristoph Hellwig /* 251*ce288e05SChristoph Hellwig * Data-less bio, nothing to bounce 252*ce288e05SChristoph Hellwig */ 253*ce288e05SChristoph Hellwig if (!bio_has_data(*bio_orig)) 254*ce288e05SChristoph Hellwig return; 255*ce288e05SChristoph Hellwig 256*ce288e05SChristoph Hellwig /* 257*ce288e05SChristoph Hellwig * Just check if the bounce pfn is equal to or bigger than the highest 258*ce288e05SChristoph Hellwig * pfn in the system -- in that case, don't waste time iterating over 259*ce288e05SChristoph Hellwig * bio segments 260*ce288e05SChristoph Hellwig */ 261*ce288e05SChristoph Hellwig if (q->limits.bounce_pfn >= blk_max_pfn) 262*ce288e05SChristoph Hellwig return; 263*ce288e05SChristoph Hellwig 264a8821f3fSNeilBrown bio_for_each_segment(from, *bio_orig, iter) { 265a8affc03SChristoph Hellwig if (i++ < BIO_MAX_VECS) 266a8821f3fSNeilBrown sectors += from.bv_len >> 9; 2671c4bc3abSChristoph Hellwig if (page_to_pfn(from.bv_page) > q->limits.bounce_pfn) 268a8821f3fSNeilBrown bounce = true; 269a8821f3fSNeilBrown } 270a8821f3fSNeilBrown if (!bounce) 271719c555fSJens Axboe return; 272a8821f3fSNeilBrown 273b90994c6SChristoph Hellwig if (!bio_is_passthrough(*bio_orig) && 274b90994c6SChristoph Hellwig sectors < bio_sectors(*bio_orig)) { 275338aa96dSKent Overstreet bio = bio_split(*bio_orig, sectors, GFP_NOIO, &bounce_bio_split); 276a8821f3fSNeilBrown bio_chain(bio, *bio_orig); 277ed00aabdSChristoph Hellwig submit_bio_noacct(*bio_orig); 278a8821f3fSNeilBrown *bio_orig = bio; 279a8821f3fSNeilBrown } 280ebfe4183SChristoph Hellwig bio = bounce_clone_bio(*bio_orig); 281719c555fSJens Axboe 2828f4e80daSMing Lei /* 2838f4e80daSMing Lei * Bvec table can't be updated by bio_for_each_segment_all(), 2848f4e80daSMing Lei * so retrieve bvec from the table directly. This way is safe 2858f4e80daSMing Lei * because the 'bio' is single-page bvec. 2868f4e80daSMing Lei */ 2878f4e80daSMing Lei for (i = 0, to = bio->bi_io_vec; i < bio->bi_vcnt; to++, i++) { 288719c555fSJens Axboe struct page *page = to->bv_page; 289719c555fSJens Axboe 2901c4bc3abSChristoph Hellwig if (page_to_pfn(page) <= q->limits.bounce_pfn) 291719c555fSJens Axboe continue; 292719c555fSJens Axboe 293*ce288e05SChristoph Hellwig to->bv_page = mempool_alloc(&page_pool, GFP_NOIO); 294393a3397SWang YanQing inc_zone_page_state(to->bv_page, NR_BOUNCE); 295719c555fSJens Axboe 296719c555fSJens Axboe if (rw == WRITE) { 297719c555fSJens Axboe char *vto, *vfrom; 298719c555fSJens Axboe 299719c555fSJens Axboe flush_dcache_page(page); 300719c555fSJens Axboe 301719c555fSJens Axboe vto = page_address(to->bv_page) + to->bv_offset; 302719c555fSJens Axboe vfrom = kmap_atomic(page) + to->bv_offset; 303719c555fSJens Axboe memcpy(vto, vfrom, to->bv_len); 304719c555fSJens Axboe kunmap_atomic(vfrom); 305719c555fSJens Axboe } 306719c555fSJens Axboe } 307719c555fSJens Axboe 308e8a676d6SChristoph Hellwig trace_block_bio_bounce(*bio_orig); 309719c555fSJens Axboe 310719c555fSJens Axboe bio->bi_flags |= (1 << BIO_BOUNCED); 311719c555fSJens Axboe 312719c555fSJens Axboe if (rw == READ) 313719c555fSJens Axboe bio->bi_end_io = bounce_end_io_read; 314*ce288e05SChristoph Hellwig else 315*ce288e05SChristoph Hellwig bio->bi_end_io = bounce_end_io_write; 316719c555fSJens Axboe 317719c555fSJens Axboe bio->bi_private = *bio_orig; 318719c555fSJens Axboe *bio_orig = bio; 319719c555fSJens Axboe } 320