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