xref: /openbmc/linux/block/bio.c (revision 56b4b5ab)
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 {
29fcade2ceSJens Axboe 	struct bio		*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,
901a7e76e4SChristoph Hellwig 			ARCH_KMALLOC_MINALIGN,
911a7e76e4SChristoph Hellwig 			SLAB_HWCACHE_ALIGN | SLAB_TYPESAFE_BY_RCU, NULL);
9249d1ec85SMing Lei 	if (!bslab->slab)
9349d1ec85SMing Lei 		goto fail_alloc_slab;
9449d1ec85SMing Lei 
9549d1ec85SMing Lei 	bslab->slab_ref = 1;
9649d1ec85SMing Lei 	bslab->slab_size = size;
9749d1ec85SMing Lei 
9849d1ec85SMing Lei 	if (!xa_err(xa_store(&bio_slabs, size, bslab, GFP_KERNEL)))
9949d1ec85SMing Lei 		return bslab;
10049d1ec85SMing Lei 
10149d1ec85SMing Lei 	kmem_cache_destroy(bslab->slab);
10249d1ec85SMing Lei 
10349d1ec85SMing Lei fail_alloc_slab:
10449d1ec85SMing Lei 	kfree(bslab);
10549d1ec85SMing Lei 	return NULL;
10649d1ec85SMing Lei }
10749d1ec85SMing Lei 
10849d1ec85SMing Lei static inline unsigned int bs_bio_slab_size(struct bio_set *bs)
10949d1ec85SMing Lei {
1109f180e31SMing Lei 	return bs->front_pad + sizeof(struct bio) + bs->back_pad;
11149d1ec85SMing Lei }
11249d1ec85SMing Lei 
11349d1ec85SMing Lei static struct kmem_cache *bio_find_or_create_slab(struct bio_set *bs)
11449d1ec85SMing Lei {
11549d1ec85SMing Lei 	unsigned int size = bs_bio_slab_size(bs);
11649d1ec85SMing Lei 	struct bio_slab *bslab;
117f9c78b2bSJens Axboe 
118f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
11949d1ec85SMing Lei 	bslab = xa_load(&bio_slabs, size);
12049d1ec85SMing Lei 	if (bslab)
121f9c78b2bSJens Axboe 		bslab->slab_ref++;
12249d1ec85SMing Lei 	else
12349d1ec85SMing Lei 		bslab = create_bio_slab(size);
124f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
12549d1ec85SMing Lei 
12649d1ec85SMing Lei 	if (bslab)
12749d1ec85SMing Lei 		return bslab->slab;
12849d1ec85SMing Lei 	return NULL;
129f9c78b2bSJens Axboe }
130f9c78b2bSJens Axboe 
131f9c78b2bSJens Axboe static void bio_put_slab(struct bio_set *bs)
132f9c78b2bSJens Axboe {
133f9c78b2bSJens Axboe 	struct bio_slab *bslab = NULL;
13449d1ec85SMing Lei 	unsigned int slab_size = bs_bio_slab_size(bs);
135f9c78b2bSJens Axboe 
136f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
137f9c78b2bSJens Axboe 
13849d1ec85SMing Lei 	bslab = xa_load(&bio_slabs, slab_size);
139f9c78b2bSJens Axboe 	if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
140f9c78b2bSJens Axboe 		goto out;
141f9c78b2bSJens Axboe 
14249d1ec85SMing Lei 	WARN_ON_ONCE(bslab->slab != bs->bio_slab);
14349d1ec85SMing Lei 
144f9c78b2bSJens Axboe 	WARN_ON(!bslab->slab_ref);
145f9c78b2bSJens Axboe 
146f9c78b2bSJens Axboe 	if (--bslab->slab_ref)
147f9c78b2bSJens Axboe 		goto out;
148f9c78b2bSJens Axboe 
14949d1ec85SMing Lei 	xa_erase(&bio_slabs, slab_size);
15049d1ec85SMing Lei 
151f9c78b2bSJens Axboe 	kmem_cache_destroy(bslab->slab);
15249d1ec85SMing Lei 	kfree(bslab);
153f9c78b2bSJens Axboe 
154f9c78b2bSJens Axboe out:
155f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
156f9c78b2bSJens Axboe }
157f9c78b2bSJens Axboe 
1587a800a20SChristoph Hellwig void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned short nr_vecs)
159f9c78b2bSJens Axboe {
1609e8c0d0dSChristoph Hellwig 	BUG_ON(nr_vecs > BIO_MAX_VECS);
161f9c78b2bSJens Axboe 
162a8affc03SChristoph Hellwig 	if (nr_vecs == BIO_MAX_VECS)
163f9c78b2bSJens Axboe 		mempool_free(bv, pool);
1647a800a20SChristoph Hellwig 	else if (nr_vecs > BIO_INLINE_VECS)
1657a800a20SChristoph Hellwig 		kmem_cache_free(biovec_slab(nr_vecs)->slab, bv);
166f9c78b2bSJens Axboe }
167f9c78b2bSJens Axboe 
168f2c3eb9bSChristoph Hellwig /*
169f2c3eb9bSChristoph Hellwig  * Make the first allocation restricted and don't dump info on allocation
170f2c3eb9bSChristoph Hellwig  * failures, since we'll fall back to the mempool in case of failure.
171f2c3eb9bSChristoph Hellwig  */
172f2c3eb9bSChristoph Hellwig static inline gfp_t bvec_alloc_gfp(gfp_t gfp)
173f9c78b2bSJens Axboe {
174f2c3eb9bSChristoph Hellwig 	return (gfp & ~(__GFP_DIRECT_RECLAIM | __GFP_IO)) |
175f2c3eb9bSChristoph Hellwig 		__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
176f2c3eb9bSChristoph Hellwig }
177f2c3eb9bSChristoph Hellwig 
1787a800a20SChristoph Hellwig struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs,
1797a800a20SChristoph Hellwig 		gfp_t gfp_mask)
180f9c78b2bSJens Axboe {
1817a800a20SChristoph Hellwig 	struct biovec_slab *bvs = biovec_slab(*nr_vecs);
1827a800a20SChristoph Hellwig 
1837a800a20SChristoph Hellwig 	if (WARN_ON_ONCE(!bvs))
184f9c78b2bSJens Axboe 		return NULL;
1857a800a20SChristoph Hellwig 
1867a800a20SChristoph Hellwig 	/*
1877a800a20SChristoph Hellwig 	 * Upgrade the nr_vecs request to take full advantage of the allocation.
1887a800a20SChristoph Hellwig 	 * We also rely on this in the bvec_free path.
1897a800a20SChristoph Hellwig 	 */
1907a800a20SChristoph Hellwig 	*nr_vecs = bvs->nr_vecs;
191f9c78b2bSJens Axboe 
192f9c78b2bSJens Axboe 	/*
193f007a3d6SChristoph Hellwig 	 * Try a slab allocation first for all smaller allocations.  If that
194f007a3d6SChristoph Hellwig 	 * fails and __GFP_DIRECT_RECLAIM is set retry with the mempool.
195a8affc03SChristoph Hellwig 	 * The mempool is sized to handle up to BIO_MAX_VECS entries.
196f9c78b2bSJens Axboe 	 */
197a8affc03SChristoph Hellwig 	if (*nr_vecs < BIO_MAX_VECS) {
198f9c78b2bSJens Axboe 		struct bio_vec *bvl;
199f9c78b2bSJens Axboe 
200f2c3eb9bSChristoph Hellwig 		bvl = kmem_cache_alloc(bvs->slab, bvec_alloc_gfp(gfp_mask));
2017a800a20SChristoph Hellwig 		if (likely(bvl) || !(gfp_mask & __GFP_DIRECT_RECLAIM))
202f9c78b2bSJens Axboe 			return bvl;
203a8affc03SChristoph Hellwig 		*nr_vecs = BIO_MAX_VECS;
204f9c78b2bSJens Axboe 	}
205f9c78b2bSJens Axboe 
206f007a3d6SChristoph Hellwig 	return mempool_alloc(pool, gfp_mask);
207f9c78b2bSJens Axboe }
208f9c78b2bSJens Axboe 
2099ae3b3f5SJens Axboe void bio_uninit(struct bio *bio)
210f9c78b2bSJens Axboe {
211db9819c7SChristoph Hellwig #ifdef CONFIG_BLK_CGROUP
212db9819c7SChristoph Hellwig 	if (bio->bi_blkg) {
213db9819c7SChristoph Hellwig 		blkg_put(bio->bi_blkg);
214db9819c7SChristoph Hellwig 		bio->bi_blkg = NULL;
215db9819c7SChristoph Hellwig 	}
216db9819c7SChristoph Hellwig #endif
217ece841abSJustin Tee 	if (bio_integrity(bio))
218ece841abSJustin Tee 		bio_integrity_free(bio);
219a892c8d5SSatya Tangirala 
220a892c8d5SSatya Tangirala 	bio_crypt_free_ctx(bio);
221f9c78b2bSJens Axboe }
2229ae3b3f5SJens Axboe EXPORT_SYMBOL(bio_uninit);
223f9c78b2bSJens Axboe 
224f9c78b2bSJens Axboe static void bio_free(struct bio *bio)
225f9c78b2bSJens Axboe {
226f9c78b2bSJens Axboe 	struct bio_set *bs = bio->bi_pool;
227f9c78b2bSJens Axboe 	void *p;
228f9c78b2bSJens Axboe 
2299ae3b3f5SJens Axboe 	bio_uninit(bio);
230f9c78b2bSJens Axboe 
231f9c78b2bSJens Axboe 	if (bs) {
2327a800a20SChristoph Hellwig 		bvec_free(&bs->bvec_pool, bio->bi_io_vec, bio->bi_max_vecs);
233f9c78b2bSJens Axboe 
234f9c78b2bSJens Axboe 		/*
235f9c78b2bSJens Axboe 		 * If we have front padding, adjust the bio pointer before freeing
236f9c78b2bSJens Axboe 		 */
237f9c78b2bSJens Axboe 		p = bio;
238f9c78b2bSJens Axboe 		p -= bs->front_pad;
239f9c78b2bSJens Axboe 
2408aa6ba2fSKent Overstreet 		mempool_free(p, &bs->bio_pool);
241f9c78b2bSJens Axboe 	} else {
242f9c78b2bSJens Axboe 		/* Bio was allocated by bio_kmalloc() */
243f9c78b2bSJens Axboe 		kfree(bio);
244f9c78b2bSJens Axboe 	}
245f9c78b2bSJens Axboe }
246f9c78b2bSJens Axboe 
2479ae3b3f5SJens Axboe /*
2489ae3b3f5SJens Axboe  * Users of this function have their own bio allocation. Subsequently,
2499ae3b3f5SJens Axboe  * they must remember to pair any call to bio_init() with bio_uninit()
2509ae3b3f5SJens Axboe  * when IO has completed, or when the bio is released.
2519ae3b3f5SJens Axboe  */
25249add496SChristoph Hellwig void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *table,
25349add496SChristoph Hellwig 	      unsigned short max_vecs, unsigned int opf)
254f9c78b2bSJens Axboe {
255da521626SJens Axboe 	bio->bi_next = NULL;
25649add496SChristoph Hellwig 	bio->bi_bdev = bdev;
25749add496SChristoph Hellwig 	bio->bi_opf = opf;
258da521626SJens Axboe 	bio->bi_flags = 0;
259da521626SJens Axboe 	bio->bi_ioprio = 0;
260da521626SJens Axboe 	bio->bi_write_hint = 0;
261da521626SJens Axboe 	bio->bi_status = 0;
262da521626SJens Axboe 	bio->bi_iter.bi_sector = 0;
263da521626SJens Axboe 	bio->bi_iter.bi_size = 0;
264da521626SJens Axboe 	bio->bi_iter.bi_idx = 0;
265da521626SJens Axboe 	bio->bi_iter.bi_bvec_done = 0;
266da521626SJens Axboe 	bio->bi_end_io = NULL;
267da521626SJens Axboe 	bio->bi_private = NULL;
268da521626SJens Axboe #ifdef CONFIG_BLK_CGROUP
269da521626SJens Axboe 	bio->bi_blkg = NULL;
270da521626SJens Axboe 	bio->bi_issue.value = 0;
27149add496SChristoph Hellwig 	if (bdev)
27249add496SChristoph Hellwig 		bio_associate_blkg(bio);
273da521626SJens Axboe #ifdef CONFIG_BLK_CGROUP_IOCOST
274da521626SJens Axboe 	bio->bi_iocost_cost = 0;
275da521626SJens Axboe #endif
276da521626SJens Axboe #endif
277da521626SJens Axboe #ifdef CONFIG_BLK_INLINE_ENCRYPTION
278da521626SJens Axboe 	bio->bi_crypt_context = NULL;
279da521626SJens Axboe #endif
280da521626SJens Axboe #ifdef CONFIG_BLK_DEV_INTEGRITY
281da521626SJens Axboe 	bio->bi_integrity = NULL;
282da521626SJens Axboe #endif
283da521626SJens Axboe 	bio->bi_vcnt = 0;
284da521626SJens Axboe 
285c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
286dac56212SJens Axboe 	atomic_set(&bio->__bi_cnt, 1);
2873e08773cSChristoph Hellwig 	bio->bi_cookie = BLK_QC_T_NONE;
2883a83f467SMing Lei 
2893a83f467SMing Lei 	bio->bi_max_vecs = max_vecs;
290da521626SJens Axboe 	bio->bi_io_vec = table;
291da521626SJens Axboe 	bio->bi_pool = NULL;
292f9c78b2bSJens Axboe }
293f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_init);
294f9c78b2bSJens Axboe 
295f9c78b2bSJens Axboe /**
296f9c78b2bSJens Axboe  * bio_reset - reinitialize a bio
297f9c78b2bSJens Axboe  * @bio:	bio to reset
298a7c50c94SChristoph Hellwig  * @bdev:	block device to use the bio for
299a7c50c94SChristoph Hellwig  * @opf:	operation and flags for bio
300f9c78b2bSJens Axboe  *
301f9c78b2bSJens Axboe  * Description:
302f9c78b2bSJens Axboe  *   After calling bio_reset(), @bio will be in the same state as a freshly
303f9c78b2bSJens Axboe  *   allocated bio returned bio bio_alloc_bioset() - the only fields that are
304f9c78b2bSJens Axboe  *   preserved are the ones that are initialized by bio_alloc_bioset(). See
305f9c78b2bSJens Axboe  *   comment in struct bio.
306f9c78b2bSJens Axboe  */
307a7c50c94SChristoph Hellwig void bio_reset(struct bio *bio, struct block_device *bdev, unsigned int opf)
308f9c78b2bSJens Axboe {
3099ae3b3f5SJens Axboe 	bio_uninit(bio);
310f9c78b2bSJens Axboe 	memset(bio, 0, BIO_RESET_BYTES);
311c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
312a7c50c94SChristoph Hellwig 	bio->bi_bdev = bdev;
31378e34374SChristoph Hellwig 	if (bio->bi_bdev)
31478e34374SChristoph Hellwig 		bio_associate_blkg(bio);
315a7c50c94SChristoph Hellwig 	bio->bi_opf = opf;
316f9c78b2bSJens Axboe }
317f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_reset);
318f9c78b2bSJens Axboe 
31938f8baaeSChristoph Hellwig static struct bio *__bio_chain_endio(struct bio *bio)
320f9c78b2bSJens Axboe {
3214246a0b6SChristoph Hellwig 	struct bio *parent = bio->bi_private;
3224246a0b6SChristoph Hellwig 
3233edf5346SYufen Yu 	if (bio->bi_status && !parent->bi_status)
3244e4cbee9SChristoph Hellwig 		parent->bi_status = bio->bi_status;
325f9c78b2bSJens Axboe 	bio_put(bio);
32638f8baaeSChristoph Hellwig 	return parent;
32738f8baaeSChristoph Hellwig }
32838f8baaeSChristoph Hellwig 
32938f8baaeSChristoph Hellwig static void bio_chain_endio(struct bio *bio)
33038f8baaeSChristoph Hellwig {
33138f8baaeSChristoph Hellwig 	bio_endio(__bio_chain_endio(bio));
332f9c78b2bSJens Axboe }
333f9c78b2bSJens Axboe 
334f9c78b2bSJens Axboe /**
335f9c78b2bSJens Axboe  * bio_chain - chain bio completions
336f9c78b2bSJens Axboe  * @bio: the target bio
3375b874af6SMauro Carvalho Chehab  * @parent: the parent bio of @bio
338f9c78b2bSJens Axboe  *
339f9c78b2bSJens Axboe  * The caller won't have a bi_end_io called when @bio completes - instead,
340f9c78b2bSJens Axboe  * @parent's bi_end_io won't be called until both @parent and @bio have
341f9c78b2bSJens Axboe  * completed; the chained bio will also be freed when it completes.
342f9c78b2bSJens Axboe  *
343f9c78b2bSJens Axboe  * The caller must not set bi_private or bi_end_io in @bio.
344f9c78b2bSJens Axboe  */
345f9c78b2bSJens Axboe void bio_chain(struct bio *bio, struct bio *parent)
346f9c78b2bSJens Axboe {
347f9c78b2bSJens Axboe 	BUG_ON(bio->bi_private || bio->bi_end_io);
348f9c78b2bSJens Axboe 
349f9c78b2bSJens Axboe 	bio->bi_private = parent;
350f9c78b2bSJens Axboe 	bio->bi_end_io	= bio_chain_endio;
351c4cf5261SJens Axboe 	bio_inc_remaining(parent);
352f9c78b2bSJens Axboe }
353f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_chain);
354f9c78b2bSJens Axboe 
3550a3140eaSChaitanya Kulkarni struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev,
3560a3140eaSChaitanya Kulkarni 		unsigned int nr_pages, unsigned int opf, gfp_t gfp)
3573b005bf6SChristoph Hellwig {
35807888c66SChristoph Hellwig 	struct bio *new = bio_alloc(bdev, nr_pages, opf, gfp);
3590a3140eaSChaitanya Kulkarni 
3603b005bf6SChristoph Hellwig 	if (bio) {
3613b005bf6SChristoph Hellwig 		bio_chain(bio, new);
3623b005bf6SChristoph Hellwig 		submit_bio(bio);
3633b005bf6SChristoph Hellwig 	}
3643b005bf6SChristoph Hellwig 
3653b005bf6SChristoph Hellwig 	return new;
3663b005bf6SChristoph Hellwig }
3673b005bf6SChristoph Hellwig EXPORT_SYMBOL_GPL(blk_next_bio);
3683b005bf6SChristoph Hellwig 
369f9c78b2bSJens Axboe static void bio_alloc_rescue(struct work_struct *work)
370f9c78b2bSJens Axboe {
371f9c78b2bSJens Axboe 	struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
372f9c78b2bSJens Axboe 	struct bio *bio;
373f9c78b2bSJens Axboe 
374f9c78b2bSJens Axboe 	while (1) {
375f9c78b2bSJens Axboe 		spin_lock(&bs->rescue_lock);
376f9c78b2bSJens Axboe 		bio = bio_list_pop(&bs->rescue_list);
377f9c78b2bSJens Axboe 		spin_unlock(&bs->rescue_lock);
378f9c78b2bSJens Axboe 
379f9c78b2bSJens Axboe 		if (!bio)
380f9c78b2bSJens Axboe 			break;
381f9c78b2bSJens Axboe 
382ed00aabdSChristoph Hellwig 		submit_bio_noacct(bio);
383f9c78b2bSJens Axboe 	}
384f9c78b2bSJens Axboe }
385f9c78b2bSJens Axboe 
386f9c78b2bSJens Axboe static void punt_bios_to_rescuer(struct bio_set *bs)
387f9c78b2bSJens Axboe {
388f9c78b2bSJens Axboe 	struct bio_list punt, nopunt;
389f9c78b2bSJens Axboe 	struct bio *bio;
390f9c78b2bSJens Axboe 
39147e0fb46SNeilBrown 	if (WARN_ON_ONCE(!bs->rescue_workqueue))
39247e0fb46SNeilBrown 		return;
393f9c78b2bSJens Axboe 	/*
394f9c78b2bSJens Axboe 	 * In order to guarantee forward progress we must punt only bios that
395f9c78b2bSJens Axboe 	 * were allocated from this bio_set; otherwise, if there was a bio on
396f9c78b2bSJens Axboe 	 * there for a stacking driver higher up in the stack, processing it
397f9c78b2bSJens Axboe 	 * could require allocating bios from this bio_set, and doing that from
398f9c78b2bSJens Axboe 	 * our own rescuer would be bad.
399f9c78b2bSJens Axboe 	 *
400f9c78b2bSJens Axboe 	 * Since bio lists are singly linked, pop them all instead of trying to
401f9c78b2bSJens Axboe 	 * remove from the middle of the list:
402f9c78b2bSJens Axboe 	 */
403f9c78b2bSJens Axboe 
404f9c78b2bSJens Axboe 	bio_list_init(&punt);
405f9c78b2bSJens Axboe 	bio_list_init(&nopunt);
406f9c78b2bSJens Axboe 
407f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[0])))
408f9c78b2bSJens Axboe 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
409f5fe1b51SNeilBrown 	current->bio_list[0] = nopunt;
410f9c78b2bSJens Axboe 
411f5fe1b51SNeilBrown 	bio_list_init(&nopunt);
412f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[1])))
413f5fe1b51SNeilBrown 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
414f5fe1b51SNeilBrown 	current->bio_list[1] = nopunt;
415f9c78b2bSJens Axboe 
416f9c78b2bSJens Axboe 	spin_lock(&bs->rescue_lock);
417f9c78b2bSJens Axboe 	bio_list_merge(&bs->rescue_list, &punt);
418f9c78b2bSJens Axboe 	spin_unlock(&bs->rescue_lock);
419f9c78b2bSJens Axboe 
420f9c78b2bSJens Axboe 	queue_work(bs->rescue_workqueue, &bs->rescue_work);
421f9c78b2bSJens Axboe }
422f9c78b2bSJens Axboe 
423f9c78b2bSJens Axboe /**
424f9c78b2bSJens Axboe  * bio_alloc_bioset - allocate a bio for I/O
425609be106SChristoph Hellwig  * @bdev:	block device to allocate the bio for (can be %NULL)
426609be106SChristoph Hellwig  * @nr_vecs:	number of bvecs to pre-allocate
427609be106SChristoph Hellwig  * @opf:	operation and flags for bio
428519c8e9fSRandy Dunlap  * @gfp_mask:   the GFP_* mask given to the slab allocator
429f9c78b2bSJens Axboe  * @bs:		the bio_set to allocate from.
430f9c78b2bSJens Axboe  *
4313175199aSChristoph Hellwig  * Allocate a bio from the mempools in @bs.
432f9c78b2bSJens Axboe  *
4333175199aSChristoph Hellwig  * If %__GFP_DIRECT_RECLAIM is set then bio_alloc will always be able to
4343175199aSChristoph Hellwig  * allocate a bio.  This is due to the mempool guarantees.  To make this work,
4353175199aSChristoph Hellwig  * callers must never allocate more than 1 bio at a time from the general pool.
4363175199aSChristoph Hellwig  * Callers that need to allocate more than 1 bio must always submit the
4373175199aSChristoph Hellwig  * previously allocated bio for IO before attempting to allocate a new one.
4383175199aSChristoph Hellwig  * Failure to do so can cause deadlocks under memory pressure.
439f9c78b2bSJens Axboe  *
4403175199aSChristoph Hellwig  * Note that when running under submit_bio_noacct() (i.e. any block driver),
4413175199aSChristoph Hellwig  * bios are not submitted until after you return - see the code in
442ed00aabdSChristoph Hellwig  * submit_bio_noacct() that converts recursion into iteration, to prevent
443f9c78b2bSJens Axboe  * stack overflows.
444f9c78b2bSJens Axboe  *
4453175199aSChristoph Hellwig  * This would normally mean allocating multiple bios under submit_bio_noacct()
4463175199aSChristoph Hellwig  * would be susceptible to deadlocks, but we have
447f9c78b2bSJens Axboe  * deadlock avoidance code that resubmits any blocked bios from a rescuer
448f9c78b2bSJens Axboe  * thread.
449f9c78b2bSJens Axboe  *
450f9c78b2bSJens Axboe  * However, we do not guarantee forward progress for allocations from other
451f9c78b2bSJens Axboe  * mempools. Doing multiple allocations from the same mempool under
452ed00aabdSChristoph Hellwig  * submit_bio_noacct() should be avoided - instead, use bio_set's front_pad
453f9c78b2bSJens Axboe  * for per bio allocations.
454f9c78b2bSJens Axboe  *
4553175199aSChristoph Hellwig  * Returns: Pointer to new bio on success, NULL on failure.
456f9c78b2bSJens Axboe  */
457609be106SChristoph Hellwig struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
458609be106SChristoph Hellwig 			     unsigned int opf, gfp_t gfp_mask,
4597a88fa19SDan Carpenter 			     struct bio_set *bs)
460f9c78b2bSJens Axboe {
461f9c78b2bSJens Axboe 	gfp_t saved_gfp = gfp_mask;
462f9c78b2bSJens Axboe 	struct bio *bio;
463f9c78b2bSJens Axboe 	void *p;
464f9c78b2bSJens Axboe 
465609be106SChristoph Hellwig 	/* should not use nobvec bioset for nr_vecs > 0 */
466609be106SChristoph Hellwig 	if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && nr_vecs > 0))
467f9c78b2bSJens Axboe 		return NULL;
468f9c78b2bSJens Axboe 
469f9c78b2bSJens Axboe 	/*
4703175199aSChristoph Hellwig 	 * submit_bio_noacct() converts recursion to iteration; this means if
4713175199aSChristoph Hellwig 	 * we're running beneath it, any bios we allocate and submit will not be
4723175199aSChristoph Hellwig 	 * submitted (and thus freed) until after we return.
473f9c78b2bSJens Axboe 	 *
4743175199aSChristoph Hellwig 	 * This exposes us to a potential deadlock if we allocate multiple bios
4753175199aSChristoph Hellwig 	 * from the same bio_set() while running underneath submit_bio_noacct().
4763175199aSChristoph Hellwig 	 * If we were to allocate multiple bios (say a stacking block driver
4773175199aSChristoph Hellwig 	 * that was splitting bios), we would deadlock if we exhausted the
4783175199aSChristoph Hellwig 	 * mempool's reserve.
479f9c78b2bSJens Axboe 	 *
480f9c78b2bSJens Axboe 	 * We solve this, and guarantee forward progress, with a rescuer
4813175199aSChristoph Hellwig 	 * workqueue per bio_set. If we go to allocate and there are bios on
4823175199aSChristoph Hellwig 	 * current->bio_list, we first try the allocation without
4833175199aSChristoph Hellwig 	 * __GFP_DIRECT_RECLAIM; if that fails, we punt those bios we would be
4843175199aSChristoph Hellwig 	 * blocking to the rescuer workqueue before we retry with the original
4853175199aSChristoph Hellwig 	 * gfp_flags.
486f9c78b2bSJens Axboe 	 */
487f5fe1b51SNeilBrown 	if (current->bio_list &&
488f5fe1b51SNeilBrown 	    (!bio_list_empty(&current->bio_list[0]) ||
48947e0fb46SNeilBrown 	     !bio_list_empty(&current->bio_list[1])) &&
49047e0fb46SNeilBrown 	    bs->rescue_workqueue)
491d0164adcSMel Gorman 		gfp_mask &= ~__GFP_DIRECT_RECLAIM;
492f9c78b2bSJens Axboe 
4938aa6ba2fSKent Overstreet 	p = mempool_alloc(&bs->bio_pool, gfp_mask);
494f9c78b2bSJens Axboe 	if (!p && gfp_mask != saved_gfp) {
495f9c78b2bSJens Axboe 		punt_bios_to_rescuer(bs);
496f9c78b2bSJens Axboe 		gfp_mask = saved_gfp;
4978aa6ba2fSKent Overstreet 		p = mempool_alloc(&bs->bio_pool, gfp_mask);
498f9c78b2bSJens Axboe 	}
499f9c78b2bSJens Axboe 	if (unlikely(!p))
500f9c78b2bSJens Axboe 		return NULL;
501f9c78b2bSJens Axboe 
5023175199aSChristoph Hellwig 	bio = p + bs->front_pad;
503609be106SChristoph Hellwig 	if (nr_vecs > BIO_INLINE_VECS) {
5043175199aSChristoph Hellwig 		struct bio_vec *bvl = NULL;
505f9c78b2bSJens Axboe 
506609be106SChristoph Hellwig 		bvl = bvec_alloc(&bs->bvec_pool, &nr_vecs, gfp_mask);
507f9c78b2bSJens Axboe 		if (!bvl && gfp_mask != saved_gfp) {
508f9c78b2bSJens Axboe 			punt_bios_to_rescuer(bs);
509f9c78b2bSJens Axboe 			gfp_mask = saved_gfp;
510609be106SChristoph Hellwig 			bvl = bvec_alloc(&bs->bvec_pool, &nr_vecs, gfp_mask);
511f9c78b2bSJens Axboe 		}
512f9c78b2bSJens Axboe 		if (unlikely(!bvl))
513f9c78b2bSJens Axboe 			goto err_free;
514f9c78b2bSJens Axboe 
51549add496SChristoph Hellwig 		bio_init(bio, bdev, bvl, nr_vecs, opf);
516609be106SChristoph Hellwig 	} else if (nr_vecs) {
51749add496SChristoph Hellwig 		bio_init(bio, bdev, bio->bi_inline_vecs, BIO_INLINE_VECS, opf);
5183175199aSChristoph Hellwig 	} else {
51949add496SChristoph Hellwig 		bio_init(bio, bdev, NULL, 0, opf);
520f9c78b2bSJens Axboe 	}
521f9c78b2bSJens Axboe 
522f9c78b2bSJens Axboe 	bio->bi_pool = bs;
523f9c78b2bSJens Axboe 	return bio;
524f9c78b2bSJens Axboe 
525f9c78b2bSJens Axboe err_free:
5268aa6ba2fSKent Overstreet 	mempool_free(p, &bs->bio_pool);
527f9c78b2bSJens Axboe 	return NULL;
528f9c78b2bSJens Axboe }
529f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_bioset);
530f9c78b2bSJens Axboe 
5313175199aSChristoph Hellwig /**
5323175199aSChristoph Hellwig  * bio_kmalloc - kmalloc a bio for I/O
5333175199aSChristoph Hellwig  * @gfp_mask:   the GFP_* mask given to the slab allocator
5343175199aSChristoph Hellwig  * @nr_iovecs:	number of iovecs to pre-allocate
5353175199aSChristoph Hellwig  *
5363175199aSChristoph Hellwig  * Use kmalloc to allocate and initialize a bio.
5373175199aSChristoph Hellwig  *
5383175199aSChristoph Hellwig  * Returns: Pointer to new bio on success, NULL on failure.
5393175199aSChristoph Hellwig  */
5400f2e6ab8SChristoph Hellwig struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned short nr_iovecs)
5413175199aSChristoph Hellwig {
5423175199aSChristoph Hellwig 	struct bio *bio;
5433175199aSChristoph Hellwig 
5443175199aSChristoph Hellwig 	if (nr_iovecs > UIO_MAXIOV)
5453175199aSChristoph Hellwig 		return NULL;
5463175199aSChristoph Hellwig 
5473175199aSChristoph Hellwig 	bio = kmalloc(struct_size(bio, bi_inline_vecs, nr_iovecs), gfp_mask);
5483175199aSChristoph Hellwig 	if (unlikely(!bio))
5493175199aSChristoph Hellwig 		return NULL;
55049add496SChristoph Hellwig 	bio_init(bio, NULL, nr_iovecs ? bio->bi_inline_vecs : NULL, nr_iovecs,
55149add496SChristoph Hellwig 		 0);
5523175199aSChristoph Hellwig 	bio->bi_pool = NULL;
5533175199aSChristoph Hellwig 	return bio;
5543175199aSChristoph Hellwig }
5553175199aSChristoph Hellwig EXPORT_SYMBOL(bio_kmalloc);
5563175199aSChristoph Hellwig 
5576f822e1bSChristoph Hellwig void zero_fill_bio(struct bio *bio)
558f9c78b2bSJens Axboe {
559f9c78b2bSJens Axboe 	struct bio_vec bv;
560f9c78b2bSJens Axboe 	struct bvec_iter iter;
561f9c78b2bSJens Axboe 
562ab6c340eSChristoph Hellwig 	bio_for_each_segment(bv, bio, iter)
563ab6c340eSChristoph Hellwig 		memzero_bvec(&bv);
564f9c78b2bSJens Axboe }
5656f822e1bSChristoph Hellwig EXPORT_SYMBOL(zero_fill_bio);
566f9c78b2bSJens Axboe 
56783c9c547SMing Lei /**
56883c9c547SMing Lei  * bio_truncate - truncate the bio to small size of @new_size
56983c9c547SMing Lei  * @bio:	the bio to be truncated
57083c9c547SMing Lei  * @new_size:	new size for truncating the bio
57183c9c547SMing Lei  *
57283c9c547SMing Lei  * Description:
57383c9c547SMing Lei  *   Truncate the bio to new size of @new_size. If bio_op(bio) is
57483c9c547SMing Lei  *   REQ_OP_READ, zero the truncated part. This function should only
57583c9c547SMing Lei  *   be used for handling corner cases, such as bio eod.
57683c9c547SMing Lei  */
5774f7ab09aSChristoph Hellwig static void bio_truncate(struct bio *bio, unsigned new_size)
57885a8ce62SMing Lei {
57985a8ce62SMing Lei 	struct bio_vec bv;
58085a8ce62SMing Lei 	struct bvec_iter iter;
58185a8ce62SMing Lei 	unsigned int done = 0;
58285a8ce62SMing Lei 	bool truncated = false;
58385a8ce62SMing Lei 
58485a8ce62SMing Lei 	if (new_size >= bio->bi_iter.bi_size)
58585a8ce62SMing Lei 		return;
58685a8ce62SMing Lei 
58783c9c547SMing Lei 	if (bio_op(bio) != REQ_OP_READ)
58885a8ce62SMing Lei 		goto exit;
58985a8ce62SMing Lei 
59085a8ce62SMing Lei 	bio_for_each_segment(bv, bio, iter) {
59185a8ce62SMing Lei 		if (done + bv.bv_len > new_size) {
59285a8ce62SMing Lei 			unsigned offset;
59385a8ce62SMing Lei 
59485a8ce62SMing Lei 			if (!truncated)
59585a8ce62SMing Lei 				offset = new_size - done;
59685a8ce62SMing Lei 			else
59785a8ce62SMing Lei 				offset = 0;
5983ee859e3SOGAWA Hirofumi 			zero_user(bv.bv_page, bv.bv_offset + offset,
5993ee859e3SOGAWA Hirofumi 				  bv.bv_len - offset);
60085a8ce62SMing Lei 			truncated = true;
60185a8ce62SMing Lei 		}
60285a8ce62SMing Lei 		done += bv.bv_len;
60385a8ce62SMing Lei 	}
60485a8ce62SMing Lei 
60585a8ce62SMing Lei  exit:
60685a8ce62SMing Lei 	/*
60785a8ce62SMing Lei 	 * Don't touch bvec table here and make it really immutable, since
60885a8ce62SMing Lei 	 * fs bio user has to retrieve all pages via bio_for_each_segment_all
60985a8ce62SMing Lei 	 * in its .end_bio() callback.
61085a8ce62SMing Lei 	 *
61185a8ce62SMing Lei 	 * It is enough to truncate bio by updating .bi_size since we can make
61285a8ce62SMing Lei 	 * correct bvec with the updated .bi_size for drivers.
61385a8ce62SMing Lei 	 */
61485a8ce62SMing Lei 	bio->bi_iter.bi_size = new_size;
61585a8ce62SMing Lei }
61685a8ce62SMing Lei 
617f9c78b2bSJens Axboe /**
61829125ed6SChristoph Hellwig  * guard_bio_eod - truncate a BIO to fit the block device
61929125ed6SChristoph Hellwig  * @bio:	bio to truncate
62029125ed6SChristoph Hellwig  *
62129125ed6SChristoph Hellwig  * This allows us to do IO even on the odd last sectors of a device, even if the
62229125ed6SChristoph Hellwig  * block size is some multiple of the physical sector size.
62329125ed6SChristoph Hellwig  *
62429125ed6SChristoph Hellwig  * We'll just truncate the bio to the size of the device, and clear the end of
62529125ed6SChristoph Hellwig  * the buffer head manually.  Truly out-of-range accesses will turn into actual
62629125ed6SChristoph Hellwig  * I/O errors, this only handles the "we need to be able to do I/O at the final
62729125ed6SChristoph Hellwig  * sector" case.
62829125ed6SChristoph Hellwig  */
62929125ed6SChristoph Hellwig void guard_bio_eod(struct bio *bio)
63029125ed6SChristoph Hellwig {
631309dca30SChristoph Hellwig 	sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
63229125ed6SChristoph Hellwig 
63329125ed6SChristoph Hellwig 	if (!maxsector)
63429125ed6SChristoph Hellwig 		return;
63529125ed6SChristoph Hellwig 
63629125ed6SChristoph Hellwig 	/*
63729125ed6SChristoph Hellwig 	 * If the *whole* IO is past the end of the device,
63829125ed6SChristoph Hellwig 	 * let it through, and the IO layer will turn it into
63929125ed6SChristoph Hellwig 	 * an EIO.
64029125ed6SChristoph Hellwig 	 */
64129125ed6SChristoph Hellwig 	if (unlikely(bio->bi_iter.bi_sector >= maxsector))
64229125ed6SChristoph Hellwig 		return;
64329125ed6SChristoph Hellwig 
64429125ed6SChristoph Hellwig 	maxsector -= bio->bi_iter.bi_sector;
64529125ed6SChristoph Hellwig 	if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
64629125ed6SChristoph Hellwig 		return;
64729125ed6SChristoph Hellwig 
64829125ed6SChristoph Hellwig 	bio_truncate(bio, maxsector << 9);
64929125ed6SChristoph Hellwig }
65029125ed6SChristoph Hellwig 
651be4d234dSJens Axboe #define ALLOC_CACHE_MAX		512
652be4d234dSJens Axboe #define ALLOC_CACHE_SLACK	 64
653be4d234dSJens Axboe 
654be4d234dSJens Axboe static void bio_alloc_cache_prune(struct bio_alloc_cache *cache,
655be4d234dSJens Axboe 				  unsigned int nr)
656be4d234dSJens Axboe {
657be4d234dSJens Axboe 	unsigned int i = 0;
658be4d234dSJens Axboe 	struct bio *bio;
659be4d234dSJens Axboe 
660fcade2ceSJens Axboe 	while ((bio = cache->free_list) != NULL) {
661fcade2ceSJens Axboe 		cache->free_list = bio->bi_next;
662be4d234dSJens Axboe 		cache->nr--;
663be4d234dSJens Axboe 		bio_free(bio);
664be4d234dSJens Axboe 		if (++i == nr)
665be4d234dSJens Axboe 			break;
666be4d234dSJens Axboe 	}
667be4d234dSJens Axboe }
668be4d234dSJens Axboe 
669be4d234dSJens Axboe static int bio_cpu_dead(unsigned int cpu, struct hlist_node *node)
670be4d234dSJens Axboe {
671be4d234dSJens Axboe 	struct bio_set *bs;
672be4d234dSJens Axboe 
673be4d234dSJens Axboe 	bs = hlist_entry_safe(node, struct bio_set, cpuhp_dead);
674be4d234dSJens Axboe 	if (bs->cache) {
675be4d234dSJens Axboe 		struct bio_alloc_cache *cache = per_cpu_ptr(bs->cache, cpu);
676be4d234dSJens Axboe 
677be4d234dSJens Axboe 		bio_alloc_cache_prune(cache, -1U);
678be4d234dSJens Axboe 	}
679be4d234dSJens Axboe 	return 0;
680be4d234dSJens Axboe }
681be4d234dSJens Axboe 
682be4d234dSJens Axboe static void bio_alloc_cache_destroy(struct bio_set *bs)
683be4d234dSJens Axboe {
684be4d234dSJens Axboe 	int cpu;
685be4d234dSJens Axboe 
686be4d234dSJens Axboe 	if (!bs->cache)
687be4d234dSJens Axboe 		return;
688be4d234dSJens Axboe 
689be4d234dSJens Axboe 	cpuhp_state_remove_instance_nocalls(CPUHP_BIO_DEAD, &bs->cpuhp_dead);
690be4d234dSJens Axboe 	for_each_possible_cpu(cpu) {
691be4d234dSJens Axboe 		struct bio_alloc_cache *cache;
692be4d234dSJens Axboe 
693be4d234dSJens Axboe 		cache = per_cpu_ptr(bs->cache, cpu);
694be4d234dSJens Axboe 		bio_alloc_cache_prune(cache, -1U);
695be4d234dSJens Axboe 	}
696be4d234dSJens Axboe 	free_percpu(bs->cache);
697be4d234dSJens Axboe }
698be4d234dSJens Axboe 
69929125ed6SChristoph Hellwig /**
700f9c78b2bSJens Axboe  * bio_put - release a reference to a bio
701f9c78b2bSJens Axboe  * @bio:   bio to release reference to
702f9c78b2bSJens Axboe  *
703f9c78b2bSJens Axboe  * Description:
704f9c78b2bSJens Axboe  *   Put a reference to a &struct bio, either one you have gotten with
7059b10f6a9SNeilBrown  *   bio_alloc, bio_get or bio_clone_*. The last put of a bio will free it.
706f9c78b2bSJens Axboe  **/
707f9c78b2bSJens Axboe void bio_put(struct bio *bio)
708f9c78b2bSJens Axboe {
709be4d234dSJens Axboe 	if (unlikely(bio_flagged(bio, BIO_REFFED))) {
7109e8c0d0dSChristoph Hellwig 		BUG_ON(!atomic_read(&bio->__bi_cnt));
711be4d234dSJens Axboe 		if (!atomic_dec_and_test(&bio->__bi_cnt))
712be4d234dSJens Axboe 			return;
713be4d234dSJens Axboe 	}
714f9c78b2bSJens Axboe 
715be4d234dSJens Axboe 	if (bio_flagged(bio, BIO_PERCPU_CACHE)) {
716be4d234dSJens Axboe 		struct bio_alloc_cache *cache;
717be4d234dSJens Axboe 
718be4d234dSJens Axboe 		bio_uninit(bio);
719be4d234dSJens Axboe 		cache = per_cpu_ptr(bio->bi_pool->cache, get_cpu());
720fcade2ceSJens Axboe 		bio->bi_next = cache->free_list;
721fcade2ceSJens Axboe 		cache->free_list = bio;
722be4d234dSJens Axboe 		if (++cache->nr > ALLOC_CACHE_MAX + ALLOC_CACHE_SLACK)
723be4d234dSJens Axboe 			bio_alloc_cache_prune(cache, ALLOC_CACHE_SLACK);
724be4d234dSJens Axboe 		put_cpu();
725be4d234dSJens Axboe 	} else {
726f9c78b2bSJens Axboe 		bio_free(bio);
727f9c78b2bSJens Axboe 	}
728dac56212SJens Axboe }
729f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_put);
730f9c78b2bSJens Axboe 
731f9c78b2bSJens Axboe /**
732f9c78b2bSJens Axboe  * 	__bio_clone_fast - clone a bio that shares the original bio's biovec
733f9c78b2bSJens Axboe  * 	@bio: destination bio
734f9c78b2bSJens Axboe  * 	@bio_src: bio to clone
735*56b4b5abSChristoph Hellwig  *	@gfp: allocation flags
736f9c78b2bSJens Axboe  *
737f9c78b2bSJens Axboe  *	Clone a &bio. Caller will own the returned bio, but not
738f9c78b2bSJens Axboe  *	the actual data it points to. Reference count of returned
739f9c78b2bSJens Axboe  * 	bio will be one.
740f9c78b2bSJens Axboe  *
741f9c78b2bSJens Axboe  * 	Caller must ensure that @bio_src is not freed before @bio.
742f9c78b2bSJens Axboe  */
743*56b4b5abSChristoph Hellwig int __bio_clone_fast(struct bio *bio, struct bio *bio_src, gfp_t gfp)
744f9c78b2bSJens Axboe {
7457a800a20SChristoph Hellwig 	WARN_ON_ONCE(bio->bi_pool && bio->bi_max_vecs);
746f9c78b2bSJens Axboe 
747f9c78b2bSJens Axboe 	/*
748309dca30SChristoph Hellwig 	 * most users will be overriding ->bi_bdev with a new target,
749f9c78b2bSJens Axboe 	 * so we don't set nor calculate new physical/hw segment counts here
750f9c78b2bSJens Axboe 	 */
751309dca30SChristoph Hellwig 	bio->bi_bdev = bio_src->bi_bdev;
752b7c44ed9SJens Axboe 	bio_set_flag(bio, BIO_CLONED);
753111be883SShaohua Li 	if (bio_flagged(bio_src, BIO_THROTTLED))
754111be883SShaohua Li 		bio_set_flag(bio, BIO_THROTTLED);
75546bbf653SChristoph Hellwig 	if (bio_flagged(bio_src, BIO_REMAPPED))
75646bbf653SChristoph Hellwig 		bio_set_flag(bio, BIO_REMAPPED);
7571eff9d32SJens Axboe 	bio->bi_opf = bio_src->bi_opf;
758ca474b73SHannes Reinecke 	bio->bi_ioprio = bio_src->bi_ioprio;
759cb6934f8SJens Axboe 	bio->bi_write_hint = bio_src->bi_write_hint;
760f9c78b2bSJens Axboe 	bio->bi_iter = bio_src->bi_iter;
761f9c78b2bSJens Axboe 	bio->bi_io_vec = bio_src->bi_io_vec;
76220bd723eSPaolo Valente 
763db6638d7SDennis Zhou 	bio_clone_blkg_association(bio, bio_src);
764e439bedfSDennis Zhou 	blkcg_bio_issue_init(bio);
765*56b4b5abSChristoph Hellwig 
766*56b4b5abSChristoph Hellwig 	if (bio_crypt_clone(bio, bio_src, gfp) < 0)
767*56b4b5abSChristoph Hellwig 		return -ENOMEM;
768*56b4b5abSChristoph Hellwig 	if (bio_integrity(bio_src) &&
769*56b4b5abSChristoph Hellwig 	    bio_integrity_clone(bio, bio_src, gfp) < 0)
770*56b4b5abSChristoph Hellwig 		return -ENOMEM;
771*56b4b5abSChristoph Hellwig 	return 0;
772f9c78b2bSJens Axboe }
773f9c78b2bSJens Axboe EXPORT_SYMBOL(__bio_clone_fast);
774f9c78b2bSJens Axboe 
775f9c78b2bSJens Axboe /**
776f9c78b2bSJens Axboe  *	bio_clone_fast - clone a bio that shares the original bio's biovec
777f9c78b2bSJens Axboe  *	@bio: bio to clone
778f9c78b2bSJens Axboe  *	@gfp_mask: allocation priority
779f9c78b2bSJens Axboe  *	@bs: bio_set to allocate from
780f9c78b2bSJens Axboe  *
781f9c78b2bSJens Axboe  * 	Like __bio_clone_fast, only also allocates the returned bio
782f9c78b2bSJens Axboe  */
783f9c78b2bSJens Axboe struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs)
784f9c78b2bSJens Axboe {
785f9c78b2bSJens Axboe 	struct bio *b;
786f9c78b2bSJens Axboe 
787609be106SChristoph Hellwig 	b = bio_alloc_bioset(NULL, 0, 0, gfp_mask, bs);
788f9c78b2bSJens Axboe 	if (!b)
789f9c78b2bSJens Axboe 		return NULL;
790f9c78b2bSJens Axboe 
791*56b4b5abSChristoph Hellwig 	if (__bio_clone_fast(b, bio, gfp_mask < 0)) {
79207560151SEric Biggers 		bio_put(b);
79307560151SEric Biggers 		return NULL;
794f9c78b2bSJens Axboe 	}
795*56b4b5abSChristoph Hellwig 
796*56b4b5abSChristoph Hellwig 	return b;
797*56b4b5abSChristoph Hellwig }
798f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_clone_fast);
799f9c78b2bSJens Axboe 
8005cbd28e3SChristoph Hellwig const char *bio_devname(struct bio *bio, char *buf)
8015cbd28e3SChristoph Hellwig {
802309dca30SChristoph Hellwig 	return bdevname(bio->bi_bdev, buf);
8035cbd28e3SChristoph Hellwig }
8045cbd28e3SChristoph Hellwig EXPORT_SYMBOL(bio_devname);
8055cbd28e3SChristoph Hellwig 
8069a6083beSChristoph Hellwig /**
8079a6083beSChristoph Hellwig  * bio_full - check if the bio is full
8089a6083beSChristoph Hellwig  * @bio:	bio to check
8099a6083beSChristoph Hellwig  * @len:	length of one segment to be added
8109a6083beSChristoph Hellwig  *
8119a6083beSChristoph Hellwig  * Return true if @bio is full and one segment with @len bytes can't be
8129a6083beSChristoph Hellwig  * added to the bio, otherwise return false
8139a6083beSChristoph Hellwig  */
8149a6083beSChristoph Hellwig static inline bool bio_full(struct bio *bio, unsigned len)
8159a6083beSChristoph Hellwig {
8169a6083beSChristoph Hellwig 	if (bio->bi_vcnt >= bio->bi_max_vecs)
8179a6083beSChristoph Hellwig 		return true;
8189a6083beSChristoph Hellwig 	if (bio->bi_iter.bi_size > UINT_MAX - len)
8199a6083beSChristoph Hellwig 		return true;
8209a6083beSChristoph Hellwig 	return false;
8219a6083beSChristoph Hellwig }
8229a6083beSChristoph Hellwig 
8235919482eSMing Lei static inline bool page_is_mergeable(const struct bio_vec *bv,
8245919482eSMing Lei 		struct page *page, unsigned int len, unsigned int off,
825ff896738SChristoph Hellwig 		bool *same_page)
8265919482eSMing Lei {
827d8166519SMatthew Wilcox (Oracle) 	size_t bv_end = bv->bv_offset + bv->bv_len;
828d8166519SMatthew Wilcox (Oracle) 	phys_addr_t vec_end_addr = page_to_phys(bv->bv_page) + bv_end - 1;
8295919482eSMing Lei 	phys_addr_t page_addr = page_to_phys(page);
8305919482eSMing Lei 
8315919482eSMing Lei 	if (vec_end_addr + 1 != page_addr + off)
8325919482eSMing Lei 		return false;
8335919482eSMing Lei 	if (xen_domain() && !xen_biovec_phys_mergeable(bv, page))
8345919482eSMing Lei 		return false;
83552d52d1cSChristoph Hellwig 
836ff896738SChristoph Hellwig 	*same_page = ((vec_end_addr & PAGE_MASK) == page_addr);
837d8166519SMatthew Wilcox (Oracle) 	if (*same_page)
8385919482eSMing Lei 		return true;
839d8166519SMatthew Wilcox (Oracle) 	return (bv->bv_page + bv_end / PAGE_SIZE) == (page + off / PAGE_SIZE);
8405919482eSMing Lei }
8415919482eSMing Lei 
8429774b391SChristoph Hellwig /**
8439774b391SChristoph Hellwig  * __bio_try_merge_page - try appending data to an existing bvec.
8449774b391SChristoph Hellwig  * @bio: destination bio
8459774b391SChristoph Hellwig  * @page: start page to add
8469774b391SChristoph Hellwig  * @len: length of the data to add
8479774b391SChristoph Hellwig  * @off: offset of the data relative to @page
8489774b391SChristoph Hellwig  * @same_page: return if the segment has been merged inside the same page
8499774b391SChristoph Hellwig  *
8509774b391SChristoph Hellwig  * Try to add the data at @page + @off to the last bvec of @bio.  This is a
8519774b391SChristoph Hellwig  * useful optimisation for file systems with a block size smaller than the
8529774b391SChristoph Hellwig  * page size.
8539774b391SChristoph Hellwig  *
8549774b391SChristoph Hellwig  * Warn if (@len, @off) crosses pages in case that @same_page is true.
8559774b391SChristoph Hellwig  *
8569774b391SChristoph Hellwig  * Return %true on success or %false on failure.
8579774b391SChristoph Hellwig  */
8589774b391SChristoph Hellwig static bool __bio_try_merge_page(struct bio *bio, struct page *page,
8599774b391SChristoph Hellwig 		unsigned int len, unsigned int off, bool *same_page)
8609774b391SChristoph Hellwig {
8619774b391SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
8629774b391SChristoph Hellwig 		return false;
8639774b391SChristoph Hellwig 
8649774b391SChristoph Hellwig 	if (bio->bi_vcnt > 0) {
8659774b391SChristoph Hellwig 		struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
8669774b391SChristoph Hellwig 
8679774b391SChristoph Hellwig 		if (page_is_mergeable(bv, page, len, off, same_page)) {
8689774b391SChristoph Hellwig 			if (bio->bi_iter.bi_size > UINT_MAX - len) {
8699774b391SChristoph Hellwig 				*same_page = false;
8709774b391SChristoph Hellwig 				return false;
8719774b391SChristoph Hellwig 			}
8729774b391SChristoph Hellwig 			bv->bv_len += len;
8739774b391SChristoph Hellwig 			bio->bi_iter.bi_size += len;
8749774b391SChristoph Hellwig 			return true;
8759774b391SChristoph Hellwig 		}
8769774b391SChristoph Hellwig 	}
8779774b391SChristoph Hellwig 	return false;
8789774b391SChristoph Hellwig }
8799774b391SChristoph Hellwig 
880e4581105SChristoph Hellwig /*
881e4581105SChristoph Hellwig  * Try to merge a page into a segment, while obeying the hardware segment
882e4581105SChristoph Hellwig  * size limit.  This is not for normal read/write bios, but for passthrough
883e4581105SChristoph Hellwig  * or Zone Append operations that we can't split.
884e4581105SChristoph Hellwig  */
885e4581105SChristoph Hellwig static bool bio_try_merge_hw_seg(struct request_queue *q, struct bio *bio,
886e4581105SChristoph Hellwig 				 struct page *page, unsigned len,
887e4581105SChristoph Hellwig 				 unsigned offset, bool *same_page)
888489fbbcbSMing Lei {
889384209cdSChristoph Hellwig 	struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
890489fbbcbSMing Lei 	unsigned long mask = queue_segment_boundary(q);
891489fbbcbSMing Lei 	phys_addr_t addr1 = page_to_phys(bv->bv_page) + bv->bv_offset;
892489fbbcbSMing Lei 	phys_addr_t addr2 = page_to_phys(page) + offset + len - 1;
893489fbbcbSMing Lei 
894489fbbcbSMing Lei 	if ((addr1 | mask) != (addr2 | mask))
895489fbbcbSMing Lei 		return false;
896489fbbcbSMing Lei 	if (bv->bv_len + len > queue_max_segment_size(q))
897489fbbcbSMing Lei 		return false;
898384209cdSChristoph Hellwig 	return __bio_try_merge_page(bio, page, len, offset, same_page);
899489fbbcbSMing Lei }
900489fbbcbSMing Lei 
901f4595875SShaohua Li /**
902e4581105SChristoph Hellwig  * bio_add_hw_page - attempt to add a page to a bio with hw constraints
903c66a14d0SKent Overstreet  * @q: the target queue
904c66a14d0SKent Overstreet  * @bio: destination bio
905c66a14d0SKent Overstreet  * @page: page to add
906c66a14d0SKent Overstreet  * @len: vec entry length
907c66a14d0SKent Overstreet  * @offset: vec entry offset
908e4581105SChristoph Hellwig  * @max_sectors: maximum number of sectors that can be added
909e4581105SChristoph Hellwig  * @same_page: return if the segment has been merged inside the same page
910f9c78b2bSJens Axboe  *
911e4581105SChristoph Hellwig  * Add a page to a bio while respecting the hardware max_sectors, max_segment
912e4581105SChristoph Hellwig  * and gap limitations.
913f9c78b2bSJens Axboe  */
914e4581105SChristoph Hellwig int bio_add_hw_page(struct request_queue *q, struct bio *bio,
91519047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset,
916e4581105SChristoph Hellwig 		unsigned int max_sectors, bool *same_page)
917f9c78b2bSJens Axboe {
918f9c78b2bSJens Axboe 	struct bio_vec *bvec;
919f9c78b2bSJens Axboe 
920e4581105SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
921f9c78b2bSJens Axboe 		return 0;
922f9c78b2bSJens Axboe 
923e4581105SChristoph Hellwig 	if (((bio->bi_iter.bi_size + len) >> 9) > max_sectors)
924f9c78b2bSJens Axboe 		return 0;
925f9c78b2bSJens Axboe 
926f9c78b2bSJens Axboe 	if (bio->bi_vcnt > 0) {
927e4581105SChristoph Hellwig 		if (bio_try_merge_hw_seg(q, bio, page, len, offset, same_page))
928384209cdSChristoph Hellwig 			return len;
929320ea869SChristoph Hellwig 
930320ea869SChristoph Hellwig 		/*
931320ea869SChristoph Hellwig 		 * If the queue doesn't support SG gaps and adding this segment
932320ea869SChristoph Hellwig 		 * would create a gap, disallow it.
933320ea869SChristoph Hellwig 		 */
934384209cdSChristoph Hellwig 		bvec = &bio->bi_io_vec[bio->bi_vcnt - 1];
935320ea869SChristoph Hellwig 		if (bvec_gap_to_prev(q, bvec, offset))
936320ea869SChristoph Hellwig 			return 0;
937f9c78b2bSJens Axboe 	}
938f9c78b2bSJens Axboe 
93979d08f89SMing Lei 	if (bio_full(bio, len))
940f9c78b2bSJens Axboe 		return 0;
941f9c78b2bSJens Axboe 
94214ccb66bSChristoph Hellwig 	if (bio->bi_vcnt >= queue_max_segments(q))
943489fbbcbSMing Lei 		return 0;
944489fbbcbSMing Lei 
945f9c78b2bSJens Axboe 	bvec = &bio->bi_io_vec[bio->bi_vcnt];
946f9c78b2bSJens Axboe 	bvec->bv_page = page;
947f9c78b2bSJens Axboe 	bvec->bv_len = len;
948f9c78b2bSJens Axboe 	bvec->bv_offset = offset;
949fcbf6a08SMaurizio Lombardi 	bio->bi_vcnt++;
950dcdca753SChristoph Hellwig 	bio->bi_iter.bi_size += len;
951f9c78b2bSJens Axboe 	return len;
952f9c78b2bSJens Axboe }
95319047087SMing Lei 
954e4581105SChristoph Hellwig /**
955e4581105SChristoph Hellwig  * bio_add_pc_page	- attempt to add page to passthrough bio
956e4581105SChristoph Hellwig  * @q: the target queue
957e4581105SChristoph Hellwig  * @bio: destination bio
958e4581105SChristoph Hellwig  * @page: page to add
959e4581105SChristoph Hellwig  * @len: vec entry length
960e4581105SChristoph Hellwig  * @offset: vec entry offset
961e4581105SChristoph Hellwig  *
962e4581105SChristoph Hellwig  * Attempt to add a page to the bio_vec maplist. This can fail for a
963e4581105SChristoph Hellwig  * number of reasons, such as the bio being full or target block device
964e4581105SChristoph Hellwig  * limitations. The target block device must allow bio's up to PAGE_SIZE,
965e4581105SChristoph Hellwig  * so it is always possible to add a single page to an empty bio.
966e4581105SChristoph Hellwig  *
967e4581105SChristoph Hellwig  * This should only be used by passthrough bios.
968e4581105SChristoph Hellwig  */
96919047087SMing Lei int bio_add_pc_page(struct request_queue *q, struct bio *bio,
97019047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset)
97119047087SMing Lei {
972d1916c86SChristoph Hellwig 	bool same_page = false;
973e4581105SChristoph Hellwig 	return bio_add_hw_page(q, bio, page, len, offset,
974e4581105SChristoph Hellwig 			queue_max_hw_sectors(q), &same_page);
97519047087SMing Lei }
976f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_pc_page);
977f9c78b2bSJens Axboe 
978f9c78b2bSJens Axboe /**
979ae29333fSJohannes Thumshirn  * bio_add_zone_append_page - attempt to add page to zone-append bio
980ae29333fSJohannes Thumshirn  * @bio: destination bio
981ae29333fSJohannes Thumshirn  * @page: page to add
982ae29333fSJohannes Thumshirn  * @len: vec entry length
983ae29333fSJohannes Thumshirn  * @offset: vec entry offset
984ae29333fSJohannes Thumshirn  *
985ae29333fSJohannes Thumshirn  * Attempt to add a page to the bio_vec maplist of a bio that will be submitted
986ae29333fSJohannes Thumshirn  * for a zone-append request. This can fail for a number of reasons, such as the
987ae29333fSJohannes Thumshirn  * bio being full or the target block device is not a zoned block device or
988ae29333fSJohannes Thumshirn  * other limitations of the target block device. The target block device must
989ae29333fSJohannes Thumshirn  * allow bio's up to PAGE_SIZE, so it is always possible to add a single page
990ae29333fSJohannes Thumshirn  * to an empty bio.
991ae29333fSJohannes Thumshirn  *
992ae29333fSJohannes Thumshirn  * Returns: number of bytes added to the bio, or 0 in case of a failure.
993ae29333fSJohannes Thumshirn  */
994ae29333fSJohannes Thumshirn int bio_add_zone_append_page(struct bio *bio, struct page *page,
995ae29333fSJohannes Thumshirn 			     unsigned int len, unsigned int offset)
996ae29333fSJohannes Thumshirn {
9973caee463SPavel Begunkov 	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
998ae29333fSJohannes Thumshirn 	bool same_page = false;
999ae29333fSJohannes Thumshirn 
1000ae29333fSJohannes Thumshirn 	if (WARN_ON_ONCE(bio_op(bio) != REQ_OP_ZONE_APPEND))
1001ae29333fSJohannes Thumshirn 		return 0;
1002ae29333fSJohannes Thumshirn 
1003ae29333fSJohannes Thumshirn 	if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
1004ae29333fSJohannes Thumshirn 		return 0;
1005ae29333fSJohannes Thumshirn 
1006ae29333fSJohannes Thumshirn 	return bio_add_hw_page(q, bio, page, len, offset,
1007ae29333fSJohannes Thumshirn 			       queue_max_zone_append_sectors(q), &same_page);
1008ae29333fSJohannes Thumshirn }
1009ae29333fSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_add_zone_append_page);
1010ae29333fSJohannes Thumshirn 
1011ae29333fSJohannes Thumshirn /**
1012551879a4SMing Lei  * __bio_add_page - add page(s) to a bio in a new segment
10130aa69fd3SChristoph Hellwig  * @bio: destination bio
1014551879a4SMing Lei  * @page: start page to add
1015551879a4SMing Lei  * @len: length of the data to add, may cross pages
1016551879a4SMing Lei  * @off: offset of the data relative to @page, may cross pages
10170aa69fd3SChristoph Hellwig  *
10180aa69fd3SChristoph Hellwig  * Add the data at @page + @off to @bio as a new bvec.  The caller must ensure
10190aa69fd3SChristoph Hellwig  * that @bio has space for another bvec.
10200aa69fd3SChristoph Hellwig  */
10210aa69fd3SChristoph Hellwig void __bio_add_page(struct bio *bio, struct page *page,
10220aa69fd3SChristoph Hellwig 		unsigned int len, unsigned int off)
10230aa69fd3SChristoph Hellwig {
10240aa69fd3SChristoph Hellwig 	struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt];
10250aa69fd3SChristoph Hellwig 
10260aa69fd3SChristoph Hellwig 	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
102779d08f89SMing Lei 	WARN_ON_ONCE(bio_full(bio, len));
10280aa69fd3SChristoph Hellwig 
10290aa69fd3SChristoph Hellwig 	bv->bv_page = page;
10300aa69fd3SChristoph Hellwig 	bv->bv_offset = off;
10310aa69fd3SChristoph Hellwig 	bv->bv_len = len;
10320aa69fd3SChristoph Hellwig 
10330aa69fd3SChristoph Hellwig 	bio->bi_iter.bi_size += len;
10340aa69fd3SChristoph Hellwig 	bio->bi_vcnt++;
1035b8e24a93SJohannes Weiner 
1036b8e24a93SJohannes Weiner 	if (!bio_flagged(bio, BIO_WORKINGSET) && unlikely(PageWorkingset(page)))
1037b8e24a93SJohannes Weiner 		bio_set_flag(bio, BIO_WORKINGSET);
10380aa69fd3SChristoph Hellwig }
10390aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_add_page);
10400aa69fd3SChristoph Hellwig 
10410aa69fd3SChristoph Hellwig /**
1042551879a4SMing Lei  *	bio_add_page	-	attempt to add page(s) to bio
1043f9c78b2bSJens Axboe  *	@bio: destination bio
1044551879a4SMing Lei  *	@page: start page to add
1045551879a4SMing Lei  *	@len: vec entry length, may cross pages
1046551879a4SMing Lei  *	@offset: vec entry offset relative to @page, may cross pages
1047f9c78b2bSJens Axboe  *
1048551879a4SMing Lei  *	Attempt to add page(s) to the bio_vec maplist. This will only fail
1049c66a14d0SKent Overstreet  *	if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
1050f9c78b2bSJens Axboe  */
1051c66a14d0SKent Overstreet int bio_add_page(struct bio *bio, struct page *page,
1052c66a14d0SKent Overstreet 		 unsigned int len, unsigned int offset)
1053f9c78b2bSJens Axboe {
1054ff896738SChristoph Hellwig 	bool same_page = false;
1055ff896738SChristoph Hellwig 
1056ff896738SChristoph Hellwig 	if (!__bio_try_merge_page(bio, page, len, offset, &same_page)) {
105779d08f89SMing Lei 		if (bio_full(bio, len))
1058c66a14d0SKent Overstreet 			return 0;
10590aa69fd3SChristoph Hellwig 		__bio_add_page(bio, page, len, offset);
1060c66a14d0SKent Overstreet 	}
1061c66a14d0SKent Overstreet 	return len;
1062f9c78b2bSJens Axboe }
1063f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_page);
1064f9c78b2bSJens Axboe 
106585f5a74cSMatthew Wilcox (Oracle) /**
106685f5a74cSMatthew Wilcox (Oracle)  * bio_add_folio - Attempt to add part of a folio to a bio.
106785f5a74cSMatthew Wilcox (Oracle)  * @bio: BIO to add to.
106885f5a74cSMatthew Wilcox (Oracle)  * @folio: Folio to add.
106985f5a74cSMatthew Wilcox (Oracle)  * @len: How many bytes from the folio to add.
107085f5a74cSMatthew Wilcox (Oracle)  * @off: First byte in this folio to add.
107185f5a74cSMatthew Wilcox (Oracle)  *
107285f5a74cSMatthew Wilcox (Oracle)  * Filesystems that use folios can call this function instead of calling
107385f5a74cSMatthew Wilcox (Oracle)  * bio_add_page() for each page in the folio.  If @off is bigger than
107485f5a74cSMatthew Wilcox (Oracle)  * PAGE_SIZE, this function can create a bio_vec that starts in a page
107585f5a74cSMatthew Wilcox (Oracle)  * after the bv_page.  BIOs do not support folios that are 4GiB or larger.
107685f5a74cSMatthew Wilcox (Oracle)  *
107785f5a74cSMatthew Wilcox (Oracle)  * Return: Whether the addition was successful.
107885f5a74cSMatthew Wilcox (Oracle)  */
107985f5a74cSMatthew Wilcox (Oracle) bool bio_add_folio(struct bio *bio, struct folio *folio, size_t len,
108085f5a74cSMatthew Wilcox (Oracle) 		   size_t off)
108185f5a74cSMatthew Wilcox (Oracle) {
108285f5a74cSMatthew Wilcox (Oracle) 	if (len > UINT_MAX || off > UINT_MAX)
1083455a844dSJiapeng Chong 		return false;
108485f5a74cSMatthew Wilcox (Oracle) 	return bio_add_page(bio, &folio->page, len, off) > 0;
108585f5a74cSMatthew Wilcox (Oracle) }
108685f5a74cSMatthew Wilcox (Oracle) 
1087c809084aSPavel Begunkov void __bio_release_pages(struct bio *bio, bool mark_dirty)
10887321ecbfSChristoph Hellwig {
10897321ecbfSChristoph Hellwig 	struct bvec_iter_all iter_all;
10907321ecbfSChristoph Hellwig 	struct bio_vec *bvec;
10917321ecbfSChristoph Hellwig 
1092d241a95fSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
1093d241a95fSChristoph Hellwig 		if (mark_dirty && !PageCompound(bvec->bv_page))
1094d241a95fSChristoph Hellwig 			set_page_dirty_lock(bvec->bv_page);
10957321ecbfSChristoph Hellwig 		put_page(bvec->bv_page);
10967321ecbfSChristoph Hellwig 	}
1097d241a95fSChristoph Hellwig }
1098c809084aSPavel Begunkov EXPORT_SYMBOL_GPL(__bio_release_pages);
10997321ecbfSChristoph Hellwig 
11001bb6b810SPavel Begunkov void bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
11016d0c48aeSJens Axboe {
1102fa5fa8ecSPavel Begunkov 	size_t size = iov_iter_count(iter);
1103fa5fa8ecSPavel Begunkov 
11047a800a20SChristoph Hellwig 	WARN_ON_ONCE(bio->bi_max_vecs);
11056d0c48aeSJens Axboe 
1106fa5fa8ecSPavel Begunkov 	if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
1107fa5fa8ecSPavel Begunkov 		struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1108fa5fa8ecSPavel Begunkov 		size_t max_sectors = queue_max_zone_append_sectors(q);
1109fa5fa8ecSPavel Begunkov 
1110fa5fa8ecSPavel Begunkov 		size = min(size, max_sectors << SECTOR_SHIFT);
1111fa5fa8ecSPavel Begunkov 	}
1112fa5fa8ecSPavel Begunkov 
1113c42bca92SPavel Begunkov 	bio->bi_vcnt = iter->nr_segs;
1114c42bca92SPavel Begunkov 	bio->bi_io_vec = (struct bio_vec *)iter->bvec;
1115c42bca92SPavel Begunkov 	bio->bi_iter.bi_bvec_done = iter->iov_offset;
1116fa5fa8ecSPavel Begunkov 	bio->bi_iter.bi_size = size;
1117ed97ce5eSChristoph Hellwig 	bio_set_flag(bio, BIO_NO_PAGE_REF);
1118977be012SChristoph Hellwig 	bio_set_flag(bio, BIO_CLONED);
11197de55b7dSJohannes Thumshirn }
11206d0c48aeSJens Axboe 
1121d9cf3bd5SPavel Begunkov static void bio_put_pages(struct page **pages, size_t size, size_t off)
1122d9cf3bd5SPavel Begunkov {
1123d9cf3bd5SPavel Begunkov 	size_t i, nr = DIV_ROUND_UP(size + (off & ~PAGE_MASK), PAGE_SIZE);
1124d9cf3bd5SPavel Begunkov 
1125d9cf3bd5SPavel Begunkov 	for (i = 0; i < nr; i++)
1126d9cf3bd5SPavel Begunkov 		put_page(pages[i]);
1127d9cf3bd5SPavel Begunkov }
1128d9cf3bd5SPavel Begunkov 
1129576ed913SChristoph Hellwig #define PAGE_PTRS_PER_BVEC     (sizeof(struct bio_vec) / sizeof(struct page *))
1130576ed913SChristoph Hellwig 
11312cefe4dbSKent Overstreet /**
113217d51b10SMartin Wilck  * __bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
11332cefe4dbSKent Overstreet  * @bio: bio to add pages to
11342cefe4dbSKent Overstreet  * @iter: iov iterator describing the region to be mapped
11352cefe4dbSKent Overstreet  *
113617d51b10SMartin Wilck  * Pins pages from *iter and appends them to @bio's bvec array. The
11372cefe4dbSKent Overstreet  * pages will have to be released using put_page() when done.
113817d51b10SMartin Wilck  * For multi-segment *iter, this function only adds pages from the
11393cf14889SRandy Dunlap  * next non-empty segment of the iov iterator.
11402cefe4dbSKent Overstreet  */
114117d51b10SMartin Wilck static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
11422cefe4dbSKent Overstreet {
1143576ed913SChristoph Hellwig 	unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt;
1144576ed913SChristoph Hellwig 	unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt;
11452cefe4dbSKent Overstreet 	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
11462cefe4dbSKent Overstreet 	struct page **pages = (struct page **)bv;
114745691804SChristoph Hellwig 	bool same_page = false;
1148576ed913SChristoph Hellwig 	ssize_t size, left;
1149576ed913SChristoph Hellwig 	unsigned len, i;
1150b403ea24SMartin Wilck 	size_t offset;
1151576ed913SChristoph Hellwig 
1152576ed913SChristoph Hellwig 	/*
1153576ed913SChristoph Hellwig 	 * Move page array up in the allocated memory for the bio vecs as far as
1154576ed913SChristoph Hellwig 	 * possible so that we can start filling biovecs from the beginning
1155576ed913SChristoph Hellwig 	 * without overwriting the temporary page array.
1156576ed913SChristoph Hellwig 	*/
1157576ed913SChristoph Hellwig 	BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2);
1158576ed913SChristoph Hellwig 	pages += entries_left * (PAGE_PTRS_PER_BVEC - 1);
11592cefe4dbSKent Overstreet 
116035c820e7SJens Axboe 	size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset);
11612cefe4dbSKent Overstreet 	if (unlikely(size <= 0))
11622cefe4dbSKent Overstreet 		return size ? size : -EFAULT;
11632cefe4dbSKent Overstreet 
1164576ed913SChristoph Hellwig 	for (left = size, i = 0; left > 0; left -= len, i++) {
1165576ed913SChristoph Hellwig 		struct page *page = pages[i];
11662cefe4dbSKent Overstreet 
1167576ed913SChristoph Hellwig 		len = min_t(size_t, PAGE_SIZE - offset, left);
116845691804SChristoph Hellwig 
116945691804SChristoph Hellwig 		if (__bio_try_merge_page(bio, page, len, offset, &same_page)) {
117045691804SChristoph Hellwig 			if (same_page)
117145691804SChristoph Hellwig 				put_page(page);
117245691804SChristoph Hellwig 		} else {
1173d9cf3bd5SPavel Begunkov 			if (WARN_ON_ONCE(bio_full(bio, len))) {
1174d9cf3bd5SPavel Begunkov 				bio_put_pages(pages + i, left, offset);
1175576ed913SChristoph Hellwig 				return -EINVAL;
1176d9cf3bd5SPavel Begunkov 			}
117745691804SChristoph Hellwig 			__bio_add_page(bio, page, len, offset);
117845691804SChristoph Hellwig 		}
1179576ed913SChristoph Hellwig 		offset = 0;
11802cefe4dbSKent Overstreet 	}
11812cefe4dbSKent Overstreet 
11822cefe4dbSKent Overstreet 	iov_iter_advance(iter, size);
11832cefe4dbSKent Overstreet 	return 0;
11842cefe4dbSKent Overstreet }
118517d51b10SMartin Wilck 
11860512a75bSKeith Busch static int __bio_iov_append_get_pages(struct bio *bio, struct iov_iter *iter)
11870512a75bSKeith Busch {
11880512a75bSKeith Busch 	unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt;
11890512a75bSKeith Busch 	unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt;
11903caee463SPavel Begunkov 	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
11910512a75bSKeith Busch 	unsigned int max_append_sectors = queue_max_zone_append_sectors(q);
11920512a75bSKeith Busch 	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
11930512a75bSKeith Busch 	struct page **pages = (struct page **)bv;
11940512a75bSKeith Busch 	ssize_t size, left;
11950512a75bSKeith Busch 	unsigned len, i;
11960512a75bSKeith Busch 	size_t offset;
11974977d121SNaohiro Aota 	int ret = 0;
11980512a75bSKeith Busch 
11990512a75bSKeith Busch 	if (WARN_ON_ONCE(!max_append_sectors))
12000512a75bSKeith Busch 		return 0;
12010512a75bSKeith Busch 
12020512a75bSKeith Busch 	/*
12030512a75bSKeith Busch 	 * Move page array up in the allocated memory for the bio vecs as far as
12040512a75bSKeith Busch 	 * possible so that we can start filling biovecs from the beginning
12050512a75bSKeith Busch 	 * without overwriting the temporary page array.
12060512a75bSKeith Busch 	 */
12070512a75bSKeith Busch 	BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2);
12080512a75bSKeith Busch 	pages += entries_left * (PAGE_PTRS_PER_BVEC - 1);
12090512a75bSKeith Busch 
12100512a75bSKeith Busch 	size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset);
12110512a75bSKeith Busch 	if (unlikely(size <= 0))
12120512a75bSKeith Busch 		return size ? size : -EFAULT;
12130512a75bSKeith Busch 
12140512a75bSKeith Busch 	for (left = size, i = 0; left > 0; left -= len, i++) {
12150512a75bSKeith Busch 		struct page *page = pages[i];
12160512a75bSKeith Busch 		bool same_page = false;
12170512a75bSKeith Busch 
12180512a75bSKeith Busch 		len = min_t(size_t, PAGE_SIZE - offset, left);
12190512a75bSKeith Busch 		if (bio_add_hw_page(q, bio, page, len, offset,
12204977d121SNaohiro Aota 				max_append_sectors, &same_page) != len) {
1221d9cf3bd5SPavel Begunkov 			bio_put_pages(pages + i, left, offset);
12224977d121SNaohiro Aota 			ret = -EINVAL;
12234977d121SNaohiro Aota 			break;
12244977d121SNaohiro Aota 		}
12250512a75bSKeith Busch 		if (same_page)
12260512a75bSKeith Busch 			put_page(page);
12270512a75bSKeith Busch 		offset = 0;
12280512a75bSKeith Busch 	}
12290512a75bSKeith Busch 
12304977d121SNaohiro Aota 	iov_iter_advance(iter, size - left);
12314977d121SNaohiro Aota 	return ret;
12320512a75bSKeith Busch }
12330512a75bSKeith Busch 
123417d51b10SMartin Wilck /**
12356d0c48aeSJens Axboe  * bio_iov_iter_get_pages - add user or kernel pages to a bio
123617d51b10SMartin Wilck  * @bio: bio to add pages to
12376d0c48aeSJens Axboe  * @iter: iov iterator describing the region to be added
123817d51b10SMartin Wilck  *
12396d0c48aeSJens Axboe  * This takes either an iterator pointing to user memory, or one pointing to
12406d0c48aeSJens Axboe  * kernel pages (BVEC iterator). If we're adding user pages, we pin them and
12416d0c48aeSJens Axboe  * map them into the kernel. On IO completion, the caller should put those
1242c42bca92SPavel Begunkov  * pages. For bvec based iterators bio_iov_iter_get_pages() uses the provided
1243c42bca92SPavel Begunkov  * bvecs rather than copying them. Hence anyone issuing kiocb based IO needs
1244c42bca92SPavel Begunkov  * to ensure the bvecs and pages stay referenced until the submitted I/O is
1245c42bca92SPavel Begunkov  * completed by a call to ->ki_complete() or returns with an error other than
1246c42bca92SPavel Begunkov  * -EIOCBQUEUED. The caller needs to check if the bio is flagged BIO_NO_PAGE_REF
1247c42bca92SPavel Begunkov  * on IO completion. If it isn't, then pages should be released.
12486d0c48aeSJens Axboe  *
124917d51b10SMartin Wilck  * The function tries, but does not guarantee, to pin as many pages as
12505cd3ddc1SMauro Carvalho Chehab  * fit into the bio, or are requested in @iter, whatever is smaller. If
12516d0c48aeSJens Axboe  * MM encounters an error pinning the requested pages, it stops. Error
12526d0c48aeSJens Axboe  * is returned only if 0 pages could be pinned.
12530cf41e5eSPavel Begunkov  *
12540cf41e5eSPavel Begunkov  * It's intended for direct IO, so doesn't do PSI tracking, the caller is
12550cf41e5eSPavel Begunkov  * responsible for setting BIO_WORKINGSET if necessary.
125617d51b10SMartin Wilck  */
125717d51b10SMartin Wilck int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
125817d51b10SMartin Wilck {
1259c42bca92SPavel Begunkov 	int ret = 0;
126014eacf12SChristoph Hellwig 
1261c42bca92SPavel Begunkov 	if (iov_iter_is_bvec(iter)) {
1262fa5fa8ecSPavel Begunkov 		bio_iov_bvec_set(bio, iter);
1263fa5fa8ecSPavel Begunkov 		iov_iter_advance(iter, bio->bi_iter.bi_size);
1264fa5fa8ecSPavel Begunkov 		return 0;
126586004515SChristoph Hellwig 	}
126617d51b10SMartin Wilck 
126717d51b10SMartin Wilck 	do {
1268c42bca92SPavel Begunkov 		if (bio_op(bio) == REQ_OP_ZONE_APPEND)
12690512a75bSKeith Busch 			ret = __bio_iov_append_get_pages(bio, iter);
12706d0c48aeSJens Axboe 		else
12716d0c48aeSJens Axboe 			ret = __bio_iov_iter_get_pages(bio, iter);
127279d08f89SMing Lei 	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
127317d51b10SMartin Wilck 
12740cf41e5eSPavel Begunkov 	/* don't account direct I/O as memory stall */
12750cf41e5eSPavel Begunkov 	bio_clear_flag(bio, BIO_WORKINGSET);
127614eacf12SChristoph Hellwig 	return bio->bi_vcnt ? 0 : ret;
127717d51b10SMartin Wilck }
127829b2a3aaSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages);
12792cefe4dbSKent Overstreet 
12804246a0b6SChristoph Hellwig static void submit_bio_wait_endio(struct bio *bio)
1281f9c78b2bSJens Axboe {
128265e53aabSChristoph Hellwig 	complete(bio->bi_private);
1283f9c78b2bSJens Axboe }
1284f9c78b2bSJens Axboe 
1285f9c78b2bSJens Axboe /**
1286f9c78b2bSJens Axboe  * submit_bio_wait - submit a bio, and wait until it completes
1287f9c78b2bSJens Axboe  * @bio: The &struct bio which describes the I/O
1288f9c78b2bSJens Axboe  *
1289f9c78b2bSJens Axboe  * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
1290f9c78b2bSJens Axboe  * bio_endio() on failure.
12913d289d68SJan Kara  *
12923d289d68SJan Kara  * WARNING: Unlike to how submit_bio() is usually used, this function does not
12933d289d68SJan Kara  * result in bio reference to be consumed. The caller must drop the reference
12943d289d68SJan Kara  * on his own.
1295f9c78b2bSJens Axboe  */
12964e49ea4aSMike Christie int submit_bio_wait(struct bio *bio)
1297f9c78b2bSJens Axboe {
1298309dca30SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK_MAP(done,
1299309dca30SChristoph Hellwig 			bio->bi_bdev->bd_disk->lockdep_map);
1300de6a78b6SMing Lei 	unsigned long hang_check;
1301f9c78b2bSJens Axboe 
130265e53aabSChristoph Hellwig 	bio->bi_private = &done;
1303f9c78b2bSJens Axboe 	bio->bi_end_io = submit_bio_wait_endio;
13041eff9d32SJens Axboe 	bio->bi_opf |= REQ_SYNC;
13054e49ea4aSMike Christie 	submit_bio(bio);
1306de6a78b6SMing Lei 
1307de6a78b6SMing Lei 	/* Prevent hang_check timer from firing at us during very long I/O */
1308de6a78b6SMing Lei 	hang_check = sysctl_hung_task_timeout_secs;
1309de6a78b6SMing Lei 	if (hang_check)
1310de6a78b6SMing Lei 		while (!wait_for_completion_io_timeout(&done,
1311de6a78b6SMing Lei 					hang_check * (HZ/2)))
1312de6a78b6SMing Lei 			;
1313de6a78b6SMing Lei 	else
131465e53aabSChristoph Hellwig 		wait_for_completion_io(&done);
1315f9c78b2bSJens Axboe 
131665e53aabSChristoph Hellwig 	return blk_status_to_errno(bio->bi_status);
1317f9c78b2bSJens Axboe }
1318f9c78b2bSJens Axboe EXPORT_SYMBOL(submit_bio_wait);
1319f9c78b2bSJens Axboe 
1320d4aa57a1SJens Axboe void __bio_advance(struct bio *bio, unsigned bytes)
1321f9c78b2bSJens Axboe {
1322f9c78b2bSJens Axboe 	if (bio_integrity(bio))
1323f9c78b2bSJens Axboe 		bio_integrity_advance(bio, bytes);
1324f9c78b2bSJens Axboe 
1325a892c8d5SSatya Tangirala 	bio_crypt_advance(bio, bytes);
1326f9c78b2bSJens Axboe 	bio_advance_iter(bio, &bio->bi_iter, bytes);
1327f9c78b2bSJens Axboe }
1328d4aa57a1SJens Axboe EXPORT_SYMBOL(__bio_advance);
1329f9c78b2bSJens Axboe 
133045db54d5SKent Overstreet void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
133145db54d5SKent Overstreet 			struct bio *src, struct bvec_iter *src_iter)
1332f9c78b2bSJens Axboe {
133345db54d5SKent Overstreet 	while (src_iter->bi_size && dst_iter->bi_size) {
1334f8b679a0SChristoph Hellwig 		struct bio_vec src_bv = bio_iter_iovec(src, *src_iter);
1335f8b679a0SChristoph Hellwig 		struct bio_vec dst_bv = bio_iter_iovec(dst, *dst_iter);
1336f8b679a0SChristoph Hellwig 		unsigned int bytes = min(src_bv.bv_len, dst_bv.bv_len);
1337f8b679a0SChristoph Hellwig 		void *src_buf;
133845db54d5SKent Overstreet 
1339f8b679a0SChristoph Hellwig 		src_buf = bvec_kmap_local(&src_bv);
1340f8b679a0SChristoph Hellwig 		memcpy_to_bvec(&dst_bv, src_buf);
1341f8b679a0SChristoph Hellwig 		kunmap_local(src_buf);
13426e6e811dSKent Overstreet 
134322b56c29SPavel Begunkov 		bio_advance_iter_single(src, src_iter, bytes);
134422b56c29SPavel Begunkov 		bio_advance_iter_single(dst, dst_iter, bytes);
134545db54d5SKent Overstreet 	}
134645db54d5SKent Overstreet }
134745db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data_iter);
134845db54d5SKent Overstreet 
134945db54d5SKent Overstreet /**
135045db54d5SKent Overstreet  * bio_copy_data - copy contents of data buffers from one bio to another
135145db54d5SKent Overstreet  * @src: source bio
135245db54d5SKent Overstreet  * @dst: destination bio
135345db54d5SKent Overstreet  *
135445db54d5SKent Overstreet  * Stops when it reaches the end of either @src or @dst - that is, copies
135545db54d5SKent Overstreet  * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
135645db54d5SKent Overstreet  */
135745db54d5SKent Overstreet void bio_copy_data(struct bio *dst, struct bio *src)
135845db54d5SKent Overstreet {
135945db54d5SKent Overstreet 	struct bvec_iter src_iter = src->bi_iter;
136045db54d5SKent Overstreet 	struct bvec_iter dst_iter = dst->bi_iter;
136145db54d5SKent Overstreet 
136245db54d5SKent Overstreet 	bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
136345db54d5SKent Overstreet }
136445db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data);
136545db54d5SKent Overstreet 
1366491221f8SGuoqing Jiang void bio_free_pages(struct bio *bio)
13671dfa0f68SChristoph Hellwig {
13681dfa0f68SChristoph Hellwig 	struct bio_vec *bvec;
13696dc4f100SMing Lei 	struct bvec_iter_all iter_all;
13701dfa0f68SChristoph Hellwig 
13712b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all)
13721dfa0f68SChristoph Hellwig 		__free_page(bvec->bv_page);
13731dfa0f68SChristoph Hellwig }
1374491221f8SGuoqing Jiang EXPORT_SYMBOL(bio_free_pages);
13751dfa0f68SChristoph Hellwig 
1376f9c78b2bSJens Axboe /*
1377f9c78b2bSJens Axboe  * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
1378f9c78b2bSJens Axboe  * for performing direct-IO in BIOs.
1379f9c78b2bSJens Axboe  *
1380f9c78b2bSJens Axboe  * The problem is that we cannot run set_page_dirty() from interrupt context
1381f9c78b2bSJens Axboe  * because the required locks are not interrupt-safe.  So what we can do is to
1382f9c78b2bSJens Axboe  * mark the pages dirty _before_ performing IO.  And in interrupt context,
1383f9c78b2bSJens Axboe  * check that the pages are still dirty.   If so, fine.  If not, redirty them
1384f9c78b2bSJens Axboe  * in process context.
1385f9c78b2bSJens Axboe  *
1386f9c78b2bSJens Axboe  * We special-case compound pages here: normally this means reads into hugetlb
1387f9c78b2bSJens Axboe  * pages.  The logic in here doesn't really work right for compound pages
1388f9c78b2bSJens Axboe  * because the VM does not uniformly chase down the head page in all cases.
1389f9c78b2bSJens Axboe  * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
1390f9c78b2bSJens Axboe  * handle them at all.  So we skip compound pages here at an early stage.
1391f9c78b2bSJens Axboe  *
1392f9c78b2bSJens Axboe  * Note that this code is very hard to test under normal circumstances because
1393f9c78b2bSJens Axboe  * direct-io pins the pages with get_user_pages().  This makes
1394f9c78b2bSJens Axboe  * is_page_cache_freeable return false, and the VM will not clean the pages.
1395f9c78b2bSJens Axboe  * But other code (eg, flusher threads) could clean the pages if they are mapped
1396f9c78b2bSJens Axboe  * pagecache.
1397f9c78b2bSJens Axboe  *
1398f9c78b2bSJens Axboe  * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
1399f9c78b2bSJens Axboe  * deferred bio dirtying paths.
1400f9c78b2bSJens Axboe  */
1401f9c78b2bSJens Axboe 
1402f9c78b2bSJens Axboe /*
1403f9c78b2bSJens Axboe  * bio_set_pages_dirty() will mark all the bio's pages as dirty.
1404f9c78b2bSJens Axboe  */
1405f9c78b2bSJens Axboe void bio_set_pages_dirty(struct bio *bio)
1406f9c78b2bSJens Axboe {
1407f9c78b2bSJens Axboe 	struct bio_vec *bvec;
14086dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1409f9c78b2bSJens Axboe 
14102b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
14113bb50983SChristoph Hellwig 		if (!PageCompound(bvec->bv_page))
14123bb50983SChristoph Hellwig 			set_page_dirty_lock(bvec->bv_page);
1413f9c78b2bSJens Axboe 	}
1414f9c78b2bSJens Axboe }
1415f9c78b2bSJens Axboe 
1416f9c78b2bSJens Axboe /*
1417f9c78b2bSJens Axboe  * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
1418f9c78b2bSJens Axboe  * If they are, then fine.  If, however, some pages are clean then they must
1419f9c78b2bSJens Axboe  * have been written out during the direct-IO read.  So we take another ref on
142024d5493fSChristoph Hellwig  * the BIO and re-dirty the pages in process context.
1421f9c78b2bSJens Axboe  *
1422f9c78b2bSJens Axboe  * It is expected that bio_check_pages_dirty() will wholly own the BIO from
1423ea1754a0SKirill A. Shutemov  * here on.  It will run one put_page() against each page and will run one
1424ea1754a0SKirill A. Shutemov  * bio_put() against the BIO.
1425f9c78b2bSJens Axboe  */
1426f9c78b2bSJens Axboe 
1427f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work);
1428f9c78b2bSJens Axboe 
1429f9c78b2bSJens Axboe static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
1430f9c78b2bSJens Axboe static DEFINE_SPINLOCK(bio_dirty_lock);
1431f9c78b2bSJens Axboe static struct bio *bio_dirty_list;
1432f9c78b2bSJens Axboe 
1433f9c78b2bSJens Axboe /*
1434f9c78b2bSJens Axboe  * This runs in process context
1435f9c78b2bSJens Axboe  */
1436f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work)
1437f9c78b2bSJens Axboe {
143824d5493fSChristoph Hellwig 	struct bio *bio, *next;
1439f9c78b2bSJens Axboe 
144024d5493fSChristoph Hellwig 	spin_lock_irq(&bio_dirty_lock);
144124d5493fSChristoph Hellwig 	next = bio_dirty_list;
1442f9c78b2bSJens Axboe 	bio_dirty_list = NULL;
144324d5493fSChristoph Hellwig 	spin_unlock_irq(&bio_dirty_lock);
1444f9c78b2bSJens Axboe 
144524d5493fSChristoph Hellwig 	while ((bio = next) != NULL) {
144624d5493fSChristoph Hellwig 		next = bio->bi_private;
1447f9c78b2bSJens Axboe 
1448d241a95fSChristoph Hellwig 		bio_release_pages(bio, true);
1449f9c78b2bSJens Axboe 		bio_put(bio);
1450f9c78b2bSJens Axboe 	}
1451f9c78b2bSJens Axboe }
1452f9c78b2bSJens Axboe 
1453f9c78b2bSJens Axboe void bio_check_pages_dirty(struct bio *bio)
1454f9c78b2bSJens Axboe {
1455f9c78b2bSJens Axboe 	struct bio_vec *bvec;
145624d5493fSChristoph Hellwig 	unsigned long flags;
14576dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1458f9c78b2bSJens Axboe 
14592b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
146024d5493fSChristoph Hellwig 		if (!PageDirty(bvec->bv_page) && !PageCompound(bvec->bv_page))
146124d5493fSChristoph Hellwig 			goto defer;
1462f9c78b2bSJens Axboe 	}
1463f9c78b2bSJens Axboe 
1464d241a95fSChristoph Hellwig 	bio_release_pages(bio, false);
146524d5493fSChristoph Hellwig 	bio_put(bio);
146624d5493fSChristoph Hellwig 	return;
146724d5493fSChristoph Hellwig defer:
1468f9c78b2bSJens Axboe 	spin_lock_irqsave(&bio_dirty_lock, flags);
1469f9c78b2bSJens Axboe 	bio->bi_private = bio_dirty_list;
1470f9c78b2bSJens Axboe 	bio_dirty_list = bio;
1471f9c78b2bSJens Axboe 	spin_unlock_irqrestore(&bio_dirty_lock, flags);
1472f9c78b2bSJens Axboe 	schedule_work(&bio_dirty_work);
1473f9c78b2bSJens Axboe }
1474f9c78b2bSJens Axboe 
1475c4cf5261SJens Axboe static inline bool bio_remaining_done(struct bio *bio)
1476c4cf5261SJens Axboe {
1477c4cf5261SJens Axboe 	/*
1478c4cf5261SJens Axboe 	 * If we're not chaining, then ->__bi_remaining is always 1 and
1479c4cf5261SJens Axboe 	 * we always end io on the first invocation.
1480c4cf5261SJens Axboe 	 */
1481c4cf5261SJens Axboe 	if (!bio_flagged(bio, BIO_CHAIN))
1482c4cf5261SJens Axboe 		return true;
1483c4cf5261SJens Axboe 
1484c4cf5261SJens Axboe 	BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
1485c4cf5261SJens Axboe 
1486326e1dbbSMike Snitzer 	if (atomic_dec_and_test(&bio->__bi_remaining)) {
1487b7c44ed9SJens Axboe 		bio_clear_flag(bio, BIO_CHAIN);
1488c4cf5261SJens Axboe 		return true;
1489326e1dbbSMike Snitzer 	}
1490c4cf5261SJens Axboe 
1491c4cf5261SJens Axboe 	return false;
1492c4cf5261SJens Axboe }
1493c4cf5261SJens Axboe 
1494f9c78b2bSJens Axboe /**
1495f9c78b2bSJens Axboe  * bio_endio - end I/O on a bio
1496f9c78b2bSJens Axboe  * @bio:	bio
1497f9c78b2bSJens Axboe  *
1498f9c78b2bSJens Axboe  * Description:
14994246a0b6SChristoph Hellwig  *   bio_endio() will end I/O on the whole bio. bio_endio() is the preferred
15004246a0b6SChristoph Hellwig  *   way to end I/O on a bio. No one should call bi_end_io() directly on a
15014246a0b6SChristoph Hellwig  *   bio unless they own it and thus know that it has an end_io function.
1502fbbaf700SNeilBrown  *
1503fbbaf700SNeilBrown  *   bio_endio() can be called several times on a bio that has been chained
1504fbbaf700SNeilBrown  *   using bio_chain().  The ->bi_end_io() function will only be called the
150560b6a7e6SEdward Hsieh  *   last time.
1506f9c78b2bSJens Axboe  **/
15074246a0b6SChristoph Hellwig void bio_endio(struct bio *bio)
1508f9c78b2bSJens Axboe {
1509ba8c6967SChristoph Hellwig again:
15102b885517SChristoph Hellwig 	if (!bio_remaining_done(bio))
1511ba8c6967SChristoph Hellwig 		return;
15127c20f116SChristoph Hellwig 	if (!bio_integrity_endio(bio))
15137c20f116SChristoph Hellwig 		return;
1514f9c78b2bSJens Axboe 
1515a647a524SMing Lei 	if (bio->bi_bdev && bio_flagged(bio, BIO_TRACKED))
15163caee463SPavel Begunkov 		rq_qos_done_bio(bdev_get_queue(bio->bi_bdev), bio);
151767b42d0bSJosef Bacik 
151860b6a7e6SEdward Hsieh 	if (bio->bi_bdev && bio_flagged(bio, BIO_TRACE_COMPLETION)) {
15193caee463SPavel Begunkov 		trace_block_bio_complete(bdev_get_queue(bio->bi_bdev), bio);
152060b6a7e6SEdward Hsieh 		bio_clear_flag(bio, BIO_TRACE_COMPLETION);
152160b6a7e6SEdward Hsieh 	}
152260b6a7e6SEdward Hsieh 
1523f9c78b2bSJens Axboe 	/*
1524ba8c6967SChristoph Hellwig 	 * Need to have a real endio function for chained bios, otherwise
1525ba8c6967SChristoph Hellwig 	 * various corner cases will break (like stacking block devices that
1526ba8c6967SChristoph Hellwig 	 * save/restore bi_end_io) - however, we want to avoid unbounded
1527ba8c6967SChristoph Hellwig 	 * recursion and blowing the stack. Tail call optimization would
1528ba8c6967SChristoph Hellwig 	 * handle this, but compiling with frame pointers also disables
1529ba8c6967SChristoph Hellwig 	 * gcc's sibling call optimization.
1530f9c78b2bSJens Axboe 	 */
1531f9c78b2bSJens Axboe 	if (bio->bi_end_io == bio_chain_endio) {
153238f8baaeSChristoph Hellwig 		bio = __bio_chain_endio(bio);
1533ba8c6967SChristoph Hellwig 		goto again;
1534ba8c6967SChristoph Hellwig 	}
1535ba8c6967SChristoph Hellwig 
15369e234eeaSShaohua Li 	blk_throtl_bio_endio(bio);
1537b222dd2fSShaohua Li 	/* release cgroup info */
1538b222dd2fSShaohua Li 	bio_uninit(bio);
1539f9c78b2bSJens Axboe 	if (bio->bi_end_io)
15404246a0b6SChristoph Hellwig 		bio->bi_end_io(bio);
1541f9c78b2bSJens Axboe }
1542f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_endio);
1543f9c78b2bSJens Axboe 
1544f9c78b2bSJens Axboe /**
1545f9c78b2bSJens Axboe  * bio_split - split a bio
1546f9c78b2bSJens Axboe  * @bio:	bio to split
1547f9c78b2bSJens Axboe  * @sectors:	number of sectors to split from the front of @bio
1548f9c78b2bSJens Axboe  * @gfp:	gfp mask
1549f9c78b2bSJens Axboe  * @bs:		bio set to allocate from
1550f9c78b2bSJens Axboe  *
1551f9c78b2bSJens Axboe  * Allocates and returns a new bio which represents @sectors from the start of
1552f9c78b2bSJens Axboe  * @bio, and updates @bio to represent the remaining sectors.
1553f9c78b2bSJens Axboe  *
1554f3f5da62SMartin K. Petersen  * Unless this is a discard request the newly allocated bio will point
1555dad77584SBart Van Assche  * to @bio's bi_io_vec. It is the caller's responsibility to ensure that
1556dad77584SBart Van Assche  * neither @bio nor @bs are freed before the split bio.
1557f9c78b2bSJens Axboe  */
1558f9c78b2bSJens Axboe struct bio *bio_split(struct bio *bio, int sectors,
1559f9c78b2bSJens Axboe 		      gfp_t gfp, struct bio_set *bs)
1560f9c78b2bSJens Axboe {
1561f341a4d3SMikulas Patocka 	struct bio *split;
1562f9c78b2bSJens Axboe 
1563f9c78b2bSJens Axboe 	BUG_ON(sectors <= 0);
1564f9c78b2bSJens Axboe 	BUG_ON(sectors >= bio_sectors(bio));
1565f9c78b2bSJens Axboe 
15660512a75bSKeith Busch 	/* Zone append commands cannot be split */
15670512a75bSKeith Busch 	if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
15680512a75bSKeith Busch 		return NULL;
15690512a75bSKeith Busch 
1570f9c78b2bSJens Axboe 	split = bio_clone_fast(bio, gfp, bs);
1571f9c78b2bSJens Axboe 	if (!split)
1572f9c78b2bSJens Axboe 		return NULL;
1573f9c78b2bSJens Axboe 
1574f9c78b2bSJens Axboe 	split->bi_iter.bi_size = sectors << 9;
1575f9c78b2bSJens Axboe 
1576f9c78b2bSJens Axboe 	if (bio_integrity(split))
1577fbd08e76SDmitry Monakhov 		bio_integrity_trim(split);
1578f9c78b2bSJens Axboe 
1579f9c78b2bSJens Axboe 	bio_advance(bio, split->bi_iter.bi_size);
1580f9c78b2bSJens Axboe 
1581fbbaf700SNeilBrown 	if (bio_flagged(bio, BIO_TRACE_COMPLETION))
158220d59023SGoldwyn Rodrigues 		bio_set_flag(split, BIO_TRACE_COMPLETION);
1583fbbaf700SNeilBrown 
1584f9c78b2bSJens Axboe 	return split;
1585f9c78b2bSJens Axboe }
1586f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_split);
1587f9c78b2bSJens Axboe 
1588f9c78b2bSJens Axboe /**
1589f9c78b2bSJens Axboe  * bio_trim - trim a bio
1590f9c78b2bSJens Axboe  * @bio:	bio to trim
1591f9c78b2bSJens Axboe  * @offset:	number of sectors to trim from the front of @bio
1592f9c78b2bSJens Axboe  * @size:	size we want to trim @bio to, in sectors
1593e83502caSChaitanya Kulkarni  *
1594e83502caSChaitanya Kulkarni  * This function is typically used for bios that are cloned and submitted
1595e83502caSChaitanya Kulkarni  * to the underlying device in parts.
1596f9c78b2bSJens Axboe  */
1597e83502caSChaitanya Kulkarni void bio_trim(struct bio *bio, sector_t offset, sector_t size)
1598f9c78b2bSJens Axboe {
1599e83502caSChaitanya Kulkarni 	if (WARN_ON_ONCE(offset > BIO_MAX_SECTORS || size > BIO_MAX_SECTORS ||
1600e83502caSChaitanya Kulkarni 			 offset + size > bio->bi_iter.bi_size))
1601e83502caSChaitanya Kulkarni 		return;
1602f9c78b2bSJens Axboe 
1603f9c78b2bSJens Axboe 	size <<= 9;
1604f9c78b2bSJens Axboe 	if (offset == 0 && size == bio->bi_iter.bi_size)
1605f9c78b2bSJens Axboe 		return;
1606f9c78b2bSJens Axboe 
1607f9c78b2bSJens Axboe 	bio_advance(bio, offset << 9);
1608f9c78b2bSJens Axboe 	bio->bi_iter.bi_size = size;
1609376a78abSDmitry Monakhov 
1610376a78abSDmitry Monakhov 	if (bio_integrity(bio))
1611fbd08e76SDmitry Monakhov 		bio_integrity_trim(bio);
1612f9c78b2bSJens Axboe }
1613f9c78b2bSJens Axboe EXPORT_SYMBOL_GPL(bio_trim);
1614f9c78b2bSJens Axboe 
1615f9c78b2bSJens Axboe /*
1616f9c78b2bSJens Axboe  * create memory pools for biovec's in a bio_set.
1617f9c78b2bSJens Axboe  * use the global biovec slabs created for general use.
1618f9c78b2bSJens Axboe  */
16198aa6ba2fSKent Overstreet int biovec_init_pool(mempool_t *pool, int pool_entries)
1620f9c78b2bSJens Axboe {
16217a800a20SChristoph Hellwig 	struct biovec_slab *bp = bvec_slabs + ARRAY_SIZE(bvec_slabs) - 1;
1622f9c78b2bSJens Axboe 
16238aa6ba2fSKent Overstreet 	return mempool_init_slab_pool(pool, pool_entries, bp->slab);
1624f9c78b2bSJens Axboe }
1625f9c78b2bSJens Axboe 
1626917a38c7SKent Overstreet /*
1627917a38c7SKent Overstreet  * bioset_exit - exit a bioset initialized with bioset_init()
1628917a38c7SKent Overstreet  *
1629917a38c7SKent Overstreet  * May be called on a zeroed but uninitialized bioset (i.e. allocated with
1630917a38c7SKent Overstreet  * kzalloc()).
1631917a38c7SKent Overstreet  */
1632917a38c7SKent Overstreet void bioset_exit(struct bio_set *bs)
1633f9c78b2bSJens Axboe {
1634be4d234dSJens Axboe 	bio_alloc_cache_destroy(bs);
1635f9c78b2bSJens Axboe 	if (bs->rescue_workqueue)
1636f9c78b2bSJens Axboe 		destroy_workqueue(bs->rescue_workqueue);
1637917a38c7SKent Overstreet 	bs->rescue_workqueue = NULL;
1638f9c78b2bSJens Axboe 
16398aa6ba2fSKent Overstreet 	mempool_exit(&bs->bio_pool);
16408aa6ba2fSKent Overstreet 	mempool_exit(&bs->bvec_pool);
1641f9c78b2bSJens Axboe 
1642f9c78b2bSJens Axboe 	bioset_integrity_free(bs);
1643917a38c7SKent Overstreet 	if (bs->bio_slab)
1644f9c78b2bSJens Axboe 		bio_put_slab(bs);
1645917a38c7SKent Overstreet 	bs->bio_slab = NULL;
1646917a38c7SKent Overstreet }
1647917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_exit);
1648f9c78b2bSJens Axboe 
1649011067b0SNeilBrown /**
1650917a38c7SKent Overstreet  * bioset_init - Initialize a bio_set
1651dad08527SKent Overstreet  * @bs:		pool to initialize
1652917a38c7SKent Overstreet  * @pool_size:	Number of bio and bio_vecs to cache in the mempool
1653917a38c7SKent Overstreet  * @front_pad:	Number of bytes to allocate in front of the returned bio
1654917a38c7SKent Overstreet  * @flags:	Flags to modify behavior, currently %BIOSET_NEED_BVECS
1655917a38c7SKent Overstreet  *              and %BIOSET_NEED_RESCUER
1656917a38c7SKent Overstreet  *
1657dad08527SKent Overstreet  * Description:
1658dad08527SKent Overstreet  *    Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
1659dad08527SKent Overstreet  *    to ask for a number of bytes to be allocated in front of the bio.
1660dad08527SKent Overstreet  *    Front pad allocation is useful for embedding the bio inside
1661dad08527SKent Overstreet  *    another structure, to avoid allocating extra data to go with the bio.
1662dad08527SKent Overstreet  *    Note that the bio must be embedded at the END of that structure always,
1663dad08527SKent Overstreet  *    or things will break badly.
1664dad08527SKent Overstreet  *    If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated
1665dad08527SKent Overstreet  *    for allocating iovecs.  This pool is not needed e.g. for bio_clone_fast().
1666dad08527SKent Overstreet  *    If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used to
1667dad08527SKent Overstreet  *    dispatch queued requests when the mempool runs out of space.
1668dad08527SKent Overstreet  *
1669917a38c7SKent Overstreet  */
1670917a38c7SKent Overstreet int bioset_init(struct bio_set *bs,
1671917a38c7SKent Overstreet 		unsigned int pool_size,
1672917a38c7SKent Overstreet 		unsigned int front_pad,
1673917a38c7SKent Overstreet 		int flags)
1674917a38c7SKent Overstreet {
1675917a38c7SKent Overstreet 	bs->front_pad = front_pad;
16769f180e31SMing Lei 	if (flags & BIOSET_NEED_BVECS)
16779f180e31SMing Lei 		bs->back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
16789f180e31SMing Lei 	else
16799f180e31SMing Lei 		bs->back_pad = 0;
1680917a38c7SKent Overstreet 
1681917a38c7SKent Overstreet 	spin_lock_init(&bs->rescue_lock);
1682917a38c7SKent Overstreet 	bio_list_init(&bs->rescue_list);
1683917a38c7SKent Overstreet 	INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1684917a38c7SKent Overstreet 
168549d1ec85SMing Lei 	bs->bio_slab = bio_find_or_create_slab(bs);
1686917a38c7SKent Overstreet 	if (!bs->bio_slab)
1687917a38c7SKent Overstreet 		return -ENOMEM;
1688917a38c7SKent Overstreet 
1689917a38c7SKent Overstreet 	if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab))
1690917a38c7SKent Overstreet 		goto bad;
1691917a38c7SKent Overstreet 
1692917a38c7SKent Overstreet 	if ((flags & BIOSET_NEED_BVECS) &&
1693917a38c7SKent Overstreet 	    biovec_init_pool(&bs->bvec_pool, pool_size))
1694917a38c7SKent Overstreet 		goto bad;
1695917a38c7SKent Overstreet 
1696be4d234dSJens Axboe 	if (flags & BIOSET_NEED_RESCUER) {
1697be4d234dSJens Axboe 		bs->rescue_workqueue = alloc_workqueue("bioset",
1698be4d234dSJens Axboe 							WQ_MEM_RECLAIM, 0);
1699917a38c7SKent Overstreet 		if (!bs->rescue_workqueue)
1700917a38c7SKent Overstreet 			goto bad;
1701be4d234dSJens Axboe 	}
1702be4d234dSJens Axboe 	if (flags & BIOSET_PERCPU_CACHE) {
1703be4d234dSJens Axboe 		bs->cache = alloc_percpu(struct bio_alloc_cache);
1704be4d234dSJens Axboe 		if (!bs->cache)
1705be4d234dSJens Axboe 			goto bad;
1706be4d234dSJens Axboe 		cpuhp_state_add_instance_nocalls(CPUHP_BIO_DEAD, &bs->cpuhp_dead);
1707be4d234dSJens Axboe 	}
1708917a38c7SKent Overstreet 
1709917a38c7SKent Overstreet 	return 0;
1710917a38c7SKent Overstreet bad:
1711917a38c7SKent Overstreet 	bioset_exit(bs);
1712917a38c7SKent Overstreet 	return -ENOMEM;
1713917a38c7SKent Overstreet }
1714917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_init);
1715917a38c7SKent Overstreet 
171628e89fd9SJens Axboe /*
171728e89fd9SJens Axboe  * Initialize and setup a new bio_set, based on the settings from
171828e89fd9SJens Axboe  * another bio_set.
171928e89fd9SJens Axboe  */
172028e89fd9SJens Axboe int bioset_init_from_src(struct bio_set *bs, struct bio_set *src)
172128e89fd9SJens Axboe {
172228e89fd9SJens Axboe 	int flags;
172328e89fd9SJens Axboe 
172428e89fd9SJens Axboe 	flags = 0;
172528e89fd9SJens Axboe 	if (src->bvec_pool.min_nr)
172628e89fd9SJens Axboe 		flags |= BIOSET_NEED_BVECS;
172728e89fd9SJens Axboe 	if (src->rescue_workqueue)
172828e89fd9SJens Axboe 		flags |= BIOSET_NEED_RESCUER;
172928e89fd9SJens Axboe 
173028e89fd9SJens Axboe 	return bioset_init(bs, src->bio_pool.min_nr, src->front_pad, flags);
173128e89fd9SJens Axboe }
173228e89fd9SJens Axboe EXPORT_SYMBOL(bioset_init_from_src);
173328e89fd9SJens Axboe 
1734be4d234dSJens Axboe /**
1735be4d234dSJens Axboe  * bio_alloc_kiocb - Allocate a bio from bio_set based on kiocb
1736be4d234dSJens Axboe  * @kiocb:	kiocb describing the IO
1737b77c88c2SChristoph Hellwig  * @bdev:	block device to allocate the bio for (can be %NULL)
17380ef47db1SJens Axboe  * @nr_vecs:	number of iovecs to pre-allocate
1739b77c88c2SChristoph Hellwig  * @opf:	operation and flags for bio
1740be4d234dSJens Axboe  * @bs:		bio_set to allocate from
1741be4d234dSJens Axboe  *
1742be4d234dSJens Axboe  * Description:
1743be4d234dSJens Axboe  *    Like @bio_alloc_bioset, but pass in the kiocb. The kiocb is only
1744be4d234dSJens Axboe  *    used to check if we should dip into the per-cpu bio_set allocation
17453d5b3fbeSJens Axboe  *    cache. The allocation uses GFP_KERNEL internally. On return, the
17463d5b3fbeSJens Axboe  *    bio is marked BIO_PERCPU_CACHEABLE, and the final put of the bio
17473d5b3fbeSJens Axboe  *    MUST be done from process context, not hard/soft IRQ.
1748be4d234dSJens Axboe  *
1749be4d234dSJens Axboe  */
1750b77c88c2SChristoph Hellwig struct bio *bio_alloc_kiocb(struct kiocb *kiocb, struct block_device *bdev,
1751b77c88c2SChristoph Hellwig 		unsigned short nr_vecs, unsigned int opf, struct bio_set *bs)
1752be4d234dSJens Axboe {
1753be4d234dSJens Axboe 	struct bio_alloc_cache *cache;
1754be4d234dSJens Axboe 	struct bio *bio;
1755be4d234dSJens Axboe 
1756be4d234dSJens Axboe 	if (!(kiocb->ki_flags & IOCB_ALLOC_CACHE) || nr_vecs > BIO_INLINE_VECS)
1757b77c88c2SChristoph Hellwig 		return bio_alloc_bioset(bdev, nr_vecs, opf, GFP_KERNEL, bs);
1758be4d234dSJens Axboe 
1759be4d234dSJens Axboe 	cache = per_cpu_ptr(bs->cache, get_cpu());
1760fcade2ceSJens Axboe 	if (cache->free_list) {
1761fcade2ceSJens Axboe 		bio = cache->free_list;
1762fcade2ceSJens Axboe 		cache->free_list = bio->bi_next;
1763be4d234dSJens Axboe 		cache->nr--;
1764be4d234dSJens Axboe 		put_cpu();
176549add496SChristoph Hellwig 		bio_init(bio, bdev, nr_vecs ? bio->bi_inline_vecs : NULL,
176649add496SChristoph Hellwig 			 nr_vecs, opf);
1767be4d234dSJens Axboe 		bio->bi_pool = bs;
1768be4d234dSJens Axboe 		bio_set_flag(bio, BIO_PERCPU_CACHE);
1769be4d234dSJens Axboe 		return bio;
1770be4d234dSJens Axboe 	}
1771be4d234dSJens Axboe 	put_cpu();
1772b77c88c2SChristoph Hellwig 	bio = bio_alloc_bioset(bdev, nr_vecs, opf, GFP_KERNEL, bs);
1773be4d234dSJens Axboe 	bio_set_flag(bio, BIO_PERCPU_CACHE);
1774be4d234dSJens Axboe 	return bio;
1775be4d234dSJens Axboe }
1776be4d234dSJens Axboe EXPORT_SYMBOL_GPL(bio_alloc_kiocb);
1777be4d234dSJens Axboe 
1778de76fd89SChristoph Hellwig static int __init init_bio(void)
1779f9c78b2bSJens Axboe {
1780f9c78b2bSJens Axboe 	int i;
1781f9c78b2bSJens Axboe 
1782f9c78b2bSJens Axboe 	bio_integrity_init();
1783de76fd89SChristoph Hellwig 
1784de76fd89SChristoph Hellwig 	for (i = 0; i < ARRAY_SIZE(bvec_slabs); i++) {
1785f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + i;
1786f9c78b2bSJens Axboe 
1787de76fd89SChristoph Hellwig 		bvs->slab = kmem_cache_create(bvs->name,
1788de76fd89SChristoph Hellwig 				bvs->nr_vecs * sizeof(struct bio_vec), 0,
1789f9c78b2bSJens Axboe 				SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
1790f9c78b2bSJens Axboe 	}
1791f9c78b2bSJens Axboe 
1792be4d234dSJens Axboe 	cpuhp_setup_state_multi(CPUHP_BIO_DEAD, "block/bio:dead", NULL,
1793be4d234dSJens Axboe 					bio_cpu_dead);
1794be4d234dSJens Axboe 
1795f4f8154aSKent Overstreet 	if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS))
1796f9c78b2bSJens Axboe 		panic("bio: can't allocate bios\n");
1797f9c78b2bSJens Axboe 
1798f4f8154aSKent Overstreet 	if (bioset_integrity_create(&fs_bio_set, BIO_POOL_SIZE))
1799f9c78b2bSJens Axboe 		panic("bio: can't create integrity pool\n");
1800f9c78b2bSJens Axboe 
1801f9c78b2bSJens Axboe 	return 0;
1802f9c78b2bSJens Axboe }
1803f9c78b2bSJens Axboe subsys_initcall(init_bio);
1804