1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk>
4 */
5 #include <linux/mm.h>
6 #include <linux/swap.h>
7 #include <linux/bio-integrity.h>
8 #include <linux/blkdev.h>
9 #include <linux/uio.h>
10 #include <linux/iocontext.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/mempool.h>
16 #include <linux/workqueue.h>
17 #include <linux/cgroup.h>
18 #include <linux/highmem.h>
19 #include <linux/blk-crypto.h>
20 #include <linux/xarray.h>
21
22 #include <trace/events/block.h>
23 #include "blk.h"
24 #include "blk-rq-qos.h"
25 #include "blk-cgroup.h"
26
27 #define ALLOC_CACHE_THRESHOLD 16
28 #define ALLOC_CACHE_MAX 256
29
30 struct bio_alloc_cache {
31 struct bio *free_list;
32 struct bio *free_list_irq;
33 unsigned int nr;
34 unsigned int nr_irq;
35 };
36
37 static struct biovec_slab {
38 int nr_vecs;
39 char *name;
40 struct kmem_cache *slab;
41 } bvec_slabs[] __read_mostly = {
42 { .nr_vecs = 16, .name = "biovec-16" },
43 { .nr_vecs = 64, .name = "biovec-64" },
44 { .nr_vecs = 128, .name = "biovec-128" },
45 { .nr_vecs = BIO_MAX_VECS, .name = "biovec-max" },
46 };
47
biovec_slab(unsigned short nr_vecs)48 static struct biovec_slab *biovec_slab(unsigned short nr_vecs)
49 {
50 switch (nr_vecs) {
51 /* smaller bios use inline vecs */
52 case 5 ... 16:
53 return &bvec_slabs[0];
54 case 17 ... 64:
55 return &bvec_slabs[1];
56 case 65 ... 128:
57 return &bvec_slabs[2];
58 case 129 ... BIO_MAX_VECS:
59 return &bvec_slabs[3];
60 default:
61 BUG();
62 return NULL;
63 }
64 }
65
66 /*
67 * fs_bio_set is the bio_set containing bio and iovec memory pools used by
68 * IO code that does not need private memory pools.
69 */
70 struct bio_set fs_bio_set;
71 EXPORT_SYMBOL(fs_bio_set);
72
73 /*
74 * Our slab pool management
75 */
76 struct bio_slab {
77 struct kmem_cache *slab;
78 unsigned int slab_ref;
79 unsigned int slab_size;
80 char name[12];
81 };
82 static DEFINE_MUTEX(bio_slab_lock);
83 static DEFINE_XARRAY(bio_slabs);
84
create_bio_slab(unsigned int size)85 static struct bio_slab *create_bio_slab(unsigned int size)
86 {
87 struct bio_slab *bslab = kzalloc_obj(*bslab);
88
89 if (!bslab)
90 return NULL;
91
92 snprintf(bslab->name, sizeof(bslab->name), "bio-%d", size);
93 bslab->slab = kmem_cache_create(bslab->name, size,
94 ARCH_KMALLOC_MINALIGN,
95 SLAB_HWCACHE_ALIGN | SLAB_TYPESAFE_BY_RCU, NULL);
96 if (!bslab->slab)
97 goto fail_alloc_slab;
98
99 bslab->slab_ref = 1;
100 bslab->slab_size = size;
101
102 if (!xa_err(xa_store(&bio_slabs, size, bslab, GFP_KERNEL)))
103 return bslab;
104
105 kmem_cache_destroy(bslab->slab);
106
107 fail_alloc_slab:
108 kfree(bslab);
109 return NULL;
110 }
111
bs_bio_slab_size(struct bio_set * bs)112 static inline unsigned int bs_bio_slab_size(struct bio_set *bs)
113 {
114 return bs->front_pad + sizeof(struct bio) + bs->back_pad;
115 }
116
bio_find_or_create_slab(struct bio_set * bs)117 static struct kmem_cache *bio_find_or_create_slab(struct bio_set *bs)
118 {
119 unsigned int size = bs_bio_slab_size(bs);
120 struct bio_slab *bslab;
121
122 mutex_lock(&bio_slab_lock);
123 bslab = xa_load(&bio_slabs, size);
124 if (bslab)
125 bslab->slab_ref++;
126 else
127 bslab = create_bio_slab(size);
128 mutex_unlock(&bio_slab_lock);
129
130 if (bslab)
131 return bslab->slab;
132 return NULL;
133 }
134
bio_put_slab(struct bio_set * bs)135 static void bio_put_slab(struct bio_set *bs)
136 {
137 struct bio_slab *bslab = NULL;
138 unsigned int slab_size = bs_bio_slab_size(bs);
139
140 mutex_lock(&bio_slab_lock);
141
142 bslab = xa_load(&bio_slabs, slab_size);
143 if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
144 goto out;
145
146 WARN_ON_ONCE(bslab->slab != bs->bio_slab);
147
148 WARN_ON(!bslab->slab_ref);
149
150 if (--bslab->slab_ref)
151 goto out;
152
153 xa_erase(&bio_slabs, slab_size);
154
155 kmem_cache_destroy(bslab->slab);
156 kfree(bslab);
157
158 out:
159 mutex_unlock(&bio_slab_lock);
160 }
161
bvec_free(mempool_t * pool,struct bio_vec * bv,unsigned short nr_vecs)162 void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned short nr_vecs)
163 {
164 BUG_ON(nr_vecs > BIO_MAX_VECS);
165
166 if (nr_vecs == BIO_MAX_VECS)
167 mempool_free(bv, pool);
168 else if (nr_vecs > BIO_INLINE_VECS)
169 kmem_cache_free(biovec_slab(nr_vecs)->slab, bv);
170 }
171
172 /*
173 * Make the first allocation restricted and don't dump info on allocation
174 * failures, since we'll fall back to the mempool in case of failure.
175 */
bvec_alloc_gfp(gfp_t gfp)176 static inline gfp_t bvec_alloc_gfp(gfp_t gfp)
177 {
178 return (gfp & ~(__GFP_DIRECT_RECLAIM | __GFP_IO)) |
179 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
180 }
181
bvec_alloc(mempool_t * pool,unsigned short * nr_vecs,gfp_t gfp_mask)182 struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs,
183 gfp_t gfp_mask)
184 {
185 struct biovec_slab *bvs = biovec_slab(*nr_vecs);
186
187 if (WARN_ON_ONCE(!bvs))
188 return NULL;
189
190 /*
191 * Upgrade the nr_vecs request to take full advantage of the allocation.
192 * We also rely on this in the bvec_free path.
193 */
194 *nr_vecs = bvs->nr_vecs;
195
196 /*
197 * Try a slab allocation first for all smaller allocations. If that
198 * fails and __GFP_DIRECT_RECLAIM is set retry with the mempool.
199 * The mempool is sized to handle up to BIO_MAX_VECS entries.
200 */
201 if (*nr_vecs < BIO_MAX_VECS) {
202 struct bio_vec *bvl;
203
204 bvl = kmem_cache_alloc(bvs->slab, bvec_alloc_gfp(gfp_mask));
205 if (likely(bvl) || !(gfp_mask & __GFP_DIRECT_RECLAIM))
206 return bvl;
207 *nr_vecs = BIO_MAX_VECS;
208 }
209
210 return mempool_alloc(pool, gfp_mask);
211 }
212
bio_uninit(struct bio * bio)213 void bio_uninit(struct bio *bio)
214 {
215 #ifdef CONFIG_BLK_CGROUP
216 if (bio->bi_blkg) {
217 blkg_put(bio->bi_blkg);
218 bio->bi_blkg = NULL;
219 }
220 #endif
221 if (bio_integrity(bio))
222 bio_integrity_free(bio);
223
224 bio_crypt_free_ctx(bio);
225 }
226 EXPORT_SYMBOL(bio_uninit);
227
bio_free(struct bio * bio)228 static void bio_free(struct bio *bio)
229 {
230 struct bio_set *bs = bio->bi_pool;
231 void *p = bio;
232
233 WARN_ON_ONCE(!bs);
234
235 bio_uninit(bio);
236 bvec_free(&bs->bvec_pool, bio->bi_io_vec, bio->bi_max_vecs);
237 mempool_free(p - bs->front_pad, &bs->bio_pool);
238 }
239
240 /*
241 * Users of this function have their own bio allocation. Subsequently,
242 * they must remember to pair any call to bio_init() with bio_uninit()
243 * when IO has completed, or when the bio is released.
244 */
bio_init(struct bio * bio,struct block_device * bdev,struct bio_vec * table,unsigned short max_vecs,blk_opf_t opf)245 void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *table,
246 unsigned short max_vecs, blk_opf_t opf)
247 {
248 bio->bi_next = NULL;
249 bio->bi_bdev = bdev;
250 bio->bi_opf = opf;
251 bio->bi_flags = 0;
252 bio->bi_ioprio = 0;
253 bio->bi_write_hint = 0;
254 bio->bi_write_stream = 0;
255 bio->bi_status = 0;
256 bio->bi_bvec_gap_bit = 0;
257 bio->bi_iter.bi_sector = 0;
258 bio->bi_iter.bi_size = 0;
259 bio->bi_iter.bi_idx = 0;
260 bio->bi_iter.bi_bvec_done = 0;
261 bio->bi_end_io = NULL;
262 bio->bi_private = NULL;
263 #ifdef CONFIG_BLK_CGROUP
264 bio->bi_blkg = NULL;
265 bio->issue_time_ns = 0;
266 if (bdev)
267 bio_associate_blkg(bio);
268 #ifdef CONFIG_BLK_CGROUP_IOCOST
269 bio->bi_iocost_cost = 0;
270 #endif
271 #endif
272 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
273 bio->bi_crypt_context = NULL;
274 #endif
275 #ifdef CONFIG_BLK_DEV_INTEGRITY
276 bio->bi_integrity = NULL;
277 #endif
278 bio->bi_vcnt = 0;
279
280 atomic_set(&bio->__bi_remaining, 1);
281 atomic_set(&bio->__bi_cnt, 1);
282 bio->bi_cookie = BLK_QC_T_NONE;
283
284 bio->bi_max_vecs = max_vecs;
285 bio->bi_io_vec = table;
286 bio->bi_pool = NULL;
287 }
288 EXPORT_SYMBOL(bio_init);
289
290 /**
291 * bio_reset - reinitialize a bio
292 * @bio: bio to reset
293 * @bdev: block device to use the bio for
294 * @opf: operation and flags for bio
295 *
296 * Description:
297 * After calling bio_reset(), @bio will be in the same state as a freshly
298 * allocated bio returned bio bio_alloc_bioset() - the only fields that are
299 * preserved are the ones that are initialized by bio_alloc_bioset(). See
300 * comment in struct bio.
301 */
bio_reset(struct bio * bio,struct block_device * bdev,blk_opf_t opf)302 void bio_reset(struct bio *bio, struct block_device *bdev, blk_opf_t opf)
303 {
304 struct bio_vec *bv = bio->bi_io_vec;
305
306 bio_uninit(bio);
307 memset(bio, 0, BIO_RESET_BYTES);
308 atomic_set(&bio->__bi_remaining, 1);
309 bio->bi_io_vec = bv;
310 bio->bi_bdev = bdev;
311 if (bio->bi_bdev)
312 bio_associate_blkg(bio);
313 bio->bi_opf = opf;
314 }
315 EXPORT_SYMBOL(bio_reset);
316
317 /**
318 * bio_reuse - reuse a bio with the payload left intact
319 * @bio: bio to reuse
320 * @opf: operation and flags for the next I/O
321 *
322 * Allow reusing an existing bio for another operation with all set up
323 * fields including the payload, device and end_io handler left intact.
324 *
325 * Typically used when @bio is first used to read data which is then written
326 * to another location without modification. @bio must not be in-flight and
327 * owned by the caller. Can't be used for cloned bios.
328 *
329 * Note: Can't be used when @bio has integrity or blk-crypto contexts for now.
330 * Feel free to add that support when you need it, though.
331 */
bio_reuse(struct bio * bio,blk_opf_t opf)332 void bio_reuse(struct bio *bio, blk_opf_t opf)
333 {
334 unsigned short vcnt = bio->bi_vcnt, i;
335 bio_end_io_t *end_io = bio->bi_end_io;
336 void *private = bio->bi_private;
337
338 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
339 WARN_ON_ONCE(bio_integrity(bio));
340 WARN_ON_ONCE(bio_has_crypt_ctx(bio));
341
342 bio_reset(bio, bio->bi_bdev, opf);
343 for (i = 0; i < vcnt; i++)
344 bio->bi_iter.bi_size += bio->bi_io_vec[i].bv_len;
345 bio->bi_vcnt = vcnt;
346 bio->bi_private = private;
347 bio->bi_end_io = end_io;
348 }
349 EXPORT_SYMBOL_GPL(bio_reuse);
350
__bio_chain_endio(struct bio * bio)351 static struct bio *__bio_chain_endio(struct bio *bio)
352 {
353 struct bio *parent = bio->bi_private;
354
355 if (bio->bi_status && !parent->bi_status)
356 parent->bi_status = bio->bi_status;
357 bio_put(bio);
358 return parent;
359 }
360
361 /*
362 * This function should only be used as a flag and must never be called.
363 * If execution reaches here, it indicates a serious programming error.
364 */
bio_chain_endio(struct bio * bio)365 static void bio_chain_endio(struct bio *bio)
366 {
367 BUG();
368 }
369
370 /**
371 * bio_chain - chain bio completions
372 * @bio: the target bio
373 * @parent: the parent bio of @bio
374 *
375 * The caller won't have a bi_end_io called when @bio completes - instead,
376 * @parent's bi_end_io won't be called until both @parent and @bio have
377 * completed; the chained bio will also be freed when it completes.
378 *
379 * The caller must not set bi_private or bi_end_io in @bio.
380 */
bio_chain(struct bio * bio,struct bio * parent)381 void bio_chain(struct bio *bio, struct bio *parent)
382 {
383 BUG_ON(bio->bi_private || bio->bi_end_io);
384
385 bio->bi_private = parent;
386 bio->bi_end_io = bio_chain_endio;
387 bio_inc_remaining(parent);
388 }
389 EXPORT_SYMBOL(bio_chain);
390
391 /**
392 * bio_chain_and_submit - submit a bio after chaining it to another one
393 * @prev: bio to chain and submit
394 * @new: bio to chain to
395 *
396 * If @prev is non-NULL, chain it to @new and submit it.
397 *
398 * Return: @new.
399 */
bio_chain_and_submit(struct bio * prev,struct bio * new)400 struct bio *bio_chain_and_submit(struct bio *prev, struct bio *new)
401 {
402 if (prev) {
403 bio_chain(prev, new);
404 submit_bio(prev);
405 }
406 return new;
407 }
408
blk_next_bio(struct bio * bio,struct block_device * bdev,unsigned int nr_pages,blk_opf_t opf,gfp_t gfp)409 struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev,
410 unsigned int nr_pages, blk_opf_t opf, gfp_t gfp)
411 {
412 return bio_chain_and_submit(bio, bio_alloc(bdev, nr_pages, opf, gfp));
413 }
414 EXPORT_SYMBOL_GPL(blk_next_bio);
415
bio_alloc_rescue(struct work_struct * work)416 static void bio_alloc_rescue(struct work_struct *work)
417 {
418 struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
419 struct bio *bio;
420
421 while (1) {
422 spin_lock(&bs->rescue_lock);
423 bio = bio_list_pop(&bs->rescue_list);
424 spin_unlock(&bs->rescue_lock);
425
426 if (!bio)
427 break;
428
429 submit_bio_noacct(bio);
430 }
431 }
432
punt_bios_to_rescuer(struct bio_set * bs)433 static void punt_bios_to_rescuer(struct bio_set *bs)
434 {
435 struct bio_list punt, nopunt;
436 struct bio *bio;
437
438 if (WARN_ON_ONCE(!bs->rescue_workqueue))
439 return;
440 /*
441 * In order to guarantee forward progress we must punt only bios that
442 * were allocated from this bio_set; otherwise, if there was a bio on
443 * there for a stacking driver higher up in the stack, processing it
444 * could require allocating bios from this bio_set, and doing that from
445 * our own rescuer would be bad.
446 *
447 * Since bio lists are singly linked, pop them all instead of trying to
448 * remove from the middle of the list:
449 */
450
451 bio_list_init(&punt);
452 bio_list_init(&nopunt);
453
454 while ((bio = bio_list_pop(¤t->bio_list[0])))
455 bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
456 current->bio_list[0] = nopunt;
457
458 bio_list_init(&nopunt);
459 while ((bio = bio_list_pop(¤t->bio_list[1])))
460 bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
461 current->bio_list[1] = nopunt;
462
463 spin_lock(&bs->rescue_lock);
464 bio_list_merge(&bs->rescue_list, &punt);
465 spin_unlock(&bs->rescue_lock);
466
467 queue_work(bs->rescue_workqueue, &bs->rescue_work);
468 }
469
bio_alloc_irq_cache_splice(struct bio_alloc_cache * cache)470 static void bio_alloc_irq_cache_splice(struct bio_alloc_cache *cache)
471 {
472 unsigned long flags;
473
474 /* cache->free_list must be empty */
475 if (WARN_ON_ONCE(cache->free_list))
476 return;
477
478 local_irq_save(flags);
479 cache->free_list = cache->free_list_irq;
480 cache->free_list_irq = NULL;
481 cache->nr += cache->nr_irq;
482 cache->nr_irq = 0;
483 local_irq_restore(flags);
484 }
485
bio_alloc_percpu_cache(struct block_device * bdev,unsigned short nr_vecs,blk_opf_t opf,gfp_t gfp,struct bio_set * bs)486 static struct bio *bio_alloc_percpu_cache(struct block_device *bdev,
487 unsigned short nr_vecs, blk_opf_t opf, gfp_t gfp,
488 struct bio_set *bs)
489 {
490 struct bio_alloc_cache *cache;
491 struct bio *bio;
492
493 cache = per_cpu_ptr(bs->cache, get_cpu());
494 if (!cache->free_list) {
495 if (READ_ONCE(cache->nr_irq) >= ALLOC_CACHE_THRESHOLD)
496 bio_alloc_irq_cache_splice(cache);
497 if (!cache->free_list) {
498 put_cpu();
499 return NULL;
500 }
501 }
502 bio = cache->free_list;
503 cache->free_list = bio->bi_next;
504 cache->nr--;
505 put_cpu();
506
507 if (nr_vecs)
508 bio_init_inline(bio, bdev, nr_vecs, opf);
509 else
510 bio_init(bio, bdev, NULL, nr_vecs, opf);
511 bio->bi_pool = bs;
512 return bio;
513 }
514
515 /**
516 * bio_alloc_bioset - allocate a bio for I/O
517 * @bdev: block device to allocate the bio for (can be %NULL)
518 * @nr_vecs: number of bvecs to pre-allocate
519 * @opf: operation and flags for bio
520 * @gfp_mask: the GFP_* mask given to the slab allocator
521 * @bs: the bio_set to allocate from.
522 *
523 * Allocate a bio from the mempools in @bs.
524 *
525 * If %__GFP_DIRECT_RECLAIM is set then bio_alloc will always be able to
526 * allocate a bio. This is due to the mempool guarantees. To make this work,
527 * callers must never allocate more than 1 bio at a time from the general pool.
528 * Callers that need to allocate more than 1 bio must always submit the
529 * previously allocated bio for IO before attempting to allocate a new one.
530 * Failure to do so can cause deadlocks under memory pressure.
531 *
532 * Note that when running under submit_bio_noacct() (i.e. any block driver),
533 * bios are not submitted until after you return - see the code in
534 * submit_bio_noacct() that converts recursion into iteration, to prevent
535 * stack overflows.
536 *
537 * This would normally mean allocating multiple bios under submit_bio_noacct()
538 * would be susceptible to deadlocks, but we have
539 * deadlock avoidance code that resubmits any blocked bios from a rescuer
540 * thread.
541 *
542 * However, we do not guarantee forward progress for allocations from other
543 * mempools. Doing multiple allocations from the same mempool under
544 * submit_bio_noacct() should be avoided - instead, use bio_set's front_pad
545 * for per bio allocations.
546 *
547 * Returns: Pointer to new bio on success, NULL on failure.
548 */
bio_alloc_bioset(struct block_device * bdev,unsigned short nr_vecs,blk_opf_t opf,gfp_t gfp_mask,struct bio_set * bs)549 struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
550 blk_opf_t opf, gfp_t gfp_mask,
551 struct bio_set *bs)
552 {
553 gfp_t saved_gfp = gfp_mask;
554 struct bio *bio;
555 void *p;
556
557 /* should not use nobvec bioset for nr_vecs > 0 */
558 if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && nr_vecs > 0))
559 return NULL;
560
561 if (bs->cache && nr_vecs <= BIO_INLINE_VECS) {
562 opf |= REQ_ALLOC_CACHE;
563 bio = bio_alloc_percpu_cache(bdev, nr_vecs, opf,
564 gfp_mask, bs);
565 if (bio)
566 return bio;
567 /*
568 * No cached bio available, bio returned below marked with
569 * REQ_ALLOC_CACHE to participate in per-cpu alloc cache.
570 */
571 } else
572 opf &= ~REQ_ALLOC_CACHE;
573
574 /*
575 * submit_bio_noacct() converts recursion to iteration; this means if
576 * we're running beneath it, any bios we allocate and submit will not be
577 * submitted (and thus freed) until after we return.
578 *
579 * This exposes us to a potential deadlock if we allocate multiple bios
580 * from the same bio_set() while running underneath submit_bio_noacct().
581 * If we were to allocate multiple bios (say a stacking block driver
582 * that was splitting bios), we would deadlock if we exhausted the
583 * mempool's reserve.
584 *
585 * We solve this, and guarantee forward progress, with a rescuer
586 * workqueue per bio_set. If we go to allocate and there are bios on
587 * current->bio_list, we first try the allocation without
588 * __GFP_DIRECT_RECLAIM; if that fails, we punt those bios we would be
589 * blocking to the rescuer workqueue before we retry with the original
590 * gfp_flags.
591 */
592 if (current->bio_list &&
593 (!bio_list_empty(¤t->bio_list[0]) ||
594 !bio_list_empty(¤t->bio_list[1])) &&
595 bs->rescue_workqueue)
596 gfp_mask &= ~__GFP_DIRECT_RECLAIM;
597
598 p = mempool_alloc(&bs->bio_pool, gfp_mask);
599 if (!p && gfp_mask != saved_gfp) {
600 punt_bios_to_rescuer(bs);
601 gfp_mask = saved_gfp;
602 p = mempool_alloc(&bs->bio_pool, gfp_mask);
603 }
604 if (unlikely(!p))
605 return NULL;
606 if (!mempool_is_saturated(&bs->bio_pool))
607 opf &= ~REQ_ALLOC_CACHE;
608
609 bio = p + bs->front_pad;
610 if (nr_vecs > BIO_INLINE_VECS) {
611 struct bio_vec *bvl = NULL;
612
613 bvl = bvec_alloc(&bs->bvec_pool, &nr_vecs, gfp_mask);
614 if (!bvl && gfp_mask != saved_gfp) {
615 punt_bios_to_rescuer(bs);
616 gfp_mask = saved_gfp;
617 bvl = bvec_alloc(&bs->bvec_pool, &nr_vecs, gfp_mask);
618 }
619 if (unlikely(!bvl))
620 goto err_free;
621
622 bio_init(bio, bdev, bvl, nr_vecs, opf);
623 } else if (nr_vecs) {
624 bio_init_inline(bio, bdev, BIO_INLINE_VECS, opf);
625 } else {
626 bio_init(bio, bdev, NULL, 0, opf);
627 }
628
629 bio->bi_pool = bs;
630 return bio;
631
632 err_free:
633 mempool_free(p, &bs->bio_pool);
634 return NULL;
635 }
636 EXPORT_SYMBOL(bio_alloc_bioset);
637
638 /**
639 * bio_kmalloc - kmalloc a bio
640 * @nr_vecs: number of bio_vecs to allocate
641 * @gfp_mask: the GFP_* mask given to the slab allocator
642 *
643 * Use kmalloc to allocate a bio (including bvecs). The bio must be initialized
644 * using bio_init() before use. To free a bio returned from this function use
645 * kfree() after calling bio_uninit(). A bio returned from this function can
646 * be reused by calling bio_uninit() before calling bio_init() again.
647 *
648 * Note that unlike bio_alloc() or bio_alloc_bioset() allocations from this
649 * function are not backed by a mempool can fail. Do not use this function
650 * for allocations in the file system I/O path.
651 *
652 * Returns: Pointer to new bio on success, NULL on failure.
653 */
bio_kmalloc(unsigned short nr_vecs,gfp_t gfp_mask)654 struct bio *bio_kmalloc(unsigned short nr_vecs, gfp_t gfp_mask)
655 {
656 struct bio *bio;
657
658 if (nr_vecs > BIO_MAX_INLINE_VECS)
659 return NULL;
660 return kmalloc(sizeof(*bio) + nr_vecs * sizeof(struct bio_vec),
661 gfp_mask);
662 }
663 EXPORT_SYMBOL(bio_kmalloc);
664
zero_fill_bio_iter(struct bio * bio,struct bvec_iter start)665 void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start)
666 {
667 struct bio_vec bv;
668 struct bvec_iter iter;
669
670 __bio_for_each_segment(bv, bio, iter, start)
671 memzero_bvec(&bv);
672 }
673 EXPORT_SYMBOL(zero_fill_bio_iter);
674
675 /**
676 * bio_truncate - truncate the bio to small size of @new_size
677 * @bio: the bio to be truncated
678 * @new_size: new size for truncating the bio
679 *
680 * Description:
681 * Truncate the bio to new size of @new_size. If bio_op(bio) is
682 * REQ_OP_READ, zero the truncated part. This function should only
683 * be used for handling corner cases, such as bio eod.
684 */
bio_truncate(struct bio * bio,unsigned new_size)685 static void bio_truncate(struct bio *bio, unsigned new_size)
686 {
687 struct bio_vec bv;
688 struct bvec_iter iter;
689 unsigned int done = 0;
690 bool truncated = false;
691
692 if (new_size >= bio->bi_iter.bi_size)
693 return;
694
695 if (bio_op(bio) != REQ_OP_READ)
696 goto exit;
697
698 bio_for_each_segment(bv, bio, iter) {
699 if (done + bv.bv_len > new_size) {
700 size_t offset;
701
702 if (!truncated)
703 offset = new_size - done;
704 else
705 offset = 0;
706 memzero_page(bv.bv_page, bv.bv_offset + offset,
707 bv.bv_len - offset);
708 truncated = true;
709 }
710 done += bv.bv_len;
711 }
712
713 exit:
714 /*
715 * Don't touch bvec table here and make it really immutable, since
716 * fs bio user has to retrieve all pages via bio_for_each_segment_all
717 * in its .end_bio() callback.
718 *
719 * It is enough to truncate bio by updating .bi_size since we can make
720 * correct bvec with the updated .bi_size for drivers.
721 */
722 bio->bi_iter.bi_size = new_size;
723 }
724
725 /**
726 * guard_bio_eod - truncate a BIO to fit the block device
727 * @bio: bio to truncate
728 *
729 * This allows us to do IO even on the odd last sectors of a device, even if the
730 * block size is some multiple of the physical sector size.
731 *
732 * We'll just truncate the bio to the size of the device, and clear the end of
733 * the buffer head manually. Truly out-of-range accesses will turn into actual
734 * I/O errors, this only handles the "we need to be able to do I/O at the final
735 * sector" case.
736 */
guard_bio_eod(struct bio * bio)737 void guard_bio_eod(struct bio *bio)
738 {
739 sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
740
741 if (!maxsector)
742 return;
743
744 /*
745 * If the *whole* IO is past the end of the device,
746 * let it through, and the IO layer will turn it into
747 * an EIO.
748 */
749 if (unlikely(bio->bi_iter.bi_sector >= maxsector))
750 return;
751
752 maxsector -= bio->bi_iter.bi_sector;
753 if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
754 return;
755
756 bio_truncate(bio, maxsector << 9);
757 }
758
__bio_alloc_cache_prune(struct bio_alloc_cache * cache,unsigned int nr)759 static int __bio_alloc_cache_prune(struct bio_alloc_cache *cache,
760 unsigned int nr)
761 {
762 unsigned int i = 0;
763 struct bio *bio;
764
765 while ((bio = cache->free_list) != NULL) {
766 cache->free_list = bio->bi_next;
767 cache->nr--;
768 bio_free(bio);
769 if (++i == nr)
770 break;
771 }
772 return i;
773 }
774
bio_alloc_cache_prune(struct bio_alloc_cache * cache,unsigned int nr)775 static void bio_alloc_cache_prune(struct bio_alloc_cache *cache,
776 unsigned int nr)
777 {
778 nr -= __bio_alloc_cache_prune(cache, nr);
779 if (!READ_ONCE(cache->free_list)) {
780 bio_alloc_irq_cache_splice(cache);
781 __bio_alloc_cache_prune(cache, nr);
782 }
783 }
784
bio_cpu_dead(unsigned int cpu,struct hlist_node * node)785 static int bio_cpu_dead(unsigned int cpu, struct hlist_node *node)
786 {
787 struct bio_set *bs;
788
789 bs = hlist_entry_safe(node, struct bio_set, cpuhp_dead);
790 if (bs->cache) {
791 struct bio_alloc_cache *cache = per_cpu_ptr(bs->cache, cpu);
792
793 bio_alloc_cache_prune(cache, -1U);
794 }
795 return 0;
796 }
797
bio_alloc_cache_destroy(struct bio_set * bs)798 static void bio_alloc_cache_destroy(struct bio_set *bs)
799 {
800 int cpu;
801
802 if (!bs->cache)
803 return;
804
805 cpuhp_state_remove_instance_nocalls(CPUHP_BIO_DEAD, &bs->cpuhp_dead);
806 for_each_possible_cpu(cpu) {
807 struct bio_alloc_cache *cache;
808
809 cache = per_cpu_ptr(bs->cache, cpu);
810 bio_alloc_cache_prune(cache, -1U);
811 }
812 free_percpu(bs->cache);
813 bs->cache = NULL;
814 }
815
bio_put_percpu_cache(struct bio * bio)816 static inline void bio_put_percpu_cache(struct bio *bio)
817 {
818 struct bio_alloc_cache *cache;
819
820 cache = per_cpu_ptr(bio->bi_pool->cache, get_cpu());
821 if (READ_ONCE(cache->nr_irq) + cache->nr > ALLOC_CACHE_MAX)
822 goto out_free;
823
824 if (in_task()) {
825 bio_uninit(bio);
826 bio->bi_next = cache->free_list;
827 /* Not necessary but helps not to iopoll already freed bios */
828 bio->bi_bdev = NULL;
829 cache->free_list = bio;
830 cache->nr++;
831 } else if (in_hardirq()) {
832 lockdep_assert_irqs_disabled();
833
834 bio_uninit(bio);
835 bio->bi_next = cache->free_list_irq;
836 cache->free_list_irq = bio;
837 cache->nr_irq++;
838 } else {
839 goto out_free;
840 }
841 put_cpu();
842 return;
843 out_free:
844 put_cpu();
845 bio_free(bio);
846 }
847
848 /**
849 * bio_put - release a reference to a bio
850 * @bio: bio to release reference to
851 *
852 * Description:
853 * Put a reference to a &struct bio, either one you have gotten with
854 * bio_alloc, bio_get or bio_clone_*. The last put of a bio will free it.
855 **/
bio_put(struct bio * bio)856 void bio_put(struct bio *bio)
857 {
858 if (unlikely(bio_flagged(bio, BIO_REFFED))) {
859 BUG_ON(!atomic_read(&bio->__bi_cnt));
860 if (!atomic_dec_and_test(&bio->__bi_cnt))
861 return;
862 }
863 if (bio->bi_opf & REQ_ALLOC_CACHE)
864 bio_put_percpu_cache(bio);
865 else
866 bio_free(bio);
867 }
868 EXPORT_SYMBOL(bio_put);
869
__bio_clone(struct bio * bio,struct bio * bio_src,gfp_t gfp)870 static int __bio_clone(struct bio *bio, struct bio *bio_src, gfp_t gfp)
871 {
872 bio_set_flag(bio, BIO_CLONED);
873 bio->bi_ioprio = bio_src->bi_ioprio;
874 bio->bi_write_hint = bio_src->bi_write_hint;
875 bio->bi_write_stream = bio_src->bi_write_stream;
876 bio->bi_iter = bio_src->bi_iter;
877
878 if (bio->bi_bdev) {
879 if (bio->bi_bdev == bio_src->bi_bdev &&
880 bio_flagged(bio_src, BIO_REMAPPED))
881 bio_set_flag(bio, BIO_REMAPPED);
882 bio_clone_blkg_association(bio, bio_src);
883 }
884
885 if (bio_crypt_clone(bio, bio_src, gfp) < 0)
886 return -ENOMEM;
887 if (bio_integrity(bio_src) &&
888 bio_integrity_clone(bio, bio_src, gfp) < 0)
889 return -ENOMEM;
890 return 0;
891 }
892
893 /**
894 * bio_alloc_clone - clone a bio that shares the original bio's biovec
895 * @bdev: block_device to clone onto
896 * @bio_src: bio to clone from
897 * @gfp: allocation priority
898 * @bs: bio_set to allocate from
899 *
900 * Allocate a new bio that is a clone of @bio_src. The caller owns the returned
901 * bio, but not the actual data it points to.
902 *
903 * The caller must ensure that the return bio is not freed before @bio_src.
904 */
bio_alloc_clone(struct block_device * bdev,struct bio * bio_src,gfp_t gfp,struct bio_set * bs)905 struct bio *bio_alloc_clone(struct block_device *bdev, struct bio *bio_src,
906 gfp_t gfp, struct bio_set *bs)
907 {
908 struct bio *bio;
909
910 bio = bio_alloc_bioset(bdev, 0, bio_src->bi_opf, gfp, bs);
911 if (!bio)
912 return NULL;
913
914 if (__bio_clone(bio, bio_src, gfp) < 0) {
915 bio_put(bio);
916 return NULL;
917 }
918 bio->bi_io_vec = bio_src->bi_io_vec;
919
920 return bio;
921 }
922 EXPORT_SYMBOL(bio_alloc_clone);
923
924 /**
925 * bio_init_clone - clone a bio that shares the original bio's biovec
926 * @bdev: block_device to clone onto
927 * @bio: bio to clone into
928 * @bio_src: bio to clone from
929 * @gfp: allocation priority
930 *
931 * Initialize a new bio in caller provided memory that is a clone of @bio_src.
932 * The caller owns the returned bio, but not the actual data it points to.
933 *
934 * The caller must ensure that @bio_src is not freed before @bio.
935 */
bio_init_clone(struct block_device * bdev,struct bio * bio,struct bio * bio_src,gfp_t gfp)936 int bio_init_clone(struct block_device *bdev, struct bio *bio,
937 struct bio *bio_src, gfp_t gfp)
938 {
939 int ret;
940
941 bio_init(bio, bdev, bio_src->bi_io_vec, 0, bio_src->bi_opf);
942 ret = __bio_clone(bio, bio_src, gfp);
943 if (ret)
944 bio_uninit(bio);
945 return ret;
946 }
947 EXPORT_SYMBOL(bio_init_clone);
948
949 /**
950 * bio_full - check if the bio is full
951 * @bio: bio to check
952 * @len: length of one segment to be added
953 *
954 * Return true if @bio is full and one segment with @len bytes can't be
955 * added to the bio, otherwise return false
956 */
bio_full(struct bio * bio,unsigned len)957 static inline bool bio_full(struct bio *bio, unsigned len)
958 {
959 if (bio->bi_vcnt >= bio->bi_max_vecs)
960 return true;
961 if (bio->bi_iter.bi_size > BIO_MAX_SIZE - len)
962 return true;
963 return false;
964 }
965
bvec_try_merge_page(struct bio_vec * bv,struct page * page,unsigned int len,unsigned int off)966 static bool bvec_try_merge_page(struct bio_vec *bv, struct page *page,
967 unsigned int len, unsigned int off)
968 {
969 size_t bv_end = bv->bv_offset + bv->bv_len;
970 phys_addr_t vec_end_addr = page_to_phys(bv->bv_page) + bv_end - 1;
971 phys_addr_t page_addr = page_to_phys(page);
972
973 if (vec_end_addr + 1 != page_addr + off)
974 return false;
975 if (xen_domain() && !xen_biovec_phys_mergeable(bv, page))
976 return false;
977
978 if ((vec_end_addr & PAGE_MASK) != ((page_addr + off) & PAGE_MASK)) {
979 if (IS_ENABLED(CONFIG_KMSAN))
980 return false;
981 if (bv->bv_page + bv_end / PAGE_SIZE != page + off / PAGE_SIZE)
982 return false;
983 }
984
985 bv->bv_len += len;
986 return true;
987 }
988
989 /*
990 * Try to merge a page into a segment, while obeying the hardware segment
991 * size limit.
992 *
993 * This is kept around for the integrity metadata, which is still tries
994 * to build the initial bio to the hardware limit and doesn't have proper
995 * helpers to split. Hopefully this will go away soon.
996 */
bvec_try_merge_hw_page(struct request_queue * q,struct bio_vec * bv,struct page * page,unsigned len,unsigned offset)997 bool bvec_try_merge_hw_page(struct request_queue *q, struct bio_vec *bv,
998 struct page *page, unsigned len, unsigned offset)
999 {
1000 unsigned long mask = queue_segment_boundary(q);
1001 phys_addr_t addr1 = bvec_phys(bv);
1002 phys_addr_t addr2 = page_to_phys(page) + offset + len - 1;
1003
1004 if ((addr1 | mask) != (addr2 | mask))
1005 return false;
1006 if (len > queue_max_segment_size(q) - bv->bv_len)
1007 return false;
1008 return bvec_try_merge_page(bv, page, len, offset);
1009 }
1010
1011 /**
1012 * __bio_add_page - add page(s) to a bio in a new segment
1013 * @bio: destination bio
1014 * @page: start page to add
1015 * @len: length of the data to add, may cross pages
1016 * @off: offset of the data relative to @page, may cross pages
1017 *
1018 * Add the data at @page + @off to @bio as a new bvec. The caller must ensure
1019 * that @bio has space for another bvec.
1020 */
__bio_add_page(struct bio * bio,struct page * page,unsigned int len,unsigned int off)1021 void __bio_add_page(struct bio *bio, struct page *page,
1022 unsigned int len, unsigned int off)
1023 {
1024 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
1025 WARN_ON_ONCE(bio_full(bio, len));
1026
1027 if (is_pci_p2pdma_page(page))
1028 bio->bi_opf |= REQ_NOMERGE;
1029
1030 bvec_set_page(&bio->bi_io_vec[bio->bi_vcnt], page, len, off);
1031 bio->bi_iter.bi_size += len;
1032 bio->bi_vcnt++;
1033 }
1034 EXPORT_SYMBOL_GPL(__bio_add_page);
1035
1036 /**
1037 * bio_add_virt_nofail - add data in the direct kernel mapping to a bio
1038 * @bio: destination bio
1039 * @vaddr: data to add
1040 * @len: length of the data to add, may cross pages
1041 *
1042 * Add the data at @vaddr to @bio. The caller must have ensure a segment
1043 * is available for the added data. No merging into an existing segment
1044 * will be performed.
1045 */
bio_add_virt_nofail(struct bio * bio,void * vaddr,unsigned len)1046 void bio_add_virt_nofail(struct bio *bio, void *vaddr, unsigned len)
1047 {
1048 __bio_add_page(bio, virt_to_page(vaddr), len, offset_in_page(vaddr));
1049 }
1050 EXPORT_SYMBOL_GPL(bio_add_virt_nofail);
1051
1052 /**
1053 * bio_add_page - attempt to add page(s) to bio
1054 * @bio: destination bio
1055 * @page: start page to add
1056 * @len: vec entry length, may cross pages
1057 * @offset: vec entry offset relative to @page, may cross pages
1058 *
1059 * Attempt to add page(s) to the bio_vec maplist. This will only fail
1060 * if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
1061 */
bio_add_page(struct bio * bio,struct page * page,unsigned int len,unsigned int offset)1062 int bio_add_page(struct bio *bio, struct page *page,
1063 unsigned int len, unsigned int offset)
1064 {
1065 if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
1066 return 0;
1067 if (bio->bi_iter.bi_size > BIO_MAX_SIZE - len)
1068 return 0;
1069
1070 if (bio->bi_vcnt > 0) {
1071 struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
1072
1073 if (!zone_device_pages_have_same_pgmap(bv->bv_page, page))
1074 return 0;
1075
1076 if (bvec_try_merge_page(bv, page, len, offset)) {
1077 bio->bi_iter.bi_size += len;
1078 return len;
1079 }
1080 }
1081
1082 if (bio->bi_vcnt >= bio->bi_max_vecs)
1083 return 0;
1084 __bio_add_page(bio, page, len, offset);
1085 return len;
1086 }
1087 EXPORT_SYMBOL(bio_add_page);
1088
bio_add_folio_nofail(struct bio * bio,struct folio * folio,size_t len,size_t off)1089 void bio_add_folio_nofail(struct bio *bio, struct folio *folio, size_t len,
1090 size_t off)
1091 {
1092 unsigned long nr = off / PAGE_SIZE;
1093
1094 WARN_ON_ONCE(len > BIO_MAX_SIZE);
1095 __bio_add_page(bio, folio_page(folio, nr), len, off % PAGE_SIZE);
1096 }
1097 EXPORT_SYMBOL_GPL(bio_add_folio_nofail);
1098
1099 /**
1100 * bio_add_folio - Attempt to add part of a folio to a bio.
1101 * @bio: BIO to add to.
1102 * @folio: Folio to add.
1103 * @len: How many bytes from the folio to add.
1104 * @off: First byte in this folio to add.
1105 *
1106 * Filesystems that use folios can call this function instead of calling
1107 * bio_add_page() for each page in the folio. If @off is bigger than
1108 * PAGE_SIZE, this function can create a bio_vec that starts in a page
1109 * after the bv_page. BIOs do not support folios that are 4GiB or larger.
1110 *
1111 * Return: Whether the addition was successful.
1112 */
bio_add_folio(struct bio * bio,struct folio * folio,size_t len,size_t off)1113 bool bio_add_folio(struct bio *bio, struct folio *folio, size_t len,
1114 size_t off)
1115 {
1116 unsigned long nr = off / PAGE_SIZE;
1117
1118 if (len > BIO_MAX_SIZE)
1119 return false;
1120 return bio_add_page(bio, folio_page(folio, nr), len, off % PAGE_SIZE) > 0;
1121 }
1122 EXPORT_SYMBOL(bio_add_folio);
1123
1124 /**
1125 * bio_add_vmalloc_chunk - add a vmalloc chunk to a bio
1126 * @bio: destination bio
1127 * @vaddr: vmalloc address to add
1128 * @len: total length in bytes of the data to add
1129 *
1130 * Add data starting at @vaddr to @bio and return how many bytes were added.
1131 * This may be less than the amount originally asked. Returns 0 if no data
1132 * could be added to @bio.
1133 *
1134 * This helper calls flush_kernel_vmap_range() for the range added. For reads
1135 * the caller still needs to manually call invalidate_kernel_vmap_range() in
1136 * the completion handler.
1137 */
bio_add_vmalloc_chunk(struct bio * bio,void * vaddr,unsigned len)1138 unsigned int bio_add_vmalloc_chunk(struct bio *bio, void *vaddr, unsigned len)
1139 {
1140 unsigned int offset = offset_in_page(vaddr);
1141
1142 len = min(len, PAGE_SIZE - offset);
1143 if (bio_add_page(bio, vmalloc_to_page(vaddr), len, offset) < len)
1144 return 0;
1145 if (op_is_write(bio_op(bio)))
1146 flush_kernel_vmap_range(vaddr, len);
1147 return len;
1148 }
1149 EXPORT_SYMBOL_GPL(bio_add_vmalloc_chunk);
1150
1151 /**
1152 * bio_add_vmalloc - add a vmalloc region to a bio
1153 * @bio: destination bio
1154 * @vaddr: vmalloc address to add
1155 * @len: total length in bytes of the data to add
1156 *
1157 * Add data starting at @vaddr to @bio. Return %true on success or %false if
1158 * @bio does not have enough space for the payload.
1159 *
1160 * This helper calls flush_kernel_vmap_range() for the range added. For reads
1161 * the caller still needs to manually call invalidate_kernel_vmap_range() in
1162 * the completion handler.
1163 */
bio_add_vmalloc(struct bio * bio,void * vaddr,unsigned int len)1164 bool bio_add_vmalloc(struct bio *bio, void *vaddr, unsigned int len)
1165 {
1166 do {
1167 unsigned int added = bio_add_vmalloc_chunk(bio, vaddr, len);
1168
1169 if (!added)
1170 return false;
1171 vaddr += added;
1172 len -= added;
1173 } while (len);
1174
1175 return true;
1176 }
1177 EXPORT_SYMBOL_GPL(bio_add_vmalloc);
1178
__bio_release_pages(struct bio * bio,bool mark_dirty)1179 void __bio_release_pages(struct bio *bio, bool mark_dirty)
1180 {
1181 struct folio_iter fi;
1182
1183 bio_for_each_folio_all(fi, bio) {
1184 size_t nr_pages;
1185
1186 if (mark_dirty) {
1187 folio_lock(fi.folio);
1188 folio_mark_dirty(fi.folio);
1189 folio_unlock(fi.folio);
1190 }
1191 nr_pages = (fi.offset + fi.length - 1) / PAGE_SIZE -
1192 fi.offset / PAGE_SIZE + 1;
1193 unpin_user_folio(fi.folio, nr_pages);
1194 }
1195 }
1196 EXPORT_SYMBOL_GPL(__bio_release_pages);
1197
bio_iov_bvec_set(struct bio * bio,const struct iov_iter * iter)1198 void bio_iov_bvec_set(struct bio *bio, const struct iov_iter *iter)
1199 {
1200 WARN_ON_ONCE(bio->bi_max_vecs);
1201
1202 bio->bi_io_vec = (struct bio_vec *)iter->bvec;
1203 bio->bi_iter.bi_idx = 0;
1204 bio->bi_iter.bi_bvec_done = iter->iov_offset;
1205 bio->bi_iter.bi_size = iov_iter_count(iter);
1206 bio_set_flag(bio, BIO_CLONED);
1207 }
1208
1209 /*
1210 * Aligns the bio size to the len_align_mask, releasing excessive bio vecs that
1211 * __bio_iov_iter_get_pages may have inserted, and reverts the trimmed length
1212 * for the next iteration.
1213 */
bio_iov_iter_align_down(struct bio * bio,struct iov_iter * iter,unsigned len_align_mask)1214 static int bio_iov_iter_align_down(struct bio *bio, struct iov_iter *iter,
1215 unsigned len_align_mask)
1216 {
1217 size_t nbytes = bio->bi_iter.bi_size & len_align_mask;
1218
1219 if (!nbytes)
1220 return 0;
1221
1222 iov_iter_revert(iter, nbytes);
1223 bio->bi_iter.bi_size -= nbytes;
1224 do {
1225 struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
1226
1227 if (nbytes < bv->bv_len) {
1228 bv->bv_len -= nbytes;
1229 break;
1230 }
1231
1232 if (bio_flagged(bio, BIO_PAGE_PINNED))
1233 unpin_user_page(bv->bv_page);
1234
1235 bio->bi_vcnt--;
1236 nbytes -= bv->bv_len;
1237 } while (nbytes);
1238
1239 if (!bio->bi_vcnt)
1240 return -EFAULT;
1241 return 0;
1242 }
1243
1244 /**
1245 * bio_iov_iter_get_pages - add user or kernel pages to a bio
1246 * @bio: bio to add pages to
1247 * @iter: iov iterator describing the region to be added
1248 * @len_align_mask: the mask to align the total size to, 0 for any length
1249 *
1250 * This takes either an iterator pointing to user memory, or one pointing to
1251 * kernel pages (BVEC iterator). If we're adding user pages, we pin them and
1252 * map them into the kernel. On IO completion, the caller should put those
1253 * pages. For bvec based iterators bio_iov_iter_get_pages() uses the provided
1254 * bvecs rather than copying them. Hence anyone issuing kiocb based IO needs
1255 * to ensure the bvecs and pages stay referenced until the submitted I/O is
1256 * completed by a call to ->ki_complete() or returns with an error other than
1257 * -EIOCBQUEUED. The caller needs to check if the bio is flagged BIO_NO_PAGE_REF
1258 * on IO completion. If it isn't, then pages should be released.
1259 *
1260 * The function tries, but does not guarantee, to pin as many pages as
1261 * fit into the bio, or are requested in @iter, whatever is smaller. If
1262 * MM encounters an error pinning the requested pages, it stops. Error
1263 * is returned only if 0 pages could be pinned.
1264 */
bio_iov_iter_get_pages(struct bio * bio,struct iov_iter * iter,unsigned len_align_mask)1265 int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter,
1266 unsigned len_align_mask)
1267 {
1268 iov_iter_extraction_t flags = 0;
1269
1270 if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
1271 return -EIO;
1272
1273 if (iov_iter_is_bvec(iter)) {
1274 bio_iov_bvec_set(bio, iter);
1275 iov_iter_advance(iter, bio->bi_iter.bi_size);
1276 return 0;
1277 }
1278
1279 if (iov_iter_extract_will_pin(iter))
1280 bio_set_flag(bio, BIO_PAGE_PINNED);
1281 if (bio->bi_bdev && blk_queue_pci_p2pdma(bio->bi_bdev->bd_disk->queue))
1282 flags |= ITER_ALLOW_P2PDMA;
1283
1284 do {
1285 ssize_t ret;
1286
1287 ret = iov_iter_extract_bvecs(iter, bio->bi_io_vec,
1288 BIO_MAX_SIZE - bio->bi_iter.bi_size,
1289 &bio->bi_vcnt, bio->bi_max_vecs, flags);
1290 if (ret <= 0) {
1291 if (!bio->bi_vcnt)
1292 return ret;
1293 break;
1294 }
1295 bio->bi_iter.bi_size += ret;
1296 } while (iov_iter_count(iter) && !bio_full(bio, 0));
1297
1298 if (is_pci_p2pdma_page(bio->bi_io_vec->bv_page))
1299 bio->bi_opf |= REQ_NOMERGE;
1300 return bio_iov_iter_align_down(bio, iter, len_align_mask);
1301 }
1302
folio_alloc_greedy(gfp_t gfp,size_t * size)1303 static struct folio *folio_alloc_greedy(gfp_t gfp, size_t *size)
1304 {
1305 struct folio *folio;
1306
1307 while (*size > PAGE_SIZE) {
1308 folio = folio_alloc(gfp | __GFP_NORETRY, get_order(*size));
1309 if (folio)
1310 return folio;
1311 *size = rounddown_pow_of_two(*size - 1);
1312 }
1313
1314 return folio_alloc(gfp, get_order(*size));
1315 }
1316
bio_free_folios(struct bio * bio)1317 static void bio_free_folios(struct bio *bio)
1318 {
1319 struct bio_vec *bv;
1320 int i;
1321
1322 bio_for_each_bvec_all(bv, bio, i) {
1323 struct folio *folio = page_folio(bv->bv_page);
1324
1325 if (!is_zero_folio(folio))
1326 folio_put(folio);
1327 }
1328 }
1329
bio_iov_iter_bounce_write(struct bio * bio,struct iov_iter * iter,size_t maxlen)1330 static int bio_iov_iter_bounce_write(struct bio *bio, struct iov_iter *iter,
1331 size_t maxlen)
1332 {
1333 size_t total_len = min(maxlen, iov_iter_count(iter));
1334
1335 if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
1336 return -EINVAL;
1337 if (WARN_ON_ONCE(bio->bi_iter.bi_size))
1338 return -EINVAL;
1339 if (WARN_ON_ONCE(bio->bi_vcnt >= bio->bi_max_vecs))
1340 return -EINVAL;
1341
1342 do {
1343 size_t this_len = min(total_len, SZ_1M);
1344 struct folio *folio;
1345
1346 if (this_len > PAGE_SIZE * 2)
1347 this_len = rounddown_pow_of_two(this_len);
1348
1349 if (bio->bi_iter.bi_size > BIO_MAX_SIZE - this_len)
1350 break;
1351
1352 folio = folio_alloc_greedy(GFP_KERNEL, &this_len);
1353 if (!folio)
1354 break;
1355 bio_add_folio_nofail(bio, folio, this_len, 0);
1356
1357 if (copy_from_iter(folio_address(folio), this_len, iter) !=
1358 this_len) {
1359 bio_free_folios(bio);
1360 return -EFAULT;
1361 }
1362
1363 total_len -= this_len;
1364 } while (total_len && bio->bi_vcnt < bio->bi_max_vecs);
1365
1366 if (!bio->bi_iter.bi_size)
1367 return -ENOMEM;
1368 return 0;
1369 }
1370
bio_iov_iter_bounce_read(struct bio * bio,struct iov_iter * iter,size_t maxlen)1371 static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter,
1372 size_t maxlen)
1373 {
1374 size_t len = min3(iov_iter_count(iter), maxlen, SZ_1M);
1375 struct folio *folio;
1376
1377 folio = folio_alloc_greedy(GFP_KERNEL, &len);
1378 if (!folio)
1379 return -ENOMEM;
1380
1381 do {
1382 ssize_t ret;
1383
1384 ret = iov_iter_extract_bvecs(iter, bio->bi_io_vec + 1, len,
1385 &bio->bi_vcnt, bio->bi_max_vecs - 1, 0);
1386 if (ret <= 0) {
1387 if (!bio->bi_vcnt) {
1388 folio_put(folio);
1389 return ret;
1390 }
1391 break;
1392 }
1393 len -= ret;
1394 bio->bi_iter.bi_size += ret;
1395 } while (len && bio->bi_vcnt < bio->bi_max_vecs - 1);
1396
1397 /*
1398 * Set the folio directly here. The above loop has already calculated
1399 * the correct bi_size, and we use bi_vcnt for the user buffers. That
1400 * is safe as bi_vcnt is only used by the submitter and not the actual
1401 * I/O path.
1402 */
1403 bvec_set_folio(&bio->bi_io_vec[0], folio, bio->bi_iter.bi_size, 0);
1404 if (iov_iter_extract_will_pin(iter))
1405 bio_set_flag(bio, BIO_PAGE_PINNED);
1406 return 0;
1407 }
1408
1409 /**
1410 * bio_iov_iter_bounce - bounce buffer data from an iter into a bio
1411 * @bio: bio to send
1412 * @iter: iter to read from / write into
1413 * @maxlen: maximum size to bounce
1414 *
1415 * Helper for direct I/O implementations that need to bounce buffer because
1416 * we need to checksum the data or perform other operations that require
1417 * consistency. Allocates folios to back the bounce buffer, and for writes
1418 * copies the data into it. Needs to be paired with bio_iov_iter_unbounce()
1419 * called on completion.
1420 */
bio_iov_iter_bounce(struct bio * bio,struct iov_iter * iter,size_t maxlen)1421 int bio_iov_iter_bounce(struct bio *bio, struct iov_iter *iter, size_t maxlen)
1422 {
1423 if (op_is_write(bio_op(bio)))
1424 return bio_iov_iter_bounce_write(bio, iter, maxlen);
1425 return bio_iov_iter_bounce_read(bio, iter, maxlen);
1426 }
1427
bvec_unpin(struct bio_vec * bv,bool mark_dirty)1428 static void bvec_unpin(struct bio_vec *bv, bool mark_dirty)
1429 {
1430 struct folio *folio = page_folio(bv->bv_page);
1431 size_t nr_pages = (bv->bv_offset + bv->bv_len - 1) / PAGE_SIZE -
1432 bv->bv_offset / PAGE_SIZE + 1;
1433
1434 if (mark_dirty)
1435 folio_mark_dirty_lock(folio);
1436 unpin_user_folio(folio, nr_pages);
1437 }
1438
bio_iov_iter_unbounce_read(struct bio * bio,bool is_error,bool mark_dirty)1439 static void bio_iov_iter_unbounce_read(struct bio *bio, bool is_error,
1440 bool mark_dirty)
1441 {
1442 unsigned int len = bio->bi_io_vec[0].bv_len;
1443
1444 if (likely(!is_error)) {
1445 void *buf = bvec_virt(&bio->bi_io_vec[0]);
1446 struct iov_iter to;
1447
1448 iov_iter_bvec(&to, ITER_DEST, bio->bi_io_vec + 1, bio->bi_vcnt,
1449 len);
1450 /* copying to pinned pages should always work */
1451 WARN_ON_ONCE(copy_to_iter(buf, len, &to) != len);
1452 } else {
1453 /* No need to mark folios dirty if never copied to them */
1454 mark_dirty = false;
1455 }
1456
1457 if (bio_flagged(bio, BIO_PAGE_PINNED)) {
1458 int i;
1459
1460 for (i = 0; i < bio->bi_vcnt; i++)
1461 bvec_unpin(&bio->bi_io_vec[1 + i], mark_dirty);
1462 }
1463
1464 folio_put(page_folio(bio->bi_io_vec[0].bv_page));
1465 }
1466
1467 /**
1468 * bio_iov_iter_unbounce - finish a bounce buffer operation
1469 * @bio: completed bio
1470 * @is_error: %true if an I/O error occurred and data should not be copied
1471 * @mark_dirty: If %true, folios will be marked dirty.
1472 *
1473 * Helper for direct I/O implementations that need to bounce buffer because
1474 * we need to checksum the data or perform other operations that require
1475 * consistency. Called to complete a bio set up by bio_iov_iter_bounce().
1476 * Copies data back for reads, and marks the original folios dirty if
1477 * requested and then frees the bounce buffer.
1478 */
bio_iov_iter_unbounce(struct bio * bio,bool is_error,bool mark_dirty)1479 void bio_iov_iter_unbounce(struct bio *bio, bool is_error, bool mark_dirty)
1480 {
1481 if (op_is_write(bio_op(bio)))
1482 bio_free_folios(bio);
1483 else
1484 bio_iov_iter_unbounce_read(bio, is_error, mark_dirty);
1485 }
1486
submit_bio_wait_endio(struct bio * bio)1487 static void submit_bio_wait_endio(struct bio *bio)
1488 {
1489 complete(bio->bi_private);
1490 }
1491
1492 /**
1493 * submit_bio_wait - submit a bio, and wait until it completes
1494 * @bio: The &struct bio which describes the I/O
1495 *
1496 * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
1497 * bio_endio() on failure.
1498 *
1499 * WARNING: Unlike to how submit_bio() is usually used, this function does not
1500 * result in bio reference to be consumed. The caller must drop the reference
1501 * on his own.
1502 */
submit_bio_wait(struct bio * bio)1503 int submit_bio_wait(struct bio *bio)
1504 {
1505 DECLARE_COMPLETION_ONSTACK_MAP(done,
1506 bio->bi_bdev->bd_disk->lockdep_map);
1507
1508 bio->bi_private = &done;
1509 bio->bi_end_io = submit_bio_wait_endio;
1510 bio->bi_opf |= REQ_SYNC;
1511 submit_bio(bio);
1512 blk_wait_io(&done);
1513
1514 return blk_status_to_errno(bio->bi_status);
1515 }
1516 EXPORT_SYMBOL(submit_bio_wait);
1517
1518 /**
1519 * bdev_rw_virt - synchronously read into / write from kernel mapping
1520 * @bdev: block device to access
1521 * @sector: sector to access
1522 * @data: data to read/write
1523 * @len: length in byte to read/write
1524 * @op: operation (e.g. REQ_OP_READ/REQ_OP_WRITE)
1525 *
1526 * Performs synchronous I/O to @bdev for @data/@len. @data must be in
1527 * the kernel direct mapping and not a vmalloc address.
1528 */
bdev_rw_virt(struct block_device * bdev,sector_t sector,void * data,size_t len,enum req_op op)1529 int bdev_rw_virt(struct block_device *bdev, sector_t sector, void *data,
1530 size_t len, enum req_op op)
1531 {
1532 struct bio_vec bv;
1533 struct bio bio;
1534 int error;
1535
1536 if (WARN_ON_ONCE(is_vmalloc_addr(data)))
1537 return -EIO;
1538
1539 bio_init(&bio, bdev, &bv, 1, op);
1540 bio.bi_iter.bi_sector = sector;
1541 bio_add_virt_nofail(&bio, data, len);
1542 error = submit_bio_wait(&bio);
1543 bio_uninit(&bio);
1544 return error;
1545 }
1546 EXPORT_SYMBOL_GPL(bdev_rw_virt);
1547
bio_wait_end_io(struct bio * bio)1548 static void bio_wait_end_io(struct bio *bio)
1549 {
1550 complete(bio->bi_private);
1551 bio_put(bio);
1552 }
1553
1554 /*
1555 * bio_await_chain - ends @bio and waits for every chained bio to complete
1556 */
bio_await_chain(struct bio * bio)1557 void bio_await_chain(struct bio *bio)
1558 {
1559 DECLARE_COMPLETION_ONSTACK_MAP(done,
1560 bio->bi_bdev->bd_disk->lockdep_map);
1561
1562 bio->bi_private = &done;
1563 bio->bi_end_io = bio_wait_end_io;
1564 bio_endio(bio);
1565 blk_wait_io(&done);
1566 }
1567
__bio_advance(struct bio * bio,unsigned bytes)1568 void __bio_advance(struct bio *bio, unsigned bytes)
1569 {
1570 if (bio_integrity(bio))
1571 bio_integrity_advance(bio, bytes);
1572
1573 bio_crypt_advance(bio, bytes);
1574 bio_advance_iter(bio, &bio->bi_iter, bytes);
1575 }
1576 EXPORT_SYMBOL(__bio_advance);
1577
bio_copy_data_iter(struct bio * dst,struct bvec_iter * dst_iter,struct bio * src,struct bvec_iter * src_iter)1578 void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
1579 struct bio *src, struct bvec_iter *src_iter)
1580 {
1581 while (src_iter->bi_size && dst_iter->bi_size) {
1582 struct bio_vec src_bv = bio_iter_iovec(src, *src_iter);
1583 struct bio_vec dst_bv = bio_iter_iovec(dst, *dst_iter);
1584 unsigned int bytes = min(src_bv.bv_len, dst_bv.bv_len);
1585 void *src_buf = bvec_kmap_local(&src_bv);
1586 void *dst_buf = bvec_kmap_local(&dst_bv);
1587
1588 memcpy(dst_buf, src_buf, bytes);
1589
1590 kunmap_local(dst_buf);
1591 kunmap_local(src_buf);
1592
1593 bio_advance_iter_single(src, src_iter, bytes);
1594 bio_advance_iter_single(dst, dst_iter, bytes);
1595 }
1596 }
1597 EXPORT_SYMBOL(bio_copy_data_iter);
1598
1599 /**
1600 * bio_copy_data - copy contents of data buffers from one bio to another
1601 * @src: source bio
1602 * @dst: destination bio
1603 *
1604 * Stops when it reaches the end of either @src or @dst - that is, copies
1605 * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
1606 */
bio_copy_data(struct bio * dst,struct bio * src)1607 void bio_copy_data(struct bio *dst, struct bio *src)
1608 {
1609 struct bvec_iter src_iter = src->bi_iter;
1610 struct bvec_iter dst_iter = dst->bi_iter;
1611
1612 bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
1613 }
1614 EXPORT_SYMBOL(bio_copy_data);
1615
bio_free_pages(struct bio * bio)1616 void bio_free_pages(struct bio *bio)
1617 {
1618 struct bio_vec *bvec;
1619 struct bvec_iter_all iter_all;
1620
1621 bio_for_each_segment_all(bvec, bio, iter_all)
1622 __free_page(bvec->bv_page);
1623 }
1624 EXPORT_SYMBOL(bio_free_pages);
1625
1626 /*
1627 * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
1628 * for performing direct-IO in BIOs.
1629 *
1630 * The problem is that we cannot run folio_mark_dirty() from interrupt context
1631 * because the required locks are not interrupt-safe. So what we can do is to
1632 * mark the pages dirty _before_ performing IO. And in interrupt context,
1633 * check that the pages are still dirty. If so, fine. If not, redirty them
1634 * in process context.
1635 *
1636 * Note that this code is very hard to test under normal circumstances because
1637 * direct-io pins the pages with get_user_pages(). This makes
1638 * is_page_cache_freeable return false, and the VM will not clean the pages.
1639 * But other code (eg, flusher threads) could clean the pages if they are mapped
1640 * pagecache.
1641 *
1642 * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
1643 * deferred bio dirtying paths.
1644 */
1645
1646 /*
1647 * bio_set_pages_dirty() will mark all the bio's pages as dirty.
1648 */
bio_set_pages_dirty(struct bio * bio)1649 void bio_set_pages_dirty(struct bio *bio)
1650 {
1651 struct folio_iter fi;
1652
1653 bio_for_each_folio_all(fi, bio) {
1654 folio_lock(fi.folio);
1655 folio_mark_dirty(fi.folio);
1656 folio_unlock(fi.folio);
1657 }
1658 }
1659 EXPORT_SYMBOL_GPL(bio_set_pages_dirty);
1660
1661 /*
1662 * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
1663 * If they are, then fine. If, however, some pages are clean then they must
1664 * have been written out during the direct-IO read. So we take another ref on
1665 * the BIO and re-dirty the pages in process context.
1666 *
1667 * It is expected that bio_check_pages_dirty() will wholly own the BIO from
1668 * here on. It will unpin each page and will run one bio_put() against the
1669 * BIO.
1670 */
1671
1672 static void bio_dirty_fn(struct work_struct *work);
1673
1674 static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
1675 static DEFINE_SPINLOCK(bio_dirty_lock);
1676 static struct bio *bio_dirty_list;
1677
1678 /*
1679 * This runs in process context
1680 */
bio_dirty_fn(struct work_struct * work)1681 static void bio_dirty_fn(struct work_struct *work)
1682 {
1683 struct bio *bio, *next;
1684
1685 spin_lock_irq(&bio_dirty_lock);
1686 next = bio_dirty_list;
1687 bio_dirty_list = NULL;
1688 spin_unlock_irq(&bio_dirty_lock);
1689
1690 while ((bio = next) != NULL) {
1691 next = bio->bi_private;
1692
1693 bio_release_pages(bio, true);
1694 bio_put(bio);
1695 }
1696 }
1697
bio_check_pages_dirty(struct bio * bio)1698 void bio_check_pages_dirty(struct bio *bio)
1699 {
1700 struct folio_iter fi;
1701 unsigned long flags;
1702
1703 bio_for_each_folio_all(fi, bio) {
1704 if (!folio_test_dirty(fi.folio))
1705 goto defer;
1706 }
1707
1708 bio_release_pages(bio, false);
1709 bio_put(bio);
1710 return;
1711 defer:
1712 spin_lock_irqsave(&bio_dirty_lock, flags);
1713 bio->bi_private = bio_dirty_list;
1714 bio_dirty_list = bio;
1715 spin_unlock_irqrestore(&bio_dirty_lock, flags);
1716 schedule_work(&bio_dirty_work);
1717 }
1718 EXPORT_SYMBOL_GPL(bio_check_pages_dirty);
1719
bio_remaining_done(struct bio * bio)1720 static inline bool bio_remaining_done(struct bio *bio)
1721 {
1722 /*
1723 * If we're not chaining, then ->__bi_remaining is always 1 and
1724 * we always end io on the first invocation.
1725 */
1726 if (!bio_flagged(bio, BIO_CHAIN))
1727 return true;
1728
1729 BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
1730
1731 if (atomic_dec_and_test(&bio->__bi_remaining)) {
1732 bio_clear_flag(bio, BIO_CHAIN);
1733 return true;
1734 }
1735
1736 return false;
1737 }
1738
1739 /**
1740 * bio_endio - end I/O on a bio
1741 * @bio: bio
1742 *
1743 * Description:
1744 * bio_endio() will end I/O on the whole bio. bio_endio() is the preferred
1745 * way to end I/O on a bio. No one should call bi_end_io() directly on a
1746 * bio unless they own it and thus know that it has an end_io function.
1747 *
1748 * bio_endio() can be called several times on a bio that has been chained
1749 * using bio_chain(). The ->bi_end_io() function will only be called the
1750 * last time.
1751 **/
bio_endio(struct bio * bio)1752 void bio_endio(struct bio *bio)
1753 {
1754 again:
1755 if (!bio_remaining_done(bio))
1756 return;
1757 if (!bio_integrity_endio(bio))
1758 return;
1759
1760 blk_zone_bio_endio(bio);
1761
1762 rq_qos_done_bio(bio);
1763
1764 if (bio->bi_bdev && bio_flagged(bio, BIO_TRACE_COMPLETION)) {
1765 trace_block_bio_complete(bdev_get_queue(bio->bi_bdev), bio);
1766 bio_clear_flag(bio, BIO_TRACE_COMPLETION);
1767 }
1768
1769 /*
1770 * Need to have a real endio function for chained bios, otherwise
1771 * various corner cases will break (like stacking block devices that
1772 * save/restore bi_end_io) - however, we want to avoid unbounded
1773 * recursion and blowing the stack. Tail call optimization would
1774 * handle this, but compiling with frame pointers also disables
1775 * gcc's sibling call optimization.
1776 */
1777 if (bio->bi_end_io == bio_chain_endio) {
1778 bio = __bio_chain_endio(bio);
1779 goto again;
1780 }
1781
1782 #ifdef CONFIG_BLK_CGROUP
1783 /*
1784 * Release cgroup info. We shouldn't have to do this here, but quite
1785 * a few callers of bio_init fail to call bio_uninit, so we cover up
1786 * for that here at least for now.
1787 */
1788 if (bio->bi_blkg) {
1789 blkg_put(bio->bi_blkg);
1790 bio->bi_blkg = NULL;
1791 }
1792 #endif
1793
1794 if (bio->bi_end_io)
1795 bio->bi_end_io(bio);
1796 }
1797 EXPORT_SYMBOL(bio_endio);
1798
1799 /**
1800 * bio_split - split a bio
1801 * @bio: bio to split
1802 * @sectors: number of sectors to split from the front of @bio
1803 * @gfp: gfp mask
1804 * @bs: bio set to allocate from
1805 *
1806 * Allocates and returns a new bio which represents @sectors from the start of
1807 * @bio, and updates @bio to represent the remaining sectors.
1808 *
1809 * Unless this is a discard request the newly allocated bio will point
1810 * to @bio's bi_io_vec. It is the caller's responsibility to ensure that
1811 * neither @bio nor @bs are freed before the split bio.
1812 */
bio_split(struct bio * bio,int sectors,gfp_t gfp,struct bio_set * bs)1813 struct bio *bio_split(struct bio *bio, int sectors,
1814 gfp_t gfp, struct bio_set *bs)
1815 {
1816 struct bio *split;
1817
1818 if (WARN_ON_ONCE(sectors <= 0))
1819 return ERR_PTR(-EINVAL);
1820 if (WARN_ON_ONCE(sectors >= bio_sectors(bio)))
1821 return ERR_PTR(-EINVAL);
1822
1823 /* Zone append commands cannot be split */
1824 if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
1825 return ERR_PTR(-EINVAL);
1826
1827 /* atomic writes cannot be split */
1828 if (bio->bi_opf & REQ_ATOMIC)
1829 return ERR_PTR(-EINVAL);
1830
1831 split = bio_alloc_clone(bio->bi_bdev, bio, gfp, bs);
1832 if (!split)
1833 return ERR_PTR(-ENOMEM);
1834
1835 split->bi_iter.bi_size = sectors << 9;
1836
1837 if (bio_integrity(split))
1838 bio_integrity_trim(split);
1839
1840 bio_advance(bio, split->bi_iter.bi_size);
1841
1842 if (bio_flagged(bio, BIO_TRACE_COMPLETION))
1843 bio_set_flag(split, BIO_TRACE_COMPLETION);
1844
1845 return split;
1846 }
1847 EXPORT_SYMBOL(bio_split);
1848
1849 /**
1850 * bio_trim - trim a bio
1851 * @bio: bio to trim
1852 * @offset: number of sectors to trim from the front of @bio
1853 * @size: size we want to trim @bio to, in sectors
1854 *
1855 * This function is typically used for bios that are cloned and submitted
1856 * to the underlying device in parts.
1857 */
bio_trim(struct bio * bio,sector_t offset,sector_t size)1858 void bio_trim(struct bio *bio, sector_t offset, sector_t size)
1859 {
1860 /* We should never trim an atomic write */
1861 if (WARN_ON_ONCE(bio->bi_opf & REQ_ATOMIC && size))
1862 return;
1863
1864 if (WARN_ON_ONCE(offset > BIO_MAX_SECTORS || size > BIO_MAX_SECTORS ||
1865 offset + size > bio_sectors(bio)))
1866 return;
1867
1868 size <<= 9;
1869 if (offset == 0 && size == bio->bi_iter.bi_size)
1870 return;
1871
1872 bio_advance(bio, offset << 9);
1873 bio->bi_iter.bi_size = size;
1874
1875 if (bio_integrity(bio))
1876 bio_integrity_trim(bio);
1877 }
1878 EXPORT_SYMBOL_GPL(bio_trim);
1879
1880 /*
1881 * create memory pools for biovec's in a bio_set.
1882 * use the global biovec slabs created for general use.
1883 */
biovec_init_pool(mempool_t * pool,int pool_entries)1884 int biovec_init_pool(mempool_t *pool, int pool_entries)
1885 {
1886 struct biovec_slab *bp = bvec_slabs + ARRAY_SIZE(bvec_slabs) - 1;
1887
1888 return mempool_init_slab_pool(pool, pool_entries, bp->slab);
1889 }
1890
1891 /*
1892 * bioset_exit - exit a bioset initialized with bioset_init()
1893 *
1894 * May be called on a zeroed but uninitialized bioset (i.e. allocated with
1895 * kzalloc()).
1896 */
bioset_exit(struct bio_set * bs)1897 void bioset_exit(struct bio_set *bs)
1898 {
1899 bio_alloc_cache_destroy(bs);
1900 if (bs->rescue_workqueue)
1901 destroy_workqueue(bs->rescue_workqueue);
1902 bs->rescue_workqueue = NULL;
1903
1904 mempool_exit(&bs->bio_pool);
1905 mempool_exit(&bs->bvec_pool);
1906
1907 if (bs->bio_slab)
1908 bio_put_slab(bs);
1909 bs->bio_slab = NULL;
1910 }
1911 EXPORT_SYMBOL(bioset_exit);
1912
1913 /**
1914 * bioset_init - Initialize a bio_set
1915 * @bs: pool to initialize
1916 * @pool_size: Number of bio and bio_vecs to cache in the mempool
1917 * @front_pad: Number of bytes to allocate in front of the returned bio
1918 * @flags: Flags to modify behavior, currently %BIOSET_NEED_BVECS
1919 * and %BIOSET_NEED_RESCUER
1920 *
1921 * Description:
1922 * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
1923 * to ask for a number of bytes to be allocated in front of the bio.
1924 * Front pad allocation is useful for embedding the bio inside
1925 * another structure, to avoid allocating extra data to go with the bio.
1926 * Note that the bio must be embedded at the END of that structure always,
1927 * or things will break badly.
1928 * If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated
1929 * for allocating iovecs. This pool is not needed e.g. for bio_init_clone().
1930 * If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used
1931 * to dispatch queued requests when the mempool runs out of space.
1932 *
1933 */
bioset_init(struct bio_set * bs,unsigned int pool_size,unsigned int front_pad,int flags)1934 int bioset_init(struct bio_set *bs,
1935 unsigned int pool_size,
1936 unsigned int front_pad,
1937 int flags)
1938 {
1939 bs->front_pad = front_pad;
1940 if (flags & BIOSET_NEED_BVECS)
1941 bs->back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
1942 else
1943 bs->back_pad = 0;
1944
1945 spin_lock_init(&bs->rescue_lock);
1946 bio_list_init(&bs->rescue_list);
1947 INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1948
1949 bs->bio_slab = bio_find_or_create_slab(bs);
1950 if (!bs->bio_slab)
1951 return -ENOMEM;
1952
1953 if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab))
1954 goto bad;
1955
1956 if ((flags & BIOSET_NEED_BVECS) &&
1957 biovec_init_pool(&bs->bvec_pool, pool_size))
1958 goto bad;
1959
1960 if (flags & BIOSET_NEED_RESCUER) {
1961 bs->rescue_workqueue = alloc_workqueue("bioset",
1962 WQ_MEM_RECLAIM, 0);
1963 if (!bs->rescue_workqueue)
1964 goto bad;
1965 }
1966 if (flags & BIOSET_PERCPU_CACHE) {
1967 bs->cache = alloc_percpu(struct bio_alloc_cache);
1968 if (!bs->cache)
1969 goto bad;
1970 cpuhp_state_add_instance_nocalls(CPUHP_BIO_DEAD, &bs->cpuhp_dead);
1971 }
1972
1973 return 0;
1974 bad:
1975 bioset_exit(bs);
1976 return -ENOMEM;
1977 }
1978 EXPORT_SYMBOL(bioset_init);
1979
init_bio(void)1980 static int __init init_bio(void)
1981 {
1982 int i;
1983
1984 BUILD_BUG_ON(BIO_FLAG_LAST > 8 * sizeof_field(struct bio, bi_flags));
1985
1986 for (i = 0; i < ARRAY_SIZE(bvec_slabs); i++) {
1987 struct biovec_slab *bvs = bvec_slabs + i;
1988
1989 bvs->slab = kmem_cache_create(bvs->name,
1990 bvs->nr_vecs * sizeof(struct bio_vec), 0,
1991 SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
1992 }
1993
1994 cpuhp_setup_state_multi(CPUHP_BIO_DEAD, "block/bio:dead", NULL,
1995 bio_cpu_dead);
1996
1997 if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0,
1998 BIOSET_NEED_BVECS | BIOSET_PERCPU_CACHE))
1999 panic("bio: can't allocate bios\n");
2000
2001 return 0;
2002 }
2003 subsys_initcall(init_bio);
2004