xref: /openbmc/linux/lib/iov_iter.c (revision f0f6b614)
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 
20210f525a8SAl Viro 	if (i->last_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);
21010f525a8SAl Viro 		if (unlikely(p->offset + p->len != abs(i->last_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:
21810f525a8SAl Viro 	printk(KERN_ERR "idx = %d, offset = %d\n", i_head, i->last_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 
26210f525a8SAl Viro static inline int last_offset(const struct pipe_buffer *buf)
2638fad7767SAl Viro {
26410f525a8SAl Viro 	if (buf->ops == &default_pipe_buf_ops)
26510f525a8SAl Viro 		return buf->len;	// buf->offset is 0 for those
26610f525a8SAl Viro 	else
26710f525a8SAl Viro 		return -(buf->offset + buf->len);
2688fad7767SAl Viro }
2698fad7767SAl Viro 
2708fad7767SAl Viro static struct page *append_pipe(struct iov_iter *i, size_t size,
2718fad7767SAl Viro 				unsigned int *off)
2728fad7767SAl Viro {
2738fad7767SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
27410f525a8SAl Viro 	int offset = i->last_offset;
2758fad7767SAl Viro 	struct pipe_buffer *buf;
2768fad7767SAl Viro 	struct page *page;
2778fad7767SAl Viro 
27810f525a8SAl Viro 	if (offset > 0 && offset < PAGE_SIZE) {
27910f525a8SAl Viro 		// some space in the last buffer; add to it
2808fad7767SAl Viro 		buf = pipe_buf(pipe, pipe->head - 1);
2818fad7767SAl Viro 		size = min_t(size_t, size, PAGE_SIZE - offset);
2828fad7767SAl Viro 		buf->len += size;
28310f525a8SAl Viro 		i->last_offset += size;
2848fad7767SAl Viro 		i->count -= size;
2858fad7767SAl Viro 		*off = offset;
2868fad7767SAl Viro 		return buf->page;
2878fad7767SAl Viro 	}
2888fad7767SAl Viro 	// OK, we need a new buffer
2898fad7767SAl Viro 	*off = 0;
2908fad7767SAl Viro 	size = min_t(size_t, size, PAGE_SIZE);
2918fad7767SAl Viro 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
2928fad7767SAl Viro 		return NULL;
2938fad7767SAl Viro 	page = push_anon(pipe, size);
2948fad7767SAl Viro 	if (!page)
2958fad7767SAl Viro 		return NULL;
2968fad7767SAl Viro 	i->head = pipe->head - 1;
29710f525a8SAl Viro 	i->last_offset = size;
2988fad7767SAl Viro 	i->count -= size;
2998fad7767SAl Viro 	return page;
3008fad7767SAl Viro }
3018fad7767SAl Viro 
302241699cdSAl Viro static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
303241699cdSAl Viro 			 struct iov_iter *i)
304241699cdSAl Viro {
305241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
30647b7fcaeSAl Viro 	unsigned int head = pipe->head;
307241699cdSAl Viro 
308241699cdSAl Viro 	if (unlikely(bytes > i->count))
309241699cdSAl Viro 		bytes = i->count;
310241699cdSAl Viro 
311241699cdSAl Viro 	if (unlikely(!bytes))
312241699cdSAl Viro 		return 0;
313241699cdSAl Viro 
314241699cdSAl Viro 	if (!sanity(i))
315241699cdSAl Viro 		return 0;
316241699cdSAl Viro 
31710f525a8SAl Viro 	if (offset && i->last_offset == -offset) { // could we merge it?
31847b7fcaeSAl Viro 		struct pipe_buffer *buf = pipe_buf(pipe, head - 1);
31947b7fcaeSAl Viro 		if (buf->page == page) {
320241699cdSAl Viro 			buf->len += bytes;
32110f525a8SAl Viro 			i->last_offset -= bytes;
32247b7fcaeSAl Viro 			i->count -= bytes;
32347b7fcaeSAl Viro 			return bytes;
324241699cdSAl Viro 		}
325241699cdSAl Viro 	}
32647b7fcaeSAl Viro 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
327241699cdSAl Viro 		return 0;
3288cefc107SDavid Howells 
32947b7fcaeSAl Viro 	push_page(pipe, page, offset, bytes);
33010f525a8SAl Viro 	i->last_offset = -(offset + bytes);
33147b7fcaeSAl Viro 	i->head = head;
332241699cdSAl Viro 	i->count -= bytes;
333241699cdSAl Viro 	return bytes;
334241699cdSAl Viro }
335241699cdSAl Viro 
336d879cb83SAl Viro /*
337a6294593SAndreas Gruenbacher  * fault_in_iov_iter_readable - fault in iov iterator for reading
338a6294593SAndreas Gruenbacher  * @i: iterator
339a6294593SAndreas Gruenbacher  * @size: maximum length
340171a0203SAnton Altaparmakov  *
341a6294593SAndreas Gruenbacher  * Fault in one or more iovecs of the given iov_iter, to a maximum length of
342a6294593SAndreas Gruenbacher  * @size.  For each iovec, fault in each page that constitutes the iovec.
343a6294593SAndreas Gruenbacher  *
344a6294593SAndreas Gruenbacher  * Returns the number of bytes not faulted in (like copy_to_user() and
345a6294593SAndreas Gruenbacher  * copy_from_user()).
346a6294593SAndreas Gruenbacher  *
347a6294593SAndreas Gruenbacher  * Always returns 0 for non-userspace iterators.
348171a0203SAnton Altaparmakov  */
349a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
350171a0203SAnton Altaparmakov {
351fcb14cb1SAl Viro 	if (iter_is_ubuf(i)) {
352fcb14cb1SAl Viro 		size_t n = min(size, iov_iter_count(i));
353fcb14cb1SAl Viro 		n -= fault_in_readable(i->ubuf + i->iov_offset, n);
354fcb14cb1SAl Viro 		return size - n;
355fcb14cb1SAl Viro 	} else if (iter_is_iovec(i)) {
356a6294593SAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
3578409a0d2SAl Viro 		const struct iovec *p;
3588409a0d2SAl Viro 		size_t skip;
3598409a0d2SAl Viro 
360a6294593SAndreas Gruenbacher 		size -= count;
361a6294593SAndreas Gruenbacher 		for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
362a6294593SAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
363a6294593SAndreas Gruenbacher 			size_t ret;
3648409a0d2SAl Viro 
3658409a0d2SAl Viro 			if (unlikely(!len))
3668409a0d2SAl Viro 				continue;
367a6294593SAndreas Gruenbacher 			ret = fault_in_readable(p->iov_base + skip, len);
368a6294593SAndreas Gruenbacher 			count -= len - ret;
369a6294593SAndreas Gruenbacher 			if (ret)
370a6294593SAndreas Gruenbacher 				break;
3718409a0d2SAl Viro 		}
372a6294593SAndreas Gruenbacher 		return count + size;
373171a0203SAnton Altaparmakov 	}
374171a0203SAnton Altaparmakov 	return 0;
375171a0203SAnton Altaparmakov }
376a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable);
377171a0203SAnton Altaparmakov 
378cdd591fcSAndreas Gruenbacher /*
379cdd591fcSAndreas Gruenbacher  * fault_in_iov_iter_writeable - fault in iov iterator for writing
380cdd591fcSAndreas Gruenbacher  * @i: iterator
381cdd591fcSAndreas Gruenbacher  * @size: maximum length
382cdd591fcSAndreas Gruenbacher  *
383cdd591fcSAndreas Gruenbacher  * Faults in the iterator using get_user_pages(), i.e., without triggering
384cdd591fcSAndreas Gruenbacher  * hardware page faults.  This is primarily useful when we already know that
385cdd591fcSAndreas Gruenbacher  * some or all of the pages in @i aren't in memory.
386cdd591fcSAndreas Gruenbacher  *
387cdd591fcSAndreas Gruenbacher  * Returns the number of bytes not faulted in, like copy_to_user() and
388cdd591fcSAndreas Gruenbacher  * copy_from_user().
389cdd591fcSAndreas Gruenbacher  *
390cdd591fcSAndreas Gruenbacher  * Always returns 0 for non-user-space iterators.
391cdd591fcSAndreas Gruenbacher  */
392cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
393cdd591fcSAndreas Gruenbacher {
394fcb14cb1SAl Viro 	if (iter_is_ubuf(i)) {
395fcb14cb1SAl Viro 		size_t n = min(size, iov_iter_count(i));
396fcb14cb1SAl Viro 		n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n);
397fcb14cb1SAl Viro 		return size - n;
398fcb14cb1SAl Viro 	} else if (iter_is_iovec(i)) {
399cdd591fcSAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
400cdd591fcSAndreas Gruenbacher 		const struct iovec *p;
401cdd591fcSAndreas Gruenbacher 		size_t skip;
402cdd591fcSAndreas Gruenbacher 
403cdd591fcSAndreas Gruenbacher 		size -= count;
404cdd591fcSAndreas Gruenbacher 		for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
405cdd591fcSAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
406cdd591fcSAndreas Gruenbacher 			size_t ret;
407cdd591fcSAndreas Gruenbacher 
408cdd591fcSAndreas Gruenbacher 			if (unlikely(!len))
409cdd591fcSAndreas Gruenbacher 				continue;
410cdd591fcSAndreas Gruenbacher 			ret = fault_in_safe_writeable(p->iov_base + skip, len);
411cdd591fcSAndreas Gruenbacher 			count -= len - ret;
412cdd591fcSAndreas Gruenbacher 			if (ret)
413cdd591fcSAndreas Gruenbacher 				break;
414cdd591fcSAndreas Gruenbacher 		}
415cdd591fcSAndreas Gruenbacher 		return count + size;
416cdd591fcSAndreas Gruenbacher 	}
417cdd591fcSAndreas Gruenbacher 	return 0;
418cdd591fcSAndreas Gruenbacher }
419cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable);
420cdd591fcSAndreas Gruenbacher 
421aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction,
422d879cb83SAl Viro 			const struct iovec *iov, unsigned long nr_segs,
423d879cb83SAl Viro 			size_t count)
424d879cb83SAl Viro {
425aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
4268cd54c1cSAl Viro 	*i = (struct iov_iter) {
4278cd54c1cSAl Viro 		.iter_type = ITER_IOVEC,
4283337ab08SAndreas Gruenbacher 		.nofault = false,
429fcb14cb1SAl Viro 		.user_backed = true,
4308cd54c1cSAl Viro 		.data_source = direction,
4318cd54c1cSAl Viro 		.iov = iov,
4328cd54c1cSAl Viro 		.nr_segs = nr_segs,
4338cd54c1cSAl Viro 		.iov_offset = 0,
4348cd54c1cSAl Viro 		.count = count
4358cd54c1cSAl Viro 	};
436d879cb83SAl Viro }
437d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init);
438d879cb83SAl Viro 
43912d426abSAl Viro // returns the offset in partial buffer (if any)
44012d426abSAl Viro static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages)
441241699cdSAl Viro {
44212d426abSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
44312d426abSAl Viro 	int used = pipe->head - pipe->tail;
44410f525a8SAl Viro 	int off = i->last_offset;
4458cefc107SDavid Howells 
44612d426abSAl Viro 	*npages = max((int)pipe->max_usage - used, 0);
44712d426abSAl Viro 
44810f525a8SAl Viro 	if (off > 0 && off < PAGE_SIZE) { // anon and not full
44912d426abSAl Viro 		(*npages)++;
45012d426abSAl Viro 		return off;
45110f525a8SAl Viro 	}
45212d426abSAl Viro 	return 0;
453241699cdSAl Viro }
454241699cdSAl Viro 
455241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
456241699cdSAl Viro 				struct iov_iter *i)
457241699cdSAl Viro {
4588fad7767SAl Viro 	unsigned int off, chunk;
4598fad7767SAl Viro 
4608fad7767SAl Viro 	if (unlikely(bytes > i->count))
4618fad7767SAl Viro 		bytes = i->count;
4628fad7767SAl Viro 	if (unlikely(!bytes))
4638fad7767SAl Viro 		return 0;
464241699cdSAl Viro 
465241699cdSAl Viro 	if (!sanity(i))
466241699cdSAl Viro 		return 0;
467241699cdSAl Viro 
4688fad7767SAl Viro 	for (size_t n = bytes; n; n -= chunk) {
4698fad7767SAl Viro 		struct page *page = append_pipe(i, n, &off);
4708fad7767SAl Viro 		chunk = min_t(size_t, n, PAGE_SIZE - off);
4718fad7767SAl Viro 		if (!page)
4728fad7767SAl Viro 			return bytes - n;
4738fad7767SAl Viro 		memcpy_to_page(page, off, addr, chunk);
474241699cdSAl Viro 		addr += chunk;
4758fad7767SAl Viro 	}
476241699cdSAl Viro 	return bytes;
477241699cdSAl Viro }
478241699cdSAl Viro 
479f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
480f9152895SAl Viro 			      __wsum sum, size_t off)
481f9152895SAl Viro {
482cc44c17bSAl Viro 	__wsum next = csum_partial_copy_nocheck(from, to, len);
483f9152895SAl Viro 	return csum_block_add(sum, next, off);
484f9152895SAl Viro }
485f9152895SAl Viro 
48678e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
4876852df12SAl Viro 					 struct iov_iter *i, __wsum *sump)
48878e1f386SAl Viro {
4896852df12SAl Viro 	__wsum sum = *sump;
4906852df12SAl Viro 	size_t off = 0;
4918fad7767SAl Viro 	unsigned int chunk, r;
4928fad7767SAl Viro 
4938fad7767SAl Viro 	if (unlikely(bytes > i->count))
4948fad7767SAl Viro 		bytes = i->count;
4958fad7767SAl Viro 	if (unlikely(!bytes))
4968fad7767SAl Viro 		return 0;
49778e1f386SAl Viro 
49878e1f386SAl Viro 	if (!sanity(i))
49978e1f386SAl Viro 		return 0;
50078e1f386SAl Viro 
5016852df12SAl Viro 	while (bytes) {
5028fad7767SAl Viro 		struct page *page = append_pipe(i, bytes, &r);
5038fad7767SAl Viro 		char *p;
5048fad7767SAl Viro 
5058fad7767SAl Viro 		if (!page)
5068fad7767SAl Viro 			break;
5078fad7767SAl Viro 		chunk = min_t(size_t, bytes, PAGE_SIZE - r);
5088fad7767SAl Viro 		p = kmap_local_page(page);
5096852df12SAl Viro 		sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
5102495bdccSAl Viro 		kunmap_local(p);
51178e1f386SAl Viro 		off += chunk;
5128fad7767SAl Viro 		bytes -= chunk;
5136852df12SAl Viro 	}
5146852df12SAl Viro 	*sump = sum;
5156852df12SAl Viro 	return off;
51678e1f386SAl Viro }
51778e1f386SAl Viro 
518aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
519d879cb83SAl Viro {
52000e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
521241699cdSAl Viro 		return copy_pipe_to_iter(addr, bytes, i);
522fcb14cb1SAl Viro 	if (user_backed_iter(i))
52309fc68dcSAl Viro 		might_fault();
5247baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
5257baa5099SAl Viro 		copyout(base, addr + off, len),
5267baa5099SAl Viro 		memcpy(base, addr + off, len)
527d879cb83SAl Viro 	)
528d879cb83SAl Viro 
529d879cb83SAl Viro 	return bytes;
530d879cb83SAl Viro }
531aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter);
532d879cb83SAl Viro 
533ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC
534ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n)
5358780356eSDan Williams {
53696d4f267SLinus Torvalds 	if (access_ok(to, n)) {
537d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
538ec6347bbSDan Williams 		n = copy_mc_to_user((__force void *) to, from, n);
5398780356eSDan Williams 	}
5408780356eSDan Williams 	return n;
5418780356eSDan Williams }
5428780356eSDan Williams 
543ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
544ca146f6fSDan Williams 				struct iov_iter *i)
545ca146f6fSDan Williams {
5468fad7767SAl Viro 	size_t xfer = 0;
5478fad7767SAl Viro 	unsigned int off, chunk;
5488fad7767SAl Viro 
5498fad7767SAl Viro 	if (unlikely(bytes > i->count))
5508fad7767SAl Viro 		bytes = i->count;
5518fad7767SAl Viro 	if (unlikely(!bytes))
5528fad7767SAl Viro 		return 0;
553ca146f6fSDan Williams 
554ca146f6fSDan Williams 	if (!sanity(i))
555ca146f6fSDan Williams 		return 0;
556ca146f6fSDan Williams 
5578fad7767SAl Viro 	while (bytes) {
5588fad7767SAl Viro 		struct page *page = append_pipe(i, bytes, &off);
559ca146f6fSDan Williams 		unsigned long rem;
5608fad7767SAl Viro 		char *p;
5618fad7767SAl Viro 
5628fad7767SAl Viro 		if (!page)
5638fad7767SAl Viro 			break;
5648fad7767SAl Viro 		chunk = min_t(size_t, bytes, PAGE_SIZE - off);
5658fad7767SAl Viro 		p = kmap_local_page(page);
5662a510a74SAl Viro 		rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
5672a510a74SAl Viro 		chunk -= rem;
5682a510a74SAl Viro 		kunmap_local(p);
5692a510a74SAl Viro 		xfer += chunk;
5708fad7767SAl Viro 		bytes -= chunk;
571c3497fd0SAl Viro 		if (rem) {
5728fad7767SAl Viro 			iov_iter_revert(i, rem);
573ca146f6fSDan Williams 			break;
574c3497fd0SAl Viro 		}
5752a510a74SAl Viro 	}
576ca146f6fSDan Williams 	return xfer;
577ca146f6fSDan Williams }
578ca146f6fSDan Williams 
579bf3eeb9bSDan Williams /**
580ec6347bbSDan Williams  * _copy_mc_to_iter - copy to iter with source memory error exception handling
581bf3eeb9bSDan Williams  * @addr: source kernel address
582bf3eeb9bSDan Williams  * @bytes: total transfer length
58344e55997SRandy Dunlap  * @i: destination iterator
584bf3eeb9bSDan Williams  *
585ec6347bbSDan Williams  * The pmem driver deploys this for the dax operation
586ec6347bbSDan Williams  * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
587ec6347bbSDan Williams  * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
588ec6347bbSDan Williams  * successfully copied.
589bf3eeb9bSDan Williams  *
590ec6347bbSDan Williams  * The main differences between this and typical _copy_to_iter().
591bf3eeb9bSDan Williams  *
592bf3eeb9bSDan Williams  * * Typical tail/residue handling after a fault retries the copy
593bf3eeb9bSDan Williams  *   byte-by-byte until the fault happens again. Re-triggering machine
594bf3eeb9bSDan Williams  *   checks is potentially fatal so the implementation uses source
595bf3eeb9bSDan Williams  *   alignment and poison alignment assumptions to avoid re-triggering
596bf3eeb9bSDan Williams  *   hardware exceptions.
597bf3eeb9bSDan Williams  *
598bf3eeb9bSDan Williams  * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
599bf3eeb9bSDan Williams  *   Compare to copy_to_iter() where only ITER_IOVEC attempts might return
600bf3eeb9bSDan Williams  *   a short copy.
60144e55997SRandy Dunlap  *
60244e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
603bf3eeb9bSDan Williams  */
604ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
6058780356eSDan Williams {
60600e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
607ec6347bbSDan Williams 		return copy_mc_pipe_to_iter(addr, bytes, i);
608fcb14cb1SAl Viro 	if (user_backed_iter(i))
6098780356eSDan Williams 		might_fault();
6107baa5099SAl Viro 	__iterate_and_advance(i, bytes, base, len, off,
6117baa5099SAl Viro 		copyout_mc(base, addr + off, len),
6127baa5099SAl Viro 		copy_mc_to_kernel(base, addr + off, len)
6138780356eSDan Williams 	)
6148780356eSDan Williams 
6158780356eSDan Williams 	return bytes;
6168780356eSDan Williams }
617ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
618ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */
6198780356eSDan Williams 
620aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
621d879cb83SAl Viro {
62200e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
623241699cdSAl Viro 		WARN_ON(1);
624241699cdSAl Viro 		return 0;
625241699cdSAl Viro 	}
626fcb14cb1SAl Viro 	if (user_backed_iter(i))
62709fc68dcSAl Viro 		might_fault();
6287baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6297baa5099SAl Viro 		copyin(addr + off, base, len),
6307baa5099SAl Viro 		memcpy(addr + off, base, len)
631d879cb83SAl Viro 	)
632d879cb83SAl Viro 
633d879cb83SAl Viro 	return bytes;
634d879cb83SAl Viro }
635aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter);
636d879cb83SAl Viro 
637aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
638d879cb83SAl Viro {
63900e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
640241699cdSAl Viro 		WARN_ON(1);
641241699cdSAl Viro 		return 0;
642241699cdSAl Viro 	}
6437baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6447baa5099SAl Viro 		__copy_from_user_inatomic_nocache(addr + off, base, len),
6457baa5099SAl Viro 		memcpy(addr + off, base, len)
646d879cb83SAl Viro 	)
647d879cb83SAl Viro 
648d879cb83SAl Viro 	return bytes;
649d879cb83SAl Viro }
650aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache);
651d879cb83SAl Viro 
6520aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
653abd08d7dSDan Williams /**
654abd08d7dSDan Williams  * _copy_from_iter_flushcache - write destination through cpu cache
655abd08d7dSDan Williams  * @addr: destination kernel address
656abd08d7dSDan Williams  * @bytes: total transfer length
65744e55997SRandy Dunlap  * @i: source iterator
658abd08d7dSDan Williams  *
659abd08d7dSDan Williams  * The pmem driver arranges for filesystem-dax to use this facility via
660abd08d7dSDan Williams  * dax_copy_from_iter() for ensuring that writes to persistent memory
661abd08d7dSDan Williams  * are flushed through the CPU cache. It is differentiated from
662abd08d7dSDan Williams  * _copy_from_iter_nocache() in that guarantees all data is flushed for
663abd08d7dSDan Williams  * all iterator types. The _copy_from_iter_nocache() only attempts to
664abd08d7dSDan Williams  * bypass the cache for the ITER_IOVEC case, and on some archs may use
665abd08d7dSDan Williams  * instructions that strand dirty-data in the cache.
66644e55997SRandy Dunlap  *
66744e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
668abd08d7dSDan Williams  */
6696a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
6700aed55afSDan Williams {
67100e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
6720aed55afSDan Williams 		WARN_ON(1);
6730aed55afSDan Williams 		return 0;
6740aed55afSDan Williams 	}
6757baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6767baa5099SAl Viro 		__copy_from_user_flushcache(addr + off, base, len),
6777baa5099SAl Viro 		memcpy_flushcache(addr + off, base, len)
6780aed55afSDan Williams 	)
6790aed55afSDan Williams 
6800aed55afSDan Williams 	return bytes;
6810aed55afSDan Williams }
6826a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
6830aed55afSDan Williams #endif
6840aed55afSDan Williams 
68572e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
68672e809edSAl Viro {
6876daef95bSEric Dumazet 	struct page *head;
6886daef95bSEric Dumazet 	size_t v = n + offset;
6896daef95bSEric Dumazet 
6906daef95bSEric Dumazet 	/*
6916daef95bSEric Dumazet 	 * The general case needs to access the page order in order
6926daef95bSEric Dumazet 	 * to compute the page size.
6936daef95bSEric Dumazet 	 * However, we mostly deal with order-0 pages and thus can
6946daef95bSEric Dumazet 	 * avoid a possible cache line miss for requests that fit all
6956daef95bSEric Dumazet 	 * page orders.
6966daef95bSEric Dumazet 	 */
6976daef95bSEric Dumazet 	if (n <= v && v <= PAGE_SIZE)
6986daef95bSEric Dumazet 		return true;
6996daef95bSEric Dumazet 
7006daef95bSEric Dumazet 	head = compound_head(page);
7016daef95bSEric Dumazet 	v += (page - head) << PAGE_SHIFT;
702a90bcb86SPetar Penkov 
703a50b854eSMatthew Wilcox (Oracle) 	if (likely(n <= v && v <= (page_size(head))))
70472e809edSAl Viro 		return true;
70572e809edSAl Viro 	WARN_ON(1);
70672e809edSAl Viro 	return false;
70772e809edSAl Viro }
708cbbd26b8SAl Viro 
70908aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
71008aa6479SAl Viro 			 struct iov_iter *i)
71108aa6479SAl Viro {
71208aa6479SAl Viro 	size_t res = 0;
71308aa6479SAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes)))
71408aa6479SAl Viro 		return 0;
715*f0f6b614SAl Viro 	if (unlikely(iov_iter_is_pipe(i)))
716*f0f6b614SAl Viro 		return copy_page_to_iter_pipe(page, offset, bytes, i);
71708aa6479SAl Viro 	page += offset / PAGE_SIZE; // first subpage
71808aa6479SAl Viro 	offset %= PAGE_SIZE;
71908aa6479SAl Viro 	while (1) {
720*f0f6b614SAl Viro 		void *kaddr = kmap_local_page(page);
721*f0f6b614SAl Viro 		size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
722*f0f6b614SAl Viro 		n = _copy_to_iter(kaddr + offset, n, i);
723*f0f6b614SAl Viro 		kunmap_local(kaddr);
72408aa6479SAl Viro 		res += n;
72508aa6479SAl Viro 		bytes -= n;
72608aa6479SAl Viro 		if (!bytes || !n)
72708aa6479SAl Viro 			break;
72808aa6479SAl Viro 		offset += n;
72908aa6479SAl Viro 		if (offset == PAGE_SIZE) {
73008aa6479SAl Viro 			page++;
73108aa6479SAl Viro 			offset = 0;
73208aa6479SAl Viro 		}
73308aa6479SAl Viro 	}
73408aa6479SAl Viro 	return res;
73508aa6479SAl Viro }
736d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter);
737d879cb83SAl Viro 
738d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
739d879cb83SAl Viro 			 struct iov_iter *i)
740d879cb83SAl Viro {
74159bb69c6SAl Viro 	if (page_copy_sane(page, offset, bytes)) {
74255ca375cSAl Viro 		void *kaddr = kmap_local_page(page);
743aa28de27SAl Viro 		size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
74455ca375cSAl Viro 		kunmap_local(kaddr);
745d879cb83SAl Viro 		return wanted;
74628f38db7SAl Viro 	}
74728f38db7SAl Viro 	return 0;
748d879cb83SAl Viro }
749d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter);
750d879cb83SAl Viro 
751241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i)
752241699cdSAl Viro {
7538fad7767SAl Viro 	unsigned int chunk, off;
7548fad7767SAl Viro 
7558fad7767SAl Viro 	if (unlikely(bytes > i->count))
7568fad7767SAl Viro 		bytes = i->count;
7578fad7767SAl Viro 	if (unlikely(!bytes))
7588fad7767SAl Viro 		return 0;
759241699cdSAl Viro 
760241699cdSAl Viro 	if (!sanity(i))
761241699cdSAl Viro 		return 0;
762241699cdSAl Viro 
7638fad7767SAl Viro 	for (size_t n = bytes; n; n -= chunk) {
7648fad7767SAl Viro 		struct page *page = append_pipe(i, n, &off);
7658fad7767SAl Viro 		char *p;
766241699cdSAl Viro 
7678fad7767SAl Viro 		if (!page)
7688fad7767SAl Viro 			return bytes - n;
7698fad7767SAl Viro 		chunk = min_t(size_t, n, PAGE_SIZE - off);
7708fad7767SAl Viro 		p = kmap_local_page(page);
771893839fdSAl Viro 		memset(p + off, 0, chunk);
772893839fdSAl Viro 		kunmap_local(p);
7738fad7767SAl Viro 	}
774241699cdSAl Viro 	return bytes;
775241699cdSAl Viro }
776241699cdSAl Viro 
777d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
778d879cb83SAl Viro {
77900e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
780241699cdSAl Viro 		return pipe_zero(bytes, i);
7817baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, count,
7827baa5099SAl Viro 		clear_user(base, len),
7837baa5099SAl Viro 		memset(base, 0, len)
784d879cb83SAl Viro 	)
785d879cb83SAl Viro 
786d879cb83SAl Viro 	return bytes;
787d879cb83SAl Viro }
788d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero);
789d879cb83SAl Viro 
790f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
791f0b65f39SAl Viro 				  struct iov_iter *i)
792d879cb83SAl Viro {
793d879cb83SAl Viro 	char *kaddr = kmap_atomic(page), *p = kaddr + offset;
79472e809edSAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes))) {
79572e809edSAl Viro 		kunmap_atomic(kaddr);
79672e809edSAl Viro 		return 0;
79772e809edSAl Viro 	}
7989ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
799241699cdSAl Viro 		kunmap_atomic(kaddr);
800241699cdSAl Viro 		WARN_ON(1);
801241699cdSAl Viro 		return 0;
802241699cdSAl Viro 	}
8037baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
8047baa5099SAl Viro 		copyin(p + off, base, len),
8057baa5099SAl Viro 		memcpy(p + off, base, len)
806d879cb83SAl Viro 	)
807d879cb83SAl Viro 	kunmap_atomic(kaddr);
808d879cb83SAl Viro 	return bytes;
809d879cb83SAl Viro }
810f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic);
811d879cb83SAl Viro 
812b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size)
813b9dc6f65SAl Viro {
814b9dc6f65SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
81510f525a8SAl Viro 	int off = i->last_offset;
8168cefc107SDavid Howells 
8172c855de9SAl Viro 	if (!off && !size) {
8182c855de9SAl Viro 		pipe_discard_from(pipe, i->start_head); // discard everything
8192c855de9SAl Viro 		return;
820b9dc6f65SAl Viro 	}
821b9dc6f65SAl Viro 	i->count -= size;
8222c855de9SAl Viro 	while (1) {
8232c855de9SAl Viro 		struct pipe_buffer *buf = pipe_buf(pipe, i->head);
8242c855de9SAl Viro 		if (off) /* make it relative to the beginning of buffer */
82510f525a8SAl Viro 			size += abs(off) - buf->offset;
8262c855de9SAl Viro 		if (size <= buf->len) {
8272c855de9SAl Viro 			buf->len = size;
82810f525a8SAl Viro 			i->last_offset = last_offset(buf);
8292c855de9SAl Viro 			break;
8302c855de9SAl Viro 		}
8312c855de9SAl Viro 		size -= buf->len;
8322c855de9SAl Viro 		i->head++;
8332c855de9SAl Viro 		off = 0;
8342c855de9SAl Viro 	}
8352c855de9SAl Viro 	pipe_discard_from(pipe, i->head + 1); // discard everything past this one
836241699cdSAl Viro }
837241699cdSAl Viro 
83854c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
83954c8195bSPavel Begunkov {
84018fa9af7SAl Viro 	const struct bio_vec *bvec, *end;
84154c8195bSPavel Begunkov 
84218fa9af7SAl Viro 	if (!i->count)
84318fa9af7SAl Viro 		return;
84418fa9af7SAl Viro 	i->count -= size;
84554c8195bSPavel Begunkov 
84618fa9af7SAl Viro 	size += i->iov_offset;
84718fa9af7SAl Viro 
84818fa9af7SAl Viro 	for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
84918fa9af7SAl Viro 		if (likely(size < bvec->bv_len))
85018fa9af7SAl Viro 			break;
85118fa9af7SAl Viro 		size -= bvec->bv_len;
85218fa9af7SAl Viro 	}
85318fa9af7SAl Viro 	i->iov_offset = size;
85418fa9af7SAl Viro 	i->nr_segs -= bvec - i->bvec;
85518fa9af7SAl Viro 	i->bvec = bvec;
85654c8195bSPavel Begunkov }
85754c8195bSPavel Begunkov 
858185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
859185ac4d4SAl Viro {
860185ac4d4SAl Viro 	const struct iovec *iov, *end;
861185ac4d4SAl Viro 
862185ac4d4SAl Viro 	if (!i->count)
863185ac4d4SAl Viro 		return;
864185ac4d4SAl Viro 	i->count -= size;
865185ac4d4SAl Viro 
866185ac4d4SAl Viro 	size += i->iov_offset; // from beginning of current segment
867185ac4d4SAl Viro 	for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
868185ac4d4SAl Viro 		if (likely(size < iov->iov_len))
869185ac4d4SAl Viro 			break;
870185ac4d4SAl Viro 		size -= iov->iov_len;
871185ac4d4SAl Viro 	}
872185ac4d4SAl Viro 	i->iov_offset = size;
873185ac4d4SAl Viro 	i->nr_segs -= iov - i->iov;
874185ac4d4SAl Viro 	i->iov = iov;
875185ac4d4SAl Viro }
876185ac4d4SAl Viro 
877d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size)
878d879cb83SAl Viro {
8793b3fc051SAl Viro 	if (unlikely(i->count < size))
8803b3fc051SAl Viro 		size = i->count;
881fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
882fcb14cb1SAl Viro 		i->iov_offset += size;
883fcb14cb1SAl Viro 		i->count -= size;
884fcb14cb1SAl Viro 	} else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
885185ac4d4SAl Viro 		/* iovec and kvec have identical layouts */
886185ac4d4SAl Viro 		iov_iter_iovec_advance(i, size);
887185ac4d4SAl Viro 	} else if (iov_iter_is_bvec(i)) {
888185ac4d4SAl Viro 		iov_iter_bvec_advance(i, size);
889185ac4d4SAl Viro 	} else if (iov_iter_is_pipe(i)) {
890241699cdSAl Viro 		pipe_advance(i, size);
891185ac4d4SAl Viro 	} else if (iov_iter_is_discard(i)) {
892185ac4d4SAl Viro 		i->count -= size;
8937ff50620SDavid Howells 	}
894d879cb83SAl Viro }
895d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance);
896d879cb83SAl Viro 
89727c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll)
89827c0e374SAl Viro {
89927c0e374SAl Viro 	if (!unroll)
90027c0e374SAl Viro 		return;
9015b47d59aSAl Viro 	if (WARN_ON(unroll > MAX_RW_COUNT))
9025b47d59aSAl Viro 		return;
90327c0e374SAl Viro 	i->count += unroll;
90400e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
90527c0e374SAl Viro 		struct pipe_inode_info *pipe = i->pipe;
90692acdc4fSAl Viro 		unsigned int head = pipe->head;
90792acdc4fSAl Viro 
90892acdc4fSAl Viro 		while (head > i->start_head) {
90992acdc4fSAl Viro 			struct pipe_buffer *b = pipe_buf(pipe, --head);
91092acdc4fSAl Viro 			if (unroll < b->len) {
91192acdc4fSAl Viro 				b->len -= unroll;
91210f525a8SAl Viro 				i->last_offset = last_offset(b);
91392acdc4fSAl Viro 				i->head = head;
91492acdc4fSAl Viro 				return;
91527c0e374SAl Viro 			}
91692acdc4fSAl Viro 			unroll -= b->len;
91792acdc4fSAl Viro 			pipe_buf_release(pipe, b);
91892acdc4fSAl Viro 			pipe->head--;
91927c0e374SAl Viro 		}
92010f525a8SAl Viro 		i->last_offset = 0;
92192acdc4fSAl Viro 		i->head = head;
92227c0e374SAl Viro 		return;
92327c0e374SAl Viro 	}
9249ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_discard(i)))
9259ea9ce04SDavid Howells 		return;
92627c0e374SAl Viro 	if (unroll <= i->iov_offset) {
92727c0e374SAl Viro 		i->iov_offset -= unroll;
92827c0e374SAl Viro 		return;
92927c0e374SAl Viro 	}
93027c0e374SAl Viro 	unroll -= i->iov_offset;
931fcb14cb1SAl Viro 	if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
9327ff50620SDavid Howells 		BUG(); /* We should never go beyond the start of the specified
9337ff50620SDavid Howells 			* range since we might then be straying into pages that
9347ff50620SDavid Howells 			* aren't pinned.
9357ff50620SDavid Howells 			*/
9367ff50620SDavid Howells 	} else if (iov_iter_is_bvec(i)) {
93727c0e374SAl Viro 		const struct bio_vec *bvec = i->bvec;
93827c0e374SAl Viro 		while (1) {
93927c0e374SAl Viro 			size_t n = (--bvec)->bv_len;
94027c0e374SAl Viro 			i->nr_segs++;
94127c0e374SAl Viro 			if (unroll <= n) {
94227c0e374SAl Viro 				i->bvec = bvec;
94327c0e374SAl Viro 				i->iov_offset = n - unroll;
94427c0e374SAl Viro 				return;
94527c0e374SAl Viro 			}
94627c0e374SAl Viro 			unroll -= n;
94727c0e374SAl Viro 		}
94827c0e374SAl Viro 	} else { /* same logics for iovec and kvec */
94927c0e374SAl Viro 		const struct iovec *iov = i->iov;
95027c0e374SAl Viro 		while (1) {
95127c0e374SAl Viro 			size_t n = (--iov)->iov_len;
95227c0e374SAl Viro 			i->nr_segs++;
95327c0e374SAl Viro 			if (unroll <= n) {
95427c0e374SAl Viro 				i->iov = iov;
95527c0e374SAl Viro 				i->iov_offset = n - unroll;
95627c0e374SAl Viro 				return;
95727c0e374SAl Viro 			}
95827c0e374SAl Viro 			unroll -= n;
95927c0e374SAl Viro 		}
96027c0e374SAl Viro 	}
96127c0e374SAl Viro }
96227c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert);
96327c0e374SAl Viro 
964d879cb83SAl Viro /*
965d879cb83SAl Viro  * Return the count of just the current iov_iter segment.
966d879cb83SAl Viro  */
967d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i)
968d879cb83SAl Viro {
96928f38db7SAl Viro 	if (i->nr_segs > 1) {
97028f38db7SAl Viro 		if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
97128f38db7SAl Viro 			return min(i->count, i->iov->iov_len - i->iov_offset);
9727ff50620SDavid Howells 		if (iov_iter_is_bvec(i))
973d879cb83SAl Viro 			return min(i->count, i->bvec->bv_len - i->iov_offset);
97428f38db7SAl Viro 	}
97528f38db7SAl Viro 	return i->count;
976d879cb83SAl Viro }
977d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count);
978d879cb83SAl Viro 
979aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
980d879cb83SAl Viro 			const struct kvec *kvec, unsigned long nr_segs,
981d879cb83SAl Viro 			size_t count)
982d879cb83SAl Viro {
983aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
9848cd54c1cSAl Viro 	*i = (struct iov_iter){
9858cd54c1cSAl Viro 		.iter_type = ITER_KVEC,
9868cd54c1cSAl Viro 		.data_source = direction,
9878cd54c1cSAl Viro 		.kvec = kvec,
9888cd54c1cSAl Viro 		.nr_segs = nr_segs,
9898cd54c1cSAl Viro 		.iov_offset = 0,
9908cd54c1cSAl Viro 		.count = count
9918cd54c1cSAl Viro 	};
992d879cb83SAl Viro }
993d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec);
994d879cb83SAl Viro 
995aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
996d879cb83SAl Viro 			const struct bio_vec *bvec, unsigned long nr_segs,
997d879cb83SAl Viro 			size_t count)
998d879cb83SAl Viro {
999aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
10008cd54c1cSAl Viro 	*i = (struct iov_iter){
10018cd54c1cSAl Viro 		.iter_type = ITER_BVEC,
10028cd54c1cSAl Viro 		.data_source = direction,
10038cd54c1cSAl Viro 		.bvec = bvec,
10048cd54c1cSAl Viro 		.nr_segs = nr_segs,
10058cd54c1cSAl Viro 		.iov_offset = 0,
10068cd54c1cSAl Viro 		.count = count
10078cd54c1cSAl Viro 	};
1008d879cb83SAl Viro }
1009d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec);
1010d879cb83SAl Viro 
1011aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1012241699cdSAl Viro 			struct pipe_inode_info *pipe,
1013241699cdSAl Viro 			size_t count)
1014241699cdSAl Viro {
1015aa563d7bSDavid Howells 	BUG_ON(direction != READ);
10168cefc107SDavid Howells 	WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
10178cd54c1cSAl Viro 	*i = (struct iov_iter){
10188cd54c1cSAl Viro 		.iter_type = ITER_PIPE,
10198cd54c1cSAl Viro 		.data_source = false,
10208cd54c1cSAl Viro 		.pipe = pipe,
10218cd54c1cSAl Viro 		.head = pipe->head,
10228cd54c1cSAl Viro 		.start_head = pipe->head,
102310f525a8SAl Viro 		.last_offset = 0,
10248cd54c1cSAl Viro 		.count = count
10258cd54c1cSAl Viro 	};
1026241699cdSAl Viro }
1027241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe);
1028241699cdSAl Viro 
10299ea9ce04SDavid Howells /**
10307ff50620SDavid Howells  * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
10317ff50620SDavid Howells  * @i: The iterator to initialise.
10327ff50620SDavid Howells  * @direction: The direction of the transfer.
10337ff50620SDavid Howells  * @xarray: The xarray to access.
10347ff50620SDavid Howells  * @start: The start file position.
10357ff50620SDavid Howells  * @count: The size of the I/O buffer in bytes.
10367ff50620SDavid Howells  *
10377ff50620SDavid Howells  * Set up an I/O iterator to either draw data out of the pages attached to an
10387ff50620SDavid Howells  * inode or to inject data into those pages.  The pages *must* be prevented
10397ff50620SDavid Howells  * from evaporation, either by taking a ref on them or locking them by the
10407ff50620SDavid Howells  * caller.
10417ff50620SDavid Howells  */
10427ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
10437ff50620SDavid Howells 		     struct xarray *xarray, loff_t start, size_t count)
10447ff50620SDavid Howells {
10457ff50620SDavid Howells 	BUG_ON(direction & ~1);
10468cd54c1cSAl Viro 	*i = (struct iov_iter) {
10478cd54c1cSAl Viro 		.iter_type = ITER_XARRAY,
10488cd54c1cSAl Viro 		.data_source = direction,
10498cd54c1cSAl Viro 		.xarray = xarray,
10508cd54c1cSAl Viro 		.xarray_start = start,
10518cd54c1cSAl Viro 		.count = count,
10528cd54c1cSAl Viro 		.iov_offset = 0
10538cd54c1cSAl Viro 	};
10547ff50620SDavid Howells }
10557ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray);
10567ff50620SDavid Howells 
10577ff50620SDavid Howells /**
10589ea9ce04SDavid Howells  * iov_iter_discard - Initialise an I/O iterator that discards data
10599ea9ce04SDavid Howells  * @i: The iterator to initialise.
10609ea9ce04SDavid Howells  * @direction: The direction of the transfer.
10619ea9ce04SDavid Howells  * @count: The size of the I/O buffer in bytes.
10629ea9ce04SDavid Howells  *
10639ea9ce04SDavid Howells  * Set up an I/O iterator that just discards everything that's written to it.
10649ea9ce04SDavid Howells  * It's only available as a READ iterator.
10659ea9ce04SDavid Howells  */
10669ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
10679ea9ce04SDavid Howells {
10689ea9ce04SDavid Howells 	BUG_ON(direction != READ);
10698cd54c1cSAl Viro 	*i = (struct iov_iter){
10708cd54c1cSAl Viro 		.iter_type = ITER_DISCARD,
10718cd54c1cSAl Viro 		.data_source = false,
10728cd54c1cSAl Viro 		.count = count,
10738cd54c1cSAl Viro 		.iov_offset = 0
10748cd54c1cSAl Viro 	};
10759ea9ce04SDavid Howells }
10769ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard);
10779ea9ce04SDavid Howells 
1078cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
1079cfa320f7SKeith Busch 				   unsigned len_mask)
1080cfa320f7SKeith Busch {
1081cfa320f7SKeith Busch 	size_t size = i->count;
1082cfa320f7SKeith Busch 	size_t skip = i->iov_offset;
1083cfa320f7SKeith Busch 	unsigned k;
1084cfa320f7SKeith Busch 
1085cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
1086cfa320f7SKeith Busch 		size_t len = i->iov[k].iov_len - skip;
1087cfa320f7SKeith Busch 
1088cfa320f7SKeith Busch 		if (len > size)
1089cfa320f7SKeith Busch 			len = size;
1090cfa320f7SKeith Busch 		if (len & len_mask)
1091cfa320f7SKeith Busch 			return false;
1092cfa320f7SKeith Busch 		if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask)
1093cfa320f7SKeith Busch 			return false;
1094cfa320f7SKeith Busch 
1095cfa320f7SKeith Busch 		size -= len;
1096cfa320f7SKeith Busch 		if (!size)
1097cfa320f7SKeith Busch 			break;
1098cfa320f7SKeith Busch 	}
1099cfa320f7SKeith Busch 	return true;
1100cfa320f7SKeith Busch }
1101cfa320f7SKeith Busch 
1102cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
1103cfa320f7SKeith Busch 				  unsigned len_mask)
1104cfa320f7SKeith Busch {
1105cfa320f7SKeith Busch 	size_t size = i->count;
1106cfa320f7SKeith Busch 	unsigned skip = i->iov_offset;
1107cfa320f7SKeith Busch 	unsigned k;
1108cfa320f7SKeith Busch 
1109cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
1110cfa320f7SKeith Busch 		size_t len = i->bvec[k].bv_len - skip;
1111cfa320f7SKeith Busch 
1112cfa320f7SKeith Busch 		if (len > size)
1113cfa320f7SKeith Busch 			len = size;
1114cfa320f7SKeith Busch 		if (len & len_mask)
1115cfa320f7SKeith Busch 			return false;
1116cfa320f7SKeith Busch 		if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
1117cfa320f7SKeith Busch 			return false;
1118cfa320f7SKeith Busch 
1119cfa320f7SKeith Busch 		size -= len;
1120cfa320f7SKeith Busch 		if (!size)
1121cfa320f7SKeith Busch 			break;
1122cfa320f7SKeith Busch 	}
1123cfa320f7SKeith Busch 	return true;
1124cfa320f7SKeith Busch }
1125cfa320f7SKeith Busch 
1126cfa320f7SKeith Busch /**
1127cfa320f7SKeith Busch  * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
1128cfa320f7SKeith Busch  * 	are aligned to the parameters.
1129cfa320f7SKeith Busch  *
1130cfa320f7SKeith Busch  * @i: &struct iov_iter to restore
1131cfa320f7SKeith Busch  * @addr_mask: bit mask to check against the iov element's addresses
1132cfa320f7SKeith Busch  * @len_mask: bit mask to check against the iov element's lengths
1133cfa320f7SKeith Busch  *
1134cfa320f7SKeith Busch  * Return: false if any addresses or lengths intersect with the provided masks
1135cfa320f7SKeith Busch  */
1136cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
1137cfa320f7SKeith Busch 			 unsigned len_mask)
1138cfa320f7SKeith Busch {
1139fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1140fcb14cb1SAl Viro 		if (i->count & len_mask)
1141fcb14cb1SAl Viro 			return false;
1142fcb14cb1SAl Viro 		if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
1143fcb14cb1SAl Viro 			return false;
1144fcb14cb1SAl Viro 		return true;
1145fcb14cb1SAl Viro 	}
1146fcb14cb1SAl Viro 
1147cfa320f7SKeith Busch 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1148cfa320f7SKeith Busch 		return iov_iter_aligned_iovec(i, addr_mask, len_mask);
1149cfa320f7SKeith Busch 
1150cfa320f7SKeith Busch 	if (iov_iter_is_bvec(i))
1151cfa320f7SKeith Busch 		return iov_iter_aligned_bvec(i, addr_mask, len_mask);
1152cfa320f7SKeith Busch 
1153cfa320f7SKeith Busch 	if (iov_iter_is_pipe(i)) {
1154cfa320f7SKeith Busch 		size_t size = i->count;
1155cfa320f7SKeith Busch 
1156cfa320f7SKeith Busch 		if (size & len_mask)
1157cfa320f7SKeith Busch 			return false;
115810f525a8SAl Viro 		if (size && i->last_offset > 0) {
115910f525a8SAl Viro 			if (i->last_offset & addr_mask)
1160cfa320f7SKeith Busch 				return false;
1161cfa320f7SKeith Busch 		}
1162cfa320f7SKeith Busch 
1163cfa320f7SKeith Busch 		return true;
1164cfa320f7SKeith Busch 	}
1165cfa320f7SKeith Busch 
1166cfa320f7SKeith Busch 	if (iov_iter_is_xarray(i)) {
1167cfa320f7SKeith Busch 		if (i->count & len_mask)
1168cfa320f7SKeith Busch 			return false;
1169cfa320f7SKeith Busch 		if ((i->xarray_start + i->iov_offset) & addr_mask)
1170cfa320f7SKeith Busch 			return false;
1171cfa320f7SKeith Busch 	}
1172cfa320f7SKeith Busch 
1173cfa320f7SKeith Busch 	return true;
1174cfa320f7SKeith Busch }
1175cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
1176cfa320f7SKeith Busch 
11779221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1178d879cb83SAl Viro {
1179d879cb83SAl Viro 	unsigned long res = 0;
1180d879cb83SAl Viro 	size_t size = i->count;
11819221d2e3SAl Viro 	size_t skip = i->iov_offset;
11829221d2e3SAl Viro 	unsigned k;
1183d879cb83SAl Viro 
11849221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
11859221d2e3SAl Viro 		size_t len = i->iov[k].iov_len - skip;
11869221d2e3SAl Viro 		if (len) {
11879221d2e3SAl Viro 			res |= (unsigned long)i->iov[k].iov_base + skip;
11889221d2e3SAl Viro 			if (len > size)
11899221d2e3SAl Viro 				len = size;
11909221d2e3SAl Viro 			res |= len;
11919221d2e3SAl Viro 			size -= len;
11929221d2e3SAl Viro 			if (!size)
11939221d2e3SAl Viro 				break;
11949221d2e3SAl Viro 		}
11959221d2e3SAl Viro 	}
11969221d2e3SAl Viro 	return res;
11979221d2e3SAl Viro }
11989221d2e3SAl Viro 
11999221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
12009221d2e3SAl Viro {
12019221d2e3SAl Viro 	unsigned res = 0;
12029221d2e3SAl Viro 	size_t size = i->count;
12039221d2e3SAl Viro 	unsigned skip = i->iov_offset;
12049221d2e3SAl Viro 	unsigned k;
12059221d2e3SAl Viro 
12069221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
12079221d2e3SAl Viro 		size_t len = i->bvec[k].bv_len - skip;
12089221d2e3SAl Viro 		res |= (unsigned long)i->bvec[k].bv_offset + skip;
12099221d2e3SAl Viro 		if (len > size)
12109221d2e3SAl Viro 			len = size;
12119221d2e3SAl Viro 		res |= len;
12129221d2e3SAl Viro 		size -= len;
12139221d2e3SAl Viro 		if (!size)
12149221d2e3SAl Viro 			break;
12159221d2e3SAl Viro 	}
12169221d2e3SAl Viro 	return res;
12179221d2e3SAl Viro }
12189221d2e3SAl Viro 
12199221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i)
12209221d2e3SAl Viro {
1221fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1222fcb14cb1SAl Viro 		size_t size = i->count;
1223fcb14cb1SAl Viro 		if (size)
1224fcb14cb1SAl Viro 			return ((unsigned long)i->ubuf + i->iov_offset) | size;
1225fcb14cb1SAl Viro 		return 0;
1226fcb14cb1SAl Viro 	}
1227fcb14cb1SAl Viro 
12289221d2e3SAl Viro 	/* iovec and kvec have identical layouts */
12299221d2e3SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
12309221d2e3SAl Viro 		return iov_iter_alignment_iovec(i);
12319221d2e3SAl Viro 
12329221d2e3SAl Viro 	if (iov_iter_is_bvec(i))
12339221d2e3SAl Viro 		return iov_iter_alignment_bvec(i);
12349221d2e3SAl Viro 
12359221d2e3SAl Viro 	if (iov_iter_is_pipe(i)) {
12369221d2e3SAl Viro 		size_t size = i->count;
1237e0ff126eSJan Kara 
123810f525a8SAl Viro 		if (size && i->last_offset > 0)
123910f525a8SAl Viro 			return size | i->last_offset;
1240241699cdSAl Viro 		return size;
1241241699cdSAl Viro 	}
12429221d2e3SAl Viro 
12439221d2e3SAl Viro 	if (iov_iter_is_xarray(i))
12443d14ec1fSDavid Howells 		return (i->xarray_start + i->iov_offset) | i->count;
12459221d2e3SAl Viro 
12469221d2e3SAl Viro 	return 0;
1247d879cb83SAl Viro }
1248d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment);
1249d879cb83SAl Viro 
1250357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1251357f435dSAl Viro {
1252357f435dSAl Viro 	unsigned long res = 0;
1253610c7a71SAl Viro 	unsigned long v = 0;
1254357f435dSAl Viro 	size_t size = i->count;
1255610c7a71SAl Viro 	unsigned k;
1256357f435dSAl Viro 
1257fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1258fcb14cb1SAl Viro 		return 0;
1259fcb14cb1SAl Viro 
1260610c7a71SAl Viro 	if (WARN_ON(!iter_is_iovec(i)))
1261241699cdSAl Viro 		return ~0U;
1262241699cdSAl Viro 
1263610c7a71SAl Viro 	for (k = 0; k < i->nr_segs; k++) {
1264610c7a71SAl Viro 		if (i->iov[k].iov_len) {
1265610c7a71SAl Viro 			unsigned long base = (unsigned long)i->iov[k].iov_base;
1266610c7a71SAl Viro 			if (v) // if not the first one
1267610c7a71SAl Viro 				res |= base | v; // this start | previous end
1268610c7a71SAl Viro 			v = base + i->iov[k].iov_len;
1269610c7a71SAl Viro 			if (size <= i->iov[k].iov_len)
1270610c7a71SAl Viro 				break;
1271610c7a71SAl Viro 			size -= i->iov[k].iov_len;
1272610c7a71SAl Viro 		}
1273610c7a71SAl Viro 	}
1274357f435dSAl Viro 	return res;
1275357f435dSAl Viro }
1276357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment);
1277357f435dSAl Viro 
12783cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size,
12793cf42da3SAl Viro 			    size_t start, unsigned int maxpages)
1280acbdeb83SAl Viro {
12813cf42da3SAl Viro 	unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE);
12823cf42da3SAl Viro 
12833cf42da3SAl Viro 	if (count > maxpages)
12843cf42da3SAl Viro 		count = maxpages;
12853cf42da3SAl Viro 	WARN_ON(!count);	// caller should've prevented that
12863cf42da3SAl Viro 	if (!*res) {
12873cf42da3SAl Viro 		*res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
12883cf42da3SAl Viro 		if (!*res)
12893cf42da3SAl Viro 			return 0;
12903cf42da3SAl Viro 	}
12913cf42da3SAl Viro 	return count;
1292acbdeb83SAl Viro }
1293acbdeb83SAl Viro 
129485200084SAl Viro static ssize_t pipe_get_pages(struct iov_iter *i,
129585200084SAl Viro 		   struct page ***pages, size_t maxsize, unsigned maxpages,
129685200084SAl Viro 		   size_t *start)
1297241699cdSAl Viro {
1298746de1f8SAl Viro 	unsigned int npages, count, off, chunk;
129985200084SAl Viro 	struct page **p;
1300746de1f8SAl Viro 	size_t left;
1301241699cdSAl Viro 
130285200084SAl Viro 	if (!sanity(i))
130385200084SAl Viro 		return -EFAULT;
130485200084SAl Viro 
130585200084SAl Viro 	*start = off = pipe_npages(i, &npages);
13063cf42da3SAl Viro 	if (!npages)
13073cf42da3SAl Viro 		return -EFAULT;
13083cf42da3SAl Viro 	count = want_pages_array(pages, maxsize, off, min(npages, maxpages));
13093cf42da3SAl Viro 	if (!count)
131085200084SAl Viro 		return -ENOMEM;
13113cf42da3SAl Viro 	p = *pages;
1312746de1f8SAl Viro 	for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) {
1313746de1f8SAl Viro 		struct page *page = append_pipe(i, left, &off);
1314e3b42964SAl Viro 		if (!page)
1315e3b42964SAl Viro 			break;
1316746de1f8SAl Viro 		chunk = min_t(size_t, left, PAGE_SIZE - off);
131785200084SAl Viro 		get_page(*p++ = page);
1318e3b42964SAl Viro 	}
131985200084SAl Viro 	if (!npages)
1320241699cdSAl Viro 		return -EFAULT;
1321746de1f8SAl Viro 	return maxsize - left;
1322241699cdSAl Viro }
1323241699cdSAl Viro 
13247ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
13257ff50620SDavid Howells 					  pgoff_t index, unsigned int nr_pages)
13267ff50620SDavid Howells {
13277ff50620SDavid Howells 	XA_STATE(xas, xa, index);
13287ff50620SDavid Howells 	struct page *page;
13297ff50620SDavid Howells 	unsigned int ret = 0;
13307ff50620SDavid Howells 
13317ff50620SDavid Howells 	rcu_read_lock();
13327ff50620SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
13337ff50620SDavid Howells 		if (xas_retry(&xas, page))
13347ff50620SDavid Howells 			continue;
13357ff50620SDavid Howells 
13367ff50620SDavid Howells 		/* Has the page moved or been split? */
13377ff50620SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
13387ff50620SDavid Howells 			xas_reset(&xas);
13397ff50620SDavid Howells 			continue;
13407ff50620SDavid Howells 		}
13417ff50620SDavid Howells 
13427ff50620SDavid Howells 		pages[ret] = find_subpage(page, xas.xa_index);
13437ff50620SDavid Howells 		get_page(pages[ret]);
13447ff50620SDavid Howells 		if (++ret == nr_pages)
13457ff50620SDavid Howells 			break;
13467ff50620SDavid Howells 	}
13477ff50620SDavid Howells 	rcu_read_unlock();
13487ff50620SDavid Howells 	return ret;
13497ff50620SDavid Howells }
13507ff50620SDavid Howells 
13517ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i,
135268fe506fSAl Viro 				     struct page ***pages, size_t maxsize,
13537ff50620SDavid Howells 				     unsigned maxpages, size_t *_start_offset)
13547ff50620SDavid Howells {
13553cf42da3SAl Viro 	unsigned nr, offset, count;
13563cf42da3SAl Viro 	pgoff_t index;
13577ff50620SDavid Howells 	loff_t pos;
13587ff50620SDavid Howells 
13597ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
13607ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
13617ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
13627ff50620SDavid Howells 	*_start_offset = offset;
13637ff50620SDavid Howells 
13643cf42da3SAl Viro 	count = want_pages_array(pages, maxsize, offset, maxpages);
13653cf42da3SAl Viro 	if (!count)
136668fe506fSAl Viro 		return -ENOMEM;
136768fe506fSAl Viro 	nr = iter_xarray_populate_pages(*pages, i->xarray, index, count);
13687ff50620SDavid Howells 	if (nr == 0)
13697ff50620SDavid Howells 		return 0;
13707ff50620SDavid Howells 
1371eba2d3d7SAl Viro 	maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1372310d9d5aSAl Viro 	i->iov_offset += maxsize;
1373310d9d5aSAl Viro 	i->count -= maxsize;
1374eba2d3d7SAl Viro 	return maxsize;
13757ff50620SDavid Howells }
13767ff50620SDavid Howells 
1377fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
1378dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
13793d671ca6SAl Viro {
13803d671ca6SAl Viro 	size_t skip;
13813d671ca6SAl Viro 	long k;
13823d671ca6SAl Viro 
1383fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1384fcb14cb1SAl Viro 		return (unsigned long)i->ubuf + i->iov_offset;
1385fcb14cb1SAl Viro 
13863d671ca6SAl Viro 	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
13873d671ca6SAl Viro 		size_t len = i->iov[k].iov_len - skip;
13883d671ca6SAl Viro 
13893d671ca6SAl Viro 		if (unlikely(!len))
13903d671ca6SAl Viro 			continue;
139159dbd7d0SAl Viro 		if (*size > len)
13923d671ca6SAl Viro 			*size = len;
1393dd45ab9dSAl Viro 		return (unsigned long)i->iov[k].iov_base + skip;
13943d671ca6SAl Viro 	}
13953d671ca6SAl Viro 	BUG(); // if it had been empty, we wouldn't get called
13963d671ca6SAl Viro }
13973d671ca6SAl Viro 
13983d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */
13993d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i,
140059dbd7d0SAl Viro 				       size_t *size, size_t *start)
14013d671ca6SAl Viro {
14023d671ca6SAl Viro 	struct page *page;
14033d671ca6SAl Viro 	size_t skip = i->iov_offset, len;
14043d671ca6SAl Viro 
14053d671ca6SAl Viro 	len = i->bvec->bv_len - skip;
140659dbd7d0SAl Viro 	if (*size > len)
140759dbd7d0SAl Viro 		*size = len;
14083d671ca6SAl Viro 	skip += i->bvec->bv_offset;
14093d671ca6SAl Viro 	page = i->bvec->bv_page + skip / PAGE_SIZE;
1410dda8e5d1SAl Viro 	*start = skip % PAGE_SIZE;
14113d671ca6SAl Viro 	return page;
14123d671ca6SAl Viro }
14133d671ca6SAl Viro 
141491329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
1415d879cb83SAl Viro 		   struct page ***pages, size_t maxsize,
1416451c0ba9SAl Viro 		   unsigned int maxpages, size_t *start)
1417d879cb83SAl Viro {
14183cf42da3SAl Viro 	unsigned int n;
1419d879cb83SAl Viro 
1420d879cb83SAl Viro 	if (maxsize > i->count)
1421d879cb83SAl Viro 		maxsize = i->count;
14223d671ca6SAl Viro 	if (!maxsize)
14233d671ca6SAl Viro 		return 0;
14247392ed17SAl Viro 	if (maxsize > MAX_RW_COUNT)
14257392ed17SAl Viro 		maxsize = MAX_RW_COUNT;
1426d879cb83SAl Viro 
1427fcb14cb1SAl Viro 	if (likely(user_backed_iter(i))) {
14283337ab08SAndreas Gruenbacher 		unsigned int gup_flags = 0;
14293d671ca6SAl Viro 		unsigned long addr;
14303cf42da3SAl Viro 		int res;
14319ea9ce04SDavid Howells 
14323337ab08SAndreas Gruenbacher 		if (iov_iter_rw(i) != WRITE)
14333337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_WRITE;
14343337ab08SAndreas Gruenbacher 		if (i->nofault)
14353337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_NOFAULT;
14363337ab08SAndreas Gruenbacher 
1437dd45ab9dSAl Viro 		addr = first_iovec_segment(i, &maxsize);
1438dd45ab9dSAl Viro 		*start = addr % PAGE_SIZE;
1439dd45ab9dSAl Viro 		addr &= PAGE_MASK;
14403cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
14413cf42da3SAl Viro 		if (!n)
1442d879cb83SAl Viro 			return -ENOMEM;
1443451c0ba9SAl Viro 		res = get_user_pages_fast(addr, n, gup_flags, *pages);
144491329559SAl Viro 		if (unlikely(res <= 0))
1445d879cb83SAl Viro 			return res;
1446eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1447eba2d3d7SAl Viro 		iov_iter_advance(i, maxsize);
1448eba2d3d7SAl Viro 		return maxsize;
14493d671ca6SAl Viro 	}
14503d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
1451451c0ba9SAl Viro 		struct page **p;
14523d671ca6SAl Viro 		struct page *page;
14533d671ca6SAl Viro 
145459dbd7d0SAl Viro 		page = first_bvec_segment(i, &maxsize, start);
14553cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
14563cf42da3SAl Viro 		if (!n)
1457d879cb83SAl Viro 			return -ENOMEM;
14583cf42da3SAl Viro 		p = *pages;
1459dda8e5d1SAl Viro 		for (int k = 0; k < n; k++)
1460eba2d3d7SAl Viro 			get_page(p[k] = page + k);
1461eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1462310d9d5aSAl Viro 		i->count -= maxsize;
1463310d9d5aSAl Viro 		i->iov_offset += maxsize;
1464310d9d5aSAl Viro 		if (i->iov_offset == i->bvec->bv_len) {
1465310d9d5aSAl Viro 			i->iov_offset = 0;
1466310d9d5aSAl Viro 			i->bvec++;
1467310d9d5aSAl Viro 			i->nr_segs--;
1468310d9d5aSAl Viro 		}
1469eba2d3d7SAl Viro 		return maxsize;
14703d671ca6SAl Viro 	}
14713d671ca6SAl Viro 	if (iov_iter_is_pipe(i))
1472451c0ba9SAl Viro 		return pipe_get_pages(i, pages, maxsize, maxpages, start);
14733d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
1474451c0ba9SAl Viro 		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1475d879cb83SAl Viro 	return -EFAULT;
1476d879cb83SAl Viro }
147791329559SAl Viro 
1478eba2d3d7SAl Viro ssize_t iov_iter_get_pages2(struct iov_iter *i,
1479451c0ba9SAl Viro 		   struct page **pages, size_t maxsize, unsigned maxpages,
1480451c0ba9SAl Viro 		   size_t *start)
1481451c0ba9SAl Viro {
1482451c0ba9SAl Viro 	if (!maxpages)
1483451c0ba9SAl Viro 		return 0;
1484451c0ba9SAl Viro 	BUG_ON(!pages);
1485451c0ba9SAl Viro 
1486451c0ba9SAl Viro 	return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, start);
1487451c0ba9SAl Viro }
1488eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2);
1489451c0ba9SAl Viro 
1490eba2d3d7SAl Viro ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i,
149191329559SAl Viro 		   struct page ***pages, size_t maxsize,
149291329559SAl Viro 		   size_t *start)
149391329559SAl Viro {
149491329559SAl Viro 	ssize_t len;
149591329559SAl Viro 
149691329559SAl Viro 	*pages = NULL;
149791329559SAl Viro 
1498451c0ba9SAl Viro 	len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start);
149991329559SAl Viro 	if (len <= 0) {
150091329559SAl Viro 		kvfree(*pages);
150191329559SAl Viro 		*pages = NULL;
150291329559SAl Viro 	}
150391329559SAl Viro 	return len;
150491329559SAl Viro }
1505eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2);
1506d879cb83SAl Viro 
1507d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1508d879cb83SAl Viro 			       struct iov_iter *i)
1509d879cb83SAl Viro {
1510d879cb83SAl Viro 	__wsum sum, next;
1511d879cb83SAl Viro 	sum = *csum;
15129ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1513241699cdSAl Viro 		WARN_ON(1);
1514241699cdSAl Viro 		return 0;
1515241699cdSAl Viro 	}
15167baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off, ({
15177baa5099SAl Viro 		next = csum_and_copy_from_user(base, addr + off, len);
1518d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
15197baa5099SAl Viro 		next ? 0 : len;
1520d879cb83SAl Viro 	}), ({
15217baa5099SAl Viro 		sum = csum_and_memcpy(addr + off, base, len, sum, off);
1522d879cb83SAl Viro 	})
1523d879cb83SAl Viro 	)
1524d879cb83SAl Viro 	*csum = sum;
1525d879cb83SAl Viro 	return bytes;
1526d879cb83SAl Viro }
1527d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter);
1528d879cb83SAl Viro 
152952cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1530d879cb83SAl Viro 			     struct iov_iter *i)
1531d879cb83SAl Viro {
153252cbd23aSWillem de Bruijn 	struct csum_state *csstate = _csstate;
1533d879cb83SAl Viro 	__wsum sum, next;
153478e1f386SAl Viro 
153578e1f386SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
1536241699cdSAl Viro 		WARN_ON(1);	/* for now */
1537241699cdSAl Viro 		return 0;
1538241699cdSAl Viro 	}
15396852df12SAl Viro 
15406852df12SAl Viro 	sum = csum_shift(csstate->csum, csstate->off);
15416852df12SAl Viro 	if (unlikely(iov_iter_is_pipe(i)))
15426852df12SAl Viro 		bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
15436852df12SAl Viro 	else iterate_and_advance(i, bytes, base, len, off, ({
15447baa5099SAl Viro 		next = csum_and_copy_to_user(addr + off, base, len);
1545d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
15467baa5099SAl Viro 		next ? 0 : len;
1547d879cb83SAl Viro 	}), ({
15487baa5099SAl Viro 		sum = csum_and_memcpy(base, addr + off, len, sum, off);
1549d879cb83SAl Viro 	})
1550d879cb83SAl Viro 	)
1551594e450bSAl Viro 	csstate->csum = csum_shift(sum, csstate->off);
1552594e450bSAl Viro 	csstate->off += bytes;
1553d879cb83SAl Viro 	return bytes;
1554d879cb83SAl Viro }
1555d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter);
1556d879cb83SAl Viro 
1557d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1558d05f4435SSagi Grimberg 		struct iov_iter *i)
1559d05f4435SSagi Grimberg {
15607999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH
1561d05f4435SSagi Grimberg 	struct ahash_request *hash = hashp;
1562d05f4435SSagi Grimberg 	struct scatterlist sg;
1563d05f4435SSagi Grimberg 	size_t copied;
1564d05f4435SSagi Grimberg 
1565d05f4435SSagi Grimberg 	copied = copy_to_iter(addr, bytes, i);
1566d05f4435SSagi Grimberg 	sg_init_one(&sg, addr, copied);
1567d05f4435SSagi Grimberg 	ahash_request_set_crypt(hash, &sg, NULL, copied);
1568d05f4435SSagi Grimberg 	crypto_ahash_update(hash);
1569d05f4435SSagi Grimberg 	return copied;
157027fad74aSYueHaibing #else
157127fad74aSYueHaibing 	return 0;
157227fad74aSYueHaibing #endif
1573d05f4435SSagi Grimberg }
1574d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter);
1575d05f4435SSagi Grimberg 
157666531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages)
1577d879cb83SAl Viro {
157866531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
157966531c65SAl Viro 	const struct iovec *p;
1580d879cb83SAl Viro 	int npages = 0;
1581d879cb83SAl Viro 
158266531c65SAl Viro 	for (p = i->iov; size; skip = 0, p++) {
158366531c65SAl Viro 		unsigned offs = offset_in_page(p->iov_base + skip);
158466531c65SAl Viro 		size_t len = min(p->iov_len - skip, size);
1585d879cb83SAl Viro 
158666531c65SAl Viro 		if (len) {
158766531c65SAl Viro 			size -= len;
158866531c65SAl Viro 			npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
158966531c65SAl Viro 			if (unlikely(npages > maxpages))
159066531c65SAl Viro 				return maxpages;
159166531c65SAl Viro 		}
159266531c65SAl Viro 	}
159366531c65SAl Viro 	return npages;
159466531c65SAl Viro }
159566531c65SAl Viro 
159666531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages)
159766531c65SAl Viro {
159866531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
159966531c65SAl Viro 	const struct bio_vec *p;
160066531c65SAl Viro 	int npages = 0;
160166531c65SAl Viro 
160266531c65SAl Viro 	for (p = i->bvec; size; skip = 0, p++) {
160366531c65SAl Viro 		unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
160466531c65SAl Viro 		size_t len = min(p->bv_len - skip, size);
160566531c65SAl Viro 
160666531c65SAl Viro 		size -= len;
160766531c65SAl Viro 		npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
160866531c65SAl Viro 		if (unlikely(npages > maxpages))
160966531c65SAl Viro 			return maxpages;
161066531c65SAl Viro 	}
161166531c65SAl Viro 	return npages;
161266531c65SAl Viro }
161366531c65SAl Viro 
161466531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages)
161566531c65SAl Viro {
161666531c65SAl Viro 	if (unlikely(!i->count))
161766531c65SAl Viro 		return 0;
1618fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1619fcb14cb1SAl Viro 		unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1620fcb14cb1SAl Viro 		int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1621fcb14cb1SAl Viro 		return min(npages, maxpages);
1622fcb14cb1SAl Viro 	}
162366531c65SAl Viro 	/* iovec and kvec have identical layouts */
162466531c65SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
162566531c65SAl Viro 		return iov_npages(i, maxpages);
162666531c65SAl Viro 	if (iov_iter_is_bvec(i))
162766531c65SAl Viro 		return bvec_npages(i, maxpages);
162866531c65SAl Viro 	if (iov_iter_is_pipe(i)) {
162966531c65SAl Viro 		int npages;
1630241699cdSAl Viro 
1631241699cdSAl Viro 		if (!sanity(i))
1632241699cdSAl Viro 			return 0;
1633241699cdSAl Viro 
163412d426abSAl Viro 		pipe_npages(i, &npages);
163566531c65SAl Viro 		return min(npages, maxpages);
163666531c65SAl Viro 	}
163766531c65SAl Viro 	if (iov_iter_is_xarray(i)) {
1638e4f8df86SAl Viro 		unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1639e4f8df86SAl Viro 		int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
164066531c65SAl Viro 		return min(npages, maxpages);
164166531c65SAl Viro 	}
164266531c65SAl Viro 	return 0;
1643d879cb83SAl Viro }
1644d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages);
1645d879cb83SAl Viro 
1646d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1647d879cb83SAl Viro {
1648d879cb83SAl Viro 	*new = *old;
164900e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(new))) {
1650241699cdSAl Viro 		WARN_ON(1);
1651241699cdSAl Viro 		return NULL;
1652241699cdSAl Viro 	}
165300e23707SDavid Howells 	if (iov_iter_is_bvec(new))
1654d879cb83SAl Viro 		return new->bvec = kmemdup(new->bvec,
1655d879cb83SAl Viro 				    new->nr_segs * sizeof(struct bio_vec),
1656d879cb83SAl Viro 				    flags);
1657fcb14cb1SAl Viro 	else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1658d879cb83SAl Viro 		/* iovec and kvec have identical layout */
1659d879cb83SAl Viro 		return new->iov = kmemdup(new->iov,
1660d879cb83SAl Viro 				   new->nr_segs * sizeof(struct iovec),
1661d879cb83SAl Viro 				   flags);
1662fcb14cb1SAl Viro 	return NULL;
1663d879cb83SAl Viro }
1664d879cb83SAl Viro EXPORT_SYMBOL(dup_iter);
1665bc917be8SAl Viro 
1666bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov,
1667bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1668bfdc5970SChristoph Hellwig {
1669bfdc5970SChristoph Hellwig 	const struct compat_iovec __user *uiov =
1670bfdc5970SChristoph Hellwig 		(const struct compat_iovec __user *)uvec;
1671bfdc5970SChristoph Hellwig 	int ret = -EFAULT, i;
1672bfdc5970SChristoph Hellwig 
1673a959a978SChristoph Hellwig 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1674bfdc5970SChristoph Hellwig 		return -EFAULT;
1675bfdc5970SChristoph Hellwig 
1676bfdc5970SChristoph Hellwig 	for (i = 0; i < nr_segs; i++) {
1677bfdc5970SChristoph Hellwig 		compat_uptr_t buf;
1678bfdc5970SChristoph Hellwig 		compat_ssize_t len;
1679bfdc5970SChristoph Hellwig 
1680bfdc5970SChristoph Hellwig 		unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1681bfdc5970SChristoph Hellwig 		unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1682bfdc5970SChristoph Hellwig 
1683bfdc5970SChristoph Hellwig 		/* check for compat_size_t not fitting in compat_ssize_t .. */
1684bfdc5970SChristoph Hellwig 		if (len < 0) {
1685bfdc5970SChristoph Hellwig 			ret = -EINVAL;
1686bfdc5970SChristoph Hellwig 			goto uaccess_end;
1687bfdc5970SChristoph Hellwig 		}
1688bfdc5970SChristoph Hellwig 		iov[i].iov_base = compat_ptr(buf);
1689bfdc5970SChristoph Hellwig 		iov[i].iov_len = len;
1690bfdc5970SChristoph Hellwig 	}
1691bfdc5970SChristoph Hellwig 
1692bfdc5970SChristoph Hellwig 	ret = 0;
1693bfdc5970SChristoph Hellwig uaccess_end:
1694bfdc5970SChristoph Hellwig 	user_access_end();
1695bfdc5970SChristoph Hellwig 	return ret;
1696bfdc5970SChristoph Hellwig }
1697bfdc5970SChristoph Hellwig 
1698bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov,
1699bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1700fb041b59SDavid Laight {
1701fb041b59SDavid Laight 	unsigned long seg;
1702bfdc5970SChristoph Hellwig 
1703bfdc5970SChristoph Hellwig 	if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1704bfdc5970SChristoph Hellwig 		return -EFAULT;
1705bfdc5970SChristoph Hellwig 	for (seg = 0; seg < nr_segs; seg++) {
1706bfdc5970SChristoph Hellwig 		if ((ssize_t)iov[seg].iov_len < 0)
1707bfdc5970SChristoph Hellwig 			return -EINVAL;
1708bfdc5970SChristoph Hellwig 	}
1709bfdc5970SChristoph Hellwig 
1710bfdc5970SChristoph Hellwig 	return 0;
1711bfdc5970SChristoph Hellwig }
1712bfdc5970SChristoph Hellwig 
1713bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec,
1714bfdc5970SChristoph Hellwig 		unsigned long nr_segs, unsigned long fast_segs,
1715bfdc5970SChristoph Hellwig 		struct iovec *fast_iov, bool compat)
1716bfdc5970SChristoph Hellwig {
1717bfdc5970SChristoph Hellwig 	struct iovec *iov = fast_iov;
1718bfdc5970SChristoph Hellwig 	int ret;
1719fb041b59SDavid Laight 
1720fb041b59SDavid Laight 	/*
1721bfdc5970SChristoph Hellwig 	 * SuS says "The readv() function *may* fail if the iovcnt argument was
1722bfdc5970SChristoph Hellwig 	 * less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1723fb041b59SDavid Laight 	 * traditionally returned zero for zero segments, so...
1724fb041b59SDavid Laight 	 */
1725bfdc5970SChristoph Hellwig 	if (nr_segs == 0)
1726bfdc5970SChristoph Hellwig 		return iov;
1727bfdc5970SChristoph Hellwig 	if (nr_segs > UIO_MAXIOV)
1728bfdc5970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1729fb041b59SDavid Laight 	if (nr_segs > fast_segs) {
1730fb041b59SDavid Laight 		iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1731bfdc5970SChristoph Hellwig 		if (!iov)
1732bfdc5970SChristoph Hellwig 			return ERR_PTR(-ENOMEM);
1733fb041b59SDavid Laight 	}
1734bfdc5970SChristoph Hellwig 
1735bfdc5970SChristoph Hellwig 	if (compat)
1736bfdc5970SChristoph Hellwig 		ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1737bfdc5970SChristoph Hellwig 	else
1738bfdc5970SChristoph Hellwig 		ret = copy_iovec_from_user(iov, uvec, nr_segs);
1739bfdc5970SChristoph Hellwig 	if (ret) {
1740bfdc5970SChristoph Hellwig 		if (iov != fast_iov)
1741bfdc5970SChristoph Hellwig 			kfree(iov);
1742bfdc5970SChristoph Hellwig 		return ERR_PTR(ret);
1743fb041b59SDavid Laight 	}
1744bfdc5970SChristoph Hellwig 
1745bfdc5970SChristoph Hellwig 	return iov;
1746bfdc5970SChristoph Hellwig }
1747bfdc5970SChristoph Hellwig 
1748bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1749bfdc5970SChristoph Hellwig 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1750bfdc5970SChristoph Hellwig 		 struct iov_iter *i, bool compat)
1751bfdc5970SChristoph Hellwig {
1752bfdc5970SChristoph Hellwig 	ssize_t total_len = 0;
1753bfdc5970SChristoph Hellwig 	unsigned long seg;
1754bfdc5970SChristoph Hellwig 	struct iovec *iov;
1755bfdc5970SChristoph Hellwig 
1756bfdc5970SChristoph Hellwig 	iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1757bfdc5970SChristoph Hellwig 	if (IS_ERR(iov)) {
1758bfdc5970SChristoph Hellwig 		*iovp = NULL;
1759bfdc5970SChristoph Hellwig 		return PTR_ERR(iov);
1760fb041b59SDavid Laight 	}
1761fb041b59SDavid Laight 
1762fb041b59SDavid Laight 	/*
1763bfdc5970SChristoph Hellwig 	 * According to the Single Unix Specification we should return EINVAL if
1764bfdc5970SChristoph Hellwig 	 * an element length is < 0 when cast to ssize_t or if the total length
1765bfdc5970SChristoph Hellwig 	 * would overflow the ssize_t return value of the system call.
1766fb041b59SDavid Laight 	 *
1767fb041b59SDavid Laight 	 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1768fb041b59SDavid Laight 	 * overflow case.
1769fb041b59SDavid Laight 	 */
1770fb041b59SDavid Laight 	for (seg = 0; seg < nr_segs; seg++) {
1771fb041b59SDavid Laight 		ssize_t len = (ssize_t)iov[seg].iov_len;
1772fb041b59SDavid Laight 
1773bfdc5970SChristoph Hellwig 		if (!access_ok(iov[seg].iov_base, len)) {
1774bfdc5970SChristoph Hellwig 			if (iov != *iovp)
1775bfdc5970SChristoph Hellwig 				kfree(iov);
1776bfdc5970SChristoph Hellwig 			*iovp = NULL;
1777bfdc5970SChristoph Hellwig 			return -EFAULT;
1778fb041b59SDavid Laight 		}
1779bfdc5970SChristoph Hellwig 
1780bfdc5970SChristoph Hellwig 		if (len > MAX_RW_COUNT - total_len) {
1781bfdc5970SChristoph Hellwig 			len = MAX_RW_COUNT - total_len;
1782fb041b59SDavid Laight 			iov[seg].iov_len = len;
1783fb041b59SDavid Laight 		}
1784bfdc5970SChristoph Hellwig 		total_len += len;
1785fb041b59SDavid Laight 	}
1786bfdc5970SChristoph Hellwig 
1787bfdc5970SChristoph Hellwig 	iov_iter_init(i, type, iov, nr_segs, total_len);
1788bfdc5970SChristoph Hellwig 	if (iov == *iovp)
1789bfdc5970SChristoph Hellwig 		*iovp = NULL;
1790bfdc5970SChristoph Hellwig 	else
1791bfdc5970SChristoph Hellwig 		*iovp = iov;
1792bfdc5970SChristoph Hellwig 	return total_len;
1793fb041b59SDavid Laight }
1794fb041b59SDavid Laight 
1795ffecee4fSVegard Nossum /**
1796ffecee4fSVegard Nossum  * import_iovec() - Copy an array of &struct iovec from userspace
1797ffecee4fSVegard Nossum  *     into the kernel, check that it is valid, and initialize a new
1798ffecee4fSVegard Nossum  *     &struct iov_iter iterator to access it.
1799ffecee4fSVegard Nossum  *
1800ffecee4fSVegard Nossum  * @type: One of %READ or %WRITE.
1801bfdc5970SChristoph Hellwig  * @uvec: Pointer to the userspace array.
1802ffecee4fSVegard Nossum  * @nr_segs: Number of elements in userspace array.
1803ffecee4fSVegard Nossum  * @fast_segs: Number of elements in @iov.
1804bfdc5970SChristoph Hellwig  * @iovp: (input and output parameter) Pointer to pointer to (usually small
1805ffecee4fSVegard Nossum  *     on-stack) kernel array.
1806ffecee4fSVegard Nossum  * @i: Pointer to iterator that will be initialized on success.
1807ffecee4fSVegard Nossum  *
1808ffecee4fSVegard Nossum  * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1809ffecee4fSVegard Nossum  * then this function places %NULL in *@iov on return. Otherwise, a new
1810ffecee4fSVegard Nossum  * array will be allocated and the result placed in *@iov. This means that
1811ffecee4fSVegard Nossum  * the caller may call kfree() on *@iov regardless of whether the small
1812ffecee4fSVegard Nossum  * on-stack array was used or not (and regardless of whether this function
1813ffecee4fSVegard Nossum  * returns an error or not).
1814ffecee4fSVegard Nossum  *
181587e5e6daSJens Axboe  * Return: Negative error code on error, bytes imported on success
1816ffecee4fSVegard Nossum  */
1817bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec,
1818bc917be8SAl Viro 		 unsigned nr_segs, unsigned fast_segs,
1819bfdc5970SChristoph Hellwig 		 struct iovec **iovp, struct iov_iter *i)
1820bc917be8SAl Viro {
182189cd35c5SChristoph Hellwig 	return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
182289cd35c5SChristoph Hellwig 			      in_compat_syscall());
1823bc917be8SAl Viro }
1824bc917be8SAl Viro EXPORT_SYMBOL(import_iovec);
1825bc917be8SAl Viro 
1826bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len,
1827bc917be8SAl Viro 		 struct iovec *iov, struct iov_iter *i)
1828bc917be8SAl Viro {
1829bc917be8SAl Viro 	if (len > MAX_RW_COUNT)
1830bc917be8SAl Viro 		len = MAX_RW_COUNT;
183196d4f267SLinus Torvalds 	if (unlikely(!access_ok(buf, len)))
1832bc917be8SAl Viro 		return -EFAULT;
1833bc917be8SAl Viro 
1834bc917be8SAl Viro 	iov->iov_base = buf;
1835bc917be8SAl Viro 	iov->iov_len = len;
1836bc917be8SAl Viro 	iov_iter_init(i, rw, iov, 1, len);
1837bc917be8SAl Viro 	return 0;
1838bc917be8SAl Viro }
1839e1267585SAl Viro EXPORT_SYMBOL(import_single_range);
18408fb0f47aSJens Axboe 
18418fb0f47aSJens Axboe /**
18428fb0f47aSJens Axboe  * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
18438fb0f47aSJens Axboe  *     iov_iter_save_state() was called.
18448fb0f47aSJens Axboe  *
18458fb0f47aSJens Axboe  * @i: &struct iov_iter to restore
18468fb0f47aSJens Axboe  * @state: state to restore from
18478fb0f47aSJens Axboe  *
18488fb0f47aSJens Axboe  * Used after iov_iter_save_state() to bring restore @i, if operations may
18498fb0f47aSJens Axboe  * have advanced it.
18508fb0f47aSJens Axboe  *
18518fb0f47aSJens Axboe  * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
18528fb0f47aSJens Axboe  */
18538fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
18548fb0f47aSJens Axboe {
18558fb0f47aSJens Axboe 	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
1856fcb14cb1SAl Viro 			 !iov_iter_is_kvec(i) && !iter_is_ubuf(i))
18578fb0f47aSJens Axboe 		return;
18588fb0f47aSJens Axboe 	i->iov_offset = state->iov_offset;
18598fb0f47aSJens Axboe 	i->count = state->count;
1860fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1861fcb14cb1SAl Viro 		return;
18628fb0f47aSJens Axboe 	/*
18638fb0f47aSJens Axboe 	 * For the *vec iters, nr_segs + iov is constant - if we increment
18648fb0f47aSJens Axboe 	 * the vec, then we also decrement the nr_segs count. Hence we don't
18658fb0f47aSJens Axboe 	 * need to track both of these, just one is enough and we can deduct
18668fb0f47aSJens Axboe 	 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
18678fb0f47aSJens Axboe 	 * size, so we can just increment the iov pointer as they are unionzed.
18688fb0f47aSJens Axboe 	 * ITER_BVEC _may_ be the same size on some archs, but on others it is
18698fb0f47aSJens Axboe 	 * not. Be safe and handle it separately.
18708fb0f47aSJens Axboe 	 */
18718fb0f47aSJens Axboe 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
18728fb0f47aSJens Axboe 	if (iov_iter_is_bvec(i))
18738fb0f47aSJens Axboe 		i->bvec -= state->nr_segs - i->nr_segs;
18748fb0f47aSJens Axboe 	else
18758fb0f47aSJens Axboe 		i->iov -= state->nr_segs - i->nr_segs;
18768fb0f47aSJens Axboe 	i->nr_segs = state->nr_segs;
18778fb0f47aSJens Axboe }
1878