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 iovec and kvec alike */
20 #define iterate_iovec(i, n, base, len, off, __p, STEP) { \
22 size_t skip = i->iov_offset; \
24 len = min(n, __p->iov_len - skip); \
26 base = __p->iov_base + skip; \
31 if (skip < __p->iov_len) \
37 i->iov_offset = skip; \
41 #define iterate_bvec(i, n, base, len, off, p, STEP) { \
43 unsigned skip = i->iov_offset; \
45 unsigned offset = p->bv_offset + skip; \
47 void *kaddr = kmap_local_page(p->bv_page + \
48 offset / PAGE_SIZE); \
49 base = kaddr + offset % PAGE_SIZE; \
50 len = min(min(n, (size_t)(p->bv_len - skip)), \
51 (size_t)(PAGE_SIZE - offset % PAGE_SIZE)); \
53 kunmap_local(kaddr); \
57 if (skip == p->bv_len) { \
65 i->iov_offset = skip; \
69 #define iterate_xarray(i, n, base, len, __off, STEP) { \
72 struct page *head = NULL; \
73 loff_t start = i->xarray_start + i->iov_offset; \
74 unsigned offset = start % PAGE_SIZE; \
75 pgoff_t index = start / PAGE_SIZE; \
78 XA_STATE(xas, i->xarray, index); \
81 xas_for_each(&xas, head, ULONG_MAX) { \
83 if (xas_retry(&xas, head)) \
85 if (WARN_ON(xa_is_value(head))) \
87 if (WARN_ON(PageHuge(head))) \
89 for (j = (head->index < index) ? index - head->index : 0; \
90 j < thp_nr_pages(head); j++) { \
91 void *kaddr = kmap_local_page(head + j); \
92 base = kaddr + offset; \
93 len = PAGE_SIZE - offset; \
96 kunmap_local(kaddr); \
100 if (left || n == 0) \
107 i->iov_offset += __off; \
111 #define __iterate_and_advance(i, n, base, len, off, I, K) { \
112 if (unlikely(i->count < n)) \
115 if (likely(iter_is_iovec(i))) { \
116 const struct iovec *iov = i->iov; \
119 iterate_iovec(i, n, base, len, off, \
121 i->nr_segs -= iov - i->iov; \
123 } else if (iov_iter_is_bvec(i)) { \
124 const struct bio_vec *bvec = i->bvec; \
127 iterate_bvec(i, n, base, len, off, \
129 i->nr_segs -= bvec - i->bvec; \
131 } else if (iov_iter_is_kvec(i)) { \
132 const struct kvec *kvec = i->kvec; \
135 iterate_iovec(i, n, base, len, off, \
137 i->nr_segs -= kvec - i->kvec; \
139 } else if (iov_iter_is_xarray(i)) { \
142 iterate_xarray(i, n, base, len, off, \
148 #define iterate_and_advance(i, n, base, len, off, I, K) \
149 __iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
151 static int copyout(void __user *to, const void *from, size_t n)
153 if (should_fail_usercopy())
155 if (access_ok(to, n)) {
156 instrument_copy_to_user(to, from, n);
157 n = raw_copy_to_user(to, from, n);
162 static int copyin(void *to, const void __user *from, size_t n)
164 if (should_fail_usercopy())
166 if (access_ok(from, n)) {
167 instrument_copy_from_user(to, from, n);
168 n = raw_copy_from_user(to, from, n);
173 static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
176 size_t skip, copy, left, wanted;
177 const struct iovec *iov;
181 if (unlikely(bytes > i->count))
184 if (unlikely(!bytes))
190 skip = i->iov_offset;
191 buf = iov->iov_base + skip;
192 copy = min(bytes, iov->iov_len - skip);
194 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
195 kaddr = kmap_atomic(page);
196 from = kaddr + offset;
198 /* first chunk, usually the only one */
199 left = copyout(buf, from, copy);
205 while (unlikely(!left && bytes)) {
208 copy = min(bytes, iov->iov_len);
209 left = copyout(buf, from, copy);
215 if (likely(!bytes)) {
216 kunmap_atomic(kaddr);
219 offset = from - kaddr;
221 kunmap_atomic(kaddr);
222 copy = min(bytes, iov->iov_len - skip);
224 /* Too bad - revert to non-atomic kmap */
227 from = kaddr + offset;
228 left = copyout(buf, from, copy);
233 while (unlikely(!left && bytes)) {
236 copy = min(bytes, iov->iov_len);
237 left = copyout(buf, from, copy);
246 if (skip == iov->iov_len) {
250 i->count -= wanted - bytes;
251 i->nr_segs -= iov - i->iov;
253 i->iov_offset = skip;
254 return wanted - bytes;
257 static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
260 size_t skip, copy, left, wanted;
261 const struct iovec *iov;
265 if (unlikely(bytes > i->count))
268 if (unlikely(!bytes))
274 skip = i->iov_offset;
275 buf = iov->iov_base + skip;
276 copy = min(bytes, iov->iov_len - skip);
278 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
279 kaddr = kmap_atomic(page);
282 /* first chunk, usually the only one */
283 left = copyin(to, buf, copy);
289 while (unlikely(!left && bytes)) {
292 copy = min(bytes, iov->iov_len);
293 left = copyin(to, buf, copy);
299 if (likely(!bytes)) {
300 kunmap_atomic(kaddr);
305 kunmap_atomic(kaddr);
306 copy = min(bytes, iov->iov_len - skip);
308 /* Too bad - revert to non-atomic kmap */
312 left = copyin(to, buf, copy);
317 while (unlikely(!left && bytes)) {
320 copy = min(bytes, iov->iov_len);
321 left = copyin(to, buf, copy);
330 if (skip == iov->iov_len) {
334 i->count -= wanted - bytes;
335 i->nr_segs -= iov - i->iov;
337 i->iov_offset = skip;
338 return wanted - bytes;
342 static bool sanity(const struct iov_iter *i)
344 struct pipe_inode_info *pipe = i->pipe;
345 unsigned int p_head = pipe->head;
346 unsigned int p_tail = pipe->tail;
347 unsigned int p_mask = pipe->ring_size - 1;
348 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
349 unsigned int i_head = i->head;
353 struct pipe_buffer *p;
354 if (unlikely(p_occupancy == 0))
355 goto Bad; // pipe must be non-empty
356 if (unlikely(i_head != p_head - 1))
357 goto Bad; // must be at the last buffer...
359 p = &pipe->bufs[i_head & p_mask];
360 if (unlikely(p->offset + p->len != i->iov_offset))
361 goto Bad; // ... at the end of segment
363 if (i_head != p_head)
364 goto Bad; // must be right after the last buffer
368 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
369 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
370 p_head, p_tail, pipe->ring_size);
371 for (idx = 0; idx < pipe->ring_size; idx++)
372 printk(KERN_ERR "[%p %p %d %d]\n",
374 pipe->bufs[idx].page,
375 pipe->bufs[idx].offset,
376 pipe->bufs[idx].len);
381 #define sanity(i) true
384 static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
387 struct pipe_inode_info *pipe = i->pipe;
388 struct pipe_buffer *buf;
389 unsigned int p_tail = pipe->tail;
390 unsigned int p_mask = pipe->ring_size - 1;
391 unsigned int i_head = i->head;
394 if (unlikely(bytes > i->count))
397 if (unlikely(!bytes))
404 buf = &pipe->bufs[i_head & p_mask];
406 if (offset == off && buf->page == page) {
407 /* merge with the last one */
409 i->iov_offset += bytes;
413 buf = &pipe->bufs[i_head & p_mask];
415 if (pipe_full(i_head, p_tail, pipe->max_usage))
418 buf->ops = &page_cache_pipe_buf_ops;
421 buf->offset = offset;
424 pipe->head = i_head + 1;
425 i->iov_offset = offset + bytes;
433 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
434 * bytes. For each iovec, fault in each page that constitutes the iovec.
436 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
437 * because it is an invalid address).
439 int iov_iter_fault_in_readable(const struct iov_iter *i, size_t bytes)
441 if (iter_is_iovec(i)) {
442 const struct iovec *p;
445 if (bytes > i->count)
447 for (p = i->iov, skip = i->iov_offset; bytes; p++, skip = 0) {
448 size_t len = min(bytes, p->iov_len - skip);
453 err = fault_in_pages_readable(p->iov_base + skip, len);
461 EXPORT_SYMBOL(iov_iter_fault_in_readable);
463 void iov_iter_init(struct iov_iter *i, unsigned int direction,
464 const struct iovec *iov, unsigned long nr_segs,
467 WARN_ON(direction & ~(READ | WRITE));
468 *i = (struct iov_iter) {
469 .iter_type = ITER_IOVEC,
470 .data_source = direction,
477 EXPORT_SYMBOL(iov_iter_init);
479 static inline bool allocated(struct pipe_buffer *buf)
481 return buf->ops == &default_pipe_buf_ops;
484 static inline void data_start(const struct iov_iter *i,
485 unsigned int *iter_headp, size_t *offp)
487 unsigned int p_mask = i->pipe->ring_size - 1;
488 unsigned int iter_head = i->head;
489 size_t off = i->iov_offset;
491 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
496 *iter_headp = iter_head;
500 static size_t push_pipe(struct iov_iter *i, size_t size,
501 int *iter_headp, size_t *offp)
503 struct pipe_inode_info *pipe = i->pipe;
504 unsigned int p_tail = pipe->tail;
505 unsigned int p_mask = pipe->ring_size - 1;
506 unsigned int iter_head;
510 if (unlikely(size > i->count))
516 data_start(i, &iter_head, &off);
517 *iter_headp = iter_head;
520 left -= PAGE_SIZE - off;
522 pipe->bufs[iter_head & p_mask].len += size;
525 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
528 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
529 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
530 struct page *page = alloc_page(GFP_USER);
534 buf->ops = &default_pipe_buf_ops;
537 buf->len = min_t(ssize_t, left, PAGE_SIZE);
540 pipe->head = iter_head;
548 static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
551 struct pipe_inode_info *pipe = i->pipe;
552 unsigned int p_mask = pipe->ring_size - 1;
559 bytes = n = push_pipe(i, bytes, &i_head, &off);
563 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
564 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
566 i->iov_offset = off + chunk;
576 static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
577 __wsum sum, size_t off)
579 __wsum next = csum_partial_copy_nocheck(from, to, len);
580 return csum_block_add(sum, next, off);
583 static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
584 struct iov_iter *i, __wsum *sump)
586 struct pipe_inode_info *pipe = i->pipe;
587 unsigned int p_mask = pipe->ring_size - 1;
596 bytes = push_pipe(i, bytes, &i_head, &r);
598 size_t chunk = min_t(size_t, bytes, PAGE_SIZE - r);
599 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
600 sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
603 i->iov_offset = r + chunk;
614 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
616 if (unlikely(iov_iter_is_pipe(i)))
617 return copy_pipe_to_iter(addr, bytes, i);
618 if (iter_is_iovec(i))
620 iterate_and_advance(i, bytes, base, len, off,
621 copyout(base, addr + off, len),
622 memcpy(base, addr + off, len)
627 EXPORT_SYMBOL(_copy_to_iter);
629 #ifdef CONFIG_ARCH_HAS_COPY_MC
630 static int copyout_mc(void __user *to, const void *from, size_t n)
632 if (access_ok(to, n)) {
633 instrument_copy_to_user(to, from, n);
634 n = copy_mc_to_user((__force void *) to, from, n);
639 static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
642 struct pipe_inode_info *pipe = i->pipe;
643 unsigned int p_mask = pipe->ring_size - 1;
645 size_t n, off, xfer = 0;
650 n = push_pipe(i, bytes, &i_head, &off);
652 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
653 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
655 rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
659 i->iov_offset = off + chunk;
672 * _copy_mc_to_iter - copy to iter with source memory error exception handling
673 * @addr: source kernel address
674 * @bytes: total transfer length
675 * @i: destination iterator
677 * The pmem driver deploys this for the dax operation
678 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
679 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
680 * successfully copied.
682 * The main differences between this and typical _copy_to_iter().
684 * * Typical tail/residue handling after a fault retries the copy
685 * byte-by-byte until the fault happens again. Re-triggering machine
686 * checks is potentially fatal so the implementation uses source
687 * alignment and poison alignment assumptions to avoid re-triggering
688 * hardware exceptions.
690 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
691 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
694 * Return: number of bytes copied (may be %0)
696 size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
698 if (unlikely(iov_iter_is_pipe(i)))
699 return copy_mc_pipe_to_iter(addr, bytes, i);
700 if (iter_is_iovec(i))
702 __iterate_and_advance(i, bytes, base, len, off,
703 copyout_mc(base, addr + off, len),
704 copy_mc_to_kernel(base, addr + off, len)
709 EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
710 #endif /* CONFIG_ARCH_HAS_COPY_MC */
712 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
714 if (unlikely(iov_iter_is_pipe(i))) {
718 if (iter_is_iovec(i))
720 iterate_and_advance(i, bytes, base, len, off,
721 copyin(addr + off, base, len),
722 memcpy(addr + off, base, len)
727 EXPORT_SYMBOL(_copy_from_iter);
729 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
731 if (unlikely(iov_iter_is_pipe(i))) {
735 iterate_and_advance(i, bytes, base, len, off,
736 __copy_from_user_inatomic_nocache(addr + off, base, len),
737 memcpy(addr + off, base, len)
742 EXPORT_SYMBOL(_copy_from_iter_nocache);
744 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
746 * _copy_from_iter_flushcache - write destination through cpu cache
747 * @addr: destination kernel address
748 * @bytes: total transfer length
749 * @i: source iterator
751 * The pmem driver arranges for filesystem-dax to use this facility via
752 * dax_copy_from_iter() for ensuring that writes to persistent memory
753 * are flushed through the CPU cache. It is differentiated from
754 * _copy_from_iter_nocache() in that guarantees all data is flushed for
755 * all iterator types. The _copy_from_iter_nocache() only attempts to
756 * bypass the cache for the ITER_IOVEC case, and on some archs may use
757 * instructions that strand dirty-data in the cache.
759 * Return: number of bytes copied (may be %0)
761 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
763 if (unlikely(iov_iter_is_pipe(i))) {
767 iterate_and_advance(i, bytes, base, len, off,
768 __copy_from_user_flushcache(addr + off, base, len),
769 memcpy_flushcache(addr + off, base, len)
774 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
777 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
780 size_t v = n + offset;
783 * The general case needs to access the page order in order
784 * to compute the page size.
785 * However, we mostly deal with order-0 pages and thus can
786 * avoid a possible cache line miss for requests that fit all
789 if (n <= v && v <= PAGE_SIZE)
792 head = compound_head(page);
793 v += (page - head) << PAGE_SHIFT;
795 if (likely(n <= v && v <= (page_size(head))))
801 static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
804 if (likely(iter_is_iovec(i)))
805 return copy_page_to_iter_iovec(page, offset, bytes, i);
806 if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
807 void *kaddr = kmap_local_page(page);
808 size_t wanted = _copy_to_iter(kaddr + offset, bytes, i);
812 if (iov_iter_is_pipe(i))
813 return copy_page_to_iter_pipe(page, offset, bytes, i);
814 if (unlikely(iov_iter_is_discard(i))) {
815 if (unlikely(i->count < bytes))
824 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
828 if (unlikely(!page_copy_sane(page, offset, bytes)))
830 page += offset / PAGE_SIZE; // first subpage
833 size_t n = __copy_page_to_iter(page, offset,
834 min(bytes, (size_t)PAGE_SIZE - offset), i);
840 if (offset == PAGE_SIZE) {
847 EXPORT_SYMBOL(copy_page_to_iter);
849 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
852 if (unlikely(!page_copy_sane(page, offset, bytes)))
854 if (likely(iter_is_iovec(i)))
855 return copy_page_from_iter_iovec(page, offset, bytes, i);
856 if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
857 void *kaddr = kmap_local_page(page);
858 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
865 EXPORT_SYMBOL(copy_page_from_iter);
867 static size_t pipe_zero(size_t bytes, struct iov_iter *i)
869 struct pipe_inode_info *pipe = i->pipe;
870 unsigned int p_mask = pipe->ring_size - 1;
877 bytes = n = push_pipe(i, bytes, &i_head, &off);
882 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
883 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
884 memset(p + off, 0, chunk);
887 i->iov_offset = off + chunk;
896 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
898 if (unlikely(iov_iter_is_pipe(i)))
899 return pipe_zero(bytes, i);
900 iterate_and_advance(i, bytes, base, len, count,
901 clear_user(base, len),
907 EXPORT_SYMBOL(iov_iter_zero);
909 size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
912 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
913 if (unlikely(!page_copy_sane(page, offset, bytes))) {
914 kunmap_atomic(kaddr);
917 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
918 kunmap_atomic(kaddr);
922 iterate_and_advance(i, bytes, base, len, off,
923 copyin(p + off, base, len),
924 memcpy(p + off, base, len)
926 kunmap_atomic(kaddr);
929 EXPORT_SYMBOL(copy_page_from_iter_atomic);
931 static inline void pipe_truncate(struct iov_iter *i)
933 struct pipe_inode_info *pipe = i->pipe;
934 unsigned int p_tail = pipe->tail;
935 unsigned int p_head = pipe->head;
936 unsigned int p_mask = pipe->ring_size - 1;
938 if (!pipe_empty(p_head, p_tail)) {
939 struct pipe_buffer *buf;
940 unsigned int i_head = i->head;
941 size_t off = i->iov_offset;
944 buf = &pipe->bufs[i_head & p_mask];
945 buf->len = off - buf->offset;
948 while (p_head != i_head) {
950 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
957 static void pipe_advance(struct iov_iter *i, size_t size)
959 struct pipe_inode_info *pipe = i->pipe;
961 struct pipe_buffer *buf;
962 unsigned int p_mask = pipe->ring_size - 1;
963 unsigned int i_head = i->head;
964 size_t off = i->iov_offset, left = size;
966 if (off) /* make it relative to the beginning of buffer */
967 left += off - pipe->bufs[i_head & p_mask].offset;
969 buf = &pipe->bufs[i_head & p_mask];
970 if (left <= buf->len)
976 i->iov_offset = buf->offset + left;
979 /* ... and discard everything past that point */
983 static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
987 bi.bi_size = i->count;
988 bi.bi_bvec_done = i->iov_offset;
990 bvec_iter_advance(i->bvec, &bi, size);
992 i->bvec += bi.bi_idx;
993 i->nr_segs -= bi.bi_idx;
994 i->count = bi.bi_size;
995 i->iov_offset = bi.bi_bvec_done;
998 static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
1000 const struct iovec *iov, *end;
1006 size += i->iov_offset; // from beginning of current segment
1007 for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
1008 if (likely(size < iov->iov_len))
1010 size -= iov->iov_len;
1012 i->iov_offset = size;
1013 i->nr_segs -= iov - i->iov;
1017 void iov_iter_advance(struct iov_iter *i, size_t size)
1019 if (unlikely(i->count < size))
1021 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
1022 /* iovec and kvec have identical layouts */
1023 iov_iter_iovec_advance(i, size);
1024 } else if (iov_iter_is_bvec(i)) {
1025 iov_iter_bvec_advance(i, size);
1026 } else if (iov_iter_is_pipe(i)) {
1027 pipe_advance(i, size);
1028 } else if (unlikely(iov_iter_is_xarray(i))) {
1029 i->iov_offset += size;
1031 } else if (iov_iter_is_discard(i)) {
1035 EXPORT_SYMBOL(iov_iter_advance);
1037 void iov_iter_revert(struct iov_iter *i, size_t unroll)
1041 if (WARN_ON(unroll > MAX_RW_COUNT))
1044 if (unlikely(iov_iter_is_pipe(i))) {
1045 struct pipe_inode_info *pipe = i->pipe;
1046 unsigned int p_mask = pipe->ring_size - 1;
1047 unsigned int i_head = i->head;
1048 size_t off = i->iov_offset;
1050 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1051 size_t n = off - b->offset;
1057 if (!unroll && i_head == i->start_head) {
1062 b = &pipe->bufs[i_head & p_mask];
1063 off = b->offset + b->len;
1065 i->iov_offset = off;
1070 if (unlikely(iov_iter_is_discard(i)))
1072 if (unroll <= i->iov_offset) {
1073 i->iov_offset -= unroll;
1076 unroll -= i->iov_offset;
1077 if (iov_iter_is_xarray(i)) {
1078 BUG(); /* We should never go beyond the start of the specified
1079 * range since we might then be straying into pages that
1082 } else if (iov_iter_is_bvec(i)) {
1083 const struct bio_vec *bvec = i->bvec;
1085 size_t n = (--bvec)->bv_len;
1089 i->iov_offset = n - unroll;
1094 } else { /* same logics for iovec and kvec */
1095 const struct iovec *iov = i->iov;
1097 size_t n = (--iov)->iov_len;
1101 i->iov_offset = n - unroll;
1108 EXPORT_SYMBOL(iov_iter_revert);
1111 * Return the count of just the current iov_iter segment.
1113 size_t iov_iter_single_seg_count(const struct iov_iter *i)
1115 if (i->nr_segs > 1) {
1116 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1117 return min(i->count, i->iov->iov_len - i->iov_offset);
1118 if (iov_iter_is_bvec(i))
1119 return min(i->count, i->bvec->bv_len - i->iov_offset);
1123 EXPORT_SYMBOL(iov_iter_single_seg_count);
1125 void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
1126 const struct kvec *kvec, unsigned long nr_segs,
1129 WARN_ON(direction & ~(READ | WRITE));
1130 *i = (struct iov_iter){
1131 .iter_type = ITER_KVEC,
1132 .data_source = direction,
1139 EXPORT_SYMBOL(iov_iter_kvec);
1141 void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1142 const struct bio_vec *bvec, unsigned long nr_segs,
1145 WARN_ON(direction & ~(READ | WRITE));
1146 *i = (struct iov_iter){
1147 .iter_type = ITER_BVEC,
1148 .data_source = direction,
1155 EXPORT_SYMBOL(iov_iter_bvec);
1157 void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1158 struct pipe_inode_info *pipe,
1161 BUG_ON(direction != READ);
1162 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
1163 *i = (struct iov_iter){
1164 .iter_type = ITER_PIPE,
1165 .data_source = false,
1168 .start_head = pipe->head,
1173 EXPORT_SYMBOL(iov_iter_pipe);
1176 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1177 * @i: The iterator to initialise.
1178 * @direction: The direction of the transfer.
1179 * @xarray: The xarray to access.
1180 * @start: The start file position.
1181 * @count: The size of the I/O buffer in bytes.
1183 * Set up an I/O iterator to either draw data out of the pages attached to an
1184 * inode or to inject data into those pages. The pages *must* be prevented
1185 * from evaporation, either by taking a ref on them or locking them by the
1188 void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1189 struct xarray *xarray, loff_t start, size_t count)
1191 BUG_ON(direction & ~1);
1192 *i = (struct iov_iter) {
1193 .iter_type = ITER_XARRAY,
1194 .data_source = direction,
1196 .xarray_start = start,
1201 EXPORT_SYMBOL(iov_iter_xarray);
1204 * iov_iter_discard - Initialise an I/O iterator that discards data
1205 * @i: The iterator to initialise.
1206 * @direction: The direction of the transfer.
1207 * @count: The size of the I/O buffer in bytes.
1209 * Set up an I/O iterator that just discards everything that's written to it.
1210 * It's only available as a READ iterator.
1212 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1214 BUG_ON(direction != READ);
1215 *i = (struct iov_iter){
1216 .iter_type = ITER_DISCARD,
1217 .data_source = false,
1222 EXPORT_SYMBOL(iov_iter_discard);
1224 static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1226 unsigned long res = 0;
1227 size_t size = i->count;
1228 size_t skip = i->iov_offset;
1231 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1232 size_t len = i->iov[k].iov_len - skip;
1234 res |= (unsigned long)i->iov[k].iov_base + skip;
1246 static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
1249 size_t size = i->count;
1250 unsigned skip = i->iov_offset;
1253 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1254 size_t len = i->bvec[k].bv_len - skip;
1255 res |= (unsigned long)i->bvec[k].bv_offset + skip;
1266 unsigned long iov_iter_alignment(const struct iov_iter *i)
1268 /* iovec and kvec have identical layouts */
1269 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1270 return iov_iter_alignment_iovec(i);
1272 if (iov_iter_is_bvec(i))
1273 return iov_iter_alignment_bvec(i);
1275 if (iov_iter_is_pipe(i)) {
1276 unsigned int p_mask = i->pipe->ring_size - 1;
1277 size_t size = i->count;
1279 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
1280 return size | i->iov_offset;
1284 if (iov_iter_is_xarray(i))
1285 return (i->xarray_start + i->iov_offset) | i->count;
1289 EXPORT_SYMBOL(iov_iter_alignment);
1291 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1293 unsigned long res = 0;
1294 unsigned long v = 0;
1295 size_t size = i->count;
1298 if (WARN_ON(!iter_is_iovec(i)))
1301 for (k = 0; k < i->nr_segs; k++) {
1302 if (i->iov[k].iov_len) {
1303 unsigned long base = (unsigned long)i->iov[k].iov_base;
1304 if (v) // if not the first one
1305 res |= base | v; // this start | previous end
1306 v = base + i->iov[k].iov_len;
1307 if (size <= i->iov[k].iov_len)
1309 size -= i->iov[k].iov_len;
1314 EXPORT_SYMBOL(iov_iter_gap_alignment);
1316 static inline ssize_t __pipe_get_pages(struct iov_iter *i,
1318 struct page **pages,
1322 struct pipe_inode_info *pipe = i->pipe;
1323 unsigned int p_mask = pipe->ring_size - 1;
1324 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
1331 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1339 static ssize_t pipe_get_pages(struct iov_iter *i,
1340 struct page **pages, size_t maxsize, unsigned maxpages,
1343 unsigned int iter_head, npages;
1349 data_start(i, &iter_head, start);
1350 /* Amount of free space: some of this one + all after this one */
1351 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1352 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
1354 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
1357 static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1358 pgoff_t index, unsigned int nr_pages)
1360 XA_STATE(xas, xa, index);
1362 unsigned int ret = 0;
1365 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1366 if (xas_retry(&xas, page))
1369 /* Has the page moved or been split? */
1370 if (unlikely(page != xas_reload(&xas))) {
1375 pages[ret] = find_subpage(page, xas.xa_index);
1376 get_page(pages[ret]);
1377 if (++ret == nr_pages)
1384 static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1385 struct page **pages, size_t maxsize,
1386 unsigned maxpages, size_t *_start_offset)
1388 unsigned nr, offset;
1389 pgoff_t index, count;
1390 size_t size = maxsize, actual;
1393 if (!size || !maxpages)
1396 pos = i->xarray_start + i->iov_offset;
1397 index = pos >> PAGE_SHIFT;
1398 offset = pos & ~PAGE_MASK;
1399 *_start_offset = offset;
1402 if (size > PAGE_SIZE - offset) {
1403 size -= PAGE_SIZE - offset;
1404 count += size >> PAGE_SHIFT;
1410 if (count > maxpages)
1413 nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
1417 actual = PAGE_SIZE * nr;
1419 if (nr == count && size > 0) {
1420 unsigned last_offset = (nr > 1) ? 0 : offset;
1421 actual -= PAGE_SIZE - (last_offset + size);
1426 /* must be done on non-empty ITER_IOVEC one */
1427 static unsigned long first_iovec_segment(const struct iov_iter *i,
1428 size_t *size, size_t *start,
1429 size_t maxsize, unsigned maxpages)
1434 for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1435 unsigned long addr = (unsigned long)i->iov[k].iov_base + skip;
1436 size_t len = i->iov[k].iov_len - skip;
1442 len += (*start = addr % PAGE_SIZE);
1443 if (len > maxpages * PAGE_SIZE)
1444 len = maxpages * PAGE_SIZE;
1446 return addr & PAGE_MASK;
1448 BUG(); // if it had been empty, we wouldn't get called
1451 /* must be done on non-empty ITER_BVEC one */
1452 static struct page *first_bvec_segment(const struct iov_iter *i,
1453 size_t *size, size_t *start,
1454 size_t maxsize, unsigned maxpages)
1457 size_t skip = i->iov_offset, len;
1459 len = i->bvec->bv_len - skip;
1462 skip += i->bvec->bv_offset;
1463 page = i->bvec->bv_page + skip / PAGE_SIZE;
1464 len += (*start = skip % PAGE_SIZE);
1465 if (len > maxpages * PAGE_SIZE)
1466 len = maxpages * PAGE_SIZE;
1471 ssize_t iov_iter_get_pages(struct iov_iter *i,
1472 struct page **pages, size_t maxsize, unsigned maxpages,
1478 if (maxsize > i->count)
1483 if (likely(iter_is_iovec(i))) {
1486 addr = first_iovec_segment(i, &len, start, maxsize, maxpages);
1487 n = DIV_ROUND_UP(len, PAGE_SIZE);
1488 res = get_user_pages_fast(addr, n,
1489 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1491 if (unlikely(res < 0))
1493 return (res == n ? len : res * PAGE_SIZE) - *start;
1495 if (iov_iter_is_bvec(i)) {
1498 page = first_bvec_segment(i, &len, start, maxsize, maxpages);
1499 n = DIV_ROUND_UP(len, PAGE_SIZE);
1501 get_page(*pages++ = page++);
1502 return len - *start;
1504 if (iov_iter_is_pipe(i))
1505 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1506 if (iov_iter_is_xarray(i))
1507 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1510 EXPORT_SYMBOL(iov_iter_get_pages);
1512 static struct page **get_pages_array(size_t n)
1514 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
1517 static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1518 struct page ***pages, size_t maxsize,
1522 unsigned int iter_head, npages;
1528 data_start(i, &iter_head, start);
1529 /* Amount of free space: some of this one + all after this one */
1530 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1531 n = npages * PAGE_SIZE - *start;
1535 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1536 p = get_pages_array(npages);
1539 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
1547 static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
1548 struct page ***pages, size_t maxsize,
1549 size_t *_start_offset)
1552 unsigned nr, offset;
1553 pgoff_t index, count;
1554 size_t size = maxsize, actual;
1560 pos = i->xarray_start + i->iov_offset;
1561 index = pos >> PAGE_SHIFT;
1562 offset = pos & ~PAGE_MASK;
1563 *_start_offset = offset;
1566 if (size > PAGE_SIZE - offset) {
1567 size -= PAGE_SIZE - offset;
1568 count += size >> PAGE_SHIFT;
1574 p = get_pages_array(count);
1579 nr = iter_xarray_populate_pages(p, i->xarray, index, count);
1583 actual = PAGE_SIZE * nr;
1585 if (nr == count && size > 0) {
1586 unsigned last_offset = (nr > 1) ? 0 : offset;
1587 actual -= PAGE_SIZE - (last_offset + size);
1592 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1593 struct page ***pages, size_t maxsize,
1600 if (maxsize > i->count)
1605 if (likely(iter_is_iovec(i))) {
1608 addr = first_iovec_segment(i, &len, start, maxsize, ~0U);
1609 n = DIV_ROUND_UP(len, PAGE_SIZE);
1610 p = get_pages_array(n);
1613 res = get_user_pages_fast(addr, n,
1614 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
1615 if (unlikely(res < 0)) {
1620 return (res == n ? len : res * PAGE_SIZE) - *start;
1622 if (iov_iter_is_bvec(i)) {
1625 page = first_bvec_segment(i, &len, start, maxsize, ~0U);
1626 n = DIV_ROUND_UP(len, PAGE_SIZE);
1627 *pages = p = get_pages_array(n);
1631 get_page(*p++ = page++);
1632 return len - *start;
1634 if (iov_iter_is_pipe(i))
1635 return pipe_get_pages_alloc(i, pages, maxsize, start);
1636 if (iov_iter_is_xarray(i))
1637 return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
1640 EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1642 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1647 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1651 iterate_and_advance(i, bytes, base, len, off, ({
1652 next = csum_and_copy_from_user(base, addr + off, len);
1653 sum = csum_block_add(sum, next, off);
1656 sum = csum_and_memcpy(addr + off, base, len, sum, off);
1662 EXPORT_SYMBOL(csum_and_copy_from_iter);
1664 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1667 struct csum_state *csstate = _csstate;
1670 if (unlikely(iov_iter_is_discard(i))) {
1671 WARN_ON(1); /* for now */
1675 sum = csum_shift(csstate->csum, csstate->off);
1676 if (unlikely(iov_iter_is_pipe(i)))
1677 bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
1678 else iterate_and_advance(i, bytes, base, len, off, ({
1679 next = csum_and_copy_to_user(addr + off, base, len);
1680 sum = csum_block_add(sum, next, off);
1683 sum = csum_and_memcpy(base, addr + off, len, sum, off);
1686 csstate->csum = csum_shift(sum, csstate->off);
1687 csstate->off += bytes;
1690 EXPORT_SYMBOL(csum_and_copy_to_iter);
1692 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1695 #ifdef CONFIG_CRYPTO_HASH
1696 struct ahash_request *hash = hashp;
1697 struct scatterlist sg;
1700 copied = copy_to_iter(addr, bytes, i);
1701 sg_init_one(&sg, addr, copied);
1702 ahash_request_set_crypt(hash, &sg, NULL, copied);
1703 crypto_ahash_update(hash);
1709 EXPORT_SYMBOL(hash_and_copy_to_iter);
1711 static int iov_npages(const struct iov_iter *i, int maxpages)
1713 size_t skip = i->iov_offset, size = i->count;
1714 const struct iovec *p;
1717 for (p = i->iov; size; skip = 0, p++) {
1718 unsigned offs = offset_in_page(p->iov_base + skip);
1719 size_t len = min(p->iov_len - skip, size);
1723 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1724 if (unlikely(npages > maxpages))
1731 static int bvec_npages(const struct iov_iter *i, int maxpages)
1733 size_t skip = i->iov_offset, size = i->count;
1734 const struct bio_vec *p;
1737 for (p = i->bvec; size; skip = 0, p++) {
1738 unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
1739 size_t len = min(p->bv_len - skip, size);
1742 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1743 if (unlikely(npages > maxpages))
1749 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1751 if (unlikely(!i->count))
1753 /* iovec and kvec have identical layouts */
1754 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1755 return iov_npages(i, maxpages);
1756 if (iov_iter_is_bvec(i))
1757 return bvec_npages(i, maxpages);
1758 if (iov_iter_is_pipe(i)) {
1759 unsigned int iter_head;
1766 data_start(i, &iter_head, &off);
1767 /* some of this one + all after this one */
1768 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1769 return min(npages, maxpages);
1771 if (iov_iter_is_xarray(i)) {
1772 unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1773 int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
1774 return min(npages, maxpages);
1778 EXPORT_SYMBOL(iov_iter_npages);
1780 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1783 if (unlikely(iov_iter_is_pipe(new))) {
1787 if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
1789 if (iov_iter_is_bvec(new))
1790 return new->bvec = kmemdup(new->bvec,
1791 new->nr_segs * sizeof(struct bio_vec),
1794 /* iovec and kvec have identical layout */
1795 return new->iov = kmemdup(new->iov,
1796 new->nr_segs * sizeof(struct iovec),
1799 EXPORT_SYMBOL(dup_iter);
1801 static int copy_compat_iovec_from_user(struct iovec *iov,
1802 const struct iovec __user *uvec, unsigned long nr_segs)
1804 const struct compat_iovec __user *uiov =
1805 (const struct compat_iovec __user *)uvec;
1806 int ret = -EFAULT, i;
1808 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1811 for (i = 0; i < nr_segs; i++) {
1815 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1816 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1818 /* check for compat_size_t not fitting in compat_ssize_t .. */
1823 iov[i].iov_base = compat_ptr(buf);
1824 iov[i].iov_len = len;
1833 static int copy_iovec_from_user(struct iovec *iov,
1834 const struct iovec __user *uvec, unsigned long nr_segs)
1838 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1840 for (seg = 0; seg < nr_segs; seg++) {
1841 if ((ssize_t)iov[seg].iov_len < 0)
1848 struct iovec *iovec_from_user(const struct iovec __user *uvec,
1849 unsigned long nr_segs, unsigned long fast_segs,
1850 struct iovec *fast_iov, bool compat)
1852 struct iovec *iov = fast_iov;
1856 * SuS says "The readv() function *may* fail if the iovcnt argument was
1857 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
1858 * traditionally returned zero for zero segments, so...
1862 if (nr_segs > UIO_MAXIOV)
1863 return ERR_PTR(-EINVAL);
1864 if (nr_segs > fast_segs) {
1865 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1867 return ERR_PTR(-ENOMEM);
1871 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1873 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1875 if (iov != fast_iov)
1877 return ERR_PTR(ret);
1883 ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1884 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1885 struct iov_iter *i, bool compat)
1887 ssize_t total_len = 0;
1891 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1894 return PTR_ERR(iov);
1898 * According to the Single Unix Specification we should return EINVAL if
1899 * an element length is < 0 when cast to ssize_t or if the total length
1900 * would overflow the ssize_t return value of the system call.
1902 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1905 for (seg = 0; seg < nr_segs; seg++) {
1906 ssize_t len = (ssize_t)iov[seg].iov_len;
1908 if (!access_ok(iov[seg].iov_base, len)) {
1915 if (len > MAX_RW_COUNT - total_len) {
1916 len = MAX_RW_COUNT - total_len;
1917 iov[seg].iov_len = len;
1922 iov_iter_init(i, type, iov, nr_segs, total_len);
1931 * import_iovec() - Copy an array of &struct iovec from userspace
1932 * into the kernel, check that it is valid, and initialize a new
1933 * &struct iov_iter iterator to access it.
1935 * @type: One of %READ or %WRITE.
1936 * @uvec: Pointer to the userspace array.
1937 * @nr_segs: Number of elements in userspace array.
1938 * @fast_segs: Number of elements in @iov.
1939 * @iovp: (input and output parameter) Pointer to pointer to (usually small
1940 * on-stack) kernel array.
1941 * @i: Pointer to iterator that will be initialized on success.
1943 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1944 * then this function places %NULL in *@iov on return. Otherwise, a new
1945 * array will be allocated and the result placed in *@iov. This means that
1946 * the caller may call kfree() on *@iov regardless of whether the small
1947 * on-stack array was used or not (and regardless of whether this function
1948 * returns an error or not).
1950 * Return: Negative error code on error, bytes imported on success
1952 ssize_t import_iovec(int type, const struct iovec __user *uvec,
1953 unsigned nr_segs, unsigned fast_segs,
1954 struct iovec **iovp, struct iov_iter *i)
1956 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1957 in_compat_syscall());
1959 EXPORT_SYMBOL(import_iovec);
1961 int import_single_range(int rw, void __user *buf, size_t len,
1962 struct iovec *iov, struct iov_iter *i)
1964 if (len > MAX_RW_COUNT)
1966 if (unlikely(!access_ok(buf, len)))
1969 iov->iov_base = buf;
1971 iov_iter_init(i, rw, iov, 1, len);
1974 EXPORT_SYMBOL(import_single_range);
1977 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
1978 * iov_iter_save_state() was called.
1980 * @i: &struct iov_iter to restore
1981 * @state: state to restore from
1983 * Used after iov_iter_save_state() to bring restore @i, if operations may
1986 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
1988 void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
1990 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
1991 !iov_iter_is_kvec(i))
1993 i->iov_offset = state->iov_offset;
1994 i->count = state->count;
1996 * For the *vec iters, nr_segs + iov is constant - if we increment
1997 * the vec, then we also decrement the nr_segs count. Hence we don't
1998 * need to track both of these, just one is enough and we can deduct
1999 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
2000 * size, so we can just increment the iov pointer as they are unionzed.
2001 * ITER_BVEC _may_ be the same size on some archs, but on others it is
2002 * not. Be safe and handle it separately.
2004 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
2005 if (iov_iter_is_bvec(i))
2006 i->bvec -= state->nr_segs - i->nr_segs;
2008 i->iov -= state->nr_segs - i->nr_segs;
2009 i->nr_segs = state->nr_segs;