blob: 6e30113303ba6047e67b2a02ed78e7750a2d193a [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
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400470static void memzero_page(struct page *page, size_t offset, size_t len)
471{
472 char *addr = kmap_atomic(page);
473 memset(addr + offset, 0, len);
474 kunmap_atomic(addr);
475}
476
Al Viro241699c2016-09-22 16:33:12 -0400477static inline bool allocated(struct pipe_buffer *buf)
478{
479 return buf->ops == &default_pipe_buf_ops;
480}
481
David Howells8cefc102019-11-15 13:30:32 +0000482static inline void data_start(const struct iov_iter *i,
483 unsigned int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400484{
David Howells8cefc102019-11-15 13:30:32 +0000485 unsigned int p_mask = i->pipe->ring_size - 1;
486 unsigned int iter_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400487 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000488
489 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
490 off == PAGE_SIZE)) {
491 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400492 off = 0;
493 }
David Howells8cefc102019-11-15 13:30:32 +0000494 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400495 *offp = off;
496}
497
498static size_t push_pipe(struct iov_iter *i, size_t size,
David Howells8cefc102019-11-15 13:30:32 +0000499 int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400500{
501 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000502 unsigned int p_tail = pipe->tail;
503 unsigned int p_mask = pipe->ring_size - 1;
504 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400505 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400506 ssize_t left;
507
508 if (unlikely(size > i->count))
509 size = i->count;
510 if (unlikely(!size))
511 return 0;
512
513 left = size;
David Howells8cefc102019-11-15 13:30:32 +0000514 data_start(i, &iter_head, &off);
515 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400516 *offp = off;
517 if (off) {
518 left -= PAGE_SIZE - off;
519 if (left <= 0) {
David Howells8cefc102019-11-15 13:30:32 +0000520 pipe->bufs[iter_head & p_mask].len += size;
Al Viro241699c2016-09-22 16:33:12 -0400521 return size;
522 }
David Howells8cefc102019-11-15 13:30:32 +0000523 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
524 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400525 }
David Howells6718b6f2019-10-16 16:47:32 +0100526 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000527 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400528 struct page *page = alloc_page(GFP_USER);
529 if (!page)
530 break;
David Howells8cefc102019-11-15 13:30:32 +0000531
532 buf->ops = &default_pipe_buf_ops;
Max Kellermannb19ec7a2022-02-21 11:03:13 +0100533 buf->flags = 0;
David Howells8cefc102019-11-15 13:30:32 +0000534 buf->page = page;
535 buf->offset = 0;
536 buf->len = min_t(ssize_t, left, PAGE_SIZE);
537 left -= buf->len;
538 iter_head++;
539 pipe->head = iter_head;
540
541 if (left == 0)
Al Viro241699c2016-09-22 16:33:12 -0400542 return size;
Al Viro241699c2016-09-22 16:33:12 -0400543 }
544 return size - left;
545}
546
547static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
548 struct iov_iter *i)
549{
550 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000551 unsigned int p_mask = pipe->ring_size - 1;
552 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400553 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400554
555 if (!sanity(i))
556 return 0;
557
David Howells8cefc102019-11-15 13:30:32 +0000558 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400559 if (unlikely(!n))
560 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000561 do {
Al Viro241699c2016-09-22 16:33:12 -0400562 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000563 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
564 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400565 i->iov_offset = off + chunk;
566 n -= chunk;
567 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000568 off = 0;
569 i_head++;
570 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400571 i->count -= bytes;
572 return bytes;
573}
574
Al Virof9152892018-11-27 22:32:59 -0500575static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
576 __wsum sum, size_t off)
577{
Al Virocc44c172020-07-11 00:12:07 -0400578 __wsum next = csum_partial_copy_nocheck(from, to, len);
Al Virof9152892018-11-27 22:32:59 -0500579 return csum_block_add(sum, next, off);
580}
581
Al Viro78e1f382018-11-25 16:24:16 -0500582static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
Willem de Bruijn46a831d2021-02-03 14:29:52 -0500583 struct csum_state *csstate,
584 struct iov_iter *i)
Al Viro78e1f382018-11-25 16:24:16 -0500585{
586 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000587 unsigned int p_mask = pipe->ring_size - 1;
Willem de Bruijn46a831d2021-02-03 14:29:52 -0500588 __wsum sum = csstate->csum;
589 size_t off = csstate->off;
David Howells8cefc102019-11-15 13:30:32 +0000590 unsigned int i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500591 size_t n, r;
Al Viro78e1f382018-11-25 16:24:16 -0500592
593 if (!sanity(i))
594 return 0;
595
David Howells8cefc102019-11-15 13:30:32 +0000596 bytes = n = push_pipe(i, bytes, &i_head, &r);
Al Viro78e1f382018-11-25 16:24:16 -0500597 if (unlikely(!n))
598 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000599 do {
Al Viro78e1f382018-11-25 16:24:16 -0500600 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
David Howells8cefc102019-11-15 13:30:32 +0000601 char *p = kmap_atomic(pipe->bufs[i_head & p_mask].page);
Al Virof9152892018-11-27 22:32:59 -0500602 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
Al Viro78e1f382018-11-25 16:24:16 -0500603 kunmap_atomic(p);
David Howells8cefc102019-11-15 13:30:32 +0000604 i->head = i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500605 i->iov_offset = r + chunk;
606 n -= chunk;
607 off += chunk;
608 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000609 r = 0;
610 i_head++;
611 } while (n);
Al Viro78e1f382018-11-25 16:24:16 -0500612 i->count -= bytes;
Willem de Bruijn46a831d2021-02-03 14:29:52 -0500613 csstate->csum = sum;
614 csstate->off = off;
Al Viro78e1f382018-11-25 16:24:16 -0500615 return bytes;
616}
617
Al Viroaa28de22017-06-29 21:45:10 -0400618size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400619{
Al Viro36f7a8a2015-12-06 16:49:22 -0500620 const char *from = addr;
David Howells00e23702018-10-22 13:07:28 +0100621 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400622 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400623 if (iter_is_iovec(i))
624 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500625 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400626 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500627 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500628 (from += v.bv_len) - v.bv_len, v.bv_len),
629 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500630 )
Al Viro62a80672014-04-04 23:12:29 -0400631
Al Viro3d4d3e42014-11-27 14:28:06 -0500632 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400633}
Al Viroaa28de22017-06-29 21:45:10 -0400634EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400635
Dan Williamsec6347b2020-10-05 20:40:16 -0700636#ifdef CONFIG_ARCH_HAS_COPY_MC
637static int copyout_mc(void __user *to, const void *from, size_t n)
Dan Williams87803562018-05-03 17:06:31 -0700638{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800639 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100640 instrument_copy_to_user(to, from, n);
Dan Williamsec6347b2020-10-05 20:40:16 -0700641 n = copy_mc_to_user((__force void *) to, from, n);
Dan Williams87803562018-05-03 17:06:31 -0700642 }
643 return n;
644}
645
Dan Williamsec6347b2020-10-05 20:40:16 -0700646static unsigned long copy_mc_to_page(struct page *page, size_t offset,
Dan Williams87803562018-05-03 17:06:31 -0700647 const char *from, size_t len)
648{
649 unsigned long ret;
650 char *to;
651
652 to = kmap_atomic(page);
Dan Williamsec6347b2020-10-05 20:40:16 -0700653 ret = copy_mc_to_kernel(to + offset, from, len);
Dan Williams87803562018-05-03 17:06:31 -0700654 kunmap_atomic(to);
655
656 return ret;
657}
658
Dan Williamsec6347b2020-10-05 20:40:16 -0700659static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
Dan Williamsca146f62018-07-08 13:46:12 -0700660 struct iov_iter *i)
661{
662 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000663 unsigned int p_mask = pipe->ring_size - 1;
664 unsigned int i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700665 size_t n, off, xfer = 0;
Dan Williamsca146f62018-07-08 13:46:12 -0700666
667 if (!sanity(i))
668 return 0;
669
David Howells8cefc102019-11-15 13:30:32 +0000670 bytes = n = push_pipe(i, bytes, &i_head, &off);
Dan Williamsca146f62018-07-08 13:46:12 -0700671 if (unlikely(!n))
672 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000673 do {
Dan Williamsca146f62018-07-08 13:46:12 -0700674 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
675 unsigned long rem;
676
Dan Williamsec6347b2020-10-05 20:40:16 -0700677 rem = copy_mc_to_page(pipe->bufs[i_head & p_mask].page,
David Howells8cefc102019-11-15 13:30:32 +0000678 off, addr, chunk);
679 i->head = i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700680 i->iov_offset = off + chunk - rem;
681 xfer += chunk - rem;
682 if (rem)
683 break;
684 n -= chunk;
685 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000686 off = 0;
687 i_head++;
688 } while (n);
Dan Williamsca146f62018-07-08 13:46:12 -0700689 i->count -= xfer;
690 return xfer;
691}
692
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700693/**
Dan Williamsec6347b2020-10-05 20:40:16 -0700694 * _copy_mc_to_iter - copy to iter with source memory error exception handling
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700695 * @addr: source kernel address
696 * @bytes: total transfer length
697 * @iter: destination iterator
698 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700699 * The pmem driver deploys this for the dax operation
700 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
701 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
702 * successfully copied.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700703 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700704 * The main differences between this and typical _copy_to_iter().
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700705 *
706 * * Typical tail/residue handling after a fault retries the copy
707 * byte-by-byte until the fault happens again. Re-triggering machine
708 * checks is potentially fatal so the implementation uses source
709 * alignment and poison alignment assumptions to avoid re-triggering
710 * hardware exceptions.
711 *
712 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
713 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
714 * a short copy.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700715 */
Dan Williamsec6347b2020-10-05 20:40:16 -0700716size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Dan Williams87803562018-05-03 17:06:31 -0700717{
718 const char *from = addr;
719 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
720
David Howells00e23702018-10-22 13:07:28 +0100721 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsec6347b2020-10-05 20:40:16 -0700722 return copy_mc_pipe_to_iter(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700723 if (iter_is_iovec(i))
724 might_fault();
725 iterate_and_advance(i, bytes, v,
Dan Williamsec6347b2020-10-05 20:40:16 -0700726 copyout_mc(v.iov_base, (from += v.iov_len) - v.iov_len,
727 v.iov_len),
Dan Williams87803562018-05-03 17:06:31 -0700728 ({
Dan Williamsec6347b2020-10-05 20:40:16 -0700729 rem = copy_mc_to_page(v.bv_page, v.bv_offset,
730 (from += v.bv_len) - v.bv_len, v.bv_len);
Dan Williams87803562018-05-03 17:06:31 -0700731 if (rem) {
732 curr_addr = (unsigned long) from;
733 bytes = curr_addr - s_addr - rem;
734 return bytes;
735 }
736 }),
737 ({
Dan Williamsec6347b2020-10-05 20:40:16 -0700738 rem = copy_mc_to_kernel(v.iov_base, (from += v.iov_len)
739 - v.iov_len, v.iov_len);
Dan Williams87803562018-05-03 17:06:31 -0700740 if (rem) {
741 curr_addr = (unsigned long) from;
742 bytes = curr_addr - s_addr - rem;
743 return bytes;
744 }
745 })
746 )
747
748 return bytes;
749}
Dan Williamsec6347b2020-10-05 20:40:16 -0700750EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
751#endif /* CONFIG_ARCH_HAS_COPY_MC */
Dan Williams87803562018-05-03 17:06:31 -0700752
Al Viroaa28de22017-06-29 21:45:10 -0400753size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400754{
Al Viro0dbca9a2014-11-27 14:26:43 -0500755 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100756 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400757 WARN_ON(1);
758 return 0;
759 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400760 if (iter_is_iovec(i))
761 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500762 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400763 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500764 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500765 v.bv_offset, v.bv_len),
766 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500767 )
768
769 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400770}
Al Viroaa28de22017-06-29 21:45:10 -0400771EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400772
Al Viroaa28de22017-06-29 21:45:10 -0400773bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400774{
775 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100776 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400777 WARN_ON(1);
778 return false;
779 }
Al Viro33844e62016-12-21 21:55:02 -0500780 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400781 return false;
782
Al Viro09fc68dc2017-06-29 22:25:14 -0400783 if (iter_is_iovec(i))
784 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400785 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68dc2017-06-29 22:25:14 -0400786 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400787 v.iov_base, v.iov_len))
788 return false;
789 0;}),
790 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
791 v.bv_offset, v.bv_len),
792 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
793 )
794
795 iov_iter_advance(i, bytes);
796 return true;
797}
Al Viroaa28de22017-06-29 21:45:10 -0400798EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400799
Al Viroaa28de22017-06-29 21:45:10 -0400800size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500801{
802 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100803 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400804 WARN_ON(1);
805 return 0;
806 }
Al Viroaa583092014-11-27 20:27:08 -0500807 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400808 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500809 v.iov_base, v.iov_len),
810 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
811 v.bv_offset, v.bv_len),
812 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
813 )
814
815 return bytes;
816}
Al Viroaa28de22017-06-29 21:45:10 -0400817EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500818
Dan Williams0aed55a2017-05-29 12:22:50 -0700819#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700820/**
821 * _copy_from_iter_flushcache - write destination through cpu cache
822 * @addr: destination kernel address
823 * @bytes: total transfer length
824 * @iter: source iterator
825 *
826 * The pmem driver arranges for filesystem-dax to use this facility via
827 * dax_copy_from_iter() for ensuring that writes to persistent memory
828 * are flushed through the CPU cache. It is differentiated from
829 * _copy_from_iter_nocache() in that guarantees all data is flushed for
830 * all iterator types. The _copy_from_iter_nocache() only attempts to
831 * bypass the cache for the ITER_IOVEC case, and on some archs may use
832 * instructions that strand dirty-data in the cache.
833 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700834size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700835{
836 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100837 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700838 WARN_ON(1);
839 return 0;
840 }
841 iterate_and_advance(i, bytes, v,
842 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
843 v.iov_base, v.iov_len),
844 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
845 v.bv_offset, v.bv_len),
846 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
847 v.iov_len)
848 )
849
850 return bytes;
851}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700852EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700853#endif
854
Al Viroaa28de22017-06-29 21:45:10 -0400855bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400856{
857 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100858 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400859 WARN_ON(1);
860 return false;
861 }
Al Viro33844e62016-12-21 21:55:02 -0500862 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400863 return false;
864 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400865 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400866 v.iov_base, v.iov_len))
867 return false;
868 0;}),
869 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
870 v.bv_offset, v.bv_len),
871 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
872 )
873
874 iov_iter_advance(i, bytes);
875 return true;
876}
Al Viroaa28de22017-06-29 21:45:10 -0400877EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400878
Al Viro72e809e2017-06-29 21:52:57 -0400879static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
880{
Eric Dumazet6daef952019-02-26 10:42:39 -0800881 struct page *head;
882 size_t v = n + offset;
883
884 /*
885 * The general case needs to access the page order in order
886 * to compute the page size.
887 * However, we mostly deal with order-0 pages and thus can
888 * avoid a possible cache line miss for requests that fit all
889 * page orders.
890 */
891 if (n <= v && v <= PAGE_SIZE)
892 return true;
893
894 head = compound_head(page);
895 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700896
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700897 if (likely(n <= v && v <= (page_size(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400898 return true;
899 WARN_ON(1);
900 return false;
901}
Al Virod2715242014-11-27 14:22:37 -0500902
903size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
904 struct iov_iter *i)
905{
Al Viro72e809e2017-06-29 21:52:57 -0400906 if (unlikely(!page_copy_sane(page, offset, bytes)))
907 return 0;
Al Virod2715242014-11-27 14:22:37 -0500908 if (i->type & (ITER_BVEC|ITER_KVEC)) {
909 void *kaddr = kmap_atomic(page);
910 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
911 kunmap_atomic(kaddr);
912 return wanted;
Al Virob6df9e42021-04-27 12:34:04 -0400913 } else if (unlikely(iov_iter_is_discard(i))) {
914 if (unlikely(i->count < bytes))
915 bytes = i->count;
916 i->count -= bytes;
David Howells9ea9ce02018-10-20 00:57:56 +0100917 return bytes;
Al Virob6df9e42021-04-27 12:34:04 -0400918 } else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500919 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400920 else
921 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500922}
923EXPORT_SYMBOL(copy_page_to_iter);
924
925size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
926 struct iov_iter *i)
927{
Al Viro72e809e2017-06-29 21:52:57 -0400928 if (unlikely(!page_copy_sane(page, offset, bytes)))
929 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +0100930 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400931 WARN_ON(1);
932 return 0;
933 }
Al Virod2715242014-11-27 14:22:37 -0500934 if (i->type & (ITER_BVEC|ITER_KVEC)) {
935 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400936 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500937 kunmap_atomic(kaddr);
938 return wanted;
939 } else
940 return copy_page_from_iter_iovec(page, offset, bytes, i);
941}
942EXPORT_SYMBOL(copy_page_from_iter);
943
Al Viro241699c2016-09-22 16:33:12 -0400944static size_t pipe_zero(size_t bytes, struct iov_iter *i)
945{
946 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000947 unsigned int p_mask = pipe->ring_size - 1;
948 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400949 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400950
951 if (!sanity(i))
952 return 0;
953
David Howells8cefc102019-11-15 13:30:32 +0000954 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400955 if (unlikely(!n))
956 return 0;
957
David Howells8cefc102019-11-15 13:30:32 +0000958 do {
Al Viro241699c2016-09-22 16:33:12 -0400959 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000960 memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk);
961 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400962 i->iov_offset = off + chunk;
963 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000964 off = 0;
965 i_head++;
966 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400967 i->count -= bytes;
968 return bytes;
969}
970
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400971size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
972{
David Howells00e23702018-10-22 13:07:28 +0100973 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400974 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500975 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400976 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500977 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
978 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500979 )
980
981 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400982}
983EXPORT_SYMBOL(iov_iter_zero);
984
Al Viro62a80672014-04-04 23:12:29 -0400985size_t iov_iter_copy_from_user_atomic(struct page *page,
986 struct iov_iter *i, unsigned long offset, size_t bytes)
987{
Al Viro04a31162014-11-27 13:51:41 -0500988 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400989 if (unlikely(!page_copy_sane(page, offset, bytes))) {
990 kunmap_atomic(kaddr);
991 return 0;
992 }
David Howells9ea9ce02018-10-20 00:57:56 +0100993 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400994 kunmap_atomic(kaddr);
995 WARN_ON(1);
996 return 0;
997 }
Al Viro04a31162014-11-27 13:51:41 -0500998 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400999 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -05001000 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -05001001 v.bv_offset, v.bv_len),
1002 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -05001003 )
1004 kunmap_atomic(kaddr);
1005 return bytes;
Al Viro62a80672014-04-04 23:12:29 -04001006}
1007EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
1008
Al Virob9dc6f62017-01-14 19:33:08 -05001009static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -04001010{
1011 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001012 unsigned int p_tail = pipe->tail;
1013 unsigned int p_head = pipe->head;
1014 unsigned int p_mask = pipe->ring_size - 1;
1015
1016 if (!pipe_empty(p_head, p_tail)) {
1017 struct pipe_buffer *buf;
1018 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001019 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +00001020
Al Virob9dc6f62017-01-14 19:33:08 -05001021 if (off) {
David Howells8cefc102019-11-15 13:30:32 +00001022 buf = &pipe->bufs[i_head & p_mask];
1023 buf->len = off - buf->offset;
1024 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001025 }
David Howells8cefc102019-11-15 13:30:32 +00001026 while (p_head != i_head) {
1027 p_head--;
1028 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
Al Viro241699c2016-09-22 16:33:12 -04001029 }
David Howells8cefc102019-11-15 13:30:32 +00001030
1031 pipe->head = p_head;
Al Viro241699c2016-09-22 16:33:12 -04001032 }
Al Virob9dc6f62017-01-14 19:33:08 -05001033}
1034
1035static void pipe_advance(struct iov_iter *i, size_t size)
1036{
1037 struct pipe_inode_info *pipe = i->pipe;
1038 if (unlikely(i->count < size))
1039 size = i->count;
1040 if (size) {
1041 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +00001042 unsigned int p_mask = pipe->ring_size - 1;
1043 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001044 size_t off = i->iov_offset, left = size;
David Howells8cefc102019-11-15 13:30:32 +00001045
Al Virob9dc6f62017-01-14 19:33:08 -05001046 if (off) /* make it relative to the beginning of buffer */
David Howells8cefc102019-11-15 13:30:32 +00001047 left += off - pipe->bufs[i_head & p_mask].offset;
Al Virob9dc6f62017-01-14 19:33:08 -05001048 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001049 buf = &pipe->bufs[i_head & p_mask];
Al Virob9dc6f62017-01-14 19:33:08 -05001050 if (left <= buf->len)
1051 break;
1052 left -= buf->len;
David Howells8cefc102019-11-15 13:30:32 +00001053 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001054 }
David Howells8cefc102019-11-15 13:30:32 +00001055 i->head = i_head;
Al Virob9dc6f62017-01-14 19:33:08 -05001056 i->iov_offset = buf->offset + left;
1057 }
1058 i->count -= size;
1059 /* ... and discard everything past that point */
1060 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001061}
1062
Al Viro62a80672014-04-04 23:12:29 -04001063void iov_iter_advance(struct iov_iter *i, size_t size)
1064{
David Howells00e23702018-10-22 13:07:28 +01001065 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001066 pipe_advance(i, size);
1067 return;
1068 }
David Howells9ea9ce02018-10-20 00:57:56 +01001069 if (unlikely(iov_iter_is_discard(i))) {
1070 i->count -= size;
1071 return;
1072 }
Al Viroa2804552014-11-27 14:48:42 -05001073 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -04001074}
1075EXPORT_SYMBOL(iov_iter_advance);
1076
Al Viro27c0e372017-02-17 18:42:24 -05001077void iov_iter_revert(struct iov_iter *i, size_t unroll)
1078{
1079 if (!unroll)
1080 return;
Al Viro5b47d592017-05-08 13:54:47 -04001081 if (WARN_ON(unroll > MAX_RW_COUNT))
1082 return;
Al Viro27c0e372017-02-17 18:42:24 -05001083 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001084 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001085 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001086 unsigned int p_mask = pipe->ring_size - 1;
1087 unsigned int i_head = i->head;
Al Viro27c0e372017-02-17 18:42:24 -05001088 size_t off = i->iov_offset;
1089 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001090 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1091 size_t n = off - b->offset;
Al Viro27c0e372017-02-17 18:42:24 -05001092 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001093 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001094 break;
1095 }
1096 unroll -= n;
David Howells8cefc102019-11-15 13:30:32 +00001097 if (!unroll && i_head == i->start_head) {
Al Viro27c0e372017-02-17 18:42:24 -05001098 off = 0;
1099 break;
1100 }
David Howells8cefc102019-11-15 13:30:32 +00001101 i_head--;
1102 b = &pipe->bufs[i_head & p_mask];
1103 off = b->offset + b->len;
Al Viro27c0e372017-02-17 18:42:24 -05001104 }
1105 i->iov_offset = off;
David Howells8cefc102019-11-15 13:30:32 +00001106 i->head = i_head;
Al Viro27c0e372017-02-17 18:42:24 -05001107 pipe_truncate(i);
1108 return;
1109 }
David Howells9ea9ce02018-10-20 00:57:56 +01001110 if (unlikely(iov_iter_is_discard(i)))
1111 return;
Al Viro27c0e372017-02-17 18:42:24 -05001112 if (unroll <= i->iov_offset) {
1113 i->iov_offset -= unroll;
1114 return;
1115 }
1116 unroll -= i->iov_offset;
David Howells00e23702018-10-22 13:07:28 +01001117 if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001118 const struct bio_vec *bvec = i->bvec;
1119 while (1) {
1120 size_t n = (--bvec)->bv_len;
1121 i->nr_segs++;
1122 if (unroll <= n) {
1123 i->bvec = bvec;
1124 i->iov_offset = n - unroll;
1125 return;
1126 }
1127 unroll -= n;
1128 }
1129 } else { /* same logics for iovec and kvec */
1130 const struct iovec *iov = i->iov;
1131 while (1) {
1132 size_t n = (--iov)->iov_len;
1133 i->nr_segs++;
1134 if (unroll <= n) {
1135 i->iov = iov;
1136 i->iov_offset = n - unroll;
1137 return;
1138 }
1139 unroll -= n;
1140 }
1141 }
1142}
1143EXPORT_SYMBOL(iov_iter_revert);
1144
Al Viro62a80672014-04-04 23:12:29 -04001145/*
1146 * Return the count of just the current iov_iter segment.
1147 */
1148size_t iov_iter_single_seg_count(const struct iov_iter *i)
1149{
David Howells00e23702018-10-22 13:07:28 +01001150 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001151 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001152 if (i->nr_segs == 1)
1153 return i->count;
David Howells9ea9ce02018-10-20 00:57:56 +01001154 if (unlikely(iov_iter_is_discard(i)))
1155 return i->count;
David Howells00e23702018-10-22 13:07:28 +01001156 else if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001157 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001158 else
1159 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001160}
1161EXPORT_SYMBOL(iov_iter_single_seg_count);
1162
David Howellsaa563d72018-10-20 00:57:56 +01001163void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001164 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001165 size_t count)
1166{
David Howellsaa563d72018-10-20 00:57:56 +01001167 WARN_ON(direction & ~(READ | WRITE));
1168 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001169 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001170 i->nr_segs = nr_segs;
1171 i->iov_offset = 0;
1172 i->count = count;
1173}
1174EXPORT_SYMBOL(iov_iter_kvec);
1175
David Howellsaa563d72018-10-20 00:57:56 +01001176void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001177 const struct bio_vec *bvec, unsigned long nr_segs,
1178 size_t count)
1179{
David Howellsaa563d72018-10-20 00:57:56 +01001180 WARN_ON(direction & ~(READ | WRITE));
1181 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001182 i->bvec = bvec;
1183 i->nr_segs = nr_segs;
1184 i->iov_offset = 0;
1185 i->count = count;
1186}
1187EXPORT_SYMBOL(iov_iter_bvec);
1188
David Howellsaa563d72018-10-20 00:57:56 +01001189void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001190 struct pipe_inode_info *pipe,
1191 size_t count)
1192{
David Howellsaa563d72018-10-20 00:57:56 +01001193 BUG_ON(direction != READ);
David Howells8cefc102019-11-15 13:30:32 +00001194 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
David Howellsaa563d72018-10-20 00:57:56 +01001195 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001196 i->pipe = pipe;
David Howells8cefc102019-11-15 13:30:32 +00001197 i->head = pipe->head;
Al Viro241699c2016-09-22 16:33:12 -04001198 i->iov_offset = 0;
1199 i->count = count;
David Howells8cefc102019-11-15 13:30:32 +00001200 i->start_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -04001201}
1202EXPORT_SYMBOL(iov_iter_pipe);
1203
David Howells9ea9ce02018-10-20 00:57:56 +01001204/**
1205 * iov_iter_discard - Initialise an I/O iterator that discards data
1206 * @i: The iterator to initialise.
1207 * @direction: The direction of the transfer.
1208 * @count: The size of the I/O buffer in bytes.
1209 *
1210 * Set up an I/O iterator that just discards everything that's written to it.
1211 * It's only available as a READ iterator.
1212 */
1213void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1214{
1215 BUG_ON(direction != READ);
1216 i->type = ITER_DISCARD | READ;
1217 i->count = count;
1218 i->iov_offset = 0;
1219}
1220EXPORT_SYMBOL(iov_iter_discard);
1221
Al Viro62a80672014-04-04 23:12:29 -04001222unsigned long iov_iter_alignment(const struct iov_iter *i)
1223{
Al Viro04a31162014-11-27 13:51:41 -05001224 unsigned long res = 0;
1225 size_t size = i->count;
1226
David Howells00e23702018-10-22 13:07:28 +01001227 if (unlikely(iov_iter_is_pipe(i))) {
Jan Karae0ff1262019-12-16 11:54:32 +01001228 unsigned int p_mask = i->pipe->ring_size - 1;
1229
David Howells8cefc102019-11-15 13:30:32 +00001230 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
Al Viro241699c2016-09-22 16:33:12 -04001231 return size | i->iov_offset;
1232 return size;
1233 }
Al Viro04a31162014-11-27 13:51:41 -05001234 iterate_all_kinds(i, size, v,
1235 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001236 res |= v.bv_offset | v.bv_len,
1237 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -05001238 )
1239 return res;
Al Viro62a80672014-04-04 23:12:29 -04001240}
1241EXPORT_SYMBOL(iov_iter_alignment);
1242
Al Viro357f4352016-04-08 19:05:19 -04001243unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1244{
Al Viro33844e62016-12-21 21:55:02 -05001245 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001246 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001247
David Howells9ea9ce02018-10-20 00:57:56 +01001248 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001249 WARN_ON(1);
1250 return ~0U;
1251 }
1252
Al Viro357f4352016-04-08 19:05:19 -04001253 iterate_all_kinds(i, size, v,
1254 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1255 (size != v.iov_len ? size : 0), 0),
1256 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1257 (size != v.bv_len ? size : 0)),
1258 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1259 (size != v.iov_len ? size : 0))
1260 );
Al Viro33844e62016-12-21 21:55:02 -05001261 return res;
Al Viro357f4352016-04-08 19:05:19 -04001262}
1263EXPORT_SYMBOL(iov_iter_gap_alignment);
1264
Ilya Dryomove76b63122018-05-02 20:16:56 +02001265static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001266 size_t maxsize,
1267 struct page **pages,
David Howells8cefc102019-11-15 13:30:32 +00001268 int iter_head,
Al Viro241699c2016-09-22 16:33:12 -04001269 size_t *start)
1270{
1271 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001272 unsigned int p_mask = pipe->ring_size - 1;
1273 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001274 if (!n)
1275 return -EFAULT;
1276
1277 maxsize = n;
1278 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001279 while (n > 0) {
David Howells8cefc102019-11-15 13:30:32 +00001280 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1281 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -04001282 n -= PAGE_SIZE;
1283 }
1284
1285 return maxsize;
1286}
1287
1288static ssize_t pipe_get_pages(struct iov_iter *i,
1289 struct page **pages, size_t maxsize, unsigned maxpages,
1290 size_t *start)
1291{
David Howells8cefc102019-11-15 13:30:32 +00001292 unsigned int iter_head, npages;
Al Viro241699c2016-09-22 16:33:12 -04001293 size_t capacity;
Al Viro241699c2016-09-22 16:33:12 -04001294
Al Viro33844e62016-12-21 21:55:02 -05001295 if (!maxsize)
1296 return 0;
1297
Al Viro241699c2016-09-22 16:33:12 -04001298 if (!sanity(i))
1299 return -EFAULT;
1300
David Howells8cefc102019-11-15 13:30:32 +00001301 data_start(i, &iter_head, start);
1302 /* Amount of free space: some of this one + all after this one */
1303 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1304 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
Al Viro241699c2016-09-22 16:33:12 -04001305
David Howells8cefc102019-11-15 13:30:32 +00001306 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001307}
1308
Al Viro62a80672014-04-04 23:12:29 -04001309ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001310 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001311 size_t *start)
1312{
Al Viroe5393fa2014-11-27 14:12:09 -05001313 if (maxsize > i->count)
1314 maxsize = i->count;
1315
David Howells00e23702018-10-22 13:07:28 +01001316 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001317 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001318 if (unlikely(iov_iter_is_discard(i)))
1319 return -EFAULT;
1320
Al Viroe5393fa2014-11-27 14:12:09 -05001321 iterate_all_kinds(i, maxsize, v, ({
1322 unsigned long addr = (unsigned long)v.iov_base;
1323 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1324 int n;
1325 int res;
1326
1327 if (len > maxpages * PAGE_SIZE)
1328 len = maxpages * PAGE_SIZE;
1329 addr &= ~(PAGE_SIZE - 1);
1330 n = DIV_ROUND_UP(len, PAGE_SIZE);
Ira Weiny73b01402019-05-13 17:17:11 -07001331 res = get_user_pages_fast(addr, n,
1332 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1333 pages);
Andreas Gruenbacherb6008662021-07-21 19:03:47 +02001334 if (unlikely(res <= 0))
Al Viroe5393fa2014-11-27 14:12:09 -05001335 return res;
1336 return (res == n ? len : res * PAGE_SIZE) - *start;
1337 0;}),({
1338 /* can't be more than PAGE_SIZE */
1339 *start = v.bv_offset;
1340 get_page(*pages = v.bv_page);
1341 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001342 }),({
1343 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001344 })
1345 )
1346 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001347}
1348EXPORT_SYMBOL(iov_iter_get_pages);
1349
Al Viro1b17f1f2014-11-27 14:14:31 -05001350static struct page **get_pages_array(size_t n)
1351{
Michal Hocko752ade62017-05-08 15:57:27 -07001352 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001353}
1354
Al Viro241699c2016-09-22 16:33:12 -04001355static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1356 struct page ***pages, size_t maxsize,
1357 size_t *start)
1358{
1359 struct page **p;
David Howells8cefc102019-11-15 13:30:32 +00001360 unsigned int iter_head, npages;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001361 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001362
Al Viro33844e62016-12-21 21:55:02 -05001363 if (!maxsize)
1364 return 0;
1365
Al Viro241699c2016-09-22 16:33:12 -04001366 if (!sanity(i))
1367 return -EFAULT;
1368
David Howells8cefc102019-11-15 13:30:32 +00001369 data_start(i, &iter_head, start);
1370 /* Amount of free space: some of this one + all after this one */
1371 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
Al Viro241699c2016-09-22 16:33:12 -04001372 n = npages * PAGE_SIZE - *start;
1373 if (maxsize > n)
1374 maxsize = n;
1375 else
1376 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1377 p = get_pages_array(npages);
1378 if (!p)
1379 return -ENOMEM;
David Howells8cefc102019-11-15 13:30:32 +00001380 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001381 if (n > 0)
1382 *pages = p;
1383 else
1384 kvfree(p);
1385 return n;
1386}
1387
Al Viro62a80672014-04-04 23:12:29 -04001388ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1389 struct page ***pages, size_t maxsize,
1390 size_t *start)
1391{
Al Viro1b17f1f2014-11-27 14:14:31 -05001392 struct page **p;
1393
1394 if (maxsize > i->count)
1395 maxsize = i->count;
1396
David Howells00e23702018-10-22 13:07:28 +01001397 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001398 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001399 if (unlikely(iov_iter_is_discard(i)))
1400 return -EFAULT;
1401
Al Viro1b17f1f2014-11-27 14:14:31 -05001402 iterate_all_kinds(i, maxsize, v, ({
1403 unsigned long addr = (unsigned long)v.iov_base;
1404 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1405 int n;
1406 int res;
1407
1408 addr &= ~(PAGE_SIZE - 1);
1409 n = DIV_ROUND_UP(len, PAGE_SIZE);
1410 p = get_pages_array(n);
1411 if (!p)
1412 return -ENOMEM;
Ira Weiny73b01402019-05-13 17:17:11 -07001413 res = get_user_pages_fast(addr, n,
1414 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
Andreas Gruenbacherb6008662021-07-21 19:03:47 +02001415 if (unlikely(res <= 0)) {
Al Viro1b17f1f2014-11-27 14:14:31 -05001416 kvfree(p);
Andreas Gruenbacherb6008662021-07-21 19:03:47 +02001417 *pages = NULL;
Al Viro1b17f1f2014-11-27 14:14:31 -05001418 return res;
1419 }
1420 *pages = p;
1421 return (res == n ? len : res * PAGE_SIZE) - *start;
1422 0;}),({
1423 /* can't be more than PAGE_SIZE */
1424 *start = v.bv_offset;
1425 *pages = p = get_pages_array(1);
1426 if (!p)
1427 return -ENOMEM;
1428 get_page(*p = v.bv_page);
1429 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001430 }),({
1431 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -05001432 })
1433 )
1434 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001435}
1436EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1437
Al Viroa604ec72014-11-24 01:08:00 -05001438size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1439 struct iov_iter *i)
1440{
1441 char *to = addr;
1442 __wsum sum, next;
1443 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -05001444 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001445 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001446 WARN_ON(1);
1447 return 0;
1448 }
Al Viroa604ec72014-11-24 01:08:00 -05001449 iterate_and_advance(i, bytes, v, ({
Al Virocbbd26b2016-11-01 22:09:04 -04001450 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001451 (to += v.iov_len) - v.iov_len,
Al Viroc693cc42020-07-11 00:27:49 -04001452 v.iov_len);
1453 if (next) {
Al Viroa604ec72014-11-24 01:08:00 -05001454 sum = csum_block_add(sum, next, off);
1455 off += v.iov_len;
1456 }
Al Viroc693cc42020-07-11 00:27:49 -04001457 next ? 0 : v.iov_len;
Al Viroa604ec72014-11-24 01:08:00 -05001458 }), ({
1459 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001460 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1461 p + v.bv_offset, v.bv_len,
1462 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001463 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001464 off += v.bv_len;
1465 }),({
Al Virof9152892018-11-27 22:32:59 -05001466 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1467 v.iov_base, v.iov_len,
1468 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001469 off += v.iov_len;
1470 })
1471 )
1472 *csum = sum;
1473 return bytes;
1474}
1475EXPORT_SYMBOL(csum_and_copy_from_iter);
1476
Al Virocbbd26b2016-11-01 22:09:04 -04001477bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1478 struct iov_iter *i)
1479{
1480 char *to = addr;
1481 __wsum sum, next;
1482 size_t off = 0;
1483 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001484 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001485 WARN_ON(1);
1486 return false;
1487 }
1488 if (unlikely(i->count < bytes))
1489 return false;
1490 iterate_all_kinds(i, bytes, v, ({
Al Virocbbd26b2016-11-01 22:09:04 -04001491 next = csum_and_copy_from_user(v.iov_base,
1492 (to += v.iov_len) - v.iov_len,
Al Viroc693cc42020-07-11 00:27:49 -04001493 v.iov_len);
1494 if (!next)
Al Virocbbd26b2016-11-01 22:09:04 -04001495 return false;
1496 sum = csum_block_add(sum, next, off);
1497 off += v.iov_len;
1498 0;
1499 }), ({
1500 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001501 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1502 p + v.bv_offset, v.bv_len,
1503 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001504 kunmap_atomic(p);
Al Virocbbd26b2016-11-01 22:09:04 -04001505 off += v.bv_len;
1506 }),({
Al Virof9152892018-11-27 22:32:59 -05001507 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1508 v.iov_base, v.iov_len,
1509 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001510 off += v.iov_len;
1511 })
1512 )
1513 *csum = sum;
1514 iov_iter_advance(i, bytes);
1515 return true;
1516}
1517EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1518
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001519size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
Al Viroa604ec72014-11-24 01:08:00 -05001520 struct iov_iter *i)
1521{
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001522 struct csum_state *csstate = _csstate;
Al Viro36f7a8a2015-12-06 16:49:22 -05001523 const char *from = addr;
Al Viroa604ec72014-11-24 01:08:00 -05001524 __wsum sum, next;
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001525 size_t off;
Al Viro78e1f382018-11-25 16:24:16 -05001526
1527 if (unlikely(iov_iter_is_pipe(i)))
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001528 return csum_and_copy_to_pipe_iter(addr, bytes, _csstate, i);
Al Viro78e1f382018-11-25 16:24:16 -05001529
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001530 sum = csstate->csum;
1531 off = csstate->off;
Al Viro78e1f382018-11-25 16:24:16 -05001532 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001533 WARN_ON(1); /* for now */
1534 return 0;
1535 }
Al Viroa604ec72014-11-24 01:08:00 -05001536 iterate_and_advance(i, bytes, v, ({
Al Viroa604ec72014-11-24 01:08:00 -05001537 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001538 v.iov_base,
Al Viroc693cc42020-07-11 00:27:49 -04001539 v.iov_len);
1540 if (next) {
Al Viroa604ec72014-11-24 01:08:00 -05001541 sum = csum_block_add(sum, next, off);
1542 off += v.iov_len;
1543 }
Al Viroc693cc42020-07-11 00:27:49 -04001544 next ? 0 : v.iov_len;
Al Viroa604ec72014-11-24 01:08:00 -05001545 }), ({
1546 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001547 sum = csum_and_memcpy(p + v.bv_offset,
1548 (from += v.bv_len) - v.bv_len,
1549 v.bv_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001550 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001551 off += v.bv_len;
1552 }),({
Al Virof9152892018-11-27 22:32:59 -05001553 sum = csum_and_memcpy(v.iov_base,
1554 (from += v.iov_len) - v.iov_len,
1555 v.iov_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001556 off += v.iov_len;
1557 })
1558 )
Willem de Bruijn46a831d2021-02-03 14:29:52 -05001559 csstate->csum = sum;
1560 csstate->off = off;
Al Viroa604ec72014-11-24 01:08:00 -05001561 return bytes;
1562}
1563EXPORT_SYMBOL(csum_and_copy_to_iter);
1564
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001565size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1566 struct iov_iter *i)
1567{
Herbert Xu79990962020-06-12 16:57:37 +10001568#ifdef CONFIG_CRYPTO_HASH
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001569 struct ahash_request *hash = hashp;
1570 struct scatterlist sg;
1571 size_t copied;
1572
1573 copied = copy_to_iter(addr, bytes, i);
1574 sg_init_one(&sg, addr, copied);
1575 ahash_request_set_crypt(hash, &sg, NULL, copied);
1576 crypto_ahash_update(hash);
1577 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001578#else
1579 return 0;
1580#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001581}
1582EXPORT_SYMBOL(hash_and_copy_to_iter);
1583
Al Viro62a80672014-04-04 23:12:29 -04001584int iov_iter_npages(const struct iov_iter *i, int maxpages)
1585{
Al Viroe0f2dc42014-11-27 14:09:46 -05001586 size_t size = i->count;
1587 int npages = 0;
1588
1589 if (!size)
1590 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001591 if (unlikely(iov_iter_is_discard(i)))
1592 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001593
David Howells00e23702018-10-22 13:07:28 +01001594 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001595 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001596 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -04001597 size_t off;
Al Viro241699c2016-09-22 16:33:12 -04001598
1599 if (!sanity(i))
1600 return 0;
1601
David Howells8cefc102019-11-15 13:30:32 +00001602 data_start(i, &iter_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001603 /* some of this one + all after this one */
David Howells8cefc102019-11-15 13:30:32 +00001604 npages = pipe_space_for_user(iter_head, pipe->tail, pipe);
Al Viro241699c2016-09-22 16:33:12 -04001605 if (npages >= maxpages)
1606 return maxpages;
1607 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001608 unsigned long p = (unsigned long)v.iov_base;
1609 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1610 - p / PAGE_SIZE;
1611 if (npages >= maxpages)
1612 return maxpages;
1613 0;}),({
1614 npages++;
1615 if (npages >= maxpages)
1616 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001617 }),({
1618 unsigned long p = (unsigned long)v.iov_base;
1619 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1620 - p / PAGE_SIZE;
1621 if (npages >= maxpages)
1622 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001623 })
1624 )
1625 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001626}
Al Virof67da302014-03-19 01:16:16 -04001627EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001628
1629const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1630{
1631 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001632 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001633 WARN_ON(1);
1634 return NULL;
1635 }
David Howells9ea9ce02018-10-20 00:57:56 +01001636 if (unlikely(iov_iter_is_discard(new)))
1637 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001638 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001639 return new->bvec = kmemdup(new->bvec,
1640 new->nr_segs * sizeof(struct bio_vec),
1641 flags);
1642 else
1643 /* iovec and kvec have identical layout */
1644 return new->iov = kmemdup(new->iov,
1645 new->nr_segs * sizeof(struct iovec),
1646 flags);
1647}
1648EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001649
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001650static int copy_compat_iovec_from_user(struct iovec *iov,
1651 const struct iovec __user *uvec, unsigned long nr_segs)
1652{
1653 const struct compat_iovec __user *uiov =
1654 (const struct compat_iovec __user *)uvec;
1655 int ret = -EFAULT, i;
1656
Christoph Hellwig37d4f782021-01-11 18:19:26 +01001657 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001658 return -EFAULT;
1659
1660 for (i = 0; i < nr_segs; i++) {
1661 compat_uptr_t buf;
1662 compat_ssize_t len;
1663
1664 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1665 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1666
1667 /* check for compat_size_t not fitting in compat_ssize_t .. */
1668 if (len < 0) {
1669 ret = -EINVAL;
1670 goto uaccess_end;
1671 }
1672 iov[i].iov_base = compat_ptr(buf);
1673 iov[i].iov_len = len;
1674 }
1675
1676 ret = 0;
1677uaccess_end:
1678 user_access_end();
1679 return ret;
1680}
1681
1682static int copy_iovec_from_user(struct iovec *iov,
1683 const struct iovec __user *uvec, unsigned long nr_segs)
David Laightfb041b52020-09-25 06:51:39 +02001684{
1685 unsigned long seg;
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001686
1687 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1688 return -EFAULT;
1689 for (seg = 0; seg < nr_segs; seg++) {
1690 if ((ssize_t)iov[seg].iov_len < 0)
1691 return -EINVAL;
1692 }
1693
1694 return 0;
1695}
1696
1697struct iovec *iovec_from_user(const struct iovec __user *uvec,
1698 unsigned long nr_segs, unsigned long fast_segs,
1699 struct iovec *fast_iov, bool compat)
1700{
1701 struct iovec *iov = fast_iov;
1702 int ret;
David Laightfb041b52020-09-25 06:51:39 +02001703
1704 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001705 * SuS says "The readv() function *may* fail if the iovcnt argument was
1706 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
David Laightfb041b52020-09-25 06:51:39 +02001707 * traditionally returned zero for zero segments, so...
1708 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001709 if (nr_segs == 0)
1710 return iov;
1711 if (nr_segs > UIO_MAXIOV)
1712 return ERR_PTR(-EINVAL);
David Laightfb041b52020-09-25 06:51:39 +02001713 if (nr_segs > fast_segs) {
1714 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001715 if (!iov)
1716 return ERR_PTR(-ENOMEM);
David Laightfb041b52020-09-25 06:51:39 +02001717 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001718
1719 if (compat)
1720 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1721 else
1722 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1723 if (ret) {
1724 if (iov != fast_iov)
1725 kfree(iov);
1726 return ERR_PTR(ret);
1727 }
1728
1729 return iov;
1730}
1731
1732ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1733 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1734 struct iov_iter *i, bool compat)
1735{
1736 ssize_t total_len = 0;
1737 unsigned long seg;
1738 struct iovec *iov;
1739
1740 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1741 if (IS_ERR(iov)) {
1742 *iovp = NULL;
1743 return PTR_ERR(iov);
David Laightfb041b52020-09-25 06:51:39 +02001744 }
1745
1746 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001747 * According to the Single Unix Specification we should return EINVAL if
1748 * an element length is < 0 when cast to ssize_t or if the total length
1749 * would overflow the ssize_t return value of the system call.
David Laightfb041b52020-09-25 06:51:39 +02001750 *
1751 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1752 * overflow case.
1753 */
David Laightfb041b52020-09-25 06:51:39 +02001754 for (seg = 0; seg < nr_segs; seg++) {
David Laightfb041b52020-09-25 06:51:39 +02001755 ssize_t len = (ssize_t)iov[seg].iov_len;
1756
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001757 if (!access_ok(iov[seg].iov_base, len)) {
1758 if (iov != *iovp)
1759 kfree(iov);
1760 *iovp = NULL;
1761 return -EFAULT;
David Laightfb041b52020-09-25 06:51:39 +02001762 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001763
1764 if (len > MAX_RW_COUNT - total_len) {
1765 len = MAX_RW_COUNT - total_len;
David Laightfb041b52020-09-25 06:51:39 +02001766 iov[seg].iov_len = len;
1767 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001768 total_len += len;
David Laightfb041b52020-09-25 06:51:39 +02001769 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001770
1771 iov_iter_init(i, type, iov, nr_segs, total_len);
1772 if (iov == *iovp)
1773 *iovp = NULL;
1774 else
1775 *iovp = iov;
1776 return total_len;
David Laightfb041b52020-09-25 06:51:39 +02001777}
1778
Vegard Nossumffecee42016-10-08 11:18:07 +02001779/**
1780 * import_iovec() - Copy an array of &struct iovec from userspace
1781 * into the kernel, check that it is valid, and initialize a new
1782 * &struct iov_iter iterator to access it.
1783 *
1784 * @type: One of %READ or %WRITE.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001785 * @uvec: Pointer to the userspace array.
Vegard Nossumffecee42016-10-08 11:18:07 +02001786 * @nr_segs: Number of elements in userspace array.
1787 * @fast_segs: Number of elements in @iov.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001788 * @iovp: (input and output parameter) Pointer to pointer to (usually small
Vegard Nossumffecee42016-10-08 11:18:07 +02001789 * on-stack) kernel array.
1790 * @i: Pointer to iterator that will be initialized on success.
1791 *
1792 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1793 * then this function places %NULL in *@iov on return. Otherwise, a new
1794 * array will be allocated and the result placed in *@iov. This means that
1795 * the caller may call kfree() on *@iov regardless of whether the small
1796 * on-stack array was used or not (and regardless of whether this function
1797 * returns an error or not).
1798 *
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001799 * Return: Negative error code on error, bytes imported on success
Vegard Nossumffecee42016-10-08 11:18:07 +02001800 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001801ssize_t import_iovec(int type, const struct iovec __user *uvec,
Al Virobc917be2015-03-21 17:45:43 -04001802 unsigned nr_segs, unsigned fast_segs,
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001803 struct iovec **iovp, struct iov_iter *i)
Al Virobc917be2015-03-21 17:45:43 -04001804{
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02001805 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1806 in_compat_syscall());
Al Virobc917be2015-03-21 17:45:43 -04001807}
1808EXPORT_SYMBOL(import_iovec);
1809
Al Virobc917be2015-03-21 17:45:43 -04001810int import_single_range(int rw, void __user *buf, size_t len,
1811 struct iovec *iov, struct iov_iter *i)
1812{
1813 if (len > MAX_RW_COUNT)
1814 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001815 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04001816 return -EFAULT;
1817
1818 iov->iov_base = buf;
1819 iov->iov_len = len;
1820 iov_iter_init(i, rw, iov, 1, len);
1821 return 0;
1822}
Al Viroe1267582015-12-06 20:38:56 -05001823EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05001824
Jens Axboee86db872021-09-10 11:18:36 -06001825/**
1826 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
1827 * iov_iter_save_state() was called.
1828 *
1829 * @i: &struct iov_iter to restore
1830 * @state: state to restore from
1831 *
1832 * Used after iov_iter_save_state() to bring restore @i, if operations may
1833 * have advanced it.
1834 *
1835 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
1836 */
1837void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
Al Viro09cf6982017-02-18 01:44:03 -05001838{
Jens Axboee86db872021-09-10 11:18:36 -06001839 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
1840 !iov_iter_is_kvec(i))
1841 return;
1842 i->iov_offset = state->iov_offset;
1843 i->count = state->count;
1844 /*
1845 * For the *vec iters, nr_segs + iov is constant - if we increment
1846 * the vec, then we also decrement the nr_segs count. Hence we don't
1847 * need to track both of these, just one is enough and we can deduct
1848 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
1849 * size, so we can just increment the iov pointer as they are unionzed.
1850 * ITER_BVEC _may_ be the same size on some archs, but on others it is
1851 * not. Be safe and handle it separately.
1852 */
1853 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
1854 if (iov_iter_is_bvec(i))
1855 i->bvec -= state->nr_segs - i->nr_segs;
1856 else
1857 i->iov -= state->nr_segs - i->nr_segs;
1858 i->nr_segs = state->nr_segs;
Al Viro09cf6982017-02-18 01:44:03 -05001859}