Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
7999096f | 2 | #include <crypto/hash.h> |
4f18cd31 | 3 | #include <linux/export.h> |
2f8b5444 | 4 | #include <linux/bvec.h> |
4d0e9df5 | 5 | #include <linux/fault-inject-usercopy.h> |
4f18cd31 AV |
6 | #include <linux/uio.h> |
7 | #include <linux/pagemap.h> | |
91f79c43 AV |
8 | #include <linux/slab.h> |
9 | #include <linux/vmalloc.h> | |
241699cd | 10 | #include <linux/splice.h> |
bfdc5970 | 11 | #include <linux/compat.h> |
a604ec7e | 12 | #include <net/checksum.h> |
d05f4435 | 13 | #include <linux/scatterlist.h> |
d0ef4c36 | 14 | #include <linux/instrumented.h> |
4f18cd31 | 15 | |
241699cd AV |
16 | #define PIPE_PARANOIA /* for now */ |
17 | ||
04a31165 AV |
18 | #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 | ||
a280455f AV |
46 | #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 | ||
1bdc76ae ML |
69 | #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) { \ | |
04a31165 | 75 | (void)(STEP); \ |
04a31165 | 76 | } \ |
04a31165 AV |
77 | } |
78 | ||
a280455f | 79 | #define iterate_all_kinds(i, n, v, I, B, K) { \ |
33844e66 AV |
80 | 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)) \ | |
9ea9ce04 | 90 | } else if (unlikely(i->type & ITER_DISCARD)) { \ |
33844e66 AV |
91 | } else { \ |
92 | const struct iovec *iov; \ | |
93 | struct iovec v; \ | |
94 | iterate_iovec(i, n, v, iov, skip, (I)) \ | |
95 | } \ | |
04a31165 AV |
96 | } \ |
97 | } | |
98 | ||
a280455f | 99 | #define iterate_and_advance(i, n, v, I, B, K) { \ |
dd254f5a AV |
100 | if (unlikely(i->count < n)) \ |
101 | n = i->count; \ | |
19f18459 | 102 | if (i->count) { \ |
dd254f5a AV |
103 | size_t skip = i->iov_offset; \ |
104 | if (unlikely(i->type & ITER_BVEC)) { \ | |
1bdc76ae | 105 | const struct bio_vec *bvec = i->bvec; \ |
dd254f5a | 106 | struct bio_vec v; \ |
1bdc76ae ML |
107 | 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; \ | |
dd254f5a AV |
112 | } 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; \ | |
9ea9ce04 DH |
122 | } else if (unlikely(i->type & ITER_DISCARD)) { \ |
123 | skip += n; \ | |
dd254f5a AV |
124 | } 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; \ | |
7ce2a91e | 134 | } \ |
dd254f5a AV |
135 | i->count -= n; \ |
136 | i->iov_offset = skip; \ | |
7ce2a91e | 137 | } \ |
7ce2a91e AV |
138 | } |
139 | ||
09fc68dc AV |
140 | static int copyout(void __user *to, const void *from, size_t n) |
141 | { | |
4d0e9df5 AL |
142 | if (should_fail_usercopy()) |
143 | return n; | |
96d4f267 | 144 | if (access_ok(to, n)) { |
d0ef4c36 | 145 | instrument_copy_to_user(to, from, n); |
09fc68dc AV |
146 | n = raw_copy_to_user(to, from, n); |
147 | } | |
148 | return n; | |
149 | } | |
150 | ||
151 | static int copyin(void *to, const void __user *from, size_t n) | |
152 | { | |
4d0e9df5 AL |
153 | if (should_fail_usercopy()) |
154 | return n; | |
96d4f267 | 155 | if (access_ok(from, n)) { |
d0ef4c36 | 156 | instrument_copy_from_user(to, from, n); |
09fc68dc AV |
157 | n = raw_copy_from_user(to, from, n); |
158 | } | |
159 | return n; | |
160 | } | |
161 | ||
62a8067a | 162 | static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes, |
4f18cd31 AV |
163 | struct iov_iter *i) |
164 | { | |
165 | size_t skip, copy, left, wanted; | |
166 | const struct iovec *iov; | |
167 | char __user *buf; | |
168 | void *kaddr, *from; | |
169 | ||
170 | if (unlikely(bytes > i->count)) | |
171 | bytes = i->count; | |
172 | ||
173 | if (unlikely(!bytes)) | |
174 | return 0; | |
175 | ||
09fc68dc | 176 | might_fault(); |
4f18cd31 AV |
177 | wanted = bytes; |
178 | iov = i->iov; | |
179 | skip = i->iov_offset; | |
180 | buf = iov->iov_base + skip; | |
181 | copy = min(bytes, iov->iov_len - skip); | |
182 | ||
3fa6c507 | 183 | if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) { |
4f18cd31 AV |
184 | kaddr = kmap_atomic(page); |
185 | from = kaddr + offset; | |
186 | ||
187 | /* first chunk, usually the only one */ | |
09fc68dc | 188 | left = copyout(buf, from, copy); |
4f18cd31 AV |
189 | copy -= left; |
190 | skip += copy; | |
191 | from += copy; | |
192 | bytes -= copy; | |
193 | ||
194 | while (unlikely(!left && bytes)) { | |
195 | iov++; | |
196 | buf = iov->iov_base; | |
197 | copy = min(bytes, iov->iov_len); | |
09fc68dc | 198 | left = copyout(buf, from, copy); |
4f18cd31 AV |
199 | copy -= left; |
200 | skip = copy; | |
201 | from += copy; | |
202 | bytes -= copy; | |
203 | } | |
204 | if (likely(!bytes)) { | |
205 | kunmap_atomic(kaddr); | |
206 | goto done; | |
207 | } | |
208 | offset = from - kaddr; | |
209 | buf += copy; | |
210 | kunmap_atomic(kaddr); | |
211 | copy = min(bytes, iov->iov_len - skip); | |
212 | } | |
213 | /* Too bad - revert to non-atomic kmap */ | |
3fa6c507 | 214 | |
4f18cd31 AV |
215 | kaddr = kmap(page); |
216 | from = kaddr + offset; | |
09fc68dc | 217 | left = copyout(buf, from, copy); |
4f18cd31 AV |
218 | copy -= left; |
219 | skip += copy; | |
220 | from += copy; | |
221 | bytes -= copy; | |
222 | while (unlikely(!left && bytes)) { | |
223 | iov++; | |
224 | buf = iov->iov_base; | |
225 | copy = min(bytes, iov->iov_len); | |
09fc68dc | 226 | left = copyout(buf, from, copy); |
4f18cd31 AV |
227 | copy -= left; |
228 | skip = copy; | |
229 | from += copy; | |
230 | bytes -= copy; | |
231 | } | |
232 | kunmap(page); | |
3fa6c507 | 233 | |
4f18cd31 | 234 | done: |
81055e58 AV |
235 | if (skip == iov->iov_len) { |
236 | iov++; | |
237 | skip = 0; | |
238 | } | |
4f18cd31 AV |
239 | i->count -= wanted - bytes; |
240 | i->nr_segs -= iov - i->iov; | |
241 | i->iov = iov; | |
242 | i->iov_offset = skip; | |
243 | return wanted - bytes; | |
244 | } | |
4f18cd31 | 245 | |
62a8067a | 246 | static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes, |
f0d1bec9 AV |
247 | struct iov_iter *i) |
248 | { | |
249 | size_t skip, copy, left, wanted; | |
250 | const struct iovec *iov; | |
251 | char __user *buf; | |
252 | void *kaddr, *to; | |
253 | ||
254 | if (unlikely(bytes > i->count)) | |
255 | bytes = i->count; | |
256 | ||
257 | if (unlikely(!bytes)) | |
258 | return 0; | |
259 | ||
09fc68dc | 260 | might_fault(); |
f0d1bec9 AV |
261 | wanted = bytes; |
262 | iov = i->iov; | |
263 | skip = i->iov_offset; | |
264 | buf = iov->iov_base + skip; | |
265 | copy = min(bytes, iov->iov_len - skip); | |
266 | ||
3fa6c507 | 267 | if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) { |
f0d1bec9 AV |
268 | kaddr = kmap_atomic(page); |
269 | to = kaddr + offset; | |
270 | ||
271 | /* first chunk, usually the only one */ | |
09fc68dc | 272 | left = copyin(to, buf, copy); |
f0d1bec9 AV |
273 | copy -= left; |
274 | skip += copy; | |
275 | to += copy; | |
276 | bytes -= copy; | |
277 | ||
278 | while (unlikely(!left && bytes)) { | |
279 | iov++; | |
280 | buf = iov->iov_base; | |
281 | copy = min(bytes, iov->iov_len); | |
09fc68dc | 282 | left = copyin(to, buf, copy); |
f0d1bec9 AV |
283 | copy -= left; |
284 | skip = copy; | |
285 | to += copy; | |
286 | bytes -= copy; | |
287 | } | |
288 | if (likely(!bytes)) { | |
289 | kunmap_atomic(kaddr); | |
290 | goto done; | |
291 | } | |
292 | offset = to - kaddr; | |
293 | buf += copy; | |
294 | kunmap_atomic(kaddr); | |
295 | copy = min(bytes, iov->iov_len - skip); | |
296 | } | |
297 | /* Too bad - revert to non-atomic kmap */ | |
3fa6c507 | 298 | |
f0d1bec9 AV |
299 | kaddr = kmap(page); |
300 | to = kaddr + offset; | |
09fc68dc | 301 | left = copyin(to, buf, copy); |
f0d1bec9 AV |
302 | copy -= left; |
303 | skip += copy; | |
304 | to += copy; | |
305 | bytes -= copy; | |
306 | while (unlikely(!left && bytes)) { | |
307 | iov++; | |
308 | buf = iov->iov_base; | |
309 | copy = min(bytes, iov->iov_len); | |
09fc68dc | 310 | left = copyin(to, buf, copy); |
f0d1bec9 AV |
311 | copy -= left; |
312 | skip = copy; | |
313 | to += copy; | |
314 | bytes -= copy; | |
315 | } | |
316 | kunmap(page); | |
3fa6c507 | 317 | |
f0d1bec9 | 318 | done: |
81055e58 AV |
319 | if (skip == iov->iov_len) { |
320 | iov++; | |
321 | skip = 0; | |
322 | } | |
f0d1bec9 AV |
323 | i->count -= wanted - bytes; |
324 | i->nr_segs -= iov - i->iov; | |
325 | i->iov = iov; | |
326 | i->iov_offset = skip; | |
327 | return wanted - bytes; | |
328 | } | |
f0d1bec9 | 329 | |
241699cd AV |
330 | #ifdef PIPE_PARANOIA |
331 | static bool sanity(const struct iov_iter *i) | |
332 | { | |
333 | struct pipe_inode_info *pipe = i->pipe; | |
8cefc107 DH |
334 | unsigned int p_head = pipe->head; |
335 | unsigned int p_tail = pipe->tail; | |
336 | unsigned int p_mask = pipe->ring_size - 1; | |
337 | unsigned int p_occupancy = pipe_occupancy(p_head, p_tail); | |
338 | unsigned int i_head = i->head; | |
339 | unsigned int idx; | |
340 | ||
241699cd AV |
341 | if (i->iov_offset) { |
342 | struct pipe_buffer *p; | |
8cefc107 | 343 | if (unlikely(p_occupancy == 0)) |
241699cd | 344 | goto Bad; // pipe must be non-empty |
8cefc107 | 345 | if (unlikely(i_head != p_head - 1)) |
241699cd AV |
346 | goto Bad; // must be at the last buffer... |
347 | ||
8cefc107 | 348 | p = &pipe->bufs[i_head & p_mask]; |
241699cd AV |
349 | if (unlikely(p->offset + p->len != i->iov_offset)) |
350 | goto Bad; // ... at the end of segment | |
351 | } else { | |
8cefc107 | 352 | if (i_head != p_head) |
241699cd AV |
353 | goto Bad; // must be right after the last buffer |
354 | } | |
355 | return true; | |
356 | Bad: | |
8cefc107 DH |
357 | printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset); |
358 | printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n", | |
359 | p_head, p_tail, pipe->ring_size); | |
360 | for (idx = 0; idx < pipe->ring_size; idx++) | |
241699cd AV |
361 | printk(KERN_ERR "[%p %p %d %d]\n", |
362 | pipe->bufs[idx].ops, | |
363 | pipe->bufs[idx].page, | |
364 | pipe->bufs[idx].offset, | |
365 | pipe->bufs[idx].len); | |
366 | WARN_ON(1); | |
367 | return false; | |
368 | } | |
369 | #else | |
370 | #define sanity(i) true | |
371 | #endif | |
372 | ||
241699cd AV |
373 | static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes, |
374 | struct iov_iter *i) | |
375 | { | |
376 | struct pipe_inode_info *pipe = i->pipe; | |
377 | struct pipe_buffer *buf; | |
8cefc107 DH |
378 | unsigned int p_tail = pipe->tail; |
379 | unsigned int p_mask = pipe->ring_size - 1; | |
380 | unsigned int i_head = i->head; | |
241699cd | 381 | size_t off; |
241699cd AV |
382 | |
383 | if (unlikely(bytes > i->count)) | |
384 | bytes = i->count; | |
385 | ||
386 | if (unlikely(!bytes)) | |
387 | return 0; | |
388 | ||
389 | if (!sanity(i)) | |
390 | return 0; | |
391 | ||
392 | off = i->iov_offset; | |
8cefc107 | 393 | buf = &pipe->bufs[i_head & p_mask]; |
241699cd AV |
394 | if (off) { |
395 | if (offset == off && buf->page == page) { | |
396 | /* merge with the last one */ | |
397 | buf->len += bytes; | |
398 | i->iov_offset += bytes; | |
399 | goto out; | |
400 | } | |
8cefc107 DH |
401 | i_head++; |
402 | buf = &pipe->bufs[i_head & p_mask]; | |
241699cd | 403 | } |
6718b6f8 | 404 | if (pipe_full(i_head, p_tail, pipe->max_usage)) |
241699cd | 405 | return 0; |
8cefc107 | 406 | |
241699cd | 407 | buf->ops = &page_cache_pipe_buf_ops; |
8cefc107 DH |
408 | get_page(page); |
409 | buf->page = page; | |
241699cd AV |
410 | buf->offset = offset; |
411 | buf->len = bytes; | |
8cefc107 DH |
412 | |
413 | pipe->head = i_head + 1; | |
241699cd | 414 | i->iov_offset = offset + bytes; |
8cefc107 | 415 | i->head = i_head; |
241699cd AV |
416 | out: |
417 | i->count -= bytes; | |
418 | return bytes; | |
419 | } | |
420 | ||
171a0203 AA |
421 | /* |
422 | * Fault in one or more iovecs of the given iov_iter, to a maximum length of | |
423 | * bytes. For each iovec, fault in each page that constitutes the iovec. | |
424 | * | |
425 | * Return 0 on success, or non-zero if the memory could not be accessed (i.e. | |
426 | * because it is an invalid address). | |
427 | */ | |
d4690f1e | 428 | int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes) |
171a0203 AA |
429 | { |
430 | size_t skip = i->iov_offset; | |
431 | const struct iovec *iov; | |
432 | int err; | |
433 | struct iovec v; | |
434 | ||
435 | if (!(i->type & (ITER_BVEC|ITER_KVEC))) { | |
436 | iterate_iovec(i, bytes, v, iov, skip, ({ | |
4bce9f6e | 437 | err = fault_in_pages_readable(v.iov_base, v.iov_len); |
171a0203 AA |
438 | if (unlikely(err)) |
439 | return err; | |
440 | 0;})) | |
441 | } | |
442 | return 0; | |
443 | } | |
d4690f1e | 444 | EXPORT_SYMBOL(iov_iter_fault_in_readable); |
171a0203 | 445 | |
aa563d7b | 446 | void iov_iter_init(struct iov_iter *i, unsigned int direction, |
71d8e532 AV |
447 | const struct iovec *iov, unsigned long nr_segs, |
448 | size_t count) | |
449 | { | |
aa563d7b DH |
450 | WARN_ON(direction & ~(READ | WRITE)); |
451 | direction &= READ | WRITE; | |
452 | ||
71d8e532 | 453 | /* It will get better. Eventually... */ |
db68ce10 | 454 | if (uaccess_kernel()) { |
aa563d7b | 455 | i->type = ITER_KVEC | direction; |
a280455f AV |
456 | i->kvec = (struct kvec *)iov; |
457 | } else { | |
aa563d7b | 458 | i->type = ITER_IOVEC | direction; |
a280455f AV |
459 | i->iov = iov; |
460 | } | |
71d8e532 AV |
461 | i->nr_segs = nr_segs; |
462 | i->iov_offset = 0; | |
463 | i->count = count; | |
464 | } | |
465 | EXPORT_SYMBOL(iov_iter_init); | |
7b2c99d1 | 466 | |
62a8067a AV |
467 | static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len) |
468 | { | |
469 | char *from = kmap_atomic(page); | |
470 | memcpy(to, from + offset, len); | |
471 | kunmap_atomic(from); | |
472 | } | |
473 | ||
36f7a8a4 | 474 | static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len) |
62a8067a AV |
475 | { |
476 | char *to = kmap_atomic(page); | |
477 | memcpy(to + offset, from, len); | |
478 | kunmap_atomic(to); | |
479 | } | |
480 | ||
c35e0248 MW |
481 | static void memzero_page(struct page *page, size_t offset, size_t len) |
482 | { | |
483 | char *addr = kmap_atomic(page); | |
484 | memset(addr + offset, 0, len); | |
485 | kunmap_atomic(addr); | |
486 | } | |
487 | ||
241699cd AV |
488 | static inline bool allocated(struct pipe_buffer *buf) |
489 | { | |
490 | return buf->ops == &default_pipe_buf_ops; | |
491 | } | |
492 | ||
8cefc107 DH |
493 | static inline void data_start(const struct iov_iter *i, |
494 | unsigned int *iter_headp, size_t *offp) | |
241699cd | 495 | { |
8cefc107 DH |
496 | unsigned int p_mask = i->pipe->ring_size - 1; |
497 | unsigned int iter_head = i->head; | |
241699cd | 498 | size_t off = i->iov_offset; |
8cefc107 DH |
499 | |
500 | if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) || | |
501 | off == PAGE_SIZE)) { | |
502 | iter_head++; | |
241699cd AV |
503 | off = 0; |
504 | } | |
8cefc107 | 505 | *iter_headp = iter_head; |
241699cd AV |
506 | *offp = off; |
507 | } | |
508 | ||
509 | static size_t push_pipe(struct iov_iter *i, size_t size, | |
8cefc107 | 510 | int *iter_headp, size_t *offp) |
241699cd AV |
511 | { |
512 | struct pipe_inode_info *pipe = i->pipe; | |
8cefc107 DH |
513 | unsigned int p_tail = pipe->tail; |
514 | unsigned int p_mask = pipe->ring_size - 1; | |
515 | unsigned int iter_head; | |
241699cd | 516 | size_t off; |
241699cd AV |
517 | ssize_t left; |
518 | ||
519 | if (unlikely(size > i->count)) | |
520 | size = i->count; | |
521 | if (unlikely(!size)) | |
522 | return 0; | |
523 | ||
524 | left = size; | |
8cefc107 DH |
525 | data_start(i, &iter_head, &off); |
526 | *iter_headp = iter_head; | |
241699cd AV |
527 | *offp = off; |
528 | if (off) { | |
529 | left -= PAGE_SIZE - off; | |
530 | if (left <= 0) { | |
8cefc107 | 531 | pipe->bufs[iter_head & p_mask].len += size; |
241699cd AV |
532 | return size; |
533 | } | |
8cefc107 DH |
534 | pipe->bufs[iter_head & p_mask].len = PAGE_SIZE; |
535 | iter_head++; | |
241699cd | 536 | } |
6718b6f8 | 537 | while (!pipe_full(iter_head, p_tail, pipe->max_usage)) { |
8cefc107 | 538 | struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask]; |
241699cd AV |
539 | struct page *page = alloc_page(GFP_USER); |
540 | if (!page) | |
541 | break; | |
8cefc107 DH |
542 | |
543 | buf->ops = &default_pipe_buf_ops; | |
544 | buf->page = page; | |
545 | buf->offset = 0; | |
546 | buf->len = min_t(ssize_t, left, PAGE_SIZE); | |
547 | left -= buf->len; | |
548 | iter_head++; | |
549 | pipe->head = iter_head; | |
550 | ||
551 | if (left == 0) | |
241699cd | 552 | return size; |
241699cd AV |
553 | } |
554 | return size - left; | |
555 | } | |
556 | ||
557 | static size_t copy_pipe_to_iter(const void *addr, size_t bytes, | |
558 | struct iov_iter *i) | |
559 | { | |
560 | struct pipe_inode_info *pipe = i->pipe; | |
8cefc107 DH |
561 | unsigned int p_mask = pipe->ring_size - 1; |
562 | unsigned int i_head; | |
241699cd | 563 | size_t n, off; |
241699cd AV |
564 | |
565 | if (!sanity(i)) | |
566 | return 0; | |
567 | ||
8cefc107 | 568 | bytes = n = push_pipe(i, bytes, &i_head, &off); |
241699cd AV |
569 | if (unlikely(!n)) |
570 | return 0; | |
8cefc107 | 571 | do { |
241699cd | 572 | size_t chunk = min_t(size_t, n, PAGE_SIZE - off); |
8cefc107 DH |
573 | memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk); |
574 | i->head = i_head; | |
241699cd AV |
575 | i->iov_offset = off + chunk; |
576 | n -= chunk; | |
577 | addr += chunk; | |
8cefc107 DH |
578 | off = 0; |
579 | i_head++; | |
580 | } while (n); | |
241699cd AV |
581 | i->count -= bytes; |
582 | return bytes; | |
583 | } | |
584 | ||
f9152895 AV |
585 | static __wsum csum_and_memcpy(void *to, const void *from, size_t len, |
586 | __wsum sum, size_t off) | |
587 | { | |
cc44c17b | 588 | __wsum next = csum_partial_copy_nocheck(from, to, len); |
f9152895 AV |
589 | return csum_block_add(sum, next, off); |
590 | } | |
591 | ||
78e1f386 | 592 | static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes, |
52cbd23a WB |
593 | struct csum_state *csstate, |
594 | struct iov_iter *i) | |
78e1f386 AV |
595 | { |
596 | struct pipe_inode_info *pipe = i->pipe; | |
8cefc107 | 597 | unsigned int p_mask = pipe->ring_size - 1; |
52cbd23a WB |
598 | __wsum sum = csstate->csum; |
599 | size_t off = csstate->off; | |
8cefc107 | 600 | unsigned int i_head; |
78e1f386 | 601 | size_t n, r; |
78e1f386 AV |
602 | |
603 | if (!sanity(i)) | |
604 | return 0; | |
605 | ||
8cefc107 | 606 | bytes = n = push_pipe(i, bytes, &i_head, &r); |
78e1f386 AV |
607 | if (unlikely(!n)) |
608 | return 0; | |
8cefc107 | 609 | do { |
78e1f386 | 610 | size_t chunk = min_t(size_t, n, PAGE_SIZE - r); |
8cefc107 | 611 | char *p = kmap_atomic(pipe->bufs[i_head & p_mask].page); |
f9152895 | 612 | sum = csum_and_memcpy(p + r, addr, chunk, sum, off); |
78e1f386 | 613 | kunmap_atomic(p); |
8cefc107 | 614 | i->head = i_head; |
78e1f386 AV |
615 | i->iov_offset = r + chunk; |
616 | n -= chunk; | |
617 | off += chunk; | |
618 | addr += chunk; | |
8cefc107 DH |
619 | r = 0; |
620 | i_head++; | |
621 | } while (n); | |
78e1f386 | 622 | i->count -= bytes; |
52cbd23a WB |
623 | csstate->csum = sum; |
624 | csstate->off = off; | |
78e1f386 AV |
625 | return bytes; |
626 | } | |
627 | ||
aa28de27 | 628 | size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) |
62a8067a | 629 | { |
36f7a8a4 | 630 | const char *from = addr; |
00e23707 | 631 | if (unlikely(iov_iter_is_pipe(i))) |
241699cd | 632 | return copy_pipe_to_iter(addr, bytes, i); |
09fc68dc AV |
633 | if (iter_is_iovec(i)) |
634 | might_fault(); | |
3d4d3e48 | 635 | iterate_and_advance(i, bytes, v, |
09fc68dc | 636 | copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len), |
3d4d3e48 | 637 | memcpy_to_page(v.bv_page, v.bv_offset, |
a280455f AV |
638 | (from += v.bv_len) - v.bv_len, v.bv_len), |
639 | memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len) | |
3d4d3e48 | 640 | ) |
62a8067a | 641 | |
3d4d3e48 | 642 | return bytes; |
c35e0248 | 643 | } |
aa28de27 | 644 | EXPORT_SYMBOL(_copy_to_iter); |
c35e0248 | 645 | |
ec6347bb DW |
646 | #ifdef CONFIG_ARCH_HAS_COPY_MC |
647 | static int copyout_mc(void __user *to, const void *from, size_t n) | |
8780356e | 648 | { |
96d4f267 | 649 | if (access_ok(to, n)) { |
d0ef4c36 | 650 | instrument_copy_to_user(to, from, n); |
ec6347bb | 651 | n = copy_mc_to_user((__force void *) to, from, n); |
8780356e DW |
652 | } |
653 | return n; | |
654 | } | |
655 | ||
ec6347bb | 656 | static unsigned long copy_mc_to_page(struct page *page, size_t offset, |
8780356e DW |
657 | const char *from, size_t len) |
658 | { | |
659 | unsigned long ret; | |
660 | char *to; | |
661 | ||
662 | to = kmap_atomic(page); | |
ec6347bb | 663 | ret = copy_mc_to_kernel(to + offset, from, len); |
8780356e DW |
664 | kunmap_atomic(to); |
665 | ||
666 | return ret; | |
667 | } | |
668 | ||
ec6347bb | 669 | static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes, |
ca146f6f DW |
670 | struct iov_iter *i) |
671 | { | |
672 | struct pipe_inode_info *pipe = i->pipe; | |
8cefc107 DH |
673 | unsigned int p_mask = pipe->ring_size - 1; |
674 | unsigned int i_head; | |
ca146f6f | 675 | size_t n, off, xfer = 0; |
ca146f6f DW |
676 | |
677 | if (!sanity(i)) | |
678 | return 0; | |
679 | ||
8cefc107 | 680 | bytes = n = push_pipe(i, bytes, &i_head, &off); |
ca146f6f DW |
681 | if (unlikely(!n)) |
682 | return 0; | |
8cefc107 | 683 | do { |
ca146f6f DW |
684 | size_t chunk = min_t(size_t, n, PAGE_SIZE - off); |
685 | unsigned long rem; | |
686 | ||
ec6347bb | 687 | rem = copy_mc_to_page(pipe->bufs[i_head & p_mask].page, |
8cefc107 DH |
688 | off, addr, chunk); |
689 | i->head = i_head; | |
ca146f6f DW |
690 | i->iov_offset = off + chunk - rem; |
691 | xfer += chunk - rem; | |
692 | if (rem) | |
693 | break; | |
694 | n -= chunk; | |
695 | addr += chunk; | |
8cefc107 DH |
696 | off = 0; |
697 | i_head++; | |
698 | } while (n); | |
ca146f6f DW |
699 | i->count -= xfer; |
700 | return xfer; | |
701 | } | |
702 | ||
bf3eeb9b | 703 | /** |
ec6347bb | 704 | * _copy_mc_to_iter - copy to iter with source memory error exception handling |
bf3eeb9b DW |
705 | * @addr: source kernel address |
706 | * @bytes: total transfer length | |
707 | * @iter: destination iterator | |
708 | * | |
ec6347bb DW |
709 | * The pmem driver deploys this for the dax operation |
710 | * (dax_copy_to_iter()) for dax reads (bypass page-cache and the | |
711 | * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes | |
712 | * successfully copied. | |
bf3eeb9b | 713 | * |
ec6347bb | 714 | * The main differences between this and typical _copy_to_iter(). |
bf3eeb9b DW |
715 | * |
716 | * * Typical tail/residue handling after a fault retries the copy | |
717 | * byte-by-byte until the fault happens again. Re-triggering machine | |
718 | * checks is potentially fatal so the implementation uses source | |
719 | * alignment and poison alignment assumptions to avoid re-triggering | |
720 | * hardware exceptions. | |
721 | * | |
722 | * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies. | |
723 | * Compare to copy_to_iter() where only ITER_IOVEC attempts might return | |
724 | * a short copy. | |
bf3eeb9b | 725 | */ |
ec6347bb | 726 | size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i) |
8780356e DW |
727 | { |
728 | const char *from = addr; | |
729 | unsigned long rem, curr_addr, s_addr = (unsigned long) addr; | |
730 | ||
00e23707 | 731 | if (unlikely(iov_iter_is_pipe(i))) |
ec6347bb | 732 | return copy_mc_pipe_to_iter(addr, bytes, i); |
8780356e DW |
733 | if (iter_is_iovec(i)) |
734 | might_fault(); | |
735 | iterate_and_advance(i, bytes, v, | |
ec6347bb DW |
736 | copyout_mc(v.iov_base, (from += v.iov_len) - v.iov_len, |
737 | v.iov_len), | |
8780356e | 738 | ({ |
ec6347bb DW |
739 | rem = copy_mc_to_page(v.bv_page, v.bv_offset, |
740 | (from += v.bv_len) - v.bv_len, v.bv_len); | |
8780356e DW |
741 | if (rem) { |
742 | curr_addr = (unsigned long) from; | |
743 | bytes = curr_addr - s_addr - rem; | |
744 | return bytes; | |
745 | } | |
746 | }), | |
747 | ({ | |
ec6347bb DW |
748 | rem = copy_mc_to_kernel(v.iov_base, (from += v.iov_len) |
749 | - v.iov_len, v.iov_len); | |
8780356e DW |
750 | if (rem) { |
751 | curr_addr = (unsigned long) from; | |
752 | bytes = curr_addr - s_addr - rem; | |
753 | return bytes; | |
754 | } | |
755 | }) | |
756 | ) | |
757 | ||
758 | return bytes; | |
759 | } | |
ec6347bb DW |
760 | EXPORT_SYMBOL_GPL(_copy_mc_to_iter); |
761 | #endif /* CONFIG_ARCH_HAS_COPY_MC */ | |
8780356e | 762 | |
aa28de27 | 763 | size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) |
c35e0248 | 764 | { |
0dbca9a4 | 765 | char *to = addr; |
00e23707 | 766 | if (unlikely(iov_iter_is_pipe(i))) { |
241699cd AV |
767 | WARN_ON(1); |
768 | return 0; | |
769 | } | |
09fc68dc AV |
770 | if (iter_is_iovec(i)) |
771 | might_fault(); | |
0dbca9a4 | 772 | iterate_and_advance(i, bytes, v, |
09fc68dc | 773 | copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len), |
0dbca9a4 | 774 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
a280455f AV |
775 | v.bv_offset, v.bv_len), |
776 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len) | |
0dbca9a4 AV |
777 | ) |
778 | ||
779 | return bytes; | |
c35e0248 | 780 | } |
aa28de27 | 781 | EXPORT_SYMBOL(_copy_from_iter); |
c35e0248 | 782 | |
aa28de27 | 783 | bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i) |
cbbd26b8 AV |
784 | { |
785 | char *to = addr; | |
00e23707 | 786 | if (unlikely(iov_iter_is_pipe(i))) { |
cbbd26b8 AV |
787 | WARN_ON(1); |
788 | return false; | |
789 | } | |
33844e66 | 790 | if (unlikely(i->count < bytes)) |
cbbd26b8 AV |
791 | return false; |
792 | ||
09fc68dc AV |
793 | if (iter_is_iovec(i)) |
794 | might_fault(); | |
cbbd26b8 | 795 | iterate_all_kinds(i, bytes, v, ({ |
09fc68dc | 796 | if (copyin((to += v.iov_len) - v.iov_len, |
cbbd26b8 AV |
797 | v.iov_base, v.iov_len)) |
798 | return false; | |
799 | 0;}), | |
800 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, | |
801 | v.bv_offset, v.bv_len), | |
802 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len) | |
803 | ) | |
804 | ||
805 | iov_iter_advance(i, bytes); | |
806 | return true; | |
807 | } | |
aa28de27 | 808 | EXPORT_SYMBOL(_copy_from_iter_full); |
cbbd26b8 | 809 | |
aa28de27 | 810 | size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) |
aa583096 AV |
811 | { |
812 | char *to = addr; | |
00e23707 | 813 | if (unlikely(iov_iter_is_pipe(i))) { |
241699cd AV |
814 | WARN_ON(1); |
815 | return 0; | |
816 | } | |
aa583096 | 817 | iterate_and_advance(i, bytes, v, |
3f763453 | 818 | __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len, |
aa583096 AV |
819 | v.iov_base, v.iov_len), |
820 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, | |
821 | v.bv_offset, v.bv_len), | |
822 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len) | |
823 | ) | |
824 | ||
825 | return bytes; | |
826 | } | |
aa28de27 | 827 | EXPORT_SYMBOL(_copy_from_iter_nocache); |
aa583096 | 828 | |
0aed55af | 829 | #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE |
abd08d7d DW |
830 | /** |
831 | * _copy_from_iter_flushcache - write destination through cpu cache | |
832 | * @addr: destination kernel address | |
833 | * @bytes: total transfer length | |
834 | * @iter: source iterator | |
835 | * | |
836 | * The pmem driver arranges for filesystem-dax to use this facility via | |
837 | * dax_copy_from_iter() for ensuring that writes to persistent memory | |
838 | * are flushed through the CPU cache. It is differentiated from | |
839 | * _copy_from_iter_nocache() in that guarantees all data is flushed for | |
840 | * all iterator types. The _copy_from_iter_nocache() only attempts to | |
841 | * bypass the cache for the ITER_IOVEC case, and on some archs may use | |
842 | * instructions that strand dirty-data in the cache. | |
843 | */ | |
6a37e940 | 844 | size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i) |
0aed55af DW |
845 | { |
846 | char *to = addr; | |
00e23707 | 847 | if (unlikely(iov_iter_is_pipe(i))) { |
0aed55af DW |
848 | WARN_ON(1); |
849 | return 0; | |
850 | } | |
851 | iterate_and_advance(i, bytes, v, | |
852 | __copy_from_user_flushcache((to += v.iov_len) - v.iov_len, | |
853 | v.iov_base, v.iov_len), | |
854 | memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page, | |
855 | v.bv_offset, v.bv_len), | |
856 | memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base, | |
857 | v.iov_len) | |
858 | ) | |
859 | ||
860 | return bytes; | |
861 | } | |
6a37e940 | 862 | EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache); |
0aed55af DW |
863 | #endif |
864 | ||
aa28de27 | 865 | bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i) |
cbbd26b8 AV |
866 | { |
867 | char *to = addr; | |
00e23707 | 868 | if (unlikely(iov_iter_is_pipe(i))) { |
cbbd26b8 AV |
869 | WARN_ON(1); |
870 | return false; | |
871 | } | |
33844e66 | 872 | if (unlikely(i->count < bytes)) |
cbbd26b8 AV |
873 | return false; |
874 | iterate_all_kinds(i, bytes, v, ({ | |
3f763453 | 875 | if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len, |
cbbd26b8 AV |
876 | v.iov_base, v.iov_len)) |
877 | return false; | |
878 | 0;}), | |
879 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, | |
880 | v.bv_offset, v.bv_len), | |
881 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len) | |
882 | ) | |
883 | ||
884 | iov_iter_advance(i, bytes); | |
885 | return true; | |
886 | } | |
aa28de27 | 887 | EXPORT_SYMBOL(_copy_from_iter_full_nocache); |
cbbd26b8 | 888 | |
72e809ed AV |
889 | static inline bool page_copy_sane(struct page *page, size_t offset, size_t n) |
890 | { | |
6daef95b ED |
891 | struct page *head; |
892 | size_t v = n + offset; | |
893 | ||
894 | /* | |
895 | * The general case needs to access the page order in order | |
896 | * to compute the page size. | |
897 | * However, we mostly deal with order-0 pages and thus can | |
898 | * avoid a possible cache line miss for requests that fit all | |
899 | * page orders. | |
900 | */ | |
901 | if (n <= v && v <= PAGE_SIZE) | |
902 | return true; | |
903 | ||
904 | head = compound_head(page); | |
905 | v += (page - head) << PAGE_SHIFT; | |
a90bcb86 | 906 | |
a50b854e | 907 | if (likely(n <= v && v <= (page_size(head)))) |
72e809ed AV |
908 | return true; |
909 | WARN_ON(1); | |
910 | return false; | |
911 | } | |
cbbd26b8 | 912 | |
62a8067a AV |
913 | size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, |
914 | struct iov_iter *i) | |
915 | { | |
72e809ed AV |
916 | if (unlikely(!page_copy_sane(page, offset, bytes))) |
917 | return 0; | |
d271524a AV |
918 | if (i->type & (ITER_BVEC|ITER_KVEC)) { |
919 | void *kaddr = kmap_atomic(page); | |
920 | size_t wanted = copy_to_iter(kaddr + offset, bytes, i); | |
921 | kunmap_atomic(kaddr); | |
922 | return wanted; | |
9ea9ce04 DH |
923 | } else if (unlikely(iov_iter_is_discard(i))) |
924 | return bytes; | |
925 | else if (likely(!iov_iter_is_pipe(i))) | |
62a8067a | 926 | return copy_page_to_iter_iovec(page, offset, bytes, i); |
241699cd AV |
927 | else |
928 | return copy_page_to_iter_pipe(page, offset, bytes, i); | |
62a8067a AV |
929 | } |
930 | EXPORT_SYMBOL(copy_page_to_iter); | |
931 | ||
932 | size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, | |
933 | struct iov_iter *i) | |
934 | { | |
72e809ed AV |
935 | if (unlikely(!page_copy_sane(page, offset, bytes))) |
936 | return 0; | |
9ea9ce04 | 937 | if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { |
241699cd AV |
938 | WARN_ON(1); |
939 | return 0; | |
940 | } | |
a280455f | 941 | if (i->type & (ITER_BVEC|ITER_KVEC)) { |
d271524a | 942 | void *kaddr = kmap_atomic(page); |
aa28de27 | 943 | size_t wanted = _copy_from_iter(kaddr + offset, bytes, i); |
d271524a AV |
944 | kunmap_atomic(kaddr); |
945 | return wanted; | |
946 | } else | |
62a8067a AV |
947 | return copy_page_from_iter_iovec(page, offset, bytes, i); |
948 | } | |
949 | EXPORT_SYMBOL(copy_page_from_iter); | |
950 | ||
241699cd AV |
951 | static size_t pipe_zero(size_t bytes, struct iov_iter *i) |
952 | { | |
953 | struct pipe_inode_info *pipe = i->pipe; | |
8cefc107 DH |
954 | unsigned int p_mask = pipe->ring_size - 1; |
955 | unsigned int i_head; | |
241699cd | 956 | size_t n, off; |
241699cd AV |
957 | |
958 | if (!sanity(i)) | |
959 | return 0; | |
960 | ||
8cefc107 | 961 | bytes = n = push_pipe(i, bytes, &i_head, &off); |
241699cd AV |
962 | if (unlikely(!n)) |
963 | return 0; | |
964 | ||
8cefc107 | 965 | do { |
241699cd | 966 | size_t chunk = min_t(size_t, n, PAGE_SIZE - off); |
8cefc107 DH |
967 | memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk); |
968 | i->head = i_head; | |
241699cd AV |
969 | i->iov_offset = off + chunk; |
970 | n -= chunk; | |
8cefc107 DH |
971 | off = 0; |
972 | i_head++; | |
973 | } while (n); | |
241699cd AV |
974 | i->count -= bytes; |
975 | return bytes; | |
976 | } | |
977 | ||
c35e0248 MW |
978 | size_t iov_iter_zero(size_t bytes, struct iov_iter *i) |
979 | { | |
00e23707 | 980 | if (unlikely(iov_iter_is_pipe(i))) |
241699cd | 981 | return pipe_zero(bytes, i); |
8442fa46 | 982 | iterate_and_advance(i, bytes, v, |
09fc68dc | 983 | clear_user(v.iov_base, v.iov_len), |
a280455f AV |
984 | memzero_page(v.bv_page, v.bv_offset, v.bv_len), |
985 | memset(v.iov_base, 0, v.iov_len) | |
8442fa46 AV |
986 | ) |
987 | ||
988 | return bytes; | |
c35e0248 MW |
989 | } |
990 | EXPORT_SYMBOL(iov_iter_zero); | |
991 | ||
62a8067a AV |
992 | size_t iov_iter_copy_from_user_atomic(struct page *page, |
993 | struct iov_iter *i, unsigned long offset, size_t bytes) | |
994 | { | |
04a31165 | 995 | char *kaddr = kmap_atomic(page), *p = kaddr + offset; |
72e809ed AV |
996 | if (unlikely(!page_copy_sane(page, offset, bytes))) { |
997 | kunmap_atomic(kaddr); | |
998 | return 0; | |
999 | } | |
9ea9ce04 | 1000 | if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { |
241699cd AV |
1001 | kunmap_atomic(kaddr); |
1002 | WARN_ON(1); | |
1003 | return 0; | |
1004 | } | |
04a31165 | 1005 | iterate_all_kinds(i, bytes, v, |
09fc68dc | 1006 | copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len), |
04a31165 | 1007 | memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page, |
a280455f AV |
1008 | v.bv_offset, v.bv_len), |
1009 | memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len) | |
04a31165 AV |
1010 | ) |
1011 | kunmap_atomic(kaddr); | |
1012 | return bytes; | |
62a8067a AV |
1013 | } |
1014 | EXPORT_SYMBOL(iov_iter_copy_from_user_atomic); | |
1015 | ||
b9dc6f65 AV |
1016 | static inline void pipe_truncate(struct iov_iter *i) |
1017 | { | |
1018 | struct pipe_inode_info *pipe = i->pipe; | |
8cefc107 DH |
1019 | unsigned int p_tail = pipe->tail; |
1020 | unsigned int p_head = pipe->head; | |
1021 | unsigned int p_mask = pipe->ring_size - 1; | |
1022 | ||
1023 | if (!pipe_empty(p_head, p_tail)) { | |
1024 | struct pipe_buffer *buf; | |
1025 | unsigned int i_head = i->head; | |
b9dc6f65 | 1026 | size_t off = i->iov_offset; |
8cefc107 | 1027 | |
b9dc6f65 | 1028 | if (off) { |
8cefc107 DH |
1029 | buf = &pipe->bufs[i_head & p_mask]; |
1030 | buf->len = off - buf->offset; | |
1031 | i_head++; | |
b9dc6f65 | 1032 | } |
8cefc107 DH |
1033 | while (p_head != i_head) { |
1034 | p_head--; | |
1035 | pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]); | |
b9dc6f65 | 1036 | } |
8cefc107 DH |
1037 | |
1038 | pipe->head = p_head; | |
b9dc6f65 AV |
1039 | } |
1040 | } | |
1041 | ||
241699cd AV |
1042 | static void pipe_advance(struct iov_iter *i, size_t size) |
1043 | { | |
1044 | struct pipe_inode_info *pipe = i->pipe; | |
241699cd AV |
1045 | if (unlikely(i->count < size)) |
1046 | size = i->count; | |
241699cd | 1047 | if (size) { |
b9dc6f65 | 1048 | struct pipe_buffer *buf; |
8cefc107 DH |
1049 | unsigned int p_mask = pipe->ring_size - 1; |
1050 | unsigned int i_head = i->head; | |
b9dc6f65 | 1051 | size_t off = i->iov_offset, left = size; |
8cefc107 | 1052 | |
241699cd | 1053 | if (off) /* make it relative to the beginning of buffer */ |
8cefc107 | 1054 | left += off - pipe->bufs[i_head & p_mask].offset; |
241699cd | 1055 | while (1) { |
8cefc107 | 1056 | buf = &pipe->bufs[i_head & p_mask]; |
b9dc6f65 | 1057 | if (left <= buf->len) |
241699cd | 1058 | break; |
b9dc6f65 | 1059 | left -= buf->len; |
8cefc107 | 1060 | i_head++; |
241699cd | 1061 | } |
8cefc107 | 1062 | i->head = i_head; |
b9dc6f65 | 1063 | i->iov_offset = buf->offset + left; |
241699cd | 1064 | } |
b9dc6f65 AV |
1065 | i->count -= size; |
1066 | /* ... and discard everything past that point */ | |
1067 | pipe_truncate(i); | |
241699cd AV |
1068 | } |
1069 | ||
54c8195b PB |
1070 | static void iov_iter_bvec_advance(struct iov_iter *i, size_t size) |
1071 | { | |
1072 | struct bvec_iter bi; | |
1073 | ||
1074 | bi.bi_size = i->count; | |
1075 | bi.bi_bvec_done = i->iov_offset; | |
1076 | bi.bi_idx = 0; | |
1077 | bvec_iter_advance(i->bvec, &bi, size); | |
1078 | ||
1079 | i->bvec += bi.bi_idx; | |
1080 | i->nr_segs -= bi.bi_idx; | |
1081 | i->count = bi.bi_size; | |
1082 | i->iov_offset = bi.bi_bvec_done; | |
1083 | } | |
1084 | ||
62a8067a AV |
1085 | void iov_iter_advance(struct iov_iter *i, size_t size) |
1086 | { | |
00e23707 | 1087 | if (unlikely(iov_iter_is_pipe(i))) { |
241699cd AV |
1088 | pipe_advance(i, size); |
1089 | return; | |
1090 | } | |
9ea9ce04 DH |
1091 | if (unlikely(iov_iter_is_discard(i))) { |
1092 | i->count -= size; | |
1093 | return; | |
1094 | } | |
54c8195b PB |
1095 | if (iov_iter_is_bvec(i)) { |
1096 | iov_iter_bvec_advance(i, size); | |
1097 | return; | |
1098 | } | |
a280455f | 1099 | iterate_and_advance(i, size, v, 0, 0, 0) |
62a8067a AV |
1100 | } |
1101 | EXPORT_SYMBOL(iov_iter_advance); | |
1102 | ||
27c0e374 AV |
1103 | void iov_iter_revert(struct iov_iter *i, size_t unroll) |
1104 | { | |
1105 | if (!unroll) | |
1106 | return; | |
5b47d59a AV |
1107 | if (WARN_ON(unroll > MAX_RW_COUNT)) |
1108 | return; | |
27c0e374 | 1109 | i->count += unroll; |
00e23707 | 1110 | if (unlikely(iov_iter_is_pipe(i))) { |
27c0e374 | 1111 | struct pipe_inode_info *pipe = i->pipe; |
8cefc107 DH |
1112 | unsigned int p_mask = pipe->ring_size - 1; |
1113 | unsigned int i_head = i->head; | |
27c0e374 AV |
1114 | size_t off = i->iov_offset; |
1115 | while (1) { | |
8cefc107 DH |
1116 | struct pipe_buffer *b = &pipe->bufs[i_head & p_mask]; |
1117 | size_t n = off - b->offset; | |
27c0e374 | 1118 | if (unroll < n) { |
4fa55cef | 1119 | off -= unroll; |
27c0e374 AV |
1120 | break; |
1121 | } | |
1122 | unroll -= n; | |
8cefc107 | 1123 | if (!unroll && i_head == i->start_head) { |
27c0e374 AV |
1124 | off = 0; |
1125 | break; | |
1126 | } | |
8cefc107 DH |
1127 | i_head--; |
1128 | b = &pipe->bufs[i_head & p_mask]; | |
1129 | off = b->offset + b->len; | |
27c0e374 AV |
1130 | } |
1131 | i->iov_offset = off; | |
8cefc107 | 1132 | i->head = i_head; |
27c0e374 AV |
1133 | pipe_truncate(i); |
1134 | return; | |
1135 | } | |
9ea9ce04 DH |
1136 | if (unlikely(iov_iter_is_discard(i))) |
1137 | return; | |
27c0e374 AV |
1138 | if (unroll <= i->iov_offset) { |
1139 | i->iov_offset -= unroll; | |
1140 | return; | |
1141 | } | |
1142 | unroll -= i->iov_offset; | |
00e23707 | 1143 | if (iov_iter_is_bvec(i)) { |
27c0e374 AV |
1144 | const struct bio_vec *bvec = i->bvec; |
1145 | while (1) { | |
1146 | size_t n = (--bvec)->bv_len; | |
1147 | i->nr_segs++; | |
1148 | if (unroll <= n) { | |
1149 | i->bvec = bvec; | |
1150 | i->iov_offset = n - unroll; | |
1151 | return; | |
1152 | } | |
1153 | unroll -= n; | |
1154 | } | |
1155 | } else { /* same logics for iovec and kvec */ | |
1156 | const struct iovec *iov = i->iov; | |
1157 | while (1) { | |
1158 | size_t n = (--iov)->iov_len; | |
1159 | i->nr_segs++; | |
1160 | if (unroll <= n) { | |
1161 | i->iov = iov; | |
1162 | i->iov_offset = n - unroll; | |
1163 | return; | |
1164 | } | |
1165 | unroll -= n; | |
1166 | } | |
1167 | } | |
1168 | } | |
1169 | EXPORT_SYMBOL(iov_iter_revert); | |
1170 | ||
62a8067a AV |
1171 | /* |
1172 | * Return the count of just the current iov_iter segment. | |
1173 | */ | |
1174 | size_t iov_iter_single_seg_count(const struct iov_iter *i) | |
1175 | { | |
00e23707 | 1176 | if (unlikely(iov_iter_is_pipe(i))) |
241699cd | 1177 | return i->count; // it is a silly place, anyway |
62a8067a AV |
1178 | if (i->nr_segs == 1) |
1179 | return i->count; | |
9ea9ce04 DH |
1180 | if (unlikely(iov_iter_is_discard(i))) |
1181 | return i->count; | |
00e23707 | 1182 | else if (iov_iter_is_bvec(i)) |
62a8067a | 1183 | return min(i->count, i->bvec->bv_len - i->iov_offset); |
ad0eab92 PM |
1184 | else |
1185 | return min(i->count, i->iov->iov_len - i->iov_offset); | |
62a8067a AV |
1186 | } |
1187 | EXPORT_SYMBOL(iov_iter_single_seg_count); | |
1188 | ||
aa563d7b | 1189 | void iov_iter_kvec(struct iov_iter *i, unsigned int direction, |
05afcb77 | 1190 | const struct kvec *kvec, unsigned long nr_segs, |
abb78f87 AV |
1191 | size_t count) |
1192 | { | |
aa563d7b DH |
1193 | WARN_ON(direction & ~(READ | WRITE)); |
1194 | i->type = ITER_KVEC | (direction & (READ | WRITE)); | |
05afcb77 | 1195 | i->kvec = kvec; |
abb78f87 AV |
1196 | i->nr_segs = nr_segs; |
1197 | i->iov_offset = 0; | |
1198 | i->count = count; | |
1199 | } | |
1200 | EXPORT_SYMBOL(iov_iter_kvec); | |
1201 | ||
aa563d7b | 1202 | void iov_iter_bvec(struct iov_iter *i, unsigned int direction, |
05afcb77 AV |
1203 | const struct bio_vec *bvec, unsigned long nr_segs, |
1204 | size_t count) | |
1205 | { | |
aa563d7b DH |
1206 | WARN_ON(direction & ~(READ | WRITE)); |
1207 | i->type = ITER_BVEC | (direction & (READ | WRITE)); | |
05afcb77 AV |
1208 | i->bvec = bvec; |
1209 | i->nr_segs = nr_segs; | |
1210 | i->iov_offset = 0; | |
1211 | i->count = count; | |
1212 | } | |
1213 | EXPORT_SYMBOL(iov_iter_bvec); | |
1214 | ||
aa563d7b | 1215 | void iov_iter_pipe(struct iov_iter *i, unsigned int direction, |
241699cd AV |
1216 | struct pipe_inode_info *pipe, |
1217 | size_t count) | |
1218 | { | |
aa563d7b | 1219 | BUG_ON(direction != READ); |
8cefc107 | 1220 | WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size)); |
aa563d7b | 1221 | i->type = ITER_PIPE | READ; |
241699cd | 1222 | i->pipe = pipe; |
8cefc107 | 1223 | i->head = pipe->head; |
241699cd AV |
1224 | i->iov_offset = 0; |
1225 | i->count = count; | |
8cefc107 | 1226 | i->start_head = i->head; |
241699cd AV |
1227 | } |
1228 | EXPORT_SYMBOL(iov_iter_pipe); | |
1229 | ||
9ea9ce04 DH |
1230 | /** |
1231 | * iov_iter_discard - Initialise an I/O iterator that discards data | |
1232 | * @i: The iterator to initialise. | |
1233 | * @direction: The direction of the transfer. | |
1234 | * @count: The size of the I/O buffer in bytes. | |
1235 | * | |
1236 | * Set up an I/O iterator that just discards everything that's written to it. | |
1237 | * It's only available as a READ iterator. | |
1238 | */ | |
1239 | void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count) | |
1240 | { | |
1241 | BUG_ON(direction != READ); | |
1242 | i->type = ITER_DISCARD | READ; | |
1243 | i->count = count; | |
1244 | i->iov_offset = 0; | |
1245 | } | |
1246 | EXPORT_SYMBOL(iov_iter_discard); | |
1247 | ||
62a8067a AV |
1248 | unsigned long iov_iter_alignment(const struct iov_iter *i) |
1249 | { | |
04a31165 AV |
1250 | unsigned long res = 0; |
1251 | size_t size = i->count; | |
1252 | ||
00e23707 | 1253 | if (unlikely(iov_iter_is_pipe(i))) { |
e0ff126e JK |
1254 | unsigned int p_mask = i->pipe->ring_size - 1; |
1255 | ||
8cefc107 | 1256 | if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask])) |
241699cd AV |
1257 | return size | i->iov_offset; |
1258 | return size; | |
1259 | } | |
04a31165 AV |
1260 | iterate_all_kinds(i, size, v, |
1261 | (res |= (unsigned long)v.iov_base | v.iov_len, 0), | |
a280455f AV |
1262 | res |= v.bv_offset | v.bv_len, |
1263 | res |= (unsigned long)v.iov_base | v.iov_len | |
04a31165 AV |
1264 | ) |
1265 | return res; | |
62a8067a AV |
1266 | } |
1267 | EXPORT_SYMBOL(iov_iter_alignment); | |
1268 | ||
357f435d AV |
1269 | unsigned long iov_iter_gap_alignment(const struct iov_iter *i) |
1270 | { | |
33844e66 | 1271 | unsigned long res = 0; |
357f435d | 1272 | size_t size = i->count; |
357f435d | 1273 | |
9ea9ce04 | 1274 | if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { |
241699cd AV |
1275 | WARN_ON(1); |
1276 | return ~0U; | |
1277 | } | |
1278 | ||
357f435d AV |
1279 | iterate_all_kinds(i, size, v, |
1280 | (res |= (!res ? 0 : (unsigned long)v.iov_base) | | |
1281 | (size != v.iov_len ? size : 0), 0), | |
1282 | (res |= (!res ? 0 : (unsigned long)v.bv_offset) | | |
1283 | (size != v.bv_len ? size : 0)), | |
1284 | (res |= (!res ? 0 : (unsigned long)v.iov_base) | | |
1285 | (size != v.iov_len ? size : 0)) | |
1286 | ); | |
33844e66 | 1287 | return res; |
357f435d AV |
1288 | } |
1289 | EXPORT_SYMBOL(iov_iter_gap_alignment); | |
1290 | ||
e76b6312 | 1291 | static inline ssize_t __pipe_get_pages(struct iov_iter *i, |
241699cd AV |
1292 | size_t maxsize, |
1293 | struct page **pages, | |
8cefc107 | 1294 | int iter_head, |
241699cd AV |
1295 | size_t *start) |
1296 | { | |
1297 | struct pipe_inode_info *pipe = i->pipe; | |
8cefc107 DH |
1298 | unsigned int p_mask = pipe->ring_size - 1; |
1299 | ssize_t n = push_pipe(i, maxsize, &iter_head, start); | |
241699cd AV |
1300 | if (!n) |
1301 | return -EFAULT; | |
1302 | ||
1303 | maxsize = n; | |
1304 | n += *start; | |
1689c73a | 1305 | while (n > 0) { |
8cefc107 DH |
1306 | get_page(*pages++ = pipe->bufs[iter_head & p_mask].page); |
1307 | iter_head++; | |
241699cd AV |
1308 | n -= PAGE_SIZE; |
1309 | } | |
1310 | ||
1311 | return maxsize; | |
1312 | } | |
1313 | ||
1314 | static ssize_t pipe_get_pages(struct iov_iter *i, | |
1315 | struct page **pages, size_t maxsize, unsigned maxpages, | |
1316 | size_t *start) | |
1317 | { | |
8cefc107 | 1318 | unsigned int iter_head, npages; |
241699cd | 1319 | size_t capacity; |
241699cd | 1320 | |
33844e66 AV |
1321 | if (!maxsize) |
1322 | return 0; | |
1323 | ||
241699cd AV |
1324 | if (!sanity(i)) |
1325 | return -EFAULT; | |
1326 | ||
8cefc107 DH |
1327 | data_start(i, &iter_head, start); |
1328 | /* Amount of free space: some of this one + all after this one */ | |
1329 | npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe); | |
1330 | capacity = min(npages, maxpages) * PAGE_SIZE - *start; | |
241699cd | 1331 | |
8cefc107 | 1332 | return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start); |
241699cd AV |
1333 | } |
1334 | ||
62a8067a | 1335 | ssize_t iov_iter_get_pages(struct iov_iter *i, |
2c80929c | 1336 | struct page **pages, size_t maxsize, unsigned maxpages, |
62a8067a AV |
1337 | size_t *start) |
1338 | { | |
e5393fae AV |
1339 | if (maxsize > i->count) |
1340 | maxsize = i->count; | |
1341 | ||
00e23707 | 1342 | if (unlikely(iov_iter_is_pipe(i))) |
241699cd | 1343 | return pipe_get_pages(i, pages, maxsize, maxpages, start); |
9ea9ce04 DH |
1344 | if (unlikely(iov_iter_is_discard(i))) |
1345 | return -EFAULT; | |
1346 | ||
e5393fae AV |
1347 | iterate_all_kinds(i, maxsize, v, ({ |
1348 | unsigned long addr = (unsigned long)v.iov_base; | |
1349 | size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1)); | |
1350 | int n; | |
1351 | int res; | |
1352 | ||
1353 | if (len > maxpages * PAGE_SIZE) | |
1354 | len = maxpages * PAGE_SIZE; | |
1355 | addr &= ~(PAGE_SIZE - 1); | |
1356 | n = DIV_ROUND_UP(len, PAGE_SIZE); | |
73b0140b IW |
1357 | res = get_user_pages_fast(addr, n, |
1358 | iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, | |
1359 | pages); | |
e5393fae AV |
1360 | if (unlikely(res < 0)) |
1361 | return res; | |
1362 | return (res == n ? len : res * PAGE_SIZE) - *start; | |
1363 | 0;}),({ | |
1364 | /* can't be more than PAGE_SIZE */ | |
1365 | *start = v.bv_offset; | |
1366 | get_page(*pages = v.bv_page); | |
1367 | return v.bv_len; | |
a280455f AV |
1368 | }),({ |
1369 | return -EFAULT; | |
e5393fae AV |
1370 | }) |
1371 | ) | |
1372 | return 0; | |
62a8067a AV |
1373 | } |
1374 | EXPORT_SYMBOL(iov_iter_get_pages); | |
1375 | ||
1b17f1f2 AV |
1376 | static struct page **get_pages_array(size_t n) |
1377 | { | |
752ade68 | 1378 | return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL); |
1b17f1f2 AV |
1379 | } |
1380 | ||
241699cd AV |
1381 | static ssize_t pipe_get_pages_alloc(struct iov_iter *i, |
1382 | struct page ***pages, size_t maxsize, | |
1383 | size_t *start) | |
1384 | { | |
1385 | struct page **p; | |
8cefc107 | 1386 | unsigned int iter_head, npages; |
d7760d63 | 1387 | ssize_t n; |
241699cd | 1388 | |
33844e66 AV |
1389 | if (!maxsize) |
1390 | return 0; | |
1391 | ||
241699cd AV |
1392 | if (!sanity(i)) |
1393 | return -EFAULT; | |
1394 | ||
8cefc107 DH |
1395 | data_start(i, &iter_head, start); |
1396 | /* Amount of free space: some of this one + all after this one */ | |
1397 | npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe); | |
241699cd AV |
1398 | n = npages * PAGE_SIZE - *start; |
1399 | if (maxsize > n) | |
1400 | maxsize = n; | |
1401 | else | |
1402 | npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE); | |
1403 | p = get_pages_array(npages); | |
1404 | if (!p) | |
1405 | return -ENOMEM; | |
8cefc107 | 1406 | n = __pipe_get_pages(i, maxsize, p, iter_head, start); |
241699cd AV |
1407 | if (n > 0) |
1408 | *pages = p; | |
1409 | else | |
1410 | kvfree(p); | |
1411 | return n; | |
1412 | } | |
1413 | ||
62a8067a AV |
1414 | ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, |
1415 | struct page ***pages, size_t maxsize, | |
1416 | size_t *start) | |
1417 | { | |
1b17f1f2 AV |
1418 | struct page **p; |
1419 | ||
1420 | if (maxsize > i->count) | |
1421 | maxsize = i->count; | |
1422 | ||
00e23707 | 1423 | if (unlikely(iov_iter_is_pipe(i))) |
241699cd | 1424 | return pipe_get_pages_alloc(i, pages, maxsize, start); |
9ea9ce04 DH |
1425 | if (unlikely(iov_iter_is_discard(i))) |
1426 | return -EFAULT; | |
1427 | ||
1b17f1f2 AV |
1428 | iterate_all_kinds(i, maxsize, v, ({ |
1429 | unsigned long addr = (unsigned long)v.iov_base; | |
1430 | size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1)); | |
1431 | int n; | |
1432 | int res; | |
1433 | ||
1434 | addr &= ~(PAGE_SIZE - 1); | |
1435 | n = DIV_ROUND_UP(len, PAGE_SIZE); | |
1436 | p = get_pages_array(n); | |
1437 | if (!p) | |
1438 | return -ENOMEM; | |
73b0140b IW |
1439 | res = get_user_pages_fast(addr, n, |
1440 | iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p); | |
1b17f1f2 AV |
1441 | if (unlikely(res < 0)) { |
1442 | kvfree(p); | |
1443 | return res; | |
1444 | } | |
1445 | *pages = p; | |
1446 | return (res == n ? len : res * PAGE_SIZE) - *start; | |
1447 | 0;}),({ | |
1448 | /* can't be more than PAGE_SIZE */ | |
1449 | *start = v.bv_offset; | |
1450 | *pages = p = get_pages_array(1); | |
1451 | if (!p) | |
1452 | return -ENOMEM; | |
1453 | get_page(*p = v.bv_page); | |
1454 | return v.bv_len; | |
a280455f AV |
1455 | }),({ |
1456 | return -EFAULT; | |
1b17f1f2 AV |
1457 | }) |
1458 | ) | |
1459 | return 0; | |
62a8067a AV |
1460 | } |
1461 | EXPORT_SYMBOL(iov_iter_get_pages_alloc); | |
1462 | ||
a604ec7e AV |
1463 | size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, |
1464 | struct iov_iter *i) | |
1465 | { | |
1466 | char *to = addr; | |
1467 | __wsum sum, next; | |
1468 | size_t off = 0; | |
a604ec7e | 1469 | sum = *csum; |
9ea9ce04 | 1470 | if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { |
241699cd AV |
1471 | WARN_ON(1); |
1472 | return 0; | |
1473 | } | |
a604ec7e | 1474 | iterate_and_advance(i, bytes, v, ({ |
cbbd26b8 | 1475 | next = csum_and_copy_from_user(v.iov_base, |
a604ec7e | 1476 | (to += v.iov_len) - v.iov_len, |
c693cc46 AV |
1477 | v.iov_len); |
1478 | if (next) { | |
a604ec7e AV |
1479 | sum = csum_block_add(sum, next, off); |
1480 | off += v.iov_len; | |
1481 | } | |
c693cc46 | 1482 | next ? 0 : v.iov_len; |
a604ec7e AV |
1483 | }), ({ |
1484 | char *p = kmap_atomic(v.bv_page); | |
f9152895 AV |
1485 | sum = csum_and_memcpy((to += v.bv_len) - v.bv_len, |
1486 | p + v.bv_offset, v.bv_len, | |
1487 | sum, off); | |
a604ec7e | 1488 | kunmap_atomic(p); |
a604ec7e AV |
1489 | off += v.bv_len; |
1490 | }),({ | |
f9152895 AV |
1491 | sum = csum_and_memcpy((to += v.iov_len) - v.iov_len, |
1492 | v.iov_base, v.iov_len, | |
1493 | sum, off); | |
a604ec7e AV |
1494 | off += v.iov_len; |
1495 | }) | |
1496 | ) | |
1497 | *csum = sum; | |
1498 | return bytes; | |
1499 | } | |
1500 | EXPORT_SYMBOL(csum_and_copy_from_iter); | |
1501 | ||
cbbd26b8 AV |
1502 | bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, |
1503 | struct iov_iter *i) | |
1504 | { | |
1505 | char *to = addr; | |
1506 | __wsum sum, next; | |
1507 | size_t off = 0; | |
1508 | sum = *csum; | |
9ea9ce04 | 1509 | if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { |
cbbd26b8 AV |
1510 | WARN_ON(1); |
1511 | return false; | |
1512 | } | |
1513 | if (unlikely(i->count < bytes)) | |
1514 | return false; | |
1515 | iterate_all_kinds(i, bytes, v, ({ | |
cbbd26b8 AV |
1516 | next = csum_and_copy_from_user(v.iov_base, |
1517 | (to += v.iov_len) - v.iov_len, | |
c693cc46 AV |
1518 | v.iov_len); |
1519 | if (!next) | |
cbbd26b8 AV |
1520 | return false; |
1521 | sum = csum_block_add(sum, next, off); | |
1522 | off += v.iov_len; | |
1523 | 0; | |
1524 | }), ({ | |
1525 | char *p = kmap_atomic(v.bv_page); | |
f9152895 AV |
1526 | sum = csum_and_memcpy((to += v.bv_len) - v.bv_len, |
1527 | p + v.bv_offset, v.bv_len, | |
1528 | sum, off); | |
cbbd26b8 | 1529 | kunmap_atomic(p); |
cbbd26b8 AV |
1530 | off += v.bv_len; |
1531 | }),({ | |
f9152895 AV |
1532 | sum = csum_and_memcpy((to += v.iov_len) - v.iov_len, |
1533 | v.iov_base, v.iov_len, | |
1534 | sum, off); | |
cbbd26b8 AV |
1535 | off += v.iov_len; |
1536 | }) | |
1537 | ) | |
1538 | *csum = sum; | |
1539 | iov_iter_advance(i, bytes); | |
1540 | return true; | |
1541 | } | |
1542 | EXPORT_SYMBOL(csum_and_copy_from_iter_full); | |
1543 | ||
52cbd23a | 1544 | size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate, |
a604ec7e AV |
1545 | struct iov_iter *i) |
1546 | { | |
52cbd23a | 1547 | struct csum_state *csstate = _csstate; |
36f7a8a4 | 1548 | const char *from = addr; |
a604ec7e | 1549 | __wsum sum, next; |
52cbd23a | 1550 | size_t off; |
78e1f386 AV |
1551 | |
1552 | if (unlikely(iov_iter_is_pipe(i))) | |
52cbd23a | 1553 | return csum_and_copy_to_pipe_iter(addr, bytes, _csstate, i); |
78e1f386 | 1554 | |
52cbd23a WB |
1555 | sum = csstate->csum; |
1556 | off = csstate->off; | |
78e1f386 | 1557 | if (unlikely(iov_iter_is_discard(i))) { |
241699cd AV |
1558 | WARN_ON(1); /* for now */ |
1559 | return 0; | |
1560 | } | |
a604ec7e | 1561 | iterate_and_advance(i, bytes, v, ({ |
a604ec7e | 1562 | next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len, |
cbbd26b8 | 1563 | v.iov_base, |
c693cc46 AV |
1564 | v.iov_len); |
1565 | if (next) { | |
a604ec7e AV |
1566 | sum = csum_block_add(sum, next, off); |
1567 | off += v.iov_len; | |
1568 | } | |
c693cc46 | 1569 | next ? 0 : v.iov_len; |
a604ec7e AV |
1570 | }), ({ |
1571 | char *p = kmap_atomic(v.bv_page); | |
f9152895 AV |
1572 | sum = csum_and_memcpy(p + v.bv_offset, |
1573 | (from += v.bv_len) - v.bv_len, | |
1574 | v.bv_len, sum, off); | |
a604ec7e | 1575 | kunmap_atomic(p); |
a604ec7e AV |
1576 | off += v.bv_len; |
1577 | }),({ | |
f9152895 AV |
1578 | sum = csum_and_memcpy(v.iov_base, |
1579 | (from += v.iov_len) - v.iov_len, | |
1580 | v.iov_len, sum, off); | |
a604ec7e AV |
1581 | off += v.iov_len; |
1582 | }) | |
1583 | ) | |
52cbd23a WB |
1584 | csstate->csum = sum; |
1585 | csstate->off = off; | |
a604ec7e AV |
1586 | return bytes; |
1587 | } | |
1588 | EXPORT_SYMBOL(csum_and_copy_to_iter); | |
1589 | ||
d05f4435 SG |
1590 | size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, |
1591 | struct iov_iter *i) | |
1592 | { | |
7999096f | 1593 | #ifdef CONFIG_CRYPTO_HASH |
d05f4435 SG |
1594 | struct ahash_request *hash = hashp; |
1595 | struct scatterlist sg; | |
1596 | size_t copied; | |
1597 | ||
1598 | copied = copy_to_iter(addr, bytes, i); | |
1599 | sg_init_one(&sg, addr, copied); | |
1600 | ahash_request_set_crypt(hash, &sg, NULL, copied); | |
1601 | crypto_ahash_update(hash); | |
1602 | return copied; | |
27fad74a Y |
1603 | #else |
1604 | return 0; | |
1605 | #endif | |
d05f4435 SG |
1606 | } |
1607 | EXPORT_SYMBOL(hash_and_copy_to_iter); | |
1608 | ||
62a8067a AV |
1609 | int iov_iter_npages(const struct iov_iter *i, int maxpages) |
1610 | { | |
e0f2dc40 AV |
1611 | size_t size = i->count; |
1612 | int npages = 0; | |
1613 | ||
1614 | if (!size) | |
1615 | return 0; | |
9ea9ce04 DH |
1616 | if (unlikely(iov_iter_is_discard(i))) |
1617 | return 0; | |
e0f2dc40 | 1618 | |
00e23707 | 1619 | if (unlikely(iov_iter_is_pipe(i))) { |
241699cd | 1620 | struct pipe_inode_info *pipe = i->pipe; |
8cefc107 | 1621 | unsigned int iter_head; |
241699cd | 1622 | size_t off; |
241699cd AV |
1623 | |
1624 | if (!sanity(i)) | |
1625 | return 0; | |
1626 | ||
8cefc107 | 1627 | data_start(i, &iter_head, &off); |
241699cd | 1628 | /* some of this one + all after this one */ |
8cefc107 | 1629 | npages = pipe_space_for_user(iter_head, pipe->tail, pipe); |
241699cd AV |
1630 | if (npages >= maxpages) |
1631 | return maxpages; | |
1632 | } else iterate_all_kinds(i, size, v, ({ | |
e0f2dc40 AV |
1633 | unsigned long p = (unsigned long)v.iov_base; |
1634 | npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE) | |
1635 | - p / PAGE_SIZE; | |
1636 | if (npages >= maxpages) | |
1637 | return maxpages; | |
1638 | 0;}),({ | |
1639 | npages++; | |
1640 | if (npages >= maxpages) | |
1641 | return maxpages; | |
a280455f AV |
1642 | }),({ |
1643 | unsigned long p = (unsigned long)v.iov_base; | |
1644 | npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE) | |
1645 | - p / PAGE_SIZE; | |
1646 | if (npages >= maxpages) | |
1647 | return maxpages; | |
e0f2dc40 AV |
1648 | }) |
1649 | ) | |
1650 | return npages; | |
62a8067a | 1651 | } |
f67da30c | 1652 | EXPORT_SYMBOL(iov_iter_npages); |
4b8164b9 AV |
1653 | |
1654 | const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags) | |
1655 | { | |
1656 | *new = *old; | |
00e23707 | 1657 | if (unlikely(iov_iter_is_pipe(new))) { |
241699cd AV |
1658 | WARN_ON(1); |
1659 | return NULL; | |
1660 | } | |
9ea9ce04 DH |
1661 | if (unlikely(iov_iter_is_discard(new))) |
1662 | return NULL; | |
00e23707 | 1663 | if (iov_iter_is_bvec(new)) |
4b8164b9 AV |
1664 | return new->bvec = kmemdup(new->bvec, |
1665 | new->nr_segs * sizeof(struct bio_vec), | |
1666 | flags); | |
1667 | else | |
1668 | /* iovec and kvec have identical layout */ | |
1669 | return new->iov = kmemdup(new->iov, | |
1670 | new->nr_segs * sizeof(struct iovec), | |
1671 | flags); | |
1672 | } | |
1673 | EXPORT_SYMBOL(dup_iter); | |
bc917be8 | 1674 | |
bfdc5970 CH |
1675 | static int copy_compat_iovec_from_user(struct iovec *iov, |
1676 | const struct iovec __user *uvec, unsigned long nr_segs) | |
1677 | { | |
1678 | const struct compat_iovec __user *uiov = | |
1679 | (const struct compat_iovec __user *)uvec; | |
1680 | int ret = -EFAULT, i; | |
1681 | ||
a959a978 | 1682 | if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) |
bfdc5970 CH |
1683 | return -EFAULT; |
1684 | ||
1685 | for (i = 0; i < nr_segs; i++) { | |
1686 | compat_uptr_t buf; | |
1687 | compat_ssize_t len; | |
1688 | ||
1689 | unsafe_get_user(len, &uiov[i].iov_len, uaccess_end); | |
1690 | unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end); | |
1691 | ||
1692 | /* check for compat_size_t not fitting in compat_ssize_t .. */ | |
1693 | if (len < 0) { | |
1694 | ret = -EINVAL; | |
1695 | goto uaccess_end; | |
1696 | } | |
1697 | iov[i].iov_base = compat_ptr(buf); | |
1698 | iov[i].iov_len = len; | |
1699 | } | |
1700 | ||
1701 | ret = 0; | |
1702 | uaccess_end: | |
1703 | user_access_end(); | |
1704 | return ret; | |
1705 | } | |
1706 | ||
1707 | static int copy_iovec_from_user(struct iovec *iov, | |
1708 | const struct iovec __user *uvec, unsigned long nr_segs) | |
fb041b59 DL |
1709 | { |
1710 | unsigned long seg; | |
fb041b59 | 1711 | |
bfdc5970 CH |
1712 | if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec))) |
1713 | return -EFAULT; | |
1714 | for (seg = 0; seg < nr_segs; seg++) { | |
1715 | if ((ssize_t)iov[seg].iov_len < 0) | |
1716 | return -EINVAL; | |
fb041b59 DL |
1717 | } |
1718 | ||
bfdc5970 CH |
1719 | return 0; |
1720 | } | |
1721 | ||
1722 | struct iovec *iovec_from_user(const struct iovec __user *uvec, | |
1723 | unsigned long nr_segs, unsigned long fast_segs, | |
1724 | struct iovec *fast_iov, bool compat) | |
1725 | { | |
1726 | struct iovec *iov = fast_iov; | |
1727 | int ret; | |
1728 | ||
fb041b59 | 1729 | /* |
bfdc5970 CH |
1730 | * SuS says "The readv() function *may* fail if the iovcnt argument was |
1731 | * less than or equal to 0, or greater than {IOV_MAX}. Linux has | |
1732 | * traditionally returned zero for zero segments, so... | |
fb041b59 | 1733 | */ |
bfdc5970 CH |
1734 | if (nr_segs == 0) |
1735 | return iov; | |
1736 | if (nr_segs > UIO_MAXIOV) | |
1737 | return ERR_PTR(-EINVAL); | |
fb041b59 DL |
1738 | if (nr_segs > fast_segs) { |
1739 | iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); | |
bfdc5970 CH |
1740 | if (!iov) |
1741 | return ERR_PTR(-ENOMEM); | |
fb041b59 | 1742 | } |
bfdc5970 CH |
1743 | |
1744 | if (compat) | |
1745 | ret = copy_compat_iovec_from_user(iov, uvec, nr_segs); | |
1746 | else | |
1747 | ret = copy_iovec_from_user(iov, uvec, nr_segs); | |
1748 | if (ret) { | |
1749 | if (iov != fast_iov) | |
1750 | kfree(iov); | |
1751 | return ERR_PTR(ret); | |
1752 | } | |
1753 | ||
1754 | return iov; | |
1755 | } | |
1756 | ||
1757 | ssize_t __import_iovec(int type, const struct iovec __user *uvec, | |
1758 | unsigned nr_segs, unsigned fast_segs, struct iovec **iovp, | |
1759 | struct iov_iter *i, bool compat) | |
1760 | { | |
1761 | ssize_t total_len = 0; | |
1762 | unsigned long seg; | |
1763 | struct iovec *iov; | |
1764 | ||
1765 | iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat); | |
1766 | if (IS_ERR(iov)) { | |
1767 | *iovp = NULL; | |
1768 | return PTR_ERR(iov); | |
fb041b59 DL |
1769 | } |
1770 | ||
1771 | /* | |
bfdc5970 CH |
1772 | * According to the Single Unix Specification we should return EINVAL if |
1773 | * an element length is < 0 when cast to ssize_t or if the total length | |
1774 | * would overflow the ssize_t return value of the system call. | |
fb041b59 DL |
1775 | * |
1776 | * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the | |
1777 | * overflow case. | |
1778 | */ | |
fb041b59 | 1779 | for (seg = 0; seg < nr_segs; seg++) { |
fb041b59 DL |
1780 | ssize_t len = (ssize_t)iov[seg].iov_len; |
1781 | ||
bfdc5970 CH |
1782 | if (!access_ok(iov[seg].iov_base, len)) { |
1783 | if (iov != *iovp) | |
1784 | kfree(iov); | |
1785 | *iovp = NULL; | |
1786 | return -EFAULT; | |
fb041b59 | 1787 | } |
bfdc5970 CH |
1788 | |
1789 | if (len > MAX_RW_COUNT - total_len) { | |
1790 | len = MAX_RW_COUNT - total_len; | |
fb041b59 DL |
1791 | iov[seg].iov_len = len; |
1792 | } | |
bfdc5970 | 1793 | total_len += len; |
fb041b59 | 1794 | } |
bfdc5970 CH |
1795 | |
1796 | iov_iter_init(i, type, iov, nr_segs, total_len); | |
1797 | if (iov == *iovp) | |
1798 | *iovp = NULL; | |
1799 | else | |
1800 | *iovp = iov; | |
1801 | return total_len; | |
fb041b59 DL |
1802 | } |
1803 | ||
ffecee4f VN |
1804 | /** |
1805 | * import_iovec() - Copy an array of &struct iovec from userspace | |
1806 | * into the kernel, check that it is valid, and initialize a new | |
1807 | * &struct iov_iter iterator to access it. | |
1808 | * | |
1809 | * @type: One of %READ or %WRITE. | |
bfdc5970 | 1810 | * @uvec: Pointer to the userspace array. |
ffecee4f VN |
1811 | * @nr_segs: Number of elements in userspace array. |
1812 | * @fast_segs: Number of elements in @iov. | |
bfdc5970 | 1813 | * @iovp: (input and output parameter) Pointer to pointer to (usually small |
ffecee4f VN |
1814 | * on-stack) kernel array. |
1815 | * @i: Pointer to iterator that will be initialized on success. | |
1816 | * | |
1817 | * If the array pointed to by *@iov is large enough to hold all @nr_segs, | |
1818 | * then this function places %NULL in *@iov on return. Otherwise, a new | |
1819 | * array will be allocated and the result placed in *@iov. This means that | |
1820 | * the caller may call kfree() on *@iov regardless of whether the small | |
1821 | * on-stack array was used or not (and regardless of whether this function | |
1822 | * returns an error or not). | |
1823 | * | |
87e5e6da | 1824 | * Return: Negative error code on error, bytes imported on success |
ffecee4f | 1825 | */ |
bfdc5970 | 1826 | ssize_t import_iovec(int type, const struct iovec __user *uvec, |
bc917be8 | 1827 | unsigned nr_segs, unsigned fast_segs, |
bfdc5970 | 1828 | struct iovec **iovp, struct iov_iter *i) |
bc917be8 | 1829 | { |
89cd35c5 CH |
1830 | return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i, |
1831 | in_compat_syscall()); | |
bc917be8 AV |
1832 | } |
1833 | EXPORT_SYMBOL(import_iovec); | |
1834 | ||
bc917be8 AV |
1835 | int import_single_range(int rw, void __user *buf, size_t len, |
1836 | struct iovec *iov, struct iov_iter *i) | |
1837 | { | |
1838 | if (len > MAX_RW_COUNT) | |
1839 | len = MAX_RW_COUNT; | |
96d4f267 | 1840 | if (unlikely(!access_ok(buf, len))) |
bc917be8 AV |
1841 | return -EFAULT; |
1842 | ||
1843 | iov->iov_base = buf; | |
1844 | iov->iov_len = len; | |
1845 | iov_iter_init(i, rw, iov, 1, len); | |
1846 | return 0; | |
1847 | } | |
e1267585 | 1848 | EXPORT_SYMBOL(import_single_range); |
09cf698a AV |
1849 | |
1850 | int iov_iter_for_each_range(struct iov_iter *i, size_t bytes, | |
1851 | int (*f)(struct kvec *vec, void *context), | |
1852 | void *context) | |
1853 | { | |
1854 | struct kvec w; | |
1855 | int err = -EINVAL; | |
1856 | if (!bytes) | |
1857 | return 0; | |
1858 | ||
1859 | iterate_all_kinds(i, bytes, v, -EINVAL, ({ | |
1860 | w.iov_base = kmap(v.bv_page) + v.bv_offset; | |
1861 | w.iov_len = v.bv_len; | |
1862 | err = f(&w, context); | |
1863 | kunmap(v.bv_page); | |
1864 | err;}), ({ | |
1865 | w = v; | |
1866 | err = f(&w, context);}) | |
1867 | ) | |
1868 | return err; | |
1869 | } | |
1870 | EXPORT_SYMBOL(iov_iter_for_each_range); |