xref: /openbmc/linux/lib/iov_iter.c (revision f741bd71)
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 
copyout(void __user * to,const void * from,size_t n)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 
copyout_nofault(void __user * to,const void * from,size_t n)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 
copyin(void * to,const void __user * from,size_t n)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  */
fault_in_iov_iter_readable(const struct iov_iter * i,size_t size)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  */
fault_in_iov_iter_writeable(const struct iov_iter * i,size_t size)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 
iov_iter_init(struct iov_iter * i,unsigned int direction,const struct iovec * iov,unsigned long nr_segs,size_t count)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 
csum_and_memcpy(void * to,const void * from,size_t len,__wsum sum,size_t off)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 
_copy_to_iter(const void * addr,size_t bytes,struct iov_iter * i)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
copyout_mc(void __user * to,const void * from,size_t n)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  */
_copy_mc_to_iter(const void * addr,size_t bytes,struct iov_iter * i)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 
memcpy_from_iter(struct iov_iter * i,void * to,const void * from,size_t size)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 
_copy_from_iter(void * addr,size_t bytes,struct iov_iter * i)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 
_copy_from_iter_nocache(void * addr,size_t bytes,struct iov_iter * i)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  */
_copy_from_iter_flushcache(void * addr,size_t bytes,struct iov_iter * i)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 
page_copy_sane(struct page * page,size_t offset,size_t n)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 
copy_page_to_iter(struct page * page,size_t offset,size_t bytes,struct iov_iter * i)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 
copy_page_to_iter_nofault(struct page * page,unsigned offset,size_t bytes,struct iov_iter * i)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 
copy_page_from_iter(struct page * page,size_t offset,size_t bytes,struct iov_iter * i)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 
iov_iter_zero(size_t bytes,struct iov_iter * i)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 
copy_page_from_iter_atomic(struct page * page,size_t offset,size_t bytes,struct iov_iter * i)5691b030698SMatthew Wilcox (Oracle) size_t copy_page_from_iter_atomic(struct page *page, size_t offset,
570908a1ad8SMatthew Wilcox (Oracle) 		size_t bytes, struct iov_iter *i)
571d879cb83SAl Viro {
572908a1ad8SMatthew Wilcox (Oracle) 	size_t n, copied = 0;
573f7f9a0c8SMatthew Wilcox (Oracle) 
574f7f9a0c8SMatthew Wilcox (Oracle) 	if (!page_copy_sane(page, offset, bytes))
57572e809edSAl Viro 		return 0;
576f7f9a0c8SMatthew Wilcox (Oracle) 	if (WARN_ON_ONCE(!i->data_source))
577241699cdSAl Viro 		return 0;
578f7f9a0c8SMatthew Wilcox (Oracle) 
579908a1ad8SMatthew Wilcox (Oracle) 	do {
580908a1ad8SMatthew Wilcox (Oracle) 		char *p;
581908a1ad8SMatthew Wilcox (Oracle) 
582908a1ad8SMatthew Wilcox (Oracle) 		n = bytes - copied;
583908a1ad8SMatthew Wilcox (Oracle) 		if (PageHighMem(page)) {
584908a1ad8SMatthew Wilcox (Oracle) 			page += offset / PAGE_SIZE;
585908a1ad8SMatthew Wilcox (Oracle) 			offset %= PAGE_SIZE;
586908a1ad8SMatthew Wilcox (Oracle) 			n = min_t(size_t, n, PAGE_SIZE - offset);
587908a1ad8SMatthew Wilcox (Oracle) 		}
588908a1ad8SMatthew Wilcox (Oracle) 
589f7f9a0c8SMatthew Wilcox (Oracle) 		p = kmap_atomic(page) + offset;
590908a1ad8SMatthew Wilcox (Oracle) 		iterate_and_advance(i, n, base, len, off,
5917baa5099SAl Viro 			copyin(p + off, base, len),
592245f0922SKefeng Wang 			memcpy_from_iter(i, p + off, base, len)
593d879cb83SAl Viro 		)
594f7f9a0c8SMatthew Wilcox (Oracle) 		kunmap_atomic(p);
595908a1ad8SMatthew Wilcox (Oracle) 		copied += n;
596908a1ad8SMatthew Wilcox (Oracle) 		offset += n;
597908a1ad8SMatthew Wilcox (Oracle) 	} while (PageHighMem(page) && copied != bytes && n > 0);
598f7f9a0c8SMatthew Wilcox (Oracle) 
599908a1ad8SMatthew Wilcox (Oracle) 	return copied;
600d879cb83SAl Viro }
601f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic);
602d879cb83SAl Viro 
iov_iter_bvec_advance(struct iov_iter * i,size_t size)60354c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
60454c8195bSPavel Begunkov {
60518fa9af7SAl Viro 	const struct bio_vec *bvec, *end;
60654c8195bSPavel Begunkov 
60718fa9af7SAl Viro 	if (!i->count)
60818fa9af7SAl Viro 		return;
60918fa9af7SAl Viro 	i->count -= size;
61054c8195bSPavel Begunkov 
61118fa9af7SAl Viro 	size += i->iov_offset;
61218fa9af7SAl Viro 
61318fa9af7SAl Viro 	for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
61418fa9af7SAl Viro 		if (likely(size < bvec->bv_len))
61518fa9af7SAl Viro 			break;
61618fa9af7SAl Viro 		size -= bvec->bv_len;
61718fa9af7SAl Viro 	}
61818fa9af7SAl Viro 	i->iov_offset = size;
61918fa9af7SAl Viro 	i->nr_segs -= bvec - i->bvec;
62018fa9af7SAl Viro 	i->bvec = bvec;
62154c8195bSPavel Begunkov }
62254c8195bSPavel Begunkov 
iov_iter_iovec_advance(struct iov_iter * i,size_t size)623185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
624185ac4d4SAl Viro {
625185ac4d4SAl Viro 	const struct iovec *iov, *end;
626185ac4d4SAl Viro 
627185ac4d4SAl Viro 	if (!i->count)
628185ac4d4SAl Viro 		return;
629185ac4d4SAl Viro 	i->count -= size;
630185ac4d4SAl Viro 
631185ac4d4SAl Viro 	size += i->iov_offset; // from beginning of current segment
632de4f5fedSJens Axboe 	for (iov = iter_iov(i), end = iov + i->nr_segs; iov < end; iov++) {
633185ac4d4SAl Viro 		if (likely(size < iov->iov_len))
634185ac4d4SAl Viro 			break;
635185ac4d4SAl Viro 		size -= iov->iov_len;
636185ac4d4SAl Viro 	}
637185ac4d4SAl Viro 	i->iov_offset = size;
638de4f5fedSJens Axboe 	i->nr_segs -= iov - iter_iov(i);
639de4f5fedSJens Axboe 	i->__iov = iov;
640185ac4d4SAl Viro }
641185ac4d4SAl Viro 
iov_iter_advance(struct iov_iter * i,size_t size)642d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size)
643d879cb83SAl Viro {
6443b3fc051SAl Viro 	if (unlikely(i->count < size))
6453b3fc051SAl Viro 		size = i->count;
646fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
647fcb14cb1SAl Viro 		i->iov_offset += size;
648fcb14cb1SAl Viro 		i->count -= size;
649fcb14cb1SAl Viro 	} else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
650185ac4d4SAl Viro 		/* iovec and kvec have identical layouts */
651185ac4d4SAl Viro 		iov_iter_iovec_advance(i, size);
652185ac4d4SAl Viro 	} else if (iov_iter_is_bvec(i)) {
653185ac4d4SAl Viro 		iov_iter_bvec_advance(i, size);
654185ac4d4SAl Viro 	} else if (iov_iter_is_discard(i)) {
655185ac4d4SAl Viro 		i->count -= size;
6567ff50620SDavid Howells 	}
657d879cb83SAl Viro }
658d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance);
659d879cb83SAl Viro 
iov_iter_revert(struct iov_iter * i,size_t unroll)66027c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll)
66127c0e374SAl Viro {
66227c0e374SAl Viro 	if (!unroll)
66327c0e374SAl Viro 		return;
6645b47d59aSAl Viro 	if (WARN_ON(unroll > MAX_RW_COUNT))
6655b47d59aSAl Viro 		return;
66627c0e374SAl Viro 	i->count += unroll;
6679ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_discard(i)))
6689ea9ce04SDavid Howells 		return;
66927c0e374SAl Viro 	if (unroll <= i->iov_offset) {
67027c0e374SAl Viro 		i->iov_offset -= unroll;
67127c0e374SAl Viro 		return;
67227c0e374SAl Viro 	}
67327c0e374SAl Viro 	unroll -= i->iov_offset;
674fcb14cb1SAl Viro 	if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
6757ff50620SDavid Howells 		BUG(); /* We should never go beyond the start of the specified
6767ff50620SDavid Howells 			* range since we might then be straying into pages that
6777ff50620SDavid Howells 			* aren't pinned.
6787ff50620SDavid Howells 			*/
6797ff50620SDavid Howells 	} else if (iov_iter_is_bvec(i)) {
68027c0e374SAl Viro 		const struct bio_vec *bvec = i->bvec;
68127c0e374SAl Viro 		while (1) {
68227c0e374SAl Viro 			size_t n = (--bvec)->bv_len;
68327c0e374SAl Viro 			i->nr_segs++;
68427c0e374SAl Viro 			if (unroll <= n) {
68527c0e374SAl Viro 				i->bvec = bvec;
68627c0e374SAl Viro 				i->iov_offset = n - unroll;
68727c0e374SAl Viro 				return;
68827c0e374SAl Viro 			}
68927c0e374SAl Viro 			unroll -= n;
69027c0e374SAl Viro 		}
69127c0e374SAl Viro 	} else { /* same logics for iovec and kvec */
692de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i);
69327c0e374SAl Viro 		while (1) {
69427c0e374SAl Viro 			size_t n = (--iov)->iov_len;
69527c0e374SAl Viro 			i->nr_segs++;
69627c0e374SAl Viro 			if (unroll <= n) {
697de4f5fedSJens Axboe 				i->__iov = iov;
69827c0e374SAl Viro 				i->iov_offset = n - unroll;
69927c0e374SAl Viro 				return;
70027c0e374SAl Viro 			}
70127c0e374SAl Viro 			unroll -= n;
70227c0e374SAl Viro 		}
70327c0e374SAl Viro 	}
70427c0e374SAl Viro }
70527c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert);
70627c0e374SAl Viro 
707d879cb83SAl Viro /*
708d879cb83SAl Viro  * Return the count of just the current iov_iter segment.
709d879cb83SAl Viro  */
iov_iter_single_seg_count(const struct iov_iter * i)710d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i)
711d879cb83SAl Viro {
71228f38db7SAl Viro 	if (i->nr_segs > 1) {
71328f38db7SAl Viro 		if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
714de4f5fedSJens Axboe 			return min(i->count, iter_iov(i)->iov_len - i->iov_offset);
7157ff50620SDavid Howells 		if (iov_iter_is_bvec(i))
716d879cb83SAl Viro 			return min(i->count, i->bvec->bv_len - i->iov_offset);
71728f38db7SAl Viro 	}
71828f38db7SAl Viro 	return i->count;
719d879cb83SAl Viro }
720d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count);
721d879cb83SAl Viro 
iov_iter_kvec(struct iov_iter * i,unsigned int direction,const struct kvec * kvec,unsigned long nr_segs,size_t count)722aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
723d879cb83SAl Viro 			const struct kvec *kvec, unsigned long nr_segs,
724d879cb83SAl Viro 			size_t count)
725d879cb83SAl Viro {
726aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
7278cd54c1cSAl Viro 	*i = (struct iov_iter){
7288cd54c1cSAl Viro 		.iter_type = ITER_KVEC,
729245f0922SKefeng Wang 		.copy_mc = false,
7308cd54c1cSAl Viro 		.data_source = direction,
7318cd54c1cSAl Viro 		.kvec = kvec,
7328cd54c1cSAl Viro 		.nr_segs = nr_segs,
7338cd54c1cSAl Viro 		.iov_offset = 0,
7348cd54c1cSAl Viro 		.count = count
7358cd54c1cSAl Viro 	};
736d879cb83SAl Viro }
737d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec);
738d879cb83SAl Viro 
iov_iter_bvec(struct iov_iter * i,unsigned int direction,const struct bio_vec * bvec,unsigned long nr_segs,size_t count)739aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
740d879cb83SAl Viro 			const struct bio_vec *bvec, unsigned long nr_segs,
741d879cb83SAl Viro 			size_t count)
742d879cb83SAl Viro {
743aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
7448cd54c1cSAl Viro 	*i = (struct iov_iter){
7458cd54c1cSAl Viro 		.iter_type = ITER_BVEC,
746245f0922SKefeng Wang 		.copy_mc = false,
7478cd54c1cSAl Viro 		.data_source = direction,
7488cd54c1cSAl Viro 		.bvec = bvec,
7498cd54c1cSAl Viro 		.nr_segs = nr_segs,
7508cd54c1cSAl Viro 		.iov_offset = 0,
7518cd54c1cSAl Viro 		.count = count
7528cd54c1cSAl Viro 	};
753d879cb83SAl Viro }
754d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec);
755d879cb83SAl Viro 
7569ea9ce04SDavid Howells /**
7577ff50620SDavid Howells  * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
7587ff50620SDavid Howells  * @i: The iterator to initialise.
7597ff50620SDavid Howells  * @direction: The direction of the transfer.
7607ff50620SDavid Howells  * @xarray: The xarray to access.
7617ff50620SDavid Howells  * @start: The start file position.
7627ff50620SDavid Howells  * @count: The size of the I/O buffer in bytes.
7637ff50620SDavid Howells  *
7647ff50620SDavid Howells  * Set up an I/O iterator to either draw data out of the pages attached to an
7657ff50620SDavid Howells  * inode or to inject data into those pages.  The pages *must* be prevented
7667ff50620SDavid Howells  * from evaporation, either by taking a ref on them or locking them by the
7677ff50620SDavid Howells  * caller.
7687ff50620SDavid Howells  */
iov_iter_xarray(struct iov_iter * i,unsigned int direction,struct xarray * xarray,loff_t start,size_t count)7697ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
7707ff50620SDavid Howells 		     struct xarray *xarray, loff_t start, size_t count)
7717ff50620SDavid Howells {
7727ff50620SDavid Howells 	BUG_ON(direction & ~1);
7738cd54c1cSAl Viro 	*i = (struct iov_iter) {
7748cd54c1cSAl Viro 		.iter_type = ITER_XARRAY,
775245f0922SKefeng Wang 		.copy_mc = false,
7768cd54c1cSAl Viro 		.data_source = direction,
7778cd54c1cSAl Viro 		.xarray = xarray,
7788cd54c1cSAl Viro 		.xarray_start = start,
7798cd54c1cSAl Viro 		.count = count,
7808cd54c1cSAl Viro 		.iov_offset = 0
7818cd54c1cSAl Viro 	};
7827ff50620SDavid Howells }
7837ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray);
7847ff50620SDavid Howells 
7857ff50620SDavid Howells /**
7869ea9ce04SDavid Howells  * iov_iter_discard - Initialise an I/O iterator that discards data
7879ea9ce04SDavid Howells  * @i: The iterator to initialise.
7889ea9ce04SDavid Howells  * @direction: The direction of the transfer.
7899ea9ce04SDavid Howells  * @count: The size of the I/O buffer in bytes.
7909ea9ce04SDavid Howells  *
7919ea9ce04SDavid Howells  * Set up an I/O iterator that just discards everything that's written to it.
7929ea9ce04SDavid Howells  * It's only available as a READ iterator.
7939ea9ce04SDavid Howells  */
iov_iter_discard(struct iov_iter * i,unsigned int direction,size_t count)7949ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
7959ea9ce04SDavid Howells {
7969ea9ce04SDavid Howells 	BUG_ON(direction != READ);
7978cd54c1cSAl Viro 	*i = (struct iov_iter){
7988cd54c1cSAl Viro 		.iter_type = ITER_DISCARD,
799245f0922SKefeng Wang 		.copy_mc = false,
8008cd54c1cSAl Viro 		.data_source = false,
8018cd54c1cSAl Viro 		.count = count,
8028cd54c1cSAl Viro 		.iov_offset = 0
8038cd54c1cSAl Viro 	};
8049ea9ce04SDavid Howells }
8059ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard);
8069ea9ce04SDavid Howells 
iov_iter_aligned_iovec(const struct iov_iter * i,unsigned addr_mask,unsigned len_mask)807cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
808cfa320f7SKeith Busch 				   unsigned len_mask)
809cfa320f7SKeith Busch {
810cfa320f7SKeith Busch 	size_t size = i->count;
811cfa320f7SKeith Busch 	size_t skip = i->iov_offset;
812cfa320f7SKeith Busch 	unsigned k;
813cfa320f7SKeith Busch 
814cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
815de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
816de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
817cfa320f7SKeith Busch 
818cfa320f7SKeith Busch 		if (len > size)
819cfa320f7SKeith Busch 			len = size;
820cfa320f7SKeith Busch 		if (len & len_mask)
821cfa320f7SKeith Busch 			return false;
822de4f5fedSJens Axboe 		if ((unsigned long)(iov->iov_base + skip) & addr_mask)
823cfa320f7SKeith Busch 			return false;
824cfa320f7SKeith Busch 
825cfa320f7SKeith Busch 		size -= len;
826cfa320f7SKeith Busch 		if (!size)
827cfa320f7SKeith Busch 			break;
828cfa320f7SKeith Busch 	}
829cfa320f7SKeith Busch 	return true;
830cfa320f7SKeith Busch }
831cfa320f7SKeith Busch 
iov_iter_aligned_bvec(const struct iov_iter * i,unsigned addr_mask,unsigned len_mask)832cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
833cfa320f7SKeith Busch 				  unsigned len_mask)
834cfa320f7SKeith Busch {
835cfa320f7SKeith Busch 	size_t size = i->count;
836cfa320f7SKeith Busch 	unsigned skip = i->iov_offset;
837cfa320f7SKeith Busch 	unsigned k;
838cfa320f7SKeith Busch 
839cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
840cfa320f7SKeith Busch 		size_t len = i->bvec[k].bv_len - skip;
841cfa320f7SKeith Busch 
842cfa320f7SKeith Busch 		if (len > size)
843cfa320f7SKeith Busch 			len = size;
844cfa320f7SKeith Busch 		if (len & len_mask)
845cfa320f7SKeith Busch 			return false;
846cfa320f7SKeith Busch 		if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
847cfa320f7SKeith Busch 			return false;
848cfa320f7SKeith Busch 
849cfa320f7SKeith Busch 		size -= len;
850cfa320f7SKeith Busch 		if (!size)
851cfa320f7SKeith Busch 			break;
852cfa320f7SKeith Busch 	}
853cfa320f7SKeith Busch 	return true;
854cfa320f7SKeith Busch }
855cfa320f7SKeith Busch 
856cfa320f7SKeith Busch /**
857cfa320f7SKeith Busch  * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
858cfa320f7SKeith Busch  * 	are aligned to the parameters.
859cfa320f7SKeith Busch  *
860cfa320f7SKeith Busch  * @i: &struct iov_iter to restore
861cfa320f7SKeith Busch  * @addr_mask: bit mask to check against the iov element's addresses
862cfa320f7SKeith Busch  * @len_mask: bit mask to check against the iov element's lengths
863cfa320f7SKeith Busch  *
864cfa320f7SKeith Busch  * Return: false if any addresses or lengths intersect with the provided masks
865cfa320f7SKeith Busch  */
iov_iter_is_aligned(const struct iov_iter * i,unsigned addr_mask,unsigned len_mask)866cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
867cfa320f7SKeith Busch 			 unsigned len_mask)
868cfa320f7SKeith Busch {
869fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
870fcb14cb1SAl Viro 		if (i->count & len_mask)
871fcb14cb1SAl Viro 			return false;
872fcb14cb1SAl Viro 		if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
873fcb14cb1SAl Viro 			return false;
874fcb14cb1SAl Viro 		return true;
875fcb14cb1SAl Viro 	}
876fcb14cb1SAl Viro 
877cfa320f7SKeith Busch 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
878cfa320f7SKeith Busch 		return iov_iter_aligned_iovec(i, addr_mask, len_mask);
879cfa320f7SKeith Busch 
880cfa320f7SKeith Busch 	if (iov_iter_is_bvec(i))
881cfa320f7SKeith Busch 		return iov_iter_aligned_bvec(i, addr_mask, len_mask);
882cfa320f7SKeith Busch 
883cfa320f7SKeith Busch 	if (iov_iter_is_xarray(i)) {
884cfa320f7SKeith Busch 		if (i->count & len_mask)
885cfa320f7SKeith Busch 			return false;
886cfa320f7SKeith Busch 		if ((i->xarray_start + i->iov_offset) & addr_mask)
887cfa320f7SKeith Busch 			return false;
888cfa320f7SKeith Busch 	}
889cfa320f7SKeith Busch 
890cfa320f7SKeith Busch 	return true;
891cfa320f7SKeith Busch }
892cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
893cfa320f7SKeith Busch 
iov_iter_alignment_iovec(const struct iov_iter * i)8949221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
895d879cb83SAl Viro {
896d879cb83SAl Viro 	unsigned long res = 0;
897d879cb83SAl Viro 	size_t size = i->count;
8989221d2e3SAl Viro 	size_t skip = i->iov_offset;
8999221d2e3SAl Viro 	unsigned k;
900d879cb83SAl Viro 
9019221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
902de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
903de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
9049221d2e3SAl Viro 		if (len) {
905de4f5fedSJens Axboe 			res |= (unsigned long)iov->iov_base + skip;
9069221d2e3SAl Viro 			if (len > size)
9079221d2e3SAl Viro 				len = size;
9089221d2e3SAl Viro 			res |= len;
9099221d2e3SAl Viro 			size -= len;
9109221d2e3SAl Viro 			if (!size)
9119221d2e3SAl Viro 				break;
9129221d2e3SAl Viro 		}
9139221d2e3SAl Viro 	}
9149221d2e3SAl Viro 	return res;
9159221d2e3SAl Viro }
9169221d2e3SAl Viro 
iov_iter_alignment_bvec(const struct iov_iter * i)9179221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
9189221d2e3SAl Viro {
9199221d2e3SAl Viro 	unsigned res = 0;
9209221d2e3SAl Viro 	size_t size = i->count;
9219221d2e3SAl Viro 	unsigned skip = i->iov_offset;
9229221d2e3SAl Viro 	unsigned k;
9239221d2e3SAl Viro 
9249221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
9259221d2e3SAl Viro 		size_t len = i->bvec[k].bv_len - skip;
9269221d2e3SAl Viro 		res |= (unsigned long)i->bvec[k].bv_offset + skip;
9279221d2e3SAl Viro 		if (len > size)
9289221d2e3SAl Viro 			len = size;
9299221d2e3SAl Viro 		res |= len;
9309221d2e3SAl Viro 		size -= len;
9319221d2e3SAl Viro 		if (!size)
9329221d2e3SAl Viro 			break;
9339221d2e3SAl Viro 	}
9349221d2e3SAl Viro 	return res;
9359221d2e3SAl Viro }
9369221d2e3SAl Viro 
iov_iter_alignment(const struct iov_iter * i)9379221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i)
9389221d2e3SAl Viro {
939fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
940fcb14cb1SAl Viro 		size_t size = i->count;
941fcb14cb1SAl Viro 		if (size)
942fcb14cb1SAl Viro 			return ((unsigned long)i->ubuf + i->iov_offset) | size;
943fcb14cb1SAl Viro 		return 0;
944fcb14cb1SAl Viro 	}
945fcb14cb1SAl Viro 
9469221d2e3SAl Viro 	/* iovec and kvec have identical layouts */
9479221d2e3SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
9489221d2e3SAl Viro 		return iov_iter_alignment_iovec(i);
9499221d2e3SAl Viro 
9509221d2e3SAl Viro 	if (iov_iter_is_bvec(i))
9519221d2e3SAl Viro 		return iov_iter_alignment_bvec(i);
9529221d2e3SAl Viro 
9539221d2e3SAl Viro 	if (iov_iter_is_xarray(i))
9543d14ec1fSDavid Howells 		return (i->xarray_start + i->iov_offset) | i->count;
9559221d2e3SAl Viro 
9569221d2e3SAl Viro 	return 0;
957d879cb83SAl Viro }
958d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment);
959d879cb83SAl Viro 
iov_iter_gap_alignment(const struct iov_iter * i)960357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
961357f435dSAl Viro {
962357f435dSAl Viro 	unsigned long res = 0;
963610c7a71SAl Viro 	unsigned long v = 0;
964357f435dSAl Viro 	size_t size = i->count;
965610c7a71SAl Viro 	unsigned k;
966357f435dSAl Viro 
967fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
968fcb14cb1SAl Viro 		return 0;
969fcb14cb1SAl Viro 
970610c7a71SAl Viro 	if (WARN_ON(!iter_is_iovec(i)))
971241699cdSAl Viro 		return ~0U;
972241699cdSAl Viro 
973610c7a71SAl Viro 	for (k = 0; k < i->nr_segs; k++) {
974de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
975de4f5fedSJens Axboe 		if (iov->iov_len) {
976de4f5fedSJens Axboe 			unsigned long base = (unsigned long)iov->iov_base;
977610c7a71SAl Viro 			if (v) // if not the first one
978610c7a71SAl Viro 				res |= base | v; // this start | previous end
979de4f5fedSJens Axboe 			v = base + iov->iov_len;
980de4f5fedSJens Axboe 			if (size <= iov->iov_len)
981610c7a71SAl Viro 				break;
982de4f5fedSJens Axboe 			size -= iov->iov_len;
983610c7a71SAl Viro 		}
984610c7a71SAl Viro 	}
985357f435dSAl Viro 	return res;
986357f435dSAl Viro }
987357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment);
988357f435dSAl Viro 
want_pages_array(struct page *** res,size_t size,size_t start,unsigned int maxpages)9893cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size,
9903cf42da3SAl Viro 			    size_t start, unsigned int maxpages)
991acbdeb83SAl Viro {
9923cf42da3SAl Viro 	unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE);
9933cf42da3SAl Viro 
9943cf42da3SAl Viro 	if (count > maxpages)
9953cf42da3SAl Viro 		count = maxpages;
9963cf42da3SAl Viro 	WARN_ON(!count);	// caller should've prevented that
9973cf42da3SAl Viro 	if (!*res) {
9983cf42da3SAl Viro 		*res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
9993cf42da3SAl Viro 		if (!*res)
10003cf42da3SAl Viro 			return 0;
10013cf42da3SAl Viro 	}
10023cf42da3SAl Viro 	return count;
1003acbdeb83SAl Viro }
1004acbdeb83SAl Viro 
iter_xarray_populate_pages(struct page ** pages,struct xarray * xa,pgoff_t index,unsigned int nr_pages)10057ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
10067ff50620SDavid Howells 					  pgoff_t index, unsigned int nr_pages)
10077ff50620SDavid Howells {
10087ff50620SDavid Howells 	XA_STATE(xas, xa, index);
10097ff50620SDavid Howells 	struct page *page;
10107ff50620SDavid Howells 	unsigned int ret = 0;
10117ff50620SDavid Howells 
10127ff50620SDavid Howells 	rcu_read_lock();
10137ff50620SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
10147ff50620SDavid Howells 		if (xas_retry(&xas, page))
10157ff50620SDavid Howells 			continue;
10167ff50620SDavid Howells 
10177ff50620SDavid Howells 		/* Has the page moved or been split? */
10187ff50620SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
10197ff50620SDavid Howells 			xas_reset(&xas);
10207ff50620SDavid Howells 			continue;
10217ff50620SDavid Howells 		}
10227ff50620SDavid Howells 
10237ff50620SDavid Howells 		pages[ret] = find_subpage(page, xas.xa_index);
10247ff50620SDavid Howells 		get_page(pages[ret]);
10257ff50620SDavid Howells 		if (++ret == nr_pages)
10267ff50620SDavid Howells 			break;
10277ff50620SDavid Howells 	}
10287ff50620SDavid Howells 	rcu_read_unlock();
10297ff50620SDavid Howells 	return ret;
10307ff50620SDavid Howells }
10317ff50620SDavid Howells 
iter_xarray_get_pages(struct iov_iter * i,struct page *** pages,size_t maxsize,unsigned maxpages,size_t * _start_offset)10327ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i,
103368fe506fSAl Viro 				     struct page ***pages, size_t maxsize,
10347ff50620SDavid Howells 				     unsigned maxpages, size_t *_start_offset)
10357ff50620SDavid Howells {
10363cf42da3SAl Viro 	unsigned nr, offset, count;
10373cf42da3SAl Viro 	pgoff_t index;
10387ff50620SDavid Howells 	loff_t pos;
10397ff50620SDavid Howells 
10407ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
10417ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
10427ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
10437ff50620SDavid Howells 	*_start_offset = offset;
10447ff50620SDavid Howells 
10453cf42da3SAl Viro 	count = want_pages_array(pages, maxsize, offset, maxpages);
10463cf42da3SAl Viro 	if (!count)
104768fe506fSAl Viro 		return -ENOMEM;
104868fe506fSAl Viro 	nr = iter_xarray_populate_pages(*pages, i->xarray, index, count);
10497ff50620SDavid Howells 	if (nr == 0)
10507ff50620SDavid Howells 		return 0;
10517ff50620SDavid Howells 
1052eba2d3d7SAl Viro 	maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1053310d9d5aSAl Viro 	i->iov_offset += maxsize;
1054310d9d5aSAl Viro 	i->count -= maxsize;
1055eba2d3d7SAl Viro 	return maxsize;
10567ff50620SDavid Howells }
10577ff50620SDavid Howells 
1058fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
first_iovec_segment(const struct iov_iter * i,size_t * size)1059dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
10603d671ca6SAl Viro {
10613d671ca6SAl Viro 	size_t skip;
10623d671ca6SAl Viro 	long k;
10633d671ca6SAl Viro 
1064fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1065fcb14cb1SAl Viro 		return (unsigned long)i->ubuf + i->iov_offset;
1066fcb14cb1SAl Viro 
10673d671ca6SAl Viro 	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1068de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
1069de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
10703d671ca6SAl Viro 
10713d671ca6SAl Viro 		if (unlikely(!len))
10723d671ca6SAl Viro 			continue;
107359dbd7d0SAl Viro 		if (*size > len)
10743d671ca6SAl Viro 			*size = len;
1075de4f5fedSJens Axboe 		return (unsigned long)iov->iov_base + skip;
10763d671ca6SAl Viro 	}
10773d671ca6SAl Viro 	BUG(); // if it had been empty, we wouldn't get called
10783d671ca6SAl Viro }
10793d671ca6SAl Viro 
10803d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */
first_bvec_segment(const struct iov_iter * i,size_t * size,size_t * start)10813d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i,
108259dbd7d0SAl Viro 				       size_t *size, size_t *start)
10833d671ca6SAl Viro {
10843d671ca6SAl Viro 	struct page *page;
10853d671ca6SAl Viro 	size_t skip = i->iov_offset, len;
10863d671ca6SAl Viro 
10873d671ca6SAl Viro 	len = i->bvec->bv_len - skip;
108859dbd7d0SAl Viro 	if (*size > len)
108959dbd7d0SAl Viro 		*size = len;
10903d671ca6SAl Viro 	skip += i->bvec->bv_offset;
10913d671ca6SAl Viro 	page = i->bvec->bv_page + skip / PAGE_SIZE;
1092dda8e5d1SAl Viro 	*start = skip % PAGE_SIZE;
10933d671ca6SAl Viro 	return page;
10943d671ca6SAl Viro }
10953d671ca6SAl Viro 
__iov_iter_get_pages_alloc(struct iov_iter * i,struct page *** pages,size_t maxsize,unsigned int maxpages,size_t * start)109691329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
1097d879cb83SAl Viro 		   struct page ***pages, size_t maxsize,
109884bd06c6SChristoph Hellwig 		   unsigned int maxpages, size_t *start)
1099d879cb83SAl Viro {
1100f62e52d1SDavid Howells 	unsigned int n, gup_flags = 0;
1101d879cb83SAl Viro 
1102d879cb83SAl Viro 	if (maxsize > i->count)
1103d879cb83SAl Viro 		maxsize = i->count;
11043d671ca6SAl Viro 	if (!maxsize)
11053d671ca6SAl Viro 		return 0;
11067392ed17SAl Viro 	if (maxsize > MAX_RW_COUNT)
11077392ed17SAl Viro 		maxsize = MAX_RW_COUNT;
1108d879cb83SAl Viro 
1109fcb14cb1SAl Viro 	if (likely(user_backed_iter(i))) {
11103d671ca6SAl Viro 		unsigned long addr;
11113cf42da3SAl Viro 		int res;
11129ea9ce04SDavid Howells 
11133337ab08SAndreas Gruenbacher 		if (iov_iter_rw(i) != WRITE)
11143337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_WRITE;
11153337ab08SAndreas Gruenbacher 		if (i->nofault)
11163337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_NOFAULT;
11173337ab08SAndreas Gruenbacher 
1118dd45ab9dSAl Viro 		addr = first_iovec_segment(i, &maxsize);
1119dd45ab9dSAl Viro 		*start = addr % PAGE_SIZE;
1120dd45ab9dSAl Viro 		addr &= PAGE_MASK;
11213cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
11223cf42da3SAl Viro 		if (!n)
1123d879cb83SAl Viro 			return -ENOMEM;
1124451c0ba9SAl Viro 		res = get_user_pages_fast(addr, n, gup_flags, *pages);
112591329559SAl Viro 		if (unlikely(res <= 0))
1126d879cb83SAl Viro 			return res;
1127eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1128eba2d3d7SAl Viro 		iov_iter_advance(i, maxsize);
1129eba2d3d7SAl Viro 		return maxsize;
11303d671ca6SAl Viro 	}
11313d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
1132451c0ba9SAl Viro 		struct page **p;
11333d671ca6SAl Viro 		struct page *page;
11343d671ca6SAl Viro 
113559dbd7d0SAl Viro 		page = first_bvec_segment(i, &maxsize, start);
11363cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
11373cf42da3SAl Viro 		if (!n)
1138d879cb83SAl Viro 			return -ENOMEM;
11393cf42da3SAl Viro 		p = *pages;
1140dda8e5d1SAl Viro 		for (int k = 0; k < n; k++)
1141eba2d3d7SAl Viro 			get_page(p[k] = page + k);
1142eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1143310d9d5aSAl Viro 		i->count -= maxsize;
1144310d9d5aSAl Viro 		i->iov_offset += maxsize;
1145310d9d5aSAl Viro 		if (i->iov_offset == i->bvec->bv_len) {
1146310d9d5aSAl Viro 			i->iov_offset = 0;
1147310d9d5aSAl Viro 			i->bvec++;
1148310d9d5aSAl Viro 			i->nr_segs--;
1149310d9d5aSAl Viro 		}
1150eba2d3d7SAl Viro 		return maxsize;
11513d671ca6SAl Viro 	}
11523d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
1153451c0ba9SAl Viro 		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1154d879cb83SAl Viro 	return -EFAULT;
1155d879cb83SAl Viro }
115691329559SAl Viro 
iov_iter_get_pages2(struct iov_iter * i,struct page ** pages,size_t maxsize,unsigned maxpages,size_t * start)115784bd06c6SChristoph Hellwig ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
115884bd06c6SChristoph Hellwig 		size_t maxsize, unsigned maxpages, size_t *start)
1159451c0ba9SAl Viro {
1160451c0ba9SAl Viro 	if (!maxpages)
1161451c0ba9SAl Viro 		return 0;
1162451c0ba9SAl Viro 	BUG_ON(!pages);
1163451c0ba9SAl Viro 
116484bd06c6SChristoph Hellwig 	return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, start);
1165451c0ba9SAl Viro }
1166eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2);
1167451c0ba9SAl Viro 
iov_iter_get_pages_alloc2(struct iov_iter * i,struct page *** pages,size_t maxsize,size_t * start)116884bd06c6SChristoph Hellwig ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i,
116984bd06c6SChristoph Hellwig 		struct page ***pages, size_t maxsize, size_t *start)
117091329559SAl Viro {
117191329559SAl Viro 	ssize_t len;
117291329559SAl Viro 
117391329559SAl Viro 	*pages = NULL;
117491329559SAl Viro 
117584bd06c6SChristoph Hellwig 	len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start);
117691329559SAl Viro 	if (len <= 0) {
117791329559SAl Viro 		kvfree(*pages);
117891329559SAl Viro 		*pages = NULL;
117991329559SAl Viro 	}
118091329559SAl Viro 	return len;
118191329559SAl Viro }
1182eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2);
1183d879cb83SAl Viro 
csum_and_copy_from_iter(void * addr,size_t bytes,__wsum * csum,struct iov_iter * i)1184d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1185d879cb83SAl Viro 			       struct iov_iter *i)
1186d879cb83SAl Viro {
1187d879cb83SAl Viro 	__wsum sum, next;
1188d879cb83SAl Viro 	sum = *csum;
1189a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source))
1190241699cdSAl Viro 		return 0;
1191a41dad90SAl Viro 
11927baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off, ({
11937baa5099SAl Viro 		next = csum_and_copy_from_user(base, addr + off, len);
1194d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
11957baa5099SAl Viro 		next ? 0 : len;
1196d879cb83SAl Viro 	}), ({
11977baa5099SAl Viro 		sum = csum_and_memcpy(addr + off, base, len, sum, off);
1198d879cb83SAl Viro 	})
1199d879cb83SAl Viro 	)
1200d879cb83SAl Viro 	*csum = sum;
1201d879cb83SAl Viro 	return bytes;
1202d879cb83SAl Viro }
1203d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter);
1204d879cb83SAl Viro 
csum_and_copy_to_iter(const void * addr,size_t bytes,void * _csstate,struct iov_iter * i)120552cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1206d879cb83SAl Viro 			     struct iov_iter *i)
1207d879cb83SAl Viro {
120852cbd23aSWillem de Bruijn 	struct csum_state *csstate = _csstate;
1209d879cb83SAl Viro 	__wsum sum, next;
121078e1f386SAl Viro 
1211a41dad90SAl Viro 	if (WARN_ON_ONCE(i->data_source))
1212a41dad90SAl Viro 		return 0;
121378e1f386SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
1214c67f1fd2SAl Viro 		// can't use csum_memcpy() for that one - data is not copied
1215c67f1fd2SAl Viro 		csstate->csum = csum_block_add(csstate->csum,
1216c67f1fd2SAl Viro 					       csum_partial(addr, bytes, 0),
1217c67f1fd2SAl Viro 					       csstate->off);
1218c67f1fd2SAl Viro 		csstate->off += bytes;
1219c67f1fd2SAl Viro 		return bytes;
1220241699cdSAl Viro 	}
12216852df12SAl Viro 
12226852df12SAl Viro 	sum = csum_shift(csstate->csum, csstate->off);
12233fc40265SDavid Howells 	iterate_and_advance(i, bytes, base, len, off, ({
12247baa5099SAl Viro 		next = csum_and_copy_to_user(addr + off, base, len);
1225d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
12267baa5099SAl Viro 		next ? 0 : len;
1227d879cb83SAl Viro 	}), ({
12287baa5099SAl Viro 		sum = csum_and_memcpy(base, addr + off, len, sum, off);
1229d879cb83SAl Viro 	})
1230d879cb83SAl Viro 	)
1231594e450bSAl Viro 	csstate->csum = csum_shift(sum, csstate->off);
1232594e450bSAl Viro 	csstate->off += bytes;
1233d879cb83SAl Viro 	return bytes;
1234d879cb83SAl Viro }
1235d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter);
1236d879cb83SAl Viro 
hash_and_copy_to_iter(const void * addr,size_t bytes,void * hashp,struct iov_iter * i)1237d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1238d05f4435SSagi Grimberg 		struct iov_iter *i)
1239d05f4435SSagi Grimberg {
12407999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH
1241d05f4435SSagi Grimberg 	struct ahash_request *hash = hashp;
1242d05f4435SSagi Grimberg 	struct scatterlist sg;
1243d05f4435SSagi Grimberg 	size_t copied;
1244d05f4435SSagi Grimberg 
1245d05f4435SSagi Grimberg 	copied = copy_to_iter(addr, bytes, i);
1246d05f4435SSagi Grimberg 	sg_init_one(&sg, addr, copied);
1247d05f4435SSagi Grimberg 	ahash_request_set_crypt(hash, &sg, NULL, copied);
1248d05f4435SSagi Grimberg 	crypto_ahash_update(hash);
1249d05f4435SSagi Grimberg 	return copied;
125027fad74aSYueHaibing #else
125127fad74aSYueHaibing 	return 0;
125227fad74aSYueHaibing #endif
1253d05f4435SSagi Grimberg }
1254d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter);
1255d05f4435SSagi Grimberg 
iov_npages(const struct iov_iter * i,int maxpages)125666531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages)
1257d879cb83SAl Viro {
125866531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
125966531c65SAl Viro 	const struct iovec *p;
1260d879cb83SAl Viro 	int npages = 0;
1261d879cb83SAl Viro 
1262de4f5fedSJens Axboe 	for (p = iter_iov(i); size; skip = 0, p++) {
126366531c65SAl Viro 		unsigned offs = offset_in_page(p->iov_base + skip);
126466531c65SAl Viro 		size_t len = min(p->iov_len - skip, size);
1265d879cb83SAl Viro 
126666531c65SAl Viro 		if (len) {
126766531c65SAl Viro 			size -= len;
126866531c65SAl Viro 			npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
126966531c65SAl Viro 			if (unlikely(npages > maxpages))
127066531c65SAl Viro 				return maxpages;
127166531c65SAl Viro 		}
127266531c65SAl Viro 	}
127366531c65SAl Viro 	return npages;
127466531c65SAl Viro }
127566531c65SAl Viro 
bvec_npages(const struct iov_iter * i,int maxpages)127666531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages)
127766531c65SAl Viro {
127866531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
127966531c65SAl Viro 	const struct bio_vec *p;
128066531c65SAl Viro 	int npages = 0;
128166531c65SAl Viro 
128266531c65SAl Viro 	for (p = i->bvec; size; skip = 0, p++) {
128366531c65SAl Viro 		unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
128466531c65SAl Viro 		size_t len = min(p->bv_len - skip, size);
128566531c65SAl Viro 
128666531c65SAl Viro 		size -= len;
128766531c65SAl Viro 		npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
128866531c65SAl Viro 		if (unlikely(npages > maxpages))
128966531c65SAl Viro 			return maxpages;
129066531c65SAl Viro 	}
129166531c65SAl Viro 	return npages;
129266531c65SAl Viro }
129366531c65SAl Viro 
iov_iter_npages(const struct iov_iter * i,int maxpages)129466531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages)
129566531c65SAl Viro {
129666531c65SAl Viro 	if (unlikely(!i->count))
129766531c65SAl Viro 		return 0;
1298fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1299fcb14cb1SAl Viro 		unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1300fcb14cb1SAl Viro 		int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1301fcb14cb1SAl Viro 		return min(npages, maxpages);
1302fcb14cb1SAl Viro 	}
130366531c65SAl Viro 	/* iovec and kvec have identical layouts */
130466531c65SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
130566531c65SAl Viro 		return iov_npages(i, maxpages);
130666531c65SAl Viro 	if (iov_iter_is_bvec(i))
130766531c65SAl Viro 		return bvec_npages(i, maxpages);
130866531c65SAl Viro 	if (iov_iter_is_xarray(i)) {
1309e4f8df86SAl Viro 		unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1310e4f8df86SAl Viro 		int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
131166531c65SAl Viro 		return min(npages, maxpages);
131266531c65SAl Viro 	}
131366531c65SAl Viro 	return 0;
1314d879cb83SAl Viro }
1315d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages);
1316d879cb83SAl Viro 
dup_iter(struct iov_iter * new,struct iov_iter * old,gfp_t flags)1317d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1318d879cb83SAl Viro {
1319d879cb83SAl Viro 	*new = *old;
132000e23707SDavid Howells 	if (iov_iter_is_bvec(new))
1321d879cb83SAl Viro 		return new->bvec = kmemdup(new->bvec,
1322d879cb83SAl Viro 				    new->nr_segs * sizeof(struct bio_vec),
1323d879cb83SAl Viro 				    flags);
1324fcb14cb1SAl Viro 	else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1325d879cb83SAl Viro 		/* iovec and kvec have identical layout */
1326de4f5fedSJens Axboe 		return new->__iov = kmemdup(new->__iov,
1327d879cb83SAl Viro 				   new->nr_segs * sizeof(struct iovec),
1328d879cb83SAl Viro 				   flags);
1329fcb14cb1SAl Viro 	return NULL;
1330d879cb83SAl Viro }
1331d879cb83SAl Viro EXPORT_SYMBOL(dup_iter);
1332bc917be8SAl Viro 
copy_compat_iovec_from_user(struct iovec * iov,const struct iovec __user * uvec,unsigned long nr_segs)133350f9a76eSJosh Poimboeuf static __noclone int copy_compat_iovec_from_user(struct iovec *iov,
1334bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1335bfdc5970SChristoph Hellwig {
1336bfdc5970SChristoph Hellwig 	const struct compat_iovec __user *uiov =
1337bfdc5970SChristoph Hellwig 		(const struct compat_iovec __user *)uvec;
1338bfdc5970SChristoph Hellwig 	int ret = -EFAULT, i;
1339bfdc5970SChristoph Hellwig 
1340a959a978SChristoph Hellwig 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1341bfdc5970SChristoph Hellwig 		return -EFAULT;
1342bfdc5970SChristoph Hellwig 
1343bfdc5970SChristoph Hellwig 	for (i = 0; i < nr_segs; i++) {
1344bfdc5970SChristoph Hellwig 		compat_uptr_t buf;
1345bfdc5970SChristoph Hellwig 		compat_ssize_t len;
1346bfdc5970SChristoph Hellwig 
1347bfdc5970SChristoph Hellwig 		unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1348bfdc5970SChristoph Hellwig 		unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1349bfdc5970SChristoph Hellwig 
1350bfdc5970SChristoph Hellwig 		/* check for compat_size_t not fitting in compat_ssize_t .. */
1351bfdc5970SChristoph Hellwig 		if (len < 0) {
1352bfdc5970SChristoph Hellwig 			ret = -EINVAL;
1353bfdc5970SChristoph Hellwig 			goto uaccess_end;
1354bfdc5970SChristoph Hellwig 		}
1355bfdc5970SChristoph Hellwig 		iov[i].iov_base = compat_ptr(buf);
1356bfdc5970SChristoph Hellwig 		iov[i].iov_len = len;
1357bfdc5970SChristoph Hellwig 	}
1358bfdc5970SChristoph Hellwig 
1359bfdc5970SChristoph Hellwig 	ret = 0;
1360bfdc5970SChristoph Hellwig uaccess_end:
1361bfdc5970SChristoph Hellwig 	user_access_end();
1362bfdc5970SChristoph Hellwig 	return ret;
1363bfdc5970SChristoph Hellwig }
1364bfdc5970SChristoph Hellwig 
copy_iovec_from_user(struct iovec * iov,const struct iovec __user * uiov,unsigned long nr_segs)1365719a937bSPeter Zijlstra static __noclone int copy_iovec_from_user(struct iovec *iov,
1366487c20b0SLinus Torvalds 		const struct iovec __user *uiov, unsigned long nr_segs)
1367fb041b59SDavid Laight {
1368487c20b0SLinus Torvalds 	int ret = -EFAULT;
1369bfdc5970SChristoph Hellwig 
1370487c20b0SLinus Torvalds 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1371bfdc5970SChristoph Hellwig 		return -EFAULT;
1372bfdc5970SChristoph Hellwig 
1373487c20b0SLinus Torvalds 	do {
1374487c20b0SLinus Torvalds 		void __user *buf;
1375487c20b0SLinus Torvalds 		ssize_t len;
1376487c20b0SLinus Torvalds 
1377487c20b0SLinus Torvalds 		unsafe_get_user(len, &uiov->iov_len, uaccess_end);
1378487c20b0SLinus Torvalds 		unsafe_get_user(buf, &uiov->iov_base, uaccess_end);
1379487c20b0SLinus Torvalds 
1380487c20b0SLinus Torvalds 		/* check for size_t not fitting in ssize_t .. */
1381487c20b0SLinus Torvalds 		if (unlikely(len < 0)) {
1382487c20b0SLinus Torvalds 			ret = -EINVAL;
1383487c20b0SLinus Torvalds 			goto uaccess_end;
1384487c20b0SLinus Torvalds 		}
1385487c20b0SLinus Torvalds 		iov->iov_base = buf;
1386487c20b0SLinus Torvalds 		iov->iov_len = len;
1387487c20b0SLinus Torvalds 
1388487c20b0SLinus Torvalds 		uiov++; iov++;
1389487c20b0SLinus Torvalds 	} while (--nr_segs);
1390487c20b0SLinus Torvalds 
1391487c20b0SLinus Torvalds 	ret = 0;
1392487c20b0SLinus Torvalds uaccess_end:
1393487c20b0SLinus Torvalds 	user_access_end();
1394487c20b0SLinus Torvalds 	return ret;
1395bfdc5970SChristoph Hellwig }
1396bfdc5970SChristoph Hellwig 
iovec_from_user(const struct iovec __user * uvec,unsigned long nr_segs,unsigned long fast_segs,struct iovec * fast_iov,bool compat)1397bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec,
1398bfdc5970SChristoph Hellwig 		unsigned long nr_segs, unsigned long fast_segs,
1399bfdc5970SChristoph Hellwig 		struct iovec *fast_iov, bool compat)
1400bfdc5970SChristoph Hellwig {
1401bfdc5970SChristoph Hellwig 	struct iovec *iov = fast_iov;
1402bfdc5970SChristoph Hellwig 	int ret;
1403fb041b59SDavid Laight 
1404fb041b59SDavid Laight 	/*
1405bfdc5970SChristoph Hellwig 	 * SuS says "The readv() function *may* fail if the iovcnt argument was
1406bfdc5970SChristoph Hellwig 	 * less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1407fb041b59SDavid Laight 	 * traditionally returned zero for zero segments, so...
1408fb041b59SDavid Laight 	 */
1409bfdc5970SChristoph Hellwig 	if (nr_segs == 0)
1410bfdc5970SChristoph Hellwig 		return iov;
1411bfdc5970SChristoph Hellwig 	if (nr_segs > UIO_MAXIOV)
1412bfdc5970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1413fb041b59SDavid Laight 	if (nr_segs > fast_segs) {
1414fb041b59SDavid Laight 		iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1415bfdc5970SChristoph Hellwig 		if (!iov)
1416bfdc5970SChristoph Hellwig 			return ERR_PTR(-ENOMEM);
1417fb041b59SDavid Laight 	}
1418bfdc5970SChristoph Hellwig 
1419487c20b0SLinus Torvalds 	if (unlikely(compat))
1420bfdc5970SChristoph Hellwig 		ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1421bfdc5970SChristoph Hellwig 	else
1422bfdc5970SChristoph Hellwig 		ret = copy_iovec_from_user(iov, uvec, nr_segs);
1423bfdc5970SChristoph Hellwig 	if (ret) {
1424bfdc5970SChristoph Hellwig 		if (iov != fast_iov)
1425bfdc5970SChristoph Hellwig 			kfree(iov);
1426bfdc5970SChristoph Hellwig 		return ERR_PTR(ret);
1427fb041b59SDavid Laight 	}
1428bfdc5970SChristoph Hellwig 
1429bfdc5970SChristoph Hellwig 	return iov;
1430bfdc5970SChristoph Hellwig }
1431bfdc5970SChristoph Hellwig 
14323b2deb0eSJens Axboe /*
14333b2deb0eSJens Axboe  * Single segment iovec supplied by the user, import it as ITER_UBUF.
14343b2deb0eSJens Axboe  */
__import_iovec_ubuf(int type,const struct iovec __user * uvec,struct iovec ** iovp,struct iov_iter * i,bool compat)14353b2deb0eSJens Axboe static ssize_t __import_iovec_ubuf(int type, const struct iovec __user *uvec,
14363b2deb0eSJens Axboe 				   struct iovec **iovp, struct iov_iter *i,
14373b2deb0eSJens Axboe 				   bool compat)
14383b2deb0eSJens Axboe {
14393b2deb0eSJens Axboe 	struct iovec *iov = *iovp;
14403b2deb0eSJens Axboe 	ssize_t ret;
14413b2deb0eSJens Axboe 
14423b2deb0eSJens Axboe 	if (compat)
14433b2deb0eSJens Axboe 		ret = copy_compat_iovec_from_user(iov, uvec, 1);
14443b2deb0eSJens Axboe 	else
14453b2deb0eSJens Axboe 		ret = copy_iovec_from_user(iov, uvec, 1);
14463b2deb0eSJens Axboe 	if (unlikely(ret))
14473b2deb0eSJens Axboe 		return ret;
14483b2deb0eSJens Axboe 
14493b2deb0eSJens Axboe 	ret = import_ubuf(type, iov->iov_base, iov->iov_len, i);
14503b2deb0eSJens Axboe 	if (unlikely(ret))
14513b2deb0eSJens Axboe 		return ret;
14523b2deb0eSJens Axboe 	*iovp = NULL;
14533b2deb0eSJens Axboe 	return i->count;
14543b2deb0eSJens Axboe }
14553b2deb0eSJens Axboe 
__import_iovec(int type,const struct iovec __user * uvec,unsigned nr_segs,unsigned fast_segs,struct iovec ** iovp,struct iov_iter * i,bool compat)1456bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1457bfdc5970SChristoph Hellwig 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1458bfdc5970SChristoph Hellwig 		 struct iov_iter *i, bool compat)
1459bfdc5970SChristoph Hellwig {
1460bfdc5970SChristoph Hellwig 	ssize_t total_len = 0;
1461bfdc5970SChristoph Hellwig 	unsigned long seg;
1462bfdc5970SChristoph Hellwig 	struct iovec *iov;
1463bfdc5970SChristoph Hellwig 
14643b2deb0eSJens Axboe 	if (nr_segs == 1)
14653b2deb0eSJens Axboe 		return __import_iovec_ubuf(type, uvec, iovp, i, compat);
14663b2deb0eSJens Axboe 
1467bfdc5970SChristoph Hellwig 	iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1468bfdc5970SChristoph Hellwig 	if (IS_ERR(iov)) {
1469bfdc5970SChristoph Hellwig 		*iovp = NULL;
1470bfdc5970SChristoph Hellwig 		return PTR_ERR(iov);
1471fb041b59SDavid Laight 	}
1472fb041b59SDavid Laight 
1473fb041b59SDavid Laight 	/*
1474bfdc5970SChristoph Hellwig 	 * According to the Single Unix Specification we should return EINVAL if
1475bfdc5970SChristoph Hellwig 	 * an element length is < 0 when cast to ssize_t or if the total length
1476bfdc5970SChristoph Hellwig 	 * would overflow the ssize_t return value of the system call.
1477fb041b59SDavid Laight 	 *
1478fb041b59SDavid Laight 	 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1479fb041b59SDavid Laight 	 * overflow case.
1480fb041b59SDavid Laight 	 */
1481fb041b59SDavid Laight 	for (seg = 0; seg < nr_segs; seg++) {
1482fb041b59SDavid Laight 		ssize_t len = (ssize_t)iov[seg].iov_len;
1483fb041b59SDavid Laight 
1484bfdc5970SChristoph Hellwig 		if (!access_ok(iov[seg].iov_base, len)) {
1485bfdc5970SChristoph Hellwig 			if (iov != *iovp)
1486bfdc5970SChristoph Hellwig 				kfree(iov);
1487bfdc5970SChristoph Hellwig 			*iovp = NULL;
1488bfdc5970SChristoph Hellwig 			return -EFAULT;
1489fb041b59SDavid Laight 		}
1490bfdc5970SChristoph Hellwig 
1491bfdc5970SChristoph Hellwig 		if (len > MAX_RW_COUNT - total_len) {
1492bfdc5970SChristoph Hellwig 			len = MAX_RW_COUNT - total_len;
1493fb041b59SDavid Laight 			iov[seg].iov_len = len;
1494fb041b59SDavid Laight 		}
1495bfdc5970SChristoph Hellwig 		total_len += len;
1496fb041b59SDavid Laight 	}
1497bfdc5970SChristoph Hellwig 
1498bfdc5970SChristoph Hellwig 	iov_iter_init(i, type, iov, nr_segs, total_len);
1499bfdc5970SChristoph Hellwig 	if (iov == *iovp)
1500bfdc5970SChristoph Hellwig 		*iovp = NULL;
1501bfdc5970SChristoph Hellwig 	else
1502bfdc5970SChristoph Hellwig 		*iovp = iov;
1503bfdc5970SChristoph Hellwig 	return total_len;
1504fb041b59SDavid Laight }
1505fb041b59SDavid Laight 
1506ffecee4fSVegard Nossum /**
1507ffecee4fSVegard Nossum  * import_iovec() - Copy an array of &struct iovec from userspace
1508ffecee4fSVegard Nossum  *     into the kernel, check that it is valid, and initialize a new
1509ffecee4fSVegard Nossum  *     &struct iov_iter iterator to access it.
1510ffecee4fSVegard Nossum  *
1511ffecee4fSVegard Nossum  * @type: One of %READ or %WRITE.
1512bfdc5970SChristoph Hellwig  * @uvec: Pointer to the userspace array.
1513ffecee4fSVegard Nossum  * @nr_segs: Number of elements in userspace array.
1514ffecee4fSVegard Nossum  * @fast_segs: Number of elements in @iov.
1515bfdc5970SChristoph Hellwig  * @iovp: (input and output parameter) Pointer to pointer to (usually small
1516ffecee4fSVegard Nossum  *     on-stack) kernel array.
1517ffecee4fSVegard Nossum  * @i: Pointer to iterator that will be initialized on success.
1518ffecee4fSVegard Nossum  *
1519ffecee4fSVegard Nossum  * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1520ffecee4fSVegard Nossum  * then this function places %NULL in *@iov on return. Otherwise, a new
1521ffecee4fSVegard Nossum  * array will be allocated and the result placed in *@iov. This means that
1522ffecee4fSVegard Nossum  * the caller may call kfree() on *@iov regardless of whether the small
1523ffecee4fSVegard Nossum  * on-stack array was used or not (and regardless of whether this function
1524ffecee4fSVegard Nossum  * returns an error or not).
1525ffecee4fSVegard Nossum  *
152687e5e6daSJens Axboe  * Return: Negative error code on error, bytes imported on success
1527ffecee4fSVegard Nossum  */
import_iovec(int type,const struct iovec __user * uvec,unsigned nr_segs,unsigned fast_segs,struct iovec ** iovp,struct iov_iter * i)1528bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec,
1529bc917be8SAl Viro 		 unsigned nr_segs, unsigned fast_segs,
1530bfdc5970SChristoph Hellwig 		 struct iovec **iovp, struct iov_iter *i)
1531bc917be8SAl Viro {
153289cd35c5SChristoph Hellwig 	return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
153389cd35c5SChristoph Hellwig 			      in_compat_syscall());
1534bc917be8SAl Viro }
1535bc917be8SAl Viro EXPORT_SYMBOL(import_iovec);
1536bc917be8SAl Viro 
import_single_range(int rw,void __user * buf,size_t len,struct iovec * iov,struct iov_iter * i)1537bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len,
1538bc917be8SAl Viro 		 struct iovec *iov, struct iov_iter *i)
1539bc917be8SAl Viro {
1540bc917be8SAl Viro 	if (len > MAX_RW_COUNT)
1541bc917be8SAl Viro 		len = MAX_RW_COUNT;
154296d4f267SLinus Torvalds 	if (unlikely(!access_ok(buf, len)))
1543bc917be8SAl Viro 		return -EFAULT;
1544bc917be8SAl Viro 
1545e03ad4eeSJens Axboe 	iov_iter_ubuf(i, rw, buf, len);
1546bc917be8SAl Viro 	return 0;
1547bc917be8SAl Viro }
1548e1267585SAl Viro EXPORT_SYMBOL(import_single_range);
15498fb0f47aSJens Axboe 
import_ubuf(int rw,void __user * buf,size_t len,struct iov_iter * i)15502ad9bd83SJens Axboe int import_ubuf(int rw, void __user *buf, size_t len, struct iov_iter *i)
15512ad9bd83SJens Axboe {
15522ad9bd83SJens Axboe 	if (len > MAX_RW_COUNT)
15532ad9bd83SJens Axboe 		len = MAX_RW_COUNT;
15542ad9bd83SJens Axboe 	if (unlikely(!access_ok(buf, len)))
15552ad9bd83SJens Axboe 		return -EFAULT;
15562ad9bd83SJens Axboe 
15572ad9bd83SJens Axboe 	iov_iter_ubuf(i, rw, buf, len);
15582ad9bd83SJens Axboe 	return 0;
15592ad9bd83SJens Axboe }
156070e969ebSTakashi Iwai EXPORT_SYMBOL_GPL(import_ubuf);
15612ad9bd83SJens Axboe 
15628fb0f47aSJens Axboe /**
15638fb0f47aSJens Axboe  * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
15648fb0f47aSJens Axboe  *     iov_iter_save_state() was called.
15658fb0f47aSJens Axboe  *
15668fb0f47aSJens Axboe  * @i: &struct iov_iter to restore
15678fb0f47aSJens Axboe  * @state: state to restore from
15688fb0f47aSJens Axboe  *
15698fb0f47aSJens Axboe  * Used after iov_iter_save_state() to bring restore @i, if operations may
15708fb0f47aSJens Axboe  * have advanced it.
15718fb0f47aSJens Axboe  *
15728fb0f47aSJens Axboe  * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
15738fb0f47aSJens Axboe  */
iov_iter_restore(struct iov_iter * i,struct iov_iter_state * state)15748fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
15758fb0f47aSJens Axboe {
15764397a17cSKeith Busch 	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) &&
15774397a17cSKeith Busch 			 !iter_is_ubuf(i)) && !iov_iter_is_kvec(i))
15788fb0f47aSJens Axboe 		return;
15798fb0f47aSJens Axboe 	i->iov_offset = state->iov_offset;
15808fb0f47aSJens Axboe 	i->count = state->count;
1581fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1582fcb14cb1SAl Viro 		return;
15838fb0f47aSJens Axboe 	/*
15848fb0f47aSJens Axboe 	 * For the *vec iters, nr_segs + iov is constant - if we increment
15858fb0f47aSJens Axboe 	 * the vec, then we also decrement the nr_segs count. Hence we don't
15868fb0f47aSJens Axboe 	 * need to track both of these, just one is enough and we can deduct
15878fb0f47aSJens Axboe 	 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
15888fb0f47aSJens Axboe 	 * size, so we can just increment the iov pointer as they are unionzed.
15898fb0f47aSJens Axboe 	 * ITER_BVEC _may_ be the same size on some archs, but on others it is
15908fb0f47aSJens Axboe 	 * not. Be safe and handle it separately.
15918fb0f47aSJens Axboe 	 */
15928fb0f47aSJens Axboe 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
15938fb0f47aSJens Axboe 	if (iov_iter_is_bvec(i))
15948fb0f47aSJens Axboe 		i->bvec -= state->nr_segs - i->nr_segs;
15958fb0f47aSJens Axboe 	else
1596de4f5fedSJens Axboe 		i->__iov -= state->nr_segs - i->nr_segs;
15978fb0f47aSJens Axboe 	i->nr_segs = state->nr_segs;
15988fb0f47aSJens Axboe }
15997d58fe73SDavid Howells 
16007d58fe73SDavid Howells /*
16017d58fe73SDavid Howells  * Extract a list of contiguous pages from an ITER_XARRAY iterator.  This does not
16027d58fe73SDavid Howells  * get references on the pages, nor does it get a pin on them.
16037d58fe73SDavid Howells  */
iov_iter_extract_xarray_pages(struct iov_iter * i,struct page *** pages,size_t maxsize,unsigned int maxpages,iov_iter_extraction_t extraction_flags,size_t * offset0)16047d58fe73SDavid Howells static ssize_t iov_iter_extract_xarray_pages(struct iov_iter *i,
16057d58fe73SDavid Howells 					     struct page ***pages, size_t maxsize,
16067d58fe73SDavid Howells 					     unsigned int maxpages,
16077d58fe73SDavid Howells 					     iov_iter_extraction_t extraction_flags,
16087d58fe73SDavid Howells 					     size_t *offset0)
16097d58fe73SDavid Howells {
16107d58fe73SDavid Howells 	struct page *page, **p;
16117d58fe73SDavid Howells 	unsigned int nr = 0, offset;
16127d58fe73SDavid Howells 	loff_t pos = i->xarray_start + i->iov_offset;
16137d58fe73SDavid Howells 	pgoff_t index = pos >> PAGE_SHIFT;
16147d58fe73SDavid Howells 	XA_STATE(xas, i->xarray, index);
16157d58fe73SDavid Howells 
16167d58fe73SDavid Howells 	offset = pos & ~PAGE_MASK;
16177d58fe73SDavid Howells 	*offset0 = offset;
16187d58fe73SDavid Howells 
16197d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
16207d58fe73SDavid Howells 	if (!maxpages)
16217d58fe73SDavid Howells 		return -ENOMEM;
16227d58fe73SDavid Howells 	p = *pages;
16237d58fe73SDavid Howells 
16247d58fe73SDavid Howells 	rcu_read_lock();
16257d58fe73SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
16267d58fe73SDavid Howells 		if (xas_retry(&xas, page))
16277d58fe73SDavid Howells 			continue;
16287d58fe73SDavid Howells 
16297d58fe73SDavid Howells 		/* Has the page moved or been split? */
16307d58fe73SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
16317d58fe73SDavid Howells 			xas_reset(&xas);
16327d58fe73SDavid Howells 			continue;
16337d58fe73SDavid Howells 		}
16347d58fe73SDavid Howells 
16357d58fe73SDavid Howells 		p[nr++] = find_subpage(page, xas.xa_index);
16367d58fe73SDavid Howells 		if (nr == maxpages)
16377d58fe73SDavid Howells 			break;
16387d58fe73SDavid Howells 	}
16397d58fe73SDavid Howells 	rcu_read_unlock();
16407d58fe73SDavid Howells 
16417d58fe73SDavid Howells 	maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
16427d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
16437d58fe73SDavid Howells 	return maxsize;
16447d58fe73SDavid Howells }
16457d58fe73SDavid Howells 
16467d58fe73SDavid Howells /*
16477d58fe73SDavid Howells  * Extract a list of contiguous pages from an ITER_BVEC iterator.  This does
16487d58fe73SDavid Howells  * not get references on the pages, nor does it get a pin on them.
16497d58fe73SDavid Howells  */
iov_iter_extract_bvec_pages(struct iov_iter * i,struct page *** pages,size_t maxsize,unsigned int maxpages,iov_iter_extraction_t extraction_flags,size_t * offset0)16507d58fe73SDavid Howells static ssize_t iov_iter_extract_bvec_pages(struct iov_iter *i,
16517d58fe73SDavid Howells 					   struct page ***pages, size_t maxsize,
16527d58fe73SDavid Howells 					   unsigned int maxpages,
16537d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
16547d58fe73SDavid Howells 					   size_t *offset0)
16557d58fe73SDavid Howells {
16567d58fe73SDavid Howells 	struct page **p, *page;
1657*f741bd71SDavid Howells 	size_t skip = i->iov_offset, offset, size;
16587d58fe73SDavid Howells 	int k;
16597d58fe73SDavid Howells 
16607d58fe73SDavid Howells 	for (;;) {
16617d58fe73SDavid Howells 		if (i->nr_segs == 0)
16627d58fe73SDavid Howells 			return 0;
1663*f741bd71SDavid Howells 		size = min(maxsize, i->bvec->bv_len - skip);
1664*f741bd71SDavid Howells 		if (size)
16657d58fe73SDavid Howells 			break;
16667d58fe73SDavid Howells 		i->iov_offset = 0;
16677d58fe73SDavid Howells 		i->nr_segs--;
16687d58fe73SDavid Howells 		i->bvec++;
16697d58fe73SDavid Howells 		skip = 0;
16707d58fe73SDavid Howells 	}
16717d58fe73SDavid Howells 
16727d58fe73SDavid Howells 	skip += i->bvec->bv_offset;
16737d58fe73SDavid Howells 	page = i->bvec->bv_page + skip / PAGE_SIZE;
16747d58fe73SDavid Howells 	offset = skip % PAGE_SIZE;
16757d58fe73SDavid Howells 	*offset0 = offset;
16767d58fe73SDavid Howells 
1677*f741bd71SDavid Howells 	maxpages = want_pages_array(pages, size, offset, maxpages);
16787d58fe73SDavid Howells 	if (!maxpages)
16797d58fe73SDavid Howells 		return -ENOMEM;
16807d58fe73SDavid Howells 	p = *pages;
16817d58fe73SDavid Howells 	for (k = 0; k < maxpages; k++)
16827d58fe73SDavid Howells 		p[k] = page + k;
16837d58fe73SDavid Howells 
1684*f741bd71SDavid Howells 	size = min_t(size_t, size, maxpages * PAGE_SIZE - offset);
1685*f741bd71SDavid Howells 	iov_iter_advance(i, size);
1686*f741bd71SDavid Howells 	return size;
16877d58fe73SDavid Howells }
16887d58fe73SDavid Howells 
16897d58fe73SDavid Howells /*
16907d58fe73SDavid Howells  * Extract a list of virtually contiguous pages from an ITER_KVEC iterator.
16917d58fe73SDavid Howells  * This does not get references on the pages, nor does it get a pin on them.
16927d58fe73SDavid Howells  */
iov_iter_extract_kvec_pages(struct iov_iter * i,struct page *** pages,size_t maxsize,unsigned int maxpages,iov_iter_extraction_t extraction_flags,size_t * offset0)16937d58fe73SDavid Howells static ssize_t iov_iter_extract_kvec_pages(struct iov_iter *i,
16947d58fe73SDavid Howells 					   struct page ***pages, size_t maxsize,
16957d58fe73SDavid Howells 					   unsigned int maxpages,
16967d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
16977d58fe73SDavid Howells 					   size_t *offset0)
16987d58fe73SDavid Howells {
16997d58fe73SDavid Howells 	struct page **p, *page;
17007d58fe73SDavid Howells 	const void *kaddr;
1701*f741bd71SDavid Howells 	size_t skip = i->iov_offset, offset, len, size;
17027d58fe73SDavid Howells 	int k;
17037d58fe73SDavid Howells 
17047d58fe73SDavid Howells 	for (;;) {
17057d58fe73SDavid Howells 		if (i->nr_segs == 0)
17067d58fe73SDavid Howells 			return 0;
1707*f741bd71SDavid Howells 		size = min(maxsize, i->kvec->iov_len - skip);
1708*f741bd71SDavid Howells 		if (size)
17097d58fe73SDavid Howells 			break;
17107d58fe73SDavid Howells 		i->iov_offset = 0;
17117d58fe73SDavid Howells 		i->nr_segs--;
17127d58fe73SDavid Howells 		i->kvec++;
17137d58fe73SDavid Howells 		skip = 0;
17147d58fe73SDavid Howells 	}
17157d58fe73SDavid Howells 
17167d58fe73SDavid Howells 	kaddr = i->kvec->iov_base + skip;
17177d58fe73SDavid Howells 	offset = (unsigned long)kaddr & ~PAGE_MASK;
17187d58fe73SDavid Howells 	*offset0 = offset;
17197d58fe73SDavid Howells 
1720*f741bd71SDavid Howells 	maxpages = want_pages_array(pages, size, offset, maxpages);
17217d58fe73SDavid Howells 	if (!maxpages)
17227d58fe73SDavid Howells 		return -ENOMEM;
17237d58fe73SDavid Howells 	p = *pages;
17247d58fe73SDavid Howells 
17257d58fe73SDavid Howells 	kaddr -= offset;
1726*f741bd71SDavid Howells 	len = offset + size;
17277d58fe73SDavid Howells 	for (k = 0; k < maxpages; k++) {
17287d58fe73SDavid Howells 		size_t seg = min_t(size_t, len, PAGE_SIZE);
17297d58fe73SDavid Howells 
17307d58fe73SDavid Howells 		if (is_vmalloc_or_module_addr(kaddr))
17317d58fe73SDavid Howells 			page = vmalloc_to_page(kaddr);
17327d58fe73SDavid Howells 		else
17337d58fe73SDavid Howells 			page = virt_to_page(kaddr);
17347d58fe73SDavid Howells 
17357d58fe73SDavid Howells 		p[k] = page;
17367d58fe73SDavid Howells 		len -= seg;
17377d58fe73SDavid Howells 		kaddr += PAGE_SIZE;
17387d58fe73SDavid Howells 	}
17397d58fe73SDavid Howells 
1740*f741bd71SDavid Howells 	size = min_t(size_t, size, maxpages * PAGE_SIZE - offset);
1741*f741bd71SDavid Howells 	iov_iter_advance(i, size);
1742*f741bd71SDavid Howells 	return size;
17437d58fe73SDavid Howells }
17447d58fe73SDavid Howells 
17457d58fe73SDavid Howells /*
17467d58fe73SDavid Howells  * Extract a list of contiguous pages from a user iterator and get a pin on
17477d58fe73SDavid Howells  * each of them.  This should only be used if the iterator is user-backed
17487d58fe73SDavid Howells  * (IOBUF/UBUF).
17497d58fe73SDavid Howells  *
17507d58fe73SDavid Howells  * It does not get refs on the pages, but the pages must be unpinned by the
17517d58fe73SDavid Howells  * caller once the transfer is complete.
17527d58fe73SDavid Howells  *
17537d58fe73SDavid Howells  * This is safe to be used where background IO/DMA *is* going to be modifying
17547d58fe73SDavid Howells  * the buffer; using a pin rather than a ref makes forces fork() to give the
17557d58fe73SDavid Howells  * child a copy of the page.
17567d58fe73SDavid Howells  */
iov_iter_extract_user_pages(struct iov_iter * i,struct page *** pages,size_t maxsize,unsigned int maxpages,iov_iter_extraction_t extraction_flags,size_t * offset0)17577d58fe73SDavid Howells static ssize_t iov_iter_extract_user_pages(struct iov_iter *i,
17587d58fe73SDavid Howells 					   struct page ***pages,
17597d58fe73SDavid Howells 					   size_t maxsize,
17607d58fe73SDavid Howells 					   unsigned int maxpages,
17617d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
17627d58fe73SDavid Howells 					   size_t *offset0)
17637d58fe73SDavid Howells {
17647d58fe73SDavid Howells 	unsigned long addr;
17657d58fe73SDavid Howells 	unsigned int gup_flags = 0;
17667d58fe73SDavid Howells 	size_t offset;
17677d58fe73SDavid Howells 	int res;
17687d58fe73SDavid Howells 
17697d58fe73SDavid Howells 	if (i->data_source == ITER_DEST)
17707d58fe73SDavid Howells 		gup_flags |= FOLL_WRITE;
17717d58fe73SDavid Howells 	if (extraction_flags & ITER_ALLOW_P2PDMA)
17727d58fe73SDavid Howells 		gup_flags |= FOLL_PCI_P2PDMA;
17737d58fe73SDavid Howells 	if (i->nofault)
17747d58fe73SDavid Howells 		gup_flags |= FOLL_NOFAULT;
17757d58fe73SDavid Howells 
17767d58fe73SDavid Howells 	addr = first_iovec_segment(i, &maxsize);
17777d58fe73SDavid Howells 	*offset0 = offset = addr % PAGE_SIZE;
17787d58fe73SDavid Howells 	addr &= PAGE_MASK;
17797d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
17807d58fe73SDavid Howells 	if (!maxpages)
17817d58fe73SDavid Howells 		return -ENOMEM;
17827d58fe73SDavid Howells 	res = pin_user_pages_fast(addr, maxpages, gup_flags, *pages);
17837d58fe73SDavid Howells 	if (unlikely(res <= 0))
17847d58fe73SDavid Howells 		return res;
17857d58fe73SDavid Howells 	maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - offset);
17867d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
17877d58fe73SDavid Howells 	return maxsize;
17887d58fe73SDavid Howells }
17897d58fe73SDavid Howells 
17907d58fe73SDavid Howells /**
17917d58fe73SDavid Howells  * iov_iter_extract_pages - Extract a list of contiguous pages from an iterator
17927d58fe73SDavid Howells  * @i: The iterator to extract from
17937d58fe73SDavid Howells  * @pages: Where to return the list of pages
17947d58fe73SDavid Howells  * @maxsize: The maximum amount of iterator to extract
17957d58fe73SDavid Howells  * @maxpages: The maximum size of the list of pages
17967d58fe73SDavid Howells  * @extraction_flags: Flags to qualify request
17977d58fe73SDavid Howells  * @offset0: Where to return the starting offset into (*@pages)[0]
17987d58fe73SDavid Howells  *
17997d58fe73SDavid Howells  * Extract a list of contiguous pages from the current point of the iterator,
18007d58fe73SDavid Howells  * advancing the iterator.  The maximum number of pages and the maximum amount
18017d58fe73SDavid Howells  * of page contents can be set.
18027d58fe73SDavid Howells  *
18037d58fe73SDavid Howells  * If *@pages is NULL, a page list will be allocated to the required size and
18047d58fe73SDavid Howells  * *@pages will be set to its base.  If *@pages is not NULL, it will be assumed
18057d58fe73SDavid Howells  * that the caller allocated a page list at least @maxpages in size and this
18067d58fe73SDavid Howells  * will be filled in.
18077d58fe73SDavid Howells  *
18087d58fe73SDavid Howells  * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA
18097d58fe73SDavid Howells  * be allowed on the pages extracted.
18107d58fe73SDavid Howells  *
18117d58fe73SDavid Howells  * The iov_iter_extract_will_pin() function can be used to query how cleanup
18127d58fe73SDavid Howells  * should be performed.
18137d58fe73SDavid Howells  *
18147d58fe73SDavid Howells  * Extra refs or pins on the pages may be obtained as follows:
18157d58fe73SDavid Howells  *
18167d58fe73SDavid Howells  *  (*) If the iterator is user-backed (ITER_IOVEC/ITER_UBUF), pins will be
18177d58fe73SDavid Howells  *      added to the pages, but refs will not be taken.
18187d58fe73SDavid Howells  *      iov_iter_extract_will_pin() will return true.
18197d58fe73SDavid Howells  *
18207d58fe73SDavid Howells  *  (*) If the iterator is ITER_KVEC, ITER_BVEC or ITER_XARRAY, the pages are
18217d58fe73SDavid Howells  *      merely listed; no extra refs or pins are obtained.
18227d58fe73SDavid Howells  *      iov_iter_extract_will_pin() will return 0.
18237d58fe73SDavid Howells  *
18247d58fe73SDavid Howells  * Note also:
18257d58fe73SDavid Howells  *
18267d58fe73SDavid Howells  *  (*) Use with ITER_DISCARD is not supported as that has no content.
18277d58fe73SDavid Howells  *
18287d58fe73SDavid Howells  * On success, the function sets *@pages to the new pagelist, if allocated, and
18297d58fe73SDavid Howells  * sets *offset0 to the offset into the first page.
18307d58fe73SDavid Howells  *
18317d58fe73SDavid Howells  * It may also return -ENOMEM and -EFAULT.
18327d58fe73SDavid Howells  */
iov_iter_extract_pages(struct iov_iter * i,struct page *** pages,size_t maxsize,unsigned int maxpages,iov_iter_extraction_t extraction_flags,size_t * offset0)18337d58fe73SDavid Howells ssize_t iov_iter_extract_pages(struct iov_iter *i,
18347d58fe73SDavid Howells 			       struct page ***pages,
18357d58fe73SDavid Howells 			       size_t maxsize,
18367d58fe73SDavid Howells 			       unsigned int maxpages,
18377d58fe73SDavid Howells 			       iov_iter_extraction_t extraction_flags,
18387d58fe73SDavid Howells 			       size_t *offset0)
18397d58fe73SDavid Howells {
18407d58fe73SDavid Howells 	maxsize = min_t(size_t, min_t(size_t, maxsize, i->count), MAX_RW_COUNT);
18417d58fe73SDavid Howells 	if (!maxsize)
18427d58fe73SDavid Howells 		return 0;
18437d58fe73SDavid Howells 
18447d58fe73SDavid Howells 	if (likely(user_backed_iter(i)))
18457d58fe73SDavid Howells 		return iov_iter_extract_user_pages(i, pages, maxsize,
18467d58fe73SDavid Howells 						   maxpages, extraction_flags,
18477d58fe73SDavid Howells 						   offset0);
18487d58fe73SDavid Howells 	if (iov_iter_is_kvec(i))
18497d58fe73SDavid Howells 		return iov_iter_extract_kvec_pages(i, pages, maxsize,
18507d58fe73SDavid Howells 						   maxpages, extraction_flags,
18517d58fe73SDavid Howells 						   offset0);
18527d58fe73SDavid Howells 	if (iov_iter_is_bvec(i))
18537d58fe73SDavid Howells 		return iov_iter_extract_bvec_pages(i, pages, maxsize,
18547d58fe73SDavid Howells 						   maxpages, extraction_flags,
18557d58fe73SDavid Howells 						   offset0);
18567d58fe73SDavid Howells 	if (iov_iter_is_xarray(i))
18577d58fe73SDavid Howells 		return iov_iter_extract_xarray_pages(i, pages, maxsize,
18587d58fe73SDavid Howells 						     maxpages, extraction_flags,
18597d58fe73SDavid Howells 						     offset0);
18607d58fe73SDavid Howells 	return -EFAULT;
18617d58fe73SDavid Howells }
18627d58fe73SDavid Howells EXPORT_SYMBOL_GPL(iov_iter_extract_pages);
1863