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