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; 32338aa96dSKent Overstreet static mempool_t page_pool, isa_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 * allocate pages in the DMA region for the ISA pool 94719c555fSJens Axboe */ 95719c555fSJens Axboe static void *mempool_alloc_pages_isa(gfp_t gfp_mask, void *data) 96719c555fSJens Axboe { 97719c555fSJens Axboe return mempool_alloc_pages(gfp_mask | GFP_DMA, data); 98719c555fSJens Axboe } 99719c555fSJens Axboe 10052990a5fSJens Axboe static DEFINE_MUTEX(isa_mutex); 10152990a5fSJens Axboe 102719c555fSJens Axboe /* 103719c555fSJens Axboe * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA 104719c555fSJens Axboe * as the max address, so check if the pool has already been created. 105719c555fSJens Axboe */ 106719c555fSJens Axboe int init_emergency_isa_pool(void) 107719c555fSJens Axboe { 108338aa96dSKent Overstreet int ret; 109338aa96dSKent Overstreet 11052990a5fSJens Axboe mutex_lock(&isa_mutex); 11152990a5fSJens Axboe 11252990a5fSJens Axboe if (mempool_initialized(&isa_page_pool)) { 11352990a5fSJens Axboe mutex_unlock(&isa_mutex); 114719c555fSJens Axboe return 0; 11552990a5fSJens Axboe } 116719c555fSJens Axboe 117338aa96dSKent Overstreet ret = mempool_init(&isa_page_pool, ISA_POOL_SIZE, mempool_alloc_pages_isa, 118719c555fSJens Axboe mempool_free_pages, (void *) 0); 119338aa96dSKent Overstreet BUG_ON(ret); 120719c555fSJens Axboe 121b1de0d13SMitchel Humpherys pr_info("isa pool size: %d pages\n", ISA_POOL_SIZE); 12252990a5fSJens Axboe init_bounce_bioset(); 12352990a5fSJens Axboe mutex_unlock(&isa_mutex); 124719c555fSJens Axboe return 0; 125719c555fSJens Axboe } 126719c555fSJens Axboe 127719c555fSJens Axboe /* 128719c555fSJens Axboe * Simple bounce buffer support for highmem pages. Depending on the 129719c555fSJens Axboe * queue gfp mask set, *to may or may not be a highmem page. kmap it 130719c555fSJens Axboe * always, it will do the Right Thing 131719c555fSJens Axboe */ 132719c555fSJens Axboe static void copy_to_high_bio_irq(struct bio *to, struct bio *from) 133719c555fSJens Axboe { 134719c555fSJens Axboe unsigned char *vfrom; 1353c892a09SMing Lei struct bio_vec tovec, fromvec; 136719c555fSJens Axboe struct bvec_iter iter; 1373c892a09SMing Lei /* 1383c892a09SMing Lei * The bio of @from is created by bounce, so we can iterate 1393c892a09SMing Lei * its bvec from start to end, but the @from->bi_iter can't be 1403c892a09SMing Lei * trusted because it might be changed by splitting. 1413c892a09SMing Lei */ 1423c892a09SMing Lei struct bvec_iter from_iter = BVEC_ITER_ALL_INIT; 143719c555fSJens Axboe 144719c555fSJens Axboe bio_for_each_segment(tovec, to, iter) { 1453c892a09SMing Lei fromvec = bio_iter_iovec(from, from_iter); 1463c892a09SMing Lei if (tovec.bv_page != fromvec.bv_page) { 147719c555fSJens Axboe /* 148719c555fSJens Axboe * fromvec->bv_offset and fromvec->bv_len might have 149719c555fSJens Axboe * been modified by the block layer, so use the original 150719c555fSJens Axboe * copy, bounce_copy_vec already uses tovec->bv_len 151719c555fSJens Axboe */ 1523c892a09SMing Lei vfrom = page_address(fromvec.bv_page) + 153719c555fSJens Axboe tovec.bv_offset; 154719c555fSJens Axboe 155719c555fSJens Axboe bounce_copy_vec(&tovec, vfrom); 156719c555fSJens Axboe flush_dcache_page(tovec.bv_page); 157719c555fSJens Axboe } 1583c892a09SMing Lei bio_advance_iter(from, &from_iter, tovec.bv_len); 159719c555fSJens Axboe } 160719c555fSJens Axboe } 161719c555fSJens Axboe 1624246a0b6SChristoph Hellwig static void bounce_end_io(struct bio *bio, mempool_t *pool) 163719c555fSJens Axboe { 164719c555fSJens Axboe struct bio *bio_orig = bio->bi_private; 1657891f05cSMing Lei struct bio_vec *bvec, orig_vec; 166719c555fSJens Axboe int i; 1677891f05cSMing Lei struct bvec_iter orig_iter = bio_orig->bi_iter; 168719c555fSJens Axboe 169719c555fSJens Axboe /* 170719c555fSJens Axboe * free up bounce indirect pages used 171719c555fSJens Axboe */ 172719c555fSJens Axboe bio_for_each_segment_all(bvec, bio, i) { 1737891f05cSMing Lei orig_vec = bio_iter_iovec(bio_orig, orig_iter); 1747891f05cSMing Lei if (bvec->bv_page != orig_vec.bv_page) { 175719c555fSJens Axboe dec_zone_page_state(bvec->bv_page, NR_BOUNCE); 176719c555fSJens Axboe mempool_free(bvec->bv_page, pool); 177719c555fSJens Axboe } 1787891f05cSMing Lei bio_advance_iter(bio_orig, &orig_iter, orig_vec.bv_len); 1797891f05cSMing Lei } 180719c555fSJens Axboe 1814e4cbee9SChristoph Hellwig bio_orig->bi_status = bio->bi_status; 1824246a0b6SChristoph Hellwig bio_endio(bio_orig); 183719c555fSJens Axboe bio_put(bio); 184719c555fSJens Axboe } 185719c555fSJens Axboe 1864246a0b6SChristoph Hellwig static void bounce_end_io_write(struct bio *bio) 187719c555fSJens Axboe { 188338aa96dSKent Overstreet bounce_end_io(bio, &page_pool); 189719c555fSJens Axboe } 190719c555fSJens Axboe 1914246a0b6SChristoph Hellwig static void bounce_end_io_write_isa(struct bio *bio) 192719c555fSJens Axboe { 193719c555fSJens Axboe 194338aa96dSKent Overstreet bounce_end_io(bio, &isa_page_pool); 195719c555fSJens Axboe } 196719c555fSJens Axboe 1974246a0b6SChristoph Hellwig static void __bounce_end_io_read(struct bio *bio, mempool_t *pool) 198719c555fSJens Axboe { 199719c555fSJens Axboe struct bio *bio_orig = bio->bi_private; 200719c555fSJens Axboe 2014e4cbee9SChristoph Hellwig if (!bio->bi_status) 202719c555fSJens Axboe copy_to_high_bio_irq(bio_orig, bio); 203719c555fSJens Axboe 2044246a0b6SChristoph Hellwig bounce_end_io(bio, pool); 205719c555fSJens Axboe } 206719c555fSJens Axboe 2074246a0b6SChristoph Hellwig static void bounce_end_io_read(struct bio *bio) 208719c555fSJens Axboe { 209338aa96dSKent Overstreet __bounce_end_io_read(bio, &page_pool); 210719c555fSJens Axboe } 211719c555fSJens Axboe 2124246a0b6SChristoph Hellwig static void bounce_end_io_read_isa(struct bio *bio) 213719c555fSJens Axboe { 214338aa96dSKent Overstreet __bounce_end_io_read(bio, &isa_page_pool); 215719c555fSJens Axboe } 216719c555fSJens Axboe 217c55183c9SChristoph Hellwig static struct bio *bounce_clone_bio(struct bio *bio_src, gfp_t gfp_mask, 218c55183c9SChristoph Hellwig struct bio_set *bs) 219c55183c9SChristoph Hellwig { 220c55183c9SChristoph Hellwig struct bvec_iter iter; 221c55183c9SChristoph Hellwig struct bio_vec bv; 222c55183c9SChristoph Hellwig struct bio *bio; 223c55183c9SChristoph Hellwig 224c55183c9SChristoph Hellwig /* 225c55183c9SChristoph Hellwig * Pre immutable biovecs, __bio_clone() used to just do a memcpy from 226c55183c9SChristoph Hellwig * bio_src->bi_io_vec to bio->bi_io_vec. 227c55183c9SChristoph Hellwig * 228c55183c9SChristoph Hellwig * We can't do that anymore, because: 229c55183c9SChristoph Hellwig * 230c55183c9SChristoph Hellwig * - The point of cloning the biovec is to produce a bio with a biovec 231c55183c9SChristoph Hellwig * the caller can modify: bi_idx and bi_bvec_done should be 0. 232c55183c9SChristoph Hellwig * 233c55183c9SChristoph Hellwig * - The original bio could've had more than BIO_MAX_PAGES biovecs; if 234c55183c9SChristoph Hellwig * we tried to clone the whole thing bio_alloc_bioset() would fail. 235c55183c9SChristoph Hellwig * But the clone should succeed as long as the number of biovecs we 236c55183c9SChristoph Hellwig * actually need to allocate is fewer than BIO_MAX_PAGES. 237c55183c9SChristoph Hellwig * 238c55183c9SChristoph Hellwig * - Lastly, bi_vcnt should not be looked at or relied upon by code 239c55183c9SChristoph Hellwig * that does not own the bio - reason being drivers don't use it for 240c55183c9SChristoph Hellwig * iterating over the biovec anymore, so expecting it to be kept up 241c55183c9SChristoph Hellwig * to date (i.e. for clones that share the parent biovec) is just 242c55183c9SChristoph Hellwig * asking for trouble and would force extra work on 243c55183c9SChristoph Hellwig * __bio_clone_fast() anyways. 244c55183c9SChristoph Hellwig */ 245c55183c9SChristoph Hellwig 246c55183c9SChristoph Hellwig bio = bio_alloc_bioset(gfp_mask, bio_segments(bio_src), bs); 247c55183c9SChristoph Hellwig if (!bio) 248c55183c9SChristoph Hellwig return NULL; 249c55183c9SChristoph Hellwig bio->bi_disk = bio_src->bi_disk; 250c55183c9SChristoph Hellwig bio->bi_opf = bio_src->bi_opf; 251c55183c9SChristoph Hellwig bio->bi_write_hint = bio_src->bi_write_hint; 252c55183c9SChristoph Hellwig bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector; 253c55183c9SChristoph Hellwig bio->bi_iter.bi_size = bio_src->bi_iter.bi_size; 254c55183c9SChristoph Hellwig 255c55183c9SChristoph Hellwig switch (bio_op(bio)) { 256c55183c9SChristoph Hellwig case REQ_OP_DISCARD: 257c55183c9SChristoph Hellwig case REQ_OP_SECURE_ERASE: 258c55183c9SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 259c55183c9SChristoph Hellwig break; 260c55183c9SChristoph Hellwig case REQ_OP_WRITE_SAME: 261c55183c9SChristoph Hellwig bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0]; 262c55183c9SChristoph Hellwig break; 263c55183c9SChristoph Hellwig default: 264c55183c9SChristoph Hellwig bio_for_each_segment(bv, bio_src, iter) 265c55183c9SChristoph Hellwig bio->bi_io_vec[bio->bi_vcnt++] = bv; 266c55183c9SChristoph Hellwig break; 267c55183c9SChristoph Hellwig } 268c55183c9SChristoph Hellwig 269c55183c9SChristoph Hellwig if (bio_integrity(bio_src)) { 270c55183c9SChristoph Hellwig int ret; 271c55183c9SChristoph Hellwig 272c55183c9SChristoph Hellwig ret = bio_integrity_clone(bio, bio_src, gfp_mask); 273c55183c9SChristoph Hellwig if (ret < 0) { 274c55183c9SChristoph Hellwig bio_put(bio); 275c55183c9SChristoph Hellwig return NULL; 276c55183c9SChristoph Hellwig } 277c55183c9SChristoph Hellwig } 278c55183c9SChristoph Hellwig 279c839e7a0SDennis Zhou (Facebook) bio_clone_blkg_association(bio, bio_src); 280c55183c9SChristoph Hellwig 2815bf9a1f3SDennis Zhou (Facebook) blkcg_bio_issue_init(bio); 2825bf9a1f3SDennis Zhou (Facebook) 283c55183c9SChristoph Hellwig return bio; 284c55183c9SChristoph Hellwig } 285c55183c9SChristoph Hellwig 286719c555fSJens Axboe static void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig, 287a3ad0a9dSJan Kara mempool_t *pool) 288719c555fSJens Axboe { 289719c555fSJens Axboe struct bio *bio; 290719c555fSJens Axboe int rw = bio_data_dir(*bio_orig); 291719c555fSJens Axboe struct bio_vec *to, from; 292719c555fSJens Axboe struct bvec_iter iter; 293a8821f3fSNeilBrown unsigned i = 0; 294a8821f3fSNeilBrown bool bounce = false; 295a8821f3fSNeilBrown int sectors = 0; 29614cb0dc6SMing Lei bool passthrough = bio_is_passthrough(*bio_orig); 297719c555fSJens Axboe 298a8821f3fSNeilBrown bio_for_each_segment(from, *bio_orig, iter) { 299a8821f3fSNeilBrown if (i++ < BIO_MAX_PAGES) 300a8821f3fSNeilBrown sectors += from.bv_len >> 9; 3011c4bc3abSChristoph Hellwig if (page_to_pfn(from.bv_page) > q->limits.bounce_pfn) 302a8821f3fSNeilBrown bounce = true; 303a8821f3fSNeilBrown } 304a8821f3fSNeilBrown if (!bounce) 305719c555fSJens Axboe return; 306a8821f3fSNeilBrown 30714cb0dc6SMing Lei if (!passthrough && sectors < bio_sectors(*bio_orig)) { 308338aa96dSKent Overstreet bio = bio_split(*bio_orig, sectors, GFP_NOIO, &bounce_bio_split); 309a8821f3fSNeilBrown bio_chain(bio, *bio_orig); 310a8821f3fSNeilBrown generic_make_request(*bio_orig); 311a8821f3fSNeilBrown *bio_orig = bio; 312a8821f3fSNeilBrown } 313c55183c9SChristoph Hellwig bio = bounce_clone_bio(*bio_orig, GFP_NOIO, passthrough ? NULL : 314338aa96dSKent Overstreet &bounce_bio_set); 315719c555fSJens Axboe 316719c555fSJens Axboe bio_for_each_segment_all(to, bio, i) { 317719c555fSJens Axboe struct page *page = to->bv_page; 318719c555fSJens Axboe 3191c4bc3abSChristoph Hellwig if (page_to_pfn(page) <= q->limits.bounce_pfn) 320719c555fSJens Axboe continue; 321719c555fSJens Axboe 322719c555fSJens Axboe to->bv_page = mempool_alloc(pool, q->bounce_gfp); 323393a3397SWang YanQing inc_zone_page_state(to->bv_page, NR_BOUNCE); 324719c555fSJens Axboe 325719c555fSJens Axboe if (rw == WRITE) { 326719c555fSJens Axboe char *vto, *vfrom; 327719c555fSJens Axboe 328719c555fSJens Axboe flush_dcache_page(page); 329719c555fSJens Axboe 330719c555fSJens Axboe vto = page_address(to->bv_page) + to->bv_offset; 331719c555fSJens Axboe vfrom = kmap_atomic(page) + to->bv_offset; 332719c555fSJens Axboe memcpy(vto, vfrom, to->bv_len); 333719c555fSJens Axboe kunmap_atomic(vfrom); 334719c555fSJens Axboe } 335719c555fSJens Axboe } 336719c555fSJens Axboe 337719c555fSJens Axboe trace_block_bio_bounce(q, *bio_orig); 338719c555fSJens Axboe 339719c555fSJens Axboe bio->bi_flags |= (1 << BIO_BOUNCED); 340719c555fSJens Axboe 341338aa96dSKent Overstreet if (pool == &page_pool) { 342719c555fSJens Axboe bio->bi_end_io = bounce_end_io_write; 343719c555fSJens Axboe if (rw == READ) 344719c555fSJens Axboe bio->bi_end_io = bounce_end_io_read; 345719c555fSJens Axboe } else { 346719c555fSJens Axboe bio->bi_end_io = bounce_end_io_write_isa; 347719c555fSJens Axboe if (rw == READ) 348719c555fSJens Axboe bio->bi_end_io = bounce_end_io_read_isa; 349719c555fSJens Axboe } 350719c555fSJens Axboe 351719c555fSJens Axboe bio->bi_private = *bio_orig; 352719c555fSJens Axboe *bio_orig = bio; 353719c555fSJens Axboe } 354719c555fSJens Axboe 355719c555fSJens Axboe void blk_queue_bounce(struct request_queue *q, struct bio **bio_orig) 356719c555fSJens Axboe { 357719c555fSJens Axboe mempool_t *pool; 358719c555fSJens Axboe 359719c555fSJens Axboe /* 360719c555fSJens Axboe * Data-less bio, nothing to bounce 361719c555fSJens Axboe */ 362719c555fSJens Axboe if (!bio_has_data(*bio_orig)) 363719c555fSJens Axboe return; 364719c555fSJens Axboe 365719c555fSJens Axboe /* 366719c555fSJens Axboe * for non-isa bounce case, just check if the bounce pfn is equal 367719c555fSJens Axboe * to or bigger than the highest pfn in the system -- in that case, 368719c555fSJens Axboe * don't waste time iterating over bio segments 369719c555fSJens Axboe */ 370719c555fSJens Axboe if (!(q->bounce_gfp & GFP_DMA)) { 3711c4bc3abSChristoph Hellwig if (q->limits.bounce_pfn >= blk_max_pfn) 372719c555fSJens Axboe return; 373338aa96dSKent Overstreet pool = &page_pool; 374719c555fSJens Axboe } else { 375338aa96dSKent Overstreet BUG_ON(!mempool_initialized(&isa_page_pool)); 376338aa96dSKent Overstreet pool = &isa_page_pool; 377719c555fSJens Axboe } 378719c555fSJens Axboe 379719c555fSJens Axboe /* 380719c555fSJens Axboe * slow path 381719c555fSJens Axboe */ 382a3ad0a9dSJan Kara __blk_queue_bounce(q, bio_orig, pool); 383719c555fSJens Axboe } 384