1 #include <linux/export.h>
2 #include <linux/bvec.h>
4 #include <linux/pagemap.h>
5 #include <linux/slab.h>
6 #include <linux/vmalloc.h>
7 #include <linux/splice.h>
8 #include <net/checksum.h>
9 #include <linux/scatterlist.h>
11 #define PIPE_PARANOIA /* for now */
13 #define iterate_iovec(i, n, __v, __p, skip, STEP) { \
17 __v.iov_len = min(n, __p->iov_len - skip); \
18 if (likely(__v.iov_len)) { \
19 __v.iov_base = __p->iov_base + skip; \
21 __v.iov_len -= left; \
22 skip += __v.iov_len; \
27 while (unlikely(!left && n)) { \
29 __v.iov_len = min(n, __p->iov_len); \
30 if (unlikely(!__v.iov_len)) \
32 __v.iov_base = __p->iov_base; \
34 __v.iov_len -= left; \
41 #define iterate_kvec(i, n, __v, __p, skip, STEP) { \
44 __v.iov_len = min(n, __p->iov_len - skip); \
45 if (likely(__v.iov_len)) { \
46 __v.iov_base = __p->iov_base + skip; \
48 skip += __v.iov_len; \
51 while (unlikely(n)) { \
53 __v.iov_len = min(n, __p->iov_len); \
54 if (unlikely(!__v.iov_len)) \
56 __v.iov_base = __p->iov_base; \
64 #define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
65 struct bvec_iter __start; \
66 __start.bi_size = n; \
67 __start.bi_bvec_done = skip; \
69 for_each_bvec(__v, i->bvec, __bi, __start) { \
76 #define iterate_all_kinds(i, n, v, I, B, K) { \
78 size_t skip = i->iov_offset; \
79 if (unlikely(i->type & ITER_BVEC)) { \
81 struct bvec_iter __bi; \
82 iterate_bvec(i, n, v, __bi, skip, (B)) \
83 } else if (unlikely(i->type & ITER_KVEC)) { \
84 const struct kvec *kvec; \
86 iterate_kvec(i, n, v, kvec, skip, (K)) \
87 } else if (unlikely(i->type & ITER_DISCARD)) { \
89 const struct iovec *iov; \
91 iterate_iovec(i, n, v, iov, skip, (I)) \
96 #define iterate_and_advance(i, n, v, I, B, K) { \
97 if (unlikely(i->count < n)) \
100 size_t skip = i->iov_offset; \
101 if (unlikely(i->type & ITER_BVEC)) { \
102 const struct bio_vec *bvec = i->bvec; \
104 struct bvec_iter __bi; \
105 iterate_bvec(i, n, v, __bi, skip, (B)) \
106 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
107 i->nr_segs -= i->bvec - bvec; \
108 skip = __bi.bi_bvec_done; \
109 } else if (unlikely(i->type & ITER_KVEC)) { \
110 const struct kvec *kvec; \
112 iterate_kvec(i, n, v, kvec, skip, (K)) \
113 if (skip == kvec->iov_len) { \
117 i->nr_segs -= kvec - i->kvec; \
119 } else if (unlikely(i->type & ITER_DISCARD)) { \
122 const struct iovec *iov; \
124 iterate_iovec(i, n, v, iov, skip, (I)) \
125 if (skip == iov->iov_len) { \
129 i->nr_segs -= iov - i->iov; \
133 i->iov_offset = skip; \
137 static int copyout(void __user *to, const void *from, size_t n)
139 if (access_ok(VERIFY_WRITE, to, n)) {
140 kasan_check_read(from, n);
141 n = raw_copy_to_user(to, from, n);
146 static int copyin(void *to, const void __user *from, size_t n)
148 if (access_ok(VERIFY_READ, from, n)) {
149 kasan_check_write(to, n);
150 n = raw_copy_from_user(to, from, n);
155 static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
158 size_t skip, copy, left, wanted;
159 const struct iovec *iov;
163 if (unlikely(bytes > i->count))
166 if (unlikely(!bytes))
172 skip = i->iov_offset;
173 buf = iov->iov_base + skip;
174 copy = min(bytes, iov->iov_len - skip);
176 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
177 kaddr = kmap_atomic(page);
178 from = kaddr + offset;
180 /* first chunk, usually the only one */
181 left = copyout(buf, from, copy);
187 while (unlikely(!left && bytes)) {
190 copy = min(bytes, iov->iov_len);
191 left = copyout(buf, from, copy);
197 if (likely(!bytes)) {
198 kunmap_atomic(kaddr);
201 offset = from - kaddr;
203 kunmap_atomic(kaddr);
204 copy = min(bytes, iov->iov_len - skip);
206 /* Too bad - revert to non-atomic kmap */
209 from = kaddr + offset;
210 left = copyout(buf, from, copy);
215 while (unlikely(!left && bytes)) {
218 copy = min(bytes, iov->iov_len);
219 left = copyout(buf, from, copy);
228 if (skip == iov->iov_len) {
232 i->count -= wanted - bytes;
233 i->nr_segs -= iov - i->iov;
235 i->iov_offset = skip;
236 return wanted - bytes;
239 static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
242 size_t skip, copy, left, wanted;
243 const struct iovec *iov;
247 if (unlikely(bytes > i->count))
250 if (unlikely(!bytes))
256 skip = i->iov_offset;
257 buf = iov->iov_base + skip;
258 copy = min(bytes, iov->iov_len - skip);
260 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
261 kaddr = kmap_atomic(page);
264 /* first chunk, usually the only one */
265 left = copyin(to, buf, copy);
271 while (unlikely(!left && bytes)) {
274 copy = min(bytes, iov->iov_len);
275 left = copyin(to, buf, copy);
281 if (likely(!bytes)) {
282 kunmap_atomic(kaddr);
287 kunmap_atomic(kaddr);
288 copy = min(bytes, iov->iov_len - skip);
290 /* Too bad - revert to non-atomic kmap */
294 left = copyin(to, buf, copy);
299 while (unlikely(!left && bytes)) {
302 copy = min(bytes, iov->iov_len);
303 left = copyin(to, buf, copy);
312 if (skip == iov->iov_len) {
316 i->count -= wanted - bytes;
317 i->nr_segs -= iov - i->iov;
319 i->iov_offset = skip;
320 return wanted - bytes;
324 static bool sanity(const struct iov_iter *i)
326 struct pipe_inode_info *pipe = i->pipe;
328 int next = pipe->curbuf + pipe->nrbufs;
330 struct pipe_buffer *p;
331 if (unlikely(!pipe->nrbufs))
332 goto Bad; // pipe must be non-empty
333 if (unlikely(idx != ((next - 1) & (pipe->buffers - 1))))
334 goto Bad; // must be at the last buffer...
336 p = &pipe->bufs[idx];
337 if (unlikely(p->offset + p->len != i->iov_offset))
338 goto Bad; // ... at the end of segment
340 if (idx != (next & (pipe->buffers - 1)))
341 goto Bad; // must be right after the last buffer
345 printk(KERN_ERR "idx = %d, offset = %zd\n", i->idx, i->iov_offset);
346 printk(KERN_ERR "curbuf = %d, nrbufs = %d, buffers = %d\n",
347 pipe->curbuf, pipe->nrbufs, pipe->buffers);
348 for (idx = 0; idx < pipe->buffers; idx++)
349 printk(KERN_ERR "[%p %p %d %d]\n",
351 pipe->bufs[idx].page,
352 pipe->bufs[idx].offset,
353 pipe->bufs[idx].len);
358 #define sanity(i) true
361 static inline int next_idx(int idx, struct pipe_inode_info *pipe)
363 return (idx + 1) & (pipe->buffers - 1);
366 static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
369 struct pipe_inode_info *pipe = i->pipe;
370 struct pipe_buffer *buf;
374 if (unlikely(bytes > i->count))
377 if (unlikely(!bytes))
385 buf = &pipe->bufs[idx];
387 if (offset == off && buf->page == page) {
388 /* merge with the last one */
390 i->iov_offset += bytes;
393 idx = next_idx(idx, pipe);
394 buf = &pipe->bufs[idx];
396 if (idx == pipe->curbuf && pipe->nrbufs)
399 buf->ops = &page_cache_pipe_buf_ops;
400 get_page(buf->page = page);
401 buf->offset = offset;
403 i->iov_offset = offset + bytes;
411 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
412 * bytes. For each iovec, fault in each page that constitutes the iovec.
414 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
415 * because it is an invalid address).
417 int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
419 size_t skip = i->iov_offset;
420 const struct iovec *iov;
424 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
425 iterate_iovec(i, bytes, v, iov, skip, ({
426 err = fault_in_pages_readable(v.iov_base, v.iov_len);
433 EXPORT_SYMBOL(iov_iter_fault_in_readable);
435 void iov_iter_init(struct iov_iter *i, unsigned int direction,
436 const struct iovec *iov, unsigned long nr_segs,
439 WARN_ON(direction & ~(READ | WRITE));
440 direction &= READ | WRITE;
442 /* It will get better. Eventually... */
443 if (uaccess_kernel()) {
444 i->type = ITER_KVEC | direction;
445 i->kvec = (struct kvec *)iov;
447 i->type = ITER_IOVEC | direction;
450 i->nr_segs = nr_segs;
454 EXPORT_SYMBOL(iov_iter_init);
456 static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
458 char *from = kmap_atomic(page);
459 memcpy(to, from + offset, len);
463 static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
465 char *to = kmap_atomic(page);
466 memcpy(to + offset, from, len);
470 static void memzero_page(struct page *page, size_t offset, size_t len)
472 char *addr = kmap_atomic(page);
473 memset(addr + offset, 0, len);
477 static inline bool allocated(struct pipe_buffer *buf)
479 return buf->ops == &default_pipe_buf_ops;
482 static inline void data_start(const struct iov_iter *i, int *idxp, size_t *offp)
484 size_t off = i->iov_offset;
486 if (off && (!allocated(&i->pipe->bufs[idx]) || off == PAGE_SIZE)) {
487 idx = next_idx(idx, i->pipe);
494 static size_t push_pipe(struct iov_iter *i, size_t size,
495 int *idxp, size_t *offp)
497 struct pipe_inode_info *pipe = i->pipe;
502 if (unlikely(size > i->count))
508 data_start(i, &idx, &off);
512 left -= PAGE_SIZE - off;
514 pipe->bufs[idx].len += size;
517 pipe->bufs[idx].len = PAGE_SIZE;
518 idx = next_idx(idx, pipe);
520 while (idx != pipe->curbuf || !pipe->nrbufs) {
521 struct page *page = alloc_page(GFP_USER);
525 pipe->bufs[idx].ops = &default_pipe_buf_ops;
526 pipe->bufs[idx].page = page;
527 pipe->bufs[idx].offset = 0;
528 if (left <= PAGE_SIZE) {
529 pipe->bufs[idx].len = left;
532 pipe->bufs[idx].len = PAGE_SIZE;
534 idx = next_idx(idx, pipe);
539 static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
542 struct pipe_inode_info *pipe = i->pipe;
549 bytes = n = push_pipe(i, bytes, &idx, &off);
552 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
553 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
554 memcpy_to_page(pipe->bufs[idx].page, off, addr, chunk);
556 i->iov_offset = off + chunk;
564 static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
565 __wsum *csum, struct iov_iter *i)
567 struct pipe_inode_info *pipe = i->pipe;
570 __wsum sum = *csum, next;
576 bytes = n = push_pipe(i, bytes, &idx, &r);
579 for ( ; n; idx = next_idx(idx, pipe), r = 0) {
580 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
581 char *p = kmap_atomic(pipe->bufs[idx].page);
582 next = csum_partial_copy_nocheck(addr, p + r, chunk, 0);
583 sum = csum_block_add(sum, next, off);
586 i->iov_offset = r + chunk;
596 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
598 const char *from = addr;
599 if (unlikely(iov_iter_is_pipe(i)))
600 return copy_pipe_to_iter(addr, bytes, i);
601 if (iter_is_iovec(i))
603 iterate_and_advance(i, bytes, v,
604 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
605 memcpy_to_page(v.bv_page, v.bv_offset,
606 (from += v.bv_len) - v.bv_len, v.bv_len),
607 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
612 EXPORT_SYMBOL(_copy_to_iter);
614 #ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
615 static int copyout_mcsafe(void __user *to, const void *from, size_t n)
617 if (access_ok(VERIFY_WRITE, to, n)) {
618 kasan_check_read(from, n);
619 n = copy_to_user_mcsafe((__force void *) to, from, n);
624 static unsigned long memcpy_mcsafe_to_page(struct page *page, size_t offset,
625 const char *from, size_t len)
630 to = kmap_atomic(page);
631 ret = memcpy_mcsafe(to + offset, from, len);
637 static size_t copy_pipe_to_iter_mcsafe(const void *addr, size_t bytes,
640 struct pipe_inode_info *pipe = i->pipe;
641 size_t n, off, xfer = 0;
647 bytes = n = push_pipe(i, bytes, &idx, &off);
650 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
651 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
654 rem = memcpy_mcsafe_to_page(pipe->bufs[idx].page, off, addr,
657 i->iov_offset = off + chunk - rem;
669 * _copy_to_iter_mcsafe - copy to user with source-read error exception handling
670 * @addr: source kernel address
671 * @bytes: total transfer length
672 * @iter: destination iterator
674 * The pmem driver arranges for filesystem-dax to use this facility via
675 * dax_copy_to_iter() for protecting read/write to persistent memory.
676 * Unless / until an architecture can guarantee identical performance
677 * between _copy_to_iter_mcsafe() and _copy_to_iter() it would be a
678 * performance regression to switch more users to the mcsafe version.
680 * Otherwise, the main differences between this and typical _copy_to_iter().
682 * * Typical tail/residue handling after a fault retries the copy
683 * byte-by-byte until the fault happens again. Re-triggering machine
684 * checks is potentially fatal so the implementation uses source
685 * alignment and poison alignment assumptions to avoid re-triggering
686 * hardware exceptions.
688 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
689 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
692 * See MCSAFE_TEST for self-test.
694 size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i)
696 const char *from = addr;
697 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
699 if (unlikely(iov_iter_is_pipe(i)))
700 return copy_pipe_to_iter_mcsafe(addr, bytes, i);
701 if (iter_is_iovec(i))
703 iterate_and_advance(i, bytes, v,
704 copyout_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
706 rem = memcpy_mcsafe_to_page(v.bv_page, v.bv_offset,
707 (from += v.bv_len) - v.bv_len, v.bv_len);
709 curr_addr = (unsigned long) from;
710 bytes = curr_addr - s_addr - rem;
715 rem = memcpy_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len,
718 curr_addr = (unsigned long) from;
719 bytes = curr_addr - s_addr - rem;
727 EXPORT_SYMBOL_GPL(_copy_to_iter_mcsafe);
728 #endif /* CONFIG_ARCH_HAS_UACCESS_MCSAFE */
730 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
733 if (unlikely(iov_iter_is_pipe(i))) {
737 if (iter_is_iovec(i))
739 iterate_and_advance(i, bytes, v,
740 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
741 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
742 v.bv_offset, v.bv_len),
743 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
748 EXPORT_SYMBOL(_copy_from_iter);
750 bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
753 if (unlikely(iov_iter_is_pipe(i))) {
757 if (unlikely(i->count < bytes))
760 if (iter_is_iovec(i))
762 iterate_all_kinds(i, bytes, v, ({
763 if (copyin((to += v.iov_len) - v.iov_len,
764 v.iov_base, v.iov_len))
767 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
768 v.bv_offset, v.bv_len),
769 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
772 iov_iter_advance(i, bytes);
775 EXPORT_SYMBOL(_copy_from_iter_full);
777 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
780 if (unlikely(iov_iter_is_pipe(i))) {
784 iterate_and_advance(i, bytes, v,
785 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
786 v.iov_base, v.iov_len),
787 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
788 v.bv_offset, v.bv_len),
789 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
794 EXPORT_SYMBOL(_copy_from_iter_nocache);
796 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
798 * _copy_from_iter_flushcache - write destination through cpu cache
799 * @addr: destination kernel address
800 * @bytes: total transfer length
801 * @iter: source iterator
803 * The pmem driver arranges for filesystem-dax to use this facility via
804 * dax_copy_from_iter() for ensuring that writes to persistent memory
805 * are flushed through the CPU cache. It is differentiated from
806 * _copy_from_iter_nocache() in that guarantees all data is flushed for
807 * all iterator types. The _copy_from_iter_nocache() only attempts to
808 * bypass the cache for the ITER_IOVEC case, and on some archs may use
809 * instructions that strand dirty-data in the cache.
811 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
814 if (unlikely(iov_iter_is_pipe(i))) {
818 iterate_and_advance(i, bytes, v,
819 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
820 v.iov_base, v.iov_len),
821 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
822 v.bv_offset, v.bv_len),
823 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
829 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
832 bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
835 if (unlikely(iov_iter_is_pipe(i))) {
839 if (unlikely(i->count < bytes))
841 iterate_all_kinds(i, bytes, v, ({
842 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
843 v.iov_base, v.iov_len))
846 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
847 v.bv_offset, v.bv_len),
848 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
851 iov_iter_advance(i, bytes);
854 EXPORT_SYMBOL(_copy_from_iter_full_nocache);
856 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
858 struct page *head = compound_head(page);
859 size_t v = n + offset + page_address(page) - page_address(head);
861 if (likely(n <= v && v <= (PAGE_SIZE << compound_order(head))))
867 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
870 if (unlikely(!page_copy_sane(page, offset, bytes)))
872 if (i->type & (ITER_BVEC|ITER_KVEC)) {
873 void *kaddr = kmap_atomic(page);
874 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
875 kunmap_atomic(kaddr);
877 } else if (unlikely(iov_iter_is_discard(i)))
879 else if (likely(!iov_iter_is_pipe(i)))
880 return copy_page_to_iter_iovec(page, offset, bytes, i);
882 return copy_page_to_iter_pipe(page, offset, bytes, i);
884 EXPORT_SYMBOL(copy_page_to_iter);
886 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
889 if (unlikely(!page_copy_sane(page, offset, bytes)))
891 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
895 if (i->type & (ITER_BVEC|ITER_KVEC)) {
896 void *kaddr = kmap_atomic(page);
897 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
898 kunmap_atomic(kaddr);
901 return copy_page_from_iter_iovec(page, offset, bytes, i);
903 EXPORT_SYMBOL(copy_page_from_iter);
905 static size_t pipe_zero(size_t bytes, struct iov_iter *i)
907 struct pipe_inode_info *pipe = i->pipe;
914 bytes = n = push_pipe(i, bytes, &idx, &off);
918 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
919 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
920 memzero_page(pipe->bufs[idx].page, off, chunk);
922 i->iov_offset = off + chunk;
929 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
931 if (unlikely(iov_iter_is_pipe(i)))
932 return pipe_zero(bytes, i);
933 iterate_and_advance(i, bytes, v,
934 clear_user(v.iov_base, v.iov_len),
935 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
936 memset(v.iov_base, 0, v.iov_len)
941 EXPORT_SYMBOL(iov_iter_zero);
943 size_t iov_iter_copy_from_user_atomic(struct page *page,
944 struct iov_iter *i, unsigned long offset, size_t bytes)
946 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
947 if (unlikely(!page_copy_sane(page, offset, bytes))) {
948 kunmap_atomic(kaddr);
951 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
952 kunmap_atomic(kaddr);
956 iterate_all_kinds(i, bytes, v,
957 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
958 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
959 v.bv_offset, v.bv_len),
960 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
962 kunmap_atomic(kaddr);
965 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
967 static inline void pipe_truncate(struct iov_iter *i)
969 struct pipe_inode_info *pipe = i->pipe;
971 size_t off = i->iov_offset;
973 int nrbufs = (idx - pipe->curbuf) & (pipe->buffers - 1);
975 pipe->bufs[idx].len = off - pipe->bufs[idx].offset;
976 idx = next_idx(idx, pipe);
979 while (pipe->nrbufs > nrbufs) {
980 pipe_buf_release(pipe, &pipe->bufs[idx]);
981 idx = next_idx(idx, pipe);
987 static void pipe_advance(struct iov_iter *i, size_t size)
989 struct pipe_inode_info *pipe = i->pipe;
990 if (unlikely(i->count < size))
993 struct pipe_buffer *buf;
994 size_t off = i->iov_offset, left = size;
996 if (off) /* make it relative to the beginning of buffer */
997 left += off - pipe->bufs[idx].offset;
999 buf = &pipe->bufs[idx];
1000 if (left <= buf->len)
1003 idx = next_idx(idx, pipe);
1006 i->iov_offset = buf->offset + left;
1009 /* ... and discard everything past that point */
1013 void iov_iter_advance(struct iov_iter *i, size_t size)
1015 if (unlikely(iov_iter_is_pipe(i))) {
1016 pipe_advance(i, size);
1019 if (unlikely(iov_iter_is_discard(i))) {
1023 iterate_and_advance(i, size, v, 0, 0, 0)
1025 EXPORT_SYMBOL(iov_iter_advance);
1027 void iov_iter_revert(struct iov_iter *i, size_t unroll)
1031 if (WARN_ON(unroll > MAX_RW_COUNT))
1034 if (unlikely(iov_iter_is_pipe(i))) {
1035 struct pipe_inode_info *pipe = i->pipe;
1037 size_t off = i->iov_offset;
1039 size_t n = off - pipe->bufs[idx].offset;
1045 if (!unroll && idx == i->start_idx) {
1050 idx = pipe->buffers - 1;
1051 off = pipe->bufs[idx].offset + pipe->bufs[idx].len;
1053 i->iov_offset = off;
1058 if (unlikely(iov_iter_is_discard(i)))
1060 if (unroll <= i->iov_offset) {
1061 i->iov_offset -= unroll;
1064 unroll -= i->iov_offset;
1065 if (iov_iter_is_bvec(i)) {
1066 const struct bio_vec *bvec = i->bvec;
1068 size_t n = (--bvec)->bv_len;
1072 i->iov_offset = n - unroll;
1077 } else { /* same logics for iovec and kvec */
1078 const struct iovec *iov = i->iov;
1080 size_t n = (--iov)->iov_len;
1084 i->iov_offset = n - unroll;
1091 EXPORT_SYMBOL(iov_iter_revert);
1094 * Return the count of just the current iov_iter segment.
1096 size_t iov_iter_single_seg_count(const struct iov_iter *i)
1098 if (unlikely(iov_iter_is_pipe(i)))
1099 return i->count; // it is a silly place, anyway
1100 if (i->nr_segs == 1)
1102 if (unlikely(iov_iter_is_discard(i)))
1104 else if (iov_iter_is_bvec(i))
1105 return min(i->count, i->bvec->bv_len - i->iov_offset);
1107 return min(i->count, i->iov->iov_len - i->iov_offset);
1109 EXPORT_SYMBOL(iov_iter_single_seg_count);
1111 void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
1112 const struct kvec *kvec, unsigned long nr_segs,
1115 WARN_ON(direction & ~(READ | WRITE));
1116 i->type = ITER_KVEC | (direction & (READ | WRITE));
1118 i->nr_segs = nr_segs;
1122 EXPORT_SYMBOL(iov_iter_kvec);
1124 void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1125 const struct bio_vec *bvec, unsigned long nr_segs,
1128 WARN_ON(direction & ~(READ | WRITE));
1129 i->type = ITER_BVEC | (direction & (READ | WRITE));
1131 i->nr_segs = nr_segs;
1135 EXPORT_SYMBOL(iov_iter_bvec);
1137 void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1138 struct pipe_inode_info *pipe,
1141 BUG_ON(direction != READ);
1142 WARN_ON(pipe->nrbufs == pipe->buffers);
1143 i->type = ITER_PIPE | READ;
1145 i->idx = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1148 i->start_idx = i->idx;
1150 EXPORT_SYMBOL(iov_iter_pipe);
1153 * iov_iter_discard - Initialise an I/O iterator that discards data
1154 * @i: The iterator to initialise.
1155 * @direction: The direction of the transfer.
1156 * @count: The size of the I/O buffer in bytes.
1158 * Set up an I/O iterator that just discards everything that's written to it.
1159 * It's only available as a READ iterator.
1161 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1163 BUG_ON(direction != READ);
1164 i->type = ITER_DISCARD | READ;
1168 EXPORT_SYMBOL(iov_iter_discard);
1170 unsigned long iov_iter_alignment(const struct iov_iter *i)
1172 unsigned long res = 0;
1173 size_t size = i->count;
1175 if (unlikely(iov_iter_is_pipe(i))) {
1176 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->idx]))
1177 return size | i->iov_offset;
1180 iterate_all_kinds(i, size, v,
1181 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
1182 res |= v.bv_offset | v.bv_len,
1183 res |= (unsigned long)v.iov_base | v.iov_len
1187 EXPORT_SYMBOL(iov_iter_alignment);
1189 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1191 unsigned long res = 0;
1192 size_t size = i->count;
1194 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1199 iterate_all_kinds(i, size, v,
1200 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1201 (size != v.iov_len ? size : 0), 0),
1202 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1203 (size != v.bv_len ? size : 0)),
1204 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1205 (size != v.iov_len ? size : 0))
1209 EXPORT_SYMBOL(iov_iter_gap_alignment);
1211 static inline ssize_t __pipe_get_pages(struct iov_iter *i,
1213 struct page **pages,
1217 struct pipe_inode_info *pipe = i->pipe;
1218 ssize_t n = push_pipe(i, maxsize, &idx, start);
1225 get_page(*pages++ = pipe->bufs[idx].page);
1226 idx = next_idx(idx, pipe);
1233 static ssize_t pipe_get_pages(struct iov_iter *i,
1234 struct page **pages, size_t maxsize, unsigned maxpages,
1247 data_start(i, &idx, start);
1248 /* some of this one + all after this one */
1249 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1250 capacity = min(npages,maxpages) * PAGE_SIZE - *start;
1252 return __pipe_get_pages(i, min(maxsize, capacity), pages, idx, start);
1255 ssize_t iov_iter_get_pages(struct iov_iter *i,
1256 struct page **pages, size_t maxsize, unsigned maxpages,
1259 if (maxsize > i->count)
1262 if (unlikely(iov_iter_is_pipe(i)))
1263 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1264 if (unlikely(iov_iter_is_discard(i)))
1267 iterate_all_kinds(i, maxsize, v, ({
1268 unsigned long addr = (unsigned long)v.iov_base;
1269 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1273 if (len > maxpages * PAGE_SIZE)
1274 len = maxpages * PAGE_SIZE;
1275 addr &= ~(PAGE_SIZE - 1);
1276 n = DIV_ROUND_UP(len, PAGE_SIZE);
1277 res = get_user_pages_fast(addr, n, iov_iter_rw(i) != WRITE, pages);
1278 if (unlikely(res < 0))
1280 return (res == n ? len : res * PAGE_SIZE) - *start;
1282 /* can't be more than PAGE_SIZE */
1283 *start = v.bv_offset;
1284 get_page(*pages = v.bv_page);
1292 EXPORT_SYMBOL(iov_iter_get_pages);
1294 static struct page **get_pages_array(size_t n)
1296 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
1299 static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1300 struct page ***pages, size_t maxsize,
1314 data_start(i, &idx, start);
1315 /* some of this one + all after this one */
1316 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1317 n = npages * PAGE_SIZE - *start;
1321 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1322 p = get_pages_array(npages);
1325 n = __pipe_get_pages(i, maxsize, p, idx, start);
1333 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1334 struct page ***pages, size_t maxsize,
1339 if (maxsize > i->count)
1342 if (unlikely(iov_iter_is_pipe(i)))
1343 return pipe_get_pages_alloc(i, pages, maxsize, start);
1344 if (unlikely(iov_iter_is_discard(i)))
1347 iterate_all_kinds(i, maxsize, v, ({
1348 unsigned long addr = (unsigned long)v.iov_base;
1349 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1353 addr &= ~(PAGE_SIZE - 1);
1354 n = DIV_ROUND_UP(len, PAGE_SIZE);
1355 p = get_pages_array(n);
1358 res = get_user_pages_fast(addr, n, iov_iter_rw(i) != WRITE, p);
1359 if (unlikely(res < 0)) {
1364 return (res == n ? len : res * PAGE_SIZE) - *start;
1366 /* can't be more than PAGE_SIZE */
1367 *start = v.bv_offset;
1368 *pages = p = get_pages_array(1);
1371 get_page(*p = v.bv_page);
1379 EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1381 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1388 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1392 iterate_and_advance(i, bytes, v, ({
1394 next = csum_and_copy_from_user(v.iov_base,
1395 (to += v.iov_len) - v.iov_len,
1396 v.iov_len, 0, &err);
1398 sum = csum_block_add(sum, next, off);
1401 err ? v.iov_len : 0;
1403 char *p = kmap_atomic(v.bv_page);
1404 next = csum_partial_copy_nocheck(p + v.bv_offset,
1405 (to += v.bv_len) - v.bv_len,
1408 sum = csum_block_add(sum, next, off);
1411 next = csum_partial_copy_nocheck(v.iov_base,
1412 (to += v.iov_len) - v.iov_len,
1414 sum = csum_block_add(sum, next, off);
1421 EXPORT_SYMBOL(csum_and_copy_from_iter);
1423 bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1430 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1434 if (unlikely(i->count < bytes))
1436 iterate_all_kinds(i, bytes, v, ({
1438 next = csum_and_copy_from_user(v.iov_base,
1439 (to += v.iov_len) - v.iov_len,
1440 v.iov_len, 0, &err);
1443 sum = csum_block_add(sum, next, off);
1447 char *p = kmap_atomic(v.bv_page);
1448 next = csum_partial_copy_nocheck(p + v.bv_offset,
1449 (to += v.bv_len) - v.bv_len,
1452 sum = csum_block_add(sum, next, off);
1455 next = csum_partial_copy_nocheck(v.iov_base,
1456 (to += v.iov_len) - v.iov_len,
1458 sum = csum_block_add(sum, next, off);
1463 iov_iter_advance(i, bytes);
1466 EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1468 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csump,
1471 const char *from = addr;
1472 __wsum *csum = csump;
1476 if (unlikely(iov_iter_is_pipe(i)))
1477 return csum_and_copy_to_pipe_iter(addr, bytes, csum, i);
1480 if (unlikely(iov_iter_is_discard(i))) {
1481 WARN_ON(1); /* for now */
1484 iterate_and_advance(i, bytes, v, ({
1486 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
1488 v.iov_len, 0, &err);
1490 sum = csum_block_add(sum, next, off);
1493 err ? v.iov_len : 0;
1495 char *p = kmap_atomic(v.bv_page);
1496 next = csum_partial_copy_nocheck((from += v.bv_len) - v.bv_len,
1500 sum = csum_block_add(sum, next, off);
1503 next = csum_partial_copy_nocheck((from += v.iov_len) - v.iov_len,
1506 sum = csum_block_add(sum, next, off);
1513 EXPORT_SYMBOL(csum_and_copy_to_iter);
1515 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1518 struct ahash_request *hash = hashp;
1519 struct scatterlist sg;
1522 copied = copy_to_iter(addr, bytes, i);
1523 sg_init_one(&sg, addr, copied);
1524 ahash_request_set_crypt(hash, &sg, NULL, copied);
1525 crypto_ahash_update(hash);
1528 EXPORT_SYMBOL(hash_and_copy_to_iter);
1530 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1532 size_t size = i->count;
1537 if (unlikely(iov_iter_is_discard(i)))
1540 if (unlikely(iov_iter_is_pipe(i))) {
1541 struct pipe_inode_info *pipe = i->pipe;
1548 data_start(i, &idx, &off);
1549 /* some of this one + all after this one */
1550 npages = ((pipe->curbuf - idx - 1) & (pipe->buffers - 1)) + 1;
1551 if (npages >= maxpages)
1553 } else iterate_all_kinds(i, size, v, ({
1554 unsigned long p = (unsigned long)v.iov_base;
1555 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1557 if (npages >= maxpages)
1561 if (npages >= maxpages)
1564 unsigned long p = (unsigned long)v.iov_base;
1565 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1567 if (npages >= maxpages)
1573 EXPORT_SYMBOL(iov_iter_npages);
1575 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1578 if (unlikely(iov_iter_is_pipe(new))) {
1582 if (unlikely(iov_iter_is_discard(new)))
1584 if (iov_iter_is_bvec(new))
1585 return new->bvec = kmemdup(new->bvec,
1586 new->nr_segs * sizeof(struct bio_vec),
1589 /* iovec and kvec have identical layout */
1590 return new->iov = kmemdup(new->iov,
1591 new->nr_segs * sizeof(struct iovec),
1594 EXPORT_SYMBOL(dup_iter);
1597 * import_iovec() - Copy an array of &struct iovec from userspace
1598 * into the kernel, check that it is valid, and initialize a new
1599 * &struct iov_iter iterator to access it.
1601 * @type: One of %READ or %WRITE.
1602 * @uvector: Pointer to the userspace array.
1603 * @nr_segs: Number of elements in userspace array.
1604 * @fast_segs: Number of elements in @iov.
1605 * @iov: (input and output parameter) Pointer to pointer to (usually small
1606 * on-stack) kernel array.
1607 * @i: Pointer to iterator that will be initialized on success.
1609 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1610 * then this function places %NULL in *@iov on return. Otherwise, a new
1611 * array will be allocated and the result placed in *@iov. This means that
1612 * the caller may call kfree() on *@iov regardless of whether the small
1613 * on-stack array was used or not (and regardless of whether this function
1614 * returns an error or not).
1616 * Return: 0 on success or negative error code on error.
1618 int import_iovec(int type, const struct iovec __user * uvector,
1619 unsigned nr_segs, unsigned fast_segs,
1620 struct iovec **iov, struct iov_iter *i)
1624 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1632 iov_iter_init(i, type, p, nr_segs, n);
1633 *iov = p == *iov ? NULL : p;
1636 EXPORT_SYMBOL(import_iovec);
1638 #ifdef CONFIG_COMPAT
1639 #include <linux/compat.h>
1641 int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
1642 unsigned nr_segs, unsigned fast_segs,
1643 struct iovec **iov, struct iov_iter *i)
1647 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1655 iov_iter_init(i, type, p, nr_segs, n);
1656 *iov = p == *iov ? NULL : p;
1661 int import_single_range(int rw, void __user *buf, size_t len,
1662 struct iovec *iov, struct iov_iter *i)
1664 if (len > MAX_RW_COUNT)
1666 if (unlikely(!access_ok(!rw, buf, len)))
1669 iov->iov_base = buf;
1671 iov_iter_init(i, rw, iov, 1, len);
1674 EXPORT_SYMBOL(import_single_range);
1676 int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1677 int (*f)(struct kvec *vec, void *context),
1685 iterate_all_kinds(i, bytes, v, -EINVAL, ({
1686 w.iov_base = kmap(v.bv_page) + v.bv_offset;
1687 w.iov_len = v.bv_len;
1688 err = f(&w, context);
1692 err = f(&w, context);})
1696 EXPORT_SYMBOL(iov_iter_for_each_range);