Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Herbert Xu | 7999096 | 2020-06-12 16:57:37 +1000 | [diff] [blame] | 2 | #include <crypto/hash.h> |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 3 | #include <linux/export.h> |
Christoph Hellwig | 2f8b544 | 2016-11-01 07:40:13 -0600 | [diff] [blame] | 4 | #include <linux/bvec.h> |
Albert van der Linde | 4d0e9df | 2020-10-15 20:13:50 -0700 | [diff] [blame] | 5 | #include <linux/fault-inject-usercopy.h> |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 6 | #include <linux/uio.h> |
| 7 | #include <linux/pagemap.h> |
Ira Weiny | 2896199 | 2021-05-04 18:40:03 -0700 | [diff] [blame] | 8 | #include <linux/highmem.h> |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 9 | #include <linux/slab.h> |
| 10 | #include <linux/vmalloc.h> |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 11 | #include <linux/splice.h> |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 12 | #include <linux/compat.h> |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 13 | #include <net/checksum.h> |
Sagi Grimberg | d05f443 | 2018-12-03 17:52:09 -0800 | [diff] [blame] | 14 | #include <linux/scatterlist.h> |
Marco Elver | d0ef4c3 | 2020-01-21 17:05:11 +0100 | [diff] [blame] | 15 | #include <linux/instrumented.h> |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 16 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 17 | #define PIPE_PARANOIA /* for now */ |
| 18 | |
Al Viro | 5c67aa9 | 2021-04-25 23:57:42 -0400 | [diff] [blame] | 19 | /* covers iovec and kvec alike */ |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 20 | #define iterate_iovec(i, n, base, len, off, __p, STEP) { \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 21 | size_t off = 0; \ |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 22 | size_t skip = i->iov_offset; \ |
Al Viro | 7a1bcb5 | 2021-04-25 23:46:09 -0400 | [diff] [blame] | 23 | do { \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 24 | 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 Viro | 7a1bcb5 | 2021-04-25 23:46:09 -0400 | [diff] [blame] | 31 | if (skip < __p->iov_len) \ |
| 32 | break; \ |
| 33 | } \ |
| 34 | __p++; \ |
| 35 | skip = 0; \ |
| 36 | } while (n); \ |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 37 | i->iov_offset = skip; \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 38 | n = off; \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 41 | #define iterate_bvec(i, n, base, len, off, p, STEP) { \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 42 | size_t off = 0; \ |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 43 | unsigned skip = i->iov_offset; \ |
Al Viro | 7491a2b | 2021-04-26 20:19:14 -0400 | [diff] [blame] | 44 | while (n) { \ |
| 45 | unsigned offset = p->bv_offset + skip; \ |
Al Viro | 1b4fb5f | 2021-04-26 20:33:42 -0400 | [diff] [blame] | 46 | unsigned left; \ |
Al Viro | 21b56c8 | 2021-04-26 20:50:05 -0400 | [diff] [blame] | 47 | void *kaddr = kmap_local_page(p->bv_page + \ |
| 48 | offset / PAGE_SIZE); \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 49 | base = kaddr + offset % PAGE_SIZE; \ |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 50 | len = min(min(n, (size_t)(p->bv_len - skip)), \ |
Al Viro | 7491a2b | 2021-04-26 20:19:14 -0400 | [diff] [blame] | 51 | (size_t)(PAGE_SIZE - offset % PAGE_SIZE)); \ |
Al Viro | 1b4fb5f | 2021-04-26 20:33:42 -0400 | [diff] [blame] | 52 | left = (STEP); \ |
Al Viro | 21b56c8 | 2021-04-26 20:50:05 -0400 | [diff] [blame] | 53 | kunmap_local(kaddr); \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 54 | len -= left; \ |
| 55 | off += len; \ |
| 56 | skip += len; \ |
Al Viro | 7491a2b | 2021-04-26 20:19:14 -0400 | [diff] [blame] | 57 | if (skip == p->bv_len) { \ |
| 58 | skip = 0; \ |
| 59 | p++; \ |
| 60 | } \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 61 | n -= len; \ |
Al Viro | 1b4fb5f | 2021-04-26 20:33:42 -0400 | [diff] [blame] | 62 | if (left) \ |
| 63 | break; \ |
Al Viro | 7491a2b | 2021-04-26 20:19:14 -0400 | [diff] [blame] | 64 | } \ |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 65 | i->iov_offset = skip; \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 66 | n = off; \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 69 | #define iterate_xarray(i, n, base, len, __off, STEP) { \ |
Al Viro | 1b4fb5f | 2021-04-26 20:33:42 -0400 | [diff] [blame] | 70 | __label__ __out; \ |
Al Viro | 622838f | 2021-05-02 11:13:09 -0400 | [diff] [blame] | 71 | size_t __off = 0; \ |
Matthew Wilcox (Oracle) | 821979f | 2021-11-28 19:18:27 -0500 | [diff] [blame] | 72 | struct folio *folio; \ |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 73 | loff_t start = i->xarray_start + i->iov_offset; \ |
Al Viro | 4b179e9 | 2021-05-04 17:50:07 -0400 | [diff] [blame] | 74 | pgoff_t index = start / PAGE_SIZE; \ |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 75 | XA_STATE(xas, i->xarray, index); \ |
| 76 | \ |
Matthew Wilcox (Oracle) | 821979f | 2021-11-28 19:18:27 -0500 | [diff] [blame] | 77 | len = PAGE_SIZE - offset_in_page(start); \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 78 | rcu_read_lock(); \ |
Matthew Wilcox (Oracle) | 821979f | 2021-11-28 19:18:27 -0500 | [diff] [blame] | 79 | xas_for_each(&xas, folio, ULONG_MAX) { \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 80 | unsigned left; \ |
Matthew Wilcox (Oracle) | 821979f | 2021-11-28 19:18:27 -0500 | [diff] [blame] | 81 | size_t offset; \ |
| 82 | if (xas_retry(&xas, folio)) \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 83 | continue; \ |
Matthew Wilcox (Oracle) | 821979f | 2021-11-28 19:18:27 -0500 | [diff] [blame] | 84 | if (WARN_ON(xa_is_value(folio))) \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 85 | break; \ |
Matthew Wilcox (Oracle) | 821979f | 2021-11-28 19:18:27 -0500 | [diff] [blame] | 86 | if (WARN_ON(folio_test_hugetlb(folio))) \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 87 | break; \ |
Matthew Wilcox (Oracle) | 821979f | 2021-11-28 19:18:27 -0500 | [diff] [blame] | 88 | offset = offset_in_folio(folio, start + __off); \ |
| 89 | while (offset < folio_size(folio)) { \ |
| 90 | base = kmap_local_folio(folio, offset); \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 91 | len = min(n, len); \ |
| 92 | left = (STEP); \ |
Matthew Wilcox (Oracle) | 821979f | 2021-11-28 19:18:27 -0500 | [diff] [blame] | 93 | kunmap_local(base); \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 94 | len -= left; \ |
| 95 | __off += len; \ |
| 96 | n -= len; \ |
| 97 | if (left || n == 0) \ |
| 98 | goto __out; \ |
Matthew Wilcox (Oracle) | 821979f | 2021-11-28 19:18:27 -0500 | [diff] [blame] | 99 | offset += len; \ |
| 100 | len = PAGE_SIZE; \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 101 | } \ |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 102 | } \ |
Al Viro | 1b4fb5f | 2021-04-26 20:33:42 -0400 | [diff] [blame] | 103 | __out: \ |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 104 | rcu_read_unlock(); \ |
Matthew Wilcox (Oracle) | 821979f | 2021-11-28 19:18:27 -0500 | [diff] [blame] | 105 | i->iov_offset += __off; \ |
Al Viro | 622838f | 2021-05-02 11:13:09 -0400 | [diff] [blame] | 106 | n = __off; \ |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 109 | #define __iterate_and_advance(i, n, base, len, off, I, K) { \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 110 | if (unlikely(i->count < n)) \ |
| 111 | n = i->count; \ |
Al Viro | f5da835 | 2021-04-28 20:59:08 -0400 | [diff] [blame] | 112 | if (likely(n)) { \ |
Al Viro | 28f38db | 2021-06-02 17:25:59 -0400 | [diff] [blame] | 113 | if (likely(iter_is_iovec(i))) { \ |
Al Viro | 5c67aa9 | 2021-04-25 23:57:42 -0400 | [diff] [blame] | 114 | const struct iovec *iov = i->iov; \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 115 | void __user *base; \ |
| 116 | size_t len; \ |
| 117 | iterate_iovec(i, n, base, len, off, \ |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 118 | iov, (I)) \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 119 | i->nr_segs -= iov - i->iov; \ |
| 120 | i->iov = iov; \ |
Al Viro | 28f38db | 2021-06-02 17:25:59 -0400 | [diff] [blame] | 121 | } else if (iov_iter_is_bvec(i)) { \ |
| 122 | const struct bio_vec *bvec = i->bvec; \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 123 | void *base; \ |
| 124 | size_t len; \ |
| 125 | iterate_bvec(i, n, base, len, off, \ |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 126 | bvec, (K)) \ |
Al Viro | 7491a2b | 2021-04-26 20:19:14 -0400 | [diff] [blame] | 127 | i->nr_segs -= bvec - i->bvec; \ |
| 128 | i->bvec = bvec; \ |
Al Viro | 28f38db | 2021-06-02 17:25:59 -0400 | [diff] [blame] | 129 | } else if (iov_iter_is_kvec(i)) { \ |
Al Viro | 5c67aa9 | 2021-04-25 23:57:42 -0400 | [diff] [blame] | 130 | const struct kvec *kvec = i->kvec; \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 131 | void *base; \ |
| 132 | size_t len; \ |
| 133 | iterate_iovec(i, n, base, len, off, \ |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 134 | kvec, (K)) \ |
Al Viro | 28f38db | 2021-06-02 17:25:59 -0400 | [diff] [blame] | 135 | i->nr_segs -= kvec - i->kvec; \ |
| 136 | i->kvec = kvec; \ |
| 137 | } else if (iov_iter_is_xarray(i)) { \ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 138 | void *base; \ |
| 139 | size_t len; \ |
| 140 | iterate_xarray(i, n, base, len, off, \ |
Al Viro | a6e4ec7 | 2021-05-02 13:03:41 -0400 | [diff] [blame] | 141 | (K)) \ |
Al Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 142 | } \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 143 | i->count -= n; \ |
Al Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 144 | } \ |
Al Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 145 | } |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 146 | #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 Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 148 | |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 149 | static int copyout(void __user *to, const void *from, size_t n) |
| 150 | { |
Albert van der Linde | 4d0e9df | 2020-10-15 20:13:50 -0700 | [diff] [blame] | 151 | if (should_fail_usercopy()) |
| 152 | return n; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 153 | if (access_ok(to, n)) { |
Marco Elver | d0ef4c3 | 2020-01-21 17:05:11 +0100 | [diff] [blame] | 154 | instrument_copy_to_user(to, from, n); |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 155 | n = raw_copy_to_user(to, from, n); |
| 156 | } |
| 157 | return n; |
| 158 | } |
| 159 | |
| 160 | static int copyin(void *to, const void __user *from, size_t n) |
| 161 | { |
Albert van der Linde | 4d0e9df | 2020-10-15 20:13:50 -0700 | [diff] [blame] | 162 | if (should_fail_usercopy()) |
| 163 | return n; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 164 | if (access_ok(from, n)) { |
Marco Elver | d0ef4c3 | 2020-01-21 17:05:11 +0100 | [diff] [blame] | 165 | instrument_copy_from_user(to, from, n); |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 166 | n = raw_copy_from_user(to, from, n); |
| 167 | } |
| 168 | return n; |
| 169 | } |
| 170 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 171 | static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes, |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 172 | 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 Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 185 | might_fault(); |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 186 | 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 Gruenbacher | bb523b4 | 2021-08-02 13:44:20 +0200 | [diff] [blame] | 192 | if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_writeable(buf, copy)) { |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 193 | kaddr = kmap_atomic(page); |
| 194 | from = kaddr + offset; |
| 195 | |
| 196 | /* first chunk, usually the only one */ |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 197 | left = copyout(buf, from, copy); |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 198 | 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 Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 207 | left = copyout(buf, from, copy); |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 208 | 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 Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 223 | |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 224 | kaddr = kmap(page); |
| 225 | from = kaddr + offset; |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 226 | left = copyout(buf, from, copy); |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 227 | 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 Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 235 | left = copyout(buf, from, copy); |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 236 | copy -= left; |
| 237 | skip = copy; |
| 238 | from += copy; |
| 239 | bytes -= copy; |
| 240 | } |
| 241 | kunmap(page); |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 242 | |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 243 | done: |
Al Viro | 81055e5 | 2014-04-04 19:23:46 -0400 | [diff] [blame] | 244 | if (skip == iov->iov_len) { |
| 245 | iov++; |
| 246 | skip = 0; |
| 247 | } |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 248 | 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 Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 254 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 255 | static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes, |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 256 | 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 Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 269 | might_fault(); |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 270 | 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 Gruenbacher | bb523b4 | 2021-08-02 13:44:20 +0200 | [diff] [blame] | 276 | if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_readable(buf, copy)) { |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 277 | kaddr = kmap_atomic(page); |
| 278 | to = kaddr + offset; |
| 279 | |
| 280 | /* first chunk, usually the only one */ |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 281 | left = copyin(to, buf, copy); |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 282 | 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 Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 291 | left = copyin(to, buf, copy); |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 292 | 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 Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 307 | |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 308 | kaddr = kmap(page); |
| 309 | to = kaddr + offset; |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 310 | left = copyin(to, buf, copy); |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 311 | 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 Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 319 | left = copyin(to, buf, copy); |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 320 | copy -= left; |
| 321 | skip = copy; |
| 322 | to += copy; |
| 323 | bytes -= copy; |
| 324 | } |
| 325 | kunmap(page); |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 326 | |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 327 | done: |
Al Viro | 81055e5 | 2014-04-04 19:23:46 -0400 | [diff] [blame] | 328 | if (skip == iov->iov_len) { |
| 329 | iov++; |
| 330 | skip = 0; |
| 331 | } |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 332 | 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 Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 338 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 339 | #ifdef PIPE_PARANOIA |
| 340 | static bool sanity(const struct iov_iter *i) |
| 341 | { |
| 342 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 343 | 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 Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 350 | if (i->iov_offset) { |
| 351 | struct pipe_buffer *p; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 352 | if (unlikely(p_occupancy == 0)) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 353 | goto Bad; // pipe must be non-empty |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 354 | if (unlikely(i_head != p_head - 1)) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 355 | goto Bad; // must be at the last buffer... |
| 356 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 357 | p = &pipe->bufs[i_head & p_mask]; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 358 | if (unlikely(p->offset + p->len != i->iov_offset)) |
| 359 | goto Bad; // ... at the end of segment |
| 360 | } else { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 361 | if (i_head != p_head) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 362 | goto Bad; // must be right after the last buffer |
| 363 | } |
| 364 | return true; |
| 365 | Bad: |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 366 | 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 Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 370 | 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 Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 382 | static 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 Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 387 | unsigned int p_tail = pipe->tail; |
| 388 | unsigned int p_mask = pipe->ring_size - 1; |
| 389 | unsigned int i_head = i->head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 390 | size_t off; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 391 | |
| 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 Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 402 | buf = &pipe->bufs[i_head & p_mask]; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 403 | 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 Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 410 | i_head++; |
| 411 | buf = &pipe->bufs[i_head & p_mask]; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 412 | } |
David Howells | 6718b6f | 2019-10-16 16:47:32 +0100 | [diff] [blame] | 413 | if (pipe_full(i_head, p_tail, pipe->max_usage)) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 414 | return 0; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 415 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 416 | buf->ops = &page_cache_pipe_buf_ops; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 417 | get_page(page); |
| 418 | buf->page = page; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 419 | buf->offset = offset; |
| 420 | buf->len = bytes; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 421 | |
| 422 | pipe->head = i_head + 1; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 423 | i->iov_offset = offset + bytes; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 424 | i->head = i_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 425 | out: |
| 426 | i->count -= bytes; |
| 427 | return bytes; |
| 428 | } |
| 429 | |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 430 | /* |
Andreas Gruenbacher | a629459 | 2021-08-02 14:54:16 +0200 | [diff] [blame] | 431 | * fault_in_iov_iter_readable - fault in iov iterator for reading |
| 432 | * @i: iterator |
| 433 | * @size: maximum length |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 434 | * |
Andreas Gruenbacher | a629459 | 2021-08-02 14:54:16 +0200 | [diff] [blame] | 435 | * 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 Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 442 | */ |
Andreas Gruenbacher | a629459 | 2021-08-02 14:54:16 +0200 | [diff] [blame] | 443 | size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size) |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 444 | { |
Al Viro | 0e8f0d6 | 2021-06-02 14:48:21 -0400 | [diff] [blame] | 445 | if (iter_is_iovec(i)) { |
Andreas Gruenbacher | a629459 | 2021-08-02 14:54:16 +0200 | [diff] [blame] | 446 | size_t count = min(size, iov_iter_count(i)); |
Al Viro | 8409a0d | 2021-05-02 11:57:37 -0400 | [diff] [blame] | 447 | const struct iovec *p; |
| 448 | size_t skip; |
| 449 | |
Andreas Gruenbacher | a629459 | 2021-08-02 14:54:16 +0200 | [diff] [blame] | 450 | 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 Viro | 8409a0d | 2021-05-02 11:57:37 -0400 | [diff] [blame] | 454 | |
| 455 | if (unlikely(!len)) |
| 456 | continue; |
Andreas Gruenbacher | a629459 | 2021-08-02 14:54:16 +0200 | [diff] [blame] | 457 | ret = fault_in_readable(p->iov_base + skip, len); |
| 458 | count -= len - ret; |
| 459 | if (ret) |
| 460 | break; |
Al Viro | 8409a0d | 2021-05-02 11:57:37 -0400 | [diff] [blame] | 461 | } |
Andreas Gruenbacher | a629459 | 2021-08-02 14:54:16 +0200 | [diff] [blame] | 462 | return count + size; |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 463 | } |
| 464 | return 0; |
| 465 | } |
Andreas Gruenbacher | a629459 | 2021-08-02 14:54:16 +0200 | [diff] [blame] | 466 | EXPORT_SYMBOL(fault_in_iov_iter_readable); |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 467 | |
Andreas Gruenbacher | cdd591f | 2021-07-05 17:26:28 +0200 | [diff] [blame] | 468 | /* |
| 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 | */ |
| 482 | size_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 | } |
| 505 | EXPORT_SYMBOL(fault_in_iov_iter_writeable); |
| 506 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 507 | void iov_iter_init(struct iov_iter *i, unsigned int direction, |
Al Viro | 71d8e53 | 2014-03-05 19:28:09 -0500 | [diff] [blame] | 508 | const struct iovec *iov, unsigned long nr_segs, |
| 509 | size_t count) |
| 510 | { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 511 | WARN_ON(direction & ~(READ | WRITE)); |
Al Viro | 8cd54c1 | 2021-04-22 14:50:39 -0400 | [diff] [blame] | 512 | *i = (struct iov_iter) { |
| 513 | .iter_type = ITER_IOVEC, |
Andreas Gruenbacher | 3337ab0 | 2021-07-12 12:06:14 +0200 | [diff] [blame] | 514 | .nofault = false, |
Al Viro | 8cd54c1 | 2021-04-22 14:50:39 -0400 | [diff] [blame] | 515 | .data_source = direction, |
| 516 | .iov = iov, |
| 517 | .nr_segs = nr_segs, |
| 518 | .iov_offset = 0, |
| 519 | .count = count |
| 520 | }; |
Al Viro | 71d8e53 | 2014-03-05 19:28:09 -0500 | [diff] [blame] | 521 | } |
| 522 | EXPORT_SYMBOL(iov_iter_init); |
Al Viro | 7b2c99d | 2014-03-15 04:05:57 -0400 | [diff] [blame] | 523 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 524 | static inline bool allocated(struct pipe_buffer *buf) |
| 525 | { |
| 526 | return buf->ops == &default_pipe_buf_ops; |
| 527 | } |
| 528 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 529 | static inline void data_start(const struct iov_iter *i, |
| 530 | unsigned int *iter_headp, size_t *offp) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 531 | { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 532 | unsigned int p_mask = i->pipe->ring_size - 1; |
| 533 | unsigned int iter_head = i->head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 534 | size_t off = i->iov_offset; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 535 | |
| 536 | if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) || |
| 537 | off == PAGE_SIZE)) { |
| 538 | iter_head++; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 539 | off = 0; |
| 540 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 541 | *iter_headp = iter_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 542 | *offp = off; |
| 543 | } |
| 544 | |
| 545 | static size_t push_pipe(struct iov_iter *i, size_t size, |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 546 | int *iter_headp, size_t *offp) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 547 | { |
| 548 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 549 | unsigned int p_tail = pipe->tail; |
| 550 | unsigned int p_mask = pipe->ring_size - 1; |
| 551 | unsigned int iter_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 552 | size_t off; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 553 | 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 Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 561 | data_start(i, &iter_head, &off); |
| 562 | *iter_headp = iter_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 563 | *offp = off; |
| 564 | if (off) { |
| 565 | left -= PAGE_SIZE - off; |
| 566 | if (left <= 0) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 567 | pipe->bufs[iter_head & p_mask].len += size; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 568 | return size; |
| 569 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 570 | pipe->bufs[iter_head & p_mask].len = PAGE_SIZE; |
| 571 | iter_head++; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 572 | } |
David Howells | 6718b6f | 2019-10-16 16:47:32 +0100 | [diff] [blame] | 573 | while (!pipe_full(iter_head, p_tail, pipe->max_usage)) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 574 | struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask]; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 575 | struct page *page = alloc_page(GFP_USER); |
| 576 | if (!page) |
| 577 | break; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 578 | |
| 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 Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 588 | return size; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 589 | } |
| 590 | return size - left; |
| 591 | } |
| 592 | |
| 593 | static 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 Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 597 | unsigned int p_mask = pipe->ring_size - 1; |
| 598 | unsigned int i_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 599 | size_t n, off; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 600 | |
| 601 | if (!sanity(i)) |
| 602 | return 0; |
| 603 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 604 | bytes = n = push_pipe(i, bytes, &i_head, &off); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 605 | if (unlikely(!n)) |
| 606 | return 0; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 607 | do { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 608 | size_t chunk = min_t(size_t, n, PAGE_SIZE - off); |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 609 | memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk); |
| 610 | i->head = i_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 611 | i->iov_offset = off + chunk; |
| 612 | n -= chunk; |
| 613 | addr += chunk; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 614 | off = 0; |
| 615 | i_head++; |
| 616 | } while (n); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 617 | i->count -= bytes; |
| 618 | return bytes; |
| 619 | } |
| 620 | |
Al Viro | f915289 | 2018-11-27 22:32:59 -0500 | [diff] [blame] | 621 | static __wsum csum_and_memcpy(void *to, const void *from, size_t len, |
| 622 | __wsum sum, size_t off) |
| 623 | { |
Al Viro | cc44c17 | 2020-07-11 00:12:07 -0400 | [diff] [blame] | 624 | __wsum next = csum_partial_copy_nocheck(from, to, len); |
Al Viro | f915289 | 2018-11-27 22:32:59 -0500 | [diff] [blame] | 625 | return csum_block_add(sum, next, off); |
| 626 | } |
| 627 | |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 628 | static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes, |
Al Viro | 6852df1 | 2021-05-02 17:24:40 -0400 | [diff] [blame] | 629 | struct iov_iter *i, __wsum *sump) |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 630 | { |
| 631 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 632 | unsigned int p_mask = pipe->ring_size - 1; |
Al Viro | 6852df1 | 2021-05-02 17:24:40 -0400 | [diff] [blame] | 633 | __wsum sum = *sump; |
| 634 | size_t off = 0; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 635 | unsigned int i_head; |
Al Viro | 6852df1 | 2021-05-02 17:24:40 -0400 | [diff] [blame] | 636 | size_t r; |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 637 | |
| 638 | if (!sanity(i)) |
| 639 | return 0; |
| 640 | |
Al Viro | 6852df1 | 2021-05-02 17:24:40 -0400 | [diff] [blame] | 641 | bytes = push_pipe(i, bytes, &i_head, &r); |
| 642 | while (bytes) { |
| 643 | size_t chunk = min_t(size_t, bytes, PAGE_SIZE - r); |
Al Viro | 2495bdcc | 2021-04-30 13:40:48 -0400 | [diff] [blame] | 644 | char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page); |
Al Viro | 6852df1 | 2021-05-02 17:24:40 -0400 | [diff] [blame] | 645 | sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off); |
Al Viro | 2495bdcc | 2021-04-30 13:40:48 -0400 | [diff] [blame] | 646 | kunmap_local(p); |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 647 | i->head = i_head; |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 648 | i->iov_offset = r + chunk; |
Al Viro | 6852df1 | 2021-05-02 17:24:40 -0400 | [diff] [blame] | 649 | bytes -= chunk; |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 650 | off += chunk; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 651 | r = 0; |
| 652 | i_head++; |
Al Viro | 6852df1 | 2021-05-02 17:24:40 -0400 | [diff] [blame] | 653 | } |
| 654 | *sump = sum; |
| 655 | i->count -= off; |
| 656 | return off; |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 657 | } |
| 658 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 659 | size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 660 | { |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 661 | if (unlikely(iov_iter_is_pipe(i))) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 662 | return copy_pipe_to_iter(addr, bytes, i); |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 663 | if (iter_is_iovec(i)) |
| 664 | might_fault(); |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 665 | iterate_and_advance(i, bytes, base, len, off, |
| 666 | copyout(base, addr + off, len), |
| 667 | memcpy(base, addr + off, len) |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 668 | ) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 669 | |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 670 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 671 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 672 | EXPORT_SYMBOL(_copy_to_iter); |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 673 | |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 674 | #ifdef CONFIG_ARCH_HAS_COPY_MC |
| 675 | static int copyout_mc(void __user *to, const void *from, size_t n) |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 676 | { |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 677 | if (access_ok(to, n)) { |
Marco Elver | d0ef4c3 | 2020-01-21 17:05:11 +0100 | [diff] [blame] | 678 | instrument_copy_to_user(to, from, n); |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 679 | n = copy_mc_to_user((__force void *) to, from, n); |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 680 | } |
| 681 | return n; |
| 682 | } |
| 683 | |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 684 | static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes, |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 685 | struct iov_iter *i) |
| 686 | { |
| 687 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 688 | unsigned int p_mask = pipe->ring_size - 1; |
| 689 | unsigned int i_head; |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 690 | size_t n, off, xfer = 0; |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 691 | |
| 692 | if (!sanity(i)) |
| 693 | return 0; |
| 694 | |
Al Viro | 2a510a7 | 2021-05-02 17:16:34 -0400 | [diff] [blame] | 695 | n = push_pipe(i, bytes, &i_head, &off); |
| 696 | while (n) { |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 697 | size_t chunk = min_t(size_t, n, PAGE_SIZE - off); |
Al Viro | 2a510a7 | 2021-05-02 17:16:34 -0400 | [diff] [blame] | 698 | char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page); |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 699 | unsigned long rem; |
Al Viro | 2a510a7 | 2021-05-02 17:16:34 -0400 | [diff] [blame] | 700 | rem = copy_mc_to_kernel(p + off, addr + xfer, chunk); |
| 701 | chunk -= rem; |
| 702 | kunmap_local(p); |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 703 | i->head = i_head; |
Al Viro | 2a510a7 | 2021-05-02 17:16:34 -0400 | [diff] [blame] | 704 | i->iov_offset = off + chunk; |
| 705 | xfer += chunk; |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 706 | if (rem) |
| 707 | break; |
| 708 | n -= chunk; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 709 | off = 0; |
| 710 | i_head++; |
Al Viro | 2a510a7 | 2021-05-02 17:16:34 -0400 | [diff] [blame] | 711 | } |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 712 | i->count -= xfer; |
| 713 | return xfer; |
| 714 | } |
| 715 | |
Dan Williams | bf3eeb9 | 2018-07-08 13:46:02 -0700 | [diff] [blame] | 716 | /** |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 717 | * _copy_mc_to_iter - copy to iter with source memory error exception handling |
Dan Williams | bf3eeb9 | 2018-07-08 13:46:02 -0700 | [diff] [blame] | 718 | * @addr: source kernel address |
| 719 | * @bytes: total transfer length |
Randy Dunlap | 44e5599 | 2021-09-07 19:58:54 -0700 | [diff] [blame] | 720 | * @i: destination iterator |
Dan Williams | bf3eeb9 | 2018-07-08 13:46:02 -0700 | [diff] [blame] | 721 | * |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 722 | * 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 Williams | bf3eeb9 | 2018-07-08 13:46:02 -0700 | [diff] [blame] | 726 | * |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 727 | * The main differences between this and typical _copy_to_iter(). |
Dan Williams | bf3eeb9 | 2018-07-08 13:46:02 -0700 | [diff] [blame] | 728 | * |
| 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 Dunlap | 44e5599 | 2021-09-07 19:58:54 -0700 | [diff] [blame] | 738 | * |
| 739 | * Return: number of bytes copied (may be %0) |
Dan Williams | bf3eeb9 | 2018-07-08 13:46:02 -0700 | [diff] [blame] | 740 | */ |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 741 | size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i) |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 742 | { |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 743 | if (unlikely(iov_iter_is_pipe(i))) |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 744 | return copy_mc_pipe_to_iter(addr, bytes, i); |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 745 | if (iter_is_iovec(i)) |
| 746 | might_fault(); |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 747 | __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 Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 750 | ) |
| 751 | |
| 752 | return bytes; |
| 753 | } |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 754 | EXPORT_SYMBOL_GPL(_copy_mc_to_iter); |
| 755 | #endif /* CONFIG_ARCH_HAS_COPY_MC */ |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 756 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 757 | size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 758 | { |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 759 | if (unlikely(iov_iter_is_pipe(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 760 | WARN_ON(1); |
| 761 | return 0; |
| 762 | } |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 763 | if (iter_is_iovec(i)) |
| 764 | might_fault(); |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 765 | iterate_and_advance(i, bytes, base, len, off, |
| 766 | copyin(addr + off, base, len), |
| 767 | memcpy(addr + off, base, len) |
Al Viro | 0dbca9a | 2014-11-27 14:26:43 -0500 | [diff] [blame] | 768 | ) |
| 769 | |
| 770 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 771 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 772 | EXPORT_SYMBOL(_copy_from_iter); |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 773 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 774 | size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) |
Al Viro | aa58309 | 2014-11-27 20:27:08 -0500 | [diff] [blame] | 775 | { |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 776 | if (unlikely(iov_iter_is_pipe(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 777 | WARN_ON(1); |
| 778 | return 0; |
| 779 | } |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 780 | 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 Viro | aa58309 | 2014-11-27 20:27:08 -0500 | [diff] [blame] | 783 | ) |
| 784 | |
| 785 | return bytes; |
| 786 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 787 | EXPORT_SYMBOL(_copy_from_iter_nocache); |
Al Viro | aa58309 | 2014-11-27 20:27:08 -0500 | [diff] [blame] | 788 | |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 789 | #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE |
Dan Williams | abd08d7 | 2018-07-08 13:46:07 -0700 | [diff] [blame] | 790 | /** |
| 791 | * _copy_from_iter_flushcache - write destination through cpu cache |
| 792 | * @addr: destination kernel address |
| 793 | * @bytes: total transfer length |
Randy Dunlap | 44e5599 | 2021-09-07 19:58:54 -0700 | [diff] [blame] | 794 | * @i: source iterator |
Dan Williams | abd08d7 | 2018-07-08 13:46:07 -0700 | [diff] [blame] | 795 | * |
| 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 Dunlap | 44e5599 | 2021-09-07 19:58:54 -0700 | [diff] [blame] | 803 | * |
| 804 | * Return: number of bytes copied (may be %0) |
Dan Williams | abd08d7 | 2018-07-08 13:46:07 -0700 | [diff] [blame] | 805 | */ |
Linus Torvalds | 6a37e94 | 2017-07-07 20:39:20 -0700 | [diff] [blame] | 806 | size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i) |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 807 | { |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 808 | if (unlikely(iov_iter_is_pipe(i))) { |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 809 | WARN_ON(1); |
| 810 | return 0; |
| 811 | } |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 812 | 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 Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 815 | ) |
| 816 | |
| 817 | return bytes; |
| 818 | } |
Linus Torvalds | 6a37e94 | 2017-07-07 20:39:20 -0700 | [diff] [blame] | 819 | EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache); |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 820 | #endif |
| 821 | |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame] | 822 | static inline bool page_copy_sane(struct page *page, size_t offset, size_t n) |
| 823 | { |
Eric Dumazet | 6daef95 | 2019-02-26 10:42:39 -0800 | [diff] [blame] | 824 | 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 Penkov | a90bcb8 | 2017-08-29 11:20:32 -0700 | [diff] [blame] | 839 | |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 840 | if (likely(n <= v && v <= (page_size(head)))) |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame] | 841 | return true; |
| 842 | WARN_ON(1); |
| 843 | return false; |
| 844 | } |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 845 | |
Al Viro | 08aa647 | 2021-04-29 20:42:25 -0400 | [diff] [blame] | 846 | static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes, |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 847 | struct iov_iter *i) |
| 848 | { |
Al Viro | 28f38db | 2021-06-02 17:25:59 -0400 | [diff] [blame] | 849 | 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 Viro | c1d4d6a | 2021-04-27 12:29:53 -0400 | [diff] [blame] | 852 | void *kaddr = kmap_local_page(page); |
| 853 | size_t wanted = _copy_to_iter(kaddr + offset, bytes, i); |
| 854 | kunmap_local(kaddr); |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 855 | return wanted; |
Al Viro | 28f38db | 2021-06-02 17:25:59 -0400 | [diff] [blame] | 856 | } |
| 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 Viro | a506abc | 2021-04-27 12:34:04 -0400 | [diff] [blame] | 860 | if (unlikely(i->count < bytes)) |
| 861 | bytes = i->count; |
| 862 | i->count -= bytes; |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 863 | return bytes; |
Al Viro | 28f38db | 2021-06-02 17:25:59 -0400 | [diff] [blame] | 864 | } |
| 865 | WARN_ON(1); |
| 866 | return 0; |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 867 | } |
Al Viro | 08aa647 | 2021-04-29 20:42:25 -0400 | [diff] [blame] | 868 | |
| 869 | size_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 Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 892 | EXPORT_SYMBOL(copy_page_to_iter); |
| 893 | |
| 894 | size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, |
| 895 | struct iov_iter *i) |
| 896 | { |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame] | 897 | if (unlikely(!page_copy_sane(page, offset, bytes))) |
| 898 | return 0; |
Al Viro | 28f38db | 2021-06-02 17:25:59 -0400 | [diff] [blame] | 899 | 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 Viro | 55ca375 | 2021-04-27 12:33:24 -0400 | [diff] [blame] | 902 | void *kaddr = kmap_local_page(page); |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 903 | size_t wanted = _copy_from_iter(kaddr + offset, bytes, i); |
Al Viro | 55ca375 | 2021-04-27 12:33:24 -0400 | [diff] [blame] | 904 | kunmap_local(kaddr); |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 905 | return wanted; |
Al Viro | 28f38db | 2021-06-02 17:25:59 -0400 | [diff] [blame] | 906 | } |
| 907 | WARN_ON(1); |
| 908 | return 0; |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 909 | } |
| 910 | EXPORT_SYMBOL(copy_page_from_iter); |
| 911 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 912 | static size_t pipe_zero(size_t bytes, struct iov_iter *i) |
| 913 | { |
| 914 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 915 | unsigned int p_mask = pipe->ring_size - 1; |
| 916 | unsigned int i_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 917 | size_t n, off; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 918 | |
| 919 | if (!sanity(i)) |
| 920 | return 0; |
| 921 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 922 | bytes = n = push_pipe(i, bytes, &i_head, &off); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 923 | if (unlikely(!n)) |
| 924 | return 0; |
| 925 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 926 | do { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 927 | size_t chunk = min_t(size_t, n, PAGE_SIZE - off); |
Al Viro | 893839f | 2021-04-30 18:39:25 -0400 | [diff] [blame] | 928 | char *p = kmap_local_page(pipe->bufs[i_head & p_mask].page); |
| 929 | memset(p + off, 0, chunk); |
| 930 | kunmap_local(p); |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 931 | i->head = i_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 932 | i->iov_offset = off + chunk; |
| 933 | n -= chunk; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 934 | off = 0; |
| 935 | i_head++; |
| 936 | } while (n); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 937 | i->count -= bytes; |
| 938 | return bytes; |
| 939 | } |
| 940 | |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 941 | size_t iov_iter_zero(size_t bytes, struct iov_iter *i) |
| 942 | { |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 943 | if (unlikely(iov_iter_is_pipe(i))) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 944 | return pipe_zero(bytes, i); |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 945 | iterate_and_advance(i, bytes, base, len, count, |
| 946 | clear_user(base, len), |
| 947 | memset(base, 0, len) |
Al Viro | 8442fa4 | 2014-11-27 14:18:54 -0500 | [diff] [blame] | 948 | ) |
| 949 | |
| 950 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 951 | } |
| 952 | EXPORT_SYMBOL(iov_iter_zero); |
| 953 | |
Al Viro | f0b65f3 | 2021-04-30 10:26:41 -0400 | [diff] [blame] | 954 | size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes, |
| 955 | struct iov_iter *i) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 956 | { |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 957 | char *kaddr = kmap_atomic(page), *p = kaddr + offset; |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame] | 958 | if (unlikely(!page_copy_sane(page, offset, bytes))) { |
| 959 | kunmap_atomic(kaddr); |
| 960 | return 0; |
| 961 | } |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 962 | if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 963 | kunmap_atomic(kaddr); |
| 964 | WARN_ON(1); |
| 965 | return 0; |
| 966 | } |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 967 | iterate_and_advance(i, bytes, base, len, off, |
| 968 | copyin(p + off, base, len), |
| 969 | memcpy(p + off, base, len) |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 970 | ) |
| 971 | kunmap_atomic(kaddr); |
| 972 | return bytes; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 973 | } |
Al Viro | f0b65f3 | 2021-04-30 10:26:41 -0400 | [diff] [blame] | 974 | EXPORT_SYMBOL(copy_page_from_iter_atomic); |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 975 | |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 976 | static inline void pipe_truncate(struct iov_iter *i) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 977 | { |
| 978 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 979 | 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 Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 986 | size_t off = i->iov_offset; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 987 | |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 988 | if (off) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 989 | buf = &pipe->bufs[i_head & p_mask]; |
| 990 | buf->len = off - buf->offset; |
| 991 | i_head++; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 992 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 993 | while (p_head != i_head) { |
| 994 | p_head--; |
| 995 | pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 996 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 997 | |
| 998 | pipe->head = p_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 999 | } |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | static void pipe_advance(struct iov_iter *i, size_t size) |
| 1003 | { |
| 1004 | struct pipe_inode_info *pipe = i->pipe; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1005 | if (size) { |
| 1006 | struct pipe_buffer *buf; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1007 | unsigned int p_mask = pipe->ring_size - 1; |
| 1008 | unsigned int i_head = i->head; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1009 | size_t off = i->iov_offset, left = size; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1010 | |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1011 | if (off) /* make it relative to the beginning of buffer */ |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1012 | left += off - pipe->bufs[i_head & p_mask].offset; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1013 | while (1) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1014 | buf = &pipe->bufs[i_head & p_mask]; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1015 | if (left <= buf->len) |
| 1016 | break; |
| 1017 | left -= buf->len; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1018 | i_head++; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1019 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1020 | i->head = i_head; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1021 | i->iov_offset = buf->offset + left; |
| 1022 | } |
| 1023 | i->count -= size; |
| 1024 | /* ... and discard everything past that point */ |
| 1025 | pipe_truncate(i); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1026 | } |
| 1027 | |
Pavel Begunkov | 54c8195 | 2021-01-09 16:03:01 +0000 | [diff] [blame] | 1028 | static 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 Viro | 185ac4d | 2021-04-23 12:58:53 -0400 | [diff] [blame] | 1043 | static 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 Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1062 | void iov_iter_advance(struct iov_iter *i, size_t size) |
| 1063 | { |
Al Viro | 3b3fc05 | 2021-04-23 22:24:08 -0400 | [diff] [blame] | 1064 | if (unlikely(i->count < size)) |
| 1065 | size = i->count; |
Al Viro | 185ac4d | 2021-04-23 12:58:53 -0400 | [diff] [blame] | 1066 | 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 Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1072 | pipe_advance(i, size); |
Al Viro | 185ac4d | 2021-04-23 12:58:53 -0400 | [diff] [blame] | 1073 | } else if (unlikely(iov_iter_is_xarray(i))) { |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1074 | i->iov_offset += size; |
| 1075 | i->count -= size; |
Al Viro | 185ac4d | 2021-04-23 12:58:53 -0400 | [diff] [blame] | 1076 | } else if (iov_iter_is_discard(i)) { |
| 1077 | i->count -= size; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1078 | } |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1079 | } |
| 1080 | EXPORT_SYMBOL(iov_iter_advance); |
| 1081 | |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1082 | void iov_iter_revert(struct iov_iter *i, size_t unroll) |
| 1083 | { |
| 1084 | if (!unroll) |
| 1085 | return; |
Al Viro | 5b47d59 | 2017-05-08 13:54:47 -0400 | [diff] [blame] | 1086 | if (WARN_ON(unroll > MAX_RW_COUNT)) |
| 1087 | return; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1088 | i->count += unroll; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1089 | if (unlikely(iov_iter_is_pipe(i))) { |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1090 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1091 | unsigned int p_mask = pipe->ring_size - 1; |
| 1092 | unsigned int i_head = i->head; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1093 | size_t off = i->iov_offset; |
| 1094 | while (1) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1095 | struct pipe_buffer *b = &pipe->bufs[i_head & p_mask]; |
| 1096 | size_t n = off - b->offset; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1097 | if (unroll < n) { |
Al Viro | 4fa55ce | 2017-04-29 16:42:30 -0400 | [diff] [blame] | 1098 | off -= unroll; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1099 | break; |
| 1100 | } |
| 1101 | unroll -= n; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1102 | if (!unroll && i_head == i->start_head) { |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1103 | off = 0; |
| 1104 | break; |
| 1105 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1106 | i_head--; |
| 1107 | b = &pipe->bufs[i_head & p_mask]; |
| 1108 | off = b->offset + b->len; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1109 | } |
| 1110 | i->iov_offset = off; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1111 | i->head = i_head; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1112 | pipe_truncate(i); |
| 1113 | return; |
| 1114 | } |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1115 | if (unlikely(iov_iter_is_discard(i))) |
| 1116 | return; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1117 | if (unroll <= i->iov_offset) { |
| 1118 | i->iov_offset -= unroll; |
| 1119 | return; |
| 1120 | } |
| 1121 | unroll -= i->iov_offset; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1122 | 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 Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1128 | 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 | } |
| 1153 | EXPORT_SYMBOL(iov_iter_revert); |
| 1154 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1155 | /* |
| 1156 | * Return the count of just the current iov_iter segment. |
| 1157 | */ |
| 1158 | size_t iov_iter_single_seg_count(const struct iov_iter *i) |
| 1159 | { |
Al Viro | 28f38db | 2021-06-02 17:25:59 -0400 | [diff] [blame] | 1160 | 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 Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1167 | } |
| 1168 | EXPORT_SYMBOL(iov_iter_single_seg_count); |
| 1169 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1170 | void iov_iter_kvec(struct iov_iter *i, unsigned int direction, |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 1171 | const struct kvec *kvec, unsigned long nr_segs, |
Al Viro | abb78f8 | 2014-11-24 14:46:11 -0500 | [diff] [blame] | 1172 | size_t count) |
| 1173 | { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1174 | WARN_ON(direction & ~(READ | WRITE)); |
Al Viro | 8cd54c1 | 2021-04-22 14:50:39 -0400 | [diff] [blame] | 1175 | *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 Viro | abb78f8 | 2014-11-24 14:46:11 -0500 | [diff] [blame] | 1183 | } |
| 1184 | EXPORT_SYMBOL(iov_iter_kvec); |
| 1185 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1186 | void iov_iter_bvec(struct iov_iter *i, unsigned int direction, |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 1187 | const struct bio_vec *bvec, unsigned long nr_segs, |
| 1188 | size_t count) |
| 1189 | { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1190 | WARN_ON(direction & ~(READ | WRITE)); |
Al Viro | 8cd54c1 | 2021-04-22 14:50:39 -0400 | [diff] [blame] | 1191 | *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 Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 1199 | } |
| 1200 | EXPORT_SYMBOL(iov_iter_bvec); |
| 1201 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1202 | void iov_iter_pipe(struct iov_iter *i, unsigned int direction, |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1203 | struct pipe_inode_info *pipe, |
| 1204 | size_t count) |
| 1205 | { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1206 | BUG_ON(direction != READ); |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1207 | WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size)); |
Al Viro | 8cd54c1 | 2021-04-22 14:50:39 -0400 | [diff] [blame] | 1208 | *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 Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1217 | } |
| 1218 | EXPORT_SYMBOL(iov_iter_pipe); |
| 1219 | |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1220 | /** |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1221 | * 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 | */ |
| 1233 | void 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 Viro | 8cd54c1 | 2021-04-22 14:50:39 -0400 | [diff] [blame] | 1237 | *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 Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1245 | } |
| 1246 | EXPORT_SYMBOL(iov_iter_xarray); |
| 1247 | |
| 1248 | /** |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1249 | * 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 | */ |
| 1257 | void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count) |
| 1258 | { |
| 1259 | BUG_ON(direction != READ); |
Al Viro | 8cd54c1 | 2021-04-22 14:50:39 -0400 | [diff] [blame] | 1260 | *i = (struct iov_iter){ |
| 1261 | .iter_type = ITER_DISCARD, |
| 1262 | .data_source = false, |
| 1263 | .count = count, |
| 1264 | .iov_offset = 0 |
| 1265 | }; |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1266 | } |
| 1267 | EXPORT_SYMBOL(iov_iter_discard); |
| 1268 | |
Al Viro | 9221d2e | 2021-04-25 00:44:35 -0400 | [diff] [blame] | 1269 | static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1270 | { |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 1271 | unsigned long res = 0; |
| 1272 | size_t size = i->count; |
Al Viro | 9221d2e | 2021-04-25 00:44:35 -0400 | [diff] [blame] | 1273 | size_t skip = i->iov_offset; |
| 1274 | unsigned k; |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 1275 | |
Al Viro | 9221d2e | 2021-04-25 00:44:35 -0400 | [diff] [blame] | 1276 | 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 | |
| 1291 | static 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 | |
| 1311 | unsigned 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 Kara | e0ff126 | 2019-12-16 11:54:32 +0100 | [diff] [blame] | 1321 | unsigned int p_mask = i->pipe->ring_size - 1; |
Al Viro | 9221d2e | 2021-04-25 00:44:35 -0400 | [diff] [blame] | 1322 | size_t size = i->count; |
Jan Kara | e0ff126 | 2019-12-16 11:54:32 +0100 | [diff] [blame] | 1323 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1324 | if (size && i->iov_offset && allocated(&i->pipe->bufs[i->head & p_mask])) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1325 | return size | i->iov_offset; |
| 1326 | return size; |
| 1327 | } |
Al Viro | 9221d2e | 2021-04-25 00:44:35 -0400 | [diff] [blame] | 1328 | |
| 1329 | if (iov_iter_is_xarray(i)) |
David Howells | 3d14ec1 | 2021-04-25 22:02:38 +0100 | [diff] [blame] | 1330 | return (i->xarray_start + i->iov_offset) | i->count; |
Al Viro | 9221d2e | 2021-04-25 00:44:35 -0400 | [diff] [blame] | 1331 | |
| 1332 | return 0; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1333 | } |
| 1334 | EXPORT_SYMBOL(iov_iter_alignment); |
| 1335 | |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 1336 | unsigned long iov_iter_gap_alignment(const struct iov_iter *i) |
| 1337 | { |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 1338 | unsigned long res = 0; |
Al Viro | 610c7a7 | 2021-04-25 01:03:16 -0400 | [diff] [blame] | 1339 | unsigned long v = 0; |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 1340 | size_t size = i->count; |
Al Viro | 610c7a7 | 2021-04-25 01:03:16 -0400 | [diff] [blame] | 1341 | unsigned k; |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 1342 | |
Al Viro | 610c7a7 | 2021-04-25 01:03:16 -0400 | [diff] [blame] | 1343 | if (WARN_ON(!iter_is_iovec(i))) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1344 | return ~0U; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1345 | |
Al Viro | 610c7a7 | 2021-04-25 01:03:16 -0400 | [diff] [blame] | 1346 | 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 Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 1357 | return res; |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 1358 | } |
| 1359 | EXPORT_SYMBOL(iov_iter_gap_alignment); |
| 1360 | |
Ilya Dryomov | e76b6312 | 2018-05-02 20:16:56 +0200 | [diff] [blame] | 1361 | static inline ssize_t __pipe_get_pages(struct iov_iter *i, |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1362 | size_t maxsize, |
| 1363 | struct page **pages, |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1364 | int iter_head, |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1365 | size_t *start) |
| 1366 | { |
| 1367 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1368 | unsigned int p_mask = pipe->ring_size - 1; |
| 1369 | ssize_t n = push_pipe(i, maxsize, &iter_head, start); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1370 | if (!n) |
| 1371 | return -EFAULT; |
| 1372 | |
| 1373 | maxsize = n; |
| 1374 | n += *start; |
Al Viro | 1689c73 | 2016-10-11 18:21:14 +0100 | [diff] [blame] | 1375 | while (n > 0) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1376 | get_page(*pages++ = pipe->bufs[iter_head & p_mask].page); |
| 1377 | iter_head++; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1378 | n -= PAGE_SIZE; |
| 1379 | } |
| 1380 | |
| 1381 | return maxsize; |
| 1382 | } |
| 1383 | |
| 1384 | static ssize_t pipe_get_pages(struct iov_iter *i, |
| 1385 | struct page **pages, size_t maxsize, unsigned maxpages, |
| 1386 | size_t *start) |
| 1387 | { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1388 | unsigned int iter_head, npages; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1389 | size_t capacity; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1390 | |
| 1391 | if (!sanity(i)) |
| 1392 | return -EFAULT; |
| 1393 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1394 | 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 Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1398 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1399 | return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1400 | } |
| 1401 | |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1402 | static 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 | |
| 1429 | static 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 Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1471 | /* must be done on non-empty ITER_IOVEC one */ |
| 1472 | static 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 */ |
| 1497 | static 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 Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1516 | ssize_t iov_iter_get_pages(struct iov_iter *i, |
Miklos Szeredi | 2c80929 | 2014-09-24 17:09:11 +0200 | [diff] [blame] | 1517 | struct page **pages, size_t maxsize, unsigned maxpages, |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1518 | size_t *start) |
| 1519 | { |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1520 | size_t len; |
| 1521 | int n, res; |
| 1522 | |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 1523 | if (maxsize > i->count) |
| 1524 | maxsize = i->count; |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1525 | if (!maxsize) |
| 1526 | return 0; |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 1527 | |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1528 | if (likely(iter_is_iovec(i))) { |
Andreas Gruenbacher | 3337ab0 | 2021-07-12 12:06:14 +0200 | [diff] [blame] | 1529 | unsigned int gup_flags = 0; |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1530 | unsigned long addr; |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1531 | |
Andreas Gruenbacher | 3337ab0 | 2021-07-12 12:06:14 +0200 | [diff] [blame] | 1532 | if (iov_iter_rw(i) != WRITE) |
| 1533 | gup_flags |= FOLL_WRITE; |
| 1534 | if (i->nofault) |
| 1535 | gup_flags |= FOLL_NOFAULT; |
| 1536 | |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1537 | addr = first_iovec_segment(i, &len, start, maxsize, maxpages); |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 1538 | n = DIV_ROUND_UP(len, PAGE_SIZE); |
Andreas Gruenbacher | 3337ab0 | 2021-07-12 12:06:14 +0200 | [diff] [blame] | 1539 | res = get_user_pages_fast(addr, n, gup_flags, pages); |
Andreas Gruenbacher | 814a667 | 2021-07-21 19:03:47 +0200 | [diff] [blame] | 1540 | if (unlikely(res <= 0)) |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 1541 | return res; |
| 1542 | return (res == n ? len : res * PAGE_SIZE) - *start; |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1543 | } |
| 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 Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1558 | } |
| 1559 | EXPORT_SYMBOL(iov_iter_get_pages); |
| 1560 | |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1561 | static struct page **get_pages_array(size_t n) |
| 1562 | { |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 1563 | return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL); |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1564 | } |
| 1565 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1566 | static 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 Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1571 | unsigned int iter_head, npages; |
Ilya Dryomov | d7760d6 | 2018-05-02 20:16:57 +0200 | [diff] [blame] | 1572 | ssize_t n; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1573 | |
| 1574 | if (!sanity(i)) |
| 1575 | return -EFAULT; |
| 1576 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1577 | 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 Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1580 | 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 Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1588 | n = __pipe_get_pages(i, maxsize, p, iter_head, start); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1589 | if (n > 0) |
| 1590 | *pages = p; |
| 1591 | else |
| 1592 | kvfree(p); |
| 1593 | return n; |
| 1594 | } |
| 1595 | |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1596 | static 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 Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1641 | ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, |
| 1642 | struct page ***pages, size_t maxsize, |
| 1643 | size_t *start) |
| 1644 | { |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1645 | struct page **p; |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1646 | size_t len; |
| 1647 | int n, res; |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1648 | |
| 1649 | if (maxsize > i->count) |
| 1650 | maxsize = i->count; |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1651 | if (!maxsize) |
| 1652 | return 0; |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1653 | |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1654 | if (likely(iter_is_iovec(i))) { |
Andreas Gruenbacher | 3337ab0 | 2021-07-12 12:06:14 +0200 | [diff] [blame] | 1655 | unsigned int gup_flags = 0; |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1656 | unsigned long addr; |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1657 | |
Andreas Gruenbacher | 3337ab0 | 2021-07-12 12:06:14 +0200 | [diff] [blame] | 1658 | if (iov_iter_rw(i) != WRITE) |
| 1659 | gup_flags |= FOLL_WRITE; |
| 1660 | if (i->nofault) |
| 1661 | gup_flags |= FOLL_NOFAULT; |
| 1662 | |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1663 | addr = first_iovec_segment(i, &len, start, maxsize, ~0U); |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1664 | n = DIV_ROUND_UP(len, PAGE_SIZE); |
| 1665 | p = get_pages_array(n); |
| 1666 | if (!p) |
| 1667 | return -ENOMEM; |
Andreas Gruenbacher | 3337ab0 | 2021-07-12 12:06:14 +0200 | [diff] [blame] | 1668 | res = get_user_pages_fast(addr, n, gup_flags, p); |
Andreas Gruenbacher | 814a667 | 2021-07-21 19:03:47 +0200 | [diff] [blame] | 1669 | if (unlikely(res <= 0)) { |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1670 | kvfree(p); |
Andreas Gruenbacher | 814a667 | 2021-07-21 19:03:47 +0200 | [diff] [blame] | 1671 | *pages = NULL; |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1672 | return res; |
| 1673 | } |
| 1674 | *pages = p; |
| 1675 | return (res == n ? len : res * PAGE_SIZE) - *start; |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1676 | } |
| 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 Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1683 | if (!p) |
| 1684 | return -ENOMEM; |
Al Viro | 3d671ca | 2021-04-25 09:14:44 -0400 | [diff] [blame] | 1685 | 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 Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1694 | } |
| 1695 | EXPORT_SYMBOL(iov_iter_get_pages_alloc); |
| 1696 | |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1697 | size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, |
| 1698 | struct iov_iter *i) |
| 1699 | { |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1700 | __wsum sum, next; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1701 | sum = *csum; |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1702 | if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1703 | WARN_ON(1); |
| 1704 | return 0; |
| 1705 | } |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 1706 | iterate_and_advance(i, bytes, base, len, off, ({ |
| 1707 | next = csum_and_copy_from_user(base, addr + off, len); |
Al Viro | 2495bdcc | 2021-04-30 13:40:48 -0400 | [diff] [blame] | 1708 | sum = csum_block_add(sum, next, off); |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 1709 | next ? 0 : len; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1710 | }), ({ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 1711 | sum = csum_and_memcpy(addr + off, base, len, sum, off); |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1712 | }) |
| 1713 | ) |
| 1714 | *csum = sum; |
| 1715 | return bytes; |
| 1716 | } |
| 1717 | EXPORT_SYMBOL(csum_and_copy_from_iter); |
| 1718 | |
Willem de Bruijn | 52cbd23 | 2021-02-03 14:29:52 -0500 | [diff] [blame] | 1719 | size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate, |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1720 | struct iov_iter *i) |
| 1721 | { |
Willem de Bruijn | 52cbd23 | 2021-02-03 14:29:52 -0500 | [diff] [blame] | 1722 | struct csum_state *csstate = _csstate; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1723 | __wsum sum, next; |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 1724 | |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 1725 | if (unlikely(iov_iter_is_discard(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1726 | WARN_ON(1); /* for now */ |
| 1727 | return 0; |
| 1728 | } |
Al Viro | 6852df1 | 2021-05-02 17:24:40 -0400 | [diff] [blame] | 1729 | |
| 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 Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 1734 | next = csum_and_copy_to_user(addr + off, base, len); |
Al Viro | 2495bdcc | 2021-04-30 13:40:48 -0400 | [diff] [blame] | 1735 | sum = csum_block_add(sum, next, off); |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 1736 | next ? 0 : len; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1737 | }), ({ |
Al Viro | 7baa509 | 2021-05-02 11:35:03 -0400 | [diff] [blame] | 1738 | sum = csum_and_memcpy(base, addr + off, len, sum, off); |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1739 | }) |
| 1740 | ) |
Al Viro | 594e450 | 2021-06-05 10:19:30 -0400 | [diff] [blame] | 1741 | csstate->csum = csum_shift(sum, csstate->off); |
| 1742 | csstate->off += bytes; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1743 | return bytes; |
| 1744 | } |
| 1745 | EXPORT_SYMBOL(csum_and_copy_to_iter); |
| 1746 | |
Sagi Grimberg | d05f443 | 2018-12-03 17:52:09 -0800 | [diff] [blame] | 1747 | size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, |
| 1748 | struct iov_iter *i) |
| 1749 | { |
Herbert Xu | 7999096 | 2020-06-12 16:57:37 +1000 | [diff] [blame] | 1750 | #ifdef CONFIG_CRYPTO_HASH |
Sagi Grimberg | d05f443 | 2018-12-03 17:52:09 -0800 | [diff] [blame] | 1751 | 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; |
YueHaibing | 27fad74 | 2019-04-04 10:31:14 +0800 | [diff] [blame] | 1760 | #else |
| 1761 | return 0; |
| 1762 | #endif |
Sagi Grimberg | d05f443 | 2018-12-03 17:52:09 -0800 | [diff] [blame] | 1763 | } |
| 1764 | EXPORT_SYMBOL(hash_and_copy_to_iter); |
| 1765 | |
Al Viro | 66531c6 | 2021-04-25 16:00:48 -0400 | [diff] [blame] | 1766 | static int iov_npages(const struct iov_iter *i, int maxpages) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1767 | { |
Al Viro | 66531c6 | 2021-04-25 16:00:48 -0400 | [diff] [blame] | 1768 | size_t skip = i->iov_offset, size = i->count; |
| 1769 | const struct iovec *p; |
Al Viro | e0f2dc4 | 2014-11-27 14:09:46 -0500 | [diff] [blame] | 1770 | int npages = 0; |
| 1771 | |
Al Viro | 66531c6 | 2021-04-25 16:00:48 -0400 | [diff] [blame] | 1772 | 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 Viro | e0f2dc4 | 2014-11-27 14:09:46 -0500 | [diff] [blame] | 1775 | |
Al Viro | 66531c6 | 2021-04-25 16:00:48 -0400 | [diff] [blame] | 1776 | 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 | |
| 1786 | static 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 | |
| 1804 | int 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 Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1814 | unsigned int iter_head; |
Al Viro | 66531c6 | 2021-04-25 16:00:48 -0400 | [diff] [blame] | 1815 | int npages; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1816 | size_t off; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1817 | |
| 1818 | if (!sanity(i)) |
| 1819 | return 0; |
| 1820 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1821 | data_start(i, &iter_head, &off); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1822 | /* some of this one + all after this one */ |
Al Viro | 66531c6 | 2021-04-25 16:00:48 -0400 | [diff] [blame] | 1823 | 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 Viro | e4f8df8 | 2021-05-03 11:05:29 -0400 | [diff] [blame] | 1827 | unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE; |
| 1828 | int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE); |
Al Viro | 66531c6 | 2021-04-25 16:00:48 -0400 | [diff] [blame] | 1829 | return min(npages, maxpages); |
| 1830 | } |
| 1831 | return 0; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1832 | } |
Al Viro | f67da30 | 2014-03-19 01:16:16 -0400 | [diff] [blame] | 1833 | EXPORT_SYMBOL(iov_iter_npages); |
Al Viro | 4b8164b | 2015-01-31 20:08:47 -0500 | [diff] [blame] | 1834 | |
| 1835 | const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags) |
| 1836 | { |
| 1837 | *new = *old; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1838 | if (unlikely(iov_iter_is_pipe(new))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1839 | WARN_ON(1); |
| 1840 | return NULL; |
| 1841 | } |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1842 | if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new))) |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1843 | return NULL; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1844 | if (iov_iter_is_bvec(new)) |
Al Viro | 4b8164b | 2015-01-31 20:08:47 -0500 | [diff] [blame] | 1845 | 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 | } |
| 1854 | EXPORT_SYMBOL(dup_iter); |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 1855 | |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1856 | static 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 Hellwig | a959a97 | 2021-01-11 18:19:26 +0100 | [diff] [blame] | 1863 | if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1864 | 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; |
| 1883 | uaccess_end: |
| 1884 | user_access_end(); |
| 1885 | return ret; |
| 1886 | } |
| 1887 | |
| 1888 | static int copy_iovec_from_user(struct iovec *iov, |
| 1889 | const struct iovec __user *uvec, unsigned long nr_segs) |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1890 | { |
| 1891 | unsigned long seg; |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1892 | |
| 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 | |
| 1903 | struct 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 Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1909 | |
| 1910 | /* |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1911 | * 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 Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1913 | * traditionally returned zero for zero segments, so... |
| 1914 | */ |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1915 | if (nr_segs == 0) |
| 1916 | return iov; |
| 1917 | if (nr_segs > UIO_MAXIOV) |
| 1918 | return ERR_PTR(-EINVAL); |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1919 | if (nr_segs > fast_segs) { |
| 1920 | iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1921 | if (!iov) |
| 1922 | return ERR_PTR(-ENOMEM); |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1923 | } |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1924 | |
| 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 | |
| 1938 | ssize_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 Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | /* |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1953 | * 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 Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1956 | * |
| 1957 | * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the |
| 1958 | * overflow case. |
| 1959 | */ |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1960 | for (seg = 0; seg < nr_segs; seg++) { |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1961 | ssize_t len = (ssize_t)iov[seg].iov_len; |
| 1962 | |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1963 | if (!access_ok(iov[seg].iov_base, len)) { |
| 1964 | if (iov != *iovp) |
| 1965 | kfree(iov); |
| 1966 | *iovp = NULL; |
| 1967 | return -EFAULT; |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1968 | } |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1969 | |
| 1970 | if (len > MAX_RW_COUNT - total_len) { |
| 1971 | len = MAX_RW_COUNT - total_len; |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1972 | iov[seg].iov_len = len; |
| 1973 | } |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1974 | total_len += len; |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1975 | } |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1976 | |
| 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 Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1983 | } |
| 1984 | |
Vegard Nossum | ffecee4 | 2016-10-08 11:18:07 +0200 | [diff] [blame] | 1985 | /** |
| 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 Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1991 | * @uvec: Pointer to the userspace array. |
Vegard Nossum | ffecee4 | 2016-10-08 11:18:07 +0200 | [diff] [blame] | 1992 | * @nr_segs: Number of elements in userspace array. |
| 1993 | * @fast_segs: Number of elements in @iov. |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1994 | * @iovp: (input and output parameter) Pointer to pointer to (usually small |
Vegard Nossum | ffecee4 | 2016-10-08 11:18:07 +0200 | [diff] [blame] | 1995 | * 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 Axboe | 87e5e6d | 2019-05-14 16:02:22 -0600 | [diff] [blame] | 2005 | * Return: Negative error code on error, bytes imported on success |
Vegard Nossum | ffecee4 | 2016-10-08 11:18:07 +0200 | [diff] [blame] | 2006 | */ |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2007 | ssize_t import_iovec(int type, const struct iovec __user *uvec, |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 2008 | unsigned nr_segs, unsigned fast_segs, |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2009 | struct iovec **iovp, struct iov_iter *i) |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 2010 | { |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 2011 | return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i, |
| 2012 | in_compat_syscall()); |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 2013 | } |
| 2014 | EXPORT_SYMBOL(import_iovec); |
| 2015 | |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 2016 | int 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 Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2021 | if (unlikely(!access_ok(buf, len))) |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 2022 | 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 Viro | e126758 | 2015-12-06 20:38:56 -0500 | [diff] [blame] | 2029 | EXPORT_SYMBOL(import_single_range); |
Jens Axboe | 8fb0f47 | 2021-09-10 11:18:36 -0600 | [diff] [blame] | 2030 | |
| 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 | */ |
| 2043 | void 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 | } |