blob: 12fb04b23143783dcc2b21597fb1bf5d5889b68b [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
Al Viro08aa6472021-04-29 20:42:25 -0400960static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
Al Virod2715242014-11-27 14:22:37 -0500961 struct iov_iter *i)
962{
David Howells7ff5062072020-02-10 10:00:21 +0000963 if (i->type & (ITER_BVEC | ITER_KVEC | ITER_XARRAY)) {
Al Virod2715242014-11-27 14:22:37 -0500964 void *kaddr = kmap_atomic(page);
965 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
966 kunmap_atomic(kaddr);
967 return wanted;
David Howells9ea9ce02018-10-20 00:57:56 +0100968 } else if (unlikely(iov_iter_is_discard(i)))
969 return bytes;
970 else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500971 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400972 else
973 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500974}
Al Viro08aa6472021-04-29 20:42:25 -0400975
976size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
977 struct iov_iter *i)
978{
979 size_t res = 0;
980 if (unlikely(!page_copy_sane(page, offset, bytes)))
981 return 0;
982 page += offset / PAGE_SIZE; // first subpage
983 offset %= PAGE_SIZE;
984 while (1) {
985 size_t n = __copy_page_to_iter(page, offset,
986 min(bytes, (size_t)PAGE_SIZE - offset), i);
987 res += n;
988 bytes -= n;
989 if (!bytes || !n)
990 break;
991 offset += n;
992 if (offset == PAGE_SIZE) {
993 page++;
994 offset = 0;
995 }
996 }
997 return res;
998}
Al Virod2715242014-11-27 14:22:37 -0500999EXPORT_SYMBOL(copy_page_to_iter);
1000
1001size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
1002 struct iov_iter *i)
1003{
Al Viro72e809e2017-06-29 21:52:57 -04001004 if (unlikely(!page_copy_sane(page, offset, bytes)))
1005 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001006 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001007 WARN_ON(1);
1008 return 0;
1009 }
David Howells7ff5062072020-02-10 10:00:21 +00001010 if (i->type & (ITER_BVEC | ITER_KVEC | ITER_XARRAY)) {
Al Virod2715242014-11-27 14:22:37 -05001011 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -04001012 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -05001013 kunmap_atomic(kaddr);
1014 return wanted;
1015 } else
1016 return copy_page_from_iter_iovec(page, offset, bytes, i);
1017}
1018EXPORT_SYMBOL(copy_page_from_iter);
1019
Al Viro241699c2016-09-22 16:33:12 -04001020static size_t pipe_zero(size_t bytes, struct iov_iter *i)
1021{
1022 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001023 unsigned int p_mask = pipe->ring_size - 1;
1024 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -04001025 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -04001026
1027 if (!sanity(i))
1028 return 0;
1029
David Howells8cefc102019-11-15 13:30:32 +00001030 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001031 if (unlikely(!n))
1032 return 0;
1033
David Howells8cefc102019-11-15 13:30:32 +00001034 do {
Al Viro241699c2016-09-22 16:33:12 -04001035 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +00001036 memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk);
1037 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -04001038 i->iov_offset = off + chunk;
1039 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +00001040 off = 0;
1041 i_head++;
1042 } while (n);
Al Viro241699c2016-09-22 16:33:12 -04001043 i->count -= bytes;
1044 return bytes;
1045}
1046
Matthew Wilcoxc35e0242014-08-01 09:27:22 -04001047size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
1048{
David Howells00e23702018-10-22 13:07:28 +01001049 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001050 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -05001051 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -04001052 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -05001053 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
David Howells7ff5062072020-02-10 10:00:21 +00001054 memset(v.iov_base, 0, v.iov_len),
1055 memzero_page(v.bv_page, v.bv_offset, v.bv_len)
Al Viro8442fa42014-11-27 14:18:54 -05001056 )
1057
1058 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -04001059}
1060EXPORT_SYMBOL(iov_iter_zero);
1061
Al Viro62a80672014-04-04 23:12:29 -04001062size_t iov_iter_copy_from_user_atomic(struct page *page,
1063 struct iov_iter *i, unsigned long offset, size_t bytes)
1064{
Al Viro04a31162014-11-27 13:51:41 -05001065 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -04001066 if (unlikely(!page_copy_sane(page, offset, bytes))) {
1067 kunmap_atomic(kaddr);
1068 return 0;
1069 }
David Howells9ea9ce02018-10-20 00:57:56 +01001070 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001071 kunmap_atomic(kaddr);
1072 WARN_ON(1);
1073 return 0;
1074 }
Al Viro04a31162014-11-27 13:51:41 -05001075 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -04001076 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -05001077 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -05001078 v.bv_offset, v.bv_len),
David Howells7ff5062072020-02-10 10:00:21 +00001079 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
1080 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
1081 v.bv_offset, v.bv_len)
Al Viro04a31162014-11-27 13:51:41 -05001082 )
1083 kunmap_atomic(kaddr);
1084 return bytes;
Al Viro62a80672014-04-04 23:12:29 -04001085}
1086EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
1087
Al Virob9dc6f62017-01-14 19:33:08 -05001088static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -04001089{
1090 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001091 unsigned int p_tail = pipe->tail;
1092 unsigned int p_head = pipe->head;
1093 unsigned int p_mask = pipe->ring_size - 1;
1094
1095 if (!pipe_empty(p_head, p_tail)) {
1096 struct pipe_buffer *buf;
1097 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001098 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +00001099
Al Virob9dc6f62017-01-14 19:33:08 -05001100 if (off) {
David Howells8cefc102019-11-15 13:30:32 +00001101 buf = &pipe->bufs[i_head & p_mask];
1102 buf->len = off - buf->offset;
1103 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001104 }
David Howells8cefc102019-11-15 13:30:32 +00001105 while (p_head != i_head) {
1106 p_head--;
1107 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
Al Viro241699c2016-09-22 16:33:12 -04001108 }
David Howells8cefc102019-11-15 13:30:32 +00001109
1110 pipe->head = p_head;
Al Viro241699c2016-09-22 16:33:12 -04001111 }
Al Virob9dc6f62017-01-14 19:33:08 -05001112}
1113
1114static void pipe_advance(struct iov_iter *i, size_t size)
1115{
1116 struct pipe_inode_info *pipe = i->pipe;
1117 if (unlikely(i->count < size))
1118 size = i->count;
1119 if (size) {
1120 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +00001121 unsigned int p_mask = pipe->ring_size - 1;
1122 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001123 size_t off = i->iov_offset, left = size;
David Howells8cefc102019-11-15 13:30:32 +00001124
Al Virob9dc6f62017-01-14 19:33:08 -05001125 if (off) /* make it relative to the beginning of buffer */
David Howells8cefc102019-11-15 13:30:32 +00001126 left += off - pipe->bufs[i_head & p_mask].offset;
Al Virob9dc6f62017-01-14 19:33:08 -05001127 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001128 buf = &pipe->bufs[i_head & p_mask];
Al Virob9dc6f62017-01-14 19:33:08 -05001129 if (left <= buf->len)
1130 break;
1131 left -= buf->len;
David Howells8cefc102019-11-15 13:30:32 +00001132 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001133 }
David Howells8cefc102019-11-15 13:30:32 +00001134 i->head = i_head;
Al Virob9dc6f62017-01-14 19:33:08 -05001135 i->iov_offset = buf->offset + left;
1136 }
1137 i->count -= size;
1138 /* ... and discard everything past that point */
1139 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001140}
1141
Pavel Begunkov54c81952021-01-09 16:03:01 +00001142static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
1143{
1144 struct bvec_iter bi;
1145
1146 bi.bi_size = i->count;
1147 bi.bi_bvec_done = i->iov_offset;
1148 bi.bi_idx = 0;
1149 bvec_iter_advance(i->bvec, &bi, size);
1150
1151 i->bvec += bi.bi_idx;
1152 i->nr_segs -= bi.bi_idx;
1153 i->count = bi.bi_size;
1154 i->iov_offset = bi.bi_bvec_done;
1155}
1156
Al Viro62a80672014-04-04 23:12:29 -04001157void iov_iter_advance(struct iov_iter *i, size_t size)
1158{
David Howells00e23702018-10-22 13:07:28 +01001159 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001160 pipe_advance(i, size);
1161 return;
1162 }
David Howells9ea9ce02018-10-20 00:57:56 +01001163 if (unlikely(iov_iter_is_discard(i))) {
1164 i->count -= size;
1165 return;
1166 }
David Howells7ff5062072020-02-10 10:00:21 +00001167 if (unlikely(iov_iter_is_xarray(i))) {
David Howells3d14ec12021-04-25 22:02:38 +01001168 size = min(size, i->count);
David Howells7ff5062072020-02-10 10:00:21 +00001169 i->iov_offset += size;
1170 i->count -= size;
1171 return;
1172 }
Pavel Begunkov54c81952021-01-09 16:03:01 +00001173 if (iov_iter_is_bvec(i)) {
1174 iov_iter_bvec_advance(i, size);
1175 return;
1176 }
David Howells7ff5062072020-02-10 10:00:21 +00001177 iterate_and_advance(i, size, v, 0, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -04001178}
1179EXPORT_SYMBOL(iov_iter_advance);
1180
Al Viro27c0e372017-02-17 18:42:24 -05001181void iov_iter_revert(struct iov_iter *i, size_t unroll)
1182{
1183 if (!unroll)
1184 return;
Al Viro5b47d592017-05-08 13:54:47 -04001185 if (WARN_ON(unroll > MAX_RW_COUNT))
1186 return;
Al Viro27c0e372017-02-17 18:42:24 -05001187 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001188 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001189 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001190 unsigned int p_mask = pipe->ring_size - 1;
1191 unsigned int i_head = i->head;
Al Viro27c0e372017-02-17 18:42:24 -05001192 size_t off = i->iov_offset;
1193 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001194 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1195 size_t n = off - b->offset;
Al Viro27c0e372017-02-17 18:42:24 -05001196 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001197 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001198 break;
1199 }
1200 unroll -= n;
David Howells8cefc102019-11-15 13:30:32 +00001201 if (!unroll && i_head == i->start_head) {
Al Viro27c0e372017-02-17 18:42:24 -05001202 off = 0;
1203 break;
1204 }
David Howells8cefc102019-11-15 13:30:32 +00001205 i_head--;
1206 b = &pipe->bufs[i_head & p_mask];
1207 off = b->offset + b->len;
Al Viro27c0e372017-02-17 18:42:24 -05001208 }
1209 i->iov_offset = off;
David Howells8cefc102019-11-15 13:30:32 +00001210 i->head = i_head;
Al Viro27c0e372017-02-17 18:42:24 -05001211 pipe_truncate(i);
1212 return;
1213 }
David Howells9ea9ce02018-10-20 00:57:56 +01001214 if (unlikely(iov_iter_is_discard(i)))
1215 return;
Al Viro27c0e372017-02-17 18:42:24 -05001216 if (unroll <= i->iov_offset) {
1217 i->iov_offset -= unroll;
1218 return;
1219 }
1220 unroll -= i->iov_offset;
David Howells7ff5062072020-02-10 10:00:21 +00001221 if (iov_iter_is_xarray(i)) {
1222 BUG(); /* We should never go beyond the start of the specified
1223 * range since we might then be straying into pages that
1224 * aren't pinned.
1225 */
1226 } else if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001227 const struct bio_vec *bvec = i->bvec;
1228 while (1) {
1229 size_t n = (--bvec)->bv_len;
1230 i->nr_segs++;
1231 if (unroll <= n) {
1232 i->bvec = bvec;
1233 i->iov_offset = n - unroll;
1234 return;
1235 }
1236 unroll -= n;
1237 }
1238 } else { /* same logics for iovec and kvec */
1239 const struct iovec *iov = i->iov;
1240 while (1) {
1241 size_t n = (--iov)->iov_len;
1242 i->nr_segs++;
1243 if (unroll <= n) {
1244 i->iov = iov;
1245 i->iov_offset = n - unroll;
1246 return;
1247 }
1248 unroll -= n;
1249 }
1250 }
1251}
1252EXPORT_SYMBOL(iov_iter_revert);
1253
Al Viro62a80672014-04-04 23:12:29 -04001254/*
1255 * Return the count of just the current iov_iter segment.
1256 */
1257size_t iov_iter_single_seg_count(const struct iov_iter *i)
1258{
David Howells00e23702018-10-22 13:07:28 +01001259 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001260 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001261 if (i->nr_segs == 1)
1262 return i->count;
David Howells7ff5062072020-02-10 10:00:21 +00001263 if (unlikely(iov_iter_is_discard(i) || iov_iter_is_xarray(i)))
David Howells9ea9ce02018-10-20 00:57:56 +01001264 return i->count;
David Howells7ff5062072020-02-10 10:00:21 +00001265 if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001266 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001267 else
1268 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001269}
1270EXPORT_SYMBOL(iov_iter_single_seg_count);
1271
David Howellsaa563d72018-10-20 00:57:56 +01001272void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001273 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001274 size_t count)
1275{
David Howellsaa563d72018-10-20 00:57:56 +01001276 WARN_ON(direction & ~(READ | WRITE));
1277 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001278 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001279 i->nr_segs = nr_segs;
1280 i->iov_offset = 0;
1281 i->count = count;
1282}
1283EXPORT_SYMBOL(iov_iter_kvec);
1284
David Howellsaa563d72018-10-20 00:57:56 +01001285void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001286 const struct bio_vec *bvec, unsigned long nr_segs,
1287 size_t count)
1288{
David Howellsaa563d72018-10-20 00:57:56 +01001289 WARN_ON(direction & ~(READ | WRITE));
1290 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001291 i->bvec = bvec;
1292 i->nr_segs = nr_segs;
1293 i->iov_offset = 0;
1294 i->count = count;
1295}
1296EXPORT_SYMBOL(iov_iter_bvec);
1297
David Howellsaa563d72018-10-20 00:57:56 +01001298void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001299 struct pipe_inode_info *pipe,
1300 size_t count)
1301{
David Howellsaa563d72018-10-20 00:57:56 +01001302 BUG_ON(direction != READ);
David Howells8cefc102019-11-15 13:30:32 +00001303 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
David Howellsaa563d72018-10-20 00:57:56 +01001304 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001305 i->pipe = pipe;
David Howells8cefc102019-11-15 13:30:32 +00001306 i->head = pipe->head;
Al Viro241699c2016-09-22 16:33:12 -04001307 i->iov_offset = 0;
1308 i->count = count;
David Howells8cefc102019-11-15 13:30:32 +00001309 i->start_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -04001310}
1311EXPORT_SYMBOL(iov_iter_pipe);
1312
David Howells9ea9ce02018-10-20 00:57:56 +01001313/**
David Howells7ff5062072020-02-10 10:00:21 +00001314 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1315 * @i: The iterator to initialise.
1316 * @direction: The direction of the transfer.
1317 * @xarray: The xarray to access.
1318 * @start: The start file position.
1319 * @count: The size of the I/O buffer in bytes.
1320 *
1321 * Set up an I/O iterator to either draw data out of the pages attached to an
1322 * inode or to inject data into those pages. The pages *must* be prevented
1323 * from evaporation, either by taking a ref on them or locking them by the
1324 * caller.
1325 */
1326void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1327 struct xarray *xarray, loff_t start, size_t count)
1328{
1329 BUG_ON(direction & ~1);
1330 i->type = ITER_XARRAY | (direction & (READ | WRITE));
1331 i->xarray = xarray;
1332 i->xarray_start = start;
1333 i->count = count;
1334 i->iov_offset = 0;
1335}
1336EXPORT_SYMBOL(iov_iter_xarray);
1337
1338/**
David Howells9ea9ce02018-10-20 00:57:56 +01001339 * iov_iter_discard - Initialise an I/O iterator that discards data
1340 * @i: The iterator to initialise.
1341 * @direction: The direction of the transfer.
1342 * @count: The size of the I/O buffer in bytes.
1343 *
1344 * Set up an I/O iterator that just discards everything that's written to it.
1345 * It's only available as a READ iterator.
1346 */
1347void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1348{
1349 BUG_ON(direction != READ);
1350 i->type = ITER_DISCARD | READ;
1351 i->count = count;
1352 i->iov_offset = 0;
1353}
1354EXPORT_SYMBOL(iov_iter_discard);
1355
Al Viro62a80672014-04-04 23:12:29 -04001356unsigned long iov_iter_alignment(const struct iov_iter *i)
1357{
Al Viro04a31162014-11-27 13:51:41 -05001358 unsigned long res = 0;
1359 size_t size = i->count;
1360
David Howells00e23702018-10-22 13:07:28 +01001361 if (unlikely(iov_iter_is_pipe(i))) {
Jan Karae0ff1262019-12-16 11:54:32 +01001362 unsigned int p_mask = i->pipe->ring_size - 1;
1363
David Howells8cefc102019-11-15 13:30:32 +00001364 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
Al Viro241699c2016-09-22 16:33:12 -04001365 return size | i->iov_offset;
1366 return size;
1367 }
David Howells3d14ec12021-04-25 22:02:38 +01001368 if (unlikely(iov_iter_is_xarray(i)))
1369 return (i->xarray_start + i->iov_offset) | i->count;
Al Viro04a31162014-11-27 13:51:41 -05001370 iterate_all_kinds(i, size, v,
1371 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001372 res |= v.bv_offset | v.bv_len,
David Howells7ff5062072020-02-10 10:00:21 +00001373 res |= (unsigned long)v.iov_base | v.iov_len,
1374 res |= v.bv_offset | v.bv_len
Al Viro04a31162014-11-27 13:51:41 -05001375 )
1376 return res;
Al Viro62a80672014-04-04 23:12:29 -04001377}
1378EXPORT_SYMBOL(iov_iter_alignment);
1379
Al Viro357f4352016-04-08 19:05:19 -04001380unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1381{
Al Viro33844e62016-12-21 21:55:02 -05001382 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001383 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001384
David Howells9ea9ce02018-10-20 00:57:56 +01001385 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001386 WARN_ON(1);
1387 return ~0U;
1388 }
1389
Al Viro357f4352016-04-08 19:05:19 -04001390 iterate_all_kinds(i, size, v,
1391 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1392 (size != v.iov_len ? size : 0), 0),
1393 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1394 (size != v.bv_len ? size : 0)),
1395 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
David Howells7ff5062072020-02-10 10:00:21 +00001396 (size != v.iov_len ? size : 0)),
1397 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1398 (size != v.bv_len ? size : 0))
Al Viro357f4352016-04-08 19:05:19 -04001399 );
Al Viro33844e62016-12-21 21:55:02 -05001400 return res;
Al Viro357f4352016-04-08 19:05:19 -04001401}
1402EXPORT_SYMBOL(iov_iter_gap_alignment);
1403
Ilya Dryomove76b63122018-05-02 20:16:56 +02001404static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001405 size_t maxsize,
1406 struct page **pages,
David Howells8cefc102019-11-15 13:30:32 +00001407 int iter_head,
Al Viro241699c2016-09-22 16:33:12 -04001408 size_t *start)
1409{
1410 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001411 unsigned int p_mask = pipe->ring_size - 1;
1412 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001413 if (!n)
1414 return -EFAULT;
1415
1416 maxsize = n;
1417 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001418 while (n > 0) {
David Howells8cefc102019-11-15 13:30:32 +00001419 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1420 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -04001421 n -= PAGE_SIZE;
1422 }
1423
1424 return maxsize;
1425}
1426
1427static ssize_t pipe_get_pages(struct iov_iter *i,
1428 struct page **pages, size_t maxsize, unsigned maxpages,
1429 size_t *start)
1430{
David Howells8cefc102019-11-15 13:30:32 +00001431 unsigned int iter_head, npages;
Al Viro241699c2016-09-22 16:33:12 -04001432 size_t capacity;
Al Viro241699c2016-09-22 16:33:12 -04001433
Al Viro33844e62016-12-21 21:55:02 -05001434 if (!maxsize)
1435 return 0;
1436
Al Viro241699c2016-09-22 16:33:12 -04001437 if (!sanity(i))
1438 return -EFAULT;
1439
David Howells8cefc102019-11-15 13:30:32 +00001440 data_start(i, &iter_head, start);
1441 /* Amount of free space: some of this one + all after this one */
1442 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1443 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
Al Viro241699c2016-09-22 16:33:12 -04001444
David Howells8cefc102019-11-15 13:30:32 +00001445 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001446}
1447
David Howells7ff5062072020-02-10 10:00:21 +00001448static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1449 pgoff_t index, unsigned int nr_pages)
1450{
1451 XA_STATE(xas, xa, index);
1452 struct page *page;
1453 unsigned int ret = 0;
1454
1455 rcu_read_lock();
1456 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1457 if (xas_retry(&xas, page))
1458 continue;
1459
1460 /* Has the page moved or been split? */
1461 if (unlikely(page != xas_reload(&xas))) {
1462 xas_reset(&xas);
1463 continue;
1464 }
1465
1466 pages[ret] = find_subpage(page, xas.xa_index);
1467 get_page(pages[ret]);
1468 if (++ret == nr_pages)
1469 break;
1470 }
1471 rcu_read_unlock();
1472 return ret;
1473}
1474
1475static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1476 struct page **pages, size_t maxsize,
1477 unsigned maxpages, size_t *_start_offset)
1478{
1479 unsigned nr, offset;
1480 pgoff_t index, count;
1481 size_t size = maxsize, actual;
1482 loff_t pos;
1483
1484 if (!size || !maxpages)
1485 return 0;
1486
1487 pos = i->xarray_start + i->iov_offset;
1488 index = pos >> PAGE_SHIFT;
1489 offset = pos & ~PAGE_MASK;
1490 *_start_offset = offset;
1491
1492 count = 1;
1493 if (size > PAGE_SIZE - offset) {
1494 size -= PAGE_SIZE - offset;
1495 count += size >> PAGE_SHIFT;
1496 size &= ~PAGE_MASK;
1497 if (size)
1498 count++;
1499 }
1500
1501 if (count > maxpages)
1502 count = maxpages;
1503
1504 nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
1505 if (nr == 0)
1506 return 0;
1507
1508 actual = PAGE_SIZE * nr;
1509 actual -= offset;
1510 if (nr == count && size > 0) {
1511 unsigned last_offset = (nr > 1) ? 0 : offset;
1512 actual -= PAGE_SIZE - (last_offset + size);
1513 }
1514 return actual;
1515}
1516
Al Viro62a80672014-04-04 23:12:29 -04001517ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001518 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001519 size_t *start)
1520{
Al Viroe5393fa2014-11-27 14:12:09 -05001521 if (maxsize > i->count)
1522 maxsize = i->count;
1523
David Howells00e23702018-10-22 13:07:28 +01001524 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001525 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells7ff5062072020-02-10 10:00:21 +00001526 if (unlikely(iov_iter_is_xarray(i)))
1527 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001528 if (unlikely(iov_iter_is_discard(i)))
1529 return -EFAULT;
1530
Al Viroe5393fa2014-11-27 14:12:09 -05001531 iterate_all_kinds(i, maxsize, v, ({
1532 unsigned long addr = (unsigned long)v.iov_base;
1533 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1534 int n;
1535 int res;
1536
1537 if (len > maxpages * PAGE_SIZE)
1538 len = maxpages * PAGE_SIZE;
1539 addr &= ~(PAGE_SIZE - 1);
1540 n = DIV_ROUND_UP(len, PAGE_SIZE);
Ira Weiny73b01402019-05-13 17:17:11 -07001541 res = get_user_pages_fast(addr, n,
1542 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1543 pages);
Al Viroe5393fa2014-11-27 14:12:09 -05001544 if (unlikely(res < 0))
1545 return res;
1546 return (res == n ? len : res * PAGE_SIZE) - *start;
1547 0;}),({
1548 /* can't be more than PAGE_SIZE */
1549 *start = v.bv_offset;
1550 get_page(*pages = v.bv_page);
1551 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001552 }),({
1553 return -EFAULT;
David Howells7ff5062072020-02-10 10:00:21 +00001554 }),
1555 0
Al Viroe5393fa2014-11-27 14:12:09 -05001556 )
1557 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001558}
1559EXPORT_SYMBOL(iov_iter_get_pages);
1560
Al Viro1b17f1f2014-11-27 14:14:31 -05001561static struct page **get_pages_array(size_t n)
1562{
Michal Hocko752ade62017-05-08 15:57:27 -07001563 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001564}
1565
Al Viro241699c2016-09-22 16:33:12 -04001566static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1567 struct page ***pages, size_t maxsize,
1568 size_t *start)
1569{
1570 struct page **p;
David Howells8cefc102019-11-15 13:30:32 +00001571 unsigned int iter_head, npages;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001572 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001573
Al Viro33844e62016-12-21 21:55:02 -05001574 if (!maxsize)
1575 return 0;
1576
Al Viro241699c2016-09-22 16:33:12 -04001577 if (!sanity(i))
1578 return -EFAULT;
1579
David Howells8cefc102019-11-15 13:30:32 +00001580 data_start(i, &iter_head, start);
1581 /* Amount of free space: some of this one + all after this one */
1582 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
Al Viro241699c2016-09-22 16:33:12 -04001583 n = npages * PAGE_SIZE - *start;
1584 if (maxsize > n)
1585 maxsize = n;
1586 else
1587 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1588 p = get_pages_array(npages);
1589 if (!p)
1590 return -ENOMEM;
David Howells8cefc102019-11-15 13:30:32 +00001591 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001592 if (n > 0)
1593 *pages = p;
1594 else
1595 kvfree(p);
1596 return n;
1597}
1598
David Howells7ff5062072020-02-10 10:00:21 +00001599static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
1600 struct page ***pages, size_t maxsize,
1601 size_t *_start_offset)
1602{
1603 struct page **p;
1604 unsigned nr, offset;
1605 pgoff_t index, count;
1606 size_t size = maxsize, actual;
1607 loff_t pos;
1608
1609 if (!size)
1610 return 0;
1611
1612 pos = i->xarray_start + i->iov_offset;
1613 index = pos >> PAGE_SHIFT;
1614 offset = pos & ~PAGE_MASK;
1615 *_start_offset = offset;
1616
1617 count = 1;
1618 if (size > PAGE_SIZE - offset) {
1619 size -= PAGE_SIZE - offset;
1620 count += size >> PAGE_SHIFT;
1621 size &= ~PAGE_MASK;
1622 if (size)
1623 count++;
1624 }
1625
1626 p = get_pages_array(count);
1627 if (!p)
1628 return -ENOMEM;
1629 *pages = p;
1630
1631 nr = iter_xarray_populate_pages(p, i->xarray, index, count);
1632 if (nr == 0)
1633 return 0;
1634
1635 actual = PAGE_SIZE * nr;
1636 actual -= offset;
1637 if (nr == count && size > 0) {
1638 unsigned last_offset = (nr > 1) ? 0 : offset;
1639 actual -= PAGE_SIZE - (last_offset + size);
1640 }
1641 return actual;
1642}
1643
Al Viro62a80672014-04-04 23:12:29 -04001644ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1645 struct page ***pages, size_t maxsize,
1646 size_t *start)
1647{
Al Viro1b17f1f2014-11-27 14:14:31 -05001648 struct page **p;
1649
1650 if (maxsize > i->count)
1651 maxsize = i->count;
1652
David Howells00e23702018-10-22 13:07:28 +01001653 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001654 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells7ff5062072020-02-10 10:00:21 +00001655 if (unlikely(iov_iter_is_xarray(i)))
1656 return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001657 if (unlikely(iov_iter_is_discard(i)))
1658 return -EFAULT;
1659
Al Viro1b17f1f2014-11-27 14:14:31 -05001660 iterate_all_kinds(i, maxsize, v, ({
1661 unsigned long addr = (unsigned long)v.iov_base;
1662 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1663 int n;
1664 int res;
1665
1666 addr &= ~(PAGE_SIZE - 1);
1667 n = DIV_ROUND_UP(len, PAGE_SIZE);
1668 p = get_pages_array(n);
1669 if (!p)
1670 return -ENOMEM;
Ira Weiny73b01402019-05-13 17:17:11 -07001671 res = get_user_pages_fast(addr, n,
1672 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
Al Viro1b17f1f2014-11-27 14:14:31 -05001673 if (unlikely(res < 0)) {
1674 kvfree(p);
1675 return res;
1676 }
1677 *pages = p;
1678 return (res == n ? len : res * PAGE_SIZE) - *start;
1679 0;}),({
1680 /* can't be more than PAGE_SIZE */
1681 *start = v.bv_offset;
1682 *pages = p = get_pages_array(1);
1683 if (!p)
1684 return -ENOMEM;
1685 get_page(*p = v.bv_page);
1686 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001687 }),({
1688 return -EFAULT;
David Howells7ff5062072020-02-10 10:00:21 +00001689 }), 0
Al Viro1b17f1f2014-11-27 14:14:31 -05001690 )
1691 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001692}
1693EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1694
Al Viroa604ec72014-11-24 01:08:00 -05001695size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1696 struct iov_iter *i)
1697{
1698 char *to = addr;
1699 __wsum sum, next;
1700 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001701 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001702 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001703 WARN_ON(1);
1704 return 0;
1705 }
Al Viroa604ec72014-11-24 01:08:00 -05001706 iterate_and_advance(i, bytes, v, ({
Al Virocbbd26b2016-11-01 22:09:04 -04001707 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001708 (to += v.iov_len) - v.iov_len,
Al Viroc693cc42020-07-11 00:27:49 -04001709 v.iov_len);
1710 if (next) {
Al Viroa604ec72014-11-24 01:08:00 -05001711 sum = csum_block_add(sum, next, off);
1712 off += v.iov_len;
1713 }
Al Viroc693cc42020-07-11 00:27:49 -04001714 next ? 0 : v.iov_len;
Al Viroa604ec72014-11-24 01:08:00 -05001715 }), ({
1716 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001717 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1718 p + v.bv_offset, v.bv_len,
1719 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001720 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001721 off += v.bv_len;
1722 }),({
Al Virof9152892018-11-27 22:32:59 -05001723 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1724 v.iov_base, v.iov_len,
1725 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001726 off += v.iov_len;
David Howells7ff5062072020-02-10 10:00:21 +00001727 }), ({
1728 char *p = kmap_atomic(v.bv_page);
1729 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1730 p + v.bv_offset, v.bv_len,
1731 sum, off);
1732 kunmap_atomic(p);
1733 off += v.bv_len;
Al Viroa604ec72014-11-24 01:08:00 -05001734 })
1735 )
1736 *csum = sum;
1737 return bytes;
1738}
1739EXPORT_SYMBOL(csum_and_copy_from_iter);
1740
Al Virocbbd26b2016-11-01 22:09:04 -04001741bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1742 struct iov_iter *i)
1743{
1744 char *to = addr;
1745 __wsum sum, next;
1746 size_t off = 0;
1747 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001748 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001749 WARN_ON(1);
1750 return false;
1751 }
1752 if (unlikely(i->count < bytes))
1753 return false;
1754 iterate_all_kinds(i, bytes, v, ({
Al Virocbbd26b2016-11-01 22:09:04 -04001755 next = csum_and_copy_from_user(v.iov_base,
1756 (to += v.iov_len) - v.iov_len,
Al Viroc693cc42020-07-11 00:27:49 -04001757 v.iov_len);
1758 if (!next)
Al Virocbbd26b2016-11-01 22:09:04 -04001759 return false;
1760 sum = csum_block_add(sum, next, off);
1761 off += v.iov_len;
1762 0;
1763 }), ({
1764 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001765 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1766 p + v.bv_offset, v.bv_len,
1767 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001768 kunmap_atomic(p);
Al Virocbbd26b2016-11-01 22:09:04 -04001769 off += v.bv_len;
1770 }),({
Al Virof9152892018-11-27 22:32:59 -05001771 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1772 v.iov_base, v.iov_len,
1773 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001774 off += v.iov_len;
David Howells7ff5062072020-02-10 10:00:21 +00001775 }), ({
1776 char *p = kmap_atomic(v.bv_page);
1777 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1778 p + v.bv_offset, v.bv_len,
1779 sum, off);
1780 kunmap_atomic(p);
1781 off += v.bv_len;
Al Virocbbd26b2016-11-01 22:09:04 -04001782 })
1783 )
1784 *csum = sum;
1785 iov_iter_advance(i, bytes);
1786 return true;
1787}
1788EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1789
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001790size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
Al Viroa604ec72014-11-24 01:08:00 -05001791 struct iov_iter *i)
1792{
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001793 struct csum_state *csstate = _csstate;
Al Viro36f7a8a2015-12-06 16:49:22 -05001794 const char *from = addr;
Al Viroa604ec72014-11-24 01:08:00 -05001795 __wsum sum, next;
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001796 size_t off;
Al Viro78e1f382018-11-25 16:24:16 -05001797
1798 if (unlikely(iov_iter_is_pipe(i)))
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001799 return csum_and_copy_to_pipe_iter(addr, bytes, _csstate, i);
Al Viro78e1f382018-11-25 16:24:16 -05001800
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001801 sum = csstate->csum;
1802 off = csstate->off;
Al Viro78e1f382018-11-25 16:24:16 -05001803 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001804 WARN_ON(1); /* for now */
1805 return 0;
1806 }
Al Viroa604ec72014-11-24 01:08:00 -05001807 iterate_and_advance(i, bytes, v, ({
Al Viroa604ec72014-11-24 01:08:00 -05001808 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001809 v.iov_base,
Al Viroc693cc42020-07-11 00:27:49 -04001810 v.iov_len);
1811 if (next) {
Al Viroa604ec72014-11-24 01:08:00 -05001812 sum = csum_block_add(sum, next, off);
1813 off += v.iov_len;
1814 }
Al Viroc693cc42020-07-11 00:27:49 -04001815 next ? 0 : v.iov_len;
Al Viroa604ec72014-11-24 01:08:00 -05001816 }), ({
1817 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001818 sum = csum_and_memcpy(p + v.bv_offset,
1819 (from += v.bv_len) - v.bv_len,
1820 v.bv_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001821 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001822 off += v.bv_len;
1823 }),({
Al Virof9152892018-11-27 22:32:59 -05001824 sum = csum_and_memcpy(v.iov_base,
1825 (from += v.iov_len) - v.iov_len,
1826 v.iov_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001827 off += v.iov_len;
David Howells7ff5062072020-02-10 10:00:21 +00001828 }), ({
1829 char *p = kmap_atomic(v.bv_page);
1830 sum = csum_and_memcpy(p + v.bv_offset,
1831 (from += v.bv_len) - v.bv_len,
1832 v.bv_len, sum, off);
1833 kunmap_atomic(p);
1834 off += v.bv_len;
Al Viroa604ec72014-11-24 01:08:00 -05001835 })
1836 )
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001837 csstate->csum = sum;
1838 csstate->off = off;
Al Viroa604ec72014-11-24 01:08:00 -05001839 return bytes;
1840}
1841EXPORT_SYMBOL(csum_and_copy_to_iter);
1842
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001843size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1844 struct iov_iter *i)
1845{
Herbert Xu79990962020-06-12 16:57:37 +10001846#ifdef CONFIG_CRYPTO_HASH
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001847 struct ahash_request *hash = hashp;
1848 struct scatterlist sg;
1849 size_t copied;
1850
1851 copied = copy_to_iter(addr, bytes, i);
1852 sg_init_one(&sg, addr, copied);
1853 ahash_request_set_crypt(hash, &sg, NULL, copied);
1854 crypto_ahash_update(hash);
1855 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001856#else
1857 return 0;
1858#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001859}
1860EXPORT_SYMBOL(hash_and_copy_to_iter);
1861
Al Viro62a80672014-04-04 23:12:29 -04001862int iov_iter_npages(const struct iov_iter *i, int maxpages)
1863{
Al Viroe0f2dc42014-11-27 14:09:46 -05001864 size_t size = i->count;
1865 int npages = 0;
1866
1867 if (!size)
1868 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001869 if (unlikely(iov_iter_is_discard(i)))
1870 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001871
David Howells00e23702018-10-22 13:07:28 +01001872 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001873 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001874 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -04001875 size_t off;
Al Viro241699c2016-09-22 16:33:12 -04001876
1877 if (!sanity(i))
1878 return 0;
1879
David Howells8cefc102019-11-15 13:30:32 +00001880 data_start(i, &iter_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001881 /* some of this one + all after this one */
David Howells8cefc102019-11-15 13:30:32 +00001882 npages = pipe_space_for_user(iter_head, pipe->tail, pipe);
Al Viro241699c2016-09-22 16:33:12 -04001883 if (npages >= maxpages)
1884 return maxpages;
David Howells7ff5062072020-02-10 10:00:21 +00001885 } else if (unlikely(iov_iter_is_xarray(i))) {
1886 unsigned offset;
1887
1888 offset = (i->xarray_start + i->iov_offset) & ~PAGE_MASK;
1889
1890 npages = 1;
1891 if (size > PAGE_SIZE - offset) {
1892 size -= PAGE_SIZE - offset;
1893 npages += size >> PAGE_SHIFT;
1894 size &= ~PAGE_MASK;
1895 if (size)
1896 npages++;
1897 }
1898 if (npages >= maxpages)
1899 return maxpages;
Al Viro241699c2016-09-22 16:33:12 -04001900 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001901 unsigned long p = (unsigned long)v.iov_base;
1902 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1903 - p / PAGE_SIZE;
1904 if (npages >= maxpages)
1905 return maxpages;
1906 0;}),({
1907 npages++;
1908 if (npages >= maxpages)
1909 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001910 }),({
1911 unsigned long p = (unsigned long)v.iov_base;
1912 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1913 - p / PAGE_SIZE;
1914 if (npages >= maxpages)
1915 return maxpages;
David Howells7ff5062072020-02-10 10:00:21 +00001916 }),
1917 0
Al Viroe0f2dc42014-11-27 14:09:46 -05001918 )
1919 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001920}
Al Virof67da302014-03-19 01:16:16 -04001921EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001922
1923const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1924{
1925 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001926 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001927 WARN_ON(1);
1928 return NULL;
1929 }
David Howells7ff5062072020-02-10 10:00:21 +00001930 if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
David Howells9ea9ce02018-10-20 00:57:56 +01001931 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001932 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001933 return new->bvec = kmemdup(new->bvec,
1934 new->nr_segs * sizeof(struct bio_vec),
1935 flags);
1936 else
1937 /* iovec and kvec have identical layout */
1938 return new->iov = kmemdup(new->iov,
1939 new->nr_segs * sizeof(struct iovec),
1940 flags);
1941}
1942EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001943
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001944static int copy_compat_iovec_from_user(struct iovec *iov,
1945 const struct iovec __user *uvec, unsigned long nr_segs)
1946{
1947 const struct compat_iovec __user *uiov =
1948 (const struct compat_iovec __user *)uvec;
1949 int ret = -EFAULT, i;
1950
Christoph Hellwiga959a972021-01-11 18:19:26 +01001951 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001952 return -EFAULT;
1953
1954 for (i = 0; i < nr_segs; i++) {
1955 compat_uptr_t buf;
1956 compat_ssize_t len;
1957
1958 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1959 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1960
1961 /* check for compat_size_t not fitting in compat_ssize_t .. */
1962 if (len < 0) {
1963 ret = -EINVAL;
1964 goto uaccess_end;
1965 }
1966 iov[i].iov_base = compat_ptr(buf);
1967 iov[i].iov_len = len;
1968 }
1969
1970 ret = 0;
1971uaccess_end:
1972 user_access_end();
1973 return ret;
1974}
1975
1976static int copy_iovec_from_user(struct iovec *iov,
1977 const struct iovec __user *uvec, unsigned long nr_segs)
David Laightfb041b52020-09-25 06:51:39 +02001978{
1979 unsigned long seg;
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001980
1981 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1982 return -EFAULT;
1983 for (seg = 0; seg < nr_segs; seg++) {
1984 if ((ssize_t)iov[seg].iov_len < 0)
1985 return -EINVAL;
1986 }
1987
1988 return 0;
1989}
1990
1991struct iovec *iovec_from_user(const struct iovec __user *uvec,
1992 unsigned long nr_segs, unsigned long fast_segs,
1993 struct iovec *fast_iov, bool compat)
1994{
1995 struct iovec *iov = fast_iov;
1996 int ret;
David Laightfb041b52020-09-25 06:51:39 +02001997
1998 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001999 * SuS says "The readv() function *may* fail if the iovcnt argument was
2000 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
David Laightfb041b52020-09-25 06:51:39 +02002001 * traditionally returned zero for zero segments, so...
2002 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002003 if (nr_segs == 0)
2004 return iov;
2005 if (nr_segs > UIO_MAXIOV)
2006 return ERR_PTR(-EINVAL);
David Laightfb041b52020-09-25 06:51:39 +02002007 if (nr_segs > fast_segs) {
2008 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002009 if (!iov)
2010 return ERR_PTR(-ENOMEM);
David Laightfb041b52020-09-25 06:51:39 +02002011 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002012
2013 if (compat)
2014 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
2015 else
2016 ret = copy_iovec_from_user(iov, uvec, nr_segs);
2017 if (ret) {
2018 if (iov != fast_iov)
2019 kfree(iov);
2020 return ERR_PTR(ret);
2021 }
2022
2023 return iov;
2024}
2025
2026ssize_t __import_iovec(int type, const struct iovec __user *uvec,
2027 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
2028 struct iov_iter *i, bool compat)
2029{
2030 ssize_t total_len = 0;
2031 unsigned long seg;
2032 struct iovec *iov;
2033
2034 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
2035 if (IS_ERR(iov)) {
2036 *iovp = NULL;
2037 return PTR_ERR(iov);
David Laightfb041b52020-09-25 06:51:39 +02002038 }
2039
2040 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002041 * According to the Single Unix Specification we should return EINVAL if
2042 * an element length is < 0 when cast to ssize_t or if the total length
2043 * would overflow the ssize_t return value of the system call.
David Laightfb041b52020-09-25 06:51:39 +02002044 *
2045 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
2046 * overflow case.
2047 */
David Laightfb041b52020-09-25 06:51:39 +02002048 for (seg = 0; seg < nr_segs; seg++) {
David Laightfb041b52020-09-25 06:51:39 +02002049 ssize_t len = (ssize_t)iov[seg].iov_len;
2050
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002051 if (!access_ok(iov[seg].iov_base, len)) {
2052 if (iov != *iovp)
2053 kfree(iov);
2054 *iovp = NULL;
2055 return -EFAULT;
David Laightfb041b52020-09-25 06:51:39 +02002056 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002057
2058 if (len > MAX_RW_COUNT - total_len) {
2059 len = MAX_RW_COUNT - total_len;
David Laightfb041b52020-09-25 06:51:39 +02002060 iov[seg].iov_len = len;
2061 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002062 total_len += len;
David Laightfb041b52020-09-25 06:51:39 +02002063 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002064
2065 iov_iter_init(i, type, iov, nr_segs, total_len);
2066 if (iov == *iovp)
2067 *iovp = NULL;
2068 else
2069 *iovp = iov;
2070 return total_len;
David Laightfb041b52020-09-25 06:51:39 +02002071}
2072
Vegard Nossumffecee42016-10-08 11:18:07 +02002073/**
2074 * import_iovec() - Copy an array of &struct iovec from userspace
2075 * into the kernel, check that it is valid, and initialize a new
2076 * &struct iov_iter iterator to access it.
2077 *
2078 * @type: One of %READ or %WRITE.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002079 * @uvec: Pointer to the userspace array.
Vegard Nossumffecee42016-10-08 11:18:07 +02002080 * @nr_segs: Number of elements in userspace array.
2081 * @fast_segs: Number of elements in @iov.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002082 * @iovp: (input and output parameter) Pointer to pointer to (usually small
Vegard Nossumffecee42016-10-08 11:18:07 +02002083 * on-stack) kernel array.
2084 * @i: Pointer to iterator that will be initialized on success.
2085 *
2086 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
2087 * then this function places %NULL in *@iov on return. Otherwise, a new
2088 * array will be allocated and the result placed in *@iov. This means that
2089 * the caller may call kfree() on *@iov regardless of whether the small
2090 * on-stack array was used or not (and regardless of whether this function
2091 * returns an error or not).
2092 *
Jens Axboe87e5e6d2019-05-14 16:02:22 -06002093 * Return: Negative error code on error, bytes imported on success
Vegard Nossumffecee42016-10-08 11:18:07 +02002094 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002095ssize_t import_iovec(int type, const struct iovec __user *uvec,
Al Virobc917be2015-03-21 17:45:43 -04002096 unsigned nr_segs, unsigned fast_segs,
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002097 struct iovec **iovp, struct iov_iter *i)
Al Virobc917be2015-03-21 17:45:43 -04002098{
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02002099 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
2100 in_compat_syscall());
Al Virobc917be2015-03-21 17:45:43 -04002101}
2102EXPORT_SYMBOL(import_iovec);
2103
Al Virobc917be2015-03-21 17:45:43 -04002104int import_single_range(int rw, void __user *buf, size_t len,
2105 struct iovec *iov, struct iov_iter *i)
2106{
2107 if (len > MAX_RW_COUNT)
2108 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002109 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04002110 return -EFAULT;
2111
2112 iov->iov_base = buf;
2113 iov->iov_len = len;
2114 iov_iter_init(i, rw, iov, 1, len);
2115 return 0;
2116}
Al Viroe1267582015-12-06 20:38:56 -05002117EXPORT_SYMBOL(import_single_range);