xref: /openbmc/linux/lib/iov_iter.c (revision 8fad7767)
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))) {		\
1295c67aa90SAl Viro 			const struct iovec *iov = i->iov;	\
1307baa5099SAl Viro 			void __user *base;			\
1317baa5099SAl Viro 			size_t len;				\
1327baa5099SAl Viro 			iterate_iovec(i, n, base, len, off,	\
133a6e4ec7bSAl Viro 						iov, (I))	\
134d879cb83SAl Viro 			i->nr_segs -= iov - i->iov;		\
135d879cb83SAl Viro 			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 {
1774d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1784d0e9df5SAlbert van der Linde 		return n;
17996d4f267SLinus Torvalds 	if (access_ok(from, n)) {
180d0ef4c36SMarco Elver 		instrument_copy_from_user(to, from, n);
18109fc68dcSAl Viro 		n = raw_copy_from_user(to, from, n);
18209fc68dcSAl Viro 	}
18309fc68dcSAl Viro 	return n;
18409fc68dcSAl Viro }
18509fc68dcSAl Viro 
1862dcedb2aSAl Viro static inline struct pipe_buffer *pipe_buf(const struct pipe_inode_info *pipe,
1872dcedb2aSAl Viro 					   unsigned int slot)
1882dcedb2aSAl Viro {
1892dcedb2aSAl Viro 	return &pipe->bufs[slot & (pipe->ring_size - 1)];
1902dcedb2aSAl Viro }
1912dcedb2aSAl Viro 
192241699cdSAl Viro #ifdef PIPE_PARANOIA
193241699cdSAl Viro static bool sanity(const struct iov_iter *i)
194241699cdSAl Viro {
195241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
1968cefc107SDavid Howells 	unsigned int p_head = pipe->head;
1978cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
1988cefc107SDavid Howells 	unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
1998cefc107SDavid Howells 	unsigned int i_head = i->head;
2008cefc107SDavid Howells 	unsigned int idx;
2018cefc107SDavid Howells 
202241699cdSAl Viro 	if (i->iov_offset) {
203241699cdSAl Viro 		struct pipe_buffer *p;
2048cefc107SDavid Howells 		if (unlikely(p_occupancy == 0))
205241699cdSAl Viro 			goto Bad;	// pipe must be non-empty
2068cefc107SDavid Howells 		if (unlikely(i_head != p_head - 1))
207241699cdSAl Viro 			goto Bad;	// must be at the last buffer...
208241699cdSAl Viro 
2092dcedb2aSAl Viro 		p = pipe_buf(pipe, i_head);
210241699cdSAl Viro 		if (unlikely(p->offset + p->len != i->iov_offset))
211241699cdSAl Viro 			goto Bad;	// ... at the end of segment
212241699cdSAl Viro 	} else {
2138cefc107SDavid Howells 		if (i_head != p_head)
214241699cdSAl Viro 			goto Bad;	// must be right after the last buffer
215241699cdSAl Viro 	}
216241699cdSAl Viro 	return true;
217241699cdSAl Viro Bad:
2188cefc107SDavid Howells 	printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
2198cefc107SDavid Howells 	printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
2208cefc107SDavid Howells 			p_head, p_tail, pipe->ring_size);
2218cefc107SDavid Howells 	for (idx = 0; idx < pipe->ring_size; idx++)
222241699cdSAl Viro 		printk(KERN_ERR "[%p %p %d %d]\n",
223241699cdSAl Viro 			pipe->bufs[idx].ops,
224241699cdSAl Viro 			pipe->bufs[idx].page,
225241699cdSAl Viro 			pipe->bufs[idx].offset,
226241699cdSAl Viro 			pipe->bufs[idx].len);
227241699cdSAl Viro 	WARN_ON(1);
228241699cdSAl Viro 	return false;
229241699cdSAl Viro }
230241699cdSAl Viro #else
231241699cdSAl Viro #define sanity(i) true
232241699cdSAl Viro #endif
233241699cdSAl Viro 
23447b7fcaeSAl Viro static struct page *push_anon(struct pipe_inode_info *pipe, unsigned size)
23547b7fcaeSAl Viro {
23647b7fcaeSAl Viro 	struct page *page = alloc_page(GFP_USER);
23747b7fcaeSAl Viro 	if (page) {
23847b7fcaeSAl Viro 		struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
23947b7fcaeSAl Viro 		*buf = (struct pipe_buffer) {
24047b7fcaeSAl Viro 			.ops = &default_pipe_buf_ops,
24147b7fcaeSAl Viro 			.page = page,
24247b7fcaeSAl Viro 			.offset = 0,
24347b7fcaeSAl Viro 			.len = size
24447b7fcaeSAl Viro 		};
24547b7fcaeSAl Viro 	}
24647b7fcaeSAl Viro 	return page;
24747b7fcaeSAl Viro }
24847b7fcaeSAl Viro 
24947b7fcaeSAl Viro static void push_page(struct pipe_inode_info *pipe, struct page *page,
25047b7fcaeSAl Viro 			unsigned int offset, unsigned int size)
25147b7fcaeSAl Viro {
25247b7fcaeSAl Viro 	struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
25347b7fcaeSAl Viro 	*buf = (struct pipe_buffer) {
25447b7fcaeSAl Viro 		.ops = &page_cache_pipe_buf_ops,
25547b7fcaeSAl Viro 		.page = page,
25647b7fcaeSAl Viro 		.offset = offset,
25747b7fcaeSAl Viro 		.len = size
25847b7fcaeSAl Viro 	};
25947b7fcaeSAl Viro 	get_page(page);
26047b7fcaeSAl Viro }
26147b7fcaeSAl Viro 
262*8fad7767SAl Viro static inline bool allocated(struct pipe_buffer *buf)
263*8fad7767SAl Viro {
264*8fad7767SAl Viro 	return buf->ops == &default_pipe_buf_ops;
265*8fad7767SAl Viro }
266*8fad7767SAl Viro 
267*8fad7767SAl Viro static struct page *append_pipe(struct iov_iter *i, size_t size,
268*8fad7767SAl Viro 				unsigned int *off)
269*8fad7767SAl Viro {
270*8fad7767SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
271*8fad7767SAl Viro 	size_t offset = i->iov_offset;
272*8fad7767SAl Viro 	struct pipe_buffer *buf;
273*8fad7767SAl Viro 	struct page *page;
274*8fad7767SAl Viro 
275*8fad7767SAl Viro 	if (offset && offset < PAGE_SIZE) {
276*8fad7767SAl Viro 		// some space in the last buffer; can we add to it?
277*8fad7767SAl Viro 		buf = pipe_buf(pipe, pipe->head - 1);
278*8fad7767SAl Viro 		if (allocated(buf)) {
279*8fad7767SAl Viro 			size = min_t(size_t, size, PAGE_SIZE - offset);
280*8fad7767SAl Viro 			buf->len += size;
281*8fad7767SAl Viro 			i->iov_offset += size;
282*8fad7767SAl Viro 			i->count -= size;
283*8fad7767SAl Viro 			*off = offset;
284*8fad7767SAl Viro 			return buf->page;
285*8fad7767SAl Viro 		}
286*8fad7767SAl Viro 	}
287*8fad7767SAl Viro 	// OK, we need a new buffer
288*8fad7767SAl Viro 	*off = 0;
289*8fad7767SAl Viro 	size = min_t(size_t, size, PAGE_SIZE);
290*8fad7767SAl Viro 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
291*8fad7767SAl Viro 		return NULL;
292*8fad7767SAl Viro 	page = push_anon(pipe, size);
293*8fad7767SAl Viro 	if (!page)
294*8fad7767SAl Viro 		return NULL;
295*8fad7767SAl Viro 	i->head = pipe->head - 1;
296*8fad7767SAl Viro 	i->iov_offset = size;
297*8fad7767SAl Viro 	i->count -= size;
298*8fad7767SAl Viro 	return page;
299*8fad7767SAl Viro }
300*8fad7767SAl Viro 
301241699cdSAl Viro static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
302241699cdSAl Viro 			 struct iov_iter *i)
303241699cdSAl Viro {
304241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
30547b7fcaeSAl Viro 	unsigned int head = pipe->head;
306241699cdSAl Viro 
307241699cdSAl Viro 	if (unlikely(bytes > i->count))
308241699cdSAl Viro 		bytes = i->count;
309241699cdSAl Viro 
310241699cdSAl Viro 	if (unlikely(!bytes))
311241699cdSAl Viro 		return 0;
312241699cdSAl Viro 
313241699cdSAl Viro 	if (!sanity(i))
314241699cdSAl Viro 		return 0;
315241699cdSAl Viro 
31647b7fcaeSAl Viro 	if (offset && i->iov_offset == offset) { // could we merge it?
31747b7fcaeSAl Viro 		struct pipe_buffer *buf = pipe_buf(pipe, head - 1);
31847b7fcaeSAl Viro 		if (buf->page == page) {
319241699cdSAl Viro 			buf->len += bytes;
320241699cdSAl Viro 			i->iov_offset += bytes;
32147b7fcaeSAl Viro 			i->count -= bytes;
32247b7fcaeSAl Viro 			return bytes;
323241699cdSAl Viro 		}
324241699cdSAl Viro 	}
32547b7fcaeSAl Viro 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
326241699cdSAl Viro 		return 0;
3278cefc107SDavid Howells 
32847b7fcaeSAl Viro 	push_page(pipe, page, offset, bytes);
329241699cdSAl Viro 	i->iov_offset = offset + bytes;
33047b7fcaeSAl Viro 	i->head = head;
331241699cdSAl Viro 	i->count -= bytes;
332241699cdSAl Viro 	return bytes;
333241699cdSAl Viro }
334241699cdSAl Viro 
335d879cb83SAl Viro /*
336a6294593SAndreas Gruenbacher  * fault_in_iov_iter_readable - fault in iov iterator for reading
337a6294593SAndreas Gruenbacher  * @i: iterator
338a6294593SAndreas Gruenbacher  * @size: maximum length
339171a0203SAnton Altaparmakov  *
340a6294593SAndreas Gruenbacher  * Fault in one or more iovecs of the given iov_iter, to a maximum length of
341a6294593SAndreas Gruenbacher  * @size.  For each iovec, fault in each page that constitutes the iovec.
342a6294593SAndreas Gruenbacher  *
343a6294593SAndreas Gruenbacher  * Returns the number of bytes not faulted in (like copy_to_user() and
344a6294593SAndreas Gruenbacher  * copy_from_user()).
345a6294593SAndreas Gruenbacher  *
346a6294593SAndreas Gruenbacher  * Always returns 0 for non-userspace iterators.
347171a0203SAnton Altaparmakov  */
348a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
349171a0203SAnton Altaparmakov {
350fcb14cb1SAl Viro 	if (iter_is_ubuf(i)) {
351fcb14cb1SAl Viro 		size_t n = min(size, iov_iter_count(i));
352fcb14cb1SAl Viro 		n -= fault_in_readable(i->ubuf + i->iov_offset, n);
353fcb14cb1SAl Viro 		return size - n;
354fcb14cb1SAl Viro 	} else if (iter_is_iovec(i)) {
355a6294593SAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
3568409a0d2SAl Viro 		const struct iovec *p;
3578409a0d2SAl Viro 		size_t skip;
3588409a0d2SAl Viro 
359a6294593SAndreas Gruenbacher 		size -= count;
360a6294593SAndreas Gruenbacher 		for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
361a6294593SAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
362a6294593SAndreas Gruenbacher 			size_t ret;
3638409a0d2SAl Viro 
3648409a0d2SAl Viro 			if (unlikely(!len))
3658409a0d2SAl Viro 				continue;
366a6294593SAndreas Gruenbacher 			ret = fault_in_readable(p->iov_base + skip, len);
367a6294593SAndreas Gruenbacher 			count -= len - ret;
368a6294593SAndreas Gruenbacher 			if (ret)
369a6294593SAndreas Gruenbacher 				break;
3708409a0d2SAl Viro 		}
371a6294593SAndreas Gruenbacher 		return count + size;
372171a0203SAnton Altaparmakov 	}
373171a0203SAnton Altaparmakov 	return 0;
374171a0203SAnton Altaparmakov }
375a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable);
376171a0203SAnton Altaparmakov 
377cdd591fcSAndreas Gruenbacher /*
378cdd591fcSAndreas Gruenbacher  * fault_in_iov_iter_writeable - fault in iov iterator for writing
379cdd591fcSAndreas Gruenbacher  * @i: iterator
380cdd591fcSAndreas Gruenbacher  * @size: maximum length
381cdd591fcSAndreas Gruenbacher  *
382cdd591fcSAndreas Gruenbacher  * Faults in the iterator using get_user_pages(), i.e., without triggering
383cdd591fcSAndreas Gruenbacher  * hardware page faults.  This is primarily useful when we already know that
384cdd591fcSAndreas Gruenbacher  * some or all of the pages in @i aren't in memory.
385cdd591fcSAndreas Gruenbacher  *
386cdd591fcSAndreas Gruenbacher  * Returns the number of bytes not faulted in, like copy_to_user() and
387cdd591fcSAndreas Gruenbacher  * copy_from_user().
388cdd591fcSAndreas Gruenbacher  *
389cdd591fcSAndreas Gruenbacher  * Always returns 0 for non-user-space iterators.
390cdd591fcSAndreas Gruenbacher  */
391cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
392cdd591fcSAndreas Gruenbacher {
393fcb14cb1SAl Viro 	if (iter_is_ubuf(i)) {
394fcb14cb1SAl Viro 		size_t n = min(size, iov_iter_count(i));
395fcb14cb1SAl Viro 		n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n);
396fcb14cb1SAl Viro 		return size - n;
397fcb14cb1SAl Viro 	} else if (iter_is_iovec(i)) {
398cdd591fcSAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
399cdd591fcSAndreas Gruenbacher 		const struct iovec *p;
400cdd591fcSAndreas Gruenbacher 		size_t skip;
401cdd591fcSAndreas Gruenbacher 
402cdd591fcSAndreas Gruenbacher 		size -= count;
403cdd591fcSAndreas Gruenbacher 		for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
404cdd591fcSAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
405cdd591fcSAndreas Gruenbacher 			size_t ret;
406cdd591fcSAndreas Gruenbacher 
407cdd591fcSAndreas Gruenbacher 			if (unlikely(!len))
408cdd591fcSAndreas Gruenbacher 				continue;
409cdd591fcSAndreas Gruenbacher 			ret = fault_in_safe_writeable(p->iov_base + skip, len);
410cdd591fcSAndreas Gruenbacher 			count -= len - ret;
411cdd591fcSAndreas Gruenbacher 			if (ret)
412cdd591fcSAndreas Gruenbacher 				break;
413cdd591fcSAndreas Gruenbacher 		}
414cdd591fcSAndreas Gruenbacher 		return count + size;
415cdd591fcSAndreas Gruenbacher 	}
416cdd591fcSAndreas Gruenbacher 	return 0;
417cdd591fcSAndreas Gruenbacher }
418cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable);
419cdd591fcSAndreas Gruenbacher 
420aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction,
421d879cb83SAl Viro 			const struct iovec *iov, unsigned long nr_segs,
422d879cb83SAl Viro 			size_t count)
423d879cb83SAl Viro {
424aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
4258cd54c1cSAl Viro 	*i = (struct iov_iter) {
4268cd54c1cSAl Viro 		.iter_type = ITER_IOVEC,
4273337ab08SAndreas Gruenbacher 		.nofault = false,
428fcb14cb1SAl Viro 		.user_backed = true,
4298cd54c1cSAl Viro 		.data_source = direction,
4308cd54c1cSAl Viro 		.iov = iov,
4318cd54c1cSAl Viro 		.nr_segs = nr_segs,
4328cd54c1cSAl Viro 		.iov_offset = 0,
4338cd54c1cSAl Viro 		.count = count
4348cd54c1cSAl Viro 	};
435d879cb83SAl Viro }
436d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init);
437d879cb83SAl Viro 
4388cefc107SDavid Howells static inline void data_start(const struct iov_iter *i,
4398cefc107SDavid Howells 			      unsigned int *iter_headp, size_t *offp)
440241699cdSAl Viro {
4418cefc107SDavid Howells 	unsigned int iter_head = i->head;
442241699cdSAl Viro 	size_t off = i->iov_offset;
4438cefc107SDavid Howells 
4442dcedb2aSAl Viro 	if (off && (!allocated(pipe_buf(i->pipe, iter_head)) ||
4458cefc107SDavid Howells 		    off == PAGE_SIZE)) {
4468cefc107SDavid Howells 		iter_head++;
447241699cdSAl Viro 		off = 0;
448241699cdSAl Viro 	}
4498cefc107SDavid Howells 	*iter_headp = iter_head;
450241699cdSAl Viro 	*offp = off;
451241699cdSAl Viro }
452241699cdSAl Viro 
453241699cdSAl Viro static size_t push_pipe(struct iov_iter *i, size_t size,
4548cefc107SDavid Howells 			int *iter_headp, size_t *offp)
455241699cdSAl Viro {
456241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
4578cefc107SDavid Howells 	unsigned int iter_head;
458241699cdSAl Viro 	size_t off;
459241699cdSAl Viro 	ssize_t left;
460241699cdSAl Viro 
461241699cdSAl Viro 	if (unlikely(size > i->count))
462241699cdSAl Viro 		size = i->count;
463241699cdSAl Viro 	if (unlikely(!size))
464241699cdSAl Viro 		return 0;
465241699cdSAl Viro 
466241699cdSAl Viro 	left = size;
4678cefc107SDavid Howells 	data_start(i, &iter_head, &off);
4688cefc107SDavid Howells 	*iter_headp = iter_head;
469241699cdSAl Viro 	*offp = off;
470241699cdSAl Viro 	if (off) {
47147b7fcaeSAl Viro 		struct pipe_buffer *buf = pipe_buf(pipe, iter_head);
47247b7fcaeSAl Viro 
473241699cdSAl Viro 		left -= PAGE_SIZE - off;
474241699cdSAl Viro 		if (left <= 0) {
47547b7fcaeSAl Viro 			buf->len += size;
476241699cdSAl Viro 			return size;
477241699cdSAl Viro 		}
47847b7fcaeSAl Viro 		buf->len = PAGE_SIZE;
479241699cdSAl Viro 	}
48047b7fcaeSAl Viro 	while (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
48147b7fcaeSAl Viro 		struct page *page = push_anon(pipe,
48247b7fcaeSAl Viro 					      min_t(ssize_t, left, PAGE_SIZE));
483241699cdSAl Viro 		if (!page)
484241699cdSAl Viro 			break;
4858cefc107SDavid Howells 
48647b7fcaeSAl Viro 		left -= PAGE_SIZE;
48747b7fcaeSAl Viro 		if (left <= 0)
488241699cdSAl Viro 			return size;
489241699cdSAl Viro 	}
490241699cdSAl Viro 	return size - left;
491241699cdSAl Viro }
492241699cdSAl Viro 
493241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
494241699cdSAl Viro 				struct iov_iter *i)
495241699cdSAl Viro {
496*8fad7767SAl Viro 	unsigned int off, chunk;
497*8fad7767SAl Viro 
498*8fad7767SAl Viro 	if (unlikely(bytes > i->count))
499*8fad7767SAl Viro 		bytes = i->count;
500*8fad7767SAl Viro 	if (unlikely(!bytes))
501*8fad7767SAl Viro 		return 0;
502241699cdSAl Viro 
503241699cdSAl Viro 	if (!sanity(i))
504241699cdSAl Viro 		return 0;
505241699cdSAl Viro 
506*8fad7767SAl Viro 	for (size_t n = bytes; n; n -= chunk) {
507*8fad7767SAl Viro 		struct page *page = append_pipe(i, n, &off);
508*8fad7767SAl Viro 		chunk = min_t(size_t, n, PAGE_SIZE - off);
509*8fad7767SAl Viro 		if (!page)
510*8fad7767SAl Viro 			return bytes - n;
511*8fad7767SAl Viro 		memcpy_to_page(page, off, addr, chunk);
512241699cdSAl Viro 		addr += chunk;
513*8fad7767SAl Viro 	}
514241699cdSAl Viro 	return bytes;
515241699cdSAl Viro }
516241699cdSAl Viro 
517f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
518f9152895SAl Viro 			      __wsum sum, size_t off)
519f9152895SAl Viro {
520cc44c17bSAl Viro 	__wsum next = csum_partial_copy_nocheck(from, to, len);
521f9152895SAl Viro 	return csum_block_add(sum, next, off);
522f9152895SAl Viro }
523f9152895SAl Viro 
52478e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
5256852df12SAl Viro 					 struct iov_iter *i, __wsum *sump)
52678e1f386SAl Viro {
5276852df12SAl Viro 	__wsum sum = *sump;
5286852df12SAl Viro 	size_t off = 0;
529*8fad7767SAl Viro 	unsigned int chunk, r;
530*8fad7767SAl Viro 
531*8fad7767SAl Viro 	if (unlikely(bytes > i->count))
532*8fad7767SAl Viro 		bytes = i->count;
533*8fad7767SAl Viro 	if (unlikely(!bytes))
534*8fad7767SAl Viro 		return 0;
53578e1f386SAl Viro 
53678e1f386SAl Viro 	if (!sanity(i))
53778e1f386SAl Viro 		return 0;
53878e1f386SAl Viro 
5396852df12SAl Viro 	while (bytes) {
540*8fad7767SAl Viro 		struct page *page = append_pipe(i, bytes, &r);
541*8fad7767SAl Viro 		char *p;
542*8fad7767SAl Viro 
543*8fad7767SAl Viro 		if (!page)
544*8fad7767SAl Viro 			break;
545*8fad7767SAl Viro 		chunk = min_t(size_t, bytes, PAGE_SIZE - r);
546*8fad7767SAl Viro 		p = kmap_local_page(page);
5476852df12SAl Viro 		sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
5482495bdccSAl Viro 		kunmap_local(p);
54978e1f386SAl Viro 		off += chunk;
550*8fad7767SAl Viro 		bytes -= chunk;
5516852df12SAl Viro 	}
5526852df12SAl Viro 	*sump = sum;
5536852df12SAl Viro 	return off;
55478e1f386SAl Viro }
55578e1f386SAl Viro 
556aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
557d879cb83SAl Viro {
55800e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
559241699cdSAl Viro 		return copy_pipe_to_iter(addr, bytes, i);
560fcb14cb1SAl Viro 	if (user_backed_iter(i))
56109fc68dcSAl Viro 		might_fault();
5627baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
5637baa5099SAl Viro 		copyout(base, addr + off, len),
5647baa5099SAl Viro 		memcpy(base, addr + off, len)
565d879cb83SAl Viro 	)
566d879cb83SAl Viro 
567d879cb83SAl Viro 	return bytes;
568d879cb83SAl Viro }
569aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter);
570d879cb83SAl Viro 
571ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC
572ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n)
5738780356eSDan Williams {
57496d4f267SLinus Torvalds 	if (access_ok(to, n)) {
575d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
576ec6347bbSDan Williams 		n = copy_mc_to_user((__force void *) to, from, n);
5778780356eSDan Williams 	}
5788780356eSDan Williams 	return n;
5798780356eSDan Williams }
5808780356eSDan Williams 
581ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
582ca146f6fSDan Williams 				struct iov_iter *i)
583ca146f6fSDan Williams {
584*8fad7767SAl Viro 	size_t xfer = 0;
585*8fad7767SAl Viro 	unsigned int off, chunk;
586*8fad7767SAl Viro 
587*8fad7767SAl Viro 	if (unlikely(bytes > i->count))
588*8fad7767SAl Viro 		bytes = i->count;
589*8fad7767SAl Viro 	if (unlikely(!bytes))
590*8fad7767SAl Viro 		return 0;
591ca146f6fSDan Williams 
592ca146f6fSDan Williams 	if (!sanity(i))
593ca146f6fSDan Williams 		return 0;
594ca146f6fSDan Williams 
595*8fad7767SAl Viro 	while (bytes) {
596*8fad7767SAl Viro 		struct page *page = append_pipe(i, bytes, &off);
597ca146f6fSDan Williams 		unsigned long rem;
598*8fad7767SAl Viro 		char *p;
599*8fad7767SAl Viro 
600*8fad7767SAl Viro 		if (!page)
601*8fad7767SAl Viro 			break;
602*8fad7767SAl Viro 		chunk = min_t(size_t, bytes, PAGE_SIZE - off);
603*8fad7767SAl Viro 		p = kmap_local_page(page);
6042a510a74SAl Viro 		rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
6052a510a74SAl Viro 		chunk -= rem;
6062a510a74SAl Viro 		kunmap_local(p);
6072a510a74SAl Viro 		xfer += chunk;
608*8fad7767SAl Viro 		bytes -= chunk;
609c3497fd0SAl Viro 		if (rem) {
610*8fad7767SAl Viro 			iov_iter_revert(i, rem);
611ca146f6fSDan Williams 			break;
612c3497fd0SAl Viro 		}
6132a510a74SAl Viro 	}
614ca146f6fSDan Williams 	return xfer;
615ca146f6fSDan Williams }
616ca146f6fSDan Williams 
617bf3eeb9bSDan Williams /**
618ec6347bbSDan Williams  * _copy_mc_to_iter - copy to iter with source memory error exception handling
619bf3eeb9bSDan Williams  * @addr: source kernel address
620bf3eeb9bSDan Williams  * @bytes: total transfer length
62144e55997SRandy Dunlap  * @i: destination iterator
622bf3eeb9bSDan Williams  *
623ec6347bbSDan Williams  * The pmem driver deploys this for the dax operation
624ec6347bbSDan Williams  * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
625ec6347bbSDan Williams  * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
626ec6347bbSDan Williams  * successfully copied.
627bf3eeb9bSDan Williams  *
628ec6347bbSDan Williams  * The main differences between this and typical _copy_to_iter().
629bf3eeb9bSDan Williams  *
630bf3eeb9bSDan Williams  * * Typical tail/residue handling after a fault retries the copy
631bf3eeb9bSDan Williams  *   byte-by-byte until the fault happens again. Re-triggering machine
632bf3eeb9bSDan Williams  *   checks is potentially fatal so the implementation uses source
633bf3eeb9bSDan Williams  *   alignment and poison alignment assumptions to avoid re-triggering
634bf3eeb9bSDan Williams  *   hardware exceptions.
635bf3eeb9bSDan Williams  *
636bf3eeb9bSDan Williams  * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
637bf3eeb9bSDan Williams  *   Compare to copy_to_iter() where only ITER_IOVEC attempts might return
638bf3eeb9bSDan Williams  *   a short copy.
63944e55997SRandy Dunlap  *
64044e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
641bf3eeb9bSDan Williams  */
642ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
6438780356eSDan Williams {
64400e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
645ec6347bbSDan Williams 		return copy_mc_pipe_to_iter(addr, bytes, i);
646fcb14cb1SAl Viro 	if (user_backed_iter(i))
6478780356eSDan Williams 		might_fault();
6487baa5099SAl Viro 	__iterate_and_advance(i, bytes, base, len, off,
6497baa5099SAl Viro 		copyout_mc(base, addr + off, len),
6507baa5099SAl Viro 		copy_mc_to_kernel(base, addr + off, len)
6518780356eSDan Williams 	)
6528780356eSDan Williams 
6538780356eSDan Williams 	return bytes;
6548780356eSDan Williams }
655ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
656ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */
6578780356eSDan Williams 
658aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
659d879cb83SAl Viro {
66000e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
661241699cdSAl Viro 		WARN_ON(1);
662241699cdSAl Viro 		return 0;
663241699cdSAl Viro 	}
664fcb14cb1SAl Viro 	if (user_backed_iter(i))
66509fc68dcSAl Viro 		might_fault();
6667baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6677baa5099SAl Viro 		copyin(addr + off, base, len),
6687baa5099SAl Viro 		memcpy(addr + off, base, len)
669d879cb83SAl Viro 	)
670d879cb83SAl Viro 
671d879cb83SAl Viro 	return bytes;
672d879cb83SAl Viro }
673aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter);
674d879cb83SAl Viro 
675aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
676d879cb83SAl Viro {
67700e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
678241699cdSAl Viro 		WARN_ON(1);
679241699cdSAl Viro 		return 0;
680241699cdSAl Viro 	}
6817baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6827baa5099SAl Viro 		__copy_from_user_inatomic_nocache(addr + off, base, len),
6837baa5099SAl Viro 		memcpy(addr + off, base, len)
684d879cb83SAl Viro 	)
685d879cb83SAl Viro 
686d879cb83SAl Viro 	return bytes;
687d879cb83SAl Viro }
688aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache);
689d879cb83SAl Viro 
6900aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
691abd08d7dSDan Williams /**
692abd08d7dSDan Williams  * _copy_from_iter_flushcache - write destination through cpu cache
693abd08d7dSDan Williams  * @addr: destination kernel address
694abd08d7dSDan Williams  * @bytes: total transfer length
69544e55997SRandy Dunlap  * @i: source iterator
696abd08d7dSDan Williams  *
697abd08d7dSDan Williams  * The pmem driver arranges for filesystem-dax to use this facility via
698abd08d7dSDan Williams  * dax_copy_from_iter() for ensuring that writes to persistent memory
699abd08d7dSDan Williams  * are flushed through the CPU cache. It is differentiated from
700abd08d7dSDan Williams  * _copy_from_iter_nocache() in that guarantees all data is flushed for
701abd08d7dSDan Williams  * all iterator types. The _copy_from_iter_nocache() only attempts to
702abd08d7dSDan Williams  * bypass the cache for the ITER_IOVEC case, and on some archs may use
703abd08d7dSDan Williams  * instructions that strand dirty-data in the cache.
70444e55997SRandy Dunlap  *
70544e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
706abd08d7dSDan Williams  */
7076a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
7080aed55afSDan Williams {
70900e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
7100aed55afSDan Williams 		WARN_ON(1);
7110aed55afSDan Williams 		return 0;
7120aed55afSDan Williams 	}
7137baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
7147baa5099SAl Viro 		__copy_from_user_flushcache(addr + off, base, len),
7157baa5099SAl Viro 		memcpy_flushcache(addr + off, base, len)
7160aed55afSDan Williams 	)
7170aed55afSDan Williams 
7180aed55afSDan Williams 	return bytes;
7190aed55afSDan Williams }
7206a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
7210aed55afSDan Williams #endif
7220aed55afSDan Williams 
72372e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
72472e809edSAl Viro {
7256daef95bSEric Dumazet 	struct page *head;
7266daef95bSEric Dumazet 	size_t v = n + offset;
7276daef95bSEric Dumazet 
7286daef95bSEric Dumazet 	/*
7296daef95bSEric Dumazet 	 * The general case needs to access the page order in order
7306daef95bSEric Dumazet 	 * to compute the page size.
7316daef95bSEric Dumazet 	 * However, we mostly deal with order-0 pages and thus can
7326daef95bSEric Dumazet 	 * avoid a possible cache line miss for requests that fit all
7336daef95bSEric Dumazet 	 * page orders.
7346daef95bSEric Dumazet 	 */
7356daef95bSEric Dumazet 	if (n <= v && v <= PAGE_SIZE)
7366daef95bSEric Dumazet 		return true;
7376daef95bSEric Dumazet 
7386daef95bSEric Dumazet 	head = compound_head(page);
7396daef95bSEric Dumazet 	v += (page - head) << PAGE_SHIFT;
740a90bcb86SPetar Penkov 
741a50b854eSMatthew Wilcox (Oracle) 	if (likely(n <= v && v <= (page_size(head))))
74272e809edSAl Viro 		return true;
74372e809edSAl Viro 	WARN_ON(1);
74472e809edSAl Viro 	return false;
74572e809edSAl Viro }
746cbbd26b8SAl Viro 
74708aa6479SAl Viro static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
748d879cb83SAl Viro 			 struct iov_iter *i)
749d879cb83SAl Viro {
75059bb69c6SAl Viro 	if (unlikely(iov_iter_is_pipe(i))) {
75159bb69c6SAl Viro 		return copy_page_to_iter_pipe(page, offset, bytes, i);
75259bb69c6SAl Viro 	} else {
753c1d4d6a9SAl Viro 		void *kaddr = kmap_local_page(page);
754c1d4d6a9SAl Viro 		size_t wanted = _copy_to_iter(kaddr + offset, bytes, i);
755c1d4d6a9SAl Viro 		kunmap_local(kaddr);
756d879cb83SAl Viro 		return wanted;
75728f38db7SAl Viro 	}
758d879cb83SAl Viro }
75908aa6479SAl Viro 
76008aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
76108aa6479SAl Viro 			 struct iov_iter *i)
76208aa6479SAl Viro {
76308aa6479SAl Viro 	size_t res = 0;
76408aa6479SAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes)))
76508aa6479SAl Viro 		return 0;
76608aa6479SAl Viro 	page += offset / PAGE_SIZE; // first subpage
76708aa6479SAl Viro 	offset %= PAGE_SIZE;
76808aa6479SAl Viro 	while (1) {
76908aa6479SAl Viro 		size_t n = __copy_page_to_iter(page, offset,
77008aa6479SAl Viro 				min(bytes, (size_t)PAGE_SIZE - offset), i);
77108aa6479SAl Viro 		res += n;
77208aa6479SAl Viro 		bytes -= n;
77308aa6479SAl Viro 		if (!bytes || !n)
77408aa6479SAl Viro 			break;
77508aa6479SAl Viro 		offset += n;
77608aa6479SAl Viro 		if (offset == PAGE_SIZE) {
77708aa6479SAl Viro 			page++;
77808aa6479SAl Viro 			offset = 0;
77908aa6479SAl Viro 		}
78008aa6479SAl Viro 	}
78108aa6479SAl Viro 	return res;
78208aa6479SAl Viro }
783d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter);
784d879cb83SAl Viro 
785d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
786d879cb83SAl Viro 			 struct iov_iter *i)
787d879cb83SAl Viro {
78859bb69c6SAl Viro 	if (page_copy_sane(page, offset, bytes)) {
78955ca375cSAl Viro 		void *kaddr = kmap_local_page(page);
790aa28de27SAl Viro 		size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
79155ca375cSAl Viro 		kunmap_local(kaddr);
792d879cb83SAl Viro 		return wanted;
79328f38db7SAl Viro 	}
79428f38db7SAl Viro 	return 0;
795d879cb83SAl Viro }
796d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter);
797d879cb83SAl Viro 
798241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i)
799241699cdSAl Viro {
800*8fad7767SAl Viro 	unsigned int chunk, off;
801*8fad7767SAl Viro 
802*8fad7767SAl Viro 	if (unlikely(bytes > i->count))
803*8fad7767SAl Viro 		bytes = i->count;
804*8fad7767SAl Viro 	if (unlikely(!bytes))
805*8fad7767SAl Viro 		return 0;
806241699cdSAl Viro 
807241699cdSAl Viro 	if (!sanity(i))
808241699cdSAl Viro 		return 0;
809241699cdSAl Viro 
810*8fad7767SAl Viro 	for (size_t n = bytes; n; n -= chunk) {
811*8fad7767SAl Viro 		struct page *page = append_pipe(i, n, &off);
812*8fad7767SAl Viro 		char *p;
813241699cdSAl Viro 
814*8fad7767SAl Viro 		if (!page)
815*8fad7767SAl Viro 			return bytes - n;
816*8fad7767SAl Viro 		chunk = min_t(size_t, n, PAGE_SIZE - off);
817*8fad7767SAl Viro 		p = kmap_local_page(page);
818893839fdSAl Viro 		memset(p + off, 0, chunk);
819893839fdSAl Viro 		kunmap_local(p);
820*8fad7767SAl Viro 	}
821241699cdSAl Viro 	return bytes;
822241699cdSAl Viro }
823241699cdSAl Viro 
824d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
825d879cb83SAl Viro {
82600e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
827241699cdSAl Viro 		return pipe_zero(bytes, i);
8287baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, count,
8297baa5099SAl Viro 		clear_user(base, len),
8307baa5099SAl Viro 		memset(base, 0, len)
831d879cb83SAl Viro 	)
832d879cb83SAl Viro 
833d879cb83SAl Viro 	return bytes;
834d879cb83SAl Viro }
835d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero);
836d879cb83SAl Viro 
837f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
838f0b65f39SAl Viro 				  struct iov_iter *i)
839d879cb83SAl Viro {
840d879cb83SAl Viro 	char *kaddr = kmap_atomic(page), *p = kaddr + offset;
84172e809edSAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes))) {
84272e809edSAl Viro 		kunmap_atomic(kaddr);
84372e809edSAl Viro 		return 0;
84472e809edSAl Viro 	}
8459ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
846241699cdSAl Viro 		kunmap_atomic(kaddr);
847241699cdSAl Viro 		WARN_ON(1);
848241699cdSAl Viro 		return 0;
849241699cdSAl Viro 	}
8507baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
8517baa5099SAl Viro 		copyin(p + off, base, len),
8527baa5099SAl Viro 		memcpy(p + off, base, len)
853d879cb83SAl Viro 	)
854d879cb83SAl Viro 	kunmap_atomic(kaddr);
855d879cb83SAl Viro 	return bytes;
856d879cb83SAl Viro }
857f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic);
858d879cb83SAl Viro 
859b9dc6f65SAl Viro static inline void pipe_truncate(struct iov_iter *i)
860241699cdSAl Viro {
861241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
8628cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
8638cefc107SDavid Howells 	unsigned int p_head = pipe->head;
8648cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
8658cefc107SDavid Howells 
8668cefc107SDavid Howells 	if (!pipe_empty(p_head, p_tail)) {
8678cefc107SDavid Howells 		struct pipe_buffer *buf;
8688cefc107SDavid Howells 		unsigned int i_head = i->head;
869b9dc6f65SAl Viro 		size_t off = i->iov_offset;
8708cefc107SDavid Howells 
871b9dc6f65SAl Viro 		if (off) {
8728cefc107SDavid Howells 			buf = &pipe->bufs[i_head & p_mask];
8738cefc107SDavid Howells 			buf->len = off - buf->offset;
8748cefc107SDavid Howells 			i_head++;
875b9dc6f65SAl Viro 		}
8768cefc107SDavid Howells 		while (p_head != i_head) {
8778cefc107SDavid Howells 			p_head--;
8788cefc107SDavid Howells 			pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
879241699cdSAl Viro 		}
8808cefc107SDavid Howells 
8818cefc107SDavid Howells 		pipe->head = p_head;
882241699cdSAl Viro 	}
883b9dc6f65SAl Viro }
884b9dc6f65SAl Viro 
885b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size)
886b9dc6f65SAl Viro {
887b9dc6f65SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
888b9dc6f65SAl Viro 	if (size) {
889b9dc6f65SAl Viro 		struct pipe_buffer *buf;
8908cefc107SDavid Howells 		unsigned int p_mask = pipe->ring_size - 1;
8918cefc107SDavid Howells 		unsigned int i_head = i->head;
892b9dc6f65SAl Viro 		size_t off = i->iov_offset, left = size;
8938cefc107SDavid Howells 
894b9dc6f65SAl Viro 		if (off) /* make it relative to the beginning of buffer */
8958cefc107SDavid Howells 			left += off - pipe->bufs[i_head & p_mask].offset;
896b9dc6f65SAl Viro 		while (1) {
8978cefc107SDavid Howells 			buf = &pipe->bufs[i_head & p_mask];
898b9dc6f65SAl Viro 			if (left <= buf->len)
899b9dc6f65SAl Viro 				break;
900b9dc6f65SAl Viro 			left -= buf->len;
9018cefc107SDavid Howells 			i_head++;
902b9dc6f65SAl Viro 		}
9038cefc107SDavid Howells 		i->head = i_head;
904b9dc6f65SAl Viro 		i->iov_offset = buf->offset + left;
905b9dc6f65SAl Viro 	}
906b9dc6f65SAl Viro 	i->count -= size;
907b9dc6f65SAl Viro 	/* ... and discard everything past that point */
908b9dc6f65SAl Viro 	pipe_truncate(i);
909241699cdSAl Viro }
910241699cdSAl Viro 
91154c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
91254c8195bSPavel Begunkov {
91318fa9af7SAl Viro 	const struct bio_vec *bvec, *end;
91454c8195bSPavel Begunkov 
91518fa9af7SAl Viro 	if (!i->count)
91618fa9af7SAl Viro 		return;
91718fa9af7SAl Viro 	i->count -= size;
91854c8195bSPavel Begunkov 
91918fa9af7SAl Viro 	size += i->iov_offset;
92018fa9af7SAl Viro 
92118fa9af7SAl Viro 	for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
92218fa9af7SAl Viro 		if (likely(size < bvec->bv_len))
92318fa9af7SAl Viro 			break;
92418fa9af7SAl Viro 		size -= bvec->bv_len;
92518fa9af7SAl Viro 	}
92618fa9af7SAl Viro 	i->iov_offset = size;
92718fa9af7SAl Viro 	i->nr_segs -= bvec - i->bvec;
92818fa9af7SAl Viro 	i->bvec = bvec;
92954c8195bSPavel Begunkov }
93054c8195bSPavel Begunkov 
931185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
932185ac4d4SAl Viro {
933185ac4d4SAl Viro 	const struct iovec *iov, *end;
934185ac4d4SAl Viro 
935185ac4d4SAl Viro 	if (!i->count)
936185ac4d4SAl Viro 		return;
937185ac4d4SAl Viro 	i->count -= size;
938185ac4d4SAl Viro 
939185ac4d4SAl Viro 	size += i->iov_offset; // from beginning of current segment
940185ac4d4SAl Viro 	for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
941185ac4d4SAl Viro 		if (likely(size < iov->iov_len))
942185ac4d4SAl Viro 			break;
943185ac4d4SAl Viro 		size -= iov->iov_len;
944185ac4d4SAl Viro 	}
945185ac4d4SAl Viro 	i->iov_offset = size;
946185ac4d4SAl Viro 	i->nr_segs -= iov - i->iov;
947185ac4d4SAl Viro 	i->iov = iov;
948185ac4d4SAl Viro }
949185ac4d4SAl Viro 
950d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size)
951d879cb83SAl Viro {
9523b3fc051SAl Viro 	if (unlikely(i->count < size))
9533b3fc051SAl Viro 		size = i->count;
954fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
955fcb14cb1SAl Viro 		i->iov_offset += size;
956fcb14cb1SAl Viro 		i->count -= size;
957fcb14cb1SAl Viro 	} else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
958185ac4d4SAl Viro 		/* iovec and kvec have identical layouts */
959185ac4d4SAl Viro 		iov_iter_iovec_advance(i, size);
960185ac4d4SAl Viro 	} else if (iov_iter_is_bvec(i)) {
961185ac4d4SAl Viro 		iov_iter_bvec_advance(i, size);
962185ac4d4SAl Viro 	} else if (iov_iter_is_pipe(i)) {
963241699cdSAl Viro 		pipe_advance(i, size);
964185ac4d4SAl Viro 	} else if (iov_iter_is_discard(i)) {
965185ac4d4SAl Viro 		i->count -= size;
9667ff50620SDavid Howells 	}
967d879cb83SAl Viro }
968d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance);
969d879cb83SAl Viro 
97027c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll)
97127c0e374SAl Viro {
97227c0e374SAl Viro 	if (!unroll)
97327c0e374SAl Viro 		return;
9745b47d59aSAl Viro 	if (WARN_ON(unroll > MAX_RW_COUNT))
9755b47d59aSAl Viro 		return;
97627c0e374SAl Viro 	i->count += unroll;
97700e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
97827c0e374SAl Viro 		struct pipe_inode_info *pipe = i->pipe;
9798cefc107SDavid Howells 		unsigned int p_mask = pipe->ring_size - 1;
9808cefc107SDavid Howells 		unsigned int i_head = i->head;
98127c0e374SAl Viro 		size_t off = i->iov_offset;
98227c0e374SAl Viro 		while (1) {
9838cefc107SDavid Howells 			struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
9848cefc107SDavid Howells 			size_t n = off - b->offset;
98527c0e374SAl Viro 			if (unroll < n) {
9864fa55cefSAl Viro 				off -= unroll;
98727c0e374SAl Viro 				break;
98827c0e374SAl Viro 			}
98927c0e374SAl Viro 			unroll -= n;
9908cefc107SDavid Howells 			if (!unroll && i_head == i->start_head) {
99127c0e374SAl Viro 				off = 0;
99227c0e374SAl Viro 				break;
99327c0e374SAl Viro 			}
9948cefc107SDavid Howells 			i_head--;
9958cefc107SDavid Howells 			b = &pipe->bufs[i_head & p_mask];
9968cefc107SDavid Howells 			off = b->offset + b->len;
99727c0e374SAl Viro 		}
99827c0e374SAl Viro 		i->iov_offset = off;
9998cefc107SDavid Howells 		i->head = i_head;
100027c0e374SAl Viro 		pipe_truncate(i);
100127c0e374SAl Viro 		return;
100227c0e374SAl Viro 	}
10039ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_discard(i)))
10049ea9ce04SDavid Howells 		return;
100527c0e374SAl Viro 	if (unroll <= i->iov_offset) {
100627c0e374SAl Viro 		i->iov_offset -= unroll;
100727c0e374SAl Viro 		return;
100827c0e374SAl Viro 	}
100927c0e374SAl Viro 	unroll -= i->iov_offset;
1010fcb14cb1SAl Viro 	if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
10117ff50620SDavid Howells 		BUG(); /* We should never go beyond the start of the specified
10127ff50620SDavid Howells 			* range since we might then be straying into pages that
10137ff50620SDavid Howells 			* aren't pinned.
10147ff50620SDavid Howells 			*/
10157ff50620SDavid Howells 	} else if (iov_iter_is_bvec(i)) {
101627c0e374SAl Viro 		const struct bio_vec *bvec = i->bvec;
101727c0e374SAl Viro 		while (1) {
101827c0e374SAl Viro 			size_t n = (--bvec)->bv_len;
101927c0e374SAl Viro 			i->nr_segs++;
102027c0e374SAl Viro 			if (unroll <= n) {
102127c0e374SAl Viro 				i->bvec = bvec;
102227c0e374SAl Viro 				i->iov_offset = n - unroll;
102327c0e374SAl Viro 				return;
102427c0e374SAl Viro 			}
102527c0e374SAl Viro 			unroll -= n;
102627c0e374SAl Viro 		}
102727c0e374SAl Viro 	} else { /* same logics for iovec and kvec */
102827c0e374SAl Viro 		const struct iovec *iov = i->iov;
102927c0e374SAl Viro 		while (1) {
103027c0e374SAl Viro 			size_t n = (--iov)->iov_len;
103127c0e374SAl Viro 			i->nr_segs++;
103227c0e374SAl Viro 			if (unroll <= n) {
103327c0e374SAl Viro 				i->iov = iov;
103427c0e374SAl Viro 				i->iov_offset = n - unroll;
103527c0e374SAl Viro 				return;
103627c0e374SAl Viro 			}
103727c0e374SAl Viro 			unroll -= n;
103827c0e374SAl Viro 		}
103927c0e374SAl Viro 	}
104027c0e374SAl Viro }
104127c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert);
104227c0e374SAl Viro 
1043d879cb83SAl Viro /*
1044d879cb83SAl Viro  * Return the count of just the current iov_iter segment.
1045d879cb83SAl Viro  */
1046d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i)
1047d879cb83SAl Viro {
104828f38db7SAl Viro 	if (i->nr_segs > 1) {
104928f38db7SAl Viro 		if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
105028f38db7SAl Viro 			return min(i->count, i->iov->iov_len - i->iov_offset);
10517ff50620SDavid Howells 		if (iov_iter_is_bvec(i))
1052d879cb83SAl Viro 			return min(i->count, i->bvec->bv_len - i->iov_offset);
105328f38db7SAl Viro 	}
105428f38db7SAl Viro 	return i->count;
1055d879cb83SAl Viro }
1056d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count);
1057d879cb83SAl Viro 
1058aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
1059d879cb83SAl Viro 			const struct kvec *kvec, unsigned long nr_segs,
1060d879cb83SAl Viro 			size_t count)
1061d879cb83SAl Viro {
1062aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
10638cd54c1cSAl Viro 	*i = (struct iov_iter){
10648cd54c1cSAl Viro 		.iter_type = ITER_KVEC,
10658cd54c1cSAl Viro 		.data_source = direction,
10668cd54c1cSAl Viro 		.kvec = kvec,
10678cd54c1cSAl Viro 		.nr_segs = nr_segs,
10688cd54c1cSAl Viro 		.iov_offset = 0,
10698cd54c1cSAl Viro 		.count = count
10708cd54c1cSAl Viro 	};
1071d879cb83SAl Viro }
1072d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec);
1073d879cb83SAl Viro 
1074aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1075d879cb83SAl Viro 			const struct bio_vec *bvec, unsigned long nr_segs,
1076d879cb83SAl Viro 			size_t count)
1077d879cb83SAl Viro {
1078aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
10798cd54c1cSAl Viro 	*i = (struct iov_iter){
10808cd54c1cSAl Viro 		.iter_type = ITER_BVEC,
10818cd54c1cSAl Viro 		.data_source = direction,
10828cd54c1cSAl Viro 		.bvec = bvec,
10838cd54c1cSAl Viro 		.nr_segs = nr_segs,
10848cd54c1cSAl Viro 		.iov_offset = 0,
10858cd54c1cSAl Viro 		.count = count
10868cd54c1cSAl Viro 	};
1087d879cb83SAl Viro }
1088d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec);
1089d879cb83SAl Viro 
1090aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1091241699cdSAl Viro 			struct pipe_inode_info *pipe,
1092241699cdSAl Viro 			size_t count)
1093241699cdSAl Viro {
1094aa563d7bSDavid Howells 	BUG_ON(direction != READ);
10958cefc107SDavid Howells 	WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
10968cd54c1cSAl Viro 	*i = (struct iov_iter){
10978cd54c1cSAl Viro 		.iter_type = ITER_PIPE,
10988cd54c1cSAl Viro 		.data_source = false,
10998cd54c1cSAl Viro 		.pipe = pipe,
11008cd54c1cSAl Viro 		.head = pipe->head,
11018cd54c1cSAl Viro 		.start_head = pipe->head,
11028cd54c1cSAl Viro 		.iov_offset = 0,
11038cd54c1cSAl Viro 		.count = count
11048cd54c1cSAl Viro 	};
1105241699cdSAl Viro }
1106241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe);
1107241699cdSAl Viro 
11089ea9ce04SDavid Howells /**
11097ff50620SDavid Howells  * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
11107ff50620SDavid Howells  * @i: The iterator to initialise.
11117ff50620SDavid Howells  * @direction: The direction of the transfer.
11127ff50620SDavid Howells  * @xarray: The xarray to access.
11137ff50620SDavid Howells  * @start: The start file position.
11147ff50620SDavid Howells  * @count: The size of the I/O buffer in bytes.
11157ff50620SDavid Howells  *
11167ff50620SDavid Howells  * Set up an I/O iterator to either draw data out of the pages attached to an
11177ff50620SDavid Howells  * inode or to inject data into those pages.  The pages *must* be prevented
11187ff50620SDavid Howells  * from evaporation, either by taking a ref on them or locking them by the
11197ff50620SDavid Howells  * caller.
11207ff50620SDavid Howells  */
11217ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
11227ff50620SDavid Howells 		     struct xarray *xarray, loff_t start, size_t count)
11237ff50620SDavid Howells {
11247ff50620SDavid Howells 	BUG_ON(direction & ~1);
11258cd54c1cSAl Viro 	*i = (struct iov_iter) {
11268cd54c1cSAl Viro 		.iter_type = ITER_XARRAY,
11278cd54c1cSAl Viro 		.data_source = direction,
11288cd54c1cSAl Viro 		.xarray = xarray,
11298cd54c1cSAl Viro 		.xarray_start = start,
11308cd54c1cSAl Viro 		.count = count,
11318cd54c1cSAl Viro 		.iov_offset = 0
11328cd54c1cSAl Viro 	};
11337ff50620SDavid Howells }
11347ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray);
11357ff50620SDavid Howells 
11367ff50620SDavid Howells /**
11379ea9ce04SDavid Howells  * iov_iter_discard - Initialise an I/O iterator that discards data
11389ea9ce04SDavid Howells  * @i: The iterator to initialise.
11399ea9ce04SDavid Howells  * @direction: The direction of the transfer.
11409ea9ce04SDavid Howells  * @count: The size of the I/O buffer in bytes.
11419ea9ce04SDavid Howells  *
11429ea9ce04SDavid Howells  * Set up an I/O iterator that just discards everything that's written to it.
11439ea9ce04SDavid Howells  * It's only available as a READ iterator.
11449ea9ce04SDavid Howells  */
11459ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
11469ea9ce04SDavid Howells {
11479ea9ce04SDavid Howells 	BUG_ON(direction != READ);
11488cd54c1cSAl Viro 	*i = (struct iov_iter){
11498cd54c1cSAl Viro 		.iter_type = ITER_DISCARD,
11508cd54c1cSAl Viro 		.data_source = false,
11518cd54c1cSAl Viro 		.count = count,
11528cd54c1cSAl Viro 		.iov_offset = 0
11538cd54c1cSAl Viro 	};
11549ea9ce04SDavid Howells }
11559ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard);
11569ea9ce04SDavid Howells 
1157cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
1158cfa320f7SKeith Busch 				   unsigned len_mask)
1159cfa320f7SKeith Busch {
1160cfa320f7SKeith Busch 	size_t size = i->count;
1161cfa320f7SKeith Busch 	size_t skip = i->iov_offset;
1162cfa320f7SKeith Busch 	unsigned k;
1163cfa320f7SKeith Busch 
1164cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
1165cfa320f7SKeith Busch 		size_t len = i->iov[k].iov_len - skip;
1166cfa320f7SKeith Busch 
1167cfa320f7SKeith Busch 		if (len > size)
1168cfa320f7SKeith Busch 			len = size;
1169cfa320f7SKeith Busch 		if (len & len_mask)
1170cfa320f7SKeith Busch 			return false;
1171cfa320f7SKeith Busch 		if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask)
1172cfa320f7SKeith Busch 			return false;
1173cfa320f7SKeith Busch 
1174cfa320f7SKeith Busch 		size -= len;
1175cfa320f7SKeith Busch 		if (!size)
1176cfa320f7SKeith Busch 			break;
1177cfa320f7SKeith Busch 	}
1178cfa320f7SKeith Busch 	return true;
1179cfa320f7SKeith Busch }
1180cfa320f7SKeith Busch 
1181cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
1182cfa320f7SKeith Busch 				  unsigned len_mask)
1183cfa320f7SKeith Busch {
1184cfa320f7SKeith Busch 	size_t size = i->count;
1185cfa320f7SKeith Busch 	unsigned skip = i->iov_offset;
1186cfa320f7SKeith Busch 	unsigned k;
1187cfa320f7SKeith Busch 
1188cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
1189cfa320f7SKeith Busch 		size_t len = i->bvec[k].bv_len - skip;
1190cfa320f7SKeith Busch 
1191cfa320f7SKeith Busch 		if (len > size)
1192cfa320f7SKeith Busch 			len = size;
1193cfa320f7SKeith Busch 		if (len & len_mask)
1194cfa320f7SKeith Busch 			return false;
1195cfa320f7SKeith Busch 		if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
1196cfa320f7SKeith Busch 			return false;
1197cfa320f7SKeith Busch 
1198cfa320f7SKeith Busch 		size -= len;
1199cfa320f7SKeith Busch 		if (!size)
1200cfa320f7SKeith Busch 			break;
1201cfa320f7SKeith Busch 	}
1202cfa320f7SKeith Busch 	return true;
1203cfa320f7SKeith Busch }
1204cfa320f7SKeith Busch 
1205cfa320f7SKeith Busch /**
1206cfa320f7SKeith Busch  * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
1207cfa320f7SKeith Busch  * 	are aligned to the parameters.
1208cfa320f7SKeith Busch  *
1209cfa320f7SKeith Busch  * @i: &struct iov_iter to restore
1210cfa320f7SKeith Busch  * @addr_mask: bit mask to check against the iov element's addresses
1211cfa320f7SKeith Busch  * @len_mask: bit mask to check against the iov element's lengths
1212cfa320f7SKeith Busch  *
1213cfa320f7SKeith Busch  * Return: false if any addresses or lengths intersect with the provided masks
1214cfa320f7SKeith Busch  */
1215cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
1216cfa320f7SKeith Busch 			 unsigned len_mask)
1217cfa320f7SKeith Busch {
1218fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1219fcb14cb1SAl Viro 		if (i->count & len_mask)
1220fcb14cb1SAl Viro 			return false;
1221fcb14cb1SAl Viro 		if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
1222fcb14cb1SAl Viro 			return false;
1223fcb14cb1SAl Viro 		return true;
1224fcb14cb1SAl Viro 	}
1225fcb14cb1SAl Viro 
1226cfa320f7SKeith Busch 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1227cfa320f7SKeith Busch 		return iov_iter_aligned_iovec(i, addr_mask, len_mask);
1228cfa320f7SKeith Busch 
1229cfa320f7SKeith Busch 	if (iov_iter_is_bvec(i))
1230cfa320f7SKeith Busch 		return iov_iter_aligned_bvec(i, addr_mask, len_mask);
1231cfa320f7SKeith Busch 
1232cfa320f7SKeith Busch 	if (iov_iter_is_pipe(i)) {
1233cfa320f7SKeith Busch 		unsigned int p_mask = i->pipe->ring_size - 1;
1234cfa320f7SKeith Busch 		size_t size = i->count;
1235cfa320f7SKeith Busch 
1236cfa320f7SKeith Busch 		if (size & len_mask)
1237cfa320f7SKeith Busch 			return false;
1238cfa320f7SKeith Busch 		if (size && allocated(&i->pipe->bufs[i->head & p_mask])) {
1239cfa320f7SKeith Busch 			if (i->iov_offset & addr_mask)
1240cfa320f7SKeith Busch 				return false;
1241cfa320f7SKeith Busch 		}
1242cfa320f7SKeith Busch 
1243cfa320f7SKeith Busch 		return true;
1244cfa320f7SKeith Busch 	}
1245cfa320f7SKeith Busch 
1246cfa320f7SKeith Busch 	if (iov_iter_is_xarray(i)) {
1247cfa320f7SKeith Busch 		if (i->count & len_mask)
1248cfa320f7SKeith Busch 			return false;
1249cfa320f7SKeith Busch 		if ((i->xarray_start + i->iov_offset) & addr_mask)
1250cfa320f7SKeith Busch 			return false;
1251cfa320f7SKeith Busch 	}
1252cfa320f7SKeith Busch 
1253cfa320f7SKeith Busch 	return true;
1254cfa320f7SKeith Busch }
1255cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
1256cfa320f7SKeith Busch 
12579221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1258d879cb83SAl Viro {
1259d879cb83SAl Viro 	unsigned long res = 0;
1260d879cb83SAl Viro 	size_t size = i->count;
12619221d2e3SAl Viro 	size_t skip = i->iov_offset;
12629221d2e3SAl Viro 	unsigned k;
1263d879cb83SAl Viro 
12649221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
12659221d2e3SAl Viro 		size_t len = i->iov[k].iov_len - skip;
12669221d2e3SAl Viro 		if (len) {
12679221d2e3SAl Viro 			res |= (unsigned long)i->iov[k].iov_base + skip;
12689221d2e3SAl Viro 			if (len > size)
12699221d2e3SAl Viro 				len = size;
12709221d2e3SAl Viro 			res |= len;
12719221d2e3SAl Viro 			size -= len;
12729221d2e3SAl Viro 			if (!size)
12739221d2e3SAl Viro 				break;
12749221d2e3SAl Viro 		}
12759221d2e3SAl Viro 	}
12769221d2e3SAl Viro 	return res;
12779221d2e3SAl Viro }
12789221d2e3SAl Viro 
12799221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
12809221d2e3SAl Viro {
12819221d2e3SAl Viro 	unsigned res = 0;
12829221d2e3SAl Viro 	size_t size = i->count;
12839221d2e3SAl Viro 	unsigned skip = i->iov_offset;
12849221d2e3SAl Viro 	unsigned k;
12859221d2e3SAl Viro 
12869221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
12879221d2e3SAl Viro 		size_t len = i->bvec[k].bv_len - skip;
12889221d2e3SAl Viro 		res |= (unsigned long)i->bvec[k].bv_offset + skip;
12899221d2e3SAl Viro 		if (len > size)
12909221d2e3SAl Viro 			len = size;
12919221d2e3SAl Viro 		res |= len;
12929221d2e3SAl Viro 		size -= len;
12939221d2e3SAl Viro 		if (!size)
12949221d2e3SAl Viro 			break;
12959221d2e3SAl Viro 	}
12969221d2e3SAl Viro 	return res;
12979221d2e3SAl Viro }
12989221d2e3SAl Viro 
12999221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i)
13009221d2e3SAl Viro {
1301fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1302fcb14cb1SAl Viro 		size_t size = i->count;
1303fcb14cb1SAl Viro 		if (size)
1304fcb14cb1SAl Viro 			return ((unsigned long)i->ubuf + i->iov_offset) | size;
1305fcb14cb1SAl Viro 		return 0;
1306fcb14cb1SAl Viro 	}
1307fcb14cb1SAl Viro 
13089221d2e3SAl Viro 	/* iovec and kvec have identical layouts */
13099221d2e3SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
13109221d2e3SAl Viro 		return iov_iter_alignment_iovec(i);
13119221d2e3SAl Viro 
13129221d2e3SAl Viro 	if (iov_iter_is_bvec(i))
13139221d2e3SAl Viro 		return iov_iter_alignment_bvec(i);
13149221d2e3SAl Viro 
13159221d2e3SAl Viro 	if (iov_iter_is_pipe(i)) {
13169221d2e3SAl Viro 		size_t size = i->count;
1317e0ff126eSJan Kara 
13182dcedb2aSAl Viro 		if (size && i->iov_offset && allocated(pipe_buf(i->pipe, i->head)))
1319241699cdSAl Viro 			return size | i->iov_offset;
1320241699cdSAl Viro 		return size;
1321241699cdSAl Viro 	}
13229221d2e3SAl Viro 
13239221d2e3SAl Viro 	if (iov_iter_is_xarray(i))
13243d14ec1fSDavid Howells 		return (i->xarray_start + i->iov_offset) | i->count;
13259221d2e3SAl Viro 
13269221d2e3SAl Viro 	return 0;
1327d879cb83SAl Viro }
1328d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment);
1329d879cb83SAl Viro 
1330357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1331357f435dSAl Viro {
1332357f435dSAl Viro 	unsigned long res = 0;
1333610c7a71SAl Viro 	unsigned long v = 0;
1334357f435dSAl Viro 	size_t size = i->count;
1335610c7a71SAl Viro 	unsigned k;
1336357f435dSAl Viro 
1337fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1338fcb14cb1SAl Viro 		return 0;
1339fcb14cb1SAl Viro 
1340610c7a71SAl Viro 	if (WARN_ON(!iter_is_iovec(i)))
1341241699cdSAl Viro 		return ~0U;
1342241699cdSAl Viro 
1343610c7a71SAl Viro 	for (k = 0; k < i->nr_segs; k++) {
1344610c7a71SAl Viro 		if (i->iov[k].iov_len) {
1345610c7a71SAl Viro 			unsigned long base = (unsigned long)i->iov[k].iov_base;
1346610c7a71SAl Viro 			if (v) // if not the first one
1347610c7a71SAl Viro 				res |= base | v; // this start | previous end
1348610c7a71SAl Viro 			v = base + i->iov[k].iov_len;
1349610c7a71SAl Viro 			if (size <= i->iov[k].iov_len)
1350610c7a71SAl Viro 				break;
1351610c7a71SAl Viro 			size -= i->iov[k].iov_len;
1352610c7a71SAl Viro 		}
1353610c7a71SAl Viro 	}
1354357f435dSAl Viro 	return res;
1355357f435dSAl Viro }
1356357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment);
1357357f435dSAl Viro 
1358e76b6312SIlya Dryomov static inline ssize_t __pipe_get_pages(struct iov_iter *i,
1359241699cdSAl Viro 				size_t maxsize,
1360241699cdSAl Viro 				struct page **pages,
13618cefc107SDavid Howells 				int iter_head,
1362241699cdSAl Viro 				size_t *start)
1363241699cdSAl Viro {
1364241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
13658cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
13668cefc107SDavid Howells 	ssize_t n = push_pipe(i, maxsize, &iter_head, start);
1367241699cdSAl Viro 	if (!n)
1368241699cdSAl Viro 		return -EFAULT;
1369241699cdSAl Viro 
1370241699cdSAl Viro 	maxsize = n;
1371241699cdSAl Viro 	n += *start;
13721689c73aSAl Viro 	while (n > 0) {
13738cefc107SDavid Howells 		get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
13748cefc107SDavid Howells 		iter_head++;
1375241699cdSAl Viro 		n -= PAGE_SIZE;
1376241699cdSAl Viro 	}
1377241699cdSAl Viro 
1378241699cdSAl Viro 	return maxsize;
1379241699cdSAl Viro }
1380241699cdSAl Viro 
1381241699cdSAl Viro static ssize_t pipe_get_pages(struct iov_iter *i,
1382241699cdSAl Viro 		   struct page **pages, size_t maxsize, unsigned maxpages,
1383241699cdSAl Viro 		   size_t *start)
1384241699cdSAl Viro {
13858cefc107SDavid Howells 	unsigned int iter_head, npages;
1386241699cdSAl Viro 	size_t capacity;
1387241699cdSAl Viro 
1388241699cdSAl Viro 	if (!sanity(i))
1389241699cdSAl Viro 		return -EFAULT;
1390241699cdSAl Viro 
13918cefc107SDavid Howells 	data_start(i, &iter_head, start);
13928cefc107SDavid Howells 	/* Amount of free space: some of this one + all after this one */
13938cefc107SDavid Howells 	npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1394241699cdSAl Viro 	capacity = min(npages, maxpages) * PAGE_SIZE - *start;
1395241699cdSAl Viro 
13968cefc107SDavid Howells 	return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
1397241699cdSAl Viro }
1398241699cdSAl Viro 
13997ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
14007ff50620SDavid Howells 					  pgoff_t index, unsigned int nr_pages)
14017ff50620SDavid Howells {
14027ff50620SDavid Howells 	XA_STATE(xas, xa, index);
14037ff50620SDavid Howells 	struct page *page;
14047ff50620SDavid Howells 	unsigned int ret = 0;
14057ff50620SDavid Howells 
14067ff50620SDavid Howells 	rcu_read_lock();
14077ff50620SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
14087ff50620SDavid Howells 		if (xas_retry(&xas, page))
14097ff50620SDavid Howells 			continue;
14107ff50620SDavid Howells 
14117ff50620SDavid Howells 		/* Has the page moved or been split? */
14127ff50620SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
14137ff50620SDavid Howells 			xas_reset(&xas);
14147ff50620SDavid Howells 			continue;
14157ff50620SDavid Howells 		}
14167ff50620SDavid Howells 
14177ff50620SDavid Howells 		pages[ret] = find_subpage(page, xas.xa_index);
14187ff50620SDavid Howells 		get_page(pages[ret]);
14197ff50620SDavid Howells 		if (++ret == nr_pages)
14207ff50620SDavid Howells 			break;
14217ff50620SDavid Howells 	}
14227ff50620SDavid Howells 	rcu_read_unlock();
14237ff50620SDavid Howells 	return ret;
14247ff50620SDavid Howells }
14257ff50620SDavid Howells 
14267ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i,
14277ff50620SDavid Howells 				     struct page **pages, size_t maxsize,
14287ff50620SDavid Howells 				     unsigned maxpages, size_t *_start_offset)
14297ff50620SDavid Howells {
14307ff50620SDavid Howells 	unsigned nr, offset;
14317ff50620SDavid Howells 	pgoff_t index, count;
14326c776766SDavid Howells 	size_t size = maxsize;
14337ff50620SDavid Howells 	loff_t pos;
14347ff50620SDavid Howells 
14357ff50620SDavid Howells 	if (!size || !maxpages)
14367ff50620SDavid Howells 		return 0;
14377ff50620SDavid Howells 
14387ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
14397ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
14407ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
14417ff50620SDavid Howells 	*_start_offset = offset;
14427ff50620SDavid Howells 
14437ff50620SDavid Howells 	count = 1;
14447ff50620SDavid Howells 	if (size > PAGE_SIZE - offset) {
14457ff50620SDavid Howells 		size -= PAGE_SIZE - offset;
14467ff50620SDavid Howells 		count += size >> PAGE_SHIFT;
14477ff50620SDavid Howells 		size &= ~PAGE_MASK;
14487ff50620SDavid Howells 		if (size)
14497ff50620SDavid Howells 			count++;
14507ff50620SDavid Howells 	}
14517ff50620SDavid Howells 
14527ff50620SDavid Howells 	if (count > maxpages)
14537ff50620SDavid Howells 		count = maxpages;
14547ff50620SDavid Howells 
14557ff50620SDavid Howells 	nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
14567ff50620SDavid Howells 	if (nr == 0)
14577ff50620SDavid Howells 		return 0;
14587ff50620SDavid Howells 
14591c27f1fcSLinus Torvalds 	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
14607ff50620SDavid Howells }
14617ff50620SDavid Howells 
1462fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
1463dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
14643d671ca6SAl Viro {
14653d671ca6SAl Viro 	size_t skip;
14663d671ca6SAl Viro 	long k;
14673d671ca6SAl Viro 
1468fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1469fcb14cb1SAl Viro 		return (unsigned long)i->ubuf + i->iov_offset;
1470fcb14cb1SAl Viro 
14713d671ca6SAl Viro 	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
14723d671ca6SAl Viro 		size_t len = i->iov[k].iov_len - skip;
14733d671ca6SAl Viro 
14743d671ca6SAl Viro 		if (unlikely(!len))
14753d671ca6SAl Viro 			continue;
147659dbd7d0SAl Viro 		if (*size > len)
14773d671ca6SAl Viro 			*size = len;
1478dd45ab9dSAl Viro 		return (unsigned long)i->iov[k].iov_base + skip;
14793d671ca6SAl Viro 	}
14803d671ca6SAl Viro 	BUG(); // if it had been empty, we wouldn't get called
14813d671ca6SAl Viro }
14823d671ca6SAl Viro 
14833d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */
14843d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i,
148559dbd7d0SAl Viro 				       size_t *size, size_t *start)
14863d671ca6SAl Viro {
14873d671ca6SAl Viro 	struct page *page;
14883d671ca6SAl Viro 	size_t skip = i->iov_offset, len;
14893d671ca6SAl Viro 
14903d671ca6SAl Viro 	len = i->bvec->bv_len - skip;
149159dbd7d0SAl Viro 	if (*size > len)
149259dbd7d0SAl Viro 		*size = len;
14933d671ca6SAl Viro 	skip += i->bvec->bv_offset;
14943d671ca6SAl Viro 	page = i->bvec->bv_page + skip / PAGE_SIZE;
1495dda8e5d1SAl Viro 	*start = skip % PAGE_SIZE;
14963d671ca6SAl Viro 	return page;
14973d671ca6SAl Viro }
14983d671ca6SAl Viro 
1499d879cb83SAl Viro ssize_t iov_iter_get_pages(struct iov_iter *i,
1500d879cb83SAl Viro 		   struct page **pages, size_t maxsize, unsigned maxpages,
1501d879cb83SAl Viro 		   size_t *start)
1502d879cb83SAl Viro {
15033d671ca6SAl Viro 	int n, res;
15043d671ca6SAl Viro 
1505d879cb83SAl Viro 	if (maxsize > i->count)
1506d879cb83SAl Viro 		maxsize = i->count;
15073d671ca6SAl Viro 	if (!maxsize)
15083d671ca6SAl Viro 		return 0;
15097392ed17SAl Viro 	if (maxsize > MAX_RW_COUNT)
15107392ed17SAl Viro 		maxsize = MAX_RW_COUNT;
1511d879cb83SAl Viro 
1512fcb14cb1SAl Viro 	if (likely(user_backed_iter(i))) {
15133337ab08SAndreas Gruenbacher 		unsigned int gup_flags = 0;
15143d671ca6SAl Viro 		unsigned long addr;
15159ea9ce04SDavid Howells 
15163337ab08SAndreas Gruenbacher 		if (iov_iter_rw(i) != WRITE)
15173337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_WRITE;
15183337ab08SAndreas Gruenbacher 		if (i->nofault)
15193337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_NOFAULT;
15203337ab08SAndreas Gruenbacher 
1521dd45ab9dSAl Viro 		addr = first_iovec_segment(i, &maxsize);
1522dd45ab9dSAl Viro 		*start = addr % PAGE_SIZE;
1523dd45ab9dSAl Viro 		addr &= PAGE_MASK;
152459dbd7d0SAl Viro 		n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1525dda8e5d1SAl Viro 		if (n > maxpages)
1526dda8e5d1SAl Viro 			n = maxpages;
15273337ab08SAndreas Gruenbacher 		res = get_user_pages_fast(addr, n, gup_flags, pages);
1528814a6674SAndreas Gruenbacher 		if (unlikely(res <= 0))
1529d879cb83SAl Viro 			return res;
153059dbd7d0SAl Viro 		return min_t(size_t, maxsize, res * PAGE_SIZE - *start);
15313d671ca6SAl Viro 	}
15323d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
15333d671ca6SAl Viro 		struct page *page;
15343d671ca6SAl Viro 
153559dbd7d0SAl Viro 		page = first_bvec_segment(i, &maxsize, start);
153659dbd7d0SAl Viro 		n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1537dda8e5d1SAl Viro 		if (n > maxpages)
1538dda8e5d1SAl Viro 			n = maxpages;
1539dda8e5d1SAl Viro 		for (int k = 0; k < n; k++)
15403d671ca6SAl Viro 			get_page(*pages++ = page++);
154159dbd7d0SAl Viro 		return min_t(size_t, maxsize, n * PAGE_SIZE - *start);
15423d671ca6SAl Viro 	}
15433d671ca6SAl Viro 	if (iov_iter_is_pipe(i))
15443d671ca6SAl Viro 		return pipe_get_pages(i, pages, maxsize, maxpages, start);
15453d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
15463d671ca6SAl Viro 		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1547d879cb83SAl Viro 	return -EFAULT;
1548d879cb83SAl Viro }
1549d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_get_pages);
1550d879cb83SAl Viro 
1551d879cb83SAl Viro static struct page **get_pages_array(size_t n)
1552d879cb83SAl Viro {
1553752ade68SMichal Hocko 	return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
1554d879cb83SAl Viro }
1555d879cb83SAl Viro 
1556241699cdSAl Viro static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1557241699cdSAl Viro 		   struct page ***pages, size_t maxsize,
1558241699cdSAl Viro 		   size_t *start)
1559241699cdSAl Viro {
1560241699cdSAl Viro 	struct page **p;
15618cefc107SDavid Howells 	unsigned int iter_head, npages;
1562d7760d63SIlya Dryomov 	ssize_t n;
1563241699cdSAl Viro 
1564241699cdSAl Viro 	if (!sanity(i))
1565241699cdSAl Viro 		return -EFAULT;
1566241699cdSAl Viro 
15678cefc107SDavid Howells 	data_start(i, &iter_head, start);
15688cefc107SDavid Howells 	/* Amount of free space: some of this one + all after this one */
15698cefc107SDavid Howells 	npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1570241699cdSAl Viro 	n = npages * PAGE_SIZE - *start;
1571241699cdSAl Viro 	if (maxsize > n)
1572241699cdSAl Viro 		maxsize = n;
1573241699cdSAl Viro 	else
1574241699cdSAl Viro 		npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1575241699cdSAl Viro 	p = get_pages_array(npages);
1576241699cdSAl Viro 	if (!p)
1577241699cdSAl Viro 		return -ENOMEM;
15788cefc107SDavid Howells 	n = __pipe_get_pages(i, maxsize, p, iter_head, start);
1579241699cdSAl Viro 	if (n > 0)
1580241699cdSAl Viro 		*pages = p;
1581241699cdSAl Viro 	else
1582241699cdSAl Viro 		kvfree(p);
1583241699cdSAl Viro 	return n;
1584241699cdSAl Viro }
1585241699cdSAl Viro 
15867ff50620SDavid Howells static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
15877ff50620SDavid Howells 					   struct page ***pages, size_t maxsize,
15887ff50620SDavid Howells 					   size_t *_start_offset)
15897ff50620SDavid Howells {
15907ff50620SDavid Howells 	struct page **p;
15917ff50620SDavid Howells 	unsigned nr, offset;
15927ff50620SDavid Howells 	pgoff_t index, count;
15936c776766SDavid Howells 	size_t size = maxsize;
15947ff50620SDavid Howells 	loff_t pos;
15957ff50620SDavid Howells 
15967ff50620SDavid Howells 	if (!size)
15977ff50620SDavid Howells 		return 0;
15987ff50620SDavid Howells 
15997ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
16007ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
16017ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
16027ff50620SDavid Howells 	*_start_offset = offset;
16037ff50620SDavid Howells 
16047ff50620SDavid Howells 	count = 1;
16057ff50620SDavid Howells 	if (size > PAGE_SIZE - offset) {
16067ff50620SDavid Howells 		size -= PAGE_SIZE - offset;
16077ff50620SDavid Howells 		count += size >> PAGE_SHIFT;
16087ff50620SDavid Howells 		size &= ~PAGE_MASK;
16097ff50620SDavid Howells 		if (size)
16107ff50620SDavid Howells 			count++;
16117ff50620SDavid Howells 	}
16127ff50620SDavid Howells 
16137ff50620SDavid Howells 	p = get_pages_array(count);
16147ff50620SDavid Howells 	if (!p)
16157ff50620SDavid Howells 		return -ENOMEM;
16167ff50620SDavid Howells 	*pages = p;
16177ff50620SDavid Howells 
16187ff50620SDavid Howells 	nr = iter_xarray_populate_pages(p, i->xarray, index, count);
16197ff50620SDavid Howells 	if (nr == 0)
16207ff50620SDavid Howells 		return 0;
16217ff50620SDavid Howells 
16221c27f1fcSLinus Torvalds 	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
16237ff50620SDavid Howells }
16247ff50620SDavid Howells 
1625d879cb83SAl Viro ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1626d879cb83SAl Viro 		   struct page ***pages, size_t maxsize,
1627d879cb83SAl Viro 		   size_t *start)
1628d879cb83SAl Viro {
1629d879cb83SAl Viro 	struct page **p;
16303d671ca6SAl Viro 	int n, res;
1631d879cb83SAl Viro 
1632d879cb83SAl Viro 	if (maxsize > i->count)
1633d879cb83SAl Viro 		maxsize = i->count;
16343d671ca6SAl Viro 	if (!maxsize)
16353d671ca6SAl Viro 		return 0;
16367392ed17SAl Viro 	if (maxsize > MAX_RW_COUNT)
16377392ed17SAl Viro 		maxsize = MAX_RW_COUNT;
1638d879cb83SAl Viro 
1639fcb14cb1SAl Viro 	if (likely(user_backed_iter(i))) {
16403337ab08SAndreas Gruenbacher 		unsigned int gup_flags = 0;
16413d671ca6SAl Viro 		unsigned long addr;
16429ea9ce04SDavid Howells 
16433337ab08SAndreas Gruenbacher 		if (iov_iter_rw(i) != WRITE)
16443337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_WRITE;
16453337ab08SAndreas Gruenbacher 		if (i->nofault)
16463337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_NOFAULT;
16473337ab08SAndreas Gruenbacher 
1648dd45ab9dSAl Viro 		addr = first_iovec_segment(i, &maxsize);
1649dd45ab9dSAl Viro 		*start = addr % PAGE_SIZE;
1650dd45ab9dSAl Viro 		addr &= PAGE_MASK;
165159dbd7d0SAl Viro 		n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1652d879cb83SAl Viro 		p = get_pages_array(n);
1653d879cb83SAl Viro 		if (!p)
1654d879cb83SAl Viro 			return -ENOMEM;
16553337ab08SAndreas Gruenbacher 		res = get_user_pages_fast(addr, n, gup_flags, p);
1656814a6674SAndreas Gruenbacher 		if (unlikely(res <= 0)) {
1657d879cb83SAl Viro 			kvfree(p);
1658814a6674SAndreas Gruenbacher 			*pages = NULL;
1659d879cb83SAl Viro 			return res;
1660d879cb83SAl Viro 		}
1661d879cb83SAl Viro 		*pages = p;
166259dbd7d0SAl Viro 		return min_t(size_t, maxsize, res * PAGE_SIZE - *start);
16633d671ca6SAl Viro 	}
16643d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
16653d671ca6SAl Viro 		struct page *page;
16663d671ca6SAl Viro 
166759dbd7d0SAl Viro 		page = first_bvec_segment(i, &maxsize, start);
166859dbd7d0SAl Viro 		n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
16693d671ca6SAl Viro 		*pages = p = get_pages_array(n);
1670d879cb83SAl Viro 		if (!p)
1671d879cb83SAl Viro 			return -ENOMEM;
1672dda8e5d1SAl Viro 		for (int k = 0; k < n; k++)
16733d671ca6SAl Viro 			get_page(*p++ = page++);
167459dbd7d0SAl Viro 		return min_t(size_t, maxsize, n * PAGE_SIZE - *start);
16753d671ca6SAl Viro 	}
16763d671ca6SAl Viro 	if (iov_iter_is_pipe(i))
16773d671ca6SAl Viro 		return pipe_get_pages_alloc(i, pages, maxsize, start);
16783d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
16793d671ca6SAl Viro 		return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
1680d879cb83SAl Viro 	return -EFAULT;
1681d879cb83SAl Viro }
1682d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1683d879cb83SAl Viro 
1684d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1685d879cb83SAl Viro 			       struct iov_iter *i)
1686d879cb83SAl Viro {
1687d879cb83SAl Viro 	__wsum sum, next;
1688d879cb83SAl Viro 	sum = *csum;
16899ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1690241699cdSAl Viro 		WARN_ON(1);
1691241699cdSAl Viro 		return 0;
1692241699cdSAl Viro 	}
16937baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off, ({
16947baa5099SAl Viro 		next = csum_and_copy_from_user(base, addr + off, len);
1695d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
16967baa5099SAl Viro 		next ? 0 : len;
1697d879cb83SAl Viro 	}), ({
16987baa5099SAl Viro 		sum = csum_and_memcpy(addr + off, base, len, sum, off);
1699d879cb83SAl Viro 	})
1700d879cb83SAl Viro 	)
1701d879cb83SAl Viro 	*csum = sum;
1702d879cb83SAl Viro 	return bytes;
1703d879cb83SAl Viro }
1704d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter);
1705d879cb83SAl Viro 
170652cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1707d879cb83SAl Viro 			     struct iov_iter *i)
1708d879cb83SAl Viro {
170952cbd23aSWillem de Bruijn 	struct csum_state *csstate = _csstate;
1710d879cb83SAl Viro 	__wsum sum, next;
171178e1f386SAl Viro 
171278e1f386SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
1713241699cdSAl Viro 		WARN_ON(1);	/* for now */
1714241699cdSAl Viro 		return 0;
1715241699cdSAl Viro 	}
17166852df12SAl Viro 
17176852df12SAl Viro 	sum = csum_shift(csstate->csum, csstate->off);
17186852df12SAl Viro 	if (unlikely(iov_iter_is_pipe(i)))
17196852df12SAl Viro 		bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
17206852df12SAl Viro 	else iterate_and_advance(i, bytes, base, len, off, ({
17217baa5099SAl Viro 		next = csum_and_copy_to_user(addr + off, base, len);
1722d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
17237baa5099SAl Viro 		next ? 0 : len;
1724d879cb83SAl Viro 	}), ({
17257baa5099SAl Viro 		sum = csum_and_memcpy(base, addr + off, len, sum, off);
1726d879cb83SAl Viro 	})
1727d879cb83SAl Viro 	)
1728594e450bSAl Viro 	csstate->csum = csum_shift(sum, csstate->off);
1729594e450bSAl Viro 	csstate->off += bytes;
1730d879cb83SAl Viro 	return bytes;
1731d879cb83SAl Viro }
1732d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter);
1733d879cb83SAl Viro 
1734d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1735d05f4435SSagi Grimberg 		struct iov_iter *i)
1736d05f4435SSagi Grimberg {
17377999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH
1738d05f4435SSagi Grimberg 	struct ahash_request *hash = hashp;
1739d05f4435SSagi Grimberg 	struct scatterlist sg;
1740d05f4435SSagi Grimberg 	size_t copied;
1741d05f4435SSagi Grimberg 
1742d05f4435SSagi Grimberg 	copied = copy_to_iter(addr, bytes, i);
1743d05f4435SSagi Grimberg 	sg_init_one(&sg, addr, copied);
1744d05f4435SSagi Grimberg 	ahash_request_set_crypt(hash, &sg, NULL, copied);
1745d05f4435SSagi Grimberg 	crypto_ahash_update(hash);
1746d05f4435SSagi Grimberg 	return copied;
174727fad74aSYueHaibing #else
174827fad74aSYueHaibing 	return 0;
174927fad74aSYueHaibing #endif
1750d05f4435SSagi Grimberg }
1751d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter);
1752d05f4435SSagi Grimberg 
175366531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages)
1754d879cb83SAl Viro {
175566531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
175666531c65SAl Viro 	const struct iovec *p;
1757d879cb83SAl Viro 	int npages = 0;
1758d879cb83SAl Viro 
175966531c65SAl Viro 	for (p = i->iov; size; skip = 0, p++) {
176066531c65SAl Viro 		unsigned offs = offset_in_page(p->iov_base + skip);
176166531c65SAl Viro 		size_t len = min(p->iov_len - skip, size);
1762d879cb83SAl Viro 
176366531c65SAl Viro 		if (len) {
176466531c65SAl Viro 			size -= len;
176566531c65SAl Viro 			npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
176666531c65SAl Viro 			if (unlikely(npages > maxpages))
176766531c65SAl Viro 				return maxpages;
176866531c65SAl Viro 		}
176966531c65SAl Viro 	}
177066531c65SAl Viro 	return npages;
177166531c65SAl Viro }
177266531c65SAl Viro 
177366531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages)
177466531c65SAl Viro {
177566531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
177666531c65SAl Viro 	const struct bio_vec *p;
177766531c65SAl Viro 	int npages = 0;
177866531c65SAl Viro 
177966531c65SAl Viro 	for (p = i->bvec; size; skip = 0, p++) {
178066531c65SAl Viro 		unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
178166531c65SAl Viro 		size_t len = min(p->bv_len - skip, size);
178266531c65SAl Viro 
178366531c65SAl Viro 		size -= len;
178466531c65SAl Viro 		npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
178566531c65SAl Viro 		if (unlikely(npages > maxpages))
178666531c65SAl Viro 			return maxpages;
178766531c65SAl Viro 	}
178866531c65SAl Viro 	return npages;
178966531c65SAl Viro }
179066531c65SAl Viro 
179166531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages)
179266531c65SAl Viro {
179366531c65SAl Viro 	if (unlikely(!i->count))
179466531c65SAl Viro 		return 0;
1795fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1796fcb14cb1SAl Viro 		unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1797fcb14cb1SAl Viro 		int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1798fcb14cb1SAl Viro 		return min(npages, maxpages);
1799fcb14cb1SAl Viro 	}
180066531c65SAl Viro 	/* iovec and kvec have identical layouts */
180166531c65SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
180266531c65SAl Viro 		return iov_npages(i, maxpages);
180366531c65SAl Viro 	if (iov_iter_is_bvec(i))
180466531c65SAl Viro 		return bvec_npages(i, maxpages);
180566531c65SAl Viro 	if (iov_iter_is_pipe(i)) {
18068cefc107SDavid Howells 		unsigned int iter_head;
180766531c65SAl Viro 		int npages;
1808241699cdSAl Viro 		size_t off;
1809241699cdSAl Viro 
1810241699cdSAl Viro 		if (!sanity(i))
1811241699cdSAl Viro 			return 0;
1812241699cdSAl Viro 
18138cefc107SDavid Howells 		data_start(i, &iter_head, &off);
1814241699cdSAl Viro 		/* some of this one + all after this one */
181566531c65SAl Viro 		npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
181666531c65SAl Viro 		return min(npages, maxpages);
181766531c65SAl Viro 	}
181866531c65SAl Viro 	if (iov_iter_is_xarray(i)) {
1819e4f8df86SAl Viro 		unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1820e4f8df86SAl Viro 		int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
182166531c65SAl Viro 		return min(npages, maxpages);
182266531c65SAl Viro 	}
182366531c65SAl Viro 	return 0;
1824d879cb83SAl Viro }
1825d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages);
1826d879cb83SAl Viro 
1827d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1828d879cb83SAl Viro {
1829d879cb83SAl Viro 	*new = *old;
183000e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(new))) {
1831241699cdSAl Viro 		WARN_ON(1);
1832241699cdSAl Viro 		return NULL;
1833241699cdSAl Viro 	}
183400e23707SDavid Howells 	if (iov_iter_is_bvec(new))
1835d879cb83SAl Viro 		return new->bvec = kmemdup(new->bvec,
1836d879cb83SAl Viro 				    new->nr_segs * sizeof(struct bio_vec),
1837d879cb83SAl Viro 				    flags);
1838fcb14cb1SAl Viro 	else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1839d879cb83SAl Viro 		/* iovec and kvec have identical layout */
1840d879cb83SAl Viro 		return new->iov = kmemdup(new->iov,
1841d879cb83SAl Viro 				   new->nr_segs * sizeof(struct iovec),
1842d879cb83SAl Viro 				   flags);
1843fcb14cb1SAl Viro 	return NULL;
1844d879cb83SAl Viro }
1845d879cb83SAl Viro EXPORT_SYMBOL(dup_iter);
1846bc917be8SAl Viro 
1847bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov,
1848bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1849bfdc5970SChristoph Hellwig {
1850bfdc5970SChristoph Hellwig 	const struct compat_iovec __user *uiov =
1851bfdc5970SChristoph Hellwig 		(const struct compat_iovec __user *)uvec;
1852bfdc5970SChristoph Hellwig 	int ret = -EFAULT, i;
1853bfdc5970SChristoph Hellwig 
1854a959a978SChristoph Hellwig 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1855bfdc5970SChristoph Hellwig 		return -EFAULT;
1856bfdc5970SChristoph Hellwig 
1857bfdc5970SChristoph Hellwig 	for (i = 0; i < nr_segs; i++) {
1858bfdc5970SChristoph Hellwig 		compat_uptr_t buf;
1859bfdc5970SChristoph Hellwig 		compat_ssize_t len;
1860bfdc5970SChristoph Hellwig 
1861bfdc5970SChristoph Hellwig 		unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1862bfdc5970SChristoph Hellwig 		unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1863bfdc5970SChristoph Hellwig 
1864bfdc5970SChristoph Hellwig 		/* check for compat_size_t not fitting in compat_ssize_t .. */
1865bfdc5970SChristoph Hellwig 		if (len < 0) {
1866bfdc5970SChristoph Hellwig 			ret = -EINVAL;
1867bfdc5970SChristoph Hellwig 			goto uaccess_end;
1868bfdc5970SChristoph Hellwig 		}
1869bfdc5970SChristoph Hellwig 		iov[i].iov_base = compat_ptr(buf);
1870bfdc5970SChristoph Hellwig 		iov[i].iov_len = len;
1871bfdc5970SChristoph Hellwig 	}
1872bfdc5970SChristoph Hellwig 
1873bfdc5970SChristoph Hellwig 	ret = 0;
1874bfdc5970SChristoph Hellwig uaccess_end:
1875bfdc5970SChristoph Hellwig 	user_access_end();
1876bfdc5970SChristoph Hellwig 	return ret;
1877bfdc5970SChristoph Hellwig }
1878bfdc5970SChristoph Hellwig 
1879bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov,
1880bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1881fb041b59SDavid Laight {
1882fb041b59SDavid Laight 	unsigned long seg;
1883bfdc5970SChristoph Hellwig 
1884bfdc5970SChristoph Hellwig 	if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1885bfdc5970SChristoph Hellwig 		return -EFAULT;
1886bfdc5970SChristoph Hellwig 	for (seg = 0; seg < nr_segs; seg++) {
1887bfdc5970SChristoph Hellwig 		if ((ssize_t)iov[seg].iov_len < 0)
1888bfdc5970SChristoph Hellwig 			return -EINVAL;
1889bfdc5970SChristoph Hellwig 	}
1890bfdc5970SChristoph Hellwig 
1891bfdc5970SChristoph Hellwig 	return 0;
1892bfdc5970SChristoph Hellwig }
1893bfdc5970SChristoph Hellwig 
1894bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec,
1895bfdc5970SChristoph Hellwig 		unsigned long nr_segs, unsigned long fast_segs,
1896bfdc5970SChristoph Hellwig 		struct iovec *fast_iov, bool compat)
1897bfdc5970SChristoph Hellwig {
1898bfdc5970SChristoph Hellwig 	struct iovec *iov = fast_iov;
1899bfdc5970SChristoph Hellwig 	int ret;
1900fb041b59SDavid Laight 
1901fb041b59SDavid Laight 	/*
1902bfdc5970SChristoph Hellwig 	 * SuS says "The readv() function *may* fail if the iovcnt argument was
1903bfdc5970SChristoph Hellwig 	 * less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1904fb041b59SDavid Laight 	 * traditionally returned zero for zero segments, so...
1905fb041b59SDavid Laight 	 */
1906bfdc5970SChristoph Hellwig 	if (nr_segs == 0)
1907bfdc5970SChristoph Hellwig 		return iov;
1908bfdc5970SChristoph Hellwig 	if (nr_segs > UIO_MAXIOV)
1909bfdc5970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1910fb041b59SDavid Laight 	if (nr_segs > fast_segs) {
1911fb041b59SDavid Laight 		iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1912bfdc5970SChristoph Hellwig 		if (!iov)
1913bfdc5970SChristoph Hellwig 			return ERR_PTR(-ENOMEM);
1914fb041b59SDavid Laight 	}
1915bfdc5970SChristoph Hellwig 
1916bfdc5970SChristoph Hellwig 	if (compat)
1917bfdc5970SChristoph Hellwig 		ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1918bfdc5970SChristoph Hellwig 	else
1919bfdc5970SChristoph Hellwig 		ret = copy_iovec_from_user(iov, uvec, nr_segs);
1920bfdc5970SChristoph Hellwig 	if (ret) {
1921bfdc5970SChristoph Hellwig 		if (iov != fast_iov)
1922bfdc5970SChristoph Hellwig 			kfree(iov);
1923bfdc5970SChristoph Hellwig 		return ERR_PTR(ret);
1924fb041b59SDavid Laight 	}
1925bfdc5970SChristoph Hellwig 
1926bfdc5970SChristoph Hellwig 	return iov;
1927bfdc5970SChristoph Hellwig }
1928bfdc5970SChristoph Hellwig 
1929bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1930bfdc5970SChristoph Hellwig 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1931bfdc5970SChristoph Hellwig 		 struct iov_iter *i, bool compat)
1932bfdc5970SChristoph Hellwig {
1933bfdc5970SChristoph Hellwig 	ssize_t total_len = 0;
1934bfdc5970SChristoph Hellwig 	unsigned long seg;
1935bfdc5970SChristoph Hellwig 	struct iovec *iov;
1936bfdc5970SChristoph Hellwig 
1937bfdc5970SChristoph Hellwig 	iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1938bfdc5970SChristoph Hellwig 	if (IS_ERR(iov)) {
1939bfdc5970SChristoph Hellwig 		*iovp = NULL;
1940bfdc5970SChristoph Hellwig 		return PTR_ERR(iov);
1941fb041b59SDavid Laight 	}
1942fb041b59SDavid Laight 
1943fb041b59SDavid Laight 	/*
1944bfdc5970SChristoph Hellwig 	 * According to the Single Unix Specification we should return EINVAL if
1945bfdc5970SChristoph Hellwig 	 * an element length is < 0 when cast to ssize_t or if the total length
1946bfdc5970SChristoph Hellwig 	 * would overflow the ssize_t return value of the system call.
1947fb041b59SDavid Laight 	 *
1948fb041b59SDavid Laight 	 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1949fb041b59SDavid Laight 	 * overflow case.
1950fb041b59SDavid Laight 	 */
1951fb041b59SDavid Laight 	for (seg = 0; seg < nr_segs; seg++) {
1952fb041b59SDavid Laight 		ssize_t len = (ssize_t)iov[seg].iov_len;
1953fb041b59SDavid Laight 
1954bfdc5970SChristoph Hellwig 		if (!access_ok(iov[seg].iov_base, len)) {
1955bfdc5970SChristoph Hellwig 			if (iov != *iovp)
1956bfdc5970SChristoph Hellwig 				kfree(iov);
1957bfdc5970SChristoph Hellwig 			*iovp = NULL;
1958bfdc5970SChristoph Hellwig 			return -EFAULT;
1959fb041b59SDavid Laight 		}
1960bfdc5970SChristoph Hellwig 
1961bfdc5970SChristoph Hellwig 		if (len > MAX_RW_COUNT - total_len) {
1962bfdc5970SChristoph Hellwig 			len = MAX_RW_COUNT - total_len;
1963fb041b59SDavid Laight 			iov[seg].iov_len = len;
1964fb041b59SDavid Laight 		}
1965bfdc5970SChristoph Hellwig 		total_len += len;
1966fb041b59SDavid Laight 	}
1967bfdc5970SChristoph Hellwig 
1968bfdc5970SChristoph Hellwig 	iov_iter_init(i, type, iov, nr_segs, total_len);
1969bfdc5970SChristoph Hellwig 	if (iov == *iovp)
1970bfdc5970SChristoph Hellwig 		*iovp = NULL;
1971bfdc5970SChristoph Hellwig 	else
1972bfdc5970SChristoph Hellwig 		*iovp = iov;
1973bfdc5970SChristoph Hellwig 	return total_len;
1974fb041b59SDavid Laight }
1975fb041b59SDavid Laight 
1976ffecee4fSVegard Nossum /**
1977ffecee4fSVegard Nossum  * import_iovec() - Copy an array of &struct iovec from userspace
1978ffecee4fSVegard Nossum  *     into the kernel, check that it is valid, and initialize a new
1979ffecee4fSVegard Nossum  *     &struct iov_iter iterator to access it.
1980ffecee4fSVegard Nossum  *
1981ffecee4fSVegard Nossum  * @type: One of %READ or %WRITE.
1982bfdc5970SChristoph Hellwig  * @uvec: Pointer to the userspace array.
1983ffecee4fSVegard Nossum  * @nr_segs: Number of elements in userspace array.
1984ffecee4fSVegard Nossum  * @fast_segs: Number of elements in @iov.
1985bfdc5970SChristoph Hellwig  * @iovp: (input and output parameter) Pointer to pointer to (usually small
1986ffecee4fSVegard Nossum  *     on-stack) kernel array.
1987ffecee4fSVegard Nossum  * @i: Pointer to iterator that will be initialized on success.
1988ffecee4fSVegard Nossum  *
1989ffecee4fSVegard Nossum  * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1990ffecee4fSVegard Nossum  * then this function places %NULL in *@iov on return. Otherwise, a new
1991ffecee4fSVegard Nossum  * array will be allocated and the result placed in *@iov. This means that
1992ffecee4fSVegard Nossum  * the caller may call kfree() on *@iov regardless of whether the small
1993ffecee4fSVegard Nossum  * on-stack array was used or not (and regardless of whether this function
1994ffecee4fSVegard Nossum  * returns an error or not).
1995ffecee4fSVegard Nossum  *
199687e5e6daSJens Axboe  * Return: Negative error code on error, bytes imported on success
1997ffecee4fSVegard Nossum  */
1998bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec,
1999bc917be8SAl Viro 		 unsigned nr_segs, unsigned fast_segs,
2000bfdc5970SChristoph Hellwig 		 struct iovec **iovp, struct iov_iter *i)
2001bc917be8SAl Viro {
200289cd35c5SChristoph Hellwig 	return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
200389cd35c5SChristoph Hellwig 			      in_compat_syscall());
2004bc917be8SAl Viro }
2005bc917be8SAl Viro EXPORT_SYMBOL(import_iovec);
2006bc917be8SAl Viro 
2007bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len,
2008bc917be8SAl Viro 		 struct iovec *iov, struct iov_iter *i)
2009bc917be8SAl Viro {
2010bc917be8SAl Viro 	if (len > MAX_RW_COUNT)
2011bc917be8SAl Viro 		len = MAX_RW_COUNT;
201296d4f267SLinus Torvalds 	if (unlikely(!access_ok(buf, len)))
2013bc917be8SAl Viro 		return -EFAULT;
2014bc917be8SAl Viro 
2015bc917be8SAl Viro 	iov->iov_base = buf;
2016bc917be8SAl Viro 	iov->iov_len = len;
2017bc917be8SAl Viro 	iov_iter_init(i, rw, iov, 1, len);
2018bc917be8SAl Viro 	return 0;
2019bc917be8SAl Viro }
2020e1267585SAl Viro EXPORT_SYMBOL(import_single_range);
20218fb0f47aSJens Axboe 
20228fb0f47aSJens Axboe /**
20238fb0f47aSJens Axboe  * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
20248fb0f47aSJens Axboe  *     iov_iter_save_state() was called.
20258fb0f47aSJens Axboe  *
20268fb0f47aSJens Axboe  * @i: &struct iov_iter to restore
20278fb0f47aSJens Axboe  * @state: state to restore from
20288fb0f47aSJens Axboe  *
20298fb0f47aSJens Axboe  * Used after iov_iter_save_state() to bring restore @i, if operations may
20308fb0f47aSJens Axboe  * have advanced it.
20318fb0f47aSJens Axboe  *
20328fb0f47aSJens Axboe  * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
20338fb0f47aSJens Axboe  */
20348fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
20358fb0f47aSJens Axboe {
20368fb0f47aSJens Axboe 	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
2037fcb14cb1SAl Viro 			 !iov_iter_is_kvec(i) && !iter_is_ubuf(i))
20388fb0f47aSJens Axboe 		return;
20398fb0f47aSJens Axboe 	i->iov_offset = state->iov_offset;
20408fb0f47aSJens Axboe 	i->count = state->count;
2041fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
2042fcb14cb1SAl Viro 		return;
20438fb0f47aSJens Axboe 	/*
20448fb0f47aSJens Axboe 	 * For the *vec iters, nr_segs + iov is constant - if we increment
20458fb0f47aSJens Axboe 	 * the vec, then we also decrement the nr_segs count. Hence we don't
20468fb0f47aSJens Axboe 	 * need to track both of these, just one is enough and we can deduct
20478fb0f47aSJens Axboe 	 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
20488fb0f47aSJens Axboe 	 * size, so we can just increment the iov pointer as they are unionzed.
20498fb0f47aSJens Axboe 	 * ITER_BVEC _may_ be the same size on some archs, but on others it is
20508fb0f47aSJens Axboe 	 * not. Be safe and handle it separately.
20518fb0f47aSJens Axboe 	 */
20528fb0f47aSJens Axboe 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
20538fb0f47aSJens Axboe 	if (iov_iter_is_bvec(i))
20548fb0f47aSJens Axboe 		i->bvec -= state->nr_segs - i->nr_segs;
20558fb0f47aSJens Axboe 	else
20568fb0f47aSJens Axboe 		i->iov -= state->nr_segs - i->nr_segs;
20578fb0f47aSJens Axboe 	i->nr_segs = state->nr_segs;
20588fb0f47aSJens Axboe }
2059