blob: b0e0acdf96c15e73326c217ae7903fcd364ddc99 [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; \
Matthew Wilcox (Oracle)821979f2021-11-28 19:18:27 -050072 struct folio *folio; \
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 pgoff_t index = start / PAGE_SIZE; \
David Howells7ff5062072020-02-10 10:00:21 +000075 XA_STATE(xas, i->xarray, index); \
76 \
Matthew Wilcox (Oracle)821979f2021-11-28 19:18:27 -050077 len = PAGE_SIZE - offset_in_page(start); \
Al Viro7baa5092021-05-02 11:35:03 -040078 rcu_read_lock(); \
Matthew Wilcox (Oracle)821979f2021-11-28 19:18:27 -050079 xas_for_each(&xas, folio, ULONG_MAX) { \
Al Viro7baa5092021-05-02 11:35:03 -040080 unsigned left; \
Matthew Wilcox (Oracle)821979f2021-11-28 19:18:27 -050081 size_t offset; \
82 if (xas_retry(&xas, folio)) \
Al Viro7baa5092021-05-02 11:35:03 -040083 continue; \
Matthew Wilcox (Oracle)821979f2021-11-28 19:18:27 -050084 if (WARN_ON(xa_is_value(folio))) \
Al Viro7baa5092021-05-02 11:35:03 -040085 break; \
Matthew Wilcox (Oracle)821979f2021-11-28 19:18:27 -050086 if (WARN_ON(folio_test_hugetlb(folio))) \
Al Viro7baa5092021-05-02 11:35:03 -040087 break; \
Matthew Wilcox (Oracle)821979f2021-11-28 19:18:27 -050088 offset = offset_in_folio(folio, start + __off); \
89 while (offset < folio_size(folio)) { \
90 base = kmap_local_folio(folio, offset); \
Al Viro7baa5092021-05-02 11:35:03 -040091 len = min(n, len); \
92 left = (STEP); \
Matthew Wilcox (Oracle)821979f2021-11-28 19:18:27 -050093 kunmap_local(base); \
Al Viro7baa5092021-05-02 11:35:03 -040094 len -= left; \
95 __off += len; \
96 n -= len; \
97 if (left || n == 0) \
98 goto __out; \
Matthew Wilcox (Oracle)821979f2021-11-28 19:18:27 -050099 offset += len; \
100 len = PAGE_SIZE; \
Al Viro7baa5092021-05-02 11:35:03 -0400101 } \
David Howells7ff5062072020-02-10 10:00:21 +0000102 } \
Al Viro1b4fb5f2021-04-26 20:33:42 -0400103__out: \
David Howells7ff5062072020-02-10 10:00:21 +0000104 rcu_read_unlock(); \
Matthew Wilcox (Oracle)821979f2021-11-28 19:18:27 -0500105 i->iov_offset += __off; \
Al Viro622838f2021-05-02 11:13:09 -0400106 n = __off; \
David Howells7ff5062072020-02-10 10:00:21 +0000107}
108
Al Viro7baa5092021-05-02 11:35:03 -0400109#define __iterate_and_advance(i, n, base, len, off, I, K) { \
Al Virodd254f52016-05-09 11:54:48 -0400110 if (unlikely(i->count < n)) \
111 n = i->count; \
Al Virof5da8352021-04-28 20:59:08 -0400112 if (likely(n)) { \
Al Viro28f38db2021-06-02 17:25:59 -0400113 if (likely(iter_is_iovec(i))) { \
Al Viro5c67aa92021-04-25 23:57:42 -0400114 const struct iovec *iov = i->iov; \
Al Viro7baa5092021-05-02 11:35:03 -0400115 void __user *base; \
116 size_t len; \
117 iterate_iovec(i, n, base, len, off, \
Al Viroa6e4ec72021-05-02 13:03:41 -0400118 iov, (I)) \
Al Virodd254f52016-05-09 11:54:48 -0400119 i->nr_segs -= iov - i->iov; \
120 i->iov = iov; \
Al Viro28f38db2021-06-02 17:25:59 -0400121 } else if (iov_iter_is_bvec(i)) { \
122 const struct bio_vec *bvec = i->bvec; \
Al Viro7baa5092021-05-02 11:35:03 -0400123 void *base; \
124 size_t len; \
125 iterate_bvec(i, n, base, len, off, \
Al Viroa6e4ec72021-05-02 13:03:41 -0400126 bvec, (K)) \
Al Viro7491a2b2021-04-26 20:19:14 -0400127 i->nr_segs -= bvec - i->bvec; \
128 i->bvec = bvec; \
Al Viro28f38db2021-06-02 17:25:59 -0400129 } else if (iov_iter_is_kvec(i)) { \
Al Viro5c67aa92021-04-25 23:57:42 -0400130 const struct kvec *kvec = i->kvec; \
Al Viro7baa5092021-05-02 11:35:03 -0400131 void *base; \
132 size_t len; \
133 iterate_iovec(i, n, base, len, off, \
Al Viroa6e4ec72021-05-02 13:03:41 -0400134 kvec, (K)) \
Al Viro28f38db2021-06-02 17:25:59 -0400135 i->nr_segs -= kvec - i->kvec; \
136 i->kvec = kvec; \
137 } else if (iov_iter_is_xarray(i)) { \
Al Viro7baa5092021-05-02 11:35:03 -0400138 void *base; \
139 size_t len; \
140 iterate_xarray(i, n, base, len, off, \
Al Viroa6e4ec72021-05-02 13:03:41 -0400141 (K)) \
Al Viro7ce2a912014-11-27 13:59:45 -0500142 } \
Al Virodd254f52016-05-09 11:54:48 -0400143 i->count -= n; \
Al Viro7ce2a912014-11-27 13:59:45 -0500144 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500145}
Al Viro7baa5092021-05-02 11:35:03 -0400146#define iterate_and_advance(i, n, base, len, off, I, K) \
147 __iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
Al Viro7ce2a912014-11-27 13:59:45 -0500148
Al Viro09fc68dc2017-06-29 22:25:14 -0400149static int copyout(void __user *to, const void *from, size_t n)
150{
Albert van der Linde4d0e9df2020-10-15 20:13:50 -0700151 if (should_fail_usercopy())
152 return n;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800153 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100154 instrument_copy_to_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400155 n = raw_copy_to_user(to, from, n);
156 }
157 return n;
158}
159
160static int copyin(void *to, const void __user *from, size_t n)
161{
Albert van der Linde4d0e9df2020-10-15 20:13:50 -0700162 if (should_fail_usercopy())
163 return n;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800164 if (access_ok(from, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100165 instrument_copy_from_user(to, from, n);
Al Viro09fc68dc2017-06-29 22:25:14 -0400166 n = raw_copy_from_user(to, from, n);
167 }
168 return n;
169}
170
Al Viro62a80672014-04-04 23:12:29 -0400171static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500172 struct iov_iter *i)
173{
174 size_t skip, copy, left, wanted;
175 const struct iovec *iov;
176 char __user *buf;
177 void *kaddr, *from;
178
179 if (unlikely(bytes > i->count))
180 bytes = i->count;
181
182 if (unlikely(!bytes))
183 return 0;
184
Al Viro09fc68dc2017-06-29 22:25:14 -0400185 might_fault();
Al Viro4f18cd32014-02-05 19:11:33 -0500186 wanted = bytes;
187 iov = i->iov;
188 skip = i->iov_offset;
189 buf = iov->iov_base + skip;
190 copy = min(bytes, iov->iov_len - skip);
191
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +0200192 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_writeable(buf, copy)) {
Al Viro4f18cd32014-02-05 19:11:33 -0500193 kaddr = kmap_atomic(page);
194 from = kaddr + offset;
195
196 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400197 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500198 copy -= left;
199 skip += copy;
200 from += copy;
201 bytes -= copy;
202
203 while (unlikely(!left && bytes)) {
204 iov++;
205 buf = iov->iov_base;
206 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400207 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500208 copy -= left;
209 skip = copy;
210 from += copy;
211 bytes -= copy;
212 }
213 if (likely(!bytes)) {
214 kunmap_atomic(kaddr);
215 goto done;
216 }
217 offset = from - kaddr;
218 buf += copy;
219 kunmap_atomic(kaddr);
220 copy = min(bytes, iov->iov_len - skip);
221 }
222 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700223
Al Viro4f18cd32014-02-05 19:11:33 -0500224 kaddr = kmap(page);
225 from = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400226 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500227 copy -= left;
228 skip += copy;
229 from += copy;
230 bytes -= copy;
231 while (unlikely(!left && bytes)) {
232 iov++;
233 buf = iov->iov_base;
234 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400235 left = copyout(buf, from, copy);
Al Viro4f18cd32014-02-05 19:11:33 -0500236 copy -= left;
237 skip = copy;
238 from += copy;
239 bytes -= copy;
240 }
241 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700242
Al Viro4f18cd32014-02-05 19:11:33 -0500243done:
Al Viro81055e52014-04-04 19:23:46 -0400244 if (skip == iov->iov_len) {
245 iov++;
246 skip = 0;
247 }
Al Viro4f18cd32014-02-05 19:11:33 -0500248 i->count -= wanted - bytes;
249 i->nr_segs -= iov - i->iov;
250 i->iov = iov;
251 i->iov_offset = skip;
252 return wanted - bytes;
253}
Al Viro4f18cd32014-02-05 19:11:33 -0500254
Al Viro62a80672014-04-04 23:12:29 -0400255static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400256 struct iov_iter *i)
257{
258 size_t skip, copy, left, wanted;
259 const struct iovec *iov;
260 char __user *buf;
261 void *kaddr, *to;
262
263 if (unlikely(bytes > i->count))
264 bytes = i->count;
265
266 if (unlikely(!bytes))
267 return 0;
268
Al Viro09fc68dc2017-06-29 22:25:14 -0400269 might_fault();
Al Virof0d1bec2014-04-03 15:05:18 -0400270 wanted = bytes;
271 iov = i->iov;
272 skip = i->iov_offset;
273 buf = iov->iov_base + skip;
274 copy = min(bytes, iov->iov_len - skip);
275
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +0200276 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_readable(buf, copy)) {
Al Virof0d1bec2014-04-03 15:05:18 -0400277 kaddr = kmap_atomic(page);
278 to = kaddr + offset;
279
280 /* first chunk, usually the only one */
Al Viro09fc68dc2017-06-29 22:25:14 -0400281 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400282 copy -= left;
283 skip += copy;
284 to += copy;
285 bytes -= copy;
286
287 while (unlikely(!left && bytes)) {
288 iov++;
289 buf = iov->iov_base;
290 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400291 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400292 copy -= left;
293 skip = copy;
294 to += copy;
295 bytes -= copy;
296 }
297 if (likely(!bytes)) {
298 kunmap_atomic(kaddr);
299 goto done;
300 }
301 offset = to - kaddr;
302 buf += copy;
303 kunmap_atomic(kaddr);
304 copy = min(bytes, iov->iov_len - skip);
305 }
306 /* Too bad - revert to non-atomic kmap */
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700307
Al Virof0d1bec2014-04-03 15:05:18 -0400308 kaddr = kmap(page);
309 to = kaddr + offset;
Al Viro09fc68dc2017-06-29 22:25:14 -0400310 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400311 copy -= left;
312 skip += copy;
313 to += copy;
314 bytes -= copy;
315 while (unlikely(!left && bytes)) {
316 iov++;
317 buf = iov->iov_base;
318 copy = min(bytes, iov->iov_len);
Al Viro09fc68dc2017-06-29 22:25:14 -0400319 left = copyin(to, buf, copy);
Al Virof0d1bec2014-04-03 15:05:18 -0400320 copy -= left;
321 skip = copy;
322 to += copy;
323 bytes -= copy;
324 }
325 kunmap(page);
Mikulas Patocka3fa6c502016-07-28 15:48:50 -0700326
Al Virof0d1bec2014-04-03 15:05:18 -0400327done:
Al Viro81055e52014-04-04 19:23:46 -0400328 if (skip == iov->iov_len) {
329 iov++;
330 skip = 0;
331 }
Al Virof0d1bec2014-04-03 15:05:18 -0400332 i->count -= wanted - bytes;
333 i->nr_segs -= iov - i->iov;
334 i->iov = iov;
335 i->iov_offset = skip;
336 return wanted - bytes;
337}
Al Virof0d1bec2014-04-03 15:05:18 -0400338
Al Viro241699c2016-09-22 16:33:12 -0400339#ifdef PIPE_PARANOIA
340static bool sanity(const struct iov_iter *i)
341{
342 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000343 unsigned int p_head = pipe->head;
344 unsigned int p_tail = pipe->tail;
345 unsigned int p_mask = pipe->ring_size - 1;
346 unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
347 unsigned int i_head = i->head;
348 unsigned int idx;
349
Al Viro241699c2016-09-22 16:33:12 -0400350 if (i->iov_offset) {
351 struct pipe_buffer *p;
David Howells8cefc102019-11-15 13:30:32 +0000352 if (unlikely(p_occupancy == 0))
Al Viro241699c2016-09-22 16:33:12 -0400353 goto Bad; // pipe must be non-empty
David Howells8cefc102019-11-15 13:30:32 +0000354 if (unlikely(i_head != p_head - 1))
Al Viro241699c2016-09-22 16:33:12 -0400355 goto Bad; // must be at the last buffer...
356
David Howells8cefc102019-11-15 13:30:32 +0000357 p = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400358 if (unlikely(p->offset + p->len != i->iov_offset))
359 goto Bad; // ... at the end of segment
360 } else {
David Howells8cefc102019-11-15 13:30:32 +0000361 if (i_head != p_head)
Al Viro241699c2016-09-22 16:33:12 -0400362 goto Bad; // must be right after the last buffer
363 }
364 return true;
365Bad:
David Howells8cefc102019-11-15 13:30:32 +0000366 printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset);
367 printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
368 p_head, p_tail, pipe->ring_size);
369 for (idx = 0; idx < pipe->ring_size; idx++)
Al Viro241699c2016-09-22 16:33:12 -0400370 printk(KERN_ERR "[%p %p %d %d]\n",
371 pipe->bufs[idx].ops,
372 pipe->bufs[idx].page,
373 pipe->bufs[idx].offset,
374 pipe->bufs[idx].len);
375 WARN_ON(1);
376 return false;
377}
378#else
379#define sanity(i) true
380#endif
381
Al Viro241699c2016-09-22 16:33:12 -0400382static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
383 struct iov_iter *i)
384{
385 struct pipe_inode_info *pipe = i->pipe;
386 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +0000387 unsigned int p_tail = pipe->tail;
388 unsigned int p_mask = pipe->ring_size - 1;
389 unsigned int i_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400390 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400391
392 if (unlikely(bytes > i->count))
393 bytes = i->count;
394
395 if (unlikely(!bytes))
396 return 0;
397
398 if (!sanity(i))
399 return 0;
400
401 off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000402 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400403 if (off) {
404 if (offset == off && buf->page == page) {
405 /* merge with the last one */
406 buf->len += bytes;
407 i->iov_offset += bytes;
408 goto out;
409 }
David Howells8cefc102019-11-15 13:30:32 +0000410 i_head++;
411 buf = &pipe->bufs[i_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400412 }
David Howells6718b6f2019-10-16 16:47:32 +0100413 if (pipe_full(i_head, p_tail, pipe->max_usage))
Al Viro241699c2016-09-22 16:33:12 -0400414 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000415
Al Viro241699c2016-09-22 16:33:12 -0400416 buf->ops = &page_cache_pipe_buf_ops;
David Howells8cefc102019-11-15 13:30:32 +0000417 get_page(page);
418 buf->page = page;
Al Viro241699c2016-09-22 16:33:12 -0400419 buf->offset = offset;
420 buf->len = bytes;
David Howells8cefc102019-11-15 13:30:32 +0000421
422 pipe->head = i_head + 1;
Al Viro241699c2016-09-22 16:33:12 -0400423 i->iov_offset = offset + bytes;
David Howells8cefc102019-11-15 13:30:32 +0000424 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400425out:
426 i->count -= bytes;
427 return bytes;
428}
429
Al Viro4f18cd32014-02-05 19:11:33 -0500430/*
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200431 * fault_in_iov_iter_readable - fault in iov iterator for reading
432 * @i: iterator
433 * @size: maximum length
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400434 *
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200435 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
436 * @size. For each iovec, fault in each page that constitutes the iovec.
437 *
438 * Returns the number of bytes not faulted in (like copy_to_user() and
439 * copy_from_user()).
440 *
441 * Always returns 0 for non-userspace iterators.
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400442 */
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200443size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400444{
Al Viro0e8f0d62021-06-02 14:48:21 -0400445 if (iter_is_iovec(i)) {
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200446 size_t count = min(size, iov_iter_count(i));
Al Viro8409a0d2021-05-02 11:57:37 -0400447 const struct iovec *p;
448 size_t skip;
449
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200450 size -= count;
451 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
452 size_t len = min(count, p->iov_len - skip);
453 size_t ret;
Al Viro8409a0d2021-05-02 11:57:37 -0400454
455 if (unlikely(!len))
456 continue;
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200457 ret = fault_in_readable(p->iov_base + skip, len);
458 count -= len - ret;
459 if (ret)
460 break;
Al Viro8409a0d2021-05-02 11:57:37 -0400461 }
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200462 return count + size;
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400463 }
464 return 0;
465}
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200466EXPORT_SYMBOL(fault_in_iov_iter_readable);
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400467
Andreas Gruenbachercdd591f2021-07-05 17:26:28 +0200468/*
469 * fault_in_iov_iter_writeable - fault in iov iterator for writing
470 * @i: iterator
471 * @size: maximum length
472 *
473 * Faults in the iterator using get_user_pages(), i.e., without triggering
474 * hardware page faults. This is primarily useful when we already know that
475 * some or all of the pages in @i aren't in memory.
476 *
477 * Returns the number of bytes not faulted in, like copy_to_user() and
478 * copy_from_user().
479 *
480 * Always returns 0 for non-user-space iterators.
481 */
482size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
483{
484 if (iter_is_iovec(i)) {
485 size_t count = min(size, iov_iter_count(i));
486 const struct iovec *p;
487 size_t skip;
488
489 size -= count;
490 for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
491 size_t len = min(count, p->iov_len - skip);
492 size_t ret;
493
494 if (unlikely(!len))
495 continue;
496 ret = fault_in_safe_writeable(p->iov_base + skip, len);
497 count -= len - ret;
498 if (ret)
499 break;
500 }
501 return count + size;
502 }
503 return 0;
504}
505EXPORT_SYMBOL(fault_in_iov_iter_writeable);
506
David Howellsaa563d72018-10-20 00:57:56 +0100507void iov_iter_init(struct iov_iter *i, unsigned int direction,
Al Viro71d8e532014-03-05 19:28:09 -0500508 const struct iovec *iov, unsigned long nr_segs,
509 size_t count)
510{
David Howellsaa563d72018-10-20 00:57:56 +0100511 WARN_ON(direction & ~(READ | WRITE));
Al Viro8cd54c12021-04-22 14:50:39 -0400512 *i = (struct iov_iter) {
513 .iter_type = ITER_IOVEC,
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +0200514 .nofault = false,
Al Viro8cd54c12021-04-22 14:50:39 -0400515 .data_source = direction,
516 .iov = iov,
517 .nr_segs = nr_segs,
518 .iov_offset = 0,
519 .count = count
520 };
Al Viro71d8e532014-03-05 19:28:09 -0500521}
522EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400523
Al Viro241699c2016-09-22 16:33:12 -0400524static inline bool allocated(struct pipe_buffer *buf)
525{
526 return buf->ops == &default_pipe_buf_ops;
527}
528
David Howells8cefc102019-11-15 13:30:32 +0000529static inline void data_start(const struct iov_iter *i,
530 unsigned int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400531{
David Howells8cefc102019-11-15 13:30:32 +0000532 unsigned int p_mask = i->pipe->ring_size - 1;
533 unsigned int iter_head = i->head;
Al Viro241699c2016-09-22 16:33:12 -0400534 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000535
536 if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) ||
537 off == PAGE_SIZE)) {
538 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400539 off = 0;
540 }
David Howells8cefc102019-11-15 13:30:32 +0000541 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400542 *offp = off;
543}
544
545static size_t push_pipe(struct iov_iter *i, size_t size,
David Howells8cefc102019-11-15 13:30:32 +0000546 int *iter_headp, size_t *offp)
Al Viro241699c2016-09-22 16:33:12 -0400547{
548 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000549 unsigned int p_tail = pipe->tail;
550 unsigned int p_mask = pipe->ring_size - 1;
551 unsigned int iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400552 size_t off;
Al Viro241699c2016-09-22 16:33:12 -0400553 ssize_t left;
554
555 if (unlikely(size > i->count))
556 size = i->count;
557 if (unlikely(!size))
558 return 0;
559
560 left = size;
David Howells8cefc102019-11-15 13:30:32 +0000561 data_start(i, &iter_head, &off);
562 *iter_headp = iter_head;
Al Viro241699c2016-09-22 16:33:12 -0400563 *offp = off;
564 if (off) {
565 left -= PAGE_SIZE - off;
566 if (left <= 0) {
David Howells8cefc102019-11-15 13:30:32 +0000567 pipe->bufs[iter_head & p_mask].len += size;
Al Viro241699c2016-09-22 16:33:12 -0400568 return size;
569 }
David Howells8cefc102019-11-15 13:30:32 +0000570 pipe->bufs[iter_head & p_mask].len = PAGE_SIZE;
571 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -0400572 }
David Howells6718b6f2019-10-16 16:47:32 +0100573 while (!pipe_full(iter_head, p_tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000574 struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask];
Al Viro241699c2016-09-22 16:33:12 -0400575 struct page *page = alloc_page(GFP_USER);
576 if (!page)
577 break;
David Howells8cefc102019-11-15 13:30:32 +0000578
579 buf->ops = &default_pipe_buf_ops;
580 buf->page = page;
581 buf->offset = 0;
582 buf->len = min_t(ssize_t, left, PAGE_SIZE);
583 left -= buf->len;
584 iter_head++;
585 pipe->head = iter_head;
586
587 if (left == 0)
Al Viro241699c2016-09-22 16:33:12 -0400588 return size;
Al Viro241699c2016-09-22 16:33:12 -0400589 }
590 return size - left;
591}
592
593static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
594 struct iov_iter *i)
595{
596 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000597 unsigned int p_mask = pipe->ring_size - 1;
598 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400599 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400600
601 if (!sanity(i))
602 return 0;
603
David Howells8cefc102019-11-15 13:30:32 +0000604 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400605 if (unlikely(!n))
606 return 0;
David Howells8cefc102019-11-15 13:30:32 +0000607 do {
Al Viro241699c2016-09-22 16:33:12 -0400608 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
David Howells8cefc102019-11-15 13:30:32 +0000609 memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk);
610 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400611 i->iov_offset = off + chunk;
612 n -= chunk;
613 addr += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000614 off = 0;
615 i_head++;
616 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400617 i->count -= bytes;
618 return bytes;
619}
620
Al Virof9152892018-11-27 22:32:59 -0500621static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
622 __wsum sum, size_t off)
623{
Al Virocc44c172020-07-11 00:12:07 -0400624 __wsum next = csum_partial_copy_nocheck(from, to, len);
Al Virof9152892018-11-27 22:32:59 -0500625 return csum_block_add(sum, next, off);
626}
627
Al Viro78e1f382018-11-25 16:24:16 -0500628static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
Al Viro6852df12021-05-02 17:24:40 -0400629 struct iov_iter *i, __wsum *sump)
Al Viro78e1f382018-11-25 16:24:16 -0500630{
631 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000632 unsigned int p_mask = pipe->ring_size - 1;
Al Viro6852df12021-05-02 17:24:40 -0400633 __wsum sum = *sump;
634 size_t off = 0;
David Howells8cefc102019-11-15 13:30:32 +0000635 unsigned int i_head;
Al Viro6852df12021-05-02 17:24:40 -0400636 size_t r;
Al Viro78e1f382018-11-25 16:24:16 -0500637
638 if (!sanity(i))
639 return 0;
640
Al Viro6852df12021-05-02 17:24:40 -0400641 bytes = push_pipe(i, bytes, &i_head, &r);
642 while (bytes) {
643 size_t chunk = min_t(size_t, bytes, PAGE_SIZE - r);
Al Viro2495bdcc2021-04-30 13:40:48 -0400644 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
Al Viro6852df12021-05-02 17:24:40 -0400645 sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
Al Viro2495bdcc2021-04-30 13:40:48 -0400646 kunmap_local(p);
David Howells8cefc102019-11-15 13:30:32 +0000647 i->head = i_head;
Al Viro78e1f382018-11-25 16:24:16 -0500648 i->iov_offset = r + chunk;
Al Viro6852df12021-05-02 17:24:40 -0400649 bytes -= chunk;
Al Viro78e1f382018-11-25 16:24:16 -0500650 off += chunk;
David Howells8cefc102019-11-15 13:30:32 +0000651 r = 0;
652 i_head++;
Al Viro6852df12021-05-02 17:24:40 -0400653 }
654 *sump = sum;
655 i->count -= off;
656 return off;
Al Viro78e1f382018-11-25 16:24:16 -0500657}
658
Al Viroaa28de22017-06-29 21:45:10 -0400659size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400660{
David Howells00e23702018-10-22 13:07:28 +0100661 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400662 return copy_pipe_to_iter(addr, bytes, i);
Al Viro09fc68dc2017-06-29 22:25:14 -0400663 if (iter_is_iovec(i))
664 might_fault();
Al Viro7baa5092021-05-02 11:35:03 -0400665 iterate_and_advance(i, bytes, base, len, off,
666 copyout(base, addr + off, len),
667 memcpy(base, addr + off, len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500668 )
Al Viro62a80672014-04-04 23:12:29 -0400669
Al Viro3d4d3e42014-11-27 14:28:06 -0500670 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400671}
Al Viroaa28de22017-06-29 21:45:10 -0400672EXPORT_SYMBOL(_copy_to_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400673
Dan Williamsec6347b2020-10-05 20:40:16 -0700674#ifdef CONFIG_ARCH_HAS_COPY_MC
675static int copyout_mc(void __user *to, const void *from, size_t n)
Dan Williams87803562018-05-03 17:06:31 -0700676{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800677 if (access_ok(to, n)) {
Marco Elverd0ef4c32020-01-21 17:05:11 +0100678 instrument_copy_to_user(to, from, n);
Dan Williamsec6347b2020-10-05 20:40:16 -0700679 n = copy_mc_to_user((__force void *) to, from, n);
Dan Williams87803562018-05-03 17:06:31 -0700680 }
681 return n;
682}
683
Dan Williamsec6347b2020-10-05 20:40:16 -0700684static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
Dan Williamsca146f62018-07-08 13:46:12 -0700685 struct iov_iter *i)
686{
687 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000688 unsigned int p_mask = pipe->ring_size - 1;
689 unsigned int i_head;
Dan Williamsca146f62018-07-08 13:46:12 -0700690 size_t n, off, xfer = 0;
Dan Williamsca146f62018-07-08 13:46:12 -0700691
692 if (!sanity(i))
693 return 0;
694
Al Viro2a510a72021-05-02 17:16:34 -0400695 n = push_pipe(i, bytes, &i_head, &off);
696 while (n) {
Dan Williamsca146f62018-07-08 13:46:12 -0700697 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
Al Viro2a510a72021-05-02 17:16:34 -0400698 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
Dan Williamsca146f62018-07-08 13:46:12 -0700699 unsigned long rem;
Al Viro2a510a72021-05-02 17:16:34 -0400700 rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
701 chunk -= rem;
702 kunmap_local(p);
David Howells8cefc102019-11-15 13:30:32 +0000703 i->head = i_head;
Al Viro2a510a72021-05-02 17:16:34 -0400704 i->iov_offset = off + chunk;
705 xfer += chunk;
Dan Williamsca146f62018-07-08 13:46:12 -0700706 if (rem)
707 break;
708 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000709 off = 0;
710 i_head++;
Al Viro2a510a72021-05-02 17:16:34 -0400711 }
Dan Williamsca146f62018-07-08 13:46:12 -0700712 i->count -= xfer;
713 return xfer;
714}
715
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700716/**
Dan Williamsec6347b2020-10-05 20:40:16 -0700717 * _copy_mc_to_iter - copy to iter with source memory error exception handling
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700718 * @addr: source kernel address
719 * @bytes: total transfer length
Randy Dunlap44e55992021-09-07 19:58:54 -0700720 * @i: destination iterator
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700721 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700722 * The pmem driver deploys this for the dax operation
723 * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
724 * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
725 * successfully copied.
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700726 *
Dan Williamsec6347b2020-10-05 20:40:16 -0700727 * The main differences between this and typical _copy_to_iter().
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700728 *
729 * * Typical tail/residue handling after a fault retries the copy
730 * byte-by-byte until the fault happens again. Re-triggering machine
731 * checks is potentially fatal so the implementation uses source
732 * alignment and poison alignment assumptions to avoid re-triggering
733 * hardware exceptions.
734 *
735 * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
736 * Compare to copy_to_iter() where only ITER_IOVEC attempts might return
737 * a short copy.
Randy Dunlap44e55992021-09-07 19:58:54 -0700738 *
739 * Return: number of bytes copied (may be %0)
Dan Williamsbf3eeb92018-07-08 13:46:02 -0700740 */
Dan Williamsec6347b2020-10-05 20:40:16 -0700741size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Dan Williams87803562018-05-03 17:06:31 -0700742{
David Howells00e23702018-10-22 13:07:28 +0100743 if (unlikely(iov_iter_is_pipe(i)))
Dan Williamsec6347b2020-10-05 20:40:16 -0700744 return copy_mc_pipe_to_iter(addr, bytes, i);
Dan Williams87803562018-05-03 17:06:31 -0700745 if (iter_is_iovec(i))
746 might_fault();
Al Viro7baa5092021-05-02 11:35:03 -0400747 __iterate_and_advance(i, bytes, base, len, off,
748 copyout_mc(base, addr + off, len),
749 copy_mc_to_kernel(base, addr + off, len)
Dan Williams87803562018-05-03 17:06:31 -0700750 )
751
752 return bytes;
753}
Dan Williamsec6347b2020-10-05 20:40:16 -0700754EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
755#endif /* CONFIG_ARCH_HAS_COPY_MC */
Dan Williams87803562018-05-03 17:06:31 -0700756
Al Viroaa28de22017-06-29 21:45:10 -0400757size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400758{
David Howells00e23702018-10-22 13:07:28 +0100759 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400760 WARN_ON(1);
761 return 0;
762 }
Al Viro09fc68dc2017-06-29 22:25:14 -0400763 if (iter_is_iovec(i))
764 might_fault();
Al Viro7baa5092021-05-02 11:35:03 -0400765 iterate_and_advance(i, bytes, base, len, off,
766 copyin(addr + off, base, len),
767 memcpy(addr + off, base, len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500768 )
769
770 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400771}
Al Viroaa28de22017-06-29 21:45:10 -0400772EXPORT_SYMBOL(_copy_from_iter);
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400773
Al Viroaa28de22017-06-29 21:45:10 -0400774size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
Al Viroaa583092014-11-27 20:27:08 -0500775{
David Howells00e23702018-10-22 13:07:28 +0100776 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400777 WARN_ON(1);
778 return 0;
779 }
Al Viro7baa5092021-05-02 11:35:03 -0400780 iterate_and_advance(i, bytes, base, len, off,
781 __copy_from_user_inatomic_nocache(addr + off, base, len),
782 memcpy(addr + off, base, len)
Al Viroaa583092014-11-27 20:27:08 -0500783 )
784
785 return bytes;
786}
Al Viroaa28de22017-06-29 21:45:10 -0400787EXPORT_SYMBOL(_copy_from_iter_nocache);
Al Viroaa583092014-11-27 20:27:08 -0500788
Dan Williams0aed55a2017-05-29 12:22:50 -0700789#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
Dan Williamsabd08d72018-07-08 13:46:07 -0700790/**
791 * _copy_from_iter_flushcache - write destination through cpu cache
792 * @addr: destination kernel address
793 * @bytes: total transfer length
Randy Dunlap44e55992021-09-07 19:58:54 -0700794 * @i: source iterator
Dan Williamsabd08d72018-07-08 13:46:07 -0700795 *
796 * The pmem driver arranges for filesystem-dax to use this facility via
797 * dax_copy_from_iter() for ensuring that writes to persistent memory
798 * are flushed through the CPU cache. It is differentiated from
799 * _copy_from_iter_nocache() in that guarantees all data is flushed for
800 * all iterator types. The _copy_from_iter_nocache() only attempts to
801 * bypass the cache for the ITER_IOVEC case, and on some archs may use
802 * instructions that strand dirty-data in the cache.
Randy Dunlap44e55992021-09-07 19:58:54 -0700803 *
804 * Return: number of bytes copied (may be %0)
Dan Williamsabd08d72018-07-08 13:46:07 -0700805 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700806size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
Dan Williams0aed55a2017-05-29 12:22:50 -0700807{
David Howells00e23702018-10-22 13:07:28 +0100808 if (unlikely(iov_iter_is_pipe(i))) {
Dan Williams0aed55a2017-05-29 12:22:50 -0700809 WARN_ON(1);
810 return 0;
811 }
Al Viro7baa5092021-05-02 11:35:03 -0400812 iterate_and_advance(i, bytes, base, len, off,
813 __copy_from_user_flushcache(addr + off, base, len),
814 memcpy_flushcache(addr + off, base, len)
Dan Williams0aed55a2017-05-29 12:22:50 -0700815 )
816
817 return bytes;
818}
Linus Torvalds6a37e942017-07-07 20:39:20 -0700819EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
Dan Williams0aed55a2017-05-29 12:22:50 -0700820#endif
821
Al Viro72e809e2017-06-29 21:52:57 -0400822static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
823{
Eric Dumazet6daef952019-02-26 10:42:39 -0800824 struct page *head;
825 size_t v = n + offset;
826
827 /*
828 * The general case needs to access the page order in order
829 * to compute the page size.
830 * However, we mostly deal with order-0 pages and thus can
831 * avoid a possible cache line miss for requests that fit all
832 * page orders.
833 */
834 if (n <= v && v <= PAGE_SIZE)
835 return true;
836
837 head = compound_head(page);
838 v += (page - head) << PAGE_SHIFT;
Petar Penkova90bcb82017-08-29 11:20:32 -0700839
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700840 if (likely(n <= v && v <= (page_size(head))))
Al Viro72e809e2017-06-29 21:52:57 -0400841 return true;
842 WARN_ON(1);
843 return false;
844}
Al Virocbbd26b2016-11-01 22:09:04 -0400845
Al Viro08aa6472021-04-29 20:42:25 -0400846static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
Al Virod2715242014-11-27 14:22:37 -0500847 struct iov_iter *i)
848{
Al Viro28f38db2021-06-02 17:25:59 -0400849 if (likely(iter_is_iovec(i)))
850 return copy_page_to_iter_iovec(page, offset, bytes, i);
851 if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
Al Viroc1d4d6a2021-04-27 12:29:53 -0400852 void *kaddr = kmap_local_page(page);
853 size_t wanted = _copy_to_iter(kaddr + offset, bytes, i);
854 kunmap_local(kaddr);
Al Virod2715242014-11-27 14:22:37 -0500855 return wanted;
Al Viro28f38db2021-06-02 17:25:59 -0400856 }
857 if (iov_iter_is_pipe(i))
858 return copy_page_to_iter_pipe(page, offset, bytes, i);
859 if (unlikely(iov_iter_is_discard(i))) {
Al Viroa506abc2021-04-27 12:34:04 -0400860 if (unlikely(i->count < bytes))
861 bytes = i->count;
862 i->count -= bytes;
David Howells9ea9ce02018-10-20 00:57:56 +0100863 return bytes;
Al Viro28f38db2021-06-02 17:25:59 -0400864 }
865 WARN_ON(1);
866 return 0;
Al Virod2715242014-11-27 14:22:37 -0500867}
Al Viro08aa6472021-04-29 20:42:25 -0400868
869size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
870 struct iov_iter *i)
871{
872 size_t res = 0;
873 if (unlikely(!page_copy_sane(page, offset, bytes)))
874 return 0;
875 page += offset / PAGE_SIZE; // first subpage
876 offset %= PAGE_SIZE;
877 while (1) {
878 size_t n = __copy_page_to_iter(page, offset,
879 min(bytes, (size_t)PAGE_SIZE - offset), i);
880 res += n;
881 bytes -= n;
882 if (!bytes || !n)
883 break;
884 offset += n;
885 if (offset == PAGE_SIZE) {
886 page++;
887 offset = 0;
888 }
889 }
890 return res;
891}
Al Virod2715242014-11-27 14:22:37 -0500892EXPORT_SYMBOL(copy_page_to_iter);
893
894size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
895 struct iov_iter *i)
896{
Al Viro72e809e2017-06-29 21:52:57 -0400897 if (unlikely(!page_copy_sane(page, offset, bytes)))
898 return 0;
Al Viro28f38db2021-06-02 17:25:59 -0400899 if (likely(iter_is_iovec(i)))
900 return copy_page_from_iter_iovec(page, offset, bytes, i);
901 if (iov_iter_is_bvec(i) || iov_iter_is_kvec(i) || iov_iter_is_xarray(i)) {
Al Viro55ca3752021-04-27 12:33:24 -0400902 void *kaddr = kmap_local_page(page);
Al Viroaa28de22017-06-29 21:45:10 -0400903 size_t wanted = _copy_from_iter(kaddr + offset, bytes, i);
Al Viro55ca3752021-04-27 12:33:24 -0400904 kunmap_local(kaddr);
Al Virod2715242014-11-27 14:22:37 -0500905 return wanted;
Al Viro28f38db2021-06-02 17:25:59 -0400906 }
907 WARN_ON(1);
908 return 0;
Al Virod2715242014-11-27 14:22:37 -0500909}
910EXPORT_SYMBOL(copy_page_from_iter);
911
Al Viro241699c2016-09-22 16:33:12 -0400912static size_t pipe_zero(size_t bytes, struct iov_iter *i)
913{
914 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000915 unsigned int p_mask = pipe->ring_size - 1;
916 unsigned int i_head;
Al Viro241699c2016-09-22 16:33:12 -0400917 size_t n, off;
Al Viro241699c2016-09-22 16:33:12 -0400918
919 if (!sanity(i))
920 return 0;
921
David Howells8cefc102019-11-15 13:30:32 +0000922 bytes = n = push_pipe(i, bytes, &i_head, &off);
Al Viro241699c2016-09-22 16:33:12 -0400923 if (unlikely(!n))
924 return 0;
925
David Howells8cefc102019-11-15 13:30:32 +0000926 do {
Al Viro241699c2016-09-22 16:33:12 -0400927 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
Al Viro893839f2021-04-30 18:39:25 -0400928 char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page);
929 memset(p + off, 0, chunk);
930 kunmap_local(p);
David Howells8cefc102019-11-15 13:30:32 +0000931 i->head = i_head;
Al Viro241699c2016-09-22 16:33:12 -0400932 i->iov_offset = off + chunk;
933 n -= chunk;
David Howells8cefc102019-11-15 13:30:32 +0000934 off = 0;
935 i_head++;
936 } while (n);
Al Viro241699c2016-09-22 16:33:12 -0400937 i->count -= bytes;
938 return bytes;
939}
940
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400941size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
942{
David Howells00e23702018-10-22 13:07:28 +0100943 if (unlikely(iov_iter_is_pipe(i)))
Al Viro241699c2016-09-22 16:33:12 -0400944 return pipe_zero(bytes, i);
Al Viro7baa5092021-05-02 11:35:03 -0400945 iterate_and_advance(i, bytes, base, len, count,
946 clear_user(base, len),
947 memset(base, 0, len)
Al Viro8442fa42014-11-27 14:18:54 -0500948 )
949
950 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400951}
952EXPORT_SYMBOL(iov_iter_zero);
953
Al Virof0b65f32021-04-30 10:26:41 -0400954size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
955 struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400956{
Al Viro04a31162014-11-27 13:51:41 -0500957 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
Al Viro72e809e2017-06-29 21:52:57 -0400958 if (unlikely(!page_copy_sane(page, offset, bytes))) {
959 kunmap_atomic(kaddr);
960 return 0;
961 }
David Howells9ea9ce02018-10-20 00:57:56 +0100962 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -0400963 kunmap_atomic(kaddr);
964 WARN_ON(1);
965 return 0;
966 }
Al Viro7baa5092021-05-02 11:35:03 -0400967 iterate_and_advance(i, bytes, base, len, off,
968 copyin(p + off, base, len),
969 memcpy(p + off, base, len)
Al Viro04a31162014-11-27 13:51:41 -0500970 )
971 kunmap_atomic(kaddr);
972 return bytes;
Al Viro62a80672014-04-04 23:12:29 -0400973}
Al Virof0b65f32021-04-30 10:26:41 -0400974EXPORT_SYMBOL(copy_page_from_iter_atomic);
Al Viro62a80672014-04-04 23:12:29 -0400975
Al Virob9dc6f62017-01-14 19:33:08 -0500976static inline void pipe_truncate(struct iov_iter *i)
Al Viro241699c2016-09-22 16:33:12 -0400977{
978 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +0000979 unsigned int p_tail = pipe->tail;
980 unsigned int p_head = pipe->head;
981 unsigned int p_mask = pipe->ring_size - 1;
982
983 if (!pipe_empty(p_head, p_tail)) {
984 struct pipe_buffer *buf;
985 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -0500986 size_t off = i->iov_offset;
David Howells8cefc102019-11-15 13:30:32 +0000987
Al Virob9dc6f62017-01-14 19:33:08 -0500988 if (off) {
David Howells8cefc102019-11-15 13:30:32 +0000989 buf = &pipe->bufs[i_head & p_mask];
990 buf->len = off - buf->offset;
991 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -0500992 }
David Howells8cefc102019-11-15 13:30:32 +0000993 while (p_head != i_head) {
994 p_head--;
995 pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]);
Al Viro241699c2016-09-22 16:33:12 -0400996 }
David Howells8cefc102019-11-15 13:30:32 +0000997
998 pipe->head = p_head;
Al Viro241699c2016-09-22 16:33:12 -0400999 }
Al Virob9dc6f62017-01-14 19:33:08 -05001000}
1001
1002static void pipe_advance(struct iov_iter *i, size_t size)
1003{
1004 struct pipe_inode_info *pipe = i->pipe;
Al Virob9dc6f62017-01-14 19:33:08 -05001005 if (size) {
1006 struct pipe_buffer *buf;
David Howells8cefc102019-11-15 13:30:32 +00001007 unsigned int p_mask = pipe->ring_size - 1;
1008 unsigned int i_head = i->head;
Al Virob9dc6f62017-01-14 19:33:08 -05001009 size_t off = i->iov_offset, left = size;
David Howells8cefc102019-11-15 13:30:32 +00001010
Al Virob9dc6f62017-01-14 19:33:08 -05001011 if (off) /* make it relative to the beginning of buffer */
David Howells8cefc102019-11-15 13:30:32 +00001012 left += off - pipe->bufs[i_head & p_mask].offset;
Al Virob9dc6f62017-01-14 19:33:08 -05001013 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001014 buf = &pipe->bufs[i_head & p_mask];
Al Virob9dc6f62017-01-14 19:33:08 -05001015 if (left <= buf->len)
1016 break;
1017 left -= buf->len;
David Howells8cefc102019-11-15 13:30:32 +00001018 i_head++;
Al Virob9dc6f62017-01-14 19:33:08 -05001019 }
David Howells8cefc102019-11-15 13:30:32 +00001020 i->head = i_head;
Al Virob9dc6f62017-01-14 19:33:08 -05001021 i->iov_offset = buf->offset + left;
1022 }
1023 i->count -= size;
1024 /* ... and discard everything past that point */
1025 pipe_truncate(i);
Al Viro241699c2016-09-22 16:33:12 -04001026}
1027
Pavel Begunkov54c81952021-01-09 16:03:01 +00001028static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
1029{
1030 struct bvec_iter bi;
1031
1032 bi.bi_size = i->count;
1033 bi.bi_bvec_done = i->iov_offset;
1034 bi.bi_idx = 0;
1035 bvec_iter_advance(i->bvec, &bi, size);
1036
1037 i->bvec += bi.bi_idx;
1038 i->nr_segs -= bi.bi_idx;
1039 i->count = bi.bi_size;
1040 i->iov_offset = bi.bi_bvec_done;
1041}
1042
Al Viro185ac4d2021-04-23 12:58:53 -04001043static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
1044{
1045 const struct iovec *iov, *end;
1046
1047 if (!i->count)
1048 return;
1049 i->count -= size;
1050
1051 size += i->iov_offset; // from beginning of current segment
1052 for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
1053 if (likely(size < iov->iov_len))
1054 break;
1055 size -= iov->iov_len;
1056 }
1057 i->iov_offset = size;
1058 i->nr_segs -= iov - i->iov;
1059 i->iov = iov;
1060}
1061
Al Viro62a80672014-04-04 23:12:29 -04001062void iov_iter_advance(struct iov_iter *i, size_t size)
1063{
Al Viro3b3fc052021-04-23 22:24:08 -04001064 if (unlikely(i->count < size))
1065 size = i->count;
Al Viro185ac4d2021-04-23 12:58:53 -04001066 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
1067 /* iovec and kvec have identical layouts */
1068 iov_iter_iovec_advance(i, size);
1069 } else if (iov_iter_is_bvec(i)) {
1070 iov_iter_bvec_advance(i, size);
1071 } else if (iov_iter_is_pipe(i)) {
Al Viro241699c2016-09-22 16:33:12 -04001072 pipe_advance(i, size);
Al Viro185ac4d2021-04-23 12:58:53 -04001073 } else if (unlikely(iov_iter_is_xarray(i))) {
David Howells7ff5062072020-02-10 10:00:21 +00001074 i->iov_offset += size;
1075 i->count -= size;
Al Viro185ac4d2021-04-23 12:58:53 -04001076 } else if (iov_iter_is_discard(i)) {
1077 i->count -= size;
David Howells7ff5062072020-02-10 10:00:21 +00001078 }
Al Viro62a80672014-04-04 23:12:29 -04001079}
1080EXPORT_SYMBOL(iov_iter_advance);
1081
Al Viro27c0e372017-02-17 18:42:24 -05001082void iov_iter_revert(struct iov_iter *i, size_t unroll)
1083{
1084 if (!unroll)
1085 return;
Al Viro5b47d592017-05-08 13:54:47 -04001086 if (WARN_ON(unroll > MAX_RW_COUNT))
1087 return;
Al Viro27c0e372017-02-17 18:42:24 -05001088 i->count += unroll;
David Howells00e23702018-10-22 13:07:28 +01001089 if (unlikely(iov_iter_is_pipe(i))) {
Al Viro27c0e372017-02-17 18:42:24 -05001090 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001091 unsigned int p_mask = pipe->ring_size - 1;
1092 unsigned int i_head = i->head;
Al Viro27c0e372017-02-17 18:42:24 -05001093 size_t off = i->iov_offset;
1094 while (1) {
David Howells8cefc102019-11-15 13:30:32 +00001095 struct pipe_buffer *b = &pipe->bufs[i_head & p_mask];
1096 size_t n = off - b->offset;
Al Viro27c0e372017-02-17 18:42:24 -05001097 if (unroll < n) {
Al Viro4fa55ce2017-04-29 16:42:30 -04001098 off -= unroll;
Al Viro27c0e372017-02-17 18:42:24 -05001099 break;
1100 }
1101 unroll -= n;
David Howells8cefc102019-11-15 13:30:32 +00001102 if (!unroll && i_head == i->start_head) {
Al Viro27c0e372017-02-17 18:42:24 -05001103 off = 0;
1104 break;
1105 }
David Howells8cefc102019-11-15 13:30:32 +00001106 i_head--;
1107 b = &pipe->bufs[i_head & p_mask];
1108 off = b->offset + b->len;
Al Viro27c0e372017-02-17 18:42:24 -05001109 }
1110 i->iov_offset = off;
David Howells8cefc102019-11-15 13:30:32 +00001111 i->head = i_head;
Al Viro27c0e372017-02-17 18:42:24 -05001112 pipe_truncate(i);
1113 return;
1114 }
David Howells9ea9ce02018-10-20 00:57:56 +01001115 if (unlikely(iov_iter_is_discard(i)))
1116 return;
Al Viro27c0e372017-02-17 18:42:24 -05001117 if (unroll <= i->iov_offset) {
1118 i->iov_offset -= unroll;
1119 return;
1120 }
1121 unroll -= i->iov_offset;
David Howells7ff5062072020-02-10 10:00:21 +00001122 if (iov_iter_is_xarray(i)) {
1123 BUG(); /* We should never go beyond the start of the specified
1124 * range since we might then be straying into pages that
1125 * aren't pinned.
1126 */
1127 } else if (iov_iter_is_bvec(i)) {
Al Viro27c0e372017-02-17 18:42:24 -05001128 const struct bio_vec *bvec = i->bvec;
1129 while (1) {
1130 size_t n = (--bvec)->bv_len;
1131 i->nr_segs++;
1132 if (unroll <= n) {
1133 i->bvec = bvec;
1134 i->iov_offset = n - unroll;
1135 return;
1136 }
1137 unroll -= n;
1138 }
1139 } else { /* same logics for iovec and kvec */
1140 const struct iovec *iov = i->iov;
1141 while (1) {
1142 size_t n = (--iov)->iov_len;
1143 i->nr_segs++;
1144 if (unroll <= n) {
1145 i->iov = iov;
1146 i->iov_offset = n - unroll;
1147 return;
1148 }
1149 unroll -= n;
1150 }
1151 }
1152}
1153EXPORT_SYMBOL(iov_iter_revert);
1154
Al Viro62a80672014-04-04 23:12:29 -04001155/*
1156 * Return the count of just the current iov_iter segment.
1157 */
1158size_t iov_iter_single_seg_count(const struct iov_iter *i)
1159{
Al Viro28f38db2021-06-02 17:25:59 -04001160 if (i->nr_segs > 1) {
1161 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1162 return min(i->count, i->iov->iov_len - i->iov_offset);
1163 if (iov_iter_is_bvec(i))
1164 return min(i->count, i->bvec->bv_len - i->iov_offset);
1165 }
1166 return i->count;
Al Viro62a80672014-04-04 23:12:29 -04001167}
1168EXPORT_SYMBOL(iov_iter_single_seg_count);
1169
David Howellsaa563d72018-10-20 00:57:56 +01001170void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001171 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -05001172 size_t count)
1173{
David Howellsaa563d72018-10-20 00:57:56 +01001174 WARN_ON(direction & ~(READ | WRITE));
Al Viro8cd54c12021-04-22 14:50:39 -04001175 *i = (struct iov_iter){
1176 .iter_type = ITER_KVEC,
1177 .data_source = direction,
1178 .kvec = kvec,
1179 .nr_segs = nr_segs,
1180 .iov_offset = 0,
1181 .count = count
1182 };
Al Viroabb78f82014-11-24 14:46:11 -05001183}
1184EXPORT_SYMBOL(iov_iter_kvec);
1185
David Howellsaa563d72018-10-20 00:57:56 +01001186void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
Al Viro05afcb72015-01-23 01:08:07 -05001187 const struct bio_vec *bvec, unsigned long nr_segs,
1188 size_t count)
1189{
David Howellsaa563d72018-10-20 00:57:56 +01001190 WARN_ON(direction & ~(READ | WRITE));
Al Viro8cd54c12021-04-22 14:50:39 -04001191 *i = (struct iov_iter){
1192 .iter_type = ITER_BVEC,
1193 .data_source = direction,
1194 .bvec = bvec,
1195 .nr_segs = nr_segs,
1196 .iov_offset = 0,
1197 .count = count
1198 };
Al Viro05afcb72015-01-23 01:08:07 -05001199}
1200EXPORT_SYMBOL(iov_iter_bvec);
1201
David Howellsaa563d72018-10-20 00:57:56 +01001202void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
Al Viro241699c2016-09-22 16:33:12 -04001203 struct pipe_inode_info *pipe,
1204 size_t count)
1205{
David Howellsaa563d72018-10-20 00:57:56 +01001206 BUG_ON(direction != READ);
David Howells8cefc102019-11-15 13:30:32 +00001207 WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
Al Viro8cd54c12021-04-22 14:50:39 -04001208 *i = (struct iov_iter){
1209 .iter_type = ITER_PIPE,
1210 .data_source = false,
1211 .pipe = pipe,
1212 .head = pipe->head,
1213 .start_head = pipe->head,
1214 .iov_offset = 0,
1215 .count = count
1216 };
Al Viro241699c2016-09-22 16:33:12 -04001217}
1218EXPORT_SYMBOL(iov_iter_pipe);
1219
David Howells9ea9ce02018-10-20 00:57:56 +01001220/**
David Howells7ff5062072020-02-10 10:00:21 +00001221 * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
1222 * @i: The iterator to initialise.
1223 * @direction: The direction of the transfer.
1224 * @xarray: The xarray to access.
1225 * @start: The start file position.
1226 * @count: The size of the I/O buffer in bytes.
1227 *
1228 * Set up an I/O iterator to either draw data out of the pages attached to an
1229 * inode or to inject data into those pages. The pages *must* be prevented
1230 * from evaporation, either by taking a ref on them or locking them by the
1231 * caller.
1232 */
1233void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
1234 struct xarray *xarray, loff_t start, size_t count)
1235{
1236 BUG_ON(direction & ~1);
Al Viro8cd54c12021-04-22 14:50:39 -04001237 *i = (struct iov_iter) {
1238 .iter_type = ITER_XARRAY,
1239 .data_source = direction,
1240 .xarray = xarray,
1241 .xarray_start = start,
1242 .count = count,
1243 .iov_offset = 0
1244 };
David Howells7ff5062072020-02-10 10:00:21 +00001245}
1246EXPORT_SYMBOL(iov_iter_xarray);
1247
1248/**
David Howells9ea9ce02018-10-20 00:57:56 +01001249 * iov_iter_discard - Initialise an I/O iterator that discards data
1250 * @i: The iterator to initialise.
1251 * @direction: The direction of the transfer.
1252 * @count: The size of the I/O buffer in bytes.
1253 *
1254 * Set up an I/O iterator that just discards everything that's written to it.
1255 * It's only available as a READ iterator.
1256 */
1257void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
1258{
1259 BUG_ON(direction != READ);
Al Viro8cd54c12021-04-22 14:50:39 -04001260 *i = (struct iov_iter){
1261 .iter_type = ITER_DISCARD,
1262 .data_source = false,
1263 .count = count,
1264 .iov_offset = 0
1265 };
David Howells9ea9ce02018-10-20 00:57:56 +01001266}
1267EXPORT_SYMBOL(iov_iter_discard);
1268
Al Viro9221d2e2021-04-25 00:44:35 -04001269static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -04001270{
Al Viro04a31162014-11-27 13:51:41 -05001271 unsigned long res = 0;
1272 size_t size = i->count;
Al Viro9221d2e2021-04-25 00:44:35 -04001273 size_t skip = i->iov_offset;
1274 unsigned k;
Al Viro04a31162014-11-27 13:51:41 -05001275
Al Viro9221d2e2021-04-25 00:44:35 -04001276 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1277 size_t len = i->iov[k].iov_len - skip;
1278 if (len) {
1279 res |= (unsigned long)i->iov[k].iov_base + skip;
1280 if (len > size)
1281 len = size;
1282 res |= len;
1283 size -= len;
1284 if (!size)
1285 break;
1286 }
1287 }
1288 return res;
1289}
1290
1291static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
1292{
1293 unsigned res = 0;
1294 size_t size = i->count;
1295 unsigned skip = i->iov_offset;
1296 unsigned k;
1297
1298 for (k = 0; k < i->nr_segs; k++, skip = 0) {
1299 size_t len = i->bvec[k].bv_len - skip;
1300 res |= (unsigned long)i->bvec[k].bv_offset + skip;
1301 if (len > size)
1302 len = size;
1303 res |= len;
1304 size -= len;
1305 if (!size)
1306 break;
1307 }
1308 return res;
1309}
1310
1311unsigned long iov_iter_alignment(const struct iov_iter *i)
1312{
1313 /* iovec and kvec have identical layouts */
1314 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1315 return iov_iter_alignment_iovec(i);
1316
1317 if (iov_iter_is_bvec(i))
1318 return iov_iter_alignment_bvec(i);
1319
1320 if (iov_iter_is_pipe(i)) {
Jan Karae0ff1262019-12-16 11:54:32 +01001321 unsigned int p_mask = i->pipe->ring_size - 1;
Al Viro9221d2e2021-04-25 00:44:35 -04001322 size_t size = i->count;
Jan Karae0ff1262019-12-16 11:54:32 +01001323
David Howells8cefc102019-11-15 13:30:32 +00001324 if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask]))
Al Viro241699c2016-09-22 16:33:12 -04001325 return size | i->iov_offset;
1326 return size;
1327 }
Al Viro9221d2e2021-04-25 00:44:35 -04001328
1329 if (iov_iter_is_xarray(i))
David Howells3d14ec12021-04-25 22:02:38 +01001330 return (i->xarray_start + i->iov_offset) | i->count;
Al Viro9221d2e2021-04-25 00:44:35 -04001331
1332 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001333}
1334EXPORT_SYMBOL(iov_iter_alignment);
1335
Al Viro357f4352016-04-08 19:05:19 -04001336unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1337{
Al Viro33844e62016-12-21 21:55:02 -05001338 unsigned long res = 0;
Al Viro610c7a72021-04-25 01:03:16 -04001339 unsigned long v = 0;
Al Viro357f4352016-04-08 19:05:19 -04001340 size_t size = i->count;
Al Viro610c7a72021-04-25 01:03:16 -04001341 unsigned k;
Al Viro357f4352016-04-08 19:05:19 -04001342
Al Viro610c7a72021-04-25 01:03:16 -04001343 if (WARN_ON(!iter_is_iovec(i)))
Al Viro241699c2016-09-22 16:33:12 -04001344 return ~0U;
Al Viro241699c2016-09-22 16:33:12 -04001345
Al Viro610c7a72021-04-25 01:03:16 -04001346 for (k = 0; k < i->nr_segs; k++) {
1347 if (i->iov[k].iov_len) {
1348 unsigned long base = (unsigned long)i->iov[k].iov_base;
1349 if (v) // if not the first one
1350 res |= base | v; // this start | previous end
1351 v = base + i->iov[k].iov_len;
1352 if (size <= i->iov[k].iov_len)
1353 break;
1354 size -= i->iov[k].iov_len;
1355 }
1356 }
Al Viro33844e62016-12-21 21:55:02 -05001357 return res;
Al Viro357f4352016-04-08 19:05:19 -04001358}
1359EXPORT_SYMBOL(iov_iter_gap_alignment);
1360
Ilya Dryomove76b63122018-05-02 20:16:56 +02001361static inline ssize_t __pipe_get_pages(struct iov_iter *i,
Al Viro241699c2016-09-22 16:33:12 -04001362 size_t maxsize,
1363 struct page **pages,
David Howells8cefc102019-11-15 13:30:32 +00001364 int iter_head,
Al Viro241699c2016-09-22 16:33:12 -04001365 size_t *start)
1366{
1367 struct pipe_inode_info *pipe = i->pipe;
David Howells8cefc102019-11-15 13:30:32 +00001368 unsigned int p_mask = pipe->ring_size - 1;
1369 ssize_t n = push_pipe(i, maxsize, &iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001370 if (!n)
1371 return -EFAULT;
1372
1373 maxsize = n;
1374 n += *start;
Al Viro1689c732016-10-11 18:21:14 +01001375 while (n > 0) {
David Howells8cefc102019-11-15 13:30:32 +00001376 get_page(*pages++ = pipe->bufs[iter_head & p_mask].page);
1377 iter_head++;
Al Viro241699c2016-09-22 16:33:12 -04001378 n -= PAGE_SIZE;
1379 }
1380
1381 return maxsize;
1382}
1383
1384static ssize_t pipe_get_pages(struct iov_iter *i,
1385 struct page **pages, size_t maxsize, unsigned maxpages,
1386 size_t *start)
1387{
David Howells8cefc102019-11-15 13:30:32 +00001388 unsigned int iter_head, npages;
Al Viro241699c2016-09-22 16:33:12 -04001389 size_t capacity;
Al Viro241699c2016-09-22 16:33:12 -04001390
1391 if (!sanity(i))
1392 return -EFAULT;
1393
David Howells8cefc102019-11-15 13:30:32 +00001394 data_start(i, &iter_head, start);
1395 /* Amount of free space: some of this one + all after this one */
1396 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1397 capacity = min(npages, maxpages) * PAGE_SIZE - *start;
Al Viro241699c2016-09-22 16:33:12 -04001398
David Howells8cefc102019-11-15 13:30:32 +00001399 return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001400}
1401
David Howells7ff5062072020-02-10 10:00:21 +00001402static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
1403 pgoff_t index, unsigned int nr_pages)
1404{
1405 XA_STATE(xas, xa, index);
1406 struct page *page;
1407 unsigned int ret = 0;
1408
1409 rcu_read_lock();
1410 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1411 if (xas_retry(&xas, page))
1412 continue;
1413
1414 /* Has the page moved or been split? */
1415 if (unlikely(page != xas_reload(&xas))) {
1416 xas_reset(&xas);
1417 continue;
1418 }
1419
1420 pages[ret] = find_subpage(page, xas.xa_index);
1421 get_page(pages[ret]);
1422 if (++ret == nr_pages)
1423 break;
1424 }
1425 rcu_read_unlock();
1426 return ret;
1427}
1428
1429static ssize_t iter_xarray_get_pages(struct iov_iter *i,
1430 struct page **pages, size_t maxsize,
1431 unsigned maxpages, size_t *_start_offset)
1432{
1433 unsigned nr, offset;
1434 pgoff_t index, count;
1435 size_t size = maxsize, actual;
1436 loff_t pos;
1437
1438 if (!size || !maxpages)
1439 return 0;
1440
1441 pos = i->xarray_start + i->iov_offset;
1442 index = pos >> PAGE_SHIFT;
1443 offset = pos & ~PAGE_MASK;
1444 *_start_offset = offset;
1445
1446 count = 1;
1447 if (size > PAGE_SIZE - offset) {
1448 size -= PAGE_SIZE - offset;
1449 count += size >> PAGE_SHIFT;
1450 size &= ~PAGE_MASK;
1451 if (size)
1452 count++;
1453 }
1454
1455 if (count > maxpages)
1456 count = maxpages;
1457
1458 nr = iter_xarray_populate_pages(pages, i->xarray, index, count);
1459 if (nr == 0)
1460 return 0;
1461
1462 actual = PAGE_SIZE * nr;
1463 actual -= offset;
1464 if (nr == count && size > 0) {
1465 unsigned last_offset = (nr > 1) ? 0 : offset;
1466 actual -= PAGE_SIZE - (last_offset + size);
1467 }
1468 return actual;
1469}
1470
Al Viro3d671ca2021-04-25 09:14:44 -04001471/* must be done on non-empty ITER_IOVEC one */
1472static unsigned long first_iovec_segment(const struct iov_iter *i,
1473 size_t *size, size_t *start,
1474 size_t maxsize, unsigned maxpages)
1475{
1476 size_t skip;
1477 long k;
1478
1479 for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
1480 unsigned long addr = (unsigned long)i->iov[k].iov_base + skip;
1481 size_t len = i->iov[k].iov_len - skip;
1482
1483 if (unlikely(!len))
1484 continue;
1485 if (len > maxsize)
1486 len = maxsize;
1487 len += (*start = addr % PAGE_SIZE);
1488 if (len > maxpages * PAGE_SIZE)
1489 len = maxpages * PAGE_SIZE;
1490 *size = len;
1491 return addr & PAGE_MASK;
1492 }
1493 BUG(); // if it had been empty, we wouldn't get called
1494}
1495
1496/* must be done on non-empty ITER_BVEC one */
1497static struct page *first_bvec_segment(const struct iov_iter *i,
1498 size_t *size, size_t *start,
1499 size_t maxsize, unsigned maxpages)
1500{
1501 struct page *page;
1502 size_t skip = i->iov_offset, len;
1503
1504 len = i->bvec->bv_len - skip;
1505 if (len > maxsize)
1506 len = maxsize;
1507 skip += i->bvec->bv_offset;
1508 page = i->bvec->bv_page + skip / PAGE_SIZE;
1509 len += (*start = skip % PAGE_SIZE);
1510 if (len > maxpages * PAGE_SIZE)
1511 len = maxpages * PAGE_SIZE;
1512 *size = len;
1513 return page;
1514}
1515
Al Viro62a80672014-04-04 23:12:29 -04001516ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +02001517 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -04001518 size_t *start)
1519{
Al Viro3d671ca2021-04-25 09:14:44 -04001520 size_t len;
1521 int n, res;
1522
Al Viroe5393fa2014-11-27 14:12:09 -05001523 if (maxsize > i->count)
1524 maxsize = i->count;
Al Viro3d671ca2021-04-25 09:14:44 -04001525 if (!maxsize)
1526 return 0;
Al Viroe5393fa2014-11-27 14:12:09 -05001527
Al Viro3d671ca2021-04-25 09:14:44 -04001528 if (likely(iter_is_iovec(i))) {
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001529 unsigned int gup_flags = 0;
Al Viro3d671ca2021-04-25 09:14:44 -04001530 unsigned long addr;
David Howells9ea9ce02018-10-20 00:57:56 +01001531
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001532 if (iov_iter_rw(i) != WRITE)
1533 gup_flags |= FOLL_WRITE;
1534 if (i->nofault)
1535 gup_flags |= FOLL_NOFAULT;
1536
Al Viro3d671ca2021-04-25 09:14:44 -04001537 addr = first_iovec_segment(i, &len, start, maxsize, maxpages);
Al Viroe5393fa2014-11-27 14:12:09 -05001538 n = DIV_ROUND_UP(len, PAGE_SIZE);
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001539 res = get_user_pages_fast(addr, n, gup_flags, pages);
Andreas Gruenbacher814a6672021-07-21 19:03:47 +02001540 if (unlikely(res <= 0))
Al Viroe5393fa2014-11-27 14:12:09 -05001541 return res;
1542 return (res == n ? len : res * PAGE_SIZE) - *start;
Al Viro3d671ca2021-04-25 09:14:44 -04001543 }
1544 if (iov_iter_is_bvec(i)) {
1545 struct page *page;
1546
1547 page = first_bvec_segment(i, &len, start, maxsize, maxpages);
1548 n = DIV_ROUND_UP(len, PAGE_SIZE);
1549 while (n--)
1550 get_page(*pages++ = page++);
1551 return len - *start;
1552 }
1553 if (iov_iter_is_pipe(i))
1554 return pipe_get_pages(i, pages, maxsize, maxpages, start);
1555 if (iov_iter_is_xarray(i))
1556 return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1557 return -EFAULT;
Al Viro62a80672014-04-04 23:12:29 -04001558}
1559EXPORT_SYMBOL(iov_iter_get_pages);
1560
Al Viro1b17f1f2014-11-27 14:14:31 -05001561static struct page **get_pages_array(size_t n)
1562{
Michal Hocko752ade62017-05-08 15:57:27 -07001563 return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
Al Viro1b17f1f2014-11-27 14:14:31 -05001564}
1565
Al Viro241699c2016-09-22 16:33:12 -04001566static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
1567 struct page ***pages, size_t maxsize,
1568 size_t *start)
1569{
1570 struct page **p;
David Howells8cefc102019-11-15 13:30:32 +00001571 unsigned int iter_head, npages;
Ilya Dryomovd7760d62018-05-02 20:16:57 +02001572 ssize_t n;
Al Viro241699c2016-09-22 16:33:12 -04001573
1574 if (!sanity(i))
1575 return -EFAULT;
1576
David Howells8cefc102019-11-15 13:30:32 +00001577 data_start(i, &iter_head, start);
1578 /* Amount of free space: some of this one + all after this one */
1579 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
Al Viro241699c2016-09-22 16:33:12 -04001580 n = npages * PAGE_SIZE - *start;
1581 if (maxsize > n)
1582 maxsize = n;
1583 else
1584 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
1585 p = get_pages_array(npages);
1586 if (!p)
1587 return -ENOMEM;
David Howells8cefc102019-11-15 13:30:32 +00001588 n = __pipe_get_pages(i, maxsize, p, iter_head, start);
Al Viro241699c2016-09-22 16:33:12 -04001589 if (n > 0)
1590 *pages = p;
1591 else
1592 kvfree(p);
1593 return n;
1594}
1595
David Howells7ff5062072020-02-10 10:00:21 +00001596static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
1597 struct page ***pages, size_t maxsize,
1598 size_t *_start_offset)
1599{
1600 struct page **p;
1601 unsigned nr, offset;
1602 pgoff_t index, count;
1603 size_t size = maxsize, actual;
1604 loff_t pos;
1605
1606 if (!size)
1607 return 0;
1608
1609 pos = i->xarray_start + i->iov_offset;
1610 index = pos >> PAGE_SHIFT;
1611 offset = pos & ~PAGE_MASK;
1612 *_start_offset = offset;
1613
1614 count = 1;
1615 if (size > PAGE_SIZE - offset) {
1616 size -= PAGE_SIZE - offset;
1617 count += size >> PAGE_SHIFT;
1618 size &= ~PAGE_MASK;
1619 if (size)
1620 count++;
1621 }
1622
1623 p = get_pages_array(count);
1624 if (!p)
1625 return -ENOMEM;
1626 *pages = p;
1627
1628 nr = iter_xarray_populate_pages(p, i->xarray, index, count);
1629 if (nr == 0)
1630 return 0;
1631
1632 actual = PAGE_SIZE * nr;
1633 actual -= offset;
1634 if (nr == count && size > 0) {
1635 unsigned last_offset = (nr > 1) ? 0 : offset;
1636 actual -= PAGE_SIZE - (last_offset + size);
1637 }
1638 return actual;
1639}
1640
Al Viro62a80672014-04-04 23:12:29 -04001641ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
1642 struct page ***pages, size_t maxsize,
1643 size_t *start)
1644{
Al Viro1b17f1f2014-11-27 14:14:31 -05001645 struct page **p;
Al Viro3d671ca2021-04-25 09:14:44 -04001646 size_t len;
1647 int n, res;
Al Viro1b17f1f2014-11-27 14:14:31 -05001648
1649 if (maxsize > i->count)
1650 maxsize = i->count;
Al Viro3d671ca2021-04-25 09:14:44 -04001651 if (!maxsize)
1652 return 0;
Al Viro1b17f1f2014-11-27 14:14:31 -05001653
Al Viro3d671ca2021-04-25 09:14:44 -04001654 if (likely(iter_is_iovec(i))) {
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001655 unsigned int gup_flags = 0;
Al Viro3d671ca2021-04-25 09:14:44 -04001656 unsigned long addr;
David Howells9ea9ce02018-10-20 00:57:56 +01001657
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001658 if (iov_iter_rw(i) != WRITE)
1659 gup_flags |= FOLL_WRITE;
1660 if (i->nofault)
1661 gup_flags |= FOLL_NOFAULT;
1662
Al Viro3d671ca2021-04-25 09:14:44 -04001663 addr = first_iovec_segment(i, &len, start, maxsize, ~0U);
Al Viro1b17f1f2014-11-27 14:14:31 -05001664 n = DIV_ROUND_UP(len, PAGE_SIZE);
1665 p = get_pages_array(n);
1666 if (!p)
1667 return -ENOMEM;
Andreas Gruenbacher3337ab02021-07-12 12:06:14 +02001668 res = get_user_pages_fast(addr, n, gup_flags, p);
Andreas Gruenbacher814a6672021-07-21 19:03:47 +02001669 if (unlikely(res <= 0)) {
Al Viro1b17f1f2014-11-27 14:14:31 -05001670 kvfree(p);
Andreas Gruenbacher814a6672021-07-21 19:03:47 +02001671 *pages = NULL;
Al Viro1b17f1f2014-11-27 14:14:31 -05001672 return res;
1673 }
1674 *pages = p;
1675 return (res == n ? len : res * PAGE_SIZE) - *start;
Al Viro3d671ca2021-04-25 09:14:44 -04001676 }
1677 if (iov_iter_is_bvec(i)) {
1678 struct page *page;
1679
1680 page = first_bvec_segment(i, &len, start, maxsize, ~0U);
1681 n = DIV_ROUND_UP(len, PAGE_SIZE);
1682 *pages = p = get_pages_array(n);
Al Viro1b17f1f2014-11-27 14:14:31 -05001683 if (!p)
1684 return -ENOMEM;
Al Viro3d671ca2021-04-25 09:14:44 -04001685 while (n--)
1686 get_page(*p++ = page++);
1687 return len - *start;
1688 }
1689 if (iov_iter_is_pipe(i))
1690 return pipe_get_pages_alloc(i, pages, maxsize, start);
1691 if (iov_iter_is_xarray(i))
1692 return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
1693 return -EFAULT;
Al Viro62a80672014-04-04 23:12:29 -04001694}
1695EXPORT_SYMBOL(iov_iter_get_pages_alloc);
1696
Al Viroa604ec72014-11-24 01:08:00 -05001697size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1698 struct iov_iter *i)
1699{
Al Viroa604ec72014-11-24 01:08:00 -05001700 __wsum sum, next;
Al Viroa604ec72014-11-24 01:08:00 -05001701 sum = *csum;
David Howells9ea9ce02018-10-20 00:57:56 +01001702 if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001703 WARN_ON(1);
1704 return 0;
1705 }
Al Viro7baa5092021-05-02 11:35:03 -04001706 iterate_and_advance(i, bytes, base, len, off, ({
1707 next = csum_and_copy_from_user(base, addr + off, len);
Al Viro2495bdcc2021-04-30 13:40:48 -04001708 sum = csum_block_add(sum, next, off);
Al Viro7baa5092021-05-02 11:35:03 -04001709 next ? 0 : len;
Al Viroa604ec72014-11-24 01:08:00 -05001710 }), ({
Al Viro7baa5092021-05-02 11:35:03 -04001711 sum = csum_and_memcpy(addr + off, base, len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001712 })
1713 )
1714 *csum = sum;
1715 return bytes;
1716}
1717EXPORT_SYMBOL(csum_and_copy_from_iter);
1718
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001719size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
Al Viroa604ec72014-11-24 01:08:00 -05001720 struct iov_iter *i)
1721{
Willem de Bruijn52cbd232021-02-03 14:29:52 -05001722 struct csum_state *csstate = _csstate;
Al Viroa604ec72014-11-24 01:08:00 -05001723 __wsum sum, next;
Al Viro78e1f382018-11-25 16:24:16 -05001724
Al Viro78e1f382018-11-25 16:24:16 -05001725 if (unlikely(iov_iter_is_discard(i))) {
Al Viro241699c2016-09-22 16:33:12 -04001726 WARN_ON(1); /* for now */
1727 return 0;
1728 }
Al Viro6852df12021-05-02 17:24:40 -04001729
1730 sum = csum_shift(csstate->csum, csstate->off);
1731 if (unlikely(iov_iter_is_pipe(i)))
1732 bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
1733 else iterate_and_advance(i, bytes, base, len, off, ({
Al Viro7baa5092021-05-02 11:35:03 -04001734 next = csum_and_copy_to_user(addr + off, base, len);
Al Viro2495bdcc2021-04-30 13:40:48 -04001735 sum = csum_block_add(sum, next, off);
Al Viro7baa5092021-05-02 11:35:03 -04001736 next ? 0 : len;
Al Viroa604ec72014-11-24 01:08:00 -05001737 }), ({
Al Viro7baa5092021-05-02 11:35:03 -04001738 sum = csum_and_memcpy(base, addr + off, len, sum, off);
Al Viroa604ec72014-11-24 01:08:00 -05001739 })
1740 )
Al Viro594e4502021-06-05 10:19:30 -04001741 csstate->csum = csum_shift(sum, csstate->off);
1742 csstate->off += bytes;
Al Viroa604ec72014-11-24 01:08:00 -05001743 return bytes;
1744}
1745EXPORT_SYMBOL(csum_and_copy_to_iter);
1746
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001747size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1748 struct iov_iter *i)
1749{
Herbert Xu79990962020-06-12 16:57:37 +10001750#ifdef CONFIG_CRYPTO_HASH
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001751 struct ahash_request *hash = hashp;
1752 struct scatterlist sg;
1753 size_t copied;
1754
1755 copied = copy_to_iter(addr, bytes, i);
1756 sg_init_one(&sg, addr, copied);
1757 ahash_request_set_crypt(hash, &sg, NULL, copied);
1758 crypto_ahash_update(hash);
1759 return copied;
YueHaibing27fad742019-04-04 10:31:14 +08001760#else
1761 return 0;
1762#endif
Sagi Grimbergd05f4432018-12-03 17:52:09 -08001763}
1764EXPORT_SYMBOL(hash_and_copy_to_iter);
1765
Al Viro66531c62021-04-25 16:00:48 -04001766static int iov_npages(const struct iov_iter *i, int maxpages)
Al Viro62a80672014-04-04 23:12:29 -04001767{
Al Viro66531c62021-04-25 16:00:48 -04001768 size_t skip = i->iov_offset, size = i->count;
1769 const struct iovec *p;
Al Viroe0f2dc42014-11-27 14:09:46 -05001770 int npages = 0;
1771
Al Viro66531c62021-04-25 16:00:48 -04001772 for (p = i->iov; size; skip = 0, p++) {
1773 unsigned offs = offset_in_page(p->iov_base + skip);
1774 size_t len = min(p->iov_len - skip, size);
Al Viroe0f2dc42014-11-27 14:09:46 -05001775
Al Viro66531c62021-04-25 16:00:48 -04001776 if (len) {
1777 size -= len;
1778 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1779 if (unlikely(npages > maxpages))
1780 return maxpages;
1781 }
1782 }
1783 return npages;
1784}
1785
1786static int bvec_npages(const struct iov_iter *i, int maxpages)
1787{
1788 size_t skip = i->iov_offset, size = i->count;
1789 const struct bio_vec *p;
1790 int npages = 0;
1791
1792 for (p = i->bvec; size; skip = 0, p++) {
1793 unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
1794 size_t len = min(p->bv_len - skip, size);
1795
1796 size -= len;
1797 npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
1798 if (unlikely(npages > maxpages))
1799 return maxpages;
1800 }
1801 return npages;
1802}
1803
1804int iov_iter_npages(const struct iov_iter *i, int maxpages)
1805{
1806 if (unlikely(!i->count))
1807 return 0;
1808 /* iovec and kvec have identical layouts */
1809 if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1810 return iov_npages(i, maxpages);
1811 if (iov_iter_is_bvec(i))
1812 return bvec_npages(i, maxpages);
1813 if (iov_iter_is_pipe(i)) {
David Howells8cefc102019-11-15 13:30:32 +00001814 unsigned int iter_head;
Al Viro66531c62021-04-25 16:00:48 -04001815 int npages;
Al Viro241699c2016-09-22 16:33:12 -04001816 size_t off;
Al Viro241699c2016-09-22 16:33:12 -04001817
1818 if (!sanity(i))
1819 return 0;
1820
David Howells8cefc102019-11-15 13:30:32 +00001821 data_start(i, &iter_head, &off);
Al Viro241699c2016-09-22 16:33:12 -04001822 /* some of this one + all after this one */
Al Viro66531c62021-04-25 16:00:48 -04001823 npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
1824 return min(npages, maxpages);
1825 }
1826 if (iov_iter_is_xarray(i)) {
Al Viroe4f8df82021-05-03 11:05:29 -04001827 unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1828 int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
Al Viro66531c62021-04-25 16:00:48 -04001829 return min(npages, maxpages);
1830 }
1831 return 0;
Al Viro62a80672014-04-04 23:12:29 -04001832}
Al Virof67da302014-03-19 01:16:16 -04001833EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -05001834
1835const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1836{
1837 *new = *old;
David Howells00e23702018-10-22 13:07:28 +01001838 if (unlikely(iov_iter_is_pipe(new))) {
Al Viro241699c2016-09-22 16:33:12 -04001839 WARN_ON(1);
1840 return NULL;
1841 }
David Howells7ff5062072020-02-10 10:00:21 +00001842 if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new)))
David Howells9ea9ce02018-10-20 00:57:56 +01001843 return NULL;
David Howells00e23702018-10-22 13:07:28 +01001844 if (iov_iter_is_bvec(new))
Al Viro4b8164b2015-01-31 20:08:47 -05001845 return new->bvec = kmemdup(new->bvec,
1846 new->nr_segs * sizeof(struct bio_vec),
1847 flags);
1848 else
1849 /* iovec and kvec have identical layout */
1850 return new->iov = kmemdup(new->iov,
1851 new->nr_segs * sizeof(struct iovec),
1852 flags);
1853}
1854EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -04001855
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001856static int copy_compat_iovec_from_user(struct iovec *iov,
1857 const struct iovec __user *uvec, unsigned long nr_segs)
1858{
1859 const struct compat_iovec __user *uiov =
1860 (const struct compat_iovec __user *)uvec;
1861 int ret = -EFAULT, i;
1862
Christoph Hellwiga959a972021-01-11 18:19:26 +01001863 if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001864 return -EFAULT;
1865
1866 for (i = 0; i < nr_segs; i++) {
1867 compat_uptr_t buf;
1868 compat_ssize_t len;
1869
1870 unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1871 unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1872
1873 /* check for compat_size_t not fitting in compat_ssize_t .. */
1874 if (len < 0) {
1875 ret = -EINVAL;
1876 goto uaccess_end;
1877 }
1878 iov[i].iov_base = compat_ptr(buf);
1879 iov[i].iov_len = len;
1880 }
1881
1882 ret = 0;
1883uaccess_end:
1884 user_access_end();
1885 return ret;
1886}
1887
1888static int copy_iovec_from_user(struct iovec *iov,
1889 const struct iovec __user *uvec, unsigned long nr_segs)
David Laightfb041b52020-09-25 06:51:39 +02001890{
1891 unsigned long seg;
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001892
1893 if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1894 return -EFAULT;
1895 for (seg = 0; seg < nr_segs; seg++) {
1896 if ((ssize_t)iov[seg].iov_len < 0)
1897 return -EINVAL;
1898 }
1899
1900 return 0;
1901}
1902
1903struct iovec *iovec_from_user(const struct iovec __user *uvec,
1904 unsigned long nr_segs, unsigned long fast_segs,
1905 struct iovec *fast_iov, bool compat)
1906{
1907 struct iovec *iov = fast_iov;
1908 int ret;
David Laightfb041b52020-09-25 06:51:39 +02001909
1910 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001911 * SuS says "The readv() function *may* fail if the iovcnt argument was
1912 * less than or equal to 0, or greater than {IOV_MAX}. Linux has
David Laightfb041b52020-09-25 06:51:39 +02001913 * traditionally returned zero for zero segments, so...
1914 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001915 if (nr_segs == 0)
1916 return iov;
1917 if (nr_segs > UIO_MAXIOV)
1918 return ERR_PTR(-EINVAL);
David Laightfb041b52020-09-25 06:51:39 +02001919 if (nr_segs > fast_segs) {
1920 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001921 if (!iov)
1922 return ERR_PTR(-ENOMEM);
David Laightfb041b52020-09-25 06:51:39 +02001923 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001924
1925 if (compat)
1926 ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1927 else
1928 ret = copy_iovec_from_user(iov, uvec, nr_segs);
1929 if (ret) {
1930 if (iov != fast_iov)
1931 kfree(iov);
1932 return ERR_PTR(ret);
1933 }
1934
1935 return iov;
1936}
1937
1938ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1939 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1940 struct iov_iter *i, bool compat)
1941{
1942 ssize_t total_len = 0;
1943 unsigned long seg;
1944 struct iovec *iov;
1945
1946 iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1947 if (IS_ERR(iov)) {
1948 *iovp = NULL;
1949 return PTR_ERR(iov);
David Laightfb041b52020-09-25 06:51:39 +02001950 }
1951
1952 /*
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001953 * According to the Single Unix Specification we should return EINVAL if
1954 * an element length is < 0 when cast to ssize_t or if the total length
1955 * would overflow the ssize_t return value of the system call.
David Laightfb041b52020-09-25 06:51:39 +02001956 *
1957 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1958 * overflow case.
1959 */
David Laightfb041b52020-09-25 06:51:39 +02001960 for (seg = 0; seg < nr_segs; seg++) {
David Laightfb041b52020-09-25 06:51:39 +02001961 ssize_t len = (ssize_t)iov[seg].iov_len;
1962
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001963 if (!access_ok(iov[seg].iov_base, len)) {
1964 if (iov != *iovp)
1965 kfree(iov);
1966 *iovp = NULL;
1967 return -EFAULT;
David Laightfb041b52020-09-25 06:51:39 +02001968 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001969
1970 if (len > MAX_RW_COUNT - total_len) {
1971 len = MAX_RW_COUNT - total_len;
David Laightfb041b52020-09-25 06:51:39 +02001972 iov[seg].iov_len = len;
1973 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001974 total_len += len;
David Laightfb041b52020-09-25 06:51:39 +02001975 }
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001976
1977 iov_iter_init(i, type, iov, nr_segs, total_len);
1978 if (iov == *iovp)
1979 *iovp = NULL;
1980 else
1981 *iovp = iov;
1982 return total_len;
David Laightfb041b52020-09-25 06:51:39 +02001983}
1984
Vegard Nossumffecee42016-10-08 11:18:07 +02001985/**
1986 * import_iovec() - Copy an array of &struct iovec from userspace
1987 * into the kernel, check that it is valid, and initialize a new
1988 * &struct iov_iter iterator to access it.
1989 *
1990 * @type: One of %READ or %WRITE.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001991 * @uvec: Pointer to the userspace array.
Vegard Nossumffecee42016-10-08 11:18:07 +02001992 * @nr_segs: Number of elements in userspace array.
1993 * @fast_segs: Number of elements in @iov.
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02001994 * @iovp: (input and output parameter) Pointer to pointer to (usually small
Vegard Nossumffecee42016-10-08 11:18:07 +02001995 * on-stack) kernel array.
1996 * @i: Pointer to iterator that will be initialized on success.
1997 *
1998 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1999 * then this function places %NULL in *@iov on return. Otherwise, a new
2000 * array will be allocated and the result placed in *@iov. This means that
2001 * the caller may call kfree() on *@iov regardless of whether the small
2002 * on-stack array was used or not (and regardless of whether this function
2003 * returns an error or not).
2004 *
Jens Axboe87e5e6d2019-05-14 16:02:22 -06002005 * Return: Negative error code on error, bytes imported on success
Vegard Nossumffecee42016-10-08 11:18:07 +02002006 */
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002007ssize_t import_iovec(int type, const struct iovec __user *uvec,
Al Virobc917be2015-03-21 17:45:43 -04002008 unsigned nr_segs, unsigned fast_segs,
Christoph Hellwigbfdc5972020-09-25 06:51:40 +02002009 struct iovec **iovp, struct iov_iter *i)
Al Virobc917be2015-03-21 17:45:43 -04002010{
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02002011 return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
2012 in_compat_syscall());
Al Virobc917be2015-03-21 17:45:43 -04002013}
2014EXPORT_SYMBOL(import_iovec);
2015
Al Virobc917be2015-03-21 17:45:43 -04002016int import_single_range(int rw, void __user *buf, size_t len,
2017 struct iovec *iov, struct iov_iter *i)
2018{
2019 if (len > MAX_RW_COUNT)
2020 len = MAX_RW_COUNT;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002021 if (unlikely(!access_ok(buf, len)))
Al Virobc917be2015-03-21 17:45:43 -04002022 return -EFAULT;
2023
2024 iov->iov_base = buf;
2025 iov->iov_len = len;
2026 iov_iter_init(i, rw, iov, 1, len);
2027 return 0;
2028}
Al Viroe1267582015-12-06 20:38:56 -05002029EXPORT_SYMBOL(import_single_range);
Jens Axboe8fb0f472021-09-10 11:18:36 -06002030
2031/**
2032 * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
2033 * iov_iter_save_state() was called.
2034 *
2035 * @i: &struct iov_iter to restore
2036 * @state: state to restore from
2037 *
2038 * Used after iov_iter_save_state() to bring restore @i, if operations may
2039 * have advanced it.
2040 *
2041 * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
2042 */
2043void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
2044{
2045 if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
2046 !iov_iter_is_kvec(i))
2047 return;
2048 i->iov_offset = state->iov_offset;
2049 i->count = state->count;
2050 /*
2051 * For the *vec iters, nr_segs + iov is constant - if we increment
2052 * the vec, then we also decrement the nr_segs count. Hence we don't
2053 * need to track both of these, just one is enough and we can deduct
2054 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
2055 * size, so we can just increment the iov pointer as they are unionzed.
2056 * ITER_BVEC _may_ be the same size on some archs, but on others it is
2057 * not. Be safe and handle it separately.
2058 */
2059 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
2060 if (iov_iter_is_bvec(i))
2061 i->bvec -= state->nr_segs - i->nr_segs;
2062 else
2063 i->iov -= state->nr_segs - i->nr_segs;
2064 i->nr_segs = state->nr_segs;
2065}