blob: d13304a034f5ee9d91ab0b32501b50980001f990 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Herbert Xu79990962020-06-12 16:57:37 +10002#include <crypto/hash.h>
Al Viro4f18cd32014-02-05 19:11:33 -05003#include <linux/export.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -06004#include <linux/bvec.h>
Al Viro4f18cd32014-02-05 19:11:33 -05005#include <linux/uio.h>
6#include <linux/pagemap.h>
Al Viro91f79c42014-03-21 04:58:33 -04007#include <linux/slab.h>
8#include <linux/vmalloc.h>
Al Viro241699c2016-09-22 16:33:12 -04009#include <linux/splice.h>
Al Viroa604ec72014-11-24 01:08:00 -050010#include <net/checksum.h>
Sagi Grimbergd05f4432018-12-03 17:52:09 -080011#include <linux/scatterlist.h>
Marco Elverd0ef4c32020-01-21 17:05:11 +010012#include <linux/instrumented.h>
Al Viro4f18cd32014-02-05 19:11:33 -050013
Al Viro241699c2016-09-22 16:33:12 -040014#define PIPE_PARANOIA /* for now */
15
Al Viro04a31162014-11-27 13:51:41 -050016#define iterate_iovec(i, n, __v, __p, skip, STEP) { \
17 size_t left; \
18 size_t wanted = n; \
19 __p = i->iov; \
20 __v.iov_len = min(n, __p->iov_len - skip); \
21 if (likely(__v.iov_len)) { \
22 __v.iov_base = __p->iov_base + skip; \
23 left = (STEP); \
24 __v.iov_len -= left; \
25 skip += __v.iov_len; \
26 n -= __v.iov_len; \
27 } else { \
28 left = 0; \
29 } \
30 while (unlikely(!left && n)) { \
31 __p++; \
32 __v.iov_len = min(n, __p->iov_len); \
33 if (unlikely(!__v.iov_len)) \
34 continue; \
35 __v.iov_base = __p->iov_base; \
36 left = (STEP); \
37 __v.iov_len -= left; \
38 skip = __v.iov_len; \
39 n -= __v.iov_len; \
40 } \
41 n = wanted - n; \
42}
43
Al Viroa2804552014-11-27 14:48:42 -050044#define iterate_kvec(i, n, __v, __p, skip, STEP) { \
45 size_t wanted = n; \
46 __p = i->kvec; \
47 __v.iov_len = min(n, __p->iov_len - skip); \
48 if (likely(__v.iov_len)) { \
49 __v.iov_base = __p->iov_base + skip; \
50 (void)(STEP); \
51 skip += __v.iov_len; \
52 n -= __v.iov_len; \
53 } \
54 while (unlikely(n)) { \
55 __p++; \
56 __v.iov_len = min(n, __p->iov_len); \
57 if (unlikely(!__v.iov_len)) \
58 continue; \
59 __v.iov_base = __p->iov_base; \
60 (void)(STEP); \
61 skip = __v.iov_len; \
62 n -= __v.iov_len; \
63 } \
64 n = wanted; \
65}
66
Ming Lei1bdc76a2016-05-30 21:34:32 +080067#define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
68 struct bvec_iter __start; \
69 __start.bi_size = n; \
70 __start.bi_bvec_done = skip; \
71 __start.bi_idx = 0; \
72 for_each_bvec(__v, i->bvec, __bi, __start) { \
73 if (!__v.bv_len) \
Al Viro04a31162014-11-27 13:51:41 -050074 continue; \
Al Viro04a31162014-11-27 13:51:41 -050075 (void)(STEP); \
Al Viro04a31162014-11-27 13:51:41 -050076 } \
Al Viro04a31162014-11-27 13:51:41 -050077}
78
Al Viroa2804552014-11-27 14:48:42 -050079#define iterate_all_kinds(i, n, v, I, B, K) { \
Al Viro33844e62016-12-21 21:55:02 -050080 if (likely(n)) { \
81 size_t skip = i->iov_offset; \
82 if (unlikely(i->type & ITER_BVEC)) { \
83 struct bio_vec v; \
84 struct bvec_iter __bi; \
85 iterate_bvec(i, n, v, __bi, skip, (B)) \
86 } else if (unlikely(i->type & ITER_KVEC)) { \
87 const struct kvec *kvec; \
88 struct kvec v; \
89 iterate_kvec(i, n, v, kvec, skip, (K)) \
David Howells9ea9ce02018-10-20 00:57:56 +010090 } else if (unlikely(i->type & ITER_DISCARD)) { \
Al Viro33844e62016-12-21 21:55:02 -050091 } else { \
92 const struct iovec *iov; \
93 struct iovec v; \
94 iterate_iovec(i, n, v, iov, skip, (I)) \
95 } \
Al Viro04a31162014-11-27 13:51:41 -050096 } \
97}
98
Al Viroa2804552014-11-27 14:48:42 -050099#define iterate_and_advance(i, n, v, I, B, K) { \
Al Virodd254f52016-05-09 11:54:48 -0400100 if (unlikely(i->count < n)) \
101 n = i->count; \
Al Viro19f18452016-05-25 17:36:19 -0400102 if (i->count) { \
Al Virodd254f52016-05-09 11:54:48 -0400103 size_t skip = i->iov_offset; \
104 if (unlikely(i->type & ITER_BVEC)) { \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800105 const struct bio_vec *bvec = i->bvec; \
Al Virodd254f52016-05-09 11:54:48 -0400106 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +0800107 struct bvec_iter __bi; \
108 iterate_bvec(i, n, v, __bi, skip, (B)) \
109 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
110 i->nr_segs -= i->bvec - bvec; \
111 skip = __bi.bi_bvec_done; \
Al Virodd254f52016-05-09 11:54:48 -0400112 } else if (unlikely(i->type & ITER_KVEC)) { \
113 const struct kvec *kvec; \
114 struct kvec v; \
115 iterate_kvec(i, n, v, kvec, skip, (K)) \
116 if (skip == kvec->iov_len) { \
117 kvec++; \
118 skip = 0; \
119 } \
120 i->nr_segs -= kvec - i->kvec; \
121 i->kvec = kvec; \
David Howells9ea9ce02018-10-20 00:57:56 +0100122 } else if (unlikely(i->type & ITER_DISCARD)) { \
123 skip += n; \
Al Virodd254f52016-05-09 11:54:48 -0400124 } else { \
125 const struct iovec *iov; \
126 struct iovec v; \
127 iterate_iovec(i, n, v, iov, skip, (I)) \
128 if (skip == iov->iov_len) { \
129 iov++; \
130 skip = 0; \
131 } \
132 i->nr_segs -= iov - i->iov; \
133 i->iov = iov; \
Al Viro7ce2a912014-11-27 13:59:45 -0500134 } \
Al Virodd254f52016-05-09 11:54:48 -0400135 i->count -= n; \
136 i->iov_offset = skip; \
Al Viro7ce2a912014-11-27 13:59:45 -0500137 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500138}
139
Al Viro09fc68dc2017-06-29 22:25:14 -0400140static int copyout(void __user *to, const void *from, size_t n)
141{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800142 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100143 instrument_copy_to_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400144 n = raw_copy_to_user(to, from, n);
145 }
146 return n;
147}
148
149static int copyin(void *to, const void __user *from, size_t n)
150{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800151 if (access_ok(from, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100152 instrument_copy_from_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400153 n = raw_copy_from_user(to, from, n);
154 }
155 return n;
156}
157
Al Viro62a80672014-04-04 23:12:29 -0400158static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500159 struct iov_iter *i)
160{
161 size_t skip, copy, left, wanted;
162 const struct iovec *iov;
163 char __user *buf;
164 void *kaddr, *from;
165
166 if (unlikely(bytes > i->count))
167 bytes = i->count;
168
169 if (unlikely(!bytes))
170 return 0;
171
Al Viro09fc68dc2017-06-29 22:25:14 -0400172 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500173 wanted = bytes;
174 iov = i->iov;
175 skip = i->iov_offset;
176 buf = iov->iov_base + skip;
177 copy = min(bytes, iov->iov_len - skip);
178
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700179 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500180 kaddr = kmap_atomic(page);
181 from = kaddr + offset;
182
183 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400184 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500185 copy -= left;
186 skip += copy;
187 from += copy;
188 bytes -= copy;
189
190 while (unlikely(!left && bytes)) {
191 iov++;
192 buf = iov->iov_base;
193 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400194 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500195 copy -= left;
196 skip = copy;
197 from += copy;
198 bytes -= copy;
199 }
200 if (likely(!bytes)) {
201 kunmap_atomic(kaddr);
202 goto done;
203 }
204 offset = from - kaddr;
205 buf += copy;
206 kunmap_atomic(kaddr);
207 copy = min(bytes, iov->iov_len - skip);
208 }
209 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700210
Al Viro4f18cd32014-02-05 19:11:33 -0500211 kaddr = kmap(page);
212 from = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400213 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500214 copy -= left;
215 skip += copy;
216 from += copy;
217 bytes -= copy;
218 while (unlikely(!left && bytes)) {
219 iov++;
220 buf = iov->iov_base;
221 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400222 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500223 copy -= left;
224 skip = copy;
225 from += copy;
226 bytes -= copy;
227 }
228 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700229
Al Viro4f18cd32014-02-05 19:11:33 -0500230done:
Al Viro81055e52014-04-04 19:23:46 -0400231 if (skip == iov->iov_len) {
232 iov++;
233 skip = 0;
234 }
Al Viro4f18cd32014-02-05 19:11:33 -0500235 i->count -= wanted - bytes;
236 i->nr_segs -= iov - i->iov;
237 i->iov = iov;
238 i->iov_offset = skip;
239 return wanted - bytes;
240}
Al Viro4f18cd32014-02-05 19:11:33 -0500241
Al Viro62a80672014-04-04 23:12:29 -0400242static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400243 struct iov_iter *i)
244{
245 size_t skip, copy, left, wanted;
246 const struct iovec *iov;
247 char __user *buf;
248 void *kaddr, *to;
249
250 if (unlikely(bytes > i->count))
251 bytes = i->count;
252
253 if (unlikely(!bytes))
254 return 0;
255
Al Viro09fc68dc2017-06-29 22:25:14 -0400256 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400257 wanted = bytes;
258 iov = i->iov;
259 skip = i->iov_offset;
260 buf = iov->iov_base + skip;
261 copy = min(bytes, iov->iov_len - skip);
262
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700263 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400264 kaddr = kmap_atomic(page);
265 to = kaddr + offset;
266
267 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400268 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400269 copy -= left;
270 skip += copy;
271 to += copy;
272 bytes -= copy;
273
274 while (unlikely(!left && bytes)) {
275 iov++;
276 buf = iov->iov_base;
277 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400278 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400279 copy -= left;
280 skip = copy;
281 to += copy;
282 bytes -= copy;
283 }
284 if (likely(!bytes)) {
285 kunmap_atomic(kaddr);
286 goto done;
287 }
288 offset = to - kaddr;
289 buf += copy;
290 kunmap_atomic(kaddr);
291 copy = min(bytes, iov->iov_len - skip);
292 }
293 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700294
Al Virof0d1bec2014-04-03 15:05:18 -0400295 kaddr = kmap(page);
296 to = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400297 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400298 copy -= left;
299 skip += copy;
300 to += copy;
301 bytes -= copy;
302 while (unlikely(!left && bytes)) {
303 iov++;
304 buf = iov->iov_base;
305 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400306 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400307 copy -= left;
308 skip = copy;
309 to += copy;
310 bytes -= copy;
311 }
312 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700313
Al Virof0d1bec2014-04-03 15:05:18 -0400314done:
Al Viro81055e52014-04-04 19:23:46 -0400315 if (skip == iov->iov_len) {
316 iov++;
317 skip = 0;
318 }
Al Virof0d1bec2014-04-03 15:05:18 -0400319 i->count -= wanted - bytes;
320 i->nr_segs -= iov - i->iov;
321 i->iov = iov;
322 i->iov_offset = skip;
323 return wanted - bytes;
324}
Al Virof0d1bec2014-04-03 15:05:18 -0400325
Al Viro241699c2016-09-22 16:33:12 -0400326#ifdef PIPE_PARANOIA
327static bool sanity(const struct iov_iter *i)
328{
329 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000330 unsigned int p_head = pipe->head;
331 unsigned int p_tail = pipe->tail;
332 unsigned int p_mask = pipe->ring_size - 1;
333 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
334 unsigned int i_head = i->head;
335 unsigned int idx;
336
Al Viro241699c2016-09-22 16:33:12 -0400337 if (i->iov_offset) {
338 struct pipe_buffer *p;
David Howells8cefc102019-11-15 13:30:32 +0000339 if (unlikely(p_occupancy == 0))
Al Viro241699c2016-09-22 16:33:12 -0400340 goto Bad; // pipe must be non-empty
David Howells8cefc102019-11-15 13:30:32 +0000341 if (unlikely(i_head != p_head - 1))
Al Viro241699c2016-09-22 16:33:12 -0400342 goto Bad; // must be at the last buffer...
343
David Howells8cefc102019-11-15 13:30:32 +0000344 p = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400345 if (unlikely(p->offset + p->len != i->iov_offset))
346 goto Bad; // ... at the end of segment
347 } else {
David Howells8cefc102019-11-15 13:30:32 +0000348 if (i_head != p_head)
Al Viro241699c2016-09-22 16:33:12 -0400349 goto Bad; // must be right after the last buffer
350 }
351 return true;
352Bad:
David Howells8cefc102019-11-15 13:30:32 +0000353 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
354 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
355 p_head, p_tail, pipe->ring_size);
356 for (idx = 0; idx < pipe->ring_size; idx++)
Al Viro241699c2016-09-22 16:33:12 -0400357 printk(KERN_ERR "[%p %p %d %d]\n",
358 pipe->bufs[idx].ops,
359 pipe->bufs[idx].page,
360 pipe->bufs[idx].offset,
361 pipe->bufs[idx].len);
362 WARN_ON(1);
363 return false;
364}
365#else
366#define sanity(i) true
367#endif
368
Al Viro241699c2016-09-22 16:33:12 -0400369static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
370 struct iov_iter *i)
371{
372 struct pipe_inode_info *pipe = i->pipe;
373 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +0000374 unsigned int p_tail = pipe->tail;
375 unsigned int p_mask = pipe->ring_size - 1;
376 unsigned int i_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400377 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400378
379 if (unlikely(bytes > i->count))
380 bytes = i->count;
381
382 if (unlikely(!bytes))
383 return 0;
384
385 if (!sanity(i))
386 return 0;
387
388 off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000389 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400390 if (off) {
391 if (offset == off && buf->page == page) {
392 /* merge with the last one */
393 buf->len += bytes;
394 i->iov_offset += bytes;
395 goto out;
396 }
David Howells8cefc102019-11-15 13:30:32 +0000397 i_head++;
398 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400399 }
David Howells6718b6f2019-10-16 16:47:32 +0100400 if (pipe_full(i_head, p_tail, pipe->max_usage))
Al Viro241699c2016-09-22 16:33:12 -0400401 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000402
Al Viro241699c2016-09-22 16:33:12 -0400403 buf->ops = &page_cache_pipe_buf_ops;
David Howells8cefc102019-11-15 13:30:32 +0000404 get_page(page);
405 buf->page = page;
Al Viro241699c2016-09-22 16:33:12 -0400406 buf->offset = offset;
407 buf->len = bytes;
David Howells8cefc102019-11-15 13:30:32 +0000408
409 pipe->head = i_head + 1;
Al Viro241699c2016-09-22 16:33:12 -0400410 i->iov_offset = offset + bytes;
David Howells8cefc102019-11-15 13:30:32 +0000411 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400412out:
413 i->count -= bytes;
414 return bytes;
415}
416
Al Viro4f18cd32014-02-05 19:11:33 -0500417/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400418 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
419 * bytes. For each iovec, fault in each page that constitutes the iovec.
420 *
421 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
422 * because it is an invalid address).
423 */
Al Virod4690f12016-09-16 00:11:45 +0100424int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400425{
426 size_t skip = i->iov_offset;
427 const struct iovec *iov;
428 int err;
429 struct iovec v;
430
431 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
432 iterate_iovec(i, bytes, v, iov, skip, ({
Al Viro4bce9f6e2016-09-17 18:02:44 -0400433 err = fault_in_pages_readable(v.iov_base, v.iov_len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400434 if (unlikely(err))
435 return err;
436 0;}))
437 }
438 return 0;
439}
Al Virod4690f12016-09-16 00:11:45 +0100440EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400441
David Howellsaa563d72018-10-20 00:57:56 +0100442void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500443 const struct iovec *iov, unsigned long nr_segs,
444 size_t count)
445{
David Howellsaa563d72018-10-20 00:57:56 +0100446 WARN_ON(direction & ~(READ | WRITE));
447 direction &= READ | WRITE;
448
Al Viro71d8e532014-03-05 19:28:09 -0500449 /* It will get better. Eventually... */
Al Virodb68ce12017-03-20 21:08:07 -0400450 if (uaccess_kernel()) {
David Howellsaa563d72018-10-20 00:57:56 +0100451 i->type = ITER_KVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500452 i->kvec = (struct kvec *)iov;
453 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100454 i->type = ITER_IOVEC | direction;
Al Viroa2804552014-11-27 14:48:42 -0500455 i->iov = iov;
456 }
Al Viro71d8e532014-03-05 19:28:09 -0500457 i->nr_segs = nr_segs;
458 i->iov_offset = 0;
459 i->count = count;
460}
461EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400462
Al Viro62a80672014-04-04 23:12:29 -0400463static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
464{
465 char *from = kmap_atomic(page);
466 memcpy(to, from + offset, len);
467 kunmap_atomic(from);
468}
469
Al Viro36f7a8a2015-12-06 16:49:22 -0500470static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
Al Viro62a80672014-04-04 23:12:29 -0400471{
472 char *to = kmap_atomic(page);
473 memcpy(to + offset, from, len);
474 kunmap_atomic(to);
475}
476
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400477static void memzero_page(struct page *page, size_t offset, size_t len)
478{
479 char *addr = kmap_atomic(page);
480 memset(addr + offset, 0, len);
481 kunmap_atomic(addr);
482}
483
Al Viro241699c2016-09-22 16:33:12 -0400484static inline bool allocated(struct pipe_buffer *buf)
485{
486 return buf->ops == &default_pipe_buf_ops;
487}
488
David Howells8cefc102019-11-15 13:30:32 +0000489static inline void data_start(const struct iov_iter *i,
490 unsigned int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400491{
David Howells8cefc102019-11-15 13:30:32 +0000492 unsigned int p_mask = i->pipe->ring_size - 1;
493 unsigned int iter_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400494 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000495
496 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
497 off == PAGE_SIZE)) {
498 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400499 off = 0;
500 }
David Howells8cefc102019-11-15 13:30:32 +0000501 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400502 *offp = off;
503}
504
505static size_t push_pipe(struct iov_iter *i, size_t size,
David Howells8cefc102019-11-15 13:30:32 +0000506 int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400507{
508 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000509 unsigned int p_tail = pipe->tail;
510 unsigned int p_mask = pipe->ring_size - 1;
511 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400512 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400513 ssize_t left;
514
515 if (unlikely(size > i->count))
516 size = i->count;
517 if (unlikely(!size))
518 return 0;
519
520 left = size;
David Howells8cefc102019-11-15 13:30:32 +0000521 data_start(i, &iter_head, &off);
522 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400523 *offp = off;
524 if (off) {
525 left -= PAGE_SIZE - off;
526 if (left <= 0) {
David Howells8cefc102019-11-15 13:30:32 +0000527 pipe->bufs[iter_head & p_mask].len += size;
Al Viro241699c2016-09-22 16:33:12 -0400528 return size;
529 }
David Howells8cefc102019-11-15 13:30:32 +0000530 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
531 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400532 }
David Howells6718b6f2019-10-16 16:47:32 +0100533 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000534 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400535 struct page *page = alloc_page(GFP_USER);
536 if (!page)
537 break;
David Howells8cefc102019-11-15 13:30:32 +0000538
539 buf->ops = &default_pipe_buf_ops;
540 buf->page = page;
541 buf->offset = 0;
542 buf->len = min_t(ssize_t, left, PAGE_SIZE);
543 left -= buf->len;
544 iter_head++;
545 pipe->head = iter_head;
546
547 if (left == 0)
Al Viro241699c2016-09-22 16:33:12 -0400548 return size;
Al Viro241699c2016-09-22 16:33:12 -0400549 }
550 return size - left;
551}
552
553static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
554 struct iov_iter *i)
555{
556 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000557 unsigned int p_mask = pipe->ring_size - 1;
558 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400559 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400560
561 if (!sanity(i))
562 return 0;
563
David Howells8cefc102019-11-15 13:30:32 +0000564 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400565 if (unlikely(!n))
566 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000567 do {
Al Viro241699c2016-09-22 16:33:12 -0400568 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000569 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
570 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400571 i->iov_offset = off + chunk;
572 n -= chunk;
573 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000574 off = 0;
575 i_head++;
576 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400577 i->count -= bytes;
578 return bytes;
579}
580
Al Virof9152892018-11-27 22:32:59 -0500581static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
582 __wsum sum, size_t off)
583{
584 __wsum next = csum_partial_copy_nocheck(from, to, len, 0);
585 return csum_block_add(sum, next, off);
586}
587
Al Viro78e1f382018-11-25 16:24:16 -0500588static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
589 __wsum *csum, struct iov_iter *i)
590{
591 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000592 unsigned int p_mask = pipe->ring_size - 1;
593 unsigned int i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500594 size_t n, r;
595 size_t off = 0;
Al Virof9152892018-11-27 22:32:59 -0500596 __wsum sum = *csum;
Al Viro78e1f382018-11-25 16:24:16 -0500597
598 if (!sanity(i))
599 return 0;
600
David Howells8cefc102019-11-15 13:30:32 +0000601 bytes = n = push_pipe(i, bytes, &i_head, &r);
Al Viro78e1f382018-11-25 16:24:16 -0500602 if (unlikely(!n))
603 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000604 do {
Al Viro78e1f382018-11-25 16:24:16 -0500605 size_t chunk = min_t(size_t, n, PAGE_SIZE - r);
David Howells8cefc102019-11-15 13:30:32 +0000606 char *p = kmap_atomic(pipe->bufs[i_head & p_mask].page);
Al Virof9152892018-11-27 22:32:59 -0500607 sum = csum_and_memcpy(p + r, addr, chunk, sum, off);
Al Viro78e1f382018-11-25 16:24:16 -0500608 kunmap_atomic(p);
David Howells8cefc102019-11-15 13:30:32 +0000609 i->head = i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500610 i->iov_offset = r + chunk;
611 n -= chunk;
612 off += chunk;
613 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000614 r = 0;
615 i_head++;
616 } while (n);
Al Viro78e1f382018-11-25 16:24:16 -0500617 i->count -= bytes;
618 *csum = sum;
619 return bytes;
620}
621
Al Viroaa28de22017-06-29 21:45:10 -0400622size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400623{
Al Viro36f7a8a2015-12-06 16:49:22 -0500624 const char *from = addr;
David Howells00e23702018-10-22 13:07:28 +0100625 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400626 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400627 if (iter_is_iovec(i))
628 might_fault();
Al Viro3d4d3e42014-11-27 14:28:06 -0500629 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400630 copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len),
Al Viro3d4d3e42014-11-27 14:28:06 -0500631 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500632 (from += v.bv_len) - v.bv_len, v.bv_len),
633 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500634 )
Al Viro62a80672014-04-04 23:12:29 -0400635
Al Viro3d4d3e42014-11-27 14:28:06 -0500636 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400637}
Al Viroaa28de22017-06-29 21:45:10 -0400638EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400639
Dan Williamsec6347b2020-10-05 20:40:16 -0700640#ifdef CONFIG_ARCH_HAS_COPY_MC
641static int copyout_mc(void __user *to, const void *from, size_t n)
Dan Williams87803562018-05-03 17:06:31 -0700642{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800643 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100644 instrument_copy_to_user(to, from, n);
Dan Williamsec6347b2020-10-05 20:40:16 -0700645 n = copy_mc_to_user((__force void *) to, from, n);
Dan Williams87803562018-05-03 17:06:31 -0700646 }
647 return n;
648}
649
Dan Williamsec6347b2020-10-05 20:40:16 -0700650static unsigned long copy_mc_to_page(struct page *page, size_t offset,
Dan Williams87803562018-05-03 17:06:31 -0700651 const char *from, size_t len)
652{
653 unsigned long ret;
654 char *to;
655
656 to = kmap_atomic(page);
Dan Williamsec6347b2020-10-05 20:40:16 -0700657 ret = copy_mc_to_kernel(to + offset, from, len);
Dan Williams87803562018-05-03 17:06:31 -0700658 kunmap_atomic(to);
659
660 return ret;
661}
662
Dan Williamsec6347b2020-10-05 20:40:16 -0700663static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
Dan Williamsca146f62018-07-08 13:46:12 -0700664 struct iov_iter *i)
665{
666 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000667 unsigned int p_mask = pipe->ring_size - 1;
668 unsigned int i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700669 size_t n, off, xfer = 0;
Dan Williamsca146f62018-07-08 13:46:12 -0700670
671 if (!sanity(i))
672 return 0;
673
David Howells8cefc102019-11-15 13:30:32 +0000674 bytes = n = push_pipe(i, bytes, &i_head, &off);
Dan Williamsca146f62018-07-08 13:46:12 -0700675 if (unlikely(!n))
676 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000677 do {
Dan Williamsca146f62018-07-08 13:46:12 -0700678 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
679 unsigned long rem;
680
Dan Williamsec6347b2020-10-05 20:40:16 -0700681 rem = copy_mc_to_page(pipe->bufs[i_head & p_mask].page,
David Howells8cefc102019-11-15 13:30:32 +0000682 off, addr, chunk);
683 i->head = i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700684 i->iov_offset = off + chunk - rem;
685 xfer += chunk - rem;
686 if (rem)
687 break;
688 n -= chunk;
689 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000690 off = 0;
691 i_head++;
692 } while (n);
Dan Williamsca146f62018-07-08 13:46:12 -0700693 i->count -= xfer;
694 return xfer;
695}
696
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700697/**
Dan Williamsec6347b2020-10-05 20:40:16 -0700698 * _copy_mc_to_iter - copy to iter with source memory error exception handling
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700699 * @addr: source kernel address
700 * @bytes: total transfer length
701 * @iter: destination iterator
702 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700703 * The pmem driver deploys this for the dax operation
704 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
705 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
706 * successfully copied.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700707 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700708 * The main differences between this and typical _copy_to_iter().
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700709 *
710 * * Typical tail/residue handling after a fault retries the copy
711 * byte-by-byte until the fault happens again. Re-triggering machine
712 * checks is potentially fatal so the implementation uses source
713 * alignment and poison alignment assumptions to avoid re-triggering
714 * hardware exceptions.
715 *
716 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
717 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
718 * a short copy.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700719 */
Dan Williamsec6347b2020-10-05 20:40:16 -0700720size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Dan Williams87803562018-05-03 17:06:31 -0700721{
722 const char *from = addr;
723 unsigned long rem, curr_addr, s_addr = (unsigned long) addr;
724
David Howells00e23702018-10-22 13:07:28 +0100725 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsec6347b2020-10-05 20:40:16 -0700726 return copy_mc_pipe_to_iter(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700727 if (iter_is_iovec(i))
728 might_fault();
729 iterate_and_advance(i, bytes, v,
Dan Williamsec6347b2020-10-05 20:40:16 -0700730 copyout_mc(v.iov_base, (from += v.iov_len) - v.iov_len,
731 v.iov_len),
Dan Williams87803562018-05-03 17:06:31 -0700732 ({
Dan Williamsec6347b2020-10-05 20:40:16 -0700733 rem = copy_mc_to_page(v.bv_page, v.bv_offset,
734 (from += v.bv_len) - v.bv_len, v.bv_len);
Dan Williams87803562018-05-03 17:06:31 -0700735 if (rem) {
736 curr_addr = (unsigned long) from;
737 bytes = curr_addr - s_addr - rem;
738 return bytes;
739 }
740 }),
741 ({
Dan Williamsec6347b2020-10-05 20:40:16 -0700742 rem = copy_mc_to_kernel(v.iov_base, (from += v.iov_len)
743 - v.iov_len, v.iov_len);
Dan Williams87803562018-05-03 17:06:31 -0700744 if (rem) {
745 curr_addr = (unsigned long) from;
746 bytes = curr_addr - s_addr - rem;
747 return bytes;
748 }
749 })
750 )
751
752 return bytes;
753}
Dan Williamsec6347b2020-10-05 20:40:16 -0700754EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
755#endif /* CONFIG_ARCH_HAS_COPY_MC */
Dan Williams87803562018-05-03 17:06:31 -0700756
Al Viroaa28de22017-06-29 21:45:10 -0400757size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400758{
Al Viro0dbca9a2014-11-27 14:26:43 -0500759 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100760 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400761 WARN_ON(1);
762 return 0;
763 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400764 if (iter_is_iovec(i))
765 might_fault();
Al Viro0dbca9a2014-11-27 14:26:43 -0500766 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400767 copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro0dbca9a2014-11-27 14:26:43 -0500768 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500769 v.bv_offset, v.bv_len),
770 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500771 )
772
773 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400774}
Al Viroaa28de22017-06-29 21:45:10 -0400775EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400776
Al Viroaa28de22017-06-29 21:45:10 -0400777bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400778{
779 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100780 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400781 WARN_ON(1);
782 return false;
783 }
Al Viro33844e62016-12-21 21:55:02 -0500784 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400785 return false;
786
Al Viro09fc68dc2017-06-29 22:25:14 -0400787 if (iter_is_iovec(i))
788 might_fault();
Al Virocbbd26b2016-11-01 22:09:04 -0400789 iterate_all_kinds(i, bytes, v, ({
Al Viro09fc68dc2017-06-29 22:25:14 -0400790 if (copyin((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400791 v.iov_base, v.iov_len))
792 return false;
793 0;}),
794 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
795 v.bv_offset, v.bv_len),
796 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
797 )
798
799 iov_iter_advance(i, bytes);
800 return true;
801}
Al Viroaa28de22017-06-29 21:45:10 -0400802EXPORT_SYMBOL(_copy_from_iter_full);
Al Virocbbd26b2016-11-01 22:09:04 -0400803
Al Viroaa28de22017-06-29 21:45:10 -0400804size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500805{
806 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100807 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400808 WARN_ON(1);
809 return 0;
810 }
Al Viroaa583092014-11-27 20:27:08 -0500811 iterate_and_advance(i, bytes, v,
Al Viro3f763452017-03-25 18:47:28 -0400812 __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Viroaa583092014-11-27 20:27:08 -0500813 v.iov_base, v.iov_len),
814 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
815 v.bv_offset, v.bv_len),
816 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
817 )
818
819 return bytes;
820}
Al Viroaa28de22017-06-29 21:45:10 -0400821EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500822
Dan Williams0aed55a2017-05-29 12:22:50 -0700823#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700824/**
825 * _copy_from_iter_flushcache - write destination through cpu cache
826 * @addr: destination kernel address
827 * @bytes: total transfer length
828 * @iter: source iterator
829 *
830 * The pmem driver arranges for filesystem-dax to use this facility via
831 * dax_copy_from_iter() for ensuring that writes to persistent memory
832 * are flushed through the CPU cache. It is differentiated from
833 * _copy_from_iter_nocache() in that guarantees all data is flushed for
834 * all iterator types. The _copy_from_iter_nocache() only attempts to
835 * bypass the cache for the ITER_IOVEC case, and on some archs may use
836 * instructions that strand dirty-data in the cache.
837 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700838size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700839{
840 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100841 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700842 WARN_ON(1);
843 return 0;
844 }
845 iterate_and_advance(i, bytes, v,
846 __copy_from_user_flushcache((to += v.iov_len) - v.iov_len,
847 v.iov_base, v.iov_len),
848 memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page,
849 v.bv_offset, v.bv_len),
850 memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base,
851 v.iov_len)
852 )
853
854 return bytes;
855}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700856EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700857#endif
858
Al Viroaa28de22017-06-29 21:45:10 -0400859bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Virocbbd26b2016-11-01 22:09:04 -0400860{
861 char *to = addr;
David Howells00e23702018-10-22 13:07:28 +0100862 if (unlikely(iov_iter_is_pipe(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -0400863 WARN_ON(1);
864 return false;
865 }
Al Viro33844e62016-12-21 21:55:02 -0500866 if (unlikely(i->count < bytes))
Al Virocbbd26b2016-11-01 22:09:04 -0400867 return false;
868 iterate_all_kinds(i, bytes, v, ({
Al Viro3f763452017-03-25 18:47:28 -0400869 if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -0400870 v.iov_base, v.iov_len))
871 return false;
872 0;}),
873 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
874 v.bv_offset, v.bv_len),
875 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
876 )
877
878 iov_iter_advance(i, bytes);
879 return true;
880}
Al Viroaa28de22017-06-29 21:45:10 -0400881EXPORT_SYMBOL(_copy_from_iter_full_nocache);
Al Virocbbd26b2016-11-01 22:09:04 -0400882
Al Viro72e809e2017-06-29 21:52:57 -0400883static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
884{
Eric Dumazet6daef952019-02-26 10:42:39 -0800885 struct page *head;
886 size_t v = n + offset;
887
888 /*
889 * The general case needs to access the page order in order
890 * to compute the page size.
891 * However, we mostly deal with order-0 pages and thus can
892 * avoid a possible cache line miss for requests that fit all
893 * page orders.
894 */
895 if (n <= v && v <= PAGE_SIZE)
896 return true;
897
898 head = compound_head(page);
899 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700900
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700901 if (likely(n <= v && v <= (page_size(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400902 return true;
903 WARN_ON(1);
904 return false;
905}
Al Virod2715242014-11-27 14:22:37 -0500906
907size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
908 struct iov_iter *i)
909{
Al Viro72e809e2017-06-29 21:52:57 -0400910 if (unlikely(!page_copy_sane(page, offset, bytes)))
911 return 0;
Al Virod2715242014-11-27 14:22:37 -0500912 if (i->type & (ITER_BVEC|ITER_KVEC)) {
913 void *kaddr = kmap_atomic(page);
914 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
915 kunmap_atomic(kaddr);
916 return wanted;
David Howells9ea9ce02018-10-20 00:57:56 +0100917 } else if (unlikely(iov_iter_is_discard(i)))
918 return bytes;
919 else if (likely(!iov_iter_is_pipe(i)))
Al Virod2715242014-11-27 14:22:37 -0500920 return copy_page_to_iter_iovec(page, offset, bytes, i);
Al Viro241699c2016-09-22 16:33:12 -0400921 else
922 return copy_page_to_iter_pipe(page, offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500923}
924EXPORT_SYMBOL(copy_page_to_iter);
925
926size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
927 struct iov_iter *i)
928{
Al Viro72e809e2017-06-29 21:52:57 -0400929 if (unlikely(!page_copy_sane(page, offset, bytes)))
930 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +0100931 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400932 WARN_ON(1);
933 return 0;
934 }
Al Virod2715242014-11-27 14:22:37 -0500935 if (i->type & (ITER_BVEC|ITER_KVEC)) {
936 void *kaddr = kmap_atomic(page);
Al Viroaa28de22017-06-29 21:45:10 -0400937 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Virod2715242014-11-27 14:22:37 -0500938 kunmap_atomic(kaddr);
939 return wanted;
940 } else
941 return copy_page_from_iter_iovec(page, offset, bytes, i);
942}
943EXPORT_SYMBOL(copy_page_from_iter);
944
Al Viro241699c2016-09-22 16:33:12 -0400945static size_t pipe_zero(size_t bytes, struct iov_iter *i)
946{
947 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000948 unsigned int p_mask = pipe->ring_size - 1;
949 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400950 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400951
952 if (!sanity(i))
953 return 0;
954
David Howells8cefc102019-11-15 13:30:32 +0000955 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400956 if (unlikely(!n))
957 return 0;
958
David Howells8cefc102019-11-15 13:30:32 +0000959 do {
Al Viro241699c2016-09-22 16:33:12 -0400960 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000961 memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk);
962 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400963 i->iov_offset = off + chunk;
964 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000965 off = 0;
966 i_head++;
967 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400968 i->count -= bytes;
969 return bytes;
970}
971
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400972size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
973{
David Howells00e23702018-10-22 13:07:28 +0100974 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400975 return pipe_zero(bytes, i);
Al Viro8442fa42014-11-27 14:18:54 -0500976 iterate_and_advance(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -0400977 clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500978 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
979 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500980 )
981
982 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400983}
984EXPORT_SYMBOL(iov_iter_zero);
985
Al Viro62a80672014-04-04 23:12:29 -0400986size_t iov_iter_copy_from_user_atomic(struct page *page,
987 struct iov_iter *i, unsigned long offset, size_t bytes)
988{
Al Viro04a31162014-11-27 13:51:41 -0500989 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400990 if (unlikely(!page_copy_sane(page, offset, bytes))) {
991 kunmap_atomic(kaddr);
992 return 0;
993 }
David Howells9ea9ce02018-10-20 00:57:56 +0100994 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400995 kunmap_atomic(kaddr);
996 WARN_ON(1);
997 return 0;
998 }
Al Viro04a31162014-11-27 13:51:41 -0500999 iterate_all_kinds(i, bytes, v,
Al Viro09fc68dc2017-06-29 22:25:14 -04001000 copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len),
Al Viro04a31162014-11-27 13:51:41 -05001001 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -05001002 v.bv_offset, v.bv_len),
1003 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -05001004 )
1005 kunmap_atomic(kaddr);
1006 return bytes;
Al Viro62a80672014-04-04 23:12:29 -04001007}
1008EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
1009
Al Virob9dc6f62017-01-14 19:33:08 -05001010static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -04001011{
1012 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001013 unsigned int p_tail = pipe->tail;
1014 unsigned int p_head = pipe->head;
1015 unsigned int p_mask = pipe->ring_size - 1;
1016
1017 if (!pipe_empty(p_head, p_tail)) {
1018 struct pipe_buffer *buf;
1019 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001020 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +00001021
Al Virob9dc6f62017-01-14 19:33:08 -05001022 if (off) {
David Howells8cefc102019-11-15 13:30:32 +00001023 buf = &pipe->bufs[i_head & p_mask];
1024 buf->len = off - buf->offset;
1025 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001026 }
David Howells8cefc102019-11-15 13:30:32 +00001027 while (p_head != i_head) {
1028 p_head--;
1029 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
Al Viro241699c2016-09-22 16:33:12 -04001030 }
David Howells8cefc102019-11-15 13:30:32 +00001031
1032 pipe->head = p_head;
Al Viro241699c2016-09-22 16:33:12 -04001033 }
Al Virob9dc6f62017-01-14 19:33:08 -05001034}
1035
1036static void pipe_advance(struct iov_iter *i, size_t size)
1037{
1038 struct pipe_inode_info *pipe = i->pipe;
1039 if (unlikely(i->count < size))
1040 size = i->count;
1041 if (size) {
1042 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +00001043 unsigned int p_mask = pipe->ring_size - 1;
1044 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001045 size_t off = i->iov_offset, left = size;
David Howells8cefc102019-11-15 13:30:32 +00001046
Al Virob9dc6f62017-01-14 19:33:08 -05001047 if (off) /* make it relative to the beginning of buffer */
David Howells8cefc102019-11-15 13:30:32 +00001048 left += off - pipe->bufs[i_head & p_mask].offset;
Al Virob9dc6f62017-01-14 19:33:08 -05001049 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001050 buf = &pipe->bufs[i_head & p_mask];
Al Virob9dc6f62017-01-14 19:33:08 -05001051 if (left <= buf->len)
1052 break;
1053 left -= buf->len;
David Howells8cefc102019-11-15 13:30:32 +00001054 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001055 }
David Howells8cefc102019-11-15 13:30:32 +00001056 i->head = i_head;
Al Virob9dc6f62017-01-14 19:33:08 -05001057 i->iov_offset = buf->offset + left;
1058 }
1059 i->count -= size;
1060 /* ... and discard everything past that point */
1061 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001062}
1063
Al Viro62a80672014-04-04 23:12:29 -04001064void iov_iter_advance(struct iov_iter *i, size_t size)
1065{
David Howells00e23702018-10-22 13:07:28 +01001066 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001067 pipe_advance(i, size);
1068 return;
1069 }
David Howells9ea9ce02018-10-20 00:57:56 +01001070 if (unlikely(iov_iter_is_discard(i))) {
1071 i->count -= size;
1072 return;
1073 }
Al Viroa2804552014-11-27 14:48:42 -05001074 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -04001075}
1076EXPORT_SYMBOL(iov_iter_advance);
1077
Al Viro27c0e372017-02-17 18:42:24 -05001078void iov_iter_revert(struct iov_iter *i, size_t unroll)
1079{
1080 if (!unroll)
1081 return;
Al Viro5b47d592017-05-08 13:54:47 -04001082 if (WARN_ON(unroll > MAX_RW_COUNT))
1083 return;
Al Viro27c0e372017-02-17 18:42:24 -05001084 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001085 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001086 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001087 unsigned int p_mask = pipe->ring_size - 1;
1088 unsigned int i_head = i->head;
Al Viro27c0e372017-02-17 18:42:24 -05001089 size_t off = i->iov_offset;
1090 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001091 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1092 size_t n = off - b->offset;
Al Viro27c0e372017-02-17 18:42:24 -05001093 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001094 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001095 break;
1096 }
1097 unroll -= n;
David Howells8cefc102019-11-15 13:30:32 +00001098 if (!unroll && i_head == i->start_head) {
Al Viro27c0e372017-02-17 18:42:24 -05001099 off = 0;
1100 break;
1101 }
David Howells8cefc102019-11-15 13:30:32 +00001102 i_head--;
1103 b = &pipe->bufs[i_head & p_mask];
1104 off = b->offset + b->len;
Al Viro27c0e372017-02-17 18:42:24 -05001105 }
1106 i->iov_offset = off;
David Howells8cefc102019-11-15 13:30:32 +00001107 i->head = i_head;
Al Viro27c0e372017-02-17 18:42:24 -05001108 pipe_truncate(i);
1109 return;
1110 }
David Howells9ea9ce02018-10-20 00:57:56 +01001111 if (unlikely(iov_iter_is_discard(i)))
1112 return;
Al Viro27c0e372017-02-17 18:42:24 -05001113 if (unroll <= i->iov_offset) {
1114 i->iov_offset -= unroll;
1115 return;
1116 }
1117 unroll -= i->iov_offset;
David Howells00e23702018-10-22 13:07:28 +01001118 if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001119 const struct bio_vec *bvec = i->bvec;
1120 while (1) {
1121 size_t n = (--bvec)->bv_len;
1122 i->nr_segs++;
1123 if (unroll <= n) {
1124 i->bvec = bvec;
1125 i->iov_offset = n - unroll;
1126 return;
1127 }
1128 unroll -= n;
1129 }
1130 } else { /* same logics for iovec and kvec */
1131 const struct iovec *iov = i->iov;
1132 while (1) {
1133 size_t n = (--iov)->iov_len;
1134 i->nr_segs++;
1135 if (unroll <= n) {
1136 i->iov = iov;
1137 i->iov_offset = n - unroll;
1138 return;
1139 }
1140 unroll -= n;
1141 }
1142 }
1143}
1144EXPORT_SYMBOL(iov_iter_revert);
1145
Al Viro62a80672014-04-04 23:12:29 -04001146/*
1147 * Return the count of just the current iov_iter segment.
1148 */
1149size_t iov_iter_single_seg_count(const struct iov_iter *i)
1150{
David Howells00e23702018-10-22 13:07:28 +01001151 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001152 return i->count; // it is a silly place, anyway
Al Viro62a80672014-04-04 23:12:29 -04001153 if (i->nr_segs == 1)
1154 return i->count;
David Howells9ea9ce02018-10-20 00:57:56 +01001155 if (unlikely(iov_iter_is_discard(i)))
1156 return i->count;
David Howells00e23702018-10-22 13:07:28 +01001157 else if (iov_iter_is_bvec(i))
Al Viro62a80672014-04-04 23:12:29 -04001158 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +11001159 else
1160 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -04001161}
1162EXPORT_SYMBOL(iov_iter_single_seg_count);
1163
David Howellsaa563d72018-10-20 00:57:56 +01001164void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001165 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001166 size_t count)
1167{
David Howellsaa563d72018-10-20 00:57:56 +01001168 WARN_ON(direction & ~(READ | WRITE));
1169 i->type = ITER_KVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001170 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -05001171 i->nr_segs = nr_segs;
1172 i->iov_offset = 0;
1173 i->count = count;
1174}
1175EXPORT_SYMBOL(iov_iter_kvec);
1176
David Howellsaa563d72018-10-20 00:57:56 +01001177void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001178 const struct bio_vec *bvec, unsigned long nr_segs,
1179 size_t count)
1180{
David Howellsaa563d72018-10-20 00:57:56 +01001181 WARN_ON(direction & ~(READ | WRITE));
1182 i->type = ITER_BVEC | (direction & (READ | WRITE));
Al Viro05afcb72015-01-23 01:08:07 -05001183 i->bvec = bvec;
1184 i->nr_segs = nr_segs;
1185 i->iov_offset = 0;
1186 i->count = count;
1187}
1188EXPORT_SYMBOL(iov_iter_bvec);
1189
David Howellsaa563d72018-10-20 00:57:56 +01001190void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001191 struct pipe_inode_info *pipe,
1192 size_t count)
1193{
David Howellsaa563d72018-10-20 00:57:56 +01001194 BUG_ON(direction != READ);
David Howells8cefc102019-11-15 13:30:32 +00001195 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
David Howellsaa563d72018-10-20 00:57:56 +01001196 i->type = ITER_PIPE | READ;
Al Viro241699c2016-09-22 16:33:12 -04001197 i->pipe = pipe;
David Howells8cefc102019-11-15 13:30:32 +00001198 i->head = pipe->head;
Al Viro241699c2016-09-22 16:33:12 -04001199 i->iov_offset = 0;
1200 i->count = count;
David Howells8cefc102019-11-15 13:30:32 +00001201 i->start_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -04001202}
1203EXPORT_SYMBOL(iov_iter_pipe);
1204
David Howells9ea9ce02018-10-20 00:57:56 +01001205/**
1206 * iov_iter_discard - Initialise an I/O iterator that discards data
1207 * @i: The iterator to initialise.
1208 * @direction: The direction of the transfer.
1209 * @count: The size of the I/O buffer in bytes.
1210 *
1211 * Set up an I/O iterator that just discards everything that's written to it.
1212 * It's only available as a READ iterator.
1213 */
1214void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1215{
1216 BUG_ON(direction != READ);
1217 i->type = ITER_DISCARD | READ;
1218 i->count = count;
1219 i->iov_offset = 0;
1220}
1221EXPORT_SYMBOL(iov_iter_discard);
1222
Al Viro62a80672014-04-04 23:12:29 -04001223unsigned long iov_iter_alignment(const struct iov_iter *i)
1224{
Al Viro04a31162014-11-27 13:51:41 -05001225 unsigned long res = 0;
1226 size_t size = i->count;
1227
David Howells00e23702018-10-22 13:07:28 +01001228 if (unlikely(iov_iter_is_pipe(i))) {
Jan Karae0ff1262019-12-16 11:54:32 +01001229 unsigned int p_mask = i->pipe->ring_size - 1;
1230
David Howells8cefc102019-11-15 13:30:32 +00001231 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
Al Viro241699c2016-09-22 16:33:12 -04001232 return size | i->iov_offset;
1233 return size;
1234 }
Al Viro04a31162014-11-27 13:51:41 -05001235 iterate_all_kinds(i, size, v,
1236 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -05001237 res |= v.bv_offset | v.bv_len,
1238 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -05001239 )
1240 return res;
Al Viro62a80672014-04-04 23:12:29 -04001241}
1242EXPORT_SYMBOL(iov_iter_alignment);
1243
Al Viro357f4352016-04-08 19:05:19 -04001244unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1245{
Al Viro33844e62016-12-21 21:55:02 -05001246 unsigned long res = 0;
Al Viro357f4352016-04-08 19:05:19 -04001247 size_t size = i->count;
Al Viro357f4352016-04-08 19:05:19 -04001248
David Howells9ea9ce02018-10-20 00:57:56 +01001249 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001250 WARN_ON(1);
1251 return ~0U;
1252 }
1253
Al Viro357f4352016-04-08 19:05:19 -04001254 iterate_all_kinds(i, size, v,
1255 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1256 (size != v.iov_len ? size : 0), 0),
1257 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
1258 (size != v.bv_len ? size : 0)),
1259 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
1260 (size != v.iov_len ? size : 0))
1261 );
Al Viro33844e62016-12-21 21:55:02 -05001262 return res;
Al Viro357f4352016-04-08 19:05:19 -04001263}
1264EXPORT_SYMBOL(iov_iter_gap_alignment);
1265
Ilya Dryomove76b63122018-05-02 20:16:56 +02001266static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001267 size_t maxsize,
1268 struct page **pages,
David Howells8cefc102019-11-15 13:30:32 +00001269 int iter_head,
Al Viro241699c2016-09-22 16:33:12 -04001270 size_t *start)
1271{
1272 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001273 unsigned int p_mask = pipe->ring_size - 1;
1274 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001275 if (!n)
1276 return -EFAULT;
1277
1278 maxsize = n;
1279 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001280 while (n > 0) {
David Howells8cefc102019-11-15 13:30:32 +00001281 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1282 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -04001283 n -= PAGE_SIZE;
1284 }
1285
1286 return maxsize;
1287}
1288
1289static ssize_t pipe_get_pages(struct iov_iter *i,
1290 struct page **pages, size_t maxsize, unsigned maxpages,
1291 size_t *start)
1292{
David Howells8cefc102019-11-15 13:30:32 +00001293 unsigned int iter_head, npages;
Al Viro241699c2016-09-22 16:33:12 -04001294 size_t capacity;
Al Viro241699c2016-09-22 16:33:12 -04001295
Al Viro33844e62016-12-21 21:55:02 -05001296 if (!maxsize)
1297 return 0;
1298
Al Viro241699c2016-09-22 16:33:12 -04001299 if (!sanity(i))
1300 return -EFAULT;
1301
David Howells8cefc102019-11-15 13:30:32 +00001302 data_start(i, &iter_head, start);
1303 /* Amount of free space: some of this one + all after this one */
1304 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1305 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
Al Viro241699c2016-09-22 16:33:12 -04001306
David Howells8cefc102019-11-15 13:30:32 +00001307 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001308}
1309
Al Viro62a80672014-04-04 23:12:29 -04001310ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001311 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001312 size_t *start)
1313{
Al Viroe5393fa2014-11-27 14:12:09 -05001314 if (maxsize > i->count)
1315 maxsize = i->count;
1316
David Howells00e23702018-10-22 13:07:28 +01001317 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001318 return pipe_get_pages(i, pages, maxsize, maxpages, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001319 if (unlikely(iov_iter_is_discard(i)))
1320 return -EFAULT;
1321
Al Viroe5393fa2014-11-27 14:12:09 -05001322 iterate_all_kinds(i, maxsize, v, ({
1323 unsigned long addr = (unsigned long)v.iov_base;
1324 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1325 int n;
1326 int res;
1327
1328 if (len > maxpages * PAGE_SIZE)
1329 len = maxpages * PAGE_SIZE;
1330 addr &= ~(PAGE_SIZE - 1);
1331 n = DIV_ROUND_UP(len, PAGE_SIZE);
Ira Weiny73b01402019-05-13 17:17:11 -07001332 res = get_user_pages_fast(addr, n,
1333 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1334 pages);
Al Viroe5393fa2014-11-27 14:12:09 -05001335 if (unlikely(res < 0))
1336 return res;
1337 return (res == n ? len : res * PAGE_SIZE) - *start;
1338 0;}),({
1339 /* can't be more than PAGE_SIZE */
1340 *start = v.bv_offset;
1341 get_page(*pages = v.bv_page);
1342 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -05001343 }),({
1344 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -05001345 })
1346 )
1347 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001348}
1349EXPORT_SYMBOL(iov_iter_get_pages);
1350
Al Viro1b17f1f2014-11-27 14:14:31 -05001351static struct page **get_pages_array(size_t n)
1352{
Michal Hocko752ade62017-05-08 15:57:27 -07001353 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001354}
1355
Al Viro241699c2016-09-22 16:33:12 -04001356static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1357 struct page ***pages, size_t maxsize,
1358 size_t *start)
1359{
1360 struct page **p;
David Howells8cefc102019-11-15 13:30:32 +00001361 unsigned int iter_head, npages;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001362 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001363
Al Viro33844e62016-12-21 21:55:02 -05001364 if (!maxsize)
1365 return 0;
1366
Al Viro241699c2016-09-22 16:33:12 -04001367 if (!sanity(i))
1368 return -EFAULT;
1369
David Howells8cefc102019-11-15 13:30:32 +00001370 data_start(i, &iter_head, start);
1371 /* Amount of free space: some of this one + all after this one */
1372 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
Al Viro241699c2016-09-22 16:33:12 -04001373 n = npages * PAGE_SIZE - *start;
1374 if (maxsize > n)
1375 maxsize = n;
1376 else
1377 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1378 p = get_pages_array(npages);
1379 if (!p)
1380 return -ENOMEM;
David Howells8cefc102019-11-15 13:30:32 +00001381 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001382 if (n > 0)
1383 *pages = p;
1384 else
1385 kvfree(p);
1386 return n;
1387}
1388
Al Viro62a80672014-04-04 23:12:29 -04001389ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1390 struct page ***pages, size_t maxsize,
1391 size_t *start)
1392{
Al Viro1b17f1f2014-11-27 14:14:31 -05001393 struct page **p;
1394
1395 if (maxsize > i->count)
1396 maxsize = i->count;
1397
David Howells00e23702018-10-22 13:07:28 +01001398 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -04001399 return pipe_get_pages_alloc(i, pages, maxsize, start);
David Howells9ea9ce02018-10-20 00:57:56 +01001400 if (unlikely(iov_iter_is_discard(i)))
1401 return -EFAULT;
1402
Al Viro1b17f1f2014-11-27 14:14:31 -05001403 iterate_all_kinds(i, maxsize, v, ({
1404 unsigned long addr = (unsigned long)v.iov_base;
1405 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
1406 int n;
1407 int res;
1408
1409 addr &= ~(PAGE_SIZE - 1);
1410 n = DIV_ROUND_UP(len, PAGE_SIZE);
1411 p = get_pages_array(n);
1412 if (!p)
1413 return -ENOMEM;
Ira Weiny73b01402019-05-13 17:17:11 -07001414 res = get_user_pages_fast(addr, n,
1415 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
Al Viro1b17f1f2014-11-27 14:14:31 -05001416 if (unlikely(res < 0)) {
1417 kvfree(p);
1418 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, ({
1450 int err = 0;
Al Virocbbd26b2016-11-01 22:09:04 -04001451 next = csum_and_copy_from_user(v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001452 (to += v.iov_len) - v.iov_len,
1453 v.iov_len, 0, &err);
1454 if (!err) {
1455 sum = csum_block_add(sum, next, off);
1456 off += v.iov_len;
1457 }
1458 err ? v.iov_len : 0;
1459 }), ({
1460 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001461 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1462 p + v.bv_offset, v.bv_len,
1463 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001464 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001465 off += v.bv_len;
1466 }),({
Al Virof9152892018-11-27 22:32:59 -05001467 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1468 v.iov_base, v.iov_len,
1469 sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001470 off += v.iov_len;
1471 })
1472 )
1473 *csum = sum;
1474 return bytes;
1475}
1476EXPORT_SYMBOL(csum_and_copy_from_iter);
1477
Al Virocbbd26b2016-11-01 22:09:04 -04001478bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum,
1479 struct iov_iter *i)
1480{
1481 char *to = addr;
1482 __wsum sum, next;
1483 size_t off = 0;
1484 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001485 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Virocbbd26b2016-11-01 22:09:04 -04001486 WARN_ON(1);
1487 return false;
1488 }
1489 if (unlikely(i->count < bytes))
1490 return false;
1491 iterate_all_kinds(i, bytes, v, ({
1492 int err = 0;
1493 next = csum_and_copy_from_user(v.iov_base,
1494 (to += v.iov_len) - v.iov_len,
1495 v.iov_len, 0, &err);
1496 if (err)
1497 return false;
1498 sum = csum_block_add(sum, next, off);
1499 off += v.iov_len;
1500 0;
1501 }), ({
1502 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001503 sum = csum_and_memcpy((to += v.bv_len) - v.bv_len,
1504 p + v.bv_offset, v.bv_len,
1505 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001506 kunmap_atomic(p);
Al Virocbbd26b2016-11-01 22:09:04 -04001507 off += v.bv_len;
1508 }),({
Al Virof9152892018-11-27 22:32:59 -05001509 sum = csum_and_memcpy((to += v.iov_len) - v.iov_len,
1510 v.iov_base, v.iov_len,
1511 sum, off);
Al Virocbbd26b2016-11-01 22:09:04 -04001512 off += v.iov_len;
1513 })
1514 )
1515 *csum = sum;
1516 iov_iter_advance(i, bytes);
1517 return true;
1518}
1519EXPORT_SYMBOL(csum_and_copy_from_iter_full);
1520
Sagi Grimbergcb002d02018-12-03 17:52:07 -08001521size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csump,
Al Viroa604ec72014-11-24 01:08:00 -05001522 struct iov_iter *i)
1523{
Al Viro36f7a8a2015-12-06 16:49:22 -05001524 const char *from = addr;
Sagi Grimbergcb002d02018-12-03 17:52:07 -08001525 __wsum *csum = csump;
Al Viroa604ec72014-11-24 01:08:00 -05001526 __wsum sum, next;
1527 size_t off = 0;
Al Viro78e1f382018-11-25 16:24:16 -05001528
1529 if (unlikely(iov_iter_is_pipe(i)))
1530 return csum_and_copy_to_pipe_iter(addr, bytes, csum, i);
1531
Al Viroa604ec72014-11-24 01:08:00 -05001532 sum = *csum;
Al Viro78e1f382018-11-25 16:24:16 -05001533 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001534 WARN_ON(1); /* for now */
1535 return 0;
1536 }
Al Viroa604ec72014-11-24 01:08:00 -05001537 iterate_and_advance(i, bytes, v, ({
1538 int err = 0;
1539 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
Al Virocbbd26b2016-11-01 22:09:04 -04001540 v.iov_base,
Al Viroa604ec72014-11-24 01:08:00 -05001541 v.iov_len, 0, &err);
1542 if (!err) {
1543 sum = csum_block_add(sum, next, off);
1544 off += v.iov_len;
1545 }
1546 err ? v.iov_len : 0;
1547 }), ({
1548 char *p = kmap_atomic(v.bv_page);
Al Virof9152892018-11-27 22:32:59 -05001549 sum = csum_and_memcpy(p + v.bv_offset,
1550 (from += v.bv_len) - v.bv_len,
1551 v.bv_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001552 kunmap_atomic(p);
Al Viroa604ec72014-11-24 01:08:00 -05001553 off += v.bv_len;
1554 }),({
Al Virof9152892018-11-27 22:32:59 -05001555 sum = csum_and_memcpy(v.iov_base,
1556 (from += v.iov_len) - v.iov_len,
1557 v.iov_len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001558 off += v.iov_len;
1559 })
1560 )
1561 *csum = sum;
1562 return bytes;
1563}
1564EXPORT_SYMBOL(csum_and_copy_to_iter);
1565
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001566size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1567 struct iov_iter *i)
1568{
Herbert Xu79990962020-06-12 16:57:37 +10001569#ifdef CONFIG_CRYPTO_HASH
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001570 struct ahash_request *hash = hashp;
1571 struct scatterlist sg;
1572 size_t copied;
1573
1574 copied = copy_to_iter(addr, bytes, i);
1575 sg_init_one(&sg, addr, copied);
1576 ahash_request_set_crypt(hash, &sg, NULL, copied);
1577 crypto_ahash_update(hash);
1578 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001579#else
1580 return 0;
1581#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001582}
1583EXPORT_SYMBOL(hash_and_copy_to_iter);
1584
Al Viro62a80672014-04-04 23:12:29 -04001585int iov_iter_npages(const struct iov_iter *i, int maxpages)
1586{
Al Viroe0f2dc42014-11-27 14:09:46 -05001587 size_t size = i->count;
1588 int npages = 0;
1589
1590 if (!size)
1591 return 0;
David Howells9ea9ce02018-10-20 00:57:56 +01001592 if (unlikely(iov_iter_is_discard(i)))
1593 return 0;
Al Viroe0f2dc42014-11-27 14:09:46 -05001594
David Howells00e23702018-10-22 13:07:28 +01001595 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001596 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001597 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -04001598 size_t off;
Al Viro241699c2016-09-22 16:33:12 -04001599
1600 if (!sanity(i))
1601 return 0;
1602
David Howells8cefc102019-11-15 13:30:32 +00001603 data_start(i, &iter_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001604 /* some of this one + all after this one */
David Howells8cefc102019-11-15 13:30:32 +00001605 npages = pipe_space_for_user(iter_head, pipe->tail, pipe);
Al Viro241699c2016-09-22 16:33:12 -04001606 if (npages >= maxpages)
1607 return maxpages;
1608 } else iterate_all_kinds(i, size, v, ({
Al Viroe0f2dc42014-11-27 14:09:46 -05001609 unsigned long p = (unsigned long)v.iov_base;
1610 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1611 - p / PAGE_SIZE;
1612 if (npages >= maxpages)
1613 return maxpages;
1614 0;}),({
1615 npages++;
1616 if (npages >= maxpages)
1617 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -05001618 }),({
1619 unsigned long p = (unsigned long)v.iov_base;
1620 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1621 - p / PAGE_SIZE;
1622 if (npages >= maxpages)
1623 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -05001624 })
1625 )
1626 return npages;
Al Viro62a80672014-04-04 23:12:29 -04001627}
Al Virof67da302014-03-19 01:16:16 -04001628EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001629
1630const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1631{
1632 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001633 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001634 WARN_ON(1);
1635 return NULL;
1636 }
David Howells9ea9ce02018-10-20 00:57:56 +01001637 if (unlikely(iov_iter_is_discard(new)))
1638 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001639 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001640 return new->bvec = kmemdup(new->bvec,
1641 new->nr_segs * sizeof(struct bio_vec),
1642 flags);
1643 else
1644 /* iovec and kvec have identical layout */
1645 return new->iov = kmemdup(new->iov,
1646 new->nr_segs * sizeof(struct iovec),
1647 flags);
1648}
1649EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001650
Vegard Nossumffecee42016-10-08 11:18:07 +02001651/**
1652 * import_iovec() - Copy an array of &struct iovec from userspace
1653 * into the kernel, check that it is valid, and initialize a new
1654 * &struct iov_iter iterator to access it.
1655 *
1656 * @type: One of %READ or %WRITE.
1657 * @uvector: Pointer to the userspace array.
1658 * @nr_segs: Number of elements in userspace array.
1659 * @fast_segs: Number of elements in @iov.
1660 * @iov: (input and output parameter) Pointer to pointer to (usually small
1661 * on-stack) kernel array.
1662 * @i: Pointer to iterator that will be initialized on success.
1663 *
1664 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1665 * then this function places %NULL in *@iov on return. Otherwise, a new
1666 * array will be allocated and the result placed in *@iov. This means that
1667 * the caller may call kfree() on *@iov regardless of whether the small
1668 * on-stack array was used or not (and regardless of whether this function
1669 * returns an error or not).
1670 *
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001671 * Return: Negative error code on error, bytes imported on success
Vegard Nossumffecee42016-10-08 11:18:07 +02001672 */
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001673ssize_t import_iovec(int type, const struct iovec __user * uvector,
Al Virobc917be2015-03-21 17:45:43 -04001674 unsigned nr_segs, unsigned fast_segs,
1675 struct iovec **iov, struct iov_iter *i)
1676{
1677 ssize_t n;
1678 struct iovec *p;
1679 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1680 *iov, &p);
1681 if (n < 0) {
1682 if (p != *iov)
1683 kfree(p);
1684 *iov = NULL;
1685 return n;
1686 }
1687 iov_iter_init(i, type, p, nr_segs, n);
1688 *iov = p == *iov ? NULL : p;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001689 return n;
Al Virobc917be2015-03-21 17:45:43 -04001690}
1691EXPORT_SYMBOL(import_iovec);
1692
1693#ifdef CONFIG_COMPAT
1694#include <linux/compat.h>
1695
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001696ssize_t compat_import_iovec(int type,
1697 const struct compat_iovec __user * uvector,
1698 unsigned nr_segs, unsigned fast_segs,
1699 struct iovec **iov, struct iov_iter *i)
Al Virobc917be2015-03-21 17:45:43 -04001700{
1701 ssize_t n;
1702 struct iovec *p;
1703 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1704 *iov, &p);
1705 if (n < 0) {
1706 if (p != *iov)
1707 kfree(p);
1708 *iov = NULL;
1709 return n;
1710 }
1711 iov_iter_init(i, type, p, nr_segs, n);
1712 *iov = p == *iov ? NULL : p;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001713 return n;
Al Virobc917be2015-03-21 17:45:43 -04001714}
Arnd Bergmann98aaaec2019-03-14 17:45:18 +01001715EXPORT_SYMBOL(compat_import_iovec);
Al Virobc917be2015-03-21 17:45:43 -04001716#endif
1717
1718int import_single_range(int rw, void __user *buf, size_t len,
1719 struct iovec *iov, struct iov_iter *i)
1720{
1721 if (len > MAX_RW_COUNT)
1722 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001723 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04001724 return -EFAULT;
1725
1726 iov->iov_base = buf;
1727 iov->iov_len = len;
1728 iov_iter_init(i, rw, iov, 1, len);
1729 return 0;
1730}
Al Viroe1267582015-12-06 20:38:56 -05001731EXPORT_SYMBOL(import_single_range);
Al Viro09cf6982017-02-18 01:44:03 -05001732
1733int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
1734 int (*f)(struct kvec *vec, void *context),
1735 void *context)
1736{
1737 struct kvec w;
1738 int err = -EINVAL;
1739 if (!bytes)
1740 return 0;
1741
1742 iterate_all_kinds(i, bytes, v, -EINVAL, ({
1743 w.iov_base = kmap(v.bv_page) + v.bv_offset;
1744 w.iov_len = v.bv_len;
1745 err = f(&w, context);
1746 kunmap(v.bv_page);
1747 err;}), ({
1748 w = v;
1749 err = f(&w, context);})
1750 )
1751 return err;
1752}
1753EXPORT_SYMBOL(iov_iter_for_each_range);