1 // SPDX-License-Identifier: GPL-2.0
3 * Functions related to mapping data to requests
5 #include <linux/kernel.h>
6 #include <linux/sched/task_stack.h>
7 #include <linux/module.h>
9 #include <linux/blkdev.h>
10 #include <linux/uio.h>
20 static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data,
23 struct bio_map_data *bmd;
25 if (data->nr_segs > UIO_MAXIOV)
28 bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask);
31 memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs);
33 bmd->iter.iov = bmd->iov;
38 * bio_copy_from_iter - copy all pages from iov_iter to bio
39 * @bio: The &struct bio which describes the I/O as destination
40 * @iter: iov_iter as source
42 * Copy all pages from iov_iter to bio.
43 * Returns 0 on success, or error on failure.
45 static int bio_copy_from_iter(struct bio *bio, struct iov_iter *iter)
48 struct bvec_iter_all iter_all;
50 bio_for_each_segment_all(bvec, bio, iter_all) {
53 ret = copy_page_from_iter(bvec->bv_page,
58 if (!iov_iter_count(iter))
61 if (ret < bvec->bv_len)
69 * bio_copy_to_iter - copy all pages from bio to iov_iter
70 * @bio: The &struct bio which describes the I/O as source
71 * @iter: iov_iter as destination
73 * Copy all pages from bio to iov_iter.
74 * Returns 0 on success, or error on failure.
76 static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter)
79 struct bvec_iter_all iter_all;
81 bio_for_each_segment_all(bvec, bio, iter_all) {
84 ret = copy_page_to_iter(bvec->bv_page,
89 if (!iov_iter_count(&iter))
92 if (ret < bvec->bv_len)
100 * bio_uncopy_user - finish previously mapped bio
101 * @bio: bio being terminated
103 * Free pages allocated from bio_copy_user_iov() and write back data
104 * to user space in case of a read.
106 static int bio_uncopy_user(struct bio *bio)
108 struct bio_map_data *bmd = bio->bi_private;
111 if (!bio_flagged(bio, BIO_NULL_MAPPED)) {
113 * if we're in a workqueue, the request is orphaned, so
114 * don't copy into a random user address space, just free
115 * and return -EINTR so user space doesn't expect any data.
119 else if (bio_data_dir(bio) == READ)
120 ret = bio_copy_to_iter(bio, bmd->iter);
121 if (bmd->is_our_pages)
130 * bio_copy_user_iov - copy user data to bio
131 * @q: destination block queue
132 * @map_data: pointer to the rq_map_data holding pages (if necessary)
133 * @iter: iovec iterator
134 * @gfp_mask: memory allocation flags
136 * Prepares and returns a bio for indirect user io, bouncing data
137 * to/from kernel pages as necessary. Must be paired with
138 * call bio_uncopy_user() on io completion.
140 static struct bio *bio_copy_user_iov(struct request_queue *q,
141 struct rq_map_data *map_data, struct iov_iter *iter,
144 struct bio_map_data *bmd;
149 unsigned int len = iter->count;
150 unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0;
152 bmd = bio_alloc_map_data(iter, gfp_mask);
154 return ERR_PTR(-ENOMEM);
157 * We need to do a deep copy of the iov_iter including the iovecs.
158 * The caller provided iov might point to an on-stack or otherwise
161 bmd->is_our_pages = map_data ? 0 : 1;
163 nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE);
164 if (nr_pages > BIO_MAX_PAGES)
165 nr_pages = BIO_MAX_PAGES;
168 bio = bio_kmalloc(gfp_mask, nr_pages);
175 nr_pages = 1 << map_data->page_order;
176 i = map_data->offset / PAGE_SIZE;
179 unsigned int bytes = PAGE_SIZE;
187 if (i == map_data->nr_entries * nr_pages) {
192 page = map_data->pages[i / nr_pages];
193 page += (i % nr_pages);
197 page = alloc_page(q->bounce_gfp | gfp_mask);
204 if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes) {
218 map_data->offset += bio->bi_iter.bi_size;
223 if ((iov_iter_rw(iter) == WRITE &&
224 (!map_data || !map_data->null_mapped)) ||
225 (map_data && map_data->from_user)) {
226 ret = bio_copy_from_iter(bio, iter);
230 if (bmd->is_our_pages)
232 iov_iter_advance(iter, bio->bi_iter.bi_size);
235 bio->bi_private = bmd;
236 if (map_data && map_data->null_mapped)
237 bio_set_flag(bio, BIO_NULL_MAPPED);
249 * bio_map_user_iov - map user iovec into bio
250 * @q: the struct request_queue for the bio
251 * @iter: iovec iterator
252 * @gfp_mask: memory allocation flags
254 * Map the user space address into a bio suitable for io to a block
255 * device. Returns an error pointer in case of error.
257 static struct bio *bio_map_user_iov(struct request_queue *q,
258 struct iov_iter *iter, gfp_t gfp_mask)
264 if (!iov_iter_count(iter))
265 return ERR_PTR(-EINVAL);
267 bio = bio_kmalloc(gfp_mask, iov_iter_npages(iter, BIO_MAX_PAGES));
269 return ERR_PTR(-ENOMEM);
271 while (iov_iter_count(iter)) {
274 size_t offs, added = 0;
277 bytes = iov_iter_get_pages_alloc(iter, &pages, LONG_MAX, &offs);
278 if (unlikely(bytes <= 0)) {
279 ret = bytes ? bytes : -EFAULT;
283 npages = DIV_ROUND_UP(offs + bytes, PAGE_SIZE);
285 if (unlikely(offs & queue_dma_alignment(q))) {
289 for (j = 0; j < npages; j++) {
290 struct page *page = pages[j];
291 unsigned int n = PAGE_SIZE - offs;
292 bool same_page = false;
297 if (!__bio_add_pc_page(q, bio, page, n, offs,
308 iov_iter_advance(iter, added);
311 * release the pages we didn't map into the bio, if any
314 put_page(pages[j++]);
316 /* couldn't stuff something into bio? */
321 bio_set_flag(bio, BIO_USER_MAPPED);
324 * subtle -- if bio_map_user_iov() ended up bouncing a bio,
325 * it would normally disappear when its bi_end_io is run.
326 * however, we need it for the unmap, so grab an extra
333 bio_release_pages(bio, false);
339 * bio_unmap_user - unmap a bio
340 * @bio: the bio being unmapped
342 * Unmap a bio previously mapped by bio_map_user_iov(). Must be called from
345 * bio_unmap_user() may sleep.
347 static void bio_unmap_user(struct bio *bio)
349 bio_release_pages(bio, bio_data_dir(bio) == READ);
354 static void bio_invalidate_vmalloc_pages(struct bio *bio)
356 #ifdef ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
357 if (bio->bi_private && !op_is_write(bio_op(bio))) {
358 unsigned long i, len = 0;
360 for (i = 0; i < bio->bi_vcnt; i++)
361 len += bio->bi_io_vec[i].bv_len;
362 invalidate_kernel_vmap_range(bio->bi_private, len);
367 static void bio_map_kern_endio(struct bio *bio)
369 bio_invalidate_vmalloc_pages(bio);
374 * bio_map_kern - map kernel address into bio
375 * @q: the struct request_queue for the bio
376 * @data: pointer to buffer to map
377 * @len: length in bytes
378 * @gfp_mask: allocation flags for bio allocation
380 * Map the kernel address into a bio suitable for io to a block
381 * device. Returns an error pointer in case of error.
383 static struct bio *bio_map_kern(struct request_queue *q, void *data,
384 unsigned int len, gfp_t gfp_mask)
386 unsigned long kaddr = (unsigned long)data;
387 unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
388 unsigned long start = kaddr >> PAGE_SHIFT;
389 const int nr_pages = end - start;
390 bool is_vmalloc = is_vmalloc_addr(data);
395 bio = bio_kmalloc(gfp_mask, nr_pages);
397 return ERR_PTR(-ENOMEM);
400 flush_kernel_vmap_range(data, len);
401 bio->bi_private = data;
404 offset = offset_in_page(kaddr);
405 for (i = 0; i < nr_pages; i++) {
406 unsigned int bytes = PAGE_SIZE - offset;
415 page = virt_to_page(data);
417 page = vmalloc_to_page(data);
418 if (bio_add_pc_page(q, bio, page, bytes,
420 /* we don't support partial mappings */
422 return ERR_PTR(-EINVAL);
430 bio->bi_end_io = bio_map_kern_endio;
434 static void bio_copy_kern_endio(struct bio *bio)
440 static void bio_copy_kern_endio_read(struct bio *bio)
442 char *p = bio->bi_private;
443 struct bio_vec *bvec;
444 struct bvec_iter_all iter_all;
446 bio_for_each_segment_all(bvec, bio, iter_all) {
447 memcpy(p, page_address(bvec->bv_page), bvec->bv_len);
451 bio_copy_kern_endio(bio);
455 * bio_copy_kern - copy kernel address into bio
456 * @q: the struct request_queue for the bio
457 * @data: pointer to buffer to copy
458 * @len: length in bytes
459 * @gfp_mask: allocation flags for bio and page allocation
460 * @reading: data direction is READ
462 * copy the kernel address into a bio suitable for io to a block
463 * device. Returns an error pointer in case of error.
465 static struct bio *bio_copy_kern(struct request_queue *q, void *data,
466 unsigned int len, gfp_t gfp_mask, int reading)
468 unsigned long kaddr = (unsigned long)data;
469 unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
470 unsigned long start = kaddr >> PAGE_SHIFT;
479 return ERR_PTR(-EINVAL);
481 nr_pages = end - start;
482 bio = bio_kmalloc(gfp_mask, nr_pages);
484 return ERR_PTR(-ENOMEM);
488 unsigned int bytes = PAGE_SIZE;
493 page = alloc_page(q->bounce_gfp | gfp_mask);
498 memcpy(page_address(page), p, bytes);
500 if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes)
508 bio->bi_end_io = bio_copy_kern_endio_read;
509 bio->bi_private = data;
511 bio->bi_end_io = bio_copy_kern_endio;
519 return ERR_PTR(-ENOMEM);
523 * Append a bio to a passthrough request. Only works if the bio can be merged
524 * into the request based on the driver constraints.
526 int blk_rq_append_bio(struct request *rq, struct bio **bio)
528 struct bio *orig_bio = *bio;
529 struct bvec_iter iter;
531 unsigned int nr_segs = 0;
533 blk_queue_bounce(rq->q, bio);
535 bio_for_each_bvec(bv, *bio, iter)
539 blk_rq_bio_prep(rq, *bio, nr_segs);
541 if (!ll_back_merge_fn(rq, *bio, nr_segs)) {
542 if (orig_bio != *bio) {
549 rq->biotail->bi_next = *bio;
551 rq->__data_len += (*bio)->bi_iter.bi_size;
556 EXPORT_SYMBOL(blk_rq_append_bio);
558 static int __blk_rq_unmap_user(struct bio *bio)
563 if (bio_flagged(bio, BIO_USER_MAPPED))
566 ret = bio_uncopy_user(bio);
572 static int __blk_rq_map_user_iov(struct request *rq,
573 struct rq_map_data *map_data, struct iov_iter *iter,
574 gfp_t gfp_mask, bool copy)
576 struct request_queue *q = rq->q;
577 struct bio *bio, *orig_bio;
581 bio = bio_copy_user_iov(q, map_data, iter, gfp_mask);
583 bio = bio_map_user_iov(q, iter, gfp_mask);
588 bio->bi_opf &= ~REQ_OP_MASK;
589 bio->bi_opf |= req_op(rq);
594 * We link the bounce buffer in and could have to traverse it
595 * later so we have to get a ref to prevent it from being freed
597 ret = blk_rq_append_bio(rq, &bio);
599 __blk_rq_unmap_user(orig_bio);
608 * blk_rq_map_user_iov - map user data to a request, for passthrough requests
609 * @q: request queue where request should be inserted
610 * @rq: request to map data to
611 * @map_data: pointer to the rq_map_data holding pages (if necessary)
612 * @iter: iovec iterator
613 * @gfp_mask: memory allocation flags
616 * Data will be mapped directly for zero copy I/O, if possible. Otherwise
617 * a kernel bounce buffer is used.
619 * A matching blk_rq_unmap_user() must be issued at the end of I/O, while
620 * still in process context.
622 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
623 * before being submitted to the device, as pages mapped may be out of
624 * reach. It's the callers responsibility to make sure this happens. The
625 * original bio must be passed back in to blk_rq_unmap_user() for proper
628 int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
629 struct rq_map_data *map_data,
630 const struct iov_iter *iter, gfp_t gfp_mask)
633 unsigned long align = q->dma_pad_mask | queue_dma_alignment(q);
634 struct bio *bio = NULL;
638 if (!iter_is_iovec(iter))
643 else if (iov_iter_alignment(iter) & align)
645 else if (queue_virt_boundary(q))
646 copy = queue_virt_boundary(q) & iov_iter_gap_alignment(iter);
650 ret =__blk_rq_map_user_iov(rq, map_data, &i, gfp_mask, copy);
655 } while (iov_iter_count(&i));
657 if (!bio_flagged(bio, BIO_USER_MAPPED))
658 rq->rq_flags |= RQF_COPY_USER;
662 blk_rq_unmap_user(bio);
667 EXPORT_SYMBOL(blk_rq_map_user_iov);
669 int blk_rq_map_user(struct request_queue *q, struct request *rq,
670 struct rq_map_data *map_data, void __user *ubuf,
671 unsigned long len, gfp_t gfp_mask)
675 int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i);
677 if (unlikely(ret < 0))
680 return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask);
682 EXPORT_SYMBOL(blk_rq_map_user);
685 * blk_rq_unmap_user - unmap a request with user data
686 * @bio: start of bio list
689 * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
690 * supply the original rq->bio from the blk_rq_map_user() return, since
691 * the I/O completion may have changed rq->bio.
693 int blk_rq_unmap_user(struct bio *bio)
695 struct bio *mapped_bio;
700 if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
701 mapped_bio = bio->bi_private;
703 ret2 = __blk_rq_unmap_user(mapped_bio);
714 EXPORT_SYMBOL(blk_rq_unmap_user);
717 * blk_rq_map_kern - map kernel data to a request, for passthrough requests
718 * @q: request queue where request should be inserted
719 * @rq: request to fill
720 * @kbuf: the kernel buffer
721 * @len: length of user data
722 * @gfp_mask: memory allocation flags
725 * Data will be mapped directly if possible. Otherwise a bounce
726 * buffer is used. Can be called multiple times to append multiple
729 int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
730 unsigned int len, gfp_t gfp_mask)
732 int reading = rq_data_dir(rq) == READ;
733 unsigned long addr = (unsigned long) kbuf;
735 struct bio *bio, *orig_bio;
738 if (len > (queue_max_hw_sectors(q) << 9))
743 do_copy = !blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf);
745 bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
747 bio = bio_map_kern(q, kbuf, len, gfp_mask);
752 bio->bi_opf &= ~REQ_OP_MASK;
753 bio->bi_opf |= req_op(rq);
756 rq->rq_flags |= RQF_COPY_USER;
759 ret = blk_rq_append_bio(rq, &bio);
761 /* request is too big */
768 EXPORT_SYMBOL(blk_rq_map_kern);