1 #include <linux/export.h>
3 #include <linux/pagemap.h>
4 #include <linux/slab.h>
5 #include <linux/vmalloc.h>
7 static size_t copy_to_iter_iovec(void *from, size_t bytes, struct iov_iter *i)
9 size_t skip, copy, left, wanted;
10 const struct iovec *iov;
13 if (unlikely(bytes > i->count))
22 buf = iov->iov_base + skip;
23 copy = min(bytes, iov->iov_len - skip);
25 left = __copy_to_user(buf, from, copy);
30 while (unlikely(!left && bytes)) {
33 copy = min(bytes, iov->iov_len);
34 left = __copy_to_user(buf, from, copy);
41 if (skip == iov->iov_len) {
45 i->count -= wanted - bytes;
46 i->nr_segs -= iov - i->iov;
49 return wanted - bytes;
52 static size_t copy_from_iter_iovec(void *to, size_t bytes, struct iov_iter *i)
54 size_t skip, copy, left, wanted;
55 const struct iovec *iov;
58 if (unlikely(bytes > i->count))
67 buf = iov->iov_base + skip;
68 copy = min(bytes, iov->iov_len - skip);
70 left = __copy_from_user(to, buf, copy);
75 while (unlikely(!left && bytes)) {
78 copy = min(bytes, iov->iov_len);
79 left = __copy_from_user(to, buf, copy);
86 if (skip == iov->iov_len) {
90 i->count -= wanted - bytes;
91 i->nr_segs -= iov - i->iov;
94 return wanted - bytes;
97 static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
100 size_t skip, copy, left, wanted;
101 const struct iovec *iov;
105 if (unlikely(bytes > i->count))
108 if (unlikely(!bytes))
113 skip = i->iov_offset;
114 buf = iov->iov_base + skip;
115 copy = min(bytes, iov->iov_len - skip);
117 if (!fault_in_pages_writeable(buf, copy)) {
118 kaddr = kmap_atomic(page);
119 from = kaddr + offset;
121 /* first chunk, usually the only one */
122 left = __copy_to_user_inatomic(buf, from, copy);
128 while (unlikely(!left && bytes)) {
131 copy = min(bytes, iov->iov_len);
132 left = __copy_to_user_inatomic(buf, from, copy);
138 if (likely(!bytes)) {
139 kunmap_atomic(kaddr);
142 offset = from - kaddr;
144 kunmap_atomic(kaddr);
145 copy = min(bytes, iov->iov_len - skip);
147 /* Too bad - revert to non-atomic kmap */
149 from = kaddr + offset;
150 left = __copy_to_user(buf, from, copy);
155 while (unlikely(!left && bytes)) {
158 copy = min(bytes, iov->iov_len);
159 left = __copy_to_user(buf, from, copy);
167 if (skip == iov->iov_len) {
171 i->count -= wanted - bytes;
172 i->nr_segs -= iov - i->iov;
174 i->iov_offset = skip;
175 return wanted - bytes;
178 static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
181 size_t skip, copy, left, wanted;
182 const struct iovec *iov;
186 if (unlikely(bytes > i->count))
189 if (unlikely(!bytes))
194 skip = i->iov_offset;
195 buf = iov->iov_base + skip;
196 copy = min(bytes, iov->iov_len - skip);
198 if (!fault_in_pages_readable(buf, copy)) {
199 kaddr = kmap_atomic(page);
202 /* first chunk, usually the only one */
203 left = __copy_from_user_inatomic(to, buf, copy);
209 while (unlikely(!left && bytes)) {
212 copy = min(bytes, iov->iov_len);
213 left = __copy_from_user_inatomic(to, buf, copy);
219 if (likely(!bytes)) {
220 kunmap_atomic(kaddr);
225 kunmap_atomic(kaddr);
226 copy = min(bytes, iov->iov_len - skip);
228 /* Too bad - revert to non-atomic kmap */
231 left = __copy_from_user(to, buf, copy);
236 while (unlikely(!left && bytes)) {
239 copy = min(bytes, iov->iov_len);
240 left = __copy_from_user(to, buf, copy);
248 if (skip == iov->iov_len) {
252 i->count -= wanted - bytes;
253 i->nr_segs -= iov - i->iov;
255 i->iov_offset = skip;
256 return wanted - bytes;
259 static size_t zero_iovec(size_t bytes, struct iov_iter *i)
261 size_t skip, copy, left, wanted;
262 const struct iovec *iov;
265 if (unlikely(bytes > i->count))
268 if (unlikely(!bytes))
273 skip = i->iov_offset;
274 buf = iov->iov_base + skip;
275 copy = min(bytes, iov->iov_len - skip);
277 left = __clear_user(buf, copy);
282 while (unlikely(!left && bytes)) {
285 copy = min(bytes, iov->iov_len);
286 left = __clear_user(buf, copy);
292 if (skip == iov->iov_len) {
296 i->count -= wanted - bytes;
297 i->nr_segs -= iov - i->iov;
299 i->iov_offset = skip;
300 return wanted - bytes;
303 static size_t __iovec_copy_from_user_inatomic(char *vaddr,
304 const struct iovec *iov, size_t base, size_t bytes)
306 size_t copied = 0, left = 0;
309 char __user *buf = iov->iov_base + base;
310 int copy = min(bytes, iov->iov_len - base);
313 left = __copy_from_user_inatomic(vaddr, buf, copy);
322 return copied - left;
326 * Copy as much as we can into the page and return the number of bytes which
327 * were successfully copied. If a fault is encountered then return the number of
328 * bytes which were copied.
330 static size_t copy_from_user_atomic_iovec(struct page *page,
331 struct iov_iter *i, unsigned long offset, size_t bytes)
336 kaddr = kmap_atomic(page);
337 if (likely(i->nr_segs == 1)) {
339 char __user *buf = i->iov->iov_base + i->iov_offset;
340 left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
341 copied = bytes - left;
343 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
344 i->iov, i->iov_offset, bytes);
346 kunmap_atomic(kaddr);
351 static void advance_iovec(struct iov_iter *i, size_t bytes)
353 BUG_ON(i->count < bytes);
355 if (likely(i->nr_segs == 1)) {
356 i->iov_offset += bytes;
359 const struct iovec *iov = i->iov;
360 size_t base = i->iov_offset;
361 unsigned long nr_segs = i->nr_segs;
364 * The !iov->iov_len check ensures we skip over unlikely
365 * zero-length segments (without overruning the iovec).
367 while (bytes || unlikely(i->count && !iov->iov_len)) {
370 copy = min(bytes, iov->iov_len - base);
371 BUG_ON(!i->count || i->count < copy);
375 if (iov->iov_len == base) {
382 i->iov_offset = base;
383 i->nr_segs = nr_segs;
388 * Fault in the first iovec of the given iov_iter, to a maximum length
389 * of bytes. Returns 0 on success, or non-zero if the memory could not be
390 * accessed (ie. because it is an invalid address).
392 * writev-intensive code may want this to prefault several iovecs -- that
393 * would be possible (callers must not rely on the fact that _only_ the
394 * first iovec will be faulted with the current implementation).
396 int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
398 if (!(i->type & ITER_BVEC)) {
399 char __user *buf = i->iov->iov_base + i->iov_offset;
400 bytes = min(bytes, i->iov->iov_len - i->iov_offset);
401 return fault_in_pages_readable(buf, bytes);
405 EXPORT_SYMBOL(iov_iter_fault_in_readable);
407 static unsigned long alignment_iovec(const struct iov_iter *i)
409 const struct iovec *iov = i->iov;
411 size_t size = i->count;
417 res = (unsigned long)iov->iov_base + i->iov_offset;
418 n = iov->iov_len - i->iov_offset;
423 while (size > (++iov)->iov_len) {
424 res |= (unsigned long)iov->iov_base | iov->iov_len;
425 size -= iov->iov_len;
427 res |= (unsigned long)iov->iov_base | size;
431 void iov_iter_init(struct iov_iter *i, int direction,
432 const struct iovec *iov, unsigned long nr_segs,
435 /* It will get better. Eventually... */
436 if (segment_eq(get_fs(), KERNEL_DS))
437 direction |= ITER_KVEC;
440 i->nr_segs = nr_segs;
444 EXPORT_SYMBOL(iov_iter_init);
446 static ssize_t get_pages_iovec(struct iov_iter *i,
447 struct page **pages, size_t maxsize, unsigned maxpages,
450 size_t offset = i->iov_offset;
451 const struct iovec *iov = i->iov;
457 len = iov->iov_len - offset;
462 addr = (unsigned long)iov->iov_base + offset;
463 len += *start = addr & (PAGE_SIZE - 1);
464 if (len > maxpages * PAGE_SIZE)
465 len = maxpages * PAGE_SIZE;
466 addr &= ~(PAGE_SIZE - 1);
467 n = (len + PAGE_SIZE - 1) / PAGE_SIZE;
468 res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, pages);
469 if (unlikely(res < 0))
471 return (res == n ? len : res * PAGE_SIZE) - *start;
474 static ssize_t get_pages_alloc_iovec(struct iov_iter *i,
475 struct page ***pages, size_t maxsize,
478 size_t offset = i->iov_offset;
479 const struct iovec *iov = i->iov;
486 len = iov->iov_len - offset;
491 addr = (unsigned long)iov->iov_base + offset;
492 len += *start = addr & (PAGE_SIZE - 1);
493 addr &= ~(PAGE_SIZE - 1);
494 n = (len + PAGE_SIZE - 1) / PAGE_SIZE;
496 p = kmalloc(n * sizeof(struct page *), GFP_KERNEL);
498 p = vmalloc(n * sizeof(struct page *));
502 res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, p);
503 if (unlikely(res < 0)) {
508 return (res == n ? len : res * PAGE_SIZE) - *start;
511 static int iov_iter_npages_iovec(const struct iov_iter *i, int maxpages)
513 size_t offset = i->iov_offset;
514 size_t size = i->count;
515 const struct iovec *iov = i->iov;
519 for (n = 0; size && n < i->nr_segs; n++, iov++) {
520 unsigned long addr = (unsigned long)iov->iov_base + offset;
521 size_t len = iov->iov_len - offset;
523 if (unlikely(!len)) /* empty segment */
527 npages += (addr + len + PAGE_SIZE - 1) / PAGE_SIZE
529 if (npages >= maxpages) /* don't bother going further */
534 return min(npages, maxpages);
537 static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
539 char *from = kmap_atomic(page);
540 memcpy(to, from + offset, len);
544 static void memcpy_to_page(struct page *page, size_t offset, char *from, size_t len)
546 char *to = kmap_atomic(page);
547 memcpy(to + offset, from, len);
551 static void memzero_page(struct page *page, size_t offset, size_t len)
553 char *addr = kmap_atomic(page);
554 memset(addr + offset, 0, len);
558 static size_t copy_to_iter_bvec(void *from, size_t bytes, struct iov_iter *i)
560 size_t skip, copy, wanted;
561 const struct bio_vec *bvec;
563 if (unlikely(bytes > i->count))
566 if (unlikely(!bytes))
571 skip = i->iov_offset;
572 copy = min_t(size_t, bytes, bvec->bv_len - skip);
574 memcpy_to_page(bvec->bv_page, skip + bvec->bv_offset, from, copy);
580 copy = min(bytes, (size_t)bvec->bv_len);
581 memcpy_to_page(bvec->bv_page, bvec->bv_offset, from, copy);
586 if (skip == bvec->bv_len) {
590 i->count -= wanted - bytes;
591 i->nr_segs -= bvec - i->bvec;
593 i->iov_offset = skip;
594 return wanted - bytes;
597 static size_t copy_from_iter_bvec(void *to, size_t bytes, struct iov_iter *i)
599 size_t skip, copy, wanted;
600 const struct bio_vec *bvec;
602 if (unlikely(bytes > i->count))
605 if (unlikely(!bytes))
610 skip = i->iov_offset;
612 copy = min(bytes, bvec->bv_len - skip);
614 memcpy_from_page(to, bvec->bv_page, bvec->bv_offset + skip, copy);
622 copy = min(bytes, (size_t)bvec->bv_len);
623 memcpy_from_page(to, bvec->bv_page, bvec->bv_offset, copy);
628 if (skip == bvec->bv_len) {
633 i->nr_segs -= bvec - i->bvec;
635 i->iov_offset = skip;
639 static size_t copy_page_to_iter_bvec(struct page *page, size_t offset,
640 size_t bytes, struct iov_iter *i)
642 void *kaddr = kmap_atomic(page);
643 size_t wanted = copy_to_iter_bvec(kaddr + offset, bytes, i);
644 kunmap_atomic(kaddr);
648 static size_t copy_page_from_iter_bvec(struct page *page, size_t offset,
649 size_t bytes, struct iov_iter *i)
651 void *kaddr = kmap_atomic(page);
652 size_t wanted = copy_from_iter_bvec(kaddr + offset, bytes, i);
653 kunmap_atomic(kaddr);
657 static size_t zero_bvec(size_t bytes, struct iov_iter *i)
659 size_t skip, copy, wanted;
660 const struct bio_vec *bvec;
662 if (unlikely(bytes > i->count))
665 if (unlikely(!bytes))
670 skip = i->iov_offset;
671 copy = min_t(size_t, bytes, bvec->bv_len - skip);
673 memzero_page(bvec->bv_page, skip + bvec->bv_offset, copy);
678 copy = min(bytes, (size_t)bvec->bv_len);
679 memzero_page(bvec->bv_page, bvec->bv_offset, copy);
683 if (skip == bvec->bv_len) {
687 i->count -= wanted - bytes;
688 i->nr_segs -= bvec - i->bvec;
690 i->iov_offset = skip;
691 return wanted - bytes;
694 static size_t copy_from_user_bvec(struct page *page,
695 struct iov_iter *i, unsigned long offset, size_t bytes)
699 const struct bio_vec *bvec;
700 size_t base = i->iov_offset;
702 kaddr = kmap_atomic(page);
703 for (left = bytes, bvec = i->bvec; left; bvec++, base = 0) {
704 size_t copy = min(left, bvec->bv_len - base);
707 memcpy_from_page(kaddr + offset, bvec->bv_page,
708 bvec->bv_offset + base, copy);
712 kunmap_atomic(kaddr);
716 static void advance_bvec(struct iov_iter *i, size_t bytes)
718 BUG_ON(i->count < bytes);
720 if (likely(i->nr_segs == 1)) {
721 i->iov_offset += bytes;
724 const struct bio_vec *bvec = i->bvec;
725 size_t base = i->iov_offset;
726 unsigned long nr_segs = i->nr_segs;
729 * The !iov->iov_len check ensures we skip over unlikely
730 * zero-length segments (without overruning the iovec).
732 while (bytes || unlikely(i->count && !bvec->bv_len)) {
735 copy = min(bytes, bvec->bv_len - base);
736 BUG_ON(!i->count || i->count < copy);
740 if (bvec->bv_len == base) {
747 i->iov_offset = base;
748 i->nr_segs = nr_segs;
752 static unsigned long alignment_bvec(const struct iov_iter *i)
754 const struct bio_vec *bvec = i->bvec;
756 size_t size = i->count;
762 res = bvec->bv_offset + i->iov_offset;
763 n = bvec->bv_len - i->iov_offset;
768 while (size > (++bvec)->bv_len) {
769 res |= bvec->bv_offset | bvec->bv_len;
770 size -= bvec->bv_len;
772 res |= bvec->bv_offset | size;
776 static ssize_t get_pages_bvec(struct iov_iter *i,
777 struct page **pages, size_t maxsize, unsigned maxpages,
780 const struct bio_vec *bvec = i->bvec;
781 size_t len = bvec->bv_len - i->iov_offset;
786 /* can't be more than PAGE_SIZE */
787 *start = bvec->bv_offset + i->iov_offset;
789 get_page(*pages = bvec->bv_page);
794 static ssize_t get_pages_alloc_bvec(struct iov_iter *i,
795 struct page ***pages, size_t maxsize,
798 const struct bio_vec *bvec = i->bvec;
799 size_t len = bvec->bv_len - i->iov_offset;
804 *start = bvec->bv_offset + i->iov_offset;
806 *pages = kmalloc(sizeof(struct page *), GFP_KERNEL);
810 get_page(**pages = bvec->bv_page);
815 static int iov_iter_npages_bvec(const struct iov_iter *i, int maxpages)
817 size_t offset = i->iov_offset;
818 size_t size = i->count;
819 const struct bio_vec *bvec = i->bvec;
823 for (n = 0; size && n < i->nr_segs; n++, bvec++) {
824 size_t len = bvec->bv_len - offset;
826 if (unlikely(!len)) /* empty segment */
831 if (npages >= maxpages) /* don't bother going further */
836 return min(npages, maxpages);
839 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
842 if (i->type & ITER_BVEC)
843 return copy_page_to_iter_bvec(page, offset, bytes, i);
845 return copy_page_to_iter_iovec(page, offset, bytes, i);
847 EXPORT_SYMBOL(copy_page_to_iter);
849 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
852 if (i->type & ITER_BVEC)
853 return copy_page_from_iter_bvec(page, offset, bytes, i);
855 return copy_page_from_iter_iovec(page, offset, bytes, i);
857 EXPORT_SYMBOL(copy_page_from_iter);
859 size_t copy_to_iter(void *addr, size_t bytes, struct iov_iter *i)
861 if (i->type & ITER_BVEC)
862 return copy_to_iter_bvec(addr, bytes, i);
864 return copy_to_iter_iovec(addr, bytes, i);
866 EXPORT_SYMBOL(copy_to_iter);
868 size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
870 if (i->type & ITER_BVEC)
871 return copy_from_iter_bvec(addr, bytes, i);
873 return copy_from_iter_iovec(addr, bytes, i);
875 EXPORT_SYMBOL(copy_from_iter);
877 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
879 if (i->type & ITER_BVEC) {
880 return zero_bvec(bytes, i);
882 return zero_iovec(bytes, i);
885 EXPORT_SYMBOL(iov_iter_zero);
887 size_t iov_iter_copy_from_user_atomic(struct page *page,
888 struct iov_iter *i, unsigned long offset, size_t bytes)
890 if (i->type & ITER_BVEC)
891 return copy_from_user_bvec(page, i, offset, bytes);
893 return copy_from_user_atomic_iovec(page, i, offset, bytes);
895 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
897 void iov_iter_advance(struct iov_iter *i, size_t size)
899 if (i->type & ITER_BVEC)
900 advance_bvec(i, size);
902 advance_iovec(i, size);
904 EXPORT_SYMBOL(iov_iter_advance);
907 * Return the count of just the current iov_iter segment.
909 size_t iov_iter_single_seg_count(const struct iov_iter *i)
913 else if (i->type & ITER_BVEC)
914 return min(i->count, i->bvec->bv_len - i->iov_offset);
916 return min(i->count, i->iov->iov_len - i->iov_offset);
918 EXPORT_SYMBOL(iov_iter_single_seg_count);
920 unsigned long iov_iter_alignment(const struct iov_iter *i)
922 if (i->type & ITER_BVEC)
923 return alignment_bvec(i);
925 return alignment_iovec(i);
927 EXPORT_SYMBOL(iov_iter_alignment);
929 ssize_t iov_iter_get_pages(struct iov_iter *i,
930 struct page **pages, size_t maxsize, unsigned maxpages,
933 if (i->type & ITER_BVEC)
934 return get_pages_bvec(i, pages, maxsize, maxpages, start);
936 return get_pages_iovec(i, pages, maxsize, maxpages, start);
938 EXPORT_SYMBOL(iov_iter_get_pages);
940 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
941 struct page ***pages, size_t maxsize,
944 if (i->type & ITER_BVEC)
945 return get_pages_alloc_bvec(i, pages, maxsize, start);
947 return get_pages_alloc_iovec(i, pages, maxsize, start);
949 EXPORT_SYMBOL(iov_iter_get_pages_alloc);
951 int iov_iter_npages(const struct iov_iter *i, int maxpages)
953 if (i->type & ITER_BVEC)
954 return iov_iter_npages_bvec(i, maxpages);
956 return iov_iter_npages_iovec(i, maxpages);
958 EXPORT_SYMBOL(iov_iter_npages);