blob: b364231b5fc8cc629f3cdf496e2b3a72ec5efbd3 [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>
Albert van der Linde4d0e9df2020-10-15 20:13:50 -07005#include <linux/fault-inject-usercopy.h>
Al Viro4f18cd32014-02-05 19:11:33 -05006#include <linux/uio.h>
7#include <linux/pagemap.h>
Al Viro91f79c42014-03-21 04:58:33 -04008#include <linux/slab.h>
9#include <linux/vmalloc.h>
Al Viro241699c2016-09-22 16:33:12 -040010#include <linux/splice.h>
Christoph Hellwigbfdc5972020-09-25 06:51:40 +020011#include <linux/compat.h>
Al Viroa604ec72014-11-24 01:08:00 -050012#include <net/checksum.h>
Sagi Grimbergd05f4432018-12-03 17:52:09 -080013#include <linux/scatterlist.h>
Marco Elverd0ef4c32020-01-21 17:05:11 +010014#include <linux/instrumented.h>
Al Viro4f18cd32014-02-05 19:11:33 -050015
Al Viro241699c2016-09-22 16:33:12 -040016#define PIPE_PARANOIA /* for now */
17
Al Viro04a31162014-11-27 13:51:41 -050018#define iterate_iovec(i, n, __v, __p, skip, STEP) { \
19 size_t left; \
20 size_t wanted = n; \
21 __p = i->iov; \
22 __v.iov_len = min(n, __p->iov_len - skip); \
23 if (likely(__v.iov_len)) { \
24 __v.iov_base = __p->iov_base + skip; \
25 left = (STEP); \
26 __v.iov_len -= left; \
27 skip += __v.iov_len; \
28 n -= __v.iov_len; \
29 } else { \
30 left = 0; \
31 } \
32 while (unlikely(!left && n)) { \
33 __p++; \
34 __v.iov_len = min(n, __p->iov_len); \
35 if (unlikely(!__v.iov_len)) \
36 continue; \
37 __v.iov_base = __p->iov_base; \
38 left = (STEP); \
39 __v.iov_len -= left; \
40 skip = __v.iov_len; \
41 n -= __v.iov_len; \
42 } \
43 n = wanted - n; \
44}
45
Al Viroa2804552014-11-27 14:48:42 -050046#define iterate_kvec(i, n, __v, __p, skip, STEP) { \
47 size_t wanted = n; \
48 __p = i->kvec; \
49 __v.iov_len = min(n, __p->iov_len - skip); \
50 if (likely(__v.iov_len)) { \
51 __v.iov_base = __p->iov_base + skip; \
52 (void)(STEP); \
53 skip += __v.iov_len; \
54 n -= __v.iov_len; \
55 } \
56 while (unlikely(n)) { \
57 __p++; \
58 __v.iov_len = min(n, __p->iov_len); \
59 if (unlikely(!__v.iov_len)) \
60 continue; \
61 __v.iov_base = __p->iov_base; \
62 (void)(STEP); \
63 skip = __v.iov_len; \
64 n -= __v.iov_len; \
65 } \
66 n = wanted; \
67}
68
Ming Lei1bdc76a2016-05-30 21:34:32 +080069#define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
70 struct bvec_iter __start; \
71 __start.bi_size = n; \
72 __start.bi_bvec_done = skip; \
73 __start.bi_idx = 0; \
74 for_each_bvec(__v, i->bvec, __bi, __start) { \
75 if (!__v.bv_len) \
Al Viro04a31162014-11-27 13:51:41 -050076 continue; \
Al Viro04a31162014-11-27 13:51:41 -050077 (void)(STEP); \
Al Viro04a31162014-11-27 13:51:41 -050078 } \
Al Viro04a31162014-11-27 13:51:41 -050079}
80
Al Viroa2804552014-11-27 14:48:42 -050081#define iterate_all_kinds(i, n, v, I, B, K) { \
Al Viro33844e62016-12-21 21:55:02 -050082 if (likely(n)) { \
83 size_t skip = i->iov_offset; \
84 if (unlikely(i->type & ITER_BVEC)) { \
85 struct bio_vec v; \
86 struct bvec_iter __bi; \
87 iterate_bvec(i, n, v, __bi, skip, (B)) \
88 } else if (unlikely(i->type & ITER_KVEC)) { \
89 const struct kvec *kvec; \
90 struct kvec v; \
91 iterate_kvec(i, n, v, kvec, skip, (K)) \
David Howells9ea9ce02018-10-20 00:57:56 +010092 } else if (unlikely(i->type & ITER_DISCARD)) { \
Al Viro33844e62016-12-21 21:55:02 -050093 } else { \
94 const struct iovec *iov; \
95 struct iovec v; \
96 iterate_iovec(i, n, v, iov, skip, (I)) \
97 } \
Al Viro04a31162014-11-27 13:51:41 -050098 } \
99}
100
Al Viroa2804552014-11-27 14:48:42 -0500101#define iterate_and_advance(i, n, v, I, B, K) { \
Al Virodd254f52016-05-09 11:54:48 -0400102 if (unlikely(i->count < n)) \
103 n = i->count; \
Al Viro19f18452016-05-25 17:36:19 -0400104 if (i->count) { \
Al Virodd254f52016-05-09 11:54:48 -0400105 size_t skip = i->iov_offset; \
106 if (unlikely(i->type & ITER_BVEC)) { \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800107 const struct bio_vec *bvec = i->bvec; \
Al Virodd254f52016-05-09 11:54:48 -0400108 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800109 struct bvec_iter __bi; \
110 iterate_bvec(i, n, v, __bi, skip, (B)) \
111 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
112 i->nr_segs -= i->bvec - bvec; \
113 skip = __bi.bi_bvec_done; \
Al Virodd254f52016-05-09 11:54:48 -0400114 } else if (unlikely(i->type & ITER_KVEC)) { \
115 const struct kvec *kvec; \
116 struct kvec v; \
117 iterate_kvec(i, n, v, kvec, skip, (K)) \
118 if (skip == kvec->iov_len) { \
119 kvec++; \
120 skip = 0; \
121 } \
122 i->nr_segs -= kvec - i->kvec; \
123 i->kvec = kvec; \
David Howells9ea9ce02018-10-20 00:57:56 +0100124 } else if (unlikely(i->type & ITER_DISCARD)) { \
125 skip += n; \
Al Virodd254f52016-05-09 11:54:48 -0400126 } else { \
127 const struct iovec *iov; \
128 struct iovec v; \
129 iterate_iovec(i, n, v, iov, skip, (I)) \
130 if (skip == iov->iov_len) { \
131 iov++; \
132 skip = 0; \
133 } \
134 i->nr_segs -= iov - i->iov; \
135 i->iov = iov; \
Al Viro7ce2a912014-11-27 13:59:45 -0500136 } \
Al Virodd254f52016-05-09 11:54:48 -0400137 i->count -= n; \
138 i->iov_offset = skip; \
Al Viro7ce2a912014-11-27 13:59:45 -0500139 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500140}
141
Al Viro09fc68dc2017-06-29 22:25:14 -0400142static int copyout(void __user *to, const void *from, size_t n)
143{
Albert van der Linde4d0e9df2020-10-15 20:13:50 -0700144 if (should_fail_usercopy())
145 return n;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800146 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100147 instrument_copy_to_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400148 n = raw_copy_to_user(to, from, n);
149 }
150 return n;
151}
152
153static int copyin(void *to, const void __user *from, size_t n)
154{
Albert van der Linde4d0e9df2020-10-15 20:13:50 -0700155 if (should_fail_usercopy())
156 return n;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800157 if (access_ok(from, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100158 instrument_copy_from_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400159 n = raw_copy_from_user(to, from, n);
160 }
161 return n;
162}
163
Al Viro62a80672014-04-04 23:12:29 -0400164static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500165 struct iov_iter *i)
166{
167 size_t skip, copy, left, wanted;
168 const struct iovec *iov;
169 char __user *buf;
170 void *kaddr, *from;
171
172 if (unlikely(bytes > i->count))
173 bytes = i->count;
174
175 if (unlikely(!bytes))
176 return 0;
177
Al Viro09fc68dc2017-06-29 22:25:14 -0400178 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500179 wanted = bytes;
180 iov = i->iov;
181 skip = i->iov_offset;
182 buf = iov->iov_base + skip;
183 copy = min(bytes, iov->iov_len - skip);
184
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700185 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500186 kaddr = kmap_atomic(page);
187 from = kaddr + offset;
188
189 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400190 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500191 copy -= left;
192 skip += copy;
193 from += copy;
194 bytes -= copy;
195
196 while (unlikely(!left && bytes)) {
197 iov++;
198 buf = iov->iov_base;
199 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400200 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500201 copy -= left;
202 skip = copy;
203 from += copy;
204 bytes -= copy;
205 }
206 if (likely(!bytes)) {
207 kunmap_atomic(kaddr);
208 goto done;
209 }
210 offset = from - kaddr;
211 buf += copy;
212 kunmap_atomic(kaddr);
213 copy = min(bytes, iov->iov_len - skip);
214 }
215 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700216
Al Viro4f18cd32014-02-05 19:11:33 -0500217 kaddr = kmap(page);
218 from = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400219 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500220 copy -= left;
221 skip += copy;
222 from += copy;
223 bytes -= copy;
224 while (unlikely(!left && bytes)) {
225 iov++;
226 buf = iov->iov_base;
227 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400228 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500229 copy -= left;
230 skip = copy;
231 from += copy;
232 bytes -= copy;
233 }
234 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700235
Al Viro4f18cd32014-02-05 19:11:33 -0500236done:
Al Viro81055e52014-04-04 19:23:46 -0400237 if (skip == iov->iov_len) {
238 iov++;
239 skip = 0;
240 }
Al Viro4f18cd32014-02-05 19:11:33 -0500241 i->count -= wanted - bytes;
242 i->nr_segs -= iov - i->iov;
243 i->iov = iov;
244 i->iov_offset = skip;
245 return wanted - bytes;
246}
Al Viro4f18cd32014-02-05 19:11:33 -0500247
Al Viro62a80672014-04-04 23:12:29 -0400248static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400249 struct iov_iter *i)
250{
251 size_t skip, copy, left, wanted;
252 const struct iovec *iov;
253 char __user *buf;
254 void *kaddr, *to;
255
256 if (unlikely(bytes > i->count))
257 bytes = i->count;
258
259 if (unlikely(!bytes))
260 return 0;
261
Al Viro09fc68dc2017-06-29 22:25:14 -0400262 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400263 wanted = bytes;
264 iov = i->iov;
265 skip = i->iov_offset;
266 buf = iov->iov_base + skip;
267 copy = min(bytes, iov->iov_len - skip);
268
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700269 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400270 kaddr = kmap_atomic(page);
271 to = kaddr + offset;
272
273 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400274 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400275 copy -= left;
276 skip += copy;
277 to += copy;
278 bytes -= copy;
279
280 while (unlikely(!left && bytes)) {
281 iov++;
282 buf = iov->iov_base;
283 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400284 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400285 copy -= left;
286 skip = copy;
287 to += copy;
288 bytes -= copy;
289 }
290 if (likely(!bytes)) {
291 kunmap_atomic(kaddr);
292 goto done;
293 }
294 offset = to - kaddr;
295 buf += copy;
296 kunmap_atomic(kaddr);
297 copy = min(bytes, iov->iov_len - skip);
298 }
299 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700300
Al Virof0d1bec2014-04-03 15:05:18 -0400301 kaddr = kmap(page);
302 to = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400303 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400304 copy -= left;
305 skip += copy;
306 to += copy;
307 bytes -= copy;
308 while (unlikely(!left && bytes)) {
309 iov++;
310 buf = iov->iov_base;
311 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400312 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400313 copy -= left;
314 skip = copy;
315 to += copy;
316 bytes -= copy;
317 }
318 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700319
Al Virof0d1bec2014-04-03 15:05:18 -0400320done:
Al Viro81055e52014-04-04 19:23:46 -0400321 if (skip == iov->iov_len) {
322 iov++;
323 skip = 0;
324 }
Al Virof0d1bec2014-04-03 15:05:18 -0400325 i->count -= wanted - bytes;
326 i->nr_segs -= iov - i->iov;
327 i->iov = iov;
328 i->iov_offset = skip;
329 return wanted - bytes;
330}
Al Virof0d1bec2014-04-03 15:05:18 -0400331
Al Viro241699c2016-09-22 16:33:12 -0400332#ifdef PIPE_PARANOIA
333static bool sanity(const struct iov_iter *i)
334{
335 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000336 unsigned int p_head = pipe->head;
337 unsigned int p_tail = pipe->tail;
338 unsigned int p_mask = pipe->ring_size - 1;
339 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
340 unsigned int i_head = i->head;
341 unsigned int idx;
342
Al Viro241699c2016-09-22 16:33:12 -0400343 if (i->iov_offset) {
344 struct pipe_buffer *p;
David Howells8cefc102019-11-15 13:30:32 +0000345 if (unlikely(p_occupancy == 0))
Al Viro241699c2016-09-22 16:33:12 -0400346 goto Bad; // pipe must be non-empty
David Howells8cefc102019-11-15 13:30:32 +0000347 if (unlikely(i_head != p_head - 1))
Al Viro241699c2016-09-22 16:33:12 -0400348 goto Bad; // must be at the last buffer...
349
David Howells8cefc102019-11-15 13:30:32 +0000350 p = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400351 if (unlikely(p->offset + p->len != i->iov_offset))
352 goto Bad; // ... at the end of segment
353 } else {
David Howells8cefc102019-11-15 13:30:32 +0000354 if (i_head != p_head)
Al Viro241699c2016-09-22 16:33:12 -0400355 goto Bad; // must be right after the last buffer
356 }
357 return true;
358Bad:
David Howells8cefc102019-11-15 13:30:32 +0000359 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
360 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
361 p_head, p_tail, pipe->ring_size);
362 for (idx = 0; idx < pipe->ring_size; idx++)
Al Viro241699c2016-09-22 16:33:12 -0400363 printk(KERN_ERR "[%p %p %d %d]\n",
364 pipe->bufs[idx].ops,
365 pipe->bufs[idx].page,
366 pipe->bufs[idx].offset,
367 pipe->bufs[idx].len);
368 WARN_ON(1);
369 return false;
370}
371#else
372#define sanity(i) true
373#endif
374
Al Viro241699c2016-09-22 16:33:12 -0400375static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
376 struct iov_iter *i)
377{
378 struct pipe_inode_info *pipe = i->pipe;
379 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +0000380 unsigned int p_tail = pipe->tail;
381 unsigned int p_mask = pipe->ring_size - 1;
382 unsigned int i_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400383 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400384
385 if (unlikely(bytes > i->count))
386 bytes = i->count;
387
388 if (unlikely(!bytes))
389 return 0;
390
391 if (!sanity(i))
392 return 0;
393
394 off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000395 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400396 if (off) {
397 if (offset == off && buf->page == page) {
398 /* merge with the last one */
399 buf->len += bytes;
400 i->iov_offset += bytes;
401 goto out;
402 }
David Howells8cefc102019-11-15 13:30:32 +0000403 i_head++;
404 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400405 }
David Howells6718b6f2019-10-16 16:47:32 +0100406 if (pipe_full(i_head, p_tail, pipe->max_usage))
Al Viro241699c2016-09-22 16:33:12 -0400407 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000408
Al Viro241699c2016-09-22 16:33:12 -0400409 buf->ops = &page_cache_pipe_buf_ops;
David Howells8cefc102019-11-15 13:30:32 +0000410 get_page(page);
411 buf->page = page;
Al Viro241699c2016-09-22 16:33:12 -0400412 buf->offset = offset;
413 buf->len = bytes;
David Howells8cefc102019-11-15 13:30:32 +0000414
415 pipe->head = i_head + 1;
Al Viro241699c2016-09-22 16:33:12 -0400416 i->iov_offset = offset + bytes;
David Howells8cefc102019-11-15 13:30:32 +0000417 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400418out:
419 i->count -= bytes;
420 return bytes;
421}
422
Al Viro4f18cd32014-02-05 19:11:33 -0500423/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400424 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
425 * bytes. For each iovec, fault in each page that constitutes the iovec.
426 *
427 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
428 * because it is an invalid address).
429 */
Al Virod4690f12016-09-16 00:11:45 +0100430int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400431{
432 size_t skip = i->iov_offset;
433 const struct iovec *iov;
434 int err;
435 struct iovec v;
436
Al Viro7b0393e2021-06-02 14:48:21 -0400437 if (iter_is_iovec(i)) {
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400438 iterate_iovec(i, bytes, v, iov, skip, ({
Al Viro4bce9f6e2016-09-17 18:02:44 -0400439 err = fault_in_pages_readable(v.iov_base, v.iov_len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400440 if (unlikely(err))
441 return err;
442 0;}))
443 }
444 return 0;
445}
Al Virod4690f12016-09-16 00:11:45 +0100446EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400447
David Howellsaa563d72018-10-20 00:57:56 +0100448void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500449 const struct iovec *iov, unsigned long nr_segs,
450 size_t count)
451{
David Howellsaa563d72018-10-20 00:57:56 +0100452 WARN_ON(direction & ~(READ | WRITE));
453 direction &= READ | WRITE;
454
Al Viro71d8e532014-03-05 19:28:09 -0500455 /* It will get better. Eventually... */
Al Virodb68ce12017-03-20 21:08:07 -0400456 if (uaccess_kernel()) {
David Howellsaa563d72018-10-20 00:57:56 +0100457 i->type = ITER_KVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500458 i->kvec = (struct kvec *)iov;
459 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100460 i->type = ITER_IOVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500461 i->iov = iov;
462 }
Al Viro71d8e532014-03-05 19:28:09 -0500463 i->nr_segs = nr_segs;
464 i->iov_offset = 0;
465 i->count = count;
466}
467EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400468
Al Viro62a80672014-04-04 23:12:29 -0400469static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
470{
471 char *from = kmap_atomic(page);
472 memcpy(to, from + offset, len);
473 kunmap_atomic(from);
474}
475
Al Viro36f7a8a2015-12-06 16:49:22 -0500476static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
Al Viro62a80672014-04-04 23:12:29 -0400477{
478 char *to = kmap_atomic(page);
479 memcpy(to + offset, from, len);
480 kunmap_atomic(to);
481}
482
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400483static void memzero_page(struct page *page, size_t offset, size_t len)
484{
485 char *addr = kmap_atomic(page);
486 memset(addr + offset, 0, len);
487 kunmap_atomic(addr);
488}
489
Al Viro241699c2016-09-22 16:33:12 -0400490static inline bool allocated(struct pipe_buffer *buf)
491{
492 return buf->ops == &default_pipe_buf_ops;
493}
494
David Howells8cefc102019-11-15 13:30:32 +0000495static inline void data_start(const struct iov_iter *i,
496 unsigned int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400497{
David Howells8cefc102019-11-15 13:30:32 +0000498 unsigned int p_mask = i->pipe->ring_size - 1;
499 unsigned int iter_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400500 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000501
502 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
503 off == PAGE_SIZE)) {
504 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400505 off = 0;
506 }
David Howells8cefc102019-11-15 13:30:32 +0000507 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400508 *offp = off;
509}
510
511static size_t push_pipe(struct iov_iter *i, size_t size,
David Howells8cefc102019-11-15 13:30:32 +0000512 int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400513{
514 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000515 unsigned int p_tail = pipe->tail;
516 unsigned int p_mask = pipe->ring_size - 1;
517 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400518 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400519 ssize_t left;
520
521 if (unlikely(size > i->count))
522 size = i->count;
523 if (unlikely(!size))
524 return 0;
525
526 left = size;
David Howells8cefc102019-11-15 13:30:32 +0000527 data_start(i, &iter_head, &off);
528 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400529 *offp = off;
530 if (off) {
531 left -= PAGE_SIZE - off;
532 if (left <= 0) {
David Howells8cefc102019-11-15 13:30:32 +0000533 pipe->bufs[iter_head & p_mask].len += size;
Al Viro241699c2016-09-22 16:33:12 -0400534 return size;
535 }
David Howells8cefc102019-11-15 13:30:32 +0000536 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
537 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400538 }
David Howells6718b6f2019-10-16 16:47:32 +0100539 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000540 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400541 struct page *page = alloc_page(GFP_USER);
542 if (!page)
543 break;
David Howells8cefc102019-11-15 13:30:32 +0000544
545 buf->ops = &default_pipe_buf_ops;
546 buf->page = page;
547 buf->offset = 0;
548 buf->len = min_t(ssize_t, left, PAGE_SIZE);
549 left -= buf->len;
550 iter_head++;
551 pipe->head = iter_head;
552
553 if (left == 0)
Al Viro241699c2016-09-22 16:33:12 -0400554 return size;
Al Viro241699c2016-09-22 16:33:12 -0400555 }
556 return size - left;
557}
558
559static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
560 struct iov_iter *i)
561{
562 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000563 unsigned int p_mask = pipe->ring_size - 1;
564 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400565 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400566
567 if (!sanity(i))
568 return 0;
569
David Howells8cefc102019-11-15 13:30:32 +0000570 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400571 if (unlikely(!n))
572 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000573 do {
Al Viro241699c2016-09-22 16:33:12 -0400574 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000575 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
576 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400577 i->iov_offset = off + chunk;
578 n -= chunk;
579 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000580 off = 0;
581 i_head++;
582 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400583 i->count -= bytes;
584 return bytes;
585}
586
Al Virof9152892018-11-27 22:32:59 -0500587static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
588 __wsum sum, size_t off)
589{
Al Virocc44c172020-07-11 00:12:07 -0400590 __wsum next = csum_partial_copy_nocheck(from, to, len);
Al Virof9152892018-11-27 22:32:59 -0500591 return csum_block_add(sum, next, off);
592}
593
Al Viro78e1f382018-11-25 16:24:16 -0500594static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
Willem de Bruijn46a831d2021-02-03 14:29:52 -0500595 struct csum_state *csstate,
596 struct iov_iter *i)
Al Viro78e1f382018-11-25 16:24:16 -0500597{
598 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000599 unsigned int p_mask = pipe->ring_size - 1;
Willem de Bruijn46a831d2021-02-03 14:29:52 -0500600 __wsum sum = csstate->csum;
601 size_t off = csstate->off;
David Howells8cefc102019-11-15 13:30:32 +0000602 unsigned int i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500603 size_t n, r;
Al Viro78e1f382018-11-25 16:24:16 -0500604
605 if (!sanity(i))
606 return 0;
607
David Howells8cefc102019-11-15 13:30:32 +0000608 bytes = n = push_pipe(i, bytes, &i_head, &r);
Al Viro78e1f382018-11-25 16:24:16 -0500609 if (unlikely(!n))
610 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000611 do {
Al Viro78e1f382018-11-25 16:24:16 -0500612 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
David Howells8cefc102019-11-15 13:30:32 +0000613 char *p = kmap_atomic(pipe->bufs[i_head & p_mask].page);
Al Virof9152892018-11-27 22:32:59 -0500614 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
Al Viro78e1f382018-11-25 16:24:16 -0500615 kunmap_atomic(p);
David Howells8cefc102019-11-15 13:30:32 +0000616 i->head = i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500617 i->iov_offset = r + chunk;
618 n -= chunk;
619 off += chunk;
620 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000621 r = 0;
622 i_head++;
623 } while (n);
Al Viro78e1f382018-11-25 16:24:16 -0500624 i->count -= bytes;
Willem de Bruijn46a831d2021-02-03 14:29:52 -0500625 csstate->csum = sum;
626 csstate->off = off;
Al Viro78e1f382018-11-25 16:24:16 -0500627 return bytes;
628}
629
Al Viroaa28de22017-06-29 21:45:10 -0400630size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400631{
Al Viro36f7a8a2015-12-06 16:49:22 -0500632 const char *from = addr;
David Howells00e23702018-10-22 13:07:28 +0100633 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400634 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400635 if (iter_is_iovec(i))
636 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500637 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400638 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500639 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500640 (from += v.bv_len) - v.bv_len, v.bv_len),
641 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500642 )
Al Viro62a80672014-04-04 23:12:29 -0400643
Al Viro3d4d3e42014-11-27 14:28:06 -0500644 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400645}
Al Viroaa28de22017-06-29 21:45:10 -0400646EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400647
Dan Williamsec6347b2020-10-05 20:40:16 -0700648#ifdef CONFIG_ARCH_HAS_COPY_MC
649static int copyout_mc(void __user *to, const void *from, size_t n)
Dan Williams87803562018-05-03 17:06:31 -0700650{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800651 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100652 instrument_copy_to_user(to, from, n);
Dan Williamsec6347b2020-10-05 20:40:16 -0700653 n = copy_mc_to_user((__force void *) to, from, n);
Dan Williams87803562018-05-03 17:06:31 -0700654 }
655 return n;
656}
657
Dan Williamsec6347b2020-10-05 20:40:16 -0700658static unsigned long copy_mc_to_page(struct page *page, size_t offset,
Dan Williams87803562018-05-03 17:06:31 -0700659 const char *from, size_t len)
660{
661 unsigned long ret;
662 char *to;
663
664 to = kmap_atomic(page);
Dan Williamsec6347b2020-10-05 20:40:16 -0700665 ret = copy_mc_to_kernel(to + offset, from, len);
Dan Williams87803562018-05-03 17:06:31 -0700666 kunmap_atomic(to);
667
668 return ret;
669}
670
Dan Williamsec6347b2020-10-05 20:40:16 -0700671static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
Dan Williamsca146f62018-07-08 13:46:12 -0700672 struct iov_iter *i)
673{
674 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000675 unsigned int p_mask = pipe->ring_size - 1;
676 unsigned int i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700677 size_t n, off, xfer = 0;
Dan Williamsca146f62018-07-08 13:46:12 -0700678
679 if (!sanity(i))
680 return 0;
681
David Howells8cefc102019-11-15 13:30:32 +0000682 bytes = n = push_pipe(i, bytes, &i_head, &off);
Dan Williamsca146f62018-07-08 13:46:12 -0700683 if (unlikely(!n))
684 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000685 do {
Dan Williamsca146f62018-07-08 13:46:12 -0700686 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
687 unsigned long rem;
688
Dan Williamsec6347b2020-10-05 20:40:16 -0700689 rem = copy_mc_to_page(pipe->bufs[i_head & p_mask].page,
David Howells8cefc102019-11-15 13:30:32 +0000690 off, addr, chunk);
691 i->head = i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700692 i->iov_offset = off + chunk - rem;
693 xfer += chunk - rem;
694 if (rem)
695 break;
696 n -= chunk;
697 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000698 off = 0;
699 i_head++;
700 } while (n);
Dan Williamsca146f62018-07-08 13:46:12 -0700701 i->count -= xfer;
702 return xfer;
703}
704
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700705/**
Dan Williamsec6347b2020-10-05 20:40:16 -0700706 * _copy_mc_to_iter - copy to iter with source memory error exception handling
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700707 * @addr: source kernel address
708 * @bytes: total transfer length
709 * @iter: destination iterator
710 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700711 * The pmem driver deploys this for the dax operation
712 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
713 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
714 * successfully copied.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700715 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700716 * The main differences between this and typical _copy_to_iter().
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700717 *
718 * * Typical tail/residue handling after a fault retries the copy
719 * byte-by-byte until the fault happens again. Re-triggering machine
720 * checks is potentially fatal so the implementation uses source
721 * alignment and poison alignment assumptions to avoid re-triggering
722 * hardware exceptions.
723 *
724 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
725 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
726 * a short copy.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700727 */
Dan Williamsec6347b2020-10-05 20:40:16 -0700728size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Dan Williams87803562018-05-03 17:06:31 -0700729{
730 const char *from = addr;
731 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
732
David Howells00e23702018-10-22 13:07:28 +0100733 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsec6347b2020-10-05 20:40:16 -0700734 return copy_mc_pipe_to_iter(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700735 if (iter_is_iovec(i))
736 might_fault();
737 iterate_and_advance(i, bytes, v,
Dan Williamsec6347b2020-10-05 20:40:16 -0700738 copyout_mc(v.iov_base, (from += v.iov_len) - v.iov_len,
739 v.iov_len),
Dan Williams87803562018-05-03 17:06:31 -0700740 ({
Dan Williamsec6347b2020-10-05 20:40:16 -0700741 rem = copy_mc_to_page(v.bv_page, v.bv_offset,
742 (from += v.bv_len) - v.bv_len, v.bv_len);
Dan Williams87803562018-05-03 17:06:31 -0700743 if (rem) {
744 curr_addr = (unsigned long) from;
745 bytes = curr_addr - s_addr - rem;
746 return bytes;
747 }
748 }),
749 ({
Dan Williamsec6347b2020-10-05 20:40:16 -0700750 rem = copy_mc_to_kernel(v.iov_base, (from += v.iov_len)
751 - v.iov_len, v.iov_len);
Dan Williams87803562018-05-03 17:06:31 -0700752 if (rem) {
753 curr_addr = (unsigned long) from;
754 bytes = curr_addr - s_addr - rem;
755 return bytes;
756 }
757 })
758 )
759
760 return bytes;
761}
Dan Williamsec6347b2020-10-05 20:40:16 -0700762EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
763#endif /* CONFIG_ARCH_HAS_COPY_MC */
Dan Williams87803562018-05-03 17:06:31 -0700764
Al Viroaa28de22017-06-29 21:45:10 -0400765size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400766{
Al Viro0dbca9a2014-11-27 14:26:43 -0500767 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100768 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400769 WARN_ON(1);
770 return 0;
771 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400772 if (iter_is_iovec(i))
773 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500774 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400775 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500776 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500777 v.bv_offset, v.bv_len),
778 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500779 )
780
781 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400782}
Al Viroaa28de22017-06-29 21:45:10 -0400783EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400784
Al Viroaa28de22017-06-29 21:45:10 -0400785bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400786{
787 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100788 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400789 WARN_ON(1);
790 return false;
791 }
Al Viro33844e62016-12-21 21:55:02 -0500792 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400793 return false;
794
Al Viro09fc68dc2017-06-29 22:25:14 -0400795 if (iter_is_iovec(i))
796 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400797 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68dc2017-06-29 22:25:14 -0400798 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400799 v.iov_base, v.iov_len))
800 return false;
801 0;}),
802 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
803 v.bv_offset, v.bv_len),
804 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
805 )
806
807 iov_iter_advance(i, bytes);
808 return true;
809}
Al Viroaa28de22017-06-29 21:45:10 -0400810EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400811
Al Viroaa28de22017-06-29 21:45:10 -0400812size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500813{
814 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100815 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400816 WARN_ON(1);
817 return 0;
818 }
Al Viroaa583092014-11-27 20:27:08 -0500819 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400820 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500821 v.iov_base, v.iov_len),
822 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
823 v.bv_offset, v.bv_len),
824 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
825 )
826
827 return bytes;
828}
Al Viroaa28de22017-06-29 21:45:10 -0400829EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500830
Dan Williams0aed55a2017-05-29 12:22:50 -0700831#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700832/**
833 * _copy_from_iter_flushcache - write destination through cpu cache
834 * @addr: destination kernel address
835 * @bytes: total transfer length
836 * @iter: source iterator
837 *
838 * The pmem driver arranges for filesystem-dax to use this facility via
839 * dax_copy_from_iter() for ensuring that writes to persistent memory
840 * are flushed through the CPU cache. It is differentiated from
841 * _copy_from_iter_nocache() in that guarantees all data is flushed for
842 * all iterator types. The _copy_from_iter_nocache() only attempts to
843 * bypass the cache for the ITER_IOVEC case, and on some archs may use
844 * instructions that strand dirty-data in the cache.
845 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700846size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700847{
848 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100849 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700850 WARN_ON(1);
851 return 0;
852 }
853 iterate_and_advance(i, bytes, v,
854 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
855 v.iov_base, v.iov_len),
856 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
857 v.bv_offset, v.bv_len),
858 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
859 v.iov_len)
860 )
861
862 return bytes;
863}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700864EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700865#endif
866
Al Viroaa28de22017-06-29 21:45:10 -0400867bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400868{
869 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100870 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400871 WARN_ON(1);
872 return false;
873 }
Al Viro33844e62016-12-21 21:55:02 -0500874 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400875 return false;
876 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400877 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400878 v.iov_base, v.iov_len))
879 return false;
880 0;}),
881 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
882 v.bv_offset, v.bv_len),
883 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
884 )
885
886 iov_iter_advance(i, bytes);
887 return true;
888}
Al Viroaa28de22017-06-29 21:45:10 -0400889EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400890
Al Viro72e809e2017-06-29 21:52:57 -0400891static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
892{
Eric Dumazet6daef952019-02-26 10:42:39 -0800893 struct page *head;
894 size_t v = n + offset;
895
896 /*
897 * The general case needs to access the page order in order
898 * to compute the page size.
899 * However, we mostly deal with order-0 pages and thus can
900 * avoid a possible cache line miss for requests that fit all
901 * page orders.
902 */
903 if (n <= v && v <= PAGE_SIZE)
904 return true;
905
906 head = compound_head(page);
907 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700908
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700909 if (likely(n <= v && v <= (page_size(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400910 return true;
911 WARN_ON(1);
912 return false;
913}
Al Virod2715242014-11-27 14:22:37 -0500914
915size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
916 struct iov_iter *i)
917{
Al Viro72e809e2017-06-29 21:52:57 -0400918 if (unlikely(!page_copy_sane(page, offset, bytes)))
919 return 0;
Al Virod2715242014-11-27 14:22:37 -0500920 if (i->type & (ITER_BVEC|ITER_KVEC)) {
921 void *kaddr = kmap_atomic(page);
922 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
923 kunmap_atomic(kaddr);
924 return wanted;
Al Virob6df9e42021-04-27 12:34:04 -0400925 } else if (unlikely(iov_iter_is_discard(i))) {
926 if (unlikely(i->count < bytes))
927 bytes = i->count;
928 i->count -= bytes;
David Howells9ea9ce02018-10-20 00:57:56 +0100929 return bytes;
Al Virob6df9e42021-04-27 12:34:04 -0400930 } else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500931 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400932 else
933 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500934}
935EXPORT_SYMBOL(copy_page_to_iter);
936
937size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
938 struct iov_iter *i)
939{
Al Viro72e809e2017-06-29 21:52:57 -0400940 if (unlikely(!page_copy_sane(page, offset, bytes)))
941 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +0100942 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400943 WARN_ON(1);
944 return 0;
945 }
Al Virod2715242014-11-27 14:22:37 -0500946 if (i->type & (ITER_BVEC|ITER_KVEC)) {
947 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400948 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500949 kunmap_atomic(kaddr);
950 return wanted;
951 } else
952 return copy_page_from_iter_iovec(page, offset, bytes, i);
953}
954EXPORT_SYMBOL(copy_page_from_iter);
955
Al Viro241699c2016-09-22 16:33:12 -0400956static size_t pipe_zero(size_t bytes, struct iov_iter *i)
957{
958 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000959 unsigned int p_mask = pipe->ring_size - 1;
960 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400961 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400962
963 if (!sanity(i))
964 return 0;
965
David Howells8cefc102019-11-15 13:30:32 +0000966 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400967 if (unlikely(!n))
968 return 0;
969
David Howells8cefc102019-11-15 13:30:32 +0000970 do {
Al Viro241699c2016-09-22 16:33:12 -0400971 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000972 memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk);
973 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400974 i->iov_offset = off + chunk;
975 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000976 off = 0;
977 i_head++;
978 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400979 i->count -= bytes;
980 return bytes;
981}
982
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400983size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
984{
David Howells00e23702018-10-22 13:07:28 +0100985 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400986 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500987 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400988 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500989 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
990 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500991 )
992
993 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400994}
995EXPORT_SYMBOL(iov_iter_zero);
996
Al Viro62a80672014-04-04 23:12:29 -0400997size_t iov_iter_copy_from_user_atomic(struct page *page,
998 struct iov_iter *i, unsigned long offset, size_t bytes)
999{
Al Viro04a31162014-11-27 13:51:41 -05001000 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -04001001 if (unlikely(!page_copy_sane(page, offset, bytes))) {
1002 kunmap_atomic(kaddr);
1003 return 0;
1004 }
David Howells9ea9ce02018-10-20 00:57:56 +01001005 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001006 kunmap_atomic(kaddr);
1007 WARN_ON(1);
1008 return 0;
1009 }
Al Viro04a31162014-11-27 13:51:41 -05001010 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -04001011 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -05001012 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -05001013 v.bv_offset, v.bv_len),
1014 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -05001015 )
1016 kunmap_atomic(kaddr);
1017 return bytes;
Al Viro62a80672014-04-04 23:12:29 -04001018}
1019EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
1020
Al Virob9dc6f62017-01-14 19:33:08 -05001021static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -04001022{
1023 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001024 unsigned int p_tail = pipe->tail;
1025 unsigned int p_head = pipe->head;
1026 unsigned int p_mask = pipe->ring_size - 1;
1027
1028 if (!pipe_empty(p_head, p_tail)) {
1029 struct pipe_buffer *buf;
1030 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001031 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +00001032
Al Virob9dc6f62017-01-14 19:33:08 -05001033 if (off) {
David Howells8cefc102019-11-15 13:30:32 +00001034 buf = &pipe->bufs[i_head & p_mask];
1035 buf->len = off - buf->offset;
1036 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001037 }
David Howells8cefc102019-11-15 13:30:32 +00001038 while (p_head != i_head) {
1039 p_head--;
1040 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
Al Viro241699c2016-09-22 16:33:12 -04001041 }
David Howells8cefc102019-11-15 13:30:32 +00001042
1043 pipe->head = p_head;
Al Viro241699c2016-09-22 16:33:12 -04001044 }
Al Virob9dc6f62017-01-14 19:33:08 -05001045}
1046
1047static void pipe_advance(struct iov_iter *i, size_t size)
1048{
1049 struct pipe_inode_info *pipe = i->pipe;
1050 if (unlikely(i->count < size))
1051 size = i->count;
1052 if (size) {
1053 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +00001054 unsigned int p_mask = pipe->ring_size - 1;
1055 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001056 size_t off = i->iov_offset, left = size;
David Howells8cefc102019-11-15 13:30:32 +00001057
Al Virob9dc6f62017-01-14 19:33:08 -05001058 if (off) /* make it relative to the beginning of buffer */
David Howells8cefc102019-11-15 13:30:32 +00001059 left += off - pipe->bufs[i_head & p_mask].offset;
Al Virob9dc6f62017-01-14 19:33:08 -05001060 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001061 buf = &pipe->bufs[i_head & p_mask];
Al Virob9dc6f62017-01-14 19:33:08 -05001062 if (left <= buf->len)
1063 break;
1064 left -= buf->len;
David Howells8cefc102019-11-15 13:30:32 +00001065 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001066 }
David Howells8cefc102019-11-15 13:30:32 +00001067 i->head = i_head;
Al Virob9dc6f62017-01-14 19:33:08 -05001068 i->iov_offset = buf->offset + left;
1069 }
1070 i->count -= size;
1071 /* ... and discard everything past that point */
1072 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001073}
1074
Al Viro62a80672014-04-04 23:12:29 -04001075void iov_iter_advance(struct iov_iter *i, size_t size)
1076{
David Howells00e23702018-10-22 13:07:28 +01001077 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001078 pipe_advance(i, size);
1079 return;
1080 }
David Howells9ea9ce02018-10-20 00:57:56 +01001081 if (unlikely(iov_iter_is_discard(i))) {
1082 i->count -= size;
1083 return;
1084 }
Al Viroa2804552014-11-27 14:48:42 -05001085 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -04001086}
1087EXPORT_SYMBOL(iov_iter_advance);
1088
Al Viro27c0e372017-02-17 18:42:24 -05001089void iov_iter_revert(struct iov_iter *i, size_t unroll)
1090{
1091 if (!unroll)
1092 return;
Al Viro5b47d592017-05-08 13:54:47 -04001093 if (WARN_ON(unroll > MAX_RW_COUNT))
1094 return;
Al Viro27c0e372017-02-17 18:42:24 -05001095 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001096 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001097 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001098 unsigned int p_mask = pipe->ring_size - 1;
1099 unsigned int i_head = i->head;
Al Viro27c0e372017-02-17 18:42:24 -05001100 size_t off = i->iov_offset;
1101 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001102 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1103 size_t n = off - b->offset;
Al Viro27c0e372017-02-17 18:42:24 -05001104 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001105 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001106 break;
1107 }
1108 unroll -= n;
David Howells8cefc102019-11-15 13:30:32 +00001109 if (!unroll && i_head == i->start_head) {
Al Viro27c0e372017-02-17 18:42:24 -05001110 off = 0;
1111 break;
1112 }
David Howells8cefc102019-11-15 13:30:32 +00001113 i_head--;
1114 b = &pipe->bufs[i_head & p_mask];
1115 off = b->offset + b->len;
Al Viro27c0e372017-02-17 18:42:24 -05001116 }
1117 i->iov_offset = off;
David Howells8cefc102019-11-15 13:30:32 +00001118 i->head = i_head;
Al Viro27c0e372017-02-17 18:42:24 -05001119 pipe_truncate(i);
1120 return;
1121 }
David Howells9ea9ce02018-10-20 00:57:56 +01001122 if (unlikely(iov_iter_is_discard(i)))
1123 return;
Al Viro27c0e372017-02-17 18:42:24 -05001124 if (unroll <= i->iov_offset) {
1125 i->iov_offset -= unroll;
1126 return;
1127 }
1128 unroll -= i->iov_offset;
David Howells00e23702018-10-22 13:07:28 +01001129 if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001130 const struct bio_vec *bvec = i->bvec;
1131 while (1) {
1132 size_t n = (--bvec)->bv_len;
1133 i->nr_segs++;
1134 if (unroll <= n) {
1135 i->bvec = bvec;
1136 i->iov_offset = n - unroll;
1137 return;
1138 }
1139 unroll -= n;
1140 }
1141 } else { /* same logics for iovec and kvec */
1142 const struct iovec *iov = i->iov;
1143 while (1) {
1144 size_t n = (--iov)->iov_len;
1145 i->nr_segs++;
1146 if (unroll <= n) {
1147 i->iov = iov;
1148 i->iov_offset = n - unroll;
1149 return;
1150 }
1151 unroll -= n;
1152 }
1153 }
1154}
1155EXPORT_SYMBOL(iov_iter_revert);
1156
Al Viro62a80672014-04-04 23:12:29 -04001157/*
1158 * Return the count of just the current iov_iter segment.
1159 */
1160size_t iov_iter_single_seg_count(const struct iov_iter *i)
1161{
David Howells00e23702018-10-22 13:07:28 +01001162 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001163 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001164 if (i->nr_segs == 1)
1165 return i->count;
David Howells9ea9ce02018-10-20 00:57:56 +01001166 if (unlikely(iov_iter_is_discard(i)))
1167 return i->count;
David Howells00e23702018-10-22 13:07:28 +01001168 else if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001169 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001170 else
1171 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001172}
1173EXPORT_SYMBOL(iov_iter_single_seg_count);
1174
David Howellsaa563d72018-10-20 00:57:56 +01001175void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001176 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001177 size_t count)
1178{
David Howellsaa563d72018-10-20 00:57:56 +01001179 WARN_ON(direction & ~(READ | WRITE));
1180 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001181 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001182 i->nr_segs = nr_segs;
1183 i->iov_offset = 0;
1184 i->count = count;
1185}
1186EXPORT_SYMBOL(iov_iter_kvec);
1187
David Howellsaa563d72018-10-20 00:57:56 +01001188void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001189 const struct bio_vec *bvec, unsigned long nr_segs,
1190 size_t count)
1191{
David Howellsaa563d72018-10-20 00:57:56 +01001192 WARN_ON(direction & ~(READ | WRITE));
1193 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001194 i->bvec = bvec;
1195 i->nr_segs = nr_segs;
1196 i->iov_offset = 0;
1197 i->count = count;
1198}
1199EXPORT_SYMBOL(iov_iter_bvec);
1200
David Howellsaa563d72018-10-20 00:57:56 +01001201void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001202 struct pipe_inode_info *pipe,
1203 size_t count)
1204{
David Howellsaa563d72018-10-20 00:57:56 +01001205 BUG_ON(direction != READ);
David Howells8cefc102019-11-15 13:30:32 +00001206 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
David Howellsaa563d72018-10-20 00:57:56 +01001207 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001208 i->pipe = pipe;
David Howells8cefc102019-11-15 13:30:32 +00001209 i->head = pipe->head;
Al Viro241699c2016-09-22 16:33:12 -04001210 i->iov_offset = 0;
1211 i->count = count;
David Howells8cefc102019-11-15 13:30:32 +00001212 i->start_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -04001213}
1214EXPORT_SYMBOL(iov_iter_pipe);
1215
David Howells9ea9ce02018-10-20 00:57:56 +01001216/**
1217 * iov_iter_discard - Initialise an I/O iterator that discards data
1218 * @i: The iterator to initialise.
1219 * @direction: The direction of the transfer.
1220 * @count: The size of the I/O buffer in bytes.
1221 *
1222 * Set up an I/O iterator that just discards everything that's written to it.
1223 * It's only available as a READ iterator.
1224 */
1225void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1226{
1227 BUG_ON(direction != READ);
1228 i->type = ITER_DISCARD | READ;
1229 i->count = count;
1230 i->iov_offset = 0;
1231}
1232EXPORT_SYMBOL(iov_iter_discard);
1233
Al Viro62a80672014-04-04 23:12:29 -04001234unsigned long iov_iter_alignment(const struct iov_iter *i)
1235{
Al Viro04a31162014-11-27 13:51:41 -05001236 unsigned long res = 0;
1237 size_t size = i->count;
1238
David Howells00e23702018-10-22 13:07:28 +01001239 if (unlikely(iov_iter_is_pipe(i))) {
Jan Karae0ff1262019-12-16 11:54:32 +01001240 unsigned int p_mask = i->pipe->ring_size - 1;
1241
David Howells8cefc102019-11-15 13:30:32 +00001242 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
Al Viro241699c2016-09-22 16:33:12 -04001243 return size | i->iov_offset;
1244 return size;
1245 }
Al Viro04a31162014-11-27 13:51:41 -05001246 iterate_all_kinds(i, size, v,
1247 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001248 res |= v.bv_offset | v.bv_len,
1249 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -05001250 )
1251 return res;
Al Viro62a80672014-04-04 23:12:29 -04001252}
1253EXPORT_SYMBOL(iov_iter_alignment);
1254
Al Viro357f4352016-04-08 19:05:19 -04001255unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1256{
Al Viro33844e62016-12-21 21:55:02 -05001257 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001258 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001259
David Howells9ea9ce02018-10-20 00:57:56 +01001260 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001261 WARN_ON(1);
1262 return ~0U;
1263 }
1264
Al Viro357f4352016-04-08 19:05:19 -04001265 iterate_all_kinds(i, size, v,
1266 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1267 (size != v.iov_len ? size : 0), 0),
1268 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1269 (size != v.bv_len ? size : 0)),
1270 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1271 (size != v.iov_len ? size : 0))
1272 );
Al Viro33844e62016-12-21 21:55:02 -05001273 return res;
Al Viro357f4352016-04-08 19:05:19 -04001274}
1275EXPORT_SYMBOL(iov_iter_gap_alignment);
1276
Ilya Dryomove76b63122018-05-02 20:16:56 +02001277static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001278 size_t maxsize,
1279 struct page **pages,
David Howells8cefc102019-11-15 13:30:32 +00001280 int iter_head,
Al Viro241699c2016-09-22 16:33:12 -04001281 size_t *start)
1282{
1283 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001284 unsigned int p_mask = pipe->ring_size - 1;
1285 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001286 if (!n)
1287 return -EFAULT;
1288
1289 maxsize = n;
1290 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001291 while (n > 0) {
David Howells8cefc102019-11-15 13:30:32 +00001292 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1293 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -04001294 n -= PAGE_SIZE;
1295 }
1296
1297 return maxsize;
1298}
1299
1300static ssize_t pipe_get_pages(struct iov_iter *i,
1301 struct page **pages, size_t maxsize, unsigned maxpages,
1302 size_t *start)
1303{
David Howells8cefc102019-11-15 13:30:32 +00001304 unsigned int iter_head, npages;
Al Viro241699c2016-09-22 16:33:12 -04001305 size_t capacity;
Al Viro241699c2016-09-22 16:33:12 -04001306
Al Viro33844e62016-12-21 21:55:02 -05001307 if (!maxsize)
1308 return 0;
1309
Al Viro241699c2016-09-22 16:33:12 -04001310 if (!sanity(i))
1311 return -EFAULT;
1312
David Howells8cefc102019-11-15 13:30:32 +00001313 data_start(i, &iter_head, start);
1314 /* Amount of free space: some of this one + all after this one */
1315 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1316 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
Al Viro241699c2016-09-22 16:33:12 -04001317
David Howells8cefc102019-11-15 13:30:32 +00001318 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001319}
1320
Al Viro62a80672014-04-04 23:12:29 -04001321ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001322 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001323 size_t *start)
1324{
Al Viroe5393fa2014-11-27 14:12:09 -05001325 if (maxsize > i->count)
1326 maxsize = i->count;
1327
David Howells00e23702018-10-22 13:07:28 +01001328 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001329 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001330 if (unlikely(iov_iter_is_discard(i)))
1331 return -EFAULT;
1332
Al Viroe5393fa2014-11-27 14:12:09 -05001333 iterate_all_kinds(i, maxsize, v, ({
1334 unsigned long addr = (unsigned long)v.iov_base;
1335 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1336 int n;
1337 int res;
1338
1339 if (len > maxpages * PAGE_SIZE)
1340 len = maxpages * PAGE_SIZE;
1341 addr &= ~(PAGE_SIZE - 1);
1342 n = DIV_ROUND_UP(len, PAGE_SIZE);
Ira Weiny73b01402019-05-13 17:17:11 -07001343 res = get_user_pages_fast(addr, n,
1344 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1345 pages);
Andreas Gruenbacherb6008662021-07-21 19:03:47 +02001346 if (unlikely(res <= 0))
Al Viroe5393fa2014-11-27 14:12:09 -05001347 return res;
1348 return (res == n ? len : res * PAGE_SIZE) - *start;
1349 0;}),({
1350 /* can't be more than PAGE_SIZE */
1351 *start = v.bv_offset;
1352 get_page(*pages = v.bv_page);
1353 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001354 }),({
1355 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001356 })
1357 )
1358 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001359}
1360EXPORT_SYMBOL(iov_iter_get_pages);
1361
Al Viro1b17f1f2014-11-27 14:14:31 -05001362static struct page **get_pages_array(size_t n)
1363{
Michal Hocko752ade62017-05-08 15:57:27 -07001364 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001365}
1366
Al Viro241699c2016-09-22 16:33:12 -04001367static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1368 struct page ***pages, size_t maxsize,
1369 size_t *start)
1370{
1371 struct page **p;
David Howells8cefc102019-11-15 13:30:32 +00001372 unsigned int iter_head, npages;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001373 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001374
Al Viro33844e62016-12-21 21:55:02 -05001375 if (!maxsize)
1376 return 0;
1377
Al Viro241699c2016-09-22 16:33:12 -04001378 if (!sanity(i))
1379 return -EFAULT;
1380
David Howells8cefc102019-11-15 13:30:32 +00001381 data_start(i, &iter_head, start);
1382 /* Amount of free space: some of this one + all after this one */
1383 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
Al Viro241699c2016-09-22 16:33:12 -04001384 n = npages * PAGE_SIZE - *start;
1385 if (maxsize > n)
1386 maxsize = n;
1387 else
1388 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1389 p = get_pages_array(npages);
1390 if (!p)
1391 return -ENOMEM;
David Howells8cefc102019-11-15 13:30:32 +00001392 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001393 if (n > 0)
1394 *pages = p;
1395 else
1396 kvfree(p);
1397 return n;
1398}
1399
Al Viro62a80672014-04-04 23:12:29 -04001400ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1401 struct page ***pages, size_t maxsize,
1402 size_t *start)
1403{
Al Viro1b17f1f2014-11-27 14:14:31 -05001404 struct page **p;
1405
1406 if (maxsize > i->count)
1407 maxsize = i->count;
1408
David Howells00e23702018-10-22 13:07:28 +01001409 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001410 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001411 if (unlikely(iov_iter_is_discard(i)))
1412 return -EFAULT;
1413
Al Viro1b17f1f2014-11-27 14:14:31 -05001414 iterate_all_kinds(i, maxsize, v, ({
1415 unsigned long addr = (unsigned long)v.iov_base;
1416 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1417 int n;
1418 int res;
1419
1420 addr &= ~(PAGE_SIZE - 1);
1421 n = DIV_ROUND_UP(len, PAGE_SIZE);
1422 p = get_pages_array(n);
1423 if (!p)
1424 return -ENOMEM;
Ira Weiny73b01402019-05-13 17:17:11 -07001425 res = get_user_pages_fast(addr, n,
1426 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
Andreas Gruenbacherb6008662021-07-21 19:03:47 +02001427 if (unlikely(res <= 0)) {
Al Viro1b17f1f2014-11-27 14:14:31 -05001428 kvfree(p);
Andreas Gruenbacherb6008662021-07-21 19:03:47 +02001429 *pages = NULL;
Al Viro1b17f1f2014-11-27 14:14:31 -05001430 return res;
1431 }
1432 *pages = p;
1433 return (res == n ? len : res * PAGE_SIZE) - *start;
1434 0;}),({
1435 /* can't be more than PAGE_SIZE */
1436 *start = v.bv_offset;
1437 *pages = p = get_pages_array(1);
1438 if (!p)
1439 return -ENOMEM;
1440 get_page(*p = v.bv_page);
1441 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001442 }),({
1443 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -05001444 })
1445 )
1446 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001447}
1448EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1449
Al Viroa604ec72014-11-24 01:08:00 -05001450size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1451 struct iov_iter *i)
1452{
1453 char *to = addr;
1454 __wsum sum, next;
1455 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001456 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001457 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001458 WARN_ON(1);
1459 return 0;
1460 }
Al Viroa604ec72014-11-24 01:08:00 -05001461 iterate_and_advance(i, bytes, v, ({
Al Virocbbd26b2016-11-01 22:09:04 -04001462 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001463 (to += v.iov_len) - v.iov_len,
Al Viroc693cc42020-07-11 00:27:49 -04001464 v.iov_len);
1465 if (next) {
Al Viroa604ec72014-11-24 01:08:00 -05001466 sum = csum_block_add(sum, next, off);
1467 off += v.iov_len;
1468 }
Al Viroc693cc42020-07-11 00:27:49 -04001469 next ? 0 : v.iov_len;
Al Viroa604ec72014-11-24 01:08:00 -05001470 }), ({
1471 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001472 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1473 p + v.bv_offset, v.bv_len,
1474 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001475 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001476 off += v.bv_len;
1477 }),({
Al Virof9152892018-11-27 22:32:59 -05001478 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1479 v.iov_base, v.iov_len,
1480 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001481 off += v.iov_len;
1482 })
1483 )
1484 *csum = sum;
1485 return bytes;
1486}
1487EXPORT_SYMBOL(csum_and_copy_from_iter);
1488
Al Virocbbd26b2016-11-01 22:09:04 -04001489bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1490 struct iov_iter *i)
1491{
1492 char *to = addr;
1493 __wsum sum, next;
1494 size_t off = 0;
1495 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001496 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001497 WARN_ON(1);
1498 return false;
1499 }
1500 if (unlikely(i->count < bytes))
1501 return false;
1502 iterate_all_kinds(i, bytes, v, ({
Al Virocbbd26b2016-11-01 22:09:04 -04001503 next = csum_and_copy_from_user(v.iov_base,
1504 (to += v.iov_len) - v.iov_len,
Al Viroc693cc42020-07-11 00:27:49 -04001505 v.iov_len);
1506 if (!next)
Al Virocbbd26b2016-11-01 22:09:04 -04001507 return false;
1508 sum = csum_block_add(sum, next, off);
1509 off += v.iov_len;
1510 0;
1511 }), ({
1512 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001513 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1514 p + v.bv_offset, v.bv_len,
1515 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001516 kunmap_atomic(p);
Al Virocbbd26b2016-11-01 22:09:04 -04001517 off += v.bv_len;
1518 }),({
Al Virof9152892018-11-27 22:32:59 -05001519 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1520 v.iov_base, v.iov_len,
1521 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001522 off += v.iov_len;
1523 })
1524 )
1525 *csum = sum;
1526 iov_iter_advance(i, bytes);
1527 return true;
1528}
1529EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1530
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001531size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
Al Viroa604ec72014-11-24 01:08:00 -05001532 struct iov_iter *i)
1533{
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001534 struct csum_state *csstate = _csstate;
Al Viro36f7a8a2015-12-06 16:49:22 -05001535 const char *from = addr;
Al Viroa604ec72014-11-24 01:08:00 -05001536 __wsum sum, next;
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001537 size_t off;
Al Viro78e1f382018-11-25 16:24:16 -05001538
1539 if (unlikely(iov_iter_is_pipe(i)))
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001540 return csum_and_copy_to_pipe_iter(addr, bytes, _csstate, i);
Al Viro78e1f382018-11-25 16:24:16 -05001541
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001542 sum = csstate->csum;
1543 off = csstate->off;
Al Viro78e1f382018-11-25 16:24:16 -05001544 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001545 WARN_ON(1); /* for now */
1546 return 0;
1547 }
Al Viroa604ec72014-11-24 01:08:00 -05001548 iterate_and_advance(i, bytes, v, ({
Al Viroa604ec72014-11-24 01:08:00 -05001549 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001550 v.iov_base,
Al Viroc693cc42020-07-11 00:27:49 -04001551 v.iov_len);
1552 if (next) {
Al Viroa604ec72014-11-24 01:08:00 -05001553 sum = csum_block_add(sum, next, off);
1554 off += v.iov_len;
1555 }
Al Viroc693cc42020-07-11 00:27:49 -04001556 next ? 0 : v.iov_len;
Al Viroa604ec72014-11-24 01:08:00 -05001557 }), ({
1558 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001559 sum = csum_and_memcpy(p + v.bv_offset,
1560 (from += v.bv_len) - v.bv_len,
1561 v.bv_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001562 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001563 off += v.bv_len;
1564 }),({
Al Virof9152892018-11-27 22:32:59 -05001565 sum = csum_and_memcpy(v.iov_base,
1566 (from += v.iov_len) - v.iov_len,
1567 v.iov_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001568 off += v.iov_len;
1569 })
1570 )
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001571 csstate->csum = sum;
1572 csstate->off = off;
Al Viroa604ec72014-11-24 01:08:00 -05001573 return bytes;
1574}
1575EXPORT_SYMBOL(csum_and_copy_to_iter);
1576
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001577size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1578 struct iov_iter *i)
1579{
Herbert Xu79990962020-06-12 16:57:37 +10001580#ifdef CONFIG_CRYPTO_HASH
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001581 struct ahash_request *hash = hashp;
1582 struct scatterlist sg;
1583 size_t copied;
1584
1585 copied = copy_to_iter(addr, bytes, i);
1586 sg_init_one(&sg, addr, copied);
1587 ahash_request_set_crypt(hash, &sg, NULL, copied);
1588 crypto_ahash_update(hash);
1589 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001590#else
1591 return 0;
1592#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001593}
1594EXPORT_SYMBOL(hash_and_copy_to_iter);
1595
Al Viro62a80672014-04-04 23:12:29 -04001596int iov_iter_npages(const struct iov_iter *i, int maxpages)
1597{
Al Viroe0f2dc42014-11-27 14:09:46 -05001598 size_t size = i->count;
1599 int npages = 0;
1600
1601 if (!size)
1602 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001603 if (unlikely(iov_iter_is_discard(i)))
1604 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001605
David Howells00e23702018-10-22 13:07:28 +01001606 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001607 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001608 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -04001609 size_t off;
Al Viro241699c2016-09-22 16:33:12 -04001610
1611 if (!sanity(i))
1612 return 0;
1613
David Howells8cefc102019-11-15 13:30:32 +00001614 data_start(i, &iter_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001615 /* some of this one + all after this one */
David Howells8cefc102019-11-15 13:30:32 +00001616 npages = pipe_space_for_user(iter_head, pipe->tail, pipe);
Al Viro241699c2016-09-22 16:33:12 -04001617 if (npages >= maxpages)
1618 return maxpages;
1619 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001620 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;
1625 0;}),({
1626 npages++;
1627 if (npages >= maxpages)
1628 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001629 }),({
1630 unsigned long p = (unsigned long)v.iov_base;
1631 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1632 - p / PAGE_SIZE;
1633 if (npages >= maxpages)
1634 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001635 })
1636 )
1637 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001638}
Al Virof67da302014-03-19 01:16:16 -04001639EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001640
1641const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1642{
1643 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001644 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001645 WARN_ON(1);
1646 return NULL;
1647 }
David Howells9ea9ce02018-10-20 00:57:56 +01001648 if (unlikely(iov_iter_is_discard(new)))
1649 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001650 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001651 return new->bvec = kmemdup(new->bvec,
1652 new->nr_segs * sizeof(struct bio_vec),
1653 flags);
1654 else
1655 /* iovec and kvec have identical layout */
1656 return new->iov = kmemdup(new->iov,
1657 new->nr_segs * sizeof(struct iovec),
1658 flags);
1659}
1660EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001661
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001662static int copy_compat_iovec_from_user(struct iovec *iov,
1663 const struct iovec __user *uvec, unsigned long nr_segs)
1664{
1665 const struct compat_iovec __user *uiov =
1666 (const struct compat_iovec __user *)uvec;
1667 int ret = -EFAULT, i;
1668
Christoph Hellwig37d4f782021-01-11 18:19:26 +01001669 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001670 return -EFAULT;
1671
1672 for (i = 0; i < nr_segs; i++) {
1673 compat_uptr_t buf;
1674 compat_ssize_t len;
1675
1676 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1677 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1678
1679 /* check for compat_size_t not fitting in compat_ssize_t .. */
1680 if (len < 0) {
1681 ret = -EINVAL;
1682 goto uaccess_end;
1683 }
1684 iov[i].iov_base = compat_ptr(buf);
1685 iov[i].iov_len = len;
1686 }
1687
1688 ret = 0;
1689uaccess_end:
1690 user_access_end();
1691 return ret;
1692}
1693
1694static int copy_iovec_from_user(struct iovec *iov,
1695 const struct iovec __user *uvec, unsigned long nr_segs)
David Laightfb041b52020-09-25 06:51:39 +02001696{
1697 unsigned long seg;
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001698
1699 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1700 return -EFAULT;
1701 for (seg = 0; seg < nr_segs; seg++) {
1702 if ((ssize_t)iov[seg].iov_len < 0)
1703 return -EINVAL;
1704 }
1705
1706 return 0;
1707}
1708
1709struct iovec *iovec_from_user(const struct iovec __user *uvec,
1710 unsigned long nr_segs, unsigned long fast_segs,
1711 struct iovec *fast_iov, bool compat)
1712{
1713 struct iovec *iov = fast_iov;
1714 int ret;
David Laightfb041b52020-09-25 06:51:39 +02001715
1716 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001717 * SuS says "The readv() function *may* fail if the iovcnt argument was
1718 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
David Laightfb041b52020-09-25 06:51:39 +02001719 * traditionally returned zero for zero segments, so...
1720 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001721 if (nr_segs == 0)
1722 return iov;
1723 if (nr_segs > UIO_MAXIOV)
1724 return ERR_PTR(-EINVAL);
David Laightfb041b52020-09-25 06:51:39 +02001725 if (nr_segs > fast_segs) {
1726 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001727 if (!iov)
1728 return ERR_PTR(-ENOMEM);
David Laightfb041b52020-09-25 06:51:39 +02001729 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001730
1731 if (compat)
1732 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1733 else
1734 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1735 if (ret) {
1736 if (iov != fast_iov)
1737 kfree(iov);
1738 return ERR_PTR(ret);
1739 }
1740
1741 return iov;
1742}
1743
1744ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1745 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1746 struct iov_iter *i, bool compat)
1747{
1748 ssize_t total_len = 0;
1749 unsigned long seg;
1750 struct iovec *iov;
1751
1752 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1753 if (IS_ERR(iov)) {
1754 *iovp = NULL;
1755 return PTR_ERR(iov);
David Laightfb041b52020-09-25 06:51:39 +02001756 }
1757
1758 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001759 * According to the Single Unix Specification we should return EINVAL if
1760 * an element length is < 0 when cast to ssize_t or if the total length
1761 * would overflow the ssize_t return value of the system call.
David Laightfb041b52020-09-25 06:51:39 +02001762 *
1763 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1764 * overflow case.
1765 */
David Laightfb041b52020-09-25 06:51:39 +02001766 for (seg = 0; seg < nr_segs; seg++) {
David Laightfb041b52020-09-25 06:51:39 +02001767 ssize_t len = (ssize_t)iov[seg].iov_len;
1768
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001769 if (!access_ok(iov[seg].iov_base, len)) {
1770 if (iov != *iovp)
1771 kfree(iov);
1772 *iovp = NULL;
1773 return -EFAULT;
David Laightfb041b52020-09-25 06:51:39 +02001774 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001775
1776 if (len > MAX_RW_COUNT - total_len) {
1777 len = MAX_RW_COUNT - total_len;
David Laightfb041b52020-09-25 06:51:39 +02001778 iov[seg].iov_len = len;
1779 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001780 total_len += len;
David Laightfb041b52020-09-25 06:51:39 +02001781 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001782
1783 iov_iter_init(i, type, iov, nr_segs, total_len);
1784 if (iov == *iovp)
1785 *iovp = NULL;
1786 else
1787 *iovp = iov;
1788 return total_len;
David Laightfb041b52020-09-25 06:51:39 +02001789}
1790
Vegard Nossumffecee42016-10-08 11:18:07 +02001791/**
1792 * import_iovec() - Copy an array of &struct iovec from userspace
1793 * into the kernel, check that it is valid, and initialize a new
1794 * &struct iov_iter iterator to access it.
1795 *
1796 * @type: One of %READ or %WRITE.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001797 * @uvec: Pointer to the userspace array.
Vegard Nossumffecee42016-10-08 11:18:07 +02001798 * @nr_segs: Number of elements in userspace array.
1799 * @fast_segs: Number of elements in @iov.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001800 * @iovp: (input and output parameter) Pointer to pointer to (usually small
Vegard Nossumffecee42016-10-08 11:18:07 +02001801 * on-stack) kernel array.
1802 * @i: Pointer to iterator that will be initialized on success.
1803 *
1804 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1805 * then this function places %NULL in *@iov on return. Otherwise, a new
1806 * array will be allocated and the result placed in *@iov. This means that
1807 * the caller may call kfree() on *@iov regardless of whether the small
1808 * on-stack array was used or not (and regardless of whether this function
1809 * returns an error or not).
1810 *
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001811 * Return: Negative error code on error, bytes imported on success
Vegard Nossumffecee42016-10-08 11:18:07 +02001812 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001813ssize_t import_iovec(int type, const struct iovec __user *uvec,
Al Virobc917be2015-03-21 17:45:43 -04001814 unsigned nr_segs, unsigned fast_segs,
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001815 struct iovec **iovp, struct iov_iter *i)
Al Virobc917be2015-03-21 17:45:43 -04001816{
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02001817 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1818 in_compat_syscall());
Al Virobc917be2015-03-21 17:45:43 -04001819}
1820EXPORT_SYMBOL(import_iovec);
1821
Al Virobc917be2015-03-21 17:45:43 -04001822int import_single_range(int rw, void __user *buf, size_t len,
1823 struct iovec *iov, struct iov_iter *i)
1824{
1825 if (len > MAX_RW_COUNT)
1826 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001827 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04001828 return -EFAULT;
1829
1830 iov->iov_base = buf;
1831 iov->iov_len = len;
1832 iov_iter_init(i, rw, iov, 1, len);
1833 return 0;
1834}
Al Viroe1267582015-12-06 20:38:56 -05001835EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05001836
1837int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1838 int (*f)(struct kvec *vec, void *context),
1839 void *context)
1840{
1841 struct kvec w;
1842 int err = -EINVAL;
1843 if (!bytes)
1844 return 0;
1845
1846 iterate_all_kinds(i, bytes, v, -EINVAL, ({
1847 w.iov_base = kmap(v.bv_page) + v.bv_offset;
1848 w.iov_len = v.bv_len;
1849 err = f(&w, context);
1850 kunmap(v.bv_page);
1851 err;}), ({
1852 w = v;
1853 err = f(&w, context);})
1854 )
1855 return err;
1856}
1857EXPORT_SYMBOL(iov_iter_for_each_range);