xref: /openbmc/linux/lib/iov_iter.c (revision 3b2deb0e)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27999096fSHerbert Xu #include <crypto/hash.h>
3d879cb83SAl Viro #include <linux/export.h>
42f8b5444SChristoph Hellwig #include <linux/bvec.h>
54d0e9df5SAlbert van der Linde #include <linux/fault-inject-usercopy.h>
6d879cb83SAl Viro #include <linux/uio.h>
7d879cb83SAl Viro #include <linux/pagemap.h>
828961998SIra Weiny #include <linux/highmem.h>
9d879cb83SAl Viro #include <linux/slab.h>
10d879cb83SAl Viro #include <linux/vmalloc.h>
11241699cdSAl Viro #include <linux/splice.h>
12bfdc5970SChristoph Hellwig #include <linux/compat.h>
13d879cb83SAl Viro #include <net/checksum.h>
14d05f4435SSagi Grimberg #include <linux/scatterlist.h>
15d0ef4c36SMarco Elver #include <linux/instrumented.h>
16d879cb83SAl Viro 
17241699cdSAl Viro #define PIPE_PARANOIA /* for now */
18241699cdSAl Viro 
19fcb14cb1SAl Viro /* covers ubuf and kbuf alike */
20fcb14cb1SAl Viro #define iterate_buf(i, n, base, len, off, __p, STEP) {		\
21fcb14cb1SAl Viro 	size_t __maybe_unused off = 0;				\
22fcb14cb1SAl Viro 	len = n;						\
23fcb14cb1SAl Viro 	base = __p + i->iov_offset;				\
24fcb14cb1SAl Viro 	len -= (STEP);						\
25fcb14cb1SAl Viro 	i->iov_offset += len;					\
26fcb14cb1SAl Viro 	n = len;						\
27fcb14cb1SAl Viro }
28fcb14cb1SAl Viro 
295c67aa90SAl Viro /* covers iovec and kvec alike */
30a6e4ec7bSAl Viro #define iterate_iovec(i, n, base, len, off, __p, STEP) {	\
317baa5099SAl Viro 	size_t off = 0;						\
32a6e4ec7bSAl Viro 	size_t skip = i->iov_offset;				\
337a1bcb5dSAl Viro 	do {							\
347baa5099SAl Viro 		len = min(n, __p->iov_len - skip);		\
357baa5099SAl Viro 		if (likely(len)) {				\
367baa5099SAl Viro 			base = __p->iov_base + skip;		\
377baa5099SAl Viro 			len -= (STEP);				\
387baa5099SAl Viro 			off += len;				\
397baa5099SAl Viro 			skip += len;				\
407baa5099SAl Viro 			n -= len;				\
417a1bcb5dSAl Viro 			if (skip < __p->iov_len)		\
427a1bcb5dSAl Viro 				break;				\
43d879cb83SAl Viro 		}						\
44d879cb83SAl Viro 		__p++;						\
457a1bcb5dSAl Viro 		skip = 0;					\
467a1bcb5dSAl Viro 	} while (n);						\
47a6e4ec7bSAl Viro 	i->iov_offset = skip;					\
487baa5099SAl Viro 	n = off;						\
49d879cb83SAl Viro }
50d879cb83SAl Viro 
51a6e4ec7bSAl Viro #define iterate_bvec(i, n, base, len, off, p, STEP) {		\
527baa5099SAl Viro 	size_t off = 0;						\
53a6e4ec7bSAl Viro 	unsigned skip = i->iov_offset;				\
547491a2bfSAl Viro 	while (n) {						\
557491a2bfSAl Viro 		unsigned offset = p->bv_offset + skip;		\
561b4fb5ffSAl Viro 		unsigned left;					\
5721b56c84SAl Viro 		void *kaddr = kmap_local_page(p->bv_page +	\
5821b56c84SAl Viro 					offset / PAGE_SIZE);	\
597baa5099SAl Viro 		base = kaddr + offset % PAGE_SIZE;		\
60a6e4ec7bSAl Viro 		len = min(min(n, (size_t)(p->bv_len - skip)),	\
617491a2bfSAl Viro 		     (size_t)(PAGE_SIZE - offset % PAGE_SIZE));	\
621b4fb5ffSAl Viro 		left = (STEP);					\
6321b56c84SAl Viro 		kunmap_local(kaddr);				\
647baa5099SAl Viro 		len -= left;					\
657baa5099SAl Viro 		off += len;					\
667baa5099SAl Viro 		skip += len;					\
677491a2bfSAl Viro 		if (skip == p->bv_len) {			\
687491a2bfSAl Viro 			skip = 0;				\
697491a2bfSAl Viro 			p++;					\
70d879cb83SAl Viro 		}						\
717baa5099SAl Viro 		n -= len;					\
721b4fb5ffSAl Viro 		if (left)					\
731b4fb5ffSAl Viro 			break;					\
747491a2bfSAl Viro 	}							\
75a6e4ec7bSAl Viro 	i->iov_offset = skip;					\
767baa5099SAl Viro 	n = off;						\
77d879cb83SAl Viro }
78d879cb83SAl Viro 
79a6e4ec7bSAl Viro #define iterate_xarray(i, n, base, len, __off, STEP) {		\
801b4fb5ffSAl Viro 	__label__ __out;					\
81622838f3SAl Viro 	size_t __off = 0;					\
82821979f5SMatthew Wilcox (Oracle) 	struct folio *folio;					\
83a6e4ec7bSAl Viro 	loff_t start = i->xarray_start + i->iov_offset;		\
844b179e9aSAl Viro 	pgoff_t index = start / PAGE_SIZE;			\
857ff50620SDavid Howells 	XA_STATE(xas, i->xarray, index);			\
867ff50620SDavid Howells 								\
87821979f5SMatthew Wilcox (Oracle) 	len = PAGE_SIZE - offset_in_page(start);		\
887ff50620SDavid Howells 	rcu_read_lock();					\
89821979f5SMatthew Wilcox (Oracle) 	xas_for_each(&xas, folio, ULONG_MAX) {			\
901b4fb5ffSAl Viro 		unsigned left;					\
91821979f5SMatthew Wilcox (Oracle) 		size_t offset;					\
92821979f5SMatthew Wilcox (Oracle) 		if (xas_retry(&xas, folio))			\
937ff50620SDavid Howells 			continue;				\
94821979f5SMatthew Wilcox (Oracle) 		if (WARN_ON(xa_is_value(folio)))		\
957ff50620SDavid Howells 			break;					\
96821979f5SMatthew Wilcox (Oracle) 		if (WARN_ON(folio_test_hugetlb(folio)))		\
977ff50620SDavid Howells 			break;					\
98821979f5SMatthew Wilcox (Oracle) 		offset = offset_in_folio(folio, start + __off);	\
99821979f5SMatthew Wilcox (Oracle) 		while (offset < folio_size(folio)) {		\
100821979f5SMatthew Wilcox (Oracle) 			base = kmap_local_folio(folio, offset);	\
1017baa5099SAl Viro 			len = min(n, len);			\
1021b4fb5ffSAl Viro 			left = (STEP);				\
103821979f5SMatthew Wilcox (Oracle) 			kunmap_local(base);			\
1047baa5099SAl Viro 			len -= left;				\
1057baa5099SAl Viro 			__off += len;				\
1067baa5099SAl Viro 			n -= len;				\
1071b4fb5ffSAl Viro 			if (left || n == 0)			\
1081b4fb5ffSAl Viro 				goto __out;			\
109821979f5SMatthew Wilcox (Oracle) 			offset += len;				\
110821979f5SMatthew Wilcox (Oracle) 			len = PAGE_SIZE;			\
1117ff50620SDavid Howells 		}						\
1127ff50620SDavid Howells 	}							\
1131b4fb5ffSAl Viro __out:								\
1147ff50620SDavid Howells 	rcu_read_unlock();					\
115a6e4ec7bSAl Viro 	i->iov_offset += __off;					\
116622838f3SAl Viro 	n = __off;						\
1177ff50620SDavid Howells }
1187ff50620SDavid Howells 
1197baa5099SAl Viro #define __iterate_and_advance(i, n, base, len, off, I, K) {	\
120dd254f5aSAl Viro 	if (unlikely(i->count < n))				\
121dd254f5aSAl Viro 		n = i->count;					\
122f5da8354SAl Viro 	if (likely(n)) {					\
123fcb14cb1SAl Viro 		if (likely(iter_is_ubuf(i))) {			\
124fcb14cb1SAl Viro 			void __user *base;			\
125fcb14cb1SAl Viro 			size_t len;				\
126fcb14cb1SAl Viro 			iterate_buf(i, n, base, len, off,	\
127fcb14cb1SAl Viro 						i->ubuf, (I)) 	\
128fcb14cb1SAl Viro 		} else if (likely(iter_is_iovec(i))) {		\
129de4f5fedSJens Axboe 			const struct iovec *iov = iter_iov(i);	\
1307baa5099SAl Viro 			void __user *base;			\
1317baa5099SAl Viro 			size_t len;				\
1327baa5099SAl Viro 			iterate_iovec(i, n, base, len, off,	\
133a6e4ec7bSAl Viro 						iov, (I))	\
134de4f5fedSJens Axboe 			i->nr_segs -= iov - iter_iov(i);	\
135de4f5fedSJens Axboe 			i->__iov = iov;				\
13628f38db7SAl Viro 		} else if (iov_iter_is_bvec(i)) {		\
13728f38db7SAl Viro 			const struct bio_vec *bvec = i->bvec;	\
1387baa5099SAl Viro 			void *base;				\
1397baa5099SAl Viro 			size_t len;				\
1407baa5099SAl Viro 			iterate_bvec(i, n, base, len, off,	\
141a6e4ec7bSAl Viro 						bvec, (K))	\
1427491a2bfSAl Viro 			i->nr_segs -= bvec - i->bvec;		\
1437491a2bfSAl Viro 			i->bvec = bvec;				\
14428f38db7SAl Viro 		} else if (iov_iter_is_kvec(i)) {		\
1455c67aa90SAl Viro 			const struct kvec *kvec = i->kvec;	\
1467baa5099SAl Viro 			void *base;				\
1477baa5099SAl Viro 			size_t len;				\
1487baa5099SAl Viro 			iterate_iovec(i, n, base, len, off,	\
149a6e4ec7bSAl Viro 						kvec, (K))	\
15028f38db7SAl Viro 			i->nr_segs -= kvec - i->kvec;		\
15128f38db7SAl Viro 			i->kvec = kvec;				\
15228f38db7SAl Viro 		} else if (iov_iter_is_xarray(i)) {		\
1537baa5099SAl Viro 			void *base;				\
1547baa5099SAl Viro 			size_t len;				\
1557baa5099SAl Viro 			iterate_xarray(i, n, base, len, off,	\
156a6e4ec7bSAl Viro 							(K))	\
157d879cb83SAl Viro 		}						\
158d879cb83SAl Viro 		i->count -= n;					\
159dd254f5aSAl Viro 	}							\
160d879cb83SAl Viro }
1617baa5099SAl Viro #define iterate_and_advance(i, n, base, len, off, I, K) \
1627baa5099SAl Viro 	__iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
163d879cb83SAl Viro 
16409fc68dcSAl Viro static int copyout(void __user *to, const void *from, size_t n)
16509fc68dcSAl Viro {
1664d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1674d0e9df5SAlbert van der Linde 		return n;
16896d4f267SLinus Torvalds 	if (access_ok(to, n)) {
169d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
17009fc68dcSAl Viro 		n = raw_copy_to_user(to, from, n);
17109fc68dcSAl Viro 	}
17209fc68dcSAl Viro 	return n;
17309fc68dcSAl Viro }
17409fc68dcSAl Viro 
17509fc68dcSAl Viro static int copyin(void *to, const void __user *from, size_t n)
17609fc68dcSAl Viro {
17733b75c1dSAlexander Potapenko 	size_t res = n;
17833b75c1dSAlexander Potapenko 
1794d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1804d0e9df5SAlbert van der Linde 		return n;
18196d4f267SLinus Torvalds 	if (access_ok(from, n)) {
18233b75c1dSAlexander Potapenko 		instrument_copy_from_user_before(to, from, n);
18333b75c1dSAlexander Potapenko 		res = raw_copy_from_user(to, from, n);
18433b75c1dSAlexander Potapenko 		instrument_copy_from_user_after(to, from, n, res);
18509fc68dcSAl Viro 	}
18633b75c1dSAlexander Potapenko 	return res;
18709fc68dcSAl Viro }
18809fc68dcSAl Viro 
189241699cdSAl Viro #ifdef PIPE_PARANOIA
190241699cdSAl Viro static bool sanity(const struct iov_iter *i)
191241699cdSAl Viro {
192241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
1938cefc107SDavid Howells 	unsigned int p_head = pipe->head;
1948cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
1958cefc107SDavid Howells 	unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
1968cefc107SDavid Howells 	unsigned int i_head = i->head;
1978cefc107SDavid Howells 	unsigned int idx;
1988cefc107SDavid Howells 
19910f525a8SAl Viro 	if (i->last_offset) {
200241699cdSAl Viro 		struct pipe_buffer *p;
2018cefc107SDavid Howells 		if (unlikely(p_occupancy == 0))
202241699cdSAl Viro 			goto Bad;	// pipe must be non-empty
2038cefc107SDavid Howells 		if (unlikely(i_head != p_head - 1))
204241699cdSAl Viro 			goto Bad;	// must be at the last buffer...
205241699cdSAl Viro 
2062dcedb2aSAl Viro 		p = pipe_buf(pipe, i_head);
20710f525a8SAl Viro 		if (unlikely(p->offset + p->len != abs(i->last_offset)))
208241699cdSAl Viro 			goto Bad;	// ... at the end of segment
209241699cdSAl Viro 	} else {
2108cefc107SDavid Howells 		if (i_head != p_head)
211241699cdSAl Viro 			goto Bad;	// must be right after the last buffer
212241699cdSAl Viro 	}
213241699cdSAl Viro 	return true;
214241699cdSAl Viro Bad:
21510f525a8SAl Viro 	printk(KERN_ERR "idx = %d, offset = %d\n", i_head, i->last_offset);
2168cefc107SDavid Howells 	printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
2178cefc107SDavid Howells 			p_head, p_tail, pipe->ring_size);
2188cefc107SDavid Howells 	for (idx = 0; idx < pipe->ring_size; idx++)
219241699cdSAl Viro 		printk(KERN_ERR "[%p %p %d %d]\n",
220241699cdSAl Viro 			pipe->bufs[idx].ops,
221241699cdSAl Viro 			pipe->bufs[idx].page,
222241699cdSAl Viro 			pipe->bufs[idx].offset,
223241699cdSAl Viro 			pipe->bufs[idx].len);
224241699cdSAl Viro 	WARN_ON(1);
225241699cdSAl Viro 	return false;
226241699cdSAl Viro }
227241699cdSAl Viro #else
228241699cdSAl Viro #define sanity(i) true
229241699cdSAl Viro #endif
230241699cdSAl Viro 
23147b7fcaeSAl Viro static struct page *push_anon(struct pipe_inode_info *pipe, unsigned size)
23247b7fcaeSAl Viro {
23347b7fcaeSAl Viro 	struct page *page = alloc_page(GFP_USER);
23447b7fcaeSAl Viro 	if (page) {
23547b7fcaeSAl Viro 		struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
23647b7fcaeSAl Viro 		*buf = (struct pipe_buffer) {
23747b7fcaeSAl Viro 			.ops = &default_pipe_buf_ops,
23847b7fcaeSAl Viro 			.page = page,
23947b7fcaeSAl Viro 			.offset = 0,
24047b7fcaeSAl Viro 			.len = size
24147b7fcaeSAl Viro 		};
24247b7fcaeSAl Viro 	}
24347b7fcaeSAl Viro 	return page;
24447b7fcaeSAl Viro }
24547b7fcaeSAl Viro 
24647b7fcaeSAl Viro static void push_page(struct pipe_inode_info *pipe, struct page *page,
24747b7fcaeSAl Viro 			unsigned int offset, unsigned int size)
24847b7fcaeSAl Viro {
24947b7fcaeSAl Viro 	struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
25047b7fcaeSAl Viro 	*buf = (struct pipe_buffer) {
25147b7fcaeSAl Viro 		.ops = &page_cache_pipe_buf_ops,
25247b7fcaeSAl Viro 		.page = page,
25347b7fcaeSAl Viro 		.offset = offset,
25447b7fcaeSAl Viro 		.len = size
25547b7fcaeSAl Viro 	};
25647b7fcaeSAl Viro 	get_page(page);
25747b7fcaeSAl Viro }
25847b7fcaeSAl Viro 
25910f525a8SAl Viro static inline int last_offset(const struct pipe_buffer *buf)
2608fad7767SAl Viro {
26110f525a8SAl Viro 	if (buf->ops == &default_pipe_buf_ops)
26210f525a8SAl Viro 		return buf->len;	// buf->offset is 0 for those
26310f525a8SAl Viro 	else
26410f525a8SAl Viro 		return -(buf->offset + buf->len);
2658fad7767SAl Viro }
2668fad7767SAl Viro 
2678fad7767SAl Viro static struct page *append_pipe(struct iov_iter *i, size_t size,
2688fad7767SAl Viro 				unsigned int *off)
2698fad7767SAl Viro {
2708fad7767SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
27110f525a8SAl Viro 	int offset = i->last_offset;
2728fad7767SAl Viro 	struct pipe_buffer *buf;
2738fad7767SAl Viro 	struct page *page;
2748fad7767SAl Viro 
27510f525a8SAl Viro 	if (offset > 0 && offset < PAGE_SIZE) {
27610f525a8SAl Viro 		// some space in the last buffer; add to it
2778fad7767SAl Viro 		buf = pipe_buf(pipe, pipe->head - 1);
2788fad7767SAl Viro 		size = min_t(size_t, size, PAGE_SIZE - offset);
2798fad7767SAl Viro 		buf->len += size;
28010f525a8SAl Viro 		i->last_offset += size;
2818fad7767SAl Viro 		i->count -= size;
2828fad7767SAl Viro 		*off = offset;
2838fad7767SAl Viro 		return buf->page;
2848fad7767SAl Viro 	}
2858fad7767SAl Viro 	// OK, we need a new buffer
2868fad7767SAl Viro 	*off = 0;
2878fad7767SAl Viro 	size = min_t(size_t, size, PAGE_SIZE);
2888fad7767SAl Viro 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
2898fad7767SAl Viro 		return NULL;
2908fad7767SAl Viro 	page = push_anon(pipe, size);
2918fad7767SAl Viro 	if (!page)
2928fad7767SAl Viro 		return NULL;
2938fad7767SAl Viro 	i->head = pipe->head - 1;
29410f525a8SAl Viro 	i->last_offset = size;
2958fad7767SAl Viro 	i->count -= size;
2968fad7767SAl Viro 	return page;
2978fad7767SAl Viro }
2988fad7767SAl Viro 
299241699cdSAl Viro static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
300241699cdSAl Viro 			 struct iov_iter *i)
301241699cdSAl Viro {
302241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
30347b7fcaeSAl Viro 	unsigned int head = pipe->head;
304241699cdSAl Viro 
305241699cdSAl Viro 	if (unlikely(bytes > i->count))
306241699cdSAl Viro 		bytes = i->count;
307241699cdSAl Viro 
308241699cdSAl Viro 	if (unlikely(!bytes))
309241699cdSAl Viro 		return 0;
310241699cdSAl Viro 
311241699cdSAl Viro 	if (!sanity(i))
312241699cdSAl Viro 		return 0;
313241699cdSAl Viro 
31410f525a8SAl Viro 	if (offset && i->last_offset == -offset) { // could we merge it?
31547b7fcaeSAl Viro 		struct pipe_buffer *buf = pipe_buf(pipe, head - 1);
31647b7fcaeSAl Viro 		if (buf->page == page) {
317241699cdSAl Viro 			buf->len += bytes;
31810f525a8SAl Viro 			i->last_offset -= bytes;
31947b7fcaeSAl Viro 			i->count -= bytes;
32047b7fcaeSAl Viro 			return bytes;
321241699cdSAl Viro 		}
322241699cdSAl Viro 	}
32347b7fcaeSAl Viro 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
324241699cdSAl Viro 		return 0;
3258cefc107SDavid Howells 
32647b7fcaeSAl Viro 	push_page(pipe, page, offset, bytes);
32710f525a8SAl Viro 	i->last_offset = -(offset + bytes);
32847b7fcaeSAl Viro 	i->head = head;
329241699cdSAl Viro 	i->count -= bytes;
330241699cdSAl Viro 	return bytes;
331241699cdSAl Viro }
332241699cdSAl Viro 
333d879cb83SAl Viro /*
334a6294593SAndreas Gruenbacher  * fault_in_iov_iter_readable - fault in iov iterator for reading
335a6294593SAndreas Gruenbacher  * @i: iterator
336a6294593SAndreas Gruenbacher  * @size: maximum length
337171a0203SAnton Altaparmakov  *
338a6294593SAndreas Gruenbacher  * Fault in one or more iovecs of the given iov_iter, to a maximum length of
339a6294593SAndreas Gruenbacher  * @size.  For each iovec, fault in each page that constitutes the iovec.
340a6294593SAndreas Gruenbacher  *
341a6294593SAndreas Gruenbacher  * Returns the number of bytes not faulted in (like copy_to_user() and
342a6294593SAndreas Gruenbacher  * copy_from_user()).
343a6294593SAndreas Gruenbacher  *
344a6294593SAndreas Gruenbacher  * Always returns 0 for non-userspace iterators.
345171a0203SAnton Altaparmakov  */
346a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
347171a0203SAnton Altaparmakov {
348fcb14cb1SAl Viro 	if (iter_is_ubuf(i)) {
349fcb14cb1SAl Viro 		size_t n = min(size, iov_iter_count(i));
350fcb14cb1SAl Viro 		n -= fault_in_readable(i->ubuf + i->iov_offset, n);
351fcb14cb1SAl Viro 		return size - n;
352fcb14cb1SAl Viro 	} else if (iter_is_iovec(i)) {
353a6294593SAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
3548409a0d2SAl Viro 		const struct iovec *p;
3558409a0d2SAl Viro 		size_t skip;
3568409a0d2SAl Viro 
357a6294593SAndreas Gruenbacher 		size -= count;
358de4f5fedSJens Axboe 		for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) {
359a6294593SAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
360a6294593SAndreas Gruenbacher 			size_t ret;
3618409a0d2SAl Viro 
3628409a0d2SAl Viro 			if (unlikely(!len))
3638409a0d2SAl Viro 				continue;
364a6294593SAndreas Gruenbacher 			ret = fault_in_readable(p->iov_base + skip, len);
365a6294593SAndreas Gruenbacher 			count -= len - ret;
366a6294593SAndreas Gruenbacher 			if (ret)
367a6294593SAndreas Gruenbacher 				break;
3688409a0d2SAl Viro 		}
369a6294593SAndreas Gruenbacher 		return count + size;
370171a0203SAnton Altaparmakov 	}
371171a0203SAnton Altaparmakov 	return 0;
372171a0203SAnton Altaparmakov }
373a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable);
374171a0203SAnton Altaparmakov 
375cdd591fcSAndreas Gruenbacher /*
376cdd591fcSAndreas Gruenbacher  * fault_in_iov_iter_writeable - fault in iov iterator for writing
377cdd591fcSAndreas Gruenbacher  * @i: iterator
378cdd591fcSAndreas Gruenbacher  * @size: maximum length
379cdd591fcSAndreas Gruenbacher  *
380cdd591fcSAndreas Gruenbacher  * Faults in the iterator using get_user_pages(), i.e., without triggering
381cdd591fcSAndreas Gruenbacher  * hardware page faults.  This is primarily useful when we already know that
382cdd591fcSAndreas Gruenbacher  * some or all of the pages in @i aren't in memory.
383cdd591fcSAndreas Gruenbacher  *
384cdd591fcSAndreas Gruenbacher  * Returns the number of bytes not faulted in, like copy_to_user() and
385cdd591fcSAndreas Gruenbacher  * copy_from_user().
386cdd591fcSAndreas Gruenbacher  *
387cdd591fcSAndreas Gruenbacher  * Always returns 0 for non-user-space iterators.
388cdd591fcSAndreas Gruenbacher  */
389cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
390cdd591fcSAndreas Gruenbacher {
391fcb14cb1SAl Viro 	if (iter_is_ubuf(i)) {
392fcb14cb1SAl Viro 		size_t n = min(size, iov_iter_count(i));
393fcb14cb1SAl Viro 		n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n);
394fcb14cb1SAl Viro 		return size - n;
395fcb14cb1SAl Viro 	} else if (iter_is_iovec(i)) {
396cdd591fcSAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
397cdd591fcSAndreas Gruenbacher 		const struct iovec *p;
398cdd591fcSAndreas Gruenbacher 		size_t skip;
399cdd591fcSAndreas Gruenbacher 
400cdd591fcSAndreas Gruenbacher 		size -= count;
401de4f5fedSJens Axboe 		for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) {
402cdd591fcSAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
403cdd591fcSAndreas Gruenbacher 			size_t ret;
404cdd591fcSAndreas Gruenbacher 
405cdd591fcSAndreas Gruenbacher 			if (unlikely(!len))
406cdd591fcSAndreas Gruenbacher 				continue;
407cdd591fcSAndreas Gruenbacher 			ret = fault_in_safe_writeable(p->iov_base + skip, len);
408cdd591fcSAndreas Gruenbacher 			count -= len - ret;
409cdd591fcSAndreas Gruenbacher 			if (ret)
410cdd591fcSAndreas Gruenbacher 				break;
411cdd591fcSAndreas Gruenbacher 		}
412cdd591fcSAndreas Gruenbacher 		return count + size;
413cdd591fcSAndreas Gruenbacher 	}
414cdd591fcSAndreas Gruenbacher 	return 0;
415cdd591fcSAndreas Gruenbacher }
416cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable);
417cdd591fcSAndreas Gruenbacher 
418aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction,
419d879cb83SAl Viro 			const struct iovec *iov, unsigned long nr_segs,
420d879cb83SAl Viro 			size_t count)
421d879cb83SAl Viro {
422aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
4238cd54c1cSAl Viro 	*i = (struct iov_iter) {
4248cd54c1cSAl Viro 		.iter_type = ITER_IOVEC,
4253337ab08SAndreas Gruenbacher 		.nofault = false,
426fcb14cb1SAl Viro 		.user_backed = true,
4278cd54c1cSAl Viro 		.data_source = direction,
428de4f5fedSJens Axboe 		.__iov = iov,
4298cd54c1cSAl Viro 		.nr_segs = nr_segs,
4308cd54c1cSAl Viro 		.iov_offset = 0,
4318cd54c1cSAl Viro 		.count = count
4328cd54c1cSAl Viro 	};
433d879cb83SAl Viro }
434d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init);
435d879cb83SAl Viro 
43612d426abSAl Viro // returns the offset in partial buffer (if any)
43712d426abSAl Viro static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages)
438241699cdSAl Viro {
43912d426abSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
44012d426abSAl Viro 	int used = pipe->head - pipe->tail;
44110f525a8SAl Viro 	int off = i->last_offset;
4428cefc107SDavid Howells 
44312d426abSAl Viro 	*npages = max((int)pipe->max_usage - used, 0);
44412d426abSAl Viro 
44510f525a8SAl Viro 	if (off > 0 && off < PAGE_SIZE) { // anon and not full
44612d426abSAl Viro 		(*npages)++;
44712d426abSAl Viro 		return off;
44810f525a8SAl Viro 	}
44912d426abSAl Viro 	return 0;
450241699cdSAl Viro }
451241699cdSAl Viro 
452241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
453241699cdSAl Viro 				struct iov_iter *i)
454241699cdSAl Viro {
4558fad7767SAl Viro 	unsigned int off, chunk;
4568fad7767SAl Viro 
4578fad7767SAl Viro 	if (unlikely(bytes > i->count))
4588fad7767SAl Viro 		bytes = i->count;
4598fad7767SAl Viro 	if (unlikely(!bytes))
4608fad7767SAl Viro 		return 0;
461241699cdSAl Viro 
462241699cdSAl Viro 	if (!sanity(i))
463241699cdSAl Viro 		return 0;
464241699cdSAl Viro 
4658fad7767SAl Viro 	for (size_t n = bytes; n; n -= chunk) {
4668fad7767SAl Viro 		struct page *page = append_pipe(i, n, &off);
4678fad7767SAl Viro 		chunk = min_t(size_t, n, PAGE_SIZE - off);
4688fad7767SAl Viro 		if (!page)
4698fad7767SAl Viro 			return bytes - n;
4708fad7767SAl Viro 		memcpy_to_page(page, off, addr, chunk);
471241699cdSAl Viro 		addr += chunk;
4728fad7767SAl Viro 	}
473241699cdSAl Viro 	return bytes;
474241699cdSAl Viro }
475241699cdSAl Viro 
476f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
477f9152895SAl Viro 			      __wsum sum, size_t off)
478f9152895SAl Viro {
479cc44c17bSAl Viro 	__wsum next = csum_partial_copy_nocheck(from, to, len);
480f9152895SAl Viro 	return csum_block_add(sum, next, off);
481f9152895SAl Viro }
482f9152895SAl Viro 
48378e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
4846852df12SAl Viro 					 struct iov_iter *i, __wsum *sump)
48578e1f386SAl Viro {
4866852df12SAl Viro 	__wsum sum = *sump;
4876852df12SAl Viro 	size_t off = 0;
4888fad7767SAl Viro 	unsigned int chunk, r;
4898fad7767SAl Viro 
4908fad7767SAl Viro 	if (unlikely(bytes > i->count))
4918fad7767SAl Viro 		bytes = i->count;
4928fad7767SAl Viro 	if (unlikely(!bytes))
4938fad7767SAl Viro 		return 0;
49478e1f386SAl Viro 
49578e1f386SAl Viro 	if (!sanity(i))
49678e1f386SAl Viro 		return 0;
49778e1f386SAl Viro 
4986852df12SAl Viro 	while (bytes) {
4998fad7767SAl Viro 		struct page *page = append_pipe(i, bytes, &r);
5008fad7767SAl Viro 		char *p;
5018fad7767SAl Viro 
5028fad7767SAl Viro 		if (!page)
5038fad7767SAl Viro 			break;
5048fad7767SAl Viro 		chunk = min_t(size_t, bytes, PAGE_SIZE - r);
5058fad7767SAl Viro 		p = kmap_local_page(page);
5066852df12SAl Viro 		sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
5072495bdccSAl Viro 		kunmap_local(p);
50878e1f386SAl Viro 		off += chunk;
5098fad7767SAl Viro 		bytes -= chunk;
5106852df12SAl Viro 	}
5116852df12SAl Viro 	*sump = sum;
5126852df12SAl Viro 	return off;
51378e1f386SAl Viro }
51478e1f386SAl Viro 
515aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
516d879cb83SAl Viro {
517a41dad90SAl Viro 	if (WARN_ON_ONCE(i->data_source))
518a41dad90SAl Viro 		return 0;
51900e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
520241699cdSAl Viro 		return copy_pipe_to_iter(addr, bytes, i);
521fcb14cb1SAl Viro 	if (user_backed_iter(i))
52209fc68dcSAl Viro 		might_fault();
5237baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
5247baa5099SAl Viro 		copyout(base, addr + off, len),
5257baa5099SAl Viro 		memcpy(base, addr + off, len)
526d879cb83SAl Viro 	)
527d879cb83SAl Viro 
528d879cb83SAl Viro 	return bytes;
529d879cb83SAl Viro }
530aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter);
531d879cb83SAl Viro 
532ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC
533ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n)
5348780356eSDan Williams {
53596d4f267SLinus Torvalds 	if (access_ok(to, n)) {
536d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
537ec6347bbSDan Williams 		n = copy_mc_to_user((__force void *) to, from, n);
5388780356eSDan Williams 	}
5398780356eSDan Williams 	return n;
5408780356eSDan Williams }
5418780356eSDan Williams 
542ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
543ca146f6fSDan Williams 				struct iov_iter *i)
544ca146f6fSDan Williams {
5458fad7767SAl Viro 	size_t xfer = 0;
5468fad7767SAl Viro 	unsigned int off, chunk;
5478fad7767SAl Viro 
5488fad7767SAl Viro 	if (unlikely(bytes > i->count))
5498fad7767SAl Viro 		bytes = i->count;
5508fad7767SAl Viro 	if (unlikely(!bytes))
5518fad7767SAl Viro 		return 0;
552ca146f6fSDan Williams 
553ca146f6fSDan Williams 	if (!sanity(i))
554ca146f6fSDan Williams 		return 0;
555ca146f6fSDan Williams 
5568fad7767SAl Viro 	while (bytes) {
5578fad7767SAl Viro 		struct page *page = append_pipe(i, bytes, &off);
558ca146f6fSDan Williams 		unsigned long rem;
5598fad7767SAl Viro 		char *p;
5608fad7767SAl Viro 
5618fad7767SAl Viro 		if (!page)
5628fad7767SAl Viro 			break;
5638fad7767SAl Viro 		chunk = min_t(size_t, bytes, PAGE_SIZE - off);
5648fad7767SAl Viro 		p = kmap_local_page(page);
5652a510a74SAl Viro 		rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
5662a510a74SAl Viro 		chunk -= rem;
5672a510a74SAl Viro 		kunmap_local(p);
5682a510a74SAl Viro 		xfer += chunk;
5698fad7767SAl Viro 		bytes -= chunk;
570c3497fd0SAl Viro 		if (rem) {
5718fad7767SAl Viro 			iov_iter_revert(i, rem);
572ca146f6fSDan Williams 			break;
573c3497fd0SAl Viro 		}
5742a510a74SAl Viro 	}
575ca146f6fSDan Williams 	return xfer;
576ca146f6fSDan Williams }
577ca146f6fSDan Williams 
578bf3eeb9bSDan Williams /**
579ec6347bbSDan Williams  * _copy_mc_to_iter - copy to iter with source memory error exception handling
580bf3eeb9bSDan Williams  * @addr: source kernel address
581bf3eeb9bSDan Williams  * @bytes: total transfer length
58244e55997SRandy Dunlap  * @i: destination iterator
583bf3eeb9bSDan Williams  *
584ec6347bbSDan Williams  * The pmem driver deploys this for the dax operation
585ec6347bbSDan Williams  * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
586ec6347bbSDan Williams  * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
587ec6347bbSDan Williams  * successfully copied.
588bf3eeb9bSDan Williams  *
589ec6347bbSDan Williams  * The main differences between this and typical _copy_to_iter().
590bf3eeb9bSDan Williams  *
591bf3eeb9bSDan Williams  * * Typical tail/residue handling after a fault retries the copy
592bf3eeb9bSDan Williams  *   byte-by-byte until the fault happens again. Re-triggering machine
593bf3eeb9bSDan Williams  *   checks is potentially fatal so the implementation uses source
594bf3eeb9bSDan Williams  *   alignment and poison alignment assumptions to avoid re-triggering
595bf3eeb9bSDan Williams  *   hardware exceptions.
596bf3eeb9bSDan Williams  *
597bf3eeb9bSDan Williams  * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
598bf3eeb9bSDan Williams  *   Compare to copy_to_iter() where only ITER_IOVEC attempts might return
599bf3eeb9bSDan Williams  *   a short copy.
60044e55997SRandy Dunlap  *
60144e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
602bf3eeb9bSDan Williams  */
603ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
6048780356eSDan Williams {
605a41dad90SAl Viro 	if (WARN_ON_ONCE(i->data_source))
606a41dad90SAl Viro 		return 0;
60700e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
608ec6347bbSDan Williams 		return copy_mc_pipe_to_iter(addr, bytes, i);
609fcb14cb1SAl Viro 	if (user_backed_iter(i))
6108780356eSDan Williams 		might_fault();
6117baa5099SAl Viro 	__iterate_and_advance(i, bytes, base, len, off,
6127baa5099SAl Viro 		copyout_mc(base, addr + off, len),
6137baa5099SAl Viro 		copy_mc_to_kernel(base, addr + off, len)
6148780356eSDan Williams 	)
6158780356eSDan Williams 
6168780356eSDan Williams 	return bytes;
6178780356eSDan Williams }
618ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
619ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */
6208780356eSDan Williams 
621aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
622d879cb83SAl Viro {
623a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source))
624241699cdSAl Viro 		return 0;
625a41dad90SAl Viro 
626fcb14cb1SAl Viro 	if (user_backed_iter(i))
62709fc68dcSAl Viro 		might_fault();
6287baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6297baa5099SAl Viro 		copyin(addr + off, base, len),
6307baa5099SAl Viro 		memcpy(addr + off, base, len)
631d879cb83SAl Viro 	)
632d879cb83SAl Viro 
633d879cb83SAl Viro 	return bytes;
634d879cb83SAl Viro }
635aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter);
636d879cb83SAl Viro 
637aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
638d879cb83SAl Viro {
639a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source))
640241699cdSAl Viro 		return 0;
641a41dad90SAl Viro 
6427baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6437baa5099SAl Viro 		__copy_from_user_inatomic_nocache(addr + off, base, len),
6447baa5099SAl Viro 		memcpy(addr + off, base, len)
645d879cb83SAl Viro 	)
646d879cb83SAl Viro 
647d879cb83SAl Viro 	return bytes;
648d879cb83SAl Viro }
649aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache);
650d879cb83SAl Viro 
6510aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
652abd08d7dSDan Williams /**
653abd08d7dSDan Williams  * _copy_from_iter_flushcache - write destination through cpu cache
654abd08d7dSDan Williams  * @addr: destination kernel address
655abd08d7dSDan Williams  * @bytes: total transfer length
65644e55997SRandy Dunlap  * @i: source iterator
657abd08d7dSDan Williams  *
658abd08d7dSDan Williams  * The pmem driver arranges for filesystem-dax to use this facility via
659abd08d7dSDan Williams  * dax_copy_from_iter() for ensuring that writes to persistent memory
660abd08d7dSDan Williams  * are flushed through the CPU cache. It is differentiated from
661abd08d7dSDan Williams  * _copy_from_iter_nocache() in that guarantees all data is flushed for
662abd08d7dSDan Williams  * all iterator types. The _copy_from_iter_nocache() only attempts to
663abd08d7dSDan Williams  * bypass the cache for the ITER_IOVEC case, and on some archs may use
664abd08d7dSDan Williams  * instructions that strand dirty-data in the cache.
66544e55997SRandy Dunlap  *
66644e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
667abd08d7dSDan Williams  */
6686a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
6690aed55afSDan Williams {
670a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source))
6710aed55afSDan Williams 		return 0;
672a41dad90SAl Viro 
6737baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6747baa5099SAl Viro 		__copy_from_user_flushcache(addr + off, base, len),
6757baa5099SAl Viro 		memcpy_flushcache(addr + off, base, len)
6760aed55afSDan Williams 	)
6770aed55afSDan Williams 
6780aed55afSDan Williams 	return bytes;
6790aed55afSDan Williams }
6806a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
6810aed55afSDan Williams #endif
6820aed55afSDan Williams 
68372e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
68472e809edSAl Viro {
6856daef95bSEric Dumazet 	struct page *head;
6866daef95bSEric Dumazet 	size_t v = n + offset;
6876daef95bSEric Dumazet 
6886daef95bSEric Dumazet 	/*
6896daef95bSEric Dumazet 	 * The general case needs to access the page order in order
6906daef95bSEric Dumazet 	 * to compute the page size.
6916daef95bSEric Dumazet 	 * However, we mostly deal with order-0 pages and thus can
6926daef95bSEric Dumazet 	 * avoid a possible cache line miss for requests that fit all
6936daef95bSEric Dumazet 	 * page orders.
6946daef95bSEric Dumazet 	 */
6956daef95bSEric Dumazet 	if (n <= v && v <= PAGE_SIZE)
6966daef95bSEric Dumazet 		return true;
6976daef95bSEric Dumazet 
6986daef95bSEric Dumazet 	head = compound_head(page);
6996daef95bSEric Dumazet 	v += (page - head) << PAGE_SHIFT;
700a90bcb86SPetar Penkov 
70140a86061SAl Viro 	if (WARN_ON(n > v || v > page_size(head)))
70272e809edSAl Viro 		return false;
70340a86061SAl Viro 	return true;
70472e809edSAl Viro }
705cbbd26b8SAl Viro 
70608aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
70708aa6479SAl Viro 			 struct iov_iter *i)
70808aa6479SAl Viro {
70908aa6479SAl Viro 	size_t res = 0;
71040a86061SAl Viro 	if (!page_copy_sane(page, offset, bytes))
71108aa6479SAl Viro 		return 0;
712a41dad90SAl Viro 	if (WARN_ON_ONCE(i->data_source))
713a41dad90SAl Viro 		return 0;
714f0f6b614SAl Viro 	if (unlikely(iov_iter_is_pipe(i)))
715f0f6b614SAl Viro 		return copy_page_to_iter_pipe(page, offset, bytes, i);
71608aa6479SAl Viro 	page += offset / PAGE_SIZE; // first subpage
71708aa6479SAl Viro 	offset %= PAGE_SIZE;
71808aa6479SAl Viro 	while (1) {
719f0f6b614SAl Viro 		void *kaddr = kmap_local_page(page);
720f0f6b614SAl Viro 		size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
721f0f6b614SAl Viro 		n = _copy_to_iter(kaddr + offset, n, i);
722f0f6b614SAl Viro 		kunmap_local(kaddr);
72308aa6479SAl Viro 		res += n;
72408aa6479SAl Viro 		bytes -= n;
72508aa6479SAl Viro 		if (!bytes || !n)
72608aa6479SAl Viro 			break;
72708aa6479SAl Viro 		offset += n;
72808aa6479SAl Viro 		if (offset == PAGE_SIZE) {
72908aa6479SAl Viro 			page++;
73008aa6479SAl Viro 			offset = 0;
73108aa6479SAl Viro 		}
73208aa6479SAl Viro 	}
73308aa6479SAl Viro 	return res;
73408aa6479SAl Viro }
735d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter);
736d879cb83SAl Viro 
737d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
738d879cb83SAl Viro 			 struct iov_iter *i)
739d879cb83SAl Viro {
740c03f05f1SAl Viro 	size_t res = 0;
741c03f05f1SAl Viro 	if (!page_copy_sane(page, offset, bytes))
74228f38db7SAl Viro 		return 0;
743c03f05f1SAl Viro 	page += offset / PAGE_SIZE; // first subpage
744c03f05f1SAl Viro 	offset %= PAGE_SIZE;
745c03f05f1SAl Viro 	while (1) {
746c03f05f1SAl Viro 		void *kaddr = kmap_local_page(page);
747c03f05f1SAl Viro 		size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
748c03f05f1SAl Viro 		n = _copy_from_iter(kaddr + offset, n, i);
749c03f05f1SAl Viro 		kunmap_local(kaddr);
750c03f05f1SAl Viro 		res += n;
751c03f05f1SAl Viro 		bytes -= n;
752c03f05f1SAl Viro 		if (!bytes || !n)
753c03f05f1SAl Viro 			break;
754c03f05f1SAl Viro 		offset += n;
755c03f05f1SAl Viro 		if (offset == PAGE_SIZE) {
756c03f05f1SAl Viro 			page++;
757c03f05f1SAl Viro 			offset = 0;
758c03f05f1SAl Viro 		}
759c03f05f1SAl Viro 	}
760c03f05f1SAl Viro 	return res;
761d879cb83SAl Viro }
762d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter);
763d879cb83SAl Viro 
764241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i)
765241699cdSAl Viro {
7668fad7767SAl Viro 	unsigned int chunk, off;
7678fad7767SAl Viro 
7688fad7767SAl Viro 	if (unlikely(bytes > i->count))
7698fad7767SAl Viro 		bytes = i->count;
7708fad7767SAl Viro 	if (unlikely(!bytes))
7718fad7767SAl Viro 		return 0;
772241699cdSAl Viro 
773241699cdSAl Viro 	if (!sanity(i))
774241699cdSAl Viro 		return 0;
775241699cdSAl Viro 
7768fad7767SAl Viro 	for (size_t n = bytes; n; n -= chunk) {
7778fad7767SAl Viro 		struct page *page = append_pipe(i, n, &off);
7788fad7767SAl Viro 		char *p;
779241699cdSAl Viro 
7808fad7767SAl Viro 		if (!page)
7818fad7767SAl Viro 			return bytes - n;
7828fad7767SAl Viro 		chunk = min_t(size_t, n, PAGE_SIZE - off);
7838fad7767SAl Viro 		p = kmap_local_page(page);
784893839fdSAl Viro 		memset(p + off, 0, chunk);
785893839fdSAl Viro 		kunmap_local(p);
7868fad7767SAl Viro 	}
787241699cdSAl Viro 	return bytes;
788241699cdSAl Viro }
789241699cdSAl Viro 
790d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
791d879cb83SAl Viro {
79200e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
793241699cdSAl Viro 		return pipe_zero(bytes, i);
7947baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, count,
7957baa5099SAl Viro 		clear_user(base, len),
7967baa5099SAl Viro 		memset(base, 0, len)
797d879cb83SAl Viro 	)
798d879cb83SAl Viro 
799d879cb83SAl Viro 	return bytes;
800d879cb83SAl Viro }
801d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero);
802d879cb83SAl Viro 
803f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
804f0b65f39SAl Viro 				  struct iov_iter *i)
805d879cb83SAl Viro {
806d879cb83SAl Viro 	char *kaddr = kmap_atomic(page), *p = kaddr + offset;
80740a86061SAl Viro 	if (!page_copy_sane(page, offset, bytes)) {
80872e809edSAl Viro 		kunmap_atomic(kaddr);
80972e809edSAl Viro 		return 0;
81072e809edSAl Viro 	}
811a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source)) {
812241699cdSAl Viro 		kunmap_atomic(kaddr);
813241699cdSAl Viro 		return 0;
814241699cdSAl Viro 	}
8157baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
8167baa5099SAl Viro 		copyin(p + off, base, len),
8177baa5099SAl Viro 		memcpy(p + off, base, len)
818d879cb83SAl Viro 	)
819d879cb83SAl Viro 	kunmap_atomic(kaddr);
820d879cb83SAl Viro 	return bytes;
821d879cb83SAl Viro }
822f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic);
823d879cb83SAl Viro 
824b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size)
825b9dc6f65SAl Viro {
826b9dc6f65SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
82710f525a8SAl Viro 	int off = i->last_offset;
8288cefc107SDavid Howells 
8292c855de9SAl Viro 	if (!off && !size) {
8302c855de9SAl Viro 		pipe_discard_from(pipe, i->start_head); // discard everything
8312c855de9SAl Viro 		return;
832b9dc6f65SAl Viro 	}
833b9dc6f65SAl Viro 	i->count -= size;
8342c855de9SAl Viro 	while (1) {
8352c855de9SAl Viro 		struct pipe_buffer *buf = pipe_buf(pipe, i->head);
8362c855de9SAl Viro 		if (off) /* make it relative to the beginning of buffer */
83710f525a8SAl Viro 			size += abs(off) - buf->offset;
8382c855de9SAl Viro 		if (size <= buf->len) {
8392c855de9SAl Viro 			buf->len = size;
84010f525a8SAl Viro 			i->last_offset = last_offset(buf);
8412c855de9SAl Viro 			break;
8422c855de9SAl Viro 		}
8432c855de9SAl Viro 		size -= buf->len;
8442c855de9SAl Viro 		i->head++;
8452c855de9SAl Viro 		off = 0;
8462c855de9SAl Viro 	}
8472c855de9SAl Viro 	pipe_discard_from(pipe, i->head + 1); // discard everything past this one
848241699cdSAl Viro }
849241699cdSAl Viro 
85054c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
85154c8195bSPavel Begunkov {
85218fa9af7SAl Viro 	const struct bio_vec *bvec, *end;
85354c8195bSPavel Begunkov 
85418fa9af7SAl Viro 	if (!i->count)
85518fa9af7SAl Viro 		return;
85618fa9af7SAl Viro 	i->count -= size;
85754c8195bSPavel Begunkov 
85818fa9af7SAl Viro 	size += i->iov_offset;
85918fa9af7SAl Viro 
86018fa9af7SAl Viro 	for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
86118fa9af7SAl Viro 		if (likely(size < bvec->bv_len))
86218fa9af7SAl Viro 			break;
86318fa9af7SAl Viro 		size -= bvec->bv_len;
86418fa9af7SAl Viro 	}
86518fa9af7SAl Viro 	i->iov_offset = size;
86618fa9af7SAl Viro 	i->nr_segs -= bvec - i->bvec;
86718fa9af7SAl Viro 	i->bvec = bvec;
86854c8195bSPavel Begunkov }
86954c8195bSPavel Begunkov 
870185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
871185ac4d4SAl Viro {
872185ac4d4SAl Viro 	const struct iovec *iov, *end;
873185ac4d4SAl Viro 
874185ac4d4SAl Viro 	if (!i->count)
875185ac4d4SAl Viro 		return;
876185ac4d4SAl Viro 	i->count -= size;
877185ac4d4SAl Viro 
878185ac4d4SAl Viro 	size += i->iov_offset; // from beginning of current segment
879de4f5fedSJens Axboe 	for (iov = iter_iov(i), end = iov + i->nr_segs; iov < end; iov++) {
880185ac4d4SAl Viro 		if (likely(size < iov->iov_len))
881185ac4d4SAl Viro 			break;
882185ac4d4SAl Viro 		size -= iov->iov_len;
883185ac4d4SAl Viro 	}
884185ac4d4SAl Viro 	i->iov_offset = size;
885de4f5fedSJens Axboe 	i->nr_segs -= iov - iter_iov(i);
886de4f5fedSJens Axboe 	i->__iov = iov;
887185ac4d4SAl Viro }
888185ac4d4SAl Viro 
889d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size)
890d879cb83SAl Viro {
8913b3fc051SAl Viro 	if (unlikely(i->count < size))
8923b3fc051SAl Viro 		size = i->count;
893fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
894fcb14cb1SAl Viro 		i->iov_offset += size;
895fcb14cb1SAl Viro 		i->count -= size;
896fcb14cb1SAl Viro 	} else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
897185ac4d4SAl Viro 		/* iovec and kvec have identical layouts */
898185ac4d4SAl Viro 		iov_iter_iovec_advance(i, size);
899185ac4d4SAl Viro 	} else if (iov_iter_is_bvec(i)) {
900185ac4d4SAl Viro 		iov_iter_bvec_advance(i, size);
901185ac4d4SAl Viro 	} else if (iov_iter_is_pipe(i)) {
902241699cdSAl Viro 		pipe_advance(i, size);
903185ac4d4SAl Viro 	} else if (iov_iter_is_discard(i)) {
904185ac4d4SAl Viro 		i->count -= size;
9057ff50620SDavid Howells 	}
906d879cb83SAl Viro }
907d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance);
908d879cb83SAl Viro 
90927c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll)
91027c0e374SAl Viro {
91127c0e374SAl Viro 	if (!unroll)
91227c0e374SAl Viro 		return;
9135b47d59aSAl Viro 	if (WARN_ON(unroll > MAX_RW_COUNT))
9145b47d59aSAl Viro 		return;
91527c0e374SAl Viro 	i->count += unroll;
91600e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
91727c0e374SAl Viro 		struct pipe_inode_info *pipe = i->pipe;
91892acdc4fSAl Viro 		unsigned int head = pipe->head;
91992acdc4fSAl Viro 
92092acdc4fSAl Viro 		while (head > i->start_head) {
92192acdc4fSAl Viro 			struct pipe_buffer *b = pipe_buf(pipe, --head);
92292acdc4fSAl Viro 			if (unroll < b->len) {
92392acdc4fSAl Viro 				b->len -= unroll;
92410f525a8SAl Viro 				i->last_offset = last_offset(b);
92592acdc4fSAl Viro 				i->head = head;
92692acdc4fSAl Viro 				return;
92727c0e374SAl Viro 			}
92892acdc4fSAl Viro 			unroll -= b->len;
92992acdc4fSAl Viro 			pipe_buf_release(pipe, b);
93092acdc4fSAl Viro 			pipe->head--;
93127c0e374SAl Viro 		}
93210f525a8SAl Viro 		i->last_offset = 0;
93392acdc4fSAl Viro 		i->head = head;
93427c0e374SAl Viro 		return;
93527c0e374SAl Viro 	}
9369ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_discard(i)))
9379ea9ce04SDavid Howells 		return;
93827c0e374SAl Viro 	if (unroll <= i->iov_offset) {
93927c0e374SAl Viro 		i->iov_offset -= unroll;
94027c0e374SAl Viro 		return;
94127c0e374SAl Viro 	}
94227c0e374SAl Viro 	unroll -= i->iov_offset;
943fcb14cb1SAl Viro 	if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
9447ff50620SDavid Howells 		BUG(); /* We should never go beyond the start of the specified
9457ff50620SDavid Howells 			* range since we might then be straying into pages that
9467ff50620SDavid Howells 			* aren't pinned.
9477ff50620SDavid Howells 			*/
9487ff50620SDavid Howells 	} else if (iov_iter_is_bvec(i)) {
94927c0e374SAl Viro 		const struct bio_vec *bvec = i->bvec;
95027c0e374SAl Viro 		while (1) {
95127c0e374SAl Viro 			size_t n = (--bvec)->bv_len;
95227c0e374SAl Viro 			i->nr_segs++;
95327c0e374SAl Viro 			if (unroll <= n) {
95427c0e374SAl Viro 				i->bvec = bvec;
95527c0e374SAl Viro 				i->iov_offset = n - unroll;
95627c0e374SAl Viro 				return;
95727c0e374SAl Viro 			}
95827c0e374SAl Viro 			unroll -= n;
95927c0e374SAl Viro 		}
96027c0e374SAl Viro 	} else { /* same logics for iovec and kvec */
961de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i);
96227c0e374SAl Viro 		while (1) {
96327c0e374SAl Viro 			size_t n = (--iov)->iov_len;
96427c0e374SAl Viro 			i->nr_segs++;
96527c0e374SAl Viro 			if (unroll <= n) {
966de4f5fedSJens Axboe 				i->__iov = iov;
96727c0e374SAl Viro 				i->iov_offset = n - unroll;
96827c0e374SAl Viro 				return;
96927c0e374SAl Viro 			}
97027c0e374SAl Viro 			unroll -= n;
97127c0e374SAl Viro 		}
97227c0e374SAl Viro 	}
97327c0e374SAl Viro }
97427c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert);
97527c0e374SAl Viro 
976d879cb83SAl Viro /*
977d879cb83SAl Viro  * Return the count of just the current iov_iter segment.
978d879cb83SAl Viro  */
979d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i)
980d879cb83SAl Viro {
98128f38db7SAl Viro 	if (i->nr_segs > 1) {
98228f38db7SAl Viro 		if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
983de4f5fedSJens Axboe 			return min(i->count, iter_iov(i)->iov_len - i->iov_offset);
9847ff50620SDavid Howells 		if (iov_iter_is_bvec(i))
985d879cb83SAl Viro 			return min(i->count, i->bvec->bv_len - i->iov_offset);
98628f38db7SAl Viro 	}
98728f38db7SAl Viro 	return i->count;
988d879cb83SAl Viro }
989d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count);
990d879cb83SAl Viro 
991aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
992d879cb83SAl Viro 			const struct kvec *kvec, unsigned long nr_segs,
993d879cb83SAl Viro 			size_t count)
994d879cb83SAl Viro {
995aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
9968cd54c1cSAl Viro 	*i = (struct iov_iter){
9978cd54c1cSAl Viro 		.iter_type = ITER_KVEC,
9988cd54c1cSAl Viro 		.data_source = direction,
9998cd54c1cSAl Viro 		.kvec = kvec,
10008cd54c1cSAl Viro 		.nr_segs = nr_segs,
10018cd54c1cSAl Viro 		.iov_offset = 0,
10028cd54c1cSAl Viro 		.count = count
10038cd54c1cSAl Viro 	};
1004d879cb83SAl Viro }
1005d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec);
1006d879cb83SAl Viro 
1007aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1008d879cb83SAl Viro 			const struct bio_vec *bvec, unsigned long nr_segs,
1009d879cb83SAl Viro 			size_t count)
1010d879cb83SAl Viro {
1011aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
10128cd54c1cSAl Viro 	*i = (struct iov_iter){
10138cd54c1cSAl Viro 		.iter_type = ITER_BVEC,
10148cd54c1cSAl Viro 		.data_source = direction,
10158cd54c1cSAl Viro 		.bvec = bvec,
10168cd54c1cSAl Viro 		.nr_segs = nr_segs,
10178cd54c1cSAl Viro 		.iov_offset = 0,
10188cd54c1cSAl Viro 		.count = count
10198cd54c1cSAl Viro 	};
1020d879cb83SAl Viro }
1021d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec);
1022d879cb83SAl Viro 
1023aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1024241699cdSAl Viro 			struct pipe_inode_info *pipe,
1025241699cdSAl Viro 			size_t count)
1026241699cdSAl Viro {
1027aa563d7bSDavid Howells 	BUG_ON(direction != READ);
10288cefc107SDavid Howells 	WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
10298cd54c1cSAl Viro 	*i = (struct iov_iter){
10308cd54c1cSAl Viro 		.iter_type = ITER_PIPE,
10318cd54c1cSAl Viro 		.data_source = false,
10328cd54c1cSAl Viro 		.pipe = pipe,
10338cd54c1cSAl Viro 		.head = pipe->head,
10348cd54c1cSAl Viro 		.start_head = pipe->head,
103510f525a8SAl Viro 		.last_offset = 0,
10368cd54c1cSAl Viro 		.count = count
10378cd54c1cSAl Viro 	};
1038241699cdSAl Viro }
1039241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe);
1040241699cdSAl Viro 
10419ea9ce04SDavid Howells /**
10427ff50620SDavid Howells  * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
10437ff50620SDavid Howells  * @i: The iterator to initialise.
10447ff50620SDavid Howells  * @direction: The direction of the transfer.
10457ff50620SDavid Howells  * @xarray: The xarray to access.
10467ff50620SDavid Howells  * @start: The start file position.
10477ff50620SDavid Howells  * @count: The size of the I/O buffer in bytes.
10487ff50620SDavid Howells  *
10497ff50620SDavid Howells  * Set up an I/O iterator to either draw data out of the pages attached to an
10507ff50620SDavid Howells  * inode or to inject data into those pages.  The pages *must* be prevented
10517ff50620SDavid Howells  * from evaporation, either by taking a ref on them or locking them by the
10527ff50620SDavid Howells  * caller.
10537ff50620SDavid Howells  */
10547ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
10557ff50620SDavid Howells 		     struct xarray *xarray, loff_t start, size_t count)
10567ff50620SDavid Howells {
10577ff50620SDavid Howells 	BUG_ON(direction & ~1);
10588cd54c1cSAl Viro 	*i = (struct iov_iter) {
10598cd54c1cSAl Viro 		.iter_type = ITER_XARRAY,
10608cd54c1cSAl Viro 		.data_source = direction,
10618cd54c1cSAl Viro 		.xarray = xarray,
10628cd54c1cSAl Viro 		.xarray_start = start,
10638cd54c1cSAl Viro 		.count = count,
10648cd54c1cSAl Viro 		.iov_offset = 0
10658cd54c1cSAl Viro 	};
10667ff50620SDavid Howells }
10677ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray);
10687ff50620SDavid Howells 
10697ff50620SDavid Howells /**
10709ea9ce04SDavid Howells  * iov_iter_discard - Initialise an I/O iterator that discards data
10719ea9ce04SDavid Howells  * @i: The iterator to initialise.
10729ea9ce04SDavid Howells  * @direction: The direction of the transfer.
10739ea9ce04SDavid Howells  * @count: The size of the I/O buffer in bytes.
10749ea9ce04SDavid Howells  *
10759ea9ce04SDavid Howells  * Set up an I/O iterator that just discards everything that's written to it.
10769ea9ce04SDavid Howells  * It's only available as a READ iterator.
10779ea9ce04SDavid Howells  */
10789ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
10799ea9ce04SDavid Howells {
10809ea9ce04SDavid Howells 	BUG_ON(direction != READ);
10818cd54c1cSAl Viro 	*i = (struct iov_iter){
10828cd54c1cSAl Viro 		.iter_type = ITER_DISCARD,
10838cd54c1cSAl Viro 		.data_source = false,
10848cd54c1cSAl Viro 		.count = count,
10858cd54c1cSAl Viro 		.iov_offset = 0
10868cd54c1cSAl Viro 	};
10879ea9ce04SDavid Howells }
10889ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard);
10899ea9ce04SDavid Howells 
1090cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
1091cfa320f7SKeith Busch 				   unsigned len_mask)
1092cfa320f7SKeith Busch {
1093cfa320f7SKeith Busch 	size_t size = i->count;
1094cfa320f7SKeith Busch 	size_t skip = i->iov_offset;
1095cfa320f7SKeith Busch 	unsigned k;
1096cfa320f7SKeith Busch 
1097cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
1098de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
1099de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
1100cfa320f7SKeith Busch 
1101cfa320f7SKeith Busch 		if (len > size)
1102cfa320f7SKeith Busch 			len = size;
1103cfa320f7SKeith Busch 		if (len & len_mask)
1104cfa320f7SKeith Busch 			return false;
1105de4f5fedSJens Axboe 		if ((unsigned long)(iov->iov_base + skip) & addr_mask)
1106cfa320f7SKeith Busch 			return false;
1107cfa320f7SKeith Busch 
1108cfa320f7SKeith Busch 		size -= len;
1109cfa320f7SKeith Busch 		if (!size)
1110cfa320f7SKeith Busch 			break;
1111cfa320f7SKeith Busch 	}
1112cfa320f7SKeith Busch 	return true;
1113cfa320f7SKeith Busch }
1114cfa320f7SKeith Busch 
1115cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
1116cfa320f7SKeith Busch 				  unsigned len_mask)
1117cfa320f7SKeith Busch {
1118cfa320f7SKeith Busch 	size_t size = i->count;
1119cfa320f7SKeith Busch 	unsigned skip = i->iov_offset;
1120cfa320f7SKeith Busch 	unsigned k;
1121cfa320f7SKeith Busch 
1122cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
1123cfa320f7SKeith Busch 		size_t len = i->bvec[k].bv_len - skip;
1124cfa320f7SKeith Busch 
1125cfa320f7SKeith Busch 		if (len > size)
1126cfa320f7SKeith Busch 			len = size;
1127cfa320f7SKeith Busch 		if (len & len_mask)
1128cfa320f7SKeith Busch 			return false;
1129cfa320f7SKeith Busch 		if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
1130cfa320f7SKeith Busch 			return false;
1131cfa320f7SKeith Busch 
1132cfa320f7SKeith Busch 		size -= len;
1133cfa320f7SKeith Busch 		if (!size)
1134cfa320f7SKeith Busch 			break;
1135cfa320f7SKeith Busch 	}
1136cfa320f7SKeith Busch 	return true;
1137cfa320f7SKeith Busch }
1138cfa320f7SKeith Busch 
1139cfa320f7SKeith Busch /**
1140cfa320f7SKeith Busch  * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
1141cfa320f7SKeith Busch  * 	are aligned to the parameters.
1142cfa320f7SKeith Busch  *
1143cfa320f7SKeith Busch  * @i: &struct iov_iter to restore
1144cfa320f7SKeith Busch  * @addr_mask: bit mask to check against the iov element's addresses
1145cfa320f7SKeith Busch  * @len_mask: bit mask to check against the iov element's lengths
1146cfa320f7SKeith Busch  *
1147cfa320f7SKeith Busch  * Return: false if any addresses or lengths intersect with the provided masks
1148cfa320f7SKeith Busch  */
1149cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
1150cfa320f7SKeith Busch 			 unsigned len_mask)
1151cfa320f7SKeith Busch {
1152fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1153fcb14cb1SAl Viro 		if (i->count & len_mask)
1154fcb14cb1SAl Viro 			return false;
1155fcb14cb1SAl Viro 		if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
1156fcb14cb1SAl Viro 			return false;
1157fcb14cb1SAl Viro 		return true;
1158fcb14cb1SAl Viro 	}
1159fcb14cb1SAl Viro 
1160cfa320f7SKeith Busch 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1161cfa320f7SKeith Busch 		return iov_iter_aligned_iovec(i, addr_mask, len_mask);
1162cfa320f7SKeith Busch 
1163cfa320f7SKeith Busch 	if (iov_iter_is_bvec(i))
1164cfa320f7SKeith Busch 		return iov_iter_aligned_bvec(i, addr_mask, len_mask);
1165cfa320f7SKeith Busch 
1166cfa320f7SKeith Busch 	if (iov_iter_is_pipe(i)) {
1167cfa320f7SKeith Busch 		size_t size = i->count;
1168cfa320f7SKeith Busch 
1169cfa320f7SKeith Busch 		if (size & len_mask)
1170cfa320f7SKeith Busch 			return false;
117110f525a8SAl Viro 		if (size && i->last_offset > 0) {
117210f525a8SAl Viro 			if (i->last_offset & addr_mask)
1173cfa320f7SKeith Busch 				return false;
1174cfa320f7SKeith Busch 		}
1175cfa320f7SKeith Busch 
1176cfa320f7SKeith Busch 		return true;
1177cfa320f7SKeith Busch 	}
1178cfa320f7SKeith Busch 
1179cfa320f7SKeith Busch 	if (iov_iter_is_xarray(i)) {
1180cfa320f7SKeith Busch 		if (i->count & len_mask)
1181cfa320f7SKeith Busch 			return false;
1182cfa320f7SKeith Busch 		if ((i->xarray_start + i->iov_offset) & addr_mask)
1183cfa320f7SKeith Busch 			return false;
1184cfa320f7SKeith Busch 	}
1185cfa320f7SKeith Busch 
1186cfa320f7SKeith Busch 	return true;
1187cfa320f7SKeith Busch }
1188cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
1189cfa320f7SKeith Busch 
11909221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1191d879cb83SAl Viro {
1192d879cb83SAl Viro 	unsigned long res = 0;
1193d879cb83SAl Viro 	size_t size = i->count;
11949221d2e3SAl Viro 	size_t skip = i->iov_offset;
11959221d2e3SAl Viro 	unsigned k;
1196d879cb83SAl Viro 
11979221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
1198de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
1199de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
12009221d2e3SAl Viro 		if (len) {
1201de4f5fedSJens Axboe 			res |= (unsigned long)iov->iov_base + skip;
12029221d2e3SAl Viro 			if (len > size)
12039221d2e3SAl Viro 				len = size;
12049221d2e3SAl Viro 			res |= len;
12059221d2e3SAl Viro 			size -= len;
12069221d2e3SAl Viro 			if (!size)
12079221d2e3SAl Viro 				break;
12089221d2e3SAl Viro 		}
12099221d2e3SAl Viro 	}
12109221d2e3SAl Viro 	return res;
12119221d2e3SAl Viro }
12129221d2e3SAl Viro 
12139221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
12149221d2e3SAl Viro {
12159221d2e3SAl Viro 	unsigned res = 0;
12169221d2e3SAl Viro 	size_t size = i->count;
12179221d2e3SAl Viro 	unsigned skip = i->iov_offset;
12189221d2e3SAl Viro 	unsigned k;
12199221d2e3SAl Viro 
12209221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
12219221d2e3SAl Viro 		size_t len = i->bvec[k].bv_len - skip;
12229221d2e3SAl Viro 		res |= (unsigned long)i->bvec[k].bv_offset + skip;
12239221d2e3SAl Viro 		if (len > size)
12249221d2e3SAl Viro 			len = size;
12259221d2e3SAl Viro 		res |= len;
12269221d2e3SAl Viro 		size -= len;
12279221d2e3SAl Viro 		if (!size)
12289221d2e3SAl Viro 			break;
12299221d2e3SAl Viro 	}
12309221d2e3SAl Viro 	return res;
12319221d2e3SAl Viro }
12329221d2e3SAl Viro 
12339221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i)
12349221d2e3SAl Viro {
1235fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1236fcb14cb1SAl Viro 		size_t size = i->count;
1237fcb14cb1SAl Viro 		if (size)
1238fcb14cb1SAl Viro 			return ((unsigned long)i->ubuf + i->iov_offset) | size;
1239fcb14cb1SAl Viro 		return 0;
1240fcb14cb1SAl Viro 	}
1241fcb14cb1SAl Viro 
12429221d2e3SAl Viro 	/* iovec and kvec have identical layouts */
12439221d2e3SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
12449221d2e3SAl Viro 		return iov_iter_alignment_iovec(i);
12459221d2e3SAl Viro 
12469221d2e3SAl Viro 	if (iov_iter_is_bvec(i))
12479221d2e3SAl Viro 		return iov_iter_alignment_bvec(i);
12489221d2e3SAl Viro 
12499221d2e3SAl Viro 	if (iov_iter_is_pipe(i)) {
12509221d2e3SAl Viro 		size_t size = i->count;
1251e0ff126eSJan Kara 
125210f525a8SAl Viro 		if (size && i->last_offset > 0)
125310f525a8SAl Viro 			return size | i->last_offset;
1254241699cdSAl Viro 		return size;
1255241699cdSAl Viro 	}
12569221d2e3SAl Viro 
12579221d2e3SAl Viro 	if (iov_iter_is_xarray(i))
12583d14ec1fSDavid Howells 		return (i->xarray_start + i->iov_offset) | i->count;
12599221d2e3SAl Viro 
12609221d2e3SAl Viro 	return 0;
1261d879cb83SAl Viro }
1262d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment);
1263d879cb83SAl Viro 
1264357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1265357f435dSAl Viro {
1266357f435dSAl Viro 	unsigned long res = 0;
1267610c7a71SAl Viro 	unsigned long v = 0;
1268357f435dSAl Viro 	size_t size = i->count;
1269610c7a71SAl Viro 	unsigned k;
1270357f435dSAl Viro 
1271fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1272fcb14cb1SAl Viro 		return 0;
1273fcb14cb1SAl Viro 
1274610c7a71SAl Viro 	if (WARN_ON(!iter_is_iovec(i)))
1275241699cdSAl Viro 		return ~0U;
1276241699cdSAl Viro 
1277610c7a71SAl Viro 	for (k = 0; k < i->nr_segs; k++) {
1278de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
1279de4f5fedSJens Axboe 		if (iov->iov_len) {
1280de4f5fedSJens Axboe 			unsigned long base = (unsigned long)iov->iov_base;
1281610c7a71SAl Viro 			if (v) // if not the first one
1282610c7a71SAl Viro 				res |= base | v; // this start | previous end
1283de4f5fedSJens Axboe 			v = base + iov->iov_len;
1284de4f5fedSJens Axboe 			if (size <= iov->iov_len)
1285610c7a71SAl Viro 				break;
1286de4f5fedSJens Axboe 			size -= iov->iov_len;
1287610c7a71SAl Viro 		}
1288610c7a71SAl Viro 	}
1289357f435dSAl Viro 	return res;
1290357f435dSAl Viro }
1291357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment);
1292357f435dSAl Viro 
12933cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size,
12943cf42da3SAl Viro 			    size_t start, unsigned int maxpages)
1295acbdeb83SAl Viro {
12963cf42da3SAl Viro 	unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE);
12973cf42da3SAl Viro 
12983cf42da3SAl Viro 	if (count > maxpages)
12993cf42da3SAl Viro 		count = maxpages;
13003cf42da3SAl Viro 	WARN_ON(!count);	// caller should've prevented that
13013cf42da3SAl Viro 	if (!*res) {
13023cf42da3SAl Viro 		*res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
13033cf42da3SAl Viro 		if (!*res)
13043cf42da3SAl Viro 			return 0;
13053cf42da3SAl Viro 	}
13063cf42da3SAl Viro 	return count;
1307acbdeb83SAl Viro }
1308acbdeb83SAl Viro 
130985200084SAl Viro static ssize_t pipe_get_pages(struct iov_iter *i,
131085200084SAl Viro 		   struct page ***pages, size_t maxsize, unsigned maxpages,
131185200084SAl Viro 		   size_t *start)
1312241699cdSAl Viro {
1313746de1f8SAl Viro 	unsigned int npages, count, off, chunk;
131485200084SAl Viro 	struct page **p;
1315746de1f8SAl Viro 	size_t left;
1316241699cdSAl Viro 
131785200084SAl Viro 	if (!sanity(i))
131885200084SAl Viro 		return -EFAULT;
131985200084SAl Viro 
132085200084SAl Viro 	*start = off = pipe_npages(i, &npages);
13213cf42da3SAl Viro 	if (!npages)
13223cf42da3SAl Viro 		return -EFAULT;
13233cf42da3SAl Viro 	count = want_pages_array(pages, maxsize, off, min(npages, maxpages));
13243cf42da3SAl Viro 	if (!count)
132585200084SAl Viro 		return -ENOMEM;
13263cf42da3SAl Viro 	p = *pages;
1327746de1f8SAl Viro 	for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) {
1328746de1f8SAl Viro 		struct page *page = append_pipe(i, left, &off);
1329e3b42964SAl Viro 		if (!page)
1330e3b42964SAl Viro 			break;
1331746de1f8SAl Viro 		chunk = min_t(size_t, left, PAGE_SIZE - off);
133285200084SAl Viro 		get_page(*p++ = page);
1333e3b42964SAl Viro 	}
133485200084SAl Viro 	if (!npages)
1335241699cdSAl Viro 		return -EFAULT;
1336746de1f8SAl Viro 	return maxsize - left;
1337241699cdSAl Viro }
1338241699cdSAl Viro 
13397ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
13407ff50620SDavid Howells 					  pgoff_t index, unsigned int nr_pages)
13417ff50620SDavid Howells {
13427ff50620SDavid Howells 	XA_STATE(xas, xa, index);
13437ff50620SDavid Howells 	struct page *page;
13447ff50620SDavid Howells 	unsigned int ret = 0;
13457ff50620SDavid Howells 
13467ff50620SDavid Howells 	rcu_read_lock();
13477ff50620SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
13487ff50620SDavid Howells 		if (xas_retry(&xas, page))
13497ff50620SDavid Howells 			continue;
13507ff50620SDavid Howells 
13517ff50620SDavid Howells 		/* Has the page moved or been split? */
13527ff50620SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
13537ff50620SDavid Howells 			xas_reset(&xas);
13547ff50620SDavid Howells 			continue;
13557ff50620SDavid Howells 		}
13567ff50620SDavid Howells 
13577ff50620SDavid Howells 		pages[ret] = find_subpage(page, xas.xa_index);
13587ff50620SDavid Howells 		get_page(pages[ret]);
13597ff50620SDavid Howells 		if (++ret == nr_pages)
13607ff50620SDavid Howells 			break;
13617ff50620SDavid Howells 	}
13627ff50620SDavid Howells 	rcu_read_unlock();
13637ff50620SDavid Howells 	return ret;
13647ff50620SDavid Howells }
13657ff50620SDavid Howells 
13667ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i,
136768fe506fSAl Viro 				     struct page ***pages, size_t maxsize,
13687ff50620SDavid Howells 				     unsigned maxpages, size_t *_start_offset)
13697ff50620SDavid Howells {
13703cf42da3SAl Viro 	unsigned nr, offset, count;
13713cf42da3SAl Viro 	pgoff_t index;
13727ff50620SDavid Howells 	loff_t pos;
13737ff50620SDavid Howells 
13747ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
13757ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
13767ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
13777ff50620SDavid Howells 	*_start_offset = offset;
13787ff50620SDavid Howells 
13793cf42da3SAl Viro 	count = want_pages_array(pages, maxsize, offset, maxpages);
13803cf42da3SAl Viro 	if (!count)
138168fe506fSAl Viro 		return -ENOMEM;
138268fe506fSAl Viro 	nr = iter_xarray_populate_pages(*pages, i->xarray, index, count);
13837ff50620SDavid Howells 	if (nr == 0)
13847ff50620SDavid Howells 		return 0;
13857ff50620SDavid Howells 
1386eba2d3d7SAl Viro 	maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1387310d9d5aSAl Viro 	i->iov_offset += maxsize;
1388310d9d5aSAl Viro 	i->count -= maxsize;
1389eba2d3d7SAl Viro 	return maxsize;
13907ff50620SDavid Howells }
13917ff50620SDavid Howells 
1392fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
1393dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
13943d671ca6SAl Viro {
13953d671ca6SAl Viro 	size_t skip;
13963d671ca6SAl Viro 	long k;
13973d671ca6SAl Viro 
1398fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1399fcb14cb1SAl Viro 		return (unsigned long)i->ubuf + i->iov_offset;
1400fcb14cb1SAl Viro 
14013d671ca6SAl Viro 	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1402de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
1403de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
14043d671ca6SAl Viro 
14053d671ca6SAl Viro 		if (unlikely(!len))
14063d671ca6SAl Viro 			continue;
140759dbd7d0SAl Viro 		if (*size > len)
14083d671ca6SAl Viro 			*size = len;
1409de4f5fedSJens Axboe 		return (unsigned long)iov->iov_base + skip;
14103d671ca6SAl Viro 	}
14113d671ca6SAl Viro 	BUG(); // if it had been empty, we wouldn't get called
14123d671ca6SAl Viro }
14133d671ca6SAl Viro 
14143d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */
14153d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i,
141659dbd7d0SAl Viro 				       size_t *size, size_t *start)
14173d671ca6SAl Viro {
14183d671ca6SAl Viro 	struct page *page;
14193d671ca6SAl Viro 	size_t skip = i->iov_offset, len;
14203d671ca6SAl Viro 
14213d671ca6SAl Viro 	len = i->bvec->bv_len - skip;
142259dbd7d0SAl Viro 	if (*size > len)
142359dbd7d0SAl Viro 		*size = len;
14243d671ca6SAl Viro 	skip += i->bvec->bv_offset;
14253d671ca6SAl Viro 	page = i->bvec->bv_page + skip / PAGE_SIZE;
1426dda8e5d1SAl Viro 	*start = skip % PAGE_SIZE;
14273d671ca6SAl Viro 	return page;
14283d671ca6SAl Viro }
14293d671ca6SAl Viro 
143091329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
1431d879cb83SAl Viro 		   struct page ***pages, size_t maxsize,
1432d8207640SLogan Gunthorpe 		   unsigned int maxpages, size_t *start,
1433f62e52d1SDavid Howells 		   iov_iter_extraction_t extraction_flags)
1434d879cb83SAl Viro {
1435f62e52d1SDavid Howells 	unsigned int n, gup_flags = 0;
1436d879cb83SAl Viro 
1437d879cb83SAl Viro 	if (maxsize > i->count)
1438d879cb83SAl Viro 		maxsize = i->count;
14393d671ca6SAl Viro 	if (!maxsize)
14403d671ca6SAl Viro 		return 0;
14417392ed17SAl Viro 	if (maxsize > MAX_RW_COUNT)
14427392ed17SAl Viro 		maxsize = MAX_RW_COUNT;
1443f62e52d1SDavid Howells 	if (extraction_flags & ITER_ALLOW_P2PDMA)
1444f62e52d1SDavid Howells 		gup_flags |= FOLL_PCI_P2PDMA;
1445d879cb83SAl Viro 
1446fcb14cb1SAl Viro 	if (likely(user_backed_iter(i))) {
14473d671ca6SAl Viro 		unsigned long addr;
14483cf42da3SAl Viro 		int res;
14499ea9ce04SDavid Howells 
14503337ab08SAndreas Gruenbacher 		if (iov_iter_rw(i) != WRITE)
14513337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_WRITE;
14523337ab08SAndreas Gruenbacher 		if (i->nofault)
14533337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_NOFAULT;
14543337ab08SAndreas Gruenbacher 
1455dd45ab9dSAl Viro 		addr = first_iovec_segment(i, &maxsize);
1456dd45ab9dSAl Viro 		*start = addr % PAGE_SIZE;
1457dd45ab9dSAl Viro 		addr &= PAGE_MASK;
14583cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
14593cf42da3SAl Viro 		if (!n)
1460d879cb83SAl Viro 			return -ENOMEM;
1461451c0ba9SAl Viro 		res = get_user_pages_fast(addr, n, gup_flags, *pages);
146291329559SAl Viro 		if (unlikely(res <= 0))
1463d879cb83SAl Viro 			return res;
1464eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1465eba2d3d7SAl Viro 		iov_iter_advance(i, maxsize);
1466eba2d3d7SAl Viro 		return maxsize;
14673d671ca6SAl Viro 	}
14683d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
1469451c0ba9SAl Viro 		struct page **p;
14703d671ca6SAl Viro 		struct page *page;
14713d671ca6SAl Viro 
147259dbd7d0SAl Viro 		page = first_bvec_segment(i, &maxsize, start);
14733cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
14743cf42da3SAl Viro 		if (!n)
1475d879cb83SAl Viro 			return -ENOMEM;
14763cf42da3SAl Viro 		p = *pages;
1477dda8e5d1SAl Viro 		for (int k = 0; k < n; k++)
1478eba2d3d7SAl Viro 			get_page(p[k] = page + k);
1479eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1480310d9d5aSAl Viro 		i->count -= maxsize;
1481310d9d5aSAl Viro 		i->iov_offset += maxsize;
1482310d9d5aSAl Viro 		if (i->iov_offset == i->bvec->bv_len) {
1483310d9d5aSAl Viro 			i->iov_offset = 0;
1484310d9d5aSAl Viro 			i->bvec++;
1485310d9d5aSAl Viro 			i->nr_segs--;
1486310d9d5aSAl Viro 		}
1487eba2d3d7SAl Viro 		return maxsize;
14883d671ca6SAl Viro 	}
14893d671ca6SAl Viro 	if (iov_iter_is_pipe(i))
1490451c0ba9SAl Viro 		return pipe_get_pages(i, pages, maxsize, maxpages, start);
14913d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
1492451c0ba9SAl Viro 		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1493d879cb83SAl Viro 	return -EFAULT;
1494d879cb83SAl Viro }
149591329559SAl Viro 
1496d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages(struct iov_iter *i,
1497451c0ba9SAl Viro 		   struct page **pages, size_t maxsize, unsigned maxpages,
1498f62e52d1SDavid Howells 		   size_t *start, iov_iter_extraction_t extraction_flags)
1499451c0ba9SAl Viro {
1500451c0ba9SAl Viro 	if (!maxpages)
1501451c0ba9SAl Viro 		return 0;
1502451c0ba9SAl Viro 	BUG_ON(!pages);
1503451c0ba9SAl Viro 
1504d8207640SLogan Gunthorpe 	return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages,
1505f62e52d1SDavid Howells 					  start, extraction_flags);
1506d8207640SLogan Gunthorpe }
1507d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages);
1508d8207640SLogan Gunthorpe 
1509d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
1510d8207640SLogan Gunthorpe 		size_t maxsize, unsigned maxpages, size_t *start)
1511d8207640SLogan Gunthorpe {
1512d8207640SLogan Gunthorpe 	return iov_iter_get_pages(i, pages, maxsize, maxpages, start, 0);
1513451c0ba9SAl Viro }
1514eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2);
1515451c0ba9SAl Viro 
1516d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
151791329559SAl Viro 		   struct page ***pages, size_t maxsize,
1518f62e52d1SDavid Howells 		   size_t *start, iov_iter_extraction_t extraction_flags)
151991329559SAl Viro {
152091329559SAl Viro 	ssize_t len;
152191329559SAl Viro 
152291329559SAl Viro 	*pages = NULL;
152391329559SAl Viro 
1524d8207640SLogan Gunthorpe 	len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start,
1525f62e52d1SDavid Howells 					 extraction_flags);
152691329559SAl Viro 	if (len <= 0) {
152791329559SAl Viro 		kvfree(*pages);
152891329559SAl Viro 		*pages = NULL;
152991329559SAl Viro 	}
153091329559SAl Viro 	return len;
153191329559SAl Viro }
1532d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages_alloc);
1533d8207640SLogan Gunthorpe 
1534d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i,
1535d8207640SLogan Gunthorpe 		struct page ***pages, size_t maxsize, size_t *start)
1536d8207640SLogan Gunthorpe {
1537d8207640SLogan Gunthorpe 	return iov_iter_get_pages_alloc(i, pages, maxsize, start, 0);
1538d8207640SLogan Gunthorpe }
1539eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2);
1540d879cb83SAl Viro 
1541d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1542d879cb83SAl Viro 			       struct iov_iter *i)
1543d879cb83SAl Viro {
1544d879cb83SAl Viro 	__wsum sum, next;
1545d879cb83SAl Viro 	sum = *csum;
1546a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source))
1547241699cdSAl Viro 		return 0;
1548a41dad90SAl Viro 
15497baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off, ({
15507baa5099SAl Viro 		next = csum_and_copy_from_user(base, addr + off, len);
1551d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
15527baa5099SAl Viro 		next ? 0 : len;
1553d879cb83SAl Viro 	}), ({
15547baa5099SAl Viro 		sum = csum_and_memcpy(addr + off, base, len, sum, off);
1555d879cb83SAl Viro 	})
1556d879cb83SAl Viro 	)
1557d879cb83SAl Viro 	*csum = sum;
1558d879cb83SAl Viro 	return bytes;
1559d879cb83SAl Viro }
1560d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter);
1561d879cb83SAl Viro 
156252cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1563d879cb83SAl Viro 			     struct iov_iter *i)
1564d879cb83SAl Viro {
156552cbd23aSWillem de Bruijn 	struct csum_state *csstate = _csstate;
1566d879cb83SAl Viro 	__wsum sum, next;
156778e1f386SAl Viro 
1568a41dad90SAl Viro 	if (WARN_ON_ONCE(i->data_source))
1569a41dad90SAl Viro 		return 0;
157078e1f386SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
1571c67f1fd2SAl Viro 		// can't use csum_memcpy() for that one - data is not copied
1572c67f1fd2SAl Viro 		csstate->csum = csum_block_add(csstate->csum,
1573c67f1fd2SAl Viro 					       csum_partial(addr, bytes, 0),
1574c67f1fd2SAl Viro 					       csstate->off);
1575c67f1fd2SAl Viro 		csstate->off += bytes;
1576c67f1fd2SAl Viro 		return bytes;
1577241699cdSAl Viro 	}
15786852df12SAl Viro 
15796852df12SAl Viro 	sum = csum_shift(csstate->csum, csstate->off);
15806852df12SAl Viro 	if (unlikely(iov_iter_is_pipe(i)))
15816852df12SAl Viro 		bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
15826852df12SAl Viro 	else iterate_and_advance(i, bytes, base, len, off, ({
15837baa5099SAl Viro 		next = csum_and_copy_to_user(addr + off, base, len);
1584d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
15857baa5099SAl Viro 		next ? 0 : len;
1586d879cb83SAl Viro 	}), ({
15877baa5099SAl Viro 		sum = csum_and_memcpy(base, addr + off, len, sum, off);
1588d879cb83SAl Viro 	})
1589d879cb83SAl Viro 	)
1590594e450bSAl Viro 	csstate->csum = csum_shift(sum, csstate->off);
1591594e450bSAl Viro 	csstate->off += bytes;
1592d879cb83SAl Viro 	return bytes;
1593d879cb83SAl Viro }
1594d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter);
1595d879cb83SAl Viro 
1596d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1597d05f4435SSagi Grimberg 		struct iov_iter *i)
1598d05f4435SSagi Grimberg {
15997999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH
1600d05f4435SSagi Grimberg 	struct ahash_request *hash = hashp;
1601d05f4435SSagi Grimberg 	struct scatterlist sg;
1602d05f4435SSagi Grimberg 	size_t copied;
1603d05f4435SSagi Grimberg 
1604d05f4435SSagi Grimberg 	copied = copy_to_iter(addr, bytes, i);
1605d05f4435SSagi Grimberg 	sg_init_one(&sg, addr, copied);
1606d05f4435SSagi Grimberg 	ahash_request_set_crypt(hash, &sg, NULL, copied);
1607d05f4435SSagi Grimberg 	crypto_ahash_update(hash);
1608d05f4435SSagi Grimberg 	return copied;
160927fad74aSYueHaibing #else
161027fad74aSYueHaibing 	return 0;
161127fad74aSYueHaibing #endif
1612d05f4435SSagi Grimberg }
1613d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter);
1614d05f4435SSagi Grimberg 
161566531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages)
1616d879cb83SAl Viro {
161766531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
161866531c65SAl Viro 	const struct iovec *p;
1619d879cb83SAl Viro 	int npages = 0;
1620d879cb83SAl Viro 
1621de4f5fedSJens Axboe 	for (p = iter_iov(i); size; skip = 0, p++) {
162266531c65SAl Viro 		unsigned offs = offset_in_page(p->iov_base + skip);
162366531c65SAl Viro 		size_t len = min(p->iov_len - skip, size);
1624d879cb83SAl Viro 
162566531c65SAl Viro 		if (len) {
162666531c65SAl Viro 			size -= len;
162766531c65SAl Viro 			npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
162866531c65SAl Viro 			if (unlikely(npages > maxpages))
162966531c65SAl Viro 				return maxpages;
163066531c65SAl Viro 		}
163166531c65SAl Viro 	}
163266531c65SAl Viro 	return npages;
163366531c65SAl Viro }
163466531c65SAl Viro 
163566531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages)
163666531c65SAl Viro {
163766531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
163866531c65SAl Viro 	const struct bio_vec *p;
163966531c65SAl Viro 	int npages = 0;
164066531c65SAl Viro 
164166531c65SAl Viro 	for (p = i->bvec; size; skip = 0, p++) {
164266531c65SAl Viro 		unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
164366531c65SAl Viro 		size_t len = min(p->bv_len - skip, size);
164466531c65SAl Viro 
164566531c65SAl Viro 		size -= len;
164666531c65SAl Viro 		npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
164766531c65SAl Viro 		if (unlikely(npages > maxpages))
164866531c65SAl Viro 			return maxpages;
164966531c65SAl Viro 	}
165066531c65SAl Viro 	return npages;
165166531c65SAl Viro }
165266531c65SAl Viro 
165366531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages)
165466531c65SAl Viro {
165566531c65SAl Viro 	if (unlikely(!i->count))
165666531c65SAl Viro 		return 0;
1657fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1658fcb14cb1SAl Viro 		unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1659fcb14cb1SAl Viro 		int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1660fcb14cb1SAl Viro 		return min(npages, maxpages);
1661fcb14cb1SAl Viro 	}
166266531c65SAl Viro 	/* iovec and kvec have identical layouts */
166366531c65SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
166466531c65SAl Viro 		return iov_npages(i, maxpages);
166566531c65SAl Viro 	if (iov_iter_is_bvec(i))
166666531c65SAl Viro 		return bvec_npages(i, maxpages);
166766531c65SAl Viro 	if (iov_iter_is_pipe(i)) {
166866531c65SAl Viro 		int npages;
1669241699cdSAl Viro 
1670241699cdSAl Viro 		if (!sanity(i))
1671241699cdSAl Viro 			return 0;
1672241699cdSAl Viro 
167312d426abSAl Viro 		pipe_npages(i, &npages);
167466531c65SAl Viro 		return min(npages, maxpages);
167566531c65SAl Viro 	}
167666531c65SAl Viro 	if (iov_iter_is_xarray(i)) {
1677e4f8df86SAl Viro 		unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1678e4f8df86SAl Viro 		int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
167966531c65SAl Viro 		return min(npages, maxpages);
168066531c65SAl Viro 	}
168166531c65SAl Viro 	return 0;
1682d879cb83SAl Viro }
1683d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages);
1684d879cb83SAl Viro 
1685d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1686d879cb83SAl Viro {
1687d879cb83SAl Viro 	*new = *old;
168800e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(new))) {
1689241699cdSAl Viro 		WARN_ON(1);
1690241699cdSAl Viro 		return NULL;
1691241699cdSAl Viro 	}
169200e23707SDavid Howells 	if (iov_iter_is_bvec(new))
1693d879cb83SAl Viro 		return new->bvec = kmemdup(new->bvec,
1694d879cb83SAl Viro 				    new->nr_segs * sizeof(struct bio_vec),
1695d879cb83SAl Viro 				    flags);
1696fcb14cb1SAl Viro 	else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1697d879cb83SAl Viro 		/* iovec and kvec have identical layout */
1698de4f5fedSJens Axboe 		return new->__iov = kmemdup(new->__iov,
1699d879cb83SAl Viro 				   new->nr_segs * sizeof(struct iovec),
1700d879cb83SAl Viro 				   flags);
1701fcb14cb1SAl Viro 	return NULL;
1702d879cb83SAl Viro }
1703d879cb83SAl Viro EXPORT_SYMBOL(dup_iter);
1704bc917be8SAl Viro 
1705bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov,
1706bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1707bfdc5970SChristoph Hellwig {
1708bfdc5970SChristoph Hellwig 	const struct compat_iovec __user *uiov =
1709bfdc5970SChristoph Hellwig 		(const struct compat_iovec __user *)uvec;
1710bfdc5970SChristoph Hellwig 	int ret = -EFAULT, i;
1711bfdc5970SChristoph Hellwig 
1712a959a978SChristoph Hellwig 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1713bfdc5970SChristoph Hellwig 		return -EFAULT;
1714bfdc5970SChristoph Hellwig 
1715bfdc5970SChristoph Hellwig 	for (i = 0; i < nr_segs; i++) {
1716bfdc5970SChristoph Hellwig 		compat_uptr_t buf;
1717bfdc5970SChristoph Hellwig 		compat_ssize_t len;
1718bfdc5970SChristoph Hellwig 
1719bfdc5970SChristoph Hellwig 		unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1720bfdc5970SChristoph Hellwig 		unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1721bfdc5970SChristoph Hellwig 
1722bfdc5970SChristoph Hellwig 		/* check for compat_size_t not fitting in compat_ssize_t .. */
1723bfdc5970SChristoph Hellwig 		if (len < 0) {
1724bfdc5970SChristoph Hellwig 			ret = -EINVAL;
1725bfdc5970SChristoph Hellwig 			goto uaccess_end;
1726bfdc5970SChristoph Hellwig 		}
1727bfdc5970SChristoph Hellwig 		iov[i].iov_base = compat_ptr(buf);
1728bfdc5970SChristoph Hellwig 		iov[i].iov_len = len;
1729bfdc5970SChristoph Hellwig 	}
1730bfdc5970SChristoph Hellwig 
1731bfdc5970SChristoph Hellwig 	ret = 0;
1732bfdc5970SChristoph Hellwig uaccess_end:
1733bfdc5970SChristoph Hellwig 	user_access_end();
1734bfdc5970SChristoph Hellwig 	return ret;
1735bfdc5970SChristoph Hellwig }
1736bfdc5970SChristoph Hellwig 
1737bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov,
1738bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1739fb041b59SDavid Laight {
1740fb041b59SDavid Laight 	unsigned long seg;
1741bfdc5970SChristoph Hellwig 
1742bfdc5970SChristoph Hellwig 	if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1743bfdc5970SChristoph Hellwig 		return -EFAULT;
1744bfdc5970SChristoph Hellwig 	for (seg = 0; seg < nr_segs; seg++) {
1745bfdc5970SChristoph Hellwig 		if ((ssize_t)iov[seg].iov_len < 0)
1746bfdc5970SChristoph Hellwig 			return -EINVAL;
1747bfdc5970SChristoph Hellwig 	}
1748bfdc5970SChristoph Hellwig 
1749bfdc5970SChristoph Hellwig 	return 0;
1750bfdc5970SChristoph Hellwig }
1751bfdc5970SChristoph Hellwig 
1752bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec,
1753bfdc5970SChristoph Hellwig 		unsigned long nr_segs, unsigned long fast_segs,
1754bfdc5970SChristoph Hellwig 		struct iovec *fast_iov, bool compat)
1755bfdc5970SChristoph Hellwig {
1756bfdc5970SChristoph Hellwig 	struct iovec *iov = fast_iov;
1757bfdc5970SChristoph Hellwig 	int ret;
1758fb041b59SDavid Laight 
1759fb041b59SDavid Laight 	/*
1760bfdc5970SChristoph Hellwig 	 * SuS says "The readv() function *may* fail if the iovcnt argument was
1761bfdc5970SChristoph Hellwig 	 * less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1762fb041b59SDavid Laight 	 * traditionally returned zero for zero segments, so...
1763fb041b59SDavid Laight 	 */
1764bfdc5970SChristoph Hellwig 	if (nr_segs == 0)
1765bfdc5970SChristoph Hellwig 		return iov;
1766bfdc5970SChristoph Hellwig 	if (nr_segs > UIO_MAXIOV)
1767bfdc5970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1768fb041b59SDavid Laight 	if (nr_segs > fast_segs) {
1769fb041b59SDavid Laight 		iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1770bfdc5970SChristoph Hellwig 		if (!iov)
1771bfdc5970SChristoph Hellwig 			return ERR_PTR(-ENOMEM);
1772fb041b59SDavid Laight 	}
1773bfdc5970SChristoph Hellwig 
1774bfdc5970SChristoph Hellwig 	if (compat)
1775bfdc5970SChristoph Hellwig 		ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1776bfdc5970SChristoph Hellwig 	else
1777bfdc5970SChristoph Hellwig 		ret = copy_iovec_from_user(iov, uvec, nr_segs);
1778bfdc5970SChristoph Hellwig 	if (ret) {
1779bfdc5970SChristoph Hellwig 		if (iov != fast_iov)
1780bfdc5970SChristoph Hellwig 			kfree(iov);
1781bfdc5970SChristoph Hellwig 		return ERR_PTR(ret);
1782fb041b59SDavid Laight 	}
1783bfdc5970SChristoph Hellwig 
1784bfdc5970SChristoph Hellwig 	return iov;
1785bfdc5970SChristoph Hellwig }
1786bfdc5970SChristoph Hellwig 
1787*3b2deb0eSJens Axboe /*
1788*3b2deb0eSJens Axboe  * Single segment iovec supplied by the user, import it as ITER_UBUF.
1789*3b2deb0eSJens Axboe  */
1790*3b2deb0eSJens Axboe static ssize_t __import_iovec_ubuf(int type, const struct iovec __user *uvec,
1791*3b2deb0eSJens Axboe 				   struct iovec **iovp, struct iov_iter *i,
1792*3b2deb0eSJens Axboe 				   bool compat)
1793*3b2deb0eSJens Axboe {
1794*3b2deb0eSJens Axboe 	struct iovec *iov = *iovp;
1795*3b2deb0eSJens Axboe 	ssize_t ret;
1796*3b2deb0eSJens Axboe 
1797*3b2deb0eSJens Axboe 	if (compat)
1798*3b2deb0eSJens Axboe 		ret = copy_compat_iovec_from_user(iov, uvec, 1);
1799*3b2deb0eSJens Axboe 	else
1800*3b2deb0eSJens Axboe 		ret = copy_iovec_from_user(iov, uvec, 1);
1801*3b2deb0eSJens Axboe 	if (unlikely(ret))
1802*3b2deb0eSJens Axboe 		return ret;
1803*3b2deb0eSJens Axboe 
1804*3b2deb0eSJens Axboe 	ret = import_ubuf(type, iov->iov_base, iov->iov_len, i);
1805*3b2deb0eSJens Axboe 	if (unlikely(ret))
1806*3b2deb0eSJens Axboe 		return ret;
1807*3b2deb0eSJens Axboe 	*iovp = NULL;
1808*3b2deb0eSJens Axboe 	return i->count;
1809*3b2deb0eSJens Axboe }
1810*3b2deb0eSJens Axboe 
1811bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1812bfdc5970SChristoph Hellwig 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1813bfdc5970SChristoph Hellwig 		 struct iov_iter *i, bool compat)
1814bfdc5970SChristoph Hellwig {
1815bfdc5970SChristoph Hellwig 	ssize_t total_len = 0;
1816bfdc5970SChristoph Hellwig 	unsigned long seg;
1817bfdc5970SChristoph Hellwig 	struct iovec *iov;
1818bfdc5970SChristoph Hellwig 
1819*3b2deb0eSJens Axboe 	if (nr_segs == 1)
1820*3b2deb0eSJens Axboe 		return __import_iovec_ubuf(type, uvec, iovp, i, compat);
1821*3b2deb0eSJens Axboe 
1822bfdc5970SChristoph Hellwig 	iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1823bfdc5970SChristoph Hellwig 	if (IS_ERR(iov)) {
1824bfdc5970SChristoph Hellwig 		*iovp = NULL;
1825bfdc5970SChristoph Hellwig 		return PTR_ERR(iov);
1826fb041b59SDavid Laight 	}
1827fb041b59SDavid Laight 
1828fb041b59SDavid Laight 	/*
1829bfdc5970SChristoph Hellwig 	 * According to the Single Unix Specification we should return EINVAL if
1830bfdc5970SChristoph Hellwig 	 * an element length is < 0 when cast to ssize_t or if the total length
1831bfdc5970SChristoph Hellwig 	 * would overflow the ssize_t return value of the system call.
1832fb041b59SDavid Laight 	 *
1833fb041b59SDavid Laight 	 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1834fb041b59SDavid Laight 	 * overflow case.
1835fb041b59SDavid Laight 	 */
1836fb041b59SDavid Laight 	for (seg = 0; seg < nr_segs; seg++) {
1837fb041b59SDavid Laight 		ssize_t len = (ssize_t)iov[seg].iov_len;
1838fb041b59SDavid Laight 
1839bfdc5970SChristoph Hellwig 		if (!access_ok(iov[seg].iov_base, len)) {
1840bfdc5970SChristoph Hellwig 			if (iov != *iovp)
1841bfdc5970SChristoph Hellwig 				kfree(iov);
1842bfdc5970SChristoph Hellwig 			*iovp = NULL;
1843bfdc5970SChristoph Hellwig 			return -EFAULT;
1844fb041b59SDavid Laight 		}
1845bfdc5970SChristoph Hellwig 
1846bfdc5970SChristoph Hellwig 		if (len > MAX_RW_COUNT - total_len) {
1847bfdc5970SChristoph Hellwig 			len = MAX_RW_COUNT - total_len;
1848fb041b59SDavid Laight 			iov[seg].iov_len = len;
1849fb041b59SDavid Laight 		}
1850bfdc5970SChristoph Hellwig 		total_len += len;
1851fb041b59SDavid Laight 	}
1852bfdc5970SChristoph Hellwig 
1853bfdc5970SChristoph Hellwig 	iov_iter_init(i, type, iov, nr_segs, total_len);
1854bfdc5970SChristoph Hellwig 	if (iov == *iovp)
1855bfdc5970SChristoph Hellwig 		*iovp = NULL;
1856bfdc5970SChristoph Hellwig 	else
1857bfdc5970SChristoph Hellwig 		*iovp = iov;
1858bfdc5970SChristoph Hellwig 	return total_len;
1859fb041b59SDavid Laight }
1860fb041b59SDavid Laight 
1861ffecee4fSVegard Nossum /**
1862ffecee4fSVegard Nossum  * import_iovec() - Copy an array of &struct iovec from userspace
1863ffecee4fSVegard Nossum  *     into the kernel, check that it is valid, and initialize a new
1864ffecee4fSVegard Nossum  *     &struct iov_iter iterator to access it.
1865ffecee4fSVegard Nossum  *
1866ffecee4fSVegard Nossum  * @type: One of %READ or %WRITE.
1867bfdc5970SChristoph Hellwig  * @uvec: Pointer to the userspace array.
1868ffecee4fSVegard Nossum  * @nr_segs: Number of elements in userspace array.
1869ffecee4fSVegard Nossum  * @fast_segs: Number of elements in @iov.
1870bfdc5970SChristoph Hellwig  * @iovp: (input and output parameter) Pointer to pointer to (usually small
1871ffecee4fSVegard Nossum  *     on-stack) kernel array.
1872ffecee4fSVegard Nossum  * @i: Pointer to iterator that will be initialized on success.
1873ffecee4fSVegard Nossum  *
1874ffecee4fSVegard Nossum  * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1875ffecee4fSVegard Nossum  * then this function places %NULL in *@iov on return. Otherwise, a new
1876ffecee4fSVegard Nossum  * array will be allocated and the result placed in *@iov. This means that
1877ffecee4fSVegard Nossum  * the caller may call kfree() on *@iov regardless of whether the small
1878ffecee4fSVegard Nossum  * on-stack array was used or not (and regardless of whether this function
1879ffecee4fSVegard Nossum  * returns an error or not).
1880ffecee4fSVegard Nossum  *
188187e5e6daSJens Axboe  * Return: Negative error code on error, bytes imported on success
1882ffecee4fSVegard Nossum  */
1883bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec,
1884bc917be8SAl Viro 		 unsigned nr_segs, unsigned fast_segs,
1885bfdc5970SChristoph Hellwig 		 struct iovec **iovp, struct iov_iter *i)
1886bc917be8SAl Viro {
188789cd35c5SChristoph Hellwig 	return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
188889cd35c5SChristoph Hellwig 			      in_compat_syscall());
1889bc917be8SAl Viro }
1890bc917be8SAl Viro EXPORT_SYMBOL(import_iovec);
1891bc917be8SAl Viro 
1892bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len,
1893bc917be8SAl Viro 		 struct iovec *iov, struct iov_iter *i)
1894bc917be8SAl Viro {
1895bc917be8SAl Viro 	if (len > MAX_RW_COUNT)
1896bc917be8SAl Viro 		len = MAX_RW_COUNT;
189796d4f267SLinus Torvalds 	if (unlikely(!access_ok(buf, len)))
1898bc917be8SAl Viro 		return -EFAULT;
1899bc917be8SAl Viro 
1900e03ad4eeSJens Axboe 	iov_iter_ubuf(i, rw, buf, len);
1901bc917be8SAl Viro 	return 0;
1902bc917be8SAl Viro }
1903e1267585SAl Viro EXPORT_SYMBOL(import_single_range);
19048fb0f47aSJens Axboe 
19052ad9bd83SJens Axboe int import_ubuf(int rw, void __user *buf, size_t len, struct iov_iter *i)
19062ad9bd83SJens Axboe {
19072ad9bd83SJens Axboe 	if (len > MAX_RW_COUNT)
19082ad9bd83SJens Axboe 		len = MAX_RW_COUNT;
19092ad9bd83SJens Axboe 	if (unlikely(!access_ok(buf, len)))
19102ad9bd83SJens Axboe 		return -EFAULT;
19112ad9bd83SJens Axboe 
19122ad9bd83SJens Axboe 	iov_iter_ubuf(i, rw, buf, len);
19132ad9bd83SJens Axboe 	return 0;
19142ad9bd83SJens Axboe }
19152ad9bd83SJens Axboe 
19168fb0f47aSJens Axboe /**
19178fb0f47aSJens Axboe  * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
19188fb0f47aSJens Axboe  *     iov_iter_save_state() was called.
19198fb0f47aSJens Axboe  *
19208fb0f47aSJens Axboe  * @i: &struct iov_iter to restore
19218fb0f47aSJens Axboe  * @state: state to restore from
19228fb0f47aSJens Axboe  *
19238fb0f47aSJens Axboe  * Used after iov_iter_save_state() to bring restore @i, if operations may
19248fb0f47aSJens Axboe  * have advanced it.
19258fb0f47aSJens Axboe  *
19268fb0f47aSJens Axboe  * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
19278fb0f47aSJens Axboe  */
19288fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
19298fb0f47aSJens Axboe {
19304397a17cSKeith Busch 	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) &&
19314397a17cSKeith Busch 			 !iter_is_ubuf(i)) && !iov_iter_is_kvec(i))
19328fb0f47aSJens Axboe 		return;
19338fb0f47aSJens Axboe 	i->iov_offset = state->iov_offset;
19348fb0f47aSJens Axboe 	i->count = state->count;
1935fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1936fcb14cb1SAl Viro 		return;
19378fb0f47aSJens Axboe 	/*
19388fb0f47aSJens Axboe 	 * For the *vec iters, nr_segs + iov is constant - if we increment
19398fb0f47aSJens Axboe 	 * the vec, then we also decrement the nr_segs count. Hence we don't
19408fb0f47aSJens Axboe 	 * need to track both of these, just one is enough and we can deduct
19418fb0f47aSJens Axboe 	 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
19428fb0f47aSJens Axboe 	 * size, so we can just increment the iov pointer as they are unionzed.
19438fb0f47aSJens Axboe 	 * ITER_BVEC _may_ be the same size on some archs, but on others it is
19448fb0f47aSJens Axboe 	 * not. Be safe and handle it separately.
19458fb0f47aSJens Axboe 	 */
19468fb0f47aSJens Axboe 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
19478fb0f47aSJens Axboe 	if (iov_iter_is_bvec(i))
19488fb0f47aSJens Axboe 		i->bvec -= state->nr_segs - i->nr_segs;
19498fb0f47aSJens Axboe 	else
1950de4f5fedSJens Axboe 		i->__iov -= state->nr_segs - i->nr_segs;
19518fb0f47aSJens Axboe 	i->nr_segs = state->nr_segs;
19528fb0f47aSJens Axboe }
19537d58fe73SDavid Howells 
19547d58fe73SDavid Howells /*
19557d58fe73SDavid Howells  * Extract a list of contiguous pages from an ITER_XARRAY iterator.  This does not
19567d58fe73SDavid Howells  * get references on the pages, nor does it get a pin on them.
19577d58fe73SDavid Howells  */
19587d58fe73SDavid Howells static ssize_t iov_iter_extract_xarray_pages(struct iov_iter *i,
19597d58fe73SDavid Howells 					     struct page ***pages, size_t maxsize,
19607d58fe73SDavid Howells 					     unsigned int maxpages,
19617d58fe73SDavid Howells 					     iov_iter_extraction_t extraction_flags,
19627d58fe73SDavid Howells 					     size_t *offset0)
19637d58fe73SDavid Howells {
19647d58fe73SDavid Howells 	struct page *page, **p;
19657d58fe73SDavid Howells 	unsigned int nr = 0, offset;
19667d58fe73SDavid Howells 	loff_t pos = i->xarray_start + i->iov_offset;
19677d58fe73SDavid Howells 	pgoff_t index = pos >> PAGE_SHIFT;
19687d58fe73SDavid Howells 	XA_STATE(xas, i->xarray, index);
19697d58fe73SDavid Howells 
19707d58fe73SDavid Howells 	offset = pos & ~PAGE_MASK;
19717d58fe73SDavid Howells 	*offset0 = offset;
19727d58fe73SDavid Howells 
19737d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
19747d58fe73SDavid Howells 	if (!maxpages)
19757d58fe73SDavid Howells 		return -ENOMEM;
19767d58fe73SDavid Howells 	p = *pages;
19777d58fe73SDavid Howells 
19787d58fe73SDavid Howells 	rcu_read_lock();
19797d58fe73SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
19807d58fe73SDavid Howells 		if (xas_retry(&xas, page))
19817d58fe73SDavid Howells 			continue;
19827d58fe73SDavid Howells 
19837d58fe73SDavid Howells 		/* Has the page moved or been split? */
19847d58fe73SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
19857d58fe73SDavid Howells 			xas_reset(&xas);
19867d58fe73SDavid Howells 			continue;
19877d58fe73SDavid Howells 		}
19887d58fe73SDavid Howells 
19897d58fe73SDavid Howells 		p[nr++] = find_subpage(page, xas.xa_index);
19907d58fe73SDavid Howells 		if (nr == maxpages)
19917d58fe73SDavid Howells 			break;
19927d58fe73SDavid Howells 	}
19937d58fe73SDavid Howells 	rcu_read_unlock();
19947d58fe73SDavid Howells 
19957d58fe73SDavid Howells 	maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
19967d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
19977d58fe73SDavid Howells 	return maxsize;
19987d58fe73SDavid Howells }
19997d58fe73SDavid Howells 
20007d58fe73SDavid Howells /*
20017d58fe73SDavid Howells  * Extract a list of contiguous pages from an ITER_BVEC iterator.  This does
20027d58fe73SDavid Howells  * not get references on the pages, nor does it get a pin on them.
20037d58fe73SDavid Howells  */
20047d58fe73SDavid Howells static ssize_t iov_iter_extract_bvec_pages(struct iov_iter *i,
20057d58fe73SDavid Howells 					   struct page ***pages, size_t maxsize,
20067d58fe73SDavid Howells 					   unsigned int maxpages,
20077d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
20087d58fe73SDavid Howells 					   size_t *offset0)
20097d58fe73SDavid Howells {
20107d58fe73SDavid Howells 	struct page **p, *page;
20117d58fe73SDavid Howells 	size_t skip = i->iov_offset, offset;
20127d58fe73SDavid Howells 	int k;
20137d58fe73SDavid Howells 
20147d58fe73SDavid Howells 	for (;;) {
20157d58fe73SDavid Howells 		if (i->nr_segs == 0)
20167d58fe73SDavid Howells 			return 0;
20177d58fe73SDavid Howells 		maxsize = min(maxsize, i->bvec->bv_len - skip);
20187d58fe73SDavid Howells 		if (maxsize)
20197d58fe73SDavid Howells 			break;
20207d58fe73SDavid Howells 		i->iov_offset = 0;
20217d58fe73SDavid Howells 		i->nr_segs--;
20227d58fe73SDavid Howells 		i->bvec++;
20237d58fe73SDavid Howells 		skip = 0;
20247d58fe73SDavid Howells 	}
20257d58fe73SDavid Howells 
20267d58fe73SDavid Howells 	skip += i->bvec->bv_offset;
20277d58fe73SDavid Howells 	page = i->bvec->bv_page + skip / PAGE_SIZE;
20287d58fe73SDavid Howells 	offset = skip % PAGE_SIZE;
20297d58fe73SDavid Howells 	*offset0 = offset;
20307d58fe73SDavid Howells 
20317d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
20327d58fe73SDavid Howells 	if (!maxpages)
20337d58fe73SDavid Howells 		return -ENOMEM;
20347d58fe73SDavid Howells 	p = *pages;
20357d58fe73SDavid Howells 	for (k = 0; k < maxpages; k++)
20367d58fe73SDavid Howells 		p[k] = page + k;
20377d58fe73SDavid Howells 
20387d58fe73SDavid Howells 	maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset);
20397d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
20407d58fe73SDavid Howells 	return maxsize;
20417d58fe73SDavid Howells }
20427d58fe73SDavid Howells 
20437d58fe73SDavid Howells /*
20447d58fe73SDavid Howells  * Extract a list of virtually contiguous pages from an ITER_KVEC iterator.
20457d58fe73SDavid Howells  * This does not get references on the pages, nor does it get a pin on them.
20467d58fe73SDavid Howells  */
20477d58fe73SDavid Howells static ssize_t iov_iter_extract_kvec_pages(struct iov_iter *i,
20487d58fe73SDavid Howells 					   struct page ***pages, size_t maxsize,
20497d58fe73SDavid Howells 					   unsigned int maxpages,
20507d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
20517d58fe73SDavid Howells 					   size_t *offset0)
20527d58fe73SDavid Howells {
20537d58fe73SDavid Howells 	struct page **p, *page;
20547d58fe73SDavid Howells 	const void *kaddr;
20557d58fe73SDavid Howells 	size_t skip = i->iov_offset, offset, len;
20567d58fe73SDavid Howells 	int k;
20577d58fe73SDavid Howells 
20587d58fe73SDavid Howells 	for (;;) {
20597d58fe73SDavid Howells 		if (i->nr_segs == 0)
20607d58fe73SDavid Howells 			return 0;
20617d58fe73SDavid Howells 		maxsize = min(maxsize, i->kvec->iov_len - skip);
20627d58fe73SDavid Howells 		if (maxsize)
20637d58fe73SDavid Howells 			break;
20647d58fe73SDavid Howells 		i->iov_offset = 0;
20657d58fe73SDavid Howells 		i->nr_segs--;
20667d58fe73SDavid Howells 		i->kvec++;
20677d58fe73SDavid Howells 		skip = 0;
20687d58fe73SDavid Howells 	}
20697d58fe73SDavid Howells 
20707d58fe73SDavid Howells 	kaddr = i->kvec->iov_base + skip;
20717d58fe73SDavid Howells 	offset = (unsigned long)kaddr & ~PAGE_MASK;
20727d58fe73SDavid Howells 	*offset0 = offset;
20737d58fe73SDavid Howells 
20747d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
20757d58fe73SDavid Howells 	if (!maxpages)
20767d58fe73SDavid Howells 		return -ENOMEM;
20777d58fe73SDavid Howells 	p = *pages;
20787d58fe73SDavid Howells 
20797d58fe73SDavid Howells 	kaddr -= offset;
20807d58fe73SDavid Howells 	len = offset + maxsize;
20817d58fe73SDavid Howells 	for (k = 0; k < maxpages; k++) {
20827d58fe73SDavid Howells 		size_t seg = min_t(size_t, len, PAGE_SIZE);
20837d58fe73SDavid Howells 
20847d58fe73SDavid Howells 		if (is_vmalloc_or_module_addr(kaddr))
20857d58fe73SDavid Howells 			page = vmalloc_to_page(kaddr);
20867d58fe73SDavid Howells 		else
20877d58fe73SDavid Howells 			page = virt_to_page(kaddr);
20887d58fe73SDavid Howells 
20897d58fe73SDavid Howells 		p[k] = page;
20907d58fe73SDavid Howells 		len -= seg;
20917d58fe73SDavid Howells 		kaddr += PAGE_SIZE;
20927d58fe73SDavid Howells 	}
20937d58fe73SDavid Howells 
20947d58fe73SDavid Howells 	maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset);
20957d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
20967d58fe73SDavid Howells 	return maxsize;
20977d58fe73SDavid Howells }
20987d58fe73SDavid Howells 
20997d58fe73SDavid Howells /*
21007d58fe73SDavid Howells  * Extract a list of contiguous pages from a user iterator and get a pin on
21017d58fe73SDavid Howells  * each of them.  This should only be used if the iterator is user-backed
21027d58fe73SDavid Howells  * (IOBUF/UBUF).
21037d58fe73SDavid Howells  *
21047d58fe73SDavid Howells  * It does not get refs on the pages, but the pages must be unpinned by the
21057d58fe73SDavid Howells  * caller once the transfer is complete.
21067d58fe73SDavid Howells  *
21077d58fe73SDavid Howells  * This is safe to be used where background IO/DMA *is* going to be modifying
21087d58fe73SDavid Howells  * the buffer; using a pin rather than a ref makes forces fork() to give the
21097d58fe73SDavid Howells  * child a copy of the page.
21107d58fe73SDavid Howells  */
21117d58fe73SDavid Howells static ssize_t iov_iter_extract_user_pages(struct iov_iter *i,
21127d58fe73SDavid Howells 					   struct page ***pages,
21137d58fe73SDavid Howells 					   size_t maxsize,
21147d58fe73SDavid Howells 					   unsigned int maxpages,
21157d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
21167d58fe73SDavid Howells 					   size_t *offset0)
21177d58fe73SDavid Howells {
21187d58fe73SDavid Howells 	unsigned long addr;
21197d58fe73SDavid Howells 	unsigned int gup_flags = 0;
21207d58fe73SDavid Howells 	size_t offset;
21217d58fe73SDavid Howells 	int res;
21227d58fe73SDavid Howells 
21237d58fe73SDavid Howells 	if (i->data_source == ITER_DEST)
21247d58fe73SDavid Howells 		gup_flags |= FOLL_WRITE;
21257d58fe73SDavid Howells 	if (extraction_flags & ITER_ALLOW_P2PDMA)
21267d58fe73SDavid Howells 		gup_flags |= FOLL_PCI_P2PDMA;
21277d58fe73SDavid Howells 	if (i->nofault)
21287d58fe73SDavid Howells 		gup_flags |= FOLL_NOFAULT;
21297d58fe73SDavid Howells 
21307d58fe73SDavid Howells 	addr = first_iovec_segment(i, &maxsize);
21317d58fe73SDavid Howells 	*offset0 = offset = addr % PAGE_SIZE;
21327d58fe73SDavid Howells 	addr &= PAGE_MASK;
21337d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
21347d58fe73SDavid Howells 	if (!maxpages)
21357d58fe73SDavid Howells 		return -ENOMEM;
21367d58fe73SDavid Howells 	res = pin_user_pages_fast(addr, maxpages, gup_flags, *pages);
21377d58fe73SDavid Howells 	if (unlikely(res <= 0))
21387d58fe73SDavid Howells 		return res;
21397d58fe73SDavid Howells 	maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - offset);
21407d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
21417d58fe73SDavid Howells 	return maxsize;
21427d58fe73SDavid Howells }
21437d58fe73SDavid Howells 
21447d58fe73SDavid Howells /**
21457d58fe73SDavid Howells  * iov_iter_extract_pages - Extract a list of contiguous pages from an iterator
21467d58fe73SDavid Howells  * @i: The iterator to extract from
21477d58fe73SDavid Howells  * @pages: Where to return the list of pages
21487d58fe73SDavid Howells  * @maxsize: The maximum amount of iterator to extract
21497d58fe73SDavid Howells  * @maxpages: The maximum size of the list of pages
21507d58fe73SDavid Howells  * @extraction_flags: Flags to qualify request
21517d58fe73SDavid Howells  * @offset0: Where to return the starting offset into (*@pages)[0]
21527d58fe73SDavid Howells  *
21537d58fe73SDavid Howells  * Extract a list of contiguous pages from the current point of the iterator,
21547d58fe73SDavid Howells  * advancing the iterator.  The maximum number of pages and the maximum amount
21557d58fe73SDavid Howells  * of page contents can be set.
21567d58fe73SDavid Howells  *
21577d58fe73SDavid Howells  * If *@pages is NULL, a page list will be allocated to the required size and
21587d58fe73SDavid Howells  * *@pages will be set to its base.  If *@pages is not NULL, it will be assumed
21597d58fe73SDavid Howells  * that the caller allocated a page list at least @maxpages in size and this
21607d58fe73SDavid Howells  * will be filled in.
21617d58fe73SDavid Howells  *
21627d58fe73SDavid Howells  * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA
21637d58fe73SDavid Howells  * be allowed on the pages extracted.
21647d58fe73SDavid Howells  *
21657d58fe73SDavid Howells  * The iov_iter_extract_will_pin() function can be used to query how cleanup
21667d58fe73SDavid Howells  * should be performed.
21677d58fe73SDavid Howells  *
21687d58fe73SDavid Howells  * Extra refs or pins on the pages may be obtained as follows:
21697d58fe73SDavid Howells  *
21707d58fe73SDavid Howells  *  (*) If the iterator is user-backed (ITER_IOVEC/ITER_UBUF), pins will be
21717d58fe73SDavid Howells  *      added to the pages, but refs will not be taken.
21727d58fe73SDavid Howells  *      iov_iter_extract_will_pin() will return true.
21737d58fe73SDavid Howells  *
21747d58fe73SDavid Howells  *  (*) If the iterator is ITER_KVEC, ITER_BVEC or ITER_XARRAY, the pages are
21757d58fe73SDavid Howells  *      merely listed; no extra refs or pins are obtained.
21767d58fe73SDavid Howells  *      iov_iter_extract_will_pin() will return 0.
21777d58fe73SDavid Howells  *
21787d58fe73SDavid Howells  * Note also:
21797d58fe73SDavid Howells  *
21807d58fe73SDavid Howells  *  (*) Use with ITER_DISCARD is not supported as that has no content.
21817d58fe73SDavid Howells  *
21827d58fe73SDavid Howells  * On success, the function sets *@pages to the new pagelist, if allocated, and
21837d58fe73SDavid Howells  * sets *offset0 to the offset into the first page.
21847d58fe73SDavid Howells  *
21857d58fe73SDavid Howells  * It may also return -ENOMEM and -EFAULT.
21867d58fe73SDavid Howells  */
21877d58fe73SDavid Howells ssize_t iov_iter_extract_pages(struct iov_iter *i,
21887d58fe73SDavid Howells 			       struct page ***pages,
21897d58fe73SDavid Howells 			       size_t maxsize,
21907d58fe73SDavid Howells 			       unsigned int maxpages,
21917d58fe73SDavid Howells 			       iov_iter_extraction_t extraction_flags,
21927d58fe73SDavid Howells 			       size_t *offset0)
21937d58fe73SDavid Howells {
21947d58fe73SDavid Howells 	maxsize = min_t(size_t, min_t(size_t, maxsize, i->count), MAX_RW_COUNT);
21957d58fe73SDavid Howells 	if (!maxsize)
21967d58fe73SDavid Howells 		return 0;
21977d58fe73SDavid Howells 
21987d58fe73SDavid Howells 	if (likely(user_backed_iter(i)))
21997d58fe73SDavid Howells 		return iov_iter_extract_user_pages(i, pages, maxsize,
22007d58fe73SDavid Howells 						   maxpages, extraction_flags,
22017d58fe73SDavid Howells 						   offset0);
22027d58fe73SDavid Howells 	if (iov_iter_is_kvec(i))
22037d58fe73SDavid Howells 		return iov_iter_extract_kvec_pages(i, pages, maxsize,
22047d58fe73SDavid Howells 						   maxpages, extraction_flags,
22057d58fe73SDavid Howells 						   offset0);
22067d58fe73SDavid Howells 	if (iov_iter_is_bvec(i))
22077d58fe73SDavid Howells 		return iov_iter_extract_bvec_pages(i, pages, maxsize,
22087d58fe73SDavid Howells 						   maxpages, extraction_flags,
22097d58fe73SDavid Howells 						   offset0);
22107d58fe73SDavid Howells 	if (iov_iter_is_xarray(i))
22117d58fe73SDavid Howells 		return iov_iter_extract_xarray_pages(i, pages, maxsize,
22127d58fe73SDavid Howells 						     maxpages, extraction_flags,
22137d58fe73SDavid Howells 						     offset0);
22147d58fe73SDavid Howells 	return -EFAULT;
22157d58fe73SDavid Howells }
22167d58fe73SDavid Howells EXPORT_SYMBOL_GPL(iov_iter_extract_pages);
2217