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