xref: /openbmc/linux/block/bio.c (revision 9e8c0d0d)
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>
2249d1ec85SMing Lei #include <linux/xarray.h>
23f9c78b2bSJens Axboe 
24f9c78b2bSJens Axboe #include <trace/events/block.h>
259e234eeaSShaohua Li #include "blk.h"
2667b42d0bSJosef Bacik #include "blk-rq-qos.h"
27f9c78b2bSJens Axboe 
28be4d234dSJens Axboe struct bio_alloc_cache {
29be4d234dSJens Axboe 	struct bio_list		free_list;
30be4d234dSJens Axboe 	unsigned int		nr;
31be4d234dSJens Axboe };
32be4d234dSJens Axboe 
33de76fd89SChristoph Hellwig static struct biovec_slab {
346ac0b715SChristoph Hellwig 	int nr_vecs;
356ac0b715SChristoph Hellwig 	char *name;
366ac0b715SChristoph Hellwig 	struct kmem_cache *slab;
37de76fd89SChristoph Hellwig } bvec_slabs[] __read_mostly = {
38de76fd89SChristoph Hellwig 	{ .nr_vecs = 16, .name = "biovec-16" },
39de76fd89SChristoph Hellwig 	{ .nr_vecs = 64, .name = "biovec-64" },
40de76fd89SChristoph Hellwig 	{ .nr_vecs = 128, .name = "biovec-128" },
41a8affc03SChristoph Hellwig 	{ .nr_vecs = BIO_MAX_VECS, .name = "biovec-max" },
42f9c78b2bSJens Axboe };
436ac0b715SChristoph Hellwig 
447a800a20SChristoph Hellwig static struct biovec_slab *biovec_slab(unsigned short nr_vecs)
457a800a20SChristoph Hellwig {
467a800a20SChristoph Hellwig 	switch (nr_vecs) {
477a800a20SChristoph Hellwig 	/* smaller bios use inline vecs */
487a800a20SChristoph Hellwig 	case 5 ... 16:
497a800a20SChristoph Hellwig 		return &bvec_slabs[0];
507a800a20SChristoph Hellwig 	case 17 ... 64:
517a800a20SChristoph Hellwig 		return &bvec_slabs[1];
527a800a20SChristoph Hellwig 	case 65 ... 128:
537a800a20SChristoph Hellwig 		return &bvec_slabs[2];
54a8affc03SChristoph Hellwig 	case 129 ... BIO_MAX_VECS:
557a800a20SChristoph Hellwig 		return &bvec_slabs[3];
567a800a20SChristoph Hellwig 	default:
577a800a20SChristoph Hellwig 		BUG();
587a800a20SChristoph Hellwig 		return NULL;
597a800a20SChristoph Hellwig 	}
607a800a20SChristoph Hellwig }
61f9c78b2bSJens Axboe 
62f9c78b2bSJens Axboe /*
63f9c78b2bSJens Axboe  * fs_bio_set is the bio_set containing bio and iovec memory pools used by
64f9c78b2bSJens Axboe  * IO code that does not need private memory pools.
65f9c78b2bSJens Axboe  */
66f4f8154aSKent Overstreet struct bio_set fs_bio_set;
67f9c78b2bSJens Axboe EXPORT_SYMBOL(fs_bio_set);
68f9c78b2bSJens Axboe 
69f9c78b2bSJens Axboe /*
70f9c78b2bSJens Axboe  * Our slab pool management
71f9c78b2bSJens Axboe  */
72f9c78b2bSJens Axboe struct bio_slab {
73f9c78b2bSJens Axboe 	struct kmem_cache *slab;
74f9c78b2bSJens Axboe 	unsigned int slab_ref;
75f9c78b2bSJens Axboe 	unsigned int slab_size;
76f9c78b2bSJens Axboe 	char name[8];
77f9c78b2bSJens Axboe };
78f9c78b2bSJens Axboe static DEFINE_MUTEX(bio_slab_lock);
7949d1ec85SMing Lei static DEFINE_XARRAY(bio_slabs);
80f9c78b2bSJens Axboe 
8149d1ec85SMing Lei static struct bio_slab *create_bio_slab(unsigned int size)
82f9c78b2bSJens Axboe {
8349d1ec85SMing Lei 	struct bio_slab *bslab = kzalloc(sizeof(*bslab), GFP_KERNEL);
8449d1ec85SMing Lei 
8549d1ec85SMing Lei 	if (!bslab)
8649d1ec85SMing Lei 		return NULL;
8749d1ec85SMing Lei 
8849d1ec85SMing Lei 	snprintf(bslab->name, sizeof(bslab->name), "bio-%d", size);
8949d1ec85SMing Lei 	bslab->slab = kmem_cache_create(bslab->name, size,
9049d1ec85SMing Lei 			ARCH_KMALLOC_MINALIGN, SLAB_HWCACHE_ALIGN, NULL);
9149d1ec85SMing Lei 	if (!bslab->slab)
9249d1ec85SMing Lei 		goto fail_alloc_slab;
9349d1ec85SMing Lei 
9449d1ec85SMing Lei 	bslab->slab_ref = 1;
9549d1ec85SMing Lei 	bslab->slab_size = size;
9649d1ec85SMing Lei 
9749d1ec85SMing Lei 	if (!xa_err(xa_store(&bio_slabs, size, bslab, GFP_KERNEL)))
9849d1ec85SMing Lei 		return bslab;
9949d1ec85SMing Lei 
10049d1ec85SMing Lei 	kmem_cache_destroy(bslab->slab);
10149d1ec85SMing Lei 
10249d1ec85SMing Lei fail_alloc_slab:
10349d1ec85SMing Lei 	kfree(bslab);
10449d1ec85SMing Lei 	return NULL;
10549d1ec85SMing Lei }
10649d1ec85SMing Lei 
10749d1ec85SMing Lei static inline unsigned int bs_bio_slab_size(struct bio_set *bs)
10849d1ec85SMing Lei {
1099f180e31SMing Lei 	return bs->front_pad + sizeof(struct bio) + bs->back_pad;
11049d1ec85SMing Lei }
11149d1ec85SMing Lei 
11249d1ec85SMing Lei static struct kmem_cache *bio_find_or_create_slab(struct bio_set *bs)
11349d1ec85SMing Lei {
11449d1ec85SMing Lei 	unsigned int size = bs_bio_slab_size(bs);
11549d1ec85SMing Lei 	struct bio_slab *bslab;
116f9c78b2bSJens Axboe 
117f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
11849d1ec85SMing Lei 	bslab = xa_load(&bio_slabs, size);
11949d1ec85SMing Lei 	if (bslab)
120f9c78b2bSJens Axboe 		bslab->slab_ref++;
12149d1ec85SMing Lei 	else
12249d1ec85SMing Lei 		bslab = create_bio_slab(size);
123f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
12449d1ec85SMing Lei 
12549d1ec85SMing Lei 	if (bslab)
12649d1ec85SMing Lei 		return bslab->slab;
12749d1ec85SMing Lei 	return NULL;
128f9c78b2bSJens Axboe }
129f9c78b2bSJens Axboe 
130f9c78b2bSJens Axboe static void bio_put_slab(struct bio_set *bs)
131f9c78b2bSJens Axboe {
132f9c78b2bSJens Axboe 	struct bio_slab *bslab = NULL;
13349d1ec85SMing Lei 	unsigned int slab_size = bs_bio_slab_size(bs);
134f9c78b2bSJens Axboe 
135f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
136f9c78b2bSJens Axboe 
13749d1ec85SMing Lei 	bslab = xa_load(&bio_slabs, slab_size);
138f9c78b2bSJens Axboe 	if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
139f9c78b2bSJens Axboe 		goto out;
140f9c78b2bSJens Axboe 
14149d1ec85SMing Lei 	WARN_ON_ONCE(bslab->slab != bs->bio_slab);
14249d1ec85SMing Lei 
143f9c78b2bSJens Axboe 	WARN_ON(!bslab->slab_ref);
144f9c78b2bSJens Axboe 
145f9c78b2bSJens Axboe 	if (--bslab->slab_ref)
146f9c78b2bSJens Axboe 		goto out;
147f9c78b2bSJens Axboe 
14849d1ec85SMing Lei 	xa_erase(&bio_slabs, slab_size);
14949d1ec85SMing Lei 
150f9c78b2bSJens Axboe 	kmem_cache_destroy(bslab->slab);
15149d1ec85SMing Lei 	kfree(bslab);
152f9c78b2bSJens Axboe 
153f9c78b2bSJens Axboe out:
154f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
155f9c78b2bSJens Axboe }
156f9c78b2bSJens Axboe 
1577a800a20SChristoph Hellwig void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned short nr_vecs)
158f9c78b2bSJens Axboe {
159*9e8c0d0dSChristoph Hellwig 	BUG_ON(nr_vecs > BIO_MAX_VECS);
160f9c78b2bSJens Axboe 
161a8affc03SChristoph Hellwig 	if (nr_vecs == BIO_MAX_VECS)
162f9c78b2bSJens Axboe 		mempool_free(bv, pool);
1637a800a20SChristoph Hellwig 	else if (nr_vecs > BIO_INLINE_VECS)
1647a800a20SChristoph Hellwig 		kmem_cache_free(biovec_slab(nr_vecs)->slab, bv);
165f9c78b2bSJens Axboe }
166f9c78b2bSJens Axboe 
167f2c3eb9bSChristoph Hellwig /*
168f2c3eb9bSChristoph Hellwig  * Make the first allocation restricted and don't dump info on allocation
169f2c3eb9bSChristoph Hellwig  * failures, since we'll fall back to the mempool in case of failure.
170f2c3eb9bSChristoph Hellwig  */
171f2c3eb9bSChristoph Hellwig static inline gfp_t bvec_alloc_gfp(gfp_t gfp)
172f9c78b2bSJens Axboe {
173f2c3eb9bSChristoph Hellwig 	return (gfp & ~(__GFP_DIRECT_RECLAIM | __GFP_IO)) |
174f2c3eb9bSChristoph Hellwig 		__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
175f2c3eb9bSChristoph Hellwig }
176f2c3eb9bSChristoph Hellwig 
1777a800a20SChristoph Hellwig struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs,
1787a800a20SChristoph Hellwig 		gfp_t gfp_mask)
179f9c78b2bSJens Axboe {
1807a800a20SChristoph Hellwig 	struct biovec_slab *bvs = biovec_slab(*nr_vecs);
1817a800a20SChristoph Hellwig 
1827a800a20SChristoph Hellwig 	if (WARN_ON_ONCE(!bvs))
183f9c78b2bSJens Axboe 		return NULL;
1847a800a20SChristoph Hellwig 
1857a800a20SChristoph Hellwig 	/*
1867a800a20SChristoph Hellwig 	 * Upgrade the nr_vecs request to take full advantage of the allocation.
1877a800a20SChristoph Hellwig 	 * We also rely on this in the bvec_free path.
1887a800a20SChristoph Hellwig 	 */
1897a800a20SChristoph Hellwig 	*nr_vecs = bvs->nr_vecs;
190f9c78b2bSJens Axboe 
191f9c78b2bSJens Axboe 	/*
192f007a3d6SChristoph Hellwig 	 * Try a slab allocation first for all smaller allocations.  If that
193f007a3d6SChristoph Hellwig 	 * fails and __GFP_DIRECT_RECLAIM is set retry with the mempool.
194a8affc03SChristoph Hellwig 	 * The mempool is sized to handle up to BIO_MAX_VECS entries.
195f9c78b2bSJens Axboe 	 */
196a8affc03SChristoph Hellwig 	if (*nr_vecs < BIO_MAX_VECS) {
197f9c78b2bSJens Axboe 		struct bio_vec *bvl;
198f9c78b2bSJens Axboe 
199f2c3eb9bSChristoph Hellwig 		bvl = kmem_cache_alloc(bvs->slab, bvec_alloc_gfp(gfp_mask));
2007a800a20SChristoph Hellwig 		if (likely(bvl) || !(gfp_mask & __GFP_DIRECT_RECLAIM))
201f9c78b2bSJens Axboe 			return bvl;
202a8affc03SChristoph Hellwig 		*nr_vecs = BIO_MAX_VECS;
203f9c78b2bSJens Axboe 	}
204f9c78b2bSJens Axboe 
205f007a3d6SChristoph Hellwig 	return mempool_alloc(pool, gfp_mask);
206f9c78b2bSJens Axboe }
207f9c78b2bSJens Axboe 
2089ae3b3f5SJens Axboe void bio_uninit(struct bio *bio)
209f9c78b2bSJens Axboe {
210db9819c7SChristoph Hellwig #ifdef CONFIG_BLK_CGROUP
211db9819c7SChristoph Hellwig 	if (bio->bi_blkg) {
212db9819c7SChristoph Hellwig 		blkg_put(bio->bi_blkg);
213db9819c7SChristoph Hellwig 		bio->bi_blkg = NULL;
214db9819c7SChristoph Hellwig 	}
215db9819c7SChristoph Hellwig #endif
216ece841abSJustin Tee 	if (bio_integrity(bio))
217ece841abSJustin Tee 		bio_integrity_free(bio);
218a892c8d5SSatya Tangirala 
219a892c8d5SSatya Tangirala 	bio_crypt_free_ctx(bio);
220f9c78b2bSJens Axboe }
2219ae3b3f5SJens Axboe EXPORT_SYMBOL(bio_uninit);
222f9c78b2bSJens Axboe 
223f9c78b2bSJens Axboe static void bio_free(struct bio *bio)
224f9c78b2bSJens Axboe {
225f9c78b2bSJens Axboe 	struct bio_set *bs = bio->bi_pool;
226f9c78b2bSJens Axboe 	void *p;
227f9c78b2bSJens Axboe 
2289ae3b3f5SJens Axboe 	bio_uninit(bio);
229f9c78b2bSJens Axboe 
230f9c78b2bSJens Axboe 	if (bs) {
2317a800a20SChristoph Hellwig 		bvec_free(&bs->bvec_pool, bio->bi_io_vec, bio->bi_max_vecs);
232f9c78b2bSJens Axboe 
233f9c78b2bSJens Axboe 		/*
234f9c78b2bSJens Axboe 		 * If we have front padding, adjust the bio pointer before freeing
235f9c78b2bSJens Axboe 		 */
236f9c78b2bSJens Axboe 		p = bio;
237f9c78b2bSJens Axboe 		p -= bs->front_pad;
238f9c78b2bSJens Axboe 
2398aa6ba2fSKent Overstreet 		mempool_free(p, &bs->bio_pool);
240f9c78b2bSJens Axboe 	} else {
241f9c78b2bSJens Axboe 		/* Bio was allocated by bio_kmalloc() */
242f9c78b2bSJens Axboe 		kfree(bio);
243f9c78b2bSJens Axboe 	}
244f9c78b2bSJens Axboe }
245f9c78b2bSJens Axboe 
2469ae3b3f5SJens Axboe /*
2479ae3b3f5SJens Axboe  * Users of this function have their own bio allocation. Subsequently,
2489ae3b3f5SJens Axboe  * they must remember to pair any call to bio_init() with bio_uninit()
2499ae3b3f5SJens Axboe  * when IO has completed, or when the bio is released.
2509ae3b3f5SJens Axboe  */
2513a83f467SMing Lei void bio_init(struct bio *bio, struct bio_vec *table,
2523a83f467SMing Lei 	      unsigned short max_vecs)
253f9c78b2bSJens Axboe {
254da521626SJens Axboe 	bio->bi_next = NULL;
255da521626SJens Axboe 	bio->bi_bdev = NULL;
256da521626SJens Axboe 	bio->bi_opf = 0;
257da521626SJens Axboe 	bio->bi_flags = 0;
258da521626SJens Axboe 	bio->bi_ioprio = 0;
259da521626SJens Axboe 	bio->bi_write_hint = 0;
260da521626SJens Axboe 	bio->bi_status = 0;
261da521626SJens Axboe 	bio->bi_iter.bi_sector = 0;
262da521626SJens Axboe 	bio->bi_iter.bi_size = 0;
263da521626SJens Axboe 	bio->bi_iter.bi_idx = 0;
264da521626SJens Axboe 	bio->bi_iter.bi_bvec_done = 0;
265da521626SJens Axboe 	bio->bi_end_io = NULL;
266da521626SJens Axboe 	bio->bi_private = NULL;
267da521626SJens Axboe #ifdef CONFIG_BLK_CGROUP
268da521626SJens Axboe 	bio->bi_blkg = NULL;
269da521626SJens Axboe 	bio->bi_issue.value = 0;
270da521626SJens Axboe #ifdef CONFIG_BLK_CGROUP_IOCOST
271da521626SJens Axboe 	bio->bi_iocost_cost = 0;
272da521626SJens Axboe #endif
273da521626SJens Axboe #endif
274da521626SJens Axboe #ifdef CONFIG_BLK_INLINE_ENCRYPTION
275da521626SJens Axboe 	bio->bi_crypt_context = NULL;
276da521626SJens Axboe #endif
277da521626SJens Axboe #ifdef CONFIG_BLK_DEV_INTEGRITY
278da521626SJens Axboe 	bio->bi_integrity = NULL;
279da521626SJens Axboe #endif
280da521626SJens Axboe 	bio->bi_vcnt = 0;
281da521626SJens Axboe 
282c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
283dac56212SJens Axboe 	atomic_set(&bio->__bi_cnt, 1);
2843a83f467SMing Lei 
2853a83f467SMing Lei 	bio->bi_max_vecs = max_vecs;
286da521626SJens Axboe 	bio->bi_io_vec = table;
287da521626SJens Axboe 	bio->bi_pool = NULL;
288f9c78b2bSJens Axboe }
289f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_init);
290f9c78b2bSJens Axboe 
291f9c78b2bSJens Axboe /**
292f9c78b2bSJens Axboe  * bio_reset - reinitialize a bio
293f9c78b2bSJens Axboe  * @bio:	bio to reset
294f9c78b2bSJens Axboe  *
295f9c78b2bSJens Axboe  * Description:
296f9c78b2bSJens Axboe  *   After calling bio_reset(), @bio will be in the same state as a freshly
297f9c78b2bSJens Axboe  *   allocated bio returned bio bio_alloc_bioset() - the only fields that are
298f9c78b2bSJens Axboe  *   preserved are the ones that are initialized by bio_alloc_bioset(). See
299f9c78b2bSJens Axboe  *   comment in struct bio.
300f9c78b2bSJens Axboe  */
301f9c78b2bSJens Axboe void bio_reset(struct bio *bio)
302f9c78b2bSJens Axboe {
3039ae3b3f5SJens Axboe 	bio_uninit(bio);
304f9c78b2bSJens Axboe 	memset(bio, 0, BIO_RESET_BYTES);
305c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
306f9c78b2bSJens Axboe }
307f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_reset);
308f9c78b2bSJens Axboe 
30938f8baaeSChristoph Hellwig static struct bio *__bio_chain_endio(struct bio *bio)
310f9c78b2bSJens Axboe {
3114246a0b6SChristoph Hellwig 	struct bio *parent = bio->bi_private;
3124246a0b6SChristoph Hellwig 
3133edf5346SYufen Yu 	if (bio->bi_status && !parent->bi_status)
3144e4cbee9SChristoph Hellwig 		parent->bi_status = bio->bi_status;
315f9c78b2bSJens Axboe 	bio_put(bio);
31638f8baaeSChristoph Hellwig 	return parent;
31738f8baaeSChristoph Hellwig }
31838f8baaeSChristoph Hellwig 
31938f8baaeSChristoph Hellwig static void bio_chain_endio(struct bio *bio)
32038f8baaeSChristoph Hellwig {
32138f8baaeSChristoph Hellwig 	bio_endio(__bio_chain_endio(bio));
322f9c78b2bSJens Axboe }
323f9c78b2bSJens Axboe 
324f9c78b2bSJens Axboe /**
325f9c78b2bSJens Axboe  * bio_chain - chain bio completions
326f9c78b2bSJens Axboe  * @bio: the target bio
3275b874af6SMauro Carvalho Chehab  * @parent: the parent bio of @bio
328f9c78b2bSJens Axboe  *
329f9c78b2bSJens Axboe  * The caller won't have a bi_end_io called when @bio completes - instead,
330f9c78b2bSJens Axboe  * @parent's bi_end_io won't be called until both @parent and @bio have
331f9c78b2bSJens Axboe  * completed; the chained bio will also be freed when it completes.
332f9c78b2bSJens Axboe  *
333f9c78b2bSJens Axboe  * The caller must not set bi_private or bi_end_io in @bio.
334f9c78b2bSJens Axboe  */
335f9c78b2bSJens Axboe void bio_chain(struct bio *bio, struct bio *parent)
336f9c78b2bSJens Axboe {
337f9c78b2bSJens Axboe 	BUG_ON(bio->bi_private || bio->bi_end_io);
338f9c78b2bSJens Axboe 
339f9c78b2bSJens Axboe 	bio->bi_private = parent;
340f9c78b2bSJens Axboe 	bio->bi_end_io	= bio_chain_endio;
341c4cf5261SJens Axboe 	bio_inc_remaining(parent);
342f9c78b2bSJens Axboe }
343f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_chain);
344f9c78b2bSJens Axboe 
345f9c78b2bSJens Axboe static void bio_alloc_rescue(struct work_struct *work)
346f9c78b2bSJens Axboe {
347f9c78b2bSJens Axboe 	struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
348f9c78b2bSJens Axboe 	struct bio *bio;
349f9c78b2bSJens Axboe 
350f9c78b2bSJens Axboe 	while (1) {
351f9c78b2bSJens Axboe 		spin_lock(&bs->rescue_lock);
352f9c78b2bSJens Axboe 		bio = bio_list_pop(&bs->rescue_list);
353f9c78b2bSJens Axboe 		spin_unlock(&bs->rescue_lock);
354f9c78b2bSJens Axboe 
355f9c78b2bSJens Axboe 		if (!bio)
356f9c78b2bSJens Axboe 			break;
357f9c78b2bSJens Axboe 
358ed00aabdSChristoph Hellwig 		submit_bio_noacct(bio);
359f9c78b2bSJens Axboe 	}
360f9c78b2bSJens Axboe }
361f9c78b2bSJens Axboe 
362f9c78b2bSJens Axboe static void punt_bios_to_rescuer(struct bio_set *bs)
363f9c78b2bSJens Axboe {
364f9c78b2bSJens Axboe 	struct bio_list punt, nopunt;
365f9c78b2bSJens Axboe 	struct bio *bio;
366f9c78b2bSJens Axboe 
36747e0fb46SNeilBrown 	if (WARN_ON_ONCE(!bs->rescue_workqueue))
36847e0fb46SNeilBrown 		return;
369f9c78b2bSJens Axboe 	/*
370f9c78b2bSJens Axboe 	 * In order to guarantee forward progress we must punt only bios that
371f9c78b2bSJens Axboe 	 * were allocated from this bio_set; otherwise, if there was a bio on
372f9c78b2bSJens Axboe 	 * there for a stacking driver higher up in the stack, processing it
373f9c78b2bSJens Axboe 	 * could require allocating bios from this bio_set, and doing that from
374f9c78b2bSJens Axboe 	 * our own rescuer would be bad.
375f9c78b2bSJens Axboe 	 *
376f9c78b2bSJens Axboe 	 * Since bio lists are singly linked, pop them all instead of trying to
377f9c78b2bSJens Axboe 	 * remove from the middle of the list:
378f9c78b2bSJens Axboe 	 */
379f9c78b2bSJens Axboe 
380f9c78b2bSJens Axboe 	bio_list_init(&punt);
381f9c78b2bSJens Axboe 	bio_list_init(&nopunt);
382f9c78b2bSJens Axboe 
383f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[0])))
384f9c78b2bSJens Axboe 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
385f5fe1b51SNeilBrown 	current->bio_list[0] = nopunt;
386f9c78b2bSJens Axboe 
387f5fe1b51SNeilBrown 	bio_list_init(&nopunt);
388f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[1])))
389f5fe1b51SNeilBrown 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
390f5fe1b51SNeilBrown 	current->bio_list[1] = nopunt;
391f9c78b2bSJens Axboe 
392f9c78b2bSJens Axboe 	spin_lock(&bs->rescue_lock);
393f9c78b2bSJens Axboe 	bio_list_merge(&bs->rescue_list, &punt);
394f9c78b2bSJens Axboe 	spin_unlock(&bs->rescue_lock);
395f9c78b2bSJens Axboe 
396f9c78b2bSJens Axboe 	queue_work(bs->rescue_workqueue, &bs->rescue_work);
397f9c78b2bSJens Axboe }
398f9c78b2bSJens Axboe 
399f9c78b2bSJens Axboe /**
400f9c78b2bSJens Axboe  * bio_alloc_bioset - allocate a bio for I/O
401519c8e9fSRandy Dunlap  * @gfp_mask:   the GFP_* mask given to the slab allocator
402f9c78b2bSJens Axboe  * @nr_iovecs:	number of iovecs to pre-allocate
403f9c78b2bSJens Axboe  * @bs:		the bio_set to allocate from.
404f9c78b2bSJens Axboe  *
4053175199aSChristoph Hellwig  * Allocate a bio from the mempools in @bs.
406f9c78b2bSJens Axboe  *
4073175199aSChristoph Hellwig  * If %__GFP_DIRECT_RECLAIM is set then bio_alloc will always be able to
4083175199aSChristoph Hellwig  * allocate a bio.  This is due to the mempool guarantees.  To make this work,
4093175199aSChristoph Hellwig  * callers must never allocate more than 1 bio at a time from the general pool.
4103175199aSChristoph Hellwig  * Callers that need to allocate more than 1 bio must always submit the
4113175199aSChristoph Hellwig  * previously allocated bio for IO before attempting to allocate a new one.
4123175199aSChristoph Hellwig  * Failure to do so can cause deadlocks under memory pressure.
413f9c78b2bSJens Axboe  *
4143175199aSChristoph Hellwig  * Note that when running under submit_bio_noacct() (i.e. any block driver),
4153175199aSChristoph Hellwig  * bios are not submitted until after you return - see the code in
416ed00aabdSChristoph Hellwig  * submit_bio_noacct() that converts recursion into iteration, to prevent
417f9c78b2bSJens Axboe  * stack overflows.
418f9c78b2bSJens Axboe  *
4193175199aSChristoph Hellwig  * This would normally mean allocating multiple bios under submit_bio_noacct()
4203175199aSChristoph Hellwig  * would be susceptible to deadlocks, but we have
421f9c78b2bSJens Axboe  * deadlock avoidance code that resubmits any blocked bios from a rescuer
422f9c78b2bSJens Axboe  * thread.
423f9c78b2bSJens Axboe  *
424f9c78b2bSJens Axboe  * However, we do not guarantee forward progress for allocations from other
425f9c78b2bSJens Axboe  * mempools. Doing multiple allocations from the same mempool under
426ed00aabdSChristoph Hellwig  * submit_bio_noacct() should be avoided - instead, use bio_set's front_pad
427f9c78b2bSJens Axboe  * for per bio allocations.
428f9c78b2bSJens Axboe  *
4293175199aSChristoph Hellwig  * Returns: Pointer to new bio on success, NULL on failure.
430f9c78b2bSJens Axboe  */
4310f2e6ab8SChristoph Hellwig struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned short nr_iovecs,
4327a88fa19SDan Carpenter 			     struct bio_set *bs)
433f9c78b2bSJens Axboe {
434f9c78b2bSJens Axboe 	gfp_t saved_gfp = gfp_mask;
435f9c78b2bSJens Axboe 	struct bio *bio;
436f9c78b2bSJens Axboe 	void *p;
437f9c78b2bSJens Axboe 
4383175199aSChristoph Hellwig 	/* should not use nobvec bioset for nr_iovecs > 0 */
4393175199aSChristoph Hellwig 	if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && nr_iovecs > 0))
440f9c78b2bSJens Axboe 		return NULL;
441f9c78b2bSJens Axboe 
442f9c78b2bSJens Axboe 	/*
4433175199aSChristoph Hellwig 	 * submit_bio_noacct() converts recursion to iteration; this means if
4443175199aSChristoph Hellwig 	 * we're running beneath it, any bios we allocate and submit will not be
4453175199aSChristoph Hellwig 	 * submitted (and thus freed) until after we return.
446f9c78b2bSJens Axboe 	 *
4473175199aSChristoph Hellwig 	 * This exposes us to a potential deadlock if we allocate multiple bios
4483175199aSChristoph Hellwig 	 * from the same bio_set() while running underneath submit_bio_noacct().
4493175199aSChristoph Hellwig 	 * If we were to allocate multiple bios (say a stacking block driver
4503175199aSChristoph Hellwig 	 * that was splitting bios), we would deadlock if we exhausted the
4513175199aSChristoph Hellwig 	 * mempool's reserve.
452f9c78b2bSJens Axboe 	 *
453f9c78b2bSJens Axboe 	 * We solve this, and guarantee forward progress, with a rescuer
4543175199aSChristoph Hellwig 	 * workqueue per bio_set. If we go to allocate and there are bios on
4553175199aSChristoph Hellwig 	 * current->bio_list, we first try the allocation without
4563175199aSChristoph Hellwig 	 * __GFP_DIRECT_RECLAIM; if that fails, we punt those bios we would be
4573175199aSChristoph Hellwig 	 * blocking to the rescuer workqueue before we retry with the original
4583175199aSChristoph Hellwig 	 * gfp_flags.
459f9c78b2bSJens Axboe 	 */
460f5fe1b51SNeilBrown 	if (current->bio_list &&
461f5fe1b51SNeilBrown 	    (!bio_list_empty(&current->bio_list[0]) ||
46247e0fb46SNeilBrown 	     !bio_list_empty(&current->bio_list[1])) &&
46347e0fb46SNeilBrown 	    bs->rescue_workqueue)
464d0164adcSMel Gorman 		gfp_mask &= ~__GFP_DIRECT_RECLAIM;
465f9c78b2bSJens Axboe 
4668aa6ba2fSKent Overstreet 	p = mempool_alloc(&bs->bio_pool, gfp_mask);
467f9c78b2bSJens Axboe 	if (!p && gfp_mask != saved_gfp) {
468f9c78b2bSJens Axboe 		punt_bios_to_rescuer(bs);
469f9c78b2bSJens Axboe 		gfp_mask = saved_gfp;
4708aa6ba2fSKent Overstreet 		p = mempool_alloc(&bs->bio_pool, gfp_mask);
471f9c78b2bSJens Axboe 	}
472f9c78b2bSJens Axboe 	if (unlikely(!p))
473f9c78b2bSJens Axboe 		return NULL;
474f9c78b2bSJens Axboe 
4753175199aSChristoph Hellwig 	bio = p + bs->front_pad;
4763175199aSChristoph Hellwig 	if (nr_iovecs > BIO_INLINE_VECS) {
4773175199aSChristoph Hellwig 		struct bio_vec *bvl = NULL;
478f9c78b2bSJens Axboe 
4797a800a20SChristoph Hellwig 		bvl = bvec_alloc(&bs->bvec_pool, &nr_iovecs, gfp_mask);
480f9c78b2bSJens Axboe 		if (!bvl && gfp_mask != saved_gfp) {
481f9c78b2bSJens Axboe 			punt_bios_to_rescuer(bs);
482f9c78b2bSJens Axboe 			gfp_mask = saved_gfp;
4837a800a20SChristoph Hellwig 			bvl = bvec_alloc(&bs->bvec_pool, &nr_iovecs, gfp_mask);
484f9c78b2bSJens Axboe 		}
485f9c78b2bSJens Axboe 		if (unlikely(!bvl))
486f9c78b2bSJens Axboe 			goto err_free;
487f9c78b2bSJens Axboe 
4887a800a20SChristoph Hellwig 		bio_init(bio, bvl, nr_iovecs);
489f9c78b2bSJens Axboe 	} else if (nr_iovecs) {
4903175199aSChristoph Hellwig 		bio_init(bio, bio->bi_inline_vecs, BIO_INLINE_VECS);
4913175199aSChristoph Hellwig 	} else {
4923175199aSChristoph Hellwig 		bio_init(bio, NULL, 0);
493f9c78b2bSJens Axboe 	}
494f9c78b2bSJens Axboe 
495f9c78b2bSJens Axboe 	bio->bi_pool = bs;
496f9c78b2bSJens Axboe 	return bio;
497f9c78b2bSJens Axboe 
498f9c78b2bSJens Axboe err_free:
4998aa6ba2fSKent Overstreet 	mempool_free(p, &bs->bio_pool);
500f9c78b2bSJens Axboe 	return NULL;
501f9c78b2bSJens Axboe }
502f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_bioset);
503f9c78b2bSJens Axboe 
5043175199aSChristoph Hellwig /**
5053175199aSChristoph Hellwig  * bio_kmalloc - kmalloc a bio for I/O
5063175199aSChristoph Hellwig  * @gfp_mask:   the GFP_* mask given to the slab allocator
5073175199aSChristoph Hellwig  * @nr_iovecs:	number of iovecs to pre-allocate
5083175199aSChristoph Hellwig  *
5093175199aSChristoph Hellwig  * Use kmalloc to allocate and initialize a bio.
5103175199aSChristoph Hellwig  *
5113175199aSChristoph Hellwig  * Returns: Pointer to new bio on success, NULL on failure.
5123175199aSChristoph Hellwig  */
5130f2e6ab8SChristoph Hellwig struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned short nr_iovecs)
5143175199aSChristoph Hellwig {
5153175199aSChristoph Hellwig 	struct bio *bio;
5163175199aSChristoph Hellwig 
5173175199aSChristoph Hellwig 	if (nr_iovecs > UIO_MAXIOV)
5183175199aSChristoph Hellwig 		return NULL;
5193175199aSChristoph Hellwig 
5203175199aSChristoph Hellwig 	bio = kmalloc(struct_size(bio, bi_inline_vecs, nr_iovecs), gfp_mask);
5213175199aSChristoph Hellwig 	if (unlikely(!bio))
5223175199aSChristoph Hellwig 		return NULL;
5233175199aSChristoph Hellwig 	bio_init(bio, nr_iovecs ? bio->bi_inline_vecs : NULL, nr_iovecs);
5243175199aSChristoph Hellwig 	bio->bi_pool = NULL;
5253175199aSChristoph Hellwig 	return bio;
5263175199aSChristoph Hellwig }
5273175199aSChristoph Hellwig EXPORT_SYMBOL(bio_kmalloc);
5283175199aSChristoph Hellwig 
5296f822e1bSChristoph Hellwig void zero_fill_bio(struct bio *bio)
530f9c78b2bSJens Axboe {
531f9c78b2bSJens Axboe 	struct bio_vec bv;
532f9c78b2bSJens Axboe 	struct bvec_iter iter;
533f9c78b2bSJens Axboe 
534ab6c340eSChristoph Hellwig 	bio_for_each_segment(bv, bio, iter)
535ab6c340eSChristoph Hellwig 		memzero_bvec(&bv);
536f9c78b2bSJens Axboe }
5376f822e1bSChristoph Hellwig EXPORT_SYMBOL(zero_fill_bio);
538f9c78b2bSJens Axboe 
53983c9c547SMing Lei /**
54083c9c547SMing Lei  * bio_truncate - truncate the bio to small size of @new_size
54183c9c547SMing Lei  * @bio:	the bio to be truncated
54283c9c547SMing Lei  * @new_size:	new size for truncating the bio
54383c9c547SMing Lei  *
54483c9c547SMing Lei  * Description:
54583c9c547SMing Lei  *   Truncate the bio to new size of @new_size. If bio_op(bio) is
54683c9c547SMing Lei  *   REQ_OP_READ, zero the truncated part. This function should only
54783c9c547SMing Lei  *   be used for handling corner cases, such as bio eod.
54883c9c547SMing Lei  */
54985a8ce62SMing Lei void bio_truncate(struct bio *bio, unsigned new_size)
55085a8ce62SMing Lei {
55185a8ce62SMing Lei 	struct bio_vec bv;
55285a8ce62SMing Lei 	struct bvec_iter iter;
55385a8ce62SMing Lei 	unsigned int done = 0;
55485a8ce62SMing Lei 	bool truncated = false;
55585a8ce62SMing Lei 
55685a8ce62SMing Lei 	if (new_size >= bio->bi_iter.bi_size)
55785a8ce62SMing Lei 		return;
55885a8ce62SMing Lei 
55983c9c547SMing Lei 	if (bio_op(bio) != REQ_OP_READ)
56085a8ce62SMing Lei 		goto exit;
56185a8ce62SMing Lei 
56285a8ce62SMing Lei 	bio_for_each_segment(bv, bio, iter) {
56385a8ce62SMing Lei 		if (done + bv.bv_len > new_size) {
56485a8ce62SMing Lei 			unsigned offset;
56585a8ce62SMing Lei 
56685a8ce62SMing Lei 			if (!truncated)
56785a8ce62SMing Lei 				offset = new_size - done;
56885a8ce62SMing Lei 			else
56985a8ce62SMing Lei 				offset = 0;
57085a8ce62SMing Lei 			zero_user(bv.bv_page, offset, bv.bv_len - offset);
57185a8ce62SMing Lei 			truncated = true;
57285a8ce62SMing Lei 		}
57385a8ce62SMing Lei 		done += bv.bv_len;
57485a8ce62SMing Lei 	}
57585a8ce62SMing Lei 
57685a8ce62SMing Lei  exit:
57785a8ce62SMing Lei 	/*
57885a8ce62SMing Lei 	 * Don't touch bvec table here and make it really immutable, since
57985a8ce62SMing Lei 	 * fs bio user has to retrieve all pages via bio_for_each_segment_all
58085a8ce62SMing Lei 	 * in its .end_bio() callback.
58185a8ce62SMing Lei 	 *
58285a8ce62SMing Lei 	 * It is enough to truncate bio by updating .bi_size since we can make
58385a8ce62SMing Lei 	 * correct bvec with the updated .bi_size for drivers.
58485a8ce62SMing Lei 	 */
58585a8ce62SMing Lei 	bio->bi_iter.bi_size = new_size;
58685a8ce62SMing Lei }
58785a8ce62SMing Lei 
588f9c78b2bSJens Axboe /**
58929125ed6SChristoph Hellwig  * guard_bio_eod - truncate a BIO to fit the block device
59029125ed6SChristoph Hellwig  * @bio:	bio to truncate
59129125ed6SChristoph Hellwig  *
59229125ed6SChristoph Hellwig  * This allows us to do IO even on the odd last sectors of a device, even if the
59329125ed6SChristoph Hellwig  * block size is some multiple of the physical sector size.
59429125ed6SChristoph Hellwig  *
59529125ed6SChristoph Hellwig  * We'll just truncate the bio to the size of the device, and clear the end of
59629125ed6SChristoph Hellwig  * the buffer head manually.  Truly out-of-range accesses will turn into actual
59729125ed6SChristoph Hellwig  * I/O errors, this only handles the "we need to be able to do I/O at the final
59829125ed6SChristoph Hellwig  * sector" case.
59929125ed6SChristoph Hellwig  */
60029125ed6SChristoph Hellwig void guard_bio_eod(struct bio *bio)
60129125ed6SChristoph Hellwig {
602309dca30SChristoph Hellwig 	sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
60329125ed6SChristoph Hellwig 
60429125ed6SChristoph Hellwig 	if (!maxsector)
60529125ed6SChristoph Hellwig 		return;
60629125ed6SChristoph Hellwig 
60729125ed6SChristoph Hellwig 	/*
60829125ed6SChristoph Hellwig 	 * If the *whole* IO is past the end of the device,
60929125ed6SChristoph Hellwig 	 * let it through, and the IO layer will turn it into
61029125ed6SChristoph Hellwig 	 * an EIO.
61129125ed6SChristoph Hellwig 	 */
61229125ed6SChristoph Hellwig 	if (unlikely(bio->bi_iter.bi_sector >= maxsector))
61329125ed6SChristoph Hellwig 		return;
61429125ed6SChristoph Hellwig 
61529125ed6SChristoph Hellwig 	maxsector -= bio->bi_iter.bi_sector;
61629125ed6SChristoph Hellwig 	if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
61729125ed6SChristoph Hellwig 		return;
61829125ed6SChristoph Hellwig 
61929125ed6SChristoph Hellwig 	bio_truncate(bio, maxsector << 9);
62029125ed6SChristoph Hellwig }
62129125ed6SChristoph Hellwig 
622be4d234dSJens Axboe #define ALLOC_CACHE_MAX		512
623be4d234dSJens Axboe #define ALLOC_CACHE_SLACK	 64
624be4d234dSJens Axboe 
625be4d234dSJens Axboe static void bio_alloc_cache_prune(struct bio_alloc_cache *cache,
626be4d234dSJens Axboe 				  unsigned int nr)
627be4d234dSJens Axboe {
628be4d234dSJens Axboe 	unsigned int i = 0;
629be4d234dSJens Axboe 	struct bio *bio;
630be4d234dSJens Axboe 
631be4d234dSJens Axboe 	while ((bio = bio_list_pop(&cache->free_list)) != NULL) {
632be4d234dSJens Axboe 		cache->nr--;
633be4d234dSJens Axboe 		bio_free(bio);
634be4d234dSJens Axboe 		if (++i == nr)
635be4d234dSJens Axboe 			break;
636be4d234dSJens Axboe 	}
637be4d234dSJens Axboe }
638be4d234dSJens Axboe 
639be4d234dSJens Axboe static int bio_cpu_dead(unsigned int cpu, struct hlist_node *node)
640be4d234dSJens Axboe {
641be4d234dSJens Axboe 	struct bio_set *bs;
642be4d234dSJens Axboe 
643be4d234dSJens Axboe 	bs = hlist_entry_safe(node, struct bio_set, cpuhp_dead);
644be4d234dSJens Axboe 	if (bs->cache) {
645be4d234dSJens Axboe 		struct bio_alloc_cache *cache = per_cpu_ptr(bs->cache, cpu);
646be4d234dSJens Axboe 
647be4d234dSJens Axboe 		bio_alloc_cache_prune(cache, -1U);
648be4d234dSJens Axboe 	}
649be4d234dSJens Axboe 	return 0;
650be4d234dSJens Axboe }
651be4d234dSJens Axboe 
652be4d234dSJens Axboe static void bio_alloc_cache_destroy(struct bio_set *bs)
653be4d234dSJens Axboe {
654be4d234dSJens Axboe 	int cpu;
655be4d234dSJens Axboe 
656be4d234dSJens Axboe 	if (!bs->cache)
657be4d234dSJens Axboe 		return;
658be4d234dSJens Axboe 
659be4d234dSJens Axboe 	cpuhp_state_remove_instance_nocalls(CPUHP_BIO_DEAD, &bs->cpuhp_dead);
660be4d234dSJens Axboe 	for_each_possible_cpu(cpu) {
661be4d234dSJens Axboe 		struct bio_alloc_cache *cache;
662be4d234dSJens Axboe 
663be4d234dSJens Axboe 		cache = per_cpu_ptr(bs->cache, cpu);
664be4d234dSJens Axboe 		bio_alloc_cache_prune(cache, -1U);
665be4d234dSJens Axboe 	}
666be4d234dSJens Axboe 	free_percpu(bs->cache);
667be4d234dSJens Axboe }
668be4d234dSJens Axboe 
66929125ed6SChristoph Hellwig /**
670f9c78b2bSJens Axboe  * bio_put - release a reference to a bio
671f9c78b2bSJens Axboe  * @bio:   bio to release reference to
672f9c78b2bSJens Axboe  *
673f9c78b2bSJens Axboe  * Description:
674f9c78b2bSJens Axboe  *   Put a reference to a &struct bio, either one you have gotten with
6759b10f6a9SNeilBrown  *   bio_alloc, bio_get or bio_clone_*. The last put of a bio will free it.
676f9c78b2bSJens Axboe  **/
677f9c78b2bSJens Axboe void bio_put(struct bio *bio)
678f9c78b2bSJens Axboe {
679be4d234dSJens Axboe 	if (unlikely(bio_flagged(bio, BIO_REFFED))) {
680*9e8c0d0dSChristoph Hellwig 		BUG_ON(!atomic_read(&bio->__bi_cnt));
681be4d234dSJens Axboe 		if (!atomic_dec_and_test(&bio->__bi_cnt))
682be4d234dSJens Axboe 			return;
683be4d234dSJens Axboe 	}
684f9c78b2bSJens Axboe 
685be4d234dSJens Axboe 	if (bio_flagged(bio, BIO_PERCPU_CACHE)) {
686be4d234dSJens Axboe 		struct bio_alloc_cache *cache;
687be4d234dSJens Axboe 
688be4d234dSJens Axboe 		bio_uninit(bio);
689be4d234dSJens Axboe 		cache = per_cpu_ptr(bio->bi_pool->cache, get_cpu());
690be4d234dSJens Axboe 		bio_list_add_head(&cache->free_list, bio);
691be4d234dSJens Axboe 		if (++cache->nr > ALLOC_CACHE_MAX + ALLOC_CACHE_SLACK)
692be4d234dSJens Axboe 			bio_alloc_cache_prune(cache, ALLOC_CACHE_SLACK);
693be4d234dSJens Axboe 		put_cpu();
694be4d234dSJens Axboe 	} else {
695f9c78b2bSJens Axboe 		bio_free(bio);
696f9c78b2bSJens Axboe 	}
697dac56212SJens Axboe }
698f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_put);
699f9c78b2bSJens Axboe 
700f9c78b2bSJens Axboe /**
701f9c78b2bSJens Axboe  * 	__bio_clone_fast - clone a bio that shares the original bio's biovec
702f9c78b2bSJens Axboe  * 	@bio: destination bio
703f9c78b2bSJens Axboe  * 	@bio_src: bio to clone
704f9c78b2bSJens Axboe  *
705f9c78b2bSJens Axboe  *	Clone a &bio. Caller will own the returned bio, but not
706f9c78b2bSJens Axboe  *	the actual data it points to. Reference count of returned
707f9c78b2bSJens Axboe  * 	bio will be one.
708f9c78b2bSJens Axboe  *
709f9c78b2bSJens Axboe  * 	Caller must ensure that @bio_src is not freed before @bio.
710f9c78b2bSJens Axboe  */
711f9c78b2bSJens Axboe void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
712f9c78b2bSJens Axboe {
7137a800a20SChristoph Hellwig 	WARN_ON_ONCE(bio->bi_pool && bio->bi_max_vecs);
714f9c78b2bSJens Axboe 
715f9c78b2bSJens Axboe 	/*
716309dca30SChristoph Hellwig 	 * most users will be overriding ->bi_bdev with a new target,
717f9c78b2bSJens Axboe 	 * so we don't set nor calculate new physical/hw segment counts here
718f9c78b2bSJens Axboe 	 */
719309dca30SChristoph Hellwig 	bio->bi_bdev = bio_src->bi_bdev;
720b7c44ed9SJens Axboe 	bio_set_flag(bio, BIO_CLONED);
721111be883SShaohua Li 	if (bio_flagged(bio_src, BIO_THROTTLED))
722111be883SShaohua Li 		bio_set_flag(bio, BIO_THROTTLED);
72346bbf653SChristoph Hellwig 	if (bio_flagged(bio_src, BIO_REMAPPED))
72446bbf653SChristoph Hellwig 		bio_set_flag(bio, BIO_REMAPPED);
7251eff9d32SJens Axboe 	bio->bi_opf = bio_src->bi_opf;
726ca474b73SHannes Reinecke 	bio->bi_ioprio = bio_src->bi_ioprio;
727cb6934f8SJens Axboe 	bio->bi_write_hint = bio_src->bi_write_hint;
728f9c78b2bSJens Axboe 	bio->bi_iter = bio_src->bi_iter;
729f9c78b2bSJens Axboe 	bio->bi_io_vec = bio_src->bi_io_vec;
73020bd723eSPaolo Valente 
731db6638d7SDennis Zhou 	bio_clone_blkg_association(bio, bio_src);
732e439bedfSDennis Zhou 	blkcg_bio_issue_init(bio);
733f9c78b2bSJens Axboe }
734f9c78b2bSJens Axboe EXPORT_SYMBOL(__bio_clone_fast);
735f9c78b2bSJens Axboe 
736f9c78b2bSJens Axboe /**
737f9c78b2bSJens Axboe  *	bio_clone_fast - clone a bio that shares the original bio's biovec
738f9c78b2bSJens Axboe  *	@bio: bio to clone
739f9c78b2bSJens Axboe  *	@gfp_mask: allocation priority
740f9c78b2bSJens Axboe  *	@bs: bio_set to allocate from
741f9c78b2bSJens Axboe  *
742f9c78b2bSJens Axboe  * 	Like __bio_clone_fast, only also allocates the returned bio
743f9c78b2bSJens Axboe  */
744f9c78b2bSJens Axboe struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs)
745f9c78b2bSJens Axboe {
746f9c78b2bSJens Axboe 	struct bio *b;
747f9c78b2bSJens Axboe 
748f9c78b2bSJens Axboe 	b = bio_alloc_bioset(gfp_mask, 0, bs);
749f9c78b2bSJens Axboe 	if (!b)
750f9c78b2bSJens Axboe 		return NULL;
751f9c78b2bSJens Axboe 
752f9c78b2bSJens Axboe 	__bio_clone_fast(b, bio);
753f9c78b2bSJens Axboe 
75407560151SEric Biggers 	if (bio_crypt_clone(b, bio, gfp_mask) < 0)
75507560151SEric Biggers 		goto err_put;
756a892c8d5SSatya Tangirala 
75707560151SEric Biggers 	if (bio_integrity(bio) &&
75807560151SEric Biggers 	    bio_integrity_clone(b, bio, gfp_mask) < 0)
75907560151SEric Biggers 		goto err_put;
760f9c78b2bSJens Axboe 
761f9c78b2bSJens Axboe 	return b;
76207560151SEric Biggers 
76307560151SEric Biggers err_put:
76407560151SEric Biggers 	bio_put(b);
76507560151SEric Biggers 	return NULL;
766f9c78b2bSJens Axboe }
767f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_clone_fast);
768f9c78b2bSJens Axboe 
7695cbd28e3SChristoph Hellwig const char *bio_devname(struct bio *bio, char *buf)
7705cbd28e3SChristoph Hellwig {
771309dca30SChristoph Hellwig 	return bdevname(bio->bi_bdev, buf);
7725cbd28e3SChristoph Hellwig }
7735cbd28e3SChristoph Hellwig EXPORT_SYMBOL(bio_devname);
7745cbd28e3SChristoph Hellwig 
7755919482eSMing Lei static inline bool page_is_mergeable(const struct bio_vec *bv,
7765919482eSMing Lei 		struct page *page, unsigned int len, unsigned int off,
777ff896738SChristoph Hellwig 		bool *same_page)
7785919482eSMing Lei {
779d8166519SMatthew Wilcox (Oracle) 	size_t bv_end = bv->bv_offset + bv->bv_len;
780d8166519SMatthew Wilcox (Oracle) 	phys_addr_t vec_end_addr = page_to_phys(bv->bv_page) + bv_end - 1;
7815919482eSMing Lei 	phys_addr_t page_addr = page_to_phys(page);
7825919482eSMing Lei 
7835919482eSMing Lei 	if (vec_end_addr + 1 != page_addr + off)
7845919482eSMing Lei 		return false;
7855919482eSMing Lei 	if (xen_domain() && !xen_biovec_phys_mergeable(bv, page))
7865919482eSMing Lei 		return false;
78752d52d1cSChristoph Hellwig 
788ff896738SChristoph Hellwig 	*same_page = ((vec_end_addr & PAGE_MASK) == page_addr);
789d8166519SMatthew Wilcox (Oracle) 	if (*same_page)
7905919482eSMing Lei 		return true;
791d8166519SMatthew Wilcox (Oracle) 	return (bv->bv_page + bv_end / PAGE_SIZE) == (page + off / PAGE_SIZE);
7925919482eSMing Lei }
7935919482eSMing Lei 
794e4581105SChristoph Hellwig /*
795e4581105SChristoph Hellwig  * Try to merge a page into a segment, while obeying the hardware segment
796e4581105SChristoph Hellwig  * size limit.  This is not for normal read/write bios, but for passthrough
797e4581105SChristoph Hellwig  * or Zone Append operations that we can't split.
798e4581105SChristoph Hellwig  */
799e4581105SChristoph Hellwig static bool bio_try_merge_hw_seg(struct request_queue *q, struct bio *bio,
800e4581105SChristoph Hellwig 				 struct page *page, unsigned len,
801e4581105SChristoph Hellwig 				 unsigned offset, bool *same_page)
802489fbbcbSMing Lei {
803384209cdSChristoph Hellwig 	struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
804489fbbcbSMing Lei 	unsigned long mask = queue_segment_boundary(q);
805489fbbcbSMing Lei 	phys_addr_t addr1 = page_to_phys(bv->bv_page) + bv->bv_offset;
806489fbbcbSMing Lei 	phys_addr_t addr2 = page_to_phys(page) + offset + len - 1;
807489fbbcbSMing Lei 
808489fbbcbSMing Lei 	if ((addr1 | mask) != (addr2 | mask))
809489fbbcbSMing Lei 		return false;
810489fbbcbSMing Lei 	if (bv->bv_len + len > queue_max_segment_size(q))
811489fbbcbSMing Lei 		return false;
812384209cdSChristoph Hellwig 	return __bio_try_merge_page(bio, page, len, offset, same_page);
813489fbbcbSMing Lei }
814489fbbcbSMing Lei 
815f4595875SShaohua Li /**
816e4581105SChristoph Hellwig  * bio_add_hw_page - attempt to add a page to a bio with hw constraints
817c66a14d0SKent Overstreet  * @q: the target queue
818c66a14d0SKent Overstreet  * @bio: destination bio
819c66a14d0SKent Overstreet  * @page: page to add
820c66a14d0SKent Overstreet  * @len: vec entry length
821c66a14d0SKent Overstreet  * @offset: vec entry offset
822e4581105SChristoph Hellwig  * @max_sectors: maximum number of sectors that can be added
823e4581105SChristoph Hellwig  * @same_page: return if the segment has been merged inside the same page
824f9c78b2bSJens Axboe  *
825e4581105SChristoph Hellwig  * Add a page to a bio while respecting the hardware max_sectors, max_segment
826e4581105SChristoph Hellwig  * and gap limitations.
827f9c78b2bSJens Axboe  */
828e4581105SChristoph Hellwig int bio_add_hw_page(struct request_queue *q, struct bio *bio,
82919047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset,
830e4581105SChristoph Hellwig 		unsigned int max_sectors, bool *same_page)
831f9c78b2bSJens Axboe {
832f9c78b2bSJens Axboe 	struct bio_vec *bvec;
833f9c78b2bSJens Axboe 
834e4581105SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
835f9c78b2bSJens Axboe 		return 0;
836f9c78b2bSJens Axboe 
837e4581105SChristoph Hellwig 	if (((bio->bi_iter.bi_size + len) >> 9) > max_sectors)
838f9c78b2bSJens Axboe 		return 0;
839f9c78b2bSJens Axboe 
840f9c78b2bSJens Axboe 	if (bio->bi_vcnt > 0) {
841e4581105SChristoph Hellwig 		if (bio_try_merge_hw_seg(q, bio, page, len, offset, same_page))
842384209cdSChristoph Hellwig 			return len;
843320ea869SChristoph Hellwig 
844320ea869SChristoph Hellwig 		/*
845320ea869SChristoph Hellwig 		 * If the queue doesn't support SG gaps and adding this segment
846320ea869SChristoph Hellwig 		 * would create a gap, disallow it.
847320ea869SChristoph Hellwig 		 */
848384209cdSChristoph Hellwig 		bvec = &bio->bi_io_vec[bio->bi_vcnt - 1];
849320ea869SChristoph Hellwig 		if (bvec_gap_to_prev(q, bvec, offset))
850320ea869SChristoph Hellwig 			return 0;
851f9c78b2bSJens Axboe 	}
852f9c78b2bSJens Axboe 
85379d08f89SMing Lei 	if (bio_full(bio, len))
854f9c78b2bSJens Axboe 		return 0;
855f9c78b2bSJens Axboe 
85614ccb66bSChristoph Hellwig 	if (bio->bi_vcnt >= queue_max_segments(q))
857489fbbcbSMing Lei 		return 0;
858489fbbcbSMing Lei 
859f9c78b2bSJens Axboe 	bvec = &bio->bi_io_vec[bio->bi_vcnt];
860f9c78b2bSJens Axboe 	bvec->bv_page = page;
861f9c78b2bSJens Axboe 	bvec->bv_len = len;
862f9c78b2bSJens Axboe 	bvec->bv_offset = offset;
863fcbf6a08SMaurizio Lombardi 	bio->bi_vcnt++;
864dcdca753SChristoph Hellwig 	bio->bi_iter.bi_size += len;
865f9c78b2bSJens Axboe 	return len;
866f9c78b2bSJens Axboe }
86719047087SMing Lei 
868e4581105SChristoph Hellwig /**
869e4581105SChristoph Hellwig  * bio_add_pc_page	- attempt to add page to passthrough bio
870e4581105SChristoph Hellwig  * @q: the target queue
871e4581105SChristoph Hellwig  * @bio: destination bio
872e4581105SChristoph Hellwig  * @page: page to add
873e4581105SChristoph Hellwig  * @len: vec entry length
874e4581105SChristoph Hellwig  * @offset: vec entry offset
875e4581105SChristoph Hellwig  *
876e4581105SChristoph Hellwig  * Attempt to add a page to the bio_vec maplist. This can fail for a
877e4581105SChristoph Hellwig  * number of reasons, such as the bio being full or target block device
878e4581105SChristoph Hellwig  * limitations. The target block device must allow bio's up to PAGE_SIZE,
879e4581105SChristoph Hellwig  * so it is always possible to add a single page to an empty bio.
880e4581105SChristoph Hellwig  *
881e4581105SChristoph Hellwig  * This should only be used by passthrough bios.
882e4581105SChristoph Hellwig  */
88319047087SMing Lei int bio_add_pc_page(struct request_queue *q, struct bio *bio,
88419047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset)
88519047087SMing Lei {
886d1916c86SChristoph Hellwig 	bool same_page = false;
887e4581105SChristoph Hellwig 	return bio_add_hw_page(q, bio, page, len, offset,
888e4581105SChristoph Hellwig 			queue_max_hw_sectors(q), &same_page);
88919047087SMing Lei }
890f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_pc_page);
891f9c78b2bSJens Axboe 
892f9c78b2bSJens Axboe /**
893ae29333fSJohannes Thumshirn  * bio_add_zone_append_page - attempt to add page to zone-append bio
894ae29333fSJohannes Thumshirn  * @bio: destination bio
895ae29333fSJohannes Thumshirn  * @page: page to add
896ae29333fSJohannes Thumshirn  * @len: vec entry length
897ae29333fSJohannes Thumshirn  * @offset: vec entry offset
898ae29333fSJohannes Thumshirn  *
899ae29333fSJohannes Thumshirn  * Attempt to add a page to the bio_vec maplist of a bio that will be submitted
900ae29333fSJohannes Thumshirn  * for a zone-append request. This can fail for a number of reasons, such as the
901ae29333fSJohannes Thumshirn  * bio being full or the target block device is not a zoned block device or
902ae29333fSJohannes Thumshirn  * other limitations of the target block device. The target block device must
903ae29333fSJohannes Thumshirn  * allow bio's up to PAGE_SIZE, so it is always possible to add a single page
904ae29333fSJohannes Thumshirn  * to an empty bio.
905ae29333fSJohannes Thumshirn  *
906ae29333fSJohannes Thumshirn  * Returns: number of bytes added to the bio, or 0 in case of a failure.
907ae29333fSJohannes Thumshirn  */
908ae29333fSJohannes Thumshirn int bio_add_zone_append_page(struct bio *bio, struct page *page,
909ae29333fSJohannes Thumshirn 			     unsigned int len, unsigned int offset)
910ae29333fSJohannes Thumshirn {
911582cd91fSLinus Torvalds 	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
912ae29333fSJohannes Thumshirn 	bool same_page = false;
913ae29333fSJohannes Thumshirn 
914ae29333fSJohannes Thumshirn 	if (WARN_ON_ONCE(bio_op(bio) != REQ_OP_ZONE_APPEND))
915ae29333fSJohannes Thumshirn 		return 0;
916ae29333fSJohannes Thumshirn 
917ae29333fSJohannes Thumshirn 	if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
918ae29333fSJohannes Thumshirn 		return 0;
919ae29333fSJohannes Thumshirn 
920ae29333fSJohannes Thumshirn 	return bio_add_hw_page(q, bio, page, len, offset,
921ae29333fSJohannes Thumshirn 			       queue_max_zone_append_sectors(q), &same_page);
922ae29333fSJohannes Thumshirn }
923ae29333fSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_add_zone_append_page);
924ae29333fSJohannes Thumshirn 
925ae29333fSJohannes Thumshirn /**
9260aa69fd3SChristoph Hellwig  * __bio_try_merge_page - try appending data to an existing bvec.
9270aa69fd3SChristoph Hellwig  * @bio: destination bio
928551879a4SMing Lei  * @page: start page to add
9290aa69fd3SChristoph Hellwig  * @len: length of the data to add
930551879a4SMing Lei  * @off: offset of the data relative to @page
931ff896738SChristoph Hellwig  * @same_page: return if the segment has been merged inside the same page
9320aa69fd3SChristoph Hellwig  *
9330aa69fd3SChristoph Hellwig  * Try to add the data at @page + @off to the last bvec of @bio.  This is a
9343cf14889SRandy Dunlap  * useful optimisation for file systems with a block size smaller than the
9350aa69fd3SChristoph Hellwig  * page size.
9360aa69fd3SChristoph Hellwig  *
937551879a4SMing Lei  * Warn if (@len, @off) crosses pages in case that @same_page is true.
938551879a4SMing Lei  *
9390aa69fd3SChristoph Hellwig  * Return %true on success or %false on failure.
9400aa69fd3SChristoph Hellwig  */
9410aa69fd3SChristoph Hellwig bool __bio_try_merge_page(struct bio *bio, struct page *page,
942ff896738SChristoph Hellwig 		unsigned int len, unsigned int off, bool *same_page)
9430aa69fd3SChristoph Hellwig {
9440aa69fd3SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
9450aa69fd3SChristoph Hellwig 		return false;
9460aa69fd3SChristoph Hellwig 
947cc90bc68SAndreas Gruenbacher 	if (bio->bi_vcnt > 0) {
9480aa69fd3SChristoph Hellwig 		struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
9490aa69fd3SChristoph Hellwig 
9505919482eSMing Lei 		if (page_is_mergeable(bv, page, len, off, same_page)) {
95135c820e7SJens Axboe 			if (bio->bi_iter.bi_size > UINT_MAX - len) {
9522cd896a5SRitesh Harjani 				*same_page = false;
953cc90bc68SAndreas Gruenbacher 				return false;
9542cd896a5SRitesh Harjani 			}
9550aa69fd3SChristoph Hellwig 			bv->bv_len += len;
9560aa69fd3SChristoph Hellwig 			bio->bi_iter.bi_size += len;
9570aa69fd3SChristoph Hellwig 			return true;
9580aa69fd3SChristoph Hellwig 		}
9595919482eSMing Lei 	}
9600aa69fd3SChristoph Hellwig 	return false;
9610aa69fd3SChristoph Hellwig }
9620aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_try_merge_page);
9630aa69fd3SChristoph Hellwig 
9640aa69fd3SChristoph Hellwig /**
965551879a4SMing Lei  * __bio_add_page - add page(s) to a bio in a new segment
9660aa69fd3SChristoph Hellwig  * @bio: destination bio
967551879a4SMing Lei  * @page: start page to add
968551879a4SMing Lei  * @len: length of the data to add, may cross pages
969551879a4SMing Lei  * @off: offset of the data relative to @page, may cross pages
9700aa69fd3SChristoph Hellwig  *
9710aa69fd3SChristoph Hellwig  * Add the data at @page + @off to @bio as a new bvec.  The caller must ensure
9720aa69fd3SChristoph Hellwig  * that @bio has space for another bvec.
9730aa69fd3SChristoph Hellwig  */
9740aa69fd3SChristoph Hellwig void __bio_add_page(struct bio *bio, struct page *page,
9750aa69fd3SChristoph Hellwig 		unsigned int len, unsigned int off)
9760aa69fd3SChristoph Hellwig {
9770aa69fd3SChristoph Hellwig 	struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt];
9780aa69fd3SChristoph Hellwig 
9790aa69fd3SChristoph Hellwig 	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
98079d08f89SMing Lei 	WARN_ON_ONCE(bio_full(bio, len));
9810aa69fd3SChristoph Hellwig 
9820aa69fd3SChristoph Hellwig 	bv->bv_page = page;
9830aa69fd3SChristoph Hellwig 	bv->bv_offset = off;
9840aa69fd3SChristoph Hellwig 	bv->bv_len = len;
9850aa69fd3SChristoph Hellwig 
9860aa69fd3SChristoph Hellwig 	bio->bi_iter.bi_size += len;
9870aa69fd3SChristoph Hellwig 	bio->bi_vcnt++;
988b8e24a93SJohannes Weiner 
989b8e24a93SJohannes Weiner 	if (!bio_flagged(bio, BIO_WORKINGSET) && unlikely(PageWorkingset(page)))
990b8e24a93SJohannes Weiner 		bio_set_flag(bio, BIO_WORKINGSET);
9910aa69fd3SChristoph Hellwig }
9920aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_add_page);
9930aa69fd3SChristoph Hellwig 
9940aa69fd3SChristoph Hellwig /**
995551879a4SMing Lei  *	bio_add_page	-	attempt to add page(s) to bio
996f9c78b2bSJens Axboe  *	@bio: destination bio
997551879a4SMing Lei  *	@page: start page to add
998551879a4SMing Lei  *	@len: vec entry length, may cross pages
999551879a4SMing Lei  *	@offset: vec entry offset relative to @page, may cross pages
1000f9c78b2bSJens Axboe  *
1001551879a4SMing Lei  *	Attempt to add page(s) to the bio_vec maplist. This will only fail
1002c66a14d0SKent Overstreet  *	if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
1003f9c78b2bSJens Axboe  */
1004c66a14d0SKent Overstreet int bio_add_page(struct bio *bio, struct page *page,
1005c66a14d0SKent Overstreet 		 unsigned int len, unsigned int offset)
1006f9c78b2bSJens Axboe {
1007ff896738SChristoph Hellwig 	bool same_page = false;
1008ff896738SChristoph Hellwig 
1009ff896738SChristoph Hellwig 	if (!__bio_try_merge_page(bio, page, len, offset, &same_page)) {
101079d08f89SMing Lei 		if (bio_full(bio, len))
1011c66a14d0SKent Overstreet 			return 0;
10120aa69fd3SChristoph Hellwig 		__bio_add_page(bio, page, len, offset);
1013c66a14d0SKent Overstreet 	}
1014c66a14d0SKent Overstreet 	return len;
1015f9c78b2bSJens Axboe }
1016f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_page);
1017f9c78b2bSJens Axboe 
1018d241a95fSChristoph Hellwig void bio_release_pages(struct bio *bio, bool mark_dirty)
10197321ecbfSChristoph Hellwig {
10207321ecbfSChristoph Hellwig 	struct bvec_iter_all iter_all;
10217321ecbfSChristoph Hellwig 	struct bio_vec *bvec;
10227321ecbfSChristoph Hellwig 
1023b2d0d991SChristoph Hellwig 	if (bio_flagged(bio, BIO_NO_PAGE_REF))
1024b2d0d991SChristoph Hellwig 		return;
1025b2d0d991SChristoph Hellwig 
1026d241a95fSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
1027d241a95fSChristoph Hellwig 		if (mark_dirty && !PageCompound(bvec->bv_page))
1028d241a95fSChristoph Hellwig 			set_page_dirty_lock(bvec->bv_page);
10297321ecbfSChristoph Hellwig 		put_page(bvec->bv_page);
10307321ecbfSChristoph Hellwig 	}
1031d241a95fSChristoph Hellwig }
103229b2a3aaSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_release_pages);
10337321ecbfSChristoph Hellwig 
10347de55b7dSJohannes Thumshirn static void __bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
10356d0c48aeSJens Axboe {
10367a800a20SChristoph Hellwig 	WARN_ON_ONCE(bio->bi_max_vecs);
10376d0c48aeSJens Axboe 
1038c42bca92SPavel Begunkov 	bio->bi_vcnt = iter->nr_segs;
1039c42bca92SPavel Begunkov 	bio->bi_io_vec = (struct bio_vec *)iter->bvec;
1040c42bca92SPavel Begunkov 	bio->bi_iter.bi_bvec_done = iter->iov_offset;
1041c42bca92SPavel Begunkov 	bio->bi_iter.bi_size = iter->count;
1042ed97ce5eSChristoph Hellwig 	bio_set_flag(bio, BIO_NO_PAGE_REF);
1043977be012SChristoph Hellwig 	bio_set_flag(bio, BIO_CLONED);
10447de55b7dSJohannes Thumshirn }
10456d0c48aeSJens Axboe 
10467de55b7dSJohannes Thumshirn static int bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
10477de55b7dSJohannes Thumshirn {
10487de55b7dSJohannes Thumshirn 	__bio_iov_bvec_set(bio, iter);
1049c42bca92SPavel Begunkov 	iov_iter_advance(iter, iter->count);
10506d0c48aeSJens Axboe 	return 0;
10516d0c48aeSJens Axboe }
10526d0c48aeSJens Axboe 
10537de55b7dSJohannes Thumshirn static int bio_iov_bvec_set_append(struct bio *bio, struct iov_iter *iter)
10547de55b7dSJohannes Thumshirn {
10557de55b7dSJohannes Thumshirn 	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
10567de55b7dSJohannes Thumshirn 	struct iov_iter i = *iter;
10577de55b7dSJohannes Thumshirn 
10587de55b7dSJohannes Thumshirn 	iov_iter_truncate(&i, queue_max_zone_append_sectors(q) << 9);
10597de55b7dSJohannes Thumshirn 	__bio_iov_bvec_set(bio, &i);
10607de55b7dSJohannes Thumshirn 	iov_iter_advance(iter, i.count);
10617de55b7dSJohannes Thumshirn 	return 0;
10627de55b7dSJohannes Thumshirn }
10637de55b7dSJohannes Thumshirn 
1064d9cf3bd5SPavel Begunkov static void bio_put_pages(struct page **pages, size_t size, size_t off)
1065d9cf3bd5SPavel Begunkov {
1066d9cf3bd5SPavel Begunkov 	size_t i, nr = DIV_ROUND_UP(size + (off & ~PAGE_MASK), PAGE_SIZE);
1067d9cf3bd5SPavel Begunkov 
1068d9cf3bd5SPavel Begunkov 	for (i = 0; i < nr; i++)
1069d9cf3bd5SPavel Begunkov 		put_page(pages[i]);
1070d9cf3bd5SPavel Begunkov }
1071d9cf3bd5SPavel Begunkov 
1072576ed913SChristoph Hellwig #define PAGE_PTRS_PER_BVEC     (sizeof(struct bio_vec) / sizeof(struct page *))
1073576ed913SChristoph Hellwig 
10742cefe4dbSKent Overstreet /**
107517d51b10SMartin Wilck  * __bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
10762cefe4dbSKent Overstreet  * @bio: bio to add pages to
10772cefe4dbSKent Overstreet  * @iter: iov iterator describing the region to be mapped
10782cefe4dbSKent Overstreet  *
107917d51b10SMartin Wilck  * Pins pages from *iter and appends them to @bio's bvec array. The
10802cefe4dbSKent Overstreet  * pages will have to be released using put_page() when done.
108117d51b10SMartin Wilck  * For multi-segment *iter, this function only adds pages from the
10823cf14889SRandy Dunlap  * next non-empty segment of the iov iterator.
10832cefe4dbSKent Overstreet  */
108417d51b10SMartin Wilck static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
10852cefe4dbSKent Overstreet {
1086576ed913SChristoph Hellwig 	unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt;
1087576ed913SChristoph Hellwig 	unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt;
10882cefe4dbSKent Overstreet 	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
10892cefe4dbSKent Overstreet 	struct page **pages = (struct page **)bv;
109045691804SChristoph Hellwig 	bool same_page = false;
1091576ed913SChristoph Hellwig 	ssize_t size, left;
1092576ed913SChristoph Hellwig 	unsigned len, i;
1093b403ea24SMartin Wilck 	size_t offset;
1094576ed913SChristoph Hellwig 
1095576ed913SChristoph Hellwig 	/*
1096576ed913SChristoph Hellwig 	 * Move page array up in the allocated memory for the bio vecs as far as
1097576ed913SChristoph Hellwig 	 * possible so that we can start filling biovecs from the beginning
1098576ed913SChristoph Hellwig 	 * without overwriting the temporary page array.
1099576ed913SChristoph Hellwig 	*/
1100576ed913SChristoph Hellwig 	BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2);
1101576ed913SChristoph Hellwig 	pages += entries_left * (PAGE_PTRS_PER_BVEC - 1);
11022cefe4dbSKent Overstreet 
110335c820e7SJens Axboe 	size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset);
11042cefe4dbSKent Overstreet 	if (unlikely(size <= 0))
11052cefe4dbSKent Overstreet 		return size ? size : -EFAULT;
11062cefe4dbSKent Overstreet 
1107576ed913SChristoph Hellwig 	for (left = size, i = 0; left > 0; left -= len, i++) {
1108576ed913SChristoph Hellwig 		struct page *page = pages[i];
11092cefe4dbSKent Overstreet 
1110576ed913SChristoph Hellwig 		len = min_t(size_t, PAGE_SIZE - offset, left);
111145691804SChristoph Hellwig 
111245691804SChristoph Hellwig 		if (__bio_try_merge_page(bio, page, len, offset, &same_page)) {
111345691804SChristoph Hellwig 			if (same_page)
111445691804SChristoph Hellwig 				put_page(page);
111545691804SChristoph Hellwig 		} else {
1116d9cf3bd5SPavel Begunkov 			if (WARN_ON_ONCE(bio_full(bio, len))) {
1117d9cf3bd5SPavel Begunkov 				bio_put_pages(pages + i, left, offset);
1118576ed913SChristoph Hellwig 				return -EINVAL;
1119d9cf3bd5SPavel Begunkov 			}
112045691804SChristoph Hellwig 			__bio_add_page(bio, page, len, offset);
112145691804SChristoph Hellwig 		}
1122576ed913SChristoph Hellwig 		offset = 0;
11232cefe4dbSKent Overstreet 	}
11242cefe4dbSKent Overstreet 
11252cefe4dbSKent Overstreet 	iov_iter_advance(iter, size);
11262cefe4dbSKent Overstreet 	return 0;
11272cefe4dbSKent Overstreet }
112817d51b10SMartin Wilck 
11290512a75bSKeith Busch static int __bio_iov_append_get_pages(struct bio *bio, struct iov_iter *iter)
11300512a75bSKeith Busch {
11310512a75bSKeith Busch 	unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt;
11320512a75bSKeith Busch 	unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt;
1133309dca30SChristoph Hellwig 	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
11340512a75bSKeith Busch 	unsigned int max_append_sectors = queue_max_zone_append_sectors(q);
11350512a75bSKeith Busch 	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
11360512a75bSKeith Busch 	struct page **pages = (struct page **)bv;
11370512a75bSKeith Busch 	ssize_t size, left;
11380512a75bSKeith Busch 	unsigned len, i;
11390512a75bSKeith Busch 	size_t offset;
11404977d121SNaohiro Aota 	int ret = 0;
11410512a75bSKeith Busch 
11420512a75bSKeith Busch 	if (WARN_ON_ONCE(!max_append_sectors))
11430512a75bSKeith Busch 		return 0;
11440512a75bSKeith Busch 
11450512a75bSKeith Busch 	/*
11460512a75bSKeith Busch 	 * Move page array up in the allocated memory for the bio vecs as far as
11470512a75bSKeith Busch 	 * possible so that we can start filling biovecs from the beginning
11480512a75bSKeith Busch 	 * without overwriting the temporary page array.
11490512a75bSKeith Busch 	 */
11500512a75bSKeith Busch 	BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2);
11510512a75bSKeith Busch 	pages += entries_left * (PAGE_PTRS_PER_BVEC - 1);
11520512a75bSKeith Busch 
11530512a75bSKeith Busch 	size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset);
11540512a75bSKeith Busch 	if (unlikely(size <= 0))
11550512a75bSKeith Busch 		return size ? size : -EFAULT;
11560512a75bSKeith Busch 
11570512a75bSKeith Busch 	for (left = size, i = 0; left > 0; left -= len, i++) {
11580512a75bSKeith Busch 		struct page *page = pages[i];
11590512a75bSKeith Busch 		bool same_page = false;
11600512a75bSKeith Busch 
11610512a75bSKeith Busch 		len = min_t(size_t, PAGE_SIZE - offset, left);
11620512a75bSKeith Busch 		if (bio_add_hw_page(q, bio, page, len, offset,
11634977d121SNaohiro Aota 				max_append_sectors, &same_page) != len) {
1164d9cf3bd5SPavel Begunkov 			bio_put_pages(pages + i, left, offset);
11654977d121SNaohiro Aota 			ret = -EINVAL;
11664977d121SNaohiro Aota 			break;
11674977d121SNaohiro Aota 		}
11680512a75bSKeith Busch 		if (same_page)
11690512a75bSKeith Busch 			put_page(page);
11700512a75bSKeith Busch 		offset = 0;
11710512a75bSKeith Busch 	}
11720512a75bSKeith Busch 
11734977d121SNaohiro Aota 	iov_iter_advance(iter, size - left);
11744977d121SNaohiro Aota 	return ret;
11750512a75bSKeith Busch }
11760512a75bSKeith Busch 
117717d51b10SMartin Wilck /**
11786d0c48aeSJens Axboe  * bio_iov_iter_get_pages - add user or kernel pages to a bio
117917d51b10SMartin Wilck  * @bio: bio to add pages to
11806d0c48aeSJens Axboe  * @iter: iov iterator describing the region to be added
118117d51b10SMartin Wilck  *
11826d0c48aeSJens Axboe  * This takes either an iterator pointing to user memory, or one pointing to
11836d0c48aeSJens Axboe  * kernel pages (BVEC iterator). If we're adding user pages, we pin them and
11846d0c48aeSJens Axboe  * map them into the kernel. On IO completion, the caller should put those
1185c42bca92SPavel Begunkov  * pages. For bvec based iterators bio_iov_iter_get_pages() uses the provided
1186c42bca92SPavel Begunkov  * bvecs rather than copying them. Hence anyone issuing kiocb based IO needs
1187c42bca92SPavel Begunkov  * to ensure the bvecs and pages stay referenced until the submitted I/O is
1188c42bca92SPavel Begunkov  * completed by a call to ->ki_complete() or returns with an error other than
1189c42bca92SPavel Begunkov  * -EIOCBQUEUED. The caller needs to check if the bio is flagged BIO_NO_PAGE_REF
1190c42bca92SPavel Begunkov  * on IO completion. If it isn't, then pages should be released.
11916d0c48aeSJens Axboe  *
119217d51b10SMartin Wilck  * The function tries, but does not guarantee, to pin as many pages as
11935cd3ddc1SMauro Carvalho Chehab  * fit into the bio, or are requested in @iter, whatever is smaller. If
11946d0c48aeSJens Axboe  * MM encounters an error pinning the requested pages, it stops. Error
11956d0c48aeSJens Axboe  * is returned only if 0 pages could be pinned.
11960cf41e5eSPavel Begunkov  *
11970cf41e5eSPavel Begunkov  * It's intended for direct IO, so doesn't do PSI tracking, the caller is
11980cf41e5eSPavel Begunkov  * responsible for setting BIO_WORKINGSET if necessary.
119917d51b10SMartin Wilck  */
120017d51b10SMartin Wilck int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
120117d51b10SMartin Wilck {
1202c42bca92SPavel Begunkov 	int ret = 0;
120314eacf12SChristoph Hellwig 
1204c42bca92SPavel Begunkov 	if (iov_iter_is_bvec(iter)) {
12057de55b7dSJohannes Thumshirn 		if (bio_op(bio) == REQ_OP_ZONE_APPEND)
12067de55b7dSJohannes Thumshirn 			return bio_iov_bvec_set_append(bio, iter);
1207ed97ce5eSChristoph Hellwig 		return bio_iov_bvec_set(bio, iter);
120886004515SChristoph Hellwig 	}
120917d51b10SMartin Wilck 
121017d51b10SMartin Wilck 	do {
1211c42bca92SPavel Begunkov 		if (bio_op(bio) == REQ_OP_ZONE_APPEND)
12120512a75bSKeith Busch 			ret = __bio_iov_append_get_pages(bio, iter);
12136d0c48aeSJens Axboe 		else
12146d0c48aeSJens Axboe 			ret = __bio_iov_iter_get_pages(bio, iter);
121579d08f89SMing Lei 	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
121617d51b10SMartin Wilck 
12170cf41e5eSPavel Begunkov 	/* don't account direct I/O as memory stall */
12180cf41e5eSPavel Begunkov 	bio_clear_flag(bio, BIO_WORKINGSET);
121914eacf12SChristoph Hellwig 	return bio->bi_vcnt ? 0 : ret;
122017d51b10SMartin Wilck }
122129b2a3aaSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages);
12222cefe4dbSKent Overstreet 
12234246a0b6SChristoph Hellwig static void submit_bio_wait_endio(struct bio *bio)
1224f9c78b2bSJens Axboe {
122565e53aabSChristoph Hellwig 	complete(bio->bi_private);
1226f9c78b2bSJens Axboe }
1227f9c78b2bSJens Axboe 
1228f9c78b2bSJens Axboe /**
1229f9c78b2bSJens Axboe  * submit_bio_wait - submit a bio, and wait until it completes
1230f9c78b2bSJens Axboe  * @bio: The &struct bio which describes the I/O
1231f9c78b2bSJens Axboe  *
1232f9c78b2bSJens Axboe  * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
1233f9c78b2bSJens Axboe  * bio_endio() on failure.
12343d289d68SJan Kara  *
12353d289d68SJan Kara  * WARNING: Unlike to how submit_bio() is usually used, this function does not
12363d289d68SJan Kara  * result in bio reference to be consumed. The caller must drop the reference
12373d289d68SJan Kara  * on his own.
1238f9c78b2bSJens Axboe  */
12394e49ea4aSMike Christie int submit_bio_wait(struct bio *bio)
1240f9c78b2bSJens Axboe {
1241309dca30SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK_MAP(done,
1242309dca30SChristoph Hellwig 			bio->bi_bdev->bd_disk->lockdep_map);
1243de6a78b6SMing Lei 	unsigned long hang_check;
1244f9c78b2bSJens Axboe 
124565e53aabSChristoph Hellwig 	bio->bi_private = &done;
1246f9c78b2bSJens Axboe 	bio->bi_end_io = submit_bio_wait_endio;
12471eff9d32SJens Axboe 	bio->bi_opf |= REQ_SYNC;
12484e49ea4aSMike Christie 	submit_bio(bio);
1249de6a78b6SMing Lei 
1250de6a78b6SMing Lei 	/* Prevent hang_check timer from firing at us during very long I/O */
1251de6a78b6SMing Lei 	hang_check = sysctl_hung_task_timeout_secs;
1252de6a78b6SMing Lei 	if (hang_check)
1253de6a78b6SMing Lei 		while (!wait_for_completion_io_timeout(&done,
1254de6a78b6SMing Lei 					hang_check * (HZ/2)))
1255de6a78b6SMing Lei 			;
1256de6a78b6SMing Lei 	else
125765e53aabSChristoph Hellwig 		wait_for_completion_io(&done);
1258f9c78b2bSJens Axboe 
125965e53aabSChristoph Hellwig 	return blk_status_to_errno(bio->bi_status);
1260f9c78b2bSJens Axboe }
1261f9c78b2bSJens Axboe EXPORT_SYMBOL(submit_bio_wait);
1262f9c78b2bSJens Axboe 
1263f9c78b2bSJens Axboe /**
1264f9c78b2bSJens Axboe  * bio_advance - increment/complete a bio by some number of bytes
1265f9c78b2bSJens Axboe  * @bio:	bio to advance
1266f9c78b2bSJens Axboe  * @bytes:	number of bytes to complete
1267f9c78b2bSJens Axboe  *
1268f9c78b2bSJens Axboe  * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
1269f9c78b2bSJens Axboe  * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
1270f9c78b2bSJens Axboe  * be updated on the last bvec as well.
1271f9c78b2bSJens Axboe  *
1272f9c78b2bSJens Axboe  * @bio will then represent the remaining, uncompleted portion of the io.
1273f9c78b2bSJens Axboe  */
1274f9c78b2bSJens Axboe void bio_advance(struct bio *bio, unsigned bytes)
1275f9c78b2bSJens Axboe {
1276f9c78b2bSJens Axboe 	if (bio_integrity(bio))
1277f9c78b2bSJens Axboe 		bio_integrity_advance(bio, bytes);
1278f9c78b2bSJens Axboe 
1279a892c8d5SSatya Tangirala 	bio_crypt_advance(bio, bytes);
1280f9c78b2bSJens Axboe 	bio_advance_iter(bio, &bio->bi_iter, bytes);
1281f9c78b2bSJens Axboe }
1282f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_advance);
1283f9c78b2bSJens Axboe 
128445db54d5SKent Overstreet void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
128545db54d5SKent Overstreet 			struct bio *src, struct bvec_iter *src_iter)
1286f9c78b2bSJens Axboe {
128745db54d5SKent Overstreet 	while (src_iter->bi_size && dst_iter->bi_size) {
1288f8b679a0SChristoph Hellwig 		struct bio_vec src_bv = bio_iter_iovec(src, *src_iter);
1289f8b679a0SChristoph Hellwig 		struct bio_vec dst_bv = bio_iter_iovec(dst, *dst_iter);
1290f8b679a0SChristoph Hellwig 		unsigned int bytes = min(src_bv.bv_len, dst_bv.bv_len);
1291f8b679a0SChristoph Hellwig 		void *src_buf;
129245db54d5SKent Overstreet 
1293f8b679a0SChristoph Hellwig 		src_buf = bvec_kmap_local(&src_bv);
1294f8b679a0SChristoph Hellwig 		memcpy_to_bvec(&dst_bv, src_buf);
1295f8b679a0SChristoph Hellwig 		kunmap_local(src_buf);
12966e6e811dSKent Overstreet 
129722b56c29SPavel Begunkov 		bio_advance_iter_single(src, src_iter, bytes);
129822b56c29SPavel Begunkov 		bio_advance_iter_single(dst, dst_iter, bytes);
129945db54d5SKent Overstreet 	}
130045db54d5SKent Overstreet }
130145db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data_iter);
130245db54d5SKent Overstreet 
130345db54d5SKent Overstreet /**
130445db54d5SKent Overstreet  * bio_copy_data - copy contents of data buffers from one bio to another
130545db54d5SKent Overstreet  * @src: source bio
130645db54d5SKent Overstreet  * @dst: destination bio
130745db54d5SKent Overstreet  *
130845db54d5SKent Overstreet  * Stops when it reaches the end of either @src or @dst - that is, copies
130945db54d5SKent Overstreet  * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
131045db54d5SKent Overstreet  */
131145db54d5SKent Overstreet void bio_copy_data(struct bio *dst, struct bio *src)
131245db54d5SKent Overstreet {
131345db54d5SKent Overstreet 	struct bvec_iter src_iter = src->bi_iter;
131445db54d5SKent Overstreet 	struct bvec_iter dst_iter = dst->bi_iter;
131545db54d5SKent Overstreet 
131645db54d5SKent Overstreet 	bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
131745db54d5SKent Overstreet }
131845db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data);
131945db54d5SKent Overstreet 
1320491221f8SGuoqing Jiang void bio_free_pages(struct bio *bio)
13211dfa0f68SChristoph Hellwig {
13221dfa0f68SChristoph Hellwig 	struct bio_vec *bvec;
13236dc4f100SMing Lei 	struct bvec_iter_all iter_all;
13241dfa0f68SChristoph Hellwig 
13252b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all)
13261dfa0f68SChristoph Hellwig 		__free_page(bvec->bv_page);
13271dfa0f68SChristoph Hellwig }
1328491221f8SGuoqing Jiang EXPORT_SYMBOL(bio_free_pages);
13291dfa0f68SChristoph Hellwig 
1330f9c78b2bSJens Axboe /*
1331f9c78b2bSJens Axboe  * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
1332f9c78b2bSJens Axboe  * for performing direct-IO in BIOs.
1333f9c78b2bSJens Axboe  *
1334f9c78b2bSJens Axboe  * The problem is that we cannot run set_page_dirty() from interrupt context
1335f9c78b2bSJens Axboe  * because the required locks are not interrupt-safe.  So what we can do is to
1336f9c78b2bSJens Axboe  * mark the pages dirty _before_ performing IO.  And in interrupt context,
1337f9c78b2bSJens Axboe  * check that the pages are still dirty.   If so, fine.  If not, redirty them
1338f9c78b2bSJens Axboe  * in process context.
1339f9c78b2bSJens Axboe  *
1340f9c78b2bSJens Axboe  * We special-case compound pages here: normally this means reads into hugetlb
1341f9c78b2bSJens Axboe  * pages.  The logic in here doesn't really work right for compound pages
1342f9c78b2bSJens Axboe  * because the VM does not uniformly chase down the head page in all cases.
1343f9c78b2bSJens Axboe  * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
1344f9c78b2bSJens Axboe  * handle them at all.  So we skip compound pages here at an early stage.
1345f9c78b2bSJens Axboe  *
1346f9c78b2bSJens Axboe  * Note that this code is very hard to test under normal circumstances because
1347f9c78b2bSJens Axboe  * direct-io pins the pages with get_user_pages().  This makes
1348f9c78b2bSJens Axboe  * is_page_cache_freeable return false, and the VM will not clean the pages.
1349f9c78b2bSJens Axboe  * But other code (eg, flusher threads) could clean the pages if they are mapped
1350f9c78b2bSJens Axboe  * pagecache.
1351f9c78b2bSJens Axboe  *
1352f9c78b2bSJens Axboe  * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
1353f9c78b2bSJens Axboe  * deferred bio dirtying paths.
1354f9c78b2bSJens Axboe  */
1355f9c78b2bSJens Axboe 
1356f9c78b2bSJens Axboe /*
1357f9c78b2bSJens Axboe  * bio_set_pages_dirty() will mark all the bio's pages as dirty.
1358f9c78b2bSJens Axboe  */
1359f9c78b2bSJens Axboe void bio_set_pages_dirty(struct bio *bio)
1360f9c78b2bSJens Axboe {
1361f9c78b2bSJens Axboe 	struct bio_vec *bvec;
13626dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1363f9c78b2bSJens Axboe 
13642b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
13653bb50983SChristoph Hellwig 		if (!PageCompound(bvec->bv_page))
13663bb50983SChristoph Hellwig 			set_page_dirty_lock(bvec->bv_page);
1367f9c78b2bSJens Axboe 	}
1368f9c78b2bSJens Axboe }
1369f9c78b2bSJens Axboe 
1370f9c78b2bSJens Axboe /*
1371f9c78b2bSJens Axboe  * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
1372f9c78b2bSJens Axboe  * If they are, then fine.  If, however, some pages are clean then they must
1373f9c78b2bSJens Axboe  * have been written out during the direct-IO read.  So we take another ref on
137424d5493fSChristoph Hellwig  * the BIO and re-dirty the pages in process context.
1375f9c78b2bSJens Axboe  *
1376f9c78b2bSJens Axboe  * It is expected that bio_check_pages_dirty() will wholly own the BIO from
1377ea1754a0SKirill A. Shutemov  * here on.  It will run one put_page() against each page and will run one
1378ea1754a0SKirill A. Shutemov  * bio_put() against the BIO.
1379f9c78b2bSJens Axboe  */
1380f9c78b2bSJens Axboe 
1381f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work);
1382f9c78b2bSJens Axboe 
1383f9c78b2bSJens Axboe static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
1384f9c78b2bSJens Axboe static DEFINE_SPINLOCK(bio_dirty_lock);
1385f9c78b2bSJens Axboe static struct bio *bio_dirty_list;
1386f9c78b2bSJens Axboe 
1387f9c78b2bSJens Axboe /*
1388f9c78b2bSJens Axboe  * This runs in process context
1389f9c78b2bSJens Axboe  */
1390f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work)
1391f9c78b2bSJens Axboe {
139224d5493fSChristoph Hellwig 	struct bio *bio, *next;
1393f9c78b2bSJens Axboe 
139424d5493fSChristoph Hellwig 	spin_lock_irq(&bio_dirty_lock);
139524d5493fSChristoph Hellwig 	next = bio_dirty_list;
1396f9c78b2bSJens Axboe 	bio_dirty_list = NULL;
139724d5493fSChristoph Hellwig 	spin_unlock_irq(&bio_dirty_lock);
1398f9c78b2bSJens Axboe 
139924d5493fSChristoph Hellwig 	while ((bio = next) != NULL) {
140024d5493fSChristoph Hellwig 		next = bio->bi_private;
1401f9c78b2bSJens Axboe 
1402d241a95fSChristoph Hellwig 		bio_release_pages(bio, true);
1403f9c78b2bSJens Axboe 		bio_put(bio);
1404f9c78b2bSJens Axboe 	}
1405f9c78b2bSJens Axboe }
1406f9c78b2bSJens Axboe 
1407f9c78b2bSJens Axboe void bio_check_pages_dirty(struct bio *bio)
1408f9c78b2bSJens Axboe {
1409f9c78b2bSJens Axboe 	struct bio_vec *bvec;
141024d5493fSChristoph Hellwig 	unsigned long flags;
14116dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1412f9c78b2bSJens Axboe 
14132b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
141424d5493fSChristoph Hellwig 		if (!PageDirty(bvec->bv_page) && !PageCompound(bvec->bv_page))
141524d5493fSChristoph Hellwig 			goto defer;
1416f9c78b2bSJens Axboe 	}
1417f9c78b2bSJens Axboe 
1418d241a95fSChristoph Hellwig 	bio_release_pages(bio, false);
141924d5493fSChristoph Hellwig 	bio_put(bio);
142024d5493fSChristoph Hellwig 	return;
142124d5493fSChristoph Hellwig defer:
1422f9c78b2bSJens Axboe 	spin_lock_irqsave(&bio_dirty_lock, flags);
1423f9c78b2bSJens Axboe 	bio->bi_private = bio_dirty_list;
1424f9c78b2bSJens Axboe 	bio_dirty_list = bio;
1425f9c78b2bSJens Axboe 	spin_unlock_irqrestore(&bio_dirty_lock, flags);
1426f9c78b2bSJens Axboe 	schedule_work(&bio_dirty_work);
1427f9c78b2bSJens Axboe }
1428f9c78b2bSJens Axboe 
1429c4cf5261SJens Axboe static inline bool bio_remaining_done(struct bio *bio)
1430c4cf5261SJens Axboe {
1431c4cf5261SJens Axboe 	/*
1432c4cf5261SJens Axboe 	 * If we're not chaining, then ->__bi_remaining is always 1 and
1433c4cf5261SJens Axboe 	 * we always end io on the first invocation.
1434c4cf5261SJens Axboe 	 */
1435c4cf5261SJens Axboe 	if (!bio_flagged(bio, BIO_CHAIN))
1436c4cf5261SJens Axboe 		return true;
1437c4cf5261SJens Axboe 
1438c4cf5261SJens Axboe 	BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
1439c4cf5261SJens Axboe 
1440326e1dbbSMike Snitzer 	if (atomic_dec_and_test(&bio->__bi_remaining)) {
1441b7c44ed9SJens Axboe 		bio_clear_flag(bio, BIO_CHAIN);
1442c4cf5261SJens Axboe 		return true;
1443326e1dbbSMike Snitzer 	}
1444c4cf5261SJens Axboe 
1445c4cf5261SJens Axboe 	return false;
1446c4cf5261SJens Axboe }
1447c4cf5261SJens Axboe 
1448f9c78b2bSJens Axboe /**
1449f9c78b2bSJens Axboe  * bio_endio - end I/O on a bio
1450f9c78b2bSJens Axboe  * @bio:	bio
1451f9c78b2bSJens Axboe  *
1452f9c78b2bSJens Axboe  * Description:
14534246a0b6SChristoph Hellwig  *   bio_endio() will end I/O on the whole bio. bio_endio() is the preferred
14544246a0b6SChristoph Hellwig  *   way to end I/O on a bio. No one should call bi_end_io() directly on a
14554246a0b6SChristoph Hellwig  *   bio unless they own it and thus know that it has an end_io function.
1456fbbaf700SNeilBrown  *
1457fbbaf700SNeilBrown  *   bio_endio() can be called several times on a bio that has been chained
1458fbbaf700SNeilBrown  *   using bio_chain().  The ->bi_end_io() function will only be called the
145960b6a7e6SEdward Hsieh  *   last time.
1460f9c78b2bSJens Axboe  **/
14614246a0b6SChristoph Hellwig void bio_endio(struct bio *bio)
1462f9c78b2bSJens Axboe {
1463ba8c6967SChristoph Hellwig again:
14642b885517SChristoph Hellwig 	if (!bio_remaining_done(bio))
1465ba8c6967SChristoph Hellwig 		return;
14667c20f116SChristoph Hellwig 	if (!bio_integrity_endio(bio))
14677c20f116SChristoph Hellwig 		return;
1468f9c78b2bSJens Axboe 
1469a647a524SMing Lei 	if (bio->bi_bdev && bio_flagged(bio, BIO_TRACKED))
1470309dca30SChristoph Hellwig 		rq_qos_done_bio(bio->bi_bdev->bd_disk->queue, bio);
147167b42d0bSJosef Bacik 
147260b6a7e6SEdward Hsieh 	if (bio->bi_bdev && bio_flagged(bio, BIO_TRACE_COMPLETION)) {
147360b6a7e6SEdward Hsieh 		trace_block_bio_complete(bio->bi_bdev->bd_disk->queue, bio);
147460b6a7e6SEdward Hsieh 		bio_clear_flag(bio, BIO_TRACE_COMPLETION);
147560b6a7e6SEdward Hsieh 	}
147660b6a7e6SEdward Hsieh 
1477f9c78b2bSJens Axboe 	/*
1478ba8c6967SChristoph Hellwig 	 * Need to have a real endio function for chained bios, otherwise
1479ba8c6967SChristoph Hellwig 	 * various corner cases will break (like stacking block devices that
1480ba8c6967SChristoph Hellwig 	 * save/restore bi_end_io) - however, we want to avoid unbounded
1481ba8c6967SChristoph Hellwig 	 * recursion and blowing the stack. Tail call optimization would
1482ba8c6967SChristoph Hellwig 	 * handle this, but compiling with frame pointers also disables
1483ba8c6967SChristoph Hellwig 	 * gcc's sibling call optimization.
1484f9c78b2bSJens Axboe 	 */
1485f9c78b2bSJens Axboe 	if (bio->bi_end_io == bio_chain_endio) {
148638f8baaeSChristoph Hellwig 		bio = __bio_chain_endio(bio);
1487ba8c6967SChristoph Hellwig 		goto again;
1488ba8c6967SChristoph Hellwig 	}
1489ba8c6967SChristoph Hellwig 
14909e234eeaSShaohua Li 	blk_throtl_bio_endio(bio);
1491b222dd2fSShaohua Li 	/* release cgroup info */
1492b222dd2fSShaohua Li 	bio_uninit(bio);
1493f9c78b2bSJens Axboe 	if (bio->bi_end_io)
14944246a0b6SChristoph Hellwig 		bio->bi_end_io(bio);
1495f9c78b2bSJens Axboe }
1496f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_endio);
1497f9c78b2bSJens Axboe 
1498f9c78b2bSJens Axboe /**
1499f9c78b2bSJens Axboe  * bio_split - split a bio
1500f9c78b2bSJens Axboe  * @bio:	bio to split
1501f9c78b2bSJens Axboe  * @sectors:	number of sectors to split from the front of @bio
1502f9c78b2bSJens Axboe  * @gfp:	gfp mask
1503f9c78b2bSJens Axboe  * @bs:		bio set to allocate from
1504f9c78b2bSJens Axboe  *
1505f9c78b2bSJens Axboe  * Allocates and returns a new bio which represents @sectors from the start of
1506f9c78b2bSJens Axboe  * @bio, and updates @bio to represent the remaining sectors.
1507f9c78b2bSJens Axboe  *
1508f3f5da62SMartin K. Petersen  * Unless this is a discard request the newly allocated bio will point
1509dad77584SBart Van Assche  * to @bio's bi_io_vec. It is the caller's responsibility to ensure that
1510dad77584SBart Van Assche  * neither @bio nor @bs are freed before the split bio.
1511f9c78b2bSJens Axboe  */
1512f9c78b2bSJens Axboe struct bio *bio_split(struct bio *bio, int sectors,
1513f9c78b2bSJens Axboe 		      gfp_t gfp, struct bio_set *bs)
1514f9c78b2bSJens Axboe {
1515f341a4d3SMikulas Patocka 	struct bio *split;
1516f9c78b2bSJens Axboe 
1517f9c78b2bSJens Axboe 	BUG_ON(sectors <= 0);
1518f9c78b2bSJens Axboe 	BUG_ON(sectors >= bio_sectors(bio));
1519f9c78b2bSJens Axboe 
15200512a75bSKeith Busch 	/* Zone append commands cannot be split */
15210512a75bSKeith Busch 	if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
15220512a75bSKeith Busch 		return NULL;
15230512a75bSKeith Busch 
1524f9c78b2bSJens Axboe 	split = bio_clone_fast(bio, gfp, bs);
1525f9c78b2bSJens Axboe 	if (!split)
1526f9c78b2bSJens Axboe 		return NULL;
1527f9c78b2bSJens Axboe 
1528f9c78b2bSJens Axboe 	split->bi_iter.bi_size = sectors << 9;
1529f9c78b2bSJens Axboe 
1530f9c78b2bSJens Axboe 	if (bio_integrity(split))
1531fbd08e76SDmitry Monakhov 		bio_integrity_trim(split);
1532f9c78b2bSJens Axboe 
1533f9c78b2bSJens Axboe 	bio_advance(bio, split->bi_iter.bi_size);
1534f9c78b2bSJens Axboe 
1535fbbaf700SNeilBrown 	if (bio_flagged(bio, BIO_TRACE_COMPLETION))
153620d59023SGoldwyn Rodrigues 		bio_set_flag(split, BIO_TRACE_COMPLETION);
1537fbbaf700SNeilBrown 
1538f9c78b2bSJens Axboe 	return split;
1539f9c78b2bSJens Axboe }
1540f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_split);
1541f9c78b2bSJens Axboe 
1542f9c78b2bSJens Axboe /**
1543f9c78b2bSJens Axboe  * bio_trim - trim a bio
1544f9c78b2bSJens Axboe  * @bio:	bio to trim
1545f9c78b2bSJens Axboe  * @offset:	number of sectors to trim from the front of @bio
1546f9c78b2bSJens Axboe  * @size:	size we want to trim @bio to, in sectors
1547e83502caSChaitanya Kulkarni  *
1548e83502caSChaitanya Kulkarni  * This function is typically used for bios that are cloned and submitted
1549e83502caSChaitanya Kulkarni  * to the underlying device in parts.
1550f9c78b2bSJens Axboe  */
1551e83502caSChaitanya Kulkarni void bio_trim(struct bio *bio, sector_t offset, sector_t size)
1552f9c78b2bSJens Axboe {
1553e83502caSChaitanya Kulkarni 	if (WARN_ON_ONCE(offset > BIO_MAX_SECTORS || size > BIO_MAX_SECTORS ||
1554e83502caSChaitanya Kulkarni 			 offset + size > bio->bi_iter.bi_size))
1555e83502caSChaitanya Kulkarni 		return;
1556f9c78b2bSJens Axboe 
1557f9c78b2bSJens Axboe 	size <<= 9;
1558f9c78b2bSJens Axboe 	if (offset == 0 && size == bio->bi_iter.bi_size)
1559f9c78b2bSJens Axboe 		return;
1560f9c78b2bSJens Axboe 
1561f9c78b2bSJens Axboe 	bio_advance(bio, offset << 9);
1562f9c78b2bSJens Axboe 	bio->bi_iter.bi_size = size;
1563376a78abSDmitry Monakhov 
1564376a78abSDmitry Monakhov 	if (bio_integrity(bio))
1565fbd08e76SDmitry Monakhov 		bio_integrity_trim(bio);
1566f9c78b2bSJens Axboe }
1567f9c78b2bSJens Axboe EXPORT_SYMBOL_GPL(bio_trim);
1568f9c78b2bSJens Axboe 
1569f9c78b2bSJens Axboe /*
1570f9c78b2bSJens Axboe  * create memory pools for biovec's in a bio_set.
1571f9c78b2bSJens Axboe  * use the global biovec slabs created for general use.
1572f9c78b2bSJens Axboe  */
15738aa6ba2fSKent Overstreet int biovec_init_pool(mempool_t *pool, int pool_entries)
1574f9c78b2bSJens Axboe {
15757a800a20SChristoph Hellwig 	struct biovec_slab *bp = bvec_slabs + ARRAY_SIZE(bvec_slabs) - 1;
1576f9c78b2bSJens Axboe 
15778aa6ba2fSKent Overstreet 	return mempool_init_slab_pool(pool, pool_entries, bp->slab);
1578f9c78b2bSJens Axboe }
1579f9c78b2bSJens Axboe 
1580917a38c7SKent Overstreet /*
1581917a38c7SKent Overstreet  * bioset_exit - exit a bioset initialized with bioset_init()
1582917a38c7SKent Overstreet  *
1583917a38c7SKent Overstreet  * May be called on a zeroed but uninitialized bioset (i.e. allocated with
1584917a38c7SKent Overstreet  * kzalloc()).
1585917a38c7SKent Overstreet  */
1586917a38c7SKent Overstreet void bioset_exit(struct bio_set *bs)
1587f9c78b2bSJens Axboe {
1588be4d234dSJens Axboe 	bio_alloc_cache_destroy(bs);
1589f9c78b2bSJens Axboe 	if (bs->rescue_workqueue)
1590f9c78b2bSJens Axboe 		destroy_workqueue(bs->rescue_workqueue);
1591917a38c7SKent Overstreet 	bs->rescue_workqueue = NULL;
1592f9c78b2bSJens Axboe 
15938aa6ba2fSKent Overstreet 	mempool_exit(&bs->bio_pool);
15948aa6ba2fSKent Overstreet 	mempool_exit(&bs->bvec_pool);
1595f9c78b2bSJens Axboe 
1596f9c78b2bSJens Axboe 	bioset_integrity_free(bs);
1597917a38c7SKent Overstreet 	if (bs->bio_slab)
1598f9c78b2bSJens Axboe 		bio_put_slab(bs);
1599917a38c7SKent Overstreet 	bs->bio_slab = NULL;
1600917a38c7SKent Overstreet }
1601917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_exit);
1602f9c78b2bSJens Axboe 
1603011067b0SNeilBrown /**
1604917a38c7SKent Overstreet  * bioset_init - Initialize a bio_set
1605dad08527SKent Overstreet  * @bs:		pool to initialize
1606917a38c7SKent Overstreet  * @pool_size:	Number of bio and bio_vecs to cache in the mempool
1607917a38c7SKent Overstreet  * @front_pad:	Number of bytes to allocate in front of the returned bio
1608917a38c7SKent Overstreet  * @flags:	Flags to modify behavior, currently %BIOSET_NEED_BVECS
1609917a38c7SKent Overstreet  *              and %BIOSET_NEED_RESCUER
1610917a38c7SKent Overstreet  *
1611dad08527SKent Overstreet  * Description:
1612dad08527SKent Overstreet  *    Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
1613dad08527SKent Overstreet  *    to ask for a number of bytes to be allocated in front of the bio.
1614dad08527SKent Overstreet  *    Front pad allocation is useful for embedding the bio inside
1615dad08527SKent Overstreet  *    another structure, to avoid allocating extra data to go with the bio.
1616dad08527SKent Overstreet  *    Note that the bio must be embedded at the END of that structure always,
1617dad08527SKent Overstreet  *    or things will break badly.
1618dad08527SKent Overstreet  *    If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated
1619dad08527SKent Overstreet  *    for allocating iovecs.  This pool is not needed e.g. for bio_clone_fast().
1620dad08527SKent Overstreet  *    If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used to
1621dad08527SKent Overstreet  *    dispatch queued requests when the mempool runs out of space.
1622dad08527SKent Overstreet  *
1623917a38c7SKent Overstreet  */
1624917a38c7SKent Overstreet int bioset_init(struct bio_set *bs,
1625917a38c7SKent Overstreet 		unsigned int pool_size,
1626917a38c7SKent Overstreet 		unsigned int front_pad,
1627917a38c7SKent Overstreet 		int flags)
1628917a38c7SKent Overstreet {
1629917a38c7SKent Overstreet 	bs->front_pad = front_pad;
16309f180e31SMing Lei 	if (flags & BIOSET_NEED_BVECS)
16319f180e31SMing Lei 		bs->back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
16329f180e31SMing Lei 	else
16339f180e31SMing Lei 		bs->back_pad = 0;
1634917a38c7SKent Overstreet 
1635917a38c7SKent Overstreet 	spin_lock_init(&bs->rescue_lock);
1636917a38c7SKent Overstreet 	bio_list_init(&bs->rescue_list);
1637917a38c7SKent Overstreet 	INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1638917a38c7SKent Overstreet 
163949d1ec85SMing Lei 	bs->bio_slab = bio_find_or_create_slab(bs);
1640917a38c7SKent Overstreet 	if (!bs->bio_slab)
1641917a38c7SKent Overstreet 		return -ENOMEM;
1642917a38c7SKent Overstreet 
1643917a38c7SKent Overstreet 	if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab))
1644917a38c7SKent Overstreet 		goto bad;
1645917a38c7SKent Overstreet 
1646917a38c7SKent Overstreet 	if ((flags & BIOSET_NEED_BVECS) &&
1647917a38c7SKent Overstreet 	    biovec_init_pool(&bs->bvec_pool, pool_size))
1648917a38c7SKent Overstreet 		goto bad;
1649917a38c7SKent Overstreet 
1650be4d234dSJens Axboe 	if (flags & BIOSET_NEED_RESCUER) {
1651be4d234dSJens Axboe 		bs->rescue_workqueue = alloc_workqueue("bioset",
1652be4d234dSJens Axboe 							WQ_MEM_RECLAIM, 0);
1653917a38c7SKent Overstreet 		if (!bs->rescue_workqueue)
1654917a38c7SKent Overstreet 			goto bad;
1655be4d234dSJens Axboe 	}
1656be4d234dSJens Axboe 	if (flags & BIOSET_PERCPU_CACHE) {
1657be4d234dSJens Axboe 		bs->cache = alloc_percpu(struct bio_alloc_cache);
1658be4d234dSJens Axboe 		if (!bs->cache)
1659be4d234dSJens Axboe 			goto bad;
1660be4d234dSJens Axboe 		cpuhp_state_add_instance_nocalls(CPUHP_BIO_DEAD, &bs->cpuhp_dead);
1661be4d234dSJens Axboe 	}
1662917a38c7SKent Overstreet 
1663917a38c7SKent Overstreet 	return 0;
1664917a38c7SKent Overstreet bad:
1665917a38c7SKent Overstreet 	bioset_exit(bs);
1666917a38c7SKent Overstreet 	return -ENOMEM;
1667917a38c7SKent Overstreet }
1668917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_init);
1669917a38c7SKent Overstreet 
167028e89fd9SJens Axboe /*
167128e89fd9SJens Axboe  * Initialize and setup a new bio_set, based on the settings from
167228e89fd9SJens Axboe  * another bio_set.
167328e89fd9SJens Axboe  */
167428e89fd9SJens Axboe int bioset_init_from_src(struct bio_set *bs, struct bio_set *src)
167528e89fd9SJens Axboe {
167628e89fd9SJens Axboe 	int flags;
167728e89fd9SJens Axboe 
167828e89fd9SJens Axboe 	flags = 0;
167928e89fd9SJens Axboe 	if (src->bvec_pool.min_nr)
168028e89fd9SJens Axboe 		flags |= BIOSET_NEED_BVECS;
168128e89fd9SJens Axboe 	if (src->rescue_workqueue)
168228e89fd9SJens Axboe 		flags |= BIOSET_NEED_RESCUER;
168328e89fd9SJens Axboe 
168428e89fd9SJens Axboe 	return bioset_init(bs, src->bio_pool.min_nr, src->front_pad, flags);
168528e89fd9SJens Axboe }
168628e89fd9SJens Axboe EXPORT_SYMBOL(bioset_init_from_src);
168728e89fd9SJens Axboe 
1688be4d234dSJens Axboe /**
1689be4d234dSJens Axboe  * bio_alloc_kiocb - Allocate a bio from bio_set based on kiocb
1690be4d234dSJens Axboe  * @kiocb:	kiocb describing the IO
16910ef47db1SJens Axboe  * @nr_vecs:	number of iovecs to pre-allocate
1692be4d234dSJens Axboe  * @bs:		bio_set to allocate from
1693be4d234dSJens Axboe  *
1694be4d234dSJens Axboe  * Description:
1695be4d234dSJens Axboe  *    Like @bio_alloc_bioset, but pass in the kiocb. The kiocb is only
1696be4d234dSJens Axboe  *    used to check if we should dip into the per-cpu bio_set allocation
16973d5b3fbeSJens Axboe  *    cache. The allocation uses GFP_KERNEL internally. On return, the
16983d5b3fbeSJens Axboe  *    bio is marked BIO_PERCPU_CACHEABLE, and the final put of the bio
16993d5b3fbeSJens Axboe  *    MUST be done from process context, not hard/soft IRQ.
1700be4d234dSJens Axboe  *
1701be4d234dSJens Axboe  */
1702be4d234dSJens Axboe struct bio *bio_alloc_kiocb(struct kiocb *kiocb, unsigned short nr_vecs,
1703be4d234dSJens Axboe 			    struct bio_set *bs)
1704be4d234dSJens Axboe {
1705be4d234dSJens Axboe 	struct bio_alloc_cache *cache;
1706be4d234dSJens Axboe 	struct bio *bio;
1707be4d234dSJens Axboe 
1708be4d234dSJens Axboe 	if (!(kiocb->ki_flags & IOCB_ALLOC_CACHE) || nr_vecs > BIO_INLINE_VECS)
1709be4d234dSJens Axboe 		return bio_alloc_bioset(GFP_KERNEL, nr_vecs, bs);
1710be4d234dSJens Axboe 
1711be4d234dSJens Axboe 	cache = per_cpu_ptr(bs->cache, get_cpu());
1712be4d234dSJens Axboe 	bio = bio_list_pop(&cache->free_list);
1713be4d234dSJens Axboe 	if (bio) {
1714be4d234dSJens Axboe 		cache->nr--;
1715be4d234dSJens Axboe 		put_cpu();
1716be4d234dSJens Axboe 		bio_init(bio, nr_vecs ? bio->bi_inline_vecs : NULL, nr_vecs);
1717be4d234dSJens Axboe 		bio->bi_pool = bs;
1718be4d234dSJens Axboe 		bio_set_flag(bio, BIO_PERCPU_CACHE);
1719be4d234dSJens Axboe 		return bio;
1720be4d234dSJens Axboe 	}
1721be4d234dSJens Axboe 	put_cpu();
1722be4d234dSJens Axboe 	bio = bio_alloc_bioset(GFP_KERNEL, nr_vecs, bs);
1723be4d234dSJens Axboe 	bio_set_flag(bio, BIO_PERCPU_CACHE);
1724be4d234dSJens Axboe 	return bio;
1725be4d234dSJens Axboe }
1726be4d234dSJens Axboe EXPORT_SYMBOL_GPL(bio_alloc_kiocb);
1727be4d234dSJens Axboe 
1728de76fd89SChristoph Hellwig static int __init init_bio(void)
1729f9c78b2bSJens Axboe {
1730f9c78b2bSJens Axboe 	int i;
1731f9c78b2bSJens Axboe 
1732f9c78b2bSJens Axboe 	bio_integrity_init();
1733de76fd89SChristoph Hellwig 
1734de76fd89SChristoph Hellwig 	for (i = 0; i < ARRAY_SIZE(bvec_slabs); i++) {
1735f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + i;
1736f9c78b2bSJens Axboe 
1737de76fd89SChristoph Hellwig 		bvs->slab = kmem_cache_create(bvs->name,
1738de76fd89SChristoph Hellwig 				bvs->nr_vecs * sizeof(struct bio_vec), 0,
1739f9c78b2bSJens Axboe 				SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
1740f9c78b2bSJens Axboe 	}
1741f9c78b2bSJens Axboe 
1742be4d234dSJens Axboe 	cpuhp_setup_state_multi(CPUHP_BIO_DEAD, "block/bio:dead", NULL,
1743be4d234dSJens Axboe 					bio_cpu_dead);
1744be4d234dSJens Axboe 
1745f4f8154aSKent Overstreet 	if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS))
1746f9c78b2bSJens Axboe 		panic("bio: can't allocate bios\n");
1747f9c78b2bSJens Axboe 
1748f4f8154aSKent Overstreet 	if (bioset_integrity_create(&fs_bio_set, BIO_POOL_SIZE))
1749f9c78b2bSJens Axboe 		panic("bio: can't create integrity pool\n");
1750f9c78b2bSJens Axboe 
1751f9c78b2bSJens Axboe 	return 0;
1752f9c78b2bSJens Axboe }
1753f9c78b2bSJens Axboe subsys_initcall(init_bio);
1754