1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <crypto/hash.h>
3 #include <linux/export.h>
4 #include <linux/bvec.h>
5 #include <linux/fault-inject-usercopy.h>
7 #include <linux/pagemap.h>
8 #include <linux/highmem.h>
9 #include <linux/slab.h>
10 #include <linux/vmalloc.h>
11 #include <linux/splice.h>
12 #include <linux/compat.h>
13 #include <net/checksum.h>
14 #include <linux/scatterlist.h>
15 #include <linux/instrumented.h>
17 #define PIPE_PARANOIA /* for now */
19 /* covers ubuf and kbuf alike */
20 #define iterate_buf(i, n, base, len, off, __p, STEP) { \
21 size_t __maybe_unused off = 0; \
23 base = __p + i->iov_offset; \
25 i->iov_offset += len; \
29 /* covers iovec and kvec alike */
30 #define iterate_iovec(i, n, base, len, off, __p, STEP) { \
32 size_t skip = i->iov_offset; \
34 len = min(n, __p->iov_len - skip); \
36 base = __p->iov_base + skip; \
41 if (skip < __p->iov_len) \
47 i->iov_offset = skip; \
51 #define iterate_bvec(i, n, base, len, off, p, STEP) { \
53 unsigned skip = i->iov_offset; \
55 unsigned offset = p->bv_offset + skip; \
57 void *kaddr = kmap_local_page(p->bv_page + \
58 offset / PAGE_SIZE); \
59 base = kaddr + offset % PAGE_SIZE; \
60 len = min(min(n, (size_t)(p->bv_len - skip)), \
61 (size_t)(PAGE_SIZE - offset % PAGE_SIZE)); \
63 kunmap_local(kaddr); \
67 if (skip == p->bv_len) { \
75 i->iov_offset = skip; \
79 #define iterate_xarray(i, n, base, len, __off, STEP) { \
82 struct folio *folio; \
83 loff_t start = i->xarray_start + i->iov_offset; \
84 pgoff_t index = start / PAGE_SIZE; \
85 XA_STATE(xas, i->xarray, index); \
87 len = PAGE_SIZE - offset_in_page(start); \
89 xas_for_each(&xas, folio, ULONG_MAX) { \
92 if (xas_retry(&xas, folio)) \
94 if (WARN_ON(xa_is_value(folio))) \
96 if (WARN_ON(folio_test_hugetlb(folio))) \
98 offset = offset_in_folio(folio, start + __off); \
99 while (offset < folio_size(folio)) { \
100 base = kmap_local_folio(folio, offset); \
103 kunmap_local(base); \
107 if (left || n == 0) \
115 i->iov_offset += __off; \
119 #define __iterate_and_advance(i, n, base, len, off, I, K) { \
120 if (unlikely(i->count < n)) \
123 if (likely(iter_is_ubuf(i))) { \
126 iterate_buf(i, n, base, len, off, \
128 } else if (likely(iter_is_iovec(i))) { \
129 const struct iovec *iov = i->iov; \
132 iterate_iovec(i, n, base, len, off, \
134 i->nr_segs -= iov - i->iov; \
136 } else if (iov_iter_is_bvec(i)) { \
137 const struct bio_vec *bvec = i->bvec; \
140 iterate_bvec(i, n, base, len, off, \
142 i->nr_segs -= bvec - i->bvec; \
144 } else if (iov_iter_is_kvec(i)) { \
145 const struct kvec *kvec = i->kvec; \
148 iterate_iovec(i, n, base, len, off, \
150 i->nr_segs -= kvec - i->kvec; \
152 } else if (iov_iter_is_xarray(i)) { \
155 iterate_xarray(i, n, base, len, off, \
161 #define iterate_and_advance(i, n, base, len, off, I, K) \
162 __iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
164 static int copyout(void __user *to, const void *from, size_t n)
166 if (should_fail_usercopy())
168 if (access_ok(to, n)) {
169 instrument_copy_to_user(to, from, n);
170 n = raw_copy_to_user(to, from, n);
175 static int copyin(void *to, const void __user *from, size_t n)
179 if (should_fail_usercopy())
181 if (access_ok(from, n)) {
182 instrument_copy_from_user_before(to, from, n);
183 res = raw_copy_from_user(to, from, n);
184 instrument_copy_from_user_after(to, from, n, res);
189 static inline struct pipe_buffer *pipe_buf(const struct pipe_inode_info *pipe,
192 return &pipe->bufs[slot & (pipe->ring_size - 1)];
196 static bool sanity(const struct iov_iter *i)
198 struct pipe_inode_info *pipe = i->pipe;
199 unsigned int p_head = pipe->head;
200 unsigned int p_tail = pipe->tail;
201 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
202 unsigned int i_head = i->head;
205 if (i->last_offset) {
206 struct pipe_buffer *p;
207 if (unlikely(p_occupancy == 0))
208 goto Bad; // pipe must be non-empty
209 if (unlikely(i_head != p_head - 1))
210 goto Bad; // must be at the last buffer...
212 p = pipe_buf(pipe, i_head);
213 if (unlikely(p->offset + p->len != abs(i->last_offset)))
214 goto Bad; // ... at the end of segment
216 if (i_head != p_head)
217 goto Bad; // must be right after the last buffer
221 printk(KERN_ERR "idx = %d, offset = %d\n", i_head, i->last_offset);
222 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
223 p_head, p_tail, pipe->ring_size);
224 for (idx = 0; idx < pipe->ring_size; idx++)
225 printk(KERN_ERR "[%p %p %d %d]\n",
227 pipe->bufs[idx].page,
228 pipe->bufs[idx].offset,
229 pipe->bufs[idx].len);
234 #define sanity(i) true
237 static struct page *push_anon(struct pipe_inode_info *pipe, unsigned size)
239 struct page *page = alloc_page(GFP_USER);
241 struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
242 *buf = (struct pipe_buffer) {
243 .ops = &default_pipe_buf_ops,
252 static void push_page(struct pipe_inode_info *pipe, struct page *page,
253 unsigned int offset, unsigned int size)
255 struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
256 *buf = (struct pipe_buffer) {
257 .ops = &page_cache_pipe_buf_ops,
265 static inline int last_offset(const struct pipe_buffer *buf)
267 if (buf->ops == &default_pipe_buf_ops)
268 return buf->len; // buf->offset is 0 for those
270 return -(buf->offset + buf->len);
273 static struct page *append_pipe(struct iov_iter *i, size_t size,
276 struct pipe_inode_info *pipe = i->pipe;
277 int offset = i->last_offset;
278 struct pipe_buffer *buf;
281 if (offset > 0 && offset < PAGE_SIZE) {
282 // some space in the last buffer; add to it
283 buf = pipe_buf(pipe, pipe->head - 1);
284 size = min_t(size_t, size, PAGE_SIZE - offset);
286 i->last_offset += size;
291 // OK, we need a new buffer
293 size = min_t(size_t, size, PAGE_SIZE);
294 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
296 page = push_anon(pipe, size);
299 i->head = pipe->head - 1;
300 i->last_offset = size;
305 static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
308 struct pipe_inode_info *pipe = i->pipe;
309 unsigned int head = pipe->head;
311 if (unlikely(bytes > i->count))
314 if (unlikely(!bytes))
320 if (offset && i->last_offset == -offset) { // could we merge it?
321 struct pipe_buffer *buf = pipe_buf(pipe, head - 1);
322 if (buf->page == page) {
324 i->last_offset -= bytes;
329 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
332 push_page(pipe, page, offset, bytes);
333 i->last_offset = -(offset + bytes);
340 * fault_in_iov_iter_readable - fault in iov iterator for reading
342 * @size: maximum length
344 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
345 * @size. For each iovec, fault in each page that constitutes the iovec.
347 * Returns the number of bytes not faulted in (like copy_to_user() and
350 * Always returns 0 for non-userspace iterators.
352 size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
354 if (iter_is_ubuf(i)) {
355 size_t n = min(size, iov_iter_count(i));
356 n -= fault_in_readable(i->ubuf + i->iov_offset, n);
358 } else if (iter_is_iovec(i)) {
359 size_t count = min(size, iov_iter_count(i));
360 const struct iovec *p;
364 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
365 size_t len = min(count, p->iov_len - skip);
370 ret = fault_in_readable(p->iov_base + skip, len);
379 EXPORT_SYMBOL(fault_in_iov_iter_readable);
382 * fault_in_iov_iter_writeable - fault in iov iterator for writing
384 * @size: maximum length
386 * Faults in the iterator using get_user_pages(), i.e., without triggering
387 * hardware page faults. This is primarily useful when we already know that
388 * some or all of the pages in @i aren't in memory.
390 * Returns the number of bytes not faulted in, like copy_to_user() and
393 * Always returns 0 for non-user-space iterators.
395 size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
397 if (iter_is_ubuf(i)) {
398 size_t n = min(size, iov_iter_count(i));
399 n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n);
401 } else if (iter_is_iovec(i)) {
402 size_t count = min(size, iov_iter_count(i));
403 const struct iovec *p;
407 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
408 size_t len = min(count, p->iov_len - skip);
413 ret = fault_in_safe_writeable(p->iov_base + skip, len);
422 EXPORT_SYMBOL(fault_in_iov_iter_writeable);
424 void iov_iter_init(struct iov_iter *i, unsigned int direction,
425 const struct iovec *iov, unsigned long nr_segs,
428 WARN_ON(direction & ~(READ | WRITE));
429 *i = (struct iov_iter) {
430 .iter_type = ITER_IOVEC,
433 .data_source = direction,
440 EXPORT_SYMBOL(iov_iter_init);
442 // returns the offset in partial buffer (if any)
443 static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages)
445 struct pipe_inode_info *pipe = i->pipe;
446 int used = pipe->head - pipe->tail;
447 int off = i->last_offset;
449 *npages = max((int)pipe->max_usage - used, 0);
451 if (off > 0 && off < PAGE_SIZE) { // anon and not full
458 static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
461 unsigned int off, chunk;
463 if (unlikely(bytes > i->count))
465 if (unlikely(!bytes))
471 for (size_t n = bytes; n; n -= chunk) {
472 struct page *page = append_pipe(i, n, &off);
473 chunk = min_t(size_t, n, PAGE_SIZE - off);
476 memcpy_to_page(page, off, addr, chunk);
482 static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
483 __wsum sum, size_t off)
485 __wsum next = csum_partial_copy_nocheck(from, to, len);
486 return csum_block_add(sum, next, off);
489 static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
490 struct iov_iter *i, __wsum *sump)
494 unsigned int chunk, r;
496 if (unlikely(bytes > i->count))
498 if (unlikely(!bytes))
505 struct page *page = append_pipe(i, bytes, &r);
510 chunk = min_t(size_t, bytes, PAGE_SIZE - r);
511 p = kmap_local_page(page);
512 sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
521 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
523 if (unlikely(iov_iter_is_pipe(i)))
524 return copy_pipe_to_iter(addr, bytes, i);
525 if (user_backed_iter(i))
527 iterate_and_advance(i, bytes, base, len, off,
528 copyout(base, addr + off, len),
529 memcpy(base, addr + off, len)
534 EXPORT_SYMBOL(_copy_to_iter);
536 #ifdef CONFIG_ARCH_HAS_COPY_MC
537 static int copyout_mc(void __user *to, const void *from, size_t n)
539 if (access_ok(to, n)) {
540 instrument_copy_to_user(to, from, n);
541 n = copy_mc_to_user((__force void *) to, from, n);
546 static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
550 unsigned int off, chunk;
552 if (unlikely(bytes > i->count))
554 if (unlikely(!bytes))
561 struct page *page = append_pipe(i, bytes, &off);
567 chunk = min_t(size_t, bytes, PAGE_SIZE - off);
568 p = kmap_local_page(page);
569 rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
575 iov_iter_revert(i, rem);
583 * _copy_mc_to_iter - copy to iter with source memory error exception handling
584 * @addr: source kernel address
585 * @bytes: total transfer length
586 * @i: destination iterator
588 * The pmem driver deploys this for the dax operation
589 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
590 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
591 * successfully copied.
593 * The main differences between this and typical _copy_to_iter().
595 * * Typical tail/residue handling after a fault retries the copy
596 * byte-by-byte until the fault happens again. Re-triggering machine
597 * checks is potentially fatal so the implementation uses source
598 * alignment and poison alignment assumptions to avoid re-triggering
599 * hardware exceptions.
601 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
602 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
605 * Return: number of bytes copied (may be %0)
607 size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
609 if (unlikely(iov_iter_is_pipe(i)))
610 return copy_mc_pipe_to_iter(addr, bytes, i);
611 if (user_backed_iter(i))
613 __iterate_and_advance(i, bytes, base, len, off,
614 copyout_mc(base, addr + off, len),
615 copy_mc_to_kernel(base, addr + off, len)
620 EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
621 #endif /* CONFIG_ARCH_HAS_COPY_MC */
623 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
625 if (unlikely(iov_iter_is_pipe(i))) {
629 if (user_backed_iter(i))
631 iterate_and_advance(i, bytes, base, len, off,
632 copyin(addr + off, base, len),
633 memcpy(addr + off, base, len)
638 EXPORT_SYMBOL(_copy_from_iter);
640 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
642 if (unlikely(iov_iter_is_pipe(i))) {
646 iterate_and_advance(i, bytes, base, len, off,
647 __copy_from_user_inatomic_nocache(addr + off, base, len),
648 memcpy(addr + off, base, len)
653 EXPORT_SYMBOL(_copy_from_iter_nocache);
655 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
657 * _copy_from_iter_flushcache - write destination through cpu cache
658 * @addr: destination kernel address
659 * @bytes: total transfer length
660 * @i: source iterator
662 * The pmem driver arranges for filesystem-dax to use this facility via
663 * dax_copy_from_iter() for ensuring that writes to persistent memory
664 * are flushed through the CPU cache. It is differentiated from
665 * _copy_from_iter_nocache() in that guarantees all data is flushed for
666 * all iterator types. The _copy_from_iter_nocache() only attempts to
667 * bypass the cache for the ITER_IOVEC case, and on some archs may use
668 * instructions that strand dirty-data in the cache.
670 * Return: number of bytes copied (may be %0)
672 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
674 if (unlikely(iov_iter_is_pipe(i))) {
678 iterate_and_advance(i, bytes, base, len, off,
679 __copy_from_user_flushcache(addr + off, base, len),
680 memcpy_flushcache(addr + off, base, len)
685 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
688 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
691 size_t v = n + offset;
694 * The general case needs to access the page order in order
695 * to compute the page size.
696 * However, we mostly deal with order-0 pages and thus can
697 * avoid a possible cache line miss for requests that fit all
700 if (n <= v && v <= PAGE_SIZE)
703 head = compound_head(page);
704 v += (page - head) << PAGE_SHIFT;
706 if (likely(n <= v && v <= (page_size(head))))
712 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
716 if (unlikely(!page_copy_sane(page, offset, bytes)))
718 if (unlikely(iov_iter_is_pipe(i)))
719 return copy_page_to_iter_pipe(page, offset, bytes, i);
720 page += offset / PAGE_SIZE; // first subpage
723 void *kaddr = kmap_local_page(page);
724 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
725 n = _copy_to_iter(kaddr + offset, n, i);
732 if (offset == PAGE_SIZE) {
739 EXPORT_SYMBOL(copy_page_to_iter);
741 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
745 if (!page_copy_sane(page, offset, bytes))
747 page += offset / PAGE_SIZE; // first subpage
750 void *kaddr = kmap_local_page(page);
751 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
752 n = _copy_from_iter(kaddr + offset, n, i);
759 if (offset == PAGE_SIZE) {
766 EXPORT_SYMBOL(copy_page_from_iter);
768 static size_t pipe_zero(size_t bytes, struct iov_iter *i)
770 unsigned int chunk, off;
772 if (unlikely(bytes > i->count))
774 if (unlikely(!bytes))
780 for (size_t n = bytes; n; n -= chunk) {
781 struct page *page = append_pipe(i, n, &off);
786 chunk = min_t(size_t, n, PAGE_SIZE - off);
787 p = kmap_local_page(page);
788 memset(p + off, 0, chunk);
794 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
796 if (unlikely(iov_iter_is_pipe(i)))
797 return pipe_zero(bytes, i);
798 iterate_and_advance(i, bytes, base, len, count,
799 clear_user(base, len),
805 EXPORT_SYMBOL(iov_iter_zero);
807 size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
810 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
811 if (unlikely(!page_copy_sane(page, offset, bytes))) {
812 kunmap_atomic(kaddr);
815 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
816 kunmap_atomic(kaddr);
820 iterate_and_advance(i, bytes, base, len, off,
821 copyin(p + off, base, len),
822 memcpy(p + off, base, len)
824 kunmap_atomic(kaddr);
827 EXPORT_SYMBOL(copy_page_from_iter_atomic);
829 static void pipe_advance(struct iov_iter *i, size_t size)
831 struct pipe_inode_info *pipe = i->pipe;
832 int off = i->last_offset;
835 pipe_discard_from(pipe, i->start_head); // discard everything
840 struct pipe_buffer *buf = pipe_buf(pipe, i->head);
841 if (off) /* make it relative to the beginning of buffer */
842 size += abs(off) - buf->offset;
843 if (size <= buf->len) {
845 i->last_offset = last_offset(buf);
852 pipe_discard_from(pipe, i->head + 1); // discard everything past this one
855 static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
857 const struct bio_vec *bvec, *end;
863 size += i->iov_offset;
865 for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
866 if (likely(size < bvec->bv_len))
868 size -= bvec->bv_len;
870 i->iov_offset = size;
871 i->nr_segs -= bvec - i->bvec;
875 static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
877 const struct iovec *iov, *end;
883 size += i->iov_offset; // from beginning of current segment
884 for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
885 if (likely(size < iov->iov_len))
887 size -= iov->iov_len;
889 i->iov_offset = size;
890 i->nr_segs -= iov - i->iov;
894 void iov_iter_advance(struct iov_iter *i, size_t size)
896 if (unlikely(i->count < size))
898 if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
899 i->iov_offset += size;
901 } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
902 /* iovec and kvec have identical layouts */
903 iov_iter_iovec_advance(i, size);
904 } else if (iov_iter_is_bvec(i)) {
905 iov_iter_bvec_advance(i, size);
906 } else if (iov_iter_is_pipe(i)) {
907 pipe_advance(i, size);
908 } else if (iov_iter_is_discard(i)) {
912 EXPORT_SYMBOL(iov_iter_advance);
914 void iov_iter_revert(struct iov_iter *i, size_t unroll)
918 if (WARN_ON(unroll > MAX_RW_COUNT))
921 if (unlikely(iov_iter_is_pipe(i))) {
922 struct pipe_inode_info *pipe = i->pipe;
923 unsigned int head = pipe->head;
925 while (head > i->start_head) {
926 struct pipe_buffer *b = pipe_buf(pipe, --head);
927 if (unroll < b->len) {
929 i->last_offset = last_offset(b);
934 pipe_buf_release(pipe, b);
941 if (unlikely(iov_iter_is_discard(i)))
943 if (unroll <= i->iov_offset) {
944 i->iov_offset -= unroll;
947 unroll -= i->iov_offset;
948 if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
949 BUG(); /* We should never go beyond the start of the specified
950 * range since we might then be straying into pages that
953 } else if (iov_iter_is_bvec(i)) {
954 const struct bio_vec *bvec = i->bvec;
956 size_t n = (--bvec)->bv_len;
960 i->iov_offset = n - unroll;
965 } else { /* same logics for iovec and kvec */
966 const struct iovec *iov = i->iov;
968 size_t n = (--iov)->iov_len;
972 i->iov_offset = n - unroll;
979 EXPORT_SYMBOL(iov_iter_revert);
982 * Return the count of just the current iov_iter segment.
984 size_t iov_iter_single_seg_count(const struct iov_iter *i)
986 if (i->nr_segs > 1) {
987 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
988 return min(i->count, i->iov->iov_len - i->iov_offset);
989 if (iov_iter_is_bvec(i))
990 return min(i->count, i->bvec->bv_len - i->iov_offset);
994 EXPORT_SYMBOL(iov_iter_single_seg_count);
996 void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
997 const struct kvec *kvec, unsigned long nr_segs,
1000 WARN_ON(direction & ~(READ | WRITE));
1001 *i = (struct iov_iter){
1002 .iter_type = ITER_KVEC,
1003 .data_source = direction,
1010 EXPORT_SYMBOL(iov_iter_kvec);
1012 void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1013 const struct bio_vec *bvec, unsigned long nr_segs,
1016 WARN_ON(direction & ~(READ | WRITE));
1017 *i = (struct iov_iter){
1018 .iter_type = ITER_BVEC,
1019 .data_source = direction,
1026 EXPORT_SYMBOL(iov_iter_bvec);
1028 void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1029 struct pipe_inode_info *pipe,
1032 BUG_ON(direction != READ);
1033 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
1034 *i = (struct iov_iter){
1035 .iter_type = ITER_PIPE,
1036 .data_source = false,
1039 .start_head = pipe->head,
1044 EXPORT_SYMBOL(iov_iter_pipe);
1047 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1048 * @i: The iterator to initialise.
1049 * @direction: The direction of the transfer.
1050 * @xarray: The xarray to access.
1051 * @start: The start file position.
1052 * @count: The size of the I/O buffer in bytes.
1054 * Set up an I/O iterator to either draw data out of the pages attached to an
1055 * inode or to inject data into those pages. The pages *must* be prevented
1056 * from evaporation, either by taking a ref on them or locking them by the
1059 void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1060 struct xarray *xarray, loff_t start, size_t count)
1062 BUG_ON(direction & ~1);
1063 *i = (struct iov_iter) {
1064 .iter_type = ITER_XARRAY,
1065 .data_source = direction,
1067 .xarray_start = start,
1072 EXPORT_SYMBOL(iov_iter_xarray);
1075 * iov_iter_discard - Initialise an I/O iterator that discards data
1076 * @i: The iterator to initialise.
1077 * @direction: The direction of the transfer.
1078 * @count: The size of the I/O buffer in bytes.
1080 * Set up an I/O iterator that just discards everything that's written to it.
1081 * It's only available as a READ iterator.
1083 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1085 BUG_ON(direction != READ);
1086 *i = (struct iov_iter){
1087 .iter_type = ITER_DISCARD,
1088 .data_source = false,
1093 EXPORT_SYMBOL(iov_iter_discard);
1095 static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
1098 size_t size = i->count;
1099 size_t skip = i->iov_offset;
1102 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1103 size_t len = i->iov[k].iov_len - skip;
1109 if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask)
1119 static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
1122 size_t size = i->count;
1123 unsigned skip = i->iov_offset;
1126 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1127 size_t len = i->bvec[k].bv_len - skip;
1133 if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
1144 * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
1145 * are aligned to the parameters.
1147 * @i: &struct iov_iter to restore
1148 * @addr_mask: bit mask to check against the iov element's addresses
1149 * @len_mask: bit mask to check against the iov element's lengths
1151 * Return: false if any addresses or lengths intersect with the provided masks
1153 bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
1156 if (likely(iter_is_ubuf(i))) {
1157 if (i->count & len_mask)
1159 if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
1164 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1165 return iov_iter_aligned_iovec(i, addr_mask, len_mask);
1167 if (iov_iter_is_bvec(i))
1168 return iov_iter_aligned_bvec(i, addr_mask, len_mask);
1170 if (iov_iter_is_pipe(i)) {
1171 size_t size = i->count;
1173 if (size & len_mask)
1175 if (size && i->last_offset > 0) {
1176 if (i->last_offset & addr_mask)
1183 if (iov_iter_is_xarray(i)) {
1184 if (i->count & len_mask)
1186 if ((i->xarray_start + i->iov_offset) & addr_mask)
1192 EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
1194 static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1196 unsigned long res = 0;
1197 size_t size = i->count;
1198 size_t skip = i->iov_offset;
1201 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1202 size_t len = i->iov[k].iov_len - skip;
1204 res |= (unsigned long)i->iov[k].iov_base + skip;
1216 static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
1219 size_t size = i->count;
1220 unsigned skip = i->iov_offset;
1223 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1224 size_t len = i->bvec[k].bv_len - skip;
1225 res |= (unsigned long)i->bvec[k].bv_offset + skip;
1236 unsigned long iov_iter_alignment(const struct iov_iter *i)
1238 if (likely(iter_is_ubuf(i))) {
1239 size_t size = i->count;
1241 return ((unsigned long)i->ubuf + i->iov_offset) | size;
1245 /* iovec and kvec have identical layouts */
1246 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1247 return iov_iter_alignment_iovec(i);
1249 if (iov_iter_is_bvec(i))
1250 return iov_iter_alignment_bvec(i);
1252 if (iov_iter_is_pipe(i)) {
1253 size_t size = i->count;
1255 if (size && i->last_offset > 0)
1256 return size | i->last_offset;
1260 if (iov_iter_is_xarray(i))
1261 return (i->xarray_start + i->iov_offset) | i->count;
1265 EXPORT_SYMBOL(iov_iter_alignment);
1267 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1269 unsigned long res = 0;
1270 unsigned long v = 0;
1271 size_t size = i->count;
1274 if (iter_is_ubuf(i))
1277 if (WARN_ON(!iter_is_iovec(i)))
1280 for (k = 0; k < i->nr_segs; k++) {
1281 if (i->iov[k].iov_len) {
1282 unsigned long base = (unsigned long)i->iov[k].iov_base;
1283 if (v) // if not the first one
1284 res |= base | v; // this start | previous end
1285 v = base + i->iov[k].iov_len;
1286 if (size <= i->iov[k].iov_len)
1288 size -= i->iov[k].iov_len;
1293 EXPORT_SYMBOL(iov_iter_gap_alignment);
1295 static int want_pages_array(struct page ***res, size_t size,
1296 size_t start, unsigned int maxpages)
1298 unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE);
1300 if (count > maxpages)
1302 WARN_ON(!count); // caller should've prevented that
1304 *res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
1311 static ssize_t pipe_get_pages(struct iov_iter *i,
1312 struct page ***pages, size_t maxsize, unsigned maxpages,
1315 unsigned int npages, count, off, chunk;
1322 *start = off = pipe_npages(i, &npages);
1325 count = want_pages_array(pages, maxsize, off, min(npages, maxpages));
1329 for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) {
1330 struct page *page = append_pipe(i, left, &off);
1333 chunk = min_t(size_t, left, PAGE_SIZE - off);
1334 get_page(*p++ = page);
1338 return maxsize - left;
1341 static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1342 pgoff_t index, unsigned int nr_pages)
1344 XA_STATE(xas, xa, index);
1346 unsigned int ret = 0;
1349 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1350 if (xas_retry(&xas, page))
1353 /* Has the page moved or been split? */
1354 if (unlikely(page != xas_reload(&xas))) {
1359 pages[ret] = find_subpage(page, xas.xa_index);
1360 get_page(pages[ret]);
1361 if (++ret == nr_pages)
1368 static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1369 struct page ***pages, size_t maxsize,
1370 unsigned maxpages, size_t *_start_offset)
1372 unsigned nr, offset, count;
1376 pos = i->xarray_start + i->iov_offset;
1377 index = pos >> PAGE_SHIFT;
1378 offset = pos & ~PAGE_MASK;
1379 *_start_offset = offset;
1381 count = want_pages_array(pages, maxsize, offset, maxpages);
1384 nr = iter_xarray_populate_pages(*pages, i->xarray, index, count);
1388 maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1389 i->iov_offset += maxsize;
1390 i->count -= maxsize;
1394 /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
1395 static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
1400 if (iter_is_ubuf(i))
1401 return (unsigned long)i->ubuf + i->iov_offset;
1403 for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1404 size_t len = i->iov[k].iov_len - skip;
1410 return (unsigned long)i->iov[k].iov_base + skip;
1412 BUG(); // if it had been empty, we wouldn't get called
1415 /* must be done on non-empty ITER_BVEC one */
1416 static struct page *first_bvec_segment(const struct iov_iter *i,
1417 size_t *size, size_t *start)
1420 size_t skip = i->iov_offset, len;
1422 len = i->bvec->bv_len - skip;
1425 skip += i->bvec->bv_offset;
1426 page = i->bvec->bv_page + skip / PAGE_SIZE;
1427 *start = skip % PAGE_SIZE;
1431 static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
1432 struct page ***pages, size_t maxsize,
1433 unsigned int maxpages, size_t *start)
1437 if (maxsize > i->count)
1441 if (maxsize > MAX_RW_COUNT)
1442 maxsize = MAX_RW_COUNT;
1444 if (likely(user_backed_iter(i))) {
1445 unsigned int gup_flags = 0;
1449 if (iov_iter_rw(i) != WRITE)
1450 gup_flags |= FOLL_WRITE;
1452 gup_flags |= FOLL_NOFAULT;
1454 addr = first_iovec_segment(i, &maxsize);
1455 *start = addr % PAGE_SIZE;
1457 n = want_pages_array(pages, maxsize, *start, maxpages);
1460 res = get_user_pages_fast(addr, n, gup_flags, *pages);
1461 if (unlikely(res <= 0))
1463 maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1464 iov_iter_advance(i, maxsize);
1467 if (iov_iter_is_bvec(i)) {
1471 page = first_bvec_segment(i, &maxsize, start);
1472 n = want_pages_array(pages, maxsize, *start, maxpages);
1476 for (int k = 0; k < n; k++)
1477 get_page(p[k] = page + k);
1478 maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1479 i->count -= maxsize;
1480 i->iov_offset += maxsize;
1481 if (i->iov_offset == i->bvec->bv_len) {
1488 if (iov_iter_is_pipe(i))
1489 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1490 if (iov_iter_is_xarray(i))
1491 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1495 ssize_t iov_iter_get_pages2(struct iov_iter *i,
1496 struct page **pages, size_t maxsize, unsigned maxpages,
1503 return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, start);
1505 EXPORT_SYMBOL(iov_iter_get_pages2);
1507 ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i,
1508 struct page ***pages, size_t maxsize,
1515 len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start);
1522 EXPORT_SYMBOL(iov_iter_get_pages_alloc2);
1524 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1529 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1533 iterate_and_advance(i, bytes, base, len, off, ({
1534 next = csum_and_copy_from_user(base, addr + off, len);
1535 sum = csum_block_add(sum, next, off);
1538 sum = csum_and_memcpy(addr + off, base, len, sum, off);
1544 EXPORT_SYMBOL(csum_and_copy_from_iter);
1546 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1549 struct csum_state *csstate = _csstate;
1552 if (unlikely(iov_iter_is_discard(i))) {
1553 WARN_ON(1); /* for now */
1557 sum = csum_shift(csstate->csum, csstate->off);
1558 if (unlikely(iov_iter_is_pipe(i)))
1559 bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
1560 else iterate_and_advance(i, bytes, base, len, off, ({
1561 next = csum_and_copy_to_user(addr + off, base, len);
1562 sum = csum_block_add(sum, next, off);
1565 sum = csum_and_memcpy(base, addr + off, len, sum, off);
1568 csstate->csum = csum_shift(sum, csstate->off);
1569 csstate->off += bytes;
1572 EXPORT_SYMBOL(csum_and_copy_to_iter);
1574 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1577 #ifdef CONFIG_CRYPTO_HASH
1578 struct ahash_request *hash = hashp;
1579 struct scatterlist sg;
1582 copied = copy_to_iter(addr, bytes, i);
1583 sg_init_one(&sg, addr, copied);
1584 ahash_request_set_crypt(hash, &sg, NULL, copied);
1585 crypto_ahash_update(hash);
1591 EXPORT_SYMBOL(hash_and_copy_to_iter);
1593 static int iov_npages(const struct iov_iter *i, int maxpages)
1595 size_t skip = i->iov_offset, size = i->count;
1596 const struct iovec *p;
1599 for (p = i->iov; size; skip = 0, p++) {
1600 unsigned offs = offset_in_page(p->iov_base + skip);
1601 size_t len = min(p->iov_len - skip, size);
1605 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1606 if (unlikely(npages > maxpages))
1613 static int bvec_npages(const struct iov_iter *i, int maxpages)
1615 size_t skip = i->iov_offset, size = i->count;
1616 const struct bio_vec *p;
1619 for (p = i->bvec; size; skip = 0, p++) {
1620 unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
1621 size_t len = min(p->bv_len - skip, size);
1624 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1625 if (unlikely(npages > maxpages))
1631 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1633 if (unlikely(!i->count))
1635 if (likely(iter_is_ubuf(i))) {
1636 unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1637 int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1638 return min(npages, maxpages);
1640 /* iovec and kvec have identical layouts */
1641 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1642 return iov_npages(i, maxpages);
1643 if (iov_iter_is_bvec(i))
1644 return bvec_npages(i, maxpages);
1645 if (iov_iter_is_pipe(i)) {
1651 pipe_npages(i, &npages);
1652 return min(npages, maxpages);
1654 if (iov_iter_is_xarray(i)) {
1655 unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1656 int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
1657 return min(npages, maxpages);
1661 EXPORT_SYMBOL(iov_iter_npages);
1663 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1666 if (unlikely(iov_iter_is_pipe(new))) {
1670 if (iov_iter_is_bvec(new))
1671 return new->bvec = kmemdup(new->bvec,
1672 new->nr_segs * sizeof(struct bio_vec),
1674 else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1675 /* iovec and kvec have identical layout */
1676 return new->iov = kmemdup(new->iov,
1677 new->nr_segs * sizeof(struct iovec),
1681 EXPORT_SYMBOL(dup_iter);
1683 static int copy_compat_iovec_from_user(struct iovec *iov,
1684 const struct iovec __user *uvec, unsigned long nr_segs)
1686 const struct compat_iovec __user *uiov =
1687 (const struct compat_iovec __user *)uvec;
1688 int ret = -EFAULT, i;
1690 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1693 for (i = 0; i < nr_segs; i++) {
1697 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1698 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1700 /* check for compat_size_t not fitting in compat_ssize_t .. */
1705 iov[i].iov_base = compat_ptr(buf);
1706 iov[i].iov_len = len;
1715 static int copy_iovec_from_user(struct iovec *iov,
1716 const struct iovec __user *uvec, unsigned long nr_segs)
1720 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1722 for (seg = 0; seg < nr_segs; seg++) {
1723 if ((ssize_t)iov[seg].iov_len < 0)
1730 struct iovec *iovec_from_user(const struct iovec __user *uvec,
1731 unsigned long nr_segs, unsigned long fast_segs,
1732 struct iovec *fast_iov, bool compat)
1734 struct iovec *iov = fast_iov;
1738 * SuS says "The readv() function *may* fail if the iovcnt argument was
1739 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
1740 * traditionally returned zero for zero segments, so...
1744 if (nr_segs > UIO_MAXIOV)
1745 return ERR_PTR(-EINVAL);
1746 if (nr_segs > fast_segs) {
1747 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1749 return ERR_PTR(-ENOMEM);
1753 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1755 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1757 if (iov != fast_iov)
1759 return ERR_PTR(ret);
1765 ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1766 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1767 struct iov_iter *i, bool compat)
1769 ssize_t total_len = 0;
1773 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1776 return PTR_ERR(iov);
1780 * According to the Single Unix Specification we should return EINVAL if
1781 * an element length is < 0 when cast to ssize_t or if the total length
1782 * would overflow the ssize_t return value of the system call.
1784 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1787 for (seg = 0; seg < nr_segs; seg++) {
1788 ssize_t len = (ssize_t)iov[seg].iov_len;
1790 if (!access_ok(iov[seg].iov_base, len)) {
1797 if (len > MAX_RW_COUNT - total_len) {
1798 len = MAX_RW_COUNT - total_len;
1799 iov[seg].iov_len = len;
1804 iov_iter_init(i, type, iov, nr_segs, total_len);
1813 * import_iovec() - Copy an array of &struct iovec from userspace
1814 * into the kernel, check that it is valid, and initialize a new
1815 * &struct iov_iter iterator to access it.
1817 * @type: One of %READ or %WRITE.
1818 * @uvec: Pointer to the userspace array.
1819 * @nr_segs: Number of elements in userspace array.
1820 * @fast_segs: Number of elements in @iov.
1821 * @iovp: (input and output parameter) Pointer to pointer to (usually small
1822 * on-stack) kernel array.
1823 * @i: Pointer to iterator that will be initialized on success.
1825 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1826 * then this function places %NULL in *@iov on return. Otherwise, a new
1827 * array will be allocated and the result placed in *@iov. This means that
1828 * the caller may call kfree() on *@iov regardless of whether the small
1829 * on-stack array was used or not (and regardless of whether this function
1830 * returns an error or not).
1832 * Return: Negative error code on error, bytes imported on success
1834 ssize_t import_iovec(int type, const struct iovec __user *uvec,
1835 unsigned nr_segs, unsigned fast_segs,
1836 struct iovec **iovp, struct iov_iter *i)
1838 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1839 in_compat_syscall());
1841 EXPORT_SYMBOL(import_iovec);
1843 int import_single_range(int rw, void __user *buf, size_t len,
1844 struct iovec *iov, struct iov_iter *i)
1846 if (len > MAX_RW_COUNT)
1848 if (unlikely(!access_ok(buf, len)))
1851 iov->iov_base = buf;
1853 iov_iter_init(i, rw, iov, 1, len);
1856 EXPORT_SYMBOL(import_single_range);
1859 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
1860 * iov_iter_save_state() was called.
1862 * @i: &struct iov_iter to restore
1863 * @state: state to restore from
1865 * Used after iov_iter_save_state() to bring restore @i, if operations may
1868 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
1870 void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
1872 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
1873 !iov_iter_is_kvec(i) && !iter_is_ubuf(i))
1875 i->iov_offset = state->iov_offset;
1876 i->count = state->count;
1877 if (iter_is_ubuf(i))
1880 * For the *vec iters, nr_segs + iov is constant - if we increment
1881 * the vec, then we also decrement the nr_segs count. Hence we don't
1882 * need to track both of these, just one is enough and we can deduct
1883 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
1884 * size, so we can just increment the iov pointer as they are unionzed.
1885 * ITER_BVEC _may_ be the same size on some archs, but on others it is
1886 * not. Be safe and handle it separately.
1888 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
1889 if (iov_iter_is_bvec(i))
1890 i->bvec -= state->nr_segs - i->nr_segs;
1892 i->iov -= state->nr_segs - i->nr_segs;
1893 i->nr_segs = state->nr_segs;