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 | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 19 | #define iterate_iovec(i, n, __v, __p, skip, STEP) { \ |
| 20 | size_t left; \ |
| 21 | size_t wanted = n; \ |
| 22 | __p = i->iov; \ |
| 23 | __v.iov_len = min(n, __p->iov_len - skip); \ |
| 24 | if (likely(__v.iov_len)) { \ |
| 25 | __v.iov_base = __p->iov_base + skip; \ |
| 26 | left = (STEP); \ |
| 27 | __v.iov_len -= left; \ |
| 28 | skip += __v.iov_len; \ |
| 29 | n -= __v.iov_len; \ |
| 30 | } else { \ |
| 31 | left = 0; \ |
| 32 | } \ |
| 33 | while (unlikely(!left && n)) { \ |
| 34 | __p++; \ |
| 35 | __v.iov_len = min(n, __p->iov_len); \ |
| 36 | if (unlikely(!__v.iov_len)) \ |
| 37 | continue; \ |
| 38 | __v.iov_base = __p->iov_base; \ |
| 39 | left = (STEP); \ |
| 40 | __v.iov_len -= left; \ |
| 41 | skip = __v.iov_len; \ |
| 42 | n -= __v.iov_len; \ |
| 43 | } \ |
| 44 | n = wanted - n; \ |
| 45 | } |
| 46 | |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 47 | #define iterate_kvec(i, n, __v, __p, skip, STEP) { \ |
| 48 | size_t wanted = n; \ |
| 49 | __p = i->kvec; \ |
| 50 | __v.iov_len = min(n, __p->iov_len - skip); \ |
| 51 | if (likely(__v.iov_len)) { \ |
| 52 | __v.iov_base = __p->iov_base + skip; \ |
| 53 | (void)(STEP); \ |
| 54 | skip += __v.iov_len; \ |
| 55 | n -= __v.iov_len; \ |
| 56 | } \ |
| 57 | while (unlikely(n)) { \ |
| 58 | __p++; \ |
| 59 | __v.iov_len = min(n, __p->iov_len); \ |
| 60 | if (unlikely(!__v.iov_len)) \ |
| 61 | continue; \ |
| 62 | __v.iov_base = __p->iov_base; \ |
| 63 | (void)(STEP); \ |
| 64 | skip = __v.iov_len; \ |
| 65 | n -= __v.iov_len; \ |
| 66 | } \ |
| 67 | n = wanted; \ |
| 68 | } |
| 69 | |
Ming Lei | 1bdc76a | 2016-05-30 21:34:32 +0800 | [diff] [blame] | 70 | #define iterate_bvec(i, n, __v, __bi, skip, STEP) { \ |
| 71 | struct bvec_iter __start; \ |
| 72 | __start.bi_size = n; \ |
| 73 | __start.bi_bvec_done = skip; \ |
| 74 | __start.bi_idx = 0; \ |
| 75 | for_each_bvec(__v, i->bvec, __bi, __start) { \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 76 | (void)(STEP); \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 77 | } \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 78 | } |
| 79 | |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 80 | #define iterate_xarray(i, n, __v, skip, STEP) { \ |
| 81 | struct page *head = NULL; \ |
| 82 | size_t wanted = n, seg, offset; \ |
| 83 | loff_t start = i->xarray_start + skip; \ |
| 84 | pgoff_t index = start >> PAGE_SHIFT; \ |
| 85 | int j; \ |
| 86 | \ |
| 87 | XA_STATE(xas, i->xarray, index); \ |
| 88 | \ |
| 89 | rcu_read_lock(); \ |
| 90 | xas_for_each(&xas, head, ULONG_MAX) { \ |
| 91 | if (xas_retry(&xas, head)) \ |
| 92 | continue; \ |
| 93 | if (WARN_ON(xa_is_value(head))) \ |
| 94 | break; \ |
| 95 | if (WARN_ON(PageHuge(head))) \ |
| 96 | break; \ |
| 97 | for (j = (head->index < index) ? index - head->index : 0; \ |
| 98 | j < thp_nr_pages(head); j++) { \ |
| 99 | __v.bv_page = head + j; \ |
| 100 | offset = (i->xarray_start + skip) & ~PAGE_MASK; \ |
| 101 | seg = PAGE_SIZE - offset; \ |
| 102 | __v.bv_offset = offset; \ |
| 103 | __v.bv_len = min(n, seg); \ |
| 104 | (void)(STEP); \ |
| 105 | n -= __v.bv_len; \ |
| 106 | skip += __v.bv_len; \ |
| 107 | if (n == 0) \ |
| 108 | break; \ |
| 109 | } \ |
| 110 | if (n == 0) \ |
| 111 | break; \ |
| 112 | } \ |
| 113 | rcu_read_unlock(); \ |
| 114 | n = wanted - n; \ |
| 115 | } |
| 116 | |
| 117 | #define iterate_all_kinds(i, n, v, I, B, K, X) { \ |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 118 | if (likely(n)) { \ |
| 119 | size_t skip = i->iov_offset; \ |
| 120 | if (unlikely(i->type & ITER_BVEC)) { \ |
| 121 | struct bio_vec v; \ |
| 122 | struct bvec_iter __bi; \ |
| 123 | iterate_bvec(i, n, v, __bi, skip, (B)) \ |
| 124 | } else if (unlikely(i->type & ITER_KVEC)) { \ |
| 125 | const struct kvec *kvec; \ |
| 126 | struct kvec v; \ |
| 127 | iterate_kvec(i, n, v, kvec, skip, (K)) \ |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 128 | } else if (unlikely(i->type & ITER_DISCARD)) { \ |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 129 | } else if (unlikely(i->type & ITER_XARRAY)) { \ |
| 130 | struct bio_vec v; \ |
| 131 | iterate_xarray(i, n, v, skip, (X)); \ |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 132 | } else { \ |
| 133 | const struct iovec *iov; \ |
| 134 | struct iovec v; \ |
| 135 | iterate_iovec(i, n, v, iov, skip, (I)) \ |
| 136 | } \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 137 | } \ |
| 138 | } |
| 139 | |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 140 | #define iterate_and_advance(i, n, v, I, B, K, X) { \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 141 | if (unlikely(i->count < n)) \ |
| 142 | n = i->count; \ |
Al Viro | 19f1845 | 2016-05-25 17:36:19 -0400 | [diff] [blame] | 143 | if (i->count) { \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 144 | size_t skip = i->iov_offset; \ |
| 145 | if (unlikely(i->type & ITER_BVEC)) { \ |
Ming Lei | 1bdc76a | 2016-05-30 21:34:32 +0800 | [diff] [blame] | 146 | const struct bio_vec *bvec = i->bvec; \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 147 | struct bio_vec v; \ |
Ming Lei | 1bdc76a | 2016-05-30 21:34:32 +0800 | [diff] [blame] | 148 | struct bvec_iter __bi; \ |
| 149 | iterate_bvec(i, n, v, __bi, skip, (B)) \ |
| 150 | i->bvec = __bvec_iter_bvec(i->bvec, __bi); \ |
| 151 | i->nr_segs -= i->bvec - bvec; \ |
| 152 | skip = __bi.bi_bvec_done; \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 153 | } else if (unlikely(i->type & ITER_KVEC)) { \ |
| 154 | const struct kvec *kvec; \ |
| 155 | struct kvec v; \ |
| 156 | iterate_kvec(i, n, v, kvec, skip, (K)) \ |
| 157 | if (skip == kvec->iov_len) { \ |
| 158 | kvec++; \ |
| 159 | skip = 0; \ |
| 160 | } \ |
| 161 | i->nr_segs -= kvec - i->kvec; \ |
| 162 | i->kvec = kvec; \ |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 163 | } else if (unlikely(i->type & ITER_DISCARD)) { \ |
| 164 | skip += n; \ |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 165 | } else if (unlikely(i->type & ITER_XARRAY)) { \ |
| 166 | struct bio_vec v; \ |
| 167 | iterate_xarray(i, n, v, skip, (X)) \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 168 | } else { \ |
| 169 | const struct iovec *iov; \ |
| 170 | struct iovec v; \ |
| 171 | iterate_iovec(i, n, v, iov, skip, (I)) \ |
| 172 | if (skip == iov->iov_len) { \ |
| 173 | iov++; \ |
| 174 | skip = 0; \ |
| 175 | } \ |
| 176 | i->nr_segs -= iov - i->iov; \ |
| 177 | i->iov = iov; \ |
Al Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 178 | } \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 179 | i->count -= n; \ |
| 180 | i->iov_offset = skip; \ |
Al Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 181 | } \ |
Al Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 184 | static int copyout(void __user *to, const void *from, size_t n) |
| 185 | { |
Albert van der Linde | 4d0e9df | 2020-10-15 20:13:50 -0700 | [diff] [blame] | 186 | if (should_fail_usercopy()) |
| 187 | return n; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 188 | if (access_ok(to, n)) { |
Marco Elver | d0ef4c3 | 2020-01-21 17:05:11 +0100 | [diff] [blame] | 189 | instrument_copy_to_user(to, from, n); |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 190 | n = raw_copy_to_user(to, from, n); |
| 191 | } |
| 192 | return n; |
| 193 | } |
| 194 | |
| 195 | static int copyin(void *to, const void __user *from, size_t n) |
| 196 | { |
Albert van der Linde | 4d0e9df | 2020-10-15 20:13:50 -0700 | [diff] [blame] | 197 | if (should_fail_usercopy()) |
| 198 | return n; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 199 | if (access_ok(from, n)) { |
Marco Elver | d0ef4c3 | 2020-01-21 17:05:11 +0100 | [diff] [blame] | 200 | instrument_copy_from_user(to, from, n); |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 201 | n = raw_copy_from_user(to, from, n); |
| 202 | } |
| 203 | return n; |
| 204 | } |
| 205 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 206 | 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] | 207 | struct iov_iter *i) |
| 208 | { |
| 209 | size_t skip, copy, left, wanted; |
| 210 | const struct iovec *iov; |
| 211 | char __user *buf; |
| 212 | void *kaddr, *from; |
| 213 | |
| 214 | if (unlikely(bytes > i->count)) |
| 215 | bytes = i->count; |
| 216 | |
| 217 | if (unlikely(!bytes)) |
| 218 | return 0; |
| 219 | |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 220 | might_fault(); |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 221 | wanted = bytes; |
| 222 | iov = i->iov; |
| 223 | skip = i->iov_offset; |
| 224 | buf = iov->iov_base + skip; |
| 225 | copy = min(bytes, iov->iov_len - skip); |
| 226 | |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 227 | if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) { |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 228 | kaddr = kmap_atomic(page); |
| 229 | from = kaddr + offset; |
| 230 | |
| 231 | /* first chunk, usually the only one */ |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 232 | left = copyout(buf, from, copy); |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 233 | copy -= left; |
| 234 | skip += copy; |
| 235 | from += copy; |
| 236 | bytes -= copy; |
| 237 | |
| 238 | while (unlikely(!left && bytes)) { |
| 239 | iov++; |
| 240 | buf = iov->iov_base; |
| 241 | copy = min(bytes, iov->iov_len); |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 242 | left = copyout(buf, from, copy); |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 243 | copy -= left; |
| 244 | skip = copy; |
| 245 | from += copy; |
| 246 | bytes -= copy; |
| 247 | } |
| 248 | if (likely(!bytes)) { |
| 249 | kunmap_atomic(kaddr); |
| 250 | goto done; |
| 251 | } |
| 252 | offset = from - kaddr; |
| 253 | buf += copy; |
| 254 | kunmap_atomic(kaddr); |
| 255 | copy = min(bytes, iov->iov_len - skip); |
| 256 | } |
| 257 | /* Too bad - revert to non-atomic kmap */ |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 258 | |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 259 | kaddr = kmap(page); |
| 260 | from = kaddr + offset; |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 261 | left = copyout(buf, from, copy); |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 262 | copy -= left; |
| 263 | skip += copy; |
| 264 | from += copy; |
| 265 | bytes -= copy; |
| 266 | while (unlikely(!left && bytes)) { |
| 267 | iov++; |
| 268 | buf = iov->iov_base; |
| 269 | copy = min(bytes, iov->iov_len); |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 270 | left = copyout(buf, from, copy); |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 271 | copy -= left; |
| 272 | skip = copy; |
| 273 | from += copy; |
| 274 | bytes -= copy; |
| 275 | } |
| 276 | kunmap(page); |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 277 | |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 278 | done: |
Al Viro | 81055e5 | 2014-04-04 19:23:46 -0400 | [diff] [blame] | 279 | if (skip == iov->iov_len) { |
| 280 | iov++; |
| 281 | skip = 0; |
| 282 | } |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 283 | i->count -= wanted - bytes; |
| 284 | i->nr_segs -= iov - i->iov; |
| 285 | i->iov = iov; |
| 286 | i->iov_offset = skip; |
| 287 | return wanted - bytes; |
| 288 | } |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 289 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 290 | 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] | 291 | struct iov_iter *i) |
| 292 | { |
| 293 | size_t skip, copy, left, wanted; |
| 294 | const struct iovec *iov; |
| 295 | char __user *buf; |
| 296 | void *kaddr, *to; |
| 297 | |
| 298 | if (unlikely(bytes > i->count)) |
| 299 | bytes = i->count; |
| 300 | |
| 301 | if (unlikely(!bytes)) |
| 302 | return 0; |
| 303 | |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 304 | might_fault(); |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 305 | wanted = bytes; |
| 306 | iov = i->iov; |
| 307 | skip = i->iov_offset; |
| 308 | buf = iov->iov_base + skip; |
| 309 | copy = min(bytes, iov->iov_len - skip); |
| 310 | |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 311 | if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) { |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 312 | kaddr = kmap_atomic(page); |
| 313 | to = kaddr + offset; |
| 314 | |
| 315 | /* first chunk, usually the only one */ |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 316 | left = copyin(to, buf, copy); |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 317 | copy -= left; |
| 318 | skip += copy; |
| 319 | to += copy; |
| 320 | bytes -= copy; |
| 321 | |
| 322 | while (unlikely(!left && bytes)) { |
| 323 | iov++; |
| 324 | buf = iov->iov_base; |
| 325 | copy = min(bytes, iov->iov_len); |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 326 | left = copyin(to, buf, copy); |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 327 | copy -= left; |
| 328 | skip = copy; |
| 329 | to += copy; |
| 330 | bytes -= copy; |
| 331 | } |
| 332 | if (likely(!bytes)) { |
| 333 | kunmap_atomic(kaddr); |
| 334 | goto done; |
| 335 | } |
| 336 | offset = to - kaddr; |
| 337 | buf += copy; |
| 338 | kunmap_atomic(kaddr); |
| 339 | copy = min(bytes, iov->iov_len - skip); |
| 340 | } |
| 341 | /* Too bad - revert to non-atomic kmap */ |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 342 | |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 343 | kaddr = kmap(page); |
| 344 | to = kaddr + offset; |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 345 | left = copyin(to, buf, copy); |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 346 | copy -= left; |
| 347 | skip += copy; |
| 348 | to += copy; |
| 349 | bytes -= copy; |
| 350 | while (unlikely(!left && bytes)) { |
| 351 | iov++; |
| 352 | buf = iov->iov_base; |
| 353 | copy = min(bytes, iov->iov_len); |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 354 | left = copyin(to, buf, copy); |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 355 | copy -= left; |
| 356 | skip = copy; |
| 357 | to += copy; |
| 358 | bytes -= copy; |
| 359 | } |
| 360 | kunmap(page); |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 361 | |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 362 | done: |
Al Viro | 81055e5 | 2014-04-04 19:23:46 -0400 | [diff] [blame] | 363 | if (skip == iov->iov_len) { |
| 364 | iov++; |
| 365 | skip = 0; |
| 366 | } |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 367 | i->count -= wanted - bytes; |
| 368 | i->nr_segs -= iov - i->iov; |
| 369 | i->iov = iov; |
| 370 | i->iov_offset = skip; |
| 371 | return wanted - bytes; |
| 372 | } |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 373 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 374 | #ifdef PIPE_PARANOIA |
| 375 | static bool sanity(const struct iov_iter *i) |
| 376 | { |
| 377 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 378 | unsigned int p_head = pipe->head; |
| 379 | unsigned int p_tail = pipe->tail; |
| 380 | unsigned int p_mask = pipe->ring_size - 1; |
| 381 | unsigned int p_occupancy = pipe_occupancy(p_head, p_tail); |
| 382 | unsigned int i_head = i->head; |
| 383 | unsigned int idx; |
| 384 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 385 | if (i->iov_offset) { |
| 386 | struct pipe_buffer *p; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 387 | if (unlikely(p_occupancy == 0)) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 388 | goto Bad; // pipe must be non-empty |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 389 | if (unlikely(i_head != p_head - 1)) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 390 | goto Bad; // must be at the last buffer... |
| 391 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 392 | p = &pipe->bufs[i_head & p_mask]; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 393 | if (unlikely(p->offset + p->len != i->iov_offset)) |
| 394 | goto Bad; // ... at the end of segment |
| 395 | } else { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 396 | if (i_head != p_head) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 397 | goto Bad; // must be right after the last buffer |
| 398 | } |
| 399 | return true; |
| 400 | Bad: |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 401 | printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset); |
| 402 | printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n", |
| 403 | p_head, p_tail, pipe->ring_size); |
| 404 | for (idx = 0; idx < pipe->ring_size; idx++) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 405 | printk(KERN_ERR "[%p %p %d %d]\n", |
| 406 | pipe->bufs[idx].ops, |
| 407 | pipe->bufs[idx].page, |
| 408 | pipe->bufs[idx].offset, |
| 409 | pipe->bufs[idx].len); |
| 410 | WARN_ON(1); |
| 411 | return false; |
| 412 | } |
| 413 | #else |
| 414 | #define sanity(i) true |
| 415 | #endif |
| 416 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 417 | static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes, |
| 418 | struct iov_iter *i) |
| 419 | { |
| 420 | struct pipe_inode_info *pipe = i->pipe; |
| 421 | struct pipe_buffer *buf; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 422 | unsigned int p_tail = pipe->tail; |
| 423 | unsigned int p_mask = pipe->ring_size - 1; |
| 424 | unsigned int i_head = i->head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 425 | size_t off; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 426 | |
| 427 | if (unlikely(bytes > i->count)) |
| 428 | bytes = i->count; |
| 429 | |
| 430 | if (unlikely(!bytes)) |
| 431 | return 0; |
| 432 | |
| 433 | if (!sanity(i)) |
| 434 | return 0; |
| 435 | |
| 436 | off = i->iov_offset; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 437 | buf = &pipe->bufs[i_head & p_mask]; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 438 | if (off) { |
| 439 | if (offset == off && buf->page == page) { |
| 440 | /* merge with the last one */ |
| 441 | buf->len += bytes; |
| 442 | i->iov_offset += bytes; |
| 443 | goto out; |
| 444 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 445 | i_head++; |
| 446 | buf = &pipe->bufs[i_head & p_mask]; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 447 | } |
David Howells | 6718b6f | 2019-10-16 16:47:32 +0100 | [diff] [blame] | 448 | if (pipe_full(i_head, p_tail, pipe->max_usage)) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 449 | return 0; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 450 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 451 | buf->ops = &page_cache_pipe_buf_ops; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 452 | get_page(page); |
| 453 | buf->page = page; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 454 | buf->offset = offset; |
| 455 | buf->len = bytes; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 456 | |
| 457 | pipe->head = i_head + 1; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 458 | i->iov_offset = offset + bytes; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 459 | i->head = i_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 460 | out: |
| 461 | i->count -= bytes; |
| 462 | return bytes; |
| 463 | } |
| 464 | |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 465 | /* |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 466 | * Fault in one or more iovecs of the given iov_iter, to a maximum length of |
| 467 | * bytes. For each iovec, fault in each page that constitutes the iovec. |
| 468 | * |
| 469 | * Return 0 on success, or non-zero if the memory could not be accessed (i.e. |
| 470 | * because it is an invalid address). |
| 471 | */ |
Al Viro | d4690f1 | 2016-09-16 00:11:45 +0100 | [diff] [blame] | 472 | int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes) |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 473 | { |
| 474 | size_t skip = i->iov_offset; |
| 475 | const struct iovec *iov; |
| 476 | int err; |
| 477 | struct iovec v; |
| 478 | |
| 479 | if (!(i->type & (ITER_BVEC|ITER_KVEC))) { |
| 480 | iterate_iovec(i, bytes, v, iov, skip, ({ |
Al Viro | 4bce9f6e | 2016-09-17 18:02:44 -0400 | [diff] [blame] | 481 | err = fault_in_pages_readable(v.iov_base, v.iov_len); |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 482 | if (unlikely(err)) |
| 483 | return err; |
| 484 | 0;})) |
| 485 | } |
| 486 | return 0; |
| 487 | } |
Al Viro | d4690f1 | 2016-09-16 00:11:45 +0100 | [diff] [blame] | 488 | EXPORT_SYMBOL(iov_iter_fault_in_readable); |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 489 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 490 | void iov_iter_init(struct iov_iter *i, unsigned int direction, |
Al Viro | 71d8e53 | 2014-03-05 19:28:09 -0500 | [diff] [blame] | 491 | const struct iovec *iov, unsigned long nr_segs, |
| 492 | size_t count) |
| 493 | { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 494 | WARN_ON(direction & ~(READ | WRITE)); |
| 495 | direction &= READ | WRITE; |
| 496 | |
Al Viro | 71d8e53 | 2014-03-05 19:28:09 -0500 | [diff] [blame] | 497 | /* It will get better. Eventually... */ |
Al Viro | db68ce1 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 498 | if (uaccess_kernel()) { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 499 | i->type = ITER_KVEC | direction; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 500 | i->kvec = (struct kvec *)iov; |
| 501 | } else { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 502 | i->type = ITER_IOVEC | direction; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 503 | i->iov = iov; |
| 504 | } |
Al Viro | 71d8e53 | 2014-03-05 19:28:09 -0500 | [diff] [blame] | 505 | i->nr_segs = nr_segs; |
| 506 | i->iov_offset = 0; |
| 507 | i->count = count; |
| 508 | } |
| 509 | EXPORT_SYMBOL(iov_iter_init); |
Al Viro | 7b2c99d | 2014-03-15 04:05:57 -0400 | [diff] [blame] | 510 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 511 | static inline bool allocated(struct pipe_buffer *buf) |
| 512 | { |
| 513 | return buf->ops == &default_pipe_buf_ops; |
| 514 | } |
| 515 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 516 | static inline void data_start(const struct iov_iter *i, |
| 517 | unsigned int *iter_headp, size_t *offp) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 518 | { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 519 | unsigned int p_mask = i->pipe->ring_size - 1; |
| 520 | unsigned int iter_head = i->head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 521 | size_t off = i->iov_offset; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 522 | |
| 523 | if (off && (!allocated(&i->pipe->bufs[iter_head & p_mask]) || |
| 524 | off == PAGE_SIZE)) { |
| 525 | iter_head++; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 526 | off = 0; |
| 527 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 528 | *iter_headp = iter_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 529 | *offp = off; |
| 530 | } |
| 531 | |
| 532 | static size_t push_pipe(struct iov_iter *i, size_t size, |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 533 | int *iter_headp, size_t *offp) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 534 | { |
| 535 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 536 | unsigned int p_tail = pipe->tail; |
| 537 | unsigned int p_mask = pipe->ring_size - 1; |
| 538 | unsigned int iter_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 539 | size_t off; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 540 | ssize_t left; |
| 541 | |
| 542 | if (unlikely(size > i->count)) |
| 543 | size = i->count; |
| 544 | if (unlikely(!size)) |
| 545 | return 0; |
| 546 | |
| 547 | left = size; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 548 | data_start(i, &iter_head, &off); |
| 549 | *iter_headp = iter_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 550 | *offp = off; |
| 551 | if (off) { |
| 552 | left -= PAGE_SIZE - off; |
| 553 | if (left <= 0) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 554 | pipe->bufs[iter_head & p_mask].len += size; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 555 | return size; |
| 556 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 557 | pipe->bufs[iter_head & p_mask].len = PAGE_SIZE; |
| 558 | iter_head++; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 559 | } |
David Howells | 6718b6f | 2019-10-16 16:47:32 +0100 | [diff] [blame] | 560 | while (!pipe_full(iter_head, p_tail, pipe->max_usage)) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 561 | struct pipe_buffer *buf = &pipe->bufs[iter_head & p_mask]; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 562 | struct page *page = alloc_page(GFP_USER); |
| 563 | if (!page) |
| 564 | break; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 565 | |
| 566 | buf->ops = &default_pipe_buf_ops; |
| 567 | buf->page = page; |
| 568 | buf->offset = 0; |
| 569 | buf->len = min_t(ssize_t, left, PAGE_SIZE); |
| 570 | left -= buf->len; |
| 571 | iter_head++; |
| 572 | pipe->head = iter_head; |
| 573 | |
| 574 | if (left == 0) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 575 | return size; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 576 | } |
| 577 | return size - left; |
| 578 | } |
| 579 | |
| 580 | static size_t copy_pipe_to_iter(const void *addr, size_t bytes, |
| 581 | struct iov_iter *i) |
| 582 | { |
| 583 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 584 | unsigned int p_mask = pipe->ring_size - 1; |
| 585 | unsigned int i_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 586 | size_t n, off; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 587 | |
| 588 | if (!sanity(i)) |
| 589 | return 0; |
| 590 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 591 | bytes = n = push_pipe(i, bytes, &i_head, &off); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 592 | if (unlikely(!n)) |
| 593 | return 0; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 594 | do { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 595 | size_t chunk = min_t(size_t, n, PAGE_SIZE - off); |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 596 | memcpy_to_page(pipe->bufs[i_head & p_mask].page, off, addr, chunk); |
| 597 | i->head = i_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 598 | i->iov_offset = off + chunk; |
| 599 | n -= chunk; |
| 600 | addr += chunk; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 601 | off = 0; |
| 602 | i_head++; |
| 603 | } while (n); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 604 | i->count -= bytes; |
| 605 | return bytes; |
| 606 | } |
| 607 | |
Al Viro | f915289 | 2018-11-27 22:32:59 -0500 | [diff] [blame] | 608 | static __wsum csum_and_memcpy(void *to, const void *from, size_t len, |
| 609 | __wsum sum, size_t off) |
| 610 | { |
Al Viro | cc44c17 | 2020-07-11 00:12:07 -0400 | [diff] [blame] | 611 | __wsum next = csum_partial_copy_nocheck(from, to, len); |
Al Viro | f915289 | 2018-11-27 22:32:59 -0500 | [diff] [blame] | 612 | return csum_block_add(sum, next, off); |
| 613 | } |
| 614 | |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 615 | static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes, |
Willem de Bruijn | 52cbd23 | 2021-02-03 14:29:52 -0500 | [diff] [blame] | 616 | struct csum_state *csstate, |
| 617 | struct iov_iter *i) |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 618 | { |
| 619 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 620 | unsigned int p_mask = pipe->ring_size - 1; |
Willem de Bruijn | 52cbd23 | 2021-02-03 14:29:52 -0500 | [diff] [blame] | 621 | __wsum sum = csstate->csum; |
| 622 | size_t off = csstate->off; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 623 | unsigned int i_head; |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 624 | size_t n, r; |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 625 | |
| 626 | if (!sanity(i)) |
| 627 | return 0; |
| 628 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 629 | bytes = n = push_pipe(i, bytes, &i_head, &r); |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 630 | if (unlikely(!n)) |
| 631 | return 0; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 632 | do { |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 633 | size_t chunk = min_t(size_t, n, PAGE_SIZE - r); |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 634 | char *p = kmap_atomic(pipe->bufs[i_head & p_mask].page); |
Al Viro | f915289 | 2018-11-27 22:32:59 -0500 | [diff] [blame] | 635 | sum = csum_and_memcpy(p + r, addr, chunk, sum, off); |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 636 | kunmap_atomic(p); |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 637 | i->head = i_head; |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 638 | i->iov_offset = r + chunk; |
| 639 | n -= chunk; |
| 640 | off += chunk; |
| 641 | addr += chunk; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 642 | r = 0; |
| 643 | i_head++; |
| 644 | } while (n); |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 645 | i->count -= bytes; |
Willem de Bruijn | 52cbd23 | 2021-02-03 14:29:52 -0500 | [diff] [blame] | 646 | csstate->csum = sum; |
| 647 | csstate->off = off; |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 648 | return bytes; |
| 649 | } |
| 650 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 651 | 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] | 652 | { |
Al Viro | 36f7a8a | 2015-12-06 16:49:22 -0500 | [diff] [blame] | 653 | const char *from = addr; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 654 | if (unlikely(iov_iter_is_pipe(i))) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 655 | return copy_pipe_to_iter(addr, bytes, i); |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 656 | if (iter_is_iovec(i)) |
| 657 | might_fault(); |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 658 | iterate_and_advance(i, bytes, v, |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 659 | copyout(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len), |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 660 | memcpy_to_page(v.bv_page, v.bv_offset, |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 661 | (from += v.bv_len) - v.bv_len, v.bv_len), |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 662 | memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len), |
| 663 | memcpy_to_page(v.bv_page, v.bv_offset, |
| 664 | (from += v.bv_len) - v.bv_len, v.bv_len) |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 665 | ) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 666 | |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 667 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 668 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 669 | EXPORT_SYMBOL(_copy_to_iter); |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 670 | |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 671 | #ifdef CONFIG_ARCH_HAS_COPY_MC |
| 672 | 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] | 673 | { |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 674 | if (access_ok(to, n)) { |
Marco Elver | d0ef4c3 | 2020-01-21 17:05:11 +0100 | [diff] [blame] | 675 | instrument_copy_to_user(to, from, n); |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 676 | n = copy_mc_to_user((__force void *) to, from, n); |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 677 | } |
| 678 | return n; |
| 679 | } |
| 680 | |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 681 | static unsigned long copy_mc_to_page(struct page *page, size_t offset, |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 682 | const char *from, size_t len) |
| 683 | { |
| 684 | unsigned long ret; |
| 685 | char *to; |
| 686 | |
| 687 | to = kmap_atomic(page); |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 688 | ret = copy_mc_to_kernel(to + offset, from, len); |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 689 | kunmap_atomic(to); |
| 690 | |
| 691 | return ret; |
| 692 | } |
| 693 | |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 694 | 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] | 695 | struct iov_iter *i) |
| 696 | { |
| 697 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 698 | unsigned int p_mask = pipe->ring_size - 1; |
| 699 | unsigned int i_head; |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 700 | size_t n, off, xfer = 0; |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 701 | |
| 702 | if (!sanity(i)) |
| 703 | return 0; |
| 704 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 705 | bytes = n = push_pipe(i, bytes, &i_head, &off); |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 706 | if (unlikely(!n)) |
| 707 | return 0; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 708 | do { |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 709 | size_t chunk = min_t(size_t, n, PAGE_SIZE - off); |
| 710 | unsigned long rem; |
| 711 | |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 712 | rem = copy_mc_to_page(pipe->bufs[i_head & p_mask].page, |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 713 | off, addr, chunk); |
| 714 | i->head = i_head; |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 715 | i->iov_offset = off + chunk - rem; |
| 716 | xfer += chunk - rem; |
| 717 | if (rem) |
| 718 | break; |
| 719 | n -= chunk; |
| 720 | addr += chunk; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 721 | off = 0; |
| 722 | i_head++; |
| 723 | } while (n); |
Dan Williams | ca146f6 | 2018-07-08 13:46:12 -0700 | [diff] [blame] | 724 | i->count -= xfer; |
| 725 | return xfer; |
| 726 | } |
| 727 | |
Dan Williams | bf3eeb9 | 2018-07-08 13:46:02 -0700 | [diff] [blame] | 728 | /** |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 729 | * _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] | 730 | * @addr: source kernel address |
| 731 | * @bytes: total transfer length |
| 732 | * @iter: destination iterator |
| 733 | * |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 734 | * The pmem driver deploys this for the dax operation |
| 735 | * (dax_copy_to_iter()) for dax reads (bypass page-cache and the |
| 736 | * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes |
| 737 | * successfully copied. |
Dan Williams | bf3eeb9 | 2018-07-08 13:46:02 -0700 | [diff] [blame] | 738 | * |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 739 | * The main differences between this and typical _copy_to_iter(). |
Dan Williams | bf3eeb9 | 2018-07-08 13:46:02 -0700 | [diff] [blame] | 740 | * |
| 741 | * * Typical tail/residue handling after a fault retries the copy |
| 742 | * byte-by-byte until the fault happens again. Re-triggering machine |
| 743 | * checks is potentially fatal so the implementation uses source |
| 744 | * alignment and poison alignment assumptions to avoid re-triggering |
| 745 | * hardware exceptions. |
| 746 | * |
| 747 | * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies. |
| 748 | * Compare to copy_to_iter() where only ITER_IOVEC attempts might return |
| 749 | * a short copy. |
Dan Williams | bf3eeb9 | 2018-07-08 13:46:02 -0700 | [diff] [blame] | 750 | */ |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 751 | 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] | 752 | { |
| 753 | const char *from = addr; |
| 754 | unsigned long rem, curr_addr, s_addr = (unsigned long) addr; |
| 755 | |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 756 | if (unlikely(iov_iter_is_pipe(i))) |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 757 | return copy_mc_pipe_to_iter(addr, bytes, i); |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 758 | if (iter_is_iovec(i)) |
| 759 | might_fault(); |
| 760 | iterate_and_advance(i, bytes, v, |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 761 | copyout_mc(v.iov_base, (from += v.iov_len) - v.iov_len, |
| 762 | v.iov_len), |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 763 | ({ |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 764 | rem = copy_mc_to_page(v.bv_page, v.bv_offset, |
| 765 | (from += v.bv_len) - v.bv_len, v.bv_len); |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 766 | if (rem) { |
| 767 | curr_addr = (unsigned long) from; |
| 768 | bytes = curr_addr - s_addr - rem; |
| 769 | return bytes; |
| 770 | } |
| 771 | }), |
| 772 | ({ |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 773 | rem = copy_mc_to_kernel(v.iov_base, (from += v.iov_len) |
| 774 | - v.iov_len, v.iov_len); |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 775 | if (rem) { |
| 776 | curr_addr = (unsigned long) from; |
| 777 | bytes = curr_addr - s_addr - rem; |
| 778 | return bytes; |
| 779 | } |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 780 | }), |
| 781 | ({ |
| 782 | rem = copy_mc_to_page(v.bv_page, v.bv_offset, |
| 783 | (from += v.bv_len) - v.bv_len, v.bv_len); |
| 784 | if (rem) { |
| 785 | curr_addr = (unsigned long) from; |
| 786 | bytes = curr_addr - s_addr - rem; |
| 787 | rcu_read_unlock(); |
David Howells | 3d14ec1 | 2021-04-25 22:02:38 +0100 | [diff] [blame] | 788 | i->iov_offset += bytes; |
| 789 | i->count -= bytes; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 790 | return bytes; |
| 791 | } |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 792 | }) |
| 793 | ) |
| 794 | |
| 795 | return bytes; |
| 796 | } |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame] | 797 | EXPORT_SYMBOL_GPL(_copy_mc_to_iter); |
| 798 | #endif /* CONFIG_ARCH_HAS_COPY_MC */ |
Dan Williams | 8780356 | 2018-05-03 17:06:31 -0700 | [diff] [blame] | 799 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 800 | 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] | 801 | { |
Al Viro | 0dbca9a | 2014-11-27 14:26:43 -0500 | [diff] [blame] | 802 | char *to = addr; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 803 | if (unlikely(iov_iter_is_pipe(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 804 | WARN_ON(1); |
| 805 | return 0; |
| 806 | } |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 807 | if (iter_is_iovec(i)) |
| 808 | might_fault(); |
Al Viro | 0dbca9a | 2014-11-27 14:26:43 -0500 | [diff] [blame] | 809 | iterate_and_advance(i, bytes, v, |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 810 | copyin((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len), |
Al Viro | 0dbca9a | 2014-11-27 14:26:43 -0500 | [diff] [blame] | 811 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 812 | v.bv_offset, v.bv_len), |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 813 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len), |
| 814 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
| 815 | v.bv_offset, v.bv_len) |
Al Viro | 0dbca9a | 2014-11-27 14:26:43 -0500 | [diff] [blame] | 816 | ) |
| 817 | |
| 818 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 819 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 820 | EXPORT_SYMBOL(_copy_from_iter); |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 821 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 822 | bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i) |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 823 | { |
| 824 | char *to = addr; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 825 | if (unlikely(iov_iter_is_pipe(i))) { |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 826 | WARN_ON(1); |
| 827 | return false; |
| 828 | } |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 829 | if (unlikely(i->count < bytes)) |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 830 | return false; |
| 831 | |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 832 | if (iter_is_iovec(i)) |
| 833 | might_fault(); |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 834 | iterate_all_kinds(i, bytes, v, ({ |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 835 | if (copyin((to += v.iov_len) - v.iov_len, |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 836 | v.iov_base, v.iov_len)) |
| 837 | return false; |
| 838 | 0;}), |
| 839 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
| 840 | v.bv_offset, v.bv_len), |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 841 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len), |
| 842 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
| 843 | v.bv_offset, v.bv_len) |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 844 | ) |
| 845 | |
| 846 | iov_iter_advance(i, bytes); |
| 847 | return true; |
| 848 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 849 | EXPORT_SYMBOL(_copy_from_iter_full); |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 850 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 851 | 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] | 852 | { |
| 853 | char *to = addr; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 854 | if (unlikely(iov_iter_is_pipe(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 855 | WARN_ON(1); |
| 856 | return 0; |
| 857 | } |
Al Viro | aa58309 | 2014-11-27 20:27:08 -0500 | [diff] [blame] | 858 | iterate_and_advance(i, bytes, v, |
Al Viro | 3f76345 | 2017-03-25 18:47:28 -0400 | [diff] [blame] | 859 | __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len, |
Al Viro | aa58309 | 2014-11-27 20:27:08 -0500 | [diff] [blame] | 860 | v.iov_base, v.iov_len), |
| 861 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
| 862 | v.bv_offset, v.bv_len), |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 863 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len), |
| 864 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
| 865 | v.bv_offset, v.bv_len) |
Al Viro | aa58309 | 2014-11-27 20:27:08 -0500 | [diff] [blame] | 866 | ) |
| 867 | |
| 868 | return bytes; |
| 869 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 870 | EXPORT_SYMBOL(_copy_from_iter_nocache); |
Al Viro | aa58309 | 2014-11-27 20:27:08 -0500 | [diff] [blame] | 871 | |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 872 | #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE |
Dan Williams | abd08d7 | 2018-07-08 13:46:07 -0700 | [diff] [blame] | 873 | /** |
| 874 | * _copy_from_iter_flushcache - write destination through cpu cache |
| 875 | * @addr: destination kernel address |
| 876 | * @bytes: total transfer length |
| 877 | * @iter: source iterator |
| 878 | * |
| 879 | * The pmem driver arranges for filesystem-dax to use this facility via |
| 880 | * dax_copy_from_iter() for ensuring that writes to persistent memory |
| 881 | * are flushed through the CPU cache. It is differentiated from |
| 882 | * _copy_from_iter_nocache() in that guarantees all data is flushed for |
| 883 | * all iterator types. The _copy_from_iter_nocache() only attempts to |
| 884 | * bypass the cache for the ITER_IOVEC case, and on some archs may use |
| 885 | * instructions that strand dirty-data in the cache. |
| 886 | */ |
Linus Torvalds | 6a37e94 | 2017-07-07 20:39:20 -0700 | [diff] [blame] | 887 | 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] | 888 | { |
| 889 | char *to = addr; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 890 | if (unlikely(iov_iter_is_pipe(i))) { |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 891 | WARN_ON(1); |
| 892 | return 0; |
| 893 | } |
| 894 | iterate_and_advance(i, bytes, v, |
| 895 | __copy_from_user_flushcache((to += v.iov_len) - v.iov_len, |
| 896 | v.iov_base, v.iov_len), |
| 897 | memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page, |
| 898 | v.bv_offset, v.bv_len), |
| 899 | memcpy_flushcache((to += v.iov_len) - v.iov_len, v.iov_base, |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 900 | v.iov_len), |
| 901 | memcpy_page_flushcache((to += v.bv_len) - v.bv_len, v.bv_page, |
| 902 | v.bv_offset, v.bv_len) |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 903 | ) |
| 904 | |
| 905 | return bytes; |
| 906 | } |
Linus Torvalds | 6a37e94 | 2017-07-07 20:39:20 -0700 | [diff] [blame] | 907 | EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache); |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 908 | #endif |
| 909 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 910 | bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i) |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 911 | { |
| 912 | char *to = addr; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 913 | if (unlikely(iov_iter_is_pipe(i))) { |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 914 | WARN_ON(1); |
| 915 | return false; |
| 916 | } |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 917 | if (unlikely(i->count < bytes)) |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 918 | return false; |
| 919 | iterate_all_kinds(i, bytes, v, ({ |
Al Viro | 3f76345 | 2017-03-25 18:47:28 -0400 | [diff] [blame] | 920 | if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len, |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 921 | v.iov_base, v.iov_len)) |
| 922 | return false; |
| 923 | 0;}), |
| 924 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
| 925 | v.bv_offset, v.bv_len), |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 926 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len), |
| 927 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
| 928 | v.bv_offset, v.bv_len) |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 929 | ) |
| 930 | |
| 931 | iov_iter_advance(i, bytes); |
| 932 | return true; |
| 933 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 934 | EXPORT_SYMBOL(_copy_from_iter_full_nocache); |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 935 | |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame] | 936 | static inline bool page_copy_sane(struct page *page, size_t offset, size_t n) |
| 937 | { |
Eric Dumazet | 6daef95 | 2019-02-26 10:42:39 -0800 | [diff] [blame] | 938 | struct page *head; |
| 939 | size_t v = n + offset; |
| 940 | |
| 941 | /* |
| 942 | * The general case needs to access the page order in order |
| 943 | * to compute the page size. |
| 944 | * However, we mostly deal with order-0 pages and thus can |
| 945 | * avoid a possible cache line miss for requests that fit all |
| 946 | * page orders. |
| 947 | */ |
| 948 | if (n <= v && v <= PAGE_SIZE) |
| 949 | return true; |
| 950 | |
| 951 | head = compound_head(page); |
| 952 | v += (page - head) << PAGE_SHIFT; |
Petar Penkov | a90bcb8 | 2017-08-29 11:20:32 -0700 | [diff] [blame] | 953 | |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 954 | if (likely(n <= v && v <= (page_size(head)))) |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame] | 955 | return true; |
| 956 | WARN_ON(1); |
| 957 | return false; |
| 958 | } |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 959 | |
Al Viro | 08aa647 | 2021-04-29 20:42:25 -0400 | [diff] [blame^] | 960 | 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] | 961 | struct iov_iter *i) |
| 962 | { |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 963 | if (i->type & (ITER_BVEC | ITER_KVEC | ITER_XARRAY)) { |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 964 | void *kaddr = kmap_atomic(page); |
| 965 | size_t wanted = copy_to_iter(kaddr + offset, bytes, i); |
| 966 | kunmap_atomic(kaddr); |
| 967 | return wanted; |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 968 | } else if (unlikely(iov_iter_is_discard(i))) |
| 969 | return bytes; |
| 970 | else if (likely(!iov_iter_is_pipe(i))) |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 971 | return copy_page_to_iter_iovec(page, offset, bytes, i); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 972 | else |
| 973 | return copy_page_to_iter_pipe(page, offset, bytes, i); |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 974 | } |
Al Viro | 08aa647 | 2021-04-29 20:42:25 -0400 | [diff] [blame^] | 975 | |
| 976 | size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, |
| 977 | struct iov_iter *i) |
| 978 | { |
| 979 | size_t res = 0; |
| 980 | if (unlikely(!page_copy_sane(page, offset, bytes))) |
| 981 | return 0; |
| 982 | page += offset / PAGE_SIZE; // first subpage |
| 983 | offset %= PAGE_SIZE; |
| 984 | while (1) { |
| 985 | size_t n = __copy_page_to_iter(page, offset, |
| 986 | min(bytes, (size_t)PAGE_SIZE - offset), i); |
| 987 | res += n; |
| 988 | bytes -= n; |
| 989 | if (!bytes || !n) |
| 990 | break; |
| 991 | offset += n; |
| 992 | if (offset == PAGE_SIZE) { |
| 993 | page++; |
| 994 | offset = 0; |
| 995 | } |
| 996 | } |
| 997 | return res; |
| 998 | } |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 999 | EXPORT_SYMBOL(copy_page_to_iter); |
| 1000 | |
| 1001 | size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, |
| 1002 | struct iov_iter *i) |
| 1003 | { |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame] | 1004 | if (unlikely(!page_copy_sane(page, offset, bytes))) |
| 1005 | return 0; |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1006 | if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1007 | WARN_ON(1); |
| 1008 | return 0; |
| 1009 | } |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1010 | if (i->type & (ITER_BVEC | ITER_KVEC | ITER_XARRAY)) { |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 1011 | void *kaddr = kmap_atomic(page); |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 1012 | size_t wanted = _copy_from_iter(kaddr + offset, bytes, i); |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 1013 | kunmap_atomic(kaddr); |
| 1014 | return wanted; |
| 1015 | } else |
| 1016 | return copy_page_from_iter_iovec(page, offset, bytes, i); |
| 1017 | } |
| 1018 | EXPORT_SYMBOL(copy_page_from_iter); |
| 1019 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1020 | static size_t pipe_zero(size_t bytes, struct iov_iter *i) |
| 1021 | { |
| 1022 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1023 | unsigned int p_mask = pipe->ring_size - 1; |
| 1024 | unsigned int i_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1025 | size_t n, off; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1026 | |
| 1027 | if (!sanity(i)) |
| 1028 | return 0; |
| 1029 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1030 | bytes = n = push_pipe(i, bytes, &i_head, &off); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1031 | if (unlikely(!n)) |
| 1032 | return 0; |
| 1033 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1034 | do { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1035 | size_t chunk = min_t(size_t, n, PAGE_SIZE - off); |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1036 | memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk); |
| 1037 | i->head = i_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1038 | i->iov_offset = off + chunk; |
| 1039 | n -= chunk; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1040 | off = 0; |
| 1041 | i_head++; |
| 1042 | } while (n); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1043 | i->count -= bytes; |
| 1044 | return bytes; |
| 1045 | } |
| 1046 | |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 1047 | size_t iov_iter_zero(size_t bytes, struct iov_iter *i) |
| 1048 | { |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1049 | if (unlikely(iov_iter_is_pipe(i))) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1050 | return pipe_zero(bytes, i); |
Al Viro | 8442fa4 | 2014-11-27 14:18:54 -0500 | [diff] [blame] | 1051 | iterate_and_advance(i, bytes, v, |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 1052 | clear_user(v.iov_base, v.iov_len), |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 1053 | memzero_page(v.bv_page, v.bv_offset, v.bv_len), |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1054 | memset(v.iov_base, 0, v.iov_len), |
| 1055 | memzero_page(v.bv_page, v.bv_offset, v.bv_len) |
Al Viro | 8442fa4 | 2014-11-27 14:18:54 -0500 | [diff] [blame] | 1056 | ) |
| 1057 | |
| 1058 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 1059 | } |
| 1060 | EXPORT_SYMBOL(iov_iter_zero); |
| 1061 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1062 | size_t iov_iter_copy_from_user_atomic(struct page *page, |
| 1063 | struct iov_iter *i, unsigned long offset, size_t bytes) |
| 1064 | { |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 1065 | char *kaddr = kmap_atomic(page), *p = kaddr + offset; |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame] | 1066 | if (unlikely(!page_copy_sane(page, offset, bytes))) { |
| 1067 | kunmap_atomic(kaddr); |
| 1068 | return 0; |
| 1069 | } |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1070 | if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1071 | kunmap_atomic(kaddr); |
| 1072 | WARN_ON(1); |
| 1073 | return 0; |
| 1074 | } |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 1075 | iterate_all_kinds(i, bytes, v, |
Al Viro | 09fc68dc | 2017-06-29 22:25:14 -0400 | [diff] [blame] | 1076 | copyin((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len), |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 1077 | memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page, |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 1078 | v.bv_offset, v.bv_len), |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1079 | memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len), |
| 1080 | memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page, |
| 1081 | v.bv_offset, v.bv_len) |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 1082 | ) |
| 1083 | kunmap_atomic(kaddr); |
| 1084 | return bytes; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1085 | } |
| 1086 | EXPORT_SYMBOL(iov_iter_copy_from_user_atomic); |
| 1087 | |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1088 | static inline void pipe_truncate(struct iov_iter *i) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1089 | { |
| 1090 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1091 | unsigned int p_tail = pipe->tail; |
| 1092 | unsigned int p_head = pipe->head; |
| 1093 | unsigned int p_mask = pipe->ring_size - 1; |
| 1094 | |
| 1095 | if (!pipe_empty(p_head, p_tail)) { |
| 1096 | struct pipe_buffer *buf; |
| 1097 | unsigned int i_head = i->head; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1098 | size_t off = i->iov_offset; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1099 | |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1100 | if (off) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1101 | buf = &pipe->bufs[i_head & p_mask]; |
| 1102 | buf->len = off - buf->offset; |
| 1103 | i_head++; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1104 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1105 | while (p_head != i_head) { |
| 1106 | p_head--; |
| 1107 | pipe_buf_release(pipe, &pipe->bufs[p_head & p_mask]); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1108 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1109 | |
| 1110 | pipe->head = p_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1111 | } |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | static void pipe_advance(struct iov_iter *i, size_t size) |
| 1115 | { |
| 1116 | struct pipe_inode_info *pipe = i->pipe; |
| 1117 | if (unlikely(i->count < size)) |
| 1118 | size = i->count; |
| 1119 | if (size) { |
| 1120 | struct pipe_buffer *buf; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1121 | unsigned int p_mask = pipe->ring_size - 1; |
| 1122 | unsigned int i_head = i->head; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1123 | size_t off = i->iov_offset, left = size; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1124 | |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1125 | if (off) /* make it relative to the beginning of buffer */ |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1126 | left += off - pipe->bufs[i_head & p_mask].offset; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1127 | while (1) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1128 | buf = &pipe->bufs[i_head & p_mask]; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1129 | if (left <= buf->len) |
| 1130 | break; |
| 1131 | left -= buf->len; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1132 | i_head++; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1133 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1134 | i->head = i_head; |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 1135 | i->iov_offset = buf->offset + left; |
| 1136 | } |
| 1137 | i->count -= size; |
| 1138 | /* ... and discard everything past that point */ |
| 1139 | pipe_truncate(i); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1140 | } |
| 1141 | |
Pavel Begunkov | 54c8195 | 2021-01-09 16:03:01 +0000 | [diff] [blame] | 1142 | static void iov_iter_bvec_advance(struct iov_iter *i, size_t size) |
| 1143 | { |
| 1144 | struct bvec_iter bi; |
| 1145 | |
| 1146 | bi.bi_size = i->count; |
| 1147 | bi.bi_bvec_done = i->iov_offset; |
| 1148 | bi.bi_idx = 0; |
| 1149 | bvec_iter_advance(i->bvec, &bi, size); |
| 1150 | |
| 1151 | i->bvec += bi.bi_idx; |
| 1152 | i->nr_segs -= bi.bi_idx; |
| 1153 | i->count = bi.bi_size; |
| 1154 | i->iov_offset = bi.bi_bvec_done; |
| 1155 | } |
| 1156 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1157 | void iov_iter_advance(struct iov_iter *i, size_t size) |
| 1158 | { |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1159 | if (unlikely(iov_iter_is_pipe(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1160 | pipe_advance(i, size); |
| 1161 | return; |
| 1162 | } |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1163 | if (unlikely(iov_iter_is_discard(i))) { |
| 1164 | i->count -= size; |
| 1165 | return; |
| 1166 | } |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1167 | if (unlikely(iov_iter_is_xarray(i))) { |
David Howells | 3d14ec1 | 2021-04-25 22:02:38 +0100 | [diff] [blame] | 1168 | size = min(size, i->count); |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1169 | i->iov_offset += size; |
| 1170 | i->count -= size; |
| 1171 | return; |
| 1172 | } |
Pavel Begunkov | 54c8195 | 2021-01-09 16:03:01 +0000 | [diff] [blame] | 1173 | if (iov_iter_is_bvec(i)) { |
| 1174 | iov_iter_bvec_advance(i, size); |
| 1175 | return; |
| 1176 | } |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1177 | iterate_and_advance(i, size, v, 0, 0, 0, 0) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1178 | } |
| 1179 | EXPORT_SYMBOL(iov_iter_advance); |
| 1180 | |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1181 | void iov_iter_revert(struct iov_iter *i, size_t unroll) |
| 1182 | { |
| 1183 | if (!unroll) |
| 1184 | return; |
Al Viro | 5b47d59 | 2017-05-08 13:54:47 -0400 | [diff] [blame] | 1185 | if (WARN_ON(unroll > MAX_RW_COUNT)) |
| 1186 | return; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1187 | i->count += unroll; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1188 | if (unlikely(iov_iter_is_pipe(i))) { |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1189 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1190 | unsigned int p_mask = pipe->ring_size - 1; |
| 1191 | unsigned int i_head = i->head; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1192 | size_t off = i->iov_offset; |
| 1193 | while (1) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1194 | struct pipe_buffer *b = &pipe->bufs[i_head & p_mask]; |
| 1195 | size_t n = off - b->offset; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1196 | if (unroll < n) { |
Al Viro | 4fa55ce | 2017-04-29 16:42:30 -0400 | [diff] [blame] | 1197 | off -= unroll; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1198 | break; |
| 1199 | } |
| 1200 | unroll -= n; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1201 | if (!unroll && i_head == i->start_head) { |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1202 | off = 0; |
| 1203 | break; |
| 1204 | } |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1205 | i_head--; |
| 1206 | b = &pipe->bufs[i_head & p_mask]; |
| 1207 | off = b->offset + b->len; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1208 | } |
| 1209 | i->iov_offset = off; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1210 | i->head = i_head; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1211 | pipe_truncate(i); |
| 1212 | return; |
| 1213 | } |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1214 | if (unlikely(iov_iter_is_discard(i))) |
| 1215 | return; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1216 | if (unroll <= i->iov_offset) { |
| 1217 | i->iov_offset -= unroll; |
| 1218 | return; |
| 1219 | } |
| 1220 | unroll -= i->iov_offset; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1221 | if (iov_iter_is_xarray(i)) { |
| 1222 | BUG(); /* We should never go beyond the start of the specified |
| 1223 | * range since we might then be straying into pages that |
| 1224 | * aren't pinned. |
| 1225 | */ |
| 1226 | } else if (iov_iter_is_bvec(i)) { |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 1227 | const struct bio_vec *bvec = i->bvec; |
| 1228 | while (1) { |
| 1229 | size_t n = (--bvec)->bv_len; |
| 1230 | i->nr_segs++; |
| 1231 | if (unroll <= n) { |
| 1232 | i->bvec = bvec; |
| 1233 | i->iov_offset = n - unroll; |
| 1234 | return; |
| 1235 | } |
| 1236 | unroll -= n; |
| 1237 | } |
| 1238 | } else { /* same logics for iovec and kvec */ |
| 1239 | const struct iovec *iov = i->iov; |
| 1240 | while (1) { |
| 1241 | size_t n = (--iov)->iov_len; |
| 1242 | i->nr_segs++; |
| 1243 | if (unroll <= n) { |
| 1244 | i->iov = iov; |
| 1245 | i->iov_offset = n - unroll; |
| 1246 | return; |
| 1247 | } |
| 1248 | unroll -= n; |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | EXPORT_SYMBOL(iov_iter_revert); |
| 1253 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1254 | /* |
| 1255 | * Return the count of just the current iov_iter segment. |
| 1256 | */ |
| 1257 | size_t iov_iter_single_seg_count(const struct iov_iter *i) |
| 1258 | { |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1259 | if (unlikely(iov_iter_is_pipe(i))) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1260 | return i->count; // it is a silly place, anyway |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1261 | if (i->nr_segs == 1) |
| 1262 | return i->count; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1263 | if (unlikely(iov_iter_is_discard(i) || iov_iter_is_xarray(i))) |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1264 | return i->count; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1265 | if (iov_iter_is_bvec(i)) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1266 | return min(i->count, i->bvec->bv_len - i->iov_offset); |
Paul Mackerras | ad0eab9 | 2014-11-13 20:15:23 +1100 | [diff] [blame] | 1267 | else |
| 1268 | return min(i->count, i->iov->iov_len - i->iov_offset); |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1269 | } |
| 1270 | EXPORT_SYMBOL(iov_iter_single_seg_count); |
| 1271 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1272 | void iov_iter_kvec(struct iov_iter *i, unsigned int direction, |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 1273 | const struct kvec *kvec, unsigned long nr_segs, |
Al Viro | abb78f8 | 2014-11-24 14:46:11 -0500 | [diff] [blame] | 1274 | size_t count) |
| 1275 | { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1276 | WARN_ON(direction & ~(READ | WRITE)); |
| 1277 | i->type = ITER_KVEC | (direction & (READ | WRITE)); |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 1278 | i->kvec = kvec; |
Al Viro | abb78f8 | 2014-11-24 14:46:11 -0500 | [diff] [blame] | 1279 | i->nr_segs = nr_segs; |
| 1280 | i->iov_offset = 0; |
| 1281 | i->count = count; |
| 1282 | } |
| 1283 | EXPORT_SYMBOL(iov_iter_kvec); |
| 1284 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1285 | void iov_iter_bvec(struct iov_iter *i, unsigned int direction, |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 1286 | const struct bio_vec *bvec, unsigned long nr_segs, |
| 1287 | size_t count) |
| 1288 | { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1289 | WARN_ON(direction & ~(READ | WRITE)); |
| 1290 | i->type = ITER_BVEC | (direction & (READ | WRITE)); |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 1291 | i->bvec = bvec; |
| 1292 | i->nr_segs = nr_segs; |
| 1293 | i->iov_offset = 0; |
| 1294 | i->count = count; |
| 1295 | } |
| 1296 | EXPORT_SYMBOL(iov_iter_bvec); |
| 1297 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1298 | void iov_iter_pipe(struct iov_iter *i, unsigned int direction, |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1299 | struct pipe_inode_info *pipe, |
| 1300 | size_t count) |
| 1301 | { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1302 | BUG_ON(direction != READ); |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1303 | WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size)); |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1304 | i->type = ITER_PIPE | READ; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1305 | i->pipe = pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1306 | i->head = pipe->head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1307 | i->iov_offset = 0; |
| 1308 | i->count = count; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1309 | i->start_head = i->head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1310 | } |
| 1311 | EXPORT_SYMBOL(iov_iter_pipe); |
| 1312 | |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1313 | /** |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1314 | * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray |
| 1315 | * @i: The iterator to initialise. |
| 1316 | * @direction: The direction of the transfer. |
| 1317 | * @xarray: The xarray to access. |
| 1318 | * @start: The start file position. |
| 1319 | * @count: The size of the I/O buffer in bytes. |
| 1320 | * |
| 1321 | * Set up an I/O iterator to either draw data out of the pages attached to an |
| 1322 | * inode or to inject data into those pages. The pages *must* be prevented |
| 1323 | * from evaporation, either by taking a ref on them or locking them by the |
| 1324 | * caller. |
| 1325 | */ |
| 1326 | void iov_iter_xarray(struct iov_iter *i, unsigned int direction, |
| 1327 | struct xarray *xarray, loff_t start, size_t count) |
| 1328 | { |
| 1329 | BUG_ON(direction & ~1); |
| 1330 | i->type = ITER_XARRAY | (direction & (READ | WRITE)); |
| 1331 | i->xarray = xarray; |
| 1332 | i->xarray_start = start; |
| 1333 | i->count = count; |
| 1334 | i->iov_offset = 0; |
| 1335 | } |
| 1336 | EXPORT_SYMBOL(iov_iter_xarray); |
| 1337 | |
| 1338 | /** |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1339 | * iov_iter_discard - Initialise an I/O iterator that discards data |
| 1340 | * @i: The iterator to initialise. |
| 1341 | * @direction: The direction of the transfer. |
| 1342 | * @count: The size of the I/O buffer in bytes. |
| 1343 | * |
| 1344 | * Set up an I/O iterator that just discards everything that's written to it. |
| 1345 | * It's only available as a READ iterator. |
| 1346 | */ |
| 1347 | void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count) |
| 1348 | { |
| 1349 | BUG_ON(direction != READ); |
| 1350 | i->type = ITER_DISCARD | READ; |
| 1351 | i->count = count; |
| 1352 | i->iov_offset = 0; |
| 1353 | } |
| 1354 | EXPORT_SYMBOL(iov_iter_discard); |
| 1355 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1356 | unsigned long iov_iter_alignment(const struct iov_iter *i) |
| 1357 | { |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 1358 | unsigned long res = 0; |
| 1359 | size_t size = i->count; |
| 1360 | |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1361 | if (unlikely(iov_iter_is_pipe(i))) { |
Jan Kara | e0ff126 | 2019-12-16 11:54:32 +0100 | [diff] [blame] | 1362 | unsigned int p_mask = i->pipe->ring_size - 1; |
| 1363 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1364 | 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] | 1365 | return size | i->iov_offset; |
| 1366 | return size; |
| 1367 | } |
David Howells | 3d14ec1 | 2021-04-25 22:02:38 +0100 | [diff] [blame] | 1368 | if (unlikely(iov_iter_is_xarray(i))) |
| 1369 | return (i->xarray_start + i->iov_offset) | i->count; |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 1370 | iterate_all_kinds(i, size, v, |
| 1371 | (res |= (unsigned long)v.iov_base | v.iov_len, 0), |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 1372 | res |= v.bv_offset | v.bv_len, |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1373 | res |= (unsigned long)v.iov_base | v.iov_len, |
| 1374 | res |= v.bv_offset | v.bv_len |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 1375 | ) |
| 1376 | return res; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1377 | } |
| 1378 | EXPORT_SYMBOL(iov_iter_alignment); |
| 1379 | |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 1380 | unsigned long iov_iter_gap_alignment(const struct iov_iter *i) |
| 1381 | { |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 1382 | unsigned long res = 0; |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 1383 | size_t size = i->count; |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 1384 | |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1385 | if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1386 | WARN_ON(1); |
| 1387 | return ~0U; |
| 1388 | } |
| 1389 | |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 1390 | iterate_all_kinds(i, size, v, |
| 1391 | (res |= (!res ? 0 : (unsigned long)v.iov_base) | |
| 1392 | (size != v.iov_len ? size : 0), 0), |
| 1393 | (res |= (!res ? 0 : (unsigned long)v.bv_offset) | |
| 1394 | (size != v.bv_len ? size : 0)), |
| 1395 | (res |= (!res ? 0 : (unsigned long)v.iov_base) | |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1396 | (size != v.iov_len ? size : 0)), |
| 1397 | (res |= (!res ? 0 : (unsigned long)v.bv_offset) | |
| 1398 | (size != v.bv_len ? size : 0)) |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 1399 | ); |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 1400 | return res; |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 1401 | } |
| 1402 | EXPORT_SYMBOL(iov_iter_gap_alignment); |
| 1403 | |
Ilya Dryomov | e76b6312 | 2018-05-02 20:16:56 +0200 | [diff] [blame] | 1404 | static inline ssize_t __pipe_get_pages(struct iov_iter *i, |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1405 | size_t maxsize, |
| 1406 | struct page **pages, |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1407 | int iter_head, |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1408 | size_t *start) |
| 1409 | { |
| 1410 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1411 | unsigned int p_mask = pipe->ring_size - 1; |
| 1412 | ssize_t n = push_pipe(i, maxsize, &iter_head, start); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1413 | if (!n) |
| 1414 | return -EFAULT; |
| 1415 | |
| 1416 | maxsize = n; |
| 1417 | n += *start; |
Al Viro | 1689c73 | 2016-10-11 18:21:14 +0100 | [diff] [blame] | 1418 | while (n > 0) { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1419 | get_page(*pages++ = pipe->bufs[iter_head & p_mask].page); |
| 1420 | iter_head++; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1421 | n -= PAGE_SIZE; |
| 1422 | } |
| 1423 | |
| 1424 | return maxsize; |
| 1425 | } |
| 1426 | |
| 1427 | static ssize_t pipe_get_pages(struct iov_iter *i, |
| 1428 | struct page **pages, size_t maxsize, unsigned maxpages, |
| 1429 | size_t *start) |
| 1430 | { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1431 | unsigned int iter_head, npages; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1432 | size_t capacity; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1433 | |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 1434 | if (!maxsize) |
| 1435 | return 0; |
| 1436 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1437 | if (!sanity(i)) |
| 1438 | return -EFAULT; |
| 1439 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1440 | data_start(i, &iter_head, start); |
| 1441 | /* Amount of free space: some of this one + all after this one */ |
| 1442 | npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe); |
| 1443 | capacity = min(npages, maxpages) * PAGE_SIZE - *start; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1444 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1445 | return __pipe_get_pages(i, min(maxsize, capacity), pages, iter_head, start); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1446 | } |
| 1447 | |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1448 | static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa, |
| 1449 | pgoff_t index, unsigned int nr_pages) |
| 1450 | { |
| 1451 | XA_STATE(xas, xa, index); |
| 1452 | struct page *page; |
| 1453 | unsigned int ret = 0; |
| 1454 | |
| 1455 | rcu_read_lock(); |
| 1456 | for (page = xas_load(&xas); page; page = xas_next(&xas)) { |
| 1457 | if (xas_retry(&xas, page)) |
| 1458 | continue; |
| 1459 | |
| 1460 | /* Has the page moved or been split? */ |
| 1461 | if (unlikely(page != xas_reload(&xas))) { |
| 1462 | xas_reset(&xas); |
| 1463 | continue; |
| 1464 | } |
| 1465 | |
| 1466 | pages[ret] = find_subpage(page, xas.xa_index); |
| 1467 | get_page(pages[ret]); |
| 1468 | if (++ret == nr_pages) |
| 1469 | break; |
| 1470 | } |
| 1471 | rcu_read_unlock(); |
| 1472 | return ret; |
| 1473 | } |
| 1474 | |
| 1475 | static ssize_t iter_xarray_get_pages(struct iov_iter *i, |
| 1476 | struct page **pages, size_t maxsize, |
| 1477 | unsigned maxpages, size_t *_start_offset) |
| 1478 | { |
| 1479 | unsigned nr, offset; |
| 1480 | pgoff_t index, count; |
| 1481 | size_t size = maxsize, actual; |
| 1482 | loff_t pos; |
| 1483 | |
| 1484 | if (!size || !maxpages) |
| 1485 | return 0; |
| 1486 | |
| 1487 | pos = i->xarray_start + i->iov_offset; |
| 1488 | index = pos >> PAGE_SHIFT; |
| 1489 | offset = pos & ~PAGE_MASK; |
| 1490 | *_start_offset = offset; |
| 1491 | |
| 1492 | count = 1; |
| 1493 | if (size > PAGE_SIZE - offset) { |
| 1494 | size -= PAGE_SIZE - offset; |
| 1495 | count += size >> PAGE_SHIFT; |
| 1496 | size &= ~PAGE_MASK; |
| 1497 | if (size) |
| 1498 | count++; |
| 1499 | } |
| 1500 | |
| 1501 | if (count > maxpages) |
| 1502 | count = maxpages; |
| 1503 | |
| 1504 | nr = iter_xarray_populate_pages(pages, i->xarray, index, count); |
| 1505 | if (nr == 0) |
| 1506 | return 0; |
| 1507 | |
| 1508 | actual = PAGE_SIZE * nr; |
| 1509 | actual -= offset; |
| 1510 | if (nr == count && size > 0) { |
| 1511 | unsigned last_offset = (nr > 1) ? 0 : offset; |
| 1512 | actual -= PAGE_SIZE - (last_offset + size); |
| 1513 | } |
| 1514 | return actual; |
| 1515 | } |
| 1516 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1517 | ssize_t iov_iter_get_pages(struct iov_iter *i, |
Miklos Szeredi | 2c80929 | 2014-09-24 17:09:11 +0200 | [diff] [blame] | 1518 | struct page **pages, size_t maxsize, unsigned maxpages, |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1519 | size_t *start) |
| 1520 | { |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 1521 | if (maxsize > i->count) |
| 1522 | maxsize = i->count; |
| 1523 | |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1524 | if (unlikely(iov_iter_is_pipe(i))) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1525 | return pipe_get_pages(i, pages, maxsize, maxpages, start); |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1526 | if (unlikely(iov_iter_is_xarray(i))) |
| 1527 | return iter_xarray_get_pages(i, pages, maxsize, maxpages, start); |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1528 | if (unlikely(iov_iter_is_discard(i))) |
| 1529 | return -EFAULT; |
| 1530 | |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 1531 | iterate_all_kinds(i, maxsize, v, ({ |
| 1532 | unsigned long addr = (unsigned long)v.iov_base; |
| 1533 | size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1)); |
| 1534 | int n; |
| 1535 | int res; |
| 1536 | |
| 1537 | if (len > maxpages * PAGE_SIZE) |
| 1538 | len = maxpages * PAGE_SIZE; |
| 1539 | addr &= ~(PAGE_SIZE - 1); |
| 1540 | n = DIV_ROUND_UP(len, PAGE_SIZE); |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 1541 | res = get_user_pages_fast(addr, n, |
| 1542 | iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, |
| 1543 | pages); |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 1544 | if (unlikely(res < 0)) |
| 1545 | return res; |
| 1546 | return (res == n ? len : res * PAGE_SIZE) - *start; |
| 1547 | 0;}),({ |
| 1548 | /* can't be more than PAGE_SIZE */ |
| 1549 | *start = v.bv_offset; |
| 1550 | get_page(*pages = v.bv_page); |
| 1551 | return v.bv_len; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 1552 | }),({ |
| 1553 | return -EFAULT; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1554 | }), |
| 1555 | 0 |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 1556 | ) |
| 1557 | return 0; |
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 | |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 1574 | if (!maxsize) |
| 1575 | return 0; |
| 1576 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1577 | if (!sanity(i)) |
| 1578 | return -EFAULT; |
| 1579 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1580 | data_start(i, &iter_head, start); |
| 1581 | /* Amount of free space: some of this one + all after this one */ |
| 1582 | npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1583 | n = npages * PAGE_SIZE - *start; |
| 1584 | if (maxsize > n) |
| 1585 | maxsize = n; |
| 1586 | else |
| 1587 | npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE); |
| 1588 | p = get_pages_array(npages); |
| 1589 | if (!p) |
| 1590 | return -ENOMEM; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1591 | n = __pipe_get_pages(i, maxsize, p, iter_head, start); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1592 | if (n > 0) |
| 1593 | *pages = p; |
| 1594 | else |
| 1595 | kvfree(p); |
| 1596 | return n; |
| 1597 | } |
| 1598 | |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1599 | static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i, |
| 1600 | struct page ***pages, size_t maxsize, |
| 1601 | size_t *_start_offset) |
| 1602 | { |
| 1603 | struct page **p; |
| 1604 | unsigned nr, offset; |
| 1605 | pgoff_t index, count; |
| 1606 | size_t size = maxsize, actual; |
| 1607 | loff_t pos; |
| 1608 | |
| 1609 | if (!size) |
| 1610 | return 0; |
| 1611 | |
| 1612 | pos = i->xarray_start + i->iov_offset; |
| 1613 | index = pos >> PAGE_SHIFT; |
| 1614 | offset = pos & ~PAGE_MASK; |
| 1615 | *_start_offset = offset; |
| 1616 | |
| 1617 | count = 1; |
| 1618 | if (size > PAGE_SIZE - offset) { |
| 1619 | size -= PAGE_SIZE - offset; |
| 1620 | count += size >> PAGE_SHIFT; |
| 1621 | size &= ~PAGE_MASK; |
| 1622 | if (size) |
| 1623 | count++; |
| 1624 | } |
| 1625 | |
| 1626 | p = get_pages_array(count); |
| 1627 | if (!p) |
| 1628 | return -ENOMEM; |
| 1629 | *pages = p; |
| 1630 | |
| 1631 | nr = iter_xarray_populate_pages(p, i->xarray, index, count); |
| 1632 | if (nr == 0) |
| 1633 | return 0; |
| 1634 | |
| 1635 | actual = PAGE_SIZE * nr; |
| 1636 | actual -= offset; |
| 1637 | if (nr == count && size > 0) { |
| 1638 | unsigned last_offset = (nr > 1) ? 0 : offset; |
| 1639 | actual -= PAGE_SIZE - (last_offset + size); |
| 1640 | } |
| 1641 | return actual; |
| 1642 | } |
| 1643 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1644 | ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, |
| 1645 | struct page ***pages, size_t maxsize, |
| 1646 | size_t *start) |
| 1647 | { |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1648 | struct page **p; |
| 1649 | |
| 1650 | if (maxsize > i->count) |
| 1651 | maxsize = i->count; |
| 1652 | |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1653 | if (unlikely(iov_iter_is_pipe(i))) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1654 | return pipe_get_pages_alloc(i, pages, maxsize, start); |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1655 | if (unlikely(iov_iter_is_xarray(i))) |
| 1656 | return iter_xarray_get_pages_alloc(i, pages, maxsize, start); |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1657 | if (unlikely(iov_iter_is_discard(i))) |
| 1658 | return -EFAULT; |
| 1659 | |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1660 | iterate_all_kinds(i, maxsize, v, ({ |
| 1661 | unsigned long addr = (unsigned long)v.iov_base; |
| 1662 | size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1)); |
| 1663 | int n; |
| 1664 | int res; |
| 1665 | |
| 1666 | addr &= ~(PAGE_SIZE - 1); |
| 1667 | n = DIV_ROUND_UP(len, PAGE_SIZE); |
| 1668 | p = get_pages_array(n); |
| 1669 | if (!p) |
| 1670 | return -ENOMEM; |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 1671 | res = get_user_pages_fast(addr, n, |
| 1672 | iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p); |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1673 | if (unlikely(res < 0)) { |
| 1674 | kvfree(p); |
| 1675 | return res; |
| 1676 | } |
| 1677 | *pages = p; |
| 1678 | return (res == n ? len : res * PAGE_SIZE) - *start; |
| 1679 | 0;}),({ |
| 1680 | /* can't be more than PAGE_SIZE */ |
| 1681 | *start = v.bv_offset; |
| 1682 | *pages = p = get_pages_array(1); |
| 1683 | if (!p) |
| 1684 | return -ENOMEM; |
| 1685 | get_page(*p = v.bv_page); |
| 1686 | return v.bv_len; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 1687 | }),({ |
| 1688 | return -EFAULT; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1689 | }), 0 |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1690 | ) |
| 1691 | return 0; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1692 | } |
| 1693 | EXPORT_SYMBOL(iov_iter_get_pages_alloc); |
| 1694 | |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1695 | size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, |
| 1696 | struct iov_iter *i) |
| 1697 | { |
| 1698 | char *to = addr; |
| 1699 | __wsum sum, next; |
| 1700 | size_t off = 0; |
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 | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1706 | iterate_and_advance(i, bytes, v, ({ |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1707 | next = csum_and_copy_from_user(v.iov_base, |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1708 | (to += v.iov_len) - v.iov_len, |
Al Viro | c693cc4 | 2020-07-11 00:27:49 -0400 | [diff] [blame] | 1709 | v.iov_len); |
| 1710 | if (next) { |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1711 | sum = csum_block_add(sum, next, off); |
| 1712 | off += v.iov_len; |
| 1713 | } |
Al Viro | c693cc4 | 2020-07-11 00:27:49 -0400 | [diff] [blame] | 1714 | next ? 0 : v.iov_len; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1715 | }), ({ |
| 1716 | char *p = kmap_atomic(v.bv_page); |
Al Viro | f915289 | 2018-11-27 22:32:59 -0500 | [diff] [blame] | 1717 | sum = csum_and_memcpy((to += v.bv_len) - v.bv_len, |
| 1718 | p + v.bv_offset, v.bv_len, |
| 1719 | sum, off); |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1720 | kunmap_atomic(p); |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1721 | off += v.bv_len; |
| 1722 | }),({ |
Al Viro | f915289 | 2018-11-27 22:32:59 -0500 | [diff] [blame] | 1723 | sum = csum_and_memcpy((to += v.iov_len) - v.iov_len, |
| 1724 | v.iov_base, v.iov_len, |
| 1725 | sum, off); |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1726 | off += v.iov_len; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1727 | }), ({ |
| 1728 | char *p = kmap_atomic(v.bv_page); |
| 1729 | sum = csum_and_memcpy((to += v.bv_len) - v.bv_len, |
| 1730 | p + v.bv_offset, v.bv_len, |
| 1731 | sum, off); |
| 1732 | kunmap_atomic(p); |
| 1733 | off += v.bv_len; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1734 | }) |
| 1735 | ) |
| 1736 | *csum = sum; |
| 1737 | return bytes; |
| 1738 | } |
| 1739 | EXPORT_SYMBOL(csum_and_copy_from_iter); |
| 1740 | |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1741 | bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, |
| 1742 | struct iov_iter *i) |
| 1743 | { |
| 1744 | char *to = addr; |
| 1745 | __wsum sum, next; |
| 1746 | size_t off = 0; |
| 1747 | sum = *csum; |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1748 | if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1749 | WARN_ON(1); |
| 1750 | return false; |
| 1751 | } |
| 1752 | if (unlikely(i->count < bytes)) |
| 1753 | return false; |
| 1754 | iterate_all_kinds(i, bytes, v, ({ |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1755 | next = csum_and_copy_from_user(v.iov_base, |
| 1756 | (to += v.iov_len) - v.iov_len, |
Al Viro | c693cc4 | 2020-07-11 00:27:49 -0400 | [diff] [blame] | 1757 | v.iov_len); |
| 1758 | if (!next) |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1759 | return false; |
| 1760 | sum = csum_block_add(sum, next, off); |
| 1761 | off += v.iov_len; |
| 1762 | 0; |
| 1763 | }), ({ |
| 1764 | char *p = kmap_atomic(v.bv_page); |
Al Viro | f915289 | 2018-11-27 22:32:59 -0500 | [diff] [blame] | 1765 | sum = csum_and_memcpy((to += v.bv_len) - v.bv_len, |
| 1766 | p + v.bv_offset, v.bv_len, |
| 1767 | sum, off); |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1768 | kunmap_atomic(p); |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1769 | off += v.bv_len; |
| 1770 | }),({ |
Al Viro | f915289 | 2018-11-27 22:32:59 -0500 | [diff] [blame] | 1771 | sum = csum_and_memcpy((to += v.iov_len) - v.iov_len, |
| 1772 | v.iov_base, v.iov_len, |
| 1773 | sum, off); |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1774 | off += v.iov_len; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1775 | }), ({ |
| 1776 | char *p = kmap_atomic(v.bv_page); |
| 1777 | sum = csum_and_memcpy((to += v.bv_len) - v.bv_len, |
| 1778 | p + v.bv_offset, v.bv_len, |
| 1779 | sum, off); |
| 1780 | kunmap_atomic(p); |
| 1781 | off += v.bv_len; |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1782 | }) |
| 1783 | ) |
| 1784 | *csum = sum; |
| 1785 | iov_iter_advance(i, bytes); |
| 1786 | return true; |
| 1787 | } |
| 1788 | EXPORT_SYMBOL(csum_and_copy_from_iter_full); |
| 1789 | |
Willem de Bruijn | 52cbd23 | 2021-02-03 14:29:52 -0500 | [diff] [blame] | 1790 | 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] | 1791 | struct iov_iter *i) |
| 1792 | { |
Willem de Bruijn | 52cbd23 | 2021-02-03 14:29:52 -0500 | [diff] [blame] | 1793 | struct csum_state *csstate = _csstate; |
Al Viro | 36f7a8a | 2015-12-06 16:49:22 -0500 | [diff] [blame] | 1794 | const char *from = addr; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1795 | __wsum sum, next; |
Willem de Bruijn | 52cbd23 | 2021-02-03 14:29:52 -0500 | [diff] [blame] | 1796 | size_t off; |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 1797 | |
| 1798 | if (unlikely(iov_iter_is_pipe(i))) |
Willem de Bruijn | 52cbd23 | 2021-02-03 14:29:52 -0500 | [diff] [blame] | 1799 | return csum_and_copy_to_pipe_iter(addr, bytes, _csstate, i); |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 1800 | |
Willem de Bruijn | 52cbd23 | 2021-02-03 14:29:52 -0500 | [diff] [blame] | 1801 | sum = csstate->csum; |
| 1802 | off = csstate->off; |
Al Viro | 78e1f38 | 2018-11-25 16:24:16 -0500 | [diff] [blame] | 1803 | if (unlikely(iov_iter_is_discard(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1804 | WARN_ON(1); /* for now */ |
| 1805 | return 0; |
| 1806 | } |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1807 | iterate_and_advance(i, bytes, v, ({ |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1808 | next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len, |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1809 | v.iov_base, |
Al Viro | c693cc4 | 2020-07-11 00:27:49 -0400 | [diff] [blame] | 1810 | v.iov_len); |
| 1811 | if (next) { |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1812 | sum = csum_block_add(sum, next, off); |
| 1813 | off += v.iov_len; |
| 1814 | } |
Al Viro | c693cc4 | 2020-07-11 00:27:49 -0400 | [diff] [blame] | 1815 | next ? 0 : v.iov_len; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1816 | }), ({ |
| 1817 | char *p = kmap_atomic(v.bv_page); |
Al Viro | f915289 | 2018-11-27 22:32:59 -0500 | [diff] [blame] | 1818 | sum = csum_and_memcpy(p + v.bv_offset, |
| 1819 | (from += v.bv_len) - v.bv_len, |
| 1820 | v.bv_len, sum, off); |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1821 | kunmap_atomic(p); |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1822 | off += v.bv_len; |
| 1823 | }),({ |
Al Viro | f915289 | 2018-11-27 22:32:59 -0500 | [diff] [blame] | 1824 | sum = csum_and_memcpy(v.iov_base, |
| 1825 | (from += v.iov_len) - v.iov_len, |
| 1826 | v.iov_len, sum, off); |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1827 | off += v.iov_len; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1828 | }), ({ |
| 1829 | char *p = kmap_atomic(v.bv_page); |
| 1830 | sum = csum_and_memcpy(p + v.bv_offset, |
| 1831 | (from += v.bv_len) - v.bv_len, |
| 1832 | v.bv_len, sum, off); |
| 1833 | kunmap_atomic(p); |
| 1834 | off += v.bv_len; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1835 | }) |
| 1836 | ) |
Willem de Bruijn | 52cbd23 | 2021-02-03 14:29:52 -0500 | [diff] [blame] | 1837 | csstate->csum = sum; |
| 1838 | csstate->off = off; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1839 | return bytes; |
| 1840 | } |
| 1841 | EXPORT_SYMBOL(csum_and_copy_to_iter); |
| 1842 | |
Sagi Grimberg | d05f443 | 2018-12-03 17:52:09 -0800 | [diff] [blame] | 1843 | size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, |
| 1844 | struct iov_iter *i) |
| 1845 | { |
Herbert Xu | 7999096 | 2020-06-12 16:57:37 +1000 | [diff] [blame] | 1846 | #ifdef CONFIG_CRYPTO_HASH |
Sagi Grimberg | d05f443 | 2018-12-03 17:52:09 -0800 | [diff] [blame] | 1847 | struct ahash_request *hash = hashp; |
| 1848 | struct scatterlist sg; |
| 1849 | size_t copied; |
| 1850 | |
| 1851 | copied = copy_to_iter(addr, bytes, i); |
| 1852 | sg_init_one(&sg, addr, copied); |
| 1853 | ahash_request_set_crypt(hash, &sg, NULL, copied); |
| 1854 | crypto_ahash_update(hash); |
| 1855 | return copied; |
YueHaibing | 27fad74 | 2019-04-04 10:31:14 +0800 | [diff] [blame] | 1856 | #else |
| 1857 | return 0; |
| 1858 | #endif |
Sagi Grimberg | d05f443 | 2018-12-03 17:52:09 -0800 | [diff] [blame] | 1859 | } |
| 1860 | EXPORT_SYMBOL(hash_and_copy_to_iter); |
| 1861 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1862 | int iov_iter_npages(const struct iov_iter *i, int maxpages) |
| 1863 | { |
Al Viro | e0f2dc4 | 2014-11-27 14:09:46 -0500 | [diff] [blame] | 1864 | size_t size = i->count; |
| 1865 | int npages = 0; |
| 1866 | |
| 1867 | if (!size) |
| 1868 | return 0; |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1869 | if (unlikely(iov_iter_is_discard(i))) |
| 1870 | return 0; |
Al Viro | e0f2dc4 | 2014-11-27 14:09:46 -0500 | [diff] [blame] | 1871 | |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1872 | if (unlikely(iov_iter_is_pipe(i))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1873 | struct pipe_inode_info *pipe = i->pipe; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1874 | unsigned int iter_head; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1875 | size_t off; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1876 | |
| 1877 | if (!sanity(i)) |
| 1878 | return 0; |
| 1879 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1880 | data_start(i, &iter_head, &off); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1881 | /* some of this one + all after this one */ |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1882 | npages = pipe_space_for_user(iter_head, pipe->tail, pipe); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1883 | if (npages >= maxpages) |
| 1884 | return maxpages; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1885 | } else if (unlikely(iov_iter_is_xarray(i))) { |
| 1886 | unsigned offset; |
| 1887 | |
| 1888 | offset = (i->xarray_start + i->iov_offset) & ~PAGE_MASK; |
| 1889 | |
| 1890 | npages = 1; |
| 1891 | if (size > PAGE_SIZE - offset) { |
| 1892 | size -= PAGE_SIZE - offset; |
| 1893 | npages += size >> PAGE_SHIFT; |
| 1894 | size &= ~PAGE_MASK; |
| 1895 | if (size) |
| 1896 | npages++; |
| 1897 | } |
| 1898 | if (npages >= maxpages) |
| 1899 | return maxpages; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1900 | } else iterate_all_kinds(i, size, v, ({ |
Al Viro | e0f2dc4 | 2014-11-27 14:09:46 -0500 | [diff] [blame] | 1901 | unsigned long p = (unsigned long)v.iov_base; |
| 1902 | npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE) |
| 1903 | - p / PAGE_SIZE; |
| 1904 | if (npages >= maxpages) |
| 1905 | return maxpages; |
| 1906 | 0;}),({ |
| 1907 | npages++; |
| 1908 | if (npages >= maxpages) |
| 1909 | return maxpages; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 1910 | }),({ |
| 1911 | unsigned long p = (unsigned long)v.iov_base; |
| 1912 | npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE) |
| 1913 | - p / PAGE_SIZE; |
| 1914 | if (npages >= maxpages) |
| 1915 | return maxpages; |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1916 | }), |
| 1917 | 0 |
Al Viro | e0f2dc4 | 2014-11-27 14:09:46 -0500 | [diff] [blame] | 1918 | ) |
| 1919 | return npages; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1920 | } |
Al Viro | f67da30 | 2014-03-19 01:16:16 -0400 | [diff] [blame] | 1921 | EXPORT_SYMBOL(iov_iter_npages); |
Al Viro | 4b8164b | 2015-01-31 20:08:47 -0500 | [diff] [blame] | 1922 | |
| 1923 | const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags) |
| 1924 | { |
| 1925 | *new = *old; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1926 | if (unlikely(iov_iter_is_pipe(new))) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1927 | WARN_ON(1); |
| 1928 | return NULL; |
| 1929 | } |
David Howells | 7ff506207 | 2020-02-10 10:00:21 +0000 | [diff] [blame] | 1930 | if (unlikely(iov_iter_is_discard(new) || iov_iter_is_xarray(new))) |
David Howells | 9ea9ce0 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1931 | return NULL; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1932 | if (iov_iter_is_bvec(new)) |
Al Viro | 4b8164b | 2015-01-31 20:08:47 -0500 | [diff] [blame] | 1933 | return new->bvec = kmemdup(new->bvec, |
| 1934 | new->nr_segs * sizeof(struct bio_vec), |
| 1935 | flags); |
| 1936 | else |
| 1937 | /* iovec and kvec have identical layout */ |
| 1938 | return new->iov = kmemdup(new->iov, |
| 1939 | new->nr_segs * sizeof(struct iovec), |
| 1940 | flags); |
| 1941 | } |
| 1942 | EXPORT_SYMBOL(dup_iter); |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 1943 | |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1944 | static int copy_compat_iovec_from_user(struct iovec *iov, |
| 1945 | const struct iovec __user *uvec, unsigned long nr_segs) |
| 1946 | { |
| 1947 | const struct compat_iovec __user *uiov = |
| 1948 | (const struct compat_iovec __user *)uvec; |
| 1949 | int ret = -EFAULT, i; |
| 1950 | |
Christoph Hellwig | a959a97 | 2021-01-11 18:19:26 +0100 | [diff] [blame] | 1951 | if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1952 | return -EFAULT; |
| 1953 | |
| 1954 | for (i = 0; i < nr_segs; i++) { |
| 1955 | compat_uptr_t buf; |
| 1956 | compat_ssize_t len; |
| 1957 | |
| 1958 | unsafe_get_user(len, &uiov[i].iov_len, uaccess_end); |
| 1959 | unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end); |
| 1960 | |
| 1961 | /* check for compat_size_t not fitting in compat_ssize_t .. */ |
| 1962 | if (len < 0) { |
| 1963 | ret = -EINVAL; |
| 1964 | goto uaccess_end; |
| 1965 | } |
| 1966 | iov[i].iov_base = compat_ptr(buf); |
| 1967 | iov[i].iov_len = len; |
| 1968 | } |
| 1969 | |
| 1970 | ret = 0; |
| 1971 | uaccess_end: |
| 1972 | user_access_end(); |
| 1973 | return ret; |
| 1974 | } |
| 1975 | |
| 1976 | static int copy_iovec_from_user(struct iovec *iov, |
| 1977 | const struct iovec __user *uvec, unsigned long nr_segs) |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1978 | { |
| 1979 | unsigned long seg; |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1980 | |
| 1981 | if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec))) |
| 1982 | return -EFAULT; |
| 1983 | for (seg = 0; seg < nr_segs; seg++) { |
| 1984 | if ((ssize_t)iov[seg].iov_len < 0) |
| 1985 | return -EINVAL; |
| 1986 | } |
| 1987 | |
| 1988 | return 0; |
| 1989 | } |
| 1990 | |
| 1991 | struct iovec *iovec_from_user(const struct iovec __user *uvec, |
| 1992 | unsigned long nr_segs, unsigned long fast_segs, |
| 1993 | struct iovec *fast_iov, bool compat) |
| 1994 | { |
| 1995 | struct iovec *iov = fast_iov; |
| 1996 | int ret; |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 1997 | |
| 1998 | /* |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 1999 | * SuS says "The readv() function *may* fail if the iovcnt argument was |
| 2000 | * 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] | 2001 | * traditionally returned zero for zero segments, so... |
| 2002 | */ |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2003 | if (nr_segs == 0) |
| 2004 | return iov; |
| 2005 | if (nr_segs > UIO_MAXIOV) |
| 2006 | return ERR_PTR(-EINVAL); |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 2007 | if (nr_segs > fast_segs) { |
| 2008 | iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2009 | if (!iov) |
| 2010 | return ERR_PTR(-ENOMEM); |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 2011 | } |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2012 | |
| 2013 | if (compat) |
| 2014 | ret = copy_compat_iovec_from_user(iov, uvec, nr_segs); |
| 2015 | else |
| 2016 | ret = copy_iovec_from_user(iov, uvec, nr_segs); |
| 2017 | if (ret) { |
| 2018 | if (iov != fast_iov) |
| 2019 | kfree(iov); |
| 2020 | return ERR_PTR(ret); |
| 2021 | } |
| 2022 | |
| 2023 | return iov; |
| 2024 | } |
| 2025 | |
| 2026 | ssize_t __import_iovec(int type, const struct iovec __user *uvec, |
| 2027 | unsigned nr_segs, unsigned fast_segs, struct iovec **iovp, |
| 2028 | struct iov_iter *i, bool compat) |
| 2029 | { |
| 2030 | ssize_t total_len = 0; |
| 2031 | unsigned long seg; |
| 2032 | struct iovec *iov; |
| 2033 | |
| 2034 | iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat); |
| 2035 | if (IS_ERR(iov)) { |
| 2036 | *iovp = NULL; |
| 2037 | return PTR_ERR(iov); |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | /* |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2041 | * According to the Single Unix Specification we should return EINVAL if |
| 2042 | * an element length is < 0 when cast to ssize_t or if the total length |
| 2043 | * would overflow the ssize_t return value of the system call. |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 2044 | * |
| 2045 | * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the |
| 2046 | * overflow case. |
| 2047 | */ |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 2048 | for (seg = 0; seg < nr_segs; seg++) { |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 2049 | ssize_t len = (ssize_t)iov[seg].iov_len; |
| 2050 | |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2051 | if (!access_ok(iov[seg].iov_base, len)) { |
| 2052 | if (iov != *iovp) |
| 2053 | kfree(iov); |
| 2054 | *iovp = NULL; |
| 2055 | return -EFAULT; |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 2056 | } |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2057 | |
| 2058 | if (len > MAX_RW_COUNT - total_len) { |
| 2059 | len = MAX_RW_COUNT - total_len; |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 2060 | iov[seg].iov_len = len; |
| 2061 | } |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2062 | total_len += len; |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 2063 | } |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2064 | |
| 2065 | iov_iter_init(i, type, iov, nr_segs, total_len); |
| 2066 | if (iov == *iovp) |
| 2067 | *iovp = NULL; |
| 2068 | else |
| 2069 | *iovp = iov; |
| 2070 | return total_len; |
David Laight | fb041b5 | 2020-09-25 06:51:39 +0200 | [diff] [blame] | 2071 | } |
| 2072 | |
Vegard Nossum | ffecee4 | 2016-10-08 11:18:07 +0200 | [diff] [blame] | 2073 | /** |
| 2074 | * import_iovec() - Copy an array of &struct iovec from userspace |
| 2075 | * into the kernel, check that it is valid, and initialize a new |
| 2076 | * &struct iov_iter iterator to access it. |
| 2077 | * |
| 2078 | * @type: One of %READ or %WRITE. |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2079 | * @uvec: Pointer to the userspace array. |
Vegard Nossum | ffecee4 | 2016-10-08 11:18:07 +0200 | [diff] [blame] | 2080 | * @nr_segs: Number of elements in userspace array. |
| 2081 | * @fast_segs: Number of elements in @iov. |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2082 | * @iovp: (input and output parameter) Pointer to pointer to (usually small |
Vegard Nossum | ffecee4 | 2016-10-08 11:18:07 +0200 | [diff] [blame] | 2083 | * on-stack) kernel array. |
| 2084 | * @i: Pointer to iterator that will be initialized on success. |
| 2085 | * |
| 2086 | * If the array pointed to by *@iov is large enough to hold all @nr_segs, |
| 2087 | * then this function places %NULL in *@iov on return. Otherwise, a new |
| 2088 | * array will be allocated and the result placed in *@iov. This means that |
| 2089 | * the caller may call kfree() on *@iov regardless of whether the small |
| 2090 | * on-stack array was used or not (and regardless of whether this function |
| 2091 | * returns an error or not). |
| 2092 | * |
Jens Axboe | 87e5e6d | 2019-05-14 16:02:22 -0600 | [diff] [blame] | 2093 | * Return: Negative error code on error, bytes imported on success |
Vegard Nossum | ffecee4 | 2016-10-08 11:18:07 +0200 | [diff] [blame] | 2094 | */ |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2095 | ssize_t import_iovec(int type, const struct iovec __user *uvec, |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 2096 | unsigned nr_segs, unsigned fast_segs, |
Christoph Hellwig | bfdc597 | 2020-09-25 06:51:40 +0200 | [diff] [blame] | 2097 | struct iovec **iovp, struct iov_iter *i) |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 2098 | { |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 2099 | return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i, |
| 2100 | in_compat_syscall()); |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 2101 | } |
| 2102 | EXPORT_SYMBOL(import_iovec); |
| 2103 | |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 2104 | int import_single_range(int rw, void __user *buf, size_t len, |
| 2105 | struct iovec *iov, struct iov_iter *i) |
| 2106 | { |
| 2107 | if (len > MAX_RW_COUNT) |
| 2108 | len = MAX_RW_COUNT; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2109 | if (unlikely(!access_ok(buf, len))) |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 2110 | return -EFAULT; |
| 2111 | |
| 2112 | iov->iov_base = buf; |
| 2113 | iov->iov_len = len; |
| 2114 | iov_iter_init(i, rw, iov, 1, len); |
| 2115 | return 0; |
| 2116 | } |
Al Viro | e126758 | 2015-12-06 20:38:56 -0500 | [diff] [blame] | 2117 | EXPORT_SYMBOL(import_single_range); |