blob: 650554964f181cc87260fab256e030f988bbf7e3 [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;
Max Kellermannb19ec7a2022-02-21 11:03:13 +0100410 buf->flags = 0;
David Howells8cefc102019-11-15 13:30:32 +0000411 get_page(page);
412 buf->page = page;
Al Viro241699c2016-09-22 16:33:12 -0400413 buf->offset = offset;
414 buf->len = bytes;
David Howells8cefc102019-11-15 13:30:32 +0000415
416 pipe->head = i_head + 1;
Al Viro241699c2016-09-22 16:33:12 -0400417 i->iov_offset = offset + bytes;
David Howells8cefc102019-11-15 13:30:32 +0000418 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400419out:
420 i->count -= bytes;
421 return bytes;
422}
423
Al Viro4f18cd32014-02-05 19:11:33 -0500424/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400425 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
426 * bytes. For each iovec, fault in each page that constitutes the iovec.
427 *
428 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
429 * because it is an invalid address).
430 */
Al Virod4690f12016-09-16 00:11:45 +0100431int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400432{
433 size_t skip = i->iov_offset;
434 const struct iovec *iov;
435 int err;
436 struct iovec v;
437
Al Viro7b0393e2021-06-02 14:48:21 -0400438 if (iter_is_iovec(i)) {
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400439 iterate_iovec(i, bytes, v, iov, skip, ({
Al Viro4bce9f6e2016-09-17 18:02:44 -0400440 err = fault_in_pages_readable(v.iov_base, v.iov_len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400441 if (unlikely(err))
442 return err;
443 0;}))
444 }
445 return 0;
446}
Al Virod4690f12016-09-16 00:11:45 +0100447EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400448
David Howellsaa563d72018-10-20 00:57:56 +0100449void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500450 const struct iovec *iov, unsigned long nr_segs,
451 size_t count)
452{
David Howellsaa563d72018-10-20 00:57:56 +0100453 WARN_ON(direction & ~(READ | WRITE));
454 direction &= READ | WRITE;
455
Al Viro71d8e532014-03-05 19:28:09 -0500456 /* It will get better. Eventually... */
Al Virodb68ce12017-03-20 21:08:07 -0400457 if (uaccess_kernel()) {
David Howellsaa563d72018-10-20 00:57:56 +0100458 i->type = ITER_KVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500459 i->kvec = (struct kvec *)iov;
460 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100461 i->type = ITER_IOVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500462 i->iov = iov;
463 }
Al Viro71d8e532014-03-05 19:28:09 -0500464 i->nr_segs = nr_segs;
465 i->iov_offset = 0;
466 i->count = count;
467}
468EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400469
Al Viro62a80672014-04-04 23:12:29 -0400470static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
471{
472 char *from = kmap_atomic(page);
473 memcpy(to, from + offset, len);
474 kunmap_atomic(from);
475}
476
Al Viro36f7a8a2015-12-06 16:49:22 -0500477static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
Al Viro62a80672014-04-04 23:12:29 -0400478{
479 char *to = kmap_atomic(page);
480 memcpy(to + offset, from, len);
481 kunmap_atomic(to);
482}
483
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400484static void memzero_page(struct page *page, size_t offset, size_t len)
485{
486 char *addr = kmap_atomic(page);
487 memset(addr + offset, 0, len);
488 kunmap_atomic(addr);
489}
490
Al Viro241699c2016-09-22 16:33:12 -0400491static inline bool allocated(struct pipe_buffer *buf)
492{
493 return buf->ops == &default_pipe_buf_ops;
494}
495
David Howells8cefc102019-11-15 13:30:32 +0000496static inline void data_start(const struct iov_iter *i,
497 unsigned int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400498{
David Howells8cefc102019-11-15 13:30:32 +0000499 unsigned int p_mask = i->pipe->ring_size - 1;
500 unsigned int iter_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400501 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000502
503 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
504 off == PAGE_SIZE)) {
505 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400506 off = 0;
507 }
David Howells8cefc102019-11-15 13:30:32 +0000508 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400509 *offp = off;
510}
511
512static size_t push_pipe(struct iov_iter *i, size_t size,
David Howells8cefc102019-11-15 13:30:32 +0000513 int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400514{
515 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000516 unsigned int p_tail = pipe->tail;
517 unsigned int p_mask = pipe->ring_size - 1;
518 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400519 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400520 ssize_t left;
521
522 if (unlikely(size > i->count))
523 size = i->count;
524 if (unlikely(!size))
525 return 0;
526
527 left = size;
David Howells8cefc102019-11-15 13:30:32 +0000528 data_start(i, &iter_head, &off);
529 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400530 *offp = off;
531 if (off) {
532 left -= PAGE_SIZE - off;
533 if (left <= 0) {
David Howells8cefc102019-11-15 13:30:32 +0000534 pipe->bufs[iter_head & p_mask].len += size;
Al Viro241699c2016-09-22 16:33:12 -0400535 return size;
536 }
David Howells8cefc102019-11-15 13:30:32 +0000537 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
538 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400539 }
David Howells6718b6f2019-10-16 16:47:32 +0100540 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000541 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400542 struct page *page = alloc_page(GFP_USER);
543 if (!page)
544 break;
David Howells8cefc102019-11-15 13:30:32 +0000545
546 buf->ops = &default_pipe_buf_ops;
Max Kellermannb19ec7a2022-02-21 11:03:13 +0100547 buf->flags = 0;
David Howells8cefc102019-11-15 13:30:32 +0000548 buf->page = page;
549 buf->offset = 0;
550 buf->len = min_t(ssize_t, left, PAGE_SIZE);
551 left -= buf->len;
552 iter_head++;
553 pipe->head = iter_head;
554
555 if (left == 0)
Al Viro241699c2016-09-22 16:33:12 -0400556 return size;
Al Viro241699c2016-09-22 16:33:12 -0400557 }
558 return size - left;
559}
560
561static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
562 struct iov_iter *i)
563{
564 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000565 unsigned int p_mask = pipe->ring_size - 1;
566 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400567 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400568
569 if (!sanity(i))
570 return 0;
571
David Howells8cefc102019-11-15 13:30:32 +0000572 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400573 if (unlikely(!n))
574 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000575 do {
Al Viro241699c2016-09-22 16:33:12 -0400576 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000577 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
578 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400579 i->iov_offset = off + chunk;
580 n -= chunk;
581 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000582 off = 0;
583 i_head++;
584 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400585 i->count -= bytes;
586 return bytes;
587}
588
Al Virof9152892018-11-27 22:32:59 -0500589static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
590 __wsum sum, size_t off)
591{
Al Virocc44c172020-07-11 00:12:07 -0400592 __wsum next = csum_partial_copy_nocheck(from, to, len);
Al Virof9152892018-11-27 22:32:59 -0500593 return csum_block_add(sum, next, off);
594}
595
Al Viro78e1f382018-11-25 16:24:16 -0500596static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
Willem de Bruijn46a831d2021-02-03 14:29:52 -0500597 struct csum_state *csstate,
598 struct iov_iter *i)
Al Viro78e1f382018-11-25 16:24:16 -0500599{
600 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000601 unsigned int p_mask = pipe->ring_size - 1;
Willem de Bruijn46a831d2021-02-03 14:29:52 -0500602 __wsum sum = csstate->csum;
603 size_t off = csstate->off;
David Howells8cefc102019-11-15 13:30:32 +0000604 unsigned int i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500605 size_t n, r;
Al Viro78e1f382018-11-25 16:24:16 -0500606
607 if (!sanity(i))
608 return 0;
609
David Howells8cefc102019-11-15 13:30:32 +0000610 bytes = n = push_pipe(i, bytes, &i_head, &r);
Al Viro78e1f382018-11-25 16:24:16 -0500611 if (unlikely(!n))
612 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000613 do {
Al Viro78e1f382018-11-25 16:24:16 -0500614 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
David Howells8cefc102019-11-15 13:30:32 +0000615 char *p = kmap_atomic(pipe->bufs[i_head & p_mask].page);
Al Virof9152892018-11-27 22:32:59 -0500616 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
Al Viro78e1f382018-11-25 16:24:16 -0500617 kunmap_atomic(p);
David Howells8cefc102019-11-15 13:30:32 +0000618 i->head = i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500619 i->iov_offset = r + chunk;
620 n -= chunk;
621 off += chunk;
622 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000623 r = 0;
624 i_head++;
625 } while (n);
Al Viro78e1f382018-11-25 16:24:16 -0500626 i->count -= bytes;
Willem de Bruijn46a831d2021-02-03 14:29:52 -0500627 csstate->csum = sum;
628 csstate->off = off;
Al Viro78e1f382018-11-25 16:24:16 -0500629 return bytes;
630}
631
Al Viroaa28de22017-06-29 21:45:10 -0400632size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400633{
Al Viro36f7a8a2015-12-06 16:49:22 -0500634 const char *from = addr;
David Howells00e23702018-10-22 13:07:28 +0100635 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400636 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400637 if (iter_is_iovec(i))
638 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500639 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400640 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500641 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500642 (from += v.bv_len) - v.bv_len, v.bv_len),
643 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500644 )
Al Viro62a80672014-04-04 23:12:29 -0400645
Al Viro3d4d3e42014-11-27 14:28:06 -0500646 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400647}
Al Viroaa28de22017-06-29 21:45:10 -0400648EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400649
Dan Williamsec6347b2020-10-05 20:40:16 -0700650#ifdef CONFIG_ARCH_HAS_COPY_MC
651static int copyout_mc(void __user *to, const void *from, size_t n)
Dan Williams87803562018-05-03 17:06:31 -0700652{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800653 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100654 instrument_copy_to_user(to, from, n);
Dan Williamsec6347b2020-10-05 20:40:16 -0700655 n = copy_mc_to_user((__force void *) to, from, n);
Dan Williams87803562018-05-03 17:06:31 -0700656 }
657 return n;
658}
659
Dan Williamsec6347b2020-10-05 20:40:16 -0700660static unsigned long copy_mc_to_page(struct page *page, size_t offset,
Dan Williams87803562018-05-03 17:06:31 -0700661 const char *from, size_t len)
662{
663 unsigned long ret;
664 char *to;
665
666 to = kmap_atomic(page);
Dan Williamsec6347b2020-10-05 20:40:16 -0700667 ret = copy_mc_to_kernel(to + offset, from, len);
Dan Williams87803562018-05-03 17:06:31 -0700668 kunmap_atomic(to);
669
670 return ret;
671}
672
Dan Williamsec6347b2020-10-05 20:40:16 -0700673static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
Dan Williamsca146f62018-07-08 13:46:12 -0700674 struct iov_iter *i)
675{
676 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000677 unsigned int p_mask = pipe->ring_size - 1;
678 unsigned int i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700679 size_t n, off, xfer = 0;
Dan Williamsca146f62018-07-08 13:46:12 -0700680
681 if (!sanity(i))
682 return 0;
683
David Howells8cefc102019-11-15 13:30:32 +0000684 bytes = n = push_pipe(i, bytes, &i_head, &off);
Dan Williamsca146f62018-07-08 13:46:12 -0700685 if (unlikely(!n))
686 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000687 do {
Dan Williamsca146f62018-07-08 13:46:12 -0700688 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
689 unsigned long rem;
690
Dan Williamsec6347b2020-10-05 20:40:16 -0700691 rem = copy_mc_to_page(pipe->bufs[i_head & p_mask].page,
David Howells8cefc102019-11-15 13:30:32 +0000692 off, addr, chunk);
693 i->head = i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700694 i->iov_offset = off + chunk - rem;
695 xfer += chunk - rem;
696 if (rem)
697 break;
698 n -= chunk;
699 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000700 off = 0;
701 i_head++;
702 } while (n);
Dan Williamsca146f62018-07-08 13:46:12 -0700703 i->count -= xfer;
704 return xfer;
705}
706
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700707/**
Dan Williamsec6347b2020-10-05 20:40:16 -0700708 * _copy_mc_to_iter - copy to iter with source memory error exception handling
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700709 * @addr: source kernel address
710 * @bytes: total transfer length
711 * @iter: destination iterator
712 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700713 * The pmem driver deploys this for the dax operation
714 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
715 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
716 * successfully copied.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700717 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700718 * The main differences between this and typical _copy_to_iter().
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700719 *
720 * * Typical tail/residue handling after a fault retries the copy
721 * byte-by-byte until the fault happens again. Re-triggering machine
722 * checks is potentially fatal so the implementation uses source
723 * alignment and poison alignment assumptions to avoid re-triggering
724 * hardware exceptions.
725 *
726 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
727 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
728 * a short copy.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700729 */
Dan Williamsec6347b2020-10-05 20:40:16 -0700730size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Dan Williams87803562018-05-03 17:06:31 -0700731{
732 const char *from = addr;
733 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
734
David Howells00e23702018-10-22 13:07:28 +0100735 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsec6347b2020-10-05 20:40:16 -0700736 return copy_mc_pipe_to_iter(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700737 if (iter_is_iovec(i))
738 might_fault();
739 iterate_and_advance(i, bytes, v,
Dan Williamsec6347b2020-10-05 20:40:16 -0700740 copyout_mc(v.iov_base, (from += v.iov_len) - v.iov_len,
741 v.iov_len),
Dan Williams87803562018-05-03 17:06:31 -0700742 ({
Dan Williamsec6347b2020-10-05 20:40:16 -0700743 rem = copy_mc_to_page(v.bv_page, v.bv_offset,
744 (from += v.bv_len) - v.bv_len, v.bv_len);
Dan Williams87803562018-05-03 17:06:31 -0700745 if (rem) {
746 curr_addr = (unsigned long) from;
747 bytes = curr_addr - s_addr - rem;
748 return bytes;
749 }
750 }),
751 ({
Dan Williamsec6347b2020-10-05 20:40:16 -0700752 rem = copy_mc_to_kernel(v.iov_base, (from += v.iov_len)
753 - v.iov_len, v.iov_len);
Dan Williams87803562018-05-03 17:06:31 -0700754 if (rem) {
755 curr_addr = (unsigned long) from;
756 bytes = curr_addr - s_addr - rem;
757 return bytes;
758 }
759 })
760 )
761
762 return bytes;
763}
Dan Williamsec6347b2020-10-05 20:40:16 -0700764EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
765#endif /* CONFIG_ARCH_HAS_COPY_MC */
Dan Williams87803562018-05-03 17:06:31 -0700766
Al Viroaa28de22017-06-29 21:45:10 -0400767size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400768{
Al Viro0dbca9a2014-11-27 14:26:43 -0500769 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100770 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400771 WARN_ON(1);
772 return 0;
773 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400774 if (iter_is_iovec(i))
775 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500776 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400777 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500778 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500779 v.bv_offset, v.bv_len),
780 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500781 )
782
783 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400784}
Al Viroaa28de22017-06-29 21:45:10 -0400785EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400786
Al Viroaa28de22017-06-29 21:45:10 -0400787bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400788{
789 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100790 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400791 WARN_ON(1);
792 return false;
793 }
Al Viro33844e62016-12-21 21:55:02 -0500794 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400795 return false;
796
Al Viro09fc68dc2017-06-29 22:25:14 -0400797 if (iter_is_iovec(i))
798 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400799 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68dc2017-06-29 22:25:14 -0400800 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400801 v.iov_base, v.iov_len))
802 return false;
803 0;}),
804 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
805 v.bv_offset, v.bv_len),
806 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
807 )
808
809 iov_iter_advance(i, bytes);
810 return true;
811}
Al Viroaa28de22017-06-29 21:45:10 -0400812EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400813
Al Viroaa28de22017-06-29 21:45:10 -0400814size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500815{
816 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100817 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400818 WARN_ON(1);
819 return 0;
820 }
Al Viroaa583092014-11-27 20:27:08 -0500821 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400822 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500823 v.iov_base, v.iov_len),
824 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
825 v.bv_offset, v.bv_len),
826 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
827 )
828
829 return bytes;
830}
Al Viroaa28de22017-06-29 21:45:10 -0400831EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500832
Dan Williams0aed55a2017-05-29 12:22:50 -0700833#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700834/**
835 * _copy_from_iter_flushcache - write destination through cpu cache
836 * @addr: destination kernel address
837 * @bytes: total transfer length
838 * @iter: source iterator
839 *
840 * The pmem driver arranges for filesystem-dax to use this facility via
841 * dax_copy_from_iter() for ensuring that writes to persistent memory
842 * are flushed through the CPU cache. It is differentiated from
843 * _copy_from_iter_nocache() in that guarantees all data is flushed for
844 * all iterator types. The _copy_from_iter_nocache() only attempts to
845 * bypass the cache for the ITER_IOVEC case, and on some archs may use
846 * instructions that strand dirty-data in the cache.
847 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700848size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700849{
850 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100851 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700852 WARN_ON(1);
853 return 0;
854 }
855 iterate_and_advance(i, bytes, v,
856 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
857 v.iov_base, v.iov_len),
858 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
859 v.bv_offset, v.bv_len),
860 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
861 v.iov_len)
862 )
863
864 return bytes;
865}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700866EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700867#endif
868
Al Viroaa28de22017-06-29 21:45:10 -0400869bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400870{
871 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100872 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400873 WARN_ON(1);
874 return false;
875 }
Al Viro33844e62016-12-21 21:55:02 -0500876 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400877 return false;
878 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400879 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400880 v.iov_base, v.iov_len))
881 return false;
882 0;}),
883 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
884 v.bv_offset, v.bv_len),
885 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
886 )
887
888 iov_iter_advance(i, bytes);
889 return true;
890}
Al Viroaa28de22017-06-29 21:45:10 -0400891EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400892
Al Viro72e809e2017-06-29 21:52:57 -0400893static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
894{
Eric Dumazet6daef952019-02-26 10:42:39 -0800895 struct page *head;
896 size_t v = n + offset;
897
898 /*
899 * The general case needs to access the page order in order
900 * to compute the page size.
901 * However, we mostly deal with order-0 pages and thus can
902 * avoid a possible cache line miss for requests that fit all
903 * page orders.
904 */
905 if (n <= v && v <= PAGE_SIZE)
906 return true;
907
908 head = compound_head(page);
909 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700910
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700911 if (likely(n <= v && v <= (page_size(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400912 return true;
913 WARN_ON(1);
914 return false;
915}
Al Virod2715242014-11-27 14:22:37 -0500916
917size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
918 struct iov_iter *i)
919{
Al Viro72e809e2017-06-29 21:52:57 -0400920 if (unlikely(!page_copy_sane(page, offset, bytes)))
921 return 0;
Al Virod2715242014-11-27 14:22:37 -0500922 if (i->type & (ITER_BVEC|ITER_KVEC)) {
923 void *kaddr = kmap_atomic(page);
924 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
925 kunmap_atomic(kaddr);
926 return wanted;
Al Virob6df9e42021-04-27 12:34:04 -0400927 } else if (unlikely(iov_iter_is_discard(i))) {
928 if (unlikely(i->count < bytes))
929 bytes = i->count;
930 i->count -= bytes;
David Howells9ea9ce02018-10-20 00:57:56 +0100931 return bytes;
Al Virob6df9e42021-04-27 12:34:04 -0400932 } else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500933 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400934 else
935 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500936}
937EXPORT_SYMBOL(copy_page_to_iter);
938
939size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
940 struct iov_iter *i)
941{
Al Viro72e809e2017-06-29 21:52:57 -0400942 if (unlikely(!page_copy_sane(page, offset, bytes)))
943 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +0100944 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400945 WARN_ON(1);
946 return 0;
947 }
Al Virod2715242014-11-27 14:22:37 -0500948 if (i->type & (ITER_BVEC|ITER_KVEC)) {
949 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400950 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500951 kunmap_atomic(kaddr);
952 return wanted;
953 } else
954 return copy_page_from_iter_iovec(page, offset, bytes, i);
955}
956EXPORT_SYMBOL(copy_page_from_iter);
957
Al Viro241699c2016-09-22 16:33:12 -0400958static size_t pipe_zero(size_t bytes, struct iov_iter *i)
959{
960 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000961 unsigned int p_mask = pipe->ring_size - 1;
962 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400963 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400964
965 if (!sanity(i))
966 return 0;
967
David Howells8cefc102019-11-15 13:30:32 +0000968 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400969 if (unlikely(!n))
970 return 0;
971
David Howells8cefc102019-11-15 13:30:32 +0000972 do {
Al Viro241699c2016-09-22 16:33:12 -0400973 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000974 memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk);
975 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400976 i->iov_offset = off + chunk;
977 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000978 off = 0;
979 i_head++;
980 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400981 i->count -= bytes;
982 return bytes;
983}
984
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400985size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
986{
David Howells00e23702018-10-22 13:07:28 +0100987 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400988 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500989 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400990 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500991 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
992 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500993 )
994
995 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400996}
997EXPORT_SYMBOL(iov_iter_zero);
998
Al Viro62a80672014-04-04 23:12:29 -0400999size_t iov_iter_copy_from_user_atomic(struct page *page,
1000 struct iov_iter *i, unsigned long offset, size_t bytes)
1001{
Al Viro04a31162014-11-27 13:51:41 -05001002 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -04001003 if (unlikely(!page_copy_sane(page, offset, bytes))) {
1004 kunmap_atomic(kaddr);
1005 return 0;
1006 }
David Howells9ea9ce02018-10-20 00:57:56 +01001007 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001008 kunmap_atomic(kaddr);
1009 WARN_ON(1);
1010 return 0;
1011 }
Al Viro04a31162014-11-27 13:51:41 -05001012 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -04001013 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -05001014 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -05001015 v.bv_offset, v.bv_len),
1016 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -05001017 )
1018 kunmap_atomic(kaddr);
1019 return bytes;
Al Viro62a80672014-04-04 23:12:29 -04001020}
1021EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
1022
Al Virob9dc6f62017-01-14 19:33:08 -05001023static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -04001024{
1025 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001026 unsigned int p_tail = pipe->tail;
1027 unsigned int p_head = pipe->head;
1028 unsigned int p_mask = pipe->ring_size - 1;
1029
1030 if (!pipe_empty(p_head, p_tail)) {
1031 struct pipe_buffer *buf;
1032 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001033 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +00001034
Al Virob9dc6f62017-01-14 19:33:08 -05001035 if (off) {
David Howells8cefc102019-11-15 13:30:32 +00001036 buf = &pipe->bufs[i_head & p_mask];
1037 buf->len = off - buf->offset;
1038 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001039 }
David Howells8cefc102019-11-15 13:30:32 +00001040 while (p_head != i_head) {
1041 p_head--;
1042 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
Al Viro241699c2016-09-22 16:33:12 -04001043 }
David Howells8cefc102019-11-15 13:30:32 +00001044
1045 pipe->head = p_head;
Al Viro241699c2016-09-22 16:33:12 -04001046 }
Al Virob9dc6f62017-01-14 19:33:08 -05001047}
1048
1049static void pipe_advance(struct iov_iter *i, size_t size)
1050{
1051 struct pipe_inode_info *pipe = i->pipe;
1052 if (unlikely(i->count < size))
1053 size = i->count;
1054 if (size) {
1055 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +00001056 unsigned int p_mask = pipe->ring_size - 1;
1057 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001058 size_t off = i->iov_offset, left = size;
David Howells8cefc102019-11-15 13:30:32 +00001059
Al Virob9dc6f62017-01-14 19:33:08 -05001060 if (off) /* make it relative to the beginning of buffer */
David Howells8cefc102019-11-15 13:30:32 +00001061 left += off - pipe->bufs[i_head & p_mask].offset;
Al Virob9dc6f62017-01-14 19:33:08 -05001062 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001063 buf = &pipe->bufs[i_head & p_mask];
Al Virob9dc6f62017-01-14 19:33:08 -05001064 if (left <= buf->len)
1065 break;
1066 left -= buf->len;
David Howells8cefc102019-11-15 13:30:32 +00001067 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001068 }
David Howells8cefc102019-11-15 13:30:32 +00001069 i->head = i_head;
Al Virob9dc6f62017-01-14 19:33:08 -05001070 i->iov_offset = buf->offset + left;
1071 }
1072 i->count -= size;
1073 /* ... and discard everything past that point */
1074 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001075}
1076
Al Viro62a80672014-04-04 23:12:29 -04001077void iov_iter_advance(struct iov_iter *i, size_t size)
1078{
David Howells00e23702018-10-22 13:07:28 +01001079 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001080 pipe_advance(i, size);
1081 return;
1082 }
David Howells9ea9ce02018-10-20 00:57:56 +01001083 if (unlikely(iov_iter_is_discard(i))) {
1084 i->count -= size;
1085 return;
1086 }
Al Viroa2804552014-11-27 14:48:42 -05001087 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -04001088}
1089EXPORT_SYMBOL(iov_iter_advance);
1090
Al Viro27c0e372017-02-17 18:42:24 -05001091void iov_iter_revert(struct iov_iter *i, size_t unroll)
1092{
1093 if (!unroll)
1094 return;
Al Viro5b47d592017-05-08 13:54:47 -04001095 if (WARN_ON(unroll > MAX_RW_COUNT))
1096 return;
Al Viro27c0e372017-02-17 18:42:24 -05001097 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001098 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001099 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001100 unsigned int p_mask = pipe->ring_size - 1;
1101 unsigned int i_head = i->head;
Al Viro27c0e372017-02-17 18:42:24 -05001102 size_t off = i->iov_offset;
1103 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001104 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1105 size_t n = off - b->offset;
Al Viro27c0e372017-02-17 18:42:24 -05001106 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001107 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001108 break;
1109 }
1110 unroll -= n;
David Howells8cefc102019-11-15 13:30:32 +00001111 if (!unroll && i_head == i->start_head) {
Al Viro27c0e372017-02-17 18:42:24 -05001112 off = 0;
1113 break;
1114 }
David Howells8cefc102019-11-15 13:30:32 +00001115 i_head--;
1116 b = &pipe->bufs[i_head & p_mask];
1117 off = b->offset + b->len;
Al Viro27c0e372017-02-17 18:42:24 -05001118 }
1119 i->iov_offset = off;
David Howells8cefc102019-11-15 13:30:32 +00001120 i->head = i_head;
Al Viro27c0e372017-02-17 18:42:24 -05001121 pipe_truncate(i);
1122 return;
1123 }
David Howells9ea9ce02018-10-20 00:57:56 +01001124 if (unlikely(iov_iter_is_discard(i)))
1125 return;
Al Viro27c0e372017-02-17 18:42:24 -05001126 if (unroll <= i->iov_offset) {
1127 i->iov_offset -= unroll;
1128 return;
1129 }
1130 unroll -= i->iov_offset;
David Howells00e23702018-10-22 13:07:28 +01001131 if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001132 const struct bio_vec *bvec = i->bvec;
1133 while (1) {
1134 size_t n = (--bvec)->bv_len;
1135 i->nr_segs++;
1136 if (unroll <= n) {
1137 i->bvec = bvec;
1138 i->iov_offset = n - unroll;
1139 return;
1140 }
1141 unroll -= n;
1142 }
1143 } else { /* same logics for iovec and kvec */
1144 const struct iovec *iov = i->iov;
1145 while (1) {
1146 size_t n = (--iov)->iov_len;
1147 i->nr_segs++;
1148 if (unroll <= n) {
1149 i->iov = iov;
1150 i->iov_offset = n - unroll;
1151 return;
1152 }
1153 unroll -= n;
1154 }
1155 }
1156}
1157EXPORT_SYMBOL(iov_iter_revert);
1158
Al Viro62a80672014-04-04 23:12:29 -04001159/*
1160 * Return the count of just the current iov_iter segment.
1161 */
1162size_t iov_iter_single_seg_count(const struct iov_iter *i)
1163{
David Howells00e23702018-10-22 13:07:28 +01001164 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001165 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001166 if (i->nr_segs == 1)
1167 return i->count;
David Howells9ea9ce02018-10-20 00:57:56 +01001168 if (unlikely(iov_iter_is_discard(i)))
1169 return i->count;
David Howells00e23702018-10-22 13:07:28 +01001170 else if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001171 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001172 else
1173 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001174}
1175EXPORT_SYMBOL(iov_iter_single_seg_count);
1176
David Howellsaa563d72018-10-20 00:57:56 +01001177void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001178 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001179 size_t count)
1180{
David Howellsaa563d72018-10-20 00:57:56 +01001181 WARN_ON(direction & ~(READ | WRITE));
1182 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001183 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001184 i->nr_segs = nr_segs;
1185 i->iov_offset = 0;
1186 i->count = count;
1187}
1188EXPORT_SYMBOL(iov_iter_kvec);
1189
David Howellsaa563d72018-10-20 00:57:56 +01001190void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001191 const struct bio_vec *bvec, unsigned long nr_segs,
1192 size_t count)
1193{
David Howellsaa563d72018-10-20 00:57:56 +01001194 WARN_ON(direction & ~(READ | WRITE));
1195 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001196 i->bvec = bvec;
1197 i->nr_segs = nr_segs;
1198 i->iov_offset = 0;
1199 i->count = count;
1200}
1201EXPORT_SYMBOL(iov_iter_bvec);
1202
David Howellsaa563d72018-10-20 00:57:56 +01001203void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001204 struct pipe_inode_info *pipe,
1205 size_t count)
1206{
David Howellsaa563d72018-10-20 00:57:56 +01001207 BUG_ON(direction != READ);
David Howells8cefc102019-11-15 13:30:32 +00001208 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
David Howellsaa563d72018-10-20 00:57:56 +01001209 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001210 i->pipe = pipe;
David Howells8cefc102019-11-15 13:30:32 +00001211 i->head = pipe->head;
Al Viro241699c2016-09-22 16:33:12 -04001212 i->iov_offset = 0;
1213 i->count = count;
David Howells8cefc102019-11-15 13:30:32 +00001214 i->start_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -04001215}
1216EXPORT_SYMBOL(iov_iter_pipe);
1217
David Howells9ea9ce02018-10-20 00:57:56 +01001218/**
1219 * iov_iter_discard - Initialise an I/O iterator that discards data
1220 * @i: The iterator to initialise.
1221 * @direction: The direction of the transfer.
1222 * @count: The size of the I/O buffer in bytes.
1223 *
1224 * Set up an I/O iterator that just discards everything that's written to it.
1225 * It's only available as a READ iterator.
1226 */
1227void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1228{
1229 BUG_ON(direction != READ);
1230 i->type = ITER_DISCARD | READ;
1231 i->count = count;
1232 i->iov_offset = 0;
1233}
1234EXPORT_SYMBOL(iov_iter_discard);
1235
Al Viro62a80672014-04-04 23:12:29 -04001236unsigned long iov_iter_alignment(const struct iov_iter *i)
1237{
Al Viro04a31162014-11-27 13:51:41 -05001238 unsigned long res = 0;
1239 size_t size = i->count;
1240
David Howells00e23702018-10-22 13:07:28 +01001241 if (unlikely(iov_iter_is_pipe(i))) {
Jan Karae0ff1262019-12-16 11:54:32 +01001242 unsigned int p_mask = i->pipe->ring_size - 1;
1243
David Howells8cefc102019-11-15 13:30:32 +00001244 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
Al Viro241699c2016-09-22 16:33:12 -04001245 return size | i->iov_offset;
1246 return size;
1247 }
Al Viro04a31162014-11-27 13:51:41 -05001248 iterate_all_kinds(i, size, v,
1249 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001250 res |= v.bv_offset | v.bv_len,
1251 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -05001252 )
1253 return res;
Al Viro62a80672014-04-04 23:12:29 -04001254}
1255EXPORT_SYMBOL(iov_iter_alignment);
1256
Al Viro357f4352016-04-08 19:05:19 -04001257unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1258{
Al Viro33844e62016-12-21 21:55:02 -05001259 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001260 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001261
David Howells9ea9ce02018-10-20 00:57:56 +01001262 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001263 WARN_ON(1);
1264 return ~0U;
1265 }
1266
Al Viro357f4352016-04-08 19:05:19 -04001267 iterate_all_kinds(i, size, v,
1268 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1269 (size != v.iov_len ? size : 0), 0),
1270 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1271 (size != v.bv_len ? size : 0)),
1272 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1273 (size != v.iov_len ? size : 0))
1274 );
Al Viro33844e62016-12-21 21:55:02 -05001275 return res;
Al Viro357f4352016-04-08 19:05:19 -04001276}
1277EXPORT_SYMBOL(iov_iter_gap_alignment);
1278
Ilya Dryomove76b63122018-05-02 20:16:56 +02001279static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001280 size_t maxsize,
1281 struct page **pages,
David Howells8cefc102019-11-15 13:30:32 +00001282 int iter_head,
Al Viro241699c2016-09-22 16:33:12 -04001283 size_t *start)
1284{
1285 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001286 unsigned int p_mask = pipe->ring_size - 1;
1287 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001288 if (!n)
1289 return -EFAULT;
1290
1291 maxsize = n;
1292 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001293 while (n > 0) {
David Howells8cefc102019-11-15 13:30:32 +00001294 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1295 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -04001296 n -= PAGE_SIZE;
1297 }
1298
1299 return maxsize;
1300}
1301
1302static ssize_t pipe_get_pages(struct iov_iter *i,
1303 struct page **pages, size_t maxsize, unsigned maxpages,
1304 size_t *start)
1305{
David Howells8cefc102019-11-15 13:30:32 +00001306 unsigned int iter_head, npages;
Al Viro241699c2016-09-22 16:33:12 -04001307 size_t capacity;
Al Viro241699c2016-09-22 16:33:12 -04001308
Al Viro33844e62016-12-21 21:55:02 -05001309 if (!maxsize)
1310 return 0;
1311
Al Viro241699c2016-09-22 16:33:12 -04001312 if (!sanity(i))
1313 return -EFAULT;
1314
David Howells8cefc102019-11-15 13:30:32 +00001315 data_start(i, &iter_head, start);
1316 /* Amount of free space: some of this one + all after this one */
1317 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1318 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
Al Viro241699c2016-09-22 16:33:12 -04001319
David Howells8cefc102019-11-15 13:30:32 +00001320 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001321}
1322
Al Viro62a80672014-04-04 23:12:29 -04001323ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001324 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001325 size_t *start)
1326{
Al Viroe5393fa2014-11-27 14:12:09 -05001327 if (maxsize > i->count)
1328 maxsize = i->count;
1329
David Howells00e23702018-10-22 13:07:28 +01001330 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001331 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001332 if (unlikely(iov_iter_is_discard(i)))
1333 return -EFAULT;
1334
Al Viroe5393fa2014-11-27 14:12:09 -05001335 iterate_all_kinds(i, maxsize, v, ({
1336 unsigned long addr = (unsigned long)v.iov_base;
1337 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1338 int n;
1339 int res;
1340
1341 if (len > maxpages * PAGE_SIZE)
1342 len = maxpages * PAGE_SIZE;
1343 addr &= ~(PAGE_SIZE - 1);
1344 n = DIV_ROUND_UP(len, PAGE_SIZE);
Ira Weiny73b01402019-05-13 17:17:11 -07001345 res = get_user_pages_fast(addr, n,
1346 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1347 pages);
Andreas Gruenbacherb6008662021-07-21 19:03:47 +02001348 if (unlikely(res <= 0))
Al Viroe5393fa2014-11-27 14:12:09 -05001349 return res;
1350 return (res == n ? len : res * PAGE_SIZE) - *start;
1351 0;}),({
1352 /* can't be more than PAGE_SIZE */
1353 *start = v.bv_offset;
1354 get_page(*pages = v.bv_page);
1355 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001356 }),({
1357 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001358 })
1359 )
1360 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001361}
1362EXPORT_SYMBOL(iov_iter_get_pages);
1363
Al Viro1b17f1f2014-11-27 14:14:31 -05001364static struct page **get_pages_array(size_t n)
1365{
Michal Hocko752ade62017-05-08 15:57:27 -07001366 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001367}
1368
Al Viro241699c2016-09-22 16:33:12 -04001369static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1370 struct page ***pages, size_t maxsize,
1371 size_t *start)
1372{
1373 struct page **p;
David Howells8cefc102019-11-15 13:30:32 +00001374 unsigned int iter_head, npages;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001375 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001376
Al Viro33844e62016-12-21 21:55:02 -05001377 if (!maxsize)
1378 return 0;
1379
Al Viro241699c2016-09-22 16:33:12 -04001380 if (!sanity(i))
1381 return -EFAULT;
1382
David Howells8cefc102019-11-15 13:30:32 +00001383 data_start(i, &iter_head, start);
1384 /* Amount of free space: some of this one + all after this one */
1385 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
Al Viro241699c2016-09-22 16:33:12 -04001386 n = npages * PAGE_SIZE - *start;
1387 if (maxsize > n)
1388 maxsize = n;
1389 else
1390 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1391 p = get_pages_array(npages);
1392 if (!p)
1393 return -ENOMEM;
David Howells8cefc102019-11-15 13:30:32 +00001394 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001395 if (n > 0)
1396 *pages = p;
1397 else
1398 kvfree(p);
1399 return n;
1400}
1401
Al Viro62a80672014-04-04 23:12:29 -04001402ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1403 struct page ***pages, size_t maxsize,
1404 size_t *start)
1405{
Al Viro1b17f1f2014-11-27 14:14:31 -05001406 struct page **p;
1407
1408 if (maxsize > i->count)
1409 maxsize = i->count;
1410
David Howells00e23702018-10-22 13:07:28 +01001411 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001412 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001413 if (unlikely(iov_iter_is_discard(i)))
1414 return -EFAULT;
1415
Al Viro1b17f1f2014-11-27 14:14:31 -05001416 iterate_all_kinds(i, maxsize, v, ({
1417 unsigned long addr = (unsigned long)v.iov_base;
1418 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1419 int n;
1420 int res;
1421
1422 addr &= ~(PAGE_SIZE - 1);
1423 n = DIV_ROUND_UP(len, PAGE_SIZE);
1424 p = get_pages_array(n);
1425 if (!p)
1426 return -ENOMEM;
Ira Weiny73b01402019-05-13 17:17:11 -07001427 res = get_user_pages_fast(addr, n,
1428 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
Andreas Gruenbacherb6008662021-07-21 19:03:47 +02001429 if (unlikely(res <= 0)) {
Al Viro1b17f1f2014-11-27 14:14:31 -05001430 kvfree(p);
Andreas Gruenbacherb6008662021-07-21 19:03:47 +02001431 *pages = NULL;
Al Viro1b17f1f2014-11-27 14:14:31 -05001432 return res;
1433 }
1434 *pages = p;
1435 return (res == n ? len : res * PAGE_SIZE) - *start;
1436 0;}),({
1437 /* can't be more than PAGE_SIZE */
1438 *start = v.bv_offset;
1439 *pages = p = get_pages_array(1);
1440 if (!p)
1441 return -ENOMEM;
1442 get_page(*p = v.bv_page);
1443 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001444 }),({
1445 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -05001446 })
1447 )
1448 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001449}
1450EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1451
Al Viroa604ec72014-11-24 01:08:00 -05001452size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1453 struct iov_iter *i)
1454{
1455 char *to = addr;
1456 __wsum sum, next;
1457 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001458 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001459 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001460 WARN_ON(1);
1461 return 0;
1462 }
Al Viroa604ec72014-11-24 01:08:00 -05001463 iterate_and_advance(i, bytes, v, ({
Al Virocbbd26b2016-11-01 22:09:04 -04001464 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001465 (to += v.iov_len) - v.iov_len,
Al Viroc693cc42020-07-11 00:27:49 -04001466 v.iov_len);
1467 if (next) {
Al Viroa604ec72014-11-24 01:08:00 -05001468 sum = csum_block_add(sum, next, off);
1469 off += v.iov_len;
1470 }
Al Viroc693cc42020-07-11 00:27:49 -04001471 next ? 0 : v.iov_len;
Al Viroa604ec72014-11-24 01:08:00 -05001472 }), ({
1473 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001474 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1475 p + v.bv_offset, v.bv_len,
1476 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001477 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001478 off += v.bv_len;
1479 }),({
Al Virof9152892018-11-27 22:32:59 -05001480 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1481 v.iov_base, v.iov_len,
1482 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001483 off += v.iov_len;
1484 })
1485 )
1486 *csum = sum;
1487 return bytes;
1488}
1489EXPORT_SYMBOL(csum_and_copy_from_iter);
1490
Al Virocbbd26b2016-11-01 22:09:04 -04001491bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1492 struct iov_iter *i)
1493{
1494 char *to = addr;
1495 __wsum sum, next;
1496 size_t off = 0;
1497 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001498 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001499 WARN_ON(1);
1500 return false;
1501 }
1502 if (unlikely(i->count < bytes))
1503 return false;
1504 iterate_all_kinds(i, bytes, v, ({
Al Virocbbd26b2016-11-01 22:09:04 -04001505 next = csum_and_copy_from_user(v.iov_base,
1506 (to += v.iov_len) - v.iov_len,
Al Viroc693cc42020-07-11 00:27:49 -04001507 v.iov_len);
1508 if (!next)
Al Virocbbd26b2016-11-01 22:09:04 -04001509 return false;
1510 sum = csum_block_add(sum, next, off);
1511 off += v.iov_len;
1512 0;
1513 }), ({
1514 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001515 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1516 p + v.bv_offset, v.bv_len,
1517 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001518 kunmap_atomic(p);
Al Virocbbd26b2016-11-01 22:09:04 -04001519 off += v.bv_len;
1520 }),({
Al Virof9152892018-11-27 22:32:59 -05001521 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1522 v.iov_base, v.iov_len,
1523 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001524 off += v.iov_len;
1525 })
1526 )
1527 *csum = sum;
1528 iov_iter_advance(i, bytes);
1529 return true;
1530}
1531EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1532
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001533size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
Al Viroa604ec72014-11-24 01:08:00 -05001534 struct iov_iter *i)
1535{
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001536 struct csum_state *csstate = _csstate;
Al Viro36f7a8a2015-12-06 16:49:22 -05001537 const char *from = addr;
Al Viroa604ec72014-11-24 01:08:00 -05001538 __wsum sum, next;
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001539 size_t off;
Al Viro78e1f382018-11-25 16:24:16 -05001540
1541 if (unlikely(iov_iter_is_pipe(i)))
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001542 return csum_and_copy_to_pipe_iter(addr, bytes, _csstate, i);
Al Viro78e1f382018-11-25 16:24:16 -05001543
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001544 sum = csstate->csum;
1545 off = csstate->off;
Al Viro78e1f382018-11-25 16:24:16 -05001546 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001547 WARN_ON(1); /* for now */
1548 return 0;
1549 }
Al Viroa604ec72014-11-24 01:08:00 -05001550 iterate_and_advance(i, bytes, v, ({
Al Viroa604ec72014-11-24 01:08:00 -05001551 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001552 v.iov_base,
Al Viroc693cc42020-07-11 00:27:49 -04001553 v.iov_len);
1554 if (next) {
Al Viroa604ec72014-11-24 01:08:00 -05001555 sum = csum_block_add(sum, next, off);
1556 off += v.iov_len;
1557 }
Al Viroc693cc42020-07-11 00:27:49 -04001558 next ? 0 : v.iov_len;
Al Viroa604ec72014-11-24 01:08:00 -05001559 }), ({
1560 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001561 sum = csum_and_memcpy(p + v.bv_offset,
1562 (from += v.bv_len) - v.bv_len,
1563 v.bv_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001564 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001565 off += v.bv_len;
1566 }),({
Al Virof9152892018-11-27 22:32:59 -05001567 sum = csum_and_memcpy(v.iov_base,
1568 (from += v.iov_len) - v.iov_len,
1569 v.iov_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001570 off += v.iov_len;
1571 })
1572 )
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001573 csstate->csum = sum;
1574 csstate->off = off;
Al Viroa604ec72014-11-24 01:08:00 -05001575 return bytes;
1576}
1577EXPORT_SYMBOL(csum_and_copy_to_iter);
1578
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001579size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1580 struct iov_iter *i)
1581{
Herbert Xu79990962020-06-12 16:57:37 +10001582#ifdef CONFIG_CRYPTO_HASH
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001583 struct ahash_request *hash = hashp;
1584 struct scatterlist sg;
1585 size_t copied;
1586
1587 copied = copy_to_iter(addr, bytes, i);
1588 sg_init_one(&sg, addr, copied);
1589 ahash_request_set_crypt(hash, &sg, NULL, copied);
1590 crypto_ahash_update(hash);
1591 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001592#else
1593 return 0;
1594#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001595}
1596EXPORT_SYMBOL(hash_and_copy_to_iter);
1597
Al Viro62a80672014-04-04 23:12:29 -04001598int iov_iter_npages(const struct iov_iter *i, int maxpages)
1599{
Al Viroe0f2dc42014-11-27 14:09:46 -05001600 size_t size = i->count;
1601 int npages = 0;
1602
1603 if (!size)
1604 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001605 if (unlikely(iov_iter_is_discard(i)))
1606 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001607
David Howells00e23702018-10-22 13:07:28 +01001608 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001609 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001610 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -04001611 size_t off;
Al Viro241699c2016-09-22 16:33:12 -04001612
1613 if (!sanity(i))
1614 return 0;
1615
David Howells8cefc102019-11-15 13:30:32 +00001616 data_start(i, &iter_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001617 /* some of this one + all after this one */
David Howells8cefc102019-11-15 13:30:32 +00001618 npages = pipe_space_for_user(iter_head, pipe->tail, pipe);
Al Viro241699c2016-09-22 16:33:12 -04001619 if (npages >= maxpages)
1620 return maxpages;
1621 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001622 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;
1627 0;}),({
1628 npages++;
1629 if (npages >= maxpages)
1630 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001631 }),({
1632 unsigned long p = (unsigned long)v.iov_base;
1633 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1634 - p / PAGE_SIZE;
1635 if (npages >= maxpages)
1636 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001637 })
1638 )
1639 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001640}
Al Virof67da302014-03-19 01:16:16 -04001641EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001642
1643const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1644{
1645 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001646 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001647 WARN_ON(1);
1648 return NULL;
1649 }
David Howells9ea9ce02018-10-20 00:57:56 +01001650 if (unlikely(iov_iter_is_discard(new)))
1651 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001652 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001653 return new->bvec = kmemdup(new->bvec,
1654 new->nr_segs * sizeof(struct bio_vec),
1655 flags);
1656 else
1657 /* iovec and kvec have identical layout */
1658 return new->iov = kmemdup(new->iov,
1659 new->nr_segs * sizeof(struct iovec),
1660 flags);
1661}
1662EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001663
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001664static int copy_compat_iovec_from_user(struct iovec *iov,
1665 const struct iovec __user *uvec, unsigned long nr_segs)
1666{
1667 const struct compat_iovec __user *uiov =
1668 (const struct compat_iovec __user *)uvec;
1669 int ret = -EFAULT, i;
1670
Christoph Hellwig37d4f782021-01-11 18:19:26 +01001671 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001672 return -EFAULT;
1673
1674 for (i = 0; i < nr_segs; i++) {
1675 compat_uptr_t buf;
1676 compat_ssize_t len;
1677
1678 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1679 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1680
1681 /* check for compat_size_t not fitting in compat_ssize_t .. */
1682 if (len < 0) {
1683 ret = -EINVAL;
1684 goto uaccess_end;
1685 }
1686 iov[i].iov_base = compat_ptr(buf);
1687 iov[i].iov_len = len;
1688 }
1689
1690 ret = 0;
1691uaccess_end:
1692 user_access_end();
1693 return ret;
1694}
1695
1696static int copy_iovec_from_user(struct iovec *iov,
1697 const struct iovec __user *uvec, unsigned long nr_segs)
David Laightfb041b52020-09-25 06:51:39 +02001698{
1699 unsigned long seg;
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001700
1701 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1702 return -EFAULT;
1703 for (seg = 0; seg < nr_segs; seg++) {
1704 if ((ssize_t)iov[seg].iov_len < 0)
1705 return -EINVAL;
1706 }
1707
1708 return 0;
1709}
1710
1711struct iovec *iovec_from_user(const struct iovec __user *uvec,
1712 unsigned long nr_segs, unsigned long fast_segs,
1713 struct iovec *fast_iov, bool compat)
1714{
1715 struct iovec *iov = fast_iov;
1716 int ret;
David Laightfb041b52020-09-25 06:51:39 +02001717
1718 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001719 * SuS says "The readv() function *may* fail if the iovcnt argument was
1720 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
David Laightfb041b52020-09-25 06:51:39 +02001721 * traditionally returned zero for zero segments, so...
1722 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001723 if (nr_segs == 0)
1724 return iov;
1725 if (nr_segs > UIO_MAXIOV)
1726 return ERR_PTR(-EINVAL);
David Laightfb041b52020-09-25 06:51:39 +02001727 if (nr_segs > fast_segs) {
1728 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001729 if (!iov)
1730 return ERR_PTR(-ENOMEM);
David Laightfb041b52020-09-25 06:51:39 +02001731 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001732
1733 if (compat)
1734 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1735 else
1736 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1737 if (ret) {
1738 if (iov != fast_iov)
1739 kfree(iov);
1740 return ERR_PTR(ret);
1741 }
1742
1743 return iov;
1744}
1745
1746ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1747 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1748 struct iov_iter *i, bool compat)
1749{
1750 ssize_t total_len = 0;
1751 unsigned long seg;
1752 struct iovec *iov;
1753
1754 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1755 if (IS_ERR(iov)) {
1756 *iovp = NULL;
1757 return PTR_ERR(iov);
David Laightfb041b52020-09-25 06:51:39 +02001758 }
1759
1760 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001761 * According to the Single Unix Specification we should return EINVAL if
1762 * an element length is < 0 when cast to ssize_t or if the total length
1763 * would overflow the ssize_t return value of the system call.
David Laightfb041b52020-09-25 06:51:39 +02001764 *
1765 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1766 * overflow case.
1767 */
David Laightfb041b52020-09-25 06:51:39 +02001768 for (seg = 0; seg < nr_segs; seg++) {
David Laightfb041b52020-09-25 06:51:39 +02001769 ssize_t len = (ssize_t)iov[seg].iov_len;
1770
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001771 if (!access_ok(iov[seg].iov_base, len)) {
1772 if (iov != *iovp)
1773 kfree(iov);
1774 *iovp = NULL;
1775 return -EFAULT;
David Laightfb041b52020-09-25 06:51:39 +02001776 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001777
1778 if (len > MAX_RW_COUNT - total_len) {
1779 len = MAX_RW_COUNT - total_len;
David Laightfb041b52020-09-25 06:51:39 +02001780 iov[seg].iov_len = len;
1781 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001782 total_len += len;
David Laightfb041b52020-09-25 06:51:39 +02001783 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001784
1785 iov_iter_init(i, type, iov, nr_segs, total_len);
1786 if (iov == *iovp)
1787 *iovp = NULL;
1788 else
1789 *iovp = iov;
1790 return total_len;
David Laightfb041b52020-09-25 06:51:39 +02001791}
1792
Vegard Nossumffecee42016-10-08 11:18:07 +02001793/**
1794 * import_iovec() - Copy an array of &struct iovec from userspace
1795 * into the kernel, check that it is valid, and initialize a new
1796 * &struct iov_iter iterator to access it.
1797 *
1798 * @type: One of %READ or %WRITE.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001799 * @uvec: Pointer to the userspace array.
Vegard Nossumffecee42016-10-08 11:18:07 +02001800 * @nr_segs: Number of elements in userspace array.
1801 * @fast_segs: Number of elements in @iov.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001802 * @iovp: (input and output parameter) Pointer to pointer to (usually small
Vegard Nossumffecee42016-10-08 11:18:07 +02001803 * on-stack) kernel array.
1804 * @i: Pointer to iterator that will be initialized on success.
1805 *
1806 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1807 * then this function places %NULL in *@iov on return. Otherwise, a new
1808 * array will be allocated and the result placed in *@iov. This means that
1809 * the caller may call kfree() on *@iov regardless of whether the small
1810 * on-stack array was used or not (and regardless of whether this function
1811 * returns an error or not).
1812 *
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001813 * Return: Negative error code on error, bytes imported on success
Vegard Nossumffecee42016-10-08 11:18:07 +02001814 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001815ssize_t import_iovec(int type, const struct iovec __user *uvec,
Al Virobc917be2015-03-21 17:45:43 -04001816 unsigned nr_segs, unsigned fast_segs,
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001817 struct iovec **iovp, struct iov_iter *i)
Al Virobc917be2015-03-21 17:45:43 -04001818{
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02001819 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1820 in_compat_syscall());
Al Virobc917be2015-03-21 17:45:43 -04001821}
1822EXPORT_SYMBOL(import_iovec);
1823
Al Virobc917be2015-03-21 17:45:43 -04001824int import_single_range(int rw, void __user *buf, size_t len,
1825 struct iovec *iov, struct iov_iter *i)
1826{
1827 if (len > MAX_RW_COUNT)
1828 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001829 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04001830 return -EFAULT;
1831
1832 iov->iov_base = buf;
1833 iov->iov_len = len;
1834 iov_iter_init(i, rw, iov, 1, len);
1835 return 0;
1836}
Al Viroe1267582015-12-06 20:38:56 -05001837EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05001838
Jens Axboee86db872021-09-10 11:18:36 -06001839/**
1840 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
1841 * iov_iter_save_state() was called.
1842 *
1843 * @i: &struct iov_iter to restore
1844 * @state: state to restore from
1845 *
1846 * Used after iov_iter_save_state() to bring restore @i, if operations may
1847 * have advanced it.
1848 *
1849 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
1850 */
1851void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
Al Viro09cf6982017-02-18 01:44:03 -05001852{
Jens Axboee86db872021-09-10 11:18:36 -06001853 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
1854 !iov_iter_is_kvec(i))
1855 return;
1856 i->iov_offset = state->iov_offset;
1857 i->count = state->count;
1858 /*
1859 * For the *vec iters, nr_segs + iov is constant - if we increment
1860 * the vec, then we also decrement the nr_segs count. Hence we don't
1861 * need to track both of these, just one is enough and we can deduct
1862 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
1863 * size, so we can just increment the iov pointer as they are unionzed.
1864 * ITER_BVEC _may_ be the same size on some archs, but on others it is
1865 * not. Be safe and handle it separately.
1866 */
1867 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
1868 if (iov_iter_is_bvec(i))
1869 i->bvec -= state->nr_segs - i->nr_segs;
1870 else
1871 i->iov -= state->nr_segs - i->nr_segs;
1872 i->nr_segs = state->nr_segs;
Al Viro09cf6982017-02-18 01:44:03 -05001873}