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 WARN_ON_ONCE(uaccess_kernel());
469 *i
= (struct iov_iter
) {
470 .iter_type
= ITER_IOVEC
,
471 .data_source
= direction
,
478 EXPORT_SYMBOL(iov_iter_init
);
480 static inline bool allocated(struct pipe_buffer
*buf
)
482 return buf
->ops
== &default_pipe_buf_ops
;
485 static inline void data_start(const struct iov_iter
*i
,
486 unsigned int *iter_headp
, size_t *offp
)
488 unsigned int p_mask
= i
->pipe
->ring_size
- 1;
489 unsigned int iter_head
= i
->head
;
490 size_t off
= i
->iov_offset
;
492 if (off
&& (!allocated(&i
->pipe
->bufs
[iter_head
& p_mask
]) ||
497 *iter_headp
= iter_head
;
501 static size_t push_pipe(struct iov_iter
*i
, size_t size
,
502 int *iter_headp
, size_t *offp
)
504 struct pipe_inode_info
*pipe
= i
->pipe
;
505 unsigned int p_tail
= pipe
->tail
;
506 unsigned int p_mask
= pipe
->ring_size
- 1;
507 unsigned int iter_head
;
511 if (unlikely(size
> i
->count
))
517 data_start(i
, &iter_head
, &off
);
518 *iter_headp
= iter_head
;
521 left
-= PAGE_SIZE
- off
;
523 pipe
->bufs
[iter_head
& p_mask
].len
+= size
;
526 pipe
->bufs
[iter_head
& p_mask
].len
= PAGE_SIZE
;
529 while (!pipe_full(iter_head
, p_tail
, pipe
->max_usage
)) {
530 struct pipe_buffer
*buf
= &pipe
->bufs
[iter_head
& p_mask
];
531 struct page
*page
= alloc_page(GFP_USER
);
535 buf
->ops
= &default_pipe_buf_ops
;
538 buf
->len
= min_t(ssize_t
, left
, PAGE_SIZE
);
541 pipe
->head
= iter_head
;
549 static size_t copy_pipe_to_iter(const void *addr
, size_t bytes
,
552 struct pipe_inode_info
*pipe
= i
->pipe
;
553 unsigned int p_mask
= pipe
->ring_size
- 1;
560 bytes
= n
= push_pipe(i
, bytes
, &i_head
, &off
);
564 size_t chunk
= min_t(size_t, n
, PAGE_SIZE
- off
);
565 memcpy_to_page(pipe
->bufs
[i_head
& p_mask
].page
, off
, addr
, chunk
);
567 i
->iov_offset
= off
+ chunk
;
577 static __wsum
csum_and_memcpy(void *to
, const void *from
, size_t len
,
578 __wsum sum
, size_t off
)
580 __wsum next
= csum_partial_copy_nocheck(from
, to
, len
);
581 return csum_block_add(sum
, next
, off
);
584 static size_t csum_and_copy_to_pipe_iter(const void *addr
, size_t bytes
,
585 struct iov_iter
*i
, __wsum
*sump
)
587 struct pipe_inode_info
*pipe
= i
->pipe
;
588 unsigned int p_mask
= pipe
->ring_size
- 1;
597 bytes
= push_pipe(i
, bytes
, &i_head
, &r
);
599 size_t chunk
= min_t(size_t, bytes
, PAGE_SIZE
- r
);
600 char *p
= kmap_local_page(pipe
->bufs
[i_head
& p_mask
].page
);
601 sum
= csum_and_memcpy(p
+ r
, addr
+ off
, chunk
, sum
, off
);
604 i
->iov_offset
= r
+ chunk
;
615 size_t _copy_to_iter(const void *addr
, size_t bytes
, struct iov_iter
*i
)
617 if (unlikely(iov_iter_is_pipe(i
)))
618 return copy_pipe_to_iter(addr
, bytes
, i
);
619 if (iter_is_iovec(i
))
621 iterate_and_advance(i
, bytes
, base
, len
, off
,
622 copyout(base
, addr
+ off
, len
),
623 memcpy(base
, addr
+ off
, len
)
628 EXPORT_SYMBOL(_copy_to_iter
);
630 #ifdef CONFIG_ARCH_HAS_COPY_MC
631 static int copyout_mc(void __user
*to
, const void *from
, size_t n
)
633 if (access_ok(to
, n
)) {
634 instrument_copy_to_user(to
, from
, n
);
635 n
= copy_mc_to_user((__force
void *) to
, from
, n
);
640 static size_t copy_mc_pipe_to_iter(const void *addr
, size_t bytes
,
643 struct pipe_inode_info
*pipe
= i
->pipe
;
644 unsigned int p_mask
= pipe
->ring_size
- 1;
646 size_t n
, off
, xfer
= 0;
651 n
= push_pipe(i
, bytes
, &i_head
, &off
);
653 size_t chunk
= min_t(size_t, n
, PAGE_SIZE
- off
);
654 char *p
= kmap_local_page(pipe
->bufs
[i_head
& p_mask
].page
);
656 rem
= copy_mc_to_kernel(p
+ off
, addr
+ xfer
, chunk
);
660 i
->iov_offset
= off
+ chunk
;
673 * _copy_mc_to_iter - copy to iter with source memory error exception handling
674 * @addr: source kernel address
675 * @bytes: total transfer length
676 * @iter: destination iterator
678 * The pmem driver deploys this for the dax operation
679 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
680 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
681 * successfully copied.
683 * The main differences between this and typical _copy_to_iter().
685 * * Typical tail/residue handling after a fault retries the copy
686 * byte-by-byte until the fault happens again. Re-triggering machine
687 * checks is potentially fatal so the implementation uses source
688 * alignment and poison alignment assumptions to avoid re-triggering
689 * hardware exceptions.
691 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
692 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
695 size_t _copy_mc_to_iter(const void *addr
, size_t bytes
, struct iov_iter
*i
)
697 if (unlikely(iov_iter_is_pipe(i
)))
698 return copy_mc_pipe_to_iter(addr
, bytes
, i
);
699 if (iter_is_iovec(i
))
701 __iterate_and_advance(i
, bytes
, base
, len
, off
,
702 copyout_mc(base
, addr
+ off
, len
),
703 copy_mc_to_kernel(base
, addr
+ off
, len
)
708 EXPORT_SYMBOL_GPL(_copy_mc_to_iter
);
709 #endif /* CONFIG_ARCH_HAS_COPY_MC */
711 size_t _copy_from_iter(void *addr
, size_t bytes
, struct iov_iter
*i
)
713 if (unlikely(iov_iter_is_pipe(i
))) {
717 if (iter_is_iovec(i
))
719 iterate_and_advance(i
, bytes
, base
, len
, off
,
720 copyin(addr
+ off
, base
, len
),
721 memcpy(addr
+ off
, base
, len
)
726 EXPORT_SYMBOL(_copy_from_iter
);
728 size_t _copy_from_iter_nocache(void *addr
, size_t bytes
, struct iov_iter
*i
)
730 if (unlikely(iov_iter_is_pipe(i
))) {
734 iterate_and_advance(i
, bytes
, base
, len
, off
,
735 __copy_from_user_inatomic_nocache(addr
+ off
, base
, len
),
736 memcpy(addr
+ off
, base
, len
)
741 EXPORT_SYMBOL(_copy_from_iter_nocache
);
743 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
745 * _copy_from_iter_flushcache - write destination through cpu cache
746 * @addr: destination kernel address
747 * @bytes: total transfer length
748 * @iter: source iterator
750 * The pmem driver arranges for filesystem-dax to use this facility via
751 * dax_copy_from_iter() for ensuring that writes to persistent memory
752 * are flushed through the CPU cache. It is differentiated from
753 * _copy_from_iter_nocache() in that guarantees all data is flushed for
754 * all iterator types. The _copy_from_iter_nocache() only attempts to
755 * bypass the cache for the ITER_IOVEC case, and on some archs may use
756 * instructions that strand dirty-data in the cache.
758 size_t _copy_from_iter_flushcache(void *addr
, size_t bytes
, struct iov_iter
*i
)
760 if (unlikely(iov_iter_is_pipe(i
))) {
764 iterate_and_advance(i
, bytes
, base
, len
, off
,
765 __copy_from_user_flushcache(addr
+ off
, base
, len
),
766 memcpy_flushcache(addr
+ off
, base
, len
)
771 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache
);
774 static inline bool page_copy_sane(struct page
*page
, size_t offset
, size_t n
)
777 size_t v
= n
+ offset
;
780 * The general case needs to access the page order in order
781 * to compute the page size.
782 * However, we mostly deal with order-0 pages and thus can
783 * avoid a possible cache line miss for requests that fit all
786 if (n
<= v
&& v
<= PAGE_SIZE
)
789 head
= compound_head(page
);
790 v
+= (page
- head
) << PAGE_SHIFT
;
792 if (likely(n
<= v
&& v
<= (page_size(head
))))
798 static size_t __copy_page_to_iter(struct page
*page
, size_t offset
, size_t bytes
,
801 if (likely(iter_is_iovec(i
)))
802 return copy_page_to_iter_iovec(page
, offset
, bytes
, i
);
803 if (iov_iter_is_bvec(i
) || iov_iter_is_kvec(i
) || iov_iter_is_xarray(i
)) {
804 void *kaddr
= kmap_local_page(page
);
805 size_t wanted
= _copy_to_iter(kaddr
+ offset
, bytes
, i
);
809 if (iov_iter_is_pipe(i
))
810 return copy_page_to_iter_pipe(page
, offset
, bytes
, i
);
811 if (unlikely(iov_iter_is_discard(i
))) {
812 if (unlikely(i
->count
< bytes
))
821 size_t copy_page_to_iter(struct page
*page
, size_t offset
, size_t bytes
,
825 if (unlikely(!page_copy_sane(page
, offset
, bytes
)))
827 page
+= offset
/ PAGE_SIZE
; // first subpage
830 size_t n
= __copy_page_to_iter(page
, offset
,
831 min(bytes
, (size_t)PAGE_SIZE
- offset
), i
);
837 if (offset
== PAGE_SIZE
) {
844 EXPORT_SYMBOL(copy_page_to_iter
);
846 size_t copy_page_from_iter(struct page
*page
, size_t offset
, size_t bytes
,
849 if (unlikely(!page_copy_sane(page
, offset
, bytes
)))
851 if (likely(iter_is_iovec(i
)))
852 return copy_page_from_iter_iovec(page
, offset
, bytes
, i
);
853 if (iov_iter_is_bvec(i
) || iov_iter_is_kvec(i
) || iov_iter_is_xarray(i
)) {
854 void *kaddr
= kmap_local_page(page
);
855 size_t wanted
= _copy_from_iter(kaddr
+ offset
, bytes
, i
);
862 EXPORT_SYMBOL(copy_page_from_iter
);
864 static size_t pipe_zero(size_t bytes
, struct iov_iter
*i
)
866 struct pipe_inode_info
*pipe
= i
->pipe
;
867 unsigned int p_mask
= pipe
->ring_size
- 1;
874 bytes
= n
= push_pipe(i
, bytes
, &i_head
, &off
);
879 size_t chunk
= min_t(size_t, n
, PAGE_SIZE
- off
);
880 char *p
= kmap_local_page(pipe
->bufs
[i_head
& p_mask
].page
);
881 memset(p
+ off
, 0, chunk
);
884 i
->iov_offset
= off
+ chunk
;
893 size_t iov_iter_zero(size_t bytes
, struct iov_iter
*i
)
895 if (unlikely(iov_iter_is_pipe(i
)))
896 return pipe_zero(bytes
, i
);
897 iterate_and_advance(i
, bytes
, base
, len
, count
,
898 clear_user(base
, len
),
904 EXPORT_SYMBOL(iov_iter_zero
);
906 size_t copy_page_from_iter_atomic(struct page
*page
, unsigned offset
, size_t bytes
,
909 char *kaddr
= kmap_atomic(page
), *p
= kaddr
+ offset
;
910 if (unlikely(!page_copy_sane(page
, offset
, bytes
))) {
911 kunmap_atomic(kaddr
);
914 if (unlikely(iov_iter_is_pipe(i
) || iov_iter_is_discard(i
))) {
915 kunmap_atomic(kaddr
);
919 iterate_and_advance(i
, bytes
, base
, len
, off
,
920 copyin(p
+ off
, base
, len
),
921 memcpy(p
+ off
, base
, len
)
923 kunmap_atomic(kaddr
);
926 EXPORT_SYMBOL(copy_page_from_iter_atomic
);
928 static inline void pipe_truncate(struct iov_iter
*i
)
930 struct pipe_inode_info
*pipe
= i
->pipe
;
931 unsigned int p_tail
= pipe
->tail
;
932 unsigned int p_head
= pipe
->head
;
933 unsigned int p_mask
= pipe
->ring_size
- 1;
935 if (!pipe_empty(p_head
, p_tail
)) {
936 struct pipe_buffer
*buf
;
937 unsigned int i_head
= i
->head
;
938 size_t off
= i
->iov_offset
;
941 buf
= &pipe
->bufs
[i_head
& p_mask
];
942 buf
->len
= off
- buf
->offset
;
945 while (p_head
!= i_head
) {
947 pipe_buf_release(pipe
, &pipe
->bufs
[p_head
& p_mask
]);
954 static void pipe_advance(struct iov_iter
*i
, size_t size
)
956 struct pipe_inode_info
*pipe
= i
->pipe
;
958 struct pipe_buffer
*buf
;
959 unsigned int p_mask
= pipe
->ring_size
- 1;
960 unsigned int i_head
= i
->head
;
961 size_t off
= i
->iov_offset
, left
= size
;
963 if (off
) /* make it relative to the beginning of buffer */
964 left
+= off
- pipe
->bufs
[i_head
& p_mask
].offset
;
966 buf
= &pipe
->bufs
[i_head
& p_mask
];
967 if (left
<= buf
->len
)
973 i
->iov_offset
= buf
->offset
+ left
;
976 /* ... and discard everything past that point */
980 static void iov_iter_bvec_advance(struct iov_iter
*i
, size_t size
)
984 bi
.bi_size
= i
->count
;
985 bi
.bi_bvec_done
= i
->iov_offset
;
987 bvec_iter_advance(i
->bvec
, &bi
, size
);
989 i
->bvec
+= bi
.bi_idx
;
990 i
->nr_segs
-= bi
.bi_idx
;
991 i
->count
= bi
.bi_size
;
992 i
->iov_offset
= bi
.bi_bvec_done
;
995 static void iov_iter_iovec_advance(struct iov_iter
*i
, size_t size
)
997 const struct iovec
*iov
, *end
;
1003 size
+= i
->iov_offset
; // from beginning of current segment
1004 for (iov
= i
->iov
, end
= iov
+ i
->nr_segs
; iov
< end
; iov
++) {
1005 if (likely(size
< iov
->iov_len
))
1007 size
-= iov
->iov_len
;
1009 i
->iov_offset
= size
;
1010 i
->nr_segs
-= iov
- i
->iov
;
1014 void iov_iter_advance(struct iov_iter
*i
, size_t size
)
1016 if (unlikely(i
->count
< size
))
1018 if (likely(iter_is_iovec(i
) || iov_iter_is_kvec(i
))) {
1019 /* iovec and kvec have identical layouts */
1020 iov_iter_iovec_advance(i
, size
);
1021 } else if (iov_iter_is_bvec(i
)) {
1022 iov_iter_bvec_advance(i
, size
);
1023 } else if (iov_iter_is_pipe(i
)) {
1024 pipe_advance(i
, size
);
1025 } else if (unlikely(iov_iter_is_xarray(i
))) {
1026 i
->iov_offset
+= size
;
1028 } else if (iov_iter_is_discard(i
)) {
1032 EXPORT_SYMBOL(iov_iter_advance
);
1034 void iov_iter_revert(struct iov_iter
*i
, size_t unroll
)
1038 if (WARN_ON(unroll
> MAX_RW_COUNT
))
1041 if (unlikely(iov_iter_is_pipe(i
))) {
1042 struct pipe_inode_info
*pipe
= i
->pipe
;
1043 unsigned int p_mask
= pipe
->ring_size
- 1;
1044 unsigned int i_head
= i
->head
;
1045 size_t off
= i
->iov_offset
;
1047 struct pipe_buffer
*b
= &pipe
->bufs
[i_head
& p_mask
];
1048 size_t n
= off
- b
->offset
;
1054 if (!unroll
&& i_head
== i
->start_head
) {
1059 b
= &pipe
->bufs
[i_head
& p_mask
];
1060 off
= b
->offset
+ b
->len
;
1062 i
->iov_offset
= off
;
1067 if (unlikely(iov_iter_is_discard(i
)))
1069 if (unroll
<= i
->iov_offset
) {
1070 i
->iov_offset
-= unroll
;
1073 unroll
-= i
->iov_offset
;
1074 if (iov_iter_is_xarray(i
)) {
1075 BUG(); /* We should never go beyond the start of the specified
1076 * range since we might then be straying into pages that
1079 } else if (iov_iter_is_bvec(i
)) {
1080 const struct bio_vec
*bvec
= i
->bvec
;
1082 size_t n
= (--bvec
)->bv_len
;
1086 i
->iov_offset
= n
- unroll
;
1091 } else { /* same logics for iovec and kvec */
1092 const struct iovec
*iov
= i
->iov
;
1094 size_t n
= (--iov
)->iov_len
;
1098 i
->iov_offset
= n
- unroll
;
1105 EXPORT_SYMBOL(iov_iter_revert
);
1108 * Return the count of just the current iov_iter segment.
1110 size_t iov_iter_single_seg_count(const struct iov_iter
*i
)
1112 if (i
->nr_segs
> 1) {
1113 if (likely(iter_is_iovec(i
) || iov_iter_is_kvec(i
)))
1114 return min(i
->count
, i
->iov
->iov_len
- i
->iov_offset
);
1115 if (iov_iter_is_bvec(i
))
1116 return min(i
->count
, i
->bvec
->bv_len
- i
->iov_offset
);
1120 EXPORT_SYMBOL(iov_iter_single_seg_count
);
1122 void iov_iter_kvec(struct iov_iter
*i
, unsigned int direction
,
1123 const struct kvec
*kvec
, unsigned long nr_segs
,
1126 WARN_ON(direction
& ~(READ
| WRITE
));
1127 *i
= (struct iov_iter
){
1128 .iter_type
= ITER_KVEC
,
1129 .data_source
= direction
,
1136 EXPORT_SYMBOL(iov_iter_kvec
);
1138 void iov_iter_bvec(struct iov_iter
*i
, unsigned int direction
,
1139 const struct bio_vec
*bvec
, unsigned long nr_segs
,
1142 WARN_ON(direction
& ~(READ
| WRITE
));
1143 *i
= (struct iov_iter
){
1144 .iter_type
= ITER_BVEC
,
1145 .data_source
= direction
,
1152 EXPORT_SYMBOL(iov_iter_bvec
);
1154 void iov_iter_pipe(struct iov_iter
*i
, unsigned int direction
,
1155 struct pipe_inode_info
*pipe
,
1158 BUG_ON(direction
!= READ
);
1159 WARN_ON(pipe_full(pipe
->head
, pipe
->tail
, pipe
->ring_size
));
1160 *i
= (struct iov_iter
){
1161 .iter_type
= ITER_PIPE
,
1162 .data_source
= false,
1165 .start_head
= pipe
->head
,
1170 EXPORT_SYMBOL(iov_iter_pipe
);
1173 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1174 * @i: The iterator to initialise.
1175 * @direction: The direction of the transfer.
1176 * @xarray: The xarray to access.
1177 * @start: The start file position.
1178 * @count: The size of the I/O buffer in bytes.
1180 * Set up an I/O iterator to either draw data out of the pages attached to an
1181 * inode or to inject data into those pages. The pages *must* be prevented
1182 * from evaporation, either by taking a ref on them or locking them by the
1185 void iov_iter_xarray(struct iov_iter
*i
, unsigned int direction
,
1186 struct xarray
*xarray
, loff_t start
, size_t count
)
1188 BUG_ON(direction
& ~1);
1189 *i
= (struct iov_iter
) {
1190 .iter_type
= ITER_XARRAY
,
1191 .data_source
= direction
,
1193 .xarray_start
= start
,
1198 EXPORT_SYMBOL(iov_iter_xarray
);
1201 * iov_iter_discard - Initialise an I/O iterator that discards data
1202 * @i: The iterator to initialise.
1203 * @direction: The direction of the transfer.
1204 * @count: The size of the I/O buffer in bytes.
1206 * Set up an I/O iterator that just discards everything that's written to it.
1207 * It's only available as a READ iterator.
1209 void iov_iter_discard(struct iov_iter
*i
, unsigned int direction
, size_t count
)
1211 BUG_ON(direction
!= READ
);
1212 *i
= (struct iov_iter
){
1213 .iter_type
= ITER_DISCARD
,
1214 .data_source
= false,
1219 EXPORT_SYMBOL(iov_iter_discard
);
1221 static unsigned long iov_iter_alignment_iovec(const struct iov_iter
*i
)
1223 unsigned long res
= 0;
1224 size_t size
= i
->count
;
1225 size_t skip
= i
->iov_offset
;
1228 for (k
= 0; k
< i
->nr_segs
; k
++, skip
= 0) {
1229 size_t len
= i
->iov
[k
].iov_len
- skip
;
1231 res
|= (unsigned long)i
->iov
[k
].iov_base
+ skip
;
1243 static unsigned long iov_iter_alignment_bvec(const struct iov_iter
*i
)
1246 size_t size
= i
->count
;
1247 unsigned skip
= i
->iov_offset
;
1250 for (k
= 0; k
< i
->nr_segs
; k
++, skip
= 0) {
1251 size_t len
= i
->bvec
[k
].bv_len
- skip
;
1252 res
|= (unsigned long)i
->bvec
[k
].bv_offset
+ skip
;
1263 unsigned long iov_iter_alignment(const struct iov_iter
*i
)
1265 /* iovec and kvec have identical layouts */
1266 if (likely(iter_is_iovec(i
) || iov_iter_is_kvec(i
)))
1267 return iov_iter_alignment_iovec(i
);
1269 if (iov_iter_is_bvec(i
))
1270 return iov_iter_alignment_bvec(i
);
1272 if (iov_iter_is_pipe(i
)) {
1273 unsigned int p_mask
= i
->pipe
->ring_size
- 1;
1274 size_t size
= i
->count
;
1276 if (size
&& i
->iov_offset
&& allocated(&i
->pipe
->bufs
[i
->head
& p_mask
]))
1277 return size
| i
->iov_offset
;
1281 if (iov_iter_is_xarray(i
))
1282 return (i
->xarray_start
+ i
->iov_offset
) | i
->count
;
1286 EXPORT_SYMBOL(iov_iter_alignment
);
1288 unsigned long iov_iter_gap_alignment(const struct iov_iter
*i
)
1290 unsigned long res
= 0;
1291 unsigned long v
= 0;
1292 size_t size
= i
->count
;
1295 if (WARN_ON(!iter_is_iovec(i
)))
1298 for (k
= 0; k
< i
->nr_segs
; k
++) {
1299 if (i
->iov
[k
].iov_len
) {
1300 unsigned long base
= (unsigned long)i
->iov
[k
].iov_base
;
1301 if (v
) // if not the first one
1302 res
|= base
| v
; // this start | previous end
1303 v
= base
+ i
->iov
[k
].iov_len
;
1304 if (size
<= i
->iov
[k
].iov_len
)
1306 size
-= i
->iov
[k
].iov_len
;
1311 EXPORT_SYMBOL(iov_iter_gap_alignment
);
1313 static inline ssize_t
__pipe_get_pages(struct iov_iter
*i
,
1315 struct page
**pages
,
1319 struct pipe_inode_info
*pipe
= i
->pipe
;
1320 unsigned int p_mask
= pipe
->ring_size
- 1;
1321 ssize_t n
= push_pipe(i
, maxsize
, &iter_head
, start
);
1328 get_page(*pages
++ = pipe
->bufs
[iter_head
& p_mask
].page
);
1336 static ssize_t
pipe_get_pages(struct iov_iter
*i
,
1337 struct page
**pages
, size_t maxsize
, unsigned maxpages
,
1340 unsigned int iter_head
, npages
;
1346 data_start(i
, &iter_head
, start
);
1347 /* Amount of free space: some of this one + all after this one */
1348 npages
= pipe_space_for_user(iter_head
, i
->pipe
->tail
, i
->pipe
);
1349 capacity
= min(npages
, maxpages
) * PAGE_SIZE
- *start
;
1351 return __pipe_get_pages(i
, min(maxsize
, capacity
), pages
, iter_head
, start
);
1354 static ssize_t
iter_xarray_populate_pages(struct page
**pages
, struct xarray
*xa
,
1355 pgoff_t index
, unsigned int nr_pages
)
1357 XA_STATE(xas
, xa
, index
);
1359 unsigned int ret
= 0;
1362 for (page
= xas_load(&xas
); page
; page
= xas_next(&xas
)) {
1363 if (xas_retry(&xas
, page
))
1366 /* Has the page moved or been split? */
1367 if (unlikely(page
!= xas_reload(&xas
))) {
1372 pages
[ret
] = find_subpage(page
, xas
.xa_index
);
1373 get_page(pages
[ret
]);
1374 if (++ret
== nr_pages
)
1381 static ssize_t
iter_xarray_get_pages(struct iov_iter
*i
,
1382 struct page
**pages
, size_t maxsize
,
1383 unsigned maxpages
, size_t *_start_offset
)
1385 unsigned nr
, offset
;
1386 pgoff_t index
, count
;
1387 size_t size
= maxsize
, actual
;
1390 if (!size
|| !maxpages
)
1393 pos
= i
->xarray_start
+ i
->iov_offset
;
1394 index
= pos
>> PAGE_SHIFT
;
1395 offset
= pos
& ~PAGE_MASK
;
1396 *_start_offset
= offset
;
1399 if (size
> PAGE_SIZE
- offset
) {
1400 size
-= PAGE_SIZE
- offset
;
1401 count
+= size
>> PAGE_SHIFT
;
1407 if (count
> maxpages
)
1410 nr
= iter_xarray_populate_pages(pages
, i
->xarray
, index
, count
);
1414 actual
= PAGE_SIZE
* nr
;
1416 if (nr
== count
&& size
> 0) {
1417 unsigned last_offset
= (nr
> 1) ? 0 : offset
;
1418 actual
-= PAGE_SIZE
- (last_offset
+ size
);
1423 /* must be done on non-empty ITER_IOVEC one */
1424 static unsigned long first_iovec_segment(const struct iov_iter
*i
,
1425 size_t *size
, size_t *start
,
1426 size_t maxsize
, unsigned maxpages
)
1431 for (k
= 0, skip
= i
->iov_offset
; k
< i
->nr_segs
; k
++, skip
= 0) {
1432 unsigned long addr
= (unsigned long)i
->iov
[k
].iov_base
+ skip
;
1433 size_t len
= i
->iov
[k
].iov_len
- skip
;
1439 len
+= (*start
= addr
% PAGE_SIZE
);
1440 if (len
> maxpages
* PAGE_SIZE
)
1441 len
= maxpages
* PAGE_SIZE
;
1443 return addr
& PAGE_MASK
;
1445 BUG(); // if it had been empty, we wouldn't get called
1448 /* must be done on non-empty ITER_BVEC one */
1449 static struct page
*first_bvec_segment(const struct iov_iter
*i
,
1450 size_t *size
, size_t *start
,
1451 size_t maxsize
, unsigned maxpages
)
1454 size_t skip
= i
->iov_offset
, len
;
1456 len
= i
->bvec
->bv_len
- skip
;
1459 skip
+= i
->bvec
->bv_offset
;
1460 page
= i
->bvec
->bv_page
+ skip
/ PAGE_SIZE
;
1461 len
+= (*start
= skip
% PAGE_SIZE
);
1462 if (len
> maxpages
* PAGE_SIZE
)
1463 len
= maxpages
* PAGE_SIZE
;
1468 ssize_t
iov_iter_get_pages(struct iov_iter
*i
,
1469 struct page
**pages
, size_t maxsize
, unsigned maxpages
,
1475 if (maxsize
> i
->count
)
1480 if (likely(iter_is_iovec(i
))) {
1483 addr
= first_iovec_segment(i
, &len
, start
, maxsize
, maxpages
);
1484 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
1485 res
= get_user_pages_fast(addr
, n
,
1486 iov_iter_rw(i
) != WRITE
? FOLL_WRITE
: 0,
1488 if (unlikely(res
< 0))
1490 return (res
== n
? len
: res
* PAGE_SIZE
) - *start
;
1492 if (iov_iter_is_bvec(i
)) {
1495 page
= first_bvec_segment(i
, &len
, start
, maxsize
, maxpages
);
1496 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
1498 get_page(*pages
++ = page
++);
1499 return len
- *start
;
1501 if (iov_iter_is_pipe(i
))
1502 return pipe_get_pages(i
, pages
, maxsize
, maxpages
, start
);
1503 if (iov_iter_is_xarray(i
))
1504 return iter_xarray_get_pages(i
, pages
, maxsize
, maxpages
, start
);
1507 EXPORT_SYMBOL(iov_iter_get_pages
);
1509 static struct page
**get_pages_array(size_t n
)
1511 return kvmalloc_array(n
, sizeof(struct page
*), GFP_KERNEL
);
1514 static ssize_t
pipe_get_pages_alloc(struct iov_iter
*i
,
1515 struct page
***pages
, size_t maxsize
,
1519 unsigned int iter_head
, npages
;
1525 data_start(i
, &iter_head
, start
);
1526 /* Amount of free space: some of this one + all after this one */
1527 npages
= pipe_space_for_user(iter_head
, i
->pipe
->tail
, i
->pipe
);
1528 n
= npages
* PAGE_SIZE
- *start
;
1532 npages
= DIV_ROUND_UP(maxsize
+ *start
, PAGE_SIZE
);
1533 p
= get_pages_array(npages
);
1536 n
= __pipe_get_pages(i
, maxsize
, p
, iter_head
, start
);
1544 static ssize_t
iter_xarray_get_pages_alloc(struct iov_iter
*i
,
1545 struct page
***pages
, size_t maxsize
,
1546 size_t *_start_offset
)
1549 unsigned nr
, offset
;
1550 pgoff_t index
, count
;
1551 size_t size
= maxsize
, actual
;
1557 pos
= i
->xarray_start
+ i
->iov_offset
;
1558 index
= pos
>> PAGE_SHIFT
;
1559 offset
= pos
& ~PAGE_MASK
;
1560 *_start_offset
= offset
;
1563 if (size
> PAGE_SIZE
- offset
) {
1564 size
-= PAGE_SIZE
- offset
;
1565 count
+= size
>> PAGE_SHIFT
;
1571 p
= get_pages_array(count
);
1576 nr
= iter_xarray_populate_pages(p
, i
->xarray
, index
, count
);
1580 actual
= PAGE_SIZE
* nr
;
1582 if (nr
== count
&& size
> 0) {
1583 unsigned last_offset
= (nr
> 1) ? 0 : offset
;
1584 actual
-= PAGE_SIZE
- (last_offset
+ size
);
1589 ssize_t
iov_iter_get_pages_alloc(struct iov_iter
*i
,
1590 struct page
***pages
, size_t maxsize
,
1597 if (maxsize
> i
->count
)
1602 if (likely(iter_is_iovec(i
))) {
1605 addr
= first_iovec_segment(i
, &len
, start
, maxsize
, ~0U);
1606 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
1607 p
= get_pages_array(n
);
1610 res
= get_user_pages_fast(addr
, n
,
1611 iov_iter_rw(i
) != WRITE
? FOLL_WRITE
: 0, p
);
1612 if (unlikely(res
< 0)) {
1617 return (res
== n
? len
: res
* PAGE_SIZE
) - *start
;
1619 if (iov_iter_is_bvec(i
)) {
1622 page
= first_bvec_segment(i
, &len
, start
, maxsize
, ~0U);
1623 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
1624 *pages
= p
= get_pages_array(n
);
1628 get_page(*p
++ = page
++);
1629 return len
- *start
;
1631 if (iov_iter_is_pipe(i
))
1632 return pipe_get_pages_alloc(i
, pages
, maxsize
, start
);
1633 if (iov_iter_is_xarray(i
))
1634 return iter_xarray_get_pages_alloc(i
, pages
, maxsize
, start
);
1637 EXPORT_SYMBOL(iov_iter_get_pages_alloc
);
1639 size_t csum_and_copy_from_iter(void *addr
, size_t bytes
, __wsum
*csum
,
1644 if (unlikely(iov_iter_is_pipe(i
) || iov_iter_is_discard(i
))) {
1648 iterate_and_advance(i
, bytes
, base
, len
, off
, ({
1649 next
= csum_and_copy_from_user(base
, addr
+ off
, len
);
1650 sum
= csum_block_add(sum
, next
, off
);
1653 sum
= csum_and_memcpy(addr
+ off
, base
, len
, sum
, off
);
1659 EXPORT_SYMBOL(csum_and_copy_from_iter
);
1661 size_t csum_and_copy_to_iter(const void *addr
, size_t bytes
, void *_csstate
,
1664 struct csum_state
*csstate
= _csstate
;
1667 if (unlikely(iov_iter_is_discard(i
))) {
1668 WARN_ON(1); /* for now */
1672 sum
= csum_shift(csstate
->csum
, csstate
->off
);
1673 if (unlikely(iov_iter_is_pipe(i
)))
1674 bytes
= csum_and_copy_to_pipe_iter(addr
, bytes
, i
, &sum
);
1675 else iterate_and_advance(i
, bytes
, base
, len
, off
, ({
1676 next
= csum_and_copy_to_user(addr
+ off
, base
, len
);
1677 sum
= csum_block_add(sum
, next
, off
);
1680 sum
= csum_and_memcpy(base
, addr
+ off
, len
, sum
, off
);
1683 csstate
->csum
= csum_shift(sum
, csstate
->off
);
1684 csstate
->off
+= bytes
;
1687 EXPORT_SYMBOL(csum_and_copy_to_iter
);
1689 size_t hash_and_copy_to_iter(const void *addr
, size_t bytes
, void *hashp
,
1692 #ifdef CONFIG_CRYPTO_HASH
1693 struct ahash_request
*hash
= hashp
;
1694 struct scatterlist sg
;
1697 copied
= copy_to_iter(addr
, bytes
, i
);
1698 sg_init_one(&sg
, addr
, copied
);
1699 ahash_request_set_crypt(hash
, &sg
, NULL
, copied
);
1700 crypto_ahash_update(hash
);
1706 EXPORT_SYMBOL(hash_and_copy_to_iter
);
1708 static int iov_npages(const struct iov_iter
*i
, int maxpages
)
1710 size_t skip
= i
->iov_offset
, size
= i
->count
;
1711 const struct iovec
*p
;
1714 for (p
= i
->iov
; size
; skip
= 0, p
++) {
1715 unsigned offs
= offset_in_page(p
->iov_base
+ skip
);
1716 size_t len
= min(p
->iov_len
- skip
, size
);
1720 npages
+= DIV_ROUND_UP(offs
+ len
, PAGE_SIZE
);
1721 if (unlikely(npages
> maxpages
))
1728 static int bvec_npages(const struct iov_iter
*i
, int maxpages
)
1730 size_t skip
= i
->iov_offset
, size
= i
->count
;
1731 const struct bio_vec
*p
;
1734 for (p
= i
->bvec
; size
; skip
= 0, p
++) {
1735 unsigned offs
= (p
->bv_offset
+ skip
) % PAGE_SIZE
;
1736 size_t len
= min(p
->bv_len
- skip
, size
);
1739 npages
+= DIV_ROUND_UP(offs
+ len
, PAGE_SIZE
);
1740 if (unlikely(npages
> maxpages
))
1746 int iov_iter_npages(const struct iov_iter
*i
, int maxpages
)
1748 if (unlikely(!i
->count
))
1750 /* iovec and kvec have identical layouts */
1751 if (likely(iter_is_iovec(i
) || iov_iter_is_kvec(i
)))
1752 return iov_npages(i
, maxpages
);
1753 if (iov_iter_is_bvec(i
))
1754 return bvec_npages(i
, maxpages
);
1755 if (iov_iter_is_pipe(i
)) {
1756 unsigned int iter_head
;
1763 data_start(i
, &iter_head
, &off
);
1764 /* some of this one + all after this one */
1765 npages
= pipe_space_for_user(iter_head
, i
->pipe
->tail
, i
->pipe
);
1766 return min(npages
, maxpages
);
1768 if (iov_iter_is_xarray(i
)) {
1769 unsigned offset
= (i
->xarray_start
+ i
->iov_offset
) % PAGE_SIZE
;
1770 int npages
= DIV_ROUND_UP(offset
+ i
->count
, PAGE_SIZE
);
1771 return min(npages
, maxpages
);
1775 EXPORT_SYMBOL(iov_iter_npages
);
1777 const void *dup_iter(struct iov_iter
*new, struct iov_iter
*old
, gfp_t flags
)
1780 if (unlikely(iov_iter_is_pipe(new))) {
1784 if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
1786 if (iov_iter_is_bvec(new))
1787 return new->bvec
= kmemdup(new->bvec
,
1788 new->nr_segs
* sizeof(struct bio_vec
),
1791 /* iovec and kvec have identical layout */
1792 return new->iov
= kmemdup(new->iov
,
1793 new->nr_segs
* sizeof(struct iovec
),
1796 EXPORT_SYMBOL(dup_iter
);
1798 static int copy_compat_iovec_from_user(struct iovec
*iov
,
1799 const struct iovec __user
*uvec
, unsigned long nr_segs
)
1801 const struct compat_iovec __user
*uiov
=
1802 (const struct compat_iovec __user
*)uvec
;
1803 int ret
= -EFAULT
, i
;
1805 if (!user_access_begin(uiov
, nr_segs
* sizeof(*uiov
)))
1808 for (i
= 0; i
< nr_segs
; i
++) {
1812 unsafe_get_user(len
, &uiov
[i
].iov_len
, uaccess_end
);
1813 unsafe_get_user(buf
, &uiov
[i
].iov_base
, uaccess_end
);
1815 /* check for compat_size_t not fitting in compat_ssize_t .. */
1820 iov
[i
].iov_base
= compat_ptr(buf
);
1821 iov
[i
].iov_len
= len
;
1830 static int copy_iovec_from_user(struct iovec
*iov
,
1831 const struct iovec __user
*uvec
, unsigned long nr_segs
)
1835 if (copy_from_user(iov
, uvec
, nr_segs
* sizeof(*uvec
)))
1837 for (seg
= 0; seg
< nr_segs
; seg
++) {
1838 if ((ssize_t
)iov
[seg
].iov_len
< 0)
1845 struct iovec
*iovec_from_user(const struct iovec __user
*uvec
,
1846 unsigned long nr_segs
, unsigned long fast_segs
,
1847 struct iovec
*fast_iov
, bool compat
)
1849 struct iovec
*iov
= fast_iov
;
1853 * SuS says "The readv() function *may* fail if the iovcnt argument was
1854 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
1855 * traditionally returned zero for zero segments, so...
1859 if (nr_segs
> UIO_MAXIOV
)
1860 return ERR_PTR(-EINVAL
);
1861 if (nr_segs
> fast_segs
) {
1862 iov
= kmalloc_array(nr_segs
, sizeof(struct iovec
), GFP_KERNEL
);
1864 return ERR_PTR(-ENOMEM
);
1868 ret
= copy_compat_iovec_from_user(iov
, uvec
, nr_segs
);
1870 ret
= copy_iovec_from_user(iov
, uvec
, nr_segs
);
1872 if (iov
!= fast_iov
)
1874 return ERR_PTR(ret
);
1880 ssize_t
__import_iovec(int type
, const struct iovec __user
*uvec
,
1881 unsigned nr_segs
, unsigned fast_segs
, struct iovec
**iovp
,
1882 struct iov_iter
*i
, bool compat
)
1884 ssize_t total_len
= 0;
1888 iov
= iovec_from_user(uvec
, nr_segs
, fast_segs
, *iovp
, compat
);
1891 return PTR_ERR(iov
);
1895 * According to the Single Unix Specification we should return EINVAL if
1896 * an element length is < 0 when cast to ssize_t or if the total length
1897 * would overflow the ssize_t return value of the system call.
1899 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1902 for (seg
= 0; seg
< nr_segs
; seg
++) {
1903 ssize_t len
= (ssize_t
)iov
[seg
].iov_len
;
1905 if (!access_ok(iov
[seg
].iov_base
, len
)) {
1912 if (len
> MAX_RW_COUNT
- total_len
) {
1913 len
= MAX_RW_COUNT
- total_len
;
1914 iov
[seg
].iov_len
= len
;
1919 iov_iter_init(i
, type
, iov
, nr_segs
, total_len
);
1928 * import_iovec() - Copy an array of &struct iovec from userspace
1929 * into the kernel, check that it is valid, and initialize a new
1930 * &struct iov_iter iterator to access it.
1932 * @type: One of %READ or %WRITE.
1933 * @uvec: Pointer to the userspace array.
1934 * @nr_segs: Number of elements in userspace array.
1935 * @fast_segs: Number of elements in @iov.
1936 * @iovp: (input and output parameter) Pointer to pointer to (usually small
1937 * on-stack) kernel array.
1938 * @i: Pointer to iterator that will be initialized on success.
1940 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1941 * then this function places %NULL in *@iov on return. Otherwise, a new
1942 * array will be allocated and the result placed in *@iov. This means that
1943 * the caller may call kfree() on *@iov regardless of whether the small
1944 * on-stack array was used or not (and regardless of whether this function
1945 * returns an error or not).
1947 * Return: Negative error code on error, bytes imported on success
1949 ssize_t
import_iovec(int type
, const struct iovec __user
*uvec
,
1950 unsigned nr_segs
, unsigned fast_segs
,
1951 struct iovec
**iovp
, struct iov_iter
*i
)
1953 return __import_iovec(type
, uvec
, nr_segs
, fast_segs
, iovp
, i
,
1954 in_compat_syscall());
1956 EXPORT_SYMBOL(import_iovec
);
1958 int import_single_range(int rw
, void __user
*buf
, size_t len
,
1959 struct iovec
*iov
, struct iov_iter
*i
)
1961 if (len
> MAX_RW_COUNT
)
1963 if (unlikely(!access_ok(buf
, len
)))
1966 iov
->iov_base
= buf
;
1968 iov_iter_init(i
, rw
, iov
, 1, len
);
1971 EXPORT_SYMBOL(import_single_range
);