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 = iter_iov(i); \
132 iterate_iovec(i, n, base, len, off, \
134 i->nr_segs -= iov - iter_iov(i); \
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 copyout_nofault(void __user *to, const void *from, size_t n)
179 if (should_fail_usercopy())
182 res = copy_to_user_nofault(to, from, n);
184 return res < 0 ? n : res;
187 static int copyin(void *to, const void __user *from, size_t n)
191 if (should_fail_usercopy())
193 if (access_ok(from, n)) {
194 instrument_copy_from_user_before(to, from, n);
195 res = raw_copy_from_user(to, from, n);
196 instrument_copy_from_user_after(to, from, n, res);
202 static bool sanity(const struct iov_iter *i)
204 struct pipe_inode_info *pipe = i->pipe;
205 unsigned int p_head = pipe->head;
206 unsigned int p_tail = pipe->tail;
207 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
208 unsigned int i_head = i->head;
211 if (i->last_offset) {
212 struct pipe_buffer *p;
213 if (unlikely(p_occupancy == 0))
214 goto Bad; // pipe must be non-empty
215 if (unlikely(i_head != p_head - 1))
216 goto Bad; // must be at the last buffer...
218 p = pipe_buf(pipe, i_head);
219 if (unlikely(p->offset + p->len != abs(i->last_offset)))
220 goto Bad; // ... at the end of segment
222 if (i_head != p_head)
223 goto Bad; // must be right after the last buffer
227 printk(KERN_ERR "idx = %d, offset = %d\n", i_head, i->last_offset);
228 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
229 p_head, p_tail, pipe->ring_size);
230 for (idx = 0; idx < pipe->ring_size; idx++)
231 printk(KERN_ERR "[%p %p %d %d]\n",
233 pipe->bufs[idx].page,
234 pipe->bufs[idx].offset,
235 pipe->bufs[idx].len);
240 #define sanity(i) true
243 static struct page *push_anon(struct pipe_inode_info *pipe, unsigned size)
245 struct page *page = alloc_page(GFP_USER);
247 struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
248 *buf = (struct pipe_buffer) {
249 .ops = &default_pipe_buf_ops,
258 static void push_page(struct pipe_inode_info *pipe, struct page *page,
259 unsigned int offset, unsigned int size)
261 struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
262 *buf = (struct pipe_buffer) {
263 .ops = &page_cache_pipe_buf_ops,
271 static inline int last_offset(const struct pipe_buffer *buf)
273 if (buf->ops == &default_pipe_buf_ops)
274 return buf->len; // buf->offset is 0 for those
276 return -(buf->offset + buf->len);
279 static struct page *append_pipe(struct iov_iter *i, size_t size,
282 struct pipe_inode_info *pipe = i->pipe;
283 int offset = i->last_offset;
284 struct pipe_buffer *buf;
287 if (offset > 0 && offset < PAGE_SIZE) {
288 // some space in the last buffer; add to it
289 buf = pipe_buf(pipe, pipe->head - 1);
290 size = min_t(size_t, size, PAGE_SIZE - offset);
292 i->last_offset += size;
297 // OK, we need a new buffer
299 size = min_t(size_t, size, PAGE_SIZE);
300 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
302 page = push_anon(pipe, size);
305 i->head = pipe->head - 1;
306 i->last_offset = size;
311 static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
314 struct pipe_inode_info *pipe = i->pipe;
315 unsigned int head = pipe->head;
317 if (unlikely(bytes > i->count))
320 if (unlikely(!bytes))
326 if (offset && i->last_offset == -offset) { // could we merge it?
327 struct pipe_buffer *buf = pipe_buf(pipe, head - 1);
328 if (buf->page == page) {
330 i->last_offset -= bytes;
335 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
338 push_page(pipe, page, offset, bytes);
339 i->last_offset = -(offset + bytes);
346 * fault_in_iov_iter_readable - fault in iov iterator for reading
348 * @size: maximum length
350 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
351 * @size. For each iovec, fault in each page that constitutes the iovec.
353 * Returns the number of bytes not faulted in (like copy_to_user() and
356 * Always returns 0 for non-userspace iterators.
358 size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
360 if (iter_is_ubuf(i)) {
361 size_t n = min(size, iov_iter_count(i));
362 n -= fault_in_readable(i->ubuf + i->iov_offset, n);
364 } else if (iter_is_iovec(i)) {
365 size_t count = min(size, iov_iter_count(i));
366 const struct iovec *p;
370 for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) {
371 size_t len = min(count, p->iov_len - skip);
376 ret = fault_in_readable(p->iov_base + skip, len);
385 EXPORT_SYMBOL(fault_in_iov_iter_readable);
388 * fault_in_iov_iter_writeable - fault in iov iterator for writing
390 * @size: maximum length
392 * Faults in the iterator using get_user_pages(), i.e., without triggering
393 * hardware page faults. This is primarily useful when we already know that
394 * some or all of the pages in @i aren't in memory.
396 * Returns the number of bytes not faulted in, like copy_to_user() and
399 * Always returns 0 for non-user-space iterators.
401 size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
403 if (iter_is_ubuf(i)) {
404 size_t n = min(size, iov_iter_count(i));
405 n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n);
407 } else if (iter_is_iovec(i)) {
408 size_t count = min(size, iov_iter_count(i));
409 const struct iovec *p;
413 for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) {
414 size_t len = min(count, p->iov_len - skip);
419 ret = fault_in_safe_writeable(p->iov_base + skip, len);
428 EXPORT_SYMBOL(fault_in_iov_iter_writeable);
430 void iov_iter_init(struct iov_iter *i, unsigned int direction,
431 const struct iovec *iov, unsigned long nr_segs,
434 WARN_ON(direction & ~(READ | WRITE));
435 *i = (struct iov_iter) {
436 .iter_type = ITER_IOVEC,
440 .data_source = direction,
447 EXPORT_SYMBOL(iov_iter_init);
449 // returns the offset in partial buffer (if any)
450 static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages)
452 struct pipe_inode_info *pipe = i->pipe;
453 int used = pipe->head - pipe->tail;
454 int off = i->last_offset;
456 *npages = max((int)pipe->max_usage - used, 0);
458 if (off > 0 && off < PAGE_SIZE) { // anon and not full
465 static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
468 unsigned int off, chunk;
470 if (unlikely(bytes > i->count))
472 if (unlikely(!bytes))
478 for (size_t n = bytes; n; n -= chunk) {
479 struct page *page = append_pipe(i, n, &off);
480 chunk = min_t(size_t, n, PAGE_SIZE - off);
483 memcpy_to_page(page, off, addr, chunk);
489 static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
490 __wsum sum, size_t off)
492 __wsum next = csum_partial_copy_nocheck(from, to, len);
493 return csum_block_add(sum, next, off);
496 static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
497 struct iov_iter *i, __wsum *sump)
501 unsigned int chunk, r;
503 if (unlikely(bytes > i->count))
505 if (unlikely(!bytes))
512 struct page *page = append_pipe(i, bytes, &r);
517 chunk = min_t(size_t, bytes, PAGE_SIZE - r);
518 p = kmap_local_page(page);
519 sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
528 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
530 if (WARN_ON_ONCE(i->data_source))
532 if (unlikely(iov_iter_is_pipe(i)))
533 return copy_pipe_to_iter(addr, bytes, i);
534 if (user_backed_iter(i))
536 iterate_and_advance(i, bytes, base, len, off,
537 copyout(base, addr + off, len),
538 memcpy(base, addr + off, len)
543 EXPORT_SYMBOL(_copy_to_iter);
545 #ifdef CONFIG_ARCH_HAS_COPY_MC
546 static int copyout_mc(void __user *to, const void *from, size_t n)
548 if (access_ok(to, n)) {
549 instrument_copy_to_user(to, from, n);
550 n = copy_mc_to_user((__force void *) to, from, n);
555 static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
559 unsigned int off, chunk;
561 if (unlikely(bytes > i->count))
563 if (unlikely(!bytes))
570 struct page *page = append_pipe(i, bytes, &off);
576 chunk = min_t(size_t, bytes, PAGE_SIZE - off);
577 p = kmap_local_page(page);
578 rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
584 iov_iter_revert(i, rem);
592 * _copy_mc_to_iter - copy to iter with source memory error exception handling
593 * @addr: source kernel address
594 * @bytes: total transfer length
595 * @i: destination iterator
597 * The pmem driver deploys this for the dax operation
598 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
599 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
600 * successfully copied.
602 * The main differences between this and typical _copy_to_iter().
604 * * Typical tail/residue handling after a fault retries the copy
605 * byte-by-byte until the fault happens again. Re-triggering machine
606 * checks is potentially fatal so the implementation uses source
607 * alignment and poison alignment assumptions to avoid re-triggering
608 * hardware exceptions.
610 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
611 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
614 * Return: number of bytes copied (may be %0)
616 size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
618 if (WARN_ON_ONCE(i->data_source))
620 if (unlikely(iov_iter_is_pipe(i)))
621 return copy_mc_pipe_to_iter(addr, bytes, i);
622 if (user_backed_iter(i))
624 __iterate_and_advance(i, bytes, base, len, off,
625 copyout_mc(base, addr + off, len),
626 copy_mc_to_kernel(base, addr + off, len)
631 EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
632 #endif /* CONFIG_ARCH_HAS_COPY_MC */
634 static void *memcpy_from_iter(struct iov_iter *i, void *to, const void *from,
637 if (iov_iter_is_copy_mc(i))
638 return (void *)copy_mc_to_kernel(to, from, size);
639 return memcpy(to, from, size);
642 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
644 if (WARN_ON_ONCE(!i->data_source))
647 if (user_backed_iter(i))
649 iterate_and_advance(i, bytes, base, len, off,
650 copyin(addr + off, base, len),
651 memcpy_from_iter(i, addr + off, base, len)
656 EXPORT_SYMBOL(_copy_from_iter);
658 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
660 if (WARN_ON_ONCE(!i->data_source))
663 iterate_and_advance(i, bytes, base, len, off,
664 __copy_from_user_inatomic_nocache(addr + off, base, len),
665 memcpy(addr + off, base, len)
670 EXPORT_SYMBOL(_copy_from_iter_nocache);
672 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
674 * _copy_from_iter_flushcache - write destination through cpu cache
675 * @addr: destination kernel address
676 * @bytes: total transfer length
677 * @i: source iterator
679 * The pmem driver arranges for filesystem-dax to use this facility via
680 * dax_copy_from_iter() for ensuring that writes to persistent memory
681 * are flushed through the CPU cache. It is differentiated from
682 * _copy_from_iter_nocache() in that guarantees all data is flushed for
683 * all iterator types. The _copy_from_iter_nocache() only attempts to
684 * bypass the cache for the ITER_IOVEC case, and on some archs may use
685 * instructions that strand dirty-data in the cache.
687 * Return: number of bytes copied (may be %0)
689 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
691 if (WARN_ON_ONCE(!i->data_source))
694 iterate_and_advance(i, bytes, base, len, off,
695 __copy_from_user_flushcache(addr + off, base, len),
696 memcpy_flushcache(addr + off, base, len)
701 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
704 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
707 size_t v = n + offset;
710 * The general case needs to access the page order in order
711 * to compute the page size.
712 * However, we mostly deal with order-0 pages and thus can
713 * avoid a possible cache line miss for requests that fit all
716 if (n <= v && v <= PAGE_SIZE)
719 head = compound_head(page);
720 v += (page - head) << PAGE_SHIFT;
722 if (WARN_ON(n > v || v > page_size(head)))
727 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
731 if (!page_copy_sane(page, offset, bytes))
733 if (WARN_ON_ONCE(i->data_source))
735 if (unlikely(iov_iter_is_pipe(i)))
736 return copy_page_to_iter_pipe(page, offset, bytes, i);
737 page += offset / PAGE_SIZE; // first subpage
740 void *kaddr = kmap_local_page(page);
741 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
742 n = _copy_to_iter(kaddr + offset, n, i);
749 if (offset == PAGE_SIZE) {
756 EXPORT_SYMBOL(copy_page_to_iter);
758 size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, size_t bytes,
763 if (!page_copy_sane(page, offset, bytes))
765 if (WARN_ON_ONCE(i->data_source))
767 if (unlikely(iov_iter_is_pipe(i)))
768 return copy_page_to_iter_pipe(page, offset, bytes, i);
769 page += offset / PAGE_SIZE; // first subpage
772 void *kaddr = kmap_local_page(page);
773 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
775 iterate_and_advance(i, n, base, len, off,
776 copyout_nofault(base, kaddr + offset + off, len),
777 memcpy(base, kaddr + offset + off, len)
785 if (offset == PAGE_SIZE) {
792 EXPORT_SYMBOL(copy_page_to_iter_nofault);
794 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
798 if (!page_copy_sane(page, offset, bytes))
800 page += offset / PAGE_SIZE; // first subpage
803 void *kaddr = kmap_local_page(page);
804 size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
805 n = _copy_from_iter(kaddr + offset, n, i);
812 if (offset == PAGE_SIZE) {
819 EXPORT_SYMBOL(copy_page_from_iter);
821 static size_t pipe_zero(size_t bytes, struct iov_iter *i)
823 unsigned int chunk, off;
825 if (unlikely(bytes > i->count))
827 if (unlikely(!bytes))
833 for (size_t n = bytes; n; n -= chunk) {
834 struct page *page = append_pipe(i, n, &off);
839 chunk = min_t(size_t, n, PAGE_SIZE - off);
840 p = kmap_local_page(page);
841 memset(p + off, 0, chunk);
847 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
849 if (unlikely(iov_iter_is_pipe(i)))
850 return pipe_zero(bytes, i);
851 iterate_and_advance(i, bytes, base, len, count,
852 clear_user(base, len),
858 EXPORT_SYMBOL(iov_iter_zero);
860 size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
863 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
864 if (!page_copy_sane(page, offset, bytes)) {
865 kunmap_atomic(kaddr);
868 if (WARN_ON_ONCE(!i->data_source)) {
869 kunmap_atomic(kaddr);
872 iterate_and_advance(i, bytes, base, len, off,
873 copyin(p + off, base, len),
874 memcpy_from_iter(i, p + off, base, len)
876 kunmap_atomic(kaddr);
879 EXPORT_SYMBOL(copy_page_from_iter_atomic);
881 static void pipe_advance(struct iov_iter *i, size_t size)
883 struct pipe_inode_info *pipe = i->pipe;
884 int off = i->last_offset;
887 pipe_discard_from(pipe, i->start_head); // discard everything
892 struct pipe_buffer *buf = pipe_buf(pipe, i->head);
893 if (off) /* make it relative to the beginning of buffer */
894 size += abs(off) - buf->offset;
895 if (size <= buf->len) {
897 i->last_offset = last_offset(buf);
904 pipe_discard_from(pipe, i->head + 1); // discard everything past this one
907 static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
909 const struct bio_vec *bvec, *end;
915 size += i->iov_offset;
917 for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
918 if (likely(size < bvec->bv_len))
920 size -= bvec->bv_len;
922 i->iov_offset = size;
923 i->nr_segs -= bvec - i->bvec;
927 static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
929 const struct iovec *iov, *end;
935 size += i->iov_offset; // from beginning of current segment
936 for (iov = iter_iov(i), end = iov + i->nr_segs; iov < end; iov++) {
937 if (likely(size < iov->iov_len))
939 size -= iov->iov_len;
941 i->iov_offset = size;
942 i->nr_segs -= iov - iter_iov(i);
946 void iov_iter_advance(struct iov_iter *i, size_t size)
948 if (unlikely(i->count < size))
950 if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
951 i->iov_offset += size;
953 } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
954 /* iovec and kvec have identical layouts */
955 iov_iter_iovec_advance(i, size);
956 } else if (iov_iter_is_bvec(i)) {
957 iov_iter_bvec_advance(i, size);
958 } else if (iov_iter_is_pipe(i)) {
959 pipe_advance(i, size);
960 } else if (iov_iter_is_discard(i)) {
964 EXPORT_SYMBOL(iov_iter_advance);
966 void iov_iter_revert(struct iov_iter *i, size_t unroll)
970 if (WARN_ON(unroll > MAX_RW_COUNT))
973 if (unlikely(iov_iter_is_pipe(i))) {
974 struct pipe_inode_info *pipe = i->pipe;
975 unsigned int head = pipe->head;
977 while (head > i->start_head) {
978 struct pipe_buffer *b = pipe_buf(pipe, --head);
979 if (unroll < b->len) {
981 i->last_offset = last_offset(b);
986 pipe_buf_release(pipe, b);
993 if (unlikely(iov_iter_is_discard(i)))
995 if (unroll <= i->iov_offset) {
996 i->iov_offset -= unroll;
999 unroll -= i->iov_offset;
1000 if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
1001 BUG(); /* We should never go beyond the start of the specified
1002 * range since we might then be straying into pages that
1005 } else if (iov_iter_is_bvec(i)) {
1006 const struct bio_vec *bvec = i->bvec;
1008 size_t n = (--bvec)->bv_len;
1012 i->iov_offset = n - unroll;
1017 } else { /* same logics for iovec and kvec */
1018 const struct iovec *iov = iter_iov(i);
1020 size_t n = (--iov)->iov_len;
1024 i->iov_offset = n - unroll;
1031 EXPORT_SYMBOL(iov_iter_revert);
1034 * Return the count of just the current iov_iter segment.
1036 size_t iov_iter_single_seg_count(const struct iov_iter *i)
1038 if (i->nr_segs > 1) {
1039 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1040 return min(i->count, iter_iov(i)->iov_len - i->iov_offset);
1041 if (iov_iter_is_bvec(i))
1042 return min(i->count, i->bvec->bv_len - i->iov_offset);
1046 EXPORT_SYMBOL(iov_iter_single_seg_count);
1048 void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
1049 const struct kvec *kvec, unsigned long nr_segs,
1052 WARN_ON(direction & ~(READ | WRITE));
1053 *i = (struct iov_iter){
1054 .iter_type = ITER_KVEC,
1056 .data_source = direction,
1063 EXPORT_SYMBOL(iov_iter_kvec);
1065 void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1066 const struct bio_vec *bvec, unsigned long nr_segs,
1069 WARN_ON(direction & ~(READ | WRITE));
1070 *i = (struct iov_iter){
1071 .iter_type = ITER_BVEC,
1073 .data_source = direction,
1080 EXPORT_SYMBOL(iov_iter_bvec);
1082 void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1083 struct pipe_inode_info *pipe,
1086 BUG_ON(direction != READ);
1087 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
1088 *i = (struct iov_iter){
1089 .iter_type = ITER_PIPE,
1090 .data_source = false,
1093 .start_head = pipe->head,
1098 EXPORT_SYMBOL(iov_iter_pipe);
1101 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1102 * @i: The iterator to initialise.
1103 * @direction: The direction of the transfer.
1104 * @xarray: The xarray to access.
1105 * @start: The start file position.
1106 * @count: The size of the I/O buffer in bytes.
1108 * Set up an I/O iterator to either draw data out of the pages attached to an
1109 * inode or to inject data into those pages. The pages *must* be prevented
1110 * from evaporation, either by taking a ref on them or locking them by the
1113 void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1114 struct xarray *xarray, loff_t start, size_t count)
1116 BUG_ON(direction & ~1);
1117 *i = (struct iov_iter) {
1118 .iter_type = ITER_XARRAY,
1120 .data_source = direction,
1122 .xarray_start = start,
1127 EXPORT_SYMBOL(iov_iter_xarray);
1130 * iov_iter_discard - Initialise an I/O iterator that discards data
1131 * @i: The iterator to initialise.
1132 * @direction: The direction of the transfer.
1133 * @count: The size of the I/O buffer in bytes.
1135 * Set up an I/O iterator that just discards everything that's written to it.
1136 * It's only available as a READ iterator.
1138 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1140 BUG_ON(direction != READ);
1141 *i = (struct iov_iter){
1142 .iter_type = ITER_DISCARD,
1144 .data_source = false,
1149 EXPORT_SYMBOL(iov_iter_discard);
1151 static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
1154 size_t size = i->count;
1155 size_t skip = i->iov_offset;
1158 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1159 const struct iovec *iov = iter_iov(i) + k;
1160 size_t len = iov->iov_len - skip;
1166 if ((unsigned long)(iov->iov_base + skip) & addr_mask)
1176 static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
1179 size_t size = i->count;
1180 unsigned skip = i->iov_offset;
1183 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1184 size_t len = i->bvec[k].bv_len - skip;
1190 if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
1201 * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
1202 * are aligned to the parameters.
1204 * @i: &struct iov_iter to restore
1205 * @addr_mask: bit mask to check against the iov element's addresses
1206 * @len_mask: bit mask to check against the iov element's lengths
1208 * Return: false if any addresses or lengths intersect with the provided masks
1210 bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
1213 if (likely(iter_is_ubuf(i))) {
1214 if (i->count & len_mask)
1216 if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
1221 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1222 return iov_iter_aligned_iovec(i, addr_mask, len_mask);
1224 if (iov_iter_is_bvec(i))
1225 return iov_iter_aligned_bvec(i, addr_mask, len_mask);
1227 if (iov_iter_is_pipe(i)) {
1228 size_t size = i->count;
1230 if (size & len_mask)
1232 if (size && i->last_offset > 0) {
1233 if (i->last_offset & addr_mask)
1240 if (iov_iter_is_xarray(i)) {
1241 if (i->count & len_mask)
1243 if ((i->xarray_start + i->iov_offset) & addr_mask)
1249 EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
1251 static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1253 unsigned long res = 0;
1254 size_t size = i->count;
1255 size_t skip = i->iov_offset;
1258 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1259 const struct iovec *iov = iter_iov(i) + k;
1260 size_t len = iov->iov_len - skip;
1262 res |= (unsigned long)iov->iov_base + skip;
1274 static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
1277 size_t size = i->count;
1278 unsigned skip = i->iov_offset;
1281 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1282 size_t len = i->bvec[k].bv_len - skip;
1283 res |= (unsigned long)i->bvec[k].bv_offset + skip;
1294 unsigned long iov_iter_alignment(const struct iov_iter *i)
1296 if (likely(iter_is_ubuf(i))) {
1297 size_t size = i->count;
1299 return ((unsigned long)i->ubuf + i->iov_offset) | size;
1303 /* iovec and kvec have identical layouts */
1304 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1305 return iov_iter_alignment_iovec(i);
1307 if (iov_iter_is_bvec(i))
1308 return iov_iter_alignment_bvec(i);
1310 if (iov_iter_is_pipe(i)) {
1311 size_t size = i->count;
1313 if (size && i->last_offset > 0)
1314 return size | i->last_offset;
1318 if (iov_iter_is_xarray(i))
1319 return (i->xarray_start + i->iov_offset) | i->count;
1323 EXPORT_SYMBOL(iov_iter_alignment);
1325 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1327 unsigned long res = 0;
1328 unsigned long v = 0;
1329 size_t size = i->count;
1332 if (iter_is_ubuf(i))
1335 if (WARN_ON(!iter_is_iovec(i)))
1338 for (k = 0; k < i->nr_segs; k++) {
1339 const struct iovec *iov = iter_iov(i) + k;
1341 unsigned long base = (unsigned long)iov->iov_base;
1342 if (v) // if not the first one
1343 res |= base | v; // this start | previous end
1344 v = base + iov->iov_len;
1345 if (size <= iov->iov_len)
1347 size -= iov->iov_len;
1352 EXPORT_SYMBOL(iov_iter_gap_alignment);
1354 static int want_pages_array(struct page ***res, size_t size,
1355 size_t start, unsigned int maxpages)
1357 unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE);
1359 if (count > maxpages)
1361 WARN_ON(!count); // caller should've prevented that
1363 *res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
1370 static ssize_t pipe_get_pages(struct iov_iter *i,
1371 struct page ***pages, size_t maxsize, unsigned maxpages,
1374 unsigned int npages, count, off, chunk;
1381 *start = off = pipe_npages(i, &npages);
1384 count = want_pages_array(pages, maxsize, off, min(npages, maxpages));
1388 for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) {
1389 struct page *page = append_pipe(i, left, &off);
1392 chunk = min_t(size_t, left, PAGE_SIZE - off);
1393 get_page(*p++ = page);
1397 return maxsize - left;
1400 static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1401 pgoff_t index, unsigned int nr_pages)
1403 XA_STATE(xas, xa, index);
1405 unsigned int ret = 0;
1408 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1409 if (xas_retry(&xas, page))
1412 /* Has the page moved or been split? */
1413 if (unlikely(page != xas_reload(&xas))) {
1418 pages[ret] = find_subpage(page, xas.xa_index);
1419 get_page(pages[ret]);
1420 if (++ret == nr_pages)
1427 static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1428 struct page ***pages, size_t maxsize,
1429 unsigned maxpages, size_t *_start_offset)
1431 unsigned nr, offset, count;
1435 pos = i->xarray_start + i->iov_offset;
1436 index = pos >> PAGE_SHIFT;
1437 offset = pos & ~PAGE_MASK;
1438 *_start_offset = offset;
1440 count = want_pages_array(pages, maxsize, offset, maxpages);
1443 nr = iter_xarray_populate_pages(*pages, i->xarray, index, count);
1447 maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1448 i->iov_offset += maxsize;
1449 i->count -= maxsize;
1453 /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
1454 static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
1459 if (iter_is_ubuf(i))
1460 return (unsigned long)i->ubuf + i->iov_offset;
1462 for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1463 const struct iovec *iov = iter_iov(i) + k;
1464 size_t len = iov->iov_len - skip;
1470 return (unsigned long)iov->iov_base + skip;
1472 BUG(); // if it had been empty, we wouldn't get called
1475 /* must be done on non-empty ITER_BVEC one */
1476 static struct page *first_bvec_segment(const struct iov_iter *i,
1477 size_t *size, size_t *start)
1480 size_t skip = i->iov_offset, len;
1482 len = i->bvec->bv_len - skip;
1485 skip += i->bvec->bv_offset;
1486 page = i->bvec->bv_page + skip / PAGE_SIZE;
1487 *start = skip % PAGE_SIZE;
1491 static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
1492 struct page ***pages, size_t maxsize,
1493 unsigned int maxpages, size_t *start,
1494 iov_iter_extraction_t extraction_flags)
1496 unsigned int n, gup_flags = 0;
1498 if (maxsize > i->count)
1502 if (maxsize > MAX_RW_COUNT)
1503 maxsize = MAX_RW_COUNT;
1504 if (extraction_flags & ITER_ALLOW_P2PDMA)
1505 gup_flags |= FOLL_PCI_P2PDMA;
1507 if (likely(user_backed_iter(i))) {
1511 if (iov_iter_rw(i) != WRITE)
1512 gup_flags |= FOLL_WRITE;
1514 gup_flags |= FOLL_NOFAULT;
1516 addr = first_iovec_segment(i, &maxsize);
1517 *start = addr % PAGE_SIZE;
1519 n = want_pages_array(pages, maxsize, *start, maxpages);
1522 res = get_user_pages_fast(addr, n, gup_flags, *pages);
1523 if (unlikely(res <= 0))
1525 maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1526 iov_iter_advance(i, maxsize);
1529 if (iov_iter_is_bvec(i)) {
1533 page = first_bvec_segment(i, &maxsize, start);
1534 n = want_pages_array(pages, maxsize, *start, maxpages);
1538 for (int k = 0; k < n; k++)
1539 get_page(p[k] = page + k);
1540 maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1541 i->count -= maxsize;
1542 i->iov_offset += maxsize;
1543 if (i->iov_offset == i->bvec->bv_len) {
1550 if (iov_iter_is_pipe(i))
1551 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1552 if (iov_iter_is_xarray(i))
1553 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1557 ssize_t iov_iter_get_pages(struct iov_iter *i,
1558 struct page **pages, size_t maxsize, unsigned maxpages,
1559 size_t *start, iov_iter_extraction_t extraction_flags)
1565 return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages,
1566 start, extraction_flags);
1568 EXPORT_SYMBOL_GPL(iov_iter_get_pages);
1570 ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
1571 size_t maxsize, unsigned maxpages, size_t *start)
1573 return iov_iter_get_pages(i, pages, maxsize, maxpages, start, 0);
1575 EXPORT_SYMBOL(iov_iter_get_pages2);
1577 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1578 struct page ***pages, size_t maxsize,
1579 size_t *start, iov_iter_extraction_t extraction_flags)
1585 len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start,
1593 EXPORT_SYMBOL_GPL(iov_iter_get_pages_alloc);
1595 ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i,
1596 struct page ***pages, size_t maxsize, size_t *start)
1598 return iov_iter_get_pages_alloc(i, pages, maxsize, start, 0);
1600 EXPORT_SYMBOL(iov_iter_get_pages_alloc2);
1602 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1607 if (WARN_ON_ONCE(!i->data_source))
1610 iterate_and_advance(i, bytes, base, len, off, ({
1611 next = csum_and_copy_from_user(base, addr + off, len);
1612 sum = csum_block_add(sum, next, off);
1615 sum = csum_and_memcpy(addr + off, base, len, sum, off);
1621 EXPORT_SYMBOL(csum_and_copy_from_iter);
1623 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1626 struct csum_state *csstate = _csstate;
1629 if (WARN_ON_ONCE(i->data_source))
1631 if (unlikely(iov_iter_is_discard(i))) {
1632 // can't use csum_memcpy() for that one - data is not copied
1633 csstate->csum = csum_block_add(csstate->csum,
1634 csum_partial(addr, bytes, 0),
1636 csstate->off += bytes;
1640 sum = csum_shift(csstate->csum, csstate->off);
1641 if (unlikely(iov_iter_is_pipe(i)))
1642 bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
1643 else iterate_and_advance(i, bytes, base, len, off, ({
1644 next = csum_and_copy_to_user(addr + off, base, len);
1645 sum = csum_block_add(sum, next, off);
1648 sum = csum_and_memcpy(base, addr + off, len, sum, off);
1651 csstate->csum = csum_shift(sum, csstate->off);
1652 csstate->off += bytes;
1655 EXPORT_SYMBOL(csum_and_copy_to_iter);
1657 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1660 #ifdef CONFIG_CRYPTO_HASH
1661 struct ahash_request *hash = hashp;
1662 struct scatterlist sg;
1665 copied = copy_to_iter(addr, bytes, i);
1666 sg_init_one(&sg, addr, copied);
1667 ahash_request_set_crypt(hash, &sg, NULL, copied);
1668 crypto_ahash_update(hash);
1674 EXPORT_SYMBOL(hash_and_copy_to_iter);
1676 static int iov_npages(const struct iov_iter *i, int maxpages)
1678 size_t skip = i->iov_offset, size = i->count;
1679 const struct iovec *p;
1682 for (p = iter_iov(i); size; skip = 0, p++) {
1683 unsigned offs = offset_in_page(p->iov_base + skip);
1684 size_t len = min(p->iov_len - skip, size);
1688 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1689 if (unlikely(npages > maxpages))
1696 static int bvec_npages(const struct iov_iter *i, int maxpages)
1698 size_t skip = i->iov_offset, size = i->count;
1699 const struct bio_vec *p;
1702 for (p = i->bvec; size; skip = 0, p++) {
1703 unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
1704 size_t len = min(p->bv_len - skip, size);
1707 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1708 if (unlikely(npages > maxpages))
1714 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1716 if (unlikely(!i->count))
1718 if (likely(iter_is_ubuf(i))) {
1719 unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1720 int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1721 return min(npages, maxpages);
1723 /* iovec and kvec have identical layouts */
1724 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1725 return iov_npages(i, maxpages);
1726 if (iov_iter_is_bvec(i))
1727 return bvec_npages(i, maxpages);
1728 if (iov_iter_is_pipe(i)) {
1734 pipe_npages(i, &npages);
1735 return min(npages, maxpages);
1737 if (iov_iter_is_xarray(i)) {
1738 unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1739 int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
1740 return min(npages, maxpages);
1744 EXPORT_SYMBOL(iov_iter_npages);
1746 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1749 if (unlikely(iov_iter_is_pipe(new))) {
1753 if (iov_iter_is_bvec(new))
1754 return new->bvec = kmemdup(new->bvec,
1755 new->nr_segs * sizeof(struct bio_vec),
1757 else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1758 /* iovec and kvec have identical layout */
1759 return new->__iov = kmemdup(new->__iov,
1760 new->nr_segs * sizeof(struct iovec),
1764 EXPORT_SYMBOL(dup_iter);
1766 static __noclone int copy_compat_iovec_from_user(struct iovec *iov,
1767 const struct iovec __user *uvec, unsigned long nr_segs)
1769 const struct compat_iovec __user *uiov =
1770 (const struct compat_iovec __user *)uvec;
1771 int ret = -EFAULT, i;
1773 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1776 for (i = 0; i < nr_segs; i++) {
1780 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1781 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1783 /* check for compat_size_t not fitting in compat_ssize_t .. */
1788 iov[i].iov_base = compat_ptr(buf);
1789 iov[i].iov_len = len;
1798 static int copy_iovec_from_user(struct iovec *iov,
1799 const struct iovec __user *uiov, unsigned long nr_segs)
1803 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1810 unsafe_get_user(len, &uiov->iov_len, uaccess_end);
1811 unsafe_get_user(buf, &uiov->iov_base, uaccess_end);
1813 /* check for size_t not fitting in ssize_t .. */
1814 if (unlikely(len < 0)) {
1818 iov->iov_base = buf;
1822 } while (--nr_segs);
1830 struct iovec *iovec_from_user(const struct iovec __user *uvec,
1831 unsigned long nr_segs, unsigned long fast_segs,
1832 struct iovec *fast_iov, bool compat)
1834 struct iovec *iov = fast_iov;
1838 * SuS says "The readv() function *may* fail if the iovcnt argument was
1839 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
1840 * traditionally returned zero for zero segments, so...
1844 if (nr_segs > UIO_MAXIOV)
1845 return ERR_PTR(-EINVAL);
1846 if (nr_segs > fast_segs) {
1847 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1849 return ERR_PTR(-ENOMEM);
1852 if (unlikely(compat))
1853 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1855 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1857 if (iov != fast_iov)
1859 return ERR_PTR(ret);
1866 * Single segment iovec supplied by the user, import it as ITER_UBUF.
1868 static ssize_t __import_iovec_ubuf(int type, const struct iovec __user *uvec,
1869 struct iovec **iovp, struct iov_iter *i,
1872 struct iovec *iov = *iovp;
1876 ret = copy_compat_iovec_from_user(iov, uvec, 1);
1878 ret = copy_iovec_from_user(iov, uvec, 1);
1882 ret = import_ubuf(type, iov->iov_base, iov->iov_len, i);
1889 ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1890 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1891 struct iov_iter *i, bool compat)
1893 ssize_t total_len = 0;
1898 return __import_iovec_ubuf(type, uvec, iovp, i, compat);
1900 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1903 return PTR_ERR(iov);
1907 * According to the Single Unix Specification we should return EINVAL if
1908 * an element length is < 0 when cast to ssize_t or if the total length
1909 * would overflow the ssize_t return value of the system call.
1911 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1914 for (seg = 0; seg < nr_segs; seg++) {
1915 ssize_t len = (ssize_t)iov[seg].iov_len;
1917 if (!access_ok(iov[seg].iov_base, len)) {
1924 if (len > MAX_RW_COUNT - total_len) {
1925 len = MAX_RW_COUNT - total_len;
1926 iov[seg].iov_len = len;
1931 iov_iter_init(i, type, iov, nr_segs, total_len);
1940 * import_iovec() - Copy an array of &struct iovec from userspace
1941 * into the kernel, check that it is valid, and initialize a new
1942 * &struct iov_iter iterator to access it.
1944 * @type: One of %READ or %WRITE.
1945 * @uvec: Pointer to the userspace array.
1946 * @nr_segs: Number of elements in userspace array.
1947 * @fast_segs: Number of elements in @iov.
1948 * @iovp: (input and output parameter) Pointer to pointer to (usually small
1949 * on-stack) kernel array.
1950 * @i: Pointer to iterator that will be initialized on success.
1952 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1953 * then this function places %NULL in *@iov on return. Otherwise, a new
1954 * array will be allocated and the result placed in *@iov. This means that
1955 * the caller may call kfree() on *@iov regardless of whether the small
1956 * on-stack array was used or not (and regardless of whether this function
1957 * returns an error or not).
1959 * Return: Negative error code on error, bytes imported on success
1961 ssize_t import_iovec(int type, const struct iovec __user *uvec,
1962 unsigned nr_segs, unsigned fast_segs,
1963 struct iovec **iovp, struct iov_iter *i)
1965 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1966 in_compat_syscall());
1968 EXPORT_SYMBOL(import_iovec);
1970 int import_single_range(int rw, void __user *buf, size_t len,
1971 struct iovec *iov, struct iov_iter *i)
1973 if (len > MAX_RW_COUNT)
1975 if (unlikely(!access_ok(buf, len)))
1978 iov_iter_ubuf(i, rw, buf, len);
1981 EXPORT_SYMBOL(import_single_range);
1983 int import_ubuf(int rw, void __user *buf, size_t len, struct iov_iter *i)
1985 if (len > MAX_RW_COUNT)
1987 if (unlikely(!access_ok(buf, len)))
1990 iov_iter_ubuf(i, rw, buf, len);
1995 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
1996 * iov_iter_save_state() was called.
1998 * @i: &struct iov_iter to restore
1999 * @state: state to restore from
2001 * Used after iov_iter_save_state() to bring restore @i, if operations may
2004 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
2006 void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
2008 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) &&
2009 !iter_is_ubuf(i)) && !iov_iter_is_kvec(i))
2011 i->iov_offset = state->iov_offset;
2012 i->count = state->count;
2013 if (iter_is_ubuf(i))
2016 * For the *vec iters, nr_segs + iov is constant - if we increment
2017 * the vec, then we also decrement the nr_segs count. Hence we don't
2018 * need to track both of these, just one is enough and we can deduct
2019 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
2020 * size, so we can just increment the iov pointer as they are unionzed.
2021 * ITER_BVEC _may_ be the same size on some archs, but on others it is
2022 * not. Be safe and handle it separately.
2024 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
2025 if (iov_iter_is_bvec(i))
2026 i->bvec -= state->nr_segs - i->nr_segs;
2028 i->__iov -= state->nr_segs - i->nr_segs;
2029 i->nr_segs = state->nr_segs;
2033 * Extract a list of contiguous pages from an ITER_XARRAY iterator. This does not
2034 * get references on the pages, nor does it get a pin on them.
2036 static ssize_t iov_iter_extract_xarray_pages(struct iov_iter *i,
2037 struct page ***pages, size_t maxsize,
2038 unsigned int maxpages,
2039 iov_iter_extraction_t extraction_flags,
2042 struct page *page, **p;
2043 unsigned int nr = 0, offset;
2044 loff_t pos = i->xarray_start + i->iov_offset;
2045 pgoff_t index = pos >> PAGE_SHIFT;
2046 XA_STATE(xas, i->xarray, index);
2048 offset = pos & ~PAGE_MASK;
2051 maxpages = want_pages_array(pages, maxsize, offset, maxpages);
2057 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
2058 if (xas_retry(&xas, page))
2061 /* Has the page moved or been split? */
2062 if (unlikely(page != xas_reload(&xas))) {
2067 p[nr++] = find_subpage(page, xas.xa_index);
2073 maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
2074 iov_iter_advance(i, maxsize);
2079 * Extract a list of contiguous pages from an ITER_BVEC iterator. This does
2080 * not get references on the pages, nor does it get a pin on them.
2082 static ssize_t iov_iter_extract_bvec_pages(struct iov_iter *i,
2083 struct page ***pages, size_t maxsize,
2084 unsigned int maxpages,
2085 iov_iter_extraction_t extraction_flags,
2088 struct page **p, *page;
2089 size_t skip = i->iov_offset, offset;
2093 if (i->nr_segs == 0)
2095 maxsize = min(maxsize, i->bvec->bv_len - skip);
2104 skip += i->bvec->bv_offset;
2105 page = i->bvec->bv_page + skip / PAGE_SIZE;
2106 offset = skip % PAGE_SIZE;
2109 maxpages = want_pages_array(pages, maxsize, offset, maxpages);
2113 for (k = 0; k < maxpages; k++)
2116 maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset);
2117 iov_iter_advance(i, maxsize);
2122 * Extract a list of virtually contiguous pages from an ITER_KVEC iterator.
2123 * This does not get references on the pages, nor does it get a pin on them.
2125 static ssize_t iov_iter_extract_kvec_pages(struct iov_iter *i,
2126 struct page ***pages, size_t maxsize,
2127 unsigned int maxpages,
2128 iov_iter_extraction_t extraction_flags,
2131 struct page **p, *page;
2133 size_t skip = i->iov_offset, offset, len;
2137 if (i->nr_segs == 0)
2139 maxsize = min(maxsize, i->kvec->iov_len - skip);
2148 kaddr = i->kvec->iov_base + skip;
2149 offset = (unsigned long)kaddr & ~PAGE_MASK;
2152 maxpages = want_pages_array(pages, maxsize, offset, maxpages);
2158 len = offset + maxsize;
2159 for (k = 0; k < maxpages; k++) {
2160 size_t seg = min_t(size_t, len, PAGE_SIZE);
2162 if (is_vmalloc_or_module_addr(kaddr))
2163 page = vmalloc_to_page(kaddr);
2165 page = virt_to_page(kaddr);
2172 maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset);
2173 iov_iter_advance(i, maxsize);
2178 * Extract a list of contiguous pages from a user iterator and get a pin on
2179 * each of them. This should only be used if the iterator is user-backed
2182 * It does not get refs on the pages, but the pages must be unpinned by the
2183 * caller once the transfer is complete.
2185 * This is safe to be used where background IO/DMA *is* going to be modifying
2186 * the buffer; using a pin rather than a ref makes forces fork() to give the
2187 * child a copy of the page.
2189 static ssize_t iov_iter_extract_user_pages(struct iov_iter *i,
2190 struct page ***pages,
2192 unsigned int maxpages,
2193 iov_iter_extraction_t extraction_flags,
2197 unsigned int gup_flags = 0;
2201 if (i->data_source == ITER_DEST)
2202 gup_flags |= FOLL_WRITE;
2203 if (extraction_flags & ITER_ALLOW_P2PDMA)
2204 gup_flags |= FOLL_PCI_P2PDMA;
2206 gup_flags |= FOLL_NOFAULT;
2208 addr = first_iovec_segment(i, &maxsize);
2209 *offset0 = offset = addr % PAGE_SIZE;
2211 maxpages = want_pages_array(pages, maxsize, offset, maxpages);
2214 res = pin_user_pages_fast(addr, maxpages, gup_flags, *pages);
2215 if (unlikely(res <= 0))
2217 maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - offset);
2218 iov_iter_advance(i, maxsize);
2223 * iov_iter_extract_pages - Extract a list of contiguous pages from an iterator
2224 * @i: The iterator to extract from
2225 * @pages: Where to return the list of pages
2226 * @maxsize: The maximum amount of iterator to extract
2227 * @maxpages: The maximum size of the list of pages
2228 * @extraction_flags: Flags to qualify request
2229 * @offset0: Where to return the starting offset into (*@pages)[0]
2231 * Extract a list of contiguous pages from the current point of the iterator,
2232 * advancing the iterator. The maximum number of pages and the maximum amount
2233 * of page contents can be set.
2235 * If *@pages is NULL, a page list will be allocated to the required size and
2236 * *@pages will be set to its base. If *@pages is not NULL, it will be assumed
2237 * that the caller allocated a page list at least @maxpages in size and this
2238 * will be filled in.
2240 * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA
2241 * be allowed on the pages extracted.
2243 * The iov_iter_extract_will_pin() function can be used to query how cleanup
2244 * should be performed.
2246 * Extra refs or pins on the pages may be obtained as follows:
2248 * (*) If the iterator is user-backed (ITER_IOVEC/ITER_UBUF), pins will be
2249 * added to the pages, but refs will not be taken.
2250 * iov_iter_extract_will_pin() will return true.
2252 * (*) If the iterator is ITER_KVEC, ITER_BVEC or ITER_XARRAY, the pages are
2253 * merely listed; no extra refs or pins are obtained.
2254 * iov_iter_extract_will_pin() will return 0.
2258 * (*) Use with ITER_DISCARD is not supported as that has no content.
2260 * On success, the function sets *@pages to the new pagelist, if allocated, and
2261 * sets *offset0 to the offset into the first page.
2263 * It may also return -ENOMEM and -EFAULT.
2265 ssize_t iov_iter_extract_pages(struct iov_iter *i,
2266 struct page ***pages,
2268 unsigned int maxpages,
2269 iov_iter_extraction_t extraction_flags,
2272 maxsize = min_t(size_t, min_t(size_t, maxsize, i->count), MAX_RW_COUNT);
2276 if (likely(user_backed_iter(i)))
2277 return iov_iter_extract_user_pages(i, pages, maxsize,
2278 maxpages, extraction_flags,
2280 if (iov_iter_is_kvec(i))
2281 return iov_iter_extract_kvec_pages(i, pages, maxsize,
2282 maxpages, extraction_flags,
2284 if (iov_iter_is_bvec(i))
2285 return iov_iter_extract_bvec_pages(i, pages, maxsize,
2286 maxpages, extraction_flags,
2288 if (iov_iter_is_xarray(i))
2289 return iov_iter_extract_xarray_pages(i, pages, maxsize,
2290 maxpages, extraction_flags,
2294 EXPORT_SYMBOL_GPL(iov_iter_extract_pages);