1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Functions related to mapping data to requests 4 */ 5 #include <linux/kernel.h> 6 #include <linux/sched/task_stack.h> 7 #include <linux/module.h> 8 #include <linux/bio.h> 9 #include <linux/blkdev.h> 10 #include <linux/uio.h> 11 12 #include "blk.h" 13 14 struct bio_map_data { 15 bool is_our_pages : 1; 16 bool is_null_mapped : 1; 17 struct iov_iter iter; 18 struct iovec iov[]; 19 }; 20 21 static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data, 22 gfp_t gfp_mask) 23 { 24 struct bio_map_data *bmd; 25 26 if (data->nr_segs > UIO_MAXIOV) 27 return NULL; 28 29 bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask); 30 if (!bmd) 31 return NULL; 32 memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs); 33 bmd->iter = *data; 34 bmd->iter.iov = bmd->iov; 35 return bmd; 36 } 37 38 /** 39 * bio_copy_from_iter - copy all pages from iov_iter to bio 40 * @bio: The &struct bio which describes the I/O as destination 41 * @iter: iov_iter as source 42 * 43 * Copy all pages from iov_iter to bio. 44 * Returns 0 on success, or error on failure. 45 */ 46 static int bio_copy_from_iter(struct bio *bio, struct iov_iter *iter) 47 { 48 struct bio_vec *bvec; 49 struct bvec_iter_all iter_all; 50 51 bio_for_each_segment_all(bvec, bio, iter_all) { 52 ssize_t ret; 53 54 ret = copy_page_from_iter(bvec->bv_page, 55 bvec->bv_offset, 56 bvec->bv_len, 57 iter); 58 59 if (!iov_iter_count(iter)) 60 break; 61 62 if (ret < bvec->bv_len) 63 return -EFAULT; 64 } 65 66 return 0; 67 } 68 69 /** 70 * bio_copy_to_iter - copy all pages from bio to iov_iter 71 * @bio: The &struct bio which describes the I/O as source 72 * @iter: iov_iter as destination 73 * 74 * Copy all pages from bio to iov_iter. 75 * Returns 0 on success, or error on failure. 76 */ 77 static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter) 78 { 79 struct bio_vec *bvec; 80 struct bvec_iter_all iter_all; 81 82 bio_for_each_segment_all(bvec, bio, iter_all) { 83 ssize_t ret; 84 85 ret = copy_page_to_iter(bvec->bv_page, 86 bvec->bv_offset, 87 bvec->bv_len, 88 &iter); 89 90 if (!iov_iter_count(&iter)) 91 break; 92 93 if (ret < bvec->bv_len) 94 return -EFAULT; 95 } 96 97 return 0; 98 } 99 100 /** 101 * bio_uncopy_user - finish previously mapped bio 102 * @bio: bio being terminated 103 * 104 * Free pages allocated from bio_copy_user_iov() and write back data 105 * to user space in case of a read. 106 */ 107 static int bio_uncopy_user(struct bio *bio) 108 { 109 struct bio_map_data *bmd = bio->bi_private; 110 int ret = 0; 111 112 if (!bmd->is_null_mapped) { 113 /* 114 * if we're in a workqueue, the request is orphaned, so 115 * don't copy into a random user address space, just free 116 * and return -EINTR so user space doesn't expect any data. 117 */ 118 if (!current->mm) 119 ret = -EINTR; 120 else if (bio_data_dir(bio) == READ) 121 ret = bio_copy_to_iter(bio, bmd->iter); 122 if (bmd->is_our_pages) 123 bio_free_pages(bio); 124 } 125 kfree(bmd); 126 return ret; 127 } 128 129 static int bio_copy_user_iov(struct request *rq, struct rq_map_data *map_data, 130 struct iov_iter *iter, gfp_t gfp_mask) 131 { 132 struct bio_map_data *bmd; 133 struct page *page; 134 struct bio *bio; 135 int i = 0, ret; 136 int nr_pages; 137 unsigned int len = iter->count; 138 unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0; 139 140 bmd = bio_alloc_map_data(iter, gfp_mask); 141 if (!bmd) 142 return -ENOMEM; 143 144 /* 145 * We need to do a deep copy of the iov_iter including the iovecs. 146 * The caller provided iov might point to an on-stack or otherwise 147 * shortlived one. 148 */ 149 bmd->is_our_pages = !map_data; 150 bmd->is_null_mapped = (map_data && map_data->null_mapped); 151 152 nr_pages = bio_max_segs(DIV_ROUND_UP(offset + len, PAGE_SIZE)); 153 154 ret = -ENOMEM; 155 bio = bio_kmalloc(nr_pages, gfp_mask); 156 if (!bio) 157 goto out_bmd; 158 bio_init(bio, NULL, bio->bi_inline_vecs, nr_pages, req_op(rq)); 159 160 if (map_data) { 161 nr_pages = 1U << map_data->page_order; 162 i = map_data->offset / PAGE_SIZE; 163 } 164 while (len) { 165 unsigned int bytes = PAGE_SIZE; 166 167 bytes -= offset; 168 169 if (bytes > len) 170 bytes = len; 171 172 if (map_data) { 173 if (i == map_data->nr_entries * nr_pages) { 174 ret = -ENOMEM; 175 goto cleanup; 176 } 177 178 page = map_data->pages[i / nr_pages]; 179 page += (i % nr_pages); 180 181 i++; 182 } else { 183 page = alloc_page(GFP_NOIO | gfp_mask); 184 if (!page) { 185 ret = -ENOMEM; 186 goto cleanup; 187 } 188 } 189 190 if (bio_add_pc_page(rq->q, bio, page, bytes, offset) < bytes) { 191 if (!map_data) 192 __free_page(page); 193 break; 194 } 195 196 len -= bytes; 197 offset = 0; 198 } 199 200 if (map_data) 201 map_data->offset += bio->bi_iter.bi_size; 202 203 /* 204 * success 205 */ 206 if ((iov_iter_rw(iter) == WRITE && 207 (!map_data || !map_data->null_mapped)) || 208 (map_data && map_data->from_user)) { 209 ret = bio_copy_from_iter(bio, iter); 210 if (ret) 211 goto cleanup; 212 } else { 213 if (bmd->is_our_pages) 214 zero_fill_bio(bio); 215 iov_iter_advance(iter, bio->bi_iter.bi_size); 216 } 217 218 bio->bi_private = bmd; 219 220 ret = blk_rq_append_bio(rq, bio); 221 if (ret) 222 goto cleanup; 223 return 0; 224 cleanup: 225 if (!map_data) 226 bio_free_pages(bio); 227 bio_uninit(bio); 228 kfree(bio); 229 out_bmd: 230 kfree(bmd); 231 return ret; 232 } 233 234 static void bio_map_put(struct bio *bio) 235 { 236 if (bio->bi_opf & REQ_ALLOC_CACHE) { 237 bio_put(bio); 238 } else { 239 bio_uninit(bio); 240 kfree(bio); 241 } 242 } 243 244 static int bio_map_user_iov(struct request *rq, struct iov_iter *iter, 245 gfp_t gfp_mask) 246 { 247 unsigned int max_sectors = queue_max_hw_sectors(rq->q); 248 unsigned int nr_vecs = iov_iter_npages(iter, BIO_MAX_VECS); 249 struct bio *bio; 250 int ret; 251 int j; 252 253 if (!iov_iter_count(iter)) 254 return -EINVAL; 255 256 if (rq->cmd_flags & REQ_POLLED) { 257 blk_opf_t opf = rq->cmd_flags | REQ_ALLOC_CACHE; 258 259 bio = bio_alloc_bioset(NULL, nr_vecs, opf, gfp_mask, 260 &fs_bio_set); 261 if (!bio) 262 return -ENOMEM; 263 } else { 264 bio = bio_kmalloc(nr_vecs, gfp_mask); 265 if (!bio) 266 return -ENOMEM; 267 bio_init(bio, NULL, bio->bi_inline_vecs, nr_vecs, req_op(rq)); 268 } 269 270 while (iov_iter_count(iter)) { 271 struct page **pages, *stack_pages[UIO_FASTIOV]; 272 ssize_t bytes; 273 size_t offs, added = 0; 274 int npages; 275 276 if (nr_vecs <= ARRAY_SIZE(stack_pages)) { 277 pages = stack_pages; 278 bytes = iov_iter_get_pages2(iter, pages, LONG_MAX, 279 nr_vecs, &offs); 280 } else { 281 bytes = iov_iter_get_pages_alloc2(iter, &pages, 282 LONG_MAX, &offs); 283 } 284 if (unlikely(bytes <= 0)) { 285 ret = bytes ? bytes : -EFAULT; 286 goto out_unmap; 287 } 288 289 npages = DIV_ROUND_UP(offs + bytes, PAGE_SIZE); 290 291 if (unlikely(offs & queue_dma_alignment(rq->q))) 292 j = 0; 293 else { 294 for (j = 0; j < npages; j++) { 295 struct page *page = pages[j]; 296 unsigned int n = PAGE_SIZE - offs; 297 bool same_page = false; 298 299 if (n > bytes) 300 n = bytes; 301 302 if (!bio_add_hw_page(rq->q, bio, page, n, offs, 303 max_sectors, &same_page)) { 304 if (same_page) 305 put_page(page); 306 break; 307 } 308 309 added += n; 310 bytes -= n; 311 offs = 0; 312 } 313 } 314 /* 315 * release the pages we didn't map into the bio, if any 316 */ 317 while (j < npages) 318 put_page(pages[j++]); 319 if (pages != stack_pages) 320 kvfree(pages); 321 /* couldn't stuff something into bio? */ 322 if (bytes) { 323 iov_iter_revert(iter, bytes); 324 break; 325 } 326 } 327 328 ret = blk_rq_append_bio(rq, bio); 329 if (ret) 330 goto out_unmap; 331 return 0; 332 333 out_unmap: 334 bio_release_pages(bio, false); 335 bio_map_put(bio); 336 return ret; 337 } 338 339 static void bio_invalidate_vmalloc_pages(struct bio *bio) 340 { 341 #ifdef ARCH_IMPLEMENTS_FLUSH_KERNEL_VMAP_RANGE 342 if (bio->bi_private && !op_is_write(bio_op(bio))) { 343 unsigned long i, len = 0; 344 345 for (i = 0; i < bio->bi_vcnt; i++) 346 len += bio->bi_io_vec[i].bv_len; 347 invalidate_kernel_vmap_range(bio->bi_private, len); 348 } 349 #endif 350 } 351 352 static void bio_map_kern_endio(struct bio *bio) 353 { 354 bio_invalidate_vmalloc_pages(bio); 355 bio_uninit(bio); 356 kfree(bio); 357 } 358 359 /** 360 * bio_map_kern - map kernel address into bio 361 * @q: the struct request_queue for the bio 362 * @data: pointer to buffer to map 363 * @len: length in bytes 364 * @gfp_mask: allocation flags for bio allocation 365 * 366 * Map the kernel address into a bio suitable for io to a block 367 * device. Returns an error pointer in case of error. 368 */ 369 static struct bio *bio_map_kern(struct request_queue *q, void *data, 370 unsigned int len, gfp_t gfp_mask) 371 { 372 unsigned long kaddr = (unsigned long)data; 373 unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 374 unsigned long start = kaddr >> PAGE_SHIFT; 375 const int nr_pages = end - start; 376 bool is_vmalloc = is_vmalloc_addr(data); 377 struct page *page; 378 int offset, i; 379 struct bio *bio; 380 381 bio = bio_kmalloc(nr_pages, gfp_mask); 382 if (!bio) 383 return ERR_PTR(-ENOMEM); 384 bio_init(bio, NULL, bio->bi_inline_vecs, nr_pages, 0); 385 386 if (is_vmalloc) { 387 flush_kernel_vmap_range(data, len); 388 bio->bi_private = data; 389 } 390 391 offset = offset_in_page(kaddr); 392 for (i = 0; i < nr_pages; i++) { 393 unsigned int bytes = PAGE_SIZE - offset; 394 395 if (len <= 0) 396 break; 397 398 if (bytes > len) 399 bytes = len; 400 401 if (!is_vmalloc) 402 page = virt_to_page(data); 403 else 404 page = vmalloc_to_page(data); 405 if (bio_add_pc_page(q, bio, page, bytes, 406 offset) < bytes) { 407 /* we don't support partial mappings */ 408 bio_uninit(bio); 409 kfree(bio); 410 return ERR_PTR(-EINVAL); 411 } 412 413 data += bytes; 414 len -= bytes; 415 offset = 0; 416 } 417 418 bio->bi_end_io = bio_map_kern_endio; 419 return bio; 420 } 421 422 static void bio_copy_kern_endio(struct bio *bio) 423 { 424 bio_free_pages(bio); 425 bio_uninit(bio); 426 kfree(bio); 427 } 428 429 static void bio_copy_kern_endio_read(struct bio *bio) 430 { 431 char *p = bio->bi_private; 432 struct bio_vec *bvec; 433 struct bvec_iter_all iter_all; 434 435 bio_for_each_segment_all(bvec, bio, iter_all) { 436 memcpy_from_bvec(p, bvec); 437 p += bvec->bv_len; 438 } 439 440 bio_copy_kern_endio(bio); 441 } 442 443 /** 444 * bio_copy_kern - copy kernel address into bio 445 * @q: the struct request_queue for the bio 446 * @data: pointer to buffer to copy 447 * @len: length in bytes 448 * @gfp_mask: allocation flags for bio and page allocation 449 * @reading: data direction is READ 450 * 451 * copy the kernel address into a bio suitable for io to a block 452 * device. Returns an error pointer in case of error. 453 */ 454 static struct bio *bio_copy_kern(struct request_queue *q, void *data, 455 unsigned int len, gfp_t gfp_mask, int reading) 456 { 457 unsigned long kaddr = (unsigned long)data; 458 unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 459 unsigned long start = kaddr >> PAGE_SHIFT; 460 struct bio *bio; 461 void *p = data; 462 int nr_pages = 0; 463 464 /* 465 * Overflow, abort 466 */ 467 if (end < start) 468 return ERR_PTR(-EINVAL); 469 470 nr_pages = end - start; 471 bio = bio_kmalloc(nr_pages, gfp_mask); 472 if (!bio) 473 return ERR_PTR(-ENOMEM); 474 bio_init(bio, NULL, bio->bi_inline_vecs, nr_pages, 0); 475 476 while (len) { 477 struct page *page; 478 unsigned int bytes = PAGE_SIZE; 479 480 if (bytes > len) 481 bytes = len; 482 483 page = alloc_page(GFP_NOIO | __GFP_ZERO | gfp_mask); 484 if (!page) 485 goto cleanup; 486 487 if (!reading) 488 memcpy(page_address(page), p, bytes); 489 490 if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes) 491 break; 492 493 len -= bytes; 494 p += bytes; 495 } 496 497 if (reading) { 498 bio->bi_end_io = bio_copy_kern_endio_read; 499 bio->bi_private = data; 500 } else { 501 bio->bi_end_io = bio_copy_kern_endio; 502 } 503 504 return bio; 505 506 cleanup: 507 bio_free_pages(bio); 508 bio_uninit(bio); 509 kfree(bio); 510 return ERR_PTR(-ENOMEM); 511 } 512 513 /* 514 * Append a bio to a passthrough request. Only works if the bio can be merged 515 * into the request based on the driver constraints. 516 */ 517 int blk_rq_append_bio(struct request *rq, struct bio *bio) 518 { 519 struct bvec_iter iter; 520 struct bio_vec bv; 521 unsigned int nr_segs = 0; 522 523 bio_for_each_bvec(bv, bio, iter) 524 nr_segs++; 525 526 if (!rq->bio) { 527 blk_rq_bio_prep(rq, bio, nr_segs); 528 } else { 529 if (!ll_back_merge_fn(rq, bio, nr_segs)) 530 return -EINVAL; 531 rq->biotail->bi_next = bio; 532 rq->biotail = bio; 533 rq->__data_len += (bio)->bi_iter.bi_size; 534 bio_crypt_free_ctx(bio); 535 } 536 537 return 0; 538 } 539 EXPORT_SYMBOL(blk_rq_append_bio); 540 541 /** 542 * blk_rq_map_user_iov - map user data to a request, for passthrough requests 543 * @q: request queue where request should be inserted 544 * @rq: request to map data to 545 * @map_data: pointer to the rq_map_data holding pages (if necessary) 546 * @iter: iovec iterator 547 * @gfp_mask: memory allocation flags 548 * 549 * Description: 550 * Data will be mapped directly for zero copy I/O, if possible. Otherwise 551 * a kernel bounce buffer is used. 552 * 553 * A matching blk_rq_unmap_user() must be issued at the end of I/O, while 554 * still in process context. 555 */ 556 int blk_rq_map_user_iov(struct request_queue *q, struct request *rq, 557 struct rq_map_data *map_data, 558 const struct iov_iter *iter, gfp_t gfp_mask) 559 { 560 bool copy = false; 561 unsigned long align = q->dma_pad_mask | queue_dma_alignment(q); 562 struct bio *bio = NULL; 563 struct iov_iter i; 564 int ret = -EINVAL; 565 566 if (!iter_is_iovec(iter)) 567 goto fail; 568 569 if (map_data) 570 copy = true; 571 else if (blk_queue_may_bounce(q)) 572 copy = true; 573 else if (iov_iter_alignment(iter) & align) 574 copy = true; 575 else if (queue_virt_boundary(q)) 576 copy = queue_virt_boundary(q) & iov_iter_gap_alignment(iter); 577 578 i = *iter; 579 do { 580 if (copy) 581 ret = bio_copy_user_iov(rq, map_data, &i, gfp_mask); 582 else 583 ret = bio_map_user_iov(rq, &i, gfp_mask); 584 if (ret) 585 goto unmap_rq; 586 if (!bio) 587 bio = rq->bio; 588 } while (iov_iter_count(&i)); 589 590 return 0; 591 592 unmap_rq: 593 blk_rq_unmap_user(bio); 594 fail: 595 rq->bio = NULL; 596 return ret; 597 } 598 EXPORT_SYMBOL(blk_rq_map_user_iov); 599 600 int blk_rq_map_user(struct request_queue *q, struct request *rq, 601 struct rq_map_data *map_data, void __user *ubuf, 602 unsigned long len, gfp_t gfp_mask) 603 { 604 struct iovec iov; 605 struct iov_iter i; 606 int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i); 607 608 if (unlikely(ret < 0)) 609 return ret; 610 611 return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask); 612 } 613 EXPORT_SYMBOL(blk_rq_map_user); 614 615 /** 616 * blk_rq_unmap_user - unmap a request with user data 617 * @bio: start of bio list 618 * 619 * Description: 620 * Unmap a rq previously mapped by blk_rq_map_user(). The caller must 621 * supply the original rq->bio from the blk_rq_map_user() return, since 622 * the I/O completion may have changed rq->bio. 623 */ 624 int blk_rq_unmap_user(struct bio *bio) 625 { 626 struct bio *next_bio; 627 int ret = 0, ret2; 628 629 while (bio) { 630 if (bio->bi_private) { 631 ret2 = bio_uncopy_user(bio); 632 if (ret2 && !ret) 633 ret = ret2; 634 } else { 635 bio_release_pages(bio, bio_data_dir(bio) == READ); 636 } 637 638 next_bio = bio; 639 bio = bio->bi_next; 640 bio_map_put(next_bio); 641 } 642 643 return ret; 644 } 645 EXPORT_SYMBOL(blk_rq_unmap_user); 646 647 /** 648 * blk_rq_map_kern - map kernel data to a request, for passthrough requests 649 * @q: request queue where request should be inserted 650 * @rq: request to fill 651 * @kbuf: the kernel buffer 652 * @len: length of user data 653 * @gfp_mask: memory allocation flags 654 * 655 * Description: 656 * Data will be mapped directly if possible. Otherwise a bounce 657 * buffer is used. Can be called multiple times to append multiple 658 * buffers. 659 */ 660 int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf, 661 unsigned int len, gfp_t gfp_mask) 662 { 663 int reading = rq_data_dir(rq) == READ; 664 unsigned long addr = (unsigned long) kbuf; 665 struct bio *bio; 666 int ret; 667 668 if (len > (queue_max_hw_sectors(q) << 9)) 669 return -EINVAL; 670 if (!len || !kbuf) 671 return -EINVAL; 672 673 if (!blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf) || 674 blk_queue_may_bounce(q)) 675 bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading); 676 else 677 bio = bio_map_kern(q, kbuf, len, gfp_mask); 678 679 if (IS_ERR(bio)) 680 return PTR_ERR(bio); 681 682 bio->bi_opf &= ~REQ_OP_MASK; 683 bio->bi_opf |= req_op(rq); 684 685 ret = blk_rq_append_bio(rq, bio); 686 if (unlikely(ret)) { 687 bio_uninit(bio); 688 kfree(bio); 689 } 690 return ret; 691 } 692 EXPORT_SYMBOL(blk_rq_map_kern); 693