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