blob: 621984743268db52dd6b8a8e1401d00ebc942588 [file] [log] [blame]
Al Viro4f18cd32014-02-05 19:11:33 -05001#include <linux/export.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -06002#include <linux/bvec.h>
Al Viro4f18cd32014-02-05 19:11:33 -05003#include <linux/uio.h>
4#include <linux/pagemap.h>
Al Viro91f79c42014-03-21 04:58:33 -04005#include <linux/slab.h>
6#include <linux/vmalloc.h>
Al Viro241699c2016-09-22 16:33:12 -04007#include <linux/splice.h>
Al Viroa604ec72014-11-24 01:08:00 -05008#include <net/checksum.h>
Al Viro4f18cd32014-02-05 19:11:33 -05009
Al Viro241699c2016-09-22 16:33:12 -040010#define PIPE_PARANOIA /* for now */
11
Al Viro04a31162014-11-27 13:51:41 -050012#define iterate_iovec(i, n, __v, __p, skip, STEP) { \
13 size_t left; \
14 size_t wanted = n; \
15 __p = i->iov; \
16 __v.iov_len = min(n, __p->iov_len - skip); \
17 if (likely(__v.iov_len)) { \
18 __v.iov_base = __p->iov_base + skip; \
19 left = (STEP); \
20 __v.iov_len -= left; \
21 skip += __v.iov_len; \
22 n -= __v.iov_len; \
23 } else { \
24 left = 0; \
25 } \
26 while (unlikely(!left && n)) { \
27 __p++; \
28 __v.iov_len = min(n, __p->iov_len); \
29 if (unlikely(!__v.iov_len)) \
30 continue; \
31 __v.iov_base = __p->iov_base; \
32 left = (STEP); \
33 __v.iov_len -= left; \
34 skip = __v.iov_len; \
35 n -= __v.iov_len; \
36 } \
37 n = wanted - n; \
38}
39
Al Viroa2804552014-11-27 14:48:42 -050040#define iterate_kvec(i, n, __v, __p, skip, STEP) { \
41 size_t wanted = n; \
42 __p = i->kvec; \
43 __v.iov_len = min(n, __p->iov_len - skip); \
44 if (likely(__v.iov_len)) { \
45 __v.iov_base = __p->iov_base + skip; \
46 (void)(STEP); \
47 skip += __v.iov_len; \
48 n -= __v.iov_len; \
49 } \
50 while (unlikely(n)) { \
51 __p++; \
52 __v.iov_len = min(n, __p->iov_len); \
53 if (unlikely(!__v.iov_len)) \
54 continue; \
55 __v.iov_base = __p->iov_base; \
56 (void)(STEP); \
57 skip = __v.iov_len; \
58 n -= __v.iov_len; \
59 } \
60 n = wanted; \
61}
62
Ming Lei1bdc76a2016-05-30 21:34:32 +080063#define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
64 struct bvec_iter __start; \
65 __start.bi_size = n; \
66 __start.bi_bvec_done = skip; \
67 __start.bi_idx = 0; \
68 for_each_bvec(__v, i->bvec, __bi, __start) { \
69 if (!__v.bv_len) \
Al Viro04a31162014-11-27 13:51:41 -050070 continue; \
Al Viro04a31162014-11-27 13:51:41 -050071 (void)(STEP); \
Al Viro04a31162014-11-27 13:51:41 -050072 } \
Al Viro04a31162014-11-27 13:51:41 -050073}
74
Al Viroa2804552014-11-27 14:48:42 -050075#define iterate_all_kinds(i, n, v, I, B, K) { \
Al Viro33844e62016-12-21 21:55:02 -050076 if (likely(n)) { \
77 size_t skip = i->iov_offset; \
78 if (unlikely(i->type & ITER_BVEC)) { \
79 struct bio_vec v; \
80 struct bvec_iter __bi; \
81 iterate_bvec(i, n, v, __bi, skip, (B)) \
82 } else if (unlikely(i->type & ITER_KVEC)) { \
83 const struct kvec *kvec; \
84 struct kvec v; \
85 iterate_kvec(i, n, v, kvec, skip, (K)) \
David Howells9ea9ce02018-10-20 00:57:56 +010086 } else if (unlikely(i->type & ITER_DISCARD)) { \
Al Viro33844e62016-12-21 21:55:02 -050087 } else { \
88 const struct iovec *iov; \
89 struct iovec v; \
90 iterate_iovec(i, n, v, iov, skip, (I)) \
91 } \
Al Viro04a31162014-11-27 13:51:41 -050092 } \
93}
94
Al Viroa2804552014-11-27 14:48:42 -050095#define iterate_and_advance(i, n, v, I, B, K) { \
Al Virodd254f52016-05-09 11:54:48 -040096 if (unlikely(i->count < n)) \
97 n = i->count; \
Al Viro19f18452016-05-25 17:36:19 -040098 if (i->count) { \
Al Virodd254f52016-05-09 11:54:48 -040099 size_t skip = i->iov_offset; \
100 if (unlikely(i->type & ITER_BVEC)) { \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800101 const struct bio_vec *bvec = i->bvec; \
Al Virodd254f52016-05-09 11:54:48 -0400102 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800103 struct bvec_iter __bi; \
104 iterate_bvec(i, n, v, __bi, skip, (B)) \
105 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
106 i->nr_segs -= i->bvec - bvec; \
107 skip = __bi.bi_bvec_done; \
Al Virodd254f52016-05-09 11:54:48 -0400108 } else if (unlikely(i->type & ITER_KVEC)) { \
109 const struct kvec *kvec; \
110 struct kvec v; \
111 iterate_kvec(i, n, v, kvec, skip, (K)) \
112 if (skip == kvec->iov_len) { \
113 kvec++; \
114 skip = 0; \
115 } \
116 i->nr_segs -= kvec - i->kvec; \
117 i->kvec = kvec; \
David Howells9ea9ce02018-10-20 00:57:56 +0100118 } else if (unlikely(i->type & ITER_DISCARD)) { \
119 skip += n; \
Al Virodd254f52016-05-09 11:54:48 -0400120 } else { \
121 const struct iovec *iov; \
122 struct iovec v; \
123 iterate_iovec(i, n, v, iov, skip, (I)) \
124 if (skip == iov->iov_len) { \
125 iov++; \
126 skip = 0; \
127 } \
128 i->nr_segs -= iov - i->iov; \
129 i->iov = iov; \
Al Viro7ce2a912014-11-27 13:59:45 -0500130 } \
Al Virodd254f52016-05-09 11:54:48 -0400131 i->count -= n; \
132 i->iov_offset = skip; \
Al Viro7ce2a912014-11-27 13:59:45 -0500133 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500134}
135
Al Viro09fc68dc2017-06-29 22:25:14 -0400136static int copyout(void __user *to, const void *from, size_t n)
137{
138 if (access_ok(VERIFY_WRITE, to, n)) {
139 kasan_check_read(from, n);
140 n = raw_copy_to_user(to, from, n);
141 }
142 return n;
143}
144
145static int copyin(void *to, const void __user *from, size_t n)
146{
147 if (access_ok(VERIFY_READ, from, n)) {
148 kasan_check_write(to, n);
149 n = raw_copy_from_user(to, from, n);
150 }
151 return n;
152}
153
Al Viro62a80672014-04-04 23:12:29 -0400154static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500155 struct iov_iter *i)
156{
157 size_t skip, copy, left, wanted;
158 const struct iovec *iov;
159 char __user *buf;
160 void *kaddr, *from;
161
162 if (unlikely(bytes > i->count))
163 bytes = i->count;
164
165 if (unlikely(!bytes))
166 return 0;
167
Al Viro09fc68dc2017-06-29 22:25:14 -0400168 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500169 wanted = bytes;
170 iov = i->iov;
171 skip = i->iov_offset;
172 buf = iov->iov_base + skip;
173 copy = min(bytes, iov->iov_len - skip);
174
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700175 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500176 kaddr = kmap_atomic(page);
177 from = kaddr + offset;
178
179 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400180 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500181 copy -= left;
182 skip += copy;
183 from += copy;
184 bytes -= copy;
185
186 while (unlikely(!left && bytes)) {
187 iov++;
188 buf = iov->iov_base;
189 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400190 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500191 copy -= left;
192 skip = copy;
193 from += copy;
194 bytes -= copy;
195 }
196 if (likely(!bytes)) {
197 kunmap_atomic(kaddr);
198 goto done;
199 }
200 offset = from - kaddr;
201 buf += copy;
202 kunmap_atomic(kaddr);
203 copy = min(bytes, iov->iov_len - skip);
204 }
205 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700206
Al Viro4f18cd32014-02-05 19:11:33 -0500207 kaddr = kmap(page);
208 from = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400209 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500210 copy -= left;
211 skip += copy;
212 from += copy;
213 bytes -= copy;
214 while (unlikely(!left && bytes)) {
215 iov++;
216 buf = iov->iov_base;
217 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400218 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500219 copy -= left;
220 skip = copy;
221 from += copy;
222 bytes -= copy;
223 }
224 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700225
Al Viro4f18cd32014-02-05 19:11:33 -0500226done:
Al Viro81055e52014-04-04 19:23:46 -0400227 if (skip == iov->iov_len) {
228 iov++;
229 skip = 0;
230 }
Al Viro4f18cd32014-02-05 19:11:33 -0500231 i->count -= wanted - bytes;
232 i->nr_segs -= iov - i->iov;
233 i->iov = iov;
234 i->iov_offset = skip;
235 return wanted - bytes;
236}
Al Viro4f18cd32014-02-05 19:11:33 -0500237
Al Viro62a80672014-04-04 23:12:29 -0400238static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400239 struct iov_iter *i)
240{
241 size_t skip, copy, left, wanted;
242 const struct iovec *iov;
243 char __user *buf;
244 void *kaddr, *to;
245
246 if (unlikely(bytes > i->count))
247 bytes = i->count;
248
249 if (unlikely(!bytes))
250 return 0;
251
Al Viro09fc68dc2017-06-29 22:25:14 -0400252 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400253 wanted = bytes;
254 iov = i->iov;
255 skip = i->iov_offset;
256 buf = iov->iov_base + skip;
257 copy = min(bytes, iov->iov_len - skip);
258
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700259 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400260 kaddr = kmap_atomic(page);
261 to = kaddr + offset;
262
263 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400264 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400265 copy -= left;
266 skip += copy;
267 to += copy;
268 bytes -= copy;
269
270 while (unlikely(!left && bytes)) {
271 iov++;
272 buf = iov->iov_base;
273 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400274 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400275 copy -= left;
276 skip = copy;
277 to += copy;
278 bytes -= copy;
279 }
280 if (likely(!bytes)) {
281 kunmap_atomic(kaddr);
282 goto done;
283 }
284 offset = to - kaddr;
285 buf += copy;
286 kunmap_atomic(kaddr);
287 copy = min(bytes, iov->iov_len - skip);
288 }
289 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700290
Al Virof0d1bec2014-04-03 15:05:18 -0400291 kaddr = kmap(page);
292 to = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400293 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400294 copy -= left;
295 skip += copy;
296 to += copy;
297 bytes -= copy;
298 while (unlikely(!left && bytes)) {
299 iov++;
300 buf = iov->iov_base;
301 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400302 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400303 copy -= left;
304 skip = copy;
305 to += copy;
306 bytes -= copy;
307 }
308 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700309
Al Virof0d1bec2014-04-03 15:05:18 -0400310done:
Al Viro81055e52014-04-04 19:23:46 -0400311 if (skip == iov->iov_len) {
312 iov++;
313 skip = 0;
314 }
Al Virof0d1bec2014-04-03 15:05:18 -0400315 i->count -= wanted - bytes;
316 i->nr_segs -= iov - i->iov;
317 i->iov = iov;
318 i->iov_offset = skip;
319 return wanted - bytes;
320}
Al Virof0d1bec2014-04-03 15:05:18 -0400321
Al Viro241699c2016-09-22 16:33:12 -0400322#ifdef PIPE_PARANOIA
323static bool sanity(const struct iov_iter *i)
324{
325 struct pipe_inode_info *pipe = i->pipe;
326 int idx = i->idx;
327 int next = pipe->curbuf + pipe->nrbufs;
328 if (i->iov_offset) {
329 struct pipe_buffer *p;
330 if (unlikely(!pipe->nrbufs))
331 goto Bad; // pipe must be non-empty
332 if (unlikely(idx != ((next - 1) & (pipe->buffers - 1))))
333 goto Bad; // must be at the last buffer...
334
335 p = &pipe->bufs[idx];
336 if (unlikely(p->offset + p->len != i->iov_offset))
337 goto Bad; // ... at the end of segment
338 } else {
339 if (idx != (next & (pipe->buffers - 1)))
340 goto Bad; // must be right after the last buffer
341 }
342 return true;
343Bad:
344 printk(KERN_ERR "idx = %d, offset = %zd\n", i->idx, i->iov_offset);
345 printk(KERN_ERR "curbuf = %d, nrbufs = %d, buffers = %d\n",
346 pipe->curbuf, pipe->nrbufs, pipe->buffers);
347 for (idx = 0; idx < pipe->buffers; idx++)
348 printk(KERN_ERR "[%p %p %d %d]\n",
349 pipe->bufs[idx].ops,
350 pipe->bufs[idx].page,
351 pipe->bufs[idx].offset,
352 pipe->bufs[idx].len);
353 WARN_ON(1);
354 return false;
355}
356#else
357#define sanity(i) true
358#endif
359
360static inline int next_idx(int idx, struct pipe_inode_info *pipe)
361{
362 return (idx + 1) & (pipe->buffers - 1);
363}
364
365static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
366 struct iov_iter *i)
367{
368 struct pipe_inode_info *pipe = i->pipe;
369 struct pipe_buffer *buf;
370 size_t off;
371 int idx;
372
373 if (unlikely(bytes > i->count))
374 bytes = i->count;
375
376 if (unlikely(!bytes))
377 return 0;
378
379 if (!sanity(i))
380 return 0;
381
382 off = i->iov_offset;
383 idx = i->idx;
384 buf = &pipe->bufs[idx];
385 if (off) {
386 if (offset == off && buf->page == page) {
387 /* merge with the last one */
388 buf->len += bytes;
389 i->iov_offset += bytes;
390 goto out;
391 }
392 idx = next_idx(idx, pipe);
393 buf = &pipe->bufs[idx];
394 }
395 if (idx == pipe->curbuf && pipe->nrbufs)
396 return 0;
397 pipe->nrbufs++;
398 buf->ops = &page_cache_pipe_buf_ops;
399 get_page(buf->page = page);
400 buf->offset = offset;
401 buf->len = bytes;
402 i->iov_offset = offset + bytes;
403 i->idx = idx;
404out:
405 i->count -= bytes;
406 return bytes;
407}
408
Al Viro4f18cd32014-02-05 19:11:33 -0500409/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400410 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
411 * bytes. For each iovec, fault in each page that constitutes the iovec.
412 *
413 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
414 * because it is an invalid address).
415 */
Al Virod4690f12016-09-16 00:11:45 +0100416int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400417{
418 size_t skip = i->iov_offset;
419 const struct iovec *iov;
420 int err;
421 struct iovec v;
422
423 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
424 iterate_iovec(i, bytes, v, iov, skip, ({
Al Viro4bce9f6e2016-09-17 18:02:44 -0400425 err = fault_in_pages_readable(v.iov_base, v.iov_len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400426 if (unlikely(err))
427 return err;
428 0;}))
429 }
430 return 0;
431}
Al Virod4690f12016-09-16 00:11:45 +0100432EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400433
David Howellsaa563d72018-10-20 00:57:56 +0100434void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500435 const struct iovec *iov, unsigned long nr_segs,
436 size_t count)
437{
David Howellsaa563d72018-10-20 00:57:56 +0100438 WARN_ON(direction & ~(READ | WRITE));
439 direction &= READ | WRITE;
440
Al Viro71d8e532014-03-05 19:28:09 -0500441 /* It will get better. Eventually... */
Al Virodb68ce12017-03-20 21:08:07 -0400442 if (uaccess_kernel()) {
David Howellsaa563d72018-10-20 00:57:56 +0100443 i->type = ITER_KVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500444 i->kvec = (struct kvec *)iov;
445 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100446 i->type = ITER_IOVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500447 i->iov = iov;
448 }
Al Viro71d8e532014-03-05 19:28:09 -0500449 i->nr_segs = nr_segs;
450 i->iov_offset = 0;
451 i->count = count;
452}
453EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400454
Al Viro62a80672014-04-04 23:12:29 -0400455static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
456{
457 char *from = kmap_atomic(page);
458 memcpy(to, from + offset, len);
459 kunmap_atomic(from);
460}
461
Al Viro36f7a8a2015-12-06 16:49:22 -0500462static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
Al Viro62a80672014-04-04 23:12:29 -0400463{
464 char *to = kmap_atomic(page);
465 memcpy(to + offset, from, len);
466 kunmap_atomic(to);
467}
468
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400469static void memzero_page(struct page *page, size_t offset, size_t len)
470{
471 char *addr = kmap_atomic(page);
472 memset(addr + offset, 0, len);
473 kunmap_atomic(addr);
474}
475
Al Viro241699c2016-09-22 16:33:12 -0400476static inline bool allocated(struct pipe_buffer *buf)
477{
478 return buf->ops == &default_pipe_buf_ops;
479}
480
481static inline void data_start(const struct iov_iter *i, int *idxp, size_t *offp)
482{
483 size_t off = i->iov_offset;
484 int idx = i->idx;
485 if (off && (!allocated(&i->pipe->bufs[idx]) || off == PAGE_SIZE)) {
486 idx = next_idx(idx, i->pipe);
487 off = 0;
488 }
489 *idxp = idx;
490 *offp = off;
491}
492
493static size_t push_pipe(struct iov_iter *i, size_t size,
494 int *idxp, size_t *offp)
495{
496 struct pipe_inode_info *pipe = i->pipe;
497 size_t off;
498 int idx;
499 ssize_t left;
500
501 if (unlikely(size > i->count))
502 size = i->count;
503 if (unlikely(!size))
504 return 0;
505
506 left = size;
507 data_start(i, &idx, &off);
508 *idxp = idx;
509 *offp = off;
510 if (off) {
511 left -= PAGE_SIZE - off;
512 if (left <= 0) {
513 pipe->bufs[idx].len += size;
514 return size;
515 }
516 pipe->bufs[idx].len = PAGE_SIZE;
517 idx = next_idx(idx, pipe);
518 }
519 while (idx != pipe->curbuf || !pipe->nrbufs) {
520 struct page *page = alloc_page(GFP_USER);
521 if (!page)
522 break;
523 pipe->nrbufs++;
524 pipe->bufs[idx].ops = &default_pipe_buf_ops;
525 pipe->bufs[idx].page = page;
526 pipe->bufs[idx].offset = 0;
527 if (left <= PAGE_SIZE) {
528 pipe->bufs[idx].len = left;
529 return size;
530 }
531 pipe->bufs[idx].len = PAGE_SIZE;
532 left -= PAGE_SIZE;
533 idx = next_idx(idx, pipe);
534 }
535 return size - left;
536}
537
538static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
539 struct iov_iter *i)
540{
541 struct pipe_inode_info *pipe = i->pipe;
542 size_t n, off;
543 int idx;
544
545 if (!sanity(i))
546 return 0;
547
548 bytes = n = push_pipe(i, bytes, &idx, &off);
549 if (unlikely(!n))
550 return 0;
551 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
552 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
553 memcpy_to_page(pipe->bufs[idx].page, off, addr, chunk);
554 i->idx = idx;
555 i->iov_offset = off + chunk;
556 n -= chunk;
557 addr += chunk;
558 }
559 i->count -= bytes;
560 return bytes;
561}
562
Al Virof9152892018-11-27 22:32:59 -0500563static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
564 __wsum sum, size_t off)
565{
566 __wsum next = csum_partial_copy_nocheck(from, to, len, 0);
567 return csum_block_add(sum, next, off);
568}
569
Al Viro78e1f382018-11-25 16:24:16 -0500570static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
571 __wsum *csum, struct iov_iter *i)
572{
573 struct pipe_inode_info *pipe = i->pipe;
574 size_t n, r;
575 size_t off = 0;
Al Virof9152892018-11-27 22:32:59 -0500576 __wsum sum = *csum;
Al Viro78e1f382018-11-25 16:24:16 -0500577 int idx;
578
579 if (!sanity(i))
580 return 0;
581
582 bytes = n = push_pipe(i, bytes, &idx, &r);
583 if (unlikely(!n))
584 return 0;
585 for ( ; n; idx = next_idx(idx, pipe), r = 0) {
586 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
587 char *p = kmap_atomic(pipe->bufs[idx].page);
Al Virof9152892018-11-27 22:32:59 -0500588 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
Al Viro78e1f382018-11-25 16:24:16 -0500589 kunmap_atomic(p);
590 i->idx = idx;
591 i->iov_offset = r + chunk;
592 n -= chunk;
593 off += chunk;
594 addr += chunk;
595 }
596 i->count -= bytes;
597 *csum = sum;
598 return bytes;
599}
600
Al Viroaa28de22017-06-29 21:45:10 -0400601size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400602{
Al Viro36f7a8a2015-12-06 16:49:22 -0500603 const char *from = addr;
David Howells00e23702018-10-22 13:07:28 +0100604 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400605 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400606 if (iter_is_iovec(i))
607 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500608 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400609 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500610 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500611 (from += v.bv_len) - v.bv_len, v.bv_len),
612 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500613 )
Al Viro62a80672014-04-04 23:12:29 -0400614
Al Viro3d4d3e42014-11-27 14:28:06 -0500615 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400616}
Al Viroaa28de22017-06-29 21:45:10 -0400617EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400618
Dan Williams87803562018-05-03 17:06:31 -0700619#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
620static int copyout_mcsafe(void __user *to, const void *from, size_t n)
621{
622 if (access_ok(VERIFY_WRITE, to, n)) {
623 kasan_check_read(from, n);
624 n = copy_to_user_mcsafe((__force void *) to, from, n);
625 }
626 return n;
627}
628
629static unsigned long memcpy_mcsafe_to_page(struct page *page, size_t offset,
630 const char *from, size_t len)
631{
632 unsigned long ret;
633 char *to;
634
635 to = kmap_atomic(page);
636 ret = memcpy_mcsafe(to + offset, from, len);
637 kunmap_atomic(to);
638
639 return ret;
640}
641
Dan Williamsca146f62018-07-08 13:46:12 -0700642static size_t copy_pipe_to_iter_mcsafe(const void *addr, size_t bytes,
643 struct iov_iter *i)
644{
645 struct pipe_inode_info *pipe = i->pipe;
646 size_t n, off, xfer = 0;
647 int idx;
648
649 if (!sanity(i))
650 return 0;
651
652 bytes = n = push_pipe(i, bytes, &idx, &off);
653 if (unlikely(!n))
654 return 0;
655 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
656 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
657 unsigned long rem;
658
659 rem = memcpy_mcsafe_to_page(pipe->bufs[idx].page, off, addr,
660 chunk);
661 i->idx = idx;
662 i->iov_offset = off + chunk - rem;
663 xfer += chunk - rem;
664 if (rem)
665 break;
666 n -= chunk;
667 addr += chunk;
668 }
669 i->count -= xfer;
670 return xfer;
671}
672
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700673/**
674 * _copy_to_iter_mcsafe - copy to user with source-read error exception handling
675 * @addr: source kernel address
676 * @bytes: total transfer length
677 * @iter: destination iterator
678 *
679 * The pmem driver arranges for filesystem-dax to use this facility via
680 * dax_copy_to_iter() for protecting read/write to persistent memory.
681 * Unless / until an architecture can guarantee identical performance
682 * between _copy_to_iter_mcsafe() and _copy_to_iter() it would be a
683 * performance regression to switch more users to the mcsafe version.
684 *
685 * Otherwise, the main differences between this and typical _copy_to_iter().
686 *
687 * * Typical tail/residue handling after a fault retries the copy
688 * byte-by-byte until the fault happens again. Re-triggering machine
689 * checks is potentially fatal so the implementation uses source
690 * alignment and poison alignment assumptions to avoid re-triggering
691 * hardware exceptions.
692 *
693 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
694 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
695 * a short copy.
696 *
697 * See MCSAFE_TEST for self-test.
698 */
Dan Williams87803562018-05-03 17:06:31 -0700699size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i)
700{
701 const char *from = addr;
702 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
703
David Howells00e23702018-10-22 13:07:28 +0100704 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsca146f62018-07-08 13:46:12 -0700705 return copy_pipe_to_iter_mcsafe(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700706 if (iter_is_iovec(i))
707 might_fault();
708 iterate_and_advance(i, bytes, v,
709 copyout_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
710 ({
711 rem = memcpy_mcsafe_to_page(v.bv_page, v.bv_offset,
712 (from += v.bv_len) - v.bv_len, v.bv_len);
713 if (rem) {
714 curr_addr = (unsigned long) from;
715 bytes = curr_addr - s_addr - rem;
716 return bytes;
717 }
718 }),
719 ({
720 rem = memcpy_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len,
721 v.iov_len);
722 if (rem) {
723 curr_addr = (unsigned long) from;
724 bytes = curr_addr - s_addr - rem;
725 return bytes;
726 }
727 })
728 )
729
730 return bytes;
731}
732EXPORT_SYMBOL_GPL(_copy_to_iter_mcsafe);
733#endif /* CONFIG_ARCH_HAS_UACCESS_MCSAFE */
734
Al Viroaa28de22017-06-29 21:45:10 -0400735size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400736{
Al Viro0dbca9a2014-11-27 14:26:43 -0500737 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100738 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400739 WARN_ON(1);
740 return 0;
741 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400742 if (iter_is_iovec(i))
743 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500744 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400745 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500746 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500747 v.bv_offset, v.bv_len),
748 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500749 )
750
751 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400752}
Al Viroaa28de22017-06-29 21:45:10 -0400753EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400754
Al Viroaa28de22017-06-29 21:45:10 -0400755bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400756{
757 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100758 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400759 WARN_ON(1);
760 return false;
761 }
Al Viro33844e62016-12-21 21:55:02 -0500762 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400763 return false;
764
Al Viro09fc68dc2017-06-29 22:25:14 -0400765 if (iter_is_iovec(i))
766 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400767 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68dc2017-06-29 22:25:14 -0400768 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400769 v.iov_base, v.iov_len))
770 return false;
771 0;}),
772 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
773 v.bv_offset, v.bv_len),
774 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
775 )
776
777 iov_iter_advance(i, bytes);
778 return true;
779}
Al Viroaa28de22017-06-29 21:45:10 -0400780EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400781
Al Viroaa28de22017-06-29 21:45:10 -0400782size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500783{
784 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100785 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400786 WARN_ON(1);
787 return 0;
788 }
Al Viroaa583092014-11-27 20:27:08 -0500789 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400790 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500791 v.iov_base, v.iov_len),
792 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
793 v.bv_offset, v.bv_len),
794 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
795 )
796
797 return bytes;
798}
Al Viroaa28de22017-06-29 21:45:10 -0400799EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500800
Dan Williams0aed55a2017-05-29 12:22:50 -0700801#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700802/**
803 * _copy_from_iter_flushcache - write destination through cpu cache
804 * @addr: destination kernel address
805 * @bytes: total transfer length
806 * @iter: source iterator
807 *
808 * The pmem driver arranges for filesystem-dax to use this facility via
809 * dax_copy_from_iter() for ensuring that writes to persistent memory
810 * are flushed through the CPU cache. It is differentiated from
811 * _copy_from_iter_nocache() in that guarantees all data is flushed for
812 * all iterator types. The _copy_from_iter_nocache() only attempts to
813 * bypass the cache for the ITER_IOVEC case, and on some archs may use
814 * instructions that strand dirty-data in the cache.
815 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700816size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700817{
818 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100819 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700820 WARN_ON(1);
821 return 0;
822 }
823 iterate_and_advance(i, bytes, v,
824 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
825 v.iov_base, v.iov_len),
826 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
827 v.bv_offset, v.bv_len),
828 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
829 v.iov_len)
830 )
831
832 return bytes;
833}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700834EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700835#endif
836
Al Viroaa28de22017-06-29 21:45:10 -0400837bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400838{
839 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100840 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400841 WARN_ON(1);
842 return false;
843 }
Al Viro33844e62016-12-21 21:55:02 -0500844 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400845 return false;
846 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400847 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400848 v.iov_base, v.iov_len))
849 return false;
850 0;}),
851 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
852 v.bv_offset, v.bv_len),
853 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
854 )
855
856 iov_iter_advance(i, bytes);
857 return true;
858}
Al Viroaa28de22017-06-29 21:45:10 -0400859EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400860
Al Viro72e809e2017-06-29 21:52:57 -0400861static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
862{
Petar Penkova90bcb82017-08-29 11:20:32 -0700863 struct page *head = compound_head(page);
864 size_t v = n + offset + page_address(page) - page_address(head);
865
866 if (likely(n <= v && v <= (PAGE_SIZE << compound_order(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400867 return true;
868 WARN_ON(1);
869 return false;
870}
Al Virod2715242014-11-27 14:22:37 -0500871
872size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
873 struct iov_iter *i)
874{
Al Viro72e809e2017-06-29 21:52:57 -0400875 if (unlikely(!page_copy_sane(page, offset, bytes)))
876 return 0;
Al Virod2715242014-11-27 14:22:37 -0500877 if (i->type & (ITER_BVEC|ITER_KVEC)) {
878 void *kaddr = kmap_atomic(page);
879 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
880 kunmap_atomic(kaddr);
881 return wanted;
David Howells9ea9ce02018-10-20 00:57:56 +0100882 } else if (unlikely(iov_iter_is_discard(i)))
883 return bytes;
884 else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500885 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400886 else
887 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500888}
889EXPORT_SYMBOL(copy_page_to_iter);
890
891size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
892 struct iov_iter *i)
893{
Al Viro72e809e2017-06-29 21:52:57 -0400894 if (unlikely(!page_copy_sane(page, offset, bytes)))
895 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +0100896 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400897 WARN_ON(1);
898 return 0;
899 }
Al Virod2715242014-11-27 14:22:37 -0500900 if (i->type & (ITER_BVEC|ITER_KVEC)) {
901 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400902 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500903 kunmap_atomic(kaddr);
904 return wanted;
905 } else
906 return copy_page_from_iter_iovec(page, offset, bytes, i);
907}
908EXPORT_SYMBOL(copy_page_from_iter);
909
Al Viro241699c2016-09-22 16:33:12 -0400910static size_t pipe_zero(size_t bytes, struct iov_iter *i)
911{
912 struct pipe_inode_info *pipe = i->pipe;
913 size_t n, off;
914 int idx;
915
916 if (!sanity(i))
917 return 0;
918
919 bytes = n = push_pipe(i, bytes, &idx, &off);
920 if (unlikely(!n))
921 return 0;
922
923 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
924 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
925 memzero_page(pipe->bufs[idx].page, off, chunk);
926 i->idx = idx;
927 i->iov_offset = off + chunk;
928 n -= chunk;
929 }
930 i->count -= bytes;
931 return bytes;
932}
933
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400934size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
935{
David Howells00e23702018-10-22 13:07:28 +0100936 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400937 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500938 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400939 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500940 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
941 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500942 )
943
944 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400945}
946EXPORT_SYMBOL(iov_iter_zero);
947
Al Viro62a80672014-04-04 23:12:29 -0400948size_t iov_iter_copy_from_user_atomic(struct page *page,
949 struct iov_iter *i, unsigned long offset, size_t bytes)
950{
Al Viro04a31162014-11-27 13:51:41 -0500951 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400952 if (unlikely(!page_copy_sane(page, offset, bytes))) {
953 kunmap_atomic(kaddr);
954 return 0;
955 }
David Howells9ea9ce02018-10-20 00:57:56 +0100956 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400957 kunmap_atomic(kaddr);
958 WARN_ON(1);
959 return 0;
960 }
Al Viro04a31162014-11-27 13:51:41 -0500961 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400962 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -0500963 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500964 v.bv_offset, v.bv_len),
965 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -0500966 )
967 kunmap_atomic(kaddr);
968 return bytes;
Al Viro62a80672014-04-04 23:12:29 -0400969}
970EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
971
Al Virob9dc6f62017-01-14 19:33:08 -0500972static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -0400973{
974 struct pipe_inode_info *pipe = i->pipe;
Al Viro241699c2016-09-22 16:33:12 -0400975 if (pipe->nrbufs) {
Al Virob9dc6f62017-01-14 19:33:08 -0500976 size_t off = i->iov_offset;
977 int idx = i->idx;
978 int nrbufs = (idx - pipe->curbuf) & (pipe->buffers - 1);
979 if (off) {
980 pipe->bufs[idx].len = off - pipe->bufs[idx].offset;
981 idx = next_idx(idx, pipe);
982 nrbufs++;
983 }
984 while (pipe->nrbufs > nrbufs) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200985 pipe_buf_release(pipe, &pipe->bufs[idx]);
Al Viro241699c2016-09-22 16:33:12 -0400986 idx = next_idx(idx, pipe);
987 pipe->nrbufs--;
988 }
989 }
Al Virob9dc6f62017-01-14 19:33:08 -0500990}
991
992static void pipe_advance(struct iov_iter *i, size_t size)
993{
994 struct pipe_inode_info *pipe = i->pipe;
995 if (unlikely(i->count < size))
996 size = i->count;
997 if (size) {
998 struct pipe_buffer *buf;
999 size_t off = i->iov_offset, left = size;
1000 int idx = i->idx;
1001 if (off) /* make it relative to the beginning of buffer */
1002 left += off - pipe->bufs[idx].offset;
1003 while (1) {
1004 buf = &pipe->bufs[idx];
1005 if (left <= buf->len)
1006 break;
1007 left -= buf->len;
1008 idx = next_idx(idx, pipe);
1009 }
1010 i->idx = idx;
1011 i->iov_offset = buf->offset + left;
1012 }
1013 i->count -= size;
1014 /* ... and discard everything past that point */
1015 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001016}
1017
Al Viro62a80672014-04-04 23:12:29 -04001018void iov_iter_advance(struct iov_iter *i, size_t size)
1019{
David Howells00e23702018-10-22 13:07:28 +01001020 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001021 pipe_advance(i, size);
1022 return;
1023 }
David Howells9ea9ce02018-10-20 00:57:56 +01001024 if (unlikely(iov_iter_is_discard(i))) {
1025 i->count -= size;
1026 return;
1027 }
Al Viroa2804552014-11-27 14:48:42 -05001028 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -04001029}
1030EXPORT_SYMBOL(iov_iter_advance);
1031
Al Viro27c0e372017-02-17 18:42:24 -05001032void iov_iter_revert(struct iov_iter *i, size_t unroll)
1033{
1034 if (!unroll)
1035 return;
Al Viro5b47d592017-05-08 13:54:47 -04001036 if (WARN_ON(unroll > MAX_RW_COUNT))
1037 return;
Al Viro27c0e372017-02-17 18:42:24 -05001038 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001039 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001040 struct pipe_inode_info *pipe = i->pipe;
1041 int idx = i->idx;
1042 size_t off = i->iov_offset;
1043 while (1) {
1044 size_t n = off - pipe->bufs[idx].offset;
1045 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001046 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001047 break;
1048 }
1049 unroll -= n;
1050 if (!unroll && idx == i->start_idx) {
1051 off = 0;
1052 break;
1053 }
1054 if (!idx--)
1055 idx = pipe->buffers - 1;
1056 off = pipe->bufs[idx].offset + pipe->bufs[idx].len;
1057 }
1058 i->iov_offset = off;
1059 i->idx = idx;
1060 pipe_truncate(i);
1061 return;
1062 }
David Howells9ea9ce02018-10-20 00:57:56 +01001063 if (unlikely(iov_iter_is_discard(i)))
1064 return;
Al Viro27c0e372017-02-17 18:42:24 -05001065 if (unroll <= i->iov_offset) {
1066 i->iov_offset -= unroll;
1067 return;
1068 }
1069 unroll -= i->iov_offset;
David Howells00e23702018-10-22 13:07:28 +01001070 if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001071 const struct bio_vec *bvec = i->bvec;
1072 while (1) {
1073 size_t n = (--bvec)->bv_len;
1074 i->nr_segs++;
1075 if (unroll <= n) {
1076 i->bvec = bvec;
1077 i->iov_offset = n - unroll;
1078 return;
1079 }
1080 unroll -= n;
1081 }
1082 } else { /* same logics for iovec and kvec */
1083 const struct iovec *iov = i->iov;
1084 while (1) {
1085 size_t n = (--iov)->iov_len;
1086 i->nr_segs++;
1087 if (unroll <= n) {
1088 i->iov = iov;
1089 i->iov_offset = n - unroll;
1090 return;
1091 }
1092 unroll -= n;
1093 }
1094 }
1095}
1096EXPORT_SYMBOL(iov_iter_revert);
1097
Al Viro62a80672014-04-04 23:12:29 -04001098/*
1099 * Return the count of just the current iov_iter segment.
1100 */
1101size_t iov_iter_single_seg_count(const struct iov_iter *i)
1102{
David Howells00e23702018-10-22 13:07:28 +01001103 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001104 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001105 if (i->nr_segs == 1)
1106 return i->count;
David Howells9ea9ce02018-10-20 00:57:56 +01001107 if (unlikely(iov_iter_is_discard(i)))
1108 return i->count;
David Howells00e23702018-10-22 13:07:28 +01001109 else if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001110 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001111 else
1112 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001113}
1114EXPORT_SYMBOL(iov_iter_single_seg_count);
1115
David Howellsaa563d72018-10-20 00:57:56 +01001116void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001117 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001118 size_t count)
1119{
David Howellsaa563d72018-10-20 00:57:56 +01001120 WARN_ON(direction & ~(READ | WRITE));
1121 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001122 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001123 i->nr_segs = nr_segs;
1124 i->iov_offset = 0;
1125 i->count = count;
1126}
1127EXPORT_SYMBOL(iov_iter_kvec);
1128
David Howellsaa563d72018-10-20 00:57:56 +01001129void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001130 const struct bio_vec *bvec, unsigned long nr_segs,
1131 size_t count)
1132{
David Howellsaa563d72018-10-20 00:57:56 +01001133 WARN_ON(direction & ~(READ | WRITE));
1134 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001135 i->bvec = bvec;
1136 i->nr_segs = nr_segs;
1137 i->iov_offset = 0;
1138 i->count = count;
1139}
1140EXPORT_SYMBOL(iov_iter_bvec);
1141
David Howellsaa563d72018-10-20 00:57:56 +01001142void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001143 struct pipe_inode_info *pipe,
1144 size_t count)
1145{
David Howellsaa563d72018-10-20 00:57:56 +01001146 BUG_ON(direction != READ);
Al Virob9dc6f62017-01-14 19:33:08 -05001147 WARN_ON(pipe->nrbufs == pipe->buffers);
David Howellsaa563d72018-10-20 00:57:56 +01001148 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001149 i->pipe = pipe;
1150 i->idx = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1151 i->iov_offset = 0;
1152 i->count = count;
Al Viro27c0e372017-02-17 18:42:24 -05001153 i->start_idx = i->idx;
Al Viro241699c2016-09-22 16:33:12 -04001154}
1155EXPORT_SYMBOL(iov_iter_pipe);
1156
David Howells9ea9ce02018-10-20 00:57:56 +01001157/**
1158 * iov_iter_discard - Initialise an I/O iterator that discards data
1159 * @i: The iterator to initialise.
1160 * @direction: The direction of the transfer.
1161 * @count: The size of the I/O buffer in bytes.
1162 *
1163 * Set up an I/O iterator that just discards everything that's written to it.
1164 * It's only available as a READ iterator.
1165 */
1166void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1167{
1168 BUG_ON(direction != READ);
1169 i->type = ITER_DISCARD | READ;
1170 i->count = count;
1171 i->iov_offset = 0;
1172}
1173EXPORT_SYMBOL(iov_iter_discard);
1174
Al Viro62a80672014-04-04 23:12:29 -04001175unsigned long iov_iter_alignment(const struct iov_iter *i)
1176{
Al Viro04a31162014-11-27 13:51:41 -05001177 unsigned long res = 0;
1178 size_t size = i->count;
1179
David Howells00e23702018-10-22 13:07:28 +01001180 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro33844e62016-12-21 21:55:02 -05001181 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->idx]))
Al Viro241699c2016-09-22 16:33:12 -04001182 return size | i->iov_offset;
1183 return size;
1184 }
Al Viro04a31162014-11-27 13:51:41 -05001185 iterate_all_kinds(i, size, v,
1186 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001187 res |= v.bv_offset | v.bv_len,
1188 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -05001189 )
1190 return res;
Al Viro62a80672014-04-04 23:12:29 -04001191}
1192EXPORT_SYMBOL(iov_iter_alignment);
1193
Al Viro357f4352016-04-08 19:05:19 -04001194unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1195{
Al Viro33844e62016-12-21 21:55:02 -05001196 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001197 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001198
David Howells9ea9ce02018-10-20 00:57:56 +01001199 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001200 WARN_ON(1);
1201 return ~0U;
1202 }
1203
Al Viro357f4352016-04-08 19:05:19 -04001204 iterate_all_kinds(i, size, v,
1205 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1206 (size != v.iov_len ? size : 0), 0),
1207 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1208 (size != v.bv_len ? size : 0)),
1209 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1210 (size != v.iov_len ? size : 0))
1211 );
Al Viro33844e62016-12-21 21:55:02 -05001212 return res;
Al Viro357f4352016-04-08 19:05:19 -04001213}
1214EXPORT_SYMBOL(iov_iter_gap_alignment);
1215
Ilya Dryomove76b63122018-05-02 20:16:56 +02001216static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001217 size_t maxsize,
1218 struct page **pages,
1219 int idx,
1220 size_t *start)
1221{
1222 struct pipe_inode_info *pipe = i->pipe;
Al Viro1689c732016-10-11 18:21:14 +01001223 ssize_t n = push_pipe(i, maxsize, &idx, start);
Al Viro241699c2016-09-22 16:33:12 -04001224 if (!n)
1225 return -EFAULT;
1226
1227 maxsize = n;
1228 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001229 while (n > 0) {
Al Viro241699c2016-09-22 16:33:12 -04001230 get_page(*pages++ = pipe->bufs[idx].page);
1231 idx = next_idx(idx, pipe);
1232 n -= PAGE_SIZE;
1233 }
1234
1235 return maxsize;
1236}
1237
1238static ssize_t pipe_get_pages(struct iov_iter *i,
1239 struct page **pages, size_t maxsize, unsigned maxpages,
1240 size_t *start)
1241{
1242 unsigned npages;
1243 size_t capacity;
1244 int idx;
1245
Al Viro33844e62016-12-21 21:55:02 -05001246 if (!maxsize)
1247 return 0;
1248
Al Viro241699c2016-09-22 16:33:12 -04001249 if (!sanity(i))
1250 return -EFAULT;
1251
1252 data_start(i, &idx, start);
1253 /* some of this one + all after this one */
1254 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1255 capacity = min(npages,maxpages) * PAGE_SIZE - *start;
1256
1257 return __pipe_get_pages(i, min(maxsize, capacity), pages, idx, start);
1258}
1259
Al Viro62a80672014-04-04 23:12:29 -04001260ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001261 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001262 size_t *start)
1263{
Al Viroe5393fa2014-11-27 14:12:09 -05001264 if (maxsize > i->count)
1265 maxsize = i->count;
1266
David Howells00e23702018-10-22 13:07:28 +01001267 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001268 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001269 if (unlikely(iov_iter_is_discard(i)))
1270 return -EFAULT;
1271
Al Viroe5393fa2014-11-27 14:12:09 -05001272 iterate_all_kinds(i, maxsize, v, ({
1273 unsigned long addr = (unsigned long)v.iov_base;
1274 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1275 int n;
1276 int res;
1277
1278 if (len > maxpages * PAGE_SIZE)
1279 len = maxpages * PAGE_SIZE;
1280 addr &= ~(PAGE_SIZE - 1);
1281 n = DIV_ROUND_UP(len, PAGE_SIZE);
David Howells00e23702018-10-22 13:07:28 +01001282 res = get_user_pages_fast(addr, n, iov_iter_rw(i) != WRITE, pages);
Al Viroe5393fa2014-11-27 14:12:09 -05001283 if (unlikely(res < 0))
1284 return res;
1285 return (res == n ? len : res * PAGE_SIZE) - *start;
1286 0;}),({
1287 /* can't be more than PAGE_SIZE */
1288 *start = v.bv_offset;
1289 get_page(*pages = v.bv_page);
1290 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001291 }),({
1292 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001293 })
1294 )
1295 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001296}
1297EXPORT_SYMBOL(iov_iter_get_pages);
1298
Al Viro1b17f1f2014-11-27 14:14:31 -05001299static struct page **get_pages_array(size_t n)
1300{
Michal Hocko752ade62017-05-08 15:57:27 -07001301 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001302}
1303
Al Viro241699c2016-09-22 16:33:12 -04001304static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1305 struct page ***pages, size_t maxsize,
1306 size_t *start)
1307{
1308 struct page **p;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001309 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001310 int idx;
1311 int npages;
1312
Al Viro33844e62016-12-21 21:55:02 -05001313 if (!maxsize)
1314 return 0;
1315
Al Viro241699c2016-09-22 16:33:12 -04001316 if (!sanity(i))
1317 return -EFAULT;
1318
1319 data_start(i, &idx, start);
1320 /* some of this one + all after this one */
1321 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1322 n = npages * PAGE_SIZE - *start;
1323 if (maxsize > n)
1324 maxsize = n;
1325 else
1326 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1327 p = get_pages_array(npages);
1328 if (!p)
1329 return -ENOMEM;
1330 n = __pipe_get_pages(i, maxsize, p, idx, start);
1331 if (n > 0)
1332 *pages = p;
1333 else
1334 kvfree(p);
1335 return n;
1336}
1337
Al Viro62a80672014-04-04 23:12:29 -04001338ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1339 struct page ***pages, size_t maxsize,
1340 size_t *start)
1341{
Al Viro1b17f1f2014-11-27 14:14:31 -05001342 struct page **p;
1343
1344 if (maxsize > i->count)
1345 maxsize = i->count;
1346
David Howells00e23702018-10-22 13:07:28 +01001347 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001348 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001349 if (unlikely(iov_iter_is_discard(i)))
1350 return -EFAULT;
1351
Al Viro1b17f1f2014-11-27 14:14:31 -05001352 iterate_all_kinds(i, maxsize, v, ({
1353 unsigned long addr = (unsigned long)v.iov_base;
1354 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1355 int n;
1356 int res;
1357
1358 addr &= ~(PAGE_SIZE - 1);
1359 n = DIV_ROUND_UP(len, PAGE_SIZE);
1360 p = get_pages_array(n);
1361 if (!p)
1362 return -ENOMEM;
David Howells00e23702018-10-22 13:07:28 +01001363 res = get_user_pages_fast(addr, n, iov_iter_rw(i) != WRITE, p);
Al Viro1b17f1f2014-11-27 14:14:31 -05001364 if (unlikely(res < 0)) {
1365 kvfree(p);
1366 return res;
1367 }
1368 *pages = p;
1369 return (res == n ? len : res * PAGE_SIZE) - *start;
1370 0;}),({
1371 /* can't be more than PAGE_SIZE */
1372 *start = v.bv_offset;
1373 *pages = p = get_pages_array(1);
1374 if (!p)
1375 return -ENOMEM;
1376 get_page(*p = v.bv_page);
1377 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001378 }),({
1379 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -05001380 })
1381 )
1382 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001383}
1384EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1385
Al Viroa604ec72014-11-24 01:08:00 -05001386size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1387 struct iov_iter *i)
1388{
1389 char *to = addr;
1390 __wsum sum, next;
1391 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001392 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001393 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001394 WARN_ON(1);
1395 return 0;
1396 }
Al Viroa604ec72014-11-24 01:08:00 -05001397 iterate_and_advance(i, bytes, v, ({
1398 int err = 0;
Al Virocbbd26b2016-11-01 22:09:04 -04001399 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001400 (to += v.iov_len) - v.iov_len,
1401 v.iov_len, 0, &err);
1402 if (!err) {
1403 sum = csum_block_add(sum, next, off);
1404 off += v.iov_len;
1405 }
1406 err ? v.iov_len : 0;
1407 }), ({
1408 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001409 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1410 p + v.bv_offset, v.bv_len,
1411 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001412 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001413 off += v.bv_len;
1414 }),({
Al Virof9152892018-11-27 22:32:59 -05001415 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1416 v.iov_base, v.iov_len,
1417 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001418 off += v.iov_len;
1419 })
1420 )
1421 *csum = sum;
1422 return bytes;
1423}
1424EXPORT_SYMBOL(csum_and_copy_from_iter);
1425
Al Virocbbd26b2016-11-01 22:09:04 -04001426bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1427 struct iov_iter *i)
1428{
1429 char *to = addr;
1430 __wsum sum, next;
1431 size_t off = 0;
1432 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001433 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001434 WARN_ON(1);
1435 return false;
1436 }
1437 if (unlikely(i->count < bytes))
1438 return false;
1439 iterate_all_kinds(i, bytes, v, ({
1440 int err = 0;
1441 next = csum_and_copy_from_user(v.iov_base,
1442 (to += v.iov_len) - v.iov_len,
1443 v.iov_len, 0, &err);
1444 if (err)
1445 return false;
1446 sum = csum_block_add(sum, next, off);
1447 off += v.iov_len;
1448 0;
1449 }), ({
1450 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001451 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1452 p + v.bv_offset, v.bv_len,
1453 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001454 kunmap_atomic(p);
Al Virocbbd26b2016-11-01 22:09:04 -04001455 off += v.bv_len;
1456 }),({
Al Virof9152892018-11-27 22:32:59 -05001457 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1458 v.iov_base, v.iov_len,
1459 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001460 off += v.iov_len;
1461 })
1462 )
1463 *csum = sum;
1464 iov_iter_advance(i, bytes);
1465 return true;
1466}
1467EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1468
Al Viro36f7a8a2015-12-06 16:49:22 -05001469size_t csum_and_copy_to_iter(const void *addr, size_t bytes, __wsum *csum,
Al Viroa604ec72014-11-24 01:08:00 -05001470 struct iov_iter *i)
1471{
Al Viro36f7a8a2015-12-06 16:49:22 -05001472 const char *from = addr;
Al Viroa604ec72014-11-24 01:08:00 -05001473 __wsum sum, next;
1474 size_t off = 0;
Al Viro78e1f382018-11-25 16:24:16 -05001475
1476 if (unlikely(iov_iter_is_pipe(i)))
1477 return csum_and_copy_to_pipe_iter(addr, bytes, csum, i);
1478
Al Viroa604ec72014-11-24 01:08:00 -05001479 sum = *csum;
Al Viro78e1f382018-11-25 16:24:16 -05001480 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001481 WARN_ON(1); /* for now */
1482 return 0;
1483 }
Al Viroa604ec72014-11-24 01:08:00 -05001484 iterate_and_advance(i, bytes, v, ({
1485 int err = 0;
1486 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001487 v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001488 v.iov_len, 0, &err);
1489 if (!err) {
1490 sum = csum_block_add(sum, next, off);
1491 off += v.iov_len;
1492 }
1493 err ? v.iov_len : 0;
1494 }), ({
1495 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001496 sum = csum_and_memcpy(p + v.bv_offset,
1497 (from += v.bv_len) - v.bv_len,
1498 v.bv_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001499 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001500 off += v.bv_len;
1501 }),({
Al Virof9152892018-11-27 22:32:59 -05001502 sum = csum_and_memcpy(v.iov_base,
1503 (from += v.iov_len) - v.iov_len,
1504 v.iov_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001505 off += v.iov_len;
1506 })
1507 )
1508 *csum = sum;
1509 return bytes;
1510}
1511EXPORT_SYMBOL(csum_and_copy_to_iter);
1512
Al Viro62a80672014-04-04 23:12:29 -04001513int iov_iter_npages(const struct iov_iter *i, int maxpages)
1514{
Al Viroe0f2dc42014-11-27 14:09:46 -05001515 size_t size = i->count;
1516 int npages = 0;
1517
1518 if (!size)
1519 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001520 if (unlikely(iov_iter_is_discard(i)))
1521 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001522
David Howells00e23702018-10-22 13:07:28 +01001523 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001524 struct pipe_inode_info *pipe = i->pipe;
1525 size_t off;
1526 int idx;
1527
1528 if (!sanity(i))
1529 return 0;
1530
1531 data_start(i, &idx, &off);
1532 /* some of this one + all after this one */
1533 npages = ((pipe->curbuf - idx - 1) & (pipe->buffers - 1)) + 1;
1534 if (npages >= maxpages)
1535 return maxpages;
1536 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001537 unsigned long p = (unsigned long)v.iov_base;
1538 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1539 - p / PAGE_SIZE;
1540 if (npages >= maxpages)
1541 return maxpages;
1542 0;}),({
1543 npages++;
1544 if (npages >= maxpages)
1545 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001546 }),({
1547 unsigned long p = (unsigned long)v.iov_base;
1548 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1549 - p / PAGE_SIZE;
1550 if (npages >= maxpages)
1551 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001552 })
1553 )
1554 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001555}
Al Virof67da302014-03-19 01:16:16 -04001556EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001557
1558const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1559{
1560 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001561 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001562 WARN_ON(1);
1563 return NULL;
1564 }
David Howells9ea9ce02018-10-20 00:57:56 +01001565 if (unlikely(iov_iter_is_discard(new)))
1566 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001567 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001568 return new->bvec = kmemdup(new->bvec,
1569 new->nr_segs * sizeof(struct bio_vec),
1570 flags);
1571 else
1572 /* iovec and kvec have identical layout */
1573 return new->iov = kmemdup(new->iov,
1574 new->nr_segs * sizeof(struct iovec),
1575 flags);
1576}
1577EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001578
Vegard Nossumffecee42016-10-08 11:18:07 +02001579/**
1580 * import_iovec() - Copy an array of &struct iovec from userspace
1581 * into the kernel, check that it is valid, and initialize a new
1582 * &struct iov_iter iterator to access it.
1583 *
1584 * @type: One of %READ or %WRITE.
1585 * @uvector: Pointer to the userspace array.
1586 * @nr_segs: Number of elements in userspace array.
1587 * @fast_segs: Number of elements in @iov.
1588 * @iov: (input and output parameter) Pointer to pointer to (usually small
1589 * on-stack) kernel array.
1590 * @i: Pointer to iterator that will be initialized on success.
1591 *
1592 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1593 * then this function places %NULL in *@iov on return. Otherwise, a new
1594 * array will be allocated and the result placed in *@iov. This means that
1595 * the caller may call kfree() on *@iov regardless of whether the small
1596 * on-stack array was used or not (and regardless of whether this function
1597 * returns an error or not).
1598 *
1599 * Return: 0 on success or negative error code on error.
1600 */
Al Virobc917be2015-03-21 17:45:43 -04001601int import_iovec(int type, const struct iovec __user * uvector,
1602 unsigned nr_segs, unsigned fast_segs,
1603 struct iovec **iov, struct iov_iter *i)
1604{
1605 ssize_t n;
1606 struct iovec *p;
1607 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1608 *iov, &p);
1609 if (n < 0) {
1610 if (p != *iov)
1611 kfree(p);
1612 *iov = NULL;
1613 return n;
1614 }
1615 iov_iter_init(i, type, p, nr_segs, n);
1616 *iov = p == *iov ? NULL : p;
1617 return 0;
1618}
1619EXPORT_SYMBOL(import_iovec);
1620
1621#ifdef CONFIG_COMPAT
1622#include <linux/compat.h>
1623
1624int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
1625 unsigned nr_segs, unsigned fast_segs,
1626 struct iovec **iov, struct iov_iter *i)
1627{
1628 ssize_t n;
1629 struct iovec *p;
1630 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1631 *iov, &p);
1632 if (n < 0) {
1633 if (p != *iov)
1634 kfree(p);
1635 *iov = NULL;
1636 return n;
1637 }
1638 iov_iter_init(i, type, p, nr_segs, n);
1639 *iov = p == *iov ? NULL : p;
1640 return 0;
1641}
1642#endif
1643
1644int import_single_range(int rw, void __user *buf, size_t len,
1645 struct iovec *iov, struct iov_iter *i)
1646{
1647 if (len > MAX_RW_COUNT)
1648 len = MAX_RW_COUNT;
1649 if (unlikely(!access_ok(!rw, buf, len)))
1650 return -EFAULT;
1651
1652 iov->iov_base = buf;
1653 iov->iov_len = len;
1654 iov_iter_init(i, rw, iov, 1, len);
1655 return 0;
1656}
Al Viroe1267582015-12-06 20:38:56 -05001657EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05001658
1659int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1660 int (*f)(struct kvec *vec, void *context),
1661 void *context)
1662{
1663 struct kvec w;
1664 int err = -EINVAL;
1665 if (!bytes)
1666 return 0;
1667
1668 iterate_all_kinds(i, bytes, v, -EINVAL, ({
1669 w.iov_base = kmap(v.bv_page) + v.bv_offset;
1670 w.iov_len = v.bv_len;
1671 err = f(&w, context);
1672 kunmap(v.bv_page);
1673 err;}), ({
1674 w = v;
1675 err = f(&w, context);})
1676 )
1677 return err;
1678}
1679EXPORT_SYMBOL(iov_iter_for_each_range);