18c16567dSChristoph Hellwig // SPDX-License-Identifier: GPL-2.0 2f9c78b2bSJens Axboe /* 3f9c78b2bSJens Axboe * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk> 4f9c78b2bSJens Axboe */ 5f9c78b2bSJens Axboe #include <linux/mm.h> 6f9c78b2bSJens Axboe #include <linux/swap.h> 7f9c78b2bSJens Axboe #include <linux/bio.h> 8f9c78b2bSJens Axboe #include <linux/blkdev.h> 9f9c78b2bSJens Axboe #include <linux/uio.h> 10f9c78b2bSJens Axboe #include <linux/iocontext.h> 11f9c78b2bSJens Axboe #include <linux/slab.h> 12f9c78b2bSJens Axboe #include <linux/init.h> 13f9c78b2bSJens Axboe #include <linux/kernel.h> 14f9c78b2bSJens Axboe #include <linux/export.h> 15f9c78b2bSJens Axboe #include <linux/mempool.h> 16f9c78b2bSJens Axboe #include <linux/workqueue.h> 17f9c78b2bSJens Axboe #include <linux/cgroup.h> 1808e18eabSJosef Bacik #include <linux/blk-cgroup.h> 19b4c5875dSDamien Le Moal #include <linux/highmem.h> 20de6a78b6SMing Lei #include <linux/sched/sysctl.h> 21a892c8d5SSatya Tangirala #include <linux/blk-crypto.h> 22f9c78b2bSJens Axboe 23f9c78b2bSJens Axboe #include <trace/events/block.h> 249e234eeaSShaohua Li #include "blk.h" 2567b42d0bSJosef Bacik #include "blk-rq-qos.h" 26f9c78b2bSJens Axboe 27f9c78b2bSJens Axboe /* 28f9c78b2bSJens Axboe * Test patch to inline a certain number of bi_io_vec's inside the bio 29f9c78b2bSJens Axboe * itself, to shrink a bio data allocation from two mempool calls to one 30f9c78b2bSJens Axboe */ 31f9c78b2bSJens Axboe #define BIO_INLINE_VECS 4 32f9c78b2bSJens Axboe 33f9c78b2bSJens Axboe /* 34f9c78b2bSJens Axboe * if you change this list, also change bvec_alloc or things will 35f9c78b2bSJens Axboe * break badly! cannot be bigger than what you can fit into an 36f9c78b2bSJens Axboe * unsigned short 37f9c78b2bSJens Axboe */ 38bd5c4facSMikulas Patocka #define BV(x, n) { .nr_vecs = x, .name = "biovec-"#n } 39ed996a52SChristoph Hellwig static struct biovec_slab bvec_slabs[BVEC_POOL_NR] __read_mostly = { 40bd5c4facSMikulas Patocka BV(1, 1), BV(4, 4), BV(16, 16), BV(64, 64), BV(128, 128), BV(BIO_MAX_PAGES, max), 41f9c78b2bSJens Axboe }; 42f9c78b2bSJens Axboe #undef BV 43f9c78b2bSJens Axboe 44f9c78b2bSJens Axboe /* 45f9c78b2bSJens Axboe * fs_bio_set is the bio_set containing bio and iovec memory pools used by 46f9c78b2bSJens Axboe * IO code that does not need private memory pools. 47f9c78b2bSJens Axboe */ 48f4f8154aSKent Overstreet struct bio_set fs_bio_set; 49f9c78b2bSJens Axboe EXPORT_SYMBOL(fs_bio_set); 50f9c78b2bSJens Axboe 51f9c78b2bSJens Axboe /* 52f9c78b2bSJens Axboe * Our slab pool management 53f9c78b2bSJens Axboe */ 54f9c78b2bSJens Axboe struct bio_slab { 55f9c78b2bSJens Axboe struct kmem_cache *slab; 56f9c78b2bSJens Axboe unsigned int slab_ref; 57f9c78b2bSJens Axboe unsigned int slab_size; 58f9c78b2bSJens Axboe char name[8]; 59f9c78b2bSJens Axboe }; 60f9c78b2bSJens Axboe static DEFINE_MUTEX(bio_slab_lock); 61f9c78b2bSJens Axboe static struct bio_slab *bio_slabs; 62f9c78b2bSJens Axboe static unsigned int bio_slab_nr, bio_slab_max; 63f9c78b2bSJens Axboe 64f9c78b2bSJens Axboe static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size) 65f9c78b2bSJens Axboe { 66f9c78b2bSJens Axboe unsigned int sz = sizeof(struct bio) + extra_size; 67f9c78b2bSJens Axboe struct kmem_cache *slab = NULL; 68f9c78b2bSJens Axboe struct bio_slab *bslab, *new_bio_slabs; 69f9c78b2bSJens Axboe unsigned int new_bio_slab_max; 70f9c78b2bSJens Axboe unsigned int i, entry = -1; 71f9c78b2bSJens Axboe 72f9c78b2bSJens Axboe mutex_lock(&bio_slab_lock); 73f9c78b2bSJens Axboe 74f9c78b2bSJens Axboe i = 0; 75f9c78b2bSJens Axboe while (i < bio_slab_nr) { 76f9c78b2bSJens Axboe bslab = &bio_slabs[i]; 77f9c78b2bSJens Axboe 78f9c78b2bSJens Axboe if (!bslab->slab && entry == -1) 79f9c78b2bSJens Axboe entry = i; 80f9c78b2bSJens Axboe else if (bslab->slab_size == sz) { 81f9c78b2bSJens Axboe slab = bslab->slab; 82f9c78b2bSJens Axboe bslab->slab_ref++; 83f9c78b2bSJens Axboe break; 84f9c78b2bSJens Axboe } 85f9c78b2bSJens Axboe i++; 86f9c78b2bSJens Axboe } 87f9c78b2bSJens Axboe 88f9c78b2bSJens Axboe if (slab) 89f9c78b2bSJens Axboe goto out_unlock; 90f9c78b2bSJens Axboe 91f9c78b2bSJens Axboe if (bio_slab_nr == bio_slab_max && entry == -1) { 92f9c78b2bSJens Axboe new_bio_slab_max = bio_slab_max << 1; 93f9c78b2bSJens Axboe new_bio_slabs = krealloc(bio_slabs, 94f9c78b2bSJens Axboe new_bio_slab_max * sizeof(struct bio_slab), 95f9c78b2bSJens Axboe GFP_KERNEL); 96f9c78b2bSJens Axboe if (!new_bio_slabs) 97f9c78b2bSJens Axboe goto out_unlock; 98f9c78b2bSJens Axboe bio_slab_max = new_bio_slab_max; 99f9c78b2bSJens Axboe bio_slabs = new_bio_slabs; 100f9c78b2bSJens Axboe } 101f9c78b2bSJens Axboe if (entry == -1) 102f9c78b2bSJens Axboe entry = bio_slab_nr++; 103f9c78b2bSJens Axboe 104f9c78b2bSJens Axboe bslab = &bio_slabs[entry]; 105f9c78b2bSJens Axboe 106f9c78b2bSJens Axboe snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry); 1076a241483SMikulas Patocka slab = kmem_cache_create(bslab->name, sz, ARCH_KMALLOC_MINALIGN, 1086a241483SMikulas Patocka SLAB_HWCACHE_ALIGN, NULL); 109f9c78b2bSJens Axboe if (!slab) 110f9c78b2bSJens Axboe goto out_unlock; 111f9c78b2bSJens Axboe 112f9c78b2bSJens Axboe bslab->slab = slab; 113f9c78b2bSJens Axboe bslab->slab_ref = 1; 114f9c78b2bSJens Axboe bslab->slab_size = sz; 115f9c78b2bSJens Axboe out_unlock: 116f9c78b2bSJens Axboe mutex_unlock(&bio_slab_lock); 117f9c78b2bSJens Axboe return slab; 118f9c78b2bSJens Axboe } 119f9c78b2bSJens Axboe 120f9c78b2bSJens Axboe static void bio_put_slab(struct bio_set *bs) 121f9c78b2bSJens Axboe { 122f9c78b2bSJens Axboe struct bio_slab *bslab = NULL; 123f9c78b2bSJens Axboe unsigned int i; 124f9c78b2bSJens Axboe 125f9c78b2bSJens Axboe mutex_lock(&bio_slab_lock); 126f9c78b2bSJens Axboe 127f9c78b2bSJens Axboe for (i = 0; i < bio_slab_nr; i++) { 128f9c78b2bSJens Axboe if (bs->bio_slab == bio_slabs[i].slab) { 129f9c78b2bSJens Axboe bslab = &bio_slabs[i]; 130f9c78b2bSJens Axboe break; 131f9c78b2bSJens Axboe } 132f9c78b2bSJens Axboe } 133f9c78b2bSJens Axboe 134f9c78b2bSJens Axboe if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n")) 135f9c78b2bSJens Axboe goto out; 136f9c78b2bSJens Axboe 137f9c78b2bSJens Axboe WARN_ON(!bslab->slab_ref); 138f9c78b2bSJens Axboe 139f9c78b2bSJens Axboe if (--bslab->slab_ref) 140f9c78b2bSJens Axboe goto out; 141f9c78b2bSJens Axboe 142f9c78b2bSJens Axboe kmem_cache_destroy(bslab->slab); 143f9c78b2bSJens Axboe bslab->slab = NULL; 144f9c78b2bSJens Axboe 145f9c78b2bSJens Axboe out: 146f9c78b2bSJens Axboe mutex_unlock(&bio_slab_lock); 147f9c78b2bSJens Axboe } 148f9c78b2bSJens Axboe 149f9c78b2bSJens Axboe unsigned int bvec_nr_vecs(unsigned short idx) 150f9c78b2bSJens Axboe { 151d6c02a9bSGreg Edwards return bvec_slabs[--idx].nr_vecs; 152f9c78b2bSJens Axboe } 153f9c78b2bSJens Axboe 154f9c78b2bSJens Axboe void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx) 155f9c78b2bSJens Axboe { 156ed996a52SChristoph Hellwig if (!idx) 157ed996a52SChristoph Hellwig return; 158ed996a52SChristoph Hellwig idx--; 159f9c78b2bSJens Axboe 160ed996a52SChristoph Hellwig BIO_BUG_ON(idx >= BVEC_POOL_NR); 161ed996a52SChristoph Hellwig 162ed996a52SChristoph Hellwig if (idx == BVEC_POOL_MAX) { 163f9c78b2bSJens Axboe mempool_free(bv, pool); 164ed996a52SChristoph Hellwig } else { 165f9c78b2bSJens Axboe struct biovec_slab *bvs = bvec_slabs + idx; 166f9c78b2bSJens Axboe 167f9c78b2bSJens Axboe kmem_cache_free(bvs->slab, bv); 168f9c78b2bSJens Axboe } 169f9c78b2bSJens Axboe } 170f9c78b2bSJens Axboe 171f9c78b2bSJens Axboe struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx, 172f9c78b2bSJens Axboe mempool_t *pool) 173f9c78b2bSJens Axboe { 174f9c78b2bSJens Axboe struct bio_vec *bvl; 175f9c78b2bSJens Axboe 176f9c78b2bSJens Axboe /* 177f9c78b2bSJens Axboe * see comment near bvec_array define! 178f9c78b2bSJens Axboe */ 179f9c78b2bSJens Axboe switch (nr) { 180f9c78b2bSJens Axboe case 1: 181f9c78b2bSJens Axboe *idx = 0; 182f9c78b2bSJens Axboe break; 183f9c78b2bSJens Axboe case 2 ... 4: 184f9c78b2bSJens Axboe *idx = 1; 185f9c78b2bSJens Axboe break; 186f9c78b2bSJens Axboe case 5 ... 16: 187f9c78b2bSJens Axboe *idx = 2; 188f9c78b2bSJens Axboe break; 189f9c78b2bSJens Axboe case 17 ... 64: 190f9c78b2bSJens Axboe *idx = 3; 191f9c78b2bSJens Axboe break; 192f9c78b2bSJens Axboe case 65 ... 128: 193f9c78b2bSJens Axboe *idx = 4; 194f9c78b2bSJens Axboe break; 195f9c78b2bSJens Axboe case 129 ... BIO_MAX_PAGES: 196f9c78b2bSJens Axboe *idx = 5; 197f9c78b2bSJens Axboe break; 198f9c78b2bSJens Axboe default: 199f9c78b2bSJens Axboe return NULL; 200f9c78b2bSJens Axboe } 201f9c78b2bSJens Axboe 202f9c78b2bSJens Axboe /* 203f9c78b2bSJens Axboe * idx now points to the pool we want to allocate from. only the 204f9c78b2bSJens Axboe * 1-vec entry pool is mempool backed. 205f9c78b2bSJens Axboe */ 206ed996a52SChristoph Hellwig if (*idx == BVEC_POOL_MAX) { 207f9c78b2bSJens Axboe fallback: 208f9c78b2bSJens Axboe bvl = mempool_alloc(pool, gfp_mask); 209f9c78b2bSJens Axboe } else { 210f9c78b2bSJens Axboe struct biovec_slab *bvs = bvec_slabs + *idx; 211d0164adcSMel Gorman gfp_t __gfp_mask = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_IO); 212f9c78b2bSJens Axboe 213f9c78b2bSJens Axboe /* 214f9c78b2bSJens Axboe * Make this allocation restricted and don't dump info on 215f9c78b2bSJens Axboe * allocation failures, since we'll fallback to the mempool 216f9c78b2bSJens Axboe * in case of failure. 217f9c78b2bSJens Axboe */ 218f9c78b2bSJens Axboe __gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 219f9c78b2bSJens Axboe 220f9c78b2bSJens Axboe /* 221d0164adcSMel Gorman * Try a slab allocation. If this fails and __GFP_DIRECT_RECLAIM 222f9c78b2bSJens Axboe * is set, retry with the 1-entry mempool 223f9c78b2bSJens Axboe */ 224f9c78b2bSJens Axboe bvl = kmem_cache_alloc(bvs->slab, __gfp_mask); 225d0164adcSMel Gorman if (unlikely(!bvl && (gfp_mask & __GFP_DIRECT_RECLAIM))) { 226ed996a52SChristoph Hellwig *idx = BVEC_POOL_MAX; 227f9c78b2bSJens Axboe goto fallback; 228f9c78b2bSJens Axboe } 229f9c78b2bSJens Axboe } 230f9c78b2bSJens Axboe 231ed996a52SChristoph Hellwig (*idx)++; 232f9c78b2bSJens Axboe return bvl; 233f9c78b2bSJens Axboe } 234f9c78b2bSJens Axboe 2359ae3b3f5SJens Axboe void bio_uninit(struct bio *bio) 236f9c78b2bSJens Axboe { 237db9819c7SChristoph Hellwig #ifdef CONFIG_BLK_CGROUP 238db9819c7SChristoph Hellwig if (bio->bi_blkg) { 239db9819c7SChristoph Hellwig blkg_put(bio->bi_blkg); 240db9819c7SChristoph Hellwig bio->bi_blkg = NULL; 241db9819c7SChristoph Hellwig } 242db9819c7SChristoph Hellwig #endif 243ece841abSJustin Tee if (bio_integrity(bio)) 244ece841abSJustin Tee bio_integrity_free(bio); 245a892c8d5SSatya Tangirala 246a892c8d5SSatya Tangirala bio_crypt_free_ctx(bio); 247f9c78b2bSJens Axboe } 2489ae3b3f5SJens Axboe EXPORT_SYMBOL(bio_uninit); 249f9c78b2bSJens Axboe 250f9c78b2bSJens Axboe static void bio_free(struct bio *bio) 251f9c78b2bSJens Axboe { 252f9c78b2bSJens Axboe struct bio_set *bs = bio->bi_pool; 253f9c78b2bSJens Axboe void *p; 254f9c78b2bSJens Axboe 2559ae3b3f5SJens Axboe bio_uninit(bio); 256f9c78b2bSJens Axboe 257f9c78b2bSJens Axboe if (bs) { 2588aa6ba2fSKent Overstreet bvec_free(&bs->bvec_pool, bio->bi_io_vec, BVEC_POOL_IDX(bio)); 259f9c78b2bSJens Axboe 260f9c78b2bSJens Axboe /* 261f9c78b2bSJens Axboe * If we have front padding, adjust the bio pointer before freeing 262f9c78b2bSJens Axboe */ 263f9c78b2bSJens Axboe p = bio; 264f9c78b2bSJens Axboe p -= bs->front_pad; 265f9c78b2bSJens Axboe 2668aa6ba2fSKent Overstreet mempool_free(p, &bs->bio_pool); 267f9c78b2bSJens Axboe } else { 268f9c78b2bSJens Axboe /* Bio was allocated by bio_kmalloc() */ 269f9c78b2bSJens Axboe kfree(bio); 270f9c78b2bSJens Axboe } 271f9c78b2bSJens Axboe } 272f9c78b2bSJens Axboe 2739ae3b3f5SJens Axboe /* 2749ae3b3f5SJens Axboe * Users of this function have their own bio allocation. Subsequently, 2759ae3b3f5SJens Axboe * they must remember to pair any call to bio_init() with bio_uninit() 2769ae3b3f5SJens Axboe * when IO has completed, or when the bio is released. 2779ae3b3f5SJens Axboe */ 2783a83f467SMing Lei void bio_init(struct bio *bio, struct bio_vec *table, 2793a83f467SMing Lei unsigned short max_vecs) 280f9c78b2bSJens Axboe { 281f9c78b2bSJens Axboe memset(bio, 0, sizeof(*bio)); 282c4cf5261SJens Axboe atomic_set(&bio->__bi_remaining, 1); 283dac56212SJens Axboe atomic_set(&bio->__bi_cnt, 1); 2843a83f467SMing Lei 2853a83f467SMing Lei bio->bi_io_vec = table; 2863a83f467SMing Lei bio->bi_max_vecs = max_vecs; 287f9c78b2bSJens Axboe } 288f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_init); 289f9c78b2bSJens Axboe 290f9c78b2bSJens Axboe /** 291f9c78b2bSJens Axboe * bio_reset - reinitialize a bio 292f9c78b2bSJens Axboe * @bio: bio to reset 293f9c78b2bSJens Axboe * 294f9c78b2bSJens Axboe * Description: 295f9c78b2bSJens Axboe * After calling bio_reset(), @bio will be in the same state as a freshly 296f9c78b2bSJens Axboe * allocated bio returned bio bio_alloc_bioset() - the only fields that are 297f9c78b2bSJens Axboe * preserved are the ones that are initialized by bio_alloc_bioset(). See 298f9c78b2bSJens Axboe * comment in struct bio. 299f9c78b2bSJens Axboe */ 300f9c78b2bSJens Axboe void bio_reset(struct bio *bio) 301f9c78b2bSJens Axboe { 302f9c78b2bSJens Axboe unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS); 303f9c78b2bSJens Axboe 3049ae3b3f5SJens Axboe bio_uninit(bio); 305f9c78b2bSJens Axboe 306f9c78b2bSJens Axboe memset(bio, 0, BIO_RESET_BYTES); 3074246a0b6SChristoph Hellwig bio->bi_flags = flags; 308c4cf5261SJens Axboe atomic_set(&bio->__bi_remaining, 1); 309f9c78b2bSJens Axboe } 310f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_reset); 311f9c78b2bSJens Axboe 31238f8baaeSChristoph Hellwig static struct bio *__bio_chain_endio(struct bio *bio) 313f9c78b2bSJens Axboe { 3144246a0b6SChristoph Hellwig struct bio *parent = bio->bi_private; 3154246a0b6SChristoph Hellwig 3164e4cbee9SChristoph Hellwig if (!parent->bi_status) 3174e4cbee9SChristoph Hellwig parent->bi_status = bio->bi_status; 318f9c78b2bSJens Axboe bio_put(bio); 31938f8baaeSChristoph Hellwig return parent; 32038f8baaeSChristoph Hellwig } 32138f8baaeSChristoph Hellwig 32238f8baaeSChristoph Hellwig static void bio_chain_endio(struct bio *bio) 32338f8baaeSChristoph Hellwig { 32438f8baaeSChristoph Hellwig bio_endio(__bio_chain_endio(bio)); 325f9c78b2bSJens Axboe } 326f9c78b2bSJens Axboe 327f9c78b2bSJens Axboe /** 328f9c78b2bSJens Axboe * bio_chain - chain bio completions 329f9c78b2bSJens Axboe * @bio: the target bio 330f9c78b2bSJens Axboe * @parent: the @bio's parent bio 331f9c78b2bSJens Axboe * 332f9c78b2bSJens Axboe * The caller won't have a bi_end_io called when @bio completes - instead, 333f9c78b2bSJens Axboe * @parent's bi_end_io won't be called until both @parent and @bio have 334f9c78b2bSJens Axboe * completed; the chained bio will also be freed when it completes. 335f9c78b2bSJens Axboe * 336f9c78b2bSJens Axboe * The caller must not set bi_private or bi_end_io in @bio. 337f9c78b2bSJens Axboe */ 338f9c78b2bSJens Axboe void bio_chain(struct bio *bio, struct bio *parent) 339f9c78b2bSJens Axboe { 340f9c78b2bSJens Axboe BUG_ON(bio->bi_private || bio->bi_end_io); 341f9c78b2bSJens Axboe 342f9c78b2bSJens Axboe bio->bi_private = parent; 343f9c78b2bSJens Axboe bio->bi_end_io = bio_chain_endio; 344c4cf5261SJens Axboe bio_inc_remaining(parent); 345f9c78b2bSJens Axboe } 346f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_chain); 347f9c78b2bSJens Axboe 348f9c78b2bSJens Axboe static void bio_alloc_rescue(struct work_struct *work) 349f9c78b2bSJens Axboe { 350f9c78b2bSJens Axboe struct bio_set *bs = container_of(work, struct bio_set, rescue_work); 351f9c78b2bSJens Axboe struct bio *bio; 352f9c78b2bSJens Axboe 353f9c78b2bSJens Axboe while (1) { 354f9c78b2bSJens Axboe spin_lock(&bs->rescue_lock); 355f9c78b2bSJens Axboe bio = bio_list_pop(&bs->rescue_list); 356f9c78b2bSJens Axboe spin_unlock(&bs->rescue_lock); 357f9c78b2bSJens Axboe 358f9c78b2bSJens Axboe if (!bio) 359f9c78b2bSJens Axboe break; 360f9c78b2bSJens Axboe 361f9c78b2bSJens Axboe generic_make_request(bio); 362f9c78b2bSJens Axboe } 363f9c78b2bSJens Axboe } 364f9c78b2bSJens Axboe 365f9c78b2bSJens Axboe static void punt_bios_to_rescuer(struct bio_set *bs) 366f9c78b2bSJens Axboe { 367f9c78b2bSJens Axboe struct bio_list punt, nopunt; 368f9c78b2bSJens Axboe struct bio *bio; 369f9c78b2bSJens Axboe 37047e0fb46SNeilBrown if (WARN_ON_ONCE(!bs->rescue_workqueue)) 37147e0fb46SNeilBrown return; 372f9c78b2bSJens Axboe /* 373f9c78b2bSJens Axboe * In order to guarantee forward progress we must punt only bios that 374f9c78b2bSJens Axboe * were allocated from this bio_set; otherwise, if there was a bio on 375f9c78b2bSJens Axboe * there for a stacking driver higher up in the stack, processing it 376f9c78b2bSJens Axboe * could require allocating bios from this bio_set, and doing that from 377f9c78b2bSJens Axboe * our own rescuer would be bad. 378f9c78b2bSJens Axboe * 379f9c78b2bSJens Axboe * Since bio lists are singly linked, pop them all instead of trying to 380f9c78b2bSJens Axboe * remove from the middle of the list: 381f9c78b2bSJens Axboe */ 382f9c78b2bSJens Axboe 383f9c78b2bSJens Axboe bio_list_init(&punt); 384f9c78b2bSJens Axboe bio_list_init(&nopunt); 385f9c78b2bSJens Axboe 386f5fe1b51SNeilBrown while ((bio = bio_list_pop(¤t->bio_list[0]))) 387f9c78b2bSJens Axboe bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio); 388f5fe1b51SNeilBrown current->bio_list[0] = nopunt; 389f9c78b2bSJens Axboe 390f5fe1b51SNeilBrown bio_list_init(&nopunt); 391f5fe1b51SNeilBrown while ((bio = bio_list_pop(¤t->bio_list[1]))) 392f5fe1b51SNeilBrown bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio); 393f5fe1b51SNeilBrown current->bio_list[1] = nopunt; 394f9c78b2bSJens Axboe 395f9c78b2bSJens Axboe spin_lock(&bs->rescue_lock); 396f9c78b2bSJens Axboe bio_list_merge(&bs->rescue_list, &punt); 397f9c78b2bSJens Axboe spin_unlock(&bs->rescue_lock); 398f9c78b2bSJens Axboe 399f9c78b2bSJens Axboe queue_work(bs->rescue_workqueue, &bs->rescue_work); 400f9c78b2bSJens Axboe } 401f9c78b2bSJens Axboe 402f9c78b2bSJens Axboe /** 403f9c78b2bSJens Axboe * bio_alloc_bioset - allocate a bio for I/O 404519c8e9fSRandy Dunlap * @gfp_mask: the GFP_* mask given to the slab allocator 405f9c78b2bSJens Axboe * @nr_iovecs: number of iovecs to pre-allocate 406f9c78b2bSJens Axboe * @bs: the bio_set to allocate from. 407f9c78b2bSJens Axboe * 408f9c78b2bSJens Axboe * Description: 409f9c78b2bSJens Axboe * If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is 410f9c78b2bSJens Axboe * backed by the @bs's mempool. 411f9c78b2bSJens Axboe * 412d0164adcSMel Gorman * When @bs is not NULL, if %__GFP_DIRECT_RECLAIM is set then bio_alloc will 413d0164adcSMel Gorman * always be able to allocate a bio. This is due to the mempool guarantees. 414d0164adcSMel Gorman * To make this work, callers must never allocate more than 1 bio at a time 415d0164adcSMel Gorman * from this pool. Callers that need to allocate more than 1 bio must always 416d0164adcSMel Gorman * submit the previously allocated bio for IO before attempting to allocate 417d0164adcSMel Gorman * a new one. Failure to do so can cause deadlocks under memory pressure. 418f9c78b2bSJens Axboe * 419f9c78b2bSJens Axboe * Note that when running under generic_make_request() (i.e. any block 420f9c78b2bSJens Axboe * driver), bios are not submitted until after you return - see the code in 421f9c78b2bSJens Axboe * generic_make_request() that converts recursion into iteration, to prevent 422f9c78b2bSJens Axboe * stack overflows. 423f9c78b2bSJens Axboe * 424f9c78b2bSJens Axboe * This would normally mean allocating multiple bios under 425f9c78b2bSJens Axboe * generic_make_request() would be susceptible to deadlocks, but we have 426f9c78b2bSJens Axboe * deadlock avoidance code that resubmits any blocked bios from a rescuer 427f9c78b2bSJens Axboe * thread. 428f9c78b2bSJens Axboe * 429f9c78b2bSJens Axboe * However, we do not guarantee forward progress for allocations from other 430f9c78b2bSJens Axboe * mempools. Doing multiple allocations from the same mempool under 431f9c78b2bSJens Axboe * generic_make_request() should be avoided - instead, use bio_set's front_pad 432f9c78b2bSJens Axboe * for per bio allocations. 433f9c78b2bSJens Axboe * 434f9c78b2bSJens Axboe * RETURNS: 435f9c78b2bSJens Axboe * Pointer to new bio on success, NULL on failure. 436f9c78b2bSJens Axboe */ 4377a88fa19SDan Carpenter struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs, 4387a88fa19SDan Carpenter struct bio_set *bs) 439f9c78b2bSJens Axboe { 440f9c78b2bSJens Axboe gfp_t saved_gfp = gfp_mask; 441f9c78b2bSJens Axboe unsigned front_pad; 442f9c78b2bSJens Axboe unsigned inline_vecs; 443f9c78b2bSJens Axboe struct bio_vec *bvl = NULL; 444f9c78b2bSJens Axboe struct bio *bio; 445f9c78b2bSJens Axboe void *p; 446f9c78b2bSJens Axboe 447f9c78b2bSJens Axboe if (!bs) { 448f9c78b2bSJens Axboe if (nr_iovecs > UIO_MAXIOV) 449f9c78b2bSJens Axboe return NULL; 450f9c78b2bSJens Axboe 4511f4fe21cSGustavo A. R. Silva p = kmalloc(struct_size(bio, bi_inline_vecs, nr_iovecs), gfp_mask); 452f9c78b2bSJens Axboe front_pad = 0; 453f9c78b2bSJens Axboe inline_vecs = nr_iovecs; 454f9c78b2bSJens Axboe } else { 455d8f429e1SJunichi Nomura /* should not use nobvec bioset for nr_iovecs > 0 */ 4568aa6ba2fSKent Overstreet if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && 4578aa6ba2fSKent Overstreet nr_iovecs > 0)) 458d8f429e1SJunichi Nomura return NULL; 459f9c78b2bSJens Axboe /* 460f9c78b2bSJens Axboe * generic_make_request() converts recursion to iteration; this 461f9c78b2bSJens Axboe * means if we're running beneath it, any bios we allocate and 462f9c78b2bSJens Axboe * submit will not be submitted (and thus freed) until after we 463f9c78b2bSJens Axboe * return. 464f9c78b2bSJens Axboe * 465f9c78b2bSJens Axboe * This exposes us to a potential deadlock if we allocate 466f9c78b2bSJens Axboe * multiple bios from the same bio_set() while running 467f9c78b2bSJens Axboe * underneath generic_make_request(). If we were to allocate 468f9c78b2bSJens Axboe * multiple bios (say a stacking block driver that was splitting 469f9c78b2bSJens Axboe * bios), we would deadlock if we exhausted the mempool's 470f9c78b2bSJens Axboe * reserve. 471f9c78b2bSJens Axboe * 472f9c78b2bSJens Axboe * We solve this, and guarantee forward progress, with a rescuer 473f9c78b2bSJens Axboe * workqueue per bio_set. If we go to allocate and there are 474f9c78b2bSJens Axboe * bios on current->bio_list, we first try the allocation 475d0164adcSMel Gorman * without __GFP_DIRECT_RECLAIM; if that fails, we punt those 476d0164adcSMel Gorman * bios we would be blocking to the rescuer workqueue before 477d0164adcSMel Gorman * we retry with the original gfp_flags. 478f9c78b2bSJens Axboe */ 479f9c78b2bSJens Axboe 480f5fe1b51SNeilBrown if (current->bio_list && 481f5fe1b51SNeilBrown (!bio_list_empty(¤t->bio_list[0]) || 48247e0fb46SNeilBrown !bio_list_empty(¤t->bio_list[1])) && 48347e0fb46SNeilBrown bs->rescue_workqueue) 484d0164adcSMel Gorman gfp_mask &= ~__GFP_DIRECT_RECLAIM; 485f9c78b2bSJens Axboe 4868aa6ba2fSKent Overstreet p = mempool_alloc(&bs->bio_pool, gfp_mask); 487f9c78b2bSJens Axboe if (!p && gfp_mask != saved_gfp) { 488f9c78b2bSJens Axboe punt_bios_to_rescuer(bs); 489f9c78b2bSJens Axboe gfp_mask = saved_gfp; 4908aa6ba2fSKent Overstreet p = mempool_alloc(&bs->bio_pool, gfp_mask); 491f9c78b2bSJens Axboe } 492f9c78b2bSJens Axboe 493f9c78b2bSJens Axboe front_pad = bs->front_pad; 494f9c78b2bSJens Axboe inline_vecs = BIO_INLINE_VECS; 495f9c78b2bSJens Axboe } 496f9c78b2bSJens Axboe 497f9c78b2bSJens Axboe if (unlikely(!p)) 498f9c78b2bSJens Axboe return NULL; 499f9c78b2bSJens Axboe 500f9c78b2bSJens Axboe bio = p + front_pad; 5013a83f467SMing Lei bio_init(bio, NULL, 0); 502f9c78b2bSJens Axboe 503f9c78b2bSJens Axboe if (nr_iovecs > inline_vecs) { 504ed996a52SChristoph Hellwig unsigned long idx = 0; 505ed996a52SChristoph Hellwig 5068aa6ba2fSKent Overstreet bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool); 507f9c78b2bSJens Axboe if (!bvl && gfp_mask != saved_gfp) { 508f9c78b2bSJens Axboe punt_bios_to_rescuer(bs); 509f9c78b2bSJens Axboe gfp_mask = saved_gfp; 5108aa6ba2fSKent Overstreet bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool); 511f9c78b2bSJens Axboe } 512f9c78b2bSJens Axboe 513f9c78b2bSJens Axboe if (unlikely(!bvl)) 514f9c78b2bSJens Axboe goto err_free; 515f9c78b2bSJens Axboe 516ed996a52SChristoph Hellwig bio->bi_flags |= idx << BVEC_POOL_OFFSET; 517f9c78b2bSJens Axboe } else if (nr_iovecs) { 518f9c78b2bSJens Axboe bvl = bio->bi_inline_vecs; 519f9c78b2bSJens Axboe } 520f9c78b2bSJens Axboe 521f9c78b2bSJens Axboe bio->bi_pool = bs; 522f9c78b2bSJens Axboe bio->bi_max_vecs = nr_iovecs; 523f9c78b2bSJens Axboe bio->bi_io_vec = bvl; 524f9c78b2bSJens Axboe return bio; 525f9c78b2bSJens Axboe 526f9c78b2bSJens Axboe err_free: 5278aa6ba2fSKent Overstreet mempool_free(p, &bs->bio_pool); 528f9c78b2bSJens Axboe return NULL; 529f9c78b2bSJens Axboe } 530f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_bioset); 531f9c78b2bSJens Axboe 53238a72dacSKent Overstreet void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start) 533f9c78b2bSJens Axboe { 534f9c78b2bSJens Axboe unsigned long flags; 535f9c78b2bSJens Axboe struct bio_vec bv; 536f9c78b2bSJens Axboe struct bvec_iter iter; 537f9c78b2bSJens Axboe 53838a72dacSKent Overstreet __bio_for_each_segment(bv, bio, iter, start) { 539f9c78b2bSJens Axboe char *data = bvec_kmap_irq(&bv, &flags); 540f9c78b2bSJens Axboe memset(data, 0, bv.bv_len); 541f9c78b2bSJens Axboe flush_dcache_page(bv.bv_page); 542f9c78b2bSJens Axboe bvec_kunmap_irq(data, &flags); 543f9c78b2bSJens Axboe } 544f9c78b2bSJens Axboe } 54538a72dacSKent Overstreet EXPORT_SYMBOL(zero_fill_bio_iter); 546f9c78b2bSJens Axboe 54783c9c547SMing Lei /** 54883c9c547SMing Lei * bio_truncate - truncate the bio to small size of @new_size 54983c9c547SMing Lei * @bio: the bio to be truncated 55083c9c547SMing Lei * @new_size: new size for truncating the bio 55183c9c547SMing Lei * 55283c9c547SMing Lei * Description: 55383c9c547SMing Lei * Truncate the bio to new size of @new_size. If bio_op(bio) is 55483c9c547SMing Lei * REQ_OP_READ, zero the truncated part. This function should only 55583c9c547SMing Lei * be used for handling corner cases, such as bio eod. 55683c9c547SMing Lei */ 55785a8ce62SMing Lei void bio_truncate(struct bio *bio, unsigned new_size) 55885a8ce62SMing Lei { 55985a8ce62SMing Lei struct bio_vec bv; 56085a8ce62SMing Lei struct bvec_iter iter; 56185a8ce62SMing Lei unsigned int done = 0; 56285a8ce62SMing Lei bool truncated = false; 56385a8ce62SMing Lei 56485a8ce62SMing Lei if (new_size >= bio->bi_iter.bi_size) 56585a8ce62SMing Lei return; 56685a8ce62SMing Lei 56783c9c547SMing Lei if (bio_op(bio) != REQ_OP_READ) 56885a8ce62SMing Lei goto exit; 56985a8ce62SMing Lei 57085a8ce62SMing Lei bio_for_each_segment(bv, bio, iter) { 57185a8ce62SMing Lei if (done + bv.bv_len > new_size) { 57285a8ce62SMing Lei unsigned offset; 57385a8ce62SMing Lei 57485a8ce62SMing Lei if (!truncated) 57585a8ce62SMing Lei offset = new_size - done; 57685a8ce62SMing Lei else 57785a8ce62SMing Lei offset = 0; 57885a8ce62SMing Lei zero_user(bv.bv_page, offset, bv.bv_len - offset); 57985a8ce62SMing Lei truncated = true; 58085a8ce62SMing Lei } 58185a8ce62SMing Lei done += bv.bv_len; 58285a8ce62SMing Lei } 58385a8ce62SMing Lei 58485a8ce62SMing Lei exit: 58585a8ce62SMing Lei /* 58685a8ce62SMing Lei * Don't touch bvec table here and make it really immutable, since 58785a8ce62SMing Lei * fs bio user has to retrieve all pages via bio_for_each_segment_all 58885a8ce62SMing Lei * in its .end_bio() callback. 58985a8ce62SMing Lei * 59085a8ce62SMing Lei * It is enough to truncate bio by updating .bi_size since we can make 59185a8ce62SMing Lei * correct bvec with the updated .bi_size for drivers. 59285a8ce62SMing Lei */ 59385a8ce62SMing Lei bio->bi_iter.bi_size = new_size; 59485a8ce62SMing Lei } 59585a8ce62SMing Lei 596f9c78b2bSJens Axboe /** 59729125ed6SChristoph Hellwig * guard_bio_eod - truncate a BIO to fit the block device 59829125ed6SChristoph Hellwig * @bio: bio to truncate 59929125ed6SChristoph Hellwig * 60029125ed6SChristoph Hellwig * This allows us to do IO even on the odd last sectors of a device, even if the 60129125ed6SChristoph Hellwig * block size is some multiple of the physical sector size. 60229125ed6SChristoph Hellwig * 60329125ed6SChristoph Hellwig * We'll just truncate the bio to the size of the device, and clear the end of 60429125ed6SChristoph Hellwig * the buffer head manually. Truly out-of-range accesses will turn into actual 60529125ed6SChristoph Hellwig * I/O errors, this only handles the "we need to be able to do I/O at the final 60629125ed6SChristoph Hellwig * sector" case. 60729125ed6SChristoph Hellwig */ 60829125ed6SChristoph Hellwig void guard_bio_eod(struct bio *bio) 60929125ed6SChristoph Hellwig { 61029125ed6SChristoph Hellwig sector_t maxsector; 61129125ed6SChristoph Hellwig struct hd_struct *part; 61229125ed6SChristoph Hellwig 61329125ed6SChristoph Hellwig rcu_read_lock(); 61429125ed6SChristoph Hellwig part = __disk_get_part(bio->bi_disk, bio->bi_partno); 61529125ed6SChristoph Hellwig if (part) 61629125ed6SChristoph Hellwig maxsector = part_nr_sects_read(part); 61729125ed6SChristoph Hellwig else 61829125ed6SChristoph Hellwig maxsector = get_capacity(bio->bi_disk); 61929125ed6SChristoph Hellwig rcu_read_unlock(); 62029125ed6SChristoph Hellwig 62129125ed6SChristoph Hellwig if (!maxsector) 62229125ed6SChristoph Hellwig return; 62329125ed6SChristoph Hellwig 62429125ed6SChristoph Hellwig /* 62529125ed6SChristoph Hellwig * If the *whole* IO is past the end of the device, 62629125ed6SChristoph Hellwig * let it through, and the IO layer will turn it into 62729125ed6SChristoph Hellwig * an EIO. 62829125ed6SChristoph Hellwig */ 62929125ed6SChristoph Hellwig if (unlikely(bio->bi_iter.bi_sector >= maxsector)) 63029125ed6SChristoph Hellwig return; 63129125ed6SChristoph Hellwig 63229125ed6SChristoph Hellwig maxsector -= bio->bi_iter.bi_sector; 63329125ed6SChristoph Hellwig if (likely((bio->bi_iter.bi_size >> 9) <= maxsector)) 63429125ed6SChristoph Hellwig return; 63529125ed6SChristoph Hellwig 63629125ed6SChristoph Hellwig bio_truncate(bio, maxsector << 9); 63729125ed6SChristoph Hellwig } 63829125ed6SChristoph Hellwig 63929125ed6SChristoph Hellwig /** 640f9c78b2bSJens Axboe * bio_put - release a reference to a bio 641f9c78b2bSJens Axboe * @bio: bio to release reference to 642f9c78b2bSJens Axboe * 643f9c78b2bSJens Axboe * Description: 644f9c78b2bSJens Axboe * Put a reference to a &struct bio, either one you have gotten with 6459b10f6a9SNeilBrown * bio_alloc, bio_get or bio_clone_*. The last put of a bio will free it. 646f9c78b2bSJens Axboe **/ 647f9c78b2bSJens Axboe void bio_put(struct bio *bio) 648f9c78b2bSJens Axboe { 649dac56212SJens Axboe if (!bio_flagged(bio, BIO_REFFED)) 650dac56212SJens Axboe bio_free(bio); 651dac56212SJens Axboe else { 652dac56212SJens Axboe BIO_BUG_ON(!atomic_read(&bio->__bi_cnt)); 653f9c78b2bSJens Axboe 654f9c78b2bSJens Axboe /* 655f9c78b2bSJens Axboe * last put frees it 656f9c78b2bSJens Axboe */ 657dac56212SJens Axboe if (atomic_dec_and_test(&bio->__bi_cnt)) 658f9c78b2bSJens Axboe bio_free(bio); 659f9c78b2bSJens Axboe } 660dac56212SJens Axboe } 661f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_put); 662f9c78b2bSJens Axboe 663f9c78b2bSJens Axboe /** 664f9c78b2bSJens Axboe * __bio_clone_fast - clone a bio that shares the original bio's biovec 665f9c78b2bSJens Axboe * @bio: destination bio 666f9c78b2bSJens Axboe * @bio_src: bio to clone 667f9c78b2bSJens Axboe * 668f9c78b2bSJens Axboe * Clone a &bio. Caller will own the returned bio, but not 669f9c78b2bSJens Axboe * the actual data it points to. Reference count of returned 670f9c78b2bSJens Axboe * bio will be one. 671f9c78b2bSJens Axboe * 672f9c78b2bSJens Axboe * Caller must ensure that @bio_src is not freed before @bio. 673f9c78b2bSJens Axboe */ 674f9c78b2bSJens Axboe void __bio_clone_fast(struct bio *bio, struct bio *bio_src) 675f9c78b2bSJens Axboe { 676ed996a52SChristoph Hellwig BUG_ON(bio->bi_pool && BVEC_POOL_IDX(bio)); 677f9c78b2bSJens Axboe 678f9c78b2bSJens Axboe /* 67974d46992SChristoph Hellwig * most users will be overriding ->bi_disk with a new target, 680f9c78b2bSJens Axboe * so we don't set nor calculate new physical/hw segment counts here 681f9c78b2bSJens Axboe */ 68274d46992SChristoph Hellwig bio->bi_disk = bio_src->bi_disk; 68362530ed8SMichael Lyle bio->bi_partno = bio_src->bi_partno; 684b7c44ed9SJens Axboe bio_set_flag(bio, BIO_CLONED); 685111be883SShaohua Li if (bio_flagged(bio_src, BIO_THROTTLED)) 686111be883SShaohua Li bio_set_flag(bio, BIO_THROTTLED); 6871eff9d32SJens Axboe bio->bi_opf = bio_src->bi_opf; 688ca474b73SHannes Reinecke bio->bi_ioprio = bio_src->bi_ioprio; 689cb6934f8SJens Axboe bio->bi_write_hint = bio_src->bi_write_hint; 690f9c78b2bSJens Axboe bio->bi_iter = bio_src->bi_iter; 691f9c78b2bSJens Axboe bio->bi_io_vec = bio_src->bi_io_vec; 69220bd723eSPaolo Valente 693db6638d7SDennis Zhou bio_clone_blkg_association(bio, bio_src); 694e439bedfSDennis Zhou blkcg_bio_issue_init(bio); 695f9c78b2bSJens Axboe } 696f9c78b2bSJens Axboe EXPORT_SYMBOL(__bio_clone_fast); 697f9c78b2bSJens Axboe 698f9c78b2bSJens Axboe /** 699f9c78b2bSJens Axboe * bio_clone_fast - clone a bio that shares the original bio's biovec 700f9c78b2bSJens Axboe * @bio: bio to clone 701f9c78b2bSJens Axboe * @gfp_mask: allocation priority 702f9c78b2bSJens Axboe * @bs: bio_set to allocate from 703f9c78b2bSJens Axboe * 704f9c78b2bSJens Axboe * Like __bio_clone_fast, only also allocates the returned bio 705f9c78b2bSJens Axboe */ 706f9c78b2bSJens Axboe struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs) 707f9c78b2bSJens Axboe { 708f9c78b2bSJens Axboe struct bio *b; 709f9c78b2bSJens Axboe 710f9c78b2bSJens Axboe b = bio_alloc_bioset(gfp_mask, 0, bs); 711f9c78b2bSJens Axboe if (!b) 712f9c78b2bSJens Axboe return NULL; 713f9c78b2bSJens Axboe 714f9c78b2bSJens Axboe __bio_clone_fast(b, bio); 715f9c78b2bSJens Axboe 716a892c8d5SSatya Tangirala bio_crypt_clone(b, bio, gfp_mask); 717a892c8d5SSatya Tangirala 718f9c78b2bSJens Axboe if (bio_integrity(bio)) { 719f9c78b2bSJens Axboe int ret; 720f9c78b2bSJens Axboe 721f9c78b2bSJens Axboe ret = bio_integrity_clone(b, bio, gfp_mask); 722f9c78b2bSJens Axboe 723f9c78b2bSJens Axboe if (ret < 0) { 724f9c78b2bSJens Axboe bio_put(b); 725f9c78b2bSJens Axboe return NULL; 726f9c78b2bSJens Axboe } 727f9c78b2bSJens Axboe } 728f9c78b2bSJens Axboe 729f9c78b2bSJens Axboe return b; 730f9c78b2bSJens Axboe } 731f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_clone_fast); 732f9c78b2bSJens Axboe 7335cbd28e3SChristoph Hellwig const char *bio_devname(struct bio *bio, char *buf) 7345cbd28e3SChristoph Hellwig { 7355cbd28e3SChristoph Hellwig return disk_name(bio->bi_disk, bio->bi_partno, buf); 7365cbd28e3SChristoph Hellwig } 7375cbd28e3SChristoph Hellwig EXPORT_SYMBOL(bio_devname); 7385cbd28e3SChristoph Hellwig 7395919482eSMing Lei static inline bool page_is_mergeable(const struct bio_vec *bv, 7405919482eSMing Lei struct page *page, unsigned int len, unsigned int off, 741ff896738SChristoph Hellwig bool *same_page) 7425919482eSMing Lei { 7435919482eSMing Lei phys_addr_t vec_end_addr = page_to_phys(bv->bv_page) + 7445919482eSMing Lei bv->bv_offset + bv->bv_len - 1; 7455919482eSMing Lei phys_addr_t page_addr = page_to_phys(page); 7465919482eSMing Lei 7475919482eSMing Lei if (vec_end_addr + 1 != page_addr + off) 7485919482eSMing Lei return false; 7495919482eSMing Lei if (xen_domain() && !xen_biovec_phys_mergeable(bv, page)) 7505919482eSMing Lei return false; 75152d52d1cSChristoph Hellwig 752ff896738SChristoph Hellwig *same_page = ((vec_end_addr & PAGE_MASK) == page_addr); 753ff896738SChristoph Hellwig if (!*same_page && pfn_to_page(PFN_DOWN(vec_end_addr)) + 1 != page) 7545919482eSMing Lei return false; 7555919482eSMing Lei return true; 7565919482eSMing Lei } 7575919482eSMing Lei 758e4581105SChristoph Hellwig /* 759e4581105SChristoph Hellwig * Try to merge a page into a segment, while obeying the hardware segment 760e4581105SChristoph Hellwig * size limit. This is not for normal read/write bios, but for passthrough 761e4581105SChristoph Hellwig * or Zone Append operations that we can't split. 762e4581105SChristoph Hellwig */ 763e4581105SChristoph Hellwig static bool bio_try_merge_hw_seg(struct request_queue *q, struct bio *bio, 764e4581105SChristoph Hellwig struct page *page, unsigned len, 765e4581105SChristoph Hellwig unsigned offset, bool *same_page) 766489fbbcbSMing Lei { 767384209cdSChristoph Hellwig struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1]; 768489fbbcbSMing Lei unsigned long mask = queue_segment_boundary(q); 769489fbbcbSMing Lei phys_addr_t addr1 = page_to_phys(bv->bv_page) + bv->bv_offset; 770489fbbcbSMing Lei phys_addr_t addr2 = page_to_phys(page) + offset + len - 1; 771489fbbcbSMing Lei 772489fbbcbSMing Lei if ((addr1 | mask) != (addr2 | mask)) 773489fbbcbSMing Lei return false; 774489fbbcbSMing Lei if (bv->bv_len + len > queue_max_segment_size(q)) 775489fbbcbSMing Lei return false; 776384209cdSChristoph Hellwig return __bio_try_merge_page(bio, page, len, offset, same_page); 777489fbbcbSMing Lei } 778489fbbcbSMing Lei 779f4595875SShaohua Li /** 780e4581105SChristoph Hellwig * bio_add_hw_page - attempt to add a page to a bio with hw constraints 781c66a14d0SKent Overstreet * @q: the target queue 782c66a14d0SKent Overstreet * @bio: destination bio 783c66a14d0SKent Overstreet * @page: page to add 784c66a14d0SKent Overstreet * @len: vec entry length 785c66a14d0SKent Overstreet * @offset: vec entry offset 786e4581105SChristoph Hellwig * @max_sectors: maximum number of sectors that can be added 787e4581105SChristoph Hellwig * @same_page: return if the segment has been merged inside the same page 788f9c78b2bSJens Axboe * 789e4581105SChristoph Hellwig * Add a page to a bio while respecting the hardware max_sectors, max_segment 790e4581105SChristoph Hellwig * and gap limitations. 791f9c78b2bSJens Axboe */ 792e4581105SChristoph Hellwig int bio_add_hw_page(struct request_queue *q, struct bio *bio, 79319047087SMing Lei struct page *page, unsigned int len, unsigned int offset, 794e4581105SChristoph Hellwig unsigned int max_sectors, bool *same_page) 795f9c78b2bSJens Axboe { 796f9c78b2bSJens Axboe struct bio_vec *bvec; 797f9c78b2bSJens Axboe 798e4581105SChristoph Hellwig if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED))) 799f9c78b2bSJens Axboe return 0; 800f9c78b2bSJens Axboe 801e4581105SChristoph Hellwig if (((bio->bi_iter.bi_size + len) >> 9) > max_sectors) 802f9c78b2bSJens Axboe return 0; 803f9c78b2bSJens Axboe 804f9c78b2bSJens Axboe if (bio->bi_vcnt > 0) { 805e4581105SChristoph Hellwig if (bio_try_merge_hw_seg(q, bio, page, len, offset, same_page)) 806384209cdSChristoph Hellwig return len; 807320ea869SChristoph Hellwig 808320ea869SChristoph Hellwig /* 809320ea869SChristoph Hellwig * If the queue doesn't support SG gaps and adding this segment 810320ea869SChristoph Hellwig * would create a gap, disallow it. 811320ea869SChristoph Hellwig */ 812384209cdSChristoph Hellwig bvec = &bio->bi_io_vec[bio->bi_vcnt - 1]; 813320ea869SChristoph Hellwig if (bvec_gap_to_prev(q, bvec, offset)) 814320ea869SChristoph Hellwig return 0; 815f9c78b2bSJens Axboe } 816f9c78b2bSJens Axboe 81779d08f89SMing Lei if (bio_full(bio, len)) 818f9c78b2bSJens Axboe return 0; 819f9c78b2bSJens Axboe 82014ccb66bSChristoph Hellwig if (bio->bi_vcnt >= queue_max_segments(q)) 821489fbbcbSMing Lei return 0; 822489fbbcbSMing Lei 823f9c78b2bSJens Axboe bvec = &bio->bi_io_vec[bio->bi_vcnt]; 824f9c78b2bSJens Axboe bvec->bv_page = page; 825f9c78b2bSJens Axboe bvec->bv_len = len; 826f9c78b2bSJens Axboe bvec->bv_offset = offset; 827fcbf6a08SMaurizio Lombardi bio->bi_vcnt++; 828dcdca753SChristoph Hellwig bio->bi_iter.bi_size += len; 829f9c78b2bSJens Axboe return len; 830f9c78b2bSJens Axboe } 83119047087SMing Lei 832e4581105SChristoph Hellwig /** 833e4581105SChristoph Hellwig * bio_add_pc_page - attempt to add page to passthrough bio 834e4581105SChristoph Hellwig * @q: the target queue 835e4581105SChristoph Hellwig * @bio: destination bio 836e4581105SChristoph Hellwig * @page: page to add 837e4581105SChristoph Hellwig * @len: vec entry length 838e4581105SChristoph Hellwig * @offset: vec entry offset 839e4581105SChristoph Hellwig * 840e4581105SChristoph Hellwig * Attempt to add a page to the bio_vec maplist. This can fail for a 841e4581105SChristoph Hellwig * number of reasons, such as the bio being full or target block device 842e4581105SChristoph Hellwig * limitations. The target block device must allow bio's up to PAGE_SIZE, 843e4581105SChristoph Hellwig * so it is always possible to add a single page to an empty bio. 844e4581105SChristoph Hellwig * 845e4581105SChristoph Hellwig * This should only be used by passthrough bios. 846e4581105SChristoph Hellwig */ 84719047087SMing Lei int bio_add_pc_page(struct request_queue *q, struct bio *bio, 84819047087SMing Lei struct page *page, unsigned int len, unsigned int offset) 84919047087SMing Lei { 850d1916c86SChristoph Hellwig bool same_page = false; 851e4581105SChristoph Hellwig return bio_add_hw_page(q, bio, page, len, offset, 852e4581105SChristoph Hellwig queue_max_hw_sectors(q), &same_page); 85319047087SMing Lei } 854f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_pc_page); 855f9c78b2bSJens Axboe 856f9c78b2bSJens Axboe /** 8570aa69fd3SChristoph Hellwig * __bio_try_merge_page - try appending data to an existing bvec. 8580aa69fd3SChristoph Hellwig * @bio: destination bio 859551879a4SMing Lei * @page: start page to add 8600aa69fd3SChristoph Hellwig * @len: length of the data to add 861551879a4SMing Lei * @off: offset of the data relative to @page 862ff896738SChristoph Hellwig * @same_page: return if the segment has been merged inside the same page 8630aa69fd3SChristoph Hellwig * 8640aa69fd3SChristoph Hellwig * Try to add the data at @page + @off to the last bvec of @bio. This is a 8650aa69fd3SChristoph Hellwig * a useful optimisation for file systems with a block size smaller than the 8660aa69fd3SChristoph Hellwig * page size. 8670aa69fd3SChristoph Hellwig * 868551879a4SMing Lei * Warn if (@len, @off) crosses pages in case that @same_page is true. 869551879a4SMing Lei * 8700aa69fd3SChristoph Hellwig * Return %true on success or %false on failure. 8710aa69fd3SChristoph Hellwig */ 8720aa69fd3SChristoph Hellwig bool __bio_try_merge_page(struct bio *bio, struct page *page, 873ff896738SChristoph Hellwig unsigned int len, unsigned int off, bool *same_page) 8740aa69fd3SChristoph Hellwig { 8750aa69fd3SChristoph Hellwig if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED))) 8760aa69fd3SChristoph Hellwig return false; 8770aa69fd3SChristoph Hellwig 878cc90bc68SAndreas Gruenbacher if (bio->bi_vcnt > 0) { 8790aa69fd3SChristoph Hellwig struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1]; 8800aa69fd3SChristoph Hellwig 8815919482eSMing Lei if (page_is_mergeable(bv, page, len, off, same_page)) { 882cc90bc68SAndreas Gruenbacher if (bio->bi_iter.bi_size > UINT_MAX - len) 883cc90bc68SAndreas Gruenbacher return false; 8840aa69fd3SChristoph Hellwig bv->bv_len += len; 8850aa69fd3SChristoph Hellwig bio->bi_iter.bi_size += len; 8860aa69fd3SChristoph Hellwig return true; 8870aa69fd3SChristoph Hellwig } 8885919482eSMing Lei } 8890aa69fd3SChristoph Hellwig return false; 8900aa69fd3SChristoph Hellwig } 8910aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_try_merge_page); 8920aa69fd3SChristoph Hellwig 8930aa69fd3SChristoph Hellwig /** 894551879a4SMing Lei * __bio_add_page - add page(s) to a bio in a new segment 8950aa69fd3SChristoph Hellwig * @bio: destination bio 896551879a4SMing Lei * @page: start page to add 897551879a4SMing Lei * @len: length of the data to add, may cross pages 898551879a4SMing Lei * @off: offset of the data relative to @page, may cross pages 8990aa69fd3SChristoph Hellwig * 9000aa69fd3SChristoph Hellwig * Add the data at @page + @off to @bio as a new bvec. The caller must ensure 9010aa69fd3SChristoph Hellwig * that @bio has space for another bvec. 9020aa69fd3SChristoph Hellwig */ 9030aa69fd3SChristoph Hellwig void __bio_add_page(struct bio *bio, struct page *page, 9040aa69fd3SChristoph Hellwig unsigned int len, unsigned int off) 9050aa69fd3SChristoph Hellwig { 9060aa69fd3SChristoph Hellwig struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt]; 9070aa69fd3SChristoph Hellwig 9080aa69fd3SChristoph Hellwig WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)); 90979d08f89SMing Lei WARN_ON_ONCE(bio_full(bio, len)); 9100aa69fd3SChristoph Hellwig 9110aa69fd3SChristoph Hellwig bv->bv_page = page; 9120aa69fd3SChristoph Hellwig bv->bv_offset = off; 9130aa69fd3SChristoph Hellwig bv->bv_len = len; 9140aa69fd3SChristoph Hellwig 9150aa69fd3SChristoph Hellwig bio->bi_iter.bi_size += len; 9160aa69fd3SChristoph Hellwig bio->bi_vcnt++; 917b8e24a93SJohannes Weiner 918b8e24a93SJohannes Weiner if (!bio_flagged(bio, BIO_WORKINGSET) && unlikely(PageWorkingset(page))) 919b8e24a93SJohannes Weiner bio_set_flag(bio, BIO_WORKINGSET); 9200aa69fd3SChristoph Hellwig } 9210aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_add_page); 9220aa69fd3SChristoph Hellwig 9230aa69fd3SChristoph Hellwig /** 924551879a4SMing Lei * bio_add_page - attempt to add page(s) to bio 925f9c78b2bSJens Axboe * @bio: destination bio 926551879a4SMing Lei * @page: start page to add 927551879a4SMing Lei * @len: vec entry length, may cross pages 928551879a4SMing Lei * @offset: vec entry offset relative to @page, may cross pages 929f9c78b2bSJens Axboe * 930551879a4SMing Lei * Attempt to add page(s) to the bio_vec maplist. This will only fail 931c66a14d0SKent Overstreet * if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio. 932f9c78b2bSJens Axboe */ 933c66a14d0SKent Overstreet int bio_add_page(struct bio *bio, struct page *page, 934c66a14d0SKent Overstreet unsigned int len, unsigned int offset) 935f9c78b2bSJens Axboe { 936ff896738SChristoph Hellwig bool same_page = false; 937ff896738SChristoph Hellwig 938ff896738SChristoph Hellwig if (!__bio_try_merge_page(bio, page, len, offset, &same_page)) { 93979d08f89SMing Lei if (bio_full(bio, len)) 940c66a14d0SKent Overstreet return 0; 9410aa69fd3SChristoph Hellwig __bio_add_page(bio, page, len, offset); 942c66a14d0SKent Overstreet } 943c66a14d0SKent Overstreet return len; 944f9c78b2bSJens Axboe } 945f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_page); 946f9c78b2bSJens Axboe 947d241a95fSChristoph Hellwig void bio_release_pages(struct bio *bio, bool mark_dirty) 9487321ecbfSChristoph Hellwig { 9497321ecbfSChristoph Hellwig struct bvec_iter_all iter_all; 9507321ecbfSChristoph Hellwig struct bio_vec *bvec; 9517321ecbfSChristoph Hellwig 952b2d0d991SChristoph Hellwig if (bio_flagged(bio, BIO_NO_PAGE_REF)) 953b2d0d991SChristoph Hellwig return; 954b2d0d991SChristoph Hellwig 955d241a95fSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 956d241a95fSChristoph Hellwig if (mark_dirty && !PageCompound(bvec->bv_page)) 957d241a95fSChristoph Hellwig set_page_dirty_lock(bvec->bv_page); 9587321ecbfSChristoph Hellwig put_page(bvec->bv_page); 9597321ecbfSChristoph Hellwig } 960d241a95fSChristoph Hellwig } 96129b2a3aaSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_release_pages); 9627321ecbfSChristoph Hellwig 9636d0c48aeSJens Axboe static int __bio_iov_bvec_add_pages(struct bio *bio, struct iov_iter *iter) 9646d0c48aeSJens Axboe { 9656d0c48aeSJens Axboe const struct bio_vec *bv = iter->bvec; 9666d0c48aeSJens Axboe unsigned int len; 9676d0c48aeSJens Axboe size_t size; 9686d0c48aeSJens Axboe 9696d0c48aeSJens Axboe if (WARN_ON_ONCE(iter->iov_offset > bv->bv_len)) 9706d0c48aeSJens Axboe return -EINVAL; 9716d0c48aeSJens Axboe 9726d0c48aeSJens Axboe len = min_t(size_t, bv->bv_len - iter->iov_offset, iter->count); 9736d0c48aeSJens Axboe size = bio_add_page(bio, bv->bv_page, len, 9746d0c48aeSJens Axboe bv->bv_offset + iter->iov_offset); 975a10584c3SChristoph Hellwig if (unlikely(size != len)) 976a10584c3SChristoph Hellwig return -EINVAL; 9776d0c48aeSJens Axboe iov_iter_advance(iter, size); 9786d0c48aeSJens Axboe return 0; 9796d0c48aeSJens Axboe } 9806d0c48aeSJens Axboe 981576ed913SChristoph Hellwig #define PAGE_PTRS_PER_BVEC (sizeof(struct bio_vec) / sizeof(struct page *)) 982576ed913SChristoph Hellwig 9832cefe4dbSKent Overstreet /** 98417d51b10SMartin Wilck * __bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio 9852cefe4dbSKent Overstreet * @bio: bio to add pages to 9862cefe4dbSKent Overstreet * @iter: iov iterator describing the region to be mapped 9872cefe4dbSKent Overstreet * 98817d51b10SMartin Wilck * Pins pages from *iter and appends them to @bio's bvec array. The 9892cefe4dbSKent Overstreet * pages will have to be released using put_page() when done. 99017d51b10SMartin Wilck * For multi-segment *iter, this function only adds pages from the 99117d51b10SMartin Wilck * the next non-empty segment of the iov iterator. 9922cefe4dbSKent Overstreet */ 99317d51b10SMartin Wilck static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) 9942cefe4dbSKent Overstreet { 995576ed913SChristoph Hellwig unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt; 996576ed913SChristoph Hellwig unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt; 9972cefe4dbSKent Overstreet struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt; 9982cefe4dbSKent Overstreet struct page **pages = (struct page **)bv; 99945691804SChristoph Hellwig bool same_page = false; 1000576ed913SChristoph Hellwig ssize_t size, left; 1001576ed913SChristoph Hellwig unsigned len, i; 1002b403ea24SMartin Wilck size_t offset; 1003576ed913SChristoph Hellwig 1004576ed913SChristoph Hellwig /* 1005576ed913SChristoph Hellwig * Move page array up in the allocated memory for the bio vecs as far as 1006576ed913SChristoph Hellwig * possible so that we can start filling biovecs from the beginning 1007576ed913SChristoph Hellwig * without overwriting the temporary page array. 1008576ed913SChristoph Hellwig */ 1009576ed913SChristoph Hellwig BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2); 1010576ed913SChristoph Hellwig pages += entries_left * (PAGE_PTRS_PER_BVEC - 1); 10112cefe4dbSKent Overstreet 10122cefe4dbSKent Overstreet size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset); 10132cefe4dbSKent Overstreet if (unlikely(size <= 0)) 10142cefe4dbSKent Overstreet return size ? size : -EFAULT; 10152cefe4dbSKent Overstreet 1016576ed913SChristoph Hellwig for (left = size, i = 0; left > 0; left -= len, i++) { 1017576ed913SChristoph Hellwig struct page *page = pages[i]; 10182cefe4dbSKent Overstreet 1019576ed913SChristoph Hellwig len = min_t(size_t, PAGE_SIZE - offset, left); 102045691804SChristoph Hellwig 102145691804SChristoph Hellwig if (__bio_try_merge_page(bio, page, len, offset, &same_page)) { 102245691804SChristoph Hellwig if (same_page) 102345691804SChristoph Hellwig put_page(page); 102445691804SChristoph Hellwig } else { 102579d08f89SMing Lei if (WARN_ON_ONCE(bio_full(bio, len))) 1026576ed913SChristoph Hellwig return -EINVAL; 102745691804SChristoph Hellwig __bio_add_page(bio, page, len, offset); 102845691804SChristoph Hellwig } 1029576ed913SChristoph Hellwig offset = 0; 10302cefe4dbSKent Overstreet } 10312cefe4dbSKent Overstreet 10322cefe4dbSKent Overstreet iov_iter_advance(iter, size); 10332cefe4dbSKent Overstreet return 0; 10342cefe4dbSKent Overstreet } 103517d51b10SMartin Wilck 10360512a75bSKeith Busch static int __bio_iov_append_get_pages(struct bio *bio, struct iov_iter *iter) 10370512a75bSKeith Busch { 10380512a75bSKeith Busch unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt; 10390512a75bSKeith Busch unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt; 10400512a75bSKeith Busch struct request_queue *q = bio->bi_disk->queue; 10410512a75bSKeith Busch unsigned int max_append_sectors = queue_max_zone_append_sectors(q); 10420512a75bSKeith Busch struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt; 10430512a75bSKeith Busch struct page **pages = (struct page **)bv; 10440512a75bSKeith Busch ssize_t size, left; 10450512a75bSKeith Busch unsigned len, i; 10460512a75bSKeith Busch size_t offset; 10470512a75bSKeith Busch 10480512a75bSKeith Busch if (WARN_ON_ONCE(!max_append_sectors)) 10490512a75bSKeith Busch return 0; 10500512a75bSKeith Busch 10510512a75bSKeith Busch /* 10520512a75bSKeith Busch * Move page array up in the allocated memory for the bio vecs as far as 10530512a75bSKeith Busch * possible so that we can start filling biovecs from the beginning 10540512a75bSKeith Busch * without overwriting the temporary page array. 10550512a75bSKeith Busch */ 10560512a75bSKeith Busch BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2); 10570512a75bSKeith Busch pages += entries_left * (PAGE_PTRS_PER_BVEC - 1); 10580512a75bSKeith Busch 10590512a75bSKeith Busch size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset); 10600512a75bSKeith Busch if (unlikely(size <= 0)) 10610512a75bSKeith Busch return size ? size : -EFAULT; 10620512a75bSKeith Busch 10630512a75bSKeith Busch for (left = size, i = 0; left > 0; left -= len, i++) { 10640512a75bSKeith Busch struct page *page = pages[i]; 10650512a75bSKeith Busch bool same_page = false; 10660512a75bSKeith Busch 10670512a75bSKeith Busch len = min_t(size_t, PAGE_SIZE - offset, left); 10680512a75bSKeith Busch if (bio_add_hw_page(q, bio, page, len, offset, 10690512a75bSKeith Busch max_append_sectors, &same_page) != len) 10700512a75bSKeith Busch return -EINVAL; 10710512a75bSKeith Busch if (same_page) 10720512a75bSKeith Busch put_page(page); 10730512a75bSKeith Busch offset = 0; 10740512a75bSKeith Busch } 10750512a75bSKeith Busch 10760512a75bSKeith Busch iov_iter_advance(iter, size); 10770512a75bSKeith Busch return 0; 10780512a75bSKeith Busch } 10790512a75bSKeith Busch 108017d51b10SMartin Wilck /** 10816d0c48aeSJens Axboe * bio_iov_iter_get_pages - add user or kernel pages to a bio 108217d51b10SMartin Wilck * @bio: bio to add pages to 10836d0c48aeSJens Axboe * @iter: iov iterator describing the region to be added 108417d51b10SMartin Wilck * 10856d0c48aeSJens Axboe * This takes either an iterator pointing to user memory, or one pointing to 10866d0c48aeSJens Axboe * kernel pages (BVEC iterator). If we're adding user pages, we pin them and 10876d0c48aeSJens Axboe * map them into the kernel. On IO completion, the caller should put those 1088399254aaSJens Axboe * pages. If we're adding kernel pages, and the caller told us it's safe to 1089399254aaSJens Axboe * do so, we just have to add the pages to the bio directly. We don't grab an 1090399254aaSJens Axboe * extra reference to those pages (the user should already have that), and we 1091399254aaSJens Axboe * don't put the page on IO completion. The caller needs to check if the bio is 1092399254aaSJens Axboe * flagged BIO_NO_PAGE_REF on IO completion. If it isn't, then pages should be 1093399254aaSJens Axboe * released. 10946d0c48aeSJens Axboe * 109517d51b10SMartin Wilck * The function tries, but does not guarantee, to pin as many pages as 10966d0c48aeSJens Axboe * fit into the bio, or are requested in *iter, whatever is smaller. If 10976d0c48aeSJens Axboe * MM encounters an error pinning the requested pages, it stops. Error 10986d0c48aeSJens Axboe * is returned only if 0 pages could be pinned. 109917d51b10SMartin Wilck */ 110017d51b10SMartin Wilck int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) 110117d51b10SMartin Wilck { 11026d0c48aeSJens Axboe const bool is_bvec = iov_iter_is_bvec(iter); 110314eacf12SChristoph Hellwig int ret; 110414eacf12SChristoph Hellwig 110514eacf12SChristoph Hellwig if (WARN_ON_ONCE(bio->bi_vcnt)) 110614eacf12SChristoph Hellwig return -EINVAL; 110717d51b10SMartin Wilck 110817d51b10SMartin Wilck do { 11090512a75bSKeith Busch if (bio_op(bio) == REQ_OP_ZONE_APPEND) { 11100512a75bSKeith Busch if (WARN_ON_ONCE(is_bvec)) 11110512a75bSKeith Busch return -EINVAL; 11120512a75bSKeith Busch ret = __bio_iov_append_get_pages(bio, iter); 11130512a75bSKeith Busch } else { 11146d0c48aeSJens Axboe if (is_bvec) 11156d0c48aeSJens Axboe ret = __bio_iov_bvec_add_pages(bio, iter); 11166d0c48aeSJens Axboe else 11176d0c48aeSJens Axboe ret = __bio_iov_iter_get_pages(bio, iter); 11180512a75bSKeith Busch } 111979d08f89SMing Lei } while (!ret && iov_iter_count(iter) && !bio_full(bio, 0)); 112017d51b10SMartin Wilck 1121b6207430SChristoph Hellwig if (is_bvec) 11227321ecbfSChristoph Hellwig bio_set_flag(bio, BIO_NO_PAGE_REF); 112314eacf12SChristoph Hellwig return bio->bi_vcnt ? 0 : ret; 112417d51b10SMartin Wilck } 112529b2a3aaSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages); 11262cefe4dbSKent Overstreet 11274246a0b6SChristoph Hellwig static void submit_bio_wait_endio(struct bio *bio) 1128f9c78b2bSJens Axboe { 112965e53aabSChristoph Hellwig complete(bio->bi_private); 1130f9c78b2bSJens Axboe } 1131f9c78b2bSJens Axboe 1132f9c78b2bSJens Axboe /** 1133f9c78b2bSJens Axboe * submit_bio_wait - submit a bio, and wait until it completes 1134f9c78b2bSJens Axboe * @bio: The &struct bio which describes the I/O 1135f9c78b2bSJens Axboe * 1136f9c78b2bSJens Axboe * Simple wrapper around submit_bio(). Returns 0 on success, or the error from 1137f9c78b2bSJens Axboe * bio_endio() on failure. 11383d289d68SJan Kara * 11393d289d68SJan Kara * WARNING: Unlike to how submit_bio() is usually used, this function does not 11403d289d68SJan Kara * result in bio reference to be consumed. The caller must drop the reference 11413d289d68SJan Kara * on his own. 1142f9c78b2bSJens Axboe */ 11434e49ea4aSMike Christie int submit_bio_wait(struct bio *bio) 1144f9c78b2bSJens Axboe { 1145e319e1fbSByungchul Park DECLARE_COMPLETION_ONSTACK_MAP(done, bio->bi_disk->lockdep_map); 1146de6a78b6SMing Lei unsigned long hang_check; 1147f9c78b2bSJens Axboe 114865e53aabSChristoph Hellwig bio->bi_private = &done; 1149f9c78b2bSJens Axboe bio->bi_end_io = submit_bio_wait_endio; 11501eff9d32SJens Axboe bio->bi_opf |= REQ_SYNC; 11514e49ea4aSMike Christie submit_bio(bio); 1152de6a78b6SMing Lei 1153de6a78b6SMing Lei /* Prevent hang_check timer from firing at us during very long I/O */ 1154de6a78b6SMing Lei hang_check = sysctl_hung_task_timeout_secs; 1155de6a78b6SMing Lei if (hang_check) 1156de6a78b6SMing Lei while (!wait_for_completion_io_timeout(&done, 1157de6a78b6SMing Lei hang_check * (HZ/2))) 1158de6a78b6SMing Lei ; 1159de6a78b6SMing Lei else 116065e53aabSChristoph Hellwig wait_for_completion_io(&done); 1161f9c78b2bSJens Axboe 116265e53aabSChristoph Hellwig return blk_status_to_errno(bio->bi_status); 1163f9c78b2bSJens Axboe } 1164f9c78b2bSJens Axboe EXPORT_SYMBOL(submit_bio_wait); 1165f9c78b2bSJens Axboe 1166f9c78b2bSJens Axboe /** 1167f9c78b2bSJens Axboe * bio_advance - increment/complete a bio by some number of bytes 1168f9c78b2bSJens Axboe * @bio: bio to advance 1169f9c78b2bSJens Axboe * @bytes: number of bytes to complete 1170f9c78b2bSJens Axboe * 1171f9c78b2bSJens Axboe * This updates bi_sector, bi_size and bi_idx; if the number of bytes to 1172f9c78b2bSJens Axboe * complete doesn't align with a bvec boundary, then bv_len and bv_offset will 1173f9c78b2bSJens Axboe * be updated on the last bvec as well. 1174f9c78b2bSJens Axboe * 1175f9c78b2bSJens Axboe * @bio will then represent the remaining, uncompleted portion of the io. 1176f9c78b2bSJens Axboe */ 1177f9c78b2bSJens Axboe void bio_advance(struct bio *bio, unsigned bytes) 1178f9c78b2bSJens Axboe { 1179f9c78b2bSJens Axboe if (bio_integrity(bio)) 1180f9c78b2bSJens Axboe bio_integrity_advance(bio, bytes); 1181f9c78b2bSJens Axboe 1182a892c8d5SSatya Tangirala bio_crypt_advance(bio, bytes); 1183f9c78b2bSJens Axboe bio_advance_iter(bio, &bio->bi_iter, bytes); 1184f9c78b2bSJens Axboe } 1185f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_advance); 1186f9c78b2bSJens Axboe 118745db54d5SKent Overstreet void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter, 118845db54d5SKent Overstreet struct bio *src, struct bvec_iter *src_iter) 1189f9c78b2bSJens Axboe { 1190f9c78b2bSJens Axboe struct bio_vec src_bv, dst_bv; 1191f9c78b2bSJens Axboe void *src_p, *dst_p; 1192f9c78b2bSJens Axboe unsigned bytes; 1193f9c78b2bSJens Axboe 119445db54d5SKent Overstreet while (src_iter->bi_size && dst_iter->bi_size) { 119545db54d5SKent Overstreet src_bv = bio_iter_iovec(src, *src_iter); 119645db54d5SKent Overstreet dst_bv = bio_iter_iovec(dst, *dst_iter); 119745db54d5SKent Overstreet 119845db54d5SKent Overstreet bytes = min(src_bv.bv_len, dst_bv.bv_len); 119945db54d5SKent Overstreet 120045db54d5SKent Overstreet src_p = kmap_atomic(src_bv.bv_page); 120145db54d5SKent Overstreet dst_p = kmap_atomic(dst_bv.bv_page); 120245db54d5SKent Overstreet 120345db54d5SKent Overstreet memcpy(dst_p + dst_bv.bv_offset, 120445db54d5SKent Overstreet src_p + src_bv.bv_offset, 120545db54d5SKent Overstreet bytes); 120645db54d5SKent Overstreet 120745db54d5SKent Overstreet kunmap_atomic(dst_p); 120845db54d5SKent Overstreet kunmap_atomic(src_p); 120945db54d5SKent Overstreet 12106e6e811dSKent Overstreet flush_dcache_page(dst_bv.bv_page); 12116e6e811dSKent Overstreet 121245db54d5SKent Overstreet bio_advance_iter(src, src_iter, bytes); 121345db54d5SKent Overstreet bio_advance_iter(dst, dst_iter, bytes); 121445db54d5SKent Overstreet } 121545db54d5SKent Overstreet } 121645db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data_iter); 121745db54d5SKent Overstreet 121845db54d5SKent Overstreet /** 121945db54d5SKent Overstreet * bio_copy_data - copy contents of data buffers from one bio to another 122045db54d5SKent Overstreet * @src: source bio 122145db54d5SKent Overstreet * @dst: destination bio 122245db54d5SKent Overstreet * 122345db54d5SKent Overstreet * Stops when it reaches the end of either @src or @dst - that is, copies 122445db54d5SKent Overstreet * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios). 122545db54d5SKent Overstreet */ 122645db54d5SKent Overstreet void bio_copy_data(struct bio *dst, struct bio *src) 122745db54d5SKent Overstreet { 122845db54d5SKent Overstreet struct bvec_iter src_iter = src->bi_iter; 122945db54d5SKent Overstreet struct bvec_iter dst_iter = dst->bi_iter; 123045db54d5SKent Overstreet 123145db54d5SKent Overstreet bio_copy_data_iter(dst, &dst_iter, src, &src_iter); 123245db54d5SKent Overstreet } 123345db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data); 123445db54d5SKent Overstreet 123545db54d5SKent Overstreet /** 123645db54d5SKent Overstreet * bio_list_copy_data - copy contents of data buffers from one chain of bios to 123745db54d5SKent Overstreet * another 123845db54d5SKent Overstreet * @src: source bio list 123945db54d5SKent Overstreet * @dst: destination bio list 124045db54d5SKent Overstreet * 124145db54d5SKent Overstreet * Stops when it reaches the end of either the @src list or @dst list - that is, 124245db54d5SKent Overstreet * copies min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of 124345db54d5SKent Overstreet * bios). 124445db54d5SKent Overstreet */ 124545db54d5SKent Overstreet void bio_list_copy_data(struct bio *dst, struct bio *src) 124645db54d5SKent Overstreet { 124745db54d5SKent Overstreet struct bvec_iter src_iter = src->bi_iter; 124845db54d5SKent Overstreet struct bvec_iter dst_iter = dst->bi_iter; 124945db54d5SKent Overstreet 1250f9c78b2bSJens Axboe while (1) { 1251f9c78b2bSJens Axboe if (!src_iter.bi_size) { 1252f9c78b2bSJens Axboe src = src->bi_next; 1253f9c78b2bSJens Axboe if (!src) 1254f9c78b2bSJens Axboe break; 1255f9c78b2bSJens Axboe 1256f9c78b2bSJens Axboe src_iter = src->bi_iter; 1257f9c78b2bSJens Axboe } 1258f9c78b2bSJens Axboe 1259f9c78b2bSJens Axboe if (!dst_iter.bi_size) { 1260f9c78b2bSJens Axboe dst = dst->bi_next; 1261f9c78b2bSJens Axboe if (!dst) 1262f9c78b2bSJens Axboe break; 1263f9c78b2bSJens Axboe 1264f9c78b2bSJens Axboe dst_iter = dst->bi_iter; 1265f9c78b2bSJens Axboe } 1266f9c78b2bSJens Axboe 126745db54d5SKent Overstreet bio_copy_data_iter(dst, &dst_iter, src, &src_iter); 1268f9c78b2bSJens Axboe } 1269f9c78b2bSJens Axboe } 127045db54d5SKent Overstreet EXPORT_SYMBOL(bio_list_copy_data); 1271f9c78b2bSJens Axboe 1272491221f8SGuoqing Jiang void bio_free_pages(struct bio *bio) 12731dfa0f68SChristoph Hellwig { 12741dfa0f68SChristoph Hellwig struct bio_vec *bvec; 12756dc4f100SMing Lei struct bvec_iter_all iter_all; 12761dfa0f68SChristoph Hellwig 12772b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) 12781dfa0f68SChristoph Hellwig __free_page(bvec->bv_page); 12791dfa0f68SChristoph Hellwig } 1280491221f8SGuoqing Jiang EXPORT_SYMBOL(bio_free_pages); 12811dfa0f68SChristoph Hellwig 1282f9c78b2bSJens Axboe /* 1283f9c78b2bSJens Axboe * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions 1284f9c78b2bSJens Axboe * for performing direct-IO in BIOs. 1285f9c78b2bSJens Axboe * 1286f9c78b2bSJens Axboe * The problem is that we cannot run set_page_dirty() from interrupt context 1287f9c78b2bSJens Axboe * because the required locks are not interrupt-safe. So what we can do is to 1288f9c78b2bSJens Axboe * mark the pages dirty _before_ performing IO. And in interrupt context, 1289f9c78b2bSJens Axboe * check that the pages are still dirty. If so, fine. If not, redirty them 1290f9c78b2bSJens Axboe * in process context. 1291f9c78b2bSJens Axboe * 1292f9c78b2bSJens Axboe * We special-case compound pages here: normally this means reads into hugetlb 1293f9c78b2bSJens Axboe * pages. The logic in here doesn't really work right for compound pages 1294f9c78b2bSJens Axboe * because the VM does not uniformly chase down the head page in all cases. 1295f9c78b2bSJens Axboe * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't 1296f9c78b2bSJens Axboe * handle them at all. So we skip compound pages here at an early stage. 1297f9c78b2bSJens Axboe * 1298f9c78b2bSJens Axboe * Note that this code is very hard to test under normal circumstances because 1299f9c78b2bSJens Axboe * direct-io pins the pages with get_user_pages(). This makes 1300f9c78b2bSJens Axboe * is_page_cache_freeable return false, and the VM will not clean the pages. 1301f9c78b2bSJens Axboe * But other code (eg, flusher threads) could clean the pages if they are mapped 1302f9c78b2bSJens Axboe * pagecache. 1303f9c78b2bSJens Axboe * 1304f9c78b2bSJens Axboe * Simply disabling the call to bio_set_pages_dirty() is a good way to test the 1305f9c78b2bSJens Axboe * deferred bio dirtying paths. 1306f9c78b2bSJens Axboe */ 1307f9c78b2bSJens Axboe 1308f9c78b2bSJens Axboe /* 1309f9c78b2bSJens Axboe * bio_set_pages_dirty() will mark all the bio's pages as dirty. 1310f9c78b2bSJens Axboe */ 1311f9c78b2bSJens Axboe void bio_set_pages_dirty(struct bio *bio) 1312f9c78b2bSJens Axboe { 1313f9c78b2bSJens Axboe struct bio_vec *bvec; 13146dc4f100SMing Lei struct bvec_iter_all iter_all; 1315f9c78b2bSJens Axboe 13162b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 13173bb50983SChristoph Hellwig if (!PageCompound(bvec->bv_page)) 13183bb50983SChristoph Hellwig set_page_dirty_lock(bvec->bv_page); 1319f9c78b2bSJens Axboe } 1320f9c78b2bSJens Axboe } 1321f9c78b2bSJens Axboe 1322f9c78b2bSJens Axboe /* 1323f9c78b2bSJens Axboe * bio_check_pages_dirty() will check that all the BIO's pages are still dirty. 1324f9c78b2bSJens Axboe * If they are, then fine. If, however, some pages are clean then they must 1325f9c78b2bSJens Axboe * have been written out during the direct-IO read. So we take another ref on 132624d5493fSChristoph Hellwig * the BIO and re-dirty the pages in process context. 1327f9c78b2bSJens Axboe * 1328f9c78b2bSJens Axboe * It is expected that bio_check_pages_dirty() will wholly own the BIO from 1329ea1754a0SKirill A. Shutemov * here on. It will run one put_page() against each page and will run one 1330ea1754a0SKirill A. Shutemov * bio_put() against the BIO. 1331f9c78b2bSJens Axboe */ 1332f9c78b2bSJens Axboe 1333f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work); 1334f9c78b2bSJens Axboe 1335f9c78b2bSJens Axboe static DECLARE_WORK(bio_dirty_work, bio_dirty_fn); 1336f9c78b2bSJens Axboe static DEFINE_SPINLOCK(bio_dirty_lock); 1337f9c78b2bSJens Axboe static struct bio *bio_dirty_list; 1338f9c78b2bSJens Axboe 1339f9c78b2bSJens Axboe /* 1340f9c78b2bSJens Axboe * This runs in process context 1341f9c78b2bSJens Axboe */ 1342f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work) 1343f9c78b2bSJens Axboe { 134424d5493fSChristoph Hellwig struct bio *bio, *next; 1345f9c78b2bSJens Axboe 134624d5493fSChristoph Hellwig spin_lock_irq(&bio_dirty_lock); 134724d5493fSChristoph Hellwig next = bio_dirty_list; 1348f9c78b2bSJens Axboe bio_dirty_list = NULL; 134924d5493fSChristoph Hellwig spin_unlock_irq(&bio_dirty_lock); 1350f9c78b2bSJens Axboe 135124d5493fSChristoph Hellwig while ((bio = next) != NULL) { 135224d5493fSChristoph Hellwig next = bio->bi_private; 1353f9c78b2bSJens Axboe 1354d241a95fSChristoph Hellwig bio_release_pages(bio, true); 1355f9c78b2bSJens Axboe bio_put(bio); 1356f9c78b2bSJens Axboe } 1357f9c78b2bSJens Axboe } 1358f9c78b2bSJens Axboe 1359f9c78b2bSJens Axboe void bio_check_pages_dirty(struct bio *bio) 1360f9c78b2bSJens Axboe { 1361f9c78b2bSJens Axboe struct bio_vec *bvec; 136224d5493fSChristoph Hellwig unsigned long flags; 13636dc4f100SMing Lei struct bvec_iter_all iter_all; 1364f9c78b2bSJens Axboe 13652b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 136624d5493fSChristoph Hellwig if (!PageDirty(bvec->bv_page) && !PageCompound(bvec->bv_page)) 136724d5493fSChristoph Hellwig goto defer; 1368f9c78b2bSJens Axboe } 1369f9c78b2bSJens Axboe 1370d241a95fSChristoph Hellwig bio_release_pages(bio, false); 137124d5493fSChristoph Hellwig bio_put(bio); 137224d5493fSChristoph Hellwig return; 137324d5493fSChristoph Hellwig defer: 1374f9c78b2bSJens Axboe spin_lock_irqsave(&bio_dirty_lock, flags); 1375f9c78b2bSJens Axboe bio->bi_private = bio_dirty_list; 1376f9c78b2bSJens Axboe bio_dirty_list = bio; 1377f9c78b2bSJens Axboe spin_unlock_irqrestore(&bio_dirty_lock, flags); 1378f9c78b2bSJens Axboe schedule_work(&bio_dirty_work); 1379f9c78b2bSJens Axboe } 1380f9c78b2bSJens Axboe 1381c4cf5261SJens Axboe static inline bool bio_remaining_done(struct bio *bio) 1382c4cf5261SJens Axboe { 1383c4cf5261SJens Axboe /* 1384c4cf5261SJens Axboe * If we're not chaining, then ->__bi_remaining is always 1 and 1385c4cf5261SJens Axboe * we always end io on the first invocation. 1386c4cf5261SJens Axboe */ 1387c4cf5261SJens Axboe if (!bio_flagged(bio, BIO_CHAIN)) 1388c4cf5261SJens Axboe return true; 1389c4cf5261SJens Axboe 1390c4cf5261SJens Axboe BUG_ON(atomic_read(&bio->__bi_remaining) <= 0); 1391c4cf5261SJens Axboe 1392326e1dbbSMike Snitzer if (atomic_dec_and_test(&bio->__bi_remaining)) { 1393b7c44ed9SJens Axboe bio_clear_flag(bio, BIO_CHAIN); 1394c4cf5261SJens Axboe return true; 1395326e1dbbSMike Snitzer } 1396c4cf5261SJens Axboe 1397c4cf5261SJens Axboe return false; 1398c4cf5261SJens Axboe } 1399c4cf5261SJens Axboe 1400f9c78b2bSJens Axboe /** 1401f9c78b2bSJens Axboe * bio_endio - end I/O on a bio 1402f9c78b2bSJens Axboe * @bio: bio 1403f9c78b2bSJens Axboe * 1404f9c78b2bSJens Axboe * Description: 14054246a0b6SChristoph Hellwig * bio_endio() will end I/O on the whole bio. bio_endio() is the preferred 14064246a0b6SChristoph Hellwig * way to end I/O on a bio. No one should call bi_end_io() directly on a 14074246a0b6SChristoph Hellwig * bio unless they own it and thus know that it has an end_io function. 1408fbbaf700SNeilBrown * 1409fbbaf700SNeilBrown * bio_endio() can be called several times on a bio that has been chained 1410fbbaf700SNeilBrown * using bio_chain(). The ->bi_end_io() function will only be called the 1411fbbaf700SNeilBrown * last time. At this point the BLK_TA_COMPLETE tracing event will be 1412fbbaf700SNeilBrown * generated if BIO_TRACE_COMPLETION is set. 1413f9c78b2bSJens Axboe **/ 14144246a0b6SChristoph Hellwig void bio_endio(struct bio *bio) 1415f9c78b2bSJens Axboe { 1416ba8c6967SChristoph Hellwig again: 14172b885517SChristoph Hellwig if (!bio_remaining_done(bio)) 1418ba8c6967SChristoph Hellwig return; 14197c20f116SChristoph Hellwig if (!bio_integrity_endio(bio)) 14207c20f116SChristoph Hellwig return; 1421f9c78b2bSJens Axboe 142267b42d0bSJosef Bacik if (bio->bi_disk) 142367b42d0bSJosef Bacik rq_qos_done_bio(bio->bi_disk->queue, bio); 142467b42d0bSJosef Bacik 1425f9c78b2bSJens Axboe /* 1426ba8c6967SChristoph Hellwig * Need to have a real endio function for chained bios, otherwise 1427ba8c6967SChristoph Hellwig * various corner cases will break (like stacking block devices that 1428ba8c6967SChristoph Hellwig * save/restore bi_end_io) - however, we want to avoid unbounded 1429ba8c6967SChristoph Hellwig * recursion and blowing the stack. Tail call optimization would 1430ba8c6967SChristoph Hellwig * handle this, but compiling with frame pointers also disables 1431ba8c6967SChristoph Hellwig * gcc's sibling call optimization. 1432f9c78b2bSJens Axboe */ 1433f9c78b2bSJens Axboe if (bio->bi_end_io == bio_chain_endio) { 143438f8baaeSChristoph Hellwig bio = __bio_chain_endio(bio); 1435ba8c6967SChristoph Hellwig goto again; 1436ba8c6967SChristoph Hellwig } 1437ba8c6967SChristoph Hellwig 143874d46992SChristoph Hellwig if (bio->bi_disk && bio_flagged(bio, BIO_TRACE_COMPLETION)) { 1439d24de76aSChristoph Hellwig trace_block_bio_complete(bio->bi_disk->queue, bio); 1440fbbaf700SNeilBrown bio_clear_flag(bio, BIO_TRACE_COMPLETION); 1441fbbaf700SNeilBrown } 1442fbbaf700SNeilBrown 14439e234eeaSShaohua Li blk_throtl_bio_endio(bio); 1444b222dd2fSShaohua Li /* release cgroup info */ 1445b222dd2fSShaohua Li bio_uninit(bio); 1446f9c78b2bSJens Axboe if (bio->bi_end_io) 14474246a0b6SChristoph Hellwig bio->bi_end_io(bio); 1448f9c78b2bSJens Axboe } 1449f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_endio); 1450f9c78b2bSJens Axboe 1451f9c78b2bSJens Axboe /** 1452f9c78b2bSJens Axboe * bio_split - split a bio 1453f9c78b2bSJens Axboe * @bio: bio to split 1454f9c78b2bSJens Axboe * @sectors: number of sectors to split from the front of @bio 1455f9c78b2bSJens Axboe * @gfp: gfp mask 1456f9c78b2bSJens Axboe * @bs: bio set to allocate from 1457f9c78b2bSJens Axboe * 1458f9c78b2bSJens Axboe * Allocates and returns a new bio which represents @sectors from the start of 1459f9c78b2bSJens Axboe * @bio, and updates @bio to represent the remaining sectors. 1460f9c78b2bSJens Axboe * 1461f3f5da62SMartin K. Petersen * Unless this is a discard request the newly allocated bio will point 1462dad77584SBart Van Assche * to @bio's bi_io_vec. It is the caller's responsibility to ensure that 1463dad77584SBart Van Assche * neither @bio nor @bs are freed before the split bio. 1464f9c78b2bSJens Axboe */ 1465f9c78b2bSJens Axboe struct bio *bio_split(struct bio *bio, int sectors, 1466f9c78b2bSJens Axboe gfp_t gfp, struct bio_set *bs) 1467f9c78b2bSJens Axboe { 1468f341a4d3SMikulas Patocka struct bio *split; 1469f9c78b2bSJens Axboe 1470f9c78b2bSJens Axboe BUG_ON(sectors <= 0); 1471f9c78b2bSJens Axboe BUG_ON(sectors >= bio_sectors(bio)); 1472f9c78b2bSJens Axboe 14730512a75bSKeith Busch /* Zone append commands cannot be split */ 14740512a75bSKeith Busch if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND)) 14750512a75bSKeith Busch return NULL; 14760512a75bSKeith Busch 1477f9c78b2bSJens Axboe split = bio_clone_fast(bio, gfp, bs); 1478f9c78b2bSJens Axboe if (!split) 1479f9c78b2bSJens Axboe return NULL; 1480f9c78b2bSJens Axboe 1481f9c78b2bSJens Axboe split->bi_iter.bi_size = sectors << 9; 1482f9c78b2bSJens Axboe 1483f9c78b2bSJens Axboe if (bio_integrity(split)) 1484fbd08e76SDmitry Monakhov bio_integrity_trim(split); 1485f9c78b2bSJens Axboe 1486f9c78b2bSJens Axboe bio_advance(bio, split->bi_iter.bi_size); 1487f9c78b2bSJens Axboe 1488fbbaf700SNeilBrown if (bio_flagged(bio, BIO_TRACE_COMPLETION)) 148920d59023SGoldwyn Rodrigues bio_set_flag(split, BIO_TRACE_COMPLETION); 1490fbbaf700SNeilBrown 1491f9c78b2bSJens Axboe return split; 1492f9c78b2bSJens Axboe } 1493f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_split); 1494f9c78b2bSJens Axboe 1495f9c78b2bSJens Axboe /** 1496f9c78b2bSJens Axboe * bio_trim - trim a bio 1497f9c78b2bSJens Axboe * @bio: bio to trim 1498f9c78b2bSJens Axboe * @offset: number of sectors to trim from the front of @bio 1499f9c78b2bSJens Axboe * @size: size we want to trim @bio to, in sectors 1500f9c78b2bSJens Axboe */ 1501f9c78b2bSJens Axboe void bio_trim(struct bio *bio, int offset, int size) 1502f9c78b2bSJens Axboe { 1503f9c78b2bSJens Axboe /* 'bio' is a cloned bio which we need to trim to match 1504f9c78b2bSJens Axboe * the given offset and size. 1505f9c78b2bSJens Axboe */ 1506f9c78b2bSJens Axboe 1507f9c78b2bSJens Axboe size <<= 9; 1508f9c78b2bSJens Axboe if (offset == 0 && size == bio->bi_iter.bi_size) 1509f9c78b2bSJens Axboe return; 1510f9c78b2bSJens Axboe 1511f9c78b2bSJens Axboe bio_advance(bio, offset << 9); 1512f9c78b2bSJens Axboe bio->bi_iter.bi_size = size; 1513376a78abSDmitry Monakhov 1514376a78abSDmitry Monakhov if (bio_integrity(bio)) 1515fbd08e76SDmitry Monakhov bio_integrity_trim(bio); 1516376a78abSDmitry Monakhov 1517f9c78b2bSJens Axboe } 1518f9c78b2bSJens Axboe EXPORT_SYMBOL_GPL(bio_trim); 1519f9c78b2bSJens Axboe 1520f9c78b2bSJens Axboe /* 1521f9c78b2bSJens Axboe * create memory pools for biovec's in a bio_set. 1522f9c78b2bSJens Axboe * use the global biovec slabs created for general use. 1523f9c78b2bSJens Axboe */ 15248aa6ba2fSKent Overstreet int biovec_init_pool(mempool_t *pool, int pool_entries) 1525f9c78b2bSJens Axboe { 1526ed996a52SChristoph Hellwig struct biovec_slab *bp = bvec_slabs + BVEC_POOL_MAX; 1527f9c78b2bSJens Axboe 15288aa6ba2fSKent Overstreet return mempool_init_slab_pool(pool, pool_entries, bp->slab); 1529f9c78b2bSJens Axboe } 1530f9c78b2bSJens Axboe 1531917a38c7SKent Overstreet /* 1532917a38c7SKent Overstreet * bioset_exit - exit a bioset initialized with bioset_init() 1533917a38c7SKent Overstreet * 1534917a38c7SKent Overstreet * May be called on a zeroed but uninitialized bioset (i.e. allocated with 1535917a38c7SKent Overstreet * kzalloc()). 1536917a38c7SKent Overstreet */ 1537917a38c7SKent Overstreet void bioset_exit(struct bio_set *bs) 1538f9c78b2bSJens Axboe { 1539f9c78b2bSJens Axboe if (bs->rescue_workqueue) 1540f9c78b2bSJens Axboe destroy_workqueue(bs->rescue_workqueue); 1541917a38c7SKent Overstreet bs->rescue_workqueue = NULL; 1542f9c78b2bSJens Axboe 15438aa6ba2fSKent Overstreet mempool_exit(&bs->bio_pool); 15448aa6ba2fSKent Overstreet mempool_exit(&bs->bvec_pool); 1545f9c78b2bSJens Axboe 1546f9c78b2bSJens Axboe bioset_integrity_free(bs); 1547917a38c7SKent Overstreet if (bs->bio_slab) 1548f9c78b2bSJens Axboe bio_put_slab(bs); 1549917a38c7SKent Overstreet bs->bio_slab = NULL; 1550917a38c7SKent Overstreet } 1551917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_exit); 1552f9c78b2bSJens Axboe 1553011067b0SNeilBrown /** 1554917a38c7SKent Overstreet * bioset_init - Initialize a bio_set 1555dad08527SKent Overstreet * @bs: pool to initialize 1556917a38c7SKent Overstreet * @pool_size: Number of bio and bio_vecs to cache in the mempool 1557917a38c7SKent Overstreet * @front_pad: Number of bytes to allocate in front of the returned bio 1558917a38c7SKent Overstreet * @flags: Flags to modify behavior, currently %BIOSET_NEED_BVECS 1559917a38c7SKent Overstreet * and %BIOSET_NEED_RESCUER 1560917a38c7SKent Overstreet * 1561dad08527SKent Overstreet * Description: 1562dad08527SKent Overstreet * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller 1563dad08527SKent Overstreet * to ask for a number of bytes to be allocated in front of the bio. 1564dad08527SKent Overstreet * Front pad allocation is useful for embedding the bio inside 1565dad08527SKent Overstreet * another structure, to avoid allocating extra data to go with the bio. 1566dad08527SKent Overstreet * Note that the bio must be embedded at the END of that structure always, 1567dad08527SKent Overstreet * or things will break badly. 1568dad08527SKent Overstreet * If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated 1569dad08527SKent Overstreet * for allocating iovecs. This pool is not needed e.g. for bio_clone_fast(). 1570dad08527SKent Overstreet * If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used to 1571dad08527SKent Overstreet * dispatch queued requests when the mempool runs out of space. 1572dad08527SKent Overstreet * 1573917a38c7SKent Overstreet */ 1574917a38c7SKent Overstreet int bioset_init(struct bio_set *bs, 1575917a38c7SKent Overstreet unsigned int pool_size, 1576917a38c7SKent Overstreet unsigned int front_pad, 1577917a38c7SKent Overstreet int flags) 1578917a38c7SKent Overstreet { 1579917a38c7SKent Overstreet unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec); 1580917a38c7SKent Overstreet 1581917a38c7SKent Overstreet bs->front_pad = front_pad; 1582917a38c7SKent Overstreet 1583917a38c7SKent Overstreet spin_lock_init(&bs->rescue_lock); 1584917a38c7SKent Overstreet bio_list_init(&bs->rescue_list); 1585917a38c7SKent Overstreet INIT_WORK(&bs->rescue_work, bio_alloc_rescue); 1586917a38c7SKent Overstreet 1587917a38c7SKent Overstreet bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad); 1588917a38c7SKent Overstreet if (!bs->bio_slab) 1589917a38c7SKent Overstreet return -ENOMEM; 1590917a38c7SKent Overstreet 1591917a38c7SKent Overstreet if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab)) 1592917a38c7SKent Overstreet goto bad; 1593917a38c7SKent Overstreet 1594917a38c7SKent Overstreet if ((flags & BIOSET_NEED_BVECS) && 1595917a38c7SKent Overstreet biovec_init_pool(&bs->bvec_pool, pool_size)) 1596917a38c7SKent Overstreet goto bad; 1597917a38c7SKent Overstreet 1598917a38c7SKent Overstreet if (!(flags & BIOSET_NEED_RESCUER)) 1599917a38c7SKent Overstreet return 0; 1600917a38c7SKent Overstreet 1601917a38c7SKent Overstreet bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0); 1602917a38c7SKent Overstreet if (!bs->rescue_workqueue) 1603917a38c7SKent Overstreet goto bad; 1604917a38c7SKent Overstreet 1605917a38c7SKent Overstreet return 0; 1606917a38c7SKent Overstreet bad: 1607917a38c7SKent Overstreet bioset_exit(bs); 1608917a38c7SKent Overstreet return -ENOMEM; 1609917a38c7SKent Overstreet } 1610917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_init); 1611917a38c7SKent Overstreet 161228e89fd9SJens Axboe /* 161328e89fd9SJens Axboe * Initialize and setup a new bio_set, based on the settings from 161428e89fd9SJens Axboe * another bio_set. 161528e89fd9SJens Axboe */ 161628e89fd9SJens Axboe int bioset_init_from_src(struct bio_set *bs, struct bio_set *src) 161728e89fd9SJens Axboe { 161828e89fd9SJens Axboe int flags; 161928e89fd9SJens Axboe 162028e89fd9SJens Axboe flags = 0; 162128e89fd9SJens Axboe if (src->bvec_pool.min_nr) 162228e89fd9SJens Axboe flags |= BIOSET_NEED_BVECS; 162328e89fd9SJens Axboe if (src->rescue_workqueue) 162428e89fd9SJens Axboe flags |= BIOSET_NEED_RESCUER; 162528e89fd9SJens Axboe 162628e89fd9SJens Axboe return bioset_init(bs, src->bio_pool.min_nr, src->front_pad, flags); 162728e89fd9SJens Axboe } 162828e89fd9SJens Axboe EXPORT_SYMBOL(bioset_init_from_src); 162928e89fd9SJens Axboe 1630f9c78b2bSJens Axboe #ifdef CONFIG_BLK_CGROUP 16311d933cf0STejun Heo 16321d933cf0STejun Heo /** 16332268c0feSDennis Zhou * __bio_associate_blkg - associate a bio with the a blkg 163408e18eabSJosef Bacik * @bio: target bio 163508e18eabSJosef Bacik * @blkg: the blkg to associate 163608e18eabSJosef Bacik * 1637beea9da0SDennis Zhou * This tries to associate @bio with the specified @blkg. Association failure 1638beea9da0SDennis Zhou * is handled by walking up the blkg tree. Therefore, the blkg associated can 1639beea9da0SDennis Zhou * be anything between @blkg and the root_blkg. This situation only happens 1640beea9da0SDennis Zhou * when a cgroup is dying and then the remaining bios will spill to the closest 1641beea9da0SDennis Zhou * alive blkg. 1642beea9da0SDennis Zhou * 1643beea9da0SDennis Zhou * A reference will be taken on the @blkg and will be released when @bio is 1644beea9da0SDennis Zhou * freed. 164508e18eabSJosef Bacik */ 16462268c0feSDennis Zhou static void __bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg) 164708e18eabSJosef Bacik { 1648db9819c7SChristoph Hellwig if (bio->bi_blkg) 1649db9819c7SChristoph Hellwig blkg_put(bio->bi_blkg); 16507754f669SDennis Zhou bio->bi_blkg = blkg_tryget_closest(blkg); 16512268c0feSDennis Zhou } 16522268c0feSDennis Zhou 1653fd42df30SDennis Zhou /** 1654fd42df30SDennis Zhou * bio_associate_blkg_from_css - associate a bio with a specified css 1655fd42df30SDennis Zhou * @bio: target bio 1656fd42df30SDennis Zhou * @css: target css 1657fd42df30SDennis Zhou * 1658fd42df30SDennis Zhou * Associate @bio with the blkg found by combining the css's blkg and the 1659fc5a828bSDennis Zhou * request_queue of the @bio. This falls back to the queue's root_blkg if 1660fc5a828bSDennis Zhou * the association fails with the css. 1661fd42df30SDennis Zhou */ 1662fd42df30SDennis Zhou void bio_associate_blkg_from_css(struct bio *bio, 1663fd42df30SDennis Zhou struct cgroup_subsys_state *css) 1664fd42df30SDennis Zhou { 1665fc5a828bSDennis Zhou struct request_queue *q = bio->bi_disk->queue; 1666fc5a828bSDennis Zhou struct blkcg_gq *blkg; 1667fc5a828bSDennis Zhou 1668fc5a828bSDennis Zhou rcu_read_lock(); 1669fc5a828bSDennis Zhou 1670fc5a828bSDennis Zhou if (!css || !css->parent) 1671fc5a828bSDennis Zhou blkg = q->root_blkg; 1672fc5a828bSDennis Zhou else 1673fc5a828bSDennis Zhou blkg = blkg_lookup_create(css_to_blkcg(css), q); 1674fc5a828bSDennis Zhou 1675fc5a828bSDennis Zhou __bio_associate_blkg(bio, blkg); 1676fc5a828bSDennis Zhou 1677fc5a828bSDennis Zhou rcu_read_unlock(); 1678fd42df30SDennis Zhou } 1679fd42df30SDennis Zhou EXPORT_SYMBOL_GPL(bio_associate_blkg_from_css); 1680fd42df30SDennis Zhou 16816a7f6d86SDennis Zhou #ifdef CONFIG_MEMCG 16826a7f6d86SDennis Zhou /** 16836a7f6d86SDennis Zhou * bio_associate_blkg_from_page - associate a bio with the page's blkg 16846a7f6d86SDennis Zhou * @bio: target bio 16856a7f6d86SDennis Zhou * @page: the page to lookup the blkcg from 16866a7f6d86SDennis Zhou * 16876a7f6d86SDennis Zhou * Associate @bio with the blkg from @page's owning memcg and the respective 1688fc5a828bSDennis Zhou * request_queue. If cgroup_e_css returns %NULL, fall back to the queue's 1689fc5a828bSDennis Zhou * root_blkg. 16906a7f6d86SDennis Zhou */ 16916a7f6d86SDennis Zhou void bio_associate_blkg_from_page(struct bio *bio, struct page *page) 16926a7f6d86SDennis Zhou { 16936a7f6d86SDennis Zhou struct cgroup_subsys_state *css; 16946a7f6d86SDennis Zhou 16956a7f6d86SDennis Zhou if (!page->mem_cgroup) 16966a7f6d86SDennis Zhou return; 16976a7f6d86SDennis Zhou 1698fc5a828bSDennis Zhou rcu_read_lock(); 1699fc5a828bSDennis Zhou 1700fc5a828bSDennis Zhou css = cgroup_e_css(page->mem_cgroup->css.cgroup, &io_cgrp_subsys); 1701fc5a828bSDennis Zhou bio_associate_blkg_from_css(bio, css); 1702fc5a828bSDennis Zhou 1703fc5a828bSDennis Zhou rcu_read_unlock(); 17046a7f6d86SDennis Zhou } 17056a7f6d86SDennis Zhou #endif /* CONFIG_MEMCG */ 17066a7f6d86SDennis Zhou 17072268c0feSDennis Zhou /** 17082268c0feSDennis Zhou * bio_associate_blkg - associate a bio with a blkg 17092268c0feSDennis Zhou * @bio: target bio 17102268c0feSDennis Zhou * 17112268c0feSDennis Zhou * Associate @bio with the blkg found from the bio's css and request_queue. 17122268c0feSDennis Zhou * If one is not found, bio_lookup_blkg() creates the blkg. If a blkg is 17132268c0feSDennis Zhou * already associated, the css is reused and association redone as the 17142268c0feSDennis Zhou * request_queue may have changed. 17152268c0feSDennis Zhou */ 17162268c0feSDennis Zhou void bio_associate_blkg(struct bio *bio) 17172268c0feSDennis Zhou { 1718fc5a828bSDennis Zhou struct cgroup_subsys_state *css; 17192268c0feSDennis Zhou 17202268c0feSDennis Zhou rcu_read_lock(); 17212268c0feSDennis Zhou 1722db6638d7SDennis Zhou if (bio->bi_blkg) 1723fc5a828bSDennis Zhou css = &bio_blkcg(bio)->css; 1724db6638d7SDennis Zhou else 1725fc5a828bSDennis Zhou css = blkcg_css(); 17262268c0feSDennis Zhou 1727fc5a828bSDennis Zhou bio_associate_blkg_from_css(bio, css); 17282268c0feSDennis Zhou 17292268c0feSDennis Zhou rcu_read_unlock(); 173008e18eabSJosef Bacik } 17315cdf2e3fSDennis Zhou EXPORT_SYMBOL_GPL(bio_associate_blkg); 173208e18eabSJosef Bacik 1733f0fcb3ecSDennis Zhou (Facebook) /** 1734db6638d7SDennis Zhou * bio_clone_blkg_association - clone blkg association from src to dst bio 173520bd723eSPaolo Valente * @dst: destination bio 173620bd723eSPaolo Valente * @src: source bio 173720bd723eSPaolo Valente */ 1738db6638d7SDennis Zhou void bio_clone_blkg_association(struct bio *dst, struct bio *src) 173920bd723eSPaolo Valente { 17406ab21879SDennis Zhou rcu_read_lock(); 17416ab21879SDennis Zhou 1742fc5a828bSDennis Zhou if (src->bi_blkg) 17432268c0feSDennis Zhou __bio_associate_blkg(dst, src->bi_blkg); 17446ab21879SDennis Zhou 17456ab21879SDennis Zhou rcu_read_unlock(); 174620bd723eSPaolo Valente } 1747db6638d7SDennis Zhou EXPORT_SYMBOL_GPL(bio_clone_blkg_association); 1748f9c78b2bSJens Axboe #endif /* CONFIG_BLK_CGROUP */ 1749f9c78b2bSJens Axboe 1750f9c78b2bSJens Axboe static void __init biovec_init_slabs(void) 1751f9c78b2bSJens Axboe { 1752f9c78b2bSJens Axboe int i; 1753f9c78b2bSJens Axboe 1754ed996a52SChristoph Hellwig for (i = 0; i < BVEC_POOL_NR; i++) { 1755f9c78b2bSJens Axboe int size; 1756f9c78b2bSJens Axboe struct biovec_slab *bvs = bvec_slabs + i; 1757f9c78b2bSJens Axboe 1758f9c78b2bSJens Axboe if (bvs->nr_vecs <= BIO_INLINE_VECS) { 1759f9c78b2bSJens Axboe bvs->slab = NULL; 1760f9c78b2bSJens Axboe continue; 1761f9c78b2bSJens Axboe } 1762f9c78b2bSJens Axboe 1763f9c78b2bSJens Axboe size = bvs->nr_vecs * sizeof(struct bio_vec); 1764f9c78b2bSJens Axboe bvs->slab = kmem_cache_create(bvs->name, size, 0, 1765f9c78b2bSJens Axboe SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); 1766f9c78b2bSJens Axboe } 1767f9c78b2bSJens Axboe } 1768f9c78b2bSJens Axboe 1769f9c78b2bSJens Axboe static int __init init_bio(void) 1770f9c78b2bSJens Axboe { 1771f9c78b2bSJens Axboe bio_slab_max = 2; 1772f9c78b2bSJens Axboe bio_slab_nr = 0; 17736396bb22SKees Cook bio_slabs = kcalloc(bio_slab_max, sizeof(struct bio_slab), 17746396bb22SKees Cook GFP_KERNEL); 17752b24e6f6SJohannes Thumshirn 17762b24e6f6SJohannes Thumshirn BUILD_BUG_ON(BIO_FLAG_LAST > BVEC_POOL_OFFSET); 17772b24e6f6SJohannes Thumshirn 1778f9c78b2bSJens Axboe if (!bio_slabs) 1779f9c78b2bSJens Axboe panic("bio: can't allocate bios\n"); 1780f9c78b2bSJens Axboe 1781f9c78b2bSJens Axboe bio_integrity_init(); 1782f9c78b2bSJens Axboe biovec_init_slabs(); 1783f9c78b2bSJens Axboe 1784f4f8154aSKent Overstreet if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS)) 1785f9c78b2bSJens Axboe panic("bio: can't allocate bios\n"); 1786f9c78b2bSJens Axboe 1787f4f8154aSKent Overstreet if (bioset_integrity_create(&fs_bio_set, BIO_POOL_SIZE)) 1788f9c78b2bSJens Axboe panic("bio: can't create integrity pool\n"); 1789f9c78b2bSJens Axboe 1790f9c78b2bSJens Axboe return 0; 1791f9c78b2bSJens Axboe } 1792f9c78b2bSJens Axboe subsys_initcall(init_bio); 1793