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/slab.h>
9 #include <linux/vmalloc.h>
10 #include <linux/splice.h>
11 #include <linux/compat.h>
12 #include <net/checksum.h>
13 #include <linux/scatterlist.h>
14 #include <linux/instrumented.h>
16 #define PIPE_PARANOIA /* for now */
18 #define iterate_iovec(i, n, __v, __p, skip, STEP) { \
22 __v.iov_len = min(n, __p->iov_len - skip); \
23 if (likely(__v.iov_len)) { \
24 __v.iov_base = __p->iov_base + skip; \
26 __v.iov_len -= left; \
27 skip += __v.iov_len; \
32 while (unlikely(!left && n)) { \
34 __v.iov_len = min(n, __p->iov_len); \
35 if (unlikely(!__v.iov_len)) \
37 __v.iov_base = __p->iov_base; \
39 __v.iov_len -= left; \
46 #define iterate_kvec(i, n, __v, __p, skip, STEP) { \
49 __v.iov_len = min(n, __p->iov_len - skip); \
50 if (likely(__v.iov_len)) { \
51 __v.iov_base = __p->iov_base + skip; \
53 skip += __v.iov_len; \
56 while (unlikely(n)) { \
58 __v.iov_len = min(n, __p->iov_len); \
59 if (unlikely(!__v.iov_len)) \
61 __v.iov_base = __p->iov_base; \
69 #define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
70 struct bvec_iter __start; \
71 __start.bi_size = n; \
72 __start.bi_bvec_done = skip; \
74 for_each_bvec(__v, i->bvec, __bi, __start) { \
81 #define iterate_all_kinds(i, n, v, I, B, K) { \
83 size_t skip = i->iov_offset; \
84 if (unlikely(i->type & ITER_BVEC)) { \
86 struct bvec_iter __bi; \
87 iterate_bvec(i, n, v, __bi, skip, (B)) \
88 } else if (unlikely(i->type & ITER_KVEC)) { \
89 const struct kvec *kvec; \
91 iterate_kvec(i, n, v, kvec, skip, (K)) \
92 } else if (unlikely(i->type & ITER_DISCARD)) { \
94 const struct iovec *iov; \
96 iterate_iovec(i, n, v, iov, skip, (I)) \
101 #define iterate_and_advance(i, n, v, I, B, K) { \
102 if (unlikely(i->count < n)) \
105 size_t skip = i->iov_offset; \
106 if (unlikely(i->type & ITER_BVEC)) { \
107 const struct bio_vec *bvec = i->bvec; \
109 struct bvec_iter __bi; \
110 iterate_bvec(i, n, v, __bi, skip, (B)) \
111 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
112 i->nr_segs -= i->bvec - bvec; \
113 skip = __bi.bi_bvec_done; \
114 } else if (unlikely(i->type & ITER_KVEC)) { \
115 const struct kvec *kvec; \
117 iterate_kvec(i, n, v, kvec, skip, (K)) \
118 if (skip == kvec->iov_len) { \
122 i->nr_segs -= kvec - i->kvec; \
124 } else if (unlikely(i->type & ITER_DISCARD)) { \
127 const struct iovec *iov; \
129 iterate_iovec(i, n, v, iov, skip, (I)) \
130 if (skip == iov->iov_len) { \
134 i->nr_segs -= iov - i->iov; \
138 i->iov_offset = skip; \
142 static int copyout(void __user *to, const void *from, size_t n)
144 if (should_fail_usercopy())
146 if (access_ok(to, n)) {
147 instrument_copy_to_user(to, from, n);
148 n = raw_copy_to_user(to, from, n);
153 static int copyin(void *to, const void __user *from, size_t n)
155 if (should_fail_usercopy())
157 if (access_ok(from, n)) {
158 instrument_copy_from_user(to, from, n);
159 n = raw_copy_from_user(to, from, n);
164 static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
167 size_t skip, copy, left, wanted;
168 const struct iovec *iov;
172 if (unlikely(bytes > i->count))
175 if (unlikely(!bytes))
181 skip = i->iov_offset;
182 buf = iov->iov_base + skip;
183 copy = min(bytes, iov->iov_len - skip);
185 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
186 kaddr = kmap_atomic(page);
187 from = kaddr + offset;
189 /* first chunk, usually the only one */
190 left = copyout(buf, from, copy);
196 while (unlikely(!left && bytes)) {
199 copy = min(bytes, iov->iov_len);
200 left = copyout(buf, from, copy);
206 if (likely(!bytes)) {
207 kunmap_atomic(kaddr);
210 offset = from - kaddr;
212 kunmap_atomic(kaddr);
213 copy = min(bytes, iov->iov_len - skip);
215 /* Too bad - revert to non-atomic kmap */
218 from = kaddr + offset;
219 left = copyout(buf, from, copy);
224 while (unlikely(!left && bytes)) {
227 copy = min(bytes, iov->iov_len);
228 left = copyout(buf, from, copy);
237 if (skip == iov->iov_len) {
241 i->count -= wanted - bytes;
242 i->nr_segs -= iov - i->iov;
244 i->iov_offset = skip;
245 return wanted - bytes;
248 static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
251 size_t skip, copy, left, wanted;
252 const struct iovec *iov;
256 if (unlikely(bytes > i->count))
259 if (unlikely(!bytes))
265 skip = i->iov_offset;
266 buf = iov->iov_base + skip;
267 copy = min(bytes, iov->iov_len - skip);
269 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
270 kaddr = kmap_atomic(page);
273 /* first chunk, usually the only one */
274 left = copyin(to, buf, copy);
280 while (unlikely(!left && bytes)) {
283 copy = min(bytes, iov->iov_len);
284 left = copyin(to, buf, copy);
290 if (likely(!bytes)) {
291 kunmap_atomic(kaddr);
296 kunmap_atomic(kaddr);
297 copy = min(bytes, iov->iov_len - skip);
299 /* Too bad - revert to non-atomic kmap */
303 left = copyin(to, buf, copy);
308 while (unlikely(!left && bytes)) {
311 copy = min(bytes, iov->iov_len);
312 left = copyin(to, buf, copy);
321 if (skip == iov->iov_len) {
325 i->count -= wanted - bytes;
326 i->nr_segs -= iov - i->iov;
328 i->iov_offset = skip;
329 return wanted - bytes;
333 static bool sanity(const struct iov_iter *i)
335 struct pipe_inode_info *pipe = i->pipe;
336 unsigned int p_head = pipe->head;
337 unsigned int p_tail = pipe->tail;
338 unsigned int p_mask = pipe->ring_size - 1;
339 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
340 unsigned int i_head = i->head;
344 struct pipe_buffer *p;
345 if (unlikely(p_occupancy == 0))
346 goto Bad; // pipe must be non-empty
347 if (unlikely(i_head != p_head - 1))
348 goto Bad; // must be at the last buffer...
350 p = &pipe->bufs[i_head & p_mask];
351 if (unlikely(p->offset + p->len != i->iov_offset))
352 goto Bad; // ... at the end of segment
354 if (i_head != p_head)
355 goto Bad; // must be right after the last buffer
359 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
360 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
361 p_head, p_tail, pipe->ring_size);
362 for (idx = 0; idx < pipe->ring_size; idx++)
363 printk(KERN_ERR "[%p %p %d %d]\n",
365 pipe->bufs[idx].page,
366 pipe->bufs[idx].offset,
367 pipe->bufs[idx].len);
372 #define sanity(i) true
375 static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
378 struct pipe_inode_info *pipe = i->pipe;
379 struct pipe_buffer *buf;
380 unsigned int p_tail = pipe->tail;
381 unsigned int p_mask = pipe->ring_size - 1;
382 unsigned int i_head = i->head;
385 if (unlikely(bytes > i->count))
388 if (unlikely(!bytes))
395 buf = &pipe->bufs[i_head & p_mask];
397 if (offset == off && buf->page == page) {
398 /* merge with the last one */
400 i->iov_offset += bytes;
404 buf = &pipe->bufs[i_head & p_mask];
406 if (pipe_full(i_head, p_tail, pipe->max_usage))
409 buf->ops = &page_cache_pipe_buf_ops;
413 buf->offset = offset;
416 pipe->head = i_head + 1;
417 i->iov_offset = offset + bytes;
425 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
426 * bytes. For each iovec, fault in each page that constitutes the iovec.
428 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
429 * because it is an invalid address).
431 int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
433 size_t skip = i->iov_offset;
434 const struct iovec *iov;
438 if (iter_is_iovec(i)) {
439 iterate_iovec(i, bytes, v, iov, skip, ({
440 err = fault_in_pages_readable(v.iov_base, v.iov_len);
447 EXPORT_SYMBOL(iov_iter_fault_in_readable);
449 void iov_iter_init(struct iov_iter *i, unsigned int direction,
450 const struct iovec *iov, unsigned long nr_segs,
453 WARN_ON(direction & ~(READ | WRITE));
454 direction &= READ | WRITE;
456 /* It will get better. Eventually... */
457 if (uaccess_kernel()) {
458 i->type = ITER_KVEC | direction;
459 i->kvec = (struct kvec *)iov;
461 i->type = ITER_IOVEC | direction;
464 i->nr_segs = nr_segs;
468 EXPORT_SYMBOL(iov_iter_init);
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,
483 unsigned int *iter_headp, size_t *offp)
485 unsigned int p_mask = i->pipe->ring_size - 1;
486 unsigned int iter_head = i->head;
487 size_t off = i->iov_offset;
489 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
494 *iter_headp = iter_head;
498 static size_t push_pipe(struct iov_iter *i, size_t size,
499 int *iter_headp, size_t *offp)
501 struct pipe_inode_info *pipe = i->pipe;
502 unsigned int p_tail = pipe->tail;
503 unsigned int p_mask = pipe->ring_size - 1;
504 unsigned int iter_head;
508 if (unlikely(size > i->count))
514 data_start(i, &iter_head, &off);
515 *iter_headp = iter_head;
518 left -= PAGE_SIZE - off;
520 pipe->bufs[iter_head & p_mask].len += size;
523 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
526 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
527 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
528 struct page *page = alloc_page(GFP_USER);
532 buf->ops = &default_pipe_buf_ops;
536 buf->len = min_t(ssize_t, left, PAGE_SIZE);
539 pipe->head = iter_head;
547 static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
550 struct pipe_inode_info *pipe = i->pipe;
551 unsigned int p_mask = pipe->ring_size - 1;
558 bytes = n = push_pipe(i, bytes, &i_head, &off);
562 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
563 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
565 i->iov_offset = off + chunk;
575 static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
576 __wsum sum, size_t off)
578 __wsum next = csum_partial_copy_nocheck(from, to, len);
579 return csum_block_add(sum, next, off);
582 static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
583 struct csum_state *csstate,
586 struct pipe_inode_info *pipe = i->pipe;
587 unsigned int p_mask = pipe->ring_size - 1;
588 __wsum sum = csstate->csum;
589 size_t off = csstate->off;
596 bytes = n = push_pipe(i, bytes, &i_head, &r);
600 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
601 char *p = kmap_atomic(pipe->bufs[i_head & p_mask].page);
602 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
605 i->iov_offset = r + chunk;
618 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
620 const char *from = addr;
621 if (unlikely(iov_iter_is_pipe(i)))
622 return copy_pipe_to_iter(addr, bytes, i);
623 if (iter_is_iovec(i))
625 iterate_and_advance(i, bytes, v,
626 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
627 memcpy_to_page(v.bv_page, v.bv_offset,
628 (from += v.bv_len) - v.bv_len, v.bv_len),
629 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
634 EXPORT_SYMBOL(_copy_to_iter);
636 #ifdef CONFIG_ARCH_HAS_COPY_MC
637 static int copyout_mc(void __user *to, const void *from, size_t n)
639 if (access_ok(to, n)) {
640 instrument_copy_to_user(to, from, n);
641 n = copy_mc_to_user((__force void *) to, from, n);
646 static unsigned long copy_mc_to_page(struct page *page, size_t offset,
647 const char *from, size_t len)
652 to = kmap_atomic(page);
653 ret = copy_mc_to_kernel(to + offset, from, len);
659 static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
662 struct pipe_inode_info *pipe = i->pipe;
663 unsigned int p_mask = pipe->ring_size - 1;
665 size_t n, off, xfer = 0;
670 bytes = n = push_pipe(i, bytes, &i_head, &off);
674 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
677 rem = copy_mc_to_page(pipe->bufs[i_head & p_mask].page,
680 i->iov_offset = off + chunk - rem;
694 * _copy_mc_to_iter - copy to iter with source memory error exception handling
695 * @addr: source kernel address
696 * @bytes: total transfer length
697 * @iter: destination iterator
699 * The pmem driver deploys this for the dax operation
700 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
701 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
702 * successfully copied.
704 * The main differences between this and typical _copy_to_iter().
706 * * Typical tail/residue handling after a fault retries the copy
707 * byte-by-byte until the fault happens again. Re-triggering machine
708 * checks is potentially fatal so the implementation uses source
709 * alignment and poison alignment assumptions to avoid re-triggering
710 * hardware exceptions.
712 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
713 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
716 size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
718 const char *from = addr;
719 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
721 if (unlikely(iov_iter_is_pipe(i)))
722 return copy_mc_pipe_to_iter(addr, bytes, i);
723 if (iter_is_iovec(i))
725 iterate_and_advance(i, bytes, v,
726 copyout_mc(v.iov_base, (from += v.iov_len) - v.iov_len,
729 rem = copy_mc_to_page(v.bv_page, v.bv_offset,
730 (from += v.bv_len) - v.bv_len, v.bv_len);
732 curr_addr = (unsigned long) from;
733 bytes = curr_addr - s_addr - rem;
738 rem = copy_mc_to_kernel(v.iov_base, (from += v.iov_len)
739 - v.iov_len, v.iov_len);
741 curr_addr = (unsigned long) from;
742 bytes = curr_addr - s_addr - rem;
750 EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
751 #endif /* CONFIG_ARCH_HAS_COPY_MC */
753 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
756 if (unlikely(iov_iter_is_pipe(i))) {
760 if (iter_is_iovec(i))
762 iterate_and_advance(i, bytes, v,
763 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
764 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
765 v.bv_offset, v.bv_len),
766 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
771 EXPORT_SYMBOL(_copy_from_iter);
773 bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
776 if (unlikely(iov_iter_is_pipe(i))) {
780 if (unlikely(i->count < bytes))
783 if (iter_is_iovec(i))
785 iterate_all_kinds(i, bytes, v, ({
786 if (copyin((to += v.iov_len) - v.iov_len,
787 v.iov_base, v.iov_len))
790 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
791 v.bv_offset, v.bv_len),
792 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
795 iov_iter_advance(i, bytes);
798 EXPORT_SYMBOL(_copy_from_iter_full);
800 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
803 if (unlikely(iov_iter_is_pipe(i))) {
807 iterate_and_advance(i, bytes, v,
808 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
809 v.iov_base, v.iov_len),
810 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
811 v.bv_offset, v.bv_len),
812 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
817 EXPORT_SYMBOL(_copy_from_iter_nocache);
819 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
821 * _copy_from_iter_flushcache - write destination through cpu cache
822 * @addr: destination kernel address
823 * @bytes: total transfer length
824 * @iter: source iterator
826 * The pmem driver arranges for filesystem-dax to use this facility via
827 * dax_copy_from_iter() for ensuring that writes to persistent memory
828 * are flushed through the CPU cache. It is differentiated from
829 * _copy_from_iter_nocache() in that guarantees all data is flushed for
830 * all iterator types. The _copy_from_iter_nocache() only attempts to
831 * bypass the cache for the ITER_IOVEC case, and on some archs may use
832 * instructions that strand dirty-data in the cache.
834 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
837 if (unlikely(iov_iter_is_pipe(i))) {
841 iterate_and_advance(i, bytes, v,
842 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
843 v.iov_base, v.iov_len),
844 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
845 v.bv_offset, v.bv_len),
846 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
852 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
855 bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
858 if (unlikely(iov_iter_is_pipe(i))) {
862 if (unlikely(i->count < bytes))
864 iterate_all_kinds(i, bytes, v, ({
865 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
866 v.iov_base, v.iov_len))
869 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
870 v.bv_offset, v.bv_len),
871 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
874 iov_iter_advance(i, bytes);
877 EXPORT_SYMBOL(_copy_from_iter_full_nocache);
879 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
882 size_t v = n + offset;
885 * The general case needs to access the page order in order
886 * to compute the page size.
887 * However, we mostly deal with order-0 pages and thus can
888 * avoid a possible cache line miss for requests that fit all
891 if (n <= v && v <= PAGE_SIZE)
894 head = compound_head(page);
895 v += (page - head) << PAGE_SHIFT;
897 if (likely(n <= v && v <= (page_size(head))))
903 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
906 if (unlikely(!page_copy_sane(page, offset, bytes)))
908 if (i->type & (ITER_BVEC|ITER_KVEC)) {
909 void *kaddr = kmap_atomic(page);
910 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
911 kunmap_atomic(kaddr);
913 } else if (unlikely(iov_iter_is_discard(i))) {
914 if (unlikely(i->count < bytes))
918 } else if (likely(!iov_iter_is_pipe(i)))
919 return copy_page_to_iter_iovec(page, offset, bytes, i);
921 return copy_page_to_iter_pipe(page, offset, bytes, i);
923 EXPORT_SYMBOL(copy_page_to_iter);
925 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
928 if (unlikely(!page_copy_sane(page, offset, bytes)))
930 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
934 if (i->type & (ITER_BVEC|ITER_KVEC)) {
935 void *kaddr = kmap_atomic(page);
936 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
937 kunmap_atomic(kaddr);
940 return copy_page_from_iter_iovec(page, offset, bytes, i);
942 EXPORT_SYMBOL(copy_page_from_iter);
944 static size_t pipe_zero(size_t bytes, struct iov_iter *i)
946 struct pipe_inode_info *pipe = i->pipe;
947 unsigned int p_mask = pipe->ring_size - 1;
954 bytes = n = push_pipe(i, bytes, &i_head, &off);
959 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
960 memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk);
962 i->iov_offset = off + chunk;
971 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
973 if (unlikely(iov_iter_is_pipe(i)))
974 return pipe_zero(bytes, i);
975 iterate_and_advance(i, bytes, v,
976 clear_user(v.iov_base, v.iov_len),
977 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
978 memset(v.iov_base, 0, v.iov_len)
983 EXPORT_SYMBOL(iov_iter_zero);
985 size_t iov_iter_copy_from_user_atomic(struct page *page,
986 struct iov_iter *i, unsigned long offset, size_t bytes)
988 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
989 if (unlikely(!page_copy_sane(page, offset, bytes))) {
990 kunmap_atomic(kaddr);
993 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
994 kunmap_atomic(kaddr);
998 iterate_all_kinds(i, bytes, v,
999 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
1000 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
1001 v.bv_offset, v.bv_len),
1002 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
1004 kunmap_atomic(kaddr);
1007 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
1009 static inline void pipe_truncate(struct iov_iter *i)
1011 struct pipe_inode_info *pipe = i->pipe;
1012 unsigned int p_tail = pipe->tail;
1013 unsigned int p_head = pipe->head;
1014 unsigned int p_mask = pipe->ring_size - 1;
1016 if (!pipe_empty(p_head, p_tail)) {
1017 struct pipe_buffer *buf;
1018 unsigned int i_head = i->head;
1019 size_t off = i->iov_offset;
1022 buf = &pipe->bufs[i_head & p_mask];
1023 buf->len = off - buf->offset;
1026 while (p_head != i_head) {
1028 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
1031 pipe->head = p_head;
1035 static void pipe_advance(struct iov_iter *i, size_t size)
1037 struct pipe_inode_info *pipe = i->pipe;
1038 if (unlikely(i->count < size))
1041 struct pipe_buffer *buf;
1042 unsigned int p_mask = pipe->ring_size - 1;
1043 unsigned int i_head = i->head;
1044 size_t off = i->iov_offset, left = size;
1046 if (off) /* make it relative to the beginning of buffer */
1047 left += off - pipe->bufs[i_head & p_mask].offset;
1049 buf = &pipe->bufs[i_head & p_mask];
1050 if (left <= buf->len)
1056 i->iov_offset = buf->offset + left;
1059 /* ... and discard everything past that point */
1063 void iov_iter_advance(struct iov_iter *i, size_t size)
1065 if (unlikely(iov_iter_is_pipe(i))) {
1066 pipe_advance(i, size);
1069 if (unlikely(iov_iter_is_discard(i))) {
1073 iterate_and_advance(i, size, v, 0, 0, 0)
1075 EXPORT_SYMBOL(iov_iter_advance);
1077 void iov_iter_revert(struct iov_iter *i, size_t unroll)
1081 if (WARN_ON(unroll > MAX_RW_COUNT))
1084 if (unlikely(iov_iter_is_pipe(i))) {
1085 struct pipe_inode_info *pipe = i->pipe;
1086 unsigned int p_mask = pipe->ring_size - 1;
1087 unsigned int i_head = i->head;
1088 size_t off = i->iov_offset;
1090 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1091 size_t n = off - b->offset;
1097 if (!unroll && i_head == i->start_head) {
1102 b = &pipe->bufs[i_head & p_mask];
1103 off = b->offset + b->len;
1105 i->iov_offset = off;
1110 if (unlikely(iov_iter_is_discard(i)))
1112 if (unroll <= i->iov_offset) {
1113 i->iov_offset -= unroll;
1116 unroll -= i->iov_offset;
1117 if (iov_iter_is_bvec(i)) {
1118 const struct bio_vec *bvec = i->bvec;
1120 size_t n = (--bvec)->bv_len;
1124 i->iov_offset = n - unroll;
1129 } else { /* same logics for iovec and kvec */
1130 const struct iovec *iov = i->iov;
1132 size_t n = (--iov)->iov_len;
1136 i->iov_offset = n - unroll;
1143 EXPORT_SYMBOL(iov_iter_revert);
1146 * Return the count of just the current iov_iter segment.
1148 size_t iov_iter_single_seg_count(const struct iov_iter *i)
1150 if (unlikely(iov_iter_is_pipe(i)))
1151 return i->count; // it is a silly place, anyway
1152 if (i->nr_segs == 1)
1154 if (unlikely(iov_iter_is_discard(i)))
1156 else if (iov_iter_is_bvec(i))
1157 return min(i->count, i->bvec->bv_len - i->iov_offset);
1159 return min(i->count, i->iov->iov_len - i->iov_offset);
1161 EXPORT_SYMBOL(iov_iter_single_seg_count);
1163 void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
1164 const struct kvec *kvec, unsigned long nr_segs,
1167 WARN_ON(direction & ~(READ | WRITE));
1168 i->type = ITER_KVEC | (direction & (READ | WRITE));
1170 i->nr_segs = nr_segs;
1174 EXPORT_SYMBOL(iov_iter_kvec);
1176 void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1177 const struct bio_vec *bvec, unsigned long nr_segs,
1180 WARN_ON(direction & ~(READ | WRITE));
1181 i->type = ITER_BVEC | (direction & (READ | WRITE));
1183 i->nr_segs = nr_segs;
1187 EXPORT_SYMBOL(iov_iter_bvec);
1189 void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1190 struct pipe_inode_info *pipe,
1193 BUG_ON(direction != READ);
1194 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
1195 i->type = ITER_PIPE | READ;
1197 i->head = pipe->head;
1200 i->start_head = i->head;
1202 EXPORT_SYMBOL(iov_iter_pipe);
1205 * iov_iter_discard - Initialise an I/O iterator that discards data
1206 * @i: The iterator to initialise.
1207 * @direction: The direction of the transfer.
1208 * @count: The size of the I/O buffer in bytes.
1210 * Set up an I/O iterator that just discards everything that's written to it.
1211 * It's only available as a READ iterator.
1213 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1215 BUG_ON(direction != READ);
1216 i->type = ITER_DISCARD | READ;
1220 EXPORT_SYMBOL(iov_iter_discard);
1222 unsigned long iov_iter_alignment(const struct iov_iter *i)
1224 unsigned long res = 0;
1225 size_t size = i->count;
1227 if (unlikely(iov_iter_is_pipe(i))) {
1228 unsigned int p_mask = i->pipe->ring_size - 1;
1230 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
1231 return size | i->iov_offset;
1234 iterate_all_kinds(i, size, v,
1235 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
1236 res |= v.bv_offset | v.bv_len,
1237 res |= (unsigned long)v.iov_base | v.iov_len
1241 EXPORT_SYMBOL(iov_iter_alignment);
1243 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1245 unsigned long res = 0;
1246 size_t size = i->count;
1248 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1253 iterate_all_kinds(i, size, v,
1254 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1255 (size != v.iov_len ? size : 0), 0),
1256 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1257 (size != v.bv_len ? size : 0)),
1258 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1259 (size != v.iov_len ? size : 0))
1263 EXPORT_SYMBOL(iov_iter_gap_alignment);
1265 static inline ssize_t __pipe_get_pages(struct iov_iter *i,
1267 struct page **pages,
1271 struct pipe_inode_info *pipe = i->pipe;
1272 unsigned int p_mask = pipe->ring_size - 1;
1273 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
1280 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1288 static ssize_t pipe_get_pages(struct iov_iter *i,
1289 struct page **pages, size_t maxsize, unsigned maxpages,
1292 unsigned int iter_head, npages;
1301 data_start(i, &iter_head, start);
1302 /* Amount of free space: some of this one + all after this one */
1303 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1304 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
1306 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
1309 ssize_t iov_iter_get_pages(struct iov_iter *i,
1310 struct page **pages, size_t maxsize, unsigned maxpages,
1313 if (maxsize > i->count)
1316 if (unlikely(iov_iter_is_pipe(i)))
1317 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1318 if (unlikely(iov_iter_is_discard(i)))
1321 iterate_all_kinds(i, maxsize, v, ({
1322 unsigned long addr = (unsigned long)v.iov_base;
1323 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1327 if (len > maxpages * PAGE_SIZE)
1328 len = maxpages * PAGE_SIZE;
1329 addr &= ~(PAGE_SIZE - 1);
1330 n = DIV_ROUND_UP(len, PAGE_SIZE);
1331 res = get_user_pages_fast(addr, n,
1332 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1334 if (unlikely(res <= 0))
1336 return (res == n ? len : res * PAGE_SIZE) - *start;
1338 /* can't be more than PAGE_SIZE */
1339 *start = v.bv_offset;
1340 get_page(*pages = v.bv_page);
1348 EXPORT_SYMBOL(iov_iter_get_pages);
1350 static struct page **get_pages_array(size_t n)
1352 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
1355 static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1356 struct page ***pages, size_t maxsize,
1360 unsigned int iter_head, npages;
1369 data_start(i, &iter_head, start);
1370 /* Amount of free space: some of this one + all after this one */
1371 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1372 n = npages * PAGE_SIZE - *start;
1376 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1377 p = get_pages_array(npages);
1380 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
1388 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1389 struct page ***pages, size_t maxsize,
1394 if (maxsize > i->count)
1397 if (unlikely(iov_iter_is_pipe(i)))
1398 return pipe_get_pages_alloc(i, pages, maxsize, start);
1399 if (unlikely(iov_iter_is_discard(i)))
1402 iterate_all_kinds(i, maxsize, v, ({
1403 unsigned long addr = (unsigned long)v.iov_base;
1404 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1408 addr &= ~(PAGE_SIZE - 1);
1409 n = DIV_ROUND_UP(len, PAGE_SIZE);
1410 p = get_pages_array(n);
1413 res = get_user_pages_fast(addr, n,
1414 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
1415 if (unlikely(res <= 0)) {
1421 return (res == n ? len : res * PAGE_SIZE) - *start;
1423 /* can't be more than PAGE_SIZE */
1424 *start = v.bv_offset;
1425 *pages = p = get_pages_array(1);
1428 get_page(*p = v.bv_page);
1436 EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1438 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1445 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1449 iterate_and_advance(i, bytes, v, ({
1450 next = csum_and_copy_from_user(v.iov_base,
1451 (to += v.iov_len) - v.iov_len,
1454 sum = csum_block_add(sum, next, off);
1457 next ? 0 : v.iov_len;
1459 char *p = kmap_atomic(v.bv_page);
1460 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1461 p + v.bv_offset, v.bv_len,
1466 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1467 v.iov_base, v.iov_len,
1475 EXPORT_SYMBOL(csum_and_copy_from_iter);
1477 bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1484 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1488 if (unlikely(i->count < bytes))
1490 iterate_all_kinds(i, bytes, v, ({
1491 next = csum_and_copy_from_user(v.iov_base,
1492 (to += v.iov_len) - v.iov_len,
1496 sum = csum_block_add(sum, next, off);
1500 char *p = kmap_atomic(v.bv_page);
1501 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1502 p + v.bv_offset, v.bv_len,
1507 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1508 v.iov_base, v.iov_len,
1514 iov_iter_advance(i, bytes);
1517 EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1519 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1522 struct csum_state *csstate = _csstate;
1523 const char *from = addr;
1527 if (unlikely(iov_iter_is_pipe(i)))
1528 return csum_and_copy_to_pipe_iter(addr, bytes, _csstate, i);
1530 sum = csstate->csum;
1532 if (unlikely(iov_iter_is_discard(i))) {
1533 WARN_ON(1); /* for now */
1536 iterate_and_advance(i, bytes, v, ({
1537 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
1541 sum = csum_block_add(sum, next, off);
1544 next ? 0 : v.iov_len;
1546 char *p = kmap_atomic(v.bv_page);
1547 sum = csum_and_memcpy(p + v.bv_offset,
1548 (from += v.bv_len) - v.bv_len,
1549 v.bv_len, sum, off);
1553 sum = csum_and_memcpy(v.iov_base,
1554 (from += v.iov_len) - v.iov_len,
1555 v.iov_len, sum, off);
1559 csstate->csum = sum;
1563 EXPORT_SYMBOL(csum_and_copy_to_iter);
1565 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1568 #ifdef CONFIG_CRYPTO_HASH
1569 struct ahash_request *hash = hashp;
1570 struct scatterlist sg;
1573 copied = copy_to_iter(addr, bytes, i);
1574 sg_init_one(&sg, addr, copied);
1575 ahash_request_set_crypt(hash, &sg, NULL, copied);
1576 crypto_ahash_update(hash);
1582 EXPORT_SYMBOL(hash_and_copy_to_iter);
1584 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1586 size_t size = i->count;
1591 if (unlikely(iov_iter_is_discard(i)))
1594 if (unlikely(iov_iter_is_pipe(i))) {
1595 struct pipe_inode_info *pipe = i->pipe;
1596 unsigned int iter_head;
1602 data_start(i, &iter_head, &off);
1603 /* some of this one + all after this one */
1604 npages = pipe_space_for_user(iter_head, pipe->tail, pipe);
1605 if (npages >= maxpages)
1607 } else iterate_all_kinds(i, size, v, ({
1608 unsigned long p = (unsigned long)v.iov_base;
1609 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1611 if (npages >= maxpages)
1615 if (npages >= maxpages)
1618 unsigned long p = (unsigned long)v.iov_base;
1619 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1621 if (npages >= maxpages)
1627 EXPORT_SYMBOL(iov_iter_npages);
1629 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1632 if (unlikely(iov_iter_is_pipe(new))) {
1636 if (unlikely(iov_iter_is_discard(new)))
1638 if (iov_iter_is_bvec(new))
1639 return new->bvec = kmemdup(new->bvec,
1640 new->nr_segs * sizeof(struct bio_vec),
1643 /* iovec and kvec have identical layout */
1644 return new->iov = kmemdup(new->iov,
1645 new->nr_segs * sizeof(struct iovec),
1648 EXPORT_SYMBOL(dup_iter);
1650 static int copy_compat_iovec_from_user(struct iovec *iov,
1651 const struct iovec __user *uvec, unsigned long nr_segs)
1653 const struct compat_iovec __user *uiov =
1654 (const struct compat_iovec __user *)uvec;
1655 int ret = -EFAULT, i;
1657 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1660 for (i = 0; i < nr_segs; i++) {
1664 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1665 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1667 /* check for compat_size_t not fitting in compat_ssize_t .. */
1672 iov[i].iov_base = compat_ptr(buf);
1673 iov[i].iov_len = len;
1682 static int copy_iovec_from_user(struct iovec *iov,
1683 const struct iovec __user *uvec, unsigned long nr_segs)
1687 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1689 for (seg = 0; seg < nr_segs; seg++) {
1690 if ((ssize_t)iov[seg].iov_len < 0)
1697 struct iovec *iovec_from_user(const struct iovec __user *uvec,
1698 unsigned long nr_segs, unsigned long fast_segs,
1699 struct iovec *fast_iov, bool compat)
1701 struct iovec *iov = fast_iov;
1705 * SuS says "The readv() function *may* fail if the iovcnt argument was
1706 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
1707 * traditionally returned zero for zero segments, so...
1711 if (nr_segs > UIO_MAXIOV)
1712 return ERR_PTR(-EINVAL);
1713 if (nr_segs > fast_segs) {
1714 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1716 return ERR_PTR(-ENOMEM);
1720 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1722 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1724 if (iov != fast_iov)
1726 return ERR_PTR(ret);
1732 ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1733 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1734 struct iov_iter *i, bool compat)
1736 ssize_t total_len = 0;
1740 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1743 return PTR_ERR(iov);
1747 * According to the Single Unix Specification we should return EINVAL if
1748 * an element length is < 0 when cast to ssize_t or if the total length
1749 * would overflow the ssize_t return value of the system call.
1751 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1754 for (seg = 0; seg < nr_segs; seg++) {
1755 ssize_t len = (ssize_t)iov[seg].iov_len;
1757 if (!access_ok(iov[seg].iov_base, len)) {
1764 if (len > MAX_RW_COUNT - total_len) {
1765 len = MAX_RW_COUNT - total_len;
1766 iov[seg].iov_len = len;
1771 iov_iter_init(i, type, iov, nr_segs, total_len);
1780 * import_iovec() - Copy an array of &struct iovec from userspace
1781 * into the kernel, check that it is valid, and initialize a new
1782 * &struct iov_iter iterator to access it.
1784 * @type: One of %READ or %WRITE.
1785 * @uvec: Pointer to the userspace array.
1786 * @nr_segs: Number of elements in userspace array.
1787 * @fast_segs: Number of elements in @iov.
1788 * @iovp: (input and output parameter) Pointer to pointer to (usually small
1789 * on-stack) kernel array.
1790 * @i: Pointer to iterator that will be initialized on success.
1792 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1793 * then this function places %NULL in *@iov on return. Otherwise, a new
1794 * array will be allocated and the result placed in *@iov. This means that
1795 * the caller may call kfree() on *@iov regardless of whether the small
1796 * on-stack array was used or not (and regardless of whether this function
1797 * returns an error or not).
1799 * Return: Negative error code on error, bytes imported on success
1801 ssize_t import_iovec(int type, const struct iovec __user *uvec,
1802 unsigned nr_segs, unsigned fast_segs,
1803 struct iovec **iovp, struct iov_iter *i)
1805 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1806 in_compat_syscall());
1808 EXPORT_SYMBOL(import_iovec);
1810 int import_single_range(int rw, void __user *buf, size_t len,
1811 struct iovec *iov, struct iov_iter *i)
1813 if (len > MAX_RW_COUNT)
1815 if (unlikely(!access_ok(buf, len)))
1818 iov->iov_base = buf;
1820 iov_iter_init(i, rw, iov, 1, len);
1823 EXPORT_SYMBOL(import_single_range);
1826 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
1827 * iov_iter_save_state() was called.
1829 * @i: &struct iov_iter to restore
1830 * @state: state to restore from
1832 * Used after iov_iter_save_state() to bring restore @i, if operations may
1835 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
1837 void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
1839 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
1840 !iov_iter_is_kvec(i))
1842 i->iov_offset = state->iov_offset;
1843 i->count = state->count;
1845 * For the *vec iters, nr_segs + iov is constant - if we increment
1846 * the vec, then we also decrement the nr_segs count. Hence we don't
1847 * need to track both of these, just one is enough and we can deduct
1848 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
1849 * size, so we can just increment the iov pointer as they are unionzed.
1850 * ITER_BVEC _may_ be the same size on some archs, but on others it is
1851 * not. Be safe and handle it separately.
1853 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
1854 if (iov_iter_is_bvec(i))
1855 i->bvec -= state->nr_segs - i->nr_segs;
1857 i->iov -= state->nr_segs - i->nr_segs;
1858 i->nr_segs = state->nr_segs;