xref: /openbmc/linux/block/bio.c (revision f25cf75a)
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>
18b4c5875dSDamien Le Moal #include <linux/highmem.h>
19de6a78b6SMing Lei #include <linux/sched/sysctl.h>
20a892c8d5SSatya Tangirala #include <linux/blk-crypto.h>
2149d1ec85SMing Lei #include <linux/xarray.h>
22f9c78b2bSJens Axboe 
23f9c78b2bSJens Axboe #include <trace/events/block.h>
249e234eeaSShaohua Li #include "blk.h"
2567b42d0bSJosef Bacik #include "blk-rq-qos.h"
26672fdcf0SMing Lei #include "blk-cgroup.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;
227066ff571SChristoph Hellwig 	void *p = bio;
228066ff571SChristoph Hellwig 
229066ff571SChristoph Hellwig 	WARN_ON_ONCE(!bs);
230f9c78b2bSJens Axboe 
2319ae3b3f5SJens Axboe 	bio_uninit(bio);
2327a800a20SChristoph Hellwig 	bvec_free(&bs->bvec_pool, bio->bi_io_vec, bio->bi_max_vecs);
233066ff571SChristoph Hellwig 	mempool_free(p - bs->front_pad, &bs->bio_pool);
234f9c78b2bSJens Axboe }
235f9c78b2bSJens Axboe 
2369ae3b3f5SJens Axboe /*
2379ae3b3f5SJens Axboe  * Users of this function have their own bio allocation. Subsequently,
2389ae3b3f5SJens Axboe  * they must remember to pair any call to bio_init() with bio_uninit()
2399ae3b3f5SJens Axboe  * when IO has completed, or when the bio is released.
2409ae3b3f5SJens Axboe  */
24149add496SChristoph Hellwig void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *table,
24216458cf3SBart Van Assche 	      unsigned short max_vecs, blk_opf_t opf)
243f9c78b2bSJens Axboe {
244da521626SJens Axboe 	bio->bi_next = NULL;
24549add496SChristoph Hellwig 	bio->bi_bdev = bdev;
24649add496SChristoph Hellwig 	bio->bi_opf = opf;
247da521626SJens Axboe 	bio->bi_flags = 0;
248da521626SJens Axboe 	bio->bi_ioprio = 0;
249da521626SJens Axboe 	bio->bi_status = 0;
250da521626SJens Axboe 	bio->bi_iter.bi_sector = 0;
251da521626SJens Axboe 	bio->bi_iter.bi_size = 0;
252da521626SJens Axboe 	bio->bi_iter.bi_idx = 0;
253da521626SJens Axboe 	bio->bi_iter.bi_bvec_done = 0;
254da521626SJens Axboe 	bio->bi_end_io = NULL;
255da521626SJens Axboe 	bio->bi_private = NULL;
256da521626SJens Axboe #ifdef CONFIG_BLK_CGROUP
257da521626SJens Axboe 	bio->bi_blkg = NULL;
258da521626SJens Axboe 	bio->bi_issue.value = 0;
25949add496SChristoph Hellwig 	if (bdev)
26049add496SChristoph Hellwig 		bio_associate_blkg(bio);
261da521626SJens Axboe #ifdef CONFIG_BLK_CGROUP_IOCOST
262da521626SJens Axboe 	bio->bi_iocost_cost = 0;
263da521626SJens Axboe #endif
264da521626SJens Axboe #endif
265da521626SJens Axboe #ifdef CONFIG_BLK_INLINE_ENCRYPTION
266da521626SJens Axboe 	bio->bi_crypt_context = NULL;
267da521626SJens Axboe #endif
268da521626SJens Axboe #ifdef CONFIG_BLK_DEV_INTEGRITY
269da521626SJens Axboe 	bio->bi_integrity = NULL;
270da521626SJens Axboe #endif
271da521626SJens Axboe 	bio->bi_vcnt = 0;
272da521626SJens Axboe 
273c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
274dac56212SJens Axboe 	atomic_set(&bio->__bi_cnt, 1);
2753e08773cSChristoph Hellwig 	bio->bi_cookie = BLK_QC_T_NONE;
2763a83f467SMing Lei 
2773a83f467SMing Lei 	bio->bi_max_vecs = max_vecs;
278da521626SJens Axboe 	bio->bi_io_vec = table;
279da521626SJens Axboe 	bio->bi_pool = NULL;
280f9c78b2bSJens Axboe }
281f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_init);
282f9c78b2bSJens Axboe 
283f9c78b2bSJens Axboe /**
284f9c78b2bSJens Axboe  * bio_reset - reinitialize a bio
285f9c78b2bSJens Axboe  * @bio:	bio to reset
286a7c50c94SChristoph Hellwig  * @bdev:	block device to use the bio for
287a7c50c94SChristoph Hellwig  * @opf:	operation and flags for bio
288f9c78b2bSJens Axboe  *
289f9c78b2bSJens Axboe  * Description:
290f9c78b2bSJens Axboe  *   After calling bio_reset(), @bio will be in the same state as a freshly
291f9c78b2bSJens Axboe  *   allocated bio returned bio bio_alloc_bioset() - the only fields that are
292f9c78b2bSJens Axboe  *   preserved are the ones that are initialized by bio_alloc_bioset(). See
293f9c78b2bSJens Axboe  *   comment in struct bio.
294f9c78b2bSJens Axboe  */
29516458cf3SBart Van Assche void bio_reset(struct bio *bio, struct block_device *bdev, blk_opf_t opf)
296f9c78b2bSJens Axboe {
2979ae3b3f5SJens Axboe 	bio_uninit(bio);
298f9c78b2bSJens Axboe 	memset(bio, 0, BIO_RESET_BYTES);
299c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
300a7c50c94SChristoph Hellwig 	bio->bi_bdev = bdev;
30178e34374SChristoph Hellwig 	if (bio->bi_bdev)
30278e34374SChristoph Hellwig 		bio_associate_blkg(bio);
303a7c50c94SChristoph Hellwig 	bio->bi_opf = opf;
304f9c78b2bSJens Axboe }
305f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_reset);
306f9c78b2bSJens Axboe 
30738f8baaeSChristoph Hellwig static struct bio *__bio_chain_endio(struct bio *bio)
308f9c78b2bSJens Axboe {
3094246a0b6SChristoph Hellwig 	struct bio *parent = bio->bi_private;
3104246a0b6SChristoph Hellwig 
3113edf5346SYufen Yu 	if (bio->bi_status && !parent->bi_status)
3124e4cbee9SChristoph Hellwig 		parent->bi_status = bio->bi_status;
313f9c78b2bSJens Axboe 	bio_put(bio);
31438f8baaeSChristoph Hellwig 	return parent;
31538f8baaeSChristoph Hellwig }
31638f8baaeSChristoph Hellwig 
31738f8baaeSChristoph Hellwig static void bio_chain_endio(struct bio *bio)
31838f8baaeSChristoph Hellwig {
31938f8baaeSChristoph Hellwig 	bio_endio(__bio_chain_endio(bio));
320f9c78b2bSJens Axboe }
321f9c78b2bSJens Axboe 
322f9c78b2bSJens Axboe /**
323f9c78b2bSJens Axboe  * bio_chain - chain bio completions
324f9c78b2bSJens Axboe  * @bio: the target bio
3255b874af6SMauro Carvalho Chehab  * @parent: the parent bio of @bio
326f9c78b2bSJens Axboe  *
327f9c78b2bSJens Axboe  * The caller won't have a bi_end_io called when @bio completes - instead,
328f9c78b2bSJens Axboe  * @parent's bi_end_io won't be called until both @parent and @bio have
329f9c78b2bSJens Axboe  * completed; the chained bio will also be freed when it completes.
330f9c78b2bSJens Axboe  *
331f9c78b2bSJens Axboe  * The caller must not set bi_private or bi_end_io in @bio.
332f9c78b2bSJens Axboe  */
333f9c78b2bSJens Axboe void bio_chain(struct bio *bio, struct bio *parent)
334f9c78b2bSJens Axboe {
335f9c78b2bSJens Axboe 	BUG_ON(bio->bi_private || bio->bi_end_io);
336f9c78b2bSJens Axboe 
337f9c78b2bSJens Axboe 	bio->bi_private = parent;
338f9c78b2bSJens Axboe 	bio->bi_end_io	= bio_chain_endio;
339c4cf5261SJens Axboe 	bio_inc_remaining(parent);
340f9c78b2bSJens Axboe }
341f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_chain);
342f9c78b2bSJens Axboe 
3430a3140eaSChaitanya Kulkarni struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev,
34416458cf3SBart Van Assche 		unsigned int nr_pages, blk_opf_t opf, gfp_t gfp)
3453b005bf6SChristoph Hellwig {
34607888c66SChristoph Hellwig 	struct bio *new = bio_alloc(bdev, nr_pages, opf, gfp);
3470a3140eaSChaitanya Kulkarni 
3483b005bf6SChristoph Hellwig 	if (bio) {
3493b005bf6SChristoph Hellwig 		bio_chain(bio, new);
3503b005bf6SChristoph Hellwig 		submit_bio(bio);
3513b005bf6SChristoph Hellwig 	}
3523b005bf6SChristoph Hellwig 
3533b005bf6SChristoph Hellwig 	return new;
3543b005bf6SChristoph Hellwig }
3553b005bf6SChristoph Hellwig EXPORT_SYMBOL_GPL(blk_next_bio);
3563b005bf6SChristoph Hellwig 
357f9c78b2bSJens Axboe static void bio_alloc_rescue(struct work_struct *work)
358f9c78b2bSJens Axboe {
359f9c78b2bSJens Axboe 	struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
360f9c78b2bSJens Axboe 	struct bio *bio;
361f9c78b2bSJens Axboe 
362f9c78b2bSJens Axboe 	while (1) {
363f9c78b2bSJens Axboe 		spin_lock(&bs->rescue_lock);
364f9c78b2bSJens Axboe 		bio = bio_list_pop(&bs->rescue_list);
365f9c78b2bSJens Axboe 		spin_unlock(&bs->rescue_lock);
366f9c78b2bSJens Axboe 
367f9c78b2bSJens Axboe 		if (!bio)
368f9c78b2bSJens Axboe 			break;
369f9c78b2bSJens Axboe 
370ed00aabdSChristoph Hellwig 		submit_bio_noacct(bio);
371f9c78b2bSJens Axboe 	}
372f9c78b2bSJens Axboe }
373f9c78b2bSJens Axboe 
374f9c78b2bSJens Axboe static void punt_bios_to_rescuer(struct bio_set *bs)
375f9c78b2bSJens Axboe {
376f9c78b2bSJens Axboe 	struct bio_list punt, nopunt;
377f9c78b2bSJens Axboe 	struct bio *bio;
378f9c78b2bSJens Axboe 
37947e0fb46SNeilBrown 	if (WARN_ON_ONCE(!bs->rescue_workqueue))
38047e0fb46SNeilBrown 		return;
381f9c78b2bSJens Axboe 	/*
382f9c78b2bSJens Axboe 	 * In order to guarantee forward progress we must punt only bios that
383f9c78b2bSJens Axboe 	 * were allocated from this bio_set; otherwise, if there was a bio on
384f9c78b2bSJens Axboe 	 * there for a stacking driver higher up in the stack, processing it
385f9c78b2bSJens Axboe 	 * could require allocating bios from this bio_set, and doing that from
386f9c78b2bSJens Axboe 	 * our own rescuer would be bad.
387f9c78b2bSJens Axboe 	 *
388f9c78b2bSJens Axboe 	 * Since bio lists are singly linked, pop them all instead of trying to
389f9c78b2bSJens Axboe 	 * remove from the middle of the list:
390f9c78b2bSJens Axboe 	 */
391f9c78b2bSJens Axboe 
392f9c78b2bSJens Axboe 	bio_list_init(&punt);
393f9c78b2bSJens Axboe 	bio_list_init(&nopunt);
394f9c78b2bSJens Axboe 
395f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[0])))
396f9c78b2bSJens Axboe 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
397f5fe1b51SNeilBrown 	current->bio_list[0] = nopunt;
398f9c78b2bSJens Axboe 
399f5fe1b51SNeilBrown 	bio_list_init(&nopunt);
400f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[1])))
401f5fe1b51SNeilBrown 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
402f5fe1b51SNeilBrown 	current->bio_list[1] = nopunt;
403f9c78b2bSJens Axboe 
404f9c78b2bSJens Axboe 	spin_lock(&bs->rescue_lock);
405f9c78b2bSJens Axboe 	bio_list_merge(&bs->rescue_list, &punt);
406f9c78b2bSJens Axboe 	spin_unlock(&bs->rescue_lock);
407f9c78b2bSJens Axboe 
408f9c78b2bSJens Axboe 	queue_work(bs->rescue_workqueue, &bs->rescue_work);
409f9c78b2bSJens Axboe }
410f9c78b2bSJens Axboe 
4110df71650SMike Snitzer static struct bio *bio_alloc_percpu_cache(struct block_device *bdev,
41216458cf3SBart Van Assche 		unsigned short nr_vecs, blk_opf_t opf, gfp_t gfp,
4130df71650SMike Snitzer 		struct bio_set *bs)
4140df71650SMike Snitzer {
4150df71650SMike Snitzer 	struct bio_alloc_cache *cache;
4160df71650SMike Snitzer 	struct bio *bio;
4170df71650SMike Snitzer 
4180df71650SMike Snitzer 	cache = per_cpu_ptr(bs->cache, get_cpu());
4190df71650SMike Snitzer 	if (!cache->free_list) {
4200df71650SMike Snitzer 		put_cpu();
4210df71650SMike Snitzer 		return NULL;
4220df71650SMike Snitzer 	}
4230df71650SMike Snitzer 	bio = cache->free_list;
4240df71650SMike Snitzer 	cache->free_list = bio->bi_next;
4250df71650SMike Snitzer 	cache->nr--;
4260df71650SMike Snitzer 	put_cpu();
4270df71650SMike Snitzer 
4280df71650SMike Snitzer 	bio_init(bio, bdev, nr_vecs ? bio->bi_inline_vecs : NULL, nr_vecs, opf);
4290df71650SMike Snitzer 	bio->bi_pool = bs;
4300df71650SMike Snitzer 	return bio;
4310df71650SMike Snitzer }
4320df71650SMike Snitzer 
433f9c78b2bSJens Axboe /**
434f9c78b2bSJens Axboe  * bio_alloc_bioset - allocate a bio for I/O
435609be106SChristoph Hellwig  * @bdev:	block device to allocate the bio for (can be %NULL)
436609be106SChristoph Hellwig  * @nr_vecs:	number of bvecs to pre-allocate
437609be106SChristoph Hellwig  * @opf:	operation and flags for bio
438519c8e9fSRandy Dunlap  * @gfp_mask:   the GFP_* mask given to the slab allocator
439f9c78b2bSJens Axboe  * @bs:		the bio_set to allocate from.
440f9c78b2bSJens Axboe  *
4413175199aSChristoph Hellwig  * Allocate a bio from the mempools in @bs.
442f9c78b2bSJens Axboe  *
4433175199aSChristoph Hellwig  * If %__GFP_DIRECT_RECLAIM is set then bio_alloc will always be able to
4443175199aSChristoph Hellwig  * allocate a bio.  This is due to the mempool guarantees.  To make this work,
4453175199aSChristoph Hellwig  * callers must never allocate more than 1 bio at a time from the general pool.
4463175199aSChristoph Hellwig  * Callers that need to allocate more than 1 bio must always submit the
4473175199aSChristoph Hellwig  * previously allocated bio for IO before attempting to allocate a new one.
4483175199aSChristoph Hellwig  * Failure to do so can cause deadlocks under memory pressure.
449f9c78b2bSJens Axboe  *
4503175199aSChristoph Hellwig  * Note that when running under submit_bio_noacct() (i.e. any block driver),
4513175199aSChristoph Hellwig  * bios are not submitted until after you return - see the code in
452ed00aabdSChristoph Hellwig  * submit_bio_noacct() that converts recursion into iteration, to prevent
453f9c78b2bSJens Axboe  * stack overflows.
454f9c78b2bSJens Axboe  *
4553175199aSChristoph Hellwig  * This would normally mean allocating multiple bios under submit_bio_noacct()
4563175199aSChristoph Hellwig  * would be susceptible to deadlocks, but we have
457f9c78b2bSJens Axboe  * deadlock avoidance code that resubmits any blocked bios from a rescuer
458f9c78b2bSJens Axboe  * thread.
459f9c78b2bSJens Axboe  *
460f9c78b2bSJens Axboe  * However, we do not guarantee forward progress for allocations from other
461f9c78b2bSJens Axboe  * mempools. Doing multiple allocations from the same mempool under
462ed00aabdSChristoph Hellwig  * submit_bio_noacct() should be avoided - instead, use bio_set's front_pad
463f9c78b2bSJens Axboe  * for per bio allocations.
464f9c78b2bSJens Axboe  *
4650df71650SMike Snitzer  * If REQ_ALLOC_CACHE is set, the final put of the bio MUST be done from process
4660df71650SMike Snitzer  * context, not hard/soft IRQ.
4670df71650SMike Snitzer  *
4683175199aSChristoph Hellwig  * Returns: Pointer to new bio on success, NULL on failure.
469f9c78b2bSJens Axboe  */
470609be106SChristoph Hellwig struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
47116458cf3SBart Van Assche 			     blk_opf_t opf, gfp_t gfp_mask,
4727a88fa19SDan Carpenter 			     struct bio_set *bs)
473f9c78b2bSJens Axboe {
474f9c78b2bSJens Axboe 	gfp_t saved_gfp = gfp_mask;
475f9c78b2bSJens Axboe 	struct bio *bio;
476f9c78b2bSJens Axboe 	void *p;
477f9c78b2bSJens Axboe 
478609be106SChristoph Hellwig 	/* should not use nobvec bioset for nr_vecs > 0 */
479609be106SChristoph Hellwig 	if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && nr_vecs > 0))
480f9c78b2bSJens Axboe 		return NULL;
481f9c78b2bSJens Axboe 
4820df71650SMike Snitzer 	if (opf & REQ_ALLOC_CACHE) {
4830df71650SMike Snitzer 		if (bs->cache && nr_vecs <= BIO_INLINE_VECS) {
4840df71650SMike Snitzer 			bio = bio_alloc_percpu_cache(bdev, nr_vecs, opf,
4850df71650SMike Snitzer 						     gfp_mask, bs);
4860df71650SMike Snitzer 			if (bio)
4870df71650SMike Snitzer 				return bio;
4880df71650SMike Snitzer 			/*
4890df71650SMike Snitzer 			 * No cached bio available, bio returned below marked with
4900df71650SMike Snitzer 			 * REQ_ALLOC_CACHE to particpate in per-cpu alloc cache.
4910df71650SMike Snitzer 			 */
4920df71650SMike Snitzer 		} else {
4930df71650SMike Snitzer 			opf &= ~REQ_ALLOC_CACHE;
4940df71650SMike Snitzer 		}
4950df71650SMike Snitzer 	}
4960df71650SMike Snitzer 
497f9c78b2bSJens Axboe 	/*
4983175199aSChristoph Hellwig 	 * submit_bio_noacct() converts recursion to iteration; this means if
4993175199aSChristoph Hellwig 	 * we're running beneath it, any bios we allocate and submit will not be
5003175199aSChristoph Hellwig 	 * submitted (and thus freed) until after we return.
501f9c78b2bSJens Axboe 	 *
5023175199aSChristoph Hellwig 	 * This exposes us to a potential deadlock if we allocate multiple bios
5033175199aSChristoph Hellwig 	 * from the same bio_set() while running underneath submit_bio_noacct().
5043175199aSChristoph Hellwig 	 * If we were to allocate multiple bios (say a stacking block driver
5053175199aSChristoph Hellwig 	 * that was splitting bios), we would deadlock if we exhausted the
5063175199aSChristoph Hellwig 	 * mempool's reserve.
507f9c78b2bSJens Axboe 	 *
508f9c78b2bSJens Axboe 	 * We solve this, and guarantee forward progress, with a rescuer
5093175199aSChristoph Hellwig 	 * workqueue per bio_set. If we go to allocate and there are bios on
5103175199aSChristoph Hellwig 	 * current->bio_list, we first try the allocation without
5113175199aSChristoph Hellwig 	 * __GFP_DIRECT_RECLAIM; if that fails, we punt those bios we would be
5123175199aSChristoph Hellwig 	 * blocking to the rescuer workqueue before we retry with the original
5133175199aSChristoph Hellwig 	 * gfp_flags.
514f9c78b2bSJens Axboe 	 */
515f5fe1b51SNeilBrown 	if (current->bio_list &&
516f5fe1b51SNeilBrown 	    (!bio_list_empty(&current->bio_list[0]) ||
51747e0fb46SNeilBrown 	     !bio_list_empty(&current->bio_list[1])) &&
51847e0fb46SNeilBrown 	    bs->rescue_workqueue)
519d0164adcSMel Gorman 		gfp_mask &= ~__GFP_DIRECT_RECLAIM;
520f9c78b2bSJens Axboe 
5218aa6ba2fSKent Overstreet 	p = mempool_alloc(&bs->bio_pool, gfp_mask);
522f9c78b2bSJens Axboe 	if (!p && gfp_mask != saved_gfp) {
523f9c78b2bSJens Axboe 		punt_bios_to_rescuer(bs);
524f9c78b2bSJens Axboe 		gfp_mask = saved_gfp;
5258aa6ba2fSKent Overstreet 		p = mempool_alloc(&bs->bio_pool, gfp_mask);
526f9c78b2bSJens Axboe 	}
527f9c78b2bSJens Axboe 	if (unlikely(!p))
528f9c78b2bSJens Axboe 		return NULL;
529759aa12fSPavel Begunkov 	if (!mempool_is_saturated(&bs->bio_pool))
530759aa12fSPavel Begunkov 		opf &= ~REQ_ALLOC_CACHE;
531f9c78b2bSJens Axboe 
5323175199aSChristoph Hellwig 	bio = p + bs->front_pad;
533609be106SChristoph Hellwig 	if (nr_vecs > BIO_INLINE_VECS) {
5343175199aSChristoph Hellwig 		struct bio_vec *bvl = NULL;
535f9c78b2bSJens Axboe 
536609be106SChristoph Hellwig 		bvl = bvec_alloc(&bs->bvec_pool, &nr_vecs, gfp_mask);
537f9c78b2bSJens Axboe 		if (!bvl && gfp_mask != saved_gfp) {
538f9c78b2bSJens Axboe 			punt_bios_to_rescuer(bs);
539f9c78b2bSJens Axboe 			gfp_mask = saved_gfp;
540609be106SChristoph Hellwig 			bvl = bvec_alloc(&bs->bvec_pool, &nr_vecs, gfp_mask);
541f9c78b2bSJens Axboe 		}
542f9c78b2bSJens Axboe 		if (unlikely(!bvl))
543f9c78b2bSJens Axboe 			goto err_free;
544f9c78b2bSJens Axboe 
54549add496SChristoph Hellwig 		bio_init(bio, bdev, bvl, nr_vecs, opf);
546609be106SChristoph Hellwig 	} else if (nr_vecs) {
54749add496SChristoph Hellwig 		bio_init(bio, bdev, bio->bi_inline_vecs, BIO_INLINE_VECS, opf);
5483175199aSChristoph Hellwig 	} else {
54949add496SChristoph Hellwig 		bio_init(bio, bdev, NULL, 0, opf);
550f9c78b2bSJens Axboe 	}
551f9c78b2bSJens Axboe 
552f9c78b2bSJens Axboe 	bio->bi_pool = bs;
553f9c78b2bSJens Axboe 	return bio;
554f9c78b2bSJens Axboe 
555f9c78b2bSJens Axboe err_free:
5568aa6ba2fSKent Overstreet 	mempool_free(p, &bs->bio_pool);
557f9c78b2bSJens Axboe 	return NULL;
558f9c78b2bSJens Axboe }
559f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_bioset);
560f9c78b2bSJens Axboe 
5613175199aSChristoph Hellwig /**
562066ff571SChristoph Hellwig  * bio_kmalloc - kmalloc a bio
563066ff571SChristoph Hellwig  * @nr_vecs:	number of bio_vecs to allocate
5643175199aSChristoph Hellwig  * @gfp_mask:   the GFP_* mask given to the slab allocator
5653175199aSChristoph Hellwig  *
566066ff571SChristoph Hellwig  * Use kmalloc to allocate a bio (including bvecs).  The bio must be initialized
567066ff571SChristoph Hellwig  * using bio_init() before use.  To free a bio returned from this function use
568066ff571SChristoph Hellwig  * kfree() after calling bio_uninit().  A bio returned from this function can
569066ff571SChristoph Hellwig  * be reused by calling bio_uninit() before calling bio_init() again.
570066ff571SChristoph Hellwig  *
571066ff571SChristoph Hellwig  * Note that unlike bio_alloc() or bio_alloc_bioset() allocations from this
572340e1347SDeming Wang  * function are not backed by a mempool can fail.  Do not use this function
573066ff571SChristoph Hellwig  * for allocations in the file system I/O path.
5743175199aSChristoph Hellwig  *
5753175199aSChristoph Hellwig  * Returns: Pointer to new bio on success, NULL on failure.
5763175199aSChristoph Hellwig  */
577066ff571SChristoph Hellwig struct bio *bio_kmalloc(unsigned short nr_vecs, gfp_t gfp_mask)
5783175199aSChristoph Hellwig {
5793175199aSChristoph Hellwig 	struct bio *bio;
5803175199aSChristoph Hellwig 
581066ff571SChristoph Hellwig 	if (nr_vecs > UIO_MAXIOV)
5823175199aSChristoph Hellwig 		return NULL;
583066ff571SChristoph Hellwig 	return kmalloc(struct_size(bio, bi_inline_vecs, nr_vecs), gfp_mask);
5843175199aSChristoph Hellwig }
5853175199aSChristoph Hellwig EXPORT_SYMBOL(bio_kmalloc);
5863175199aSChristoph Hellwig 
5876f822e1bSChristoph Hellwig void zero_fill_bio(struct bio *bio)
588f9c78b2bSJens Axboe {
589f9c78b2bSJens Axboe 	struct bio_vec bv;
590f9c78b2bSJens Axboe 	struct bvec_iter iter;
591f9c78b2bSJens Axboe 
592ab6c340eSChristoph Hellwig 	bio_for_each_segment(bv, bio, iter)
593ab6c340eSChristoph Hellwig 		memzero_bvec(&bv);
594f9c78b2bSJens Axboe }
5956f822e1bSChristoph Hellwig EXPORT_SYMBOL(zero_fill_bio);
596f9c78b2bSJens Axboe 
59783c9c547SMing Lei /**
59883c9c547SMing Lei  * bio_truncate - truncate the bio to small size of @new_size
59983c9c547SMing Lei  * @bio:	the bio to be truncated
60083c9c547SMing Lei  * @new_size:	new size for truncating the bio
60183c9c547SMing Lei  *
60283c9c547SMing Lei  * Description:
60383c9c547SMing Lei  *   Truncate the bio to new size of @new_size. If bio_op(bio) is
60483c9c547SMing Lei  *   REQ_OP_READ, zero the truncated part. This function should only
60583c9c547SMing Lei  *   be used for handling corner cases, such as bio eod.
60683c9c547SMing Lei  */
6074f7ab09aSChristoph Hellwig static void bio_truncate(struct bio *bio, unsigned new_size)
60885a8ce62SMing Lei {
60985a8ce62SMing Lei 	struct bio_vec bv;
61085a8ce62SMing Lei 	struct bvec_iter iter;
61185a8ce62SMing Lei 	unsigned int done = 0;
61285a8ce62SMing Lei 	bool truncated = false;
61385a8ce62SMing Lei 
61485a8ce62SMing Lei 	if (new_size >= bio->bi_iter.bi_size)
61585a8ce62SMing Lei 		return;
61685a8ce62SMing Lei 
61783c9c547SMing Lei 	if (bio_op(bio) != REQ_OP_READ)
61885a8ce62SMing Lei 		goto exit;
61985a8ce62SMing Lei 
62085a8ce62SMing Lei 	bio_for_each_segment(bv, bio, iter) {
62185a8ce62SMing Lei 		if (done + bv.bv_len > new_size) {
62285a8ce62SMing Lei 			unsigned offset;
62385a8ce62SMing Lei 
62485a8ce62SMing Lei 			if (!truncated)
62585a8ce62SMing Lei 				offset = new_size - done;
62685a8ce62SMing Lei 			else
62785a8ce62SMing Lei 				offset = 0;
6283ee859e3SOGAWA Hirofumi 			zero_user(bv.bv_page, bv.bv_offset + offset,
6293ee859e3SOGAWA Hirofumi 				  bv.bv_len - offset);
63085a8ce62SMing Lei 			truncated = true;
63185a8ce62SMing Lei 		}
63285a8ce62SMing Lei 		done += bv.bv_len;
63385a8ce62SMing Lei 	}
63485a8ce62SMing Lei 
63585a8ce62SMing Lei  exit:
63685a8ce62SMing Lei 	/*
63785a8ce62SMing Lei 	 * Don't touch bvec table here and make it really immutable, since
63885a8ce62SMing Lei 	 * fs bio user has to retrieve all pages via bio_for_each_segment_all
63985a8ce62SMing Lei 	 * in its .end_bio() callback.
64085a8ce62SMing Lei 	 *
64185a8ce62SMing Lei 	 * It is enough to truncate bio by updating .bi_size since we can make
64285a8ce62SMing Lei 	 * correct bvec with the updated .bi_size for drivers.
64385a8ce62SMing Lei 	 */
64485a8ce62SMing Lei 	bio->bi_iter.bi_size = new_size;
64585a8ce62SMing Lei }
64685a8ce62SMing Lei 
647f9c78b2bSJens Axboe /**
64829125ed6SChristoph Hellwig  * guard_bio_eod - truncate a BIO to fit the block device
64929125ed6SChristoph Hellwig  * @bio:	bio to truncate
65029125ed6SChristoph Hellwig  *
65129125ed6SChristoph Hellwig  * This allows us to do IO even on the odd last sectors of a device, even if the
65229125ed6SChristoph Hellwig  * block size is some multiple of the physical sector size.
65329125ed6SChristoph Hellwig  *
65429125ed6SChristoph Hellwig  * We'll just truncate the bio to the size of the device, and clear the end of
65529125ed6SChristoph Hellwig  * the buffer head manually.  Truly out-of-range accesses will turn into actual
65629125ed6SChristoph Hellwig  * I/O errors, this only handles the "we need to be able to do I/O at the final
65729125ed6SChristoph Hellwig  * sector" case.
65829125ed6SChristoph Hellwig  */
65929125ed6SChristoph Hellwig void guard_bio_eod(struct bio *bio)
66029125ed6SChristoph Hellwig {
661309dca30SChristoph Hellwig 	sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
66229125ed6SChristoph Hellwig 
66329125ed6SChristoph Hellwig 	if (!maxsector)
66429125ed6SChristoph Hellwig 		return;
66529125ed6SChristoph Hellwig 
66629125ed6SChristoph Hellwig 	/*
66729125ed6SChristoph Hellwig 	 * If the *whole* IO is past the end of the device,
66829125ed6SChristoph Hellwig 	 * let it through, and the IO layer will turn it into
66929125ed6SChristoph Hellwig 	 * an EIO.
67029125ed6SChristoph Hellwig 	 */
67129125ed6SChristoph Hellwig 	if (unlikely(bio->bi_iter.bi_sector >= maxsector))
67229125ed6SChristoph Hellwig 		return;
67329125ed6SChristoph Hellwig 
67429125ed6SChristoph Hellwig 	maxsector -= bio->bi_iter.bi_sector;
67529125ed6SChristoph Hellwig 	if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
67629125ed6SChristoph Hellwig 		return;
67729125ed6SChristoph Hellwig 
67829125ed6SChristoph Hellwig 	bio_truncate(bio, maxsector << 9);
67929125ed6SChristoph Hellwig }
68029125ed6SChristoph Hellwig 
681be4d234dSJens Axboe #define ALLOC_CACHE_MAX		512
682be4d234dSJens Axboe #define ALLOC_CACHE_SLACK	 64
683be4d234dSJens Axboe 
684be4d234dSJens Axboe static void bio_alloc_cache_prune(struct bio_alloc_cache *cache,
685be4d234dSJens Axboe 				  unsigned int nr)
686be4d234dSJens Axboe {
687be4d234dSJens Axboe 	unsigned int i = 0;
688be4d234dSJens Axboe 	struct bio *bio;
689be4d234dSJens Axboe 
690fcade2ceSJens Axboe 	while ((bio = cache->free_list) != NULL) {
691fcade2ceSJens Axboe 		cache->free_list = bio->bi_next;
692be4d234dSJens Axboe 		cache->nr--;
693be4d234dSJens Axboe 		bio_free(bio);
694be4d234dSJens Axboe 		if (++i == nr)
695be4d234dSJens Axboe 			break;
696be4d234dSJens Axboe 	}
697be4d234dSJens Axboe }
698be4d234dSJens Axboe 
699be4d234dSJens Axboe static int bio_cpu_dead(unsigned int cpu, struct hlist_node *node)
700be4d234dSJens Axboe {
701be4d234dSJens Axboe 	struct bio_set *bs;
702be4d234dSJens Axboe 
703be4d234dSJens Axboe 	bs = hlist_entry_safe(node, struct bio_set, cpuhp_dead);
704be4d234dSJens Axboe 	if (bs->cache) {
705be4d234dSJens Axboe 		struct bio_alloc_cache *cache = per_cpu_ptr(bs->cache, cpu);
706be4d234dSJens Axboe 
707be4d234dSJens Axboe 		bio_alloc_cache_prune(cache, -1U);
708be4d234dSJens Axboe 	}
709be4d234dSJens Axboe 	return 0;
710be4d234dSJens Axboe }
711be4d234dSJens Axboe 
712be4d234dSJens Axboe static void bio_alloc_cache_destroy(struct bio_set *bs)
713be4d234dSJens Axboe {
714be4d234dSJens Axboe 	int cpu;
715be4d234dSJens Axboe 
716be4d234dSJens Axboe 	if (!bs->cache)
717be4d234dSJens Axboe 		return;
718be4d234dSJens Axboe 
719be4d234dSJens Axboe 	cpuhp_state_remove_instance_nocalls(CPUHP_BIO_DEAD, &bs->cpuhp_dead);
720be4d234dSJens Axboe 	for_each_possible_cpu(cpu) {
721be4d234dSJens Axboe 		struct bio_alloc_cache *cache;
722be4d234dSJens Axboe 
723be4d234dSJens Axboe 		cache = per_cpu_ptr(bs->cache, cpu);
724be4d234dSJens Axboe 		bio_alloc_cache_prune(cache, -1U);
725be4d234dSJens Axboe 	}
726be4d234dSJens Axboe 	free_percpu(bs->cache);
727605f7415SJens Axboe 	bs->cache = NULL;
728be4d234dSJens Axboe }
729be4d234dSJens Axboe 
730*f25cf75aSPavel Begunkov static inline void bio_put_percpu_cache(struct bio *bio)
731*f25cf75aSPavel Begunkov {
732*f25cf75aSPavel Begunkov 	struct bio_alloc_cache *cache;
733*f25cf75aSPavel Begunkov 
734*f25cf75aSPavel Begunkov 	cache = per_cpu_ptr(bio->bi_pool->cache, get_cpu());
735*f25cf75aSPavel Begunkov 	bio_uninit(bio);
736*f25cf75aSPavel Begunkov 
737*f25cf75aSPavel Begunkov 	if ((bio->bi_opf & REQ_POLLED) && !WARN_ON_ONCE(in_interrupt())) {
738*f25cf75aSPavel Begunkov 		bio->bi_next = cache->free_list;
739*f25cf75aSPavel Begunkov 		cache->free_list = bio;
740*f25cf75aSPavel Begunkov 		cache->nr++;
741*f25cf75aSPavel Begunkov 	} else {
742*f25cf75aSPavel Begunkov 		put_cpu();
743*f25cf75aSPavel Begunkov 		bio_free(bio);
744*f25cf75aSPavel Begunkov 		return;
745*f25cf75aSPavel Begunkov 	}
746*f25cf75aSPavel Begunkov 
747*f25cf75aSPavel Begunkov 	if (cache->nr > ALLOC_CACHE_MAX + ALLOC_CACHE_SLACK)
748*f25cf75aSPavel Begunkov 		bio_alloc_cache_prune(cache, ALLOC_CACHE_SLACK);
749*f25cf75aSPavel Begunkov 	put_cpu();
750*f25cf75aSPavel Begunkov }
751*f25cf75aSPavel Begunkov 
75229125ed6SChristoph Hellwig /**
753f9c78b2bSJens Axboe  * bio_put - release a reference to a bio
754f9c78b2bSJens Axboe  * @bio:   bio to release reference to
755f9c78b2bSJens Axboe  *
756f9c78b2bSJens Axboe  * Description:
757f9c78b2bSJens Axboe  *   Put a reference to a &struct bio, either one you have gotten with
7589b10f6a9SNeilBrown  *   bio_alloc, bio_get or bio_clone_*. The last put of a bio will free it.
759f9c78b2bSJens Axboe  **/
760f9c78b2bSJens Axboe void bio_put(struct bio *bio)
761f9c78b2bSJens Axboe {
762be4d234dSJens Axboe 	if (unlikely(bio_flagged(bio, BIO_REFFED))) {
7639e8c0d0dSChristoph Hellwig 		BUG_ON(!atomic_read(&bio->__bi_cnt));
764be4d234dSJens Axboe 		if (!atomic_dec_and_test(&bio->__bi_cnt))
765be4d234dSJens Axboe 			return;
766be4d234dSJens Axboe 	}
767*f25cf75aSPavel Begunkov 	if (bio->bi_opf & REQ_ALLOC_CACHE)
768*f25cf75aSPavel Begunkov 		bio_put_percpu_cache(bio);
769*f25cf75aSPavel Begunkov 	else
770f9c78b2bSJens Axboe 		bio_free(bio);
771f9c78b2bSJens Axboe }
772f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_put);
773f9c78b2bSJens Axboe 
774a0e8de79SChristoph Hellwig static int __bio_clone(struct bio *bio, struct bio *bio_src, gfp_t gfp)
775f9c78b2bSJens Axboe {
776b7c44ed9SJens Axboe 	bio_set_flag(bio, BIO_CLONED);
777ca474b73SHannes Reinecke 	bio->bi_ioprio = bio_src->bi_ioprio;
778f9c78b2bSJens Axboe 	bio->bi_iter = bio_src->bi_iter;
77920bd723eSPaolo Valente 
7807ecc56c6SChristoph Hellwig 	if (bio->bi_bdev) {
7817ecc56c6SChristoph Hellwig 		if (bio->bi_bdev == bio_src->bi_bdev &&
7827ecc56c6SChristoph Hellwig 		    bio_flagged(bio_src, BIO_REMAPPED))
7837ecc56c6SChristoph Hellwig 			bio_set_flag(bio, BIO_REMAPPED);
784db6638d7SDennis Zhou 		bio_clone_blkg_association(bio, bio_src);
7857ecc56c6SChristoph Hellwig 	}
78656b4b5abSChristoph Hellwig 
78756b4b5abSChristoph Hellwig 	if (bio_crypt_clone(bio, bio_src, gfp) < 0)
78856b4b5abSChristoph Hellwig 		return -ENOMEM;
78956b4b5abSChristoph Hellwig 	if (bio_integrity(bio_src) &&
79056b4b5abSChristoph Hellwig 	    bio_integrity_clone(bio, bio_src, gfp) < 0)
79156b4b5abSChristoph Hellwig 		return -ENOMEM;
79256b4b5abSChristoph Hellwig 	return 0;
793f9c78b2bSJens Axboe }
794f9c78b2bSJens Axboe 
795f9c78b2bSJens Axboe /**
796abfc426dSChristoph Hellwig  * bio_alloc_clone - clone a bio that shares the original bio's biovec
797abfc426dSChristoph Hellwig  * @bdev: block_device to clone onto
798a0e8de79SChristoph Hellwig  * @bio_src: bio to clone from
799a0e8de79SChristoph Hellwig  * @gfp: allocation priority
800f9c78b2bSJens Axboe  * @bs: bio_set to allocate from
801f9c78b2bSJens Axboe  *
802a0e8de79SChristoph Hellwig  * Allocate a new bio that is a clone of @bio_src. The caller owns the returned
803a0e8de79SChristoph Hellwig  * bio, but not the actual data it points to.
804a0e8de79SChristoph Hellwig  *
805a0e8de79SChristoph Hellwig  * The caller must ensure that the return bio is not freed before @bio_src.
806f9c78b2bSJens Axboe  */
807abfc426dSChristoph Hellwig struct bio *bio_alloc_clone(struct block_device *bdev, struct bio *bio_src,
808abfc426dSChristoph Hellwig 		gfp_t gfp, struct bio_set *bs)
809f9c78b2bSJens Axboe {
810a0e8de79SChristoph Hellwig 	struct bio *bio;
811f9c78b2bSJens Axboe 
812abfc426dSChristoph Hellwig 	bio = bio_alloc_bioset(bdev, 0, bio_src->bi_opf, gfp, bs);
813a0e8de79SChristoph Hellwig 	if (!bio)
814f9c78b2bSJens Axboe 		return NULL;
815f9c78b2bSJens Axboe 
816a0e8de79SChristoph Hellwig 	if (__bio_clone(bio, bio_src, gfp) < 0) {
817a0e8de79SChristoph Hellwig 		bio_put(bio);
81807560151SEric Biggers 		return NULL;
819f9c78b2bSJens Axboe 	}
820a0e8de79SChristoph Hellwig 	bio->bi_io_vec = bio_src->bi_io_vec;
82156b4b5abSChristoph Hellwig 
822a0e8de79SChristoph Hellwig 	return bio;
82356b4b5abSChristoph Hellwig }
824abfc426dSChristoph Hellwig EXPORT_SYMBOL(bio_alloc_clone);
825f9c78b2bSJens Axboe 
826a0e8de79SChristoph Hellwig /**
827abfc426dSChristoph Hellwig  * bio_init_clone - clone a bio that shares the original bio's biovec
828abfc426dSChristoph Hellwig  * @bdev: block_device to clone onto
829a0e8de79SChristoph Hellwig  * @bio: bio to clone into
830a0e8de79SChristoph Hellwig  * @bio_src: bio to clone from
831a0e8de79SChristoph Hellwig  * @gfp: allocation priority
832a0e8de79SChristoph Hellwig  *
833a0e8de79SChristoph Hellwig  * Initialize a new bio in caller provided memory that is a clone of @bio_src.
834a0e8de79SChristoph Hellwig  * The caller owns the returned bio, but not the actual data it points to.
835a0e8de79SChristoph Hellwig  *
836a0e8de79SChristoph Hellwig  * The caller must ensure that @bio_src is not freed before @bio.
837a0e8de79SChristoph Hellwig  */
838abfc426dSChristoph Hellwig int bio_init_clone(struct block_device *bdev, struct bio *bio,
839abfc426dSChristoph Hellwig 		struct bio *bio_src, gfp_t gfp)
840a0e8de79SChristoph Hellwig {
841a0e8de79SChristoph Hellwig 	int ret;
842a0e8de79SChristoph Hellwig 
843abfc426dSChristoph Hellwig 	bio_init(bio, bdev, bio_src->bi_io_vec, 0, bio_src->bi_opf);
844a0e8de79SChristoph Hellwig 	ret = __bio_clone(bio, bio_src, gfp);
845a0e8de79SChristoph Hellwig 	if (ret)
846a0e8de79SChristoph Hellwig 		bio_uninit(bio);
847a0e8de79SChristoph Hellwig 	return ret;
848a0e8de79SChristoph Hellwig }
849abfc426dSChristoph Hellwig EXPORT_SYMBOL(bio_init_clone);
850a0e8de79SChristoph Hellwig 
8519a6083beSChristoph Hellwig /**
8529a6083beSChristoph Hellwig  * bio_full - check if the bio is full
8539a6083beSChristoph Hellwig  * @bio:	bio to check
8549a6083beSChristoph Hellwig  * @len:	length of one segment to be added
8559a6083beSChristoph Hellwig  *
8569a6083beSChristoph Hellwig  * Return true if @bio is full and one segment with @len bytes can't be
8579a6083beSChristoph Hellwig  * added to the bio, otherwise return false
8589a6083beSChristoph Hellwig  */
8599a6083beSChristoph Hellwig static inline bool bio_full(struct bio *bio, unsigned len)
8609a6083beSChristoph Hellwig {
8619a6083beSChristoph Hellwig 	if (bio->bi_vcnt >= bio->bi_max_vecs)
8629a6083beSChristoph Hellwig 		return true;
8639a6083beSChristoph Hellwig 	if (bio->bi_iter.bi_size > UINT_MAX - len)
8649a6083beSChristoph Hellwig 		return true;
8659a6083beSChristoph Hellwig 	return false;
8669a6083beSChristoph Hellwig }
8679a6083beSChristoph Hellwig 
8685919482eSMing Lei static inline bool page_is_mergeable(const struct bio_vec *bv,
8695919482eSMing Lei 		struct page *page, unsigned int len, unsigned int off,
870ff896738SChristoph Hellwig 		bool *same_page)
8715919482eSMing Lei {
872d8166519SMatthew Wilcox (Oracle) 	size_t bv_end = bv->bv_offset + bv->bv_len;
873d8166519SMatthew Wilcox (Oracle) 	phys_addr_t vec_end_addr = page_to_phys(bv->bv_page) + bv_end - 1;
8745919482eSMing Lei 	phys_addr_t page_addr = page_to_phys(page);
8755919482eSMing Lei 
8765919482eSMing Lei 	if (vec_end_addr + 1 != page_addr + off)
8775919482eSMing Lei 		return false;
8785919482eSMing Lei 	if (xen_domain() && !xen_biovec_phys_mergeable(bv, page))
8795919482eSMing Lei 		return false;
88049580e69SLogan Gunthorpe 	if (!zone_device_pages_have_same_pgmap(bv->bv_page, page))
88149580e69SLogan Gunthorpe 		return false;
88252d52d1cSChristoph Hellwig 
883ff896738SChristoph Hellwig 	*same_page = ((vec_end_addr & PAGE_MASK) == page_addr);
884d8166519SMatthew Wilcox (Oracle) 	if (*same_page)
8855919482eSMing Lei 		return true;
88611b331f8SAlexander Potapenko 	else if (IS_ENABLED(CONFIG_KMSAN))
88711b331f8SAlexander Potapenko 		return false;
888d8166519SMatthew Wilcox (Oracle) 	return (bv->bv_page + bv_end / PAGE_SIZE) == (page + off / PAGE_SIZE);
8895919482eSMing Lei }
8905919482eSMing Lei 
8919774b391SChristoph Hellwig /**
8929774b391SChristoph Hellwig  * __bio_try_merge_page - try appending data to an existing bvec.
8939774b391SChristoph Hellwig  * @bio: destination bio
8949774b391SChristoph Hellwig  * @page: start page to add
8959774b391SChristoph Hellwig  * @len: length of the data to add
8969774b391SChristoph Hellwig  * @off: offset of the data relative to @page
8979774b391SChristoph Hellwig  * @same_page: return if the segment has been merged inside the same page
8989774b391SChristoph Hellwig  *
8999774b391SChristoph Hellwig  * Try to add the data at @page + @off to the last bvec of @bio.  This is a
9009774b391SChristoph Hellwig  * useful optimisation for file systems with a block size smaller than the
9019774b391SChristoph Hellwig  * page size.
9029774b391SChristoph Hellwig  *
9039774b391SChristoph Hellwig  * Warn if (@len, @off) crosses pages in case that @same_page is true.
9049774b391SChristoph Hellwig  *
9059774b391SChristoph Hellwig  * Return %true on success or %false on failure.
9069774b391SChristoph Hellwig  */
9079774b391SChristoph Hellwig static bool __bio_try_merge_page(struct bio *bio, struct page *page,
9089774b391SChristoph Hellwig 		unsigned int len, unsigned int off, bool *same_page)
9099774b391SChristoph Hellwig {
9109774b391SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
9119774b391SChristoph Hellwig 		return false;
9129774b391SChristoph Hellwig 
9139774b391SChristoph Hellwig 	if (bio->bi_vcnt > 0) {
9149774b391SChristoph Hellwig 		struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
9159774b391SChristoph Hellwig 
9169774b391SChristoph Hellwig 		if (page_is_mergeable(bv, page, len, off, same_page)) {
9179774b391SChristoph Hellwig 			if (bio->bi_iter.bi_size > UINT_MAX - len) {
9189774b391SChristoph Hellwig 				*same_page = false;
9199774b391SChristoph Hellwig 				return false;
9209774b391SChristoph Hellwig 			}
9219774b391SChristoph Hellwig 			bv->bv_len += len;
9229774b391SChristoph Hellwig 			bio->bi_iter.bi_size += len;
9239774b391SChristoph Hellwig 			return true;
9249774b391SChristoph Hellwig 		}
9259774b391SChristoph Hellwig 	}
9269774b391SChristoph Hellwig 	return false;
9279774b391SChristoph Hellwig }
9289774b391SChristoph Hellwig 
929e4581105SChristoph Hellwig /*
930e4581105SChristoph Hellwig  * Try to merge a page into a segment, while obeying the hardware segment
931e4581105SChristoph Hellwig  * size limit.  This is not for normal read/write bios, but for passthrough
932e4581105SChristoph Hellwig  * or Zone Append operations that we can't split.
933e4581105SChristoph Hellwig  */
934e4581105SChristoph Hellwig static bool bio_try_merge_hw_seg(struct request_queue *q, struct bio *bio,
935e4581105SChristoph Hellwig 				 struct page *page, unsigned len,
936e4581105SChristoph Hellwig 				 unsigned offset, bool *same_page)
937489fbbcbSMing Lei {
938384209cdSChristoph Hellwig 	struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
939489fbbcbSMing Lei 	unsigned long mask = queue_segment_boundary(q);
940489fbbcbSMing Lei 	phys_addr_t addr1 = page_to_phys(bv->bv_page) + bv->bv_offset;
941489fbbcbSMing Lei 	phys_addr_t addr2 = page_to_phys(page) + offset + len - 1;
942489fbbcbSMing Lei 
943489fbbcbSMing Lei 	if ((addr1 | mask) != (addr2 | mask))
944489fbbcbSMing Lei 		return false;
945489fbbcbSMing Lei 	if (bv->bv_len + len > queue_max_segment_size(q))
946489fbbcbSMing Lei 		return false;
947384209cdSChristoph Hellwig 	return __bio_try_merge_page(bio, page, len, offset, same_page);
948489fbbcbSMing Lei }
949489fbbcbSMing Lei 
950f4595875SShaohua Li /**
951e4581105SChristoph Hellwig  * bio_add_hw_page - attempt to add a page to a bio with hw constraints
952c66a14d0SKent Overstreet  * @q: the target queue
953c66a14d0SKent Overstreet  * @bio: destination bio
954c66a14d0SKent Overstreet  * @page: page to add
955c66a14d0SKent Overstreet  * @len: vec entry length
956c66a14d0SKent Overstreet  * @offset: vec entry offset
957e4581105SChristoph Hellwig  * @max_sectors: maximum number of sectors that can be added
958e4581105SChristoph Hellwig  * @same_page: return if the segment has been merged inside the same page
959f9c78b2bSJens Axboe  *
960e4581105SChristoph Hellwig  * Add a page to a bio while respecting the hardware max_sectors, max_segment
961e4581105SChristoph Hellwig  * and gap limitations.
962f9c78b2bSJens Axboe  */
963e4581105SChristoph Hellwig int bio_add_hw_page(struct request_queue *q, struct bio *bio,
96419047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset,
965e4581105SChristoph Hellwig 		unsigned int max_sectors, bool *same_page)
966f9c78b2bSJens Axboe {
967f9c78b2bSJens Axboe 	struct bio_vec *bvec;
968f9c78b2bSJens Axboe 
969e4581105SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
970f9c78b2bSJens Axboe 		return 0;
971f9c78b2bSJens Axboe 
972e4581105SChristoph Hellwig 	if (((bio->bi_iter.bi_size + len) >> 9) > max_sectors)
973f9c78b2bSJens Axboe 		return 0;
974f9c78b2bSJens Axboe 
975f9c78b2bSJens Axboe 	if (bio->bi_vcnt > 0) {
976e4581105SChristoph Hellwig 		if (bio_try_merge_hw_seg(q, bio, page, len, offset, same_page))
977384209cdSChristoph Hellwig 			return len;
978320ea869SChristoph Hellwig 
979320ea869SChristoph Hellwig 		/*
980320ea869SChristoph Hellwig 		 * If the queue doesn't support SG gaps and adding this segment
981320ea869SChristoph Hellwig 		 * would create a gap, disallow it.
982320ea869SChristoph Hellwig 		 */
983384209cdSChristoph Hellwig 		bvec = &bio->bi_io_vec[bio->bi_vcnt - 1];
984c55ddd90SChristoph Hellwig 		if (bvec_gap_to_prev(&q->limits, bvec, offset))
985320ea869SChristoph Hellwig 			return 0;
986f9c78b2bSJens Axboe 	}
987f9c78b2bSJens Axboe 
98879d08f89SMing Lei 	if (bio_full(bio, len))
989f9c78b2bSJens Axboe 		return 0;
990f9c78b2bSJens Axboe 
99114ccb66bSChristoph Hellwig 	if (bio->bi_vcnt >= queue_max_segments(q))
992489fbbcbSMing Lei 		return 0;
993489fbbcbSMing Lei 
994f9c78b2bSJens Axboe 	bvec = &bio->bi_io_vec[bio->bi_vcnt];
995f9c78b2bSJens Axboe 	bvec->bv_page = page;
996f9c78b2bSJens Axboe 	bvec->bv_len = len;
997f9c78b2bSJens Axboe 	bvec->bv_offset = offset;
998fcbf6a08SMaurizio Lombardi 	bio->bi_vcnt++;
999dcdca753SChristoph Hellwig 	bio->bi_iter.bi_size += len;
1000f9c78b2bSJens Axboe 	return len;
1001f9c78b2bSJens Axboe }
100219047087SMing Lei 
1003e4581105SChristoph Hellwig /**
1004e4581105SChristoph Hellwig  * bio_add_pc_page	- attempt to add page to passthrough bio
1005e4581105SChristoph Hellwig  * @q: the target queue
1006e4581105SChristoph Hellwig  * @bio: destination bio
1007e4581105SChristoph Hellwig  * @page: page to add
1008e4581105SChristoph Hellwig  * @len: vec entry length
1009e4581105SChristoph Hellwig  * @offset: vec entry offset
1010e4581105SChristoph Hellwig  *
1011e4581105SChristoph Hellwig  * Attempt to add a page to the bio_vec maplist. This can fail for a
1012e4581105SChristoph Hellwig  * number of reasons, such as the bio being full or target block device
1013e4581105SChristoph Hellwig  * limitations. The target block device must allow bio's up to PAGE_SIZE,
1014e4581105SChristoph Hellwig  * so it is always possible to add a single page to an empty bio.
1015e4581105SChristoph Hellwig  *
1016e4581105SChristoph Hellwig  * This should only be used by passthrough bios.
1017e4581105SChristoph Hellwig  */
101819047087SMing Lei int bio_add_pc_page(struct request_queue *q, struct bio *bio,
101919047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset)
102019047087SMing Lei {
1021d1916c86SChristoph Hellwig 	bool same_page = false;
1022e4581105SChristoph Hellwig 	return bio_add_hw_page(q, bio, page, len, offset,
1023e4581105SChristoph Hellwig 			queue_max_hw_sectors(q), &same_page);
102419047087SMing Lei }
1025f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_pc_page);
1026f9c78b2bSJens Axboe 
1027f9c78b2bSJens Axboe /**
1028ae29333fSJohannes Thumshirn  * bio_add_zone_append_page - attempt to add page to zone-append bio
1029ae29333fSJohannes Thumshirn  * @bio: destination bio
1030ae29333fSJohannes Thumshirn  * @page: page to add
1031ae29333fSJohannes Thumshirn  * @len: vec entry length
1032ae29333fSJohannes Thumshirn  * @offset: vec entry offset
1033ae29333fSJohannes Thumshirn  *
1034ae29333fSJohannes Thumshirn  * Attempt to add a page to the bio_vec maplist of a bio that will be submitted
1035ae29333fSJohannes Thumshirn  * for a zone-append request. This can fail for a number of reasons, such as the
1036ae29333fSJohannes Thumshirn  * bio being full or the target block device is not a zoned block device or
1037ae29333fSJohannes Thumshirn  * other limitations of the target block device. The target block device must
1038ae29333fSJohannes Thumshirn  * allow bio's up to PAGE_SIZE, so it is always possible to add a single page
1039ae29333fSJohannes Thumshirn  * to an empty bio.
1040ae29333fSJohannes Thumshirn  *
1041ae29333fSJohannes Thumshirn  * Returns: number of bytes added to the bio, or 0 in case of a failure.
1042ae29333fSJohannes Thumshirn  */
1043ae29333fSJohannes Thumshirn int bio_add_zone_append_page(struct bio *bio, struct page *page,
1044ae29333fSJohannes Thumshirn 			     unsigned int len, unsigned int offset)
1045ae29333fSJohannes Thumshirn {
10463caee463SPavel Begunkov 	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1047ae29333fSJohannes Thumshirn 	bool same_page = false;
1048ae29333fSJohannes Thumshirn 
1049ae29333fSJohannes Thumshirn 	if (WARN_ON_ONCE(bio_op(bio) != REQ_OP_ZONE_APPEND))
1050ae29333fSJohannes Thumshirn 		return 0;
1051ae29333fSJohannes Thumshirn 
1052edd1dbc8SChristoph Hellwig 	if (WARN_ON_ONCE(!bdev_is_zoned(bio->bi_bdev)))
1053ae29333fSJohannes Thumshirn 		return 0;
1054ae29333fSJohannes Thumshirn 
1055ae29333fSJohannes Thumshirn 	return bio_add_hw_page(q, bio, page, len, offset,
1056ae29333fSJohannes Thumshirn 			       queue_max_zone_append_sectors(q), &same_page);
1057ae29333fSJohannes Thumshirn }
1058ae29333fSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_add_zone_append_page);
1059ae29333fSJohannes Thumshirn 
1060ae29333fSJohannes Thumshirn /**
1061551879a4SMing Lei  * __bio_add_page - add page(s) to a bio in a new segment
10620aa69fd3SChristoph Hellwig  * @bio: destination bio
1063551879a4SMing Lei  * @page: start page to add
1064551879a4SMing Lei  * @len: length of the data to add, may cross pages
1065551879a4SMing Lei  * @off: offset of the data relative to @page, may cross pages
10660aa69fd3SChristoph Hellwig  *
10670aa69fd3SChristoph Hellwig  * Add the data at @page + @off to @bio as a new bvec.  The caller must ensure
10680aa69fd3SChristoph Hellwig  * that @bio has space for another bvec.
10690aa69fd3SChristoph Hellwig  */
10700aa69fd3SChristoph Hellwig void __bio_add_page(struct bio *bio, struct page *page,
10710aa69fd3SChristoph Hellwig 		unsigned int len, unsigned int off)
10720aa69fd3SChristoph Hellwig {
10730aa69fd3SChristoph Hellwig 	struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt];
10740aa69fd3SChristoph Hellwig 
10750aa69fd3SChristoph Hellwig 	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
107679d08f89SMing Lei 	WARN_ON_ONCE(bio_full(bio, len));
10770aa69fd3SChristoph Hellwig 
10780aa69fd3SChristoph Hellwig 	bv->bv_page = page;
10790aa69fd3SChristoph Hellwig 	bv->bv_offset = off;
10800aa69fd3SChristoph Hellwig 	bv->bv_len = len;
10810aa69fd3SChristoph Hellwig 
10820aa69fd3SChristoph Hellwig 	bio->bi_iter.bi_size += len;
10830aa69fd3SChristoph Hellwig 	bio->bi_vcnt++;
10840aa69fd3SChristoph Hellwig }
10850aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_add_page);
10860aa69fd3SChristoph Hellwig 
10870aa69fd3SChristoph Hellwig /**
1088551879a4SMing Lei  *	bio_add_page	-	attempt to add page(s) to bio
1089f9c78b2bSJens Axboe  *	@bio: destination bio
1090551879a4SMing Lei  *	@page: start page to add
1091551879a4SMing Lei  *	@len: vec entry length, may cross pages
1092551879a4SMing Lei  *	@offset: vec entry offset relative to @page, may cross pages
1093f9c78b2bSJens Axboe  *
1094551879a4SMing Lei  *	Attempt to add page(s) to the bio_vec maplist. This will only fail
1095c66a14d0SKent Overstreet  *	if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
1096f9c78b2bSJens Axboe  */
1097c66a14d0SKent Overstreet int bio_add_page(struct bio *bio, struct page *page,
1098c66a14d0SKent Overstreet 		 unsigned int len, unsigned int offset)
1099f9c78b2bSJens Axboe {
1100ff896738SChristoph Hellwig 	bool same_page = false;
1101ff896738SChristoph Hellwig 
1102ff896738SChristoph Hellwig 	if (!__bio_try_merge_page(bio, page, len, offset, &same_page)) {
110379d08f89SMing Lei 		if (bio_full(bio, len))
1104c66a14d0SKent Overstreet 			return 0;
11050aa69fd3SChristoph Hellwig 		__bio_add_page(bio, page, len, offset);
1106c66a14d0SKent Overstreet 	}
1107c66a14d0SKent Overstreet 	return len;
1108f9c78b2bSJens Axboe }
1109f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_page);
1110f9c78b2bSJens Axboe 
111185f5a74cSMatthew Wilcox (Oracle) /**
111285f5a74cSMatthew Wilcox (Oracle)  * bio_add_folio - Attempt to add part of a folio to a bio.
111385f5a74cSMatthew Wilcox (Oracle)  * @bio: BIO to add to.
111485f5a74cSMatthew Wilcox (Oracle)  * @folio: Folio to add.
111585f5a74cSMatthew Wilcox (Oracle)  * @len: How many bytes from the folio to add.
111685f5a74cSMatthew Wilcox (Oracle)  * @off: First byte in this folio to add.
111785f5a74cSMatthew Wilcox (Oracle)  *
111885f5a74cSMatthew Wilcox (Oracle)  * Filesystems that use folios can call this function instead of calling
111985f5a74cSMatthew Wilcox (Oracle)  * bio_add_page() for each page in the folio.  If @off is bigger than
112085f5a74cSMatthew Wilcox (Oracle)  * PAGE_SIZE, this function can create a bio_vec that starts in a page
112185f5a74cSMatthew Wilcox (Oracle)  * after the bv_page.  BIOs do not support folios that are 4GiB or larger.
112285f5a74cSMatthew Wilcox (Oracle)  *
112385f5a74cSMatthew Wilcox (Oracle)  * Return: Whether the addition was successful.
112485f5a74cSMatthew Wilcox (Oracle)  */
112585f5a74cSMatthew Wilcox (Oracle) bool bio_add_folio(struct bio *bio, struct folio *folio, size_t len,
112685f5a74cSMatthew Wilcox (Oracle) 		   size_t off)
112785f5a74cSMatthew Wilcox (Oracle) {
112885f5a74cSMatthew Wilcox (Oracle) 	if (len > UINT_MAX || off > UINT_MAX)
1129455a844dSJiapeng Chong 		return false;
113085f5a74cSMatthew Wilcox (Oracle) 	return bio_add_page(bio, &folio->page, len, off) > 0;
113185f5a74cSMatthew Wilcox (Oracle) }
113285f5a74cSMatthew Wilcox (Oracle) 
1133c809084aSPavel Begunkov void __bio_release_pages(struct bio *bio, bool mark_dirty)
11347321ecbfSChristoph Hellwig {
11357321ecbfSChristoph Hellwig 	struct bvec_iter_all iter_all;
11367321ecbfSChristoph Hellwig 	struct bio_vec *bvec;
11377321ecbfSChristoph Hellwig 
1138d241a95fSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
1139d241a95fSChristoph Hellwig 		if (mark_dirty && !PageCompound(bvec->bv_page))
1140d241a95fSChristoph Hellwig 			set_page_dirty_lock(bvec->bv_page);
11417321ecbfSChristoph Hellwig 		put_page(bvec->bv_page);
11427321ecbfSChristoph Hellwig 	}
1143d241a95fSChristoph Hellwig }
1144c809084aSPavel Begunkov EXPORT_SYMBOL_GPL(__bio_release_pages);
11457321ecbfSChristoph Hellwig 
11461bb6b810SPavel Begunkov void bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
11476d0c48aeSJens Axboe {
1148fa5fa8ecSPavel Begunkov 	size_t size = iov_iter_count(iter);
1149fa5fa8ecSPavel Begunkov 
11507a800a20SChristoph Hellwig 	WARN_ON_ONCE(bio->bi_max_vecs);
11516d0c48aeSJens Axboe 
1152fa5fa8ecSPavel Begunkov 	if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
1153fa5fa8ecSPavel Begunkov 		struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1154fa5fa8ecSPavel Begunkov 		size_t max_sectors = queue_max_zone_append_sectors(q);
1155fa5fa8ecSPavel Begunkov 
1156fa5fa8ecSPavel Begunkov 		size = min(size, max_sectors << SECTOR_SHIFT);
1157fa5fa8ecSPavel Begunkov 	}
1158fa5fa8ecSPavel Begunkov 
1159c42bca92SPavel Begunkov 	bio->bi_vcnt = iter->nr_segs;
1160c42bca92SPavel Begunkov 	bio->bi_io_vec = (struct bio_vec *)iter->bvec;
1161c42bca92SPavel Begunkov 	bio->bi_iter.bi_bvec_done = iter->iov_offset;
1162fa5fa8ecSPavel Begunkov 	bio->bi_iter.bi_size = size;
1163ed97ce5eSChristoph Hellwig 	bio_set_flag(bio, BIO_NO_PAGE_REF);
1164977be012SChristoph Hellwig 	bio_set_flag(bio, BIO_CLONED);
11657de55b7dSJohannes Thumshirn }
11666d0c48aeSJens Axboe 
1167c58c0074SKeith Busch static int bio_iov_add_page(struct bio *bio, struct page *page,
1168c58c0074SKeith Busch 		unsigned int len, unsigned int offset)
1169c58c0074SKeith Busch {
1170c58c0074SKeith Busch 	bool same_page = false;
1171c58c0074SKeith Busch 
1172c58c0074SKeith Busch 	if (!__bio_try_merge_page(bio, page, len, offset, &same_page)) {
1173c58c0074SKeith Busch 		__bio_add_page(bio, page, len, offset);
1174c58c0074SKeith Busch 		return 0;
1175c58c0074SKeith Busch 	}
1176c58c0074SKeith Busch 
1177c58c0074SKeith Busch 	if (same_page)
1178c58c0074SKeith Busch 		put_page(page);
1179c58c0074SKeith Busch 	return 0;
1180c58c0074SKeith Busch }
1181c58c0074SKeith Busch 
1182c58c0074SKeith Busch static int bio_iov_add_zone_append_page(struct bio *bio, struct page *page,
1183c58c0074SKeith Busch 		unsigned int len, unsigned int offset)
1184c58c0074SKeith Busch {
1185c58c0074SKeith Busch 	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1186c58c0074SKeith Busch 	bool same_page = false;
1187c58c0074SKeith Busch 
1188c58c0074SKeith Busch 	if (bio_add_hw_page(q, bio, page, len, offset,
1189c58c0074SKeith Busch 			queue_max_zone_append_sectors(q), &same_page) != len)
1190c58c0074SKeith Busch 		return -EINVAL;
1191c58c0074SKeith Busch 	if (same_page)
1192c58c0074SKeith Busch 		put_page(page);
1193c58c0074SKeith Busch 	return 0;
1194c58c0074SKeith Busch }
1195c58c0074SKeith Busch 
1196576ed913SChristoph Hellwig #define PAGE_PTRS_PER_BVEC     (sizeof(struct bio_vec) / sizeof(struct page *))
1197576ed913SChristoph Hellwig 
11982cefe4dbSKent Overstreet /**
119917d51b10SMartin Wilck  * __bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
12002cefe4dbSKent Overstreet  * @bio: bio to add pages to
12012cefe4dbSKent Overstreet  * @iter: iov iterator describing the region to be mapped
12022cefe4dbSKent Overstreet  *
120317d51b10SMartin Wilck  * Pins pages from *iter and appends them to @bio's bvec array. The
12042cefe4dbSKent Overstreet  * pages will have to be released using put_page() when done.
120517d51b10SMartin Wilck  * For multi-segment *iter, this function only adds pages from the
12063cf14889SRandy Dunlap  * next non-empty segment of the iov iterator.
12072cefe4dbSKent Overstreet  */
120817d51b10SMartin Wilck static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
12092cefe4dbSKent Overstreet {
1210576ed913SChristoph Hellwig 	unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt;
1211576ed913SChristoph Hellwig 	unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt;
12122cefe4dbSKent Overstreet 	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
12132cefe4dbSKent Overstreet 	struct page **pages = (struct page **)bv;
12145e3e3f2eSLogan Gunthorpe 	unsigned int gup_flags = 0;
1215576ed913SChristoph Hellwig 	ssize_t size, left;
1216e97424fdSKeith Busch 	unsigned len, i = 0;
1217480cb846SAl Viro 	size_t offset, trim;
1218325347d9SKeith Busch 	int ret = 0;
1219576ed913SChristoph Hellwig 
1220576ed913SChristoph Hellwig 	/*
1221576ed913SChristoph Hellwig 	 * Move page array up in the allocated memory for the bio vecs as far as
1222576ed913SChristoph Hellwig 	 * possible so that we can start filling biovecs from the beginning
1223576ed913SChristoph Hellwig 	 * without overwriting the temporary page array.
1224576ed913SChristoph Hellwig 	 */
1225576ed913SChristoph Hellwig 	BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2);
1226576ed913SChristoph Hellwig 	pages += entries_left * (PAGE_PTRS_PER_BVEC - 1);
12272cefe4dbSKent Overstreet 
12285e3e3f2eSLogan Gunthorpe 	if (bio->bi_bdev && blk_queue_pci_p2pdma(bio->bi_bdev->bd_disk->queue))
12295e3e3f2eSLogan Gunthorpe 		gup_flags |= FOLL_PCI_P2PDMA;
12305e3e3f2eSLogan Gunthorpe 
1231b1a000d3SKeith Busch 	/*
1232b1a000d3SKeith Busch 	 * Each segment in the iov is required to be a block size multiple.
1233b1a000d3SKeith Busch 	 * However, we may not be able to get the entire segment if it spans
1234b1a000d3SKeith Busch 	 * more pages than bi_max_vecs allows, so we have to ALIGN_DOWN the
1235b1a000d3SKeith Busch 	 * result to ensure the bio's total size is correct. The remainder of
1236b1a000d3SKeith Busch 	 * the iov data will be picked up in the next bio iteration.
1237b1a000d3SKeith Busch 	 */
12385e3e3f2eSLogan Gunthorpe 	size = iov_iter_get_pages(iter, pages,
12395e3e3f2eSLogan Gunthorpe 				  UINT_MAX - bio->bi_iter.bi_size,
12405e3e3f2eSLogan Gunthorpe 				  nr_pages, &offset, gup_flags);
1241480cb846SAl Viro 	if (unlikely(size <= 0))
1242480cb846SAl Viro 		return size ? size : -EFAULT;
1243e97424fdSKeith Busch 
1244480cb846SAl Viro 	nr_pages = DIV_ROUND_UP(offset + size, PAGE_SIZE);
1245480cb846SAl Viro 
1246480cb846SAl Viro 	trim = size & (bdev_logical_block_size(bio->bi_bdev) - 1);
1247480cb846SAl Viro 	iov_iter_revert(iter, trim);
1248480cb846SAl Viro 
1249480cb846SAl Viro 	size -= trim;
1250480cb846SAl Viro 	if (unlikely(!size)) {
1251480cb846SAl Viro 		ret = -EFAULT;
1252e97424fdSKeith Busch 		goto out;
1253e97424fdSKeith Busch 	}
12542cefe4dbSKent Overstreet 
1255576ed913SChristoph Hellwig 	for (left = size, i = 0; left > 0; left -= len, i++) {
1256576ed913SChristoph Hellwig 		struct page *page = pages[i];
12572cefe4dbSKent Overstreet 
1258576ed913SChristoph Hellwig 		len = min_t(size_t, PAGE_SIZE - offset, left);
125934cdb8c8SKeith Busch 		if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
1260c58c0074SKeith Busch 			ret = bio_iov_add_zone_append_page(bio, page, len,
1261c58c0074SKeith Busch 					offset);
1262e97424fdSKeith Busch 			if (ret)
1263325347d9SKeith Busch 				break;
126434cdb8c8SKeith Busch 		} else
126534cdb8c8SKeith Busch 			bio_iov_add_page(bio, page, len, offset);
126634cdb8c8SKeith Busch 
1267576ed913SChristoph Hellwig 		offset = 0;
12682cefe4dbSKent Overstreet 	}
12692cefe4dbSKent Overstreet 
1270480cb846SAl Viro 	iov_iter_revert(iter, left);
1271e97424fdSKeith Busch out:
1272e97424fdSKeith Busch 	while (i < nr_pages)
1273e97424fdSKeith Busch 		put_page(pages[i++]);
1274e97424fdSKeith Busch 
1275325347d9SKeith Busch 	return ret;
12762cefe4dbSKent Overstreet }
127717d51b10SMartin Wilck 
127817d51b10SMartin Wilck /**
12796d0c48aeSJens Axboe  * bio_iov_iter_get_pages - add user or kernel pages to a bio
128017d51b10SMartin Wilck  * @bio: bio to add pages to
12816d0c48aeSJens Axboe  * @iter: iov iterator describing the region to be added
128217d51b10SMartin Wilck  *
12836d0c48aeSJens Axboe  * This takes either an iterator pointing to user memory, or one pointing to
12846d0c48aeSJens Axboe  * kernel pages (BVEC iterator). If we're adding user pages, we pin them and
12856d0c48aeSJens Axboe  * map them into the kernel. On IO completion, the caller should put those
1286c42bca92SPavel Begunkov  * pages. For bvec based iterators bio_iov_iter_get_pages() uses the provided
1287c42bca92SPavel Begunkov  * bvecs rather than copying them. Hence anyone issuing kiocb based IO needs
1288c42bca92SPavel Begunkov  * to ensure the bvecs and pages stay referenced until the submitted I/O is
1289c42bca92SPavel Begunkov  * completed by a call to ->ki_complete() or returns with an error other than
1290c42bca92SPavel Begunkov  * -EIOCBQUEUED. The caller needs to check if the bio is flagged BIO_NO_PAGE_REF
1291c42bca92SPavel Begunkov  * on IO completion. If it isn't, then pages should be released.
12926d0c48aeSJens Axboe  *
129317d51b10SMartin Wilck  * The function tries, but does not guarantee, to pin as many pages as
12945cd3ddc1SMauro Carvalho Chehab  * fit into the bio, or are requested in @iter, whatever is smaller. If
12956d0c48aeSJens Axboe  * MM encounters an error pinning the requested pages, it stops. Error
12966d0c48aeSJens Axboe  * is returned only if 0 pages could be pinned.
129717d51b10SMartin Wilck  */
129817d51b10SMartin Wilck int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
129917d51b10SMartin Wilck {
1300c42bca92SPavel Begunkov 	int ret = 0;
130114eacf12SChristoph Hellwig 
1302c42bca92SPavel Begunkov 	if (iov_iter_is_bvec(iter)) {
1303fa5fa8ecSPavel Begunkov 		bio_iov_bvec_set(bio, iter);
1304fa5fa8ecSPavel Begunkov 		iov_iter_advance(iter, bio->bi_iter.bi_size);
1305fa5fa8ecSPavel Begunkov 		return 0;
130686004515SChristoph Hellwig 	}
130717d51b10SMartin Wilck 
130817d51b10SMartin Wilck 	do {
13096d0c48aeSJens Axboe 		ret = __bio_iov_iter_get_pages(bio, iter);
131079d08f89SMing Lei 	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
131117d51b10SMartin Wilck 
131214eacf12SChristoph Hellwig 	return bio->bi_vcnt ? 0 : ret;
131317d51b10SMartin Wilck }
131429b2a3aaSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages);
13152cefe4dbSKent Overstreet 
13164246a0b6SChristoph Hellwig static void submit_bio_wait_endio(struct bio *bio)
1317f9c78b2bSJens Axboe {
131865e53aabSChristoph Hellwig 	complete(bio->bi_private);
1319f9c78b2bSJens Axboe }
1320f9c78b2bSJens Axboe 
1321f9c78b2bSJens Axboe /**
1322f9c78b2bSJens Axboe  * submit_bio_wait - submit a bio, and wait until it completes
1323f9c78b2bSJens Axboe  * @bio: The &struct bio which describes the I/O
1324f9c78b2bSJens Axboe  *
1325f9c78b2bSJens Axboe  * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
1326f9c78b2bSJens Axboe  * bio_endio() on failure.
13273d289d68SJan Kara  *
13283d289d68SJan Kara  * WARNING: Unlike to how submit_bio() is usually used, this function does not
13293d289d68SJan Kara  * result in bio reference to be consumed. The caller must drop the reference
13303d289d68SJan Kara  * on his own.
1331f9c78b2bSJens Axboe  */
13324e49ea4aSMike Christie int submit_bio_wait(struct bio *bio)
1333f9c78b2bSJens Axboe {
1334309dca30SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK_MAP(done,
1335309dca30SChristoph Hellwig 			bio->bi_bdev->bd_disk->lockdep_map);
1336de6a78b6SMing Lei 	unsigned long hang_check;
1337f9c78b2bSJens Axboe 
133865e53aabSChristoph Hellwig 	bio->bi_private = &done;
1339f9c78b2bSJens Axboe 	bio->bi_end_io = submit_bio_wait_endio;
13401eff9d32SJens Axboe 	bio->bi_opf |= REQ_SYNC;
13414e49ea4aSMike Christie 	submit_bio(bio);
1342de6a78b6SMing Lei 
1343de6a78b6SMing Lei 	/* Prevent hang_check timer from firing at us during very long I/O */
1344de6a78b6SMing Lei 	hang_check = sysctl_hung_task_timeout_secs;
1345de6a78b6SMing Lei 	if (hang_check)
1346de6a78b6SMing Lei 		while (!wait_for_completion_io_timeout(&done,
1347de6a78b6SMing Lei 					hang_check * (HZ/2)))
1348de6a78b6SMing Lei 			;
1349de6a78b6SMing Lei 	else
135065e53aabSChristoph Hellwig 		wait_for_completion_io(&done);
1351f9c78b2bSJens Axboe 
135265e53aabSChristoph Hellwig 	return blk_status_to_errno(bio->bi_status);
1353f9c78b2bSJens Axboe }
1354f9c78b2bSJens Axboe EXPORT_SYMBOL(submit_bio_wait);
1355f9c78b2bSJens Axboe 
1356d4aa57a1SJens Axboe void __bio_advance(struct bio *bio, unsigned bytes)
1357f9c78b2bSJens Axboe {
1358f9c78b2bSJens Axboe 	if (bio_integrity(bio))
1359f9c78b2bSJens Axboe 		bio_integrity_advance(bio, bytes);
1360f9c78b2bSJens Axboe 
1361a892c8d5SSatya Tangirala 	bio_crypt_advance(bio, bytes);
1362f9c78b2bSJens Axboe 	bio_advance_iter(bio, &bio->bi_iter, bytes);
1363f9c78b2bSJens Axboe }
1364d4aa57a1SJens Axboe EXPORT_SYMBOL(__bio_advance);
1365f9c78b2bSJens Axboe 
136645db54d5SKent Overstreet void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
136745db54d5SKent Overstreet 			struct bio *src, struct bvec_iter *src_iter)
1368f9c78b2bSJens Axboe {
136945db54d5SKent Overstreet 	while (src_iter->bi_size && dst_iter->bi_size) {
1370f8b679a0SChristoph Hellwig 		struct bio_vec src_bv = bio_iter_iovec(src, *src_iter);
1371f8b679a0SChristoph Hellwig 		struct bio_vec dst_bv = bio_iter_iovec(dst, *dst_iter);
1372f8b679a0SChristoph Hellwig 		unsigned int bytes = min(src_bv.bv_len, dst_bv.bv_len);
1373403d5034SChristoph Hellwig 		void *src_buf = bvec_kmap_local(&src_bv);
1374403d5034SChristoph Hellwig 		void *dst_buf = bvec_kmap_local(&dst_bv);
137545db54d5SKent Overstreet 
1376403d5034SChristoph Hellwig 		memcpy(dst_buf, src_buf, bytes);
1377403d5034SChristoph Hellwig 
1378403d5034SChristoph Hellwig 		kunmap_local(dst_buf);
1379f8b679a0SChristoph Hellwig 		kunmap_local(src_buf);
13806e6e811dSKent Overstreet 
138122b56c29SPavel Begunkov 		bio_advance_iter_single(src, src_iter, bytes);
138222b56c29SPavel Begunkov 		bio_advance_iter_single(dst, dst_iter, bytes);
138345db54d5SKent Overstreet 	}
138445db54d5SKent Overstreet }
138545db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data_iter);
138645db54d5SKent Overstreet 
138745db54d5SKent Overstreet /**
138845db54d5SKent Overstreet  * bio_copy_data - copy contents of data buffers from one bio to another
138945db54d5SKent Overstreet  * @src: source bio
139045db54d5SKent Overstreet  * @dst: destination bio
139145db54d5SKent Overstreet  *
139245db54d5SKent Overstreet  * Stops when it reaches the end of either @src or @dst - that is, copies
139345db54d5SKent Overstreet  * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
139445db54d5SKent Overstreet  */
139545db54d5SKent Overstreet void bio_copy_data(struct bio *dst, struct bio *src)
139645db54d5SKent Overstreet {
139745db54d5SKent Overstreet 	struct bvec_iter src_iter = src->bi_iter;
139845db54d5SKent Overstreet 	struct bvec_iter dst_iter = dst->bi_iter;
139945db54d5SKent Overstreet 
140045db54d5SKent Overstreet 	bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
140145db54d5SKent Overstreet }
140245db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data);
140345db54d5SKent Overstreet 
1404491221f8SGuoqing Jiang void bio_free_pages(struct bio *bio)
14051dfa0f68SChristoph Hellwig {
14061dfa0f68SChristoph Hellwig 	struct bio_vec *bvec;
14076dc4f100SMing Lei 	struct bvec_iter_all iter_all;
14081dfa0f68SChristoph Hellwig 
14092b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all)
14101dfa0f68SChristoph Hellwig 		__free_page(bvec->bv_page);
14111dfa0f68SChristoph Hellwig }
1412491221f8SGuoqing Jiang EXPORT_SYMBOL(bio_free_pages);
14131dfa0f68SChristoph Hellwig 
1414f9c78b2bSJens Axboe /*
1415f9c78b2bSJens Axboe  * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
1416f9c78b2bSJens Axboe  * for performing direct-IO in BIOs.
1417f9c78b2bSJens Axboe  *
1418f9c78b2bSJens Axboe  * The problem is that we cannot run set_page_dirty() from interrupt context
1419f9c78b2bSJens Axboe  * because the required locks are not interrupt-safe.  So what we can do is to
1420f9c78b2bSJens Axboe  * mark the pages dirty _before_ performing IO.  And in interrupt context,
1421f9c78b2bSJens Axboe  * check that the pages are still dirty.   If so, fine.  If not, redirty them
1422f9c78b2bSJens Axboe  * in process context.
1423f9c78b2bSJens Axboe  *
1424f9c78b2bSJens Axboe  * We special-case compound pages here: normally this means reads into hugetlb
1425f9c78b2bSJens Axboe  * pages.  The logic in here doesn't really work right for compound pages
1426f9c78b2bSJens Axboe  * because the VM does not uniformly chase down the head page in all cases.
1427f9c78b2bSJens Axboe  * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
1428f9c78b2bSJens Axboe  * handle them at all.  So we skip compound pages here at an early stage.
1429f9c78b2bSJens Axboe  *
1430f9c78b2bSJens Axboe  * Note that this code is very hard to test under normal circumstances because
1431f9c78b2bSJens Axboe  * direct-io pins the pages with get_user_pages().  This makes
1432f9c78b2bSJens Axboe  * is_page_cache_freeable return false, and the VM will not clean the pages.
1433f9c78b2bSJens Axboe  * But other code (eg, flusher threads) could clean the pages if they are mapped
1434f9c78b2bSJens Axboe  * pagecache.
1435f9c78b2bSJens Axboe  *
1436f9c78b2bSJens Axboe  * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
1437f9c78b2bSJens Axboe  * deferred bio dirtying paths.
1438f9c78b2bSJens Axboe  */
1439f9c78b2bSJens Axboe 
1440f9c78b2bSJens Axboe /*
1441f9c78b2bSJens Axboe  * bio_set_pages_dirty() will mark all the bio's pages as dirty.
1442f9c78b2bSJens Axboe  */
1443f9c78b2bSJens Axboe void bio_set_pages_dirty(struct bio *bio)
1444f9c78b2bSJens Axboe {
1445f9c78b2bSJens Axboe 	struct bio_vec *bvec;
14466dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1447f9c78b2bSJens Axboe 
14482b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
14493bb50983SChristoph Hellwig 		if (!PageCompound(bvec->bv_page))
14503bb50983SChristoph Hellwig 			set_page_dirty_lock(bvec->bv_page);
1451f9c78b2bSJens Axboe 	}
1452f9c78b2bSJens Axboe }
1453f9c78b2bSJens Axboe 
1454f9c78b2bSJens Axboe /*
1455f9c78b2bSJens Axboe  * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
1456f9c78b2bSJens Axboe  * If they are, then fine.  If, however, some pages are clean then they must
1457f9c78b2bSJens Axboe  * have been written out during the direct-IO read.  So we take another ref on
145824d5493fSChristoph Hellwig  * the BIO and re-dirty the pages in process context.
1459f9c78b2bSJens Axboe  *
1460f9c78b2bSJens Axboe  * It is expected that bio_check_pages_dirty() will wholly own the BIO from
1461ea1754a0SKirill A. Shutemov  * here on.  It will run one put_page() against each page and will run one
1462ea1754a0SKirill A. Shutemov  * bio_put() against the BIO.
1463f9c78b2bSJens Axboe  */
1464f9c78b2bSJens Axboe 
1465f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work);
1466f9c78b2bSJens Axboe 
1467f9c78b2bSJens Axboe static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
1468f9c78b2bSJens Axboe static DEFINE_SPINLOCK(bio_dirty_lock);
1469f9c78b2bSJens Axboe static struct bio *bio_dirty_list;
1470f9c78b2bSJens Axboe 
1471f9c78b2bSJens Axboe /*
1472f9c78b2bSJens Axboe  * This runs in process context
1473f9c78b2bSJens Axboe  */
1474f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work)
1475f9c78b2bSJens Axboe {
147624d5493fSChristoph Hellwig 	struct bio *bio, *next;
1477f9c78b2bSJens Axboe 
147824d5493fSChristoph Hellwig 	spin_lock_irq(&bio_dirty_lock);
147924d5493fSChristoph Hellwig 	next = bio_dirty_list;
1480f9c78b2bSJens Axboe 	bio_dirty_list = NULL;
148124d5493fSChristoph Hellwig 	spin_unlock_irq(&bio_dirty_lock);
1482f9c78b2bSJens Axboe 
148324d5493fSChristoph Hellwig 	while ((bio = next) != NULL) {
148424d5493fSChristoph Hellwig 		next = bio->bi_private;
1485f9c78b2bSJens Axboe 
1486d241a95fSChristoph Hellwig 		bio_release_pages(bio, true);
1487f9c78b2bSJens Axboe 		bio_put(bio);
1488f9c78b2bSJens Axboe 	}
1489f9c78b2bSJens Axboe }
1490f9c78b2bSJens Axboe 
1491f9c78b2bSJens Axboe void bio_check_pages_dirty(struct bio *bio)
1492f9c78b2bSJens Axboe {
1493f9c78b2bSJens Axboe 	struct bio_vec *bvec;
149424d5493fSChristoph Hellwig 	unsigned long flags;
14956dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1496f9c78b2bSJens Axboe 
14972b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
149824d5493fSChristoph Hellwig 		if (!PageDirty(bvec->bv_page) && !PageCompound(bvec->bv_page))
149924d5493fSChristoph Hellwig 			goto defer;
1500f9c78b2bSJens Axboe 	}
1501f9c78b2bSJens Axboe 
1502d241a95fSChristoph Hellwig 	bio_release_pages(bio, false);
150324d5493fSChristoph Hellwig 	bio_put(bio);
150424d5493fSChristoph Hellwig 	return;
150524d5493fSChristoph Hellwig defer:
1506f9c78b2bSJens Axboe 	spin_lock_irqsave(&bio_dirty_lock, flags);
1507f9c78b2bSJens Axboe 	bio->bi_private = bio_dirty_list;
1508f9c78b2bSJens Axboe 	bio_dirty_list = bio;
1509f9c78b2bSJens Axboe 	spin_unlock_irqrestore(&bio_dirty_lock, flags);
1510f9c78b2bSJens Axboe 	schedule_work(&bio_dirty_work);
1511f9c78b2bSJens Axboe }
1512f9c78b2bSJens Axboe 
1513c4cf5261SJens Axboe static inline bool bio_remaining_done(struct bio *bio)
1514c4cf5261SJens Axboe {
1515c4cf5261SJens Axboe 	/*
1516c4cf5261SJens Axboe 	 * If we're not chaining, then ->__bi_remaining is always 1 and
1517c4cf5261SJens Axboe 	 * we always end io on the first invocation.
1518c4cf5261SJens Axboe 	 */
1519c4cf5261SJens Axboe 	if (!bio_flagged(bio, BIO_CHAIN))
1520c4cf5261SJens Axboe 		return true;
1521c4cf5261SJens Axboe 
1522c4cf5261SJens Axboe 	BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
1523c4cf5261SJens Axboe 
1524326e1dbbSMike Snitzer 	if (atomic_dec_and_test(&bio->__bi_remaining)) {
1525b7c44ed9SJens Axboe 		bio_clear_flag(bio, BIO_CHAIN);
1526c4cf5261SJens Axboe 		return true;
1527326e1dbbSMike Snitzer 	}
1528c4cf5261SJens Axboe 
1529c4cf5261SJens Axboe 	return false;
1530c4cf5261SJens Axboe }
1531c4cf5261SJens Axboe 
1532f9c78b2bSJens Axboe /**
1533f9c78b2bSJens Axboe  * bio_endio - end I/O on a bio
1534f9c78b2bSJens Axboe  * @bio:	bio
1535f9c78b2bSJens Axboe  *
1536f9c78b2bSJens Axboe  * Description:
15374246a0b6SChristoph Hellwig  *   bio_endio() will end I/O on the whole bio. bio_endio() is the preferred
15384246a0b6SChristoph Hellwig  *   way to end I/O on a bio. No one should call bi_end_io() directly on a
15394246a0b6SChristoph Hellwig  *   bio unless they own it and thus know that it has an end_io function.
1540fbbaf700SNeilBrown  *
1541fbbaf700SNeilBrown  *   bio_endio() can be called several times on a bio that has been chained
1542fbbaf700SNeilBrown  *   using bio_chain().  The ->bi_end_io() function will only be called the
154360b6a7e6SEdward Hsieh  *   last time.
1544f9c78b2bSJens Axboe  **/
15454246a0b6SChristoph Hellwig void bio_endio(struct bio *bio)
1546f9c78b2bSJens Axboe {
1547ba8c6967SChristoph Hellwig again:
15482b885517SChristoph Hellwig 	if (!bio_remaining_done(bio))
1549ba8c6967SChristoph Hellwig 		return;
15507c20f116SChristoph Hellwig 	if (!bio_integrity_endio(bio))
15517c20f116SChristoph Hellwig 		return;
1552f9c78b2bSJens Axboe 
1553aa1b46dcSTejun Heo 	rq_qos_done_bio(bio);
155467b42d0bSJosef Bacik 
155560b6a7e6SEdward Hsieh 	if (bio->bi_bdev && bio_flagged(bio, BIO_TRACE_COMPLETION)) {
15563caee463SPavel Begunkov 		trace_block_bio_complete(bdev_get_queue(bio->bi_bdev), bio);
155760b6a7e6SEdward Hsieh 		bio_clear_flag(bio, BIO_TRACE_COMPLETION);
155860b6a7e6SEdward Hsieh 	}
155960b6a7e6SEdward Hsieh 
1560f9c78b2bSJens Axboe 	/*
1561ba8c6967SChristoph Hellwig 	 * Need to have a real endio function for chained bios, otherwise
1562ba8c6967SChristoph Hellwig 	 * various corner cases will break (like stacking block devices that
1563ba8c6967SChristoph Hellwig 	 * save/restore bi_end_io) - however, we want to avoid unbounded
1564ba8c6967SChristoph Hellwig 	 * recursion and blowing the stack. Tail call optimization would
1565ba8c6967SChristoph Hellwig 	 * handle this, but compiling with frame pointers also disables
1566ba8c6967SChristoph Hellwig 	 * gcc's sibling call optimization.
1567f9c78b2bSJens Axboe 	 */
1568f9c78b2bSJens Axboe 	if (bio->bi_end_io == bio_chain_endio) {
156938f8baaeSChristoph Hellwig 		bio = __bio_chain_endio(bio);
1570ba8c6967SChristoph Hellwig 		goto again;
1571ba8c6967SChristoph Hellwig 	}
1572ba8c6967SChristoph Hellwig 
15739e234eeaSShaohua Li 	blk_throtl_bio_endio(bio);
1574b222dd2fSShaohua Li 	/* release cgroup info */
1575b222dd2fSShaohua Li 	bio_uninit(bio);
1576f9c78b2bSJens Axboe 	if (bio->bi_end_io)
15774246a0b6SChristoph Hellwig 		bio->bi_end_io(bio);
1578f9c78b2bSJens Axboe }
1579f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_endio);
1580f9c78b2bSJens Axboe 
1581f9c78b2bSJens Axboe /**
1582f9c78b2bSJens Axboe  * bio_split - split a bio
1583f9c78b2bSJens Axboe  * @bio:	bio to split
1584f9c78b2bSJens Axboe  * @sectors:	number of sectors to split from the front of @bio
1585f9c78b2bSJens Axboe  * @gfp:	gfp mask
1586f9c78b2bSJens Axboe  * @bs:		bio set to allocate from
1587f9c78b2bSJens Axboe  *
1588f9c78b2bSJens Axboe  * Allocates and returns a new bio which represents @sectors from the start of
1589f9c78b2bSJens Axboe  * @bio, and updates @bio to represent the remaining sectors.
1590f9c78b2bSJens Axboe  *
1591f3f5da62SMartin K. Petersen  * Unless this is a discard request the newly allocated bio will point
1592dad77584SBart Van Assche  * to @bio's bi_io_vec. It is the caller's responsibility to ensure that
1593dad77584SBart Van Assche  * neither @bio nor @bs are freed before the split bio.
1594f9c78b2bSJens Axboe  */
1595f9c78b2bSJens Axboe struct bio *bio_split(struct bio *bio, int sectors,
1596f9c78b2bSJens Axboe 		      gfp_t gfp, struct bio_set *bs)
1597f9c78b2bSJens Axboe {
1598f341a4d3SMikulas Patocka 	struct bio *split;
1599f9c78b2bSJens Axboe 
1600f9c78b2bSJens Axboe 	BUG_ON(sectors <= 0);
1601f9c78b2bSJens Axboe 	BUG_ON(sectors >= bio_sectors(bio));
1602f9c78b2bSJens Axboe 
16030512a75bSKeith Busch 	/* Zone append commands cannot be split */
16040512a75bSKeith Busch 	if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
16050512a75bSKeith Busch 		return NULL;
16060512a75bSKeith Busch 
1607abfc426dSChristoph Hellwig 	split = bio_alloc_clone(bio->bi_bdev, bio, gfp, bs);
1608f9c78b2bSJens Axboe 	if (!split)
1609f9c78b2bSJens Axboe 		return NULL;
1610f9c78b2bSJens Axboe 
1611f9c78b2bSJens Axboe 	split->bi_iter.bi_size = sectors << 9;
1612f9c78b2bSJens Axboe 
1613f9c78b2bSJens Axboe 	if (bio_integrity(split))
1614fbd08e76SDmitry Monakhov 		bio_integrity_trim(split);
1615f9c78b2bSJens Axboe 
1616f9c78b2bSJens Axboe 	bio_advance(bio, split->bi_iter.bi_size);
1617f9c78b2bSJens Axboe 
1618fbbaf700SNeilBrown 	if (bio_flagged(bio, BIO_TRACE_COMPLETION))
161920d59023SGoldwyn Rodrigues 		bio_set_flag(split, BIO_TRACE_COMPLETION);
1620fbbaf700SNeilBrown 
1621f9c78b2bSJens Axboe 	return split;
1622f9c78b2bSJens Axboe }
1623f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_split);
1624f9c78b2bSJens Axboe 
1625f9c78b2bSJens Axboe /**
1626f9c78b2bSJens Axboe  * bio_trim - trim a bio
1627f9c78b2bSJens Axboe  * @bio:	bio to trim
1628f9c78b2bSJens Axboe  * @offset:	number of sectors to trim from the front of @bio
1629f9c78b2bSJens Axboe  * @size:	size we want to trim @bio to, in sectors
1630e83502caSChaitanya Kulkarni  *
1631e83502caSChaitanya Kulkarni  * This function is typically used for bios that are cloned and submitted
1632e83502caSChaitanya Kulkarni  * to the underlying device in parts.
1633f9c78b2bSJens Axboe  */
1634e83502caSChaitanya Kulkarni void bio_trim(struct bio *bio, sector_t offset, sector_t size)
1635f9c78b2bSJens Axboe {
1636e83502caSChaitanya Kulkarni 	if (WARN_ON_ONCE(offset > BIO_MAX_SECTORS || size > BIO_MAX_SECTORS ||
16378535c018SMing Lei 			 offset + size > bio_sectors(bio)))
1638e83502caSChaitanya Kulkarni 		return;
1639f9c78b2bSJens Axboe 
1640f9c78b2bSJens Axboe 	size <<= 9;
1641f9c78b2bSJens Axboe 	if (offset == 0 && size == bio->bi_iter.bi_size)
1642f9c78b2bSJens Axboe 		return;
1643f9c78b2bSJens Axboe 
1644f9c78b2bSJens Axboe 	bio_advance(bio, offset << 9);
1645f9c78b2bSJens Axboe 	bio->bi_iter.bi_size = size;
1646376a78abSDmitry Monakhov 
1647376a78abSDmitry Monakhov 	if (bio_integrity(bio))
1648fbd08e76SDmitry Monakhov 		bio_integrity_trim(bio);
1649f9c78b2bSJens Axboe }
1650f9c78b2bSJens Axboe EXPORT_SYMBOL_GPL(bio_trim);
1651f9c78b2bSJens Axboe 
1652f9c78b2bSJens Axboe /*
1653f9c78b2bSJens Axboe  * create memory pools for biovec's in a bio_set.
1654f9c78b2bSJens Axboe  * use the global biovec slabs created for general use.
1655f9c78b2bSJens Axboe  */
16568aa6ba2fSKent Overstreet int biovec_init_pool(mempool_t *pool, int pool_entries)
1657f9c78b2bSJens Axboe {
16587a800a20SChristoph Hellwig 	struct biovec_slab *bp = bvec_slabs + ARRAY_SIZE(bvec_slabs) - 1;
1659f9c78b2bSJens Axboe 
16608aa6ba2fSKent Overstreet 	return mempool_init_slab_pool(pool, pool_entries, bp->slab);
1661f9c78b2bSJens Axboe }
1662f9c78b2bSJens Axboe 
1663917a38c7SKent Overstreet /*
1664917a38c7SKent Overstreet  * bioset_exit - exit a bioset initialized with bioset_init()
1665917a38c7SKent Overstreet  *
1666917a38c7SKent Overstreet  * May be called on a zeroed but uninitialized bioset (i.e. allocated with
1667917a38c7SKent Overstreet  * kzalloc()).
1668917a38c7SKent Overstreet  */
1669917a38c7SKent Overstreet void bioset_exit(struct bio_set *bs)
1670f9c78b2bSJens Axboe {
1671be4d234dSJens Axboe 	bio_alloc_cache_destroy(bs);
1672f9c78b2bSJens Axboe 	if (bs->rescue_workqueue)
1673f9c78b2bSJens Axboe 		destroy_workqueue(bs->rescue_workqueue);
1674917a38c7SKent Overstreet 	bs->rescue_workqueue = NULL;
1675f9c78b2bSJens Axboe 
16768aa6ba2fSKent Overstreet 	mempool_exit(&bs->bio_pool);
16778aa6ba2fSKent Overstreet 	mempool_exit(&bs->bvec_pool);
1678f9c78b2bSJens Axboe 
1679f9c78b2bSJens Axboe 	bioset_integrity_free(bs);
1680917a38c7SKent Overstreet 	if (bs->bio_slab)
1681f9c78b2bSJens Axboe 		bio_put_slab(bs);
1682917a38c7SKent Overstreet 	bs->bio_slab = NULL;
1683917a38c7SKent Overstreet }
1684917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_exit);
1685f9c78b2bSJens Axboe 
1686011067b0SNeilBrown /**
1687917a38c7SKent Overstreet  * bioset_init - Initialize a bio_set
1688dad08527SKent Overstreet  * @bs:		pool to initialize
1689917a38c7SKent Overstreet  * @pool_size:	Number of bio and bio_vecs to cache in the mempool
1690917a38c7SKent Overstreet  * @front_pad:	Number of bytes to allocate in front of the returned bio
1691917a38c7SKent Overstreet  * @flags:	Flags to modify behavior, currently %BIOSET_NEED_BVECS
1692917a38c7SKent Overstreet  *              and %BIOSET_NEED_RESCUER
1693917a38c7SKent Overstreet  *
1694dad08527SKent Overstreet  * Description:
1695dad08527SKent Overstreet  *    Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
1696dad08527SKent Overstreet  *    to ask for a number of bytes to be allocated in front of the bio.
1697dad08527SKent Overstreet  *    Front pad allocation is useful for embedding the bio inside
1698dad08527SKent Overstreet  *    another structure, to avoid allocating extra data to go with the bio.
1699dad08527SKent Overstreet  *    Note that the bio must be embedded at the END of that structure always,
1700dad08527SKent Overstreet  *    or things will break badly.
1701dad08527SKent Overstreet  *    If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated
1702abfc426dSChristoph Hellwig  *    for allocating iovecs.  This pool is not needed e.g. for bio_init_clone().
1703abfc426dSChristoph Hellwig  *    If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used
1704abfc426dSChristoph Hellwig  *    to dispatch queued requests when the mempool runs out of space.
1705dad08527SKent Overstreet  *
1706917a38c7SKent Overstreet  */
1707917a38c7SKent Overstreet int bioset_init(struct bio_set *bs,
1708917a38c7SKent Overstreet 		unsigned int pool_size,
1709917a38c7SKent Overstreet 		unsigned int front_pad,
1710917a38c7SKent Overstreet 		int flags)
1711917a38c7SKent Overstreet {
1712917a38c7SKent Overstreet 	bs->front_pad = front_pad;
17139f180e31SMing Lei 	if (flags & BIOSET_NEED_BVECS)
17149f180e31SMing Lei 		bs->back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
17159f180e31SMing Lei 	else
17169f180e31SMing Lei 		bs->back_pad = 0;
1717917a38c7SKent Overstreet 
1718917a38c7SKent Overstreet 	spin_lock_init(&bs->rescue_lock);
1719917a38c7SKent Overstreet 	bio_list_init(&bs->rescue_list);
1720917a38c7SKent Overstreet 	INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1721917a38c7SKent Overstreet 
172249d1ec85SMing Lei 	bs->bio_slab = bio_find_or_create_slab(bs);
1723917a38c7SKent Overstreet 	if (!bs->bio_slab)
1724917a38c7SKent Overstreet 		return -ENOMEM;
1725917a38c7SKent Overstreet 
1726917a38c7SKent Overstreet 	if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab))
1727917a38c7SKent Overstreet 		goto bad;
1728917a38c7SKent Overstreet 
1729917a38c7SKent Overstreet 	if ((flags & BIOSET_NEED_BVECS) &&
1730917a38c7SKent Overstreet 	    biovec_init_pool(&bs->bvec_pool, pool_size))
1731917a38c7SKent Overstreet 		goto bad;
1732917a38c7SKent Overstreet 
1733be4d234dSJens Axboe 	if (flags & BIOSET_NEED_RESCUER) {
1734be4d234dSJens Axboe 		bs->rescue_workqueue = alloc_workqueue("bioset",
1735be4d234dSJens Axboe 							WQ_MEM_RECLAIM, 0);
1736917a38c7SKent Overstreet 		if (!bs->rescue_workqueue)
1737917a38c7SKent Overstreet 			goto bad;
1738be4d234dSJens Axboe 	}
1739be4d234dSJens Axboe 	if (flags & BIOSET_PERCPU_CACHE) {
1740be4d234dSJens Axboe 		bs->cache = alloc_percpu(struct bio_alloc_cache);
1741be4d234dSJens Axboe 		if (!bs->cache)
1742be4d234dSJens Axboe 			goto bad;
1743be4d234dSJens Axboe 		cpuhp_state_add_instance_nocalls(CPUHP_BIO_DEAD, &bs->cpuhp_dead);
1744be4d234dSJens Axboe 	}
1745917a38c7SKent Overstreet 
1746917a38c7SKent Overstreet 	return 0;
1747917a38c7SKent Overstreet bad:
1748917a38c7SKent Overstreet 	bioset_exit(bs);
1749917a38c7SKent Overstreet 	return -ENOMEM;
1750917a38c7SKent Overstreet }
1751917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_init);
1752917a38c7SKent Overstreet 
1753de76fd89SChristoph Hellwig static int __init init_bio(void)
1754f9c78b2bSJens Axboe {
1755f9c78b2bSJens Axboe 	int i;
1756f9c78b2bSJens Axboe 
1757f9c78b2bSJens Axboe 	bio_integrity_init();
1758de76fd89SChristoph Hellwig 
1759de76fd89SChristoph Hellwig 	for (i = 0; i < ARRAY_SIZE(bvec_slabs); i++) {
1760f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + i;
1761f9c78b2bSJens Axboe 
1762de76fd89SChristoph Hellwig 		bvs->slab = kmem_cache_create(bvs->name,
1763de76fd89SChristoph Hellwig 				bvs->nr_vecs * sizeof(struct bio_vec), 0,
1764f9c78b2bSJens Axboe 				SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
1765f9c78b2bSJens Axboe 	}
1766f9c78b2bSJens Axboe 
1767be4d234dSJens Axboe 	cpuhp_setup_state_multi(CPUHP_BIO_DEAD, "block/bio:dead", NULL,
1768be4d234dSJens Axboe 					bio_cpu_dead);
1769be4d234dSJens Axboe 
177012c5b70cSJens Axboe 	if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0,
177112c5b70cSJens Axboe 			BIOSET_NEED_BVECS | BIOSET_PERCPU_CACHE))
1772f9c78b2bSJens Axboe 		panic("bio: can't allocate bios\n");
1773f9c78b2bSJens Axboe 
1774f4f8154aSKent Overstreet 	if (bioset_integrity_create(&fs_bio_set, BIO_POOL_SIZE))
1775f9c78b2bSJens Axboe 		panic("bio: can't create integrity pool\n");
1776f9c78b2bSJens Axboe 
1777f9c78b2bSJens Axboe 	return 0;
1778f9c78b2bSJens Axboe }
1779f9c78b2bSJens Axboe subsys_initcall(init_bio);
1780