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 folio *folio; \
73 loff_t start = i->xarray_start + i->iov_offset; \
74 pgoff_t index = start / PAGE_SIZE; \
75 XA_STATE(xas, i->xarray, index); \
77 len = PAGE_SIZE - offset_in_page(start); \
79 xas_for_each(&xas, folio, ULONG_MAX) { \
82 if (xas_retry(&xas, folio)) \
84 if (WARN_ON(xa_is_value(folio))) \
86 if (WARN_ON(folio_test_hugetlb(folio))) \
88 offset = offset_in_folio(folio, start + __off); \
89 while (offset < folio_size(folio)) { \
90 base = kmap_local_folio(folio, offset); \
105 i->iov_offset += __off; \
109 #define __iterate_and_advance(i, n, base, len, off, I, K) { \
110 if (unlikely(i->count < n)) \
113 if (likely(iter_is_iovec(i))) { \
114 const struct iovec *iov = i->iov; \
117 iterate_iovec(i, n, base, len, off, \
119 i->nr_segs -= iov - i->iov; \
121 } else if (iov_iter_is_bvec(i)) { \
122 const struct bio_vec *bvec = i->bvec; \
125 iterate_bvec(i, n, base, len, off, \
127 i->nr_segs -= bvec - i->bvec; \
129 } else if (iov_iter_is_kvec(i)) { \
130 const struct kvec *kvec = i->kvec; \
133 iterate_iovec(i, n, base, len, off, \
135 i->nr_segs -= kvec - i->kvec; \
137 } else if (iov_iter_is_xarray(i)) { \
140 iterate_xarray(i, n, base, len, off, \
146 #define iterate_and_advance(i, n, base, len, off, I, K) \
147 __iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
149 static int copyout(void __user *to, const void *from, size_t n)
151 if (should_fail_usercopy())
153 if (access_ok(to, n)) {
154 instrument_copy_to_user(to, from, n);
155 n = raw_copy_to_user(to, from, n);
160 static int copyin(void *to, const void __user *from, size_t n)
162 if (should_fail_usercopy())
164 if (access_ok(from, n)) {
165 instrument_copy_from_user(to, from, n);
166 n = raw_copy_from_user(to, from, n);
172 static bool sanity(const struct iov_iter *i)
174 struct pipe_inode_info *pipe = i->pipe;
175 unsigned int p_head = pipe->head;
176 unsigned int p_tail = pipe->tail;
177 unsigned int p_mask = pipe->ring_size - 1;
178 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
179 unsigned int i_head = i->head;
183 struct pipe_buffer *p;
184 if (unlikely(p_occupancy == 0))
185 goto Bad; // pipe must be non-empty
186 if (unlikely(i_head != p_head - 1))
187 goto Bad; // must be at the last buffer...
189 p = &pipe->bufs[i_head & p_mask];
190 if (unlikely(p->offset + p->len != i->iov_offset))
191 goto Bad; // ... at the end of segment
193 if (i_head != p_head)
194 goto Bad; // must be right after the last buffer
198 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
199 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
200 p_head, p_tail, pipe->ring_size);
201 for (idx = 0; idx < pipe->ring_size; idx++)
202 printk(KERN_ERR "[%p %p %d %d]\n",
204 pipe->bufs[idx].page,
205 pipe->bufs[idx].offset,
206 pipe->bufs[idx].len);
211 #define sanity(i) true
214 static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
217 struct pipe_inode_info *pipe = i->pipe;
218 struct pipe_buffer *buf;
219 unsigned int p_tail = pipe->tail;
220 unsigned int p_mask = pipe->ring_size - 1;
221 unsigned int i_head = i->head;
224 if (unlikely(bytes > i->count))
227 if (unlikely(!bytes))
234 buf = &pipe->bufs[i_head & p_mask];
236 if (offset == off && buf->page == page) {
237 /* merge with the last one */
239 i->iov_offset += bytes;
243 buf = &pipe->bufs[i_head & p_mask];
245 if (pipe_full(i_head, p_tail, pipe->max_usage))
248 buf->ops = &page_cache_pipe_buf_ops;
252 buf->offset = offset;
255 pipe->head = i_head + 1;
256 i->iov_offset = offset + bytes;
264 * fault_in_iov_iter_readable - fault in iov iterator for reading
266 * @size: maximum length
268 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
269 * @size. For each iovec, fault in each page that constitutes the iovec.
271 * Returns the number of bytes not faulted in (like copy_to_user() and
274 * Always returns 0 for non-userspace iterators.
276 size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
278 if (iter_is_iovec(i)) {
279 size_t count = min(size, iov_iter_count(i));
280 const struct iovec *p;
284 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
285 size_t len = min(count, p->iov_len - skip);
290 ret = fault_in_readable(p->iov_base + skip, len);
299 EXPORT_SYMBOL(fault_in_iov_iter_readable);
302 * fault_in_iov_iter_writeable - fault in iov iterator for writing
304 * @size: maximum length
306 * Faults in the iterator using get_user_pages(), i.e., without triggering
307 * hardware page faults. This is primarily useful when we already know that
308 * some or all of the pages in @i aren't in memory.
310 * Returns the number of bytes not faulted in, like copy_to_user() and
313 * Always returns 0 for non-user-space iterators.
315 size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
317 if (iter_is_iovec(i)) {
318 size_t count = min(size, iov_iter_count(i));
319 const struct iovec *p;
323 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
324 size_t len = min(count, p->iov_len - skip);
329 ret = fault_in_safe_writeable(p->iov_base + skip, len);
338 EXPORT_SYMBOL(fault_in_iov_iter_writeable);
340 void iov_iter_init(struct iov_iter *i, unsigned int direction,
341 const struct iovec *iov, unsigned long nr_segs,
344 WARN_ON(direction & ~(READ | WRITE));
345 *i = (struct iov_iter) {
346 .iter_type = ITER_IOVEC,
348 .data_source = direction,
355 EXPORT_SYMBOL(iov_iter_init);
357 static inline bool allocated(struct pipe_buffer *buf)
359 return buf->ops == &default_pipe_buf_ops;
362 static inline void data_start(const struct iov_iter *i,
363 unsigned int *iter_headp, size_t *offp)
365 unsigned int p_mask = i->pipe->ring_size - 1;
366 unsigned int iter_head = i->head;
367 size_t off = i->iov_offset;
369 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
374 *iter_headp = iter_head;
378 static size_t push_pipe(struct iov_iter *i, size_t size,
379 int *iter_headp, size_t *offp)
381 struct pipe_inode_info *pipe = i->pipe;
382 unsigned int p_tail = pipe->tail;
383 unsigned int p_mask = pipe->ring_size - 1;
384 unsigned int iter_head;
388 if (unlikely(size > i->count))
394 data_start(i, &iter_head, &off);
395 *iter_headp = iter_head;
398 left -= PAGE_SIZE - off;
400 pipe->bufs[iter_head & p_mask].len += size;
403 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
406 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
407 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
408 struct page *page = alloc_page(GFP_USER);
412 buf->ops = &default_pipe_buf_ops;
416 buf->len = min_t(ssize_t, left, PAGE_SIZE);
419 pipe->head = iter_head;
427 static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
430 struct pipe_inode_info *pipe = i->pipe;
431 unsigned int p_mask = pipe->ring_size - 1;
438 bytes = n = push_pipe(i, bytes, &i_head, &off);
442 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
443 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
445 i->iov_offset = off + chunk;
455 static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
456 __wsum sum, size_t off)
458 __wsum next = csum_partial_copy_nocheck(from, to, len);
459 return csum_block_add(sum, next, off);
462 static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
463 struct iov_iter *i, __wsum *sump)
465 struct pipe_inode_info *pipe = i->pipe;
466 unsigned int p_mask = pipe->ring_size - 1;
475 bytes = push_pipe(i, bytes, &i_head, &r);
477 size_t chunk = min_t(size_t, bytes, PAGE_SIZE - r);
478 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
479 sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
482 i->iov_offset = r + chunk;
493 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
495 if (unlikely(iov_iter_is_pipe(i)))
496 return copy_pipe_to_iter(addr, bytes, i);
497 if (iter_is_iovec(i))
499 iterate_and_advance(i, bytes, base, len, off,
500 copyout(base, addr + off, len),
501 memcpy(base, addr + off, len)
506 EXPORT_SYMBOL(_copy_to_iter);
508 #ifdef CONFIG_ARCH_HAS_COPY_MC
509 static int copyout_mc(void __user *to, const void *from, size_t n)
511 if (access_ok(to, n)) {
512 instrument_copy_to_user(to, from, n);
513 n = copy_mc_to_user((__force void *) to, from, n);
518 static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
521 struct pipe_inode_info *pipe = i->pipe;
522 unsigned int p_mask = pipe->ring_size - 1;
524 size_t n, off, xfer = 0;
529 n = push_pipe(i, bytes, &i_head, &off);
531 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
532 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
534 rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
538 i->iov_offset = off + chunk;
551 * _copy_mc_to_iter - copy to iter with source memory error exception handling
552 * @addr: source kernel address
553 * @bytes: total transfer length
554 * @i: destination iterator
556 * The pmem driver deploys this for the dax operation
557 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
558 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
559 * successfully copied.
561 * The main differences between this and typical _copy_to_iter().
563 * * Typical tail/residue handling after a fault retries the copy
564 * byte-by-byte until the fault happens again. Re-triggering machine
565 * checks is potentially fatal so the implementation uses source
566 * alignment and poison alignment assumptions to avoid re-triggering
567 * hardware exceptions.
569 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
570 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
573 * Return: number of bytes copied (may be %0)
575 size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
577 if (unlikely(iov_iter_is_pipe(i)))
578 return copy_mc_pipe_to_iter(addr, bytes, i);
579 if (iter_is_iovec(i))
581 __iterate_and_advance(i, bytes, base, len, off,
582 copyout_mc(base, addr + off, len),
583 copy_mc_to_kernel(base, addr + off, len)
588 EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
589 #endif /* CONFIG_ARCH_HAS_COPY_MC */
591 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
593 if (unlikely(iov_iter_is_pipe(i))) {
597 if (iter_is_iovec(i))
599 iterate_and_advance(i, bytes, base, len, off,
600 copyin(addr + off, base, len),
601 memcpy(addr + off, base, len)
606 EXPORT_SYMBOL(_copy_from_iter);
608 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
610 if (unlikely(iov_iter_is_pipe(i))) {
614 iterate_and_advance(i, bytes, base, len, off,
615 __copy_from_user_inatomic_nocache(addr + off, base, len),
616 memcpy(addr + off, base, len)
621 EXPORT_SYMBOL(_copy_from_iter_nocache);
623 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
625 * _copy_from_iter_flushcache - write destination through cpu cache
626 * @addr: destination kernel address
627 * @bytes: total transfer length
628 * @i: source iterator
630 * The pmem driver arranges for filesystem-dax to use this facility via
631 * dax_copy_from_iter() for ensuring that writes to persistent memory
632 * are flushed through the CPU cache. It is differentiated from
633 * _copy_from_iter_nocache() in that guarantees all data is flushed for
634 * all iterator types. The _copy_from_iter_nocache() only attempts to
635 * bypass the cache for the ITER_IOVEC case, and on some archs may use
636 * instructions that strand dirty-data in the cache.
638 * Return: number of bytes copied (may be %0)
640 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
642 if (unlikely(iov_iter_is_pipe(i))) {
646 iterate_and_advance(i, bytes, base, len, off,
647 __copy_from_user_flushcache(addr + off, base, len),
648 memcpy_flushcache(addr + off, base, len)
653 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
656 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
659 size_t v = n + offset;
662 * The general case needs to access the page order in order
663 * to compute the page size.
664 * However, we mostly deal with order-0 pages and thus can
665 * avoid a possible cache line miss for requests that fit all
668 if (n <= v && v <= PAGE_SIZE)
671 head = compound_head(page);
672 v += (page - head) << PAGE_SHIFT;
674 if (likely(n <= v && v <= (page_size(head))))
680 static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
683 if (unlikely(iov_iter_is_pipe(i))) {
684 return copy_page_to_iter_pipe(page, offset, bytes, i);
686 void *kaddr = kmap_local_page(page);
687 size_t wanted = _copy_to_iter(kaddr + offset, bytes, i);
693 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
697 if (unlikely(!page_copy_sane(page, offset, bytes)))
699 page += offset / PAGE_SIZE; // first subpage
702 size_t n = __copy_page_to_iter(page, offset,
703 min(bytes, (size_t)PAGE_SIZE - offset), i);
709 if (offset == PAGE_SIZE) {
716 EXPORT_SYMBOL(copy_page_to_iter);
718 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
721 if (page_copy_sane(page, offset, bytes)) {
722 void *kaddr = kmap_local_page(page);
723 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
729 EXPORT_SYMBOL(copy_page_from_iter);
731 static size_t pipe_zero(size_t bytes, struct iov_iter *i)
733 struct pipe_inode_info *pipe = i->pipe;
734 unsigned int p_mask = pipe->ring_size - 1;
741 bytes = n = push_pipe(i, bytes, &i_head, &off);
746 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
747 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
748 memset(p + off, 0, chunk);
751 i->iov_offset = off + chunk;
760 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
762 if (unlikely(iov_iter_is_pipe(i)))
763 return pipe_zero(bytes, i);
764 iterate_and_advance(i, bytes, base, len, count,
765 clear_user(base, len),
771 EXPORT_SYMBOL(iov_iter_zero);
773 size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
776 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
777 if (unlikely(!page_copy_sane(page, offset, bytes))) {
778 kunmap_atomic(kaddr);
781 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
782 kunmap_atomic(kaddr);
786 iterate_and_advance(i, bytes, base, len, off,
787 copyin(p + off, base, len),
788 memcpy(p + off, base, len)
790 kunmap_atomic(kaddr);
793 EXPORT_SYMBOL(copy_page_from_iter_atomic);
795 static inline void pipe_truncate(struct iov_iter *i)
797 struct pipe_inode_info *pipe = i->pipe;
798 unsigned int p_tail = pipe->tail;
799 unsigned int p_head = pipe->head;
800 unsigned int p_mask = pipe->ring_size - 1;
802 if (!pipe_empty(p_head, p_tail)) {
803 struct pipe_buffer *buf;
804 unsigned int i_head = i->head;
805 size_t off = i->iov_offset;
808 buf = &pipe->bufs[i_head & p_mask];
809 buf->len = off - buf->offset;
812 while (p_head != i_head) {
814 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
821 static void pipe_advance(struct iov_iter *i, size_t size)
823 struct pipe_inode_info *pipe = i->pipe;
825 struct pipe_buffer *buf;
826 unsigned int p_mask = pipe->ring_size - 1;
827 unsigned int i_head = i->head;
828 size_t off = i->iov_offset, left = size;
830 if (off) /* make it relative to the beginning of buffer */
831 left += off - pipe->bufs[i_head & p_mask].offset;
833 buf = &pipe->bufs[i_head & p_mask];
834 if (left <= buf->len)
840 i->iov_offset = buf->offset + left;
843 /* ... and discard everything past that point */
847 static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
849 const struct bio_vec *bvec, *end;
855 size += i->iov_offset;
857 for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
858 if (likely(size < bvec->bv_len))
860 size -= bvec->bv_len;
862 i->iov_offset = size;
863 i->nr_segs -= bvec - i->bvec;
867 static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
869 const struct iovec *iov, *end;
875 size += i->iov_offset; // from beginning of current segment
876 for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
877 if (likely(size < iov->iov_len))
879 size -= iov->iov_len;
881 i->iov_offset = size;
882 i->nr_segs -= iov - i->iov;
886 void iov_iter_advance(struct iov_iter *i, size_t size)
888 if (unlikely(i->count < size))
890 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
891 /* iovec and kvec have identical layouts */
892 iov_iter_iovec_advance(i, size);
893 } else if (iov_iter_is_bvec(i)) {
894 iov_iter_bvec_advance(i, size);
895 } else if (iov_iter_is_pipe(i)) {
896 pipe_advance(i, size);
897 } else if (unlikely(iov_iter_is_xarray(i))) {
898 i->iov_offset += size;
900 } else if (iov_iter_is_discard(i)) {
904 EXPORT_SYMBOL(iov_iter_advance);
906 void iov_iter_revert(struct iov_iter *i, size_t unroll)
910 if (WARN_ON(unroll > MAX_RW_COUNT))
913 if (unlikely(iov_iter_is_pipe(i))) {
914 struct pipe_inode_info *pipe = i->pipe;
915 unsigned int p_mask = pipe->ring_size - 1;
916 unsigned int i_head = i->head;
917 size_t off = i->iov_offset;
919 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
920 size_t n = off - b->offset;
926 if (!unroll && i_head == i->start_head) {
931 b = &pipe->bufs[i_head & p_mask];
932 off = b->offset + b->len;
939 if (unlikely(iov_iter_is_discard(i)))
941 if (unroll <= i->iov_offset) {
942 i->iov_offset -= unroll;
945 unroll -= i->iov_offset;
946 if (iov_iter_is_xarray(i)) {
947 BUG(); /* We should never go beyond the start of the specified
948 * range since we might then be straying into pages that
951 } else if (iov_iter_is_bvec(i)) {
952 const struct bio_vec *bvec = i->bvec;
954 size_t n = (--bvec)->bv_len;
958 i->iov_offset = n - unroll;
963 } else { /* same logics for iovec and kvec */
964 const struct iovec *iov = i->iov;
966 size_t n = (--iov)->iov_len;
970 i->iov_offset = n - unroll;
977 EXPORT_SYMBOL(iov_iter_revert);
980 * Return the count of just the current iov_iter segment.
982 size_t iov_iter_single_seg_count(const struct iov_iter *i)
984 if (i->nr_segs > 1) {
985 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
986 return min(i->count, i->iov->iov_len - i->iov_offset);
987 if (iov_iter_is_bvec(i))
988 return min(i->count, i->bvec->bv_len - i->iov_offset);
992 EXPORT_SYMBOL(iov_iter_single_seg_count);
994 void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
995 const struct kvec *kvec, unsigned long nr_segs,
998 WARN_ON(direction & ~(READ | WRITE));
999 *i = (struct iov_iter){
1000 .iter_type = ITER_KVEC,
1001 .data_source = direction,
1008 EXPORT_SYMBOL(iov_iter_kvec);
1010 void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1011 const struct bio_vec *bvec, unsigned long nr_segs,
1014 WARN_ON(direction & ~(READ | WRITE));
1015 *i = (struct iov_iter){
1016 .iter_type = ITER_BVEC,
1017 .data_source = direction,
1024 EXPORT_SYMBOL(iov_iter_bvec);
1026 void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1027 struct pipe_inode_info *pipe,
1030 BUG_ON(direction != READ);
1031 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
1032 *i = (struct iov_iter){
1033 .iter_type = ITER_PIPE,
1034 .data_source = false,
1037 .start_head = pipe->head,
1042 EXPORT_SYMBOL(iov_iter_pipe);
1045 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1046 * @i: The iterator to initialise.
1047 * @direction: The direction of the transfer.
1048 * @xarray: The xarray to access.
1049 * @start: The start file position.
1050 * @count: The size of the I/O buffer in bytes.
1052 * Set up an I/O iterator to either draw data out of the pages attached to an
1053 * inode or to inject data into those pages. The pages *must* be prevented
1054 * from evaporation, either by taking a ref on them or locking them by the
1057 void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1058 struct xarray *xarray, loff_t start, size_t count)
1060 BUG_ON(direction & ~1);
1061 *i = (struct iov_iter) {
1062 .iter_type = ITER_XARRAY,
1063 .data_source = direction,
1065 .xarray_start = start,
1070 EXPORT_SYMBOL(iov_iter_xarray);
1073 * iov_iter_discard - Initialise an I/O iterator that discards data
1074 * @i: The iterator to initialise.
1075 * @direction: The direction of the transfer.
1076 * @count: The size of the I/O buffer in bytes.
1078 * Set up an I/O iterator that just discards everything that's written to it.
1079 * It's only available as a READ iterator.
1081 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1083 BUG_ON(direction != READ);
1084 *i = (struct iov_iter){
1085 .iter_type = ITER_DISCARD,
1086 .data_source = false,
1091 EXPORT_SYMBOL(iov_iter_discard);
1093 static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
1096 size_t size = i->count;
1097 size_t skip = i->iov_offset;
1100 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1101 size_t len = i->iov[k].iov_len - skip;
1107 if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask)
1117 static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
1120 size_t size = i->count;
1121 unsigned skip = i->iov_offset;
1124 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1125 size_t len = i->bvec[k].bv_len - skip;
1131 if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
1142 * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
1143 * are aligned to the parameters.
1145 * @i: &struct iov_iter to restore
1146 * @addr_mask: bit mask to check against the iov element's addresses
1147 * @len_mask: bit mask to check against the iov element's lengths
1149 * Return: false if any addresses or lengths intersect with the provided masks
1151 bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
1154 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1155 return iov_iter_aligned_iovec(i, addr_mask, len_mask);
1157 if (iov_iter_is_bvec(i))
1158 return iov_iter_aligned_bvec(i, addr_mask, len_mask);
1160 if (iov_iter_is_pipe(i)) {
1161 unsigned int p_mask = i->pipe->ring_size - 1;
1162 size_t size = i->count;
1164 if (size & len_mask)
1166 if (size && allocated(&i->pipe->bufs[i->head & p_mask])) {
1167 if (i->iov_offset & addr_mask)
1174 if (iov_iter_is_xarray(i)) {
1175 if (i->count & len_mask)
1177 if ((i->xarray_start + i->iov_offset) & addr_mask)
1183 EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
1185 static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1187 unsigned long res = 0;
1188 size_t size = i->count;
1189 size_t skip = i->iov_offset;
1192 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1193 size_t len = i->iov[k].iov_len - skip;
1195 res |= (unsigned long)i->iov[k].iov_base + skip;
1207 static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
1210 size_t size = i->count;
1211 unsigned skip = i->iov_offset;
1214 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1215 size_t len = i->bvec[k].bv_len - skip;
1216 res |= (unsigned long)i->bvec[k].bv_offset + skip;
1227 unsigned long iov_iter_alignment(const struct iov_iter *i)
1229 /* iovec and kvec have identical layouts */
1230 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1231 return iov_iter_alignment_iovec(i);
1233 if (iov_iter_is_bvec(i))
1234 return iov_iter_alignment_bvec(i);
1236 if (iov_iter_is_pipe(i)) {
1237 unsigned int p_mask = i->pipe->ring_size - 1;
1238 size_t size = i->count;
1240 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
1241 return size | i->iov_offset;
1245 if (iov_iter_is_xarray(i))
1246 return (i->xarray_start + i->iov_offset) | i->count;
1250 EXPORT_SYMBOL(iov_iter_alignment);
1252 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1254 unsigned long res = 0;
1255 unsigned long v = 0;
1256 size_t size = i->count;
1259 if (WARN_ON(!iter_is_iovec(i)))
1262 for (k = 0; k < i->nr_segs; k++) {
1263 if (i->iov[k].iov_len) {
1264 unsigned long base = (unsigned long)i->iov[k].iov_base;
1265 if (v) // if not the first one
1266 res |= base | v; // this start | previous end
1267 v = base + i->iov[k].iov_len;
1268 if (size <= i->iov[k].iov_len)
1270 size -= i->iov[k].iov_len;
1275 EXPORT_SYMBOL(iov_iter_gap_alignment);
1277 static inline ssize_t __pipe_get_pages(struct iov_iter *i,
1279 struct page **pages,
1283 struct pipe_inode_info *pipe = i->pipe;
1284 unsigned int p_mask = pipe->ring_size - 1;
1285 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
1292 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1300 static ssize_t pipe_get_pages(struct iov_iter *i,
1301 struct page **pages, size_t maxsize, unsigned maxpages,
1304 unsigned int iter_head, npages;
1310 data_start(i, &iter_head, start);
1311 /* Amount of free space: some of this one + all after this one */
1312 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1313 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
1315 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
1318 static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1319 pgoff_t index, unsigned int nr_pages)
1321 XA_STATE(xas, xa, index);
1323 unsigned int ret = 0;
1326 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1327 if (xas_retry(&xas, page))
1330 /* Has the page moved or been split? */
1331 if (unlikely(page != xas_reload(&xas))) {
1336 pages[ret] = find_subpage(page, xas.xa_index);
1337 get_page(pages[ret]);
1338 if (++ret == nr_pages)
1345 static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1346 struct page **pages, size_t maxsize,
1347 unsigned maxpages, size_t *_start_offset)
1349 unsigned nr, offset;
1350 pgoff_t index, count;
1351 size_t size = maxsize;
1354 if (!size || !maxpages)
1357 pos = i->xarray_start + i->iov_offset;
1358 index = pos >> PAGE_SHIFT;
1359 offset = pos & ~PAGE_MASK;
1360 *_start_offset = offset;
1363 if (size > PAGE_SIZE - offset) {
1364 size -= PAGE_SIZE - offset;
1365 count += size >> PAGE_SHIFT;
1371 if (count > maxpages)
1374 nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
1378 return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1381 /* must be done on non-empty ITER_IOVEC one */
1382 static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
1387 for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1388 size_t len = i->iov[k].iov_len - skip;
1394 return (unsigned long)i->iov[k].iov_base + skip;
1396 BUG(); // if it had been empty, we wouldn't get called
1399 /* must be done on non-empty ITER_BVEC one */
1400 static struct page *first_bvec_segment(const struct iov_iter *i,
1401 size_t *size, size_t *start)
1404 size_t skip = i->iov_offset, len;
1406 len = i->bvec->bv_len - skip;
1409 skip += i->bvec->bv_offset;
1410 page = i->bvec->bv_page + skip / PAGE_SIZE;
1411 *start = skip % PAGE_SIZE;
1415 ssize_t iov_iter_get_pages(struct iov_iter *i,
1416 struct page **pages, size_t maxsize, unsigned maxpages,
1421 if (maxsize > i->count)
1425 if (maxsize > MAX_RW_COUNT)
1426 maxsize = MAX_RW_COUNT;
1428 if (likely(iter_is_iovec(i))) {
1429 unsigned int gup_flags = 0;
1432 if (iov_iter_rw(i) != WRITE)
1433 gup_flags |= FOLL_WRITE;
1435 gup_flags |= FOLL_NOFAULT;
1437 addr = first_iovec_segment(i, &maxsize);
1438 *start = addr % PAGE_SIZE;
1440 n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1443 res = get_user_pages_fast(addr, n, gup_flags, pages);
1444 if (unlikely(res <= 0))
1446 return min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1448 if (iov_iter_is_bvec(i)) {
1451 page = first_bvec_segment(i, &maxsize, start);
1452 n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1455 for (int k = 0; k < n; k++)
1456 get_page(*pages++ = page++);
1457 return min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1459 if (iov_iter_is_pipe(i))
1460 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1461 if (iov_iter_is_xarray(i))
1462 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1465 EXPORT_SYMBOL(iov_iter_get_pages);
1467 static struct page **get_pages_array(size_t n)
1469 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
1472 static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1473 struct page ***pages, size_t maxsize,
1477 unsigned int iter_head, npages;
1483 data_start(i, &iter_head, start);
1484 /* Amount of free space: some of this one + all after this one */
1485 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1486 n = npages * PAGE_SIZE - *start;
1490 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1491 p = get_pages_array(npages);
1494 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
1502 static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
1503 struct page ***pages, size_t maxsize,
1504 size_t *_start_offset)
1507 unsigned nr, offset;
1508 pgoff_t index, count;
1509 size_t size = maxsize;
1515 pos = i->xarray_start + i->iov_offset;
1516 index = pos >> PAGE_SHIFT;
1517 offset = pos & ~PAGE_MASK;
1518 *_start_offset = offset;
1521 if (size > PAGE_SIZE - offset) {
1522 size -= PAGE_SIZE - offset;
1523 count += size >> PAGE_SHIFT;
1529 p = get_pages_array(count);
1534 nr = iter_xarray_populate_pages(p, i->xarray, index, count);
1538 return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1541 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1542 struct page ***pages, size_t maxsize,
1548 if (maxsize > i->count)
1552 if (maxsize > MAX_RW_COUNT)
1553 maxsize = MAX_RW_COUNT;
1555 if (likely(iter_is_iovec(i))) {
1556 unsigned int gup_flags = 0;
1559 if (iov_iter_rw(i) != WRITE)
1560 gup_flags |= FOLL_WRITE;
1562 gup_flags |= FOLL_NOFAULT;
1564 addr = first_iovec_segment(i, &maxsize);
1565 *start = addr % PAGE_SIZE;
1567 n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1568 p = get_pages_array(n);
1571 res = get_user_pages_fast(addr, n, gup_flags, p);
1572 if (unlikely(res <= 0)) {
1578 return min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1580 if (iov_iter_is_bvec(i)) {
1583 page = first_bvec_segment(i, &maxsize, start);
1584 n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1585 *pages = p = get_pages_array(n);
1588 for (int k = 0; k < n; k++)
1589 get_page(*p++ = page++);
1590 return min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1592 if (iov_iter_is_pipe(i))
1593 return pipe_get_pages_alloc(i, pages, maxsize, start);
1594 if (iov_iter_is_xarray(i))
1595 return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
1598 EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1600 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1605 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1609 iterate_and_advance(i, bytes, base, len, off, ({
1610 next = csum_and_copy_from_user(base, addr + off, len);
1611 sum = csum_block_add(sum, next, off);
1614 sum = csum_and_memcpy(addr + off, base, len, sum, off);
1620 EXPORT_SYMBOL(csum_and_copy_from_iter);
1622 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1625 struct csum_state *csstate = _csstate;
1628 if (unlikely(iov_iter_is_discard(i))) {
1629 WARN_ON(1); /* for now */
1633 sum = csum_shift(csstate->csum, csstate->off);
1634 if (unlikely(iov_iter_is_pipe(i)))
1635 bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
1636 else iterate_and_advance(i, bytes, base, len, off, ({
1637 next = csum_and_copy_to_user(addr + off, base, len);
1638 sum = csum_block_add(sum, next, off);
1641 sum = csum_and_memcpy(base, addr + off, len, sum, off);
1644 csstate->csum = csum_shift(sum, csstate->off);
1645 csstate->off += bytes;
1648 EXPORT_SYMBOL(csum_and_copy_to_iter);
1650 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1653 #ifdef CONFIG_CRYPTO_HASH
1654 struct ahash_request *hash = hashp;
1655 struct scatterlist sg;
1658 copied = copy_to_iter(addr, bytes, i);
1659 sg_init_one(&sg, addr, copied);
1660 ahash_request_set_crypt(hash, &sg, NULL, copied);
1661 crypto_ahash_update(hash);
1667 EXPORT_SYMBOL(hash_and_copy_to_iter);
1669 static int iov_npages(const struct iov_iter *i, int maxpages)
1671 size_t skip = i->iov_offset, size = i->count;
1672 const struct iovec *p;
1675 for (p = i->iov; size; skip = 0, p++) {
1676 unsigned offs = offset_in_page(p->iov_base + skip);
1677 size_t len = min(p->iov_len - skip, size);
1681 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1682 if (unlikely(npages > maxpages))
1689 static int bvec_npages(const struct iov_iter *i, int maxpages)
1691 size_t skip = i->iov_offset, size = i->count;
1692 const struct bio_vec *p;
1695 for (p = i->bvec; size; skip = 0, p++) {
1696 unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
1697 size_t len = min(p->bv_len - skip, size);
1700 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1701 if (unlikely(npages > maxpages))
1707 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1709 if (unlikely(!i->count))
1711 /* iovec and kvec have identical layouts */
1712 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1713 return iov_npages(i, maxpages);
1714 if (iov_iter_is_bvec(i))
1715 return bvec_npages(i, maxpages);
1716 if (iov_iter_is_pipe(i)) {
1717 unsigned int iter_head;
1724 data_start(i, &iter_head, &off);
1725 /* some of this one + all after this one */
1726 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1727 return min(npages, maxpages);
1729 if (iov_iter_is_xarray(i)) {
1730 unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1731 int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
1732 return min(npages, maxpages);
1736 EXPORT_SYMBOL(iov_iter_npages);
1738 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1741 if (unlikely(iov_iter_is_pipe(new))) {
1745 if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
1747 if (iov_iter_is_bvec(new))
1748 return new->bvec = kmemdup(new->bvec,
1749 new->nr_segs * sizeof(struct bio_vec),
1752 /* iovec and kvec have identical layout */
1753 return new->iov = kmemdup(new->iov,
1754 new->nr_segs * sizeof(struct iovec),
1757 EXPORT_SYMBOL(dup_iter);
1759 static int copy_compat_iovec_from_user(struct iovec *iov,
1760 const struct iovec __user *uvec, unsigned long nr_segs)
1762 const struct compat_iovec __user *uiov =
1763 (const struct compat_iovec __user *)uvec;
1764 int ret = -EFAULT, i;
1766 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1769 for (i = 0; i < nr_segs; i++) {
1773 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1774 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1776 /* check for compat_size_t not fitting in compat_ssize_t .. */
1781 iov[i].iov_base = compat_ptr(buf);
1782 iov[i].iov_len = len;
1791 static int copy_iovec_from_user(struct iovec *iov,
1792 const struct iovec __user *uvec, unsigned long nr_segs)
1796 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1798 for (seg = 0; seg < nr_segs; seg++) {
1799 if ((ssize_t)iov[seg].iov_len < 0)
1806 struct iovec *iovec_from_user(const struct iovec __user *uvec,
1807 unsigned long nr_segs, unsigned long fast_segs,
1808 struct iovec *fast_iov, bool compat)
1810 struct iovec *iov = fast_iov;
1814 * SuS says "The readv() function *may* fail if the iovcnt argument was
1815 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
1816 * traditionally returned zero for zero segments, so...
1820 if (nr_segs > UIO_MAXIOV)
1821 return ERR_PTR(-EINVAL);
1822 if (nr_segs > fast_segs) {
1823 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1825 return ERR_PTR(-ENOMEM);
1829 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1831 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1833 if (iov != fast_iov)
1835 return ERR_PTR(ret);
1841 ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1842 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1843 struct iov_iter *i, bool compat)
1845 ssize_t total_len = 0;
1849 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1852 return PTR_ERR(iov);
1856 * According to the Single Unix Specification we should return EINVAL if
1857 * an element length is < 0 when cast to ssize_t or if the total length
1858 * would overflow the ssize_t return value of the system call.
1860 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1863 for (seg = 0; seg < nr_segs; seg++) {
1864 ssize_t len = (ssize_t)iov[seg].iov_len;
1866 if (!access_ok(iov[seg].iov_base, len)) {
1873 if (len > MAX_RW_COUNT - total_len) {
1874 len = MAX_RW_COUNT - total_len;
1875 iov[seg].iov_len = len;
1880 iov_iter_init(i, type, iov, nr_segs, total_len);
1889 * import_iovec() - Copy an array of &struct iovec from userspace
1890 * into the kernel, check that it is valid, and initialize a new
1891 * &struct iov_iter iterator to access it.
1893 * @type: One of %READ or %WRITE.
1894 * @uvec: Pointer to the userspace array.
1895 * @nr_segs: Number of elements in userspace array.
1896 * @fast_segs: Number of elements in @iov.
1897 * @iovp: (input and output parameter) Pointer to pointer to (usually small
1898 * on-stack) kernel array.
1899 * @i: Pointer to iterator that will be initialized on success.
1901 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1902 * then this function places %NULL in *@iov on return. Otherwise, a new
1903 * array will be allocated and the result placed in *@iov. This means that
1904 * the caller may call kfree() on *@iov regardless of whether the small
1905 * on-stack array was used or not (and regardless of whether this function
1906 * returns an error or not).
1908 * Return: Negative error code on error, bytes imported on success
1910 ssize_t import_iovec(int type, const struct iovec __user *uvec,
1911 unsigned nr_segs, unsigned fast_segs,
1912 struct iovec **iovp, struct iov_iter *i)
1914 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1915 in_compat_syscall());
1917 EXPORT_SYMBOL(import_iovec);
1919 int import_single_range(int rw, void __user *buf, size_t len,
1920 struct iovec *iov, struct iov_iter *i)
1922 if (len > MAX_RW_COUNT)
1924 if (unlikely(!access_ok(buf, len)))
1927 iov->iov_base = buf;
1929 iov_iter_init(i, rw, iov, 1, len);
1932 EXPORT_SYMBOL(import_single_range);
1935 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
1936 * iov_iter_save_state() was called.
1938 * @i: &struct iov_iter to restore
1939 * @state: state to restore from
1941 * Used after iov_iter_save_state() to bring restore @i, if operations may
1944 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
1946 void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
1948 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
1949 !iov_iter_is_kvec(i))
1951 i->iov_offset = state->iov_offset;
1952 i->count = state->count;
1954 * For the *vec iters, nr_segs + iov is constant - if we increment
1955 * the vec, then we also decrement the nr_segs count. Hence we don't
1956 * need to track both of these, just one is enough and we can deduct
1957 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
1958 * size, so we can just increment the iov pointer as they are unionzed.
1959 * ITER_BVEC _may_ be the same size on some archs, but on others it is
1960 * not. Be safe and handle it separately.
1962 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
1963 if (iov_iter_is_bvec(i))
1964 i->bvec -= state->nr_segs - i->nr_segs;
1966 i->iov -= state->nr_segs - i->nr_segs;
1967 i->nr_segs = state->nr_segs;