cancel.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. #include <uapi/linux/io_uring.h>
  10. #include "filetable.h"
  11. #include "io_uring.h"
  12. #include "tctx.h"
  13. #include "sqpoll.h"
  14. #include "uring_cmd.h"
  15. #include "poll.h"
  16. #include "timeout.h"
  17. #include "waitid.h"
  18. #include "futex.h"
  19. #include "cancel.h"
  20. #include "wait.h"
  21. struct io_cancel {
  22. struct file *file;
  23. u64 addr;
  24. u32 flags;
  25. s32 fd;
  26. u8 opcode;
  27. };
  28. #define CANCEL_FLAGS (IORING_ASYNC_CANCEL_ALL | IORING_ASYNC_CANCEL_FD | \
  29. IORING_ASYNC_CANCEL_ANY | IORING_ASYNC_CANCEL_FD_FIXED | \
  30. IORING_ASYNC_CANCEL_USERDATA | IORING_ASYNC_CANCEL_OP)
  31. /*
  32. * Returns true if the request matches the criteria outlined by 'cd'.
  33. */
  34. bool io_cancel_req_match(struct io_kiocb *req, struct io_cancel_data *cd)
  35. {
  36. bool match_user_data = cd->flags & IORING_ASYNC_CANCEL_USERDATA;
  37. if (req->ctx != cd->ctx)
  38. return false;
  39. if (!(cd->flags & (IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_OP)))
  40. match_user_data = true;
  41. if (cd->flags & IORING_ASYNC_CANCEL_ANY)
  42. goto check_seq;
  43. if (cd->flags & IORING_ASYNC_CANCEL_FD) {
  44. if (req->file != cd->file)
  45. return false;
  46. }
  47. if (cd->flags & IORING_ASYNC_CANCEL_OP) {
  48. if (req->opcode != cd->opcode)
  49. return false;
  50. }
  51. if (match_user_data && req->cqe.user_data != cd->data)
  52. return false;
  53. if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
  54. check_seq:
  55. if (io_cancel_match_sequence(req, cd->seq))
  56. return false;
  57. }
  58. return true;
  59. }
  60. static bool io_cancel_cb(struct io_wq_work *work, void *data)
  61. {
  62. struct io_kiocb *req = container_of(work, struct io_kiocb, work);
  63. struct io_cancel_data *cd = data;
  64. return io_cancel_req_match(req, cd);
  65. }
  66. static int io_async_cancel_one(struct io_uring_task *tctx,
  67. struct io_cancel_data *cd)
  68. {
  69. enum io_wq_cancel cancel_ret;
  70. int ret = 0;
  71. bool all;
  72. if (!tctx || !tctx->io_wq)
  73. return -ENOENT;
  74. all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY);
  75. cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, cd, all);
  76. switch (cancel_ret) {
  77. case IO_WQ_CANCEL_OK:
  78. ret = 0;
  79. break;
  80. case IO_WQ_CANCEL_RUNNING:
  81. ret = -EALREADY;
  82. break;
  83. case IO_WQ_CANCEL_NOTFOUND:
  84. ret = -ENOENT;
  85. break;
  86. }
  87. return ret;
  88. }
  89. int io_try_cancel(struct io_uring_task *tctx, struct io_cancel_data *cd,
  90. unsigned issue_flags)
  91. {
  92. struct io_ring_ctx *ctx = cd->ctx;
  93. int ret;
  94. WARN_ON_ONCE(!io_wq_current_is_worker() && tctx != current->io_uring);
  95. ret = io_async_cancel_one(tctx, cd);
  96. /*
  97. * Fall-through even for -EALREADY, as we may have poll armed
  98. * that need unarming.
  99. */
  100. if (!ret)
  101. return 0;
  102. ret = io_poll_cancel(ctx, cd, issue_flags);
  103. if (ret != -ENOENT)
  104. return ret;
  105. ret = io_waitid_cancel(ctx, cd, issue_flags);
  106. if (ret != -ENOENT)
  107. return ret;
  108. ret = io_futex_cancel(ctx, cd, issue_flags);
  109. if (ret != -ENOENT)
  110. return ret;
  111. spin_lock(&ctx->completion_lock);
  112. if (!(cd->flags & IORING_ASYNC_CANCEL_FD))
  113. ret = io_timeout_cancel(ctx, cd);
  114. spin_unlock(&ctx->completion_lock);
  115. return ret;
  116. }
  117. int io_async_cancel_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  118. {
  119. struct io_cancel *cancel = io_kiocb_to_cmd(req, struct io_cancel);
  120. if (unlikely(req->flags & REQ_F_BUFFER_SELECT))
  121. return -EINVAL;
  122. if (sqe->off || sqe->splice_fd_in)
  123. return -EINVAL;
  124. cancel->addr = READ_ONCE(sqe->addr);
  125. cancel->flags = READ_ONCE(sqe->cancel_flags);
  126. if (cancel->flags & ~CANCEL_FLAGS)
  127. return -EINVAL;
  128. if (cancel->flags & IORING_ASYNC_CANCEL_FD) {
  129. if (cancel->flags & IORING_ASYNC_CANCEL_ANY)
  130. return -EINVAL;
  131. cancel->fd = READ_ONCE(sqe->fd);
  132. }
  133. if (cancel->flags & IORING_ASYNC_CANCEL_OP) {
  134. if (cancel->flags & IORING_ASYNC_CANCEL_ANY)
  135. return -EINVAL;
  136. cancel->opcode = READ_ONCE(sqe->len);
  137. }
  138. return 0;
  139. }
  140. static int __io_async_cancel(struct io_cancel_data *cd,
  141. struct io_uring_task *tctx,
  142. unsigned int issue_flags)
  143. {
  144. bool all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY);
  145. struct io_ring_ctx *ctx = cd->ctx;
  146. struct io_tctx_node *node;
  147. int ret, nr = 0;
  148. do {
  149. ret = io_try_cancel(tctx, cd, issue_flags);
  150. if (ret == -ENOENT)
  151. break;
  152. if (!all)
  153. return ret;
  154. nr++;
  155. } while (1);
  156. /* slow path, try all io-wq's */
  157. __set_current_state(TASK_RUNNING);
  158. io_ring_submit_lock(ctx, issue_flags);
  159. mutex_lock(&ctx->tctx_lock);
  160. ret = -ENOENT;
  161. list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
  162. ret = io_async_cancel_one(node->task->io_uring, cd);
  163. if (ret != -ENOENT) {
  164. if (!all)
  165. break;
  166. nr++;
  167. }
  168. }
  169. mutex_unlock(&ctx->tctx_lock);
  170. io_ring_submit_unlock(ctx, issue_flags);
  171. return all ? nr : ret;
  172. }
  173. int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
  174. {
  175. struct io_cancel *cancel = io_kiocb_to_cmd(req, struct io_cancel);
  176. struct io_cancel_data cd = {
  177. .ctx = req->ctx,
  178. .data = cancel->addr,
  179. .flags = cancel->flags,
  180. .opcode = cancel->opcode,
  181. .seq = atomic_inc_return(&req->ctx->cancel_seq),
  182. };
  183. struct io_uring_task *tctx = req->tctx;
  184. int ret;
  185. if (cd.flags & IORING_ASYNC_CANCEL_FD) {
  186. if (req->flags & REQ_F_FIXED_FILE ||
  187. cd.flags & IORING_ASYNC_CANCEL_FD_FIXED) {
  188. req->flags |= REQ_F_FIXED_FILE;
  189. req->file = io_file_get_fixed(req, cancel->fd,
  190. issue_flags);
  191. } else {
  192. req->file = io_file_get_normal(req, cancel->fd);
  193. }
  194. if (!req->file) {
  195. ret = -EBADF;
  196. goto done;
  197. }
  198. cd.file = req->file;
  199. }
  200. ret = __io_async_cancel(&cd, tctx, issue_flags);
  201. done:
  202. if (ret < 0)
  203. req_set_fail(req);
  204. io_req_set_res(req, ret, 0);
  205. return IOU_COMPLETE;
  206. }
  207. static int __io_sync_cancel(struct io_uring_task *tctx,
  208. struct io_cancel_data *cd, int fd)
  209. {
  210. struct io_ring_ctx *ctx = cd->ctx;
  211. /* fixed must be grabbed every time since we drop the uring_lock */
  212. if ((cd->flags & IORING_ASYNC_CANCEL_FD) &&
  213. (cd->flags & IORING_ASYNC_CANCEL_FD_FIXED)) {
  214. struct io_rsrc_node *node;
  215. node = io_rsrc_node_lookup(&ctx->file_table.data, fd);
  216. if (unlikely(!node))
  217. return -EBADF;
  218. cd->file = io_slot_file(node);
  219. if (!cd->file)
  220. return -EBADF;
  221. }
  222. return __io_async_cancel(cd, tctx, 0);
  223. }
  224. int io_sync_cancel(struct io_ring_ctx *ctx, void __user *arg)
  225. __must_hold(&ctx->uring_lock)
  226. {
  227. struct io_cancel_data cd = {
  228. .ctx = ctx,
  229. .seq = atomic_inc_return(&ctx->cancel_seq),
  230. };
  231. ktime_t timeout = KTIME_MAX;
  232. struct io_uring_sync_cancel_reg sc;
  233. struct file *file = NULL;
  234. DEFINE_WAIT(wait);
  235. int ret, i;
  236. if (copy_from_user(&sc, arg, sizeof(sc)))
  237. return -EFAULT;
  238. if (sc.flags & ~CANCEL_FLAGS)
  239. return -EINVAL;
  240. for (i = 0; i < ARRAY_SIZE(sc.pad); i++)
  241. if (sc.pad[i])
  242. return -EINVAL;
  243. for (i = 0; i < ARRAY_SIZE(sc.pad2); i++)
  244. if (sc.pad2[i])
  245. return -EINVAL;
  246. cd.data = sc.addr;
  247. cd.flags = sc.flags;
  248. cd.opcode = sc.opcode;
  249. /* we can grab a normal file descriptor upfront */
  250. if ((cd.flags & IORING_ASYNC_CANCEL_FD) &&
  251. !(cd.flags & IORING_ASYNC_CANCEL_FD_FIXED)) {
  252. file = fget(sc.fd);
  253. if (!file)
  254. return -EBADF;
  255. cd.file = file;
  256. }
  257. ret = __io_sync_cancel(current->io_uring, &cd, sc.fd);
  258. /* found something, done! */
  259. if (ret != -EALREADY)
  260. goto out;
  261. if (sc.timeout.tv_sec != -1UL || sc.timeout.tv_nsec != -1UL) {
  262. struct timespec64 ts = {
  263. .tv_sec = sc.timeout.tv_sec,
  264. .tv_nsec = sc.timeout.tv_nsec
  265. };
  266. timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns());
  267. }
  268. /*
  269. * Keep looking until we get -ENOENT. we'll get woken everytime
  270. * every time a request completes and will retry the cancelation.
  271. */
  272. do {
  273. cd.seq = atomic_inc_return(&ctx->cancel_seq);
  274. prepare_to_wait(&ctx->cq_wait, &wait, TASK_INTERRUPTIBLE);
  275. ret = __io_sync_cancel(current->io_uring, &cd, sc.fd);
  276. mutex_unlock(&ctx->uring_lock);
  277. if (ret != -EALREADY)
  278. break;
  279. ret = io_run_task_work_sig(ctx);
  280. if (ret < 0)
  281. break;
  282. ret = schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS);
  283. if (!ret) {
  284. ret = -ETIME;
  285. break;
  286. }
  287. mutex_lock(&ctx->uring_lock);
  288. } while (1);
  289. finish_wait(&ctx->cq_wait, &wait);
  290. mutex_lock(&ctx->uring_lock);
  291. if (ret == -ENOENT || ret > 0)
  292. ret = 0;
  293. out:
  294. if (file)
  295. fput(file);
  296. return ret;
  297. }
  298. bool io_cancel_remove_all(struct io_ring_ctx *ctx, struct io_uring_task *tctx,
  299. struct hlist_head *list, bool cancel_all,
  300. bool (*cancel)(struct io_kiocb *))
  301. {
  302. struct hlist_node *tmp;
  303. struct io_kiocb *req;
  304. bool found = false;
  305. lockdep_assert_held(&ctx->uring_lock);
  306. hlist_for_each_entry_safe(req, tmp, list, hash_node) {
  307. if (!io_match_task_safe(req, tctx, cancel_all))
  308. continue;
  309. hlist_del_init(&req->hash_node);
  310. if (cancel(req))
  311. found = true;
  312. }
  313. return found;
  314. }
  315. int io_cancel_remove(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
  316. unsigned int issue_flags, struct hlist_head *list,
  317. bool (*cancel)(struct io_kiocb *))
  318. {
  319. struct hlist_node *tmp;
  320. struct io_kiocb *req;
  321. int nr = 0;
  322. io_ring_submit_lock(ctx, issue_flags);
  323. hlist_for_each_entry_safe(req, tmp, list, hash_node) {
  324. if (!io_cancel_req_match(req, cd))
  325. continue;
  326. if (cancel(req))
  327. nr++;
  328. if (!(cd->flags & IORING_ASYNC_CANCEL_ALL))
  329. break;
  330. }
  331. io_ring_submit_unlock(ctx, issue_flags);
  332. return nr ?: -ENOENT;
  333. }
  334. static bool io_match_linked(struct io_kiocb *head)
  335. {
  336. struct io_kiocb *req;
  337. io_for_each_link(req, head) {
  338. if (req->flags & REQ_F_INFLIGHT)
  339. return true;
  340. }
  341. return false;
  342. }
  343. /*
  344. * As io_match_task() but protected against racing with linked timeouts.
  345. * User must not hold timeout_lock.
  346. */
  347. bool io_match_task_safe(struct io_kiocb *head, struct io_uring_task *tctx,
  348. bool cancel_all)
  349. {
  350. bool matched;
  351. if (tctx && head->tctx != tctx)
  352. return false;
  353. if (cancel_all)
  354. return true;
  355. if (head->flags & REQ_F_LINK_TIMEOUT) {
  356. struct io_ring_ctx *ctx = head->ctx;
  357. /* protect against races with linked timeouts */
  358. raw_spin_lock_irq(&ctx->timeout_lock);
  359. matched = io_match_linked(head);
  360. raw_spin_unlock_irq(&ctx->timeout_lock);
  361. } else {
  362. matched = io_match_linked(head);
  363. }
  364. return matched;
  365. }
  366. void __io_uring_cancel(bool cancel_all)
  367. {
  368. io_uring_unreg_ringfd();
  369. io_uring_cancel_generic(cancel_all, NULL);
  370. }
  371. struct io_task_cancel {
  372. struct io_uring_task *tctx;
  373. bool all;
  374. };
  375. static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
  376. {
  377. struct io_kiocb *req = container_of(work, struct io_kiocb, work);
  378. struct io_task_cancel *cancel = data;
  379. return io_match_task_safe(req, cancel->tctx, cancel->all);
  380. }
  381. static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
  382. struct io_uring_task *tctx,
  383. bool cancel_all)
  384. {
  385. struct io_defer_entry *de;
  386. LIST_HEAD(list);
  387. list_for_each_entry_reverse(de, &ctx->defer_list, list) {
  388. if (io_match_task_safe(de->req, tctx, cancel_all)) {
  389. list_cut_position(&list, &ctx->defer_list, &de->list);
  390. break;
  391. }
  392. }
  393. if (list_empty(&list))
  394. return false;
  395. while (!list_empty(&list)) {
  396. de = list_first_entry(&list, struct io_defer_entry, list);
  397. list_del_init(&de->list);
  398. ctx->nr_drained -= io_linked_nr(de->req);
  399. io_req_task_queue_fail(de->req, -ECANCELED);
  400. kfree(de);
  401. }
  402. return true;
  403. }
  404. __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
  405. {
  406. struct io_kiocb *req = container_of(work, struct io_kiocb, work);
  407. return req->ctx == data;
  408. }
  409. static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
  410. {
  411. struct io_tctx_node *node;
  412. enum io_wq_cancel cret;
  413. bool ret = false;
  414. mutex_lock(&ctx->uring_lock);
  415. mutex_lock(&ctx->tctx_lock);
  416. list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
  417. struct io_uring_task *tctx = node->task->io_uring;
  418. /*
  419. * io_wq will stay alive while we hold uring_lock, because it's
  420. * killed after ctx nodes, which requires to take the lock.
  421. */
  422. if (!tctx || !tctx->io_wq)
  423. continue;
  424. cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
  425. ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
  426. }
  427. mutex_unlock(&ctx->tctx_lock);
  428. mutex_unlock(&ctx->uring_lock);
  429. return ret;
  430. }
  431. __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
  432. struct io_uring_task *tctx,
  433. bool cancel_all, bool is_sqpoll_thread)
  434. {
  435. struct io_task_cancel cancel = { .tctx = tctx, .all = cancel_all, };
  436. enum io_wq_cancel cret;
  437. bool ret = false;
  438. /* set it so io_req_local_work_add() would wake us up */
  439. if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
  440. atomic_set(&ctx->cq_wait_nr, 1);
  441. smp_mb();
  442. }
  443. /* failed during ring init, it couldn't have issued any requests */
  444. if (!ctx->rings)
  445. return false;
  446. if (!tctx) {
  447. ret |= io_uring_try_cancel_iowq(ctx);
  448. } else if (tctx->io_wq) {
  449. /*
  450. * Cancels requests of all rings, not only @ctx, but
  451. * it's fine as the task is in exit/exec.
  452. */
  453. cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
  454. &cancel, true);
  455. ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
  456. }
  457. /* SQPOLL thread does its own polling */
  458. if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
  459. is_sqpoll_thread) {
  460. while (!list_empty(&ctx->iopoll_list)) {
  461. io_iopoll_try_reap_events(ctx);
  462. ret = true;
  463. cond_resched();
  464. }
  465. }
  466. if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) &&
  467. io_allowed_defer_tw_run(ctx))
  468. ret |= io_run_local_work(ctx, INT_MAX, INT_MAX) > 0;
  469. mutex_lock(&ctx->uring_lock);
  470. ret |= io_cancel_defer_files(ctx, tctx, cancel_all);
  471. ret |= io_poll_remove_all(ctx, tctx, cancel_all);
  472. ret |= io_waitid_remove_all(ctx, tctx, cancel_all);
  473. ret |= io_futex_remove_all(ctx, tctx, cancel_all);
  474. ret |= io_uring_try_cancel_uring_cmd(ctx, tctx, cancel_all);
  475. mutex_unlock(&ctx->uring_lock);
  476. ret |= io_kill_timeouts(ctx, tctx, cancel_all);
  477. if (tctx)
  478. ret |= io_run_task_work() > 0;
  479. else
  480. ret |= flush_delayed_work(&ctx->fallback_work);
  481. return ret;
  482. }
  483. static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
  484. {
  485. if (tracked)
  486. return atomic_read(&tctx->inflight_tracked);
  487. return percpu_counter_sum(&tctx->inflight);
  488. }
  489. /*
  490. * Find any io_uring ctx that this task has registered or done IO on, and cancel
  491. * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation.
  492. */
  493. __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
  494. {
  495. struct io_uring_task *tctx = current->io_uring;
  496. struct io_ring_ctx *ctx;
  497. struct io_tctx_node *node;
  498. unsigned long index;
  499. s64 inflight;
  500. DEFINE_WAIT(wait);
  501. WARN_ON_ONCE(sqd && sqpoll_task_locked(sqd) != current);
  502. if (!current->io_uring)
  503. return;
  504. if (tctx->io_wq)
  505. io_wq_exit_start(tctx->io_wq);
  506. atomic_inc(&tctx->in_cancel);
  507. do {
  508. bool loop = false;
  509. io_uring_drop_tctx_refs(current);
  510. if (!tctx_inflight(tctx, !cancel_all))
  511. break;
  512. /* read completions before cancelations */
  513. inflight = tctx_inflight(tctx, false);
  514. if (!inflight)
  515. break;
  516. if (!sqd) {
  517. xa_for_each(&tctx->xa, index, node) {
  518. /* sqpoll task will cancel all its requests */
  519. if (node->ctx->sq_data)
  520. continue;
  521. loop |= io_uring_try_cancel_requests(node->ctx,
  522. current->io_uring,
  523. cancel_all,
  524. false);
  525. }
  526. } else {
  527. list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
  528. loop |= io_uring_try_cancel_requests(ctx,
  529. current->io_uring,
  530. cancel_all,
  531. true);
  532. }
  533. if (loop) {
  534. cond_resched();
  535. continue;
  536. }
  537. prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE);
  538. io_run_task_work();
  539. io_uring_drop_tctx_refs(current);
  540. xa_for_each(&tctx->xa, index, node) {
  541. if (io_local_work_pending(node->ctx)) {
  542. WARN_ON_ONCE(node->ctx->submitter_task &&
  543. node->ctx->submitter_task != current);
  544. goto end_wait;
  545. }
  546. }
  547. /*
  548. * If we've seen completions, retry without waiting. This
  549. * avoids a race where a completion comes in before we did
  550. * prepare_to_wait().
  551. */
  552. if (inflight == tctx_inflight(tctx, !cancel_all))
  553. schedule();
  554. end_wait:
  555. finish_wait(&tctx->wait, &wait);
  556. } while (1);
  557. io_uring_clean_tctx(tctx);
  558. if (cancel_all) {
  559. /*
  560. * We shouldn't run task_works after cancel, so just leave
  561. * ->in_cancel set for normal exit.
  562. */
  563. atomic_dec(&tctx->in_cancel);
  564. /* for exec all current's requests should be gone, kill tctx */
  565. __io_uring_free(current);
  566. }
  567. }