xref: /openbmc/linux/block/bio.c (revision 86004515)
18c16567dSChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
2f9c78b2bSJens Axboe /*
3f9c78b2bSJens Axboe  * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk>
4f9c78b2bSJens Axboe  */
5f9c78b2bSJens Axboe #include <linux/mm.h>
6f9c78b2bSJens Axboe #include <linux/swap.h>
7f9c78b2bSJens Axboe #include <linux/bio.h>
8f9c78b2bSJens Axboe #include <linux/blkdev.h>
9f9c78b2bSJens Axboe #include <linux/uio.h>
10f9c78b2bSJens Axboe #include <linux/iocontext.h>
11f9c78b2bSJens Axboe #include <linux/slab.h>
12f9c78b2bSJens Axboe #include <linux/init.h>
13f9c78b2bSJens Axboe #include <linux/kernel.h>
14f9c78b2bSJens Axboe #include <linux/export.h>
15f9c78b2bSJens Axboe #include <linux/mempool.h>
16f9c78b2bSJens Axboe #include <linux/workqueue.h>
17f9c78b2bSJens Axboe #include <linux/cgroup.h>
1808e18eabSJosef Bacik #include <linux/blk-cgroup.h>
19b4c5875dSDamien Le Moal #include <linux/highmem.h>
20de6a78b6SMing Lei #include <linux/sched/sysctl.h>
21a892c8d5SSatya Tangirala #include <linux/blk-crypto.h>
2249d1ec85SMing Lei #include <linux/xarray.h>
23f9c78b2bSJens Axboe 
24f9c78b2bSJens Axboe #include <trace/events/block.h>
259e234eeaSShaohua Li #include "blk.h"
2667b42d0bSJosef Bacik #include "blk-rq-qos.h"
27f9c78b2bSJens Axboe 
28de76fd89SChristoph Hellwig static struct biovec_slab {
296ac0b715SChristoph Hellwig 	int nr_vecs;
306ac0b715SChristoph Hellwig 	char *name;
316ac0b715SChristoph Hellwig 	struct kmem_cache *slab;
32de76fd89SChristoph Hellwig } bvec_slabs[] __read_mostly = {
33de76fd89SChristoph Hellwig 	{ .nr_vecs = 16, .name = "biovec-16" },
34de76fd89SChristoph Hellwig 	{ .nr_vecs = 64, .name = "biovec-64" },
35de76fd89SChristoph Hellwig 	{ .nr_vecs = 128, .name = "biovec-128" },
36de76fd89SChristoph Hellwig 	{ .nr_vecs = BIO_MAX_PAGES, .name = "biovec-max" },
376ac0b715SChristoph Hellwig };
386ac0b715SChristoph Hellwig 
39f9c78b2bSJens Axboe /*
40f9c78b2bSJens Axboe  * fs_bio_set is the bio_set containing bio and iovec memory pools used by
41f9c78b2bSJens Axboe  * IO code that does not need private memory pools.
42f9c78b2bSJens Axboe  */
43f4f8154aSKent Overstreet struct bio_set fs_bio_set;
44f9c78b2bSJens Axboe EXPORT_SYMBOL(fs_bio_set);
45f9c78b2bSJens Axboe 
46f9c78b2bSJens Axboe /*
47f9c78b2bSJens Axboe  * Our slab pool management
48f9c78b2bSJens Axboe  */
49f9c78b2bSJens Axboe struct bio_slab {
50f9c78b2bSJens Axboe 	struct kmem_cache *slab;
51f9c78b2bSJens Axboe 	unsigned int slab_ref;
52f9c78b2bSJens Axboe 	unsigned int slab_size;
53f9c78b2bSJens Axboe 	char name[8];
54f9c78b2bSJens Axboe };
55f9c78b2bSJens Axboe static DEFINE_MUTEX(bio_slab_lock);
5649d1ec85SMing Lei static DEFINE_XARRAY(bio_slabs);
57f9c78b2bSJens Axboe 
5849d1ec85SMing Lei static struct bio_slab *create_bio_slab(unsigned int size)
59f9c78b2bSJens Axboe {
6049d1ec85SMing Lei 	struct bio_slab *bslab = kzalloc(sizeof(*bslab), GFP_KERNEL);
6149d1ec85SMing Lei 
6249d1ec85SMing Lei 	if (!bslab)
6349d1ec85SMing Lei 		return NULL;
6449d1ec85SMing Lei 
6549d1ec85SMing Lei 	snprintf(bslab->name, sizeof(bslab->name), "bio-%d", size);
6649d1ec85SMing Lei 	bslab->slab = kmem_cache_create(bslab->name, size,
6749d1ec85SMing Lei 			ARCH_KMALLOC_MINALIGN, SLAB_HWCACHE_ALIGN, NULL);
6849d1ec85SMing Lei 	if (!bslab->slab)
6949d1ec85SMing Lei 		goto fail_alloc_slab;
7049d1ec85SMing Lei 
7149d1ec85SMing Lei 	bslab->slab_ref = 1;
7249d1ec85SMing Lei 	bslab->slab_size = size;
7349d1ec85SMing Lei 
7449d1ec85SMing Lei 	if (!xa_err(xa_store(&bio_slabs, size, bslab, GFP_KERNEL)))
7549d1ec85SMing Lei 		return bslab;
7649d1ec85SMing Lei 
7749d1ec85SMing Lei 	kmem_cache_destroy(bslab->slab);
7849d1ec85SMing Lei 
7949d1ec85SMing Lei fail_alloc_slab:
8049d1ec85SMing Lei 	kfree(bslab);
8149d1ec85SMing Lei 	return NULL;
8249d1ec85SMing Lei }
8349d1ec85SMing Lei 
8449d1ec85SMing Lei static inline unsigned int bs_bio_slab_size(struct bio_set *bs)
8549d1ec85SMing Lei {
869f180e31SMing Lei 	return bs->front_pad + sizeof(struct bio) + bs->back_pad;
8749d1ec85SMing Lei }
8849d1ec85SMing Lei 
8949d1ec85SMing Lei static struct kmem_cache *bio_find_or_create_slab(struct bio_set *bs)
9049d1ec85SMing Lei {
9149d1ec85SMing Lei 	unsigned int size = bs_bio_slab_size(bs);
9249d1ec85SMing Lei 	struct bio_slab *bslab;
93f9c78b2bSJens Axboe 
94f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
9549d1ec85SMing Lei 	bslab = xa_load(&bio_slabs, size);
9649d1ec85SMing Lei 	if (bslab)
97f9c78b2bSJens Axboe 		bslab->slab_ref++;
9849d1ec85SMing Lei 	else
9949d1ec85SMing Lei 		bslab = create_bio_slab(size);
100f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
10149d1ec85SMing Lei 
10249d1ec85SMing Lei 	if (bslab)
10349d1ec85SMing Lei 		return bslab->slab;
10449d1ec85SMing Lei 	return NULL;
105f9c78b2bSJens Axboe }
106f9c78b2bSJens Axboe 
107f9c78b2bSJens Axboe static void bio_put_slab(struct bio_set *bs)
108f9c78b2bSJens Axboe {
109f9c78b2bSJens Axboe 	struct bio_slab *bslab = NULL;
11049d1ec85SMing Lei 	unsigned int slab_size = bs_bio_slab_size(bs);
111f9c78b2bSJens Axboe 
112f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
113f9c78b2bSJens Axboe 
11449d1ec85SMing Lei 	bslab = xa_load(&bio_slabs, slab_size);
115f9c78b2bSJens Axboe 	if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
116f9c78b2bSJens Axboe 		goto out;
117f9c78b2bSJens Axboe 
11849d1ec85SMing Lei 	WARN_ON_ONCE(bslab->slab != bs->bio_slab);
11949d1ec85SMing Lei 
120f9c78b2bSJens Axboe 	WARN_ON(!bslab->slab_ref);
121f9c78b2bSJens Axboe 
122f9c78b2bSJens Axboe 	if (--bslab->slab_ref)
123f9c78b2bSJens Axboe 		goto out;
124f9c78b2bSJens Axboe 
12549d1ec85SMing Lei 	xa_erase(&bio_slabs, slab_size);
12649d1ec85SMing Lei 
127f9c78b2bSJens Axboe 	kmem_cache_destroy(bslab->slab);
12849d1ec85SMing Lei 	kfree(bslab);
129f9c78b2bSJens Axboe 
130f9c78b2bSJens Axboe out:
131f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
132f9c78b2bSJens Axboe }
133f9c78b2bSJens Axboe 
134f9c78b2bSJens Axboe unsigned int bvec_nr_vecs(unsigned short idx)
135f9c78b2bSJens Axboe {
136d6c02a9bSGreg Edwards 	return bvec_slabs[--idx].nr_vecs;
137f9c78b2bSJens Axboe }
138f9c78b2bSJens Axboe 
139f9c78b2bSJens Axboe void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
140f9c78b2bSJens Axboe {
141ed996a52SChristoph Hellwig 	if (!idx)
142ed996a52SChristoph Hellwig 		return;
143ed996a52SChristoph Hellwig 	idx--;
144f9c78b2bSJens Axboe 
145ed996a52SChristoph Hellwig 	BIO_BUG_ON(idx >= BVEC_POOL_NR);
146ed996a52SChristoph Hellwig 
147ed996a52SChristoph Hellwig 	if (idx == BVEC_POOL_MAX) {
148f9c78b2bSJens Axboe 		mempool_free(bv, pool);
149ed996a52SChristoph Hellwig 	} else {
150f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + idx;
151f9c78b2bSJens Axboe 
152f9c78b2bSJens Axboe 		kmem_cache_free(bvs->slab, bv);
153f9c78b2bSJens Axboe 	}
154f9c78b2bSJens Axboe }
155f9c78b2bSJens Axboe 
156f2c3eb9bSChristoph Hellwig /*
157f2c3eb9bSChristoph Hellwig  * Make the first allocation restricted and don't dump info on allocation
158f2c3eb9bSChristoph Hellwig  * failures, since we'll fall back to the mempool in case of failure.
159f2c3eb9bSChristoph Hellwig  */
160f2c3eb9bSChristoph Hellwig static inline gfp_t bvec_alloc_gfp(gfp_t gfp)
161f2c3eb9bSChristoph Hellwig {
162f2c3eb9bSChristoph Hellwig 	return (gfp & ~(__GFP_DIRECT_RECLAIM | __GFP_IO)) |
163f2c3eb9bSChristoph Hellwig 		__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
164f2c3eb9bSChristoph Hellwig }
165f2c3eb9bSChristoph Hellwig 
166f9c78b2bSJens Axboe struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
167f9c78b2bSJens Axboe 			   mempool_t *pool)
168f9c78b2bSJens Axboe {
169f9c78b2bSJens Axboe 	/*
170f9c78b2bSJens Axboe 	 * see comment near bvec_array define!
171f9c78b2bSJens Axboe 	 */
172f9c78b2bSJens Axboe 	switch (nr) {
173de76fd89SChristoph Hellwig 	/* smaller bios use inline vecs */
174f9c78b2bSJens Axboe 	case 5 ... 16:
175f9c78b2bSJens Axboe 		*idx = 2;
176f9c78b2bSJens Axboe 		break;
177f9c78b2bSJens Axboe 	case 17 ... 64:
178f9c78b2bSJens Axboe 		*idx = 3;
179f9c78b2bSJens Axboe 		break;
180f9c78b2bSJens Axboe 	case 65 ... 128:
181f9c78b2bSJens Axboe 		*idx = 4;
182f9c78b2bSJens Axboe 		break;
183f9c78b2bSJens Axboe 	case 129 ... BIO_MAX_PAGES:
184f9c78b2bSJens Axboe 		*idx = 5;
185f9c78b2bSJens Axboe 		break;
186f9c78b2bSJens Axboe 	default:
187f9c78b2bSJens Axboe 		return NULL;
188f9c78b2bSJens Axboe 	}
189f9c78b2bSJens Axboe 
190f9c78b2bSJens Axboe 	/*
191f007a3d6SChristoph Hellwig 	 * Try a slab allocation first for all smaller allocations.  If that
192f007a3d6SChristoph Hellwig 	 * fails and __GFP_DIRECT_RECLAIM is set retry with the mempool.
193f007a3d6SChristoph Hellwig 	 * The mempool is sized to handle up to BIO_MAX_PAGES entries.
194f9c78b2bSJens Axboe 	 */
195f007a3d6SChristoph Hellwig 	if (*idx < BVEC_POOL_MAX) {
196f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + *idx;
197f007a3d6SChristoph Hellwig 		struct bio_vec *bvl;
198f9c78b2bSJens Axboe 
199f2c3eb9bSChristoph Hellwig 		bvl = kmem_cache_alloc(bvs->slab, bvec_alloc_gfp(gfp_mask));
200f007a3d6SChristoph Hellwig 		if (likely(bvl) || !(gfp_mask & __GFP_DIRECT_RECLAIM)) {
201f007a3d6SChristoph Hellwig 			(*idx)++;
202f007a3d6SChristoph Hellwig 			return bvl;
203f9c78b2bSJens Axboe 		}
204f007a3d6SChristoph Hellwig 		*idx = BVEC_POOL_MAX;
205f9c78b2bSJens Axboe 	}
206f9c78b2bSJens Axboe 
207ed996a52SChristoph Hellwig 	(*idx)++;
208f007a3d6SChristoph Hellwig 	return mempool_alloc(pool, gfp_mask);
209f9c78b2bSJens Axboe }
210f9c78b2bSJens Axboe 
2119ae3b3f5SJens Axboe void bio_uninit(struct bio *bio)
212f9c78b2bSJens Axboe {
213db9819c7SChristoph Hellwig #ifdef CONFIG_BLK_CGROUP
214db9819c7SChristoph Hellwig 	if (bio->bi_blkg) {
215db9819c7SChristoph Hellwig 		blkg_put(bio->bi_blkg);
216db9819c7SChristoph Hellwig 		bio->bi_blkg = NULL;
217db9819c7SChristoph Hellwig 	}
218db9819c7SChristoph Hellwig #endif
219ece841abSJustin Tee 	if (bio_integrity(bio))
220ece841abSJustin Tee 		bio_integrity_free(bio);
221a892c8d5SSatya Tangirala 
222a892c8d5SSatya Tangirala 	bio_crypt_free_ctx(bio);
223f9c78b2bSJens Axboe }
2249ae3b3f5SJens Axboe EXPORT_SYMBOL(bio_uninit);
225f9c78b2bSJens Axboe 
226f9c78b2bSJens Axboe static void bio_free(struct bio *bio)
227f9c78b2bSJens Axboe {
228f9c78b2bSJens Axboe 	struct bio_set *bs = bio->bi_pool;
229f9c78b2bSJens Axboe 	void *p;
230f9c78b2bSJens Axboe 
2319ae3b3f5SJens Axboe 	bio_uninit(bio);
232f9c78b2bSJens Axboe 
233f9c78b2bSJens Axboe 	if (bs) {
2348aa6ba2fSKent Overstreet 		bvec_free(&bs->bvec_pool, bio->bi_io_vec, BVEC_POOL_IDX(bio));
235f9c78b2bSJens Axboe 
236f9c78b2bSJens Axboe 		/*
237f9c78b2bSJens Axboe 		 * If we have front padding, adjust the bio pointer before freeing
238f9c78b2bSJens Axboe 		 */
239f9c78b2bSJens Axboe 		p = bio;
240f9c78b2bSJens Axboe 		p -= bs->front_pad;
241f9c78b2bSJens Axboe 
2428aa6ba2fSKent Overstreet 		mempool_free(p, &bs->bio_pool);
243f9c78b2bSJens Axboe 	} else {
244f9c78b2bSJens Axboe 		/* Bio was allocated by bio_kmalloc() */
245f9c78b2bSJens Axboe 		kfree(bio);
246f9c78b2bSJens Axboe 	}
247f9c78b2bSJens Axboe }
248f9c78b2bSJens Axboe 
2499ae3b3f5SJens Axboe /*
2509ae3b3f5SJens Axboe  * Users of this function have their own bio allocation. Subsequently,
2519ae3b3f5SJens Axboe  * they must remember to pair any call to bio_init() with bio_uninit()
2529ae3b3f5SJens Axboe  * when IO has completed, or when the bio is released.
2539ae3b3f5SJens Axboe  */
2543a83f467SMing Lei void bio_init(struct bio *bio, struct bio_vec *table,
2553a83f467SMing Lei 	      unsigned short max_vecs)
256f9c78b2bSJens Axboe {
257f9c78b2bSJens Axboe 	memset(bio, 0, sizeof(*bio));
258c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
259dac56212SJens Axboe 	atomic_set(&bio->__bi_cnt, 1);
2603a83f467SMing Lei 
2613a83f467SMing Lei 	bio->bi_io_vec = table;
2623a83f467SMing Lei 	bio->bi_max_vecs = max_vecs;
263f9c78b2bSJens Axboe }
264f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_init);
265f9c78b2bSJens Axboe 
266f9c78b2bSJens Axboe /**
267f9c78b2bSJens Axboe  * bio_reset - reinitialize a bio
268f9c78b2bSJens Axboe  * @bio:	bio to reset
269f9c78b2bSJens Axboe  *
270f9c78b2bSJens Axboe  * Description:
271f9c78b2bSJens Axboe  *   After calling bio_reset(), @bio will be in the same state as a freshly
272f9c78b2bSJens Axboe  *   allocated bio returned bio bio_alloc_bioset() - the only fields that are
273f9c78b2bSJens Axboe  *   preserved are the ones that are initialized by bio_alloc_bioset(). See
274f9c78b2bSJens Axboe  *   comment in struct bio.
275f9c78b2bSJens Axboe  */
276f9c78b2bSJens Axboe void bio_reset(struct bio *bio)
277f9c78b2bSJens Axboe {
278f9c78b2bSJens Axboe 	unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS);
279f9c78b2bSJens Axboe 
2809ae3b3f5SJens Axboe 	bio_uninit(bio);
281f9c78b2bSJens Axboe 
282f9c78b2bSJens Axboe 	memset(bio, 0, BIO_RESET_BYTES);
2834246a0b6SChristoph Hellwig 	bio->bi_flags = flags;
284c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
285f9c78b2bSJens Axboe }
286f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_reset);
287f9c78b2bSJens Axboe 
28838f8baaeSChristoph Hellwig static struct bio *__bio_chain_endio(struct bio *bio)
289f9c78b2bSJens Axboe {
2904246a0b6SChristoph Hellwig 	struct bio *parent = bio->bi_private;
2914246a0b6SChristoph Hellwig 
2924e4cbee9SChristoph Hellwig 	if (!parent->bi_status)
2934e4cbee9SChristoph Hellwig 		parent->bi_status = bio->bi_status;
294f9c78b2bSJens Axboe 	bio_put(bio);
29538f8baaeSChristoph Hellwig 	return parent;
29638f8baaeSChristoph Hellwig }
29738f8baaeSChristoph Hellwig 
29838f8baaeSChristoph Hellwig static void bio_chain_endio(struct bio *bio)
29938f8baaeSChristoph Hellwig {
30038f8baaeSChristoph Hellwig 	bio_endio(__bio_chain_endio(bio));
301f9c78b2bSJens Axboe }
302f9c78b2bSJens Axboe 
303f9c78b2bSJens Axboe /**
304f9c78b2bSJens Axboe  * bio_chain - chain bio completions
305f9c78b2bSJens Axboe  * @bio: the target bio
3065b874af6SMauro Carvalho Chehab  * @parent: the parent bio of @bio
307f9c78b2bSJens Axboe  *
308f9c78b2bSJens Axboe  * The caller won't have a bi_end_io called when @bio completes - instead,
309f9c78b2bSJens Axboe  * @parent's bi_end_io won't be called until both @parent and @bio have
310f9c78b2bSJens Axboe  * completed; the chained bio will also be freed when it completes.
311f9c78b2bSJens Axboe  *
312f9c78b2bSJens Axboe  * The caller must not set bi_private or bi_end_io in @bio.
313f9c78b2bSJens Axboe  */
314f9c78b2bSJens Axboe void bio_chain(struct bio *bio, struct bio *parent)
315f9c78b2bSJens Axboe {
316f9c78b2bSJens Axboe 	BUG_ON(bio->bi_private || bio->bi_end_io);
317f9c78b2bSJens Axboe 
318f9c78b2bSJens Axboe 	bio->bi_private = parent;
319f9c78b2bSJens Axboe 	bio->bi_end_io	= bio_chain_endio;
320c4cf5261SJens Axboe 	bio_inc_remaining(parent);
321f9c78b2bSJens Axboe }
322f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_chain);
323f9c78b2bSJens Axboe 
324f9c78b2bSJens Axboe static void bio_alloc_rescue(struct work_struct *work)
325f9c78b2bSJens Axboe {
326f9c78b2bSJens Axboe 	struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
327f9c78b2bSJens Axboe 	struct bio *bio;
328f9c78b2bSJens Axboe 
329f9c78b2bSJens Axboe 	while (1) {
330f9c78b2bSJens Axboe 		spin_lock(&bs->rescue_lock);
331f9c78b2bSJens Axboe 		bio = bio_list_pop(&bs->rescue_list);
332f9c78b2bSJens Axboe 		spin_unlock(&bs->rescue_lock);
333f9c78b2bSJens Axboe 
334f9c78b2bSJens Axboe 		if (!bio)
335f9c78b2bSJens Axboe 			break;
336f9c78b2bSJens Axboe 
337ed00aabdSChristoph Hellwig 		submit_bio_noacct(bio);
338f9c78b2bSJens Axboe 	}
339f9c78b2bSJens Axboe }
340f9c78b2bSJens Axboe 
341f9c78b2bSJens Axboe static void punt_bios_to_rescuer(struct bio_set *bs)
342f9c78b2bSJens Axboe {
343f9c78b2bSJens Axboe 	struct bio_list punt, nopunt;
344f9c78b2bSJens Axboe 	struct bio *bio;
345f9c78b2bSJens Axboe 
34647e0fb46SNeilBrown 	if (WARN_ON_ONCE(!bs->rescue_workqueue))
34747e0fb46SNeilBrown 		return;
348f9c78b2bSJens Axboe 	/*
349f9c78b2bSJens Axboe 	 * In order to guarantee forward progress we must punt only bios that
350f9c78b2bSJens Axboe 	 * were allocated from this bio_set; otherwise, if there was a bio on
351f9c78b2bSJens Axboe 	 * there for a stacking driver higher up in the stack, processing it
352f9c78b2bSJens Axboe 	 * could require allocating bios from this bio_set, and doing that from
353f9c78b2bSJens Axboe 	 * our own rescuer would be bad.
354f9c78b2bSJens Axboe 	 *
355f9c78b2bSJens Axboe 	 * Since bio lists are singly linked, pop them all instead of trying to
356f9c78b2bSJens Axboe 	 * remove from the middle of the list:
357f9c78b2bSJens Axboe 	 */
358f9c78b2bSJens Axboe 
359f9c78b2bSJens Axboe 	bio_list_init(&punt);
360f9c78b2bSJens Axboe 	bio_list_init(&nopunt);
361f9c78b2bSJens Axboe 
362f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[0])))
363f9c78b2bSJens Axboe 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
364f5fe1b51SNeilBrown 	current->bio_list[0] = nopunt;
365f9c78b2bSJens Axboe 
366f5fe1b51SNeilBrown 	bio_list_init(&nopunt);
367f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[1])))
368f5fe1b51SNeilBrown 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
369f5fe1b51SNeilBrown 	current->bio_list[1] = nopunt;
370f9c78b2bSJens Axboe 
371f9c78b2bSJens Axboe 	spin_lock(&bs->rescue_lock);
372f9c78b2bSJens Axboe 	bio_list_merge(&bs->rescue_list, &punt);
373f9c78b2bSJens Axboe 	spin_unlock(&bs->rescue_lock);
374f9c78b2bSJens Axboe 
375f9c78b2bSJens Axboe 	queue_work(bs->rescue_workqueue, &bs->rescue_work);
376f9c78b2bSJens Axboe }
377f9c78b2bSJens Axboe 
378f9c78b2bSJens Axboe /**
379f9c78b2bSJens Axboe  * bio_alloc_bioset - allocate a bio for I/O
380519c8e9fSRandy Dunlap  * @gfp_mask:   the GFP_* mask given to the slab allocator
381f9c78b2bSJens Axboe  * @nr_iovecs:	number of iovecs to pre-allocate
382f9c78b2bSJens Axboe  * @bs:		the bio_set to allocate from.
383f9c78b2bSJens Axboe  *
3843175199aSChristoph Hellwig  * Allocate a bio from the mempools in @bs.
385f9c78b2bSJens Axboe  *
3863175199aSChristoph Hellwig  * If %__GFP_DIRECT_RECLAIM is set then bio_alloc will always be able to
3873175199aSChristoph Hellwig  * allocate a bio.  This is due to the mempool guarantees.  To make this work,
3883175199aSChristoph Hellwig  * callers must never allocate more than 1 bio at a time from the general pool.
3893175199aSChristoph Hellwig  * Callers that need to allocate more than 1 bio must always submit the
3903175199aSChristoph Hellwig  * previously allocated bio for IO before attempting to allocate a new one.
3913175199aSChristoph Hellwig  * Failure to do so can cause deadlocks under memory pressure.
392f9c78b2bSJens Axboe  *
3933175199aSChristoph Hellwig  * Note that when running under submit_bio_noacct() (i.e. any block driver),
3943175199aSChristoph Hellwig  * bios are not submitted until after you return - see the code in
395ed00aabdSChristoph Hellwig  * submit_bio_noacct() that converts recursion into iteration, to prevent
396f9c78b2bSJens Axboe  * stack overflows.
397f9c78b2bSJens Axboe  *
3983175199aSChristoph Hellwig  * This would normally mean allocating multiple bios under submit_bio_noacct()
3993175199aSChristoph Hellwig  * would be susceptible to deadlocks, but we have
400f9c78b2bSJens Axboe  * deadlock avoidance code that resubmits any blocked bios from a rescuer
401f9c78b2bSJens Axboe  * thread.
402f9c78b2bSJens Axboe  *
403f9c78b2bSJens Axboe  * However, we do not guarantee forward progress for allocations from other
404f9c78b2bSJens Axboe  * mempools. Doing multiple allocations from the same mempool under
405ed00aabdSChristoph Hellwig  * submit_bio_noacct() should be avoided - instead, use bio_set's front_pad
406f9c78b2bSJens Axboe  * for per bio allocations.
407f9c78b2bSJens Axboe  *
4083175199aSChristoph Hellwig  * Returns: Pointer to new bio on success, NULL on failure.
409f9c78b2bSJens Axboe  */
4100f2e6ab8SChristoph Hellwig struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned short nr_iovecs,
4117a88fa19SDan Carpenter 			     struct bio_set *bs)
412f9c78b2bSJens Axboe {
413f9c78b2bSJens Axboe 	gfp_t saved_gfp = gfp_mask;
414f9c78b2bSJens Axboe 	struct bio *bio;
415f9c78b2bSJens Axboe 	void *p;
416f9c78b2bSJens Axboe 
4173175199aSChristoph Hellwig 	/* should not use nobvec bioset for nr_iovecs > 0 */
4183175199aSChristoph Hellwig 	if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && nr_iovecs > 0))
419f9c78b2bSJens Axboe 		return NULL;
420f9c78b2bSJens Axboe 
421f9c78b2bSJens Axboe 	/*
4223175199aSChristoph Hellwig 	 * submit_bio_noacct() converts recursion to iteration; this means if
4233175199aSChristoph Hellwig 	 * we're running beneath it, any bios we allocate and submit will not be
4243175199aSChristoph Hellwig 	 * submitted (and thus freed) until after we return.
425f9c78b2bSJens Axboe 	 *
4263175199aSChristoph Hellwig 	 * This exposes us to a potential deadlock if we allocate multiple bios
4273175199aSChristoph Hellwig 	 * from the same bio_set() while running underneath submit_bio_noacct().
4283175199aSChristoph Hellwig 	 * If we were to allocate multiple bios (say a stacking block driver
4293175199aSChristoph Hellwig 	 * that was splitting bios), we would deadlock if we exhausted the
4303175199aSChristoph Hellwig 	 * mempool's reserve.
431f9c78b2bSJens Axboe 	 *
432f9c78b2bSJens Axboe 	 * We solve this, and guarantee forward progress, with a rescuer
4333175199aSChristoph Hellwig 	 * workqueue per bio_set. If we go to allocate and there are bios on
4343175199aSChristoph Hellwig 	 * current->bio_list, we first try the allocation without
4353175199aSChristoph Hellwig 	 * __GFP_DIRECT_RECLAIM; if that fails, we punt those bios we would be
4363175199aSChristoph Hellwig 	 * blocking to the rescuer workqueue before we retry with the original
4373175199aSChristoph Hellwig 	 * gfp_flags.
438f9c78b2bSJens Axboe 	 */
439f5fe1b51SNeilBrown 	if (current->bio_list &&
440f5fe1b51SNeilBrown 	    (!bio_list_empty(&current->bio_list[0]) ||
44147e0fb46SNeilBrown 	     !bio_list_empty(&current->bio_list[1])) &&
44247e0fb46SNeilBrown 	    bs->rescue_workqueue)
443d0164adcSMel Gorman 		gfp_mask &= ~__GFP_DIRECT_RECLAIM;
444f9c78b2bSJens Axboe 
4458aa6ba2fSKent Overstreet 	p = mempool_alloc(&bs->bio_pool, gfp_mask);
446f9c78b2bSJens Axboe 	if (!p && gfp_mask != saved_gfp) {
447f9c78b2bSJens Axboe 		punt_bios_to_rescuer(bs);
448f9c78b2bSJens Axboe 		gfp_mask = saved_gfp;
4498aa6ba2fSKent Overstreet 		p = mempool_alloc(&bs->bio_pool, gfp_mask);
450f9c78b2bSJens Axboe 	}
451f9c78b2bSJens Axboe 	if (unlikely(!p))
452f9c78b2bSJens Axboe 		return NULL;
453f9c78b2bSJens Axboe 
4543175199aSChristoph Hellwig 	bio = p + bs->front_pad;
4553175199aSChristoph Hellwig 	if (nr_iovecs > BIO_INLINE_VECS) {
456ed996a52SChristoph Hellwig 		unsigned long idx = 0;
4573175199aSChristoph Hellwig 		struct bio_vec *bvl = NULL;
458ed996a52SChristoph Hellwig 
4598aa6ba2fSKent Overstreet 		bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool);
460f9c78b2bSJens Axboe 		if (!bvl && gfp_mask != saved_gfp) {
461f9c78b2bSJens Axboe 			punt_bios_to_rescuer(bs);
462f9c78b2bSJens Axboe 			gfp_mask = saved_gfp;
4633175199aSChristoph Hellwig 			bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx,
4643175199aSChristoph Hellwig 					 &bs->bvec_pool);
465f9c78b2bSJens Axboe 		}
466f9c78b2bSJens Axboe 
467f9c78b2bSJens Axboe 		if (unlikely(!bvl))
468f9c78b2bSJens Axboe 			goto err_free;
469f9c78b2bSJens Axboe 
4703175199aSChristoph Hellwig 		bio_init(bio, bvl, bvec_nr_vecs(idx));
4718358c28aSMing Lei 		bio->bi_flags |= idx << BVEC_POOL_OFFSET;
472f9c78b2bSJens Axboe 	} else if (nr_iovecs) {
4733175199aSChristoph Hellwig 		bio_init(bio, bio->bi_inline_vecs, BIO_INLINE_VECS);
4743175199aSChristoph Hellwig 	} else {
4753175199aSChristoph Hellwig 		bio_init(bio, NULL, 0);
476f9c78b2bSJens Axboe 	}
477f9c78b2bSJens Axboe 
478f9c78b2bSJens Axboe 	bio->bi_pool = bs;
479f9c78b2bSJens Axboe 	return bio;
480f9c78b2bSJens Axboe 
481f9c78b2bSJens Axboe err_free:
4828aa6ba2fSKent Overstreet 	mempool_free(p, &bs->bio_pool);
483f9c78b2bSJens Axboe 	return NULL;
484f9c78b2bSJens Axboe }
485f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_bioset);
486f9c78b2bSJens Axboe 
4873175199aSChristoph Hellwig /**
4883175199aSChristoph Hellwig  * bio_kmalloc - kmalloc a bio for I/O
4893175199aSChristoph Hellwig  * @gfp_mask:   the GFP_* mask given to the slab allocator
4903175199aSChristoph Hellwig  * @nr_iovecs:	number of iovecs to pre-allocate
4913175199aSChristoph Hellwig  *
4923175199aSChristoph Hellwig  * Use kmalloc to allocate and initialize a bio.
4933175199aSChristoph Hellwig  *
4943175199aSChristoph Hellwig  * Returns: Pointer to new bio on success, NULL on failure.
4953175199aSChristoph Hellwig  */
4960f2e6ab8SChristoph Hellwig struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned short nr_iovecs)
4973175199aSChristoph Hellwig {
4983175199aSChristoph Hellwig 	struct bio *bio;
4993175199aSChristoph Hellwig 
5003175199aSChristoph Hellwig 	if (nr_iovecs > UIO_MAXIOV)
5013175199aSChristoph Hellwig 		return NULL;
5023175199aSChristoph Hellwig 
5033175199aSChristoph Hellwig 	bio = kmalloc(struct_size(bio, bi_inline_vecs, nr_iovecs), gfp_mask);
5043175199aSChristoph Hellwig 	if (unlikely(!bio))
5053175199aSChristoph Hellwig 		return NULL;
5063175199aSChristoph Hellwig 	bio_init(bio, nr_iovecs ? bio->bi_inline_vecs : NULL, nr_iovecs);
5073175199aSChristoph Hellwig 	bio->bi_pool = NULL;
5083175199aSChristoph Hellwig 	return bio;
5093175199aSChristoph Hellwig }
5103175199aSChristoph Hellwig EXPORT_SYMBOL(bio_kmalloc);
5113175199aSChristoph Hellwig 
51238a72dacSKent Overstreet void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start)
513f9c78b2bSJens Axboe {
514f9c78b2bSJens Axboe 	unsigned long flags;
515f9c78b2bSJens Axboe 	struct bio_vec bv;
516f9c78b2bSJens Axboe 	struct bvec_iter iter;
517f9c78b2bSJens Axboe 
51838a72dacSKent Overstreet 	__bio_for_each_segment(bv, bio, iter, start) {
519f9c78b2bSJens Axboe 		char *data = bvec_kmap_irq(&bv, &flags);
520f9c78b2bSJens Axboe 		memset(data, 0, bv.bv_len);
521f9c78b2bSJens Axboe 		flush_dcache_page(bv.bv_page);
522f9c78b2bSJens Axboe 		bvec_kunmap_irq(data, &flags);
523f9c78b2bSJens Axboe 	}
524f9c78b2bSJens Axboe }
52538a72dacSKent Overstreet EXPORT_SYMBOL(zero_fill_bio_iter);
526f9c78b2bSJens Axboe 
52783c9c547SMing Lei /**
52883c9c547SMing Lei  * bio_truncate - truncate the bio to small size of @new_size
52983c9c547SMing Lei  * @bio:	the bio to be truncated
53083c9c547SMing Lei  * @new_size:	new size for truncating the bio
53183c9c547SMing Lei  *
53283c9c547SMing Lei  * Description:
53383c9c547SMing Lei  *   Truncate the bio to new size of @new_size. If bio_op(bio) is
53483c9c547SMing Lei  *   REQ_OP_READ, zero the truncated part. This function should only
53583c9c547SMing Lei  *   be used for handling corner cases, such as bio eod.
53683c9c547SMing Lei  */
53785a8ce62SMing Lei void bio_truncate(struct bio *bio, unsigned new_size)
53885a8ce62SMing Lei {
53985a8ce62SMing Lei 	struct bio_vec bv;
54085a8ce62SMing Lei 	struct bvec_iter iter;
54185a8ce62SMing Lei 	unsigned int done = 0;
54285a8ce62SMing Lei 	bool truncated = false;
54385a8ce62SMing Lei 
54485a8ce62SMing Lei 	if (new_size >= bio->bi_iter.bi_size)
54585a8ce62SMing Lei 		return;
54685a8ce62SMing Lei 
54783c9c547SMing Lei 	if (bio_op(bio) != REQ_OP_READ)
54885a8ce62SMing Lei 		goto exit;
54985a8ce62SMing Lei 
55085a8ce62SMing Lei 	bio_for_each_segment(bv, bio, iter) {
55185a8ce62SMing Lei 		if (done + bv.bv_len > new_size) {
55285a8ce62SMing Lei 			unsigned offset;
55385a8ce62SMing Lei 
55485a8ce62SMing Lei 			if (!truncated)
55585a8ce62SMing Lei 				offset = new_size - done;
55685a8ce62SMing Lei 			else
55785a8ce62SMing Lei 				offset = 0;
55885a8ce62SMing Lei 			zero_user(bv.bv_page, offset, bv.bv_len - offset);
55985a8ce62SMing Lei 			truncated = true;
56085a8ce62SMing Lei 		}
56185a8ce62SMing Lei 		done += bv.bv_len;
56285a8ce62SMing Lei 	}
56385a8ce62SMing Lei 
56485a8ce62SMing Lei  exit:
56585a8ce62SMing Lei 	/*
56685a8ce62SMing Lei 	 * Don't touch bvec table here and make it really immutable, since
56785a8ce62SMing Lei 	 * fs bio user has to retrieve all pages via bio_for_each_segment_all
56885a8ce62SMing Lei 	 * in its .end_bio() callback.
56985a8ce62SMing Lei 	 *
57085a8ce62SMing Lei 	 * It is enough to truncate bio by updating .bi_size since we can make
57185a8ce62SMing Lei 	 * correct bvec with the updated .bi_size for drivers.
57285a8ce62SMing Lei 	 */
57385a8ce62SMing Lei 	bio->bi_iter.bi_size = new_size;
57485a8ce62SMing Lei }
57585a8ce62SMing Lei 
576f9c78b2bSJens Axboe /**
57729125ed6SChristoph Hellwig  * guard_bio_eod - truncate a BIO to fit the block device
57829125ed6SChristoph Hellwig  * @bio:	bio to truncate
57929125ed6SChristoph Hellwig  *
58029125ed6SChristoph Hellwig  * This allows us to do IO even on the odd last sectors of a device, even if the
58129125ed6SChristoph Hellwig  * block size is some multiple of the physical sector size.
58229125ed6SChristoph Hellwig  *
58329125ed6SChristoph Hellwig  * We'll just truncate the bio to the size of the device, and clear the end of
58429125ed6SChristoph Hellwig  * the buffer head manually.  Truly out-of-range accesses will turn into actual
58529125ed6SChristoph Hellwig  * I/O errors, this only handles the "we need to be able to do I/O at the final
58629125ed6SChristoph Hellwig  * sector" case.
58729125ed6SChristoph Hellwig  */
58829125ed6SChristoph Hellwig void guard_bio_eod(struct bio *bio)
58929125ed6SChristoph Hellwig {
590309dca30SChristoph Hellwig 	sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
59129125ed6SChristoph Hellwig 
59229125ed6SChristoph Hellwig 	if (!maxsector)
59329125ed6SChristoph Hellwig 		return;
59429125ed6SChristoph Hellwig 
59529125ed6SChristoph Hellwig 	/*
59629125ed6SChristoph Hellwig 	 * If the *whole* IO is past the end of the device,
59729125ed6SChristoph Hellwig 	 * let it through, and the IO layer will turn it into
59829125ed6SChristoph Hellwig 	 * an EIO.
59929125ed6SChristoph Hellwig 	 */
60029125ed6SChristoph Hellwig 	if (unlikely(bio->bi_iter.bi_sector >= maxsector))
60129125ed6SChristoph Hellwig 		return;
60229125ed6SChristoph Hellwig 
60329125ed6SChristoph Hellwig 	maxsector -= bio->bi_iter.bi_sector;
60429125ed6SChristoph Hellwig 	if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
60529125ed6SChristoph Hellwig 		return;
60629125ed6SChristoph Hellwig 
60729125ed6SChristoph Hellwig 	bio_truncate(bio, maxsector << 9);
60829125ed6SChristoph Hellwig }
60929125ed6SChristoph Hellwig 
61029125ed6SChristoph Hellwig /**
611f9c78b2bSJens Axboe  * bio_put - release a reference to a bio
612f9c78b2bSJens Axboe  * @bio:   bio to release reference to
613f9c78b2bSJens Axboe  *
614f9c78b2bSJens Axboe  * Description:
615f9c78b2bSJens Axboe  *   Put a reference to a &struct bio, either one you have gotten with
6169b10f6a9SNeilBrown  *   bio_alloc, bio_get or bio_clone_*. The last put of a bio will free it.
617f9c78b2bSJens Axboe  **/
618f9c78b2bSJens Axboe void bio_put(struct bio *bio)
619f9c78b2bSJens Axboe {
620dac56212SJens Axboe 	if (!bio_flagged(bio, BIO_REFFED))
621dac56212SJens Axboe 		bio_free(bio);
622dac56212SJens Axboe 	else {
623dac56212SJens Axboe 		BIO_BUG_ON(!atomic_read(&bio->__bi_cnt));
624f9c78b2bSJens Axboe 
625f9c78b2bSJens Axboe 		/*
626f9c78b2bSJens Axboe 		 * last put frees it
627f9c78b2bSJens Axboe 		 */
628dac56212SJens Axboe 		if (atomic_dec_and_test(&bio->__bi_cnt))
629f9c78b2bSJens Axboe 			bio_free(bio);
630f9c78b2bSJens Axboe 	}
631dac56212SJens Axboe }
632f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_put);
633f9c78b2bSJens Axboe 
634f9c78b2bSJens Axboe /**
635f9c78b2bSJens Axboe  * 	__bio_clone_fast - clone a bio that shares the original bio's biovec
636f9c78b2bSJens Axboe  * 	@bio: destination bio
637f9c78b2bSJens Axboe  * 	@bio_src: bio to clone
638f9c78b2bSJens Axboe  *
639f9c78b2bSJens Axboe  *	Clone a &bio. Caller will own the returned bio, but not
640f9c78b2bSJens Axboe  *	the actual data it points to. Reference count of returned
641f9c78b2bSJens Axboe  * 	bio will be one.
642f9c78b2bSJens Axboe  *
643f9c78b2bSJens Axboe  * 	Caller must ensure that @bio_src is not freed before @bio.
644f9c78b2bSJens Axboe  */
645f9c78b2bSJens Axboe void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
646f9c78b2bSJens Axboe {
647ed996a52SChristoph Hellwig 	BUG_ON(bio->bi_pool && BVEC_POOL_IDX(bio));
648f9c78b2bSJens Axboe 
649f9c78b2bSJens Axboe 	/*
650309dca30SChristoph Hellwig 	 * most users will be overriding ->bi_bdev with a new target,
651f9c78b2bSJens Axboe 	 * so we don't set nor calculate new physical/hw segment counts here
652f9c78b2bSJens Axboe 	 */
653309dca30SChristoph Hellwig 	bio->bi_bdev = bio_src->bi_bdev;
654b7c44ed9SJens Axboe 	bio_set_flag(bio, BIO_CLONED);
655111be883SShaohua Li 	if (bio_flagged(bio_src, BIO_THROTTLED))
656111be883SShaohua Li 		bio_set_flag(bio, BIO_THROTTLED);
65746bbf653SChristoph Hellwig 	if (bio_flagged(bio_src, BIO_REMAPPED))
65846bbf653SChristoph Hellwig 		bio_set_flag(bio, BIO_REMAPPED);
6591eff9d32SJens Axboe 	bio->bi_opf = bio_src->bi_opf;
660ca474b73SHannes Reinecke 	bio->bi_ioprio = bio_src->bi_ioprio;
661cb6934f8SJens Axboe 	bio->bi_write_hint = bio_src->bi_write_hint;
662f9c78b2bSJens Axboe 	bio->bi_iter = bio_src->bi_iter;
663f9c78b2bSJens Axboe 	bio->bi_io_vec = bio_src->bi_io_vec;
66420bd723eSPaolo Valente 
665db6638d7SDennis Zhou 	bio_clone_blkg_association(bio, bio_src);
666e439bedfSDennis Zhou 	blkcg_bio_issue_init(bio);
667f9c78b2bSJens Axboe }
668f9c78b2bSJens Axboe EXPORT_SYMBOL(__bio_clone_fast);
669f9c78b2bSJens Axboe 
670f9c78b2bSJens Axboe /**
671f9c78b2bSJens Axboe  *	bio_clone_fast - clone a bio that shares the original bio's biovec
672f9c78b2bSJens Axboe  *	@bio: bio to clone
673f9c78b2bSJens Axboe  *	@gfp_mask: allocation priority
674f9c78b2bSJens Axboe  *	@bs: bio_set to allocate from
675f9c78b2bSJens Axboe  *
676f9c78b2bSJens Axboe  * 	Like __bio_clone_fast, only also allocates the returned bio
677f9c78b2bSJens Axboe  */
678f9c78b2bSJens Axboe struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs)
679f9c78b2bSJens Axboe {
680f9c78b2bSJens Axboe 	struct bio *b;
681f9c78b2bSJens Axboe 
682f9c78b2bSJens Axboe 	b = bio_alloc_bioset(gfp_mask, 0, bs);
683f9c78b2bSJens Axboe 	if (!b)
684f9c78b2bSJens Axboe 		return NULL;
685f9c78b2bSJens Axboe 
686f9c78b2bSJens Axboe 	__bio_clone_fast(b, bio);
687f9c78b2bSJens Axboe 
68807560151SEric Biggers 	if (bio_crypt_clone(b, bio, gfp_mask) < 0)
68907560151SEric Biggers 		goto err_put;
690a892c8d5SSatya Tangirala 
69107560151SEric Biggers 	if (bio_integrity(bio) &&
69207560151SEric Biggers 	    bio_integrity_clone(b, bio, gfp_mask) < 0)
69307560151SEric Biggers 		goto err_put;
694f9c78b2bSJens Axboe 
695f9c78b2bSJens Axboe 	return b;
69607560151SEric Biggers 
69707560151SEric Biggers err_put:
69807560151SEric Biggers 	bio_put(b);
69907560151SEric Biggers 	return NULL;
700f9c78b2bSJens Axboe }
701f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_clone_fast);
702f9c78b2bSJens Axboe 
7035cbd28e3SChristoph Hellwig const char *bio_devname(struct bio *bio, char *buf)
7045cbd28e3SChristoph Hellwig {
705309dca30SChristoph Hellwig 	return bdevname(bio->bi_bdev, buf);
7065cbd28e3SChristoph Hellwig }
7075cbd28e3SChristoph Hellwig EXPORT_SYMBOL(bio_devname);
7085cbd28e3SChristoph Hellwig 
7095919482eSMing Lei static inline bool page_is_mergeable(const struct bio_vec *bv,
7105919482eSMing Lei 		struct page *page, unsigned int len, unsigned int off,
711ff896738SChristoph Hellwig 		bool *same_page)
7125919482eSMing Lei {
713d8166519SMatthew Wilcox (Oracle) 	size_t bv_end = bv->bv_offset + bv->bv_len;
714d8166519SMatthew Wilcox (Oracle) 	phys_addr_t vec_end_addr = page_to_phys(bv->bv_page) + bv_end - 1;
7155919482eSMing Lei 	phys_addr_t page_addr = page_to_phys(page);
7165919482eSMing Lei 
7175919482eSMing Lei 	if (vec_end_addr + 1 != page_addr + off)
7185919482eSMing Lei 		return false;
7195919482eSMing Lei 	if (xen_domain() && !xen_biovec_phys_mergeable(bv, page))
7205919482eSMing Lei 		return false;
72152d52d1cSChristoph Hellwig 
722ff896738SChristoph Hellwig 	*same_page = ((vec_end_addr & PAGE_MASK) == page_addr);
723d8166519SMatthew Wilcox (Oracle) 	if (*same_page)
7245919482eSMing Lei 		return true;
725d8166519SMatthew Wilcox (Oracle) 	return (bv->bv_page + bv_end / PAGE_SIZE) == (page + off / PAGE_SIZE);
7265919482eSMing Lei }
7275919482eSMing Lei 
728e4581105SChristoph Hellwig /*
729e4581105SChristoph Hellwig  * Try to merge a page into a segment, while obeying the hardware segment
730e4581105SChristoph Hellwig  * size limit.  This is not for normal read/write bios, but for passthrough
731e4581105SChristoph Hellwig  * or Zone Append operations that we can't split.
732e4581105SChristoph Hellwig  */
733e4581105SChristoph Hellwig static bool bio_try_merge_hw_seg(struct request_queue *q, struct bio *bio,
734e4581105SChristoph Hellwig 				 struct page *page, unsigned len,
735e4581105SChristoph Hellwig 				 unsigned offset, bool *same_page)
736489fbbcbSMing Lei {
737384209cdSChristoph Hellwig 	struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
738489fbbcbSMing Lei 	unsigned long mask = queue_segment_boundary(q);
739489fbbcbSMing Lei 	phys_addr_t addr1 = page_to_phys(bv->bv_page) + bv->bv_offset;
740489fbbcbSMing Lei 	phys_addr_t addr2 = page_to_phys(page) + offset + len - 1;
741489fbbcbSMing Lei 
742489fbbcbSMing Lei 	if ((addr1 | mask) != (addr2 | mask))
743489fbbcbSMing Lei 		return false;
744489fbbcbSMing Lei 	if (bv->bv_len + len > queue_max_segment_size(q))
745489fbbcbSMing Lei 		return false;
746384209cdSChristoph Hellwig 	return __bio_try_merge_page(bio, page, len, offset, same_page);
747489fbbcbSMing Lei }
748489fbbcbSMing Lei 
749f4595875SShaohua Li /**
750e4581105SChristoph Hellwig  * bio_add_hw_page - attempt to add a page to a bio with hw constraints
751c66a14d0SKent Overstreet  * @q: the target queue
752c66a14d0SKent Overstreet  * @bio: destination bio
753c66a14d0SKent Overstreet  * @page: page to add
754c66a14d0SKent Overstreet  * @len: vec entry length
755c66a14d0SKent Overstreet  * @offset: vec entry offset
756e4581105SChristoph Hellwig  * @max_sectors: maximum number of sectors that can be added
757e4581105SChristoph Hellwig  * @same_page: return if the segment has been merged inside the same page
758f9c78b2bSJens Axboe  *
759e4581105SChristoph Hellwig  * Add a page to a bio while respecting the hardware max_sectors, max_segment
760e4581105SChristoph Hellwig  * and gap limitations.
761f9c78b2bSJens Axboe  */
762e4581105SChristoph Hellwig int bio_add_hw_page(struct request_queue *q, struct bio *bio,
76319047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset,
764e4581105SChristoph Hellwig 		unsigned int max_sectors, bool *same_page)
765f9c78b2bSJens Axboe {
766f9c78b2bSJens Axboe 	struct bio_vec *bvec;
767f9c78b2bSJens Axboe 
768e4581105SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
769f9c78b2bSJens Axboe 		return 0;
770f9c78b2bSJens Axboe 
771e4581105SChristoph Hellwig 	if (((bio->bi_iter.bi_size + len) >> 9) > max_sectors)
772f9c78b2bSJens Axboe 		return 0;
773f9c78b2bSJens Axboe 
774f9c78b2bSJens Axboe 	if (bio->bi_vcnt > 0) {
775e4581105SChristoph Hellwig 		if (bio_try_merge_hw_seg(q, bio, page, len, offset, same_page))
776384209cdSChristoph Hellwig 			return len;
777320ea869SChristoph Hellwig 
778320ea869SChristoph Hellwig 		/*
779320ea869SChristoph Hellwig 		 * If the queue doesn't support SG gaps and adding this segment
780320ea869SChristoph Hellwig 		 * would create a gap, disallow it.
781320ea869SChristoph Hellwig 		 */
782384209cdSChristoph Hellwig 		bvec = &bio->bi_io_vec[bio->bi_vcnt - 1];
783320ea869SChristoph Hellwig 		if (bvec_gap_to_prev(q, bvec, offset))
784320ea869SChristoph Hellwig 			return 0;
785f9c78b2bSJens Axboe 	}
786f9c78b2bSJens Axboe 
78779d08f89SMing Lei 	if (bio_full(bio, len))
788f9c78b2bSJens Axboe 		return 0;
789f9c78b2bSJens Axboe 
79014ccb66bSChristoph Hellwig 	if (bio->bi_vcnt >= queue_max_segments(q))
791489fbbcbSMing Lei 		return 0;
792489fbbcbSMing Lei 
793f9c78b2bSJens Axboe 	bvec = &bio->bi_io_vec[bio->bi_vcnt];
794f9c78b2bSJens Axboe 	bvec->bv_page = page;
795f9c78b2bSJens Axboe 	bvec->bv_len = len;
796f9c78b2bSJens Axboe 	bvec->bv_offset = offset;
797fcbf6a08SMaurizio Lombardi 	bio->bi_vcnt++;
798dcdca753SChristoph Hellwig 	bio->bi_iter.bi_size += len;
799f9c78b2bSJens Axboe 	return len;
800f9c78b2bSJens Axboe }
80119047087SMing Lei 
802e4581105SChristoph Hellwig /**
803e4581105SChristoph Hellwig  * bio_add_pc_page	- attempt to add page to passthrough bio
804e4581105SChristoph Hellwig  * @q: the target queue
805e4581105SChristoph Hellwig  * @bio: destination bio
806e4581105SChristoph Hellwig  * @page: page to add
807e4581105SChristoph Hellwig  * @len: vec entry length
808e4581105SChristoph Hellwig  * @offset: vec entry offset
809e4581105SChristoph Hellwig  *
810e4581105SChristoph Hellwig  * Attempt to add a page to the bio_vec maplist. This can fail for a
811e4581105SChristoph Hellwig  * number of reasons, such as the bio being full or target block device
812e4581105SChristoph Hellwig  * limitations. The target block device must allow bio's up to PAGE_SIZE,
813e4581105SChristoph Hellwig  * so it is always possible to add a single page to an empty bio.
814e4581105SChristoph Hellwig  *
815e4581105SChristoph Hellwig  * This should only be used by passthrough bios.
816e4581105SChristoph Hellwig  */
81719047087SMing Lei int bio_add_pc_page(struct request_queue *q, struct bio *bio,
81819047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset)
81919047087SMing Lei {
820d1916c86SChristoph Hellwig 	bool same_page = false;
821e4581105SChristoph Hellwig 	return bio_add_hw_page(q, bio, page, len, offset,
822e4581105SChristoph Hellwig 			queue_max_hw_sectors(q), &same_page);
82319047087SMing Lei }
824f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_pc_page);
825f9c78b2bSJens Axboe 
826f9c78b2bSJens Axboe /**
8270aa69fd3SChristoph Hellwig  * __bio_try_merge_page - try appending data to an existing bvec.
8280aa69fd3SChristoph Hellwig  * @bio: destination bio
829551879a4SMing Lei  * @page: start page to add
8300aa69fd3SChristoph Hellwig  * @len: length of the data to add
831551879a4SMing Lei  * @off: offset of the data relative to @page
832ff896738SChristoph Hellwig  * @same_page: return if the segment has been merged inside the same page
8330aa69fd3SChristoph Hellwig  *
8340aa69fd3SChristoph Hellwig  * Try to add the data at @page + @off to the last bvec of @bio.  This is a
8353cf14889SRandy Dunlap  * useful optimisation for file systems with a block size smaller than the
8360aa69fd3SChristoph Hellwig  * page size.
8370aa69fd3SChristoph Hellwig  *
838551879a4SMing Lei  * Warn if (@len, @off) crosses pages in case that @same_page is true.
839551879a4SMing Lei  *
8400aa69fd3SChristoph Hellwig  * Return %true on success or %false on failure.
8410aa69fd3SChristoph Hellwig  */
8420aa69fd3SChristoph Hellwig bool __bio_try_merge_page(struct bio *bio, struct page *page,
843ff896738SChristoph Hellwig 		unsigned int len, unsigned int off, bool *same_page)
8440aa69fd3SChristoph Hellwig {
8450aa69fd3SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
8460aa69fd3SChristoph Hellwig 		return false;
8470aa69fd3SChristoph Hellwig 
848cc90bc68SAndreas Gruenbacher 	if (bio->bi_vcnt > 0) {
8490aa69fd3SChristoph Hellwig 		struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
8500aa69fd3SChristoph Hellwig 
8515919482eSMing Lei 		if (page_is_mergeable(bv, page, len, off, same_page)) {
8522cd896a5SRitesh Harjani 			if (bio->bi_iter.bi_size > UINT_MAX - len) {
8532cd896a5SRitesh Harjani 				*same_page = false;
854cc90bc68SAndreas Gruenbacher 				return false;
8552cd896a5SRitesh Harjani 			}
8560aa69fd3SChristoph Hellwig 			bv->bv_len += len;
8570aa69fd3SChristoph Hellwig 			bio->bi_iter.bi_size += len;
8580aa69fd3SChristoph Hellwig 			return true;
8590aa69fd3SChristoph Hellwig 		}
8605919482eSMing Lei 	}
8610aa69fd3SChristoph Hellwig 	return false;
8620aa69fd3SChristoph Hellwig }
8630aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_try_merge_page);
8640aa69fd3SChristoph Hellwig 
8650aa69fd3SChristoph Hellwig /**
866551879a4SMing Lei  * __bio_add_page - add page(s) to a bio in a new segment
8670aa69fd3SChristoph Hellwig  * @bio: destination bio
868551879a4SMing Lei  * @page: start page to add
869551879a4SMing Lei  * @len: length of the data to add, may cross pages
870551879a4SMing Lei  * @off: offset of the data relative to @page, may cross pages
8710aa69fd3SChristoph Hellwig  *
8720aa69fd3SChristoph Hellwig  * Add the data at @page + @off to @bio as a new bvec.  The caller must ensure
8730aa69fd3SChristoph Hellwig  * that @bio has space for another bvec.
8740aa69fd3SChristoph Hellwig  */
8750aa69fd3SChristoph Hellwig void __bio_add_page(struct bio *bio, struct page *page,
8760aa69fd3SChristoph Hellwig 		unsigned int len, unsigned int off)
8770aa69fd3SChristoph Hellwig {
8780aa69fd3SChristoph Hellwig 	struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt];
8790aa69fd3SChristoph Hellwig 
8800aa69fd3SChristoph Hellwig 	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
88179d08f89SMing Lei 	WARN_ON_ONCE(bio_full(bio, len));
8820aa69fd3SChristoph Hellwig 
8830aa69fd3SChristoph Hellwig 	bv->bv_page = page;
8840aa69fd3SChristoph Hellwig 	bv->bv_offset = off;
8850aa69fd3SChristoph Hellwig 	bv->bv_len = len;
8860aa69fd3SChristoph Hellwig 
8870aa69fd3SChristoph Hellwig 	bio->bi_iter.bi_size += len;
8880aa69fd3SChristoph Hellwig 	bio->bi_vcnt++;
889b8e24a93SJohannes Weiner 
890b8e24a93SJohannes Weiner 	if (!bio_flagged(bio, BIO_WORKINGSET) && unlikely(PageWorkingset(page)))
891b8e24a93SJohannes Weiner 		bio_set_flag(bio, BIO_WORKINGSET);
8920aa69fd3SChristoph Hellwig }
8930aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_add_page);
8940aa69fd3SChristoph Hellwig 
8950aa69fd3SChristoph Hellwig /**
896551879a4SMing Lei  *	bio_add_page	-	attempt to add page(s) to bio
897f9c78b2bSJens Axboe  *	@bio: destination bio
898551879a4SMing Lei  *	@page: start page to add
899551879a4SMing Lei  *	@len: vec entry length, may cross pages
900551879a4SMing Lei  *	@offset: vec entry offset relative to @page, may cross pages
901f9c78b2bSJens Axboe  *
902551879a4SMing Lei  *	Attempt to add page(s) to the bio_vec maplist. This will only fail
903c66a14d0SKent Overstreet  *	if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
904f9c78b2bSJens Axboe  */
905c66a14d0SKent Overstreet int bio_add_page(struct bio *bio, struct page *page,
906c66a14d0SKent Overstreet 		 unsigned int len, unsigned int offset)
907f9c78b2bSJens Axboe {
908ff896738SChristoph Hellwig 	bool same_page = false;
909ff896738SChristoph Hellwig 
910ff896738SChristoph Hellwig 	if (!__bio_try_merge_page(bio, page, len, offset, &same_page)) {
91179d08f89SMing Lei 		if (bio_full(bio, len))
912c66a14d0SKent Overstreet 			return 0;
9130aa69fd3SChristoph Hellwig 		__bio_add_page(bio, page, len, offset);
914c66a14d0SKent Overstreet 	}
915c66a14d0SKent Overstreet 	return len;
916f9c78b2bSJens Axboe }
917f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_page);
918f9c78b2bSJens Axboe 
919d241a95fSChristoph Hellwig void bio_release_pages(struct bio *bio, bool mark_dirty)
9207321ecbfSChristoph Hellwig {
9217321ecbfSChristoph Hellwig 	struct bvec_iter_all iter_all;
9227321ecbfSChristoph Hellwig 	struct bio_vec *bvec;
9237321ecbfSChristoph Hellwig 
924b2d0d991SChristoph Hellwig 	if (bio_flagged(bio, BIO_NO_PAGE_REF))
925b2d0d991SChristoph Hellwig 		return;
926b2d0d991SChristoph Hellwig 
927d241a95fSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
928d241a95fSChristoph Hellwig 		if (mark_dirty && !PageCompound(bvec->bv_page))
929d241a95fSChristoph Hellwig 			set_page_dirty_lock(bvec->bv_page);
9307321ecbfSChristoph Hellwig 		put_page(bvec->bv_page);
9317321ecbfSChristoph Hellwig 	}
932d241a95fSChristoph Hellwig }
93329b2a3aaSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_release_pages);
9347321ecbfSChristoph Hellwig 
935c42bca92SPavel Begunkov static int bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
9366d0c48aeSJens Axboe {
937c42bca92SPavel Begunkov 	WARN_ON_ONCE(BVEC_POOL_IDX(bio) != 0);
9386d0c48aeSJens Axboe 
939c42bca92SPavel Begunkov 	bio->bi_vcnt = iter->nr_segs;
940c42bca92SPavel Begunkov 	bio->bi_max_vecs = iter->nr_segs;
941c42bca92SPavel Begunkov 	bio->bi_io_vec = (struct bio_vec *)iter->bvec;
942c42bca92SPavel Begunkov 	bio->bi_iter.bi_bvec_done = iter->iov_offset;
943c42bca92SPavel Begunkov 	bio->bi_iter.bi_size = iter->count;
9446d0c48aeSJens Axboe 
945c42bca92SPavel Begunkov 	iov_iter_advance(iter, iter->count);
9466d0c48aeSJens Axboe 	return 0;
9476d0c48aeSJens Axboe }
9486d0c48aeSJens Axboe 
949576ed913SChristoph Hellwig #define PAGE_PTRS_PER_BVEC     (sizeof(struct bio_vec) / sizeof(struct page *))
950576ed913SChristoph Hellwig 
9512cefe4dbSKent Overstreet /**
95217d51b10SMartin Wilck  * __bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
9532cefe4dbSKent Overstreet  * @bio: bio to add pages to
9542cefe4dbSKent Overstreet  * @iter: iov iterator describing the region to be mapped
9552cefe4dbSKent Overstreet  *
95617d51b10SMartin Wilck  * Pins pages from *iter and appends them to @bio's bvec array. The
9572cefe4dbSKent Overstreet  * pages will have to be released using put_page() when done.
95817d51b10SMartin Wilck  * For multi-segment *iter, this function only adds pages from the
9593cf14889SRandy Dunlap  * next non-empty segment of the iov iterator.
9602cefe4dbSKent Overstreet  */
96117d51b10SMartin Wilck static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
9622cefe4dbSKent Overstreet {
963576ed913SChristoph Hellwig 	unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt;
964576ed913SChristoph Hellwig 	unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt;
9652cefe4dbSKent Overstreet 	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
9662cefe4dbSKent Overstreet 	struct page **pages = (struct page **)bv;
96745691804SChristoph Hellwig 	bool same_page = false;
968576ed913SChristoph Hellwig 	ssize_t size, left;
969576ed913SChristoph Hellwig 	unsigned len, i;
970b403ea24SMartin Wilck 	size_t offset;
971576ed913SChristoph Hellwig 
972576ed913SChristoph Hellwig 	/*
973576ed913SChristoph Hellwig 	 * Move page array up in the allocated memory for the bio vecs as far as
974576ed913SChristoph Hellwig 	 * possible so that we can start filling biovecs from the beginning
975576ed913SChristoph Hellwig 	 * without overwriting the temporary page array.
976576ed913SChristoph Hellwig 	*/
977576ed913SChristoph Hellwig 	BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2);
978576ed913SChristoph Hellwig 	pages += entries_left * (PAGE_PTRS_PER_BVEC - 1);
9792cefe4dbSKent Overstreet 
9802cefe4dbSKent Overstreet 	size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset);
9812cefe4dbSKent Overstreet 	if (unlikely(size <= 0))
9822cefe4dbSKent Overstreet 		return size ? size : -EFAULT;
9832cefe4dbSKent Overstreet 
984576ed913SChristoph Hellwig 	for (left = size, i = 0; left > 0; left -= len, i++) {
985576ed913SChristoph Hellwig 		struct page *page = pages[i];
9862cefe4dbSKent Overstreet 
987576ed913SChristoph Hellwig 		len = min_t(size_t, PAGE_SIZE - offset, left);
98845691804SChristoph Hellwig 
98945691804SChristoph Hellwig 		if (__bio_try_merge_page(bio, page, len, offset, &same_page)) {
99045691804SChristoph Hellwig 			if (same_page)
99145691804SChristoph Hellwig 				put_page(page);
99245691804SChristoph Hellwig 		} else {
99379d08f89SMing Lei 			if (WARN_ON_ONCE(bio_full(bio, len)))
994576ed913SChristoph Hellwig                                 return -EINVAL;
99545691804SChristoph Hellwig 			__bio_add_page(bio, page, len, offset);
99645691804SChristoph Hellwig 		}
997576ed913SChristoph Hellwig 		offset = 0;
9982cefe4dbSKent Overstreet 	}
9992cefe4dbSKent Overstreet 
10002cefe4dbSKent Overstreet 	iov_iter_advance(iter, size);
10012cefe4dbSKent Overstreet 	return 0;
10022cefe4dbSKent Overstreet }
100317d51b10SMartin Wilck 
10040512a75bSKeith Busch static int __bio_iov_append_get_pages(struct bio *bio, struct iov_iter *iter)
10050512a75bSKeith Busch {
10060512a75bSKeith Busch 	unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt;
10070512a75bSKeith Busch 	unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt;
1008309dca30SChristoph Hellwig 	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
10090512a75bSKeith Busch 	unsigned int max_append_sectors = queue_max_zone_append_sectors(q);
10100512a75bSKeith Busch 	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
10110512a75bSKeith Busch 	struct page **pages = (struct page **)bv;
10120512a75bSKeith Busch 	ssize_t size, left;
10130512a75bSKeith Busch 	unsigned len, i;
10140512a75bSKeith Busch 	size_t offset;
10154977d121SNaohiro Aota 	int ret = 0;
10160512a75bSKeith Busch 
10170512a75bSKeith Busch 	if (WARN_ON_ONCE(!max_append_sectors))
10180512a75bSKeith Busch 		return 0;
10190512a75bSKeith Busch 
10200512a75bSKeith Busch 	/*
10210512a75bSKeith Busch 	 * Move page array up in the allocated memory for the bio vecs as far as
10220512a75bSKeith Busch 	 * possible so that we can start filling biovecs from the beginning
10230512a75bSKeith Busch 	 * without overwriting the temporary page array.
10240512a75bSKeith Busch 	 */
10250512a75bSKeith Busch 	BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2);
10260512a75bSKeith Busch 	pages += entries_left * (PAGE_PTRS_PER_BVEC - 1);
10270512a75bSKeith Busch 
10280512a75bSKeith Busch 	size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset);
10290512a75bSKeith Busch 	if (unlikely(size <= 0))
10300512a75bSKeith Busch 		return size ? size : -EFAULT;
10310512a75bSKeith Busch 
10320512a75bSKeith Busch 	for (left = size, i = 0; left > 0; left -= len, i++) {
10330512a75bSKeith Busch 		struct page *page = pages[i];
10340512a75bSKeith Busch 		bool same_page = false;
10350512a75bSKeith Busch 
10360512a75bSKeith Busch 		len = min_t(size_t, PAGE_SIZE - offset, left);
10370512a75bSKeith Busch 		if (bio_add_hw_page(q, bio, page, len, offset,
10384977d121SNaohiro Aota 				max_append_sectors, &same_page) != len) {
10394977d121SNaohiro Aota 			ret = -EINVAL;
10404977d121SNaohiro Aota 			break;
10414977d121SNaohiro Aota 		}
10420512a75bSKeith Busch 		if (same_page)
10430512a75bSKeith Busch 			put_page(page);
10440512a75bSKeith Busch 		offset = 0;
10450512a75bSKeith Busch 	}
10460512a75bSKeith Busch 
10474977d121SNaohiro Aota 	iov_iter_advance(iter, size - left);
10484977d121SNaohiro Aota 	return ret;
10490512a75bSKeith Busch }
10500512a75bSKeith Busch 
105117d51b10SMartin Wilck /**
10526d0c48aeSJens Axboe  * bio_iov_iter_get_pages - add user or kernel pages to a bio
105317d51b10SMartin Wilck  * @bio: bio to add pages to
10546d0c48aeSJens Axboe  * @iter: iov iterator describing the region to be added
105517d51b10SMartin Wilck  *
10566d0c48aeSJens Axboe  * This takes either an iterator pointing to user memory, or one pointing to
10576d0c48aeSJens Axboe  * kernel pages (BVEC iterator). If we're adding user pages, we pin them and
10586d0c48aeSJens Axboe  * map them into the kernel. On IO completion, the caller should put those
1059c42bca92SPavel Begunkov  * pages. For bvec based iterators bio_iov_iter_get_pages() uses the provided
1060c42bca92SPavel Begunkov  * bvecs rather than copying them. Hence anyone issuing kiocb based IO needs
1061c42bca92SPavel Begunkov  * to ensure the bvecs and pages stay referenced until the submitted I/O is
1062c42bca92SPavel Begunkov  * completed by a call to ->ki_complete() or returns with an error other than
1063c42bca92SPavel Begunkov  * -EIOCBQUEUED. The caller needs to check if the bio is flagged BIO_NO_PAGE_REF
1064c42bca92SPavel Begunkov  * on IO completion. If it isn't, then pages should be released.
10656d0c48aeSJens Axboe  *
106617d51b10SMartin Wilck  * The function tries, but does not guarantee, to pin as many pages as
10675cd3ddc1SMauro Carvalho Chehab  * fit into the bio, or are requested in @iter, whatever is smaller. If
10686d0c48aeSJens Axboe  * MM encounters an error pinning the requested pages, it stops. Error
10696d0c48aeSJens Axboe  * is returned only if 0 pages could be pinned.
10700cf41e5eSPavel Begunkov  *
10710cf41e5eSPavel Begunkov  * It's intended for direct IO, so doesn't do PSI tracking, the caller is
10720cf41e5eSPavel Begunkov  * responsible for setting BIO_WORKINGSET if necessary.
107317d51b10SMartin Wilck  */
107417d51b10SMartin Wilck int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
107517d51b10SMartin Wilck {
1076c42bca92SPavel Begunkov 	int ret = 0;
107714eacf12SChristoph Hellwig 
1078c42bca92SPavel Begunkov 	if (iov_iter_is_bvec(iter)) {
1079c42bca92SPavel Begunkov 		if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
108014eacf12SChristoph Hellwig 			return -EINVAL;
1081c42bca92SPavel Begunkov 		bio_iov_bvec_set(bio, iter);
1082c42bca92SPavel Begunkov 		bio_set_flag(bio, BIO_NO_PAGE_REF);
1083c42bca92SPavel Begunkov 		return 0;
1084*86004515SChristoph Hellwig 	}
1085*86004515SChristoph Hellwig 
1086c42bca92SPavel Begunkov 	do {
1087c42bca92SPavel Begunkov 		if (bio_op(bio) == REQ_OP_ZONE_APPEND)
1088c42bca92SPavel Begunkov 			ret = __bio_iov_append_get_pages(bio, iter);
10896d0c48aeSJens Axboe 		else
10906d0c48aeSJens Axboe 			ret = __bio_iov_iter_get_pages(bio, iter);
109179d08f89SMing Lei 	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
10920cf41e5eSPavel Begunkov 
10930cf41e5eSPavel Begunkov 	/* don't account direct I/O as memory stall */
10940cf41e5eSPavel Begunkov 	bio_clear_flag(bio, BIO_WORKINGSET);
109514eacf12SChristoph Hellwig 	return bio->bi_vcnt ? 0 : ret;
109617d51b10SMartin Wilck }
109729b2a3aaSJohannes Thumshirn EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages);
10982cefe4dbSKent Overstreet 
10994246a0b6SChristoph Hellwig static void submit_bio_wait_endio(struct bio *bio)
1100f9c78b2bSJens Axboe {
110165e53aabSChristoph Hellwig 	complete(bio->bi_private);
1102f9c78b2bSJens Axboe }
1103f9c78b2bSJens Axboe 
1104f9c78b2bSJens Axboe /**
1105f9c78b2bSJens Axboe  * submit_bio_wait - submit a bio, and wait until it completes
1106f9c78b2bSJens Axboe  * @bio: The &struct bio which describes the I/O
1107f9c78b2bSJens Axboe  *
1108f9c78b2bSJens Axboe  * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
1109f9c78b2bSJens Axboe  * bio_endio() on failure.
11103d289d68SJan Kara  *
11113d289d68SJan Kara  * WARNING: Unlike to how submit_bio() is usually used, this function does not
11123d289d68SJan Kara  * result in bio reference to be consumed. The caller must drop the reference
11133d289d68SJan Kara  * on his own.
1114f9c78b2bSJens Axboe  */
11154e49ea4aSMike Christie int submit_bio_wait(struct bio *bio)
1116f9c78b2bSJens Axboe {
1117309dca30SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK_MAP(done,
1118309dca30SChristoph Hellwig 			bio->bi_bdev->bd_disk->lockdep_map);
1119de6a78b6SMing Lei 	unsigned long hang_check;
1120f9c78b2bSJens Axboe 
112165e53aabSChristoph Hellwig 	bio->bi_private = &done;
1122f9c78b2bSJens Axboe 	bio->bi_end_io = submit_bio_wait_endio;
11231eff9d32SJens Axboe 	bio->bi_opf |= REQ_SYNC;
11244e49ea4aSMike Christie 	submit_bio(bio);
1125de6a78b6SMing Lei 
1126de6a78b6SMing Lei 	/* Prevent hang_check timer from firing at us during very long I/O */
1127de6a78b6SMing Lei 	hang_check = sysctl_hung_task_timeout_secs;
1128de6a78b6SMing Lei 	if (hang_check)
1129de6a78b6SMing Lei 		while (!wait_for_completion_io_timeout(&done,
1130de6a78b6SMing Lei 					hang_check * (HZ/2)))
1131de6a78b6SMing Lei 			;
1132de6a78b6SMing Lei 	else
113365e53aabSChristoph Hellwig 		wait_for_completion_io(&done);
1134f9c78b2bSJens Axboe 
113565e53aabSChristoph Hellwig 	return blk_status_to_errno(bio->bi_status);
1136f9c78b2bSJens Axboe }
1137f9c78b2bSJens Axboe EXPORT_SYMBOL(submit_bio_wait);
1138f9c78b2bSJens Axboe 
1139f9c78b2bSJens Axboe /**
1140f9c78b2bSJens Axboe  * bio_advance - increment/complete a bio by some number of bytes
1141f9c78b2bSJens Axboe  * @bio:	bio to advance
1142f9c78b2bSJens Axboe  * @bytes:	number of bytes to complete
1143f9c78b2bSJens Axboe  *
1144f9c78b2bSJens Axboe  * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
1145f9c78b2bSJens Axboe  * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
1146f9c78b2bSJens Axboe  * be updated on the last bvec as well.
1147f9c78b2bSJens Axboe  *
1148f9c78b2bSJens Axboe  * @bio will then represent the remaining, uncompleted portion of the io.
1149f9c78b2bSJens Axboe  */
1150f9c78b2bSJens Axboe void bio_advance(struct bio *bio, unsigned bytes)
1151f9c78b2bSJens Axboe {
1152f9c78b2bSJens Axboe 	if (bio_integrity(bio))
1153f9c78b2bSJens Axboe 		bio_integrity_advance(bio, bytes);
1154f9c78b2bSJens Axboe 
1155a892c8d5SSatya Tangirala 	bio_crypt_advance(bio, bytes);
1156f9c78b2bSJens Axboe 	bio_advance_iter(bio, &bio->bi_iter, bytes);
1157f9c78b2bSJens Axboe }
1158f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_advance);
1159f9c78b2bSJens Axboe 
116045db54d5SKent Overstreet void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
116145db54d5SKent Overstreet 			struct bio *src, struct bvec_iter *src_iter)
1162f9c78b2bSJens Axboe {
1163f9c78b2bSJens Axboe 	struct bio_vec src_bv, dst_bv;
1164f9c78b2bSJens Axboe 	void *src_p, *dst_p;
1165f9c78b2bSJens Axboe 	unsigned bytes;
1166f9c78b2bSJens Axboe 
116745db54d5SKent Overstreet 	while (src_iter->bi_size && dst_iter->bi_size) {
116845db54d5SKent Overstreet 		src_bv = bio_iter_iovec(src, *src_iter);
116945db54d5SKent Overstreet 		dst_bv = bio_iter_iovec(dst, *dst_iter);
117045db54d5SKent Overstreet 
117145db54d5SKent Overstreet 		bytes = min(src_bv.bv_len, dst_bv.bv_len);
117245db54d5SKent Overstreet 
117345db54d5SKent Overstreet 		src_p = kmap_atomic(src_bv.bv_page);
117445db54d5SKent Overstreet 		dst_p = kmap_atomic(dst_bv.bv_page);
117545db54d5SKent Overstreet 
117645db54d5SKent Overstreet 		memcpy(dst_p + dst_bv.bv_offset,
117745db54d5SKent Overstreet 		       src_p + src_bv.bv_offset,
117845db54d5SKent Overstreet 		       bytes);
117945db54d5SKent Overstreet 
118045db54d5SKent Overstreet 		kunmap_atomic(dst_p);
118145db54d5SKent Overstreet 		kunmap_atomic(src_p);
118245db54d5SKent Overstreet 
11836e6e811dSKent Overstreet 		flush_dcache_page(dst_bv.bv_page);
11846e6e811dSKent Overstreet 
118522b56c29SPavel Begunkov 		bio_advance_iter_single(src, src_iter, bytes);
118622b56c29SPavel Begunkov 		bio_advance_iter_single(dst, dst_iter, bytes);
118745db54d5SKent Overstreet 	}
118845db54d5SKent Overstreet }
118945db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data_iter);
119045db54d5SKent Overstreet 
119145db54d5SKent Overstreet /**
119245db54d5SKent Overstreet  * bio_copy_data - copy contents of data buffers from one bio to another
119345db54d5SKent Overstreet  * @src: source bio
119445db54d5SKent Overstreet  * @dst: destination bio
119545db54d5SKent Overstreet  *
119645db54d5SKent Overstreet  * Stops when it reaches the end of either @src or @dst - that is, copies
119745db54d5SKent Overstreet  * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
119845db54d5SKent Overstreet  */
119945db54d5SKent Overstreet void bio_copy_data(struct bio *dst, struct bio *src)
120045db54d5SKent Overstreet {
120145db54d5SKent Overstreet 	struct bvec_iter src_iter = src->bi_iter;
120245db54d5SKent Overstreet 	struct bvec_iter dst_iter = dst->bi_iter;
120345db54d5SKent Overstreet 
120445db54d5SKent Overstreet 	bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
120545db54d5SKent Overstreet }
120645db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data);
120745db54d5SKent Overstreet 
120845db54d5SKent Overstreet /**
120945db54d5SKent Overstreet  * bio_list_copy_data - copy contents of data buffers from one chain of bios to
121045db54d5SKent Overstreet  * another
121145db54d5SKent Overstreet  * @src: source bio list
121245db54d5SKent Overstreet  * @dst: destination bio list
121345db54d5SKent Overstreet  *
121445db54d5SKent Overstreet  * Stops when it reaches the end of either the @src list or @dst list - that is,
121545db54d5SKent Overstreet  * copies min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of
121645db54d5SKent Overstreet  * bios).
121745db54d5SKent Overstreet  */
121845db54d5SKent Overstreet void bio_list_copy_data(struct bio *dst, struct bio *src)
121945db54d5SKent Overstreet {
122045db54d5SKent Overstreet 	struct bvec_iter src_iter = src->bi_iter;
122145db54d5SKent Overstreet 	struct bvec_iter dst_iter = dst->bi_iter;
122245db54d5SKent Overstreet 
1223f9c78b2bSJens Axboe 	while (1) {
1224f9c78b2bSJens Axboe 		if (!src_iter.bi_size) {
1225f9c78b2bSJens Axboe 			src = src->bi_next;
1226f9c78b2bSJens Axboe 			if (!src)
1227f9c78b2bSJens Axboe 				break;
1228f9c78b2bSJens Axboe 
1229f9c78b2bSJens Axboe 			src_iter = src->bi_iter;
1230f9c78b2bSJens Axboe 		}
1231f9c78b2bSJens Axboe 
1232f9c78b2bSJens Axboe 		if (!dst_iter.bi_size) {
1233f9c78b2bSJens Axboe 			dst = dst->bi_next;
1234f9c78b2bSJens Axboe 			if (!dst)
1235f9c78b2bSJens Axboe 				break;
1236f9c78b2bSJens Axboe 
1237f9c78b2bSJens Axboe 			dst_iter = dst->bi_iter;
1238f9c78b2bSJens Axboe 		}
1239f9c78b2bSJens Axboe 
124045db54d5SKent Overstreet 		bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
1241f9c78b2bSJens Axboe 	}
1242f9c78b2bSJens Axboe }
124345db54d5SKent Overstreet EXPORT_SYMBOL(bio_list_copy_data);
1244f9c78b2bSJens Axboe 
1245491221f8SGuoqing Jiang void bio_free_pages(struct bio *bio)
12461dfa0f68SChristoph Hellwig {
12471dfa0f68SChristoph Hellwig 	struct bio_vec *bvec;
12486dc4f100SMing Lei 	struct bvec_iter_all iter_all;
12491dfa0f68SChristoph Hellwig 
12502b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all)
12511dfa0f68SChristoph Hellwig 		__free_page(bvec->bv_page);
12521dfa0f68SChristoph Hellwig }
1253491221f8SGuoqing Jiang EXPORT_SYMBOL(bio_free_pages);
12541dfa0f68SChristoph Hellwig 
1255f9c78b2bSJens Axboe /*
1256f9c78b2bSJens Axboe  * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
1257f9c78b2bSJens Axboe  * for performing direct-IO in BIOs.
1258f9c78b2bSJens Axboe  *
1259f9c78b2bSJens Axboe  * The problem is that we cannot run set_page_dirty() from interrupt context
1260f9c78b2bSJens Axboe  * because the required locks are not interrupt-safe.  So what we can do is to
1261f9c78b2bSJens Axboe  * mark the pages dirty _before_ performing IO.  And in interrupt context,
1262f9c78b2bSJens Axboe  * check that the pages are still dirty.   If so, fine.  If not, redirty them
1263f9c78b2bSJens Axboe  * in process context.
1264f9c78b2bSJens Axboe  *
1265f9c78b2bSJens Axboe  * We special-case compound pages here: normally this means reads into hugetlb
1266f9c78b2bSJens Axboe  * pages.  The logic in here doesn't really work right for compound pages
1267f9c78b2bSJens Axboe  * because the VM does not uniformly chase down the head page in all cases.
1268f9c78b2bSJens Axboe  * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
1269f9c78b2bSJens Axboe  * handle them at all.  So we skip compound pages here at an early stage.
1270f9c78b2bSJens Axboe  *
1271f9c78b2bSJens Axboe  * Note that this code is very hard to test under normal circumstances because
1272f9c78b2bSJens Axboe  * direct-io pins the pages with get_user_pages().  This makes
1273f9c78b2bSJens Axboe  * is_page_cache_freeable return false, and the VM will not clean the pages.
1274f9c78b2bSJens Axboe  * But other code (eg, flusher threads) could clean the pages if they are mapped
1275f9c78b2bSJens Axboe  * pagecache.
1276f9c78b2bSJens Axboe  *
1277f9c78b2bSJens Axboe  * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
1278f9c78b2bSJens Axboe  * deferred bio dirtying paths.
1279f9c78b2bSJens Axboe  */
1280f9c78b2bSJens Axboe 
1281f9c78b2bSJens Axboe /*
1282f9c78b2bSJens Axboe  * bio_set_pages_dirty() will mark all the bio's pages as dirty.
1283f9c78b2bSJens Axboe  */
1284f9c78b2bSJens Axboe void bio_set_pages_dirty(struct bio *bio)
1285f9c78b2bSJens Axboe {
1286f9c78b2bSJens Axboe 	struct bio_vec *bvec;
12876dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1288f9c78b2bSJens Axboe 
12892b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
12903bb50983SChristoph Hellwig 		if (!PageCompound(bvec->bv_page))
12913bb50983SChristoph Hellwig 			set_page_dirty_lock(bvec->bv_page);
1292f9c78b2bSJens Axboe 	}
1293f9c78b2bSJens Axboe }
1294f9c78b2bSJens Axboe 
1295f9c78b2bSJens Axboe /*
1296f9c78b2bSJens Axboe  * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
1297f9c78b2bSJens Axboe  * If they are, then fine.  If, however, some pages are clean then they must
1298f9c78b2bSJens Axboe  * have been written out during the direct-IO read.  So we take another ref on
129924d5493fSChristoph Hellwig  * the BIO and re-dirty the pages in process context.
1300f9c78b2bSJens Axboe  *
1301f9c78b2bSJens Axboe  * It is expected that bio_check_pages_dirty() will wholly own the BIO from
1302ea1754a0SKirill A. Shutemov  * here on.  It will run one put_page() against each page and will run one
1303ea1754a0SKirill A. Shutemov  * bio_put() against the BIO.
1304f9c78b2bSJens Axboe  */
1305f9c78b2bSJens Axboe 
1306f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work);
1307f9c78b2bSJens Axboe 
1308f9c78b2bSJens Axboe static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
1309f9c78b2bSJens Axboe static DEFINE_SPINLOCK(bio_dirty_lock);
1310f9c78b2bSJens Axboe static struct bio *bio_dirty_list;
1311f9c78b2bSJens Axboe 
1312f9c78b2bSJens Axboe /*
1313f9c78b2bSJens Axboe  * This runs in process context
1314f9c78b2bSJens Axboe  */
1315f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work)
1316f9c78b2bSJens Axboe {
131724d5493fSChristoph Hellwig 	struct bio *bio, *next;
1318f9c78b2bSJens Axboe 
131924d5493fSChristoph Hellwig 	spin_lock_irq(&bio_dirty_lock);
132024d5493fSChristoph Hellwig 	next = bio_dirty_list;
1321f9c78b2bSJens Axboe 	bio_dirty_list = NULL;
132224d5493fSChristoph Hellwig 	spin_unlock_irq(&bio_dirty_lock);
1323f9c78b2bSJens Axboe 
132424d5493fSChristoph Hellwig 	while ((bio = next) != NULL) {
132524d5493fSChristoph Hellwig 		next = bio->bi_private;
1326f9c78b2bSJens Axboe 
1327d241a95fSChristoph Hellwig 		bio_release_pages(bio, true);
1328f9c78b2bSJens Axboe 		bio_put(bio);
1329f9c78b2bSJens Axboe 	}
1330f9c78b2bSJens Axboe }
1331f9c78b2bSJens Axboe 
1332f9c78b2bSJens Axboe void bio_check_pages_dirty(struct bio *bio)
1333f9c78b2bSJens Axboe {
1334f9c78b2bSJens Axboe 	struct bio_vec *bvec;
133524d5493fSChristoph Hellwig 	unsigned long flags;
13366dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1337f9c78b2bSJens Axboe 
13382b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
133924d5493fSChristoph Hellwig 		if (!PageDirty(bvec->bv_page) && !PageCompound(bvec->bv_page))
134024d5493fSChristoph Hellwig 			goto defer;
1341f9c78b2bSJens Axboe 	}
1342f9c78b2bSJens Axboe 
1343d241a95fSChristoph Hellwig 	bio_release_pages(bio, false);
134424d5493fSChristoph Hellwig 	bio_put(bio);
134524d5493fSChristoph Hellwig 	return;
134624d5493fSChristoph Hellwig defer:
1347f9c78b2bSJens Axboe 	spin_lock_irqsave(&bio_dirty_lock, flags);
1348f9c78b2bSJens Axboe 	bio->bi_private = bio_dirty_list;
1349f9c78b2bSJens Axboe 	bio_dirty_list = bio;
1350f9c78b2bSJens Axboe 	spin_unlock_irqrestore(&bio_dirty_lock, flags);
1351f9c78b2bSJens Axboe 	schedule_work(&bio_dirty_work);
1352f9c78b2bSJens Axboe }
1353f9c78b2bSJens Axboe 
1354c4cf5261SJens Axboe static inline bool bio_remaining_done(struct bio *bio)
1355c4cf5261SJens Axboe {
1356c4cf5261SJens Axboe 	/*
1357c4cf5261SJens Axboe 	 * If we're not chaining, then ->__bi_remaining is always 1 and
1358c4cf5261SJens Axboe 	 * we always end io on the first invocation.
1359c4cf5261SJens Axboe 	 */
1360c4cf5261SJens Axboe 	if (!bio_flagged(bio, BIO_CHAIN))
1361c4cf5261SJens Axboe 		return true;
1362c4cf5261SJens Axboe 
1363c4cf5261SJens Axboe 	BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
1364c4cf5261SJens Axboe 
1365326e1dbbSMike Snitzer 	if (atomic_dec_and_test(&bio->__bi_remaining)) {
1366b7c44ed9SJens Axboe 		bio_clear_flag(bio, BIO_CHAIN);
1367c4cf5261SJens Axboe 		return true;
1368326e1dbbSMike Snitzer 	}
1369c4cf5261SJens Axboe 
1370c4cf5261SJens Axboe 	return false;
1371c4cf5261SJens Axboe }
1372c4cf5261SJens Axboe 
1373f9c78b2bSJens Axboe /**
1374f9c78b2bSJens Axboe  * bio_endio - end I/O on a bio
1375f9c78b2bSJens Axboe  * @bio:	bio
1376f9c78b2bSJens Axboe  *
1377f9c78b2bSJens Axboe  * Description:
13784246a0b6SChristoph Hellwig  *   bio_endio() will end I/O on the whole bio. bio_endio() is the preferred
13794246a0b6SChristoph Hellwig  *   way to end I/O on a bio. No one should call bi_end_io() directly on a
13804246a0b6SChristoph Hellwig  *   bio unless they own it and thus know that it has an end_io function.
1381fbbaf700SNeilBrown  *
1382fbbaf700SNeilBrown  *   bio_endio() can be called several times on a bio that has been chained
1383fbbaf700SNeilBrown  *   using bio_chain().  The ->bi_end_io() function will only be called the
1384fbbaf700SNeilBrown  *   last time.  At this point the BLK_TA_COMPLETE tracing event will be
1385fbbaf700SNeilBrown  *   generated if BIO_TRACE_COMPLETION is set.
1386f9c78b2bSJens Axboe  **/
13874246a0b6SChristoph Hellwig void bio_endio(struct bio *bio)
1388f9c78b2bSJens Axboe {
1389ba8c6967SChristoph Hellwig again:
13902b885517SChristoph Hellwig 	if (!bio_remaining_done(bio))
1391ba8c6967SChristoph Hellwig 		return;
13927c20f116SChristoph Hellwig 	if (!bio_integrity_endio(bio))
13937c20f116SChristoph Hellwig 		return;
1394f9c78b2bSJens Axboe 
1395309dca30SChristoph Hellwig 	if (bio->bi_bdev)
1396309dca30SChristoph Hellwig 		rq_qos_done_bio(bio->bi_bdev->bd_disk->queue, bio);
139767b42d0bSJosef Bacik 
1398f9c78b2bSJens Axboe 	/*
1399ba8c6967SChristoph Hellwig 	 * Need to have a real endio function for chained bios, otherwise
1400ba8c6967SChristoph Hellwig 	 * various corner cases will break (like stacking block devices that
1401ba8c6967SChristoph Hellwig 	 * save/restore bi_end_io) - however, we want to avoid unbounded
1402ba8c6967SChristoph Hellwig 	 * recursion and blowing the stack. Tail call optimization would
1403ba8c6967SChristoph Hellwig 	 * handle this, but compiling with frame pointers also disables
1404ba8c6967SChristoph Hellwig 	 * gcc's sibling call optimization.
1405f9c78b2bSJens Axboe 	 */
1406f9c78b2bSJens Axboe 	if (bio->bi_end_io == bio_chain_endio) {
140738f8baaeSChristoph Hellwig 		bio = __bio_chain_endio(bio);
1408ba8c6967SChristoph Hellwig 		goto again;
1409ba8c6967SChristoph Hellwig 	}
1410ba8c6967SChristoph Hellwig 
1411309dca30SChristoph Hellwig 	if (bio->bi_bdev && bio_flagged(bio, BIO_TRACE_COMPLETION)) {
1412309dca30SChristoph Hellwig 		trace_block_bio_complete(bio->bi_bdev->bd_disk->queue, bio);
1413fbbaf700SNeilBrown 		bio_clear_flag(bio, BIO_TRACE_COMPLETION);
1414fbbaf700SNeilBrown 	}
1415fbbaf700SNeilBrown 
14169e234eeaSShaohua Li 	blk_throtl_bio_endio(bio);
1417b222dd2fSShaohua Li 	/* release cgroup info */
1418b222dd2fSShaohua Li 	bio_uninit(bio);
1419f9c78b2bSJens Axboe 	if (bio->bi_end_io)
14204246a0b6SChristoph Hellwig 		bio->bi_end_io(bio);
1421f9c78b2bSJens Axboe }
1422f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_endio);
1423f9c78b2bSJens Axboe 
1424f9c78b2bSJens Axboe /**
1425f9c78b2bSJens Axboe  * bio_split - split a bio
1426f9c78b2bSJens Axboe  * @bio:	bio to split
1427f9c78b2bSJens Axboe  * @sectors:	number of sectors to split from the front of @bio
1428f9c78b2bSJens Axboe  * @gfp:	gfp mask
1429f9c78b2bSJens Axboe  * @bs:		bio set to allocate from
1430f9c78b2bSJens Axboe  *
1431f9c78b2bSJens Axboe  * Allocates and returns a new bio which represents @sectors from the start of
1432f9c78b2bSJens Axboe  * @bio, and updates @bio to represent the remaining sectors.
1433f9c78b2bSJens Axboe  *
1434f3f5da62SMartin K. Petersen  * Unless this is a discard request the newly allocated bio will point
1435dad77584SBart Van Assche  * to @bio's bi_io_vec. It is the caller's responsibility to ensure that
1436dad77584SBart Van Assche  * neither @bio nor @bs are freed before the split bio.
1437f9c78b2bSJens Axboe  */
1438f9c78b2bSJens Axboe struct bio *bio_split(struct bio *bio, int sectors,
1439f9c78b2bSJens Axboe 		      gfp_t gfp, struct bio_set *bs)
1440f9c78b2bSJens Axboe {
1441f341a4d3SMikulas Patocka 	struct bio *split;
1442f9c78b2bSJens Axboe 
1443f9c78b2bSJens Axboe 	BUG_ON(sectors <= 0);
1444f9c78b2bSJens Axboe 	BUG_ON(sectors >= bio_sectors(bio));
1445f9c78b2bSJens Axboe 
14460512a75bSKeith Busch 	/* Zone append commands cannot be split */
14470512a75bSKeith Busch 	if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
14480512a75bSKeith Busch 		return NULL;
14490512a75bSKeith Busch 
1450f9c78b2bSJens Axboe 	split = bio_clone_fast(bio, gfp, bs);
1451f9c78b2bSJens Axboe 	if (!split)
1452f9c78b2bSJens Axboe 		return NULL;
1453f9c78b2bSJens Axboe 
1454f9c78b2bSJens Axboe 	split->bi_iter.bi_size = sectors << 9;
1455f9c78b2bSJens Axboe 
1456f9c78b2bSJens Axboe 	if (bio_integrity(split))
1457fbd08e76SDmitry Monakhov 		bio_integrity_trim(split);
1458f9c78b2bSJens Axboe 
1459f9c78b2bSJens Axboe 	bio_advance(bio, split->bi_iter.bi_size);
1460f9c78b2bSJens Axboe 
1461fbbaf700SNeilBrown 	if (bio_flagged(bio, BIO_TRACE_COMPLETION))
146220d59023SGoldwyn Rodrigues 		bio_set_flag(split, BIO_TRACE_COMPLETION);
1463fbbaf700SNeilBrown 
1464f9c78b2bSJens Axboe 	return split;
1465f9c78b2bSJens Axboe }
1466f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_split);
1467f9c78b2bSJens Axboe 
1468f9c78b2bSJens Axboe /**
1469f9c78b2bSJens Axboe  * bio_trim - trim a bio
1470f9c78b2bSJens Axboe  * @bio:	bio to trim
1471f9c78b2bSJens Axboe  * @offset:	number of sectors to trim from the front of @bio
1472f9c78b2bSJens Axboe  * @size:	size we want to trim @bio to, in sectors
1473f9c78b2bSJens Axboe  */
1474f9c78b2bSJens Axboe void bio_trim(struct bio *bio, int offset, int size)
1475f9c78b2bSJens Axboe {
1476f9c78b2bSJens Axboe 	/* 'bio' is a cloned bio which we need to trim to match
1477f9c78b2bSJens Axboe 	 * the given offset and size.
1478f9c78b2bSJens Axboe 	 */
1479f9c78b2bSJens Axboe 
1480f9c78b2bSJens Axboe 	size <<= 9;
1481f9c78b2bSJens Axboe 	if (offset == 0 && size == bio->bi_iter.bi_size)
1482f9c78b2bSJens Axboe 		return;
1483f9c78b2bSJens Axboe 
1484f9c78b2bSJens Axboe 	bio_advance(bio, offset << 9);
1485f9c78b2bSJens Axboe 	bio->bi_iter.bi_size = size;
1486376a78abSDmitry Monakhov 
1487376a78abSDmitry Monakhov 	if (bio_integrity(bio))
1488fbd08e76SDmitry Monakhov 		bio_integrity_trim(bio);
1489376a78abSDmitry Monakhov 
1490f9c78b2bSJens Axboe }
1491f9c78b2bSJens Axboe EXPORT_SYMBOL_GPL(bio_trim);
1492f9c78b2bSJens Axboe 
1493f9c78b2bSJens Axboe /*
1494f9c78b2bSJens Axboe  * create memory pools for biovec's in a bio_set.
1495f9c78b2bSJens Axboe  * use the global biovec slabs created for general use.
1496f9c78b2bSJens Axboe  */
14978aa6ba2fSKent Overstreet int biovec_init_pool(mempool_t *pool, int pool_entries)
1498f9c78b2bSJens Axboe {
1499ed996a52SChristoph Hellwig 	struct biovec_slab *bp = bvec_slabs + BVEC_POOL_MAX;
1500f9c78b2bSJens Axboe 
15018aa6ba2fSKent Overstreet 	return mempool_init_slab_pool(pool, pool_entries, bp->slab);
1502f9c78b2bSJens Axboe }
1503f9c78b2bSJens Axboe 
1504917a38c7SKent Overstreet /*
1505917a38c7SKent Overstreet  * bioset_exit - exit a bioset initialized with bioset_init()
1506917a38c7SKent Overstreet  *
1507917a38c7SKent Overstreet  * May be called on a zeroed but uninitialized bioset (i.e. allocated with
1508917a38c7SKent Overstreet  * kzalloc()).
1509917a38c7SKent Overstreet  */
1510917a38c7SKent Overstreet void bioset_exit(struct bio_set *bs)
1511f9c78b2bSJens Axboe {
1512f9c78b2bSJens Axboe 	if (bs->rescue_workqueue)
1513f9c78b2bSJens Axboe 		destroy_workqueue(bs->rescue_workqueue);
1514917a38c7SKent Overstreet 	bs->rescue_workqueue = NULL;
1515f9c78b2bSJens Axboe 
15168aa6ba2fSKent Overstreet 	mempool_exit(&bs->bio_pool);
15178aa6ba2fSKent Overstreet 	mempool_exit(&bs->bvec_pool);
1518f9c78b2bSJens Axboe 
1519f9c78b2bSJens Axboe 	bioset_integrity_free(bs);
1520917a38c7SKent Overstreet 	if (bs->bio_slab)
1521f9c78b2bSJens Axboe 		bio_put_slab(bs);
1522917a38c7SKent Overstreet 	bs->bio_slab = NULL;
1523917a38c7SKent Overstreet }
1524917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_exit);
1525f9c78b2bSJens Axboe 
1526011067b0SNeilBrown /**
1527917a38c7SKent Overstreet  * bioset_init - Initialize a bio_set
1528dad08527SKent Overstreet  * @bs:		pool to initialize
1529917a38c7SKent Overstreet  * @pool_size:	Number of bio and bio_vecs to cache in the mempool
1530917a38c7SKent Overstreet  * @front_pad:	Number of bytes to allocate in front of the returned bio
1531917a38c7SKent Overstreet  * @flags:	Flags to modify behavior, currently %BIOSET_NEED_BVECS
1532917a38c7SKent Overstreet  *              and %BIOSET_NEED_RESCUER
1533917a38c7SKent Overstreet  *
1534dad08527SKent Overstreet  * Description:
1535dad08527SKent Overstreet  *    Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
1536dad08527SKent Overstreet  *    to ask for a number of bytes to be allocated in front of the bio.
1537dad08527SKent Overstreet  *    Front pad allocation is useful for embedding the bio inside
1538dad08527SKent Overstreet  *    another structure, to avoid allocating extra data to go with the bio.
1539dad08527SKent Overstreet  *    Note that the bio must be embedded at the END of that structure always,
1540dad08527SKent Overstreet  *    or things will break badly.
1541dad08527SKent Overstreet  *    If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated
1542dad08527SKent Overstreet  *    for allocating iovecs.  This pool is not needed e.g. for bio_clone_fast().
1543dad08527SKent Overstreet  *    If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used to
1544dad08527SKent Overstreet  *    dispatch queued requests when the mempool runs out of space.
1545dad08527SKent Overstreet  *
1546917a38c7SKent Overstreet  */
1547917a38c7SKent Overstreet int bioset_init(struct bio_set *bs,
1548917a38c7SKent Overstreet 		unsigned int pool_size,
1549917a38c7SKent Overstreet 		unsigned int front_pad,
1550917a38c7SKent Overstreet 		int flags)
1551917a38c7SKent Overstreet {
1552917a38c7SKent Overstreet 	bs->front_pad = front_pad;
15539f180e31SMing Lei 	if (flags & BIOSET_NEED_BVECS)
15549f180e31SMing Lei 		bs->back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
15559f180e31SMing Lei 	else
15569f180e31SMing Lei 		bs->back_pad = 0;
1557917a38c7SKent Overstreet 
1558917a38c7SKent Overstreet 	spin_lock_init(&bs->rescue_lock);
1559917a38c7SKent Overstreet 	bio_list_init(&bs->rescue_list);
1560917a38c7SKent Overstreet 	INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1561917a38c7SKent Overstreet 
156249d1ec85SMing Lei 	bs->bio_slab = bio_find_or_create_slab(bs);
1563917a38c7SKent Overstreet 	if (!bs->bio_slab)
1564917a38c7SKent Overstreet 		return -ENOMEM;
1565917a38c7SKent Overstreet 
1566917a38c7SKent Overstreet 	if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab))
1567917a38c7SKent Overstreet 		goto bad;
1568917a38c7SKent Overstreet 
1569917a38c7SKent Overstreet 	if ((flags & BIOSET_NEED_BVECS) &&
1570917a38c7SKent Overstreet 	    biovec_init_pool(&bs->bvec_pool, pool_size))
1571917a38c7SKent Overstreet 		goto bad;
1572917a38c7SKent Overstreet 
1573917a38c7SKent Overstreet 	if (!(flags & BIOSET_NEED_RESCUER))
1574917a38c7SKent Overstreet 		return 0;
1575917a38c7SKent Overstreet 
1576917a38c7SKent Overstreet 	bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
1577917a38c7SKent Overstreet 	if (!bs->rescue_workqueue)
1578917a38c7SKent Overstreet 		goto bad;
1579917a38c7SKent Overstreet 
1580917a38c7SKent Overstreet 	return 0;
1581917a38c7SKent Overstreet bad:
1582917a38c7SKent Overstreet 	bioset_exit(bs);
1583917a38c7SKent Overstreet 	return -ENOMEM;
1584917a38c7SKent Overstreet }
1585917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_init);
1586917a38c7SKent Overstreet 
158728e89fd9SJens Axboe /*
158828e89fd9SJens Axboe  * Initialize and setup a new bio_set, based on the settings from
158928e89fd9SJens Axboe  * another bio_set.
159028e89fd9SJens Axboe  */
159128e89fd9SJens Axboe int bioset_init_from_src(struct bio_set *bs, struct bio_set *src)
159228e89fd9SJens Axboe {
159328e89fd9SJens Axboe 	int flags;
159428e89fd9SJens Axboe 
159528e89fd9SJens Axboe 	flags = 0;
159628e89fd9SJens Axboe 	if (src->bvec_pool.min_nr)
159728e89fd9SJens Axboe 		flags |= BIOSET_NEED_BVECS;
159828e89fd9SJens Axboe 	if (src->rescue_workqueue)
159928e89fd9SJens Axboe 		flags |= BIOSET_NEED_RESCUER;
160028e89fd9SJens Axboe 
160128e89fd9SJens Axboe 	return bioset_init(bs, src->bio_pool.min_nr, src->front_pad, flags);
160228e89fd9SJens Axboe }
160328e89fd9SJens Axboe EXPORT_SYMBOL(bioset_init_from_src);
160428e89fd9SJens Axboe 
1605de76fd89SChristoph Hellwig static int __init init_bio(void)
1606f9c78b2bSJens Axboe {
1607f9c78b2bSJens Axboe 	int i;
1608f9c78b2bSJens Axboe 
16092b24e6f6SJohannes Thumshirn 	BUILD_BUG_ON(BIO_FLAG_LAST > BVEC_POOL_OFFSET);
16102b24e6f6SJohannes Thumshirn 
1611f9c78b2bSJens Axboe 	bio_integrity_init();
1612de76fd89SChristoph Hellwig 
1613de76fd89SChristoph Hellwig 	for (i = 0; i < ARRAY_SIZE(bvec_slabs); i++) {
1614de76fd89SChristoph Hellwig 		struct biovec_slab *bvs = bvec_slabs + i;
1615de76fd89SChristoph Hellwig 
1616de76fd89SChristoph Hellwig 		bvs->slab = kmem_cache_create(bvs->name,
1617de76fd89SChristoph Hellwig 				bvs->nr_vecs * sizeof(struct bio_vec), 0,
1618de76fd89SChristoph Hellwig 				SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
1619de76fd89SChristoph Hellwig 	}
1620f9c78b2bSJens Axboe 
1621f4f8154aSKent Overstreet 	if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS))
1622f9c78b2bSJens Axboe 		panic("bio: can't allocate bios\n");
1623f9c78b2bSJens Axboe 
1624f4f8154aSKent Overstreet 	if (bioset_integrity_create(&fs_bio_set, BIO_POOL_SIZE))
1625f9c78b2bSJens Axboe 		panic("bio: can't create integrity pool\n");
1626f9c78b2bSJens Axboe 
1627f9c78b2bSJens Axboe 	return 0;
1628f9c78b2bSJens Axboe }
1629f9c78b2bSJens Axboe subsys_initcall(init_bio);
1630