blob: c93870987b58279bc21a1cfa64d991aff160378d [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>
Sagi Grimbergd05f4432018-12-03 17:52:09 -08009#include <linux/scatterlist.h>
Al Viro4f18cd32014-02-05 19:11:33 -050010
Al Viro241699c2016-09-22 16:33:12 -040011#define PIPE_PARANOIA /* for now */
12
Al Viro04a31162014-11-27 13:51:41 -050013#define iterate_iovec(i, n, __v, __p, skip, STEP) { \
14 size_t left; \
15 size_t wanted = n; \
16 __p = i->iov; \
17 __v.iov_len = min(n, __p->iov_len - skip); \
18 if (likely(__v.iov_len)) { \
19 __v.iov_base = __p->iov_base + skip; \
20 left = (STEP); \
21 __v.iov_len -= left; \
22 skip += __v.iov_len; \
23 n -= __v.iov_len; \
24 } else { \
25 left = 0; \
26 } \
27 while (unlikely(!left && n)) { \
28 __p++; \
29 __v.iov_len = min(n, __p->iov_len); \
30 if (unlikely(!__v.iov_len)) \
31 continue; \
32 __v.iov_base = __p->iov_base; \
33 left = (STEP); \
34 __v.iov_len -= left; \
35 skip = __v.iov_len; \
36 n -= __v.iov_len; \
37 } \
38 n = wanted - n; \
39}
40
Al Viroa2804552014-11-27 14:48:42 -050041#define iterate_kvec(i, n, __v, __p, skip, STEP) { \
42 size_t wanted = n; \
43 __p = i->kvec; \
44 __v.iov_len = min(n, __p->iov_len - skip); \
45 if (likely(__v.iov_len)) { \
46 __v.iov_base = __p->iov_base + skip; \
47 (void)(STEP); \
48 skip += __v.iov_len; \
49 n -= __v.iov_len; \
50 } \
51 while (unlikely(n)) { \
52 __p++; \
53 __v.iov_len = min(n, __p->iov_len); \
54 if (unlikely(!__v.iov_len)) \
55 continue; \
56 __v.iov_base = __p->iov_base; \
57 (void)(STEP); \
58 skip = __v.iov_len; \
59 n -= __v.iov_len; \
60 } \
61 n = wanted; \
62}
63
Ming Lei1bdc76a2016-05-30 21:34:32 +080064#define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
65 struct bvec_iter __start; \
66 __start.bi_size = n; \
67 __start.bi_bvec_done = skip; \
68 __start.bi_idx = 0; \
69 for_each_bvec(__v, i->bvec, __bi, __start) { \
70 if (!__v.bv_len) \
Al Viro04a31162014-11-27 13:51:41 -050071 continue; \
Al Viro04a31162014-11-27 13:51:41 -050072 (void)(STEP); \
Al Viro04a31162014-11-27 13:51:41 -050073 } \
Al Viro04a31162014-11-27 13:51:41 -050074}
75
Al Viroa2804552014-11-27 14:48:42 -050076#define iterate_all_kinds(i, n, v, I, B, K) { \
Al Viro33844e62016-12-21 21:55:02 -050077 if (likely(n)) { \
78 size_t skip = i->iov_offset; \
79 if (unlikely(i->type & ITER_BVEC)) { \
80 struct bio_vec v; \
81 struct bvec_iter __bi; \
82 iterate_bvec(i, n, v, __bi, skip, (B)) \
83 } else if (unlikely(i->type & ITER_KVEC)) { \
84 const struct kvec *kvec; \
85 struct kvec v; \
86 iterate_kvec(i, n, v, kvec, skip, (K)) \
David Howells9ea9ce02018-10-20 00:57:56 +010087 } else if (unlikely(i->type & ITER_DISCARD)) { \
Al Viro33844e62016-12-21 21:55:02 -050088 } else { \
89 const struct iovec *iov; \
90 struct iovec v; \
91 iterate_iovec(i, n, v, iov, skip, (I)) \
92 } \
Al Viro04a31162014-11-27 13:51:41 -050093 } \
94}
95
Al Viroa2804552014-11-27 14:48:42 -050096#define iterate_and_advance(i, n, v, I, B, K) { \
Al Virodd254f52016-05-09 11:54:48 -040097 if (unlikely(i->count < n)) \
98 n = i->count; \
Al Viro19f18452016-05-25 17:36:19 -040099 if (i->count) { \
Al Virodd254f52016-05-09 11:54:48 -0400100 size_t skip = i->iov_offset; \
101 if (unlikely(i->type & ITER_BVEC)) { \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800102 const struct bio_vec *bvec = i->bvec; \
Al Virodd254f52016-05-09 11:54:48 -0400103 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800104 struct bvec_iter __bi; \
105 iterate_bvec(i, n, v, __bi, skip, (B)) \
106 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
107 i->nr_segs -= i->bvec - bvec; \
108 skip = __bi.bi_bvec_done; \
Al Virodd254f52016-05-09 11:54:48 -0400109 } else if (unlikely(i->type & ITER_KVEC)) { \
110 const struct kvec *kvec; \
111 struct kvec v; \
112 iterate_kvec(i, n, v, kvec, skip, (K)) \
113 if (skip == kvec->iov_len) { \
114 kvec++; \
115 skip = 0; \
116 } \
117 i->nr_segs -= kvec - i->kvec; \
118 i->kvec = kvec; \
David Howells9ea9ce02018-10-20 00:57:56 +0100119 } else if (unlikely(i->type & ITER_DISCARD)) { \
120 skip += n; \
Al Virodd254f52016-05-09 11:54:48 -0400121 } else { \
122 const struct iovec *iov; \
123 struct iovec v; \
124 iterate_iovec(i, n, v, iov, skip, (I)) \
125 if (skip == iov->iov_len) { \
126 iov++; \
127 skip = 0; \
128 } \
129 i->nr_segs -= iov - i->iov; \
130 i->iov = iov; \
Al Viro7ce2a912014-11-27 13:59:45 -0500131 } \
Al Virodd254f52016-05-09 11:54:48 -0400132 i->count -= n; \
133 i->iov_offset = skip; \
Al Viro7ce2a912014-11-27 13:59:45 -0500134 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500135}
136
Al Viro09fc68dc2017-06-29 22:25:14 -0400137static int copyout(void __user *to, const void *from, size_t n)
138{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800139 if (access_ok(to, n)) {
Al Viro09fc68dc2017-06-29 22:25:14 -0400140 kasan_check_read(from, n);
141 n = raw_copy_to_user(to, from, n);
142 }
143 return n;
144}
145
146static int copyin(void *to, const void __user *from, size_t n)
147{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800148 if (access_ok(from, n)) {
Al Viro09fc68dc2017-06-29 22:25:14 -0400149 kasan_check_write(to, n);
150 n = raw_copy_from_user(to, from, n);
151 }
152 return n;
153}
154
Al Viro62a80672014-04-04 23:12:29 -0400155static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500156 struct iov_iter *i)
157{
158 size_t skip, copy, left, wanted;
159 const struct iovec *iov;
160 char __user *buf;
161 void *kaddr, *from;
162
163 if (unlikely(bytes > i->count))
164 bytes = i->count;
165
166 if (unlikely(!bytes))
167 return 0;
168
Al Viro09fc68dc2017-06-29 22:25:14 -0400169 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500170 wanted = bytes;
171 iov = i->iov;
172 skip = i->iov_offset;
173 buf = iov->iov_base + skip;
174 copy = min(bytes, iov->iov_len - skip);
175
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700176 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500177 kaddr = kmap_atomic(page);
178 from = kaddr + offset;
179
180 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400181 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500182 copy -= left;
183 skip += copy;
184 from += copy;
185 bytes -= copy;
186
187 while (unlikely(!left && bytes)) {
188 iov++;
189 buf = iov->iov_base;
190 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400191 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500192 copy -= left;
193 skip = copy;
194 from += copy;
195 bytes -= copy;
196 }
197 if (likely(!bytes)) {
198 kunmap_atomic(kaddr);
199 goto done;
200 }
201 offset = from - kaddr;
202 buf += copy;
203 kunmap_atomic(kaddr);
204 copy = min(bytes, iov->iov_len - skip);
205 }
206 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700207
Al Viro4f18cd32014-02-05 19:11:33 -0500208 kaddr = kmap(page);
209 from = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400210 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500211 copy -= left;
212 skip += copy;
213 from += copy;
214 bytes -= copy;
215 while (unlikely(!left && bytes)) {
216 iov++;
217 buf = iov->iov_base;
218 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400219 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500220 copy -= left;
221 skip = copy;
222 from += copy;
223 bytes -= copy;
224 }
225 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700226
Al Viro4f18cd32014-02-05 19:11:33 -0500227done:
Al Viro81055e52014-04-04 19:23:46 -0400228 if (skip == iov->iov_len) {
229 iov++;
230 skip = 0;
231 }
Al Viro4f18cd32014-02-05 19:11:33 -0500232 i->count -= wanted - bytes;
233 i->nr_segs -= iov - i->iov;
234 i->iov = iov;
235 i->iov_offset = skip;
236 return wanted - bytes;
237}
Al Viro4f18cd32014-02-05 19:11:33 -0500238
Al Viro62a80672014-04-04 23:12:29 -0400239static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400240 struct iov_iter *i)
241{
242 size_t skip, copy, left, wanted;
243 const struct iovec *iov;
244 char __user *buf;
245 void *kaddr, *to;
246
247 if (unlikely(bytes > i->count))
248 bytes = i->count;
249
250 if (unlikely(!bytes))
251 return 0;
252
Al Viro09fc68dc2017-06-29 22:25:14 -0400253 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400254 wanted = bytes;
255 iov = i->iov;
256 skip = i->iov_offset;
257 buf = iov->iov_base + skip;
258 copy = min(bytes, iov->iov_len - skip);
259
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700260 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400261 kaddr = kmap_atomic(page);
262 to = kaddr + offset;
263
264 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400265 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400266 copy -= left;
267 skip += copy;
268 to += copy;
269 bytes -= copy;
270
271 while (unlikely(!left && bytes)) {
272 iov++;
273 buf = iov->iov_base;
274 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400275 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400276 copy -= left;
277 skip = copy;
278 to += copy;
279 bytes -= copy;
280 }
281 if (likely(!bytes)) {
282 kunmap_atomic(kaddr);
283 goto done;
284 }
285 offset = to - kaddr;
286 buf += copy;
287 kunmap_atomic(kaddr);
288 copy = min(bytes, iov->iov_len - skip);
289 }
290 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700291
Al Virof0d1bec2014-04-03 15:05:18 -0400292 kaddr = kmap(page);
293 to = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400294 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400295 copy -= left;
296 skip += copy;
297 to += copy;
298 bytes -= copy;
299 while (unlikely(!left && bytes)) {
300 iov++;
301 buf = iov->iov_base;
302 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400303 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400304 copy -= left;
305 skip = copy;
306 to += copy;
307 bytes -= copy;
308 }
309 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700310
Al Virof0d1bec2014-04-03 15:05:18 -0400311done:
Al Viro81055e52014-04-04 19:23:46 -0400312 if (skip == iov->iov_len) {
313 iov++;
314 skip = 0;
315 }
Al Virof0d1bec2014-04-03 15:05:18 -0400316 i->count -= wanted - bytes;
317 i->nr_segs -= iov - i->iov;
318 i->iov = iov;
319 i->iov_offset = skip;
320 return wanted - bytes;
321}
Al Virof0d1bec2014-04-03 15:05:18 -0400322
Al Viro241699c2016-09-22 16:33:12 -0400323#ifdef PIPE_PARANOIA
324static bool sanity(const struct iov_iter *i)
325{
326 struct pipe_inode_info *pipe = i->pipe;
327 int idx = i->idx;
328 int next = pipe->curbuf + pipe->nrbufs;
329 if (i->iov_offset) {
330 struct pipe_buffer *p;
331 if (unlikely(!pipe->nrbufs))
332 goto Bad; // pipe must be non-empty
333 if (unlikely(idx != ((next - 1) & (pipe->buffers - 1))))
334 goto Bad; // must be at the last buffer...
335
336 p = &pipe->bufs[idx];
337 if (unlikely(p->offset + p->len != i->iov_offset))
338 goto Bad; // ... at the end of segment
339 } else {
340 if (idx != (next & (pipe->buffers - 1)))
341 goto Bad; // must be right after the last buffer
342 }
343 return true;
344Bad:
345 printk(KERN_ERR "idx = %d, offset = %zd\n", i->idx, i->iov_offset);
346 printk(KERN_ERR "curbuf = %d, nrbufs = %d, buffers = %d\n",
347 pipe->curbuf, pipe->nrbufs, pipe->buffers);
348 for (idx = 0; idx < pipe->buffers; idx++)
349 printk(KERN_ERR "[%p %p %d %d]\n",
350 pipe->bufs[idx].ops,
351 pipe->bufs[idx].page,
352 pipe->bufs[idx].offset,
353 pipe->bufs[idx].len);
354 WARN_ON(1);
355 return false;
356}
357#else
358#define sanity(i) true
359#endif
360
361static inline int next_idx(int idx, struct pipe_inode_info *pipe)
362{
363 return (idx + 1) & (pipe->buffers - 1);
364}
365
366static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
367 struct iov_iter *i)
368{
369 struct pipe_inode_info *pipe = i->pipe;
370 struct pipe_buffer *buf;
371 size_t off;
372 int idx;
373
374 if (unlikely(bytes > i->count))
375 bytes = i->count;
376
377 if (unlikely(!bytes))
378 return 0;
379
380 if (!sanity(i))
381 return 0;
382
383 off = i->iov_offset;
384 idx = i->idx;
385 buf = &pipe->bufs[idx];
386 if (off) {
387 if (offset == off && buf->page == page) {
388 /* merge with the last one */
389 buf->len += bytes;
390 i->iov_offset += bytes;
391 goto out;
392 }
393 idx = next_idx(idx, pipe);
394 buf = &pipe->bufs[idx];
395 }
396 if (idx == pipe->curbuf && pipe->nrbufs)
397 return 0;
398 pipe->nrbufs++;
399 buf->ops = &page_cache_pipe_buf_ops;
400 get_page(buf->page = page);
401 buf->offset = offset;
402 buf->len = bytes;
403 i->iov_offset = offset + bytes;
404 i->idx = idx;
405out:
406 i->count -= bytes;
407 return bytes;
408}
409
Al Viro4f18cd32014-02-05 19:11:33 -0500410/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400411 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
412 * bytes. For each iovec, fault in each page that constitutes the iovec.
413 *
414 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
415 * because it is an invalid address).
416 */
Al Virod4690f12016-09-16 00:11:45 +0100417int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400418{
419 size_t skip = i->iov_offset;
420 const struct iovec *iov;
421 int err;
422 struct iovec v;
423
424 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
425 iterate_iovec(i, bytes, v, iov, skip, ({
Al Viro4bce9f6e2016-09-17 18:02:44 -0400426 err = fault_in_pages_readable(v.iov_base, v.iov_len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400427 if (unlikely(err))
428 return err;
429 0;}))
430 }
431 return 0;
432}
Al Virod4690f12016-09-16 00:11:45 +0100433EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400434
David Howellsaa563d72018-10-20 00:57:56 +0100435void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500436 const struct iovec *iov, unsigned long nr_segs,
437 size_t count)
438{
David Howellsaa563d72018-10-20 00:57:56 +0100439 WARN_ON(direction & ~(READ | WRITE));
440 direction &= READ | WRITE;
441
Al Viro71d8e532014-03-05 19:28:09 -0500442 /* It will get better. Eventually... */
Al Virodb68ce12017-03-20 21:08:07 -0400443 if (uaccess_kernel()) {
David Howellsaa563d72018-10-20 00:57:56 +0100444 i->type = ITER_KVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500445 i->kvec = (struct kvec *)iov;
446 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100447 i->type = ITER_IOVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500448 i->iov = iov;
449 }
Al Viro71d8e532014-03-05 19:28:09 -0500450 i->nr_segs = nr_segs;
451 i->iov_offset = 0;
452 i->count = count;
453}
454EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400455
Al Viro62a80672014-04-04 23:12:29 -0400456static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
457{
458 char *from = kmap_atomic(page);
459 memcpy(to, from + offset, len);
460 kunmap_atomic(from);
461}
462
Al Viro36f7a8a2015-12-06 16:49:22 -0500463static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
Al Viro62a80672014-04-04 23:12:29 -0400464{
465 char *to = kmap_atomic(page);
466 memcpy(to + offset, from, len);
467 kunmap_atomic(to);
468}
469
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400470static void memzero_page(struct page *page, size_t offset, size_t len)
471{
472 char *addr = kmap_atomic(page);
473 memset(addr + offset, 0, len);
474 kunmap_atomic(addr);
475}
476
Al Viro241699c2016-09-22 16:33:12 -0400477static inline bool allocated(struct pipe_buffer *buf)
478{
479 return buf->ops == &default_pipe_buf_ops;
480}
481
482static inline void data_start(const struct iov_iter *i, int *idxp, size_t *offp)
483{
484 size_t off = i->iov_offset;
485 int idx = i->idx;
486 if (off && (!allocated(&i->pipe->bufs[idx]) || off == PAGE_SIZE)) {
487 idx = next_idx(idx, i->pipe);
488 off = 0;
489 }
490 *idxp = idx;
491 *offp = off;
492}
493
494static size_t push_pipe(struct iov_iter *i, size_t size,
495 int *idxp, size_t *offp)
496{
497 struct pipe_inode_info *pipe = i->pipe;
498 size_t off;
499 int idx;
500 ssize_t left;
501
502 if (unlikely(size > i->count))
503 size = i->count;
504 if (unlikely(!size))
505 return 0;
506
507 left = size;
508 data_start(i, &idx, &off);
509 *idxp = idx;
510 *offp = off;
511 if (off) {
512 left -= PAGE_SIZE - off;
513 if (left <= 0) {
514 pipe->bufs[idx].len += size;
515 return size;
516 }
517 pipe->bufs[idx].len = PAGE_SIZE;
518 idx = next_idx(idx, pipe);
519 }
520 while (idx != pipe->curbuf || !pipe->nrbufs) {
521 struct page *page = alloc_page(GFP_USER);
522 if (!page)
523 break;
524 pipe->nrbufs++;
525 pipe->bufs[idx].ops = &default_pipe_buf_ops;
526 pipe->bufs[idx].page = page;
527 pipe->bufs[idx].offset = 0;
528 if (left <= PAGE_SIZE) {
529 pipe->bufs[idx].len = left;
530 return size;
531 }
532 pipe->bufs[idx].len = PAGE_SIZE;
533 left -= PAGE_SIZE;
534 idx = next_idx(idx, pipe);
535 }
536 return size - left;
537}
538
539static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
540 struct iov_iter *i)
541{
542 struct pipe_inode_info *pipe = i->pipe;
543 size_t n, off;
544 int idx;
545
546 if (!sanity(i))
547 return 0;
548
549 bytes = n = push_pipe(i, bytes, &idx, &off);
550 if (unlikely(!n))
551 return 0;
552 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
553 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
554 memcpy_to_page(pipe->bufs[idx].page, off, addr, chunk);
555 i->idx = idx;
556 i->iov_offset = off + chunk;
557 n -= chunk;
558 addr += chunk;
559 }
560 i->count -= bytes;
561 return bytes;
562}
563
Al Viro78e1f382018-11-25 16:24:16 -0500564static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
565 __wsum *csum, struct iov_iter *i)
566{
567 struct pipe_inode_info *pipe = i->pipe;
568 size_t n, r;
569 size_t off = 0;
570 __wsum sum = *csum, next;
571 int idx;
572
573 if (!sanity(i))
574 return 0;
575
576 bytes = n = push_pipe(i, bytes, &idx, &r);
577 if (unlikely(!n))
578 return 0;
579 for ( ; n; idx = next_idx(idx, pipe), r = 0) {
580 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
581 char *p = kmap_atomic(pipe->bufs[idx].page);
582 next = csum_partial_copy_nocheck(addr, p + r, chunk, 0);
583 sum = csum_block_add(sum, next, off);
584 kunmap_atomic(p);
585 i->idx = idx;
586 i->iov_offset = r + chunk;
587 n -= chunk;
588 off += chunk;
589 addr += chunk;
590 }
591 i->count -= bytes;
592 *csum = sum;
593 return bytes;
594}
595
Al Viroaa28de22017-06-29 21:45:10 -0400596size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400597{
Al Viro36f7a8a2015-12-06 16:49:22 -0500598 const char *from = addr;
David Howells00e23702018-10-22 13:07:28 +0100599 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400600 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400601 if (iter_is_iovec(i))
602 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500603 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400604 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500605 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500606 (from += v.bv_len) - v.bv_len, v.bv_len),
607 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500608 )
Al Viro62a80672014-04-04 23:12:29 -0400609
Al Viro3d4d3e42014-11-27 14:28:06 -0500610 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400611}
Al Viroaa28de22017-06-29 21:45:10 -0400612EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400613
Dan Williams87803562018-05-03 17:06:31 -0700614#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
615static int copyout_mcsafe(void __user *to, const void *from, size_t n)
616{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800617 if (access_ok(to, n)) {
Dan Williams87803562018-05-03 17:06:31 -0700618 kasan_check_read(from, n);
619 n = copy_to_user_mcsafe((__force void *) to, from, n);
620 }
621 return n;
622}
623
624static unsigned long memcpy_mcsafe_to_page(struct page *page, size_t offset,
625 const char *from, size_t len)
626{
627 unsigned long ret;
628 char *to;
629
630 to = kmap_atomic(page);
631 ret = memcpy_mcsafe(to + offset, from, len);
632 kunmap_atomic(to);
633
634 return ret;
635}
636
Dan Williamsca146f62018-07-08 13:46:12 -0700637static size_t copy_pipe_to_iter_mcsafe(const void *addr, size_t bytes,
638 struct iov_iter *i)
639{
640 struct pipe_inode_info *pipe = i->pipe;
641 size_t n, off, xfer = 0;
642 int idx;
643
644 if (!sanity(i))
645 return 0;
646
647 bytes = n = push_pipe(i, bytes, &idx, &off);
648 if (unlikely(!n))
649 return 0;
650 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
651 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
652 unsigned long rem;
653
654 rem = memcpy_mcsafe_to_page(pipe->bufs[idx].page, off, addr,
655 chunk);
656 i->idx = idx;
657 i->iov_offset = off + chunk - rem;
658 xfer += chunk - rem;
659 if (rem)
660 break;
661 n -= chunk;
662 addr += chunk;
663 }
664 i->count -= xfer;
665 return xfer;
666}
667
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700668/**
669 * _copy_to_iter_mcsafe - copy to user with source-read error exception handling
670 * @addr: source kernel address
671 * @bytes: total transfer length
672 * @iter: destination iterator
673 *
674 * The pmem driver arranges for filesystem-dax to use this facility via
675 * dax_copy_to_iter() for protecting read/write to persistent memory.
676 * Unless / until an architecture can guarantee identical performance
677 * between _copy_to_iter_mcsafe() and _copy_to_iter() it would be a
678 * performance regression to switch more users to the mcsafe version.
679 *
680 * Otherwise, the main differences between this and typical _copy_to_iter().
681 *
682 * * Typical tail/residue handling after a fault retries the copy
683 * byte-by-byte until the fault happens again. Re-triggering machine
684 * checks is potentially fatal so the implementation uses source
685 * alignment and poison alignment assumptions to avoid re-triggering
686 * hardware exceptions.
687 *
688 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
689 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
690 * a short copy.
691 *
692 * See MCSAFE_TEST for self-test.
693 */
Dan Williams87803562018-05-03 17:06:31 -0700694size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i)
695{
696 const char *from = addr;
697 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
698
David Howells00e23702018-10-22 13:07:28 +0100699 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsca146f62018-07-08 13:46:12 -0700700 return copy_pipe_to_iter_mcsafe(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700701 if (iter_is_iovec(i))
702 might_fault();
703 iterate_and_advance(i, bytes, v,
704 copyout_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
705 ({
706 rem = memcpy_mcsafe_to_page(v.bv_page, v.bv_offset,
707 (from += v.bv_len) - v.bv_len, v.bv_len);
708 if (rem) {
709 curr_addr = (unsigned long) from;
710 bytes = curr_addr - s_addr - rem;
711 return bytes;
712 }
713 }),
714 ({
715 rem = memcpy_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len,
716 v.iov_len);
717 if (rem) {
718 curr_addr = (unsigned long) from;
719 bytes = curr_addr - s_addr - rem;
720 return bytes;
721 }
722 })
723 )
724
725 return bytes;
726}
727EXPORT_SYMBOL_GPL(_copy_to_iter_mcsafe);
728#endif /* CONFIG_ARCH_HAS_UACCESS_MCSAFE */
729
Al Viroaa28de22017-06-29 21:45:10 -0400730size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400731{
Al Viro0dbca9a2014-11-27 14:26:43 -0500732 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100733 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400734 WARN_ON(1);
735 return 0;
736 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400737 if (iter_is_iovec(i))
738 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500739 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400740 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500741 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500742 v.bv_offset, v.bv_len),
743 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500744 )
745
746 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400747}
Al Viroaa28de22017-06-29 21:45:10 -0400748EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400749
Al Viroaa28de22017-06-29 21:45:10 -0400750bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400751{
752 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100753 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400754 WARN_ON(1);
755 return false;
756 }
Al Viro33844e62016-12-21 21:55:02 -0500757 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400758 return false;
759
Al Viro09fc68dc2017-06-29 22:25:14 -0400760 if (iter_is_iovec(i))
761 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400762 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68dc2017-06-29 22:25:14 -0400763 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400764 v.iov_base, v.iov_len))
765 return false;
766 0;}),
767 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
768 v.bv_offset, v.bv_len),
769 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
770 )
771
772 iov_iter_advance(i, bytes);
773 return true;
774}
Al Viroaa28de22017-06-29 21:45:10 -0400775EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400776
Al Viroaa28de22017-06-29 21:45:10 -0400777size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500778{
779 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100780 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400781 WARN_ON(1);
782 return 0;
783 }
Al Viroaa583092014-11-27 20:27:08 -0500784 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400785 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500786 v.iov_base, v.iov_len),
787 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
788 v.bv_offset, v.bv_len),
789 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
790 )
791
792 return bytes;
793}
Al Viroaa28de22017-06-29 21:45:10 -0400794EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500795
Dan Williams0aed55a2017-05-29 12:22:50 -0700796#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700797/**
798 * _copy_from_iter_flushcache - write destination through cpu cache
799 * @addr: destination kernel address
800 * @bytes: total transfer length
801 * @iter: source iterator
802 *
803 * The pmem driver arranges for filesystem-dax to use this facility via
804 * dax_copy_from_iter() for ensuring that writes to persistent memory
805 * are flushed through the CPU cache. It is differentiated from
806 * _copy_from_iter_nocache() in that guarantees all data is flushed for
807 * all iterator types. The _copy_from_iter_nocache() only attempts to
808 * bypass the cache for the ITER_IOVEC case, and on some archs may use
809 * instructions that strand dirty-data in the cache.
810 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700811size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700812{
813 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100814 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700815 WARN_ON(1);
816 return 0;
817 }
818 iterate_and_advance(i, bytes, v,
819 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
820 v.iov_base, v.iov_len),
821 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
822 v.bv_offset, v.bv_len),
823 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
824 v.iov_len)
825 )
826
827 return bytes;
828}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700829EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700830#endif
831
Al Viroaa28de22017-06-29 21:45:10 -0400832bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400833{
834 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100835 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400836 WARN_ON(1);
837 return false;
838 }
Al Viro33844e62016-12-21 21:55:02 -0500839 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400840 return false;
841 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400842 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400843 v.iov_base, v.iov_len))
844 return false;
845 0;}),
846 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
847 v.bv_offset, v.bv_len),
848 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
849 )
850
851 iov_iter_advance(i, bytes);
852 return true;
853}
Al Viroaa28de22017-06-29 21:45:10 -0400854EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400855
Al Viro72e809e2017-06-29 21:52:57 -0400856static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
857{
Petar Penkova90bcb82017-08-29 11:20:32 -0700858 struct page *head = compound_head(page);
859 size_t v = n + offset + page_address(page) - page_address(head);
860
861 if (likely(n <= v && v <= (PAGE_SIZE << compound_order(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400862 return true;
863 WARN_ON(1);
864 return false;
865}
Al Virod2715242014-11-27 14:22:37 -0500866
867size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
868 struct iov_iter *i)
869{
Al Viro72e809e2017-06-29 21:52:57 -0400870 if (unlikely(!page_copy_sane(page, offset, bytes)))
871 return 0;
Al Virod2715242014-11-27 14:22:37 -0500872 if (i->type & (ITER_BVEC|ITER_KVEC)) {
873 void *kaddr = kmap_atomic(page);
874 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
875 kunmap_atomic(kaddr);
876 return wanted;
David Howells9ea9ce02018-10-20 00:57:56 +0100877 } else if (unlikely(iov_iter_is_discard(i)))
878 return bytes;
879 else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500880 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400881 else
882 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500883}
884EXPORT_SYMBOL(copy_page_to_iter);
885
886size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
887 struct iov_iter *i)
888{
Al Viro72e809e2017-06-29 21:52:57 -0400889 if (unlikely(!page_copy_sane(page, offset, bytes)))
890 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +0100891 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400892 WARN_ON(1);
893 return 0;
894 }
Al Virod2715242014-11-27 14:22:37 -0500895 if (i->type & (ITER_BVEC|ITER_KVEC)) {
896 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400897 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500898 kunmap_atomic(kaddr);
899 return wanted;
900 } else
901 return copy_page_from_iter_iovec(page, offset, bytes, i);
902}
903EXPORT_SYMBOL(copy_page_from_iter);
904
Al Viro241699c2016-09-22 16:33:12 -0400905static size_t pipe_zero(size_t bytes, struct iov_iter *i)
906{
907 struct pipe_inode_info *pipe = i->pipe;
908 size_t n, off;
909 int idx;
910
911 if (!sanity(i))
912 return 0;
913
914 bytes = n = push_pipe(i, bytes, &idx, &off);
915 if (unlikely(!n))
916 return 0;
917
918 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
919 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
920 memzero_page(pipe->bufs[idx].page, off, chunk);
921 i->idx = idx;
922 i->iov_offset = off + chunk;
923 n -= chunk;
924 }
925 i->count -= bytes;
926 return bytes;
927}
928
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400929size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
930{
David Howells00e23702018-10-22 13:07:28 +0100931 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400932 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500933 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400934 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500935 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
936 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500937 )
938
939 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400940}
941EXPORT_SYMBOL(iov_iter_zero);
942
Al Viro62a80672014-04-04 23:12:29 -0400943size_t iov_iter_copy_from_user_atomic(struct page *page,
944 struct iov_iter *i, unsigned long offset, size_t bytes)
945{
Al Viro04a31162014-11-27 13:51:41 -0500946 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400947 if (unlikely(!page_copy_sane(page, offset, bytes))) {
948 kunmap_atomic(kaddr);
949 return 0;
950 }
David Howells9ea9ce02018-10-20 00:57:56 +0100951 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400952 kunmap_atomic(kaddr);
953 WARN_ON(1);
954 return 0;
955 }
Al Viro04a31162014-11-27 13:51:41 -0500956 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400957 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -0500958 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500959 v.bv_offset, v.bv_len),
960 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -0500961 )
962 kunmap_atomic(kaddr);
963 return bytes;
Al Viro62a80672014-04-04 23:12:29 -0400964}
965EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
966
Al Virob9dc6f62017-01-14 19:33:08 -0500967static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -0400968{
969 struct pipe_inode_info *pipe = i->pipe;
Al Viro241699c2016-09-22 16:33:12 -0400970 if (pipe->nrbufs) {
Al Virob9dc6f62017-01-14 19:33:08 -0500971 size_t off = i->iov_offset;
972 int idx = i->idx;
973 int nrbufs = (idx - pipe->curbuf) & (pipe->buffers - 1);
974 if (off) {
975 pipe->bufs[idx].len = off - pipe->bufs[idx].offset;
976 idx = next_idx(idx, pipe);
977 nrbufs++;
978 }
979 while (pipe->nrbufs > nrbufs) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200980 pipe_buf_release(pipe, &pipe->bufs[idx]);
Al Viro241699c2016-09-22 16:33:12 -0400981 idx = next_idx(idx, pipe);
982 pipe->nrbufs--;
983 }
984 }
Al Virob9dc6f62017-01-14 19:33:08 -0500985}
986
987static void pipe_advance(struct iov_iter *i, size_t size)
988{
989 struct pipe_inode_info *pipe = i->pipe;
990 if (unlikely(i->count < size))
991 size = i->count;
992 if (size) {
993 struct pipe_buffer *buf;
994 size_t off = i->iov_offset, left = size;
995 int idx = i->idx;
996 if (off) /* make it relative to the beginning of buffer */
997 left += off - pipe->bufs[idx].offset;
998 while (1) {
999 buf = &pipe->bufs[idx];
1000 if (left <= buf->len)
1001 break;
1002 left -= buf->len;
1003 idx = next_idx(idx, pipe);
1004 }
1005 i->idx = idx;
1006 i->iov_offset = buf->offset + left;
1007 }
1008 i->count -= size;
1009 /* ... and discard everything past that point */
1010 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001011}
1012
Al Viro62a80672014-04-04 23:12:29 -04001013void iov_iter_advance(struct iov_iter *i, size_t size)
1014{
David Howells00e23702018-10-22 13:07:28 +01001015 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001016 pipe_advance(i, size);
1017 return;
1018 }
David Howells9ea9ce02018-10-20 00:57:56 +01001019 if (unlikely(iov_iter_is_discard(i))) {
1020 i->count -= size;
1021 return;
1022 }
Al Viroa2804552014-11-27 14:48:42 -05001023 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -04001024}
1025EXPORT_SYMBOL(iov_iter_advance);
1026
Al Viro27c0e372017-02-17 18:42:24 -05001027void iov_iter_revert(struct iov_iter *i, size_t unroll)
1028{
1029 if (!unroll)
1030 return;
Al Viro5b47d592017-05-08 13:54:47 -04001031 if (WARN_ON(unroll > MAX_RW_COUNT))
1032 return;
Al Viro27c0e372017-02-17 18:42:24 -05001033 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001034 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001035 struct pipe_inode_info *pipe = i->pipe;
1036 int idx = i->idx;
1037 size_t off = i->iov_offset;
1038 while (1) {
1039 size_t n = off - pipe->bufs[idx].offset;
1040 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001041 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001042 break;
1043 }
1044 unroll -= n;
1045 if (!unroll && idx == i->start_idx) {
1046 off = 0;
1047 break;
1048 }
1049 if (!idx--)
1050 idx = pipe->buffers - 1;
1051 off = pipe->bufs[idx].offset + pipe->bufs[idx].len;
1052 }
1053 i->iov_offset = off;
1054 i->idx = idx;
1055 pipe_truncate(i);
1056 return;
1057 }
David Howells9ea9ce02018-10-20 00:57:56 +01001058 if (unlikely(iov_iter_is_discard(i)))
1059 return;
Al Viro27c0e372017-02-17 18:42:24 -05001060 if (unroll <= i->iov_offset) {
1061 i->iov_offset -= unroll;
1062 return;
1063 }
1064 unroll -= i->iov_offset;
David Howells00e23702018-10-22 13:07:28 +01001065 if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001066 const struct bio_vec *bvec = i->bvec;
1067 while (1) {
1068 size_t n = (--bvec)->bv_len;
1069 i->nr_segs++;
1070 if (unroll <= n) {
1071 i->bvec = bvec;
1072 i->iov_offset = n - unroll;
1073 return;
1074 }
1075 unroll -= n;
1076 }
1077 } else { /* same logics for iovec and kvec */
1078 const struct iovec *iov = i->iov;
1079 while (1) {
1080 size_t n = (--iov)->iov_len;
1081 i->nr_segs++;
1082 if (unroll <= n) {
1083 i->iov = iov;
1084 i->iov_offset = n - unroll;
1085 return;
1086 }
1087 unroll -= n;
1088 }
1089 }
1090}
1091EXPORT_SYMBOL(iov_iter_revert);
1092
Al Viro62a80672014-04-04 23:12:29 -04001093/*
1094 * Return the count of just the current iov_iter segment.
1095 */
1096size_t iov_iter_single_seg_count(const struct iov_iter *i)
1097{
David Howells00e23702018-10-22 13:07:28 +01001098 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001099 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001100 if (i->nr_segs == 1)
1101 return i->count;
David Howells9ea9ce02018-10-20 00:57:56 +01001102 if (unlikely(iov_iter_is_discard(i)))
1103 return i->count;
David Howells00e23702018-10-22 13:07:28 +01001104 else if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001105 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001106 else
1107 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001108}
1109EXPORT_SYMBOL(iov_iter_single_seg_count);
1110
David Howellsaa563d72018-10-20 00:57:56 +01001111void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001112 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001113 size_t count)
1114{
David Howellsaa563d72018-10-20 00:57:56 +01001115 WARN_ON(direction & ~(READ | WRITE));
1116 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001117 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001118 i->nr_segs = nr_segs;
1119 i->iov_offset = 0;
1120 i->count = count;
1121}
1122EXPORT_SYMBOL(iov_iter_kvec);
1123
David Howellsaa563d72018-10-20 00:57:56 +01001124void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001125 const struct bio_vec *bvec, unsigned long nr_segs,
1126 size_t count)
1127{
David Howellsaa563d72018-10-20 00:57:56 +01001128 WARN_ON(direction & ~(READ | WRITE));
1129 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001130 i->bvec = bvec;
1131 i->nr_segs = nr_segs;
1132 i->iov_offset = 0;
1133 i->count = count;
1134}
1135EXPORT_SYMBOL(iov_iter_bvec);
1136
David Howellsaa563d72018-10-20 00:57:56 +01001137void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001138 struct pipe_inode_info *pipe,
1139 size_t count)
1140{
David Howellsaa563d72018-10-20 00:57:56 +01001141 BUG_ON(direction != READ);
Al Virob9dc6f62017-01-14 19:33:08 -05001142 WARN_ON(pipe->nrbufs == pipe->buffers);
David Howellsaa563d72018-10-20 00:57:56 +01001143 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001144 i->pipe = pipe;
1145 i->idx = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1146 i->iov_offset = 0;
1147 i->count = count;
Al Viro27c0e372017-02-17 18:42:24 -05001148 i->start_idx = i->idx;
Al Viro241699c2016-09-22 16:33:12 -04001149}
1150EXPORT_SYMBOL(iov_iter_pipe);
1151
David Howells9ea9ce02018-10-20 00:57:56 +01001152/**
1153 * iov_iter_discard - Initialise an I/O iterator that discards data
1154 * @i: The iterator to initialise.
1155 * @direction: The direction of the transfer.
1156 * @count: The size of the I/O buffer in bytes.
1157 *
1158 * Set up an I/O iterator that just discards everything that's written to it.
1159 * It's only available as a READ iterator.
1160 */
1161void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1162{
1163 BUG_ON(direction != READ);
1164 i->type = ITER_DISCARD | READ;
1165 i->count = count;
1166 i->iov_offset = 0;
1167}
1168EXPORT_SYMBOL(iov_iter_discard);
1169
Al Viro62a80672014-04-04 23:12:29 -04001170unsigned long iov_iter_alignment(const struct iov_iter *i)
1171{
Al Viro04a31162014-11-27 13:51:41 -05001172 unsigned long res = 0;
1173 size_t size = i->count;
1174
David Howells00e23702018-10-22 13:07:28 +01001175 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro33844e62016-12-21 21:55:02 -05001176 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->idx]))
Al Viro241699c2016-09-22 16:33:12 -04001177 return size | i->iov_offset;
1178 return size;
1179 }
Al Viro04a31162014-11-27 13:51:41 -05001180 iterate_all_kinds(i, size, v,
1181 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001182 res |= v.bv_offset | v.bv_len,
1183 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -05001184 )
1185 return res;
Al Viro62a80672014-04-04 23:12:29 -04001186}
1187EXPORT_SYMBOL(iov_iter_alignment);
1188
Al Viro357f4352016-04-08 19:05:19 -04001189unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1190{
Al Viro33844e62016-12-21 21:55:02 -05001191 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001192 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001193
David Howells9ea9ce02018-10-20 00:57:56 +01001194 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001195 WARN_ON(1);
1196 return ~0U;
1197 }
1198
Al Viro357f4352016-04-08 19:05:19 -04001199 iterate_all_kinds(i, size, v,
1200 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1201 (size != v.iov_len ? size : 0), 0),
1202 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1203 (size != v.bv_len ? size : 0)),
1204 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1205 (size != v.iov_len ? size : 0))
1206 );
Al Viro33844e62016-12-21 21:55:02 -05001207 return res;
Al Viro357f4352016-04-08 19:05:19 -04001208}
1209EXPORT_SYMBOL(iov_iter_gap_alignment);
1210
Ilya Dryomove76b63122018-05-02 20:16:56 +02001211static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001212 size_t maxsize,
1213 struct page **pages,
1214 int idx,
1215 size_t *start)
1216{
1217 struct pipe_inode_info *pipe = i->pipe;
Al Viro1689c732016-10-11 18:21:14 +01001218 ssize_t n = push_pipe(i, maxsize, &idx, start);
Al Viro241699c2016-09-22 16:33:12 -04001219 if (!n)
1220 return -EFAULT;
1221
1222 maxsize = n;
1223 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001224 while (n > 0) {
Al Viro241699c2016-09-22 16:33:12 -04001225 get_page(*pages++ = pipe->bufs[idx].page);
1226 idx = next_idx(idx, pipe);
1227 n -= PAGE_SIZE;
1228 }
1229
1230 return maxsize;
1231}
1232
1233static ssize_t pipe_get_pages(struct iov_iter *i,
1234 struct page **pages, size_t maxsize, unsigned maxpages,
1235 size_t *start)
1236{
1237 unsigned npages;
1238 size_t capacity;
1239 int idx;
1240
Al Viro33844e62016-12-21 21:55:02 -05001241 if (!maxsize)
1242 return 0;
1243
Al Viro241699c2016-09-22 16:33:12 -04001244 if (!sanity(i))
1245 return -EFAULT;
1246
1247 data_start(i, &idx, start);
1248 /* some of this one + all after this one */
1249 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1250 capacity = min(npages,maxpages) * PAGE_SIZE - *start;
1251
1252 return __pipe_get_pages(i, min(maxsize, capacity), pages, idx, start);
1253}
1254
Al Viro62a80672014-04-04 23:12:29 -04001255ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001256 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001257 size_t *start)
1258{
Al Viroe5393fa2014-11-27 14:12:09 -05001259 if (maxsize > i->count)
1260 maxsize = i->count;
1261
David Howells00e23702018-10-22 13:07:28 +01001262 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001263 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001264 if (unlikely(iov_iter_is_discard(i)))
1265 return -EFAULT;
1266
Al Viroe5393fa2014-11-27 14:12:09 -05001267 iterate_all_kinds(i, maxsize, v, ({
1268 unsigned long addr = (unsigned long)v.iov_base;
1269 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1270 int n;
1271 int res;
1272
1273 if (len > maxpages * PAGE_SIZE)
1274 len = maxpages * PAGE_SIZE;
1275 addr &= ~(PAGE_SIZE - 1);
1276 n = DIV_ROUND_UP(len, PAGE_SIZE);
David Howells00e23702018-10-22 13:07:28 +01001277 res = get_user_pages_fast(addr, n, iov_iter_rw(i) != WRITE, pages);
Al Viroe5393fa2014-11-27 14:12:09 -05001278 if (unlikely(res < 0))
1279 return res;
1280 return (res == n ? len : res * PAGE_SIZE) - *start;
1281 0;}),({
1282 /* can't be more than PAGE_SIZE */
1283 *start = v.bv_offset;
1284 get_page(*pages = v.bv_page);
1285 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001286 }),({
1287 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001288 })
1289 )
1290 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001291}
1292EXPORT_SYMBOL(iov_iter_get_pages);
1293
Al Viro1b17f1f2014-11-27 14:14:31 -05001294static struct page **get_pages_array(size_t n)
1295{
Michal Hocko752ade62017-05-08 15:57:27 -07001296 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001297}
1298
Al Viro241699c2016-09-22 16:33:12 -04001299static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1300 struct page ***pages, size_t maxsize,
1301 size_t *start)
1302{
1303 struct page **p;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001304 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001305 int idx;
1306 int npages;
1307
Al Viro33844e62016-12-21 21:55:02 -05001308 if (!maxsize)
1309 return 0;
1310
Al Viro241699c2016-09-22 16:33:12 -04001311 if (!sanity(i))
1312 return -EFAULT;
1313
1314 data_start(i, &idx, start);
1315 /* some of this one + all after this one */
1316 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
1317 n = npages * PAGE_SIZE - *start;
1318 if (maxsize > n)
1319 maxsize = n;
1320 else
1321 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1322 p = get_pages_array(npages);
1323 if (!p)
1324 return -ENOMEM;
1325 n = __pipe_get_pages(i, maxsize, p, idx, start);
1326 if (n > 0)
1327 *pages = p;
1328 else
1329 kvfree(p);
1330 return n;
1331}
1332
Al Viro62a80672014-04-04 23:12:29 -04001333ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1334 struct page ***pages, size_t maxsize,
1335 size_t *start)
1336{
Al Viro1b17f1f2014-11-27 14:14:31 -05001337 struct page **p;
1338
1339 if (maxsize > i->count)
1340 maxsize = i->count;
1341
David Howells00e23702018-10-22 13:07:28 +01001342 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001343 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001344 if (unlikely(iov_iter_is_discard(i)))
1345 return -EFAULT;
1346
Al Viro1b17f1f2014-11-27 14:14:31 -05001347 iterate_all_kinds(i, maxsize, v, ({
1348 unsigned long addr = (unsigned long)v.iov_base;
1349 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1350 int n;
1351 int res;
1352
1353 addr &= ~(PAGE_SIZE - 1);
1354 n = DIV_ROUND_UP(len, PAGE_SIZE);
1355 p = get_pages_array(n);
1356 if (!p)
1357 return -ENOMEM;
David Howells00e23702018-10-22 13:07:28 +01001358 res = get_user_pages_fast(addr, n, iov_iter_rw(i) != WRITE, p);
Al Viro1b17f1f2014-11-27 14:14:31 -05001359 if (unlikely(res < 0)) {
1360 kvfree(p);
1361 return res;
1362 }
1363 *pages = p;
1364 return (res == n ? len : res * PAGE_SIZE) - *start;
1365 0;}),({
1366 /* can't be more than PAGE_SIZE */
1367 *start = v.bv_offset;
1368 *pages = p = get_pages_array(1);
1369 if (!p)
1370 return -ENOMEM;
1371 get_page(*p = v.bv_page);
1372 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001373 }),({
1374 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -05001375 })
1376 )
1377 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001378}
1379EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1380
Al Viroa604ec72014-11-24 01:08:00 -05001381size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1382 struct iov_iter *i)
1383{
1384 char *to = addr;
1385 __wsum sum, next;
1386 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001387 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001388 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001389 WARN_ON(1);
1390 return 0;
1391 }
Al Viroa604ec72014-11-24 01:08:00 -05001392 iterate_and_advance(i, bytes, v, ({
1393 int err = 0;
Al Virocbbd26b2016-11-01 22:09:04 -04001394 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001395 (to += v.iov_len) - v.iov_len,
1396 v.iov_len, 0, &err);
1397 if (!err) {
1398 sum = csum_block_add(sum, next, off);
1399 off += v.iov_len;
1400 }
1401 err ? v.iov_len : 0;
1402 }), ({
1403 char *p = kmap_atomic(v.bv_page);
1404 next = csum_partial_copy_nocheck(p + v.bv_offset,
1405 (to += v.bv_len) - v.bv_len,
1406 v.bv_len, 0);
1407 kunmap_atomic(p);
1408 sum = csum_block_add(sum, next, off);
1409 off += v.bv_len;
1410 }),({
1411 next = csum_partial_copy_nocheck(v.iov_base,
1412 (to += v.iov_len) - v.iov_len,
1413 v.iov_len, 0);
1414 sum = csum_block_add(sum, next, off);
1415 off += v.iov_len;
1416 })
1417 )
1418 *csum = sum;
1419 return bytes;
1420}
1421EXPORT_SYMBOL(csum_and_copy_from_iter);
1422
Al Virocbbd26b2016-11-01 22:09:04 -04001423bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1424 struct iov_iter *i)
1425{
1426 char *to = addr;
1427 __wsum sum, next;
1428 size_t off = 0;
1429 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001430 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001431 WARN_ON(1);
1432 return false;
1433 }
1434 if (unlikely(i->count < bytes))
1435 return false;
1436 iterate_all_kinds(i, bytes, v, ({
1437 int err = 0;
1438 next = csum_and_copy_from_user(v.iov_base,
1439 (to += v.iov_len) - v.iov_len,
1440 v.iov_len, 0, &err);
1441 if (err)
1442 return false;
1443 sum = csum_block_add(sum, next, off);
1444 off += v.iov_len;
1445 0;
1446 }), ({
1447 char *p = kmap_atomic(v.bv_page);
1448 next = csum_partial_copy_nocheck(p + v.bv_offset,
1449 (to += v.bv_len) - v.bv_len,
1450 v.bv_len, 0);
1451 kunmap_atomic(p);
1452 sum = csum_block_add(sum, next, off);
1453 off += v.bv_len;
1454 }),({
1455 next = csum_partial_copy_nocheck(v.iov_base,
1456 (to += v.iov_len) - v.iov_len,
1457 v.iov_len, 0);
1458 sum = csum_block_add(sum, next, off);
1459 off += v.iov_len;
1460 })
1461 )
1462 *csum = sum;
1463 iov_iter_advance(i, bytes);
1464 return true;
1465}
1466EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1467
Sagi Grimbergcb002d02018-12-03 17:52:07 -08001468size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csump,
Al Viroa604ec72014-11-24 01:08:00 -05001469 struct iov_iter *i)
1470{
Al Viro36f7a8a2015-12-06 16:49:22 -05001471 const char *from = addr;
Sagi Grimbergcb002d02018-12-03 17:52:07 -08001472 __wsum *csum = csump;
Al Viroa604ec72014-11-24 01:08:00 -05001473 __wsum sum, next;
1474 size_t off = 0;
Al Viro78e1f382018-11-25 16:24:16 -05001475
1476 if (unlikely(iov_iter_is_pipe(i)))
1477 return csum_and_copy_to_pipe_iter(addr, bytes, csum, i);
1478
Al Viroa604ec72014-11-24 01:08:00 -05001479 sum = *csum;
Al Viro78e1f382018-11-25 16:24:16 -05001480 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001481 WARN_ON(1); /* for now */
1482 return 0;
1483 }
Al Viroa604ec72014-11-24 01:08:00 -05001484 iterate_and_advance(i, bytes, v, ({
1485 int err = 0;
1486 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001487 v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001488 v.iov_len, 0, &err);
1489 if (!err) {
1490 sum = csum_block_add(sum, next, off);
1491 off += v.iov_len;
1492 }
1493 err ? v.iov_len : 0;
1494 }), ({
1495 char *p = kmap_atomic(v.bv_page);
1496 next = csum_partial_copy_nocheck((from += v.bv_len) - v.bv_len,
1497 p + v.bv_offset,
1498 v.bv_len, 0);
1499 kunmap_atomic(p);
1500 sum = csum_block_add(sum, next, off);
1501 off += v.bv_len;
1502 }),({
1503 next = csum_partial_copy_nocheck((from += v.iov_len) - v.iov_len,
1504 v.iov_base,
1505 v.iov_len, 0);
1506 sum = csum_block_add(sum, next, off);
1507 off += v.iov_len;
1508 })
1509 )
1510 *csum = sum;
1511 return bytes;
1512}
1513EXPORT_SYMBOL(csum_and_copy_to_iter);
1514
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001515size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1516 struct iov_iter *i)
1517{
1518 struct ahash_request *hash = hashp;
1519 struct scatterlist sg;
1520 size_t copied;
1521
1522 copied = copy_to_iter(addr, bytes, i);
1523 sg_init_one(&sg, addr, copied);
1524 ahash_request_set_crypt(hash, &sg, NULL, copied);
1525 crypto_ahash_update(hash);
1526 return copied;
1527}
1528EXPORT_SYMBOL(hash_and_copy_to_iter);
1529
Al Viro62a80672014-04-04 23:12:29 -04001530int iov_iter_npages(const struct iov_iter *i, int maxpages)
1531{
Al Viroe0f2dc42014-11-27 14:09:46 -05001532 size_t size = i->count;
1533 int npages = 0;
1534
1535 if (!size)
1536 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001537 if (unlikely(iov_iter_is_discard(i)))
1538 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001539
David Howells00e23702018-10-22 13:07:28 +01001540 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001541 struct pipe_inode_info *pipe = i->pipe;
1542 size_t off;
1543 int idx;
1544
1545 if (!sanity(i))
1546 return 0;
1547
1548 data_start(i, &idx, &off);
1549 /* some of this one + all after this one */
1550 npages = ((pipe->curbuf - idx - 1) & (pipe->buffers - 1)) + 1;
1551 if (npages >= maxpages)
1552 return maxpages;
1553 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001554 unsigned long p = (unsigned long)v.iov_base;
1555 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1556 - p / PAGE_SIZE;
1557 if (npages >= maxpages)
1558 return maxpages;
1559 0;}),({
1560 npages++;
1561 if (npages >= maxpages)
1562 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001563 }),({
1564 unsigned long p = (unsigned long)v.iov_base;
1565 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1566 - p / PAGE_SIZE;
1567 if (npages >= maxpages)
1568 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001569 })
1570 )
1571 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001572}
Al Virof67da302014-03-19 01:16:16 -04001573EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001574
1575const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1576{
1577 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001578 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001579 WARN_ON(1);
1580 return NULL;
1581 }
David Howells9ea9ce02018-10-20 00:57:56 +01001582 if (unlikely(iov_iter_is_discard(new)))
1583 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001584 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001585 return new->bvec = kmemdup(new->bvec,
1586 new->nr_segs * sizeof(struct bio_vec),
1587 flags);
1588 else
1589 /* iovec and kvec have identical layout */
1590 return new->iov = kmemdup(new->iov,
1591 new->nr_segs * sizeof(struct iovec),
1592 flags);
1593}
1594EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001595
Vegard Nossumffecee42016-10-08 11:18:07 +02001596/**
1597 * import_iovec() - Copy an array of &struct iovec from userspace
1598 * into the kernel, check that it is valid, and initialize a new
1599 * &struct iov_iter iterator to access it.
1600 *
1601 * @type: One of %READ or %WRITE.
1602 * @uvector: Pointer to the userspace array.
1603 * @nr_segs: Number of elements in userspace array.
1604 * @fast_segs: Number of elements in @iov.
1605 * @iov: (input and output parameter) Pointer to pointer to (usually small
1606 * on-stack) kernel array.
1607 * @i: Pointer to iterator that will be initialized on success.
1608 *
1609 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1610 * then this function places %NULL in *@iov on return. Otherwise, a new
1611 * array will be allocated and the result placed in *@iov. This means that
1612 * the caller may call kfree() on *@iov regardless of whether the small
1613 * on-stack array was used or not (and regardless of whether this function
1614 * returns an error or not).
1615 *
1616 * Return: 0 on success or negative error code on error.
1617 */
Al Virobc917be2015-03-21 17:45:43 -04001618int import_iovec(int type, const struct iovec __user * uvector,
1619 unsigned nr_segs, unsigned fast_segs,
1620 struct iovec **iov, struct iov_iter *i)
1621{
1622 ssize_t n;
1623 struct iovec *p;
1624 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1625 *iov, &p);
1626 if (n < 0) {
1627 if (p != *iov)
1628 kfree(p);
1629 *iov = NULL;
1630 return n;
1631 }
1632 iov_iter_init(i, type, p, nr_segs, n);
1633 *iov = p == *iov ? NULL : p;
1634 return 0;
1635}
1636EXPORT_SYMBOL(import_iovec);
1637
1638#ifdef CONFIG_COMPAT
1639#include <linux/compat.h>
1640
1641int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
1642 unsigned nr_segs, unsigned fast_segs,
1643 struct iovec **iov, struct iov_iter *i)
1644{
1645 ssize_t n;
1646 struct iovec *p;
1647 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1648 *iov, &p);
1649 if (n < 0) {
1650 if (p != *iov)
1651 kfree(p);
1652 *iov = NULL;
1653 return n;
1654 }
1655 iov_iter_init(i, type, p, nr_segs, n);
1656 *iov = p == *iov ? NULL : p;
1657 return 0;
1658}
1659#endif
1660
1661int import_single_range(int rw, void __user *buf, size_t len,
1662 struct iovec *iov, struct iov_iter *i)
1663{
1664 if (len > MAX_RW_COUNT)
1665 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001666 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04001667 return -EFAULT;
1668
1669 iov->iov_base = buf;
1670 iov->iov_len = len;
1671 iov_iter_init(i, rw, iov, 1, len);
1672 return 0;
1673}
Al Viroe1267582015-12-06 20:38:56 -05001674EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05001675
1676int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1677 int (*f)(struct kvec *vec, void *context),
1678 void *context)
1679{
1680 struct kvec w;
1681 int err = -EINVAL;
1682 if (!bytes)
1683 return 0;
1684
1685 iterate_all_kinds(i, bytes, v, -EINVAL, ({
1686 w.iov_base = kmap(v.bv_page) + v.bv_offset;
1687 w.iov_len = v.bv_len;
1688 err = f(&w, context);
1689 kunmap(v.bv_page);
1690 err;}), ({
1691 w = v;
1692 err = f(&w, context);})
1693 )
1694 return err;
1695}
1696EXPORT_SYMBOL(iov_iter_for_each_range);