1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/kernel.h> 3 #include <linux/errno.h> 4 #include <linux/fs.h> 5 #include <linux/mm.h> 6 #include <linux/slab.h> 7 #include <linux/nospec.h> 8 #include <linux/io_uring.h> 9 10 #include <uapi/linux/io_uring.h> 11 12 #include "filetable.h" 13 #include "io_uring.h" 14 #include "tctx.h" 15 #include "sqpoll.h" 16 #include "uring_cmd.h" 17 #include "poll.h" 18 #include "timeout.h" 19 #include "waitid.h" 20 #include "futex.h" 21 #include "cancel.h" 22 #include "wait.h" 23 24 struct io_cancel { 25 struct file *file; 26 u64 addr; 27 u32 flags; 28 s32 fd; 29 u8 opcode; 30 }; 31 32 #define CANCEL_FLAGS (IORING_ASYNC_CANCEL_ALL | IORING_ASYNC_CANCEL_FD | \ 33 IORING_ASYNC_CANCEL_ANY | IORING_ASYNC_CANCEL_FD_FIXED | \ 34 IORING_ASYNC_CANCEL_USERDATA | IORING_ASYNC_CANCEL_OP) 35 36 /* 37 * Returns true if the request matches the criteria outlined by 'cd'. 38 */ 39 bool io_cancel_req_match(struct io_kiocb *req, struct io_cancel_data *cd) 40 { 41 bool match_user_data = cd->flags & IORING_ASYNC_CANCEL_USERDATA; 42 43 if (req->ctx != cd->ctx) 44 return false; 45 46 if (!(cd->flags & (IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_OP))) 47 match_user_data = true; 48 49 if (cd->flags & IORING_ASYNC_CANCEL_ANY) 50 goto check_seq; 51 if (cd->flags & IORING_ASYNC_CANCEL_FD) { 52 if (req->file != cd->file) 53 return false; 54 } 55 if (cd->flags & IORING_ASYNC_CANCEL_OP) { 56 if (req->opcode != cd->opcode) 57 return false; 58 } 59 if (match_user_data && req->cqe.user_data != cd->data) 60 return false; 61 if (cd->flags & IORING_ASYNC_CANCEL_ALL) { 62 check_seq: 63 if (io_cancel_match_sequence(req, cd->seq)) 64 return false; 65 } 66 67 return true; 68 } 69 70 static bool io_cancel_cb(struct io_wq_work *work, void *data) 71 { 72 struct io_kiocb *req = container_of(work, struct io_kiocb, work); 73 struct io_cancel_data *cd = data; 74 75 if (!io_cancel_req_match(req, cd)) 76 return false; 77 /* 78 * Pending work is gone once it has matched. Work that is running may 79 * park the request where the same cancellation looks next. Don't make 80 * it skip the request as one that it has dealt with. 81 */ 82 if (cd->flags & (IORING_ASYNC_CANCEL_ALL | IORING_ASYNC_CANCEL_ANY)) 83 req->cancel_seq_set = false; 84 return true; 85 } 86 87 static int io_async_cancel_one(struct io_uring_task *tctx, 88 struct io_cancel_data *cd) 89 { 90 enum io_wq_cancel cancel_ret; 91 int ret = 0; 92 bool all; 93 94 if (!tctx || !tctx->io_wq) 95 return -ENOENT; 96 97 all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY); 98 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, cd, all); 99 switch (cancel_ret) { 100 case IO_WQ_CANCEL_OK: 101 ret = 0; 102 break; 103 case IO_WQ_CANCEL_RUNNING: 104 ret = -EALREADY; 105 break; 106 case IO_WQ_CANCEL_NOTFOUND: 107 ret = -ENOENT; 108 break; 109 } 110 111 return ret; 112 } 113 114 int io_try_cancel(struct io_uring_task *tctx, struct io_cancel_data *cd, 115 unsigned issue_flags) 116 { 117 struct io_ring_ctx *ctx = cd->ctx; 118 int ret; 119 120 WARN_ON_ONCE(!io_wq_current_is_worker() && tctx != current->io_uring); 121 122 ret = io_async_cancel_one(tctx, cd); 123 /* 124 * Fall-through even for -EALREADY, as we may have poll armed 125 * that need unarming. 126 */ 127 if (!ret) 128 return 0; 129 if (ret == -EALREADY) 130 cd->running = true; 131 132 ret = io_poll_cancel(ctx, cd, issue_flags); 133 if (ret != -ENOENT) 134 return ret; 135 136 ret = io_waitid_cancel(ctx, cd, issue_flags); 137 if (ret != -ENOENT) 138 return ret; 139 140 ret = io_futex_cancel(ctx, cd, issue_flags); 141 if (ret != -ENOENT) 142 return ret; 143 144 spin_lock(&ctx->completion_lock); 145 if (!(cd->flags & IORING_ASYNC_CANCEL_FD)) 146 ret = io_timeout_cancel(ctx, cd); 147 spin_unlock(&ctx->completion_lock); 148 return ret; 149 } 150 151 int io_async_cancel_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 152 { 153 struct io_cancel *cancel = io_kiocb_to_cmd(req, struct io_cancel); 154 155 if (unlikely(req->flags & REQ_F_BUFFER_SELECT)) 156 return -EINVAL; 157 if (sqe->off || sqe->splice_fd_in) 158 return -EINVAL; 159 160 cancel->addr = READ_ONCE(sqe->addr); 161 cancel->flags = READ_ONCE(sqe->cancel_flags); 162 if (cancel->flags & ~CANCEL_FLAGS) 163 return -EINVAL; 164 if (cancel->flags & IORING_ASYNC_CANCEL_FD) { 165 if (cancel->flags & IORING_ASYNC_CANCEL_ANY) 166 return -EINVAL; 167 cancel->fd = READ_ONCE(sqe->fd); 168 } 169 if (cancel->flags & IORING_ASYNC_CANCEL_OP) { 170 u32 op; 171 172 if (cancel->flags & IORING_ASYNC_CANCEL_ANY) 173 return -EINVAL; 174 175 op = READ_ONCE(sqe->len); 176 if (op >= IORING_OP_LAST) 177 return -EINVAL; 178 179 cancel->opcode = op; 180 } 181 182 return 0; 183 } 184 185 static int __io_async_cancel(struct io_cancel_data *cd, 186 struct io_uring_task *tctx, 187 unsigned int issue_flags) 188 { 189 bool all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY); 190 struct io_ring_ctx *ctx = cd->ctx; 191 struct io_tctx_node *node; 192 int ret, nr = 0; 193 194 cd->running = false; 195 do { 196 ret = io_try_cancel(tctx, cd, issue_flags); 197 if (ret == -ENOENT) 198 break; 199 if (!all) 200 return ret; 201 /* poll, futex and waitid return how many they cancelled */ 202 nr += max(ret, 1); 203 } while (1); 204 205 /* slow path, try all io-wq's */ 206 __set_current_state(TASK_RUNNING); 207 io_ring_submit_lock(ctx, issue_flags); 208 mutex_lock(&ctx->tctx_lock); 209 ret = -ENOENT; 210 list_for_each_entry(node, &ctx->tctx_list, ctx_node) { 211 /* has been done above, don't count what is running there */ 212 if (all && node->task->io_uring == tctx) 213 continue; 214 ret = io_async_cancel_one(node->task->io_uring, cd); 215 if (ret != -ENOENT) { 216 if (!all) 217 break; 218 nr++; 219 } 220 } 221 mutex_unlock(&ctx->tctx_lock); 222 io_ring_submit_unlock(ctx, issue_flags); 223 if (all) 224 return nr; 225 /* 226 * The worker may be done with the work that was found running, and 227 * flagged, above. That isn't the same as not having found it. 228 */ 229 if (ret == -ENOENT && cd->running) 230 ret = -EALREADY; 231 return ret; 232 } 233 234 int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) 235 { 236 struct io_cancel *cancel = io_kiocb_to_cmd(req, struct io_cancel); 237 struct io_cancel_data cd = { 238 .ctx = req->ctx, 239 .data = cancel->addr, 240 .flags = cancel->flags, 241 .opcode = cancel->opcode, 242 .seq = atomic_inc_return(&req->ctx->cancel_seq), 243 }; 244 struct io_uring_task *tctx = req->tctx; 245 int ret; 246 247 if (cd.flags & IORING_ASYNC_CANCEL_FD) { 248 if (req->flags & REQ_F_FIXED_FILE || 249 cd.flags & IORING_ASYNC_CANCEL_FD_FIXED) { 250 req->flags |= REQ_F_FIXED_FILE; 251 req->file = io_file_get_fixed(req, cancel->fd, 252 issue_flags); 253 } else { 254 req->file = io_file_get_normal(req, cancel->fd); 255 } 256 if (!req->file) { 257 ret = -EBADF; 258 goto done; 259 } 260 cd.file = req->file; 261 } 262 263 ret = __io_async_cancel(&cd, tctx, issue_flags); 264 done: 265 if (ret < 0) 266 req_set_fail(req); 267 io_req_set_res(req, ret, 0); 268 return IOU_COMPLETE; 269 } 270 271 static int __io_sync_cancel(struct io_uring_task *tctx, 272 struct io_cancel_data *cd, int fd) 273 { 274 struct io_ring_ctx *ctx = cd->ctx; 275 276 /* fixed must be grabbed every time since we drop the uring_lock */ 277 if ((cd->flags & IORING_ASYNC_CANCEL_FD) && 278 (cd->flags & IORING_ASYNC_CANCEL_FD_FIXED)) { 279 struct io_rsrc_node *node; 280 281 node = io_rsrc_node_lookup(&ctx->file_table.data, fd); 282 if (unlikely(!node)) 283 return -EBADF; 284 cd->file = io_slot_file(node); 285 if (!cd->file) 286 return -EBADF; 287 } 288 289 return __io_async_cancel(cd, tctx, 0); 290 } 291 292 int io_sync_cancel(struct io_ring_ctx *ctx, void __user *arg) 293 __must_hold(&ctx->uring_lock) 294 { 295 struct io_cancel_data cd = { 296 .ctx = ctx, 297 .seq = atomic_inc_return(&ctx->cancel_seq), 298 }; 299 ktime_t timeout = KTIME_MAX; 300 struct io_uring_sync_cancel_reg sc; 301 struct file *file = NULL; 302 DEFINE_WAIT(wait); 303 int ret, i; 304 305 if (copy_from_user(&sc, arg, sizeof(sc))) 306 return -EFAULT; 307 if (sc.flags & ~CANCEL_FLAGS) 308 return -EINVAL; 309 for (i = 0; i < ARRAY_SIZE(sc.pad); i++) 310 if (sc.pad[i]) 311 return -EINVAL; 312 for (i = 0; i < ARRAY_SIZE(sc.pad2); i++) 313 if (sc.pad2[i]) 314 return -EINVAL; 315 316 cd.data = sc.addr; 317 cd.flags = sc.flags; 318 cd.opcode = sc.opcode; 319 320 /* we can grab a normal file descriptor upfront */ 321 if ((cd.flags & IORING_ASYNC_CANCEL_FD) && 322 !(cd.flags & IORING_ASYNC_CANCEL_FD_FIXED)) { 323 file = fget(sc.fd); 324 if (!file) 325 return -EBADF; 326 cd.file = file; 327 } 328 329 ret = __io_sync_cancel(current->io_uring, &cd, sc.fd); 330 331 /* found something, done! */ 332 if (ret != -EALREADY) 333 goto out; 334 335 if (sc.timeout.tv_sec != -1 || sc.timeout.tv_nsec != -1) { 336 struct timespec64 ts = { 337 .tv_sec = sc.timeout.tv_sec, 338 .tv_nsec = sc.timeout.tv_nsec 339 }; 340 341 timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); 342 } 343 344 /* 345 * Keep looking until we get -ENOENT. we'll get woken everytime 346 * every time a request completes and will retry the cancelation. 347 */ 348 do { 349 cd.seq = atomic_inc_return(&ctx->cancel_seq); 350 351 prepare_to_wait(&ctx->cq_wait, &wait, TASK_INTERRUPTIBLE); 352 353 ret = __io_sync_cancel(current->io_uring, &cd, sc.fd); 354 355 mutex_unlock(&ctx->uring_lock); 356 if (ret != -EALREADY) 357 break; 358 359 ret = io_run_task_work_sig(ctx); 360 if (ret < 0) 361 break; 362 ret = schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS); 363 if (!ret) { 364 ret = -ETIME; 365 break; 366 } 367 mutex_lock(&ctx->uring_lock); 368 } while (1); 369 370 finish_wait(&ctx->cq_wait, &wait); 371 mutex_lock(&ctx->uring_lock); 372 373 if (ret == -ENOENT || ret > 0) 374 ret = 0; 375 out: 376 if (file) 377 fput(file); 378 return ret; 379 } 380 381 bool io_cancel_remove_all(struct io_ring_ctx *ctx, struct io_uring_task *tctx, 382 struct hlist_head *list, bool cancel_all, 383 bool (*cancel)(struct io_kiocb *)) 384 { 385 struct hlist_node *tmp; 386 struct io_kiocb *req; 387 bool found = false; 388 389 lockdep_assert_held(&ctx->uring_lock); 390 391 hlist_for_each_entry_safe(req, tmp, list, hash_node) { 392 if (!io_match_task_safe(req, tctx, cancel_all)) 393 continue; 394 hlist_del_init(&req->hash_node); 395 if (cancel(req)) 396 found = true; 397 } 398 399 return found; 400 } 401 402 int io_cancel_remove(struct io_ring_ctx *ctx, struct io_cancel_data *cd, 403 unsigned int issue_flags, struct hlist_head *list, 404 bool (*cancel)(struct io_kiocb *)) 405 { 406 struct hlist_node *tmp; 407 struct io_kiocb *req; 408 int nr = 0; 409 410 io_ring_submit_lock(ctx, issue_flags); 411 hlist_for_each_entry_safe(req, tmp, list, hash_node) { 412 if (!io_cancel_req_match(req, cd)) 413 continue; 414 if (cancel(req)) 415 nr++; 416 if (!(cd->flags & IORING_ASYNC_CANCEL_ALL)) 417 break; 418 } 419 io_ring_submit_unlock(ctx, issue_flags); 420 return nr ?: -ENOENT; 421 } 422 423 static bool io_match_linked(struct io_kiocb *head) 424 { 425 struct io_kiocb *req; 426 427 io_for_each_link(req, head) { 428 if (req->flags & REQ_F_INFLIGHT) 429 return true; 430 } 431 return false; 432 } 433 434 /* 435 * As io_match_task() but protected against racing with linked timeouts. 436 * User must not hold timeout_lock. 437 */ 438 bool io_match_task_safe(struct io_kiocb *head, struct io_uring_task *tctx, 439 bool cancel_all) 440 { 441 bool matched; 442 443 if (tctx && head->tctx != tctx) 444 return false; 445 if (cancel_all) 446 return true; 447 448 if (head->flags & REQ_F_LINK_TIMEOUT) { 449 struct io_ring_ctx *ctx = head->ctx; 450 451 /* protect against races with linked timeouts */ 452 raw_spin_lock_irq(&ctx->timeout_lock); 453 matched = io_match_linked(head); 454 raw_spin_unlock_irq(&ctx->timeout_lock); 455 } else { 456 matched = io_match_linked(head); 457 } 458 return matched; 459 } 460 461 void __io_uring_cancel(bool cancel_all) 462 { 463 io_uring_unreg_ringfd(); 464 io_uring_cancel_generic(cancel_all, NULL); 465 } 466 467 struct io_task_cancel { 468 struct io_uring_task *tctx; 469 bool all; 470 }; 471 472 static bool io_cancel_task_cb(struct io_wq_work *work, void *data) 473 { 474 struct io_kiocb *req = container_of(work, struct io_kiocb, work); 475 struct io_task_cancel *cancel = data; 476 477 return io_match_task_safe(req, cancel->tctx, cancel->all); 478 } 479 480 static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, 481 struct io_uring_task *tctx, 482 bool cancel_all) 483 { 484 struct io_defer_entry *de; 485 LIST_HEAD(list); 486 487 list_for_each_entry_reverse(de, &ctx->defer_list, list) { 488 if (io_match_task_safe(de->req, tctx, cancel_all)) { 489 list_cut_position(&list, &ctx->defer_list, &de->list); 490 break; 491 } 492 } 493 if (list_empty(&list)) 494 return false; 495 496 while (!list_empty(&list)) { 497 de = list_first_entry(&list, struct io_defer_entry, list); 498 list_del_init(&de->list); 499 ctx->nr_drained -= io_linked_nr(de->req); 500 io_req_task_queue_fail(de->req, -ECANCELED); 501 kfree(de); 502 } 503 return true; 504 } 505 506 __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) 507 { 508 struct io_kiocb *req = container_of(work, struct io_kiocb, work); 509 510 return req->ctx == data; 511 } 512 513 static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) 514 { 515 struct io_tctx_node *node; 516 enum io_wq_cancel cret; 517 bool ret = false; 518 519 mutex_lock(&ctx->uring_lock); 520 mutex_lock(&ctx->tctx_lock); 521 list_for_each_entry(node, &ctx->tctx_list, ctx_node) { 522 struct io_uring_task *tctx = node->task->io_uring; 523 524 /* 525 * io_wq will stay alive while we hold uring_lock, because it's 526 * killed after ctx nodes, which requires to take the lock. 527 */ 528 if (!tctx || !tctx->io_wq) 529 continue; 530 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); 531 ret |= (cret != IO_WQ_CANCEL_NOTFOUND); 532 } 533 mutex_unlock(&ctx->tctx_lock); 534 mutex_unlock(&ctx->uring_lock); 535 536 return ret; 537 } 538 539 __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx, 540 struct io_uring_task *tctx, 541 unsigned int flags) 542 { 543 bool cancel_all = flags & IO_CANCEL_ALL; 544 struct io_task_cancel cancel = { .tctx = tctx, .all = cancel_all, }; 545 enum io_wq_cancel cret; 546 bool ret = false; 547 548 /* set it so io_req_local_work_add() would wake us up */ 549 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) { 550 atomic_set(&ctx->cq_wait_nr, 1); 551 smp_mb(); 552 } 553 554 /* failed during ring init, it couldn't have issued any requests */ 555 if (!ctx->rings) 556 return false; 557 558 if (!tctx) { 559 ret |= io_uring_try_cancel_iowq(ctx); 560 } else if (tctx->io_wq) { 561 /* 562 * Cancels requests of all rings, not only @ctx, but 563 * it's fine as the task is in exit/exec. 564 */ 565 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, 566 &cancel, true); 567 ret |= (cret != IO_WQ_CANCEL_NOTFOUND); 568 } 569 570 /* SQPOLL thread does its own polling */ 571 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || 572 (flags & IO_CANCEL_SQPOLL)) { 573 while (!list_empty(&ctx->iopoll_list)) { 574 io_iopoll_try_reap_events(ctx); 575 ret = true; 576 cond_resched(); 577 } 578 } 579 580 if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) && 581 io_allowed_defer_tw_run(ctx)) 582 ret |= io_run_local_work(ctx, INT_MAX, INT_MAX) > 0; 583 mutex_lock(&ctx->uring_lock); 584 ret |= io_cancel_defer_files(ctx, tctx, cancel_all); 585 ret |= io_poll_remove_all(ctx, tctx, cancel_all); 586 ret |= io_waitid_remove_all(ctx, tctx, cancel_all); 587 ret |= io_futex_remove_all(ctx, tctx, cancel_all); 588 ret |= io_uring_try_cancel_uring_cmd(ctx, tctx); 589 if (flags & IO_CANCEL_KEEP_TIMEOUTS) 590 cancel_all = false; 591 ret |= io_kill_timeouts(ctx, tctx, cancel_all); 592 mutex_unlock(&ctx->uring_lock); 593 if (tctx) 594 ret |= io_run_task_work() > 0; 595 return ret; 596 } 597 598 static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) 599 { 600 if (tracked) 601 return atomic_read(&tctx->inflight_tracked); 602 return percpu_counter_sum(&tctx->inflight); 603 } 604 605 /* 606 * If true, whole thread group is exiting, at which point no task is left that 607 * can reap completions and care about requests in-flight. 608 */ 609 static bool io_task_group_exiting(void) 610 { 611 return current->signal->flags & SIGNAL_GROUP_EXIT; 612 } 613 614 /* 615 * Return a count of requests an exiting task should wait for. 616 */ 617 static s64 tctx_inflight_exit(struct io_uring_task *tctx) 618 { 619 struct io_tctx_node *node; 620 unsigned long index; 621 s64 inflight; 622 623 inflight = tctx_inflight(tctx, false); 624 xa_for_each(&tctx->xa, index, node) { 625 /* unlocked read is fine, the caller re-evaluates until done */ 626 inflight -= data_race(node->ctx->nr_notifs); 627 /* takes ->uring_lock, we hold nothing on the group exit path */ 628 inflight -= io_timeouts_armed(node->ctx, tctx); 629 } 630 return inflight; 631 } 632 633 static bool io_tctx_cancel_done(struct io_uring_task *tctx, bool cancel_all, 634 bool group_exit) 635 { 636 if (cancel_all) 637 return !tctx_inflight(tctx, false); 638 if (tctx_inflight(tctx, true)) 639 return false; 640 return !group_exit || tctx_inflight_exit(tctx) <= 0; 641 } 642 643 /* 644 * Find any io_uring ctx that this task has registered or done IO on, and cancel 645 * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. 646 */ 647 __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) 648 { 649 struct io_uring_task *tctx = current->io_uring; 650 struct io_ring_ctx *ctx; 651 struct io_tctx_node *node; 652 unsigned int flags = 0; 653 unsigned long index; 654 bool group_exit; 655 s64 inflight; 656 DEFINE_WAIT(wait); 657 658 WARN_ON_ONCE(sqd && sqpoll_task_locked(sqd) != current); 659 660 if (!current->io_uring) 661 return; 662 if (tctx->io_wq) 663 io_wq_exit_start(tctx->io_wq); 664 665 /* 666 * If a whole thread group is exiting, nobody will look at completions. 667 * If a single thread is exiting, cancel only those that belong to that 668 * thread. 669 */ 670 group_exit = !cancel_all && io_task_group_exiting(); 671 if (cancel_all) 672 flags = IO_CANCEL_ALL; 673 else if (group_exit) 674 flags = IO_CANCEL_ALL | IO_CANCEL_KEEP_TIMEOUTS; 675 if (sqd) 676 flags |= IO_CANCEL_SQPOLL; 677 678 atomic_inc(&tctx->in_cancel); 679 do { 680 bool loop = false; 681 unsigned int state; 682 683 io_uring_drop_tctx_refs(current); 684 if (io_tctx_cancel_done(tctx, cancel_all, group_exit)) 685 break; 686 687 /* read completions before cancelations */ 688 inflight = tctx_inflight(tctx, false); 689 690 if (!sqd) { 691 xa_for_each(&tctx->xa, index, node) { 692 /* sqpoll task will cancel all its requests */ 693 if (node->ctx->sq_data) 694 continue; 695 loop |= io_uring_try_cancel_requests(node->ctx, 696 current->io_uring, 697 flags); 698 } 699 } else { 700 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) 701 loop |= io_uring_try_cancel_requests(ctx, 702 current->io_uring, 703 flags); 704 } 705 706 if (loop) { 707 /* 708 * A request that runs in io-wq and doesn't end makes 709 * each round come here. Don't hold sqd->lock for that 710 * long either. 711 */ 712 if (sqd) 713 mutex_unlock(&sqd->lock); 714 cond_resched(); 715 if (sqd) 716 mutex_lock(&sqd->lock); 717 continue; 718 } 719 720 state = TASK_INTERRUPTIBLE; 721 if (task_sigpending(current)) 722 state = TASK_UNINTERRUPTIBLE; 723 if (!cancel_all) 724 state |= TASK_FREEZABLE; 725 prepare_to_wait(&tctx->wait, &wait, state); 726 io_run_task_work(); 727 io_uring_drop_tctx_refs(current); 728 /* only skip the sleep for local work this task may run */ 729 xa_for_each(&tctx->xa, index, node) { 730 if (!(node->ctx->flags & IORING_SETUP_DEFER_TASKRUN) || 731 !io_allowed_defer_tw_run(node->ctx)) 732 continue; 733 /* 734 * The wake up io_uring_try_cancel_requests() asked for 735 * may have been issued while we were still running. Ask 736 * again now that it can't be missed. 737 */ 738 atomic_set(&node->ctx->cq_wait_nr, 1); 739 /* pairs with the xchg() in mpscq_push() */ 740 smp_mb(); 741 if (io_local_work_pending(node->ctx)) 742 goto end_wait; 743 } 744 /* 745 * If we've seen completions, retry without waiting. This 746 * avoids a race where a completion comes in before we did 747 * prepare_to_wait(). 748 */ 749 if (inflight == tctx_inflight(tctx, false)) { 750 unsigned long timeout = 1; 751 752 if (state & TASK_INTERRUPTIBLE) 753 timeout = MAX_SCHEDULE_TIMEOUT; 754 /* 755 * A zero-copy notification can't be canceled, and a 756 * peer that doesn't read keeps it pending. Don't hold 757 * sqd->lock while waiting for it, or whoever wants the 758 * lock sleeps as long. No ring is used across this, 759 * the next round walks ->ctx_list anew. 760 */ 761 if (sqd) 762 mutex_unlock(&sqd->lock); 763 schedule_timeout(timeout); 764 if (sqd) 765 mutex_lock(&sqd->lock); 766 } 767 end_wait: 768 finish_wait(&tctx->wait, &wait); 769 } while (1); 770 771 io_uring_clean_tctx(tctx); 772 if (cancel_all) { 773 /* 774 * We shouldn't run task_works after cancel, so just leave 775 * ->in_cancel set for normal exit. 776 */ 777 atomic_dec(&tctx->in_cancel); 778 /* for exec all current's requests should be gone, kill tctx */ 779 io_uring_free_tctx(current); 780 } 781 } 782