1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <crypto/hash.h>
3 #include <linux/export.h>
4 #include <linux/bvec.h>
5 #include <linux/fault-inject-usercopy.h>
7 #include <linux/pagemap.h>
8 #include <linux/highmem.h>
9 #include <linux/slab.h>
10 #include <linux/vmalloc.h>
11 #include <linux/splice.h>
12 #include <linux/compat.h>
13 #include <net/checksum.h>
14 #include <linux/scatterlist.h>
15 #include <linux/instrumented.h>
17 /* covers ubuf and kbuf alike */
18 #define iterate_buf(i, n, base, len, off, __p, STEP) { \
19 size_t __maybe_unused off = 0; \
21 base = __p + i->iov_offset; \
23 i->iov_offset += len; \
27 /* covers iovec and kvec alike */
28 #define iterate_iovec(i, n, base, len, off, __p, STEP) { \
30 size_t skip = i->iov_offset; \
32 len = min(n, __p->iov_len - skip); \
34 base = __p->iov_base + skip; \
39 if (skip < __p->iov_len) \
45 i->iov_offset = skip; \
49 #define iterate_bvec(i, n, base, len, off, p, STEP) { \
51 unsigned skip = i->iov_offset; \
53 unsigned offset = p->bv_offset + skip; \
55 void *kaddr = kmap_local_page(p->bv_page + \
56 offset / PAGE_SIZE); \
57 base = kaddr + offset % PAGE_SIZE; \
58 len = min(min(n, (size_t)(p->bv_len - skip)), \
59 (size_t)(PAGE_SIZE - offset % PAGE_SIZE)); \
61 kunmap_local(kaddr); \
65 if (skip == p->bv_len) { \
73 i->iov_offset = skip; \
77 #define iterate_xarray(i, n, base, len, __off, STEP) { \
80 struct folio *folio; \
81 loff_t start = i->xarray_start + i->iov_offset; \
82 pgoff_t index = start / PAGE_SIZE; \
83 XA_STATE(xas, i->xarray, index); \
85 len = PAGE_SIZE - offset_in_page(start); \
87 xas_for_each(&xas, folio, ULONG_MAX) { \
90 if (xas_retry(&xas, folio)) \
92 if (WARN_ON(xa_is_value(folio))) \
94 if (WARN_ON(folio_test_hugetlb(folio))) \
96 offset = offset_in_folio(folio, start + __off); \
97 while (offset < folio_size(folio)) { \
98 base = kmap_local_folio(folio, offset); \
101 kunmap_local(base); \
105 if (left || n == 0) \
113 i->iov_offset += __off; \
117 #define __iterate_and_advance(i, n, base, len, off, I, K) { \
118 if (unlikely(i->count < n)) \
121 if (likely(iter_is_ubuf(i))) { \
124 iterate_buf(i, n, base, len, off, \
126 } else if (likely(iter_is_iovec(i))) { \
127 const struct iovec *iov = iter_iov(i); \
130 iterate_iovec(i, n, base, len, off, \
132 i->nr_segs -= iov - iter_iov(i); \
134 } else if (iov_iter_is_bvec(i)) { \
135 const struct bio_vec *bvec = i->bvec; \
138 iterate_bvec(i, n, base, len, off, \
140 i->nr_segs -= bvec - i->bvec; \
142 } else if (iov_iter_is_kvec(i)) { \
143 const struct kvec *kvec = i->kvec; \
146 iterate_iovec(i, n, base, len, off, \
148 i->nr_segs -= kvec - i->kvec; \
150 } else if (iov_iter_is_xarray(i)) { \
153 iterate_xarray(i, n, base, len, off, \
159 #define iterate_and_advance(i, n, base, len, off, I, K) \
160 __iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
162 static int copyout(void __user *to, const void *from, size_t n)
164 if (should_fail_usercopy())
166 if (access_ok(to, n)) {
167 instrument_copy_to_user(to, from, n);
168 n = raw_copy_to_user(to, from, n);
173 static int copyout_nofault(void __user *to, const void *from, size_t n)
177 if (should_fail_usercopy())
180 res = copy_to_user_nofault(to, from, n);
182 return res < 0 ? n : res;
185 static int copyin(void *to, const void __user *from, size_t n)
189 if (should_fail_usercopy())
191 if (access_ok(from, n)) {
192 instrument_copy_from_user_before(to, from, n);
193 res = raw_copy_from_user(to, from, n);
194 instrument_copy_from_user_after(to, from, n, res);
200 * fault_in_iov_iter_readable - fault in iov iterator for reading
202 * @size: maximum length
204 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
205 * @size. For each iovec, fault in each page that constitutes the iovec.
207 * Returns the number of bytes not faulted in (like copy_to_user() and
210 * Always returns 0 for non-userspace iterators.
212 size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
214 if (iter_is_ubuf(i)) {
215 size_t n = min(size, iov_iter_count(i));
216 n -= fault_in_readable(i->ubuf + i->iov_offset, n);
218 } else if (iter_is_iovec(i)) {
219 size_t count = min(size, iov_iter_count(i));
220 const struct iovec *p;
224 for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) {
225 size_t len = min(count, p->iov_len - skip);
230 ret = fault_in_readable(p->iov_base + skip, len);
239 EXPORT_SYMBOL(fault_in_iov_iter_readable);
242 * fault_in_iov_iter_writeable - fault in iov iterator for writing
244 * @size: maximum length
246 * Faults in the iterator using get_user_pages(), i.e., without triggering
247 * hardware page faults. This is primarily useful when we already know that
248 * some or all of the pages in @i aren't in memory.
250 * Returns the number of bytes not faulted in, like copy_to_user() and
253 * Always returns 0 for non-user-space iterators.
255 size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
257 if (iter_is_ubuf(i)) {
258 size_t n = min(size, iov_iter_count(i));
259 n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n);
261 } else if (iter_is_iovec(i)) {
262 size_t count = min(size, iov_iter_count(i));
263 const struct iovec *p;
267 for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) {
268 size_t len = min(count, p->iov_len - skip);
273 ret = fault_in_safe_writeable(p->iov_base + skip, len);
282 EXPORT_SYMBOL(fault_in_iov_iter_writeable);
284 void iov_iter_init(struct iov_iter *i, unsigned int direction,
285 const struct iovec *iov, unsigned long nr_segs,
288 WARN_ON(direction & ~(READ | WRITE));
289 *i = (struct iov_iter) {
290 .iter_type = ITER_IOVEC,
294 .data_source = direction,
301 EXPORT_SYMBOL(iov_iter_init);
303 static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
304 __wsum sum, size_t off)
306 __wsum next = csum_partial_copy_nocheck(from, to, len);
307 return csum_block_add(sum, next, off);
310 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
312 if (WARN_ON_ONCE(i->data_source))
314 if (user_backed_iter(i))
316 iterate_and_advance(i, bytes, base, len, off,
317 copyout(base, addr + off, len),
318 memcpy(base, addr + off, len)
323 EXPORT_SYMBOL(_copy_to_iter);
325 #ifdef CONFIG_ARCH_HAS_COPY_MC
326 static int copyout_mc(void __user *to, const void *from, size_t n)
328 if (access_ok(to, n)) {
329 instrument_copy_to_user(to, from, n);
330 n = copy_mc_to_user((__force void *) to, from, n);
336 * _copy_mc_to_iter - copy to iter with source memory error exception handling
337 * @addr: source kernel address
338 * @bytes: total transfer length
339 * @i: destination iterator
341 * The pmem driver deploys this for the dax operation
342 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
343 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
344 * successfully copied.
346 * The main differences between this and typical _copy_to_iter().
348 * * Typical tail/residue handling after a fault retries the copy
349 * byte-by-byte until the fault happens again. Re-triggering machine
350 * checks is potentially fatal so the implementation uses source
351 * alignment and poison alignment assumptions to avoid re-triggering
352 * hardware exceptions.
354 * * ITER_KVEC and ITER_BVEC can return short copies. Compare to
355 * copy_to_iter() where only ITER_IOVEC attempts might return a short copy.
357 * Return: number of bytes copied (may be %0)
359 size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
361 if (WARN_ON_ONCE(i->data_source))
363 if (user_backed_iter(i))
365 __iterate_and_advance(i, bytes, base, len, off,
366 copyout_mc(base, addr + off, len),
367 copy_mc_to_kernel(base, addr + off, len)
372 EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
373 #endif /* CONFIG_ARCH_HAS_COPY_MC */
375 static void *memcpy_from_iter(struct iov_iter *i, void *to, const void *from,
378 if (iov_iter_is_copy_mc(i))
379 return (void *)copy_mc_to_kernel(to, from, size);
380 return memcpy(to, from, size);
383 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
385 if (WARN_ON_ONCE(!i->data_source))
388 if (user_backed_iter(i))
390 iterate_and_advance(i, bytes, base, len, off,
391 copyin(addr + off, base, len),
392 memcpy_from_iter(i, addr + off, base, len)
397 EXPORT_SYMBOL(_copy_from_iter);
399 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
401 if (WARN_ON_ONCE(!i->data_source))
404 iterate_and_advance(i, bytes, base, len, off,
405 __copy_from_user_inatomic_nocache(addr + off, base, len),
406 memcpy(addr + off, base, len)
411 EXPORT_SYMBOL(_copy_from_iter_nocache);
413 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
415 * _copy_from_iter_flushcache - write destination through cpu cache
416 * @addr: destination kernel address
417 * @bytes: total transfer length
418 * @i: source iterator
420 * The pmem driver arranges for filesystem-dax to use this facility via
421 * dax_copy_from_iter() for ensuring that writes to persistent memory
422 * are flushed through the CPU cache. It is differentiated from
423 * _copy_from_iter_nocache() in that guarantees all data is flushed for
424 * all iterator types. The _copy_from_iter_nocache() only attempts to
425 * bypass the cache for the ITER_IOVEC case, and on some archs may use
426 * instructions that strand dirty-data in the cache.
428 * Return: number of bytes copied (may be %0)
430 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
432 if (WARN_ON_ONCE(!i->data_source))
435 iterate_and_advance(i, bytes, base, len, off,
436 __copy_from_user_flushcache(addr + off, base, len),
437 memcpy_flushcache(addr + off, base, len)
442 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
445 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
448 size_t v = n + offset;
451 * The general case needs to access the page order in order
452 * to compute the page size.
453 * However, we mostly deal with order-0 pages and thus can
454 * avoid a possible cache line miss for requests that fit all
457 if (n <= v && v <= PAGE_SIZE)
460 head = compound_head(page);
461 v += (page - head) << PAGE_SHIFT;
463 if (WARN_ON(n > v || v > page_size(head)))
468 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
472 if (!page_copy_sane(page, offset, bytes))
474 if (WARN_ON_ONCE(i->data_source))
476 page += offset / PAGE_SIZE; // first subpage
479 void *kaddr = kmap_local_page(page);
480 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
481 n = _copy_to_iter(kaddr + offset, n, i);
488 if (offset == PAGE_SIZE) {
495 EXPORT_SYMBOL(copy_page_to_iter);
497 size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, size_t bytes,
502 if (!page_copy_sane(page, offset, bytes))
504 if (WARN_ON_ONCE(i->data_source))
506 page += offset / PAGE_SIZE; // first subpage
509 void *kaddr = kmap_local_page(page);
510 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
512 iterate_and_advance(i, n, base, len, off,
513 copyout_nofault(base, kaddr + offset + off, len),
514 memcpy(base, kaddr + offset + off, len)
522 if (offset == PAGE_SIZE) {
529 EXPORT_SYMBOL(copy_page_to_iter_nofault);
531 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
535 if (!page_copy_sane(page, offset, bytes))
537 page += offset / PAGE_SIZE; // first subpage
540 void *kaddr = kmap_local_page(page);
541 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
542 n = _copy_from_iter(kaddr + offset, n, i);
549 if (offset == PAGE_SIZE) {
556 EXPORT_SYMBOL(copy_page_from_iter);
558 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
560 iterate_and_advance(i, bytes, base, len, count,
561 clear_user(base, len),
567 EXPORT_SYMBOL(iov_iter_zero);
569 size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
572 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
573 if (!page_copy_sane(page, offset, bytes)) {
574 kunmap_atomic(kaddr);
577 if (WARN_ON_ONCE(!i->data_source)) {
578 kunmap_atomic(kaddr);
581 iterate_and_advance(i, bytes, base, len, off,
582 copyin(p + off, base, len),
583 memcpy_from_iter(i, p + off, base, len)
585 kunmap_atomic(kaddr);
588 EXPORT_SYMBOL(copy_page_from_iter_atomic);
590 static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
592 const struct bio_vec *bvec, *end;
598 size += i->iov_offset;
600 for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
601 if (likely(size < bvec->bv_len))
603 size -= bvec->bv_len;
605 i->iov_offset = size;
606 i->nr_segs -= bvec - i->bvec;
610 static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
612 const struct iovec *iov, *end;
618 size += i->iov_offset; // from beginning of current segment
619 for (iov = iter_iov(i), end = iov + i->nr_segs; iov < end; iov++) {
620 if (likely(size < iov->iov_len))
622 size -= iov->iov_len;
624 i->iov_offset = size;
625 i->nr_segs -= iov - iter_iov(i);
629 void iov_iter_advance(struct iov_iter *i, size_t size)
631 if (unlikely(i->count < size))
633 if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
634 i->iov_offset += size;
636 } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
637 /* iovec and kvec have identical layouts */
638 iov_iter_iovec_advance(i, size);
639 } else if (iov_iter_is_bvec(i)) {
640 iov_iter_bvec_advance(i, size);
641 } else if (iov_iter_is_discard(i)) {
645 EXPORT_SYMBOL(iov_iter_advance);
647 void iov_iter_revert(struct iov_iter *i, size_t unroll)
651 if (WARN_ON(unroll > MAX_RW_COUNT))
654 if (unlikely(iov_iter_is_discard(i)))
656 if (unroll <= i->iov_offset) {
657 i->iov_offset -= unroll;
660 unroll -= i->iov_offset;
661 if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
662 BUG(); /* We should never go beyond the start of the specified
663 * range since we might then be straying into pages that
666 } else if (iov_iter_is_bvec(i)) {
667 const struct bio_vec *bvec = i->bvec;
669 size_t n = (--bvec)->bv_len;
673 i->iov_offset = n - unroll;
678 } else { /* same logics for iovec and kvec */
679 const struct iovec *iov = iter_iov(i);
681 size_t n = (--iov)->iov_len;
685 i->iov_offset = n - unroll;
692 EXPORT_SYMBOL(iov_iter_revert);
695 * Return the count of just the current iov_iter segment.
697 size_t iov_iter_single_seg_count(const struct iov_iter *i)
699 if (i->nr_segs > 1) {
700 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
701 return min(i->count, iter_iov(i)->iov_len - i->iov_offset);
702 if (iov_iter_is_bvec(i))
703 return min(i->count, i->bvec->bv_len - i->iov_offset);
707 EXPORT_SYMBOL(iov_iter_single_seg_count);
709 void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
710 const struct kvec *kvec, unsigned long nr_segs,
713 WARN_ON(direction & ~(READ | WRITE));
714 *i = (struct iov_iter){
715 .iter_type = ITER_KVEC,
717 .data_source = direction,
724 EXPORT_SYMBOL(iov_iter_kvec);
726 void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
727 const struct bio_vec *bvec, unsigned long nr_segs,
730 WARN_ON(direction & ~(READ | WRITE));
731 *i = (struct iov_iter){
732 .iter_type = ITER_BVEC,
734 .data_source = direction,
741 EXPORT_SYMBOL(iov_iter_bvec);
744 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
745 * @i: The iterator to initialise.
746 * @direction: The direction of the transfer.
747 * @xarray: The xarray to access.
748 * @start: The start file position.
749 * @count: The size of the I/O buffer in bytes.
751 * Set up an I/O iterator to either draw data out of the pages attached to an
752 * inode or to inject data into those pages. The pages *must* be prevented
753 * from evaporation, either by taking a ref on them or locking them by the
756 void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
757 struct xarray *xarray, loff_t start, size_t count)
759 BUG_ON(direction & ~1);
760 *i = (struct iov_iter) {
761 .iter_type = ITER_XARRAY,
763 .data_source = direction,
765 .xarray_start = start,
770 EXPORT_SYMBOL(iov_iter_xarray);
773 * iov_iter_discard - Initialise an I/O iterator that discards data
774 * @i: The iterator to initialise.
775 * @direction: The direction of the transfer.
776 * @count: The size of the I/O buffer in bytes.
778 * Set up an I/O iterator that just discards everything that's written to it.
779 * It's only available as a READ iterator.
781 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
783 BUG_ON(direction != READ);
784 *i = (struct iov_iter){
785 .iter_type = ITER_DISCARD,
787 .data_source = false,
792 EXPORT_SYMBOL(iov_iter_discard);
794 static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
797 size_t size = i->count;
798 size_t skip = i->iov_offset;
801 for (k = 0; k < i->nr_segs; k++, skip = 0) {
802 const struct iovec *iov = iter_iov(i) + k;
803 size_t len = iov->iov_len - skip;
809 if ((unsigned long)(iov->iov_base + skip) & addr_mask)
819 static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
822 size_t size = i->count;
823 unsigned skip = i->iov_offset;
826 for (k = 0; k < i->nr_segs; k++, skip = 0) {
827 size_t len = i->bvec[k].bv_len - skip;
833 if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
844 * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
845 * are aligned to the parameters.
847 * @i: &struct iov_iter to restore
848 * @addr_mask: bit mask to check against the iov element's addresses
849 * @len_mask: bit mask to check against the iov element's lengths
851 * Return: false if any addresses or lengths intersect with the provided masks
853 bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
856 if (likely(iter_is_ubuf(i))) {
857 if (i->count & len_mask)
859 if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
864 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
865 return iov_iter_aligned_iovec(i, addr_mask, len_mask);
867 if (iov_iter_is_bvec(i))
868 return iov_iter_aligned_bvec(i, addr_mask, len_mask);
870 if (iov_iter_is_xarray(i)) {
871 if (i->count & len_mask)
873 if ((i->xarray_start + i->iov_offset) & addr_mask)
879 EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
881 static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
883 unsigned long res = 0;
884 size_t size = i->count;
885 size_t skip = i->iov_offset;
888 for (k = 0; k < i->nr_segs; k++, skip = 0) {
889 const struct iovec *iov = iter_iov(i) + k;
890 size_t len = iov->iov_len - skip;
892 res |= (unsigned long)iov->iov_base + skip;
904 static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
907 size_t size = i->count;
908 unsigned skip = i->iov_offset;
911 for (k = 0; k < i->nr_segs; k++, skip = 0) {
912 size_t len = i->bvec[k].bv_len - skip;
913 res |= (unsigned long)i->bvec[k].bv_offset + skip;
924 unsigned long iov_iter_alignment(const struct iov_iter *i)
926 if (likely(iter_is_ubuf(i))) {
927 size_t size = i->count;
929 return ((unsigned long)i->ubuf + i->iov_offset) | size;
933 /* iovec and kvec have identical layouts */
934 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
935 return iov_iter_alignment_iovec(i);
937 if (iov_iter_is_bvec(i))
938 return iov_iter_alignment_bvec(i);
940 if (iov_iter_is_xarray(i))
941 return (i->xarray_start + i->iov_offset) | i->count;
945 EXPORT_SYMBOL(iov_iter_alignment);
947 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
949 unsigned long res = 0;
951 size_t size = i->count;
957 if (WARN_ON(!iter_is_iovec(i)))
960 for (k = 0; k < i->nr_segs; k++) {
961 const struct iovec *iov = iter_iov(i) + k;
963 unsigned long base = (unsigned long)iov->iov_base;
964 if (v) // if not the first one
965 res |= base | v; // this start | previous end
966 v = base + iov->iov_len;
967 if (size <= iov->iov_len)
969 size -= iov->iov_len;
974 EXPORT_SYMBOL(iov_iter_gap_alignment);
976 static int want_pages_array(struct page ***res, size_t size,
977 size_t start, unsigned int maxpages)
979 unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE);
981 if (count > maxpages)
983 WARN_ON(!count); // caller should've prevented that
985 *res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
992 static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
993 pgoff_t index, unsigned int nr_pages)
995 XA_STATE(xas, xa, index);
997 unsigned int ret = 0;
1000 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1001 if (xas_retry(&xas, page))
1004 /* Has the page moved or been split? */
1005 if (unlikely(page != xas_reload(&xas))) {
1010 pages[ret] = find_subpage(page, xas.xa_index);
1011 get_page(pages[ret]);
1012 if (++ret == nr_pages)
1019 static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1020 struct page ***pages, size_t maxsize,
1021 unsigned maxpages, size_t *_start_offset)
1023 unsigned nr, offset, count;
1027 pos = i->xarray_start + i->iov_offset;
1028 index = pos >> PAGE_SHIFT;
1029 offset = pos & ~PAGE_MASK;
1030 *_start_offset = offset;
1032 count = want_pages_array(pages, maxsize, offset, maxpages);
1035 nr = iter_xarray_populate_pages(*pages, i->xarray, index, count);
1039 maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1040 i->iov_offset += maxsize;
1041 i->count -= maxsize;
1045 /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
1046 static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
1051 if (iter_is_ubuf(i))
1052 return (unsigned long)i->ubuf + i->iov_offset;
1054 for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1055 const struct iovec *iov = iter_iov(i) + k;
1056 size_t len = iov->iov_len - skip;
1062 return (unsigned long)iov->iov_base + skip;
1064 BUG(); // if it had been empty, we wouldn't get called
1067 /* must be done on non-empty ITER_BVEC one */
1068 static struct page *first_bvec_segment(const struct iov_iter *i,
1069 size_t *size, size_t *start)
1072 size_t skip = i->iov_offset, len;
1074 len = i->bvec->bv_len - skip;
1077 skip += i->bvec->bv_offset;
1078 page = i->bvec->bv_page + skip / PAGE_SIZE;
1079 *start = skip % PAGE_SIZE;
1083 static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
1084 struct page ***pages, size_t maxsize,
1085 unsigned int maxpages, size_t *start)
1087 unsigned int n, gup_flags = 0;
1089 if (maxsize > i->count)
1093 if (maxsize > MAX_RW_COUNT)
1094 maxsize = MAX_RW_COUNT;
1096 if (likely(user_backed_iter(i))) {
1100 if (iov_iter_rw(i) != WRITE)
1101 gup_flags |= FOLL_WRITE;
1103 gup_flags |= FOLL_NOFAULT;
1105 addr = first_iovec_segment(i, &maxsize);
1106 *start = addr % PAGE_SIZE;
1108 n = want_pages_array(pages, maxsize, *start, maxpages);
1111 res = get_user_pages_fast(addr, n, gup_flags, *pages);
1112 if (unlikely(res <= 0))
1114 maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1115 iov_iter_advance(i, maxsize);
1118 if (iov_iter_is_bvec(i)) {
1122 page = first_bvec_segment(i, &maxsize, start);
1123 n = want_pages_array(pages, maxsize, *start, maxpages);
1127 for (int k = 0; k < n; k++)
1128 get_page(p[k] = page + k);
1129 maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1130 i->count -= maxsize;
1131 i->iov_offset += maxsize;
1132 if (i->iov_offset == i->bvec->bv_len) {
1139 if (iov_iter_is_xarray(i))
1140 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1144 ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
1145 size_t maxsize, unsigned maxpages, size_t *start)
1151 return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, start);
1153 EXPORT_SYMBOL(iov_iter_get_pages2);
1155 ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i,
1156 struct page ***pages, size_t maxsize, size_t *start)
1162 len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start);
1169 EXPORT_SYMBOL(iov_iter_get_pages_alloc2);
1171 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1176 if (WARN_ON_ONCE(!i->data_source))
1179 iterate_and_advance(i, bytes, base, len, off, ({
1180 next = csum_and_copy_from_user(base, addr + off, len);
1181 sum = csum_block_add(sum, next, off);
1184 sum = csum_and_memcpy(addr + off, base, len, sum, off);
1190 EXPORT_SYMBOL(csum_and_copy_from_iter);
1192 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1195 struct csum_state *csstate = _csstate;
1198 if (WARN_ON_ONCE(i->data_source))
1200 if (unlikely(iov_iter_is_discard(i))) {
1201 // can't use csum_memcpy() for that one - data is not copied
1202 csstate->csum = csum_block_add(csstate->csum,
1203 csum_partial(addr, bytes, 0),
1205 csstate->off += bytes;
1209 sum = csum_shift(csstate->csum, csstate->off);
1210 iterate_and_advance(i, bytes, base, len, off, ({
1211 next = csum_and_copy_to_user(addr + off, base, len);
1212 sum = csum_block_add(sum, next, off);
1215 sum = csum_and_memcpy(base, addr + off, len, sum, off);
1218 csstate->csum = csum_shift(sum, csstate->off);
1219 csstate->off += bytes;
1222 EXPORT_SYMBOL(csum_and_copy_to_iter);
1224 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1227 #ifdef CONFIG_CRYPTO_HASH
1228 struct ahash_request *hash = hashp;
1229 struct scatterlist sg;
1232 copied = copy_to_iter(addr, bytes, i);
1233 sg_init_one(&sg, addr, copied);
1234 ahash_request_set_crypt(hash, &sg, NULL, copied);
1235 crypto_ahash_update(hash);
1241 EXPORT_SYMBOL(hash_and_copy_to_iter);
1243 static int iov_npages(const struct iov_iter *i, int maxpages)
1245 size_t skip = i->iov_offset, size = i->count;
1246 const struct iovec *p;
1249 for (p = iter_iov(i); size; skip = 0, p++) {
1250 unsigned offs = offset_in_page(p->iov_base + skip);
1251 size_t len = min(p->iov_len - skip, size);
1255 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1256 if (unlikely(npages > maxpages))
1263 static int bvec_npages(const struct iov_iter *i, int maxpages)
1265 size_t skip = i->iov_offset, size = i->count;
1266 const struct bio_vec *p;
1269 for (p = i->bvec; size; skip = 0, p++) {
1270 unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
1271 size_t len = min(p->bv_len - skip, size);
1274 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1275 if (unlikely(npages > maxpages))
1281 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1283 if (unlikely(!i->count))
1285 if (likely(iter_is_ubuf(i))) {
1286 unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1287 int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1288 return min(npages, maxpages);
1290 /* iovec and kvec have identical layouts */
1291 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1292 return iov_npages(i, maxpages);
1293 if (iov_iter_is_bvec(i))
1294 return bvec_npages(i, maxpages);
1295 if (iov_iter_is_xarray(i)) {
1296 unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1297 int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
1298 return min(npages, maxpages);
1302 EXPORT_SYMBOL(iov_iter_npages);
1304 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1307 if (iov_iter_is_bvec(new))
1308 return new->bvec = kmemdup(new->bvec,
1309 new->nr_segs * sizeof(struct bio_vec),
1311 else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1312 /* iovec and kvec have identical layout */
1313 return new->__iov = kmemdup(new->__iov,
1314 new->nr_segs * sizeof(struct iovec),
1318 EXPORT_SYMBOL(dup_iter);
1320 static __noclone int copy_compat_iovec_from_user(struct iovec *iov,
1321 const struct iovec __user *uvec, unsigned long nr_segs)
1323 const struct compat_iovec __user *uiov =
1324 (const struct compat_iovec __user *)uvec;
1325 int ret = -EFAULT, i;
1327 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1330 for (i = 0; i < nr_segs; i++) {
1334 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1335 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1337 /* check for compat_size_t not fitting in compat_ssize_t .. */
1342 iov[i].iov_base = compat_ptr(buf);
1343 iov[i].iov_len = len;
1352 static __noclone int copy_iovec_from_user(struct iovec *iov,
1353 const struct iovec __user *uiov, unsigned long nr_segs)
1357 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1364 unsafe_get_user(len, &uiov->iov_len, uaccess_end);
1365 unsafe_get_user(buf, &uiov->iov_base, uaccess_end);
1367 /* check for size_t not fitting in ssize_t .. */
1368 if (unlikely(len < 0)) {
1372 iov->iov_base = buf;
1376 } while (--nr_segs);
1384 struct iovec *iovec_from_user(const struct iovec __user *uvec,
1385 unsigned long nr_segs, unsigned long fast_segs,
1386 struct iovec *fast_iov, bool compat)
1388 struct iovec *iov = fast_iov;
1392 * SuS says "The readv() function *may* fail if the iovcnt argument was
1393 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
1394 * traditionally returned zero for zero segments, so...
1398 if (nr_segs > UIO_MAXIOV)
1399 return ERR_PTR(-EINVAL);
1400 if (nr_segs > fast_segs) {
1401 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1403 return ERR_PTR(-ENOMEM);
1406 if (unlikely(compat))
1407 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1409 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1411 if (iov != fast_iov)
1413 return ERR_PTR(ret);
1420 * Single segment iovec supplied by the user, import it as ITER_UBUF.
1422 static ssize_t __import_iovec_ubuf(int type, const struct iovec __user *uvec,
1423 struct iovec **iovp, struct iov_iter *i,
1426 struct iovec *iov = *iovp;
1430 ret = copy_compat_iovec_from_user(iov, uvec, 1);
1432 ret = copy_iovec_from_user(iov, uvec, 1);
1436 ret = import_ubuf(type, iov->iov_base, iov->iov_len, i);
1443 ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1444 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1445 struct iov_iter *i, bool compat)
1447 ssize_t total_len = 0;
1452 return __import_iovec_ubuf(type, uvec, iovp, i, compat);
1454 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1457 return PTR_ERR(iov);
1461 * According to the Single Unix Specification we should return EINVAL if
1462 * an element length is < 0 when cast to ssize_t or if the total length
1463 * would overflow the ssize_t return value of the system call.
1465 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1468 for (seg = 0; seg < nr_segs; seg++) {
1469 ssize_t len = (ssize_t)iov[seg].iov_len;
1471 if (!access_ok(iov[seg].iov_base, len)) {
1478 if (len > MAX_RW_COUNT - total_len) {
1479 len = MAX_RW_COUNT - total_len;
1480 iov[seg].iov_len = len;
1485 iov_iter_init(i, type, iov, nr_segs, total_len);
1494 * import_iovec() - Copy an array of &struct iovec from userspace
1495 * into the kernel, check that it is valid, and initialize a new
1496 * &struct iov_iter iterator to access it.
1498 * @type: One of %READ or %WRITE.
1499 * @uvec: Pointer to the userspace array.
1500 * @nr_segs: Number of elements in userspace array.
1501 * @fast_segs: Number of elements in @iov.
1502 * @iovp: (input and output parameter) Pointer to pointer to (usually small
1503 * on-stack) kernel array.
1504 * @i: Pointer to iterator that will be initialized on success.
1506 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1507 * then this function places %NULL in *@iov on return. Otherwise, a new
1508 * array will be allocated and the result placed in *@iov. This means that
1509 * the caller may call kfree() on *@iov regardless of whether the small
1510 * on-stack array was used or not (and regardless of whether this function
1511 * returns an error or not).
1513 * Return: Negative error code on error, bytes imported on success
1515 ssize_t import_iovec(int type, const struct iovec __user *uvec,
1516 unsigned nr_segs, unsigned fast_segs,
1517 struct iovec **iovp, struct iov_iter *i)
1519 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1520 in_compat_syscall());
1522 EXPORT_SYMBOL(import_iovec);
1524 int import_single_range(int rw, void __user *buf, size_t len,
1525 struct iovec *iov, struct iov_iter *i)
1527 if (len > MAX_RW_COUNT)
1529 if (unlikely(!access_ok(buf, len)))
1532 iov_iter_ubuf(i, rw, buf, len);
1535 EXPORT_SYMBOL(import_single_range);
1537 int import_ubuf(int rw, void __user *buf, size_t len, struct iov_iter *i)
1539 if (len > MAX_RW_COUNT)
1541 if (unlikely(!access_ok(buf, len)))
1544 iov_iter_ubuf(i, rw, buf, len);
1547 EXPORT_SYMBOL_GPL(import_ubuf);
1550 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
1551 * iov_iter_save_state() was called.
1553 * @i: &struct iov_iter to restore
1554 * @state: state to restore from
1556 * Used after iov_iter_save_state() to bring restore @i, if operations may
1559 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
1561 void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
1563 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) &&
1564 !iter_is_ubuf(i)) && !iov_iter_is_kvec(i))
1566 i->iov_offset = state->iov_offset;
1567 i->count = state->count;
1568 if (iter_is_ubuf(i))
1571 * For the *vec iters, nr_segs + iov is constant - if we increment
1572 * the vec, then we also decrement the nr_segs count. Hence we don't
1573 * need to track both of these, just one is enough and we can deduct
1574 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
1575 * size, so we can just increment the iov pointer as they are unionzed.
1576 * ITER_BVEC _may_ be the same size on some archs, but on others it is
1577 * not. Be safe and handle it separately.
1579 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
1580 if (iov_iter_is_bvec(i))
1581 i->bvec -= state->nr_segs - i->nr_segs;
1583 i->__iov -= state->nr_segs - i->nr_segs;
1584 i->nr_segs = state->nr_segs;
1588 * Extract a list of contiguous pages from an ITER_XARRAY iterator. This does not
1589 * get references on the pages, nor does it get a pin on them.
1591 static ssize_t iov_iter_extract_xarray_pages(struct iov_iter *i,
1592 struct page ***pages, size_t maxsize,
1593 unsigned int maxpages,
1594 iov_iter_extraction_t extraction_flags,
1597 struct page *page, **p;
1598 unsigned int nr = 0, offset;
1599 loff_t pos = i->xarray_start + i->iov_offset;
1600 pgoff_t index = pos >> PAGE_SHIFT;
1601 XA_STATE(xas, i->xarray, index);
1603 offset = pos & ~PAGE_MASK;
1606 maxpages = want_pages_array(pages, maxsize, offset, maxpages);
1612 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1613 if (xas_retry(&xas, page))
1616 /* Has the page moved or been split? */
1617 if (unlikely(page != xas_reload(&xas))) {
1622 p[nr++] = find_subpage(page, xas.xa_index);
1628 maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1629 iov_iter_advance(i, maxsize);
1634 * Extract a list of contiguous pages from an ITER_BVEC iterator. This does
1635 * not get references on the pages, nor does it get a pin on them.
1637 static ssize_t iov_iter_extract_bvec_pages(struct iov_iter *i,
1638 struct page ***pages, size_t maxsize,
1639 unsigned int maxpages,
1640 iov_iter_extraction_t extraction_flags,
1643 struct page **p, *page;
1644 size_t skip = i->iov_offset, offset;
1648 if (i->nr_segs == 0)
1650 maxsize = min(maxsize, i->bvec->bv_len - skip);
1659 skip += i->bvec->bv_offset;
1660 page = i->bvec->bv_page + skip / PAGE_SIZE;
1661 offset = skip % PAGE_SIZE;
1664 maxpages = want_pages_array(pages, maxsize, offset, maxpages);
1668 for (k = 0; k < maxpages; k++)
1671 maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset);
1672 iov_iter_advance(i, maxsize);
1677 * Extract a list of virtually contiguous pages from an ITER_KVEC iterator.
1678 * This does not get references on the pages, nor does it get a pin on them.
1680 static ssize_t iov_iter_extract_kvec_pages(struct iov_iter *i,
1681 struct page ***pages, size_t maxsize,
1682 unsigned int maxpages,
1683 iov_iter_extraction_t extraction_flags,
1686 struct page **p, *page;
1688 size_t skip = i->iov_offset, offset, len;
1692 if (i->nr_segs == 0)
1694 maxsize = min(maxsize, i->kvec->iov_len - skip);
1703 kaddr = i->kvec->iov_base + skip;
1704 offset = (unsigned long)kaddr & ~PAGE_MASK;
1707 maxpages = want_pages_array(pages, maxsize, offset, maxpages);
1713 len = offset + maxsize;
1714 for (k = 0; k < maxpages; k++) {
1715 size_t seg = min_t(size_t, len, PAGE_SIZE);
1717 if (is_vmalloc_or_module_addr(kaddr))
1718 page = vmalloc_to_page(kaddr);
1720 page = virt_to_page(kaddr);
1727 maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset);
1728 iov_iter_advance(i, maxsize);
1733 * Extract a list of contiguous pages from a user iterator and get a pin on
1734 * each of them. This should only be used if the iterator is user-backed
1737 * It does not get refs on the pages, but the pages must be unpinned by the
1738 * caller once the transfer is complete.
1740 * This is safe to be used where background IO/DMA *is* going to be modifying
1741 * the buffer; using a pin rather than a ref makes forces fork() to give the
1742 * child a copy of the page.
1744 static ssize_t iov_iter_extract_user_pages(struct iov_iter *i,
1745 struct page ***pages,
1747 unsigned int maxpages,
1748 iov_iter_extraction_t extraction_flags,
1752 unsigned int gup_flags = 0;
1756 if (i->data_source == ITER_DEST)
1757 gup_flags |= FOLL_WRITE;
1758 if (extraction_flags & ITER_ALLOW_P2PDMA)
1759 gup_flags |= FOLL_PCI_P2PDMA;
1761 gup_flags |= FOLL_NOFAULT;
1763 addr = first_iovec_segment(i, &maxsize);
1764 *offset0 = offset = addr % PAGE_SIZE;
1766 maxpages = want_pages_array(pages, maxsize, offset, maxpages);
1769 res = pin_user_pages_fast(addr, maxpages, gup_flags, *pages);
1770 if (unlikely(res <= 0))
1772 maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - offset);
1773 iov_iter_advance(i, maxsize);
1778 * iov_iter_extract_pages - Extract a list of contiguous pages from an iterator
1779 * @i: The iterator to extract from
1780 * @pages: Where to return the list of pages
1781 * @maxsize: The maximum amount of iterator to extract
1782 * @maxpages: The maximum size of the list of pages
1783 * @extraction_flags: Flags to qualify request
1784 * @offset0: Where to return the starting offset into (*@pages)[0]
1786 * Extract a list of contiguous pages from the current point of the iterator,
1787 * advancing the iterator. The maximum number of pages and the maximum amount
1788 * of page contents can be set.
1790 * If *@pages is NULL, a page list will be allocated to the required size and
1791 * *@pages will be set to its base. If *@pages is not NULL, it will be assumed
1792 * that the caller allocated a page list at least @maxpages in size and this
1793 * will be filled in.
1795 * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA
1796 * be allowed on the pages extracted.
1798 * The iov_iter_extract_will_pin() function can be used to query how cleanup
1799 * should be performed.
1801 * Extra refs or pins on the pages may be obtained as follows:
1803 * (*) If the iterator is user-backed (ITER_IOVEC/ITER_UBUF), pins will be
1804 * added to the pages, but refs will not be taken.
1805 * iov_iter_extract_will_pin() will return true.
1807 * (*) If the iterator is ITER_KVEC, ITER_BVEC or ITER_XARRAY, the pages are
1808 * merely listed; no extra refs or pins are obtained.
1809 * iov_iter_extract_will_pin() will return 0.
1813 * (*) Use with ITER_DISCARD is not supported as that has no content.
1815 * On success, the function sets *@pages to the new pagelist, if allocated, and
1816 * sets *offset0 to the offset into the first page.
1818 * It may also return -ENOMEM and -EFAULT.
1820 ssize_t iov_iter_extract_pages(struct iov_iter *i,
1821 struct page ***pages,
1823 unsigned int maxpages,
1824 iov_iter_extraction_t extraction_flags,
1827 maxsize = min_t(size_t, min_t(size_t, maxsize, i->count), MAX_RW_COUNT);
1831 if (likely(user_backed_iter(i)))
1832 return iov_iter_extract_user_pages(i, pages, maxsize,
1833 maxpages, extraction_flags,
1835 if (iov_iter_is_kvec(i))
1836 return iov_iter_extract_kvec_pages(i, pages, maxsize,
1837 maxpages, extraction_flags,
1839 if (iov_iter_is_bvec(i))
1840 return iov_iter_extract_bvec_pages(i, pages, maxsize,
1841 maxpages, extraction_flags,
1843 if (iov_iter_is_xarray(i))
1844 return iov_iter_extract_xarray_pages(i, pages, maxsize,
1845 maxpages, extraction_flags,
1849 EXPORT_SYMBOL_GPL(iov_iter_extract_pages);