xref: /openbmc/linux/lib/iov_iter.c (revision cdd591fc)
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;					\
727ff50620SDavid Howells 	struct page *head = NULL;				\
73a6e4ec7bSAl Viro 	loff_t start = i->xarray_start + i->iov_offset;		\
744b179e9aSAl Viro 	unsigned offset = start % PAGE_SIZE;			\
754b179e9aSAl Viro 	pgoff_t index = start / PAGE_SIZE;			\
767ff50620SDavid Howells 	int j;							\
777ff50620SDavid Howells 								\
787ff50620SDavid Howells 	XA_STATE(xas, i->xarray, index);			\
797ff50620SDavid Howells 								\
807ff50620SDavid Howells 	rcu_read_lock();					\
817ff50620SDavid Howells 	xas_for_each(&xas, head, ULONG_MAX) {			\
821b4fb5ffSAl Viro 		unsigned left;					\
837ff50620SDavid Howells 		if (xas_retry(&xas, head))			\
847ff50620SDavid Howells 			continue;				\
857ff50620SDavid Howells 		if (WARN_ON(xa_is_value(head)))			\
867ff50620SDavid Howells 			break;					\
877ff50620SDavid Howells 		if (WARN_ON(PageHuge(head)))			\
887ff50620SDavid Howells 			break;					\
897ff50620SDavid Howells 		for (j = (head->index < index) ? index - head->index : 0; \
907ff50620SDavid Howells 		     j < thp_nr_pages(head); j++) {		\
9121b56c84SAl Viro 			void *kaddr = kmap_local_page(head + j);	\
927baa5099SAl Viro 			base = kaddr + offset;			\
937baa5099SAl Viro 			len = PAGE_SIZE - offset;		\
947baa5099SAl Viro 			len = min(n, len);			\
951b4fb5ffSAl Viro 			left = (STEP);				\
9621b56c84SAl Viro 			kunmap_local(kaddr);			\
977baa5099SAl Viro 			len -= left;				\
987baa5099SAl Viro 			__off += len;				\
997baa5099SAl Viro 			n -= len;				\
1001b4fb5ffSAl Viro 			if (left || n == 0)			\
1011b4fb5ffSAl Viro 				goto __out;			\
1024b179e9aSAl Viro 			offset = 0;				\
1037ff50620SDavid Howells 		}						\
1047ff50620SDavid Howells 	}							\
1051b4fb5ffSAl Viro __out:								\
1067ff50620SDavid Howells 	rcu_read_unlock();					\
107a6e4ec7bSAl Viro 	i->iov_offset += __off;						\
108622838f3SAl Viro 	n = __off;						\
1097ff50620SDavid Howells }
1107ff50620SDavid Howells 
1117baa5099SAl Viro #define __iterate_and_advance(i, n, base, len, off, I, K) {	\
112dd254f5aSAl Viro 	if (unlikely(i->count < n))				\
113dd254f5aSAl Viro 		n = i->count;					\
114f5da8354SAl Viro 	if (likely(n)) {					\
11528f38db7SAl Viro 		if (likely(iter_is_iovec(i))) {			\
1165c67aa90SAl Viro 			const struct iovec *iov = i->iov;	\
1177baa5099SAl Viro 			void __user *base;			\
1187baa5099SAl Viro 			size_t len;				\
1197baa5099SAl Viro 			iterate_iovec(i, n, base, len, off,	\
120a6e4ec7bSAl Viro 						iov, (I))	\
121d879cb83SAl Viro 			i->nr_segs -= iov - i->iov;		\
122d879cb83SAl Viro 			i->iov = iov;				\
12328f38db7SAl Viro 		} else if (iov_iter_is_bvec(i)) {		\
12428f38db7SAl Viro 			const struct bio_vec *bvec = i->bvec;	\
1257baa5099SAl Viro 			void *base;				\
1267baa5099SAl Viro 			size_t len;				\
1277baa5099SAl Viro 			iterate_bvec(i, n, base, len, off,	\
128a6e4ec7bSAl Viro 						bvec, (K))	\
1297491a2bfSAl Viro 			i->nr_segs -= bvec - i->bvec;		\
1307491a2bfSAl Viro 			i->bvec = bvec;				\
13128f38db7SAl Viro 		} else if (iov_iter_is_kvec(i)) {		\
1325c67aa90SAl Viro 			const struct kvec *kvec = i->kvec;	\
1337baa5099SAl Viro 			void *base;				\
1347baa5099SAl Viro 			size_t len;				\
1357baa5099SAl Viro 			iterate_iovec(i, n, base, len, off,	\
136a6e4ec7bSAl Viro 						kvec, (K))	\
13728f38db7SAl Viro 			i->nr_segs -= kvec - i->kvec;		\
13828f38db7SAl Viro 			i->kvec = kvec;				\
13928f38db7SAl Viro 		} else if (iov_iter_is_xarray(i)) {		\
1407baa5099SAl Viro 			void *base;				\
1417baa5099SAl Viro 			size_t len;				\
1427baa5099SAl Viro 			iterate_xarray(i, n, base, len, off,	\
143a6e4ec7bSAl Viro 							(K))	\
144d879cb83SAl Viro 		}						\
145d879cb83SAl Viro 		i->count -= n;					\
146dd254f5aSAl Viro 	}							\
147d879cb83SAl Viro }
1487baa5099SAl Viro #define iterate_and_advance(i, n, base, len, off, I, K) \
1497baa5099SAl Viro 	__iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
150d879cb83SAl Viro 
15109fc68dcSAl Viro static int copyout(void __user *to, const void *from, size_t n)
15209fc68dcSAl Viro {
1534d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1544d0e9df5SAlbert van der Linde 		return n;
15596d4f267SLinus Torvalds 	if (access_ok(to, n)) {
156d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
15709fc68dcSAl Viro 		n = raw_copy_to_user(to, from, n);
15809fc68dcSAl Viro 	}
15909fc68dcSAl Viro 	return n;
16009fc68dcSAl Viro }
16109fc68dcSAl Viro 
16209fc68dcSAl Viro static int copyin(void *to, const void __user *from, size_t n)
16309fc68dcSAl Viro {
1644d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1654d0e9df5SAlbert van der Linde 		return n;
16696d4f267SLinus Torvalds 	if (access_ok(from, n)) {
167d0ef4c36SMarco Elver 		instrument_copy_from_user(to, from, n);
16809fc68dcSAl Viro 		n = raw_copy_from_user(to, from, n);
16909fc68dcSAl Viro 	}
17009fc68dcSAl Viro 	return n;
17109fc68dcSAl Viro }
17209fc68dcSAl Viro 
173d879cb83SAl Viro static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
174d879cb83SAl Viro 			 struct iov_iter *i)
175d879cb83SAl Viro {
176d879cb83SAl Viro 	size_t skip, copy, left, wanted;
177d879cb83SAl Viro 	const struct iovec *iov;
178d879cb83SAl Viro 	char __user *buf;
179d879cb83SAl Viro 	void *kaddr, *from;
180d879cb83SAl Viro 
181d879cb83SAl Viro 	if (unlikely(bytes > i->count))
182d879cb83SAl Viro 		bytes = i->count;
183d879cb83SAl Viro 
184d879cb83SAl Viro 	if (unlikely(!bytes))
185d879cb83SAl Viro 		return 0;
186d879cb83SAl Viro 
18709fc68dcSAl Viro 	might_fault();
188d879cb83SAl Viro 	wanted = bytes;
189d879cb83SAl Viro 	iov = i->iov;
190d879cb83SAl Viro 	skip = i->iov_offset;
191d879cb83SAl Viro 	buf = iov->iov_base + skip;
192d879cb83SAl Viro 	copy = min(bytes, iov->iov_len - skip);
193d879cb83SAl Viro 
194bb523b40SAndreas Gruenbacher 	if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_writeable(buf, copy)) {
195d879cb83SAl Viro 		kaddr = kmap_atomic(page);
196d879cb83SAl Viro 		from = kaddr + offset;
197d879cb83SAl Viro 
198d879cb83SAl Viro 		/* first chunk, usually the only one */
19909fc68dcSAl Viro 		left = copyout(buf, from, copy);
200d879cb83SAl Viro 		copy -= left;
201d879cb83SAl Viro 		skip += copy;
202d879cb83SAl Viro 		from += copy;
203d879cb83SAl Viro 		bytes -= copy;
204d879cb83SAl Viro 
205d879cb83SAl Viro 		while (unlikely(!left && bytes)) {
206d879cb83SAl Viro 			iov++;
207d879cb83SAl Viro 			buf = iov->iov_base;
208d879cb83SAl Viro 			copy = min(bytes, iov->iov_len);
20909fc68dcSAl Viro 			left = copyout(buf, from, copy);
210d879cb83SAl Viro 			copy -= left;
211d879cb83SAl Viro 			skip = copy;
212d879cb83SAl Viro 			from += copy;
213d879cb83SAl Viro 			bytes -= copy;
214d879cb83SAl Viro 		}
215d879cb83SAl Viro 		if (likely(!bytes)) {
216d879cb83SAl Viro 			kunmap_atomic(kaddr);
217d879cb83SAl Viro 			goto done;
218d879cb83SAl Viro 		}
219d879cb83SAl Viro 		offset = from - kaddr;
220d879cb83SAl Viro 		buf += copy;
221d879cb83SAl Viro 		kunmap_atomic(kaddr);
222d879cb83SAl Viro 		copy = min(bytes, iov->iov_len - skip);
223d879cb83SAl Viro 	}
224d879cb83SAl Viro 	/* Too bad - revert to non-atomic kmap */
2253fa6c507SMikulas Patocka 
226d879cb83SAl Viro 	kaddr = kmap(page);
227d879cb83SAl Viro 	from = kaddr + offset;
22809fc68dcSAl Viro 	left = copyout(buf, from, copy);
229d879cb83SAl Viro 	copy -= left;
230d879cb83SAl Viro 	skip += copy;
231d879cb83SAl Viro 	from += copy;
232d879cb83SAl Viro 	bytes -= copy;
233d879cb83SAl Viro 	while (unlikely(!left && bytes)) {
234d879cb83SAl Viro 		iov++;
235d879cb83SAl Viro 		buf = iov->iov_base;
236d879cb83SAl Viro 		copy = min(bytes, iov->iov_len);
23709fc68dcSAl Viro 		left = copyout(buf, from, copy);
238d879cb83SAl Viro 		copy -= left;
239d879cb83SAl Viro 		skip = copy;
240d879cb83SAl Viro 		from += copy;
241d879cb83SAl Viro 		bytes -= copy;
242d879cb83SAl Viro 	}
243d879cb83SAl Viro 	kunmap(page);
2443fa6c507SMikulas Patocka 
245d879cb83SAl Viro done:
246d879cb83SAl Viro 	if (skip == iov->iov_len) {
247d879cb83SAl Viro 		iov++;
248d879cb83SAl Viro 		skip = 0;
249d879cb83SAl Viro 	}
250d879cb83SAl Viro 	i->count -= wanted - bytes;
251d879cb83SAl Viro 	i->nr_segs -= iov - i->iov;
252d879cb83SAl Viro 	i->iov = iov;
253d879cb83SAl Viro 	i->iov_offset = skip;
254d879cb83SAl Viro 	return wanted - bytes;
255d879cb83SAl Viro }
256d879cb83SAl Viro 
257d879cb83SAl Viro static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
258d879cb83SAl Viro 			 struct iov_iter *i)
259d879cb83SAl Viro {
260d879cb83SAl Viro 	size_t skip, copy, left, wanted;
261d879cb83SAl Viro 	const struct iovec *iov;
262d879cb83SAl Viro 	char __user *buf;
263d879cb83SAl Viro 	void *kaddr, *to;
264d879cb83SAl Viro 
265d879cb83SAl Viro 	if (unlikely(bytes > i->count))
266d879cb83SAl Viro 		bytes = i->count;
267d879cb83SAl Viro 
268d879cb83SAl Viro 	if (unlikely(!bytes))
269d879cb83SAl Viro 		return 0;
270d879cb83SAl Viro 
27109fc68dcSAl Viro 	might_fault();
272d879cb83SAl Viro 	wanted = bytes;
273d879cb83SAl Viro 	iov = i->iov;
274d879cb83SAl Viro 	skip = i->iov_offset;
275d879cb83SAl Viro 	buf = iov->iov_base + skip;
276d879cb83SAl Viro 	copy = min(bytes, iov->iov_len - skip);
277d879cb83SAl Viro 
278bb523b40SAndreas Gruenbacher 	if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_readable(buf, copy)) {
279d879cb83SAl Viro 		kaddr = kmap_atomic(page);
280d879cb83SAl Viro 		to = kaddr + offset;
281d879cb83SAl Viro 
282d879cb83SAl Viro 		/* first chunk, usually the only one */
28309fc68dcSAl Viro 		left = copyin(to, buf, copy);
284d879cb83SAl Viro 		copy -= left;
285d879cb83SAl Viro 		skip += copy;
286d879cb83SAl Viro 		to += copy;
287d879cb83SAl Viro 		bytes -= copy;
288d879cb83SAl Viro 
289d879cb83SAl Viro 		while (unlikely(!left && bytes)) {
290d879cb83SAl Viro 			iov++;
291d879cb83SAl Viro 			buf = iov->iov_base;
292d879cb83SAl Viro 			copy = min(bytes, iov->iov_len);
29309fc68dcSAl Viro 			left = copyin(to, buf, copy);
294d879cb83SAl Viro 			copy -= left;
295d879cb83SAl Viro 			skip = copy;
296d879cb83SAl Viro 			to += copy;
297d879cb83SAl Viro 			bytes -= copy;
298d879cb83SAl Viro 		}
299d879cb83SAl Viro 		if (likely(!bytes)) {
300d879cb83SAl Viro 			kunmap_atomic(kaddr);
301d879cb83SAl Viro 			goto done;
302d879cb83SAl Viro 		}
303d879cb83SAl Viro 		offset = to - kaddr;
304d879cb83SAl Viro 		buf += copy;
305d879cb83SAl Viro 		kunmap_atomic(kaddr);
306d879cb83SAl Viro 		copy = min(bytes, iov->iov_len - skip);
307d879cb83SAl Viro 	}
308d879cb83SAl Viro 	/* Too bad - revert to non-atomic kmap */
3093fa6c507SMikulas Patocka 
310d879cb83SAl Viro 	kaddr = kmap(page);
311d879cb83SAl Viro 	to = kaddr + offset;
31209fc68dcSAl Viro 	left = copyin(to, buf, copy);
313d879cb83SAl Viro 	copy -= left;
314d879cb83SAl Viro 	skip += copy;
315d879cb83SAl Viro 	to += copy;
316d879cb83SAl Viro 	bytes -= copy;
317d879cb83SAl Viro 	while (unlikely(!left && bytes)) {
318d879cb83SAl Viro 		iov++;
319d879cb83SAl Viro 		buf = iov->iov_base;
320d879cb83SAl Viro 		copy = min(bytes, iov->iov_len);
32109fc68dcSAl Viro 		left = copyin(to, buf, copy);
322d879cb83SAl Viro 		copy -= left;
323d879cb83SAl Viro 		skip = copy;
324d879cb83SAl Viro 		to += copy;
325d879cb83SAl Viro 		bytes -= copy;
326d879cb83SAl Viro 	}
327d879cb83SAl Viro 	kunmap(page);
3283fa6c507SMikulas Patocka 
329d879cb83SAl Viro done:
330d879cb83SAl Viro 	if (skip == iov->iov_len) {
331d879cb83SAl Viro 		iov++;
332d879cb83SAl Viro 		skip = 0;
333d879cb83SAl Viro 	}
334d879cb83SAl Viro 	i->count -= wanted - bytes;
335d879cb83SAl Viro 	i->nr_segs -= iov - i->iov;
336d879cb83SAl Viro 	i->iov = iov;
337d879cb83SAl Viro 	i->iov_offset = skip;
338d879cb83SAl Viro 	return wanted - bytes;
339d879cb83SAl Viro }
340d879cb83SAl Viro 
341241699cdSAl Viro #ifdef PIPE_PARANOIA
342241699cdSAl Viro static bool sanity(const struct iov_iter *i)
343241699cdSAl Viro {
344241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
3458cefc107SDavid Howells 	unsigned int p_head = pipe->head;
3468cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
3478cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
3488cefc107SDavid Howells 	unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
3498cefc107SDavid Howells 	unsigned int i_head = i->head;
3508cefc107SDavid Howells 	unsigned int idx;
3518cefc107SDavid Howells 
352241699cdSAl Viro 	if (i->iov_offset) {
353241699cdSAl Viro 		struct pipe_buffer *p;
3548cefc107SDavid Howells 		if (unlikely(p_occupancy == 0))
355241699cdSAl Viro 			goto Bad;	// pipe must be non-empty
3568cefc107SDavid Howells 		if (unlikely(i_head != p_head - 1))
357241699cdSAl Viro 			goto Bad;	// must be at the last buffer...
358241699cdSAl Viro 
3598cefc107SDavid Howells 		p = &pipe->bufs[i_head & p_mask];
360241699cdSAl Viro 		if (unlikely(p->offset + p->len != i->iov_offset))
361241699cdSAl Viro 			goto Bad;	// ... at the end of segment
362241699cdSAl Viro 	} else {
3638cefc107SDavid Howells 		if (i_head != p_head)
364241699cdSAl Viro 			goto Bad;	// must be right after the last buffer
365241699cdSAl Viro 	}
366241699cdSAl Viro 	return true;
367241699cdSAl Viro Bad:
3688cefc107SDavid Howells 	printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
3698cefc107SDavid Howells 	printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
3708cefc107SDavid Howells 			p_head, p_tail, pipe->ring_size);
3718cefc107SDavid Howells 	for (idx = 0; idx < pipe->ring_size; idx++)
372241699cdSAl Viro 		printk(KERN_ERR "[%p %p %d %d]\n",
373241699cdSAl Viro 			pipe->bufs[idx].ops,
374241699cdSAl Viro 			pipe->bufs[idx].page,
375241699cdSAl Viro 			pipe->bufs[idx].offset,
376241699cdSAl Viro 			pipe->bufs[idx].len);
377241699cdSAl Viro 	WARN_ON(1);
378241699cdSAl Viro 	return false;
379241699cdSAl Viro }
380241699cdSAl Viro #else
381241699cdSAl Viro #define sanity(i) true
382241699cdSAl Viro #endif
383241699cdSAl Viro 
384241699cdSAl Viro static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
385241699cdSAl Viro 			 struct iov_iter *i)
386241699cdSAl Viro {
387241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
388241699cdSAl Viro 	struct pipe_buffer *buf;
3898cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
3908cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
3918cefc107SDavid Howells 	unsigned int i_head = i->head;
392241699cdSAl Viro 	size_t off;
393241699cdSAl Viro 
394241699cdSAl Viro 	if (unlikely(bytes > i->count))
395241699cdSAl Viro 		bytes = i->count;
396241699cdSAl Viro 
397241699cdSAl Viro 	if (unlikely(!bytes))
398241699cdSAl Viro 		return 0;
399241699cdSAl Viro 
400241699cdSAl Viro 	if (!sanity(i))
401241699cdSAl Viro 		return 0;
402241699cdSAl Viro 
403241699cdSAl Viro 	off = i->iov_offset;
4048cefc107SDavid Howells 	buf = &pipe->bufs[i_head & p_mask];
405241699cdSAl Viro 	if (off) {
406241699cdSAl Viro 		if (offset == off && buf->page == page) {
407241699cdSAl Viro 			/* merge with the last one */
408241699cdSAl Viro 			buf->len += bytes;
409241699cdSAl Viro 			i->iov_offset += bytes;
410241699cdSAl Viro 			goto out;
411241699cdSAl Viro 		}
4128cefc107SDavid Howells 		i_head++;
4138cefc107SDavid Howells 		buf = &pipe->bufs[i_head & p_mask];
414241699cdSAl Viro 	}
4156718b6f8SDavid Howells 	if (pipe_full(i_head, p_tail, pipe->max_usage))
416241699cdSAl Viro 		return 0;
4178cefc107SDavid Howells 
418241699cdSAl Viro 	buf->ops = &page_cache_pipe_buf_ops;
4198cefc107SDavid Howells 	get_page(page);
4208cefc107SDavid Howells 	buf->page = page;
421241699cdSAl Viro 	buf->offset = offset;
422241699cdSAl Viro 	buf->len = bytes;
4238cefc107SDavid Howells 
4248cefc107SDavid Howells 	pipe->head = i_head + 1;
425241699cdSAl Viro 	i->iov_offset = offset + bytes;
4268cefc107SDavid Howells 	i->head = i_head;
427241699cdSAl Viro out:
428241699cdSAl Viro 	i->count -= bytes;
429241699cdSAl Viro 	return bytes;
430241699cdSAl Viro }
431241699cdSAl Viro 
432d879cb83SAl Viro /*
433a6294593SAndreas Gruenbacher  * fault_in_iov_iter_readable - fault in iov iterator for reading
434a6294593SAndreas Gruenbacher  * @i: iterator
435a6294593SAndreas Gruenbacher  * @size: maximum length
436171a0203SAnton Altaparmakov  *
437a6294593SAndreas Gruenbacher  * Fault in one or more iovecs of the given iov_iter, to a maximum length of
438a6294593SAndreas Gruenbacher  * @size.  For each iovec, fault in each page that constitutes the iovec.
439a6294593SAndreas Gruenbacher  *
440a6294593SAndreas Gruenbacher  * Returns the number of bytes not faulted in (like copy_to_user() and
441a6294593SAndreas Gruenbacher  * copy_from_user()).
442a6294593SAndreas Gruenbacher  *
443a6294593SAndreas Gruenbacher  * Always returns 0 for non-userspace iterators.
444171a0203SAnton Altaparmakov  */
445a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
446171a0203SAnton Altaparmakov {
4470e8f0d67SAl Viro 	if (iter_is_iovec(i)) {
448a6294593SAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
4498409a0d2SAl Viro 		const struct iovec *p;
4508409a0d2SAl Viro 		size_t skip;
4518409a0d2SAl Viro 
452a6294593SAndreas Gruenbacher 		size -= count;
453a6294593SAndreas Gruenbacher 		for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
454a6294593SAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
455a6294593SAndreas Gruenbacher 			size_t ret;
4568409a0d2SAl Viro 
4578409a0d2SAl Viro 			if (unlikely(!len))
4588409a0d2SAl Viro 				continue;
459a6294593SAndreas Gruenbacher 			ret = fault_in_readable(p->iov_base + skip, len);
460a6294593SAndreas Gruenbacher 			count -= len - ret;
461a6294593SAndreas Gruenbacher 			if (ret)
462a6294593SAndreas Gruenbacher 				break;
4638409a0d2SAl Viro 		}
464a6294593SAndreas Gruenbacher 		return count + size;
465171a0203SAnton Altaparmakov 	}
466171a0203SAnton Altaparmakov 	return 0;
467171a0203SAnton Altaparmakov }
468a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable);
469171a0203SAnton Altaparmakov 
470*cdd591fcSAndreas Gruenbacher /*
471*cdd591fcSAndreas Gruenbacher  * fault_in_iov_iter_writeable - fault in iov iterator for writing
472*cdd591fcSAndreas Gruenbacher  * @i: iterator
473*cdd591fcSAndreas Gruenbacher  * @size: maximum length
474*cdd591fcSAndreas Gruenbacher  *
475*cdd591fcSAndreas Gruenbacher  * Faults in the iterator using get_user_pages(), i.e., without triggering
476*cdd591fcSAndreas Gruenbacher  * hardware page faults.  This is primarily useful when we already know that
477*cdd591fcSAndreas Gruenbacher  * some or all of the pages in @i aren't in memory.
478*cdd591fcSAndreas Gruenbacher  *
479*cdd591fcSAndreas Gruenbacher  * Returns the number of bytes not faulted in, like copy_to_user() and
480*cdd591fcSAndreas Gruenbacher  * copy_from_user().
481*cdd591fcSAndreas Gruenbacher  *
482*cdd591fcSAndreas Gruenbacher  * Always returns 0 for non-user-space iterators.
483*cdd591fcSAndreas Gruenbacher  */
484*cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
485*cdd591fcSAndreas Gruenbacher {
486*cdd591fcSAndreas Gruenbacher 	if (iter_is_iovec(i)) {
487*cdd591fcSAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
488*cdd591fcSAndreas Gruenbacher 		const struct iovec *p;
489*cdd591fcSAndreas Gruenbacher 		size_t skip;
490*cdd591fcSAndreas Gruenbacher 
491*cdd591fcSAndreas Gruenbacher 		size -= count;
492*cdd591fcSAndreas Gruenbacher 		for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
493*cdd591fcSAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
494*cdd591fcSAndreas Gruenbacher 			size_t ret;
495*cdd591fcSAndreas Gruenbacher 
496*cdd591fcSAndreas Gruenbacher 			if (unlikely(!len))
497*cdd591fcSAndreas Gruenbacher 				continue;
498*cdd591fcSAndreas Gruenbacher 			ret = fault_in_safe_writeable(p->iov_base + skip, len);
499*cdd591fcSAndreas Gruenbacher 			count -= len - ret;
500*cdd591fcSAndreas Gruenbacher 			if (ret)
501*cdd591fcSAndreas Gruenbacher 				break;
502*cdd591fcSAndreas Gruenbacher 		}
503*cdd591fcSAndreas Gruenbacher 		return count + size;
504*cdd591fcSAndreas Gruenbacher 	}
505*cdd591fcSAndreas Gruenbacher 	return 0;
506*cdd591fcSAndreas Gruenbacher }
507*cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable);
508*cdd591fcSAndreas Gruenbacher 
509aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction,
510d879cb83SAl Viro 			const struct iovec *iov, unsigned long nr_segs,
511d879cb83SAl Viro 			size_t count)
512d879cb83SAl Viro {
513aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
5148cd54c1cSAl Viro 	*i = (struct iov_iter) {
5158cd54c1cSAl Viro 		.iter_type = ITER_IOVEC,
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;
5818cefc107SDavid Howells 		buf->page = page;
5828cefc107SDavid Howells 		buf->offset = 0;
5838cefc107SDavid Howells 		buf->len = min_t(ssize_t, left, PAGE_SIZE);
5848cefc107SDavid Howells 		left -= buf->len;
5858cefc107SDavid Howells 		iter_head++;
5868cefc107SDavid Howells 		pipe->head = iter_head;
5878cefc107SDavid Howells 
5888cefc107SDavid Howells 		if (left == 0)
589241699cdSAl Viro 			return size;
590241699cdSAl Viro 	}
591241699cdSAl Viro 	return size - left;
592241699cdSAl Viro }
593241699cdSAl Viro 
594241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
595241699cdSAl Viro 				struct iov_iter *i)
596241699cdSAl Viro {
597241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
5988cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
5998cefc107SDavid Howells 	unsigned int i_head;
600241699cdSAl Viro 	size_t n, off;
601241699cdSAl Viro 
602241699cdSAl Viro 	if (!sanity(i))
603241699cdSAl Viro 		return 0;
604241699cdSAl Viro 
6058cefc107SDavid Howells 	bytes = n = push_pipe(i, bytes, &i_head, &off);
606241699cdSAl Viro 	if (unlikely(!n))
607241699cdSAl Viro 		return 0;
6088cefc107SDavid Howells 	do {
609241699cdSAl Viro 		size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
6108cefc107SDavid Howells 		memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
6118cefc107SDavid Howells 		i->head = i_head;
612241699cdSAl Viro 		i->iov_offset = off + chunk;
613241699cdSAl Viro 		n -= chunk;
614241699cdSAl Viro 		addr += chunk;
6158cefc107SDavid Howells 		off = 0;
6168cefc107SDavid Howells 		i_head++;
6178cefc107SDavid Howells 	} while (n);
618241699cdSAl Viro 	i->count -= bytes;
619241699cdSAl Viro 	return bytes;
620241699cdSAl Viro }
621241699cdSAl Viro 
622f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
623f9152895SAl Viro 			      __wsum sum, size_t off)
624f9152895SAl Viro {
625cc44c17bSAl Viro 	__wsum next = csum_partial_copy_nocheck(from, to, len);
626f9152895SAl Viro 	return csum_block_add(sum, next, off);
627f9152895SAl Viro }
628f9152895SAl Viro 
62978e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
6306852df12SAl Viro 					 struct iov_iter *i, __wsum *sump)
63178e1f386SAl Viro {
63278e1f386SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
6338cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
6346852df12SAl Viro 	__wsum sum = *sump;
6356852df12SAl Viro 	size_t off = 0;
6368cefc107SDavid Howells 	unsigned int i_head;
6376852df12SAl Viro 	size_t r;
63878e1f386SAl Viro 
63978e1f386SAl Viro 	if (!sanity(i))
64078e1f386SAl Viro 		return 0;
64178e1f386SAl Viro 
6426852df12SAl Viro 	bytes = push_pipe(i, bytes, &i_head, &r);
6436852df12SAl Viro 	while (bytes) {
6446852df12SAl Viro 		size_t chunk = min_t(size_t, bytes, PAGE_SIZE - r);
6452495bdccSAl Viro 		char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
6466852df12SAl Viro 		sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
6472495bdccSAl Viro 		kunmap_local(p);
6488cefc107SDavid Howells 		i->head = i_head;
64978e1f386SAl Viro 		i->iov_offset = r + chunk;
6506852df12SAl Viro 		bytes -= chunk;
65178e1f386SAl Viro 		off += chunk;
6528cefc107SDavid Howells 		r = 0;
6538cefc107SDavid Howells 		i_head++;
6546852df12SAl Viro 	}
6556852df12SAl Viro 	*sump = sum;
6566852df12SAl Viro 	i->count -= off;
6576852df12SAl Viro 	return off;
65878e1f386SAl Viro }
65978e1f386SAl Viro 
660aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
661d879cb83SAl Viro {
66200e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
663241699cdSAl Viro 		return copy_pipe_to_iter(addr, bytes, i);
66409fc68dcSAl Viro 	if (iter_is_iovec(i))
66509fc68dcSAl Viro 		might_fault();
6667baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6677baa5099SAl Viro 		copyout(base, addr + off, len),
6687baa5099SAl Viro 		memcpy(base, addr + off, len)
669d879cb83SAl Viro 	)
670d879cb83SAl Viro 
671d879cb83SAl Viro 	return bytes;
672d879cb83SAl Viro }
673aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter);
674d879cb83SAl Viro 
675ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC
676ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n)
6778780356eSDan Williams {
67896d4f267SLinus Torvalds 	if (access_ok(to, n)) {
679d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
680ec6347bbSDan Williams 		n = copy_mc_to_user((__force void *) to, from, n);
6818780356eSDan Williams 	}
6828780356eSDan Williams 	return n;
6838780356eSDan Williams }
6848780356eSDan Williams 
685ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
686ca146f6fSDan Williams 				struct iov_iter *i)
687ca146f6fSDan Williams {
688ca146f6fSDan Williams 	struct pipe_inode_info *pipe = i->pipe;
6898cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
6908cefc107SDavid Howells 	unsigned int i_head;
691ca146f6fSDan Williams 	size_t n, off, xfer = 0;
692ca146f6fSDan Williams 
693ca146f6fSDan Williams 	if (!sanity(i))
694ca146f6fSDan Williams 		return 0;
695ca146f6fSDan Williams 
6962a510a74SAl Viro 	n = push_pipe(i, bytes, &i_head, &off);
6972a510a74SAl Viro 	while (n) {
698ca146f6fSDan Williams 		size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
6992a510a74SAl Viro 		char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
700ca146f6fSDan Williams 		unsigned long rem;
7012a510a74SAl Viro 		rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
7022a510a74SAl Viro 		chunk -= rem;
7032a510a74SAl Viro 		kunmap_local(p);
7048cefc107SDavid Howells 		i->head = i_head;
7052a510a74SAl Viro 		i->iov_offset = off + chunk;
7062a510a74SAl Viro 		xfer += chunk;
707ca146f6fSDan Williams 		if (rem)
708ca146f6fSDan Williams 			break;
709ca146f6fSDan Williams 		n -= chunk;
7108cefc107SDavid Howells 		off = 0;
7118cefc107SDavid Howells 		i_head++;
7122a510a74SAl Viro 	}
713ca146f6fSDan Williams 	i->count -= xfer;
714ca146f6fSDan Williams 	return xfer;
715ca146f6fSDan Williams }
716ca146f6fSDan Williams 
717bf3eeb9bSDan Williams /**
718ec6347bbSDan Williams  * _copy_mc_to_iter - copy to iter with source memory error exception handling
719bf3eeb9bSDan Williams  * @addr: source kernel address
720bf3eeb9bSDan Williams  * @bytes: total transfer length
72144e55997SRandy Dunlap  * @i: destination iterator
722bf3eeb9bSDan Williams  *
723ec6347bbSDan Williams  * The pmem driver deploys this for the dax operation
724ec6347bbSDan Williams  * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
725ec6347bbSDan Williams  * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
726ec6347bbSDan Williams  * successfully copied.
727bf3eeb9bSDan Williams  *
728ec6347bbSDan Williams  * The main differences between this and typical _copy_to_iter().
729bf3eeb9bSDan Williams  *
730bf3eeb9bSDan Williams  * * Typical tail/residue handling after a fault retries the copy
731bf3eeb9bSDan Williams  *   byte-by-byte until the fault happens again. Re-triggering machine
732bf3eeb9bSDan Williams  *   checks is potentially fatal so the implementation uses source
733bf3eeb9bSDan Williams  *   alignment and poison alignment assumptions to avoid re-triggering
734bf3eeb9bSDan Williams  *   hardware exceptions.
735bf3eeb9bSDan Williams  *
736bf3eeb9bSDan Williams  * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
737bf3eeb9bSDan Williams  *   Compare to copy_to_iter() where only ITER_IOVEC attempts might return
738bf3eeb9bSDan Williams  *   a short copy.
73944e55997SRandy Dunlap  *
74044e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
741bf3eeb9bSDan Williams  */
742ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
7438780356eSDan Williams {
74400e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
745ec6347bbSDan Williams 		return copy_mc_pipe_to_iter(addr, bytes, i);
7468780356eSDan Williams 	if (iter_is_iovec(i))
7478780356eSDan Williams 		might_fault();
7487baa5099SAl Viro 	__iterate_and_advance(i, bytes, base, len, off,
7497baa5099SAl Viro 		copyout_mc(base, addr + off, len),
7507baa5099SAl Viro 		copy_mc_to_kernel(base, addr + off, len)
7518780356eSDan Williams 	)
7528780356eSDan Williams 
7538780356eSDan Williams 	return bytes;
7548780356eSDan Williams }
755ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
756ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */
7578780356eSDan Williams 
758aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
759d879cb83SAl Viro {
76000e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
761241699cdSAl Viro 		WARN_ON(1);
762241699cdSAl Viro 		return 0;
763241699cdSAl Viro 	}
76409fc68dcSAl Viro 	if (iter_is_iovec(i))
76509fc68dcSAl Viro 		might_fault();
7667baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
7677baa5099SAl Viro 		copyin(addr + off, base, len),
7687baa5099SAl Viro 		memcpy(addr + off, base, len)
769d879cb83SAl Viro 	)
770d879cb83SAl Viro 
771d879cb83SAl Viro 	return bytes;
772d879cb83SAl Viro }
773aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter);
774d879cb83SAl Viro 
775aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
776d879cb83SAl Viro {
77700e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
778241699cdSAl Viro 		WARN_ON(1);
779241699cdSAl Viro 		return 0;
780241699cdSAl Viro 	}
7817baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
7827baa5099SAl Viro 		__copy_from_user_inatomic_nocache(addr + off, base, len),
7837baa5099SAl Viro 		memcpy(addr + off, base, len)
784d879cb83SAl Viro 	)
785d879cb83SAl Viro 
786d879cb83SAl Viro 	return bytes;
787d879cb83SAl Viro }
788aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache);
789d879cb83SAl Viro 
7900aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
791abd08d7dSDan Williams /**
792abd08d7dSDan Williams  * _copy_from_iter_flushcache - write destination through cpu cache
793abd08d7dSDan Williams  * @addr: destination kernel address
794abd08d7dSDan Williams  * @bytes: total transfer length
79544e55997SRandy Dunlap  * @i: source iterator
796abd08d7dSDan Williams  *
797abd08d7dSDan Williams  * The pmem driver arranges for filesystem-dax to use this facility via
798abd08d7dSDan Williams  * dax_copy_from_iter() for ensuring that writes to persistent memory
799abd08d7dSDan Williams  * are flushed through the CPU cache. It is differentiated from
800abd08d7dSDan Williams  * _copy_from_iter_nocache() in that guarantees all data is flushed for
801abd08d7dSDan Williams  * all iterator types. The _copy_from_iter_nocache() only attempts to
802abd08d7dSDan Williams  * bypass the cache for the ITER_IOVEC case, and on some archs may use
803abd08d7dSDan Williams  * instructions that strand dirty-data in the cache.
80444e55997SRandy Dunlap  *
80544e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
806abd08d7dSDan Williams  */
8076a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
8080aed55afSDan Williams {
80900e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
8100aed55afSDan Williams 		WARN_ON(1);
8110aed55afSDan Williams 		return 0;
8120aed55afSDan Williams 	}
8137baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
8147baa5099SAl Viro 		__copy_from_user_flushcache(addr + off, base, len),
8157baa5099SAl Viro 		memcpy_flushcache(addr + off, base, len)
8160aed55afSDan Williams 	)
8170aed55afSDan Williams 
8180aed55afSDan Williams 	return bytes;
8190aed55afSDan Williams }
8206a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
8210aed55afSDan Williams #endif
8220aed55afSDan Williams 
82372e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
82472e809edSAl Viro {
8256daef95bSEric Dumazet 	struct page *head;
8266daef95bSEric Dumazet 	size_t v = n + offset;
8276daef95bSEric Dumazet 
8286daef95bSEric Dumazet 	/*
8296daef95bSEric Dumazet 	 * The general case needs to access the page order in order
8306daef95bSEric Dumazet 	 * to compute the page size.
8316daef95bSEric Dumazet 	 * However, we mostly deal with order-0 pages and thus can
8326daef95bSEric Dumazet 	 * avoid a possible cache line miss for requests that fit all
8336daef95bSEric Dumazet 	 * page orders.
8346daef95bSEric Dumazet 	 */
8356daef95bSEric Dumazet 	if (n <= v && v <= PAGE_SIZE)
8366daef95bSEric Dumazet 		return true;
8376daef95bSEric Dumazet 
8386daef95bSEric Dumazet 	head = compound_head(page);
8396daef95bSEric Dumazet 	v += (page - head) << PAGE_SHIFT;
840a90bcb86SPetar Penkov 
841a50b854eSMatthew Wilcox (Oracle) 	if (likely(n <= v && v <= (page_size(head))))
84272e809edSAl Viro 		return true;
84372e809edSAl Viro 	WARN_ON(1);
84472e809edSAl Viro 	return false;
84572e809edSAl Viro }
846cbbd26b8SAl Viro 
84708aa6479SAl Viro static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
848d879cb83SAl Viro 			 struct iov_iter *i)
849d879cb83SAl Viro {
85028f38db7SAl Viro 	if (likely(iter_is_iovec(i)))
85128f38db7SAl Viro 		return copy_page_to_iter_iovec(page, offset, bytes, i);
85228f38db7SAl Viro 	if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
853c1d4d6a9SAl Viro 		void *kaddr = kmap_local_page(page);
854c1d4d6a9SAl Viro 		size_t wanted = _copy_to_iter(kaddr + offset, bytes, i);
855c1d4d6a9SAl Viro 		kunmap_local(kaddr);
856d879cb83SAl Viro 		return wanted;
85728f38db7SAl Viro 	}
85828f38db7SAl Viro 	if (iov_iter_is_pipe(i))
85928f38db7SAl Viro 		return copy_page_to_iter_pipe(page, offset, bytes, i);
86028f38db7SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
861a506abc7SAl Viro 		if (unlikely(i->count < bytes))
862a506abc7SAl Viro 			bytes = i->count;
863a506abc7SAl Viro 		i->count -= bytes;
8649ea9ce04SDavid Howells 		return bytes;
86528f38db7SAl Viro 	}
86628f38db7SAl Viro 	WARN_ON(1);
86728f38db7SAl Viro 	return 0;
868d879cb83SAl Viro }
86908aa6479SAl Viro 
87008aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
87108aa6479SAl Viro 			 struct iov_iter *i)
87208aa6479SAl Viro {
87308aa6479SAl Viro 	size_t res = 0;
87408aa6479SAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes)))
87508aa6479SAl Viro 		return 0;
87608aa6479SAl Viro 	page += offset / PAGE_SIZE; // first subpage
87708aa6479SAl Viro 	offset %= PAGE_SIZE;
87808aa6479SAl Viro 	while (1) {
87908aa6479SAl Viro 		size_t n = __copy_page_to_iter(page, offset,
88008aa6479SAl Viro 				min(bytes, (size_t)PAGE_SIZE - offset), i);
88108aa6479SAl Viro 		res += n;
88208aa6479SAl Viro 		bytes -= n;
88308aa6479SAl Viro 		if (!bytes || !n)
88408aa6479SAl Viro 			break;
88508aa6479SAl Viro 		offset += n;
88608aa6479SAl Viro 		if (offset == PAGE_SIZE) {
88708aa6479SAl Viro 			page++;
88808aa6479SAl Viro 			offset = 0;
88908aa6479SAl Viro 		}
89008aa6479SAl Viro 	}
89108aa6479SAl Viro 	return res;
89208aa6479SAl Viro }
893d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter);
894d879cb83SAl Viro 
895d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
896d879cb83SAl Viro 			 struct iov_iter *i)
897d879cb83SAl Viro {
89872e809edSAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes)))
89972e809edSAl Viro 		return 0;
90028f38db7SAl Viro 	if (likely(iter_is_iovec(i)))
90128f38db7SAl Viro 		return copy_page_from_iter_iovec(page, offset, bytes, i);
90228f38db7SAl Viro 	if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
90355ca375cSAl Viro 		void *kaddr = kmap_local_page(page);
904aa28de27SAl Viro 		size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
90555ca375cSAl Viro 		kunmap_local(kaddr);
906d879cb83SAl Viro 		return wanted;
90728f38db7SAl Viro 	}
90828f38db7SAl Viro 	WARN_ON(1);
90928f38db7SAl Viro 	return 0;
910d879cb83SAl Viro }
911d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter);
912d879cb83SAl Viro 
913241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i)
914241699cdSAl Viro {
915241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
9168cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
9178cefc107SDavid Howells 	unsigned int i_head;
918241699cdSAl Viro 	size_t n, off;
919241699cdSAl Viro 
920241699cdSAl Viro 	if (!sanity(i))
921241699cdSAl Viro 		return 0;
922241699cdSAl Viro 
9238cefc107SDavid Howells 	bytes = n = push_pipe(i, bytes, &i_head, &off);
924241699cdSAl Viro 	if (unlikely(!n))
925241699cdSAl Viro 		return 0;
926241699cdSAl Viro 
9278cefc107SDavid Howells 	do {
928241699cdSAl Viro 		size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
929893839fdSAl Viro 		char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
930893839fdSAl Viro 		memset(p + off, 0, chunk);
931893839fdSAl Viro 		kunmap_local(p);
9328cefc107SDavid Howells 		i->head = i_head;
933241699cdSAl Viro 		i->iov_offset = off + chunk;
934241699cdSAl Viro 		n -= chunk;
9358cefc107SDavid Howells 		off = 0;
9368cefc107SDavid Howells 		i_head++;
9378cefc107SDavid Howells 	} while (n);
938241699cdSAl Viro 	i->count -= bytes;
939241699cdSAl Viro 	return bytes;
940241699cdSAl Viro }
941241699cdSAl Viro 
942d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
943d879cb83SAl Viro {
94400e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
945241699cdSAl Viro 		return pipe_zero(bytes, i);
9467baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, count,
9477baa5099SAl Viro 		clear_user(base, len),
9487baa5099SAl Viro 		memset(base, 0, len)
949d879cb83SAl Viro 	)
950d879cb83SAl Viro 
951d879cb83SAl Viro 	return bytes;
952d879cb83SAl Viro }
953d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero);
954d879cb83SAl Viro 
955f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
956f0b65f39SAl Viro 				  struct iov_iter *i)
957d879cb83SAl Viro {
958d879cb83SAl Viro 	char *kaddr = kmap_atomic(page), *p = kaddr + offset;
95972e809edSAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes))) {
96072e809edSAl Viro 		kunmap_atomic(kaddr);
96172e809edSAl Viro 		return 0;
96272e809edSAl Viro 	}
9639ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
964241699cdSAl Viro 		kunmap_atomic(kaddr);
965241699cdSAl Viro 		WARN_ON(1);
966241699cdSAl Viro 		return 0;
967241699cdSAl Viro 	}
9687baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
9697baa5099SAl Viro 		copyin(p + off, base, len),
9707baa5099SAl Viro 		memcpy(p + off, base, len)
971d879cb83SAl Viro 	)
972d879cb83SAl Viro 	kunmap_atomic(kaddr);
973d879cb83SAl Viro 	return bytes;
974d879cb83SAl Viro }
975f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic);
976d879cb83SAl Viro 
977b9dc6f65SAl Viro static inline void pipe_truncate(struct iov_iter *i)
978241699cdSAl Viro {
979241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
9808cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
9818cefc107SDavid Howells 	unsigned int p_head = pipe->head;
9828cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
9838cefc107SDavid Howells 
9848cefc107SDavid Howells 	if (!pipe_empty(p_head, p_tail)) {
9858cefc107SDavid Howells 		struct pipe_buffer *buf;
9868cefc107SDavid Howells 		unsigned int i_head = i->head;
987b9dc6f65SAl Viro 		size_t off = i->iov_offset;
9888cefc107SDavid Howells 
989b9dc6f65SAl Viro 		if (off) {
9908cefc107SDavid Howells 			buf = &pipe->bufs[i_head & p_mask];
9918cefc107SDavid Howells 			buf->len = off - buf->offset;
9928cefc107SDavid Howells 			i_head++;
993b9dc6f65SAl Viro 		}
9948cefc107SDavid Howells 		while (p_head != i_head) {
9958cefc107SDavid Howells 			p_head--;
9968cefc107SDavid Howells 			pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
997241699cdSAl Viro 		}
9988cefc107SDavid Howells 
9998cefc107SDavid Howells 		pipe->head = p_head;
1000241699cdSAl Viro 	}
1001b9dc6f65SAl Viro }
1002b9dc6f65SAl Viro 
1003b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size)
1004b9dc6f65SAl Viro {
1005b9dc6f65SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
1006b9dc6f65SAl Viro 	if (size) {
1007b9dc6f65SAl Viro 		struct pipe_buffer *buf;
10088cefc107SDavid Howells 		unsigned int p_mask = pipe->ring_size - 1;
10098cefc107SDavid Howells 		unsigned int i_head = i->head;
1010b9dc6f65SAl Viro 		size_t off = i->iov_offset, left = size;
10118cefc107SDavid Howells 
1012b9dc6f65SAl Viro 		if (off) /* make it relative to the beginning of buffer */
10138cefc107SDavid Howells 			left += off - pipe->bufs[i_head & p_mask].offset;
1014b9dc6f65SAl Viro 		while (1) {
10158cefc107SDavid Howells 			buf = &pipe->bufs[i_head & p_mask];
1016b9dc6f65SAl Viro 			if (left <= buf->len)
1017b9dc6f65SAl Viro 				break;
1018b9dc6f65SAl Viro 			left -= buf->len;
10198cefc107SDavid Howells 			i_head++;
1020b9dc6f65SAl Viro 		}
10218cefc107SDavid Howells 		i->head = i_head;
1022b9dc6f65SAl Viro 		i->iov_offset = buf->offset + left;
1023b9dc6f65SAl Viro 	}
1024b9dc6f65SAl Viro 	i->count -= size;
1025b9dc6f65SAl Viro 	/* ... and discard everything past that point */
1026b9dc6f65SAl Viro 	pipe_truncate(i);
1027241699cdSAl Viro }
1028241699cdSAl Viro 
102954c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
103054c8195bSPavel Begunkov {
103154c8195bSPavel Begunkov 	struct bvec_iter bi;
103254c8195bSPavel Begunkov 
103354c8195bSPavel Begunkov 	bi.bi_size = i->count;
103454c8195bSPavel Begunkov 	bi.bi_bvec_done = i->iov_offset;
103554c8195bSPavel Begunkov 	bi.bi_idx = 0;
103654c8195bSPavel Begunkov 	bvec_iter_advance(i->bvec, &bi, size);
103754c8195bSPavel Begunkov 
103854c8195bSPavel Begunkov 	i->bvec += bi.bi_idx;
103954c8195bSPavel Begunkov 	i->nr_segs -= bi.bi_idx;
104054c8195bSPavel Begunkov 	i->count = bi.bi_size;
104154c8195bSPavel Begunkov 	i->iov_offset = bi.bi_bvec_done;
104254c8195bSPavel Begunkov }
104354c8195bSPavel Begunkov 
1044185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
1045185ac4d4SAl Viro {
1046185ac4d4SAl Viro 	const struct iovec *iov, *end;
1047185ac4d4SAl Viro 
1048185ac4d4SAl Viro 	if (!i->count)
1049185ac4d4SAl Viro 		return;
1050185ac4d4SAl Viro 	i->count -= size;
1051185ac4d4SAl Viro 
1052185ac4d4SAl Viro 	size += i->iov_offset; // from beginning of current segment
1053185ac4d4SAl Viro 	for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
1054185ac4d4SAl Viro 		if (likely(size < iov->iov_len))
1055185ac4d4SAl Viro 			break;
1056185ac4d4SAl Viro 		size -= iov->iov_len;
1057185ac4d4SAl Viro 	}
1058185ac4d4SAl Viro 	i->iov_offset = size;
1059185ac4d4SAl Viro 	i->nr_segs -= iov - i->iov;
1060185ac4d4SAl Viro 	i->iov = iov;
1061185ac4d4SAl Viro }
1062185ac4d4SAl Viro 
1063d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size)
1064d879cb83SAl Viro {
10653b3fc051SAl Viro 	if (unlikely(i->count < size))
10663b3fc051SAl Viro 		size = i->count;
1067185ac4d4SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
1068185ac4d4SAl Viro 		/* iovec and kvec have identical layouts */
1069185ac4d4SAl Viro 		iov_iter_iovec_advance(i, size);
1070185ac4d4SAl Viro 	} else if (iov_iter_is_bvec(i)) {
1071185ac4d4SAl Viro 		iov_iter_bvec_advance(i, size);
1072185ac4d4SAl Viro 	} else if (iov_iter_is_pipe(i)) {
1073241699cdSAl Viro 		pipe_advance(i, size);
1074185ac4d4SAl Viro 	} else if (unlikely(iov_iter_is_xarray(i))) {
10757ff50620SDavid Howells 		i->iov_offset += size;
10767ff50620SDavid Howells 		i->count -= size;
1077185ac4d4SAl Viro 	} else if (iov_iter_is_discard(i)) {
1078185ac4d4SAl Viro 		i->count -= size;
10797ff50620SDavid Howells 	}
1080d879cb83SAl Viro }
1081d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance);
1082d879cb83SAl Viro 
108327c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll)
108427c0e374SAl Viro {
108527c0e374SAl Viro 	if (!unroll)
108627c0e374SAl Viro 		return;
10875b47d59aSAl Viro 	if (WARN_ON(unroll > MAX_RW_COUNT))
10885b47d59aSAl Viro 		return;
108927c0e374SAl Viro 	i->count += unroll;
109000e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
109127c0e374SAl Viro 		struct pipe_inode_info *pipe = i->pipe;
10928cefc107SDavid Howells 		unsigned int p_mask = pipe->ring_size - 1;
10938cefc107SDavid Howells 		unsigned int i_head = i->head;
109427c0e374SAl Viro 		size_t off = i->iov_offset;
109527c0e374SAl Viro 		while (1) {
10968cefc107SDavid Howells 			struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
10978cefc107SDavid Howells 			size_t n = off - b->offset;
109827c0e374SAl Viro 			if (unroll < n) {
10994fa55cefSAl Viro 				off -= unroll;
110027c0e374SAl Viro 				break;
110127c0e374SAl Viro 			}
110227c0e374SAl Viro 			unroll -= n;
11038cefc107SDavid Howells 			if (!unroll && i_head == i->start_head) {
110427c0e374SAl Viro 				off = 0;
110527c0e374SAl Viro 				break;
110627c0e374SAl Viro 			}
11078cefc107SDavid Howells 			i_head--;
11088cefc107SDavid Howells 			b = &pipe->bufs[i_head & p_mask];
11098cefc107SDavid Howells 			off = b->offset + b->len;
111027c0e374SAl Viro 		}
111127c0e374SAl Viro 		i->iov_offset = off;
11128cefc107SDavid Howells 		i->head = i_head;
111327c0e374SAl Viro 		pipe_truncate(i);
111427c0e374SAl Viro 		return;
111527c0e374SAl Viro 	}
11169ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_discard(i)))
11179ea9ce04SDavid Howells 		return;
111827c0e374SAl Viro 	if (unroll <= i->iov_offset) {
111927c0e374SAl Viro 		i->iov_offset -= unroll;
112027c0e374SAl Viro 		return;
112127c0e374SAl Viro 	}
112227c0e374SAl Viro 	unroll -= i->iov_offset;
11237ff50620SDavid Howells 	if (iov_iter_is_xarray(i)) {
11247ff50620SDavid Howells 		BUG(); /* We should never go beyond the start of the specified
11257ff50620SDavid Howells 			* range since we might then be straying into pages that
11267ff50620SDavid Howells 			* aren't pinned.
11277ff50620SDavid Howells 			*/
11287ff50620SDavid Howells 	} else if (iov_iter_is_bvec(i)) {
112927c0e374SAl Viro 		const struct bio_vec *bvec = i->bvec;
113027c0e374SAl Viro 		while (1) {
113127c0e374SAl Viro 			size_t n = (--bvec)->bv_len;
113227c0e374SAl Viro 			i->nr_segs++;
113327c0e374SAl Viro 			if (unroll <= n) {
113427c0e374SAl Viro 				i->bvec = bvec;
113527c0e374SAl Viro 				i->iov_offset = n - unroll;
113627c0e374SAl Viro 				return;
113727c0e374SAl Viro 			}
113827c0e374SAl Viro 			unroll -= n;
113927c0e374SAl Viro 		}
114027c0e374SAl Viro 	} else { /* same logics for iovec and kvec */
114127c0e374SAl Viro 		const struct iovec *iov = i->iov;
114227c0e374SAl Viro 		while (1) {
114327c0e374SAl Viro 			size_t n = (--iov)->iov_len;
114427c0e374SAl Viro 			i->nr_segs++;
114527c0e374SAl Viro 			if (unroll <= n) {
114627c0e374SAl Viro 				i->iov = iov;
114727c0e374SAl Viro 				i->iov_offset = n - unroll;
114827c0e374SAl Viro 				return;
114927c0e374SAl Viro 			}
115027c0e374SAl Viro 			unroll -= n;
115127c0e374SAl Viro 		}
115227c0e374SAl Viro 	}
115327c0e374SAl Viro }
115427c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert);
115527c0e374SAl Viro 
1156d879cb83SAl Viro /*
1157d879cb83SAl Viro  * Return the count of just the current iov_iter segment.
1158d879cb83SAl Viro  */
1159d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i)
1160d879cb83SAl Viro {
116128f38db7SAl Viro 	if (i->nr_segs > 1) {
116228f38db7SAl Viro 		if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
116328f38db7SAl Viro 			return min(i->count, i->iov->iov_len - i->iov_offset);
11647ff50620SDavid Howells 		if (iov_iter_is_bvec(i))
1165d879cb83SAl Viro 			return min(i->count, i->bvec->bv_len - i->iov_offset);
116628f38db7SAl Viro 	}
116728f38db7SAl Viro 	return i->count;
1168d879cb83SAl Viro }
1169d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count);
1170d879cb83SAl Viro 
1171aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
1172d879cb83SAl Viro 			const struct kvec *kvec, unsigned long nr_segs,
1173d879cb83SAl Viro 			size_t count)
1174d879cb83SAl Viro {
1175aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
11768cd54c1cSAl Viro 	*i = (struct iov_iter){
11778cd54c1cSAl Viro 		.iter_type = ITER_KVEC,
11788cd54c1cSAl Viro 		.data_source = direction,
11798cd54c1cSAl Viro 		.kvec = kvec,
11808cd54c1cSAl Viro 		.nr_segs = nr_segs,
11818cd54c1cSAl Viro 		.iov_offset = 0,
11828cd54c1cSAl Viro 		.count = count
11838cd54c1cSAl Viro 	};
1184d879cb83SAl Viro }
1185d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec);
1186d879cb83SAl Viro 
1187aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1188d879cb83SAl Viro 			const struct bio_vec *bvec, unsigned long nr_segs,
1189d879cb83SAl Viro 			size_t count)
1190d879cb83SAl Viro {
1191aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
11928cd54c1cSAl Viro 	*i = (struct iov_iter){
11938cd54c1cSAl Viro 		.iter_type = ITER_BVEC,
11948cd54c1cSAl Viro 		.data_source = direction,
11958cd54c1cSAl Viro 		.bvec = bvec,
11968cd54c1cSAl Viro 		.nr_segs = nr_segs,
11978cd54c1cSAl Viro 		.iov_offset = 0,
11988cd54c1cSAl Viro 		.count = count
11998cd54c1cSAl Viro 	};
1200d879cb83SAl Viro }
1201d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec);
1202d879cb83SAl Viro 
1203aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1204241699cdSAl Viro 			struct pipe_inode_info *pipe,
1205241699cdSAl Viro 			size_t count)
1206241699cdSAl Viro {
1207aa563d7bSDavid Howells 	BUG_ON(direction != READ);
12088cefc107SDavid Howells 	WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
12098cd54c1cSAl Viro 	*i = (struct iov_iter){
12108cd54c1cSAl Viro 		.iter_type = ITER_PIPE,
12118cd54c1cSAl Viro 		.data_source = false,
12128cd54c1cSAl Viro 		.pipe = pipe,
12138cd54c1cSAl Viro 		.head = pipe->head,
12148cd54c1cSAl Viro 		.start_head = pipe->head,
12158cd54c1cSAl Viro 		.iov_offset = 0,
12168cd54c1cSAl Viro 		.count = count
12178cd54c1cSAl Viro 	};
1218241699cdSAl Viro }
1219241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe);
1220241699cdSAl Viro 
12219ea9ce04SDavid Howells /**
12227ff50620SDavid Howells  * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
12237ff50620SDavid Howells  * @i: The iterator to initialise.
12247ff50620SDavid Howells  * @direction: The direction of the transfer.
12257ff50620SDavid Howells  * @xarray: The xarray to access.
12267ff50620SDavid Howells  * @start: The start file position.
12277ff50620SDavid Howells  * @count: The size of the I/O buffer in bytes.
12287ff50620SDavid Howells  *
12297ff50620SDavid Howells  * Set up an I/O iterator to either draw data out of the pages attached to an
12307ff50620SDavid Howells  * inode or to inject data into those pages.  The pages *must* be prevented
12317ff50620SDavid Howells  * from evaporation, either by taking a ref on them or locking them by the
12327ff50620SDavid Howells  * caller.
12337ff50620SDavid Howells  */
12347ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
12357ff50620SDavid Howells 		     struct xarray *xarray, loff_t start, size_t count)
12367ff50620SDavid Howells {
12377ff50620SDavid Howells 	BUG_ON(direction & ~1);
12388cd54c1cSAl Viro 	*i = (struct iov_iter) {
12398cd54c1cSAl Viro 		.iter_type = ITER_XARRAY,
12408cd54c1cSAl Viro 		.data_source = direction,
12418cd54c1cSAl Viro 		.xarray = xarray,
12428cd54c1cSAl Viro 		.xarray_start = start,
12438cd54c1cSAl Viro 		.count = count,
12448cd54c1cSAl Viro 		.iov_offset = 0
12458cd54c1cSAl Viro 	};
12467ff50620SDavid Howells }
12477ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray);
12487ff50620SDavid Howells 
12497ff50620SDavid Howells /**
12509ea9ce04SDavid Howells  * iov_iter_discard - Initialise an I/O iterator that discards data
12519ea9ce04SDavid Howells  * @i: The iterator to initialise.
12529ea9ce04SDavid Howells  * @direction: The direction of the transfer.
12539ea9ce04SDavid Howells  * @count: The size of the I/O buffer in bytes.
12549ea9ce04SDavid Howells  *
12559ea9ce04SDavid Howells  * Set up an I/O iterator that just discards everything that's written to it.
12569ea9ce04SDavid Howells  * It's only available as a READ iterator.
12579ea9ce04SDavid Howells  */
12589ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
12599ea9ce04SDavid Howells {
12609ea9ce04SDavid Howells 	BUG_ON(direction != READ);
12618cd54c1cSAl Viro 	*i = (struct iov_iter){
12628cd54c1cSAl Viro 		.iter_type = ITER_DISCARD,
12638cd54c1cSAl Viro 		.data_source = false,
12648cd54c1cSAl Viro 		.count = count,
12658cd54c1cSAl Viro 		.iov_offset = 0
12668cd54c1cSAl Viro 	};
12679ea9ce04SDavid Howells }
12689ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard);
12699ea9ce04SDavid Howells 
12709221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1271d879cb83SAl Viro {
1272d879cb83SAl Viro 	unsigned long res = 0;
1273d879cb83SAl Viro 	size_t size = i->count;
12749221d2e3SAl Viro 	size_t skip = i->iov_offset;
12759221d2e3SAl Viro 	unsigned k;
1276d879cb83SAl Viro 
12779221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
12789221d2e3SAl Viro 		size_t len = i->iov[k].iov_len - skip;
12799221d2e3SAl Viro 		if (len) {
12809221d2e3SAl Viro 			res |= (unsigned long)i->iov[k].iov_base + skip;
12819221d2e3SAl Viro 			if (len > size)
12829221d2e3SAl Viro 				len = size;
12839221d2e3SAl Viro 			res |= len;
12849221d2e3SAl Viro 			size -= len;
12859221d2e3SAl Viro 			if (!size)
12869221d2e3SAl Viro 				break;
12879221d2e3SAl Viro 		}
12889221d2e3SAl Viro 	}
12899221d2e3SAl Viro 	return res;
12909221d2e3SAl Viro }
12919221d2e3SAl Viro 
12929221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
12939221d2e3SAl Viro {
12949221d2e3SAl Viro 	unsigned res = 0;
12959221d2e3SAl Viro 	size_t size = i->count;
12969221d2e3SAl Viro 	unsigned skip = i->iov_offset;
12979221d2e3SAl Viro 	unsigned k;
12989221d2e3SAl Viro 
12999221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
13009221d2e3SAl Viro 		size_t len = i->bvec[k].bv_len - skip;
13019221d2e3SAl Viro 		res |= (unsigned long)i->bvec[k].bv_offset + skip;
13029221d2e3SAl Viro 		if (len > size)
13039221d2e3SAl Viro 			len = size;
13049221d2e3SAl Viro 		res |= len;
13059221d2e3SAl Viro 		size -= len;
13069221d2e3SAl Viro 		if (!size)
13079221d2e3SAl Viro 			break;
13089221d2e3SAl Viro 	}
13099221d2e3SAl Viro 	return res;
13109221d2e3SAl Viro }
13119221d2e3SAl Viro 
13129221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i)
13139221d2e3SAl Viro {
13149221d2e3SAl Viro 	/* iovec and kvec have identical layouts */
13159221d2e3SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
13169221d2e3SAl Viro 		return iov_iter_alignment_iovec(i);
13179221d2e3SAl Viro 
13189221d2e3SAl Viro 	if (iov_iter_is_bvec(i))
13199221d2e3SAl Viro 		return iov_iter_alignment_bvec(i);
13209221d2e3SAl Viro 
13219221d2e3SAl Viro 	if (iov_iter_is_pipe(i)) {
1322e0ff126eSJan Kara 		unsigned int p_mask = i->pipe->ring_size - 1;
13239221d2e3SAl Viro 		size_t size = i->count;
1324e0ff126eSJan Kara 
13258cefc107SDavid Howells 		if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
1326241699cdSAl Viro 			return size | i->iov_offset;
1327241699cdSAl Viro 		return size;
1328241699cdSAl Viro 	}
13299221d2e3SAl Viro 
13309221d2e3SAl Viro 	if (iov_iter_is_xarray(i))
13313d14ec1fSDavid Howells 		return (i->xarray_start + i->iov_offset) | i->count;
13329221d2e3SAl Viro 
13339221d2e3SAl Viro 	return 0;
1334d879cb83SAl Viro }
1335d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment);
1336d879cb83SAl Viro 
1337357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1338357f435dSAl Viro {
1339357f435dSAl Viro 	unsigned long res = 0;
1340610c7a71SAl Viro 	unsigned long v = 0;
1341357f435dSAl Viro 	size_t size = i->count;
1342610c7a71SAl Viro 	unsigned k;
1343357f435dSAl Viro 
1344610c7a71SAl Viro 	if (WARN_ON(!iter_is_iovec(i)))
1345241699cdSAl Viro 		return ~0U;
1346241699cdSAl Viro 
1347610c7a71SAl Viro 	for (k = 0; k < i->nr_segs; k++) {
1348610c7a71SAl Viro 		if (i->iov[k].iov_len) {
1349610c7a71SAl Viro 			unsigned long base = (unsigned long)i->iov[k].iov_base;
1350610c7a71SAl Viro 			if (v) // if not the first one
1351610c7a71SAl Viro 				res |= base | v; // this start | previous end
1352610c7a71SAl Viro 			v = base + i->iov[k].iov_len;
1353610c7a71SAl Viro 			if (size <= i->iov[k].iov_len)
1354610c7a71SAl Viro 				break;
1355610c7a71SAl Viro 			size -= i->iov[k].iov_len;
1356610c7a71SAl Viro 		}
1357610c7a71SAl Viro 	}
1358357f435dSAl Viro 	return res;
1359357f435dSAl Viro }
1360357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment);
1361357f435dSAl Viro 
1362e76b6312SIlya Dryomov static inline ssize_t __pipe_get_pages(struct iov_iter *i,
1363241699cdSAl Viro 				size_t maxsize,
1364241699cdSAl Viro 				struct page **pages,
13658cefc107SDavid Howells 				int iter_head,
1366241699cdSAl Viro 				size_t *start)
1367241699cdSAl Viro {
1368241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
13698cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
13708cefc107SDavid Howells 	ssize_t n = push_pipe(i, maxsize, &iter_head, start);
1371241699cdSAl Viro 	if (!n)
1372241699cdSAl Viro 		return -EFAULT;
1373241699cdSAl Viro 
1374241699cdSAl Viro 	maxsize = n;
1375241699cdSAl Viro 	n += *start;
13761689c73aSAl Viro 	while (n > 0) {
13778cefc107SDavid Howells 		get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
13788cefc107SDavid Howells 		iter_head++;
1379241699cdSAl Viro 		n -= PAGE_SIZE;
1380241699cdSAl Viro 	}
1381241699cdSAl Viro 
1382241699cdSAl Viro 	return maxsize;
1383241699cdSAl Viro }
1384241699cdSAl Viro 
1385241699cdSAl Viro static ssize_t pipe_get_pages(struct iov_iter *i,
1386241699cdSAl Viro 		   struct page **pages, size_t maxsize, unsigned maxpages,
1387241699cdSAl Viro 		   size_t *start)
1388241699cdSAl Viro {
13898cefc107SDavid Howells 	unsigned int iter_head, npages;
1390241699cdSAl Viro 	size_t capacity;
1391241699cdSAl Viro 
1392241699cdSAl Viro 	if (!sanity(i))
1393241699cdSAl Viro 		return -EFAULT;
1394241699cdSAl Viro 
13958cefc107SDavid Howells 	data_start(i, &iter_head, start);
13968cefc107SDavid Howells 	/* Amount of free space: some of this one + all after this one */
13978cefc107SDavid Howells 	npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1398241699cdSAl Viro 	capacity = min(npages, maxpages) * PAGE_SIZE - *start;
1399241699cdSAl Viro 
14008cefc107SDavid Howells 	return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
1401241699cdSAl Viro }
1402241699cdSAl Viro 
14037ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
14047ff50620SDavid Howells 					  pgoff_t index, unsigned int nr_pages)
14057ff50620SDavid Howells {
14067ff50620SDavid Howells 	XA_STATE(xas, xa, index);
14077ff50620SDavid Howells 	struct page *page;
14087ff50620SDavid Howells 	unsigned int ret = 0;
14097ff50620SDavid Howells 
14107ff50620SDavid Howells 	rcu_read_lock();
14117ff50620SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
14127ff50620SDavid Howells 		if (xas_retry(&xas, page))
14137ff50620SDavid Howells 			continue;
14147ff50620SDavid Howells 
14157ff50620SDavid Howells 		/* Has the page moved or been split? */
14167ff50620SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
14177ff50620SDavid Howells 			xas_reset(&xas);
14187ff50620SDavid Howells 			continue;
14197ff50620SDavid Howells 		}
14207ff50620SDavid Howells 
14217ff50620SDavid Howells 		pages[ret] = find_subpage(page, xas.xa_index);
14227ff50620SDavid Howells 		get_page(pages[ret]);
14237ff50620SDavid Howells 		if (++ret == nr_pages)
14247ff50620SDavid Howells 			break;
14257ff50620SDavid Howells 	}
14267ff50620SDavid Howells 	rcu_read_unlock();
14277ff50620SDavid Howells 	return ret;
14287ff50620SDavid Howells }
14297ff50620SDavid Howells 
14307ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i,
14317ff50620SDavid Howells 				     struct page **pages, size_t maxsize,
14327ff50620SDavid Howells 				     unsigned maxpages, size_t *_start_offset)
14337ff50620SDavid Howells {
14347ff50620SDavid Howells 	unsigned nr, offset;
14357ff50620SDavid Howells 	pgoff_t index, count;
14367ff50620SDavid Howells 	size_t size = maxsize, actual;
14377ff50620SDavid Howells 	loff_t pos;
14387ff50620SDavid Howells 
14397ff50620SDavid Howells 	if (!size || !maxpages)
14407ff50620SDavid Howells 		return 0;
14417ff50620SDavid Howells 
14427ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
14437ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
14447ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
14457ff50620SDavid Howells 	*_start_offset = offset;
14467ff50620SDavid Howells 
14477ff50620SDavid Howells 	count = 1;
14487ff50620SDavid Howells 	if (size > PAGE_SIZE - offset) {
14497ff50620SDavid Howells 		size -= PAGE_SIZE - offset;
14507ff50620SDavid Howells 		count += size >> PAGE_SHIFT;
14517ff50620SDavid Howells 		size &= ~PAGE_MASK;
14527ff50620SDavid Howells 		if (size)
14537ff50620SDavid Howells 			count++;
14547ff50620SDavid Howells 	}
14557ff50620SDavid Howells 
14567ff50620SDavid Howells 	if (count > maxpages)
14577ff50620SDavid Howells 		count = maxpages;
14587ff50620SDavid Howells 
14597ff50620SDavid Howells 	nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
14607ff50620SDavid Howells 	if (nr == 0)
14617ff50620SDavid Howells 		return 0;
14627ff50620SDavid Howells 
14637ff50620SDavid Howells 	actual = PAGE_SIZE * nr;
14647ff50620SDavid Howells 	actual -= offset;
14657ff50620SDavid Howells 	if (nr == count && size > 0) {
14667ff50620SDavid Howells 		unsigned last_offset = (nr > 1) ? 0 : offset;
14677ff50620SDavid Howells 		actual -= PAGE_SIZE - (last_offset + size);
14687ff50620SDavid Howells 	}
14697ff50620SDavid Howells 	return actual;
14707ff50620SDavid Howells }
14717ff50620SDavid Howells 
14723d671ca6SAl Viro /* must be done on non-empty ITER_IOVEC one */
14733d671ca6SAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i,
14743d671ca6SAl Viro 					 size_t *size, size_t *start,
14753d671ca6SAl Viro 					 size_t maxsize, unsigned maxpages)
14763d671ca6SAl Viro {
14773d671ca6SAl Viro 	size_t skip;
14783d671ca6SAl Viro 	long k;
14793d671ca6SAl Viro 
14803d671ca6SAl Viro 	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
14813d671ca6SAl Viro 		unsigned long addr = (unsigned long)i->iov[k].iov_base + skip;
14823d671ca6SAl Viro 		size_t len = i->iov[k].iov_len - skip;
14833d671ca6SAl Viro 
14843d671ca6SAl Viro 		if (unlikely(!len))
14853d671ca6SAl Viro 			continue;
14863d671ca6SAl Viro 		if (len > maxsize)
14873d671ca6SAl Viro 			len = maxsize;
14883d671ca6SAl Viro 		len += (*start = addr % PAGE_SIZE);
14893d671ca6SAl Viro 		if (len > maxpages * PAGE_SIZE)
14903d671ca6SAl Viro 			len = maxpages * PAGE_SIZE;
14913d671ca6SAl Viro 		*size = len;
14923d671ca6SAl Viro 		return addr & PAGE_MASK;
14933d671ca6SAl Viro 	}
14943d671ca6SAl Viro 	BUG(); // if it had been empty, we wouldn't get called
14953d671ca6SAl Viro }
14963d671ca6SAl Viro 
14973d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */
14983d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i,
14993d671ca6SAl Viro 				       size_t *size, size_t *start,
15003d671ca6SAl Viro 				       size_t maxsize, unsigned maxpages)
15013d671ca6SAl Viro {
15023d671ca6SAl Viro 	struct page *page;
15033d671ca6SAl Viro 	size_t skip = i->iov_offset, len;
15043d671ca6SAl Viro 
15053d671ca6SAl Viro 	len = i->bvec->bv_len - skip;
15063d671ca6SAl Viro 	if (len > maxsize)
15073d671ca6SAl Viro 		len = maxsize;
15083d671ca6SAl Viro 	skip += i->bvec->bv_offset;
15093d671ca6SAl Viro 	page = i->bvec->bv_page + skip / PAGE_SIZE;
15103d671ca6SAl Viro 	len += (*start = skip % PAGE_SIZE);
15113d671ca6SAl Viro 	if (len > maxpages * PAGE_SIZE)
15123d671ca6SAl Viro 		len = maxpages * PAGE_SIZE;
15133d671ca6SAl Viro 	*size = len;
15143d671ca6SAl Viro 	return page;
15153d671ca6SAl Viro }
15163d671ca6SAl Viro 
1517d879cb83SAl Viro ssize_t iov_iter_get_pages(struct iov_iter *i,
1518d879cb83SAl Viro 		   struct page **pages, size_t maxsize, unsigned maxpages,
1519d879cb83SAl Viro 		   size_t *start)
1520d879cb83SAl Viro {
15213d671ca6SAl Viro 	size_t len;
15223d671ca6SAl Viro 	int n, res;
15233d671ca6SAl Viro 
1524d879cb83SAl Viro 	if (maxsize > i->count)
1525d879cb83SAl Viro 		maxsize = i->count;
15263d671ca6SAl Viro 	if (!maxsize)
15273d671ca6SAl Viro 		return 0;
1528d879cb83SAl Viro 
15293d671ca6SAl Viro 	if (likely(iter_is_iovec(i))) {
15303d671ca6SAl Viro 		unsigned long addr;
15319ea9ce04SDavid Howells 
15323d671ca6SAl Viro 		addr = first_iovec_segment(i, &len, start, maxsize, maxpages);
1533d879cb83SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
153473b0140bSIra Weiny 		res = get_user_pages_fast(addr, n,
153573b0140bSIra Weiny 				iov_iter_rw(i) != WRITE ?  FOLL_WRITE : 0,
153673b0140bSIra Weiny 				pages);
1537814a6674SAndreas Gruenbacher 		if (unlikely(res <= 0))
1538d879cb83SAl Viro 			return res;
1539d879cb83SAl Viro 		return (res == n ? len : res * PAGE_SIZE) - *start;
15403d671ca6SAl Viro 	}
15413d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
15423d671ca6SAl Viro 		struct page *page;
15433d671ca6SAl Viro 
15443d671ca6SAl Viro 		page = first_bvec_segment(i, &len, start, maxsize, maxpages);
15453d671ca6SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
15463d671ca6SAl Viro 		while (n--)
15473d671ca6SAl Viro 			get_page(*pages++ = page++);
15483d671ca6SAl Viro 		return len - *start;
15493d671ca6SAl Viro 	}
15503d671ca6SAl Viro 	if (iov_iter_is_pipe(i))
15513d671ca6SAl Viro 		return pipe_get_pages(i, pages, maxsize, maxpages, start);
15523d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
15533d671ca6SAl Viro 		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1554d879cb83SAl Viro 	return -EFAULT;
1555d879cb83SAl Viro }
1556d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_get_pages);
1557d879cb83SAl Viro 
1558d879cb83SAl Viro static struct page **get_pages_array(size_t n)
1559d879cb83SAl Viro {
1560752ade68SMichal Hocko 	return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
1561d879cb83SAl Viro }
1562d879cb83SAl Viro 
1563241699cdSAl Viro static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1564241699cdSAl Viro 		   struct page ***pages, size_t maxsize,
1565241699cdSAl Viro 		   size_t *start)
1566241699cdSAl Viro {
1567241699cdSAl Viro 	struct page **p;
15688cefc107SDavid Howells 	unsigned int iter_head, npages;
1569d7760d63SIlya Dryomov 	ssize_t n;
1570241699cdSAl Viro 
1571241699cdSAl Viro 	if (!sanity(i))
1572241699cdSAl Viro 		return -EFAULT;
1573241699cdSAl Viro 
15748cefc107SDavid Howells 	data_start(i, &iter_head, start);
15758cefc107SDavid Howells 	/* Amount of free space: some of this one + all after this one */
15768cefc107SDavid Howells 	npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1577241699cdSAl Viro 	n = npages * PAGE_SIZE - *start;
1578241699cdSAl Viro 	if (maxsize > n)
1579241699cdSAl Viro 		maxsize = n;
1580241699cdSAl Viro 	else
1581241699cdSAl Viro 		npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1582241699cdSAl Viro 	p = get_pages_array(npages);
1583241699cdSAl Viro 	if (!p)
1584241699cdSAl Viro 		return -ENOMEM;
15858cefc107SDavid Howells 	n = __pipe_get_pages(i, maxsize, p, iter_head, start);
1586241699cdSAl Viro 	if (n > 0)
1587241699cdSAl Viro 		*pages = p;
1588241699cdSAl Viro 	else
1589241699cdSAl Viro 		kvfree(p);
1590241699cdSAl Viro 	return n;
1591241699cdSAl Viro }
1592241699cdSAl Viro 
15937ff50620SDavid Howells static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
15947ff50620SDavid Howells 					   struct page ***pages, size_t maxsize,
15957ff50620SDavid Howells 					   size_t *_start_offset)
15967ff50620SDavid Howells {
15977ff50620SDavid Howells 	struct page **p;
15987ff50620SDavid Howells 	unsigned nr, offset;
15997ff50620SDavid Howells 	pgoff_t index, count;
16007ff50620SDavid Howells 	size_t size = maxsize, actual;
16017ff50620SDavid Howells 	loff_t pos;
16027ff50620SDavid Howells 
16037ff50620SDavid Howells 	if (!size)
16047ff50620SDavid Howells 		return 0;
16057ff50620SDavid Howells 
16067ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
16077ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
16087ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
16097ff50620SDavid Howells 	*_start_offset = offset;
16107ff50620SDavid Howells 
16117ff50620SDavid Howells 	count = 1;
16127ff50620SDavid Howells 	if (size > PAGE_SIZE - offset) {
16137ff50620SDavid Howells 		size -= PAGE_SIZE - offset;
16147ff50620SDavid Howells 		count += size >> PAGE_SHIFT;
16157ff50620SDavid Howells 		size &= ~PAGE_MASK;
16167ff50620SDavid Howells 		if (size)
16177ff50620SDavid Howells 			count++;
16187ff50620SDavid Howells 	}
16197ff50620SDavid Howells 
16207ff50620SDavid Howells 	p = get_pages_array(count);
16217ff50620SDavid Howells 	if (!p)
16227ff50620SDavid Howells 		return -ENOMEM;
16237ff50620SDavid Howells 	*pages = p;
16247ff50620SDavid Howells 
16257ff50620SDavid Howells 	nr = iter_xarray_populate_pages(p, i->xarray, index, count);
16267ff50620SDavid Howells 	if (nr == 0)
16277ff50620SDavid Howells 		return 0;
16287ff50620SDavid Howells 
16297ff50620SDavid Howells 	actual = PAGE_SIZE * nr;
16307ff50620SDavid Howells 	actual -= offset;
16317ff50620SDavid Howells 	if (nr == count && size > 0) {
16327ff50620SDavid Howells 		unsigned last_offset = (nr > 1) ? 0 : offset;
16337ff50620SDavid Howells 		actual -= PAGE_SIZE - (last_offset + size);
16347ff50620SDavid Howells 	}
16357ff50620SDavid Howells 	return actual;
16367ff50620SDavid Howells }
16377ff50620SDavid Howells 
1638d879cb83SAl Viro ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1639d879cb83SAl Viro 		   struct page ***pages, size_t maxsize,
1640d879cb83SAl Viro 		   size_t *start)
1641d879cb83SAl Viro {
1642d879cb83SAl Viro 	struct page **p;
16433d671ca6SAl Viro 	size_t len;
16443d671ca6SAl Viro 	int n, res;
1645d879cb83SAl Viro 
1646d879cb83SAl Viro 	if (maxsize > i->count)
1647d879cb83SAl Viro 		maxsize = i->count;
16483d671ca6SAl Viro 	if (!maxsize)
16493d671ca6SAl Viro 		return 0;
1650d879cb83SAl Viro 
16513d671ca6SAl Viro 	if (likely(iter_is_iovec(i))) {
16523d671ca6SAl Viro 		unsigned long addr;
16539ea9ce04SDavid Howells 
16543d671ca6SAl Viro 		addr = first_iovec_segment(i, &len, start, maxsize, ~0U);
1655d879cb83SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
1656d879cb83SAl Viro 		p = get_pages_array(n);
1657d879cb83SAl Viro 		if (!p)
1658d879cb83SAl Viro 			return -ENOMEM;
165973b0140bSIra Weiny 		res = get_user_pages_fast(addr, n,
166073b0140bSIra Weiny 				iov_iter_rw(i) != WRITE ?  FOLL_WRITE : 0, p);
1661814a6674SAndreas Gruenbacher 		if (unlikely(res <= 0)) {
1662d879cb83SAl Viro 			kvfree(p);
1663814a6674SAndreas Gruenbacher 			*pages = NULL;
1664d879cb83SAl Viro 			return res;
1665d879cb83SAl Viro 		}
1666d879cb83SAl Viro 		*pages = p;
1667d879cb83SAl Viro 		return (res == n ? len : res * PAGE_SIZE) - *start;
16683d671ca6SAl Viro 	}
16693d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
16703d671ca6SAl Viro 		struct page *page;
16713d671ca6SAl Viro 
16723d671ca6SAl Viro 		page = first_bvec_segment(i, &len, start, maxsize, ~0U);
16733d671ca6SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
16743d671ca6SAl Viro 		*pages = p = get_pages_array(n);
1675d879cb83SAl Viro 		if (!p)
1676d879cb83SAl Viro 			return -ENOMEM;
16773d671ca6SAl Viro 		while (n--)
16783d671ca6SAl Viro 			get_page(*p++ = page++);
16793d671ca6SAl Viro 		return len - *start;
16803d671ca6SAl Viro 	}
16813d671ca6SAl Viro 	if (iov_iter_is_pipe(i))
16823d671ca6SAl Viro 		return pipe_get_pages_alloc(i, pages, maxsize, start);
16833d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
16843d671ca6SAl Viro 		return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
1685d879cb83SAl Viro 	return -EFAULT;
1686d879cb83SAl Viro }
1687d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1688d879cb83SAl Viro 
1689d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1690d879cb83SAl Viro 			       struct iov_iter *i)
1691d879cb83SAl Viro {
1692d879cb83SAl Viro 	__wsum sum, next;
1693d879cb83SAl Viro 	sum = *csum;
16949ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1695241699cdSAl Viro 		WARN_ON(1);
1696241699cdSAl Viro 		return 0;
1697241699cdSAl Viro 	}
16987baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off, ({
16997baa5099SAl Viro 		next = csum_and_copy_from_user(base, addr + off, len);
1700d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
17017baa5099SAl Viro 		next ? 0 : len;
1702d879cb83SAl Viro 	}), ({
17037baa5099SAl Viro 		sum = csum_and_memcpy(addr + off, base, len, sum, off);
1704d879cb83SAl Viro 	})
1705d879cb83SAl Viro 	)
1706d879cb83SAl Viro 	*csum = sum;
1707d879cb83SAl Viro 	return bytes;
1708d879cb83SAl Viro }
1709d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter);
1710d879cb83SAl Viro 
171152cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1712d879cb83SAl Viro 			     struct iov_iter *i)
1713d879cb83SAl Viro {
171452cbd23aSWillem de Bruijn 	struct csum_state *csstate = _csstate;
1715d879cb83SAl Viro 	__wsum sum, next;
171678e1f386SAl Viro 
171778e1f386SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
1718241699cdSAl Viro 		WARN_ON(1);	/* for now */
1719241699cdSAl Viro 		return 0;
1720241699cdSAl Viro 	}
17216852df12SAl Viro 
17226852df12SAl Viro 	sum = csum_shift(csstate->csum, csstate->off);
17236852df12SAl Viro 	if (unlikely(iov_iter_is_pipe(i)))
17246852df12SAl Viro 		bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
17256852df12SAl Viro 	else iterate_and_advance(i, bytes, base, len, off, ({
17267baa5099SAl Viro 		next = csum_and_copy_to_user(addr + off, base, len);
1727d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
17287baa5099SAl Viro 		next ? 0 : len;
1729d879cb83SAl Viro 	}), ({
17307baa5099SAl Viro 		sum = csum_and_memcpy(base, addr + off, len, sum, off);
1731d879cb83SAl Viro 	})
1732d879cb83SAl Viro 	)
1733594e450bSAl Viro 	csstate->csum = csum_shift(sum, csstate->off);
1734594e450bSAl Viro 	csstate->off += bytes;
1735d879cb83SAl Viro 	return bytes;
1736d879cb83SAl Viro }
1737d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter);
1738d879cb83SAl Viro 
1739d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1740d05f4435SSagi Grimberg 		struct iov_iter *i)
1741d05f4435SSagi Grimberg {
17427999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH
1743d05f4435SSagi Grimberg 	struct ahash_request *hash = hashp;
1744d05f4435SSagi Grimberg 	struct scatterlist sg;
1745d05f4435SSagi Grimberg 	size_t copied;
1746d05f4435SSagi Grimberg 
1747d05f4435SSagi Grimberg 	copied = copy_to_iter(addr, bytes, i);
1748d05f4435SSagi Grimberg 	sg_init_one(&sg, addr, copied);
1749d05f4435SSagi Grimberg 	ahash_request_set_crypt(hash, &sg, NULL, copied);
1750d05f4435SSagi Grimberg 	crypto_ahash_update(hash);
1751d05f4435SSagi Grimberg 	return copied;
175227fad74aSYueHaibing #else
175327fad74aSYueHaibing 	return 0;
175427fad74aSYueHaibing #endif
1755d05f4435SSagi Grimberg }
1756d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter);
1757d05f4435SSagi Grimberg 
175866531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages)
1759d879cb83SAl Viro {
176066531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
176166531c65SAl Viro 	const struct iovec *p;
1762d879cb83SAl Viro 	int npages = 0;
1763d879cb83SAl Viro 
176466531c65SAl Viro 	for (p = i->iov; size; skip = 0, p++) {
176566531c65SAl Viro 		unsigned offs = offset_in_page(p->iov_base + skip);
176666531c65SAl Viro 		size_t len = min(p->iov_len - skip, size);
1767d879cb83SAl Viro 
176866531c65SAl Viro 		if (len) {
176966531c65SAl Viro 			size -= len;
177066531c65SAl Viro 			npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
177166531c65SAl Viro 			if (unlikely(npages > maxpages))
177266531c65SAl Viro 				return maxpages;
177366531c65SAl Viro 		}
177466531c65SAl Viro 	}
177566531c65SAl Viro 	return npages;
177666531c65SAl Viro }
177766531c65SAl Viro 
177866531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages)
177966531c65SAl Viro {
178066531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
178166531c65SAl Viro 	const struct bio_vec *p;
178266531c65SAl Viro 	int npages = 0;
178366531c65SAl Viro 
178466531c65SAl Viro 	for (p = i->bvec; size; skip = 0, p++) {
178566531c65SAl Viro 		unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
178666531c65SAl Viro 		size_t len = min(p->bv_len - skip, size);
178766531c65SAl Viro 
178866531c65SAl Viro 		size -= len;
178966531c65SAl Viro 		npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
179066531c65SAl Viro 		if (unlikely(npages > maxpages))
179166531c65SAl Viro 			return maxpages;
179266531c65SAl Viro 	}
179366531c65SAl Viro 	return npages;
179466531c65SAl Viro }
179566531c65SAl Viro 
179666531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages)
179766531c65SAl Viro {
179866531c65SAl Viro 	if (unlikely(!i->count))
179966531c65SAl Viro 		return 0;
180066531c65SAl Viro 	/* iovec and kvec have identical layouts */
180166531c65SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
180266531c65SAl Viro 		return iov_npages(i, maxpages);
180366531c65SAl Viro 	if (iov_iter_is_bvec(i))
180466531c65SAl Viro 		return bvec_npages(i, maxpages);
180566531c65SAl Viro 	if (iov_iter_is_pipe(i)) {
18068cefc107SDavid Howells 		unsigned int iter_head;
180766531c65SAl Viro 		int npages;
1808241699cdSAl Viro 		size_t off;
1809241699cdSAl Viro 
1810241699cdSAl Viro 		if (!sanity(i))
1811241699cdSAl Viro 			return 0;
1812241699cdSAl Viro 
18138cefc107SDavid Howells 		data_start(i, &iter_head, &off);
1814241699cdSAl Viro 		/* some of this one + all after this one */
181566531c65SAl Viro 		npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
181666531c65SAl Viro 		return min(npages, maxpages);
181766531c65SAl Viro 	}
181866531c65SAl Viro 	if (iov_iter_is_xarray(i)) {
1819e4f8df86SAl Viro 		unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1820e4f8df86SAl Viro 		int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
182166531c65SAl Viro 		return min(npages, maxpages);
182266531c65SAl Viro 	}
182366531c65SAl Viro 	return 0;
1824d879cb83SAl Viro }
1825d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages);
1826d879cb83SAl Viro 
1827d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1828d879cb83SAl Viro {
1829d879cb83SAl Viro 	*new = *old;
183000e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(new))) {
1831241699cdSAl Viro 		WARN_ON(1);
1832241699cdSAl Viro 		return NULL;
1833241699cdSAl Viro 	}
18347ff50620SDavid Howells 	if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
18359ea9ce04SDavid Howells 		return NULL;
183600e23707SDavid Howells 	if (iov_iter_is_bvec(new))
1837d879cb83SAl Viro 		return new->bvec = kmemdup(new->bvec,
1838d879cb83SAl Viro 				    new->nr_segs * sizeof(struct bio_vec),
1839d879cb83SAl Viro 				    flags);
1840d879cb83SAl Viro 	else
1841d879cb83SAl Viro 		/* iovec and kvec have identical layout */
1842d879cb83SAl Viro 		return new->iov = kmemdup(new->iov,
1843d879cb83SAl Viro 				   new->nr_segs * sizeof(struct iovec),
1844d879cb83SAl Viro 				   flags);
1845d879cb83SAl Viro }
1846d879cb83SAl Viro EXPORT_SYMBOL(dup_iter);
1847bc917be8SAl Viro 
1848bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov,
1849bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1850bfdc5970SChristoph Hellwig {
1851bfdc5970SChristoph Hellwig 	const struct compat_iovec __user *uiov =
1852bfdc5970SChristoph Hellwig 		(const struct compat_iovec __user *)uvec;
1853bfdc5970SChristoph Hellwig 	int ret = -EFAULT, i;
1854bfdc5970SChristoph Hellwig 
1855a959a978SChristoph Hellwig 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1856bfdc5970SChristoph Hellwig 		return -EFAULT;
1857bfdc5970SChristoph Hellwig 
1858bfdc5970SChristoph Hellwig 	for (i = 0; i < nr_segs; i++) {
1859bfdc5970SChristoph Hellwig 		compat_uptr_t buf;
1860bfdc5970SChristoph Hellwig 		compat_ssize_t len;
1861bfdc5970SChristoph Hellwig 
1862bfdc5970SChristoph Hellwig 		unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1863bfdc5970SChristoph Hellwig 		unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1864bfdc5970SChristoph Hellwig 
1865bfdc5970SChristoph Hellwig 		/* check for compat_size_t not fitting in compat_ssize_t .. */
1866bfdc5970SChristoph Hellwig 		if (len < 0) {
1867bfdc5970SChristoph Hellwig 			ret = -EINVAL;
1868bfdc5970SChristoph Hellwig 			goto uaccess_end;
1869bfdc5970SChristoph Hellwig 		}
1870bfdc5970SChristoph Hellwig 		iov[i].iov_base = compat_ptr(buf);
1871bfdc5970SChristoph Hellwig 		iov[i].iov_len = len;
1872bfdc5970SChristoph Hellwig 	}
1873bfdc5970SChristoph Hellwig 
1874bfdc5970SChristoph Hellwig 	ret = 0;
1875bfdc5970SChristoph Hellwig uaccess_end:
1876bfdc5970SChristoph Hellwig 	user_access_end();
1877bfdc5970SChristoph Hellwig 	return ret;
1878bfdc5970SChristoph Hellwig }
1879bfdc5970SChristoph Hellwig 
1880bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov,
1881bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1882fb041b59SDavid Laight {
1883fb041b59SDavid Laight 	unsigned long seg;
1884bfdc5970SChristoph Hellwig 
1885bfdc5970SChristoph Hellwig 	if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1886bfdc5970SChristoph Hellwig 		return -EFAULT;
1887bfdc5970SChristoph Hellwig 	for (seg = 0; seg < nr_segs; seg++) {
1888bfdc5970SChristoph Hellwig 		if ((ssize_t)iov[seg].iov_len < 0)
1889bfdc5970SChristoph Hellwig 			return -EINVAL;
1890bfdc5970SChristoph Hellwig 	}
1891bfdc5970SChristoph Hellwig 
1892bfdc5970SChristoph Hellwig 	return 0;
1893bfdc5970SChristoph Hellwig }
1894bfdc5970SChristoph Hellwig 
1895bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec,
1896bfdc5970SChristoph Hellwig 		unsigned long nr_segs, unsigned long fast_segs,
1897bfdc5970SChristoph Hellwig 		struct iovec *fast_iov, bool compat)
1898bfdc5970SChristoph Hellwig {
1899bfdc5970SChristoph Hellwig 	struct iovec *iov = fast_iov;
1900bfdc5970SChristoph Hellwig 	int ret;
1901fb041b59SDavid Laight 
1902fb041b59SDavid Laight 	/*
1903bfdc5970SChristoph Hellwig 	 * SuS says "The readv() function *may* fail if the iovcnt argument was
1904bfdc5970SChristoph Hellwig 	 * less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1905fb041b59SDavid Laight 	 * traditionally returned zero for zero segments, so...
1906fb041b59SDavid Laight 	 */
1907bfdc5970SChristoph Hellwig 	if (nr_segs == 0)
1908bfdc5970SChristoph Hellwig 		return iov;
1909bfdc5970SChristoph Hellwig 	if (nr_segs > UIO_MAXIOV)
1910bfdc5970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1911fb041b59SDavid Laight 	if (nr_segs > fast_segs) {
1912fb041b59SDavid Laight 		iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1913bfdc5970SChristoph Hellwig 		if (!iov)
1914bfdc5970SChristoph Hellwig 			return ERR_PTR(-ENOMEM);
1915fb041b59SDavid Laight 	}
1916bfdc5970SChristoph Hellwig 
1917bfdc5970SChristoph Hellwig 	if (compat)
1918bfdc5970SChristoph Hellwig 		ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1919bfdc5970SChristoph Hellwig 	else
1920bfdc5970SChristoph Hellwig 		ret = copy_iovec_from_user(iov, uvec, nr_segs);
1921bfdc5970SChristoph Hellwig 	if (ret) {
1922bfdc5970SChristoph Hellwig 		if (iov != fast_iov)
1923bfdc5970SChristoph Hellwig 			kfree(iov);
1924bfdc5970SChristoph Hellwig 		return ERR_PTR(ret);
1925fb041b59SDavid Laight 	}
1926bfdc5970SChristoph Hellwig 
1927bfdc5970SChristoph Hellwig 	return iov;
1928bfdc5970SChristoph Hellwig }
1929bfdc5970SChristoph Hellwig 
1930bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1931bfdc5970SChristoph Hellwig 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1932bfdc5970SChristoph Hellwig 		 struct iov_iter *i, bool compat)
1933bfdc5970SChristoph Hellwig {
1934bfdc5970SChristoph Hellwig 	ssize_t total_len = 0;
1935bfdc5970SChristoph Hellwig 	unsigned long seg;
1936bfdc5970SChristoph Hellwig 	struct iovec *iov;
1937bfdc5970SChristoph Hellwig 
1938bfdc5970SChristoph Hellwig 	iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1939bfdc5970SChristoph Hellwig 	if (IS_ERR(iov)) {
1940bfdc5970SChristoph Hellwig 		*iovp = NULL;
1941bfdc5970SChristoph Hellwig 		return PTR_ERR(iov);
1942fb041b59SDavid Laight 	}
1943fb041b59SDavid Laight 
1944fb041b59SDavid Laight 	/*
1945bfdc5970SChristoph Hellwig 	 * According to the Single Unix Specification we should return EINVAL if
1946bfdc5970SChristoph Hellwig 	 * an element length is < 0 when cast to ssize_t or if the total length
1947bfdc5970SChristoph Hellwig 	 * would overflow the ssize_t return value of the system call.
1948fb041b59SDavid Laight 	 *
1949fb041b59SDavid Laight 	 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1950fb041b59SDavid Laight 	 * overflow case.
1951fb041b59SDavid Laight 	 */
1952fb041b59SDavid Laight 	for (seg = 0; seg < nr_segs; seg++) {
1953fb041b59SDavid Laight 		ssize_t len = (ssize_t)iov[seg].iov_len;
1954fb041b59SDavid Laight 
1955bfdc5970SChristoph Hellwig 		if (!access_ok(iov[seg].iov_base, len)) {
1956bfdc5970SChristoph Hellwig 			if (iov != *iovp)
1957bfdc5970SChristoph Hellwig 				kfree(iov);
1958bfdc5970SChristoph Hellwig 			*iovp = NULL;
1959bfdc5970SChristoph Hellwig 			return -EFAULT;
1960fb041b59SDavid Laight 		}
1961bfdc5970SChristoph Hellwig 
1962bfdc5970SChristoph Hellwig 		if (len > MAX_RW_COUNT - total_len) {
1963bfdc5970SChristoph Hellwig 			len = MAX_RW_COUNT - total_len;
1964fb041b59SDavid Laight 			iov[seg].iov_len = len;
1965fb041b59SDavid Laight 		}
1966bfdc5970SChristoph Hellwig 		total_len += len;
1967fb041b59SDavid Laight 	}
1968bfdc5970SChristoph Hellwig 
1969bfdc5970SChristoph Hellwig 	iov_iter_init(i, type, iov, nr_segs, total_len);
1970bfdc5970SChristoph Hellwig 	if (iov == *iovp)
1971bfdc5970SChristoph Hellwig 		*iovp = NULL;
1972bfdc5970SChristoph Hellwig 	else
1973bfdc5970SChristoph Hellwig 		*iovp = iov;
1974bfdc5970SChristoph Hellwig 	return total_len;
1975fb041b59SDavid Laight }
1976fb041b59SDavid Laight 
1977ffecee4fSVegard Nossum /**
1978ffecee4fSVegard Nossum  * import_iovec() - Copy an array of &struct iovec from userspace
1979ffecee4fSVegard Nossum  *     into the kernel, check that it is valid, and initialize a new
1980ffecee4fSVegard Nossum  *     &struct iov_iter iterator to access it.
1981ffecee4fSVegard Nossum  *
1982ffecee4fSVegard Nossum  * @type: One of %READ or %WRITE.
1983bfdc5970SChristoph Hellwig  * @uvec: Pointer to the userspace array.
1984ffecee4fSVegard Nossum  * @nr_segs: Number of elements in userspace array.
1985ffecee4fSVegard Nossum  * @fast_segs: Number of elements in @iov.
1986bfdc5970SChristoph Hellwig  * @iovp: (input and output parameter) Pointer to pointer to (usually small
1987ffecee4fSVegard Nossum  *     on-stack) kernel array.
1988ffecee4fSVegard Nossum  * @i: Pointer to iterator that will be initialized on success.
1989ffecee4fSVegard Nossum  *
1990ffecee4fSVegard Nossum  * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1991ffecee4fSVegard Nossum  * then this function places %NULL in *@iov on return. Otherwise, a new
1992ffecee4fSVegard Nossum  * array will be allocated and the result placed in *@iov. This means that
1993ffecee4fSVegard Nossum  * the caller may call kfree() on *@iov regardless of whether the small
1994ffecee4fSVegard Nossum  * on-stack array was used or not (and regardless of whether this function
1995ffecee4fSVegard Nossum  * returns an error or not).
1996ffecee4fSVegard Nossum  *
199787e5e6daSJens Axboe  * Return: Negative error code on error, bytes imported on success
1998ffecee4fSVegard Nossum  */
1999bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec,
2000bc917be8SAl Viro 		 unsigned nr_segs, unsigned fast_segs,
2001bfdc5970SChristoph Hellwig 		 struct iovec **iovp, struct iov_iter *i)
2002bc917be8SAl Viro {
200389cd35c5SChristoph Hellwig 	return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
200489cd35c5SChristoph Hellwig 			      in_compat_syscall());
2005bc917be8SAl Viro }
2006bc917be8SAl Viro EXPORT_SYMBOL(import_iovec);
2007bc917be8SAl Viro 
2008bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len,
2009bc917be8SAl Viro 		 struct iovec *iov, struct iov_iter *i)
2010bc917be8SAl Viro {
2011bc917be8SAl Viro 	if (len > MAX_RW_COUNT)
2012bc917be8SAl Viro 		len = MAX_RW_COUNT;
201396d4f267SLinus Torvalds 	if (unlikely(!access_ok(buf, len)))
2014bc917be8SAl Viro 		return -EFAULT;
2015bc917be8SAl Viro 
2016bc917be8SAl Viro 	iov->iov_base = buf;
2017bc917be8SAl Viro 	iov->iov_len = len;
2018bc917be8SAl Viro 	iov_iter_init(i, rw, iov, 1, len);
2019bc917be8SAl Viro 	return 0;
2020bc917be8SAl Viro }
2021e1267585SAl Viro EXPORT_SYMBOL(import_single_range);
20228fb0f47aSJens Axboe 
20238fb0f47aSJens Axboe /**
20248fb0f47aSJens Axboe  * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
20258fb0f47aSJens Axboe  *     iov_iter_save_state() was called.
20268fb0f47aSJens Axboe  *
20278fb0f47aSJens Axboe  * @i: &struct iov_iter to restore
20288fb0f47aSJens Axboe  * @state: state to restore from
20298fb0f47aSJens Axboe  *
20308fb0f47aSJens Axboe  * Used after iov_iter_save_state() to bring restore @i, if operations may
20318fb0f47aSJens Axboe  * have advanced it.
20328fb0f47aSJens Axboe  *
20338fb0f47aSJens Axboe  * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
20348fb0f47aSJens Axboe  */
20358fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
20368fb0f47aSJens Axboe {
20378fb0f47aSJens Axboe 	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
20388fb0f47aSJens Axboe 			 !iov_iter_is_kvec(i))
20398fb0f47aSJens Axboe 		return;
20408fb0f47aSJens Axboe 	i->iov_offset = state->iov_offset;
20418fb0f47aSJens Axboe 	i->count = state->count;
20428fb0f47aSJens Axboe 	/*
20438fb0f47aSJens Axboe 	 * For the *vec iters, nr_segs + iov is constant - if we increment
20448fb0f47aSJens Axboe 	 * the vec, then we also decrement the nr_segs count. Hence we don't
20458fb0f47aSJens Axboe 	 * need to track both of these, just one is enough and we can deduct
20468fb0f47aSJens Axboe 	 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
20478fb0f47aSJens Axboe 	 * size, so we can just increment the iov pointer as they are unionzed.
20488fb0f47aSJens Axboe 	 * ITER_BVEC _may_ be the same size on some archs, but on others it is
20498fb0f47aSJens Axboe 	 * not. Be safe and handle it separately.
20508fb0f47aSJens Axboe 	 */
20518fb0f47aSJens Axboe 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
20528fb0f47aSJens Axboe 	if (iov_iter_is_bvec(i))
20538fb0f47aSJens Axboe 		i->bvec -= state->nr_segs - i->nr_segs;
20548fb0f47aSJens Axboe 	else
20558fb0f47aSJens Axboe 		i->iov -= state->nr_segs - i->nr_segs;
20568fb0f47aSJens Axboe 	i->nr_segs = state->nr_segs;
20578fb0f47aSJens Axboe }
2058