1#include <linux/export.h>
2#include <linux/bvec.h>
3#include <linux/uio.h>
4#include <linux/pagemap.h>
5#include <linux/slab.h>
6#include <linux/vmalloc.h>
7#include <linux/splice.h>
8#include <net/checksum.h>
9#include <linux/scatterlist.h>
10
11#define PIPE_PARANOIA /* for now */
12
13#define iterate_iovec(i, n, __v, __p, skip, STEP) { \
14 size_t left; \
15 size_t wanted = n; \
16 __p = i->iov; \
17 __v.iov_len = min(n, __p->iov_len - skip); \
18 if (likely(__v.iov_len)) { \
19 __v.iov_base = __p->iov_base + skip; \
20 left = (STEP); \
21 __v.iov_len -= left; \
22 skip += __v.iov_len; \
23 n -= __v.iov_len; \
24 } else { \
25 left = 0; \
26 } \
27 while (unlikely(!left && n)) { \
28 __p++; \
29 __v.iov_len = min(n, __p->iov_len); \
30 if (unlikely(!__v.iov_len)) \
31 continue; \
32 __v.iov_base = __p->iov_base; \
33 left = (STEP); \
34 __v.iov_len -= left; \
35 skip = __v.iov_len; \
36 n -= __v.iov_len; \
37 } \
38 n = wanted - n; \
39}
40
41#define iterate_kvec(i, n, __v, __p, skip, STEP) { \
42 size_t wanted = n; \
43 __p = i->kvec; \
44 __v.iov_len = min(n, __p->iov_len - skip); \
45 if (likely(__v.iov_len)) { \
46 __v.iov_base = __p->iov_base + skip; \
47 (void)(STEP); \
48 skip += __v.iov_len; \
49 n -= __v.iov_len; \
50 } \
51 while (unlikely(n)) { \
52 __p++; \
53 __v.iov_len = min(n, __p->iov_len); \
54 if (unlikely(!__v.iov_len)) \
55 continue; \
56 __v.iov_base = __p->iov_base; \
57 (void)(STEP); \
58 skip = __v.iov_len; \
59 n -= __v.iov_len; \
60 } \
61 n = wanted; \
62}
63
64#define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
65 struct bvec_iter __start; \
66 __start.bi_size = n; \
67 __start.bi_bvec_done = skip; \
68 __start.bi_idx = 0; \
69 for_each_bvec(__v, i->bvec, __bi, __start) { \
70 if (!__v.bv_len) \
71 continue; \
72 (void)(STEP); \
73 } \
74}
75
76#define iterate_all_kinds(i, n, v, I, B, K) { \
77 if (likely(n)) { \
78 size_t skip = i->iov_offset; \
79 if (unlikely(i->type & ITER_BVEC)) { \
80 struct bio_vec v; \
81 struct bvec_iter __bi; \
82 iterate_bvec(i, n, v, __bi, skip, (B)) \
83 } else if (unlikely(i->type & ITER_KVEC)) { \
84 const struct kvec *kvec; \
85 struct kvec v; \
86 iterate_kvec(i, n, v, kvec, skip, (K)) \
87 } else if (unlikely(i->type & ITER_DISCARD)) { \
88 } else { \
89 const struct iovec *iov; \
90 struct iovec v; \
91 iterate_iovec(i, n, v, iov, skip, (I)) \
92 } \
93 } \
94}
95
96#define iterate_and_advance(i, n, v, I, B, K) { \
97 if (unlikely(i->count < n)) \
98 n = i->count; \
99 if (i->count) { \
100 size_t skip = i->iov_offset; \
101 if (unlikely(i->type & ITER_BVEC)) { \
102 const struct bio_vec *bvec = i->bvec; \
103 struct bio_vec v; \
104 struct bvec_iter __bi; \
105 iterate_bvec(i, n, v, __bi, skip, (B)) \
106 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
107 i->nr_segs -= i->bvec - bvec; \
108 skip = __bi.bi_bvec_done; \
109 } else if (unlikely(i->type & ITER_KVEC)) { \
110 const struct kvec *kvec; \
111 struct kvec v; \
112 iterate_kvec(i, n, v, kvec, skip, (K)) \
113 if (skip == kvec->iov_len) { \
114 kvec++; \
115 skip = 0; \
116 } \
117 i->nr_segs -= kvec - i->kvec; \
118 i->kvec = kvec; \
119 } else if (unlikely(i->type & ITER_DISCARD)) { \
120 skip += n; \
121 } else { \
122 const struct iovec *iov; \
123 struct iovec v; \
124 iterate_iovec(i, n, v, iov, skip, (I)) \
125 if (skip == iov->iov_len) { \
126 iov++; \
127 skip = 0; \
128 } \
129 i->nr_segs -= iov - i->iov; \
130 i->iov = iov; \
131 } \
132 i->count -= n; \
133 i->iov_offset = skip; \
134 } \
135}
136
137static int copyout(void __user *to, const void *from, size_t n)
138{
139 if (access_ok(to, n)) {
140 kasan_check_read(from, n);
141 n = raw_copy_to_user(to, from, n);
142 }
143 return n;
144}
145
146static int copyin(void *to, const void __user *from, size_t n)
147{
148 if (access_ok(from, n)) {
149 kasan_check_write(to, n);
150 n = raw_copy_from_user(to, from, n);
151 }
152 return n;
153}
154
155static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
156 struct iov_iter *i)
157{
158 size_t skip, copy, left, wanted;
159 const struct iovec *iov;
160 char __user *buf;
161 void *kaddr, *from;
162
163 if (unlikely(bytes > i->count))
164 bytes = i->count;
165
166 if (unlikely(!bytes))
167 return 0;
168
169 might_fault();
170 wanted = bytes;
171 iov = i->iov;
172 skip = i->iov_offset;
173 buf = iov->iov_base + skip;
174 copy = min(bytes, iov->iov_len - skip);
175
176 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
177 kaddr = kmap_atomic(page);
178 from = kaddr + offset;
179
180 /* first chunk, usually the only one */
181 left = copyout(buf, from, copy);
182 copy -= left;
183 skip += copy;
184 from += copy;
185 bytes -= copy;
186
187 while (unlikely(!left && bytes)) {
188 iov++;
189 buf = iov->iov_base;
190 copy = min(bytes, iov->iov_len);
191 left = copyout(buf, from, copy);
192 copy -= left;
193 skip = copy;
194 from += copy;
195 bytes -= copy;
196 }
197 if (likely(!bytes)) {
198 kunmap_atomic(kaddr);
199 goto done;
200 }
201 offset = from - kaddr;
202 buf += copy;
203 kunmap_atomic(kaddr);
204 copy = min(bytes, iov->iov_len - skip);
205 }
206 /* Too bad - revert to non-atomic kmap */
207
208 kaddr = kmap(page);
209 from = kaddr + offset;
210 left = copyout(buf, from, copy);
211 copy -= left;
212 skip += copy;
213 from += copy;
214 bytes -= copy;
215 while (unlikely(!left && bytes)) {
216 iov++;
217 buf = iov->iov_base;
218 copy = min(bytes, iov->iov_len);
219 left = copyout(buf, from, copy);
220 copy -= left;
221 skip = copy;
222 from += copy;
223 bytes -= copy;
224 }
225 kunmap(page);
226
227done:
228 if (skip == iov->iov_len) {
229 iov++;
230 skip = 0;
231 }
232 i->count -= wanted - bytes;
233 i->nr_segs -= iov - i->iov;
234 i->iov = iov;
235 i->iov_offset = skip;
236 return wanted - bytes;
237}
238
239static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
240 struct iov_iter *i)
241{
242 size_t skip, copy, left, wanted;
243 const struct iovec *iov;
244 char __user *buf;
245 void *kaddr, *to;
246
247 if (unlikely(bytes > i->count))
248 bytes = i->count;
249
250 if (unlikely(!bytes))
251 return 0;
252
253 might_fault();
254 wanted = bytes;
255 iov = i->iov;
256 skip = i->iov_offset;
257 buf = iov->iov_base + skip;
258 copy = min(bytes, iov->iov_len - skip);
259
260 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
261 kaddr = kmap_atomic(page);
262 to = kaddr + offset;
263
264 /* first chunk, usually the only one */
265 left = copyin(to, buf, copy);
266 copy -= left;
267 skip += copy;
268 to += copy;
269 bytes -= copy;
270
271 while (unlikely(!left && bytes)) {
272 iov++;
273 buf = iov->iov_base;
274 copy = min(bytes, iov->iov_len);
275 left = copyin(to, buf, copy);
276 copy -= left;
277 skip = copy;
278 to += copy;
279 bytes -= copy;
280 }
281 if (likely(!bytes)) {
282 kunmap_atomic(kaddr);
283 goto done;
284 }
285 offset = to - kaddr;
286 buf += copy;
287 kunmap_atomic(kaddr);
288 copy = min(bytes, iov->iov_len - skip);
289 }
290 /* Too bad - revert to non-atomic kmap */
291
292 kaddr = kmap(page);
293 to = kaddr + offset;
294 left = copyin(to, buf, copy);
295 copy -= left;
296 skip += copy;
297 to += copy;
298 bytes -= copy;
299 while (unlikely(!left && bytes)) {
300 iov++;
301 buf = iov->iov_base;
302 copy = min(bytes, iov->iov_len);
303 left = copyin(to, buf, copy);
304 copy -= left;
305 skip = copy;
306 to += copy;
307 bytes -= copy;
308 }
309 kunmap(page);
310
311done:
312 if (skip == iov->iov_len) {
313 iov++;
314 skip = 0;
315 }
316 i->count -= wanted - bytes;
317 i->nr_segs -= iov - i->iov;
318 i->iov = iov;
319 i->iov_offset = skip;
320 return wanted - bytes;
321}
322
323#ifdef PIPE_PARANOIA
324static bool sanity(const struct iov_iter *i)
325{
326 struct pipe_inode_info *pipe = i->pipe;
327 int idx = i->idx;
328 int next = pipe->curbuf + pipe->nrbufs;
329 if (i->iov_offset) {
330 struct pipe_buffer *p;
331 if (unlikely(!pipe->nrbufs))
332 goto Bad; // pipe must be non-empty
333 if (unlikely(idx != ((next - 1) & (pipe->buffers - 1))))
334 goto Bad; // must be at the last buffer...
335
336 p = &pipe->bufs[idx];
337 if (unlikely(p->offset + p->len != i->iov_offset))
338 goto Bad; // ... at the end of segment
339 } else {
340 if (idx != (next & (pipe->buffers - 1)))
341 goto Bad; // must be right after the last buffer
342 }
343 return true;
344Bad:
345 printk(KERN_ERR "idx = %d, offset = %zd\n", i->idx, i->iov_offset);
346 printk(KERN_ERR "curbuf = %d, nrbufs = %d, buffers = %d\n",
347 pipe->curbuf, pipe->nrbufs, pipe->buffers);
348 for (idx = 0; idx < pipe->buffers; idx++)
349 printk(KERN_ERR "[%p %p %d %d]\n",
350 pipe->bufs[idx].ops,
351 pipe->bufs[idx].page,
352 pipe->bufs[idx].offset,
353 pipe->bufs[idx].len);
354 WARN_ON(1);
355 return false;
356}
357#else
358#define sanity(i) true
359#endif
360
361static inline int next_idx(int idx, struct pipe_inode_info *pipe)
362{
363 return (idx + 1) & (pipe->buffers - 1);
364}
365
366static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
367 struct iov_iter *i)
368{
369 struct pipe_inode_info *pipe = i->pipe;
370 struct pipe_buffer *buf;
371 size_t off;
372 int idx;
373
374 if (unlikely(bytes > i->count))
375 bytes = i->count;
376
377 if (unlikely(!bytes))
378 return 0;
379
380 if (!sanity(i))
381 return 0;
382
383 off = i->iov_offset;
384 idx = i->idx;
385 buf = &pipe->bufs[idx];
386 if (off) {
387 if (offset == off && buf->page == page) {
388 /* merge with the last one */
389 buf->len += bytes;
390 i->iov_offset += bytes;
391 goto out;
392 }
393 idx = next_idx(idx, pipe);
394 buf = &pipe->bufs[idx];
395 }
396 if (idx == pipe->curbuf && pipe->nrbufs)
397 return 0;
398 pipe->nrbufs++;
399 buf->ops = &page_cache_pipe_buf_ops;
400 get_page(buf->page = page);
401 buf->offset = offset;
402 buf->len = bytes;
403 i->iov_offset = offset + bytes;
404 i->idx = idx;
405out:
406 i->count -= bytes;
407 return bytes;
408}
409
410/*
411 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
412 * bytes. For each iovec, fault in each page that constitutes the iovec.
413 *
414 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
415 * because it is an invalid address).
416 */
417int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
418{
419 size_t skip = i->iov_offset;
420 const struct iovec *iov;
421 int err;
422 struct iovec v;
423
424 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
425 iterate_iovec(i, bytes, v, iov, skip, ({
426 err = fault_in_pages_readable(v.iov_base, v.iov_len);
427 if (unlikely(err))
428 return err;
429 0;}))
430 }
431 return 0;
432}
433EXPORT_SYMBOL(iov_iter_fault_in_readable);
434
435void iov_iter_init(struct iov_iter *i, unsigned int direction,
436 const struct iovec *iov, unsigned long nr_segs,
437 size_t count)
438{
439 WARN_ON(direction & ~(READ | WRITE));
440 direction &= READ | WRITE;
441
442 /* It will get better. Eventually... */
443 if (uaccess_kernel()) {
444 i->type = ITER_KVEC | direction;
445 i->kvec = (struct kvec *)iov;
446 } else {
447 i->type = ITER_IOVEC | direction;
448 i->iov = iov;
449 }
450 i->nr_segs = nr_segs;
451 i->iov_offset = 0;
452 i->count = count;
453}
454EXPORT_SYMBOL(iov_iter_init);
455
456static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
457{
458 char *from = kmap_atomic(page);
459 memcpy(to, from + offset, len);
460 kunmap_atomic(from);
461}
462
463static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
464{
465 char *to = kmap_atomic(page);
466 memcpy(to + offset, from, len);
467 kunmap_atomic(to);
468}
469
470static void memzero_page(struct page *page, size_t offset, size_t len)
471{
472 char *addr = kmap_atomic(page);
473 memset(addr + offset, 0, len);
474 kunmap_atomic(addr);
475}
476
477static inline bool allocated(struct pipe_buffer *buf)
478{
479 return buf->ops == &default_pipe_buf_ops;
480}
481
482static inline void data_start(const struct iov_iter *i, int *idxp, size_t *offp)
483{
484 size_t off = i->iov_offset;
485 int idx = i->idx;
486 if (off && (!allocated(&i->pipe->bufs[idx]) || off == PAGE_SIZE)) {
487 idx = next_idx(idx, i->pipe);
488 off = 0;
489 }
490 *idxp = idx;
491 *offp = off;
492}
493
494static size_t push_pipe(struct iov_iter *i, size_t size,
495 int *idxp, size_t *offp)
496{
497 struct pipe_inode_info *pipe = i->pipe;
498 size_t off;
499 int idx;
500 ssize_t left;
501
502 if (unlikely(size > i->count))
503 size = i->count;
504 if (unlikely(!size))
505 return 0;
506
507 left = size;
508 data_start(i, &idx, &off);
509 *idxp = idx;
510 *offp = off;
511 if (off) {
512 left -= PAGE_SIZE - off;
513 if (left <= 0) {
514 pipe->bufs[idx].len += size;
515 return size;
516 }
517 pipe->bufs[idx].len = PAGE_SIZE;
518 idx = next_idx(idx, pipe);
519 }
520 while (idx != pipe->curbuf || !pipe->nrbufs) {
521 struct page *page = alloc_page(GFP_USER);
522 if (!page)
523 break;
524 pipe->nrbufs++;
525 pipe->bufs[idx].ops = &default_pipe_buf_ops;
526 pipe->bufs[idx].page = page;
527 pipe->bufs[idx].offset = 0;
528 if (left <= PAGE_SIZE) {
529 pipe->bufs[idx].len = left;
530 return size;
531 }
532 pipe->bufs[idx].len = PAGE_SIZE;
533 left -= PAGE_SIZE;
534 idx = next_idx(idx, pipe);
535 }
536 return size - left;
537}
538
539static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
540 struct iov_iter *i)
541{
542 struct pipe_inode_info *pipe = i->pipe;
543 size_t n, off;
544 int idx;
545
546 if (!sanity(i))
547 return 0;
548
549 bytes = n = push_pipe(i, bytes, &idx, &off);
550 if (unlikely(!n))
551 return 0;
552 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
553 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
554 memcpy_to_page(pipe->bufs[idx].page, off, addr, chunk);
555 i->idx = idx;
556 i->iov_offset = off + chunk;
557 n -= chunk;
558 addr += chunk;
559 }
560 i->count -= bytes;
561 return bytes;
562}
563
564static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
565 __wsum sum, size_t off)
566{
567 __wsum next = csum_partial_copy_nocheck(from, to, len, 0);
568 return csum_block_add(sum, next, off);
569}
570
571static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
572 __wsum *csum, struct iov_iter *i)
573{
574 struct pipe_inode_info *pipe = i->pipe;
575 size_t n, r;
576 size_t off = 0;
577 __wsum sum = *csum;
578 int idx;
579
580 if (!sanity(i))
581 return 0;
582
583 bytes = n = push_pipe(i, bytes, &idx, &r);
584 if (unlikely(!n))
585 return 0;
586 for ( ; n; idx = next_idx(idx, pipe), r = 0) {
587 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
588 char *p = kmap_atomic(pipe->bufs[idx].page);
589 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
590 kunmap_atomic(p);
591 i->idx = idx;
592 i->iov_offset = r + chunk;
593 n -= chunk;
594 off += chunk;
595 addr += chunk;
596 }
597 i->count -= bytes;
598 *csum = sum;
599 return bytes;
600}
601
602size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
603{
604 const char *from = addr;
605 if (unlikely(iov_iter_is_pipe(i)))
606 return copy_pipe_to_iter(addr, bytes, i);
607 if (iter_is_iovec(i))
608 might_fault();
609 iterate_and_advance(i, bytes, v,
610 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
611 memcpy_to_page(v.bv_page, v.bv_offset,
612 (from += v.bv_len) - v.bv_len, v.bv_len),
613 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
614 )
615
616 return bytes;
617}
618EXPORT_SYMBOL(_copy_to_iter);
619
620#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
621static int copyout_mcsafe(void __user *to, const void *from, size_t n)
622{
623 if (access_ok(to, n)) {
624 kasan_check_read(from, n);
625 n = copy_to_user_mcsafe((__force void *) to, from, n);
626 }
627 return n;
628}
629
630static unsigned long memcpy_mcsafe_to_page(struct page *page, size_t offset,
631 const char *from, size_t len)
632{
633 unsigned long ret;
634 char *to;
635
636 to = kmap_atomic(page);
637 ret = memcpy_mcsafe(to + offset, from, len);
638 kunmap_atomic(to);
639
640 return ret;
641}
642
643static size_t copy_pipe_to_iter_mcsafe(const void *addr, size_t bytes,
644 struct iov_iter *i)
645{
646 struct pipe_inode_info *pipe = i->pipe;
647 size_t n, off, xfer = 0;
648 int idx;
649
650 if (!sanity(i))
651 return 0;
652
653 bytes = n = push_pipe(i, bytes, &idx, &off);
654 if (unlikely(!n))
655 return 0;
656 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
657 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
658 unsigned long rem;
659
660 rem = memcpy_mcsafe_to_page(pipe->bufs[idx].page, off, addr,
661 chunk);
662 i->idx = idx;
663 i->iov_offset = off + chunk - rem;
664 xfer += chunk - rem;
665 if (rem)
666 break;
667 n -= chunk;
668 addr += chunk;
669 }
670 i->count -= xfer;
671 return xfer;
672}
673
674/**
675 * _copy_to_iter_mcsafe - copy to user with source-read error exception handling
676 * @addr: source kernel address
677 * @bytes: total transfer length
678 * @iter: destination iterator
679 *
680 * The pmem driver arranges for filesystem-dax to use this facility via
681 * dax_copy_to_iter() for protecting read/write to persistent memory.
682 * Unless / until an architecture can guarantee identical performance
683 * between _copy_to_iter_mcsafe() and _copy_to_iter() it would be a
684 * performance regression to switch more users to the mcsafe version.
685 *
686 * Otherwise, the main differences between this and typical _copy_to_iter().
687 *
688 * * Typical tail/residue handling after a fault retries the copy
689 * byte-by-byte until the fault happens again. Re-triggering machine
690 * checks is potentially fatal so the implementation uses source
691 * alignment and poison alignment assumptions to avoid re-triggering
692 * hardware exceptions.
693 *
694 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
695 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
696 * a short copy.
697 *
698 * See MCSAFE_TEST for self-test.
699 */
700size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i)
701{
702 const char *from = addr;
703 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
704
705 if (unlikely(iov_iter_is_pipe(i)))
706 return copy_pipe_to_iter_mcsafe(addr, bytes, i);
707 if (iter_is_iovec(i))
708 might_fault();
709 iterate_and_advance(i, bytes, v,
710 copyout_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
711 ({
712 rem = memcpy_mcsafe_to_page(v.bv_page, v.bv_offset,
713 (from += v.bv_len) - v.bv_len, v.bv_len);
714 if (rem) {
715 curr_addr = (unsigned long) from;
716 bytes = curr_addr - s_addr - rem;
717 return bytes;
718 }
719 }),
720 ({
721 rem = memcpy_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len,
722 v.iov_len);
723 if (rem) {
724 curr_addr = (unsigned long) from;
725 bytes = curr_addr - s_addr - rem;
726 return bytes;
727 }
728 })
729 )
730
731 return bytes;
732}
733EXPORT_SYMBOL_GPL(_copy_to_iter_mcsafe);
734#endif /* CONFIG_ARCH_HAS_UACCESS_MCSAFE */
735
736size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
737{
738 char *to = addr;
739 if (unlikely(iov_iter_is_pipe(i))) {
740 WARN_ON(1);
741 return 0;
742 }
743 if (iter_is_iovec(i))
744 might_fault();
745 iterate_and_advance(i, bytes, v,
746 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
747 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
748 v.bv_offset, v.bv_len),
749 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
750 )
751
752 return bytes;
753}
754EXPORT_SYMBOL(_copy_from_iter);
755
756bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
757{
758 char *to = addr;
759 if (unlikely(iov_iter_is_pipe(i))) {
760 WARN_ON(1);
761 return false;
762 }
763 if (unlikely(i->count < bytes))
764 return false;
765
766 if (iter_is_iovec(i))
767 might_fault();
768 iterate_all_kinds(i, bytes, v, ({
769 if (copyin((to += v.iov_len) - v.iov_len,
770 v.iov_base, v.iov_len))
771 return false;
772 0;}),
773 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
774 v.bv_offset, v.bv_len),
775 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
776 )
777
778 iov_iter_advance(i, bytes);
779 return true;
780}
781EXPORT_SYMBOL(_copy_from_iter_full);
782
783size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
784{
785 char *to = addr;
786 if (unlikely(iov_iter_is_pipe(i))) {
787 WARN_ON(1);
788 return 0;
789 }
790 iterate_and_advance(i, bytes, v,
791 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
792 v.iov_base, v.iov_len),
793 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
794 v.bv_offset, v.bv_len),
795 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
796 )
797
798 return bytes;
799}
800EXPORT_SYMBOL(_copy_from_iter_nocache);
801
802#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
803/**
804 * _copy_from_iter_flushcache - write destination through cpu cache
805 * @addr: destination kernel address
806 * @bytes: total transfer length
807 * @iter: source iterator
808 *
809 * The pmem driver arranges for filesystem-dax to use this facility via
810 * dax_copy_from_iter() for ensuring that writes to persistent memory
811 * are flushed through the CPU cache. It is differentiated from
812 * _copy_from_iter_nocache() in that guarantees all data is flushed for
813 * all iterator types. The _copy_from_iter_nocache() only attempts to
814 * bypass the cache for the ITER_IOVEC case, and on some archs may use
815 * instructions that strand dirty-data in the cache.
816 */
817size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
818{
819 char *to = addr;
820 if (unlikely(iov_iter_is_pipe(i))) {
821 WARN_ON(1);
822 return 0;
823 }
824 iterate_and_advance(i, bytes, v,
825 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
826 v.iov_base, v.iov_len),
827 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
828 v.bv_offset, v.bv_len),
829 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
830 v.iov_len)
831 )
832
833 return bytes;
834}
835EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
836#endif
837
838bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
839{
840 char *to = addr;
841 if (unlikely(iov_iter_is_pipe(i))) {
842 WARN_ON(1);
843 return false;
844 }
845 if (unlikely(i->count < bytes))
846 return false;
847 iterate_all_kinds(i, bytes, v, ({
848 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
849 v.iov_base, v.iov_len))
850 return false;
851 0;}),
852 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
853 v.bv_offset, v.bv_len),
854 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
855 )
856
857 iov_iter_advance(i, bytes);
858 return true;
859}
860EXPORT_SYMBOL(_copy_from_iter_full_nocache);
861
862static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
863{
864 struct page *head;
865 size_t v = n + offset;
866
867 /*
868 * The general case needs to access the page order in order
869 * to compute the page size.
870 * However, we mostly deal with order-0 pages and thus can
871 * avoid a possible cache line miss for requests that fit all
872 * page orders.
873 */
874 if (n <= v && v <= PAGE_SIZE)
875 return true;
876
877 head = compound_head(page);
878 v += (page - head) << PAGE_SHIFT;
879
880 if (likely(n <= v && v <= (PAGE_SIZE << compound_order(head))))
881 return true;
882 WARN_ON(1);
883 return false;
884}
885
886size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
887 struct iov_iter *i)
888{
889 if (unlikely(!page_copy_sane(page, offset, bytes)))
890 return 0;
891 if (i->type & (ITER_BVEC|ITER_KVEC)) {
892 void *kaddr = kmap_atomic(page);
893 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
894 kunmap_atomic(kaddr);
895 return wanted;
896 } else if (unlikely(iov_iter_is_discard(i)))
897 return bytes;
898 else if (likely(!iov_iter_is_pipe(i)))
899 return copy_page_to_iter_iovec(page, offset, bytes, i);
900 else
901 return copy_page_to_iter_pipe(page, offset, bytes, i);
902}
903EXPORT_SYMBOL(copy_page_to_iter);
904
905size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
906 struct iov_iter *i)
907{
908 if (unlikely(!page_copy_sane(page, offset, bytes)))
909 return 0;
910 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
911 WARN_ON(1);
912 return 0;
913 }
914 if (i->type & (ITER_BVEC|ITER_KVEC)) {
915 void *kaddr = kmap_atomic(page);
916 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
917 kunmap_atomic(kaddr);
918 return wanted;
919 } else
920 return copy_page_from_iter_iovec(page, offset, bytes, i);
921}
922EXPORT_SYMBOL(copy_page_from_iter);
923
924static size_t pipe_zero(size_t bytes, struct iov_iter *i)
925{
926 struct pipe_inode_info *pipe = i->pipe;
927 size_t n, off;
928 int idx;
929
930 if (!sanity(i))
931 return 0;
932
933 bytes = n = push_pipe(i, bytes, &idx, &off);
934 if (unlikely(!n))
935 return 0;
936
937 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
938 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
939 memzero_page(pipe->bufs[idx].page, off, chunk);
940 i->idx = idx;
941 i->iov_offset = off + chunk;
942 n -= chunk;
943 }
944 i->count -= bytes;
945 return bytes;
946}
947
948size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
949{
950 if (unlikely(iov_iter_is_pipe(i)))
951 return pipe_zero(bytes, i);
952