blob: 7ebccb5c16377eb77176d0df689e1cfb7af7b36d [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 Viro4bce9f62016-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 Viroaa28de22017-06-29 21:45:10 -0400563size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400564{
Al Viro36f7a8a2015-12-06 16:49:22 -0500565 const char *from = addr;
David Howells00e23702018-10-22 13:07:28 +0100566 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400567 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400568 if (iter_is_iovec(i))
569 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500570 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400571 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500572 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500573 (from += v.bv_len) - v.bv_len, v.bv_len),
574 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500575 )
Al Viro62a80672014-04-04 23:12:29 -0400576
Al Viro3d4d3e42014-11-27 14:28:06 -0500577 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400578}
Al Viroaa28de22017-06-29 21:45:10 -0400579EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400580
Dan Williams87803562018-05-03 17:06:31 -0700581#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
582static int copyout_mcsafe(void __user *to, const void *from, size_t n)
583{
584 if (access_ok(VERIFY_WRITE, to, n)) {
585 kasan_check_read(from, n);
586 n = copy_to_user_mcsafe((__force void *) to, from, n);
587 }
588 return n;
589}
590
591static unsigned long memcpy_mcsafe_to_page(struct page *page, size_t offset,
592 const char *from, size_t len)
593{
594 unsigned long ret;
595 char *to;
596
597 to = kmap_atomic(page);
598 ret = memcpy_mcsafe(to + offset, from, len);
599 kunmap_atomic(to);
600
601 return ret;
602}
603
Dan Williamsca146f62018-07-08 13:46:12 -0700604static size_t copy_pipe_to_iter_mcsafe(const void *addr, size_t bytes,
605 struct iov_iter *i)
606{
607 struct pipe_inode_info *pipe = i->pipe;
608 size_t n, off, xfer = 0;
609 int idx;
610
611 if (!sanity(i))
612 return 0;
613
614 bytes = n = push_pipe(i, bytes, &idx, &off);
615 if (unlikely(!n))
616 return 0;
617 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
618 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
619 unsigned long rem;
620
621 rem = memcpy_mcsafe_to_page(pipe->bufs[idx].page, off, addr,
622 chunk);
623 i->idx = idx;
624 i->iov_offset = off + chunk - rem;
625 xfer += chunk - rem;
626 if (rem)
627 break;
628 n -= chunk;
629 addr += chunk;
630 }
631 i->count -= xfer;
632 return xfer;
633}
634
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700635/**
636 * _copy_to_iter_mcsafe - copy to user with source-read error exception handling
637 * @addr: source kernel address
638 * @bytes: total transfer length
639 * @iter: destination iterator
640 *
641 * The pmem driver arranges for filesystem-dax to use this facility via
642 * dax_copy_to_iter() for protecting read/write to persistent memory.
643 * Unless / until an architecture can guarantee identical performance
644 * between _copy_to_iter_mcsafe() and _copy_to_iter() it would be a
645 * performance regression to switch more users to the mcsafe version.
646 *
647 * Otherwise, the main differences between this and typical _copy_to_iter().
648 *
649 * * Typical tail/residue handling after a fault retries the copy
650 * byte-by-byte until the fault happens again. Re-triggering machine
651 * checks is potentially fatal so the implementation uses source
652 * alignment and poison alignment assumptions to avoid re-triggering
653 * hardware exceptions.
654 *
655 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
656 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
657 * a short copy.
658 *
659 * See MCSAFE_TEST for self-test.
660 */
Dan Williams87803562018-05-03 17:06:31 -0700661size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i)
662{
663 const char *from = addr;
664 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
665
David Howells00e23702018-10-22 13:07:28 +0100666 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsca146f62018-07-08 13:46:12 -0700667 return copy_pipe_to_iter_mcsafe(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700668 if (iter_is_iovec(i))
669 might_fault();
670 iterate_and_advance(i, bytes, v,
671 copyout_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
672 ({
673 rem = memcpy_mcsafe_to_page(v.bv_page, v.bv_offset,
674 (from += v.bv_len) - v.bv_len, v.bv_len);
675 if (rem) {
676 curr_addr = (unsigned long) from;
677 bytes = curr_addr - s_addr - rem;
678 return bytes;
679 }
680 }),
681 ({
682 rem = memcpy_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len,
683 v.iov_len);
684 if (rem) {
685 curr_addr = (unsigned long) from;
686 bytes = curr_addr - s_addr - rem;
687 return bytes;
688 }
689 })
690 )
691
692 return bytes;
693}
694EXPORT_SYMBOL_GPL(_copy_to_iter_mcsafe);
695#endif /* CONFIG_ARCH_HAS_UACCESS_MCSAFE */
696
Al Viroaa28de22017-06-29 21:45:10 -0400697size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400698{
Al Viro0dbca9a2014-11-27 14:26:43 -0500699 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100700 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400701 WARN_ON(1);
702 return 0;
703 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400704 if (iter_is_iovec(i))
705 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500706 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400707 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500708 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500709 v.bv_offset, v.bv_len),
710 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500711 )
712
713 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400714}
Al Viroaa28de22017-06-29 21:45:10 -0400715EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400716
Al Viroaa28de22017-06-29 21:45:10 -0400717bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400718{
719 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100720 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400721 WARN_ON(1);
722 return false;
723 }
Al Viro33844e62016-12-21 21:55:02 -0500724 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400725 return false;
726
Al Viro09fc68dc2017-06-29 22:25:14 -0400727 if (iter_is_iovec(i))
728 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400729 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68dc2017-06-29 22:25:14 -0400730 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400731 v.iov_base, v.iov_len))
732 return false;
733 0;}),
734 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
735 v.bv_offset, v.bv_len),
736 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
737 )
738
739 iov_iter_advance(i, bytes);
740 return true;
741}
Al Viroaa28de22017-06-29 21:45:10 -0400742EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400743
Al Viroaa28de22017-06-29 21:45:10 -0400744size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500745{
746 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100747 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400748 WARN_ON(1);
749 return 0;
750 }
Al Viroaa583092014-11-27 20:27:08 -0500751 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400752 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500753 v.iov_base, v.iov_len),
754 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
755 v.bv_offset, v.bv_len),
756 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
757 )
758
759 return bytes;
760}
Al Viroaa28de22017-06-29 21:45:10 -0400761EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500762
Dan Williams0aed55a2017-05-29 12:22:50 -0700763#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700764/**
765 * _copy_from_iter_flushcache - write destination through cpu cache
766 * @addr: destination kernel address
767 * @bytes: total transfer length
768 * @iter: source iterator
769 *
770 * The pmem driver arranges for filesystem-dax to use this facility via
771 * dax_copy_from_iter() for ensuring that writes to persistent memory
772 * are flushed through the CPU cache. It is differentiated from
773 * _copy_from_iter_nocache() in that guarantees all data is flushed for
774 * all iterator types. The _copy_from_iter_nocache() only attempts to
775 * bypass the cache for the ITER_IOVEC case, and on some archs may use
776 * instructions that strand dirty-data in the cache.
777 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700778size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700779{
780 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100781 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700782 WARN_ON(1);
783 return 0;
784 }
785 iterate_and_advance(i, bytes, v,
786 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
787 v.iov_base, v.iov_len),
788 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
789 v.bv_offset, v.bv_len),
790 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
791 v.iov_len)
792 )
793
794 return bytes;
795}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700796EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700797#endif
798
Al Viroaa28de22017-06-29 21:45:10 -0400799bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400800{
801 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100802 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400803 WARN_ON(1);
804 return false;
805 }
Al Viro33844e62016-12-21 21:55:02 -0500806 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400807 return false;
808 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400809 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400810 v.iov_base, v.iov_len))
811 return false;
812 0;}),
813 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
814 v.bv_offset, v.bv_len),
815 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
816 )
817
818 iov_iter_advance(i, bytes);
819 return true;
820}
Al Viroaa28de22017-06-29 21:45:10 -0400821EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400822
Al Viro72e809e2017-06-29 21:52:57 -0400823static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
824{
Petar Penkova90bcb82017-08-29 11:20:32 -0700825 struct page *head = compound_head(page);
826 size_t v = n + offset + page_address(page) - page_address(head);
827
828 if (likely(n <= v && v <= (PAGE_SIZE << compound_order(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400829 return true;
830 WARN_ON(1);
831 return false;
832}
Al Virod2715242014-11-27 14:22:37 -0500833
834size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
835 struct iov_iter *i)
836{
Al Viro72e809e2017-06-29 21:52:57 -0400837 if (unlikely(!page_copy_sane(page, offset, bytes)))
838 return 0;
Al Virod2715242014-11-27 14:22:37 -0500839 if (i->type & (ITER_BVEC|ITER_KVEC)) {
840 void *kaddr = kmap_atomic(page);
841 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
842 kunmap_atomic(kaddr);
843 return wanted;
David Howells9ea9ce02018-10-20 00:57:56 +0100844 } else if (unlikely(iov_iter_is_discard(i)))
845 return bytes;
846 else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500847 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400848 else
849 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500850}
851EXPORT_SYMBOL(copy_page_to_iter);
852
853size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
854 struct iov_iter *i)
855{
Al Viro72e809e2017-06-29 21:52:57 -0400856 if (unlikely(!page_copy_sane(page, offset, bytes)))
857 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +0100858 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400859 WARN_ON(1);
860 return 0;
861 }
Al Virod2715242014-11-27 14:22:37 -0500862 if (i->type & (ITER_BVEC|ITER_KVEC)) {
863 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400864 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500865 kunmap_atomic(kaddr);
866 return wanted;
867 } else
868 return copy_page_from_iter_iovec(page, offset, bytes, i);
869}
870EXPORT_SYMBOL(copy_page_from_iter);
871
Al Viro241699c2016-09-22 16:33:12 -0400872static size_t pipe_zero(size_t bytes, struct iov_iter *i)
873{
874 struct pipe_inode_info *pipe = i->pipe;
875 size_t n, off;
876 int idx;
877
878 if (!sanity(i))
879 return 0;
880
881 bytes = n = push_pipe(i, bytes, &idx, &off);
882 if (unlikely(!n))
883 return 0;
884
885 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
886 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
887 memzero_page(pipe->bufs[idx].page, off, chunk);
888 i->idx = idx;
889 i->iov_offset = off + chunk;
890 n -= chunk;
891 }
892 i->count -= bytes;
893 return bytes;
894}
895
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400896size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
897{
David Howells00e23702018-10-22 13:07:28 +0100898 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400899 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500900 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400901 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500902 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
903 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500904 )
905
906 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400907}
908EXPORT_SYMBOL(iov_iter_zero);
909
Al Viro62a80672014-04-04 23:12:29 -0400910size_t iov_iter_copy_from_user_atomic(struct page *page,
911 struct iov_iter *i, unsigned long offset, size_t bytes)
912{
Al Viro04a31162014-11-27 13:51:41 -0500913 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400914 if (unlikely(!page_copy_sane(page, offset, bytes))) {
915 kunmap_atomic(kaddr);
916 return 0;
917 }
David Howells9ea9ce02018-10-20 00:57:56 +0100918 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400919 kunmap_atomic(kaddr);
920 WARN_ON(1);
921 return 0;
922 }
Al Viro04a31162014-11-27 13:51:41 -0500923 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400924 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -0500925 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500926 v.bv_offset, v.bv_len),
927 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -0500928 )
929 kunmap_atomic(kaddr);
930 return bytes;
Al Viro62a80672014-04-04 23:12:29 -0400931}
932EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
933
Al Virob9dc6f62017-01-14 19:33:08 -0500934static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -0400935{
936 struct pipe_inode_info *pipe = i->pipe;
Al Viro241699c2016-09-22 16:33:12 -0400937 if (pipe->nrbufs) {
Al Virob9dc6f62017-01-14 19:33:08 -0500938 size_t off = i->iov_offset;
939 int idx = i->idx;
940 int nrbufs = (idx - pipe->curbuf) & (pipe->buffers - 1);
941 if (off) {
942 pipe->bufs[idx].len = off - pipe->bufs[idx].offset;
943 idx = next_idx(idx, pipe);
944 nrbufs++;
945 }
946 while (pipe->nrbufs > nrbufs) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200947 pipe_buf_release(pipe, &pipe->bufs[idx]);
Al Viro241699c2016-09-22 16:33:12 -0400948 idx = next_idx(idx, pipe);
949 pipe->nrbufs--;
950 }
951 }
Al Virob9dc6f62017-01-14 19:33:08 -0500952}
953
954static void pipe_advance(struct iov_iter *i, size_t size)
955{
956 struct pipe_inode_info *pipe = i->pipe;
957 if (unlikely(i->count < size))
958 size = i->count;
959 if (size) {
960 struct pipe_buffer *buf;
961 size_t off = i->iov_offset, left = size;
962 int idx = i->idx;
963 if (off) /* make it relative to the beginning of buffer */
964 left += off - pipe->bufs[idx].offset;
965 while (1) {
966 buf = &pipe->bufs[idx];
967 if (left <= buf->len)
968 break;
969 left -= buf->len;
970 idx = next_idx(idx, pipe);
971 }
972 i->idx = idx;
973 i->iov_offset = buf->offset + left;
974 }
975 i->count -= size;
976 /* ... and discard everything past that point */
977 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -0400978}
979
Al Viro62a80672014-04-04 23:12:29 -0400980void iov_iter_advance(struct iov_iter *i, size_t size)
981{
David Howells00e23702018-10-22 13:07:28 +0100982 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400983 pipe_advance(i, size);
984 return;
985 }
David Howells9ea9ce02018-10-20 00:57:56 +0100986 if (unlikely(iov_iter_is_discard(i))) {
987 i->count -= size;
988 return;
989 }
Al Viroa2804552014-11-27 14:48:42 -0500990 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -0400991}
992EXPORT_SYMBOL(iov_iter_advance);
993
Al Viro27c0e372017-02-17 18:42:24 -0500994void iov_iter_revert(struct iov_iter *i, size_t unroll)
995{
996 if (!unroll)
997 return;
Al Viro5b47d592017-05-08 13:54:47 -0400998 if (WARN_ON(unroll > MAX_RW_COUNT))
999 return;
Al Viro27c0e372017-02-17 18:42:24 -05001000 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001001 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001002 struct pipe_inode_info *pipe = i->pipe;
1003 int idx = i->idx;
1004 size_t off = i->iov_offset;
1005 while (1) {
1006 size_t n = off - pipe->bufs[idx].offset;
1007 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001008 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001009 break;
1010 }
1011 unroll -= n;
1012 if (!unroll && idx == i->start_idx) {
1013 off = 0;
1014 break;
1015 }
1016 if (!idx--)
1017 idx = pipe->buffers - 1;
1018 off = pipe->bufs[idx].offset + pipe->bufs[idx].len;
1019 }
1020 i->iov_offset = off;
1021 i->idx = idx;
1022 pipe_truncate(i);
1023 return;
1024 }
David Howells9ea9ce02018-10-20 00:57:56 +01001025 if (unlikely(iov_iter_is_discard(i)))
1026 return;
Al Viro27c0e372017-02-17 18:42:24 -05001027 if (unroll <= i->iov_offset) {
1028 i->iov_offset -= unroll;
1029 return;
1030 }
1031 unroll -= i->iov_offset;
David Howells00e23702018-10-22 13:07:28 +01001032 if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001033 const struct bio_vec *bvec = i->bvec;
1034 while (1) {
1035 size_t n = (--bvec)->bv_len;
1036 i->nr_segs++;
1037 if (unroll <= n) {
1038 i->bvec = bvec;
1039 i->iov_offset = n - unroll;
1040 return;
1041 }
1042 unroll -= n;
1043 }
1044 } else { /* same logics for iovec and kvec */
1045 const struct iovec *iov = i->iov;
1046 while (1) {
1047 size_t n = (--iov)->iov_len;
1048 i->nr_segs++;
1049 if (unroll <= n) {
1050 i->iov = iov;
1051 i->iov_offset = n - unroll;
1052 return;
1053 }
1054 unroll -= n;
1055 }
1056 }
1057}
1058EXPORT_SYMBOL(iov_iter_revert);
1059
Al Viro62a80672014-04-04 23:12:29 -04001060/*
1061 * Return the count of just the current iov_iter segment.
1062 */
1063size_t iov_iter_single_seg_count(const struct iov_iter *i)
1064{
David Howells00e23702018-10-22 13:07:28 +01001065 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001066 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001067 if (i->nr_segs == 1)
1068 return i->count;
David Howells9ea9ce02018-10-20 00:57:56 +01001069 if (unlikely(iov_iter_is_discard(i)))
1070 return i->count;
David Howells00e23702018-10-22 13:07:28 +01001071 else if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001072 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001073 else
1074 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001075}
1076EXPORT_SYMBOL(iov_iter_single_seg_count);
1077
David Howellsaa563d72018-10-20 00:57:56 +01001078void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001079 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001080 size_t count)
1081{
David Howellsaa563d72018-10-20 00:57:56 +01001082 WARN_ON(direction & ~(READ | WRITE));
1083 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001084 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001085 i->nr_segs = nr_segs;
1086 i->iov_offset = 0;
1087 i->count = count;
1088}
1089EXPORT_SYMBOL(iov_iter_kvec);
1090
David Howellsaa563d72018-10-20 00:57:56 +01001091void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001092 const struct bio_vec *bvec, unsigned long nr_segs,
1093 size_t count)
1094{
David Howellsaa563d72018-10-20 00:57:56 +01001095 WARN_ON(direction & ~(READ | WRITE));
1096 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001097 i->bvec = bvec;
1098 i->nr_segs = nr_segs;
1099 i->iov_offset = 0;
1100 i->count = count;
1101}
1102EXPORT_SYMBOL(iov_iter_bvec);
1103
David Howellsaa563d72018-10-20 00:57:56 +01001104void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001105 struct pipe_inode_info *pipe,
1106 size_t count)
1107{
David Howellsaa563d72018-10-20 00:57:56 +01001108 BUG_ON(direction != READ);
Al Virob9dc6f62017-01-14 19:33:08 -05001109 WARN_ON(pipe->nrbufs == pipe->buffers);
David Howellsaa563d72018-10-20 00:57:56 +01001110 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001111 i->pipe = pipe;
1112 i->idx = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1113 i->iov_offset = 0;
1114 i->count = count;
Al Viro27c0e372017-02-17 18:42:24 -05001115 i->start_idx = i->idx;
Al Viro241699c2016-09-22 16:33:12 -04001116}
1117EXPORT_SYMBOL(iov_iter_pipe);
1118
David Howells9ea9ce02018-10-20 00:57:56 +01001119/**
1120 * iov_iter_discard - Initialise an I/O iterator that discards data
1121 * @i: The iterator to initialise.
1122 * @direction: The direction of the transfer.
1123 * @count: The size of the I/O buffer in bytes.
1124 *
1125 * Set up an I/O iterator that just discards everything that's written to it.
1126 * It's only available as a READ iterator.
1127 */
1128void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1129{
1130 BUG_ON(direction != READ);
1131 i->type = ITER_DISCARD | READ;
1132 i->count = count;
1133 i->iov_offset = 0;
1134}
1135EXPORT_SYMBOL(iov_iter_discard);
1136
Al Viro62a80672014-04-04 23:12:29 -04001137unsigned long iov_iter_alignment(const struct iov_iter *i)
1138{
Al Viro04a31162014-11-27 13:51:41 -05001139 unsigned long res = 0;
1140 size_t size = i->count;
1141
David Howells00e23702018-10-22 13:07:28 +01001142 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro33844e62016-12-21 21:55:02 -05001143 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->idx]))
Al Viro241699c2016-09-22 16:33:12 -04001144 return size | i->iov_offset;
1145 return size;
1146 }
Al Viro04a31162014-11-27 13:51:41 -05001147 iterate_all_kinds(i, size, v,
1148 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001149 res |= v.bv_offset | v.bv_len,
1150 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -05001151 )
1152 return res;
Al Viro62a80672014-04-04 23:12:29 -04001153}
1154EXPORT_SYMBOL(iov_iter_alignment);
1155
Al Viro357f4352016-04-08 19:05:19 -04001156unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1157{
Al Viro33844e62016-12-21 21:55:02 -05001158 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001159 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001160
David Howells9ea9ce02018-10-20 00:57:56 +01001161 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001162 WARN_ON(1);
1163 return ~0U;
1164 }
1165
Al Viro357f4352016-04-08 19:05:19 -04001166 iterate_all_kinds(i, size, v,
1167 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1168 (size != v.iov_len ? size : 0), 0),
1169 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1170 (size != v.bv_len ? size : 0)),
1171 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1172 (size != v.iov_len ? size : 0))
1173 );
Al Viro33844e62016-12-21 21:55:02 -05001174 return res;
Al Viro357f4352016-04-08 19:05:19 -04001175}
1176EXPORT_SYMBOL(iov_iter_gap_alignment);
1177
Ilya Dryomove76b63122018-05-02 20:16:56 +02001178static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001179 size_t maxsize,
1180 struct page **pages,
1181 int idx,
1182 size_t *start)
1183{
1184 struct pipe_inode_info *pipe = i->pipe;
Al Viro1689c732016-10-11 18:21:14 +01001185 ssize_t n = push_pipe(i, maxsize, &idx, start);
Al Viro241699c2016-09-22 16:33:12 -04001186 if (!n)
1187 return -EFAULT;
1188
1189 maxsize = n;
1190 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001191 while (n > 0) {
Al Viro241699c2016-09-22 16:33:12 -04001192 get_page(*pages++ = pipe->bufs[idx].page);
1193 idx = next_idx(idx, pipe);
1194 n -= PAGE_SIZE;
1195 }
1196
1197 return maxsize;
1198}
1199
1200static ssize_t pipe_get_pages(struct iov_iter *i,
1201 struct page **pages, size_t maxsize, unsigned maxpages,
1202 size_t *start)
1203{
1204 unsigned npages;
1205 size_t capacity;
1206 int idx;
1207
Al Viro33844e62016-12-21 21:55:02 -05001208 if (!maxsize)
1209 return 0;
1210
Al Viro241699c2016-09-22 16:33:12 -04001211 if (!sanity(i))
1212 return -EFAULT;
1213
1214 data_start(i, &idx, start);
1215 /* some of this one + all after this one */
1216 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1217 capacity = min(npages,maxpages) * PAGE_SIZE - *start;
1218
1219 return __pipe_get_pages(i, min(maxsize, capacity), pages, idx, start);
1220}
1221
Al Viro62a80672014-04-04 23:12:29 -04001222ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001223 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001224 size_t *start)
1225{
Al Viroe5393fa2014-11-27 14:12:09 -05001226 if (maxsize > i->count)
1227 maxsize = i->count;
1228
David Howells00e23702018-10-22 13:07:28 +01001229 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001230 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001231 if (unlikely(iov_iter_is_discard(i)))
1232 return -EFAULT;
1233
Al Viroe5393fa2014-11-27 14:12:09 -05001234 iterate_all_kinds(i, maxsize, v, ({
1235 unsigned long addr = (unsigned long)v.iov_base;
1236 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1237 int n;
1238 int res;
1239
1240 if (len > maxpages * PAGE_SIZE)
1241 len = maxpages * PAGE_SIZE;
1242 addr &= ~(PAGE_SIZE - 1);
1243 n = DIV_ROUND_UP(len, PAGE_SIZE);
David Howells00e23702018-10-22 13:07:28 +01001244 res = get_user_pages_fast(addr, n, iov_iter_rw(i) != WRITE, pages);
Al Viroe5393fa2014-11-27 14:12:09 -05001245 if (unlikely(res < 0))
1246 return res;
1247 return (res == n ? len : res * PAGE_SIZE) - *start;
1248 0;}),({
1249 /* can't be more than PAGE_SIZE */
1250 *start = v.bv_offset;
1251 get_page(*pages = v.bv_page);
1252 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001253 }),({
1254 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001255 })
1256 )
1257 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001258}
1259EXPORT_SYMBOL(iov_iter_get_pages);
1260
Al Viro1b17f1f2014-11-27 14:14:31 -05001261static struct page **get_pages_array(size_t n)
1262{
Michal Hocko752ade62017-05-08 15:57:27 -07001263 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001264}
1265
Al Viro241699c2016-09-22 16:33:12 -04001266static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1267 struct page ***pages, size_t maxsize,
1268 size_t *start)
1269{
1270 struct page **p;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001271 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001272 int idx;
1273 int npages;
1274
Al Viro33844e62016-12-21 21:55:02 -05001275 if (!maxsize)
1276 return 0;
1277
Al Viro241699c2016-09-22 16:33:12 -04001278 if (!sanity(i))
1279 return -EFAULT;
1280
1281 data_start(i, &idx, start);
1282 /* some of this one + all after this one */
1283 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1284 n = npages * PAGE_SIZE - *start;
1285 if (maxsize > n)
1286 maxsize = n;
1287 else
1288 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1289 p = get_pages_array(npages);
1290 if (!p)
1291 return -ENOMEM;
1292 n = __pipe_get_pages(i, maxsize, p, idx, start);
1293 if (n > 0)
1294 *pages = p;
1295 else
1296 kvfree(p);
1297 return n;
1298}
1299
Al Viro62a80672014-04-04 23:12:29 -04001300ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1301 struct page ***pages, size_t maxsize,
1302 size_t *start)
1303{
Al Viro1b17f1f2014-11-27 14:14:31 -05001304 struct page **p;
1305
1306 if (maxsize > i->count)
1307 maxsize = i->count;
1308
David Howells00e23702018-10-22 13:07:28 +01001309 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001310 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001311 if (unlikely(iov_iter_is_discard(i)))
1312 return -EFAULT;
1313
Al Viro1b17f1f2014-11-27 14:14:31 -05001314 iterate_all_kinds(i, maxsize, v, ({
1315 unsigned long addr = (unsigned long)v.iov_base;
1316 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1317 int n;
1318 int res;
1319
1320 addr &= ~(PAGE_SIZE - 1);
1321 n = DIV_ROUND_UP(len, PAGE_SIZE);
1322 p = get_pages_array(n);
1323 if (!p)
1324 return -ENOMEM;
David Howells00e23702018-10-22 13:07:28 +01001325 res = get_user_pages_fast(addr, n, iov_iter_rw(i) != WRITE, p);
Al Viro1b17f1f2014-11-27 14:14:31 -05001326 if (unlikely(res < 0)) {
1327 kvfree(p);
1328 return res;
1329 }
1330 *pages = p;
1331 return (res == n ? len : res * PAGE_SIZE) - *start;
1332 0;}),({
1333 /* can't be more than PAGE_SIZE */
1334 *start = v.bv_offset;
1335 *pages = p = get_pages_array(1);
1336 if (!p)
1337 return -ENOMEM;
1338 get_page(*p = v.bv_page);
1339 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001340 }),({
1341 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -05001342 })
1343 )
1344 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001345}
1346EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1347
Al Viroa604ec72014-11-24 01:08:00 -05001348size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1349 struct iov_iter *i)
1350{
1351 char *to = addr;
1352 __wsum sum, next;
1353 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001354 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001355 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001356 WARN_ON(1);
1357 return 0;
1358 }
Al Viroa604ec72014-11-24 01:08:00 -05001359 iterate_and_advance(i, bytes, v, ({
1360 int err = 0;
Al Virocbbd26b2016-11-01 22:09:04 -04001361 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001362 (to += v.iov_len) - v.iov_len,
1363 v.iov_len, 0, &err);
1364 if (!err) {
1365 sum = csum_block_add(sum, next, off);
1366 off += v.iov_len;
1367 }
1368 err ? v.iov_len : 0;
1369 }), ({
1370 char *p = kmap_atomic(v.bv_page);
1371 next = csum_partial_copy_nocheck(p + v.bv_offset,
1372 (to += v.bv_len) - v.bv_len,
1373 v.bv_len, 0);
1374 kunmap_atomic(p);
1375 sum = csum_block_add(sum, next, off);
1376 off += v.bv_len;
1377 }),({
1378 next = csum_partial_copy_nocheck(v.iov_base,
1379 (to += v.iov_len) - v.iov_len,
1380 v.iov_len, 0);
1381 sum = csum_block_add(sum, next, off);
1382 off += v.iov_len;
1383 })
1384 )
1385 *csum = sum;
1386 return bytes;
1387}
1388EXPORT_SYMBOL(csum_and_copy_from_iter);
1389
Al Virocbbd26b2016-11-01 22:09:04 -04001390bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1391 struct iov_iter *i)
1392{
1393 char *to = addr;
1394 __wsum sum, next;
1395 size_t off = 0;
1396 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001397 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001398 WARN_ON(1);
1399 return false;
1400 }
1401 if (unlikely(i->count < bytes))
1402 return false;
1403 iterate_all_kinds(i, bytes, v, ({
1404 int err = 0;
1405 next = csum_and_copy_from_user(v.iov_base,
1406 (to += v.iov_len) - v.iov_len,
1407 v.iov_len, 0, &err);
1408 if (err)
1409 return false;
1410 sum = csum_block_add(sum, next, off);
1411 off += v.iov_len;
1412 0;
1413 }), ({
1414 char *p = kmap_atomic(v.bv_page);
1415 next = csum_partial_copy_nocheck(p + v.bv_offset,
1416 (to += v.bv_len) - v.bv_len,
1417 v.bv_len, 0);
1418 kunmap_atomic(p);
1419 sum = csum_block_add(sum, next, off);
1420 off += v.bv_len;
1421 }),({
1422 next = csum_partial_copy_nocheck(v.iov_base,
1423 (to += v.iov_len) - v.iov_len,
1424 v.iov_len, 0);
1425 sum = csum_block_add(sum, next, off);
1426 off += v.iov_len;
1427 })
1428 )
1429 *csum = sum;
1430 iov_iter_advance(i, bytes);
1431 return true;
1432}
1433EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1434
Al Viro36f7a8a2015-12-06 16:49:22 -05001435size_t csum_and_copy_to_iter(const void *addr, size_t bytes, __wsum *csum,
Al Viroa604ec72014-11-24 01:08:00 -05001436 struct iov_iter *i)
1437{
Al Viro36f7a8a2015-12-06 16:49:22 -05001438 const char *from = addr;
Al Viroa604ec72014-11-24 01:08:00 -05001439 __wsum sum, next;
1440 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001441 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001442 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001443 WARN_ON(1); /* for now */
1444 return 0;
1445 }
Al Viroa604ec72014-11-24 01:08:00 -05001446 iterate_and_advance(i, bytes, v, ({
1447 int err = 0;
1448 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001449 v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001450 v.iov_len, 0, &err);
1451 if (!err) {
1452 sum = csum_block_add(sum, next, off);
1453 off += v.iov_len;
1454 }
1455 err ? v.iov_len : 0;
1456 }), ({
1457 char *p = kmap_atomic(v.bv_page);
1458 next = csum_partial_copy_nocheck((from += v.bv_len) - v.bv_len,
1459 p + v.bv_offset,
1460 v.bv_len, 0);
1461 kunmap_atomic(p);
1462 sum = csum_block_add(sum, next, off);
1463 off += v.bv_len;
1464 }),({
1465 next = csum_partial_copy_nocheck((from += v.iov_len) - v.iov_len,
1466 v.iov_base,
1467 v.iov_len, 0);
1468 sum = csum_block_add(sum, next, off);
1469 off += v.iov_len;
1470 })
1471 )
1472 *csum = sum;
1473 return bytes;
1474}
1475EXPORT_SYMBOL(csum_and_copy_to_iter);
1476
Al Viro62a80672014-04-04 23:12:29 -04001477int iov_iter_npages(const struct iov_iter *i, int maxpages)
1478{
Al Viroe0f2dc42014-11-27 14:09:46 -05001479 size_t size = i->count;
1480 int npages = 0;
1481
1482 if (!size)
1483 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001484 if (unlikely(iov_iter_is_discard(i)))
1485 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001486
David Howells00e23702018-10-22 13:07:28 +01001487 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001488 struct pipe_inode_info *pipe = i->pipe;
1489 size_t off;
1490 int idx;
1491
1492 if (!sanity(i))
1493 return 0;
1494
1495 data_start(i, &idx, &off);
1496 /* some of this one + all after this one */
1497 npages = ((pipe->curbuf - idx - 1) & (pipe->buffers - 1)) + 1;
1498 if (npages >= maxpages)
1499 return maxpages;
1500 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001501 unsigned long p = (unsigned long)v.iov_base;
1502 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1503 - p / PAGE_SIZE;
1504 if (npages >= maxpages)
1505 return maxpages;
1506 0;}),({
1507 npages++;
1508 if (npages >= maxpages)
1509 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001510 }),({
1511 unsigned long p = (unsigned long)v.iov_base;
1512 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1513 - p / PAGE_SIZE;
1514 if (npages >= maxpages)
1515 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001516 })
1517 )
1518 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001519}
Al Virof67da302014-03-19 01:16:16 -04001520EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001521
1522const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1523{
1524 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001525 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001526 WARN_ON(1);
1527 return NULL;
1528 }
David Howells9ea9ce02018-10-20 00:57:56 +01001529 if (unlikely(iov_iter_is_discard(new)))
1530 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001531 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001532 return new->bvec = kmemdup(new->bvec,
1533 new->nr_segs * sizeof(struct bio_vec),
1534 flags);
1535 else
1536 /* iovec and kvec have identical layout */
1537 return new->iov = kmemdup(new->iov,
1538 new->nr_segs * sizeof(struct iovec),
1539 flags);
1540}
1541EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001542
Vegard Nossumffecee42016-10-08 11:18:07 +02001543/**
1544 * import_iovec() - Copy an array of &struct iovec from userspace
1545 * into the kernel, check that it is valid, and initialize a new
1546 * &struct iov_iter iterator to access it.
1547 *
1548 * @type: One of %READ or %WRITE.
1549 * @uvector: Pointer to the userspace array.
1550 * @nr_segs: Number of elements in userspace array.
1551 * @fast_segs: Number of elements in @iov.
1552 * @iov: (input and output parameter) Pointer to pointer to (usually small
1553 * on-stack) kernel array.
1554 * @i: Pointer to iterator that will be initialized on success.
1555 *
1556 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1557 * then this function places %NULL in *@iov on return. Otherwise, a new
1558 * array will be allocated and the result placed in *@iov. This means that
1559 * the caller may call kfree() on *@iov regardless of whether the small
1560 * on-stack array was used or not (and regardless of whether this function
1561 * returns an error or not).
1562 *
1563 * Return: 0 on success or negative error code on error.
1564 */
Al Virobc917be2015-03-21 17:45:43 -04001565int import_iovec(int type, const struct iovec __user * uvector,
1566 unsigned nr_segs, unsigned fast_segs,
1567 struct iovec **iov, struct iov_iter *i)
1568{
1569 ssize_t n;
1570 struct iovec *p;
1571 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1572 *iov, &p);
1573 if (n < 0) {
1574 if (p != *iov)
1575 kfree(p);
1576 *iov = NULL;
1577 return n;
1578 }
1579 iov_iter_init(i, type, p, nr_segs, n);
1580 *iov = p == *iov ? NULL : p;
1581 return 0;
1582}
1583EXPORT_SYMBOL(import_iovec);
1584
1585#ifdef CONFIG_COMPAT
1586#include <linux/compat.h>
1587
1588int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
1589 unsigned nr_segs, unsigned fast_segs,
1590 struct iovec **iov, struct iov_iter *i)
1591{
1592 ssize_t n;
1593 struct iovec *p;
1594 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1595 *iov, &p);
1596 if (n < 0) {
1597 if (p != *iov)
1598 kfree(p);
1599 *iov = NULL;
1600 return n;
1601 }
1602 iov_iter_init(i, type, p, nr_segs, n);
1603 *iov = p == *iov ? NULL : p;
1604 return 0;
1605}
1606#endif
1607
1608int import_single_range(int rw, void __user *buf, size_t len,
1609 struct iovec *iov, struct iov_iter *i)
1610{
1611 if (len > MAX_RW_COUNT)
1612 len = MAX_RW_COUNT;
1613 if (unlikely(!access_ok(!rw, buf, len)))
1614 return -EFAULT;
1615
1616 iov->iov_base = buf;
1617 iov->iov_len = len;
1618 iov_iter_init(i, rw, iov, 1, len);
1619 return 0;
1620}
Al Viroe1267582015-12-06 20:38:56 -05001621EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05001622
1623int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1624 int (*f)(struct kvec *vec, void *context),
1625 void *context)
1626{
1627 struct kvec w;
1628 int err = -EINVAL;
1629 if (!bytes)
1630 return 0;
1631
1632 iterate_all_kinds(i, bytes, v, -EINVAL, ({
1633 w.iov_base = kmap(v.bv_page) + v.bv_offset;
1634 w.iov_len = v.bv_len;
1635 err = f(&w, context);
1636 kunmap(v.bv_page);
1637 err;}), ({
1638 w = v;
1639 err = f(&w, context);})
1640 )
1641 return err;
1642}
1643EXPORT_SYMBOL(iov_iter_for_each_range);