blob: 52c8dd6d8e8290fa8df614e0017542a600b9c536 [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)) \
86 } else { \
87 const struct iovec *iov; \
88 struct iovec v; \
89 iterate_iovec(i, n, v, iov, skip, (I)) \
90 } \
Al Viro04a31162014-11-27 13:51:41 -050091 } \
92}
93
Al Viroa2804552014-11-27 14:48:42 -050094#define iterate_and_advance(i, n, v, I, B, K) { \
Al Virodd254f52016-05-09 11:54:48 -040095 if (unlikely(i->count < n)) \
96 n = i->count; \
Al Viro19f18452016-05-25 17:36:19 -040097 if (i->count) { \
Al Virodd254f52016-05-09 11:54:48 -040098 size_t skip = i->iov_offset; \
99 if (unlikely(i->type & ITER_BVEC)) { \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800100 const struct bio_vec *bvec = i->bvec; \
Al Virodd254f52016-05-09 11:54:48 -0400101 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800102 struct bvec_iter __bi; \
103 iterate_bvec(i, n, v, __bi, skip, (B)) \
104 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
105 i->nr_segs -= i->bvec - bvec; \
106 skip = __bi.bi_bvec_done; \
Al Virodd254f52016-05-09 11:54:48 -0400107 } else if (unlikely(i->type & ITER_KVEC)) { \
108 const struct kvec *kvec; \
109 struct kvec v; \
110 iterate_kvec(i, n, v, kvec, skip, (K)) \
111 if (skip == kvec->iov_len) { \
112 kvec++; \
113 skip = 0; \
114 } \
115 i->nr_segs -= kvec - i->kvec; \
116 i->kvec = kvec; \
117 } else { \
118 const struct iovec *iov; \
119 struct iovec v; \
120 iterate_iovec(i, n, v, iov, skip, (I)) \
121 if (skip == iov->iov_len) { \
122 iov++; \
123 skip = 0; \
124 } \
125 i->nr_segs -= iov - i->iov; \
126 i->iov = iov; \
Al Viro7ce2a912014-11-27 13:59:45 -0500127 } \
Al Virodd254f52016-05-09 11:54:48 -0400128 i->count -= n; \
129 i->iov_offset = skip; \
Al Viro7ce2a912014-11-27 13:59:45 -0500130 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500131}
132
Al Viro09fc68d2017-06-29 22:25:14 -0400133static int copyout(void __user *to, const void *from, size_t n)
134{
135 if (access_ok(VERIFY_WRITE, to, n)) {
136 kasan_check_read(from, n);
137 n = raw_copy_to_user(to, from, n);
138 }
139 return n;
140}
141
142static int copyin(void *to, const void __user *from, size_t n)
143{
144 if (access_ok(VERIFY_READ, from, n)) {
145 kasan_check_write(to, n);
146 n = raw_copy_from_user(to, from, n);
147 }
148 return n;
149}
150
Al Viro62a80672014-04-04 23:12:29 -0400151static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500152 struct iov_iter *i)
153{
154 size_t skip, copy, left, wanted;
155 const struct iovec *iov;
156 char __user *buf;
157 void *kaddr, *from;
158
159 if (unlikely(bytes > i->count))
160 bytes = i->count;
161
162 if (unlikely(!bytes))
163 return 0;
164
Al Viro09fc68d2017-06-29 22:25:14 -0400165 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500166 wanted = bytes;
167 iov = i->iov;
168 skip = i->iov_offset;
169 buf = iov->iov_base + skip;
170 copy = min(bytes, iov->iov_len - skip);
171
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700172 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500173 kaddr = kmap_atomic(page);
174 from = kaddr + offset;
175
176 /* first chunk, usually the only one */
Al Viro09fc68d2017-06-29 22:25:14 -0400177 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500178 copy -= left;
179 skip += copy;
180 from += copy;
181 bytes -= copy;
182
183 while (unlikely(!left && bytes)) {
184 iov++;
185 buf = iov->iov_base;
186 copy = min(bytes, iov->iov_len);
Al Viro09fc68d2017-06-29 22:25:14 -0400187 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500188 copy -= left;
189 skip = copy;
190 from += copy;
191 bytes -= copy;
192 }
193 if (likely(!bytes)) {
194 kunmap_atomic(kaddr);
195 goto done;
196 }
197 offset = from - kaddr;
198 buf += copy;
199 kunmap_atomic(kaddr);
200 copy = min(bytes, iov->iov_len - skip);
201 }
202 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700203
Al Viro4f18cd32014-02-05 19:11:33 -0500204 kaddr = kmap(page);
205 from = kaddr + offset;
Al Viro09fc68d2017-06-29 22:25:14 -0400206 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500207 copy -= left;
208 skip += copy;
209 from += copy;
210 bytes -= copy;
211 while (unlikely(!left && bytes)) {
212 iov++;
213 buf = iov->iov_base;
214 copy = min(bytes, iov->iov_len);
Al Viro09fc68d2017-06-29 22:25:14 -0400215 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500216 copy -= left;
217 skip = copy;
218 from += copy;
219 bytes -= copy;
220 }
221 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700222
Al Viro4f18cd32014-02-05 19:11:33 -0500223done:
Al Viro81055e52014-04-04 19:23:46 -0400224 if (skip == iov->iov_len) {
225 iov++;
226 skip = 0;
227 }
Al Viro4f18cd32014-02-05 19:11:33 -0500228 i->count -= wanted - bytes;
229 i->nr_segs -= iov - i->iov;
230 i->iov = iov;
231 i->iov_offset = skip;
232 return wanted - bytes;
233}
Al Viro4f18cd32014-02-05 19:11:33 -0500234
Al Viro62a80672014-04-04 23:12:29 -0400235static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400236 struct iov_iter *i)
237{
238 size_t skip, copy, left, wanted;
239 const struct iovec *iov;
240 char __user *buf;
241 void *kaddr, *to;
242
243 if (unlikely(bytes > i->count))
244 bytes = i->count;
245
246 if (unlikely(!bytes))
247 return 0;
248
Al Viro09fc68d2017-06-29 22:25:14 -0400249 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400250 wanted = bytes;
251 iov = i->iov;
252 skip = i->iov_offset;
253 buf = iov->iov_base + skip;
254 copy = min(bytes, iov->iov_len - skip);
255
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700256 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400257 kaddr = kmap_atomic(page);
258 to = kaddr + offset;
259
260 /* first chunk, usually the only one */
Al Viro09fc68d2017-06-29 22:25:14 -0400261 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400262 copy -= left;
263 skip += copy;
264 to += copy;
265 bytes -= copy;
266
267 while (unlikely(!left && bytes)) {
268 iov++;
269 buf = iov->iov_base;
270 copy = min(bytes, iov->iov_len);
Al Viro09fc68d2017-06-29 22:25:14 -0400271 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400272 copy -= left;
273 skip = copy;
274 to += copy;
275 bytes -= copy;
276 }
277 if (likely(!bytes)) {
278 kunmap_atomic(kaddr);
279 goto done;
280 }
281 offset = to - kaddr;
282 buf += copy;
283 kunmap_atomic(kaddr);
284 copy = min(bytes, iov->iov_len - skip);
285 }
286 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700287
Al Virof0d1bec2014-04-03 15:05:18 -0400288 kaddr = kmap(page);
289 to = kaddr + offset;
Al Viro09fc68d2017-06-29 22:25:14 -0400290 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400291 copy -= left;
292 skip += copy;
293 to += copy;
294 bytes -= copy;
295 while (unlikely(!left && bytes)) {
296 iov++;
297 buf = iov->iov_base;
298 copy = min(bytes, iov->iov_len);
Al Viro09fc68d2017-06-29 22:25:14 -0400299 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400300 copy -= left;
301 skip = copy;
302 to += copy;
303 bytes -= copy;
304 }
305 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700306
Al Virof0d1bec2014-04-03 15:05:18 -0400307done:
Al Viro81055e52014-04-04 19:23:46 -0400308 if (skip == iov->iov_len) {
309 iov++;
310 skip = 0;
311 }
Al Virof0d1bec2014-04-03 15:05:18 -0400312 i->count -= wanted - bytes;
313 i->nr_segs -= iov - i->iov;
314 i->iov = iov;
315 i->iov_offset = skip;
316 return wanted - bytes;
317}
Al Virof0d1bec2014-04-03 15:05:18 -0400318
Al Viro241699c2016-09-22 16:33:12 -0400319#ifdef PIPE_PARANOIA
320static bool sanity(const struct iov_iter *i)
321{
322 struct pipe_inode_info *pipe = i->pipe;
323 int idx = i->idx;
324 int next = pipe->curbuf + pipe->nrbufs;
325 if (i->iov_offset) {
326 struct pipe_buffer *p;
327 if (unlikely(!pipe->nrbufs))
328 goto Bad; // pipe must be non-empty
329 if (unlikely(idx != ((next - 1) & (pipe->buffers - 1))))
330 goto Bad; // must be at the last buffer...
331
332 p = &pipe->bufs[idx];
333 if (unlikely(p->offset + p->len != i->iov_offset))
334 goto Bad; // ... at the end of segment
335 } else {
336 if (idx != (next & (pipe->buffers - 1)))
337 goto Bad; // must be right after the last buffer
338 }
339 return true;
340Bad:
341 printk(KERN_ERR "idx = %d, offset = %zd\n", i->idx, i->iov_offset);
342 printk(KERN_ERR "curbuf = %d, nrbufs = %d, buffers = %d\n",
343 pipe->curbuf, pipe->nrbufs, pipe->buffers);
344 for (idx = 0; idx < pipe->buffers; idx++)
345 printk(KERN_ERR "[%p %p %d %d]\n",
346 pipe->bufs[idx].ops,
347 pipe->bufs[idx].page,
348 pipe->bufs[idx].offset,
349 pipe->bufs[idx].len);
350 WARN_ON(1);
351 return false;
352}
353#else
354#define sanity(i) true
355#endif
356
357static inline int next_idx(int idx, struct pipe_inode_info *pipe)
358{
359 return (idx + 1) & (pipe->buffers - 1);
360}
361
362static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
363 struct iov_iter *i)
364{
365 struct pipe_inode_info *pipe = i->pipe;
366 struct pipe_buffer *buf;
367 size_t off;
368 int idx;
369
370 if (unlikely(bytes > i->count))
371 bytes = i->count;
372
373 if (unlikely(!bytes))
374 return 0;
375
376 if (!sanity(i))
377 return 0;
378
379 off = i->iov_offset;
380 idx = i->idx;
381 buf = &pipe->bufs[idx];
382 if (off) {
383 if (offset == off && buf->page == page) {
384 /* merge with the last one */
385 buf->len += bytes;
386 i->iov_offset += bytes;
387 goto out;
388 }
389 idx = next_idx(idx, pipe);
390 buf = &pipe->bufs[idx];
391 }
392 if (idx == pipe->curbuf && pipe->nrbufs)
393 return 0;
394 pipe->nrbufs++;
395 buf->ops = &page_cache_pipe_buf_ops;
396 get_page(buf->page = page);
397 buf->offset = offset;
398 buf->len = bytes;
399 i->iov_offset = offset + bytes;
400 i->idx = idx;
401out:
402 i->count -= bytes;
403 return bytes;
404}
405
Al Viro4f18cd32014-02-05 19:11:33 -0500406/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400407 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
408 * bytes. For each iovec, fault in each page that constitutes the iovec.
409 *
410 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
411 * because it is an invalid address).
412 */
Al Virod4690f12016-09-16 00:11:45 +0100413int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400414{
415 size_t skip = i->iov_offset;
416 const struct iovec *iov;
417 int err;
418 struct iovec v;
419
420 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
421 iterate_iovec(i, bytes, v, iov, skip, ({
Al Viro4bce9f62016-09-17 18:02:44 -0400422 err = fault_in_pages_readable(v.iov_base, v.iov_len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400423 if (unlikely(err))
424 return err;
425 0;}))
426 }
427 return 0;
428}
Al Virod4690f12016-09-16 00:11:45 +0100429EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400430
Al Viro71d8e532014-03-05 19:28:09 -0500431void iov_iter_init(struct iov_iter *i, int direction,
432 const struct iovec *iov, unsigned long nr_segs,
433 size_t count)
434{
435 /* It will get better. Eventually... */
Al Virodb68ce12017-03-20 21:08:07 -0400436 if (uaccess_kernel()) {
Al Viro62a80672014-04-04 23:12:29 -0400437 direction |= ITER_KVEC;
Al Viroa2804552014-11-27 14:48:42 -0500438 i->type = direction;
439 i->kvec = (struct kvec *)iov;
440 } else {
441 i->type = direction;
442 i->iov = iov;
443 }
Al Viro71d8e532014-03-05 19:28:09 -0500444 i->nr_segs = nr_segs;
445 i->iov_offset = 0;
446 i->count = count;
447}
448EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400449
Al Viro62a80672014-04-04 23:12:29 -0400450static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
451{
452 char *from = kmap_atomic(page);
453 memcpy(to, from + offset, len);
454 kunmap_atomic(from);
455}
456
Al Viro36f7a8a2015-12-06 16:49:22 -0500457static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
Al Viro62a80672014-04-04 23:12:29 -0400458{
459 char *to = kmap_atomic(page);
460 memcpy(to + offset, from, len);
461 kunmap_atomic(to);
462}
463
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400464static void memzero_page(struct page *page, size_t offset, size_t len)
465{
466 char *addr = kmap_atomic(page);
467 memset(addr + offset, 0, len);
468 kunmap_atomic(addr);
469}
470
Al Viro241699c2016-09-22 16:33:12 -0400471static inline bool allocated(struct pipe_buffer *buf)
472{
473 return buf->ops == &default_pipe_buf_ops;
474}
475
476static inline void data_start(const struct iov_iter *i, int *idxp, size_t *offp)
477{
478 size_t off = i->iov_offset;
479 int idx = i->idx;
480 if (off && (!allocated(&i->pipe->bufs[idx]) || off == PAGE_SIZE)) {
481 idx = next_idx(idx, i->pipe);
482 off = 0;
483 }
484 *idxp = idx;
485 *offp = off;
486}
487
488static size_t push_pipe(struct iov_iter *i, size_t size,
489 int *idxp, size_t *offp)
490{
491 struct pipe_inode_info *pipe = i->pipe;
492 size_t off;
493 int idx;
494 ssize_t left;
495
496 if (unlikely(size > i->count))
497 size = i->count;
498 if (unlikely(!size))
499 return 0;
500
501 left = size;
502 data_start(i, &idx, &off);
503 *idxp = idx;
504 *offp = off;
505 if (off) {
506 left -= PAGE_SIZE - off;
507 if (left <= 0) {
508 pipe->bufs[idx].len += size;
509 return size;
510 }
511 pipe->bufs[idx].len = PAGE_SIZE;
512 idx = next_idx(idx, pipe);
513 }
514 while (idx != pipe->curbuf || !pipe->nrbufs) {
515 struct page *page = alloc_page(GFP_USER);
516 if (!page)
517 break;
518 pipe->nrbufs++;
519 pipe->bufs[idx].ops = &default_pipe_buf_ops;
520 pipe->bufs[idx].page = page;
521 pipe->bufs[idx].offset = 0;
522 if (left <= PAGE_SIZE) {
523 pipe->bufs[idx].len = left;
524 return size;
525 }
526 pipe->bufs[idx].len = PAGE_SIZE;
527 left -= PAGE_SIZE;
528 idx = next_idx(idx, pipe);
529 }
530 return size - left;
531}
532
533static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
534 struct iov_iter *i)
535{
536 struct pipe_inode_info *pipe = i->pipe;
537 size_t n, off;
538 int idx;
539
540 if (!sanity(i))
541 return 0;
542
543 bytes = n = push_pipe(i, bytes, &idx, &off);
544 if (unlikely(!n))
545 return 0;
546 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
547 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
548 memcpy_to_page(pipe->bufs[idx].page, off, addr, chunk);
549 i->idx = idx;
550 i->iov_offset = off + chunk;
551 n -= chunk;
552 addr += chunk;
553 }
554 i->count -= bytes;
555 return bytes;
556}
557
Al Viroaa28de22017-06-29 21:45:10 -0400558size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400559{
Al Viro36f7a8a2015-12-06 16:49:22 -0500560 const char *from = addr;
Al Viro241699c2016-09-22 16:33:12 -0400561 if (unlikely(i->type & ITER_PIPE))
562 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68d2017-06-29 22:25:14 -0400563 if (iter_is_iovec(i))
564 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500565 iterate_and_advance(i, bytes, v,
Al Viro09fc68d2017-06-29 22:25:14 -0400566 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500567 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500568 (from += v.bv_len) - v.bv_len, v.bv_len),
569 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500570 )
Al Viro62a80672014-04-04 23:12:29 -0400571
Al Viro3d4d3e42014-11-27 14:28:06 -0500572 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400573}
Al Viroaa28de22017-06-29 21:45:10 -0400574EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400575
Al Viroaa28de22017-06-29 21:45:10 -0400576size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400577{
Al Viro0dbca9a2014-11-27 14:26:43 -0500578 char *to = addr;
Al Viro241699c2016-09-22 16:33:12 -0400579 if (unlikely(i->type & ITER_PIPE)) {
580 WARN_ON(1);
581 return 0;
582 }
Al Viro09fc68d2017-06-29 22:25:14 -0400583 if (iter_is_iovec(i))
584 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500585 iterate_and_advance(i, bytes, v,
Al Viro09fc68d2017-06-29 22:25:14 -0400586 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500587 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500588 v.bv_offset, v.bv_len),
589 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500590 )
591
592 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400593}
Al Viroaa28de22017-06-29 21:45:10 -0400594EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400595
Al Viroaa28de22017-06-29 21:45:10 -0400596bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400597{
598 char *to = addr;
599 if (unlikely(i->type & ITER_PIPE)) {
600 WARN_ON(1);
601 return false;
602 }
Al Viro33844e62016-12-21 21:55:02 -0500603 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400604 return false;
605
Al Viro09fc68d2017-06-29 22:25:14 -0400606 if (iter_is_iovec(i))
607 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400608 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68d2017-06-29 22:25:14 -0400609 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400610 v.iov_base, v.iov_len))
611 return false;
612 0;}),
613 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
614 v.bv_offset, v.bv_len),
615 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
616 )
617
618 iov_iter_advance(i, bytes);
619 return true;
620}
Al Viroaa28de22017-06-29 21:45:10 -0400621EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400622
Al Viroaa28de22017-06-29 21:45:10 -0400623size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500624{
625 char *to = addr;
Al Viro241699c2016-09-22 16:33:12 -0400626 if (unlikely(i->type & ITER_PIPE)) {
627 WARN_ON(1);
628 return 0;
629 }
Al Viroaa583092014-11-27 20:27:08 -0500630 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400631 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500632 v.iov_base, v.iov_len),
633 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
634 v.bv_offset, v.bv_len),
635 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
636 )
637
638 return bytes;
639}
Al Viroaa28de22017-06-29 21:45:10 -0400640EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500641
Dan Williams0aed55a2017-05-29 12:22:50 -0700642#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Linus Torvalds6a37e942017-07-07 20:39:20 -0700643size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700644{
645 char *to = addr;
646 if (unlikely(i->type & ITER_PIPE)) {
647 WARN_ON(1);
648 return 0;
649 }
650 iterate_and_advance(i, bytes, v,
651 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
652 v.iov_base, v.iov_len),
653 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
654 v.bv_offset, v.bv_len),
655 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
656 v.iov_len)
657 )
658
659 return bytes;
660}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700661EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700662#endif
663
Al Viroaa28de22017-06-29 21:45:10 -0400664bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400665{
666 char *to = addr;
667 if (unlikely(i->type & ITER_PIPE)) {
668 WARN_ON(1);
669 return false;
670 }
Al Viro33844e62016-12-21 21:55:02 -0500671 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400672 return false;
673 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400674 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400675 v.iov_base, v.iov_len))
676 return false;
677 0;}),
678 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
679 v.bv_offset, v.bv_len),
680 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
681 )
682
683 iov_iter_advance(i, bytes);
684 return true;
685}
Al Viroaa28de22017-06-29 21:45:10 -0400686EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400687
Al Viro72e809e2017-06-29 21:52:57 -0400688static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
689{
690 size_t v = n + offset;
691 if (likely(n <= v && v <= (PAGE_SIZE << compound_order(page))))
692 return true;
693 WARN_ON(1);
694 return false;
695}
Al Virod2715242014-11-27 14:22:37 -0500696
697size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
698 struct iov_iter *i)
699{
Al Viro72e809e2017-06-29 21:52:57 -0400700 if (unlikely(!page_copy_sane(page, offset, bytes)))
701 return 0;
Al Virod2715242014-11-27 14:22:37 -0500702 if (i->type & (ITER_BVEC|ITER_KVEC)) {
703 void *kaddr = kmap_atomic(page);
704 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
705 kunmap_atomic(kaddr);
706 return wanted;
Al Viro241699c2016-09-22 16:33:12 -0400707 } else if (likely(!(i->type & ITER_PIPE)))
Al Virod2715242014-11-27 14:22:37 -0500708 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400709 else
710 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500711}
712EXPORT_SYMBOL(copy_page_to_iter);
713
714size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
715 struct iov_iter *i)
716{
Al Viro72e809e2017-06-29 21:52:57 -0400717 if (unlikely(!page_copy_sane(page, offset, bytes)))
718 return 0;
Al Viro241699c2016-09-22 16:33:12 -0400719 if (unlikely(i->type & ITER_PIPE)) {
720 WARN_ON(1);
721 return 0;
722 }
Al Virod2715242014-11-27 14:22:37 -0500723 if (i->type & (ITER_BVEC|ITER_KVEC)) {
724 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400725 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500726 kunmap_atomic(kaddr);
727 return wanted;
728 } else
729 return copy_page_from_iter_iovec(page, offset, bytes, i);
730}
731EXPORT_SYMBOL(copy_page_from_iter);
732
Al Viro241699c2016-09-22 16:33:12 -0400733static size_t pipe_zero(size_t bytes, struct iov_iter *i)
734{
735 struct pipe_inode_info *pipe = i->pipe;
736 size_t n, off;
737 int idx;
738
739 if (!sanity(i))
740 return 0;
741
742 bytes = n = push_pipe(i, bytes, &idx, &off);
743 if (unlikely(!n))
744 return 0;
745
746 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
747 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
748 memzero_page(pipe->bufs[idx].page, off, chunk);
749 i->idx = idx;
750 i->iov_offset = off + chunk;
751 n -= chunk;
752 }
753 i->count -= bytes;
754 return bytes;
755}
756
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400757size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
758{
Al Viro241699c2016-09-22 16:33:12 -0400759 if (unlikely(i->type & ITER_PIPE))
760 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500761 iterate_and_advance(i, bytes, v,
Al Viro09fc68d2017-06-29 22:25:14 -0400762 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500763 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
764 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500765 )
766
767 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400768}
769EXPORT_SYMBOL(iov_iter_zero);
770
Al Viro62a80672014-04-04 23:12:29 -0400771size_t iov_iter_copy_from_user_atomic(struct page *page,
772 struct iov_iter *i, unsigned long offset, size_t bytes)
773{
Al Viro04a31162014-11-27 13:51:41 -0500774 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400775 if (unlikely(!page_copy_sane(page, offset, bytes))) {
776 kunmap_atomic(kaddr);
777 return 0;
778 }
Al Viro241699c2016-09-22 16:33:12 -0400779 if (unlikely(i->type & ITER_PIPE)) {
780 kunmap_atomic(kaddr);
781 WARN_ON(1);
782 return 0;
783 }
Al Viro04a31162014-11-27 13:51:41 -0500784 iterate_all_kinds(i, bytes, v,
Al Viro09fc68d2017-06-29 22:25:14 -0400785 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -0500786 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500787 v.bv_offset, v.bv_len),
788 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -0500789 )
790 kunmap_atomic(kaddr);
791 return bytes;
Al Viro62a80672014-04-04 23:12:29 -0400792}
793EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
794
Al Virob9dc6f62017-01-14 19:33:08 -0500795static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -0400796{
797 struct pipe_inode_info *pipe = i->pipe;
Al Viro241699c2016-09-22 16:33:12 -0400798 if (pipe->nrbufs) {
Al Virob9dc6f62017-01-14 19:33:08 -0500799 size_t off = i->iov_offset;
800 int idx = i->idx;
801 int nrbufs = (idx - pipe->curbuf) & (pipe->buffers - 1);
802 if (off) {
803 pipe->bufs[idx].len = off - pipe->bufs[idx].offset;
804 idx = next_idx(idx, pipe);
805 nrbufs++;
806 }
807 while (pipe->nrbufs > nrbufs) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200808 pipe_buf_release(pipe, &pipe->bufs[idx]);
Al Viro241699c2016-09-22 16:33:12 -0400809 idx = next_idx(idx, pipe);
810 pipe->nrbufs--;
811 }
812 }
Al Virob9dc6f62017-01-14 19:33:08 -0500813}
814
815static void pipe_advance(struct iov_iter *i, size_t size)
816{
817 struct pipe_inode_info *pipe = i->pipe;
818 if (unlikely(i->count < size))
819 size = i->count;
820 if (size) {
821 struct pipe_buffer *buf;
822 size_t off = i->iov_offset, left = size;
823 int idx = i->idx;
824 if (off) /* make it relative to the beginning of buffer */
825 left += off - pipe->bufs[idx].offset;
826 while (1) {
827 buf = &pipe->bufs[idx];
828 if (left <= buf->len)
829 break;
830 left -= buf->len;
831 idx = next_idx(idx, pipe);
832 }
833 i->idx = idx;
834 i->iov_offset = buf->offset + left;
835 }
836 i->count -= size;
837 /* ... and discard everything past that point */
838 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -0400839}
840
Al Viro62a80672014-04-04 23:12:29 -0400841void iov_iter_advance(struct iov_iter *i, size_t size)
842{
Al Viro241699c2016-09-22 16:33:12 -0400843 if (unlikely(i->type & ITER_PIPE)) {
844 pipe_advance(i, size);
845 return;
846 }
Al Viroa2804552014-11-27 14:48:42 -0500847 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -0400848}
849EXPORT_SYMBOL(iov_iter_advance);
850
Al Viro27c0e372017-02-17 18:42:24 -0500851void iov_iter_revert(struct iov_iter *i, size_t unroll)
852{
853 if (!unroll)
854 return;
Al Viro5b47d592017-05-08 13:54:47 -0400855 if (WARN_ON(unroll > MAX_RW_COUNT))
856 return;
Al Viro27c0e372017-02-17 18:42:24 -0500857 i->count += unroll;
858 if (unlikely(i->type & ITER_PIPE)) {
859 struct pipe_inode_info *pipe = i->pipe;
860 int idx = i->idx;
861 size_t off = i->iov_offset;
862 while (1) {
863 size_t n = off - pipe->bufs[idx].offset;
864 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -0400865 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -0500866 break;
867 }
868 unroll -= n;
869 if (!unroll && idx == i->start_idx) {
870 off = 0;
871 break;
872 }
873 if (!idx--)
874 idx = pipe->buffers - 1;
875 off = pipe->bufs[idx].offset + pipe->bufs[idx].len;
876 }
877 i->iov_offset = off;
878 i->idx = idx;
879 pipe_truncate(i);
880 return;
881 }
882 if (unroll <= i->iov_offset) {
883 i->iov_offset -= unroll;
884 return;
885 }
886 unroll -= i->iov_offset;
887 if (i->type & ITER_BVEC) {
888 const struct bio_vec *bvec = i->bvec;
889 while (1) {
890 size_t n = (--bvec)->bv_len;
891 i->nr_segs++;
892 if (unroll <= n) {
893 i->bvec = bvec;
894 i->iov_offset = n - unroll;
895 return;
896 }
897 unroll -= n;
898 }
899 } else { /* same logics for iovec and kvec */
900 const struct iovec *iov = i->iov;
901 while (1) {
902 size_t n = (--iov)->iov_len;
903 i->nr_segs++;
904 if (unroll <= n) {
905 i->iov = iov;
906 i->iov_offset = n - unroll;
907 return;
908 }
909 unroll -= n;
910 }
911 }
912}
913EXPORT_SYMBOL(iov_iter_revert);
914
Al Viro62a80672014-04-04 23:12:29 -0400915/*
916 * Return the count of just the current iov_iter segment.
917 */
918size_t iov_iter_single_seg_count(const struct iov_iter *i)
919{
Al Viro241699c2016-09-22 16:33:12 -0400920 if (unlikely(i->type & ITER_PIPE))
921 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -0400922 if (i->nr_segs == 1)
923 return i->count;
924 else if (i->type & ITER_BVEC)
Al Viro62a80672014-04-04 23:12:29 -0400925 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +1100926 else
927 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -0400928}
929EXPORT_SYMBOL(iov_iter_single_seg_count);
930
Al Viroabb78f82014-11-24 14:46:11 -0500931void iov_iter_kvec(struct iov_iter *i, int direction,
Al Viro05afcb72015-01-23 01:08:07 -0500932 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -0500933 size_t count)
934{
935 BUG_ON(!(direction & ITER_KVEC));
936 i->type = direction;
Al Viro05afcb72015-01-23 01:08:07 -0500937 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -0500938 i->nr_segs = nr_segs;
939 i->iov_offset = 0;
940 i->count = count;
941}
942EXPORT_SYMBOL(iov_iter_kvec);
943
Al Viro05afcb72015-01-23 01:08:07 -0500944void iov_iter_bvec(struct iov_iter *i, int direction,
945 const struct bio_vec *bvec, unsigned long nr_segs,
946 size_t count)
947{
948 BUG_ON(!(direction & ITER_BVEC));
949 i->type = direction;
950 i->bvec = bvec;
951 i->nr_segs = nr_segs;
952 i->iov_offset = 0;
953 i->count = count;
954}
955EXPORT_SYMBOL(iov_iter_bvec);
956
Al Viro241699c2016-09-22 16:33:12 -0400957void iov_iter_pipe(struct iov_iter *i, int direction,
958 struct pipe_inode_info *pipe,
959 size_t count)
960{
961 BUG_ON(direction != ITER_PIPE);
Al Virob9dc6f62017-01-14 19:33:08 -0500962 WARN_ON(pipe->nrbufs == pipe->buffers);
Al Viro241699c2016-09-22 16:33:12 -0400963 i->type = direction;
964 i->pipe = pipe;
965 i->idx = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
966 i->iov_offset = 0;
967 i->count = count;
Al Viro27c0e372017-02-17 18:42:24 -0500968 i->start_idx = i->idx;
Al Viro241699c2016-09-22 16:33:12 -0400969}
970EXPORT_SYMBOL(iov_iter_pipe);
971
Al Viro62a80672014-04-04 23:12:29 -0400972unsigned long iov_iter_alignment(const struct iov_iter *i)
973{
Al Viro04a31162014-11-27 13:51:41 -0500974 unsigned long res = 0;
975 size_t size = i->count;
976
Al Viro241699c2016-09-22 16:33:12 -0400977 if (unlikely(i->type & ITER_PIPE)) {
Al Viro33844e62016-12-21 21:55:02 -0500978 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->idx]))
Al Viro241699c2016-09-22 16:33:12 -0400979 return size | i->iov_offset;
980 return size;
981 }
Al Viro04a31162014-11-27 13:51:41 -0500982 iterate_all_kinds(i, size, v,
983 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -0500984 res |= v.bv_offset | v.bv_len,
985 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -0500986 )
987 return res;
Al Viro62a80672014-04-04 23:12:29 -0400988}
989EXPORT_SYMBOL(iov_iter_alignment);
990
Al Viro357f4352016-04-08 19:05:19 -0400991unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
992{
Al Viro33844e62016-12-21 21:55:02 -0500993 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -0400994 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -0400995
Al Viro241699c2016-09-22 16:33:12 -0400996 if (unlikely(i->type & ITER_PIPE)) {
997 WARN_ON(1);
998 return ~0U;
999 }
1000
Al Viro357f4352016-04-08 19:05:19 -04001001 iterate_all_kinds(i, size, v,
1002 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1003 (size != v.iov_len ? size : 0), 0),
1004 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1005 (size != v.bv_len ? size : 0)),
1006 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1007 (size != v.iov_len ? size : 0))
1008 );
Al Viro33844e62016-12-21 21:55:02 -05001009 return res;
Al Viro357f4352016-04-08 19:05:19 -04001010}
1011EXPORT_SYMBOL(iov_iter_gap_alignment);
1012
Al Viro241699c2016-09-22 16:33:12 -04001013static inline size_t __pipe_get_pages(struct iov_iter *i,
1014 size_t maxsize,
1015 struct page **pages,
1016 int idx,
1017 size_t *start)
1018{
1019 struct pipe_inode_info *pipe = i->pipe;
Al Viro1689c732016-10-11 18:21:14 +01001020 ssize_t n = push_pipe(i, maxsize, &idx, start);
Al Viro241699c2016-09-22 16:33:12 -04001021 if (!n)
1022 return -EFAULT;
1023
1024 maxsize = n;
1025 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001026 while (n > 0) {
Al Viro241699c2016-09-22 16:33:12 -04001027 get_page(*pages++ = pipe->bufs[idx].page);
1028 idx = next_idx(idx, pipe);
1029 n -= PAGE_SIZE;
1030 }
1031
1032 return maxsize;
1033}
1034
1035static ssize_t pipe_get_pages(struct iov_iter *i,
1036 struct page **pages, size_t maxsize, unsigned maxpages,
1037 size_t *start)
1038{
1039 unsigned npages;
1040 size_t capacity;
1041 int idx;
1042
Al Viro33844e62016-12-21 21:55:02 -05001043 if (!maxsize)
1044 return 0;
1045
Al Viro241699c2016-09-22 16:33:12 -04001046 if (!sanity(i))
1047 return -EFAULT;
1048
1049 data_start(i, &idx, start);
1050 /* some of this one + all after this one */
1051 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1052 capacity = min(npages,maxpages) * PAGE_SIZE - *start;
1053
1054 return __pipe_get_pages(i, min(maxsize, capacity), pages, idx, start);
1055}
1056
Al Viro62a80672014-04-04 23:12:29 -04001057ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001058 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001059 size_t *start)
1060{
Al Viroe5393fa2014-11-27 14:12:09 -05001061 if (maxsize > i->count)
1062 maxsize = i->count;
1063
Al Viro241699c2016-09-22 16:33:12 -04001064 if (unlikely(i->type & ITER_PIPE))
1065 return pipe_get_pages(i, pages, maxsize, maxpages, start);
Al Viroe5393fa2014-11-27 14:12:09 -05001066 iterate_all_kinds(i, maxsize, v, ({
1067 unsigned long addr = (unsigned long)v.iov_base;
1068 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1069 int n;
1070 int res;
1071
1072 if (len > maxpages * PAGE_SIZE)
1073 len = maxpages * PAGE_SIZE;
1074 addr &= ~(PAGE_SIZE - 1);
1075 n = DIV_ROUND_UP(len, PAGE_SIZE);
1076 res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, pages);
1077 if (unlikely(res < 0))
1078 return res;
1079 return (res == n ? len : res * PAGE_SIZE) - *start;
1080 0;}),({
1081 /* can't be more than PAGE_SIZE */
1082 *start = v.bv_offset;
1083 get_page(*pages = v.bv_page);
1084 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001085 }),({
1086 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001087 })
1088 )
1089 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001090}
1091EXPORT_SYMBOL(iov_iter_get_pages);
1092
Al Viro1b17f1f2014-11-27 14:14:31 -05001093static struct page **get_pages_array(size_t n)
1094{
Michal Hocko752ade62017-05-08 15:57:27 -07001095 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001096}
1097
Al Viro241699c2016-09-22 16:33:12 -04001098static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1099 struct page ***pages, size_t maxsize,
1100 size_t *start)
1101{
1102 struct page **p;
1103 size_t n;
1104 int idx;
1105 int npages;
1106
Al Viro33844e62016-12-21 21:55:02 -05001107 if (!maxsize)
1108 return 0;
1109
Al Viro241699c2016-09-22 16:33:12 -04001110 if (!sanity(i))
1111 return -EFAULT;
1112
1113 data_start(i, &idx, start);
1114 /* some of this one + all after this one */
1115 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1116 n = npages * PAGE_SIZE - *start;
1117 if (maxsize > n)
1118 maxsize = n;
1119 else
1120 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1121 p = get_pages_array(npages);
1122 if (!p)
1123 return -ENOMEM;
1124 n = __pipe_get_pages(i, maxsize, p, idx, start);
1125 if (n > 0)
1126 *pages = p;
1127 else
1128 kvfree(p);
1129 return n;
1130}
1131
Al Viro62a80672014-04-04 23:12:29 -04001132ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1133 struct page ***pages, size_t maxsize,
1134 size_t *start)
1135{
Al Viro1b17f1f2014-11-27 14:14:31 -05001136 struct page **p;
1137
1138 if (maxsize > i->count)
1139 maxsize = i->count;
1140
Al Viro241699c2016-09-22 16:33:12 -04001141 if (unlikely(i->type & ITER_PIPE))
1142 return pipe_get_pages_alloc(i, pages, maxsize, start);
Al Viro1b17f1f2014-11-27 14:14:31 -05001143 iterate_all_kinds(i, maxsize, v, ({
1144 unsigned long addr = (unsigned long)v.iov_base;
1145 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1146 int n;
1147 int res;
1148
1149 addr &= ~(PAGE_SIZE - 1);
1150 n = DIV_ROUND_UP(len, PAGE_SIZE);
1151 p = get_pages_array(n);
1152 if (!p)
1153 return -ENOMEM;
1154 res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, p);
1155 if (unlikely(res < 0)) {
1156 kvfree(p);
1157 return res;
1158 }
1159 *pages = p;
1160 return (res == n ? len : res * PAGE_SIZE) - *start;
1161 0;}),({
1162 /* can't be more than PAGE_SIZE */
1163 *start = v.bv_offset;
1164 *pages = p = get_pages_array(1);
1165 if (!p)
1166 return -ENOMEM;
1167 get_page(*p = v.bv_page);
1168 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001169 }),({
1170 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -05001171 })
1172 )
1173 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001174}
1175EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1176
Al Viroa604ec72014-11-24 01:08:00 -05001177size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1178 struct iov_iter *i)
1179{
1180 char *to = addr;
1181 __wsum sum, next;
1182 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001183 sum = *csum;
Al Viro241699c2016-09-22 16:33:12 -04001184 if (unlikely(i->type & ITER_PIPE)) {
1185 WARN_ON(1);
1186 return 0;
1187 }
Al Viroa604ec72014-11-24 01:08:00 -05001188 iterate_and_advance(i, bytes, v, ({
1189 int err = 0;
Al Virocbbd26b2016-11-01 22:09:04 -04001190 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001191 (to += v.iov_len) - v.iov_len,
1192 v.iov_len, 0, &err);
1193 if (!err) {
1194 sum = csum_block_add(sum, next, off);
1195 off += v.iov_len;
1196 }
1197 err ? v.iov_len : 0;
1198 }), ({
1199 char *p = kmap_atomic(v.bv_page);
1200 next = csum_partial_copy_nocheck(p + v.bv_offset,
1201 (to += v.bv_len) - v.bv_len,
1202 v.bv_len, 0);
1203 kunmap_atomic(p);
1204 sum = csum_block_add(sum, next, off);
1205 off += v.bv_len;
1206 }),({
1207 next = csum_partial_copy_nocheck(v.iov_base,
1208 (to += v.iov_len) - v.iov_len,
1209 v.iov_len, 0);
1210 sum = csum_block_add(sum, next, off);
1211 off += v.iov_len;
1212 })
1213 )
1214 *csum = sum;
1215 return bytes;
1216}
1217EXPORT_SYMBOL(csum_and_copy_from_iter);
1218
Al Virocbbd26b2016-11-01 22:09:04 -04001219bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1220 struct iov_iter *i)
1221{
1222 char *to = addr;
1223 __wsum sum, next;
1224 size_t off = 0;
1225 sum = *csum;
1226 if (unlikely(i->type & ITER_PIPE)) {
1227 WARN_ON(1);
1228 return false;
1229 }
1230 if (unlikely(i->count < bytes))
1231 return false;
1232 iterate_all_kinds(i, bytes, v, ({
1233 int err = 0;
1234 next = csum_and_copy_from_user(v.iov_base,
1235 (to += v.iov_len) - v.iov_len,
1236 v.iov_len, 0, &err);
1237 if (err)
1238 return false;
1239 sum = csum_block_add(sum, next, off);
1240 off += v.iov_len;
1241 0;
1242 }), ({
1243 char *p = kmap_atomic(v.bv_page);
1244 next = csum_partial_copy_nocheck(p + v.bv_offset,
1245 (to += v.bv_len) - v.bv_len,
1246 v.bv_len, 0);
1247 kunmap_atomic(p);
1248 sum = csum_block_add(sum, next, off);
1249 off += v.bv_len;
1250 }),({
1251 next = csum_partial_copy_nocheck(v.iov_base,
1252 (to += v.iov_len) - v.iov_len,
1253 v.iov_len, 0);
1254 sum = csum_block_add(sum, next, off);
1255 off += v.iov_len;
1256 })
1257 )
1258 *csum = sum;
1259 iov_iter_advance(i, bytes);
1260 return true;
1261}
1262EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1263
Al Viro36f7a8a2015-12-06 16:49:22 -05001264size_t csum_and_copy_to_iter(const void *addr, size_t bytes, __wsum *csum,
Al Viroa604ec72014-11-24 01:08:00 -05001265 struct iov_iter *i)
1266{
Al Viro36f7a8a2015-12-06 16:49:22 -05001267 const char *from = addr;
Al Viroa604ec72014-11-24 01:08:00 -05001268 __wsum sum, next;
1269 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001270 sum = *csum;
Al Viro241699c2016-09-22 16:33:12 -04001271 if (unlikely(i->type & ITER_PIPE)) {
1272 WARN_ON(1); /* for now */
1273 return 0;
1274 }
Al Viroa604ec72014-11-24 01:08:00 -05001275 iterate_and_advance(i, bytes, v, ({
1276 int err = 0;
1277 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001278 v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001279 v.iov_len, 0, &err);
1280 if (!err) {
1281 sum = csum_block_add(sum, next, off);
1282 off += v.iov_len;
1283 }
1284 err ? v.iov_len : 0;
1285 }), ({
1286 char *p = kmap_atomic(v.bv_page);
1287 next = csum_partial_copy_nocheck((from += v.bv_len) - v.bv_len,
1288 p + v.bv_offset,
1289 v.bv_len, 0);
1290 kunmap_atomic(p);
1291 sum = csum_block_add(sum, next, off);
1292 off += v.bv_len;
1293 }),({
1294 next = csum_partial_copy_nocheck((from += v.iov_len) - v.iov_len,
1295 v.iov_base,
1296 v.iov_len, 0);
1297 sum = csum_block_add(sum, next, off);
1298 off += v.iov_len;
1299 })
1300 )
1301 *csum = sum;
1302 return bytes;
1303}
1304EXPORT_SYMBOL(csum_and_copy_to_iter);
1305
Al Viro62a80672014-04-04 23:12:29 -04001306int iov_iter_npages(const struct iov_iter *i, int maxpages)
1307{
Al Viroe0f2dc42014-11-27 14:09:46 -05001308 size_t size = i->count;
1309 int npages = 0;
1310
1311 if (!size)
1312 return 0;
1313
Al Viro241699c2016-09-22 16:33:12 -04001314 if (unlikely(i->type & ITER_PIPE)) {
1315 struct pipe_inode_info *pipe = i->pipe;
1316 size_t off;
1317 int idx;
1318
1319 if (!sanity(i))
1320 return 0;
1321
1322 data_start(i, &idx, &off);
1323 /* some of this one + all after this one */
1324 npages = ((pipe->curbuf - idx - 1) & (pipe->buffers - 1)) + 1;
1325 if (npages >= maxpages)
1326 return maxpages;
1327 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001328 unsigned long p = (unsigned long)v.iov_base;
1329 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1330 - p / PAGE_SIZE;
1331 if (npages >= maxpages)
1332 return maxpages;
1333 0;}),({
1334 npages++;
1335 if (npages >= maxpages)
1336 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001337 }),({
1338 unsigned long p = (unsigned long)v.iov_base;
1339 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1340 - p / PAGE_SIZE;
1341 if (npages >= maxpages)
1342 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001343 })
1344 )
1345 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001346}
Al Virof67da302014-03-19 01:16:16 -04001347EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001348
1349const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1350{
1351 *new = *old;
Al Viro241699c2016-09-22 16:33:12 -04001352 if (unlikely(new->type & ITER_PIPE)) {
1353 WARN_ON(1);
1354 return NULL;
1355 }
Al Viro4b8164b2015-01-31 20:08:47 -05001356 if (new->type & ITER_BVEC)
1357 return new->bvec = kmemdup(new->bvec,
1358 new->nr_segs * sizeof(struct bio_vec),
1359 flags);
1360 else
1361 /* iovec and kvec have identical layout */
1362 return new->iov = kmemdup(new->iov,
1363 new->nr_segs * sizeof(struct iovec),
1364 flags);
1365}
1366EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001367
Vegard Nossumffecee42016-10-08 11:18:07 +02001368/**
1369 * import_iovec() - Copy an array of &struct iovec from userspace
1370 * into the kernel, check that it is valid, and initialize a new
1371 * &struct iov_iter iterator to access it.
1372 *
1373 * @type: One of %READ or %WRITE.
1374 * @uvector: Pointer to the userspace array.
1375 * @nr_segs: Number of elements in userspace array.
1376 * @fast_segs: Number of elements in @iov.
1377 * @iov: (input and output parameter) Pointer to pointer to (usually small
1378 * on-stack) kernel array.
1379 * @i: Pointer to iterator that will be initialized on success.
1380 *
1381 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1382 * then this function places %NULL in *@iov on return. Otherwise, a new
1383 * array will be allocated and the result placed in *@iov. This means that
1384 * the caller may call kfree() on *@iov regardless of whether the small
1385 * on-stack array was used or not (and regardless of whether this function
1386 * returns an error or not).
1387 *
1388 * Return: 0 on success or negative error code on error.
1389 */
Al Virobc917be2015-03-21 17:45:43 -04001390int import_iovec(int type, const struct iovec __user * uvector,
1391 unsigned nr_segs, unsigned fast_segs,
1392 struct iovec **iov, struct iov_iter *i)
1393{
1394 ssize_t n;
1395 struct iovec *p;
1396 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1397 *iov, &p);
1398 if (n < 0) {
1399 if (p != *iov)
1400 kfree(p);
1401 *iov = NULL;
1402 return n;
1403 }
1404 iov_iter_init(i, type, p, nr_segs, n);
1405 *iov = p == *iov ? NULL : p;
1406 return 0;
1407}
1408EXPORT_SYMBOL(import_iovec);
1409
1410#ifdef CONFIG_COMPAT
1411#include <linux/compat.h>
1412
1413int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
1414 unsigned nr_segs, unsigned fast_segs,
1415 struct iovec **iov, struct iov_iter *i)
1416{
1417 ssize_t n;
1418 struct iovec *p;
1419 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1420 *iov, &p);
1421 if (n < 0) {
1422 if (p != *iov)
1423 kfree(p);
1424 *iov = NULL;
1425 return n;
1426 }
1427 iov_iter_init(i, type, p, nr_segs, n);
1428 *iov = p == *iov ? NULL : p;
1429 return 0;
1430}
1431#endif
1432
1433int import_single_range(int rw, void __user *buf, size_t len,
1434 struct iovec *iov, struct iov_iter *i)
1435{
1436 if (len > MAX_RW_COUNT)
1437 len = MAX_RW_COUNT;
1438 if (unlikely(!access_ok(!rw, buf, len)))
1439 return -EFAULT;
1440
1441 iov->iov_base = buf;
1442 iov->iov_len = len;
1443 iov_iter_init(i, rw, iov, 1, len);
1444 return 0;
1445}
Al Viroe1267582015-12-06 20:38:56 -05001446EXPORT_SYMBOL(import_single_range);