xref: /openbmc/linux/block/bio.c (revision 7d376555)
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 
28b99182c5SPavel Begunkov #define ALLOC_CACHE_THRESHOLD	16
2942b2b2fbSPavel Begunkov #define ALLOC_CACHE_MAX		256
30b99182c5SPavel Begunkov 
31be4d234dSJens Axboe struct bio_alloc_cache {
32fcade2ceSJens Axboe 	struct bio		*free_list;
33b99182c5SPavel Begunkov 	struct bio		*free_list_irq;
34be4d234dSJens Axboe 	unsigned int		nr;
35b99182c5SPavel Begunkov 	unsigned int		nr_irq;
36be4d234dSJens Axboe };
37be4d234dSJens Axboe 
38de76fd89SChristoph Hellwig static struct biovec_slab {
396ac0b715SChristoph Hellwig 	int nr_vecs;
406ac0b715SChristoph Hellwig 	char *name;
416ac0b715SChristoph Hellwig 	struct kmem_cache *slab;
42de76fd89SChristoph Hellwig } bvec_slabs[] __read_mostly = {
43de76fd89SChristoph Hellwig 	{ .nr_vecs = 16, .name = "biovec-16" },
44de76fd89SChristoph Hellwig 	{ .nr_vecs = 64, .name = "biovec-64" },
45de76fd89SChristoph Hellwig 	{ .nr_vecs = 128, .name = "biovec-128" },
46a8affc03SChristoph Hellwig 	{ .nr_vecs = BIO_MAX_VECS, .name = "biovec-max" },
47f9c78b2bSJens Axboe };
486ac0b715SChristoph Hellwig 
biovec_slab(unsigned short nr_vecs)497a800a20SChristoph Hellwig static struct biovec_slab *biovec_slab(unsigned short nr_vecs)
507a800a20SChristoph Hellwig {
517a800a20SChristoph Hellwig 	switch (nr_vecs) {
527a800a20SChristoph Hellwig 	/* smaller bios use inline vecs */
537a800a20SChristoph Hellwig 	case 5 ... 16:
547a800a20SChristoph Hellwig 		return &bvec_slabs[0];
557a800a20SChristoph Hellwig 	case 17 ... 64:
567a800a20SChristoph Hellwig 		return &bvec_slabs[1];
577a800a20SChristoph Hellwig 	case 65 ... 128:
587a800a20SChristoph Hellwig 		return &bvec_slabs[2];
59a8affc03SChristoph Hellwig 	case 129 ... BIO_MAX_VECS:
607a800a20SChristoph Hellwig 		return &bvec_slabs[3];
617a800a20SChristoph Hellwig 	default:
627a800a20SChristoph Hellwig 		BUG();
637a800a20SChristoph Hellwig 		return NULL;
647a800a20SChristoph Hellwig 	}
657a800a20SChristoph Hellwig }
66f9c78b2bSJens Axboe 
67f9c78b2bSJens Axboe /*
68f9c78b2bSJens Axboe  * fs_bio_set is the bio_set containing bio and iovec memory pools used by
69f9c78b2bSJens Axboe  * IO code that does not need private memory pools.
70f9c78b2bSJens Axboe  */
71f4f8154aSKent Overstreet struct bio_set fs_bio_set;
72f9c78b2bSJens Axboe EXPORT_SYMBOL(fs_bio_set);
73f9c78b2bSJens Axboe 
74f9c78b2bSJens Axboe /*
75f9c78b2bSJens Axboe  * Our slab pool management
76f9c78b2bSJens Axboe  */
77f9c78b2bSJens Axboe struct bio_slab {
78f9c78b2bSJens Axboe 	struct kmem_cache *slab;
79f9c78b2bSJens Axboe 	unsigned int slab_ref;
80f9c78b2bSJens Axboe 	unsigned int slab_size;
81f9c78b2bSJens Axboe 	char name[8];
82f9c78b2bSJens Axboe };
83f9c78b2bSJens Axboe static DEFINE_MUTEX(bio_slab_lock);
8449d1ec85SMing Lei static DEFINE_XARRAY(bio_slabs);
85f9c78b2bSJens Axboe 
create_bio_slab(unsigned int size)8649d1ec85SMing Lei static struct bio_slab *create_bio_slab(unsigned int size)
87f9c78b2bSJens Axboe {
8849d1ec85SMing Lei 	struct bio_slab *bslab = kzalloc(sizeof(*bslab), GFP_KERNEL);
8949d1ec85SMing Lei 
9049d1ec85SMing Lei 	if (!bslab)
9149d1ec85SMing Lei 		return NULL;
9249d1ec85SMing Lei 
9349d1ec85SMing Lei 	snprintf(bslab->name, sizeof(bslab->name), "bio-%d", size);
9449d1ec85SMing Lei 	bslab->slab = kmem_cache_create(bslab->name, size,
951a7e76e4SChristoph Hellwig 			ARCH_KMALLOC_MINALIGN,
961a7e76e4SChristoph Hellwig 			SLAB_HWCACHE_ALIGN | SLAB_TYPESAFE_BY_RCU, NULL);
9749d1ec85SMing Lei 	if (!bslab->slab)
9849d1ec85SMing Lei 		goto fail_alloc_slab;
9949d1ec85SMing Lei 
10049d1ec85SMing Lei 	bslab->slab_ref = 1;
10149d1ec85SMing Lei 	bslab->slab_size = size;
10249d1ec85SMing Lei 
10349d1ec85SMing Lei 	if (!xa_err(xa_store(&bio_slabs, size, bslab, GFP_KERNEL)))
10449d1ec85SMing Lei 		return bslab;
10549d1ec85SMing Lei 
10649d1ec85SMing Lei 	kmem_cache_destroy(bslab->slab);
10749d1ec85SMing Lei 
10849d1ec85SMing Lei fail_alloc_slab:
10949d1ec85SMing Lei 	kfree(bslab);
11049d1ec85SMing Lei 	return NULL;
11149d1ec85SMing Lei }
11249d1ec85SMing Lei 
bs_bio_slab_size(struct bio_set * bs)11349d1ec85SMing Lei static inline unsigned int bs_bio_slab_size(struct bio_set *bs)
11449d1ec85SMing Lei {
1159f180e31SMing Lei 	return bs->front_pad + sizeof(struct bio) + bs->back_pad;
11649d1ec85SMing Lei }
11749d1ec85SMing Lei 
bio_find_or_create_slab(struct bio_set * bs)11849d1ec85SMing Lei static struct kmem_cache *bio_find_or_create_slab(struct bio_set *bs)
11949d1ec85SMing Lei {
12049d1ec85SMing Lei 	unsigned int size = bs_bio_slab_size(bs);
12149d1ec85SMing Lei 	struct bio_slab *bslab;
122f9c78b2bSJens Axboe 
123f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
12449d1ec85SMing Lei 	bslab = xa_load(&bio_slabs, size);
12549d1ec85SMing Lei 	if (bslab)
126f9c78b2bSJens Axboe 		bslab->slab_ref++;
12749d1ec85SMing Lei 	else
12849d1ec85SMing Lei 		bslab = create_bio_slab(size);
129f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
13049d1ec85SMing Lei 
13149d1ec85SMing Lei 	if (bslab)
13249d1ec85SMing Lei 		return bslab->slab;
13349d1ec85SMing Lei 	return NULL;
134f9c78b2bSJens Axboe }
135f9c78b2bSJens Axboe 
bio_put_slab(struct bio_set * bs)136f9c78b2bSJens Axboe static void bio_put_slab(struct bio_set *bs)
137f9c78b2bSJens Axboe {
138f9c78b2bSJens Axboe 	struct bio_slab *bslab = NULL;
13949d1ec85SMing Lei 	unsigned int slab_size = bs_bio_slab_size(bs);
140f9c78b2bSJens Axboe 
141f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
142f9c78b2bSJens Axboe 
14349d1ec85SMing Lei 	bslab = xa_load(&bio_slabs, slab_size);
144f9c78b2bSJens Axboe 	if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
145f9c78b2bSJens Axboe 		goto out;
146f9c78b2bSJens Axboe 
14749d1ec85SMing Lei 	WARN_ON_ONCE(bslab->slab != bs->bio_slab);
14849d1ec85SMing Lei 
149f9c78b2bSJens Axboe 	WARN_ON(!bslab->slab_ref);
150f9c78b2bSJens Axboe 
151f9c78b2bSJens Axboe 	if (--bslab->slab_ref)
152f9c78b2bSJens Axboe 		goto out;
153f9c78b2bSJens Axboe 
15449d1ec85SMing Lei 	xa_erase(&bio_slabs, slab_size);
15549d1ec85SMing Lei 
156f9c78b2bSJens Axboe 	kmem_cache_destroy(bslab->slab);
15749d1ec85SMing Lei 	kfree(bslab);
158f9c78b2bSJens Axboe 
159f9c78b2bSJens Axboe out:
160f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
161f9c78b2bSJens Axboe }
162f9c78b2bSJens Axboe 
bvec_free(mempool_t * pool,struct bio_vec * bv,unsigned short nr_vecs)1637a800a20SChristoph Hellwig void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned short nr_vecs)
164f9c78b2bSJens Axboe {
1659e8c0d0dSChristoph Hellwig 	BUG_ON(nr_vecs > BIO_MAX_VECS);
166f9c78b2bSJens Axboe 
167a8affc03SChristoph Hellwig 	if (nr_vecs == BIO_MAX_VECS)
168f9c78b2bSJens Axboe 		mempool_free(bv, pool);
1697a800a20SChristoph Hellwig 	else if (nr_vecs > BIO_INLINE_VECS)
1707a800a20SChristoph Hellwig 		kmem_cache_free(biovec_slab(nr_vecs)->slab, bv);
171f9c78b2bSJens Axboe }
172f9c78b2bSJens Axboe 
173f2c3eb9bSChristoph Hellwig /*
174f2c3eb9bSChristoph Hellwig  * Make the first allocation restricted and don't dump info on allocation
175f2c3eb9bSChristoph Hellwig  * failures, since we'll fall back to the mempool in case of failure.
176f2c3eb9bSChristoph Hellwig  */
bvec_alloc_gfp(gfp_t gfp)177f2c3eb9bSChristoph Hellwig static inline gfp_t bvec_alloc_gfp(gfp_t gfp)
178f9c78b2bSJens Axboe {
179f2c3eb9bSChristoph Hellwig 	return (gfp & ~(__GFP_DIRECT_RECLAIM | __GFP_IO)) |
180f2c3eb9bSChristoph Hellwig 		__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
181f2c3eb9bSChristoph Hellwig }
182f2c3eb9bSChristoph Hellwig 
bvec_alloc(mempool_t * pool,unsigned short * nr_vecs,gfp_t gfp_mask)1837a800a20SChristoph Hellwig struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs,
1847a800a20SChristoph Hellwig 		gfp_t gfp_mask)
185f9c78b2bSJens Axboe {
1867a800a20SChristoph Hellwig 	struct biovec_slab *bvs = biovec_slab(*nr_vecs);
1877a800a20SChristoph Hellwig 
1887a800a20SChristoph Hellwig 	if (WARN_ON_ONCE(!bvs))
189f9c78b2bSJens Axboe 		return NULL;
1907a800a20SChristoph Hellwig 
1917a800a20SChristoph Hellwig 	/*
1927a800a20SChristoph Hellwig 	 * Upgrade the nr_vecs request to take full advantage of the allocation.
1937a800a20SChristoph Hellwig 	 * We also rely on this in the bvec_free path.
1947a800a20SChristoph Hellwig 	 */
1957a800a20SChristoph Hellwig 	*nr_vecs = bvs->nr_vecs;
196f9c78b2bSJens Axboe 
197f9c78b2bSJens Axboe 	/*
198f007a3d6SChristoph Hellwig 	 * Try a slab allocation first for all smaller allocations.  If that
199f007a3d6SChristoph Hellwig 	 * fails and __GFP_DIRECT_RECLAIM is set retry with the mempool.
200a8affc03SChristoph Hellwig 	 * The mempool is sized to handle up to BIO_MAX_VECS entries.
201f9c78b2bSJens Axboe 	 */
202a8affc03SChristoph Hellwig 	if (*nr_vecs < BIO_MAX_VECS) {
203f9c78b2bSJens Axboe 		struct bio_vec *bvl;
204f9c78b2bSJens Axboe 
205f2c3eb9bSChristoph Hellwig 		bvl = kmem_cache_alloc(bvs->slab, bvec_alloc_gfp(gfp_mask));
2067a800a20SChristoph Hellwig 		if (likely(bvl) || !(gfp_mask & __GFP_DIRECT_RECLAIM))
207f9c78b2bSJens Axboe 			return bvl;
208a8affc03SChristoph Hellwig 		*nr_vecs = BIO_MAX_VECS;
209f9c78b2bSJens Axboe 	}
210f9c78b2bSJens Axboe 
211f007a3d6SChristoph Hellwig 	return mempool_alloc(pool, gfp_mask);
212f9c78b2bSJens Axboe }
213f9c78b2bSJens Axboe 
bio_uninit(struct bio * bio)2149ae3b3f5SJens Axboe void bio_uninit(struct bio *bio)
215f9c78b2bSJens Axboe {
216db9819c7SChristoph Hellwig #ifdef CONFIG_BLK_CGROUP
217db9819c7SChristoph Hellwig 	if (bio->bi_blkg) {
218db9819c7SChristoph Hellwig 		blkg_put(bio->bi_blkg);
219db9819c7SChristoph Hellwig 		bio->bi_blkg = NULL;
220db9819c7SChristoph Hellwig 	}
221db9819c7SChristoph Hellwig #endif
222ece841abSJustin Tee 	if (bio_integrity(bio))
223ece841abSJustin Tee 		bio_integrity_free(bio);
224a892c8d5SSatya Tangirala 
225a892c8d5SSatya Tangirala 	bio_crypt_free_ctx(bio);
226f9c78b2bSJens Axboe }
2279ae3b3f5SJens Axboe EXPORT_SYMBOL(bio_uninit);
228f9c78b2bSJens Axboe 
bio_free(struct bio * bio)229f9c78b2bSJens Axboe static void bio_free(struct bio *bio)
230f9c78b2bSJens Axboe {
231f9c78b2bSJens Axboe 	struct bio_set *bs = bio->bi_pool;
232066ff571SChristoph Hellwig 	void *p = bio;
233066ff571SChristoph Hellwig 
234066ff571SChristoph Hellwig 	WARN_ON_ONCE(!bs);
235f9c78b2bSJens Axboe 
2369ae3b3f5SJens Axboe 	bio_uninit(bio);
2377a800a20SChristoph Hellwig 	bvec_free(&bs->bvec_pool, bio->bi_io_vec, bio->bi_max_vecs);
238066ff571SChristoph Hellwig 	mempool_free(p - bs->front_pad, &bs->bio_pool);
239f9c78b2bSJens Axboe }
240f9c78b2bSJens Axboe 
2419ae3b3f5SJens Axboe /*
2429ae3b3f5SJens Axboe  * Users of this function have their own bio allocation. Subsequently,
2439ae3b3f5SJens Axboe  * they must remember to pair any call to bio_init() with bio_uninit()
2449ae3b3f5SJens Axboe  * when IO has completed, or when the bio is released.
2459ae3b3f5SJens Axboe  */
bio_init(struct bio * bio,struct block_device * bdev,struct bio_vec * table,unsigned short max_vecs,blk_opf_t opf)24649add496SChristoph Hellwig void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *table,
24716458cf3SBart Van Assche 	      unsigned short max_vecs, blk_opf_t opf)
248f9c78b2bSJens Axboe {
249da521626SJens Axboe 	bio->bi_next = NULL;
25049add496SChristoph Hellwig 	bio->bi_bdev = bdev;
25149add496SChristoph Hellwig 	bio->bi_opf = opf;
252da521626SJens Axboe 	bio->bi_flags = 0;
253da521626SJens Axboe 	bio->bi_ioprio = 0;
254da521626SJens Axboe 	bio->bi_status = 0;
255da521626SJens Axboe 	bio->bi_iter.bi_sector = 0;
256da521626SJens Axboe 	bio->bi_iter.bi_size = 0;
257da521626SJens Axboe 	bio->bi_iter.bi_idx = 0;
258da521626SJens Axboe 	bio->bi_iter.bi_bvec_done = 0;
259da521626SJens Axboe 	bio->bi_end_io = NULL;
260da521626SJens Axboe 	bio->bi_private = NULL;
261da521626SJens Axboe #ifdef CONFIG_BLK_CGROUP
262da521626SJens Axboe 	bio->bi_blkg = NULL;
263da521626SJens Axboe 	bio->bi_issue.value = 0;
26449add496SChristoph Hellwig 	if (bdev)
26549add496SChristoph Hellwig 		bio_associate_blkg(bio);
266da521626SJens Axboe #ifdef CONFIG_BLK_CGROUP_IOCOST
267da521626SJens Axboe 	bio->bi_iocost_cost = 0;
268da521626SJens Axboe #endif
269da521626SJens Axboe #endif
270da521626SJens Axboe #ifdef CONFIG_BLK_INLINE_ENCRYPTION
271da521626SJens Axboe 	bio->bi_crypt_context = NULL;
272da521626SJens Axboe #endif
273da521626SJens Axboe #ifdef CONFIG_BLK_DEV_INTEGRITY
274da521626SJens Axboe 	bio->bi_integrity = NULL;
275da521626SJens Axboe #endif
276da521626SJens Axboe 	bio->bi_vcnt = 0;
277da521626SJens Axboe 
278c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
279dac56212SJens Axboe 	atomic_set(&bio->__bi_cnt, 1);
2803e08773cSChristoph Hellwig 	bio->bi_cookie = BLK_QC_T_NONE;
2813a83f467SMing Lei 
2823a83f467SMing Lei 	bio->bi_max_vecs = max_vecs;
283da521626SJens Axboe 	bio->bi_io_vec = table;
284da521626SJens Axboe 	bio->bi_pool = NULL;
285f9c78b2bSJens Axboe }
286f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_init);
287f9c78b2bSJens Axboe 
288f9c78b2bSJens Axboe /**
289f9c78b2bSJens Axboe  * bio_reset - reinitialize a bio
290f9c78b2bSJens Axboe  * @bio:	bio to reset
291a7c50c94SChristoph Hellwig  * @bdev:	block device to use the bio for
292a7c50c94SChristoph Hellwig  * @opf:	operation and flags for bio
293f9c78b2bSJens Axboe  *
294f9c78b2bSJens Axboe  * Description:
295f9c78b2bSJens Axboe  *   After calling bio_reset(), @bio will be in the same state as a freshly
296f9c78b2bSJens Axboe  *   allocated bio returned bio bio_alloc_bioset() - the only fields that are
297f9c78b2bSJens Axboe  *   preserved are the ones that are initialized by bio_alloc_bioset(). See
298f9c78b2bSJens Axboe  *   comment in struct bio.
299f9c78b2bSJens Axboe  */
bio_reset(struct bio * bio,struct block_device * bdev,blk_opf_t opf)30016458cf3SBart Van Assche void bio_reset(struct bio *bio, struct block_device *bdev, blk_opf_t opf)
301f9c78b2bSJens Axboe {
3029ae3b3f5SJens Axboe 	bio_uninit(bio);
303f9c78b2bSJens Axboe 	memset(bio, 0, BIO_RESET_BYTES);
304c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
305a7c50c94SChristoph Hellwig 	bio->bi_bdev = bdev;
30678e34374SChristoph Hellwig 	if (bio->bi_bdev)
30778e34374SChristoph Hellwig 		bio_associate_blkg(bio);
308a7c50c94SChristoph Hellwig 	bio->bi_opf = opf;
309f9c78b2bSJens Axboe }
310f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_reset);
311f9c78b2bSJens Axboe 
__bio_chain_endio(struct bio * bio)31238f8baaeSChristoph Hellwig static struct bio *__bio_chain_endio(struct bio *bio)
313f9c78b2bSJens Axboe {
3144246a0b6SChristoph Hellwig 	struct bio *parent = bio->bi_private;
3154246a0b6SChristoph Hellwig 
3163edf5346SYufen Yu 	if (bio->bi_status && !parent->bi_status)
3174e4cbee9SChristoph Hellwig 		parent->bi_status = bio->bi_status;
318f9c78b2bSJens Axboe 	bio_put(bio);
31938f8baaeSChristoph Hellwig 	return parent;
32038f8baaeSChristoph Hellwig }
32138f8baaeSChristoph Hellwig 
bio_chain_endio(struct bio * bio)32238f8baaeSChristoph Hellwig static void bio_chain_endio(struct bio *bio)
32338f8baaeSChristoph Hellwig {
32438f8baaeSChristoph Hellwig 	bio_endio(__bio_chain_endio(bio));
325f9c78b2bSJens Axboe }
326f9c78b2bSJens Axboe 
327f9c78b2bSJens Axboe /**
328f9c78b2bSJens Axboe  * bio_chain - chain bio completions
329f9c78b2bSJens Axboe  * @bio: the target bio
3305b874af6SMauro Carvalho Chehab  * @parent: the parent bio of @bio
331f9c78b2bSJens Axboe  *
332f9c78b2bSJens Axboe  * The caller won't have a bi_end_io called when @bio completes - instead,
333f9c78b2bSJens Axboe  * @parent's bi_end_io won't be called until both @parent and @bio have
334f9c78b2bSJens Axboe  * completed; the chained bio will also be freed when it completes.
335f9c78b2bSJens Axboe  *
336f9c78b2bSJens Axboe  * The caller must not set bi_private or bi_end_io in @bio.
337f9c78b2bSJens Axboe  */
bio_chain(struct bio * bio,struct bio * parent)338f9c78b2bSJens Axboe void bio_chain(struct bio *bio, struct bio *parent)
339f9c78b2bSJens Axboe {
340f9c78b2bSJens Axboe 	BUG_ON(bio->bi_private || bio->bi_end_io);
341f9c78b2bSJens Axboe 
342f9c78b2bSJens Axboe 	bio->bi_private = parent;
343f9c78b2bSJens Axboe 	bio->bi_end_io	= bio_chain_endio;
344c4cf5261SJens Axboe 	bio_inc_remaining(parent);
345f9c78b2bSJens Axboe }
346f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_chain);
347f9c78b2bSJens Axboe 
blk_next_bio(struct bio * bio,struct block_device * bdev,unsigned int nr_pages,blk_opf_t opf,gfp_t gfp)3480a3140eaSChaitanya Kulkarni struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev,
34916458cf3SBart Van Assche 		unsigned int nr_pages, blk_opf_t opf, gfp_t gfp)
3503b005bf6SChristoph Hellwig {
35107888c66SChristoph Hellwig 	struct bio *new = bio_alloc(bdev, nr_pages, opf, gfp);
3520a3140eaSChaitanya Kulkarni 
3533b005bf6SChristoph Hellwig 	if (bio) {
3543b005bf6SChristoph Hellwig 		bio_chain(bio, new);
3553b005bf6SChristoph Hellwig 		submit_bio(bio);
3563b005bf6SChristoph Hellwig 	}
3573b005bf6SChristoph Hellwig 
3583b005bf6SChristoph Hellwig 	return new;
3593b005bf6SChristoph Hellwig }
3603b005bf6SChristoph Hellwig EXPORT_SYMBOL_GPL(blk_next_bio);
3613b005bf6SChristoph Hellwig 
bio_alloc_rescue(struct work_struct * work)362f9c78b2bSJens Axboe static void bio_alloc_rescue(struct work_struct *work)
363f9c78b2bSJens Axboe {
364f9c78b2bSJens Axboe 	struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
365f9c78b2bSJens Axboe 	struct bio *bio;
366f9c78b2bSJens Axboe 
367f9c78b2bSJens Axboe 	while (1) {
368f9c78b2bSJens Axboe 		spin_lock(&bs->rescue_lock);
369f9c78b2bSJens Axboe 		bio = bio_list_pop(&bs->rescue_list);
370f9c78b2bSJens Axboe 		spin_unlock(&bs->rescue_lock);
371f9c78b2bSJens Axboe 
372f9c78b2bSJens Axboe 		if (!bio)
373f9c78b2bSJens Axboe 			break;
374f9c78b2bSJens Axboe 
375ed00aabdSChristoph Hellwig 		submit_bio_noacct(bio);
376f9c78b2bSJens Axboe 	}
377f9c78b2bSJens Axboe }
378f9c78b2bSJens Axboe 
punt_bios_to_rescuer(struct bio_set * bs)379f9c78b2bSJens Axboe static void punt_bios_to_rescuer(struct bio_set *bs)
380f9c78b2bSJens Axboe {
381f9c78b2bSJens Axboe 	struct bio_list punt, nopunt;
382f9c78b2bSJens Axboe 	struct bio *bio;
383f9c78b2bSJens Axboe 
38447e0fb46SNeilBrown 	if (WARN_ON_ONCE(!bs->rescue_workqueue))
38547e0fb46SNeilBrown 		return;
386f9c78b2bSJens Axboe 	/*
387f9c78b2bSJens Axboe 	 * In order to guarantee forward progress we must punt only bios that
388f9c78b2bSJens Axboe 	 * were allocated from this bio_set; otherwise, if there was a bio on
389f9c78b2bSJens Axboe 	 * there for a stacking driver higher up in the stack, processing it
390f9c78b2bSJens Axboe 	 * could require allocating bios from this bio_set, and doing that from
391f9c78b2bSJens Axboe 	 * our own rescuer would be bad.
392f9c78b2bSJens Axboe 	 *
393f9c78b2bSJens Axboe 	 * Since bio lists are singly linked, pop them all instead of trying to
394f9c78b2bSJens Axboe 	 * remove from the middle of the list:
395f9c78b2bSJens Axboe 	 */
396f9c78b2bSJens Axboe 
397f9c78b2bSJens Axboe 	bio_list_init(&punt);
398f9c78b2bSJens Axboe 	bio_list_init(&nopunt);
399f9c78b2bSJens Axboe 
400f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[0])))
401f9c78b2bSJens Axboe 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
402f5fe1b51SNeilBrown 	current->bio_list[0] = nopunt;
403f9c78b2bSJens Axboe 
404f5fe1b51SNeilBrown 	bio_list_init(&nopunt);
405f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[1])))
406f5fe1b51SNeilBrown 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
407f5fe1b51SNeilBrown 	current->bio_list[1] = nopunt;
408f9c78b2bSJens Axboe 
409f9c78b2bSJens Axboe 	spin_lock(&bs->rescue_lock);
410f9c78b2bSJens Axboe 	bio_list_merge(&bs->rescue_list, &punt);
411f9c78b2bSJens Axboe 	spin_unlock(&bs->rescue_lock);
412f9c78b2bSJens Axboe 
413f9c78b2bSJens Axboe 	queue_work(bs->rescue_workqueue, &bs->rescue_work);
414f9c78b2bSJens Axboe }
415f9c78b2bSJens Axboe 
bio_alloc_irq_cache_splice(struct bio_alloc_cache * cache)416b99182c5SPavel Begunkov static void bio_alloc_irq_cache_splice(struct bio_alloc_cache *cache)
417b99182c5SPavel Begunkov {
418b99182c5SPavel Begunkov 	unsigned long flags;
419b99182c5SPavel Begunkov 
420b99182c5SPavel Begunkov 	/* cache->free_list must be empty */
421b99182c5SPavel Begunkov 	if (WARN_ON_ONCE(cache->free_list))
422b99182c5SPavel Begunkov 		return;
423b99182c5SPavel Begunkov 
424b99182c5SPavel Begunkov 	local_irq_save(flags);
425b99182c5SPavel Begunkov 	cache->free_list = cache->free_list_irq;
426b99182c5SPavel Begunkov 	cache->free_list_irq = NULL;
427b99182c5SPavel Begunkov 	cache->nr += cache->nr_irq;
428b99182c5SPavel Begunkov 	cache->nr_irq = 0;
429b99182c5SPavel Begunkov 	local_irq_restore(flags);
430b99182c5SPavel Begunkov }
431b99182c5SPavel Begunkov 
bio_alloc_percpu_cache(struct block_device * bdev,unsigned short nr_vecs,blk_opf_t opf,gfp_t gfp,struct bio_set * bs)4320df71650SMike Snitzer static struct bio *bio_alloc_percpu_cache(struct block_device *bdev,
43316458cf3SBart Van Assche 		unsigned short nr_vecs, blk_opf_t opf, gfp_t gfp,
4340df71650SMike Snitzer 		struct bio_set *bs)
4350df71650SMike Snitzer {
4360df71650SMike Snitzer 	struct bio_alloc_cache *cache;
4370df71650SMike Snitzer 	struct bio *bio;
4380df71650SMike Snitzer 
4390df71650SMike Snitzer 	cache = per_cpu_ptr(bs->cache, get_cpu());
4400df71650SMike Snitzer 	if (!cache->free_list) {
441b99182c5SPavel Begunkov 		if (READ_ONCE(cache->nr_irq) >= ALLOC_CACHE_THRESHOLD)
442b99182c5SPavel Begunkov 			bio_alloc_irq_cache_splice(cache);
443b99182c5SPavel Begunkov 		if (!cache->free_list) {
4440df71650SMike Snitzer 			put_cpu();
4450df71650SMike Snitzer 			return NULL;
4460df71650SMike Snitzer 		}
447b99182c5SPavel Begunkov 	}
4480df71650SMike Snitzer 	bio = cache->free_list;
4490df71650SMike Snitzer 	cache->free_list = bio->bi_next;
4500df71650SMike Snitzer 	cache->nr--;
4510df71650SMike Snitzer 	put_cpu();
4520df71650SMike Snitzer 
4530df71650SMike Snitzer 	bio_init(bio, bdev, nr_vecs ? bio->bi_inline_vecs : NULL, nr_vecs, opf);
4540df71650SMike Snitzer 	bio->bi_pool = bs;
4550df71650SMike Snitzer 	return bio;
4560df71650SMike Snitzer }
4570df71650SMike Snitzer 
458f9c78b2bSJens Axboe /**
459f9c78b2bSJens Axboe  * bio_alloc_bioset - allocate a bio for I/O
460609be106SChristoph Hellwig  * @bdev:	block device to allocate the bio for (can be %NULL)
461609be106SChristoph Hellwig  * @nr_vecs:	number of bvecs to pre-allocate
462609be106SChristoph Hellwig  * @opf:	operation and flags for bio
463519c8e9fSRandy Dunlap  * @gfp_mask:   the GFP_* mask given to the slab allocator
464f9c78b2bSJens Axboe  * @bs:		the bio_set to allocate from.
465f9c78b2bSJens Axboe  *
4663175199aSChristoph Hellwig  * Allocate a bio from the mempools in @bs.
467f9c78b2bSJens Axboe  *
4683175199aSChristoph Hellwig  * If %__GFP_DIRECT_RECLAIM is set then bio_alloc will always be able to
4693175199aSChristoph Hellwig  * allocate a bio.  This is due to the mempool guarantees.  To make this work,
4703175199aSChristoph Hellwig  * callers must never allocate more than 1 bio at a time from the general pool.
4713175199aSChristoph Hellwig  * Callers that need to allocate more than 1 bio must always submit the
4723175199aSChristoph Hellwig  * previously allocated bio for IO before attempting to allocate a new one.
4733175199aSChristoph Hellwig  * Failure to do so can cause deadlocks under memory pressure.
474f9c78b2bSJens Axboe  *
4753175199aSChristoph Hellwig  * Note that when running under submit_bio_noacct() (i.e. any block driver),
4763175199aSChristoph Hellwig  * bios are not submitted until after you return - see the code in
477ed00aabdSChristoph Hellwig  * submit_bio_noacct() that converts recursion into iteration, to prevent
478f9c78b2bSJens Axboe  * stack overflows.
479f9c78b2bSJens Axboe  *
4803175199aSChristoph Hellwig  * This would normally mean allocating multiple bios under submit_bio_noacct()
4813175199aSChristoph Hellwig  * would be susceptible to deadlocks, but we have
482f9c78b2bSJens Axboe  * deadlock avoidance code that resubmits any blocked bios from a rescuer
483f9c78b2bSJens Axboe  * thread.
484f9c78b2bSJens Axboe  *
485f9c78b2bSJens Axboe  * However, we do not guarantee forward progress for allocations from other
486f9c78b2bSJens Axboe  * mempools. Doing multiple allocations from the same mempool under
487ed00aabdSChristoph Hellwig  * submit_bio_noacct() should be avoided - instead, use bio_set's front_pad
488f9c78b2bSJens Axboe  * for per bio allocations.
489f9c78b2bSJens Axboe  *
4903175199aSChristoph Hellwig  * Returns: Pointer to new bio on success, NULL on failure.
491f9c78b2bSJens Axboe  */
bio_alloc_bioset(struct block_device * bdev,unsigned short nr_vecs,blk_opf_t opf,gfp_t gfp_mask,struct bio_set * bs)492609be106SChristoph Hellwig struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
49316458cf3SBart Van Assche 			     blk_opf_t opf, gfp_t gfp_mask,
4947a88fa19SDan Carpenter 			     struct bio_set *bs)
495f9c78b2bSJens Axboe {
496f9c78b2bSJens Axboe 	gfp_t saved_gfp = gfp_mask;
497f9c78b2bSJens Axboe 	struct bio *bio;
498f9c78b2bSJens Axboe 	void *p;
499f9c78b2bSJens Axboe 
500609be106SChristoph Hellwig 	/* should not use nobvec bioset for nr_vecs > 0 */
501609be106SChristoph Hellwig 	if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && nr_vecs > 0))
502f9c78b2bSJens Axboe 		return NULL;
503f9c78b2bSJens Axboe 
5040df71650SMike Snitzer 	if (opf & REQ_ALLOC_CACHE) {
5050df71650SMike Snitzer 		if (bs->cache && nr_vecs <= BIO_INLINE_VECS) {
5060df71650SMike Snitzer 			bio = bio_alloc_percpu_cache(bdev, nr_vecs, opf,
5070df71650SMike Snitzer 						     gfp_mask, bs);
5080df71650SMike Snitzer 			if (bio)
5090df71650SMike Snitzer 				return bio;
5100df71650SMike Snitzer 			/*
5110df71650SMike Snitzer 			 * No cached bio available, bio returned below marked with
5120df71650SMike Snitzer 			 * REQ_ALLOC_CACHE to particpate in per-cpu alloc cache.
5130df71650SMike Snitzer 			 */
5140df71650SMike Snitzer 		} else {
5150df71650SMike Snitzer 			opf &= ~REQ_ALLOC_CACHE;
5160df71650SMike Snitzer 		}
5170df71650SMike Snitzer 	}
5180df71650SMike Snitzer 
519f9c78b2bSJens Axboe 	/*
5203175199aSChristoph Hellwig 	 * submit_bio_noacct() converts recursion to iteration; this means if
5213175199aSChristoph Hellwig 	 * we're running beneath it, any bios we allocate and submit will not be
5223175199aSChristoph Hellwig 	 * submitted (and thus freed) until after we return.
523f9c78b2bSJens Axboe 	 *
5243175199aSChristoph Hellwig 	 * This exposes us to a potential deadlock if we allocate multiple bios
5253175199aSChristoph Hellwig 	 * from the same bio_set() while running underneath submit_bio_noacct().
5263175199aSChristoph Hellwig 	 * If we were to allocate multiple bios (say a stacking block driver
5273175199aSChristoph Hellwig 	 * that was splitting bios), we would deadlock if we exhausted the
5283175199aSChristoph Hellwig 	 * mempool's reserve.
529f9c78b2bSJens Axboe 	 *
530f9c78b2bSJens Axboe 	 * We solve this, and guarantee forward progress, with a rescuer
5313175199aSChristoph Hellwig 	 * workqueue per bio_set. If we go to allocate and there are bios on
5323175199aSChristoph Hellwig 	 * current->bio_list, we first try the allocation without
5333175199aSChristoph Hellwig 	 * __GFP_DIRECT_RECLAIM; if that fails, we punt those bios we would be
5343175199aSChristoph Hellwig 	 * blocking to the rescuer workqueue before we retry with the original
5353175199aSChristoph Hellwig 	 * gfp_flags.
536f9c78b2bSJens Axboe 	 */
537f5fe1b51SNeilBrown 	if (current->bio_list &&
538f5fe1b51SNeilBrown 	    (!bio_list_empty(&current->bio_list[0]) ||
53947e0fb46SNeilBrown 	     !bio_list_empty(&current->bio_list[1])) &&
54047e0fb46SNeilBrown 	    bs->rescue_workqueue)
541d0164adcSMel Gorman 		gfp_mask &= ~__GFP_DIRECT_RECLAIM;
542f9c78b2bSJens Axboe 
5438aa6ba2fSKent Overstreet 	p = mempool_alloc(&bs->bio_pool, gfp_mask);
544f9c78b2bSJens Axboe 	if (!p && gfp_mask != saved_gfp) {
545f9c78b2bSJens Axboe 		punt_bios_to_rescuer(bs);
546f9c78b2bSJens Axboe 		gfp_mask = saved_gfp;
5478aa6ba2fSKent Overstreet 		p = mempool_alloc(&bs->bio_pool, gfp_mask);
548f9c78b2bSJens Axboe 	}
549f9c78b2bSJens Axboe 	if (unlikely(!p))
550f9c78b2bSJens Axboe 		return NULL;
551759aa12fSPavel Begunkov 	if (!mempool_is_saturated(&bs->bio_pool))
552759aa12fSPavel Begunkov 		opf &= ~REQ_ALLOC_CACHE;
553f9c78b2bSJens Axboe 
5543175199aSChristoph Hellwig 	bio = p + bs->front_pad;
555609be106SChristoph Hellwig 	if (nr_vecs > BIO_INLINE_VECS) {
5563175199aSChristoph Hellwig 		struct bio_vec *bvl = NULL;
557f9c78b2bSJens Axboe 
558609be106SChristoph Hellwig 		bvl = bvec_alloc(&bs->bvec_pool, &nr_vecs, gfp_mask);
559f9c78b2bSJens Axboe 		if (!bvl && gfp_mask != saved_gfp) {
560f9c78b2bSJens Axboe 			punt_bios_to_rescuer(bs);
561f9c78b2bSJens Axboe 			gfp_mask = saved_gfp;
562609be106SChristoph Hellwig 			bvl = bvec_alloc(&bs->bvec_pool, &nr_vecs, gfp_mask);
563f9c78b2bSJens Axboe 		}
564f9c78b2bSJens Axboe 		if (unlikely(!bvl))
565f9c78b2bSJens Axboe 			goto err_free;
566f9c78b2bSJens Axboe 
56749add496SChristoph Hellwig 		bio_init(bio, bdev, bvl, nr_vecs, opf);
568609be106SChristoph Hellwig 	} else if (nr_vecs) {
56949add496SChristoph Hellwig 		bio_init(bio, bdev, bio->bi_inline_vecs, BIO_INLINE_VECS, opf);
5703175199aSChristoph Hellwig 	} else {
57149add496SChristoph Hellwig 		bio_init(bio, bdev, NULL, 0, opf);
572f9c78b2bSJens Axboe 	}
573f9c78b2bSJens Axboe 
574f9c78b2bSJens Axboe 	bio->bi_pool = bs;
575f9c78b2bSJens Axboe 	return bio;
576f9c78b2bSJens Axboe 
577f9c78b2bSJens Axboe err_free:
5788aa6ba2fSKent Overstreet 	mempool_free(p, &bs->bio_pool);
579f9c78b2bSJens Axboe 	return NULL;
580f9c78b2bSJens Axboe }
581f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_bioset);
582f9c78b2bSJens Axboe 
5833175199aSChristoph Hellwig /**
584066ff571SChristoph Hellwig  * bio_kmalloc - kmalloc a bio
585066ff571SChristoph Hellwig  * @nr_vecs:	number of bio_vecs to allocate
5863175199aSChristoph Hellwig  * @gfp_mask:   the GFP_* mask given to the slab allocator
5873175199aSChristoph Hellwig  *
588066ff571SChristoph Hellwig  * Use kmalloc to allocate a bio (including bvecs).  The bio must be initialized
589066ff571SChristoph Hellwig  * using bio_init() before use.  To free a bio returned from this function use
590066ff571SChristoph Hellwig  * kfree() after calling bio_uninit().  A bio returned from this function can
591066ff571SChristoph Hellwig  * be reused by calling bio_uninit() before calling bio_init() again.
592066ff571SChristoph Hellwig  *
593066ff571SChristoph Hellwig  * Note that unlike bio_alloc() or bio_alloc_bioset() allocations from this
594340e1347SDeming Wang  * function are not backed by a mempool can fail.  Do not use this function
595066ff571SChristoph Hellwig  * for allocations in the file system I/O path.
5963175199aSChristoph Hellwig  *
5973175199aSChristoph Hellwig  * Returns: Pointer to new bio on success, NULL on failure.
5983175199aSChristoph Hellwig  */
bio_kmalloc(unsigned short nr_vecs,gfp_t gfp_mask)599066ff571SChristoph Hellwig struct bio *bio_kmalloc(unsigned short nr_vecs, gfp_t gfp_mask)
6003175199aSChristoph Hellwig {
6013175199aSChristoph Hellwig 	struct bio *bio;
6023175199aSChristoph Hellwig 
603066ff571SChristoph Hellwig 	if (nr_vecs > UIO_MAXIOV)
6043175199aSChristoph Hellwig 		return NULL;
605066ff571SChristoph Hellwig 	return kmalloc(struct_size(bio, bi_inline_vecs, nr_vecs), gfp_mask);
6063175199aSChristoph Hellwig }
6073175199aSChristoph Hellwig EXPORT_SYMBOL(bio_kmalloc);
6083175199aSChristoph Hellwig 
zero_fill_bio_iter(struct bio * bio,struct bvec_iter start)609649f070eSKent Overstreet void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start)
610f9c78b2bSJens Axboe {
611f9c78b2bSJens Axboe 	struct bio_vec bv;
612f9c78b2bSJens Axboe 	struct bvec_iter iter;
613f9c78b2bSJens Axboe 
614649f070eSKent Overstreet 	__bio_for_each_segment(bv, bio, iter, start)
615ab6c340eSChristoph Hellwig 		memzero_bvec(&bv);
616f9c78b2bSJens Axboe }
617649f070eSKent Overstreet EXPORT_SYMBOL(zero_fill_bio_iter);
618f9c78b2bSJens Axboe 
61983c9c547SMing Lei /**
62083c9c547SMing Lei  * bio_truncate - truncate the bio to small size of @new_size
62183c9c547SMing Lei  * @bio:	the bio to be truncated
62283c9c547SMing Lei  * @new_size:	new size for truncating the bio
62383c9c547SMing Lei  *
62483c9c547SMing Lei  * Description:
62583c9c547SMing Lei  *   Truncate the bio to new size of @new_size. If bio_op(bio) is
62683c9c547SMing Lei  *   REQ_OP_READ, zero the truncated part. This function should only
62783c9c547SMing Lei  *   be used for handling corner cases, such as bio eod.
62883c9c547SMing Lei  */
bio_truncate(struct bio * bio,unsigned new_size)6294f7ab09aSChristoph Hellwig static void bio_truncate(struct bio *bio, unsigned new_size)
63085a8ce62SMing Lei {
63185a8ce62SMing Lei 	struct bio_vec bv;
63285a8ce62SMing Lei 	struct bvec_iter iter;
63385a8ce62SMing Lei 	unsigned int done = 0;
63485a8ce62SMing Lei 	bool truncated = false;
63585a8ce62SMing Lei 
63685a8ce62SMing Lei 	if (new_size >= bio->bi_iter.bi_size)
63785a8ce62SMing Lei 		return;
63885a8ce62SMing Lei 
63983c9c547SMing Lei 	if (bio_op(bio) != REQ_OP_READ)
64085a8ce62SMing Lei 		goto exit;
64185a8ce62SMing Lei 
64285a8ce62SMing Lei 	bio_for_each_segment(bv, bio, iter) {
64385a8ce62SMing Lei 		if (done + bv.bv_len > new_size) {
64485a8ce62SMing Lei 			unsigned offset;
64585a8ce62SMing Lei 
64685a8ce62SMing Lei 			if (!truncated)
64785a8ce62SMing Lei 				offset = new_size - done;
64885a8ce62SMing Lei 			else
64985a8ce62SMing Lei 				offset = 0;
6503ee859e3SOGAWA Hirofumi 			zero_user(bv.bv_page, bv.bv_offset + offset,
6513ee859e3SOGAWA Hirofumi 				  bv.bv_len - offset);
65285a8ce62SMing Lei 			truncated = true;
65385a8ce62SMing Lei 		}
65485a8ce62SMing Lei 		done += bv.bv_len;
65585a8ce62SMing Lei 	}
65685a8ce62SMing Lei 
65785a8ce62SMing Lei  exit:
65885a8ce62SMing Lei 	/*
65985a8ce62SMing Lei 	 * Don't touch bvec table here and make it really immutable, since
66085a8ce62SMing Lei 	 * fs bio user has to retrieve all pages via bio_for_each_segment_all
66185a8ce62SMing Lei 	 * in its .end_bio() callback.
66285a8ce62SMing Lei 	 *
66385a8ce62SMing Lei 	 * It is enough to truncate bio by updating .bi_size since we can make
66485a8ce62SMing Lei 	 * correct bvec with the updated .bi_size for drivers.
66585a8ce62SMing Lei 	 */
66685a8ce62SMing Lei 	bio->bi_iter.bi_size = new_size;
66785a8ce62SMing Lei }
66885a8ce62SMing Lei 
669f9c78b2bSJens Axboe /**
67029125ed6SChristoph Hellwig  * guard_bio_eod - truncate a BIO to fit the block device
67129125ed6SChristoph Hellwig  * @bio:	bio to truncate
67229125ed6SChristoph Hellwig  *
67329125ed6SChristoph Hellwig  * This allows us to do IO even on the odd last sectors of a device, even if the
67429125ed6SChristoph Hellwig  * block size is some multiple of the physical sector size.
67529125ed6SChristoph Hellwig  *
67629125ed6SChristoph Hellwig  * We'll just truncate the bio to the size of the device, and clear the end of
67729125ed6SChristoph Hellwig  * the buffer head manually.  Truly out-of-range accesses will turn into actual
67829125ed6SChristoph Hellwig  * I/O errors, this only handles the "we need to be able to do I/O at the final
67929125ed6SChristoph Hellwig  * sector" case.
68029125ed6SChristoph Hellwig  */
guard_bio_eod(struct bio * bio)68129125ed6SChristoph Hellwig void guard_bio_eod(struct bio *bio)
68229125ed6SChristoph Hellwig {
683309dca30SChristoph Hellwig 	sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
68429125ed6SChristoph Hellwig 
68529125ed6SChristoph Hellwig 	if (!maxsector)
68629125ed6SChristoph Hellwig 		return;
68729125ed6SChristoph Hellwig 
68829125ed6SChristoph Hellwig 	/*
68929125ed6SChristoph Hellwig 	 * If the *whole* IO is past the end of the device,
69029125ed6SChristoph Hellwig 	 * let it through, and the IO layer will turn it into
69129125ed6SChristoph Hellwig 	 * an EIO.
69229125ed6SChristoph Hellwig 	 */
69329125ed6SChristoph Hellwig 	if (unlikely(bio->bi_iter.bi_sector >= maxsector))
69429125ed6SChristoph Hellwig 		return;
69529125ed6SChristoph Hellwig 
69629125ed6SChristoph Hellwig 	maxsector -= bio->bi_iter.bi_sector;
69729125ed6SChristoph Hellwig 	if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
69829125ed6SChristoph Hellwig 		return;
69929125ed6SChristoph Hellwig 
70029125ed6SChristoph Hellwig 	bio_truncate(bio, maxsector << 9);
70129125ed6SChristoph Hellwig }
70229125ed6SChristoph Hellwig 
__bio_alloc_cache_prune(struct bio_alloc_cache * cache,unsigned int nr)703b99182c5SPavel Begunkov static int __bio_alloc_cache_prune(struct bio_alloc_cache *cache,
704be4d234dSJens Axboe 				   unsigned int nr)
705be4d234dSJens Axboe {
706be4d234dSJens Axboe 	unsigned int i = 0;
707be4d234dSJens Axboe 	struct bio *bio;
708be4d234dSJens Axboe 
709fcade2ceSJens Axboe 	while ((bio = cache->free_list) != NULL) {
710fcade2ceSJens Axboe 		cache->free_list = bio->bi_next;
711be4d234dSJens Axboe 		cache->nr--;
712be4d234dSJens Axboe 		bio_free(bio);
713be4d234dSJens Axboe 		if (++i == nr)
714be4d234dSJens Axboe 			break;
715be4d234dSJens Axboe 	}
716b99182c5SPavel Begunkov 	return i;
717b99182c5SPavel Begunkov }
718b99182c5SPavel Begunkov 
bio_alloc_cache_prune(struct bio_alloc_cache * cache,unsigned int nr)719b99182c5SPavel Begunkov static void bio_alloc_cache_prune(struct bio_alloc_cache *cache,
720b99182c5SPavel Begunkov 				  unsigned int nr)
721b99182c5SPavel Begunkov {
722b99182c5SPavel Begunkov 	nr -= __bio_alloc_cache_prune(cache, nr);
723b99182c5SPavel Begunkov 	if (!READ_ONCE(cache->free_list)) {
724b99182c5SPavel Begunkov 		bio_alloc_irq_cache_splice(cache);
725b99182c5SPavel Begunkov 		__bio_alloc_cache_prune(cache, nr);
726b99182c5SPavel Begunkov 	}
727be4d234dSJens Axboe }
728be4d234dSJens Axboe 
bio_cpu_dead(unsigned int cpu,struct hlist_node * node)729be4d234dSJens Axboe static int bio_cpu_dead(unsigned int cpu, struct hlist_node *node)
730be4d234dSJens Axboe {
731be4d234dSJens Axboe 	struct bio_set *bs;
732be4d234dSJens Axboe 
733be4d234dSJens Axboe 	bs = hlist_entry_safe(node, struct bio_set, cpuhp_dead);
734be4d234dSJens Axboe 	if (bs->cache) {
735be4d234dSJens Axboe 		struct bio_alloc_cache *cache = per_cpu_ptr(bs->cache, cpu);
736be4d234dSJens Axboe 
737be4d234dSJens Axboe 		bio_alloc_cache_prune(cache, -1U);
738be4d234dSJens Axboe 	}
739be4d234dSJens Axboe 	return 0;
740be4d234dSJens Axboe }
741be4d234dSJens Axboe 
bio_alloc_cache_destroy(struct bio_set * bs)742be4d234dSJens Axboe static void bio_alloc_cache_destroy(struct bio_set *bs)
743be4d234dSJens Axboe {
744be4d234dSJens Axboe 	int cpu;
745be4d234dSJens Axboe 
746be4d234dSJens Axboe 	if (!bs->cache)
747be4d234dSJens Axboe 		return;
748be4d234dSJens Axboe 
749be4d234dSJens Axboe 	cpuhp_state_remove_instance_nocalls(CPUHP_BIO_DEAD, &bs->cpuhp_dead);
750be4d234dSJens Axboe 	for_each_possible_cpu(cpu) {
751be4d234dSJens Axboe 		struct bio_alloc_cache *cache;
752be4d234dSJens Axboe 
753be4d234dSJens Axboe 		cache = per_cpu_ptr(bs->cache, cpu);
754be4d234dSJens Axboe 		bio_alloc_cache_prune(cache, -1U);
755be4d234dSJens Axboe 	}
756be4d234dSJens Axboe 	free_percpu(bs->cache);
757605f7415SJens Axboe 	bs->cache = NULL;
758be4d234dSJens Axboe }
759be4d234dSJens Axboe 
bio_put_percpu_cache(struct bio * bio)760f25cf75aSPavel Begunkov static inline void bio_put_percpu_cache(struct bio *bio)
761f25cf75aSPavel Begunkov {
762f25cf75aSPavel Begunkov 	struct bio_alloc_cache *cache;
763f25cf75aSPavel Begunkov 
764f25cf75aSPavel Begunkov 	cache = per_cpu_ptr(bio->bi_pool->cache, get_cpu());
765b99182c5SPavel Begunkov 	if (READ_ONCE(cache->nr_irq) + cache->nr > ALLOC_CACHE_MAX) {
766b99182c5SPavel Begunkov 		put_cpu();
767b99182c5SPavel Begunkov 		bio_free(bio);
768b99182c5SPavel Begunkov 		return;
769b99182c5SPavel Begunkov 	}
770b99182c5SPavel Begunkov 
771f25cf75aSPavel Begunkov 	bio_uninit(bio);
772f25cf75aSPavel Begunkov 
773f25cf75aSPavel Begunkov 	if ((bio->bi_opf & REQ_POLLED) && !WARN_ON_ONCE(in_interrupt())) {
774f25cf75aSPavel Begunkov 		bio->bi_next = cache->free_list;
77511eb695fSJens Axboe 		bio->bi_bdev = NULL;
776f25cf75aSPavel Begunkov 		cache->free_list = bio;
777f25cf75aSPavel Begunkov 		cache->nr++;
778f25cf75aSPavel Begunkov 	} else {
779b99182c5SPavel Begunkov 		unsigned long flags;
780f25cf75aSPavel Begunkov 
781b99182c5SPavel Begunkov 		local_irq_save(flags);
782b99182c5SPavel Begunkov 		bio->bi_next = cache->free_list_irq;
783b99182c5SPavel Begunkov 		cache->free_list_irq = bio;
784b99182c5SPavel Begunkov 		cache->nr_irq++;
785b99182c5SPavel Begunkov 		local_irq_restore(flags);
786b99182c5SPavel Begunkov 	}
787f25cf75aSPavel Begunkov 	put_cpu();
788f25cf75aSPavel Begunkov }
789f25cf75aSPavel Begunkov 
79029125ed6SChristoph Hellwig /**
791f9c78b2bSJens Axboe  * bio_put - release a reference to a bio
792f9c78b2bSJens Axboe  * @bio:   bio to release reference to
793f9c78b2bSJens Axboe  *
794f9c78b2bSJens Axboe  * Description:
795f9c78b2bSJens Axboe  *   Put a reference to a &struct bio, either one you have gotten with
7969b10f6a9SNeilBrown  *   bio_alloc, bio_get or bio_clone_*. The last put of a bio will free it.
797f9c78b2bSJens Axboe  **/
bio_put(struct bio * bio)798f9c78b2bSJens Axboe void bio_put(struct bio *bio)
799f9c78b2bSJens Axboe {
800be4d234dSJens Axboe 	if (unlikely(bio_flagged(bio, BIO_REFFED))) {
8019e8c0d0dSChristoph Hellwig 		BUG_ON(!atomic_read(&bio->__bi_cnt));
802be4d234dSJens Axboe 		if (!atomic_dec_and_test(&bio->__bi_cnt))
803be4d234dSJens Axboe 			return;
804be4d234dSJens Axboe 	}
805f25cf75aSPavel Begunkov 	if (bio->bi_opf & REQ_ALLOC_CACHE)
806f25cf75aSPavel Begunkov 		bio_put_percpu_cache(bio);
807f25cf75aSPavel Begunkov 	else
808f9c78b2bSJens Axboe 		bio_free(bio);
809f9c78b2bSJens Axboe }
810f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_put);
811f9c78b2bSJens Axboe 
__bio_clone(struct bio * bio,struct bio * bio_src,gfp_t gfp)812a0e8de79SChristoph Hellwig static int __bio_clone(struct bio *bio, struct bio *bio_src, gfp_t gfp)
813f9c78b2bSJens Axboe {
814b7c44ed9SJens Axboe 	bio_set_flag(bio, BIO_CLONED);
815ca474b73SHannes Reinecke 	bio->bi_ioprio = bio_src->bi_ioprio;
816f9c78b2bSJens Axboe 	bio->bi_iter = bio_src->bi_iter;
81720bd723eSPaolo Valente 
8187ecc56c6SChristoph Hellwig 	if (bio->bi_bdev) {
8197ecc56c6SChristoph Hellwig 		if (bio->bi_bdev == bio_src->bi_bdev &&
8207ecc56c6SChristoph Hellwig 		    bio_flagged(bio_src, BIO_REMAPPED))
8217ecc56c6SChristoph Hellwig 			bio_set_flag(bio, BIO_REMAPPED);
822db6638d7SDennis Zhou 		bio_clone_blkg_association(bio, bio_src);
8237ecc56c6SChristoph Hellwig 	}
82456b4b5abSChristoph Hellwig 
82556b4b5abSChristoph Hellwig 	if (bio_crypt_clone(bio, bio_src, gfp) < 0)
82656b4b5abSChristoph Hellwig 		return -ENOMEM;
82756b4b5abSChristoph Hellwig 	if (bio_integrity(bio_src) &&
82856b4b5abSChristoph Hellwig 	    bio_integrity_clone(bio, bio_src, gfp) < 0)
82956b4b5abSChristoph Hellwig 		return -ENOMEM;
83056b4b5abSChristoph Hellwig 	return 0;
831f9c78b2bSJens Axboe }
832f9c78b2bSJens Axboe 
833f9c78b2bSJens Axboe /**
834abfc426dSChristoph Hellwig  * bio_alloc_clone - clone a bio that shares the original bio's biovec
835abfc426dSChristoph Hellwig  * @bdev: block_device to clone onto
836a0e8de79SChristoph Hellwig  * @bio_src: bio to clone from
837a0e8de79SChristoph Hellwig  * @gfp: allocation priority
838f9c78b2bSJens Axboe  * @bs: bio_set to allocate from
839f9c78b2bSJens Axboe  *
840a0e8de79SChristoph Hellwig  * Allocate a new bio that is a clone of @bio_src. The caller owns the returned
841a0e8de79SChristoph Hellwig  * bio, but not the actual data it points to.
842a0e8de79SChristoph Hellwig  *
843a0e8de79SChristoph Hellwig  * The caller must ensure that the return bio is not freed before @bio_src.
844f9c78b2bSJens Axboe  */
bio_alloc_clone(struct block_device * bdev,struct bio * bio_src,gfp_t gfp,struct bio_set * bs)845abfc426dSChristoph Hellwig struct bio *bio_alloc_clone(struct block_device *bdev, struct bio *bio_src,
846abfc426dSChristoph Hellwig 		gfp_t gfp, struct bio_set *bs)
847f9c78b2bSJens Axboe {
848a0e8de79SChristoph Hellwig 	struct bio *bio;
849f9c78b2bSJens Axboe 
850abfc426dSChristoph Hellwig 	bio = bio_alloc_bioset(bdev, 0, bio_src->bi_opf, gfp, bs);
851a0e8de79SChristoph Hellwig 	if (!bio)
852f9c78b2bSJens Axboe 		return NULL;
853f9c78b2bSJens Axboe 
854a0e8de79SChristoph Hellwig 	if (__bio_clone(bio, bio_src, gfp) < 0) {
855a0e8de79SChristoph Hellwig 		bio_put(bio);
85607560151SEric Biggers 		return NULL;
857f9c78b2bSJens Axboe 	}
858a0e8de79SChristoph Hellwig 	bio->bi_io_vec = bio_src->bi_io_vec;
85956b4b5abSChristoph Hellwig 
860a0e8de79SChristoph Hellwig 	return bio;
86156b4b5abSChristoph Hellwig }
862abfc426dSChristoph Hellwig EXPORT_SYMBOL(bio_alloc_clone);
863f9c78b2bSJens Axboe 
864a0e8de79SChristoph Hellwig /**
865abfc426dSChristoph Hellwig  * bio_init_clone - clone a bio that shares the original bio's biovec
866abfc426dSChristoph Hellwig  * @bdev: block_device to clone onto
867a0e8de79SChristoph Hellwig  * @bio: bio to clone into
868a0e8de79SChristoph Hellwig  * @bio_src: bio to clone from
869a0e8de79SChristoph Hellwig  * @gfp: allocation priority
870a0e8de79SChristoph Hellwig  *
871a0e8de79SChristoph Hellwig  * Initialize a new bio in caller provided memory that is a clone of @bio_src.
872a0e8de79SChristoph Hellwig  * The caller owns the returned bio, but not the actual data it points to.
873a0e8de79SChristoph Hellwig  *
874a0e8de79SChristoph Hellwig  * The caller must ensure that @bio_src is not freed before @bio.
875a0e8de79SChristoph Hellwig  */
bio_init_clone(struct block_device * bdev,struct bio * bio,struct bio * bio_src,gfp_t gfp)876abfc426dSChristoph Hellwig int bio_init_clone(struct block_device *bdev, struct bio *bio,
877abfc426dSChristoph Hellwig 		struct bio *bio_src, gfp_t gfp)
878a0e8de79SChristoph Hellwig {
879a0e8de79SChristoph Hellwig 	int ret;
880a0e8de79SChristoph Hellwig 
881abfc426dSChristoph Hellwig 	bio_init(bio, bdev, bio_src->bi_io_vec, 0, bio_src->bi_opf);
882a0e8de79SChristoph Hellwig 	ret = __bio_clone(bio, bio_src, gfp);
883a0e8de79SChristoph Hellwig 	if (ret)
884a0e8de79SChristoph Hellwig 		bio_uninit(bio);
885a0e8de79SChristoph Hellwig 	return ret;
886a0e8de79SChristoph Hellwig }
887abfc426dSChristoph Hellwig EXPORT_SYMBOL(bio_init_clone);
888a0e8de79SChristoph Hellwig 
8899a6083beSChristoph Hellwig /**
8909a6083beSChristoph Hellwig  * bio_full - check if the bio is full
8919a6083beSChristoph Hellwig  * @bio:	bio to check
8929a6083beSChristoph Hellwig  * @len:	length of one segment to be added
8939a6083beSChristoph Hellwig  *
8949a6083beSChristoph Hellwig  * Return true if @bio is full and one segment with @len bytes can't be
8959a6083beSChristoph Hellwig  * added to the bio, otherwise return false
8969a6083beSChristoph Hellwig  */
bio_full(struct bio * bio,unsigned len)8979a6083beSChristoph Hellwig static inline bool bio_full(struct bio *bio, unsigned len)
8989a6083beSChristoph Hellwig {
8999a6083beSChristoph Hellwig 	if (bio->bi_vcnt >= bio->bi_max_vecs)
9009a6083beSChristoph Hellwig 		return true;
9019a6083beSChristoph Hellwig 	if (bio->bi_iter.bi_size > UINT_MAX - len)
9029a6083beSChristoph Hellwig 		return true;
9039a6083beSChristoph Hellwig 	return false;
9049a6083beSChristoph Hellwig }
9059a6083beSChristoph Hellwig 
bvec_try_merge_page(struct bio_vec * bv,struct page * page,unsigned int len,unsigned int off,bool * same_page)906858c708dSChristoph Hellwig static bool bvec_try_merge_page(struct bio_vec *bv, struct page *page,
907858c708dSChristoph Hellwig 		unsigned int len, unsigned int off, bool *same_page)
9085919482eSMing Lei {
909d8166519SMatthew Wilcox (Oracle) 	size_t bv_end = bv->bv_offset + bv->bv_len;
910d8166519SMatthew Wilcox (Oracle) 	phys_addr_t vec_end_addr = page_to_phys(bv->bv_page) + bv_end - 1;
9115919482eSMing Lei 	phys_addr_t page_addr = page_to_phys(page);
9125919482eSMing Lei 
9135919482eSMing Lei 	if (vec_end_addr + 1 != page_addr + off)
9145919482eSMing Lei 		return false;
9155919482eSMing Lei 	if (xen_domain() && !xen_biovec_phys_mergeable(bv, page))
9165919482eSMing Lei 		return false;
91749580e69SLogan Gunthorpe 	if (!zone_device_pages_have_same_pgmap(bv->bv_page, page))
91849580e69SLogan Gunthorpe 		return false;
91952d52d1cSChristoph Hellwig 
920ff896738SChristoph Hellwig 	*same_page = ((vec_end_addr & PAGE_MASK) == page_addr);
921858c708dSChristoph Hellwig 	if (!*same_page) {
922858c708dSChristoph Hellwig 		if (IS_ENABLED(CONFIG_KMSAN))
92311b331f8SAlexander Potapenko 			return false;
924858c708dSChristoph Hellwig 		if (bv->bv_page + bv_end / PAGE_SIZE != page + off / PAGE_SIZE)
925858c708dSChristoph Hellwig 			return false;
9265919482eSMing Lei 	}
9275919482eSMing Lei 
9289774b391SChristoph Hellwig 	bv->bv_len += len;
9299774b391SChristoph Hellwig 	return true;
9309774b391SChristoph Hellwig }
9319774b391SChristoph Hellwig 
932e4581105SChristoph Hellwig /*
933e4581105SChristoph Hellwig  * Try to merge a page into a segment, while obeying the hardware segment
934e4581105SChristoph Hellwig  * size limit.  This is not for normal read/write bios, but for passthrough
935e4581105SChristoph Hellwig  * or Zone Append operations that we can't split.
936e4581105SChristoph Hellwig  */
bvec_try_merge_hw_page(struct request_queue * q,struct bio_vec * bv,struct page * page,unsigned len,unsigned offset,bool * same_page)9377c8998f7SJinyoung Choi bool bvec_try_merge_hw_page(struct request_queue *q, struct bio_vec *bv,
938ae42f0b3SChristoph Hellwig 		struct page *page, unsigned len, unsigned offset,
939ae42f0b3SChristoph Hellwig 		bool *same_page)
940489fbbcbSMing Lei {
941489fbbcbSMing Lei 	unsigned long mask = queue_segment_boundary(q);
942489fbbcbSMing Lei 	phys_addr_t addr1 = page_to_phys(bv->bv_page) + bv->bv_offset;
943489fbbcbSMing Lei 	phys_addr_t addr2 = page_to_phys(page) + offset + len - 1;
944489fbbcbSMing Lei 
945489fbbcbSMing Lei 	if ((addr1 | mask) != (addr2 | mask))
946489fbbcbSMing Lei 		return false;
94785f24d72SChristoph Hellwig 	if (len > queue_max_segment_size(q) - bv->bv_len)
948489fbbcbSMing Lei 		return false;
949858c708dSChristoph Hellwig 	return bvec_try_merge_page(bv, page, len, offset, same_page);
950489fbbcbSMing Lei }
951489fbbcbSMing Lei 
952f4595875SShaohua Li /**
953e4581105SChristoph Hellwig  * bio_add_hw_page - attempt to add a page to a bio with hw constraints
954c66a14d0SKent Overstreet  * @q: the target queue
955c66a14d0SKent Overstreet  * @bio: destination bio
956c66a14d0SKent Overstreet  * @page: page to add
957c66a14d0SKent Overstreet  * @len: vec entry length
958c66a14d0SKent Overstreet  * @offset: vec entry offset
959e4581105SChristoph Hellwig  * @max_sectors: maximum number of sectors that can be added
960e4581105SChristoph Hellwig  * @same_page: return if the segment has been merged inside the same page
961f9c78b2bSJens Axboe  *
962e4581105SChristoph Hellwig  * Add a page to a bio while respecting the hardware max_sectors, max_segment
963e4581105SChristoph Hellwig  * and gap limitations.
964f9c78b2bSJens Axboe  */
bio_add_hw_page(struct request_queue * q,struct bio * bio,struct page * page,unsigned int len,unsigned int offset,unsigned int max_sectors,bool * same_page)965e4581105SChristoph Hellwig int bio_add_hw_page(struct request_queue *q, struct bio *bio,
96619047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset,
967e4581105SChristoph Hellwig 		unsigned int max_sectors, bool *same_page)
968f9c78b2bSJens Axboe {
969e4581105SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
970f9c78b2bSJens Axboe 		return 0;
971f9c78b2bSJens Axboe 
9726850b2ddSChristoph Hellwig 	if (((bio->bi_iter.bi_size + len) >> SECTOR_SHIFT) > max_sectors)
973f9c78b2bSJens Axboe 		return 0;
974f9c78b2bSJens Axboe 
975f9c78b2bSJens Axboe 	if (bio->bi_vcnt > 0) {
976ae42f0b3SChristoph Hellwig 		struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
977ae42f0b3SChristoph Hellwig 
978ae42f0b3SChristoph Hellwig 		if (bvec_try_merge_hw_page(q, bv, page, len, offset,
979858c708dSChristoph Hellwig 				same_page)) {
980858c708dSChristoph Hellwig 			bio->bi_iter.bi_size += len;
981384209cdSChristoph Hellwig 			return len;
982858c708dSChristoph Hellwig 		}
983320ea869SChristoph Hellwig 
984cd1d83e2SChristoph Hellwig 		if (bio->bi_vcnt >=
985cd1d83e2SChristoph Hellwig 		    min(bio->bi_max_vecs, queue_max_segments(q)))
986cd1d83e2SChristoph Hellwig 			return 0;
987cd1d83e2SChristoph Hellwig 
988320ea869SChristoph Hellwig 		/*
989320ea869SChristoph Hellwig 		 * If the queue doesn't support SG gaps and adding this segment
990320ea869SChristoph Hellwig 		 * would create a gap, disallow it.
991320ea869SChristoph Hellwig 		 */
992ae42f0b3SChristoph Hellwig 		if (bvec_gap_to_prev(&q->limits, bv, offset))
993320ea869SChristoph Hellwig 			return 0;
994f9c78b2bSJens Axboe 	}
995f9c78b2bSJens Axboe 
996d58cdfaeSChristoph Hellwig 	bvec_set_page(&bio->bi_io_vec[bio->bi_vcnt], page, len, offset);
997fcbf6a08SMaurizio Lombardi 	bio->bi_vcnt++;
998dcdca753SChristoph Hellwig 	bio->bi_iter.bi_size += len;
999f9c78b2bSJens Axboe 	return len;
1000f9c78b2bSJens Axboe }
100119047087SMing Lei 
1002e4581105SChristoph Hellwig /**
1003e4581105SChristoph Hellwig  * bio_add_pc_page	- attempt to add page to passthrough bio
1004e4581105SChristoph Hellwig  * @q: the target queue
1005e4581105SChristoph Hellwig  * @bio: destination bio
1006e4581105SChristoph Hellwig  * @page: page to add
1007e4581105SChristoph Hellwig  * @len: vec entry length
1008e4581105SChristoph Hellwig  * @offset: vec entry offset
1009e4581105SChristoph Hellwig  *
1010e4581105SChristoph Hellwig  * Attempt to add a page to the bio_vec maplist. This can fail for a
1011e4581105SChristoph Hellwig  * number of reasons, such as the bio being full or target block device
1012e4581105SChristoph Hellwig  * limitations. The target block device must allow bio's up to PAGE_SIZE,
1013e4581105SChristoph Hellwig  * so it is always possible to add a single page to an empty bio.
1014e4581105SChristoph Hellwig  *
1015e4581105SChristoph Hellwig  * This should only be used by passthrough bios.
1016e4581105SChristoph Hellwig  */
bio_add_pc_page(struct request_queue * q,struct bio * bio,struct page * page,unsigned int len,unsigned int offset)101719047087SMing Lei int bio_add_pc_page(struct request_queue *q, struct bio *bio,
101819047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset)
101919047087SMing Lei {
1020d1916c86SChristoph Hellwig 	bool same_page = false;
1021e4581105SChristoph Hellwig 	return bio_add_hw_page(q, bio, page, len, offset,
1022e4581105SChristoph Hellwig 			queue_max_hw_sectors(q), &same_page);
102319047087SMing Lei }
1024f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_pc_page);
1025f9c78b2bSJens Axboe 
1026f9c78b2bSJens Axboe /**
1027ae29333fSJohannes Thumshirn  * bio_add_zone_append_page - attempt to add page to zone-append bio
1028ae29333fSJohannes Thumshirn  * @bio: destination bio
1029ae29333fSJohannes Thumshirn  * @page: page to add
1030ae29333fSJohannes Thumshirn  * @len: vec entry length
1031ae29333fSJohannes Thumshirn  * @offset: vec entry offset
1032ae29333fSJohannes Thumshirn  *
1033ae29333fSJohannes Thumshirn  * Attempt to add a page to the bio_vec maplist of a bio that will be submitted
1034ae29333fSJohannes Thumshirn  * for a zone-append request. This can fail for a number of reasons, such as the
1035ae29333fSJohannes Thumshirn  * bio being full or the target block device is not a zoned block device or
1036ae29333fSJohannes Thumshirn  * other limitations of the target block device. The target block device must
1037ae29333fSJohannes Thumshirn  * allow bio's up to PAGE_SIZE, so it is always possible to add a single page
1038ae29333fSJohannes Thumshirn  * to an empty bio.
1039ae29333fSJohannes Thumshirn  *
1040ae29333fSJohannes Thumshirn  * Returns: number of bytes added to the bio, or 0 in case of a failure.
1041ae29333fSJohannes Thumshirn  */
bio_add_zone_append_page(struct bio * bio,struct page * page,unsigned int len,unsigned int offset)1042ae29333fSJohannes Thumshirn int bio_add_zone_append_page(struct bio *bio, struct page *page,
1043ae29333fSJohannes Thumshirn 			     unsigned int len, unsigned int offset)
1044ae29333fSJohannes Thumshirn {
10453caee463SPavel Begunkov 	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1046ae29333fSJohannes Thumshirn 	bool same_page = false;
1047ae29333fSJohannes Thumshirn 
1048ae29333fSJohannes Thumshirn 	if (WARN_ON_ONCE(bio_op(bio) != REQ_OP_ZONE_APPEND))
1049ae29333fSJohannes Thumshirn 		return 0;
1050ae29333fSJohannes Thumshirn 
1051edd1dbc8SChristoph Hellwig 	if (WARN_ON_ONCE(!bdev_is_zoned(bio->bi_bdev)))
1052ae29333fSJohannes Thumshirn 		return 0;
1053ae29333fSJohannes Thumshirn 
1054ae29333fSJohannes Thumshirn 	return bio_add_hw_page(q, bio, page, len, offset,
1055ae29333fSJohannes Thumshirn 			       queue_max_zone_append_sectors(q), &same_page);
1056ae29333fSJohannes Thumshirn }
1057ae29333fSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_add_zone_append_page);
1058ae29333fSJohannes Thumshirn 
1059ae29333fSJohannes Thumshirn /**
1060551879a4SMing Lei  * __bio_add_page - add page(s) to a bio in a new segment
10610aa69fd3SChristoph Hellwig  * @bio: destination bio
1062551879a4SMing Lei  * @page: start page to add
1063551879a4SMing Lei  * @len: length of the data to add, may cross pages
1064551879a4SMing Lei  * @off: offset of the data relative to @page, may cross pages
10650aa69fd3SChristoph Hellwig  *
10660aa69fd3SChristoph Hellwig  * Add the data at @page + @off to @bio as a new bvec.  The caller must ensure
10670aa69fd3SChristoph Hellwig  * that @bio has space for another bvec.
10680aa69fd3SChristoph Hellwig  */
__bio_add_page(struct bio * bio,struct page * page,unsigned int len,unsigned int off)10690aa69fd3SChristoph Hellwig void __bio_add_page(struct bio *bio, struct page *page,
10700aa69fd3SChristoph Hellwig 		unsigned int len, unsigned int off)
10710aa69fd3SChristoph Hellwig {
10720aa69fd3SChristoph Hellwig 	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
107379d08f89SMing Lei 	WARN_ON_ONCE(bio_full(bio, len));
10740aa69fd3SChristoph Hellwig 
1075d58cdfaeSChristoph Hellwig 	bvec_set_page(&bio->bi_io_vec[bio->bi_vcnt], page, len, off);
10760aa69fd3SChristoph Hellwig 	bio->bi_iter.bi_size += len;
10770aa69fd3SChristoph Hellwig 	bio->bi_vcnt++;
10780aa69fd3SChristoph Hellwig }
10790aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_add_page);
10800aa69fd3SChristoph Hellwig 
10810aa69fd3SChristoph Hellwig /**
1082551879a4SMing Lei  *	bio_add_page	-	attempt to add page(s) to bio
1083f9c78b2bSJens Axboe  *	@bio: destination bio
1084551879a4SMing Lei  *	@page: start page to add
1085551879a4SMing Lei  *	@len: vec entry length, may cross pages
1086551879a4SMing Lei  *	@offset: vec entry offset relative to @page, may cross pages
1087f9c78b2bSJens Axboe  *
1088551879a4SMing Lei  *	Attempt to add page(s) to the bio_vec maplist. This will only fail
1089c66a14d0SKent Overstreet  *	if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
1090f9c78b2bSJens Axboe  */
bio_add_page(struct bio * bio,struct page * page,unsigned int len,unsigned int offset)1091c66a14d0SKent Overstreet int bio_add_page(struct bio *bio, struct page *page,
1092c66a14d0SKent Overstreet 		 unsigned int len, unsigned int offset)
1093f9c78b2bSJens Axboe {
1094ff896738SChristoph Hellwig 	bool same_page = false;
1095ff896738SChristoph Hellwig 
1096939e1a37SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
1097939e1a37SChristoph Hellwig 		return 0;
109861369905SChristoph Hellwig 	if (bio->bi_iter.bi_size > UINT_MAX - len)
109961369905SChristoph Hellwig 		return 0;
1100939e1a37SChristoph Hellwig 
11010eca8b6fSChristoph Hellwig 	if (bio->bi_vcnt > 0 &&
1102858c708dSChristoph Hellwig 	    bvec_try_merge_page(&bio->bi_io_vec[bio->bi_vcnt - 1],
1103858c708dSChristoph Hellwig 				page, len, offset, &same_page)) {
1104858c708dSChristoph Hellwig 		bio->bi_iter.bi_size += len;
11050eca8b6fSChristoph Hellwig 		return len;
1106858c708dSChristoph Hellwig 	}
11070eca8b6fSChristoph Hellwig 
110880232b52SChristoph Hellwig 	if (bio->bi_vcnt >= bio->bi_max_vecs)
1109c66a14d0SKent Overstreet 		return 0;
11100aa69fd3SChristoph Hellwig 	__bio_add_page(bio, page, len, offset);
1111c66a14d0SKent Overstreet 	return len;
1112f9c78b2bSJens Axboe }
1113f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_page);
1114f9c78b2bSJens Axboe 
bio_add_folio_nofail(struct bio * bio,struct folio * folio,size_t len,size_t off)11157a150f1eSJohannes Thumshirn void bio_add_folio_nofail(struct bio *bio, struct folio *folio, size_t len,
11167a150f1eSJohannes Thumshirn 			  size_t off)
11177a150f1eSJohannes Thumshirn {
11187a150f1eSJohannes Thumshirn 	WARN_ON_ONCE(len > UINT_MAX);
11197a150f1eSJohannes Thumshirn 	WARN_ON_ONCE(off > UINT_MAX);
11207a150f1eSJohannes Thumshirn 	__bio_add_page(bio, &folio->page, len, off);
11217a150f1eSJohannes Thumshirn }
11227a150f1eSJohannes Thumshirn 
112385f5a74cSMatthew Wilcox (Oracle) /**
112485f5a74cSMatthew Wilcox (Oracle)  * bio_add_folio - Attempt to add part of a folio to a bio.
112585f5a74cSMatthew Wilcox (Oracle)  * @bio: BIO to add to.
112685f5a74cSMatthew Wilcox (Oracle)  * @folio: Folio to add.
112785f5a74cSMatthew Wilcox (Oracle)  * @len: How many bytes from the folio to add.
112885f5a74cSMatthew Wilcox (Oracle)  * @off: First byte in this folio to add.
112985f5a74cSMatthew Wilcox (Oracle)  *
113085f5a74cSMatthew Wilcox (Oracle)  * Filesystems that use folios can call this function instead of calling
113185f5a74cSMatthew Wilcox (Oracle)  * bio_add_page() for each page in the folio.  If @off is bigger than
113285f5a74cSMatthew Wilcox (Oracle)  * PAGE_SIZE, this function can create a bio_vec that starts in a page
113385f5a74cSMatthew Wilcox (Oracle)  * after the bv_page.  BIOs do not support folios that are 4GiB or larger.
113485f5a74cSMatthew Wilcox (Oracle)  *
113585f5a74cSMatthew Wilcox (Oracle)  * Return: Whether the addition was successful.
113685f5a74cSMatthew Wilcox (Oracle)  */
bio_add_folio(struct bio * bio,struct folio * folio,size_t len,size_t off)113785f5a74cSMatthew Wilcox (Oracle) bool bio_add_folio(struct bio *bio, struct folio *folio, size_t len,
113885f5a74cSMatthew Wilcox (Oracle) 		   size_t off)
113985f5a74cSMatthew Wilcox (Oracle) {
114085f5a74cSMatthew Wilcox (Oracle) 	if (len > UINT_MAX || off > UINT_MAX)
1141455a844dSJiapeng Chong 		return false;
114285f5a74cSMatthew Wilcox (Oracle) 	return bio_add_page(bio, &folio->page, len, off) > 0;
114385f5a74cSMatthew Wilcox (Oracle) }
1144cd57b771SMatthew Wilcox EXPORT_SYMBOL(bio_add_folio);
114585f5a74cSMatthew Wilcox (Oracle) 
__bio_release_pages(struct bio * bio,bool mark_dirty)1146c809084aSPavel Begunkov void __bio_release_pages(struct bio *bio, bool mark_dirty)
11477321ecbfSChristoph Hellwig {
11488955324cSMatthew Wilcox (Oracle) 	struct folio_iter fi;
11497321ecbfSChristoph Hellwig 
11508955324cSMatthew Wilcox (Oracle) 	bio_for_each_folio_all(fi, bio) {
11518955324cSMatthew Wilcox (Oracle) 		struct page *page;
11527d376555STony Battersby 		size_t nr_pages;
11538955324cSMatthew Wilcox (Oracle) 
11548955324cSMatthew Wilcox (Oracle) 		if (mark_dirty) {
11558955324cSMatthew Wilcox (Oracle) 			folio_lock(fi.folio);
11568955324cSMatthew Wilcox (Oracle) 			folio_mark_dirty(fi.folio);
11578955324cSMatthew Wilcox (Oracle) 			folio_unlock(fi.folio);
11588955324cSMatthew Wilcox (Oracle) 		}
11598955324cSMatthew Wilcox (Oracle) 		page = folio_page(fi.folio, fi.offset / PAGE_SIZE);
11607d376555STony Battersby 		nr_pages = (fi.offset + fi.length - 1) / PAGE_SIZE -
11617d376555STony Battersby 			   fi.offset / PAGE_SIZE + 1;
11628955324cSMatthew Wilcox (Oracle) 		do {
11638955324cSMatthew Wilcox (Oracle) 			bio_release_page(bio, page++);
11647d376555STony Battersby 		} while (--nr_pages != 0);
11657321ecbfSChristoph Hellwig 	}
1166d241a95fSChristoph Hellwig }
1167c809084aSPavel Begunkov EXPORT_SYMBOL_GPL(__bio_release_pages);
11687321ecbfSChristoph Hellwig 
bio_iov_bvec_set(struct bio * bio,struct iov_iter * iter)11691bb6b810SPavel Begunkov void bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
11706d0c48aeSJens Axboe {
1171fa5fa8ecSPavel Begunkov 	size_t size = iov_iter_count(iter);
1172fa5fa8ecSPavel Begunkov 
11737a800a20SChristoph Hellwig 	WARN_ON_ONCE(bio->bi_max_vecs);
11746d0c48aeSJens Axboe 
1175fa5fa8ecSPavel Begunkov 	if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
1176fa5fa8ecSPavel Begunkov 		struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1177fa5fa8ecSPavel Begunkov 		size_t max_sectors = queue_max_zone_append_sectors(q);
1178fa5fa8ecSPavel Begunkov 
1179fa5fa8ecSPavel Begunkov 		size = min(size, max_sectors << SECTOR_SHIFT);
1180fa5fa8ecSPavel Begunkov 	}
1181fa5fa8ecSPavel Begunkov 
1182c42bca92SPavel Begunkov 	bio->bi_vcnt = iter->nr_segs;
1183c42bca92SPavel Begunkov 	bio->bi_io_vec = (struct bio_vec *)iter->bvec;
1184c42bca92SPavel Begunkov 	bio->bi_iter.bi_bvec_done = iter->iov_offset;
1185fa5fa8ecSPavel Begunkov 	bio->bi_iter.bi_size = size;
1186977be012SChristoph Hellwig 	bio_set_flag(bio, BIO_CLONED);
11877de55b7dSJohannes Thumshirn }
11886d0c48aeSJens Axboe 
bio_iov_add_page(struct bio * bio,struct page * page,unsigned int len,unsigned int offset)1189c58c0074SKeith Busch static int bio_iov_add_page(struct bio *bio, struct page *page,
1190c58c0074SKeith Busch 		unsigned int len, unsigned int offset)
1191c58c0074SKeith Busch {
1192c58c0074SKeith Busch 	bool same_page = false;
1193c58c0074SKeith Busch 
119461369905SChristoph Hellwig 	if (WARN_ON_ONCE(bio->bi_iter.bi_size > UINT_MAX - len))
119561369905SChristoph Hellwig 		return -EIO;
119661369905SChristoph Hellwig 
11970eca8b6fSChristoph Hellwig 	if (bio->bi_vcnt > 0 &&
1198858c708dSChristoph Hellwig 	    bvec_try_merge_page(&bio->bi_io_vec[bio->bi_vcnt - 1],
1199858c708dSChristoph Hellwig 				page, len, offset, &same_page)) {
1200858c708dSChristoph Hellwig 		bio->bi_iter.bi_size += len;
1201c58c0074SKeith Busch 		if (same_page)
1202a7e689ddSDavid Howells 			bio_release_page(bio, page);
1203c58c0074SKeith Busch 		return 0;
1204c58c0074SKeith Busch 	}
12050eca8b6fSChristoph Hellwig 	__bio_add_page(bio, page, len, offset);
12060eca8b6fSChristoph Hellwig 	return 0;
12070eca8b6fSChristoph Hellwig }
1208c58c0074SKeith Busch 
bio_iov_add_zone_append_page(struct bio * bio,struct page * page,unsigned int len,unsigned int offset)1209c58c0074SKeith Busch static int bio_iov_add_zone_append_page(struct bio *bio, struct page *page,
1210c58c0074SKeith Busch 		unsigned int len, unsigned int offset)
1211c58c0074SKeith Busch {
1212c58c0074SKeith Busch 	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1213c58c0074SKeith Busch 	bool same_page = false;
1214c58c0074SKeith Busch 
1215c58c0074SKeith Busch 	if (bio_add_hw_page(q, bio, page, len, offset,
1216c58c0074SKeith Busch 			queue_max_zone_append_sectors(q), &same_page) != len)
1217c58c0074SKeith Busch 		return -EINVAL;
1218c58c0074SKeith Busch 	if (same_page)
1219a7e689ddSDavid Howells 		bio_release_page(bio, page);
1220c58c0074SKeith Busch 	return 0;
1221c58c0074SKeith Busch }
1222c58c0074SKeith Busch 
1223576ed913SChristoph Hellwig #define PAGE_PTRS_PER_BVEC     (sizeof(struct bio_vec) / sizeof(struct page *))
1224576ed913SChristoph Hellwig 
12252cefe4dbSKent Overstreet /**
122617d51b10SMartin Wilck  * __bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
12272cefe4dbSKent Overstreet  * @bio: bio to add pages to
12282cefe4dbSKent Overstreet  * @iter: iov iterator describing the region to be mapped
12292cefe4dbSKent Overstreet  *
1230a7e689ddSDavid Howells  * Extracts pages from *iter and appends them to @bio's bvec array.  The pages
1231a7e689ddSDavid Howells  * will have to be cleaned up in the way indicated by the BIO_PAGE_PINNED flag.
1232a7e689ddSDavid Howells  * For a multi-segment *iter, this function only adds pages from the next
1233a7e689ddSDavid Howells  * non-empty segment of the iov iterator.
12342cefe4dbSKent Overstreet  */
__bio_iov_iter_get_pages(struct bio * bio,struct iov_iter * iter)123517d51b10SMartin Wilck static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
12362cefe4dbSKent Overstreet {
1237f62e52d1SDavid Howells 	iov_iter_extraction_t extraction_flags = 0;
1238576ed913SChristoph Hellwig 	unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt;
1239576ed913SChristoph Hellwig 	unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt;
12402cefe4dbSKent Overstreet 	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
12412cefe4dbSKent Overstreet 	struct page **pages = (struct page **)bv;
1242576ed913SChristoph Hellwig 	ssize_t size, left;
1243e97424fdSKeith Busch 	unsigned len, i = 0;
1244168145f6SKent Overstreet 	size_t offset;
1245325347d9SKeith Busch 	int ret = 0;
1246576ed913SChristoph Hellwig 
1247576ed913SChristoph Hellwig 	/*
1248576ed913SChristoph Hellwig 	 * Move page array up in the allocated memory for the bio vecs as far as
1249576ed913SChristoph Hellwig 	 * possible so that we can start filling biovecs from the beginning
1250576ed913SChristoph Hellwig 	 * without overwriting the temporary page array.
1251576ed913SChristoph Hellwig 	 */
1252576ed913SChristoph Hellwig 	BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2);
1253576ed913SChristoph Hellwig 	pages += entries_left * (PAGE_PTRS_PER_BVEC - 1);
12542cefe4dbSKent Overstreet 
12555e3e3f2eSLogan Gunthorpe 	if (bio->bi_bdev && blk_queue_pci_p2pdma(bio->bi_bdev->bd_disk->queue))
1256f62e52d1SDavid Howells 		extraction_flags |= ITER_ALLOW_P2PDMA;
12575e3e3f2eSLogan Gunthorpe 
1258b1a000d3SKeith Busch 	/*
1259b1a000d3SKeith Busch 	 * Each segment in the iov is required to be a block size multiple.
1260b1a000d3SKeith Busch 	 * However, we may not be able to get the entire segment if it spans
1261b1a000d3SKeith Busch 	 * more pages than bi_max_vecs allows, so we have to ALIGN_DOWN the
1262b1a000d3SKeith Busch 	 * result to ensure the bio's total size is correct. The remainder of
1263b1a000d3SKeith Busch 	 * the iov data will be picked up in the next bio iteration.
1264b1a000d3SKeith Busch 	 */
1265a7e689ddSDavid Howells 	size = iov_iter_extract_pages(iter, &pages,
12665e3e3f2eSLogan Gunthorpe 				      UINT_MAX - bio->bi_iter.bi_size,
1267a7e689ddSDavid Howells 				      nr_pages, extraction_flags, &offset);
1268480cb846SAl Viro 	if (unlikely(size <= 0))
1269480cb846SAl Viro 		return size ? size : -EFAULT;
1270e97424fdSKeith Busch 
1271480cb846SAl Viro 	nr_pages = DIV_ROUND_UP(offset + size, PAGE_SIZE);
1272480cb846SAl Viro 
1273168145f6SKent Overstreet 	if (bio->bi_bdev) {
1274168145f6SKent Overstreet 		size_t trim = size & (bdev_logical_block_size(bio->bi_bdev) - 1);
1275480cb846SAl Viro 		iov_iter_revert(iter, trim);
1276480cb846SAl Viro 		size -= trim;
1277168145f6SKent Overstreet 	}
1278168145f6SKent Overstreet 
1279480cb846SAl Viro 	if (unlikely(!size)) {
1280480cb846SAl Viro 		ret = -EFAULT;
1281e97424fdSKeith Busch 		goto out;
1282e97424fdSKeith Busch 	}
12832cefe4dbSKent Overstreet 
1284576ed913SChristoph Hellwig 	for (left = size, i = 0; left > 0; left -= len, i++) {
1285576ed913SChristoph Hellwig 		struct page *page = pages[i];
12862cefe4dbSKent Overstreet 
1287576ed913SChristoph Hellwig 		len = min_t(size_t, PAGE_SIZE - offset, left);
128834cdb8c8SKeith Busch 		if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
1289c58c0074SKeith Busch 			ret = bio_iov_add_zone_append_page(bio, page, len,
1290c58c0074SKeith Busch 					offset);
1291e97424fdSKeith Busch 			if (ret)
1292325347d9SKeith Busch 				break;
129334cdb8c8SKeith Busch 		} else
129434cdb8c8SKeith Busch 			bio_iov_add_page(bio, page, len, offset);
129534cdb8c8SKeith Busch 
1296576ed913SChristoph Hellwig 		offset = 0;
12972cefe4dbSKent Overstreet 	}
12982cefe4dbSKent Overstreet 
1299480cb846SAl Viro 	iov_iter_revert(iter, left);
1300e97424fdSKeith Busch out:
1301e97424fdSKeith Busch 	while (i < nr_pages)
1302a7e689ddSDavid Howells 		bio_release_page(bio, pages[i++]);
1303e97424fdSKeith Busch 
1304325347d9SKeith Busch 	return ret;
13052cefe4dbSKent Overstreet }
130617d51b10SMartin Wilck 
130717d51b10SMartin Wilck /**
13086d0c48aeSJens Axboe  * bio_iov_iter_get_pages - add user or kernel pages to a bio
130917d51b10SMartin Wilck  * @bio: bio to add pages to
13106d0c48aeSJens Axboe  * @iter: iov iterator describing the region to be added
131117d51b10SMartin Wilck  *
13126d0c48aeSJens Axboe  * This takes either an iterator pointing to user memory, or one pointing to
13136d0c48aeSJens Axboe  * kernel pages (BVEC iterator). If we're adding user pages, we pin them and
13146d0c48aeSJens Axboe  * map them into the kernel. On IO completion, the caller should put those
1315c42bca92SPavel Begunkov  * pages. For bvec based iterators bio_iov_iter_get_pages() uses the provided
1316c42bca92SPavel Begunkov  * bvecs rather than copying them. Hence anyone issuing kiocb based IO needs
1317c42bca92SPavel Begunkov  * to ensure the bvecs and pages stay referenced until the submitted I/O is
1318c42bca92SPavel Begunkov  * completed by a call to ->ki_complete() or returns with an error other than
1319c42bca92SPavel Begunkov  * -EIOCBQUEUED. The caller needs to check if the bio is flagged BIO_NO_PAGE_REF
1320c42bca92SPavel Begunkov  * on IO completion. If it isn't, then pages should be released.
13216d0c48aeSJens Axboe  *
132217d51b10SMartin Wilck  * The function tries, but does not guarantee, to pin as many pages as
13235cd3ddc1SMauro Carvalho Chehab  * fit into the bio, or are requested in @iter, whatever is smaller. If
13246d0c48aeSJens Axboe  * MM encounters an error pinning the requested pages, it stops. Error
13256d0c48aeSJens Axboe  * is returned only if 0 pages could be pinned.
132617d51b10SMartin Wilck  */
bio_iov_iter_get_pages(struct bio * bio,struct iov_iter * iter)132717d51b10SMartin Wilck int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
132817d51b10SMartin Wilck {
1329c42bca92SPavel Begunkov 	int ret = 0;
133014eacf12SChristoph Hellwig 
1331939e1a37SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
1332939e1a37SChristoph Hellwig 		return -EIO;
1333939e1a37SChristoph Hellwig 
1334c42bca92SPavel Begunkov 	if (iov_iter_is_bvec(iter)) {
1335fa5fa8ecSPavel Begunkov 		bio_iov_bvec_set(bio, iter);
1336fa5fa8ecSPavel Begunkov 		iov_iter_advance(iter, bio->bi_iter.bi_size);
1337fa5fa8ecSPavel Begunkov 		return 0;
133886004515SChristoph Hellwig 	}
133917d51b10SMartin Wilck 
1340a7e689ddSDavid Howells 	if (iov_iter_extract_will_pin(iter))
1341a7e689ddSDavid Howells 		bio_set_flag(bio, BIO_PAGE_PINNED);
134217d51b10SMartin Wilck 	do {
13436d0c48aeSJens Axboe 		ret = __bio_iov_iter_get_pages(bio, iter);
134479d08f89SMing Lei 	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
134517d51b10SMartin Wilck 
134614eacf12SChristoph Hellwig 	return bio->bi_vcnt ? 0 : ret;
134717d51b10SMartin Wilck }
134829b2a3aaSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages);
13492cefe4dbSKent Overstreet 
submit_bio_wait_endio(struct bio * bio)13504246a0b6SChristoph Hellwig static void submit_bio_wait_endio(struct bio *bio)
1351f9c78b2bSJens Axboe {
135265e53aabSChristoph Hellwig 	complete(bio->bi_private);
1353f9c78b2bSJens Axboe }
1354f9c78b2bSJens Axboe 
1355f9c78b2bSJens Axboe /**
1356f9c78b2bSJens Axboe  * submit_bio_wait - submit a bio, and wait until it completes
1357f9c78b2bSJens Axboe  * @bio: The &struct bio which describes the I/O
1358f9c78b2bSJens Axboe  *
1359f9c78b2bSJens Axboe  * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
1360f9c78b2bSJens Axboe  * bio_endio() on failure.
13613d289d68SJan Kara  *
13623d289d68SJan Kara  * WARNING: Unlike to how submit_bio() is usually used, this function does not
13633d289d68SJan Kara  * result in bio reference to be consumed. The caller must drop the reference
13643d289d68SJan Kara  * on his own.
1365f9c78b2bSJens Axboe  */
submit_bio_wait(struct bio * bio)13664e49ea4aSMike Christie int submit_bio_wait(struct bio *bio)
1367f9c78b2bSJens Axboe {
1368309dca30SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK_MAP(done,
1369309dca30SChristoph Hellwig 			bio->bi_bdev->bd_disk->lockdep_map);
1370de6a78b6SMing Lei 	unsigned long hang_check;
1371f9c78b2bSJens Axboe 
137265e53aabSChristoph Hellwig 	bio->bi_private = &done;
1373f9c78b2bSJens Axboe 	bio->bi_end_io = submit_bio_wait_endio;
13741eff9d32SJens Axboe 	bio->bi_opf |= REQ_SYNC;
13754e49ea4aSMike Christie 	submit_bio(bio);
1376de6a78b6SMing Lei 
1377de6a78b6SMing Lei 	/* Prevent hang_check timer from firing at us during very long I/O */
1378de6a78b6SMing Lei 	hang_check = sysctl_hung_task_timeout_secs;
1379de6a78b6SMing Lei 	if (hang_check)
1380de6a78b6SMing Lei 		while (!wait_for_completion_io_timeout(&done,
1381de6a78b6SMing Lei 					hang_check * (HZ/2)))
1382de6a78b6SMing Lei 			;
1383de6a78b6SMing Lei 	else
138465e53aabSChristoph Hellwig 		wait_for_completion_io(&done);
1385f9c78b2bSJens Axboe 
138665e53aabSChristoph Hellwig 	return blk_status_to_errno(bio->bi_status);
1387f9c78b2bSJens Axboe }
1388f9c78b2bSJens Axboe EXPORT_SYMBOL(submit_bio_wait);
1389f9c78b2bSJens Axboe 
__bio_advance(struct bio * bio,unsigned bytes)1390d4aa57a1SJens Axboe void __bio_advance(struct bio *bio, unsigned bytes)
1391f9c78b2bSJens Axboe {
1392f9c78b2bSJens Axboe 	if (bio_integrity(bio))
1393f9c78b2bSJens Axboe 		bio_integrity_advance(bio, bytes);
1394f9c78b2bSJens Axboe 
1395a892c8d5SSatya Tangirala 	bio_crypt_advance(bio, bytes);
1396f9c78b2bSJens Axboe 	bio_advance_iter(bio, &bio->bi_iter, bytes);
1397f9c78b2bSJens Axboe }
1398d4aa57a1SJens Axboe EXPORT_SYMBOL(__bio_advance);
1399f9c78b2bSJens Axboe 
bio_copy_data_iter(struct bio * dst,struct bvec_iter * dst_iter,struct bio * src,struct bvec_iter * src_iter)1400ee4b4e22SJens Axboe void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
1401ee4b4e22SJens Axboe 			struct bio *src, struct bvec_iter *src_iter)
1402ee4b4e22SJens Axboe {
1403ee4b4e22SJens Axboe 	while (src_iter->bi_size && dst_iter->bi_size) {
1404ee4b4e22SJens Axboe 		struct bio_vec src_bv = bio_iter_iovec(src, *src_iter);
1405ee4b4e22SJens Axboe 		struct bio_vec dst_bv = bio_iter_iovec(dst, *dst_iter);
1406ee4b4e22SJens Axboe 		unsigned int bytes = min(src_bv.bv_len, dst_bv.bv_len);
1407ee4b4e22SJens Axboe 		void *src_buf = bvec_kmap_local(&src_bv);
1408ee4b4e22SJens Axboe 		void *dst_buf = bvec_kmap_local(&dst_bv);
1409ee4b4e22SJens Axboe 
1410ee4b4e22SJens Axboe 		memcpy(dst_buf, src_buf, bytes);
1411ee4b4e22SJens Axboe 
1412ee4b4e22SJens Axboe 		kunmap_local(dst_buf);
1413ee4b4e22SJens Axboe 		kunmap_local(src_buf);
1414ee4b4e22SJens Axboe 
1415ee4b4e22SJens Axboe 		bio_advance_iter_single(src, src_iter, bytes);
1416ee4b4e22SJens Axboe 		bio_advance_iter_single(dst, dst_iter, bytes);
1417ee4b4e22SJens Axboe 	}
1418ee4b4e22SJens Axboe }
1419ee4b4e22SJens Axboe EXPORT_SYMBOL(bio_copy_data_iter);
1420ee4b4e22SJens Axboe 
142145db54d5SKent Overstreet /**
142245db54d5SKent Overstreet  * bio_copy_data - copy contents of data buffers from one bio to another
142345db54d5SKent Overstreet  * @src: source bio
142445db54d5SKent Overstreet  * @dst: destination bio
142545db54d5SKent Overstreet  *
142645db54d5SKent Overstreet  * Stops when it reaches the end of either @src or @dst - that is, copies
142745db54d5SKent Overstreet  * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
142845db54d5SKent Overstreet  */
bio_copy_data(struct bio * dst,struct bio * src)142945db54d5SKent Overstreet void bio_copy_data(struct bio *dst, struct bio *src)
143045db54d5SKent Overstreet {
143145db54d5SKent Overstreet 	struct bvec_iter src_iter = src->bi_iter;
143245db54d5SKent Overstreet 	struct bvec_iter dst_iter = dst->bi_iter;
143345db54d5SKent Overstreet 
1434ee4b4e22SJens Axboe 	bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
143545db54d5SKent Overstreet }
143645db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data);
143745db54d5SKent Overstreet 
bio_free_pages(struct bio * bio)1438491221f8SGuoqing Jiang void bio_free_pages(struct bio *bio)
14391dfa0f68SChristoph Hellwig {
14401dfa0f68SChristoph Hellwig 	struct bio_vec *bvec;
14416dc4f100SMing Lei 	struct bvec_iter_all iter_all;
14421dfa0f68SChristoph Hellwig 
14432b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all)
14441dfa0f68SChristoph Hellwig 		__free_page(bvec->bv_page);
14451dfa0f68SChristoph Hellwig }
1446491221f8SGuoqing Jiang EXPORT_SYMBOL(bio_free_pages);
14471dfa0f68SChristoph Hellwig 
1448f9c78b2bSJens Axboe /*
1449f9c78b2bSJens Axboe  * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
1450f9c78b2bSJens Axboe  * for performing direct-IO in BIOs.
1451f9c78b2bSJens Axboe  *
14528955324cSMatthew Wilcox (Oracle)  * The problem is that we cannot run folio_mark_dirty() from interrupt context
1453f9c78b2bSJens Axboe  * because the required locks are not interrupt-safe.  So what we can do is to
1454f9c78b2bSJens Axboe  * mark the pages dirty _before_ performing IO.  And in interrupt context,
1455f9c78b2bSJens Axboe  * check that the pages are still dirty.   If so, fine.  If not, redirty them
1456f9c78b2bSJens Axboe  * in process context.
1457f9c78b2bSJens Axboe  *
1458f9c78b2bSJens Axboe  * Note that this code is very hard to test under normal circumstances because
1459f9c78b2bSJens Axboe  * direct-io pins the pages with get_user_pages().  This makes
1460f9c78b2bSJens Axboe  * is_page_cache_freeable return false, and the VM will not clean the pages.
1461f9c78b2bSJens Axboe  * But other code (eg, flusher threads) could clean the pages if they are mapped
1462f9c78b2bSJens Axboe  * pagecache.
1463f9c78b2bSJens Axboe  *
1464f9c78b2bSJens Axboe  * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
1465f9c78b2bSJens Axboe  * deferred bio dirtying paths.
1466f9c78b2bSJens Axboe  */
1467f9c78b2bSJens Axboe 
1468f9c78b2bSJens Axboe /*
1469f9c78b2bSJens Axboe  * bio_set_pages_dirty() will mark all the bio's pages as dirty.
1470f9c78b2bSJens Axboe  */
bio_set_pages_dirty(struct bio * bio)1471f9c78b2bSJens Axboe void bio_set_pages_dirty(struct bio *bio)
1472f9c78b2bSJens Axboe {
14738955324cSMatthew Wilcox (Oracle) 	struct folio_iter fi;
1474f9c78b2bSJens Axboe 
14758955324cSMatthew Wilcox (Oracle) 	bio_for_each_folio_all(fi, bio) {
14768955324cSMatthew Wilcox (Oracle) 		folio_lock(fi.folio);
14778955324cSMatthew Wilcox (Oracle) 		folio_mark_dirty(fi.folio);
14788955324cSMatthew Wilcox (Oracle) 		folio_unlock(fi.folio);
1479f9c78b2bSJens Axboe 	}
1480f9c78b2bSJens Axboe }
14817ba37927SKent Overstreet EXPORT_SYMBOL_GPL(bio_set_pages_dirty);
1482f9c78b2bSJens Axboe 
1483f9c78b2bSJens Axboe /*
1484f9c78b2bSJens Axboe  * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
1485f9c78b2bSJens Axboe  * If they are, then fine.  If, however, some pages are clean then they must
1486f9c78b2bSJens Axboe  * have been written out during the direct-IO read.  So we take another ref on
148724d5493fSChristoph Hellwig  * the BIO and re-dirty the pages in process context.
1488f9c78b2bSJens Axboe  *
1489f9c78b2bSJens Axboe  * It is expected that bio_check_pages_dirty() will wholly own the BIO from
1490fd363244SDavid Howells  * here on.  It will unpin each page and will run one bio_put() against the
1491fd363244SDavid Howells  * BIO.
1492f9c78b2bSJens Axboe  */
1493f9c78b2bSJens Axboe 
1494f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work);
1495f9c78b2bSJens Axboe 
1496f9c78b2bSJens Axboe static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
1497f9c78b2bSJens Axboe static DEFINE_SPINLOCK(bio_dirty_lock);
1498f9c78b2bSJens Axboe static struct bio *bio_dirty_list;
1499f9c78b2bSJens Axboe 
1500f9c78b2bSJens Axboe /*
1501f9c78b2bSJens Axboe  * This runs in process context
1502f9c78b2bSJens Axboe  */
bio_dirty_fn(struct work_struct * work)1503f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work)
1504f9c78b2bSJens Axboe {
150524d5493fSChristoph Hellwig 	struct bio *bio, *next;
1506f9c78b2bSJens Axboe 
150724d5493fSChristoph Hellwig 	spin_lock_irq(&bio_dirty_lock);
150824d5493fSChristoph Hellwig 	next = bio_dirty_list;
1509f9c78b2bSJens Axboe 	bio_dirty_list = NULL;
151024d5493fSChristoph Hellwig 	spin_unlock_irq(&bio_dirty_lock);
1511f9c78b2bSJens Axboe 
151224d5493fSChristoph Hellwig 	while ((bio = next) != NULL) {
151324d5493fSChristoph Hellwig 		next = bio->bi_private;
1514f9c78b2bSJens Axboe 
1515d241a95fSChristoph Hellwig 		bio_release_pages(bio, true);
1516f9c78b2bSJens Axboe 		bio_put(bio);
1517f9c78b2bSJens Axboe 	}
1518f9c78b2bSJens Axboe }
1519f9c78b2bSJens Axboe 
bio_check_pages_dirty(struct bio * bio)1520f9c78b2bSJens Axboe void bio_check_pages_dirty(struct bio *bio)
1521f9c78b2bSJens Axboe {
15228955324cSMatthew Wilcox (Oracle) 	struct folio_iter fi;
152324d5493fSChristoph Hellwig 	unsigned long flags;
1524f9c78b2bSJens Axboe 
15258955324cSMatthew Wilcox (Oracle) 	bio_for_each_folio_all(fi, bio) {
15268955324cSMatthew Wilcox (Oracle) 		if (!folio_test_dirty(fi.folio))
152724d5493fSChristoph Hellwig 			goto defer;
1528f9c78b2bSJens Axboe 	}
1529f9c78b2bSJens Axboe 
1530d241a95fSChristoph Hellwig 	bio_release_pages(bio, false);
153124d5493fSChristoph Hellwig 	bio_put(bio);
153224d5493fSChristoph Hellwig 	return;
153324d5493fSChristoph Hellwig defer:
1534f9c78b2bSJens Axboe 	spin_lock_irqsave(&bio_dirty_lock, flags);
1535f9c78b2bSJens Axboe 	bio->bi_private = bio_dirty_list;
1536f9c78b2bSJens Axboe 	bio_dirty_list = bio;
1537f9c78b2bSJens Axboe 	spin_unlock_irqrestore(&bio_dirty_lock, flags);
1538f9c78b2bSJens Axboe 	schedule_work(&bio_dirty_work);
1539f9c78b2bSJens Axboe }
15407ba37927SKent Overstreet EXPORT_SYMBOL_GPL(bio_check_pages_dirty);
1541f9c78b2bSJens Axboe 
bio_remaining_done(struct bio * bio)1542c4cf5261SJens Axboe static inline bool bio_remaining_done(struct bio *bio)
1543c4cf5261SJens Axboe {
1544c4cf5261SJens Axboe 	/*
1545c4cf5261SJens Axboe 	 * If we're not chaining, then ->__bi_remaining is always 1 and
1546c4cf5261SJens Axboe 	 * we always end io on the first invocation.
1547c4cf5261SJens Axboe 	 */
1548c4cf5261SJens Axboe 	if (!bio_flagged(bio, BIO_CHAIN))
1549c4cf5261SJens Axboe 		return true;
1550c4cf5261SJens Axboe 
1551c4cf5261SJens Axboe 	BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
1552c4cf5261SJens Axboe 
1553326e1dbbSMike Snitzer 	if (atomic_dec_and_test(&bio->__bi_remaining)) {
1554b7c44ed9SJens Axboe 		bio_clear_flag(bio, BIO_CHAIN);
1555c4cf5261SJens Axboe 		return true;
1556326e1dbbSMike Snitzer 	}
1557c4cf5261SJens Axboe 
1558c4cf5261SJens Axboe 	return false;
1559c4cf5261SJens Axboe }
1560c4cf5261SJens Axboe 
1561f9c78b2bSJens Axboe /**
1562f9c78b2bSJens Axboe  * bio_endio - end I/O on a bio
1563f9c78b2bSJens Axboe  * @bio:	bio
1564f9c78b2bSJens Axboe  *
1565f9c78b2bSJens Axboe  * Description:
15664246a0b6SChristoph Hellwig  *   bio_endio() will end I/O on the whole bio. bio_endio() is the preferred
15674246a0b6SChristoph Hellwig  *   way to end I/O on a bio. No one should call bi_end_io() directly on a
15684246a0b6SChristoph Hellwig  *   bio unless they own it and thus know that it has an end_io function.
1569fbbaf700SNeilBrown  *
1570fbbaf700SNeilBrown  *   bio_endio() can be called several times on a bio that has been chained
1571fbbaf700SNeilBrown  *   using bio_chain().  The ->bi_end_io() function will only be called the
157260b6a7e6SEdward Hsieh  *   last time.
1573f9c78b2bSJens Axboe  **/
bio_endio(struct bio * bio)15744246a0b6SChristoph Hellwig void bio_endio(struct bio *bio)
1575f9c78b2bSJens Axboe {
1576ba8c6967SChristoph Hellwig again:
15772b885517SChristoph Hellwig 	if (!bio_remaining_done(bio))
1578ba8c6967SChristoph Hellwig 		return;
15797c20f116SChristoph Hellwig 	if (!bio_integrity_endio(bio))
15807c20f116SChristoph Hellwig 		return;
1581f9c78b2bSJens Axboe 
1582aa1b46dcSTejun Heo 	rq_qos_done_bio(bio);
158367b42d0bSJosef Bacik 
158460b6a7e6SEdward Hsieh 	if (bio->bi_bdev && bio_flagged(bio, BIO_TRACE_COMPLETION)) {
15853caee463SPavel Begunkov 		trace_block_bio_complete(bdev_get_queue(bio->bi_bdev), bio);
158660b6a7e6SEdward Hsieh 		bio_clear_flag(bio, BIO_TRACE_COMPLETION);
158760b6a7e6SEdward Hsieh 	}
158860b6a7e6SEdward Hsieh 
1589f9c78b2bSJens Axboe 	/*
1590ba8c6967SChristoph Hellwig 	 * Need to have a real endio function for chained bios, otherwise
1591ba8c6967SChristoph Hellwig 	 * various corner cases will break (like stacking block devices that
1592ba8c6967SChristoph Hellwig 	 * save/restore bi_end_io) - however, we want to avoid unbounded
1593ba8c6967SChristoph Hellwig 	 * recursion and blowing the stack. Tail call optimization would
1594ba8c6967SChristoph Hellwig 	 * handle this, but compiling with frame pointers also disables
1595ba8c6967SChristoph Hellwig 	 * gcc's sibling call optimization.
1596f9c78b2bSJens Axboe 	 */
1597f9c78b2bSJens Axboe 	if (bio->bi_end_io == bio_chain_endio) {
159838f8baaeSChristoph Hellwig 		bio = __bio_chain_endio(bio);
1599ba8c6967SChristoph Hellwig 		goto again;
1600ba8c6967SChristoph Hellwig 	}
1601ba8c6967SChristoph Hellwig 
16029e234eeaSShaohua Li 	blk_throtl_bio_endio(bio);
1603b222dd2fSShaohua Li 	/* release cgroup info */
1604b222dd2fSShaohua Li 	bio_uninit(bio);
1605f9c78b2bSJens Axboe 	if (bio->bi_end_io)
16064246a0b6SChristoph Hellwig 		bio->bi_end_io(bio);
1607f9c78b2bSJens Axboe }
1608f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_endio);
1609f9c78b2bSJens Axboe 
1610f9c78b2bSJens Axboe /**
1611f9c78b2bSJens Axboe  * bio_split - split a bio
1612f9c78b2bSJens Axboe  * @bio:	bio to split
1613f9c78b2bSJens Axboe  * @sectors:	number of sectors to split from the front of @bio
1614f9c78b2bSJens Axboe  * @gfp:	gfp mask
1615f9c78b2bSJens Axboe  * @bs:		bio set to allocate from
1616f9c78b2bSJens Axboe  *
1617f9c78b2bSJens Axboe  * Allocates and returns a new bio which represents @sectors from the start of
1618f9c78b2bSJens Axboe  * @bio, and updates @bio to represent the remaining sectors.
1619f9c78b2bSJens Axboe  *
1620f3f5da62SMartin K. Petersen  * Unless this is a discard request the newly allocated bio will point
1621dad77584SBart Van Assche  * to @bio's bi_io_vec. It is the caller's responsibility to ensure that
1622dad77584SBart Van Assche  * neither @bio nor @bs are freed before the split bio.
1623f9c78b2bSJens Axboe  */
bio_split(struct bio * bio,int sectors,gfp_t gfp,struct bio_set * bs)1624f9c78b2bSJens Axboe struct bio *bio_split(struct bio *bio, int sectors,
1625f9c78b2bSJens Axboe 		      gfp_t gfp, struct bio_set *bs)
1626f9c78b2bSJens Axboe {
1627f341a4d3SMikulas Patocka 	struct bio *split;
1628f9c78b2bSJens Axboe 
1629f9c78b2bSJens Axboe 	BUG_ON(sectors <= 0);
1630f9c78b2bSJens Axboe 	BUG_ON(sectors >= bio_sectors(bio));
1631f9c78b2bSJens Axboe 
16320512a75bSKeith Busch 	/* Zone append commands cannot be split */
16330512a75bSKeith Busch 	if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
16340512a75bSKeith Busch 		return NULL;
16350512a75bSKeith Busch 
1636abfc426dSChristoph Hellwig 	split = bio_alloc_clone(bio->bi_bdev, bio, gfp, bs);
1637f9c78b2bSJens Axboe 	if (!split)
1638f9c78b2bSJens Axboe 		return NULL;
1639f9c78b2bSJens Axboe 
1640f9c78b2bSJens Axboe 	split->bi_iter.bi_size = sectors << 9;
1641f9c78b2bSJens Axboe 
1642f9c78b2bSJens Axboe 	if (bio_integrity(split))
1643fbd08e76SDmitry Monakhov 		bio_integrity_trim(split);
1644f9c78b2bSJens Axboe 
1645f9c78b2bSJens Axboe 	bio_advance(bio, split->bi_iter.bi_size);
1646f9c78b2bSJens Axboe 
1647fbbaf700SNeilBrown 	if (bio_flagged(bio, BIO_TRACE_COMPLETION))
164820d59023SGoldwyn Rodrigues 		bio_set_flag(split, BIO_TRACE_COMPLETION);
1649fbbaf700SNeilBrown 
1650f9c78b2bSJens Axboe 	return split;
1651f9c78b2bSJens Axboe }
1652f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_split);
1653f9c78b2bSJens Axboe 
1654f9c78b2bSJens Axboe /**
1655f9c78b2bSJens Axboe  * bio_trim - trim a bio
1656f9c78b2bSJens Axboe  * @bio:	bio to trim
1657f9c78b2bSJens Axboe  * @offset:	number of sectors to trim from the front of @bio
1658f9c78b2bSJens Axboe  * @size:	size we want to trim @bio to, in sectors
1659e83502caSChaitanya Kulkarni  *
1660e83502caSChaitanya Kulkarni  * This function is typically used for bios that are cloned and submitted
1661e83502caSChaitanya Kulkarni  * to the underlying device in parts.
1662f9c78b2bSJens Axboe  */
bio_trim(struct bio * bio,sector_t offset,sector_t size)1663e83502caSChaitanya Kulkarni void bio_trim(struct bio *bio, sector_t offset, sector_t size)
1664f9c78b2bSJens Axboe {
1665e83502caSChaitanya Kulkarni 	if (WARN_ON_ONCE(offset > BIO_MAX_SECTORS || size > BIO_MAX_SECTORS ||
16668535c018SMing Lei 			 offset + size > bio_sectors(bio)))
1667e83502caSChaitanya Kulkarni 		return;
1668f9c78b2bSJens Axboe 
1669f9c78b2bSJens Axboe 	size <<= 9;
1670f9c78b2bSJens Axboe 	if (offset == 0 && size == bio->bi_iter.bi_size)
1671f9c78b2bSJens Axboe 		return;
1672f9c78b2bSJens Axboe 
1673f9c78b2bSJens Axboe 	bio_advance(bio, offset << 9);
1674f9c78b2bSJens Axboe 	bio->bi_iter.bi_size = size;
1675376a78abSDmitry Monakhov 
1676376a78abSDmitry Monakhov 	if (bio_integrity(bio))
1677fbd08e76SDmitry Monakhov 		bio_integrity_trim(bio);
1678f9c78b2bSJens Axboe }
1679f9c78b2bSJens Axboe EXPORT_SYMBOL_GPL(bio_trim);
1680f9c78b2bSJens Axboe 
1681f9c78b2bSJens Axboe /*
1682f9c78b2bSJens Axboe  * create memory pools for biovec's in a bio_set.
1683f9c78b2bSJens Axboe  * use the global biovec slabs created for general use.
1684f9c78b2bSJens Axboe  */
biovec_init_pool(mempool_t * pool,int pool_entries)16858aa6ba2fSKent Overstreet int biovec_init_pool(mempool_t *pool, int pool_entries)
1686f9c78b2bSJens Axboe {
16877a800a20SChristoph Hellwig 	struct biovec_slab *bp = bvec_slabs + ARRAY_SIZE(bvec_slabs) - 1;
1688f9c78b2bSJens Axboe 
16898aa6ba2fSKent Overstreet 	return mempool_init_slab_pool(pool, pool_entries, bp->slab);
1690f9c78b2bSJens Axboe }
1691f9c78b2bSJens Axboe 
1692917a38c7SKent Overstreet /*
1693917a38c7SKent Overstreet  * bioset_exit - exit a bioset initialized with bioset_init()
1694917a38c7SKent Overstreet  *
1695917a38c7SKent Overstreet  * May be called on a zeroed but uninitialized bioset (i.e. allocated with
1696917a38c7SKent Overstreet  * kzalloc()).
1697917a38c7SKent Overstreet  */
bioset_exit(struct bio_set * bs)1698917a38c7SKent Overstreet void bioset_exit(struct bio_set *bs)
1699f9c78b2bSJens Axboe {
1700be4d234dSJens Axboe 	bio_alloc_cache_destroy(bs);
1701f9c78b2bSJens Axboe 	if (bs->rescue_workqueue)
1702f9c78b2bSJens Axboe 		destroy_workqueue(bs->rescue_workqueue);
1703917a38c7SKent Overstreet 	bs->rescue_workqueue = NULL;
1704f9c78b2bSJens Axboe 
17058aa6ba2fSKent Overstreet 	mempool_exit(&bs->bio_pool);
17068aa6ba2fSKent Overstreet 	mempool_exit(&bs->bvec_pool);
1707f9c78b2bSJens Axboe 
1708f9c78b2bSJens Axboe 	bioset_integrity_free(bs);
1709917a38c7SKent Overstreet 	if (bs->bio_slab)
1710f9c78b2bSJens Axboe 		bio_put_slab(bs);
1711917a38c7SKent Overstreet 	bs->bio_slab = NULL;
1712917a38c7SKent Overstreet }
1713917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_exit);
1714f9c78b2bSJens Axboe 
1715011067b0SNeilBrown /**
1716917a38c7SKent Overstreet  * bioset_init - Initialize a bio_set
1717dad08527SKent Overstreet  * @bs:		pool to initialize
1718917a38c7SKent Overstreet  * @pool_size:	Number of bio and bio_vecs to cache in the mempool
1719917a38c7SKent Overstreet  * @front_pad:	Number of bytes to allocate in front of the returned bio
1720917a38c7SKent Overstreet  * @flags:	Flags to modify behavior, currently %BIOSET_NEED_BVECS
1721917a38c7SKent Overstreet  *              and %BIOSET_NEED_RESCUER
1722917a38c7SKent Overstreet  *
1723dad08527SKent Overstreet  * Description:
1724dad08527SKent Overstreet  *    Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
1725dad08527SKent Overstreet  *    to ask for a number of bytes to be allocated in front of the bio.
1726dad08527SKent Overstreet  *    Front pad allocation is useful for embedding the bio inside
1727dad08527SKent Overstreet  *    another structure, to avoid allocating extra data to go with the bio.
1728dad08527SKent Overstreet  *    Note that the bio must be embedded at the END of that structure always,
1729dad08527SKent Overstreet  *    or things will break badly.
1730dad08527SKent Overstreet  *    If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated
1731abfc426dSChristoph Hellwig  *    for allocating iovecs.  This pool is not needed e.g. for bio_init_clone().
1732abfc426dSChristoph Hellwig  *    If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used
1733abfc426dSChristoph Hellwig  *    to dispatch queued requests when the mempool runs out of space.
1734dad08527SKent Overstreet  *
1735917a38c7SKent Overstreet  */
bioset_init(struct bio_set * bs,unsigned int pool_size,unsigned int front_pad,int flags)1736917a38c7SKent Overstreet int bioset_init(struct bio_set *bs,
1737917a38c7SKent Overstreet 		unsigned int pool_size,
1738917a38c7SKent Overstreet 		unsigned int front_pad,
1739917a38c7SKent Overstreet 		int flags)
1740917a38c7SKent Overstreet {
1741917a38c7SKent Overstreet 	bs->front_pad = front_pad;
17429f180e31SMing Lei 	if (flags & BIOSET_NEED_BVECS)
17439f180e31SMing Lei 		bs->back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
17449f180e31SMing Lei 	else
17459f180e31SMing Lei 		bs->back_pad = 0;
1746917a38c7SKent Overstreet 
1747917a38c7SKent Overstreet 	spin_lock_init(&bs->rescue_lock);
1748917a38c7SKent Overstreet 	bio_list_init(&bs->rescue_list);
1749917a38c7SKent Overstreet 	INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1750917a38c7SKent Overstreet 
175149d1ec85SMing Lei 	bs->bio_slab = bio_find_or_create_slab(bs);
1752917a38c7SKent Overstreet 	if (!bs->bio_slab)
1753917a38c7SKent Overstreet 		return -ENOMEM;
1754917a38c7SKent Overstreet 
1755917a38c7SKent Overstreet 	if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab))
1756917a38c7SKent Overstreet 		goto bad;
1757917a38c7SKent Overstreet 
1758917a38c7SKent Overstreet 	if ((flags & BIOSET_NEED_BVECS) &&
1759917a38c7SKent Overstreet 	    biovec_init_pool(&bs->bvec_pool, pool_size))
1760917a38c7SKent Overstreet 		goto bad;
1761917a38c7SKent Overstreet 
1762be4d234dSJens Axboe 	if (flags & BIOSET_NEED_RESCUER) {
1763be4d234dSJens Axboe 		bs->rescue_workqueue = alloc_workqueue("bioset",
1764be4d234dSJens Axboe 							WQ_MEM_RECLAIM, 0);
1765917a38c7SKent Overstreet 		if (!bs->rescue_workqueue)
1766917a38c7SKent Overstreet 			goto bad;
1767be4d234dSJens Axboe 	}
1768be4d234dSJens Axboe 	if (flags & BIOSET_PERCPU_CACHE) {
1769be4d234dSJens Axboe 		bs->cache = alloc_percpu(struct bio_alloc_cache);
1770be4d234dSJens Axboe 		if (!bs->cache)
1771be4d234dSJens Axboe 			goto bad;
1772be4d234dSJens Axboe 		cpuhp_state_add_instance_nocalls(CPUHP_BIO_DEAD, &bs->cpuhp_dead);
1773be4d234dSJens Axboe 	}
1774917a38c7SKent Overstreet 
1775917a38c7SKent Overstreet 	return 0;
1776917a38c7SKent Overstreet bad:
1777917a38c7SKent Overstreet 	bioset_exit(bs);
1778917a38c7SKent Overstreet 	return -ENOMEM;
1779917a38c7SKent Overstreet }
1780917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_init);
1781917a38c7SKent Overstreet 
init_bio(void)1782de76fd89SChristoph Hellwig static int __init init_bio(void)
1783f9c78b2bSJens Axboe {
1784f9c78b2bSJens Axboe 	int i;
1785f9c78b2bSJens Axboe 
1786a3df2e45SJens Axboe 	BUILD_BUG_ON(BIO_FLAG_LAST > 8 * sizeof_field(struct bio, bi_flags));
1787a3df2e45SJens Axboe 
1788f9c78b2bSJens Axboe 	bio_integrity_init();
1789de76fd89SChristoph Hellwig 
1790de76fd89SChristoph Hellwig 	for (i = 0; i < ARRAY_SIZE(bvec_slabs); i++) {
1791f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + i;
1792f9c78b2bSJens Axboe 
1793de76fd89SChristoph Hellwig 		bvs->slab = kmem_cache_create(bvs->name,
1794de76fd89SChristoph Hellwig 				bvs->nr_vecs * sizeof(struct bio_vec), 0,
1795f9c78b2bSJens Axboe 				SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
1796f9c78b2bSJens Axboe 	}
1797f9c78b2bSJens Axboe 
1798be4d234dSJens Axboe 	cpuhp_setup_state_multi(CPUHP_BIO_DEAD, "block/bio:dead", NULL,
1799be4d234dSJens Axboe 					bio_cpu_dead);
1800be4d234dSJens Axboe 
180112c5b70cSJens Axboe 	if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0,
180212c5b70cSJens Axboe 			BIOSET_NEED_BVECS | BIOSET_PERCPU_CACHE))
1803f9c78b2bSJens Axboe 		panic("bio: can't allocate bios\n");
1804f9c78b2bSJens Axboe 
1805f4f8154aSKent Overstreet 	if (bioset_integrity_create(&fs_bio_set, BIO_POOL_SIZE))
1806f9c78b2bSJens Axboe 		panic("bio: can't create integrity pool\n");
1807f9c78b2bSJens Axboe 
1808f9c78b2bSJens Axboe 	return 0;
1809f9c78b2bSJens Axboe }
1810f9c78b2bSJens Axboe subsys_initcall(init_bio);
1811