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