timeout.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/errno.h>
  4. #include <linux/file.h>
  5. #include <linux/io_uring.h>
  6. #include <trace/events/io_uring.h>
  7. #include <uapi/linux/io_uring.h>
  8. #include "io_uring.h"
  9. #include "refs.h"
  10. #include "cancel.h"
  11. #include "timeout.h"
  12. struct io_timeout {
  13. struct file *file;
  14. u32 off;
  15. u32 target_seq;
  16. u32 repeats;
  17. struct list_head list;
  18. /* head of the link, used by linked timeouts only */
  19. struct io_kiocb *head;
  20. /* for linked completions */
  21. struct io_kiocb *prev;
  22. };
  23. struct io_timeout_rem {
  24. struct file *file;
  25. u64 addr;
  26. /* timeout update */
  27. struct timespec64 ts;
  28. u32 flags;
  29. bool ltimeout;
  30. };
  31. static struct io_kiocb *__io_disarm_linked_timeout(struct io_kiocb *req,
  32. struct io_kiocb *link);
  33. static inline bool io_is_timeout_noseq(struct io_kiocb *req)
  34. {
  35. struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
  36. struct io_timeout_data *data = req->async_data;
  37. return !timeout->off || data->flags & IORING_TIMEOUT_MULTISHOT;
  38. }
  39. static inline void io_put_req(struct io_kiocb *req)
  40. {
  41. if (req_ref_put_and_test(req)) {
  42. io_queue_next(req);
  43. io_free_req(req);
  44. }
  45. }
  46. static inline bool io_timeout_finish(struct io_timeout *timeout,
  47. struct io_timeout_data *data)
  48. {
  49. if (!(data->flags & IORING_TIMEOUT_MULTISHOT))
  50. return true;
  51. if (!timeout->off || (timeout->repeats && --timeout->repeats))
  52. return false;
  53. return true;
  54. }
  55. static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer);
  56. static void io_timeout_complete(struct io_tw_req tw_req, io_tw_token_t tw)
  57. {
  58. struct io_kiocb *req = tw_req.req;
  59. struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
  60. struct io_timeout_data *data = req->async_data;
  61. struct io_ring_ctx *ctx = req->ctx;
  62. if (!io_timeout_finish(timeout, data)) {
  63. if (io_req_post_cqe(req, -ETIME, IORING_CQE_F_MORE)) {
  64. /* re-arm timer */
  65. raw_spin_lock_irq(&ctx->timeout_lock);
  66. list_add(&timeout->list, ctx->timeout_list.prev);
  67. hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
  68. raw_spin_unlock_irq(&ctx->timeout_lock);
  69. return;
  70. }
  71. }
  72. io_req_task_complete(tw_req, tw);
  73. }
  74. static __cold bool io_flush_killed_timeouts(struct list_head *list, int err)
  75. {
  76. if (list_empty(list))
  77. return false;
  78. while (!list_empty(list)) {
  79. struct io_timeout *timeout;
  80. struct io_kiocb *req;
  81. timeout = list_first_entry(list, struct io_timeout, list);
  82. list_del_init(&timeout->list);
  83. req = cmd_to_io_kiocb(timeout);
  84. if (err)
  85. req_set_fail(req);
  86. io_req_queue_tw_complete(req, err);
  87. }
  88. return true;
  89. }
  90. static void io_kill_timeout(struct io_kiocb *req, struct list_head *list)
  91. __must_hold(&req->ctx->timeout_lock)
  92. {
  93. struct io_timeout_data *io = req->async_data;
  94. if (hrtimer_try_to_cancel(&io->timer) != -1) {
  95. struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
  96. atomic_set(&req->ctx->cq_timeouts,
  97. atomic_read(&req->ctx->cq_timeouts) + 1);
  98. list_move_tail(&timeout->list, list);
  99. }
  100. }
  101. __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
  102. {
  103. struct io_timeout *timeout, *tmp;
  104. LIST_HEAD(list);
  105. u32 seq;
  106. raw_spin_lock_irq(&ctx->timeout_lock);
  107. seq = READ_ONCE(ctx->cached_cq_tail) - atomic_read(&ctx->cq_timeouts);
  108. list_for_each_entry_safe(timeout, tmp, &ctx->timeout_list, list) {
  109. struct io_kiocb *req = cmd_to_io_kiocb(timeout);
  110. u32 events_needed, events_got;
  111. if (io_is_timeout_noseq(req))
  112. break;
  113. /*
  114. * Since seq can easily wrap around over time, subtract
  115. * the last seq at which timeouts were flushed before comparing.
  116. * Assuming not more than 2^31-1 events have happened since,
  117. * these subtractions won't have wrapped, so we can check if
  118. * target is in [last_seq, current_seq] by comparing the two.
  119. */
  120. events_needed = timeout->target_seq - ctx->cq_last_tm_flush;
  121. events_got = seq - ctx->cq_last_tm_flush;
  122. if (events_got < events_needed)
  123. break;
  124. io_kill_timeout(req, &list);
  125. }
  126. ctx->cq_last_tm_flush = seq;
  127. raw_spin_unlock_irq(&ctx->timeout_lock);
  128. io_flush_killed_timeouts(&list, 0);
  129. }
  130. static void io_req_tw_fail_links(struct io_tw_req tw_req, io_tw_token_t tw)
  131. {
  132. struct io_kiocb *link = tw_req.req;
  133. io_tw_lock(link->ctx, tw);
  134. while (link) {
  135. struct io_kiocb *nxt = link->link;
  136. long res = -ECANCELED;
  137. if (link->flags & REQ_F_FAIL)
  138. res = link->cqe.res;
  139. link->link = NULL;
  140. io_req_set_res(link, res, 0);
  141. io_req_task_complete((struct io_tw_req){link}, tw);
  142. link = nxt;
  143. }
  144. }
  145. static void io_fail_links(struct io_kiocb *req)
  146. __must_hold(&req->ctx->completion_lock)
  147. {
  148. struct io_kiocb *link = req->link;
  149. bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES;
  150. if (!link)
  151. return;
  152. while (link) {
  153. if (ignore_cqes)
  154. link->flags |= REQ_F_CQE_SKIP;
  155. else
  156. link->flags &= ~REQ_F_CQE_SKIP;
  157. trace_io_uring_fail_link(req, link);
  158. link = link->link;
  159. }
  160. link = req->link;
  161. link->io_task_work.func = io_req_tw_fail_links;
  162. io_req_task_work_add(link);
  163. req->link = NULL;
  164. }
  165. static inline void io_remove_next_linked(struct io_kiocb *req)
  166. {
  167. struct io_kiocb *nxt = req->link;
  168. req->link = nxt->link;
  169. nxt->link = NULL;
  170. }
  171. void io_disarm_next(struct io_kiocb *req)
  172. __must_hold(&req->ctx->completion_lock)
  173. {
  174. struct io_kiocb *link = NULL;
  175. if (req->flags & REQ_F_ARM_LTIMEOUT) {
  176. link = req->link;
  177. req->flags &= ~REQ_F_ARM_LTIMEOUT;
  178. if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
  179. io_remove_next_linked(req);
  180. io_req_queue_tw_complete(link, -ECANCELED);
  181. }
  182. } else if (req->flags & REQ_F_LINK_TIMEOUT) {
  183. struct io_ring_ctx *ctx = req->ctx;
  184. raw_spin_lock_irq(&ctx->timeout_lock);
  185. if (req->link && req->link->opcode == IORING_OP_LINK_TIMEOUT)
  186. link = __io_disarm_linked_timeout(req, req->link);
  187. raw_spin_unlock_irq(&ctx->timeout_lock);
  188. if (link)
  189. io_req_queue_tw_complete(link, -ECANCELED);
  190. }
  191. if (unlikely((req->flags & REQ_F_FAIL) &&
  192. !(req->flags & REQ_F_HARDLINK)))
  193. io_fail_links(req);
  194. }
  195. static struct io_kiocb *__io_disarm_linked_timeout(struct io_kiocb *req,
  196. struct io_kiocb *link)
  197. __must_hold(&req->ctx->completion_lock)
  198. __must_hold(&req->ctx->timeout_lock)
  199. {
  200. struct io_timeout_data *io = link->async_data;
  201. struct io_timeout *timeout = io_kiocb_to_cmd(link, struct io_timeout);
  202. io_remove_next_linked(req);
  203. timeout->head = NULL;
  204. if (hrtimer_try_to_cancel(&io->timer) != -1) {
  205. list_del(&timeout->list);
  206. return link;
  207. }
  208. return NULL;
  209. }
  210. static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
  211. {
  212. struct io_timeout_data *data = container_of(timer,
  213. struct io_timeout_data, timer);
  214. struct io_kiocb *req = data->req;
  215. struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
  216. struct io_ring_ctx *ctx = req->ctx;
  217. unsigned long flags;
  218. raw_spin_lock_irqsave(&ctx->timeout_lock, flags);
  219. list_del_init(&timeout->list);
  220. atomic_set(&req->ctx->cq_timeouts,
  221. atomic_read(&req->ctx->cq_timeouts) + 1);
  222. raw_spin_unlock_irqrestore(&ctx->timeout_lock, flags);
  223. if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
  224. req_set_fail(req);
  225. io_req_set_res(req, -ETIME, 0);
  226. req->io_task_work.func = io_timeout_complete;
  227. io_req_task_work_add(req);
  228. return HRTIMER_NORESTART;
  229. }
  230. static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
  231. struct io_cancel_data *cd)
  232. __must_hold(&ctx->timeout_lock)
  233. {
  234. struct io_timeout *timeout;
  235. struct io_timeout_data *io;
  236. struct io_kiocb *req = NULL;
  237. list_for_each_entry(timeout, &ctx->timeout_list, list) {
  238. struct io_kiocb *tmp = cmd_to_io_kiocb(timeout);
  239. if (io_cancel_req_match(tmp, cd)) {
  240. req = tmp;
  241. break;
  242. }
  243. }
  244. if (!req)
  245. return ERR_PTR(-ENOENT);
  246. io = req->async_data;
  247. if (hrtimer_try_to_cancel(&io->timer) == -1)
  248. return ERR_PTR(-EALREADY);
  249. timeout = io_kiocb_to_cmd(req, struct io_timeout);
  250. list_del_init(&timeout->list);
  251. return req;
  252. }
  253. int io_timeout_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
  254. __must_hold(&ctx->completion_lock)
  255. {
  256. struct io_kiocb *req;
  257. raw_spin_lock_irq(&ctx->timeout_lock);
  258. req = io_timeout_extract(ctx, cd);
  259. raw_spin_unlock_irq(&ctx->timeout_lock);
  260. if (IS_ERR(req))
  261. return PTR_ERR(req);
  262. io_req_task_queue_fail(req, -ECANCELED);
  263. return 0;
  264. }
  265. static void io_req_task_link_timeout(struct io_tw_req tw_req, io_tw_token_t tw)
  266. {
  267. struct io_kiocb *req = tw_req.req;
  268. struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
  269. struct io_kiocb *prev = timeout->prev;
  270. int ret;
  271. if (prev) {
  272. if (!tw.cancel) {
  273. struct io_cancel_data cd = {
  274. .ctx = req->ctx,
  275. .data = prev->cqe.user_data,
  276. };
  277. ret = io_try_cancel(req->tctx, &cd, 0);
  278. } else {
  279. ret = -ECANCELED;
  280. }
  281. io_req_set_res(req, ret ?: -ETIME, 0);
  282. io_req_task_complete(tw_req, tw);
  283. io_put_req(prev);
  284. } else {
  285. io_req_set_res(req, -ETIME, 0);
  286. io_req_task_complete(tw_req, tw);
  287. }
  288. }
  289. static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
  290. {
  291. struct io_timeout_data *data = container_of(timer,
  292. struct io_timeout_data, timer);
  293. struct io_kiocb *prev, *req = data->req;
  294. struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
  295. struct io_ring_ctx *ctx = req->ctx;
  296. unsigned long flags;
  297. raw_spin_lock_irqsave(&ctx->timeout_lock, flags);
  298. prev = timeout->head;
  299. timeout->head = NULL;
  300. /*
  301. * We don't expect the list to be empty, that will only happen if we
  302. * race with the completion of the linked work.
  303. */
  304. if (prev) {
  305. io_remove_next_linked(prev);
  306. if (!req_ref_inc_not_zero(prev))
  307. prev = NULL;
  308. }
  309. list_del(&timeout->list);
  310. timeout->prev = prev;
  311. raw_spin_unlock_irqrestore(&ctx->timeout_lock, flags);
  312. req->io_task_work.func = io_req_task_link_timeout;
  313. io_req_task_work_add(req);
  314. return HRTIMER_NORESTART;
  315. }
  316. static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
  317. {
  318. switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
  319. case IORING_TIMEOUT_BOOTTIME:
  320. return CLOCK_BOOTTIME;
  321. case IORING_TIMEOUT_REALTIME:
  322. return CLOCK_REALTIME;
  323. default:
  324. /* can't happen, vetted at prep time */
  325. WARN_ON_ONCE(1);
  326. fallthrough;
  327. case 0:
  328. return CLOCK_MONOTONIC;
  329. }
  330. }
  331. static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
  332. struct timespec64 *ts, enum hrtimer_mode mode)
  333. __must_hold(&ctx->timeout_lock)
  334. {
  335. struct io_timeout_data *io;
  336. struct io_timeout *timeout;
  337. struct io_kiocb *req = NULL;
  338. list_for_each_entry(timeout, &ctx->ltimeout_list, list) {
  339. struct io_kiocb *tmp = cmd_to_io_kiocb(timeout);
  340. if (user_data == tmp->cqe.user_data) {
  341. req = tmp;
  342. break;
  343. }
  344. }
  345. if (!req)
  346. return -ENOENT;
  347. io = req->async_data;
  348. if (hrtimer_try_to_cancel(&io->timer) == -1)
  349. return -EALREADY;
  350. hrtimer_setup(&io->timer, io_link_timeout_fn, io_timeout_get_clock(io), mode);
  351. hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
  352. return 0;
  353. }
  354. static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
  355. struct timespec64 *ts, enum hrtimer_mode mode)
  356. __must_hold(&ctx->timeout_lock)
  357. {
  358. struct io_cancel_data cd = { .ctx = ctx, .data = user_data, };
  359. struct io_kiocb *req = io_timeout_extract(ctx, &cd);
  360. struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
  361. struct io_timeout_data *data;
  362. if (IS_ERR(req))
  363. return PTR_ERR(req);
  364. timeout->off = 0; /* noseq */
  365. data = req->async_data;
  366. data->ts = *ts;
  367. list_add_tail(&timeout->list, &ctx->timeout_list);
  368. hrtimer_setup(&data->timer, io_timeout_fn, io_timeout_get_clock(data), mode);
  369. hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), mode);
  370. return 0;
  371. }
  372. int io_timeout_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  373. {
  374. struct io_timeout_rem *tr = io_kiocb_to_cmd(req, struct io_timeout_rem);
  375. if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
  376. return -EINVAL;
  377. if (sqe->buf_index || sqe->len || sqe->splice_fd_in)
  378. return -EINVAL;
  379. tr->ltimeout = false;
  380. tr->addr = READ_ONCE(sqe->addr);
  381. tr->flags = READ_ONCE(sqe->timeout_flags);
  382. if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
  383. if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
  384. return -EINVAL;
  385. if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
  386. tr->ltimeout = true;
  387. if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
  388. return -EINVAL;
  389. if (get_timespec64(&tr->ts, u64_to_user_ptr(READ_ONCE(sqe->addr2))))
  390. return -EFAULT;
  391. if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0)
  392. return -EINVAL;
  393. } else if (tr->flags) {
  394. /* timeout removal doesn't support flags */
  395. return -EINVAL;
  396. }
  397. return 0;
  398. }
  399. static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
  400. {
  401. return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
  402. : HRTIMER_MODE_REL;
  403. }
  404. /*
  405. * Remove or update an existing timeout command
  406. */
  407. int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
  408. {
  409. struct io_timeout_rem *tr = io_kiocb_to_cmd(req, struct io_timeout_rem);
  410. struct io_ring_ctx *ctx = req->ctx;
  411. int ret;
  412. if (!(tr->flags & IORING_TIMEOUT_UPDATE)) {
  413. struct io_cancel_data cd = { .ctx = ctx, .data = tr->addr, };
  414. spin_lock(&ctx->completion_lock);
  415. ret = io_timeout_cancel(ctx, &cd);
  416. spin_unlock(&ctx->completion_lock);
  417. } else {
  418. enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
  419. raw_spin_lock_irq(&ctx->timeout_lock);
  420. if (tr->ltimeout)
  421. ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
  422. else
  423. ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
  424. raw_spin_unlock_irq(&ctx->timeout_lock);
  425. }
  426. if (ret < 0)
  427. req_set_fail(req);
  428. io_req_set_res(req, ret, 0);
  429. return IOU_COMPLETE;
  430. }
  431. static int __io_timeout_prep(struct io_kiocb *req,
  432. const struct io_uring_sqe *sqe,
  433. bool is_timeout_link)
  434. {
  435. struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
  436. struct io_timeout_data *data;
  437. unsigned flags;
  438. u32 off = READ_ONCE(sqe->off);
  439. if (sqe->buf_index || sqe->len != 1 || sqe->splice_fd_in)
  440. return -EINVAL;
  441. if (off && is_timeout_link)
  442. return -EINVAL;
  443. flags = READ_ONCE(sqe->timeout_flags);
  444. if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
  445. IORING_TIMEOUT_ETIME_SUCCESS |
  446. IORING_TIMEOUT_MULTISHOT))
  447. return -EINVAL;
  448. /* more than one clock specified is invalid, obviously */
  449. if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
  450. return -EINVAL;
  451. /* multishot requests only make sense with rel values */
  452. if (!(~flags & (IORING_TIMEOUT_MULTISHOT | IORING_TIMEOUT_ABS)))
  453. return -EINVAL;
  454. INIT_LIST_HEAD(&timeout->list);
  455. timeout->off = off;
  456. if (unlikely(off && !req->ctx->off_timeout_used))
  457. req->ctx->off_timeout_used = true;
  458. /*
  459. * for multishot reqs w/ fixed nr of repeats, repeats tracks the
  460. * remaining nr
  461. */
  462. timeout->repeats = 0;
  463. if ((flags & IORING_TIMEOUT_MULTISHOT) && off > 0)
  464. timeout->repeats = off;
  465. if (WARN_ON_ONCE(req_has_async_data(req)))
  466. return -EFAULT;
  467. data = io_uring_alloc_async_data(NULL, req);
  468. if (!data)
  469. return -ENOMEM;
  470. data->req = req;
  471. data->flags = flags;
  472. if (get_timespec64(&data->ts, u64_to_user_ptr(READ_ONCE(sqe->addr))))
  473. return -EFAULT;
  474. if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0)
  475. return -EINVAL;
  476. data->mode = io_translate_timeout_mode(flags);
  477. if (is_timeout_link) {
  478. struct io_submit_link *link = &req->ctx->submit_state.link;
  479. if (!link->head)
  480. return -EINVAL;
  481. if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
  482. return -EINVAL;
  483. timeout->head = link->last;
  484. link->last->flags |= REQ_F_ARM_LTIMEOUT;
  485. hrtimer_setup(&data->timer, io_link_timeout_fn, io_timeout_get_clock(data),
  486. data->mode);
  487. } else {
  488. hrtimer_setup(&data->timer, io_timeout_fn, io_timeout_get_clock(data), data->mode);
  489. }
  490. return 0;
  491. }
  492. int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  493. {
  494. return __io_timeout_prep(req, sqe, false);
  495. }
  496. int io_link_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  497. {
  498. return __io_timeout_prep(req, sqe, true);
  499. }
  500. int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
  501. {
  502. struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
  503. struct io_ring_ctx *ctx = req->ctx;
  504. struct io_timeout_data *data = req->async_data;
  505. struct list_head *entry;
  506. u32 tail, off = timeout->off;
  507. raw_spin_lock_irq(&ctx->timeout_lock);
  508. /*
  509. * sqe->off holds how many events that need to occur for this
  510. * timeout event to be satisfied. If it isn't set, then this is
  511. * a pure timeout request, sequence isn't used.
  512. */
  513. if (io_is_timeout_noseq(req)) {
  514. entry = ctx->timeout_list.prev;
  515. goto add;
  516. }
  517. tail = data_race(ctx->cached_cq_tail) - atomic_read(&ctx->cq_timeouts);
  518. timeout->target_seq = tail + off;
  519. /* Update the last seq here in case io_flush_timeouts() hasn't.
  520. * This is safe because ->completion_lock is held, and submissions
  521. * and completions are never mixed in the same ->completion_lock section.
  522. */
  523. ctx->cq_last_tm_flush = tail;
  524. /*
  525. * Insertion sort, ensuring the first entry in the list is always
  526. * the one we need first.
  527. */
  528. list_for_each_prev(entry, &ctx->timeout_list) {
  529. struct io_timeout *nextt = list_entry(entry, struct io_timeout, list);
  530. struct io_kiocb *nxt = cmd_to_io_kiocb(nextt);
  531. if (io_is_timeout_noseq(nxt))
  532. continue;
  533. /* nxt.seq is behind @tail, otherwise would've been completed */
  534. if (off >= nextt->target_seq - tail)
  535. break;
  536. }
  537. add:
  538. list_add(&timeout->list, entry);
  539. hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
  540. raw_spin_unlock_irq(&ctx->timeout_lock);
  541. return IOU_ISSUE_SKIP_COMPLETE;
  542. }
  543. void io_queue_linked_timeout(struct io_kiocb *req)
  544. {
  545. struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
  546. struct io_ring_ctx *ctx = req->ctx;
  547. raw_spin_lock_irq(&ctx->timeout_lock);
  548. /*
  549. * If the back reference is NULL, then our linked request finished
  550. * before we got a chance to setup the timer
  551. */
  552. if (timeout->head) {
  553. struct io_timeout_data *data = req->async_data;
  554. hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
  555. data->mode);
  556. list_add_tail(&timeout->list, &ctx->ltimeout_list);
  557. }
  558. raw_spin_unlock_irq(&ctx->timeout_lock);
  559. /* drop submission reference */
  560. io_put_req(req);
  561. }
  562. static bool io_match_task(struct io_kiocb *head, struct io_uring_task *tctx,
  563. bool cancel_all)
  564. __must_hold(&head->ctx->timeout_lock)
  565. {
  566. struct io_kiocb *req;
  567. if (tctx && head->tctx != tctx)
  568. return false;
  569. if (cancel_all)
  570. return true;
  571. io_for_each_link(req, head) {
  572. if (req->flags & REQ_F_INFLIGHT)
  573. return true;
  574. }
  575. return false;
  576. }
  577. /* Returns true if we found and killed one or more timeouts */
  578. __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, struct io_uring_task *tctx,
  579. bool cancel_all)
  580. {
  581. struct io_timeout *timeout, *tmp;
  582. LIST_HEAD(list);
  583. /*
  584. * completion_lock is needed for io_match_task(). Take it before
  585. * timeout_lockfirst to keep locking ordering.
  586. */
  587. spin_lock(&ctx->completion_lock);
  588. raw_spin_lock_irq(&ctx->timeout_lock);
  589. list_for_each_entry_safe(timeout, tmp, &ctx->timeout_list, list) {
  590. struct io_kiocb *req = cmd_to_io_kiocb(timeout);
  591. if (io_match_task(req, tctx, cancel_all))
  592. io_kill_timeout(req, &list);
  593. }
  594. raw_spin_unlock_irq(&ctx->timeout_lock);
  595. spin_unlock(&ctx->completion_lock);
  596. return io_flush_killed_timeouts(&list, -ECANCELED);
  597. }