xref: /openbmc/linux/lib/iov_iter.c (revision a6294593)
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 /*
433*a6294593SAndreas Gruenbacher  * fault_in_iov_iter_readable - fault in iov iterator for reading
434*a6294593SAndreas Gruenbacher  * @i: iterator
435*a6294593SAndreas Gruenbacher  * @size: maximum length
436171a0203SAnton Altaparmakov  *
437*a6294593SAndreas Gruenbacher  * Fault in one or more iovecs of the given iov_iter, to a maximum length of
438*a6294593SAndreas Gruenbacher  * @size.  For each iovec, fault in each page that constitutes the iovec.
439*a6294593SAndreas Gruenbacher  *
440*a6294593SAndreas Gruenbacher  * Returns the number of bytes not faulted in (like copy_to_user() and
441*a6294593SAndreas Gruenbacher  * copy_from_user()).
442*a6294593SAndreas Gruenbacher  *
443*a6294593SAndreas Gruenbacher  * Always returns 0 for non-userspace iterators.
444171a0203SAnton Altaparmakov  */
445*a6294593SAndreas 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)) {
448*a6294593SAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
4498409a0d2SAl Viro 		const struct iovec *p;
4508409a0d2SAl Viro 		size_t skip;
4518409a0d2SAl Viro 
452*a6294593SAndreas Gruenbacher 		size -= count;
453*a6294593SAndreas Gruenbacher 		for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
454*a6294593SAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
455*a6294593SAndreas Gruenbacher 			size_t ret;
4568409a0d2SAl Viro 
4578409a0d2SAl Viro 			if (unlikely(!len))
4588409a0d2SAl Viro 				continue;
459*a6294593SAndreas Gruenbacher 			ret = fault_in_readable(p->iov_base + skip, len);
460*a6294593SAndreas Gruenbacher 			count -= len - ret;
461*a6294593SAndreas Gruenbacher 			if (ret)
462*a6294593SAndreas Gruenbacher 				break;
4638409a0d2SAl Viro 		}
464*a6294593SAndreas Gruenbacher 		return count + size;
465171a0203SAnton Altaparmakov 	}
466171a0203SAnton Altaparmakov 	return 0;
467171a0203SAnton Altaparmakov }
468*a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable);
469171a0203SAnton Altaparmakov 
470aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction,
471d879cb83SAl Viro 			const struct iovec *iov, unsigned long nr_segs,
472d879cb83SAl Viro 			size_t count)
473d879cb83SAl Viro {
474aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
4758cd54c1cSAl Viro 	*i = (struct iov_iter) {
4768cd54c1cSAl Viro 		.iter_type = ITER_IOVEC,
4778cd54c1cSAl Viro 		.data_source = direction,
4788cd54c1cSAl Viro 		.iov = iov,
4798cd54c1cSAl Viro 		.nr_segs = nr_segs,
4808cd54c1cSAl Viro 		.iov_offset = 0,
4818cd54c1cSAl Viro 		.count = count
4828cd54c1cSAl Viro 	};
483d879cb83SAl Viro }
484d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init);
485d879cb83SAl Viro 
486241699cdSAl Viro static inline bool allocated(struct pipe_buffer *buf)
487241699cdSAl Viro {
488241699cdSAl Viro 	return buf->ops == &default_pipe_buf_ops;
489241699cdSAl Viro }
490241699cdSAl Viro 
4918cefc107SDavid Howells static inline void data_start(const struct iov_iter *i,
4928cefc107SDavid Howells 			      unsigned int *iter_headp, size_t *offp)
493241699cdSAl Viro {
4948cefc107SDavid Howells 	unsigned int p_mask = i->pipe->ring_size - 1;
4958cefc107SDavid Howells 	unsigned int iter_head = i->head;
496241699cdSAl Viro 	size_t off = i->iov_offset;
4978cefc107SDavid Howells 
4988cefc107SDavid Howells 	if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
4998cefc107SDavid Howells 		    off == PAGE_SIZE)) {
5008cefc107SDavid Howells 		iter_head++;
501241699cdSAl Viro 		off = 0;
502241699cdSAl Viro 	}
5038cefc107SDavid Howells 	*iter_headp = iter_head;
504241699cdSAl Viro 	*offp = off;
505241699cdSAl Viro }
506241699cdSAl Viro 
507241699cdSAl Viro static size_t push_pipe(struct iov_iter *i, size_t size,
5088cefc107SDavid Howells 			int *iter_headp, size_t *offp)
509241699cdSAl Viro {
510241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
5118cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
5128cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
5138cefc107SDavid Howells 	unsigned int iter_head;
514241699cdSAl Viro 	size_t off;
515241699cdSAl Viro 	ssize_t left;
516241699cdSAl Viro 
517241699cdSAl Viro 	if (unlikely(size > i->count))
518241699cdSAl Viro 		size = i->count;
519241699cdSAl Viro 	if (unlikely(!size))
520241699cdSAl Viro 		return 0;
521241699cdSAl Viro 
522241699cdSAl Viro 	left = size;
5238cefc107SDavid Howells 	data_start(i, &iter_head, &off);
5248cefc107SDavid Howells 	*iter_headp = iter_head;
525241699cdSAl Viro 	*offp = off;
526241699cdSAl Viro 	if (off) {
527241699cdSAl Viro 		left -= PAGE_SIZE - off;
528241699cdSAl Viro 		if (left <= 0) {
5298cefc107SDavid Howells 			pipe->bufs[iter_head & p_mask].len += size;
530241699cdSAl Viro 			return size;
531241699cdSAl Viro 		}
5328cefc107SDavid Howells 		pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
5338cefc107SDavid Howells 		iter_head++;
534241699cdSAl Viro 	}
5356718b6f8SDavid Howells 	while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
5368cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
537241699cdSAl Viro 		struct page *page = alloc_page(GFP_USER);
538241699cdSAl Viro 		if (!page)
539241699cdSAl Viro 			break;
5408cefc107SDavid Howells 
5418cefc107SDavid Howells 		buf->ops = &default_pipe_buf_ops;
5428cefc107SDavid Howells 		buf->page = page;
5438cefc107SDavid Howells 		buf->offset = 0;
5448cefc107SDavid Howells 		buf->len = min_t(ssize_t, left, PAGE_SIZE);
5458cefc107SDavid Howells 		left -= buf->len;
5468cefc107SDavid Howells 		iter_head++;
5478cefc107SDavid Howells 		pipe->head = iter_head;
5488cefc107SDavid Howells 
5498cefc107SDavid Howells 		if (left == 0)
550241699cdSAl Viro 			return size;
551241699cdSAl Viro 	}
552241699cdSAl Viro 	return size - left;
553241699cdSAl Viro }
554241699cdSAl Viro 
555241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
556241699cdSAl Viro 				struct iov_iter *i)
557241699cdSAl Viro {
558241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
5598cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
5608cefc107SDavid Howells 	unsigned int i_head;
561241699cdSAl Viro 	size_t n, off;
562241699cdSAl Viro 
563241699cdSAl Viro 	if (!sanity(i))
564241699cdSAl Viro 		return 0;
565241699cdSAl Viro 
5668cefc107SDavid Howells 	bytes = n = push_pipe(i, bytes, &i_head, &off);
567241699cdSAl Viro 	if (unlikely(!n))
568241699cdSAl Viro 		return 0;
5698cefc107SDavid Howells 	do {
570241699cdSAl Viro 		size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
5718cefc107SDavid Howells 		memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
5728cefc107SDavid Howells 		i->head = i_head;
573241699cdSAl Viro 		i->iov_offset = off + chunk;
574241699cdSAl Viro 		n -= chunk;
575241699cdSAl Viro 		addr += chunk;
5768cefc107SDavid Howells 		off = 0;
5778cefc107SDavid Howells 		i_head++;
5788cefc107SDavid Howells 	} while (n);
579241699cdSAl Viro 	i->count -= bytes;
580241699cdSAl Viro 	return bytes;
581241699cdSAl Viro }
582241699cdSAl Viro 
583f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
584f9152895SAl Viro 			      __wsum sum, size_t off)
585f9152895SAl Viro {
586cc44c17bSAl Viro 	__wsum next = csum_partial_copy_nocheck(from, to, len);
587f9152895SAl Viro 	return csum_block_add(sum, next, off);
588f9152895SAl Viro }
589f9152895SAl Viro 
59078e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
5916852df12SAl Viro 					 struct iov_iter *i, __wsum *sump)
59278e1f386SAl Viro {
59378e1f386SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
5948cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
5956852df12SAl Viro 	__wsum sum = *sump;
5966852df12SAl Viro 	size_t off = 0;
5978cefc107SDavid Howells 	unsigned int i_head;
5986852df12SAl Viro 	size_t r;
59978e1f386SAl Viro 
60078e1f386SAl Viro 	if (!sanity(i))
60178e1f386SAl Viro 		return 0;
60278e1f386SAl Viro 
6036852df12SAl Viro 	bytes = push_pipe(i, bytes, &i_head, &r);
6046852df12SAl Viro 	while (bytes) {
6056852df12SAl Viro 		size_t chunk = min_t(size_t, bytes, PAGE_SIZE - r);
6062495bdccSAl Viro 		char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
6076852df12SAl Viro 		sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
6082495bdccSAl Viro 		kunmap_local(p);
6098cefc107SDavid Howells 		i->head = i_head;
61078e1f386SAl Viro 		i->iov_offset = r + chunk;
6116852df12SAl Viro 		bytes -= chunk;
61278e1f386SAl Viro 		off += chunk;
6138cefc107SDavid Howells 		r = 0;
6148cefc107SDavid Howells 		i_head++;
6156852df12SAl Viro 	}
6166852df12SAl Viro 	*sump = sum;
6176852df12SAl Viro 	i->count -= off;
6186852df12SAl Viro 	return off;
61978e1f386SAl Viro }
62078e1f386SAl Viro 
621aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
622d879cb83SAl Viro {
62300e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
624241699cdSAl Viro 		return copy_pipe_to_iter(addr, bytes, i);
62509fc68dcSAl Viro 	if (iter_is_iovec(i))
62609fc68dcSAl Viro 		might_fault();
6277baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6287baa5099SAl Viro 		copyout(base, addr + off, len),
6297baa5099SAl Viro 		memcpy(base, addr + off, len)
630d879cb83SAl Viro 	)
631d879cb83SAl Viro 
632d879cb83SAl Viro 	return bytes;
633d879cb83SAl Viro }
634aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter);
635d879cb83SAl Viro 
636ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC
637ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n)
6388780356eSDan Williams {
63996d4f267SLinus Torvalds 	if (access_ok(to, n)) {
640d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
641ec6347bbSDan Williams 		n = copy_mc_to_user((__force void *) to, from, n);
6428780356eSDan Williams 	}
6438780356eSDan Williams 	return n;
6448780356eSDan Williams }
6458780356eSDan Williams 
646ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
647ca146f6fSDan Williams 				struct iov_iter *i)
648ca146f6fSDan Williams {
649ca146f6fSDan Williams 	struct pipe_inode_info *pipe = i->pipe;
6508cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
6518cefc107SDavid Howells 	unsigned int i_head;
652ca146f6fSDan Williams 	size_t n, off, xfer = 0;
653ca146f6fSDan Williams 
654ca146f6fSDan Williams 	if (!sanity(i))
655ca146f6fSDan Williams 		return 0;
656ca146f6fSDan Williams 
6572a510a74SAl Viro 	n = push_pipe(i, bytes, &i_head, &off);
6582a510a74SAl Viro 	while (n) {
659ca146f6fSDan Williams 		size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
6602a510a74SAl Viro 		char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
661ca146f6fSDan Williams 		unsigned long rem;
6622a510a74SAl Viro 		rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
6632a510a74SAl Viro 		chunk -= rem;
6642a510a74SAl Viro 		kunmap_local(p);
6658cefc107SDavid Howells 		i->head = i_head;
6662a510a74SAl Viro 		i->iov_offset = off + chunk;
6672a510a74SAl Viro 		xfer += chunk;
668ca146f6fSDan Williams 		if (rem)
669ca146f6fSDan Williams 			break;
670ca146f6fSDan Williams 		n -= chunk;
6718cefc107SDavid Howells 		off = 0;
6728cefc107SDavid Howells 		i_head++;
6732a510a74SAl Viro 	}
674ca146f6fSDan Williams 	i->count -= xfer;
675ca146f6fSDan Williams 	return xfer;
676ca146f6fSDan Williams }
677ca146f6fSDan Williams 
678bf3eeb9bSDan Williams /**
679ec6347bbSDan Williams  * _copy_mc_to_iter - copy to iter with source memory error exception handling
680bf3eeb9bSDan Williams  * @addr: source kernel address
681bf3eeb9bSDan Williams  * @bytes: total transfer length
68244e55997SRandy Dunlap  * @i: destination iterator
683bf3eeb9bSDan Williams  *
684ec6347bbSDan Williams  * The pmem driver deploys this for the dax operation
685ec6347bbSDan Williams  * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
686ec6347bbSDan Williams  * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
687ec6347bbSDan Williams  * successfully copied.
688bf3eeb9bSDan Williams  *
689ec6347bbSDan Williams  * The main differences between this and typical _copy_to_iter().
690bf3eeb9bSDan Williams  *
691bf3eeb9bSDan Williams  * * Typical tail/residue handling after a fault retries the copy
692bf3eeb9bSDan Williams  *   byte-by-byte until the fault happens again. Re-triggering machine
693bf3eeb9bSDan Williams  *   checks is potentially fatal so the implementation uses source
694bf3eeb9bSDan Williams  *   alignment and poison alignment assumptions to avoid re-triggering
695bf3eeb9bSDan Williams  *   hardware exceptions.
696bf3eeb9bSDan Williams  *
697bf3eeb9bSDan Williams  * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
698bf3eeb9bSDan Williams  *   Compare to copy_to_iter() where only ITER_IOVEC attempts might return
699bf3eeb9bSDan Williams  *   a short copy.
70044e55997SRandy Dunlap  *
70144e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
702bf3eeb9bSDan Williams  */
703ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
7048780356eSDan Williams {
70500e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
706ec6347bbSDan Williams 		return copy_mc_pipe_to_iter(addr, bytes, i);
7078780356eSDan Williams 	if (iter_is_iovec(i))
7088780356eSDan Williams 		might_fault();
7097baa5099SAl Viro 	__iterate_and_advance(i, bytes, base, len, off,
7107baa5099SAl Viro 		copyout_mc(base, addr + off, len),
7117baa5099SAl Viro 		copy_mc_to_kernel(base, addr + off, len)
7128780356eSDan Williams 	)
7138780356eSDan Williams 
7148780356eSDan Williams 	return bytes;
7158780356eSDan Williams }
716ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
717ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */
7188780356eSDan Williams 
719aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
720d879cb83SAl Viro {
72100e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
722241699cdSAl Viro 		WARN_ON(1);
723241699cdSAl Viro 		return 0;
724241699cdSAl Viro 	}
72509fc68dcSAl Viro 	if (iter_is_iovec(i))
72609fc68dcSAl Viro 		might_fault();
7277baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
7287baa5099SAl Viro 		copyin(addr + off, base, len),
7297baa5099SAl Viro 		memcpy(addr + off, base, len)
730d879cb83SAl Viro 	)
731d879cb83SAl Viro 
732d879cb83SAl Viro 	return bytes;
733d879cb83SAl Viro }
734aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter);
735d879cb83SAl Viro 
736aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
737d879cb83SAl Viro {
73800e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
739241699cdSAl Viro 		WARN_ON(1);
740241699cdSAl Viro 		return 0;
741241699cdSAl Viro 	}
7427baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
7437baa5099SAl Viro 		__copy_from_user_inatomic_nocache(addr + off, base, len),
7447baa5099SAl Viro 		memcpy(addr + off, base, len)
745d879cb83SAl Viro 	)
746d879cb83SAl Viro 
747d879cb83SAl Viro 	return bytes;
748d879cb83SAl Viro }
749aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache);
750d879cb83SAl Viro 
7510aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
752abd08d7dSDan Williams /**
753abd08d7dSDan Williams  * _copy_from_iter_flushcache - write destination through cpu cache
754abd08d7dSDan Williams  * @addr: destination kernel address
755abd08d7dSDan Williams  * @bytes: total transfer length
75644e55997SRandy Dunlap  * @i: source iterator
757abd08d7dSDan Williams  *
758abd08d7dSDan Williams  * The pmem driver arranges for filesystem-dax to use this facility via
759abd08d7dSDan Williams  * dax_copy_from_iter() for ensuring that writes to persistent memory
760abd08d7dSDan Williams  * are flushed through the CPU cache. It is differentiated from
761abd08d7dSDan Williams  * _copy_from_iter_nocache() in that guarantees all data is flushed for
762abd08d7dSDan Williams  * all iterator types. The _copy_from_iter_nocache() only attempts to
763abd08d7dSDan Williams  * bypass the cache for the ITER_IOVEC case, and on some archs may use
764abd08d7dSDan Williams  * instructions that strand dirty-data in the cache.
76544e55997SRandy Dunlap  *
76644e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
767abd08d7dSDan Williams  */
7686a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
7690aed55afSDan Williams {
77000e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
7710aed55afSDan Williams 		WARN_ON(1);
7720aed55afSDan Williams 		return 0;
7730aed55afSDan Williams 	}
7747baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
7757baa5099SAl Viro 		__copy_from_user_flushcache(addr + off, base, len),
7767baa5099SAl Viro 		memcpy_flushcache(addr + off, base, len)
7770aed55afSDan Williams 	)
7780aed55afSDan Williams 
7790aed55afSDan Williams 	return bytes;
7800aed55afSDan Williams }
7816a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
7820aed55afSDan Williams #endif
7830aed55afSDan Williams 
78472e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
78572e809edSAl Viro {
7866daef95bSEric Dumazet 	struct page *head;
7876daef95bSEric Dumazet 	size_t v = n + offset;
7886daef95bSEric Dumazet 
7896daef95bSEric Dumazet 	/*
7906daef95bSEric Dumazet 	 * The general case needs to access the page order in order
7916daef95bSEric Dumazet 	 * to compute the page size.
7926daef95bSEric Dumazet 	 * However, we mostly deal with order-0 pages and thus can
7936daef95bSEric Dumazet 	 * avoid a possible cache line miss for requests that fit all
7946daef95bSEric Dumazet 	 * page orders.
7956daef95bSEric Dumazet 	 */
7966daef95bSEric Dumazet 	if (n <= v && v <= PAGE_SIZE)
7976daef95bSEric Dumazet 		return true;
7986daef95bSEric Dumazet 
7996daef95bSEric Dumazet 	head = compound_head(page);
8006daef95bSEric Dumazet 	v += (page - head) << PAGE_SHIFT;
801a90bcb86SPetar Penkov 
802a50b854eSMatthew Wilcox (Oracle) 	if (likely(n <= v && v <= (page_size(head))))
80372e809edSAl Viro 		return true;
80472e809edSAl Viro 	WARN_ON(1);
80572e809edSAl Viro 	return false;
80672e809edSAl Viro }
807cbbd26b8SAl Viro 
80808aa6479SAl Viro static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
809d879cb83SAl Viro 			 struct iov_iter *i)
810d879cb83SAl Viro {
81128f38db7SAl Viro 	if (likely(iter_is_iovec(i)))
81228f38db7SAl Viro 		return copy_page_to_iter_iovec(page, offset, bytes, i);
81328f38db7SAl Viro 	if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
814c1d4d6a9SAl Viro 		void *kaddr = kmap_local_page(page);
815c1d4d6a9SAl Viro 		size_t wanted = _copy_to_iter(kaddr + offset, bytes, i);
816c1d4d6a9SAl Viro 		kunmap_local(kaddr);
817d879cb83SAl Viro 		return wanted;
81828f38db7SAl Viro 	}
81928f38db7SAl Viro 	if (iov_iter_is_pipe(i))
82028f38db7SAl Viro 		return copy_page_to_iter_pipe(page, offset, bytes, i);
82128f38db7SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
822a506abc7SAl Viro 		if (unlikely(i->count < bytes))
823a506abc7SAl Viro 			bytes = i->count;
824a506abc7SAl Viro 		i->count -= bytes;
8259ea9ce04SDavid Howells 		return bytes;
82628f38db7SAl Viro 	}
82728f38db7SAl Viro 	WARN_ON(1);
82828f38db7SAl Viro 	return 0;
829d879cb83SAl Viro }
83008aa6479SAl Viro 
83108aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
83208aa6479SAl Viro 			 struct iov_iter *i)
83308aa6479SAl Viro {
83408aa6479SAl Viro 	size_t res = 0;
83508aa6479SAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes)))
83608aa6479SAl Viro 		return 0;
83708aa6479SAl Viro 	page += offset / PAGE_SIZE; // first subpage
83808aa6479SAl Viro 	offset %= PAGE_SIZE;
83908aa6479SAl Viro 	while (1) {
84008aa6479SAl Viro 		size_t n = __copy_page_to_iter(page, offset,
84108aa6479SAl Viro 				min(bytes, (size_t)PAGE_SIZE - offset), i);
84208aa6479SAl Viro 		res += n;
84308aa6479SAl Viro 		bytes -= n;
84408aa6479SAl Viro 		if (!bytes || !n)
84508aa6479SAl Viro 			break;
84608aa6479SAl Viro 		offset += n;
84708aa6479SAl Viro 		if (offset == PAGE_SIZE) {
84808aa6479SAl Viro 			page++;
84908aa6479SAl Viro 			offset = 0;
85008aa6479SAl Viro 		}
85108aa6479SAl Viro 	}
85208aa6479SAl Viro 	return res;
85308aa6479SAl Viro }
854d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter);
855d879cb83SAl Viro 
856d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
857d879cb83SAl Viro 			 struct iov_iter *i)
858d879cb83SAl Viro {
85972e809edSAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes)))
86072e809edSAl Viro 		return 0;
86128f38db7SAl Viro 	if (likely(iter_is_iovec(i)))
86228f38db7SAl Viro 		return copy_page_from_iter_iovec(page, offset, bytes, i);
86328f38db7SAl Viro 	if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
86455ca375cSAl Viro 		void *kaddr = kmap_local_page(page);
865aa28de27SAl Viro 		size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
86655ca375cSAl Viro 		kunmap_local(kaddr);
867d879cb83SAl Viro 		return wanted;
86828f38db7SAl Viro 	}
86928f38db7SAl Viro 	WARN_ON(1);
87028f38db7SAl Viro 	return 0;
871d879cb83SAl Viro }
872d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter);
873d879cb83SAl Viro 
874241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i)
875241699cdSAl Viro {
876241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
8778cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
8788cefc107SDavid Howells 	unsigned int i_head;
879241699cdSAl Viro 	size_t n, off;
880241699cdSAl Viro 
881241699cdSAl Viro 	if (!sanity(i))
882241699cdSAl Viro 		return 0;
883241699cdSAl Viro 
8848cefc107SDavid Howells 	bytes = n = push_pipe(i, bytes, &i_head, &off);
885241699cdSAl Viro 	if (unlikely(!n))
886241699cdSAl Viro 		return 0;
887241699cdSAl Viro 
8888cefc107SDavid Howells 	do {
889241699cdSAl Viro 		size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
890893839fdSAl Viro 		char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
891893839fdSAl Viro 		memset(p + off, 0, chunk);
892893839fdSAl Viro 		kunmap_local(p);
8938cefc107SDavid Howells 		i->head = i_head;
894241699cdSAl Viro 		i->iov_offset = off + chunk;
895241699cdSAl Viro 		n -= chunk;
8968cefc107SDavid Howells 		off = 0;
8978cefc107SDavid Howells 		i_head++;
8988cefc107SDavid Howells 	} while (n);
899241699cdSAl Viro 	i->count -= bytes;
900241699cdSAl Viro 	return bytes;
901241699cdSAl Viro }
902241699cdSAl Viro 
903d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
904d879cb83SAl Viro {
90500e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
906241699cdSAl Viro 		return pipe_zero(bytes, i);
9077baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, count,
9087baa5099SAl Viro 		clear_user(base, len),
9097baa5099SAl Viro 		memset(base, 0, len)
910d879cb83SAl Viro 	)
911d879cb83SAl Viro 
912d879cb83SAl Viro 	return bytes;
913d879cb83SAl Viro }
914d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero);
915d879cb83SAl Viro 
916f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
917f0b65f39SAl Viro 				  struct iov_iter *i)
918d879cb83SAl Viro {
919d879cb83SAl Viro 	char *kaddr = kmap_atomic(page), *p = kaddr + offset;
92072e809edSAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes))) {
92172e809edSAl Viro 		kunmap_atomic(kaddr);
92272e809edSAl Viro 		return 0;
92372e809edSAl Viro 	}
9249ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
925241699cdSAl Viro 		kunmap_atomic(kaddr);
926241699cdSAl Viro 		WARN_ON(1);
927241699cdSAl Viro 		return 0;
928241699cdSAl Viro 	}
9297baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
9307baa5099SAl Viro 		copyin(p + off, base, len),
9317baa5099SAl Viro 		memcpy(p + off, base, len)
932d879cb83SAl Viro 	)
933d879cb83SAl Viro 	kunmap_atomic(kaddr);
934d879cb83SAl Viro 	return bytes;
935d879cb83SAl Viro }
936f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic);
937d879cb83SAl Viro 
938b9dc6f65SAl Viro static inline void pipe_truncate(struct iov_iter *i)
939241699cdSAl Viro {
940241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
9418cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
9428cefc107SDavid Howells 	unsigned int p_head = pipe->head;
9438cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
9448cefc107SDavid Howells 
9458cefc107SDavid Howells 	if (!pipe_empty(p_head, p_tail)) {
9468cefc107SDavid Howells 		struct pipe_buffer *buf;
9478cefc107SDavid Howells 		unsigned int i_head = i->head;
948b9dc6f65SAl Viro 		size_t off = i->iov_offset;
9498cefc107SDavid Howells 
950b9dc6f65SAl Viro 		if (off) {
9518cefc107SDavid Howells 			buf = &pipe->bufs[i_head & p_mask];
9528cefc107SDavid Howells 			buf->len = off - buf->offset;
9538cefc107SDavid Howells 			i_head++;
954b9dc6f65SAl Viro 		}
9558cefc107SDavid Howells 		while (p_head != i_head) {
9568cefc107SDavid Howells 			p_head--;
9578cefc107SDavid Howells 			pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
958241699cdSAl Viro 		}
9598cefc107SDavid Howells 
9608cefc107SDavid Howells 		pipe->head = p_head;
961241699cdSAl Viro 	}
962b9dc6f65SAl Viro }
963b9dc6f65SAl Viro 
964b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size)
965b9dc6f65SAl Viro {
966b9dc6f65SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
967b9dc6f65SAl Viro 	if (size) {
968b9dc6f65SAl Viro 		struct pipe_buffer *buf;
9698cefc107SDavid Howells 		unsigned int p_mask = pipe->ring_size - 1;
9708cefc107SDavid Howells 		unsigned int i_head = i->head;
971b9dc6f65SAl Viro 		size_t off = i->iov_offset, left = size;
9728cefc107SDavid Howells 
973b9dc6f65SAl Viro 		if (off) /* make it relative to the beginning of buffer */
9748cefc107SDavid Howells 			left += off - pipe->bufs[i_head & p_mask].offset;
975b9dc6f65SAl Viro 		while (1) {
9768cefc107SDavid Howells 			buf = &pipe->bufs[i_head & p_mask];
977b9dc6f65SAl Viro 			if (left <= buf->len)
978b9dc6f65SAl Viro 				break;
979b9dc6f65SAl Viro 			left -= buf->len;
9808cefc107SDavid Howells 			i_head++;
981b9dc6f65SAl Viro 		}
9828cefc107SDavid Howells 		i->head = i_head;
983b9dc6f65SAl Viro 		i->iov_offset = buf->offset + left;
984b9dc6f65SAl Viro 	}
985b9dc6f65SAl Viro 	i->count -= size;
986b9dc6f65SAl Viro 	/* ... and discard everything past that point */
987b9dc6f65SAl Viro 	pipe_truncate(i);
988241699cdSAl Viro }
989241699cdSAl Viro 
99054c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
99154c8195bSPavel Begunkov {
99254c8195bSPavel Begunkov 	struct bvec_iter bi;
99354c8195bSPavel Begunkov 
99454c8195bSPavel Begunkov 	bi.bi_size = i->count;
99554c8195bSPavel Begunkov 	bi.bi_bvec_done = i->iov_offset;
99654c8195bSPavel Begunkov 	bi.bi_idx = 0;
99754c8195bSPavel Begunkov 	bvec_iter_advance(i->bvec, &bi, size);
99854c8195bSPavel Begunkov 
99954c8195bSPavel Begunkov 	i->bvec += bi.bi_idx;
100054c8195bSPavel Begunkov 	i->nr_segs -= bi.bi_idx;
100154c8195bSPavel Begunkov 	i->count = bi.bi_size;
100254c8195bSPavel Begunkov 	i->iov_offset = bi.bi_bvec_done;
100354c8195bSPavel Begunkov }
100454c8195bSPavel Begunkov 
1005185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
1006185ac4d4SAl Viro {
1007185ac4d4SAl Viro 	const struct iovec *iov, *end;
1008185ac4d4SAl Viro 
1009185ac4d4SAl Viro 	if (!i->count)
1010185ac4d4SAl Viro 		return;
1011185ac4d4SAl Viro 	i->count -= size;
1012185ac4d4SAl Viro 
1013185ac4d4SAl Viro 	size += i->iov_offset; // from beginning of current segment
1014185ac4d4SAl Viro 	for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
1015185ac4d4SAl Viro 		if (likely(size < iov->iov_len))
1016185ac4d4SAl Viro 			break;
1017185ac4d4SAl Viro 		size -= iov->iov_len;
1018185ac4d4SAl Viro 	}
1019185ac4d4SAl Viro 	i->iov_offset = size;
1020185ac4d4SAl Viro 	i->nr_segs -= iov - i->iov;
1021185ac4d4SAl Viro 	i->iov = iov;
1022185ac4d4SAl Viro }
1023185ac4d4SAl Viro 
1024d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size)
1025d879cb83SAl Viro {
10263b3fc051SAl Viro 	if (unlikely(i->count < size))
10273b3fc051SAl Viro 		size = i->count;
1028185ac4d4SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
1029185ac4d4SAl Viro 		/* iovec and kvec have identical layouts */
1030185ac4d4SAl Viro 		iov_iter_iovec_advance(i, size);
1031185ac4d4SAl Viro 	} else if (iov_iter_is_bvec(i)) {
1032185ac4d4SAl Viro 		iov_iter_bvec_advance(i, size);
1033185ac4d4SAl Viro 	} else if (iov_iter_is_pipe(i)) {
1034241699cdSAl Viro 		pipe_advance(i, size);
1035185ac4d4SAl Viro 	} else if (unlikely(iov_iter_is_xarray(i))) {
10367ff50620SDavid Howells 		i->iov_offset += size;
10377ff50620SDavid Howells 		i->count -= size;
1038185ac4d4SAl Viro 	} else if (iov_iter_is_discard(i)) {
1039185ac4d4SAl Viro 		i->count -= size;
10407ff50620SDavid Howells 	}
1041d879cb83SAl Viro }
1042d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance);
1043d879cb83SAl Viro 
104427c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll)
104527c0e374SAl Viro {
104627c0e374SAl Viro 	if (!unroll)
104727c0e374SAl Viro 		return;
10485b47d59aSAl Viro 	if (WARN_ON(unroll > MAX_RW_COUNT))
10495b47d59aSAl Viro 		return;
105027c0e374SAl Viro 	i->count += unroll;
105100e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
105227c0e374SAl Viro 		struct pipe_inode_info *pipe = i->pipe;
10538cefc107SDavid Howells 		unsigned int p_mask = pipe->ring_size - 1;
10548cefc107SDavid Howells 		unsigned int i_head = i->head;
105527c0e374SAl Viro 		size_t off = i->iov_offset;
105627c0e374SAl Viro 		while (1) {
10578cefc107SDavid Howells 			struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
10588cefc107SDavid Howells 			size_t n = off - b->offset;
105927c0e374SAl Viro 			if (unroll < n) {
10604fa55cefSAl Viro 				off -= unroll;
106127c0e374SAl Viro 				break;
106227c0e374SAl Viro 			}
106327c0e374SAl Viro 			unroll -= n;
10648cefc107SDavid Howells 			if (!unroll && i_head == i->start_head) {
106527c0e374SAl Viro 				off = 0;
106627c0e374SAl Viro 				break;
106727c0e374SAl Viro 			}
10688cefc107SDavid Howells 			i_head--;
10698cefc107SDavid Howells 			b = &pipe->bufs[i_head & p_mask];
10708cefc107SDavid Howells 			off = b->offset + b->len;
107127c0e374SAl Viro 		}
107227c0e374SAl Viro 		i->iov_offset = off;
10738cefc107SDavid Howells 		i->head = i_head;
107427c0e374SAl Viro 		pipe_truncate(i);
107527c0e374SAl Viro 		return;
107627c0e374SAl Viro 	}
10779ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_discard(i)))
10789ea9ce04SDavid Howells 		return;
107927c0e374SAl Viro 	if (unroll <= i->iov_offset) {
108027c0e374SAl Viro 		i->iov_offset -= unroll;
108127c0e374SAl Viro 		return;
108227c0e374SAl Viro 	}
108327c0e374SAl Viro 	unroll -= i->iov_offset;
10847ff50620SDavid Howells 	if (iov_iter_is_xarray(i)) {
10857ff50620SDavid Howells 		BUG(); /* We should never go beyond the start of the specified
10867ff50620SDavid Howells 			* range since we might then be straying into pages that
10877ff50620SDavid Howells 			* aren't pinned.
10887ff50620SDavid Howells 			*/
10897ff50620SDavid Howells 	} else if (iov_iter_is_bvec(i)) {
109027c0e374SAl Viro 		const struct bio_vec *bvec = i->bvec;
109127c0e374SAl Viro 		while (1) {
109227c0e374SAl Viro 			size_t n = (--bvec)->bv_len;
109327c0e374SAl Viro 			i->nr_segs++;
109427c0e374SAl Viro 			if (unroll <= n) {
109527c0e374SAl Viro 				i->bvec = bvec;
109627c0e374SAl Viro 				i->iov_offset = n - unroll;
109727c0e374SAl Viro 				return;
109827c0e374SAl Viro 			}
109927c0e374SAl Viro 			unroll -= n;
110027c0e374SAl Viro 		}
110127c0e374SAl Viro 	} else { /* same logics for iovec and kvec */
110227c0e374SAl Viro 		const struct iovec *iov = i->iov;
110327c0e374SAl Viro 		while (1) {
110427c0e374SAl Viro 			size_t n = (--iov)->iov_len;
110527c0e374SAl Viro 			i->nr_segs++;
110627c0e374SAl Viro 			if (unroll <= n) {
110727c0e374SAl Viro 				i->iov = iov;
110827c0e374SAl Viro 				i->iov_offset = n - unroll;
110927c0e374SAl Viro 				return;
111027c0e374SAl Viro 			}
111127c0e374SAl Viro 			unroll -= n;
111227c0e374SAl Viro 		}
111327c0e374SAl Viro 	}
111427c0e374SAl Viro }
111527c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert);
111627c0e374SAl Viro 
1117d879cb83SAl Viro /*
1118d879cb83SAl Viro  * Return the count of just the current iov_iter segment.
1119d879cb83SAl Viro  */
1120d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i)
1121d879cb83SAl Viro {
112228f38db7SAl Viro 	if (i->nr_segs > 1) {
112328f38db7SAl Viro 		if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
112428f38db7SAl Viro 			return min(i->count, i->iov->iov_len - i->iov_offset);
11257ff50620SDavid Howells 		if (iov_iter_is_bvec(i))
1126d879cb83SAl Viro 			return min(i->count, i->bvec->bv_len - i->iov_offset);
112728f38db7SAl Viro 	}
112828f38db7SAl Viro 	return i->count;
1129d879cb83SAl Viro }
1130d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count);
1131d879cb83SAl Viro 
1132aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
1133d879cb83SAl Viro 			const struct kvec *kvec, unsigned long nr_segs,
1134d879cb83SAl Viro 			size_t count)
1135d879cb83SAl Viro {
1136aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
11378cd54c1cSAl Viro 	*i = (struct iov_iter){
11388cd54c1cSAl Viro 		.iter_type = ITER_KVEC,
11398cd54c1cSAl Viro 		.data_source = direction,
11408cd54c1cSAl Viro 		.kvec = kvec,
11418cd54c1cSAl Viro 		.nr_segs = nr_segs,
11428cd54c1cSAl Viro 		.iov_offset = 0,
11438cd54c1cSAl Viro 		.count = count
11448cd54c1cSAl Viro 	};
1145d879cb83SAl Viro }
1146d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec);
1147d879cb83SAl Viro 
1148aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1149d879cb83SAl Viro 			const struct bio_vec *bvec, unsigned long nr_segs,
1150d879cb83SAl Viro 			size_t count)
1151d879cb83SAl Viro {
1152aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
11538cd54c1cSAl Viro 	*i = (struct iov_iter){
11548cd54c1cSAl Viro 		.iter_type = ITER_BVEC,
11558cd54c1cSAl Viro 		.data_source = direction,
11568cd54c1cSAl Viro 		.bvec = bvec,
11578cd54c1cSAl Viro 		.nr_segs = nr_segs,
11588cd54c1cSAl Viro 		.iov_offset = 0,
11598cd54c1cSAl Viro 		.count = count
11608cd54c1cSAl Viro 	};
1161d879cb83SAl Viro }
1162d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec);
1163d879cb83SAl Viro 
1164aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1165241699cdSAl Viro 			struct pipe_inode_info *pipe,
1166241699cdSAl Viro 			size_t count)
1167241699cdSAl Viro {
1168aa563d7bSDavid Howells 	BUG_ON(direction != READ);
11698cefc107SDavid Howells 	WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
11708cd54c1cSAl Viro 	*i = (struct iov_iter){
11718cd54c1cSAl Viro 		.iter_type = ITER_PIPE,
11728cd54c1cSAl Viro 		.data_source = false,
11738cd54c1cSAl Viro 		.pipe = pipe,
11748cd54c1cSAl Viro 		.head = pipe->head,
11758cd54c1cSAl Viro 		.start_head = pipe->head,
11768cd54c1cSAl Viro 		.iov_offset = 0,
11778cd54c1cSAl Viro 		.count = count
11788cd54c1cSAl Viro 	};
1179241699cdSAl Viro }
1180241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe);
1181241699cdSAl Viro 
11829ea9ce04SDavid Howells /**
11837ff50620SDavid Howells  * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
11847ff50620SDavid Howells  * @i: The iterator to initialise.
11857ff50620SDavid Howells  * @direction: The direction of the transfer.
11867ff50620SDavid Howells  * @xarray: The xarray to access.
11877ff50620SDavid Howells  * @start: The start file position.
11887ff50620SDavid Howells  * @count: The size of the I/O buffer in bytes.
11897ff50620SDavid Howells  *
11907ff50620SDavid Howells  * Set up an I/O iterator to either draw data out of the pages attached to an
11917ff50620SDavid Howells  * inode or to inject data into those pages.  The pages *must* be prevented
11927ff50620SDavid Howells  * from evaporation, either by taking a ref on them or locking them by the
11937ff50620SDavid Howells  * caller.
11947ff50620SDavid Howells  */
11957ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
11967ff50620SDavid Howells 		     struct xarray *xarray, loff_t start, size_t count)
11977ff50620SDavid Howells {
11987ff50620SDavid Howells 	BUG_ON(direction & ~1);
11998cd54c1cSAl Viro 	*i = (struct iov_iter) {
12008cd54c1cSAl Viro 		.iter_type = ITER_XARRAY,
12018cd54c1cSAl Viro 		.data_source = direction,
12028cd54c1cSAl Viro 		.xarray = xarray,
12038cd54c1cSAl Viro 		.xarray_start = start,
12048cd54c1cSAl Viro 		.count = count,
12058cd54c1cSAl Viro 		.iov_offset = 0
12068cd54c1cSAl Viro 	};
12077ff50620SDavid Howells }
12087ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray);
12097ff50620SDavid Howells 
12107ff50620SDavid Howells /**
12119ea9ce04SDavid Howells  * iov_iter_discard - Initialise an I/O iterator that discards data
12129ea9ce04SDavid Howells  * @i: The iterator to initialise.
12139ea9ce04SDavid Howells  * @direction: The direction of the transfer.
12149ea9ce04SDavid Howells  * @count: The size of the I/O buffer in bytes.
12159ea9ce04SDavid Howells  *
12169ea9ce04SDavid Howells  * Set up an I/O iterator that just discards everything that's written to it.
12179ea9ce04SDavid Howells  * It's only available as a READ iterator.
12189ea9ce04SDavid Howells  */
12199ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
12209ea9ce04SDavid Howells {
12219ea9ce04SDavid Howells 	BUG_ON(direction != READ);
12228cd54c1cSAl Viro 	*i = (struct iov_iter){
12238cd54c1cSAl Viro 		.iter_type = ITER_DISCARD,
12248cd54c1cSAl Viro 		.data_source = false,
12258cd54c1cSAl Viro 		.count = count,
12268cd54c1cSAl Viro 		.iov_offset = 0
12278cd54c1cSAl Viro 	};
12289ea9ce04SDavid Howells }
12299ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard);
12309ea9ce04SDavid Howells 
12319221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1232d879cb83SAl Viro {
1233d879cb83SAl Viro 	unsigned long res = 0;
1234d879cb83SAl Viro 	size_t size = i->count;
12359221d2e3SAl Viro 	size_t skip = i->iov_offset;
12369221d2e3SAl Viro 	unsigned k;
1237d879cb83SAl Viro 
12389221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
12399221d2e3SAl Viro 		size_t len = i->iov[k].iov_len - skip;
12409221d2e3SAl Viro 		if (len) {
12419221d2e3SAl Viro 			res |= (unsigned long)i->iov[k].iov_base + skip;
12429221d2e3SAl Viro 			if (len > size)
12439221d2e3SAl Viro 				len = size;
12449221d2e3SAl Viro 			res |= len;
12459221d2e3SAl Viro 			size -= len;
12469221d2e3SAl Viro 			if (!size)
12479221d2e3SAl Viro 				break;
12489221d2e3SAl Viro 		}
12499221d2e3SAl Viro 	}
12509221d2e3SAl Viro 	return res;
12519221d2e3SAl Viro }
12529221d2e3SAl Viro 
12539221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
12549221d2e3SAl Viro {
12559221d2e3SAl Viro 	unsigned res = 0;
12569221d2e3SAl Viro 	size_t size = i->count;
12579221d2e3SAl Viro 	unsigned skip = i->iov_offset;
12589221d2e3SAl Viro 	unsigned k;
12599221d2e3SAl Viro 
12609221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
12619221d2e3SAl Viro 		size_t len = i->bvec[k].bv_len - skip;
12629221d2e3SAl Viro 		res |= (unsigned long)i->bvec[k].bv_offset + skip;
12639221d2e3SAl Viro 		if (len > size)
12649221d2e3SAl Viro 			len = size;
12659221d2e3SAl Viro 		res |= len;
12669221d2e3SAl Viro 		size -= len;
12679221d2e3SAl Viro 		if (!size)
12689221d2e3SAl Viro 			break;
12699221d2e3SAl Viro 	}
12709221d2e3SAl Viro 	return res;
12719221d2e3SAl Viro }
12729221d2e3SAl Viro 
12739221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i)
12749221d2e3SAl Viro {
12759221d2e3SAl Viro 	/* iovec and kvec have identical layouts */
12769221d2e3SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
12779221d2e3SAl Viro 		return iov_iter_alignment_iovec(i);
12789221d2e3SAl Viro 
12799221d2e3SAl Viro 	if (iov_iter_is_bvec(i))
12809221d2e3SAl Viro 		return iov_iter_alignment_bvec(i);
12819221d2e3SAl Viro 
12829221d2e3SAl Viro 	if (iov_iter_is_pipe(i)) {
1283e0ff126eSJan Kara 		unsigned int p_mask = i->pipe->ring_size - 1;
12849221d2e3SAl Viro 		size_t size = i->count;
1285e0ff126eSJan Kara 
12868cefc107SDavid Howells 		if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
1287241699cdSAl Viro 			return size | i->iov_offset;
1288241699cdSAl Viro 		return size;
1289241699cdSAl Viro 	}
12909221d2e3SAl Viro 
12919221d2e3SAl Viro 	if (iov_iter_is_xarray(i))
12923d14ec1fSDavid Howells 		return (i->xarray_start + i->iov_offset) | i->count;
12939221d2e3SAl Viro 
12949221d2e3SAl Viro 	return 0;
1295d879cb83SAl Viro }
1296d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment);
1297d879cb83SAl Viro 
1298357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1299357f435dSAl Viro {
1300357f435dSAl Viro 	unsigned long res = 0;
1301610c7a71SAl Viro 	unsigned long v = 0;
1302357f435dSAl Viro 	size_t size = i->count;
1303610c7a71SAl Viro 	unsigned k;
1304357f435dSAl Viro 
1305610c7a71SAl Viro 	if (WARN_ON(!iter_is_iovec(i)))
1306241699cdSAl Viro 		return ~0U;
1307241699cdSAl Viro 
1308610c7a71SAl Viro 	for (k = 0; k < i->nr_segs; k++) {
1309610c7a71SAl Viro 		if (i->iov[k].iov_len) {
1310610c7a71SAl Viro 			unsigned long base = (unsigned long)i->iov[k].iov_base;
1311610c7a71SAl Viro 			if (v) // if not the first one
1312610c7a71SAl Viro 				res |= base | v; // this start | previous end
1313610c7a71SAl Viro 			v = base + i->iov[k].iov_len;
1314610c7a71SAl Viro 			if (size <= i->iov[k].iov_len)
1315610c7a71SAl Viro 				break;
1316610c7a71SAl Viro 			size -= i->iov[k].iov_len;
1317610c7a71SAl Viro 		}
1318610c7a71SAl Viro 	}
1319357f435dSAl Viro 	return res;
1320357f435dSAl Viro }
1321357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment);
1322357f435dSAl Viro 
1323e76b6312SIlya Dryomov static inline ssize_t __pipe_get_pages(struct iov_iter *i,
1324241699cdSAl Viro 				size_t maxsize,
1325241699cdSAl Viro 				struct page **pages,
13268cefc107SDavid Howells 				int iter_head,
1327241699cdSAl Viro 				size_t *start)
1328241699cdSAl Viro {
1329241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
13308cefc107SDavid Howells 	unsigned int p_mask = pipe->ring_size - 1;
13318cefc107SDavid Howells 	ssize_t n = push_pipe(i, maxsize, &iter_head, start);
1332241699cdSAl Viro 	if (!n)
1333241699cdSAl Viro 		return -EFAULT;
1334241699cdSAl Viro 
1335241699cdSAl Viro 	maxsize = n;
1336241699cdSAl Viro 	n += *start;
13371689c73aSAl Viro 	while (n > 0) {
13388cefc107SDavid Howells 		get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
13398cefc107SDavid Howells 		iter_head++;
1340241699cdSAl Viro 		n -= PAGE_SIZE;
1341241699cdSAl Viro 	}
1342241699cdSAl Viro 
1343241699cdSAl Viro 	return maxsize;
1344241699cdSAl Viro }
1345241699cdSAl Viro 
1346241699cdSAl Viro static ssize_t pipe_get_pages(struct iov_iter *i,
1347241699cdSAl Viro 		   struct page **pages, size_t maxsize, unsigned maxpages,
1348241699cdSAl Viro 		   size_t *start)
1349241699cdSAl Viro {
13508cefc107SDavid Howells 	unsigned int iter_head, npages;
1351241699cdSAl Viro 	size_t capacity;
1352241699cdSAl Viro 
1353241699cdSAl Viro 	if (!sanity(i))
1354241699cdSAl Viro 		return -EFAULT;
1355241699cdSAl Viro 
13568cefc107SDavid Howells 	data_start(i, &iter_head, start);
13578cefc107SDavid Howells 	/* Amount of free space: some of this one + all after this one */
13588cefc107SDavid Howells 	npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1359241699cdSAl Viro 	capacity = min(npages, maxpages) * PAGE_SIZE - *start;
1360241699cdSAl Viro 
13618cefc107SDavid Howells 	return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
1362241699cdSAl Viro }
1363241699cdSAl Viro 
13647ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
13657ff50620SDavid Howells 					  pgoff_t index, unsigned int nr_pages)
13667ff50620SDavid Howells {
13677ff50620SDavid Howells 	XA_STATE(xas, xa, index);
13687ff50620SDavid Howells 	struct page *page;
13697ff50620SDavid Howells 	unsigned int ret = 0;
13707ff50620SDavid Howells 
13717ff50620SDavid Howells 	rcu_read_lock();
13727ff50620SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
13737ff50620SDavid Howells 		if (xas_retry(&xas, page))
13747ff50620SDavid Howells 			continue;
13757ff50620SDavid Howells 
13767ff50620SDavid Howells 		/* Has the page moved or been split? */
13777ff50620SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
13787ff50620SDavid Howells 			xas_reset(&xas);
13797ff50620SDavid Howells 			continue;
13807ff50620SDavid Howells 		}
13817ff50620SDavid Howells 
13827ff50620SDavid Howells 		pages[ret] = find_subpage(page, xas.xa_index);
13837ff50620SDavid Howells 		get_page(pages[ret]);
13847ff50620SDavid Howells 		if (++ret == nr_pages)
13857ff50620SDavid Howells 			break;
13867ff50620SDavid Howells 	}
13877ff50620SDavid Howells 	rcu_read_unlock();
13887ff50620SDavid Howells 	return ret;
13897ff50620SDavid Howells }
13907ff50620SDavid Howells 
13917ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i,
13927ff50620SDavid Howells 				     struct page **pages, size_t maxsize,
13937ff50620SDavid Howells 				     unsigned maxpages, size_t *_start_offset)
13947ff50620SDavid Howells {
13957ff50620SDavid Howells 	unsigned nr, offset;
13967ff50620SDavid Howells 	pgoff_t index, count;
13977ff50620SDavid Howells 	size_t size = maxsize, actual;
13987ff50620SDavid Howells 	loff_t pos;
13997ff50620SDavid Howells 
14007ff50620SDavid Howells 	if (!size || !maxpages)
14017ff50620SDavid Howells 		return 0;
14027ff50620SDavid Howells 
14037ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
14047ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
14057ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
14067ff50620SDavid Howells 	*_start_offset = offset;
14077ff50620SDavid Howells 
14087ff50620SDavid Howells 	count = 1;
14097ff50620SDavid Howells 	if (size > PAGE_SIZE - offset) {
14107ff50620SDavid Howells 		size -= PAGE_SIZE - offset;
14117ff50620SDavid Howells 		count += size >> PAGE_SHIFT;
14127ff50620SDavid Howells 		size &= ~PAGE_MASK;
14137ff50620SDavid Howells 		if (size)
14147ff50620SDavid Howells 			count++;
14157ff50620SDavid Howells 	}
14167ff50620SDavid Howells 
14177ff50620SDavid Howells 	if (count > maxpages)
14187ff50620SDavid Howells 		count = maxpages;
14197ff50620SDavid Howells 
14207ff50620SDavid Howells 	nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
14217ff50620SDavid Howells 	if (nr == 0)
14227ff50620SDavid Howells 		return 0;
14237ff50620SDavid Howells 
14247ff50620SDavid Howells 	actual = PAGE_SIZE * nr;
14257ff50620SDavid Howells 	actual -= offset;
14267ff50620SDavid Howells 	if (nr == count && size > 0) {
14277ff50620SDavid Howells 		unsigned last_offset = (nr > 1) ? 0 : offset;
14287ff50620SDavid Howells 		actual -= PAGE_SIZE - (last_offset + size);
14297ff50620SDavid Howells 	}
14307ff50620SDavid Howells 	return actual;
14317ff50620SDavid Howells }
14327ff50620SDavid Howells 
14333d671ca6SAl Viro /* must be done on non-empty ITER_IOVEC one */
14343d671ca6SAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i,
14353d671ca6SAl Viro 					 size_t *size, size_t *start,
14363d671ca6SAl Viro 					 size_t maxsize, unsigned maxpages)
14373d671ca6SAl Viro {
14383d671ca6SAl Viro 	size_t skip;
14393d671ca6SAl Viro 	long k;
14403d671ca6SAl Viro 
14413d671ca6SAl Viro 	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
14423d671ca6SAl Viro 		unsigned long addr = (unsigned long)i->iov[k].iov_base + skip;
14433d671ca6SAl Viro 		size_t len = i->iov[k].iov_len - skip;
14443d671ca6SAl Viro 
14453d671ca6SAl Viro 		if (unlikely(!len))
14463d671ca6SAl Viro 			continue;
14473d671ca6SAl Viro 		if (len > maxsize)
14483d671ca6SAl Viro 			len = maxsize;
14493d671ca6SAl Viro 		len += (*start = addr % PAGE_SIZE);
14503d671ca6SAl Viro 		if (len > maxpages * PAGE_SIZE)
14513d671ca6SAl Viro 			len = maxpages * PAGE_SIZE;
14523d671ca6SAl Viro 		*size = len;
14533d671ca6SAl Viro 		return addr & PAGE_MASK;
14543d671ca6SAl Viro 	}
14553d671ca6SAl Viro 	BUG(); // if it had been empty, we wouldn't get called
14563d671ca6SAl Viro }
14573d671ca6SAl Viro 
14583d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */
14593d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i,
14603d671ca6SAl Viro 				       size_t *size, size_t *start,
14613d671ca6SAl Viro 				       size_t maxsize, unsigned maxpages)
14623d671ca6SAl Viro {
14633d671ca6SAl Viro 	struct page *page;
14643d671ca6SAl Viro 	size_t skip = i->iov_offset, len;
14653d671ca6SAl Viro 
14663d671ca6SAl Viro 	len = i->bvec->bv_len - skip;
14673d671ca6SAl Viro 	if (len > maxsize)
14683d671ca6SAl Viro 		len = maxsize;
14693d671ca6SAl Viro 	skip += i->bvec->bv_offset;
14703d671ca6SAl Viro 	page = i->bvec->bv_page + skip / PAGE_SIZE;
14713d671ca6SAl Viro 	len += (*start = skip % PAGE_SIZE);
14723d671ca6SAl Viro 	if (len > maxpages * PAGE_SIZE)
14733d671ca6SAl Viro 		len = maxpages * PAGE_SIZE;
14743d671ca6SAl Viro 	*size = len;
14753d671ca6SAl Viro 	return page;
14763d671ca6SAl Viro }
14773d671ca6SAl Viro 
1478d879cb83SAl Viro ssize_t iov_iter_get_pages(struct iov_iter *i,
1479d879cb83SAl Viro 		   struct page **pages, size_t maxsize, unsigned maxpages,
1480d879cb83SAl Viro 		   size_t *start)
1481d879cb83SAl Viro {
14823d671ca6SAl Viro 	size_t len;
14833d671ca6SAl Viro 	int n, res;
14843d671ca6SAl Viro 
1485d879cb83SAl Viro 	if (maxsize > i->count)
1486d879cb83SAl Viro 		maxsize = i->count;
14873d671ca6SAl Viro 	if (!maxsize)
14883d671ca6SAl Viro 		return 0;
1489d879cb83SAl Viro 
14903d671ca6SAl Viro 	if (likely(iter_is_iovec(i))) {
14913d671ca6SAl Viro 		unsigned long addr;
14929ea9ce04SDavid Howells 
14933d671ca6SAl Viro 		addr = first_iovec_segment(i, &len, start, maxsize, maxpages);
1494d879cb83SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
149573b0140bSIra Weiny 		res = get_user_pages_fast(addr, n,
149673b0140bSIra Weiny 				iov_iter_rw(i) != WRITE ?  FOLL_WRITE : 0,
149773b0140bSIra Weiny 				pages);
1498814a6674SAndreas Gruenbacher 		if (unlikely(res <= 0))
1499d879cb83SAl Viro 			return res;
1500d879cb83SAl Viro 		return (res == n ? len : res * PAGE_SIZE) - *start;
15013d671ca6SAl Viro 	}
15023d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
15033d671ca6SAl Viro 		struct page *page;
15043d671ca6SAl Viro 
15053d671ca6SAl Viro 		page = first_bvec_segment(i, &len, start, maxsize, maxpages);
15063d671ca6SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
15073d671ca6SAl Viro 		while (n--)
15083d671ca6SAl Viro 			get_page(*pages++ = page++);
15093d671ca6SAl Viro 		return len - *start;
15103d671ca6SAl Viro 	}
15113d671ca6SAl Viro 	if (iov_iter_is_pipe(i))
15123d671ca6SAl Viro 		return pipe_get_pages(i, pages, maxsize, maxpages, start);
15133d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
15143d671ca6SAl Viro 		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1515d879cb83SAl Viro 	return -EFAULT;
1516d879cb83SAl Viro }
1517d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_get_pages);
1518d879cb83SAl Viro 
1519d879cb83SAl Viro static struct page **get_pages_array(size_t n)
1520d879cb83SAl Viro {
1521752ade68SMichal Hocko 	return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
1522d879cb83SAl Viro }
1523d879cb83SAl Viro 
1524241699cdSAl Viro static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1525241699cdSAl Viro 		   struct page ***pages, size_t maxsize,
1526241699cdSAl Viro 		   size_t *start)
1527241699cdSAl Viro {
1528241699cdSAl Viro 	struct page **p;
15298cefc107SDavid Howells 	unsigned int iter_head, npages;
1530d7760d63SIlya Dryomov 	ssize_t n;
1531241699cdSAl Viro 
1532241699cdSAl Viro 	if (!sanity(i))
1533241699cdSAl Viro 		return -EFAULT;
1534241699cdSAl Viro 
15358cefc107SDavid Howells 	data_start(i, &iter_head, start);
15368cefc107SDavid Howells 	/* Amount of free space: some of this one + all after this one */
15378cefc107SDavid Howells 	npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1538241699cdSAl Viro 	n = npages * PAGE_SIZE - *start;
1539241699cdSAl Viro 	if (maxsize > n)
1540241699cdSAl Viro 		maxsize = n;
1541241699cdSAl Viro 	else
1542241699cdSAl Viro 		npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1543241699cdSAl Viro 	p = get_pages_array(npages);
1544241699cdSAl Viro 	if (!p)
1545241699cdSAl Viro 		return -ENOMEM;
15468cefc107SDavid Howells 	n = __pipe_get_pages(i, maxsize, p, iter_head, start);
1547241699cdSAl Viro 	if (n > 0)
1548241699cdSAl Viro 		*pages = p;
1549241699cdSAl Viro 	else
1550241699cdSAl Viro 		kvfree(p);
1551241699cdSAl Viro 	return n;
1552241699cdSAl Viro }
1553241699cdSAl Viro 
15547ff50620SDavid Howells static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
15557ff50620SDavid Howells 					   struct page ***pages, size_t maxsize,
15567ff50620SDavid Howells 					   size_t *_start_offset)
15577ff50620SDavid Howells {
15587ff50620SDavid Howells 	struct page **p;
15597ff50620SDavid Howells 	unsigned nr, offset;
15607ff50620SDavid Howells 	pgoff_t index, count;
15617ff50620SDavid Howells 	size_t size = maxsize, actual;
15627ff50620SDavid Howells 	loff_t pos;
15637ff50620SDavid Howells 
15647ff50620SDavid Howells 	if (!size)
15657ff50620SDavid Howells 		return 0;
15667ff50620SDavid Howells 
15677ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
15687ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
15697ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
15707ff50620SDavid Howells 	*_start_offset = offset;
15717ff50620SDavid Howells 
15727ff50620SDavid Howells 	count = 1;
15737ff50620SDavid Howells 	if (size > PAGE_SIZE - offset) {
15747ff50620SDavid Howells 		size -= PAGE_SIZE - offset;
15757ff50620SDavid Howells 		count += size >> PAGE_SHIFT;
15767ff50620SDavid Howells 		size &= ~PAGE_MASK;
15777ff50620SDavid Howells 		if (size)
15787ff50620SDavid Howells 			count++;
15797ff50620SDavid Howells 	}
15807ff50620SDavid Howells 
15817ff50620SDavid Howells 	p = get_pages_array(count);
15827ff50620SDavid Howells 	if (!p)
15837ff50620SDavid Howells 		return -ENOMEM;
15847ff50620SDavid Howells 	*pages = p;
15857ff50620SDavid Howells 
15867ff50620SDavid Howells 	nr = iter_xarray_populate_pages(p, i->xarray, index, count);
15877ff50620SDavid Howells 	if (nr == 0)
15887ff50620SDavid Howells 		return 0;
15897ff50620SDavid Howells 
15907ff50620SDavid Howells 	actual = PAGE_SIZE * nr;
15917ff50620SDavid Howells 	actual -= offset;
15927ff50620SDavid Howells 	if (nr == count && size > 0) {
15937ff50620SDavid Howells 		unsigned last_offset = (nr > 1) ? 0 : offset;
15947ff50620SDavid Howells 		actual -= PAGE_SIZE - (last_offset + size);
15957ff50620SDavid Howells 	}
15967ff50620SDavid Howells 	return actual;
15977ff50620SDavid Howells }
15987ff50620SDavid Howells 
1599d879cb83SAl Viro ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1600d879cb83SAl Viro 		   struct page ***pages, size_t maxsize,
1601d879cb83SAl Viro 		   size_t *start)
1602d879cb83SAl Viro {
1603d879cb83SAl Viro 	struct page **p;
16043d671ca6SAl Viro 	size_t len;
16053d671ca6SAl Viro 	int n, res;
1606d879cb83SAl Viro 
1607d879cb83SAl Viro 	if (maxsize > i->count)
1608d879cb83SAl Viro 		maxsize = i->count;
16093d671ca6SAl Viro 	if (!maxsize)
16103d671ca6SAl Viro 		return 0;
1611d879cb83SAl Viro 
16123d671ca6SAl Viro 	if (likely(iter_is_iovec(i))) {
16133d671ca6SAl Viro 		unsigned long addr;
16149ea9ce04SDavid Howells 
16153d671ca6SAl Viro 		addr = first_iovec_segment(i, &len, start, maxsize, ~0U);
1616d879cb83SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
1617d879cb83SAl Viro 		p = get_pages_array(n);
1618d879cb83SAl Viro 		if (!p)
1619d879cb83SAl Viro 			return -ENOMEM;
162073b0140bSIra Weiny 		res = get_user_pages_fast(addr, n,
162173b0140bSIra Weiny 				iov_iter_rw(i) != WRITE ?  FOLL_WRITE : 0, p);
1622814a6674SAndreas Gruenbacher 		if (unlikely(res <= 0)) {
1623d879cb83SAl Viro 			kvfree(p);
1624814a6674SAndreas Gruenbacher 			*pages = NULL;
1625d879cb83SAl Viro 			return res;
1626d879cb83SAl Viro 		}
1627d879cb83SAl Viro 		*pages = p;
1628d879cb83SAl Viro 		return (res == n ? len : res * PAGE_SIZE) - *start;
16293d671ca6SAl Viro 	}
16303d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
16313d671ca6SAl Viro 		struct page *page;
16323d671ca6SAl Viro 
16333d671ca6SAl Viro 		page = first_bvec_segment(i, &len, start, maxsize, ~0U);
16343d671ca6SAl Viro 		n = DIV_ROUND_UP(len, PAGE_SIZE);
16353d671ca6SAl Viro 		*pages = p = get_pages_array(n);
1636d879cb83SAl Viro 		if (!p)
1637d879cb83SAl Viro 			return -ENOMEM;
16383d671ca6SAl Viro 		while (n--)
16393d671ca6SAl Viro 			get_page(*p++ = page++);
16403d671ca6SAl Viro 		return len - *start;
16413d671ca6SAl Viro 	}
16423d671ca6SAl Viro 	if (iov_iter_is_pipe(i))
16433d671ca6SAl Viro 		return pipe_get_pages_alloc(i, pages, maxsize, start);
16443d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
16453d671ca6SAl Viro 		return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
1646d879cb83SAl Viro 	return -EFAULT;
1647d879cb83SAl Viro }
1648d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1649d879cb83SAl Viro 
1650d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1651d879cb83SAl Viro 			       struct iov_iter *i)
1652d879cb83SAl Viro {
1653d879cb83SAl Viro 	__wsum sum, next;
1654d879cb83SAl Viro 	sum = *csum;
16559ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1656241699cdSAl Viro 		WARN_ON(1);
1657241699cdSAl Viro 		return 0;
1658241699cdSAl Viro 	}
16597baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off, ({
16607baa5099SAl Viro 		next = csum_and_copy_from_user(base, addr + off, len);
1661d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
16627baa5099SAl Viro 		next ? 0 : len;
1663d879cb83SAl Viro 	}), ({
16647baa5099SAl Viro 		sum = csum_and_memcpy(addr + off, base, len, sum, off);
1665d879cb83SAl Viro 	})
1666d879cb83SAl Viro 	)
1667d879cb83SAl Viro 	*csum = sum;
1668d879cb83SAl Viro 	return bytes;
1669d879cb83SAl Viro }
1670d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter);
1671d879cb83SAl Viro 
167252cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1673d879cb83SAl Viro 			     struct iov_iter *i)
1674d879cb83SAl Viro {
167552cbd23aSWillem de Bruijn 	struct csum_state *csstate = _csstate;
1676d879cb83SAl Viro 	__wsum sum, next;
167778e1f386SAl Viro 
167878e1f386SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
1679241699cdSAl Viro 		WARN_ON(1);	/* for now */
1680241699cdSAl Viro 		return 0;
1681241699cdSAl Viro 	}
16826852df12SAl Viro 
16836852df12SAl Viro 	sum = csum_shift(csstate->csum, csstate->off);
16846852df12SAl Viro 	if (unlikely(iov_iter_is_pipe(i)))
16856852df12SAl Viro 		bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
16866852df12SAl Viro 	else iterate_and_advance(i, bytes, base, len, off, ({
16877baa5099SAl Viro 		next = csum_and_copy_to_user(addr + off, base, len);
1688d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
16897baa5099SAl Viro 		next ? 0 : len;
1690d879cb83SAl Viro 	}), ({
16917baa5099SAl Viro 		sum = csum_and_memcpy(base, addr + off, len, sum, off);
1692d879cb83SAl Viro 	})
1693d879cb83SAl Viro 	)
1694594e450bSAl Viro 	csstate->csum = csum_shift(sum, csstate->off);
1695594e450bSAl Viro 	csstate->off += bytes;
1696d879cb83SAl Viro 	return bytes;
1697d879cb83SAl Viro }
1698d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter);
1699d879cb83SAl Viro 
1700d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1701d05f4435SSagi Grimberg 		struct iov_iter *i)
1702d05f4435SSagi Grimberg {
17037999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH
1704d05f4435SSagi Grimberg 	struct ahash_request *hash = hashp;
1705d05f4435SSagi Grimberg 	struct scatterlist sg;
1706d05f4435SSagi Grimberg 	size_t copied;
1707d05f4435SSagi Grimberg 
1708d05f4435SSagi Grimberg 	copied = copy_to_iter(addr, bytes, i);
1709d05f4435SSagi Grimberg 	sg_init_one(&sg, addr, copied);
1710d05f4435SSagi Grimberg 	ahash_request_set_crypt(hash, &sg, NULL, copied);
1711d05f4435SSagi Grimberg 	crypto_ahash_update(hash);
1712d05f4435SSagi Grimberg 	return copied;
171327fad74aSYueHaibing #else
171427fad74aSYueHaibing 	return 0;
171527fad74aSYueHaibing #endif
1716d05f4435SSagi Grimberg }
1717d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter);
1718d05f4435SSagi Grimberg 
171966531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages)
1720d879cb83SAl Viro {
172166531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
172266531c65SAl Viro 	const struct iovec *p;
1723d879cb83SAl Viro 	int npages = 0;
1724d879cb83SAl Viro 
172566531c65SAl Viro 	for (p = i->iov; size; skip = 0, p++) {
172666531c65SAl Viro 		unsigned offs = offset_in_page(p->iov_base + skip);
172766531c65SAl Viro 		size_t len = min(p->iov_len - skip, size);
1728d879cb83SAl Viro 
172966531c65SAl Viro 		if (len) {
173066531c65SAl Viro 			size -= len;
173166531c65SAl Viro 			npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
173266531c65SAl Viro 			if (unlikely(npages > maxpages))
173366531c65SAl Viro 				return maxpages;
173466531c65SAl Viro 		}
173566531c65SAl Viro 	}
173666531c65SAl Viro 	return npages;
173766531c65SAl Viro }
173866531c65SAl Viro 
173966531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages)
174066531c65SAl Viro {
174166531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
174266531c65SAl Viro 	const struct bio_vec *p;
174366531c65SAl Viro 	int npages = 0;
174466531c65SAl Viro 
174566531c65SAl Viro 	for (p = i->bvec; size; skip = 0, p++) {
174666531c65SAl Viro 		unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
174766531c65SAl Viro 		size_t len = min(p->bv_len - skip, size);
174866531c65SAl Viro 
174966531c65SAl Viro 		size -= len;
175066531c65SAl Viro 		npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
175166531c65SAl Viro 		if (unlikely(npages > maxpages))
175266531c65SAl Viro 			return maxpages;
175366531c65SAl Viro 	}
175466531c65SAl Viro 	return npages;
175566531c65SAl Viro }
175666531c65SAl Viro 
175766531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages)
175866531c65SAl Viro {
175966531c65SAl Viro 	if (unlikely(!i->count))
176066531c65SAl Viro 		return 0;
176166531c65SAl Viro 	/* iovec and kvec have identical layouts */
176266531c65SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
176366531c65SAl Viro 		return iov_npages(i, maxpages);
176466531c65SAl Viro 	if (iov_iter_is_bvec(i))
176566531c65SAl Viro 		return bvec_npages(i, maxpages);
176666531c65SAl Viro 	if (iov_iter_is_pipe(i)) {
17678cefc107SDavid Howells 		unsigned int iter_head;
176866531c65SAl Viro 		int npages;
1769241699cdSAl Viro 		size_t off;
1770241699cdSAl Viro 
1771241699cdSAl Viro 		if (!sanity(i))
1772241699cdSAl Viro 			return 0;
1773241699cdSAl Viro 
17748cefc107SDavid Howells 		data_start(i, &iter_head, &off);
1775241699cdSAl Viro 		/* some of this one + all after this one */
177666531c65SAl Viro 		npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
177766531c65SAl Viro 		return min(npages, maxpages);
177866531c65SAl Viro 	}
177966531c65SAl Viro 	if (iov_iter_is_xarray(i)) {
1780e4f8df86SAl Viro 		unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1781e4f8df86SAl Viro 		int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
178266531c65SAl Viro 		return min(npages, maxpages);
178366531c65SAl Viro 	}
178466531c65SAl Viro 	return 0;
1785d879cb83SAl Viro }
1786d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages);
1787d879cb83SAl Viro 
1788d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1789d879cb83SAl Viro {
1790d879cb83SAl Viro 	*new = *old;
179100e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(new))) {
1792241699cdSAl Viro 		WARN_ON(1);
1793241699cdSAl Viro 		return NULL;
1794241699cdSAl Viro 	}
17957ff50620SDavid Howells 	if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
17969ea9ce04SDavid Howells 		return NULL;
179700e23707SDavid Howells 	if (iov_iter_is_bvec(new))
1798d879cb83SAl Viro 		return new->bvec = kmemdup(new->bvec,
1799d879cb83SAl Viro 				    new->nr_segs * sizeof(struct bio_vec),
1800d879cb83SAl Viro 				    flags);
1801d879cb83SAl Viro 	else
1802d879cb83SAl Viro 		/* iovec and kvec have identical layout */
1803d879cb83SAl Viro 		return new->iov = kmemdup(new->iov,
1804d879cb83SAl Viro 				   new->nr_segs * sizeof(struct iovec),
1805d879cb83SAl Viro 				   flags);
1806d879cb83SAl Viro }
1807d879cb83SAl Viro EXPORT_SYMBOL(dup_iter);
1808bc917be8SAl Viro 
1809bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov,
1810bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1811bfdc5970SChristoph Hellwig {
1812bfdc5970SChristoph Hellwig 	const struct compat_iovec __user *uiov =
1813bfdc5970SChristoph Hellwig 		(const struct compat_iovec __user *)uvec;
1814bfdc5970SChristoph Hellwig 	int ret = -EFAULT, i;
1815bfdc5970SChristoph Hellwig 
1816a959a978SChristoph Hellwig 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1817bfdc5970SChristoph Hellwig 		return -EFAULT;
1818bfdc5970SChristoph Hellwig 
1819bfdc5970SChristoph Hellwig 	for (i = 0; i < nr_segs; i++) {
1820bfdc5970SChristoph Hellwig 		compat_uptr_t buf;
1821bfdc5970SChristoph Hellwig 		compat_ssize_t len;
1822bfdc5970SChristoph Hellwig 
1823bfdc5970SChristoph Hellwig 		unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1824bfdc5970SChristoph Hellwig 		unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1825bfdc5970SChristoph Hellwig 
1826bfdc5970SChristoph Hellwig 		/* check for compat_size_t not fitting in compat_ssize_t .. */
1827bfdc5970SChristoph Hellwig 		if (len < 0) {
1828bfdc5970SChristoph Hellwig 			ret = -EINVAL;
1829bfdc5970SChristoph Hellwig 			goto uaccess_end;
1830bfdc5970SChristoph Hellwig 		}
1831bfdc5970SChristoph Hellwig 		iov[i].iov_base = compat_ptr(buf);
1832bfdc5970SChristoph Hellwig 		iov[i].iov_len = len;
1833bfdc5970SChristoph Hellwig 	}
1834bfdc5970SChristoph Hellwig 
1835bfdc5970SChristoph Hellwig 	ret = 0;
1836bfdc5970SChristoph Hellwig uaccess_end:
1837bfdc5970SChristoph Hellwig 	user_access_end();
1838bfdc5970SChristoph Hellwig 	return ret;
1839bfdc5970SChristoph Hellwig }
1840bfdc5970SChristoph Hellwig 
1841bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov,
1842bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1843fb041b59SDavid Laight {
1844fb041b59SDavid Laight 	unsigned long seg;
1845bfdc5970SChristoph Hellwig 
1846bfdc5970SChristoph Hellwig 	if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1847bfdc5970SChristoph Hellwig 		return -EFAULT;
1848bfdc5970SChristoph Hellwig 	for (seg = 0; seg < nr_segs; seg++) {
1849bfdc5970SChristoph Hellwig 		if ((ssize_t)iov[seg].iov_len < 0)
1850bfdc5970SChristoph Hellwig 			return -EINVAL;
1851bfdc5970SChristoph Hellwig 	}
1852bfdc5970SChristoph Hellwig 
1853bfdc5970SChristoph Hellwig 	return 0;
1854bfdc5970SChristoph Hellwig }
1855bfdc5970SChristoph Hellwig 
1856bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec,
1857bfdc5970SChristoph Hellwig 		unsigned long nr_segs, unsigned long fast_segs,
1858bfdc5970SChristoph Hellwig 		struct iovec *fast_iov, bool compat)
1859bfdc5970SChristoph Hellwig {
1860bfdc5970SChristoph Hellwig 	struct iovec *iov = fast_iov;
1861bfdc5970SChristoph Hellwig 	int ret;
1862fb041b59SDavid Laight 
1863fb041b59SDavid Laight 	/*
1864bfdc5970SChristoph Hellwig 	 * SuS says "The readv() function *may* fail if the iovcnt argument was
1865bfdc5970SChristoph Hellwig 	 * less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1866fb041b59SDavid Laight 	 * traditionally returned zero for zero segments, so...
1867fb041b59SDavid Laight 	 */
1868bfdc5970SChristoph Hellwig 	if (nr_segs == 0)
1869bfdc5970SChristoph Hellwig 		return iov;
1870bfdc5970SChristoph Hellwig 	if (nr_segs > UIO_MAXIOV)
1871bfdc5970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1872fb041b59SDavid Laight 	if (nr_segs > fast_segs) {
1873fb041b59SDavid Laight 		iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1874bfdc5970SChristoph Hellwig 		if (!iov)
1875bfdc5970SChristoph Hellwig 			return ERR_PTR(-ENOMEM);
1876fb041b59SDavid Laight 	}
1877bfdc5970SChristoph Hellwig 
1878bfdc5970SChristoph Hellwig 	if (compat)
1879bfdc5970SChristoph Hellwig 		ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1880bfdc5970SChristoph Hellwig 	else
1881bfdc5970SChristoph Hellwig 		ret = copy_iovec_from_user(iov, uvec, nr_segs);
1882bfdc5970SChristoph Hellwig 	if (ret) {
1883bfdc5970SChristoph Hellwig 		if (iov != fast_iov)
1884bfdc5970SChristoph Hellwig 			kfree(iov);
1885bfdc5970SChristoph Hellwig 		return ERR_PTR(ret);
1886fb041b59SDavid Laight 	}
1887bfdc5970SChristoph Hellwig 
1888bfdc5970SChristoph Hellwig 	return iov;
1889bfdc5970SChristoph Hellwig }
1890bfdc5970SChristoph Hellwig 
1891bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1892bfdc5970SChristoph Hellwig 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1893bfdc5970SChristoph Hellwig 		 struct iov_iter *i, bool compat)
1894bfdc5970SChristoph Hellwig {
1895bfdc5970SChristoph Hellwig 	ssize_t total_len = 0;
1896bfdc5970SChristoph Hellwig 	unsigned long seg;
1897bfdc5970SChristoph Hellwig 	struct iovec *iov;
1898bfdc5970SChristoph Hellwig 
1899bfdc5970SChristoph Hellwig 	iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1900bfdc5970SChristoph Hellwig 	if (IS_ERR(iov)) {
1901bfdc5970SChristoph Hellwig 		*iovp = NULL;
1902bfdc5970SChristoph Hellwig 		return PTR_ERR(iov);
1903fb041b59SDavid Laight 	}
1904fb041b59SDavid Laight 
1905fb041b59SDavid Laight 	/*
1906bfdc5970SChristoph Hellwig 	 * According to the Single Unix Specification we should return EINVAL if
1907bfdc5970SChristoph Hellwig 	 * an element length is < 0 when cast to ssize_t or if the total length
1908bfdc5970SChristoph Hellwig 	 * would overflow the ssize_t return value of the system call.
1909fb041b59SDavid Laight 	 *
1910fb041b59SDavid Laight 	 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1911fb041b59SDavid Laight 	 * overflow case.
1912fb041b59SDavid Laight 	 */
1913fb041b59SDavid Laight 	for (seg = 0; seg < nr_segs; seg++) {
1914fb041b59SDavid Laight 		ssize_t len = (ssize_t)iov[seg].iov_len;
1915fb041b59SDavid Laight 
1916bfdc5970SChristoph Hellwig 		if (!access_ok(iov[seg].iov_base, len)) {
1917bfdc5970SChristoph Hellwig 			if (iov != *iovp)
1918bfdc5970SChristoph Hellwig 				kfree(iov);
1919bfdc5970SChristoph Hellwig 			*iovp = NULL;
1920bfdc5970SChristoph Hellwig 			return -EFAULT;
1921fb041b59SDavid Laight 		}
1922bfdc5970SChristoph Hellwig 
1923bfdc5970SChristoph Hellwig 		if (len > MAX_RW_COUNT - total_len) {
1924bfdc5970SChristoph Hellwig 			len = MAX_RW_COUNT - total_len;
1925fb041b59SDavid Laight 			iov[seg].iov_len = len;
1926fb041b59SDavid Laight 		}
1927bfdc5970SChristoph Hellwig 		total_len += len;
1928fb041b59SDavid Laight 	}
1929bfdc5970SChristoph Hellwig 
1930bfdc5970SChristoph Hellwig 	iov_iter_init(i, type, iov, nr_segs, total_len);
1931bfdc5970SChristoph Hellwig 	if (iov == *iovp)
1932bfdc5970SChristoph Hellwig 		*iovp = NULL;
1933bfdc5970SChristoph Hellwig 	else
1934bfdc5970SChristoph Hellwig 		*iovp = iov;
1935bfdc5970SChristoph Hellwig 	return total_len;
1936fb041b59SDavid Laight }
1937fb041b59SDavid Laight 
1938ffecee4fSVegard Nossum /**
1939ffecee4fSVegard Nossum  * import_iovec() - Copy an array of &struct iovec from userspace
1940ffecee4fSVegard Nossum  *     into the kernel, check that it is valid, and initialize a new
1941ffecee4fSVegard Nossum  *     &struct iov_iter iterator to access it.
1942ffecee4fSVegard Nossum  *
1943ffecee4fSVegard Nossum  * @type: One of %READ or %WRITE.
1944bfdc5970SChristoph Hellwig  * @uvec: Pointer to the userspace array.
1945ffecee4fSVegard Nossum  * @nr_segs: Number of elements in userspace array.
1946ffecee4fSVegard Nossum  * @fast_segs: Number of elements in @iov.
1947bfdc5970SChristoph Hellwig  * @iovp: (input and output parameter) Pointer to pointer to (usually small
1948ffecee4fSVegard Nossum  *     on-stack) kernel array.
1949ffecee4fSVegard Nossum  * @i: Pointer to iterator that will be initialized on success.
1950ffecee4fSVegard Nossum  *
1951ffecee4fSVegard Nossum  * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1952ffecee4fSVegard Nossum  * then this function places %NULL in *@iov on return. Otherwise, a new
1953ffecee4fSVegard Nossum  * array will be allocated and the result placed in *@iov. This means that
1954ffecee4fSVegard Nossum  * the caller may call kfree() on *@iov regardless of whether the small
1955ffecee4fSVegard Nossum  * on-stack array was used or not (and regardless of whether this function
1956ffecee4fSVegard Nossum  * returns an error or not).
1957ffecee4fSVegard Nossum  *
195887e5e6daSJens Axboe  * Return: Negative error code on error, bytes imported on success
1959ffecee4fSVegard Nossum  */
1960bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec,
1961bc917be8SAl Viro 		 unsigned nr_segs, unsigned fast_segs,
1962bfdc5970SChristoph Hellwig 		 struct iovec **iovp, struct iov_iter *i)
1963bc917be8SAl Viro {
196489cd35c5SChristoph Hellwig 	return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
196589cd35c5SChristoph Hellwig 			      in_compat_syscall());
1966bc917be8SAl Viro }
1967bc917be8SAl Viro EXPORT_SYMBOL(import_iovec);
1968bc917be8SAl Viro 
1969bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len,
1970bc917be8SAl Viro 		 struct iovec *iov, struct iov_iter *i)
1971bc917be8SAl Viro {
1972bc917be8SAl Viro 	if (len > MAX_RW_COUNT)
1973bc917be8SAl Viro 		len = MAX_RW_COUNT;
197496d4f267SLinus Torvalds 	if (unlikely(!access_ok(buf, len)))
1975bc917be8SAl Viro 		return -EFAULT;
1976bc917be8SAl Viro 
1977bc917be8SAl Viro 	iov->iov_base = buf;
1978bc917be8SAl Viro 	iov->iov_len = len;
1979bc917be8SAl Viro 	iov_iter_init(i, rw, iov, 1, len);
1980bc917be8SAl Viro 	return 0;
1981bc917be8SAl Viro }
1982e1267585SAl Viro EXPORT_SYMBOL(import_single_range);
19838fb0f47aSJens Axboe 
19848fb0f47aSJens Axboe /**
19858fb0f47aSJens Axboe  * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
19868fb0f47aSJens Axboe  *     iov_iter_save_state() was called.
19878fb0f47aSJens Axboe  *
19888fb0f47aSJens Axboe  * @i: &struct iov_iter to restore
19898fb0f47aSJens Axboe  * @state: state to restore from
19908fb0f47aSJens Axboe  *
19918fb0f47aSJens Axboe  * Used after iov_iter_save_state() to bring restore @i, if operations may
19928fb0f47aSJens Axboe  * have advanced it.
19938fb0f47aSJens Axboe  *
19948fb0f47aSJens Axboe  * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
19958fb0f47aSJens Axboe  */
19968fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
19978fb0f47aSJens Axboe {
19988fb0f47aSJens Axboe 	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
19998fb0f47aSJens Axboe 			 !iov_iter_is_kvec(i))
20008fb0f47aSJens Axboe 		return;
20018fb0f47aSJens Axboe 	i->iov_offset = state->iov_offset;
20028fb0f47aSJens Axboe 	i->count = state->count;
20038fb0f47aSJens Axboe 	/*
20048fb0f47aSJens Axboe 	 * For the *vec iters, nr_segs + iov is constant - if we increment
20058fb0f47aSJens Axboe 	 * the vec, then we also decrement the nr_segs count. Hence we don't
20068fb0f47aSJens Axboe 	 * need to track both of these, just one is enough and we can deduct
20078fb0f47aSJens Axboe 	 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
20088fb0f47aSJens Axboe 	 * size, so we can just increment the iov pointer as they are unionzed.
20098fb0f47aSJens Axboe 	 * ITER_BVEC _may_ be the same size on some archs, but on others it is
20108fb0f47aSJens Axboe 	 * not. Be safe and handle it separately.
20118fb0f47aSJens Axboe 	 */
20128fb0f47aSJens Axboe 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
20138fb0f47aSJens Axboe 	if (iov_iter_is_bvec(i))
20148fb0f47aSJens Axboe 		i->bvec -= state->nr_segs - i->nr_segs;
20158fb0f47aSJens Axboe 	else
20168fb0f47aSJens Axboe 		i->iov -= state->nr_segs - i->nr_segs;
20178fb0f47aSJens Axboe 	i->nr_segs = state->nr_segs;
20188fb0f47aSJens Axboe }
2019