1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Contains the core associated with submission side polling of the SQ 4 * ring, offloading submissions from the application to a kernel thread. 5 */ 6 #include <linux/kernel.h> 7 #include <linux/errno.h> 8 #include <linux/file.h> 9 #include <linux/mm.h> 10 #include <linux/slab.h> 11 #include <linux/audit.h> 12 #include <linux/security.h> 13 #include <linux/cpuset.h> 14 #include <linux/sched/cputime.h> 15 #include <linux/io_uring.h> 16 #include <linux/kcov.h> 17 18 #include <uapi/linux/io_uring.h> 19 20 #include "io_uring.h" 21 #include "tctx.h" 22 #include "napi.h" 23 #include "cancel.h" 24 #include "sqpoll.h" 25 26 #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 27 #define IORING_TW_CAP_ENTRIES_VALUE 32 28 29 enum { 30 IO_SQ_THREAD_SHOULD_STOP = 0, 31 IO_SQ_THREAD_SHOULD_PARK, 32 IO_SQ_THREAD_EXITING, 33 }; 34 35 void io_sq_thread_unpark(struct io_sq_data *sqd) 36 __releases(&sqd->lock) 37 { 38 WARN_ON_ONCE(sqpoll_task_locked(sqd) == current); 39 40 /* 41 * Do the dance but not conditional clear_bit() because it'd race with 42 * other threads incrementing park_pending and setting the bit. 43 */ 44 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); 45 if (atomic_dec_return(&sqd->park_pending)) 46 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); 47 mutex_unlock(&sqd->lock); 48 wake_up(&sqd->wait); 49 } 50 51 void io_sq_thread_park(struct io_sq_data *sqd) 52 __acquires(&sqd->lock) 53 { 54 struct task_struct *tsk; 55 56 atomic_inc(&sqd->park_pending); 57 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); 58 mutex_lock(&sqd->lock); 59 60 tsk = sqpoll_task_locked(sqd); 61 if (tsk) { 62 WARN_ON_ONCE(tsk == current); 63 wake_up_process(tsk); 64 } 65 } 66 67 /* 68 * The sqd has two holders: its rings, as one, and its thread. Neither waits 69 * for the other to let go. The one that lets go last frees the sqd. 70 */ 71 static void io_sq_data_release(struct io_sq_data *sqd) 72 { 73 if (refcount_dec_and_test(&sqd->free_refs)) 74 kfree(sqd); 75 } 76 77 /* 78 * Ask the thread to exit, and don't wait for it. A thread that is stopped or 79 * frozen exits only when a user lets it run again, and the exit work of the 80 * ring would keep its place in the workqueue until then. All rings have left 81 * ctx_list by now and have no request left, so the thread can reach none of 82 * them. What it still uses is its own or counted: the sqd by free_refs, the 83 * hash of its io-wq by hash->refs. 84 */ 85 void io_sq_thread_stop(struct io_sq_data *sqd) 86 { 87 struct task_struct *tsk; 88 89 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); 90 91 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); 92 mutex_lock(&sqd->lock); 93 tsk = sqpoll_task_locked(sqd); 94 if (tsk) { 95 WARN_ON_ONCE(tsk == current); 96 wake_up_process(tsk); 97 } 98 mutex_unlock(&sqd->lock); 99 } 100 101 void io_put_sq_data(struct io_sq_data *sqd) 102 { 103 if (refcount_dec_and_test(&sqd->refs)) { 104 WARN_ON_ONCE(atomic_read(&sqd->park_pending)); 105 106 io_sq_thread_stop(sqd); 107 io_sq_data_release(sqd); 108 } 109 } 110 111 static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) 112 { 113 struct io_ring_ctx *ctx; 114 unsigned sq_thread_idle = 0; 115 116 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) 117 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); 118 sqd->sq_thread_idle = sq_thread_idle; 119 } 120 121 void io_sq_thread_finish(struct io_ring_ctx *ctx) 122 { 123 struct io_sq_data *sqd = ctx->sq_data; 124 125 if (sqd) { 126 io_sq_thread_park(sqd); 127 list_del_init(&ctx->sqd_list); 128 io_sqd_update_thread_idle(sqd); 129 io_sq_thread_unpark(sqd); 130 131 io_put_sq_data(sqd); 132 ctx->sq_data = NULL; 133 } 134 } 135 136 static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) 137 { 138 struct io_ring_ctx *ctx_attach; 139 struct io_sq_data *sqd; 140 CLASS(fd, f)(p->wq_fd); 141 142 if (fd_empty(f)) 143 return ERR_PTR(-ENXIO); 144 if (!io_is_uring_fops(fd_file(f))) 145 return ERR_PTR(-EINVAL); 146 147 ctx_attach = fd_file(f)->private_data; 148 sqd = ctx_attach->sq_data; 149 if (!sqd) 150 return ERR_PTR(-EINVAL); 151 if (sqd->task_tgid != current->tgid) 152 return ERR_PTR(-EPERM); 153 154 refcount_inc(&sqd->refs); 155 return sqd; 156 } 157 158 static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, 159 bool *attached) 160 { 161 struct io_sq_data *sqd; 162 163 *attached = false; 164 if (p->flags & IORING_SETUP_ATTACH_WQ) { 165 sqd = io_attach_sq_data(p); 166 if (!IS_ERR(sqd)) { 167 *attached = true; 168 return sqd; 169 } 170 /* fall through for EPERM case, setup new sqd/task */ 171 if (PTR_ERR(sqd) != -EPERM) 172 return sqd; 173 } 174 175 sqd = kzalloc_obj(*sqd); 176 if (!sqd) 177 return ERR_PTR(-ENOMEM); 178 179 atomic_set(&sqd->park_pending, 0); 180 refcount_set(&sqd->refs, 1); 181 refcount_set(&sqd->free_refs, 2); 182 INIT_LIST_HEAD(&sqd->ctx_list); 183 mutex_init(&sqd->lock); 184 init_waitqueue_head(&sqd->wait); 185 return sqd; 186 } 187 188 static inline bool io_sqd_events_pending(struct io_sq_data *sqd) 189 { 190 return READ_ONCE(sqd->state); 191 } 192 193 struct io_sq_time { 194 bool started; 195 u64 usec; 196 }; 197 198 u64 io_sq_cpu_usec(struct task_struct *tsk) 199 { 200 u64 utime, stime; 201 202 task_cputime_adjusted(tsk, &utime, &stime); 203 do_div(stime, 1000); 204 return stime; 205 } 206 207 static void io_sq_update_worktime(struct io_sq_data *sqd, struct io_sq_time *ist) 208 { 209 if (!ist->started) 210 return; 211 ist->started = false; 212 sqd->work_time += io_sq_cpu_usec(current) - ist->usec; 213 } 214 215 static void io_sq_start_worktime(struct io_sq_time *ist) 216 { 217 if (ist->started) 218 return; 219 ist->started = true; 220 ist->usec = io_sq_cpu_usec(current); 221 } 222 223 static int __io_sq_thread(struct io_ring_ctx *ctx, struct io_sq_data *sqd, 224 bool cap_entries, struct io_sq_time *ist) 225 { 226 unsigned int to_submit; 227 int ret = 0; 228 229 to_submit = io_sqring_entries(ctx); 230 /* if we're handling multiple rings, cap submit size for fairness */ 231 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) 232 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; 233 234 if (to_submit || !list_empty(&ctx->iopoll_list)) { 235 const struct cred *creds = NULL; 236 237 io_sq_start_worktime(ist); 238 239 if (ctx->sq_creds != current_cred()) 240 creds = override_creds(ctx->sq_creds); 241 242 mutex_lock(&ctx->uring_lock); 243 if (!list_empty(&ctx->iopoll_list)) 244 io_do_iopoll(ctx, true); 245 246 /* 247 * Don't submit if refs are dying, good for io_uring_register(), 248 * but also it is relied upon by io_ring_exit_work() 249 */ 250 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && 251 !(ctx->flags & IORING_SETUP_R_DISABLED)) 252 ret = io_submit_sqes(ctx, to_submit); 253 mutex_unlock(&ctx->uring_lock); 254 255 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) 256 wake_up(&ctx->sqo_sq_wait); 257 if (creds) 258 revert_creds(creds); 259 } 260 261 return ret; 262 } 263 264 static bool io_sqd_handle_event(struct io_sq_data *sqd) 265 { 266 bool did_sig = false; 267 struct ksignal ksig; 268 269 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || 270 signal_pending(current)) { 271 mutex_unlock(&sqd->lock); 272 if (signal_pending(current)) 273 did_sig = get_signal(&ksig); 274 wait_event(sqd->wait, !atomic_read(&sqd->park_pending)); 275 mutex_lock(&sqd->lock); 276 sqd->sq_cpu = raw_smp_processor_id(); 277 } 278 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); 279 } 280 281 /* 282 * Run task_work, processing no more than max_entries at a time. If more 283 * than that is pending, it simply stays on the queue for the next run. 284 */ 285 static unsigned int io_sq_tw(int max_entries) 286 { 287 struct io_uring_task *tctx = current->io_uring; 288 unsigned int count = 0; 289 290 tctx_task_work_run(tctx, max_entries, &count); 291 if (task_work_pending(current)) 292 task_work_run(); 293 return count; 294 } 295 296 static bool io_sq_tw_pending(void) 297 { 298 struct io_uring_task *tctx = current->io_uring; 299 300 return !mpscq_empty(&tctx->task_list); 301 } 302 303 static int io_sq_thread(void *data) 304 { 305 struct io_sq_data *sqd = data; 306 struct io_ring_ctx *ctx; 307 unsigned long timeout = 0; 308 char buf[TASK_COMM_LEN] = {}; 309 DEFINE_WAIT(wait); 310 311 /* offload context creation failed, just exit */ 312 if (!current->io_uring) { 313 mutex_lock(&sqd->lock); 314 rcu_assign_pointer(sqd->thread, NULL); 315 put_task_struct(current); 316 mutex_unlock(&sqd->lock); 317 goto err_out; 318 } 319 320 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); 321 set_task_comm(current, buf); 322 323 /* reset to our pid after we've set task_comm, for fdinfo */ 324 sqd->task_pid = current->pid; 325 326 if (sqd->sq_cpu != -1) { 327 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); 328 } else { 329 set_cpus_allowed_ptr(current, cpu_online_mask); 330 sqd->sq_cpu = raw_smp_processor_id(); 331 } 332 333 /* 334 * Force audit context to get setup, in case we do prep side async 335 * operations that would trigger an audit call before any issue side 336 * audit has been done. 337 */ 338 audit_uring_entry(IORING_OP_NOP); 339 audit_uring_exit(true, 0); 340 341 mutex_lock(&sqd->lock); 342 while (1) { 343 bool cap_entries, sqt_spin = false; 344 struct io_sq_time ist = { }; 345 346 if (io_sqd_events_pending(sqd) || signal_pending(current)) { 347 if (io_sqd_handle_event(sqd)) 348 break; 349 timeout = jiffies + sqd->sq_thread_idle; 350 } 351 352 /* 353 * The SQPOLL thread never returns to userspace, so do the 354 * return-to-userspace work, like memory.high reclaim, here. 355 * It can sleep for a long time, so drop sqd->lock. 356 */ 357 if (test_thread_flag(TIF_NOTIFY_RESUME)) { 358 mutex_unlock(&sqd->lock); 359 resume_user_mode_work(NULL); 360 mutex_lock(&sqd->lock); 361 sqd->sq_cpu = raw_smp_processor_id(); 362 } 363 364 cap_entries = !list_is_singular(&sqd->ctx_list); 365 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { 366 int ret; 367 368 kcov_remote_start_common(ctx->kcov_handle); 369 ret = __io_sq_thread(ctx, sqd, cap_entries, &ist); 370 371 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) 372 sqt_spin = true; 373 kcov_remote_stop(); 374 } 375 if (io_sq_tw(IORING_TW_CAP_ENTRIES_VALUE)) 376 sqt_spin = true; 377 378 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { 379 if (io_napi(ctx)) { 380 io_sq_start_worktime(&ist); 381 io_napi_sqpoll_busy_poll(ctx); 382 } 383 } 384 385 io_sq_update_worktime(sqd, &ist); 386 387 if (sqt_spin || !time_after(jiffies, timeout)) { 388 if (sqt_spin) 389 timeout = jiffies + sqd->sq_thread_idle; 390 if (unlikely(need_resched())) { 391 mutex_unlock(&sqd->lock); 392 cond_resched(); 393 mutex_lock(&sqd->lock); 394 sqd->sq_cpu = raw_smp_processor_id(); 395 } 396 continue; 397 } 398 399 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); 400 if (!io_sqd_events_pending(sqd) && !io_sq_tw_pending()) { 401 bool needs_sched = true; 402 403 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { 404 atomic_or(IORING_SQ_NEED_WAKEUP, 405 &ctx->rings->sq_flags); 406 if ((ctx->flags & IORING_SETUP_IOPOLL) && 407 !list_empty(&ctx->iopoll_list)) { 408 needs_sched = false; 409 break; 410 } 411 412 /* 413 * Ensure the store of the wakeup flag is not 414 * reordered with the load of the SQ tail 415 */ 416 smp_mb__after_atomic(); 417 418 /* 419 * Nothing gets submitted before the ring is 420 * enabled, which comes with a wake up. 421 */ 422 if (READ_ONCE(ctx->flags) & IORING_SETUP_R_DISABLED) 423 continue; 424 425 if (io_sqring_entries(ctx)) { 426 needs_sched = false; 427 break; 428 } 429 } 430 431 if (needs_sched) { 432 mutex_unlock(&sqd->lock); 433 schedule(); 434 mutex_lock(&sqd->lock); 435 sqd->sq_cpu = raw_smp_processor_id(); 436 } 437 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) 438 atomic_andnot(IORING_SQ_NEED_WAKEUP, 439 &ctx->rings->sq_flags); 440 } 441 442 finish_wait(&sqd->wait, &wait); 443 timeout = jiffies + sqd->sq_thread_idle; 444 } 445 446 if (io_sq_tw_pending()) 447 io_sq_tw(UINT_MAX); 448 449 /* 450 * Cancelation drops sqd->lock while it waits, and a ring could attach 451 * to a thread that is on its way out. A fatal signal doesn't set the 452 * stop bit, so mark the thread as exiting here for 453 * io_sq_offload_create() to see. 454 */ 455 set_bit(IO_SQ_THREAD_EXITING, &sqd->state); 456 io_uring_cancel_generic(true, sqd); 457 rcu_assign_pointer(sqd->thread, NULL); 458 put_task_struct(current); 459 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) 460 atomic_or(IORING_SQ_NEED_WAKEUP, &ctx->rings->sq_flags); 461 io_run_task_work(); 462 mutex_unlock(&sqd->lock); 463 err_out: 464 io_sq_data_release(sqd); 465 do_exit(0); 466 } 467 468 void io_sqpoll_wait_sq(struct io_ring_ctx *ctx) 469 { 470 DEFINE_WAIT(wait); 471 472 do { 473 if (!io_sqring_full(ctx)) 474 break; 475 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); 476 477 if (!io_sqring_full(ctx)) 478 break; 479 schedule(); 480 } while (!signal_pending(current)); 481 482 finish_wait(&ctx->sqo_sq_wait, &wait); 483 } 484 485 __cold int io_sq_offload_create(struct io_ring_ctx *ctx, 486 struct io_uring_params *p) 487 { 488 int ret; 489 490 /* Retain compatibility with failing for an invalid attach attempt */ 491 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == 492 IORING_SETUP_ATTACH_WQ) { 493 CLASS(fd, f)(p->wq_fd); 494 if (fd_empty(f)) 495 return -ENXIO; 496 if (!io_is_uring_fops(fd_file(f))) 497 return -EINVAL; 498 } 499 if (ctx->flags & IORING_SETUP_SQPOLL) { 500 struct io_uring_task *tctx; 501 struct task_struct *tsk; 502 struct io_sq_data *sqd; 503 bool attached; 504 505 ret = security_uring_sqpoll(); 506 if (ret) 507 return ret; 508 509 sqd = io_get_sq_data(p, &attached); 510 if (IS_ERR(sqd)) { 511 ret = PTR_ERR(sqd); 512 goto err; 513 } 514 515 ctx->sq_creds = get_current_cred(); 516 ctx->sq_data = sqd; 517 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); 518 if (!ctx->sq_thread_idle) 519 ctx->sq_thread_idle = HZ; 520 521 io_sq_thread_park(sqd); 522 list_add(&ctx->sqd_list, &sqd->ctx_list); 523 io_sqd_update_thread_idle(sqd); 524 /* don't attach to a dying SQPOLL thread, would be racy */ 525 ret = 0; 526 if (attached && (!sqd->thread || 527 test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state) || 528 test_bit(IO_SQ_THREAD_EXITING, &sqd->state))) 529 ret = -ENXIO; 530 io_sq_thread_unpark(sqd); 531 532 if (ret < 0) 533 goto err; 534 if (attached) 535 return 0; 536 537 if (p->flags & IORING_SETUP_SQ_AFF) { 538 cpumask_var_t allowed_mask; 539 int cpu = p->sq_thread_cpu; 540 541 ret = -EINVAL; 542 if (cpu >= nr_cpu_ids || !cpu_online(cpu)) 543 goto err_sqpoll; 544 ret = -ENOMEM; 545 if (!alloc_cpumask_var(&allowed_mask, GFP_KERNEL)) 546 goto err_sqpoll; 547 ret = -EINVAL; 548 cpuset_cpus_allowed(current, allowed_mask); 549 if (!cpumask_test_cpu(cpu, allowed_mask)) { 550 free_cpumask_var(allowed_mask); 551 goto err_sqpoll; 552 } 553 free_cpumask_var(allowed_mask); 554 sqd->sq_cpu = cpu; 555 } else { 556 sqd->sq_cpu = -1; 557 } 558 559 sqd->task_pid = current->pid; 560 sqd->task_tgid = current->tgid; 561 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); 562 if (IS_ERR(tsk)) { 563 ret = PTR_ERR(tsk); 564 goto err_sqpoll; 565 } 566 567 mutex_lock(&sqd->lock); 568 rcu_assign_pointer(sqd->thread, tsk); 569 mutex_unlock(&sqd->lock); 570 571 ret = 0; 572 get_task_struct(tsk); 573 tctx = io_uring_alloc_task_context(tsk, ctx); 574 if (!IS_ERR(tctx)) 575 tsk->io_uring = tctx; 576 else 577 ret = PTR_ERR(tctx); 578 wake_up_new_task(tsk); 579 if (ret) 580 goto err; 581 } else if (p->flags & IORING_SETUP_SQ_AFF) { 582 /* Can't have SQ_AFF without SQPOLL */ 583 ret = -EINVAL; 584 goto err; 585 } 586 return 0; 587 err_sqpoll: 588 /* no thread was made: let go of its share */ 589 io_sq_data_release(ctx->sq_data); 590 err: 591 io_sq_thread_finish(ctx); 592 return ret; 593 } 594 595 __cold int io_sqpoll_wq_cpu_affinity(struct io_ring_ctx *ctx, 596 cpumask_var_t mask) 597 { 598 struct io_sq_data *sqd = ctx->sq_data; 599 int ret = -EINVAL; 600 601 if (sqd) { 602 struct task_struct *tsk; 603 604 io_sq_thread_park(sqd); 605 /* Don't set affinity for a dying thread */ 606 tsk = sqpoll_task_locked(sqd); 607 if (tsk) 608 ret = io_wq_cpu_affinity(tsk->io_uring, mask); 609 io_sq_thread_unpark(sqd); 610 } 611 612 return ret; 613 } 614