1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <crypto/hash.h>
3 #include <linux/export.h>
4 #include <linux/bvec.h>
6 #include <linux/pagemap.h>
7 #include <linux/slab.h>
8 #include <linux/vmalloc.h>
9 #include <linux/splice.h>
10 #include <net/checksum.h>
11 #include <linux/scatterlist.h>
12 #include <linux/instrumented.h>
14 #define PIPE_PARANOIA /* for now */
16 #define iterate_iovec(i, n, __v, __p, skip, STEP) { \
20 __v.iov_len = min(n, __p->iov_len - skip); \
21 if (likely(__v.iov_len)) { \
22 __v.iov_base = __p->iov_base + skip; \
24 __v.iov_len -= left; \
25 skip += __v.iov_len; \
30 while (unlikely(!left && n)) { \
32 __v.iov_len = min(n, __p->iov_len); \
33 if (unlikely(!__v.iov_len)) \
35 __v.iov_base = __p->iov_base; \
37 __v.iov_len -= left; \
44 #define iterate_kvec(i, n, __v, __p, skip, STEP) { \
47 __v.iov_len = min(n, __p->iov_len - skip); \
48 if (likely(__v.iov_len)) { \
49 __v.iov_base = __p->iov_base + skip; \
51 skip += __v.iov_len; \
54 while (unlikely(n)) { \
56 __v.iov_len = min(n, __p->iov_len); \
57 if (unlikely(!__v.iov_len)) \
59 __v.iov_base = __p->iov_base; \
67 #define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
68 struct bvec_iter __start; \
69 __start.bi_size = n; \
70 __start.bi_bvec_done = skip; \
72 for_each_bvec(__v, i->bvec, __bi, __start) { \
79 #define iterate_all_kinds(i, n, v, I, B, K) { \
81 size_t skip = i->iov_offset; \
82 if (unlikely(i->type & ITER_BVEC)) { \
84 struct bvec_iter __bi; \
85 iterate_bvec(i, n, v, __bi, skip, (B)) \
86 } else if (unlikely(i->type & ITER_KVEC)) { \
87 const struct kvec *kvec; \
89 iterate_kvec(i, n, v, kvec, skip, (K)) \
90 } else if (unlikely(i->type & ITER_DISCARD)) { \
92 const struct iovec *iov; \
94 iterate_iovec(i, n, v, iov, skip, (I)) \
99 #define iterate_and_advance(i, n, v, I, B, K) { \
100 if (unlikely(i->count < n)) \
103 size_t skip = i->iov_offset; \
104 if (unlikely(i->type & ITER_BVEC)) { \
105 const struct bio_vec *bvec = i->bvec; \
107 struct bvec_iter __bi; \
108 iterate_bvec(i, n, v, __bi, skip, (B)) \
109 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
110 i->nr_segs -= i->bvec - bvec; \
111 skip = __bi.bi_bvec_done; \
112 } else if (unlikely(i->type & ITER_KVEC)) { \
113 const struct kvec *kvec; \
115 iterate_kvec(i, n, v, kvec, skip, (K)) \
116 if (skip == kvec->iov_len) { \
120 i->nr_segs -= kvec - i->kvec; \
122 } else if (unlikely(i->type & ITER_DISCARD)) { \
125 const struct iovec *iov; \
127 iterate_iovec(i, n, v, iov, skip, (I)) \
128 if (skip == iov->iov_len) { \
132 i->nr_segs -= iov - i->iov; \
136 i->iov_offset = skip; \
140 static int copyout(void __user *to, const void *from, size_t n)
142 if (access_ok(to, n)) {
143 instrument_copy_to_user(to, from, n);
144 n = raw_copy_to_user(to, from, n);
149 static int copyin(void *to, const void __user *from, size_t n)
151 if (access_ok(from, n)) {
152 instrument_copy_from_user(to, from, n);
153 n = raw_copy_from_user(to, from, n);
158 static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
161 size_t skip, copy, left, wanted;
162 const struct iovec *iov;
166 if (unlikely(bytes > i->count))
169 if (unlikely(!bytes))
175 skip = i->iov_offset;
176 buf = iov->iov_base + skip;
177 copy = min(bytes, iov->iov_len - skip);
179 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
180 kaddr = kmap_atomic(page);
181 from = kaddr + offset;
183 /* first chunk, usually the only one */
184 left = copyout(buf, from, copy);
190 while (unlikely(!left && bytes)) {
193 copy = min(bytes, iov->iov_len);
194 left = copyout(buf, from, copy);
200 if (likely(!bytes)) {
201 kunmap_atomic(kaddr);
204 offset = from - kaddr;
206 kunmap_atomic(kaddr);
207 copy = min(bytes, iov->iov_len - skip);
209 /* Too bad - revert to non-atomic kmap */
212 from = kaddr + offset;
213 left = copyout(buf, from, copy);
218 while (unlikely(!left && bytes)) {
221 copy = min(bytes, iov->iov_len);
222 left = copyout(buf, from, copy);
231 if (skip == iov->iov_len) {
235 i->count -= wanted - bytes;
236 i->nr_segs -= iov - i->iov;
238 i->iov_offset = skip;
239 return wanted - bytes;
242 static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
245 size_t skip, copy, left, wanted;
246 const struct iovec *iov;
250 if (unlikely(bytes > i->count))
253 if (unlikely(!bytes))
259 skip = i->iov_offset;
260 buf = iov->iov_base + skip;
261 copy = min(bytes, iov->iov_len - skip);
263 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
264 kaddr = kmap_atomic(page);
267 /* first chunk, usually the only one */
268 left = copyin(to, buf, copy);
274 while (unlikely(!left && bytes)) {
277 copy = min(bytes, iov->iov_len);
278 left = copyin(to, buf, copy);
284 if (likely(!bytes)) {
285 kunmap_atomic(kaddr);
290 kunmap_atomic(kaddr);
291 copy = min(bytes, iov->iov_len - skip);
293 /* Too bad - revert to non-atomic kmap */
297 left = copyin(to, buf, copy);
302 while (unlikely(!left && bytes)) {
305 copy = min(bytes, iov->iov_len);
306 left = copyin(to, buf, copy);
315 if (skip == iov->iov_len) {
319 i->count -= wanted - bytes;
320 i->nr_segs -= iov - i->iov;
322 i->iov_offset = skip;
323 return wanted - bytes;
327 static bool sanity(const struct iov_iter *i)
329 struct pipe_inode_info *pipe = i->pipe;
330 unsigned int p_head = pipe->head;
331 unsigned int p_tail = pipe->tail;
332 unsigned int p_mask = pipe->ring_size - 1;
333 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
334 unsigned int i_head = i->head;
338 struct pipe_buffer *p;
339 if (unlikely(p_occupancy == 0))
340 goto Bad; // pipe must be non-empty
341 if (unlikely(i_head != p_head - 1))
342 goto Bad; // must be at the last buffer...
344 p = &pipe->bufs[i_head & p_mask];
345 if (unlikely(p->offset + p->len != i->iov_offset))
346 goto Bad; // ... at the end of segment
348 if (i_head != p_head)
349 goto Bad; // must be right after the last buffer
353 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
354 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
355 p_head, p_tail, pipe->ring_size);
356 for (idx = 0; idx < pipe->ring_size; idx++)
357 printk(KERN_ERR "[%p %p %d %d]\n",
359 pipe->bufs[idx].page,
360 pipe->bufs[idx].offset,
361 pipe->bufs[idx].len);
366 #define sanity(i) true
369 static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
372 struct pipe_inode_info *pipe = i->pipe;
373 struct pipe_buffer *buf;
374 unsigned int p_tail = pipe->tail;
375 unsigned int p_mask = pipe->ring_size - 1;
376 unsigned int i_head = i->head;
379 if (unlikely(bytes > i->count))
382 if (unlikely(!bytes))
389 buf = &pipe->bufs[i_head & p_mask];
391 if (offset == off && buf->page == page) {
392 /* merge with the last one */
394 i->iov_offset += bytes;
398 buf = &pipe->bufs[i_head & p_mask];
400 if (pipe_full(i_head, p_tail, pipe->max_usage))
403 buf->ops = &page_cache_pipe_buf_ops;
406 buf->offset = offset;
409 pipe->head = i_head + 1;
410 i->iov_offset = offset + bytes;
418 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
419 * bytes. For each iovec, fault in each page that constitutes the iovec.
421 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
422 * because it is an invalid address).
424 int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
426 size_t skip = i->iov_offset;
427 const struct iovec *iov;
431 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
432 iterate_iovec(i, bytes, v, iov, skip, ({
433 err = fault_in_pages_readable(v.iov_base, v.iov_len);
440 EXPORT_SYMBOL(iov_iter_fault_in_readable);
442 void iov_iter_init(struct iov_iter *i, unsigned int direction,
443 const struct iovec *iov, unsigned long nr_segs,
446 WARN_ON(direction & ~(READ | WRITE));
447 direction &= READ | WRITE;
449 /* It will get better. Eventually... */
450 if (uaccess_kernel()) {
451 i->type = ITER_KVEC | direction;
452 i->kvec = (struct kvec *)iov;
454 i->type = ITER_IOVEC | direction;
457 i->nr_segs = nr_segs;
461 EXPORT_SYMBOL(iov_iter_init);
463 static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
465 char *from = kmap_atomic(page);
466 memcpy(to, from + offset, len);
470 static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
472 char *to = kmap_atomic(page);
473 memcpy(to + offset, from, len);
477 static void memzero_page(struct page *page, size_t offset, size_t len)
479 char *addr = kmap_atomic(page);
480 memset(addr + offset, 0, len);
484 static inline bool allocated(struct pipe_buffer *buf)
486 return buf->ops == &default_pipe_buf_ops;
489 static inline void data_start(const struct iov_iter *i,
490 unsigned int *iter_headp, size_t *offp)
492 unsigned int p_mask = i->pipe->ring_size - 1;
493 unsigned int iter_head = i->head;
494 size_t off = i->iov_offset;
496 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
501 *iter_headp = iter_head;
505 static size_t push_pipe(struct iov_iter *i, size_t size,
506 int *iter_headp, size_t *offp)
508 struct pipe_inode_info *pipe = i->pipe;
509 unsigned int p_tail = pipe->tail;
510 unsigned int p_mask = pipe->ring_size - 1;
511 unsigned int iter_head;
515 if (unlikely(size > i->count))
521 data_start(i, &iter_head, &off);
522 *iter_headp = iter_head;
525 left -= PAGE_SIZE - off;
527 pipe->bufs[iter_head & p_mask].len += size;
530 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
533 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
534 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
535 struct page *page = alloc_page(GFP_USER);
539 buf->ops = &default_pipe_buf_ops;
542 buf->len = min_t(ssize_t, left, PAGE_SIZE);
545 pipe->head = iter_head;
553 static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
556 struct pipe_inode_info *pipe = i->pipe;
557 unsigned int p_mask = pipe->ring_size - 1;
564 bytes = n = push_pipe(i, bytes, &i_head, &off);
568 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
569 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
571 i->iov_offset = off + chunk;
581 static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
582 __wsum sum, size_t off)
584 __wsum next = csum_partial_copy_nocheck(from, to, len, 0);
585 return csum_block_add(sum, next, off);
588 static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
589 __wsum *csum, struct iov_iter *i)
591 struct pipe_inode_info *pipe = i->pipe;
592 unsigned int p_mask = pipe->ring_size - 1;
601 bytes = n = push_pipe(i, bytes, &i_head, &r);
605 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
606 char *p = kmap_atomic(pipe->bufs[i_head & p_mask].page);
607 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
610 i->iov_offset = r + chunk;
622 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
624 const char *from = addr;
625 if (unlikely(iov_iter_is_pipe(i)))
626 return copy_pipe_to_iter(addr, bytes, i);
627 if (iter_is_iovec(i))
629 iterate_and_advance(i, bytes, v,
630 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
631 memcpy_to_page(v.bv_page, v.bv_offset,
632 (from += v.bv_len) - v.bv_len, v.bv_len),
633 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
638 EXPORT_SYMBOL(_copy_to_iter);
640 #ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
641 static int copyout_mcsafe(void __user *to, const void *from, size_t n)
643 if (access_ok(to, n)) {
644 instrument_copy_to_user(to, from, n);
645 n = copy_to_user_mcsafe((__force void *) to, from, n);
650 static unsigned long memcpy_mcsafe_to_page(struct page *page, size_t offset,
651 const char *from, size_t len)
656 to = kmap_atomic(page);
657 ret = memcpy_mcsafe(to + offset, from, len);
663 static size_t copy_pipe_to_iter_mcsafe(const void *addr, size_t bytes,
666 struct pipe_inode_info *pipe = i->pipe;
667 unsigned int p_mask = pipe->ring_size - 1;
669 size_t n, off, xfer = 0;
674 bytes = n = push_pipe(i, bytes, &i_head, &off);
678 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
681 rem = memcpy_mcsafe_to_page(pipe->bufs[i_head & p_mask].page,
684 i->iov_offset = off + chunk - rem;
698 * _copy_to_iter_mcsafe - copy to user with source-read error exception handling
699 * @addr: source kernel address
700 * @bytes: total transfer length
701 * @iter: destination iterator
703 * The pmem driver arranges for filesystem-dax to use this facility via
704 * dax_copy_to_iter() for protecting read/write to persistent memory.
705 * Unless / until an architecture can guarantee identical performance
706 * between _copy_to_iter_mcsafe() and _copy_to_iter() it would be a
707 * performance regression to switch more users to the mcsafe version.
709 * Otherwise, the main differences between this and typical _copy_to_iter().
711 * * Typical tail/residue handling after a fault retries the copy
712 * byte-by-byte until the fault happens again. Re-triggering machine
713 * checks is potentially fatal so the implementation uses source
714 * alignment and poison alignment assumptions to avoid re-triggering
715 * hardware exceptions.
717 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
718 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
721 * See MCSAFE_TEST for self-test.
723 size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i)
725 const char *from = addr;
726 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
728 if (unlikely(iov_iter_is_pipe(i)))
729 return copy_pipe_to_iter_mcsafe(addr, bytes, i);
730 if (iter_is_iovec(i))
732 iterate_and_advance(i, bytes, v,
733 copyout_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
735 rem = memcpy_mcsafe_to_page(v.bv_page, v.bv_offset,
736 (from += v.bv_len) - v.bv_len, v.bv_len);
738 curr_addr = (unsigned long) from;
739 bytes = curr_addr - s_addr - rem;
744 rem = memcpy_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len,
747 curr_addr = (unsigned long) from;
748 bytes = curr_addr - s_addr - rem;
756 EXPORT_SYMBOL_GPL(_copy_to_iter_mcsafe);
757 #endif /* CONFIG_ARCH_HAS_UACCESS_MCSAFE */
759 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
762 if (unlikely(iov_iter_is_pipe(i))) {
766 if (iter_is_iovec(i))
768 iterate_and_advance(i, bytes, v,
769 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
770 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
771 v.bv_offset, v.bv_len),
772 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
777 EXPORT_SYMBOL(_copy_from_iter);
779 bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
782 if (unlikely(iov_iter_is_pipe(i))) {
786 if (unlikely(i->count < bytes))
789 if (iter_is_iovec(i))
791 iterate_all_kinds(i, bytes, v, ({
792 if (copyin((to += v.iov_len) - v.iov_len,
793 v.iov_base, v.iov_len))
796 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
797 v.bv_offset, v.bv_len),
798 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
801 iov_iter_advance(i, bytes);
804 EXPORT_SYMBOL(_copy_from_iter_full);
806 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
809 if (unlikely(iov_iter_is_pipe(i))) {
813 iterate_and_advance(i, bytes, v,
814 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
815 v.iov_base, v.iov_len),
816 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
817 v.bv_offset, v.bv_len),
818 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
823 EXPORT_SYMBOL(_copy_from_iter_nocache);
825 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
827 * _copy_from_iter_flushcache - write destination through cpu cache
828 * @addr: destination kernel address
829 * @bytes: total transfer length
830 * @iter: source iterator
832 * The pmem driver arranges for filesystem-dax to use this facility via
833 * dax_copy_from_iter() for ensuring that writes to persistent memory
834 * are flushed through the CPU cache. It is differentiated from
835 * _copy_from_iter_nocache() in that guarantees all data is flushed for
836 * all iterator types. The _copy_from_iter_nocache() only attempts to
837 * bypass the cache for the ITER_IOVEC case, and on some archs may use
838 * instructions that strand dirty-data in the cache.
840 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
843 if (unlikely(iov_iter_is_pipe(i))) {
847 iterate_and_advance(i, bytes, v,
848 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
849 v.iov_base, v.iov_len),
850 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
851 v.bv_offset, v.bv_len),
852 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
858 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
861 bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
864 if (unlikely(iov_iter_is_pipe(i))) {
868 if (unlikely(i->count < bytes))
870 iterate_all_kinds(i, bytes, v, ({
871 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
872 v.iov_base, v.iov_len))
875 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
876 v.bv_offset, v.bv_len),
877 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
880 iov_iter_advance(i, bytes);
883 EXPORT_SYMBOL(_copy_from_iter_full_nocache);
885 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
888 size_t v = n + offset;
891 * The general case needs to access the page order in order
892 * to compute the page size.
893 * However, we mostly deal with order-0 pages and thus can
894 * avoid a possible cache line miss for requests that fit all
897 if (n <= v && v <= PAGE_SIZE)
900 head = compound_head(page);
901 v += (page - head) << PAGE_SHIFT;
903 if (likely(n <= v && v <= (page_size(head))))
909 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
912 if (unlikely(!page_copy_sane(page, offset, bytes)))
914 if (i->type & (ITER_BVEC|ITER_KVEC)) {
915 void *kaddr = kmap_atomic(page);
916 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
917 kunmap_atomic(kaddr);
919 } else if (unlikely(iov_iter_is_discard(i)))
921 else if (likely(!iov_iter_is_pipe(i)))
922 return copy_page_to_iter_iovec(page, offset, bytes, i);
924 return copy_page_to_iter_pipe(page, offset, bytes, i);
926 EXPORT_SYMBOL(copy_page_to_iter);
928 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
931 if (unlikely(!page_copy_sane(page, offset, bytes)))
933 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
937 if (i->type & (ITER_BVEC|ITER_KVEC)) {
938 void *kaddr = kmap_atomic(page);
939 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
940 kunmap_atomic(kaddr);
943 return copy_page_from_iter_iovec(page, offset, bytes, i);
945 EXPORT_SYMBOL(copy_page_from_iter);
947 static size_t pipe_zero(size_t bytes, struct iov_iter *i)
949 struct pipe_inode_info *pipe = i->pipe;
950 unsigned int p_mask = pipe->ring_size - 1;
957 bytes = n = push_pipe(i, bytes, &i_head, &off);
962 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
963 memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk);
965 i->iov_offset = off + chunk;
974 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
976 if (unlikely(iov_iter_is_pipe(i)))
977 return pipe_zero(bytes, i);
978 iterate_and_advance(i, bytes, v,
979 clear_user(v.iov_base, v.iov_len),
980 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
981 memset(v.iov_base, 0, v.iov_len)
986 EXPORT_SYMBOL(iov_iter_zero);
988 size_t iov_iter_copy_from_user_atomic(struct page *page,
989 struct iov_iter *i, unsigned long offset, size_t bytes)
991 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
992 if (unlikely(!page_copy_sane(page, offset, bytes))) {
993 kunmap_atomic(kaddr);
996 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
997 kunmap_atomic(kaddr);
1001 iterate_all_kinds(i, bytes, v,
1002 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
1003 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
1004 v.bv_offset, v.bv_len),
1005 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
1007 kunmap_atomic(kaddr);
1010 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
1012 static inline void pipe_truncate(struct iov_iter *i)
1014 struct pipe_inode_info *pipe = i->pipe;
1015 unsigned int p_tail = pipe->tail;
1016 unsigned int p_head = pipe->head;
1017 unsigned int p_mask = pipe->ring_size - 1;
1019 if (!pipe_empty(p_head, p_tail)) {
1020 struct pipe_buffer *buf;
1021 unsigned int i_head = i->head;
1022 size_t off = i->iov_offset;
1025 buf = &pipe->bufs[i_head & p_mask];
1026 buf->len = off - buf->offset;
1029 while (p_head != i_head) {
1031 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
1034 pipe->head = p_head;
1038 static void pipe_advance(struct iov_iter *i, size_t size)
1040 struct pipe_inode_info *pipe = i->pipe;
1041 if (unlikely(i->count < size))
1044 struct pipe_buffer *buf;
1045 unsigned int p_mask = pipe->ring_size - 1;
1046 unsigned int i_head = i->head;
1047 size_t off = i->iov_offset, left = size;
1049 if (off) /* make it relative to the beginning of buffer */
1050 left += off - pipe->bufs[i_head & p_mask].offset;
1052 buf = &pipe->bufs[i_head & p_mask];
1053 if (left <= buf->len)
1059 i->iov_offset = buf->offset + left;
1062 /* ... and discard everything past that point */
1066 void iov_iter_advance(struct iov_iter *i, size_t size)
1068 if (unlikely(iov_iter_is_pipe(i))) {
1069 pipe_advance(i, size);
1072 if (unlikely(iov_iter_is_discard(i))) {
1076 iterate_and_advance(i, size, v, 0, 0, 0)
1078 EXPORT_SYMBOL(iov_iter_advance);
1080 void iov_iter_revert(struct iov_iter *i, size_t unroll)
1084 if (WARN_ON(unroll > MAX_RW_COUNT))
1087 if (unlikely(iov_iter_is_pipe(i))) {
1088 struct pipe_inode_info *pipe = i->pipe;
1089 unsigned int p_mask = pipe->ring_size - 1;
1090 unsigned int i_head = i->head;
1091 size_t off = i->iov_offset;
1093 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1094 size_t n = off - b->offset;
1100 if (!unroll && i_head == i->start_head) {
1105 b = &pipe->bufs[i_head & p_mask];
1106 off = b->offset + b->len;
1108 i->iov_offset = off;
1113 if (unlikely(iov_iter_is_discard(i)))
1115 if (unroll <= i->iov_offset) {
1116 i->iov_offset -= unroll;
1119 unroll -= i->iov_offset;
1120 if (iov_iter_is_bvec(i)) {
1121 const struct bio_vec *bvec = i->bvec;
1123 size_t n = (--bvec)->bv_len;
1127 i->iov_offset = n - unroll;
1132 } else { /* same logics for iovec and kvec */
1133 const struct iovec *iov = i->iov;
1135 size_t n = (--iov)->iov_len;
1139 i->iov_offset = n - unroll;
1146 EXPORT_SYMBOL(iov_iter_revert);
1149 * Return the count of just the current iov_iter segment.
1151 size_t iov_iter_single_seg_count(const struct iov_iter *i)
1153 if (unlikely(iov_iter_is_pipe(i)))
1154 return i->count; // it is a silly place, anyway
1155 if (i->nr_segs == 1)
1157 if (unlikely(iov_iter_is_discard(i)))
1159 else if (iov_iter_is_bvec(i))
1160 return min(i->count, i->bvec->bv_len - i->iov_offset);
1162 return min(i->count, i->iov->iov_len - i->iov_offset);
1164 EXPORT_SYMBOL(iov_iter_single_seg_count);
1166 void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
1167 const struct kvec *kvec, unsigned long nr_segs,
1170 WARN_ON(direction & ~(READ | WRITE));
1171 i->type = ITER_KVEC | (direction & (READ | WRITE));
1173 i->nr_segs = nr_segs;
1177 EXPORT_SYMBOL(iov_iter_kvec);
1179 void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1180 const struct bio_vec *bvec, unsigned long nr_segs,
1183 WARN_ON(direction & ~(READ | WRITE));
1184 i->type = ITER_BVEC | (direction & (READ | WRITE));
1186 i->nr_segs = nr_segs;
1190 EXPORT_SYMBOL(iov_iter_bvec);
1192 void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1193 struct pipe_inode_info *pipe,
1196 BUG_ON(direction != READ);
1197 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
1198 i->type = ITER_PIPE | READ;
1200 i->head = pipe->head;
1203 i->start_head = i->head;
1205 EXPORT_SYMBOL(iov_iter_pipe);
1208 * iov_iter_discard - Initialise an I/O iterator that discards data
1209 * @i: The iterator to initialise.
1210 * @direction: The direction of the transfer.
1211 * @count: The size of the I/O buffer in bytes.
1213 * Set up an I/O iterator that just discards everything that's written to it.
1214 * It's only available as a READ iterator.
1216 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1218 BUG_ON(direction != READ);
1219 i->type = ITER_DISCARD | READ;
1223 EXPORT_SYMBOL(iov_iter_discard);
1225 unsigned long iov_iter_alignment(const struct iov_iter *i)
1227 unsigned long res = 0;
1228 size_t size = i->count;
1230 if (unlikely(iov_iter_is_pipe(i))) {
1231 unsigned int p_mask = i->pipe->ring_size - 1;
1233 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
1234 return size | i->iov_offset;
1237 iterate_all_kinds(i, size, v,
1238 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
1239 res |= v.bv_offset | v.bv_len,
1240 res |= (unsigned long)v.iov_base | v.iov_len
1244 EXPORT_SYMBOL(iov_iter_alignment);
1246 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1248 unsigned long res = 0;
1249 size_t size = i->count;
1251 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1256 iterate_all_kinds(i, size, v,
1257 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1258 (size != v.iov_len ? size : 0), 0),
1259 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1260 (size != v.bv_len ? size : 0)),
1261 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1262 (size != v.iov_len ? size : 0))
1266 EXPORT_SYMBOL(iov_iter_gap_alignment);
1268 static inline ssize_t __pipe_get_pages(struct iov_iter *i,
1270 struct page **pages,
1274 struct pipe_inode_info *pipe = i->pipe;
1275 unsigned int p_mask = pipe->ring_size - 1;
1276 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
1283 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1291 static ssize_t pipe_get_pages(struct iov_iter *i,
1292 struct page **pages, size_t maxsize, unsigned maxpages,
1295 unsigned int iter_head, npages;
1304 data_start(i, &iter_head, start);
1305 /* Amount of free space: some of this one + all after this one */
1306 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1307 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
1309 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
1312 ssize_t iov_iter_get_pages(struct iov_iter *i,
1313 struct page **pages, size_t maxsize, unsigned maxpages,
1316 if (maxsize > i->count)
1319 if (unlikely(iov_iter_is_pipe(i)))
1320 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1321 if (unlikely(iov_iter_is_discard(i)))
1324 iterate_all_kinds(i, maxsize, v, ({
1325 unsigned long addr = (unsigned long)v.iov_base;
1326 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1330 if (len > maxpages * PAGE_SIZE)
1331 len = maxpages * PAGE_SIZE;
1332 addr &= ~(PAGE_SIZE - 1);
1333 n = DIV_ROUND_UP(len, PAGE_SIZE);
1334 res = get_user_pages_fast(addr, n,
1335 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1337 if (unlikely(res < 0))
1339 return (res == n ? len : res * PAGE_SIZE) - *start;
1341 /* can't be more than PAGE_SIZE */
1342 *start = v.bv_offset;
1343 get_page(*pages = v.bv_page);
1351 EXPORT_SYMBOL(iov_iter_get_pages);
1353 static struct page **get_pages_array(size_t n)
1355 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
1358 static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1359 struct page ***pages, size_t maxsize,
1363 unsigned int iter_head, npages;
1372 data_start(i, &iter_head, start);
1373 /* Amount of free space: some of this one + all after this one */
1374 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1375 n = npages * PAGE_SIZE - *start;
1379 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1380 p = get_pages_array(npages);
1383 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
1391 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1392 struct page ***pages, size_t maxsize,
1397 if (maxsize > i->count)
1400 if (unlikely(iov_iter_is_pipe(i)))
1401 return pipe_get_pages_alloc(i, pages, maxsize, start);
1402 if (unlikely(iov_iter_is_discard(i)))
1405 iterate_all_kinds(i, maxsize, v, ({
1406 unsigned long addr = (unsigned long)v.iov_base;
1407 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1411 addr &= ~(PAGE_SIZE - 1);
1412 n = DIV_ROUND_UP(len, PAGE_SIZE);
1413 p = get_pages_array(n);
1416 res = get_user_pages_fast(addr, n,
1417 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
1418 if (unlikely(res < 0)) {
1423 return (res == n ? len : res * PAGE_SIZE) - *start;
1425 /* can't be more than PAGE_SIZE */
1426 *start = v.bv_offset;
1427 *pages = p = get_pages_array(1);
1430 get_page(*p = v.bv_page);
1438 EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1440 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1447 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1451 iterate_and_advance(i, bytes, v, ({
1453 next = csum_and_copy_from_user(v.iov_base,
1454 (to += v.iov_len) - v.iov_len,
1455 v.iov_len, 0, &err);
1457 sum = csum_block_add(sum, next, off);
1460 err ? v.iov_len : 0;
1462 char *p = kmap_atomic(v.bv_page);
1463 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1464 p + v.bv_offset, v.bv_len,
1469 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1470 v.iov_base, v.iov_len,
1478 EXPORT_SYMBOL(csum_and_copy_from_iter);
1480 bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1487 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1491 if (unlikely(i->count < bytes))
1493 iterate_all_kinds(i, bytes, v, ({
1495 next = csum_and_copy_from_user(v.iov_base,
1496 (to += v.iov_len) - v.iov_len,
1497 v.iov_len, 0, &err);
1500 sum = csum_block_add(sum, next, off);
1504 char *p = kmap_atomic(v.bv_page);
1505 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1506 p + v.bv_offset, v.bv_len,
1511 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1512 v.iov_base, v.iov_len,
1518 iov_iter_advance(i, bytes);
1521 EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1523 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csump,
1526 const char *from = addr;
1527 __wsum *csum = csump;
1531 if (unlikely(iov_iter_is_pipe(i)))
1532 return csum_and_copy_to_pipe_iter(addr, bytes, csum, i);
1535 if (unlikely(iov_iter_is_discard(i))) {
1536 WARN_ON(1); /* for now */
1539 iterate_and_advance(i, bytes, v, ({
1541 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
1543 v.iov_len, 0, &err);
1545 sum = csum_block_add(sum, next, off);
1548 err ? v.iov_len : 0;
1550 char *p = kmap_atomic(v.bv_page);
1551 sum = csum_and_memcpy(p + v.bv_offset,
1552 (from += v.bv_len) - v.bv_len,
1553 v.bv_len, sum, off);
1557 sum = csum_and_memcpy(v.iov_base,
1558 (from += v.iov_len) - v.iov_len,
1559 v.iov_len, sum, off);
1566 EXPORT_SYMBOL(csum_and_copy_to_iter);
1568 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1571 #ifdef CONFIG_CRYPTO_HASH
1572 struct ahash_request *hash = hashp;
1573 struct scatterlist sg;
1576 copied = copy_to_iter(addr, bytes, i);
1577 sg_init_one(&sg, addr, copied);
1578 ahash_request_set_crypt(hash, &sg, NULL, copied);
1579 crypto_ahash_update(hash);
1585 EXPORT_SYMBOL(hash_and_copy_to_iter);
1587 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1589 size_t size = i->count;
1594 if (unlikely(iov_iter_is_discard(i)))
1597 if (unlikely(iov_iter_is_pipe(i))) {
1598 struct pipe_inode_info *pipe = i->pipe;
1599 unsigned int iter_head;
1605 data_start(i, &iter_head, &off);
1606 /* some of this one + all after this one */
1607 npages = pipe_space_for_user(iter_head, pipe->tail, pipe);
1608 if (npages >= maxpages)
1610 } else iterate_all_kinds(i, size, v, ({
1611 unsigned long p = (unsigned long)v.iov_base;
1612 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1614 if (npages >= maxpages)
1618 if (npages >= maxpages)
1621 unsigned long p = (unsigned long)v.iov_base;
1622 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1624 if (npages >= maxpages)
1630 EXPORT_SYMBOL(iov_iter_npages);
1632 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1635 if (unlikely(iov_iter_is_pipe(new))) {
1639 if (unlikely(iov_iter_is_discard(new)))
1641 if (iov_iter_is_bvec(new))
1642 return new->bvec = kmemdup(new->bvec,
1643 new->nr_segs * sizeof(struct bio_vec),
1646 /* iovec and kvec have identical layout */
1647 return new->iov = kmemdup(new->iov,
1648 new->nr_segs * sizeof(struct iovec),
1651 EXPORT_SYMBOL(dup_iter);
1654 * import_iovec() - Copy an array of &struct iovec from userspace
1655 * into the kernel, check that it is valid, and initialize a new
1656 * &struct iov_iter iterator to access it.
1658 * @type: One of %READ or %WRITE.
1659 * @uvector: Pointer to the userspace array.
1660 * @nr_segs: Number of elements in userspace array.
1661 * @fast_segs: Number of elements in @iov.
1662 * @iov: (input and output parameter) Pointer to pointer to (usually small
1663 * on-stack) kernel array.
1664 * @i: Pointer to iterator that will be initialized on success.
1666 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1667 * then this function places %NULL in *@iov on return. Otherwise, a new
1668 * array will be allocated and the result placed in *@iov. This means that
1669 * the caller may call kfree() on *@iov regardless of whether the small
1670 * on-stack array was used or not (and regardless of whether this function
1671 * returns an error or not).
1673 * Return: Negative error code on error, bytes imported on success
1675 ssize_t import_iovec(int type, const struct iovec __user * uvector,
1676 unsigned nr_segs, unsigned fast_segs,
1677 struct iovec **iov, struct iov_iter *i)
1681 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1689 iov_iter_init(i, type, p, nr_segs, n);
1690 *iov = p == *iov ? NULL : p;
1693 EXPORT_SYMBOL(import_iovec);
1695 #ifdef CONFIG_COMPAT
1696 #include <linux/compat.h>
1698 ssize_t compat_import_iovec(int type,
1699 const struct compat_iovec __user * uvector,
1700 unsigned nr_segs, unsigned fast_segs,
1701 struct iovec **iov, struct iov_iter *i)
1705 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1713 iov_iter_init(i, type, p, nr_segs, n);
1714 *iov = p == *iov ? NULL : p;
1717 EXPORT_SYMBOL(compat_import_iovec);
1720 int import_single_range(int rw, void __user *buf, size_t len,
1721 struct iovec *iov, struct iov_iter *i)
1723 if (len > MAX_RW_COUNT)
1725 if (unlikely(!access_ok(buf, len)))
1728 iov->iov_base = buf;
1730 iov_iter_init(i, rw, iov, 1, len);
1733 EXPORT_SYMBOL(import_single_range);
1735 int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1736 int (*f)(struct kvec *vec, void *context),
1744 iterate_all_kinds(i, bytes, v, -EINVAL, ({
1745 w.iov_base = kmap(v.bv_page) + v.bv_offset;
1746 w.iov_len = v.bv_len;
1747 err = f(&w, context);
1751 err = f(&w, context);})
1755 EXPORT_SYMBOL(iov_iter_for_each_range);