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