xref: /openbmc/linux/lib/iov_iter.c (revision 84bd06c6)
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 
17fcb14cb1SAl Viro /* covers ubuf and kbuf alike */
18fcb14cb1SAl Viro #define iterate_buf(i, n, base, len, off, __p, STEP) {		\
19fcb14cb1SAl Viro 	size_t __maybe_unused off = 0;				\
20fcb14cb1SAl Viro 	len = n;						\
21fcb14cb1SAl Viro 	base = __p + i->iov_offset;				\
22fcb14cb1SAl Viro 	len -= (STEP);						\
23fcb14cb1SAl Viro 	i->iov_offset += len;					\
24fcb14cb1SAl Viro 	n = len;						\
25fcb14cb1SAl Viro }
26fcb14cb1SAl Viro 
275c67aa90SAl Viro /* covers iovec and kvec alike */
28a6e4ec7bSAl Viro #define iterate_iovec(i, n, base, len, off, __p, STEP) {	\
297baa5099SAl Viro 	size_t off = 0;						\
30a6e4ec7bSAl Viro 	size_t skip = i->iov_offset;				\
317a1bcb5dSAl Viro 	do {							\
327baa5099SAl Viro 		len = min(n, __p->iov_len - skip);		\
337baa5099SAl Viro 		if (likely(len)) {				\
347baa5099SAl Viro 			base = __p->iov_base + skip;		\
357baa5099SAl Viro 			len -= (STEP);				\
367baa5099SAl Viro 			off += len;				\
377baa5099SAl Viro 			skip += len;				\
387baa5099SAl Viro 			n -= len;				\
397a1bcb5dSAl Viro 			if (skip < __p->iov_len)		\
407a1bcb5dSAl Viro 				break;				\
41d879cb83SAl Viro 		}						\
42d879cb83SAl Viro 		__p++;						\
437a1bcb5dSAl Viro 		skip = 0;					\
447a1bcb5dSAl Viro 	} while (n);						\
45a6e4ec7bSAl Viro 	i->iov_offset = skip;					\
467baa5099SAl Viro 	n = off;						\
47d879cb83SAl Viro }
48d879cb83SAl Viro 
49a6e4ec7bSAl Viro #define iterate_bvec(i, n, base, len, off, p, STEP) {		\
507baa5099SAl Viro 	size_t off = 0;						\
51a6e4ec7bSAl Viro 	unsigned skip = i->iov_offset;				\
527491a2bfSAl Viro 	while (n) {						\
537491a2bfSAl Viro 		unsigned offset = p->bv_offset + skip;		\
541b4fb5ffSAl Viro 		unsigned left;					\
5521b56c84SAl Viro 		void *kaddr = kmap_local_page(p->bv_page +	\
5621b56c84SAl Viro 					offset / PAGE_SIZE);	\
577baa5099SAl Viro 		base = kaddr + offset % PAGE_SIZE;		\
58a6e4ec7bSAl Viro 		len = min(min(n, (size_t)(p->bv_len - skip)),	\
597491a2bfSAl Viro 		     (size_t)(PAGE_SIZE - offset % PAGE_SIZE));	\
601b4fb5ffSAl Viro 		left = (STEP);					\
6121b56c84SAl Viro 		kunmap_local(kaddr);				\
627baa5099SAl Viro 		len -= left;					\
637baa5099SAl Viro 		off += len;					\
647baa5099SAl Viro 		skip += len;					\
657491a2bfSAl Viro 		if (skip == p->bv_len) {			\
667491a2bfSAl Viro 			skip = 0;				\
677491a2bfSAl Viro 			p++;					\
68d879cb83SAl Viro 		}						\
697baa5099SAl Viro 		n -= len;					\
701b4fb5ffSAl Viro 		if (left)					\
711b4fb5ffSAl Viro 			break;					\
727491a2bfSAl Viro 	}							\
73a6e4ec7bSAl Viro 	i->iov_offset = skip;					\
747baa5099SAl Viro 	n = off;						\
75d879cb83SAl Viro }
76d879cb83SAl Viro 
77a6e4ec7bSAl Viro #define iterate_xarray(i, n, base, len, __off, STEP) {		\
781b4fb5ffSAl Viro 	__label__ __out;					\
79622838f3SAl Viro 	size_t __off = 0;					\
80821979f5SMatthew Wilcox (Oracle) 	struct folio *folio;					\
81a6e4ec7bSAl Viro 	loff_t start = i->xarray_start + i->iov_offset;		\
824b179e9aSAl Viro 	pgoff_t index = start / PAGE_SIZE;			\
837ff50620SDavid Howells 	XA_STATE(xas, i->xarray, index);			\
847ff50620SDavid Howells 								\
85821979f5SMatthew Wilcox (Oracle) 	len = PAGE_SIZE - offset_in_page(start);		\
867ff50620SDavid Howells 	rcu_read_lock();					\
87821979f5SMatthew Wilcox (Oracle) 	xas_for_each(&xas, folio, ULONG_MAX) {			\
881b4fb5ffSAl Viro 		unsigned left;					\
89821979f5SMatthew Wilcox (Oracle) 		size_t offset;					\
90821979f5SMatthew Wilcox (Oracle) 		if (xas_retry(&xas, folio))			\
917ff50620SDavid Howells 			continue;				\
92821979f5SMatthew Wilcox (Oracle) 		if (WARN_ON(xa_is_value(folio)))		\
937ff50620SDavid Howells 			break;					\
94821979f5SMatthew Wilcox (Oracle) 		if (WARN_ON(folio_test_hugetlb(folio)))		\
957ff50620SDavid Howells 			break;					\
96821979f5SMatthew Wilcox (Oracle) 		offset = offset_in_folio(folio, start + __off);	\
97821979f5SMatthew Wilcox (Oracle) 		while (offset < folio_size(folio)) {		\
98821979f5SMatthew Wilcox (Oracle) 			base = kmap_local_folio(folio, offset);	\
997baa5099SAl Viro 			len = min(n, len);			\
1001b4fb5ffSAl Viro 			left = (STEP);				\
101821979f5SMatthew Wilcox (Oracle) 			kunmap_local(base);			\
1027baa5099SAl Viro 			len -= left;				\
1037baa5099SAl Viro 			__off += len;				\
1047baa5099SAl Viro 			n -= len;				\
1051b4fb5ffSAl Viro 			if (left || n == 0)			\
1061b4fb5ffSAl Viro 				goto __out;			\
107821979f5SMatthew Wilcox (Oracle) 			offset += len;				\
108821979f5SMatthew Wilcox (Oracle) 			len = PAGE_SIZE;			\
1097ff50620SDavid Howells 		}						\
1107ff50620SDavid Howells 	}							\
1111b4fb5ffSAl Viro __out:								\
1127ff50620SDavid Howells 	rcu_read_unlock();					\
113a6e4ec7bSAl Viro 	i->iov_offset += __off;					\
114622838f3SAl Viro 	n = __off;						\
1157ff50620SDavid Howells }
1167ff50620SDavid Howells 
1177baa5099SAl Viro #define __iterate_and_advance(i, n, base, len, off, I, K) {	\
118dd254f5aSAl Viro 	if (unlikely(i->count < n))				\
119dd254f5aSAl Viro 		n = i->count;					\
120f5da8354SAl Viro 	if (likely(n)) {					\
121fcb14cb1SAl Viro 		if (likely(iter_is_ubuf(i))) {			\
122fcb14cb1SAl Viro 			void __user *base;			\
123fcb14cb1SAl Viro 			size_t len;				\
124fcb14cb1SAl Viro 			iterate_buf(i, n, base, len, off,	\
125fcb14cb1SAl Viro 						i->ubuf, (I)) 	\
126fcb14cb1SAl Viro 		} else if (likely(iter_is_iovec(i))) {		\
127de4f5fedSJens Axboe 			const struct iovec *iov = iter_iov(i);	\
1287baa5099SAl Viro 			void __user *base;			\
1297baa5099SAl Viro 			size_t len;				\
1307baa5099SAl Viro 			iterate_iovec(i, n, base, len, off,	\
131a6e4ec7bSAl Viro 						iov, (I))	\
132de4f5fedSJens Axboe 			i->nr_segs -= iov - iter_iov(i);	\
133de4f5fedSJens Axboe 			i->__iov = iov;				\
13428f38db7SAl Viro 		} else if (iov_iter_is_bvec(i)) {		\
13528f38db7SAl Viro 			const struct bio_vec *bvec = i->bvec;	\
1367baa5099SAl Viro 			void *base;				\
1377baa5099SAl Viro 			size_t len;				\
1387baa5099SAl Viro 			iterate_bvec(i, n, base, len, off,	\
139a6e4ec7bSAl Viro 						bvec, (K))	\
1407491a2bfSAl Viro 			i->nr_segs -= bvec - i->bvec;		\
1417491a2bfSAl Viro 			i->bvec = bvec;				\
14228f38db7SAl Viro 		} else if (iov_iter_is_kvec(i)) {		\
1435c67aa90SAl Viro 			const struct kvec *kvec = i->kvec;	\
1447baa5099SAl Viro 			void *base;				\
1457baa5099SAl Viro 			size_t len;				\
1467baa5099SAl Viro 			iterate_iovec(i, n, base, len, off,	\
147a6e4ec7bSAl Viro 						kvec, (K))	\
14828f38db7SAl Viro 			i->nr_segs -= kvec - i->kvec;		\
14928f38db7SAl Viro 			i->kvec = kvec;				\
15028f38db7SAl Viro 		} else if (iov_iter_is_xarray(i)) {		\
1517baa5099SAl Viro 			void *base;				\
1527baa5099SAl Viro 			size_t len;				\
1537baa5099SAl Viro 			iterate_xarray(i, n, base, len, off,	\
154a6e4ec7bSAl Viro 							(K))	\
155d879cb83SAl Viro 		}						\
156d879cb83SAl Viro 		i->count -= n;					\
157dd254f5aSAl Viro 	}							\
158d879cb83SAl Viro }
1597baa5099SAl Viro #define iterate_and_advance(i, n, base, len, off, I, K) \
1607baa5099SAl Viro 	__iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
161d879cb83SAl Viro 
16209fc68dcSAl Viro static int copyout(void __user *to, const void *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(to, n)) {
167d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
16809fc68dcSAl Viro 		n = raw_copy_to_user(to, from, n);
16909fc68dcSAl Viro 	}
17009fc68dcSAl Viro 	return n;
17109fc68dcSAl Viro }
17209fc68dcSAl Viro 
1734f80818bSLorenzo Stoakes static int copyout_nofault(void __user *to, const void *from, size_t n)
1744f80818bSLorenzo Stoakes {
1754f80818bSLorenzo Stoakes 	long res;
1764f80818bSLorenzo Stoakes 
1774f80818bSLorenzo Stoakes 	if (should_fail_usercopy())
1784f80818bSLorenzo Stoakes 		return n;
1794f80818bSLorenzo Stoakes 
1804f80818bSLorenzo Stoakes 	res = copy_to_user_nofault(to, from, n);
1814f80818bSLorenzo Stoakes 
1824f80818bSLorenzo Stoakes 	return res < 0 ? n : res;
1834f80818bSLorenzo Stoakes }
1844f80818bSLorenzo Stoakes 
18509fc68dcSAl Viro static int copyin(void *to, const void __user *from, size_t n)
18609fc68dcSAl Viro {
18733b75c1dSAlexander Potapenko 	size_t res = n;
18833b75c1dSAlexander Potapenko 
1894d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1904d0e9df5SAlbert van der Linde 		return n;
19196d4f267SLinus Torvalds 	if (access_ok(from, n)) {
19233b75c1dSAlexander Potapenko 		instrument_copy_from_user_before(to, from, n);
19333b75c1dSAlexander Potapenko 		res = raw_copy_from_user(to, from, n);
19433b75c1dSAlexander Potapenko 		instrument_copy_from_user_after(to, from, n, res);
19509fc68dcSAl Viro 	}
19633b75c1dSAlexander Potapenko 	return res;
19709fc68dcSAl Viro }
19809fc68dcSAl Viro 
199d879cb83SAl Viro /*
200a6294593SAndreas Gruenbacher  * fault_in_iov_iter_readable - fault in iov iterator for reading
201a6294593SAndreas Gruenbacher  * @i: iterator
202a6294593SAndreas Gruenbacher  * @size: maximum length
203171a0203SAnton Altaparmakov  *
204a6294593SAndreas Gruenbacher  * Fault in one or more iovecs of the given iov_iter, to a maximum length of
205a6294593SAndreas Gruenbacher  * @size.  For each iovec, fault in each page that constitutes the iovec.
206a6294593SAndreas Gruenbacher  *
207a6294593SAndreas Gruenbacher  * Returns the number of bytes not faulted in (like copy_to_user() and
208a6294593SAndreas Gruenbacher  * copy_from_user()).
209a6294593SAndreas Gruenbacher  *
210a6294593SAndreas Gruenbacher  * Always returns 0 for non-userspace iterators.
211171a0203SAnton Altaparmakov  */
212a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
213171a0203SAnton Altaparmakov {
214fcb14cb1SAl Viro 	if (iter_is_ubuf(i)) {
215fcb14cb1SAl Viro 		size_t n = min(size, iov_iter_count(i));
216fcb14cb1SAl Viro 		n -= fault_in_readable(i->ubuf + i->iov_offset, n);
217fcb14cb1SAl Viro 		return size - n;
218fcb14cb1SAl Viro 	} else if (iter_is_iovec(i)) {
219a6294593SAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
2208409a0d2SAl Viro 		const struct iovec *p;
2218409a0d2SAl Viro 		size_t skip;
2228409a0d2SAl Viro 
223a6294593SAndreas Gruenbacher 		size -= count;
224de4f5fedSJens Axboe 		for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) {
225a6294593SAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
226a6294593SAndreas Gruenbacher 			size_t ret;
2278409a0d2SAl Viro 
2288409a0d2SAl Viro 			if (unlikely(!len))
2298409a0d2SAl Viro 				continue;
230a6294593SAndreas Gruenbacher 			ret = fault_in_readable(p->iov_base + skip, len);
231a6294593SAndreas Gruenbacher 			count -= len - ret;
232a6294593SAndreas Gruenbacher 			if (ret)
233a6294593SAndreas Gruenbacher 				break;
2348409a0d2SAl Viro 		}
235a6294593SAndreas Gruenbacher 		return count + size;
236171a0203SAnton Altaparmakov 	}
237171a0203SAnton Altaparmakov 	return 0;
238171a0203SAnton Altaparmakov }
239a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable);
240171a0203SAnton Altaparmakov 
241cdd591fcSAndreas Gruenbacher /*
242cdd591fcSAndreas Gruenbacher  * fault_in_iov_iter_writeable - fault in iov iterator for writing
243cdd591fcSAndreas Gruenbacher  * @i: iterator
244cdd591fcSAndreas Gruenbacher  * @size: maximum length
245cdd591fcSAndreas Gruenbacher  *
246cdd591fcSAndreas Gruenbacher  * Faults in the iterator using get_user_pages(), i.e., without triggering
247cdd591fcSAndreas Gruenbacher  * hardware page faults.  This is primarily useful when we already know that
248cdd591fcSAndreas Gruenbacher  * some or all of the pages in @i aren't in memory.
249cdd591fcSAndreas Gruenbacher  *
250cdd591fcSAndreas Gruenbacher  * Returns the number of bytes not faulted in, like copy_to_user() and
251cdd591fcSAndreas Gruenbacher  * copy_from_user().
252cdd591fcSAndreas Gruenbacher  *
253cdd591fcSAndreas Gruenbacher  * Always returns 0 for non-user-space iterators.
254cdd591fcSAndreas Gruenbacher  */
255cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
256cdd591fcSAndreas Gruenbacher {
257fcb14cb1SAl Viro 	if (iter_is_ubuf(i)) {
258fcb14cb1SAl Viro 		size_t n = min(size, iov_iter_count(i));
259fcb14cb1SAl Viro 		n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n);
260fcb14cb1SAl Viro 		return size - n;
261fcb14cb1SAl Viro 	} else if (iter_is_iovec(i)) {
262cdd591fcSAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
263cdd591fcSAndreas Gruenbacher 		const struct iovec *p;
264cdd591fcSAndreas Gruenbacher 		size_t skip;
265cdd591fcSAndreas Gruenbacher 
266cdd591fcSAndreas Gruenbacher 		size -= count;
267de4f5fedSJens Axboe 		for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) {
268cdd591fcSAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
269cdd591fcSAndreas Gruenbacher 			size_t ret;
270cdd591fcSAndreas Gruenbacher 
271cdd591fcSAndreas Gruenbacher 			if (unlikely(!len))
272cdd591fcSAndreas Gruenbacher 				continue;
273cdd591fcSAndreas Gruenbacher 			ret = fault_in_safe_writeable(p->iov_base + skip, len);
274cdd591fcSAndreas Gruenbacher 			count -= len - ret;
275cdd591fcSAndreas Gruenbacher 			if (ret)
276cdd591fcSAndreas Gruenbacher 				break;
277cdd591fcSAndreas Gruenbacher 		}
278cdd591fcSAndreas Gruenbacher 		return count + size;
279cdd591fcSAndreas Gruenbacher 	}
280cdd591fcSAndreas Gruenbacher 	return 0;
281cdd591fcSAndreas Gruenbacher }
282cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable);
283cdd591fcSAndreas Gruenbacher 
284aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction,
285d879cb83SAl Viro 			const struct iovec *iov, unsigned long nr_segs,
286d879cb83SAl Viro 			size_t count)
287d879cb83SAl Viro {
288aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
2898cd54c1cSAl Viro 	*i = (struct iov_iter) {
2908cd54c1cSAl Viro 		.iter_type = ITER_IOVEC,
291245f0922SKefeng Wang 		.copy_mc = false,
2923337ab08SAndreas Gruenbacher 		.nofault = false,
293fcb14cb1SAl Viro 		.user_backed = true,
2948cd54c1cSAl Viro 		.data_source = direction,
295de4f5fedSJens Axboe 		.__iov = iov,
2968cd54c1cSAl Viro 		.nr_segs = nr_segs,
2978cd54c1cSAl Viro 		.iov_offset = 0,
2988cd54c1cSAl Viro 		.count = count
2998cd54c1cSAl Viro 	};
300d879cb83SAl Viro }
301d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init);
302d879cb83SAl Viro 
303f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
304f9152895SAl Viro 			      __wsum sum, size_t off)
305f9152895SAl Viro {
306cc44c17bSAl Viro 	__wsum next = csum_partial_copy_nocheck(from, to, len);
307f9152895SAl Viro 	return csum_block_add(sum, next, off);
308f9152895SAl Viro }
309f9152895SAl Viro 
310aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
311d879cb83SAl Viro {
312a41dad90SAl Viro 	if (WARN_ON_ONCE(i->data_source))
313a41dad90SAl Viro 		return 0;
314fcb14cb1SAl Viro 	if (user_backed_iter(i))
31509fc68dcSAl Viro 		might_fault();
3167baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
3177baa5099SAl Viro 		copyout(base, addr + off, len),
3187baa5099SAl Viro 		memcpy(base, addr + off, len)
319d879cb83SAl Viro 	)
320d879cb83SAl Viro 
321d879cb83SAl Viro 	return bytes;
322d879cb83SAl Viro }
323aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter);
324d879cb83SAl Viro 
325ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC
326ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n)
3278780356eSDan Williams {
32896d4f267SLinus Torvalds 	if (access_ok(to, n)) {
329d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
330ec6347bbSDan Williams 		n = copy_mc_to_user((__force void *) to, from, n);
3318780356eSDan Williams 	}
3328780356eSDan Williams 	return n;
3338780356eSDan Williams }
3348780356eSDan Williams 
335bf3eeb9bSDan Williams /**
336ec6347bbSDan Williams  * _copy_mc_to_iter - copy to iter with source memory error exception handling
337bf3eeb9bSDan Williams  * @addr: source kernel address
338bf3eeb9bSDan Williams  * @bytes: total transfer length
33944e55997SRandy Dunlap  * @i: destination iterator
340bf3eeb9bSDan Williams  *
341ec6347bbSDan Williams  * The pmem driver deploys this for the dax operation
342ec6347bbSDan Williams  * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
343ec6347bbSDan Williams  * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
344ec6347bbSDan Williams  * successfully copied.
345bf3eeb9bSDan Williams  *
346ec6347bbSDan Williams  * The main differences between this and typical _copy_to_iter().
347bf3eeb9bSDan Williams  *
348bf3eeb9bSDan Williams  * * Typical tail/residue handling after a fault retries the copy
349bf3eeb9bSDan Williams  *   byte-by-byte until the fault happens again. Re-triggering machine
350bf3eeb9bSDan Williams  *   checks is potentially fatal so the implementation uses source
351bf3eeb9bSDan Williams  *   alignment and poison alignment assumptions to avoid re-triggering
352bf3eeb9bSDan Williams  *   hardware exceptions.
353bf3eeb9bSDan Williams  *
3543fc40265SDavid Howells  * * ITER_KVEC and ITER_BVEC can return short copies.  Compare to
3553fc40265SDavid Howells  *   copy_to_iter() where only ITER_IOVEC attempts might return a short copy.
35644e55997SRandy Dunlap  *
35744e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
358bf3eeb9bSDan Williams  */
359ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
3608780356eSDan Williams {
361a41dad90SAl Viro 	if (WARN_ON_ONCE(i->data_source))
362a41dad90SAl Viro 		return 0;
363fcb14cb1SAl Viro 	if (user_backed_iter(i))
3648780356eSDan Williams 		might_fault();
3657baa5099SAl Viro 	__iterate_and_advance(i, bytes, base, len, off,
3667baa5099SAl Viro 		copyout_mc(base, addr + off, len),
3677baa5099SAl Viro 		copy_mc_to_kernel(base, addr + off, len)
3688780356eSDan Williams 	)
3698780356eSDan Williams 
3708780356eSDan Williams 	return bytes;
3718780356eSDan Williams }
372ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
373ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */
3748780356eSDan Williams 
375245f0922SKefeng Wang static void *memcpy_from_iter(struct iov_iter *i, void *to, const void *from,
376245f0922SKefeng Wang 				 size_t size)
377245f0922SKefeng Wang {
378245f0922SKefeng Wang 	if (iov_iter_is_copy_mc(i))
379245f0922SKefeng Wang 		return (void *)copy_mc_to_kernel(to, from, size);
380245f0922SKefeng Wang 	return memcpy(to, from, size);
381245f0922SKefeng Wang }
382245f0922SKefeng Wang 
383aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
384d879cb83SAl Viro {
385a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source))
386241699cdSAl Viro 		return 0;
387a41dad90SAl Viro 
388fcb14cb1SAl Viro 	if (user_backed_iter(i))
38909fc68dcSAl Viro 		might_fault();
3907baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
3917baa5099SAl Viro 		copyin(addr + off, base, len),
392245f0922SKefeng Wang 		memcpy_from_iter(i, addr + off, base, len)
393d879cb83SAl Viro 	)
394d879cb83SAl Viro 
395d879cb83SAl Viro 	return bytes;
396d879cb83SAl Viro }
397aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter);
398d879cb83SAl Viro 
399aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
400d879cb83SAl Viro {
401a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source))
402241699cdSAl Viro 		return 0;
403a41dad90SAl Viro 
4047baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
4057baa5099SAl Viro 		__copy_from_user_inatomic_nocache(addr + off, base, len),
4067baa5099SAl Viro 		memcpy(addr + off, base, len)
407d879cb83SAl Viro 	)
408d879cb83SAl Viro 
409d879cb83SAl Viro 	return bytes;
410d879cb83SAl Viro }
411aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache);
412d879cb83SAl Viro 
4130aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
414abd08d7dSDan Williams /**
415abd08d7dSDan Williams  * _copy_from_iter_flushcache - write destination through cpu cache
416abd08d7dSDan Williams  * @addr: destination kernel address
417abd08d7dSDan Williams  * @bytes: total transfer length
41844e55997SRandy Dunlap  * @i: source iterator
419abd08d7dSDan Williams  *
420abd08d7dSDan Williams  * The pmem driver arranges for filesystem-dax to use this facility via
421abd08d7dSDan Williams  * dax_copy_from_iter() for ensuring that writes to persistent memory
422abd08d7dSDan Williams  * are flushed through the CPU cache. It is differentiated from
423abd08d7dSDan Williams  * _copy_from_iter_nocache() in that guarantees all data is flushed for
424abd08d7dSDan Williams  * all iterator types. The _copy_from_iter_nocache() only attempts to
425abd08d7dSDan Williams  * bypass the cache for the ITER_IOVEC case, and on some archs may use
426abd08d7dSDan Williams  * instructions that strand dirty-data in the cache.
42744e55997SRandy Dunlap  *
42844e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
429abd08d7dSDan Williams  */
4306a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
4310aed55afSDan Williams {
432a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source))
4330aed55afSDan Williams 		return 0;
434a41dad90SAl Viro 
4357baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
4367baa5099SAl Viro 		__copy_from_user_flushcache(addr + off, base, len),
4377baa5099SAl Viro 		memcpy_flushcache(addr + off, base, len)
4380aed55afSDan Williams 	)
4390aed55afSDan Williams 
4400aed55afSDan Williams 	return bytes;
4410aed55afSDan Williams }
4426a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
4430aed55afSDan Williams #endif
4440aed55afSDan Williams 
44572e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
44672e809edSAl Viro {
4476daef95bSEric Dumazet 	struct page *head;
4486daef95bSEric Dumazet 	size_t v = n + offset;
4496daef95bSEric Dumazet 
4506daef95bSEric Dumazet 	/*
4516daef95bSEric Dumazet 	 * The general case needs to access the page order in order
4526daef95bSEric Dumazet 	 * to compute the page size.
4536daef95bSEric Dumazet 	 * However, we mostly deal with order-0 pages and thus can
4546daef95bSEric Dumazet 	 * avoid a possible cache line miss for requests that fit all
4556daef95bSEric Dumazet 	 * page orders.
4566daef95bSEric Dumazet 	 */
4576daef95bSEric Dumazet 	if (n <= v && v <= PAGE_SIZE)
4586daef95bSEric Dumazet 		return true;
4596daef95bSEric Dumazet 
4606daef95bSEric Dumazet 	head = compound_head(page);
4616daef95bSEric Dumazet 	v += (page - head) << PAGE_SHIFT;
462a90bcb86SPetar Penkov 
46340a86061SAl Viro 	if (WARN_ON(n > v || v > page_size(head)))
46472e809edSAl Viro 		return false;
46540a86061SAl Viro 	return true;
46672e809edSAl Viro }
467cbbd26b8SAl Viro 
46808aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
46908aa6479SAl Viro 			 struct iov_iter *i)
47008aa6479SAl Viro {
47108aa6479SAl Viro 	size_t res = 0;
47240a86061SAl Viro 	if (!page_copy_sane(page, offset, bytes))
47308aa6479SAl Viro 		return 0;
474a41dad90SAl Viro 	if (WARN_ON_ONCE(i->data_source))
475a41dad90SAl Viro 		return 0;
47608aa6479SAl Viro 	page += offset / PAGE_SIZE; // first subpage
47708aa6479SAl Viro 	offset %= PAGE_SIZE;
47808aa6479SAl Viro 	while (1) {
479f0f6b614SAl Viro 		void *kaddr = kmap_local_page(page);
480f0f6b614SAl Viro 		size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
481f0f6b614SAl Viro 		n = _copy_to_iter(kaddr + offset, n, i);
482f0f6b614SAl Viro 		kunmap_local(kaddr);
48308aa6479SAl Viro 		res += n;
48408aa6479SAl Viro 		bytes -= n;
48508aa6479SAl Viro 		if (!bytes || !n)
48608aa6479SAl Viro 			break;
48708aa6479SAl Viro 		offset += n;
48808aa6479SAl Viro 		if (offset == PAGE_SIZE) {
48908aa6479SAl Viro 			page++;
49008aa6479SAl Viro 			offset = 0;
49108aa6479SAl Viro 		}
49208aa6479SAl Viro 	}
49308aa6479SAl Viro 	return res;
49408aa6479SAl Viro }
495d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter);
496d879cb83SAl Viro 
4974f80818bSLorenzo Stoakes size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, size_t bytes,
4984f80818bSLorenzo Stoakes 				 struct iov_iter *i)
4994f80818bSLorenzo Stoakes {
5004f80818bSLorenzo Stoakes 	size_t res = 0;
5014f80818bSLorenzo Stoakes 
5024f80818bSLorenzo Stoakes 	if (!page_copy_sane(page, offset, bytes))
5034f80818bSLorenzo Stoakes 		return 0;
5044f80818bSLorenzo Stoakes 	if (WARN_ON_ONCE(i->data_source))
5054f80818bSLorenzo Stoakes 		return 0;
5064f80818bSLorenzo Stoakes 	page += offset / PAGE_SIZE; // first subpage
5074f80818bSLorenzo Stoakes 	offset %= PAGE_SIZE;
5084f80818bSLorenzo Stoakes 	while (1) {
5094f80818bSLorenzo Stoakes 		void *kaddr = kmap_local_page(page);
5104f80818bSLorenzo Stoakes 		size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
5114f80818bSLorenzo Stoakes 
5124f80818bSLorenzo Stoakes 		iterate_and_advance(i, n, base, len, off,
5134f80818bSLorenzo Stoakes 			copyout_nofault(base, kaddr + offset + off, len),
5144f80818bSLorenzo Stoakes 			memcpy(base, kaddr + offset + off, len)
5154f80818bSLorenzo Stoakes 		)
5164f80818bSLorenzo Stoakes 		kunmap_local(kaddr);
5174f80818bSLorenzo Stoakes 		res += n;
5184f80818bSLorenzo Stoakes 		bytes -= n;
5194f80818bSLorenzo Stoakes 		if (!bytes || !n)
5204f80818bSLorenzo Stoakes 			break;
5214f80818bSLorenzo Stoakes 		offset += n;
5224f80818bSLorenzo Stoakes 		if (offset == PAGE_SIZE) {
5234f80818bSLorenzo Stoakes 			page++;
5244f80818bSLorenzo Stoakes 			offset = 0;
5254f80818bSLorenzo Stoakes 		}
5264f80818bSLorenzo Stoakes 	}
5274f80818bSLorenzo Stoakes 	return res;
5284f80818bSLorenzo Stoakes }
5294f80818bSLorenzo Stoakes EXPORT_SYMBOL(copy_page_to_iter_nofault);
5304f80818bSLorenzo Stoakes 
531d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
532d879cb83SAl Viro 			 struct iov_iter *i)
533d879cb83SAl Viro {
534c03f05f1SAl Viro 	size_t res = 0;
535c03f05f1SAl Viro 	if (!page_copy_sane(page, offset, bytes))
53628f38db7SAl Viro 		return 0;
537c03f05f1SAl Viro 	page += offset / PAGE_SIZE; // first subpage
538c03f05f1SAl Viro 	offset %= PAGE_SIZE;
539c03f05f1SAl Viro 	while (1) {
540c03f05f1SAl Viro 		void *kaddr = kmap_local_page(page);
541c03f05f1SAl Viro 		size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
542c03f05f1SAl Viro 		n = _copy_from_iter(kaddr + offset, n, i);
543c03f05f1SAl Viro 		kunmap_local(kaddr);
544c03f05f1SAl Viro 		res += n;
545c03f05f1SAl Viro 		bytes -= n;
546c03f05f1SAl Viro 		if (!bytes || !n)
547c03f05f1SAl Viro 			break;
548c03f05f1SAl Viro 		offset += n;
549c03f05f1SAl Viro 		if (offset == PAGE_SIZE) {
550c03f05f1SAl Viro 			page++;
551c03f05f1SAl Viro 			offset = 0;
552c03f05f1SAl Viro 		}
553c03f05f1SAl Viro 	}
554c03f05f1SAl Viro 	return res;
555d879cb83SAl Viro }
556d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter);
557d879cb83SAl Viro 
558d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
559d879cb83SAl Viro {
5607baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, count,
5617baa5099SAl Viro 		clear_user(base, len),
5627baa5099SAl Viro 		memset(base, 0, len)
563d879cb83SAl Viro 	)
564d879cb83SAl Viro 
565d879cb83SAl Viro 	return bytes;
566d879cb83SAl Viro }
567d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero);
568d879cb83SAl Viro 
569f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
570f0b65f39SAl Viro 				  struct iov_iter *i)
571d879cb83SAl Viro {
572d879cb83SAl Viro 	char *kaddr = kmap_atomic(page), *p = kaddr + offset;
57340a86061SAl Viro 	if (!page_copy_sane(page, offset, bytes)) {
57472e809edSAl Viro 		kunmap_atomic(kaddr);
57572e809edSAl Viro 		return 0;
57672e809edSAl Viro 	}
577a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source)) {
578241699cdSAl Viro 		kunmap_atomic(kaddr);
579241699cdSAl Viro 		return 0;
580241699cdSAl Viro 	}
5817baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
5827baa5099SAl Viro 		copyin(p + off, base, len),
583245f0922SKefeng Wang 		memcpy_from_iter(i, p + off, base, len)
584d879cb83SAl Viro 	)
585d879cb83SAl Viro 	kunmap_atomic(kaddr);
586d879cb83SAl Viro 	return bytes;
587d879cb83SAl Viro }
588f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic);
589d879cb83SAl Viro 
59054c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
59154c8195bSPavel Begunkov {
59218fa9af7SAl Viro 	const struct bio_vec *bvec, *end;
59354c8195bSPavel Begunkov 
59418fa9af7SAl Viro 	if (!i->count)
59518fa9af7SAl Viro 		return;
59618fa9af7SAl Viro 	i->count -= size;
59754c8195bSPavel Begunkov 
59818fa9af7SAl Viro 	size += i->iov_offset;
59918fa9af7SAl Viro 
60018fa9af7SAl Viro 	for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
60118fa9af7SAl Viro 		if (likely(size < bvec->bv_len))
60218fa9af7SAl Viro 			break;
60318fa9af7SAl Viro 		size -= bvec->bv_len;
60418fa9af7SAl Viro 	}
60518fa9af7SAl Viro 	i->iov_offset = size;
60618fa9af7SAl Viro 	i->nr_segs -= bvec - i->bvec;
60718fa9af7SAl Viro 	i->bvec = bvec;
60854c8195bSPavel Begunkov }
60954c8195bSPavel Begunkov 
610185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
611185ac4d4SAl Viro {
612185ac4d4SAl Viro 	const struct iovec *iov, *end;
613185ac4d4SAl Viro 
614185ac4d4SAl Viro 	if (!i->count)
615185ac4d4SAl Viro 		return;
616185ac4d4SAl Viro 	i->count -= size;
617185ac4d4SAl Viro 
618185ac4d4SAl Viro 	size += i->iov_offset; // from beginning of current segment
619de4f5fedSJens Axboe 	for (iov = iter_iov(i), end = iov + i->nr_segs; iov < end; iov++) {
620185ac4d4SAl Viro 		if (likely(size < iov->iov_len))
621185ac4d4SAl Viro 			break;
622185ac4d4SAl Viro 		size -= iov->iov_len;
623185ac4d4SAl Viro 	}
624185ac4d4SAl Viro 	i->iov_offset = size;
625de4f5fedSJens Axboe 	i->nr_segs -= iov - iter_iov(i);
626de4f5fedSJens Axboe 	i->__iov = iov;
627185ac4d4SAl Viro }
628185ac4d4SAl Viro 
629d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size)
630d879cb83SAl Viro {
6313b3fc051SAl Viro 	if (unlikely(i->count < size))
6323b3fc051SAl Viro 		size = i->count;
633fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
634fcb14cb1SAl Viro 		i->iov_offset += size;
635fcb14cb1SAl Viro 		i->count -= size;
636fcb14cb1SAl Viro 	} else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
637185ac4d4SAl Viro 		/* iovec and kvec have identical layouts */
638185ac4d4SAl Viro 		iov_iter_iovec_advance(i, size);
639185ac4d4SAl Viro 	} else if (iov_iter_is_bvec(i)) {
640185ac4d4SAl Viro 		iov_iter_bvec_advance(i, size);
641185ac4d4SAl Viro 	} else if (iov_iter_is_discard(i)) {
642185ac4d4SAl Viro 		i->count -= size;
6437ff50620SDavid Howells 	}
644d879cb83SAl Viro }
645d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance);
646d879cb83SAl Viro 
64727c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll)
64827c0e374SAl Viro {
64927c0e374SAl Viro 	if (!unroll)
65027c0e374SAl Viro 		return;
6515b47d59aSAl Viro 	if (WARN_ON(unroll > MAX_RW_COUNT))
6525b47d59aSAl Viro 		return;
65327c0e374SAl Viro 	i->count += unroll;
6549ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_discard(i)))
6559ea9ce04SDavid Howells 		return;
65627c0e374SAl Viro 	if (unroll <= i->iov_offset) {
65727c0e374SAl Viro 		i->iov_offset -= unroll;
65827c0e374SAl Viro 		return;
65927c0e374SAl Viro 	}
66027c0e374SAl Viro 	unroll -= i->iov_offset;
661fcb14cb1SAl Viro 	if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
6627ff50620SDavid Howells 		BUG(); /* We should never go beyond the start of the specified
6637ff50620SDavid Howells 			* range since we might then be straying into pages that
6647ff50620SDavid Howells 			* aren't pinned.
6657ff50620SDavid Howells 			*/
6667ff50620SDavid Howells 	} else if (iov_iter_is_bvec(i)) {
66727c0e374SAl Viro 		const struct bio_vec *bvec = i->bvec;
66827c0e374SAl Viro 		while (1) {
66927c0e374SAl Viro 			size_t n = (--bvec)->bv_len;
67027c0e374SAl Viro 			i->nr_segs++;
67127c0e374SAl Viro 			if (unroll <= n) {
67227c0e374SAl Viro 				i->bvec = bvec;
67327c0e374SAl Viro 				i->iov_offset = n - unroll;
67427c0e374SAl Viro 				return;
67527c0e374SAl Viro 			}
67627c0e374SAl Viro 			unroll -= n;
67727c0e374SAl Viro 		}
67827c0e374SAl Viro 	} else { /* same logics for iovec and kvec */
679de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i);
68027c0e374SAl Viro 		while (1) {
68127c0e374SAl Viro 			size_t n = (--iov)->iov_len;
68227c0e374SAl Viro 			i->nr_segs++;
68327c0e374SAl Viro 			if (unroll <= n) {
684de4f5fedSJens Axboe 				i->__iov = iov;
68527c0e374SAl Viro 				i->iov_offset = n - unroll;
68627c0e374SAl Viro 				return;
68727c0e374SAl Viro 			}
68827c0e374SAl Viro 			unroll -= n;
68927c0e374SAl Viro 		}
69027c0e374SAl Viro 	}
69127c0e374SAl Viro }
69227c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert);
69327c0e374SAl Viro 
694d879cb83SAl Viro /*
695d879cb83SAl Viro  * Return the count of just the current iov_iter segment.
696d879cb83SAl Viro  */
697d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i)
698d879cb83SAl Viro {
69928f38db7SAl Viro 	if (i->nr_segs > 1) {
70028f38db7SAl Viro 		if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
701de4f5fedSJens Axboe 			return min(i->count, iter_iov(i)->iov_len - i->iov_offset);
7027ff50620SDavid Howells 		if (iov_iter_is_bvec(i))
703d879cb83SAl Viro 			return min(i->count, i->bvec->bv_len - i->iov_offset);
70428f38db7SAl Viro 	}
70528f38db7SAl Viro 	return i->count;
706d879cb83SAl Viro }
707d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count);
708d879cb83SAl Viro 
709aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
710d879cb83SAl Viro 			const struct kvec *kvec, unsigned long nr_segs,
711d879cb83SAl Viro 			size_t count)
712d879cb83SAl Viro {
713aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
7148cd54c1cSAl Viro 	*i = (struct iov_iter){
7158cd54c1cSAl Viro 		.iter_type = ITER_KVEC,
716245f0922SKefeng Wang 		.copy_mc = false,
7178cd54c1cSAl Viro 		.data_source = direction,
7188cd54c1cSAl Viro 		.kvec = kvec,
7198cd54c1cSAl Viro 		.nr_segs = nr_segs,
7208cd54c1cSAl Viro 		.iov_offset = 0,
7218cd54c1cSAl Viro 		.count = count
7228cd54c1cSAl Viro 	};
723d879cb83SAl Viro }
724d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec);
725d879cb83SAl Viro 
726aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
727d879cb83SAl Viro 			const struct bio_vec *bvec, unsigned long nr_segs,
728d879cb83SAl Viro 			size_t count)
729d879cb83SAl Viro {
730aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
7318cd54c1cSAl Viro 	*i = (struct iov_iter){
7328cd54c1cSAl Viro 		.iter_type = ITER_BVEC,
733245f0922SKefeng Wang 		.copy_mc = false,
7348cd54c1cSAl Viro 		.data_source = direction,
7358cd54c1cSAl Viro 		.bvec = bvec,
7368cd54c1cSAl Viro 		.nr_segs = nr_segs,
7378cd54c1cSAl Viro 		.iov_offset = 0,
7388cd54c1cSAl Viro 		.count = count
7398cd54c1cSAl Viro 	};
740d879cb83SAl Viro }
741d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec);
742d879cb83SAl Viro 
7439ea9ce04SDavid Howells /**
7447ff50620SDavid Howells  * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
7457ff50620SDavid Howells  * @i: The iterator to initialise.
7467ff50620SDavid Howells  * @direction: The direction of the transfer.
7477ff50620SDavid Howells  * @xarray: The xarray to access.
7487ff50620SDavid Howells  * @start: The start file position.
7497ff50620SDavid Howells  * @count: The size of the I/O buffer in bytes.
7507ff50620SDavid Howells  *
7517ff50620SDavid Howells  * Set up an I/O iterator to either draw data out of the pages attached to an
7527ff50620SDavid Howells  * inode or to inject data into those pages.  The pages *must* be prevented
7537ff50620SDavid Howells  * from evaporation, either by taking a ref on them or locking them by the
7547ff50620SDavid Howells  * caller.
7557ff50620SDavid Howells  */
7567ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
7577ff50620SDavid Howells 		     struct xarray *xarray, loff_t start, size_t count)
7587ff50620SDavid Howells {
7597ff50620SDavid Howells 	BUG_ON(direction & ~1);
7608cd54c1cSAl Viro 	*i = (struct iov_iter) {
7618cd54c1cSAl Viro 		.iter_type = ITER_XARRAY,
762245f0922SKefeng Wang 		.copy_mc = false,
7638cd54c1cSAl Viro 		.data_source = direction,
7648cd54c1cSAl Viro 		.xarray = xarray,
7658cd54c1cSAl Viro 		.xarray_start = start,
7668cd54c1cSAl Viro 		.count = count,
7678cd54c1cSAl Viro 		.iov_offset = 0
7688cd54c1cSAl Viro 	};
7697ff50620SDavid Howells }
7707ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray);
7717ff50620SDavid Howells 
7727ff50620SDavid Howells /**
7739ea9ce04SDavid Howells  * iov_iter_discard - Initialise an I/O iterator that discards data
7749ea9ce04SDavid Howells  * @i: The iterator to initialise.
7759ea9ce04SDavid Howells  * @direction: The direction of the transfer.
7769ea9ce04SDavid Howells  * @count: The size of the I/O buffer in bytes.
7779ea9ce04SDavid Howells  *
7789ea9ce04SDavid Howells  * Set up an I/O iterator that just discards everything that's written to it.
7799ea9ce04SDavid Howells  * It's only available as a READ iterator.
7809ea9ce04SDavid Howells  */
7819ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
7829ea9ce04SDavid Howells {
7839ea9ce04SDavid Howells 	BUG_ON(direction != READ);
7848cd54c1cSAl Viro 	*i = (struct iov_iter){
7858cd54c1cSAl Viro 		.iter_type = ITER_DISCARD,
786245f0922SKefeng Wang 		.copy_mc = false,
7878cd54c1cSAl Viro 		.data_source = false,
7888cd54c1cSAl Viro 		.count = count,
7898cd54c1cSAl Viro 		.iov_offset = 0
7908cd54c1cSAl Viro 	};
7919ea9ce04SDavid Howells }
7929ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard);
7939ea9ce04SDavid Howells 
794cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
795cfa320f7SKeith Busch 				   unsigned len_mask)
796cfa320f7SKeith Busch {
797cfa320f7SKeith Busch 	size_t size = i->count;
798cfa320f7SKeith Busch 	size_t skip = i->iov_offset;
799cfa320f7SKeith Busch 	unsigned k;
800cfa320f7SKeith Busch 
801cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
802de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
803de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
804cfa320f7SKeith Busch 
805cfa320f7SKeith Busch 		if (len > size)
806cfa320f7SKeith Busch 			len = size;
807cfa320f7SKeith Busch 		if (len & len_mask)
808cfa320f7SKeith Busch 			return false;
809de4f5fedSJens Axboe 		if ((unsigned long)(iov->iov_base + skip) & addr_mask)
810cfa320f7SKeith Busch 			return false;
811cfa320f7SKeith Busch 
812cfa320f7SKeith Busch 		size -= len;
813cfa320f7SKeith Busch 		if (!size)
814cfa320f7SKeith Busch 			break;
815cfa320f7SKeith Busch 	}
816cfa320f7SKeith Busch 	return true;
817cfa320f7SKeith Busch }
818cfa320f7SKeith Busch 
819cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
820cfa320f7SKeith Busch 				  unsigned len_mask)
821cfa320f7SKeith Busch {
822cfa320f7SKeith Busch 	size_t size = i->count;
823cfa320f7SKeith Busch 	unsigned skip = i->iov_offset;
824cfa320f7SKeith Busch 	unsigned k;
825cfa320f7SKeith Busch 
826cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
827cfa320f7SKeith Busch 		size_t len = i->bvec[k].bv_len - skip;
828cfa320f7SKeith Busch 
829cfa320f7SKeith Busch 		if (len > size)
830cfa320f7SKeith Busch 			len = size;
831cfa320f7SKeith Busch 		if (len & len_mask)
832cfa320f7SKeith Busch 			return false;
833cfa320f7SKeith Busch 		if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
834cfa320f7SKeith Busch 			return false;
835cfa320f7SKeith Busch 
836cfa320f7SKeith Busch 		size -= len;
837cfa320f7SKeith Busch 		if (!size)
838cfa320f7SKeith Busch 			break;
839cfa320f7SKeith Busch 	}
840cfa320f7SKeith Busch 	return true;
841cfa320f7SKeith Busch }
842cfa320f7SKeith Busch 
843cfa320f7SKeith Busch /**
844cfa320f7SKeith Busch  * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
845cfa320f7SKeith Busch  * 	are aligned to the parameters.
846cfa320f7SKeith Busch  *
847cfa320f7SKeith Busch  * @i: &struct iov_iter to restore
848cfa320f7SKeith Busch  * @addr_mask: bit mask to check against the iov element's addresses
849cfa320f7SKeith Busch  * @len_mask: bit mask to check against the iov element's lengths
850cfa320f7SKeith Busch  *
851cfa320f7SKeith Busch  * Return: false if any addresses or lengths intersect with the provided masks
852cfa320f7SKeith Busch  */
853cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
854cfa320f7SKeith Busch 			 unsigned len_mask)
855cfa320f7SKeith Busch {
856fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
857fcb14cb1SAl Viro 		if (i->count & len_mask)
858fcb14cb1SAl Viro 			return false;
859fcb14cb1SAl Viro 		if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
860fcb14cb1SAl Viro 			return false;
861fcb14cb1SAl Viro 		return true;
862fcb14cb1SAl Viro 	}
863fcb14cb1SAl Viro 
864cfa320f7SKeith Busch 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
865cfa320f7SKeith Busch 		return iov_iter_aligned_iovec(i, addr_mask, len_mask);
866cfa320f7SKeith Busch 
867cfa320f7SKeith Busch 	if (iov_iter_is_bvec(i))
868cfa320f7SKeith Busch 		return iov_iter_aligned_bvec(i, addr_mask, len_mask);
869cfa320f7SKeith Busch 
870cfa320f7SKeith Busch 	if (iov_iter_is_xarray(i)) {
871cfa320f7SKeith Busch 		if (i->count & len_mask)
872cfa320f7SKeith Busch 			return false;
873cfa320f7SKeith Busch 		if ((i->xarray_start + i->iov_offset) & addr_mask)
874cfa320f7SKeith Busch 			return false;
875cfa320f7SKeith Busch 	}
876cfa320f7SKeith Busch 
877cfa320f7SKeith Busch 	return true;
878cfa320f7SKeith Busch }
879cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
880cfa320f7SKeith Busch 
8819221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
882d879cb83SAl Viro {
883d879cb83SAl Viro 	unsigned long res = 0;
884d879cb83SAl Viro 	size_t size = i->count;
8859221d2e3SAl Viro 	size_t skip = i->iov_offset;
8869221d2e3SAl Viro 	unsigned k;
887d879cb83SAl Viro 
8889221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
889de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
890de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
8919221d2e3SAl Viro 		if (len) {
892de4f5fedSJens Axboe 			res |= (unsigned long)iov->iov_base + skip;
8939221d2e3SAl Viro 			if (len > size)
8949221d2e3SAl Viro 				len = size;
8959221d2e3SAl Viro 			res |= len;
8969221d2e3SAl Viro 			size -= len;
8979221d2e3SAl Viro 			if (!size)
8989221d2e3SAl Viro 				break;
8999221d2e3SAl Viro 		}
9009221d2e3SAl Viro 	}
9019221d2e3SAl Viro 	return res;
9029221d2e3SAl Viro }
9039221d2e3SAl Viro 
9049221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
9059221d2e3SAl Viro {
9069221d2e3SAl Viro 	unsigned res = 0;
9079221d2e3SAl Viro 	size_t size = i->count;
9089221d2e3SAl Viro 	unsigned skip = i->iov_offset;
9099221d2e3SAl Viro 	unsigned k;
9109221d2e3SAl Viro 
9119221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
9129221d2e3SAl Viro 		size_t len = i->bvec[k].bv_len - skip;
9139221d2e3SAl Viro 		res |= (unsigned long)i->bvec[k].bv_offset + skip;
9149221d2e3SAl Viro 		if (len > size)
9159221d2e3SAl Viro 			len = size;
9169221d2e3SAl Viro 		res |= len;
9179221d2e3SAl Viro 		size -= len;
9189221d2e3SAl Viro 		if (!size)
9199221d2e3SAl Viro 			break;
9209221d2e3SAl Viro 	}
9219221d2e3SAl Viro 	return res;
9229221d2e3SAl Viro }
9239221d2e3SAl Viro 
9249221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i)
9259221d2e3SAl Viro {
926fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
927fcb14cb1SAl Viro 		size_t size = i->count;
928fcb14cb1SAl Viro 		if (size)
929fcb14cb1SAl Viro 			return ((unsigned long)i->ubuf + i->iov_offset) | size;
930fcb14cb1SAl Viro 		return 0;
931fcb14cb1SAl Viro 	}
932fcb14cb1SAl Viro 
9339221d2e3SAl Viro 	/* iovec and kvec have identical layouts */
9349221d2e3SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
9359221d2e3SAl Viro 		return iov_iter_alignment_iovec(i);
9369221d2e3SAl Viro 
9379221d2e3SAl Viro 	if (iov_iter_is_bvec(i))
9389221d2e3SAl Viro 		return iov_iter_alignment_bvec(i);
9399221d2e3SAl Viro 
9409221d2e3SAl Viro 	if (iov_iter_is_xarray(i))
9413d14ec1fSDavid Howells 		return (i->xarray_start + i->iov_offset) | i->count;
9429221d2e3SAl Viro 
9439221d2e3SAl Viro 	return 0;
944d879cb83SAl Viro }
945d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment);
946d879cb83SAl Viro 
947357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
948357f435dSAl Viro {
949357f435dSAl Viro 	unsigned long res = 0;
950610c7a71SAl Viro 	unsigned long v = 0;
951357f435dSAl Viro 	size_t size = i->count;
952610c7a71SAl Viro 	unsigned k;
953357f435dSAl Viro 
954fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
955fcb14cb1SAl Viro 		return 0;
956fcb14cb1SAl Viro 
957610c7a71SAl Viro 	if (WARN_ON(!iter_is_iovec(i)))
958241699cdSAl Viro 		return ~0U;
959241699cdSAl Viro 
960610c7a71SAl Viro 	for (k = 0; k < i->nr_segs; k++) {
961de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
962de4f5fedSJens Axboe 		if (iov->iov_len) {
963de4f5fedSJens Axboe 			unsigned long base = (unsigned long)iov->iov_base;
964610c7a71SAl Viro 			if (v) // if not the first one
965610c7a71SAl Viro 				res |= base | v; // this start | previous end
966de4f5fedSJens Axboe 			v = base + iov->iov_len;
967de4f5fedSJens Axboe 			if (size <= iov->iov_len)
968610c7a71SAl Viro 				break;
969de4f5fedSJens Axboe 			size -= iov->iov_len;
970610c7a71SAl Viro 		}
971610c7a71SAl Viro 	}
972357f435dSAl Viro 	return res;
973357f435dSAl Viro }
974357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment);
975357f435dSAl Viro 
9763cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size,
9773cf42da3SAl Viro 			    size_t start, unsigned int maxpages)
978acbdeb83SAl Viro {
9793cf42da3SAl Viro 	unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE);
9803cf42da3SAl Viro 
9813cf42da3SAl Viro 	if (count > maxpages)
9823cf42da3SAl Viro 		count = maxpages;
9833cf42da3SAl Viro 	WARN_ON(!count);	// caller should've prevented that
9843cf42da3SAl Viro 	if (!*res) {
9853cf42da3SAl Viro 		*res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
9863cf42da3SAl Viro 		if (!*res)
9873cf42da3SAl Viro 			return 0;
9883cf42da3SAl Viro 	}
9893cf42da3SAl Viro 	return count;
990acbdeb83SAl Viro }
991acbdeb83SAl Viro 
9927ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
9937ff50620SDavid Howells 					  pgoff_t index, unsigned int nr_pages)
9947ff50620SDavid Howells {
9957ff50620SDavid Howells 	XA_STATE(xas, xa, index);
9967ff50620SDavid Howells 	struct page *page;
9977ff50620SDavid Howells 	unsigned int ret = 0;
9987ff50620SDavid Howells 
9997ff50620SDavid Howells 	rcu_read_lock();
10007ff50620SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
10017ff50620SDavid Howells 		if (xas_retry(&xas, page))
10027ff50620SDavid Howells 			continue;
10037ff50620SDavid Howells 
10047ff50620SDavid Howells 		/* Has the page moved or been split? */
10057ff50620SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
10067ff50620SDavid Howells 			xas_reset(&xas);
10077ff50620SDavid Howells 			continue;
10087ff50620SDavid Howells 		}
10097ff50620SDavid Howells 
10107ff50620SDavid Howells 		pages[ret] = find_subpage(page, xas.xa_index);
10117ff50620SDavid Howells 		get_page(pages[ret]);
10127ff50620SDavid Howells 		if (++ret == nr_pages)
10137ff50620SDavid Howells 			break;
10147ff50620SDavid Howells 	}
10157ff50620SDavid Howells 	rcu_read_unlock();
10167ff50620SDavid Howells 	return ret;
10177ff50620SDavid Howells }
10187ff50620SDavid Howells 
10197ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i,
102068fe506fSAl Viro 				     struct page ***pages, size_t maxsize,
10217ff50620SDavid Howells 				     unsigned maxpages, size_t *_start_offset)
10227ff50620SDavid Howells {
10233cf42da3SAl Viro 	unsigned nr, offset, count;
10243cf42da3SAl Viro 	pgoff_t index;
10257ff50620SDavid Howells 	loff_t pos;
10267ff50620SDavid Howells 
10277ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
10287ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
10297ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
10307ff50620SDavid Howells 	*_start_offset = offset;
10317ff50620SDavid Howells 
10323cf42da3SAl Viro 	count = want_pages_array(pages, maxsize, offset, maxpages);
10333cf42da3SAl Viro 	if (!count)
103468fe506fSAl Viro 		return -ENOMEM;
103568fe506fSAl Viro 	nr = iter_xarray_populate_pages(*pages, i->xarray, index, count);
10367ff50620SDavid Howells 	if (nr == 0)
10377ff50620SDavid Howells 		return 0;
10387ff50620SDavid Howells 
1039eba2d3d7SAl Viro 	maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1040310d9d5aSAl Viro 	i->iov_offset += maxsize;
1041310d9d5aSAl Viro 	i->count -= maxsize;
1042eba2d3d7SAl Viro 	return maxsize;
10437ff50620SDavid Howells }
10447ff50620SDavid Howells 
1045fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
1046dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
10473d671ca6SAl Viro {
10483d671ca6SAl Viro 	size_t skip;
10493d671ca6SAl Viro 	long k;
10503d671ca6SAl Viro 
1051fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1052fcb14cb1SAl Viro 		return (unsigned long)i->ubuf + i->iov_offset;
1053fcb14cb1SAl Viro 
10543d671ca6SAl Viro 	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1055de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
1056de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
10573d671ca6SAl Viro 
10583d671ca6SAl Viro 		if (unlikely(!len))
10593d671ca6SAl Viro 			continue;
106059dbd7d0SAl Viro 		if (*size > len)
10613d671ca6SAl Viro 			*size = len;
1062de4f5fedSJens Axboe 		return (unsigned long)iov->iov_base + skip;
10633d671ca6SAl Viro 	}
10643d671ca6SAl Viro 	BUG(); // if it had been empty, we wouldn't get called
10653d671ca6SAl Viro }
10663d671ca6SAl Viro 
10673d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */
10683d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i,
106959dbd7d0SAl Viro 				       size_t *size, size_t *start)
10703d671ca6SAl Viro {
10713d671ca6SAl Viro 	struct page *page;
10723d671ca6SAl Viro 	size_t skip = i->iov_offset, len;
10733d671ca6SAl Viro 
10743d671ca6SAl Viro 	len = i->bvec->bv_len - skip;
107559dbd7d0SAl Viro 	if (*size > len)
107659dbd7d0SAl Viro 		*size = len;
10773d671ca6SAl Viro 	skip += i->bvec->bv_offset;
10783d671ca6SAl Viro 	page = i->bvec->bv_page + skip / PAGE_SIZE;
1079dda8e5d1SAl Viro 	*start = skip % PAGE_SIZE;
10803d671ca6SAl Viro 	return page;
10813d671ca6SAl Viro }
10823d671ca6SAl Viro 
108391329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
1084d879cb83SAl Viro 		   struct page ***pages, size_t maxsize,
1085*84bd06c6SChristoph Hellwig 		   unsigned int maxpages, size_t *start)
1086d879cb83SAl Viro {
1087f62e52d1SDavid Howells 	unsigned int n, gup_flags = 0;
1088d879cb83SAl Viro 
1089d879cb83SAl Viro 	if (maxsize > i->count)
1090d879cb83SAl Viro 		maxsize = i->count;
10913d671ca6SAl Viro 	if (!maxsize)
10923d671ca6SAl Viro 		return 0;
10937392ed17SAl Viro 	if (maxsize > MAX_RW_COUNT)
10947392ed17SAl Viro 		maxsize = MAX_RW_COUNT;
1095d879cb83SAl Viro 
1096fcb14cb1SAl Viro 	if (likely(user_backed_iter(i))) {
10973d671ca6SAl Viro 		unsigned long addr;
10983cf42da3SAl Viro 		int res;
10999ea9ce04SDavid Howells 
11003337ab08SAndreas Gruenbacher 		if (iov_iter_rw(i) != WRITE)
11013337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_WRITE;
11023337ab08SAndreas Gruenbacher 		if (i->nofault)
11033337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_NOFAULT;
11043337ab08SAndreas Gruenbacher 
1105dd45ab9dSAl Viro 		addr = first_iovec_segment(i, &maxsize);
1106dd45ab9dSAl Viro 		*start = addr % PAGE_SIZE;
1107dd45ab9dSAl Viro 		addr &= PAGE_MASK;
11083cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
11093cf42da3SAl Viro 		if (!n)
1110d879cb83SAl Viro 			return -ENOMEM;
1111451c0ba9SAl Viro 		res = get_user_pages_fast(addr, n, gup_flags, *pages);
111291329559SAl Viro 		if (unlikely(res <= 0))
1113d879cb83SAl Viro 			return res;
1114eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1115eba2d3d7SAl Viro 		iov_iter_advance(i, maxsize);
1116eba2d3d7SAl Viro 		return maxsize;
11173d671ca6SAl Viro 	}
11183d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
1119451c0ba9SAl Viro 		struct page **p;
11203d671ca6SAl Viro 		struct page *page;
11213d671ca6SAl Viro 
112259dbd7d0SAl Viro 		page = first_bvec_segment(i, &maxsize, start);
11233cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
11243cf42da3SAl Viro 		if (!n)
1125d879cb83SAl Viro 			return -ENOMEM;
11263cf42da3SAl Viro 		p = *pages;
1127dda8e5d1SAl Viro 		for (int k = 0; k < n; k++)
1128eba2d3d7SAl Viro 			get_page(p[k] = page + k);
1129eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1130310d9d5aSAl Viro 		i->count -= maxsize;
1131310d9d5aSAl Viro 		i->iov_offset += maxsize;
1132310d9d5aSAl Viro 		if (i->iov_offset == i->bvec->bv_len) {
1133310d9d5aSAl Viro 			i->iov_offset = 0;
1134310d9d5aSAl Viro 			i->bvec++;
1135310d9d5aSAl Viro 			i->nr_segs--;
1136310d9d5aSAl Viro 		}
1137eba2d3d7SAl Viro 		return maxsize;
11383d671ca6SAl Viro 	}
11393d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
1140451c0ba9SAl Viro 		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1141d879cb83SAl Viro 	return -EFAULT;
1142d879cb83SAl Viro }
114391329559SAl Viro 
1144*84bd06c6SChristoph Hellwig ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
1145*84bd06c6SChristoph Hellwig 		size_t maxsize, unsigned maxpages, size_t *start)
1146451c0ba9SAl Viro {
1147451c0ba9SAl Viro 	if (!maxpages)
1148451c0ba9SAl Viro 		return 0;
1149451c0ba9SAl Viro 	BUG_ON(!pages);
1150451c0ba9SAl Viro 
1151*84bd06c6SChristoph Hellwig 	return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, start);
1152451c0ba9SAl Viro }
1153eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2);
1154451c0ba9SAl Viro 
1155*84bd06c6SChristoph Hellwig ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i,
1156*84bd06c6SChristoph Hellwig 		struct page ***pages, size_t maxsize, size_t *start)
115791329559SAl Viro {
115891329559SAl Viro 	ssize_t len;
115991329559SAl Viro 
116091329559SAl Viro 	*pages = NULL;
116191329559SAl Viro 
1162*84bd06c6SChristoph Hellwig 	len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start);
116391329559SAl Viro 	if (len <= 0) {
116491329559SAl Viro 		kvfree(*pages);
116591329559SAl Viro 		*pages = NULL;
116691329559SAl Viro 	}
116791329559SAl Viro 	return len;
116891329559SAl Viro }
1169eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2);
1170d879cb83SAl Viro 
1171d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1172d879cb83SAl Viro 			       struct iov_iter *i)
1173d879cb83SAl Viro {
1174d879cb83SAl Viro 	__wsum sum, next;
1175d879cb83SAl Viro 	sum = *csum;
1176a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source))
1177241699cdSAl Viro 		return 0;
1178a41dad90SAl Viro 
11797baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off, ({
11807baa5099SAl Viro 		next = csum_and_copy_from_user(base, addr + off, len);
1181d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
11827baa5099SAl Viro 		next ? 0 : len;
1183d879cb83SAl Viro 	}), ({
11847baa5099SAl Viro 		sum = csum_and_memcpy(addr + off, base, len, sum, off);
1185d879cb83SAl Viro 	})
1186d879cb83SAl Viro 	)
1187d879cb83SAl Viro 	*csum = sum;
1188d879cb83SAl Viro 	return bytes;
1189d879cb83SAl Viro }
1190d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter);
1191d879cb83SAl Viro 
119252cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1193d879cb83SAl Viro 			     struct iov_iter *i)
1194d879cb83SAl Viro {
119552cbd23aSWillem de Bruijn 	struct csum_state *csstate = _csstate;
1196d879cb83SAl Viro 	__wsum sum, next;
119778e1f386SAl Viro 
1198a41dad90SAl Viro 	if (WARN_ON_ONCE(i->data_source))
1199a41dad90SAl Viro 		return 0;
120078e1f386SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
1201c67f1fd2SAl Viro 		// can't use csum_memcpy() for that one - data is not copied
1202c67f1fd2SAl Viro 		csstate->csum = csum_block_add(csstate->csum,
1203c67f1fd2SAl Viro 					       csum_partial(addr, bytes, 0),
1204c67f1fd2SAl Viro 					       csstate->off);
1205c67f1fd2SAl Viro 		csstate->off += bytes;
1206c67f1fd2SAl Viro 		return bytes;
1207241699cdSAl Viro 	}
12086852df12SAl Viro 
12096852df12SAl Viro 	sum = csum_shift(csstate->csum, csstate->off);
12103fc40265SDavid Howells 	iterate_and_advance(i, bytes, base, len, off, ({
12117baa5099SAl Viro 		next = csum_and_copy_to_user(addr + off, base, len);
1212d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
12137baa5099SAl Viro 		next ? 0 : len;
1214d879cb83SAl Viro 	}), ({
12157baa5099SAl Viro 		sum = csum_and_memcpy(base, addr + off, len, sum, off);
1216d879cb83SAl Viro 	})
1217d879cb83SAl Viro 	)
1218594e450bSAl Viro 	csstate->csum = csum_shift(sum, csstate->off);
1219594e450bSAl Viro 	csstate->off += bytes;
1220d879cb83SAl Viro 	return bytes;
1221d879cb83SAl Viro }
1222d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter);
1223d879cb83SAl Viro 
1224d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1225d05f4435SSagi Grimberg 		struct iov_iter *i)
1226d05f4435SSagi Grimberg {
12277999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH
1228d05f4435SSagi Grimberg 	struct ahash_request *hash = hashp;
1229d05f4435SSagi Grimberg 	struct scatterlist sg;
1230d05f4435SSagi Grimberg 	size_t copied;
1231d05f4435SSagi Grimberg 
1232d05f4435SSagi Grimberg 	copied = copy_to_iter(addr, bytes, i);
1233d05f4435SSagi Grimberg 	sg_init_one(&sg, addr, copied);
1234d05f4435SSagi Grimberg 	ahash_request_set_crypt(hash, &sg, NULL, copied);
1235d05f4435SSagi Grimberg 	crypto_ahash_update(hash);
1236d05f4435SSagi Grimberg 	return copied;
123727fad74aSYueHaibing #else
123827fad74aSYueHaibing 	return 0;
123927fad74aSYueHaibing #endif
1240d05f4435SSagi Grimberg }
1241d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter);
1242d05f4435SSagi Grimberg 
124366531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages)
1244d879cb83SAl Viro {
124566531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
124666531c65SAl Viro 	const struct iovec *p;
1247d879cb83SAl Viro 	int npages = 0;
1248d879cb83SAl Viro 
1249de4f5fedSJens Axboe 	for (p = iter_iov(i); size; skip = 0, p++) {
125066531c65SAl Viro 		unsigned offs = offset_in_page(p->iov_base + skip);
125166531c65SAl Viro 		size_t len = min(p->iov_len - skip, size);
1252d879cb83SAl Viro 
125366531c65SAl Viro 		if (len) {
125466531c65SAl Viro 			size -= len;
125566531c65SAl Viro 			npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
125666531c65SAl Viro 			if (unlikely(npages > maxpages))
125766531c65SAl Viro 				return maxpages;
125866531c65SAl Viro 		}
125966531c65SAl Viro 	}
126066531c65SAl Viro 	return npages;
126166531c65SAl Viro }
126266531c65SAl Viro 
126366531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages)
126466531c65SAl Viro {
126566531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
126666531c65SAl Viro 	const struct bio_vec *p;
126766531c65SAl Viro 	int npages = 0;
126866531c65SAl Viro 
126966531c65SAl Viro 	for (p = i->bvec; size; skip = 0, p++) {
127066531c65SAl Viro 		unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
127166531c65SAl Viro 		size_t len = min(p->bv_len - skip, size);
127266531c65SAl Viro 
127366531c65SAl Viro 		size -= len;
127466531c65SAl Viro 		npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
127566531c65SAl Viro 		if (unlikely(npages > maxpages))
127666531c65SAl Viro 			return maxpages;
127766531c65SAl Viro 	}
127866531c65SAl Viro 	return npages;
127966531c65SAl Viro }
128066531c65SAl Viro 
128166531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages)
128266531c65SAl Viro {
128366531c65SAl Viro 	if (unlikely(!i->count))
128466531c65SAl Viro 		return 0;
1285fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1286fcb14cb1SAl Viro 		unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1287fcb14cb1SAl Viro 		int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1288fcb14cb1SAl Viro 		return min(npages, maxpages);
1289fcb14cb1SAl Viro 	}
129066531c65SAl Viro 	/* iovec and kvec have identical layouts */
129166531c65SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
129266531c65SAl Viro 		return iov_npages(i, maxpages);
129366531c65SAl Viro 	if (iov_iter_is_bvec(i))
129466531c65SAl Viro 		return bvec_npages(i, maxpages);
129566531c65SAl Viro 	if (iov_iter_is_xarray(i)) {
1296e4f8df86SAl Viro 		unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1297e4f8df86SAl Viro 		int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
129866531c65SAl Viro 		return min(npages, maxpages);
129966531c65SAl Viro 	}
130066531c65SAl Viro 	return 0;
1301d879cb83SAl Viro }
1302d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages);
1303d879cb83SAl Viro 
1304d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1305d879cb83SAl Viro {
1306d879cb83SAl Viro 	*new = *old;
130700e23707SDavid Howells 	if (iov_iter_is_bvec(new))
1308d879cb83SAl Viro 		return new->bvec = kmemdup(new->bvec,
1309d879cb83SAl Viro 				    new->nr_segs * sizeof(struct bio_vec),
1310d879cb83SAl Viro 				    flags);
1311fcb14cb1SAl Viro 	else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1312d879cb83SAl Viro 		/* iovec and kvec have identical layout */
1313de4f5fedSJens Axboe 		return new->__iov = kmemdup(new->__iov,
1314d879cb83SAl Viro 				   new->nr_segs * sizeof(struct iovec),
1315d879cb83SAl Viro 				   flags);
1316fcb14cb1SAl Viro 	return NULL;
1317d879cb83SAl Viro }
1318d879cb83SAl Viro EXPORT_SYMBOL(dup_iter);
1319bc917be8SAl Viro 
132050f9a76eSJosh Poimboeuf static __noclone int copy_compat_iovec_from_user(struct iovec *iov,
1321bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1322bfdc5970SChristoph Hellwig {
1323bfdc5970SChristoph Hellwig 	const struct compat_iovec __user *uiov =
1324bfdc5970SChristoph Hellwig 		(const struct compat_iovec __user *)uvec;
1325bfdc5970SChristoph Hellwig 	int ret = -EFAULT, i;
1326bfdc5970SChristoph Hellwig 
1327a959a978SChristoph Hellwig 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1328bfdc5970SChristoph Hellwig 		return -EFAULT;
1329bfdc5970SChristoph Hellwig 
1330bfdc5970SChristoph Hellwig 	for (i = 0; i < nr_segs; i++) {
1331bfdc5970SChristoph Hellwig 		compat_uptr_t buf;
1332bfdc5970SChristoph Hellwig 		compat_ssize_t len;
1333bfdc5970SChristoph Hellwig 
1334bfdc5970SChristoph Hellwig 		unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1335bfdc5970SChristoph Hellwig 		unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1336bfdc5970SChristoph Hellwig 
1337bfdc5970SChristoph Hellwig 		/* check for compat_size_t not fitting in compat_ssize_t .. */
1338bfdc5970SChristoph Hellwig 		if (len < 0) {
1339bfdc5970SChristoph Hellwig 			ret = -EINVAL;
1340bfdc5970SChristoph Hellwig 			goto uaccess_end;
1341bfdc5970SChristoph Hellwig 		}
1342bfdc5970SChristoph Hellwig 		iov[i].iov_base = compat_ptr(buf);
1343bfdc5970SChristoph Hellwig 		iov[i].iov_len = len;
1344bfdc5970SChristoph Hellwig 	}
1345bfdc5970SChristoph Hellwig 
1346bfdc5970SChristoph Hellwig 	ret = 0;
1347bfdc5970SChristoph Hellwig uaccess_end:
1348bfdc5970SChristoph Hellwig 	user_access_end();
1349bfdc5970SChristoph Hellwig 	return ret;
1350bfdc5970SChristoph Hellwig }
1351bfdc5970SChristoph Hellwig 
1352bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov,
1353487c20b0SLinus Torvalds 		const struct iovec __user *uiov, unsigned long nr_segs)
1354fb041b59SDavid Laight {
1355487c20b0SLinus Torvalds 	int ret = -EFAULT;
1356bfdc5970SChristoph Hellwig 
1357487c20b0SLinus Torvalds 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1358bfdc5970SChristoph Hellwig 		return -EFAULT;
1359bfdc5970SChristoph Hellwig 
1360487c20b0SLinus Torvalds 	do {
1361487c20b0SLinus Torvalds 		void __user *buf;
1362487c20b0SLinus Torvalds 		ssize_t len;
1363487c20b0SLinus Torvalds 
1364487c20b0SLinus Torvalds 		unsafe_get_user(len, &uiov->iov_len, uaccess_end);
1365487c20b0SLinus Torvalds 		unsafe_get_user(buf, &uiov->iov_base, uaccess_end);
1366487c20b0SLinus Torvalds 
1367487c20b0SLinus Torvalds 		/* check for size_t not fitting in ssize_t .. */
1368487c20b0SLinus Torvalds 		if (unlikely(len < 0)) {
1369487c20b0SLinus Torvalds 			ret = -EINVAL;
1370487c20b0SLinus Torvalds 			goto uaccess_end;
1371487c20b0SLinus Torvalds 		}
1372487c20b0SLinus Torvalds 		iov->iov_base = buf;
1373487c20b0SLinus Torvalds 		iov->iov_len = len;
1374487c20b0SLinus Torvalds 
1375487c20b0SLinus Torvalds 		uiov++; iov++;
1376487c20b0SLinus Torvalds 	} while (--nr_segs);
1377487c20b0SLinus Torvalds 
1378487c20b0SLinus Torvalds 	ret = 0;
1379487c20b0SLinus Torvalds uaccess_end:
1380487c20b0SLinus Torvalds 	user_access_end();
1381487c20b0SLinus Torvalds 	return ret;
1382bfdc5970SChristoph Hellwig }
1383bfdc5970SChristoph Hellwig 
1384bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec,
1385bfdc5970SChristoph Hellwig 		unsigned long nr_segs, unsigned long fast_segs,
1386bfdc5970SChristoph Hellwig 		struct iovec *fast_iov, bool compat)
1387bfdc5970SChristoph Hellwig {
1388bfdc5970SChristoph Hellwig 	struct iovec *iov = fast_iov;
1389bfdc5970SChristoph Hellwig 	int ret;
1390fb041b59SDavid Laight 
1391fb041b59SDavid Laight 	/*
1392bfdc5970SChristoph Hellwig 	 * SuS says "The readv() function *may* fail if the iovcnt argument was
1393bfdc5970SChristoph Hellwig 	 * less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1394fb041b59SDavid Laight 	 * traditionally returned zero for zero segments, so...
1395fb041b59SDavid Laight 	 */
1396bfdc5970SChristoph Hellwig 	if (nr_segs == 0)
1397bfdc5970SChristoph Hellwig 		return iov;
1398bfdc5970SChristoph Hellwig 	if (nr_segs > UIO_MAXIOV)
1399bfdc5970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1400fb041b59SDavid Laight 	if (nr_segs > fast_segs) {
1401fb041b59SDavid Laight 		iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1402bfdc5970SChristoph Hellwig 		if (!iov)
1403bfdc5970SChristoph Hellwig 			return ERR_PTR(-ENOMEM);
1404fb041b59SDavid Laight 	}
1405bfdc5970SChristoph Hellwig 
1406487c20b0SLinus Torvalds 	if (unlikely(compat))
1407bfdc5970SChristoph Hellwig 		ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1408bfdc5970SChristoph Hellwig 	else
1409bfdc5970SChristoph Hellwig 		ret = copy_iovec_from_user(iov, uvec, nr_segs);
1410bfdc5970SChristoph Hellwig 	if (ret) {
1411bfdc5970SChristoph Hellwig 		if (iov != fast_iov)
1412bfdc5970SChristoph Hellwig 			kfree(iov);
1413bfdc5970SChristoph Hellwig 		return ERR_PTR(ret);
1414fb041b59SDavid Laight 	}
1415bfdc5970SChristoph Hellwig 
1416bfdc5970SChristoph Hellwig 	return iov;
1417bfdc5970SChristoph Hellwig }
1418bfdc5970SChristoph Hellwig 
14193b2deb0eSJens Axboe /*
14203b2deb0eSJens Axboe  * Single segment iovec supplied by the user, import it as ITER_UBUF.
14213b2deb0eSJens Axboe  */
14223b2deb0eSJens Axboe static ssize_t __import_iovec_ubuf(int type, const struct iovec __user *uvec,
14233b2deb0eSJens Axboe 				   struct iovec **iovp, struct iov_iter *i,
14243b2deb0eSJens Axboe 				   bool compat)
14253b2deb0eSJens Axboe {
14263b2deb0eSJens Axboe 	struct iovec *iov = *iovp;
14273b2deb0eSJens Axboe 	ssize_t ret;
14283b2deb0eSJens Axboe 
14293b2deb0eSJens Axboe 	if (compat)
14303b2deb0eSJens Axboe 		ret = copy_compat_iovec_from_user(iov, uvec, 1);
14313b2deb0eSJens Axboe 	else
14323b2deb0eSJens Axboe 		ret = copy_iovec_from_user(iov, uvec, 1);
14333b2deb0eSJens Axboe 	if (unlikely(ret))
14343b2deb0eSJens Axboe 		return ret;
14353b2deb0eSJens Axboe 
14363b2deb0eSJens Axboe 	ret = import_ubuf(type, iov->iov_base, iov->iov_len, i);
14373b2deb0eSJens Axboe 	if (unlikely(ret))
14383b2deb0eSJens Axboe 		return ret;
14393b2deb0eSJens Axboe 	*iovp = NULL;
14403b2deb0eSJens Axboe 	return i->count;
14413b2deb0eSJens Axboe }
14423b2deb0eSJens Axboe 
1443bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1444bfdc5970SChristoph Hellwig 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1445bfdc5970SChristoph Hellwig 		 struct iov_iter *i, bool compat)
1446bfdc5970SChristoph Hellwig {
1447bfdc5970SChristoph Hellwig 	ssize_t total_len = 0;
1448bfdc5970SChristoph Hellwig 	unsigned long seg;
1449bfdc5970SChristoph Hellwig 	struct iovec *iov;
1450bfdc5970SChristoph Hellwig 
14513b2deb0eSJens Axboe 	if (nr_segs == 1)
14523b2deb0eSJens Axboe 		return __import_iovec_ubuf(type, uvec, iovp, i, compat);
14533b2deb0eSJens Axboe 
1454bfdc5970SChristoph Hellwig 	iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1455bfdc5970SChristoph Hellwig 	if (IS_ERR(iov)) {
1456bfdc5970SChristoph Hellwig 		*iovp = NULL;
1457bfdc5970SChristoph Hellwig 		return PTR_ERR(iov);
1458fb041b59SDavid Laight 	}
1459fb041b59SDavid Laight 
1460fb041b59SDavid Laight 	/*
1461bfdc5970SChristoph Hellwig 	 * According to the Single Unix Specification we should return EINVAL if
1462bfdc5970SChristoph Hellwig 	 * an element length is < 0 when cast to ssize_t or if the total length
1463bfdc5970SChristoph Hellwig 	 * would overflow the ssize_t return value of the system call.
1464fb041b59SDavid Laight 	 *
1465fb041b59SDavid Laight 	 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1466fb041b59SDavid Laight 	 * overflow case.
1467fb041b59SDavid Laight 	 */
1468fb041b59SDavid Laight 	for (seg = 0; seg < nr_segs; seg++) {
1469fb041b59SDavid Laight 		ssize_t len = (ssize_t)iov[seg].iov_len;
1470fb041b59SDavid Laight 
1471bfdc5970SChristoph Hellwig 		if (!access_ok(iov[seg].iov_base, len)) {
1472bfdc5970SChristoph Hellwig 			if (iov != *iovp)
1473bfdc5970SChristoph Hellwig 				kfree(iov);
1474bfdc5970SChristoph Hellwig 			*iovp = NULL;
1475bfdc5970SChristoph Hellwig 			return -EFAULT;
1476fb041b59SDavid Laight 		}
1477bfdc5970SChristoph Hellwig 
1478bfdc5970SChristoph Hellwig 		if (len > MAX_RW_COUNT - total_len) {
1479bfdc5970SChristoph Hellwig 			len = MAX_RW_COUNT - total_len;
1480fb041b59SDavid Laight 			iov[seg].iov_len = len;
1481fb041b59SDavid Laight 		}
1482bfdc5970SChristoph Hellwig 		total_len += len;
1483fb041b59SDavid Laight 	}
1484bfdc5970SChristoph Hellwig 
1485bfdc5970SChristoph Hellwig 	iov_iter_init(i, type, iov, nr_segs, total_len);
1486bfdc5970SChristoph Hellwig 	if (iov == *iovp)
1487bfdc5970SChristoph Hellwig 		*iovp = NULL;
1488bfdc5970SChristoph Hellwig 	else
1489bfdc5970SChristoph Hellwig 		*iovp = iov;
1490bfdc5970SChristoph Hellwig 	return total_len;
1491fb041b59SDavid Laight }
1492fb041b59SDavid Laight 
1493ffecee4fSVegard Nossum /**
1494ffecee4fSVegard Nossum  * import_iovec() - Copy an array of &struct iovec from userspace
1495ffecee4fSVegard Nossum  *     into the kernel, check that it is valid, and initialize a new
1496ffecee4fSVegard Nossum  *     &struct iov_iter iterator to access it.
1497ffecee4fSVegard Nossum  *
1498ffecee4fSVegard Nossum  * @type: One of %READ or %WRITE.
1499bfdc5970SChristoph Hellwig  * @uvec: Pointer to the userspace array.
1500ffecee4fSVegard Nossum  * @nr_segs: Number of elements in userspace array.
1501ffecee4fSVegard Nossum  * @fast_segs: Number of elements in @iov.
1502bfdc5970SChristoph Hellwig  * @iovp: (input and output parameter) Pointer to pointer to (usually small
1503ffecee4fSVegard Nossum  *     on-stack) kernel array.
1504ffecee4fSVegard Nossum  * @i: Pointer to iterator that will be initialized on success.
1505ffecee4fSVegard Nossum  *
1506ffecee4fSVegard Nossum  * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1507ffecee4fSVegard Nossum  * then this function places %NULL in *@iov on return. Otherwise, a new
1508ffecee4fSVegard Nossum  * array will be allocated and the result placed in *@iov. This means that
1509ffecee4fSVegard Nossum  * the caller may call kfree() on *@iov regardless of whether the small
1510ffecee4fSVegard Nossum  * on-stack array was used or not (and regardless of whether this function
1511ffecee4fSVegard Nossum  * returns an error or not).
1512ffecee4fSVegard Nossum  *
151387e5e6daSJens Axboe  * Return: Negative error code on error, bytes imported on success
1514ffecee4fSVegard Nossum  */
1515bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec,
1516bc917be8SAl Viro 		 unsigned nr_segs, unsigned fast_segs,
1517bfdc5970SChristoph Hellwig 		 struct iovec **iovp, struct iov_iter *i)
1518bc917be8SAl Viro {
151989cd35c5SChristoph Hellwig 	return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
152089cd35c5SChristoph Hellwig 			      in_compat_syscall());
1521bc917be8SAl Viro }
1522bc917be8SAl Viro EXPORT_SYMBOL(import_iovec);
1523bc917be8SAl Viro 
1524bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len,
1525bc917be8SAl Viro 		 struct iovec *iov, struct iov_iter *i)
1526bc917be8SAl Viro {
1527bc917be8SAl Viro 	if (len > MAX_RW_COUNT)
1528bc917be8SAl Viro 		len = MAX_RW_COUNT;
152996d4f267SLinus Torvalds 	if (unlikely(!access_ok(buf, len)))
1530bc917be8SAl Viro 		return -EFAULT;
1531bc917be8SAl Viro 
1532e03ad4eeSJens Axboe 	iov_iter_ubuf(i, rw, buf, len);
1533bc917be8SAl Viro 	return 0;
1534bc917be8SAl Viro }
1535e1267585SAl Viro EXPORT_SYMBOL(import_single_range);
15368fb0f47aSJens Axboe 
15372ad9bd83SJens Axboe int import_ubuf(int rw, void __user *buf, size_t len, struct iov_iter *i)
15382ad9bd83SJens Axboe {
15392ad9bd83SJens Axboe 	if (len > MAX_RW_COUNT)
15402ad9bd83SJens Axboe 		len = MAX_RW_COUNT;
15412ad9bd83SJens Axboe 	if (unlikely(!access_ok(buf, len)))
15422ad9bd83SJens Axboe 		return -EFAULT;
15432ad9bd83SJens Axboe 
15442ad9bd83SJens Axboe 	iov_iter_ubuf(i, rw, buf, len);
15452ad9bd83SJens Axboe 	return 0;
15462ad9bd83SJens Axboe }
15472ad9bd83SJens Axboe 
15488fb0f47aSJens Axboe /**
15498fb0f47aSJens Axboe  * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
15508fb0f47aSJens Axboe  *     iov_iter_save_state() was called.
15518fb0f47aSJens Axboe  *
15528fb0f47aSJens Axboe  * @i: &struct iov_iter to restore
15538fb0f47aSJens Axboe  * @state: state to restore from
15548fb0f47aSJens Axboe  *
15558fb0f47aSJens Axboe  * Used after iov_iter_save_state() to bring restore @i, if operations may
15568fb0f47aSJens Axboe  * have advanced it.
15578fb0f47aSJens Axboe  *
15588fb0f47aSJens Axboe  * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
15598fb0f47aSJens Axboe  */
15608fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
15618fb0f47aSJens Axboe {
15624397a17cSKeith Busch 	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) &&
15634397a17cSKeith Busch 			 !iter_is_ubuf(i)) && !iov_iter_is_kvec(i))
15648fb0f47aSJens Axboe 		return;
15658fb0f47aSJens Axboe 	i->iov_offset = state->iov_offset;
15668fb0f47aSJens Axboe 	i->count = state->count;
1567fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1568fcb14cb1SAl Viro 		return;
15698fb0f47aSJens Axboe 	/*
15708fb0f47aSJens Axboe 	 * For the *vec iters, nr_segs + iov is constant - if we increment
15718fb0f47aSJens Axboe 	 * the vec, then we also decrement the nr_segs count. Hence we don't
15728fb0f47aSJens Axboe 	 * need to track both of these, just one is enough and we can deduct
15738fb0f47aSJens Axboe 	 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
15748fb0f47aSJens Axboe 	 * size, so we can just increment the iov pointer as they are unionzed.
15758fb0f47aSJens Axboe 	 * ITER_BVEC _may_ be the same size on some archs, but on others it is
15768fb0f47aSJens Axboe 	 * not. Be safe and handle it separately.
15778fb0f47aSJens Axboe 	 */
15788fb0f47aSJens Axboe 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
15798fb0f47aSJens Axboe 	if (iov_iter_is_bvec(i))
15808fb0f47aSJens Axboe 		i->bvec -= state->nr_segs - i->nr_segs;
15818fb0f47aSJens Axboe 	else
1582de4f5fedSJens Axboe 		i->__iov -= state->nr_segs - i->nr_segs;
15838fb0f47aSJens Axboe 	i->nr_segs = state->nr_segs;
15848fb0f47aSJens Axboe }
15857d58fe73SDavid Howells 
15867d58fe73SDavid Howells /*
15877d58fe73SDavid Howells  * Extract a list of contiguous pages from an ITER_XARRAY iterator.  This does not
15887d58fe73SDavid Howells  * get references on the pages, nor does it get a pin on them.
15897d58fe73SDavid Howells  */
15907d58fe73SDavid Howells static ssize_t iov_iter_extract_xarray_pages(struct iov_iter *i,
15917d58fe73SDavid Howells 					     struct page ***pages, size_t maxsize,
15927d58fe73SDavid Howells 					     unsigned int maxpages,
15937d58fe73SDavid Howells 					     iov_iter_extraction_t extraction_flags,
15947d58fe73SDavid Howells 					     size_t *offset0)
15957d58fe73SDavid Howells {
15967d58fe73SDavid Howells 	struct page *page, **p;
15977d58fe73SDavid Howells 	unsigned int nr = 0, offset;
15987d58fe73SDavid Howells 	loff_t pos = i->xarray_start + i->iov_offset;
15997d58fe73SDavid Howells 	pgoff_t index = pos >> PAGE_SHIFT;
16007d58fe73SDavid Howells 	XA_STATE(xas, i->xarray, index);
16017d58fe73SDavid Howells 
16027d58fe73SDavid Howells 	offset = pos & ~PAGE_MASK;
16037d58fe73SDavid Howells 	*offset0 = offset;
16047d58fe73SDavid Howells 
16057d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
16067d58fe73SDavid Howells 	if (!maxpages)
16077d58fe73SDavid Howells 		return -ENOMEM;
16087d58fe73SDavid Howells 	p = *pages;
16097d58fe73SDavid Howells 
16107d58fe73SDavid Howells 	rcu_read_lock();
16117d58fe73SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
16127d58fe73SDavid Howells 		if (xas_retry(&xas, page))
16137d58fe73SDavid Howells 			continue;
16147d58fe73SDavid Howells 
16157d58fe73SDavid Howells 		/* Has the page moved or been split? */
16167d58fe73SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
16177d58fe73SDavid Howells 			xas_reset(&xas);
16187d58fe73SDavid Howells 			continue;
16197d58fe73SDavid Howells 		}
16207d58fe73SDavid Howells 
16217d58fe73SDavid Howells 		p[nr++] = find_subpage(page, xas.xa_index);
16227d58fe73SDavid Howells 		if (nr == maxpages)
16237d58fe73SDavid Howells 			break;
16247d58fe73SDavid Howells 	}
16257d58fe73SDavid Howells 	rcu_read_unlock();
16267d58fe73SDavid Howells 
16277d58fe73SDavid Howells 	maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
16287d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
16297d58fe73SDavid Howells 	return maxsize;
16307d58fe73SDavid Howells }
16317d58fe73SDavid Howells 
16327d58fe73SDavid Howells /*
16337d58fe73SDavid Howells  * Extract a list of contiguous pages from an ITER_BVEC iterator.  This does
16347d58fe73SDavid Howells  * not get references on the pages, nor does it get a pin on them.
16357d58fe73SDavid Howells  */
16367d58fe73SDavid Howells static ssize_t iov_iter_extract_bvec_pages(struct iov_iter *i,
16377d58fe73SDavid Howells 					   struct page ***pages, size_t maxsize,
16387d58fe73SDavid Howells 					   unsigned int maxpages,
16397d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
16407d58fe73SDavid Howells 					   size_t *offset0)
16417d58fe73SDavid Howells {
16427d58fe73SDavid Howells 	struct page **p, *page;
16437d58fe73SDavid Howells 	size_t skip = i->iov_offset, offset;
16447d58fe73SDavid Howells 	int k;
16457d58fe73SDavid Howells 
16467d58fe73SDavid Howells 	for (;;) {
16477d58fe73SDavid Howells 		if (i->nr_segs == 0)
16487d58fe73SDavid Howells 			return 0;
16497d58fe73SDavid Howells 		maxsize = min(maxsize, i->bvec->bv_len - skip);
16507d58fe73SDavid Howells 		if (maxsize)
16517d58fe73SDavid Howells 			break;
16527d58fe73SDavid Howells 		i->iov_offset = 0;
16537d58fe73SDavid Howells 		i->nr_segs--;
16547d58fe73SDavid Howells 		i->bvec++;
16557d58fe73SDavid Howells 		skip = 0;
16567d58fe73SDavid Howells 	}
16577d58fe73SDavid Howells 
16587d58fe73SDavid Howells 	skip += i->bvec->bv_offset;
16597d58fe73SDavid Howells 	page = i->bvec->bv_page + skip / PAGE_SIZE;
16607d58fe73SDavid Howells 	offset = skip % PAGE_SIZE;
16617d58fe73SDavid Howells 	*offset0 = offset;
16627d58fe73SDavid Howells 
16637d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
16647d58fe73SDavid Howells 	if (!maxpages)
16657d58fe73SDavid Howells 		return -ENOMEM;
16667d58fe73SDavid Howells 	p = *pages;
16677d58fe73SDavid Howells 	for (k = 0; k < maxpages; k++)
16687d58fe73SDavid Howells 		p[k] = page + k;
16697d58fe73SDavid Howells 
16707d58fe73SDavid Howells 	maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset);
16717d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
16727d58fe73SDavid Howells 	return maxsize;
16737d58fe73SDavid Howells }
16747d58fe73SDavid Howells 
16757d58fe73SDavid Howells /*
16767d58fe73SDavid Howells  * Extract a list of virtually contiguous pages from an ITER_KVEC iterator.
16777d58fe73SDavid Howells  * This does not get references on the pages, nor does it get a pin on them.
16787d58fe73SDavid Howells  */
16797d58fe73SDavid Howells static ssize_t iov_iter_extract_kvec_pages(struct iov_iter *i,
16807d58fe73SDavid Howells 					   struct page ***pages, size_t maxsize,
16817d58fe73SDavid Howells 					   unsigned int maxpages,
16827d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
16837d58fe73SDavid Howells 					   size_t *offset0)
16847d58fe73SDavid Howells {
16857d58fe73SDavid Howells 	struct page **p, *page;
16867d58fe73SDavid Howells 	const void *kaddr;
16877d58fe73SDavid Howells 	size_t skip = i->iov_offset, offset, len;
16887d58fe73SDavid Howells 	int k;
16897d58fe73SDavid Howells 
16907d58fe73SDavid Howells 	for (;;) {
16917d58fe73SDavid Howells 		if (i->nr_segs == 0)
16927d58fe73SDavid Howells 			return 0;
16937d58fe73SDavid Howells 		maxsize = min(maxsize, i->kvec->iov_len - skip);
16947d58fe73SDavid Howells 		if (maxsize)
16957d58fe73SDavid Howells 			break;
16967d58fe73SDavid Howells 		i->iov_offset = 0;
16977d58fe73SDavid Howells 		i->nr_segs--;
16987d58fe73SDavid Howells 		i->kvec++;
16997d58fe73SDavid Howells 		skip = 0;
17007d58fe73SDavid Howells 	}
17017d58fe73SDavid Howells 
17027d58fe73SDavid Howells 	kaddr = i->kvec->iov_base + skip;
17037d58fe73SDavid Howells 	offset = (unsigned long)kaddr & ~PAGE_MASK;
17047d58fe73SDavid Howells 	*offset0 = offset;
17057d58fe73SDavid Howells 
17067d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
17077d58fe73SDavid Howells 	if (!maxpages)
17087d58fe73SDavid Howells 		return -ENOMEM;
17097d58fe73SDavid Howells 	p = *pages;
17107d58fe73SDavid Howells 
17117d58fe73SDavid Howells 	kaddr -= offset;
17127d58fe73SDavid Howells 	len = offset + maxsize;
17137d58fe73SDavid Howells 	for (k = 0; k < maxpages; k++) {
17147d58fe73SDavid Howells 		size_t seg = min_t(size_t, len, PAGE_SIZE);
17157d58fe73SDavid Howells 
17167d58fe73SDavid Howells 		if (is_vmalloc_or_module_addr(kaddr))
17177d58fe73SDavid Howells 			page = vmalloc_to_page(kaddr);
17187d58fe73SDavid Howells 		else
17197d58fe73SDavid Howells 			page = virt_to_page(kaddr);
17207d58fe73SDavid Howells 
17217d58fe73SDavid Howells 		p[k] = page;
17227d58fe73SDavid Howells 		len -= seg;
17237d58fe73SDavid Howells 		kaddr += PAGE_SIZE;
17247d58fe73SDavid Howells 	}
17257d58fe73SDavid Howells 
17267d58fe73SDavid Howells 	maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset);
17277d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
17287d58fe73SDavid Howells 	return maxsize;
17297d58fe73SDavid Howells }
17307d58fe73SDavid Howells 
17317d58fe73SDavid Howells /*
17327d58fe73SDavid Howells  * Extract a list of contiguous pages from a user iterator and get a pin on
17337d58fe73SDavid Howells  * each of them.  This should only be used if the iterator is user-backed
17347d58fe73SDavid Howells  * (IOBUF/UBUF).
17357d58fe73SDavid Howells  *
17367d58fe73SDavid Howells  * It does not get refs on the pages, but the pages must be unpinned by the
17377d58fe73SDavid Howells  * caller once the transfer is complete.
17387d58fe73SDavid Howells  *
17397d58fe73SDavid Howells  * This is safe to be used where background IO/DMA *is* going to be modifying
17407d58fe73SDavid Howells  * the buffer; using a pin rather than a ref makes forces fork() to give the
17417d58fe73SDavid Howells  * child a copy of the page.
17427d58fe73SDavid Howells  */
17437d58fe73SDavid Howells static ssize_t iov_iter_extract_user_pages(struct iov_iter *i,
17447d58fe73SDavid Howells 					   struct page ***pages,
17457d58fe73SDavid Howells 					   size_t maxsize,
17467d58fe73SDavid Howells 					   unsigned int maxpages,
17477d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
17487d58fe73SDavid Howells 					   size_t *offset0)
17497d58fe73SDavid Howells {
17507d58fe73SDavid Howells 	unsigned long addr;
17517d58fe73SDavid Howells 	unsigned int gup_flags = 0;
17527d58fe73SDavid Howells 	size_t offset;
17537d58fe73SDavid Howells 	int res;
17547d58fe73SDavid Howells 
17557d58fe73SDavid Howells 	if (i->data_source == ITER_DEST)
17567d58fe73SDavid Howells 		gup_flags |= FOLL_WRITE;
17577d58fe73SDavid Howells 	if (extraction_flags & ITER_ALLOW_P2PDMA)
17587d58fe73SDavid Howells 		gup_flags |= FOLL_PCI_P2PDMA;
17597d58fe73SDavid Howells 	if (i->nofault)
17607d58fe73SDavid Howells 		gup_flags |= FOLL_NOFAULT;
17617d58fe73SDavid Howells 
17627d58fe73SDavid Howells 	addr = first_iovec_segment(i, &maxsize);
17637d58fe73SDavid Howells 	*offset0 = offset = addr % PAGE_SIZE;
17647d58fe73SDavid Howells 	addr &= PAGE_MASK;
17657d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
17667d58fe73SDavid Howells 	if (!maxpages)
17677d58fe73SDavid Howells 		return -ENOMEM;
17687d58fe73SDavid Howells 	res = pin_user_pages_fast(addr, maxpages, gup_flags, *pages);
17697d58fe73SDavid Howells 	if (unlikely(res <= 0))
17707d58fe73SDavid Howells 		return res;
17717d58fe73SDavid Howells 	maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - offset);
17727d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
17737d58fe73SDavid Howells 	return maxsize;
17747d58fe73SDavid Howells }
17757d58fe73SDavid Howells 
17767d58fe73SDavid Howells /**
17777d58fe73SDavid Howells  * iov_iter_extract_pages - Extract a list of contiguous pages from an iterator
17787d58fe73SDavid Howells  * @i: The iterator to extract from
17797d58fe73SDavid Howells  * @pages: Where to return the list of pages
17807d58fe73SDavid Howells  * @maxsize: The maximum amount of iterator to extract
17817d58fe73SDavid Howells  * @maxpages: The maximum size of the list of pages
17827d58fe73SDavid Howells  * @extraction_flags: Flags to qualify request
17837d58fe73SDavid Howells  * @offset0: Where to return the starting offset into (*@pages)[0]
17847d58fe73SDavid Howells  *
17857d58fe73SDavid Howells  * Extract a list of contiguous pages from the current point of the iterator,
17867d58fe73SDavid Howells  * advancing the iterator.  The maximum number of pages and the maximum amount
17877d58fe73SDavid Howells  * of page contents can be set.
17887d58fe73SDavid Howells  *
17897d58fe73SDavid Howells  * If *@pages is NULL, a page list will be allocated to the required size and
17907d58fe73SDavid Howells  * *@pages will be set to its base.  If *@pages is not NULL, it will be assumed
17917d58fe73SDavid Howells  * that the caller allocated a page list at least @maxpages in size and this
17927d58fe73SDavid Howells  * will be filled in.
17937d58fe73SDavid Howells  *
17947d58fe73SDavid Howells  * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA
17957d58fe73SDavid Howells  * be allowed on the pages extracted.
17967d58fe73SDavid Howells  *
17977d58fe73SDavid Howells  * The iov_iter_extract_will_pin() function can be used to query how cleanup
17987d58fe73SDavid Howells  * should be performed.
17997d58fe73SDavid Howells  *
18007d58fe73SDavid Howells  * Extra refs or pins on the pages may be obtained as follows:
18017d58fe73SDavid Howells  *
18027d58fe73SDavid Howells  *  (*) If the iterator is user-backed (ITER_IOVEC/ITER_UBUF), pins will be
18037d58fe73SDavid Howells  *      added to the pages, but refs will not be taken.
18047d58fe73SDavid Howells  *      iov_iter_extract_will_pin() will return true.
18057d58fe73SDavid Howells  *
18067d58fe73SDavid Howells  *  (*) If the iterator is ITER_KVEC, ITER_BVEC or ITER_XARRAY, the pages are
18077d58fe73SDavid Howells  *      merely listed; no extra refs or pins are obtained.
18087d58fe73SDavid Howells  *      iov_iter_extract_will_pin() will return 0.
18097d58fe73SDavid Howells  *
18107d58fe73SDavid Howells  * Note also:
18117d58fe73SDavid Howells  *
18127d58fe73SDavid Howells  *  (*) Use with ITER_DISCARD is not supported as that has no content.
18137d58fe73SDavid Howells  *
18147d58fe73SDavid Howells  * On success, the function sets *@pages to the new pagelist, if allocated, and
18157d58fe73SDavid Howells  * sets *offset0 to the offset into the first page.
18167d58fe73SDavid Howells  *
18177d58fe73SDavid Howells  * It may also return -ENOMEM and -EFAULT.
18187d58fe73SDavid Howells  */
18197d58fe73SDavid Howells ssize_t iov_iter_extract_pages(struct iov_iter *i,
18207d58fe73SDavid Howells 			       struct page ***pages,
18217d58fe73SDavid Howells 			       size_t maxsize,
18227d58fe73SDavid Howells 			       unsigned int maxpages,
18237d58fe73SDavid Howells 			       iov_iter_extraction_t extraction_flags,
18247d58fe73SDavid Howells 			       size_t *offset0)
18257d58fe73SDavid Howells {
18267d58fe73SDavid Howells 	maxsize = min_t(size_t, min_t(size_t, maxsize, i->count), MAX_RW_COUNT);
18277d58fe73SDavid Howells 	if (!maxsize)
18287d58fe73SDavid Howells 		return 0;
18297d58fe73SDavid Howells 
18307d58fe73SDavid Howells 	if (likely(user_backed_iter(i)))
18317d58fe73SDavid Howells 		return iov_iter_extract_user_pages(i, pages, maxsize,
18327d58fe73SDavid Howells 						   maxpages, extraction_flags,
18337d58fe73SDavid Howells 						   offset0);
18347d58fe73SDavid Howells 	if (iov_iter_is_kvec(i))
18357d58fe73SDavid Howells 		return iov_iter_extract_kvec_pages(i, pages, maxsize,
18367d58fe73SDavid Howells 						   maxpages, extraction_flags,
18377d58fe73SDavid Howells 						   offset0);
18387d58fe73SDavid Howells 	if (iov_iter_is_bvec(i))
18397d58fe73SDavid Howells 		return iov_iter_extract_bvec_pages(i, pages, maxsize,
18407d58fe73SDavid Howells 						   maxpages, extraction_flags,
18417d58fe73SDavid Howells 						   offset0);
18427d58fe73SDavid Howells 	if (iov_iter_is_xarray(i))
18437d58fe73SDavid Howells 		return iov_iter_extract_xarray_pages(i, pages, maxsize,
18447d58fe73SDavid Howells 						     maxpages, extraction_flags,
18457d58fe73SDavid Howells 						     offset0);
18467d58fe73SDavid Howells 	return -EFAULT;
18477d58fe73SDavid Howells }
18487d58fe73SDavid Howells EXPORT_SYMBOL_GPL(iov_iter_extract_pages);
1849