blob: 97e04c5dbeef280978265aa45cf106d4ec345091 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Herbert Xu79990962020-06-12 16:57:37 +10002#include <crypto/hash.h>
Al Viro4f18cd32014-02-05 19:11:33 -05003#include <linux/export.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -06004#include <linux/bvec.h>
Albert van der Linde4d0e9df2020-10-15 20:13:50 -07005#include <linux/fault-inject-usercopy.h>
Al Viro4f18cd32014-02-05 19:11:33 -05006#include <linux/uio.h>
7#include <linux/pagemap.h>
Ira Weiny28961992021-05-04 18:40:03 -07008#include <linux/highmem.h>
Al Viro91f79c42014-03-21 04:58:33 -04009#include <linux/slab.h>
10#include <linux/vmalloc.h>
Al Viro241699c2016-09-22 16:33:12 -040011#include <linux/splice.h>
Christoph Hellwigbfdc5972020-09-25 06:51:40 +020012#include <linux/compat.h>
Al Viroa604ec72014-11-24 01:08:00 -050013#include <net/checksum.h>
Sagi Grimbergd05f4432018-12-03 17:52:09 -080014#include <linux/scatterlist.h>
Marco Elverd0ef4c32020-01-21 17:05:11 +010015#include <linux/instrumented.h>
Al Viro4f18cd32014-02-05 19:11:33 -050016
Al Viro241699c2016-09-22 16:33:12 -040017#define PIPE_PARANOIA /* for now */
18
Al Viro5c67aa92021-04-25 23:57:42 -040019/* covers iovec and kvec alike */
Al Viroa6e4ec72021-05-02 13:03:41 -040020#define iterate_iovec(i, n, base, len, off, __p, STEP) { \
Al Viro7baa5092021-05-02 11:35:03 -040021 size_t off = 0; \
Al Viroa6e4ec72021-05-02 13:03:41 -040022 size_t skip = i->iov_offset; \
Al Viro7a1bcb52021-04-25 23:46:09 -040023 do { \
Al Viro7baa5092021-05-02 11:35:03 -040024 len = min(n, __p->iov_len - skip); \
25 if (likely(len)) { \
26 base = __p->iov_base + skip; \
27 len -= (STEP); \
28 off += len; \
29 skip += len; \
30 n -= len; \
Al Viro7a1bcb52021-04-25 23:46:09 -040031 if (skip < __p->iov_len) \
32 break; \
33 } \
34 __p++; \
35 skip = 0; \
36 } while (n); \
Al Viroa6e4ec72021-05-02 13:03:41 -040037 i->iov_offset = skip; \
Al Viro7baa5092021-05-02 11:35:03 -040038 n = off; \
Al Viro04a31162014-11-27 13:51:41 -050039}
40
Al Viroa6e4ec72021-05-02 13:03:41 -040041#define iterate_bvec(i, n, base, len, off, p, STEP) { \
Al Viro7baa5092021-05-02 11:35:03 -040042 size_t off = 0; \
Al Viroa6e4ec72021-05-02 13:03:41 -040043 unsigned skip = i->iov_offset; \
Al Viro7491a2b2021-04-26 20:19:14 -040044 while (n) { \
45 unsigned offset = p->bv_offset + skip; \
Al Viro1b4fb5f2021-04-26 20:33:42 -040046 unsigned left; \
Al Viro21b56c82021-04-26 20:50:05 -040047 void *kaddr = kmap_local_page(p->bv_page + \
48 offset / PAGE_SIZE); \
Al Viro7baa5092021-05-02 11:35:03 -040049 base = kaddr + offset % PAGE_SIZE; \
Al Viroa6e4ec72021-05-02 13:03:41 -040050 len = min(min(n, (size_t)(p->bv_len - skip)), \
Al Viro7491a2b2021-04-26 20:19:14 -040051 (size_t)(PAGE_SIZE - offset % PAGE_SIZE)); \
Al Viro1b4fb5f2021-04-26 20:33:42 -040052 left = (STEP); \
Al Viro21b56c82021-04-26 20:50:05 -040053 kunmap_local(kaddr); \
Al Viro7baa5092021-05-02 11:35:03 -040054 len -= left; \
55 off += len; \
56 skip += len; \
Al Viro7491a2b2021-04-26 20:19:14 -040057 if (skip == p->bv_len) { \
58 skip = 0; \
59 p++; \
60 } \
Al Viro7baa5092021-05-02 11:35:03 -040061 n -= len; \
Al Viro1b4fb5f2021-04-26 20:33:42 -040062 if (left) \
63 break; \
Al Viro7491a2b2021-04-26 20:19:14 -040064 } \
Al Viroa6e4ec72021-05-02 13:03:41 -040065 i->iov_offset = skip; \
Al Viro7baa5092021-05-02 11:35:03 -040066 n = off; \
Al Viro04a31162014-11-27 13:51:41 -050067}
68
Al Viroa6e4ec72021-05-02 13:03:41 -040069#define iterate_xarray(i, n, base, len, __off, STEP) { \
Al Viro1b4fb5f2021-04-26 20:33:42 -040070 __label__ __out; \
Al Viro622838f2021-05-02 11:13:09 -040071 size_t __off = 0; \
David Howells7ff5062072020-02-10 10:00:21 +000072 struct page *head = NULL; \
Al Viroa6e4ec72021-05-02 13:03:41 -040073 loff_t start = i->xarray_start + i->iov_offset; \
Al Viro4b179e92021-05-04 17:50:07 -040074 unsigned offset = start % PAGE_SIZE; \
75 pgoff_t index = start / PAGE_SIZE; \
David Howells7ff5062072020-02-10 10:00:21 +000076 int j; \
77 \
78 XA_STATE(xas, i->xarray, index); \
79 \
Al Viro7baa5092021-05-02 11:35:03 -040080 rcu_read_lock(); \
81 xas_for_each(&xas, head, ULONG_MAX) { \
82 unsigned left; \
83 if (xas_retry(&xas, head)) \
84 continue; \
85 if (WARN_ON(xa_is_value(head))) \
86 break; \
87 if (WARN_ON(PageHuge(head))) \
88 break; \
David Howells7ff5062072020-02-10 10:00:21 +000089 for (j = (head->index < index) ? index - head->index : 0; \
Al Viro7baa5092021-05-02 11:35:03 -040090 j < thp_nr_pages(head); j++) { \
Al Viro21b56c82021-04-26 20:50:05 -040091 void *kaddr = kmap_local_page(head + j); \
Al Viro7baa5092021-05-02 11:35:03 -040092 base = kaddr + offset; \
93 len = PAGE_SIZE - offset; \
94 len = min(n, len); \
95 left = (STEP); \
96 kunmap_local(kaddr); \
97 len -= left; \
98 __off += len; \
99 n -= len; \
100 if (left || n == 0) \
101 goto __out; \
Al Viro4b179e92021-05-04 17:50:07 -0400102 offset = 0; \
Al Viro7baa5092021-05-02 11:35:03 -0400103 } \
David Howells7ff5062072020-02-10 10:00:21 +0000104 } \
Al Viro1b4fb5f2021-04-26 20:33:42 -0400105__out: \
David Howells7ff5062072020-02-10 10:00:21 +0000106 rcu_read_unlock(); \
Al Viroa6e4ec72021-05-02 13:03:41 -0400107 i->iov_offset += __off; \
Al Viro622838f2021-05-02 11:13:09 -0400108 n = __off; \
David Howells7ff5062072020-02-10 10:00:21 +0000109}
110
Al Viro7baa5092021-05-02 11:35:03 -0400111#define __iterate_and_advance(i, n, base, len, off, I, K) { \
Al Virodd254f52016-05-09 11:54:48 -0400112 if (unlikely(i->count < n)) \
113 n = i->count; \
Al Virof5da8352021-04-28 20:59:08 -0400114 if (likely(n)) { \
Al Viro28f38db2021-06-02 17:25:59 -0400115 if (likely(iter_is_iovec(i))) { \
Al Viro5c67aa92021-04-25 23:57:42 -0400116 const struct iovec *iov = i->iov; \
Al Viro7baa5092021-05-02 11:35:03 -0400117 void __user *base; \
118 size_t len; \
119 iterate_iovec(i, n, base, len, off, \
Al Viroa6e4ec72021-05-02 13:03:41 -0400120 iov, (I)) \
Al Virodd254f52016-05-09 11:54:48 -0400121 i->nr_segs -= iov - i->iov; \
122 i->iov = iov; \
Al Viro28f38db2021-06-02 17:25:59 -0400123 } else if (iov_iter_is_bvec(i)) { \
124 const struct bio_vec *bvec = i->bvec; \
Al Viro7baa5092021-05-02 11:35:03 -0400125 void *base; \
126 size_t len; \
127 iterate_bvec(i, n, base, len, off, \
Al Viroa6e4ec72021-05-02 13:03:41 -0400128 bvec, (K)) \
Al Viro7491a2b2021-04-26 20:19:14 -0400129 i->nr_segs -= bvec - i->bvec; \
130 i->bvec = bvec; \
Al Viro28f38db2021-06-02 17:25:59 -0400131 } else if (iov_iter_is_kvec(i)) { \
Al Viro5c67aa92021-04-25 23:57:42 -0400132 const struct kvec *kvec = i->kvec; \
Al Viro7baa5092021-05-02 11:35:03 -0400133 void *base; \
134 size_t len; \
135 iterate_iovec(i, n, base, len, off, \
Al Viroa6e4ec72021-05-02 13:03:41 -0400136 kvec, (K)) \
Al Viro28f38db2021-06-02 17:25:59 -0400137 i->nr_segs -= kvec - i->kvec; \
138 i->kvec = kvec; \
139 } else if (iov_iter_is_xarray(i)) { \
Al Viro7baa5092021-05-02 11:35:03 -0400140 void *base; \
141 size_t len; \
142 iterate_xarray(i, n, base, len, off, \
Al Viroa6e4ec72021-05-02 13:03:41 -0400143 (K)) \
Al Viro7ce2a912014-11-27 13:59:45 -0500144 } \
Al Virodd254f52016-05-09 11:54:48 -0400145 i->count -= n; \
Al Viro7ce2a912014-11-27 13:59:45 -0500146 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500147}
Al Viro7baa5092021-05-02 11:35:03 -0400148#define iterate_and_advance(i, n, base, len, off, I, K) \
149 __iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
Al Viro7ce2a912014-11-27 13:59:45 -0500150
Al Viro09fc68dc2017-06-29 22:25:14 -0400151static int copyout(void __user *to, const void *from, size_t n)
152{
Albert van der Linde4d0e9df2020-10-15 20:13:50 -0700153 if (should_fail_usercopy())
154 return n;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800155 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100156 instrument_copy_to_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400157 n = raw_copy_to_user(to, from, n);
158 }
159 return n;
160}
161
162static int copyin(void *to, const void __user *from, size_t n)
163{
Albert van der Linde4d0e9df2020-10-15 20:13:50 -0700164 if (should_fail_usercopy())
165 return n;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800166 if (access_ok(from, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100167 instrument_copy_from_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400168 n = raw_copy_from_user(to, from, n);
169 }
170 return n;
171}
172
Al Viro62a80672014-04-04 23:12:29 -0400173static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500174 struct iov_iter *i)
175{
176 size_t skip, copy, left, wanted;
177 const struct iovec *iov;
178 char __user *buf;
179 void *kaddr, *from;
180
181 if (unlikely(bytes > i->count))
182 bytes = i->count;
183
184 if (unlikely(!bytes))
185 return 0;
186
Al Viro09fc68dc2017-06-29 22:25:14 -0400187 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500188 wanted = bytes;
189 iov = i->iov;
190 skip = i->iov_offset;
191 buf = iov->iov_base + skip;
192 copy = min(bytes, iov->iov_len - skip);
193
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700194 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500195 kaddr = kmap_atomic(page);
196 from = kaddr + offset;
197
198 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400199 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500200 copy -= left;
201 skip += copy;
202 from += copy;
203 bytes -= copy;
204
205 while (unlikely(!left && bytes)) {
206 iov++;
207 buf = iov->iov_base;
208 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400209 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500210 copy -= left;
211 skip = copy;
212 from += copy;
213 bytes -= copy;
214 }
215 if (likely(!bytes)) {
216 kunmap_atomic(kaddr);
217 goto done;
218 }
219 offset = from - kaddr;
220 buf += copy;
221 kunmap_atomic(kaddr);
222 copy = min(bytes, iov->iov_len - skip);
223 }
224 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700225
Al Viro4f18cd32014-02-05 19:11:33 -0500226 kaddr = kmap(page);
227 from = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400228 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500229 copy -= left;
230 skip += copy;
231 from += copy;
232 bytes -= copy;
233 while (unlikely(!left && bytes)) {
234 iov++;
235 buf = iov->iov_base;
236 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400237 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500238 copy -= left;
239 skip = copy;
240 from += copy;
241 bytes -= copy;
242 }
243 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700244
Al Viro4f18cd32014-02-05 19:11:33 -0500245done:
Al Viro81055e52014-04-04 19:23:46 -0400246 if (skip == iov->iov_len) {
247 iov++;
248 skip = 0;
249 }
Al Viro4f18cd32014-02-05 19:11:33 -0500250 i->count -= wanted - bytes;
251 i->nr_segs -= iov - i->iov;
252 i->iov = iov;
253 i->iov_offset = skip;
254 return wanted - bytes;
255}
Al Viro4f18cd32014-02-05 19:11:33 -0500256
Al Viro62a80672014-04-04 23:12:29 -0400257static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400258 struct iov_iter *i)
259{
260 size_t skip, copy, left, wanted;
261 const struct iovec *iov;
262 char __user *buf;
263 void *kaddr, *to;
264
265 if (unlikely(bytes > i->count))
266 bytes = i->count;
267
268 if (unlikely(!bytes))
269 return 0;
270
Al Viro09fc68dc2017-06-29 22:25:14 -0400271 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400272 wanted = bytes;
273 iov = i->iov;
274 skip = i->iov_offset;
275 buf = iov->iov_base + skip;
276 copy = min(bytes, iov->iov_len - skip);
277
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700278 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400279 kaddr = kmap_atomic(page);
280 to = kaddr + offset;
281
282 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400283 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400284 copy -= left;
285 skip += copy;
286 to += copy;
287 bytes -= copy;
288
289 while (unlikely(!left && bytes)) {
290 iov++;
291 buf = iov->iov_base;
292 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400293 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400294 copy -= left;
295 skip = copy;
296 to += copy;
297 bytes -= copy;
298 }
299 if (likely(!bytes)) {
300 kunmap_atomic(kaddr);
301 goto done;
302 }
303 offset = to - kaddr;
304 buf += copy;
305 kunmap_atomic(kaddr);
306 copy = min(bytes, iov->iov_len - skip);
307 }
308 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700309
Al Virof0d1bec2014-04-03 15:05:18 -0400310 kaddr = kmap(page);
311 to = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400312 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400313 copy -= left;
314 skip += copy;
315 to += copy;
316 bytes -= copy;
317 while (unlikely(!left && bytes)) {
318 iov++;
319 buf = iov->iov_base;
320 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400321 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400322 copy -= left;
323 skip = copy;
324 to += copy;
325 bytes -= copy;
326 }
327 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700328
Al Virof0d1bec2014-04-03 15:05:18 -0400329done:
Al Viro81055e52014-04-04 19:23:46 -0400330 if (skip == iov->iov_len) {
331 iov++;
332 skip = 0;
333 }
Al Virof0d1bec2014-04-03 15:05:18 -0400334 i->count -= wanted - bytes;
335 i->nr_segs -= iov - i->iov;
336 i->iov = iov;
337 i->iov_offset = skip;
338 return wanted - bytes;
339}
Al Virof0d1bec2014-04-03 15:05:18 -0400340
Al Viro241699c2016-09-22 16:33:12 -0400341#ifdef PIPE_PARANOIA
342static bool sanity(const struct iov_iter *i)
343{
344 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000345 unsigned int p_head = pipe->head;
346 unsigned int p_tail = pipe->tail;
347 unsigned int p_mask = pipe->ring_size - 1;
348 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
349 unsigned int i_head = i->head;
350 unsigned int idx;
351
Al Viro241699c2016-09-22 16:33:12 -0400352 if (i->iov_offset) {
353 struct pipe_buffer *p;
David Howells8cefc102019-11-15 13:30:32 +0000354 if (unlikely(p_occupancy == 0))
Al Viro241699c2016-09-22 16:33:12 -0400355 goto Bad; // pipe must be non-empty
David Howells8cefc102019-11-15 13:30:32 +0000356 if (unlikely(i_head != p_head - 1))
Al Viro241699c2016-09-22 16:33:12 -0400357 goto Bad; // must be at the last buffer...
358
David Howells8cefc102019-11-15 13:30:32 +0000359 p = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400360 if (unlikely(p->offset + p->len != i->iov_offset))
361 goto Bad; // ... at the end of segment
362 } else {
David Howells8cefc102019-11-15 13:30:32 +0000363 if (i_head != p_head)
Al Viro241699c2016-09-22 16:33:12 -0400364 goto Bad; // must be right after the last buffer
365 }
366 return true;
367Bad:
David Howells8cefc102019-11-15 13:30:32 +0000368 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
369 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
370 p_head, p_tail, pipe->ring_size);
371 for (idx = 0; idx < pipe->ring_size; idx++)
Al Viro241699c2016-09-22 16:33:12 -0400372 printk(KERN_ERR "[%p %p %d %d]\n",
373 pipe->bufs[idx].ops,
374 pipe->bufs[idx].page,
375 pipe->bufs[idx].offset,
376 pipe->bufs[idx].len);
377 WARN_ON(1);
378 return false;
379}
380#else
381#define sanity(i) true
382#endif
383
Al Viro241699c2016-09-22 16:33:12 -0400384static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
385 struct iov_iter *i)
386{
387 struct pipe_inode_info *pipe = i->pipe;
388 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +0000389 unsigned int p_tail = pipe->tail;
390 unsigned int p_mask = pipe->ring_size - 1;
391 unsigned int i_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400392 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400393
394 if (unlikely(bytes > i->count))
395 bytes = i->count;
396
397 if (unlikely(!bytes))
398 return 0;
399
400 if (!sanity(i))
401 return 0;
402
403 off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000404 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400405 if (off) {
406 if (offset == off && buf->page == page) {
407 /* merge with the last one */
408 buf->len += bytes;
409 i->iov_offset += bytes;
410 goto out;
411 }
David Howells8cefc102019-11-15 13:30:32 +0000412 i_head++;
413 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400414 }
David Howells6718b6f2019-10-16 16:47:32 +0100415 if (pipe_full(i_head, p_tail, pipe->max_usage))
Al Viro241699c2016-09-22 16:33:12 -0400416 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000417
Al Viro241699c2016-09-22 16:33:12 -0400418 buf->ops = &page_cache_pipe_buf_ops;
David Howells8cefc102019-11-15 13:30:32 +0000419 get_page(page);
420 buf->page = page;
Al Viro241699c2016-09-22 16:33:12 -0400421 buf->offset = offset;
422 buf->len = bytes;
David Howells8cefc102019-11-15 13:30:32 +0000423
424 pipe->head = i_head + 1;
Al Viro241699c2016-09-22 16:33:12 -0400425 i->iov_offset = offset + bytes;
David Howells8cefc102019-11-15 13:30:32 +0000426 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400427out:
428 i->count -= bytes;
429 return bytes;
430}
431
Al Viro4f18cd32014-02-05 19:11:33 -0500432/*
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400433 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
434 * bytes. For each iovec, fault in each page that constitutes the iovec.
435 *
436 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
437 * because it is an invalid address).
438 */
Al Viro8409a0d2021-05-02 11:57:37 -0400439int iov_iter_fault_in_readable(const struct iov_iter *i, size_t bytes)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400440{
Al Viro0e8f0d62021-06-02 14:48:21 -0400441 if (iter_is_iovec(i)) {
Al Viro8409a0d2021-05-02 11:57:37 -0400442 const struct iovec *p;
443 size_t skip;
444
445 if (bytes > i->count)
446 bytes = i->count;
447 for (p = i->iov, skip = i->iov_offset; bytes; p++, skip = 0) {
448 size_t len = min(bytes, p->iov_len - skip);
449 int err;
450
451 if (unlikely(!len))
452 continue;
453 err = fault_in_pages_readable(p->iov_base + skip, len);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400454 if (unlikely(err))
Al Viro8409a0d2021-05-02 11:57:37 -0400455 return err;
456 bytes -= len;
457 }
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400458 }
459 return 0;
460}
Al Virod4690f12016-09-16 00:11:45 +0100461EXPORT_SYMBOL(iov_iter_fault_in_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400462
David Howellsaa563d72018-10-20 00:57:56 +0100463void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500464 const struct iovec *iov, unsigned long nr_segs,
465 size_t count)
466{
David Howellsaa563d72018-10-20 00:57:56 +0100467 WARN_ON(direction & ~(READ | WRITE));
Al Viro8cd54c12021-04-22 14:50:39 -0400468 WARN_ON_ONCE(uaccess_kernel());
469 *i = (struct iov_iter) {
470 .iter_type = ITER_IOVEC,
471 .data_source = direction,
472 .iov = iov,
473 .nr_segs = nr_segs,
474 .iov_offset = 0,
475 .count = count
476 };
Al Viro71d8e532014-03-05 19:28:09 -0500477}
478EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400479
Al Viro241699c2016-09-22 16:33:12 -0400480static inline bool allocated(struct pipe_buffer *buf)
481{
482 return buf->ops == &default_pipe_buf_ops;
483}
484
David Howells8cefc102019-11-15 13:30:32 +0000485static inline void data_start(const struct iov_iter *i,
486 unsigned int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400487{
David Howells8cefc102019-11-15 13:30:32 +0000488 unsigned int p_mask = i->pipe->ring_size - 1;
489 unsigned int iter_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400490 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000491
492 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
493 off == PAGE_SIZE)) {
494 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400495 off = 0;
496 }
David Howells8cefc102019-11-15 13:30:32 +0000497 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400498 *offp = off;
499}
500
501static size_t push_pipe(struct iov_iter *i, size_t size,
David Howells8cefc102019-11-15 13:30:32 +0000502 int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400503{
504 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000505 unsigned int p_tail = pipe->tail;
506 unsigned int p_mask = pipe->ring_size - 1;
507 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400508 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400509 ssize_t left;
510
511 if (unlikely(size > i->count))
512 size = i->count;
513 if (unlikely(!size))
514 return 0;
515
516 left = size;
David Howells8cefc102019-11-15 13:30:32 +0000517 data_start(i, &iter_head, &off);
518 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400519 *offp = off;
520 if (off) {
521 left -= PAGE_SIZE - off;
522 if (left <= 0) {
David Howells8cefc102019-11-15 13:30:32 +0000523 pipe->bufs[iter_head & p_mask].len += size;
Al Viro241699c2016-09-22 16:33:12 -0400524 return size;
525 }
David Howells8cefc102019-11-15 13:30:32 +0000526 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
527 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400528 }
David Howells6718b6f2019-10-16 16:47:32 +0100529 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000530 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400531 struct page *page = alloc_page(GFP_USER);
532 if (!page)
533 break;
David Howells8cefc102019-11-15 13:30:32 +0000534
535 buf->ops = &default_pipe_buf_ops;
536 buf->page = page;
537 buf->offset = 0;
538 buf->len = min_t(ssize_t, left, PAGE_SIZE);
539 left -= buf->len;
540 iter_head++;
541 pipe->head = iter_head;
542
543 if (left == 0)
Al Viro241699c2016-09-22 16:33:12 -0400544 return size;
Al Viro241699c2016-09-22 16:33:12 -0400545 }
546 return size - left;
547}
548
549static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
550 struct iov_iter *i)
551{
552 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000553 unsigned int p_mask = pipe->ring_size - 1;
554 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400555 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400556
557 if (!sanity(i))
558 return 0;
559
David Howells8cefc102019-11-15 13:30:32 +0000560 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400561 if (unlikely(!n))
562 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000563 do {
Al Viro241699c2016-09-22 16:33:12 -0400564 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000565 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
566 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400567 i->iov_offset = off + chunk;
568 n -= chunk;
569 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000570 off = 0;
571 i_head++;
572 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400573 i->count -= bytes;
574 return bytes;
575}
576
Al Virof9152892018-11-27 22:32:59 -0500577static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
578 __wsum sum, size_t off)
579{
Al Virocc44c172020-07-11 00:12:07 -0400580 __wsum next = csum_partial_copy_nocheck(from, to, len);
Al Virof9152892018-11-27 22:32:59 -0500581 return csum_block_add(sum, next, off);
582}
583
Al Viro78e1f382018-11-25 16:24:16 -0500584static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
Al Viro6852df12021-05-02 17:24:40 -0400585 struct iov_iter *i, __wsum *sump)
Al Viro78e1f382018-11-25 16:24:16 -0500586{
587 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000588 unsigned int p_mask = pipe->ring_size - 1;
Al Viro6852df12021-05-02 17:24:40 -0400589 __wsum sum = *sump;
590 size_t off = 0;
David Howells8cefc102019-11-15 13:30:32 +0000591 unsigned int i_head;
Al Viro6852df12021-05-02 17:24:40 -0400592 size_t r;
Al Viro78e1f382018-11-25 16:24:16 -0500593
594 if (!sanity(i))
595 return 0;
596
Al Viro6852df12021-05-02 17:24:40 -0400597 bytes = push_pipe(i, bytes, &i_head, &r);
598 while (bytes) {
599 size_t chunk = min_t(size_t, bytes, PAGE_SIZE - r);
Al Viro2495bdcc2021-04-30 13:40:48 -0400600 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
Al Viro6852df12021-05-02 17:24:40 -0400601 sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
Al Viro2495bdcc2021-04-30 13:40:48 -0400602 kunmap_local(p);
David Howells8cefc102019-11-15 13:30:32 +0000603 i->head = i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500604 i->iov_offset = r + chunk;
Al Viro6852df12021-05-02 17:24:40 -0400605 bytes -= chunk;
Al Viro78e1f382018-11-25 16:24:16 -0500606 off += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000607 r = 0;
608 i_head++;
Al Viro6852df12021-05-02 17:24:40 -0400609 }
610 *sump = sum;
611 i->count -= off;
612 return off;
Al Viro78e1f382018-11-25 16:24:16 -0500613}
614
Al Viroaa28de22017-06-29 21:45:10 -0400615size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400616{
David Howells00e23702018-10-22 13:07:28 +0100617 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400618 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400619 if (iter_is_iovec(i))
620 might_fault();
Al Viro7baa5092021-05-02 11:35:03 -0400621 iterate_and_advance(i, bytes, base, len, off,
622 copyout(base, addr + off, len),
623 memcpy(base, addr + off, len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500624 )
Al Viro62a80672014-04-04 23:12:29 -0400625
Al Viro3d4d3e42014-11-27 14:28:06 -0500626 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400627}
Al Viroaa28de22017-06-29 21:45:10 -0400628EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400629
Dan Williamsec6347b2020-10-05 20:40:16 -0700630#ifdef CONFIG_ARCH_HAS_COPY_MC
631static int copyout_mc(void __user *to, const void *from, size_t n)
Dan Williams87803562018-05-03 17:06:31 -0700632{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800633 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100634 instrument_copy_to_user(to, from, n);
Dan Williamsec6347b2020-10-05 20:40:16 -0700635 n = copy_mc_to_user((__force void *) to, from, n);
Dan Williams87803562018-05-03 17:06:31 -0700636 }
637 return n;
638}
639
Dan Williamsec6347b2020-10-05 20:40:16 -0700640static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
Dan Williamsca146f62018-07-08 13:46:12 -0700641 struct iov_iter *i)
642{
643 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000644 unsigned int p_mask = pipe->ring_size - 1;
645 unsigned int i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700646 size_t n, off, xfer = 0;
Dan Williamsca146f62018-07-08 13:46:12 -0700647
648 if (!sanity(i))
649 return 0;
650
Al Viro2a510a72021-05-02 17:16:34 -0400651 n = push_pipe(i, bytes, &i_head, &off);
652 while (n) {
Dan Williamsca146f62018-07-08 13:46:12 -0700653 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
Al Viro2a510a72021-05-02 17:16:34 -0400654 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
Dan Williamsca146f62018-07-08 13:46:12 -0700655 unsigned long rem;
Al Viro2a510a72021-05-02 17:16:34 -0400656 rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
657 chunk -= rem;
658 kunmap_local(p);
David Howells8cefc102019-11-15 13:30:32 +0000659 i->head = i_head;
Al Viro2a510a72021-05-02 17:16:34 -0400660 i->iov_offset = off + chunk;
661 xfer += chunk;
Dan Williamsca146f62018-07-08 13:46:12 -0700662 if (rem)
663 break;
664 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000665 off = 0;
666 i_head++;
Al Viro2a510a72021-05-02 17:16:34 -0400667 }
Dan Williamsca146f62018-07-08 13:46:12 -0700668 i->count -= xfer;
669 return xfer;
670}
671
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700672/**
Dan Williamsec6347b2020-10-05 20:40:16 -0700673 * _copy_mc_to_iter - copy to iter with source memory error exception handling
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700674 * @addr: source kernel address
675 * @bytes: total transfer length
676 * @iter: destination iterator
677 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700678 * The pmem driver deploys this for the dax operation
679 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
680 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
681 * successfully copied.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700682 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700683 * The main differences between this and typical _copy_to_iter().
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700684 *
685 * * Typical tail/residue handling after a fault retries the copy
686 * byte-by-byte until the fault happens again. Re-triggering machine
687 * checks is potentially fatal so the implementation uses source
688 * alignment and poison alignment assumptions to avoid re-triggering
689 * hardware exceptions.
690 *
691 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
692 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
693 * a short copy.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700694 */
Dan Williamsec6347b2020-10-05 20:40:16 -0700695size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Dan Williams87803562018-05-03 17:06:31 -0700696{
David Howells00e23702018-10-22 13:07:28 +0100697 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsec6347b2020-10-05 20:40:16 -0700698 return copy_mc_pipe_to_iter(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700699 if (iter_is_iovec(i))
700 might_fault();
Al Viro7baa5092021-05-02 11:35:03 -0400701 __iterate_and_advance(i, bytes, base, len, off,
702 copyout_mc(base, addr + off, len),
703 copy_mc_to_kernel(base, addr + off, len)
Dan Williams87803562018-05-03 17:06:31 -0700704 )
705
706 return bytes;
707}
Dan Williamsec6347b2020-10-05 20:40:16 -0700708EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
709#endif /* CONFIG_ARCH_HAS_COPY_MC */
Dan Williams87803562018-05-03 17:06:31 -0700710
Al Viroaa28de22017-06-29 21:45:10 -0400711size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400712{
David Howells00e23702018-10-22 13:07:28 +0100713 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400714 WARN_ON(1);
715 return 0;
716 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400717 if (iter_is_iovec(i))
718 might_fault();
Al Viro7baa5092021-05-02 11:35:03 -0400719 iterate_and_advance(i, bytes, base, len, off,
720 copyin(addr + off, base, len),
721 memcpy(addr + off, base, len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500722 )
723
724 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400725}
Al Viroaa28de22017-06-29 21:45:10 -0400726EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400727
Al Viroaa28de22017-06-29 21:45:10 -0400728size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500729{
David Howells00e23702018-10-22 13:07:28 +0100730 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400731 WARN_ON(1);
732 return 0;
733 }
Al Viro7baa5092021-05-02 11:35:03 -0400734 iterate_and_advance(i, bytes, base, len, off,
735 __copy_from_user_inatomic_nocache(addr + off, base, len),
736 memcpy(addr + off, base, len)
Al Viroaa583092014-11-27 20:27:08 -0500737 )
738
739 return bytes;
740}
Al Viroaa28de22017-06-29 21:45:10 -0400741EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500742
Dan Williams0aed55a2017-05-29 12:22:50 -0700743#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700744/**
745 * _copy_from_iter_flushcache - write destination through cpu cache
746 * @addr: destination kernel address
747 * @bytes: total transfer length
748 * @iter: source iterator
749 *
750 * The pmem driver arranges for filesystem-dax to use this facility via
751 * dax_copy_from_iter() for ensuring that writes to persistent memory
752 * are flushed through the CPU cache. It is differentiated from
753 * _copy_from_iter_nocache() in that guarantees all data is flushed for
754 * all iterator types. The _copy_from_iter_nocache() only attempts to
755 * bypass the cache for the ITER_IOVEC case, and on some archs may use
756 * instructions that strand dirty-data in the cache.
757 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700758size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700759{
David Howells00e23702018-10-22 13:07:28 +0100760 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700761 WARN_ON(1);
762 return 0;
763 }
Al Viro7baa5092021-05-02 11:35:03 -0400764 iterate_and_advance(i, bytes, base, len, off,
765 __copy_from_user_flushcache(addr + off, base, len),
766 memcpy_flushcache(addr + off, base, len)
Dan Williams0aed55a2017-05-29 12:22:50 -0700767 )
768
769 return bytes;
770}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700771EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700772#endif
773
Al Viro72e809e2017-06-29 21:52:57 -0400774static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
775{
Eric Dumazet6daef952019-02-26 10:42:39 -0800776 struct page *head;
777 size_t v = n + offset;
778
779 /*
780 * The general case needs to access the page order in order
781 * to compute the page size.
782 * However, we mostly deal with order-0 pages and thus can
783 * avoid a possible cache line miss for requests that fit all
784 * page orders.
785 */
786 if (n <= v && v <= PAGE_SIZE)
787 return true;
788
789 head = compound_head(page);
790 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700791
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700792 if (likely(n <= v && v <= (page_size(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400793 return true;
794 WARN_ON(1);
795 return false;
796}
Al Virocbbd26b2016-11-01 22:09:04 -0400797
Al Viro08aa6472021-04-29 20:42:25 -0400798static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
Al Virod2715242014-11-27 14:22:37 -0500799 struct iov_iter *i)
800{
Al Viro28f38db2021-06-02 17:25:59 -0400801 if (likely(iter_is_iovec(i)))
802 return copy_page_to_iter_iovec(page, offset, bytes, i);
803 if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
Al Viroc1d4d6a2021-04-27 12:29:53 -0400804 void *kaddr = kmap_local_page(page);
805 size_t wanted = _copy_to_iter(kaddr + offset, bytes, i);
806 kunmap_local(kaddr);
Al Virod2715242014-11-27 14:22:37 -0500807 return wanted;
Al Viro28f38db2021-06-02 17:25:59 -0400808 }
809 if (iov_iter_is_pipe(i))
810 return copy_page_to_iter_pipe(page, offset, bytes, i);
811 if (unlikely(iov_iter_is_discard(i))) {
Al Viroa506abc2021-04-27 12:34:04 -0400812 if (unlikely(i->count < bytes))
813 bytes = i->count;
814 i->count -= bytes;
David Howells9ea9ce02018-10-20 00:57:56 +0100815 return bytes;
Al Viro28f38db2021-06-02 17:25:59 -0400816 }
817 WARN_ON(1);
818 return 0;
Al Virod2715242014-11-27 14:22:37 -0500819}
Al Viro08aa6472021-04-29 20:42:25 -0400820
821size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
822 struct iov_iter *i)
823{
824 size_t res = 0;
825 if (unlikely(!page_copy_sane(page, offset, bytes)))
826 return 0;
827 page += offset / PAGE_SIZE; // first subpage
828 offset %= PAGE_SIZE;
829 while (1) {
830 size_t n = __copy_page_to_iter(page, offset,
831 min(bytes, (size_t)PAGE_SIZE - offset), i);
832 res += n;
833 bytes -= n;
834 if (!bytes || !n)
835 break;
836 offset += n;
837 if (offset == PAGE_SIZE) {
838 page++;
839 offset = 0;
840 }
841 }
842 return res;
843}
Al Virod2715242014-11-27 14:22:37 -0500844EXPORT_SYMBOL(copy_page_to_iter);
845
846size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
847 struct iov_iter *i)
848{
Al Viro72e809e2017-06-29 21:52:57 -0400849 if (unlikely(!page_copy_sane(page, offset, bytes)))
850 return 0;
Al Viro28f38db2021-06-02 17:25:59 -0400851 if (likely(iter_is_iovec(i)))
852 return copy_page_from_iter_iovec(page, offset, bytes, i);
853 if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
Al Viro55ca3752021-04-27 12:33:24 -0400854 void *kaddr = kmap_local_page(page);
Al Viroaa28de22017-06-29 21:45:10 -0400855 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Viro55ca3752021-04-27 12:33:24 -0400856 kunmap_local(kaddr);
Al Virod2715242014-11-27 14:22:37 -0500857 return wanted;
Al Viro28f38db2021-06-02 17:25:59 -0400858 }
859 WARN_ON(1);
860 return 0;
Al Virod2715242014-11-27 14:22:37 -0500861}
862EXPORT_SYMBOL(copy_page_from_iter);
863
Al Viro241699c2016-09-22 16:33:12 -0400864static size_t pipe_zero(size_t bytes, struct iov_iter *i)
865{
866 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000867 unsigned int p_mask = pipe->ring_size - 1;
868 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400869 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400870
871 if (!sanity(i))
872 return 0;
873
David Howells8cefc102019-11-15 13:30:32 +0000874 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400875 if (unlikely(!n))
876 return 0;
877
David Howells8cefc102019-11-15 13:30:32 +0000878 do {
Al Viro241699c2016-09-22 16:33:12 -0400879 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
Al Viro893839f2021-04-30 18:39:25 -0400880 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
881 memset(p + off, 0, chunk);
882 kunmap_local(p);
David Howells8cefc102019-11-15 13:30:32 +0000883 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400884 i->iov_offset = off + chunk;
885 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000886 off = 0;
887 i_head++;
888 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400889 i->count -= bytes;
890 return bytes;
891}
892
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400893size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
894{
David Howells00e23702018-10-22 13:07:28 +0100895 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400896 return pipe_zero(bytes, i);
Al Viro7baa5092021-05-02 11:35:03 -0400897 iterate_and_advance(i, bytes, base, len, count,
898 clear_user(base, len),
899 memset(base, 0, len)
Al Viro8442fa42014-11-27 14:18:54 -0500900 )
901
902 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400903}
904EXPORT_SYMBOL(iov_iter_zero);
905
Al Virof0b65f32021-04-30 10:26:41 -0400906size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
907 struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400908{
Al Viro04a31162014-11-27 13:51:41 -0500909 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400910 if (unlikely(!page_copy_sane(page, offset, bytes))) {
911 kunmap_atomic(kaddr);
912 return 0;
913 }
David Howells9ea9ce02018-10-20 00:57:56 +0100914 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400915 kunmap_atomic(kaddr);
916 WARN_ON(1);
917 return 0;
918 }
Al Viro7baa5092021-05-02 11:35:03 -0400919 iterate_and_advance(i, bytes, base, len, off,
920 copyin(p + off, base, len),
921 memcpy(p + off, base, len)
Al Viro04a31162014-11-27 13:51:41 -0500922 )
923 kunmap_atomic(kaddr);
924 return bytes;
Al Viro62a80672014-04-04 23:12:29 -0400925}
Al Virof0b65f32021-04-30 10:26:41 -0400926EXPORT_SYMBOL(copy_page_from_iter_atomic);
Al Viro62a80672014-04-04 23:12:29 -0400927
Al Virob9dc6f62017-01-14 19:33:08 -0500928static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -0400929{
930 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000931 unsigned int p_tail = pipe->tail;
932 unsigned int p_head = pipe->head;
933 unsigned int p_mask = pipe->ring_size - 1;
934
935 if (!pipe_empty(p_head, p_tail)) {
936 struct pipe_buffer *buf;
937 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -0500938 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000939
Al Virob9dc6f62017-01-14 19:33:08 -0500940 if (off) {
David Howells8cefc102019-11-15 13:30:32 +0000941 buf = &pipe->bufs[i_head & p_mask];
942 buf->len = off - buf->offset;
943 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -0500944 }
David Howells8cefc102019-11-15 13:30:32 +0000945 while (p_head != i_head) {
946 p_head--;
947 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
Al Viro241699c2016-09-22 16:33:12 -0400948 }
David Howells8cefc102019-11-15 13:30:32 +0000949
950 pipe->head = p_head;
Al Viro241699c2016-09-22 16:33:12 -0400951 }
Al Virob9dc6f62017-01-14 19:33:08 -0500952}
953
954static void pipe_advance(struct iov_iter *i, size_t size)
955{
956 struct pipe_inode_info *pipe = i->pipe;
Al Virob9dc6f62017-01-14 19:33:08 -0500957 if (size) {
958 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +0000959 unsigned int p_mask = pipe->ring_size - 1;
960 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -0500961 size_t off = i->iov_offset, left = size;
David Howells8cefc102019-11-15 13:30:32 +0000962
Al Virob9dc6f62017-01-14 19:33:08 -0500963 if (off) /* make it relative to the beginning of buffer */
David Howells8cefc102019-11-15 13:30:32 +0000964 left += off - pipe->bufs[i_head & p_mask].offset;
Al Virob9dc6f62017-01-14 19:33:08 -0500965 while (1) {
David Howells8cefc102019-11-15 13:30:32 +0000966 buf = &pipe->bufs[i_head & p_mask];
Al Virob9dc6f62017-01-14 19:33:08 -0500967 if (left <= buf->len)
968 break;
969 left -= buf->len;
David Howells8cefc102019-11-15 13:30:32 +0000970 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -0500971 }
David Howells8cefc102019-11-15 13:30:32 +0000972 i->head = i_head;
Al Virob9dc6f62017-01-14 19:33:08 -0500973 i->iov_offset = buf->offset + left;
974 }
975 i->count -= size;
976 /* ... and discard everything past that point */
977 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -0400978}
979
Pavel Begunkov54c81952021-01-09 16:03:01 +0000980static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
981{
982 struct bvec_iter bi;
983
984 bi.bi_size = i->count;
985 bi.bi_bvec_done = i->iov_offset;
986 bi.bi_idx = 0;
987 bvec_iter_advance(i->bvec, &bi, size);
988
989 i->bvec += bi.bi_idx;
990 i->nr_segs -= bi.bi_idx;
991 i->count = bi.bi_size;
992 i->iov_offset = bi.bi_bvec_done;
993}
994
Al Viro185ac4d2021-04-23 12:58:53 -0400995static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
996{
997 const struct iovec *iov, *end;
998
999 if (!i->count)
1000 return;
1001 i->count -= size;
1002
1003 size += i->iov_offset; // from beginning of current segment
1004 for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
1005 if (likely(size < iov->iov_len))
1006 break;
1007 size -= iov->iov_len;
1008 }
1009 i->iov_offset = size;
1010 i->nr_segs -= iov - i->iov;
1011 i->iov = iov;
1012}
1013
Al Viro62a80672014-04-04 23:12:29 -04001014void iov_iter_advance(struct iov_iter *i, size_t size)
1015{
Al Viro3b3fc052021-04-23 22:24:08 -04001016 if (unlikely(i->count < size))
1017 size = i->count;
Al Viro185ac4d2021-04-23 12:58:53 -04001018 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
1019 /* iovec and kvec have identical layouts */
1020 iov_iter_iovec_advance(i, size);
1021 } else if (iov_iter_is_bvec(i)) {
1022 iov_iter_bvec_advance(i, size);
1023 } else if (iov_iter_is_pipe(i)) {
Al Viro241699c2016-09-22 16:33:12 -04001024 pipe_advance(i, size);
Al Viro185ac4d2021-04-23 12:58:53 -04001025 } else if (unlikely(iov_iter_is_xarray(i))) {
David Howells7ff5062072020-02-10 10:00:21 +00001026 i->iov_offset += size;
1027 i->count -= size;
Al Viro185ac4d2021-04-23 12:58:53 -04001028 } else if (iov_iter_is_discard(i)) {
1029 i->count -= size;
David Howells7ff5062072020-02-10 10:00:21 +00001030 }
Al Viro62a80672014-04-04 23:12:29 -04001031}
1032EXPORT_SYMBOL(iov_iter_advance);
1033
Al Viro27c0e372017-02-17 18:42:24 -05001034void iov_iter_revert(struct iov_iter *i, size_t unroll)
1035{
1036 if (!unroll)
1037 return;
Al Viro5b47d592017-05-08 13:54:47 -04001038 if (WARN_ON(unroll > MAX_RW_COUNT))
1039 return;
Al Viro27c0e372017-02-17 18:42:24 -05001040 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001041 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001042 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001043 unsigned int p_mask = pipe->ring_size - 1;
1044 unsigned int i_head = i->head;
Al Viro27c0e372017-02-17 18:42:24 -05001045 size_t off = i->iov_offset;
1046 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001047 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1048 size_t n = off - b->offset;
Al Viro27c0e372017-02-17 18:42:24 -05001049 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001050 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001051 break;
1052 }
1053 unroll -= n;
David Howells8cefc102019-11-15 13:30:32 +00001054 if (!unroll && i_head == i->start_head) {
Al Viro27c0e372017-02-17 18:42:24 -05001055 off = 0;
1056 break;
1057 }
David Howells8cefc102019-11-15 13:30:32 +00001058 i_head--;
1059 b = &pipe->bufs[i_head & p_mask];
1060 off = b->offset + b->len;
Al Viro27c0e372017-02-17 18:42:24 -05001061 }
1062 i->iov_offset = off;
David Howells8cefc102019-11-15 13:30:32 +00001063 i->head = i_head;
Al Viro27c0e372017-02-17 18:42:24 -05001064 pipe_truncate(i);
1065 return;
1066 }
David Howells9ea9ce02018-10-20 00:57:56 +01001067 if (unlikely(iov_iter_is_discard(i)))
1068 return;
Al Viro27c0e372017-02-17 18:42:24 -05001069 if (unroll <= i->iov_offset) {
1070 i->iov_offset -= unroll;
1071 return;
1072 }
1073 unroll -= i->iov_offset;
David Howells7ff5062072020-02-10 10:00:21 +00001074 if (iov_iter_is_xarray(i)) {
1075 BUG(); /* We should never go beyond the start of the specified
1076 * range since we might then be straying into pages that
1077 * aren't pinned.
1078 */
1079 } else if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001080 const struct bio_vec *bvec = i->bvec;
1081 while (1) {
1082 size_t n = (--bvec)->bv_len;
1083 i->nr_segs++;
1084 if (unroll <= n) {
1085 i->bvec = bvec;
1086 i->iov_offset = n - unroll;
1087 return;
1088 }
1089 unroll -= n;
1090 }
1091 } else { /* same logics for iovec and kvec */
1092 const struct iovec *iov = i->iov;
1093 while (1) {
1094 size_t n = (--iov)->iov_len;
1095 i->nr_segs++;
1096 if (unroll <= n) {
1097 i->iov = iov;
1098 i->iov_offset = n - unroll;
1099 return;
1100 }
1101 unroll -= n;
1102 }
1103 }
1104}
1105EXPORT_SYMBOL(iov_iter_revert);
1106
Al Viro62a80672014-04-04 23:12:29 -04001107/*
1108 * Return the count of just the current iov_iter segment.
1109 */
1110size_t iov_iter_single_seg_count(const struct iov_iter *i)
1111{
Al Viro28f38db2021-06-02 17:25:59 -04001112 if (i->nr_segs > 1) {
1113 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1114 return min(i->count, i->iov->iov_len - i->iov_offset);
1115 if (iov_iter_is_bvec(i))
1116 return min(i->count, i->bvec->bv_len - i->iov_offset);
1117 }
1118 return i->count;
Al Viro62a80672014-04-04 23:12:29 -04001119}
1120EXPORT_SYMBOL(iov_iter_single_seg_count);
1121
David Howellsaa563d72018-10-20 00:57:56 +01001122void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001123 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001124 size_t count)
1125{
David Howellsaa563d72018-10-20 00:57:56 +01001126 WARN_ON(direction & ~(READ | WRITE));
Al Viro8cd54c12021-04-22 14:50:39 -04001127 *i = (struct iov_iter){
1128 .iter_type = ITER_KVEC,
1129 .data_source = direction,
1130 .kvec = kvec,
1131 .nr_segs = nr_segs,
1132 .iov_offset = 0,
1133 .count = count
1134 };
Al Viroabb78f82014-11-24 14:46:11 -05001135}
1136EXPORT_SYMBOL(iov_iter_kvec);
1137
David Howellsaa563d72018-10-20 00:57:56 +01001138void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001139 const struct bio_vec *bvec, unsigned long nr_segs,
1140 size_t count)
1141{
David Howellsaa563d72018-10-20 00:57:56 +01001142 WARN_ON(direction & ~(READ | WRITE));
Al Viro8cd54c12021-04-22 14:50:39 -04001143 *i = (struct iov_iter){
1144 .iter_type = ITER_BVEC,
1145 .data_source = direction,
1146 .bvec = bvec,
1147 .nr_segs = nr_segs,
1148 .iov_offset = 0,
1149 .count = count
1150 };
Al Viro05afcb72015-01-23 01:08:07 -05001151}
1152EXPORT_SYMBOL(iov_iter_bvec);
1153
David Howellsaa563d72018-10-20 00:57:56 +01001154void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001155 struct pipe_inode_info *pipe,
1156 size_t count)
1157{
David Howellsaa563d72018-10-20 00:57:56 +01001158 BUG_ON(direction != READ);
David Howells8cefc102019-11-15 13:30:32 +00001159 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
Al Viro8cd54c12021-04-22 14:50:39 -04001160 *i = (struct iov_iter){
1161 .iter_type = ITER_PIPE,
1162 .data_source = false,
1163 .pipe = pipe,
1164 .head = pipe->head,
1165 .start_head = pipe->head,
1166 .iov_offset = 0,
1167 .count = count
1168 };
Al Viro241699c2016-09-22 16:33:12 -04001169}
1170EXPORT_SYMBOL(iov_iter_pipe);
1171
David Howells9ea9ce02018-10-20 00:57:56 +01001172/**
David Howells7ff5062072020-02-10 10:00:21 +00001173 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1174 * @i: The iterator to initialise.
1175 * @direction: The direction of the transfer.
1176 * @xarray: The xarray to access.
1177 * @start: The start file position.
1178 * @count: The size of the I/O buffer in bytes.
1179 *
1180 * Set up an I/O iterator to either draw data out of the pages attached to an
1181 * inode or to inject data into those pages. The pages *must* be prevented
1182 * from evaporation, either by taking a ref on them or locking them by the
1183 * caller.
1184 */
1185void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1186 struct xarray *xarray, loff_t start, size_t count)
1187{
1188 BUG_ON(direction & ~1);
Al Viro8cd54c12021-04-22 14:50:39 -04001189 *i = (struct iov_iter) {
1190 .iter_type = ITER_XARRAY,
1191 .data_source = direction,
1192 .xarray = xarray,
1193 .xarray_start = start,
1194 .count = count,
1195 .iov_offset = 0
1196 };
David Howells7ff5062072020-02-10 10:00:21 +00001197}
1198EXPORT_SYMBOL(iov_iter_xarray);
1199
1200/**
David Howells9ea9ce02018-10-20 00:57:56 +01001201 * iov_iter_discard - Initialise an I/O iterator that discards data
1202 * @i: The iterator to initialise.
1203 * @direction: The direction of the transfer.
1204 * @count: The size of the I/O buffer in bytes.
1205 *
1206 * Set up an I/O iterator that just discards everything that's written to it.
1207 * It's only available as a READ iterator.
1208 */
1209void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1210{
1211 BUG_ON(direction != READ);
Al Viro8cd54c12021-04-22 14:50:39 -04001212 *i = (struct iov_iter){
1213 .iter_type = ITER_DISCARD,
1214 .data_source = false,
1215 .count = count,
1216 .iov_offset = 0
1217 };
David Howells9ea9ce02018-10-20 00:57:56 +01001218}
1219EXPORT_SYMBOL(iov_iter_discard);
1220
Al Viro9221d2e2021-04-25 00:44:35 -04001221static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -04001222{
Al Viro04a31162014-11-27 13:51:41 -05001223 unsigned long res = 0;
1224 size_t size = i->count;
Al Viro9221d2e2021-04-25 00:44:35 -04001225 size_t skip = i->iov_offset;
1226 unsigned k;
Al Viro04a31162014-11-27 13:51:41 -05001227
Al Viro9221d2e2021-04-25 00:44:35 -04001228 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1229 size_t len = i->iov[k].iov_len - skip;
1230 if (len) {
1231 res |= (unsigned long)i->iov[k].iov_base + skip;
1232 if (len > size)
1233 len = size;
1234 res |= len;
1235 size -= len;
1236 if (!size)
1237 break;
1238 }
1239 }
1240 return res;
1241}
1242
1243static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
1244{
1245 unsigned res = 0;
1246 size_t size = i->count;
1247 unsigned skip = i->iov_offset;
1248 unsigned k;
1249
1250 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1251 size_t len = i->bvec[k].bv_len - skip;
1252 res |= (unsigned long)i->bvec[k].bv_offset + skip;
1253 if (len > size)
1254 len = size;
1255 res |= len;
1256 size -= len;
1257 if (!size)
1258 break;
1259 }
1260 return res;
1261}
1262
1263unsigned long iov_iter_alignment(const struct iov_iter *i)
1264{
1265 /* iovec and kvec have identical layouts */
1266 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1267 return iov_iter_alignment_iovec(i);
1268
1269 if (iov_iter_is_bvec(i))
1270 return iov_iter_alignment_bvec(i);
1271
1272 if (iov_iter_is_pipe(i)) {
Jan Karae0ff1262019-12-16 11:54:32 +01001273 unsigned int p_mask = i->pipe->ring_size - 1;
Al Viro9221d2e2021-04-25 00:44:35 -04001274 size_t size = i->count;
Jan Karae0ff1262019-12-16 11:54:32 +01001275
David Howells8cefc102019-11-15 13:30:32 +00001276 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
Al Viro241699c2016-09-22 16:33:12 -04001277 return size | i->iov_offset;
1278 return size;
1279 }
Al Viro9221d2e2021-04-25 00:44:35 -04001280
1281 if (iov_iter_is_xarray(i))
David Howells3d14ec12021-04-25 22:02:38 +01001282 return (i->xarray_start + i->iov_offset) | i->count;
Al Viro9221d2e2021-04-25 00:44:35 -04001283
1284 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001285}
1286EXPORT_SYMBOL(iov_iter_alignment);
1287
Al Viro357f4352016-04-08 19:05:19 -04001288unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1289{
Al Viro33844e62016-12-21 21:55:02 -05001290 unsigned long res = 0;
Al Viro610c7a72021-04-25 01:03:16 -04001291 unsigned long v = 0;
Al Viro357f4352016-04-08 19:05:19 -04001292 size_t size = i->count;
Al Viro610c7a72021-04-25 01:03:16 -04001293 unsigned k;
Al Viro357f4352016-04-08 19:05:19 -04001294
Al Viro610c7a72021-04-25 01:03:16 -04001295 if (WARN_ON(!iter_is_iovec(i)))
Al Viro241699c2016-09-22 16:33:12 -04001296 return ~0U;
Al Viro241699c2016-09-22 16:33:12 -04001297
Al Viro610c7a72021-04-25 01:03:16 -04001298 for (k = 0; k < i->nr_segs; k++) {
1299 if (i->iov[k].iov_len) {
1300 unsigned long base = (unsigned long)i->iov[k].iov_base;
1301 if (v) // if not the first one
1302 res |= base | v; // this start | previous end
1303 v = base + i->iov[k].iov_len;
1304 if (size <= i->iov[k].iov_len)
1305 break;
1306 size -= i->iov[k].iov_len;
1307 }
1308 }
Al Viro33844e62016-12-21 21:55:02 -05001309 return res;
Al Viro357f4352016-04-08 19:05:19 -04001310}
1311EXPORT_SYMBOL(iov_iter_gap_alignment);
1312
Ilya Dryomove76b63122018-05-02 20:16:56 +02001313static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001314 size_t maxsize,
1315 struct page **pages,
David Howells8cefc102019-11-15 13:30:32 +00001316 int iter_head,
Al Viro241699c2016-09-22 16:33:12 -04001317 size_t *start)
1318{
1319 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001320 unsigned int p_mask = pipe->ring_size - 1;
1321 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001322 if (!n)
1323 return -EFAULT;
1324
1325 maxsize = n;
1326 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001327 while (n > 0) {
David Howells8cefc102019-11-15 13:30:32 +00001328 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1329 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -04001330 n -= PAGE_SIZE;
1331 }
1332
1333 return maxsize;
1334}
1335
1336static ssize_t pipe_get_pages(struct iov_iter *i,
1337 struct page **pages, size_t maxsize, unsigned maxpages,
1338 size_t *start)
1339{
David Howells8cefc102019-11-15 13:30:32 +00001340 unsigned int iter_head, npages;
Al Viro241699c2016-09-22 16:33:12 -04001341 size_t capacity;
Al Viro241699c2016-09-22 16:33:12 -04001342
1343 if (!sanity(i))
1344 return -EFAULT;
1345
David Howells8cefc102019-11-15 13:30:32 +00001346 data_start(i, &iter_head, start);
1347 /* Amount of free space: some of this one + all after this one */
1348 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1349 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
Al Viro241699c2016-09-22 16:33:12 -04001350
David Howells8cefc102019-11-15 13:30:32 +00001351 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001352}
1353
David Howells7ff5062072020-02-10 10:00:21 +00001354static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1355 pgoff_t index, unsigned int nr_pages)
1356{
1357 XA_STATE(xas, xa, index);
1358 struct page *page;
1359 unsigned int ret = 0;
1360
1361 rcu_read_lock();
1362 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1363 if (xas_retry(&xas, page))
1364 continue;
1365
1366 /* Has the page moved or been split? */
1367 if (unlikely(page != xas_reload(&xas))) {
1368 xas_reset(&xas);
1369 continue;
1370 }
1371
1372 pages[ret] = find_subpage(page, xas.xa_index);
1373 get_page(pages[ret]);
1374 if (++ret == nr_pages)
1375 break;
1376 }
1377 rcu_read_unlock();
1378 return ret;
1379}
1380
1381static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1382 struct page **pages, size_t maxsize,
1383 unsigned maxpages, size_t *_start_offset)
1384{
1385 unsigned nr, offset;
1386 pgoff_t index, count;
1387 size_t size = maxsize, actual;
1388 loff_t pos;
1389
1390 if (!size || !maxpages)
1391 return 0;
1392
1393 pos = i->xarray_start + i->iov_offset;
1394 index = pos >> PAGE_SHIFT;
1395 offset = pos & ~PAGE_MASK;
1396 *_start_offset = offset;
1397
1398 count = 1;
1399 if (size > PAGE_SIZE - offset) {
1400 size -= PAGE_SIZE - offset;
1401 count += size >> PAGE_SHIFT;
1402 size &= ~PAGE_MASK;
1403 if (size)
1404 count++;
1405 }
1406
1407 if (count > maxpages)
1408 count = maxpages;
1409
1410 nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
1411 if (nr == 0)
1412 return 0;
1413
1414 actual = PAGE_SIZE * nr;
1415 actual -= offset;
1416 if (nr == count && size > 0) {
1417 unsigned last_offset = (nr > 1) ? 0 : offset;
1418 actual -= PAGE_SIZE - (last_offset + size);
1419 }
1420 return actual;
1421}
1422
Al Viro3d671ca2021-04-25 09:14:44 -04001423/* must be done on non-empty ITER_IOVEC one */
1424static unsigned long first_iovec_segment(const struct iov_iter *i,
1425 size_t *size, size_t *start,
1426 size_t maxsize, unsigned maxpages)
1427{
1428 size_t skip;
1429 long k;
1430
1431 for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1432 unsigned long addr = (unsigned long)i->iov[k].iov_base + skip;
1433 size_t len = i->iov[k].iov_len - skip;
1434
1435 if (unlikely(!len))
1436 continue;
1437 if (len > maxsize)
1438 len = maxsize;
1439 len += (*start = addr % PAGE_SIZE);
1440 if (len > maxpages * PAGE_SIZE)
1441 len = maxpages * PAGE_SIZE;
1442 *size = len;
1443 return addr & PAGE_MASK;
1444 }
1445 BUG(); // if it had been empty, we wouldn't get called
1446}
1447
1448/* must be done on non-empty ITER_BVEC one */
1449static struct page *first_bvec_segment(const struct iov_iter *i,
1450 size_t *size, size_t *start,
1451 size_t maxsize, unsigned maxpages)
1452{
1453 struct page *page;
1454 size_t skip = i->iov_offset, len;
1455
1456 len = i->bvec->bv_len - skip;
1457 if (len > maxsize)
1458 len = maxsize;
1459 skip += i->bvec->bv_offset;
1460 page = i->bvec->bv_page + skip / PAGE_SIZE;
1461 len += (*start = skip % PAGE_SIZE);
1462 if (len > maxpages * PAGE_SIZE)
1463 len = maxpages * PAGE_SIZE;
1464 *size = len;
1465 return page;
1466}
1467
Al Viro62a80672014-04-04 23:12:29 -04001468ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001469 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001470 size_t *start)
1471{
Al Viro3d671ca2021-04-25 09:14:44 -04001472 size_t len;
1473 int n, res;
1474
Al Viroe5393fa2014-11-27 14:12:09 -05001475 if (maxsize > i->count)
1476 maxsize = i->count;
Al Viro3d671ca2021-04-25 09:14:44 -04001477 if (!maxsize)
1478 return 0;
Al Viroe5393fa2014-11-27 14:12:09 -05001479
Al Viro3d671ca2021-04-25 09:14:44 -04001480 if (likely(iter_is_iovec(i))) {
1481 unsigned long addr;
David Howells9ea9ce02018-10-20 00:57:56 +01001482
Al Viro3d671ca2021-04-25 09:14:44 -04001483 addr = first_iovec_segment(i, &len, start, maxsize, maxpages);
Al Viroe5393fa2014-11-27 14:12:09 -05001484 n = DIV_ROUND_UP(len, PAGE_SIZE);
Ira Weiny73b01402019-05-13 17:17:11 -07001485 res = get_user_pages_fast(addr, n,
1486 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
1487 pages);
Al Viroe5393fa2014-11-27 14:12:09 -05001488 if (unlikely(res < 0))
1489 return res;
1490 return (res == n ? len : res * PAGE_SIZE) - *start;
Al Viro3d671ca2021-04-25 09:14:44 -04001491 }
1492 if (iov_iter_is_bvec(i)) {
1493 struct page *page;
1494
1495 page = first_bvec_segment(i, &len, start, maxsize, maxpages);
1496 n = DIV_ROUND_UP(len, PAGE_SIZE);
1497 while (n--)
1498 get_page(*pages++ = page++);
1499 return len - *start;
1500 }
1501 if (iov_iter_is_pipe(i))
1502 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1503 if (iov_iter_is_xarray(i))
1504 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1505 return -EFAULT;
Al Viro62a80672014-04-04 23:12:29 -04001506}
1507EXPORT_SYMBOL(iov_iter_get_pages);
1508
Al Viro1b17f1f2014-11-27 14:14:31 -05001509static struct page **get_pages_array(size_t n)
1510{
Michal Hocko752ade62017-05-08 15:57:27 -07001511 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001512}
1513
Al Viro241699c2016-09-22 16:33:12 -04001514static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1515 struct page ***pages, size_t maxsize,
1516 size_t *start)
1517{
1518 struct page **p;
David Howells8cefc102019-11-15 13:30:32 +00001519 unsigned int iter_head, npages;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001520 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001521
1522 if (!sanity(i))
1523 return -EFAULT;
1524
David Howells8cefc102019-11-15 13:30:32 +00001525 data_start(i, &iter_head, start);
1526 /* Amount of free space: some of this one + all after this one */
1527 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
Al Viro241699c2016-09-22 16:33:12 -04001528 n = npages * PAGE_SIZE - *start;
1529 if (maxsize > n)
1530 maxsize = n;
1531 else
1532 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1533 p = get_pages_array(npages);
1534 if (!p)
1535 return -ENOMEM;
David Howells8cefc102019-11-15 13:30:32 +00001536 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001537 if (n > 0)
1538 *pages = p;
1539 else
1540 kvfree(p);
1541 return n;
1542}
1543
David Howells7ff5062072020-02-10 10:00:21 +00001544static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
1545 struct page ***pages, size_t maxsize,
1546 size_t *_start_offset)
1547{
1548 struct page **p;
1549 unsigned nr, offset;
1550 pgoff_t index, count;
1551 size_t size = maxsize, actual;
1552 loff_t pos;
1553
1554 if (!size)
1555 return 0;
1556
1557 pos = i->xarray_start + i->iov_offset;
1558 index = pos >> PAGE_SHIFT;
1559 offset = pos & ~PAGE_MASK;
1560 *_start_offset = offset;
1561
1562 count = 1;
1563 if (size > PAGE_SIZE - offset) {
1564 size -= PAGE_SIZE - offset;
1565 count += size >> PAGE_SHIFT;
1566 size &= ~PAGE_MASK;
1567 if (size)
1568 count++;
1569 }
1570
1571 p = get_pages_array(count);
1572 if (!p)
1573 return -ENOMEM;
1574 *pages = p;
1575
1576 nr = iter_xarray_populate_pages(p, i->xarray, index, count);
1577 if (nr == 0)
1578 return 0;
1579
1580 actual = PAGE_SIZE * nr;
1581 actual -= offset;
1582 if (nr == count && size > 0) {
1583 unsigned last_offset = (nr > 1) ? 0 : offset;
1584 actual -= PAGE_SIZE - (last_offset + size);
1585 }
1586 return actual;
1587}
1588
Al Viro62a80672014-04-04 23:12:29 -04001589ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1590 struct page ***pages, size_t maxsize,
1591 size_t *start)
1592{
Al Viro1b17f1f2014-11-27 14:14:31 -05001593 struct page **p;
Al Viro3d671ca2021-04-25 09:14:44 -04001594 size_t len;
1595 int n, res;
Al Viro1b17f1f2014-11-27 14:14:31 -05001596
1597 if (maxsize > i->count)
1598 maxsize = i->count;
Al Viro3d671ca2021-04-25 09:14:44 -04001599 if (!maxsize)
1600 return 0;
Al Viro1b17f1f2014-11-27 14:14:31 -05001601
Al Viro3d671ca2021-04-25 09:14:44 -04001602 if (likely(iter_is_iovec(i))) {
1603 unsigned long addr;
David Howells9ea9ce02018-10-20 00:57:56 +01001604
Al Viro3d671ca2021-04-25 09:14:44 -04001605 addr = first_iovec_segment(i, &len, start, maxsize, ~0U);
Al Viro1b17f1f2014-11-27 14:14:31 -05001606 n = DIV_ROUND_UP(len, PAGE_SIZE);
1607 p = get_pages_array(n);
1608 if (!p)
1609 return -ENOMEM;
Ira Weiny73b01402019-05-13 17:17:11 -07001610 res = get_user_pages_fast(addr, n,
1611 iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
Al Viro1b17f1f2014-11-27 14:14:31 -05001612 if (unlikely(res < 0)) {
1613 kvfree(p);
1614 return res;
1615 }
1616 *pages = p;
1617 return (res == n ? len : res * PAGE_SIZE) - *start;
Al Viro3d671ca2021-04-25 09:14:44 -04001618 }
1619 if (iov_iter_is_bvec(i)) {
1620 struct page *page;
1621
1622 page = first_bvec_segment(i, &len, start, maxsize, ~0U);
1623 n = DIV_ROUND_UP(len, PAGE_SIZE);
1624 *pages = p = get_pages_array(n);
Al Viro1b17f1f2014-11-27 14:14:31 -05001625 if (!p)
1626 return -ENOMEM;
Al Viro3d671ca2021-04-25 09:14:44 -04001627 while (n--)
1628 get_page(*p++ = page++);
1629 return len - *start;
1630 }
1631 if (iov_iter_is_pipe(i))
1632 return pipe_get_pages_alloc(i, pages, maxsize, start);
1633 if (iov_iter_is_xarray(i))
1634 return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
1635 return -EFAULT;
Al Viro62a80672014-04-04 23:12:29 -04001636}
1637EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1638
Al Viroa604ec72014-11-24 01:08:00 -05001639size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1640 struct iov_iter *i)
1641{
Al Viroa604ec72014-11-24 01:08:00 -05001642 __wsum sum, next;
Al Viroa604ec72014-11-24 01:08:00 -05001643 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001644 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001645 WARN_ON(1);
1646 return 0;
1647 }
Al Viro7baa5092021-05-02 11:35:03 -04001648 iterate_and_advance(i, bytes, base, len, off, ({
1649 next = csum_and_copy_from_user(base, addr + off, len);
Al Viro2495bdcc2021-04-30 13:40:48 -04001650 sum = csum_block_add(sum, next, off);
Al Viro7baa5092021-05-02 11:35:03 -04001651 next ? 0 : len;
Al Viroa604ec72014-11-24 01:08:00 -05001652 }), ({
Al Viro7baa5092021-05-02 11:35:03 -04001653 sum = csum_and_memcpy(addr + off, base, len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001654 })
1655 )
1656 *csum = sum;
1657 return bytes;
1658}
1659EXPORT_SYMBOL(csum_and_copy_from_iter);
1660
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001661size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
Al Viroa604ec72014-11-24 01:08:00 -05001662 struct iov_iter *i)
1663{
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001664 struct csum_state *csstate = _csstate;
Al Viroa604ec72014-11-24 01:08:00 -05001665 __wsum sum, next;
Al Viro78e1f382018-11-25 16:24:16 -05001666
Al Viro78e1f382018-11-25 16:24:16 -05001667 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001668 WARN_ON(1); /* for now */
1669 return 0;
1670 }
Al Viro6852df12021-05-02 17:24:40 -04001671
1672 sum = csum_shift(csstate->csum, csstate->off);
1673 if (unlikely(iov_iter_is_pipe(i)))
1674 bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
1675 else iterate_and_advance(i, bytes, base, len, off, ({
Al Viro7baa5092021-05-02 11:35:03 -04001676 next = csum_and_copy_to_user(addr + off, base, len);
Al Viro2495bdcc2021-04-30 13:40:48 -04001677 sum = csum_block_add(sum, next, off);
Al Viro7baa5092021-05-02 11:35:03 -04001678 next ? 0 : len;
Al Viroa604ec72014-11-24 01:08:00 -05001679 }), ({
Al Viro7baa5092021-05-02 11:35:03 -04001680 sum = csum_and_memcpy(base, addr + off, len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001681 })
1682 )
Al Viro594e4502021-06-05 10:19:30 -04001683 csstate->csum = csum_shift(sum, csstate->off);
1684 csstate->off += bytes;
Al Viroa604ec72014-11-24 01:08:00 -05001685 return bytes;
1686}
1687EXPORT_SYMBOL(csum_and_copy_to_iter);
1688
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001689size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1690 struct iov_iter *i)
1691{
Herbert Xu79990962020-06-12 16:57:37 +10001692#ifdef CONFIG_CRYPTO_HASH
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001693 struct ahash_request *hash = hashp;
1694 struct scatterlist sg;
1695 size_t copied;
1696
1697 copied = copy_to_iter(addr, bytes, i);
1698 sg_init_one(&sg, addr, copied);
1699 ahash_request_set_crypt(hash, &sg, NULL, copied);
1700 crypto_ahash_update(hash);
1701 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001702#else
1703 return 0;
1704#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001705}
1706EXPORT_SYMBOL(hash_and_copy_to_iter);
1707
Al Viro66531c62021-04-25 16:00:48 -04001708static int iov_npages(const struct iov_iter *i, int maxpages)
Al Viro62a80672014-04-04 23:12:29 -04001709{
Al Viro66531c62021-04-25 16:00:48 -04001710 size_t skip = i->iov_offset, size = i->count;
1711 const struct iovec *p;
Al Viroe0f2dc42014-11-27 14:09:46 -05001712 int npages = 0;
1713
Al Viro66531c62021-04-25 16:00:48 -04001714 for (p = i->iov; size; skip = 0, p++) {
1715 unsigned offs = offset_in_page(p->iov_base + skip);
1716 size_t len = min(p->iov_len - skip, size);
Al Viroe0f2dc42014-11-27 14:09:46 -05001717
Al Viro66531c62021-04-25 16:00:48 -04001718 if (len) {
1719 size -= len;
1720 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1721 if (unlikely(npages > maxpages))
1722 return maxpages;
1723 }
1724 }
1725 return npages;
1726}
1727
1728static int bvec_npages(const struct iov_iter *i, int maxpages)
1729{
1730 size_t skip = i->iov_offset, size = i->count;
1731 const struct bio_vec *p;
1732 int npages = 0;
1733
1734 for (p = i->bvec; size; skip = 0, p++) {
1735 unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
1736 size_t len = min(p->bv_len - skip, size);
1737
1738 size -= len;
1739 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1740 if (unlikely(npages > maxpages))
1741 return maxpages;
1742 }
1743 return npages;
1744}
1745
1746int iov_iter_npages(const struct iov_iter *i, int maxpages)
1747{
1748 if (unlikely(!i->count))
1749 return 0;
1750 /* iovec and kvec have identical layouts */
1751 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1752 return iov_npages(i, maxpages);
1753 if (iov_iter_is_bvec(i))
1754 return bvec_npages(i, maxpages);
1755 if (iov_iter_is_pipe(i)) {
David Howells8cefc102019-11-15 13:30:32 +00001756 unsigned int iter_head;
Al Viro66531c62021-04-25 16:00:48 -04001757 int npages;
Al Viro241699c2016-09-22 16:33:12 -04001758 size_t off;
Al Viro241699c2016-09-22 16:33:12 -04001759
1760 if (!sanity(i))
1761 return 0;
1762
David Howells8cefc102019-11-15 13:30:32 +00001763 data_start(i, &iter_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001764 /* some of this one + all after this one */
Al Viro66531c62021-04-25 16:00:48 -04001765 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1766 return min(npages, maxpages);
1767 }
1768 if (iov_iter_is_xarray(i)) {
Al Viroe4f8df82021-05-03 11:05:29 -04001769 unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1770 int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
Al Viro66531c62021-04-25 16:00:48 -04001771 return min(npages, maxpages);
1772 }
1773 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001774}
Al Virof67da302014-03-19 01:16:16 -04001775EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001776
1777const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1778{
1779 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001780 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001781 WARN_ON(1);
1782 return NULL;
1783 }
David Howells7ff5062072020-02-10 10:00:21 +00001784 if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
David Howells9ea9ce02018-10-20 00:57:56 +01001785 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001786 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001787 return new->bvec = kmemdup(new->bvec,
1788 new->nr_segs * sizeof(struct bio_vec),
1789 flags);
1790 else
1791 /* iovec and kvec have identical layout */
1792 return new->iov = kmemdup(new->iov,
1793 new->nr_segs * sizeof(struct iovec),
1794 flags);
1795}
1796EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001797
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001798static int copy_compat_iovec_from_user(struct iovec *iov,
1799 const struct iovec __user *uvec, unsigned long nr_segs)
1800{
1801 const struct compat_iovec __user *uiov =
1802 (const struct compat_iovec __user *)uvec;
1803 int ret = -EFAULT, i;
1804
Christoph Hellwiga959a972021-01-11 18:19:26 +01001805 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001806 return -EFAULT;
1807
1808 for (i = 0; i < nr_segs; i++) {
1809 compat_uptr_t buf;
1810 compat_ssize_t len;
1811
1812 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1813 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1814
1815 /* check for compat_size_t not fitting in compat_ssize_t .. */
1816 if (len < 0) {
1817 ret = -EINVAL;
1818 goto uaccess_end;
1819 }
1820 iov[i].iov_base = compat_ptr(buf);
1821 iov[i].iov_len = len;
1822 }
1823
1824 ret = 0;
1825uaccess_end:
1826 user_access_end();
1827 return ret;
1828}
1829
1830static int copy_iovec_from_user(struct iovec *iov,
1831 const struct iovec __user *uvec, unsigned long nr_segs)
David Laightfb041b52020-09-25 06:51:39 +02001832{
1833 unsigned long seg;
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001834
1835 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1836 return -EFAULT;
1837 for (seg = 0; seg < nr_segs; seg++) {
1838 if ((ssize_t)iov[seg].iov_len < 0)
1839 return -EINVAL;
1840 }
1841
1842 return 0;
1843}
1844
1845struct iovec *iovec_from_user(const struct iovec __user *uvec,
1846 unsigned long nr_segs, unsigned long fast_segs,
1847 struct iovec *fast_iov, bool compat)
1848{
1849 struct iovec *iov = fast_iov;
1850 int ret;
David Laightfb041b52020-09-25 06:51:39 +02001851
1852 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001853 * SuS says "The readv() function *may* fail if the iovcnt argument was
1854 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
David Laightfb041b52020-09-25 06:51:39 +02001855 * traditionally returned zero for zero segments, so...
1856 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001857 if (nr_segs == 0)
1858 return iov;
1859 if (nr_segs > UIO_MAXIOV)
1860 return ERR_PTR(-EINVAL);
David Laightfb041b52020-09-25 06:51:39 +02001861 if (nr_segs > fast_segs) {
1862 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001863 if (!iov)
1864 return ERR_PTR(-ENOMEM);
David Laightfb041b52020-09-25 06:51:39 +02001865 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001866
1867 if (compat)
1868 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1869 else
1870 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1871 if (ret) {
1872 if (iov != fast_iov)
1873 kfree(iov);
1874 return ERR_PTR(ret);
1875 }
1876
1877 return iov;
1878}
1879
1880ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1881 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1882 struct iov_iter *i, bool compat)
1883{
1884 ssize_t total_len = 0;
1885 unsigned long seg;
1886 struct iovec *iov;
1887
1888 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1889 if (IS_ERR(iov)) {
1890 *iovp = NULL;
1891 return PTR_ERR(iov);
David Laightfb041b52020-09-25 06:51:39 +02001892 }
1893
1894 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001895 * According to the Single Unix Specification we should return EINVAL if
1896 * an element length is < 0 when cast to ssize_t or if the total length
1897 * would overflow the ssize_t return value of the system call.
David Laightfb041b52020-09-25 06:51:39 +02001898 *
1899 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1900 * overflow case.
1901 */
David Laightfb041b52020-09-25 06:51:39 +02001902 for (seg = 0; seg < nr_segs; seg++) {
David Laightfb041b52020-09-25 06:51:39 +02001903 ssize_t len = (ssize_t)iov[seg].iov_len;
1904
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001905 if (!access_ok(iov[seg].iov_base, len)) {
1906 if (iov != *iovp)
1907 kfree(iov);
1908 *iovp = NULL;
1909 return -EFAULT;
David Laightfb041b52020-09-25 06:51:39 +02001910 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001911
1912 if (len > MAX_RW_COUNT - total_len) {
1913 len = MAX_RW_COUNT - total_len;
David Laightfb041b52020-09-25 06:51:39 +02001914 iov[seg].iov_len = len;
1915 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001916 total_len += len;
David Laightfb041b52020-09-25 06:51:39 +02001917 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001918
1919 iov_iter_init(i, type, iov, nr_segs, total_len);
1920 if (iov == *iovp)
1921 *iovp = NULL;
1922 else
1923 *iovp = iov;
1924 return total_len;
David Laightfb041b52020-09-25 06:51:39 +02001925}
1926
Vegard Nossumffecee42016-10-08 11:18:07 +02001927/**
1928 * import_iovec() - Copy an array of &struct iovec from userspace
1929 * into the kernel, check that it is valid, and initialize a new
1930 * &struct iov_iter iterator to access it.
1931 *
1932 * @type: One of %READ or %WRITE.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001933 * @uvec: Pointer to the userspace array.
Vegard Nossumffecee42016-10-08 11:18:07 +02001934 * @nr_segs: Number of elements in userspace array.
1935 * @fast_segs: Number of elements in @iov.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001936 * @iovp: (input and output parameter) Pointer to pointer to (usually small
Vegard Nossumffecee42016-10-08 11:18:07 +02001937 * on-stack) kernel array.
1938 * @i: Pointer to iterator that will be initialized on success.
1939 *
1940 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1941 * then this function places %NULL in *@iov on return. Otherwise, a new
1942 * array will be allocated and the result placed in *@iov. This means that
1943 * the caller may call kfree() on *@iov regardless of whether the small
1944 * on-stack array was used or not (and regardless of whether this function
1945 * returns an error or not).
1946 *
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001947 * Return: Negative error code on error, bytes imported on success
Vegard Nossumffecee42016-10-08 11:18:07 +02001948 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001949ssize_t import_iovec(int type, const struct iovec __user *uvec,
Al Virobc917be2015-03-21 17:45:43 -04001950 unsigned nr_segs, unsigned fast_segs,
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001951 struct iovec **iovp, struct iov_iter *i)
Al Virobc917be2015-03-21 17:45:43 -04001952{
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02001953 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
1954 in_compat_syscall());
Al Virobc917be2015-03-21 17:45:43 -04001955}
1956EXPORT_SYMBOL(import_iovec);
1957
Al Virobc917be2015-03-21 17:45:43 -04001958int import_single_range(int rw, void __user *buf, size_t len,
1959 struct iovec *iov, struct iov_iter *i)
1960{
1961 if (len > MAX_RW_COUNT)
1962 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001963 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04001964 return -EFAULT;
1965
1966 iov->iov_base = buf;
1967 iov->iov_len = len;
1968 iov_iter_init(i, rw, iov, 1, len);
1969 return 0;
1970}
Al Viroe1267582015-12-06 20:38:56 -05001971EXPORT_SYMBOL(import_single_range);