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 * @iter: 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 size_t _copy_mc_to_iter(const void *addr
, size_t bytes
, struct iov_iter
*i
)
696 if (unlikely(iov_iter_is_pipe(i
)))
697 return copy_mc_pipe_to_iter(addr
, bytes
, i
);
698 if (iter_is_iovec(i
))
700 __iterate_and_advance(i
, bytes
, base
, len
, off
,
701 copyout_mc(base
, addr
+ off
, len
),
702 copy_mc_to_kernel(base
, addr
+ off
, len
)
707 EXPORT_SYMBOL_GPL(_copy_mc_to_iter
);
708 #endif /* CONFIG_ARCH_HAS_COPY_MC */
710 size_t _copy_from_iter(void *addr
, size_t bytes
, struct iov_iter
*i
)
712 if (unlikely(iov_iter_is_pipe(i
))) {
716 if (iter_is_iovec(i
))
718 iterate_and_advance(i
, bytes
, base
, len
, off
,
719 copyin(addr
+ off
, base
, len
),
720 memcpy(addr
+ off
, base
, len
)
725 EXPORT_SYMBOL(_copy_from_iter
);
727 size_t _copy_from_iter_nocache(void *addr
, size_t bytes
, struct iov_iter
*i
)
729 if (unlikely(iov_iter_is_pipe(i
))) {
733 iterate_and_advance(i
, bytes
, base
, len
, off
,
734 __copy_from_user_inatomic_nocache(addr
+ off
, base
, len
),
735 memcpy(addr
+ off
, base
, len
)
740 EXPORT_SYMBOL(_copy_from_iter_nocache
);
742 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
744 * _copy_from_iter_flushcache - write destination through cpu cache
745 * @addr: destination kernel address
746 * @bytes: total transfer length
747 * @iter: source iterator
749 * The pmem driver arranges for filesystem-dax to use this facility via
750 * dax_copy_from_iter() for ensuring that writes to persistent memory
751 * are flushed through the CPU cache. It is differentiated from
752 * _copy_from_iter_nocache() in that guarantees all data is flushed for
753 * all iterator types. The _copy_from_iter_nocache() only attempts to
754 * bypass the cache for the ITER_IOVEC case, and on some archs may use
755 * instructions that strand dirty-data in the cache.
757 size_t _copy_from_iter_flushcache(void *addr
, size_t bytes
, struct iov_iter
*i
)
759 if (unlikely(iov_iter_is_pipe(i
))) {
763 iterate_and_advance(i
, bytes
, base
, len
, off
,
764 __copy_from_user_flushcache(addr
+ off
, base
, len
),
765 memcpy_flushcache(addr
+ off
, base
, len
)
770 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache
);
773 static inline bool page_copy_sane(struct page
*page
, size_t offset
, size_t n
)
776 size_t v
= n
+ offset
;
779 * The general case needs to access the page order in order
780 * to compute the page size.
781 * However, we mostly deal with order-0 pages and thus can
782 * avoid a possible cache line miss for requests that fit all
785 if (n
<= v
&& v
<= PAGE_SIZE
)
788 head
= compound_head(page
);
789 v
+= (page
- head
) << PAGE_SHIFT
;
791 if (likely(n
<= v
&& v
<= (page_size(head
))))
797 static size_t __copy_page_to_iter(struct page
*page
, size_t offset
, size_t bytes
,
800 if (likely(iter_is_iovec(i
)))
801 return copy_page_to_iter_iovec(page
, offset
, bytes
, i
);
802 if (iov_iter_is_bvec(i
) || iov_iter_is_kvec(i
) || iov_iter_is_xarray(i
)) {
803 void *kaddr
= kmap_local_page(page
);
804 size_t wanted
= _copy_to_iter(kaddr
+ offset
, bytes
, i
);
808 if (iov_iter_is_pipe(i
))
809 return copy_page_to_iter_pipe(page
, offset
, bytes
, i
);
810 if (unlikely(iov_iter_is_discard(i
))) {
811 if (unlikely(i
->count
< bytes
))
820 size_t copy_page_to_iter(struct page
*page
, size_t offset
, size_t bytes
,
824 if (unlikely(!page_copy_sane(page
, offset
, bytes
)))
826 page
+= offset
/ PAGE_SIZE
; // first subpage
829 size_t n
= __copy_page_to_iter(page
, offset
,
830 min(bytes
, (size_t)PAGE_SIZE
- offset
), i
);
836 if (offset
== PAGE_SIZE
) {
843 EXPORT_SYMBOL(copy_page_to_iter
);
845 size_t copy_page_from_iter(struct page
*page
, size_t offset
, size_t bytes
,
848 if (unlikely(!page_copy_sane(page
, offset
, bytes
)))
850 if (likely(iter_is_iovec(i
)))
851 return copy_page_from_iter_iovec(page
, offset
, bytes
, i
);
852 if (iov_iter_is_bvec(i
) || iov_iter_is_kvec(i
) || iov_iter_is_xarray(i
)) {
853 void *kaddr
= kmap_local_page(page
);
854 size_t wanted
= _copy_from_iter(kaddr
+ offset
, bytes
, i
);
861 EXPORT_SYMBOL(copy_page_from_iter
);
863 static size_t pipe_zero(size_t bytes
, struct iov_iter
*i
)
865 struct pipe_inode_info
*pipe
= i
->pipe
;
866 unsigned int p_mask
= pipe
->ring_size
- 1;
873 bytes
= n
= push_pipe(i
, bytes
, &i_head
, &off
);
878 size_t chunk
= min_t(size_t, n
, PAGE_SIZE
- off
);
879 char *p
= kmap_local_page(pipe
->bufs
[i_head
& p_mask
].page
);
880 memset(p
+ off
, 0, chunk
);
883 i
->iov_offset
= off
+ chunk
;
892 size_t iov_iter_zero(size_t bytes
, struct iov_iter
*i
)
894 if (unlikely(iov_iter_is_pipe(i
)))
895 return pipe_zero(bytes
, i
);
896 iterate_and_advance(i
, bytes
, base
, len
, count
,
897 clear_user(base
, len
),
903 EXPORT_SYMBOL(iov_iter_zero
);
905 size_t copy_page_from_iter_atomic(struct page
*page
, unsigned offset
, size_t bytes
,
908 char *kaddr
= kmap_atomic(page
), *p
= kaddr
+ offset
;
909 if (unlikely(!page_copy_sane(page
, offset
, bytes
))) {
910 kunmap_atomic(kaddr
);
913 if (unlikely(iov_iter_is_pipe(i
) || iov_iter_is_discard(i
))) {
914 kunmap_atomic(kaddr
);
918 iterate_and_advance(i
, bytes
, base
, len
, off
,
919 copyin(p
+ off
, base
, len
),
920 memcpy(p
+ off
, base
, len
)
922 kunmap_atomic(kaddr
);
925 EXPORT_SYMBOL(copy_page_from_iter_atomic
);
927 static inline void pipe_truncate(struct iov_iter
*i
)
929 struct pipe_inode_info
*pipe
= i
->pipe
;
930 unsigned int p_tail
= pipe
->tail
;
931 unsigned int p_head
= pipe
->head
;
932 unsigned int p_mask
= pipe
->ring_size
- 1;
934 if (!pipe_empty(p_head
, p_tail
)) {
935 struct pipe_buffer
*buf
;
936 unsigned int i_head
= i
->head
;
937 size_t off
= i
->iov_offset
;
940 buf
= &pipe
->bufs
[i_head
& p_mask
];
941 buf
->len
= off
- buf
->offset
;
944 while (p_head
!= i_head
) {
946 pipe_buf_release(pipe
, &pipe
->bufs
[p_head
& p_mask
]);
953 static void pipe_advance(struct iov_iter
*i
, size_t size
)
955 struct pipe_inode_info
*pipe
= i
->pipe
;
957 struct pipe_buffer
*buf
;
958 unsigned int p_mask
= pipe
->ring_size
- 1;
959 unsigned int i_head
= i
->head
;
960 size_t off
= i
->iov_offset
, left
= size
;
962 if (off
) /* make it relative to the beginning of buffer */
963 left
+= off
- pipe
->bufs
[i_head
& p_mask
].offset
;
965 buf
= &pipe
->bufs
[i_head
& p_mask
];
966 if (left
<= buf
->len
)
972 i
->iov_offset
= buf
->offset
+ left
;
975 /* ... and discard everything past that point */
979 static void iov_iter_bvec_advance(struct iov_iter
*i
, size_t size
)
983 bi
.bi_size
= i
->count
;
984 bi
.bi_bvec_done
= i
->iov_offset
;
986 bvec_iter_advance(i
->bvec
, &bi
, size
);
988 i
->bvec
+= bi
.bi_idx
;
989 i
->nr_segs
-= bi
.bi_idx
;
990 i
->count
= bi
.bi_size
;
991 i
->iov_offset
= bi
.bi_bvec_done
;
994 static void iov_iter_iovec_advance(struct iov_iter
*i
, size_t size
)
996 const struct iovec
*iov
, *end
;
1002 size
+= i
->iov_offset
; // from beginning of current segment
1003 for (iov
= i
->iov
, end
= iov
+ i
->nr_segs
; iov
< end
; iov
++) {
1004 if (likely(size
< iov
->iov_len
))
1006 size
-= iov
->iov_len
;
1008 i
->iov_offset
= size
;
1009 i
->nr_segs
-= iov
- i
->iov
;
1013 void iov_iter_advance(struct iov_iter
*i
, size_t size
)
1015 if (unlikely(i
->count
< size
))
1017 if (likely(iter_is_iovec(i
) || iov_iter_is_kvec(i
))) {
1018 /* iovec and kvec have identical layouts */
1019 iov_iter_iovec_advance(i
, size
);
1020 } else if (iov_iter_is_bvec(i
)) {
1021 iov_iter_bvec_advance(i
, size
);
1022 } else if (iov_iter_is_pipe(i
)) {
1023 pipe_advance(i
, size
);
1024 } else if (unlikely(iov_iter_is_xarray(i
))) {
1025 i
->iov_offset
+= size
;
1027 } else if (iov_iter_is_discard(i
)) {
1031 EXPORT_SYMBOL(iov_iter_advance
);
1033 void iov_iter_revert(struct iov_iter
*i
, size_t unroll
)
1037 if (WARN_ON(unroll
> MAX_RW_COUNT
))
1040 if (unlikely(iov_iter_is_pipe(i
))) {
1041 struct pipe_inode_info
*pipe
= i
->pipe
;
1042 unsigned int p_mask
= pipe
->ring_size
- 1;
1043 unsigned int i_head
= i
->head
;
1044 size_t off
= i
->iov_offset
;
1046 struct pipe_buffer
*b
= &pipe
->bufs
[i_head
& p_mask
];
1047 size_t n
= off
- b
->offset
;
1053 if (!unroll
&& i_head
== i
->start_head
) {
1058 b
= &pipe
->bufs
[i_head
& p_mask
];
1059 off
= b
->offset
+ b
->len
;
1061 i
->iov_offset
= off
;
1066 if (unlikely(iov_iter_is_discard(i
)))
1068 if (unroll
<= i
->iov_offset
) {
1069 i
->iov_offset
-= unroll
;
1072 unroll
-= i
->iov_offset
;
1073 if (iov_iter_is_xarray(i
)) {
1074 BUG(); /* We should never go beyond the start of the specified
1075 * range since we might then be straying into pages that
1078 } else if (iov_iter_is_bvec(i
)) {
1079 const struct bio_vec
*bvec
= i
->bvec
;
1081 size_t n
= (--bvec
)->bv_len
;
1085 i
->iov_offset
= n
- unroll
;
1090 } else { /* same logics for iovec and kvec */
1091 const struct iovec
*iov
= i
->iov
;
1093 size_t n
= (--iov
)->iov_len
;
1097 i
->iov_offset
= n
- unroll
;
1104 EXPORT_SYMBOL(iov_iter_revert
);
1107 * Return the count of just the current iov_iter segment.
1109 size_t iov_iter_single_seg_count(const struct iov_iter
*i
)
1111 if (i
->nr_segs
> 1) {
1112 if (likely(iter_is_iovec(i
) || iov_iter_is_kvec(i
)))
1113 return min(i
->count
, i
->iov
->iov_len
- i
->iov_offset
);
1114 if (iov_iter_is_bvec(i
))
1115 return min(i
->count
, i
->bvec
->bv_len
- i
->iov_offset
);
1119 EXPORT_SYMBOL(iov_iter_single_seg_count
);
1121 void iov_iter_kvec(struct iov_iter
*i
, unsigned int direction
,
1122 const struct kvec
*kvec
, unsigned long nr_segs
,
1125 WARN_ON(direction
& ~(READ
| WRITE
));
1126 *i
= (struct iov_iter
){
1127 .iter_type
= ITER_KVEC
,
1128 .data_source
= direction
,
1135 EXPORT_SYMBOL(iov_iter_kvec
);
1137 void iov_iter_bvec(struct iov_iter
*i
, unsigned int direction
,
1138 const struct bio_vec
*bvec
, unsigned long nr_segs
,
1141 WARN_ON(direction
& ~(READ
| WRITE
));
1142 *i
= (struct iov_iter
){
1143 .iter_type
= ITER_BVEC
,
1144 .data_source
= direction
,
1151 EXPORT_SYMBOL(iov_iter_bvec
);
1153 void iov_iter_pipe(struct iov_iter
*i
, unsigned int direction
,
1154 struct pipe_inode_info
*pipe
,
1157 BUG_ON(direction
!= READ
);
1158 WARN_ON(pipe_full(pipe
->head
, pipe
->tail
, pipe
->ring_size
));
1159 *i
= (struct iov_iter
){
1160 .iter_type
= ITER_PIPE
,
1161 .data_source
= false,
1164 .start_head
= pipe
->head
,
1169 EXPORT_SYMBOL(iov_iter_pipe
);
1172 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1173 * @i: The iterator to initialise.
1174 * @direction: The direction of the transfer.
1175 * @xarray: The xarray to access.
1176 * @start: The start file position.
1177 * @count: The size of the I/O buffer in bytes.
1179 * Set up an I/O iterator to either draw data out of the pages attached to an
1180 * inode or to inject data into those pages. The pages *must* be prevented
1181 * from evaporation, either by taking a ref on them or locking them by the
1184 void iov_iter_xarray(struct iov_iter
*i
, unsigned int direction
,
1185 struct xarray
*xarray
, loff_t start
, size_t count
)
1187 BUG_ON(direction
& ~1);
1188 *i
= (struct iov_iter
) {
1189 .iter_type
= ITER_XARRAY
,
1190 .data_source
= direction
,
1192 .xarray_start
= start
,
1197 EXPORT_SYMBOL(iov_iter_xarray
);
1200 * iov_iter_discard - Initialise an I/O iterator that discards data
1201 * @i: The iterator to initialise.
1202 * @direction: The direction of the transfer.
1203 * @count: The size of the I/O buffer in bytes.
1205 * Set up an I/O iterator that just discards everything that's written to it.
1206 * It's only available as a READ iterator.
1208 void iov_iter_discard(struct iov_iter
*i
, unsigned int direction
, size_t count
)
1210 BUG_ON(direction
!= READ
);
1211 *i
= (struct iov_iter
){
1212 .iter_type
= ITER_DISCARD
,
1213 .data_source
= false,
1218 EXPORT_SYMBOL(iov_iter_discard
);
1220 static unsigned long iov_iter_alignment_iovec(const struct iov_iter
*i
)
1222 unsigned long res
= 0;
1223 size_t size
= i
->count
;
1224 size_t skip
= i
->iov_offset
;
1227 for (k
= 0; k
< i
->nr_segs
; k
++, skip
= 0) {
1228 size_t len
= i
->iov
[k
].iov_len
- skip
;
1230 res
|= (unsigned long)i
->iov
[k
].iov_base
+ skip
;
1242 static unsigned long iov_iter_alignment_bvec(const struct iov_iter
*i
)
1245 size_t size
= i
->count
;
1246 unsigned skip
= i
->iov_offset
;
1249 for (k
= 0; k
< i
->nr_segs
; k
++, skip
= 0) {
1250 size_t len
= i
->bvec
[k
].bv_len
- skip
;
1251 res
|= (unsigned long)i
->bvec
[k
].bv_offset
+ skip
;
1262 unsigned long iov_iter_alignment(const struct iov_iter
*i
)
1264 /* iovec and kvec have identical layouts */
1265 if (likely(iter_is_iovec(i
) || iov_iter_is_kvec(i
)))
1266 return iov_iter_alignment_iovec(i
);
1268 if (iov_iter_is_bvec(i
))
1269 return iov_iter_alignment_bvec(i
);
1271 if (iov_iter_is_pipe(i
)) {
1272 unsigned int p_mask
= i
->pipe
->ring_size
- 1;
1273 size_t size
= i
->count
;
1275 if (size
&& i
->iov_offset
&& allocated(&i
->pipe
->bufs
[i
->head
& p_mask
]))
1276 return size
| i
->iov_offset
;
1280 if (iov_iter_is_xarray(i
))
1281 return (i
->xarray_start
+ i
->iov_offset
) | i
->count
;
1285 EXPORT_SYMBOL(iov_iter_alignment
);
1287 unsigned long iov_iter_gap_alignment(const struct iov_iter
*i
)
1289 unsigned long res
= 0;
1290 unsigned long v
= 0;
1291 size_t size
= i
->count
;
1294 if (WARN_ON(!iter_is_iovec(i
)))
1297 for (k
= 0; k
< i
->nr_segs
; k
++) {
1298 if (i
->iov
[k
].iov_len
) {
1299 unsigned long base
= (unsigned long)i
->iov
[k
].iov_base
;
1300 if (v
) // if not the first one
1301 res
|= base
| v
; // this start | previous end
1302 v
= base
+ i
->iov
[k
].iov_len
;
1303 if (size
<= i
->iov
[k
].iov_len
)
1305 size
-= i
->iov
[k
].iov_len
;
1310 EXPORT_SYMBOL(iov_iter_gap_alignment
);
1312 static inline ssize_t
__pipe_get_pages(struct iov_iter
*i
,
1314 struct page
**pages
,
1318 struct pipe_inode_info
*pipe
= i
->pipe
;
1319 unsigned int p_mask
= pipe
->ring_size
- 1;
1320 ssize_t n
= push_pipe(i
, maxsize
, &iter_head
, start
);
1327 get_page(*pages
++ = pipe
->bufs
[iter_head
& p_mask
].page
);
1335 static ssize_t
pipe_get_pages(struct iov_iter
*i
,
1336 struct page
**pages
, size_t maxsize
, unsigned maxpages
,
1339 unsigned int iter_head
, npages
;
1345 data_start(i
, &iter_head
, start
);
1346 /* Amount of free space: some of this one + all after this one */
1347 npages
= pipe_space_for_user(iter_head
, i
->pipe
->tail
, i
->pipe
);
1348 capacity
= min(npages
, maxpages
) * PAGE_SIZE
- *start
;
1350 return __pipe_get_pages(i
, min(maxsize
, capacity
), pages
, iter_head
, start
);
1353 static ssize_t
iter_xarray_populate_pages(struct page
**pages
, struct xarray
*xa
,
1354 pgoff_t index
, unsigned int nr_pages
)
1356 XA_STATE(xas
, xa
, index
);
1358 unsigned int ret
= 0;
1361 for (page
= xas_load(&xas
); page
; page
= xas_next(&xas
)) {
1362 if (xas_retry(&xas
, page
))
1365 /* Has the page moved or been split? */
1366 if (unlikely(page
!= xas_reload(&xas
))) {
1371 pages
[ret
] = find_subpage(page
, xas
.xa_index
);
1372 get_page(pages
[ret
]);
1373 if (++ret
== nr_pages
)
1380 static ssize_t
iter_xarray_get_pages(struct iov_iter
*i
,
1381 struct page
**pages
, size_t maxsize
,
1382 unsigned maxpages
, size_t *_start_offset
)
1384 unsigned nr
, offset
;
1385 pgoff_t index
, count
;
1386 size_t size
= maxsize
, actual
;
1389 if (!size
|| !maxpages
)
1392 pos
= i
->xarray_start
+ i
->iov_offset
;
1393 index
= pos
>> PAGE_SHIFT
;
1394 offset
= pos
& ~PAGE_MASK
;
1395 *_start_offset
= offset
;
1398 if (size
> PAGE_SIZE
- offset
) {
1399 size
-= PAGE_SIZE
- offset
;
1400 count
+= size
>> PAGE_SHIFT
;
1406 if (count
> maxpages
)
1409 nr
= iter_xarray_populate_pages(pages
, i
->xarray
, index
, count
);
1413 actual
= PAGE_SIZE
* nr
;
1415 if (nr
== count
&& size
> 0) {
1416 unsigned last_offset
= (nr
> 1) ? 0 : offset
;
1417 actual
-= PAGE_SIZE
- (last_offset
+ size
);
1422 /* must be done on non-empty ITER_IOVEC one */
1423 static unsigned long first_iovec_segment(const struct iov_iter
*i
,
1424 size_t *size
, size_t *start
,
1425 size_t maxsize
, unsigned maxpages
)
1430 for (k
= 0, skip
= i
->iov_offset
; k
< i
->nr_segs
; k
++, skip
= 0) {
1431 unsigned long addr
= (unsigned long)i
->iov
[k
].iov_base
+ skip
;
1432 size_t len
= i
->iov
[k
].iov_len
- skip
;
1438 len
+= (*start
= addr
% PAGE_SIZE
);
1439 if (len
> maxpages
* PAGE_SIZE
)
1440 len
= maxpages
* PAGE_SIZE
;
1442 return addr
& PAGE_MASK
;
1444 BUG(); // if it had been empty, we wouldn't get called
1447 /* must be done on non-empty ITER_BVEC one */
1448 static struct page
*first_bvec_segment(const struct iov_iter
*i
,
1449 size_t *size
, size_t *start
,
1450 size_t maxsize
, unsigned maxpages
)
1453 size_t skip
= i
->iov_offset
, len
;
1455 len
= i
->bvec
->bv_len
- skip
;
1458 skip
+= i
->bvec
->bv_offset
;
1459 page
= i
->bvec
->bv_page
+ skip
/ PAGE_SIZE
;
1460 len
+= (*start
= skip
% PAGE_SIZE
);
1461 if (len
> maxpages
* PAGE_SIZE
)
1462 len
= maxpages
* PAGE_SIZE
;
1467 ssize_t
iov_iter_get_pages(struct iov_iter
*i
,
1468 struct page
**pages
, size_t maxsize
, unsigned maxpages
,
1474 if (maxsize
> i
->count
)
1479 if (likely(iter_is_iovec(i
))) {
1482 addr
= first_iovec_segment(i
, &len
, start
, maxsize
, maxpages
);
1483 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
1484 res
= get_user_pages_fast(addr
, n
,
1485 iov_iter_rw(i
) != WRITE
? FOLL_WRITE
: 0,
1487 if (unlikely(res
< 0))
1489 return (res
== n
? len
: res
* PAGE_SIZE
) - *start
;
1491 if (iov_iter_is_bvec(i
)) {
1494 page
= first_bvec_segment(i
, &len
, start
, maxsize
, maxpages
);
1495 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
1497 get_page(*pages
++ = page
++);
1498 return len
- *start
;
1500 if (iov_iter_is_pipe(i
))
1501 return pipe_get_pages(i
, pages
, maxsize
, maxpages
, start
);
1502 if (iov_iter_is_xarray(i
))
1503 return iter_xarray_get_pages(i
, pages
, maxsize
, maxpages
, start
);
1506 EXPORT_SYMBOL(iov_iter_get_pages
);
1508 static struct page
**get_pages_array(size_t n
)
1510 return kvmalloc_array(n
, sizeof(struct page
*), GFP_KERNEL
);
1513 static ssize_t
pipe_get_pages_alloc(struct iov_iter
*i
,
1514 struct page
***pages
, size_t maxsize
,
1518 unsigned int iter_head
, npages
;
1524 data_start(i
, &iter_head
, start
);
1525 /* Amount of free space: some of this one + all after this one */
1526 npages
= pipe_space_for_user(iter_head
, i
->pipe
->tail
, i
->pipe
);
1527 n
= npages
* PAGE_SIZE
- *start
;
1531 npages
= DIV_ROUND_UP(maxsize
+ *start
, PAGE_SIZE
);
1532 p
= get_pages_array(npages
);
1535 n
= __pipe_get_pages(i
, maxsize
, p
, iter_head
, start
);
1543 static ssize_t
iter_xarray_get_pages_alloc(struct iov_iter
*i
,
1544 struct page
***pages
, size_t maxsize
,
1545 size_t *_start_offset
)
1548 unsigned nr
, offset
;
1549 pgoff_t index
, count
;
1550 size_t size
= maxsize
, actual
;
1556 pos
= i
->xarray_start
+ i
->iov_offset
;
1557 index
= pos
>> PAGE_SHIFT
;
1558 offset
= pos
& ~PAGE_MASK
;
1559 *_start_offset
= offset
;
1562 if (size
> PAGE_SIZE
- offset
) {
1563 size
-= PAGE_SIZE
- offset
;
1564 count
+= size
>> PAGE_SHIFT
;
1570 p
= get_pages_array(count
);
1575 nr
= iter_xarray_populate_pages(p
, i
->xarray
, index
, count
);
1579 actual
= PAGE_SIZE
* nr
;
1581 if (nr
== count
&& size
> 0) {
1582 unsigned last_offset
= (nr
> 1) ? 0 : offset
;
1583 actual
-= PAGE_SIZE
- (last_offset
+ size
);
1588 ssize_t
iov_iter_get_pages_alloc(struct iov_iter
*i
,
1589 struct page
***pages
, size_t maxsize
,
1596 if (maxsize
> i
->count
)
1601 if (likely(iter_is_iovec(i
))) {
1604 addr
= first_iovec_segment(i
, &len
, start
, maxsize
, ~0U);
1605 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
1606 p
= get_pages_array(n
);
1609 res
= get_user_pages_fast(addr
, n
,
1610 iov_iter_rw(i
) != WRITE
? FOLL_WRITE
: 0, p
);
1611 if (unlikely(res
< 0)) {
1616 return (res
== n
? len
: res
* PAGE_SIZE
) - *start
;
1618 if (iov_iter_is_bvec(i
)) {
1621 page
= first_bvec_segment(i
, &len
, start
, maxsize
, ~0U);
1622 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
1623 *pages
= p
= get_pages_array(n
);
1627 get_page(*p
++ = page
++);
1628 return len
- *start
;
1630 if (iov_iter_is_pipe(i
))
1631 return pipe_get_pages_alloc(i
, pages
, maxsize
, start
);
1632 if (iov_iter_is_xarray(i
))
1633 return iter_xarray_get_pages_alloc(i
, pages
, maxsize
, start
);
1636 EXPORT_SYMBOL(iov_iter_get_pages_alloc
);
1638 size_t csum_and_copy_from_iter(void *addr
, size_t bytes
, __wsum
*csum
,
1643 if (unlikely(iov_iter_is_pipe(i
) || iov_iter_is_discard(i
))) {
1647 iterate_and_advance(i
, bytes
, base
, len
, off
, ({
1648 next
= csum_and_copy_from_user(base
, addr
+ off
, len
);
1649 sum
= csum_block_add(sum
, next
, off
);
1652 sum
= csum_and_memcpy(addr
+ off
, base
, len
, sum
, off
);
1658 EXPORT_SYMBOL(csum_and_copy_from_iter
);
1660 size_t csum_and_copy_to_iter(const void *addr
, size_t bytes
, void *_csstate
,
1663 struct csum_state
*csstate
= _csstate
;
1666 if (unlikely(iov_iter_is_discard(i
))) {
1667 WARN_ON(1); /* for now */
1671 sum
= csum_shift(csstate
->csum
, csstate
->off
);
1672 if (unlikely(iov_iter_is_pipe(i
)))
1673 bytes
= csum_and_copy_to_pipe_iter(addr
, bytes
, i
, &sum
);
1674 else iterate_and_advance(i
, bytes
, base
, len
, off
, ({
1675 next
= csum_and_copy_to_user(addr
+ off
, base
, len
);
1676 sum
= csum_block_add(sum
, next
, off
);
1679 sum
= csum_and_memcpy(base
, addr
+ off
, len
, sum
, off
);
1682 csstate
->csum
= csum_shift(sum
, csstate
->off
);
1683 csstate
->off
+= bytes
;
1686 EXPORT_SYMBOL(csum_and_copy_to_iter
);
1688 size_t hash_and_copy_to_iter(const void *addr
, size_t bytes
, void *hashp
,
1691 #ifdef CONFIG_CRYPTO_HASH
1692 struct ahash_request
*hash
= hashp
;
1693 struct scatterlist sg
;
1696 copied
= copy_to_iter(addr
, bytes
, i
);
1697 sg_init_one(&sg
, addr
, copied
);
1698 ahash_request_set_crypt(hash
, &sg
, NULL
, copied
);
1699 crypto_ahash_update(hash
);
1705 EXPORT_SYMBOL(hash_and_copy_to_iter
);
1707 static int iov_npages(const struct iov_iter
*i
, int maxpages
)
1709 size_t skip
= i
->iov_offset
, size
= i
->count
;
1710 const struct iovec
*p
;
1713 for (p
= i
->iov
; size
; skip
= 0, p
++) {
1714 unsigned offs
= offset_in_page(p
->iov_base
+ skip
);
1715 size_t len
= min(p
->iov_len
- skip
, size
);
1719 npages
+= DIV_ROUND_UP(offs
+ len
, PAGE_SIZE
);
1720 if (unlikely(npages
> maxpages
))
1727 static int bvec_npages(const struct iov_iter
*i
, int maxpages
)
1729 size_t skip
= i
->iov_offset
, size
= i
->count
;
1730 const struct bio_vec
*p
;
1733 for (p
= i
->bvec
; size
; skip
= 0, p
++) {
1734 unsigned offs
= (p
->bv_offset
+ skip
) % PAGE_SIZE
;
1735 size_t len
= min(p
->bv_len
- skip
, size
);
1738 npages
+= DIV_ROUND_UP(offs
+ len
, PAGE_SIZE
);
1739 if (unlikely(npages
> maxpages
))
1745 int iov_iter_npages(const struct iov_iter
*i
, int maxpages
)
1747 if (unlikely(!i
->count
))
1749 /* iovec and kvec have identical layouts */
1750 if (likely(iter_is_iovec(i
) || iov_iter_is_kvec(i
)))
1751 return iov_npages(i
, maxpages
);
1752 if (iov_iter_is_bvec(i
))
1753 return bvec_npages(i
, maxpages
);
1754 if (iov_iter_is_pipe(i
)) {
1755 unsigned int iter_head
;
1762 data_start(i
, &iter_head
, &off
);
1763 /* some of this one + all after this one */
1764 npages
= pipe_space_for_user(iter_head
, i
->pipe
->tail
, i
->pipe
);
1765 return min(npages
, maxpages
);
1767 if (iov_iter_is_xarray(i
)) {
1768 unsigned offset
= (i
->xarray_start
+ i
->iov_offset
) % PAGE_SIZE
;
1769 int npages
= DIV_ROUND_UP(offset
+ i
->count
, PAGE_SIZE
);
1770 return min(npages
, maxpages
);
1774 EXPORT_SYMBOL(iov_iter_npages
);
1776 const void *dup_iter(struct iov_iter
*new, struct iov_iter
*old
, gfp_t flags
)
1779 if (unlikely(iov_iter_is_pipe(new))) {
1783 if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
1785 if (iov_iter_is_bvec(new))
1786 return new->bvec
= kmemdup(new->bvec
,
1787 new->nr_segs
* sizeof(struct bio_vec
),
1790 /* iovec and kvec have identical layout */
1791 return new->iov
= kmemdup(new->iov
,
1792 new->nr_segs
* sizeof(struct iovec
),
1795 EXPORT_SYMBOL(dup_iter
);
1797 static int copy_compat_iovec_from_user(struct iovec
*iov
,
1798 const struct iovec __user
*uvec
, unsigned long nr_segs
)
1800 const struct compat_iovec __user
*uiov
=
1801 (const struct compat_iovec __user
*)uvec
;
1802 int ret
= -EFAULT
, i
;
1804 if (!user_access_begin(uiov
, nr_segs
* sizeof(*uiov
)))
1807 for (i
= 0; i
< nr_segs
; i
++) {
1811 unsafe_get_user(len
, &uiov
[i
].iov_len
, uaccess_end
);
1812 unsafe_get_user(buf
, &uiov
[i
].iov_base
, uaccess_end
);
1814 /* check for compat_size_t not fitting in compat_ssize_t .. */
1819 iov
[i
].iov_base
= compat_ptr(buf
);
1820 iov
[i
].iov_len
= len
;
1829 static int copy_iovec_from_user(struct iovec
*iov
,
1830 const struct iovec __user
*uvec
, unsigned long nr_segs
)
1834 if (copy_from_user(iov
, uvec
, nr_segs
* sizeof(*uvec
)))
1836 for (seg
= 0; seg
< nr_segs
; seg
++) {
1837 if ((ssize_t
)iov
[seg
].iov_len
< 0)
1844 struct iovec
*iovec_from_user(const struct iovec __user
*uvec
,
1845 unsigned long nr_segs
, unsigned long fast_segs
,
1846 struct iovec
*fast_iov
, bool compat
)
1848 struct iovec
*iov
= fast_iov
;
1852 * SuS says "The readv() function *may* fail if the iovcnt argument was
1853 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
1854 * traditionally returned zero for zero segments, so...
1858 if (nr_segs
> UIO_MAXIOV
)
1859 return ERR_PTR(-EINVAL
);
1860 if (nr_segs
> fast_segs
) {
1861 iov
= kmalloc_array(nr_segs
, sizeof(struct iovec
), GFP_KERNEL
);
1863 return ERR_PTR(-ENOMEM
);
1867 ret
= copy_compat_iovec_from_user(iov
, uvec
, nr_segs
);
1869 ret
= copy_iovec_from_user(iov
, uvec
, nr_segs
);
1871 if (iov
!= fast_iov
)
1873 return ERR_PTR(ret
);
1879 ssize_t
__import_iovec(int type
, const struct iovec __user
*uvec
,
1880 unsigned nr_segs
, unsigned fast_segs
, struct iovec
**iovp
,
1881 struct iov_iter
*i
, bool compat
)
1883 ssize_t total_len
= 0;
1887 iov
= iovec_from_user(uvec
, nr_segs
, fast_segs
, *iovp
, compat
);
1890 return PTR_ERR(iov
);
1894 * According to the Single Unix Specification we should return EINVAL if
1895 * an element length is < 0 when cast to ssize_t or if the total length
1896 * would overflow the ssize_t return value of the system call.
1898 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1901 for (seg
= 0; seg
< nr_segs
; seg
++) {
1902 ssize_t len
= (ssize_t
)iov
[seg
].iov_len
;
1904 if (!access_ok(iov
[seg
].iov_base
, len
)) {
1911 if (len
> MAX_RW_COUNT
- total_len
) {
1912 len
= MAX_RW_COUNT
- total_len
;
1913 iov
[seg
].iov_len
= len
;
1918 iov_iter_init(i
, type
, iov
, nr_segs
, total_len
);
1927 * import_iovec() - Copy an array of &struct iovec from userspace
1928 * into the kernel, check that it is valid, and initialize a new
1929 * &struct iov_iter iterator to access it.
1931 * @type: One of %READ or %WRITE.
1932 * @uvec: Pointer to the userspace array.
1933 * @nr_segs: Number of elements in userspace array.
1934 * @fast_segs: Number of elements in @iov.
1935 * @iovp: (input and output parameter) Pointer to pointer to (usually small
1936 * on-stack) kernel array.
1937 * @i: Pointer to iterator that will be initialized on success.
1939 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1940 * then this function places %NULL in *@iov on return. Otherwise, a new
1941 * array will be allocated and the result placed in *@iov. This means that
1942 * the caller may call kfree() on *@iov regardless of whether the small
1943 * on-stack array was used or not (and regardless of whether this function
1944 * returns an error or not).
1946 * Return: Negative error code on error, bytes imported on success
1948 ssize_t
import_iovec(int type
, const struct iovec __user
*uvec
,
1949 unsigned nr_segs
, unsigned fast_segs
,
1950 struct iovec
**iovp
, struct iov_iter
*i
)
1952 return __import_iovec(type
, uvec
, nr_segs
, fast_segs
, iovp
, i
,
1953 in_compat_syscall());
1955 EXPORT_SYMBOL(import_iovec
);
1957 int import_single_range(int rw
, void __user
*buf
, size_t len
,
1958 struct iovec
*iov
, struct iov_iter
*i
)
1960 if (len
> MAX_RW_COUNT
)
1962 if (unlikely(!access_ok(buf
, len
)))
1965 iov
->iov_base
= buf
;
1967 iov_iter_init(i
, rw
, iov
, 1, len
);
1970 EXPORT_SYMBOL(import_single_range
);