blob: acd7b97c16f242751fbed28593021e7906bd34e1 [file] [log] [blame]
Al Viro4f18cd32014-02-05 19:11:33 -05001#include <linux/export.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -06002#include <linux/bvec.h>
Al Viro4f18cd32014-02-05 19:11:33 -05003#include <linux/uio.h>
4#include <linux/pagemap.h>
Al Viro91f79c42014-03-21 04:58:33 -04005#include <linux/slab.h>
6#include <linux/vmalloc.h>
Al Viro241699c2016-09-22 16:33:12 -04007#include <linux/splice.h>
Al Viroa604ec72014-11-24 01:08:00 -05008#include <net/checksum.h>
Al Viro4f18cd32014-02-05 19:11:33 -05009
Al Viro241699c2016-09-22 16:33:12 -040010#define PIPE_PARANOIA /* for now */
11
Al Viro04a31162014-11-27 13:51:41 -050012#define iterate_iovec(i, n, __v, __p, skip, STEP) { \
13 size_t left; \
14 size_t wanted = n; \
15 __p = i->iov; \
16 __v.iov_len = min(n, __p->iov_len - skip); \
17 if (likely(__v.iov_len)) { \
18 __v.iov_base = __p->iov_base + skip; \
19 left = (STEP); \
20 __v.iov_len -= left; \
21 skip += __v.iov_len; \
22 n -= __v.iov_len; \
23 } else { \
24 left = 0; \
25 } \
26 while (unlikely(!left && n)) { \
27 __p++; \
28 __v.iov_len = min(n, __p->iov_len); \
29 if (unlikely(!__v.iov_len)) \
30 continue; \
31 __v.iov_base = __p->iov_base; \
32 left = (STEP); \
33 __v.iov_len -= left; \
34 skip = __v.iov_len; \
35 n -= __v.iov_len; \
36 } \
37 n = wanted - n; \
38}
39
Al Viroa2804552014-11-27 14:48:42 -050040#define iterate_kvec(i, n, __v, __p, skip, STEP) { \
41 size_t wanted = n; \
42 __p = i->kvec; \
43 __v.iov_len = min(n, __p->iov_len - skip); \
44 if (likely(__v.iov_len)) { \
45 __v.iov_base = __p->iov_base + skip; \
46 (void)(STEP); \
47 skip += __v.iov_len; \
48 n -= __v.iov_len; \
49 } \
50 while (unlikely(n)) { \
51 __p++; \
52 __v.iov_len = min(n, __p->iov_len); \
53 if (unlikely(!__v.iov_len)) \
54 continue; \
55 __v.iov_base = __p->iov_base; \
56 (void)(STEP); \
57 skip = __v.iov_len; \
58 n -= __v.iov_len; \
59 } \
60 n = wanted; \
61}
62
Ming Lei1bdc76a2016-05-30 21:34:32 +080063#define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
64 struct bvec_iter __start; \
65 __start.bi_size = n; \
66 __start.bi_bvec_done = skip; \
67 __start.bi_idx = 0; \
68 for_each_bvec(__v, i->bvec, __bi, __start) { \
69 if (!__v.bv_len) \
Al Viro04a31162014-11-27 13:51:41 -050070 continue; \
Al Viro04a31162014-11-27 13:51:41 -050071 (void)(STEP); \
Al Viro04a31162014-11-27 13:51:41 -050072 } \
Al Viro04a31162014-11-27 13:51:41 -050073}
74
Al Viroa2804552014-11-27 14:48:42 -050075#define iterate_all_kinds(i, n, v, I, B, K) { \
Al Viro33844e62016-12-21 21:55:02 -050076 if (likely(n)) { \
77 size_t skip = i->iov_offset; \
78 if (unlikely(i->type & ITER_BVEC)) { \
79 struct bio_vec v; \
80 struct bvec_iter __bi; \
81 iterate_bvec(i, n, v, __bi, skip, (B)) \
82 } else if (unlikely(i->type & ITER_KVEC)) { \
83 const struct kvec *kvec; \
84 struct kvec v; \
85 iterate_kvec(i, n, v, kvec, skip, (K)) \
86 } else { \
87 const struct iovec *iov; \
88 struct iovec v; \
89 iterate_iovec(i, n, v, iov, skip, (I)) \
90 } \
Al Viro04a31162014-11-27 13:51:41 -050091 } \
92}
93
Al Viroa2804552014-11-27 14:48:42 -050094#define iterate_and_advance(i, n, v, I, B, K) { \
Al Virodd254f52016-05-09 11:54:48 -040095 if (unlikely(i->count < n)) \
96 n = i->count; \
Al Viro19f18452016-05-25 17:36:19 -040097 if (i->count) { \
Al Virodd254f52016-05-09 11:54:48 -040098 size_t skip = i->iov_offset; \
99 if (unlikely(i->type & ITER_BVEC)) { \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800100 const struct bio_vec *bvec = i->bvec; \
Al Virodd254f52016-05-09 11:54:48 -0400101 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800102 struct bvec_iter __bi; \
103 iterate_bvec(i, n, v, __bi, skip, (B)) \
104 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
105 i->nr_segs -= i->bvec - bvec; \
106 skip = __bi.bi_bvec_done; \
Al Virodd254f52016-05-09 11:54:48 -0400107 } else if (unlikely(i->type & ITER_KVEC)) { \
108 const struct kvec *kvec; \
109 struct kvec v; \
110 iterate_kvec(i, n, v, kvec, skip, (K)) \
111 if (skip == kvec->iov_len) { \
112 kvec++; \
113 skip = 0; \
114 } \
115 i->nr_segs -= kvec - i->kvec; \
116 i->kvec = kvec; \
117 } else { \
118 const struct iovec *iov; \
119 struct iovec v; \
120 iterate_iovec(i, n, v, iov, skip, (I)) \
121 if (skip == iov->iov_len) { \
122 iov++; \
123 skip = 0; \
124 } \
125 i->nr_segs -= iov - i->iov; \
126 i->iov = iov; \
Al Viro7ce2a912014-11-27 13:59:45 -0500127 } \
Al Virodd254f52016-05-09 11:54:48 -0400128 i->count -= n; \
129 i->iov_offset = skip; \
Al Viro7ce2a912014-11-27 13:59:45 -0500130 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500131}
132
Al Viro09fc68d2017-06-29 22:25:14 -0400133static int copyout(void __user *to, const void *from, size_t n)
134{
135 if (access_ok(VERIFY_WRITE, to, n)) {
136 kasan_check_read(from, n);
137 n = raw_copy_to_user(to, from, n);
138 }
139 return n;
140}
141
142static int copyin(void *to, const void __user *from, size_t n)
143{
144 if (access_ok(VERIFY_READ, from, n)) {
145 kasan_check_write(to, n);
146 n = raw_copy_from_user(to, from, n);
147 }
148 return n;
149}
150
Al Viro62a80672014-04-04 23:12:29 -0400151static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500152 struct iov_iter *i)
153{
154 size_t skip, copy, left, wanted;
155 const struct iovec *iov;
156 char __user *buf;
157 void *kaddr, *from;
158
159 if (unlikely(bytes > i->count))
160 bytes = i->count;
161
162 if (unlikely(!bytes))
163 return 0;
164
Al Viro09fc68d2017-06-29 22:25:14 -0400165 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500166 wanted = bytes;
167 iov = i->iov;
168 skip = i->iov_offset;
169 buf = iov->iov_base + skip;
170 copy = min(bytes, iov->iov_len - skip);
171
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700172 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500173 kaddr = kmap_atomic(page);
174 from = kaddr + offset;
175
176 /* first chunk, usually the only one */
Al Viro09fc68d2017-06-29 22:25:14 -0400177 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500178 copy -= left;
179 skip += copy;
180 from += copy;
181 bytes -= copy;
182
183 while (unlikely(!left && bytes)) {
184 iov++;
185 buf = iov->iov_base;
186 copy = min(bytes, iov->iov_len);
Al Viro09fc68d2017-06-29 22:25:14 -0400187 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500188 copy -= left;
189 skip = copy;
190 from += copy;
191 bytes -= copy;
192 }
193 if (likely(!bytes)) {
194 kunmap_atomic(kaddr);
195 goto done;
196 }
197 offset = from - kaddr;
198 buf += copy;
199 kunmap_atomic(kaddr);
200 copy = min(bytes, iov->iov_len - skip);
201 }
202 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700203
Al Viro4f18cd32014-02-05 19:11:33 -0500204 kaddr = kmap(page);
205 from = kaddr + offset;
Al Viro09fc68d2017-06-29 22:25:14 -0400206 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500207 copy -= left;
208 skip += copy;
209 from += copy;
210 bytes -= copy;
211 while (unlikely(!left && bytes)) {
212 iov++;
213 buf = iov->iov_base;
214 copy = min(bytes, iov->iov_len);
Al Viro09fc68d2017-06-29 22:25:14 -0400215 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500216 copy -= left;
217 skip = copy;
218 from += copy;
219 bytes -= copy;
220 }
221 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700222
Al Viro4f18cd32014-02-05 19:11:33 -0500223done:
Al Viro81055e52014-04-04 19:23:46 -0400224 if (skip == iov->iov_len) {
225 iov++;
226 skip = 0;
227 }
Al Viro4f18cd32014-02-05 19:11:33 -0500228 i->count -= wanted - bytes;
229 i->nr_segs -= iov - i->iov;
230 i->iov = iov;
231 i->iov_offset = skip;
232 return wanted - bytes;
233}
Al Viro4f18cd32014-02-05 19:11:33 -0500234
Al Viro62a80672014-04-04 23:12:29 -0400235static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400236 struct iov_iter *i)
237{
238 size_t skip, copy, left, wanted;
239 const struct iovec *iov;
240 char __user *buf;
241 void *kaddr, *to;
242
243 if (unlikely(bytes > i->count))
244 bytes = i->count;
245
246 if (unlikely(!bytes))
247 return 0;
248
Al Viro09fc68d2017-06-29 22:25:14 -0400249 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400250 wanted = bytes;
251 iov = i->iov;
252 skip = i->iov_offset;
253 buf = iov->iov_base + skip;
254 copy = min(bytes, iov->iov_len - skip);
255
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700256 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400257 kaddr = kmap_atomic(page);
258 to = kaddr + offset;
259
260 /* first chunk, usually the only one */
Al Viro09fc68d2017-06-29 22:25:14 -0400261 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400262 copy -= left;
263 skip += copy;
264 to += copy;
265 bytes -= copy;
266
267 while (unlikely(!left && bytes)) {
268 iov++;
269 buf = iov->iov_base;
270 copy = min(bytes, iov->iov_len);
Al Viro09fc68d2017-06-29 22:25:14 -0400271 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400272 copy -= left;
273 skip = copy;
274 to += copy;
275 bytes -= copy;
276 }
277 if (likely(!bytes)) {
278 kunmap_atomic(kaddr);
279 goto done;
280 }
281 offset = to - kaddr;
282 buf += copy;
283 kunmap_atomic(kaddr);
284 copy = min(bytes, iov->iov_len - skip);
285 }
286 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700287
Al Virof0d1bec2014-04-03 15:05:18 -0400288 kaddr = kmap(page);
289 to = kaddr + offset;
Al Viro09fc68d2017-06-29 22:25:14 -0400290 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400291 copy -= left;
292 skip += copy;
293 to += copy;
294 bytes -= copy;
295 while (unlikely(!left && bytes)) {
296 iov++;
297 buf = iov->iov_base;
298 copy = min(bytes, iov->iov_len);
Al Viro09fc68d2017-06-29 22:25:14 -0400299 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400300 copy -= left;
301 skip = copy;
302 to += copy;
303 bytes -= copy;
304 }
305 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700306
Al Virof0d1bec2014-04-03 15:05:18 -0400307done:
Al Viro81055e52014-04-04 19:23:46 -0400308 if (skip == iov->iov_len) {
309 iov++;
310 skip = 0;
311 }
Al Virof0d1bec2014-04-03 15:05:18 -0400312 i->count -= wanted - bytes;
313 i->nr_segs -= iov - i->iov;
314 i->iov = iov;
315 i->iov_offset = skip;
316 return wanted - bytes;
317}
Al Virof0d1bec2014-04-03 15:05:18 -0400318
Al Viro241699c2016-09-22 16:33:12 -0400319#ifdef PIPE_PARANOIA
320static bool sanity(const struct iov_iter *i)
321{
322 struct pipe_inode_info *pipe = i->pipe;
323 int idx = i->idx;
324 int next = pipe->curbuf + pipe->nrbufs;
325 if (i->iov_offset) {
326 struct pipe_buffer *p;
327 if (unlikely(!pipe->nrbufs))
328 goto Bad; // pipe must be non-empty
329 if (unlikely(idx != ((next - 1) & (pipe->buffers - 1))))
330 goto Bad; // must be at the last buffer...
331
332 p = &pipe->bufs[idx];
333 if (unlikely(p->offset + p->len != i->iov_offset))
334 goto Bad; // ... at the end of segment
335 } else {
336 if (idx != (next & (pipe->buffers - 1)))
337 goto Bad; // must be right after the last buffer
338 }
339 return true;
340Bad:
341 printk(KERN_ERR "idx = %d, offset = %zd\n", i->idx, i->iov_offset);
342 printk(KERN_ERR "curbuf = %d, nrbufs = %d, buffers = %d\n",
343 pipe->curbuf, pipe->nrbufs, pipe->buffers);
344 for (idx = 0; idx < pipe->buffers; idx++)
345 printk(KERN_ERR "[%p %p %d %d]\n",
346 pipe->bufs[idx].ops,
347 pipe->bufs[idx].page,
348 pipe->bufs[idx].offset,
349 pipe->bufs[idx].len);
350 WARN_ON(1);
351 return false;
352}
353#else
354#define sanity(i) true
355#endif
356
357static inline int next_idx(int idx, struct pipe_inode_info *pipe)
358{
359 return (idx + 1) & (pipe->buffers - 1);
360}
361
362static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
363 struct iov_iter *i)
364{
365 struct pipe_inode_info *pipe = i->pipe;
366 struct pipe_buffer *buf;
367 size_t off;
368 int idx;
369
370 if (unlikely(bytes > i->count))
371 bytes = i->count;
372
373 if (unlikely(!bytes))
374 return 0;
375
376 if (!sanity(i))
377 return 0;
378
379 off = i->iov_offset;
380 idx = i->idx;
381 buf = &pipe->bufs[idx];
382 if (off) {
383 if (offset == off && buf->page == page) {
384 /* merge with the last one */
385 buf->len += bytes;
386 i->iov_offset += bytes;
387 goto out;
388 }
389 idx = next_idx(idx, pipe);
390 buf = &pipe->bufs[idx];
391 }
392 if (idx == pipe->curbuf && pipe->nrbufs)
393 return 0;
394 pipe->nrbufs++;
395 buf->ops = &page_cache_pipe_buf_ops;
396 get_page(buf->page = page);
397 buf->offset = offset;
398 buf->len = bytes;
399 i->iov_offset = offset + bytes;
400 i->idx = idx;
401out:
402 i->count -= bytes;
403 return bytes;
404}
405
Al Viro4f18cd32014-02-05 19:11:33 -0500406/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400407 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
408 * bytes. For each iovec, fault in each page that constitutes the iovec.
409 *
410 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
411 * because it is an invalid address).
412 */
Al Virod4690f12016-09-16 00:11:45 +0100413int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400414{
415 size_t skip = i->iov_offset;
416 const struct iovec *iov;
417 int err;
418 struct iovec v;
419
420 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
421 iterate_iovec(i, bytes, v, iov, skip, ({
Al Viro4bce9f62016-09-17 18:02:44 -0400422 err = fault_in_pages_readable(v.iov_base, v.iov_len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400423 if (unlikely(err))
424 return err;
425 0;}))
426 }
427 return 0;
428}
Al Virod4690f12016-09-16 00:11:45 +0100429EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400430
Al Viro71d8e532014-03-05 19:28:09 -0500431void iov_iter_init(struct iov_iter *i, int direction,
432 const struct iovec *iov, unsigned long nr_segs,
433 size_t count)
434{
435 /* It will get better. Eventually... */
Al Virodb68ce12017-03-20 21:08:07 -0400436 if (uaccess_kernel()) {
Al Viro62a80672014-04-04 23:12:29 -0400437 direction |= ITER_KVEC;
Al Viroa2804552014-11-27 14:48:42 -0500438 i->type = direction;
439 i->kvec = (struct kvec *)iov;
440 } else {
441 i->type = direction;
442 i->iov = iov;
443 }
Al Viro71d8e532014-03-05 19:28:09 -0500444 i->nr_segs = nr_segs;
445 i->iov_offset = 0;
446 i->count = count;
447}
448EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400449
Al Viro62a80672014-04-04 23:12:29 -0400450static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
451{
452 char *from = kmap_atomic(page);
453 memcpy(to, from + offset, len);
454 kunmap_atomic(from);
455}
456
Al Viro36f7a8a2015-12-06 16:49:22 -0500457static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
Al Viro62a80672014-04-04 23:12:29 -0400458{
459 char *to = kmap_atomic(page);
460 memcpy(to + offset, from, len);
461 kunmap_atomic(to);
462}
463
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400464static void memzero_page(struct page *page, size_t offset, size_t len)
465{
466 char *addr = kmap_atomic(page);
467 memset(addr + offset, 0, len);
468 kunmap_atomic(addr);
469}
470
Al Viro241699c2016-09-22 16:33:12 -0400471static inline bool allocated(struct pipe_buffer *buf)
472{
473 return buf->ops == &default_pipe_buf_ops;
474}
475
476static inline void data_start(const struct iov_iter *i, int *idxp, size_t *offp)
477{
478 size_t off = i->iov_offset;
479 int idx = i->idx;
480 if (off && (!allocated(&i->pipe->bufs[idx]) || off == PAGE_SIZE)) {
481 idx = next_idx(idx, i->pipe);
482 off = 0;
483 }
484 *idxp = idx;
485 *offp = off;
486}
487
488static size_t push_pipe(struct iov_iter *i, size_t size,
489 int *idxp, size_t *offp)
490{
491 struct pipe_inode_info *pipe = i->pipe;
492 size_t off;
493 int idx;
494 ssize_t left;
495
496 if (unlikely(size > i->count))
497 size = i->count;
498 if (unlikely(!size))
499 return 0;
500
501 left = size;
502 data_start(i, &idx, &off);
503 *idxp = idx;
504 *offp = off;
505 if (off) {
506 left -= PAGE_SIZE - off;
507 if (left <= 0) {
508 pipe->bufs[idx].len += size;
509 return size;
510 }
511 pipe->bufs[idx].len = PAGE_SIZE;
512 idx = next_idx(idx, pipe);
513 }
514 while (idx != pipe->curbuf || !pipe->nrbufs) {
515 struct page *page = alloc_page(GFP_USER);
516 if (!page)
517 break;
518 pipe->nrbufs++;
519 pipe->bufs[idx].ops = &default_pipe_buf_ops;
520 pipe->bufs[idx].page = page;
521 pipe->bufs[idx].offset = 0;
522 if (left <= PAGE_SIZE) {
523 pipe->bufs[idx].len = left;
524 return size;
525 }
526 pipe->bufs[idx].len = PAGE_SIZE;
527 left -= PAGE_SIZE;
528 idx = next_idx(idx, pipe);
529 }
530 return size - left;
531}
532
533static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
534 struct iov_iter *i)
535{
536 struct pipe_inode_info *pipe = i->pipe;
537 size_t n, off;
538 int idx;
539
540 if (!sanity(i))
541 return 0;
542
543 bytes = n = push_pipe(i, bytes, &idx, &off);
544 if (unlikely(!n))
545 return 0;
546 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
547 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
548 memcpy_to_page(pipe->bufs[idx].page, off, addr, chunk);
549 i->idx = idx;
550 i->iov_offset = off + chunk;
551 n -= chunk;
552 addr += chunk;
553 }
554 i->count -= bytes;
555 return bytes;
556}
557
Al Viroaa28de22017-06-29 21:45:10 -0400558size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400559{
Al Viro36f7a8a2015-12-06 16:49:22 -0500560 const char *from = addr;
Al Viro241699c2016-09-22 16:33:12 -0400561 if (unlikely(i->type & ITER_PIPE))
562 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68d2017-06-29 22:25:14 -0400563 if (iter_is_iovec(i))
564 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500565 iterate_and_advance(i, bytes, v,
Al Viro09fc68d2017-06-29 22:25:14 -0400566 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500567 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500568 (from += v.bv_len) - v.bv_len, v.bv_len),
569 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500570 )
Al Viro62a80672014-04-04 23:12:29 -0400571
Al Viro3d4d3e42014-11-27 14:28:06 -0500572 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400573}
Al Viroaa28de22017-06-29 21:45:10 -0400574EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400575
Dan Williams87803562018-05-03 17:06:31 -0700576#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
577static int copyout_mcsafe(void __user *to, const void *from, size_t n)
578{
579 if (access_ok(VERIFY_WRITE, to, n)) {
580 kasan_check_read(from, n);
581 n = copy_to_user_mcsafe((__force void *) to, from, n);
582 }
583 return n;
584}
585
586static unsigned long memcpy_mcsafe_to_page(struct page *page, size_t offset,
587 const char *from, size_t len)
588{
589 unsigned long ret;
590 char *to;
591
592 to = kmap_atomic(page);
593 ret = memcpy_mcsafe(to + offset, from, len);
594 kunmap_atomic(to);
595
596 return ret;
597}
598
Dan Williamsca146f62018-07-08 13:46:12 -0700599static size_t copy_pipe_to_iter_mcsafe(const void *addr, size_t bytes,
600 struct iov_iter *i)
601{
602 struct pipe_inode_info *pipe = i->pipe;
603 size_t n, off, xfer = 0;
604 int idx;
605
606 if (!sanity(i))
607 return 0;
608
609 bytes = n = push_pipe(i, bytes, &idx, &off);
610 if (unlikely(!n))
611 return 0;
612 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
613 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
614 unsigned long rem;
615
616 rem = memcpy_mcsafe_to_page(pipe->bufs[idx].page, off, addr,
617 chunk);
618 i->idx = idx;
619 i->iov_offset = off + chunk - rem;
620 xfer += chunk - rem;
621 if (rem)
622 break;
623 n -= chunk;
624 addr += chunk;
625 }
626 i->count -= xfer;
627 return xfer;
628}
629
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700630/**
631 * _copy_to_iter_mcsafe - copy to user with source-read error exception handling
632 * @addr: source kernel address
633 * @bytes: total transfer length
634 * @iter: destination iterator
635 *
636 * The pmem driver arranges for filesystem-dax to use this facility via
637 * dax_copy_to_iter() for protecting read/write to persistent memory.
638 * Unless / until an architecture can guarantee identical performance
639 * between _copy_to_iter_mcsafe() and _copy_to_iter() it would be a
640 * performance regression to switch more users to the mcsafe version.
641 *
642 * Otherwise, the main differences between this and typical _copy_to_iter().
643 *
644 * * Typical tail/residue handling after a fault retries the copy
645 * byte-by-byte until the fault happens again. Re-triggering machine
646 * checks is potentially fatal so the implementation uses source
647 * alignment and poison alignment assumptions to avoid re-triggering
648 * hardware exceptions.
649 *
650 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
651 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
652 * a short copy.
653 *
654 * See MCSAFE_TEST for self-test.
655 */
Dan Williams87803562018-05-03 17:06:31 -0700656size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i)
657{
658 const char *from = addr;
659 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
660
Dan Williamsca146f62018-07-08 13:46:12 -0700661 if (unlikely(i->type & ITER_PIPE))
662 return copy_pipe_to_iter_mcsafe(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700663 if (iter_is_iovec(i))
664 might_fault();
665 iterate_and_advance(i, bytes, v,
666 copyout_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
667 ({
668 rem = memcpy_mcsafe_to_page(v.bv_page, v.bv_offset,
669 (from += v.bv_len) - v.bv_len, v.bv_len);
670 if (rem) {
671 curr_addr = (unsigned long) from;
672 bytes = curr_addr - s_addr - rem;
673 return bytes;
674 }
675 }),
676 ({
677 rem = memcpy_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len,
678 v.iov_len);
679 if (rem) {
680 curr_addr = (unsigned long) from;
681 bytes = curr_addr - s_addr - rem;
682 return bytes;
683 }
684 })
685 )
686
687 return bytes;
688}
689EXPORT_SYMBOL_GPL(_copy_to_iter_mcsafe);
690#endif /* CONFIG_ARCH_HAS_UACCESS_MCSAFE */
691
Al Viroaa28de22017-06-29 21:45:10 -0400692size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400693{
Al Viro0dbca9a2014-11-27 14:26:43 -0500694 char *to = addr;
Al Viro241699c2016-09-22 16:33:12 -0400695 if (unlikely(i->type & ITER_PIPE)) {
696 WARN_ON(1);
697 return 0;
698 }
Al Viro09fc68d2017-06-29 22:25:14 -0400699 if (iter_is_iovec(i))
700 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500701 iterate_and_advance(i, bytes, v,
Al Viro09fc68d2017-06-29 22:25:14 -0400702 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500703 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500704 v.bv_offset, v.bv_len),
705 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500706 )
707
708 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400709}
Al Viroaa28de22017-06-29 21:45:10 -0400710EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400711
Al Viroaa28de22017-06-29 21:45:10 -0400712bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400713{
714 char *to = addr;
715 if (unlikely(i->type & ITER_PIPE)) {
716 WARN_ON(1);
717 return false;
718 }
Al Viro33844e62016-12-21 21:55:02 -0500719 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400720 return false;
721
Al Viro09fc68d2017-06-29 22:25:14 -0400722 if (iter_is_iovec(i))
723 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400724 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68d2017-06-29 22:25:14 -0400725 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400726 v.iov_base, v.iov_len))
727 return false;
728 0;}),
729 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
730 v.bv_offset, v.bv_len),
731 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
732 )
733
734 iov_iter_advance(i, bytes);
735 return true;
736}
Al Viroaa28de22017-06-29 21:45:10 -0400737EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400738
Al Viroaa28de22017-06-29 21:45:10 -0400739size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500740{
741 char *to = addr;
Al Viro241699c2016-09-22 16:33:12 -0400742 if (unlikely(i->type & ITER_PIPE)) {
743 WARN_ON(1);
744 return 0;
745 }
Al Viroaa583092014-11-27 20:27:08 -0500746 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400747 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500748 v.iov_base, v.iov_len),
749 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
750 v.bv_offset, v.bv_len),
751 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
752 )
753
754 return bytes;
755}
Al Viroaa28de22017-06-29 21:45:10 -0400756EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500757
Dan Williams0aed55a2017-05-29 12:22:50 -0700758#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700759/**
760 * _copy_from_iter_flushcache - write destination through cpu cache
761 * @addr: destination kernel address
762 * @bytes: total transfer length
763 * @iter: source iterator
764 *
765 * The pmem driver arranges for filesystem-dax to use this facility via
766 * dax_copy_from_iter() for ensuring that writes to persistent memory
767 * are flushed through the CPU cache. It is differentiated from
768 * _copy_from_iter_nocache() in that guarantees all data is flushed for
769 * all iterator types. The _copy_from_iter_nocache() only attempts to
770 * bypass the cache for the ITER_IOVEC case, and on some archs may use
771 * instructions that strand dirty-data in the cache.
772 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700773size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700774{
775 char *to = addr;
776 if (unlikely(i->type & ITER_PIPE)) {
777 WARN_ON(1);
778 return 0;
779 }
780 iterate_and_advance(i, bytes, v,
781 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
782 v.iov_base, v.iov_len),
783 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
784 v.bv_offset, v.bv_len),
785 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
786 v.iov_len)
787 )
788
789 return bytes;
790}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700791EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700792#endif
793
Al Viroaa28de22017-06-29 21:45:10 -0400794bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400795{
796 char *to = addr;
797 if (unlikely(i->type & ITER_PIPE)) {
798 WARN_ON(1);
799 return false;
800 }
Al Viro33844e62016-12-21 21:55:02 -0500801 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400802 return false;
803 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400804 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400805 v.iov_base, v.iov_len))
806 return false;
807 0;}),
808 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
809 v.bv_offset, v.bv_len),
810 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
811 )
812
813 iov_iter_advance(i, bytes);
814 return true;
815}
Al Viroaa28de22017-06-29 21:45:10 -0400816EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400817
Al Viro72e809e2017-06-29 21:52:57 -0400818static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
819{
Eric Dumazet627bb2d2019-02-26 10:42:39 -0800820 struct page *head;
821 size_t v = n + offset;
822
823 /*
824 * The general case needs to access the page order in order
825 * to compute the page size.
826 * However, we mostly deal with order-0 pages and thus can
827 * avoid a possible cache line miss for requests that fit all
828 * page orders.
829 */
830 if (n <= v && v <= PAGE_SIZE)
831 return true;
832
833 head = compound_head(page);
834 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700835
836 if (likely(n <= v && v <= (PAGE_SIZE << compound_order(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400837 return true;
838 WARN_ON(1);
839 return false;
840}
Al Virod2715242014-11-27 14:22:37 -0500841
842size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
843 struct iov_iter *i)
844{
Al Viro72e809e2017-06-29 21:52:57 -0400845 if (unlikely(!page_copy_sane(page, offset, bytes)))
846 return 0;
Al Virod2715242014-11-27 14:22:37 -0500847 if (i->type & (ITER_BVEC|ITER_KVEC)) {
848 void *kaddr = kmap_atomic(page);
849 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
850 kunmap_atomic(kaddr);
851 return wanted;
Al Viro241699c2016-09-22 16:33:12 -0400852 } else if (likely(!(i->type & ITER_PIPE)))
Al Virod2715242014-11-27 14:22:37 -0500853 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400854 else
855 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500856}
857EXPORT_SYMBOL(copy_page_to_iter);
858
859size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
860 struct iov_iter *i)
861{
Al Viro72e809e2017-06-29 21:52:57 -0400862 if (unlikely(!page_copy_sane(page, offset, bytes)))
863 return 0;
Al Viro241699c2016-09-22 16:33:12 -0400864 if (unlikely(i->type & ITER_PIPE)) {
865 WARN_ON(1);
866 return 0;
867 }
Al Virod2715242014-11-27 14:22:37 -0500868 if (i->type & (ITER_BVEC|ITER_KVEC)) {
869 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400870 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500871 kunmap_atomic(kaddr);
872 return wanted;
873 } else
874 return copy_page_from_iter_iovec(page, offset, bytes, i);
875}
876EXPORT_SYMBOL(copy_page_from_iter);
877
Al Viro241699c2016-09-22 16:33:12 -0400878static size_t pipe_zero(size_t bytes, struct iov_iter *i)
879{
880 struct pipe_inode_info *pipe = i->pipe;
881 size_t n, off;
882 int idx;
883
884 if (!sanity(i))
885 return 0;
886
887 bytes = n = push_pipe(i, bytes, &idx, &off);
888 if (unlikely(!n))
889 return 0;
890
891 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
892 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
893 memzero_page(pipe->bufs[idx].page, off, chunk);
894 i->idx = idx;
895 i->iov_offset = off + chunk;
896 n -= chunk;
897 }
898 i->count -= bytes;
899 return bytes;
900}
901
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400902size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
903{
Al Viro241699c2016-09-22 16:33:12 -0400904 if (unlikely(i->type & ITER_PIPE))
905 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500906 iterate_and_advance(i, bytes, v,
Al Viro09fc68d2017-06-29 22:25:14 -0400907 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500908 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
909 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500910 )
911
912 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400913}
914EXPORT_SYMBOL(iov_iter_zero);
915
Al Viro62a80672014-04-04 23:12:29 -0400916size_t iov_iter_copy_from_user_atomic(struct page *page,
917 struct iov_iter *i, unsigned long offset, size_t bytes)
918{
Al Viro04a31162014-11-27 13:51:41 -0500919 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400920 if (unlikely(!page_copy_sane(page, offset, bytes))) {
921 kunmap_atomic(kaddr);
922 return 0;
923 }
Al Viro241699c2016-09-22 16:33:12 -0400924 if (unlikely(i->type & ITER_PIPE)) {
925 kunmap_atomic(kaddr);
926 WARN_ON(1);
927 return 0;
928 }
Al Viro04a31162014-11-27 13:51:41 -0500929 iterate_all_kinds(i, bytes, v,
Al Viro09fc68d2017-06-29 22:25:14 -0400930 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -0500931 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500932 v.bv_offset, v.bv_len),
933 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -0500934 )
935 kunmap_atomic(kaddr);
936 return bytes;
Al Viro62a80672014-04-04 23:12:29 -0400937}
938EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
939
Al Virob9dc6f62017-01-14 19:33:08 -0500940static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -0400941{
942 struct pipe_inode_info *pipe = i->pipe;
Al Viro241699c2016-09-22 16:33:12 -0400943 if (pipe->nrbufs) {
Al Virob9dc6f62017-01-14 19:33:08 -0500944 size_t off = i->iov_offset;
945 int idx = i->idx;
946 int nrbufs = (idx - pipe->curbuf) & (pipe->buffers - 1);
947 if (off) {
948 pipe->bufs[idx].len = off - pipe->bufs[idx].offset;
949 idx = next_idx(idx, pipe);
950 nrbufs++;
951 }
952 while (pipe->nrbufs > nrbufs) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200953 pipe_buf_release(pipe, &pipe->bufs[idx]);
Al Viro241699c2016-09-22 16:33:12 -0400954 idx = next_idx(idx, pipe);
955 pipe->nrbufs--;
956 }
957 }
Al Virob9dc6f62017-01-14 19:33:08 -0500958}
959
960static void pipe_advance(struct iov_iter *i, size_t size)
961{
962 struct pipe_inode_info *pipe = i->pipe;
963 if (unlikely(i->count < size))
964 size = i->count;
965 if (size) {
966 struct pipe_buffer *buf;
967 size_t off = i->iov_offset, left = size;
968 int idx = i->idx;
969 if (off) /* make it relative to the beginning of buffer */
970 left += off - pipe->bufs[idx].offset;
971 while (1) {
972 buf = &pipe->bufs[idx];
973 if (left <= buf->len)
974 break;
975 left -= buf->len;
976 idx = next_idx(idx, pipe);
977 }
978 i->idx = idx;
979 i->iov_offset = buf->offset + left;
980 }
981 i->count -= size;
982 /* ... and discard everything past that point */
983 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -0400984}
985
Al Viro62a80672014-04-04 23:12:29 -0400986void iov_iter_advance(struct iov_iter *i, size_t size)
987{
Al Viro241699c2016-09-22 16:33:12 -0400988 if (unlikely(i->type & ITER_PIPE)) {
989 pipe_advance(i, size);
990 return;
991 }
Al Viroa2804552014-11-27 14:48:42 -0500992 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -0400993}
994EXPORT_SYMBOL(iov_iter_advance);
995
Al Viro27c0e372017-02-17 18:42:24 -0500996void iov_iter_revert(struct iov_iter *i, size_t unroll)
997{
998 if (!unroll)
999 return;
Al Viro5b47d592017-05-08 13:54:47 -04001000 if (WARN_ON(unroll > MAX_RW_COUNT))
1001 return;
Al Viro27c0e372017-02-17 18:42:24 -05001002 i->count += unroll;
1003 if (unlikely(i->type & ITER_PIPE)) {
1004 struct pipe_inode_info *pipe = i->pipe;
1005 int idx = i->idx;
1006 size_t off = i->iov_offset;
1007 while (1) {
1008 size_t n = off - pipe->bufs[idx].offset;
1009 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001010 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001011 break;
1012 }
1013 unroll -= n;
1014 if (!unroll && idx == i->start_idx) {
1015 off = 0;
1016 break;
1017 }
1018 if (!idx--)
1019 idx = pipe->buffers - 1;
1020 off = pipe->bufs[idx].offset + pipe->bufs[idx].len;
1021 }
1022 i->iov_offset = off;
1023 i->idx = idx;
1024 pipe_truncate(i);
1025 return;
1026 }
1027 if (unroll <= i->iov_offset) {
1028 i->iov_offset -= unroll;
1029 return;
1030 }
1031 unroll -= i->iov_offset;
1032 if (i->type & ITER_BVEC) {
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;
1041 }
1042 unroll -= n;
1043 }
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;
1053 }
1054 unroll -= n;
1055 }
1056 }
1057}
1058EXPORT_SYMBOL(iov_iter_revert);
1059
Al Viro62a80672014-04-04 23:12:29 -04001060/*
1061 * Return the count of just the current iov_iter segment.
1062 */
1063size_t iov_iter_single_seg_count(const struct iov_iter *i)
1064{
Al Viro241699c2016-09-22 16:33:12 -04001065 if (unlikely(i->type & ITER_PIPE))
1066 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001067 if (i->nr_segs == 1)
1068 return i->count;
1069 else if (i->type & ITER_BVEC)
Al Viro62a80672014-04-04 23:12:29 -04001070 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001071 else
1072 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001073}
1074EXPORT_SYMBOL(iov_iter_single_seg_count);
1075
Al Viroabb78f82014-11-24 14:46:11 -05001076void iov_iter_kvec(struct iov_iter *i, int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001077 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001078 size_t count)
1079{
1080 BUG_ON(!(direction & ITER_KVEC));
1081 i->type = direction;
Al Viro05afcb72015-01-23 01:08:07 -05001082 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001083 i->nr_segs = nr_segs;
1084 i->iov_offset = 0;
1085 i->count = count;
1086}
1087EXPORT_SYMBOL(iov_iter_kvec);
1088
Al Viro05afcb72015-01-23 01:08:07 -05001089void iov_iter_bvec(struct iov_iter *i, int direction,
1090 const struct bio_vec *bvec, unsigned long nr_segs,
1091 size_t count)
1092{
1093 BUG_ON(!(direction & ITER_BVEC));
1094 i->type = direction;
1095 i->bvec = bvec;
1096 i->nr_segs = nr_segs;
1097 i->iov_offset = 0;
1098 i->count = count;
1099}
1100EXPORT_SYMBOL(iov_iter_bvec);
1101
Al Viro241699c2016-09-22 16:33:12 -04001102void iov_iter_pipe(struct iov_iter *i, int direction,
1103 struct pipe_inode_info *pipe,
1104 size_t count)
1105{
1106 BUG_ON(direction != ITER_PIPE);
Al Virob9dc6f62017-01-14 19:33:08 -05001107 WARN_ON(pipe->nrbufs == pipe->buffers);
Al Viro241699c2016-09-22 16:33:12 -04001108 i->type = direction;
1109 i->pipe = pipe;
1110 i->idx = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1111 i->iov_offset = 0;
1112 i->count = count;
Al Viro27c0e372017-02-17 18:42:24 -05001113 i->start_idx = i->idx;
Al Viro241699c2016-09-22 16:33:12 -04001114}
1115EXPORT_SYMBOL(iov_iter_pipe);
1116
Al Viro62a80672014-04-04 23:12:29 -04001117unsigned long iov_iter_alignment(const struct iov_iter *i)
1118{
Al Viro04a31162014-11-27 13:51:41 -05001119 unsigned long res = 0;
1120 size_t size = i->count;
1121
Al Viro241699c2016-09-22 16:33:12 -04001122 if (unlikely(i->type & ITER_PIPE)) {
Al Viro33844e62016-12-21 21:55:02 -05001123 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->idx]))
Al Viro241699c2016-09-22 16:33:12 -04001124 return size | i->iov_offset;
1125 return size;
1126 }
Al Viro04a31162014-11-27 13:51:41 -05001127 iterate_all_kinds(i, size, v,
1128 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001129 res |= v.bv_offset | v.bv_len,
1130 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -05001131 )
1132 return res;
Al Viro62a80672014-04-04 23:12:29 -04001133}
1134EXPORT_SYMBOL(iov_iter_alignment);
1135
Al Viro357f4352016-04-08 19:05:19 -04001136unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1137{
Al Viro33844e62016-12-21 21:55:02 -05001138 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001139 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001140
Al Viro241699c2016-09-22 16:33:12 -04001141 if (unlikely(i->type & ITER_PIPE)) {
1142 WARN_ON(1);
1143 return ~0U;
1144 }
1145
Al Viro357f4352016-04-08 19:05:19 -04001146 iterate_all_kinds(i, size, v,
1147 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1148 (size != v.iov_len ? size : 0), 0),
1149 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1150 (size != v.bv_len ? size : 0)),
1151 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1152 (size != v.iov_len ? size : 0))
1153 );
Al Viro33844e62016-12-21 21:55:02 -05001154 return res;
Al Viro357f4352016-04-08 19:05:19 -04001155}
1156EXPORT_SYMBOL(iov_iter_gap_alignment);
1157
Ilya Dryomove76b63122018-05-02 20:16:56 +02001158static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001159 size_t maxsize,
1160 struct page **pages,
1161 int idx,
1162 size_t *start)
1163{
1164 struct pipe_inode_info *pipe = i->pipe;
Al Viro1689c732016-10-11 18:21:14 +01001165 ssize_t n = push_pipe(i, maxsize, &idx, start);
Al Viro241699c2016-09-22 16:33:12 -04001166 if (!n)
1167 return -EFAULT;
1168
1169 maxsize = n;
1170 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001171 while (n > 0) {
Al Viro241699c2016-09-22 16:33:12 -04001172 get_page(*pages++ = pipe->bufs[idx].page);
1173 idx = next_idx(idx, pipe);
1174 n -= PAGE_SIZE;
1175 }
1176
1177 return maxsize;
1178}
1179
1180static ssize_t pipe_get_pages(struct iov_iter *i,
1181 struct page **pages, size_t maxsize, unsigned maxpages,
1182 size_t *start)
1183{
1184 unsigned npages;
1185 size_t capacity;
1186 int idx;
1187
Al Viro33844e62016-12-21 21:55:02 -05001188 if (!maxsize)
1189 return 0;
1190
Al Viro241699c2016-09-22 16:33:12 -04001191 if (!sanity(i))
1192 return -EFAULT;
1193
1194 data_start(i, &idx, start);
1195 /* some of this one + all after this one */
1196 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1197 capacity = min(npages,maxpages) * PAGE_SIZE - *start;
1198
1199 return __pipe_get_pages(i, min(maxsize, capacity), pages, idx, start);
1200}
1201
Al Viro62a80672014-04-04 23:12:29 -04001202ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001203 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001204 size_t *start)
1205{
Al Viroe5393fa2014-11-27 14:12:09 -05001206 if (maxsize > i->count)
1207 maxsize = i->count;
1208
Al Viro241699c2016-09-22 16:33:12 -04001209 if (unlikely(i->type & ITER_PIPE))
1210 return pipe_get_pages(i, pages, maxsize, maxpages, start);
Al Viroe5393fa2014-11-27 14:12:09 -05001211 iterate_all_kinds(i, maxsize, v, ({
1212 unsigned long addr = (unsigned long)v.iov_base;
1213 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1214 int n;
1215 int res;
1216
1217 if (len > maxpages * PAGE_SIZE)
1218 len = maxpages * PAGE_SIZE;
1219 addr &= ~(PAGE_SIZE - 1);
1220 n = DIV_ROUND_UP(len, PAGE_SIZE);
1221 res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, pages);
1222 if (unlikely(res < 0))
1223 return res;
1224 return (res == n ? len : res * PAGE_SIZE) - *start;
1225 0;}),({
1226 /* can't be more than PAGE_SIZE */
1227 *start = v.bv_offset;
1228 get_page(*pages = v.bv_page);
1229 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001230 }),({
1231 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001232 })
1233 )
1234 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001235}
1236EXPORT_SYMBOL(iov_iter_get_pages);
1237
Al Viro1b17f1f2014-11-27 14:14:31 -05001238static struct page **get_pages_array(size_t n)
1239{
Michal Hocko752ade62017-05-08 15:57:27 -07001240 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001241}
1242
Al Viro241699c2016-09-22 16:33:12 -04001243static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1244 struct page ***pages, size_t maxsize,
1245 size_t *start)
1246{
1247 struct page **p;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001248 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001249 int idx;
1250 int npages;
1251
Al Viro33844e62016-12-21 21:55:02 -05001252 if (!maxsize)
1253 return 0;
1254
Al Viro241699c2016-09-22 16:33:12 -04001255 if (!sanity(i))
1256 return -EFAULT;
1257
1258 data_start(i, &idx, start);
1259 /* some of this one + all after this one */
1260 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1261 n = npages * PAGE_SIZE - *start;
1262 if (maxsize > n)
1263 maxsize = n;
1264 else
1265 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1266 p = get_pages_array(npages);
1267 if (!p)
1268 return -ENOMEM;
1269 n = __pipe_get_pages(i, maxsize, p, idx, start);
1270 if (n > 0)
1271 *pages = p;
1272 else
1273 kvfree(p);
1274 return n;
1275}
1276
Al Viro62a80672014-04-04 23:12:29 -04001277ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1278 struct page ***pages, size_t maxsize,
1279 size_t *start)
1280{
Al Viro1b17f1f2014-11-27 14:14:31 -05001281 struct page **p;
1282
1283 if (maxsize > i->count)
1284 maxsize = i->count;
1285
Al Viro241699c2016-09-22 16:33:12 -04001286 if (unlikely(i->type & ITER_PIPE))
1287 return pipe_get_pages_alloc(i, pages, maxsize, start);
Al Viro1b17f1f2014-11-27 14:14:31 -05001288 iterate_all_kinds(i, maxsize, v, ({
1289 unsigned long addr = (unsigned long)v.iov_base;
1290 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1291 int n;
1292 int res;
1293
1294 addr &= ~(PAGE_SIZE - 1);
1295 n = DIV_ROUND_UP(len, PAGE_SIZE);
1296 p = get_pages_array(n);
1297 if (!p)
1298 return -ENOMEM;
1299 res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, p);
1300 if (unlikely(res < 0)) {
1301 kvfree(p);
1302 return res;
1303 }
1304 *pages = p;
1305 return (res == n ? len : res * PAGE_SIZE) - *start;
1306 0;}),({
1307 /* can't be more than PAGE_SIZE */
1308 *start = v.bv_offset;
1309 *pages = p = get_pages_array(1);
1310 if (!p)
1311 return -ENOMEM;
1312 get_page(*p = v.bv_page);
1313 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001314 }),({
1315 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -05001316 })
1317 )
1318 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001319}
1320EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1321
Al Viroa604ec72014-11-24 01:08:00 -05001322size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1323 struct iov_iter *i)
1324{
1325 char *to = addr;
1326 __wsum sum, next;
1327 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001328 sum = *csum;
Al Viro241699c2016-09-22 16:33:12 -04001329 if (unlikely(i->type & ITER_PIPE)) {
1330 WARN_ON(1);
1331 return 0;
1332 }
Al Viroa604ec72014-11-24 01:08:00 -05001333 iterate_and_advance(i, bytes, v, ({
1334 int err = 0;
Al Virocbbd26b2016-11-01 22:09:04 -04001335 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001336 (to += v.iov_len) - v.iov_len,
1337 v.iov_len, 0, &err);
1338 if (!err) {
1339 sum = csum_block_add(sum, next, off);
1340 off += v.iov_len;
1341 }
1342 err ? v.iov_len : 0;
1343 }), ({
1344 char *p = kmap_atomic(v.bv_page);
1345 next = csum_partial_copy_nocheck(p + v.bv_offset,
1346 (to += v.bv_len) - v.bv_len,
1347 v.bv_len, 0);
1348 kunmap_atomic(p);
1349 sum = csum_block_add(sum, next, off);
1350 off += v.bv_len;
1351 }),({
1352 next = csum_partial_copy_nocheck(v.iov_base,
1353 (to += v.iov_len) - v.iov_len,
1354 v.iov_len, 0);
1355 sum = csum_block_add(sum, next, off);
1356 off += v.iov_len;
1357 })
1358 )
1359 *csum = sum;
1360 return bytes;
1361}
1362EXPORT_SYMBOL(csum_and_copy_from_iter);
1363
Al Virocbbd26b2016-11-01 22:09:04 -04001364bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1365 struct iov_iter *i)
1366{
1367 char *to = addr;
1368 __wsum sum, next;
1369 size_t off = 0;
1370 sum = *csum;
1371 if (unlikely(i->type & ITER_PIPE)) {
1372 WARN_ON(1);
1373 return false;
1374 }
1375 if (unlikely(i->count < bytes))
1376 return false;
1377 iterate_all_kinds(i, bytes, v, ({
1378 int err = 0;
1379 next = csum_and_copy_from_user(v.iov_base,
1380 (to += v.iov_len) - v.iov_len,
1381 v.iov_len, 0, &err);
1382 if (err)
1383 return false;
1384 sum = csum_block_add(sum, next, off);
1385 off += v.iov_len;
1386 0;
1387 }), ({
1388 char *p = kmap_atomic(v.bv_page);
1389 next = csum_partial_copy_nocheck(p + v.bv_offset,
1390 (to += v.bv_len) - v.bv_len,
1391 v.bv_len, 0);
1392 kunmap_atomic(p);
1393 sum = csum_block_add(sum, next, off);
1394 off += v.bv_len;
1395 }),({
1396 next = csum_partial_copy_nocheck(v.iov_base,
1397 (to += v.iov_len) - v.iov_len,
1398 v.iov_len, 0);
1399 sum = csum_block_add(sum, next, off);
1400 off += v.iov_len;
1401 })
1402 )
1403 *csum = sum;
1404 iov_iter_advance(i, bytes);
1405 return true;
1406}
1407EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1408
Al Viro36f7a8a2015-12-06 16:49:22 -05001409size_t csum_and_copy_to_iter(const void *addr, size_t bytes, __wsum *csum,
Al Viroa604ec72014-11-24 01:08:00 -05001410 struct iov_iter *i)
1411{
Al Viro36f7a8a2015-12-06 16:49:22 -05001412 const char *from = addr;
Al Viroa604ec72014-11-24 01:08:00 -05001413 __wsum sum, next;
1414 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001415 sum = *csum;
Al Viro241699c2016-09-22 16:33:12 -04001416 if (unlikely(i->type & ITER_PIPE)) {
1417 WARN_ON(1); /* for now */
1418 return 0;
1419 }
Al Viroa604ec72014-11-24 01:08:00 -05001420 iterate_and_advance(i, bytes, v, ({
1421 int err = 0;
1422 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001423 v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001424 v.iov_len, 0, &err);
1425 if (!err) {
1426 sum = csum_block_add(sum, next, off);
1427 off += v.iov_len;
1428 }
1429 err ? v.iov_len : 0;
1430 }), ({
1431 char *p = kmap_atomic(v.bv_page);
1432 next = csum_partial_copy_nocheck((from += v.bv_len) - v.bv_len,
1433 p + v.bv_offset,
1434 v.bv_len, 0);
1435 kunmap_atomic(p);
1436 sum = csum_block_add(sum, next, off);
1437 off += v.bv_len;
1438 }),({
1439 next = csum_partial_copy_nocheck((from += v.iov_len) - v.iov_len,
1440 v.iov_base,
1441 v.iov_len, 0);
1442 sum = csum_block_add(sum, next, off);
1443 off += v.iov_len;
1444 })
1445 )
1446 *csum = sum;
1447 return bytes;
1448}
1449EXPORT_SYMBOL(csum_and_copy_to_iter);
1450
Al Viro62a80672014-04-04 23:12:29 -04001451int iov_iter_npages(const struct iov_iter *i, int maxpages)
1452{
Al Viroe0f2dc42014-11-27 14:09:46 -05001453 size_t size = i->count;
1454 int npages = 0;
1455
1456 if (!size)
1457 return 0;
1458
Al Viro241699c2016-09-22 16:33:12 -04001459 if (unlikely(i->type & ITER_PIPE)) {
1460 struct pipe_inode_info *pipe = i->pipe;
1461 size_t off;
1462 int idx;
1463
1464 if (!sanity(i))
1465 return 0;
1466
1467 data_start(i, &idx, &off);
1468 /* some of this one + all after this one */
1469 npages = ((pipe->curbuf - idx - 1) & (pipe->buffers - 1)) + 1;
1470 if (npages >= maxpages)
1471 return maxpages;
1472 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001473 unsigned long p = (unsigned long)v.iov_base;
1474 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1475 - p / PAGE_SIZE;
1476 if (npages >= maxpages)
1477 return maxpages;
1478 0;}),({
1479 npages++;
1480 if (npages >= maxpages)
1481 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001482 }),({
1483 unsigned long p = (unsigned long)v.iov_base;
1484 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1485 - p / PAGE_SIZE;
1486 if (npages >= maxpages)
1487 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001488 })
1489 )
1490 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001491}
Al Virof67da302014-03-19 01:16:16 -04001492EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001493
1494const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1495{
1496 *new = *old;
Al Viro241699c2016-09-22 16:33:12 -04001497 if (unlikely(new->type & ITER_PIPE)) {
1498 WARN_ON(1);
1499 return NULL;
1500 }
Al Viro4b8164b2015-01-31 20:08:47 -05001501 if (new->type & ITER_BVEC)
1502 return new->bvec = kmemdup(new->bvec,
1503 new->nr_segs * sizeof(struct bio_vec),
1504 flags);
1505 else
1506 /* iovec and kvec have identical layout */
1507 return new->iov = kmemdup(new->iov,
1508 new->nr_segs * sizeof(struct iovec),
1509 flags);
1510}
1511EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001512
Vegard Nossumffecee42016-10-08 11:18:07 +02001513/**
1514 * import_iovec() - Copy an array of &struct iovec from userspace
1515 * into the kernel, check that it is valid, and initialize a new
1516 * &struct iov_iter iterator to access it.
1517 *
1518 * @type: One of %READ or %WRITE.
1519 * @uvector: Pointer to the userspace array.
1520 * @nr_segs: Number of elements in userspace array.
1521 * @fast_segs: Number of elements in @iov.
1522 * @iov: (input and output parameter) Pointer to pointer to (usually small
1523 * on-stack) kernel array.
1524 * @i: Pointer to iterator that will be initialized on success.
1525 *
1526 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1527 * then this function places %NULL in *@iov on return. Otherwise, a new
1528 * array will be allocated and the result placed in *@iov. This means that
1529 * the caller may call kfree() on *@iov regardless of whether the small
1530 * on-stack array was used or not (and regardless of whether this function
1531 * returns an error or not).
1532 *
1533 * Return: 0 on success or negative error code on error.
1534 */
Al Virobc917be2015-03-21 17:45:43 -04001535int import_iovec(int type, const struct iovec __user * uvector,
1536 unsigned nr_segs, unsigned fast_segs,
1537 struct iovec **iov, struct iov_iter *i)
1538{
1539 ssize_t n;
1540 struct iovec *p;
1541 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1542 *iov, &p);
1543 if (n < 0) {
1544 if (p != *iov)
1545 kfree(p);
1546 *iov = NULL;
1547 return n;
1548 }
1549 iov_iter_init(i, type, p, nr_segs, n);
1550 *iov = p == *iov ? NULL : p;
1551 return 0;
1552}
1553EXPORT_SYMBOL(import_iovec);
1554
1555#ifdef CONFIG_COMPAT
1556#include <linux/compat.h>
1557
1558int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
1559 unsigned nr_segs, unsigned fast_segs,
1560 struct iovec **iov, struct iov_iter *i)
1561{
1562 ssize_t n;
1563 struct iovec *p;
1564 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1565 *iov, &p);
1566 if (n < 0) {
1567 if (p != *iov)
1568 kfree(p);
1569 *iov = NULL;
1570 return n;
1571 }
1572 iov_iter_init(i, type, p, nr_segs, n);
1573 *iov = p == *iov ? NULL : p;
1574 return 0;
1575}
1576#endif
1577
1578int import_single_range(int rw, void __user *buf, size_t len,
1579 struct iovec *iov, struct iov_iter *i)
1580{
1581 if (len > MAX_RW_COUNT)
1582 len = MAX_RW_COUNT;
1583 if (unlikely(!access_ok(!rw, buf, len)))
1584 return -EFAULT;
1585
1586 iov->iov_base = buf;
1587 iov->iov_len = len;
1588 iov_iter_init(i, rw, iov, 1, len);
1589 return 0;
1590}
Al Viroe1267582015-12-06 20:38:56 -05001591EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05001592
1593int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1594 int (*f)(struct kvec *vec, void *context),
1595 void *context)
1596{
1597 struct kvec w;
1598 int err = -EINVAL;
1599 if (!bytes)
1600 return 0;
1601
1602 iterate_all_kinds(i, bytes, v, -EINVAL, ({
1603 w.iov_base = kmap(v.bv_page) + v.bv_offset;
1604 w.iov_len = v.bv_len;
1605 err = f(&w, context);
1606 kunmap(v.bv_page);
1607 err;}), ({
1608 w = v;
1609 err = f(&w, context);})
1610 )
1611 return err;
1612}
1613EXPORT_SYMBOL(iov_iter_for_each_range);