ARM: dts: uniphier: add MIO DMAC nodes
[linux/fpc-iii.git] / lib / iov_iter.c
blob7ebccb5c16377eb77176d0df689e1cfb7af7b36d
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>
10 #define PIPE_PARANOIA /* for now */
12 #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; \
40 #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; \
63 #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) \
70 continue; \
71 (void)(STEP); \
72 } \
75 #define iterate_all_kinds(i, n, v, I, B, K) { \
76 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 if (unlikely(i->type & ITER_DISCARD)) { \
87 } else { \
88 const struct iovec *iov; \
89 struct iovec v; \
90 iterate_iovec(i, n, v, iov, skip, (I)) \
91 } \
92 } \
95 #define iterate_and_advance(i, n, v, I, B, K) { \
96 if (unlikely(i->count < n)) \
97 n = i->count; \
98 if (i->count) { \
99 size_t skip = i->iov_offset; \
100 if (unlikely(i->type & ITER_BVEC)) { \
101 const struct bio_vec *bvec = i->bvec; \
102 struct bio_vec v; \
103 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; \
108 } 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; \
116 i->nr_segs -= kvec - i->kvec; \
117 i->kvec = kvec; \
118 } else if (unlikely(i->type & ITER_DISCARD)) { \
119 skip += n; \
120 } 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; \
128 i->nr_segs -= iov - i->iov; \
129 i->iov = iov; \
131 i->count -= n; \
132 i->iov_offset = skip; \
136 static int copyout(void __user *to, const void *from, size_t n)
138 if (access_ok(VERIFY_WRITE, to, n)) {
139 kasan_check_read(from, n);
140 n = raw_copy_to_user(to, from, n);
142 return n;
145 static int copyin(void *to, const void __user *from, size_t n)
147 if (access_ok(VERIFY_READ, from, n)) {
148 kasan_check_write(to, n);
149 n = raw_copy_from_user(to, from, n);
151 return n;
154 static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
155 struct iov_iter *i)
157 size_t skip, copy, left, wanted;
158 const struct iovec *iov;
159 char __user *buf;
160 void *kaddr, *from;
162 if (unlikely(bytes > i->count))
163 bytes = i->count;
165 if (unlikely(!bytes))
166 return 0;
168 might_fault();
169 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);
175 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
176 kaddr = kmap_atomic(page);
177 from = kaddr + offset;
179 /* first chunk, usually the only one */
180 left = copyout(buf, from, copy);
181 copy -= left;
182 skip += copy;
183 from += copy;
184 bytes -= copy;
186 while (unlikely(!left && bytes)) {
187 iov++;
188 buf = iov->iov_base;
189 copy = min(bytes, iov->iov_len);
190 left = copyout(buf, from, copy);
191 copy -= left;
192 skip = copy;
193 from += copy;
194 bytes -= copy;
196 if (likely(!bytes)) {
197 kunmap_atomic(kaddr);
198 goto done;
200 offset = from - kaddr;
201 buf += copy;
202 kunmap_atomic(kaddr);
203 copy = min(bytes, iov->iov_len - skip);
205 /* Too bad - revert to non-atomic kmap */
207 kaddr = kmap(page);
208 from = kaddr + offset;
209 left = copyout(buf, from, copy);
210 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);
218 left = copyout(buf, from, copy);
219 copy -= left;
220 skip = copy;
221 from += copy;
222 bytes -= copy;
224 kunmap(page);
226 done:
227 if (skip == iov->iov_len) {
228 iov++;
229 skip = 0;
231 i->count -= wanted - bytes;
232 i->nr_segs -= iov - i->iov;
233 i->iov = iov;
234 i->iov_offset = skip;
235 return wanted - bytes;
238 static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
239 struct iov_iter *i)
241 size_t skip, copy, left, wanted;
242 const struct iovec *iov;
243 char __user *buf;
244 void *kaddr, *to;
246 if (unlikely(bytes > i->count))
247 bytes = i->count;
249 if (unlikely(!bytes))
250 return 0;
252 might_fault();
253 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);
259 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
260 kaddr = kmap_atomic(page);
261 to = kaddr + offset;
263 /* first chunk, usually the only one */
264 left = copyin(to, buf, copy);
265 copy -= left;
266 skip += copy;
267 to += copy;
268 bytes -= copy;
270 while (unlikely(!left && bytes)) {
271 iov++;
272 buf = iov->iov_base;
273 copy = min(bytes, iov->iov_len);
274 left = copyin(to, buf, copy);
275 copy -= left;
276 skip = copy;
277 to += copy;
278 bytes -= copy;
280 if (likely(!bytes)) {
281 kunmap_atomic(kaddr);
282 goto done;
284 offset = to - kaddr;
285 buf += copy;
286 kunmap_atomic(kaddr);
287 copy = min(bytes, iov->iov_len - skip);
289 /* Too bad - revert to non-atomic kmap */
291 kaddr = kmap(page);
292 to = kaddr + offset;
293 left = copyin(to, buf, copy);
294 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);
302 left = copyin(to, buf, copy);
303 copy -= left;
304 skip = copy;
305 to += copy;
306 bytes -= copy;
308 kunmap(page);
310 done:
311 if (skip == iov->iov_len) {
312 iov++;
313 skip = 0;
315 i->count -= wanted - bytes;
316 i->nr_segs -= iov - i->iov;
317 i->iov = iov;
318 i->iov_offset = skip;
319 return wanted - bytes;
322 #ifdef PIPE_PARANOIA
323 static bool sanity(const struct iov_iter *i)
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...
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
342 return true;
343 Bad:
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;
356 #else
357 #define sanity(i) true
358 #endif
360 static inline int next_idx(int idx, struct pipe_inode_info *pipe)
362 return (idx + 1) & (pipe->buffers - 1);
365 static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
366 struct iov_iter *i)
368 struct pipe_inode_info *pipe = i->pipe;
369 struct pipe_buffer *buf;
370 size_t off;
371 int idx;
373 if (unlikely(bytes > i->count))
374 bytes = i->count;
376 if (unlikely(!bytes))
377 return 0;
379 if (!sanity(i))
380 return 0;
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;
392 idx = next_idx(idx, pipe);
393 buf = &pipe->bufs[idx];
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;
404 out:
405 i->count -= bytes;
406 return bytes;
410 * 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.
413 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
414 * because it is an invalid address).
416 int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
418 size_t skip = i->iov_offset;
419 const struct iovec *iov;
420 int err;
421 struct iovec v;
423 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
424 iterate_iovec(i, bytes, v, iov, skip, ({
425 err = fault_in_pages_readable(v.iov_base, v.iov_len);
426 if (unlikely(err))
427 return err;
428 0;}))
430 return 0;
432 EXPORT_SYMBOL(iov_iter_fault_in_readable);
434 void iov_iter_init(struct iov_iter *i, unsigned int direction,
435 const struct iovec *iov, unsigned long nr_segs,
436 size_t count)
438 WARN_ON(direction & ~(READ | WRITE));
439 direction &= READ | WRITE;
441 /* It will get better. Eventually... */
442 if (uaccess_kernel()) {
443 i->type = ITER_KVEC | direction;
444 i->kvec = (struct kvec *)iov;
445 } else {
446 i->type = ITER_IOVEC | direction;
447 i->iov = iov;
449 i->nr_segs = nr_segs;
450 i->iov_offset = 0;
451 i->count = count;
453 EXPORT_SYMBOL(iov_iter_init);
455 static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
457 char *from = kmap_atomic(page);
458 memcpy(to, from + offset, len);
459 kunmap_atomic(from);
462 static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
464 char *to = kmap_atomic(page);
465 memcpy(to + offset, from, len);
466 kunmap_atomic(to);
469 static void memzero_page(struct page *page, size_t offset, size_t len)
471 char *addr = kmap_atomic(page);
472 memset(addr + offset, 0, len);
473 kunmap_atomic(addr);
476 static inline bool allocated(struct pipe_buffer *buf)
478 return buf->ops == &default_pipe_buf_ops;
481 static inline void data_start(const struct iov_iter *i, int *idxp, size_t *offp)
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;
489 *idxp = idx;
490 *offp = off;
493 static size_t push_pipe(struct iov_iter *i, size_t size,
494 int *idxp, size_t *offp)
496 struct pipe_inode_info *pipe = i->pipe;
497 size_t off;
498 int idx;
499 ssize_t left;
501 if (unlikely(size > i->count))
502 size = i->count;
503 if (unlikely(!size))
504 return 0;
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;
516 pipe->bufs[idx].len = PAGE_SIZE;
517 idx = next_idx(idx, pipe);
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;
531 pipe->bufs[idx].len = PAGE_SIZE;
532 left -= PAGE_SIZE;
533 idx = next_idx(idx, pipe);
535 return size - left;
538 static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
539 struct iov_iter *i)
541 struct pipe_inode_info *pipe = i->pipe;
542 size_t n, off;
543 int idx;
545 if (!sanity(i))
546 return 0;
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;
559 i->count -= bytes;
560 return bytes;
563 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
565 const char *from = addr;
566 if (unlikely(iov_iter_is_pipe(i)))
567 return copy_pipe_to_iter(addr, bytes, i);
568 if (iter_is_iovec(i))
569 might_fault();
570 iterate_and_advance(i, bytes, v,
571 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
572 memcpy_to_page(v.bv_page, v.bv_offset,
573 (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)
577 return bytes;
579 EXPORT_SYMBOL(_copy_to_iter);
581 #ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
582 static int copyout_mcsafe(void __user *to, const void *from, size_t n)
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);
588 return n;
591 static unsigned long memcpy_mcsafe_to_page(struct page *page, size_t offset,
592 const char *from, size_t len)
594 unsigned long ret;
595 char *to;
597 to = kmap_atomic(page);
598 ret = memcpy_mcsafe(to + offset, from, len);
599 kunmap_atomic(to);
601 return ret;
604 static size_t copy_pipe_to_iter_mcsafe(const void *addr, size_t bytes,
605 struct iov_iter *i)
607 struct pipe_inode_info *pipe = i->pipe;
608 size_t n, off, xfer = 0;
609 int idx;
611 if (!sanity(i))
612 return 0;
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;
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;
631 i->count -= xfer;
632 return xfer;
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
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.
647 * Otherwise, the main differences between this and typical _copy_to_iter().
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.
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.
659 * See MCSAFE_TEST for self-test.
661 size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i)
663 const char *from = addr;
664 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
666 if (unlikely(iov_iter_is_pipe(i)))
667 return copy_pipe_to_iter_mcsafe(addr, bytes, i);
668 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),
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;
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;
692 return bytes;
694 EXPORT_SYMBOL_GPL(_copy_to_iter_mcsafe);
695 #endif /* CONFIG_ARCH_HAS_UACCESS_MCSAFE */
697 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
699 char *to = addr;
700 if (unlikely(iov_iter_is_pipe(i))) {
701 WARN_ON(1);
702 return 0;
704 if (iter_is_iovec(i))
705 might_fault();
706 iterate_and_advance(i, bytes, v,
707 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
708 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
709 v.bv_offset, v.bv_len),
710 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
713 return bytes;
715 EXPORT_SYMBOL(_copy_from_iter);
717 bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
719 char *to = addr;
720 if (unlikely(iov_iter_is_pipe(i))) {
721 WARN_ON(1);
722 return false;
724 if (unlikely(i->count < bytes))
725 return false;
727 if (iter_is_iovec(i))
728 might_fault();
729 iterate_all_kinds(i, bytes, v, ({
730 if (copyin((to += v.iov_len) - v.iov_len,
731 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)
739 iov_iter_advance(i, bytes);
740 return true;
742 EXPORT_SYMBOL(_copy_from_iter_full);
744 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
746 char *to = addr;
747 if (unlikely(iov_iter_is_pipe(i))) {
748 WARN_ON(1);
749 return 0;
751 iterate_and_advance(i, bytes, v,
752 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
753 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)
759 return bytes;
761 EXPORT_SYMBOL(_copy_from_iter_nocache);
763 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
765 * _copy_from_iter_flushcache - write destination through cpu cache
766 * @addr: destination kernel address
767 * @bytes: total transfer length
768 * @iter: source iterator
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.
778 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
780 char *to = addr;
781 if (unlikely(iov_iter_is_pipe(i))) {
782 WARN_ON(1);
783 return 0;
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)
794 return bytes;
796 EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
797 #endif
799 bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
801 char *to = addr;
802 if (unlikely(iov_iter_is_pipe(i))) {
803 WARN_ON(1);
804 return false;
806 if (unlikely(i->count < bytes))
807 return false;
808 iterate_all_kinds(i, bytes, v, ({
809 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
810 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)
818 iov_iter_advance(i, bytes);
819 return true;
821 EXPORT_SYMBOL(_copy_from_iter_full_nocache);
823 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
825 struct page *head = compound_head(page);
826 size_t v = n + offset + page_address(page) - page_address(head);
828 if (likely(n <= v && v <= (PAGE_SIZE << compound_order(head))))
829 return true;
830 WARN_ON(1);
831 return false;
834 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
835 struct iov_iter *i)
837 if (unlikely(!page_copy_sane(page, offset, bytes)))
838 return 0;
839 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;
844 } else if (unlikely(iov_iter_is_discard(i)))
845 return bytes;
846 else if (likely(!iov_iter_is_pipe(i)))
847 return copy_page_to_iter_iovec(page, offset, bytes, i);
848 else
849 return copy_page_to_iter_pipe(page, offset, bytes, i);
851 EXPORT_SYMBOL(copy_page_to_iter);
853 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
854 struct iov_iter *i)
856 if (unlikely(!page_copy_sane(page, offset, bytes)))
857 return 0;
858 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
859 WARN_ON(1);
860 return 0;
862 if (i->type & (ITER_BVEC|ITER_KVEC)) {
863 void *kaddr = kmap_atomic(page);
864 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
865 kunmap_atomic(kaddr);
866 return wanted;
867 } else
868 return copy_page_from_iter_iovec(page, offset, bytes, i);
870 EXPORT_SYMBOL(copy_page_from_iter);
872 static size_t pipe_zero(size_t bytes, struct iov_iter *i)
874 struct pipe_inode_info *pipe = i->pipe;
875 size_t n, off;
876 int idx;
878 if (!sanity(i))
879 return 0;
881 bytes = n = push_pipe(i, bytes, &idx, &off);
882 if (unlikely(!n))
883 return 0;
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;
892 i->count -= bytes;
893 return bytes;
896 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
898 if (unlikely(iov_iter_is_pipe(i)))
899 return pipe_zero(bytes, i);
900 iterate_and_advance(i, bytes, v,
901 clear_user(v.iov_base, v.iov_len),
902 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
903 memset(v.iov_base, 0, v.iov_len)
906 return bytes;
908 EXPORT_SYMBOL(iov_iter_zero);
910 size_t iov_iter_copy_from_user_atomic(struct page *page,
911 struct iov_iter *i, unsigned long offset, size_t bytes)
913 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
914 if (unlikely(!page_copy_sane(page, offset, bytes))) {
915 kunmap_atomic(kaddr);
916 return 0;
918 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
919 kunmap_atomic(kaddr);
920 WARN_ON(1);
921 return 0;
923 iterate_all_kinds(i, bytes, v,
924 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
925 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
926 v.bv_offset, v.bv_len),
927 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
929 kunmap_atomic(kaddr);
930 return bytes;
932 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
934 static inline void pipe_truncate(struct iov_iter *i)
936 struct pipe_inode_info *pipe = i->pipe;
937 if (pipe->nrbufs) {
938 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++;
946 while (pipe->nrbufs > nrbufs) {
947 pipe_buf_release(pipe, &pipe->bufs[idx]);
948 idx = next_idx(idx, pipe);
949 pipe->nrbufs--;
954 static void pipe_advance(struct iov_iter *i, size_t size)
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);
972 i->idx = idx;
973 i->iov_offset = buf->offset + left;
975 i->count -= size;
976 /* ... and discard everything past that point */
977 pipe_truncate(i);
980 void iov_iter_advance(struct iov_iter *i, size_t size)
982 if (unlikely(iov_iter_is_pipe(i))) {
983 pipe_advance(i, size);
984 return;
986 if (unlikely(iov_iter_is_discard(i))) {
987 i->count -= size;
988 return;
990 iterate_and_advance(i, size, v, 0, 0, 0)
992 EXPORT_SYMBOL(iov_iter_advance);
994 void iov_iter_revert(struct iov_iter *i, size_t unroll)
996 if (!unroll)
997 return;
998 if (WARN_ON(unroll > MAX_RW_COUNT))
999 return;
1000 i->count += unroll;
1001 if (unlikely(iov_iter_is_pipe(i))) {
1002 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) {
1008 off -= unroll;
1009 break;
1011 unroll -= n;
1012 if (!unroll && idx == i->start_idx) {
1013 off = 0;
1014 break;
1016 if (!idx--)
1017 idx = pipe->buffers - 1;
1018 off = pipe->bufs[idx].offset + pipe->bufs[idx].len;
1020 i->iov_offset = off;
1021 i->idx = idx;
1022 pipe_truncate(i);
1023 return;
1025 if (unlikely(iov_iter_is_discard(i)))
1026 return;
1027 if (unroll <= i->iov_offset) {
1028 i->iov_offset -= unroll;
1029 return;
1031 unroll -= i->iov_offset;
1032 if (iov_iter_is_bvec(i)) {
1033 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;
1042 unroll -= n;
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;
1054 unroll -= n;
1058 EXPORT_SYMBOL(iov_iter_revert);
1061 * Return the count of just the current iov_iter segment.
1063 size_t iov_iter_single_seg_count(const struct iov_iter *i)
1065 if (unlikely(iov_iter_is_pipe(i)))
1066 return i->count; // it is a silly place, anyway
1067 if (i->nr_segs == 1)
1068 return i->count;
1069 if (unlikely(iov_iter_is_discard(i)))
1070 return i->count;
1071 else if (iov_iter_is_bvec(i))
1072 return min(i->count, i->bvec->bv_len - i->iov_offset);
1073 else
1074 return min(i->count, i->iov->iov_len - i->iov_offset);
1076 EXPORT_SYMBOL(iov_iter_single_seg_count);
1078 void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
1079 const struct kvec *kvec, unsigned long nr_segs,
1080 size_t count)
1082 WARN_ON(direction & ~(READ | WRITE));
1083 i->type = ITER_KVEC | (direction & (READ | WRITE));
1084 i->kvec = kvec;
1085 i->nr_segs = nr_segs;
1086 i->iov_offset = 0;
1087 i->count = count;
1089 EXPORT_SYMBOL(iov_iter_kvec);
1091 void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1092 const struct bio_vec *bvec, unsigned long nr_segs,
1093 size_t count)
1095 WARN_ON(direction & ~(READ | WRITE));
1096 i->type = ITER_BVEC | (direction & (READ | WRITE));
1097 i->bvec = bvec;
1098 i->nr_segs = nr_segs;
1099 i->iov_offset = 0;
1100 i->count = count;
1102 EXPORT_SYMBOL(iov_iter_bvec);
1104 void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1105 struct pipe_inode_info *pipe,
1106 size_t count)
1108 BUG_ON(direction != READ);
1109 WARN_ON(pipe->nrbufs == pipe->buffers);
1110 i->type = ITER_PIPE | READ;
1111 i->pipe = pipe;
1112 i->idx = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1113 i->iov_offset = 0;
1114 i->count = count;
1115 i->start_idx = i->idx;
1117 EXPORT_SYMBOL(iov_iter_pipe);
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.
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.
1128 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1130 BUG_ON(direction != READ);
1131 i->type = ITER_DISCARD | READ;
1132 i->count = count;
1133 i->iov_offset = 0;
1135 EXPORT_SYMBOL(iov_iter_discard);
1137 unsigned long iov_iter_alignment(const struct iov_iter *i)
1139 unsigned long res = 0;
1140 size_t size = i->count;
1142 if (unlikely(iov_iter_is_pipe(i))) {
1143 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->idx]))
1144 return size | i->iov_offset;
1145 return size;
1147 iterate_all_kinds(i, size, v,
1148 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
1149 res |= v.bv_offset | v.bv_len,
1150 res |= (unsigned long)v.iov_base | v.iov_len
1152 return res;
1154 EXPORT_SYMBOL(iov_iter_alignment);
1156 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1158 unsigned long res = 0;
1159 size_t size = i->count;
1161 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1162 WARN_ON(1);
1163 return ~0U;
1166 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))
1174 return res;
1176 EXPORT_SYMBOL(iov_iter_gap_alignment);
1178 static inline ssize_t __pipe_get_pages(struct iov_iter *i,
1179 size_t maxsize,
1180 struct page **pages,
1181 int idx,
1182 size_t *start)
1184 struct pipe_inode_info *pipe = i->pipe;
1185 ssize_t n = push_pipe(i, maxsize, &idx, start);
1186 if (!n)
1187 return -EFAULT;
1189 maxsize = n;
1190 n += *start;
1191 while (n > 0) {
1192 get_page(*pages++ = pipe->bufs[idx].page);
1193 idx = next_idx(idx, pipe);
1194 n -= PAGE_SIZE;
1197 return maxsize;
1200 static ssize_t pipe_get_pages(struct iov_iter *i,
1201 struct page **pages, size_t maxsize, unsigned maxpages,
1202 size_t *start)
1204 unsigned npages;
1205 size_t capacity;
1206 int idx;
1208 if (!maxsize)
1209 return 0;
1211 if (!sanity(i))
1212 return -EFAULT;
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;
1219 return __pipe_get_pages(i, min(maxsize, capacity), pages, idx, start);
1222 ssize_t iov_iter_get_pages(struct iov_iter *i,
1223 struct page **pages, size_t maxsize, unsigned maxpages,
1224 size_t *start)
1226 if (maxsize > i->count)
1227 maxsize = i->count;
1229 if (unlikely(iov_iter_is_pipe(i)))
1230 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1231 if (unlikely(iov_iter_is_discard(i)))
1232 return -EFAULT;
1234 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;
1240 if (len > maxpages * PAGE_SIZE)
1241 len = maxpages * PAGE_SIZE;
1242 addr &= ~(PAGE_SIZE - 1);
1243 n = DIV_ROUND_UP(len, PAGE_SIZE);
1244 res = get_user_pages_fast(addr, n, iov_iter_rw(i) != WRITE, pages);
1245 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;
1253 }),({
1254 return -EFAULT;
1257 return 0;
1259 EXPORT_SYMBOL(iov_iter_get_pages);
1261 static struct page **get_pages_array(size_t n)
1263 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
1266 static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1267 struct page ***pages, size_t maxsize,
1268 size_t *start)
1270 struct page **p;
1271 ssize_t n;
1272 int idx;
1273 int npages;
1275 if (!maxsize)
1276 return 0;
1278 if (!sanity(i))
1279 return -EFAULT;
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;
1300 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1301 struct page ***pages, size_t maxsize,
1302 size_t *start)
1304 struct page **p;
1306 if (maxsize > i->count)
1307 maxsize = i->count;
1309 if (unlikely(iov_iter_is_pipe(i)))
1310 return pipe_get_pages_alloc(i, pages, maxsize, start);
1311 if (unlikely(iov_iter_is_discard(i)))
1312 return -EFAULT;
1314 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;
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;
1325 res = get_user_pages_fast(addr, n, iov_iter_rw(i) != WRITE, p);
1326 if (unlikely(res < 0)) {
1327 kvfree(p);
1328 return res;
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;
1340 }),({
1341 return -EFAULT;
1344 return 0;
1346 EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1348 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1349 struct iov_iter *i)
1351 char *to = addr;
1352 __wsum sum, next;
1353 size_t off = 0;
1354 sum = *csum;
1355 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1356 WARN_ON(1);
1357 return 0;
1359 iterate_and_advance(i, bytes, v, ({
1360 int err = 0;
1361 next = csum_and_copy_from_user(v.iov_base,
1362 (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;
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;
1385 *csum = sum;
1386 return bytes;
1388 EXPORT_SYMBOL(csum_and_copy_from_iter);
1390 bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1391 struct iov_iter *i)
1393 char *to = addr;
1394 __wsum sum, next;
1395 size_t off = 0;
1396 sum = *csum;
1397 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1398 WARN_ON(1);
1399 return false;
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;
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;
1429 *csum = sum;
1430 iov_iter_advance(i, bytes);
1431 return true;
1433 EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1435 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, __wsum *csum,
1436 struct iov_iter *i)
1438 const char *from = addr;
1439 __wsum sum, next;
1440 size_t off = 0;
1441 sum = *csum;
1442 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1443 WARN_ON(1); /* for now */
1444 return 0;
1446 iterate_and_advance(i, bytes, v, ({
1447 int err = 0;
1448 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
1449 v.iov_base,
1450 v.iov_len, 0, &err);
1451 if (!err) {
1452 sum = csum_block_add(sum, next, off);
1453 off += v.iov_len;
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;
1472 *csum = sum;
1473 return bytes;
1475 EXPORT_SYMBOL(csum_and_copy_to_iter);
1477 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1479 size_t size = i->count;
1480 int npages = 0;
1482 if (!size)
1483 return 0;
1484 if (unlikely(iov_iter_is_discard(i)))
1485 return 0;
1487 if (unlikely(iov_iter_is_pipe(i))) {
1488 struct pipe_inode_info *pipe = i->pipe;
1489 size_t off;
1490 int idx;
1492 if (!sanity(i))
1493 return 0;
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, ({
1501 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;
1510 }),({
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;
1518 return npages;
1520 EXPORT_SYMBOL(iov_iter_npages);
1522 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1524 *new = *old;
1525 if (unlikely(iov_iter_is_pipe(new))) {
1526 WARN_ON(1);
1527 return NULL;
1529 if (unlikely(iov_iter_is_discard(new)))
1530 return NULL;
1531 if (iov_iter_is_bvec(new))
1532 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);
1541 EXPORT_SYMBOL(dup_iter);
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.
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.
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).
1563 * Return: 0 on success or negative error code on error.
1565 int import_iovec(int type, const struct iovec __user * uvector,
1566 unsigned nr_segs, unsigned fast_segs,
1567 struct iovec **iov, struct iov_iter *i)
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;
1579 iov_iter_init(i, type, p, nr_segs, n);
1580 *iov = p == *iov ? NULL : p;
1581 return 0;
1583 EXPORT_SYMBOL(import_iovec);
1585 #ifdef CONFIG_COMPAT
1586 #include <linux/compat.h>
1588 int 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)
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;
1602 iov_iter_init(i, type, p, nr_segs, n);
1603 *iov = p == *iov ? NULL : p;
1604 return 0;
1606 #endif
1608 int import_single_range(int rw, void __user *buf, size_t len,
1609 struct iovec *iov, struct iov_iter *i)
1611 if (len > MAX_RW_COUNT)
1612 len = MAX_RW_COUNT;
1613 if (unlikely(!access_ok(!rw, buf, len)))
1614 return -EFAULT;
1616 iov->iov_base = buf;
1617 iov->iov_len = len;
1618 iov_iter_init(i, rw, iov, 1, len);
1619 return 0;
1621 EXPORT_SYMBOL(import_single_range);
1623 int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1624 int (*f)(struct kvec *vec, void *context),
1625 void *context)
1627 struct kvec w;
1628 int err = -EINVAL;
1629 if (!bytes)
1630 return 0;
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);})
1641 return err;
1643 EXPORT_SYMBOL(iov_iter_for_each_range);