xref: /openbmc/linux/lib/iov_iter.c (revision d37cf9b63113f13d742713881ce691fc615d8b3b)
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;
5734f7ffa83SHugh Dickins 	bool uses_kmap = IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP) ||
5744f7ffa83SHugh Dickins 			 PageHighMem(page);
575f7f9a0c8SMatthew Wilcox (Oracle) 
576f7f9a0c8SMatthew Wilcox (Oracle) 	if (!page_copy_sane(page, offset, bytes))
57772e809edSAl Viro 		return 0;
578f7f9a0c8SMatthew Wilcox (Oracle) 	if (WARN_ON_ONCE(!i->data_source))
579241699cdSAl Viro 		return 0;
580f7f9a0c8SMatthew Wilcox (Oracle) 
581908a1ad8SMatthew Wilcox (Oracle) 	do {
582908a1ad8SMatthew Wilcox (Oracle) 		char *p;
583908a1ad8SMatthew Wilcox (Oracle) 
584908a1ad8SMatthew Wilcox (Oracle) 		n = bytes - copied;
5854f7ffa83SHugh Dickins 		if (uses_kmap) {
586908a1ad8SMatthew Wilcox (Oracle) 			page += offset / PAGE_SIZE;
587908a1ad8SMatthew Wilcox (Oracle) 			offset %= PAGE_SIZE;
588908a1ad8SMatthew Wilcox (Oracle) 			n = min_t(size_t, n, PAGE_SIZE - offset);
589908a1ad8SMatthew Wilcox (Oracle) 		}
590908a1ad8SMatthew Wilcox (Oracle) 
591f7f9a0c8SMatthew Wilcox (Oracle) 		p = kmap_atomic(page) + offset;
592908a1ad8SMatthew Wilcox (Oracle) 		iterate_and_advance(i, n, base, len, off,
5937baa5099SAl Viro 			copyin(p + off, base, len),
594245f0922SKefeng Wang 			memcpy_from_iter(i, p + off, base, len)
595d879cb83SAl Viro 		)
596f7f9a0c8SMatthew Wilcox (Oracle) 		kunmap_atomic(p);
597908a1ad8SMatthew Wilcox (Oracle) 		copied += n;
598908a1ad8SMatthew Wilcox (Oracle) 		offset += n;
5994f7ffa83SHugh Dickins 	} while (uses_kmap && copied != bytes && n > 0);
600f7f9a0c8SMatthew Wilcox (Oracle) 
601908a1ad8SMatthew Wilcox (Oracle) 	return copied;
602d879cb83SAl Viro }
603f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic);
604d879cb83SAl Viro 
iov_iter_bvec_advance(struct iov_iter * i,size_t size)60554c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
60654c8195bSPavel Begunkov {
60718fa9af7SAl Viro 	const struct bio_vec *bvec, *end;
60854c8195bSPavel Begunkov 
60918fa9af7SAl Viro 	if (!i->count)
61018fa9af7SAl Viro 		return;
61118fa9af7SAl Viro 	i->count -= size;
61254c8195bSPavel Begunkov 
61318fa9af7SAl Viro 	size += i->iov_offset;
61418fa9af7SAl Viro 
61518fa9af7SAl Viro 	for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
61618fa9af7SAl Viro 		if (likely(size < bvec->bv_len))
61718fa9af7SAl Viro 			break;
61818fa9af7SAl Viro 		size -= bvec->bv_len;
61918fa9af7SAl Viro 	}
62018fa9af7SAl Viro 	i->iov_offset = size;
62118fa9af7SAl Viro 	i->nr_segs -= bvec - i->bvec;
62218fa9af7SAl Viro 	i->bvec = bvec;
62354c8195bSPavel Begunkov }
62454c8195bSPavel Begunkov 
iov_iter_iovec_advance(struct iov_iter * i,size_t size)625185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
626185ac4d4SAl Viro {
627185ac4d4SAl Viro 	const struct iovec *iov, *end;
628185ac4d4SAl Viro 
629185ac4d4SAl Viro 	if (!i->count)
630185ac4d4SAl Viro 		return;
631185ac4d4SAl Viro 	i->count -= size;
632185ac4d4SAl Viro 
633185ac4d4SAl Viro 	size += i->iov_offset; // from beginning of current segment
634de4f5fedSJens Axboe 	for (iov = iter_iov(i), end = iov + i->nr_segs; iov < end; iov++) {
635185ac4d4SAl Viro 		if (likely(size < iov->iov_len))
636185ac4d4SAl Viro 			break;
637185ac4d4SAl Viro 		size -= iov->iov_len;
638185ac4d4SAl Viro 	}
639185ac4d4SAl Viro 	i->iov_offset = size;
640de4f5fedSJens Axboe 	i->nr_segs -= iov - iter_iov(i);
641de4f5fedSJens Axboe 	i->__iov = iov;
642185ac4d4SAl Viro }
643185ac4d4SAl Viro 
iov_iter_advance(struct iov_iter * i,size_t size)644d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size)
645d879cb83SAl Viro {
6463b3fc051SAl Viro 	if (unlikely(i->count < size))
6473b3fc051SAl Viro 		size = i->count;
648fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
649fcb14cb1SAl Viro 		i->iov_offset += size;
650fcb14cb1SAl Viro 		i->count -= size;
651fcb14cb1SAl Viro 	} else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
652185ac4d4SAl Viro 		/* iovec and kvec have identical layouts */
653185ac4d4SAl Viro 		iov_iter_iovec_advance(i, size);
654185ac4d4SAl Viro 	} else if (iov_iter_is_bvec(i)) {
655185ac4d4SAl Viro 		iov_iter_bvec_advance(i, size);
656185ac4d4SAl Viro 	} else if (iov_iter_is_discard(i)) {
657185ac4d4SAl Viro 		i->count -= size;
6587ff50620SDavid Howells 	}
659d879cb83SAl Viro }
660d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance);
661d879cb83SAl Viro 
iov_iter_revert(struct iov_iter * i,size_t unroll)66227c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll)
66327c0e374SAl Viro {
66427c0e374SAl Viro 	if (!unroll)
66527c0e374SAl Viro 		return;
6665b47d59aSAl Viro 	if (WARN_ON(unroll > MAX_RW_COUNT))
6675b47d59aSAl Viro 		return;
66827c0e374SAl Viro 	i->count += unroll;
6699ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_discard(i)))
6709ea9ce04SDavid Howells 		return;
67127c0e374SAl Viro 	if (unroll <= i->iov_offset) {
67227c0e374SAl Viro 		i->iov_offset -= unroll;
67327c0e374SAl Viro 		return;
67427c0e374SAl Viro 	}
67527c0e374SAl Viro 	unroll -= i->iov_offset;
676fcb14cb1SAl Viro 	if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
6777ff50620SDavid Howells 		BUG(); /* We should never go beyond the start of the specified
6787ff50620SDavid Howells 			* range since we might then be straying into pages that
6797ff50620SDavid Howells 			* aren't pinned.
6807ff50620SDavid Howells 			*/
6817ff50620SDavid Howells 	} else if (iov_iter_is_bvec(i)) {
68227c0e374SAl Viro 		const struct bio_vec *bvec = i->bvec;
68327c0e374SAl Viro 		while (1) {
68427c0e374SAl Viro 			size_t n = (--bvec)->bv_len;
68527c0e374SAl Viro 			i->nr_segs++;
68627c0e374SAl Viro 			if (unroll <= n) {
68727c0e374SAl Viro 				i->bvec = bvec;
68827c0e374SAl Viro 				i->iov_offset = n - unroll;
68927c0e374SAl Viro 				return;
69027c0e374SAl Viro 			}
69127c0e374SAl Viro 			unroll -= n;
69227c0e374SAl Viro 		}
69327c0e374SAl Viro 	} else { /* same logics for iovec and kvec */
694de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i);
69527c0e374SAl Viro 		while (1) {
69627c0e374SAl Viro 			size_t n = (--iov)->iov_len;
69727c0e374SAl Viro 			i->nr_segs++;
69827c0e374SAl Viro 			if (unroll <= n) {
699de4f5fedSJens Axboe 				i->__iov = iov;
70027c0e374SAl Viro 				i->iov_offset = n - unroll;
70127c0e374SAl Viro 				return;
70227c0e374SAl Viro 			}
70327c0e374SAl Viro 			unroll -= n;
70427c0e374SAl Viro 		}
70527c0e374SAl Viro 	}
70627c0e374SAl Viro }
70727c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert);
70827c0e374SAl Viro 
709d879cb83SAl Viro /*
710d879cb83SAl Viro  * Return the count of just the current iov_iter segment.
711d879cb83SAl Viro  */
iov_iter_single_seg_count(const struct iov_iter * i)712d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i)
713d879cb83SAl Viro {
71428f38db7SAl Viro 	if (i->nr_segs > 1) {
71528f38db7SAl Viro 		if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
716de4f5fedSJens Axboe 			return min(i->count, iter_iov(i)->iov_len - i->iov_offset);
7177ff50620SDavid Howells 		if (iov_iter_is_bvec(i))
718d879cb83SAl Viro 			return min(i->count, i->bvec->bv_len - i->iov_offset);
71928f38db7SAl Viro 	}
72028f38db7SAl Viro 	return i->count;
721d879cb83SAl Viro }
722d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count);
723d879cb83SAl Viro 
iov_iter_kvec(struct iov_iter * i,unsigned int direction,const struct kvec * kvec,unsigned long nr_segs,size_t count)724aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
725d879cb83SAl Viro 			const struct kvec *kvec, unsigned long nr_segs,
726d879cb83SAl Viro 			size_t count)
727d879cb83SAl Viro {
728aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
7298cd54c1cSAl Viro 	*i = (struct iov_iter){
7308cd54c1cSAl Viro 		.iter_type = ITER_KVEC,
731245f0922SKefeng Wang 		.copy_mc = false,
7328cd54c1cSAl Viro 		.data_source = direction,
7338cd54c1cSAl Viro 		.kvec = kvec,
7348cd54c1cSAl Viro 		.nr_segs = nr_segs,
7358cd54c1cSAl Viro 		.iov_offset = 0,
7368cd54c1cSAl Viro 		.count = count
7378cd54c1cSAl Viro 	};
738d879cb83SAl Viro }
739d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec);
740d879cb83SAl Viro 
iov_iter_bvec(struct iov_iter * i,unsigned int direction,const struct bio_vec * bvec,unsigned long nr_segs,size_t count)741aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
742d879cb83SAl Viro 			const struct bio_vec *bvec, unsigned long nr_segs,
743d879cb83SAl Viro 			size_t count)
744d879cb83SAl Viro {
745aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
7468cd54c1cSAl Viro 	*i = (struct iov_iter){
7478cd54c1cSAl Viro 		.iter_type = ITER_BVEC,
748245f0922SKefeng Wang 		.copy_mc = false,
7498cd54c1cSAl Viro 		.data_source = direction,
7508cd54c1cSAl Viro 		.bvec = bvec,
7518cd54c1cSAl Viro 		.nr_segs = nr_segs,
7528cd54c1cSAl Viro 		.iov_offset = 0,
7538cd54c1cSAl Viro 		.count = count
7548cd54c1cSAl Viro 	};
755d879cb83SAl Viro }
756d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec);
757d879cb83SAl Viro 
7589ea9ce04SDavid Howells /**
7597ff50620SDavid Howells  * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
7607ff50620SDavid Howells  * @i: The iterator to initialise.
7617ff50620SDavid Howells  * @direction: The direction of the transfer.
7627ff50620SDavid Howells  * @xarray: The xarray to access.
7637ff50620SDavid Howells  * @start: The start file position.
7647ff50620SDavid Howells  * @count: The size of the I/O buffer in bytes.
7657ff50620SDavid Howells  *
7667ff50620SDavid Howells  * Set up an I/O iterator to either draw data out of the pages attached to an
7677ff50620SDavid Howells  * inode or to inject data into those pages.  The pages *must* be prevented
7687ff50620SDavid Howells  * from evaporation, either by taking a ref on them or locking them by the
7697ff50620SDavid Howells  * caller.
7707ff50620SDavid Howells  */
iov_iter_xarray(struct iov_iter * i,unsigned int direction,struct xarray * xarray,loff_t start,size_t count)7717ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
7727ff50620SDavid Howells 		     struct xarray *xarray, loff_t start, size_t count)
7737ff50620SDavid Howells {
7747ff50620SDavid Howells 	BUG_ON(direction & ~1);
7758cd54c1cSAl Viro 	*i = (struct iov_iter) {
7768cd54c1cSAl Viro 		.iter_type = ITER_XARRAY,
777245f0922SKefeng Wang 		.copy_mc = false,
7788cd54c1cSAl Viro 		.data_source = direction,
7798cd54c1cSAl Viro 		.xarray = xarray,
7808cd54c1cSAl Viro 		.xarray_start = start,
7818cd54c1cSAl Viro 		.count = count,
7828cd54c1cSAl Viro 		.iov_offset = 0
7838cd54c1cSAl Viro 	};
7847ff50620SDavid Howells }
7857ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray);
7867ff50620SDavid Howells 
7877ff50620SDavid Howells /**
7889ea9ce04SDavid Howells  * iov_iter_discard - Initialise an I/O iterator that discards data
7899ea9ce04SDavid Howells  * @i: The iterator to initialise.
7909ea9ce04SDavid Howells  * @direction: The direction of the transfer.
7919ea9ce04SDavid Howells  * @count: The size of the I/O buffer in bytes.
7929ea9ce04SDavid Howells  *
7939ea9ce04SDavid Howells  * Set up an I/O iterator that just discards everything that's written to it.
7949ea9ce04SDavid Howells  * It's only available as a READ iterator.
7959ea9ce04SDavid Howells  */
iov_iter_discard(struct iov_iter * i,unsigned int direction,size_t count)7969ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
7979ea9ce04SDavid Howells {
7989ea9ce04SDavid Howells 	BUG_ON(direction != READ);
7998cd54c1cSAl Viro 	*i = (struct iov_iter){
8008cd54c1cSAl Viro 		.iter_type = ITER_DISCARD,
801245f0922SKefeng Wang 		.copy_mc = false,
8028cd54c1cSAl Viro 		.data_source = false,
8038cd54c1cSAl Viro 		.count = count,
8048cd54c1cSAl Viro 		.iov_offset = 0
8058cd54c1cSAl Viro 	};
8069ea9ce04SDavid Howells }
8079ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard);
8089ea9ce04SDavid Howells 
iov_iter_aligned_iovec(const struct iov_iter * i,unsigned addr_mask,unsigned len_mask)809cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
810cfa320f7SKeith Busch 				   unsigned len_mask)
811cfa320f7SKeith Busch {
812cfa320f7SKeith Busch 	size_t size = i->count;
813cfa320f7SKeith Busch 	size_t skip = i->iov_offset;
814cfa320f7SKeith Busch 	unsigned k;
815cfa320f7SKeith Busch 
816cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
817de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
818de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
819cfa320f7SKeith Busch 
820cfa320f7SKeith Busch 		if (len > size)
821cfa320f7SKeith Busch 			len = size;
822cfa320f7SKeith Busch 		if (len & len_mask)
823cfa320f7SKeith Busch 			return false;
824de4f5fedSJens Axboe 		if ((unsigned long)(iov->iov_base + skip) & addr_mask)
825cfa320f7SKeith Busch 			return false;
826cfa320f7SKeith Busch 
827cfa320f7SKeith Busch 		size -= len;
828cfa320f7SKeith Busch 		if (!size)
829cfa320f7SKeith Busch 			break;
830cfa320f7SKeith Busch 	}
831cfa320f7SKeith Busch 	return true;
832cfa320f7SKeith Busch }
833cfa320f7SKeith Busch 
iov_iter_aligned_bvec(const struct iov_iter * i,unsigned addr_mask,unsigned len_mask)834cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
835cfa320f7SKeith Busch 				  unsigned len_mask)
836cfa320f7SKeith Busch {
837cfa320f7SKeith Busch 	size_t size = i->count;
838cfa320f7SKeith Busch 	unsigned skip = i->iov_offset;
839cfa320f7SKeith Busch 	unsigned k;
840cfa320f7SKeith Busch 
841cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
842cfa320f7SKeith Busch 		size_t len = i->bvec[k].bv_len - skip;
843cfa320f7SKeith Busch 
844cfa320f7SKeith Busch 		if (len > size)
845cfa320f7SKeith Busch 			len = size;
846cfa320f7SKeith Busch 		if (len & len_mask)
847cfa320f7SKeith Busch 			return false;
848cfa320f7SKeith Busch 		if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
849cfa320f7SKeith Busch 			return false;
850cfa320f7SKeith Busch 
851cfa320f7SKeith Busch 		size -= len;
852cfa320f7SKeith Busch 		if (!size)
853cfa320f7SKeith Busch 			break;
854cfa320f7SKeith Busch 	}
855cfa320f7SKeith Busch 	return true;
856cfa320f7SKeith Busch }
857cfa320f7SKeith Busch 
858cfa320f7SKeith Busch /**
859cfa320f7SKeith Busch  * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
860cfa320f7SKeith Busch  * 	are aligned to the parameters.
861cfa320f7SKeith Busch  *
862cfa320f7SKeith Busch  * @i: &struct iov_iter to restore
863cfa320f7SKeith Busch  * @addr_mask: bit mask to check against the iov element's addresses
864cfa320f7SKeith Busch  * @len_mask: bit mask to check against the iov element's lengths
865cfa320f7SKeith Busch  *
866cfa320f7SKeith Busch  * Return: false if any addresses or lengths intersect with the provided masks
867cfa320f7SKeith Busch  */
iov_iter_is_aligned(const struct iov_iter * i,unsigned addr_mask,unsigned len_mask)868cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
869cfa320f7SKeith Busch 			 unsigned len_mask)
870cfa320f7SKeith Busch {
871fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
872fcb14cb1SAl Viro 		if (i->count & len_mask)
873fcb14cb1SAl Viro 			return false;
874fcb14cb1SAl Viro 		if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
875fcb14cb1SAl Viro 			return false;
876fcb14cb1SAl Viro 		return true;
877fcb14cb1SAl Viro 	}
878fcb14cb1SAl Viro 
879cfa320f7SKeith Busch 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
880cfa320f7SKeith Busch 		return iov_iter_aligned_iovec(i, addr_mask, len_mask);
881cfa320f7SKeith Busch 
882cfa320f7SKeith Busch 	if (iov_iter_is_bvec(i))
883cfa320f7SKeith Busch 		return iov_iter_aligned_bvec(i, addr_mask, len_mask);
884cfa320f7SKeith Busch 
885cfa320f7SKeith Busch 	if (iov_iter_is_xarray(i)) {
886cfa320f7SKeith Busch 		if (i->count & len_mask)
887cfa320f7SKeith Busch 			return false;
888cfa320f7SKeith Busch 		if ((i->xarray_start + i->iov_offset) & addr_mask)
889cfa320f7SKeith Busch 			return false;
890cfa320f7SKeith Busch 	}
891cfa320f7SKeith Busch 
892cfa320f7SKeith Busch 	return true;
893cfa320f7SKeith Busch }
894cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
895cfa320f7SKeith Busch 
iov_iter_alignment_iovec(const struct iov_iter * i)8969221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
897d879cb83SAl Viro {
898d879cb83SAl Viro 	unsigned long res = 0;
899d879cb83SAl Viro 	size_t size = i->count;
9009221d2e3SAl Viro 	size_t skip = i->iov_offset;
9019221d2e3SAl Viro 	unsigned k;
902d879cb83SAl Viro 
9039221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
904de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
905de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
9069221d2e3SAl Viro 		if (len) {
907de4f5fedSJens Axboe 			res |= (unsigned long)iov->iov_base + skip;
9089221d2e3SAl Viro 			if (len > size)
9099221d2e3SAl Viro 				len = size;
9109221d2e3SAl Viro 			res |= len;
9119221d2e3SAl Viro 			size -= len;
9129221d2e3SAl Viro 			if (!size)
9139221d2e3SAl Viro 				break;
9149221d2e3SAl Viro 		}
9159221d2e3SAl Viro 	}
9169221d2e3SAl Viro 	return res;
9179221d2e3SAl Viro }
9189221d2e3SAl Viro 
iov_iter_alignment_bvec(const struct iov_iter * i)9199221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
9209221d2e3SAl Viro {
9219221d2e3SAl Viro 	unsigned res = 0;
9229221d2e3SAl Viro 	size_t size = i->count;
9239221d2e3SAl Viro 	unsigned skip = i->iov_offset;
9249221d2e3SAl Viro 	unsigned k;
9259221d2e3SAl Viro 
9269221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
9279221d2e3SAl Viro 		size_t len = i->bvec[k].bv_len - skip;
9289221d2e3SAl Viro 		res |= (unsigned long)i->bvec[k].bv_offset + skip;
9299221d2e3SAl Viro 		if (len > size)
9309221d2e3SAl Viro 			len = size;
9319221d2e3SAl Viro 		res |= len;
9329221d2e3SAl Viro 		size -= len;
9339221d2e3SAl Viro 		if (!size)
9349221d2e3SAl Viro 			break;
9359221d2e3SAl Viro 	}
9369221d2e3SAl Viro 	return res;
9379221d2e3SAl Viro }
9389221d2e3SAl Viro 
iov_iter_alignment(const struct iov_iter * i)9399221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i)
9409221d2e3SAl Viro {
941fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
942fcb14cb1SAl Viro 		size_t size = i->count;
943fcb14cb1SAl Viro 		if (size)
944fcb14cb1SAl Viro 			return ((unsigned long)i->ubuf + i->iov_offset) | size;
945fcb14cb1SAl Viro 		return 0;
946fcb14cb1SAl Viro 	}
947fcb14cb1SAl Viro 
9489221d2e3SAl Viro 	/* iovec and kvec have identical layouts */
9499221d2e3SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
9509221d2e3SAl Viro 		return iov_iter_alignment_iovec(i);
9519221d2e3SAl Viro 
9529221d2e3SAl Viro 	if (iov_iter_is_bvec(i))
9539221d2e3SAl Viro 		return iov_iter_alignment_bvec(i);
9549221d2e3SAl Viro 
9559221d2e3SAl Viro 	if (iov_iter_is_xarray(i))
9563d14ec1fSDavid Howells 		return (i->xarray_start + i->iov_offset) | i->count;
9579221d2e3SAl Viro 
9589221d2e3SAl Viro 	return 0;
959d879cb83SAl Viro }
960d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment);
961d879cb83SAl Viro 
iov_iter_gap_alignment(const struct iov_iter * i)962357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
963357f435dSAl Viro {
964357f435dSAl Viro 	unsigned long res = 0;
965610c7a71SAl Viro 	unsigned long v = 0;
966357f435dSAl Viro 	size_t size = i->count;
967610c7a71SAl Viro 	unsigned k;
968357f435dSAl Viro 
969fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
970fcb14cb1SAl Viro 		return 0;
971fcb14cb1SAl Viro 
972610c7a71SAl Viro 	if (WARN_ON(!iter_is_iovec(i)))
973241699cdSAl Viro 		return ~0U;
974241699cdSAl Viro 
975610c7a71SAl Viro 	for (k = 0; k < i->nr_segs; k++) {
976de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
977de4f5fedSJens Axboe 		if (iov->iov_len) {
978de4f5fedSJens Axboe 			unsigned long base = (unsigned long)iov->iov_base;
979610c7a71SAl Viro 			if (v) // if not the first one
980610c7a71SAl Viro 				res |= base | v; // this start | previous end
981de4f5fedSJens Axboe 			v = base + iov->iov_len;
982de4f5fedSJens Axboe 			if (size <= iov->iov_len)
983610c7a71SAl Viro 				break;
984de4f5fedSJens Axboe 			size -= iov->iov_len;
985610c7a71SAl Viro 		}
986610c7a71SAl Viro 	}
987357f435dSAl Viro 	return res;
988357f435dSAl Viro }
989357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment);
990357f435dSAl Viro 
want_pages_array(struct page *** res,size_t size,size_t start,unsigned int maxpages)9913cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size,
9923cf42da3SAl Viro 			    size_t start, unsigned int maxpages)
993acbdeb83SAl Viro {
9943cf42da3SAl Viro 	unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE);
9953cf42da3SAl Viro 
9963cf42da3SAl Viro 	if (count > maxpages)
9973cf42da3SAl Viro 		count = maxpages;
9983cf42da3SAl Viro 	WARN_ON(!count);	// caller should've prevented that
9993cf42da3SAl Viro 	if (!*res) {
10003cf42da3SAl Viro 		*res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
10013cf42da3SAl Viro 		if (!*res)
10023cf42da3SAl Viro 			return 0;
10033cf42da3SAl Viro 	}
10043cf42da3SAl Viro 	return count;
1005acbdeb83SAl Viro }
1006acbdeb83SAl Viro 
iter_xarray_populate_pages(struct page ** pages,struct xarray * xa,pgoff_t index,unsigned int nr_pages)10077ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
10087ff50620SDavid Howells 					  pgoff_t index, unsigned int nr_pages)
10097ff50620SDavid Howells {
10107ff50620SDavid Howells 	XA_STATE(xas, xa, index);
10117ff50620SDavid Howells 	struct page *page;
10127ff50620SDavid Howells 	unsigned int ret = 0;
10137ff50620SDavid Howells 
10147ff50620SDavid Howells 	rcu_read_lock();
10157ff50620SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
10167ff50620SDavid Howells 		if (xas_retry(&xas, page))
10177ff50620SDavid Howells 			continue;
10187ff50620SDavid Howells 
10197ff50620SDavid Howells 		/* Has the page moved or been split? */
10207ff50620SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
10217ff50620SDavid Howells 			xas_reset(&xas);
10227ff50620SDavid Howells 			continue;
10237ff50620SDavid Howells 		}
10247ff50620SDavid Howells 
10257ff50620SDavid Howells 		pages[ret] = find_subpage(page, xas.xa_index);
10267ff50620SDavid Howells 		get_page(pages[ret]);
10277ff50620SDavid Howells 		if (++ret == nr_pages)
10287ff50620SDavid Howells 			break;
10297ff50620SDavid Howells 	}
10307ff50620SDavid Howells 	rcu_read_unlock();
10317ff50620SDavid Howells 	return ret;
10327ff50620SDavid Howells }
10337ff50620SDavid Howells 
iter_xarray_get_pages(struct iov_iter * i,struct page *** pages,size_t maxsize,unsigned maxpages,size_t * _start_offset)10347ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i,
103568fe506fSAl Viro 				     struct page ***pages, size_t maxsize,
10367ff50620SDavid Howells 				     unsigned maxpages, size_t *_start_offset)
10377ff50620SDavid Howells {
10383cf42da3SAl Viro 	unsigned nr, offset, count;
10393cf42da3SAl Viro 	pgoff_t index;
10407ff50620SDavid Howells 	loff_t pos;
10417ff50620SDavid Howells 
10427ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
10437ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
10447ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
10457ff50620SDavid Howells 	*_start_offset = offset;
10467ff50620SDavid Howells 
10473cf42da3SAl Viro 	count = want_pages_array(pages, maxsize, offset, maxpages);
10483cf42da3SAl Viro 	if (!count)
104968fe506fSAl Viro 		return -ENOMEM;
105068fe506fSAl Viro 	nr = iter_xarray_populate_pages(*pages, i->xarray, index, count);
10517ff50620SDavid Howells 	if (nr == 0)
10527ff50620SDavid Howells 		return 0;
10537ff50620SDavid Howells 
1054eba2d3d7SAl Viro 	maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1055310d9d5aSAl Viro 	i->iov_offset += maxsize;
1056310d9d5aSAl Viro 	i->count -= maxsize;
1057eba2d3d7SAl Viro 	return maxsize;
10587ff50620SDavid Howells }
10597ff50620SDavid Howells 
1060fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
first_iovec_segment(const struct iov_iter * i,size_t * size)1061dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
10623d671ca6SAl Viro {
10633d671ca6SAl Viro 	size_t skip;
10643d671ca6SAl Viro 	long k;
10653d671ca6SAl Viro 
1066fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1067fcb14cb1SAl Viro 		return (unsigned long)i->ubuf + i->iov_offset;
1068fcb14cb1SAl Viro 
10693d671ca6SAl Viro 	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1070de4f5fedSJens Axboe 		const struct iovec *iov = iter_iov(i) + k;
1071de4f5fedSJens Axboe 		size_t len = iov->iov_len - skip;
10723d671ca6SAl Viro 
10733d671ca6SAl Viro 		if (unlikely(!len))
10743d671ca6SAl Viro 			continue;
107559dbd7d0SAl Viro 		if (*size > len)
10763d671ca6SAl Viro 			*size = len;
1077de4f5fedSJens Axboe 		return (unsigned long)iov->iov_base + skip;
10783d671ca6SAl Viro 	}
10793d671ca6SAl Viro 	BUG(); // if it had been empty, we wouldn't get called
10803d671ca6SAl Viro }
10813d671ca6SAl Viro 
10823d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */
first_bvec_segment(const struct iov_iter * i,size_t * size,size_t * start)10833d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i,
108459dbd7d0SAl Viro 				       size_t *size, size_t *start)
10853d671ca6SAl Viro {
10863d671ca6SAl Viro 	struct page *page;
10873d671ca6SAl Viro 	size_t skip = i->iov_offset, len;
10883d671ca6SAl Viro 
10893d671ca6SAl Viro 	len = i->bvec->bv_len - skip;
109059dbd7d0SAl Viro 	if (*size > len)
109159dbd7d0SAl Viro 		*size = len;
10923d671ca6SAl Viro 	skip += i->bvec->bv_offset;
10933d671ca6SAl Viro 	page = i->bvec->bv_page + skip / PAGE_SIZE;
1094dda8e5d1SAl Viro 	*start = skip % PAGE_SIZE;
10953d671ca6SAl Viro 	return page;
10963d671ca6SAl Viro }
10973d671ca6SAl Viro 
__iov_iter_get_pages_alloc(struct iov_iter * i,struct page *** pages,size_t maxsize,unsigned int maxpages,size_t * start)109891329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
1099d879cb83SAl Viro 		   struct page ***pages, size_t maxsize,
110084bd06c6SChristoph Hellwig 		   unsigned int maxpages, size_t *start)
1101d879cb83SAl Viro {
1102f62e52d1SDavid Howells 	unsigned int n, gup_flags = 0;
1103d879cb83SAl Viro 
1104d879cb83SAl Viro 	if (maxsize > i->count)
1105d879cb83SAl Viro 		maxsize = i->count;
11063d671ca6SAl Viro 	if (!maxsize)
11073d671ca6SAl Viro 		return 0;
11087392ed17SAl Viro 	if (maxsize > MAX_RW_COUNT)
11097392ed17SAl Viro 		maxsize = MAX_RW_COUNT;
1110d879cb83SAl Viro 
1111fcb14cb1SAl Viro 	if (likely(user_backed_iter(i))) {
11123d671ca6SAl Viro 		unsigned long addr;
11133cf42da3SAl Viro 		int res;
11149ea9ce04SDavid Howells 
11153337ab08SAndreas Gruenbacher 		if (iov_iter_rw(i) != WRITE)
11163337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_WRITE;
11173337ab08SAndreas Gruenbacher 		if (i->nofault)
11183337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_NOFAULT;
11193337ab08SAndreas Gruenbacher 
1120dd45ab9dSAl Viro 		addr = first_iovec_segment(i, &maxsize);
1121dd45ab9dSAl Viro 		*start = addr % PAGE_SIZE;
1122dd45ab9dSAl Viro 		addr &= PAGE_MASK;
11233cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
11243cf42da3SAl Viro 		if (!n)
1125d879cb83SAl Viro 			return -ENOMEM;
1126451c0ba9SAl Viro 		res = get_user_pages_fast(addr, n, gup_flags, *pages);
112791329559SAl Viro 		if (unlikely(res <= 0))
1128d879cb83SAl Viro 			return res;
1129eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1130eba2d3d7SAl Viro 		iov_iter_advance(i, maxsize);
1131eba2d3d7SAl Viro 		return maxsize;
11323d671ca6SAl Viro 	}
11333d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
1134451c0ba9SAl Viro 		struct page **p;
11353d671ca6SAl Viro 		struct page *page;
11363d671ca6SAl Viro 
113759dbd7d0SAl Viro 		page = first_bvec_segment(i, &maxsize, start);
11383cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
11393cf42da3SAl Viro 		if (!n)
1140d879cb83SAl Viro 			return -ENOMEM;
11413cf42da3SAl Viro 		p = *pages;
1142dda8e5d1SAl Viro 		for (int k = 0; k < n; k++)
1143eba2d3d7SAl Viro 			get_page(p[k] = page + k);
1144eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1145310d9d5aSAl Viro 		i->count -= maxsize;
1146310d9d5aSAl Viro 		i->iov_offset += maxsize;
1147310d9d5aSAl Viro 		if (i->iov_offset == i->bvec->bv_len) {
1148310d9d5aSAl Viro 			i->iov_offset = 0;
1149310d9d5aSAl Viro 			i->bvec++;
1150310d9d5aSAl Viro 			i->nr_segs--;
1151310d9d5aSAl Viro 		}
1152eba2d3d7SAl Viro 		return maxsize;
11533d671ca6SAl Viro 	}
11543d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
1155451c0ba9SAl Viro 		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1156d879cb83SAl Viro 	return -EFAULT;
1157d879cb83SAl Viro }
115891329559SAl Viro 
iov_iter_get_pages2(struct iov_iter * i,struct page ** pages,size_t maxsize,unsigned maxpages,size_t * start)115984bd06c6SChristoph Hellwig ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
116084bd06c6SChristoph Hellwig 		size_t maxsize, unsigned maxpages, size_t *start)
1161451c0ba9SAl Viro {
1162451c0ba9SAl Viro 	if (!maxpages)
1163451c0ba9SAl Viro 		return 0;
1164451c0ba9SAl Viro 	BUG_ON(!pages);
1165451c0ba9SAl Viro 
116684bd06c6SChristoph Hellwig 	return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, start);
1167451c0ba9SAl Viro }
1168eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2);
1169451c0ba9SAl Viro 
iov_iter_get_pages_alloc2(struct iov_iter * i,struct page *** pages,size_t maxsize,size_t * start)117084bd06c6SChristoph Hellwig ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i,
117184bd06c6SChristoph Hellwig 		struct page ***pages, size_t maxsize, size_t *start)
117291329559SAl Viro {
117391329559SAl Viro 	ssize_t len;
117491329559SAl Viro 
117591329559SAl Viro 	*pages = NULL;
117691329559SAl Viro 
117784bd06c6SChristoph Hellwig 	len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start);
117891329559SAl Viro 	if (len <= 0) {
117991329559SAl Viro 		kvfree(*pages);
118091329559SAl Viro 		*pages = NULL;
118191329559SAl Viro 	}
118291329559SAl Viro 	return len;
118391329559SAl Viro }
1184eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2);
1185d879cb83SAl Viro 
csum_and_copy_from_iter(void * addr,size_t bytes,__wsum * csum,struct iov_iter * i)1186d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1187d879cb83SAl Viro 			       struct iov_iter *i)
1188d879cb83SAl Viro {
1189d879cb83SAl Viro 	__wsum sum, next;
1190d879cb83SAl Viro 	sum = *csum;
1191a41dad90SAl Viro 	if (WARN_ON_ONCE(!i->data_source))
1192241699cdSAl Viro 		return 0;
1193a41dad90SAl Viro 
11947baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off, ({
11957baa5099SAl Viro 		next = csum_and_copy_from_user(base, addr + off, len);
1196d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
11977baa5099SAl Viro 		next ? 0 : len;
1198d879cb83SAl Viro 	}), ({
11997baa5099SAl Viro 		sum = csum_and_memcpy(addr + off, base, len, sum, off);
1200d879cb83SAl Viro 	})
1201d879cb83SAl Viro 	)
1202d879cb83SAl Viro 	*csum = sum;
1203d879cb83SAl Viro 	return bytes;
1204d879cb83SAl Viro }
1205d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter);
1206d879cb83SAl Viro 
csum_and_copy_to_iter(const void * addr,size_t bytes,void * _csstate,struct iov_iter * i)120752cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1208d879cb83SAl Viro 			     struct iov_iter *i)
1209d879cb83SAl Viro {
121052cbd23aSWillem de Bruijn 	struct csum_state *csstate = _csstate;
1211d879cb83SAl Viro 	__wsum sum, next;
121278e1f386SAl Viro 
1213a41dad90SAl Viro 	if (WARN_ON_ONCE(i->data_source))
1214a41dad90SAl Viro 		return 0;
121578e1f386SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
1216c67f1fd2SAl Viro 		// can't use csum_memcpy() for that one - data is not copied
1217c67f1fd2SAl Viro 		csstate->csum = csum_block_add(csstate->csum,
1218c67f1fd2SAl Viro 					       csum_partial(addr, bytes, 0),
1219c67f1fd2SAl Viro 					       csstate->off);
1220c67f1fd2SAl Viro 		csstate->off += bytes;
1221c67f1fd2SAl Viro 		return bytes;
1222241699cdSAl Viro 	}
12236852df12SAl Viro 
12246852df12SAl Viro 	sum = csum_shift(csstate->csum, csstate->off);
12253fc40265SDavid Howells 	iterate_and_advance(i, bytes, base, len, off, ({
12267baa5099SAl Viro 		next = csum_and_copy_to_user(addr + off, base, len);
1227d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
12287baa5099SAl Viro 		next ? 0 : len;
1229d879cb83SAl Viro 	}), ({
12307baa5099SAl Viro 		sum = csum_and_memcpy(base, addr + off, len, sum, off);
1231d879cb83SAl Viro 	})
1232d879cb83SAl Viro 	)
1233594e450bSAl Viro 	csstate->csum = csum_shift(sum, csstate->off);
1234594e450bSAl Viro 	csstate->off += bytes;
1235d879cb83SAl Viro 	return bytes;
1236d879cb83SAl Viro }
1237d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter);
1238d879cb83SAl Viro 
hash_and_copy_to_iter(const void * addr,size_t bytes,void * hashp,struct iov_iter * i)1239d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1240d05f4435SSagi Grimberg 		struct iov_iter *i)
1241d05f4435SSagi Grimberg {
12427999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH
1243d05f4435SSagi Grimberg 	struct ahash_request *hash = hashp;
1244d05f4435SSagi Grimberg 	struct scatterlist sg;
1245d05f4435SSagi Grimberg 	size_t copied;
1246d05f4435SSagi Grimberg 
1247d05f4435SSagi Grimberg 	copied = copy_to_iter(addr, bytes, i);
1248d05f4435SSagi Grimberg 	sg_init_one(&sg, addr, copied);
1249d05f4435SSagi Grimberg 	ahash_request_set_crypt(hash, &sg, NULL, copied);
1250d05f4435SSagi Grimberg 	crypto_ahash_update(hash);
1251d05f4435SSagi Grimberg 	return copied;
125227fad74aSYueHaibing #else
125327fad74aSYueHaibing 	return 0;
125427fad74aSYueHaibing #endif
1255d05f4435SSagi Grimberg }
1256d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter);
1257d05f4435SSagi Grimberg 
iov_npages(const struct iov_iter * i,int maxpages)125866531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages)
1259d879cb83SAl Viro {
126066531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
126166531c65SAl Viro 	const struct iovec *p;
1262d879cb83SAl Viro 	int npages = 0;
1263d879cb83SAl Viro 
1264de4f5fedSJens Axboe 	for (p = iter_iov(i); size; skip = 0, p++) {
126566531c65SAl Viro 		unsigned offs = offset_in_page(p->iov_base + skip);
126666531c65SAl Viro 		size_t len = min(p->iov_len - skip, size);
1267d879cb83SAl Viro 
126866531c65SAl Viro 		if (len) {
126966531c65SAl Viro 			size -= len;
127066531c65SAl Viro 			npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
127166531c65SAl Viro 			if (unlikely(npages > maxpages))
127266531c65SAl Viro 				return maxpages;
127366531c65SAl Viro 		}
127466531c65SAl Viro 	}
127566531c65SAl Viro 	return npages;
127666531c65SAl Viro }
127766531c65SAl Viro 
bvec_npages(const struct iov_iter * i,int maxpages)127866531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages)
127966531c65SAl Viro {
128066531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
128166531c65SAl Viro 	const struct bio_vec *p;
128266531c65SAl Viro 	int npages = 0;
128366531c65SAl Viro 
128466531c65SAl Viro 	for (p = i->bvec; size; skip = 0, p++) {
128566531c65SAl Viro 		unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
128666531c65SAl Viro 		size_t len = min(p->bv_len - skip, size);
128766531c65SAl Viro 
128866531c65SAl Viro 		size -= len;
128966531c65SAl Viro 		npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
129066531c65SAl Viro 		if (unlikely(npages > maxpages))
129166531c65SAl Viro 			return maxpages;
129266531c65SAl Viro 	}
129366531c65SAl Viro 	return npages;
129466531c65SAl Viro }
129566531c65SAl Viro 
iov_iter_npages(const struct iov_iter * i,int maxpages)129666531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages)
129766531c65SAl Viro {
129866531c65SAl Viro 	if (unlikely(!i->count))
129966531c65SAl Viro 		return 0;
1300fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1301fcb14cb1SAl Viro 		unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1302fcb14cb1SAl Viro 		int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1303fcb14cb1SAl Viro 		return min(npages, maxpages);
1304fcb14cb1SAl Viro 	}
130566531c65SAl Viro 	/* iovec and kvec have identical layouts */
130666531c65SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
130766531c65SAl Viro 		return iov_npages(i, maxpages);
130866531c65SAl Viro 	if (iov_iter_is_bvec(i))
130966531c65SAl Viro 		return bvec_npages(i, maxpages);
131066531c65SAl Viro 	if (iov_iter_is_xarray(i)) {
1311e4f8df86SAl Viro 		unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1312e4f8df86SAl Viro 		int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
131366531c65SAl Viro 		return min(npages, maxpages);
131466531c65SAl Viro 	}
131566531c65SAl Viro 	return 0;
1316d879cb83SAl Viro }
1317d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages);
1318d879cb83SAl Viro 
dup_iter(struct iov_iter * new,struct iov_iter * old,gfp_t flags)1319d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1320d879cb83SAl Viro {
1321d879cb83SAl Viro 	*new = *old;
132200e23707SDavid Howells 	if (iov_iter_is_bvec(new))
1323d879cb83SAl Viro 		return new->bvec = kmemdup(new->bvec,
1324d879cb83SAl Viro 				    new->nr_segs * sizeof(struct bio_vec),
1325d879cb83SAl Viro 				    flags);
1326fcb14cb1SAl Viro 	else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1327d879cb83SAl Viro 		/* iovec and kvec have identical layout */
1328de4f5fedSJens Axboe 		return new->__iov = kmemdup(new->__iov,
1329d879cb83SAl Viro 				   new->nr_segs * sizeof(struct iovec),
1330d879cb83SAl Viro 				   flags);
1331fcb14cb1SAl Viro 	return NULL;
1332d879cb83SAl Viro }
1333d879cb83SAl Viro EXPORT_SYMBOL(dup_iter);
1334bc917be8SAl Viro 
copy_compat_iovec_from_user(struct iovec * iov,const struct iovec __user * uvec,unsigned long nr_segs)133550f9a76eSJosh Poimboeuf static __noclone int copy_compat_iovec_from_user(struct iovec *iov,
1336bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1337bfdc5970SChristoph Hellwig {
1338bfdc5970SChristoph Hellwig 	const struct compat_iovec __user *uiov =
1339bfdc5970SChristoph Hellwig 		(const struct compat_iovec __user *)uvec;
1340bfdc5970SChristoph Hellwig 	int ret = -EFAULT, i;
1341bfdc5970SChristoph Hellwig 
1342a959a978SChristoph Hellwig 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1343bfdc5970SChristoph Hellwig 		return -EFAULT;
1344bfdc5970SChristoph Hellwig 
1345bfdc5970SChristoph Hellwig 	for (i = 0; i < nr_segs; i++) {
1346bfdc5970SChristoph Hellwig 		compat_uptr_t buf;
1347bfdc5970SChristoph Hellwig 		compat_ssize_t len;
1348bfdc5970SChristoph Hellwig 
1349bfdc5970SChristoph Hellwig 		unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1350bfdc5970SChristoph Hellwig 		unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1351bfdc5970SChristoph Hellwig 
1352bfdc5970SChristoph Hellwig 		/* check for compat_size_t not fitting in compat_ssize_t .. */
1353bfdc5970SChristoph Hellwig 		if (len < 0) {
1354bfdc5970SChristoph Hellwig 			ret = -EINVAL;
1355bfdc5970SChristoph Hellwig 			goto uaccess_end;
1356bfdc5970SChristoph Hellwig 		}
1357bfdc5970SChristoph Hellwig 		iov[i].iov_base = compat_ptr(buf);
1358bfdc5970SChristoph Hellwig 		iov[i].iov_len = len;
1359bfdc5970SChristoph Hellwig 	}
1360bfdc5970SChristoph Hellwig 
1361bfdc5970SChristoph Hellwig 	ret = 0;
1362bfdc5970SChristoph Hellwig uaccess_end:
1363bfdc5970SChristoph Hellwig 	user_access_end();
1364bfdc5970SChristoph Hellwig 	return ret;
1365bfdc5970SChristoph Hellwig }
1366bfdc5970SChristoph Hellwig 
copy_iovec_from_user(struct iovec * iov,const struct iovec __user * uiov,unsigned long nr_segs)1367719a937bSPeter Zijlstra static __noclone int copy_iovec_from_user(struct iovec *iov,
1368487c20b0SLinus Torvalds 		const struct iovec __user *uiov, unsigned long nr_segs)
1369fb041b59SDavid Laight {
1370487c20b0SLinus Torvalds 	int ret = -EFAULT;
1371bfdc5970SChristoph Hellwig 
1372487c20b0SLinus Torvalds 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1373bfdc5970SChristoph Hellwig 		return -EFAULT;
1374bfdc5970SChristoph Hellwig 
1375487c20b0SLinus Torvalds 	do {
1376487c20b0SLinus Torvalds 		void __user *buf;
1377487c20b0SLinus Torvalds 		ssize_t len;
1378487c20b0SLinus Torvalds 
1379487c20b0SLinus Torvalds 		unsafe_get_user(len, &uiov->iov_len, uaccess_end);
1380487c20b0SLinus Torvalds 		unsafe_get_user(buf, &uiov->iov_base, uaccess_end);
1381487c20b0SLinus Torvalds 
1382487c20b0SLinus Torvalds 		/* check for size_t not fitting in ssize_t .. */
1383487c20b0SLinus Torvalds 		if (unlikely(len < 0)) {
1384487c20b0SLinus Torvalds 			ret = -EINVAL;
1385487c20b0SLinus Torvalds 			goto uaccess_end;
1386487c20b0SLinus Torvalds 		}
1387487c20b0SLinus Torvalds 		iov->iov_base = buf;
1388487c20b0SLinus Torvalds 		iov->iov_len = len;
1389487c20b0SLinus Torvalds 
1390487c20b0SLinus Torvalds 		uiov++; iov++;
1391487c20b0SLinus Torvalds 	} while (--nr_segs);
1392487c20b0SLinus Torvalds 
1393487c20b0SLinus Torvalds 	ret = 0;
1394487c20b0SLinus Torvalds uaccess_end:
1395487c20b0SLinus Torvalds 	user_access_end();
1396487c20b0SLinus Torvalds 	return ret;
1397bfdc5970SChristoph Hellwig }
1398bfdc5970SChristoph Hellwig 
iovec_from_user(const struct iovec __user * uvec,unsigned long nr_segs,unsigned long fast_segs,struct iovec * fast_iov,bool compat)1399bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec,
1400bfdc5970SChristoph Hellwig 		unsigned long nr_segs, unsigned long fast_segs,
1401bfdc5970SChristoph Hellwig 		struct iovec *fast_iov, bool compat)
1402bfdc5970SChristoph Hellwig {
1403bfdc5970SChristoph Hellwig 	struct iovec *iov = fast_iov;
1404bfdc5970SChristoph Hellwig 	int ret;
1405fb041b59SDavid Laight 
1406fb041b59SDavid Laight 	/*
1407bfdc5970SChristoph Hellwig 	 * SuS says "The readv() function *may* fail if the iovcnt argument was
1408bfdc5970SChristoph Hellwig 	 * less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1409fb041b59SDavid Laight 	 * traditionally returned zero for zero segments, so...
1410fb041b59SDavid Laight 	 */
1411bfdc5970SChristoph Hellwig 	if (nr_segs == 0)
1412bfdc5970SChristoph Hellwig 		return iov;
1413bfdc5970SChristoph Hellwig 	if (nr_segs > UIO_MAXIOV)
1414bfdc5970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1415fb041b59SDavid Laight 	if (nr_segs > fast_segs) {
1416fb041b59SDavid Laight 		iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1417bfdc5970SChristoph Hellwig 		if (!iov)
1418bfdc5970SChristoph Hellwig 			return ERR_PTR(-ENOMEM);
1419fb041b59SDavid Laight 	}
1420bfdc5970SChristoph Hellwig 
1421487c20b0SLinus Torvalds 	if (unlikely(compat))
1422bfdc5970SChristoph Hellwig 		ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1423bfdc5970SChristoph Hellwig 	else
1424bfdc5970SChristoph Hellwig 		ret = copy_iovec_from_user(iov, uvec, nr_segs);
1425bfdc5970SChristoph Hellwig 	if (ret) {
1426bfdc5970SChristoph Hellwig 		if (iov != fast_iov)
1427bfdc5970SChristoph Hellwig 			kfree(iov);
1428bfdc5970SChristoph Hellwig 		return ERR_PTR(ret);
1429fb041b59SDavid Laight 	}
1430bfdc5970SChristoph Hellwig 
1431bfdc5970SChristoph Hellwig 	return iov;
1432bfdc5970SChristoph Hellwig }
1433bfdc5970SChristoph Hellwig 
14343b2deb0eSJens Axboe /*
14353b2deb0eSJens Axboe  * Single segment iovec supplied by the user, import it as ITER_UBUF.
14363b2deb0eSJens Axboe  */
__import_iovec_ubuf(int type,const struct iovec __user * uvec,struct iovec ** iovp,struct iov_iter * i,bool compat)14373b2deb0eSJens Axboe static ssize_t __import_iovec_ubuf(int type, const struct iovec __user *uvec,
14383b2deb0eSJens Axboe 				   struct iovec **iovp, struct iov_iter *i,
14393b2deb0eSJens Axboe 				   bool compat)
14403b2deb0eSJens Axboe {
14413b2deb0eSJens Axboe 	struct iovec *iov = *iovp;
14423b2deb0eSJens Axboe 	ssize_t ret;
14433b2deb0eSJens Axboe 
1444*95b93d54SPavel Begunkov 	*iovp = NULL;
1445*95b93d54SPavel Begunkov 
14463b2deb0eSJens Axboe 	if (compat)
14473b2deb0eSJens Axboe 		ret = copy_compat_iovec_from_user(iov, uvec, 1);
14483b2deb0eSJens Axboe 	else
14493b2deb0eSJens Axboe 		ret = copy_iovec_from_user(iov, uvec, 1);
14503b2deb0eSJens Axboe 	if (unlikely(ret))
14513b2deb0eSJens Axboe 		return ret;
14523b2deb0eSJens Axboe 
14533b2deb0eSJens Axboe 	ret = import_ubuf(type, iov->iov_base, iov->iov_len, i);
14543b2deb0eSJens Axboe 	if (unlikely(ret))
14553b2deb0eSJens Axboe 		return ret;
14563b2deb0eSJens Axboe 	return i->count;
14573b2deb0eSJens Axboe }
14583b2deb0eSJens 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)1459bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1460bfdc5970SChristoph Hellwig 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1461bfdc5970SChristoph Hellwig 		 struct iov_iter *i, bool compat)
1462bfdc5970SChristoph Hellwig {
1463bfdc5970SChristoph Hellwig 	ssize_t total_len = 0;
1464bfdc5970SChristoph Hellwig 	unsigned long seg;
1465bfdc5970SChristoph Hellwig 	struct iovec *iov;
1466bfdc5970SChristoph Hellwig 
14673b2deb0eSJens Axboe 	if (nr_segs == 1)
14683b2deb0eSJens Axboe 		return __import_iovec_ubuf(type, uvec, iovp, i, compat);
14693b2deb0eSJens Axboe 
1470bfdc5970SChristoph Hellwig 	iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1471bfdc5970SChristoph Hellwig 	if (IS_ERR(iov)) {
1472bfdc5970SChristoph Hellwig 		*iovp = NULL;
1473bfdc5970SChristoph Hellwig 		return PTR_ERR(iov);
1474fb041b59SDavid Laight 	}
1475fb041b59SDavid Laight 
1476fb041b59SDavid Laight 	/*
1477bfdc5970SChristoph Hellwig 	 * According to the Single Unix Specification we should return EINVAL if
1478bfdc5970SChristoph Hellwig 	 * an element length is < 0 when cast to ssize_t or if the total length
1479bfdc5970SChristoph Hellwig 	 * would overflow the ssize_t return value of the system call.
1480fb041b59SDavid Laight 	 *
1481fb041b59SDavid Laight 	 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1482fb041b59SDavid Laight 	 * overflow case.
1483fb041b59SDavid Laight 	 */
1484fb041b59SDavid Laight 	for (seg = 0; seg < nr_segs; seg++) {
1485fb041b59SDavid Laight 		ssize_t len = (ssize_t)iov[seg].iov_len;
1486fb041b59SDavid Laight 
1487bfdc5970SChristoph Hellwig 		if (!access_ok(iov[seg].iov_base, len)) {
1488bfdc5970SChristoph Hellwig 			if (iov != *iovp)
1489bfdc5970SChristoph Hellwig 				kfree(iov);
1490bfdc5970SChristoph Hellwig 			*iovp = NULL;
1491bfdc5970SChristoph Hellwig 			return -EFAULT;
1492fb041b59SDavid Laight 		}
1493bfdc5970SChristoph Hellwig 
1494bfdc5970SChristoph Hellwig 		if (len > MAX_RW_COUNT - total_len) {
1495bfdc5970SChristoph Hellwig 			len = MAX_RW_COUNT - total_len;
1496fb041b59SDavid Laight 			iov[seg].iov_len = len;
1497fb041b59SDavid Laight 		}
1498bfdc5970SChristoph Hellwig 		total_len += len;
1499fb041b59SDavid Laight 	}
1500bfdc5970SChristoph Hellwig 
1501bfdc5970SChristoph Hellwig 	iov_iter_init(i, type, iov, nr_segs, total_len);
1502bfdc5970SChristoph Hellwig 	if (iov == *iovp)
1503bfdc5970SChristoph Hellwig 		*iovp = NULL;
1504bfdc5970SChristoph Hellwig 	else
1505bfdc5970SChristoph Hellwig 		*iovp = iov;
1506bfdc5970SChristoph Hellwig 	return total_len;
1507fb041b59SDavid Laight }
1508fb041b59SDavid Laight 
1509ffecee4fSVegard Nossum /**
1510ffecee4fSVegard Nossum  * import_iovec() - Copy an array of &struct iovec from userspace
1511ffecee4fSVegard Nossum  *     into the kernel, check that it is valid, and initialize a new
1512ffecee4fSVegard Nossum  *     &struct iov_iter iterator to access it.
1513ffecee4fSVegard Nossum  *
1514ffecee4fSVegard Nossum  * @type: One of %READ or %WRITE.
1515bfdc5970SChristoph Hellwig  * @uvec: Pointer to the userspace array.
1516ffecee4fSVegard Nossum  * @nr_segs: Number of elements in userspace array.
1517ffecee4fSVegard Nossum  * @fast_segs: Number of elements in @iov.
1518bfdc5970SChristoph Hellwig  * @iovp: (input and output parameter) Pointer to pointer to (usually small
1519ffecee4fSVegard Nossum  *     on-stack) kernel array.
1520ffecee4fSVegard Nossum  * @i: Pointer to iterator that will be initialized on success.
1521ffecee4fSVegard Nossum  *
1522ffecee4fSVegard Nossum  * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1523ffecee4fSVegard Nossum  * then this function places %NULL in *@iov on return. Otherwise, a new
1524ffecee4fSVegard Nossum  * array will be allocated and the result placed in *@iov. This means that
1525ffecee4fSVegard Nossum  * the caller may call kfree() on *@iov regardless of whether the small
1526ffecee4fSVegard Nossum  * on-stack array was used or not (and regardless of whether this function
1527ffecee4fSVegard Nossum  * returns an error or not).
1528ffecee4fSVegard Nossum  *
152987e5e6daSJens Axboe  * Return: Negative error code on error, bytes imported on success
1530ffecee4fSVegard Nossum  */
import_iovec(int type,const struct iovec __user * uvec,unsigned nr_segs,unsigned fast_segs,struct iovec ** iovp,struct iov_iter * i)1531bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec,
1532bc917be8SAl Viro 		 unsigned nr_segs, unsigned fast_segs,
1533bfdc5970SChristoph Hellwig 		 struct iovec **iovp, struct iov_iter *i)
1534bc917be8SAl Viro {
153589cd35c5SChristoph Hellwig 	return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
153689cd35c5SChristoph Hellwig 			      in_compat_syscall());
1537bc917be8SAl Viro }
1538bc917be8SAl Viro EXPORT_SYMBOL(import_iovec);
1539bc917be8SAl Viro 
import_single_range(int rw,void __user * buf,size_t len,struct iovec * iov,struct iov_iter * i)1540bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len,
1541bc917be8SAl Viro 		 struct iovec *iov, struct iov_iter *i)
1542bc917be8SAl Viro {
1543bc917be8SAl Viro 	if (len > MAX_RW_COUNT)
1544bc917be8SAl Viro 		len = MAX_RW_COUNT;
154596d4f267SLinus Torvalds 	if (unlikely(!access_ok(buf, len)))
1546bc917be8SAl Viro 		return -EFAULT;
1547bc917be8SAl Viro 
1548e03ad4eeSJens Axboe 	iov_iter_ubuf(i, rw, buf, len);
1549bc917be8SAl Viro 	return 0;
1550bc917be8SAl Viro }
1551e1267585SAl Viro EXPORT_SYMBOL(import_single_range);
15528fb0f47aSJens Axboe 
import_ubuf(int rw,void __user * buf,size_t len,struct iov_iter * i)15532ad9bd83SJens Axboe int import_ubuf(int rw, void __user *buf, size_t len, struct iov_iter *i)
15542ad9bd83SJens Axboe {
15552ad9bd83SJens Axboe 	if (len > MAX_RW_COUNT)
15562ad9bd83SJens Axboe 		len = MAX_RW_COUNT;
15572ad9bd83SJens Axboe 	if (unlikely(!access_ok(buf, len)))
15582ad9bd83SJens Axboe 		return -EFAULT;
15592ad9bd83SJens Axboe 
15602ad9bd83SJens Axboe 	iov_iter_ubuf(i, rw, buf, len);
15612ad9bd83SJens Axboe 	return 0;
15622ad9bd83SJens Axboe }
156370e969ebSTakashi Iwai EXPORT_SYMBOL_GPL(import_ubuf);
15642ad9bd83SJens Axboe 
15658fb0f47aSJens Axboe /**
15668fb0f47aSJens Axboe  * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
15678fb0f47aSJens Axboe  *     iov_iter_save_state() was called.
15688fb0f47aSJens Axboe  *
15698fb0f47aSJens Axboe  * @i: &struct iov_iter to restore
15708fb0f47aSJens Axboe  * @state: state to restore from
15718fb0f47aSJens Axboe  *
15728fb0f47aSJens Axboe  * Used after iov_iter_save_state() to bring restore @i, if operations may
15738fb0f47aSJens Axboe  * have advanced it.
15748fb0f47aSJens Axboe  *
15758fb0f47aSJens Axboe  * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
15768fb0f47aSJens Axboe  */
iov_iter_restore(struct iov_iter * i,struct iov_iter_state * state)15778fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
15788fb0f47aSJens Axboe {
15794397a17cSKeith Busch 	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) &&
15804397a17cSKeith Busch 			 !iter_is_ubuf(i)) && !iov_iter_is_kvec(i))
15818fb0f47aSJens Axboe 		return;
15828fb0f47aSJens Axboe 	i->iov_offset = state->iov_offset;
15838fb0f47aSJens Axboe 	i->count = state->count;
1584fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1585fcb14cb1SAl Viro 		return;
15868fb0f47aSJens Axboe 	/*
15878fb0f47aSJens Axboe 	 * For the *vec iters, nr_segs + iov is constant - if we increment
15888fb0f47aSJens Axboe 	 * the vec, then we also decrement the nr_segs count. Hence we don't
15898fb0f47aSJens Axboe 	 * need to track both of these, just one is enough and we can deduct
15908fb0f47aSJens Axboe 	 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
15918fb0f47aSJens Axboe 	 * size, so we can just increment the iov pointer as they are unionzed.
15928fb0f47aSJens Axboe 	 * ITER_BVEC _may_ be the same size on some archs, but on others it is
15938fb0f47aSJens Axboe 	 * not. Be safe and handle it separately.
15948fb0f47aSJens Axboe 	 */
15958fb0f47aSJens Axboe 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
15968fb0f47aSJens Axboe 	if (iov_iter_is_bvec(i))
15978fb0f47aSJens Axboe 		i->bvec -= state->nr_segs - i->nr_segs;
15988fb0f47aSJens Axboe 	else
1599de4f5fedSJens Axboe 		i->__iov -= state->nr_segs - i->nr_segs;
16008fb0f47aSJens Axboe 	i->nr_segs = state->nr_segs;
16018fb0f47aSJens Axboe }
16027d58fe73SDavid Howells 
16037d58fe73SDavid Howells /*
16047d58fe73SDavid Howells  * Extract a list of contiguous pages from an ITER_XARRAY iterator.  This does not
16057d58fe73SDavid Howells  * get references on the pages, nor does it get a pin on them.
16067d58fe73SDavid 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)16077d58fe73SDavid Howells static ssize_t iov_iter_extract_xarray_pages(struct iov_iter *i,
16087d58fe73SDavid Howells 					     struct page ***pages, size_t maxsize,
16097d58fe73SDavid Howells 					     unsigned int maxpages,
16107d58fe73SDavid Howells 					     iov_iter_extraction_t extraction_flags,
16117d58fe73SDavid Howells 					     size_t *offset0)
16127d58fe73SDavid Howells {
16137d58fe73SDavid Howells 	struct page *page, **p;
16147d58fe73SDavid Howells 	unsigned int nr = 0, offset;
16157d58fe73SDavid Howells 	loff_t pos = i->xarray_start + i->iov_offset;
16167d58fe73SDavid Howells 	pgoff_t index = pos >> PAGE_SHIFT;
16177d58fe73SDavid Howells 	XA_STATE(xas, i->xarray, index);
16187d58fe73SDavid Howells 
16197d58fe73SDavid Howells 	offset = pos & ~PAGE_MASK;
16207d58fe73SDavid Howells 	*offset0 = offset;
16217d58fe73SDavid Howells 
16227d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
16237d58fe73SDavid Howells 	if (!maxpages)
16247d58fe73SDavid Howells 		return -ENOMEM;
16257d58fe73SDavid Howells 	p = *pages;
16267d58fe73SDavid Howells 
16277d58fe73SDavid Howells 	rcu_read_lock();
16287d58fe73SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
16297d58fe73SDavid Howells 		if (xas_retry(&xas, page))
16307d58fe73SDavid Howells 			continue;
16317d58fe73SDavid Howells 
16327d58fe73SDavid Howells 		/* Has the page moved or been split? */
16337d58fe73SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
16347d58fe73SDavid Howells 			xas_reset(&xas);
16357d58fe73SDavid Howells 			continue;
16367d58fe73SDavid Howells 		}
16377d58fe73SDavid Howells 
16387d58fe73SDavid Howells 		p[nr++] = find_subpage(page, xas.xa_index);
16397d58fe73SDavid Howells 		if (nr == maxpages)
16407d58fe73SDavid Howells 			break;
16417d58fe73SDavid Howells 	}
16427d58fe73SDavid Howells 	rcu_read_unlock();
16437d58fe73SDavid Howells 
16447d58fe73SDavid Howells 	maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
16457d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
16467d58fe73SDavid Howells 	return maxsize;
16477d58fe73SDavid Howells }
16487d58fe73SDavid Howells 
16497d58fe73SDavid Howells /*
16507d58fe73SDavid Howells  * Extract a list of contiguous pages from an ITER_BVEC iterator.  This does
16517d58fe73SDavid Howells  * not get references on the pages, nor does it get a pin on them.
16527d58fe73SDavid 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)16537d58fe73SDavid Howells static ssize_t iov_iter_extract_bvec_pages(struct iov_iter *i,
16547d58fe73SDavid Howells 					   struct page ***pages, size_t maxsize,
16557d58fe73SDavid Howells 					   unsigned int maxpages,
16567d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
16577d58fe73SDavid Howells 					   size_t *offset0)
16587d58fe73SDavid Howells {
16597d58fe73SDavid Howells 	struct page **p, *page;
1660f741bd71SDavid Howells 	size_t skip = i->iov_offset, offset, size;
16617d58fe73SDavid Howells 	int k;
16627d58fe73SDavid Howells 
16637d58fe73SDavid Howells 	for (;;) {
16647d58fe73SDavid Howells 		if (i->nr_segs == 0)
16657d58fe73SDavid Howells 			return 0;
1666f741bd71SDavid Howells 		size = min(maxsize, i->bvec->bv_len - skip);
1667f741bd71SDavid Howells 		if (size)
16687d58fe73SDavid Howells 			break;
16697d58fe73SDavid Howells 		i->iov_offset = 0;
16707d58fe73SDavid Howells 		i->nr_segs--;
16717d58fe73SDavid Howells 		i->bvec++;
16727d58fe73SDavid Howells 		skip = 0;
16737d58fe73SDavid Howells 	}
16747d58fe73SDavid Howells 
16757d58fe73SDavid Howells 	skip += i->bvec->bv_offset;
16767d58fe73SDavid Howells 	page = i->bvec->bv_page + skip / PAGE_SIZE;
16777d58fe73SDavid Howells 	offset = skip % PAGE_SIZE;
16787d58fe73SDavid Howells 	*offset0 = offset;
16797d58fe73SDavid Howells 
1680f741bd71SDavid Howells 	maxpages = want_pages_array(pages, size, offset, maxpages);
16817d58fe73SDavid Howells 	if (!maxpages)
16827d58fe73SDavid Howells 		return -ENOMEM;
16837d58fe73SDavid Howells 	p = *pages;
16847d58fe73SDavid Howells 	for (k = 0; k < maxpages; k++)
16857d58fe73SDavid Howells 		p[k] = page + k;
16867d58fe73SDavid Howells 
1687f741bd71SDavid Howells 	size = min_t(size_t, size, maxpages * PAGE_SIZE - offset);
1688f741bd71SDavid Howells 	iov_iter_advance(i, size);
1689f741bd71SDavid Howells 	return size;
16907d58fe73SDavid Howells }
16917d58fe73SDavid Howells 
16927d58fe73SDavid Howells /*
16937d58fe73SDavid Howells  * Extract a list of virtually contiguous pages from an ITER_KVEC iterator.
16947d58fe73SDavid Howells  * This does not get references on the pages, nor does it get a pin on them.
16957d58fe73SDavid 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)16967d58fe73SDavid Howells static ssize_t iov_iter_extract_kvec_pages(struct iov_iter *i,
16977d58fe73SDavid Howells 					   struct page ***pages, size_t maxsize,
16987d58fe73SDavid Howells 					   unsigned int maxpages,
16997d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
17007d58fe73SDavid Howells 					   size_t *offset0)
17017d58fe73SDavid Howells {
17027d58fe73SDavid Howells 	struct page **p, *page;
17037d58fe73SDavid Howells 	const void *kaddr;
1704f741bd71SDavid Howells 	size_t skip = i->iov_offset, offset, len, size;
17057d58fe73SDavid Howells 	int k;
17067d58fe73SDavid Howells 
17077d58fe73SDavid Howells 	for (;;) {
17087d58fe73SDavid Howells 		if (i->nr_segs == 0)
17097d58fe73SDavid Howells 			return 0;
1710f741bd71SDavid Howells 		size = min(maxsize, i->kvec->iov_len - skip);
1711f741bd71SDavid Howells 		if (size)
17127d58fe73SDavid Howells 			break;
17137d58fe73SDavid Howells 		i->iov_offset = 0;
17147d58fe73SDavid Howells 		i->nr_segs--;
17157d58fe73SDavid Howells 		i->kvec++;
17167d58fe73SDavid Howells 		skip = 0;
17177d58fe73SDavid Howells 	}
17187d58fe73SDavid Howells 
17197d58fe73SDavid Howells 	kaddr = i->kvec->iov_base + skip;
17207d58fe73SDavid Howells 	offset = (unsigned long)kaddr & ~PAGE_MASK;
17217d58fe73SDavid Howells 	*offset0 = offset;
17227d58fe73SDavid Howells 
1723f741bd71SDavid Howells 	maxpages = want_pages_array(pages, size, offset, maxpages);
17247d58fe73SDavid Howells 	if (!maxpages)
17257d58fe73SDavid Howells 		return -ENOMEM;
17267d58fe73SDavid Howells 	p = *pages;
17277d58fe73SDavid Howells 
17287d58fe73SDavid Howells 	kaddr -= offset;
1729f741bd71SDavid Howells 	len = offset + size;
17307d58fe73SDavid Howells 	for (k = 0; k < maxpages; k++) {
17317d58fe73SDavid Howells 		size_t seg = min_t(size_t, len, PAGE_SIZE);
17327d58fe73SDavid Howells 
17337d58fe73SDavid Howells 		if (is_vmalloc_or_module_addr(kaddr))
17347d58fe73SDavid Howells 			page = vmalloc_to_page(kaddr);
17357d58fe73SDavid Howells 		else
17367d58fe73SDavid Howells 			page = virt_to_page(kaddr);
17377d58fe73SDavid Howells 
17387d58fe73SDavid Howells 		p[k] = page;
17397d58fe73SDavid Howells 		len -= seg;
17407d58fe73SDavid Howells 		kaddr += PAGE_SIZE;
17417d58fe73SDavid Howells 	}
17427d58fe73SDavid Howells 
1743f741bd71SDavid Howells 	size = min_t(size_t, size, maxpages * PAGE_SIZE - offset);
1744f741bd71SDavid Howells 	iov_iter_advance(i, size);
1745f741bd71SDavid Howells 	return size;
17467d58fe73SDavid Howells }
17477d58fe73SDavid Howells 
17487d58fe73SDavid Howells /*
17497d58fe73SDavid Howells  * Extract a list of contiguous pages from a user iterator and get a pin on
17507d58fe73SDavid Howells  * each of them.  This should only be used if the iterator is user-backed
17517d58fe73SDavid Howells  * (IOBUF/UBUF).
17527d58fe73SDavid Howells  *
17537d58fe73SDavid Howells  * It does not get refs on the pages, but the pages must be unpinned by the
17547d58fe73SDavid Howells  * caller once the transfer is complete.
17557d58fe73SDavid Howells  *
17567d58fe73SDavid Howells  * This is safe to be used where background IO/DMA *is* going to be modifying
17577d58fe73SDavid Howells  * the buffer; using a pin rather than a ref makes forces fork() to give the
17587d58fe73SDavid Howells  * child a copy of the page.
17597d58fe73SDavid 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)17607d58fe73SDavid Howells static ssize_t iov_iter_extract_user_pages(struct iov_iter *i,
17617d58fe73SDavid Howells 					   struct page ***pages,
17627d58fe73SDavid Howells 					   size_t maxsize,
17637d58fe73SDavid Howells 					   unsigned int maxpages,
17647d58fe73SDavid Howells 					   iov_iter_extraction_t extraction_flags,
17657d58fe73SDavid Howells 					   size_t *offset0)
17667d58fe73SDavid Howells {
17677d58fe73SDavid Howells 	unsigned long addr;
17687d58fe73SDavid Howells 	unsigned int gup_flags = 0;
17697d58fe73SDavid Howells 	size_t offset;
17707d58fe73SDavid Howells 	int res;
17717d58fe73SDavid Howells 
17727d58fe73SDavid Howells 	if (i->data_source == ITER_DEST)
17737d58fe73SDavid Howells 		gup_flags |= FOLL_WRITE;
17747d58fe73SDavid Howells 	if (extraction_flags & ITER_ALLOW_P2PDMA)
17757d58fe73SDavid Howells 		gup_flags |= FOLL_PCI_P2PDMA;
17767d58fe73SDavid Howells 	if (i->nofault)
17777d58fe73SDavid Howells 		gup_flags |= FOLL_NOFAULT;
17787d58fe73SDavid Howells 
17797d58fe73SDavid Howells 	addr = first_iovec_segment(i, &maxsize);
17807d58fe73SDavid Howells 	*offset0 = offset = addr % PAGE_SIZE;
17817d58fe73SDavid Howells 	addr &= PAGE_MASK;
17827d58fe73SDavid Howells 	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
17837d58fe73SDavid Howells 	if (!maxpages)
17847d58fe73SDavid Howells 		return -ENOMEM;
17857d58fe73SDavid Howells 	res = pin_user_pages_fast(addr, maxpages, gup_flags, *pages);
17867d58fe73SDavid Howells 	if (unlikely(res <= 0))
17877d58fe73SDavid Howells 		return res;
17887d58fe73SDavid Howells 	maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - offset);
17897d58fe73SDavid Howells 	iov_iter_advance(i, maxsize);
17907d58fe73SDavid Howells 	return maxsize;
17917d58fe73SDavid Howells }
17927d58fe73SDavid Howells 
17937d58fe73SDavid Howells /**
17947d58fe73SDavid Howells  * iov_iter_extract_pages - Extract a list of contiguous pages from an iterator
17957d58fe73SDavid Howells  * @i: The iterator to extract from
17967d58fe73SDavid Howells  * @pages: Where to return the list of pages
17977d58fe73SDavid Howells  * @maxsize: The maximum amount of iterator to extract
17987d58fe73SDavid Howells  * @maxpages: The maximum size of the list of pages
17997d58fe73SDavid Howells  * @extraction_flags: Flags to qualify request
18007d58fe73SDavid Howells  * @offset0: Where to return the starting offset into (*@pages)[0]
18017d58fe73SDavid Howells  *
18027d58fe73SDavid Howells  * Extract a list of contiguous pages from the current point of the iterator,
18037d58fe73SDavid Howells  * advancing the iterator.  The maximum number of pages and the maximum amount
18047d58fe73SDavid Howells  * of page contents can be set.
18057d58fe73SDavid Howells  *
18067d58fe73SDavid Howells  * If *@pages is NULL, a page list will be allocated to the required size and
18077d58fe73SDavid Howells  * *@pages will be set to its base.  If *@pages is not NULL, it will be assumed
18087d58fe73SDavid Howells  * that the caller allocated a page list at least @maxpages in size and this
18097d58fe73SDavid Howells  * will be filled in.
18107d58fe73SDavid Howells  *
18117d58fe73SDavid Howells  * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA
18127d58fe73SDavid Howells  * be allowed on the pages extracted.
18137d58fe73SDavid Howells  *
18147d58fe73SDavid Howells  * The iov_iter_extract_will_pin() function can be used to query how cleanup
18157d58fe73SDavid Howells  * should be performed.
18167d58fe73SDavid Howells  *
18177d58fe73SDavid Howells  * Extra refs or pins on the pages may be obtained as follows:
18187d58fe73SDavid Howells  *
18197d58fe73SDavid Howells  *  (*) If the iterator is user-backed (ITER_IOVEC/ITER_UBUF), pins will be
18207d58fe73SDavid Howells  *      added to the pages, but refs will not be taken.
18217d58fe73SDavid Howells  *      iov_iter_extract_will_pin() will return true.
18227d58fe73SDavid Howells  *
18237d58fe73SDavid Howells  *  (*) If the iterator is ITER_KVEC, ITER_BVEC or ITER_XARRAY, the pages are
18247d58fe73SDavid Howells  *      merely listed; no extra refs or pins are obtained.
18257d58fe73SDavid Howells  *      iov_iter_extract_will_pin() will return 0.
18267d58fe73SDavid Howells  *
18277d58fe73SDavid Howells  * Note also:
18287d58fe73SDavid Howells  *
18297d58fe73SDavid Howells  *  (*) Use with ITER_DISCARD is not supported as that has no content.
18307d58fe73SDavid Howells  *
18317d58fe73SDavid Howells  * On success, the function sets *@pages to the new pagelist, if allocated, and
18327d58fe73SDavid Howells  * sets *offset0 to the offset into the first page.
18337d58fe73SDavid Howells  *
18347d58fe73SDavid Howells  * It may also return -ENOMEM and -EFAULT.
18357d58fe73SDavid 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)18367d58fe73SDavid Howells ssize_t iov_iter_extract_pages(struct iov_iter *i,
18377d58fe73SDavid Howells 			       struct page ***pages,
18387d58fe73SDavid Howells 			       size_t maxsize,
18397d58fe73SDavid Howells 			       unsigned int maxpages,
18407d58fe73SDavid Howells 			       iov_iter_extraction_t extraction_flags,
18417d58fe73SDavid Howells 			       size_t *offset0)
18427d58fe73SDavid Howells {
18437d58fe73SDavid Howells 	maxsize = min_t(size_t, min_t(size_t, maxsize, i->count), MAX_RW_COUNT);
18447d58fe73SDavid Howells 	if (!maxsize)
18457d58fe73SDavid Howells 		return 0;
18467d58fe73SDavid Howells 
18477d58fe73SDavid Howells 	if (likely(user_backed_iter(i)))
18487d58fe73SDavid Howells 		return iov_iter_extract_user_pages(i, pages, maxsize,
18497d58fe73SDavid Howells 						   maxpages, extraction_flags,
18507d58fe73SDavid Howells 						   offset0);
18517d58fe73SDavid Howells 	if (iov_iter_is_kvec(i))
18527d58fe73SDavid Howells 		return iov_iter_extract_kvec_pages(i, pages, maxsize,
18537d58fe73SDavid Howells 						   maxpages, extraction_flags,
18547d58fe73SDavid Howells 						   offset0);
18557d58fe73SDavid Howells 	if (iov_iter_is_bvec(i))
18567d58fe73SDavid Howells 		return iov_iter_extract_bvec_pages(i, pages, maxsize,
18577d58fe73SDavid Howells 						   maxpages, extraction_flags,
18587d58fe73SDavid Howells 						   offset0);
18597d58fe73SDavid Howells 	if (iov_iter_is_xarray(i))
18607d58fe73SDavid Howells 		return iov_iter_extract_xarray_pages(i, pages, maxsize,
18617d58fe73SDavid Howells 						     maxpages, extraction_flags,
18627d58fe73SDavid Howells 						     offset0);
18637d58fe73SDavid Howells 	return -EFAULT;
18647d58fe73SDavid Howells }
18657d58fe73SDavid Howells EXPORT_SYMBOL_GPL(iov_iter_extract_pages);
1866