blob: c701b7a187f2baf3276fdffb3dc29d3210620789 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Herbert Xu79990962020-06-12 16:57:37 +10002#include <crypto/hash.h>
Al Viro4f18cd32014-02-05 19:11:33 -05003#include <linux/export.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -06004#include <linux/bvec.h>
Albert van der Linde4d0e9df2020-10-15 20:13:50 -07005#include <linux/fault-inject-usercopy.h>
Al Viro4f18cd32014-02-05 19:11:33 -05006#include <linux/uio.h>
7#include <linux/pagemap.h>
Ira Weiny28961992021-05-04 18:40:03 -07008#include <linux/highmem.h>
Al Viro91f79c42014-03-21 04:58:33 -04009#include <linux/slab.h>
10#include <linux/vmalloc.h>
Al Viro241699c2016-09-22 16:33:12 -040011#include <linux/splice.h>
Christoph Hellwigbfdc5972020-09-25 06:51:40 +020012#include <linux/compat.h>
Al Viroa604ec72014-11-24 01:08:00 -050013#include <net/checksum.h>
Sagi Grimbergd05f4432018-12-03 17:52:09 -080014#include <linux/scatterlist.h>
Marco Elverd0ef4c32020-01-21 17:05:11 +010015#include <linux/instrumented.h>
Al Viro4f18cd32014-02-05 19:11:33 -050016
Al Viro241699c2016-09-22 16:33:12 -040017#define PIPE_PARANOIA /* for now */
18
Al Viro04a31162014-11-27 13:51:41 -050019#define iterate_iovec(i, n, __v, __p, skip, STEP) { \
20 size_t left; \
21 size_t wanted = n; \
22 __p = i->iov; \
23 __v.iov_len = min(n, __p->iov_len - skip); \
24 if (likely(__v.iov_len)) { \
25 __v.iov_base = __p->iov_base + skip; \
26 left = (STEP); \
27 __v.iov_len -= left; \
28 skip += __v.iov_len; \
29 n -= __v.iov_len; \
30 } else { \
31 left = 0; \
32 } \
33 while (unlikely(!left && n)) { \
34 __p++; \
35 __v.iov_len = min(n, __p->iov_len); \
36 if (unlikely(!__v.iov_len)) \
37 continue; \
38 __v.iov_base = __p->iov_base; \
39 left = (STEP); \
40 __v.iov_len -= left; \
41 skip = __v.iov_len; \
42 n -= __v.iov_len; \
43 } \
44 n = wanted - n; \
45}
46
Al Viroa2804552014-11-27 14:48:42 -050047#define iterate_kvec(i, n, __v, __p, skip, STEP) { \
48 size_t wanted = n; \
49 __p = i->kvec; \
50 __v.iov_len = min(n, __p->iov_len - skip); \
51 if (likely(__v.iov_len)) { \
52 __v.iov_base = __p->iov_base + skip; \
53 (void)(STEP); \
54 skip += __v.iov_len; \
55 n -= __v.iov_len; \
56 } \
57 while (unlikely(n)) { \
58 __p++; \
59 __v.iov_len = min(n, __p->iov_len); \
60 if (unlikely(!__v.iov_len)) \
61 continue; \
62 __v.iov_base = __p->iov_base; \
63 (void)(STEP); \
64 skip = __v.iov_len; \
65 n -= __v.iov_len; \
66 } \
67 n = wanted; \
68}
69
Ming Lei1bdc76a2016-05-30 21:34:32 +080070#define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
71 struct bvec_iter __start; \
72 __start.bi_size = n; \
73 __start.bi_bvec_done = skip; \
74 __start.bi_idx = 0; \
75 for_each_bvec(__v, i->bvec, __bi, __start) { \
Al Viro04a31162014-11-27 13:51:41 -050076 (void)(STEP); \
Al Viro04a31162014-11-27 13:51:41 -050077 } \
Al Viro04a31162014-11-27 13:51:41 -050078}
79
David Howells7ff5062072020-02-10 10:00:21 +000080#define iterate_xarray(i, n, __v, skip, STEP) { \
81 struct page *head = NULL; \
82 size_t wanted = n, seg, offset; \
83 loff_t start = i->xarray_start + skip; \
84 pgoff_t index = start >> PAGE_SHIFT; \
85 int j; \
86 \
87 XA_STATE(xas, i->xarray, index); \
88 \
89 rcu_read_lock(); \
90 xas_for_each(&xas, head, ULONG_MAX) { \
91 if (xas_retry(&xas, head)) \
92 continue; \
93 if (WARN_ON(xa_is_value(head))) \
94 break; \
95 if (WARN_ON(PageHuge(head))) \
96 break; \
97 for (j = (head->index < index) ? index - head->index : 0; \
98 j < thp_nr_pages(head); j++) { \
99 __v.bv_page = head + j; \
100 offset = (i->xarray_start + skip) & ~PAGE_MASK; \
101 seg = PAGE_SIZE - offset; \
102 __v.bv_offset = offset; \
103 __v.bv_len = min(n, seg); \
104 (void)(STEP); \
105 n -= __v.bv_len; \
106 skip += __v.bv_len; \
107 if (n == 0) \
108 break; \
109 } \
110 if (n == 0) \
111 break; \
112 } \
113 rcu_read_unlock(); \
114 n = wanted - n; \
115}
116
117#define iterate_all_kinds(i, n, v, I, B, K, X) { \
Al Viro33844e62016-12-21 21:55:02 -0500118 if (likely(n)) { \
119 size_t skip = i->iov_offset; \
120 if (unlikely(i->type & ITER_BVEC)) { \
121 struct bio_vec v; \
122 struct bvec_iter __bi; \
123 iterate_bvec(i, n, v, __bi, skip, (B)) \
124 } else if (unlikely(i->type & ITER_KVEC)) { \
125 const struct kvec *kvec; \
126 struct kvec v; \
127 iterate_kvec(i, n, v, kvec, skip, (K)) \
David Howells9ea9ce02018-10-20 00:57:56 +0100128 } else if (unlikely(i->type & ITER_DISCARD)) { \
David Howells7ff5062072020-02-10 10:00:21 +0000129 } else if (unlikely(i->type & ITER_XARRAY)) { \
130 struct bio_vec v; \
131 iterate_xarray(i, n, v, skip, (X)); \
Al Viro33844e62016-12-21 21:55:02 -0500132 } else { \
133 const struct iovec *iov; \
134 struct iovec v; \
135 iterate_iovec(i, n, v, iov, skip, (I)) \
136 } \
Al Viro04a31162014-11-27 13:51:41 -0500137 } \
138}
139
David Howells7ff5062072020-02-10 10:00:21 +0000140#define iterate_and_advance(i, n, v, I, B, K, X) { \
Al Virodd254f52016-05-09 11:54:48 -0400141 if (unlikely(i->count < n)) \
142 n = i->count; \
Al Viro19f18452016-05-25 17:36:19 -0400143 if (i->count) { \
Al Virodd254f52016-05-09 11:54:48 -0400144 size_t skip = i->iov_offset; \
145 if (unlikely(i->type & ITER_BVEC)) { \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800146 const struct bio_vec *bvec = i->bvec; \
Al Virodd254f52016-05-09 11:54:48 -0400147 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800148 struct bvec_iter __bi; \
149 iterate_bvec(i, n, v, __bi, skip, (B)) \
150 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
151 i->nr_segs -= i->bvec - bvec; \
152 skip = __bi.bi_bvec_done; \
Al Virodd254f52016-05-09 11:54:48 -0400153 } else if (unlikely(i->type & ITER_KVEC)) { \
154 const struct kvec *kvec; \
155 struct kvec v; \
156 iterate_kvec(i, n, v, kvec, skip, (K)) \
157 if (skip == kvec->iov_len) { \
158 kvec++; \
159 skip = 0; \
160 } \
161 i->nr_segs -= kvec - i->kvec; \
162 i->kvec = kvec; \
David Howells9ea9ce02018-10-20 00:57:56 +0100163 } else if (unlikely(i->type & ITER_DISCARD)) { \
164 skip += n; \
David Howells7ff5062072020-02-10 10:00:21 +0000165 } else if (unlikely(i->type & ITER_XARRAY)) { \
166 struct bio_vec v; \
167 iterate_xarray(i, n, v, skip, (X)) \
Al Virodd254f52016-05-09 11:54:48 -0400168 } else { \
169 const struct iovec *iov; \
170 struct iovec v; \
171 iterate_iovec(i, n, v, iov, skip, (I)) \
172 if (skip == iov->iov_len) { \
173 iov++; \
174 skip = 0; \
175 } \
176 i->nr_segs -= iov - i->iov; \
177 i->iov = iov; \
Al Viro7ce2a912014-11-27 13:59:45 -0500178 } \
Al Virodd254f52016-05-09 11:54:48 -0400179 i->count -= n; \
180 i->iov_offset = skip; \
Al Viro7ce2a912014-11-27 13:59:45 -0500181 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500182}
183
Al Viro09fc68dc2017-06-29 22:25:14 -0400184static int copyout(void __user *to, const void *from, size_t n)
185{
Albert van der Linde4d0e9df2020-10-15 20:13:50 -0700186 if (should_fail_usercopy())
187 return n;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800188 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100189 instrument_copy_to_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400190 n = raw_copy_to_user(to, from, n);
191 }
192 return n;
193}
194
195static int copyin(void *to, const void __user *from, size_t n)
196{
Albert van der Linde4d0e9df2020-10-15 20:13:50 -0700197 if (should_fail_usercopy())
198 return n;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800199 if (access_ok(from, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100200 instrument_copy_from_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400201 n = raw_copy_from_user(to, from, n);
202 }
203 return n;
204}
205
Al Viro62a80672014-04-04 23:12:29 -0400206static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500207 struct iov_iter *i)
208{
209 size_t skip, copy, left, wanted;
210 const struct iovec *iov;
211 char __user *buf;
212 void *kaddr, *from;
213
214 if (unlikely(bytes > i->count))
215 bytes = i->count;
216
217 if (unlikely(!bytes))
218 return 0;
219
Al Viro09fc68dc2017-06-29 22:25:14 -0400220 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500221 wanted = bytes;
222 iov = i->iov;
223 skip = i->iov_offset;
224 buf = iov->iov_base + skip;
225 copy = min(bytes, iov->iov_len - skip);
226
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700227 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500228 kaddr = kmap_atomic(page);
229 from = kaddr + offset;
230
231 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400232 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500233 copy -= left;
234 skip += copy;
235 from += copy;
236 bytes -= copy;
237
238 while (unlikely(!left && bytes)) {
239 iov++;
240 buf = iov->iov_base;
241 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400242 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500243 copy -= left;
244 skip = copy;
245 from += copy;
246 bytes -= copy;
247 }
248 if (likely(!bytes)) {
249 kunmap_atomic(kaddr);
250 goto done;
251 }
252 offset = from - kaddr;
253 buf += copy;
254 kunmap_atomic(kaddr);
255 copy = min(bytes, iov->iov_len - skip);
256 }
257 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700258
Al Viro4f18cd32014-02-05 19:11:33 -0500259 kaddr = kmap(page);
260 from = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400261 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500262 copy -= left;
263 skip += copy;
264 from += copy;
265 bytes -= copy;
266 while (unlikely(!left && bytes)) {
267 iov++;
268 buf = iov->iov_base;
269 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400270 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500271 copy -= left;
272 skip = copy;
273 from += copy;
274 bytes -= copy;
275 }
276 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700277
Al Viro4f18cd32014-02-05 19:11:33 -0500278done:
Al Viro81055e52014-04-04 19:23:46 -0400279 if (skip == iov->iov_len) {
280 iov++;
281 skip = 0;
282 }
Al Viro4f18cd32014-02-05 19:11:33 -0500283 i->count -= wanted - bytes;
284 i->nr_segs -= iov - i->iov;
285 i->iov = iov;
286 i->iov_offset = skip;
287 return wanted - bytes;
288}
Al Viro4f18cd32014-02-05 19:11:33 -0500289
Al Viro62a80672014-04-04 23:12:29 -0400290static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400291 struct iov_iter *i)
292{
293 size_t skip, copy, left, wanted;
294 const struct iovec *iov;
295 char __user *buf;
296 void *kaddr, *to;
297
298 if (unlikely(bytes > i->count))
299 bytes = i->count;
300
301 if (unlikely(!bytes))
302 return 0;
303
Al Viro09fc68dc2017-06-29 22:25:14 -0400304 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400305 wanted = bytes;
306 iov = i->iov;
307 skip = i->iov_offset;
308 buf = iov->iov_base + skip;
309 copy = min(bytes, iov->iov_len - skip);
310
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700311 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400312 kaddr = kmap_atomic(page);
313 to = kaddr + offset;
314
315 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400316 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400317 copy -= left;
318 skip += copy;
319 to += copy;
320 bytes -= copy;
321
322 while (unlikely(!left && bytes)) {
323 iov++;
324 buf = iov->iov_base;
325 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400326 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400327 copy -= left;
328 skip = copy;
329 to += copy;
330 bytes -= copy;
331 }
332 if (likely(!bytes)) {
333 kunmap_atomic(kaddr);
334 goto done;
335 }
336 offset = to - kaddr;
337 buf += copy;
338 kunmap_atomic(kaddr);
339 copy = min(bytes, iov->iov_len - skip);
340 }
341 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700342
Al Virof0d1bec2014-04-03 15:05:18 -0400343 kaddr = kmap(page);
344 to = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400345 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400346 copy -= left;
347 skip += copy;
348 to += copy;
349 bytes -= copy;
350 while (unlikely(!left && bytes)) {
351 iov++;
352 buf = iov->iov_base;
353 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400354 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400355 copy -= left;
356 skip = copy;
357 to += copy;
358 bytes -= copy;
359 }
360 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700361
Al Virof0d1bec2014-04-03 15:05:18 -0400362done:
Al Viro81055e52014-04-04 19:23:46 -0400363 if (skip == iov->iov_len) {
364 iov++;
365 skip = 0;
366 }
Al Virof0d1bec2014-04-03 15:05:18 -0400367 i->count -= wanted - bytes;
368 i->nr_segs -= iov - i->iov;
369 i->iov = iov;
370 i->iov_offset = skip;
371 return wanted - bytes;
372}
Al Virof0d1bec2014-04-03 15:05:18 -0400373
Al Viro241699c2016-09-22 16:33:12 -0400374#ifdef PIPE_PARANOIA
375static bool sanity(const struct iov_iter *i)
376{
377 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000378 unsigned int p_head = pipe->head;
379 unsigned int p_tail = pipe->tail;
380 unsigned int p_mask = pipe->ring_size - 1;
381 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
382 unsigned int i_head = i->head;
383 unsigned int idx;
384
Al Viro241699c2016-09-22 16:33:12 -0400385 if (i->iov_offset) {
386 struct pipe_buffer *p;
David Howells8cefc102019-11-15 13:30:32 +0000387 if (unlikely(p_occupancy == 0))
Al Viro241699c2016-09-22 16:33:12 -0400388 goto Bad; // pipe must be non-empty
David Howells8cefc102019-11-15 13:30:32 +0000389 if (unlikely(i_head != p_head - 1))
Al Viro241699c2016-09-22 16:33:12 -0400390 goto Bad; // must be at the last buffer...
391
David Howells8cefc102019-11-15 13:30:32 +0000392 p = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400393 if (unlikely(p->offset + p->len != i->iov_offset))
394 goto Bad; // ... at the end of segment
395 } else {
David Howells8cefc102019-11-15 13:30:32 +0000396 if (i_head != p_head)
Al Viro241699c2016-09-22 16:33:12 -0400397 goto Bad; // must be right after the last buffer
398 }
399 return true;
400Bad:
David Howells8cefc102019-11-15 13:30:32 +0000401 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
402 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
403 p_head, p_tail, pipe->ring_size);
404 for (idx = 0; idx < pipe->ring_size; idx++)
Al Viro241699c2016-09-22 16:33:12 -0400405 printk(KERN_ERR "[%p %p %d %d]\n",
406 pipe->bufs[idx].ops,
407 pipe->bufs[idx].page,
408 pipe->bufs[idx].offset,
409 pipe->bufs[idx].len);
410 WARN_ON(1);
411 return false;
412}
413#else
414#define sanity(i) true
415#endif
416
Al Viro241699c2016-09-22 16:33:12 -0400417static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
418 struct iov_iter *i)
419{
420 struct pipe_inode_info *pipe = i->pipe;
421 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +0000422 unsigned int p_tail = pipe->tail;
423 unsigned int p_mask = pipe->ring_size - 1;
424 unsigned int i_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400425 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400426
427 if (unlikely(bytes > i->count))
428 bytes = i->count;
429
430 if (unlikely(!bytes))
431 return 0;
432
433 if (!sanity(i))
434 return 0;
435
436 off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000437 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400438 if (off) {
439 if (offset == off && buf->page == page) {
440 /* merge with the last one */
441 buf->len += bytes;
442 i->iov_offset += bytes;
443 goto out;
444 }
David Howells8cefc102019-11-15 13:30:32 +0000445 i_head++;
446 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400447 }
David Howells6718b6f2019-10-16 16:47:32 +0100448 if (pipe_full(i_head, p_tail, pipe->max_usage))
Al Viro241699c2016-09-22 16:33:12 -0400449 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000450
Al Viro241699c2016-09-22 16:33:12 -0400451 buf->ops = &page_cache_pipe_buf_ops;
David Howells8cefc102019-11-15 13:30:32 +0000452 get_page(page);
453 buf->page = page;
Al Viro241699c2016-09-22 16:33:12 -0400454 buf->offset = offset;
455 buf->len = bytes;
David Howells8cefc102019-11-15 13:30:32 +0000456
457 pipe->head = i_head + 1;
Al Viro241699c2016-09-22 16:33:12 -0400458 i->iov_offset = offset + bytes;
David Howells8cefc102019-11-15 13:30:32 +0000459 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400460out:
461 i->count -= bytes;
462 return bytes;
463}
464
Al Viro4f18cd32014-02-05 19:11:33 -0500465/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400466 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
467 * bytes. For each iovec, fault in each page that constitutes the iovec.
468 *
469 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
470 * because it is an invalid address).
471 */
Al Virod4690f12016-09-16 00:11:45 +0100472int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400473{
474 size_t skip = i->iov_offset;
475 const struct iovec *iov;
476 int err;
477 struct iovec v;
478
479 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
480 iterate_iovec(i, bytes, v, iov, skip, ({
Al Viro4bce9f6e2016-09-17 18:02:44 -0400481 err = fault_in_pages_readable(v.iov_base, v.iov_len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400482 if (unlikely(err))
483 return err;
484 0;}))
485 }
486 return 0;
487}
Al Virod4690f12016-09-16 00:11:45 +0100488EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400489
David Howellsaa563d72018-10-20 00:57:56 +0100490void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500491 const struct iovec *iov, unsigned long nr_segs,
492 size_t count)
493{
David Howellsaa563d72018-10-20 00:57:56 +0100494 WARN_ON(direction & ~(READ | WRITE));
495 direction &= READ | WRITE;
496
Al Viro71d8e532014-03-05 19:28:09 -0500497 /* It will get better. Eventually... */
Al Virodb68ce12017-03-20 21:08:07 -0400498 if (uaccess_kernel()) {
David Howellsaa563d72018-10-20 00:57:56 +0100499 i->type = ITER_KVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500500 i->kvec = (struct kvec *)iov;
501 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100502 i->type = ITER_IOVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500503 i->iov = iov;
504 }
Al Viro71d8e532014-03-05 19:28:09 -0500505 i->nr_segs = nr_segs;
506 i->iov_offset = 0;
507 i->count = count;
508}
509EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400510
Al Viro241699c2016-09-22 16:33:12 -0400511static inline bool allocated(struct pipe_buffer *buf)
512{
513 return buf->ops == &default_pipe_buf_ops;
514}
515
David Howells8cefc102019-11-15 13:30:32 +0000516static inline void data_start(const struct iov_iter *i,
517 unsigned int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400518{
David Howells8cefc102019-11-15 13:30:32 +0000519 unsigned int p_mask = i->pipe->ring_size - 1;
520 unsigned int iter_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400521 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000522
523 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
524 off == PAGE_SIZE)) {
525 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400526 off = 0;
527 }
David Howells8cefc102019-11-15 13:30:32 +0000528 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400529 *offp = off;
530}
531
532static size_t push_pipe(struct iov_iter *i, size_t size,
David Howells8cefc102019-11-15 13:30:32 +0000533 int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400534{
535 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000536 unsigned int p_tail = pipe->tail;
537 unsigned int p_mask = pipe->ring_size - 1;
538 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400539 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400540 ssize_t left;
541
542 if (unlikely(size > i->count))
543 size = i->count;
544 if (unlikely(!size))
545 return 0;
546
547 left = size;
David Howells8cefc102019-11-15 13:30:32 +0000548 data_start(i, &iter_head, &off);
549 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400550 *offp = off;
551 if (off) {
552 left -= PAGE_SIZE - off;
553 if (left <= 0) {
David Howells8cefc102019-11-15 13:30:32 +0000554 pipe->bufs[iter_head & p_mask].len += size;
Al Viro241699c2016-09-22 16:33:12 -0400555 return size;
556 }
David Howells8cefc102019-11-15 13:30:32 +0000557 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
558 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400559 }
David Howells6718b6f2019-10-16 16:47:32 +0100560 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000561 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400562 struct page *page = alloc_page(GFP_USER);
563 if (!page)
564 break;
David Howells8cefc102019-11-15 13:30:32 +0000565
566 buf->ops = &default_pipe_buf_ops;
567 buf->page = page;
568 buf->offset = 0;
569 buf->len = min_t(ssize_t, left, PAGE_SIZE);
570 left -= buf->len;
571 iter_head++;
572 pipe->head = iter_head;
573
574 if (left == 0)
Al Viro241699c2016-09-22 16:33:12 -0400575 return size;
Al Viro241699c2016-09-22 16:33:12 -0400576 }
577 return size - left;
578}
579
580static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
581 struct iov_iter *i)
582{
583 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000584 unsigned int p_mask = pipe->ring_size - 1;
585 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400586 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400587
588 if (!sanity(i))
589 return 0;
590
David Howells8cefc102019-11-15 13:30:32 +0000591 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400592 if (unlikely(!n))
593 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000594 do {
Al Viro241699c2016-09-22 16:33:12 -0400595 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000596 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
597 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400598 i->iov_offset = off + chunk;
599 n -= chunk;
600 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000601 off = 0;
602 i_head++;
603 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400604 i->count -= bytes;
605 return bytes;
606}
607
Al Virof9152892018-11-27 22:32:59 -0500608static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
609 __wsum sum, size_t off)
610{
Al Virocc44c172020-07-11 00:12:07 -0400611 __wsum next = csum_partial_copy_nocheck(from, to, len);
Al Virof9152892018-11-27 22:32:59 -0500612 return csum_block_add(sum, next, off);
613}
614
Al Viro78e1f382018-11-25 16:24:16 -0500615static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
Willem de Bruijn52cbd232021-02-03 14:29:52 -0500616 struct csum_state *csstate,
617 struct iov_iter *i)
Al Viro78e1f382018-11-25 16:24:16 -0500618{
619 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000620 unsigned int p_mask = pipe->ring_size - 1;
Willem de Bruijn52cbd232021-02-03 14:29:52 -0500621 __wsum sum = csstate->csum;
622 size_t off = csstate->off;
David Howells8cefc102019-11-15 13:30:32 +0000623 unsigned int i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500624 size_t n, r;
Al Viro78e1f382018-11-25 16:24:16 -0500625
626 if (!sanity(i))
627 return 0;
628
David Howells8cefc102019-11-15 13:30:32 +0000629 bytes = n = push_pipe(i, bytes, &i_head, &r);
Al Viro78e1f382018-11-25 16:24:16 -0500630 if (unlikely(!n))
631 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000632 do {
Al Viro78e1f382018-11-25 16:24:16 -0500633 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
David Howells8cefc102019-11-15 13:30:32 +0000634 char *p = kmap_atomic(pipe->bufs[i_head & p_mask].page);
Al Virof9152892018-11-27 22:32:59 -0500635 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
Al Viro78e1f382018-11-25 16:24:16 -0500636 kunmap_atomic(p);
David Howells8cefc102019-11-15 13:30:32 +0000637 i->head = i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500638 i->iov_offset = r + chunk;
639 n -= chunk;
640 off += chunk;
641 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000642 r = 0;
643 i_head++;
644 } while (n);
Al Viro78e1f382018-11-25 16:24:16 -0500645 i->count -= bytes;
Willem de Bruijn52cbd232021-02-03 14:29:52 -0500646 csstate->csum = sum;
647 csstate->off = off;
Al Viro78e1f382018-11-25 16:24:16 -0500648 return bytes;
649}
650
Al Viroaa28de22017-06-29 21:45:10 -0400651size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400652{
Al Viro36f7a8a2015-12-06 16:49:22 -0500653 const char *from = addr;
David Howells00e23702018-10-22 13:07:28 +0100654 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400655 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400656 if (iter_is_iovec(i))
657 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500658 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400659 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500660 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500661 (from += v.bv_len) - v.bv_len, v.bv_len),
David Howells7ff5062072020-02-10 10:00:21 +0000662 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
663 memcpy_to_page(v.bv_page, v.bv_offset,
664 (from += v.bv_len) - v.bv_len, v.bv_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500665 )
Al Viro62a80672014-04-04 23:12:29 -0400666
Al Viro3d4d3e42014-11-27 14:28:06 -0500667 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400668}
Al Viroaa28de22017-06-29 21:45:10 -0400669EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400670
Dan Williamsec6347b2020-10-05 20:40:16 -0700671#ifdef CONFIG_ARCH_HAS_COPY_MC
672static int copyout_mc(void __user *to, const void *from, size_t n)
Dan Williams87803562018-05-03 17:06:31 -0700673{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800674 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100675 instrument_copy_to_user(to, from, n);
Dan Williamsec6347b2020-10-05 20:40:16 -0700676 n = copy_mc_to_user((__force void *) to, from, n);
Dan Williams87803562018-05-03 17:06:31 -0700677 }
678 return n;
679}
680
Dan Williamsec6347b2020-10-05 20:40:16 -0700681static unsigned long copy_mc_to_page(struct page *page, size_t offset,
Dan Williams87803562018-05-03 17:06:31 -0700682 const char *from, size_t len)
683{
684 unsigned long ret;
685 char *to;
686
687 to = kmap_atomic(page);
Dan Williamsec6347b2020-10-05 20:40:16 -0700688 ret = copy_mc_to_kernel(to + offset, from, len);
Dan Williams87803562018-05-03 17:06:31 -0700689 kunmap_atomic(to);
690
691 return ret;
692}
693
Dan Williamsec6347b2020-10-05 20:40:16 -0700694static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
Dan Williamsca146f62018-07-08 13:46:12 -0700695 struct iov_iter *i)
696{
697 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000698 unsigned int p_mask = pipe->ring_size - 1;
699 unsigned int i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700700 size_t n, off, xfer = 0;
Dan Williamsca146f62018-07-08 13:46:12 -0700701
702 if (!sanity(i))
703 return 0;
704
David Howells8cefc102019-11-15 13:30:32 +0000705 bytes = n = push_pipe(i, bytes, &i_head, &off);
Dan Williamsca146f62018-07-08 13:46:12 -0700706 if (unlikely(!n))
707 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000708 do {
Dan Williamsca146f62018-07-08 13:46:12 -0700709 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
710 unsigned long rem;
711
Dan Williamsec6347b2020-10-05 20:40:16 -0700712 rem = copy_mc_to_page(pipe->bufs[i_head & p_mask].page,
David Howells8cefc102019-11-15 13:30:32 +0000713 off, addr, chunk);
714 i->head = i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700715 i->iov_offset = off + chunk - rem;
716 xfer += chunk - rem;
717 if (rem)
718 break;
719 n -= chunk;
720 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000721 off = 0;
722 i_head++;
723 } while (n);
Dan Williamsca146f62018-07-08 13:46:12 -0700724 i->count -= xfer;
725 return xfer;
726}
727
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700728/**
Dan Williamsec6347b2020-10-05 20:40:16 -0700729 * _copy_mc_to_iter - copy to iter with source memory error exception handling
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700730 * @addr: source kernel address
731 * @bytes: total transfer length
732 * @iter: destination iterator
733 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700734 * The pmem driver deploys this for the dax operation
735 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
736 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
737 * successfully copied.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700738 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700739 * The main differences between this and typical _copy_to_iter().
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700740 *
741 * * Typical tail/residue handling after a fault retries the copy
742 * byte-by-byte until the fault happens again. Re-triggering machine
743 * checks is potentially fatal so the implementation uses source
744 * alignment and poison alignment assumptions to avoid re-triggering
745 * hardware exceptions.
746 *
747 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
748 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
749 * a short copy.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700750 */
Dan Williamsec6347b2020-10-05 20:40:16 -0700751size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Dan Williams87803562018-05-03 17:06:31 -0700752{
753 const char *from = addr;
754 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
755
David Howells00e23702018-10-22 13:07:28 +0100756 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsec6347b2020-10-05 20:40:16 -0700757 return copy_mc_pipe_to_iter(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700758 if (iter_is_iovec(i))
759 might_fault();
760 iterate_and_advance(i, bytes, v,
Dan Williamsec6347b2020-10-05 20:40:16 -0700761 copyout_mc(v.iov_base, (from += v.iov_len) - v.iov_len,
762 v.iov_len),
Dan Williams87803562018-05-03 17:06:31 -0700763 ({
Dan Williamsec6347b2020-10-05 20:40:16 -0700764 rem = copy_mc_to_page(v.bv_page, v.bv_offset,
765 (from += v.bv_len) - v.bv_len, v.bv_len);
Dan Williams87803562018-05-03 17:06:31 -0700766 if (rem) {
767 curr_addr = (unsigned long) from;
768 bytes = curr_addr - s_addr - rem;
769 return bytes;
770 }
771 }),
772 ({
Dan Williamsec6347b2020-10-05 20:40:16 -0700773 rem = copy_mc_to_kernel(v.iov_base, (from += v.iov_len)
774 - v.iov_len, v.iov_len);
Dan Williams87803562018-05-03 17:06:31 -0700775 if (rem) {
776 curr_addr = (unsigned long) from;
777 bytes = curr_addr - s_addr - rem;
778 return bytes;
779 }
David Howells7ff5062072020-02-10 10:00:21 +0000780 }),
781 ({
782 rem = copy_mc_to_page(v.bv_page, v.bv_offset,
783 (from += v.bv_len) - v.bv_len, v.bv_len);
784 if (rem) {
785 curr_addr = (unsigned long) from;
786 bytes = curr_addr - s_addr - rem;
787 rcu_read_unlock();
David Howells3d14ec12021-04-25 22:02:38 +0100788 i->iov_offset += bytes;
789 i->count -= bytes;
David Howells7ff5062072020-02-10 10:00:21 +0000790 return bytes;
791 }
Dan Williams87803562018-05-03 17:06:31 -0700792 })
793 )
794
795 return bytes;
796}
Dan Williamsec6347b2020-10-05 20:40:16 -0700797EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
798#endif /* CONFIG_ARCH_HAS_COPY_MC */
Dan Williams87803562018-05-03 17:06:31 -0700799
Al Viroaa28de22017-06-29 21:45:10 -0400800size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400801{
Al Viro0dbca9a2014-11-27 14:26:43 -0500802 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100803 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400804 WARN_ON(1);
805 return 0;
806 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400807 if (iter_is_iovec(i))
808 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500809 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400810 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500811 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500812 v.bv_offset, v.bv_len),
David Howells7ff5062072020-02-10 10:00:21 +0000813 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
814 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
815 v.bv_offset, v.bv_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500816 )
817
818 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400819}
Al Viroaa28de22017-06-29 21:45:10 -0400820EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400821
Al Viroaa28de22017-06-29 21:45:10 -0400822bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400823{
824 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100825 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400826 WARN_ON(1);
827 return false;
828 }
Al Viro33844e62016-12-21 21:55:02 -0500829 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400830 return false;
831
Al Viro09fc68dc2017-06-29 22:25:14 -0400832 if (iter_is_iovec(i))
833 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400834 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68dc2017-06-29 22:25:14 -0400835 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400836 v.iov_base, v.iov_len))
837 return false;
838 0;}),
839 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
840 v.bv_offset, v.bv_len),
David Howells7ff5062072020-02-10 10:00:21 +0000841 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
842 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
843 v.bv_offset, v.bv_len)
Al Virocbbd26b2016-11-01 22:09:04 -0400844 )
845
846 iov_iter_advance(i, bytes);
847 return true;
848}
Al Viroaa28de22017-06-29 21:45:10 -0400849EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400850
Al Viroaa28de22017-06-29 21:45:10 -0400851size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500852{
853 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100854 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400855 WARN_ON(1);
856 return 0;
857 }
Al Viroaa583092014-11-27 20:27:08 -0500858 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400859 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500860 v.iov_base, v.iov_len),
861 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
862 v.bv_offset, v.bv_len),
David Howells7ff5062072020-02-10 10:00:21 +0000863 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
864 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
865 v.bv_offset, v.bv_len)
Al Viroaa583092014-11-27 20:27:08 -0500866 )
867
868 return bytes;
869}
Al Viroaa28de22017-06-29 21:45:10 -0400870EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500871
Dan Williams0aed55a2017-05-29 12:22:50 -0700872#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700873/**
874 * _copy_from_iter_flushcache - write destination through cpu cache
875 * @addr: destination kernel address
876 * @bytes: total transfer length
877 * @iter: source iterator
878 *
879 * The pmem driver arranges for filesystem-dax to use this facility via
880 * dax_copy_from_iter() for ensuring that writes to persistent memory
881 * are flushed through the CPU cache. It is differentiated from
882 * _copy_from_iter_nocache() in that guarantees all data is flushed for
883 * all iterator types. The _copy_from_iter_nocache() only attempts to
884 * bypass the cache for the ITER_IOVEC case, and on some archs may use
885 * instructions that strand dirty-data in the cache.
886 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700887size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700888{
889 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100890 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700891 WARN_ON(1);
892 return 0;
893 }
894 iterate_and_advance(i, bytes, v,
895 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
896 v.iov_base, v.iov_len),
897 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
898 v.bv_offset, v.bv_len),
899 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
David Howells7ff5062072020-02-10 10:00:21 +0000900 v.iov_len),
901 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
902 v.bv_offset, v.bv_len)
Dan Williams0aed55a2017-05-29 12:22:50 -0700903 )
904
905 return bytes;
906}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700907EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700908#endif
909
Al Viroaa28de22017-06-29 21:45:10 -0400910bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400911{
912 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100913 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400914 WARN_ON(1);
915 return false;
916 }
Al Viro33844e62016-12-21 21:55:02 -0500917 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400918 return false;
919 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400920 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400921 v.iov_base, v.iov_len))
922 return false;
923 0;}),
924 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
925 v.bv_offset, v.bv_len),
David Howells7ff5062072020-02-10 10:00:21 +0000926 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
927 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
928 v.bv_offset, v.bv_len)
Al Virocbbd26b2016-11-01 22:09:04 -0400929 )
930
931 iov_iter_advance(i, bytes);
932 return true;
933}
Al Viroaa28de22017-06-29 21:45:10 -0400934EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400935
Al Viro72e809e2017-06-29 21:52:57 -0400936static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
937{
Eric Dumazet6daef952019-02-26 10:42:39 -0800938 struct page *head;
939 size_t v = n + offset;
940
941 /*
942 * The general case needs to access the page order in order
943 * to compute the page size.
944 * However, we mostly deal with order-0 pages and thus can
945 * avoid a possible cache line miss for requests that fit all
946 * page orders.
947 */
948 if (n <= v && v <= PAGE_SIZE)
949 return true;
950
951 head = compound_head(page);
952 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700953
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700954 if (likely(n <= v && v <= (page_size(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400955 return true;
956 WARN_ON(1);
957 return false;
958}
Al Virod2715242014-11-27 14:22:37 -0500959
960size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
961 struct iov_iter *i)
962{
Al Viro72e809e2017-06-29 21:52:57 -0400963 if (unlikely(!page_copy_sane(page, offset, bytes)))
964 return 0;
David Howells7ff5062072020-02-10 10:00:21 +0000965 if (i->type & (ITER_BVEC | ITER_KVEC | ITER_XARRAY)) {
Al Virod2715242014-11-27 14:22:37 -0500966 void *kaddr = kmap_atomic(page);
967 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
968 kunmap_atomic(kaddr);
969 return wanted;
David Howells9ea9ce02018-10-20 00:57:56 +0100970 } else if (unlikely(iov_iter_is_discard(i)))
971 return bytes;
972 else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500973 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400974 else
975 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500976}
977EXPORT_SYMBOL(copy_page_to_iter);
978
979size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
980 struct iov_iter *i)
981{
Al Viro72e809e2017-06-29 21:52:57 -0400982 if (unlikely(!page_copy_sane(page, offset, bytes)))
983 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +0100984 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400985 WARN_ON(1);
986 return 0;
987 }
David Howells7ff5062072020-02-10 10:00:21 +0000988 if (i->type & (ITER_BVEC | ITER_KVEC | ITER_XARRAY)) {
Al Virod2715242014-11-27 14:22:37 -0500989 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400990 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500991 kunmap_atomic(kaddr);
992 return wanted;
993 } else
994 return copy_page_from_iter_iovec(page, offset, bytes, i);
995}
996EXPORT_SYMBOL(copy_page_from_iter);
997
Al Viro241699c2016-09-22 16:33:12 -0400998static size_t pipe_zero(size_t bytes, struct iov_iter *i)
999{
1000 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001001 unsigned int p_mask = pipe->ring_size - 1;
1002 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -04001003 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -04001004
1005 if (!sanity(i))
1006 return 0;
1007
David Howells8cefc102019-11-15 13:30:32 +00001008 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001009 if (unlikely(!n))
1010 return 0;
1011
David Howells8cefc102019-11-15 13:30:32 +00001012 do {
Al Viro241699c2016-09-22 16:33:12 -04001013 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +00001014 memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk);
1015 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -04001016 i->iov_offset = off + chunk;
1017 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +00001018 off = 0;
1019 i_head++;
1020 } while (n);
Al Viro241699c2016-09-22 16:33:12 -04001021 i->count -= bytes;
1022 return bytes;
1023}
1024
Matthew Wilcoxc35e0242014-08-01 09:27:22 -04001025size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
1026{
David Howells00e23702018-10-22 13:07:28 +01001027 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001028 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -05001029 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -04001030 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -05001031 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
David Howells7ff5062072020-02-10 10:00:21 +00001032 memset(v.iov_base, 0, v.iov_len),
1033 memzero_page(v.bv_page, v.bv_offset, v.bv_len)
Al Viro8442fa42014-11-27 14:18:54 -05001034 )
1035
1036 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -04001037}
1038EXPORT_SYMBOL(iov_iter_zero);
1039
Al Viro62a80672014-04-04 23:12:29 -04001040size_t iov_iter_copy_from_user_atomic(struct page *page,
1041 struct iov_iter *i, unsigned long offset, size_t bytes)
1042{
Al Viro04a31162014-11-27 13:51:41 -05001043 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -04001044 if (unlikely(!page_copy_sane(page, offset, bytes))) {
1045 kunmap_atomic(kaddr);
1046 return 0;
1047 }
David Howells9ea9ce02018-10-20 00:57:56 +01001048 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001049 kunmap_atomic(kaddr);
1050 WARN_ON(1);
1051 return 0;
1052 }
Al Viro04a31162014-11-27 13:51:41 -05001053 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -04001054 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -05001055 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -05001056 v.bv_offset, v.bv_len),
David Howells7ff5062072020-02-10 10:00:21 +00001057 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
1058 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
1059 v.bv_offset, v.bv_len)
Al Viro04a31162014-11-27 13:51:41 -05001060 )
1061 kunmap_atomic(kaddr);
1062 return bytes;
Al Viro62a80672014-04-04 23:12:29 -04001063}
1064EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
1065
Al Virob9dc6f62017-01-14 19:33:08 -05001066static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -04001067{
1068 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001069 unsigned int p_tail = pipe->tail;
1070 unsigned int p_head = pipe->head;
1071 unsigned int p_mask = pipe->ring_size - 1;
1072
1073 if (!pipe_empty(p_head, p_tail)) {
1074 struct pipe_buffer *buf;
1075 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001076 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +00001077
Al Virob9dc6f62017-01-14 19:33:08 -05001078 if (off) {
David Howells8cefc102019-11-15 13:30:32 +00001079 buf = &pipe->bufs[i_head & p_mask];
1080 buf->len = off - buf->offset;
1081 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001082 }
David Howells8cefc102019-11-15 13:30:32 +00001083 while (p_head != i_head) {
1084 p_head--;
1085 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
Al Viro241699c2016-09-22 16:33:12 -04001086 }
David Howells8cefc102019-11-15 13:30:32 +00001087
1088 pipe->head = p_head;
Al Viro241699c2016-09-22 16:33:12 -04001089 }
Al Virob9dc6f62017-01-14 19:33:08 -05001090}
1091
1092static void pipe_advance(struct iov_iter *i, size_t size)
1093{
1094 struct pipe_inode_info *pipe = i->pipe;
1095 if (unlikely(i->count < size))
1096 size = i->count;
1097 if (size) {
1098 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +00001099 unsigned int p_mask = pipe->ring_size - 1;
1100 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001101 size_t off = i->iov_offset, left = size;
David Howells8cefc102019-11-15 13:30:32 +00001102
Al Virob9dc6f62017-01-14 19:33:08 -05001103 if (off) /* make it relative to the beginning of buffer */
David Howells8cefc102019-11-15 13:30:32 +00001104 left += off - pipe->bufs[i_head & p_mask].offset;
Al Virob9dc6f62017-01-14 19:33:08 -05001105 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001106 buf = &pipe->bufs[i_head & p_mask];
Al Virob9dc6f62017-01-14 19:33:08 -05001107 if (left <= buf->len)
1108 break;
1109 left -= buf->len;
David Howells8cefc102019-11-15 13:30:32 +00001110 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001111 }
David Howells8cefc102019-11-15 13:30:32 +00001112 i->head = i_head;
Al Virob9dc6f62017-01-14 19:33:08 -05001113 i->iov_offset = buf->offset + left;
1114 }
1115 i->count -= size;
1116 /* ... and discard everything past that point */
1117 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001118}
1119
Pavel Begunkov54c81952021-01-09 16:03:01 +00001120static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
1121{
1122 struct bvec_iter bi;
1123
1124 bi.bi_size = i->count;
1125 bi.bi_bvec_done = i->iov_offset;
1126 bi.bi_idx = 0;
1127 bvec_iter_advance(i->bvec, &bi, size);
1128
1129 i->bvec += bi.bi_idx;
1130 i->nr_segs -= bi.bi_idx;
1131 i->count = bi.bi_size;
1132 i->iov_offset = bi.bi_bvec_done;
1133}
1134
Al Viro62a80672014-04-04 23:12:29 -04001135void iov_iter_advance(struct iov_iter *i, size_t size)
1136{
David Howells00e23702018-10-22 13:07:28 +01001137 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001138 pipe_advance(i, size);
1139 return;
1140 }
David Howells9ea9ce02018-10-20 00:57:56 +01001141 if (unlikely(iov_iter_is_discard(i))) {
1142 i->count -= size;
1143 return;
1144 }
David Howells7ff5062072020-02-10 10:00:21 +00001145 if (unlikely(iov_iter_is_xarray(i))) {
David Howells3d14ec12021-04-25 22:02:38 +01001146 size = min(size, i->count);
David Howells7ff5062072020-02-10 10:00:21 +00001147 i->iov_offset += size;
1148 i->count -= size;
1149 return;
1150 }
Pavel Begunkov54c81952021-01-09 16:03:01 +00001151 if (iov_iter_is_bvec(i)) {
1152 iov_iter_bvec_advance(i, size);
1153 return;
1154 }
David Howells7ff5062072020-02-10 10:00:21 +00001155 iterate_and_advance(i, size, v, 0, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -04001156}
1157EXPORT_SYMBOL(iov_iter_advance);
1158
Al Viro27c0e372017-02-17 18:42:24 -05001159void iov_iter_revert(struct iov_iter *i, size_t unroll)
1160{
1161 if (!unroll)
1162 return;
Al Viro5b47d592017-05-08 13:54:47 -04001163 if (WARN_ON(unroll > MAX_RW_COUNT))
1164 return;
Al Viro27c0e372017-02-17 18:42:24 -05001165 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001166 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001167 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001168 unsigned int p_mask = pipe->ring_size - 1;
1169 unsigned int i_head = i->head;
Al Viro27c0e372017-02-17 18:42:24 -05001170 size_t off = i->iov_offset;
1171 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001172 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1173 size_t n = off - b->offset;
Al Viro27c0e372017-02-17 18:42:24 -05001174 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001175 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001176 break;
1177 }
1178 unroll -= n;
David Howells8cefc102019-11-15 13:30:32 +00001179 if (!unroll && i_head == i->start_head) {
Al Viro27c0e372017-02-17 18:42:24 -05001180 off = 0;
1181 break;
1182 }
David Howells8cefc102019-11-15 13:30:32 +00001183 i_head--;
1184 b = &pipe->bufs[i_head & p_mask];
1185 off = b->offset + b->len;
Al Viro27c0e372017-02-17 18:42:24 -05001186 }
1187 i->iov_offset = off;
David Howells8cefc102019-11-15 13:30:32 +00001188 i->head = i_head;
Al Viro27c0e372017-02-17 18:42:24 -05001189 pipe_truncate(i);
1190 return;
1191 }
David Howells9ea9ce02018-10-20 00:57:56 +01001192 if (unlikely(iov_iter_is_discard(i)))
1193 return;
Al Viro27c0e372017-02-17 18:42:24 -05001194 if (unroll <= i->iov_offset) {
1195 i->iov_offset -= unroll;
1196 return;
1197 }
1198 unroll -= i->iov_offset;
David Howells7ff5062072020-02-10 10:00:21 +00001199 if (iov_iter_is_xarray(i)) {
1200 BUG(); /* We should never go beyond the start of the specified
1201 * range since we might then be straying into pages that
1202 * aren't pinned.
1203 */
1204 } else if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001205 const struct bio_vec *bvec = i->bvec;
1206 while (1) {
1207 size_t n = (--bvec)->bv_len;
1208 i->nr_segs++;
1209 if (unroll <= n) {
1210 i->bvec = bvec;
1211 i->iov_offset = n - unroll;
1212 return;
1213 }
1214 unroll -= n;
1215 }
1216 } else { /* same logics for iovec and kvec */
1217 const struct iovec *iov = i->iov;
1218 while (1) {
1219 size_t n = (--iov)->iov_len;
1220 i->nr_segs++;
1221 if (unroll <= n) {
1222 i->iov = iov;
1223 i->iov_offset = n - unroll;
1224 return;
1225 }
1226 unroll -= n;
1227 }
1228 }
1229}
1230EXPORT_SYMBOL(iov_iter_revert);
1231
Al Viro62a80672014-04-04 23:12:29 -04001232/*
1233 * Return the count of just the current iov_iter segment.
1234 */
1235size_t iov_iter_single_seg_count(const struct iov_iter *i)
1236{
David Howells00e23702018-10-22 13:07:28 +01001237 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001238 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001239 if (i->nr_segs == 1)
1240 return i->count;
David Howells7ff5062072020-02-10 10:00:21 +00001241 if (unlikely(iov_iter_is_discard(i) || iov_iter_is_xarray(i)))
David Howells9ea9ce02018-10-20 00:57:56 +01001242 return i->count;
David Howells7ff5062072020-02-10 10:00:21 +00001243 if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001244 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001245 else
1246 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001247}
1248EXPORT_SYMBOL(iov_iter_single_seg_count);
1249
David Howellsaa563d72018-10-20 00:57:56 +01001250void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001251 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001252 size_t count)
1253{
David Howellsaa563d72018-10-20 00:57:56 +01001254 WARN_ON(direction & ~(READ | WRITE));
1255 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001256 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001257 i->nr_segs = nr_segs;
1258 i->iov_offset = 0;
1259 i->count = count;
1260}
1261EXPORT_SYMBOL(iov_iter_kvec);
1262
David Howellsaa563d72018-10-20 00:57:56 +01001263void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001264 const struct bio_vec *bvec, unsigned long nr_segs,
1265 size_t count)
1266{
David Howellsaa563d72018-10-20 00:57:56 +01001267 WARN_ON(direction & ~(READ | WRITE));
1268 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001269 i->bvec = bvec;
1270 i->nr_segs = nr_segs;
1271 i->iov_offset = 0;
1272 i->count = count;
1273}
1274EXPORT_SYMBOL(iov_iter_bvec);
1275
David Howellsaa563d72018-10-20 00:57:56 +01001276void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001277 struct pipe_inode_info *pipe,
1278 size_t count)
1279{
David Howellsaa563d72018-10-20 00:57:56 +01001280 BUG_ON(direction != READ);
David Howells8cefc102019-11-15 13:30:32 +00001281 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
David Howellsaa563d72018-10-20 00:57:56 +01001282 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001283 i->pipe = pipe;
David Howells8cefc102019-11-15 13:30:32 +00001284 i->head = pipe->head;
Al Viro241699c2016-09-22 16:33:12 -04001285 i->iov_offset = 0;
1286 i->count = count;
David Howells8cefc102019-11-15 13:30:32 +00001287 i->start_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -04001288}
1289EXPORT_SYMBOL(iov_iter_pipe);
1290
David Howells9ea9ce02018-10-20 00:57:56 +01001291/**
David Howells7ff5062072020-02-10 10:00:21 +00001292 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1293 * @i: The iterator to initialise.
1294 * @direction: The direction of the transfer.
1295 * @xarray: The xarray to access.
1296 * @start: The start file position.
1297 * @count: The size of the I/O buffer in bytes.
1298 *
1299 * Set up an I/O iterator to either draw data out of the pages attached to an
1300 * inode or to inject data into those pages. The pages *must* be prevented
1301 * from evaporation, either by taking a ref on them or locking them by the
1302 * caller.
1303 */
1304void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1305 struct xarray *xarray, loff_t start, size_t count)
1306{
1307 BUG_ON(direction & ~1);
1308 i->type = ITER_XARRAY | (direction & (READ | WRITE));
1309 i->xarray = xarray;
1310 i->xarray_start = start;
1311 i->count = count;
1312 i->iov_offset = 0;
1313}
1314EXPORT_SYMBOL(iov_iter_xarray);
1315
1316/**
David Howells9ea9ce02018-10-20 00:57:56 +01001317 * iov_iter_discard - Initialise an I/O iterator that discards data
1318 * @i: The iterator to initialise.
1319 * @direction: The direction of the transfer.
1320 * @count: The size of the I/O buffer in bytes.
1321 *
1322 * Set up an I/O iterator that just discards everything that's written to it.
1323 * It's only available as a READ iterator.
1324 */
1325void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1326{
1327 BUG_ON(direction != READ);
1328 i->type = ITER_DISCARD | READ;
1329 i->count = count;
1330 i->iov_offset = 0;
1331}
1332EXPORT_SYMBOL(iov_iter_discard);
1333
Al Viro62a80672014-04-04 23:12:29 -04001334unsigned long iov_iter_alignment(const struct iov_iter *i)
1335{
Al Viro04a31162014-11-27 13:51:41 -05001336 unsigned long res = 0;
1337 size_t size = i->count;
1338
David Howells00e23702018-10-22 13:07:28 +01001339 if (unlikely(iov_iter_is_pipe(i))) {
Jan Karae0ff1262019-12-16 11:54:32 +01001340 unsigned int p_mask = i->pipe->ring_size - 1;
1341
David Howells8cefc102019-11-15 13:30:32 +00001342 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
Al Viro241699c2016-09-22 16:33:12 -04001343 return size | i->iov_offset;
1344 return size;
1345 }
David Howells3d14ec12021-04-25 22:02:38 +01001346 if (unlikely(iov_iter_is_xarray(i)))
1347 return (i->xarray_start + i->iov_offset) | i->count;
Al Viro04a31162014-11-27 13:51:41 -05001348 iterate_all_kinds(i, size, v,
1349 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001350 res |= v.bv_offset | v.bv_len,
David Howells7ff5062072020-02-10 10:00:21 +00001351 res |= (unsigned long)v.iov_base | v.iov_len,
1352 res |= v.bv_offset | v.bv_len
Al Viro04a31162014-11-27 13:51:41 -05001353 )
1354 return res;
Al Viro62a80672014-04-04 23:12:29 -04001355}
1356EXPORT_SYMBOL(iov_iter_alignment);
1357
Al Viro357f4352016-04-08 19:05:19 -04001358unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1359{
Al Viro33844e62016-12-21 21:55:02 -05001360 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001361 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001362
David Howells9ea9ce02018-10-20 00:57:56 +01001363 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001364 WARN_ON(1);
1365 return ~0U;
1366 }
1367
Al Viro357f4352016-04-08 19:05:19 -04001368 iterate_all_kinds(i, size, v,
1369 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1370 (size != v.iov_len ? size : 0), 0),
1371 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1372 (size != v.bv_len ? size : 0)),
1373 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
David Howells7ff5062072020-02-10 10:00:21 +00001374 (size != v.iov_len ? size : 0)),
1375 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1376 (size != v.bv_len ? size : 0))
Al Viro357f4352016-04-08 19:05:19 -04001377 );
Al Viro33844e62016-12-21 21:55:02 -05001378 return res;
Al Viro357f4352016-04-08 19:05:19 -04001379}
1380EXPORT_SYMBOL(iov_iter_gap_alignment);
1381
Ilya Dryomove76b63122018-05-02 20:16:56 +02001382static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001383 size_t maxsize,
1384 struct page **pages,
David Howells8cefc102019-11-15 13:30:32 +00001385 int iter_head,
Al Viro241699c2016-09-22 16:33:12 -04001386 size_t *start)
1387{
1388 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001389 unsigned int p_mask = pipe->ring_size - 1;
1390 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001391 if (!n)
1392 return -EFAULT;
1393
1394 maxsize = n;
1395 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001396 while (n > 0) {
David Howells8cefc102019-11-15 13:30:32 +00001397 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1398 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -04001399 n -= PAGE_SIZE;
1400 }
1401
1402 return maxsize;
1403}
1404
1405static ssize_t pipe_get_pages(struct iov_iter *i,
1406 struct page **pages, size_t maxsize, unsigned maxpages,
1407 size_t *start)
1408{
David Howells8cefc102019-11-15 13:30:32 +00001409 unsigned int iter_head, npages;
Al Viro241699c2016-09-22 16:33:12 -04001410 size_t capacity;
Al Viro241699c2016-09-22 16:33:12 -04001411
Al Viro33844e62016-12-21 21:55:02 -05001412 if (!maxsize)
1413 return 0;
1414
Al Viro241699c2016-09-22 16:33:12 -04001415 if (!sanity(i))
1416 return -EFAULT;
1417
David Howells8cefc102019-11-15 13:30:32 +00001418 data_start(i, &iter_head, start);
1419 /* Amount of free space: some of this one + all after this one */
1420 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1421 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
Al Viro241699c2016-09-22 16:33:12 -04001422
David Howells8cefc102019-11-15 13:30:32 +00001423 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001424}
1425
David Howells7ff5062072020-02-10 10:00:21 +00001426static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1427 pgoff_t index, unsigned int nr_pages)
1428{
1429 XA_STATE(xas, xa, index);
1430 struct page *page;
1431 unsigned int ret = 0;
1432
1433 rcu_read_lock();
1434 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1435 if (xas_retry(&xas, page))
1436 continue;
1437
1438 /* Has the page moved or been split? */
1439 if (unlikely(page != xas_reload(&xas))) {
1440 xas_reset(&xas);
1441 continue;
1442 }
1443
1444 pages[ret] = find_subpage(page, xas.xa_index);
1445 get_page(pages[ret]);
1446 if (++ret == nr_pages)
1447 break;
1448 }
1449 rcu_read_unlock();
1450 return ret;
1451}
1452
1453static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1454 struct page **pages, size_t maxsize,
1455 unsigned maxpages, size_t *_start_offset)
1456{
1457 unsigned nr, offset;
1458 pgoff_t index, count;
1459 size_t size = maxsize, actual;
1460 loff_t pos;
1461
1462 if (!size || !maxpages)
1463 return 0;
1464
1465 pos = i->xarray_start + i->iov_offset;
1466 index = pos >> PAGE_SHIFT;
1467 offset = pos & ~PAGE_MASK;
1468 *_start_offset = offset;
1469
1470 count = 1;
1471 if (size > PAGE_SIZE - offset) {
1472 size -= PAGE_SIZE - offset;
1473 count += size >> PAGE_SHIFT;
1474 size &= ~PAGE_MASK;
1475 if (size)
1476 count++;
1477 }
1478
1479 if (count > maxpages)
1480 count = maxpages;
1481
1482 nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
1483 if (nr == 0)
1484 return 0;
1485
1486 actual = PAGE_SIZE * nr;
1487 actual -= offset;
1488 if (nr == count && size > 0) {
1489 unsigned last_offset = (nr > 1) ? 0 : offset;
1490 actual -= PAGE_SIZE - (last_offset + size);
1491 }
1492 return actual;
1493}
1494
Al Viro62a80672014-04-04 23:12:29 -04001495ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001496 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001497 size_t *start)
1498{
Al Viroe5393fa2014-11-27 14:12:09 -05001499 if (maxsize > i->count)
1500 maxsize = i->count;
1501
David Howells00e23702018-10-22 13:07:28 +01001502 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001503 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells7ff5062072020-02-10 10:00:21 +00001504 if (unlikely(iov_iter_is_xarray(i)))
1505 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001506 if (unlikely(iov_iter_is_discard(i)))
1507 return -EFAULT;
1508
Al Viroe5393fa2014-11-27 14:12:09 -05001509 iterate_all_kinds(i, maxsize, v, ({
1510 unsigned long addr = (unsigned long)v.iov_base;
1511 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1512 int n;
1513 int res;
1514
1515 if (len > maxpages * PAGE_SIZE)
1516 len = maxpages * PAGE_SIZE;
1517 addr &= ~(PAGE_SIZE - 1);
1518 n = DIV_ROUND_UP(len, PAGE_SIZE);
Ira Weiny73b01402019-05-13 17:17:11 -07001519 res = get_user_pages_fast(addr, n,
1520 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1521 pages);
Al Viroe5393fa2014-11-27 14:12:09 -05001522 if (unlikely(res < 0))
1523 return res;
1524 return (res == n ? len : res * PAGE_SIZE) - *start;
1525 0;}),({
1526 /* can't be more than PAGE_SIZE */
1527 *start = v.bv_offset;
1528 get_page(*pages = v.bv_page);
1529 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001530 }),({
1531 return -EFAULT;
David Howells7ff5062072020-02-10 10:00:21 +00001532 }),
1533 0
Al Viroe5393fa2014-11-27 14:12:09 -05001534 )
1535 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001536}
1537EXPORT_SYMBOL(iov_iter_get_pages);
1538
Al Viro1b17f1f2014-11-27 14:14:31 -05001539static struct page **get_pages_array(size_t n)
1540{
Michal Hocko752ade62017-05-08 15:57:27 -07001541 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001542}
1543
Al Viro241699c2016-09-22 16:33:12 -04001544static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1545 struct page ***pages, size_t maxsize,
1546 size_t *start)
1547{
1548 struct page **p;
David Howells8cefc102019-11-15 13:30:32 +00001549 unsigned int iter_head, npages;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001550 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001551
Al Viro33844e62016-12-21 21:55:02 -05001552 if (!maxsize)
1553 return 0;
1554
Al Viro241699c2016-09-22 16:33:12 -04001555 if (!sanity(i))
1556 return -EFAULT;
1557
David Howells8cefc102019-11-15 13:30:32 +00001558 data_start(i, &iter_head, start);
1559 /* Amount of free space: some of this one + all after this one */
1560 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
Al Viro241699c2016-09-22 16:33:12 -04001561 n = npages * PAGE_SIZE - *start;
1562 if (maxsize > n)
1563 maxsize = n;
1564 else
1565 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1566 p = get_pages_array(npages);
1567 if (!p)
1568 return -ENOMEM;
David Howells8cefc102019-11-15 13:30:32 +00001569 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001570 if (n > 0)
1571 *pages = p;
1572 else
1573 kvfree(p);
1574 return n;
1575}
1576
David Howells7ff5062072020-02-10 10:00:21 +00001577static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
1578 struct page ***pages, size_t maxsize,
1579 size_t *_start_offset)
1580{
1581 struct page **p;
1582 unsigned nr, offset;
1583 pgoff_t index, count;
1584 size_t size = maxsize, actual;
1585 loff_t pos;
1586
1587 if (!size)
1588 return 0;
1589
1590 pos = i->xarray_start + i->iov_offset;
1591 index = pos >> PAGE_SHIFT;
1592 offset = pos & ~PAGE_MASK;
1593 *_start_offset = offset;
1594
1595 count = 1;
1596 if (size > PAGE_SIZE - offset) {
1597 size -= PAGE_SIZE - offset;
1598 count += size >> PAGE_SHIFT;
1599 size &= ~PAGE_MASK;
1600 if (size)
1601 count++;
1602 }
1603
1604 p = get_pages_array(count);
1605 if (!p)
1606 return -ENOMEM;
1607 *pages = p;
1608
1609 nr = iter_xarray_populate_pages(p, i->xarray, index, count);
1610 if (nr == 0)
1611 return 0;
1612
1613 actual = PAGE_SIZE * nr;
1614 actual -= offset;
1615 if (nr == count && size > 0) {
1616 unsigned last_offset = (nr > 1) ? 0 : offset;
1617 actual -= PAGE_SIZE - (last_offset + size);
1618 }
1619 return actual;
1620}
1621
Al Viro62a80672014-04-04 23:12:29 -04001622ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1623 struct page ***pages, size_t maxsize,
1624 size_t *start)
1625{
Al Viro1b17f1f2014-11-27 14:14:31 -05001626 struct page **p;
1627
1628 if (maxsize > i->count)
1629 maxsize = i->count;
1630
David Howells00e23702018-10-22 13:07:28 +01001631 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001632 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells7ff5062072020-02-10 10:00:21 +00001633 if (unlikely(iov_iter_is_xarray(i)))
1634 return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001635 if (unlikely(iov_iter_is_discard(i)))
1636 return -EFAULT;
1637
Al Viro1b17f1f2014-11-27 14:14:31 -05001638 iterate_all_kinds(i, maxsize, v, ({
1639 unsigned long addr = (unsigned long)v.iov_base;
1640 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1641 int n;
1642 int res;
1643
1644 addr &= ~(PAGE_SIZE - 1);
1645 n = DIV_ROUND_UP(len, PAGE_SIZE);
1646 p = get_pages_array(n);
1647 if (!p)
1648 return -ENOMEM;
Ira Weiny73b01402019-05-13 17:17:11 -07001649 res = get_user_pages_fast(addr, n,
1650 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
Al Viro1b17f1f2014-11-27 14:14:31 -05001651 if (unlikely(res < 0)) {
1652 kvfree(p);
1653 return res;
1654 }
1655 *pages = p;
1656 return (res == n ? len : res * PAGE_SIZE) - *start;
1657 0;}),({
1658 /* can't be more than PAGE_SIZE */
1659 *start = v.bv_offset;
1660 *pages = p = get_pages_array(1);
1661 if (!p)
1662 return -ENOMEM;
1663 get_page(*p = v.bv_page);
1664 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001665 }),({
1666 return -EFAULT;
David Howells7ff5062072020-02-10 10:00:21 +00001667 }), 0
Al Viro1b17f1f2014-11-27 14:14:31 -05001668 )
1669 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001670}
1671EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1672
Al Viroa604ec72014-11-24 01:08:00 -05001673size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1674 struct iov_iter *i)
1675{
1676 char *to = addr;
1677 __wsum sum, next;
1678 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001679 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001680 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001681 WARN_ON(1);
1682 return 0;
1683 }
Al Viroa604ec72014-11-24 01:08:00 -05001684 iterate_and_advance(i, bytes, v, ({
Al Virocbbd26b2016-11-01 22:09:04 -04001685 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001686 (to += v.iov_len) - v.iov_len,
Al Viroc693cc42020-07-11 00:27:49 -04001687 v.iov_len);
1688 if (next) {
Al Viroa604ec72014-11-24 01:08:00 -05001689 sum = csum_block_add(sum, next, off);
1690 off += v.iov_len;
1691 }
Al Viroc693cc42020-07-11 00:27:49 -04001692 next ? 0 : v.iov_len;
Al Viroa604ec72014-11-24 01:08:00 -05001693 }), ({
1694 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001695 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1696 p + v.bv_offset, v.bv_len,
1697 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001698 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001699 off += v.bv_len;
1700 }),({
Al Virof9152892018-11-27 22:32:59 -05001701 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1702 v.iov_base, v.iov_len,
1703 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001704 off += v.iov_len;
David Howells7ff5062072020-02-10 10:00:21 +00001705 }), ({
1706 char *p = kmap_atomic(v.bv_page);
1707 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1708 p + v.bv_offset, v.bv_len,
1709 sum, off);
1710 kunmap_atomic(p);
1711 off += v.bv_len;
Al Viroa604ec72014-11-24 01:08:00 -05001712 })
1713 )
1714 *csum = sum;
1715 return bytes;
1716}
1717EXPORT_SYMBOL(csum_and_copy_from_iter);
1718
Al Virocbbd26b2016-11-01 22:09:04 -04001719bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1720 struct iov_iter *i)
1721{
1722 char *to = addr;
1723 __wsum sum, next;
1724 size_t off = 0;
1725 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001726 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001727 WARN_ON(1);
1728 return false;
1729 }
1730 if (unlikely(i->count < bytes))
1731 return false;
1732 iterate_all_kinds(i, bytes, v, ({
Al Virocbbd26b2016-11-01 22:09:04 -04001733 next = csum_and_copy_from_user(v.iov_base,
1734 (to += v.iov_len) - v.iov_len,
Al Viroc693cc42020-07-11 00:27:49 -04001735 v.iov_len);
1736 if (!next)
Al Virocbbd26b2016-11-01 22:09:04 -04001737 return false;
1738 sum = csum_block_add(sum, next, off);
1739 off += v.iov_len;
1740 0;
1741 }), ({
1742 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001743 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1744 p + v.bv_offset, v.bv_len,
1745 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001746 kunmap_atomic(p);
Al Virocbbd26b2016-11-01 22:09:04 -04001747 off += v.bv_len;
1748 }),({
Al Virof9152892018-11-27 22:32:59 -05001749 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1750 v.iov_base, v.iov_len,
1751 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001752 off += v.iov_len;
David Howells7ff5062072020-02-10 10:00:21 +00001753 }), ({
1754 char *p = kmap_atomic(v.bv_page);
1755 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1756 p + v.bv_offset, v.bv_len,
1757 sum, off);
1758 kunmap_atomic(p);
1759 off += v.bv_len;
Al Virocbbd26b2016-11-01 22:09:04 -04001760 })
1761 )
1762 *csum = sum;
1763 iov_iter_advance(i, bytes);
1764 return true;
1765}
1766EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1767
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001768size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
Al Viroa604ec72014-11-24 01:08:00 -05001769 struct iov_iter *i)
1770{
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001771 struct csum_state *csstate = _csstate;
Al Viro36f7a8a2015-12-06 16:49:22 -05001772 const char *from = addr;
Al Viroa604ec72014-11-24 01:08:00 -05001773 __wsum sum, next;
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001774 size_t off;
Al Viro78e1f382018-11-25 16:24:16 -05001775
1776 if (unlikely(iov_iter_is_pipe(i)))
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001777 return csum_and_copy_to_pipe_iter(addr, bytes, _csstate, i);
Al Viro78e1f382018-11-25 16:24:16 -05001778
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001779 sum = csstate->csum;
1780 off = csstate->off;
Al Viro78e1f382018-11-25 16:24:16 -05001781 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001782 WARN_ON(1); /* for now */
1783 return 0;
1784 }
Al Viroa604ec72014-11-24 01:08:00 -05001785 iterate_and_advance(i, bytes, v, ({
Al Viroa604ec72014-11-24 01:08:00 -05001786 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001787 v.iov_base,
Al Viroc693cc42020-07-11 00:27:49 -04001788 v.iov_len);
1789 if (next) {
Al Viroa604ec72014-11-24 01:08:00 -05001790 sum = csum_block_add(sum, next, off);
1791 off += v.iov_len;
1792 }
Al Viroc693cc42020-07-11 00:27:49 -04001793 next ? 0 : v.iov_len;
Al Viroa604ec72014-11-24 01:08:00 -05001794 }), ({
1795 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001796 sum = csum_and_memcpy(p + v.bv_offset,
1797 (from += v.bv_len) - v.bv_len,
1798 v.bv_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001799 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001800 off += v.bv_len;
1801 }),({
Al Virof9152892018-11-27 22:32:59 -05001802 sum = csum_and_memcpy(v.iov_base,
1803 (from += v.iov_len) - v.iov_len,
1804 v.iov_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001805 off += v.iov_len;
David Howells7ff5062072020-02-10 10:00:21 +00001806 }), ({
1807 char *p = kmap_atomic(v.bv_page);
1808 sum = csum_and_memcpy(p + v.bv_offset,
1809 (from += v.bv_len) - v.bv_len,
1810 v.bv_len, sum, off);
1811 kunmap_atomic(p);
1812 off += v.bv_len;
Al Viroa604ec72014-11-24 01:08:00 -05001813 })
1814 )
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001815 csstate->csum = sum;
1816 csstate->off = off;
Al Viroa604ec72014-11-24 01:08:00 -05001817 return bytes;
1818}
1819EXPORT_SYMBOL(csum_and_copy_to_iter);
1820
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001821size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1822 struct iov_iter *i)
1823{
Herbert Xu79990962020-06-12 16:57:37 +10001824#ifdef CONFIG_CRYPTO_HASH
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001825 struct ahash_request *hash = hashp;
1826 struct scatterlist sg;
1827 size_t copied;
1828
1829 copied = copy_to_iter(addr, bytes, i);
1830 sg_init_one(&sg, addr, copied);
1831 ahash_request_set_crypt(hash, &sg, NULL, copied);
1832 crypto_ahash_update(hash);
1833 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001834#else
1835 return 0;
1836#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001837}
1838EXPORT_SYMBOL(hash_and_copy_to_iter);
1839
Al Viro62a80672014-04-04 23:12:29 -04001840int iov_iter_npages(const struct iov_iter *i, int maxpages)
1841{
Al Viroe0f2dc42014-11-27 14:09:46 -05001842 size_t size = i->count;
1843 int npages = 0;
1844
1845 if (!size)
1846 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001847 if (unlikely(iov_iter_is_discard(i)))
1848 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001849
David Howells00e23702018-10-22 13:07:28 +01001850 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001851 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001852 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -04001853 size_t off;
Al Viro241699c2016-09-22 16:33:12 -04001854
1855 if (!sanity(i))
1856 return 0;
1857
David Howells8cefc102019-11-15 13:30:32 +00001858 data_start(i, &iter_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001859 /* some of this one + all after this one */
David Howells8cefc102019-11-15 13:30:32 +00001860 npages = pipe_space_for_user(iter_head, pipe->tail, pipe);
Al Viro241699c2016-09-22 16:33:12 -04001861 if (npages >= maxpages)
1862 return maxpages;
David Howells7ff5062072020-02-10 10:00:21 +00001863 } else if (unlikely(iov_iter_is_xarray(i))) {
1864 unsigned offset;
1865
1866 offset = (i->xarray_start + i->iov_offset) & ~PAGE_MASK;
1867
1868 npages = 1;
1869 if (size > PAGE_SIZE - offset) {
1870 size -= PAGE_SIZE - offset;
1871 npages += size >> PAGE_SHIFT;
1872 size &= ~PAGE_MASK;
1873 if (size)
1874 npages++;
1875 }
1876 if (npages >= maxpages)
1877 return maxpages;
Al Viro241699c2016-09-22 16:33:12 -04001878 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001879 unsigned long p = (unsigned long)v.iov_base;
1880 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1881 - p / PAGE_SIZE;
1882 if (npages >= maxpages)
1883 return maxpages;
1884 0;}),({
1885 npages++;
1886 if (npages >= maxpages)
1887 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001888 }),({
1889 unsigned long p = (unsigned long)v.iov_base;
1890 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1891 - p / PAGE_SIZE;
1892 if (npages >= maxpages)
1893 return maxpages;
David Howells7ff5062072020-02-10 10:00:21 +00001894 }),
1895 0
Al Viroe0f2dc42014-11-27 14:09:46 -05001896 )
1897 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001898}
Al Virof67da302014-03-19 01:16:16 -04001899EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001900
1901const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1902{
1903 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001904 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001905 WARN_ON(1);
1906 return NULL;
1907 }
David Howells7ff5062072020-02-10 10:00:21 +00001908 if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
David Howells9ea9ce02018-10-20 00:57:56 +01001909 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001910 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001911 return new->bvec = kmemdup(new->bvec,
1912 new->nr_segs * sizeof(struct bio_vec),
1913 flags);
1914 else
1915 /* iovec and kvec have identical layout */
1916 return new->iov = kmemdup(new->iov,
1917 new->nr_segs * sizeof(struct iovec),
1918 flags);
1919}
1920EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001921
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001922static int copy_compat_iovec_from_user(struct iovec *iov,
1923 const struct iovec __user *uvec, unsigned long nr_segs)
1924{
1925 const struct compat_iovec __user *uiov =
1926 (const struct compat_iovec __user *)uvec;
1927 int ret = -EFAULT, i;
1928
Christoph Hellwiga959a972021-01-11 18:19:26 +01001929 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001930 return -EFAULT;
1931
1932 for (i = 0; i < nr_segs; i++) {
1933 compat_uptr_t buf;
1934 compat_ssize_t len;
1935
1936 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1937 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1938
1939 /* check for compat_size_t not fitting in compat_ssize_t .. */
1940 if (len < 0) {
1941 ret = -EINVAL;
1942 goto uaccess_end;
1943 }
1944 iov[i].iov_base = compat_ptr(buf);
1945 iov[i].iov_len = len;
1946 }
1947
1948 ret = 0;
1949uaccess_end:
1950 user_access_end();
1951 return ret;
1952}
1953
1954static int copy_iovec_from_user(struct iovec *iov,
1955 const struct iovec __user *uvec, unsigned long nr_segs)
David Laightfb041b52020-09-25 06:51:39 +02001956{
1957 unsigned long seg;
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001958
1959 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1960 return -EFAULT;
1961 for (seg = 0; seg < nr_segs; seg++) {
1962 if ((ssize_t)iov[seg].iov_len < 0)
1963 return -EINVAL;
1964 }
1965
1966 return 0;
1967}
1968
1969struct iovec *iovec_from_user(const struct iovec __user *uvec,
1970 unsigned long nr_segs, unsigned long fast_segs,
1971 struct iovec *fast_iov, bool compat)
1972{
1973 struct iovec *iov = fast_iov;
1974 int ret;
David Laightfb041b52020-09-25 06:51:39 +02001975
1976 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001977 * SuS says "The readv() function *may* fail if the iovcnt argument was
1978 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
David Laightfb041b52020-09-25 06:51:39 +02001979 * traditionally returned zero for zero segments, so...
1980 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001981 if (nr_segs == 0)
1982 return iov;
1983 if (nr_segs > UIO_MAXIOV)
1984 return ERR_PTR(-EINVAL);
David Laightfb041b52020-09-25 06:51:39 +02001985 if (nr_segs > fast_segs) {
1986 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001987 if (!iov)
1988 return ERR_PTR(-ENOMEM);
David Laightfb041b52020-09-25 06:51:39 +02001989 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001990
1991 if (compat)
1992 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1993 else
1994 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1995 if (ret) {
1996 if (iov != fast_iov)
1997 kfree(iov);
1998 return ERR_PTR(ret);
1999 }
2000
2001 return iov;
2002}
2003
2004ssize_t __import_iovec(int type, const struct iovec __user *uvec,
2005 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
2006 struct iov_iter *i, bool compat)
2007{
2008 ssize_t total_len = 0;
2009 unsigned long seg;
2010 struct iovec *iov;
2011
2012 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
2013 if (IS_ERR(iov)) {
2014 *iovp = NULL;
2015 return PTR_ERR(iov);
David Laightfb041b52020-09-25 06:51:39 +02002016 }
2017
2018 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002019 * According to the Single Unix Specification we should return EINVAL if
2020 * an element length is < 0 when cast to ssize_t or if the total length
2021 * would overflow the ssize_t return value of the system call.
David Laightfb041b52020-09-25 06:51:39 +02002022 *
2023 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
2024 * overflow case.
2025 */
David Laightfb041b52020-09-25 06:51:39 +02002026 for (seg = 0; seg < nr_segs; seg++) {
David Laightfb041b52020-09-25 06:51:39 +02002027 ssize_t len = (ssize_t)iov[seg].iov_len;
2028
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002029 if (!access_ok(iov[seg].iov_base, len)) {
2030 if (iov != *iovp)
2031 kfree(iov);
2032 *iovp = NULL;
2033 return -EFAULT;
David Laightfb041b52020-09-25 06:51:39 +02002034 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002035
2036 if (len > MAX_RW_COUNT - total_len) {
2037 len = MAX_RW_COUNT - total_len;
David Laightfb041b52020-09-25 06:51:39 +02002038 iov[seg].iov_len = len;
2039 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002040 total_len += len;
David Laightfb041b52020-09-25 06:51:39 +02002041 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002042
2043 iov_iter_init(i, type, iov, nr_segs, total_len);
2044 if (iov == *iovp)
2045 *iovp = NULL;
2046 else
2047 *iovp = iov;
2048 return total_len;
David Laightfb041b52020-09-25 06:51:39 +02002049}
2050
Vegard Nossumffecee42016-10-08 11:18:07 +02002051/**
2052 * import_iovec() - Copy an array of &struct iovec from userspace
2053 * into the kernel, check that it is valid, and initialize a new
2054 * &struct iov_iter iterator to access it.
2055 *
2056 * @type: One of %READ or %WRITE.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002057 * @uvec: Pointer to the userspace array.
Vegard Nossumffecee42016-10-08 11:18:07 +02002058 * @nr_segs: Number of elements in userspace array.
2059 * @fast_segs: Number of elements in @iov.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002060 * @iovp: (input and output parameter) Pointer to pointer to (usually small
Vegard Nossumffecee42016-10-08 11:18:07 +02002061 * on-stack) kernel array.
2062 * @i: Pointer to iterator that will be initialized on success.
2063 *
2064 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
2065 * then this function places %NULL in *@iov on return. Otherwise, a new
2066 * array will be allocated and the result placed in *@iov. This means that
2067 * the caller may call kfree() on *@iov regardless of whether the small
2068 * on-stack array was used or not (and regardless of whether this function
2069 * returns an error or not).
2070 *
Jens Axboe87e5e6d2019-05-14 16:02:22 -06002071 * Return: Negative error code on error, bytes imported on success
Vegard Nossumffecee42016-10-08 11:18:07 +02002072 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002073ssize_t import_iovec(int type, const struct iovec __user *uvec,
Al Virobc917be2015-03-21 17:45:43 -04002074 unsigned nr_segs, unsigned fast_segs,
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002075 struct iovec **iovp, struct iov_iter *i)
Al Virobc917be2015-03-21 17:45:43 -04002076{
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02002077 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
2078 in_compat_syscall());
Al Virobc917be2015-03-21 17:45:43 -04002079}
2080EXPORT_SYMBOL(import_iovec);
2081
Al Virobc917be2015-03-21 17:45:43 -04002082int import_single_range(int rw, void __user *buf, size_t len,
2083 struct iovec *iov, struct iov_iter *i)
2084{
2085 if (len > MAX_RW_COUNT)
2086 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002087 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04002088 return -EFAULT;
2089
2090 iov->iov_base = buf;
2091 iov->iov_len = len;
2092 iov_iter_init(i, rw, iov, 1, len);
2093 return 0;
2094}
Al Viroe1267582015-12-06 20:38:56 -05002095EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05002096
2097int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
2098 int (*f)(struct kvec *vec, void *context),
2099 void *context)
2100{
2101 struct kvec w;
2102 int err = -EINVAL;
2103 if (!bytes)
2104 return 0;
2105
2106 iterate_all_kinds(i, bytes, v, -EINVAL, ({
2107 w.iov_base = kmap(v.bv_page) + v.bv_offset;
2108 w.iov_len = v.bv_len;
2109 err = f(&w, context);
2110 kunmap(v.bv_page);
2111 err;}), ({
2112 w = v;
David Howells7ff5062072020-02-10 10:00:21 +00002113 err = f(&w, context);}), ({
2114 w.iov_base = kmap(v.bv_page) + v.bv_offset;
2115 w.iov_len = v.bv_len;
2116 err = f(&w, context);
2117 kunmap(v.bv_page);
2118 err;})
Al Viro09cf6982017-02-18 01:44:03 -05002119 )
2120 return err;
2121}
2122EXPORT_SYMBOL(iov_iter_for_each_range);