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)
177 if (should_fail_usercopy())
179 if (access_ok(from, n)) {
180 instrument_copy_from_user(to, from, n);
181 n = raw_copy_from_user(to, from, n);
186 static inline struct pipe_buffer *pipe_buf(const struct pipe_inode_info *pipe,
189 return &pipe->bufs[slot & (pipe->ring_size - 1)];
193 static bool sanity(const struct iov_iter *i)
195 struct pipe_inode_info *pipe = i->pipe;
196 unsigned int p_head = pipe->head;
197 unsigned int p_tail = pipe->tail;
198 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
199 unsigned int i_head = i->head;
202 if (i->last_offset) {
203 struct pipe_buffer *p;
204 if (unlikely(p_occupancy == 0))
205 goto Bad; // pipe must be non-empty
206 if (unlikely(i_head != p_head - 1))
207 goto Bad; // must be at the last buffer...
209 p = pipe_buf(pipe, i_head);
210 if (unlikely(p->offset + p->len != abs(i->last_offset)))
211 goto Bad; // ... at the end of segment
213 if (i_head != p_head)
214 goto Bad; // must be right after the last buffer
218 printk(KERN_ERR "idx = %d, offset = %d\n", i_head, i->last_offset);
219 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
220 p_head, p_tail, pipe->ring_size);
221 for (idx = 0; idx < pipe->ring_size; idx++)
222 printk(KERN_ERR "[%p %p %d %d]\n",
224 pipe->bufs[idx].page,
225 pipe->bufs[idx].offset,
226 pipe->bufs[idx].len);
231 #define sanity(i) true
234 static struct page *push_anon(struct pipe_inode_info *pipe, unsigned size)
236 struct page *page = alloc_page(GFP_USER);
238 struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
239 *buf = (struct pipe_buffer) {
240 .ops = &default_pipe_buf_ops,
249 static void push_page(struct pipe_inode_info *pipe, struct page *page,
250 unsigned int offset, unsigned int size)
252 struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
253 *buf = (struct pipe_buffer) {
254 .ops = &page_cache_pipe_buf_ops,
262 static inline int last_offset(const struct pipe_buffer *buf)
264 if (buf->ops == &default_pipe_buf_ops)
265 return buf->len; // buf->offset is 0 for those
267 return -(buf->offset + buf->len);
270 static struct page *append_pipe(struct iov_iter *i, size_t size,
273 struct pipe_inode_info *pipe = i->pipe;
274 int offset = i->last_offset;
275 struct pipe_buffer *buf;
278 if (offset > 0 && offset < PAGE_SIZE) {
279 // some space in the last buffer; add to it
280 buf = pipe_buf(pipe, pipe->head - 1);
281 size = min_t(size_t, size, PAGE_SIZE - offset);
283 i->last_offset += size;
288 // OK, we need a new buffer
290 size = min_t(size_t, size, PAGE_SIZE);
291 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
293 page = push_anon(pipe, size);
296 i->head = pipe->head - 1;
297 i->last_offset = size;
302 static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
305 struct pipe_inode_info *pipe = i->pipe;
306 unsigned int head = pipe->head;
308 if (unlikely(bytes > i->count))
311 if (unlikely(!bytes))
317 if (offset && i->last_offset == -offset) { // could we merge it?
318 struct pipe_buffer *buf = pipe_buf(pipe, head - 1);
319 if (buf->page == page) {
321 i->last_offset -= bytes;
326 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
329 push_page(pipe, page, offset, bytes);
330 i->last_offset = -(offset + bytes);
337 * fault_in_iov_iter_readable - fault in iov iterator for reading
339 * @size: maximum length
341 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
342 * @size. For each iovec, fault in each page that constitutes the iovec.
344 * Returns the number of bytes not faulted in (like copy_to_user() and
347 * Always returns 0 for non-userspace iterators.
349 size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
351 if (iter_is_ubuf(i)) {
352 size_t n = min(size, iov_iter_count(i));
353 n -= fault_in_readable(i->ubuf + i->iov_offset, n);
355 } else if (iter_is_iovec(i)) {
356 size_t count = min(size, iov_iter_count(i));
357 const struct iovec *p;
361 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
362 size_t len = min(count, p->iov_len - skip);
367 ret = fault_in_readable(p->iov_base + skip, len);
376 EXPORT_SYMBOL(fault_in_iov_iter_readable);
379 * fault_in_iov_iter_writeable - fault in iov iterator for writing
381 * @size: maximum length
383 * Faults in the iterator using get_user_pages(), i.e., without triggering
384 * hardware page faults. This is primarily useful when we already know that
385 * some or all of the pages in @i aren't in memory.
387 * Returns the number of bytes not faulted in, like copy_to_user() and
390 * Always returns 0 for non-user-space iterators.
392 size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
394 if (iter_is_ubuf(i)) {
395 size_t n = min(size, iov_iter_count(i));
396 n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n);
398 } else if (iter_is_iovec(i)) {
399 size_t count = min(size, iov_iter_count(i));
400 const struct iovec *p;
404 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
405 size_t len = min(count, p->iov_len - skip);
410 ret = fault_in_safe_writeable(p->iov_base + skip, len);
419 EXPORT_SYMBOL(fault_in_iov_iter_writeable);
421 void iov_iter_init(struct iov_iter *i, unsigned int direction,
422 const struct iovec *iov, unsigned long nr_segs,
425 WARN_ON(direction & ~(READ | WRITE));
426 *i = (struct iov_iter) {
427 .iter_type = ITER_IOVEC,
430 .data_source = direction,
437 EXPORT_SYMBOL(iov_iter_init);
439 // returns the offset in partial buffer (if any)
440 static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages)
442 struct pipe_inode_info *pipe = i->pipe;
443 int used = pipe->head - pipe->tail;
444 int off = i->last_offset;
446 *npages = max((int)pipe->max_usage - used, 0);
448 if (off > 0 && off < PAGE_SIZE) { // anon and not full
455 static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
458 unsigned int off, chunk;
460 if (unlikely(bytes > i->count))
462 if (unlikely(!bytes))
468 for (size_t n = bytes; n; n -= chunk) {
469 struct page *page = append_pipe(i, n, &off);
470 chunk = min_t(size_t, n, PAGE_SIZE - off);
473 memcpy_to_page(page, off, addr, chunk);
479 static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
480 __wsum sum, size_t off)
482 __wsum next = csum_partial_copy_nocheck(from, to, len);
483 return csum_block_add(sum, next, off);
486 static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
487 struct iov_iter *i, __wsum *sump)
491 unsigned int chunk, r;
493 if (unlikely(bytes > i->count))
495 if (unlikely(!bytes))
502 struct page *page = append_pipe(i, bytes, &r);
507 chunk = min_t(size_t, bytes, PAGE_SIZE - r);
508 p = kmap_local_page(page);
509 sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
518 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
520 if (unlikely(iov_iter_is_pipe(i)))
521 return copy_pipe_to_iter(addr, bytes, i);
522 if (user_backed_iter(i))
524 iterate_and_advance(i, bytes, base, len, off,
525 copyout(base, addr + off, len),
526 memcpy(base, addr + off, len)
531 EXPORT_SYMBOL(_copy_to_iter);
533 #ifdef CONFIG_ARCH_HAS_COPY_MC
534 static int copyout_mc(void __user *to, const void *from, size_t n)
536 if (access_ok(to, n)) {
537 instrument_copy_to_user(to, from, n);
538 n = copy_mc_to_user((__force void *) to, from, n);
543 static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
547 unsigned int off, chunk;
549 if (unlikely(bytes > i->count))
551 if (unlikely(!bytes))
558 struct page *page = append_pipe(i, bytes, &off);
564 chunk = min_t(size_t, bytes, PAGE_SIZE - off);
565 p = kmap_local_page(page);
566 rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
572 iov_iter_revert(i, rem);
580 * _copy_mc_to_iter - copy to iter with source memory error exception handling
581 * @addr: source kernel address
582 * @bytes: total transfer length
583 * @i: destination iterator
585 * The pmem driver deploys this for the dax operation
586 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
587 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
588 * successfully copied.
590 * The main differences between this and typical _copy_to_iter().
592 * * Typical tail/residue handling after a fault retries the copy
593 * byte-by-byte until the fault happens again. Re-triggering machine
594 * checks is potentially fatal so the implementation uses source
595 * alignment and poison alignment assumptions to avoid re-triggering
596 * hardware exceptions.
598 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
599 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
602 * Return: number of bytes copied (may be %0)
604 size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
606 if (unlikely(iov_iter_is_pipe(i)))
607 return copy_mc_pipe_to_iter(addr, bytes, i);
608 if (user_backed_iter(i))
610 __iterate_and_advance(i, bytes, base, len, off,
611 copyout_mc(base, addr + off, len),
612 copy_mc_to_kernel(base, addr + off, len)
617 EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
618 #endif /* CONFIG_ARCH_HAS_COPY_MC */
620 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
622 if (unlikely(iov_iter_is_pipe(i))) {
626 if (user_backed_iter(i))
628 iterate_and_advance(i, bytes, base, len, off,
629 copyin(addr + off, base, len),
630 memcpy(addr + off, base, len)
635 EXPORT_SYMBOL(_copy_from_iter);
637 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
639 if (unlikely(iov_iter_is_pipe(i))) {
643 iterate_and_advance(i, bytes, base, len, off,
644 __copy_from_user_inatomic_nocache(addr + off, base, len),
645 memcpy(addr + off, base, len)
650 EXPORT_SYMBOL(_copy_from_iter_nocache);
652 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
654 * _copy_from_iter_flushcache - write destination through cpu cache
655 * @addr: destination kernel address
656 * @bytes: total transfer length
657 * @i: source iterator
659 * The pmem driver arranges for filesystem-dax to use this facility via
660 * dax_copy_from_iter() for ensuring that writes to persistent memory
661 * are flushed through the CPU cache. It is differentiated from
662 * _copy_from_iter_nocache() in that guarantees all data is flushed for
663 * all iterator types. The _copy_from_iter_nocache() only attempts to
664 * bypass the cache for the ITER_IOVEC case, and on some archs may use
665 * instructions that strand dirty-data in the cache.
667 * Return: number of bytes copied (may be %0)
669 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
671 if (unlikely(iov_iter_is_pipe(i))) {
675 iterate_and_advance(i, bytes, base, len, off,
676 __copy_from_user_flushcache(addr + off, base, len),
677 memcpy_flushcache(addr + off, base, len)
682 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
685 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
688 size_t v = n + offset;
691 * The general case needs to access the page order in order
692 * to compute the page size.
693 * However, we mostly deal with order-0 pages and thus can
694 * avoid a possible cache line miss for requests that fit all
697 if (n <= v && v <= PAGE_SIZE)
700 head = compound_head(page);
701 v += (page - head) << PAGE_SHIFT;
703 if (likely(n <= v && v <= (page_size(head))))
709 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
713 if (unlikely(!page_copy_sane(page, offset, bytes)))
715 if (unlikely(iov_iter_is_pipe(i)))
716 return copy_page_to_iter_pipe(page, offset, bytes, i);
717 page += offset / PAGE_SIZE; // first subpage
720 void *kaddr = kmap_local_page(page);
721 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
722 n = _copy_to_iter(kaddr + offset, n, i);
729 if (offset == PAGE_SIZE) {
736 EXPORT_SYMBOL(copy_page_to_iter);
738 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
742 if (!page_copy_sane(page, offset, bytes))
744 page += offset / PAGE_SIZE; // first subpage
747 void *kaddr = kmap_local_page(page);
748 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
749 n = _copy_from_iter(kaddr + offset, n, i);
756 if (offset == PAGE_SIZE) {
763 EXPORT_SYMBOL(copy_page_from_iter);
765 static size_t pipe_zero(size_t bytes, struct iov_iter *i)
767 unsigned int chunk, off;
769 if (unlikely(bytes > i->count))
771 if (unlikely(!bytes))
777 for (size_t n = bytes; n; n -= chunk) {
778 struct page *page = append_pipe(i, n, &off);
783 chunk = min_t(size_t, n, PAGE_SIZE - off);
784 p = kmap_local_page(page);
785 memset(p + off, 0, chunk);
791 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
793 if (unlikely(iov_iter_is_pipe(i)))
794 return pipe_zero(bytes, i);
795 iterate_and_advance(i, bytes, base, len, count,
796 clear_user(base, len),
802 EXPORT_SYMBOL(iov_iter_zero);
804 size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
807 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
808 if (unlikely(!page_copy_sane(page, offset, bytes))) {
809 kunmap_atomic(kaddr);
812 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
813 kunmap_atomic(kaddr);
817 iterate_and_advance(i, bytes, base, len, off,
818 copyin(p + off, base, len),
819 memcpy(p + off, base, len)
821 kunmap_atomic(kaddr);
824 EXPORT_SYMBOL(copy_page_from_iter_atomic);
826 static void pipe_advance(struct iov_iter *i, size_t size)
828 struct pipe_inode_info *pipe = i->pipe;
829 int off = i->last_offset;
832 pipe_discard_from(pipe, i->start_head); // discard everything
837 struct pipe_buffer *buf = pipe_buf(pipe, i->head);
838 if (off) /* make it relative to the beginning of buffer */
839 size += abs(off) - buf->offset;
840 if (size <= buf->len) {
842 i->last_offset = last_offset(buf);
849 pipe_discard_from(pipe, i->head + 1); // discard everything past this one
852 static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
854 const struct bio_vec *bvec, *end;
860 size += i->iov_offset;
862 for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
863 if (likely(size < bvec->bv_len))
865 size -= bvec->bv_len;
867 i->iov_offset = size;
868 i->nr_segs -= bvec - i->bvec;
872 static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
874 const struct iovec *iov, *end;
880 size += i->iov_offset; // from beginning of current segment
881 for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
882 if (likely(size < iov->iov_len))
884 size -= iov->iov_len;
886 i->iov_offset = size;
887 i->nr_segs -= iov - i->iov;
891 void iov_iter_advance(struct iov_iter *i, size_t size)
893 if (unlikely(i->count < size))
895 if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
896 i->iov_offset += size;
898 } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
899 /* iovec and kvec have identical layouts */
900 iov_iter_iovec_advance(i, size);
901 } else if (iov_iter_is_bvec(i)) {
902 iov_iter_bvec_advance(i, size);
903 } else if (iov_iter_is_pipe(i)) {
904 pipe_advance(i, size);
905 } else if (iov_iter_is_discard(i)) {
909 EXPORT_SYMBOL(iov_iter_advance);
911 void iov_iter_revert(struct iov_iter *i, size_t unroll)
915 if (WARN_ON(unroll > MAX_RW_COUNT))
918 if (unlikely(iov_iter_is_pipe(i))) {
919 struct pipe_inode_info *pipe = i->pipe;
920 unsigned int head = pipe->head;
922 while (head > i->start_head) {
923 struct pipe_buffer *b = pipe_buf(pipe, --head);
924 if (unroll < b->len) {
926 i->last_offset = last_offset(b);
931 pipe_buf_release(pipe, b);
938 if (unlikely(iov_iter_is_discard(i)))
940 if (unroll <= i->iov_offset) {
941 i->iov_offset -= unroll;
944 unroll -= i->iov_offset;
945 if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
946 BUG(); /* We should never go beyond the start of the specified
947 * range since we might then be straying into pages that
950 } else if (iov_iter_is_bvec(i)) {
951 const struct bio_vec *bvec = i->bvec;
953 size_t n = (--bvec)->bv_len;
957 i->iov_offset = n - unroll;
962 } else { /* same logics for iovec and kvec */
963 const struct iovec *iov = i->iov;
965 size_t n = (--iov)->iov_len;
969 i->iov_offset = n - unroll;
976 EXPORT_SYMBOL(iov_iter_revert);
979 * Return the count of just the current iov_iter segment.
981 size_t iov_iter_single_seg_count(const struct iov_iter *i)
983 if (i->nr_segs > 1) {
984 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
985 return min(i->count, i->iov->iov_len - i->iov_offset);
986 if (iov_iter_is_bvec(i))
987 return min(i->count, i->bvec->bv_len - i->iov_offset);
991 EXPORT_SYMBOL(iov_iter_single_seg_count);
993 void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
994 const struct kvec *kvec, unsigned long nr_segs,
997 WARN_ON(direction & ~(READ | WRITE));
998 *i = (struct iov_iter){
999 .iter_type = ITER_KVEC,
1000 .data_source = direction,
1007 EXPORT_SYMBOL(iov_iter_kvec);
1009 void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1010 const struct bio_vec *bvec, unsigned long nr_segs,
1013 WARN_ON(direction & ~(READ | WRITE));
1014 *i = (struct iov_iter){
1015 .iter_type = ITER_BVEC,
1016 .data_source = direction,
1023 EXPORT_SYMBOL(iov_iter_bvec);
1025 void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1026 struct pipe_inode_info *pipe,
1029 BUG_ON(direction != READ);
1030 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
1031 *i = (struct iov_iter){
1032 .iter_type = ITER_PIPE,
1033 .data_source = false,
1036 .start_head = pipe->head,
1041 EXPORT_SYMBOL(iov_iter_pipe);
1044 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1045 * @i: The iterator to initialise.
1046 * @direction: The direction of the transfer.
1047 * @xarray: The xarray to access.
1048 * @start: The start file position.
1049 * @count: The size of the I/O buffer in bytes.
1051 * Set up an I/O iterator to either draw data out of the pages attached to an
1052 * inode or to inject data into those pages. The pages *must* be prevented
1053 * from evaporation, either by taking a ref on them or locking them by the
1056 void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1057 struct xarray *xarray, loff_t start, size_t count)
1059 BUG_ON(direction & ~1);
1060 *i = (struct iov_iter) {
1061 .iter_type = ITER_XARRAY,
1062 .data_source = direction,
1064 .xarray_start = start,
1069 EXPORT_SYMBOL(iov_iter_xarray);
1072 * iov_iter_discard - Initialise an I/O iterator that discards data
1073 * @i: The iterator to initialise.
1074 * @direction: The direction of the transfer.
1075 * @count: The size of the I/O buffer in bytes.
1077 * Set up an I/O iterator that just discards everything that's written to it.
1078 * It's only available as a READ iterator.
1080 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1082 BUG_ON(direction != READ);
1083 *i = (struct iov_iter){
1084 .iter_type = ITER_DISCARD,
1085 .data_source = false,
1090 EXPORT_SYMBOL(iov_iter_discard);
1092 static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
1095 size_t size = i->count;
1096 size_t skip = i->iov_offset;
1099 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1100 size_t len = i->iov[k].iov_len - skip;
1106 if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask)
1116 static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
1119 size_t size = i->count;
1120 unsigned skip = i->iov_offset;
1123 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1124 size_t len = i->bvec[k].bv_len - skip;
1130 if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
1141 * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
1142 * are aligned to the parameters.
1144 * @i: &struct iov_iter to restore
1145 * @addr_mask: bit mask to check against the iov element's addresses
1146 * @len_mask: bit mask to check against the iov element's lengths
1148 * Return: false if any addresses or lengths intersect with the provided masks
1150 bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
1153 if (likely(iter_is_ubuf(i))) {
1154 if (i->count & len_mask)
1156 if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
1161 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1162 return iov_iter_aligned_iovec(i, addr_mask, len_mask);
1164 if (iov_iter_is_bvec(i))
1165 return iov_iter_aligned_bvec(i, addr_mask, len_mask);
1167 if (iov_iter_is_pipe(i)) {
1168 size_t size = i->count;
1170 if (size & len_mask)
1172 if (size && i->last_offset > 0) {
1173 if (i->last_offset & addr_mask)
1180 if (iov_iter_is_xarray(i)) {
1181 if (i->count & len_mask)
1183 if ((i->xarray_start + i->iov_offset) & addr_mask)
1189 EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
1191 static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1193 unsigned long res = 0;
1194 size_t size = i->count;
1195 size_t skip = i->iov_offset;
1198 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1199 size_t len = i->iov[k].iov_len - skip;
1201 res |= (unsigned long)i->iov[k].iov_base + skip;
1213 static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
1216 size_t size = i->count;
1217 unsigned skip = i->iov_offset;
1220 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1221 size_t len = i->bvec[k].bv_len - skip;
1222 res |= (unsigned long)i->bvec[k].bv_offset + skip;
1233 unsigned long iov_iter_alignment(const struct iov_iter *i)
1235 if (likely(iter_is_ubuf(i))) {
1236 size_t size = i->count;
1238 return ((unsigned long)i->ubuf + i->iov_offset) | size;
1242 /* iovec and kvec have identical layouts */
1243 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1244 return iov_iter_alignment_iovec(i);
1246 if (iov_iter_is_bvec(i))
1247 return iov_iter_alignment_bvec(i);
1249 if (iov_iter_is_pipe(i)) {
1250 size_t size = i->count;
1252 if (size && i->last_offset > 0)
1253 return size | i->last_offset;
1257 if (iov_iter_is_xarray(i))
1258 return (i->xarray_start + i->iov_offset) | i->count;
1262 EXPORT_SYMBOL(iov_iter_alignment);
1264 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1266 unsigned long res = 0;
1267 unsigned long v = 0;
1268 size_t size = i->count;
1271 if (iter_is_ubuf(i))
1274 if (WARN_ON(!iter_is_iovec(i)))
1277 for (k = 0; k < i->nr_segs; k++) {
1278 if (i->iov[k].iov_len) {
1279 unsigned long base = (unsigned long)i->iov[k].iov_base;
1280 if (v) // if not the first one
1281 res |= base | v; // this start | previous end
1282 v = base + i->iov[k].iov_len;
1283 if (size <= i->iov[k].iov_len)
1285 size -= i->iov[k].iov_len;
1290 EXPORT_SYMBOL(iov_iter_gap_alignment);
1292 static int want_pages_array(struct page ***res, size_t size,
1293 size_t start, unsigned int maxpages)
1295 unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE);
1297 if (count > maxpages)
1299 WARN_ON(!count); // caller should've prevented that
1301 *res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
1308 static ssize_t pipe_get_pages(struct iov_iter *i,
1309 struct page ***pages, size_t maxsize, unsigned maxpages,
1312 unsigned int npages, count, off, chunk;
1319 *start = off = pipe_npages(i, &npages);
1322 count = want_pages_array(pages, maxsize, off, min(npages, maxpages));
1326 for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) {
1327 struct page *page = append_pipe(i, left, &off);
1330 chunk = min_t(size_t, left, PAGE_SIZE - off);
1331 get_page(*p++ = page);
1335 return maxsize - left;
1338 static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1339 pgoff_t index, unsigned int nr_pages)
1341 XA_STATE(xas, xa, index);
1343 unsigned int ret = 0;
1346 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1347 if (xas_retry(&xas, page))
1350 /* Has the page moved or been split? */
1351 if (unlikely(page != xas_reload(&xas))) {
1356 pages[ret] = find_subpage(page, xas.xa_index);
1357 get_page(pages[ret]);
1358 if (++ret == nr_pages)
1365 static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1366 struct page ***pages, size_t maxsize,
1367 unsigned maxpages, size_t *_start_offset)
1369 unsigned nr, offset, count;
1373 pos = i->xarray_start + i->iov_offset;
1374 index = pos >> PAGE_SHIFT;
1375 offset = pos & ~PAGE_MASK;
1376 *_start_offset = offset;
1378 count = want_pages_array(pages, maxsize, offset, maxpages);
1381 nr = iter_xarray_populate_pages(*pages, i->xarray, index, count);
1385 maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1386 i->iov_offset += maxsize;
1387 i->count -= maxsize;
1391 /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
1392 static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
1397 if (iter_is_ubuf(i))
1398 return (unsigned long)i->ubuf + i->iov_offset;
1400 for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1401 size_t len = i->iov[k].iov_len - skip;
1407 return (unsigned long)i->iov[k].iov_base + skip;
1409 BUG(); // if it had been empty, we wouldn't get called
1412 /* must be done on non-empty ITER_BVEC one */
1413 static struct page *first_bvec_segment(const struct iov_iter *i,
1414 size_t *size, size_t *start)
1417 size_t skip = i->iov_offset, len;
1419 len = i->bvec->bv_len - skip;
1422 skip += i->bvec->bv_offset;
1423 page = i->bvec->bv_page + skip / PAGE_SIZE;
1424 *start = skip % PAGE_SIZE;
1428 static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
1429 struct page ***pages, size_t maxsize,
1430 unsigned int maxpages, size_t *start)
1434 if (maxsize > i->count)
1438 if (maxsize > MAX_RW_COUNT)
1439 maxsize = MAX_RW_COUNT;
1441 if (likely(user_backed_iter(i))) {
1442 unsigned int gup_flags = 0;
1446 if (iov_iter_rw(i) != WRITE)
1447 gup_flags |= FOLL_WRITE;
1449 gup_flags |= FOLL_NOFAULT;
1451 addr = first_iovec_segment(i, &maxsize);
1452 *start = addr % PAGE_SIZE;
1454 n = want_pages_array(pages, maxsize, *start, maxpages);
1457 res = get_user_pages_fast(addr, n, gup_flags, *pages);
1458 if (unlikely(res <= 0))
1460 maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1461 iov_iter_advance(i, maxsize);
1464 if (iov_iter_is_bvec(i)) {
1468 page = first_bvec_segment(i, &maxsize, start);
1469 n = want_pages_array(pages, maxsize, *start, maxpages);
1473 for (int k = 0; k < n; k++)
1474 get_page(p[k] = page + k);
1475 maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1476 i->count -= maxsize;
1477 i->iov_offset += maxsize;
1478 if (i->iov_offset == i->bvec->bv_len) {
1485 if (iov_iter_is_pipe(i))
1486 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1487 if (iov_iter_is_xarray(i))
1488 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1492 ssize_t iov_iter_get_pages2(struct iov_iter *i,
1493 struct page **pages, size_t maxsize, unsigned maxpages,
1500 return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, start);
1502 EXPORT_SYMBOL(iov_iter_get_pages2);
1504 ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i,
1505 struct page ***pages, size_t maxsize,
1512 len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start);
1519 EXPORT_SYMBOL(iov_iter_get_pages_alloc2);
1521 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1526 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1530 iterate_and_advance(i, bytes, base, len, off, ({
1531 next = csum_and_copy_from_user(base, addr + off, len);
1532 sum = csum_block_add(sum, next, off);
1535 sum = csum_and_memcpy(addr + off, base, len, sum, off);
1541 EXPORT_SYMBOL(csum_and_copy_from_iter);
1543 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1546 struct csum_state *csstate = _csstate;
1549 if (unlikely(iov_iter_is_discard(i))) {
1550 WARN_ON(1); /* for now */
1554 sum = csum_shift(csstate->csum, csstate->off);
1555 if (unlikely(iov_iter_is_pipe(i)))
1556 bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
1557 else iterate_and_advance(i, bytes, base, len, off, ({
1558 next = csum_and_copy_to_user(addr + off, base, len);
1559 sum = csum_block_add(sum, next, off);
1562 sum = csum_and_memcpy(base, addr + off, len, sum, off);
1565 csstate->csum = csum_shift(sum, csstate->off);
1566 csstate->off += bytes;
1569 EXPORT_SYMBOL(csum_and_copy_to_iter);
1571 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1574 #ifdef CONFIG_CRYPTO_HASH
1575 struct ahash_request *hash = hashp;
1576 struct scatterlist sg;
1579 copied = copy_to_iter(addr, bytes, i);
1580 sg_init_one(&sg, addr, copied);
1581 ahash_request_set_crypt(hash, &sg, NULL, copied);
1582 crypto_ahash_update(hash);
1588 EXPORT_SYMBOL(hash_and_copy_to_iter);
1590 static int iov_npages(const struct iov_iter *i, int maxpages)
1592 size_t skip = i->iov_offset, size = i->count;
1593 const struct iovec *p;
1596 for (p = i->iov; size; skip = 0, p++) {
1597 unsigned offs = offset_in_page(p->iov_base + skip);
1598 size_t len = min(p->iov_len - skip, size);
1602 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1603 if (unlikely(npages > maxpages))
1610 static int bvec_npages(const struct iov_iter *i, int maxpages)
1612 size_t skip = i->iov_offset, size = i->count;
1613 const struct bio_vec *p;
1616 for (p = i->bvec; size; skip = 0, p++) {
1617 unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
1618 size_t len = min(p->bv_len - skip, size);
1621 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1622 if (unlikely(npages > maxpages))
1628 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1630 if (unlikely(!i->count))
1632 if (likely(iter_is_ubuf(i))) {
1633 unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1634 int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1635 return min(npages, maxpages);
1637 /* iovec and kvec have identical layouts */
1638 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1639 return iov_npages(i, maxpages);
1640 if (iov_iter_is_bvec(i))
1641 return bvec_npages(i, maxpages);
1642 if (iov_iter_is_pipe(i)) {
1648 pipe_npages(i, &npages);
1649 return min(npages, maxpages);
1651 if (iov_iter_is_xarray(i)) {
1652 unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1653 int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
1654 return min(npages, maxpages);
1658 EXPORT_SYMBOL(iov_iter_npages);
1660 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1663 if (unlikely(iov_iter_is_pipe(new))) {
1667 if (iov_iter_is_bvec(new))
1668 return new->bvec = kmemdup(new->bvec,
1669 new->nr_segs * sizeof(struct bio_vec),
1671 else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1672 /* iovec and kvec have identical layout */
1673 return new->iov = kmemdup(new->iov,
1674 new->nr_segs * sizeof(struct iovec),
1678 EXPORT_SYMBOL(dup_iter);
1680 static int copy_compat_iovec_from_user(struct iovec *iov,
1681 const struct iovec __user *uvec, unsigned long nr_segs)
1683 const struct compat_iovec __user *uiov =
1684 (const struct compat_iovec __user *)uvec;
1685 int ret = -EFAULT, i;
1687 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1690 for (i = 0; i < nr_segs; i++) {
1694 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1695 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1697 /* check for compat_size_t not fitting in compat_ssize_t .. */
1702 iov[i].iov_base = compat_ptr(buf);
1703 iov[i].iov_len = len;
1712 static int copy_iovec_from_user(struct iovec *iov,
1713 const struct iovec __user *uvec, unsigned long nr_segs)
1717 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1719 for (seg = 0; seg < nr_segs; seg++) {
1720 if ((ssize_t)iov[seg].iov_len < 0)
1727 struct iovec *iovec_from_user(const struct iovec __user *uvec,
1728 unsigned long nr_segs, unsigned long fast_segs,
1729 struct iovec *fast_iov, bool compat)
1731 struct iovec *iov = fast_iov;
1735 * SuS says "The readv() function *may* fail if the iovcnt argument was
1736 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
1737 * traditionally returned zero for zero segments, so...
1741 if (nr_segs > UIO_MAXIOV)
1742 return ERR_PTR(-EINVAL);
1743 if (nr_segs > fast_segs) {
1744 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1746 return ERR_PTR(-ENOMEM);
1750 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1752 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1754 if (iov != fast_iov)
1756 return ERR_PTR(ret);
1762 ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1763 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1764 struct iov_iter *i, bool compat)
1766 ssize_t total_len = 0;
1770 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1773 return PTR_ERR(iov);
1777 * According to the Single Unix Specification we should return EINVAL if
1778 * an element length is < 0 when cast to ssize_t or if the total length
1779 * would overflow the ssize_t return value of the system call.
1781 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1784 for (seg = 0; seg < nr_segs; seg++) {
1785 ssize_t len = (ssize_t)iov[seg].iov_len;
1787 if (!access_ok(iov[seg].iov_base, len)) {
1794 if (len > MAX_RW_COUNT - total_len) {
1795 len = MAX_RW_COUNT - total_len;
1796 iov[seg].iov_len = len;
1801 iov_iter_init(i, type, iov, nr_segs, total_len);
1810 * import_iovec() - Copy an array of &struct iovec from userspace
1811 * into the kernel, check that it is valid, and initialize a new
1812 * &struct iov_iter iterator to access it.
1814 * @type: One of %READ or %WRITE.
1815 * @uvec: Pointer to the userspace array.
1816 * @nr_segs: Number of elements in userspace array.
1817 * @fast_segs: Number of elements in @iov.
1818 * @iovp: (input and output parameter) Pointer to pointer to (usually small
1819 * on-stack) kernel array.
1820 * @i: Pointer to iterator that will be initialized on success.
1822 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1823 * then this function places %NULL in *@iov on return. Otherwise, a new
1824 * array will be allocated and the result placed in *@iov. This means that
1825 * the caller may call kfree() on *@iov regardless of whether the small
1826 * on-stack array was used or not (and regardless of whether this function
1827 * returns an error or not).
1829 * Return: Negative error code on error, bytes imported on success
1831 ssize_t import_iovec(int type, const struct iovec __user *uvec,
1832 unsigned nr_segs, unsigned fast_segs,
1833 struct iovec **iovp, struct iov_iter *i)
1835 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1836 in_compat_syscall());
1838 EXPORT_SYMBOL(import_iovec);
1840 int import_single_range(int rw, void __user *buf, size_t len,
1841 struct iovec *iov, struct iov_iter *i)
1843 if (len > MAX_RW_COUNT)
1845 if (unlikely(!access_ok(buf, len)))
1848 iov->iov_base = buf;
1850 iov_iter_init(i, rw, iov, 1, len);
1853 EXPORT_SYMBOL(import_single_range);
1856 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
1857 * iov_iter_save_state() was called.
1859 * @i: &struct iov_iter to restore
1860 * @state: state to restore from
1862 * Used after iov_iter_save_state() to bring restore @i, if operations may
1865 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
1867 void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
1869 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
1870 !iov_iter_is_kvec(i) && !iter_is_ubuf(i))
1872 i->iov_offset = state->iov_offset;
1873 i->count = state->count;
1874 if (iter_is_ubuf(i))
1877 * For the *vec iters, nr_segs + iov is constant - if we increment
1878 * the vec, then we also decrement the nr_segs count. Hence we don't
1879 * need to track both of these, just one is enough and we can deduct
1880 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
1881 * size, so we can just increment the iov pointer as they are unionzed.
1882 * ITER_BVEC _may_ be the same size on some archs, but on others it is
1883 * not. Be safe and handle it separately.
1885 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
1886 if (iov_iter_is_bvec(i))
1887 i->bvec -= state->nr_segs - i->nr_segs;
1889 i->iov -= state->nr_segs - i->nr_segs;
1890 i->nr_segs = state->nr_segs;