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