blob: bf538c2bec777f4efde6ee468f475aa400c06e0b [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Al Viro4f18cd32014-02-05 19:11:33 -05002#include <linux/export.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -06003#include <linux/bvec.h>
Al Viro4f18cd32014-02-05 19:11:33 -05004#include <linux/uio.h>
5#include <linux/pagemap.h>
Al Viro91f79c42014-03-21 04:58:33 -04006#include <linux/slab.h>
7#include <linux/vmalloc.h>
Al Viro241699c2016-09-22 16:33:12 -04008#include <linux/splice.h>
Al Viroa604ec72014-11-24 01:08:00 -05009#include <net/checksum.h>
Sagi Grimbergd05f4432018-12-03 17:52:09 -080010#include <linux/scatterlist.h>
Marco Elverd0ef4c32020-01-21 17:05:11 +010011#include <linux/instrumented.h>
Al Viro4f18cd32014-02-05 19:11:33 -050012
Al Viro241699c2016-09-22 16:33:12 -040013#define PIPE_PARANOIA /* for now */
14
Al Viro04a31162014-11-27 13:51:41 -050015#define iterate_iovec(i, n, __v, __p, skip, STEP) { \
16 size_t left; \
17 size_t wanted = n; \
18 __p = i->iov; \
19 __v.iov_len = min(n, __p->iov_len - skip); \
20 if (likely(__v.iov_len)) { \
21 __v.iov_base = __p->iov_base + skip; \
22 left = (STEP); \
23 __v.iov_len -= left; \
24 skip += __v.iov_len; \
25 n -= __v.iov_len; \
26 } else { \
27 left = 0; \
28 } \
29 while (unlikely(!left && n)) { \
30 __p++; \
31 __v.iov_len = min(n, __p->iov_len); \
32 if (unlikely(!__v.iov_len)) \
33 continue; \
34 __v.iov_base = __p->iov_base; \
35 left = (STEP); \
36 __v.iov_len -= left; \
37 skip = __v.iov_len; \
38 n -= __v.iov_len; \
39 } \
40 n = wanted - n; \
41}
42
Al Viroa2804552014-11-27 14:48:42 -050043#define iterate_kvec(i, n, __v, __p, skip, STEP) { \
44 size_t wanted = n; \
45 __p = i->kvec; \
46 __v.iov_len = min(n, __p->iov_len - skip); \
47 if (likely(__v.iov_len)) { \
48 __v.iov_base = __p->iov_base + skip; \
49 (void)(STEP); \
50 skip += __v.iov_len; \
51 n -= __v.iov_len; \
52 } \
53 while (unlikely(n)) { \
54 __p++; \
55 __v.iov_len = min(n, __p->iov_len); \
56 if (unlikely(!__v.iov_len)) \
57 continue; \
58 __v.iov_base = __p->iov_base; \
59 (void)(STEP); \
60 skip = __v.iov_len; \
61 n -= __v.iov_len; \
62 } \
63 n = wanted; \
64}
65
Ming Lei1bdc76a2016-05-30 21:34:32 +080066#define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
67 struct bvec_iter __start; \
68 __start.bi_size = n; \
69 __start.bi_bvec_done = skip; \
70 __start.bi_idx = 0; \
71 for_each_bvec(__v, i->bvec, __bi, __start) { \
72 if (!__v.bv_len) \
Al Viro04a31162014-11-27 13:51:41 -050073 continue; \
Al Viro04a31162014-11-27 13:51:41 -050074 (void)(STEP); \
Al Viro04a31162014-11-27 13:51:41 -050075 } \
Al Viro04a31162014-11-27 13:51:41 -050076}
77
Al Viroa2804552014-11-27 14:48:42 -050078#define iterate_all_kinds(i, n, v, I, B, K) { \
Al Viro33844e62016-12-21 21:55:02 -050079 if (likely(n)) { \
80 size_t skip = i->iov_offset; \
81 if (unlikely(i->type & ITER_BVEC)) { \
82 struct bio_vec v; \
83 struct bvec_iter __bi; \
84 iterate_bvec(i, n, v, __bi, skip, (B)) \
85 } else if (unlikely(i->type & ITER_KVEC)) { \
86 const struct kvec *kvec; \
87 struct kvec v; \
88 iterate_kvec(i, n, v, kvec, skip, (K)) \
David Howells9ea9ce02018-10-20 00:57:56 +010089 } else if (unlikely(i->type & ITER_DISCARD)) { \
Al Viro33844e62016-12-21 21:55:02 -050090 } else { \
91 const struct iovec *iov; \
92 struct iovec v; \
93 iterate_iovec(i, n, v, iov, skip, (I)) \
94 } \
Al Viro04a31162014-11-27 13:51:41 -050095 } \
96}
97
Al Viroa2804552014-11-27 14:48:42 -050098#define iterate_and_advance(i, n, v, I, B, K) { \
Al Virodd254f52016-05-09 11:54:48 -040099 if (unlikely(i->count < n)) \
100 n = i->count; \
Al Viro19f18452016-05-25 17:36:19 -0400101 if (i->count) { \
Al Virodd254f52016-05-09 11:54:48 -0400102 size_t skip = i->iov_offset; \
103 if (unlikely(i->type & ITER_BVEC)) { \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800104 const struct bio_vec *bvec = i->bvec; \
Al Virodd254f52016-05-09 11:54:48 -0400105 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800106 struct bvec_iter __bi; \
107 iterate_bvec(i, n, v, __bi, skip, (B)) \
108 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
109 i->nr_segs -= i->bvec - bvec; \
110 skip = __bi.bi_bvec_done; \
Al Virodd254f52016-05-09 11:54:48 -0400111 } else if (unlikely(i->type & ITER_KVEC)) { \
112 const struct kvec *kvec; \
113 struct kvec v; \
114 iterate_kvec(i, n, v, kvec, skip, (K)) \
115 if (skip == kvec->iov_len) { \
116 kvec++; \
117 skip = 0; \
118 } \
119 i->nr_segs -= kvec - i->kvec; \
120 i->kvec = kvec; \
David Howells9ea9ce02018-10-20 00:57:56 +0100121 } else if (unlikely(i->type & ITER_DISCARD)) { \
122 skip += n; \
Al Virodd254f52016-05-09 11:54:48 -0400123 } else { \
124 const struct iovec *iov; \
125 struct iovec v; \
126 iterate_iovec(i, n, v, iov, skip, (I)) \
127 if (skip == iov->iov_len) { \
128 iov++; \
129 skip = 0; \
130 } \
131 i->nr_segs -= iov - i->iov; \
132 i->iov = iov; \
Al Viro7ce2a912014-11-27 13:59:45 -0500133 } \
Al Virodd254f52016-05-09 11:54:48 -0400134 i->count -= n; \
135 i->iov_offset = skip; \
Al Viro7ce2a912014-11-27 13:59:45 -0500136 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500137}
138
Al Viro09fc68dc2017-06-29 22:25:14 -0400139static int copyout(void __user *to, const void *from, size_t n)
140{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800141 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100142 instrument_copy_to_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400143 n = raw_copy_to_user(to, from, n);
144 }
145 return n;
146}
147
148static int copyin(void *to, const void __user *from, size_t n)
149{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800150 if (access_ok(from, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100151 instrument_copy_from_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400152 n = raw_copy_from_user(to, from, n);
153 }
154 return n;
155}
156
Al Viro62a80672014-04-04 23:12:29 -0400157static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500158 struct iov_iter *i)
159{
160 size_t skip, copy, left, wanted;
161 const struct iovec *iov;
162 char __user *buf;
163 void *kaddr, *from;
164
165 if (unlikely(bytes > i->count))
166 bytes = i->count;
167
168 if (unlikely(!bytes))
169 return 0;
170
Al Viro09fc68dc2017-06-29 22:25:14 -0400171 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500172 wanted = bytes;
173 iov = i->iov;
174 skip = i->iov_offset;
175 buf = iov->iov_base + skip;
176 copy = min(bytes, iov->iov_len - skip);
177
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700178 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500179 kaddr = kmap_atomic(page);
180 from = kaddr + offset;
181
182 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400183 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500184 copy -= left;
185 skip += copy;
186 from += copy;
187 bytes -= copy;
188
189 while (unlikely(!left && bytes)) {
190 iov++;
191 buf = iov->iov_base;
192 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400193 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500194 copy -= left;
195 skip = copy;
196 from += copy;
197 bytes -= copy;
198 }
199 if (likely(!bytes)) {
200 kunmap_atomic(kaddr);
201 goto done;
202 }
203 offset = from - kaddr;
204 buf += copy;
205 kunmap_atomic(kaddr);
206 copy = min(bytes, iov->iov_len - skip);
207 }
208 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700209
Al Viro4f18cd32014-02-05 19:11:33 -0500210 kaddr = kmap(page);
211 from = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400212 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500213 copy -= left;
214 skip += copy;
215 from += copy;
216 bytes -= copy;
217 while (unlikely(!left && bytes)) {
218 iov++;
219 buf = iov->iov_base;
220 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400221 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500222 copy -= left;
223 skip = copy;
224 from += copy;
225 bytes -= copy;
226 }
227 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700228
Al Viro4f18cd32014-02-05 19:11:33 -0500229done:
Al Viro81055e52014-04-04 19:23:46 -0400230 if (skip == iov->iov_len) {
231 iov++;
232 skip = 0;
233 }
Al Viro4f18cd32014-02-05 19:11:33 -0500234 i->count -= wanted - bytes;
235 i->nr_segs -= iov - i->iov;
236 i->iov = iov;
237 i->iov_offset = skip;
238 return wanted - bytes;
239}
Al Viro4f18cd32014-02-05 19:11:33 -0500240
Al Viro62a80672014-04-04 23:12:29 -0400241static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400242 struct iov_iter *i)
243{
244 size_t skip, copy, left, wanted;
245 const struct iovec *iov;
246 char __user *buf;
247 void *kaddr, *to;
248
249 if (unlikely(bytes > i->count))
250 bytes = i->count;
251
252 if (unlikely(!bytes))
253 return 0;
254
Al Viro09fc68dc2017-06-29 22:25:14 -0400255 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400256 wanted = bytes;
257 iov = i->iov;
258 skip = i->iov_offset;
259 buf = iov->iov_base + skip;
260 copy = min(bytes, iov->iov_len - skip);
261
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700262 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400263 kaddr = kmap_atomic(page);
264 to = kaddr + offset;
265
266 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400267 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400268 copy -= left;
269 skip += copy;
270 to += copy;
271 bytes -= copy;
272
273 while (unlikely(!left && bytes)) {
274 iov++;
275 buf = iov->iov_base;
276 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400277 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400278 copy -= left;
279 skip = copy;
280 to += copy;
281 bytes -= copy;
282 }
283 if (likely(!bytes)) {
284 kunmap_atomic(kaddr);
285 goto done;
286 }
287 offset = to - kaddr;
288 buf += copy;
289 kunmap_atomic(kaddr);
290 copy = min(bytes, iov->iov_len - skip);
291 }
292 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700293
Al Virof0d1bec2014-04-03 15:05:18 -0400294 kaddr = kmap(page);
295 to = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400296 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400297 copy -= left;
298 skip += copy;
299 to += copy;
300 bytes -= copy;
301 while (unlikely(!left && bytes)) {
302 iov++;
303 buf = iov->iov_base;
304 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400305 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400306 copy -= left;
307 skip = copy;
308 to += copy;
309 bytes -= copy;
310 }
311 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700312
Al Virof0d1bec2014-04-03 15:05:18 -0400313done:
Al Viro81055e52014-04-04 19:23:46 -0400314 if (skip == iov->iov_len) {
315 iov++;
316 skip = 0;
317 }
Al Virof0d1bec2014-04-03 15:05:18 -0400318 i->count -= wanted - bytes;
319 i->nr_segs -= iov - i->iov;
320 i->iov = iov;
321 i->iov_offset = skip;
322 return wanted - bytes;
323}
Al Virof0d1bec2014-04-03 15:05:18 -0400324
Al Viro241699c2016-09-22 16:33:12 -0400325#ifdef PIPE_PARANOIA
326static bool sanity(const struct iov_iter *i)
327{
328 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000329 unsigned int p_head = pipe->head;
330 unsigned int p_tail = pipe->tail;
331 unsigned int p_mask = pipe->ring_size - 1;
332 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
333 unsigned int i_head = i->head;
334 unsigned int idx;
335
Al Viro241699c2016-09-22 16:33:12 -0400336 if (i->iov_offset) {
337 struct pipe_buffer *p;
David Howells8cefc102019-11-15 13:30:32 +0000338 if (unlikely(p_occupancy == 0))
Al Viro241699c2016-09-22 16:33:12 -0400339 goto Bad; // pipe must be non-empty
David Howells8cefc102019-11-15 13:30:32 +0000340 if (unlikely(i_head != p_head - 1))
Al Viro241699c2016-09-22 16:33:12 -0400341 goto Bad; // must be at the last buffer...
342
David Howells8cefc102019-11-15 13:30:32 +0000343 p = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400344 if (unlikely(p->offset + p->len != i->iov_offset))
345 goto Bad; // ... at the end of segment
346 } else {
David Howells8cefc102019-11-15 13:30:32 +0000347 if (i_head != p_head)
Al Viro241699c2016-09-22 16:33:12 -0400348 goto Bad; // must be right after the last buffer
349 }
350 return true;
351Bad:
David Howells8cefc102019-11-15 13:30:32 +0000352 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
353 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
354 p_head, p_tail, pipe->ring_size);
355 for (idx = 0; idx < pipe->ring_size; idx++)
Al Viro241699c2016-09-22 16:33:12 -0400356 printk(KERN_ERR "[%p %p %d %d]\n",
357 pipe->bufs[idx].ops,
358 pipe->bufs[idx].page,
359 pipe->bufs[idx].offset,
360 pipe->bufs[idx].len);
361 WARN_ON(1);
362 return false;
363}
364#else
365#define sanity(i) true
366#endif
367
Al Viro241699c2016-09-22 16:33:12 -0400368static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
369 struct iov_iter *i)
370{
371 struct pipe_inode_info *pipe = i->pipe;
372 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +0000373 unsigned int p_tail = pipe->tail;
374 unsigned int p_mask = pipe->ring_size - 1;
375 unsigned int i_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400376 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400377
378 if (unlikely(bytes > i->count))
379 bytes = i->count;
380
381 if (unlikely(!bytes))
382 return 0;
383
384 if (!sanity(i))
385 return 0;
386
387 off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000388 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400389 if (off) {
390 if (offset == off && buf->page == page) {
391 /* merge with the last one */
392 buf->len += bytes;
393 i->iov_offset += bytes;
394 goto out;
395 }
David Howells8cefc102019-11-15 13:30:32 +0000396 i_head++;
397 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400398 }
David Howells6718b6f2019-10-16 16:47:32 +0100399 if (pipe_full(i_head, p_tail, pipe->max_usage))
Al Viro241699c2016-09-22 16:33:12 -0400400 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000401
Al Viro241699c2016-09-22 16:33:12 -0400402 buf->ops = &page_cache_pipe_buf_ops;
David Howells8cefc102019-11-15 13:30:32 +0000403 get_page(page);
404 buf->page = page;
Al Viro241699c2016-09-22 16:33:12 -0400405 buf->offset = offset;
406 buf->len = bytes;
David Howells8cefc102019-11-15 13:30:32 +0000407
408 pipe->head = i_head + 1;
Al Viro241699c2016-09-22 16:33:12 -0400409 i->iov_offset = offset + bytes;
David Howells8cefc102019-11-15 13:30:32 +0000410 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400411out:
412 i->count -= bytes;
413 return bytes;
414}
415
Al Viro4f18cd32014-02-05 19:11:33 -0500416/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400417 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
418 * bytes. For each iovec, fault in each page that constitutes the iovec.
419 *
420 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
421 * because it is an invalid address).
422 */
Al Virod4690f12016-09-16 00:11:45 +0100423int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400424{
425 size_t skip = i->iov_offset;
426 const struct iovec *iov;
427 int err;
428 struct iovec v;
429
430 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
431 iterate_iovec(i, bytes, v, iov, skip, ({
Al Viro4bce9f6e2016-09-17 18:02:44 -0400432 err = fault_in_pages_readable(v.iov_base, v.iov_len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400433 if (unlikely(err))
434 return err;
435 0;}))
436 }
437 return 0;
438}
Al Virod4690f12016-09-16 00:11:45 +0100439EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400440
David Howellsaa563d72018-10-20 00:57:56 +0100441void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500442 const struct iovec *iov, unsigned long nr_segs,
443 size_t count)
444{
David Howellsaa563d72018-10-20 00:57:56 +0100445 WARN_ON(direction & ~(READ | WRITE));
446 direction &= READ | WRITE;
447
Al Viro71d8e532014-03-05 19:28:09 -0500448 /* It will get better. Eventually... */
Al Virodb68ce12017-03-20 21:08:07 -0400449 if (uaccess_kernel()) {
David Howellsaa563d72018-10-20 00:57:56 +0100450 i->type = ITER_KVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500451 i->kvec = (struct kvec *)iov;
452 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100453 i->type = ITER_IOVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500454 i->iov = iov;
455 }
Al Viro71d8e532014-03-05 19:28:09 -0500456 i->nr_segs = nr_segs;
457 i->iov_offset = 0;
458 i->count = count;
459}
460EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400461
Al Viro62a80672014-04-04 23:12:29 -0400462static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
463{
464 char *from = kmap_atomic(page);
465 memcpy(to, from + offset, len);
466 kunmap_atomic(from);
467}
468
Al Viro36f7a8a2015-12-06 16:49:22 -0500469static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
Al Viro62a80672014-04-04 23:12:29 -0400470{
471 char *to = kmap_atomic(page);
472 memcpy(to + offset, from, len);
473 kunmap_atomic(to);
474}
475
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400476static void memzero_page(struct page *page, size_t offset, size_t len)
477{
478 char *addr = kmap_atomic(page);
479 memset(addr + offset, 0, len);
480 kunmap_atomic(addr);
481}
482
Al Viro241699c2016-09-22 16:33:12 -0400483static inline bool allocated(struct pipe_buffer *buf)
484{
485 return buf->ops == &default_pipe_buf_ops;
486}
487
David Howells8cefc102019-11-15 13:30:32 +0000488static inline void data_start(const struct iov_iter *i,
489 unsigned int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400490{
David Howells8cefc102019-11-15 13:30:32 +0000491 unsigned int p_mask = i->pipe->ring_size - 1;
492 unsigned int iter_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400493 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000494
495 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
496 off == PAGE_SIZE)) {
497 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400498 off = 0;
499 }
David Howells8cefc102019-11-15 13:30:32 +0000500 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400501 *offp = off;
502}
503
504static size_t push_pipe(struct iov_iter *i, size_t size,
David Howells8cefc102019-11-15 13:30:32 +0000505 int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400506{
507 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000508 unsigned int p_tail = pipe->tail;
509 unsigned int p_mask = pipe->ring_size - 1;
510 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400511 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400512 ssize_t left;
513
514 if (unlikely(size > i->count))
515 size = i->count;
516 if (unlikely(!size))
517 return 0;
518
519 left = size;
David Howells8cefc102019-11-15 13:30:32 +0000520 data_start(i, &iter_head, &off);
521 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400522 *offp = off;
523 if (off) {
524 left -= PAGE_SIZE - off;
525 if (left <= 0) {
David Howells8cefc102019-11-15 13:30:32 +0000526 pipe->bufs[iter_head & p_mask].len += size;
Al Viro241699c2016-09-22 16:33:12 -0400527 return size;
528 }
David Howells8cefc102019-11-15 13:30:32 +0000529 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
530 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400531 }
David Howells6718b6f2019-10-16 16:47:32 +0100532 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000533 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400534 struct page *page = alloc_page(GFP_USER);
535 if (!page)
536 break;
David Howells8cefc102019-11-15 13:30:32 +0000537
538 buf->ops = &default_pipe_buf_ops;
539 buf->page = page;
540 buf->offset = 0;
541 buf->len = min_t(ssize_t, left, PAGE_SIZE);
542 left -= buf->len;
543 iter_head++;
544 pipe->head = iter_head;
545
546 if (left == 0)
Al Viro241699c2016-09-22 16:33:12 -0400547 return size;
Al Viro241699c2016-09-22 16:33:12 -0400548 }
549 return size - left;
550}
551
552static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
553 struct iov_iter *i)
554{
555 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000556 unsigned int p_mask = pipe->ring_size - 1;
557 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400558 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400559
560 if (!sanity(i))
561 return 0;
562
David Howells8cefc102019-11-15 13:30:32 +0000563 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400564 if (unlikely(!n))
565 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000566 do {
Al Viro241699c2016-09-22 16:33:12 -0400567 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000568 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
569 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400570 i->iov_offset = off + chunk;
571 n -= chunk;
572 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000573 off = 0;
574 i_head++;
575 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400576 i->count -= bytes;
577 return bytes;
578}
579
Al Virof9152892018-11-27 22:32:59 -0500580static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
581 __wsum sum, size_t off)
582{
583 __wsum next = csum_partial_copy_nocheck(from, to, len, 0);
584 return csum_block_add(sum, next, off);
585}
586
Al Viro78e1f382018-11-25 16:24:16 -0500587static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
588 __wsum *csum, struct iov_iter *i)
589{
590 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000591 unsigned int p_mask = pipe->ring_size - 1;
592 unsigned int i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500593 size_t n, r;
594 size_t off = 0;
Al Virof9152892018-11-27 22:32:59 -0500595 __wsum sum = *csum;
Al Viro78e1f382018-11-25 16:24:16 -0500596
597 if (!sanity(i))
598 return 0;
599
David Howells8cefc102019-11-15 13:30:32 +0000600 bytes = n = push_pipe(i, bytes, &i_head, &r);
Al Viro78e1f382018-11-25 16:24:16 -0500601 if (unlikely(!n))
602 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000603 do {
Al Viro78e1f382018-11-25 16:24:16 -0500604 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
David Howells8cefc102019-11-15 13:30:32 +0000605 char *p = kmap_atomic(pipe->bufs[i_head & p_mask].page);
Al Virof9152892018-11-27 22:32:59 -0500606 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
Al Viro78e1f382018-11-25 16:24:16 -0500607 kunmap_atomic(p);
David Howells8cefc102019-11-15 13:30:32 +0000608 i->head = i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500609 i->iov_offset = r + chunk;
610 n -= chunk;
611 off += chunk;
612 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000613 r = 0;
614 i_head++;
615 } while (n);
Al Viro78e1f382018-11-25 16:24:16 -0500616 i->count -= bytes;
617 *csum = sum;
618 return bytes;
619}
620
Al Viroaa28de22017-06-29 21:45:10 -0400621size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400622{
Al Viro36f7a8a2015-12-06 16:49:22 -0500623 const char *from = addr;
David Howells00e23702018-10-22 13:07:28 +0100624 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400625 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400626 if (iter_is_iovec(i))
627 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500628 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400629 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500630 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500631 (from += v.bv_len) - v.bv_len, v.bv_len),
632 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500633 )
Al Viro62a80672014-04-04 23:12:29 -0400634
Al Viro3d4d3e42014-11-27 14:28:06 -0500635 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400636}
Al Viroaa28de22017-06-29 21:45:10 -0400637EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400638
Dan Williams87803562018-05-03 17:06:31 -0700639#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
640static int copyout_mcsafe(void __user *to, const void *from, size_t n)
641{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800642 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100643 instrument_copy_to_user(to, from, n);
Dan Williams87803562018-05-03 17:06:31 -0700644 n = copy_to_user_mcsafe((__force void *) to, from, n);
645 }
646 return n;
647}
648
649static unsigned long memcpy_mcsafe_to_page(struct page *page, size_t offset,
650 const char *from, size_t len)
651{
652 unsigned long ret;
653 char *to;
654
655 to = kmap_atomic(page);
656 ret = memcpy_mcsafe(to + offset, from, len);
657 kunmap_atomic(to);
658
659 return ret;
660}
661
Dan Williamsca146f62018-07-08 13:46:12 -0700662static size_t copy_pipe_to_iter_mcsafe(const void *addr, size_t bytes,
663 struct iov_iter *i)
664{
665 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000666 unsigned int p_mask = pipe->ring_size - 1;
667 unsigned int i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700668 size_t n, off, xfer = 0;
Dan Williamsca146f62018-07-08 13:46:12 -0700669
670 if (!sanity(i))
671 return 0;
672
David Howells8cefc102019-11-15 13:30:32 +0000673 bytes = n = push_pipe(i, bytes, &i_head, &off);
Dan Williamsca146f62018-07-08 13:46:12 -0700674 if (unlikely(!n))
675 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000676 do {
Dan Williamsca146f62018-07-08 13:46:12 -0700677 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
678 unsigned long rem;
679
David Howells8cefc102019-11-15 13:30:32 +0000680 rem = memcpy_mcsafe_to_page(pipe->bufs[i_head & p_mask].page,
681 off, addr, chunk);
682 i->head = i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700683 i->iov_offset = off + chunk - rem;
684 xfer += chunk - rem;
685 if (rem)
686 break;
687 n -= chunk;
688 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000689 off = 0;
690 i_head++;
691 } while (n);
Dan Williamsca146f62018-07-08 13:46:12 -0700692 i->count -= xfer;
693 return xfer;
694}
695
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700696/**
697 * _copy_to_iter_mcsafe - copy to user with source-read error exception handling
698 * @addr: source kernel address
699 * @bytes: total transfer length
700 * @iter: destination iterator
701 *
702 * The pmem driver arranges for filesystem-dax to use this facility via
703 * dax_copy_to_iter() for protecting read/write to persistent memory.
704 * Unless / until an architecture can guarantee identical performance
705 * between _copy_to_iter_mcsafe() and _copy_to_iter() it would be a
706 * performance regression to switch more users to the mcsafe version.
707 *
708 * Otherwise, the main differences between this and typical _copy_to_iter().
709 *
710 * * Typical tail/residue handling after a fault retries the copy
711 * byte-by-byte until the fault happens again. Re-triggering machine
712 * checks is potentially fatal so the implementation uses source
713 * alignment and poison alignment assumptions to avoid re-triggering
714 * hardware exceptions.
715 *
716 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
717 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
718 * a short copy.
719 *
720 * See MCSAFE_TEST for self-test.
721 */
Dan Williams87803562018-05-03 17:06:31 -0700722size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i)
723{
724 const char *from = addr;
725 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
726
David Howells00e23702018-10-22 13:07:28 +0100727 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsca146f62018-07-08 13:46:12 -0700728 return copy_pipe_to_iter_mcsafe(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700729 if (iter_is_iovec(i))
730 might_fault();
731 iterate_and_advance(i, bytes, v,
732 copyout_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
733 ({
734 rem = memcpy_mcsafe_to_page(v.bv_page, v.bv_offset,
735 (from += v.bv_len) - v.bv_len, v.bv_len);
736 if (rem) {
737 curr_addr = (unsigned long) from;
738 bytes = curr_addr - s_addr - rem;
739 return bytes;
740 }
741 }),
742 ({
743 rem = memcpy_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len,
744 v.iov_len);
745 if (rem) {
746 curr_addr = (unsigned long) from;
747 bytes = curr_addr - s_addr - rem;
748 return bytes;
749 }
750 })
751 )
752
753 return bytes;
754}
755EXPORT_SYMBOL_GPL(_copy_to_iter_mcsafe);
756#endif /* CONFIG_ARCH_HAS_UACCESS_MCSAFE */
757
Al Viroaa28de22017-06-29 21:45:10 -0400758size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400759{
Al Viro0dbca9a2014-11-27 14:26:43 -0500760 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100761 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400762 WARN_ON(1);
763 return 0;
764 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400765 if (iter_is_iovec(i))
766 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500767 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400768 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500769 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500770 v.bv_offset, v.bv_len),
771 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500772 )
773
774 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400775}
Al Viroaa28de22017-06-29 21:45:10 -0400776EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400777
Al Viroaa28de22017-06-29 21:45:10 -0400778bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400779{
780 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100781 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400782 WARN_ON(1);
783 return false;
784 }
Al Viro33844e62016-12-21 21:55:02 -0500785 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400786 return false;
787
Al Viro09fc68dc2017-06-29 22:25:14 -0400788 if (iter_is_iovec(i))
789 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400790 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68dc2017-06-29 22:25:14 -0400791 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400792 v.iov_base, v.iov_len))
793 return false;
794 0;}),
795 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
796 v.bv_offset, v.bv_len),
797 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
798 )
799
800 iov_iter_advance(i, bytes);
801 return true;
802}
Al Viroaa28de22017-06-29 21:45:10 -0400803EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400804
Al Viroaa28de22017-06-29 21:45:10 -0400805size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500806{
807 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100808 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400809 WARN_ON(1);
810 return 0;
811 }
Al Viroaa583092014-11-27 20:27:08 -0500812 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400813 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500814 v.iov_base, v.iov_len),
815 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
816 v.bv_offset, v.bv_len),
817 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
818 )
819
820 return bytes;
821}
Al Viroaa28de22017-06-29 21:45:10 -0400822EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500823
Dan Williams0aed55a2017-05-29 12:22:50 -0700824#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700825/**
826 * _copy_from_iter_flushcache - write destination through cpu cache
827 * @addr: destination kernel address
828 * @bytes: total transfer length
829 * @iter: source iterator
830 *
831 * The pmem driver arranges for filesystem-dax to use this facility via
832 * dax_copy_from_iter() for ensuring that writes to persistent memory
833 * are flushed through the CPU cache. It is differentiated from
834 * _copy_from_iter_nocache() in that guarantees all data is flushed for
835 * all iterator types. The _copy_from_iter_nocache() only attempts to
836 * bypass the cache for the ITER_IOVEC case, and on some archs may use
837 * instructions that strand dirty-data in the cache.
838 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700839size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700840{
841 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100842 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700843 WARN_ON(1);
844 return 0;
845 }
846 iterate_and_advance(i, bytes, v,
847 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
848 v.iov_base, v.iov_len),
849 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
850 v.bv_offset, v.bv_len),
851 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
852 v.iov_len)
853 )
854
855 return bytes;
856}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700857EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700858#endif
859
Al Viroaa28de22017-06-29 21:45:10 -0400860bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400861{
862 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100863 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400864 WARN_ON(1);
865 return false;
866 }
Al Viro33844e62016-12-21 21:55:02 -0500867 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400868 return false;
869 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400870 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400871 v.iov_base, v.iov_len))
872 return false;
873 0;}),
874 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
875 v.bv_offset, v.bv_len),
876 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
877 )
878
879 iov_iter_advance(i, bytes);
880 return true;
881}
Al Viroaa28de22017-06-29 21:45:10 -0400882EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400883
Al Viro72e809e2017-06-29 21:52:57 -0400884static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
885{
Eric Dumazet6daef952019-02-26 10:42:39 -0800886 struct page *head;
887 size_t v = n + offset;
888
889 /*
890 * The general case needs to access the page order in order
891 * to compute the page size.
892 * However, we mostly deal with order-0 pages and thus can
893 * avoid a possible cache line miss for requests that fit all
894 * page orders.
895 */
896 if (n <= v && v <= PAGE_SIZE)
897 return true;
898
899 head = compound_head(page);
900 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700901
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700902 if (likely(n <= v && v <= (page_size(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400903 return true;
904 WARN_ON(1);
905 return false;
906}
Al Virod2715242014-11-27 14:22:37 -0500907
908size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
909 struct iov_iter *i)
910{
Al Viro72e809e2017-06-29 21:52:57 -0400911 if (unlikely(!page_copy_sane(page, offset, bytes)))
912 return 0;
Al Virod2715242014-11-27 14:22:37 -0500913 if (i->type & (ITER_BVEC|ITER_KVEC)) {
914 void *kaddr = kmap_atomic(page);
915 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
916 kunmap_atomic(kaddr);
917 return wanted;
David Howells9ea9ce02018-10-20 00:57:56 +0100918 } else if (unlikely(iov_iter_is_discard(i)))
919 return bytes;
920 else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500921 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400922 else
923 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500924}
925EXPORT_SYMBOL(copy_page_to_iter);
926
927size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
928 struct iov_iter *i)
929{
Al Viro72e809e2017-06-29 21:52:57 -0400930 if (unlikely(!page_copy_sane(page, offset, bytes)))
931 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +0100932 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400933 WARN_ON(1);
934 return 0;
935 }
Al Virod2715242014-11-27 14:22:37 -0500936 if (i->type & (ITER_BVEC|ITER_KVEC)) {
937 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400938 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500939 kunmap_atomic(kaddr);
940 return wanted;
941 } else
942 return copy_page_from_iter_iovec(page, offset, bytes, i);
943}
944EXPORT_SYMBOL(copy_page_from_iter);
945
Al Viro241699c2016-09-22 16:33:12 -0400946static size_t pipe_zero(size_t bytes, struct iov_iter *i)
947{
948 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000949 unsigned int p_mask = pipe->ring_size - 1;
950 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400951 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400952
953 if (!sanity(i))
954 return 0;
955
David Howells8cefc102019-11-15 13:30:32 +0000956 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400957 if (unlikely(!n))
958 return 0;
959
David Howells8cefc102019-11-15 13:30:32 +0000960 do {
Al Viro241699c2016-09-22 16:33:12 -0400961 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000962 memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk);
963 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400964 i->iov_offset = off + chunk;
965 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000966 off = 0;
967 i_head++;
968 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400969 i->count -= bytes;
970 return bytes;
971}
972
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400973size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
974{
David Howells00e23702018-10-22 13:07:28 +0100975 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400976 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500977 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400978 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500979 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
980 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500981 )
982
983 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400984}
985EXPORT_SYMBOL(iov_iter_zero);
986
Al Viro62a80672014-04-04 23:12:29 -0400987size_t iov_iter_copy_from_user_atomic(struct page *page,
988 struct iov_iter *i, unsigned long offset, size_t bytes)
989{
Al Viro04a31162014-11-27 13:51:41 -0500990 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400991 if (unlikely(!page_copy_sane(page, offset, bytes))) {
992 kunmap_atomic(kaddr);
993 return 0;
994 }
David Howells9ea9ce02018-10-20 00:57:56 +0100995 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400996 kunmap_atomic(kaddr);
997 WARN_ON(1);
998 return 0;
999 }
Al Viro04a31162014-11-27 13:51:41 -05001000 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -04001001 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -05001002 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -05001003 v.bv_offset, v.bv_len),
1004 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -05001005 )
1006 kunmap_atomic(kaddr);
1007 return bytes;
Al Viro62a80672014-04-04 23:12:29 -04001008}
1009EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
1010
Al Virob9dc6f62017-01-14 19:33:08 -05001011static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -04001012{
1013 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001014 unsigned int p_tail = pipe->tail;
1015 unsigned int p_head = pipe->head;
1016 unsigned int p_mask = pipe->ring_size - 1;
1017
1018 if (!pipe_empty(p_head, p_tail)) {
1019 struct pipe_buffer *buf;
1020 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001021 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +00001022
Al Virob9dc6f62017-01-14 19:33:08 -05001023 if (off) {
David Howells8cefc102019-11-15 13:30:32 +00001024 buf = &pipe->bufs[i_head & p_mask];
1025 buf->len = off - buf->offset;
1026 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001027 }
David Howells8cefc102019-11-15 13:30:32 +00001028 while (p_head != i_head) {
1029 p_head--;
1030 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
Al Viro241699c2016-09-22 16:33:12 -04001031 }
David Howells8cefc102019-11-15 13:30:32 +00001032
1033 pipe->head = p_head;
Al Viro241699c2016-09-22 16:33:12 -04001034 }
Al Virob9dc6f62017-01-14 19:33:08 -05001035}
1036
1037static void pipe_advance(struct iov_iter *i, size_t size)
1038{
1039 struct pipe_inode_info *pipe = i->pipe;
1040 if (unlikely(i->count < size))
1041 size = i->count;
1042 if (size) {
1043 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +00001044 unsigned int p_mask = pipe->ring_size - 1;
1045 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001046 size_t off = i->iov_offset, left = size;
David Howells8cefc102019-11-15 13:30:32 +00001047
Al Virob9dc6f62017-01-14 19:33:08 -05001048 if (off) /* make it relative to the beginning of buffer */
David Howells8cefc102019-11-15 13:30:32 +00001049 left += off - pipe->bufs[i_head & p_mask].offset;
Al Virob9dc6f62017-01-14 19:33:08 -05001050 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001051 buf = &pipe->bufs[i_head & p_mask];
Al Virob9dc6f62017-01-14 19:33:08 -05001052 if (left <= buf->len)
1053 break;
1054 left -= buf->len;
David Howells8cefc102019-11-15 13:30:32 +00001055 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001056 }
David Howells8cefc102019-11-15 13:30:32 +00001057 i->head = i_head;
Al Virob9dc6f62017-01-14 19:33:08 -05001058 i->iov_offset = buf->offset + left;
1059 }
1060 i->count -= size;
1061 /* ... and discard everything past that point */
1062 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001063}
1064
Al Viro62a80672014-04-04 23:12:29 -04001065void iov_iter_advance(struct iov_iter *i, size_t size)
1066{
David Howells00e23702018-10-22 13:07:28 +01001067 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001068 pipe_advance(i, size);
1069 return;
1070 }
David Howells9ea9ce02018-10-20 00:57:56 +01001071 if (unlikely(iov_iter_is_discard(i))) {
1072 i->count -= size;
1073 return;
1074 }
Al Viroa2804552014-11-27 14:48:42 -05001075 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -04001076}
1077EXPORT_SYMBOL(iov_iter_advance);
1078
Al Viro27c0e372017-02-17 18:42:24 -05001079void iov_iter_revert(struct iov_iter *i, size_t unroll)
1080{
1081 if (!unroll)
1082 return;
Al Viro5b47d592017-05-08 13:54:47 -04001083 if (WARN_ON(unroll > MAX_RW_COUNT))
1084 return;
Al Viro27c0e372017-02-17 18:42:24 -05001085 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001086 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001087 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001088 unsigned int p_mask = pipe->ring_size - 1;
1089 unsigned int i_head = i->head;
Al Viro27c0e372017-02-17 18:42:24 -05001090 size_t off = i->iov_offset;
1091 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001092 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1093 size_t n = off - b->offset;
Al Viro27c0e372017-02-17 18:42:24 -05001094 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001095 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001096 break;
1097 }
1098 unroll -= n;
David Howells8cefc102019-11-15 13:30:32 +00001099 if (!unroll && i_head == i->start_head) {
Al Viro27c0e372017-02-17 18:42:24 -05001100 off = 0;
1101 break;
1102 }
David Howells8cefc102019-11-15 13:30:32 +00001103 i_head--;
1104 b = &pipe->bufs[i_head & p_mask];
1105 off = b->offset + b->len;
Al Viro27c0e372017-02-17 18:42:24 -05001106 }
1107 i->iov_offset = off;
David Howells8cefc102019-11-15 13:30:32 +00001108 i->head = i_head;
Al Viro27c0e372017-02-17 18:42:24 -05001109 pipe_truncate(i);
1110 return;
1111 }
David Howells9ea9ce02018-10-20 00:57:56 +01001112 if (unlikely(iov_iter_is_discard(i)))
1113 return;
Al Viro27c0e372017-02-17 18:42:24 -05001114 if (unroll <= i->iov_offset) {
1115 i->iov_offset -= unroll;
1116 return;
1117 }
1118 unroll -= i->iov_offset;
David Howells00e23702018-10-22 13:07:28 +01001119 if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001120 const struct bio_vec *bvec = i->bvec;
1121 while (1) {
1122 size_t n = (--bvec)->bv_len;
1123 i->nr_segs++;
1124 if (unroll <= n) {
1125 i->bvec = bvec;
1126 i->iov_offset = n - unroll;
1127 return;
1128 }
1129 unroll -= n;
1130 }
1131 } else { /* same logics for iovec and kvec */
1132 const struct iovec *iov = i->iov;
1133 while (1) {
1134 size_t n = (--iov)->iov_len;
1135 i->nr_segs++;
1136 if (unroll <= n) {
1137 i->iov = iov;
1138 i->iov_offset = n - unroll;
1139 return;
1140 }
1141 unroll -= n;
1142 }
1143 }
1144}
1145EXPORT_SYMBOL(iov_iter_revert);
1146
Al Viro62a80672014-04-04 23:12:29 -04001147/*
1148 * Return the count of just the current iov_iter segment.
1149 */
1150size_t iov_iter_single_seg_count(const struct iov_iter *i)
1151{
David Howells00e23702018-10-22 13:07:28 +01001152 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001153 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001154 if (i->nr_segs == 1)
1155 return i->count;
David Howells9ea9ce02018-10-20 00:57:56 +01001156 if (unlikely(iov_iter_is_discard(i)))
1157 return i->count;
David Howells00e23702018-10-22 13:07:28 +01001158 else if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001159 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001160 else
1161 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001162}
1163EXPORT_SYMBOL(iov_iter_single_seg_count);
1164
David Howellsaa563d72018-10-20 00:57:56 +01001165void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001166 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001167 size_t count)
1168{
David Howellsaa563d72018-10-20 00:57:56 +01001169 WARN_ON(direction & ~(READ | WRITE));
1170 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001171 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001172 i->nr_segs = nr_segs;
1173 i->iov_offset = 0;
1174 i->count = count;
1175}
1176EXPORT_SYMBOL(iov_iter_kvec);
1177
David Howellsaa563d72018-10-20 00:57:56 +01001178void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001179 const struct bio_vec *bvec, unsigned long nr_segs,
1180 size_t count)
1181{
David Howellsaa563d72018-10-20 00:57:56 +01001182 WARN_ON(direction & ~(READ | WRITE));
1183 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001184 i->bvec = bvec;
1185 i->nr_segs = nr_segs;
1186 i->iov_offset = 0;
1187 i->count = count;
1188}
1189EXPORT_SYMBOL(iov_iter_bvec);
1190
David Howellsaa563d72018-10-20 00:57:56 +01001191void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001192 struct pipe_inode_info *pipe,
1193 size_t count)
1194{
David Howellsaa563d72018-10-20 00:57:56 +01001195 BUG_ON(direction != READ);
David Howells8cefc102019-11-15 13:30:32 +00001196 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
David Howellsaa563d72018-10-20 00:57:56 +01001197 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001198 i->pipe = pipe;
David Howells8cefc102019-11-15 13:30:32 +00001199 i->head = pipe->head;
Al Viro241699c2016-09-22 16:33:12 -04001200 i->iov_offset = 0;
1201 i->count = count;
David Howells8cefc102019-11-15 13:30:32 +00001202 i->start_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -04001203}
1204EXPORT_SYMBOL(iov_iter_pipe);
1205
David Howells9ea9ce02018-10-20 00:57:56 +01001206/**
1207 * iov_iter_discard - Initialise an I/O iterator that discards data
1208 * @i: The iterator to initialise.
1209 * @direction: The direction of the transfer.
1210 * @count: The size of the I/O buffer in bytes.
1211 *
1212 * Set up an I/O iterator that just discards everything that's written to it.
1213 * It's only available as a READ iterator.
1214 */
1215void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1216{
1217 BUG_ON(direction != READ);
1218 i->type = ITER_DISCARD | READ;
1219 i->count = count;
1220 i->iov_offset = 0;
1221}
1222EXPORT_SYMBOL(iov_iter_discard);
1223
Al Viro62a80672014-04-04 23:12:29 -04001224unsigned long iov_iter_alignment(const struct iov_iter *i)
1225{
Al Viro04a31162014-11-27 13:51:41 -05001226 unsigned long res = 0;
1227 size_t size = i->count;
1228
David Howells00e23702018-10-22 13:07:28 +01001229 if (unlikely(iov_iter_is_pipe(i))) {
Jan Karae0ff1262019-12-16 11:54:32 +01001230 unsigned int p_mask = i->pipe->ring_size - 1;
1231
David Howells8cefc102019-11-15 13:30:32 +00001232 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
Al Viro241699c2016-09-22 16:33:12 -04001233 return size | i->iov_offset;
1234 return size;
1235 }
Al Viro04a31162014-11-27 13:51:41 -05001236 iterate_all_kinds(i, size, v,
1237 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001238 res |= v.bv_offset | v.bv_len,
1239 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -05001240 )
1241 return res;
Al Viro62a80672014-04-04 23:12:29 -04001242}
1243EXPORT_SYMBOL(iov_iter_alignment);
1244
Al Viro357f4352016-04-08 19:05:19 -04001245unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1246{
Al Viro33844e62016-12-21 21:55:02 -05001247 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001248 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001249
David Howells9ea9ce02018-10-20 00:57:56 +01001250 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001251 WARN_ON(1);
1252 return ~0U;
1253 }
1254
Al Viro357f4352016-04-08 19:05:19 -04001255 iterate_all_kinds(i, size, v,
1256 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1257 (size != v.iov_len ? size : 0), 0),
1258 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1259 (size != v.bv_len ? size : 0)),
1260 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1261 (size != v.iov_len ? size : 0))
1262 );
Al Viro33844e62016-12-21 21:55:02 -05001263 return res;
Al Viro357f4352016-04-08 19:05:19 -04001264}
1265EXPORT_SYMBOL(iov_iter_gap_alignment);
1266
Ilya Dryomove76b63122018-05-02 20:16:56 +02001267static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001268 size_t maxsize,
1269 struct page **pages,
David Howells8cefc102019-11-15 13:30:32 +00001270 int iter_head,
Al Viro241699c2016-09-22 16:33:12 -04001271 size_t *start)
1272{
1273 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001274 unsigned int p_mask = pipe->ring_size - 1;
1275 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001276 if (!n)
1277 return -EFAULT;
1278
1279 maxsize = n;
1280 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001281 while (n > 0) {
David Howells8cefc102019-11-15 13:30:32 +00001282 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1283 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -04001284 n -= PAGE_SIZE;
1285 }
1286
1287 return maxsize;
1288}
1289
1290static ssize_t pipe_get_pages(struct iov_iter *i,
1291 struct page **pages, size_t maxsize, unsigned maxpages,
1292 size_t *start)
1293{
David Howells8cefc102019-11-15 13:30:32 +00001294 unsigned int iter_head, npages;
Al Viro241699c2016-09-22 16:33:12 -04001295 size_t capacity;
Al Viro241699c2016-09-22 16:33:12 -04001296
Al Viro33844e62016-12-21 21:55:02 -05001297 if (!maxsize)
1298 return 0;
1299
Al Viro241699c2016-09-22 16:33:12 -04001300 if (!sanity(i))
1301 return -EFAULT;
1302
David Howells8cefc102019-11-15 13:30:32 +00001303 data_start(i, &iter_head, start);
1304 /* Amount of free space: some of this one + all after this one */
1305 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1306 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
Al Viro241699c2016-09-22 16:33:12 -04001307
David Howells8cefc102019-11-15 13:30:32 +00001308 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001309}
1310
Al Viro62a80672014-04-04 23:12:29 -04001311ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001312 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001313 size_t *start)
1314{
Al Viroe5393fa2014-11-27 14:12:09 -05001315 if (maxsize > i->count)
1316 maxsize = i->count;
1317
David Howells00e23702018-10-22 13:07:28 +01001318 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001319 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001320 if (unlikely(iov_iter_is_discard(i)))
1321 return -EFAULT;
1322
Al Viroe5393fa2014-11-27 14:12:09 -05001323 iterate_all_kinds(i, maxsize, v, ({
1324 unsigned long addr = (unsigned long)v.iov_base;
1325 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1326 int n;
1327 int res;
1328
1329 if (len > maxpages * PAGE_SIZE)
1330 len = maxpages * PAGE_SIZE;
1331 addr &= ~(PAGE_SIZE - 1);
1332 n = DIV_ROUND_UP(len, PAGE_SIZE);
Ira Weiny73b01402019-05-13 17:17:11 -07001333 res = get_user_pages_fast(addr, n,
1334 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1335 pages);
Al Viroe5393fa2014-11-27 14:12:09 -05001336 if (unlikely(res < 0))
1337 return res;
1338 return (res == n ? len : res * PAGE_SIZE) - *start;
1339 0;}),({
1340 /* can't be more than PAGE_SIZE */
1341 *start = v.bv_offset;
1342 get_page(*pages = v.bv_page);
1343 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001344 }),({
1345 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001346 })
1347 )
1348 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001349}
1350EXPORT_SYMBOL(iov_iter_get_pages);
1351
Al Viro1b17f1f2014-11-27 14:14:31 -05001352static struct page **get_pages_array(size_t n)
1353{
Michal Hocko752ade62017-05-08 15:57:27 -07001354 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001355}
1356
Al Viro241699c2016-09-22 16:33:12 -04001357static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1358 struct page ***pages, size_t maxsize,
1359 size_t *start)
1360{
1361 struct page **p;
David Howells8cefc102019-11-15 13:30:32 +00001362 unsigned int iter_head, npages;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001363 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001364
Al Viro33844e62016-12-21 21:55:02 -05001365 if (!maxsize)
1366 return 0;
1367
Al Viro241699c2016-09-22 16:33:12 -04001368 if (!sanity(i))
1369 return -EFAULT;
1370
David Howells8cefc102019-11-15 13:30:32 +00001371 data_start(i, &iter_head, start);
1372 /* Amount of free space: some of this one + all after this one */
1373 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
Al Viro241699c2016-09-22 16:33:12 -04001374 n = npages * PAGE_SIZE - *start;
1375 if (maxsize > n)
1376 maxsize = n;
1377 else
1378 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1379 p = get_pages_array(npages);
1380 if (!p)
1381 return -ENOMEM;
David Howells8cefc102019-11-15 13:30:32 +00001382 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001383 if (n > 0)
1384 *pages = p;
1385 else
1386 kvfree(p);
1387 return n;
1388}
1389
Al Viro62a80672014-04-04 23:12:29 -04001390ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1391 struct page ***pages, size_t maxsize,
1392 size_t *start)
1393{
Al Viro1b17f1f2014-11-27 14:14:31 -05001394 struct page **p;
1395
1396 if (maxsize > i->count)
1397 maxsize = i->count;
1398
David Howells00e23702018-10-22 13:07:28 +01001399 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001400 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001401 if (unlikely(iov_iter_is_discard(i)))
1402 return -EFAULT;
1403
Al Viro1b17f1f2014-11-27 14:14:31 -05001404 iterate_all_kinds(i, maxsize, v, ({
1405 unsigned long addr = (unsigned long)v.iov_base;
1406 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1407 int n;
1408 int res;
1409
1410 addr &= ~(PAGE_SIZE - 1);
1411 n = DIV_ROUND_UP(len, PAGE_SIZE);
1412 p = get_pages_array(n);
1413 if (!p)
1414 return -ENOMEM;
Ira Weiny73b01402019-05-13 17:17:11 -07001415 res = get_user_pages_fast(addr, n,
1416 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
Al Viro1b17f1f2014-11-27 14:14:31 -05001417 if (unlikely(res < 0)) {
1418 kvfree(p);
1419 return res;
1420 }
1421 *pages = p;
1422 return (res == n ? len : res * PAGE_SIZE) - *start;
1423 0;}),({
1424 /* can't be more than PAGE_SIZE */
1425 *start = v.bv_offset;
1426 *pages = p = get_pages_array(1);
1427 if (!p)
1428 return -ENOMEM;
1429 get_page(*p = v.bv_page);
1430 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001431 }),({
1432 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -05001433 })
1434 )
1435 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001436}
1437EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1438
Al Viroa604ec72014-11-24 01:08:00 -05001439size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1440 struct iov_iter *i)
1441{
1442 char *to = addr;
1443 __wsum sum, next;
1444 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001445 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001446 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001447 WARN_ON(1);
1448 return 0;
1449 }
Al Viroa604ec72014-11-24 01:08:00 -05001450 iterate_and_advance(i, bytes, v, ({
1451 int err = 0;
Al Virocbbd26b2016-11-01 22:09:04 -04001452 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001453 (to += v.iov_len) - v.iov_len,
1454 v.iov_len, 0, &err);
1455 if (!err) {
1456 sum = csum_block_add(sum, next, off);
1457 off += v.iov_len;
1458 }
1459 err ? v.iov_len : 0;
1460 }), ({
1461 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001462 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1463 p + v.bv_offset, v.bv_len,
1464 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001465 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001466 off += v.bv_len;
1467 }),({
Al Virof9152892018-11-27 22:32:59 -05001468 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1469 v.iov_base, v.iov_len,
1470 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001471 off += v.iov_len;
1472 })
1473 )
1474 *csum = sum;
1475 return bytes;
1476}
1477EXPORT_SYMBOL(csum_and_copy_from_iter);
1478
Al Virocbbd26b2016-11-01 22:09:04 -04001479bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1480 struct iov_iter *i)
1481{
1482 char *to = addr;
1483 __wsum sum, next;
1484 size_t off = 0;
1485 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001486 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001487 WARN_ON(1);
1488 return false;
1489 }
1490 if (unlikely(i->count < bytes))
1491 return false;
1492 iterate_all_kinds(i, bytes, v, ({
1493 int err = 0;
1494 next = csum_and_copy_from_user(v.iov_base,
1495 (to += v.iov_len) - v.iov_len,
1496 v.iov_len, 0, &err);
1497 if (err)
1498 return false;
1499 sum = csum_block_add(sum, next, off);
1500 off += v.iov_len;
1501 0;
1502 }), ({
1503 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001504 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1505 p + v.bv_offset, v.bv_len,
1506 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001507 kunmap_atomic(p);
Al Virocbbd26b2016-11-01 22:09:04 -04001508 off += v.bv_len;
1509 }),({
Al Virof9152892018-11-27 22:32:59 -05001510 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1511 v.iov_base, v.iov_len,
1512 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001513 off += v.iov_len;
1514 })
1515 )
1516 *csum = sum;
1517 iov_iter_advance(i, bytes);
1518 return true;
1519}
1520EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1521
Sagi Grimbergcb002d02018-12-03 17:52:07 -08001522size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csump,
Al Viroa604ec72014-11-24 01:08:00 -05001523 struct iov_iter *i)
1524{
Al Viro36f7a8a2015-12-06 16:49:22 -05001525 const char *from = addr;
Sagi Grimbergcb002d02018-12-03 17:52:07 -08001526 __wsum *csum = csump;
Al Viroa604ec72014-11-24 01:08:00 -05001527 __wsum sum, next;
1528 size_t off = 0;
Al Viro78e1f382018-11-25 16:24:16 -05001529
1530 if (unlikely(iov_iter_is_pipe(i)))
1531 return csum_and_copy_to_pipe_iter(addr, bytes, csum, i);
1532
Al Viroa604ec72014-11-24 01:08:00 -05001533 sum = *csum;
Al Viro78e1f382018-11-25 16:24:16 -05001534 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001535 WARN_ON(1); /* for now */
1536 return 0;
1537 }
Al Viroa604ec72014-11-24 01:08:00 -05001538 iterate_and_advance(i, bytes, v, ({
1539 int err = 0;
1540 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001541 v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001542 v.iov_len, 0, &err);
1543 if (!err) {
1544 sum = csum_block_add(sum, next, off);
1545 off += v.iov_len;
1546 }
1547 err ? v.iov_len : 0;
1548 }), ({
1549 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001550 sum = csum_and_memcpy(p + v.bv_offset,
1551 (from += v.bv_len) - v.bv_len,
1552 v.bv_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001553 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001554 off += v.bv_len;
1555 }),({
Al Virof9152892018-11-27 22:32:59 -05001556 sum = csum_and_memcpy(v.iov_base,
1557 (from += v.iov_len) - v.iov_len,
1558 v.iov_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001559 off += v.iov_len;
1560 })
1561 )
1562 *csum = sum;
1563 return bytes;
1564}
1565EXPORT_SYMBOL(csum_and_copy_to_iter);
1566
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001567size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1568 struct iov_iter *i)
1569{
YueHaibing27fad742019-04-04 10:31:14 +08001570#ifdef CONFIG_CRYPTO
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001571 struct ahash_request *hash = hashp;
1572 struct scatterlist sg;
1573 size_t copied;
1574
1575 copied = copy_to_iter(addr, bytes, i);
1576 sg_init_one(&sg, addr, copied);
1577 ahash_request_set_crypt(hash, &sg, NULL, copied);
1578 crypto_ahash_update(hash);
1579 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001580#else
1581 return 0;
1582#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001583}
1584EXPORT_SYMBOL(hash_and_copy_to_iter);
1585
Al Viro62a80672014-04-04 23:12:29 -04001586int iov_iter_npages(const struct iov_iter *i, int maxpages)
1587{
Al Viroe0f2dc42014-11-27 14:09:46 -05001588 size_t size = i->count;
1589 int npages = 0;
1590
1591 if (!size)
1592 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001593 if (unlikely(iov_iter_is_discard(i)))
1594 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001595
David Howells00e23702018-10-22 13:07:28 +01001596 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001597 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001598 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -04001599 size_t off;
Al Viro241699c2016-09-22 16:33:12 -04001600
1601 if (!sanity(i))
1602 return 0;
1603
David Howells8cefc102019-11-15 13:30:32 +00001604 data_start(i, &iter_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001605 /* some of this one + all after this one */
David Howells8cefc102019-11-15 13:30:32 +00001606 npages = pipe_space_for_user(iter_head, pipe->tail, pipe);
Al Viro241699c2016-09-22 16:33:12 -04001607 if (npages >= maxpages)
1608 return maxpages;
1609 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001610 unsigned long p = (unsigned long)v.iov_base;
1611 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1612 - p / PAGE_SIZE;
1613 if (npages >= maxpages)
1614 return maxpages;
1615 0;}),({
1616 npages++;
1617 if (npages >= maxpages)
1618 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001619 }),({
1620 unsigned long p = (unsigned long)v.iov_base;
1621 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1622 - p / PAGE_SIZE;
1623 if (npages >= maxpages)
1624 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001625 })
1626 )
1627 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001628}
Al Virof67da302014-03-19 01:16:16 -04001629EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001630
1631const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1632{
1633 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001634 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001635 WARN_ON(1);
1636 return NULL;
1637 }
David Howells9ea9ce02018-10-20 00:57:56 +01001638 if (unlikely(iov_iter_is_discard(new)))
1639 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001640 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001641 return new->bvec = kmemdup(new->bvec,
1642 new->nr_segs * sizeof(struct bio_vec),
1643 flags);
1644 else
1645 /* iovec and kvec have identical layout */
1646 return new->iov = kmemdup(new->iov,
1647 new->nr_segs * sizeof(struct iovec),
1648 flags);
1649}
1650EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001651
Vegard Nossumffecee42016-10-08 11:18:07 +02001652/**
1653 * import_iovec() - Copy an array of &struct iovec from userspace
1654 * into the kernel, check that it is valid, and initialize a new
1655 * &struct iov_iter iterator to access it.
1656 *
1657 * @type: One of %READ or %WRITE.
1658 * @uvector: Pointer to the userspace array.
1659 * @nr_segs: Number of elements in userspace array.
1660 * @fast_segs: Number of elements in @iov.
1661 * @iov: (input and output parameter) Pointer to pointer to (usually small
1662 * on-stack) kernel array.
1663 * @i: Pointer to iterator that will be initialized on success.
1664 *
1665 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1666 * then this function places %NULL in *@iov on return. Otherwise, a new
1667 * array will be allocated and the result placed in *@iov. This means that
1668 * the caller may call kfree() on *@iov regardless of whether the small
1669 * on-stack array was used or not (and regardless of whether this function
1670 * returns an error or not).
1671 *
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001672 * Return: Negative error code on error, bytes imported on success
Vegard Nossumffecee42016-10-08 11:18:07 +02001673 */
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001674ssize_t import_iovec(int type, const struct iovec __user * uvector,
Al Virobc917be2015-03-21 17:45:43 -04001675 unsigned nr_segs, unsigned fast_segs,
1676 struct iovec **iov, struct iov_iter *i)
1677{
1678 ssize_t n;
1679 struct iovec *p;
1680 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1681 *iov, &p);
1682 if (n < 0) {
1683 if (p != *iov)
1684 kfree(p);
1685 *iov = NULL;
1686 return n;
1687 }
1688 iov_iter_init(i, type, p, nr_segs, n);
1689 *iov = p == *iov ? NULL : p;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001690 return n;
Al Virobc917be2015-03-21 17:45:43 -04001691}
1692EXPORT_SYMBOL(import_iovec);
1693
1694#ifdef CONFIG_COMPAT
1695#include <linux/compat.h>
1696
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001697ssize_t compat_import_iovec(int type,
1698 const struct compat_iovec __user * uvector,
1699 unsigned nr_segs, unsigned fast_segs,
1700 struct iovec **iov, struct iov_iter *i)
Al Virobc917be2015-03-21 17:45:43 -04001701{
1702 ssize_t n;
1703 struct iovec *p;
1704 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1705 *iov, &p);
1706 if (n < 0) {
1707 if (p != *iov)
1708 kfree(p);
1709 *iov = NULL;
1710 return n;
1711 }
1712 iov_iter_init(i, type, p, nr_segs, n);
1713 *iov = p == *iov ? NULL : p;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001714 return n;
Al Virobc917be2015-03-21 17:45:43 -04001715}
Arnd Bergmann98aaaec2019-03-14 17:45:18 +01001716EXPORT_SYMBOL(compat_import_iovec);
Al Virobc917be2015-03-21 17:45:43 -04001717#endif
1718
1719int import_single_range(int rw, void __user *buf, size_t len,
1720 struct iovec *iov, struct iov_iter *i)
1721{
1722 if (len > MAX_RW_COUNT)
1723 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001724 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04001725 return -EFAULT;
1726
1727 iov->iov_base = buf;
1728 iov->iov_len = len;
1729 iov_iter_init(i, rw, iov, 1, len);
1730 return 0;
1731}
Al Viroe1267582015-12-06 20:38:56 -05001732EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05001733
1734int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1735 int (*f)(struct kvec *vec, void *context),
1736 void *context)
1737{
1738 struct kvec w;
1739 int err = -EINVAL;
1740 if (!bytes)
1741 return 0;
1742
1743 iterate_all_kinds(i, bytes, v, -EINVAL, ({
1744 w.iov_base = kmap(v.bv_page) + v.bv_offset;
1745 w.iov_len = v.bv_len;
1746 err = f(&w, context);
1747 kunmap(v.bv_page);
1748 err;}), ({
1749 w = v;
1750 err = f(&w, context);})
1751 )
1752 return err;
1753}
1754EXPORT_SYMBOL(iov_iter_for_each_range);