blob: f99c41d4eb545e395dd9b7256b92919ff9b3a5c3 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Al Viro4f18cd32014-02-05 19:11:33 -05002#include <linux/export.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -06003#include <linux/bvec.h>
Al Viro4f18cd32014-02-05 19:11:33 -05004#include <linux/uio.h>
5#include <linux/pagemap.h>
Al Viro91f79c42014-03-21 04:58:33 -04006#include <linux/slab.h>
7#include <linux/vmalloc.h>
Al Viro241699c2016-09-22 16:33:12 -04008#include <linux/splice.h>
Al Viroa604ec72014-11-24 01:08:00 -05009#include <net/checksum.h>
Sagi Grimbergd05f4432018-12-03 17:52:09 -080010#include <linux/scatterlist.h>
Al Viro4f18cd32014-02-05 19:11:33 -050011
Al Viro241699c2016-09-22 16:33:12 -040012#define PIPE_PARANOIA /* for now */
13
Al Viro04a31162014-11-27 13:51:41 -050014#define iterate_iovec(i, n, __v, __p, skip, STEP) { \
15 size_t left; \
16 size_t wanted = n; \
17 __p = i->iov; \
18 __v.iov_len = min(n, __p->iov_len - skip); \
19 if (likely(__v.iov_len)) { \
20 __v.iov_base = __p->iov_base + skip; \
21 left = (STEP); \
22 __v.iov_len -= left; \
23 skip += __v.iov_len; \
24 n -= __v.iov_len; \
25 } else { \
26 left = 0; \
27 } \
28 while (unlikely(!left && n)) { \
29 __p++; \
30 __v.iov_len = min(n, __p->iov_len); \
31 if (unlikely(!__v.iov_len)) \
32 continue; \
33 __v.iov_base = __p->iov_base; \
34 left = (STEP); \
35 __v.iov_len -= left; \
36 skip = __v.iov_len; \
37 n -= __v.iov_len; \
38 } \
39 n = wanted - n; \
40}
41
Al Viroa2804552014-11-27 14:48:42 -050042#define iterate_kvec(i, n, __v, __p, skip, STEP) { \
43 size_t wanted = n; \
44 __p = i->kvec; \
45 __v.iov_len = min(n, __p->iov_len - skip); \
46 if (likely(__v.iov_len)) { \
47 __v.iov_base = __p->iov_base + skip; \
48 (void)(STEP); \
49 skip += __v.iov_len; \
50 n -= __v.iov_len; \
51 } \
52 while (unlikely(n)) { \
53 __p++; \
54 __v.iov_len = min(n, __p->iov_len); \
55 if (unlikely(!__v.iov_len)) \
56 continue; \
57 __v.iov_base = __p->iov_base; \
58 (void)(STEP); \
59 skip = __v.iov_len; \
60 n -= __v.iov_len; \
61 } \
62 n = wanted; \
63}
64
Ming Lei1bdc76a2016-05-30 21:34:32 +080065#define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
66 struct bvec_iter __start; \
67 __start.bi_size = n; \
68 __start.bi_bvec_done = skip; \
69 __start.bi_idx = 0; \
70 for_each_bvec(__v, i->bvec, __bi, __start) { \
71 if (!__v.bv_len) \
Al Viro04a31162014-11-27 13:51:41 -050072 continue; \
Al Viro04a31162014-11-27 13:51:41 -050073 (void)(STEP); \
Al Viro04a31162014-11-27 13:51:41 -050074 } \
Al Viro04a31162014-11-27 13:51:41 -050075}
76
Al Viroa2804552014-11-27 14:48:42 -050077#define iterate_all_kinds(i, n, v, I, B, K) { \
Al Viro33844e62016-12-21 21:55:02 -050078 if (likely(n)) { \
79 size_t skip = i->iov_offset; \
80 if (unlikely(i->type & ITER_BVEC)) { \
81 struct bio_vec v; \
82 struct bvec_iter __bi; \
83 iterate_bvec(i, n, v, __bi, skip, (B)) \
84 } else if (unlikely(i->type & ITER_KVEC)) { \
85 const struct kvec *kvec; \
86 struct kvec v; \
87 iterate_kvec(i, n, v, kvec, skip, (K)) \
David Howells9ea9ce02018-10-20 00:57:56 +010088 } else if (unlikely(i->type & ITER_DISCARD)) { \
Al Viro33844e62016-12-21 21:55:02 -050089 } else { \
90 const struct iovec *iov; \
91 struct iovec v; \
92 iterate_iovec(i, n, v, iov, skip, (I)) \
93 } \
Al Viro04a31162014-11-27 13:51:41 -050094 } \
95}
96
Al Viroa2804552014-11-27 14:48:42 -050097#define iterate_and_advance(i, n, v, I, B, K) { \
Al Virodd254f52016-05-09 11:54:48 -040098 if (unlikely(i->count < n)) \
99 n = i->count; \
Al Viro19f18452016-05-25 17:36:19 -0400100 if (i->count) { \
Al Virodd254f52016-05-09 11:54:48 -0400101 size_t skip = i->iov_offset; \
102 if (unlikely(i->type & ITER_BVEC)) { \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800103 const struct bio_vec *bvec = i->bvec; \
Al Virodd254f52016-05-09 11:54:48 -0400104 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800105 struct bvec_iter __bi; \
106 iterate_bvec(i, n, v, __bi, skip, (B)) \
107 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
108 i->nr_segs -= i->bvec - bvec; \
109 skip = __bi.bi_bvec_done; \
Al Virodd254f52016-05-09 11:54:48 -0400110 } else if (unlikely(i->type & ITER_KVEC)) { \
111 const struct kvec *kvec; \
112 struct kvec v; \
113 iterate_kvec(i, n, v, kvec, skip, (K)) \
114 if (skip == kvec->iov_len) { \
115 kvec++; \
116 skip = 0; \
117 } \
118 i->nr_segs -= kvec - i->kvec; \
119 i->kvec = kvec; \
David Howells9ea9ce02018-10-20 00:57:56 +0100120 } else if (unlikely(i->type & ITER_DISCARD)) { \
121 skip += n; \
Al Virodd254f52016-05-09 11:54:48 -0400122 } else { \
123 const struct iovec *iov; \
124 struct iovec v; \
125 iterate_iovec(i, n, v, iov, skip, (I)) \
126 if (skip == iov->iov_len) { \
127 iov++; \
128 skip = 0; \
129 } \
130 i->nr_segs -= iov - i->iov; \
131 i->iov = iov; \
Al Viro7ce2a912014-11-27 13:59:45 -0500132 } \
Al Virodd254f52016-05-09 11:54:48 -0400133 i->count -= n; \
134 i->iov_offset = skip; \
Al Viro7ce2a912014-11-27 13:59:45 -0500135 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500136}
137
Al Viro09fc68dc2017-06-29 22:25:14 -0400138static int copyout(void __user *to, const void *from, size_t n)
139{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800140 if (access_ok(to, n)) {
Al Viro09fc68dc2017-06-29 22:25:14 -0400141 kasan_check_read(from, n);
142 n = raw_copy_to_user(to, from, n);
143 }
144 return n;
145}
146
147static int copyin(void *to, const void __user *from, size_t n)
148{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800149 if (access_ok(from, n)) {
Al Viro09fc68dc2017-06-29 22:25:14 -0400150 kasan_check_write(to, n);
151 n = raw_copy_from_user(to, from, n);
152 }
153 return n;
154}
155
Al Viro62a80672014-04-04 23:12:29 -0400156static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500157 struct iov_iter *i)
158{
159 size_t skip, copy, left, wanted;
160 const struct iovec *iov;
161 char __user *buf;
162 void *kaddr, *from;
163
164 if (unlikely(bytes > i->count))
165 bytes = i->count;
166
167 if (unlikely(!bytes))
168 return 0;
169
Al Viro09fc68dc2017-06-29 22:25:14 -0400170 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500171 wanted = bytes;
172 iov = i->iov;
173 skip = i->iov_offset;
174 buf = iov->iov_base + skip;
175 copy = min(bytes, iov->iov_len - skip);
176
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700177 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500178 kaddr = kmap_atomic(page);
179 from = kaddr + offset;
180
181 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400182 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500183 copy -= left;
184 skip += copy;
185 from += copy;
186 bytes -= copy;
187
188 while (unlikely(!left && bytes)) {
189 iov++;
190 buf = iov->iov_base;
191 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400192 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500193 copy -= left;
194 skip = copy;
195 from += copy;
196 bytes -= copy;
197 }
198 if (likely(!bytes)) {
199 kunmap_atomic(kaddr);
200 goto done;
201 }
202 offset = from - kaddr;
203 buf += copy;
204 kunmap_atomic(kaddr);
205 copy = min(bytes, iov->iov_len - skip);
206 }
207 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700208
Al Viro4f18cd32014-02-05 19:11:33 -0500209 kaddr = kmap(page);
210 from = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400211 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500212 copy -= left;
213 skip += copy;
214 from += copy;
215 bytes -= copy;
216 while (unlikely(!left && bytes)) {
217 iov++;
218 buf = iov->iov_base;
219 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400220 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500221 copy -= left;
222 skip = copy;
223 from += copy;
224 bytes -= copy;
225 }
226 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700227
Al Viro4f18cd32014-02-05 19:11:33 -0500228done:
Al Viro81055e52014-04-04 19:23:46 -0400229 if (skip == iov->iov_len) {
230 iov++;
231 skip = 0;
232 }
Al Viro4f18cd32014-02-05 19:11:33 -0500233 i->count -= wanted - bytes;
234 i->nr_segs -= iov - i->iov;
235 i->iov = iov;
236 i->iov_offset = skip;
237 return wanted - bytes;
238}
Al Viro4f18cd32014-02-05 19:11:33 -0500239
Al Viro62a80672014-04-04 23:12:29 -0400240static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400241 struct iov_iter *i)
242{
243 size_t skip, copy, left, wanted;
244 const struct iovec *iov;
245 char __user *buf;
246 void *kaddr, *to;
247
248 if (unlikely(bytes > i->count))
249 bytes = i->count;
250
251 if (unlikely(!bytes))
252 return 0;
253
Al Viro09fc68dc2017-06-29 22:25:14 -0400254 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400255 wanted = bytes;
256 iov = i->iov;
257 skip = i->iov_offset;
258 buf = iov->iov_base + skip;
259 copy = min(bytes, iov->iov_len - skip);
260
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700261 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400262 kaddr = kmap_atomic(page);
263 to = kaddr + offset;
264
265 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400266 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400267 copy -= left;
268 skip += copy;
269 to += copy;
270 bytes -= copy;
271
272 while (unlikely(!left && bytes)) {
273 iov++;
274 buf = iov->iov_base;
275 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400276 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400277 copy -= left;
278 skip = copy;
279 to += copy;
280 bytes -= copy;
281 }
282 if (likely(!bytes)) {
283 kunmap_atomic(kaddr);
284 goto done;
285 }
286 offset = to - kaddr;
287 buf += copy;
288 kunmap_atomic(kaddr);
289 copy = min(bytes, iov->iov_len - skip);
290 }
291 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700292
Al Virof0d1bec2014-04-03 15:05:18 -0400293 kaddr = kmap(page);
294 to = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400295 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400296 copy -= left;
297 skip += copy;
298 to += copy;
299 bytes -= copy;
300 while (unlikely(!left && bytes)) {
301 iov++;
302 buf = iov->iov_base;
303 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400304 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400305 copy -= left;
306 skip = copy;
307 to += copy;
308 bytes -= copy;
309 }
310 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700311
Al Virof0d1bec2014-04-03 15:05:18 -0400312done:
Al Viro81055e52014-04-04 19:23:46 -0400313 if (skip == iov->iov_len) {
314 iov++;
315 skip = 0;
316 }
Al Virof0d1bec2014-04-03 15:05:18 -0400317 i->count -= wanted - bytes;
318 i->nr_segs -= iov - i->iov;
319 i->iov = iov;
320 i->iov_offset = skip;
321 return wanted - bytes;
322}
Al Virof0d1bec2014-04-03 15:05:18 -0400323
Al Viro241699c2016-09-22 16:33:12 -0400324#ifdef PIPE_PARANOIA
325static bool sanity(const struct iov_iter *i)
326{
327 struct pipe_inode_info *pipe = i->pipe;
328 int idx = i->idx;
329 int next = pipe->curbuf + pipe->nrbufs;
330 if (i->iov_offset) {
331 struct pipe_buffer *p;
332 if (unlikely(!pipe->nrbufs))
333 goto Bad; // pipe must be non-empty
334 if (unlikely(idx != ((next - 1) & (pipe->buffers - 1))))
335 goto Bad; // must be at the last buffer...
336
337 p = &pipe->bufs[idx];
338 if (unlikely(p->offset + p->len != i->iov_offset))
339 goto Bad; // ... at the end of segment
340 } else {
341 if (idx != (next & (pipe->buffers - 1)))
342 goto Bad; // must be right after the last buffer
343 }
344 return true;
345Bad:
346 printk(KERN_ERR "idx = %d, offset = %zd\n", i->idx, i->iov_offset);
347 printk(KERN_ERR "curbuf = %d, nrbufs = %d, buffers = %d\n",
348 pipe->curbuf, pipe->nrbufs, pipe->buffers);
349 for (idx = 0; idx < pipe->buffers; idx++)
350 printk(KERN_ERR "[%p %p %d %d]\n",
351 pipe->bufs[idx].ops,
352 pipe->bufs[idx].page,
353 pipe->bufs[idx].offset,
354 pipe->bufs[idx].len);
355 WARN_ON(1);
356 return false;
357}
358#else
359#define sanity(i) true
360#endif
361
362static inline int next_idx(int idx, struct pipe_inode_info *pipe)
363{
364 return (idx + 1) & (pipe->buffers - 1);
365}
366
367static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
368 struct iov_iter *i)
369{
370 struct pipe_inode_info *pipe = i->pipe;
371 struct pipe_buffer *buf;
372 size_t off;
373 int idx;
374
375 if (unlikely(bytes > i->count))
376 bytes = i->count;
377
378 if (unlikely(!bytes))
379 return 0;
380
381 if (!sanity(i))
382 return 0;
383
384 off = i->iov_offset;
385 idx = i->idx;
386 buf = &pipe->bufs[idx];
387 if (off) {
388 if (offset == off && buf->page == page) {
389 /* merge with the last one */
390 buf->len += bytes;
391 i->iov_offset += bytes;
392 goto out;
393 }
394 idx = next_idx(idx, pipe);
395 buf = &pipe->bufs[idx];
396 }
397 if (idx == pipe->curbuf && pipe->nrbufs)
398 return 0;
399 pipe->nrbufs++;
400 buf->ops = &page_cache_pipe_buf_ops;
401 get_page(buf->page = page);
402 buf->offset = offset;
403 buf->len = bytes;
404 i->iov_offset = offset + bytes;
405 i->idx = idx;
406out:
407 i->count -= bytes;
408 return bytes;
409}
410
Al Viro4f18cd32014-02-05 19:11:33 -0500411/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400412 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
413 * bytes. For each iovec, fault in each page that constitutes the iovec.
414 *
415 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
416 * because it is an invalid address).
417 */
Al Virod4690f12016-09-16 00:11:45 +0100418int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400419{
420 size_t skip = i->iov_offset;
421 const struct iovec *iov;
422 int err;
423 struct iovec v;
424
425 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
426 iterate_iovec(i, bytes, v, iov, skip, ({
Al Viro4bce9f62016-09-17 18:02:44 -0400427 err = fault_in_pages_readable(v.iov_base, v.iov_len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400428 if (unlikely(err))
429 return err;
430 0;}))
431 }
432 return 0;
433}
Al Virod4690f12016-09-16 00:11:45 +0100434EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400435
David Howellsaa563d72018-10-20 00:57:56 +0100436void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500437 const struct iovec *iov, unsigned long nr_segs,
438 size_t count)
439{
David Howellsaa563d72018-10-20 00:57:56 +0100440 WARN_ON(direction & ~(READ | WRITE));
441 direction &= READ | WRITE;
442
Al Viro71d8e532014-03-05 19:28:09 -0500443 /* It will get better. Eventually... */
Al Virodb68ce12017-03-20 21:08:07 -0400444 if (uaccess_kernel()) {
David Howellsaa563d72018-10-20 00:57:56 +0100445 i->type = ITER_KVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500446 i->kvec = (struct kvec *)iov;
447 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100448 i->type = ITER_IOVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500449 i->iov = iov;
450 }
Al Viro71d8e532014-03-05 19:28:09 -0500451 i->nr_segs = nr_segs;
452 i->iov_offset = 0;
453 i->count = count;
454}
455EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400456
Al Viro62a80672014-04-04 23:12:29 -0400457static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
458{
459 char *from = kmap_atomic(page);
460 memcpy(to, from + offset, len);
461 kunmap_atomic(from);
462}
463
Al Viro36f7a8a2015-12-06 16:49:22 -0500464static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
Al Viro62a80672014-04-04 23:12:29 -0400465{
466 char *to = kmap_atomic(page);
467 memcpy(to + offset, from, len);
468 kunmap_atomic(to);
469}
470
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400471static void memzero_page(struct page *page, size_t offset, size_t len)
472{
473 char *addr = kmap_atomic(page);
474 memset(addr + offset, 0, len);
475 kunmap_atomic(addr);
476}
477
Al Viro241699c2016-09-22 16:33:12 -0400478static inline bool allocated(struct pipe_buffer *buf)
479{
480 return buf->ops == &default_pipe_buf_ops;
481}
482
483static inline void data_start(const struct iov_iter *i, int *idxp, size_t *offp)
484{
485 size_t off = i->iov_offset;
486 int idx = i->idx;
487 if (off && (!allocated(&i->pipe->bufs[idx]) || off == PAGE_SIZE)) {
488 idx = next_idx(idx, i->pipe);
489 off = 0;
490 }
491 *idxp = idx;
492 *offp = off;
493}
494
495static size_t push_pipe(struct iov_iter *i, size_t size,
496 int *idxp, size_t *offp)
497{
498 struct pipe_inode_info *pipe = i->pipe;
499 size_t off;
500 int idx;
501 ssize_t left;
502
503 if (unlikely(size > i->count))
504 size = i->count;
505 if (unlikely(!size))
506 return 0;
507
508 left = size;
509 data_start(i, &idx, &off);
510 *idxp = idx;
511 *offp = off;
512 if (off) {
513 left -= PAGE_SIZE - off;
514 if (left <= 0) {
515 pipe->bufs[idx].len += size;
516 return size;
517 }
518 pipe->bufs[idx].len = PAGE_SIZE;
519 idx = next_idx(idx, pipe);
520 }
521 while (idx != pipe->curbuf || !pipe->nrbufs) {
522 struct page *page = alloc_page(GFP_USER);
523 if (!page)
524 break;
525 pipe->nrbufs++;
526 pipe->bufs[idx].ops = &default_pipe_buf_ops;
527 pipe->bufs[idx].page = page;
528 pipe->bufs[idx].offset = 0;
529 if (left <= PAGE_SIZE) {
530 pipe->bufs[idx].len = left;
531 return size;
532 }
533 pipe->bufs[idx].len = PAGE_SIZE;
534 left -= PAGE_SIZE;
535 idx = next_idx(idx, pipe);
536 }
537 return size - left;
538}
539
540static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
541 struct iov_iter *i)
542{
543 struct pipe_inode_info *pipe = i->pipe;
544 size_t n, off;
545 int idx;
546
547 if (!sanity(i))
548 return 0;
549
550 bytes = n = push_pipe(i, bytes, &idx, &off);
551 if (unlikely(!n))
552 return 0;
553 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
554 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
555 memcpy_to_page(pipe->bufs[idx].page, off, addr, chunk);
556 i->idx = idx;
557 i->iov_offset = off + chunk;
558 n -= chunk;
559 addr += chunk;
560 }
561 i->count -= bytes;
562 return bytes;
563}
564
Al Virof9152892018-11-27 22:32:59 -0500565static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
566 __wsum sum, size_t off)
567{
568 __wsum next = csum_partial_copy_nocheck(from, to, len, 0);
569 return csum_block_add(sum, next, off);
570}
571
Al Viro78e1f382018-11-25 16:24:16 -0500572static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
573 __wsum *csum, struct iov_iter *i)
574{
575 struct pipe_inode_info *pipe = i->pipe;
576 size_t n, r;
577 size_t off = 0;
Al Virof9152892018-11-27 22:32:59 -0500578 __wsum sum = *csum;
Al Viro78e1f382018-11-25 16:24:16 -0500579 int idx;
580
581 if (!sanity(i))
582 return 0;
583
584 bytes = n = push_pipe(i, bytes, &idx, &r);
585 if (unlikely(!n))
586 return 0;
587 for ( ; n; idx = next_idx(idx, pipe), r = 0) {
588 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
589 char *p = kmap_atomic(pipe->bufs[idx].page);
Al Virof9152892018-11-27 22:32:59 -0500590 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
Al Viro78e1f382018-11-25 16:24:16 -0500591 kunmap_atomic(p);
592 i->idx = idx;
593 i->iov_offset = r + chunk;
594 n -= chunk;
595 off += chunk;
596 addr += chunk;
597 }
598 i->count -= bytes;
599 *csum = sum;
600 return bytes;
601}
602
Al Viroaa28de22017-06-29 21:45:10 -0400603size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400604{
Al Viro36f7a8a2015-12-06 16:49:22 -0500605 const char *from = addr;
David Howells00e23702018-10-22 13:07:28 +0100606 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400607 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400608 if (iter_is_iovec(i))
609 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500610 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400611 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500612 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500613 (from += v.bv_len) - v.bv_len, v.bv_len),
614 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500615 )
Al Viro62a80672014-04-04 23:12:29 -0400616
Al Viro3d4d3e42014-11-27 14:28:06 -0500617 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400618}
Al Viroaa28de22017-06-29 21:45:10 -0400619EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400620
Dan Williams87803562018-05-03 17:06:31 -0700621#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
622static int copyout_mcsafe(void __user *to, const void *from, size_t n)
623{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800624 if (access_ok(to, n)) {
Dan Williams87803562018-05-03 17:06:31 -0700625 kasan_check_read(from, n);
626 n = copy_to_user_mcsafe((__force void *) to, from, n);
627 }
628 return n;
629}
630
631static unsigned long memcpy_mcsafe_to_page(struct page *page, size_t offset,
632 const char *from, size_t len)
633{
634 unsigned long ret;
635 char *to;
636
637 to = kmap_atomic(page);
638 ret = memcpy_mcsafe(to + offset, from, len);
639 kunmap_atomic(to);
640
641 return ret;
642}
643
Dan Williamsca146f62018-07-08 13:46:12 -0700644static size_t copy_pipe_to_iter_mcsafe(const void *addr, size_t bytes,
645 struct iov_iter *i)
646{
647 struct pipe_inode_info *pipe = i->pipe;
648 size_t n, off, xfer = 0;
649 int idx;
650
651 if (!sanity(i))
652 return 0;
653
654 bytes = n = push_pipe(i, bytes, &idx, &off);
655 if (unlikely(!n))
656 return 0;
657 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
658 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
659 unsigned long rem;
660
661 rem = memcpy_mcsafe_to_page(pipe->bufs[idx].page, off, addr,
662 chunk);
663 i->idx = idx;
664 i->iov_offset = off + chunk - rem;
665 xfer += chunk - rem;
666 if (rem)
667 break;
668 n -= chunk;
669 addr += chunk;
670 }
671 i->count -= xfer;
672 return xfer;
673}
674
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700675/**
676 * _copy_to_iter_mcsafe - copy to user with source-read error exception handling
677 * @addr: source kernel address
678 * @bytes: total transfer length
679 * @iter: destination iterator
680 *
681 * The pmem driver arranges for filesystem-dax to use this facility via
682 * dax_copy_to_iter() for protecting read/write to persistent memory.
683 * Unless / until an architecture can guarantee identical performance
684 * between _copy_to_iter_mcsafe() and _copy_to_iter() it would be a
685 * performance regression to switch more users to the mcsafe version.
686 *
687 * Otherwise, the main differences between this and typical _copy_to_iter().
688 *
689 * * Typical tail/residue handling after a fault retries the copy
690 * byte-by-byte until the fault happens again. Re-triggering machine
691 * checks is potentially fatal so the implementation uses source
692 * alignment and poison alignment assumptions to avoid re-triggering
693 * hardware exceptions.
694 *
695 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
696 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
697 * a short copy.
698 *
699 * See MCSAFE_TEST for self-test.
700 */
Dan Williams87803562018-05-03 17:06:31 -0700701size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i)
702{
703 const char *from = addr;
704 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
705
David Howells00e23702018-10-22 13:07:28 +0100706 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsca146f62018-07-08 13:46:12 -0700707 return copy_pipe_to_iter_mcsafe(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700708 if (iter_is_iovec(i))
709 might_fault();
710 iterate_and_advance(i, bytes, v,
711 copyout_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
712 ({
713 rem = memcpy_mcsafe_to_page(v.bv_page, v.bv_offset,
714 (from += v.bv_len) - v.bv_len, v.bv_len);
715 if (rem) {
716 curr_addr = (unsigned long) from;
717 bytes = curr_addr - s_addr - rem;
718 return bytes;
719 }
720 }),
721 ({
722 rem = memcpy_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len,
723 v.iov_len);
724 if (rem) {
725 curr_addr = (unsigned long) from;
726 bytes = curr_addr - s_addr - rem;
727 return bytes;
728 }
729 })
730 )
731
732 return bytes;
733}
734EXPORT_SYMBOL_GPL(_copy_to_iter_mcsafe);
735#endif /* CONFIG_ARCH_HAS_UACCESS_MCSAFE */
736
Al Viroaa28de22017-06-29 21:45:10 -0400737size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400738{
Al Viro0dbca9a2014-11-27 14:26:43 -0500739 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100740 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400741 WARN_ON(1);
742 return 0;
743 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400744 if (iter_is_iovec(i))
745 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500746 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400747 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500748 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500749 v.bv_offset, v.bv_len),
750 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500751 )
752
753 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400754}
Al Viroaa28de22017-06-29 21:45:10 -0400755EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400756
Al Viroaa28de22017-06-29 21:45:10 -0400757bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400758{
759 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100760 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400761 WARN_ON(1);
762 return false;
763 }
Al Viro33844e62016-12-21 21:55:02 -0500764 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400765 return false;
766
Al Viro09fc68dc2017-06-29 22:25:14 -0400767 if (iter_is_iovec(i))
768 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400769 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68dc2017-06-29 22:25:14 -0400770 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400771 v.iov_base, v.iov_len))
772 return false;
773 0;}),
774 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
775 v.bv_offset, v.bv_len),
776 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
777 )
778
779 iov_iter_advance(i, bytes);
780 return true;
781}
Al Viroaa28de22017-06-29 21:45:10 -0400782EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400783
Al Viroaa28de22017-06-29 21:45:10 -0400784size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500785{
786 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100787 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400788 WARN_ON(1);
789 return 0;
790 }
Al Viroaa583092014-11-27 20:27:08 -0500791 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400792 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500793 v.iov_base, v.iov_len),
794 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
795 v.bv_offset, v.bv_len),
796 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
797 )
798
799 return bytes;
800}
Al Viroaa28de22017-06-29 21:45:10 -0400801EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500802
Dan Williams0aed55a2017-05-29 12:22:50 -0700803#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700804/**
805 * _copy_from_iter_flushcache - write destination through cpu cache
806 * @addr: destination kernel address
807 * @bytes: total transfer length
808 * @iter: source iterator
809 *
810 * The pmem driver arranges for filesystem-dax to use this facility via
811 * dax_copy_from_iter() for ensuring that writes to persistent memory
812 * are flushed through the CPU cache. It is differentiated from
813 * _copy_from_iter_nocache() in that guarantees all data is flushed for
814 * all iterator types. The _copy_from_iter_nocache() only attempts to
815 * bypass the cache for the ITER_IOVEC case, and on some archs may use
816 * instructions that strand dirty-data in the cache.
817 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700818size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700819{
820 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100821 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700822 WARN_ON(1);
823 return 0;
824 }
825 iterate_and_advance(i, bytes, v,
826 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
827 v.iov_base, v.iov_len),
828 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
829 v.bv_offset, v.bv_len),
830 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
831 v.iov_len)
832 )
833
834 return bytes;
835}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700836EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700837#endif
838
Al Viroaa28de22017-06-29 21:45:10 -0400839bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400840{
841 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100842 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400843 WARN_ON(1);
844 return false;
845 }
Al Viro33844e62016-12-21 21:55:02 -0500846 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400847 return false;
848 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400849 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400850 v.iov_base, v.iov_len))
851 return false;
852 0;}),
853 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
854 v.bv_offset, v.bv_len),
855 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
856 )
857
858 iov_iter_advance(i, bytes);
859 return true;
860}
Al Viroaa28de22017-06-29 21:45:10 -0400861EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400862
Al Viro72e809e2017-06-29 21:52:57 -0400863static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
864{
Eric Dumazet6daef952019-02-26 10:42:39 -0800865 struct page *head;
866 size_t v = n + offset;
867
868 /*
869 * The general case needs to access the page order in order
870 * to compute the page size.
871 * However, we mostly deal with order-0 pages and thus can
872 * avoid a possible cache line miss for requests that fit all
873 * page orders.
874 */
875 if (n <= v && v <= PAGE_SIZE)
876 return true;
877
878 head = compound_head(page);
879 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700880
881 if (likely(n <= v && v <= (PAGE_SIZE << compound_order(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400882 return true;
883 WARN_ON(1);
884 return false;
885}
Al Virod2715242014-11-27 14:22:37 -0500886
887size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
888 struct iov_iter *i)
889{
Al Viro72e809e2017-06-29 21:52:57 -0400890 if (unlikely(!page_copy_sane(page, offset, bytes)))
891 return 0;
Al Virod2715242014-11-27 14:22:37 -0500892 if (i->type & (ITER_BVEC|ITER_KVEC)) {
893 void *kaddr = kmap_atomic(page);
894 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
895 kunmap_atomic(kaddr);
896 return wanted;
David Howells9ea9ce02018-10-20 00:57:56 +0100897 } else if (unlikely(iov_iter_is_discard(i)))
898 return bytes;
899 else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500900 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400901 else
902 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500903}
904EXPORT_SYMBOL(copy_page_to_iter);
905
906size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
907 struct iov_iter *i)
908{
Al Viro72e809e2017-06-29 21:52:57 -0400909 if (unlikely(!page_copy_sane(page, offset, bytes)))
910 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +0100911 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400912 WARN_ON(1);
913 return 0;
914 }
Al Virod2715242014-11-27 14:22:37 -0500915 if (i->type & (ITER_BVEC|ITER_KVEC)) {
916 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400917 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500918 kunmap_atomic(kaddr);
919 return wanted;
920 } else
921 return copy_page_from_iter_iovec(page, offset, bytes, i);
922}
923EXPORT_SYMBOL(copy_page_from_iter);
924
Al Viro241699c2016-09-22 16:33:12 -0400925static size_t pipe_zero(size_t bytes, struct iov_iter *i)
926{
927 struct pipe_inode_info *pipe = i->pipe;
928 size_t n, off;
929 int idx;
930
931 if (!sanity(i))
932 return 0;
933
934 bytes = n = push_pipe(i, bytes, &idx, &off);
935 if (unlikely(!n))
936 return 0;
937
938 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
939 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
940 memzero_page(pipe->bufs[idx].page, off, chunk);
941 i->idx = idx;
942 i->iov_offset = off + chunk;
943 n -= chunk;
944 }
945 i->count -= bytes;
946 return bytes;
947}
948
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400949size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
950{
David Howells00e23702018-10-22 13:07:28 +0100951 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400952 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500953 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400954 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500955 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
956 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500957 )
958
959 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400960}
961EXPORT_SYMBOL(iov_iter_zero);
962
Al Viro62a80672014-04-04 23:12:29 -0400963size_t iov_iter_copy_from_user_atomic(struct page *page,
964 struct iov_iter *i, unsigned long offset, size_t bytes)
965{
Al Viro04a31162014-11-27 13:51:41 -0500966 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400967 if (unlikely(!page_copy_sane(page, offset, bytes))) {
968 kunmap_atomic(kaddr);
969 return 0;
970 }
David Howells9ea9ce02018-10-20 00:57:56 +0100971 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400972 kunmap_atomic(kaddr);
973 WARN_ON(1);
974 return 0;
975 }
Al Viro04a31162014-11-27 13:51:41 -0500976 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400977 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -0500978 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500979 v.bv_offset, v.bv_len),
980 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -0500981 )
982 kunmap_atomic(kaddr);
983 return bytes;
Al Viro62a80672014-04-04 23:12:29 -0400984}
985EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
986
Al Virob9dc6f62017-01-14 19:33:08 -0500987static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -0400988{
989 struct pipe_inode_info *pipe = i->pipe;
Al Viro241699c2016-09-22 16:33:12 -0400990 if (pipe->nrbufs) {
Al Virob9dc6f62017-01-14 19:33:08 -0500991 size_t off = i->iov_offset;
992 int idx = i->idx;
993 int nrbufs = (idx - pipe->curbuf) & (pipe->buffers - 1);
994 if (off) {
995 pipe->bufs[idx].len = off - pipe->bufs[idx].offset;
996 idx = next_idx(idx, pipe);
997 nrbufs++;
998 }
999 while (pipe->nrbufs > nrbufs) {
Miklos Szeredia7796382016-09-27 10:45:12 +02001000 pipe_buf_release(pipe, &pipe->bufs[idx]);
Al Viro241699c2016-09-22 16:33:12 -04001001 idx = next_idx(idx, pipe);
1002 pipe->nrbufs--;
1003 }
1004 }
Al Virob9dc6f62017-01-14 19:33:08 -05001005}
1006
1007static void pipe_advance(struct iov_iter *i, size_t size)
1008{
1009 struct pipe_inode_info *pipe = i->pipe;
1010 if (unlikely(i->count < size))
1011 size = i->count;
1012 if (size) {
1013 struct pipe_buffer *buf;
1014 size_t off = i->iov_offset, left = size;
1015 int idx = i->idx;
1016 if (off) /* make it relative to the beginning of buffer */
1017 left += off - pipe->bufs[idx].offset;
1018 while (1) {
1019 buf = &pipe->bufs[idx];
1020 if (left <= buf->len)
1021 break;
1022 left -= buf->len;
1023 idx = next_idx(idx, pipe);
1024 }
1025 i->idx = idx;
1026 i->iov_offset = buf->offset + left;
1027 }
1028 i->count -= size;
1029 /* ... and discard everything past that point */
1030 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001031}
1032
Al Viro62a80672014-04-04 23:12:29 -04001033void iov_iter_advance(struct iov_iter *i, size_t size)
1034{
David Howells00e23702018-10-22 13:07:28 +01001035 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001036 pipe_advance(i, size);
1037 return;
1038 }
David Howells9ea9ce02018-10-20 00:57:56 +01001039 if (unlikely(iov_iter_is_discard(i))) {
1040 i->count -= size;
1041 return;
1042 }
Al Viroa2804552014-11-27 14:48:42 -05001043 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -04001044}
1045EXPORT_SYMBOL(iov_iter_advance);
1046
Al Viro27c0e372017-02-17 18:42:24 -05001047void iov_iter_revert(struct iov_iter *i, size_t unroll)
1048{
1049 if (!unroll)
1050 return;
Al Viro5b47d592017-05-08 13:54:47 -04001051 if (WARN_ON(unroll > MAX_RW_COUNT))
1052 return;
Al Viro27c0e372017-02-17 18:42:24 -05001053 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001054 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001055 struct pipe_inode_info *pipe = i->pipe;
1056 int idx = i->idx;
1057 size_t off = i->iov_offset;
1058 while (1) {
1059 size_t n = off - pipe->bufs[idx].offset;
1060 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001061 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001062 break;
1063 }
1064 unroll -= n;
1065 if (!unroll && idx == i->start_idx) {
1066 off = 0;
1067 break;
1068 }
1069 if (!idx--)
1070 idx = pipe->buffers - 1;
1071 off = pipe->bufs[idx].offset + pipe->bufs[idx].len;
1072 }
1073 i->iov_offset = off;
1074 i->idx = idx;
1075 pipe_truncate(i);
1076 return;
1077 }
David Howells9ea9ce02018-10-20 00:57:56 +01001078 if (unlikely(iov_iter_is_discard(i)))
1079 return;
Al Viro27c0e372017-02-17 18:42:24 -05001080 if (unroll <= i->iov_offset) {
1081 i->iov_offset -= unroll;
1082 return;
1083 }
1084 unroll -= i->iov_offset;
David Howells00e23702018-10-22 13:07:28 +01001085 if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001086 const struct bio_vec *bvec = i->bvec;
1087 while (1) {
1088 size_t n = (--bvec)->bv_len;
1089 i->nr_segs++;
1090 if (unroll <= n) {
1091 i->bvec = bvec;
1092 i->iov_offset = n - unroll;
1093 return;
1094 }
1095 unroll -= n;
1096 }
1097 } else { /* same logics for iovec and kvec */
1098 const struct iovec *iov = i->iov;
1099 while (1) {
1100 size_t n = (--iov)->iov_len;
1101 i->nr_segs++;
1102 if (unroll <= n) {
1103 i->iov = iov;
1104 i->iov_offset = n - unroll;
1105 return;
1106 }
1107 unroll -= n;
1108 }
1109 }
1110}
1111EXPORT_SYMBOL(iov_iter_revert);
1112
Al Viro62a80672014-04-04 23:12:29 -04001113/*
1114 * Return the count of just the current iov_iter segment.
1115 */
1116size_t iov_iter_single_seg_count(const struct iov_iter *i)
1117{
David Howells00e23702018-10-22 13:07:28 +01001118 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001119 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001120 if (i->nr_segs == 1)
1121 return i->count;
David Howells9ea9ce02018-10-20 00:57:56 +01001122 if (unlikely(iov_iter_is_discard(i)))
1123 return i->count;
David Howells00e23702018-10-22 13:07:28 +01001124 else if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001125 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001126 else
1127 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001128}
1129EXPORT_SYMBOL(iov_iter_single_seg_count);
1130
David Howellsaa563d72018-10-20 00:57:56 +01001131void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001132 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001133 size_t count)
1134{
David Howellsaa563d72018-10-20 00:57:56 +01001135 WARN_ON(direction & ~(READ | WRITE));
1136 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001137 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001138 i->nr_segs = nr_segs;
1139 i->iov_offset = 0;
1140 i->count = count;
1141}
1142EXPORT_SYMBOL(iov_iter_kvec);
1143
David Howellsaa563d72018-10-20 00:57:56 +01001144void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001145 const struct bio_vec *bvec, unsigned long nr_segs,
1146 size_t count)
1147{
David Howellsaa563d72018-10-20 00:57:56 +01001148 WARN_ON(direction & ~(READ | WRITE));
1149 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001150 i->bvec = bvec;
1151 i->nr_segs = nr_segs;
1152 i->iov_offset = 0;
1153 i->count = count;
1154}
1155EXPORT_SYMBOL(iov_iter_bvec);
1156
David Howellsaa563d72018-10-20 00:57:56 +01001157void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001158 struct pipe_inode_info *pipe,
1159 size_t count)
1160{
David Howellsaa563d72018-10-20 00:57:56 +01001161 BUG_ON(direction != READ);
Al Virob9dc6f62017-01-14 19:33:08 -05001162 WARN_ON(pipe->nrbufs == pipe->buffers);
David Howellsaa563d72018-10-20 00:57:56 +01001163 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001164 i->pipe = pipe;
1165 i->idx = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1166 i->iov_offset = 0;
1167 i->count = count;
Al Viro27c0e372017-02-17 18:42:24 -05001168 i->start_idx = i->idx;
Al Viro241699c2016-09-22 16:33:12 -04001169}
1170EXPORT_SYMBOL(iov_iter_pipe);
1171
David Howells9ea9ce02018-10-20 00:57:56 +01001172/**
1173 * iov_iter_discard - Initialise an I/O iterator that discards data
1174 * @i: The iterator to initialise.
1175 * @direction: The direction of the transfer.
1176 * @count: The size of the I/O buffer in bytes.
1177 *
1178 * Set up an I/O iterator that just discards everything that's written to it.
1179 * It's only available as a READ iterator.
1180 */
1181void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1182{
1183 BUG_ON(direction != READ);
1184 i->type = ITER_DISCARD | READ;
1185 i->count = count;
1186 i->iov_offset = 0;
1187}
1188EXPORT_SYMBOL(iov_iter_discard);
1189
Al Viro62a80672014-04-04 23:12:29 -04001190unsigned long iov_iter_alignment(const struct iov_iter *i)
1191{
Al Viro04a31162014-11-27 13:51:41 -05001192 unsigned long res = 0;
1193 size_t size = i->count;
1194
David Howells00e23702018-10-22 13:07:28 +01001195 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro33844e62016-12-21 21:55:02 -05001196 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->idx]))
Al Viro241699c2016-09-22 16:33:12 -04001197 return size | i->iov_offset;
1198 return size;
1199 }
Al Viro04a31162014-11-27 13:51:41 -05001200 iterate_all_kinds(i, size, v,
1201 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001202 res |= v.bv_offset | v.bv_len,
1203 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -05001204 )
1205 return res;
Al Viro62a80672014-04-04 23:12:29 -04001206}
1207EXPORT_SYMBOL(iov_iter_alignment);
1208
Al Viro357f4352016-04-08 19:05:19 -04001209unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1210{
Al Viro33844e62016-12-21 21:55:02 -05001211 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001212 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001213
David Howells9ea9ce02018-10-20 00:57:56 +01001214 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001215 WARN_ON(1);
1216 return ~0U;
1217 }
1218
Al Viro357f4352016-04-08 19:05:19 -04001219 iterate_all_kinds(i, size, v,
1220 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1221 (size != v.iov_len ? size : 0), 0),
1222 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1223 (size != v.bv_len ? size : 0)),
1224 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1225 (size != v.iov_len ? size : 0))
1226 );
Al Viro33844e62016-12-21 21:55:02 -05001227 return res;
Al Viro357f4352016-04-08 19:05:19 -04001228}
1229EXPORT_SYMBOL(iov_iter_gap_alignment);
1230
Ilya Dryomove76b63122018-05-02 20:16:56 +02001231static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001232 size_t maxsize,
1233 struct page **pages,
1234 int idx,
1235 size_t *start)
1236{
1237 struct pipe_inode_info *pipe = i->pipe;
Al Viro1689c732016-10-11 18:21:14 +01001238 ssize_t n = push_pipe(i, maxsize, &idx, start);
Al Viro241699c2016-09-22 16:33:12 -04001239 if (!n)
1240 return -EFAULT;
1241
1242 maxsize = n;
1243 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001244 while (n > 0) {
Al Viro241699c2016-09-22 16:33:12 -04001245 get_page(*pages++ = pipe->bufs[idx].page);
1246 idx = next_idx(idx, pipe);
1247 n -= PAGE_SIZE;
1248 }
1249
1250 return maxsize;
1251}
1252
1253static ssize_t pipe_get_pages(struct iov_iter *i,
1254 struct page **pages, size_t maxsize, unsigned maxpages,
1255 size_t *start)
1256{
1257 unsigned npages;
1258 size_t capacity;
1259 int idx;
1260
Al Viro33844e62016-12-21 21:55:02 -05001261 if (!maxsize)
1262 return 0;
1263
Al Viro241699c2016-09-22 16:33:12 -04001264 if (!sanity(i))
1265 return -EFAULT;
1266
1267 data_start(i, &idx, start);
1268 /* some of this one + all after this one */
1269 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1270 capacity = min(npages,maxpages) * PAGE_SIZE - *start;
1271
1272 return __pipe_get_pages(i, min(maxsize, capacity), pages, idx, start);
1273}
1274
Al Viro62a80672014-04-04 23:12:29 -04001275ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001276 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001277 size_t *start)
1278{
Al Viroe5393fa2014-11-27 14:12:09 -05001279 if (maxsize > i->count)
1280 maxsize = i->count;
1281
David Howells00e23702018-10-22 13:07:28 +01001282 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001283 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001284 if (unlikely(iov_iter_is_discard(i)))
1285 return -EFAULT;
1286
Al Viroe5393fa2014-11-27 14:12:09 -05001287 iterate_all_kinds(i, maxsize, v, ({
1288 unsigned long addr = (unsigned long)v.iov_base;
1289 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1290 int n;
1291 int res;
1292
1293 if (len > maxpages * PAGE_SIZE)
1294 len = maxpages * PAGE_SIZE;
1295 addr &= ~(PAGE_SIZE - 1);
1296 n = DIV_ROUND_UP(len, PAGE_SIZE);
Ira Weiny73b01402019-05-13 17:17:11 -07001297 res = get_user_pages_fast(addr, n,
1298 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1299 pages);
Al Viroe5393fa2014-11-27 14:12:09 -05001300 if (unlikely(res < 0))
1301 return res;
1302 return (res == n ? len : res * PAGE_SIZE) - *start;
1303 0;}),({
1304 /* can't be more than PAGE_SIZE */
1305 *start = v.bv_offset;
1306 get_page(*pages = v.bv_page);
1307 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001308 }),({
1309 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001310 })
1311 )
1312 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001313}
1314EXPORT_SYMBOL(iov_iter_get_pages);
1315
Al Viro1b17f1f2014-11-27 14:14:31 -05001316static struct page **get_pages_array(size_t n)
1317{
Michal Hocko752ade62017-05-08 15:57:27 -07001318 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001319}
1320
Al Viro241699c2016-09-22 16:33:12 -04001321static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1322 struct page ***pages, size_t maxsize,
1323 size_t *start)
1324{
1325 struct page **p;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001326 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001327 int idx;
1328 int npages;
1329
Al Viro33844e62016-12-21 21:55:02 -05001330 if (!maxsize)
1331 return 0;
1332
Al Viro241699c2016-09-22 16:33:12 -04001333 if (!sanity(i))
1334 return -EFAULT;
1335
1336 data_start(i, &idx, start);
1337 /* some of this one + all after this one */
1338 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1339 n = npages * PAGE_SIZE - *start;
1340 if (maxsize > n)
1341 maxsize = n;
1342 else
1343 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1344 p = get_pages_array(npages);
1345 if (!p)
1346 return -ENOMEM;
1347 n = __pipe_get_pages(i, maxsize, p, idx, start);
1348 if (n > 0)
1349 *pages = p;
1350 else
1351 kvfree(p);
1352 return n;
1353}
1354
Al Viro62a80672014-04-04 23:12:29 -04001355ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1356 struct page ***pages, size_t maxsize,
1357 size_t *start)
1358{
Al Viro1b17f1f2014-11-27 14:14:31 -05001359 struct page **p;
1360
1361 if (maxsize > i->count)
1362 maxsize = i->count;
1363
David Howells00e23702018-10-22 13:07:28 +01001364 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001365 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001366 if (unlikely(iov_iter_is_discard(i)))
1367 return -EFAULT;
1368
Al Viro1b17f1f2014-11-27 14:14:31 -05001369 iterate_all_kinds(i, maxsize, v, ({
1370 unsigned long addr = (unsigned long)v.iov_base;
1371 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1372 int n;
1373 int res;
1374
1375 addr &= ~(PAGE_SIZE - 1);
1376 n = DIV_ROUND_UP(len, PAGE_SIZE);
1377 p = get_pages_array(n);
1378 if (!p)
1379 return -ENOMEM;
Ira Weiny73b01402019-05-13 17:17:11 -07001380 res = get_user_pages_fast(addr, n,
1381 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
Al Viro1b17f1f2014-11-27 14:14:31 -05001382 if (unlikely(res < 0)) {
1383 kvfree(p);
1384 return res;
1385 }
1386 *pages = p;
1387 return (res == n ? len : res * PAGE_SIZE) - *start;
1388 0;}),({
1389 /* can't be more than PAGE_SIZE */
1390 *start = v.bv_offset;
1391 *pages = p = get_pages_array(1);
1392 if (!p)
1393 return -ENOMEM;
1394 get_page(*p = v.bv_page);
1395 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001396 }),({
1397 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -05001398 })
1399 )
1400 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001401}
1402EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1403
Al Viroa604ec72014-11-24 01:08:00 -05001404size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1405 struct iov_iter *i)
1406{
1407 char *to = addr;
1408 __wsum sum, next;
1409 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001410 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001411 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001412 WARN_ON(1);
1413 return 0;
1414 }
Al Viroa604ec72014-11-24 01:08:00 -05001415 iterate_and_advance(i, bytes, v, ({
1416 int err = 0;
Al Virocbbd26b2016-11-01 22:09:04 -04001417 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001418 (to += v.iov_len) - v.iov_len,
1419 v.iov_len, 0, &err);
1420 if (!err) {
1421 sum = csum_block_add(sum, next, off);
1422 off += v.iov_len;
1423 }
1424 err ? v.iov_len : 0;
1425 }), ({
1426 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001427 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1428 p + v.bv_offset, v.bv_len,
1429 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001430 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001431 off += v.bv_len;
1432 }),({
Al Virof9152892018-11-27 22:32:59 -05001433 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1434 v.iov_base, v.iov_len,
1435 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001436 off += v.iov_len;
1437 })
1438 )
1439 *csum = sum;
1440 return bytes;
1441}
1442EXPORT_SYMBOL(csum_and_copy_from_iter);
1443
Al Virocbbd26b2016-11-01 22:09:04 -04001444bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1445 struct iov_iter *i)
1446{
1447 char *to = addr;
1448 __wsum sum, next;
1449 size_t off = 0;
1450 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001451 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001452 WARN_ON(1);
1453 return false;
1454 }
1455 if (unlikely(i->count < bytes))
1456 return false;
1457 iterate_all_kinds(i, bytes, v, ({
1458 int err = 0;
1459 next = csum_and_copy_from_user(v.iov_base,
1460 (to += v.iov_len) - v.iov_len,
1461 v.iov_len, 0, &err);
1462 if (err)
1463 return false;
1464 sum = csum_block_add(sum, next, off);
1465 off += v.iov_len;
1466 0;
1467 }), ({
1468 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001469 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1470 p + v.bv_offset, v.bv_len,
1471 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001472 kunmap_atomic(p);
Al Virocbbd26b2016-11-01 22:09:04 -04001473 off += v.bv_len;
1474 }),({
Al Virof9152892018-11-27 22:32:59 -05001475 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1476 v.iov_base, v.iov_len,
1477 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001478 off += v.iov_len;
1479 })
1480 )
1481 *csum = sum;
1482 iov_iter_advance(i, bytes);
1483 return true;
1484}
1485EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1486
Sagi Grimbergcb002d02018-12-03 17:52:07 -08001487size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csump,
Al Viroa604ec72014-11-24 01:08:00 -05001488 struct iov_iter *i)
1489{
Al Viro36f7a8a2015-12-06 16:49:22 -05001490 const char *from = addr;
Sagi Grimbergcb002d02018-12-03 17:52:07 -08001491 __wsum *csum = csump;
Al Viroa604ec72014-11-24 01:08:00 -05001492 __wsum sum, next;
1493 size_t off = 0;
Al Viro78e1f382018-11-25 16:24:16 -05001494
1495 if (unlikely(iov_iter_is_pipe(i)))
1496 return csum_and_copy_to_pipe_iter(addr, bytes, csum, i);
1497
Al Viroa604ec72014-11-24 01:08:00 -05001498 sum = *csum;
Al Viro78e1f382018-11-25 16:24:16 -05001499 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001500 WARN_ON(1); /* for now */
1501 return 0;
1502 }
Al Viroa604ec72014-11-24 01:08:00 -05001503 iterate_and_advance(i, bytes, v, ({
1504 int err = 0;
1505 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001506 v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001507 v.iov_len, 0, &err);
1508 if (!err) {
1509 sum = csum_block_add(sum, next, off);
1510 off += v.iov_len;
1511 }
1512 err ? v.iov_len : 0;
1513 }), ({
1514 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001515 sum = csum_and_memcpy(p + v.bv_offset,
1516 (from += v.bv_len) - v.bv_len,
1517 v.bv_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001518 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001519 off += v.bv_len;
1520 }),({
Al Virof9152892018-11-27 22:32:59 -05001521 sum = csum_and_memcpy(v.iov_base,
1522 (from += v.iov_len) - v.iov_len,
1523 v.iov_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001524 off += v.iov_len;
1525 })
1526 )
1527 *csum = sum;
1528 return bytes;
1529}
1530EXPORT_SYMBOL(csum_and_copy_to_iter);
1531
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001532size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1533 struct iov_iter *i)
1534{
YueHaibing27fad742019-04-04 10:31:14 +08001535#ifdef CONFIG_CRYPTO
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001536 struct ahash_request *hash = hashp;
1537 struct scatterlist sg;
1538 size_t copied;
1539
1540 copied = copy_to_iter(addr, bytes, i);
1541 sg_init_one(&sg, addr, copied);
1542 ahash_request_set_crypt(hash, &sg, NULL, copied);
1543 crypto_ahash_update(hash);
1544 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001545#else
1546 return 0;
1547#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001548}
1549EXPORT_SYMBOL(hash_and_copy_to_iter);
1550
Al Viro62a80672014-04-04 23:12:29 -04001551int iov_iter_npages(const struct iov_iter *i, int maxpages)
1552{
Al Viroe0f2dc42014-11-27 14:09:46 -05001553 size_t size = i->count;
1554 int npages = 0;
1555
1556 if (!size)
1557 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001558 if (unlikely(iov_iter_is_discard(i)))
1559 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001560
David Howells00e23702018-10-22 13:07:28 +01001561 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001562 struct pipe_inode_info *pipe = i->pipe;
1563 size_t off;
1564 int idx;
1565
1566 if (!sanity(i))
1567 return 0;
1568
1569 data_start(i, &idx, &off);
1570 /* some of this one + all after this one */
1571 npages = ((pipe->curbuf - idx - 1) & (pipe->buffers - 1)) + 1;
1572 if (npages >= maxpages)
1573 return maxpages;
1574 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001575 unsigned long p = (unsigned long)v.iov_base;
1576 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1577 - p / PAGE_SIZE;
1578 if (npages >= maxpages)
1579 return maxpages;
1580 0;}),({
1581 npages++;
1582 if (npages >= maxpages)
1583 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001584 }),({
1585 unsigned long p = (unsigned long)v.iov_base;
1586 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1587 - p / PAGE_SIZE;
1588 if (npages >= maxpages)
1589 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001590 })
1591 )
1592 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001593}
Al Virof67da302014-03-19 01:16:16 -04001594EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001595
1596const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1597{
1598 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001599 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001600 WARN_ON(1);
1601 return NULL;
1602 }
David Howells9ea9ce02018-10-20 00:57:56 +01001603 if (unlikely(iov_iter_is_discard(new)))
1604 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001605 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001606 return new->bvec = kmemdup(new->bvec,
1607 new->nr_segs * sizeof(struct bio_vec),
1608 flags);
1609 else
1610 /* iovec and kvec have identical layout */
1611 return new->iov = kmemdup(new->iov,
1612 new->nr_segs * sizeof(struct iovec),
1613 flags);
1614}
1615EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001616
Vegard Nossumffecee42016-10-08 11:18:07 +02001617/**
1618 * import_iovec() - Copy an array of &struct iovec from userspace
1619 * into the kernel, check that it is valid, and initialize a new
1620 * &struct iov_iter iterator to access it.
1621 *
1622 * @type: One of %READ or %WRITE.
1623 * @uvector: Pointer to the userspace array.
1624 * @nr_segs: Number of elements in userspace array.
1625 * @fast_segs: Number of elements in @iov.
1626 * @iov: (input and output parameter) Pointer to pointer to (usually small
1627 * on-stack) kernel array.
1628 * @i: Pointer to iterator that will be initialized on success.
1629 *
1630 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1631 * then this function places %NULL in *@iov on return. Otherwise, a new
1632 * array will be allocated and the result placed in *@iov. This means that
1633 * the caller may call kfree() on *@iov regardless of whether the small
1634 * on-stack array was used or not (and regardless of whether this function
1635 * returns an error or not).
1636 *
1637 * Return: 0 on success or negative error code on error.
1638 */
Al Virobc917be2015-03-21 17:45:43 -04001639int import_iovec(int type, const struct iovec __user * uvector,
1640 unsigned nr_segs, unsigned fast_segs,
1641 struct iovec **iov, struct iov_iter *i)
1642{
1643 ssize_t n;
1644 struct iovec *p;
1645 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1646 *iov, &p);
1647 if (n < 0) {
1648 if (p != *iov)
1649 kfree(p);
1650 *iov = NULL;
1651 return n;
1652 }
1653 iov_iter_init(i, type, p, nr_segs, n);
1654 *iov = p == *iov ? NULL : p;
1655 return 0;
1656}
1657EXPORT_SYMBOL(import_iovec);
1658
1659#ifdef CONFIG_COMPAT
1660#include <linux/compat.h>
1661
1662int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
1663 unsigned nr_segs, unsigned fast_segs,
1664 struct iovec **iov, struct iov_iter *i)
1665{
1666 ssize_t n;
1667 struct iovec *p;
1668 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1669 *iov, &p);
1670 if (n < 0) {
1671 if (p != *iov)
1672 kfree(p);
1673 *iov = NULL;
1674 return n;
1675 }
1676 iov_iter_init(i, type, p, nr_segs, n);
1677 *iov = p == *iov ? NULL : p;
1678 return 0;
1679}
1680#endif
1681
1682int import_single_range(int rw, void __user *buf, size_t len,
1683 struct iovec *iov, struct iov_iter *i)
1684{
1685 if (len > MAX_RW_COUNT)
1686 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001687 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04001688 return -EFAULT;
1689
1690 iov->iov_base = buf;
1691 iov->iov_len = len;
1692 iov_iter_init(i, rw, iov, 1, len);
1693 return 0;
1694}
Al Viroe1267582015-12-06 20:38:56 -05001695EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05001696
1697int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1698 int (*f)(struct kvec *vec, void *context),
1699 void *context)
1700{
1701 struct kvec w;
1702 int err = -EINVAL;
1703 if (!bytes)
1704 return 0;
1705
1706 iterate_all_kinds(i, bytes, v, -EINVAL, ({
1707 w.iov_base = kmap(v.bv_page) + v.bv_offset;
1708 w.iov_len = v.bv_len;
1709 err = f(&w, context);
1710 kunmap(v.bv_page);
1711 err;}), ({
1712 w = v;
1713 err = f(&w, context);})
1714 )
1715 return err;
1716}
1717EXPORT_SYMBOL(iov_iter_for_each_range);