blob: 66a740e6e153c4846abef78d54dfcff97e96219c [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
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +0200194 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_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
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +0200278 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_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/*
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200433 * fault_in_iov_iter_readable - fault in iov iterator for reading
434 * @i: iterator
435 * @size: maximum length
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400436 *
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200437 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
438 * @size. For each iovec, fault in each page that constitutes the iovec.
439 *
440 * Returns the number of bytes not faulted in (like copy_to_user() and
441 * copy_from_user()).
442 *
443 * Always returns 0 for non-userspace iterators.
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400444 */
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200445size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400446{
Al Viro0e8f0d62021-06-02 14:48:21 -0400447 if (iter_is_iovec(i)) {
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200448 size_t count = min(size, iov_iter_count(i));
Al Viro8409a0d2021-05-02 11:57:37 -0400449 const struct iovec *p;
450 size_t skip;
451
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200452 size -= count;
453 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
454 size_t len = min(count, p->iov_len - skip);
455 size_t ret;
Al Viro8409a0d2021-05-02 11:57:37 -0400456
457 if (unlikely(!len))
458 continue;
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200459 ret = fault_in_readable(p->iov_base + skip, len);
460 count -= len - ret;
461 if (ret)
462 break;
Al Viro8409a0d2021-05-02 11:57:37 -0400463 }
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200464 return count + size;
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400465 }
466 return 0;
467}
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200468EXPORT_SYMBOL(fault_in_iov_iter_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400469
Andreas Gruenbachercdd591f2021-07-05 17:26:28 +0200470/*
471 * fault_in_iov_iter_writeable - fault in iov iterator for writing
472 * @i: iterator
473 * @size: maximum length
474 *
475 * Faults in the iterator using get_user_pages(), i.e., without triggering
476 * hardware page faults. This is primarily useful when we already know that
477 * some or all of the pages in @i aren't in memory.
478 *
479 * Returns the number of bytes not faulted in, like copy_to_user() and
480 * copy_from_user().
481 *
482 * Always returns 0 for non-user-space iterators.
483 */
484size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
485{
486 if (iter_is_iovec(i)) {
487 size_t count = min(size, iov_iter_count(i));
488 const struct iovec *p;
489 size_t skip;
490
491 size -= count;
492 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
493 size_t len = min(count, p->iov_len - skip);
494 size_t ret;
495
496 if (unlikely(!len))
497 continue;
498 ret = fault_in_safe_writeable(p->iov_base + skip, len);
499 count -= len - ret;
500 if (ret)
501 break;
502 }
503 return count + size;
504 }
505 return 0;
506}
507EXPORT_SYMBOL(fault_in_iov_iter_writeable);
508
David Howellsaa563d72018-10-20 00:57:56 +0100509void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500510 const struct iovec *iov, unsigned long nr_segs,
511 size_t count)
512{
David Howellsaa563d72018-10-20 00:57:56 +0100513 WARN_ON(direction & ~(READ | WRITE));
Al Viro8cd54c12021-04-22 14:50:39 -0400514 *i = (struct iov_iter) {
515 .iter_type = ITER_IOVEC,
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +0200516 .nofault = false,
Al Viro8cd54c12021-04-22 14:50:39 -0400517 .data_source = direction,
518 .iov = iov,
519 .nr_segs = nr_segs,
520 .iov_offset = 0,
521 .count = count
522 };
Al Viro71d8e532014-03-05 19:28:09 -0500523}
524EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400525
Al Viro241699c2016-09-22 16:33:12 -0400526static inline bool allocated(struct pipe_buffer *buf)
527{
528 return buf->ops == &default_pipe_buf_ops;
529}
530
David Howells8cefc102019-11-15 13:30:32 +0000531static inline void data_start(const struct iov_iter *i,
532 unsigned int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400533{
David Howells8cefc102019-11-15 13:30:32 +0000534 unsigned int p_mask = i->pipe->ring_size - 1;
535 unsigned int iter_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400536 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000537
538 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
539 off == PAGE_SIZE)) {
540 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400541 off = 0;
542 }
David Howells8cefc102019-11-15 13:30:32 +0000543 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400544 *offp = off;
545}
546
547static size_t push_pipe(struct iov_iter *i, size_t size,
David Howells8cefc102019-11-15 13:30:32 +0000548 int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400549{
550 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000551 unsigned int p_tail = pipe->tail;
552 unsigned int p_mask = pipe->ring_size - 1;
553 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400554 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400555 ssize_t left;
556
557 if (unlikely(size > i->count))
558 size = i->count;
559 if (unlikely(!size))
560 return 0;
561
562 left = size;
David Howells8cefc102019-11-15 13:30:32 +0000563 data_start(i, &iter_head, &off);
564 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400565 *offp = off;
566 if (off) {
567 left -= PAGE_SIZE - off;
568 if (left <= 0) {
David Howells8cefc102019-11-15 13:30:32 +0000569 pipe->bufs[iter_head & p_mask].len += size;
Al Viro241699c2016-09-22 16:33:12 -0400570 return size;
571 }
David Howells8cefc102019-11-15 13:30:32 +0000572 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
573 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400574 }
David Howells6718b6f2019-10-16 16:47:32 +0100575 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000576 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400577 struct page *page = alloc_page(GFP_USER);
578 if (!page)
579 break;
David Howells8cefc102019-11-15 13:30:32 +0000580
581 buf->ops = &default_pipe_buf_ops;
582 buf->page = page;
583 buf->offset = 0;
584 buf->len = min_t(ssize_t, left, PAGE_SIZE);
585 left -= buf->len;
586 iter_head++;
587 pipe->head = iter_head;
588
589 if (left == 0)
Al Viro241699c2016-09-22 16:33:12 -0400590 return size;
Al Viro241699c2016-09-22 16:33:12 -0400591 }
592 return size - left;
593}
594
595static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
596 struct iov_iter *i)
597{
598 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000599 unsigned int p_mask = pipe->ring_size - 1;
600 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400601 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400602
603 if (!sanity(i))
604 return 0;
605
David Howells8cefc102019-11-15 13:30:32 +0000606 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400607 if (unlikely(!n))
608 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000609 do {
Al Viro241699c2016-09-22 16:33:12 -0400610 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000611 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
612 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400613 i->iov_offset = off + chunk;
614 n -= chunk;
615 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000616 off = 0;
617 i_head++;
618 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400619 i->count -= bytes;
620 return bytes;
621}
622
Al Virof9152892018-11-27 22:32:59 -0500623static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
624 __wsum sum, size_t off)
625{
Al Virocc44c172020-07-11 00:12:07 -0400626 __wsum next = csum_partial_copy_nocheck(from, to, len);
Al Virof9152892018-11-27 22:32:59 -0500627 return csum_block_add(sum, next, off);
628}
629
Al Viro78e1f382018-11-25 16:24:16 -0500630static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
Al Viro6852df12021-05-02 17:24:40 -0400631 struct iov_iter *i, __wsum *sump)
Al Viro78e1f382018-11-25 16:24:16 -0500632{
633 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000634 unsigned int p_mask = pipe->ring_size - 1;
Al Viro6852df12021-05-02 17:24:40 -0400635 __wsum sum = *sump;
636 size_t off = 0;
David Howells8cefc102019-11-15 13:30:32 +0000637 unsigned int i_head;
Al Viro6852df12021-05-02 17:24:40 -0400638 size_t r;
Al Viro78e1f382018-11-25 16:24:16 -0500639
640 if (!sanity(i))
641 return 0;
642
Al Viro6852df12021-05-02 17:24:40 -0400643 bytes = push_pipe(i, bytes, &i_head, &r);
644 while (bytes) {
645 size_t chunk = min_t(size_t, bytes, PAGE_SIZE - r);
Al Viro2495bdcc2021-04-30 13:40:48 -0400646 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
Al Viro6852df12021-05-02 17:24:40 -0400647 sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
Al Viro2495bdcc2021-04-30 13:40:48 -0400648 kunmap_local(p);
David Howells8cefc102019-11-15 13:30:32 +0000649 i->head = i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500650 i->iov_offset = r + chunk;
Al Viro6852df12021-05-02 17:24:40 -0400651 bytes -= chunk;
Al Viro78e1f382018-11-25 16:24:16 -0500652 off += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000653 r = 0;
654 i_head++;
Al Viro6852df12021-05-02 17:24:40 -0400655 }
656 *sump = sum;
657 i->count -= off;
658 return off;
Al Viro78e1f382018-11-25 16:24:16 -0500659}
660
Al Viroaa28de22017-06-29 21:45:10 -0400661size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400662{
David Howells00e23702018-10-22 13:07:28 +0100663 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400664 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400665 if (iter_is_iovec(i))
666 might_fault();
Al Viro7baa5092021-05-02 11:35:03 -0400667 iterate_and_advance(i, bytes, base, len, off,
668 copyout(base, addr + off, len),
669 memcpy(base, addr + off, len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500670 )
Al Viro62a80672014-04-04 23:12:29 -0400671
Al Viro3d4d3e42014-11-27 14:28:06 -0500672 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400673}
Al Viroaa28de22017-06-29 21:45:10 -0400674EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400675
Dan Williamsec6347b2020-10-05 20:40:16 -0700676#ifdef CONFIG_ARCH_HAS_COPY_MC
677static int copyout_mc(void __user *to, const void *from, size_t n)
Dan Williams87803562018-05-03 17:06:31 -0700678{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800679 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100680 instrument_copy_to_user(to, from, n);
Dan Williamsec6347b2020-10-05 20:40:16 -0700681 n = copy_mc_to_user((__force void *) to, from, n);
Dan Williams87803562018-05-03 17:06:31 -0700682 }
683 return n;
684}
685
Dan Williamsec6347b2020-10-05 20:40:16 -0700686static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
Dan Williamsca146f62018-07-08 13:46:12 -0700687 struct iov_iter *i)
688{
689 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000690 unsigned int p_mask = pipe->ring_size - 1;
691 unsigned int i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700692 size_t n, off, xfer = 0;
Dan Williamsca146f62018-07-08 13:46:12 -0700693
694 if (!sanity(i))
695 return 0;
696
Al Viro2a510a72021-05-02 17:16:34 -0400697 n = push_pipe(i, bytes, &i_head, &off);
698 while (n) {
Dan Williamsca146f62018-07-08 13:46:12 -0700699 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
Al Viro2a510a72021-05-02 17:16:34 -0400700 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
Dan Williamsca146f62018-07-08 13:46:12 -0700701 unsigned long rem;
Al Viro2a510a72021-05-02 17:16:34 -0400702 rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
703 chunk -= rem;
704 kunmap_local(p);
David Howells8cefc102019-11-15 13:30:32 +0000705 i->head = i_head;
Al Viro2a510a72021-05-02 17:16:34 -0400706 i->iov_offset = off + chunk;
707 xfer += chunk;
Dan Williamsca146f62018-07-08 13:46:12 -0700708 if (rem)
709 break;
710 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000711 off = 0;
712 i_head++;
Al Viro2a510a72021-05-02 17:16:34 -0400713 }
Dan Williamsca146f62018-07-08 13:46:12 -0700714 i->count -= xfer;
715 return xfer;
716}
717
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700718/**
Dan Williamsec6347b2020-10-05 20:40:16 -0700719 * _copy_mc_to_iter - copy to iter with source memory error exception handling
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700720 * @addr: source kernel address
721 * @bytes: total transfer length
Randy Dunlap44e55992021-09-07 19:58:54 -0700722 * @i: destination iterator
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700723 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700724 * The pmem driver deploys this for the dax operation
725 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
726 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
727 * successfully copied.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700728 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700729 * The main differences between this and typical _copy_to_iter().
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700730 *
731 * * Typical tail/residue handling after a fault retries the copy
732 * byte-by-byte until the fault happens again. Re-triggering machine
733 * checks is potentially fatal so the implementation uses source
734 * alignment and poison alignment assumptions to avoid re-triggering
735 * hardware exceptions.
736 *
737 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
738 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
739 * a short copy.
Randy Dunlap44e55992021-09-07 19:58:54 -0700740 *
741 * Return: number of bytes copied (may be %0)
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700742 */
Dan Williamsec6347b2020-10-05 20:40:16 -0700743size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Dan Williams87803562018-05-03 17:06:31 -0700744{
David Howells00e23702018-10-22 13:07:28 +0100745 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsec6347b2020-10-05 20:40:16 -0700746 return copy_mc_pipe_to_iter(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700747 if (iter_is_iovec(i))
748 might_fault();
Al Viro7baa5092021-05-02 11:35:03 -0400749 __iterate_and_advance(i, bytes, base, len, off,
750 copyout_mc(base, addr + off, len),
751 copy_mc_to_kernel(base, addr + off, len)
Dan Williams87803562018-05-03 17:06:31 -0700752 )
753
754 return bytes;
755}
Dan Williamsec6347b2020-10-05 20:40:16 -0700756EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
757#endif /* CONFIG_ARCH_HAS_COPY_MC */
Dan Williams87803562018-05-03 17:06:31 -0700758
Al Viroaa28de22017-06-29 21:45:10 -0400759size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400760{
David Howells00e23702018-10-22 13:07:28 +0100761 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400762 WARN_ON(1);
763 return 0;
764 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400765 if (iter_is_iovec(i))
766 might_fault();
Al Viro7baa5092021-05-02 11:35:03 -0400767 iterate_and_advance(i, bytes, base, len, off,
768 copyin(addr + off, base, len),
769 memcpy(addr + off, base, len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500770 )
771
772 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400773}
Al Viroaa28de22017-06-29 21:45:10 -0400774EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400775
Al Viroaa28de22017-06-29 21:45:10 -0400776size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500777{
David Howells00e23702018-10-22 13:07:28 +0100778 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400779 WARN_ON(1);
780 return 0;
781 }
Al Viro7baa5092021-05-02 11:35:03 -0400782 iterate_and_advance(i, bytes, base, len, off,
783 __copy_from_user_inatomic_nocache(addr + off, base, len),
784 memcpy(addr + off, base, len)
Al Viroaa583092014-11-27 20:27:08 -0500785 )
786
787 return bytes;
788}
Al Viroaa28de22017-06-29 21:45:10 -0400789EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500790
Dan Williams0aed55a2017-05-29 12:22:50 -0700791#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700792/**
793 * _copy_from_iter_flushcache - write destination through cpu cache
794 * @addr: destination kernel address
795 * @bytes: total transfer length
Randy Dunlap44e55992021-09-07 19:58:54 -0700796 * @i: source iterator
Dan Williamsabd08d72018-07-08 13:46:07 -0700797 *
798 * The pmem driver arranges for filesystem-dax to use this facility via
799 * dax_copy_from_iter() for ensuring that writes to persistent memory
800 * are flushed through the CPU cache. It is differentiated from
801 * _copy_from_iter_nocache() in that guarantees all data is flushed for
802 * all iterator types. The _copy_from_iter_nocache() only attempts to
803 * bypass the cache for the ITER_IOVEC case, and on some archs may use
804 * instructions that strand dirty-data in the cache.
Randy Dunlap44e55992021-09-07 19:58:54 -0700805 *
806 * Return: number of bytes copied (may be %0)
Dan Williamsabd08d72018-07-08 13:46:07 -0700807 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700808size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700809{
David Howells00e23702018-10-22 13:07:28 +0100810 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700811 WARN_ON(1);
812 return 0;
813 }
Al Viro7baa5092021-05-02 11:35:03 -0400814 iterate_and_advance(i, bytes, base, len, off,
815 __copy_from_user_flushcache(addr + off, base, len),
816 memcpy_flushcache(addr + off, base, len)
Dan Williams0aed55a2017-05-29 12:22:50 -0700817 )
818
819 return bytes;
820}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700821EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700822#endif
823
Al Viro72e809e2017-06-29 21:52:57 -0400824static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
825{
Eric Dumazet6daef952019-02-26 10:42:39 -0800826 struct page *head;
827 size_t v = n + offset;
828
829 /*
830 * The general case needs to access the page order in order
831 * to compute the page size.
832 * However, we mostly deal with order-0 pages and thus can
833 * avoid a possible cache line miss for requests that fit all
834 * page orders.
835 */
836 if (n <= v && v <= PAGE_SIZE)
837 return true;
838
839 head = compound_head(page);
840 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700841
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700842 if (likely(n <= v && v <= (page_size(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400843 return true;
844 WARN_ON(1);
845 return false;
846}
Al Virocbbd26b2016-11-01 22:09:04 -0400847
Al Viro08aa6472021-04-29 20:42:25 -0400848static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
Al Virod2715242014-11-27 14:22:37 -0500849 struct iov_iter *i)
850{
Al Viro28f38db2021-06-02 17:25:59 -0400851 if (likely(iter_is_iovec(i)))
852 return copy_page_to_iter_iovec(page, offset, bytes, i);
853 if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
Al Viroc1d4d6a2021-04-27 12:29:53 -0400854 void *kaddr = kmap_local_page(page);
855 size_t wanted = _copy_to_iter(kaddr + offset, bytes, i);
856 kunmap_local(kaddr);
Al Virod2715242014-11-27 14:22:37 -0500857 return wanted;
Al Viro28f38db2021-06-02 17:25:59 -0400858 }
859 if (iov_iter_is_pipe(i))
860 return copy_page_to_iter_pipe(page, offset, bytes, i);
861 if (unlikely(iov_iter_is_discard(i))) {
Al Viroa506abc2021-04-27 12:34:04 -0400862 if (unlikely(i->count < bytes))
863 bytes = i->count;
864 i->count -= bytes;
David Howells9ea9ce02018-10-20 00:57:56 +0100865 return bytes;
Al Viro28f38db2021-06-02 17:25:59 -0400866 }
867 WARN_ON(1);
868 return 0;
Al Virod2715242014-11-27 14:22:37 -0500869}
Al Viro08aa6472021-04-29 20:42:25 -0400870
871size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
872 struct iov_iter *i)
873{
874 size_t res = 0;
875 if (unlikely(!page_copy_sane(page, offset, bytes)))
876 return 0;
877 page += offset / PAGE_SIZE; // first subpage
878 offset %= PAGE_SIZE;
879 while (1) {
880 size_t n = __copy_page_to_iter(page, offset,
881 min(bytes, (size_t)PAGE_SIZE - offset), i);
882 res += n;
883 bytes -= n;
884 if (!bytes || !n)
885 break;
886 offset += n;
887 if (offset == PAGE_SIZE) {
888 page++;
889 offset = 0;
890 }
891 }
892 return res;
893}
Al Virod2715242014-11-27 14:22:37 -0500894EXPORT_SYMBOL(copy_page_to_iter);
895
896size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
897 struct iov_iter *i)
898{
Al Viro72e809e2017-06-29 21:52:57 -0400899 if (unlikely(!page_copy_sane(page, offset, bytes)))
900 return 0;
Al Viro28f38db2021-06-02 17:25:59 -0400901 if (likely(iter_is_iovec(i)))
902 return copy_page_from_iter_iovec(page, offset, bytes, i);
903 if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
Al Viro55ca3752021-04-27 12:33:24 -0400904 void *kaddr = kmap_local_page(page);
Al Viroaa28de22017-06-29 21:45:10 -0400905 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Viro55ca3752021-04-27 12:33:24 -0400906 kunmap_local(kaddr);
Al Virod2715242014-11-27 14:22:37 -0500907 return wanted;
Al Viro28f38db2021-06-02 17:25:59 -0400908 }
909 WARN_ON(1);
910 return 0;
Al Virod2715242014-11-27 14:22:37 -0500911}
912EXPORT_SYMBOL(copy_page_from_iter);
913
Al Viro241699c2016-09-22 16:33:12 -0400914static size_t pipe_zero(size_t bytes, struct iov_iter *i)
915{
916 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000917 unsigned int p_mask = pipe->ring_size - 1;
918 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400919 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400920
921 if (!sanity(i))
922 return 0;
923
David Howells8cefc102019-11-15 13:30:32 +0000924 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400925 if (unlikely(!n))
926 return 0;
927
David Howells8cefc102019-11-15 13:30:32 +0000928 do {
Al Viro241699c2016-09-22 16:33:12 -0400929 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
Al Viro893839f2021-04-30 18:39:25 -0400930 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
931 memset(p + off, 0, chunk);
932 kunmap_local(p);
David Howells8cefc102019-11-15 13:30:32 +0000933 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400934 i->iov_offset = off + chunk;
935 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000936 off = 0;
937 i_head++;
938 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400939 i->count -= bytes;
940 return bytes;
941}
942
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400943size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
944{
David Howells00e23702018-10-22 13:07:28 +0100945 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400946 return pipe_zero(bytes, i);
Al Viro7baa5092021-05-02 11:35:03 -0400947 iterate_and_advance(i, bytes, base, len, count,
948 clear_user(base, len),
949 memset(base, 0, len)
Al Viro8442fa42014-11-27 14:18:54 -0500950 )
951
952 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400953}
954EXPORT_SYMBOL(iov_iter_zero);
955
Al Virof0b65f32021-04-30 10:26:41 -0400956size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
957 struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400958{
Al Viro04a31162014-11-27 13:51:41 -0500959 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400960 if (unlikely(!page_copy_sane(page, offset, bytes))) {
961 kunmap_atomic(kaddr);
962 return 0;
963 }
David Howells9ea9ce02018-10-20 00:57:56 +0100964 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400965 kunmap_atomic(kaddr);
966 WARN_ON(1);
967 return 0;
968 }
Al Viro7baa5092021-05-02 11:35:03 -0400969 iterate_and_advance(i, bytes, base, len, off,
970 copyin(p + off, base, len),
971 memcpy(p + off, base, len)
Al Viro04a31162014-11-27 13:51:41 -0500972 )
973 kunmap_atomic(kaddr);
974 return bytes;
Al Viro62a80672014-04-04 23:12:29 -0400975}
Al Virof0b65f32021-04-30 10:26:41 -0400976EXPORT_SYMBOL(copy_page_from_iter_atomic);
Al Viro62a80672014-04-04 23:12:29 -0400977
Al Virob9dc6f62017-01-14 19:33:08 -0500978static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -0400979{
980 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000981 unsigned int p_tail = pipe->tail;
982 unsigned int p_head = pipe->head;
983 unsigned int p_mask = pipe->ring_size - 1;
984
985 if (!pipe_empty(p_head, p_tail)) {
986 struct pipe_buffer *buf;
987 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -0500988 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000989
Al Virob9dc6f62017-01-14 19:33:08 -0500990 if (off) {
David Howells8cefc102019-11-15 13:30:32 +0000991 buf = &pipe->bufs[i_head & p_mask];
992 buf->len = off - buf->offset;
993 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -0500994 }
David Howells8cefc102019-11-15 13:30:32 +0000995 while (p_head != i_head) {
996 p_head--;
997 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
Al Viro241699c2016-09-22 16:33:12 -0400998 }
David Howells8cefc102019-11-15 13:30:32 +0000999
1000 pipe->head = p_head;
Al Viro241699c2016-09-22 16:33:12 -04001001 }
Al Virob9dc6f62017-01-14 19:33:08 -05001002}
1003
1004static void pipe_advance(struct iov_iter *i, size_t size)
1005{
1006 struct pipe_inode_info *pipe = i->pipe;
Al Virob9dc6f62017-01-14 19:33:08 -05001007 if (size) {
1008 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +00001009 unsigned int p_mask = pipe->ring_size - 1;
1010 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001011 size_t off = i->iov_offset, left = size;
David Howells8cefc102019-11-15 13:30:32 +00001012
Al Virob9dc6f62017-01-14 19:33:08 -05001013 if (off) /* make it relative to the beginning of buffer */
David Howells8cefc102019-11-15 13:30:32 +00001014 left += off - pipe->bufs[i_head & p_mask].offset;
Al Virob9dc6f62017-01-14 19:33:08 -05001015 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001016 buf = &pipe->bufs[i_head & p_mask];
Al Virob9dc6f62017-01-14 19:33:08 -05001017 if (left <= buf->len)
1018 break;
1019 left -= buf->len;
David Howells8cefc102019-11-15 13:30:32 +00001020 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001021 }
David Howells8cefc102019-11-15 13:30:32 +00001022 i->head = i_head;
Al Virob9dc6f62017-01-14 19:33:08 -05001023 i->iov_offset = buf->offset + left;
1024 }
1025 i->count -= size;
1026 /* ... and discard everything past that point */
1027 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001028}
1029
Pavel Begunkov54c81952021-01-09 16:03:01 +00001030static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
1031{
1032 struct bvec_iter bi;
1033
1034 bi.bi_size = i->count;
1035 bi.bi_bvec_done = i->iov_offset;
1036 bi.bi_idx = 0;
1037 bvec_iter_advance(i->bvec, &bi, size);
1038
1039 i->bvec += bi.bi_idx;
1040 i->nr_segs -= bi.bi_idx;
1041 i->count = bi.bi_size;
1042 i->iov_offset = bi.bi_bvec_done;
1043}
1044
Al Viro185ac4d2021-04-23 12:58:53 -04001045static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
1046{
1047 const struct iovec *iov, *end;
1048
1049 if (!i->count)
1050 return;
1051 i->count -= size;
1052
1053 size += i->iov_offset; // from beginning of current segment
1054 for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
1055 if (likely(size < iov->iov_len))
1056 break;
1057 size -= iov->iov_len;
1058 }
1059 i->iov_offset = size;
1060 i->nr_segs -= iov - i->iov;
1061 i->iov = iov;
1062}
1063
Al Viro62a80672014-04-04 23:12:29 -04001064void iov_iter_advance(struct iov_iter *i, size_t size)
1065{
Al Viro3b3fc052021-04-23 22:24:08 -04001066 if (unlikely(i->count < size))
1067 size = i->count;
Al Viro185ac4d2021-04-23 12:58:53 -04001068 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
1069 /* iovec and kvec have identical layouts */
1070 iov_iter_iovec_advance(i, size);
1071 } else if (iov_iter_is_bvec(i)) {
1072 iov_iter_bvec_advance(i, size);
1073 } else if (iov_iter_is_pipe(i)) {
Al Viro241699c2016-09-22 16:33:12 -04001074 pipe_advance(i, size);
Al Viro185ac4d2021-04-23 12:58:53 -04001075 } else if (unlikely(iov_iter_is_xarray(i))) {
David Howells7ff5062072020-02-10 10:00:21 +00001076 i->iov_offset += size;
1077 i->count -= size;
Al Viro185ac4d2021-04-23 12:58:53 -04001078 } else if (iov_iter_is_discard(i)) {
1079 i->count -= size;
David Howells7ff5062072020-02-10 10:00:21 +00001080 }
Al Viro62a80672014-04-04 23:12:29 -04001081}
1082EXPORT_SYMBOL(iov_iter_advance);
1083
Al Viro27c0e372017-02-17 18:42:24 -05001084void iov_iter_revert(struct iov_iter *i, size_t unroll)
1085{
1086 if (!unroll)
1087 return;
Al Viro5b47d592017-05-08 13:54:47 -04001088 if (WARN_ON(unroll > MAX_RW_COUNT))
1089 return;
Al Viro27c0e372017-02-17 18:42:24 -05001090 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001091 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001092 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001093 unsigned int p_mask = pipe->ring_size - 1;
1094 unsigned int i_head = i->head;
Al Viro27c0e372017-02-17 18:42:24 -05001095 size_t off = i->iov_offset;
1096 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001097 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1098 size_t n = off - b->offset;
Al Viro27c0e372017-02-17 18:42:24 -05001099 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001100 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001101 break;
1102 }
1103 unroll -= n;
David Howells8cefc102019-11-15 13:30:32 +00001104 if (!unroll && i_head == i->start_head) {
Al Viro27c0e372017-02-17 18:42:24 -05001105 off = 0;
1106 break;
1107 }
David Howells8cefc102019-11-15 13:30:32 +00001108 i_head--;
1109 b = &pipe->bufs[i_head & p_mask];
1110 off = b->offset + b->len;
Al Viro27c0e372017-02-17 18:42:24 -05001111 }
1112 i->iov_offset = off;
David Howells8cefc102019-11-15 13:30:32 +00001113 i->head = i_head;
Al Viro27c0e372017-02-17 18:42:24 -05001114 pipe_truncate(i);
1115 return;
1116 }
David Howells9ea9ce02018-10-20 00:57:56 +01001117 if (unlikely(iov_iter_is_discard(i)))
1118 return;
Al Viro27c0e372017-02-17 18:42:24 -05001119 if (unroll <= i->iov_offset) {
1120 i->iov_offset -= unroll;
1121 return;
1122 }
1123 unroll -= i->iov_offset;
David Howells7ff5062072020-02-10 10:00:21 +00001124 if (iov_iter_is_xarray(i)) {
1125 BUG(); /* We should never go beyond the start of the specified
1126 * range since we might then be straying into pages that
1127 * aren't pinned.
1128 */
1129 } else if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001130 const struct bio_vec *bvec = i->bvec;
1131 while (1) {
1132 size_t n = (--bvec)->bv_len;
1133 i->nr_segs++;
1134 if (unroll <= n) {
1135 i->bvec = bvec;
1136 i->iov_offset = n - unroll;
1137 return;
1138 }
1139 unroll -= n;
1140 }
1141 } else { /* same logics for iovec and kvec */
1142 const struct iovec *iov = i->iov;
1143 while (1) {
1144 size_t n = (--iov)->iov_len;
1145 i->nr_segs++;
1146 if (unroll <= n) {
1147 i->iov = iov;
1148 i->iov_offset = n - unroll;
1149 return;
1150 }
1151 unroll -= n;
1152 }
1153 }
1154}
1155EXPORT_SYMBOL(iov_iter_revert);
1156
Al Viro62a80672014-04-04 23:12:29 -04001157/*
1158 * Return the count of just the current iov_iter segment.
1159 */
1160size_t iov_iter_single_seg_count(const struct iov_iter *i)
1161{
Al Viro28f38db2021-06-02 17:25:59 -04001162 if (i->nr_segs > 1) {
1163 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1164 return min(i->count, i->iov->iov_len - i->iov_offset);
1165 if (iov_iter_is_bvec(i))
1166 return min(i->count, i->bvec->bv_len - i->iov_offset);
1167 }
1168 return i->count;
Al Viro62a80672014-04-04 23:12:29 -04001169}
1170EXPORT_SYMBOL(iov_iter_single_seg_count);
1171
David Howellsaa563d72018-10-20 00:57:56 +01001172void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001173 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001174 size_t count)
1175{
David Howellsaa563d72018-10-20 00:57:56 +01001176 WARN_ON(direction & ~(READ | WRITE));
Al Viro8cd54c12021-04-22 14:50:39 -04001177 *i = (struct iov_iter){
1178 .iter_type = ITER_KVEC,
1179 .data_source = direction,
1180 .kvec = kvec,
1181 .nr_segs = nr_segs,
1182 .iov_offset = 0,
1183 .count = count
1184 };
Al Viroabb78f82014-11-24 14:46:11 -05001185}
1186EXPORT_SYMBOL(iov_iter_kvec);
1187
David Howellsaa563d72018-10-20 00:57:56 +01001188void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001189 const struct bio_vec *bvec, unsigned long nr_segs,
1190 size_t count)
1191{
David Howellsaa563d72018-10-20 00:57:56 +01001192 WARN_ON(direction & ~(READ | WRITE));
Al Viro8cd54c12021-04-22 14:50:39 -04001193 *i = (struct iov_iter){
1194 .iter_type = ITER_BVEC,
1195 .data_source = direction,
1196 .bvec = bvec,
1197 .nr_segs = nr_segs,
1198 .iov_offset = 0,
1199 .count = count
1200 };
Al Viro05afcb72015-01-23 01:08:07 -05001201}
1202EXPORT_SYMBOL(iov_iter_bvec);
1203
David Howellsaa563d72018-10-20 00:57:56 +01001204void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001205 struct pipe_inode_info *pipe,
1206 size_t count)
1207{
David Howellsaa563d72018-10-20 00:57:56 +01001208 BUG_ON(direction != READ);
David Howells8cefc102019-11-15 13:30:32 +00001209 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
Al Viro8cd54c12021-04-22 14:50:39 -04001210 *i = (struct iov_iter){
1211 .iter_type = ITER_PIPE,
1212 .data_source = false,
1213 .pipe = pipe,
1214 .head = pipe->head,
1215 .start_head = pipe->head,
1216 .iov_offset = 0,
1217 .count = count
1218 };
Al Viro241699c2016-09-22 16:33:12 -04001219}
1220EXPORT_SYMBOL(iov_iter_pipe);
1221
David Howells9ea9ce02018-10-20 00:57:56 +01001222/**
David Howells7ff5062072020-02-10 10:00:21 +00001223 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1224 * @i: The iterator to initialise.
1225 * @direction: The direction of the transfer.
1226 * @xarray: The xarray to access.
1227 * @start: The start file position.
1228 * @count: The size of the I/O buffer in bytes.
1229 *
1230 * Set up an I/O iterator to either draw data out of the pages attached to an
1231 * inode or to inject data into those pages. The pages *must* be prevented
1232 * from evaporation, either by taking a ref on them or locking them by the
1233 * caller.
1234 */
1235void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1236 struct xarray *xarray, loff_t start, size_t count)
1237{
1238 BUG_ON(direction & ~1);
Al Viro8cd54c12021-04-22 14:50:39 -04001239 *i = (struct iov_iter) {
1240 .iter_type = ITER_XARRAY,
1241 .data_source = direction,
1242 .xarray = xarray,
1243 .xarray_start = start,
1244 .count = count,
1245 .iov_offset = 0
1246 };
David Howells7ff5062072020-02-10 10:00:21 +00001247}
1248EXPORT_SYMBOL(iov_iter_xarray);
1249
1250/**
David Howells9ea9ce02018-10-20 00:57:56 +01001251 * iov_iter_discard - Initialise an I/O iterator that discards data
1252 * @i: The iterator to initialise.
1253 * @direction: The direction of the transfer.
1254 * @count: The size of the I/O buffer in bytes.
1255 *
1256 * Set up an I/O iterator that just discards everything that's written to it.
1257 * It's only available as a READ iterator.
1258 */
1259void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1260{
1261 BUG_ON(direction != READ);
Al Viro8cd54c12021-04-22 14:50:39 -04001262 *i = (struct iov_iter){
1263 .iter_type = ITER_DISCARD,
1264 .data_source = false,
1265 .count = count,
1266 .iov_offset = 0
1267 };
David Howells9ea9ce02018-10-20 00:57:56 +01001268}
1269EXPORT_SYMBOL(iov_iter_discard);
1270
Al Viro9221d2e2021-04-25 00:44:35 -04001271static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -04001272{
Al Viro04a31162014-11-27 13:51:41 -05001273 unsigned long res = 0;
1274 size_t size = i->count;
Al Viro9221d2e2021-04-25 00:44:35 -04001275 size_t skip = i->iov_offset;
1276 unsigned k;
Al Viro04a31162014-11-27 13:51:41 -05001277
Al Viro9221d2e2021-04-25 00:44:35 -04001278 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1279 size_t len = i->iov[k].iov_len - skip;
1280 if (len) {
1281 res |= (unsigned long)i->iov[k].iov_base + skip;
1282 if (len > size)
1283 len = size;
1284 res |= len;
1285 size -= len;
1286 if (!size)
1287 break;
1288 }
1289 }
1290 return res;
1291}
1292
1293static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
1294{
1295 unsigned res = 0;
1296 size_t size = i->count;
1297 unsigned skip = i->iov_offset;
1298 unsigned k;
1299
1300 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1301 size_t len = i->bvec[k].bv_len - skip;
1302 res |= (unsigned long)i->bvec[k].bv_offset + skip;
1303 if (len > size)
1304 len = size;
1305 res |= len;
1306 size -= len;
1307 if (!size)
1308 break;
1309 }
1310 return res;
1311}
1312
1313unsigned long iov_iter_alignment(const struct iov_iter *i)
1314{
1315 /* iovec and kvec have identical layouts */
1316 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1317 return iov_iter_alignment_iovec(i);
1318
1319 if (iov_iter_is_bvec(i))
1320 return iov_iter_alignment_bvec(i);
1321
1322 if (iov_iter_is_pipe(i)) {
Jan Karae0ff1262019-12-16 11:54:32 +01001323 unsigned int p_mask = i->pipe->ring_size - 1;
Al Viro9221d2e2021-04-25 00:44:35 -04001324 size_t size = i->count;
Jan Karae0ff1262019-12-16 11:54:32 +01001325
David Howells8cefc102019-11-15 13:30:32 +00001326 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
Al Viro241699c2016-09-22 16:33:12 -04001327 return size | i->iov_offset;
1328 return size;
1329 }
Al Viro9221d2e2021-04-25 00:44:35 -04001330
1331 if (iov_iter_is_xarray(i))
David Howells3d14ec12021-04-25 22:02:38 +01001332 return (i->xarray_start + i->iov_offset) | i->count;
Al Viro9221d2e2021-04-25 00:44:35 -04001333
1334 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001335}
1336EXPORT_SYMBOL(iov_iter_alignment);
1337
Al Viro357f4352016-04-08 19:05:19 -04001338unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1339{
Al Viro33844e62016-12-21 21:55:02 -05001340 unsigned long res = 0;
Al Viro610c7a72021-04-25 01:03:16 -04001341 unsigned long v = 0;
Al Viro357f4352016-04-08 19:05:19 -04001342 size_t size = i->count;
Al Viro610c7a72021-04-25 01:03:16 -04001343 unsigned k;
Al Viro357f4352016-04-08 19:05:19 -04001344
Al Viro610c7a72021-04-25 01:03:16 -04001345 if (WARN_ON(!iter_is_iovec(i)))
Al Viro241699c2016-09-22 16:33:12 -04001346 return ~0U;
Al Viro241699c2016-09-22 16:33:12 -04001347
Al Viro610c7a72021-04-25 01:03:16 -04001348 for (k = 0; k < i->nr_segs; k++) {
1349 if (i->iov[k].iov_len) {
1350 unsigned long base = (unsigned long)i->iov[k].iov_base;
1351 if (v) // if not the first one
1352 res |= base | v; // this start | previous end
1353 v = base + i->iov[k].iov_len;
1354 if (size <= i->iov[k].iov_len)
1355 break;
1356 size -= i->iov[k].iov_len;
1357 }
1358 }
Al Viro33844e62016-12-21 21:55:02 -05001359 return res;
Al Viro357f4352016-04-08 19:05:19 -04001360}
1361EXPORT_SYMBOL(iov_iter_gap_alignment);
1362
Ilya Dryomove76b63122018-05-02 20:16:56 +02001363static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001364 size_t maxsize,
1365 struct page **pages,
David Howells8cefc102019-11-15 13:30:32 +00001366 int iter_head,
Al Viro241699c2016-09-22 16:33:12 -04001367 size_t *start)
1368{
1369 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001370 unsigned int p_mask = pipe->ring_size - 1;
1371 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001372 if (!n)
1373 return -EFAULT;
1374
1375 maxsize = n;
1376 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001377 while (n > 0) {
David Howells8cefc102019-11-15 13:30:32 +00001378 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1379 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -04001380 n -= PAGE_SIZE;
1381 }
1382
1383 return maxsize;
1384}
1385
1386static ssize_t pipe_get_pages(struct iov_iter *i,
1387 struct page **pages, size_t maxsize, unsigned maxpages,
1388 size_t *start)
1389{
David Howells8cefc102019-11-15 13:30:32 +00001390 unsigned int iter_head, npages;
Al Viro241699c2016-09-22 16:33:12 -04001391 size_t capacity;
Al Viro241699c2016-09-22 16:33:12 -04001392
1393 if (!sanity(i))
1394 return -EFAULT;
1395
David Howells8cefc102019-11-15 13:30:32 +00001396 data_start(i, &iter_head, start);
1397 /* Amount of free space: some of this one + all after this one */
1398 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1399 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
Al Viro241699c2016-09-22 16:33:12 -04001400
David Howells8cefc102019-11-15 13:30:32 +00001401 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001402}
1403
David Howells7ff5062072020-02-10 10:00:21 +00001404static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1405 pgoff_t index, unsigned int nr_pages)
1406{
1407 XA_STATE(xas, xa, index);
1408 struct page *page;
1409 unsigned int ret = 0;
1410
1411 rcu_read_lock();
1412 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1413 if (xas_retry(&xas, page))
1414 continue;
1415
1416 /* Has the page moved or been split? */
1417 if (unlikely(page != xas_reload(&xas))) {
1418 xas_reset(&xas);
1419 continue;
1420 }
1421
1422 pages[ret] = find_subpage(page, xas.xa_index);
1423 get_page(pages[ret]);
1424 if (++ret == nr_pages)
1425 break;
1426 }
1427 rcu_read_unlock();
1428 return ret;
1429}
1430
1431static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1432 struct page **pages, size_t maxsize,
1433 unsigned maxpages, size_t *_start_offset)
1434{
1435 unsigned nr, offset;
1436 pgoff_t index, count;
1437 size_t size = maxsize, actual;
1438 loff_t pos;
1439
1440 if (!size || !maxpages)
1441 return 0;
1442
1443 pos = i->xarray_start + i->iov_offset;
1444 index = pos >> PAGE_SHIFT;
1445 offset = pos & ~PAGE_MASK;
1446 *_start_offset = offset;
1447
1448 count = 1;
1449 if (size > PAGE_SIZE - offset) {
1450 size -= PAGE_SIZE - offset;
1451 count += size >> PAGE_SHIFT;
1452 size &= ~PAGE_MASK;
1453 if (size)
1454 count++;
1455 }
1456
1457 if (count > maxpages)
1458 count = maxpages;
1459
1460 nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
1461 if (nr == 0)
1462 return 0;
1463
1464 actual = PAGE_SIZE * nr;
1465 actual -= offset;
1466 if (nr == count && size > 0) {
1467 unsigned last_offset = (nr > 1) ? 0 : offset;
1468 actual -= PAGE_SIZE - (last_offset + size);
1469 }
1470 return actual;
1471}
1472
Al Viro3d671ca2021-04-25 09:14:44 -04001473/* must be done on non-empty ITER_IOVEC one */
1474static unsigned long first_iovec_segment(const struct iov_iter *i,
1475 size_t *size, size_t *start,
1476 size_t maxsize, unsigned maxpages)
1477{
1478 size_t skip;
1479 long k;
1480
1481 for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1482 unsigned long addr = (unsigned long)i->iov[k].iov_base + skip;
1483 size_t len = i->iov[k].iov_len - skip;
1484
1485 if (unlikely(!len))
1486 continue;
1487 if (len > maxsize)
1488 len = maxsize;
1489 len += (*start = addr % PAGE_SIZE);
1490 if (len > maxpages * PAGE_SIZE)
1491 len = maxpages * PAGE_SIZE;
1492 *size = len;
1493 return addr & PAGE_MASK;
1494 }
1495 BUG(); // if it had been empty, we wouldn't get called
1496}
1497
1498/* must be done on non-empty ITER_BVEC one */
1499static struct page *first_bvec_segment(const struct iov_iter *i,
1500 size_t *size, size_t *start,
1501 size_t maxsize, unsigned maxpages)
1502{
1503 struct page *page;
1504 size_t skip = i->iov_offset, len;
1505
1506 len = i->bvec->bv_len - skip;
1507 if (len > maxsize)
1508 len = maxsize;
1509 skip += i->bvec->bv_offset;
1510 page = i->bvec->bv_page + skip / PAGE_SIZE;
1511 len += (*start = skip % PAGE_SIZE);
1512 if (len > maxpages * PAGE_SIZE)
1513 len = maxpages * PAGE_SIZE;
1514 *size = len;
1515 return page;
1516}
1517
Al Viro62a80672014-04-04 23:12:29 -04001518ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001519 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001520 size_t *start)
1521{
Al Viro3d671ca2021-04-25 09:14:44 -04001522 size_t len;
1523 int n, res;
1524
Al Viroe5393fa2014-11-27 14:12:09 -05001525 if (maxsize > i->count)
1526 maxsize = i->count;
Al Viro3d671ca2021-04-25 09:14:44 -04001527 if (!maxsize)
1528 return 0;
Al Viroe5393fa2014-11-27 14:12:09 -05001529
Al Viro3d671ca2021-04-25 09:14:44 -04001530 if (likely(iter_is_iovec(i))) {
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001531 unsigned int gup_flags = 0;
Al Viro3d671ca2021-04-25 09:14:44 -04001532 unsigned long addr;
David Howells9ea9ce02018-10-20 00:57:56 +01001533
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001534 if (iov_iter_rw(i) != WRITE)
1535 gup_flags |= FOLL_WRITE;
1536 if (i->nofault)
1537 gup_flags |= FOLL_NOFAULT;
1538
Al Viro3d671ca2021-04-25 09:14:44 -04001539 addr = first_iovec_segment(i, &len, start, maxsize, maxpages);
Al Viroe5393fa2014-11-27 14:12:09 -05001540 n = DIV_ROUND_UP(len, PAGE_SIZE);
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001541 res = get_user_pages_fast(addr, n, gup_flags, pages);
Andreas Gruenbacher814a6672021-07-21 19:03:47 +02001542 if (unlikely(res <= 0))
Al Viroe5393fa2014-11-27 14:12:09 -05001543 return res;
1544 return (res == n ? len : res * PAGE_SIZE) - *start;
Al Viro3d671ca2021-04-25 09:14:44 -04001545 }
1546 if (iov_iter_is_bvec(i)) {
1547 struct page *page;
1548
1549 page = first_bvec_segment(i, &len, start, maxsize, maxpages);
1550 n = DIV_ROUND_UP(len, PAGE_SIZE);
1551 while (n--)
1552 get_page(*pages++ = page++);
1553 return len - *start;
1554 }
1555 if (iov_iter_is_pipe(i))
1556 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1557 if (iov_iter_is_xarray(i))
1558 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1559 return -EFAULT;
Al Viro62a80672014-04-04 23:12:29 -04001560}
1561EXPORT_SYMBOL(iov_iter_get_pages);
1562
Al Viro1b17f1f2014-11-27 14:14:31 -05001563static struct page **get_pages_array(size_t n)
1564{
Michal Hocko752ade62017-05-08 15:57:27 -07001565 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001566}
1567
Al Viro241699c2016-09-22 16:33:12 -04001568static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1569 struct page ***pages, size_t maxsize,
1570 size_t *start)
1571{
1572 struct page **p;
David Howells8cefc102019-11-15 13:30:32 +00001573 unsigned int iter_head, npages;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001574 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001575
1576 if (!sanity(i))
1577 return -EFAULT;
1578
David Howells8cefc102019-11-15 13:30:32 +00001579 data_start(i, &iter_head, start);
1580 /* Amount of free space: some of this one + all after this one */
1581 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
Al Viro241699c2016-09-22 16:33:12 -04001582 n = npages * PAGE_SIZE - *start;
1583 if (maxsize > n)
1584 maxsize = n;
1585 else
1586 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1587 p = get_pages_array(npages);
1588 if (!p)
1589 return -ENOMEM;
David Howells8cefc102019-11-15 13:30:32 +00001590 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001591 if (n > 0)
1592 *pages = p;
1593 else
1594 kvfree(p);
1595 return n;
1596}
1597
David Howells7ff5062072020-02-10 10:00:21 +00001598static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
1599 struct page ***pages, size_t maxsize,
1600 size_t *_start_offset)
1601{
1602 struct page **p;
1603 unsigned nr, offset;
1604 pgoff_t index, count;
1605 size_t size = maxsize, actual;
1606 loff_t pos;
1607
1608 if (!size)
1609 return 0;
1610
1611 pos = i->xarray_start + i->iov_offset;
1612 index = pos >> PAGE_SHIFT;
1613 offset = pos & ~PAGE_MASK;
1614 *_start_offset = offset;
1615
1616 count = 1;
1617 if (size > PAGE_SIZE - offset) {
1618 size -= PAGE_SIZE - offset;
1619 count += size >> PAGE_SHIFT;
1620 size &= ~PAGE_MASK;
1621 if (size)
1622 count++;
1623 }
1624
1625 p = get_pages_array(count);
1626 if (!p)
1627 return -ENOMEM;
1628 *pages = p;
1629
1630 nr = iter_xarray_populate_pages(p, i->xarray, index, count);
1631 if (nr == 0)
1632 return 0;
1633
1634 actual = PAGE_SIZE * nr;
1635 actual -= offset;
1636 if (nr == count && size > 0) {
1637 unsigned last_offset = (nr > 1) ? 0 : offset;
1638 actual -= PAGE_SIZE - (last_offset + size);
1639 }
1640 return actual;
1641}
1642
Al Viro62a80672014-04-04 23:12:29 -04001643ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1644 struct page ***pages, size_t maxsize,
1645 size_t *start)
1646{
Al Viro1b17f1f2014-11-27 14:14:31 -05001647 struct page **p;
Al Viro3d671ca2021-04-25 09:14:44 -04001648 size_t len;
1649 int n, res;
Al Viro1b17f1f2014-11-27 14:14:31 -05001650
1651 if (maxsize > i->count)
1652 maxsize = i->count;
Al Viro3d671ca2021-04-25 09:14:44 -04001653 if (!maxsize)
1654 return 0;
Al Viro1b17f1f2014-11-27 14:14:31 -05001655
Al Viro3d671ca2021-04-25 09:14:44 -04001656 if (likely(iter_is_iovec(i))) {
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001657 unsigned int gup_flags = 0;
Al Viro3d671ca2021-04-25 09:14:44 -04001658 unsigned long addr;
David Howells9ea9ce02018-10-20 00:57:56 +01001659
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001660 if (iov_iter_rw(i) != WRITE)
1661 gup_flags |= FOLL_WRITE;
1662 if (i->nofault)
1663 gup_flags |= FOLL_NOFAULT;
1664
Al Viro3d671ca2021-04-25 09:14:44 -04001665 addr = first_iovec_segment(i, &len, start, maxsize, ~0U);
Al Viro1b17f1f2014-11-27 14:14:31 -05001666 n = DIV_ROUND_UP(len, PAGE_SIZE);
1667 p = get_pages_array(n);
1668 if (!p)
1669 return -ENOMEM;
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001670 res = get_user_pages_fast(addr, n, gup_flags, p);
Andreas Gruenbacher814a6672021-07-21 19:03:47 +02001671 if (unlikely(res <= 0)) {
Al Viro1b17f1f2014-11-27 14:14:31 -05001672 kvfree(p);
Andreas Gruenbacher814a6672021-07-21 19:03:47 +02001673 *pages = NULL;
Al Viro1b17f1f2014-11-27 14:14:31 -05001674 return res;
1675 }
1676 *pages = p;
1677 return (res == n ? len : res * PAGE_SIZE) - *start;
Al Viro3d671ca2021-04-25 09:14:44 -04001678 }
1679 if (iov_iter_is_bvec(i)) {
1680 struct page *page;
1681
1682 page = first_bvec_segment(i, &len, start, maxsize, ~0U);
1683 n = DIV_ROUND_UP(len, PAGE_SIZE);
1684 *pages = p = get_pages_array(n);
Al Viro1b17f1f2014-11-27 14:14:31 -05001685 if (!p)
1686 return -ENOMEM;
Al Viro3d671ca2021-04-25 09:14:44 -04001687 while (n--)
1688 get_page(*p++ = page++);
1689 return len - *start;
1690 }
1691 if (iov_iter_is_pipe(i))
1692 return pipe_get_pages_alloc(i, pages, maxsize, start);
1693 if (iov_iter_is_xarray(i))
1694 return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
1695 return -EFAULT;
Al Viro62a80672014-04-04 23:12:29 -04001696}
1697EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1698
Al Viroa604ec72014-11-24 01:08:00 -05001699size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1700 struct iov_iter *i)
1701{
Al Viroa604ec72014-11-24 01:08:00 -05001702 __wsum sum, next;
Al Viroa604ec72014-11-24 01:08:00 -05001703 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001704 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001705 WARN_ON(1);
1706 return 0;
1707 }
Al Viro7baa5092021-05-02 11:35:03 -04001708 iterate_and_advance(i, bytes, base, len, off, ({
1709 next = csum_and_copy_from_user(base, addr + off, len);
Al Viro2495bdcc2021-04-30 13:40:48 -04001710 sum = csum_block_add(sum, next, off);
Al Viro7baa5092021-05-02 11:35:03 -04001711 next ? 0 : len;
Al Viroa604ec72014-11-24 01:08:00 -05001712 }), ({
Al Viro7baa5092021-05-02 11:35:03 -04001713 sum = csum_and_memcpy(addr + off, base, len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001714 })
1715 )
1716 *csum = sum;
1717 return bytes;
1718}
1719EXPORT_SYMBOL(csum_and_copy_from_iter);
1720
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001721size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
Al Viroa604ec72014-11-24 01:08:00 -05001722 struct iov_iter *i)
1723{
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001724 struct csum_state *csstate = _csstate;
Al Viroa604ec72014-11-24 01:08:00 -05001725 __wsum sum, next;
Al Viro78e1f382018-11-25 16:24:16 -05001726
Al Viro78e1f382018-11-25 16:24:16 -05001727 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001728 WARN_ON(1); /* for now */
1729 return 0;
1730 }
Al Viro6852df12021-05-02 17:24:40 -04001731
1732 sum = csum_shift(csstate->csum, csstate->off);
1733 if (unlikely(iov_iter_is_pipe(i)))
1734 bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
1735 else iterate_and_advance(i, bytes, base, len, off, ({
Al Viro7baa5092021-05-02 11:35:03 -04001736 next = csum_and_copy_to_user(addr + off, base, len);
Al Viro2495bdcc2021-04-30 13:40:48 -04001737 sum = csum_block_add(sum, next, off);
Al Viro7baa5092021-05-02 11:35:03 -04001738 next ? 0 : len;
Al Viroa604ec72014-11-24 01:08:00 -05001739 }), ({
Al Viro7baa5092021-05-02 11:35:03 -04001740 sum = csum_and_memcpy(base, addr + off, len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001741 })
1742 )
Al Viro594e4502021-06-05 10:19:30 -04001743 csstate->csum = csum_shift(sum, csstate->off);
1744 csstate->off += bytes;
Al Viroa604ec72014-11-24 01:08:00 -05001745 return bytes;
1746}
1747EXPORT_SYMBOL(csum_and_copy_to_iter);
1748
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001749size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1750 struct iov_iter *i)
1751{
Herbert Xu79990962020-06-12 16:57:37 +10001752#ifdef CONFIG_CRYPTO_HASH
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001753 struct ahash_request *hash = hashp;
1754 struct scatterlist sg;
1755 size_t copied;
1756
1757 copied = copy_to_iter(addr, bytes, i);
1758 sg_init_one(&sg, addr, copied);
1759 ahash_request_set_crypt(hash, &sg, NULL, copied);
1760 crypto_ahash_update(hash);
1761 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001762#else
1763 return 0;
1764#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001765}
1766EXPORT_SYMBOL(hash_and_copy_to_iter);
1767
Al Viro66531c62021-04-25 16:00:48 -04001768static int iov_npages(const struct iov_iter *i, int maxpages)
Al Viro62a80672014-04-04 23:12:29 -04001769{
Al Viro66531c62021-04-25 16:00:48 -04001770 size_t skip = i->iov_offset, size = i->count;
1771 const struct iovec *p;
Al Viroe0f2dc42014-11-27 14:09:46 -05001772 int npages = 0;
1773
Al Viro66531c62021-04-25 16:00:48 -04001774 for (p = i->iov; size; skip = 0, p++) {
1775 unsigned offs = offset_in_page(p->iov_base + skip);
1776 size_t len = min(p->iov_len - skip, size);
Al Viroe0f2dc42014-11-27 14:09:46 -05001777
Al Viro66531c62021-04-25 16:00:48 -04001778 if (len) {
1779 size -= len;
1780 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1781 if (unlikely(npages > maxpages))
1782 return maxpages;
1783 }
1784 }
1785 return npages;
1786}
1787
1788static int bvec_npages(const struct iov_iter *i, int maxpages)
1789{
1790 size_t skip = i->iov_offset, size = i->count;
1791 const struct bio_vec *p;
1792 int npages = 0;
1793
1794 for (p = i->bvec; size; skip = 0, p++) {
1795 unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
1796 size_t len = min(p->bv_len - skip, size);
1797
1798 size -= len;
1799 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1800 if (unlikely(npages > maxpages))
1801 return maxpages;
1802 }
1803 return npages;
1804}
1805
1806int iov_iter_npages(const struct iov_iter *i, int maxpages)
1807{
1808 if (unlikely(!i->count))
1809 return 0;
1810 /* iovec and kvec have identical layouts */
1811 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1812 return iov_npages(i, maxpages);
1813 if (iov_iter_is_bvec(i))
1814 return bvec_npages(i, maxpages);
1815 if (iov_iter_is_pipe(i)) {
David Howells8cefc102019-11-15 13:30:32 +00001816 unsigned int iter_head;
Al Viro66531c62021-04-25 16:00:48 -04001817 int npages;
Al Viro241699c2016-09-22 16:33:12 -04001818 size_t off;
Al Viro241699c2016-09-22 16:33:12 -04001819
1820 if (!sanity(i))
1821 return 0;
1822
David Howells8cefc102019-11-15 13:30:32 +00001823 data_start(i, &iter_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001824 /* some of this one + all after this one */
Al Viro66531c62021-04-25 16:00:48 -04001825 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1826 return min(npages, maxpages);
1827 }
1828 if (iov_iter_is_xarray(i)) {
Al Viroe4f8df82021-05-03 11:05:29 -04001829 unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1830 int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
Al Viro66531c62021-04-25 16:00:48 -04001831 return min(npages, maxpages);
1832 }
1833 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001834}
Al Virof67da302014-03-19 01:16:16 -04001835EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001836
1837const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1838{
1839 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001840 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001841 WARN_ON(1);
1842 return NULL;
1843 }
David Howells7ff5062072020-02-10 10:00:21 +00001844 if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
David Howells9ea9ce02018-10-20 00:57:56 +01001845 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001846 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001847 return new->bvec = kmemdup(new->bvec,
1848 new->nr_segs * sizeof(struct bio_vec),
1849 flags);
1850 else
1851 /* iovec and kvec have identical layout */
1852 return new->iov = kmemdup(new->iov,
1853 new->nr_segs * sizeof(struct iovec),
1854 flags);
1855}
1856EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001857
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001858static int copy_compat_iovec_from_user(struct iovec *iov,
1859 const struct iovec __user *uvec, unsigned long nr_segs)
1860{
1861 const struct compat_iovec __user *uiov =
1862 (const struct compat_iovec __user *)uvec;
1863 int ret = -EFAULT, i;
1864
Christoph Hellwiga959a972021-01-11 18:19:26 +01001865 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001866 return -EFAULT;
1867
1868 for (i = 0; i < nr_segs; i++) {
1869 compat_uptr_t buf;
1870 compat_ssize_t len;
1871
1872 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1873 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1874
1875 /* check for compat_size_t not fitting in compat_ssize_t .. */
1876 if (len < 0) {
1877 ret = -EINVAL;
1878 goto uaccess_end;
1879 }
1880 iov[i].iov_base = compat_ptr(buf);
1881 iov[i].iov_len = len;
1882 }
1883
1884 ret = 0;
1885uaccess_end:
1886 user_access_end();
1887 return ret;
1888}
1889
1890static int copy_iovec_from_user(struct iovec *iov,
1891 const struct iovec __user *uvec, unsigned long nr_segs)
David Laightfb041b52020-09-25 06:51:39 +02001892{
1893 unsigned long seg;
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001894
1895 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1896 return -EFAULT;
1897 for (seg = 0; seg < nr_segs; seg++) {
1898 if ((ssize_t)iov[seg].iov_len < 0)
1899 return -EINVAL;
1900 }
1901
1902 return 0;
1903}
1904
1905struct iovec *iovec_from_user(const struct iovec __user *uvec,
1906 unsigned long nr_segs, unsigned long fast_segs,
1907 struct iovec *fast_iov, bool compat)
1908{
1909 struct iovec *iov = fast_iov;
1910 int ret;
David Laightfb041b52020-09-25 06:51:39 +02001911
1912 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001913 * SuS says "The readv() function *may* fail if the iovcnt argument was
1914 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
David Laightfb041b52020-09-25 06:51:39 +02001915 * traditionally returned zero for zero segments, so...
1916 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001917 if (nr_segs == 0)
1918 return iov;
1919 if (nr_segs > UIO_MAXIOV)
1920 return ERR_PTR(-EINVAL);
David Laightfb041b52020-09-25 06:51:39 +02001921 if (nr_segs > fast_segs) {
1922 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001923 if (!iov)
1924 return ERR_PTR(-ENOMEM);
David Laightfb041b52020-09-25 06:51:39 +02001925 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001926
1927 if (compat)
1928 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1929 else
1930 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1931 if (ret) {
1932 if (iov != fast_iov)
1933 kfree(iov);
1934 return ERR_PTR(ret);
1935 }
1936
1937 return iov;
1938}
1939
1940ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1941 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1942 struct iov_iter *i, bool compat)
1943{
1944 ssize_t total_len = 0;
1945 unsigned long seg;
1946 struct iovec *iov;
1947
1948 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1949 if (IS_ERR(iov)) {
1950 *iovp = NULL;
1951 return PTR_ERR(iov);
David Laightfb041b52020-09-25 06:51:39 +02001952 }
1953
1954 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001955 * According to the Single Unix Specification we should return EINVAL if
1956 * an element length is < 0 when cast to ssize_t or if the total length
1957 * would overflow the ssize_t return value of the system call.
David Laightfb041b52020-09-25 06:51:39 +02001958 *
1959 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1960 * overflow case.
1961 */
David Laightfb041b52020-09-25 06:51:39 +02001962 for (seg = 0; seg < nr_segs; seg++) {
David Laightfb041b52020-09-25 06:51:39 +02001963 ssize_t len = (ssize_t)iov[seg].iov_len;
1964
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001965 if (!access_ok(iov[seg].iov_base, len)) {
1966 if (iov != *iovp)
1967 kfree(iov);
1968 *iovp = NULL;
1969 return -EFAULT;
David Laightfb041b52020-09-25 06:51:39 +02001970 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001971
1972 if (len > MAX_RW_COUNT - total_len) {
1973 len = MAX_RW_COUNT - total_len;
David Laightfb041b52020-09-25 06:51:39 +02001974 iov[seg].iov_len = len;
1975 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001976 total_len += len;
David Laightfb041b52020-09-25 06:51:39 +02001977 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001978
1979 iov_iter_init(i, type, iov, nr_segs, total_len);
1980 if (iov == *iovp)
1981 *iovp = NULL;
1982 else
1983 *iovp = iov;
1984 return total_len;
David Laightfb041b52020-09-25 06:51:39 +02001985}
1986
Vegard Nossumffecee42016-10-08 11:18:07 +02001987/**
1988 * import_iovec() - Copy an array of &struct iovec from userspace
1989 * into the kernel, check that it is valid, and initialize a new
1990 * &struct iov_iter iterator to access it.
1991 *
1992 * @type: One of %READ or %WRITE.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001993 * @uvec: Pointer to the userspace array.
Vegard Nossumffecee42016-10-08 11:18:07 +02001994 * @nr_segs: Number of elements in userspace array.
1995 * @fast_segs: Number of elements in @iov.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001996 * @iovp: (input and output parameter) Pointer to pointer to (usually small
Vegard Nossumffecee42016-10-08 11:18:07 +02001997 * on-stack) kernel array.
1998 * @i: Pointer to iterator that will be initialized on success.
1999 *
2000 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
2001 * then this function places %NULL in *@iov on return. Otherwise, a new
2002 * array will be allocated and the result placed in *@iov. This means that
2003 * the caller may call kfree() on *@iov regardless of whether the small
2004 * on-stack array was used or not (and regardless of whether this function
2005 * returns an error or not).
2006 *
Jens Axboe87e5e6d2019-05-14 16:02:22 -06002007 * Return: Negative error code on error, bytes imported on success
Vegard Nossumffecee42016-10-08 11:18:07 +02002008 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002009ssize_t import_iovec(int type, const struct iovec __user *uvec,
Al Virobc917be2015-03-21 17:45:43 -04002010 unsigned nr_segs, unsigned fast_segs,
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002011 struct iovec **iovp, struct iov_iter *i)
Al Virobc917be2015-03-21 17:45:43 -04002012{
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02002013 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
2014 in_compat_syscall());
Al Virobc917be2015-03-21 17:45:43 -04002015}
2016EXPORT_SYMBOL(import_iovec);
2017
Al Virobc917be2015-03-21 17:45:43 -04002018int import_single_range(int rw, void __user *buf, size_t len,
2019 struct iovec *iov, struct iov_iter *i)
2020{
2021 if (len > MAX_RW_COUNT)
2022 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002023 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04002024 return -EFAULT;
2025
2026 iov->iov_base = buf;
2027 iov->iov_len = len;
2028 iov_iter_init(i, rw, iov, 1, len);
2029 return 0;
2030}
Al Viroe1267582015-12-06 20:38:56 -05002031EXPORT_SYMBOL(import_single_range);
Jens Axboe8fb0f472021-09-10 11:18:36 -06002032
2033/**
2034 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
2035 * iov_iter_save_state() was called.
2036 *
2037 * @i: &struct iov_iter to restore
2038 * @state: state to restore from
2039 *
2040 * Used after iov_iter_save_state() to bring restore @i, if operations may
2041 * have advanced it.
2042 *
2043 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
2044 */
2045void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
2046{
2047 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
2048 !iov_iter_is_kvec(i))
2049 return;
2050 i->iov_offset = state->iov_offset;
2051 i->count = state->count;
2052 /*
2053 * For the *vec iters, nr_segs + iov is constant - if we increment
2054 * the vec, then we also decrement the nr_segs count. Hence we don't
2055 * need to track both of these, just one is enough and we can deduct
2056 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
2057 * size, so we can just increment the iov pointer as they are unionzed.
2058 * ITER_BVEC _may_ be the same size on some archs, but on others it is
2059 * not. Be safe and handle it separately.
2060 */
2061 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
2062 if (iov_iter_is_bvec(i))
2063 i->bvec -= state->nr_segs - i->nr_segs;
2064 else
2065 i->iov -= state->nr_segs - i->nr_segs;
2066 i->nr_segs = state->nr_segs;
2067}