1 #include <linux/export.h>
3 #include <linux/pagemap.h>
4 #include <linux/slab.h>
5 #include <linux/vmalloc.h>
6 #include <net/checksum.h>
8 #define iterate_iovec(i, n, __v, __p, skip, STEP) { \
12 __v.iov_len = min(n, __p->iov_len - skip); \
13 if (likely(__v.iov_len)) { \
14 __v.iov_base = __p->iov_base + skip; \
16 __v.iov_len -= left; \
17 skip += __v.iov_len; \
22 while (unlikely(!left && n)) { \
24 __v.iov_len = min(n, __p->iov_len); \
25 if (unlikely(!__v.iov_len)) \
27 __v.iov_base = __p->iov_base; \
29 __v.iov_len -= left; \
36 #define iterate_kvec(i, n, __v, __p, skip, STEP) { \
39 __v.iov_len = min(n, __p->iov_len - skip); \
40 if (likely(__v.iov_len)) { \
41 __v.iov_base = __p->iov_base + skip; \
43 skip += __v.iov_len; \
46 while (unlikely(n)) { \
48 __v.iov_len = min(n, __p->iov_len); \
49 if (unlikely(!__v.iov_len)) \
51 __v.iov_base = __p->iov_base; \
59 #define iterate_bvec(i, n, __v, __p, skip, STEP) { \
62 __v.bv_len = min_t(size_t, n, __p->bv_len - skip); \
63 if (likely(__v.bv_len)) { \
64 __v.bv_page = __p->bv_page; \
65 __v.bv_offset = __p->bv_offset + skip; \
70 while (unlikely(n)) { \
72 __v.bv_len = min_t(size_t, n, __p->bv_len); \
73 if (unlikely(!__v.bv_len)) \
75 __v.bv_page = __p->bv_page; \
76 __v.bv_offset = __p->bv_offset; \
84 #define iterate_all_kinds(i, n, v, I, B, K) { \
85 size_t skip = i->iov_offset; \
86 if (unlikely(i->type & ITER_BVEC)) { \
87 const struct bio_vec *bvec; \
89 iterate_bvec(i, n, v, bvec, skip, (B)) \
90 } else if (unlikely(i->type & ITER_KVEC)) { \
91 const struct kvec *kvec; \
93 iterate_kvec(i, n, v, kvec, skip, (K)) \
95 const struct iovec *iov; \
97 iterate_iovec(i, n, v, iov, skip, (I)) \
101 #define iterate_and_advance(i, n, v, I, B, K) { \
102 size_t skip = i->iov_offset; \
103 if (unlikely(i->type & ITER_BVEC)) { \
104 const struct bio_vec *bvec; \
106 iterate_bvec(i, n, v, bvec, skip, (B)) \
107 if (skip == bvec->bv_len) { \
111 i->nr_segs -= bvec - i->bvec; \
113 } else if (unlikely(i->type & ITER_KVEC)) { \
114 const struct kvec *kvec; \
116 iterate_kvec(i, n, v, kvec, skip, (K)) \
117 if (skip == kvec->iov_len) { \
121 i->nr_segs -= kvec - i->kvec; \
124 const struct iovec *iov; \
126 iterate_iovec(i, n, v, iov, skip, (I)) \
127 if (skip == iov->iov_len) { \
131 i->nr_segs -= iov - i->iov; \
135 i->iov_offset = skip; \
138 static size_t copy_page_to_iter_iovec(struct page
*page
, size_t offset
, size_t bytes
,
141 size_t skip
, copy
, left
, wanted
;
142 const struct iovec
*iov
;
146 if (unlikely(bytes
> i
->count
))
149 if (unlikely(!bytes
))
154 skip
= i
->iov_offset
;
155 buf
= iov
->iov_base
+ skip
;
156 copy
= min(bytes
, iov
->iov_len
- skip
);
158 if (!fault_in_pages_writeable(buf
, copy
)) {
159 kaddr
= kmap_atomic(page
);
160 from
= kaddr
+ offset
;
162 /* first chunk, usually the only one */
163 left
= __copy_to_user_inatomic(buf
, from
, copy
);
169 while (unlikely(!left
&& bytes
)) {
172 copy
= min(bytes
, iov
->iov_len
);
173 left
= __copy_to_user_inatomic(buf
, from
, copy
);
179 if (likely(!bytes
)) {
180 kunmap_atomic(kaddr
);
183 offset
= from
- kaddr
;
185 kunmap_atomic(kaddr
);
186 copy
= min(bytes
, iov
->iov_len
- skip
);
188 /* Too bad - revert to non-atomic kmap */
190 from
= kaddr
+ offset
;
191 left
= __copy_to_user(buf
, from
, copy
);
196 while (unlikely(!left
&& bytes
)) {
199 copy
= min(bytes
, iov
->iov_len
);
200 left
= __copy_to_user(buf
, from
, copy
);
208 if (skip
== iov
->iov_len
) {
212 i
->count
-= wanted
- bytes
;
213 i
->nr_segs
-= iov
- i
->iov
;
215 i
->iov_offset
= skip
;
216 return wanted
- bytes
;
219 static size_t copy_page_from_iter_iovec(struct page
*page
, size_t offset
, size_t bytes
,
222 size_t skip
, copy
, left
, wanted
;
223 const struct iovec
*iov
;
227 if (unlikely(bytes
> i
->count
))
230 if (unlikely(!bytes
))
235 skip
= i
->iov_offset
;
236 buf
= iov
->iov_base
+ skip
;
237 copy
= min(bytes
, iov
->iov_len
- skip
);
239 if (!fault_in_pages_readable(buf
, copy
)) {
240 kaddr
= kmap_atomic(page
);
243 /* first chunk, usually the only one */
244 left
= __copy_from_user_inatomic(to
, buf
, copy
);
250 while (unlikely(!left
&& bytes
)) {
253 copy
= min(bytes
, iov
->iov_len
);
254 left
= __copy_from_user_inatomic(to
, buf
, copy
);
260 if (likely(!bytes
)) {
261 kunmap_atomic(kaddr
);
266 kunmap_atomic(kaddr
);
267 copy
= min(bytes
, iov
->iov_len
- skip
);
269 /* Too bad - revert to non-atomic kmap */
272 left
= __copy_from_user(to
, buf
, copy
);
277 while (unlikely(!left
&& bytes
)) {
280 copy
= min(bytes
, iov
->iov_len
);
281 left
= __copy_from_user(to
, buf
, copy
);
289 if (skip
== iov
->iov_len
) {
293 i
->count
-= wanted
- bytes
;
294 i
->nr_segs
-= iov
- i
->iov
;
296 i
->iov_offset
= skip
;
297 return wanted
- bytes
;
301 * Fault in the first iovec of the given iov_iter, to a maximum length
302 * of bytes. Returns 0 on success, or non-zero if the memory could not be
303 * accessed (ie. because it is an invalid address).
305 * writev-intensive code may want this to prefault several iovecs -- that
306 * would be possible (callers must not rely on the fact that _only_ the
307 * first iovec will be faulted with the current implementation).
309 int iov_iter_fault_in_readable(struct iov_iter
*i
, size_t bytes
)
311 if (!(i
->type
& (ITER_BVEC
|ITER_KVEC
))) {
312 char __user
*buf
= i
->iov
->iov_base
+ i
->iov_offset
;
313 bytes
= min(bytes
, i
->iov
->iov_len
- i
->iov_offset
);
314 return fault_in_pages_readable(buf
, bytes
);
318 EXPORT_SYMBOL(iov_iter_fault_in_readable
);
320 void iov_iter_init(struct iov_iter
*i
, int direction
,
321 const struct iovec
*iov
, unsigned long nr_segs
,
324 /* It will get better. Eventually... */
325 if (segment_eq(get_fs(), KERNEL_DS
)) {
326 direction
|= ITER_KVEC
;
328 i
->kvec
= (struct kvec
*)iov
;
333 i
->nr_segs
= nr_segs
;
337 EXPORT_SYMBOL(iov_iter_init
);
339 static void memcpy_from_page(char *to
, struct page
*page
, size_t offset
, size_t len
)
341 char *from
= kmap_atomic(page
);
342 memcpy(to
, from
+ offset
, len
);
346 static void memcpy_to_page(struct page
*page
, size_t offset
, char *from
, size_t len
)
348 char *to
= kmap_atomic(page
);
349 memcpy(to
+ offset
, from
, len
);
353 static void memzero_page(struct page
*page
, size_t offset
, size_t len
)
355 char *addr
= kmap_atomic(page
);
356 memset(addr
+ offset
, 0, len
);
360 size_t copy_to_iter(void *addr
, size_t bytes
, struct iov_iter
*i
)
363 if (unlikely(bytes
> i
->count
))
366 if (unlikely(!bytes
))
369 iterate_and_advance(i
, bytes
, v
,
370 __copy_to_user(v
.iov_base
, (from
+= v
.iov_len
) - v
.iov_len
,
372 memcpy_to_page(v
.bv_page
, v
.bv_offset
,
373 (from
+= v
.bv_len
) - v
.bv_len
, v
.bv_len
),
374 memcpy(v
.iov_base
, (from
+= v
.iov_len
) - v
.iov_len
, v
.iov_len
)
379 EXPORT_SYMBOL(copy_to_iter
);
381 size_t copy_from_iter(void *addr
, size_t bytes
, struct iov_iter
*i
)
384 if (unlikely(bytes
> i
->count
))
387 if (unlikely(!bytes
))
390 iterate_and_advance(i
, bytes
, v
,
391 __copy_from_user((to
+= v
.iov_len
) - v
.iov_len
, v
.iov_base
,
393 memcpy_from_page((to
+= v
.bv_len
) - v
.bv_len
, v
.bv_page
,
394 v
.bv_offset
, v
.bv_len
),
395 memcpy((to
+= v
.iov_len
) - v
.iov_len
, v
.iov_base
, v
.iov_len
)
400 EXPORT_SYMBOL(copy_from_iter
);
402 size_t copy_from_iter_nocache(void *addr
, size_t bytes
, struct iov_iter
*i
)
405 if (unlikely(bytes
> i
->count
))
408 if (unlikely(!bytes
))
411 iterate_and_advance(i
, bytes
, v
,
412 __copy_from_user_nocache((to
+= v
.iov_len
) - v
.iov_len
,
413 v
.iov_base
, v
.iov_len
),
414 memcpy_from_page((to
+= v
.bv_len
) - v
.bv_len
, v
.bv_page
,
415 v
.bv_offset
, v
.bv_len
),
416 memcpy((to
+= v
.iov_len
) - v
.iov_len
, v
.iov_base
, v
.iov_len
)
421 EXPORT_SYMBOL(copy_from_iter_nocache
);
423 size_t copy_page_to_iter(struct page
*page
, size_t offset
, size_t bytes
,
426 if (i
->type
& (ITER_BVEC
|ITER_KVEC
)) {
427 void *kaddr
= kmap_atomic(page
);
428 size_t wanted
= copy_to_iter(kaddr
+ offset
, bytes
, i
);
429 kunmap_atomic(kaddr
);
432 return copy_page_to_iter_iovec(page
, offset
, bytes
, i
);
434 EXPORT_SYMBOL(copy_page_to_iter
);
436 size_t copy_page_from_iter(struct page
*page
, size_t offset
, size_t bytes
,
439 if (i
->type
& (ITER_BVEC
|ITER_KVEC
)) {
440 void *kaddr
= kmap_atomic(page
);
441 size_t wanted
= copy_from_iter(kaddr
+ offset
, bytes
, i
);
442 kunmap_atomic(kaddr
);
445 return copy_page_from_iter_iovec(page
, offset
, bytes
, i
);
447 EXPORT_SYMBOL(copy_page_from_iter
);
449 size_t iov_iter_zero(size_t bytes
, struct iov_iter
*i
)
451 if (unlikely(bytes
> i
->count
))
454 if (unlikely(!bytes
))
457 iterate_and_advance(i
, bytes
, v
,
458 __clear_user(v
.iov_base
, v
.iov_len
),
459 memzero_page(v
.bv_page
, v
.bv_offset
, v
.bv_len
),
460 memset(v
.iov_base
, 0, v
.iov_len
)
465 EXPORT_SYMBOL(iov_iter_zero
);
467 size_t iov_iter_copy_from_user_atomic(struct page
*page
,
468 struct iov_iter
*i
, unsigned long offset
, size_t bytes
)
470 char *kaddr
= kmap_atomic(page
), *p
= kaddr
+ offset
;
471 iterate_all_kinds(i
, bytes
, v
,
472 __copy_from_user_inatomic((p
+= v
.iov_len
) - v
.iov_len
,
473 v
.iov_base
, v
.iov_len
),
474 memcpy_from_page((p
+= v
.bv_len
) - v
.bv_len
, v
.bv_page
,
475 v
.bv_offset
, v
.bv_len
),
476 memcpy((p
+= v
.iov_len
) - v
.iov_len
, v
.iov_base
, v
.iov_len
)
478 kunmap_atomic(kaddr
);
481 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic
);
483 void iov_iter_advance(struct iov_iter
*i
, size_t size
)
485 iterate_and_advance(i
, size
, v
, 0, 0, 0)
487 EXPORT_SYMBOL(iov_iter_advance
);
490 * Return the count of just the current iov_iter segment.
492 size_t iov_iter_single_seg_count(const struct iov_iter
*i
)
496 else if (i
->type
& ITER_BVEC
)
497 return min(i
->count
, i
->bvec
->bv_len
- i
->iov_offset
);
499 return min(i
->count
, i
->iov
->iov_len
- i
->iov_offset
);
501 EXPORT_SYMBOL(iov_iter_single_seg_count
);
503 void iov_iter_kvec(struct iov_iter
*i
, int direction
,
504 const struct kvec
*kvec
, unsigned long nr_segs
,
507 BUG_ON(!(direction
& ITER_KVEC
));
510 i
->nr_segs
= nr_segs
;
514 EXPORT_SYMBOL(iov_iter_kvec
);
516 void iov_iter_bvec(struct iov_iter
*i
, int direction
,
517 const struct bio_vec
*bvec
, unsigned long nr_segs
,
520 BUG_ON(!(direction
& ITER_BVEC
));
523 i
->nr_segs
= nr_segs
;
527 EXPORT_SYMBOL(iov_iter_bvec
);
529 unsigned long iov_iter_alignment(const struct iov_iter
*i
)
531 unsigned long res
= 0;
532 size_t size
= i
->count
;
537 iterate_all_kinds(i
, size
, v
,
538 (res
|= (unsigned long)v
.iov_base
| v
.iov_len
, 0),
539 res
|= v
.bv_offset
| v
.bv_len
,
540 res
|= (unsigned long)v
.iov_base
| v
.iov_len
544 EXPORT_SYMBOL(iov_iter_alignment
);
546 ssize_t
iov_iter_get_pages(struct iov_iter
*i
,
547 struct page
**pages
, size_t maxsize
, unsigned maxpages
,
550 if (maxsize
> i
->count
)
556 iterate_all_kinds(i
, maxsize
, v
, ({
557 unsigned long addr
= (unsigned long)v
.iov_base
;
558 size_t len
= v
.iov_len
+ (*start
= addr
& (PAGE_SIZE
- 1));
562 if (len
> maxpages
* PAGE_SIZE
)
563 len
= maxpages
* PAGE_SIZE
;
564 addr
&= ~(PAGE_SIZE
- 1);
565 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
566 res
= get_user_pages_fast(addr
, n
, (i
->type
& WRITE
) != WRITE
, pages
);
567 if (unlikely(res
< 0))
569 return (res
== n
? len
: res
* PAGE_SIZE
) - *start
;
571 /* can't be more than PAGE_SIZE */
572 *start
= v
.bv_offset
;
573 get_page(*pages
= v
.bv_page
);
581 EXPORT_SYMBOL(iov_iter_get_pages
);
583 static struct page
**get_pages_array(size_t n
)
585 struct page
**p
= kmalloc(n
* sizeof(struct page
*), GFP_KERNEL
);
587 p
= vmalloc(n
* sizeof(struct page
*));
591 ssize_t
iov_iter_get_pages_alloc(struct iov_iter
*i
,
592 struct page
***pages
, size_t maxsize
,
597 if (maxsize
> i
->count
)
603 iterate_all_kinds(i
, maxsize
, v
, ({
604 unsigned long addr
= (unsigned long)v
.iov_base
;
605 size_t len
= v
.iov_len
+ (*start
= addr
& (PAGE_SIZE
- 1));
609 addr
&= ~(PAGE_SIZE
- 1);
610 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
611 p
= get_pages_array(n
);
614 res
= get_user_pages_fast(addr
, n
, (i
->type
& WRITE
) != WRITE
, p
);
615 if (unlikely(res
< 0)) {
620 return (res
== n
? len
: res
* PAGE_SIZE
) - *start
;
622 /* can't be more than PAGE_SIZE */
623 *start
= v
.bv_offset
;
624 *pages
= p
= get_pages_array(1);
627 get_page(*p
= v
.bv_page
);
635 EXPORT_SYMBOL(iov_iter_get_pages_alloc
);
637 size_t csum_and_copy_from_iter(void *addr
, size_t bytes
, __wsum
*csum
,
643 if (unlikely(bytes
> i
->count
))
646 if (unlikely(!bytes
))
650 iterate_and_advance(i
, bytes
, v
, ({
652 next
= csum_and_copy_from_user(v
.iov_base
,
653 (to
+= v
.iov_len
) - v
.iov_len
,
656 sum
= csum_block_add(sum
, next
, off
);
661 char *p
= kmap_atomic(v
.bv_page
);
662 next
= csum_partial_copy_nocheck(p
+ v
.bv_offset
,
663 (to
+= v
.bv_len
) - v
.bv_len
,
666 sum
= csum_block_add(sum
, next
, off
);
669 next
= csum_partial_copy_nocheck(v
.iov_base
,
670 (to
+= v
.iov_len
) - v
.iov_len
,
672 sum
= csum_block_add(sum
, next
, off
);
679 EXPORT_SYMBOL(csum_and_copy_from_iter
);
681 size_t csum_and_copy_to_iter(void *addr
, size_t bytes
, __wsum
*csum
,
687 if (unlikely(bytes
> i
->count
))
690 if (unlikely(!bytes
))
694 iterate_and_advance(i
, bytes
, v
, ({
696 next
= csum_and_copy_to_user((from
+= v
.iov_len
) - v
.iov_len
,
700 sum
= csum_block_add(sum
, next
, off
);
705 char *p
= kmap_atomic(v
.bv_page
);
706 next
= csum_partial_copy_nocheck((from
+= v
.bv_len
) - v
.bv_len
,
710 sum
= csum_block_add(sum
, next
, off
);
713 next
= csum_partial_copy_nocheck((from
+= v
.iov_len
) - v
.iov_len
,
716 sum
= csum_block_add(sum
, next
, off
);
723 EXPORT_SYMBOL(csum_and_copy_to_iter
);
725 int iov_iter_npages(const struct iov_iter
*i
, int maxpages
)
727 size_t size
= i
->count
;
733 iterate_all_kinds(i
, size
, v
, ({
734 unsigned long p
= (unsigned long)v
.iov_base
;
735 npages
+= DIV_ROUND_UP(p
+ v
.iov_len
, PAGE_SIZE
)
737 if (npages
>= maxpages
)
741 if (npages
>= maxpages
)
744 unsigned long p
= (unsigned long)v
.iov_base
;
745 npages
+= DIV_ROUND_UP(p
+ v
.iov_len
, PAGE_SIZE
)
747 if (npages
>= maxpages
)
753 EXPORT_SYMBOL(iov_iter_npages
);