splice: stop abusing iov_iter_advance() to flush a pipe
[linux-2.6-block.git] / lib / iov_iter.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
7999096f 2#include <crypto/hash.h>
4f18cd31 3#include <linux/export.h>
2f8b5444 4#include <linux/bvec.h>
4d0e9df5 5#include <linux/fault-inject-usercopy.h>
4f18cd31
AV
6#include <linux/uio.h>
7#include <linux/pagemap.h>
28961998 8#include <linux/highmem.h>
91f79c43
AV
9#include <linux/slab.h>
10#include <linux/vmalloc.h>
241699cd 11#include <linux/splice.h>
bfdc5970 12#include <linux/compat.h>
a604ec7e 13#include <net/checksum.h>
d05f4435 14#include <linux/scatterlist.h>
d0ef4c36 15#include <linux/instrumented.h>
4f18cd31 16
241699cd
AV
17#define PIPE_PARANOIA /* for now */
18
fcb14cb1
AV
19/* covers ubuf and kbuf alike */
20#define iterate_buf(i, n, base, len, off, __p, STEP) { \
21 size_t __maybe_unused off = 0; \
22 len = n; \
23 base = __p + i->iov_offset; \
24 len -= (STEP); \
25 i->iov_offset += len; \
26 n = len; \
27}
28
5c67aa90 29/* covers iovec and kvec alike */
a6e4ec7b 30#define iterate_iovec(i, n, base, len, off, __p, STEP) { \
7baa5099 31 size_t off = 0; \
a6e4ec7b 32 size_t skip = i->iov_offset; \
7a1bcb5d 33 do { \
7baa5099
AV
34 len = min(n, __p->iov_len - skip); \
35 if (likely(len)) { \
36 base = __p->iov_base + skip; \
37 len -= (STEP); \
38 off += len; \
39 skip += len; \
40 n -= len; \
7a1bcb5d
AV
41 if (skip < __p->iov_len) \
42 break; \
43 } \
44 __p++; \
45 skip = 0; \
46 } while (n); \
a6e4ec7b 47 i->iov_offset = skip; \
7baa5099 48 n = off; \
04a31165
AV
49}
50
a6e4ec7b 51#define iterate_bvec(i, n, base, len, off, p, STEP) { \
7baa5099 52 size_t off = 0; \
a6e4ec7b 53 unsigned skip = i->iov_offset; \
7491a2bf
AV
54 while (n) { \
55 unsigned offset = p->bv_offset + skip; \
1b4fb5ff 56 unsigned left; \
21b56c84
AV
57 void *kaddr = kmap_local_page(p->bv_page + \
58 offset / PAGE_SIZE); \
7baa5099 59 base = kaddr + offset % PAGE_SIZE; \
a6e4ec7b 60 len = min(min(n, (size_t)(p->bv_len - skip)), \
7491a2bf 61 (size_t)(PAGE_SIZE - offset % PAGE_SIZE)); \
1b4fb5ff 62 left = (STEP); \
21b56c84 63 kunmap_local(kaddr); \
7baa5099
AV
64 len -= left; \
65 off += len; \
66 skip += len; \
7491a2bf
AV
67 if (skip == p->bv_len) { \
68 skip = 0; \
69 p++; \
70 } \
7baa5099 71 n -= len; \
1b4fb5ff
AV
72 if (left) \
73 break; \
7491a2bf 74 } \
a6e4ec7b 75 i->iov_offset = skip; \
7baa5099 76 n = off; \
04a31165
AV
77}
78
a6e4ec7b 79#define iterate_xarray(i, n, base, len, __off, STEP) { \
1b4fb5ff 80 __label__ __out; \
622838f3 81 size_t __off = 0; \
821979f5 82 struct folio *folio; \
a6e4ec7b 83 loff_t start = i->xarray_start + i->iov_offset; \
4b179e9a 84 pgoff_t index = start / PAGE_SIZE; \
7ff50620
DH
85 XA_STATE(xas, i->xarray, index); \
86 \
821979f5 87 len = PAGE_SIZE - offset_in_page(start); \
7baa5099 88 rcu_read_lock(); \
821979f5 89 xas_for_each(&xas, folio, ULONG_MAX) { \
7baa5099 90 unsigned left; \
821979f5
MWO
91 size_t offset; \
92 if (xas_retry(&xas, folio)) \
7baa5099 93 continue; \
821979f5 94 if (WARN_ON(xa_is_value(folio))) \
7baa5099 95 break; \
821979f5 96 if (WARN_ON(folio_test_hugetlb(folio))) \
7baa5099 97 break; \
821979f5
MWO
98 offset = offset_in_folio(folio, start + __off); \
99 while (offset < folio_size(folio)) { \
100 base = kmap_local_folio(folio, offset); \
7baa5099
AV
101 len = min(n, len); \
102 left = (STEP); \
821979f5 103 kunmap_local(base); \
7baa5099
AV
104 len -= left; \
105 __off += len; \
106 n -= len; \
107 if (left || n == 0) \
108 goto __out; \
821979f5
MWO
109 offset += len; \
110 len = PAGE_SIZE; \
7baa5099 111 } \
7ff50620 112 } \
1b4fb5ff 113__out: \
7ff50620 114 rcu_read_unlock(); \
821979f5 115 i->iov_offset += __off; \
622838f3 116 n = __off; \
7ff50620
DH
117}
118
7baa5099 119#define __iterate_and_advance(i, n, base, len, off, I, K) { \
dd254f5a
AV
120 if (unlikely(i->count < n)) \
121 n = i->count; \
f5da8354 122 if (likely(n)) { \
fcb14cb1
AV
123 if (likely(iter_is_ubuf(i))) { \
124 void __user *base; \
125 size_t len; \
126 iterate_buf(i, n, base, len, off, \
127 i->ubuf, (I)) \
128 } else if (likely(iter_is_iovec(i))) { \
5c67aa90 129 const struct iovec *iov = i->iov; \
7baa5099
AV
130 void __user *base; \
131 size_t len; \
132 iterate_iovec(i, n, base, len, off, \
a6e4ec7b 133 iov, (I)) \
28f38db7
AV
134 i->nr_segs -= iov - i->iov; \
135 i->iov = iov; \
136 } else if (iov_iter_is_bvec(i)) { \
1bdc76ae 137 const struct bio_vec *bvec = i->bvec; \
7baa5099
AV
138 void *base; \
139 size_t len; \
140 iterate_bvec(i, n, base, len, off, \
a6e4ec7b 141 bvec, (K)) \
7491a2bf
AV
142 i->nr_segs -= bvec - i->bvec; \
143 i->bvec = bvec; \
28f38db7 144 } else if (iov_iter_is_kvec(i)) { \
5c67aa90 145 const struct kvec *kvec = i->kvec; \
7baa5099
AV
146 void *base; \
147 size_t len; \
148 iterate_iovec(i, n, base, len, off, \
a6e4ec7b 149 kvec, (K)) \
dd254f5a
AV
150 i->nr_segs -= kvec - i->kvec; \
151 i->kvec = kvec; \
28f38db7 152 } else if (iov_iter_is_xarray(i)) { \
7baa5099
AV
153 void *base; \
154 size_t len; \
155 iterate_xarray(i, n, base, len, off, \
a6e4ec7b 156 (K)) \
7ce2a91e 157 } \
dd254f5a 158 i->count -= n; \
7ce2a91e 159 } \
7ce2a91e 160}
7baa5099
AV
161#define iterate_and_advance(i, n, base, len, off, I, K) \
162 __iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
7ce2a91e 163
09fc68dc
AV
164static int copyout(void __user *to, const void *from, size_t n)
165{
4d0e9df5
AL
166 if (should_fail_usercopy())
167 return n;
96d4f267 168 if (access_ok(to, n)) {
d0ef4c36 169 instrument_copy_to_user(to, from, n);
09fc68dc
AV
170 n = raw_copy_to_user(to, from, n);
171 }
172 return n;
173}
174
175static int copyin(void *to, const void __user *from, size_t n)
176{
4d0e9df5
AL
177 if (should_fail_usercopy())
178 return n;
96d4f267 179 if (access_ok(from, n)) {
d0ef4c36 180 instrument_copy_from_user(to, from, n);
09fc68dc
AV
181 n = raw_copy_from_user(to, from, n);
182 }
183 return n;
184}
185
241699cd
AV
186#ifdef PIPE_PARANOIA
187static bool sanity(const struct iov_iter *i)
188{
189 struct pipe_inode_info *pipe = i->pipe;
8cefc107
DH
190 unsigned int p_head = pipe->head;
191 unsigned int p_tail = pipe->tail;
192 unsigned int p_mask = pipe->ring_size - 1;
193 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
194 unsigned int i_head = i->head;
195 unsigned int idx;
196
241699cd
AV
197 if (i->iov_offset) {
198 struct pipe_buffer *p;
8cefc107 199 if (unlikely(p_occupancy == 0))
241699cd 200 goto Bad; // pipe must be non-empty
8cefc107 201 if (unlikely(i_head != p_head - 1))
241699cd
AV
202 goto Bad; // must be at the last buffer...
203
8cefc107 204 p = &pipe->bufs[i_head & p_mask];
241699cd
AV
205 if (unlikely(p->offset + p->len != i->iov_offset))
206 goto Bad; // ... at the end of segment
207 } else {
8cefc107 208 if (i_head != p_head)
241699cd
AV
209 goto Bad; // must be right after the last buffer
210 }
211 return true;
212Bad:
8cefc107
DH
213 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
214 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
215 p_head, p_tail, pipe->ring_size);
216 for (idx = 0; idx < pipe->ring_size; idx++)
241699cd
AV
217 printk(KERN_ERR "[%p %p %d %d]\n",
218 pipe->bufs[idx].ops,
219 pipe->bufs[idx].page,
220 pipe->bufs[idx].offset,
221 pipe->bufs[idx].len);
222 WARN_ON(1);
223 return false;
224}
225#else
226#define sanity(i) true
227#endif
228
241699cd
AV
229static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
230 struct iov_iter *i)
231{
232 struct pipe_inode_info *pipe = i->pipe;
233 struct pipe_buffer *buf;
8cefc107
DH
234 unsigned int p_tail = pipe->tail;
235 unsigned int p_mask = pipe->ring_size - 1;
236 unsigned int i_head = i->head;
241699cd 237 size_t off;
241699cd
AV
238
239 if (unlikely(bytes > i->count))
240 bytes = i->count;
241
242 if (unlikely(!bytes))
243 return 0;
244
245 if (!sanity(i))
246 return 0;
247
248 off = i->iov_offset;
8cefc107 249 buf = &pipe->bufs[i_head & p_mask];
241699cd
AV
250 if (off) {
251 if (offset == off && buf->page == page) {
252 /* merge with the last one */
253 buf->len += bytes;
254 i->iov_offset += bytes;
255 goto out;
256 }
8cefc107
DH
257 i_head++;
258 buf = &pipe->bufs[i_head & p_mask];
241699cd 259 }
6718b6f8 260 if (pipe_full(i_head, p_tail, pipe->max_usage))
241699cd 261 return 0;
8cefc107 262
241699cd 263 buf->ops = &page_cache_pipe_buf_ops;
9d2231c5 264 buf->flags = 0;
8cefc107
DH
265 get_page(page);
266 buf->page = page;
241699cd
AV
267 buf->offset = offset;
268 buf->len = bytes;
8cefc107
DH
269
270 pipe->head = i_head + 1;
241699cd 271 i->iov_offset = offset + bytes;
8cefc107 272 i->head = i_head;
241699cd
AV
273out:
274 i->count -= bytes;
275 return bytes;
276}
277
171a0203 278/*
a6294593
AG
279 * fault_in_iov_iter_readable - fault in iov iterator for reading
280 * @i: iterator
281 * @size: maximum length
282 *
171a0203 283 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
a6294593
AG
284 * @size. For each iovec, fault in each page that constitutes the iovec.
285 *
286 * Returns the number of bytes not faulted in (like copy_to_user() and
287 * copy_from_user()).
171a0203 288 *
a6294593 289 * Always returns 0 for non-userspace iterators.
171a0203 290 */
a6294593 291size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
171a0203 292{
fcb14cb1
AV
293 if (iter_is_ubuf(i)) {
294 size_t n = min(size, iov_iter_count(i));
295 n -= fault_in_readable(i->ubuf + i->iov_offset, n);
296 return size - n;
297 } else if (iter_is_iovec(i)) {
a6294593 298 size_t count = min(size, iov_iter_count(i));
8409a0d2
AV
299 const struct iovec *p;
300 size_t skip;
301
a6294593
AG
302 size -= count;
303 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
304 size_t len = min(count, p->iov_len - skip);
305 size_t ret;
8409a0d2
AV
306
307 if (unlikely(!len))
308 continue;
a6294593
AG
309 ret = fault_in_readable(p->iov_base + skip, len);
310 count -= len - ret;
311 if (ret)
312 break;
8409a0d2 313 }
a6294593 314 return count + size;
171a0203
AA
315 }
316 return 0;
317}
a6294593 318EXPORT_SYMBOL(fault_in_iov_iter_readable);
171a0203 319
cdd591fc
AG
320/*
321 * fault_in_iov_iter_writeable - fault in iov iterator for writing
322 * @i: iterator
323 * @size: maximum length
324 *
325 * Faults in the iterator using get_user_pages(), i.e., without triggering
326 * hardware page faults. This is primarily useful when we already know that
327 * some or all of the pages in @i aren't in memory.
328 *
329 * Returns the number of bytes not faulted in, like copy_to_user() and
330 * copy_from_user().
331 *
332 * Always returns 0 for non-user-space iterators.
333 */
334size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
335{
fcb14cb1
AV
336 if (iter_is_ubuf(i)) {
337 size_t n = min(size, iov_iter_count(i));
338 n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n);
339 return size - n;
340 } else if (iter_is_iovec(i)) {
cdd591fc
AG
341 size_t count = min(size, iov_iter_count(i));
342 const struct iovec *p;
343 size_t skip;
344
345 size -= count;
346 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
347 size_t len = min(count, p->iov_len - skip);
348 size_t ret;
349
350 if (unlikely(!len))
351 continue;
352 ret = fault_in_safe_writeable(p->iov_base + skip, len);
353 count -= len - ret;
354 if (ret)
355 break;
356 }
357 return count + size;
358 }
359 return 0;
360}
361EXPORT_SYMBOL(fault_in_iov_iter_writeable);
362
aa563d7b 363void iov_iter_init(struct iov_iter *i, unsigned int direction,
71d8e532
AV
364 const struct iovec *iov, unsigned long nr_segs,
365 size_t count)
366{
aa563d7b 367 WARN_ON(direction & ~(READ | WRITE));
8cd54c1c
AV
368 *i = (struct iov_iter) {
369 .iter_type = ITER_IOVEC,
3337ab08 370 .nofault = false,
fcb14cb1 371 .user_backed = true,
8cd54c1c
AV
372 .data_source = direction,
373 .iov = iov,
374 .nr_segs = nr_segs,
375 .iov_offset = 0,
376 .count = count
377 };
71d8e532
AV
378}
379EXPORT_SYMBOL(iov_iter_init);
7b2c99d1 380
241699cd
AV
381static inline bool allocated(struct pipe_buffer *buf)
382{
383 return buf->ops == &default_pipe_buf_ops;
384}
385
8cefc107
DH
386static inline void data_start(const struct iov_iter *i,
387 unsigned int *iter_headp, size_t *offp)
241699cd 388{
8cefc107
DH
389 unsigned int p_mask = i->pipe->ring_size - 1;
390 unsigned int iter_head = i->head;
241699cd 391 size_t off = i->iov_offset;
8cefc107
DH
392
393 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
394 off == PAGE_SIZE)) {
395 iter_head++;
241699cd
AV
396 off = 0;
397 }
8cefc107 398 *iter_headp = iter_head;
241699cd
AV
399 *offp = off;
400}
401
402static size_t push_pipe(struct iov_iter *i, size_t size,
8cefc107 403 int *iter_headp, size_t *offp)
241699cd
AV
404{
405 struct pipe_inode_info *pipe = i->pipe;
8cefc107
DH
406 unsigned int p_tail = pipe->tail;
407 unsigned int p_mask = pipe->ring_size - 1;
408 unsigned int iter_head;
241699cd 409 size_t off;
241699cd
AV
410 ssize_t left;
411
412 if (unlikely(size > i->count))
413 size = i->count;
414 if (unlikely(!size))
415 return 0;
416
417 left = size;
8cefc107
DH
418 data_start(i, &iter_head, &off);
419 *iter_headp = iter_head;
241699cd
AV
420 *offp = off;
421 if (off) {
422 left -= PAGE_SIZE - off;
423 if (left <= 0) {
8cefc107 424 pipe->bufs[iter_head & p_mask].len += size;
241699cd
AV
425 return size;
426 }
8cefc107
DH
427 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
428 iter_head++;
241699cd 429 }
6718b6f8 430 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
8cefc107 431 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
241699cd
AV
432 struct page *page = alloc_page(GFP_USER);
433 if (!page)
434 break;
8cefc107
DH
435
436 buf->ops = &default_pipe_buf_ops;
9d2231c5 437 buf->flags = 0;
8cefc107
DH
438 buf->page = page;
439 buf->offset = 0;
440 buf->len = min_t(ssize_t, left, PAGE_SIZE);
441 left -= buf->len;
442 iter_head++;
443 pipe->head = iter_head;
444
445 if (left == 0)
241699cd 446 return size;
241699cd
AV
447 }
448 return size - left;
449}
450
451static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
452 struct iov_iter *i)
453{
454 struct pipe_inode_info *pipe = i->pipe;
8cefc107
DH
455 unsigned int p_mask = pipe->ring_size - 1;
456 unsigned int i_head;
241699cd 457 size_t n, off;
241699cd
AV
458
459 if (!sanity(i))
460 return 0;
461
8cefc107 462 bytes = n = push_pipe(i, bytes, &i_head, &off);
241699cd
AV
463 if (unlikely(!n))
464 return 0;
8cefc107 465 do {
241699cd 466 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
8cefc107
DH
467 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
468 i->head = i_head;
241699cd
AV
469 i->iov_offset = off + chunk;
470 n -= chunk;
471 addr += chunk;
8cefc107
DH
472 off = 0;
473 i_head++;
474 } while (n);
241699cd
AV
475 i->count -= bytes;
476 return bytes;
477}
478
f9152895
AV
479static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
480 __wsum sum, size_t off)
481{
cc44c17b 482 __wsum next = csum_partial_copy_nocheck(from, to, len);
f9152895
AV
483 return csum_block_add(sum, next, off);
484}
485
78e1f386 486static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
6852df12 487 struct iov_iter *i, __wsum *sump)
78e1f386
AV
488{
489 struct pipe_inode_info *pipe = i->pipe;
8cefc107 490 unsigned int p_mask = pipe->ring_size - 1;
6852df12
AV
491 __wsum sum = *sump;
492 size_t off = 0;
8cefc107 493 unsigned int i_head;
6852df12 494 size_t r;
78e1f386
AV
495
496 if (!sanity(i))
497 return 0;
498
6852df12
AV
499 bytes = push_pipe(i, bytes, &i_head, &r);
500 while (bytes) {
501 size_t chunk = min_t(size_t, bytes, PAGE_SIZE - r);
2495bdcc 502 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
6852df12 503 sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
2495bdcc 504 kunmap_local(p);
8cefc107 505 i->head = i_head;
78e1f386 506 i->iov_offset = r + chunk;
6852df12 507 bytes -= chunk;
78e1f386 508 off += chunk;
8cefc107
DH
509 r = 0;
510 i_head++;
6852df12
AV
511 }
512 *sump = sum;
513 i->count -= off;
514 return off;
78e1f386
AV
515}
516
aa28de27 517size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
62a8067a 518{
00e23707 519 if (unlikely(iov_iter_is_pipe(i)))
241699cd 520 return copy_pipe_to_iter(addr, bytes, i);
fcb14cb1 521 if (user_backed_iter(i))
09fc68dc 522 might_fault();
7baa5099
AV
523 iterate_and_advance(i, bytes, base, len, off,
524 copyout(base, addr + off, len),
525 memcpy(base, addr + off, len)
3d4d3e48 526 )
62a8067a 527
3d4d3e48 528 return bytes;
c35e0248 529}
aa28de27 530EXPORT_SYMBOL(_copy_to_iter);
c35e0248 531
ec6347bb
DW
532#ifdef CONFIG_ARCH_HAS_COPY_MC
533static int copyout_mc(void __user *to, const void *from, size_t n)
8780356e 534{
96d4f267 535 if (access_ok(to, n)) {
d0ef4c36 536 instrument_copy_to_user(to, from, n);
ec6347bb 537 n = copy_mc_to_user((__force void *) to, from, n);
8780356e
DW
538 }
539 return n;
540}
541
ec6347bb 542static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
ca146f6f
DW
543 struct iov_iter *i)
544{
545 struct pipe_inode_info *pipe = i->pipe;
8cefc107
DH
546 unsigned int p_mask = pipe->ring_size - 1;
547 unsigned int i_head;
c3497fd0 548 unsigned int valid = pipe->head;
ca146f6f 549 size_t n, off, xfer = 0;
ca146f6f
DW
550
551 if (!sanity(i))
552 return 0;
553
2a510a74
AV
554 n = push_pipe(i, bytes, &i_head, &off);
555 while (n) {
ca146f6f 556 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
2a510a74 557 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
ca146f6f 558 unsigned long rem;
2a510a74
AV
559 rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
560 chunk -= rem;
561 kunmap_local(p);
c3497fd0
AV
562 if (chunk) {
563 i->head = i_head;
564 i->iov_offset = off + chunk;
565 xfer += chunk;
566 valid = i_head + 1;
567 }
568 if (rem) {
569 pipe->bufs[i_head & p_mask].len -= rem;
570 pipe_discard_from(pipe, valid);
ca146f6f 571 break;
c3497fd0 572 }
ca146f6f 573 n -= chunk;
8cefc107
DH
574 off = 0;
575 i_head++;
2a510a74 576 }
ca146f6f
DW
577 i->count -= xfer;
578 return xfer;
579}
580
bf3eeb9b 581/**
ec6347bb 582 * _copy_mc_to_iter - copy to iter with source memory error exception handling
bf3eeb9b
DW
583 * @addr: source kernel address
584 * @bytes: total transfer length
44e55997 585 * @i: destination iterator
bf3eeb9b 586 *
ec6347bb
DW
587 * The pmem driver deploys this for the dax operation
588 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
589 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
590 * successfully copied.
bf3eeb9b 591 *
ec6347bb 592 * The main differences between this and typical _copy_to_iter().
bf3eeb9b
DW
593 *
594 * * Typical tail/residue handling after a fault retries the copy
595 * byte-by-byte until the fault happens again. Re-triggering machine
596 * checks is potentially fatal so the implementation uses source
597 * alignment and poison alignment assumptions to avoid re-triggering
598 * hardware exceptions.
599 *
600 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
601 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
602 * a short copy.
44e55997
RD
603 *
604 * Return: number of bytes copied (may be %0)
bf3eeb9b 605 */
ec6347bb 606size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
8780356e 607{
00e23707 608 if (unlikely(iov_iter_is_pipe(i)))
ec6347bb 609 return copy_mc_pipe_to_iter(addr, bytes, i);
fcb14cb1 610 if (user_backed_iter(i))
8780356e 611 might_fault();
7baa5099
AV
612 __iterate_and_advance(i, bytes, base, len, off,
613 copyout_mc(base, addr + off, len),
614 copy_mc_to_kernel(base, addr + off, len)
8780356e
DW
615 )
616
617 return bytes;
618}
ec6347bb
DW
619EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
620#endif /* CONFIG_ARCH_HAS_COPY_MC */
8780356e 621
aa28de27 622size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
c35e0248 623{
00e23707 624 if (unlikely(iov_iter_is_pipe(i))) {
241699cd
AV
625 WARN_ON(1);
626 return 0;
627 }
fcb14cb1 628 if (user_backed_iter(i))
09fc68dc 629 might_fault();
7baa5099
AV
630 iterate_and_advance(i, bytes, base, len, off,
631 copyin(addr + off, base, len),
632 memcpy(addr + off, base, len)
0dbca9a4
AV
633 )
634
635 return bytes;
c35e0248 636}
aa28de27 637EXPORT_SYMBOL(_copy_from_iter);
c35e0248 638
aa28de27 639size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
aa583096 640{
00e23707 641 if (unlikely(iov_iter_is_pipe(i))) {
241699cd
AV
642 WARN_ON(1);
643 return 0;
644 }
7baa5099
AV
645 iterate_and_advance(i, bytes, base, len, off,
646 __copy_from_user_inatomic_nocache(addr + off, base, len),
647 memcpy(addr + off, base, len)
aa583096
AV
648 )
649
650 return bytes;
651}
aa28de27 652EXPORT_SYMBOL(_copy_from_iter_nocache);
aa583096 653
0aed55af 654#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
abd08d7d
DW
655/**
656 * _copy_from_iter_flushcache - write destination through cpu cache
657 * @addr: destination kernel address
658 * @bytes: total transfer length
44e55997 659 * @i: source iterator
abd08d7d
DW
660 *
661 * The pmem driver arranges for filesystem-dax to use this facility via
662 * dax_copy_from_iter() for ensuring that writes to persistent memory
663 * are flushed through the CPU cache. It is differentiated from
664 * _copy_from_iter_nocache() in that guarantees all data is flushed for
665 * all iterator types. The _copy_from_iter_nocache() only attempts to
666 * bypass the cache for the ITER_IOVEC case, and on some archs may use
667 * instructions that strand dirty-data in the cache.
44e55997
RD
668 *
669 * Return: number of bytes copied (may be %0)
abd08d7d 670 */
6a37e940 671size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
0aed55af 672{
00e23707 673 if (unlikely(iov_iter_is_pipe(i))) {
0aed55af
DW
674 WARN_ON(1);
675 return 0;
676 }
7baa5099
AV
677 iterate_and_advance(i, bytes, base, len, off,
678 __copy_from_user_flushcache(addr + off, base, len),
679 memcpy_flushcache(addr + off, base, len)
0aed55af
DW
680 )
681
682 return bytes;
683}
6a37e940 684EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
0aed55af
DW
685#endif
686
72e809ed
AV
687static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
688{
6daef95b
ED
689 struct page *head;
690 size_t v = n + offset;
691
692 /*
693 * The general case needs to access the page order in order
694 * to compute the page size.
695 * However, we mostly deal with order-0 pages and thus can
696 * avoid a possible cache line miss for requests that fit all
697 * page orders.
698 */
699 if (n <= v && v <= PAGE_SIZE)
700 return true;
701
702 head = compound_head(page);
703 v += (page - head) << PAGE_SHIFT;
a90bcb86 704
a50b854e 705 if (likely(n <= v && v <= (page_size(head))))
72e809ed
AV
706 return true;
707 WARN_ON(1);
708 return false;
709}
cbbd26b8 710
08aa6479 711static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
62a8067a
AV
712 struct iov_iter *i)
713{
59bb69c6
AV
714 if (unlikely(iov_iter_is_pipe(i))) {
715 return copy_page_to_iter_pipe(page, offset, bytes, i);
716 } else {
c1d4d6a9
AV
717 void *kaddr = kmap_local_page(page);
718 size_t wanted = _copy_to_iter(kaddr + offset, bytes, i);
719 kunmap_local(kaddr);
d271524a 720 return wanted;
28f38db7 721 }
62a8067a 722}
08aa6479
AV
723
724size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
725 struct iov_iter *i)
726{
727 size_t res = 0;
728 if (unlikely(!page_copy_sane(page, offset, bytes)))
729 return 0;
730 page += offset / PAGE_SIZE; // first subpage
731 offset %= PAGE_SIZE;
732 while (1) {
733 size_t n = __copy_page_to_iter(page, offset,
734 min(bytes, (size_t)PAGE_SIZE - offset), i);
735 res += n;
736 bytes -= n;
737 if (!bytes || !n)
738 break;
739 offset += n;
740 if (offset == PAGE_SIZE) {
741 page++;
742 offset = 0;
743 }
744 }
745 return res;
746}
62a8067a
AV
747EXPORT_SYMBOL(copy_page_to_iter);
748
749size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
750 struct iov_iter *i)
751{
59bb69c6 752 if (page_copy_sane(page, offset, bytes)) {
55ca375c 753 void *kaddr = kmap_local_page(page);
aa28de27 754 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
55ca375c 755 kunmap_local(kaddr);
d271524a 756 return wanted;
28f38db7 757 }
28f38db7 758 return 0;
62a8067a
AV
759}
760EXPORT_SYMBOL(copy_page_from_iter);
761
241699cd
AV
762static size_t pipe_zero(size_t bytes, struct iov_iter *i)
763{
764 struct pipe_inode_info *pipe = i->pipe;
8cefc107
DH
765 unsigned int p_mask = pipe->ring_size - 1;
766 unsigned int i_head;
241699cd 767 size_t n, off;
241699cd
AV
768
769 if (!sanity(i))
770 return 0;
771
8cefc107 772 bytes = n = push_pipe(i, bytes, &i_head, &off);
241699cd
AV
773 if (unlikely(!n))
774 return 0;
775
8cefc107 776 do {
241699cd 777 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
893839fd
AV
778 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
779 memset(p + off, 0, chunk);
780 kunmap_local(p);
8cefc107 781 i->head = i_head;
241699cd
AV
782 i->iov_offset = off + chunk;
783 n -= chunk;
8cefc107
DH
784 off = 0;
785 i_head++;
786 } while (n);
241699cd
AV
787 i->count -= bytes;
788 return bytes;
789}
790
c35e0248
MW
791size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
792{
00e23707 793 if (unlikely(iov_iter_is_pipe(i)))
241699cd 794 return pipe_zero(bytes, i);
7baa5099
AV
795 iterate_and_advance(i, bytes, base, len, count,
796 clear_user(base, len),
797 memset(base, 0, len)
8442fa46
AV
798 )
799
800 return bytes;
c35e0248
MW
801}
802EXPORT_SYMBOL(iov_iter_zero);
803
f0b65f39
AV
804size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
805 struct iov_iter *i)
62a8067a 806{
04a31165 807 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
72e809ed
AV
808 if (unlikely(!page_copy_sane(page, offset, bytes))) {
809 kunmap_atomic(kaddr);
810 return 0;
811 }
9ea9ce04 812 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
241699cd
AV
813 kunmap_atomic(kaddr);
814 WARN_ON(1);
815 return 0;
816 }
7baa5099
AV
817 iterate_and_advance(i, bytes, base, len, off,
818 copyin(p + off, base, len),
819 memcpy(p + off, base, len)
04a31165
AV
820 )
821 kunmap_atomic(kaddr);
822 return bytes;
62a8067a 823}
f0b65f39 824EXPORT_SYMBOL(copy_page_from_iter_atomic);
62a8067a 825
b9dc6f65
AV
826static inline void pipe_truncate(struct iov_iter *i)
827{
828 struct pipe_inode_info *pipe = i->pipe;
8cefc107
DH
829 unsigned int p_tail = pipe->tail;
830 unsigned int p_head = pipe->head;
831 unsigned int p_mask = pipe->ring_size - 1;
832
833 if (!pipe_empty(p_head, p_tail)) {
834 struct pipe_buffer *buf;
835 unsigned int i_head = i->head;
b9dc6f65 836 size_t off = i->iov_offset;
8cefc107 837
b9dc6f65 838 if (off) {
8cefc107
DH
839 buf = &pipe->bufs[i_head & p_mask];
840 buf->len = off - buf->offset;
841 i_head++;
b9dc6f65 842 }
8cefc107
DH
843 while (p_head != i_head) {
844 p_head--;
845 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
b9dc6f65 846 }
8cefc107
DH
847
848 pipe->head = p_head;
b9dc6f65
AV
849 }
850}
851
241699cd
AV
852static void pipe_advance(struct iov_iter *i, size_t size)
853{
854 struct pipe_inode_info *pipe = i->pipe;
241699cd 855 if (size) {
b9dc6f65 856 struct pipe_buffer *buf;
8cefc107
DH
857 unsigned int p_mask = pipe->ring_size - 1;
858 unsigned int i_head = i->head;
b9dc6f65 859 size_t off = i->iov_offset, left = size;
8cefc107 860
241699cd 861 if (off) /* make it relative to the beginning of buffer */
8cefc107 862 left += off - pipe->bufs[i_head & p_mask].offset;
241699cd 863 while (1) {
8cefc107 864 buf = &pipe->bufs[i_head & p_mask];
b9dc6f65 865 if (left <= buf->len)
241699cd 866 break;
b9dc6f65 867 left -= buf->len;
8cefc107 868 i_head++;
241699cd 869 }
8cefc107 870 i->head = i_head;
b9dc6f65 871 i->iov_offset = buf->offset + left;
241699cd 872 }
b9dc6f65
AV
873 i->count -= size;
874 /* ... and discard everything past that point */
875 pipe_truncate(i);
241699cd
AV
876}
877
54c8195b
PB
878static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
879{
18fa9af7 880 const struct bio_vec *bvec, *end;
54c8195b 881
18fa9af7
AV
882 if (!i->count)
883 return;
884 i->count -= size;
885
886 size += i->iov_offset;
54c8195b 887
18fa9af7
AV
888 for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
889 if (likely(size < bvec->bv_len))
890 break;
891 size -= bvec->bv_len;
892 }
893 i->iov_offset = size;
894 i->nr_segs -= bvec - i->bvec;
895 i->bvec = bvec;
54c8195b
PB
896}
897
185ac4d4
AV
898static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
899{
900 const struct iovec *iov, *end;
901
902 if (!i->count)
903 return;
904 i->count -= size;
905
906 size += i->iov_offset; // from beginning of current segment
907 for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
908 if (likely(size < iov->iov_len))
909 break;
910 size -= iov->iov_len;
911 }
912 i->iov_offset = size;
913 i->nr_segs -= iov - i->iov;
914 i->iov = iov;
915}
916
62a8067a
AV
917void iov_iter_advance(struct iov_iter *i, size_t size)
918{
3b3fc051
AV
919 if (unlikely(i->count < size))
920 size = i->count;
fcb14cb1
AV
921 if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
922 i->iov_offset += size;
923 i->count -= size;
924 } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
185ac4d4
AV
925 /* iovec and kvec have identical layouts */
926 iov_iter_iovec_advance(i, size);
927 } else if (iov_iter_is_bvec(i)) {
928 iov_iter_bvec_advance(i, size);
929 } else if (iov_iter_is_pipe(i)) {
241699cd 930 pipe_advance(i, size);
185ac4d4
AV
931 } else if (iov_iter_is_discard(i)) {
932 i->count -= size;
54c8195b 933 }
62a8067a
AV
934}
935EXPORT_SYMBOL(iov_iter_advance);
936
27c0e374
AV
937void iov_iter_revert(struct iov_iter *i, size_t unroll)
938{
939 if (!unroll)
940 return;
5b47d59a
AV
941 if (WARN_ON(unroll > MAX_RW_COUNT))
942 return;
27c0e374 943 i->count += unroll;
00e23707 944 if (unlikely(iov_iter_is_pipe(i))) {
27c0e374 945 struct pipe_inode_info *pipe = i->pipe;
8cefc107
DH
946 unsigned int p_mask = pipe->ring_size - 1;
947 unsigned int i_head = i->head;
27c0e374
AV
948 size_t off = i->iov_offset;
949 while (1) {
8cefc107
DH
950 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
951 size_t n = off - b->offset;
27c0e374 952 if (unroll < n) {
4fa55cef 953 off -= unroll;
27c0e374
AV
954 break;
955 }
956 unroll -= n;
8cefc107 957 if (!unroll && i_head == i->start_head) {
27c0e374
AV
958 off = 0;
959 break;
960 }
8cefc107
DH
961 i_head--;
962 b = &pipe->bufs[i_head & p_mask];
963 off = b->offset + b->len;
27c0e374
AV
964 }
965 i->iov_offset = off;
8cefc107 966 i->head = i_head;
27c0e374
AV
967 pipe_truncate(i);
968 return;
969 }
9ea9ce04
DH
970 if (unlikely(iov_iter_is_discard(i)))
971 return;
27c0e374
AV
972 if (unroll <= i->iov_offset) {
973 i->iov_offset -= unroll;
974 return;
975 }
976 unroll -= i->iov_offset;
fcb14cb1 977 if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
7ff50620
DH
978 BUG(); /* We should never go beyond the start of the specified
979 * range since we might then be straying into pages that
980 * aren't pinned.
981 */
982 } else if (iov_iter_is_bvec(i)) {
27c0e374
AV
983 const struct bio_vec *bvec = i->bvec;
984 while (1) {
985 size_t n = (--bvec)->bv_len;
986 i->nr_segs++;
987 if (unroll <= n) {
988 i->bvec = bvec;
989 i->iov_offset = n - unroll;
990 return;
991 }
992 unroll -= n;
993 }
994 } else { /* same logics for iovec and kvec */
995 const struct iovec *iov = i->iov;
996 while (1) {
997 size_t n = (--iov)->iov_len;
998 i->nr_segs++;
999 if (unroll <= n) {
1000 i->iov = iov;
1001 i->iov_offset = n - unroll;
1002 return;
1003 }
1004 unroll -= n;
1005 }
1006 }
1007}
1008EXPORT_SYMBOL(iov_iter_revert);
1009
62a8067a
AV
1010/*
1011 * Return the count of just the current iov_iter segment.
1012 */
1013size_t iov_iter_single_seg_count(const struct iov_iter *i)
1014{
28f38db7
AV
1015 if (i->nr_segs > 1) {
1016 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1017 return min(i->count, i->iov->iov_len - i->iov_offset);
1018 if (iov_iter_is_bvec(i))
1019 return min(i->count, i->bvec->bv_len - i->iov_offset);
1020 }
1021 return i->count;
62a8067a
AV
1022}
1023EXPORT_SYMBOL(iov_iter_single_seg_count);
1024
aa563d7b 1025void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
05afcb77 1026 const struct kvec *kvec, unsigned long nr_segs,
abb78f87
AV
1027 size_t count)
1028{
aa563d7b 1029 WARN_ON(direction & ~(READ | WRITE));
8cd54c1c
AV
1030 *i = (struct iov_iter){
1031 .iter_type = ITER_KVEC,
1032 .data_source = direction,
1033 .kvec = kvec,
1034 .nr_segs = nr_segs,
1035 .iov_offset = 0,
1036 .count = count
1037 };
abb78f87
AV
1038}
1039EXPORT_SYMBOL(iov_iter_kvec);
1040
aa563d7b 1041void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
05afcb77
AV
1042 const struct bio_vec *bvec, unsigned long nr_segs,
1043 size_t count)
1044{
aa563d7b 1045 WARN_ON(direction & ~(READ | WRITE));
8cd54c1c
AV
1046 *i = (struct iov_iter){
1047 .iter_type = ITER_BVEC,
1048 .data_source = direction,
1049 .bvec = bvec,
1050 .nr_segs = nr_segs,
1051 .iov_offset = 0,
1052 .count = count
1053 };
05afcb77
AV
1054}
1055EXPORT_SYMBOL(iov_iter_bvec);
1056
aa563d7b 1057void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
241699cd
AV
1058 struct pipe_inode_info *pipe,
1059 size_t count)
1060{
aa563d7b 1061 BUG_ON(direction != READ);
8cefc107 1062 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
8cd54c1c
AV
1063 *i = (struct iov_iter){
1064 .iter_type = ITER_PIPE,
1065 .data_source = false,
1066 .pipe = pipe,
1067 .head = pipe->head,
1068 .start_head = pipe->head,
1069 .iov_offset = 0,
1070 .count = count
1071 };
241699cd
AV
1072}
1073EXPORT_SYMBOL(iov_iter_pipe);
1074
7ff50620
DH
1075/**
1076 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1077 * @i: The iterator to initialise.
1078 * @direction: The direction of the transfer.
1079 * @xarray: The xarray to access.
1080 * @start: The start file position.
1081 * @count: The size of the I/O buffer in bytes.
1082 *
1083 * Set up an I/O iterator to either draw data out of the pages attached to an
1084 * inode or to inject data into those pages. The pages *must* be prevented
1085 * from evaporation, either by taking a ref on them or locking them by the
1086 * caller.
1087 */
1088void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1089 struct xarray *xarray, loff_t start, size_t count)
1090{
1091 BUG_ON(direction & ~1);
8cd54c1c
AV
1092 *i = (struct iov_iter) {
1093 .iter_type = ITER_XARRAY,
1094 .data_source = direction,
1095 .xarray = xarray,
1096 .xarray_start = start,
1097 .count = count,
1098 .iov_offset = 0
1099 };
7ff50620
DH
1100}
1101EXPORT_SYMBOL(iov_iter_xarray);
1102
9ea9ce04
DH
1103/**
1104 * iov_iter_discard - Initialise an I/O iterator that discards data
1105 * @i: The iterator to initialise.
1106 * @direction: The direction of the transfer.
1107 * @count: The size of the I/O buffer in bytes.
1108 *
1109 * Set up an I/O iterator that just discards everything that's written to it.
1110 * It's only available as a READ iterator.
1111 */
1112void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1113{
1114 BUG_ON(direction != READ);
8cd54c1c
AV
1115 *i = (struct iov_iter){
1116 .iter_type = ITER_DISCARD,
1117 .data_source = false,
1118 .count = count,
1119 .iov_offset = 0
1120 };
9ea9ce04
DH
1121}
1122EXPORT_SYMBOL(iov_iter_discard);
1123
cfa320f7
KB
1124static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
1125 unsigned len_mask)
1126{
1127 size_t size = i->count;
1128 size_t skip = i->iov_offset;
1129 unsigned k;
1130
1131 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1132 size_t len = i->iov[k].iov_len - skip;
1133
1134 if (len > size)
1135 len = size;
1136 if (len & len_mask)
1137 return false;
1138 if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask)
1139 return false;
1140
1141 size -= len;
1142 if (!size)
1143 break;
1144 }
1145 return true;
1146}
1147
1148static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
1149 unsigned len_mask)
1150{
1151 size_t size = i->count;
1152 unsigned skip = i->iov_offset;
1153 unsigned k;
1154
1155 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1156 size_t len = i->bvec[k].bv_len - skip;
1157
1158 if (len > size)
1159 len = size;
1160 if (len & len_mask)
1161 return false;
1162 if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
1163 return false;
1164
1165 size -= len;
1166 if (!size)
1167 break;
1168 }
1169 return true;
1170}
1171
1172/**
1173 * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
1174 * are aligned to the parameters.
1175 *
1176 * @i: &struct iov_iter to restore
1177 * @addr_mask: bit mask to check against the iov element's addresses
1178 * @len_mask: bit mask to check against the iov element's lengths
1179 *
1180 * Return: false if any addresses or lengths intersect with the provided masks
1181 */
1182bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
1183 unsigned len_mask)
1184{
fcb14cb1
AV
1185 if (likely(iter_is_ubuf(i))) {
1186 if (i->count & len_mask)
1187 return false;
1188 if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
1189 return false;
1190 return true;
1191 }
1192
cfa320f7
KB
1193 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1194 return iov_iter_aligned_iovec(i, addr_mask, len_mask);
1195
1196 if (iov_iter_is_bvec(i))
1197 return iov_iter_aligned_bvec(i, addr_mask, len_mask);
1198
1199 if (iov_iter_is_pipe(i)) {
1200 unsigned int p_mask = i->pipe->ring_size - 1;
1201 size_t size = i->count;
1202
1203 if (size & len_mask)
1204 return false;
1205 if (size && allocated(&i->pipe->bufs[i->head & p_mask])) {
1206 if (i->iov_offset & addr_mask)
1207 return false;
1208 }
1209
1210 return true;
1211 }
1212
1213 if (iov_iter_is_xarray(i)) {
1214 if (i->count & len_mask)
1215 return false;
1216 if ((i->xarray_start + i->iov_offset) & addr_mask)
1217 return false;
1218 }
1219
1220 return true;
1221}
1222EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
1223
9221d2e3 1224static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
62a8067a 1225{
04a31165
AV
1226 unsigned long res = 0;
1227 size_t size = i->count;
9221d2e3
AV
1228 size_t skip = i->iov_offset;
1229 unsigned k;
1230
1231 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1232 size_t len = i->iov[k].iov_len - skip;
1233 if (len) {
1234 res |= (unsigned long)i->iov[k].iov_base + skip;
1235 if (len > size)
1236 len = size;
1237 res |= len;
1238 size -= len;
1239 if (!size)
1240 break;
1241 }
1242 }
1243 return res;
1244}
04a31165 1245
9221d2e3
AV
1246static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
1247{
1248 unsigned res = 0;
1249 size_t size = i->count;
1250 unsigned skip = i->iov_offset;
1251 unsigned k;
1252
1253 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1254 size_t len = i->bvec[k].bv_len - skip;
1255 res |= (unsigned long)i->bvec[k].bv_offset + skip;
1256 if (len > size)
1257 len = size;
1258 res |= len;
1259 size -= len;
1260 if (!size)
1261 break;
1262 }
1263 return res;
1264}
1265
1266unsigned long iov_iter_alignment(const struct iov_iter *i)
1267{
fcb14cb1
AV
1268 if (likely(iter_is_ubuf(i))) {
1269 size_t size = i->count;
1270 if (size)
1271 return ((unsigned long)i->ubuf + i->iov_offset) | size;
1272 return 0;
1273 }
1274
9221d2e3
AV
1275 /* iovec and kvec have identical layouts */
1276 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1277 return iov_iter_alignment_iovec(i);
1278
1279 if (iov_iter_is_bvec(i))
1280 return iov_iter_alignment_bvec(i);
1281
1282 if (iov_iter_is_pipe(i)) {
e0ff126e 1283 unsigned int p_mask = i->pipe->ring_size - 1;
9221d2e3 1284 size_t size = i->count;
e0ff126e 1285
8cefc107 1286 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
241699cd
AV
1287 return size | i->iov_offset;
1288 return size;
1289 }
9221d2e3
AV
1290
1291 if (iov_iter_is_xarray(i))
3d14ec1f 1292 return (i->xarray_start + i->iov_offset) | i->count;
9221d2e3
AV
1293
1294 return 0;
62a8067a
AV
1295}
1296EXPORT_SYMBOL(iov_iter_alignment);
1297
357f435d
AV
1298unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1299{
33844e66 1300 unsigned long res = 0;
610c7a71 1301 unsigned long v = 0;
357f435d 1302 size_t size = i->count;
610c7a71 1303 unsigned k;
357f435d 1304
fcb14cb1
AV
1305 if (iter_is_ubuf(i))
1306 return 0;
1307
610c7a71 1308 if (WARN_ON(!iter_is_iovec(i)))
241699cd 1309 return ~0U;
241699cd 1310
610c7a71
AV
1311 for (k = 0; k < i->nr_segs; k++) {
1312 if (i->iov[k].iov_len) {
1313 unsigned long base = (unsigned long)i->iov[k].iov_base;
1314 if (v) // if not the first one
1315 res |= base | v; // this start | previous end
1316 v = base + i->iov[k].iov_len;
1317 if (size <= i->iov[k].iov_len)
1318 break;
1319 size -= i->iov[k].iov_len;
1320 }
1321 }
33844e66 1322 return res;
357f435d
AV
1323}
1324EXPORT_SYMBOL(iov_iter_gap_alignment);
1325
e76b6312 1326static inline ssize_t __pipe_get_pages(struct iov_iter *i,
241699cd
AV
1327 size_t maxsize,
1328 struct page **pages,
8cefc107 1329 int iter_head,
241699cd
AV
1330 size_t *start)
1331{
1332 struct pipe_inode_info *pipe = i->pipe;
8cefc107
DH
1333 unsigned int p_mask = pipe->ring_size - 1;
1334 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
241699cd
AV
1335 if (!n)
1336 return -EFAULT;
1337
1338 maxsize = n;
1339 n += *start;
1689c73a 1340 while (n > 0) {
8cefc107
DH
1341 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1342 iter_head++;
241699cd
AV
1343 n -= PAGE_SIZE;
1344 }
1345
1346 return maxsize;
1347}
1348
1349static ssize_t pipe_get_pages(struct iov_iter *i,
1350 struct page **pages, size_t maxsize, unsigned maxpages,
1351 size_t *start)
1352{
8cefc107 1353 unsigned int iter_head, npages;
241699cd 1354 size_t capacity;
241699cd
AV
1355
1356 if (!sanity(i))
1357 return -EFAULT;
1358
8cefc107
DH
1359 data_start(i, &iter_head, start);
1360 /* Amount of free space: some of this one + all after this one */
1361 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1362 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
241699cd 1363
8cefc107 1364 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
241699cd
AV
1365}
1366
7ff50620
DH
1367static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1368 pgoff_t index, unsigned int nr_pages)
1369{
1370 XA_STATE(xas, xa, index);
1371 struct page *page;
1372 unsigned int ret = 0;
1373
1374 rcu_read_lock();
1375 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1376 if (xas_retry(&xas, page))
1377 continue;
1378
1379 /* Has the page moved or been split? */
1380 if (unlikely(page != xas_reload(&xas))) {
1381 xas_reset(&xas);
1382 continue;
1383 }
1384
1385 pages[ret] = find_subpage(page, xas.xa_index);
1386 get_page(pages[ret]);
1387 if (++ret == nr_pages)
1388 break;
1389 }
1390 rcu_read_unlock();
1391 return ret;
1392}
1393
1394static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1395 struct page **pages, size_t maxsize,
1396 unsigned maxpages, size_t *_start_offset)
1397{
1398 unsigned nr, offset;
1399 pgoff_t index, count;
6c776766 1400 size_t size = maxsize;
7ff50620
DH
1401 loff_t pos;
1402
1403 if (!size || !maxpages)
1404 return 0;
1405
1406 pos = i->xarray_start + i->iov_offset;
1407 index = pos >> PAGE_SHIFT;
1408 offset = pos & ~PAGE_MASK;
1409 *_start_offset = offset;
1410
1411 count = 1;
1412 if (size > PAGE_SIZE - offset) {
1413 size -= PAGE_SIZE - offset;
1414 count += size >> PAGE_SHIFT;
1415 size &= ~PAGE_MASK;
1416 if (size)
1417 count++;
1418 }
1419
1420 if (count > maxpages)
1421 count = maxpages;
1422
1423 nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
1424 if (nr == 0)
1425 return 0;
1426
1c27f1fc 1427 return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
7ff50620
DH
1428}
1429
fcb14cb1 1430/* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
dd45ab9d 1431static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
3d671ca6
AV
1432{
1433 size_t skip;
1434 long k;
1435
fcb14cb1
AV
1436 if (iter_is_ubuf(i))
1437 return (unsigned long)i->ubuf + i->iov_offset;
1438
3d671ca6 1439 for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
3d671ca6
AV
1440 size_t len = i->iov[k].iov_len - skip;
1441
1442 if (unlikely(!len))
1443 continue;
59dbd7d0
AV
1444 if (*size > len)
1445 *size = len;
dd45ab9d 1446 return (unsigned long)i->iov[k].iov_base + skip;
3d671ca6
AV
1447 }
1448 BUG(); // if it had been empty, we wouldn't get called
1449}
1450
1451/* must be done on non-empty ITER_BVEC one */
1452static struct page *first_bvec_segment(const struct iov_iter *i,
59dbd7d0 1453 size_t *size, size_t *start)
3d671ca6
AV
1454{
1455 struct page *page;
1456 size_t skip = i->iov_offset, len;
1457
1458 len = i->bvec->bv_len - skip;
59dbd7d0
AV
1459 if (*size > len)
1460 *size = len;
3d671ca6
AV
1461 skip += i->bvec->bv_offset;
1462 page = i->bvec->bv_page + skip / PAGE_SIZE;
dda8e5d1 1463 *start = skip % PAGE_SIZE;
3d671ca6
AV
1464 return page;
1465}
1466
62a8067a 1467ssize_t iov_iter_get_pages(struct iov_iter *i,
2c80929c 1468 struct page **pages, size_t maxsize, unsigned maxpages,
62a8067a
AV
1469 size_t *start)
1470{
3d671ca6
AV
1471 int n, res;
1472
e5393fae
AV
1473 if (maxsize > i->count)
1474 maxsize = i->count;
3d671ca6
AV
1475 if (!maxsize)
1476 return 0;
7392ed17
AV
1477 if (maxsize > MAX_RW_COUNT)
1478 maxsize = MAX_RW_COUNT;
e5393fae 1479
fcb14cb1 1480 if (likely(user_backed_iter(i))) {
3337ab08 1481 unsigned int gup_flags = 0;
3d671ca6 1482 unsigned long addr;
e5393fae 1483
3337ab08
AG
1484 if (iov_iter_rw(i) != WRITE)
1485 gup_flags |= FOLL_WRITE;
1486 if (i->nofault)
1487 gup_flags |= FOLL_NOFAULT;
1488
dd45ab9d
AV
1489 addr = first_iovec_segment(i, &maxsize);
1490 *start = addr % PAGE_SIZE;
1491 addr &= PAGE_MASK;
59dbd7d0 1492 n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
dda8e5d1
AV
1493 if (n > maxpages)
1494 n = maxpages;
3337ab08 1495 res = get_user_pages_fast(addr, n, gup_flags, pages);
814a6674 1496 if (unlikely(res <= 0))
e5393fae 1497 return res;
59dbd7d0 1498 return min_t(size_t, maxsize, res * PAGE_SIZE - *start);
3d671ca6
AV
1499 }
1500 if (iov_iter_is_bvec(i)) {
1501 struct page *page;
1502
59dbd7d0
AV
1503 page = first_bvec_segment(i, &maxsize, start);
1504 n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
dda8e5d1
AV
1505 if (n > maxpages)
1506 n = maxpages;
1507 for (int k = 0; k < n; k++)
3d671ca6 1508 get_page(*pages++ = page++);
59dbd7d0 1509 return min_t(size_t, maxsize, n * PAGE_SIZE - *start);
3d671ca6
AV
1510 }
1511 if (iov_iter_is_pipe(i))
1512 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1513 if (iov_iter_is_xarray(i))
1514 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1515 return -EFAULT;
62a8067a
AV
1516}
1517EXPORT_SYMBOL(iov_iter_get_pages);
1518
1b17f1f2
AV
1519static struct page **get_pages_array(size_t n)
1520{
752ade68 1521 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
1b17f1f2
AV
1522}
1523
241699cd
AV
1524static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1525 struct page ***pages, size_t maxsize,
1526 size_t *start)
1527{
1528 struct page **p;
8cefc107 1529 unsigned int iter_head, npages;
d7760d63 1530 ssize_t n;
241699cd
AV
1531
1532 if (!sanity(i))
1533 return -EFAULT;
1534
8cefc107
DH
1535 data_start(i, &iter_head, start);
1536 /* Amount of free space: some of this one + all after this one */
1537 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
241699cd
AV
1538 n = npages * PAGE_SIZE - *start;
1539 if (maxsize > n)
1540 maxsize = n;
1541 else
1542 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1543 p = get_pages_array(npages);
1544 if (!p)
1545 return -ENOMEM;
8cefc107 1546 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
241699cd
AV
1547 if (n > 0)
1548 *pages = p;
1549 else
1550 kvfree(p);
1551 return n;
1552}
1553
7ff50620
DH
1554static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
1555 struct page ***pages, size_t maxsize,
1556 size_t *_start_offset)
1557{
1558 struct page **p;
1559 unsigned nr, offset;
1560 pgoff_t index, count;
6c776766 1561 size_t size = maxsize;
7ff50620
DH
1562 loff_t pos;
1563
1564 if (!size)
1565 return 0;
1566
1567 pos = i->xarray_start + i->iov_offset;
1568 index = pos >> PAGE_SHIFT;
1569 offset = pos & ~PAGE_MASK;
1570 *_start_offset = offset;
1571
1572 count = 1;
1573 if (size > PAGE_SIZE - offset) {
1574 size -= PAGE_SIZE - offset;
1575 count += size >> PAGE_SHIFT;
1576 size &= ~PAGE_MASK;
1577 if (size)
1578 count++;
1579 }
1580
1581 p = get_pages_array(count);
1582 if (!p)
1583 return -ENOMEM;
1584 *pages = p;
1585
1586 nr = iter_xarray_populate_pages(p, i->xarray, index, count);
1587 if (nr == 0)
1588 return 0;
1589
1c27f1fc 1590 return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
7ff50620
DH
1591}
1592
62a8067a
AV
1593ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1594 struct page ***pages, size_t maxsize,
1595 size_t *start)
1596{
1b17f1f2 1597 struct page **p;
3d671ca6 1598 int n, res;
1b17f1f2
AV
1599
1600 if (maxsize > i->count)
1601 maxsize = i->count;
3d671ca6
AV
1602 if (!maxsize)
1603 return 0;
7392ed17
AV
1604 if (maxsize > MAX_RW_COUNT)
1605 maxsize = MAX_RW_COUNT;
1b17f1f2 1606
fcb14cb1 1607 if (likely(user_backed_iter(i))) {
3337ab08 1608 unsigned int gup_flags = 0;
3d671ca6 1609 unsigned long addr;
1b17f1f2 1610
3337ab08
AG
1611 if (iov_iter_rw(i) != WRITE)
1612 gup_flags |= FOLL_WRITE;
1613 if (i->nofault)
1614 gup_flags |= FOLL_NOFAULT;
1615
dd45ab9d
AV
1616 addr = first_iovec_segment(i, &maxsize);
1617 *start = addr % PAGE_SIZE;
1618 addr &= PAGE_MASK;
59dbd7d0 1619 n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1b17f1f2
AV
1620 p = get_pages_array(n);
1621 if (!p)
1622 return -ENOMEM;
3337ab08 1623 res = get_user_pages_fast(addr, n, gup_flags, p);
814a6674 1624 if (unlikely(res <= 0)) {
1b17f1f2 1625 kvfree(p);
814a6674 1626 *pages = NULL;
1b17f1f2
AV
1627 return res;
1628 }
1629 *pages = p;
59dbd7d0 1630 return min_t(size_t, maxsize, res * PAGE_SIZE - *start);
3d671ca6
AV
1631 }
1632 if (iov_iter_is_bvec(i)) {
1633 struct page *page;
1634
59dbd7d0
AV
1635 page = first_bvec_segment(i, &maxsize, start);
1636 n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
3d671ca6 1637 *pages = p = get_pages_array(n);
1b17f1f2
AV
1638 if (!p)
1639 return -ENOMEM;
dda8e5d1 1640 for (int k = 0; k < n; k++)
3d671ca6 1641 get_page(*p++ = page++);
59dbd7d0 1642 return min_t(size_t, maxsize, n * PAGE_SIZE - *start);
3d671ca6
AV
1643 }
1644 if (iov_iter_is_pipe(i))
1645 return pipe_get_pages_alloc(i, pages, maxsize, start);
1646 if (iov_iter_is_xarray(i))
1647 return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
1648 return -EFAULT;
62a8067a
AV
1649}
1650EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1651
a604ec7e
AV
1652size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1653 struct iov_iter *i)
1654{
a604ec7e 1655 __wsum sum, next;
a604ec7e 1656 sum = *csum;
9ea9ce04 1657 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
241699cd
AV
1658 WARN_ON(1);
1659 return 0;
1660 }
7baa5099
AV
1661 iterate_and_advance(i, bytes, base, len, off, ({
1662 next = csum_and_copy_from_user(base, addr + off, len);
2495bdcc 1663 sum = csum_block_add(sum, next, off);
7baa5099 1664 next ? 0 : len;
a604ec7e 1665 }), ({
7baa5099 1666 sum = csum_and_memcpy(addr + off, base, len, sum, off);
a604ec7e
AV
1667 })
1668 )
1669 *csum = sum;
1670 return bytes;
1671}
1672EXPORT_SYMBOL(csum_and_copy_from_iter);
1673
52cbd23a 1674size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
a604ec7e
AV
1675 struct iov_iter *i)
1676{
52cbd23a 1677 struct csum_state *csstate = _csstate;
a604ec7e 1678 __wsum sum, next;
78e1f386 1679
78e1f386 1680 if (unlikely(iov_iter_is_discard(i))) {
241699cd
AV
1681 WARN_ON(1); /* for now */
1682 return 0;
1683 }
6852df12
AV
1684
1685 sum = csum_shift(csstate->csum, csstate->off);
1686 if (unlikely(iov_iter_is_pipe(i)))
1687 bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
1688 else iterate_and_advance(i, bytes, base, len, off, ({
7baa5099 1689 next = csum_and_copy_to_user(addr + off, base, len);
2495bdcc 1690 sum = csum_block_add(sum, next, off);
7baa5099 1691 next ? 0 : len;
a604ec7e 1692 }), ({
7baa5099 1693 sum = csum_and_memcpy(base, addr + off, len, sum, off);
a604ec7e
AV
1694 })
1695 )
594e450b
AV
1696 csstate->csum = csum_shift(sum, csstate->off);
1697 csstate->off += bytes;
a604ec7e
AV
1698 return bytes;
1699}
1700EXPORT_SYMBOL(csum_and_copy_to_iter);
1701
d05f4435
SG
1702size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1703 struct iov_iter *i)
1704{
7999096f 1705#ifdef CONFIG_CRYPTO_HASH
d05f4435
SG
1706 struct ahash_request *hash = hashp;
1707 struct scatterlist sg;
1708 size_t copied;
1709
1710 copied = copy_to_iter(addr, bytes, i);
1711 sg_init_one(&sg, addr, copied);
1712 ahash_request_set_crypt(hash, &sg, NULL, copied);
1713 crypto_ahash_update(hash);
1714 return copied;
27fad74a
Y
1715#else
1716 return 0;
1717#endif
d05f4435
SG
1718}
1719EXPORT_SYMBOL(hash_and_copy_to_iter);
1720
66531c65 1721static int iov_npages(const struct iov_iter *i, int maxpages)
62a8067a 1722{
66531c65
AV
1723 size_t skip = i->iov_offset, size = i->count;
1724 const struct iovec *p;
e0f2dc40
AV
1725 int npages = 0;
1726
66531c65
AV
1727 for (p = i->iov; size; skip = 0, p++) {
1728 unsigned offs = offset_in_page(p->iov_base + skip);
1729 size_t len = min(p->iov_len - skip, size);
e0f2dc40 1730
66531c65
AV
1731 if (len) {
1732 size -= len;
1733 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1734 if (unlikely(npages > maxpages))
1735 return maxpages;
1736 }
1737 }
1738 return npages;
1739}
1740
1741static int bvec_npages(const struct iov_iter *i, int maxpages)
1742{
1743 size_t skip = i->iov_offset, size = i->count;
1744 const struct bio_vec *p;
1745 int npages = 0;
1746
1747 for (p = i->bvec; size; skip = 0, p++) {
1748 unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
1749 size_t len = min(p->bv_len - skip, size);
1750
1751 size -= len;
1752 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1753 if (unlikely(npages > maxpages))
1754 return maxpages;
1755 }
1756 return npages;
1757}
1758
1759int iov_iter_npages(const struct iov_iter *i, int maxpages)
1760{
1761 if (unlikely(!i->count))
1762 return 0;
fcb14cb1
AV
1763 if (likely(iter_is_ubuf(i))) {
1764 unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1765 int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1766 return min(npages, maxpages);
1767 }
66531c65
AV
1768 /* iovec and kvec have identical layouts */
1769 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1770 return iov_npages(i, maxpages);
1771 if (iov_iter_is_bvec(i))
1772 return bvec_npages(i, maxpages);
1773 if (iov_iter_is_pipe(i)) {
8cefc107 1774 unsigned int iter_head;
66531c65 1775 int npages;
241699cd 1776 size_t off;
241699cd
AV
1777
1778 if (!sanity(i))
1779 return 0;
1780
8cefc107 1781 data_start(i, &iter_head, &off);
241699cd 1782 /* some of this one + all after this one */
66531c65
AV
1783 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1784 return min(npages, maxpages);
1785 }
1786 if (iov_iter_is_xarray(i)) {
e4f8df86
AV
1787 unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1788 int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
66531c65
AV
1789 return min(npages, maxpages);
1790 }
1791 return 0;
62a8067a 1792}
f67da30c 1793EXPORT_SYMBOL(iov_iter_npages);
4b8164b9
AV
1794
1795const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1796{
1797 *new = *old;
00e23707 1798 if (unlikely(iov_iter_is_pipe(new))) {
241699cd
AV
1799 WARN_ON(1);
1800 return NULL;
1801 }
00e23707 1802 if (iov_iter_is_bvec(new))
4b8164b9
AV
1803 return new->bvec = kmemdup(new->bvec,
1804 new->nr_segs * sizeof(struct bio_vec),
1805 flags);
fcb14cb1 1806 else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
4b8164b9
AV
1807 /* iovec and kvec have identical layout */
1808 return new->iov = kmemdup(new->iov,
1809 new->nr_segs * sizeof(struct iovec),
1810 flags);
fcb14cb1 1811 return NULL;
4b8164b9
AV
1812}
1813EXPORT_SYMBOL(dup_iter);
bc917be8 1814
bfdc5970
CH
1815static int copy_compat_iovec_from_user(struct iovec *iov,
1816 const struct iovec __user *uvec, unsigned long nr_segs)
1817{
1818 const struct compat_iovec __user *uiov =
1819 (const struct compat_iovec __user *)uvec;
1820 int ret = -EFAULT, i;
1821
a959a978 1822 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
bfdc5970
CH
1823 return -EFAULT;
1824
1825 for (i = 0; i < nr_segs; i++) {
1826 compat_uptr_t buf;
1827 compat_ssize_t len;
1828
1829 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1830 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1831
1832 /* check for compat_size_t not fitting in compat_ssize_t .. */
1833 if (len < 0) {
1834 ret = -EINVAL;
1835 goto uaccess_end;
1836 }
1837 iov[i].iov_base = compat_ptr(buf);
1838 iov[i].iov_len = len;
1839 }
1840
1841 ret = 0;
1842uaccess_end:
1843 user_access_end();
1844 return ret;
1845}
1846
1847static int copy_iovec_from_user(struct iovec *iov,
1848 const struct iovec __user *uvec, unsigned long nr_segs)
fb041b59
DL
1849{
1850 unsigned long seg;
fb041b59 1851
bfdc5970
CH
1852 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1853 return -EFAULT;
1854 for (seg = 0; seg < nr_segs; seg++) {
1855 if ((ssize_t)iov[seg].iov_len < 0)
1856 return -EINVAL;
fb041b59
DL
1857 }
1858
bfdc5970
CH
1859 return 0;
1860}
1861
1862struct iovec *iovec_from_user(const struct iovec __user *uvec,
1863 unsigned long nr_segs, unsigned long fast_segs,
1864 struct iovec *fast_iov, bool compat)
1865{
1866 struct iovec *iov = fast_iov;
1867 int ret;
1868
fb041b59 1869 /*
bfdc5970
CH
1870 * SuS says "The readv() function *may* fail if the iovcnt argument was
1871 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
1872 * traditionally returned zero for zero segments, so...
fb041b59 1873 */
bfdc5970
CH
1874 if (nr_segs == 0)
1875 return iov;
1876 if (nr_segs > UIO_MAXIOV)
1877 return ERR_PTR(-EINVAL);
fb041b59
DL
1878 if (nr_segs > fast_segs) {
1879 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
bfdc5970
CH
1880 if (!iov)
1881 return ERR_PTR(-ENOMEM);
fb041b59 1882 }
bfdc5970
CH
1883
1884 if (compat)
1885 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1886 else
1887 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1888 if (ret) {
1889 if (iov != fast_iov)
1890 kfree(iov);
1891 return ERR_PTR(ret);
1892 }
1893
1894 return iov;
1895}
1896
1897ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1898 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1899 struct iov_iter *i, bool compat)
1900{
1901 ssize_t total_len = 0;
1902 unsigned long seg;
1903 struct iovec *iov;
1904
1905 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1906 if (IS_ERR(iov)) {
1907 *iovp = NULL;
1908 return PTR_ERR(iov);
fb041b59
DL
1909 }
1910
1911 /*
bfdc5970
CH
1912 * According to the Single Unix Specification we should return EINVAL if
1913 * an element length is < 0 when cast to ssize_t or if the total length
1914 * would overflow the ssize_t return value of the system call.
fb041b59
DL
1915 *
1916 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1917 * overflow case.
1918 */
fb041b59 1919 for (seg = 0; seg < nr_segs; seg++) {
fb041b59
DL
1920 ssize_t len = (ssize_t)iov[seg].iov_len;
1921
bfdc5970
CH
1922 if (!access_ok(iov[seg].iov_base, len)) {
1923 if (iov != *iovp)
1924 kfree(iov);
1925 *iovp = NULL;
1926 return -EFAULT;
fb041b59 1927 }
bfdc5970
CH
1928
1929 if (len > MAX_RW_COUNT - total_len) {
1930 len = MAX_RW_COUNT - total_len;
fb041b59
DL
1931 iov[seg].iov_len = len;
1932 }
bfdc5970 1933 total_len += len;
fb041b59 1934 }
bfdc5970
CH
1935
1936 iov_iter_init(i, type, iov, nr_segs, total_len);
1937 if (iov == *iovp)
1938 *iovp = NULL;
1939 else
1940 *iovp = iov;
1941 return total_len;
fb041b59
DL
1942}
1943
ffecee4f
VN
1944/**
1945 * import_iovec() - Copy an array of &struct iovec from userspace
1946 * into the kernel, check that it is valid, and initialize a new
1947 * &struct iov_iter iterator to access it.
1948 *
1949 * @type: One of %READ or %WRITE.
bfdc5970 1950 * @uvec: Pointer to the userspace array.
ffecee4f
VN
1951 * @nr_segs: Number of elements in userspace array.
1952 * @fast_segs: Number of elements in @iov.
bfdc5970 1953 * @iovp: (input and output parameter) Pointer to pointer to (usually small
ffecee4f
VN
1954 * on-stack) kernel array.
1955 * @i: Pointer to iterator that will be initialized on success.
1956 *
1957 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1958 * then this function places %NULL in *@iov on return. Otherwise, a new
1959 * array will be allocated and the result placed in *@iov. This means that
1960 * the caller may call kfree() on *@iov regardless of whether the small
1961 * on-stack array was used or not (and regardless of whether this function
1962 * returns an error or not).
1963 *
87e5e6da 1964 * Return: Negative error code on error, bytes imported on success
ffecee4f 1965 */
bfdc5970 1966ssize_t import_iovec(int type, const struct iovec __user *uvec,
bc917be8 1967 unsigned nr_segs, unsigned fast_segs,
bfdc5970 1968 struct iovec **iovp, struct iov_iter *i)
bc917be8 1969{
89cd35c5
CH
1970 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1971 in_compat_syscall());
bc917be8
AV
1972}
1973EXPORT_SYMBOL(import_iovec);
1974
bc917be8
AV
1975int import_single_range(int rw, void __user *buf, size_t len,
1976 struct iovec *iov, struct iov_iter *i)
1977{
1978 if (len > MAX_RW_COUNT)
1979 len = MAX_RW_COUNT;
96d4f267 1980 if (unlikely(!access_ok(buf, len)))
bc917be8
AV
1981 return -EFAULT;
1982
1983 iov->iov_base = buf;
1984 iov->iov_len = len;
1985 iov_iter_init(i, rw, iov, 1, len);
1986 return 0;
1987}
e1267585 1988EXPORT_SYMBOL(import_single_range);
8fb0f47a
JA
1989
1990/**
1991 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
1992 * iov_iter_save_state() was called.
1993 *
1994 * @i: &struct iov_iter to restore
1995 * @state: state to restore from
1996 *
1997 * Used after iov_iter_save_state() to bring restore @i, if operations may
1998 * have advanced it.
1999 *
2000 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
2001 */
2002void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
2003{
2004 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
fcb14cb1 2005 !iov_iter_is_kvec(i) && !iter_is_ubuf(i))
8fb0f47a
JA
2006 return;
2007 i->iov_offset = state->iov_offset;
2008 i->count = state->count;
fcb14cb1
AV
2009 if (iter_is_ubuf(i))
2010 return;
8fb0f47a
JA
2011 /*
2012 * For the *vec iters, nr_segs + iov is constant - if we increment
2013 * the vec, then we also decrement the nr_segs count. Hence we don't
2014 * need to track both of these, just one is enough and we can deduct
2015 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
2016 * size, so we can just increment the iov pointer as they are unionzed.
2017 * ITER_BVEC _may_ be the same size on some archs, but on others it is
2018 * not. Be safe and handle it separately.
2019 */
2020 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
2021 if (iov_iter_is_bvec(i))
2022 i->bvec -= state->nr_segs - i->nr_segs;
2023 else
2024 i->iov -= state->nr_segs - i->nr_segs;
2025 i->nr_segs = state->nr_segs;
2026}