1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Shared application/kernel submission and completion ring pairs, for 4 * supporting fast/efficient IO. 5 * 6 * A note on the read/write ordering memory barriers that are matched between 7 * the application and kernel side. 8 * 9 * After the application reads the CQ ring tail, it must use an 10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses 11 * before writing the tail (using smp_load_acquire to read the tail will 12 * do). It also needs a smp_mb() before updating CQ head (ordering the 13 * entry load(s) with the head store), pairing with an implicit barrier 14 * through a control-dependency in io_get_cqe (smp_store_release to 15 * store head will do). Failure to do so could lead to reading invalid 16 * CQ entries. 17 * 18 * Likewise, the application must use an appropriate smp_wmb() before 19 * writing the SQ tail (ordering SQ entry stores with the tail store), 20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release 21 * to store the tail will do). And it needs a barrier ordering the SQ 22 * head load before writing new SQ entries (smp_load_acquire to read 23 * head will do). 24 * 25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application 26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* 27 * updating the SQ tail; a full memory barrier smp_mb() is needed 28 * between. 29 * 30 * Also see the examples in the liburing library: 31 * 32 * git://git.kernel.org/pub/scm/linux/kernel/git/axboe/liburing.git 33 * 34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens 35 * from data shared between the kernel and application. This is done both 36 * for ordering purposes, but also to ensure that once a value is loaded from 37 * data that the application could potentially modify, it remains stable. 38 * 39 * Copyright (C) 2018-2019 Jens Axboe 40 * Copyright (c) 2018-2019 Christoph Hellwig 41 */ 42 #include <linux/kernel.h> 43 #include <linux/errno.h> 44 #include <linux/syscalls.h> 45 #include <linux/refcount.h> 46 #include <linux/bits.h> 47 48 #include <linux/sched/signal.h> 49 #include <linux/fs.h> 50 #include <linux/mm.h> 51 #include <linux/percpu.h> 52 #include <linux/slab.h> 53 #include <linux/anon_inodes.h> 54 #include <linux/uaccess.h> 55 #include <linux/nospec.h> 56 #include <linux/task_work.h> 57 #include <linux/io_uring.h> 58 #include <linux/io_uring/cmd.h> 59 #include <linux/audit.h> 60 #include <linux/security.h> 61 #include <linux/jump_label.h> 62 #include <linux/kcov.h> 63 64 #define CREATE_TRACE_POINTS 65 #include <trace/events/io_uring.h> 66 67 #include <uapi/linux/io_uring.h> 68 69 #include "io-wq.h" 70 71 #include "filetable.h" 72 #include "io_uring.h" 73 #include "opdef.h" 74 #include "refs.h" 75 #include "tctx.h" 76 #include "register.h" 77 #include "sqpoll.h" 78 #include "fdinfo.h" 79 #include "kbuf.h" 80 #include "rsrc.h" 81 #include "cancel.h" 82 #include "net.h" 83 #include "notif.h" 84 #include "waitid.h" 85 #include "futex.h" 86 #include "napi.h" 87 #include "uring_cmd.h" 88 #include "msg_ring.h" 89 #include "memmap.h" 90 #include "zcrx.h" 91 #include "bpf-ops.h" 92 93 #include "timeout.h" 94 #include "poll.h" 95 #include "rw.h" 96 #include "alloc_cache.h" 97 #include "eventfd.h" 98 #include "wait.h" 99 #include "bpf_filter.h" 100 #include "loop.h" 101 102 #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ 103 IOSQE_IO_HARDLINK | IOSQE_ASYNC) 104 105 #define IO_REQ_LINK_FLAGS (REQ_F_LINK | REQ_F_HARDLINK) 106 107 #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ 108 REQ_F_INFLIGHT | REQ_F_CREDS | REQ_F_ASYNC_DATA) 109 110 #define IO_REQ_CLEAN_SLOW_FLAGS (REQ_F_REFCOUNT | IO_REQ_LINK_FLAGS | \ 111 REQ_F_REISSUE | REQ_F_POLLED | \ 112 IO_REQ_CLEAN_FLAGS) 113 114 #define IO_TCTX_REFS_CACHE_NR (1U << 10) 115 116 #define IO_COMPL_BATCH 32 117 #define IO_REQ_ALLOC_BATCH 8 118 119 /* requests with any of those set should undergo io_disarm_next() */ 120 #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) 121 122 static void io_queue_sqe(struct io_kiocb *req, unsigned int extra_flags); 123 static void __io_req_caches_free(struct io_ring_ctx *ctx); 124 125 static __read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE(io_key_has_sqarray, HZ); 126 127 struct kmem_cache *req_cachep; 128 static struct workqueue_struct *iou_wq __ro_after_init; 129 130 static int __read_mostly sysctl_io_uring_disabled; 131 static int __read_mostly sysctl_io_uring_group = -1; 132 133 #ifdef CONFIG_SYSCTL 134 static const struct ctl_table kernel_io_uring_disabled_table[] = { 135 { 136 .procname = "io_uring_disabled", 137 .data = &sysctl_io_uring_disabled, 138 .maxlen = sizeof(sysctl_io_uring_disabled), 139 .mode = 0644, 140 .proc_handler = proc_dointvec_minmax, 141 .extra1 = SYSCTL_ZERO, 142 .extra2 = SYSCTL_TWO, 143 }, 144 { 145 .procname = "io_uring_group", 146 .data = &sysctl_io_uring_group, 147 .maxlen = sizeof(gid_t), 148 .mode = 0644, 149 .proc_handler = proc_dointvec, 150 }, 151 }; 152 #endif 153 154 static void io_poison_cached_req(struct io_kiocb *req) 155 { 156 req->ctx = IO_URING_PTR_POISON; 157 req->tctx = IO_URING_PTR_POISON; 158 req->file = IO_URING_PTR_POISON; 159 req->creds = IO_URING_PTR_POISON; 160 req->io_task_work.func = IO_URING_PTR_POISON; 161 req->apoll = IO_URING_PTR_POISON; 162 } 163 164 void io_poison_req(struct io_kiocb *req) 165 { 166 io_poison_cached_req(req); 167 req->async_data = IO_URING_PTR_POISON; 168 req->kbuf = IO_URING_PTR_POISON; 169 req->comp_list.next = IO_URING_PTR_POISON; 170 req->file_node = IO_URING_PTR_POISON; 171 req->link = IO_URING_PTR_POISON; 172 } 173 174 static inline void req_fail_link_node(struct io_kiocb *req, int res) 175 { 176 req_set_fail(req); 177 io_req_set_res(req, res, 0); 178 } 179 180 static inline void io_req_add_to_cache(struct io_kiocb *req, struct io_ring_ctx *ctx) 181 { 182 if (IS_ENABLED(CONFIG_KASAN)) 183 io_poison_cached_req(req); 184 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); 185 } 186 187 static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) 188 { 189 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); 190 191 complete(&ctx->ref_comp); 192 } 193 194 static int io_alloc_hash_table(struct io_hash_table *table, unsigned bits) 195 { 196 unsigned int hash_buckets; 197 int i; 198 199 do { 200 hash_buckets = 1U << bits; 201 table->hbs = kvmalloc_objs(table->hbs[0], hash_buckets, 202 GFP_KERNEL_ACCOUNT); 203 if (table->hbs) 204 break; 205 if (bits == 1) 206 return -ENOMEM; 207 bits--; 208 } while (1); 209 210 table->hash_bits = bits; 211 for (i = 0; i < hash_buckets; i++) 212 INIT_HLIST_HEAD(&table->hbs[i].list); 213 return 0; 214 } 215 216 static void io_free_alloc_caches(struct io_ring_ctx *ctx) 217 { 218 io_alloc_cache_free(&ctx->apoll_cache, kfree); 219 io_alloc_cache_free(&ctx->netmsg_cache, io_netmsg_cache_free); 220 io_alloc_cache_free(&ctx->rw_cache, io_rw_cache_free); 221 io_alloc_cache_free(&ctx->cmd_cache, io_cmd_cache_free); 222 io_futex_cache_free(ctx); 223 io_rsrc_cache_free(ctx); 224 } 225 226 static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) 227 { 228 struct io_ring_ctx *ctx; 229 int hash_bits; 230 bool ret; 231 232 ctx = kzalloc_obj(*ctx); 233 if (!ctx) 234 return NULL; 235 236 xa_init(&ctx->io_bl_xa); 237 xa_init(&ctx->hpage_acct); 238 239 /* 240 * Use 5 bits less than the max cq entries, that should give us around 241 * 32 entries per hash list if totally full and uniformly spread, but 242 * don't keep too many buckets to not overconsume memory. 243 */ 244 hash_bits = ilog2(p->cq_entries) - 5; 245 hash_bits = clamp(hash_bits, 1, 8); 246 if (io_alloc_hash_table(&ctx->cancel_table, hash_bits)) 247 goto err; 248 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, 249 0, GFP_KERNEL)) 250 goto err; 251 252 ctx->flags = p->flags; 253 ctx->hybrid_poll_time = LLONG_MAX; 254 atomic_set(&ctx->cq_wait_nr, IO_CQ_WAKE_INIT); 255 init_waitqueue_head(&ctx->sqo_sq_wait); 256 INIT_LIST_HEAD(&ctx->sqd_list); 257 INIT_LIST_HEAD(&ctx->cq_overflow_list); 258 ret = io_alloc_cache_init(&ctx->apoll_cache, IO_POLL_ALLOC_CACHE_MAX, 259 sizeof(struct async_poll), 0); 260 ret |= io_alloc_cache_init(&ctx->netmsg_cache, IO_ALLOC_CACHE_MAX, 261 sizeof(struct io_async_msghdr), 262 offsetof(struct io_async_msghdr, clear)); 263 ret |= io_alloc_cache_init(&ctx->rw_cache, IO_ALLOC_CACHE_MAX, 264 sizeof(struct io_async_rw), 265 offsetof(struct io_async_rw, clear)); 266 ret |= io_alloc_cache_init(&ctx->cmd_cache, IO_ALLOC_CACHE_MAX, 267 sizeof(struct io_async_cmd), 268 sizeof(struct io_async_cmd)); 269 ret |= io_futex_cache_init(ctx); 270 ret |= io_rsrc_cache_init(ctx); 271 if (ret) 272 goto free_ref; 273 init_completion(&ctx->ref_comp); 274 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); 275 mutex_init(&ctx->uring_lock); 276 init_waitqueue_head(&ctx->cq_wait); 277 init_waitqueue_head(&ctx->poll_wq); 278 spin_lock_init(&ctx->completion_lock); 279 raw_spin_lock_init(&ctx->timeout_lock); 280 INIT_LIST_HEAD(&ctx->iopoll_list); 281 INIT_LIST_HEAD(&ctx->defer_list); 282 INIT_LIST_HEAD(&ctx->timeout_list); 283 INIT_LIST_HEAD(&ctx->ltimeout_list); 284 mpscq_init(&ctx->work_list, &ctx->work_head); 285 INIT_LIST_HEAD(&ctx->tctx_list); 286 mutex_init(&ctx->tctx_lock); 287 ctx->submit_state.free_list.next = NULL; 288 INIT_HLIST_HEAD(&ctx->waitid_list); 289 xa_init_flags(&ctx->zcrx_ctxs, XA_FLAGS_ALLOC); 290 #ifdef CONFIG_FUTEX 291 INIT_HLIST_HEAD(&ctx->futex_list); 292 #endif 293 INIT_WQ_LIST(&ctx->submit_state.compl_reqs); 294 INIT_HLIST_HEAD(&ctx->cancelable_uring_cmd); 295 io_napi_init(ctx); 296 mutex_init(&ctx->mmap_lock); 297 ctx->kcov_handle = kcov_common_handle(); 298 299 return ctx; 300 301 free_ref: 302 percpu_ref_exit(&ctx->refs); 303 err: 304 io_free_alloc_caches(ctx); 305 kvfree(ctx->cancel_table.hbs); 306 xa_destroy(&ctx->io_bl_xa); 307 xa_destroy(&ctx->hpage_acct); 308 kfree(ctx); 309 return NULL; 310 } 311 312 static void io_clean_op(struct io_kiocb *req) 313 { 314 if (unlikely(req->flags & REQ_F_BUFFER_SELECTED)) 315 io_kbuf_drop_legacy(req); 316 317 if (req->flags & REQ_F_NEED_CLEANUP) { 318 const struct io_cold_def *def = &io_cold_defs[req->opcode]; 319 320 if (def->cleanup) 321 def->cleanup(req); 322 } 323 if (req->flags & REQ_F_INFLIGHT) 324 atomic_dec(&req->tctx->inflight_tracked); 325 if (req->flags & REQ_F_CREDS) 326 put_cred(req->creds); 327 if (req->flags & REQ_F_ASYNC_DATA) { 328 kfree(req->async_data); 329 req->async_data = NULL; 330 } 331 req->flags &= ~IO_REQ_CLEAN_FLAGS; 332 } 333 334 /* 335 * Mark the request as inflight, so that file cancelation will find it. 336 * Can be used if the file is an io_uring instance, or if the request itself 337 * relies on ->mm being alive for the duration of the request. 338 */ 339 inline void io_req_track_inflight(struct io_kiocb *req) 340 { 341 if (!(req->flags & REQ_F_INFLIGHT)) { 342 req->flags |= REQ_F_INFLIGHT; 343 atomic_inc(&req->tctx->inflight_tracked); 344 } 345 } 346 347 static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) 348 { 349 if (WARN_ON_ONCE(!req->link)) 350 return NULL; 351 352 req->flags &= ~REQ_F_ARM_LTIMEOUT; 353 req->flags |= REQ_F_LINK_TIMEOUT; 354 355 /* linked timeouts should have two refs once prep'ed */ 356 io_req_set_refcount(req); 357 __io_req_set_refcount(req->link, 2); 358 return req->link; 359 } 360 361 static void io_prep_async_work(struct io_kiocb *req) 362 { 363 const struct io_issue_def *def = &io_issue_defs[req->opcode]; 364 365 if (!(req->flags & REQ_F_CREDS)) { 366 req->flags |= REQ_F_CREDS; 367 req->creds = get_current_cred(); 368 } 369 370 req->work.list.next = NULL; 371 atomic_set(&req->work.flags, 0); 372 if (req->flags & REQ_F_FORCE_ASYNC) 373 atomic_or(IO_WQ_WORK_CONCURRENT, &req->work.flags); 374 375 if (req->file && !(req->flags & REQ_F_FIXED_FILE)) 376 req->flags |= io_file_get_flags(req->file); 377 378 if (req->file && (req->flags & REQ_F_ISREG)) { 379 bool should_hash = def->hash_reg_file; 380 381 /* don't serialize this request if the fs doesn't need it */ 382 if (should_hash && (req->file->f_flags & O_DIRECT) && 383 (req->file->f_op->fop_flags & FOP_DIO_PARALLEL_WRITE)) 384 should_hash = false; 385 if (should_hash || (req->flags & REQ_F_IOPOLL)) 386 io_wq_hash_work(&req->work, file_inode(req->file)); 387 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { 388 if (def->unbound_nonreg_file) 389 atomic_or(IO_WQ_WORK_UNBOUND, &req->work.flags); 390 } 391 } 392 393 static void io_prep_async_link(struct io_kiocb *req) 394 { 395 struct io_kiocb *cur; 396 397 if (req->flags & REQ_F_LINK_TIMEOUT) { 398 struct io_ring_ctx *ctx = req->ctx; 399 400 raw_spin_lock_irq(&ctx->timeout_lock); 401 io_for_each_link(cur, req) 402 io_prep_async_work(cur); 403 raw_spin_unlock_irq(&ctx->timeout_lock); 404 } else { 405 io_for_each_link(cur, req) 406 io_prep_async_work(cur); 407 } 408 } 409 410 void io_queue_iowq(struct io_kiocb *req) 411 { 412 struct io_uring_task *tctx = req->tctx; 413 414 BUG_ON(!tctx); 415 416 if ((current->flags & PF_KTHREAD) || !tctx->io_wq) { 417 io_req_task_queue_fail(req, -ECANCELED); 418 return; 419 } 420 421 /* init ->work of the whole link before punting */ 422 io_prep_async_link(req); 423 424 /* 425 * Not expected to happen, but if we do have a bug where this _can_ 426 * happen, catch it here and ensure the request is marked as 427 * canceled. That will make io-wq go through the usual work cancel 428 * procedure rather than attempt to run this request (or create a new 429 * worker for it). 430 */ 431 if (WARN_ON_ONCE(!same_thread_group(tctx->task, current))) 432 atomic_or(IO_WQ_WORK_CANCEL, &req->work.flags); 433 434 trace_io_uring_queue_async_work(req, io_wq_is_hashed(&req->work)); 435 io_wq_enqueue(tctx->io_wq, &req->work); 436 } 437 438 unsigned io_linked_nr(struct io_kiocb *req) 439 { 440 struct io_kiocb *tmp; 441 unsigned nr = 0; 442 443 io_for_each_link(tmp, req) 444 nr++; 445 return nr; 446 } 447 448 static __cold noinline void io_queue_deferred(struct io_ring_ctx *ctx) 449 { 450 bool drain_seen = false, first = true; 451 452 lockdep_assert_held(&ctx->uring_lock); 453 __io_req_caches_free(ctx); 454 455 while (!list_empty(&ctx->defer_list)) { 456 struct io_defer_entry *de = list_first_entry(&ctx->defer_list, 457 struct io_defer_entry, list); 458 459 drain_seen |= de->req->flags & REQ_F_IO_DRAIN; 460 if ((drain_seen || first) && ctx->nr_req_allocated != ctx->nr_drained) 461 return; 462 463 list_del_init(&de->list); 464 ctx->nr_drained -= io_linked_nr(de->req); 465 io_req_task_queue(de->req); 466 kfree(de); 467 first = false; 468 } 469 } 470 471 void __io_commit_cqring_flush(struct io_ring_ctx *ctx) 472 { 473 if (ctx->int_flags & IO_RING_F_POLL_ACTIVATED) 474 io_poll_wq_wake(ctx); 475 if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED) 476 io_flush_timeouts(ctx); 477 if (ctx->int_flags & IO_RING_F_HAS_EVFD) 478 io_eventfd_signal(ctx, true, false); 479 } 480 481 static inline void __io_cq_lock(struct io_ring_ctx *ctx) 482 { 483 if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) 484 spin_lock(&ctx->completion_lock); 485 } 486 487 static inline void io_cq_lock(struct io_ring_ctx *ctx) 488 __acquires(ctx->completion_lock) 489 { 490 spin_lock(&ctx->completion_lock); 491 } 492 493 static inline void __io_cq_unlock_post(struct io_ring_ctx *ctx) 494 { 495 io_commit_cqring(ctx); 496 if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)) { 497 if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) 498 spin_unlock(&ctx->completion_lock); 499 /* IOPOLL rings only need to wake up if it's also SQPOLL */ 500 if (!(ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL)) 501 io_cqring_wake(ctx); 502 } 503 io_commit_cqring_flush(ctx); 504 } 505 506 static void io_cq_unlock_post(struct io_ring_ctx *ctx) 507 __releases(ctx->completion_lock) 508 { 509 io_commit_cqring(ctx); 510 spin_unlock(&ctx->completion_lock); 511 io_cqring_wake(ctx); 512 io_commit_cqring_flush(ctx); 513 } 514 515 static void __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool dying) 516 { 517 lockdep_assert_held(&ctx->uring_lock); 518 519 /* don't abort if we're dying, entries must get freed */ 520 if (!dying && __io_cqring_events(ctx) == ctx->cq_entries) 521 return; 522 523 io_cq_lock(ctx); 524 while (!list_empty(&ctx->cq_overflow_list)) { 525 size_t cqe_size = sizeof(struct io_uring_cqe); 526 struct io_uring_cqe *cqe; 527 struct io_overflow_cqe *ocqe; 528 bool is_cqe32 = false; 529 530 ocqe = list_first_entry(&ctx->cq_overflow_list, 531 struct io_overflow_cqe, list); 532 if (ocqe->cqe.flags & IORING_CQE_F_32 || 533 ctx->flags & IORING_SETUP_CQE32) { 534 is_cqe32 = true; 535 cqe_size <<= 1; 536 } 537 if (ctx->flags & IORING_SETUP_CQE32) 538 is_cqe32 = false; 539 540 if (!dying) { 541 if (!io_get_cqe_overflow(ctx, &cqe, true, is_cqe32)) 542 break; 543 memcpy(cqe, &ocqe->cqe, cqe_size); 544 } 545 list_del(&ocqe->list); 546 kfree(ocqe); 547 548 /* 549 * For silly syzbot cases that deliberately overflow by huge 550 * amounts, check if we need to resched and drop and 551 * reacquire the locks if so. Nothing real would ever hit this. 552 * Ideally we'd have a non-posting unlock for this, but hard 553 * to care for a non-real case. 554 */ 555 if (need_resched()) { 556 ctx->cqe_sentinel = ctx->cqe_cached; 557 io_cq_unlock_post(ctx); 558 mutex_unlock(&ctx->uring_lock); 559 cond_resched(); 560 mutex_lock(&ctx->uring_lock); 561 io_cq_lock(ctx); 562 } 563 } 564 565 if (list_empty(&ctx->cq_overflow_list)) { 566 clear_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq); 567 atomic_andnot(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags); 568 } 569 io_cq_unlock_post(ctx); 570 } 571 572 static void io_cqring_overflow_kill(struct io_ring_ctx *ctx) 573 { 574 if (ctx->rings) 575 __io_cqring_overflow_flush(ctx, true); 576 } 577 578 void io_cqring_do_overflow_flush(struct io_ring_ctx *ctx) 579 { 580 mutex_lock(&ctx->uring_lock); 581 __io_cqring_overflow_flush(ctx, false); 582 mutex_unlock(&ctx->uring_lock); 583 } 584 585 void io_cqring_overflow_flush_locked(struct io_ring_ctx *ctx) 586 { 587 __io_cqring_overflow_flush(ctx, false); 588 } 589 590 /* must to be called somewhat shortly after putting a request */ 591 static inline void io_put_task(struct io_kiocb *req) 592 { 593 struct io_uring_task *tctx = req->tctx; 594 595 if (likely(tctx->task == current)) { 596 tctx->cached_refs++; 597 } else { 598 percpu_counter_sub(&tctx->inflight, 1); 599 if (unlikely(atomic_read(&tctx->in_cancel))) 600 wake_up(&tctx->wait); 601 put_task_struct(tctx->task); 602 } 603 } 604 605 void io_task_refs_refill(struct io_uring_task *tctx) 606 { 607 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; 608 609 percpu_counter_add(&tctx->inflight, refill); 610 refcount_add(refill, ¤t->usage); 611 tctx->cached_refs += refill; 612 } 613 614 __cold void io_uring_drop_tctx_refs(struct task_struct *task) 615 { 616 struct io_uring_task *tctx = task->io_uring; 617 unsigned int refs = tctx->cached_refs; 618 619 if (refs) { 620 tctx->cached_refs = 0; 621 percpu_counter_sub(&tctx->inflight, refs); 622 put_task_struct_many(task, refs); 623 } 624 } 625 626 static __cold bool io_cqring_add_overflow(struct io_ring_ctx *ctx, 627 struct io_overflow_cqe *ocqe) 628 { 629 lockdep_assert_held(&ctx->completion_lock); 630 631 if (!ocqe) { 632 struct io_rings *r = ctx->rings; 633 634 /* 635 * If we're in ring overflow flush mode, or in task cancel mode, 636 * or cannot allocate an overflow entry, then we need to drop it 637 * on the floor. 638 */ 639 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); 640 set_bit(IO_CHECK_CQ_DROPPED_BIT, &ctx->check_cq); 641 return false; 642 } 643 if (list_empty(&ctx->cq_overflow_list)) { 644 set_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq); 645 atomic_or(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags); 646 647 } 648 list_add_tail(&ocqe->list, &ctx->cq_overflow_list); 649 return true; 650 } 651 652 static struct io_overflow_cqe *io_alloc_ocqe(struct io_ring_ctx *ctx, 653 struct io_cqe *cqe, 654 struct io_big_cqe *big_cqe, gfp_t gfp) 655 { 656 struct io_overflow_cqe *ocqe; 657 size_t ocq_size = sizeof(struct io_overflow_cqe); 658 bool is_cqe32 = false; 659 660 if (cqe->flags & IORING_CQE_F_32 || ctx->flags & IORING_SETUP_CQE32) { 661 is_cqe32 = true; 662 ocq_size += sizeof(struct io_uring_cqe); 663 } 664 665 ocqe = kzalloc(ocq_size, gfp | __GFP_ACCOUNT); 666 trace_io_uring_cqe_overflow(ctx, cqe->user_data, cqe->res, cqe->flags, ocqe); 667 if (ocqe) { 668 ocqe->cqe.user_data = cqe->user_data; 669 ocqe->cqe.res = cqe->res; 670 ocqe->cqe.flags = cqe->flags; 671 if (is_cqe32 && big_cqe) { 672 ocqe->cqe.big_cqe[0] = big_cqe->extra1; 673 ocqe->cqe.big_cqe[1] = big_cqe->extra2; 674 } 675 } 676 if (big_cqe) 677 big_cqe->extra1 = big_cqe->extra2 = 0; 678 return ocqe; 679 } 680 681 /* 682 * Compute queued CQEs for free-space calculation, clamped to cq_entries. 683 */ 684 static unsigned int io_cqring_queued(struct io_ring_ctx *ctx) 685 { 686 struct io_rings *rings = io_get_rings(ctx); 687 int diff; 688 689 diff = (int)(ctx->cached_cq_tail - READ_ONCE(rings->cq.head)); 690 if (diff >= 0) 691 return min((unsigned int)diff, ctx->cq_entries); 692 return 0; 693 } 694 695 /* 696 * Fill an empty dummy CQE, in case alignment is off for posting a 32b CQE 697 * because the ring is a single 16b entry away from wrapping. 698 */ 699 static bool io_fill_nop_cqe(struct io_ring_ctx *ctx, unsigned int off) 700 { 701 if (io_cqring_queued(ctx) < ctx->cq_entries) { 702 struct io_uring_cqe *cqe = &ctx->rings->cqes[off]; 703 704 cqe->user_data = 0; 705 cqe->res = 0; 706 cqe->flags = IORING_CQE_F_SKIP; 707 ctx->cached_cq_tail++; 708 return true; 709 } 710 return false; 711 } 712 713 /* 714 * writes to the cq entry need to come after reading head; the 715 * control dependency is enough as we're using WRITE_ONCE to 716 * fill the cq entry 717 */ 718 bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow, bool cqe32) 719 { 720 struct io_rings *rings = ctx->rings; 721 unsigned int off = ctx->cached_cq_tail & (ctx->cq_entries - 1); 722 unsigned int free, len; 723 724 /* 725 * Posting into the CQ when there are pending overflowed CQEs may break 726 * ordering guarantees, which will affect links, F_MORE users and more. 727 * Force overflow the completion. 728 */ 729 if (!overflow && (ctx->check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT))) 730 return false; 731 732 /* 733 * Post dummy CQE if a 32b CQE is needed and there's only room for a 734 * 16b CQE before the ring wraps. 735 */ 736 if (cqe32 && off + 1 == ctx->cq_entries) { 737 if (!io_fill_nop_cqe(ctx, off)) 738 return false; 739 off = 0; 740 } 741 742 free = ctx->cq_entries - io_cqring_queued(ctx); 743 /* we need a contiguous range, limit based on the current array offset */ 744 len = min(free, ctx->cq_entries - off); 745 if (len < (cqe32 + 1)) 746 return false; 747 748 if (ctx->flags & IORING_SETUP_CQE32) { 749 off <<= 1; 750 len <<= 1; 751 } 752 753 ctx->cqe_cached = &rings->cqes[off]; 754 ctx->cqe_sentinel = ctx->cqe_cached + len; 755 return true; 756 } 757 758 static bool io_fill_cqe_aux32(struct io_ring_ctx *ctx, 759 struct io_uring_cqe src_cqe[2]) 760 { 761 struct io_uring_cqe *cqe; 762 763 if (WARN_ON_ONCE(!(ctx->flags & (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED)))) 764 return false; 765 if (unlikely(!io_get_cqe(ctx, &cqe, true))) 766 return false; 767 768 memcpy(cqe, src_cqe, 2 * sizeof(*cqe)); 769 trace_io_uring_complete(ctx, NULL, cqe); 770 return true; 771 } 772 773 static bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, s32 res, 774 u32 cflags) 775 { 776 bool cqe32 = cflags & IORING_CQE_F_32; 777 struct io_uring_cqe *cqe; 778 779 if (likely(io_get_cqe(ctx, &cqe, cqe32))) { 780 WRITE_ONCE(cqe->user_data, user_data); 781 WRITE_ONCE(cqe->res, res); 782 WRITE_ONCE(cqe->flags, cflags); 783 784 if (cqe32) { 785 WRITE_ONCE(cqe->big_cqe[0], 0); 786 WRITE_ONCE(cqe->big_cqe[1], 0); 787 } 788 789 trace_io_uring_complete(ctx, NULL, cqe); 790 return true; 791 } 792 return false; 793 } 794 795 static inline struct io_cqe io_init_cqe(u64 user_data, s32 res, u32 cflags) 796 { 797 return (struct io_cqe) { .user_data = user_data, .res = res, .flags = cflags }; 798 } 799 800 static __cold void io_cqe_overflow(struct io_ring_ctx *ctx, struct io_cqe *cqe, 801 struct io_big_cqe *big_cqe) 802 { 803 struct io_overflow_cqe *ocqe; 804 805 ocqe = io_alloc_ocqe(ctx, cqe, big_cqe, GFP_KERNEL); 806 spin_lock(&ctx->completion_lock); 807 io_cqring_add_overflow(ctx, ocqe); 808 spin_unlock(&ctx->completion_lock); 809 } 810 811 static __cold bool io_cqe_overflow_locked(struct io_ring_ctx *ctx, 812 struct io_cqe *cqe, 813 struct io_big_cqe *big_cqe) 814 { 815 struct io_overflow_cqe *ocqe; 816 817 ocqe = io_alloc_ocqe(ctx, cqe, big_cqe, GFP_NOWAIT); 818 return io_cqring_add_overflow(ctx, ocqe); 819 } 820 821 bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags) 822 { 823 bool filled; 824 825 io_cq_lock(ctx); 826 filled = io_fill_cqe_aux(ctx, user_data, res, cflags); 827 if (unlikely(!filled)) { 828 struct io_cqe cqe = io_init_cqe(user_data, res, cflags); 829 830 filled = io_cqe_overflow_locked(ctx, &cqe, NULL); 831 } 832 io_cq_unlock_post(ctx); 833 return filled; 834 } 835 836 /* 837 * Must be called from inline task_work so we know a flush will happen later, 838 * and obviously with ctx->uring_lock held (tw always has that). 839 */ 840 void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags) 841 { 842 lockdep_assert_held(&ctx->uring_lock); 843 lockdep_assert(ctx->int_flags & IO_RING_F_LOCKLESS_CQ); 844 845 if (!io_fill_cqe_aux(ctx, user_data, res, cflags)) { 846 struct io_cqe cqe = io_init_cqe(user_data, res, cflags); 847 848 io_cqe_overflow(ctx, &cqe, NULL); 849 } 850 ctx->submit_state.cq_flush = true; 851 } 852 853 /* 854 * A helper for multishot requests posting additional CQEs. 855 * Should only be used from a task_work including IO_URING_F_MULTISHOT. 856 */ 857 bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags) 858 { 859 struct io_ring_ctx *ctx = req->ctx; 860 bool posted; 861 862 /* 863 * If multishot has already posted deferred completions, ensure that 864 * those are flushed first before posting this one. If not, CQEs 865 * could get reordered. 866 */ 867 if (!wq_list_empty(&ctx->submit_state.compl_reqs)) 868 __io_submit_flush_completions(ctx); 869 870 lockdep_assert(!io_wq_current_is_worker()); 871 lockdep_assert_held(&ctx->uring_lock); 872 873 if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) { 874 spin_lock(&ctx->completion_lock); 875 posted = io_fill_cqe_aux(ctx, req->cqe.user_data, res, cflags); 876 spin_unlock(&ctx->completion_lock); 877 } else { 878 posted = io_fill_cqe_aux(ctx, req->cqe.user_data, res, cflags); 879 } 880 881 ctx->submit_state.cq_flush = true; 882 return posted; 883 } 884 885 /* 886 * A helper for multishot requests posting additional CQEs. 887 * Should only be used from a task_work including IO_URING_F_MULTISHOT. 888 */ 889 bool io_req_post_cqe32(struct io_kiocb *req, struct io_uring_cqe cqe[2]) 890 { 891 struct io_ring_ctx *ctx = req->ctx; 892 bool posted; 893 894 lockdep_assert(!io_wq_current_is_worker()); 895 lockdep_assert_held(&ctx->uring_lock); 896 897 cqe[0].user_data = req->cqe.user_data; 898 if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) { 899 spin_lock(&ctx->completion_lock); 900 posted = io_fill_cqe_aux32(ctx, cqe); 901 spin_unlock(&ctx->completion_lock); 902 } else { 903 posted = io_fill_cqe_aux32(ctx, cqe); 904 } 905 906 ctx->submit_state.cq_flush = true; 907 return posted; 908 } 909 910 static void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags) 911 { 912 struct io_ring_ctx *ctx = req->ctx; 913 bool completed = true; 914 915 /* 916 * All execution paths but io-wq use the deferred completions by 917 * passing IO_URING_F_COMPLETE_DEFER and thus should not end up here. 918 */ 919 if (WARN_ON_ONCE(!(issue_flags & IO_URING_F_IOWQ))) 920 return; 921 922 /* 923 * Handle special CQ sync cases via task_work. DEFER_TASKRUN requires 924 * the submitter task context, IOPOLL protects with uring_lock. 925 */ 926 if ((ctx->int_flags & IO_RING_F_LOCKLESS_CQ) || (req->flags & REQ_F_REISSUE)) { 927 defer_complete: 928 req->io_task_work.func = io_req_task_complete; 929 io_req_task_work_add(req); 930 return; 931 } 932 933 io_cq_lock(ctx); 934 if (!(req->flags & REQ_F_CQE_SKIP)) 935 completed = io_fill_cqe_req(ctx, req); 936 io_cq_unlock_post(ctx); 937 938 if (!completed) 939 goto defer_complete; 940 941 /* 942 * We don't free the request here because we know it's called from 943 * io-wq only, which holds a reference, so it cannot be the last put. 944 */ 945 req_ref_put(req); 946 } 947 948 void io_req_defer_failed(struct io_kiocb *req, s32 res) 949 __must_hold(&ctx->uring_lock) 950 { 951 const struct io_cold_def *def = &io_cold_defs[req->opcode]; 952 953 lockdep_assert_held(&req->ctx->uring_lock); 954 955 req_set_fail(req); 956 io_req_set_res(req, res, io_put_kbuf(req, res, NULL)); 957 if (def->fail) 958 def->fail(req); 959 io_req_complete_defer(req); 960 } 961 962 /* 963 * A request might get retired back into the request caches even before opcode 964 * handlers and io_issue_sqe() are done with it, e.g. inline completion path. 965 * Because of that, io_alloc_req() should be called only under ->uring_lock 966 * and with extra caution to not get a request that is still worked on. 967 */ 968 __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) 969 __must_hold(&ctx->uring_lock) 970 { 971 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO; 972 void *reqs[IO_REQ_ALLOC_BATCH]; 973 int nr_reqs = ARRAY_SIZE(reqs); 974 975 /* 976 * Bulk alloc is all-or-nothing. If we fail to get a batch, retry a 977 * single allocation to be on the safe side. 978 */ 979 if (!kmem_cache_alloc_bulk(req_cachep, gfp, nr_reqs, reqs)) { 980 reqs[0] = kmem_cache_alloc(req_cachep, gfp); 981 if (!reqs[0]) 982 return false; 983 nr_reqs = 1; 984 } 985 986 percpu_ref_get_many(&ctx->refs, nr_reqs); 987 ctx->nr_req_allocated += nr_reqs; 988 989 while (nr_reqs--) 990 io_req_add_to_cache(reqs[nr_reqs], ctx); 991 return true; 992 } 993 994 __cold void io_free_req(struct io_kiocb *req) 995 { 996 /* refs were already put, restore them for io_req_task_complete() */ 997 req->flags &= ~REQ_F_REFCOUNT; 998 /* we only want to free it, don't post CQEs */ 999 req->flags |= REQ_F_CQE_SKIP; 1000 req->io_task_work.func = io_req_task_complete; 1001 io_req_task_work_add(req); 1002 } 1003 1004 static void __io_req_find_next_prep(struct io_kiocb *req) 1005 { 1006 struct io_ring_ctx *ctx = req->ctx; 1007 1008 spin_lock(&ctx->completion_lock); 1009 io_disarm_next(req); 1010 spin_unlock(&ctx->completion_lock); 1011 } 1012 1013 static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) 1014 { 1015 struct io_kiocb *nxt; 1016 1017 /* 1018 * If LINK is set, we have dependent requests in this chain. If we 1019 * didn't fail this request, queue the first one up, moving any other 1020 * dependencies to the next request. In case of failure, fail the rest 1021 * of the chain. 1022 */ 1023 if (unlikely(req->flags & IO_DISARM_MASK)) 1024 __io_req_find_next_prep(req); 1025 nxt = req->link; 1026 req->link = NULL; 1027 return nxt; 1028 } 1029 1030 static void io_req_task_cancel(struct io_tw_req tw_req, io_tw_token_t tw) 1031 { 1032 struct io_kiocb *req = tw_req.req; 1033 1034 io_tw_lock(req->ctx, tw); 1035 io_req_defer_failed(req, req->cqe.res); 1036 } 1037 1038 void io_req_task_submit(struct io_tw_req tw_req, io_tw_token_t tw) 1039 { 1040 struct io_kiocb *req = tw_req.req; 1041 struct io_ring_ctx *ctx = req->ctx; 1042 1043 io_tw_lock(ctx, tw); 1044 if (unlikely(tw.cancel)) 1045 io_req_defer_failed(req, -EFAULT); 1046 else if (req->flags & REQ_F_FORCE_ASYNC) 1047 io_queue_iowq(req); 1048 else 1049 io_queue_sqe(req, 0); 1050 } 1051 1052 void io_req_task_queue_fail(struct io_kiocb *req, int ret) 1053 { 1054 io_req_set_res(req, ret, 0); 1055 req->io_task_work.func = io_req_task_cancel; 1056 io_req_task_work_add(req); 1057 } 1058 1059 void io_req_task_queue(struct io_kiocb *req) 1060 { 1061 req->io_task_work.func = io_req_task_submit; 1062 io_req_task_work_add(req); 1063 } 1064 1065 void io_queue_next(struct io_kiocb *req) 1066 { 1067 struct io_kiocb *nxt = io_req_find_next(req); 1068 1069 if (nxt) 1070 io_req_task_queue(nxt); 1071 } 1072 1073 static inline void io_req_put_rsrc_nodes(struct io_kiocb *req) 1074 { 1075 struct io_ring_ctx *ctx = req->ctx; 1076 1077 if (req->file_node) { 1078 io_put_rsrc_node(ctx, req->file_node); 1079 req->file_node = NULL; 1080 } 1081 if (req->flags & REQ_F_BUF_NODE) 1082 io_put_rsrc_node(ctx, req->buf_node); 1083 } 1084 1085 static void io_free_batch_list(struct io_ring_ctx *ctx, 1086 struct io_wq_work_node *node) 1087 __must_hold(&ctx->uring_lock) 1088 { 1089 do { 1090 struct io_kiocb *req = container_of(node, struct io_kiocb, 1091 comp_list); 1092 1093 if (unlikely(req->flags & IO_REQ_CLEAN_SLOW_FLAGS)) { 1094 if (req->flags & REQ_F_REISSUE) { 1095 node = req->comp_list.next; 1096 req->flags &= ~REQ_F_REISSUE; 1097 io_queue_iowq(req); 1098 continue; 1099 } 1100 if (req->flags & REQ_F_REFCOUNT) { 1101 node = req->comp_list.next; 1102 if (!req_ref_put_and_test(req)) 1103 continue; 1104 } 1105 if ((req->flags & REQ_F_POLLED) && req->apoll) { 1106 struct async_poll *apoll = req->apoll; 1107 1108 if (apoll->double_poll) 1109 kfree(apoll->double_poll); 1110 io_cache_free(&ctx->apoll_cache, apoll); 1111 req->flags &= ~REQ_F_POLLED; 1112 } 1113 if (req->flags & IO_REQ_LINK_FLAGS) 1114 io_queue_next(req); 1115 if (unlikely(req->flags & IO_REQ_CLEAN_FLAGS)) 1116 io_clean_op(req); 1117 } 1118 io_put_file(req); 1119 io_req_put_rsrc_nodes(req); 1120 io_put_task(req); 1121 1122 node = req->comp_list.next; 1123 io_req_add_to_cache(req, ctx); 1124 } while (node); 1125 } 1126 1127 void __io_submit_flush_completions(struct io_ring_ctx *ctx) 1128 __must_hold(&ctx->uring_lock) 1129 { 1130 struct io_submit_state *state = &ctx->submit_state; 1131 struct io_wq_work_node *node; 1132 1133 __io_cq_lock(ctx); 1134 __wq_list_for_each(node, &state->compl_reqs) { 1135 struct io_kiocb *req = container_of(node, struct io_kiocb, 1136 comp_list); 1137 1138 /* 1139 * Requests marked with REQUEUE should not post a CQE, they 1140 * will go through the io-wq retry machinery and post one 1141 * later. 1142 */ 1143 if (!(req->flags & (REQ_F_CQE_SKIP | REQ_F_REISSUE)) && 1144 unlikely(!io_fill_cqe_req(ctx, req))) { 1145 if (ctx->int_flags & IO_RING_F_LOCKLESS_CQ) 1146 io_cqe_overflow(ctx, &req->cqe, &req->big_cqe); 1147 else 1148 io_cqe_overflow_locked(ctx, &req->cqe, &req->big_cqe); 1149 } 1150 } 1151 __io_cq_unlock_post(ctx); 1152 1153 if (!wq_list_empty(&state->compl_reqs)) { 1154 io_free_batch_list(ctx, state->compl_reqs.first); 1155 INIT_WQ_LIST(&state->compl_reqs); 1156 } 1157 1158 if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_ACTIVE)) 1159 io_queue_deferred(ctx); 1160 1161 ctx->submit_state.cq_flush = false; 1162 } 1163 1164 /* 1165 * We can't just wait for polled events to come to us, we have to actively 1166 * find and complete them. 1167 */ 1168 __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) 1169 { 1170 if (!(ctx->flags & IORING_SETUP_IOPOLL)) 1171 return; 1172 1173 mutex_lock(&ctx->uring_lock); 1174 while (!list_empty(&ctx->iopoll_list)) { 1175 /* let it sleep and repeat later if can't complete a request */ 1176 if (io_do_iopoll(ctx, true) == 0) 1177 break; 1178 /* 1179 * Ensure we allow local-to-the-cpu processing to take place, 1180 * in this case we need to ensure that we reap all events. 1181 * Also let task_work, etc. to progress by releasing the mutex 1182 */ 1183 if (need_resched()) { 1184 mutex_unlock(&ctx->uring_lock); 1185 cond_resched(); 1186 mutex_lock(&ctx->uring_lock); 1187 } 1188 } 1189 mutex_unlock(&ctx->uring_lock); 1190 1191 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) 1192 io_cancel_local_task_work(ctx); 1193 } 1194 1195 static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned int min_events) 1196 { 1197 unsigned long check_cq; 1198 1199 min_events = min(min_events, ctx->cq_entries); 1200 1201 lockdep_assert_held(&ctx->uring_lock); 1202 1203 if (!io_allowed_run_tw(ctx)) 1204 return -EEXIST; 1205 1206 check_cq = READ_ONCE(ctx->check_cq); 1207 if (unlikely(check_cq)) { 1208 if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT)) 1209 __io_cqring_overflow_flush(ctx, false); 1210 /* 1211 * Similarly do not spin if we have not informed the user of any 1212 * dropped CQE. 1213 */ 1214 if (check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT)) 1215 return -EBADR; 1216 } 1217 /* 1218 * Don't enter poll loop if we already have events pending. 1219 * If we do, we can potentially be spinning for commands that 1220 * already triggered a CQE (eg in error). 1221 */ 1222 if (io_cqring_events(ctx)) 1223 return 0; 1224 1225 do { 1226 int ret = 0; 1227 1228 /* 1229 * If a submit got punted to a workqueue, we can have the 1230 * application entering polling for a command before it gets 1231 * issued. That app will hold the uring_lock for the duration 1232 * of the poll right here, so we need to take a breather every 1233 * now and then to ensure that the issue has a chance to add 1234 * the poll to the issued list. Otherwise we can spin here 1235 * forever, while the workqueue is stuck trying to acquire the 1236 * very same mutex. 1237 */ 1238 if (list_empty(&ctx->iopoll_list) || io_task_work_pending(ctx)) { 1239 (void) io_run_local_work_locked(ctx, min_events); 1240 1241 if (task_work_pending(current) || list_empty(&ctx->iopoll_list)) { 1242 mutex_unlock(&ctx->uring_lock); 1243 io_run_task_work(); 1244 mutex_lock(&ctx->uring_lock); 1245 } 1246 /* some requests don't go through iopoll_list */ 1247 if (list_empty(&ctx->iopoll_list)) 1248 break; 1249 } 1250 ret = io_do_iopoll(ctx, !min_events); 1251 if (unlikely(ret < 0)) 1252 return ret; 1253 1254 if (task_sigpending(current)) 1255 return -EINTR; 1256 if (need_resched()) 1257 break; 1258 } while (io_cqring_events(ctx) < min_events); 1259 1260 return 0; 1261 } 1262 1263 void io_req_task_complete(struct io_tw_req tw_req, io_tw_token_t tw) 1264 { 1265 io_req_complete_defer(tw_req.req); 1266 } 1267 1268 /* 1269 * After the iocb has been issued, it's safe to be found on the poll list. 1270 * Adding the kiocb to the list AFTER submission ensures that we don't 1271 * find it from a io_do_iopoll() thread before the issuer is done 1272 * accessing the kiocb cookie. 1273 */ 1274 static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) 1275 { 1276 struct io_ring_ctx *ctx = req->ctx; 1277 const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; 1278 1279 /* workqueue context doesn't hold uring_lock, grab it now */ 1280 if (unlikely(needs_lock)) 1281 mutex_lock(&ctx->uring_lock); 1282 1283 /* 1284 * Track whether we have multiple files in our lists. This will impact 1285 * how we do polling eventually, not spinning if we're on potentially 1286 * different devices. 1287 */ 1288 if (list_empty(&ctx->iopoll_list)) { 1289 ctx->poll_multi_queue = false; 1290 } else if (!ctx->poll_multi_queue) { 1291 struct io_kiocb *list_req; 1292 1293 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, iopoll_node); 1294 if (list_req->file != req->file) 1295 ctx->poll_multi_queue = true; 1296 } 1297 1298 list_add_tail(&req->iopoll_node, &ctx->iopoll_list); 1299 1300 if (unlikely(needs_lock)) { 1301 /* 1302 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle 1303 * in sq thread task context or in io worker task context. If 1304 * current task context is sq thread, we don't need to check 1305 * whether should wake up sq thread. 1306 */ 1307 if ((ctx->flags & IORING_SETUP_SQPOLL) && 1308 wq_has_sleeper(&ctx->sq_data->wait)) 1309 wake_up(&ctx->sq_data->wait); 1310 1311 mutex_unlock(&ctx->uring_lock); 1312 } 1313 } 1314 1315 io_req_flags_t io_file_get_flags(struct file *file) 1316 { 1317 io_req_flags_t res = 0; 1318 1319 BUILD_BUG_ON(REQ_F_ISREG_BIT != REQ_F_SUPPORT_NOWAIT_BIT + 1); 1320 1321 if (S_ISREG(file_inode(file)->i_mode)) 1322 res |= REQ_F_ISREG; 1323 if ((file->f_flags & O_NONBLOCK) || (file->f_mode & FMODE_NOWAIT)) 1324 res |= REQ_F_SUPPORT_NOWAIT; 1325 return res; 1326 } 1327 1328 static __cold void io_drain_req(struct io_kiocb *req) 1329 __must_hold(&ctx->uring_lock) 1330 { 1331 struct io_ring_ctx *ctx = req->ctx; 1332 bool drain = req->flags & IOSQE_IO_DRAIN; 1333 struct io_defer_entry *de; 1334 1335 de = kmalloc_obj(*de, GFP_KERNEL_ACCOUNT); 1336 if (!de) { 1337 io_req_defer_failed(req, -ENOMEM); 1338 return; 1339 } 1340 1341 io_prep_async_link(req); 1342 trace_io_uring_defer(req); 1343 de->req = req; 1344 1345 ctx->nr_drained += io_linked_nr(req); 1346 list_add_tail(&de->list, &ctx->defer_list); 1347 io_queue_deferred(ctx); 1348 if (!drain && list_empty(&ctx->defer_list)) 1349 ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE; 1350 } 1351 1352 static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def, 1353 unsigned int issue_flags) 1354 { 1355 if (req->file || !def->needs_file) 1356 return true; 1357 1358 if (req->flags & REQ_F_FIXED_FILE) 1359 req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags); 1360 else 1361 req->file = io_file_get_normal(req, req->cqe.fd); 1362 1363 return !!req->file; 1364 } 1365 1366 #define REQ_ISSUE_SLOW_FLAGS (REQ_F_CREDS | REQ_F_ARM_LTIMEOUT) 1367 1368 static inline int __io_issue_sqe(struct io_kiocb *req, 1369 unsigned int issue_flags, 1370 const struct io_issue_def *def) 1371 { 1372 const struct cred *creds = NULL; 1373 struct io_kiocb *link = NULL; 1374 int ret; 1375 1376 if (unlikely(req->flags & REQ_ISSUE_SLOW_FLAGS)) { 1377 if ((req->flags & REQ_F_CREDS) && req->creds != current_cred()) 1378 creds = override_creds(req->creds); 1379 if (req->flags & REQ_F_ARM_LTIMEOUT) 1380 link = __io_prep_linked_timeout(req); 1381 } 1382 1383 if (!def->audit_skip) 1384 audit_uring_entry(req->opcode); 1385 1386 ret = def->issue(req, issue_flags); 1387 1388 if (!def->audit_skip) 1389 audit_uring_exit(!ret, ret); 1390 1391 if (unlikely(creds || link)) { 1392 if (creds) 1393 revert_creds(creds); 1394 if (link) 1395 io_queue_linked_timeout(link); 1396 } 1397 1398 return ret; 1399 } 1400 1401 static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) 1402 { 1403 const struct io_issue_def *def = &io_issue_defs[req->opcode]; 1404 int ret; 1405 1406 if (unlikely(!io_assign_file(req, def, issue_flags))) 1407 return -EBADF; 1408 1409 ret = __io_issue_sqe(req, issue_flags, def); 1410 1411 if (ret == IOU_COMPLETE) { 1412 if (issue_flags & IO_URING_F_COMPLETE_DEFER) 1413 io_req_complete_defer(req); 1414 else 1415 io_req_complete_post(req, issue_flags); 1416 1417 return 0; 1418 } 1419 1420 if (ret == IOU_ISSUE_SKIP_COMPLETE) { 1421 ret = 0; 1422 1423 if (req->flags & REQ_F_IOPOLL) 1424 io_iopoll_req_issued(req, issue_flags); 1425 } 1426 return ret; 1427 } 1428 1429 int io_poll_issue(struct io_kiocb *req, io_tw_token_t tw) 1430 { 1431 const unsigned int issue_flags = IO_URING_F_NONBLOCK | 1432 IO_URING_F_MULTISHOT | 1433 IO_URING_F_COMPLETE_DEFER; 1434 int ret; 1435 1436 io_tw_lock(req->ctx, tw); 1437 1438 WARN_ON_ONCE(!req->file); 1439 if (WARN_ON_ONCE(req->flags & REQ_F_IOPOLL)) 1440 return -EFAULT; 1441 1442 ret = __io_issue_sqe(req, issue_flags, &io_issue_defs[req->opcode]); 1443 1444 WARN_ON_ONCE(ret == IOU_ISSUE_SKIP_COMPLETE); 1445 return ret; 1446 } 1447 1448 struct io_wq_work *io_wq_free_work(struct io_wq_work *work) 1449 { 1450 struct io_kiocb *req = container_of(work, struct io_kiocb, work); 1451 struct io_kiocb *nxt = NULL; 1452 1453 if (req_ref_put_and_test_atomic(req)) { 1454 if (req->flags & IO_REQ_LINK_FLAGS) { 1455 struct io_ring_ctx *ctx = req->ctx; 1456 1457 mutex_lock(&ctx->uring_lock); 1458 nxt = io_req_find_next(req); 1459 mutex_unlock(&ctx->uring_lock); 1460 } 1461 io_free_req(req); 1462 } 1463 return nxt ? &nxt->work : NULL; 1464 } 1465 1466 void io_wq_submit_work(struct io_wq_work *work) 1467 { 1468 struct io_kiocb *req = container_of(work, struct io_kiocb, work); 1469 const struct io_issue_def *def = &io_issue_defs[req->opcode]; 1470 unsigned int issue_flags = IO_URING_F_UNLOCKED | IO_URING_F_IOWQ; 1471 bool needs_poll = false; 1472 int ret = 0, err = -ECANCELED; 1473 1474 /* one will be dropped by io_wq_free_work() after returning to io-wq */ 1475 if (!(req->flags & REQ_F_REFCOUNT)) 1476 __io_req_set_refcount(req, 2); 1477 else 1478 req_ref_get(req); 1479 1480 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ 1481 if (atomic_read(&work->flags) & IO_WQ_WORK_CANCEL) { 1482 fail: 1483 io_req_task_queue_fail(req, err); 1484 return; 1485 } 1486 if (!io_assign_file(req, def, issue_flags)) { 1487 err = -EBADF; 1488 atomic_or(IO_WQ_WORK_CANCEL, &work->flags); 1489 goto fail; 1490 } 1491 1492 /* 1493 * If DEFER_TASKRUN is set, it's only allowed to post CQEs from the 1494 * submitter task context. Final request completions are handed to the 1495 * right context, however this is not the case of auxiliary CQEs, 1496 * which is the main mean of operation for multishot requests. 1497 * Don't allow any multishot execution from io-wq. It's more restrictive 1498 * than necessary and also cleaner. 1499 */ 1500 if (req->flags & (REQ_F_MULTISHOT|REQ_F_APOLL_MULTISHOT)) { 1501 err = -EBADFD; 1502 if (!io_file_can_poll(req)) 1503 goto fail; 1504 if (req->file->f_flags & O_NONBLOCK || 1505 req->file->f_mode & FMODE_NOWAIT) { 1506 err = -ECANCELED; 1507 if (io_arm_poll_handler(req, issue_flags) != IO_APOLL_OK) 1508 goto fail; 1509 return; 1510 } else { 1511 req->flags &= ~(REQ_F_APOLL_MULTISHOT|REQ_F_MULTISHOT); 1512 } 1513 } 1514 1515 if (req->flags & REQ_F_FORCE_ASYNC) { 1516 bool opcode_poll = def->pollin || def->pollout; 1517 1518 if (opcode_poll && io_file_can_poll(req)) { 1519 needs_poll = true; 1520 issue_flags |= IO_URING_F_NONBLOCK; 1521 } 1522 } 1523 1524 do { 1525 ret = io_issue_sqe(req, issue_flags); 1526 if (ret != -EAGAIN) 1527 break; 1528 1529 /* 1530 * If REQ_F_NOWAIT is set, then don't wait or retry with 1531 * poll. -EAGAIN is final for that case. 1532 */ 1533 if (req->flags & REQ_F_NOWAIT) 1534 break; 1535 1536 /* 1537 * We can get EAGAIN for iopolled IO even though we're 1538 * forcing a sync submission from here, since we can't 1539 * wait for request slots on the block side. 1540 */ 1541 if (!needs_poll) { 1542 if (!(req->flags & REQ_F_IOPOLL)) 1543 break; 1544 if (io_wq_worker_stopped()) 1545 break; 1546 cond_resched(); 1547 continue; 1548 } 1549 1550 if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK) 1551 return; 1552 /* aborted or ready, in either case retry blocking */ 1553 needs_poll = false; 1554 issue_flags &= ~IO_URING_F_NONBLOCK; 1555 } while (1); 1556 1557 /* avoid locking problems by failing it from a clean context */ 1558 if (ret) 1559 io_req_task_queue_fail(req, ret); 1560 } 1561 1562 inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, 1563 unsigned int issue_flags) 1564 { 1565 struct io_ring_ctx *ctx = req->ctx; 1566 struct io_rsrc_node *node; 1567 struct file *file = NULL; 1568 1569 io_ring_submit_lock(ctx, issue_flags); 1570 node = io_rsrc_node_lookup(&ctx->file_table.data, fd); 1571 if (node) { 1572 node->refs++; 1573 req->file_node = node; 1574 req->flags |= io_slot_flags(node); 1575 file = io_slot_file(node); 1576 } 1577 io_ring_submit_unlock(ctx, issue_flags); 1578 return file; 1579 } 1580 1581 struct file *io_file_get_normal(struct io_kiocb *req, int fd) 1582 { 1583 struct file *file = fget(fd); 1584 1585 trace_io_uring_file_get(req, fd); 1586 1587 /* we don't allow fixed io_uring files */ 1588 if (file && io_is_uring_fops(file)) 1589 io_req_track_inflight(req); 1590 return file; 1591 } 1592 1593 static int io_req_sqe_copy(struct io_kiocb *req, unsigned int issue_flags) 1594 { 1595 const struct io_cold_def *def = &io_cold_defs[req->opcode]; 1596 1597 if (req->flags & REQ_F_SQE_COPIED) 1598 return 0; 1599 req->flags |= REQ_F_SQE_COPIED; 1600 if (!def->sqe_copy) 1601 return 0; 1602 if (WARN_ON_ONCE(!(issue_flags & IO_URING_F_INLINE))) 1603 return -EFAULT; 1604 def->sqe_copy(req); 1605 return 0; 1606 } 1607 1608 static void io_queue_async(struct io_kiocb *req, unsigned int issue_flags, int ret) 1609 __must_hold(&req->ctx->uring_lock) 1610 { 1611 if (ret != -EAGAIN || (req->flags & REQ_F_NOWAIT)) { 1612 fail: 1613 io_req_defer_failed(req, ret); 1614 return; 1615 } 1616 1617 ret = io_req_sqe_copy(req, issue_flags); 1618 if (unlikely(ret)) 1619 goto fail; 1620 1621 switch (io_arm_poll_handler(req, 0)) { 1622 case IO_APOLL_READY: 1623 io_req_task_queue(req); 1624 break; 1625 case IO_APOLL_ABORTED: 1626 io_queue_iowq(req); 1627 break; 1628 case IO_APOLL_OK: 1629 break; 1630 } 1631 } 1632 1633 static inline void io_queue_sqe(struct io_kiocb *req, unsigned int extra_flags) 1634 __must_hold(&req->ctx->uring_lock) 1635 { 1636 unsigned int issue_flags = IO_URING_F_NONBLOCK | 1637 IO_URING_F_COMPLETE_DEFER | extra_flags; 1638 int ret; 1639 1640 ret = io_issue_sqe(req, issue_flags); 1641 1642 /* 1643 * We async punt it if the file wasn't marked NOWAIT, or if the file 1644 * doesn't support non-blocking read/write attempts 1645 */ 1646 if (unlikely(ret)) 1647 io_queue_async(req, issue_flags, ret); 1648 } 1649 1650 static void io_queue_sqe_fallback(struct io_kiocb *req) 1651 __must_hold(&req->ctx->uring_lock) 1652 { 1653 if (unlikely(req->flags & REQ_F_FAIL)) { 1654 /* 1655 * We don't submit, fail them all, for that replace hardlinks 1656 * with normal links. Extra REQ_F_LINK is tolerated. 1657 */ 1658 req->flags &= ~REQ_F_HARDLINK; 1659 req->flags |= REQ_F_LINK; 1660 io_req_defer_failed(req, req->cqe.res); 1661 } else { 1662 /* can't fail with IO_URING_F_INLINE */ 1663 io_req_sqe_copy(req, IO_URING_F_INLINE); 1664 if (unlikely(req->ctx->int_flags & IO_RING_F_DRAIN_ACTIVE)) 1665 io_drain_req(req); 1666 else 1667 io_queue_iowq(req); 1668 } 1669 } 1670 1671 /* 1672 * Check SQE restrictions (opcode and flags). 1673 * 1674 * Returns 'true' if SQE is allowed, 'false' otherwise. 1675 */ 1676 static inline bool io_check_restriction(struct io_ring_ctx *ctx, 1677 struct io_kiocb *req, 1678 unsigned int sqe_flags) 1679 { 1680 if (!(ctx->int_flags & IO_RING_F_OP_RESTRICTED)) 1681 return true; 1682 if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) 1683 return false; 1684 1685 if ((sqe_flags & ctx->restrictions.sqe_flags_required) != 1686 ctx->restrictions.sqe_flags_required) 1687 return false; 1688 1689 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | 1690 ctx->restrictions.sqe_flags_required)) 1691 return false; 1692 1693 return true; 1694 } 1695 1696 static void io_init_drain(struct io_ring_ctx *ctx) 1697 { 1698 struct io_kiocb *head = ctx->submit_state.link.head; 1699 1700 ctx->int_flags |= IO_RING_F_DRAIN_ACTIVE; 1701 if (head) { 1702 /* 1703 * If we need to drain a request in the middle of a link, drain 1704 * the head request and the next request/link after the current 1705 * link. Considering sequential execution of links, 1706 * REQ_F_IO_DRAIN will be maintained for every request of our 1707 * link. 1708 */ 1709 head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; 1710 ctx->int_flags |= IO_RING_F_DRAIN_NEXT; 1711 } 1712 } 1713 1714 static __cold int io_init_fail_req(struct io_kiocb *req, int err) 1715 { 1716 /* ensure per-opcode data is cleared if we fail before prep */ 1717 memset(&req->cmd.data, 0, sizeof(req->cmd.data)); 1718 return err; 1719 } 1720 1721 static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, 1722 const struct io_uring_sqe *sqe, unsigned int *left) 1723 __must_hold(&ctx->uring_lock) 1724 { 1725 const struct io_issue_def *def; 1726 unsigned int sqe_flags; 1727 int personality; 1728 1729 req->ctx = ctx; 1730 req->opcode = READ_ONCE(sqe->opcode); 1731 /* same numerical values with corresponding REQ_F_*, safe to copy */ 1732 sqe_flags = READ_ONCE(sqe->flags); 1733 req->flags = (__force io_req_flags_t) sqe_flags; 1734 req->cqe.user_data = READ_ONCE(sqe->user_data); 1735 req->file = NULL; 1736 req->tctx = current->io_uring; 1737 req->cancel_seq_set = false; 1738 req->async_data = NULL; 1739 1740 if (unlikely(req->opcode >= IORING_OP_LAST)) { 1741 req->opcode = 0; 1742 return io_init_fail_req(req, -EINVAL); 1743 } 1744 req->opcode = array_index_nospec(req->opcode, IORING_OP_LAST); 1745 1746 def = &io_issue_defs[req->opcode]; 1747 if (def->is_128 && !(ctx->flags & IORING_SETUP_SQE128)) { 1748 /* 1749 * A 128b op on a non-128b SQ requires mixed SQE support as 1750 * well as 2 contiguous entries. 1751 */ 1752 if (!(ctx->flags & IORING_SETUP_SQE_MIXED) || *left < 2 || 1753 (unsigned)(sqe - ctx->sq_sqes) >= ctx->sq_entries - 1) 1754 return io_init_fail_req(req, -EINVAL); 1755 /* 1756 * A 128b operation on a mixed SQ uses two entries, so we have 1757 * to increment the head and cached refs, and decrement what's 1758 * left. 1759 */ 1760 current->io_uring->cached_refs++; 1761 ctx->cached_sq_head++; 1762 (*left)--; 1763 } 1764 1765 if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { 1766 /* enforce forwards compatibility on users */ 1767 if (sqe_flags & ~SQE_VALID_FLAGS) 1768 return io_init_fail_req(req, -EINVAL); 1769 if (sqe_flags & IOSQE_BUFFER_SELECT) { 1770 if (!def->buffer_select) 1771 return io_init_fail_req(req, -EOPNOTSUPP); 1772 req->buf_index = READ_ONCE(sqe->buf_group); 1773 } 1774 if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) 1775 ctx->int_flags |= IO_RING_F_DRAIN_DISABLED; 1776 if (sqe_flags & IOSQE_IO_DRAIN) { 1777 if (ctx->int_flags & IO_RING_F_DRAIN_DISABLED) 1778 return io_init_fail_req(req, -EOPNOTSUPP); 1779 io_init_drain(ctx); 1780 } 1781 } 1782 if (unlikely(ctx->int_flags & (IO_RING_F_OP_RESTRICTED | IO_RING_F_DRAIN_ACTIVE | IO_RING_F_DRAIN_NEXT))) { 1783 if (!io_check_restriction(ctx, req, sqe_flags)) 1784 return io_init_fail_req(req, -EACCES); 1785 /* knock it to the slow queue path, will be drained there */ 1786 if (ctx->int_flags & IO_RING_F_DRAIN_ACTIVE) 1787 req->flags |= REQ_F_FORCE_ASYNC; 1788 /* if there is no link, we're at "next" request and need to drain */ 1789 if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_NEXT) && !ctx->submit_state.link.head) { 1790 ctx->int_flags &= ~IO_RING_F_DRAIN_NEXT; 1791 ctx->int_flags |= IO_RING_F_DRAIN_ACTIVE; 1792 req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; 1793 } 1794 } 1795 1796 if (!def->ioprio && sqe->ioprio) 1797 return io_init_fail_req(req, -EINVAL); 1798 if (!def->iopoll && (ctx->flags & IORING_SETUP_IOPOLL)) 1799 return io_init_fail_req(req, -EINVAL); 1800 1801 if (def->needs_file) { 1802 struct io_submit_state *state = &ctx->submit_state; 1803 1804 req->cqe.fd = READ_ONCE(sqe->fd); 1805 1806 /* 1807 * Plug now if we have more than 2 IO left after this, and the 1808 * target is potentially a read/write to block based storage. 1809 */ 1810 if (state->need_plug && def->plug) { 1811 state->plug_started = true; 1812 state->need_plug = false; 1813 blk_start_plug_nr_ios(&state->plug, state->submit_nr); 1814 } 1815 } 1816 1817 personality = READ_ONCE(sqe->personality); 1818 if (personality) { 1819 int ret; 1820 1821 req->creds = xa_load(&ctx->personalities, personality); 1822 if (!req->creds) 1823 return io_init_fail_req(req, -EINVAL); 1824 get_cred(req->creds); 1825 ret = security_uring_override_creds(req->creds); 1826 if (ret) { 1827 put_cred(req->creds); 1828 return io_init_fail_req(req, ret); 1829 } 1830 req->flags |= REQ_F_CREDS; 1831 } 1832 1833 return def->prep(req, sqe); 1834 } 1835 1836 static __cold int io_submit_fail_init(const struct io_uring_sqe *sqe, 1837 struct io_kiocb *req, int ret) 1838 { 1839 struct io_ring_ctx *ctx = req->ctx; 1840 struct io_submit_link *link = &ctx->submit_state.link; 1841 struct io_kiocb *head = link->head; 1842 1843 trace_io_uring_req_failed(sqe, req, ret); 1844 1845 /* 1846 * Avoid breaking links in the middle as it renders links with SQPOLL 1847 * unusable. Instead of failing eagerly, continue assembling the link if 1848 * applicable and mark the head with REQ_F_FAIL. The link flushing code 1849 * should find the flag and handle the rest. 1850 */ 1851 req_fail_link_node(req, ret); 1852 if (head && !(head->flags & REQ_F_FAIL)) 1853 req_fail_link_node(head, -ECANCELED); 1854 1855 if (!(req->flags & IO_REQ_LINK_FLAGS)) { 1856 if (head) { 1857 link->last->link = req; 1858 link->head = NULL; 1859 req = head; 1860 } 1861 io_queue_sqe_fallback(req); 1862 return ret; 1863 } 1864 1865 if (head) 1866 link->last->link = req; 1867 else 1868 link->head = req; 1869 link->last = req; 1870 return 0; 1871 } 1872 1873 static inline int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, 1874 const struct io_uring_sqe *sqe, unsigned int *left) 1875 __must_hold(&ctx->uring_lock) 1876 { 1877 struct io_submit_link *link = &ctx->submit_state.link; 1878 int ret; 1879 1880 ret = io_init_req(ctx, req, sqe, left); 1881 if (unlikely(ret)) 1882 return io_submit_fail_init(sqe, req, ret); 1883 1884 if (unlikely(ctx->bpf_filters)) { 1885 ret = io_uring_run_bpf_filters(ctx->bpf_filters, req); 1886 if (ret) 1887 return io_submit_fail_init(sqe, req, ret); 1888 } 1889 1890 trace_io_uring_submit_req(req); 1891 1892 /* 1893 * If we already have a head request, queue this one for async 1894 * submittal once the head completes. If we don't have a head but 1895 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be 1896 * submitted sync once the chain is complete. If none of those 1897 * conditions are true (normal request), then just queue it. 1898 */ 1899 if (unlikely(link->head)) { 1900 trace_io_uring_link(req, link->last); 1901 io_req_sqe_copy(req, IO_URING_F_INLINE); 1902 link->last->link = req; 1903 link->last = req; 1904 1905 if (req->flags & IO_REQ_LINK_FLAGS) 1906 return 0; 1907 /* last request of the link, flush it */ 1908 req = link->head; 1909 link->head = NULL; 1910 if (req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)) 1911 goto fallback; 1912 1913 } else if (unlikely(req->flags & (IO_REQ_LINK_FLAGS | 1914 REQ_F_FORCE_ASYNC | REQ_F_FAIL))) { 1915 if (req->flags & IO_REQ_LINK_FLAGS) { 1916 link->head = req; 1917 link->last = req; 1918 } else { 1919 fallback: 1920 io_queue_sqe_fallback(req); 1921 } 1922 return 0; 1923 } 1924 1925 io_queue_sqe(req, IO_URING_F_INLINE); 1926 return 0; 1927 } 1928 1929 /* 1930 * Batched submission is done, ensure local IO is flushed out. 1931 */ 1932 static void io_submit_state_end(struct io_ring_ctx *ctx) 1933 { 1934 struct io_submit_state *state = &ctx->submit_state; 1935 1936 if (unlikely(state->link.head)) 1937 io_queue_sqe_fallback(state->link.head); 1938 /* flush only after queuing links as they can generate completions */ 1939 io_submit_flush_completions(ctx); 1940 if (state->plug_started) 1941 blk_finish_plug(&state->plug); 1942 } 1943 1944 /* 1945 * Start submission side cache. 1946 */ 1947 static void io_submit_state_start(struct io_submit_state *state, 1948 unsigned int max_ios) 1949 { 1950 state->plug_started = false; 1951 state->need_plug = max_ios > 2; 1952 state->submit_nr = max_ios; 1953 /* set only head, no need to init link_last in advance */ 1954 state->link.head = NULL; 1955 } 1956 1957 static void io_commit_sqring(struct io_ring_ctx *ctx) 1958 { 1959 struct io_rings *rings = ctx->rings; 1960 1961 if (ctx->flags & IORING_SETUP_SQ_REWIND) { 1962 ctx->cached_sq_head = 0; 1963 } else { 1964 /* 1965 * Ensure any loads from the SQEs are done at this point, 1966 * since once we write the new head, the application could 1967 * write new data to them. 1968 */ 1969 smp_store_release(&rings->sq.head, ctx->cached_sq_head); 1970 } 1971 } 1972 1973 /* 1974 * Fetch an sqe, if one is available. Note this returns a pointer to memory 1975 * that is mapped by userspace. This means that care needs to be taken to 1976 * ensure that reads are stable, as we cannot rely on userspace always 1977 * being a good citizen. If members of the sqe are validated and then later 1978 * used, it's important that those reads are done through READ_ONCE() to 1979 * prevent a re-load down the line. 1980 */ 1981 static bool io_get_sqe(struct io_ring_ctx *ctx, const struct io_uring_sqe **sqe) 1982 { 1983 unsigned mask = ctx->sq_entries - 1; 1984 unsigned head = ctx->cached_sq_head++ & mask; 1985 1986 if (static_branch_unlikely(&io_key_has_sqarray.key) && 1987 (!(ctx->flags & IORING_SETUP_NO_SQARRAY))) { 1988 head = READ_ONCE(ctx->sq_array[head]); 1989 if (unlikely(head >= ctx->sq_entries)) { 1990 WRITE_ONCE(ctx->rings->sq_dropped, 1991 READ_ONCE(ctx->rings->sq_dropped) + 1); 1992 return false; 1993 } 1994 head = array_index_nospec(head, ctx->sq_entries); 1995 } 1996 1997 /* 1998 * The cached sq head (or cq tail) serves two purposes: 1999 * 2000 * 1) allows us to batch the cost of updating the user visible 2001 * head updates. 2002 * 2) allows the kernel side to track the head on its own, even 2003 * though the application is the one updating it. 2004 */ 2005 2006 /* double index for 128-byte SQEs, twice as long */ 2007 if (ctx->flags & IORING_SETUP_SQE128) 2008 head <<= 1; 2009 *sqe = &ctx->sq_sqes[head]; 2010 return true; 2011 } 2012 2013 int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) 2014 __must_hold(&ctx->uring_lock) 2015 { 2016 unsigned int entries; 2017 unsigned int left; 2018 int ret; 2019 2020 if (ctx->flags & IORING_SETUP_SQ_REWIND) 2021 entries = ctx->sq_entries; 2022 else 2023 entries = __io_sqring_entries(ctx); 2024 2025 entries = min(nr, entries); 2026 if (unlikely(!entries)) 2027 return 0; 2028 2029 ret = left = entries; 2030 io_get_task_refs(left); 2031 io_submit_state_start(&ctx->submit_state, left); 2032 2033 do { 2034 const struct io_uring_sqe *sqe; 2035 struct io_kiocb *req; 2036 2037 if (unlikely(!io_alloc_req(ctx, &req))) 2038 break; 2039 if (unlikely(!io_get_sqe(ctx, &sqe))) { 2040 io_req_add_to_cache(req, ctx); 2041 break; 2042 } 2043 2044 /* 2045 * Continue submitting even for sqe failure if the 2046 * ring was setup with IORING_SETUP_SUBMIT_ALL 2047 */ 2048 if (unlikely(io_submit_sqe(ctx, req, sqe, &left)) && 2049 !(ctx->flags & IORING_SETUP_SUBMIT_ALL)) { 2050 left--; 2051 break; 2052 } 2053 } while (--left); 2054 2055 if (unlikely(left)) { 2056 ret -= left; 2057 /* try again if it submitted nothing and can't allocate a req */ 2058 if (!ret && io_req_cache_empty(ctx)) 2059 ret = -EAGAIN; 2060 current->io_uring->cached_refs += left; 2061 } 2062 2063 io_submit_state_end(ctx); 2064 /* Commit SQ ring head once we've consumed and submitted all SQEs */ 2065 io_commit_sqring(ctx); 2066 return ret; 2067 } 2068 2069 static void io_rings_free(struct io_ring_ctx *ctx) 2070 { 2071 io_free_region(ctx->user, &ctx->sq_region); 2072 io_free_region(ctx->user, &ctx->ring_region); 2073 ctx->rings = NULL; 2074 RCU_INIT_POINTER(ctx->rings_rcu, NULL); 2075 ctx->sq_sqes = NULL; 2076 } 2077 2078 static int rings_size(unsigned int flags, unsigned int sq_entries, 2079 unsigned int cq_entries, struct io_rings_layout *rl) 2080 { 2081 struct io_rings *rings; 2082 size_t sqe_size; 2083 size_t off; 2084 2085 if (flags & IORING_SETUP_CQE_MIXED) { 2086 if (cq_entries < 2) 2087 return -EOVERFLOW; 2088 } 2089 if (flags & IORING_SETUP_SQE_MIXED) { 2090 if (sq_entries < 2) 2091 return -EOVERFLOW; 2092 } 2093 2094 rl->sq_array_offset = SIZE_MAX; 2095 2096 sqe_size = sizeof(struct io_uring_sqe); 2097 if (flags & IORING_SETUP_SQE128) 2098 sqe_size *= 2; 2099 2100 rl->sq_size = array_size(sqe_size, sq_entries); 2101 if (rl->sq_size == SIZE_MAX) 2102 return -EOVERFLOW; 2103 2104 off = struct_size(rings, cqes, cq_entries); 2105 if (flags & IORING_SETUP_CQE32) 2106 off = size_mul(off, 2); 2107 if (off == SIZE_MAX) 2108 return -EOVERFLOW; 2109 2110 #ifdef CONFIG_SMP 2111 off = ALIGN(off, SMP_CACHE_BYTES); 2112 if (off == 0) 2113 return -EOVERFLOW; 2114 #endif 2115 2116 if (!(flags & IORING_SETUP_NO_SQARRAY)) { 2117 size_t sq_array_size; 2118 2119 rl->sq_array_offset = off; 2120 2121 sq_array_size = array_size(sizeof(u32), sq_entries); 2122 off = size_add(off, sq_array_size); 2123 if (off == SIZE_MAX) 2124 return -EOVERFLOW; 2125 } 2126 2127 rl->rings_size = off; 2128 return 0; 2129 } 2130 2131 static __cold void __io_req_caches_free(struct io_ring_ctx *ctx) 2132 { 2133 struct io_kiocb *req; 2134 int nr = 0; 2135 2136 while (!io_req_cache_empty(ctx)) { 2137 req = io_extract_req(ctx); 2138 io_poison_req(req); 2139 kmem_cache_free(req_cachep, req); 2140 nr++; 2141 } 2142 if (nr) { 2143 ctx->nr_req_allocated -= nr; 2144 percpu_ref_put_many(&ctx->refs, nr); 2145 } 2146 } 2147 2148 static __cold void io_req_caches_free(struct io_ring_ctx *ctx) 2149 { 2150 guard(mutex)(&ctx->uring_lock); 2151 __io_req_caches_free(ctx); 2152 } 2153 2154 static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) 2155 { 2156 io_unregister_bpf_ops(ctx); 2157 io_sq_thread_finish(ctx); 2158 2159 mutex_lock(&ctx->uring_lock); 2160 io_sqe_buffers_unregister(ctx); 2161 io_sqe_files_unregister(ctx); 2162 io_unregister_zcrx(ctx); 2163 io_cqring_overflow_kill(ctx); 2164 io_eventfd_unregister(ctx); 2165 io_free_alloc_caches(ctx); 2166 io_destroy_buffers(ctx); 2167 io_free_region(ctx->user, &ctx->param_region); 2168 mutex_unlock(&ctx->uring_lock); 2169 if (ctx->sq_creds) 2170 put_cred(ctx->sq_creds); 2171 if (ctx->submitter_task) 2172 put_task_struct(ctx->submitter_task); 2173 2174 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); 2175 2176 if (ctx->mm_account) { 2177 mmdrop(ctx->mm_account); 2178 ctx->mm_account = NULL; 2179 } 2180 io_rings_free(ctx); 2181 2182 if (!(ctx->flags & IORING_SETUP_NO_SQARRAY)) 2183 static_branch_slow_dec_deferred(&io_key_has_sqarray); 2184 2185 percpu_ref_exit(&ctx->refs); 2186 free_uid(ctx->user); 2187 io_req_caches_free(ctx); 2188 2189 if (ctx->restrictions.bpf_filters) { 2190 WARN_ON_ONCE(ctx->bpf_filters != 2191 ctx->restrictions.bpf_filters->filters); 2192 } else { 2193 WARN_ON_ONCE(ctx->bpf_filters); 2194 } 2195 io_put_bpf_filters(&ctx->restrictions); 2196 2197 WARN_ON_ONCE(ctx->nr_req_allocated); 2198 2199 if (ctx->hash_map) 2200 io_wq_put_hash(ctx->hash_map); 2201 io_napi_free(ctx); 2202 kvfree(ctx->cancel_table.hbs); 2203 xa_destroy(&ctx->io_bl_xa); 2204 xa_destroy(&ctx->hpage_acct); 2205 kfree(ctx); 2206 } 2207 2208 static __cold void io_activate_pollwq_cb(struct callback_head *cb) 2209 { 2210 struct io_ring_ctx *ctx = container_of(cb, struct io_ring_ctx, 2211 poll_wq_task_work); 2212 2213 mutex_lock(&ctx->uring_lock); 2214 ctx->int_flags |= IO_RING_F_POLL_ACTIVATED; 2215 mutex_unlock(&ctx->uring_lock); 2216 2217 /* 2218 * Wake ups for some events between start of polling and activation 2219 * might've been lost due to loose synchronisation. 2220 */ 2221 wake_up_all(&ctx->poll_wq); 2222 percpu_ref_put(&ctx->refs); 2223 } 2224 2225 __cold void io_activate_pollwq(struct io_ring_ctx *ctx) 2226 { 2227 spin_lock(&ctx->completion_lock); 2228 /* already activated or in progress */ 2229 if ((ctx->int_flags & IO_RING_F_POLL_ACTIVATED) || ctx->poll_wq_task_work.func) 2230 goto out; 2231 if (WARN_ON_ONCE(!(ctx->int_flags & IO_RING_F_TASK_COMPLETE))) 2232 goto out; 2233 if (!ctx->submitter_task) 2234 goto out; 2235 /* 2236 * with ->submitter_task only the submitter task completes requests, we 2237 * only need to sync with it, which is done by injecting a tw 2238 */ 2239 init_task_work(&ctx->poll_wq_task_work, io_activate_pollwq_cb); 2240 percpu_ref_get(&ctx->refs); 2241 if (task_work_add(ctx->submitter_task, &ctx->poll_wq_task_work, TWA_SIGNAL)) 2242 percpu_ref_put(&ctx->refs); 2243 out: 2244 spin_unlock(&ctx->completion_lock); 2245 } 2246 2247 static __poll_t io_uring_poll(struct file *file, poll_table *wait) 2248 { 2249 struct io_ring_ctx *ctx = file->private_data; 2250 __poll_t mask = 0; 2251 2252 if (unlikely(!(data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED))) 2253 io_activate_pollwq(ctx); 2254 /* 2255 * provides mb() which pairs with barrier from wq_has_sleeper 2256 * call in io_commit_cqring 2257 */ 2258 poll_wait(file, &ctx->poll_wq, wait); 2259 2260 rcu_read_lock(); 2261 2262 if (!__io_sqring_full(ctx)) 2263 mask |= EPOLLOUT | EPOLLWRNORM; 2264 2265 /* 2266 * Don't flush cqring overflow list here, just do a simple check. 2267 * Otherwise there could possible be ABBA deadlock: 2268 * CPU0 CPU1 2269 * ---- ---- 2270 * lock(&ctx->uring_lock); 2271 * lock(&ep->mtx); 2272 * lock(&ctx->uring_lock); 2273 * lock(&ep->mtx); 2274 * 2275 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this 2276 * pushes them to do the flush. 2277 */ 2278 2279 if (__io_cqring_events_user(ctx) || io_has_work(ctx)) 2280 mask |= EPOLLIN | EPOLLRDNORM; 2281 2282 rcu_read_unlock(); 2283 return mask; 2284 } 2285 2286 struct io_tctx_exit { 2287 struct callback_head task_work; 2288 struct completion completion; 2289 struct io_ring_ctx *ctx; 2290 }; 2291 2292 static __cold void io_tctx_exit_cb(struct callback_head *cb) 2293 { 2294 struct io_uring_task *tctx = current->io_uring; 2295 struct io_tctx_exit *work; 2296 2297 work = container_of(cb, struct io_tctx_exit, task_work); 2298 /* 2299 * When @in_cancel, we're in cancellation and it's racy to remove the 2300 * node. It'll be removed by the end of cancellation, just ignore it. 2301 * tctx can be NULL if the queueing of this task_work raced with 2302 * work cancelation off the exec path. 2303 */ 2304 if (tctx && !atomic_read(&tctx->in_cancel)) 2305 io_uring_del_tctx_node((unsigned long)work->ctx); 2306 complete(&work->completion); 2307 } 2308 2309 static __cold void io_ring_exit_work(struct work_struct *work) 2310 { 2311 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); 2312 unsigned long timeout = jiffies + IO_URING_EXIT_WAIT_MAX; 2313 unsigned long interval = HZ / 20; 2314 struct io_tctx_exit exit; 2315 struct io_tctx_node *node; 2316 int ret; 2317 2318 mutex_lock(&ctx->uring_lock); 2319 io_terminate_zcrx(ctx); 2320 mutex_unlock(&ctx->uring_lock); 2321 2322 /* 2323 * If we're doing polled IO and end up having requests being 2324 * submitted async (out-of-line), then completions can come in while 2325 * we're waiting for refs to drop. We need to reap these manually, 2326 * as nobody else will be looking for them. 2327 */ 2328 do { 2329 if (test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq)) { 2330 mutex_lock(&ctx->uring_lock); 2331 io_cqring_overflow_kill(ctx); 2332 mutex_unlock(&ctx->uring_lock); 2333 } 2334 2335 /* The SQPOLL thread never reaches this path */ 2336 do { 2337 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) 2338 io_cancel_local_task_work(ctx); 2339 cond_resched(); 2340 } while (io_uring_try_cancel_requests(ctx, NULL, true, false)); 2341 2342 if (ctx->sq_data) { 2343 struct io_sq_data *sqd = ctx->sq_data; 2344 struct task_struct *tsk; 2345 2346 io_sq_thread_park(sqd); 2347 tsk = sqpoll_task_locked(sqd); 2348 if (tsk && tsk->io_uring && tsk->io_uring->io_wq) 2349 io_wq_cancel_cb(tsk->io_uring->io_wq, 2350 io_cancel_ctx_cb, ctx, true); 2351 io_sq_thread_unpark(sqd); 2352 } 2353 2354 io_req_caches_free(ctx); 2355 2356 if (WARN_ON_ONCE(time_after(jiffies, timeout))) { 2357 /* there is little hope left, don't run it too often */ 2358 interval = HZ * 60; 2359 } 2360 /* 2361 * This is really an uninterruptible wait, as it has to be 2362 * complete. But it's also run from a kworker, which doesn't 2363 * take signals, so it's fine to make it interruptible. This 2364 * avoids scenarios where we knowingly can wait much longer 2365 * on completions, for example if someone does a SIGSTOP on 2366 * a task that needs to finish task_work to make this loop 2367 * complete. That's a synthetic situation that should not 2368 * cause a stuck task backtrace, and hence a potential panic 2369 * on stuck tasks if that is enabled. 2370 */ 2371 } while (!wait_for_completion_interruptible_timeout(&ctx->ref_comp, interval)); 2372 2373 init_completion(&exit.completion); 2374 init_task_work(&exit.task_work, io_tctx_exit_cb); 2375 exit.ctx = ctx; 2376 2377 mutex_lock(&ctx->uring_lock); 2378 mutex_lock(&ctx->tctx_lock); 2379 while (!list_empty(&ctx->tctx_list)) { 2380 WARN_ON_ONCE(time_after(jiffies, timeout)); 2381 2382 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, 2383 ctx_node); 2384 /* don't spin on a single task if cancellation failed */ 2385 list_rotate_left(&ctx->tctx_list); 2386 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); 2387 if (WARN_ON_ONCE(ret)) 2388 continue; 2389 2390 mutex_unlock(&ctx->tctx_lock); 2391 mutex_unlock(&ctx->uring_lock); 2392 /* 2393 * See comment above for 2394 * wait_for_completion_interruptible_timeout() on why this 2395 * wait is marked as interruptible. 2396 */ 2397 wait_for_completion_interruptible(&exit.completion); 2398 mutex_lock(&ctx->uring_lock); 2399 mutex_lock(&ctx->tctx_lock); 2400 } 2401 mutex_unlock(&ctx->tctx_lock); 2402 mutex_unlock(&ctx->uring_lock); 2403 spin_lock(&ctx->completion_lock); 2404 spin_unlock(&ctx->completion_lock); 2405 2406 /* pairs with RCU read section in io_req_local_work_add() */ 2407 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) 2408 synchronize_rcu(); 2409 2410 io_ring_ctx_free(ctx); 2411 } 2412 2413 static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) 2414 { 2415 unsigned long index; 2416 struct cred *creds; 2417 2418 mutex_lock(&ctx->uring_lock); 2419 percpu_ref_kill(&ctx->refs); 2420 xa_for_each(&ctx->personalities, index, creds) 2421 io_unregister_personality(ctx, index); 2422 mutex_unlock(&ctx->uring_lock); 2423 2424 INIT_WORK(&ctx->exit_work, io_ring_exit_work); 2425 /* 2426 * Use system_dfl_wq to avoid spawning tons of event kworkers 2427 * if we're exiting a ton of rings at the same time. It just adds 2428 * noise and overhead, there's no discernable change in runtime 2429 * over using system_percpu_wq. 2430 */ 2431 queue_work(iou_wq, &ctx->exit_work); 2432 } 2433 2434 static int io_uring_release(struct inode *inode, struct file *file) 2435 { 2436 struct io_ring_ctx *ctx = file->private_data; 2437 2438 file->private_data = NULL; 2439 io_ring_ctx_wait_and_kill(ctx); 2440 return 0; 2441 } 2442 2443 static struct io_uring_reg_wait *io_get_ext_arg_reg(struct io_ring_ctx *ctx, 2444 const struct io_uring_getevents_arg __user *uarg) 2445 { 2446 unsigned long size = sizeof(struct io_uring_reg_wait); 2447 unsigned long offset = (uintptr_t)uarg; 2448 unsigned long end; 2449 2450 if (unlikely(offset % sizeof(long))) 2451 return ERR_PTR(-EFAULT); 2452 2453 /* also protects from NULL ->cq_wait_arg as the size would be 0 */ 2454 if (unlikely(check_add_overflow(offset, size, &end) || 2455 end > ctx->cq_wait_size)) 2456 return ERR_PTR(-EFAULT); 2457 2458 offset = array_index_nospec(offset, ctx->cq_wait_size - size); 2459 return ctx->cq_wait_arg + offset; 2460 } 2461 2462 static int io_validate_ext_arg(struct io_ring_ctx *ctx, unsigned flags, 2463 const void __user *argp, size_t argsz) 2464 { 2465 struct io_uring_getevents_arg arg; 2466 2467 if (!(flags & IORING_ENTER_EXT_ARG)) 2468 return 0; 2469 if (flags & IORING_ENTER_EXT_ARG_REG) 2470 return -EINVAL; 2471 if (argsz != sizeof(arg)) 2472 return -EINVAL; 2473 if (copy_from_user(&arg, argp, sizeof(arg))) 2474 return -EFAULT; 2475 return 0; 2476 } 2477 2478 static int io_get_ext_arg(struct io_ring_ctx *ctx, unsigned flags, 2479 const void __user *argp, struct ext_arg *ext_arg) 2480 { 2481 const struct io_uring_getevents_arg __user *uarg = argp; 2482 struct io_uring_getevents_arg arg; 2483 2484 ext_arg->iowait = !(flags & IORING_ENTER_NO_IOWAIT); 2485 2486 /* 2487 * If EXT_ARG isn't set, then we have no timespec and the argp pointer 2488 * is just a pointer to the sigset_t. 2489 */ 2490 if (!(flags & IORING_ENTER_EXT_ARG)) { 2491 ext_arg->sig = (const sigset_t __user *) argp; 2492 return 0; 2493 } 2494 2495 if (flags & IORING_ENTER_EXT_ARG_REG) { 2496 struct io_uring_reg_wait *w; 2497 2498 if (ext_arg->argsz != sizeof(struct io_uring_reg_wait)) 2499 return -EINVAL; 2500 w = io_get_ext_arg_reg(ctx, argp); 2501 if (IS_ERR(w)) 2502 return PTR_ERR(w); 2503 2504 if (w->flags & ~IORING_REG_WAIT_TS) 2505 return -EINVAL; 2506 ext_arg->min_time = READ_ONCE(w->min_wait_usec) * NSEC_PER_USEC; 2507 ext_arg->sig = u64_to_user_ptr(READ_ONCE(w->sigmask)); 2508 ext_arg->argsz = READ_ONCE(w->sigmask_sz); 2509 if (w->flags & IORING_REG_WAIT_TS) { 2510 ext_arg->ts.tv_sec = READ_ONCE(w->ts.tv_sec); 2511 ext_arg->ts.tv_nsec = READ_ONCE(w->ts.tv_nsec); 2512 ext_arg->ts_set = true; 2513 } 2514 return 0; 2515 } 2516 2517 /* 2518 * EXT_ARG is set - ensure we agree on the size of it and copy in our 2519 * timespec and sigset_t pointers if good. 2520 */ 2521 if (ext_arg->argsz != sizeof(arg)) 2522 return -EINVAL; 2523 #ifdef CONFIG_64BIT 2524 if (!user_access_begin(uarg, sizeof(*uarg))) 2525 return -EFAULT; 2526 unsafe_get_user(arg.sigmask, &uarg->sigmask, uaccess_end); 2527 unsafe_get_user(arg.sigmask_sz, &uarg->sigmask_sz, uaccess_end); 2528 unsafe_get_user(arg.min_wait_usec, &uarg->min_wait_usec, uaccess_end); 2529 unsafe_get_user(arg.ts, &uarg->ts, uaccess_end); 2530 user_access_end(); 2531 #else 2532 if (copy_from_user(&arg, uarg, sizeof(arg))) 2533 return -EFAULT; 2534 #endif 2535 ext_arg->min_time = arg.min_wait_usec * NSEC_PER_USEC; 2536 ext_arg->sig = u64_to_user_ptr(arg.sigmask); 2537 ext_arg->argsz = arg.sigmask_sz; 2538 if (arg.ts) { 2539 if (get_timespec64(&ext_arg->ts, u64_to_user_ptr(arg.ts))) 2540 return -EFAULT; 2541 ext_arg->ts_set = true; 2542 } 2543 return 0; 2544 #ifdef CONFIG_64BIT 2545 uaccess_end: 2546 user_access_end(); 2547 return -EFAULT; 2548 #endif 2549 } 2550 2551 /* 2552 * Given an 'fd' value, return the ctx associated with if. If 'registered' is 2553 * true, then the registered index is used. Otherwise, the normal fd table. 2554 * Caller must call fput() on the returned file if it isn't a registered file, 2555 * unless it's an ERR_PTR. 2556 */ 2557 struct file *io_uring_ctx_get_file(unsigned int fd, bool registered) 2558 { 2559 struct file *file; 2560 2561 if (registered) { 2562 /* 2563 * Ring fd has been registered via IORING_REGISTER_RING_FDS, we 2564 * need only dereference our task private array to find it. 2565 */ 2566 struct io_uring_task *tctx = current->io_uring; 2567 2568 if (unlikely(!tctx || fd >= IO_RINGFD_REG_MAX)) 2569 return ERR_PTR(-EINVAL); 2570 fd = array_index_nospec(fd, IO_RINGFD_REG_MAX); 2571 file = tctx->registered_rings[fd]; 2572 } else { 2573 file = fget(fd); 2574 } 2575 2576 if (unlikely(!file)) 2577 return ERR_PTR(-EBADF); 2578 if (io_is_uring_fops(file)) 2579 return file; 2580 if (!registered) 2581 fput(file); 2582 return ERR_PTR(-EOPNOTSUPP); 2583 } 2584 2585 2586 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, 2587 u32, min_complete, u32, flags, const void __user *, argp, 2588 size_t, argsz) 2589 { 2590 struct io_ring_ctx *ctx; 2591 struct file *file; 2592 long ret; 2593 2594 if (unlikely(flags & ~IORING_ENTER_FLAGS)) 2595 return -EINVAL; 2596 2597 file = io_uring_ctx_get_file(fd, flags & IORING_ENTER_REGISTERED_RING); 2598 if (IS_ERR(file)) 2599 return PTR_ERR(file); 2600 ctx = file->private_data; 2601 ret = -EBADFD; 2602 /* 2603 * Keep IORING_SETUP_R_DISABLED check before submitter_task load 2604 * in io_uring_add_tctx_node() -> __io_uring_add_tctx_node_from_submit() 2605 */ 2606 if (unlikely(smp_load_acquire(&ctx->flags) & IORING_SETUP_R_DISABLED)) 2607 goto out; 2608 2609 if (io_has_loop_ops(ctx)) { 2610 ret = io_run_loop(ctx); 2611 goto out; 2612 } 2613 2614 /* 2615 * For SQ polling, the thread will do all submissions and completions. 2616 * Just return the requested submit count, and wake the thread if 2617 * we were asked to. 2618 */ 2619 ret = 0; 2620 if (ctx->flags & IORING_SETUP_SQPOLL) { 2621 if (unlikely(ctx->sq_data->thread == NULL)) { 2622 ret = -EOWNERDEAD; 2623 goto out; 2624 } 2625 if (flags & IORING_ENTER_SQ_WAKEUP) 2626 wake_up(&ctx->sq_data->wait); 2627 if (flags & IORING_ENTER_SQ_WAIT) 2628 io_sqpoll_wait_sq(ctx); 2629 2630 ret = to_submit; 2631 } else if (to_submit) { 2632 ret = io_uring_add_tctx_node(ctx); 2633 if (unlikely(ret)) 2634 goto out; 2635 2636 mutex_lock(&ctx->uring_lock); 2637 ret = io_submit_sqes(ctx, to_submit); 2638 if (ret != to_submit) { 2639 mutex_unlock(&ctx->uring_lock); 2640 goto out; 2641 } 2642 if (flags & IORING_ENTER_GETEVENTS) { 2643 if (ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL) 2644 goto iopoll_locked; 2645 /* 2646 * Ignore errors, we'll soon call io_cqring_wait() and 2647 * it should handle ownership problems if any. 2648 */ 2649 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) 2650 (void)io_run_local_work_locked(ctx, min_complete); 2651 } 2652 mutex_unlock(&ctx->uring_lock); 2653 } 2654 2655 if (flags & IORING_ENTER_GETEVENTS) { 2656 int ret2; 2657 2658 if (ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL) { 2659 /* 2660 * We disallow the app entering submit/complete with 2661 * polling, but we still need to lock the ring to 2662 * prevent racing with polled issue that got punted to 2663 * a workqueue. 2664 */ 2665 mutex_lock(&ctx->uring_lock); 2666 iopoll_locked: 2667 ret2 = io_validate_ext_arg(ctx, flags, argp, argsz); 2668 if (likely(!ret2)) 2669 ret2 = io_iopoll_check(ctx, min_complete); 2670 mutex_unlock(&ctx->uring_lock); 2671 } else { 2672 struct ext_arg ext_arg = { .argsz = argsz }; 2673 2674 ret2 = io_get_ext_arg(ctx, flags, argp, &ext_arg); 2675 if (likely(!ret2)) 2676 ret2 = io_cqring_wait(ctx, min_complete, flags, 2677 &ext_arg); 2678 } 2679 2680 if (!ret) { 2681 ret = ret2; 2682 2683 /* 2684 * EBADR indicates that one or more CQE were dropped. 2685 * Once the user has been informed we can clear the bit 2686 * as they are obviously ok with those drops. 2687 */ 2688 if (unlikely(ret2 == -EBADR)) 2689 clear_bit(IO_CHECK_CQ_DROPPED_BIT, 2690 &ctx->check_cq); 2691 } 2692 } 2693 out: 2694 if (!(flags & IORING_ENTER_REGISTERED_RING)) 2695 fput(file); 2696 return ret; 2697 } 2698 2699 static const struct file_operations io_uring_fops = { 2700 .release = io_uring_release, 2701 .mmap = io_uring_mmap, 2702 .get_unmapped_area = io_uring_get_unmapped_area, 2703 #ifndef CONFIG_MMU 2704 .mmap_capabilities = io_uring_nommu_mmap_capabilities, 2705 #endif 2706 .poll = io_uring_poll, 2707 #ifdef CONFIG_PROC_FS 2708 .show_fdinfo = io_uring_show_fdinfo, 2709 #endif 2710 }; 2711 2712 bool io_is_uring_fops(struct file *file) 2713 { 2714 return file->f_op == &io_uring_fops; 2715 } 2716 2717 static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, 2718 struct io_ctx_config *config) 2719 { 2720 struct io_uring_params *p = &config->p; 2721 struct io_rings_layout *rl = &config->layout; 2722 struct io_uring_region_desc rd; 2723 struct io_rings *rings; 2724 int ret; 2725 2726 /* make sure these are sane, as we already accounted them */ 2727 ctx->sq_entries = p->sq_entries; 2728 ctx->cq_entries = p->cq_entries; 2729 2730 memset(&rd, 0, sizeof(rd)); 2731 rd.size = PAGE_ALIGN(rl->rings_size); 2732 if (ctx->flags & IORING_SETUP_NO_MMAP) { 2733 rd.user_addr = p->cq_off.user_addr; 2734 rd.flags |= IORING_MEM_REGION_TYPE_USER; 2735 } 2736 ret = io_create_region(ctx, &ctx->ring_region, &rd, IORING_OFF_CQ_RING); 2737 if (ret) 2738 return ret; 2739 ctx->rings = rings = io_region_get_ptr(&ctx->ring_region); 2740 rcu_assign_pointer(ctx->rings_rcu, rings); 2741 if (!(ctx->flags & IORING_SETUP_NO_SQARRAY)) 2742 ctx->sq_array = (u32 *)((char *)rings + rl->sq_array_offset); 2743 2744 memset(&rd, 0, sizeof(rd)); 2745 rd.size = PAGE_ALIGN(rl->sq_size); 2746 if (ctx->flags & IORING_SETUP_NO_MMAP) { 2747 rd.user_addr = p->sq_off.user_addr; 2748 rd.flags |= IORING_MEM_REGION_TYPE_USER; 2749 } 2750 ret = io_create_region(ctx, &ctx->sq_region, &rd, IORING_OFF_SQES); 2751 if (ret) { 2752 io_rings_free(ctx); 2753 return ret; 2754 } 2755 ctx->sq_sqes = io_region_get_ptr(&ctx->sq_region); 2756 2757 memset(rings, 0, sizeof(*rings)); 2758 WRITE_ONCE(rings->sq_ring_mask, ctx->sq_entries - 1); 2759 WRITE_ONCE(rings->cq_ring_mask, ctx->cq_entries - 1); 2760 WRITE_ONCE(rings->sq_ring_entries, ctx->sq_entries); 2761 WRITE_ONCE(rings->cq_ring_entries, ctx->cq_entries); 2762 return 0; 2763 } 2764 2765 static int io_uring_install_fd(struct file *file) 2766 { 2767 int fd; 2768 2769 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); 2770 if (fd < 0) 2771 return fd; 2772 fd_install(fd, file); 2773 return fd; 2774 } 2775 2776 /* 2777 * Allocate an anonymous fd, this is what constitutes the application 2778 * visible backing of an io_uring instance. The application mmaps this 2779 * fd to gain access to the SQ/CQ ring details. 2780 */ 2781 static struct file *io_uring_get_file(struct io_ring_ctx *ctx) 2782 { 2783 /* Create a new inode so that the LSM can block the creation. */ 2784 return anon_inode_create_getfile("[io_uring]", &io_uring_fops, ctx, 2785 O_RDWR | O_CLOEXEC, NULL); 2786 } 2787 2788 static int io_uring_sanitise_params(struct io_uring_params *p) 2789 { 2790 unsigned flags = p->flags; 2791 2792 if (flags & ~IORING_SETUP_FLAGS) 2793 return -EINVAL; 2794 2795 if (flags & IORING_SETUP_SQ_REWIND) { 2796 if ((flags & IORING_SETUP_SQPOLL) || 2797 !(flags & IORING_SETUP_NO_SQARRAY)) 2798 return -EINVAL; 2799 } 2800 2801 /* There is no way to mmap rings without a real fd */ 2802 if ((flags & IORING_SETUP_REGISTERED_FD_ONLY) && 2803 !(flags & IORING_SETUP_NO_MMAP)) 2804 return -EINVAL; 2805 2806 if (flags & IORING_SETUP_SQPOLL) { 2807 /* IPI related flags don't make sense with SQPOLL */ 2808 if (flags & (IORING_SETUP_COOP_TASKRUN | 2809 IORING_SETUP_TASKRUN_FLAG | 2810 IORING_SETUP_DEFER_TASKRUN)) 2811 return -EINVAL; 2812 } 2813 2814 if (flags & IORING_SETUP_TASKRUN_FLAG) { 2815 if (!(flags & (IORING_SETUP_COOP_TASKRUN | 2816 IORING_SETUP_DEFER_TASKRUN))) 2817 return -EINVAL; 2818 } 2819 2820 /* HYBRID_IOPOLL only valid with IOPOLL */ 2821 if ((flags & IORING_SETUP_HYBRID_IOPOLL) && !(flags & IORING_SETUP_IOPOLL)) 2822 return -EINVAL; 2823 2824 /* 2825 * For DEFER_TASKRUN we require the completion task to be the same as 2826 * the submission task. This implies that there is only one submitter. 2827 */ 2828 if ((flags & IORING_SETUP_DEFER_TASKRUN) && 2829 !(flags & IORING_SETUP_SINGLE_ISSUER)) 2830 return -EINVAL; 2831 2832 /* 2833 * Nonsensical to ask for CQE32 and mixed CQE support, it's not 2834 * supported to post 16b CQEs on a ring setup with CQE32. 2835 */ 2836 if ((flags & (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED)) == 2837 (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED)) 2838 return -EINVAL; 2839 /* 2840 * Nonsensical to ask for SQE128 and mixed SQE support, it's not 2841 * supported to post 64b SQEs on a ring setup with SQE128. 2842 */ 2843 if ((flags & (IORING_SETUP_SQE128|IORING_SETUP_SQE_MIXED)) == 2844 (IORING_SETUP_SQE128|IORING_SETUP_SQE_MIXED)) 2845 return -EINVAL; 2846 2847 return 0; 2848 } 2849 2850 static int io_uring_fill_params(struct io_uring_params *p) 2851 { 2852 unsigned entries = p->sq_entries; 2853 2854 if (!entries) 2855 return -EINVAL; 2856 if (entries > IORING_MAX_ENTRIES) { 2857 if (!(p->flags & IORING_SETUP_CLAMP)) 2858 return -EINVAL; 2859 entries = IORING_MAX_ENTRIES; 2860 } 2861 2862 /* 2863 * Use twice as many entries for the CQ ring. It's possible for the 2864 * application to drive a higher depth than the size of the SQ ring, 2865 * since the sqes are only used at submission time. This allows for 2866 * some flexibility in overcommitting a bit. If the application has 2867 * set IORING_SETUP_CQSIZE, it will have passed in the desired number 2868 * of CQ ring entries manually. 2869 */ 2870 p->sq_entries = roundup_pow_of_two(entries); 2871 if (p->flags & IORING_SETUP_CQSIZE) { 2872 /* 2873 * If IORING_SETUP_CQSIZE is set, we do the same roundup 2874 * to a power-of-two, if it isn't already. We do NOT impose 2875 * any cq vs sq ring sizing. 2876 */ 2877 if (!p->cq_entries) 2878 return -EINVAL; 2879 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { 2880 if (!(p->flags & IORING_SETUP_CLAMP)) 2881 return -EINVAL; 2882 p->cq_entries = IORING_MAX_CQ_ENTRIES; 2883 } 2884 p->cq_entries = roundup_pow_of_two(p->cq_entries); 2885 if (p->cq_entries < p->sq_entries) 2886 return -EINVAL; 2887 } else { 2888 p->cq_entries = 2 * p->sq_entries; 2889 } 2890 2891 return 0; 2892 } 2893 2894 int io_prepare_config(struct io_ctx_config *config) 2895 { 2896 struct io_uring_params *p = &config->p; 2897 int ret; 2898 2899 ret = io_uring_sanitise_params(p); 2900 if (ret) 2901 return ret; 2902 2903 ret = io_uring_fill_params(p); 2904 if (ret) 2905 return ret; 2906 2907 ret = rings_size(p->flags, p->sq_entries, p->cq_entries, 2908 &config->layout); 2909 if (ret) 2910 return ret; 2911 2912 p->sq_off.head = offsetof(struct io_rings, sq.head); 2913 p->sq_off.tail = offsetof(struct io_rings, sq.tail); 2914 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); 2915 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); 2916 p->sq_off.flags = offsetof(struct io_rings, sq_flags); 2917 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); 2918 p->sq_off.resv1 = 0; 2919 if (!(p->flags & IORING_SETUP_NO_MMAP)) 2920 p->sq_off.user_addr = 0; 2921 2922 p->cq_off.head = offsetof(struct io_rings, cq.head); 2923 p->cq_off.tail = offsetof(struct io_rings, cq.tail); 2924 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); 2925 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); 2926 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); 2927 p->cq_off.cqes = offsetof(struct io_rings, cqes); 2928 p->cq_off.flags = offsetof(struct io_rings, cq_flags); 2929 p->cq_off.resv1 = 0; 2930 if (!(p->flags & IORING_SETUP_NO_MMAP)) 2931 p->cq_off.user_addr = 0; 2932 if (!(p->flags & IORING_SETUP_NO_SQARRAY)) 2933 p->sq_off.array = config->layout.sq_array_offset; 2934 2935 return 0; 2936 } 2937 2938 void io_restriction_clone(struct io_restriction *dst, struct io_restriction *src) 2939 { 2940 memcpy(&dst->register_op, &src->register_op, sizeof(dst->register_op)); 2941 memcpy(&dst->sqe_op, &src->sqe_op, sizeof(dst->sqe_op)); 2942 dst->sqe_flags_allowed = src->sqe_flags_allowed; 2943 dst->sqe_flags_required = src->sqe_flags_required; 2944 dst->op_registered = src->op_registered; 2945 dst->reg_registered = src->reg_registered; 2946 2947 io_bpf_filter_clone(dst, src); 2948 } 2949 2950 static void io_ctx_restriction_clone(struct io_ring_ctx *ctx, 2951 struct io_restriction *src) 2952 { 2953 struct io_restriction *dst = &ctx->restrictions; 2954 2955 io_restriction_clone(dst, src); 2956 if (dst->bpf_filters) 2957 WRITE_ONCE(ctx->bpf_filters, dst->bpf_filters->filters); 2958 if (dst->op_registered) 2959 ctx->int_flags |= IO_RING_F_OP_RESTRICTED; 2960 if (dst->reg_registered) 2961 ctx->int_flags |= IO_RING_F_REG_RESTRICTED; 2962 } 2963 2964 static __cold int io_uring_create(struct io_ctx_config *config) 2965 { 2966 struct io_uring_params *p = &config->p; 2967 struct io_ring_ctx *ctx; 2968 struct io_uring_task *tctx; 2969 struct file *file; 2970 int ret; 2971 2972 ret = io_prepare_config(config); 2973 if (ret) 2974 return ret; 2975 2976 ctx = io_ring_ctx_alloc(p); 2977 if (!ctx) 2978 return -ENOMEM; 2979 2980 ctx->clockid = CLOCK_MONOTONIC; 2981 ctx->clock_offset = 0; 2982 2983 if (!(ctx->flags & IORING_SETUP_NO_SQARRAY)) 2984 static_branch_deferred_inc(&io_key_has_sqarray); 2985 2986 if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) && 2987 !(ctx->flags & IORING_SETUP_IOPOLL)) 2988 ctx->int_flags |= IO_RING_F_TASK_COMPLETE; 2989 2990 if ((ctx->int_flags & IO_RING_F_TASK_COMPLETE) || 2991 (ctx->flags & IORING_SETUP_IOPOLL)) 2992 ctx->int_flags |= IO_RING_F_LOCKLESS_CQ; 2993 2994 /* 2995 * lazy poll_wq activation relies on ->task_complete for synchronisation 2996 * purposes, see io_activate_pollwq() 2997 */ 2998 if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)) 2999 ctx->int_flags |= IO_RING_F_POLL_ACTIVATED; 3000 3001 /* 3002 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user 3003 * space applications don't need to do io completion events 3004 * polling again, they can rely on io_sq_thread to do polling 3005 * work, which can reduce cpu usage and uring_lock contention. 3006 */ 3007 if (ctx->flags & IORING_SETUP_IOPOLL && 3008 !(ctx->flags & IORING_SETUP_SQPOLL)) 3009 ctx->int_flags |= IO_RING_F_SYSCALL_IOPOLL; 3010 3011 if (in_compat_syscall()) 3012 ctx->int_flags |= IO_RING_F_COMPAT; 3013 if (!ns_capable_noaudit(&init_user_ns, CAP_IPC_LOCK)) 3014 ctx->user = get_uid(current_user()); 3015 3016 /* 3017 * For SQPOLL, we just need a wakeup, always. For !SQPOLL, if 3018 * COOP_TASKRUN is set, then IPIs are never needed by the app. 3019 */ 3020 if (ctx->flags & (IORING_SETUP_SQPOLL|IORING_SETUP_COOP_TASKRUN)) 3021 ctx->notify_method = TWA_SIGNAL_NO_IPI; 3022 else 3023 ctx->notify_method = TWA_SIGNAL; 3024 3025 /* 3026 * If the current task has restrictions enabled, then copy them to 3027 * our newly created ring and mark it as registered. 3028 */ 3029 if (current->io_uring_restrict) 3030 io_ctx_restriction_clone(ctx, current->io_uring_restrict); 3031 3032 /* 3033 * This is just grabbed for accounting purposes. When a process exits, 3034 * the mm is exited and dropped before the files, hence we need to hang 3035 * on to this mm purely for the purposes of being able to unaccount 3036 * memory (locked/pinned vm). It's not used for anything else. 3037 */ 3038 mmgrab(current->mm); 3039 ctx->mm_account = current->mm; 3040 3041 ret = io_allocate_scq_urings(ctx, config); 3042 if (ret) 3043 goto err; 3044 3045 ret = io_sq_offload_create(ctx, p); 3046 if (ret) 3047 goto err; 3048 3049 p->features = IORING_FEAT_FLAGS; 3050 3051 if (copy_to_user(config->uptr, p, sizeof(*p))) { 3052 ret = -EFAULT; 3053 goto err; 3054 } 3055 3056 if (ctx->flags & IORING_SETUP_SINGLE_ISSUER 3057 && !(ctx->flags & IORING_SETUP_R_DISABLED)) 3058 ctx->submitter_task = get_task_struct(current); 3059 3060 file = io_uring_get_file(ctx); 3061 if (IS_ERR(file)) { 3062 ret = PTR_ERR(file); 3063 goto err; 3064 } 3065 3066 ret = __io_uring_add_tctx_node(ctx); 3067 if (ret) 3068 goto err_fput; 3069 tctx = current->io_uring; 3070 3071 /* 3072 * Install ring fd as the very last thing, so we don't risk someone 3073 * having closed it before we finish setup 3074 */ 3075 if (p->flags & IORING_SETUP_REGISTERED_FD_ONLY) 3076 ret = io_ring_add_registered_file(tctx, file, 0, IO_RINGFD_REG_MAX); 3077 else 3078 ret = io_uring_install_fd(file); 3079 if (ret < 0) 3080 goto err_fput; 3081 3082 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); 3083 return ret; 3084 err: 3085 io_ring_ctx_wait_and_kill(ctx); 3086 return ret; 3087 err_fput: 3088 fput(file); 3089 return ret; 3090 } 3091 3092 /* 3093 * Sets up an aio uring context, and returns the fd. Applications asks for a 3094 * ring size, we return the actual sq/cq ring sizes (among other things) in the 3095 * params structure passed in. 3096 */ 3097 static long io_uring_setup(u32 entries, struct io_uring_params __user *params) 3098 { 3099 struct io_ctx_config config; 3100 3101 memset(&config, 0, sizeof(config)); 3102 3103 if (copy_from_user(&config.p, params, sizeof(config.p))) 3104 return -EFAULT; 3105 3106 if (!mem_is_zero(&config.p.resv, sizeof(config.p.resv))) 3107 return -EINVAL; 3108 3109 config.p.sq_entries = entries; 3110 config.uptr = params; 3111 return io_uring_create(&config); 3112 } 3113 3114 static inline int io_uring_allowed(void) 3115 { 3116 int disabled = READ_ONCE(sysctl_io_uring_disabled); 3117 kgid_t io_uring_group; 3118 3119 if (disabled == 2) 3120 return -EPERM; 3121 3122 if (disabled == 0 || capable(CAP_SYS_ADMIN)) 3123 goto allowed_lsm; 3124 3125 io_uring_group = make_kgid(&init_user_ns, sysctl_io_uring_group); 3126 if (!gid_valid(io_uring_group)) 3127 return -EPERM; 3128 3129 if (!in_group_p(io_uring_group)) 3130 return -EPERM; 3131 3132 allowed_lsm: 3133 return security_uring_allowed(); 3134 } 3135 3136 SYSCALL_DEFINE2(io_uring_setup, u32, entries, 3137 struct io_uring_params __user *, params) 3138 { 3139 int ret; 3140 3141 ret = io_uring_allowed(); 3142 if (ret) 3143 return ret; 3144 3145 return io_uring_setup(entries, params); 3146 } 3147 3148 static int __init io_uring_init(void) 3149 { 3150 struct kmem_cache_args kmem_args = { 3151 .useroffset = offsetof(struct io_kiocb, cmd.data), 3152 .usersize = sizeof_field(struct io_kiocb, cmd.data), 3153 .freeptr_offset = offsetof(struct io_kiocb, work), 3154 .use_freeptr_offset = true, 3155 }; 3156 3157 #define __BUILD_BUG_VERIFY_OFFSET_SIZE(stype, eoffset, esize, ename) do { \ 3158 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ 3159 BUILD_BUG_ON(sizeof_field(stype, ename) != esize); \ 3160 } while (0) 3161 3162 #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ 3163 __BUILD_BUG_VERIFY_OFFSET_SIZE(struct io_uring_sqe, eoffset, sizeof(etype), ename) 3164 #define BUILD_BUG_SQE_ELEM_SIZE(eoffset, esize, ename) \ 3165 __BUILD_BUG_VERIFY_OFFSET_SIZE(struct io_uring_sqe, eoffset, esize, ename) 3166 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); 3167 BUILD_BUG_SQE_ELEM(0, __u8, opcode); 3168 BUILD_BUG_SQE_ELEM(1, __u8, flags); 3169 BUILD_BUG_SQE_ELEM(2, __u16, ioprio); 3170 BUILD_BUG_SQE_ELEM(4, __s32, fd); 3171 BUILD_BUG_SQE_ELEM(8, __u64, off); 3172 BUILD_BUG_SQE_ELEM(8, __u64, addr2); 3173 BUILD_BUG_SQE_ELEM(8, __u32, cmd_op); 3174 BUILD_BUG_SQE_ELEM(12, __u32, __pad1); 3175 BUILD_BUG_SQE_ELEM(16, __u64, addr); 3176 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); 3177 BUILD_BUG_SQE_ELEM(24, __u32, len); 3178 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); 3179 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); 3180 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); 3181 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); 3182 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); 3183 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); 3184 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); 3185 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); 3186 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); 3187 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); 3188 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); 3189 BUILD_BUG_SQE_ELEM(28, __u32, open_flags); 3190 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); 3191 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); 3192 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); 3193 BUILD_BUG_SQE_ELEM(28, __u32, rename_flags); 3194 BUILD_BUG_SQE_ELEM(28, __u32, unlink_flags); 3195 BUILD_BUG_SQE_ELEM(28, __u32, hardlink_flags); 3196 BUILD_BUG_SQE_ELEM(28, __u32, xattr_flags); 3197 BUILD_BUG_SQE_ELEM(28, __u32, msg_ring_flags); 3198 BUILD_BUG_SQE_ELEM(32, __u64, user_data); 3199 BUILD_BUG_SQE_ELEM(40, __u16, buf_index); 3200 BUILD_BUG_SQE_ELEM(40, __u16, buf_group); 3201 BUILD_BUG_SQE_ELEM(42, __u16, personality); 3202 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); 3203 BUILD_BUG_SQE_ELEM(44, __u32, file_index); 3204 BUILD_BUG_SQE_ELEM(44, __u16, addr_len); 3205 BUILD_BUG_SQE_ELEM(44, __u8, write_stream); 3206 BUILD_BUG_SQE_ELEM(45, __u8, __pad4[0]); 3207 BUILD_BUG_SQE_ELEM(46, __u16, __pad3[0]); 3208 BUILD_BUG_SQE_ELEM(48, __u64, addr3); 3209 BUILD_BUG_SQE_ELEM_SIZE(48, 0, cmd); 3210 BUILD_BUG_SQE_ELEM(48, __u64, attr_ptr); 3211 BUILD_BUG_SQE_ELEM(56, __u64, attr_type_mask); 3212 BUILD_BUG_SQE_ELEM(56, __u64, __pad2); 3213 3214 BUILD_BUG_ON(sizeof(struct io_uring_files_update) != 3215 sizeof(struct io_uring_rsrc_update)); 3216 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > 3217 sizeof(struct io_uring_rsrc_update2)); 3218 3219 /* ->buf_index is u16 */ 3220 BUILD_BUG_ON(offsetof(struct io_uring_buf_ring, bufs) != 0); 3221 BUILD_BUG_ON(offsetof(struct io_uring_buf, resv) != 3222 offsetof(struct io_uring_buf_ring, tail)); 3223 3224 /* should fit into one byte */ 3225 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); 3226 BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); 3227 BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); 3228 3229 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof_field(struct io_kiocb, flags)); 3230 3231 BUILD_BUG_ON(sizeof(atomic_t) != sizeof(u32)); 3232 3233 /* top 8bits are for internal use */ 3234 BUILD_BUG_ON((IORING_URING_CMD_MASK & 0xff000000) != 0); 3235 3236 io_uring_optable_init(); 3237 3238 /* imu->dir is u8 */ 3239 BUILD_BUG_ON((IO_IMU_DEST | IO_IMU_SOURCE) > U8_MAX); 3240 3241 /* 3242 * Allow user copy in the per-command field, which starts after the 3243 * file in io_kiocb and until the opcode field. The openat2 handling 3244 * requires copying in user memory into the io_kiocb object in that 3245 * range, and HARDENED_USERCOPY will complain if we haven't 3246 * correctly annotated this range. 3247 */ 3248 req_cachep = kmem_cache_create("io_kiocb", sizeof(struct io_kiocb), &kmem_args, 3249 SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT | 3250 SLAB_TYPESAFE_BY_RCU); 3251 3252 iou_wq = alloc_workqueue("iou_exit", WQ_UNBOUND, 64); 3253 BUG_ON(!iou_wq); 3254 3255 #ifdef CONFIG_SYSCTL 3256 register_sysctl_init("kernel", kernel_io_uring_disabled_table); 3257 #endif 3258 3259 return 0; 3260 }; 3261 __initcall(io_uring_init); 3262