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 (WARN_ON_ONCE(i->data_source))
525 if (unlikely(iov_iter_is_pipe(i)))
526 return copy_pipe_to_iter(addr, bytes, i);
527 if (user_backed_iter(i))
529 iterate_and_advance(i, bytes, base, len, off,
530 copyout(base, addr + off, len),
531 memcpy(base, addr + off, len)
536 EXPORT_SYMBOL(_copy_to_iter);
538 #ifdef CONFIG_ARCH_HAS_COPY_MC
539 static int copyout_mc(void __user *to, const void *from, size_t n)
541 if (access_ok(to, n)) {
542 instrument_copy_to_user(to, from, n);
543 n = copy_mc_to_user((__force void *) to, from, n);
548 static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
552 unsigned int off, chunk;
554 if (unlikely(bytes > i->count))
556 if (unlikely(!bytes))
563 struct page *page = append_pipe(i, bytes, &off);
569 chunk = min_t(size_t, bytes, PAGE_SIZE - off);
570 p = kmap_local_page(page);
571 rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
577 iov_iter_revert(i, rem);
585 * _copy_mc_to_iter - copy to iter with source memory error exception handling
586 * @addr: source kernel address
587 * @bytes: total transfer length
588 * @i: destination iterator
590 * The pmem driver deploys this for the dax operation
591 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
592 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
593 * successfully copied.
595 * The main differences between this and typical _copy_to_iter().
597 * * Typical tail/residue handling after a fault retries the copy
598 * byte-by-byte until the fault happens again. Re-triggering machine
599 * checks is potentially fatal so the implementation uses source
600 * alignment and poison alignment assumptions to avoid re-triggering
601 * hardware exceptions.
603 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
604 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
607 * Return: number of bytes copied (may be %0)
609 size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
611 if (WARN_ON_ONCE(i->data_source))
613 if (unlikely(iov_iter_is_pipe(i)))
614 return copy_mc_pipe_to_iter(addr, bytes, i);
615 if (user_backed_iter(i))
617 __iterate_and_advance(i, bytes, base, len, off,
618 copyout_mc(base, addr + off, len),
619 copy_mc_to_kernel(base, addr + off, len)
624 EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
625 #endif /* CONFIG_ARCH_HAS_COPY_MC */
627 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
629 if (WARN_ON_ONCE(!i->data_source))
632 if (user_backed_iter(i))
634 iterate_and_advance(i, bytes, base, len, off,
635 copyin(addr + off, base, len),
636 memcpy(addr + off, base, len)
641 EXPORT_SYMBOL(_copy_from_iter);
643 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
645 if (WARN_ON_ONCE(!i->data_source))
648 iterate_and_advance(i, bytes, base, len, off,
649 __copy_from_user_inatomic_nocache(addr + off, base, len),
650 memcpy(addr + off, base, len)
655 EXPORT_SYMBOL(_copy_from_iter_nocache);
657 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
659 * _copy_from_iter_flushcache - write destination through cpu cache
660 * @addr: destination kernel address
661 * @bytes: total transfer length
662 * @i: source iterator
664 * The pmem driver arranges for filesystem-dax to use this facility via
665 * dax_copy_from_iter() for ensuring that writes to persistent memory
666 * are flushed through the CPU cache. It is differentiated from
667 * _copy_from_iter_nocache() in that guarantees all data is flushed for
668 * all iterator types. The _copy_from_iter_nocache() only attempts to
669 * bypass the cache for the ITER_IOVEC case, and on some archs may use
670 * instructions that strand dirty-data in the cache.
672 * Return: number of bytes copied (may be %0)
674 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
676 if (WARN_ON_ONCE(!i->data_source))
679 iterate_and_advance(i, bytes, base, len, off,
680 __copy_from_user_flushcache(addr + off, base, len),
681 memcpy_flushcache(addr + off, base, len)
686 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
689 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
692 size_t v = n + offset;
695 * The general case needs to access the page order in order
696 * to compute the page size.
697 * However, we mostly deal with order-0 pages and thus can
698 * avoid a possible cache line miss for requests that fit all
701 if (n <= v && v <= PAGE_SIZE)
704 head = compound_head(page);
705 v += (page - head) << PAGE_SHIFT;
707 if (WARN_ON(n > v || v > page_size(head)))
712 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
716 if (!page_copy_sane(page, offset, bytes))
718 if (WARN_ON_ONCE(i->data_source))
720 if (unlikely(iov_iter_is_pipe(i)))
721 return copy_page_to_iter_pipe(page, offset, bytes, i);
722 page += offset / PAGE_SIZE; // first subpage
725 void *kaddr = kmap_local_page(page);
726 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
727 n = _copy_to_iter(kaddr + offset, n, i);
734 if (offset == PAGE_SIZE) {
741 EXPORT_SYMBOL(copy_page_to_iter);
743 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
747 if (!page_copy_sane(page, offset, bytes))
749 page += offset / PAGE_SIZE; // first subpage
752 void *kaddr = kmap_local_page(page);
753 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
754 n = _copy_from_iter(kaddr + offset, n, i);
761 if (offset == PAGE_SIZE) {
768 EXPORT_SYMBOL(copy_page_from_iter);
770 static size_t pipe_zero(size_t bytes, struct iov_iter *i)
772 unsigned int chunk, off;
774 if (unlikely(bytes > i->count))
776 if (unlikely(!bytes))
782 for (size_t n = bytes; n; n -= chunk) {
783 struct page *page = append_pipe(i, n, &off);
788 chunk = min_t(size_t, n, PAGE_SIZE - off);
789 p = kmap_local_page(page);
790 memset(p + off, 0, chunk);
796 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
798 if (unlikely(iov_iter_is_pipe(i)))
799 return pipe_zero(bytes, i);
800 iterate_and_advance(i, bytes, base, len, count,
801 clear_user(base, len),
807 EXPORT_SYMBOL(iov_iter_zero);
809 size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
812 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
813 if (!page_copy_sane(page, offset, bytes)) {
814 kunmap_atomic(kaddr);
817 if (WARN_ON_ONCE(!i->data_source)) {
818 kunmap_atomic(kaddr);
821 iterate_and_advance(i, bytes, base, len, off,
822 copyin(p + off, base, len),
823 memcpy(p + off, base, len)
825 kunmap_atomic(kaddr);
828 EXPORT_SYMBOL(copy_page_from_iter_atomic);
830 static void pipe_advance(struct iov_iter *i, size_t size)
832 struct pipe_inode_info *pipe = i->pipe;
833 int off = i->last_offset;
836 pipe_discard_from(pipe, i->start_head); // discard everything
841 struct pipe_buffer *buf = pipe_buf(pipe, i->head);
842 if (off) /* make it relative to the beginning of buffer */
843 size += abs(off) - buf->offset;
844 if (size <= buf->len) {
846 i->last_offset = last_offset(buf);
853 pipe_discard_from(pipe, i->head + 1); // discard everything past this one
856 static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
858 const struct bio_vec *bvec, *end;
864 size += i->iov_offset;
866 for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
867 if (likely(size < bvec->bv_len))
869 size -= bvec->bv_len;
871 i->iov_offset = size;
872 i->nr_segs -= bvec - i->bvec;
876 static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
878 const struct iovec *iov, *end;
884 size += i->iov_offset; // from beginning of current segment
885 for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
886 if (likely(size < iov->iov_len))
888 size -= iov->iov_len;
890 i->iov_offset = size;
891 i->nr_segs -= iov - i->iov;
895 void iov_iter_advance(struct iov_iter *i, size_t size)
897 if (unlikely(i->count < size))
899 if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
900 i->iov_offset += size;
902 } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
903 /* iovec and kvec have identical layouts */
904 iov_iter_iovec_advance(i, size);
905 } else if (iov_iter_is_bvec(i)) {
906 iov_iter_bvec_advance(i, size);
907 } else if (iov_iter_is_pipe(i)) {
908 pipe_advance(i, size);
909 } else if (iov_iter_is_discard(i)) {
913 EXPORT_SYMBOL(iov_iter_advance);
915 void iov_iter_revert(struct iov_iter *i, size_t unroll)
919 if (WARN_ON(unroll > MAX_RW_COUNT))
922 if (unlikely(iov_iter_is_pipe(i))) {
923 struct pipe_inode_info *pipe = i->pipe;
924 unsigned int head = pipe->head;
926 while (head > i->start_head) {
927 struct pipe_buffer *b = pipe_buf(pipe, --head);
928 if (unroll < b->len) {
930 i->last_offset = last_offset(b);
935 pipe_buf_release(pipe, b);
942 if (unlikely(iov_iter_is_discard(i)))
944 if (unroll <= i->iov_offset) {
945 i->iov_offset -= unroll;
948 unroll -= i->iov_offset;
949 if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
950 BUG(); /* We should never go beyond the start of the specified
951 * range since we might then be straying into pages that
954 } else if (iov_iter_is_bvec(i)) {
955 const struct bio_vec *bvec = i->bvec;
957 size_t n = (--bvec)->bv_len;
961 i->iov_offset = n - unroll;
966 } else { /* same logics for iovec and kvec */
967 const struct iovec *iov = i->iov;
969 size_t n = (--iov)->iov_len;
973 i->iov_offset = n - unroll;
980 EXPORT_SYMBOL(iov_iter_revert);
983 * Return the count of just the current iov_iter segment.
985 size_t iov_iter_single_seg_count(const struct iov_iter *i)
987 if (i->nr_segs > 1) {
988 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
989 return min(i->count, i->iov->iov_len - i->iov_offset);
990 if (iov_iter_is_bvec(i))
991 return min(i->count, i->bvec->bv_len - i->iov_offset);
995 EXPORT_SYMBOL(iov_iter_single_seg_count);
997 void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
998 const struct kvec *kvec, unsigned long nr_segs,
1001 WARN_ON(direction & ~(READ | WRITE));
1002 *i = (struct iov_iter){
1003 .iter_type = ITER_KVEC,
1004 .data_source = direction,
1011 EXPORT_SYMBOL(iov_iter_kvec);
1013 void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1014 const struct bio_vec *bvec, unsigned long nr_segs,
1017 WARN_ON(direction & ~(READ | WRITE));
1018 *i = (struct iov_iter){
1019 .iter_type = ITER_BVEC,
1020 .data_source = direction,
1027 EXPORT_SYMBOL(iov_iter_bvec);
1029 void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1030 struct pipe_inode_info *pipe,
1033 BUG_ON(direction != READ);
1034 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
1035 *i = (struct iov_iter){
1036 .iter_type = ITER_PIPE,
1037 .data_source = false,
1040 .start_head = pipe->head,
1045 EXPORT_SYMBOL(iov_iter_pipe);
1048 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1049 * @i: The iterator to initialise.
1050 * @direction: The direction of the transfer.
1051 * @xarray: The xarray to access.
1052 * @start: The start file position.
1053 * @count: The size of the I/O buffer in bytes.
1055 * Set up an I/O iterator to either draw data out of the pages attached to an
1056 * inode or to inject data into those pages. The pages *must* be prevented
1057 * from evaporation, either by taking a ref on them or locking them by the
1060 void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1061 struct xarray *xarray, loff_t start, size_t count)
1063 BUG_ON(direction & ~1);
1064 *i = (struct iov_iter) {
1065 .iter_type = ITER_XARRAY,
1066 .data_source = direction,
1068 .xarray_start = start,
1073 EXPORT_SYMBOL(iov_iter_xarray);
1076 * iov_iter_discard - Initialise an I/O iterator that discards data
1077 * @i: The iterator to initialise.
1078 * @direction: The direction of the transfer.
1079 * @count: The size of the I/O buffer in bytes.
1081 * Set up an I/O iterator that just discards everything that's written to it.
1082 * It's only available as a READ iterator.
1084 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1086 BUG_ON(direction != READ);
1087 *i = (struct iov_iter){
1088 .iter_type = ITER_DISCARD,
1089 .data_source = false,
1094 EXPORT_SYMBOL(iov_iter_discard);
1096 static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
1099 size_t size = i->count;
1100 size_t skip = i->iov_offset;
1103 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1104 size_t len = i->iov[k].iov_len - skip;
1110 if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask)
1120 static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
1123 size_t size = i->count;
1124 unsigned skip = i->iov_offset;
1127 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1128 size_t len = i->bvec[k].bv_len - skip;
1134 if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
1145 * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
1146 * are aligned to the parameters.
1148 * @i: &struct iov_iter to restore
1149 * @addr_mask: bit mask to check against the iov element's addresses
1150 * @len_mask: bit mask to check against the iov element's lengths
1152 * Return: false if any addresses or lengths intersect with the provided masks
1154 bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
1157 if (likely(iter_is_ubuf(i))) {
1158 if (i->count & len_mask)
1160 if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
1165 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1166 return iov_iter_aligned_iovec(i, addr_mask, len_mask);
1168 if (iov_iter_is_bvec(i))
1169 return iov_iter_aligned_bvec(i, addr_mask, len_mask);
1171 if (iov_iter_is_pipe(i)) {
1172 size_t size = i->count;
1174 if (size & len_mask)
1176 if (size && i->last_offset > 0) {
1177 if (i->last_offset & addr_mask)
1184 if (iov_iter_is_xarray(i)) {
1185 if (i->count & len_mask)
1187 if ((i->xarray_start + i->iov_offset) & addr_mask)
1193 EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
1195 static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1197 unsigned long res = 0;
1198 size_t size = i->count;
1199 size_t skip = i->iov_offset;
1202 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1203 size_t len = i->iov[k].iov_len - skip;
1205 res |= (unsigned long)i->iov[k].iov_base + skip;
1217 static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
1220 size_t size = i->count;
1221 unsigned skip = i->iov_offset;
1224 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1225 size_t len = i->bvec[k].bv_len - skip;
1226 res |= (unsigned long)i->bvec[k].bv_offset + skip;
1237 unsigned long iov_iter_alignment(const struct iov_iter *i)
1239 if (likely(iter_is_ubuf(i))) {
1240 size_t size = i->count;
1242 return ((unsigned long)i->ubuf + i->iov_offset) | size;
1246 /* iovec and kvec have identical layouts */
1247 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1248 return iov_iter_alignment_iovec(i);
1250 if (iov_iter_is_bvec(i))
1251 return iov_iter_alignment_bvec(i);
1253 if (iov_iter_is_pipe(i)) {
1254 size_t size = i->count;
1256 if (size && i->last_offset > 0)
1257 return size | i->last_offset;
1261 if (iov_iter_is_xarray(i))
1262 return (i->xarray_start + i->iov_offset) | i->count;
1266 EXPORT_SYMBOL(iov_iter_alignment);
1268 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1270 unsigned long res = 0;
1271 unsigned long v = 0;
1272 size_t size = i->count;
1275 if (iter_is_ubuf(i))
1278 if (WARN_ON(!iter_is_iovec(i)))
1281 for (k = 0; k < i->nr_segs; k++) {
1282 if (i->iov[k].iov_len) {
1283 unsigned long base = (unsigned long)i->iov[k].iov_base;
1284 if (v) // if not the first one
1285 res |= base | v; // this start | previous end
1286 v = base + i->iov[k].iov_len;
1287 if (size <= i->iov[k].iov_len)
1289 size -= i->iov[k].iov_len;
1294 EXPORT_SYMBOL(iov_iter_gap_alignment);
1296 static int want_pages_array(struct page ***res, size_t size,
1297 size_t start, unsigned int maxpages)
1299 unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE);
1301 if (count > maxpages)
1303 WARN_ON(!count); // caller should've prevented that
1305 *res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
1312 static ssize_t pipe_get_pages(struct iov_iter *i,
1313 struct page ***pages, size_t maxsize, unsigned maxpages,
1316 unsigned int npages, count, off, chunk;
1323 *start = off = pipe_npages(i, &npages);
1326 count = want_pages_array(pages, maxsize, off, min(npages, maxpages));
1330 for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) {
1331 struct page *page = append_pipe(i, left, &off);
1334 chunk = min_t(size_t, left, PAGE_SIZE - off);
1335 get_page(*p++ = page);
1339 return maxsize - left;
1342 static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1343 pgoff_t index, unsigned int nr_pages)
1345 XA_STATE(xas, xa, index);
1347 unsigned int ret = 0;
1350 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1351 if (xas_retry(&xas, page))
1354 /* Has the page moved or been split? */
1355 if (unlikely(page != xas_reload(&xas))) {
1360 pages[ret] = find_subpage(page, xas.xa_index);
1361 get_page(pages[ret]);
1362 if (++ret == nr_pages)
1369 static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1370 struct page ***pages, size_t maxsize,
1371 unsigned maxpages, size_t *_start_offset)
1373 unsigned nr, offset, count;
1377 pos = i->xarray_start + i->iov_offset;
1378 index = pos >> PAGE_SHIFT;
1379 offset = pos & ~PAGE_MASK;
1380 *_start_offset = offset;
1382 count = want_pages_array(pages, maxsize, offset, maxpages);
1385 nr = iter_xarray_populate_pages(*pages, i->xarray, index, count);
1389 maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1390 i->iov_offset += maxsize;
1391 i->count -= maxsize;
1395 /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
1396 static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
1401 if (iter_is_ubuf(i))
1402 return (unsigned long)i->ubuf + i->iov_offset;
1404 for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1405 size_t len = i->iov[k].iov_len - skip;
1411 return (unsigned long)i->iov[k].iov_base + skip;
1413 BUG(); // if it had been empty, we wouldn't get called
1416 /* must be done on non-empty ITER_BVEC one */
1417 static struct page *first_bvec_segment(const struct iov_iter *i,
1418 size_t *size, size_t *start)
1421 size_t skip = i->iov_offset, len;
1423 len = i->bvec->bv_len - skip;
1426 skip += i->bvec->bv_offset;
1427 page = i->bvec->bv_page + skip / PAGE_SIZE;
1428 *start = skip % PAGE_SIZE;
1432 static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
1433 struct page ***pages, size_t maxsize,
1434 unsigned int maxpages, size_t *start,
1435 unsigned int gup_flags)
1439 if (maxsize > i->count)
1443 if (maxsize > MAX_RW_COUNT)
1444 maxsize = MAX_RW_COUNT;
1446 if (likely(user_backed_iter(i))) {
1450 if (iov_iter_rw(i) != WRITE)
1451 gup_flags |= FOLL_WRITE;
1453 gup_flags |= FOLL_NOFAULT;
1455 addr = first_iovec_segment(i, &maxsize);
1456 *start = addr % PAGE_SIZE;
1458 n = want_pages_array(pages, maxsize, *start, maxpages);
1461 res = get_user_pages_fast(addr, n, gup_flags, *pages);
1462 if (unlikely(res <= 0))
1464 maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1465 iov_iter_advance(i, maxsize);
1468 if (iov_iter_is_bvec(i)) {
1472 page = first_bvec_segment(i, &maxsize, start);
1473 n = want_pages_array(pages, maxsize, *start, maxpages);
1477 for (int k = 0; k < n; k++)
1478 get_page(p[k] = page + k);
1479 maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1480 i->count -= maxsize;
1481 i->iov_offset += maxsize;
1482 if (i->iov_offset == i->bvec->bv_len) {
1489 if (iov_iter_is_pipe(i))
1490 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1491 if (iov_iter_is_xarray(i))
1492 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1496 ssize_t iov_iter_get_pages(struct iov_iter *i,
1497 struct page **pages, size_t maxsize, unsigned maxpages,
1498 size_t *start, unsigned gup_flags)
1504 return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages,
1507 EXPORT_SYMBOL_GPL(iov_iter_get_pages);
1509 ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
1510 size_t maxsize, unsigned maxpages, size_t *start)
1512 return iov_iter_get_pages(i, pages, maxsize, maxpages, start, 0);
1514 EXPORT_SYMBOL(iov_iter_get_pages2);
1516 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1517 struct page ***pages, size_t maxsize,
1518 size_t *start, unsigned gup_flags)
1524 len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start,
1532 EXPORT_SYMBOL_GPL(iov_iter_get_pages_alloc);
1534 ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i,
1535 struct page ***pages, size_t maxsize, size_t *start)
1537 return iov_iter_get_pages_alloc(i, pages, maxsize, start, 0);
1539 EXPORT_SYMBOL(iov_iter_get_pages_alloc2);
1541 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1546 if (WARN_ON_ONCE(!i->data_source))
1549 iterate_and_advance(i, bytes, base, len, off, ({
1550 next = csum_and_copy_from_user(base, addr + off, len);
1551 sum = csum_block_add(sum, next, off);
1554 sum = csum_and_memcpy(addr + off, base, len, sum, off);
1560 EXPORT_SYMBOL(csum_and_copy_from_iter);
1562 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1565 struct csum_state *csstate = _csstate;
1568 if (WARN_ON_ONCE(i->data_source))
1570 if (unlikely(iov_iter_is_discard(i))) {
1571 // can't use csum_memcpy() for that one - data is not copied
1572 csstate->csum = csum_block_add(csstate->csum,
1573 csum_partial(addr, bytes, 0),
1575 csstate->off += bytes;
1579 sum = csum_shift(csstate->csum, csstate->off);
1580 if (unlikely(iov_iter_is_pipe(i)))
1581 bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
1582 else iterate_and_advance(i, bytes, base, len, off, ({
1583 next = csum_and_copy_to_user(addr + off, base, len);
1584 sum = csum_block_add(sum, next, off);
1587 sum = csum_and_memcpy(base, addr + off, len, sum, off);
1590 csstate->csum = csum_shift(sum, csstate->off);
1591 csstate->off += bytes;
1594 EXPORT_SYMBOL(csum_and_copy_to_iter);
1596 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1599 #ifdef CONFIG_CRYPTO_HASH
1600 struct ahash_request *hash = hashp;
1601 struct scatterlist sg;
1604 copied = copy_to_iter(addr, bytes, i);
1605 sg_init_one(&sg, addr, copied);
1606 ahash_request_set_crypt(hash, &sg, NULL, copied);
1607 crypto_ahash_update(hash);
1613 EXPORT_SYMBOL(hash_and_copy_to_iter);
1615 static int iov_npages(const struct iov_iter *i, int maxpages)
1617 size_t skip = i->iov_offset, size = i->count;
1618 const struct iovec *p;
1621 for (p = i->iov; size; skip = 0, p++) {
1622 unsigned offs = offset_in_page(p->iov_base + skip);
1623 size_t len = min(p->iov_len - skip, size);
1627 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1628 if (unlikely(npages > maxpages))
1635 static int bvec_npages(const struct iov_iter *i, int maxpages)
1637 size_t skip = i->iov_offset, size = i->count;
1638 const struct bio_vec *p;
1641 for (p = i->bvec; size; skip = 0, p++) {
1642 unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
1643 size_t len = min(p->bv_len - skip, size);
1646 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1647 if (unlikely(npages > maxpages))
1653 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1655 if (unlikely(!i->count))
1657 if (likely(iter_is_ubuf(i))) {
1658 unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1659 int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1660 return min(npages, maxpages);
1662 /* iovec and kvec have identical layouts */
1663 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1664 return iov_npages(i, maxpages);
1665 if (iov_iter_is_bvec(i))
1666 return bvec_npages(i, maxpages);
1667 if (iov_iter_is_pipe(i)) {
1673 pipe_npages(i, &npages);
1674 return min(npages, maxpages);
1676 if (iov_iter_is_xarray(i)) {
1677 unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1678 int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
1679 return min(npages, maxpages);
1683 EXPORT_SYMBOL(iov_iter_npages);
1685 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1688 if (unlikely(iov_iter_is_pipe(new))) {
1692 if (iov_iter_is_bvec(new))
1693 return new->bvec = kmemdup(new->bvec,
1694 new->nr_segs * sizeof(struct bio_vec),
1696 else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1697 /* iovec and kvec have identical layout */
1698 return new->iov = kmemdup(new->iov,
1699 new->nr_segs * sizeof(struct iovec),
1703 EXPORT_SYMBOL(dup_iter);
1705 static int copy_compat_iovec_from_user(struct iovec *iov,
1706 const struct iovec __user *uvec, unsigned long nr_segs)
1708 const struct compat_iovec __user *uiov =
1709 (const struct compat_iovec __user *)uvec;
1710 int ret = -EFAULT, i;
1712 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1715 for (i = 0; i < nr_segs; i++) {
1719 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1720 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1722 /* check for compat_size_t not fitting in compat_ssize_t .. */
1727 iov[i].iov_base = compat_ptr(buf);
1728 iov[i].iov_len = len;
1737 static int copy_iovec_from_user(struct iovec *iov,
1738 const struct iovec __user *uvec, unsigned long nr_segs)
1742 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1744 for (seg = 0; seg < nr_segs; seg++) {
1745 if ((ssize_t)iov[seg].iov_len < 0)
1752 struct iovec *iovec_from_user(const struct iovec __user *uvec,
1753 unsigned long nr_segs, unsigned long fast_segs,
1754 struct iovec *fast_iov, bool compat)
1756 struct iovec *iov = fast_iov;
1760 * SuS says "The readv() function *may* fail if the iovcnt argument was
1761 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
1762 * traditionally returned zero for zero segments, so...
1766 if (nr_segs > UIO_MAXIOV)
1767 return ERR_PTR(-EINVAL);
1768 if (nr_segs > fast_segs) {
1769 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1771 return ERR_PTR(-ENOMEM);
1775 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1777 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1779 if (iov != fast_iov)
1781 return ERR_PTR(ret);
1787 ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1788 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1789 struct iov_iter *i, bool compat)
1791 ssize_t total_len = 0;
1795 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1798 return PTR_ERR(iov);
1802 * According to the Single Unix Specification we should return EINVAL if
1803 * an element length is < 0 when cast to ssize_t or if the total length
1804 * would overflow the ssize_t return value of the system call.
1806 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1809 for (seg = 0; seg < nr_segs; seg++) {
1810 ssize_t len = (ssize_t)iov[seg].iov_len;
1812 if (!access_ok(iov[seg].iov_base, len)) {
1819 if (len > MAX_RW_COUNT - total_len) {
1820 len = MAX_RW_COUNT - total_len;
1821 iov[seg].iov_len = len;
1826 iov_iter_init(i, type, iov, nr_segs, total_len);
1835 * import_iovec() - Copy an array of &struct iovec from userspace
1836 * into the kernel, check that it is valid, and initialize a new
1837 * &struct iov_iter iterator to access it.
1839 * @type: One of %READ or %WRITE.
1840 * @uvec: Pointer to the userspace array.
1841 * @nr_segs: Number of elements in userspace array.
1842 * @fast_segs: Number of elements in @iov.
1843 * @iovp: (input and output parameter) Pointer to pointer to (usually small
1844 * on-stack) kernel array.
1845 * @i: Pointer to iterator that will be initialized on success.
1847 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1848 * then this function places %NULL in *@iov on return. Otherwise, a new
1849 * array will be allocated and the result placed in *@iov. This means that
1850 * the caller may call kfree() on *@iov regardless of whether the small
1851 * on-stack array was used or not (and regardless of whether this function
1852 * returns an error or not).
1854 * Return: Negative error code on error, bytes imported on success
1856 ssize_t import_iovec(int type, const struct iovec __user *uvec,
1857 unsigned nr_segs, unsigned fast_segs,
1858 struct iovec **iovp, struct iov_iter *i)
1860 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1861 in_compat_syscall());
1863 EXPORT_SYMBOL(import_iovec);
1865 int import_single_range(int rw, void __user *buf, size_t len,
1866 struct iovec *iov, struct iov_iter *i)
1868 if (len > MAX_RW_COUNT)
1870 if (unlikely(!access_ok(buf, len)))
1873 iov->iov_base = buf;
1875 iov_iter_init(i, rw, iov, 1, len);
1878 EXPORT_SYMBOL(import_single_range);
1881 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
1882 * iov_iter_save_state() was called.
1884 * @i: &struct iov_iter to restore
1885 * @state: state to restore from
1887 * Used after iov_iter_save_state() to bring restore @i, if operations may
1890 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
1892 void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
1894 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
1895 !iov_iter_is_kvec(i) && !iter_is_ubuf(i))
1897 i->iov_offset = state->iov_offset;
1898 i->count = state->count;
1899 if (iter_is_ubuf(i))
1902 * For the *vec iters, nr_segs + iov is constant - if we increment
1903 * the vec, then we also decrement the nr_segs count. Hence we don't
1904 * need to track both of these, just one is enough and we can deduct
1905 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
1906 * size, so we can just increment the iov pointer as they are unionzed.
1907 * ITER_BVEC _may_ be the same size on some archs, but on others it is
1908 * not. Be safe and handle it separately.
1910 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
1911 if (iov_iter_is_bvec(i))
1912 i->bvec -= state->nr_segs - i->nr_segs;
1914 i->iov -= state->nr_segs - i->nr_segs;
1915 i->nr_segs = state->nr_segs;