xref: /openbmc/linux/lib/iov_iter.c (revision cfa320f7)
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 
195c67aa90SAl Viro /* covers iovec and kvec alike */
20a6e4ec7bSAl Viro #define iterate_iovec(i, n, base, len, off, __p, STEP) {	\
217baa5099SAl Viro 	size_t off = 0;						\
22a6e4ec7bSAl Viro 	size_t skip = i->iov_offset;				\
237a1bcb5dSAl Viro 	do {							\
247baa5099SAl Viro 		len = min(n, __p->iov_len - skip);		\
257baa5099SAl Viro 		if (likely(len)) {				\
267baa5099SAl Viro 			base = __p->iov_base + skip;		\
277baa5099SAl Viro 			len -= (STEP);				\
287baa5099SAl Viro 			off += len;				\
297baa5099SAl Viro 			skip += len;				\
307baa5099SAl Viro 			n -= len;				\
317a1bcb5dSAl Viro 			if (skip < __p->iov_len)		\
327a1bcb5dSAl Viro 				break;				\
33d879cb83SAl Viro 		}						\
34d879cb83SAl Viro 		__p++;						\
357a1bcb5dSAl Viro 		skip = 0;					\
367a1bcb5dSAl Viro 	} while (n);						\
37a6e4ec7bSAl Viro 	i->iov_offset = skip;					\
387baa5099SAl Viro 	n = off;						\
39d879cb83SAl Viro }
40d879cb83SAl Viro 
41a6e4ec7bSAl Viro #define iterate_bvec(i, n, base, len, off, p, STEP) {		\
427baa5099SAl Viro 	size_t off = 0;						\
43a6e4ec7bSAl Viro 	unsigned skip = i->iov_offset;				\
447491a2bfSAl Viro 	while (n) {						\
457491a2bfSAl Viro 		unsigned offset = p->bv_offset + skip;		\
461b4fb5ffSAl Viro 		unsigned left;					\
4721b56c84SAl Viro 		void *kaddr = kmap_local_page(p->bv_page +	\
4821b56c84SAl Viro 					offset / PAGE_SIZE);	\
497baa5099SAl Viro 		base = kaddr + offset % PAGE_SIZE;		\
50a6e4ec7bSAl Viro 		len = min(min(n, (size_t)(p->bv_len - skip)),	\
517491a2bfSAl Viro 		     (size_t)(PAGE_SIZE - offset % PAGE_SIZE));	\
521b4fb5ffSAl Viro 		left = (STEP);					\
5321b56c84SAl Viro 		kunmap_local(kaddr);				\
547baa5099SAl Viro 		len -= left;					\
557baa5099SAl Viro 		off += len;					\
567baa5099SAl Viro 		skip += len;					\
577491a2bfSAl Viro 		if (skip == p->bv_len) {			\
587491a2bfSAl Viro 			skip = 0;				\
597491a2bfSAl Viro 			p++;					\
60d879cb83SAl Viro 		}						\
617baa5099SAl Viro 		n -= len;					\
621b4fb5ffSAl Viro 		if (left)					\
631b4fb5ffSAl Viro 			break;					\
647491a2bfSAl Viro 	}							\
65a6e4ec7bSAl Viro 	i->iov_offset = skip;					\
667baa5099SAl Viro 	n = off;						\
67d879cb83SAl Viro }
68d879cb83SAl Viro 
69a6e4ec7bSAl Viro #define iterate_xarray(i, n, base, len, __off, STEP) {		\
701b4fb5ffSAl Viro 	__label__ __out;					\
71622838f3SAl Viro 	size_t __off = 0;					\
72821979f5SMatthew Wilcox (Oracle) 	struct folio *folio;					\
73a6e4ec7bSAl Viro 	loff_t start = i->xarray_start + i->iov_offset;		\
744b179e9aSAl Viro 	pgoff_t index = start / PAGE_SIZE;			\
757ff50620SDavid Howells 	XA_STATE(xas, i->xarray, index);			\
767ff50620SDavid Howells 								\
77821979f5SMatthew Wilcox (Oracle) 	len = PAGE_SIZE - offset_in_page(start);		\
787ff50620SDavid Howells 	rcu_read_lock();					\
79821979f5SMatthew Wilcox (Oracle) 	xas_for_each(&xas, folio, ULONG_MAX) {			\
801b4fb5ffSAl Viro 		unsigned left;					\
81821979f5SMatthew Wilcox (Oracle) 		size_t offset;					\
82821979f5SMatthew Wilcox (Oracle) 		if (xas_retry(&xas, folio))			\
837ff50620SDavid Howells 			continue;				\
84821979f5SMatthew Wilcox (Oracle) 		if (WARN_ON(xa_is_value(folio)))		\
857ff50620SDavid Howells 			break;					\
86821979f5SMatthew Wilcox (Oracle) 		if (WARN_ON(folio_test_hugetlb(folio)))		\
877ff50620SDavid Howells 			break;					\
88821979f5SMatthew Wilcox (Oracle) 		offset = offset_in_folio(folio, start + __off);	\
89821979f5SMatthew Wilcox (Oracle) 		while (offset < folio_size(folio)) {		\
90821979f5SMatthew Wilcox (Oracle) 			base = kmap_local_folio(folio, offset);	\
917baa5099SAl Viro 			len = min(n, len);			\
921b4fb5ffSAl Viro 			left = (STEP);				\
93821979f5SMatthew Wilcox (Oracle) 			kunmap_local(base);			\
947baa5099SAl Viro 			len -= left;				\
957baa5099SAl Viro 			__off += len;				\
967baa5099SAl Viro 			n -= len;				\
971b4fb5ffSAl Viro 			if (left || n == 0)			\
981b4fb5ffSAl Viro 				goto __out;			\
99821979f5SMatthew Wilcox (Oracle) 			offset += len;				\
100821979f5SMatthew Wilcox (Oracle) 			len = PAGE_SIZE;			\
1017ff50620SDavid Howells 		}						\
1027ff50620SDavid Howells 	}							\
1031b4fb5ffSAl Viro __out:								\
1047ff50620SDavid Howells 	rcu_read_unlock();					\
105a6e4ec7bSAl Viro 	i->iov_offset += __off;					\
106622838f3SAl Viro 	n = __off;						\
1077ff50620SDavid Howells }
1087ff50620SDavid Howells 
1097baa5099SAl Viro #define __iterate_and_advance(i, n, base, len, off, I, K) {	\
110dd254f5aSAl Viro 	if (unlikely(i->count < n))				\
111dd254f5aSAl Viro 		n = i->count;					\
112f5da8354SAl Viro 	if (likely(n)) {					\
11328f38db7SAl Viro 		if (likely(iter_is_iovec(i))) {			\
1145c67aa90SAl Viro 			const struct iovec *iov = i->iov;	\
1157baa5099SAl Viro 			void __user *base;			\
1167baa5099SAl Viro 			size_t len;				\
1177baa5099SAl Viro 			iterate_iovec(i, n, base, len, off,	\
118a6e4ec7bSAl Viro 						iov, (I))	\
119d879cb83SAl Viro 			i->nr_segs -= iov - i->iov;		\
120d879cb83SAl Viro 			i->iov = iov;				\
12128f38db7SAl Viro 		} else if (iov_iter_is_bvec(i)) {		\
12228f38db7SAl Viro 			const struct bio_vec *bvec = i->bvec;	\
1237baa5099SAl Viro 			void *base;				\
1247baa5099SAl Viro 			size_t len;				\
1257baa5099SAl Viro 			iterate_bvec(i, n, base, len, off,	\
126a6e4ec7bSAl Viro 						bvec, (K))	\
1277491a2bfSAl Viro 			i->nr_segs -= bvec - i->bvec;		\
1287491a2bfSAl Viro 			i->bvec = bvec;				\
12928f38db7SAl Viro 		} else if (iov_iter_is_kvec(i)) {		\
1305c67aa90SAl Viro 			const struct kvec *kvec = i->kvec;	\
1317baa5099SAl Viro 			void *base;				\
1327baa5099SAl Viro 			size_t len;				\
1337baa5099SAl Viro 			iterate_iovec(i, n, base, len, off,	\
134a6e4ec7bSAl Viro 						kvec, (K))	\
13528f38db7SAl Viro 			i->nr_segs -= kvec - i->kvec;		\
13628f38db7SAl Viro 			i->kvec = kvec;				\
13728f38db7SAl Viro 		} else if (iov_iter_is_xarray(i)) {		\
1387baa5099SAl Viro 			void *base;				\
1397baa5099SAl Viro 			size_t len;				\
1407baa5099SAl Viro 			iterate_xarray(i, n, base, len, off,	\
141a6e4ec7bSAl Viro 							(K))	\
142d879cb83SAl Viro 		}						\
143d879cb83SAl Viro 		i->count -= n;					\
144dd254f5aSAl Viro 	}							\
145d879cb83SAl Viro }
1467baa5099SAl Viro #define iterate_and_advance(i, n, base, len, off, I, K) \
1477baa5099SAl Viro 	__iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
148d879cb83SAl Viro 
14909fc68dcSAl Viro static int copyout(void __user *to, const void *from, size_t n)
15009fc68dcSAl Viro {
1514d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1524d0e9df5SAlbert van der Linde 		return n;
15396d4f267SLinus Torvalds 	if (access_ok(to, n)) {
154d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
15509fc68dcSAl Viro 		n = raw_copy_to_user(to, from, n);
15609fc68dcSAl Viro 	}
15709fc68dcSAl Viro 	return n;
15809fc68dcSAl Viro }
15909fc68dcSAl Viro 
16009fc68dcSAl Viro static int copyin(void *to, const void __user *from, size_t n)
16109fc68dcSAl Viro {
1624d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1634d0e9df5SAlbert van der Linde 		return n;
16496d4f267SLinus Torvalds 	if (access_ok(from, n)) {
165d0ef4c36SMarco Elver 		instrument_copy_from_user(to, from, n);
16609fc68dcSAl Viro 		n = raw_copy_from_user(to, from, n);
16709fc68dcSAl Viro 	}
16809fc68dcSAl Viro 	return n;
16909fc68dcSAl Viro }
17009fc68dcSAl Viro 
171d879cb83SAl Viro static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
172d879cb83SAl Viro 			 struct iov_iter *i)
173d879cb83SAl Viro {
174d879cb83SAl Viro 	size_t skip, copy, left, wanted;
175d879cb83SAl Viro 	const struct iovec *iov;
176d879cb83SAl Viro 	char __user *buf;
177d879cb83SAl Viro 	void *kaddr, *from;
178d879cb83SAl Viro 
179d879cb83SAl Viro 	if (unlikely(bytes > i->count))
180d879cb83SAl Viro 		bytes = i->count;
181d879cb83SAl Viro 
182d879cb83SAl Viro 	if (unlikely(!bytes))
183d879cb83SAl Viro 		return 0;
184d879cb83SAl Viro 
18509fc68dcSAl Viro 	might_fault();
186d879cb83SAl Viro 	wanted = bytes;
187d879cb83SAl Viro 	iov = i->iov;
188d879cb83SAl Viro 	skip = i->iov_offset;
189d879cb83SAl Viro 	buf = iov->iov_base + skip;
190d879cb83SAl Viro 	copy = min(bytes, iov->iov_len - skip);
191d879cb83SAl Viro 
192bb523b40SAndreas Gruenbacher 	if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_writeable(buf, copy)) {
193d879cb83SAl Viro 		kaddr = kmap_atomic(page);
194d879cb83SAl Viro 		from = kaddr + offset;
195d879cb83SAl Viro 
196d879cb83SAl Viro 		/* first chunk, usually the only one */
19709fc68dcSAl Viro 		left = copyout(buf, from, copy);
198d879cb83SAl Viro 		copy -= left;
199d879cb83SAl Viro 		skip += copy;
200d879cb83SAl Viro 		from += copy;
201d879cb83SAl Viro 		bytes -= copy;
202d879cb83SAl Viro 
203d879cb83SAl Viro 		while (unlikely(!left && bytes)) {
204d879cb83SAl Viro 			iov++;
205d879cb83SAl Viro 			buf = iov->iov_base;
206d879cb83SAl Viro 			copy = min(bytes, iov->iov_len);
20709fc68dcSAl Viro 			left = copyout(buf, from, copy);
208d879cb83SAl Viro 			copy -= left;
209d879cb83SAl Viro 			skip = copy;
210d879cb83SAl Viro 			from += copy;
211d879cb83SAl Viro 			bytes -= copy;
212d879cb83SAl Viro 		}
213d879cb83SAl Viro 		if (likely(!bytes)) {
214d879cb83SAl Viro 			kunmap_atomic(kaddr);
215d879cb83SAl Viro 			goto done;
216d879cb83SAl Viro 		}
217d879cb83SAl Viro 		offset = from - kaddr;
218d879cb83SAl Viro 		buf += copy;
219d879cb83SAl Viro 		kunmap_atomic(kaddr);
220d879cb83SAl Viro 		copy = min(bytes, iov->iov_len - skip);
221d879cb83SAl Viro 	}
222d879cb83SAl Viro 	/* Too bad - revert to non-atomic kmap */
2233fa6c507SMikulas Patocka 
224d879cb83SAl Viro 	kaddr = kmap(page);
225d879cb83SAl Viro 	from = kaddr + offset;
22609fc68dcSAl Viro 	left = copyout(buf, from, copy);
227d879cb83SAl Viro 	copy -= left;
228d879cb83SAl Viro 	skip += copy;
229d879cb83SAl Viro 	from += copy;
230d879cb83SAl Viro 	bytes -= copy;
231d879cb83SAl Viro 	while (unlikely(!left && bytes)) {
232d879cb83SAl Viro 		iov++;
233d879cb83SAl Viro 		buf = iov->iov_base;
234d879cb83SAl Viro 		copy = min(bytes, iov->iov_len);
23509fc68dcSAl Viro 		left = copyout(buf, from, copy);
236d879cb83SAl Viro 		copy -= left;
237d879cb83SAl Viro 		skip = copy;
238d879cb83SAl Viro 		from += copy;
239d879cb83SAl Viro 		bytes -= copy;
240d879cb83SAl Viro 	}
241d879cb83SAl Viro 	kunmap(page);
2423fa6c507SMikulas Patocka 
243d879cb83SAl Viro done:
244d879cb83SAl Viro 	if (skip == iov->iov_len) {
245d879cb83SAl Viro 		iov++;
246d879cb83SAl Viro 		skip = 0;
247d879cb83SAl Viro 	}
248d879cb83SAl Viro 	i->count -= wanted - bytes;
249d879cb83SAl Viro 	i->nr_segs -= iov - i->iov;
250d879cb83SAl Viro 	i->iov = iov;
251d879cb83SAl Viro 	i->iov_offset = skip;
252d879cb83SAl Viro 	return wanted - bytes;
253d879cb83SAl Viro }
254d879cb83SAl Viro 
255d879cb83SAl Viro static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
256d879cb83SAl Viro 			 struct iov_iter *i)
257d879cb83SAl Viro {
258d879cb83SAl Viro 	size_t skip, copy, left, wanted;
259d879cb83SAl Viro 	const struct iovec *iov;
260d879cb83SAl Viro 	char __user *buf;
261d879cb83SAl Viro 	void *kaddr, *to;
262d879cb83SAl Viro 
263d879cb83SAl Viro 	if (unlikely(bytes > i->count))
264d879cb83SAl Viro 		bytes = i->count;
265d879cb83SAl Viro 
266d879cb83SAl Viro 	if (unlikely(!bytes))
267d879cb83SAl Viro 		return 0;
268d879cb83SAl Viro 
26909fc68dcSAl Viro 	might_fault();
270d879cb83SAl Viro 	wanted = bytes;
271d879cb83SAl Viro 	iov = i->iov;
272d879cb83SAl Viro 	skip = i->iov_offset;
273d879cb83SAl Viro 	buf = iov->iov_base + skip;
274d879cb83SAl Viro 	copy = min(bytes, iov->iov_len - skip);
275d879cb83SAl Viro 
276bb523b40SAndreas Gruenbacher 	if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_readable(buf, copy)) {
277d879cb83SAl Viro 		kaddr = kmap_atomic(page);
278d879cb83SAl Viro 		to = kaddr + offset;
279d879cb83SAl Viro 
280d879cb83SAl Viro 		/* first chunk, usually the only one */
28109fc68dcSAl Viro 		left = copyin(to, buf, copy);
282d879cb83SAl Viro 		copy -= left;
283d879cb83SAl Viro 		skip += copy;
284d879cb83SAl Viro 		to += copy;
285d879cb83SAl Viro 		bytes -= copy;
286d879cb83SAl Viro 
287d879cb83SAl Viro 		while (unlikely(!left && bytes)) {
288d879cb83SAl Viro 			iov++;
289d879cb83SAl Viro 			buf = iov->iov_base;
290d879cb83SAl Viro 			copy = min(bytes, iov->iov_len);
29109fc68dcSAl Viro 			left = copyin(to, buf, copy);
292d879cb83SAl Viro 			copy -= left;
293d879cb83SAl Viro 			skip = copy;
294d879cb83SAl Viro 			to += copy;
295d879cb83SAl Viro 			bytes -= copy;
296d879cb83SAl Viro 		}
297d879cb83SAl Viro 		if (likely(!bytes)) {
298d879cb83SAl Viro 			kunmap_atomic(kaddr);
299d879cb83SAl Viro 			goto done;
300d879cb83SAl Viro 		}
301d879cb83SAl Viro 		offset = to - kaddr;
302d879cb83SAl Viro 		buf += copy;
303d879cb83SAl Viro 		kunmap_atomic(kaddr);
304d879cb83SAl Viro 		copy = min(bytes, iov->iov_len - skip);
305d879cb83SAl Viro 	}
306d879cb83SAl Viro 	/* Too bad - revert to non-atomic kmap */
3073fa6c507SMikulas Patocka 
308d879cb83SAl Viro 	kaddr = kmap(page);
309d879cb83SAl Viro 	to = kaddr + offset;
31009fc68dcSAl Viro 	left = copyin(to, buf, copy);
311d879cb83SAl Viro 	copy -= left;
312d879cb83SAl Viro 	skip += copy;
313d879cb83SAl Viro 	to += copy;
314d879cb83SAl Viro 	bytes -= copy;
315d879cb83SAl Viro 	while (unlikely(!left && bytes)) {
316d879cb83SAl Viro 		iov++;
317d879cb83SAl Viro 		buf = iov->iov_base;
318d879cb83SAl Viro 		copy = min(bytes, iov->iov_len);
31909fc68dcSAl Viro 		left = copyin(to, buf, copy);
320d879cb83SAl Viro 		copy -= left;
321d879cb83SAl Viro 		skip = copy;
322d879cb83SAl Viro 		to += copy;
323d879cb83SAl Viro 		bytes -= copy;
324d879cb83SAl Viro 	}
325d879cb83SAl Viro 	kunmap(page);
3263fa6c507SMikulas Patocka 
327d879cb83SAl Viro done:
328d879cb83SAl Viro 	if (skip == iov->iov_len) {
329d879cb83SAl Viro 		iov++;
330d879cb83SAl Viro 		skip = 0;
331d879cb83SAl Viro 	}
332d879cb83SAl Viro 	i->count -= wanted - bytes;
333d879cb83SAl Viro 	i->nr_segs -= iov - i->iov;
334d879cb83SAl Viro 	i->iov = iov;
335d879cb83SAl Viro 	i->iov_offset = skip;
336d879cb83SAl Viro 	return wanted - bytes;
337d879cb83SAl Viro }
338d879cb83SAl Viro 
339241699cdSAl Viro #ifdef PIPE_PARANOIA
340241699cdSAl Viro static bool sanity(const struct iov_iter *i)
341241699cdSAl Viro {
342241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
3438cefc107SDavid Howells 	unsigned int p_head = pipe->head;
3448cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
3458cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
3468cefc107SDavid Howells 	unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
3478cefc107SDavid Howells 	unsigned int i_head = i->head;
3488cefc107SDavid Howells 	unsigned int idx;
3498cefc107SDavid Howells 
350241699cdSAl Viro 	if (i->iov_offset) {
351241699cdSAl Viro 		struct pipe_buffer *p;
3528cefc107SDavid Howells 		if (unlikely(p_occupancy == 0))
353241699cdSAl Viro 			goto Bad;	// pipe must be non-empty
3548cefc107SDavid Howells 		if (unlikely(i_head != p_head - 1))
355241699cdSAl Viro 			goto Bad;	// must be at the last buffer...
356241699cdSAl Viro 
3578cefc107SDavid Howells 		p = &pipe->bufs[i_head & p_mask];
358241699cdSAl Viro 		if (unlikely(p->offset + p->len != i->iov_offset))
359241699cdSAl Viro 			goto Bad;	// ... at the end of segment
360241699cdSAl Viro 	} else {
3618cefc107SDavid Howells 		if (i_head != p_head)
362241699cdSAl Viro 			goto Bad;	// must be right after the last buffer
363241699cdSAl Viro 	}
364241699cdSAl Viro 	return true;
365241699cdSAl Viro Bad:
3668cefc107SDavid Howells 	printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
3678cefc107SDavid Howells 	printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
3688cefc107SDavid Howells 			p_head, p_tail, pipe->ring_size);
3698cefc107SDavid Howells 	for (idx = 0; idx < pipe->ring_size; idx++)
370241699cdSAl Viro 		printk(KERN_ERR "[%p %p %d %d]\n",
371241699cdSAl Viro 			pipe->bufs[idx].ops,
372241699cdSAl Viro 			pipe->bufs[idx].page,
373241699cdSAl Viro 			pipe->bufs[idx].offset,
374241699cdSAl Viro 			pipe->bufs[idx].len);
375241699cdSAl Viro 	WARN_ON(1);
376241699cdSAl Viro 	return false;
377241699cdSAl Viro }
378241699cdSAl Viro #else
379241699cdSAl Viro #define sanity(i) true
380241699cdSAl Viro #endif
381241699cdSAl Viro 
382241699cdSAl Viro static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
383241699cdSAl Viro 			 struct iov_iter *i)
384241699cdSAl Viro {
385241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
386241699cdSAl Viro 	struct pipe_buffer *buf;
3878cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
3888cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
3898cefc107SDavid Howells 	unsigned int i_head = i->head;
390241699cdSAl Viro 	size_t off;
391241699cdSAl Viro 
392241699cdSAl Viro 	if (unlikely(bytes > i->count))
393241699cdSAl Viro 		bytes = i->count;
394241699cdSAl Viro 
395241699cdSAl Viro 	if (unlikely(!bytes))
396241699cdSAl Viro 		return 0;
397241699cdSAl Viro 
398241699cdSAl Viro 	if (!sanity(i))
399241699cdSAl Viro 		return 0;
400241699cdSAl Viro 
401241699cdSAl Viro 	off = i->iov_offset;
4028cefc107SDavid Howells 	buf = &pipe->bufs[i_head & p_mask];
403241699cdSAl Viro 	if (off) {
404241699cdSAl Viro 		if (offset == off && buf->page == page) {
405241699cdSAl Viro 			/* merge with the last one */
406241699cdSAl Viro 			buf->len += bytes;
407241699cdSAl Viro 			i->iov_offset += bytes;
408241699cdSAl Viro 			goto out;
409241699cdSAl Viro 		}
4108cefc107SDavid Howells 		i_head++;
4118cefc107SDavid Howells 		buf = &pipe->bufs[i_head & p_mask];
412241699cdSAl Viro 	}
4136718b6f8SDavid Howells 	if (pipe_full(i_head, p_tail, pipe->max_usage))
414241699cdSAl Viro 		return 0;
4158cefc107SDavid Howells 
416241699cdSAl Viro 	buf->ops = &page_cache_pipe_buf_ops;
4179d2231c5SMax Kellermann 	buf->flags = 0;
4188cefc107SDavid Howells 	get_page(page);
4198cefc107SDavid Howells 	buf->page = page;
420241699cdSAl Viro 	buf->offset = offset;
421241699cdSAl Viro 	buf->len = bytes;
4228cefc107SDavid Howells 
4238cefc107SDavid Howells 	pipe->head = i_head + 1;
424241699cdSAl Viro 	i->iov_offset = offset + bytes;
4258cefc107SDavid Howells 	i->head = i_head;
426241699cdSAl Viro out:
427241699cdSAl Viro 	i->count -= bytes;
428241699cdSAl Viro 	return bytes;
429241699cdSAl Viro }
430241699cdSAl Viro 
431d879cb83SAl Viro /*
432a6294593SAndreas Gruenbacher  * fault_in_iov_iter_readable - fault in iov iterator for reading
433a6294593SAndreas Gruenbacher  * @i: iterator
434a6294593SAndreas Gruenbacher  * @size: maximum length
435171a0203SAnton Altaparmakov  *
436a6294593SAndreas Gruenbacher  * Fault in one or more iovecs of the given iov_iter, to a maximum length of
437a6294593SAndreas Gruenbacher  * @size.  For each iovec, fault in each page that constitutes the iovec.
438a6294593SAndreas Gruenbacher  *
439a6294593SAndreas Gruenbacher  * Returns the number of bytes not faulted in (like copy_to_user() and
440a6294593SAndreas Gruenbacher  * copy_from_user()).
441a6294593SAndreas Gruenbacher  *
442a6294593SAndreas Gruenbacher  * Always returns 0 for non-userspace iterators.
443171a0203SAnton Altaparmakov  */
444a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
445171a0203SAnton Altaparmakov {
4460e8f0d67SAl Viro 	if (iter_is_iovec(i)) {
447a6294593SAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
4488409a0d2SAl Viro 		const struct iovec *p;
4498409a0d2SAl Viro 		size_t skip;
4508409a0d2SAl Viro 
451a6294593SAndreas Gruenbacher 		size -= count;
452a6294593SAndreas Gruenbacher 		for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
453a6294593SAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
454a6294593SAndreas Gruenbacher 			size_t ret;
4558409a0d2SAl Viro 
4568409a0d2SAl Viro 			if (unlikely(!len))
4578409a0d2SAl Viro 				continue;
458a6294593SAndreas Gruenbacher 			ret = fault_in_readable(p->iov_base + skip, len);
459a6294593SAndreas Gruenbacher 			count -= len - ret;
460a6294593SAndreas Gruenbacher 			if (ret)
461a6294593SAndreas Gruenbacher 				break;
4628409a0d2SAl Viro 		}
463a6294593SAndreas Gruenbacher 		return count + size;
464171a0203SAnton Altaparmakov 	}
465171a0203SAnton Altaparmakov 	return 0;
466171a0203SAnton Altaparmakov }
467a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable);
468171a0203SAnton Altaparmakov 
469cdd591fcSAndreas Gruenbacher /*
470cdd591fcSAndreas Gruenbacher  * fault_in_iov_iter_writeable - fault in iov iterator for writing
471cdd591fcSAndreas Gruenbacher  * @i: iterator
472cdd591fcSAndreas Gruenbacher  * @size: maximum length
473cdd591fcSAndreas Gruenbacher  *
474cdd591fcSAndreas Gruenbacher  * Faults in the iterator using get_user_pages(), i.e., without triggering
475cdd591fcSAndreas Gruenbacher  * hardware page faults.  This is primarily useful when we already know that
476cdd591fcSAndreas Gruenbacher  * some or all of the pages in @i aren't in memory.
477cdd591fcSAndreas Gruenbacher  *
478cdd591fcSAndreas Gruenbacher  * Returns the number of bytes not faulted in, like copy_to_user() and
479cdd591fcSAndreas Gruenbacher  * copy_from_user().
480cdd591fcSAndreas Gruenbacher  *
481cdd591fcSAndreas Gruenbacher  * Always returns 0 for non-user-space iterators.
482cdd591fcSAndreas Gruenbacher  */
483cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
484cdd591fcSAndreas Gruenbacher {
485cdd591fcSAndreas Gruenbacher 	if (iter_is_iovec(i)) {
486cdd591fcSAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
487cdd591fcSAndreas Gruenbacher 		const struct iovec *p;
488cdd591fcSAndreas Gruenbacher 		size_t skip;
489cdd591fcSAndreas Gruenbacher 
490cdd591fcSAndreas Gruenbacher 		size -= count;
491cdd591fcSAndreas Gruenbacher 		for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
492cdd591fcSAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
493cdd591fcSAndreas Gruenbacher 			size_t ret;
494cdd591fcSAndreas Gruenbacher 
495cdd591fcSAndreas Gruenbacher 			if (unlikely(!len))
496cdd591fcSAndreas Gruenbacher 				continue;
497cdd591fcSAndreas Gruenbacher 			ret = fault_in_safe_writeable(p->iov_base + skip, len);
498cdd591fcSAndreas Gruenbacher 			count -= len - ret;
499cdd591fcSAndreas Gruenbacher 			if (ret)
500cdd591fcSAndreas Gruenbacher 				break;
501cdd591fcSAndreas Gruenbacher 		}
502cdd591fcSAndreas Gruenbacher 		return count + size;
503cdd591fcSAndreas Gruenbacher 	}
504cdd591fcSAndreas Gruenbacher 	return 0;
505cdd591fcSAndreas Gruenbacher }
506cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable);
507cdd591fcSAndreas Gruenbacher 
508aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction,
509d879cb83SAl Viro 			const struct iovec *iov, unsigned long nr_segs,
510d879cb83SAl Viro 			size_t count)
511d879cb83SAl Viro {
512aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
5138cd54c1cSAl Viro 	*i = (struct iov_iter) {
5148cd54c1cSAl Viro 		.iter_type = ITER_IOVEC,
5153337ab08SAndreas Gruenbacher 		.nofault = false,
5168cd54c1cSAl Viro 		.data_source = direction,
5178cd54c1cSAl Viro 		.iov = iov,
5188cd54c1cSAl Viro 		.nr_segs = nr_segs,
5198cd54c1cSAl Viro 		.iov_offset = 0,
5208cd54c1cSAl Viro 		.count = count
5218cd54c1cSAl Viro 	};
522d879cb83SAl Viro }
523d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init);
524d879cb83SAl Viro 
525241699cdSAl Viro static inline bool allocated(struct pipe_buffer *buf)
526241699cdSAl Viro {
527241699cdSAl Viro 	return buf->ops == &default_pipe_buf_ops;
528241699cdSAl Viro }
529241699cdSAl Viro 
5308cefc107SDavid Howells static inline void data_start(const struct iov_iter *i,
5318cefc107SDavid Howells 			      unsigned int *iter_headp, size_t *offp)
532241699cdSAl Viro {
5338cefc107SDavid Howells 	unsigned int p_mask = i->pipe->ring_size - 1;
5348cefc107SDavid Howells 	unsigned int iter_head = i->head;
535241699cdSAl Viro 	size_t off = i->iov_offset;
5368cefc107SDavid Howells 
5378cefc107SDavid Howells 	if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
5388cefc107SDavid Howells 		    off == PAGE_SIZE)) {
5398cefc107SDavid Howells 		iter_head++;
540241699cdSAl Viro 		off = 0;
541241699cdSAl Viro 	}
5428cefc107SDavid Howells 	*iter_headp = iter_head;
543241699cdSAl Viro 	*offp = off;
544241699cdSAl Viro }
545241699cdSAl Viro 
546241699cdSAl Viro static size_t push_pipe(struct iov_iter *i, size_t size,
5478cefc107SDavid Howells 			int *iter_headp, size_t *offp)
548241699cdSAl Viro {
549241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
5508cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
5518cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
5528cefc107SDavid Howells 	unsigned int iter_head;
553241699cdSAl Viro 	size_t off;
554241699cdSAl Viro 	ssize_t left;
555241699cdSAl Viro 
556241699cdSAl Viro 	if (unlikely(size > i->count))
557241699cdSAl Viro 		size = i->count;
558241699cdSAl Viro 	if (unlikely(!size))
559241699cdSAl Viro 		return 0;
560241699cdSAl Viro 
561241699cdSAl Viro 	left = size;
5628cefc107SDavid Howells 	data_start(i, &iter_head, &off);
5638cefc107SDavid Howells 	*iter_headp = iter_head;
564241699cdSAl Viro 	*offp = off;
565241699cdSAl Viro 	if (off) {
566241699cdSAl Viro 		left -= PAGE_SIZE - off;
567241699cdSAl Viro 		if (left <= 0) {
5688cefc107SDavid Howells 			pipe->bufs[iter_head & p_mask].len += size;
569241699cdSAl Viro 			return size;
570241699cdSAl Viro 		}
5718cefc107SDavid Howells 		pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
5728cefc107SDavid Howells 		iter_head++;
573241699cdSAl Viro 	}
5746718b6f8SDavid Howells 	while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
5758cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
576241699cdSAl Viro 		struct page *page = alloc_page(GFP_USER);
577241699cdSAl Viro 		if (!page)
578241699cdSAl Viro 			break;
5798cefc107SDavid Howells 
5808cefc107SDavid Howells 		buf->ops = &default_pipe_buf_ops;
5819d2231c5SMax Kellermann 		buf->flags = 0;
5828cefc107SDavid Howells 		buf->page = page;
5838cefc107SDavid Howells 		buf->offset = 0;
5848cefc107SDavid Howells 		buf->len = min_t(ssize_t, left, PAGE_SIZE);
5858cefc107SDavid Howells 		left -= buf->len;
5868cefc107SDavid Howells 		iter_head++;
5878cefc107SDavid Howells 		pipe->head = iter_head;
5888cefc107SDavid Howells 
5898cefc107SDavid Howells 		if (left == 0)
590241699cdSAl Viro 			return size;
591241699cdSAl Viro 	}
592241699cdSAl Viro 	return size - left;
593241699cdSAl Viro }
594241699cdSAl Viro 
595241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
596241699cdSAl Viro 				struct iov_iter *i)
597241699cdSAl Viro {
598241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
5998cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
6008cefc107SDavid Howells 	unsigned int i_head;
601241699cdSAl Viro 	size_t n, off;
602241699cdSAl Viro 
603241699cdSAl Viro 	if (!sanity(i))
604241699cdSAl Viro 		return 0;
605241699cdSAl Viro 
6068cefc107SDavid Howells 	bytes = n = push_pipe(i, bytes, &i_head, &off);
607241699cdSAl Viro 	if (unlikely(!n))
608241699cdSAl Viro 		return 0;
6098cefc107SDavid Howells 	do {
610241699cdSAl Viro 		size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
6118cefc107SDavid Howells 		memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
6128cefc107SDavid Howells 		i->head = i_head;
613241699cdSAl Viro 		i->iov_offset = off + chunk;
614241699cdSAl Viro 		n -= chunk;
615241699cdSAl Viro 		addr += chunk;
6168cefc107SDavid Howells 		off = 0;
6178cefc107SDavid Howells 		i_head++;
6188cefc107SDavid Howells 	} while (n);
619241699cdSAl Viro 	i->count -= bytes;
620241699cdSAl Viro 	return bytes;
621241699cdSAl Viro }
622241699cdSAl Viro 
623f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
624f9152895SAl Viro 			      __wsum sum, size_t off)
625f9152895SAl Viro {
626cc44c17bSAl Viro 	__wsum next = csum_partial_copy_nocheck(from, to, len);
627f9152895SAl Viro 	return csum_block_add(sum, next, off);
628f9152895SAl Viro }
629f9152895SAl Viro 
63078e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
6316852df12SAl Viro 					 struct iov_iter *i, __wsum *sump)
63278e1f386SAl Viro {
63378e1f386SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
6348cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
6356852df12SAl Viro 	__wsum sum = *sump;
6366852df12SAl Viro 	size_t off = 0;
6378cefc107SDavid Howells 	unsigned int i_head;
6386852df12SAl Viro 	size_t r;
63978e1f386SAl Viro 
64078e1f386SAl Viro 	if (!sanity(i))
64178e1f386SAl Viro 		return 0;
64278e1f386SAl Viro 
6436852df12SAl Viro 	bytes = push_pipe(i, bytes, &i_head, &r);
6446852df12SAl Viro 	while (bytes) {
6456852df12SAl Viro 		size_t chunk = min_t(size_t, bytes, PAGE_SIZE - r);
6462495bdccSAl Viro 		char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
6476852df12SAl Viro 		sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
6482495bdccSAl Viro 		kunmap_local(p);
6498cefc107SDavid Howells 		i->head = i_head;
65078e1f386SAl Viro 		i->iov_offset = r + chunk;
6516852df12SAl Viro 		bytes -= chunk;
65278e1f386SAl Viro 		off += chunk;
6538cefc107SDavid Howells 		r = 0;
6548cefc107SDavid Howells 		i_head++;
6556852df12SAl Viro 	}
6566852df12SAl Viro 	*sump = sum;
6576852df12SAl Viro 	i->count -= off;
6586852df12SAl Viro 	return off;
65978e1f386SAl Viro }
66078e1f386SAl Viro 
661aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
662d879cb83SAl Viro {
66300e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
664241699cdSAl Viro 		return copy_pipe_to_iter(addr, bytes, i);
66509fc68dcSAl Viro 	if (iter_is_iovec(i))
66609fc68dcSAl Viro 		might_fault();
6677baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6687baa5099SAl Viro 		copyout(base, addr + off, len),
6697baa5099SAl Viro 		memcpy(base, addr + off, len)
670d879cb83SAl Viro 	)
671d879cb83SAl Viro 
672d879cb83SAl Viro 	return bytes;
673d879cb83SAl Viro }
674aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter);
675d879cb83SAl Viro 
676ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC
677ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n)
6788780356eSDan Williams {
67996d4f267SLinus Torvalds 	if (access_ok(to, n)) {
680d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
681ec6347bbSDan Williams 		n = copy_mc_to_user((__force void *) to, from, n);
6828780356eSDan Williams 	}
6838780356eSDan Williams 	return n;
6848780356eSDan Williams }
6858780356eSDan Williams 
686ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
687ca146f6fSDan Williams 				struct iov_iter *i)
688ca146f6fSDan Williams {
689ca146f6fSDan Williams 	struct pipe_inode_info *pipe = i->pipe;
6908cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
6918cefc107SDavid Howells 	unsigned int i_head;
692ca146f6fSDan Williams 	size_t n, off, xfer = 0;
693ca146f6fSDan Williams 
694ca146f6fSDan Williams 	if (!sanity(i))
695ca146f6fSDan Williams 		return 0;
696ca146f6fSDan Williams 
6972a510a74SAl Viro 	n = push_pipe(i, bytes, &i_head, &off);
6982a510a74SAl Viro 	while (n) {
699ca146f6fSDan Williams 		size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
7002a510a74SAl Viro 		char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
701ca146f6fSDan Williams 		unsigned long rem;
7022a510a74SAl Viro 		rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
7032a510a74SAl Viro 		chunk -= rem;
7042a510a74SAl Viro 		kunmap_local(p);
7058cefc107SDavid Howells 		i->head = i_head;
7062a510a74SAl Viro 		i->iov_offset = off + chunk;
7072a510a74SAl Viro 		xfer += chunk;
708ca146f6fSDan Williams 		if (rem)
709ca146f6fSDan Williams 			break;
710ca146f6fSDan Williams 		n -= chunk;
7118cefc107SDavid Howells 		off = 0;
7128cefc107SDavid Howells 		i_head++;
7132a510a74SAl Viro 	}
714ca146f6fSDan Williams 	i->count -= xfer;
715ca146f6fSDan Williams 	return xfer;
716ca146f6fSDan Williams }
717ca146f6fSDan Williams 
718bf3eeb9bSDan Williams /**
719ec6347bbSDan Williams  * _copy_mc_to_iter - copy to iter with source memory error exception handling
720bf3eeb9bSDan Williams  * @addr: source kernel address
721bf3eeb9bSDan Williams  * @bytes: total transfer length
72244e55997SRandy Dunlap  * @i: destination iterator
723bf3eeb9bSDan Williams  *
724ec6347bbSDan Williams  * The pmem driver deploys this for the dax operation
725ec6347bbSDan Williams  * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
726ec6347bbSDan Williams  * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
727ec6347bbSDan Williams  * successfully copied.
728bf3eeb9bSDan Williams  *
729ec6347bbSDan Williams  * The main differences between this and typical _copy_to_iter().
730bf3eeb9bSDan Williams  *
731bf3eeb9bSDan Williams  * * Typical tail/residue handling after a fault retries the copy
732bf3eeb9bSDan Williams  *   byte-by-byte until the fault happens again. Re-triggering machine
733bf3eeb9bSDan Williams  *   checks is potentially fatal so the implementation uses source
734bf3eeb9bSDan Williams  *   alignment and poison alignment assumptions to avoid re-triggering
735bf3eeb9bSDan Williams  *   hardware exceptions.
736bf3eeb9bSDan Williams  *
737bf3eeb9bSDan Williams  * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
738bf3eeb9bSDan Williams  *   Compare to copy_to_iter() where only ITER_IOVEC attempts might return
739bf3eeb9bSDan Williams  *   a short copy.
74044e55997SRandy Dunlap  *
74144e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
742bf3eeb9bSDan Williams  */
743ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
7448780356eSDan Williams {
74500e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
746ec6347bbSDan Williams 		return copy_mc_pipe_to_iter(addr, bytes, i);
7478780356eSDan Williams 	if (iter_is_iovec(i))
7488780356eSDan Williams 		might_fault();
7497baa5099SAl Viro 	__iterate_and_advance(i, bytes, base, len, off,
7507baa5099SAl Viro 		copyout_mc(base, addr + off, len),
7517baa5099SAl Viro 		copy_mc_to_kernel(base, addr + off, len)
7528780356eSDan Williams 	)
7538780356eSDan Williams 
7548780356eSDan Williams 	return bytes;
7558780356eSDan Williams }
756ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
757ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */
7588780356eSDan Williams 
759aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
760d879cb83SAl Viro {
76100e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
762241699cdSAl Viro 		WARN_ON(1);
763241699cdSAl Viro 		return 0;
764241699cdSAl Viro 	}
76509fc68dcSAl Viro 	if (iter_is_iovec(i))
76609fc68dcSAl Viro 		might_fault();
7677baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
7687baa5099SAl Viro 		copyin(addr + off, base, len),
7697baa5099SAl Viro 		memcpy(addr + off, base, len)
770d879cb83SAl Viro 	)
771d879cb83SAl Viro 
772d879cb83SAl Viro 	return bytes;
773d879cb83SAl Viro }
774aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter);
775d879cb83SAl Viro 
776aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
777d879cb83SAl Viro {
77800e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
779241699cdSAl Viro 		WARN_ON(1);
780241699cdSAl Viro 		return 0;
781241699cdSAl Viro 	}
7827baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
7837baa5099SAl Viro 		__copy_from_user_inatomic_nocache(addr + off, base, len),
7847baa5099SAl Viro 		memcpy(addr + off, base, len)
785d879cb83SAl Viro 	)
786d879cb83SAl Viro 
787d879cb83SAl Viro 	return bytes;
788d879cb83SAl Viro }
789aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache);
790d879cb83SAl Viro 
7910aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
792abd08d7dSDan Williams /**
793abd08d7dSDan Williams  * _copy_from_iter_flushcache - write destination through cpu cache
794abd08d7dSDan Williams  * @addr: destination kernel address
795abd08d7dSDan Williams  * @bytes: total transfer length
79644e55997SRandy Dunlap  * @i: source iterator
797abd08d7dSDan Williams  *
798abd08d7dSDan Williams  * The pmem driver arranges for filesystem-dax to use this facility via
799abd08d7dSDan Williams  * dax_copy_from_iter() for ensuring that writes to persistent memory
800abd08d7dSDan Williams  * are flushed through the CPU cache. It is differentiated from
801abd08d7dSDan Williams  * _copy_from_iter_nocache() in that guarantees all data is flushed for
802abd08d7dSDan Williams  * all iterator types. The _copy_from_iter_nocache() only attempts to
803abd08d7dSDan Williams  * bypass the cache for the ITER_IOVEC case, and on some archs may use
804abd08d7dSDan Williams  * instructions that strand dirty-data in the cache.
80544e55997SRandy Dunlap  *
80644e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
807abd08d7dSDan Williams  */
8086a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
8090aed55afSDan Williams {
81000e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
8110aed55afSDan Williams 		WARN_ON(1);
8120aed55afSDan Williams 		return 0;
8130aed55afSDan Williams 	}
8147baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
8157baa5099SAl Viro 		__copy_from_user_flushcache(addr + off, base, len),
8167baa5099SAl Viro 		memcpy_flushcache(addr + off, base, len)
8170aed55afSDan Williams 	)
8180aed55afSDan Williams 
8190aed55afSDan Williams 	return bytes;
8200aed55afSDan Williams }
8216a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
8220aed55afSDan Williams #endif
8230aed55afSDan Williams 
82472e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
82572e809edSAl Viro {
8266daef95bSEric Dumazet 	struct page *head;
8276daef95bSEric Dumazet 	size_t v = n + offset;
8286daef95bSEric Dumazet 
8296daef95bSEric Dumazet 	/*
8306daef95bSEric Dumazet 	 * The general case needs to access the page order in order
8316daef95bSEric Dumazet 	 * to compute the page size.
8326daef95bSEric Dumazet 	 * However, we mostly deal with order-0 pages and thus can
8336daef95bSEric Dumazet 	 * avoid a possible cache line miss for requests that fit all
8346daef95bSEric Dumazet 	 * page orders.
8356daef95bSEric Dumazet 	 */
8366daef95bSEric Dumazet 	if (n <= v && v <= PAGE_SIZE)
8376daef95bSEric Dumazet 		return true;
8386daef95bSEric Dumazet 
8396daef95bSEric Dumazet 	head = compound_head(page);
8406daef95bSEric Dumazet 	v += (page - head) << PAGE_SHIFT;
841a90bcb86SPetar Penkov 
842a50b854eSMatthew Wilcox (Oracle) 	if (likely(n <= v && v <= (page_size(head))))
84372e809edSAl Viro 		return true;
84472e809edSAl Viro 	WARN_ON(1);
84572e809edSAl Viro 	return false;
84672e809edSAl Viro }
847cbbd26b8SAl Viro 
84808aa6479SAl Viro static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
849d879cb83SAl Viro 			 struct iov_iter *i)
850d879cb83SAl Viro {
85128f38db7SAl Viro 	if (likely(iter_is_iovec(i)))
85228f38db7SAl Viro 		return copy_page_to_iter_iovec(page, offset, bytes, i);
85328f38db7SAl Viro 	if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
854c1d4d6a9SAl Viro 		void *kaddr = kmap_local_page(page);
855c1d4d6a9SAl Viro 		size_t wanted = _copy_to_iter(kaddr + offset, bytes, i);
856c1d4d6a9SAl Viro 		kunmap_local(kaddr);
857d879cb83SAl Viro 		return wanted;
85828f38db7SAl Viro 	}
85928f38db7SAl Viro 	if (iov_iter_is_pipe(i))
86028f38db7SAl Viro 		return copy_page_to_iter_pipe(page, offset, bytes, i);
86128f38db7SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
862a506abc7SAl Viro 		if (unlikely(i->count < bytes))
863a506abc7SAl Viro 			bytes = i->count;
864a506abc7SAl Viro 		i->count -= bytes;
8659ea9ce04SDavid Howells 		return bytes;
86628f38db7SAl Viro 	}
86728f38db7SAl Viro 	WARN_ON(1);
86828f38db7SAl Viro 	return 0;
869d879cb83SAl Viro }
87008aa6479SAl Viro 
87108aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
87208aa6479SAl Viro 			 struct iov_iter *i)
87308aa6479SAl Viro {
87408aa6479SAl Viro 	size_t res = 0;
87508aa6479SAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes)))
87608aa6479SAl Viro 		return 0;
87708aa6479SAl Viro 	page += offset / PAGE_SIZE; // first subpage
87808aa6479SAl Viro 	offset %= PAGE_SIZE;
87908aa6479SAl Viro 	while (1) {
88008aa6479SAl Viro 		size_t n = __copy_page_to_iter(page, offset,
88108aa6479SAl Viro 				min(bytes, (size_t)PAGE_SIZE - offset), i);
88208aa6479SAl Viro 		res += n;
88308aa6479SAl Viro 		bytes -= n;
88408aa6479SAl Viro 		if (!bytes || !n)
88508aa6479SAl Viro 			break;
88608aa6479SAl Viro 		offset += n;
88708aa6479SAl Viro 		if (offset == PAGE_SIZE) {
88808aa6479SAl Viro 			page++;
88908aa6479SAl Viro 			offset = 0;
89008aa6479SAl Viro 		}
89108aa6479SAl Viro 	}
89208aa6479SAl Viro 	return res;
89308aa6479SAl Viro }
894d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter);
895d879cb83SAl Viro 
896d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
897d879cb83SAl Viro 			 struct iov_iter *i)
898d879cb83SAl Viro {
89972e809edSAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes)))
90072e809edSAl Viro 		return 0;
90128f38db7SAl Viro 	if (likely(iter_is_iovec(i)))
90228f38db7SAl Viro 		return copy_page_from_iter_iovec(page, offset, bytes, i);
90328f38db7SAl Viro 	if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
90455ca375cSAl Viro 		void *kaddr = kmap_local_page(page);
905aa28de27SAl Viro 		size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
90655ca375cSAl Viro 		kunmap_local(kaddr);
907d879cb83SAl Viro 		return wanted;
90828f38db7SAl Viro 	}
90928f38db7SAl Viro 	WARN_ON(1);
91028f38db7SAl Viro 	return 0;
911d879cb83SAl Viro }
912d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter);
913d879cb83SAl Viro 
914241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i)
915241699cdSAl Viro {
916241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
9178cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
9188cefc107SDavid Howells 	unsigned int i_head;
919241699cdSAl Viro 	size_t n, off;
920241699cdSAl Viro 
921241699cdSAl Viro 	if (!sanity(i))
922241699cdSAl Viro 		return 0;
923241699cdSAl Viro 
9248cefc107SDavid Howells 	bytes = n = push_pipe(i, bytes, &i_head, &off);
925241699cdSAl Viro 	if (unlikely(!n))
926241699cdSAl Viro 		return 0;
927241699cdSAl Viro 
9288cefc107SDavid Howells 	do {
929241699cdSAl Viro 		size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
930893839fdSAl Viro 		char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
931893839fdSAl Viro 		memset(p + off, 0, chunk);
932893839fdSAl Viro 		kunmap_local(p);
9338cefc107SDavid Howells 		i->head = i_head;
934241699cdSAl Viro 		i->iov_offset = off + chunk;
935241699cdSAl Viro 		n -= chunk;
9368cefc107SDavid Howells 		off = 0;
9378cefc107SDavid Howells 		i_head++;
9388cefc107SDavid Howells 	} while (n);
939241699cdSAl Viro 	i->count -= bytes;
940241699cdSAl Viro 	return bytes;
941241699cdSAl Viro }
942241699cdSAl Viro 
943d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
944d879cb83SAl Viro {
94500e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
946241699cdSAl Viro 		return pipe_zero(bytes, i);
9477baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, count,
9487baa5099SAl Viro 		clear_user(base, len),
9497baa5099SAl Viro 		memset(base, 0, len)
950d879cb83SAl Viro 	)
951d879cb83SAl Viro 
952d879cb83SAl Viro 	return bytes;
953d879cb83SAl Viro }
954d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero);
955d879cb83SAl Viro 
956f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
957f0b65f39SAl Viro 				  struct iov_iter *i)
958d879cb83SAl Viro {
959d879cb83SAl Viro 	char *kaddr = kmap_atomic(page), *p = kaddr + offset;
96072e809edSAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes))) {
96172e809edSAl Viro 		kunmap_atomic(kaddr);
96272e809edSAl Viro 		return 0;
96372e809edSAl Viro 	}
9649ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
965241699cdSAl Viro 		kunmap_atomic(kaddr);
966241699cdSAl Viro 		WARN_ON(1);
967241699cdSAl Viro 		return 0;
968241699cdSAl Viro 	}
9697baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
9707baa5099SAl Viro 		copyin(p + off, base, len),
9717baa5099SAl Viro 		memcpy(p + off, base, len)
972d879cb83SAl Viro 	)
973d879cb83SAl Viro 	kunmap_atomic(kaddr);
974d879cb83SAl Viro 	return bytes;
975d879cb83SAl Viro }
976f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic);
977d879cb83SAl Viro 
978b9dc6f65SAl Viro static inline void pipe_truncate(struct iov_iter *i)
979241699cdSAl Viro {
980241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
9818cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
9828cefc107SDavid Howells 	unsigned int p_head = pipe->head;
9838cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
9848cefc107SDavid Howells 
9858cefc107SDavid Howells 	if (!pipe_empty(p_head, p_tail)) {
9868cefc107SDavid Howells 		struct pipe_buffer *buf;
9878cefc107SDavid Howells 		unsigned int i_head = i->head;
988b9dc6f65SAl Viro 		size_t off = i->iov_offset;
9898cefc107SDavid Howells 
990b9dc6f65SAl Viro 		if (off) {
9918cefc107SDavid Howells 			buf = &pipe->bufs[i_head & p_mask];
9928cefc107SDavid Howells 			buf->len = off - buf->offset;
9938cefc107SDavid Howells 			i_head++;
994b9dc6f65SAl Viro 		}
9958cefc107SDavid Howells 		while (p_head != i_head) {
9968cefc107SDavid Howells 			p_head--;
9978cefc107SDavid Howells 			pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
998241699cdSAl Viro 		}
9998cefc107SDavid Howells 
10008cefc107SDavid Howells 		pipe->head = p_head;
1001241699cdSAl Viro 	}
1002b9dc6f65SAl Viro }
1003b9dc6f65SAl Viro 
1004b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size)
1005b9dc6f65SAl Viro {
1006b9dc6f65SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
1007b9dc6f65SAl Viro 	if (size) {
1008b9dc6f65SAl Viro 		struct pipe_buffer *buf;
10098cefc107SDavid Howells 		unsigned int p_mask = pipe->ring_size - 1;
10108cefc107SDavid Howells 		unsigned int i_head = i->head;
1011b9dc6f65SAl Viro 		size_t off = i->iov_offset, left = size;
10128cefc107SDavid Howells 
1013b9dc6f65SAl Viro 		if (off) /* make it relative to the beginning of buffer */
10148cefc107SDavid Howells 			left += off - pipe->bufs[i_head & p_mask].offset;
1015b9dc6f65SAl Viro 		while (1) {
10168cefc107SDavid Howells 			buf = &pipe->bufs[i_head & p_mask];
1017b9dc6f65SAl Viro 			if (left <= buf->len)
1018b9dc6f65SAl Viro 				break;
1019b9dc6f65SAl Viro 			left -= buf->len;
10208cefc107SDavid Howells 			i_head++;
1021b9dc6f65SAl Viro 		}
10228cefc107SDavid Howells 		i->head = i_head;
1023b9dc6f65SAl Viro 		i->iov_offset = buf->offset + left;
1024b9dc6f65SAl Viro 	}
1025b9dc6f65SAl Viro 	i->count -= size;
1026b9dc6f65SAl Viro 	/* ... and discard everything past that point */
1027b9dc6f65SAl Viro 	pipe_truncate(i);
1028241699cdSAl Viro }
1029241699cdSAl Viro 
103054c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
103154c8195bSPavel Begunkov {
103254c8195bSPavel Begunkov 	struct bvec_iter bi;
103354c8195bSPavel Begunkov 
103454c8195bSPavel Begunkov 	bi.bi_size = i->count;
103554c8195bSPavel Begunkov 	bi.bi_bvec_done = i->iov_offset;
103654c8195bSPavel Begunkov 	bi.bi_idx = 0;
103754c8195bSPavel Begunkov 	bvec_iter_advance(i->bvec, &bi, size);
103854c8195bSPavel Begunkov 
103954c8195bSPavel Begunkov 	i->bvec += bi.bi_idx;
104054c8195bSPavel Begunkov 	i->nr_segs -= bi.bi_idx;
104154c8195bSPavel Begunkov 	i->count = bi.bi_size;
104254c8195bSPavel Begunkov 	i->iov_offset = bi.bi_bvec_done;
104354c8195bSPavel Begunkov }
104454c8195bSPavel Begunkov 
1045185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
1046185ac4d4SAl Viro {
1047185ac4d4SAl Viro 	const struct iovec *iov, *end;
1048185ac4d4SAl Viro 
1049185ac4d4SAl Viro 	if (!i->count)
1050185ac4d4SAl Viro 		return;
1051185ac4d4SAl Viro 	i->count -= size;
1052185ac4d4SAl Viro 
1053185ac4d4SAl Viro 	size += i->iov_offset; // from beginning of current segment
1054185ac4d4SAl Viro 	for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
1055185ac4d4SAl Viro 		if (likely(size < iov->iov_len))
1056185ac4d4SAl Viro 			break;
1057185ac4d4SAl Viro 		size -= iov->iov_len;
1058185ac4d4SAl Viro 	}
1059185ac4d4SAl Viro 	i->iov_offset = size;
1060185ac4d4SAl Viro 	i->nr_segs -= iov - i->iov;
1061185ac4d4SAl Viro 	i->iov = iov;
1062185ac4d4SAl Viro }
1063185ac4d4SAl Viro 
1064d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size)
1065d879cb83SAl Viro {
10663b3fc051SAl Viro 	if (unlikely(i->count < size))
10673b3fc051SAl Viro 		size = i->count;
1068185ac4d4SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
1069185ac4d4SAl Viro 		/* iovec and kvec have identical layouts */
1070185ac4d4SAl Viro 		iov_iter_iovec_advance(i, size);
1071185ac4d4SAl Viro 	} else if (iov_iter_is_bvec(i)) {
1072185ac4d4SAl Viro 		iov_iter_bvec_advance(i, size);
1073185ac4d4SAl Viro 	} else if (iov_iter_is_pipe(i)) {
1074241699cdSAl Viro 		pipe_advance(i, size);
1075185ac4d4SAl Viro 	} else if (unlikely(iov_iter_is_xarray(i))) {
10767ff50620SDavid Howells 		i->iov_offset += size;
10777ff50620SDavid Howells 		i->count -= size;
1078185ac4d4SAl Viro 	} else if (iov_iter_is_discard(i)) {
1079185ac4d4SAl Viro 		i->count -= size;
10807ff50620SDavid Howells 	}
1081d879cb83SAl Viro }
1082d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance);
1083d879cb83SAl Viro 
108427c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll)
108527c0e374SAl Viro {
108627c0e374SAl Viro 	if (!unroll)
108727c0e374SAl Viro 		return;
10885b47d59aSAl Viro 	if (WARN_ON(unroll > MAX_RW_COUNT))
10895b47d59aSAl Viro 		return;
109027c0e374SAl Viro 	i->count += unroll;
109100e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
109227c0e374SAl Viro 		struct pipe_inode_info *pipe = i->pipe;
10938cefc107SDavid Howells 		unsigned int p_mask = pipe->ring_size - 1;
10948cefc107SDavid Howells 		unsigned int i_head = i->head;
109527c0e374SAl Viro 		size_t off = i->iov_offset;
109627c0e374SAl Viro 		while (1) {
10978cefc107SDavid Howells 			struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
10988cefc107SDavid Howells 			size_t n = off - b->offset;
109927c0e374SAl Viro 			if (unroll < n) {
11004fa55cefSAl Viro 				off -= unroll;
110127c0e374SAl Viro 				break;
110227c0e374SAl Viro 			}
110327c0e374SAl Viro 			unroll -= n;
11048cefc107SDavid Howells 			if (!unroll && i_head == i->start_head) {
110527c0e374SAl Viro 				off = 0;
110627c0e374SAl Viro 				break;
110727c0e374SAl Viro 			}
11088cefc107SDavid Howells 			i_head--;
11098cefc107SDavid Howells 			b = &pipe->bufs[i_head & p_mask];
11108cefc107SDavid Howells 			off = b->offset + b->len;
111127c0e374SAl Viro 		}
111227c0e374SAl Viro 		i->iov_offset = off;
11138cefc107SDavid Howells 		i->head = i_head;
111427c0e374SAl Viro 		pipe_truncate(i);
111527c0e374SAl Viro 		return;
111627c0e374SAl Viro 	}
11179ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_discard(i)))
11189ea9ce04SDavid Howells 		return;
111927c0e374SAl Viro 	if (unroll <= i->iov_offset) {
112027c0e374SAl Viro 		i->iov_offset -= unroll;
112127c0e374SAl Viro 		return;
112227c0e374SAl Viro 	}
112327c0e374SAl Viro 	unroll -= i->iov_offset;
11247ff50620SDavid Howells 	if (iov_iter_is_xarray(i)) {
11257ff50620SDavid Howells 		BUG(); /* We should never go beyond the start of the specified
11267ff50620SDavid Howells 			* range since we might then be straying into pages that
11277ff50620SDavid Howells 			* aren't pinned.
11287ff50620SDavid Howells 			*/
11297ff50620SDavid Howells 	} else if (iov_iter_is_bvec(i)) {
113027c0e374SAl Viro 		const struct bio_vec *bvec = i->bvec;
113127c0e374SAl Viro 		while (1) {
113227c0e374SAl Viro 			size_t n = (--bvec)->bv_len;
113327c0e374SAl Viro 			i->nr_segs++;
113427c0e374SAl Viro 			if (unroll <= n) {
113527c0e374SAl Viro 				i->bvec = bvec;
113627c0e374SAl Viro 				i->iov_offset = n - unroll;
113727c0e374SAl Viro 				return;
113827c0e374SAl Viro 			}
113927c0e374SAl Viro 			unroll -= n;
114027c0e374SAl Viro 		}
114127c0e374SAl Viro 	} else { /* same logics for iovec and kvec */
114227c0e374SAl Viro 		const struct iovec *iov = i->iov;
114327c0e374SAl Viro 		while (1) {
114427c0e374SAl Viro 			size_t n = (--iov)->iov_len;
114527c0e374SAl Viro 			i->nr_segs++;
114627c0e374SAl Viro 			if (unroll <= n) {
114727c0e374SAl Viro 				i->iov = iov;
114827c0e374SAl Viro 				i->iov_offset = n - unroll;
114927c0e374SAl Viro 				return;
115027c0e374SAl Viro 			}
115127c0e374SAl Viro 			unroll -= n;
115227c0e374SAl Viro 		}
115327c0e374SAl Viro 	}
115427c0e374SAl Viro }
115527c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert);
115627c0e374SAl Viro 
1157d879cb83SAl Viro /*
1158d879cb83SAl Viro  * Return the count of just the current iov_iter segment.
1159d879cb83SAl Viro  */
1160d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i)
1161d879cb83SAl Viro {
116228f38db7SAl Viro 	if (i->nr_segs > 1) {
116328f38db7SAl Viro 		if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
116428f38db7SAl Viro 			return min(i->count, i->iov->iov_len - i->iov_offset);
11657ff50620SDavid Howells 		if (iov_iter_is_bvec(i))
1166d879cb83SAl Viro 			return min(i->count, i->bvec->bv_len - i->iov_offset);
116728f38db7SAl Viro 	}
116828f38db7SAl Viro 	return i->count;
1169d879cb83SAl Viro }
1170d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count);
1171d879cb83SAl Viro 
1172aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
1173d879cb83SAl Viro 			const struct kvec *kvec, unsigned long nr_segs,
1174d879cb83SAl Viro 			size_t count)
1175d879cb83SAl Viro {
1176aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
11778cd54c1cSAl Viro 	*i = (struct iov_iter){
11788cd54c1cSAl Viro 		.iter_type = ITER_KVEC,
11798cd54c1cSAl Viro 		.data_source = direction,
11808cd54c1cSAl Viro 		.kvec = kvec,
11818cd54c1cSAl Viro 		.nr_segs = nr_segs,
11828cd54c1cSAl Viro 		.iov_offset = 0,
11838cd54c1cSAl Viro 		.count = count
11848cd54c1cSAl Viro 	};
1185d879cb83SAl Viro }
1186d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec);
1187d879cb83SAl Viro 
1188aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1189d879cb83SAl Viro 			const struct bio_vec *bvec, unsigned long nr_segs,
1190d879cb83SAl Viro 			size_t count)
1191d879cb83SAl Viro {
1192aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
11938cd54c1cSAl Viro 	*i = (struct iov_iter){
11948cd54c1cSAl Viro 		.iter_type = ITER_BVEC,
11958cd54c1cSAl Viro 		.data_source = direction,
11968cd54c1cSAl Viro 		.bvec = bvec,
11978cd54c1cSAl Viro 		.nr_segs = nr_segs,
11988cd54c1cSAl Viro 		.iov_offset = 0,
11998cd54c1cSAl Viro 		.count = count
12008cd54c1cSAl Viro 	};
1201d879cb83SAl Viro }
1202d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec);
1203d879cb83SAl Viro 
1204aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1205241699cdSAl Viro 			struct pipe_inode_info *pipe,
1206241699cdSAl Viro 			size_t count)
1207241699cdSAl Viro {
1208aa563d7bSDavid Howells 	BUG_ON(direction != READ);
12098cefc107SDavid Howells 	WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
12108cd54c1cSAl Viro 	*i = (struct iov_iter){
12118cd54c1cSAl Viro 		.iter_type = ITER_PIPE,
12128cd54c1cSAl Viro 		.data_source = false,
12138cd54c1cSAl Viro 		.pipe = pipe,
12148cd54c1cSAl Viro 		.head = pipe->head,
12158cd54c1cSAl Viro 		.start_head = pipe->head,
12168cd54c1cSAl Viro 		.iov_offset = 0,
12178cd54c1cSAl Viro 		.count = count
12188cd54c1cSAl Viro 	};
1219241699cdSAl Viro }
1220241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe);
1221241699cdSAl Viro 
12229ea9ce04SDavid Howells /**
12237ff50620SDavid Howells  * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
12247ff50620SDavid Howells  * @i: The iterator to initialise.
12257ff50620SDavid Howells  * @direction: The direction of the transfer.
12267ff50620SDavid Howells  * @xarray: The xarray to access.
12277ff50620SDavid Howells  * @start: The start file position.
12287ff50620SDavid Howells  * @count: The size of the I/O buffer in bytes.
12297ff50620SDavid Howells  *
12307ff50620SDavid Howells  * Set up an I/O iterator to either draw data out of the pages attached to an
12317ff50620SDavid Howells  * inode or to inject data into those pages.  The pages *must* be prevented
12327ff50620SDavid Howells  * from evaporation, either by taking a ref on them or locking them by the
12337ff50620SDavid Howells  * caller.
12347ff50620SDavid Howells  */
12357ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
12367ff50620SDavid Howells 		     struct xarray *xarray, loff_t start, size_t count)
12377ff50620SDavid Howells {
12387ff50620SDavid Howells 	BUG_ON(direction & ~1);
12398cd54c1cSAl Viro 	*i = (struct iov_iter) {
12408cd54c1cSAl Viro 		.iter_type = ITER_XARRAY,
12418cd54c1cSAl Viro 		.data_source = direction,
12428cd54c1cSAl Viro 		.xarray = xarray,
12438cd54c1cSAl Viro 		.xarray_start = start,
12448cd54c1cSAl Viro 		.count = count,
12458cd54c1cSAl Viro 		.iov_offset = 0
12468cd54c1cSAl Viro 	};
12477ff50620SDavid Howells }
12487ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray);
12497ff50620SDavid Howells 
12507ff50620SDavid Howells /**
12519ea9ce04SDavid Howells  * iov_iter_discard - Initialise an I/O iterator that discards data
12529ea9ce04SDavid Howells  * @i: The iterator to initialise.
12539ea9ce04SDavid Howells  * @direction: The direction of the transfer.
12549ea9ce04SDavid Howells  * @count: The size of the I/O buffer in bytes.
12559ea9ce04SDavid Howells  *
12569ea9ce04SDavid Howells  * Set up an I/O iterator that just discards everything that's written to it.
12579ea9ce04SDavid Howells  * It's only available as a READ iterator.
12589ea9ce04SDavid Howells  */
12599ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
12609ea9ce04SDavid Howells {
12619ea9ce04SDavid Howells 	BUG_ON(direction != READ);
12628cd54c1cSAl Viro 	*i = (struct iov_iter){
12638cd54c1cSAl Viro 		.iter_type = ITER_DISCARD,
12648cd54c1cSAl Viro 		.data_source = false,
12658cd54c1cSAl Viro 		.count = count,
12668cd54c1cSAl Viro 		.iov_offset = 0
12678cd54c1cSAl Viro 	};
12689ea9ce04SDavid Howells }
12699ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard);
12709ea9ce04SDavid Howells 
1271*cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
1272*cfa320f7SKeith Busch 				   unsigned len_mask)
1273*cfa320f7SKeith Busch {
1274*cfa320f7SKeith Busch 	size_t size = i->count;
1275*cfa320f7SKeith Busch 	size_t skip = i->iov_offset;
1276*cfa320f7SKeith Busch 	unsigned k;
1277*cfa320f7SKeith Busch 
1278*cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
1279*cfa320f7SKeith Busch 		size_t len = i->iov[k].iov_len - skip;
1280*cfa320f7SKeith Busch 
1281*cfa320f7SKeith Busch 		if (len > size)
1282*cfa320f7SKeith Busch 			len = size;
1283*cfa320f7SKeith Busch 		if (len & len_mask)
1284*cfa320f7SKeith Busch 			return false;
1285*cfa320f7SKeith Busch 		if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask)
1286*cfa320f7SKeith Busch 			return false;
1287*cfa320f7SKeith Busch 
1288*cfa320f7SKeith Busch 		size -= len;
1289*cfa320f7SKeith Busch 		if (!size)
1290*cfa320f7SKeith Busch 			break;
1291*cfa320f7SKeith Busch 	}
1292*cfa320f7SKeith Busch 	return true;
1293*cfa320f7SKeith Busch }
1294*cfa320f7SKeith Busch 
1295*cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
1296*cfa320f7SKeith Busch 				  unsigned len_mask)
1297*cfa320f7SKeith Busch {
1298*cfa320f7SKeith Busch 	size_t size = i->count;
1299*cfa320f7SKeith Busch 	unsigned skip = i->iov_offset;
1300*cfa320f7SKeith Busch 	unsigned k;
1301*cfa320f7SKeith Busch 
1302*cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
1303*cfa320f7SKeith Busch 		size_t len = i->bvec[k].bv_len - skip;
1304*cfa320f7SKeith Busch 
1305*cfa320f7SKeith Busch 		if (len > size)
1306*cfa320f7SKeith Busch 			len = size;
1307*cfa320f7SKeith Busch 		if (len & len_mask)
1308*cfa320f7SKeith Busch 			return false;
1309*cfa320f7SKeith Busch 		if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
1310*cfa320f7SKeith Busch 			return false;
1311*cfa320f7SKeith Busch 
1312*cfa320f7SKeith Busch 		size -= len;
1313*cfa320f7SKeith Busch 		if (!size)
1314*cfa320f7SKeith Busch 			break;
1315*cfa320f7SKeith Busch 	}
1316*cfa320f7SKeith Busch 	return true;
1317*cfa320f7SKeith Busch }
1318*cfa320f7SKeith Busch 
1319*cfa320f7SKeith Busch /**
1320*cfa320f7SKeith Busch  * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
1321*cfa320f7SKeith Busch  * 	are aligned to the parameters.
1322*cfa320f7SKeith Busch  *
1323*cfa320f7SKeith Busch  * @i: &struct iov_iter to restore
1324*cfa320f7SKeith Busch  * @addr_mask: bit mask to check against the iov element's addresses
1325*cfa320f7SKeith Busch  * @len_mask: bit mask to check against the iov element's lengths
1326*cfa320f7SKeith Busch  *
1327*cfa320f7SKeith Busch  * Return: false if any addresses or lengths intersect with the provided masks
1328*cfa320f7SKeith Busch  */
1329*cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
1330*cfa320f7SKeith Busch 			 unsigned len_mask)
1331*cfa320f7SKeith Busch {
1332*cfa320f7SKeith Busch 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1333*cfa320f7SKeith Busch 		return iov_iter_aligned_iovec(i, addr_mask, len_mask);
1334*cfa320f7SKeith Busch 
1335*cfa320f7SKeith Busch 	if (iov_iter_is_bvec(i))
1336*cfa320f7SKeith Busch 		return iov_iter_aligned_bvec(i, addr_mask, len_mask);
1337*cfa320f7SKeith Busch 
1338*cfa320f7SKeith Busch 	if (iov_iter_is_pipe(i)) {
1339*cfa320f7SKeith Busch 		unsigned int p_mask = i->pipe->ring_size - 1;
1340*cfa320f7SKeith Busch 		size_t size = i->count;
1341*cfa320f7SKeith Busch 
1342*cfa320f7SKeith Busch 		if (size & len_mask)
1343*cfa320f7SKeith Busch 			return false;
1344*cfa320f7SKeith Busch 		if (size && allocated(&i->pipe->bufs[i->head & p_mask])) {
1345*cfa320f7SKeith Busch 			if (i->iov_offset & addr_mask)
1346*cfa320f7SKeith Busch 				return false;
1347*cfa320f7SKeith Busch 		}
1348*cfa320f7SKeith Busch 
1349*cfa320f7SKeith Busch 		return true;
1350*cfa320f7SKeith Busch 	}
1351*cfa320f7SKeith Busch 
1352*cfa320f7SKeith Busch 	if (iov_iter_is_xarray(i)) {
1353*cfa320f7SKeith Busch 		if (i->count & len_mask)
1354*cfa320f7SKeith Busch 			return false;
1355*cfa320f7SKeith Busch 		if ((i->xarray_start + i->iov_offset) & addr_mask)
1356*cfa320f7SKeith Busch 			return false;
1357*cfa320f7SKeith Busch 	}
1358*cfa320f7SKeith Busch 
1359*cfa320f7SKeith Busch 	return true;
1360*cfa320f7SKeith Busch }
1361*cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
1362*cfa320f7SKeith Busch 
13639221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1364d879cb83SAl Viro {
1365d879cb83SAl Viro 	unsigned long res = 0;
1366d879cb83SAl Viro 	size_t size = i->count;
13679221d2e3SAl Viro 	size_t skip = i->iov_offset;
13689221d2e3SAl Viro 	unsigned k;
1369d879cb83SAl Viro 
13709221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
13719221d2e3SAl Viro 		size_t len = i->iov[k].iov_len - skip;
13729221d2e3SAl Viro 		if (len) {
13739221d2e3SAl Viro 			res |= (unsigned long)i->iov[k].iov_base + skip;
13749221d2e3SAl Viro 			if (len > size)
13759221d2e3SAl Viro 				len = size;
13769221d2e3SAl Viro 			res |= len;
13779221d2e3SAl Viro 			size -= len;
13789221d2e3SAl Viro 			if (!size)
13799221d2e3SAl Viro 				break;
13809221d2e3SAl Viro 		}
13819221d2e3SAl Viro 	}
13829221d2e3SAl Viro 	return res;
13839221d2e3SAl Viro }
13849221d2e3SAl Viro 
13859221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
13869221d2e3SAl Viro {
13879221d2e3SAl Viro 	unsigned res = 0;
13889221d2e3SAl Viro 	size_t size = i->count;
13899221d2e3SAl Viro 	unsigned skip = i->iov_offset;
13909221d2e3SAl Viro 	unsigned k;
13919221d2e3SAl Viro 
13929221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
13939221d2e3SAl Viro 		size_t len = i->bvec[k].bv_len - skip;
13949221d2e3SAl Viro 		res |= (unsigned long)i->bvec[k].bv_offset + skip;
13959221d2e3SAl Viro 		if (len > size)
13969221d2e3SAl Viro 			len = size;
13979221d2e3SAl Viro 		res |= len;
13989221d2e3SAl Viro 		size -= len;
13999221d2e3SAl Viro 		if (!size)
14009221d2e3SAl Viro 			break;
14019221d2e3SAl Viro 	}
14029221d2e3SAl Viro 	return res;
14039221d2e3SAl Viro }
14049221d2e3SAl Viro 
14059221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i)
14069221d2e3SAl Viro {
14079221d2e3SAl Viro 	/* iovec and kvec have identical layouts */
14089221d2e3SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
14099221d2e3SAl Viro 		return iov_iter_alignment_iovec(i);
14109221d2e3SAl Viro 
14119221d2e3SAl Viro 	if (iov_iter_is_bvec(i))
14129221d2e3SAl Viro 		return iov_iter_alignment_bvec(i);
14139221d2e3SAl Viro 
14149221d2e3SAl Viro 	if (iov_iter_is_pipe(i)) {
1415e0ff126eSJan Kara 		unsigned int p_mask = i->pipe->ring_size - 1;
14169221d2e3SAl Viro 		size_t size = i->count;
1417e0ff126eSJan Kara 
14188cefc107SDavid Howells 		if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
1419241699cdSAl Viro 			return size | i->iov_offset;
1420241699cdSAl Viro 		return size;
1421241699cdSAl Viro 	}
14229221d2e3SAl Viro 
14239221d2e3SAl Viro 	if (iov_iter_is_xarray(i))
14243d14ec1fSDavid Howells 		return (i->xarray_start + i->iov_offset) | i->count;
14259221d2e3SAl Viro 
14269221d2e3SAl Viro 	return 0;
1427d879cb83SAl Viro }
1428d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment);
1429d879cb83SAl Viro 
1430357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1431357f435dSAl Viro {
1432357f435dSAl Viro 	unsigned long res = 0;
1433610c7a71SAl Viro 	unsigned long v = 0;
1434357f435dSAl Viro 	size_t size = i->count;
1435610c7a71SAl Viro 	unsigned k;
1436357f435dSAl Viro 
1437610c7a71SAl Viro 	if (WARN_ON(!iter_is_iovec(i)))
1438241699cdSAl Viro 		return ~0U;
1439241699cdSAl Viro 
1440610c7a71SAl Viro 	for (k = 0; k < i->nr_segs; k++) {
1441610c7a71SAl Viro 		if (i->iov[k].iov_len) {
1442610c7a71SAl Viro 			unsigned long base = (unsigned long)i->iov[k].iov_base;
1443610c7a71SAl Viro 			if (v) // if not the first one
1444610c7a71SAl Viro 				res |= base | v; // this start | previous end
1445610c7a71SAl Viro 			v = base + i->iov[k].iov_len;
1446610c7a71SAl Viro 			if (size <= i->iov[k].iov_len)
1447610c7a71SAl Viro 				break;
1448610c7a71SAl Viro 			size -= i->iov[k].iov_len;
1449610c7a71SAl Viro 		}
1450610c7a71SAl Viro 	}
1451357f435dSAl Viro 	return res;
1452357f435dSAl Viro }
1453357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment);
1454357f435dSAl Viro 
1455e76b6312SIlya Dryomov static inline ssize_t __pipe_get_pages(struct iov_iter *i,
1456241699cdSAl Viro 				size_t maxsize,
1457241699cdSAl Viro 				struct page **pages,
14588cefc107SDavid Howells 				int iter_head,
1459241699cdSAl Viro 				size_t *start)
1460241699cdSAl Viro {
1461241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
14628cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
14638cefc107SDavid Howells 	ssize_t n = push_pipe(i, maxsize, &iter_head, start);
1464241699cdSAl Viro 	if (!n)
1465241699cdSAl Viro 		return -EFAULT;
1466241699cdSAl Viro 
1467241699cdSAl Viro 	maxsize = n;
1468241699cdSAl Viro 	n += *start;
14691689c73aSAl Viro 	while (n > 0) {
14708cefc107SDavid Howells 		get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
14718cefc107SDavid Howells 		iter_head++;
1472241699cdSAl Viro 		n -= PAGE_SIZE;
1473241699cdSAl Viro 	}
1474241699cdSAl Viro 
1475241699cdSAl Viro 	return maxsize;
1476241699cdSAl Viro }
1477241699cdSAl Viro 
1478241699cdSAl Viro static ssize_t pipe_get_pages(struct iov_iter *i,
1479241699cdSAl Viro 		   struct page **pages, size_t maxsize, unsigned maxpages,
1480241699cdSAl Viro 		   size_t *start)
1481241699cdSAl Viro {
14828cefc107SDavid Howells 	unsigned int iter_head, npages;
1483241699cdSAl Viro 	size_t capacity;
1484241699cdSAl Viro 
1485241699cdSAl Viro 	if (!sanity(i))
1486241699cdSAl Viro 		return -EFAULT;
1487241699cdSAl Viro 
14888cefc107SDavid Howells 	data_start(i, &iter_head, start);
14898cefc107SDavid Howells 	/* Amount of free space: some of this one + all after this one */
14908cefc107SDavid Howells 	npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1491241699cdSAl Viro 	capacity = min(npages, maxpages) * PAGE_SIZE - *start;
1492241699cdSAl Viro 
14938cefc107SDavid Howells 	return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
1494241699cdSAl Viro }
1495241699cdSAl Viro 
14967ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
14977ff50620SDavid Howells 					  pgoff_t index, unsigned int nr_pages)
14987ff50620SDavid Howells {
14997ff50620SDavid Howells 	XA_STATE(xas, xa, index);
15007ff50620SDavid Howells 	struct page *page;
15017ff50620SDavid Howells 	unsigned int ret = 0;
15027ff50620SDavid Howells 
15037ff50620SDavid Howells 	rcu_read_lock();
15047ff50620SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
15057ff50620SDavid Howells 		if (xas_retry(&xas, page))
15067ff50620SDavid Howells 			continue;
15077ff50620SDavid Howells 
15087ff50620SDavid Howells 		/* Has the page moved or been split? */
15097ff50620SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
15107ff50620SDavid Howells 			xas_reset(&xas);
15117ff50620SDavid Howells 			continue;
15127ff50620SDavid Howells 		}
15137ff50620SDavid Howells 
15147ff50620SDavid Howells 		pages[ret] = find_subpage(page, xas.xa_index);
15157ff50620SDavid Howells 		get_page(pages[ret]);
15167ff50620SDavid Howells 		if (++ret == nr_pages)
15177ff50620SDavid Howells 			break;
15187ff50620SDavid Howells 	}
15197ff50620SDavid Howells 	rcu_read_unlock();
15207ff50620SDavid Howells 	return ret;
15217ff50620SDavid Howells }
15227ff50620SDavid Howells 
15237ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i,
15247ff50620SDavid Howells 				     struct page **pages, size_t maxsize,
15257ff50620SDavid Howells 				     unsigned maxpages, size_t *_start_offset)
15267ff50620SDavid Howells {
15277ff50620SDavid Howells 	unsigned nr, offset;
15287ff50620SDavid Howells 	pgoff_t index, count;
15296c776766SDavid Howells 	size_t size = maxsize;
15307ff50620SDavid Howells 	loff_t pos;
15317ff50620SDavid Howells 
15327ff50620SDavid Howells 	if (!size || !maxpages)
15337ff50620SDavid Howells 		return 0;
15347ff50620SDavid Howells 
15357ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
15367ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
15377ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
15387ff50620SDavid Howells 	*_start_offset = offset;
15397ff50620SDavid Howells 
15407ff50620SDavid Howells 	count = 1;
15417ff50620SDavid Howells 	if (size > PAGE_SIZE - offset) {
15427ff50620SDavid Howells 		size -= PAGE_SIZE - offset;
15437ff50620SDavid Howells 		count += size >> PAGE_SHIFT;
15447ff50620SDavid Howells 		size &= ~PAGE_MASK;
15457ff50620SDavid Howells 		if (size)
15467ff50620SDavid Howells 			count++;
15477ff50620SDavid Howells 	}
15487ff50620SDavid Howells 
15497ff50620SDavid Howells 	if (count > maxpages)
15507ff50620SDavid Howells 		count = maxpages;
15517ff50620SDavid Howells 
15527ff50620SDavid Howells 	nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
15537ff50620SDavid Howells 	if (nr == 0)
15547ff50620SDavid Howells 		return 0;
15557ff50620SDavid Howells 
15561c27f1fcSLinus Torvalds 	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
15577ff50620SDavid Howells }
15587ff50620SDavid Howells 
15593d671ca6SAl Viro /* must be done on non-empty ITER_IOVEC one */
15603d671ca6SAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i,
15613d671ca6SAl Viro 					 size_t *size, size_t *start,
15623d671ca6SAl Viro 					 size_t maxsize, unsigned maxpages)
15633d671ca6SAl Viro {
15643d671ca6SAl Viro 	size_t skip;
15653d671ca6SAl Viro 	long k;
15663d671ca6SAl Viro 
15673d671ca6SAl Viro 	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
15683d671ca6SAl Viro 		unsigned long addr = (unsigned long)i->iov[k].iov_base + skip;
15693d671ca6SAl Viro 		size_t len = i->iov[k].iov_len - skip;
15703d671ca6SAl Viro 
15713d671ca6SAl Viro 		if (unlikely(!len))
15723d671ca6SAl Viro 			continue;
15733d671ca6SAl Viro 		if (len > maxsize)
15743d671ca6SAl Viro 			len = maxsize;
15753d671ca6SAl Viro 		len += (*start = addr % PAGE_SIZE);
15763d671ca6SAl Viro 		if (len > maxpages * PAGE_SIZE)
15773d671ca6SAl Viro 			len = maxpages * PAGE_SIZE;
15783d671ca6SAl Viro 		*size = len;
15793d671ca6SAl Viro 		return addr & PAGE_MASK;
15803d671ca6SAl Viro 	}
15813d671ca6SAl Viro 	BUG(); // if it had been empty, we wouldn't get called
15823d671ca6SAl Viro }
15833d671ca6SAl Viro 
15843d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */
15853d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i,
15863d671ca6SAl Viro 				       size_t *size, size_t *start,
15873d671ca6SAl Viro 				       size_t maxsize, unsigned maxpages)
15883d671ca6SAl Viro {
15893d671ca6SAl Viro 	struct page *page;
15903d671ca6SAl Viro 	size_t skip = i->iov_offset, len;
15913d671ca6SAl Viro 
15923d671ca6SAl Viro 	len = i->bvec->bv_len - skip;
15933d671ca6SAl Viro 	if (len > maxsize)
15943d671ca6SAl Viro 		len = maxsize;
15953d671ca6SAl Viro 	skip += i->bvec->bv_offset;
15963d671ca6SAl Viro 	page = i->bvec->bv_page + skip / PAGE_SIZE;
15973d671ca6SAl Viro 	len += (*start = skip % PAGE_SIZE);
15983d671ca6SAl Viro 	if (len > maxpages * PAGE_SIZE)
15993d671ca6SAl Viro 		len = maxpages * PAGE_SIZE;
16003d671ca6SAl Viro 	*size = len;
16013d671ca6SAl Viro 	return page;
16023d671ca6SAl Viro }
16033d671ca6SAl Viro 
1604d879cb83SAl Viro ssize_t iov_iter_get_pages(struct iov_iter *i,
1605d879cb83SAl Viro 		   struct page **pages, size_t maxsize, unsigned maxpages,
1606d879cb83SAl Viro 		   size_t *start)
1607d879cb83SAl Viro {
16083d671ca6SAl Viro 	size_t len;
16093d671ca6SAl Viro 	int n, res;
16103d671ca6SAl Viro 
1611d879cb83SAl Viro 	if (maxsize > i->count)
1612d879cb83SAl Viro 		maxsize = i->count;
16133d671ca6SAl Viro 	if (!maxsize)
16143d671ca6SAl Viro 		return 0;
1615d879cb83SAl Viro 
16163d671ca6SAl Viro 	if (likely(iter_is_iovec(i))) {
16173337ab08SAndreas Gruenbacher 		unsigned int gup_flags = 0;
16183d671ca6SAl Viro 		unsigned long addr;
16199ea9ce04SDavid Howells 
16203337ab08SAndreas Gruenbacher 		if (iov_iter_rw(i) != WRITE)
16213337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_WRITE;
16223337ab08SAndreas Gruenbacher 		if (i->nofault)
16233337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_NOFAULT;
16243337ab08SAndreas Gruenbacher 
16253d671ca6SAl Viro 		addr = first_iovec_segment(i, &len, start, maxsize, maxpages);
1626d879cb83SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
16273337ab08SAndreas Gruenbacher 		res = get_user_pages_fast(addr, n, gup_flags, pages);
1628814a6674SAndreas Gruenbacher 		if (unlikely(res <= 0))
1629d879cb83SAl Viro 			return res;
1630d879cb83SAl Viro 		return (res == n ? len : res * PAGE_SIZE) - *start;
16313d671ca6SAl Viro 	}
16323d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
16333d671ca6SAl Viro 		struct page *page;
16343d671ca6SAl Viro 
16353d671ca6SAl Viro 		page = first_bvec_segment(i, &len, start, maxsize, maxpages);
16363d671ca6SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
16373d671ca6SAl Viro 		while (n--)
16383d671ca6SAl Viro 			get_page(*pages++ = page++);
16393d671ca6SAl Viro 		return len - *start;
16403d671ca6SAl Viro 	}
16413d671ca6SAl Viro 	if (iov_iter_is_pipe(i))
16423d671ca6SAl Viro 		return pipe_get_pages(i, pages, maxsize, maxpages, start);
16433d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
16443d671ca6SAl Viro 		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1645d879cb83SAl Viro 	return -EFAULT;
1646d879cb83SAl Viro }
1647d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_get_pages);
1648d879cb83SAl Viro 
1649d879cb83SAl Viro static struct page **get_pages_array(size_t n)
1650d879cb83SAl Viro {
1651752ade68SMichal Hocko 	return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
1652d879cb83SAl Viro }
1653d879cb83SAl Viro 
1654241699cdSAl Viro static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1655241699cdSAl Viro 		   struct page ***pages, size_t maxsize,
1656241699cdSAl Viro 		   size_t *start)
1657241699cdSAl Viro {
1658241699cdSAl Viro 	struct page **p;
16598cefc107SDavid Howells 	unsigned int iter_head, npages;
1660d7760d63SIlya Dryomov 	ssize_t n;
1661241699cdSAl Viro 
1662241699cdSAl Viro 	if (!sanity(i))
1663241699cdSAl Viro 		return -EFAULT;
1664241699cdSAl Viro 
16658cefc107SDavid Howells 	data_start(i, &iter_head, start);
16668cefc107SDavid Howells 	/* Amount of free space: some of this one + all after this one */
16678cefc107SDavid Howells 	npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1668241699cdSAl Viro 	n = npages * PAGE_SIZE - *start;
1669241699cdSAl Viro 	if (maxsize > n)
1670241699cdSAl Viro 		maxsize = n;
1671241699cdSAl Viro 	else
1672241699cdSAl Viro 		npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1673241699cdSAl Viro 	p = get_pages_array(npages);
1674241699cdSAl Viro 	if (!p)
1675241699cdSAl Viro 		return -ENOMEM;
16768cefc107SDavid Howells 	n = __pipe_get_pages(i, maxsize, p, iter_head, start);
1677241699cdSAl Viro 	if (n > 0)
1678241699cdSAl Viro 		*pages = p;
1679241699cdSAl Viro 	else
1680241699cdSAl Viro 		kvfree(p);
1681241699cdSAl Viro 	return n;
1682241699cdSAl Viro }
1683241699cdSAl Viro 
16847ff50620SDavid Howells static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
16857ff50620SDavid Howells 					   struct page ***pages, size_t maxsize,
16867ff50620SDavid Howells 					   size_t *_start_offset)
16877ff50620SDavid Howells {
16887ff50620SDavid Howells 	struct page **p;
16897ff50620SDavid Howells 	unsigned nr, offset;
16907ff50620SDavid Howells 	pgoff_t index, count;
16916c776766SDavid Howells 	size_t size = maxsize;
16927ff50620SDavid Howells 	loff_t pos;
16937ff50620SDavid Howells 
16947ff50620SDavid Howells 	if (!size)
16957ff50620SDavid Howells 		return 0;
16967ff50620SDavid Howells 
16977ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
16987ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
16997ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
17007ff50620SDavid Howells 	*_start_offset = offset;
17017ff50620SDavid Howells 
17027ff50620SDavid Howells 	count = 1;
17037ff50620SDavid Howells 	if (size > PAGE_SIZE - offset) {
17047ff50620SDavid Howells 		size -= PAGE_SIZE - offset;
17057ff50620SDavid Howells 		count += size >> PAGE_SHIFT;
17067ff50620SDavid Howells 		size &= ~PAGE_MASK;
17077ff50620SDavid Howells 		if (size)
17087ff50620SDavid Howells 			count++;
17097ff50620SDavid Howells 	}
17107ff50620SDavid Howells 
17117ff50620SDavid Howells 	p = get_pages_array(count);
17127ff50620SDavid Howells 	if (!p)
17137ff50620SDavid Howells 		return -ENOMEM;
17147ff50620SDavid Howells 	*pages = p;
17157ff50620SDavid Howells 
17167ff50620SDavid Howells 	nr = iter_xarray_populate_pages(p, i->xarray, index, count);
17177ff50620SDavid Howells 	if (nr == 0)
17187ff50620SDavid Howells 		return 0;
17197ff50620SDavid Howells 
17201c27f1fcSLinus Torvalds 	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
17217ff50620SDavid Howells }
17227ff50620SDavid Howells 
1723d879cb83SAl Viro ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1724d879cb83SAl Viro 		   struct page ***pages, size_t maxsize,
1725d879cb83SAl Viro 		   size_t *start)
1726d879cb83SAl Viro {
1727d879cb83SAl Viro 	struct page **p;
17283d671ca6SAl Viro 	size_t len;
17293d671ca6SAl Viro 	int n, res;
1730d879cb83SAl Viro 
1731d879cb83SAl Viro 	if (maxsize > i->count)
1732d879cb83SAl Viro 		maxsize = i->count;
17333d671ca6SAl Viro 	if (!maxsize)
17343d671ca6SAl Viro 		return 0;
1735d879cb83SAl Viro 
17363d671ca6SAl Viro 	if (likely(iter_is_iovec(i))) {
17373337ab08SAndreas Gruenbacher 		unsigned int gup_flags = 0;
17383d671ca6SAl Viro 		unsigned long addr;
17399ea9ce04SDavid Howells 
17403337ab08SAndreas Gruenbacher 		if (iov_iter_rw(i) != WRITE)
17413337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_WRITE;
17423337ab08SAndreas Gruenbacher 		if (i->nofault)
17433337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_NOFAULT;
17443337ab08SAndreas Gruenbacher 
17453d671ca6SAl Viro 		addr = first_iovec_segment(i, &len, start, maxsize, ~0U);
1746d879cb83SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
1747d879cb83SAl Viro 		p = get_pages_array(n);
1748d879cb83SAl Viro 		if (!p)
1749d879cb83SAl Viro 			return -ENOMEM;
17503337ab08SAndreas Gruenbacher 		res = get_user_pages_fast(addr, n, gup_flags, p);
1751814a6674SAndreas Gruenbacher 		if (unlikely(res <= 0)) {
1752d879cb83SAl Viro 			kvfree(p);
1753814a6674SAndreas Gruenbacher 			*pages = NULL;
1754d879cb83SAl Viro 			return res;
1755d879cb83SAl Viro 		}
1756d879cb83SAl Viro 		*pages = p;
1757d879cb83SAl Viro 		return (res == n ? len : res * PAGE_SIZE) - *start;
17583d671ca6SAl Viro 	}
17593d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
17603d671ca6SAl Viro 		struct page *page;
17613d671ca6SAl Viro 
17623d671ca6SAl Viro 		page = first_bvec_segment(i, &len, start, maxsize, ~0U);
17633d671ca6SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
17643d671ca6SAl Viro 		*pages = p = get_pages_array(n);
1765d879cb83SAl Viro 		if (!p)
1766d879cb83SAl Viro 			return -ENOMEM;
17673d671ca6SAl Viro 		while (n--)
17683d671ca6SAl Viro 			get_page(*p++ = page++);
17693d671ca6SAl Viro 		return len - *start;
17703d671ca6SAl Viro 	}
17713d671ca6SAl Viro 	if (iov_iter_is_pipe(i))
17723d671ca6SAl Viro 		return pipe_get_pages_alloc(i, pages, maxsize, start);
17733d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
17743d671ca6SAl Viro 		return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
1775d879cb83SAl Viro 	return -EFAULT;
1776d879cb83SAl Viro }
1777d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1778d879cb83SAl Viro 
1779d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1780d879cb83SAl Viro 			       struct iov_iter *i)
1781d879cb83SAl Viro {
1782d879cb83SAl Viro 	__wsum sum, next;
1783d879cb83SAl Viro 	sum = *csum;
17849ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1785241699cdSAl Viro 		WARN_ON(1);
1786241699cdSAl Viro 		return 0;
1787241699cdSAl Viro 	}
17887baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off, ({
17897baa5099SAl Viro 		next = csum_and_copy_from_user(base, addr + off, len);
1790d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
17917baa5099SAl Viro 		next ? 0 : len;
1792d879cb83SAl Viro 	}), ({
17937baa5099SAl Viro 		sum = csum_and_memcpy(addr + off, base, len, sum, off);
1794d879cb83SAl Viro 	})
1795d879cb83SAl Viro 	)
1796d879cb83SAl Viro 	*csum = sum;
1797d879cb83SAl Viro 	return bytes;
1798d879cb83SAl Viro }
1799d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter);
1800d879cb83SAl Viro 
180152cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1802d879cb83SAl Viro 			     struct iov_iter *i)
1803d879cb83SAl Viro {
180452cbd23aSWillem de Bruijn 	struct csum_state *csstate = _csstate;
1805d879cb83SAl Viro 	__wsum sum, next;
180678e1f386SAl Viro 
180778e1f386SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
1808241699cdSAl Viro 		WARN_ON(1);	/* for now */
1809241699cdSAl Viro 		return 0;
1810241699cdSAl Viro 	}
18116852df12SAl Viro 
18126852df12SAl Viro 	sum = csum_shift(csstate->csum, csstate->off);
18136852df12SAl Viro 	if (unlikely(iov_iter_is_pipe(i)))
18146852df12SAl Viro 		bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
18156852df12SAl Viro 	else iterate_and_advance(i, bytes, base, len, off, ({
18167baa5099SAl Viro 		next = csum_and_copy_to_user(addr + off, base, len);
1817d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
18187baa5099SAl Viro 		next ? 0 : len;
1819d879cb83SAl Viro 	}), ({
18207baa5099SAl Viro 		sum = csum_and_memcpy(base, addr + off, len, sum, off);
1821d879cb83SAl Viro 	})
1822d879cb83SAl Viro 	)
1823594e450bSAl Viro 	csstate->csum = csum_shift(sum, csstate->off);
1824594e450bSAl Viro 	csstate->off += bytes;
1825d879cb83SAl Viro 	return bytes;
1826d879cb83SAl Viro }
1827d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter);
1828d879cb83SAl Viro 
1829d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1830d05f4435SSagi Grimberg 		struct iov_iter *i)
1831d05f4435SSagi Grimberg {
18327999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH
1833d05f4435SSagi Grimberg 	struct ahash_request *hash = hashp;
1834d05f4435SSagi Grimberg 	struct scatterlist sg;
1835d05f4435SSagi Grimberg 	size_t copied;
1836d05f4435SSagi Grimberg 
1837d05f4435SSagi Grimberg 	copied = copy_to_iter(addr, bytes, i);
1838d05f4435SSagi Grimberg 	sg_init_one(&sg, addr, copied);
1839d05f4435SSagi Grimberg 	ahash_request_set_crypt(hash, &sg, NULL, copied);
1840d05f4435SSagi Grimberg 	crypto_ahash_update(hash);
1841d05f4435SSagi Grimberg 	return copied;
184227fad74aSYueHaibing #else
184327fad74aSYueHaibing 	return 0;
184427fad74aSYueHaibing #endif
1845d05f4435SSagi Grimberg }
1846d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter);
1847d05f4435SSagi Grimberg 
184866531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages)
1849d879cb83SAl Viro {
185066531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
185166531c65SAl Viro 	const struct iovec *p;
1852d879cb83SAl Viro 	int npages = 0;
1853d879cb83SAl Viro 
185466531c65SAl Viro 	for (p = i->iov; size; skip = 0, p++) {
185566531c65SAl Viro 		unsigned offs = offset_in_page(p->iov_base + skip);
185666531c65SAl Viro 		size_t len = min(p->iov_len - skip, size);
1857d879cb83SAl Viro 
185866531c65SAl Viro 		if (len) {
185966531c65SAl Viro 			size -= len;
186066531c65SAl Viro 			npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
186166531c65SAl Viro 			if (unlikely(npages > maxpages))
186266531c65SAl Viro 				return maxpages;
186366531c65SAl Viro 		}
186466531c65SAl Viro 	}
186566531c65SAl Viro 	return npages;
186666531c65SAl Viro }
186766531c65SAl Viro 
186866531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages)
186966531c65SAl Viro {
187066531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
187166531c65SAl Viro 	const struct bio_vec *p;
187266531c65SAl Viro 	int npages = 0;
187366531c65SAl Viro 
187466531c65SAl Viro 	for (p = i->bvec; size; skip = 0, p++) {
187566531c65SAl Viro 		unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
187666531c65SAl Viro 		size_t len = min(p->bv_len - skip, size);
187766531c65SAl Viro 
187866531c65SAl Viro 		size -= len;
187966531c65SAl Viro 		npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
188066531c65SAl Viro 		if (unlikely(npages > maxpages))
188166531c65SAl Viro 			return maxpages;
188266531c65SAl Viro 	}
188366531c65SAl Viro 	return npages;
188466531c65SAl Viro }
188566531c65SAl Viro 
188666531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages)
188766531c65SAl Viro {
188866531c65SAl Viro 	if (unlikely(!i->count))
188966531c65SAl Viro 		return 0;
189066531c65SAl Viro 	/* iovec and kvec have identical layouts */
189166531c65SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
189266531c65SAl Viro 		return iov_npages(i, maxpages);
189366531c65SAl Viro 	if (iov_iter_is_bvec(i))
189466531c65SAl Viro 		return bvec_npages(i, maxpages);
189566531c65SAl Viro 	if (iov_iter_is_pipe(i)) {
18968cefc107SDavid Howells 		unsigned int iter_head;
189766531c65SAl Viro 		int npages;
1898241699cdSAl Viro 		size_t off;
1899241699cdSAl Viro 
1900241699cdSAl Viro 		if (!sanity(i))
1901241699cdSAl Viro 			return 0;
1902241699cdSAl Viro 
19038cefc107SDavid Howells 		data_start(i, &iter_head, &off);
1904241699cdSAl Viro 		/* some of this one + all after this one */
190566531c65SAl Viro 		npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
190666531c65SAl Viro 		return min(npages, maxpages);
190766531c65SAl Viro 	}
190866531c65SAl Viro 	if (iov_iter_is_xarray(i)) {
1909e4f8df86SAl Viro 		unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1910e4f8df86SAl Viro 		int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
191166531c65SAl Viro 		return min(npages, maxpages);
191266531c65SAl Viro 	}
191366531c65SAl Viro 	return 0;
1914d879cb83SAl Viro }
1915d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages);
1916d879cb83SAl Viro 
1917d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1918d879cb83SAl Viro {
1919d879cb83SAl Viro 	*new = *old;
192000e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(new))) {
1921241699cdSAl Viro 		WARN_ON(1);
1922241699cdSAl Viro 		return NULL;
1923241699cdSAl Viro 	}
19247ff50620SDavid Howells 	if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
19259ea9ce04SDavid Howells 		return NULL;
192600e23707SDavid Howells 	if (iov_iter_is_bvec(new))
1927d879cb83SAl Viro 		return new->bvec = kmemdup(new->bvec,
1928d879cb83SAl Viro 				    new->nr_segs * sizeof(struct bio_vec),
1929d879cb83SAl Viro 				    flags);
1930d879cb83SAl Viro 	else
1931d879cb83SAl Viro 		/* iovec and kvec have identical layout */
1932d879cb83SAl Viro 		return new->iov = kmemdup(new->iov,
1933d879cb83SAl Viro 				   new->nr_segs * sizeof(struct iovec),
1934d879cb83SAl Viro 				   flags);
1935d879cb83SAl Viro }
1936d879cb83SAl Viro EXPORT_SYMBOL(dup_iter);
1937bc917be8SAl Viro 
1938bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov,
1939bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1940bfdc5970SChristoph Hellwig {
1941bfdc5970SChristoph Hellwig 	const struct compat_iovec __user *uiov =
1942bfdc5970SChristoph Hellwig 		(const struct compat_iovec __user *)uvec;
1943bfdc5970SChristoph Hellwig 	int ret = -EFAULT, i;
1944bfdc5970SChristoph Hellwig 
1945a959a978SChristoph Hellwig 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1946bfdc5970SChristoph Hellwig 		return -EFAULT;
1947bfdc5970SChristoph Hellwig 
1948bfdc5970SChristoph Hellwig 	for (i = 0; i < nr_segs; i++) {
1949bfdc5970SChristoph Hellwig 		compat_uptr_t buf;
1950bfdc5970SChristoph Hellwig 		compat_ssize_t len;
1951bfdc5970SChristoph Hellwig 
1952bfdc5970SChristoph Hellwig 		unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1953bfdc5970SChristoph Hellwig 		unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1954bfdc5970SChristoph Hellwig 
1955bfdc5970SChristoph Hellwig 		/* check for compat_size_t not fitting in compat_ssize_t .. */
1956bfdc5970SChristoph Hellwig 		if (len < 0) {
1957bfdc5970SChristoph Hellwig 			ret = -EINVAL;
1958bfdc5970SChristoph Hellwig 			goto uaccess_end;
1959bfdc5970SChristoph Hellwig 		}
1960bfdc5970SChristoph Hellwig 		iov[i].iov_base = compat_ptr(buf);
1961bfdc5970SChristoph Hellwig 		iov[i].iov_len = len;
1962bfdc5970SChristoph Hellwig 	}
1963bfdc5970SChristoph Hellwig 
1964bfdc5970SChristoph Hellwig 	ret = 0;
1965bfdc5970SChristoph Hellwig uaccess_end:
1966bfdc5970SChristoph Hellwig 	user_access_end();
1967bfdc5970SChristoph Hellwig 	return ret;
1968bfdc5970SChristoph Hellwig }
1969bfdc5970SChristoph Hellwig 
1970bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov,
1971bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1972fb041b59SDavid Laight {
1973fb041b59SDavid Laight 	unsigned long seg;
1974bfdc5970SChristoph Hellwig 
1975bfdc5970SChristoph Hellwig 	if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1976bfdc5970SChristoph Hellwig 		return -EFAULT;
1977bfdc5970SChristoph Hellwig 	for (seg = 0; seg < nr_segs; seg++) {
1978bfdc5970SChristoph Hellwig 		if ((ssize_t)iov[seg].iov_len < 0)
1979bfdc5970SChristoph Hellwig 			return -EINVAL;
1980bfdc5970SChristoph Hellwig 	}
1981bfdc5970SChristoph Hellwig 
1982bfdc5970SChristoph Hellwig 	return 0;
1983bfdc5970SChristoph Hellwig }
1984bfdc5970SChristoph Hellwig 
1985bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec,
1986bfdc5970SChristoph Hellwig 		unsigned long nr_segs, unsigned long fast_segs,
1987bfdc5970SChristoph Hellwig 		struct iovec *fast_iov, bool compat)
1988bfdc5970SChristoph Hellwig {
1989bfdc5970SChristoph Hellwig 	struct iovec *iov = fast_iov;
1990bfdc5970SChristoph Hellwig 	int ret;
1991fb041b59SDavid Laight 
1992fb041b59SDavid Laight 	/*
1993bfdc5970SChristoph Hellwig 	 * SuS says "The readv() function *may* fail if the iovcnt argument was
1994bfdc5970SChristoph Hellwig 	 * less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1995fb041b59SDavid Laight 	 * traditionally returned zero for zero segments, so...
1996fb041b59SDavid Laight 	 */
1997bfdc5970SChristoph Hellwig 	if (nr_segs == 0)
1998bfdc5970SChristoph Hellwig 		return iov;
1999bfdc5970SChristoph Hellwig 	if (nr_segs > UIO_MAXIOV)
2000bfdc5970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
2001fb041b59SDavid Laight 	if (nr_segs > fast_segs) {
2002fb041b59SDavid Laight 		iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
2003bfdc5970SChristoph Hellwig 		if (!iov)
2004bfdc5970SChristoph Hellwig 			return ERR_PTR(-ENOMEM);
2005fb041b59SDavid Laight 	}
2006bfdc5970SChristoph Hellwig 
2007bfdc5970SChristoph Hellwig 	if (compat)
2008bfdc5970SChristoph Hellwig 		ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
2009bfdc5970SChristoph Hellwig 	else
2010bfdc5970SChristoph Hellwig 		ret = copy_iovec_from_user(iov, uvec, nr_segs);
2011bfdc5970SChristoph Hellwig 	if (ret) {
2012bfdc5970SChristoph Hellwig 		if (iov != fast_iov)
2013bfdc5970SChristoph Hellwig 			kfree(iov);
2014bfdc5970SChristoph Hellwig 		return ERR_PTR(ret);
2015fb041b59SDavid Laight 	}
2016bfdc5970SChristoph Hellwig 
2017bfdc5970SChristoph Hellwig 	return iov;
2018bfdc5970SChristoph Hellwig }
2019bfdc5970SChristoph Hellwig 
2020bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec,
2021bfdc5970SChristoph Hellwig 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
2022bfdc5970SChristoph Hellwig 		 struct iov_iter *i, bool compat)
2023bfdc5970SChristoph Hellwig {
2024bfdc5970SChristoph Hellwig 	ssize_t total_len = 0;
2025bfdc5970SChristoph Hellwig 	unsigned long seg;
2026bfdc5970SChristoph Hellwig 	struct iovec *iov;
2027bfdc5970SChristoph Hellwig 
2028bfdc5970SChristoph Hellwig 	iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
2029bfdc5970SChristoph Hellwig 	if (IS_ERR(iov)) {
2030bfdc5970SChristoph Hellwig 		*iovp = NULL;
2031bfdc5970SChristoph Hellwig 		return PTR_ERR(iov);
2032fb041b59SDavid Laight 	}
2033fb041b59SDavid Laight 
2034fb041b59SDavid Laight 	/*
2035bfdc5970SChristoph Hellwig 	 * According to the Single Unix Specification we should return EINVAL if
2036bfdc5970SChristoph Hellwig 	 * an element length is < 0 when cast to ssize_t or if the total length
2037bfdc5970SChristoph Hellwig 	 * would overflow the ssize_t return value of the system call.
2038fb041b59SDavid Laight 	 *
2039fb041b59SDavid Laight 	 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
2040fb041b59SDavid Laight 	 * overflow case.
2041fb041b59SDavid Laight 	 */
2042fb041b59SDavid Laight 	for (seg = 0; seg < nr_segs; seg++) {
2043fb041b59SDavid Laight 		ssize_t len = (ssize_t)iov[seg].iov_len;
2044fb041b59SDavid Laight 
2045bfdc5970SChristoph Hellwig 		if (!access_ok(iov[seg].iov_base, len)) {
2046bfdc5970SChristoph Hellwig 			if (iov != *iovp)
2047bfdc5970SChristoph Hellwig 				kfree(iov);
2048bfdc5970SChristoph Hellwig 			*iovp = NULL;
2049bfdc5970SChristoph Hellwig 			return -EFAULT;
2050fb041b59SDavid Laight 		}
2051bfdc5970SChristoph Hellwig 
2052bfdc5970SChristoph Hellwig 		if (len > MAX_RW_COUNT - total_len) {
2053bfdc5970SChristoph Hellwig 			len = MAX_RW_COUNT - total_len;
2054fb041b59SDavid Laight 			iov[seg].iov_len = len;
2055fb041b59SDavid Laight 		}
2056bfdc5970SChristoph Hellwig 		total_len += len;
2057fb041b59SDavid Laight 	}
2058bfdc5970SChristoph Hellwig 
2059bfdc5970SChristoph Hellwig 	iov_iter_init(i, type, iov, nr_segs, total_len);
2060bfdc5970SChristoph Hellwig 	if (iov == *iovp)
2061bfdc5970SChristoph Hellwig 		*iovp = NULL;
2062bfdc5970SChristoph Hellwig 	else
2063bfdc5970SChristoph Hellwig 		*iovp = iov;
2064bfdc5970SChristoph Hellwig 	return total_len;
2065fb041b59SDavid Laight }
2066fb041b59SDavid Laight 
2067ffecee4fSVegard Nossum /**
2068ffecee4fSVegard Nossum  * import_iovec() - Copy an array of &struct iovec from userspace
2069ffecee4fSVegard Nossum  *     into the kernel, check that it is valid, and initialize a new
2070ffecee4fSVegard Nossum  *     &struct iov_iter iterator to access it.
2071ffecee4fSVegard Nossum  *
2072ffecee4fSVegard Nossum  * @type: One of %READ or %WRITE.
2073bfdc5970SChristoph Hellwig  * @uvec: Pointer to the userspace array.
2074ffecee4fSVegard Nossum  * @nr_segs: Number of elements in userspace array.
2075ffecee4fSVegard Nossum  * @fast_segs: Number of elements in @iov.
2076bfdc5970SChristoph Hellwig  * @iovp: (input and output parameter) Pointer to pointer to (usually small
2077ffecee4fSVegard Nossum  *     on-stack) kernel array.
2078ffecee4fSVegard Nossum  * @i: Pointer to iterator that will be initialized on success.
2079ffecee4fSVegard Nossum  *
2080ffecee4fSVegard Nossum  * If the array pointed to by *@iov is large enough to hold all @nr_segs,
2081ffecee4fSVegard Nossum  * then this function places %NULL in *@iov on return. Otherwise, a new
2082ffecee4fSVegard Nossum  * array will be allocated and the result placed in *@iov. This means that
2083ffecee4fSVegard Nossum  * the caller may call kfree() on *@iov regardless of whether the small
2084ffecee4fSVegard Nossum  * on-stack array was used or not (and regardless of whether this function
2085ffecee4fSVegard Nossum  * returns an error or not).
2086ffecee4fSVegard Nossum  *
208787e5e6daSJens Axboe  * Return: Negative error code on error, bytes imported on success
2088ffecee4fSVegard Nossum  */
2089bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec,
2090bc917be8SAl Viro 		 unsigned nr_segs, unsigned fast_segs,
2091bfdc5970SChristoph Hellwig 		 struct iovec **iovp, struct iov_iter *i)
2092bc917be8SAl Viro {
209389cd35c5SChristoph Hellwig 	return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
209489cd35c5SChristoph Hellwig 			      in_compat_syscall());
2095bc917be8SAl Viro }
2096bc917be8SAl Viro EXPORT_SYMBOL(import_iovec);
2097bc917be8SAl Viro 
2098bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len,
2099bc917be8SAl Viro 		 struct iovec *iov, struct iov_iter *i)
2100bc917be8SAl Viro {
2101bc917be8SAl Viro 	if (len > MAX_RW_COUNT)
2102bc917be8SAl Viro 		len = MAX_RW_COUNT;
210396d4f267SLinus Torvalds 	if (unlikely(!access_ok(buf, len)))
2104bc917be8SAl Viro 		return -EFAULT;
2105bc917be8SAl Viro 
2106bc917be8SAl Viro 	iov->iov_base = buf;
2107bc917be8SAl Viro 	iov->iov_len = len;
2108bc917be8SAl Viro 	iov_iter_init(i, rw, iov, 1, len);
2109bc917be8SAl Viro 	return 0;
2110bc917be8SAl Viro }
2111e1267585SAl Viro EXPORT_SYMBOL(import_single_range);
21128fb0f47aSJens Axboe 
21138fb0f47aSJens Axboe /**
21148fb0f47aSJens Axboe  * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
21158fb0f47aSJens Axboe  *     iov_iter_save_state() was called.
21168fb0f47aSJens Axboe  *
21178fb0f47aSJens Axboe  * @i: &struct iov_iter to restore
21188fb0f47aSJens Axboe  * @state: state to restore from
21198fb0f47aSJens Axboe  *
21208fb0f47aSJens Axboe  * Used after iov_iter_save_state() to bring restore @i, if operations may
21218fb0f47aSJens Axboe  * have advanced it.
21228fb0f47aSJens Axboe  *
21238fb0f47aSJens Axboe  * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
21248fb0f47aSJens Axboe  */
21258fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
21268fb0f47aSJens Axboe {
21278fb0f47aSJens Axboe 	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
21288fb0f47aSJens Axboe 			 !iov_iter_is_kvec(i))
21298fb0f47aSJens Axboe 		return;
21308fb0f47aSJens Axboe 	i->iov_offset = state->iov_offset;
21318fb0f47aSJens Axboe 	i->count = state->count;
21328fb0f47aSJens Axboe 	/*
21338fb0f47aSJens Axboe 	 * For the *vec iters, nr_segs + iov is constant - if we increment
21348fb0f47aSJens Axboe 	 * the vec, then we also decrement the nr_segs count. Hence we don't
21358fb0f47aSJens Axboe 	 * need to track both of these, just one is enough and we can deduct
21368fb0f47aSJens Axboe 	 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
21378fb0f47aSJens Axboe 	 * size, so we can just increment the iov pointer as they are unionzed.
21388fb0f47aSJens Axboe 	 * ITER_BVEC _may_ be the same size on some archs, but on others it is
21398fb0f47aSJens Axboe 	 * not. Be safe and handle it separately.
21408fb0f47aSJens Axboe 	 */
21418fb0f47aSJens Axboe 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
21428fb0f47aSJens Axboe 	if (iov_iter_is_bvec(i))
21438fb0f47aSJens Axboe 		i->bvec -= state->nr_segs - i->nr_segs;
21448fb0f47aSJens Axboe 	else
21458fb0f47aSJens Axboe 		i->iov -= state->nr_segs - i->nr_segs;
21468fb0f47aSJens Axboe 	i->nr_segs = state->nr_segs;
21478fb0f47aSJens Axboe }
2148