1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/kernel.h> 3 #include <linux/errno.h> 4 #include <linux/fs.h> 5 #include <linux/file.h> 6 #include <linux/mm.h> 7 #include <linux/slab.h> 8 #include <linux/nospec.h> 9 #include <linux/hugetlb.h> 10 #include <linux/compat.h> 11 #include <linux/io_uring.h> 12 13 #include <uapi/linux/io_uring.h> 14 15 #include "io_uring.h" 16 #include "openclose.h" 17 #include "rsrc.h" 18 19 struct io_rsrc_update { 20 struct file *file; 21 u64 arg; 22 u32 nr_args; 23 u32 offset; 24 }; 25 26 static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); 27 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); 28 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, 29 struct io_mapped_ubuf **pimu, 30 struct page **last_hpage); 31 32 /* only define max */ 33 #define IORING_MAX_FIXED_FILES (1U << 20) 34 #define IORING_MAX_REG_BUFFERS (1U << 14) 35 36 int __io_account_mem(struct user_struct *user, unsigned long nr_pages) 37 { 38 unsigned long page_limit, cur_pages, new_pages; 39 40 if (!nr_pages) 41 return 0; 42 43 /* Don't allow more pages than we can safely lock */ 44 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 45 46 cur_pages = atomic_long_read(&user->locked_vm); 47 do { 48 new_pages = cur_pages + nr_pages; 49 if (new_pages > page_limit) 50 return -ENOMEM; 51 } while (!atomic_long_try_cmpxchg(&user->locked_vm, 52 &cur_pages, new_pages)); 53 return 0; 54 } 55 56 static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) 57 { 58 if (ctx->user) 59 __io_unaccount_mem(ctx->user, nr_pages); 60 61 if (ctx->mm_account) 62 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); 63 } 64 65 static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) 66 { 67 int ret; 68 69 if (ctx->user) { 70 ret = __io_account_mem(ctx->user, nr_pages); 71 if (ret) 72 return ret; 73 } 74 75 if (ctx->mm_account) 76 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); 77 78 return 0; 79 } 80 81 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, 82 void __user *arg, unsigned index) 83 { 84 struct iovec __user *src; 85 86 #ifdef CONFIG_COMPAT 87 if (ctx->compat) { 88 struct compat_iovec __user *ciovs; 89 struct compat_iovec ciov; 90 91 ciovs = (struct compat_iovec __user *) arg; 92 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) 93 return -EFAULT; 94 95 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); 96 dst->iov_len = ciov.iov_len; 97 return 0; 98 } 99 #endif 100 src = (struct iovec __user *) arg; 101 if (copy_from_user(dst, &src[index], sizeof(*dst))) 102 return -EFAULT; 103 return 0; 104 } 105 106 static int io_buffer_validate(struct iovec *iov) 107 { 108 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); 109 110 /* 111 * Don't impose further limits on the size and buffer 112 * constraints here, we'll -EINVAL later when IO is 113 * submitted if they are wrong. 114 */ 115 if (!iov->iov_base) 116 return iov->iov_len ? -EFAULT : 0; 117 if (!iov->iov_len) 118 return -EFAULT; 119 120 /* arbitrary limit, but we need something */ 121 if (iov->iov_len > SZ_1G) 122 return -EFAULT; 123 124 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) 125 return -EOVERFLOW; 126 127 return 0; 128 } 129 130 static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) 131 { 132 struct io_mapped_ubuf *imu = *slot; 133 unsigned int i; 134 135 if (imu != ctx->dummy_ubuf) { 136 for (i = 0; i < imu->nr_bvecs; i++) 137 unpin_user_page(imu->bvec[i].bv_page); 138 if (imu->acct_pages) 139 io_unaccount_mem(ctx, imu->acct_pages); 140 kvfree(imu); 141 } 142 *slot = NULL; 143 } 144 145 static void io_rsrc_put_work(struct io_rsrc_node *node) 146 { 147 struct io_rsrc_put *prsrc = &node->item; 148 149 if (prsrc->tag) 150 io_post_aux_cqe(node->ctx, prsrc->tag, 0, 0); 151 152 switch (node->type) { 153 case IORING_RSRC_FILE: 154 io_rsrc_file_put(node->ctx, prsrc); 155 break; 156 case IORING_RSRC_BUFFER: 157 io_rsrc_buf_put(node->ctx, prsrc); 158 break; 159 default: 160 WARN_ON_ONCE(1); 161 break; 162 } 163 } 164 165 void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *node) 166 { 167 if (!io_alloc_cache_put(&ctx->rsrc_node_cache, &node->cache)) 168 kfree(node); 169 } 170 171 void io_rsrc_node_ref_zero(struct io_rsrc_node *node) 172 __must_hold(&node->ctx->uring_lock) 173 { 174 struct io_ring_ctx *ctx = node->ctx; 175 176 while (!list_empty(&ctx->rsrc_ref_list)) { 177 node = list_first_entry(&ctx->rsrc_ref_list, 178 struct io_rsrc_node, node); 179 /* recycle ref nodes in order */ 180 if (node->refs) 181 break; 182 list_del(&node->node); 183 184 if (likely(!node->empty)) 185 io_rsrc_put_work(node); 186 io_rsrc_node_destroy(ctx, node); 187 } 188 if (list_empty(&ctx->rsrc_ref_list) && unlikely(ctx->rsrc_quiesce)) 189 wake_up_all(&ctx->rsrc_quiesce_wq); 190 } 191 192 struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) 193 { 194 struct io_rsrc_node *ref_node; 195 struct io_cache_entry *entry; 196 197 entry = io_alloc_cache_get(&ctx->rsrc_node_cache); 198 if (entry) { 199 ref_node = container_of(entry, struct io_rsrc_node, cache); 200 } else { 201 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); 202 if (!ref_node) 203 return NULL; 204 } 205 206 ref_node->ctx = ctx; 207 ref_node->empty = 0; 208 ref_node->refs = 1; 209 return ref_node; 210 } 211 212 __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, 213 struct io_ring_ctx *ctx) 214 { 215 struct io_rsrc_node *backup; 216 DEFINE_WAIT(we); 217 int ret; 218 219 /* As We may drop ->uring_lock, other task may have started quiesce */ 220 if (data->quiesce) 221 return -ENXIO; 222 223 backup = io_rsrc_node_alloc(ctx); 224 if (!backup) 225 return -ENOMEM; 226 ctx->rsrc_node->empty = true; 227 ctx->rsrc_node->type = -1; 228 list_add_tail(&ctx->rsrc_node->node, &ctx->rsrc_ref_list); 229 io_put_rsrc_node(ctx, ctx->rsrc_node); 230 ctx->rsrc_node = backup; 231 232 if (list_empty(&ctx->rsrc_ref_list)) 233 return 0; 234 235 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) { 236 atomic_set(&ctx->cq_wait_nr, 1); 237 smp_mb(); 238 } 239 240 ctx->rsrc_quiesce++; 241 data->quiesce = true; 242 do { 243 prepare_to_wait(&ctx->rsrc_quiesce_wq, &we, TASK_INTERRUPTIBLE); 244 mutex_unlock(&ctx->uring_lock); 245 246 ret = io_run_task_work_sig(ctx); 247 if (ret < 0) { 248 mutex_lock(&ctx->uring_lock); 249 if (list_empty(&ctx->rsrc_ref_list)) 250 ret = 0; 251 break; 252 } 253 254 schedule(); 255 __set_current_state(TASK_RUNNING); 256 mutex_lock(&ctx->uring_lock); 257 ret = 0; 258 } while (!list_empty(&ctx->rsrc_ref_list)); 259 260 finish_wait(&ctx->rsrc_quiesce_wq, &we); 261 data->quiesce = false; 262 ctx->rsrc_quiesce--; 263 264 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) { 265 atomic_set(&ctx->cq_wait_nr, 0); 266 smp_mb(); 267 } 268 return ret; 269 } 270 271 static void io_free_page_table(void **table, size_t size) 272 { 273 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); 274 275 for (i = 0; i < nr_tables; i++) 276 kfree(table[i]); 277 kfree(table); 278 } 279 280 static void io_rsrc_data_free(struct io_rsrc_data *data) 281 { 282 size_t size = data->nr * sizeof(data->tags[0][0]); 283 284 if (data->tags) 285 io_free_page_table((void **)data->tags, size); 286 kfree(data); 287 } 288 289 static __cold void **io_alloc_page_table(size_t size) 290 { 291 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); 292 size_t init_size = size; 293 void **table; 294 295 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); 296 if (!table) 297 return NULL; 298 299 for (i = 0; i < nr_tables; i++) { 300 unsigned int this_size = min_t(size_t, size, PAGE_SIZE); 301 302 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); 303 if (!table[i]) { 304 io_free_page_table(table, init_size); 305 return NULL; 306 } 307 size -= this_size; 308 } 309 return table; 310 } 311 312 __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, int type, 313 u64 __user *utags, 314 unsigned nr, struct io_rsrc_data **pdata) 315 { 316 struct io_rsrc_data *data; 317 int ret = 0; 318 unsigned i; 319 320 data = kzalloc(sizeof(*data), GFP_KERNEL); 321 if (!data) 322 return -ENOMEM; 323 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); 324 if (!data->tags) { 325 kfree(data); 326 return -ENOMEM; 327 } 328 329 data->nr = nr; 330 data->ctx = ctx; 331 data->rsrc_type = type; 332 if (utags) { 333 ret = -EFAULT; 334 for (i = 0; i < nr; i++) { 335 u64 *tag_slot = io_get_tag_slot(data, i); 336 337 if (copy_from_user(tag_slot, &utags[i], 338 sizeof(*tag_slot))) 339 goto fail; 340 } 341 } 342 *pdata = data; 343 return 0; 344 fail: 345 io_rsrc_data_free(data); 346 return ret; 347 } 348 349 static int __io_sqe_files_update(struct io_ring_ctx *ctx, 350 struct io_uring_rsrc_update2 *up, 351 unsigned nr_args) 352 { 353 u64 __user *tags = u64_to_user_ptr(up->tags); 354 __s32 __user *fds = u64_to_user_ptr(up->data); 355 struct io_rsrc_data *data = ctx->file_data; 356 struct io_fixed_file *file_slot; 357 struct file *file; 358 int fd, i, err = 0; 359 unsigned int done; 360 361 if (!ctx->file_data) 362 return -ENXIO; 363 if (up->offset + nr_args > ctx->nr_user_files) 364 return -EINVAL; 365 366 for (done = 0; done < nr_args; done++) { 367 u64 tag = 0; 368 369 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || 370 copy_from_user(&fd, &fds[done], sizeof(fd))) { 371 err = -EFAULT; 372 break; 373 } 374 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { 375 err = -EINVAL; 376 break; 377 } 378 if (fd == IORING_REGISTER_FILES_SKIP) 379 continue; 380 381 i = array_index_nospec(up->offset + done, ctx->nr_user_files); 382 file_slot = io_fixed_file_slot(&ctx->file_table, i); 383 384 if (file_slot->file_ptr) { 385 file = (struct file *)(file_slot->file_ptr & FFS_MASK); 386 err = io_queue_rsrc_removal(data, i, file); 387 if (err) 388 break; 389 file_slot->file_ptr = 0; 390 io_file_bitmap_clear(&ctx->file_table, i); 391 } 392 if (fd != -1) { 393 file = fget(fd); 394 if (!file) { 395 err = -EBADF; 396 break; 397 } 398 /* 399 * Don't allow io_uring instances to be registered. If 400 * UNIX isn't enabled, then this causes a reference 401 * cycle and this instance can never get freed. If UNIX 402 * is enabled we'll handle it just fine, but there's 403 * still no point in allowing a ring fd as it doesn't 404 * support regular read/write anyway. 405 */ 406 if (io_is_uring_fops(file)) { 407 fput(file); 408 err = -EBADF; 409 break; 410 } 411 err = io_scm_file_account(ctx, file); 412 if (err) { 413 fput(file); 414 break; 415 } 416 *io_get_tag_slot(data, i) = tag; 417 io_fixed_file_set(file_slot, file); 418 io_file_bitmap_set(&ctx->file_table, i); 419 } 420 } 421 return done ? done : err; 422 } 423 424 static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, 425 struct io_uring_rsrc_update2 *up, 426 unsigned int nr_args) 427 { 428 u64 __user *tags = u64_to_user_ptr(up->tags); 429 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); 430 struct page *last_hpage = NULL; 431 __u32 done; 432 int i, err; 433 434 if (!ctx->buf_data) 435 return -ENXIO; 436 if (up->offset + nr_args > ctx->nr_user_bufs) 437 return -EINVAL; 438 439 for (done = 0; done < nr_args; done++) { 440 struct io_mapped_ubuf *imu; 441 u64 tag = 0; 442 443 err = io_copy_iov(ctx, &iov, iovs, done); 444 if (err) 445 break; 446 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { 447 err = -EFAULT; 448 break; 449 } 450 err = io_buffer_validate(&iov); 451 if (err) 452 break; 453 if (!iov.iov_base && tag) { 454 err = -EINVAL; 455 break; 456 } 457 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); 458 if (err) 459 break; 460 461 i = array_index_nospec(up->offset + done, ctx->nr_user_bufs); 462 if (ctx->user_bufs[i] != ctx->dummy_ubuf) { 463 err = io_queue_rsrc_removal(ctx->buf_data, i, 464 ctx->user_bufs[i]); 465 if (unlikely(err)) { 466 io_buffer_unmap(ctx, &imu); 467 break; 468 } 469 ctx->user_bufs[i] = ctx->dummy_ubuf; 470 } 471 472 ctx->user_bufs[i] = imu; 473 *io_get_tag_slot(ctx->buf_data, i) = tag; 474 } 475 return done ? done : err; 476 } 477 478 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, 479 struct io_uring_rsrc_update2 *up, 480 unsigned nr_args) 481 { 482 __u32 tmp; 483 484 lockdep_assert_held(&ctx->uring_lock); 485 486 if (check_add_overflow(up->offset, nr_args, &tmp)) 487 return -EOVERFLOW; 488 489 switch (type) { 490 case IORING_RSRC_FILE: 491 return __io_sqe_files_update(ctx, up, nr_args); 492 case IORING_RSRC_BUFFER: 493 return __io_sqe_buffers_update(ctx, up, nr_args); 494 } 495 return -EINVAL; 496 } 497 498 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, 499 unsigned nr_args) 500 { 501 struct io_uring_rsrc_update2 up; 502 503 if (!nr_args) 504 return -EINVAL; 505 memset(&up, 0, sizeof(up)); 506 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) 507 return -EFAULT; 508 if (up.resv || up.resv2) 509 return -EINVAL; 510 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); 511 } 512 513 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, 514 unsigned size, unsigned type) 515 { 516 struct io_uring_rsrc_update2 up; 517 518 if (size != sizeof(up)) 519 return -EINVAL; 520 if (copy_from_user(&up, arg, sizeof(up))) 521 return -EFAULT; 522 if (!up.nr || up.resv || up.resv2) 523 return -EINVAL; 524 return __io_register_rsrc_update(ctx, type, &up, up.nr); 525 } 526 527 __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, 528 unsigned int size, unsigned int type) 529 { 530 struct io_uring_rsrc_register rr; 531 532 /* keep it extendible */ 533 if (size != sizeof(rr)) 534 return -EINVAL; 535 536 memset(&rr, 0, sizeof(rr)); 537 if (copy_from_user(&rr, arg, size)) 538 return -EFAULT; 539 if (!rr.nr || rr.resv2) 540 return -EINVAL; 541 if (rr.flags & ~IORING_RSRC_REGISTER_SPARSE) 542 return -EINVAL; 543 544 switch (type) { 545 case IORING_RSRC_FILE: 546 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data) 547 break; 548 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), 549 rr.nr, u64_to_user_ptr(rr.tags)); 550 case IORING_RSRC_BUFFER: 551 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data) 552 break; 553 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), 554 rr.nr, u64_to_user_ptr(rr.tags)); 555 } 556 return -EINVAL; 557 } 558 559 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 560 { 561 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update); 562 563 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) 564 return -EINVAL; 565 if (sqe->rw_flags || sqe->splice_fd_in) 566 return -EINVAL; 567 568 up->offset = READ_ONCE(sqe->off); 569 up->nr_args = READ_ONCE(sqe->len); 570 if (!up->nr_args) 571 return -EINVAL; 572 up->arg = READ_ONCE(sqe->addr); 573 return 0; 574 } 575 576 static int io_files_update_with_index_alloc(struct io_kiocb *req, 577 unsigned int issue_flags) 578 { 579 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update); 580 __s32 __user *fds = u64_to_user_ptr(up->arg); 581 unsigned int done; 582 struct file *file; 583 int ret, fd; 584 585 if (!req->ctx->file_data) 586 return -ENXIO; 587 588 for (done = 0; done < up->nr_args; done++) { 589 if (copy_from_user(&fd, &fds[done], sizeof(fd))) { 590 ret = -EFAULT; 591 break; 592 } 593 594 file = fget(fd); 595 if (!file) { 596 ret = -EBADF; 597 break; 598 } 599 ret = io_fixed_fd_install(req, issue_flags, file, 600 IORING_FILE_INDEX_ALLOC); 601 if (ret < 0) 602 break; 603 if (copy_to_user(&fds[done], &ret, sizeof(ret))) { 604 __io_close_fixed(req->ctx, issue_flags, ret); 605 ret = -EFAULT; 606 break; 607 } 608 } 609 610 if (done) 611 return done; 612 return ret; 613 } 614 615 int io_files_update(struct io_kiocb *req, unsigned int issue_flags) 616 { 617 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update); 618 struct io_ring_ctx *ctx = req->ctx; 619 struct io_uring_rsrc_update2 up2; 620 int ret; 621 622 up2.offset = up->offset; 623 up2.data = up->arg; 624 up2.nr = 0; 625 up2.tags = 0; 626 up2.resv = 0; 627 up2.resv2 = 0; 628 629 if (up->offset == IORING_FILE_INDEX_ALLOC) { 630 ret = io_files_update_with_index_alloc(req, issue_flags); 631 } else { 632 io_ring_submit_lock(ctx, issue_flags); 633 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, 634 &up2, up->nr_args); 635 io_ring_submit_unlock(ctx, issue_flags); 636 } 637 638 if (ret < 0) 639 req_set_fail(req); 640 io_req_set_res(req, ret, 0); 641 return IOU_OK; 642 } 643 644 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, void *rsrc) 645 { 646 struct io_ring_ctx *ctx = data->ctx; 647 struct io_rsrc_node *node = ctx->rsrc_node; 648 u64 *tag_slot = io_get_tag_slot(data, idx); 649 650 ctx->rsrc_node = io_rsrc_node_alloc(ctx); 651 if (unlikely(!ctx->rsrc_node)) { 652 ctx->rsrc_node = node; 653 return -ENOMEM; 654 } 655 656 node->item.rsrc = rsrc; 657 node->type = data->rsrc_type; 658 node->item.tag = *tag_slot; 659 *tag_slot = 0; 660 list_add_tail(&node->node, &ctx->rsrc_ref_list); 661 io_put_rsrc_node(ctx, node); 662 return 0; 663 } 664 665 void __io_sqe_files_unregister(struct io_ring_ctx *ctx) 666 { 667 int i; 668 669 for (i = 0; i < ctx->nr_user_files; i++) { 670 struct file *file = io_file_from_index(&ctx->file_table, i); 671 672 /* skip scm accounted files, they'll be freed by ->ring_sock */ 673 if (!file || io_file_need_scm(file)) 674 continue; 675 io_file_bitmap_clear(&ctx->file_table, i); 676 fput(file); 677 } 678 679 #if defined(CONFIG_UNIX) 680 if (ctx->ring_sock) { 681 struct sock *sock = ctx->ring_sock->sk; 682 struct sk_buff *skb; 683 684 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) 685 kfree_skb(skb); 686 } 687 #endif 688 io_free_file_tables(&ctx->file_table); 689 io_file_table_set_alloc_range(ctx, 0, 0); 690 io_rsrc_data_free(ctx->file_data); 691 ctx->file_data = NULL; 692 ctx->nr_user_files = 0; 693 } 694 695 int io_sqe_files_unregister(struct io_ring_ctx *ctx) 696 { 697 unsigned nr = ctx->nr_user_files; 698 int ret; 699 700 if (!ctx->file_data) 701 return -ENXIO; 702 703 /* 704 * Quiesce may unlock ->uring_lock, and while it's not held 705 * prevent new requests using the table. 706 */ 707 ctx->nr_user_files = 0; 708 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); 709 ctx->nr_user_files = nr; 710 if (!ret) 711 __io_sqe_files_unregister(ctx); 712 return ret; 713 } 714 715 /* 716 * Ensure the UNIX gc is aware of our file set, so we are certain that 717 * the io_uring can be safely unregistered on process exit, even if we have 718 * loops in the file referencing. We account only files that can hold other 719 * files because otherwise they can't form a loop and so are not interesting 720 * for GC. 721 */ 722 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file) 723 { 724 #if defined(CONFIG_UNIX) 725 struct sock *sk = ctx->ring_sock->sk; 726 struct sk_buff_head *head = &sk->sk_receive_queue; 727 struct scm_fp_list *fpl; 728 struct sk_buff *skb; 729 730 if (likely(!io_file_need_scm(file))) 731 return 0; 732 733 /* 734 * See if we can merge this file into an existing skb SCM_RIGHTS 735 * file set. If there's no room, fall back to allocating a new skb 736 * and filling it in. 737 */ 738 spin_lock_irq(&head->lock); 739 skb = skb_peek(head); 740 if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD) 741 __skb_unlink(skb, head); 742 else 743 skb = NULL; 744 spin_unlock_irq(&head->lock); 745 746 if (!skb) { 747 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); 748 if (!fpl) 749 return -ENOMEM; 750 751 skb = alloc_skb(0, GFP_KERNEL); 752 if (!skb) { 753 kfree(fpl); 754 return -ENOMEM; 755 } 756 757 fpl->user = get_uid(current_user()); 758 fpl->max = SCM_MAX_FD; 759 fpl->count = 0; 760 761 UNIXCB(skb).fp = fpl; 762 skb->sk = sk; 763 skb->scm_io_uring = 1; 764 skb->destructor = unix_destruct_scm; 765 refcount_add(skb->truesize, &sk->sk_wmem_alloc); 766 } 767 768 fpl = UNIXCB(skb).fp; 769 fpl->fp[fpl->count++] = get_file(file); 770 unix_inflight(fpl->user, file); 771 skb_queue_head(head, skb); 772 fput(file); 773 #endif 774 return 0; 775 } 776 777 static __cold void io_rsrc_file_scm_put(struct io_ring_ctx *ctx, struct file *file) 778 { 779 #if defined(CONFIG_UNIX) 780 struct sock *sock = ctx->ring_sock->sk; 781 struct sk_buff_head list, *head = &sock->sk_receive_queue; 782 struct sk_buff *skb; 783 int i; 784 785 __skb_queue_head_init(&list); 786 787 /* 788 * Find the skb that holds this file in its SCM_RIGHTS. When found, 789 * remove this entry and rearrange the file array. 790 */ 791 skb = skb_dequeue(head); 792 while (skb) { 793 struct scm_fp_list *fp; 794 795 fp = UNIXCB(skb).fp; 796 for (i = 0; i < fp->count; i++) { 797 int left; 798 799 if (fp->fp[i] != file) 800 continue; 801 802 unix_notinflight(fp->user, fp->fp[i]); 803 left = fp->count - 1 - i; 804 if (left) { 805 memmove(&fp->fp[i], &fp->fp[i + 1], 806 left * sizeof(struct file *)); 807 } 808 fp->count--; 809 if (!fp->count) { 810 kfree_skb(skb); 811 skb = NULL; 812 } else { 813 __skb_queue_tail(&list, skb); 814 } 815 fput(file); 816 file = NULL; 817 break; 818 } 819 820 if (!file) 821 break; 822 823 __skb_queue_tail(&list, skb); 824 825 skb = skb_dequeue(head); 826 } 827 828 if (skb_peek(&list)) { 829 spin_lock_irq(&head->lock); 830 while ((skb = __skb_dequeue(&list)) != NULL) 831 __skb_queue_tail(head, skb); 832 spin_unlock_irq(&head->lock); 833 } 834 #endif 835 } 836 837 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) 838 { 839 struct file *file = prsrc->file; 840 841 if (likely(!io_file_need_scm(file))) 842 fput(file); 843 else 844 io_rsrc_file_scm_put(ctx, file); 845 } 846 847 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, 848 unsigned nr_args, u64 __user *tags) 849 { 850 __s32 __user *fds = (__s32 __user *) arg; 851 struct file *file; 852 int fd, ret; 853 unsigned i; 854 855 if (ctx->file_data) 856 return -EBUSY; 857 if (!nr_args) 858 return -EINVAL; 859 if (nr_args > IORING_MAX_FIXED_FILES) 860 return -EMFILE; 861 if (nr_args > rlimit(RLIMIT_NOFILE)) 862 return -EMFILE; 863 ret = io_rsrc_data_alloc(ctx, IORING_RSRC_FILE, tags, nr_args, 864 &ctx->file_data); 865 if (ret) 866 return ret; 867 868 if (!io_alloc_file_tables(&ctx->file_table, nr_args)) { 869 io_rsrc_data_free(ctx->file_data); 870 ctx->file_data = NULL; 871 return -ENOMEM; 872 } 873 874 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { 875 struct io_fixed_file *file_slot; 876 877 if (fds && copy_from_user(&fd, &fds[i], sizeof(fd))) { 878 ret = -EFAULT; 879 goto fail; 880 } 881 /* allow sparse sets */ 882 if (!fds || fd == -1) { 883 ret = -EINVAL; 884 if (unlikely(*io_get_tag_slot(ctx->file_data, i))) 885 goto fail; 886 continue; 887 } 888 889 file = fget(fd); 890 ret = -EBADF; 891 if (unlikely(!file)) 892 goto fail; 893 894 /* 895 * Don't allow io_uring instances to be registered. If UNIX 896 * isn't enabled, then this causes a reference cycle and this 897 * instance can never get freed. If UNIX is enabled we'll 898 * handle it just fine, but there's still no point in allowing 899 * a ring fd as it doesn't support regular read/write anyway. 900 */ 901 if (io_is_uring_fops(file)) { 902 fput(file); 903 goto fail; 904 } 905 ret = io_scm_file_account(ctx, file); 906 if (ret) { 907 fput(file); 908 goto fail; 909 } 910 file_slot = io_fixed_file_slot(&ctx->file_table, i); 911 io_fixed_file_set(file_slot, file); 912 io_file_bitmap_set(&ctx->file_table, i); 913 } 914 915 /* default it to the whole table */ 916 io_file_table_set_alloc_range(ctx, 0, ctx->nr_user_files); 917 return 0; 918 fail: 919 __io_sqe_files_unregister(ctx); 920 return ret; 921 } 922 923 static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) 924 { 925 io_buffer_unmap(ctx, &prsrc->buf); 926 prsrc->buf = NULL; 927 } 928 929 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) 930 { 931 unsigned int i; 932 933 for (i = 0; i < ctx->nr_user_bufs; i++) 934 io_buffer_unmap(ctx, &ctx->user_bufs[i]); 935 kfree(ctx->user_bufs); 936 io_rsrc_data_free(ctx->buf_data); 937 ctx->user_bufs = NULL; 938 ctx->buf_data = NULL; 939 ctx->nr_user_bufs = 0; 940 } 941 942 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) 943 { 944 unsigned nr = ctx->nr_user_bufs; 945 int ret; 946 947 if (!ctx->buf_data) 948 return -ENXIO; 949 950 /* 951 * Quiesce may unlock ->uring_lock, and while it's not held 952 * prevent new requests using the table. 953 */ 954 ctx->nr_user_bufs = 0; 955 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); 956 ctx->nr_user_bufs = nr; 957 if (!ret) 958 __io_sqe_buffers_unregister(ctx); 959 return ret; 960 } 961 962 /* 963 * Not super efficient, but this is just a registration time. And we do cache 964 * the last compound head, so generally we'll only do a full search if we don't 965 * match that one. 966 * 967 * We check if the given compound head page has already been accounted, to 968 * avoid double accounting it. This allows us to account the full size of the 969 * page, not just the constituent pages of a huge page. 970 */ 971 static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, 972 int nr_pages, struct page *hpage) 973 { 974 int i, j; 975 976 /* check current page array */ 977 for (i = 0; i < nr_pages; i++) { 978 if (!PageCompound(pages[i])) 979 continue; 980 if (compound_head(pages[i]) == hpage) 981 return true; 982 } 983 984 /* check previously registered pages */ 985 for (i = 0; i < ctx->nr_user_bufs; i++) { 986 struct io_mapped_ubuf *imu = ctx->user_bufs[i]; 987 988 for (j = 0; j < imu->nr_bvecs; j++) { 989 if (!PageCompound(imu->bvec[j].bv_page)) 990 continue; 991 if (compound_head(imu->bvec[j].bv_page) == hpage) 992 return true; 993 } 994 } 995 996 return false; 997 } 998 999 static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, 1000 int nr_pages, struct io_mapped_ubuf *imu, 1001 struct page **last_hpage) 1002 { 1003 int i, ret; 1004 1005 imu->acct_pages = 0; 1006 for (i = 0; i < nr_pages; i++) { 1007 if (!PageCompound(pages[i])) { 1008 imu->acct_pages++; 1009 } else { 1010 struct page *hpage; 1011 1012 hpage = compound_head(pages[i]); 1013 if (hpage == *last_hpage) 1014 continue; 1015 *last_hpage = hpage; 1016 if (headpage_already_acct(ctx, pages, i, hpage)) 1017 continue; 1018 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; 1019 } 1020 } 1021 1022 if (!imu->acct_pages) 1023 return 0; 1024 1025 ret = io_account_mem(ctx, imu->acct_pages); 1026 if (ret) 1027 imu->acct_pages = 0; 1028 return ret; 1029 } 1030 1031 struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages) 1032 { 1033 unsigned long start, end, nr_pages; 1034 struct vm_area_struct **vmas = NULL; 1035 struct page **pages = NULL; 1036 int i, pret, ret = -ENOMEM; 1037 1038 end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 1039 start = ubuf >> PAGE_SHIFT; 1040 nr_pages = end - start; 1041 1042 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); 1043 if (!pages) 1044 goto done; 1045 1046 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), 1047 GFP_KERNEL); 1048 if (!vmas) 1049 goto done; 1050 1051 ret = 0; 1052 mmap_read_lock(current->mm); 1053 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, 1054 pages, vmas); 1055 if (pret == nr_pages) { 1056 /* don't support file backed memory */ 1057 for (i = 0; i < nr_pages; i++) { 1058 struct vm_area_struct *vma = vmas[i]; 1059 1060 if (vma_is_shmem(vma)) 1061 continue; 1062 if (vma->vm_file && 1063 !is_file_hugepages(vma->vm_file)) { 1064 ret = -EOPNOTSUPP; 1065 break; 1066 } 1067 } 1068 *npages = nr_pages; 1069 } else { 1070 ret = pret < 0 ? pret : -EFAULT; 1071 } 1072 mmap_read_unlock(current->mm); 1073 if (ret) { 1074 /* 1075 * if we did partial map, or found file backed vmas, 1076 * release any pages we did get 1077 */ 1078 if (pret > 0) 1079 unpin_user_pages(pages, pret); 1080 goto done; 1081 } 1082 ret = 0; 1083 done: 1084 kvfree(vmas); 1085 if (ret < 0) { 1086 kvfree(pages); 1087 pages = ERR_PTR(ret); 1088 } 1089 return pages; 1090 } 1091 1092 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, 1093 struct io_mapped_ubuf **pimu, 1094 struct page **last_hpage) 1095 { 1096 struct io_mapped_ubuf *imu = NULL; 1097 struct page **pages = NULL; 1098 unsigned long off; 1099 size_t size; 1100 int ret, nr_pages, i; 1101 struct folio *folio = NULL; 1102 1103 *pimu = ctx->dummy_ubuf; 1104 if (!iov->iov_base) 1105 return 0; 1106 1107 ret = -ENOMEM; 1108 pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len, 1109 &nr_pages); 1110 if (IS_ERR(pages)) { 1111 ret = PTR_ERR(pages); 1112 pages = NULL; 1113 goto done; 1114 } 1115 1116 /* If it's a huge page, try to coalesce them into a single bvec entry */ 1117 if (nr_pages > 1) { 1118 folio = page_folio(pages[0]); 1119 for (i = 1; i < nr_pages; i++) { 1120 if (page_folio(pages[i]) != folio) { 1121 folio = NULL; 1122 break; 1123 } 1124 } 1125 if (folio) { 1126 /* 1127 * The pages are bound to the folio, it doesn't 1128 * actually unpin them but drops all but one reference, 1129 * which is usually put down by io_buffer_unmap(). 1130 * Note, needs a better helper. 1131 */ 1132 unpin_user_pages(&pages[1], nr_pages - 1); 1133 nr_pages = 1; 1134 } 1135 } 1136 1137 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); 1138 if (!imu) 1139 goto done; 1140 1141 ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage); 1142 if (ret) { 1143 unpin_user_pages(pages, nr_pages); 1144 goto done; 1145 } 1146 1147 off = (unsigned long) iov->iov_base & ~PAGE_MASK; 1148 size = iov->iov_len; 1149 /* store original address for later verification */ 1150 imu->ubuf = (unsigned long) iov->iov_base; 1151 imu->ubuf_end = imu->ubuf + iov->iov_len; 1152 imu->nr_bvecs = nr_pages; 1153 *pimu = imu; 1154 ret = 0; 1155 1156 if (folio) { 1157 bvec_set_page(&imu->bvec[0], pages[0], size, off); 1158 goto done; 1159 } 1160 for (i = 0; i < nr_pages; i++) { 1161 size_t vec_len; 1162 1163 vec_len = min_t(size_t, size, PAGE_SIZE - off); 1164 bvec_set_page(&imu->bvec[i], pages[i], vec_len, off); 1165 off = 0; 1166 size -= vec_len; 1167 } 1168 done: 1169 if (ret) 1170 kvfree(imu); 1171 kvfree(pages); 1172 return ret; 1173 } 1174 1175 static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) 1176 { 1177 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); 1178 return ctx->user_bufs ? 0 : -ENOMEM; 1179 } 1180 1181 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, 1182 unsigned int nr_args, u64 __user *tags) 1183 { 1184 struct page *last_hpage = NULL; 1185 struct io_rsrc_data *data; 1186 int i, ret; 1187 struct iovec iov; 1188 1189 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); 1190 1191 if (ctx->user_bufs) 1192 return -EBUSY; 1193 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) 1194 return -EINVAL; 1195 ret = io_rsrc_data_alloc(ctx, IORING_RSRC_BUFFER, tags, nr_args, &data); 1196 if (ret) 1197 return ret; 1198 ret = io_buffers_map_alloc(ctx, nr_args); 1199 if (ret) { 1200 io_rsrc_data_free(data); 1201 return ret; 1202 } 1203 1204 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { 1205 if (arg) { 1206 ret = io_copy_iov(ctx, &iov, arg, i); 1207 if (ret) 1208 break; 1209 ret = io_buffer_validate(&iov); 1210 if (ret) 1211 break; 1212 } else { 1213 memset(&iov, 0, sizeof(iov)); 1214 } 1215 1216 if (!iov.iov_base && *io_get_tag_slot(data, i)) { 1217 ret = -EINVAL; 1218 break; 1219 } 1220 1221 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], 1222 &last_hpage); 1223 if (ret) 1224 break; 1225 } 1226 1227 WARN_ON_ONCE(ctx->buf_data); 1228 1229 ctx->buf_data = data; 1230 if (ret) 1231 __io_sqe_buffers_unregister(ctx); 1232 return ret; 1233 } 1234 1235 int io_import_fixed(int ddir, struct iov_iter *iter, 1236 struct io_mapped_ubuf *imu, 1237 u64 buf_addr, size_t len) 1238 { 1239 u64 buf_end; 1240 size_t offset; 1241 1242 if (WARN_ON_ONCE(!imu)) 1243 return -EFAULT; 1244 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) 1245 return -EFAULT; 1246 /* not inside the mapped region */ 1247 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) 1248 return -EFAULT; 1249 1250 /* 1251 * Might not be a start of buffer, set size appropriately 1252 * and advance us to the beginning. 1253 */ 1254 offset = buf_addr - imu->ubuf; 1255 iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, offset + len); 1256 1257 if (offset) { 1258 /* 1259 * Don't use iov_iter_advance() here, as it's really slow for 1260 * using the latter parts of a big fixed buffer - it iterates 1261 * over each segment manually. We can cheat a bit here, because 1262 * we know that: 1263 * 1264 * 1) it's a BVEC iter, we set it up 1265 * 2) all bvecs are PAGE_SIZE in size, except potentially the 1266 * first and last bvec 1267 * 1268 * So just find our index, and adjust the iterator afterwards. 1269 * If the offset is within the first bvec (or the whole first 1270 * bvec, just use iov_iter_advance(). This makes it easier 1271 * since we can just skip the first segment, which may not 1272 * be PAGE_SIZE aligned. 1273 */ 1274 const struct bio_vec *bvec = imu->bvec; 1275 1276 if (offset <= bvec->bv_len) { 1277 /* 1278 * Note, huge pages buffers consists of one large 1279 * bvec entry and should always go this way. The other 1280 * branch doesn't expect non PAGE_SIZE'd chunks. 1281 */ 1282 iter->bvec = bvec; 1283 iter->nr_segs = bvec->bv_len; 1284 iter->count -= offset; 1285 iter->iov_offset = offset; 1286 } else { 1287 unsigned long seg_skip; 1288 1289 /* skip first vec */ 1290 offset -= bvec->bv_len; 1291 seg_skip = 1 + (offset >> PAGE_SHIFT); 1292 1293 iter->bvec = bvec + seg_skip; 1294 iter->nr_segs -= seg_skip; 1295 iter->count -= bvec->bv_len + offset; 1296 iter->iov_offset = offset & ~PAGE_MASK; 1297 } 1298 } 1299 1300 return 0; 1301 } 1302