blob: 2e452ce7388b8c1511488e6c775c04323780483a [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Herbert Xu79990962020-06-12 16:57:37 +10002#include <crypto/hash.h>
Al Viro4f18cd32014-02-05 19:11:33 -05003#include <linux/export.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -06004#include <linux/bvec.h>
Al Viro4f18cd32014-02-05 19:11:33 -05005#include <linux/uio.h>
6#include <linux/pagemap.h>
Al Viro91f79c42014-03-21 04:58:33 -04007#include <linux/slab.h>
8#include <linux/vmalloc.h>
Al Viro241699c2016-09-22 16:33:12 -04009#include <linux/splice.h>
Christoph Hellwigbfdc5972020-09-25 06:51:40 +020010#include <linux/compat.h>
Al Viroa604ec72014-11-24 01:08:00 -050011#include <net/checksum.h>
Sagi Grimbergd05f4432018-12-03 17:52:09 -080012#include <linux/scatterlist.h>
Marco Elverd0ef4c32020-01-21 17:05:11 +010013#include <linux/instrumented.h>
Al Viro4f18cd32014-02-05 19:11:33 -050014
Al Viro241699c2016-09-22 16:33:12 -040015#define PIPE_PARANOIA /* for now */
16
Al Viro04a31162014-11-27 13:51:41 -050017#define iterate_iovec(i, n, __v, __p, skip, STEP) { \
18 size_t left; \
19 size_t wanted = n; \
20 __p = i->iov; \
21 __v.iov_len = min(n, __p->iov_len - skip); \
22 if (likely(__v.iov_len)) { \
23 __v.iov_base = __p->iov_base + skip; \
24 left = (STEP); \
25 __v.iov_len -= left; \
26 skip += __v.iov_len; \
27 n -= __v.iov_len; \
28 } else { \
29 left = 0; \
30 } \
31 while (unlikely(!left && n)) { \
32 __p++; \
33 __v.iov_len = min(n, __p->iov_len); \
34 if (unlikely(!__v.iov_len)) \
35 continue; \
36 __v.iov_base = __p->iov_base; \
37 left = (STEP); \
38 __v.iov_len -= left; \
39 skip = __v.iov_len; \
40 n -= __v.iov_len; \
41 } \
42 n = wanted - n; \
43}
44
Al Viroa2804552014-11-27 14:48:42 -050045#define iterate_kvec(i, n, __v, __p, skip, STEP) { \
46 size_t wanted = n; \
47 __p = i->kvec; \
48 __v.iov_len = min(n, __p->iov_len - skip); \
49 if (likely(__v.iov_len)) { \
50 __v.iov_base = __p->iov_base + skip; \
51 (void)(STEP); \
52 skip += __v.iov_len; \
53 n -= __v.iov_len; \
54 } \
55 while (unlikely(n)) { \
56 __p++; \
57 __v.iov_len = min(n, __p->iov_len); \
58 if (unlikely(!__v.iov_len)) \
59 continue; \
60 __v.iov_base = __p->iov_base; \
61 (void)(STEP); \
62 skip = __v.iov_len; \
63 n -= __v.iov_len; \
64 } \
65 n = wanted; \
66}
67
Ming Lei1bdc76a2016-05-30 21:34:32 +080068#define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
69 struct bvec_iter __start; \
70 __start.bi_size = n; \
71 __start.bi_bvec_done = skip; \
72 __start.bi_idx = 0; \
73 for_each_bvec(__v, i->bvec, __bi, __start) { \
74 if (!__v.bv_len) \
Al Viro04a31162014-11-27 13:51:41 -050075 continue; \
Al Viro04a31162014-11-27 13:51:41 -050076 (void)(STEP); \
Al Viro04a31162014-11-27 13:51:41 -050077 } \
Al Viro04a31162014-11-27 13:51:41 -050078}
79
Al Viroa2804552014-11-27 14:48:42 -050080#define iterate_all_kinds(i, n, v, I, B, K) { \
Al Viro33844e62016-12-21 21:55:02 -050081 if (likely(n)) { \
82 size_t skip = i->iov_offset; \
83 if (unlikely(i->type & ITER_BVEC)) { \
84 struct bio_vec v; \
85 struct bvec_iter __bi; \
86 iterate_bvec(i, n, v, __bi, skip, (B)) \
87 } else if (unlikely(i->type & ITER_KVEC)) { \
88 const struct kvec *kvec; \
89 struct kvec v; \
90 iterate_kvec(i, n, v, kvec, skip, (K)) \
David Howells9ea9ce02018-10-20 00:57:56 +010091 } else if (unlikely(i->type & ITER_DISCARD)) { \
Al Viro33844e62016-12-21 21:55:02 -050092 } else { \
93 const struct iovec *iov; \
94 struct iovec v; \
95 iterate_iovec(i, n, v, iov, skip, (I)) \
96 } \
Al Viro04a31162014-11-27 13:51:41 -050097 } \
98}
99
Al Viroa2804552014-11-27 14:48:42 -0500100#define iterate_and_advance(i, n, v, I, B, K) { \
Al Virodd254f52016-05-09 11:54:48 -0400101 if (unlikely(i->count < n)) \
102 n = i->count; \
Al Viro19f18452016-05-25 17:36:19 -0400103 if (i->count) { \
Al Virodd254f52016-05-09 11:54:48 -0400104 size_t skip = i->iov_offset; \
105 if (unlikely(i->type & ITER_BVEC)) { \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800106 const struct bio_vec *bvec = i->bvec; \
Al Virodd254f52016-05-09 11:54:48 -0400107 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800108 struct bvec_iter __bi; \
109 iterate_bvec(i, n, v, __bi, skip, (B)) \
110 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
111 i->nr_segs -= i->bvec - bvec; \
112 skip = __bi.bi_bvec_done; \
Al Virodd254f52016-05-09 11:54:48 -0400113 } else if (unlikely(i->type & ITER_KVEC)) { \
114 const struct kvec *kvec; \
115 struct kvec v; \
116 iterate_kvec(i, n, v, kvec, skip, (K)) \
117 if (skip == kvec->iov_len) { \
118 kvec++; \
119 skip = 0; \
120 } \
121 i->nr_segs -= kvec - i->kvec; \
122 i->kvec = kvec; \
David Howells9ea9ce02018-10-20 00:57:56 +0100123 } else if (unlikely(i->type & ITER_DISCARD)) { \
124 skip += n; \
Al Virodd254f52016-05-09 11:54:48 -0400125 } else { \
126 const struct iovec *iov; \
127 struct iovec v; \
128 iterate_iovec(i, n, v, iov, skip, (I)) \
129 if (skip == iov->iov_len) { \
130 iov++; \
131 skip = 0; \
132 } \
133 i->nr_segs -= iov - i->iov; \
134 i->iov = iov; \
Al Viro7ce2a912014-11-27 13:59:45 -0500135 } \
Al Virodd254f52016-05-09 11:54:48 -0400136 i->count -= n; \
137 i->iov_offset = skip; \
Al Viro7ce2a912014-11-27 13:59:45 -0500138 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500139}
140
Al Viro09fc68dc2017-06-29 22:25:14 -0400141static int copyout(void __user *to, const void *from, size_t n)
142{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800143 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100144 instrument_copy_to_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400145 n = raw_copy_to_user(to, from, n);
146 }
147 return n;
148}
149
150static int copyin(void *to, const void __user *from, size_t n)
151{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800152 if (access_ok(from, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100153 instrument_copy_from_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400154 n = raw_copy_from_user(to, from, n);
155 }
156 return n;
157}
158
Al Viro62a80672014-04-04 23:12:29 -0400159static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500160 struct iov_iter *i)
161{
162 size_t skip, copy, left, wanted;
163 const struct iovec *iov;
164 char __user *buf;
165 void *kaddr, *from;
166
167 if (unlikely(bytes > i->count))
168 bytes = i->count;
169
170 if (unlikely(!bytes))
171 return 0;
172
Al Viro09fc68dc2017-06-29 22:25:14 -0400173 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500174 wanted = bytes;
175 iov = i->iov;
176 skip = i->iov_offset;
177 buf = iov->iov_base + skip;
178 copy = min(bytes, iov->iov_len - skip);
179
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700180 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500181 kaddr = kmap_atomic(page);
182 from = kaddr + offset;
183
184 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400185 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500186 copy -= left;
187 skip += copy;
188 from += copy;
189 bytes -= copy;
190
191 while (unlikely(!left && bytes)) {
192 iov++;
193 buf = iov->iov_base;
194 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400195 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500196 copy -= left;
197 skip = copy;
198 from += copy;
199 bytes -= copy;
200 }
201 if (likely(!bytes)) {
202 kunmap_atomic(kaddr);
203 goto done;
204 }
205 offset = from - kaddr;
206 buf += copy;
207 kunmap_atomic(kaddr);
208 copy = min(bytes, iov->iov_len - skip);
209 }
210 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700211
Al Viro4f18cd32014-02-05 19:11:33 -0500212 kaddr = kmap(page);
213 from = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400214 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500215 copy -= left;
216 skip += copy;
217 from += copy;
218 bytes -= copy;
219 while (unlikely(!left && bytes)) {
220 iov++;
221 buf = iov->iov_base;
222 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400223 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500224 copy -= left;
225 skip = copy;
226 from += copy;
227 bytes -= copy;
228 }
229 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700230
Al Viro4f18cd32014-02-05 19:11:33 -0500231done:
Al Viro81055e52014-04-04 19:23:46 -0400232 if (skip == iov->iov_len) {
233 iov++;
234 skip = 0;
235 }
Al Viro4f18cd32014-02-05 19:11:33 -0500236 i->count -= wanted - bytes;
237 i->nr_segs -= iov - i->iov;
238 i->iov = iov;
239 i->iov_offset = skip;
240 return wanted - bytes;
241}
Al Viro4f18cd32014-02-05 19:11:33 -0500242
Al Viro62a80672014-04-04 23:12:29 -0400243static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400244 struct iov_iter *i)
245{
246 size_t skip, copy, left, wanted;
247 const struct iovec *iov;
248 char __user *buf;
249 void *kaddr, *to;
250
251 if (unlikely(bytes > i->count))
252 bytes = i->count;
253
254 if (unlikely(!bytes))
255 return 0;
256
Al Viro09fc68dc2017-06-29 22:25:14 -0400257 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400258 wanted = bytes;
259 iov = i->iov;
260 skip = i->iov_offset;
261 buf = iov->iov_base + skip;
262 copy = min(bytes, iov->iov_len - skip);
263
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700264 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400265 kaddr = kmap_atomic(page);
266 to = kaddr + offset;
267
268 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400269 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400270 copy -= left;
271 skip += copy;
272 to += copy;
273 bytes -= copy;
274
275 while (unlikely(!left && bytes)) {
276 iov++;
277 buf = iov->iov_base;
278 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400279 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400280 copy -= left;
281 skip = copy;
282 to += copy;
283 bytes -= copy;
284 }
285 if (likely(!bytes)) {
286 kunmap_atomic(kaddr);
287 goto done;
288 }
289 offset = to - kaddr;
290 buf += copy;
291 kunmap_atomic(kaddr);
292 copy = min(bytes, iov->iov_len - skip);
293 }
294 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700295
Al Virof0d1bec2014-04-03 15:05:18 -0400296 kaddr = kmap(page);
297 to = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400298 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400299 copy -= left;
300 skip += copy;
301 to += copy;
302 bytes -= copy;
303 while (unlikely(!left && bytes)) {
304 iov++;
305 buf = iov->iov_base;
306 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400307 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400308 copy -= left;
309 skip = copy;
310 to += copy;
311 bytes -= copy;
312 }
313 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700314
Al Virof0d1bec2014-04-03 15:05:18 -0400315done:
Al Viro81055e52014-04-04 19:23:46 -0400316 if (skip == iov->iov_len) {
317 iov++;
318 skip = 0;
319 }
Al Virof0d1bec2014-04-03 15:05:18 -0400320 i->count -= wanted - bytes;
321 i->nr_segs -= iov - i->iov;
322 i->iov = iov;
323 i->iov_offset = skip;
324 return wanted - bytes;
325}
Al Virof0d1bec2014-04-03 15:05:18 -0400326
Al Viro241699c2016-09-22 16:33:12 -0400327#ifdef PIPE_PARANOIA
328static bool sanity(const struct iov_iter *i)
329{
330 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000331 unsigned int p_head = pipe->head;
332 unsigned int p_tail = pipe->tail;
333 unsigned int p_mask = pipe->ring_size - 1;
334 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
335 unsigned int i_head = i->head;
336 unsigned int idx;
337
Al Viro241699c2016-09-22 16:33:12 -0400338 if (i->iov_offset) {
339 struct pipe_buffer *p;
David Howells8cefc102019-11-15 13:30:32 +0000340 if (unlikely(p_occupancy == 0))
Al Viro241699c2016-09-22 16:33:12 -0400341 goto Bad; // pipe must be non-empty
David Howells8cefc102019-11-15 13:30:32 +0000342 if (unlikely(i_head != p_head - 1))
Al Viro241699c2016-09-22 16:33:12 -0400343 goto Bad; // must be at the last buffer...
344
David Howells8cefc102019-11-15 13:30:32 +0000345 p = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400346 if (unlikely(p->offset + p->len != i->iov_offset))
347 goto Bad; // ... at the end of segment
348 } else {
David Howells8cefc102019-11-15 13:30:32 +0000349 if (i_head != p_head)
Al Viro241699c2016-09-22 16:33:12 -0400350 goto Bad; // must be right after the last buffer
351 }
352 return true;
353Bad:
David Howells8cefc102019-11-15 13:30:32 +0000354 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
355 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
356 p_head, p_tail, pipe->ring_size);
357 for (idx = 0; idx < pipe->ring_size; idx++)
Al Viro241699c2016-09-22 16:33:12 -0400358 printk(KERN_ERR "[%p %p %d %d]\n",
359 pipe->bufs[idx].ops,
360 pipe->bufs[idx].page,
361 pipe->bufs[idx].offset,
362 pipe->bufs[idx].len);
363 WARN_ON(1);
364 return false;
365}
366#else
367#define sanity(i) true
368#endif
369
Al Viro241699c2016-09-22 16:33:12 -0400370static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
371 struct iov_iter *i)
372{
373 struct pipe_inode_info *pipe = i->pipe;
374 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +0000375 unsigned int p_tail = pipe->tail;
376 unsigned int p_mask = pipe->ring_size - 1;
377 unsigned int i_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400378 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400379
380 if (unlikely(bytes > i->count))
381 bytes = i->count;
382
383 if (unlikely(!bytes))
384 return 0;
385
386 if (!sanity(i))
387 return 0;
388
389 off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000390 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400391 if (off) {
392 if (offset == off && buf->page == page) {
393 /* merge with the last one */
394 buf->len += bytes;
395 i->iov_offset += bytes;
396 goto out;
397 }
David Howells8cefc102019-11-15 13:30:32 +0000398 i_head++;
399 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400400 }
David Howells6718b6f2019-10-16 16:47:32 +0100401 if (pipe_full(i_head, p_tail, pipe->max_usage))
Al Viro241699c2016-09-22 16:33:12 -0400402 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000403
Al Viro241699c2016-09-22 16:33:12 -0400404 buf->ops = &page_cache_pipe_buf_ops;
David Howells8cefc102019-11-15 13:30:32 +0000405 get_page(page);
406 buf->page = page;
Al Viro241699c2016-09-22 16:33:12 -0400407 buf->offset = offset;
408 buf->len = bytes;
David Howells8cefc102019-11-15 13:30:32 +0000409
410 pipe->head = i_head + 1;
Al Viro241699c2016-09-22 16:33:12 -0400411 i->iov_offset = offset + bytes;
David Howells8cefc102019-11-15 13:30:32 +0000412 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400413out:
414 i->count -= bytes;
415 return bytes;
416}
417
Al Viro4f18cd32014-02-05 19:11:33 -0500418/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400419 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
420 * bytes. For each iovec, fault in each page that constitutes the iovec.
421 *
422 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
423 * because it is an invalid address).
424 */
Al Virod4690f12016-09-16 00:11:45 +0100425int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400426{
427 size_t skip = i->iov_offset;
428 const struct iovec *iov;
429 int err;
430 struct iovec v;
431
432 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
433 iterate_iovec(i, bytes, v, iov, skip, ({
Al Viro4bce9f6e2016-09-17 18:02:44 -0400434 err = fault_in_pages_readable(v.iov_base, v.iov_len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400435 if (unlikely(err))
436 return err;
437 0;}))
438 }
439 return 0;
440}
Al Virod4690f12016-09-16 00:11:45 +0100441EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400442
David Howellsaa563d72018-10-20 00:57:56 +0100443void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500444 const struct iovec *iov, unsigned long nr_segs,
445 size_t count)
446{
David Howellsaa563d72018-10-20 00:57:56 +0100447 WARN_ON(direction & ~(READ | WRITE));
448 direction &= READ | WRITE;
449
Al Viro71d8e532014-03-05 19:28:09 -0500450 /* It will get better. Eventually... */
Al Virodb68ce12017-03-20 21:08:07 -0400451 if (uaccess_kernel()) {
David Howellsaa563d72018-10-20 00:57:56 +0100452 i->type = ITER_KVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500453 i->kvec = (struct kvec *)iov;
454 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100455 i->type = ITER_IOVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500456 i->iov = iov;
457 }
Al Viro71d8e532014-03-05 19:28:09 -0500458 i->nr_segs = nr_segs;
459 i->iov_offset = 0;
460 i->count = count;
461}
462EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400463
Al Viro62a80672014-04-04 23:12:29 -0400464static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
465{
466 char *from = kmap_atomic(page);
467 memcpy(to, from + offset, len);
468 kunmap_atomic(from);
469}
470
Al Viro36f7a8a2015-12-06 16:49:22 -0500471static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
Al Viro62a80672014-04-04 23:12:29 -0400472{
473 char *to = kmap_atomic(page);
474 memcpy(to + offset, from, len);
475 kunmap_atomic(to);
476}
477
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400478static void memzero_page(struct page *page, size_t offset, size_t len)
479{
480 char *addr = kmap_atomic(page);
481 memset(addr + offset, 0, len);
482 kunmap_atomic(addr);
483}
484
Al Viro241699c2016-09-22 16:33:12 -0400485static inline bool allocated(struct pipe_buffer *buf)
486{
487 return buf->ops == &default_pipe_buf_ops;
488}
489
David Howells8cefc102019-11-15 13:30:32 +0000490static inline void data_start(const struct iov_iter *i,
491 unsigned int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400492{
David Howells8cefc102019-11-15 13:30:32 +0000493 unsigned int p_mask = i->pipe->ring_size - 1;
494 unsigned int iter_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400495 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000496
497 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
498 off == PAGE_SIZE)) {
499 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400500 off = 0;
501 }
David Howells8cefc102019-11-15 13:30:32 +0000502 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400503 *offp = off;
504}
505
506static size_t push_pipe(struct iov_iter *i, size_t size,
David Howells8cefc102019-11-15 13:30:32 +0000507 int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400508{
509 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000510 unsigned int p_tail = pipe->tail;
511 unsigned int p_mask = pipe->ring_size - 1;
512 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400513 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400514 ssize_t left;
515
516 if (unlikely(size > i->count))
517 size = i->count;
518 if (unlikely(!size))
519 return 0;
520
521 left = size;
David Howells8cefc102019-11-15 13:30:32 +0000522 data_start(i, &iter_head, &off);
523 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400524 *offp = off;
525 if (off) {
526 left -= PAGE_SIZE - off;
527 if (left <= 0) {
David Howells8cefc102019-11-15 13:30:32 +0000528 pipe->bufs[iter_head & p_mask].len += size;
Al Viro241699c2016-09-22 16:33:12 -0400529 return size;
530 }
David Howells8cefc102019-11-15 13:30:32 +0000531 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
532 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400533 }
David Howells6718b6f2019-10-16 16:47:32 +0100534 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000535 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400536 struct page *page = alloc_page(GFP_USER);
537 if (!page)
538 break;
David Howells8cefc102019-11-15 13:30:32 +0000539
540 buf->ops = &default_pipe_buf_ops;
541 buf->page = page;
542 buf->offset = 0;
543 buf->len = min_t(ssize_t, left, PAGE_SIZE);
544 left -= buf->len;
545 iter_head++;
546 pipe->head = iter_head;
547
548 if (left == 0)
Al Viro241699c2016-09-22 16:33:12 -0400549 return size;
Al Viro241699c2016-09-22 16:33:12 -0400550 }
551 return size - left;
552}
553
554static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
555 struct iov_iter *i)
556{
557 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000558 unsigned int p_mask = pipe->ring_size - 1;
559 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400560 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400561
562 if (!sanity(i))
563 return 0;
564
David Howells8cefc102019-11-15 13:30:32 +0000565 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400566 if (unlikely(!n))
567 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000568 do {
Al Viro241699c2016-09-22 16:33:12 -0400569 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000570 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
571 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400572 i->iov_offset = off + chunk;
573 n -= chunk;
574 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000575 off = 0;
576 i_head++;
577 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400578 i->count -= bytes;
579 return bytes;
580}
581
Al Virof9152892018-11-27 22:32:59 -0500582static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
583 __wsum sum, size_t off)
584{
585 __wsum next = csum_partial_copy_nocheck(from, to, len, 0);
586 return csum_block_add(sum, next, off);
587}
588
Al Viro78e1f382018-11-25 16:24:16 -0500589static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
590 __wsum *csum, struct iov_iter *i)
591{
592 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000593 unsigned int p_mask = pipe->ring_size - 1;
594 unsigned int i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500595 size_t n, r;
596 size_t off = 0;
Al Virof9152892018-11-27 22:32:59 -0500597 __wsum sum = *csum;
Al Viro78e1f382018-11-25 16:24:16 -0500598
599 if (!sanity(i))
600 return 0;
601
David Howells8cefc102019-11-15 13:30:32 +0000602 bytes = n = push_pipe(i, bytes, &i_head, &r);
Al Viro78e1f382018-11-25 16:24:16 -0500603 if (unlikely(!n))
604 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000605 do {
Al Viro78e1f382018-11-25 16:24:16 -0500606 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
David Howells8cefc102019-11-15 13:30:32 +0000607 char *p = kmap_atomic(pipe->bufs[i_head & p_mask].page);
Al Virof9152892018-11-27 22:32:59 -0500608 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
Al Viro78e1f382018-11-25 16:24:16 -0500609 kunmap_atomic(p);
David Howells8cefc102019-11-15 13:30:32 +0000610 i->head = i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500611 i->iov_offset = r + chunk;
612 n -= chunk;
613 off += chunk;
614 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000615 r = 0;
616 i_head++;
617 } while (n);
Al Viro78e1f382018-11-25 16:24:16 -0500618 i->count -= bytes;
619 *csum = sum;
620 return bytes;
621}
622
Al Viroaa28de22017-06-29 21:45:10 -0400623size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400624{
Al Viro36f7a8a2015-12-06 16:49:22 -0500625 const char *from = addr;
David Howells00e23702018-10-22 13:07:28 +0100626 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400627 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400628 if (iter_is_iovec(i))
629 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500630 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400631 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500632 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500633 (from += v.bv_len) - v.bv_len, v.bv_len),
634 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500635 )
Al Viro62a80672014-04-04 23:12:29 -0400636
Al Viro3d4d3e42014-11-27 14:28:06 -0500637 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400638}
Al Viroaa28de22017-06-29 21:45:10 -0400639EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400640
Dan Williams87803562018-05-03 17:06:31 -0700641#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
642static int copyout_mcsafe(void __user *to, const void *from, size_t n)
643{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800644 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100645 instrument_copy_to_user(to, from, n);
Dan Williams87803562018-05-03 17:06:31 -0700646 n = copy_to_user_mcsafe((__force void *) to, from, n);
647 }
648 return n;
649}
650
651static unsigned long memcpy_mcsafe_to_page(struct page *page, size_t offset,
652 const char *from, size_t len)
653{
654 unsigned long ret;
655 char *to;
656
657 to = kmap_atomic(page);
658 ret = memcpy_mcsafe(to + offset, from, len);
659 kunmap_atomic(to);
660
661 return ret;
662}
663
Dan Williamsca146f62018-07-08 13:46:12 -0700664static size_t copy_pipe_to_iter_mcsafe(const void *addr, size_t bytes,
665 struct iov_iter *i)
666{
667 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000668 unsigned int p_mask = pipe->ring_size - 1;
669 unsigned int i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700670 size_t n, off, xfer = 0;
Dan Williamsca146f62018-07-08 13:46:12 -0700671
672 if (!sanity(i))
673 return 0;
674
David Howells8cefc102019-11-15 13:30:32 +0000675 bytes = n = push_pipe(i, bytes, &i_head, &off);
Dan Williamsca146f62018-07-08 13:46:12 -0700676 if (unlikely(!n))
677 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000678 do {
Dan Williamsca146f62018-07-08 13:46:12 -0700679 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
680 unsigned long rem;
681
David Howells8cefc102019-11-15 13:30:32 +0000682 rem = memcpy_mcsafe_to_page(pipe->bufs[i_head & p_mask].page,
683 off, addr, chunk);
684 i->head = i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700685 i->iov_offset = off + chunk - rem;
686 xfer += chunk - rem;
687 if (rem)
688 break;
689 n -= chunk;
690 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000691 off = 0;
692 i_head++;
693 } while (n);
Dan Williamsca146f62018-07-08 13:46:12 -0700694 i->count -= xfer;
695 return xfer;
696}
697
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700698/**
699 * _copy_to_iter_mcsafe - copy to user with source-read error exception handling
700 * @addr: source kernel address
701 * @bytes: total transfer length
702 * @iter: destination iterator
703 *
704 * The pmem driver arranges for filesystem-dax to use this facility via
705 * dax_copy_to_iter() for protecting read/write to persistent memory.
706 * Unless / until an architecture can guarantee identical performance
707 * between _copy_to_iter_mcsafe() and _copy_to_iter() it would be a
708 * performance regression to switch more users to the mcsafe version.
709 *
710 * Otherwise, the main differences between this and typical _copy_to_iter().
711 *
712 * * Typical tail/residue handling after a fault retries the copy
713 * byte-by-byte until the fault happens again. Re-triggering machine
714 * checks is potentially fatal so the implementation uses source
715 * alignment and poison alignment assumptions to avoid re-triggering
716 * hardware exceptions.
717 *
718 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
719 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
720 * a short copy.
721 *
722 * See MCSAFE_TEST for self-test.
723 */
Dan Williams87803562018-05-03 17:06:31 -0700724size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i)
725{
726 const char *from = addr;
727 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
728
David Howells00e23702018-10-22 13:07:28 +0100729 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsca146f62018-07-08 13:46:12 -0700730 return copy_pipe_to_iter_mcsafe(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700731 if (iter_is_iovec(i))
732 might_fault();
733 iterate_and_advance(i, bytes, v,
734 copyout_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
735 ({
736 rem = memcpy_mcsafe_to_page(v.bv_page, v.bv_offset,
737 (from += v.bv_len) - v.bv_len, v.bv_len);
738 if (rem) {
739 curr_addr = (unsigned long) from;
740 bytes = curr_addr - s_addr - rem;
741 return bytes;
742 }
743 }),
744 ({
745 rem = memcpy_mcsafe(v.iov_base, (from += v.iov_len) - v.iov_len,
746 v.iov_len);
747 if (rem) {
748 curr_addr = (unsigned long) from;
749 bytes = curr_addr - s_addr - rem;
750 return bytes;
751 }
752 })
753 )
754
755 return bytes;
756}
757EXPORT_SYMBOL_GPL(_copy_to_iter_mcsafe);
758#endif /* CONFIG_ARCH_HAS_UACCESS_MCSAFE */
759
Al Viroaa28de22017-06-29 21:45:10 -0400760size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400761{
Al Viro0dbca9a2014-11-27 14:26:43 -0500762 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100763 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400764 WARN_ON(1);
765 return 0;
766 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400767 if (iter_is_iovec(i))
768 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500769 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400770 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500771 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500772 v.bv_offset, v.bv_len),
773 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500774 )
775
776 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400777}
Al Viroaa28de22017-06-29 21:45:10 -0400778EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400779
Al Viroaa28de22017-06-29 21:45:10 -0400780bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400781{
782 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100783 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400784 WARN_ON(1);
785 return false;
786 }
Al Viro33844e62016-12-21 21:55:02 -0500787 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400788 return false;
789
Al Viro09fc68dc2017-06-29 22:25:14 -0400790 if (iter_is_iovec(i))
791 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400792 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68dc2017-06-29 22:25:14 -0400793 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400794 v.iov_base, v.iov_len))
795 return false;
796 0;}),
797 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
798 v.bv_offset, v.bv_len),
799 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
800 )
801
802 iov_iter_advance(i, bytes);
803 return true;
804}
Al Viroaa28de22017-06-29 21:45:10 -0400805EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400806
Al Viroaa28de22017-06-29 21:45:10 -0400807size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500808{
809 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100810 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400811 WARN_ON(1);
812 return 0;
813 }
Al Viroaa583092014-11-27 20:27:08 -0500814 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400815 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500816 v.iov_base, v.iov_len),
817 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
818 v.bv_offset, v.bv_len),
819 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
820 )
821
822 return bytes;
823}
Al Viroaa28de22017-06-29 21:45:10 -0400824EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500825
Dan Williams0aed55a2017-05-29 12:22:50 -0700826#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700827/**
828 * _copy_from_iter_flushcache - write destination through cpu cache
829 * @addr: destination kernel address
830 * @bytes: total transfer length
831 * @iter: source iterator
832 *
833 * The pmem driver arranges for filesystem-dax to use this facility via
834 * dax_copy_from_iter() for ensuring that writes to persistent memory
835 * are flushed through the CPU cache. It is differentiated from
836 * _copy_from_iter_nocache() in that guarantees all data is flushed for
837 * all iterator types. The _copy_from_iter_nocache() only attempts to
838 * bypass the cache for the ITER_IOVEC case, and on some archs may use
839 * instructions that strand dirty-data in the cache.
840 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700841size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700842{
843 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100844 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700845 WARN_ON(1);
846 return 0;
847 }
848 iterate_and_advance(i, bytes, v,
849 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
850 v.iov_base, v.iov_len),
851 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
852 v.bv_offset, v.bv_len),
853 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
854 v.iov_len)
855 )
856
857 return bytes;
858}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700859EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700860#endif
861
Al Viroaa28de22017-06-29 21:45:10 -0400862bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400863{
864 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100865 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400866 WARN_ON(1);
867 return false;
868 }
Al Viro33844e62016-12-21 21:55:02 -0500869 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400870 return false;
871 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400872 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400873 v.iov_base, v.iov_len))
874 return false;
875 0;}),
876 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
877 v.bv_offset, v.bv_len),
878 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
879 )
880
881 iov_iter_advance(i, bytes);
882 return true;
883}
Al Viroaa28de22017-06-29 21:45:10 -0400884EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400885
Al Viro72e809e2017-06-29 21:52:57 -0400886static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
887{
Eric Dumazet6daef952019-02-26 10:42:39 -0800888 struct page *head;
889 size_t v = n + offset;
890
891 /*
892 * The general case needs to access the page order in order
893 * to compute the page size.
894 * However, we mostly deal with order-0 pages and thus can
895 * avoid a possible cache line miss for requests that fit all
896 * page orders.
897 */
898 if (n <= v && v <= PAGE_SIZE)
899 return true;
900
901 head = compound_head(page);
902 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700903
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700904 if (likely(n <= v && v <= (page_size(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400905 return true;
906 WARN_ON(1);
907 return false;
908}
Al Virod2715242014-11-27 14:22:37 -0500909
910size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
911 struct iov_iter *i)
912{
Al Viro72e809e2017-06-29 21:52:57 -0400913 if (unlikely(!page_copy_sane(page, offset, bytes)))
914 return 0;
Al Virod2715242014-11-27 14:22:37 -0500915 if (i->type & (ITER_BVEC|ITER_KVEC)) {
916 void *kaddr = kmap_atomic(page);
917 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
918 kunmap_atomic(kaddr);
919 return wanted;
David Howells9ea9ce02018-10-20 00:57:56 +0100920 } else if (unlikely(iov_iter_is_discard(i)))
921 return bytes;
922 else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500923 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400924 else
925 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500926}
927EXPORT_SYMBOL(copy_page_to_iter);
928
929size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
930 struct iov_iter *i)
931{
Al Viro72e809e2017-06-29 21:52:57 -0400932 if (unlikely(!page_copy_sane(page, offset, bytes)))
933 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +0100934 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400935 WARN_ON(1);
936 return 0;
937 }
Al Virod2715242014-11-27 14:22:37 -0500938 if (i->type & (ITER_BVEC|ITER_KVEC)) {
939 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400940 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500941 kunmap_atomic(kaddr);
942 return wanted;
943 } else
944 return copy_page_from_iter_iovec(page, offset, bytes, i);
945}
946EXPORT_SYMBOL(copy_page_from_iter);
947
Al Viro241699c2016-09-22 16:33:12 -0400948static size_t pipe_zero(size_t bytes, struct iov_iter *i)
949{
950 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000951 unsigned int p_mask = pipe->ring_size - 1;
952 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400953 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400954
955 if (!sanity(i))
956 return 0;
957
David Howells8cefc102019-11-15 13:30:32 +0000958 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400959 if (unlikely(!n))
960 return 0;
961
David Howells8cefc102019-11-15 13:30:32 +0000962 do {
Al Viro241699c2016-09-22 16:33:12 -0400963 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000964 memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk);
965 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400966 i->iov_offset = off + chunk;
967 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000968 off = 0;
969 i_head++;
970 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400971 i->count -= bytes;
972 return bytes;
973}
974
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400975size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
976{
David Howells00e23702018-10-22 13:07:28 +0100977 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400978 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500979 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400980 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500981 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
982 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500983 )
984
985 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400986}
987EXPORT_SYMBOL(iov_iter_zero);
988
Al Viro62a80672014-04-04 23:12:29 -0400989size_t iov_iter_copy_from_user_atomic(struct page *page,
990 struct iov_iter *i, unsigned long offset, size_t bytes)
991{
Al Viro04a31162014-11-27 13:51:41 -0500992 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400993 if (unlikely(!page_copy_sane(page, offset, bytes))) {
994 kunmap_atomic(kaddr);
995 return 0;
996 }
David Howells9ea9ce02018-10-20 00:57:56 +0100997 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400998 kunmap_atomic(kaddr);
999 WARN_ON(1);
1000 return 0;
1001 }
Al Viro04a31162014-11-27 13:51:41 -05001002 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -04001003 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -05001004 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -05001005 v.bv_offset, v.bv_len),
1006 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -05001007 )
1008 kunmap_atomic(kaddr);
1009 return bytes;
Al Viro62a80672014-04-04 23:12:29 -04001010}
1011EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
1012
Al Virob9dc6f62017-01-14 19:33:08 -05001013static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -04001014{
1015 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001016 unsigned int p_tail = pipe->tail;
1017 unsigned int p_head = pipe->head;
1018 unsigned int p_mask = pipe->ring_size - 1;
1019
1020 if (!pipe_empty(p_head, p_tail)) {
1021 struct pipe_buffer *buf;
1022 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001023 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +00001024
Al Virob9dc6f62017-01-14 19:33:08 -05001025 if (off) {
David Howells8cefc102019-11-15 13:30:32 +00001026 buf = &pipe->bufs[i_head & p_mask];
1027 buf->len = off - buf->offset;
1028 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001029 }
David Howells8cefc102019-11-15 13:30:32 +00001030 while (p_head != i_head) {
1031 p_head--;
1032 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
Al Viro241699c2016-09-22 16:33:12 -04001033 }
David Howells8cefc102019-11-15 13:30:32 +00001034
1035 pipe->head = p_head;
Al Viro241699c2016-09-22 16:33:12 -04001036 }
Al Virob9dc6f62017-01-14 19:33:08 -05001037}
1038
1039static void pipe_advance(struct iov_iter *i, size_t size)
1040{
1041 struct pipe_inode_info *pipe = i->pipe;
1042 if (unlikely(i->count < size))
1043 size = i->count;
1044 if (size) {
1045 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +00001046 unsigned int p_mask = pipe->ring_size - 1;
1047 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001048 size_t off = i->iov_offset, left = size;
David Howells8cefc102019-11-15 13:30:32 +00001049
Al Virob9dc6f62017-01-14 19:33:08 -05001050 if (off) /* make it relative to the beginning of buffer */
David Howells8cefc102019-11-15 13:30:32 +00001051 left += off - pipe->bufs[i_head & p_mask].offset;
Al Virob9dc6f62017-01-14 19:33:08 -05001052 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001053 buf = &pipe->bufs[i_head & p_mask];
Al Virob9dc6f62017-01-14 19:33:08 -05001054 if (left <= buf->len)
1055 break;
1056 left -= buf->len;
David Howells8cefc102019-11-15 13:30:32 +00001057 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001058 }
David Howells8cefc102019-11-15 13:30:32 +00001059 i->head = i_head;
Al Virob9dc6f62017-01-14 19:33:08 -05001060 i->iov_offset = buf->offset + left;
1061 }
1062 i->count -= size;
1063 /* ... and discard everything past that point */
1064 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001065}
1066
Al Viro62a80672014-04-04 23:12:29 -04001067void iov_iter_advance(struct iov_iter *i, size_t size)
1068{
David Howells00e23702018-10-22 13:07:28 +01001069 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001070 pipe_advance(i, size);
1071 return;
1072 }
David Howells9ea9ce02018-10-20 00:57:56 +01001073 if (unlikely(iov_iter_is_discard(i))) {
1074 i->count -= size;
1075 return;
1076 }
Al Viroa2804552014-11-27 14:48:42 -05001077 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -04001078}
1079EXPORT_SYMBOL(iov_iter_advance);
1080
Al Viro27c0e372017-02-17 18:42:24 -05001081void iov_iter_revert(struct iov_iter *i, size_t unroll)
1082{
1083 if (!unroll)
1084 return;
Al Viro5b47d592017-05-08 13:54:47 -04001085 if (WARN_ON(unroll > MAX_RW_COUNT))
1086 return;
Al Viro27c0e372017-02-17 18:42:24 -05001087 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001088 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001089 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001090 unsigned int p_mask = pipe->ring_size - 1;
1091 unsigned int i_head = i->head;
Al Viro27c0e372017-02-17 18:42:24 -05001092 size_t off = i->iov_offset;
1093 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001094 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1095 size_t n = off - b->offset;
Al Viro27c0e372017-02-17 18:42:24 -05001096 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001097 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001098 break;
1099 }
1100 unroll -= n;
David Howells8cefc102019-11-15 13:30:32 +00001101 if (!unroll && i_head == i->start_head) {
Al Viro27c0e372017-02-17 18:42:24 -05001102 off = 0;
1103 break;
1104 }
David Howells8cefc102019-11-15 13:30:32 +00001105 i_head--;
1106 b = &pipe->bufs[i_head & p_mask];
1107 off = b->offset + b->len;
Al Viro27c0e372017-02-17 18:42:24 -05001108 }
1109 i->iov_offset = off;
David Howells8cefc102019-11-15 13:30:32 +00001110 i->head = i_head;
Al Viro27c0e372017-02-17 18:42:24 -05001111 pipe_truncate(i);
1112 return;
1113 }
David Howells9ea9ce02018-10-20 00:57:56 +01001114 if (unlikely(iov_iter_is_discard(i)))
1115 return;
Al Viro27c0e372017-02-17 18:42:24 -05001116 if (unroll <= i->iov_offset) {
1117 i->iov_offset -= unroll;
1118 return;
1119 }
1120 unroll -= i->iov_offset;
David Howells00e23702018-10-22 13:07:28 +01001121 if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001122 const struct bio_vec *bvec = i->bvec;
1123 while (1) {
1124 size_t n = (--bvec)->bv_len;
1125 i->nr_segs++;
1126 if (unroll <= n) {
1127 i->bvec = bvec;
1128 i->iov_offset = n - unroll;
1129 return;
1130 }
1131 unroll -= n;
1132 }
1133 } else { /* same logics for iovec and kvec */
1134 const struct iovec *iov = i->iov;
1135 while (1) {
1136 size_t n = (--iov)->iov_len;
1137 i->nr_segs++;
1138 if (unroll <= n) {
1139 i->iov = iov;
1140 i->iov_offset = n - unroll;
1141 return;
1142 }
1143 unroll -= n;
1144 }
1145 }
1146}
1147EXPORT_SYMBOL(iov_iter_revert);
1148
Al Viro62a80672014-04-04 23:12:29 -04001149/*
1150 * Return the count of just the current iov_iter segment.
1151 */
1152size_t iov_iter_single_seg_count(const struct iov_iter *i)
1153{
David Howells00e23702018-10-22 13:07:28 +01001154 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001155 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001156 if (i->nr_segs == 1)
1157 return i->count;
David Howells9ea9ce02018-10-20 00:57:56 +01001158 if (unlikely(iov_iter_is_discard(i)))
1159 return i->count;
David Howells00e23702018-10-22 13:07:28 +01001160 else if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001161 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001162 else
1163 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001164}
1165EXPORT_SYMBOL(iov_iter_single_seg_count);
1166
David Howellsaa563d72018-10-20 00:57:56 +01001167void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001168 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001169 size_t count)
1170{
David Howellsaa563d72018-10-20 00:57:56 +01001171 WARN_ON(direction & ~(READ | WRITE));
1172 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001173 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001174 i->nr_segs = nr_segs;
1175 i->iov_offset = 0;
1176 i->count = count;
1177}
1178EXPORT_SYMBOL(iov_iter_kvec);
1179
David Howellsaa563d72018-10-20 00:57:56 +01001180void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001181 const struct bio_vec *bvec, unsigned long nr_segs,
1182 size_t count)
1183{
David Howellsaa563d72018-10-20 00:57:56 +01001184 WARN_ON(direction & ~(READ | WRITE));
1185 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001186 i->bvec = bvec;
1187 i->nr_segs = nr_segs;
1188 i->iov_offset = 0;
1189 i->count = count;
1190}
1191EXPORT_SYMBOL(iov_iter_bvec);
1192
David Howellsaa563d72018-10-20 00:57:56 +01001193void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001194 struct pipe_inode_info *pipe,
1195 size_t count)
1196{
David Howellsaa563d72018-10-20 00:57:56 +01001197 BUG_ON(direction != READ);
David Howells8cefc102019-11-15 13:30:32 +00001198 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
David Howellsaa563d72018-10-20 00:57:56 +01001199 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001200 i->pipe = pipe;
David Howells8cefc102019-11-15 13:30:32 +00001201 i->head = pipe->head;
Al Viro241699c2016-09-22 16:33:12 -04001202 i->iov_offset = 0;
1203 i->count = count;
David Howells8cefc102019-11-15 13:30:32 +00001204 i->start_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -04001205}
1206EXPORT_SYMBOL(iov_iter_pipe);
1207
David Howells9ea9ce02018-10-20 00:57:56 +01001208/**
1209 * iov_iter_discard - Initialise an I/O iterator that discards data
1210 * @i: The iterator to initialise.
1211 * @direction: The direction of the transfer.
1212 * @count: The size of the I/O buffer in bytes.
1213 *
1214 * Set up an I/O iterator that just discards everything that's written to it.
1215 * It's only available as a READ iterator.
1216 */
1217void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1218{
1219 BUG_ON(direction != READ);
1220 i->type = ITER_DISCARD | READ;
1221 i->count = count;
1222 i->iov_offset = 0;
1223}
1224EXPORT_SYMBOL(iov_iter_discard);
1225
Al Viro62a80672014-04-04 23:12:29 -04001226unsigned long iov_iter_alignment(const struct iov_iter *i)
1227{
Al Viro04a31162014-11-27 13:51:41 -05001228 unsigned long res = 0;
1229 size_t size = i->count;
1230
David Howells00e23702018-10-22 13:07:28 +01001231 if (unlikely(iov_iter_is_pipe(i))) {
Jan Karae0ff1262019-12-16 11:54:32 +01001232 unsigned int p_mask = i->pipe->ring_size - 1;
1233
David Howells8cefc102019-11-15 13:30:32 +00001234 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
Al Viro241699c2016-09-22 16:33:12 -04001235 return size | i->iov_offset;
1236 return size;
1237 }
Al Viro04a31162014-11-27 13:51:41 -05001238 iterate_all_kinds(i, size, v,
1239 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001240 res |= v.bv_offset | v.bv_len,
1241 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -05001242 )
1243 return res;
Al Viro62a80672014-04-04 23:12:29 -04001244}
1245EXPORT_SYMBOL(iov_iter_alignment);
1246
Al Viro357f4352016-04-08 19:05:19 -04001247unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1248{
Al Viro33844e62016-12-21 21:55:02 -05001249 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001250 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001251
David Howells9ea9ce02018-10-20 00:57:56 +01001252 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001253 WARN_ON(1);
1254 return ~0U;
1255 }
1256
Al Viro357f4352016-04-08 19:05:19 -04001257 iterate_all_kinds(i, size, v,
1258 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1259 (size != v.iov_len ? size : 0), 0),
1260 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1261 (size != v.bv_len ? size : 0)),
1262 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1263 (size != v.iov_len ? size : 0))
1264 );
Al Viro33844e62016-12-21 21:55:02 -05001265 return res;
Al Viro357f4352016-04-08 19:05:19 -04001266}
1267EXPORT_SYMBOL(iov_iter_gap_alignment);
1268
Ilya Dryomove76b63122018-05-02 20:16:56 +02001269static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001270 size_t maxsize,
1271 struct page **pages,
David Howells8cefc102019-11-15 13:30:32 +00001272 int iter_head,
Al Viro241699c2016-09-22 16:33:12 -04001273 size_t *start)
1274{
1275 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001276 unsigned int p_mask = pipe->ring_size - 1;
1277 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001278 if (!n)
1279 return -EFAULT;
1280
1281 maxsize = n;
1282 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001283 while (n > 0) {
David Howells8cefc102019-11-15 13:30:32 +00001284 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1285 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -04001286 n -= PAGE_SIZE;
1287 }
1288
1289 return maxsize;
1290}
1291
1292static ssize_t pipe_get_pages(struct iov_iter *i,
1293 struct page **pages, size_t maxsize, unsigned maxpages,
1294 size_t *start)
1295{
David Howells8cefc102019-11-15 13:30:32 +00001296 unsigned int iter_head, npages;
Al Viro241699c2016-09-22 16:33:12 -04001297 size_t capacity;
Al Viro241699c2016-09-22 16:33:12 -04001298
Al Viro33844e62016-12-21 21:55:02 -05001299 if (!maxsize)
1300 return 0;
1301
Al Viro241699c2016-09-22 16:33:12 -04001302 if (!sanity(i))
1303 return -EFAULT;
1304
David Howells8cefc102019-11-15 13:30:32 +00001305 data_start(i, &iter_head, start);
1306 /* Amount of free space: some of this one + all after this one */
1307 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1308 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
Al Viro241699c2016-09-22 16:33:12 -04001309
David Howells8cefc102019-11-15 13:30:32 +00001310 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001311}
1312
Al Viro62a80672014-04-04 23:12:29 -04001313ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001314 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001315 size_t *start)
1316{
Al Viroe5393fa2014-11-27 14:12:09 -05001317 if (maxsize > i->count)
1318 maxsize = i->count;
1319
David Howells00e23702018-10-22 13:07:28 +01001320 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001321 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001322 if (unlikely(iov_iter_is_discard(i)))
1323 return -EFAULT;
1324
Al Viroe5393fa2014-11-27 14:12:09 -05001325 iterate_all_kinds(i, maxsize, v, ({
1326 unsigned long addr = (unsigned long)v.iov_base;
1327 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1328 int n;
1329 int res;
1330
1331 if (len > maxpages * PAGE_SIZE)
1332 len = maxpages * PAGE_SIZE;
1333 addr &= ~(PAGE_SIZE - 1);
1334 n = DIV_ROUND_UP(len, PAGE_SIZE);
Ira Weiny73b01402019-05-13 17:17:11 -07001335 res = get_user_pages_fast(addr, n,
1336 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1337 pages);
Al Viroe5393fa2014-11-27 14:12:09 -05001338 if (unlikely(res < 0))
1339 return res;
1340 return (res == n ? len : res * PAGE_SIZE) - *start;
1341 0;}),({
1342 /* can't be more than PAGE_SIZE */
1343 *start = v.bv_offset;
1344 get_page(*pages = v.bv_page);
1345 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001346 }),({
1347 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001348 })
1349 )
1350 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001351}
1352EXPORT_SYMBOL(iov_iter_get_pages);
1353
Al Viro1b17f1f2014-11-27 14:14:31 -05001354static struct page **get_pages_array(size_t n)
1355{
Michal Hocko752ade62017-05-08 15:57:27 -07001356 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001357}
1358
Al Viro241699c2016-09-22 16:33:12 -04001359static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1360 struct page ***pages, size_t maxsize,
1361 size_t *start)
1362{
1363 struct page **p;
David Howells8cefc102019-11-15 13:30:32 +00001364 unsigned int iter_head, npages;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001365 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001366
Al Viro33844e62016-12-21 21:55:02 -05001367 if (!maxsize)
1368 return 0;
1369
Al Viro241699c2016-09-22 16:33:12 -04001370 if (!sanity(i))
1371 return -EFAULT;
1372
David Howells8cefc102019-11-15 13:30:32 +00001373 data_start(i, &iter_head, start);
1374 /* Amount of free space: some of this one + all after this one */
1375 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
Al Viro241699c2016-09-22 16:33:12 -04001376 n = npages * PAGE_SIZE - *start;
1377 if (maxsize > n)
1378 maxsize = n;
1379 else
1380 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1381 p = get_pages_array(npages);
1382 if (!p)
1383 return -ENOMEM;
David Howells8cefc102019-11-15 13:30:32 +00001384 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001385 if (n > 0)
1386 *pages = p;
1387 else
1388 kvfree(p);
1389 return n;
1390}
1391
Al Viro62a80672014-04-04 23:12:29 -04001392ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1393 struct page ***pages, size_t maxsize,
1394 size_t *start)
1395{
Al Viro1b17f1f2014-11-27 14:14:31 -05001396 struct page **p;
1397
1398 if (maxsize > i->count)
1399 maxsize = i->count;
1400
David Howells00e23702018-10-22 13:07:28 +01001401 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001402 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001403 if (unlikely(iov_iter_is_discard(i)))
1404 return -EFAULT;
1405
Al Viro1b17f1f2014-11-27 14:14:31 -05001406 iterate_all_kinds(i, maxsize, v, ({
1407 unsigned long addr = (unsigned long)v.iov_base;
1408 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1409 int n;
1410 int res;
1411
1412 addr &= ~(PAGE_SIZE - 1);
1413 n = DIV_ROUND_UP(len, PAGE_SIZE);
1414 p = get_pages_array(n);
1415 if (!p)
1416 return -ENOMEM;
Ira Weiny73b01402019-05-13 17:17:11 -07001417 res = get_user_pages_fast(addr, n,
1418 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
Al Viro1b17f1f2014-11-27 14:14:31 -05001419 if (unlikely(res < 0)) {
1420 kvfree(p);
1421 return res;
1422 }
1423 *pages = p;
1424 return (res == n ? len : res * PAGE_SIZE) - *start;
1425 0;}),({
1426 /* can't be more than PAGE_SIZE */
1427 *start = v.bv_offset;
1428 *pages = p = get_pages_array(1);
1429 if (!p)
1430 return -ENOMEM;
1431 get_page(*p = v.bv_page);
1432 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001433 }),({
1434 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -05001435 })
1436 )
1437 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001438}
1439EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1440
Al Viroa604ec72014-11-24 01:08:00 -05001441size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1442 struct iov_iter *i)
1443{
1444 char *to = addr;
1445 __wsum sum, next;
1446 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001447 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001448 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001449 WARN_ON(1);
1450 return 0;
1451 }
Al Viroa604ec72014-11-24 01:08:00 -05001452 iterate_and_advance(i, bytes, v, ({
1453 int err = 0;
Al Virocbbd26b2016-11-01 22:09:04 -04001454 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001455 (to += v.iov_len) - v.iov_len,
1456 v.iov_len, 0, &err);
1457 if (!err) {
1458 sum = csum_block_add(sum, next, off);
1459 off += v.iov_len;
1460 }
1461 err ? v.iov_len : 0;
1462 }), ({
1463 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001464 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1465 p + v.bv_offset, v.bv_len,
1466 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001467 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001468 off += v.bv_len;
1469 }),({
Al Virof9152892018-11-27 22:32:59 -05001470 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1471 v.iov_base, v.iov_len,
1472 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001473 off += v.iov_len;
1474 })
1475 )
1476 *csum = sum;
1477 return bytes;
1478}
1479EXPORT_SYMBOL(csum_and_copy_from_iter);
1480
Al Virocbbd26b2016-11-01 22:09:04 -04001481bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1482 struct iov_iter *i)
1483{
1484 char *to = addr;
1485 __wsum sum, next;
1486 size_t off = 0;
1487 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001488 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001489 WARN_ON(1);
1490 return false;
1491 }
1492 if (unlikely(i->count < bytes))
1493 return false;
1494 iterate_all_kinds(i, bytes, v, ({
1495 int err = 0;
1496 next = csum_and_copy_from_user(v.iov_base,
1497 (to += v.iov_len) - v.iov_len,
1498 v.iov_len, 0, &err);
1499 if (err)
1500 return false;
1501 sum = csum_block_add(sum, next, off);
1502 off += v.iov_len;
1503 0;
1504 }), ({
1505 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001506 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1507 p + v.bv_offset, v.bv_len,
1508 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001509 kunmap_atomic(p);
Al Virocbbd26b2016-11-01 22:09:04 -04001510 off += v.bv_len;
1511 }),({
Al Virof9152892018-11-27 22:32:59 -05001512 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1513 v.iov_base, v.iov_len,
1514 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001515 off += v.iov_len;
1516 })
1517 )
1518 *csum = sum;
1519 iov_iter_advance(i, bytes);
1520 return true;
1521}
1522EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1523
Sagi Grimbergcb002d02018-12-03 17:52:07 -08001524size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csump,
Al Viroa604ec72014-11-24 01:08:00 -05001525 struct iov_iter *i)
1526{
Al Viro36f7a8a2015-12-06 16:49:22 -05001527 const char *from = addr;
Sagi Grimbergcb002d02018-12-03 17:52:07 -08001528 __wsum *csum = csump;
Al Viroa604ec72014-11-24 01:08:00 -05001529 __wsum sum, next;
1530 size_t off = 0;
Al Viro78e1f382018-11-25 16:24:16 -05001531
1532 if (unlikely(iov_iter_is_pipe(i)))
1533 return csum_and_copy_to_pipe_iter(addr, bytes, csum, i);
1534
Al Viroa604ec72014-11-24 01:08:00 -05001535 sum = *csum;
Al Viro78e1f382018-11-25 16:24:16 -05001536 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001537 WARN_ON(1); /* for now */
1538 return 0;
1539 }
Al Viroa604ec72014-11-24 01:08:00 -05001540 iterate_and_advance(i, bytes, v, ({
1541 int err = 0;
1542 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001543 v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001544 v.iov_len, 0, &err);
1545 if (!err) {
1546 sum = csum_block_add(sum, next, off);
1547 off += v.iov_len;
1548 }
1549 err ? v.iov_len : 0;
1550 }), ({
1551 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001552 sum = csum_and_memcpy(p + v.bv_offset,
1553 (from += v.bv_len) - v.bv_len,
1554 v.bv_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001555 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001556 off += v.bv_len;
1557 }),({
Al Virof9152892018-11-27 22:32:59 -05001558 sum = csum_and_memcpy(v.iov_base,
1559 (from += v.iov_len) - v.iov_len,
1560 v.iov_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001561 off += v.iov_len;
1562 })
1563 )
1564 *csum = sum;
1565 return bytes;
1566}
1567EXPORT_SYMBOL(csum_and_copy_to_iter);
1568
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001569size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1570 struct iov_iter *i)
1571{
Herbert Xu79990962020-06-12 16:57:37 +10001572#ifdef CONFIG_CRYPTO_HASH
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001573 struct ahash_request *hash = hashp;
1574 struct scatterlist sg;
1575 size_t copied;
1576
1577 copied = copy_to_iter(addr, bytes, i);
1578 sg_init_one(&sg, addr, copied);
1579 ahash_request_set_crypt(hash, &sg, NULL, copied);
1580 crypto_ahash_update(hash);
1581 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001582#else
1583 return 0;
1584#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001585}
1586EXPORT_SYMBOL(hash_and_copy_to_iter);
1587
Al Viro62a80672014-04-04 23:12:29 -04001588int iov_iter_npages(const struct iov_iter *i, int maxpages)
1589{
Al Viroe0f2dc42014-11-27 14:09:46 -05001590 size_t size = i->count;
1591 int npages = 0;
1592
1593 if (!size)
1594 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001595 if (unlikely(iov_iter_is_discard(i)))
1596 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001597
David Howells00e23702018-10-22 13:07:28 +01001598 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001599 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001600 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -04001601 size_t off;
Al Viro241699c2016-09-22 16:33:12 -04001602
1603 if (!sanity(i))
1604 return 0;
1605
David Howells8cefc102019-11-15 13:30:32 +00001606 data_start(i, &iter_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001607 /* some of this one + all after this one */
David Howells8cefc102019-11-15 13:30:32 +00001608 npages = pipe_space_for_user(iter_head, pipe->tail, pipe);
Al Viro241699c2016-09-22 16:33:12 -04001609 if (npages >= maxpages)
1610 return maxpages;
1611 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001612 unsigned long p = (unsigned long)v.iov_base;
1613 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1614 - p / PAGE_SIZE;
1615 if (npages >= maxpages)
1616 return maxpages;
1617 0;}),({
1618 npages++;
1619 if (npages >= maxpages)
1620 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001621 }),({
1622 unsigned long p = (unsigned long)v.iov_base;
1623 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1624 - p / PAGE_SIZE;
1625 if (npages >= maxpages)
1626 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001627 })
1628 )
1629 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001630}
Al Virof67da302014-03-19 01:16:16 -04001631EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001632
1633const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1634{
1635 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001636 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001637 WARN_ON(1);
1638 return NULL;
1639 }
David Howells9ea9ce02018-10-20 00:57:56 +01001640 if (unlikely(iov_iter_is_discard(new)))
1641 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001642 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001643 return new->bvec = kmemdup(new->bvec,
1644 new->nr_segs * sizeof(struct bio_vec),
1645 flags);
1646 else
1647 /* iovec and kvec have identical layout */
1648 return new->iov = kmemdup(new->iov,
1649 new->nr_segs * sizeof(struct iovec),
1650 flags);
1651}
1652EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001653
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001654static int copy_compat_iovec_from_user(struct iovec *iov,
1655 const struct iovec __user *uvec, unsigned long nr_segs)
1656{
1657 const struct compat_iovec __user *uiov =
1658 (const struct compat_iovec __user *)uvec;
1659 int ret = -EFAULT, i;
1660
1661 if (!user_access_begin(uvec, nr_segs * sizeof(*uvec)))
1662 return -EFAULT;
1663
1664 for (i = 0; i < nr_segs; i++) {
1665 compat_uptr_t buf;
1666 compat_ssize_t len;
1667
1668 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1669 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1670
1671 /* check for compat_size_t not fitting in compat_ssize_t .. */
1672 if (len < 0) {
1673 ret = -EINVAL;
1674 goto uaccess_end;
1675 }
1676 iov[i].iov_base = compat_ptr(buf);
1677 iov[i].iov_len = len;
1678 }
1679
1680 ret = 0;
1681uaccess_end:
1682 user_access_end();
1683 return ret;
1684}
1685
1686static int copy_iovec_from_user(struct iovec *iov,
1687 const struct iovec __user *uvec, unsigned long nr_segs)
David Laightfb041b52020-09-25 06:51:39 +02001688{
1689 unsigned long seg;
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001690
1691 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1692 return -EFAULT;
1693 for (seg = 0; seg < nr_segs; seg++) {
1694 if ((ssize_t)iov[seg].iov_len < 0)
1695 return -EINVAL;
1696 }
1697
1698 return 0;
1699}
1700
1701struct iovec *iovec_from_user(const struct iovec __user *uvec,
1702 unsigned long nr_segs, unsigned long fast_segs,
1703 struct iovec *fast_iov, bool compat)
1704{
1705 struct iovec *iov = fast_iov;
1706 int ret;
David Laightfb041b52020-09-25 06:51:39 +02001707
1708 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001709 * SuS says "The readv() function *may* fail if the iovcnt argument was
1710 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
David Laightfb041b52020-09-25 06:51:39 +02001711 * traditionally returned zero for zero segments, so...
1712 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001713 if (nr_segs == 0)
1714 return iov;
1715 if (nr_segs > UIO_MAXIOV)
1716 return ERR_PTR(-EINVAL);
David Laightfb041b52020-09-25 06:51:39 +02001717 if (nr_segs > fast_segs) {
1718 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001719 if (!iov)
1720 return ERR_PTR(-ENOMEM);
David Laightfb041b52020-09-25 06:51:39 +02001721 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001722
1723 if (compat)
1724 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1725 else
1726 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1727 if (ret) {
1728 if (iov != fast_iov)
1729 kfree(iov);
1730 return ERR_PTR(ret);
1731 }
1732
1733 return iov;
1734}
1735
1736ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1737 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1738 struct iov_iter *i, bool compat)
1739{
1740 ssize_t total_len = 0;
1741 unsigned long seg;
1742 struct iovec *iov;
1743
1744 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1745 if (IS_ERR(iov)) {
1746 *iovp = NULL;
1747 return PTR_ERR(iov);
David Laightfb041b52020-09-25 06:51:39 +02001748 }
1749
1750 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001751 * According to the Single Unix Specification we should return EINVAL if
1752 * an element length is < 0 when cast to ssize_t or if the total length
1753 * would overflow the ssize_t return value of the system call.
David Laightfb041b52020-09-25 06:51:39 +02001754 *
1755 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1756 * overflow case.
1757 */
David Laightfb041b52020-09-25 06:51:39 +02001758 for (seg = 0; seg < nr_segs; seg++) {
David Laightfb041b52020-09-25 06:51:39 +02001759 ssize_t len = (ssize_t)iov[seg].iov_len;
1760
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001761 if (!access_ok(iov[seg].iov_base, len)) {
1762 if (iov != *iovp)
1763 kfree(iov);
1764 *iovp = NULL;
1765 return -EFAULT;
David Laightfb041b52020-09-25 06:51:39 +02001766 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001767
1768 if (len > MAX_RW_COUNT - total_len) {
1769 len = MAX_RW_COUNT - total_len;
David Laightfb041b52020-09-25 06:51:39 +02001770 iov[seg].iov_len = len;
1771 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001772 total_len += len;
David Laightfb041b52020-09-25 06:51:39 +02001773 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001774
1775 iov_iter_init(i, type, iov, nr_segs, total_len);
1776 if (iov == *iovp)
1777 *iovp = NULL;
1778 else
1779 *iovp = iov;
1780 return total_len;
David Laightfb041b52020-09-25 06:51:39 +02001781}
1782
1783/**
Vegard Nossumffecee42016-10-08 11:18:07 +02001784 * import_iovec() - Copy an array of &struct iovec from userspace
1785 * into the kernel, check that it is valid, and initialize a new
1786 * &struct iov_iter iterator to access it.
1787 *
1788 * @type: One of %READ or %WRITE.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001789 * @uvec: Pointer to the userspace array.
Vegard Nossumffecee42016-10-08 11:18:07 +02001790 * @nr_segs: Number of elements in userspace array.
1791 * @fast_segs: Number of elements in @iov.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001792 * @iovp: (input and output parameter) Pointer to pointer to (usually small
Vegard Nossumffecee42016-10-08 11:18:07 +02001793 * on-stack) kernel array.
1794 * @i: Pointer to iterator that will be initialized on success.
1795 *
1796 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1797 * then this function places %NULL in *@iov on return. Otherwise, a new
1798 * array will be allocated and the result placed in *@iov. This means that
1799 * the caller may call kfree() on *@iov regardless of whether the small
1800 * on-stack array was used or not (and regardless of whether this function
1801 * returns an error or not).
1802 *
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001803 * Return: Negative error code on error, bytes imported on success
Vegard Nossumffecee42016-10-08 11:18:07 +02001804 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001805ssize_t import_iovec(int type, const struct iovec __user *uvec,
Al Virobc917be2015-03-21 17:45:43 -04001806 unsigned nr_segs, unsigned fast_segs,
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001807 struct iovec **iovp, struct iov_iter *i)
Al Virobc917be2015-03-21 17:45:43 -04001808{
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02001809 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1810 in_compat_syscall());
Al Virobc917be2015-03-21 17:45:43 -04001811}
1812EXPORT_SYMBOL(import_iovec);
1813
Al Virobc917be2015-03-21 17:45:43 -04001814int import_single_range(int rw, void __user *buf, size_t len,
1815 struct iovec *iov, struct iov_iter *i)
1816{
1817 if (len > MAX_RW_COUNT)
1818 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001819 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04001820 return -EFAULT;
1821
1822 iov->iov_base = buf;
1823 iov->iov_len = len;
1824 iov_iter_init(i, rw, iov, 1, len);
1825 return 0;
1826}
Al Viroe1267582015-12-06 20:38:56 -05001827EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05001828
1829int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1830 int (*f)(struct kvec *vec, void *context),
1831 void *context)
1832{
1833 struct kvec w;
1834 int err = -EINVAL;
1835 if (!bytes)
1836 return 0;
1837
1838 iterate_all_kinds(i, bytes, v, -EINVAL, ({
1839 w.iov_base = kmap(v.bv_page) + v.bv_offset;
1840 w.iov_len = v.bv_len;
1841 err = f(&w, context);
1842 kunmap(v.bv_page);
1843 err;}), ({
1844 w = v;
1845 err = f(&w, context);})
1846 )
1847 return err;
1848}
1849EXPORT_SYMBOL(iov_iter_for_each_range);