159915143SJens Axboe // SPDX-License-Identifier: GPL-2.0
259915143SJens Axboe #include <linux/kernel.h>
359915143SJens Axboe #include <linux/errno.h>
459915143SJens Axboe #include <linux/file.h>
559915143SJens Axboe #include <linux/io_uring.h>
659915143SJens Axboe
759915143SJens Axboe #include <trace/events/io_uring.h>
859915143SJens Axboe
959915143SJens Axboe #include <uapi/linux/io_uring.h>
1059915143SJens Axboe
1159915143SJens Axboe #include "io_uring.h"
1259915143SJens Axboe #include "refs.h"
137aaff708SJens Axboe #include "cancel.h"
1459915143SJens Axboe #include "timeout.h"
1559915143SJens Axboe
1659915143SJens Axboe struct io_timeout {
1759915143SJens Axboe struct file *file;
1859915143SJens Axboe u32 off;
1959915143SJens Axboe u32 target_seq;
20ea97f6c8SDavid Wei u32 repeats;
2159915143SJens Axboe struct list_head list;
2259915143SJens Axboe /* head of the link, used by linked timeouts only */
2359915143SJens Axboe struct io_kiocb *head;
2459915143SJens Axboe /* for linked completions */
2559915143SJens Axboe struct io_kiocb *prev;
2659915143SJens Axboe };
2759915143SJens Axboe
2859915143SJens Axboe struct io_timeout_rem {
2959915143SJens Axboe struct file *file;
3059915143SJens Axboe u64 addr;
3159915143SJens Axboe
3259915143SJens Axboe /* timeout update */
3359915143SJens Axboe struct timespec64 ts;
3459915143SJens Axboe u32 flags;
3559915143SJens Axboe bool ltimeout;
3659915143SJens Axboe };
3759915143SJens Axboe
io_is_timeout_noseq(struct io_kiocb * req)3859915143SJens Axboe static inline bool io_is_timeout_noseq(struct io_kiocb *req)
3959915143SJens Axboe {
40f2ccb5aeSStefan Metzmacher struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
41ea97f6c8SDavid Wei struct io_timeout_data *data = req->async_data;
4259915143SJens Axboe
43ea97f6c8SDavid Wei return !timeout->off || data->flags & IORING_TIMEOUT_MULTISHOT;
4459915143SJens Axboe }
4559915143SJens Axboe
io_put_req(struct io_kiocb * req)4659915143SJens Axboe static inline void io_put_req(struct io_kiocb *req)
4759915143SJens Axboe {
4859915143SJens Axboe if (req_ref_put_and_test(req)) {
4959915143SJens Axboe io_queue_next(req);
5059915143SJens Axboe io_free_req(req);
5159915143SJens Axboe }
5259915143SJens Axboe }
5359915143SJens Axboe
io_timeout_finish(struct io_timeout * timeout,struct io_timeout_data * data)54ea97f6c8SDavid Wei static inline bool io_timeout_finish(struct io_timeout *timeout,
55ea97f6c8SDavid Wei struct io_timeout_data *data)
56ea97f6c8SDavid Wei {
57ea97f6c8SDavid Wei if (!(data->flags & IORING_TIMEOUT_MULTISHOT))
58ea97f6c8SDavid Wei return true;
59ea97f6c8SDavid Wei
60ea97f6c8SDavid Wei if (!timeout->off || (timeout->repeats && --timeout->repeats))
61ea97f6c8SDavid Wei return false;
62ea97f6c8SDavid Wei
63ea97f6c8SDavid Wei return true;
64ea97f6c8SDavid Wei }
65ea97f6c8SDavid Wei
66ea97f6c8SDavid Wei static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer);
67ea97f6c8SDavid Wei
io_timeout_complete(struct io_kiocb * req,struct io_tw_state * ts)68ea97f6c8SDavid Wei static void io_timeout_complete(struct io_kiocb *req, struct io_tw_state *ts)
69ea97f6c8SDavid Wei {
70ea97f6c8SDavid Wei struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
71ea97f6c8SDavid Wei struct io_timeout_data *data = req->async_data;
72ea97f6c8SDavid Wei struct io_ring_ctx *ctx = req->ctx;
73ea97f6c8SDavid Wei
74ea97f6c8SDavid Wei if (!io_timeout_finish(timeout, data)) {
75e5c12945SPavel Begunkov if (io_req_post_cqe(req, -ETIME, IORING_CQE_F_MORE)) {
76ea97f6c8SDavid Wei /* re-arm timer */
77ea97f6c8SDavid Wei spin_lock_irq(&ctx->timeout_lock);
78ea97f6c8SDavid Wei list_add(&timeout->list, ctx->timeout_list.prev);
79ea97f6c8SDavid Wei data->timer.function = io_timeout_fn;
80ea97f6c8SDavid Wei hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
81ea97f6c8SDavid Wei spin_unlock_irq(&ctx->timeout_lock);
82ea97f6c8SDavid Wei return;
83ea97f6c8SDavid Wei }
84ea97f6c8SDavid Wei }
85ea97f6c8SDavid Wei
86ea97f6c8SDavid Wei io_req_task_complete(req, ts);
87ea97f6c8SDavid Wei }
88ea97f6c8SDavid Wei
io_kill_timeout(struct io_kiocb * req,int status)89ba3cdb6fSPavel Begunkov static bool io_kill_timeout(struct io_kiocb *req, int status)
9059915143SJens Axboe __must_hold(&req->ctx->timeout_lock)
9159915143SJens Axboe {
9259915143SJens Axboe struct io_timeout_data *io = req->async_data;
9359915143SJens Axboe
9459915143SJens Axboe if (hrtimer_try_to_cancel(&io->timer) != -1) {
95f2ccb5aeSStefan Metzmacher struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
9659915143SJens Axboe
9759915143SJens Axboe if (status)
9859915143SJens Axboe req_set_fail(req);
9959915143SJens Axboe atomic_set(&req->ctx->cq_timeouts,
10059915143SJens Axboe atomic_read(&req->ctx->cq_timeouts) + 1);
10159915143SJens Axboe list_del_init(&timeout->list);
102833b5dffSPavel Begunkov io_req_queue_tw_complete(req, status);
103ba3cdb6fSPavel Begunkov return true;
10459915143SJens Axboe }
105ba3cdb6fSPavel Begunkov return false;
10659915143SJens Axboe }
10759915143SJens Axboe
io_flush_timeouts(struct io_ring_ctx * ctx)10859915143SJens Axboe __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
10959915143SJens Axboe {
110ea011ee1SPavel Begunkov u32 seq;
11159915143SJens Axboe struct io_timeout *timeout, *tmp;
11259915143SJens Axboe
11359915143SJens Axboe spin_lock_irq(&ctx->timeout_lock);
114ea011ee1SPavel Begunkov seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
115ea011ee1SPavel Begunkov
11659915143SJens Axboe list_for_each_entry_safe(timeout, tmp, &ctx->timeout_list, list) {
11759915143SJens Axboe struct io_kiocb *req = cmd_to_io_kiocb(timeout);
11859915143SJens Axboe u32 events_needed, events_got;
11959915143SJens Axboe
12059915143SJens Axboe if (io_is_timeout_noseq(req))
12159915143SJens Axboe break;
12259915143SJens Axboe
12359915143SJens Axboe /*
12459915143SJens Axboe * Since seq can easily wrap around over time, subtract
12559915143SJens Axboe * the last seq at which timeouts were flushed before comparing.
12659915143SJens Axboe * Assuming not more than 2^31-1 events have happened since,
12759915143SJens Axboe * these subtractions won't have wrapped, so we can check if
12859915143SJens Axboe * target is in [last_seq, current_seq] by comparing the two.
12959915143SJens Axboe */
13059915143SJens Axboe events_needed = timeout->target_seq - ctx->cq_last_tm_flush;
13159915143SJens Axboe events_got = seq - ctx->cq_last_tm_flush;
13259915143SJens Axboe if (events_got < events_needed)
13359915143SJens Axboe break;
13459915143SJens Axboe
13559915143SJens Axboe io_kill_timeout(req, 0);
13659915143SJens Axboe }
13759915143SJens Axboe ctx->cq_last_tm_flush = seq;
13859915143SJens Axboe spin_unlock_irq(&ctx->timeout_lock);
13959915143SJens Axboe }
14059915143SJens Axboe
io_req_tw_fail_links(struct io_kiocb * link,struct io_tw_state * ts)141a282967cSPavel Begunkov static void io_req_tw_fail_links(struct io_kiocb *link, struct io_tw_state *ts)
14259915143SJens Axboe {
143a282967cSPavel Begunkov io_tw_lock(link->ctx, ts);
14459915143SJens Axboe while (link) {
14537c7bd31SPavel Begunkov struct io_kiocb *nxt = link->link;
14659915143SJens Axboe long res = -ECANCELED;
14759915143SJens Axboe
14859915143SJens Axboe if (link->flags & REQ_F_FAIL)
14959915143SJens Axboe res = link->cqe.res;
15059915143SJens Axboe link->link = NULL;
15137c7bd31SPavel Begunkov io_req_set_res(link, res, 0);
152a282967cSPavel Begunkov io_req_task_complete(link, ts);
15337c7bd31SPavel Begunkov link = nxt;
15437c7bd31SPavel Begunkov }
15537c7bd31SPavel Begunkov }
15659915143SJens Axboe
io_fail_links(struct io_kiocb * req)15737c7bd31SPavel Begunkov static void io_fail_links(struct io_kiocb *req)
15837c7bd31SPavel Begunkov __must_hold(&req->ctx->completion_lock)
15937c7bd31SPavel Begunkov {
16037c7bd31SPavel Begunkov struct io_kiocb *link = req->link;
16137c7bd31SPavel Begunkov bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES;
16259915143SJens Axboe
16337c7bd31SPavel Begunkov if (!link)
16437c7bd31SPavel Begunkov return;
16537c7bd31SPavel Begunkov
16637c7bd31SPavel Begunkov while (link) {
16759915143SJens Axboe if (ignore_cqes)
16859915143SJens Axboe link->flags |= REQ_F_CQE_SKIP;
16959915143SJens Axboe else
17059915143SJens Axboe link->flags &= ~REQ_F_CQE_SKIP;
17137c7bd31SPavel Begunkov trace_io_uring_fail_link(req, link);
17237c7bd31SPavel Begunkov link = link->link;
17359915143SJens Axboe }
17437c7bd31SPavel Begunkov
17537c7bd31SPavel Begunkov link = req->link;
17637c7bd31SPavel Begunkov link->io_task_work.func = io_req_tw_fail_links;
17737c7bd31SPavel Begunkov io_req_task_work_add(link);
17837c7bd31SPavel Begunkov req->link = NULL;
17959915143SJens Axboe }
18059915143SJens Axboe
io_remove_next_linked(struct io_kiocb * req)18159915143SJens Axboe static inline void io_remove_next_linked(struct io_kiocb *req)
18259915143SJens Axboe {
18359915143SJens Axboe struct io_kiocb *nxt = req->link;
18459915143SJens Axboe
18559915143SJens Axboe req->link = nxt->link;
18659915143SJens Axboe nxt->link = NULL;
18759915143SJens Axboe }
18859915143SJens Axboe
io_disarm_next(struct io_kiocb * req)189c0dc995eSPavel Begunkov void io_disarm_next(struct io_kiocb *req)
19059915143SJens Axboe __must_hold(&req->ctx->completion_lock)
19159915143SJens Axboe {
19259915143SJens Axboe struct io_kiocb *link = NULL;
19359915143SJens Axboe
19459915143SJens Axboe if (req->flags & REQ_F_ARM_LTIMEOUT) {
19559915143SJens Axboe link = req->link;
19659915143SJens Axboe req->flags &= ~REQ_F_ARM_LTIMEOUT;
19759915143SJens Axboe if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
19859915143SJens Axboe io_remove_next_linked(req);
199833b5dffSPavel Begunkov io_req_queue_tw_complete(link, -ECANCELED);
20059915143SJens Axboe }
20159915143SJens Axboe } else if (req->flags & REQ_F_LINK_TIMEOUT) {
20259915143SJens Axboe struct io_ring_ctx *ctx = req->ctx;
20359915143SJens Axboe
20459915143SJens Axboe spin_lock_irq(&ctx->timeout_lock);
20559915143SJens Axboe link = io_disarm_linked_timeout(req);
20659915143SJens Axboe spin_unlock_irq(&ctx->timeout_lock);
207c0dc995eSPavel Begunkov if (link)
208833b5dffSPavel Begunkov io_req_queue_tw_complete(link, -ECANCELED);
20959915143SJens Axboe }
21059915143SJens Axboe if (unlikely((req->flags & REQ_F_FAIL) &&
211c0dc995eSPavel Begunkov !(req->flags & REQ_F_HARDLINK)))
21259915143SJens Axboe io_fail_links(req);
21359915143SJens Axboe }
21459915143SJens Axboe
__io_disarm_linked_timeout(struct io_kiocb * req,struct io_kiocb * link)21559915143SJens Axboe struct io_kiocb *__io_disarm_linked_timeout(struct io_kiocb *req,
21659915143SJens Axboe struct io_kiocb *link)
21759915143SJens Axboe __must_hold(&req->ctx->completion_lock)
21859915143SJens Axboe __must_hold(&req->ctx->timeout_lock)
21959915143SJens Axboe {
22059915143SJens Axboe struct io_timeout_data *io = link->async_data;
221f2ccb5aeSStefan Metzmacher struct io_timeout *timeout = io_kiocb_to_cmd(link, struct io_timeout);
22259915143SJens Axboe
22359915143SJens Axboe io_remove_next_linked(req);
22459915143SJens Axboe timeout->head = NULL;
22559915143SJens Axboe if (hrtimer_try_to_cancel(&io->timer) != -1) {
22659915143SJens Axboe list_del(&timeout->list);
22759915143SJens Axboe return link;
22859915143SJens Axboe }
22959915143SJens Axboe
23059915143SJens Axboe return NULL;
23159915143SJens Axboe }
23259915143SJens Axboe
io_timeout_fn(struct hrtimer * timer)23359915143SJens Axboe static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
23459915143SJens Axboe {
23559915143SJens Axboe struct io_timeout_data *data = container_of(timer,
23659915143SJens Axboe struct io_timeout_data, timer);
23759915143SJens Axboe struct io_kiocb *req = data->req;
238f2ccb5aeSStefan Metzmacher struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
23959915143SJens Axboe struct io_ring_ctx *ctx = req->ctx;
24059915143SJens Axboe unsigned long flags;
24159915143SJens Axboe
24259915143SJens Axboe spin_lock_irqsave(&ctx->timeout_lock, flags);
24359915143SJens Axboe list_del_init(&timeout->list);
24459915143SJens Axboe atomic_set(&req->ctx->cq_timeouts,
24559915143SJens Axboe atomic_read(&req->ctx->cq_timeouts) + 1);
24659915143SJens Axboe spin_unlock_irqrestore(&ctx->timeout_lock, flags);
24759915143SJens Axboe
24859915143SJens Axboe if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
24959915143SJens Axboe req_set_fail(req);
25059915143SJens Axboe
25159915143SJens Axboe io_req_set_res(req, -ETIME, 0);
252ea97f6c8SDavid Wei req->io_task_work.func = io_timeout_complete;
25359915143SJens Axboe io_req_task_work_add(req);
25459915143SJens Axboe return HRTIMER_NORESTART;
25559915143SJens Axboe }
25659915143SJens Axboe
io_timeout_extract(struct io_ring_ctx * ctx,struct io_cancel_data * cd)25759915143SJens Axboe static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
25859915143SJens Axboe struct io_cancel_data *cd)
25959915143SJens Axboe __must_hold(&ctx->timeout_lock)
26059915143SJens Axboe {
26159915143SJens Axboe struct io_timeout *timeout;
26259915143SJens Axboe struct io_timeout_data *io;
26359915143SJens Axboe struct io_kiocb *req = NULL;
26459915143SJens Axboe
26559915143SJens Axboe list_for_each_entry(timeout, &ctx->timeout_list, list) {
26659915143SJens Axboe struct io_kiocb *tmp = cmd_to_io_kiocb(timeout);
26759915143SJens Axboe
268a30badf6SJens Axboe if (io_cancel_req_match(tmp, cd)) {
26959915143SJens Axboe req = tmp;
27059915143SJens Axboe break;
27159915143SJens Axboe }
272a30badf6SJens Axboe }
27359915143SJens Axboe if (!req)
27459915143SJens Axboe return ERR_PTR(-ENOENT);
27559915143SJens Axboe
27659915143SJens Axboe io = req->async_data;
27759915143SJens Axboe if (hrtimer_try_to_cancel(&io->timer) == -1)
27859915143SJens Axboe return ERR_PTR(-EALREADY);
279f2ccb5aeSStefan Metzmacher timeout = io_kiocb_to_cmd(req, struct io_timeout);
28059915143SJens Axboe list_del_init(&timeout->list);
28159915143SJens Axboe return req;
28259915143SJens Axboe }
28359915143SJens Axboe
io_timeout_cancel(struct io_ring_ctx * ctx,struct io_cancel_data * cd)28459915143SJens Axboe int io_timeout_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
28559915143SJens Axboe __must_hold(&ctx->completion_lock)
28659915143SJens Axboe {
28759915143SJens Axboe struct io_kiocb *req;
28859915143SJens Axboe
28959915143SJens Axboe spin_lock_irq(&ctx->timeout_lock);
29059915143SJens Axboe req = io_timeout_extract(ctx, cd);
29159915143SJens Axboe spin_unlock_irq(&ctx->timeout_lock);
29259915143SJens Axboe
29359915143SJens Axboe if (IS_ERR(req))
29459915143SJens Axboe return PTR_ERR(req);
29559915143SJens Axboe io_req_task_queue_fail(req, -ECANCELED);
29659915143SJens Axboe return 0;
29759915143SJens Axboe }
29859915143SJens Axboe
io_req_task_link_timeout(struct io_kiocb * req,struct io_tw_state * ts)299a282967cSPavel Begunkov static void io_req_task_link_timeout(struct io_kiocb *req, struct io_tw_state *ts)
30059915143SJens Axboe {
301f2ccb5aeSStefan Metzmacher struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
30259915143SJens Axboe struct io_kiocb *prev = timeout->prev;
30359915143SJens Axboe int ret = -ENOENT;
30459915143SJens Axboe
30559915143SJens Axboe if (prev) {
30659915143SJens Axboe if (!(req->task->flags & PF_EXITING)) {
30759915143SJens Axboe struct io_cancel_data cd = {
30859915143SJens Axboe .ctx = req->ctx,
30959915143SJens Axboe .data = prev->cqe.user_data,
31059915143SJens Axboe };
31159915143SJens Axboe
3128e5b3b89SPavel Begunkov ret = io_try_cancel(req->task->io_uring, &cd, 0);
31359915143SJens Axboe }
31459915143SJens Axboe io_req_set_res(req, ret ?: -ETIME, 0);
315a282967cSPavel Begunkov io_req_task_complete(req, ts);
31659915143SJens Axboe io_put_req(prev);
31759915143SJens Axboe } else {
31859915143SJens Axboe io_req_set_res(req, -ETIME, 0);
319a282967cSPavel Begunkov io_req_task_complete(req, ts);
32059915143SJens Axboe }
32159915143SJens Axboe }
32259915143SJens Axboe
io_link_timeout_fn(struct hrtimer * timer)32359915143SJens Axboe static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
32459915143SJens Axboe {
32559915143SJens Axboe struct io_timeout_data *data = container_of(timer,
32659915143SJens Axboe struct io_timeout_data, timer);
32759915143SJens Axboe struct io_kiocb *prev, *req = data->req;
328f2ccb5aeSStefan Metzmacher struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
32959915143SJens Axboe struct io_ring_ctx *ctx = req->ctx;
33059915143SJens Axboe unsigned long flags;
33159915143SJens Axboe
33259915143SJens Axboe spin_lock_irqsave(&ctx->timeout_lock, flags);
33359915143SJens Axboe prev = timeout->head;
33459915143SJens Axboe timeout->head = NULL;
33559915143SJens Axboe
33659915143SJens Axboe /*
33759915143SJens Axboe * We don't expect the list to be empty, that will only happen if we
33859915143SJens Axboe * race with the completion of the linked work.
33959915143SJens Axboe */
34059915143SJens Axboe if (prev) {
34159915143SJens Axboe io_remove_next_linked(prev);
34259915143SJens Axboe if (!req_ref_inc_not_zero(prev))
34359915143SJens Axboe prev = NULL;
34459915143SJens Axboe }
34559915143SJens Axboe list_del(&timeout->list);
34659915143SJens Axboe timeout->prev = prev;
34759915143SJens Axboe spin_unlock_irqrestore(&ctx->timeout_lock, flags);
34859915143SJens Axboe
34959915143SJens Axboe req->io_task_work.func = io_req_task_link_timeout;
35059915143SJens Axboe io_req_task_work_add(req);
35159915143SJens Axboe return HRTIMER_NORESTART;
35259915143SJens Axboe }
35359915143SJens Axboe
io_timeout_get_clock(struct io_timeout_data * data)35459915143SJens Axboe static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
35559915143SJens Axboe {
35659915143SJens Axboe switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
35759915143SJens Axboe case IORING_TIMEOUT_BOOTTIME:
35859915143SJens Axboe return CLOCK_BOOTTIME;
35959915143SJens Axboe case IORING_TIMEOUT_REALTIME:
36059915143SJens Axboe return CLOCK_REALTIME;
36159915143SJens Axboe default:
36259915143SJens Axboe /* can't happen, vetted at prep time */
36359915143SJens Axboe WARN_ON_ONCE(1);
36459915143SJens Axboe fallthrough;
36559915143SJens Axboe case 0:
36659915143SJens Axboe return CLOCK_MONOTONIC;
36759915143SJens Axboe }
36859915143SJens Axboe }
36959915143SJens Axboe
io_linked_timeout_update(struct io_ring_ctx * ctx,__u64 user_data,struct timespec64 * ts,enum hrtimer_mode mode)37059915143SJens Axboe static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
37159915143SJens Axboe struct timespec64 *ts, enum hrtimer_mode mode)
37259915143SJens Axboe __must_hold(&ctx->timeout_lock)
37359915143SJens Axboe {
37459915143SJens Axboe struct io_timeout_data *io;
37559915143SJens Axboe struct io_timeout *timeout;
37659915143SJens Axboe struct io_kiocb *req = NULL;
37759915143SJens Axboe
37859915143SJens Axboe list_for_each_entry(timeout, &ctx->ltimeout_list, list) {
37959915143SJens Axboe struct io_kiocb *tmp = cmd_to_io_kiocb(timeout);
38059915143SJens Axboe
38159915143SJens Axboe if (user_data == tmp->cqe.user_data) {
38259915143SJens Axboe req = tmp;
38359915143SJens Axboe break;
38459915143SJens Axboe }
38559915143SJens Axboe }
38659915143SJens Axboe if (!req)
38759915143SJens Axboe return -ENOENT;
38859915143SJens Axboe
38959915143SJens Axboe io = req->async_data;
39059915143SJens Axboe if (hrtimer_try_to_cancel(&io->timer) == -1)
39159915143SJens Axboe return -EALREADY;
39259915143SJens Axboe hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
39359915143SJens Axboe io->timer.function = io_link_timeout_fn;
39459915143SJens Axboe hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
39559915143SJens Axboe return 0;
39659915143SJens Axboe }
39759915143SJens Axboe
io_timeout_update(struct io_ring_ctx * ctx,__u64 user_data,struct timespec64 * ts,enum hrtimer_mode mode)39859915143SJens Axboe static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
39959915143SJens Axboe struct timespec64 *ts, enum hrtimer_mode mode)
40059915143SJens Axboe __must_hold(&ctx->timeout_lock)
40159915143SJens Axboe {
402faa9c0eeSJens Axboe struct io_cancel_data cd = { .ctx = ctx, .data = user_data, };
40359915143SJens Axboe struct io_kiocb *req = io_timeout_extract(ctx, &cd);
404f2ccb5aeSStefan Metzmacher struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
40559915143SJens Axboe struct io_timeout_data *data;
40659915143SJens Axboe
40759915143SJens Axboe if (IS_ERR(req))
40859915143SJens Axboe return PTR_ERR(req);
40959915143SJens Axboe
41059915143SJens Axboe timeout->off = 0; /* noseq */
41159915143SJens Axboe data = req->async_data;
41259915143SJens Axboe list_add_tail(&timeout->list, &ctx->timeout_list);
41359915143SJens Axboe hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
41459915143SJens Axboe data->timer.function = io_timeout_fn;
41559915143SJens Axboe hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
41659915143SJens Axboe return 0;
41759915143SJens Axboe }
41859915143SJens Axboe
io_timeout_remove_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)41959915143SJens Axboe int io_timeout_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
42059915143SJens Axboe {
421f2ccb5aeSStefan Metzmacher struct io_timeout_rem *tr = io_kiocb_to_cmd(req, struct io_timeout_rem);
42259915143SJens Axboe
42359915143SJens Axboe if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
42459915143SJens Axboe return -EINVAL;
42559915143SJens Axboe if (sqe->buf_index || sqe->len || sqe->splice_fd_in)
42659915143SJens Axboe return -EINVAL;
42759915143SJens Axboe
42859915143SJens Axboe tr->ltimeout = false;
42959915143SJens Axboe tr->addr = READ_ONCE(sqe->addr);
43059915143SJens Axboe tr->flags = READ_ONCE(sqe->timeout_flags);
43159915143SJens Axboe if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
43259915143SJens Axboe if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
43359915143SJens Axboe return -EINVAL;
43459915143SJens Axboe if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
43559915143SJens Axboe tr->ltimeout = true;
43659915143SJens Axboe if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
43759915143SJens Axboe return -EINVAL;
43859915143SJens Axboe if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
43959915143SJens Axboe return -EFAULT;
44059915143SJens Axboe if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0)
44159915143SJens Axboe return -EINVAL;
44259915143SJens Axboe } else if (tr->flags) {
44359915143SJens Axboe /* timeout removal doesn't support flags */
44459915143SJens Axboe return -EINVAL;
44559915143SJens Axboe }
44659915143SJens Axboe
44759915143SJens Axboe return 0;
44859915143SJens Axboe }
44959915143SJens Axboe
io_translate_timeout_mode(unsigned int flags)45059915143SJens Axboe static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
45159915143SJens Axboe {
45259915143SJens Axboe return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
45359915143SJens Axboe : HRTIMER_MODE_REL;
45459915143SJens Axboe }
45559915143SJens Axboe
45659915143SJens Axboe /*
45759915143SJens Axboe * Remove or update an existing timeout command
45859915143SJens Axboe */
io_timeout_remove(struct io_kiocb * req,unsigned int issue_flags)45959915143SJens Axboe int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
46059915143SJens Axboe {
461f2ccb5aeSStefan Metzmacher struct io_timeout_rem *tr = io_kiocb_to_cmd(req, struct io_timeout_rem);
46259915143SJens Axboe struct io_ring_ctx *ctx = req->ctx;
46359915143SJens Axboe int ret;
46459915143SJens Axboe
46559915143SJens Axboe if (!(tr->flags & IORING_TIMEOUT_UPDATE)) {
466faa9c0eeSJens Axboe struct io_cancel_data cd = { .ctx = ctx, .data = tr->addr, };
46759915143SJens Axboe
46859915143SJens Axboe spin_lock(&ctx->completion_lock);
46959915143SJens Axboe ret = io_timeout_cancel(ctx, &cd);
47059915143SJens Axboe spin_unlock(&ctx->completion_lock);
47159915143SJens Axboe } else {
47259915143SJens Axboe enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
47359915143SJens Axboe
47459915143SJens Axboe spin_lock_irq(&ctx->timeout_lock);
47559915143SJens Axboe if (tr->ltimeout)
47659915143SJens Axboe ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
47759915143SJens Axboe else
47859915143SJens Axboe ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
47959915143SJens Axboe spin_unlock_irq(&ctx->timeout_lock);
48059915143SJens Axboe }
48159915143SJens Axboe
48259915143SJens Axboe if (ret < 0)
48359915143SJens Axboe req_set_fail(req);
48459915143SJens Axboe io_req_set_res(req, ret, 0);
48559915143SJens Axboe return IOU_OK;
48659915143SJens Axboe }
48759915143SJens Axboe
__io_timeout_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe,bool is_timeout_link)48859915143SJens Axboe static int __io_timeout_prep(struct io_kiocb *req,
48959915143SJens Axboe const struct io_uring_sqe *sqe,
49059915143SJens Axboe bool is_timeout_link)
49159915143SJens Axboe {
492f2ccb5aeSStefan Metzmacher struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
49359915143SJens Axboe struct io_timeout_data *data;
49459915143SJens Axboe unsigned flags;
49559915143SJens Axboe u32 off = READ_ONCE(sqe->off);
49659915143SJens Axboe
49759915143SJens Axboe if (sqe->buf_index || sqe->len != 1 || sqe->splice_fd_in)
49859915143SJens Axboe return -EINVAL;
49959915143SJens Axboe if (off && is_timeout_link)
50059915143SJens Axboe return -EINVAL;
50159915143SJens Axboe flags = READ_ONCE(sqe->timeout_flags);
50259915143SJens Axboe if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
503ea97f6c8SDavid Wei IORING_TIMEOUT_ETIME_SUCCESS |
504ea97f6c8SDavid Wei IORING_TIMEOUT_MULTISHOT))
50559915143SJens Axboe return -EINVAL;
50659915143SJens Axboe /* more than one clock specified is invalid, obviously */
50759915143SJens Axboe if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
50859915143SJens Axboe return -EINVAL;
509ea97f6c8SDavid Wei /* multishot requests only make sense with rel values */
510ea97f6c8SDavid Wei if (!(~flags & (IORING_TIMEOUT_MULTISHOT | IORING_TIMEOUT_ABS)))
511ea97f6c8SDavid Wei return -EINVAL;
51259915143SJens Axboe
51359915143SJens Axboe INIT_LIST_HEAD(&timeout->list);
51459915143SJens Axboe timeout->off = off;
51559915143SJens Axboe if (unlikely(off && !req->ctx->off_timeout_used))
51659915143SJens Axboe req->ctx->off_timeout_used = true;
517ea97f6c8SDavid Wei /*
518ea97f6c8SDavid Wei * for multishot reqs w/ fixed nr of repeats, repeats tracks the
519ea97f6c8SDavid Wei * remaining nr
520ea97f6c8SDavid Wei */
521ea97f6c8SDavid Wei timeout->repeats = 0;
522ea97f6c8SDavid Wei if ((flags & IORING_TIMEOUT_MULTISHOT) && off > 0)
523ea97f6c8SDavid Wei timeout->repeats = off;
52459915143SJens Axboe
52559915143SJens Axboe if (WARN_ON_ONCE(req_has_async_data(req)))
52659915143SJens Axboe return -EFAULT;
52759915143SJens Axboe if (io_alloc_async_data(req))
52859915143SJens Axboe return -ENOMEM;
52959915143SJens Axboe
53059915143SJens Axboe data = req->async_data;
53159915143SJens Axboe data->req = req;
53259915143SJens Axboe data->flags = flags;
53359915143SJens Axboe
53459915143SJens Axboe if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
53559915143SJens Axboe return -EFAULT;
53659915143SJens Axboe
53759915143SJens Axboe if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0)
53859915143SJens Axboe return -EINVAL;
53959915143SJens Axboe
54059915143SJens Axboe data->mode = io_translate_timeout_mode(flags);
54159915143SJens Axboe hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
54259915143SJens Axboe
54359915143SJens Axboe if (is_timeout_link) {
54459915143SJens Axboe struct io_submit_link *link = &req->ctx->submit_state.link;
54559915143SJens Axboe
54659915143SJens Axboe if (!link->head)
54759915143SJens Axboe return -EINVAL;
54859915143SJens Axboe if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
54959915143SJens Axboe return -EINVAL;
55059915143SJens Axboe timeout->head = link->last;
55159915143SJens Axboe link->last->flags |= REQ_F_ARM_LTIMEOUT;
55259915143SJens Axboe }
55359915143SJens Axboe return 0;
55459915143SJens Axboe }
55559915143SJens Axboe
io_timeout_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)55659915143SJens Axboe int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
55759915143SJens Axboe {
55859915143SJens Axboe return __io_timeout_prep(req, sqe, false);
55959915143SJens Axboe }
56059915143SJens Axboe
io_link_timeout_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)56159915143SJens Axboe int io_link_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
56259915143SJens Axboe {
56359915143SJens Axboe return __io_timeout_prep(req, sqe, true);
56459915143SJens Axboe }
56559915143SJens Axboe
io_timeout(struct io_kiocb * req,unsigned int issue_flags)56659915143SJens Axboe int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
56759915143SJens Axboe {
568f2ccb5aeSStefan Metzmacher struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
56959915143SJens Axboe struct io_ring_ctx *ctx = req->ctx;
57059915143SJens Axboe struct io_timeout_data *data = req->async_data;
57159915143SJens Axboe struct list_head *entry;
57259915143SJens Axboe u32 tail, off = timeout->off;
57359915143SJens Axboe
57459915143SJens Axboe spin_lock_irq(&ctx->timeout_lock);
57559915143SJens Axboe
57659915143SJens Axboe /*
57759915143SJens Axboe * sqe->off holds how many events that need to occur for this
57859915143SJens Axboe * timeout event to be satisfied. If it isn't set, then this is
57959915143SJens Axboe * a pure timeout request, sequence isn't used.
58059915143SJens Axboe */
58159915143SJens Axboe if (io_is_timeout_noseq(req)) {
58259915143SJens Axboe entry = ctx->timeout_list.prev;
58359915143SJens Axboe goto add;
58459915143SJens Axboe }
58559915143SJens Axboe
5865498bf28SPavel Begunkov tail = data_race(ctx->cached_cq_tail) - atomic_read(&ctx->cq_timeouts);
58759915143SJens Axboe timeout->target_seq = tail + off;
58859915143SJens Axboe
58959915143SJens Axboe /* Update the last seq here in case io_flush_timeouts() hasn't.
59059915143SJens Axboe * This is safe because ->completion_lock is held, and submissions
59159915143SJens Axboe * and completions are never mixed in the same ->completion_lock section.
59259915143SJens Axboe */
59359915143SJens Axboe ctx->cq_last_tm_flush = tail;
59459915143SJens Axboe
59559915143SJens Axboe /*
59659915143SJens Axboe * Insertion sort, ensuring the first entry in the list is always
59759915143SJens Axboe * the one we need first.
59859915143SJens Axboe */
59959915143SJens Axboe list_for_each_prev(entry, &ctx->timeout_list) {
60059915143SJens Axboe struct io_timeout *nextt = list_entry(entry, struct io_timeout, list);
60159915143SJens Axboe struct io_kiocb *nxt = cmd_to_io_kiocb(nextt);
60259915143SJens Axboe
60359915143SJens Axboe if (io_is_timeout_noseq(nxt))
60459915143SJens Axboe continue;
60559915143SJens Axboe /* nxt.seq is behind @tail, otherwise would've been completed */
60659915143SJens Axboe if (off >= nextt->target_seq - tail)
60759915143SJens Axboe break;
60859915143SJens Axboe }
60959915143SJens Axboe add:
61059915143SJens Axboe list_add(&timeout->list, entry);
61159915143SJens Axboe data->timer.function = io_timeout_fn;
61259915143SJens Axboe hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
61359915143SJens Axboe spin_unlock_irq(&ctx->timeout_lock);
61459915143SJens Axboe return IOU_ISSUE_SKIP_COMPLETE;
61559915143SJens Axboe }
61659915143SJens Axboe
io_queue_linked_timeout(struct io_kiocb * req)61759915143SJens Axboe void io_queue_linked_timeout(struct io_kiocb *req)
61859915143SJens Axboe {
619f2ccb5aeSStefan Metzmacher struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
62059915143SJens Axboe struct io_ring_ctx *ctx = req->ctx;
62159915143SJens Axboe
62259915143SJens Axboe spin_lock_irq(&ctx->timeout_lock);
62359915143SJens Axboe /*
62459915143SJens Axboe * If the back reference is NULL, then our linked request finished
62559915143SJens Axboe * before we got a chance to setup the timer
62659915143SJens Axboe */
62759915143SJens Axboe if (timeout->head) {
62859915143SJens Axboe struct io_timeout_data *data = req->async_data;
62959915143SJens Axboe
63059915143SJens Axboe data->timer.function = io_link_timeout_fn;
63159915143SJens Axboe hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
63259915143SJens Axboe data->mode);
63359915143SJens Axboe list_add_tail(&timeout->list, &ctx->ltimeout_list);
63459915143SJens Axboe }
63559915143SJens Axboe spin_unlock_irq(&ctx->timeout_lock);
63659915143SJens Axboe /* drop submission reference */
63759915143SJens Axboe io_put_req(req);
63859915143SJens Axboe }
63959915143SJens Axboe
io_match_task(struct io_kiocb * head,struct task_struct * task,bool cancel_all)64059915143SJens Axboe static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
64159915143SJens Axboe bool cancel_all)
642*e142e9cdSPavel Begunkov __must_hold(&head->ctx->timeout_lock)
64359915143SJens Axboe {
64459915143SJens Axboe struct io_kiocb *req;
64559915143SJens Axboe
64659915143SJens Axboe if (task && head->task != task)
64759915143SJens Axboe return false;
64859915143SJens Axboe if (cancel_all)
64959915143SJens Axboe return true;
65059915143SJens Axboe
65159915143SJens Axboe io_for_each_link(req, head) {
65259915143SJens Axboe if (req->flags & REQ_F_INFLIGHT)
65359915143SJens Axboe return true;
65459915143SJens Axboe }
65559915143SJens Axboe return false;
65659915143SJens Axboe }
65759915143SJens Axboe
65859915143SJens Axboe /* Returns true if we found and killed one or more timeouts */
io_kill_timeouts(struct io_ring_ctx * ctx,struct task_struct * tsk,bool cancel_all)65959915143SJens Axboe __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
66059915143SJens Axboe bool cancel_all)
66159915143SJens Axboe {
66259915143SJens Axboe struct io_timeout *timeout, *tmp;
66359915143SJens Axboe int canceled = 0;
66459915143SJens Axboe
6656971253fSPavel Begunkov /*
6666971253fSPavel Begunkov * completion_lock is needed for io_match_task(). Take it before
6676971253fSPavel Begunkov * timeout_lockfirst to keep locking ordering.
6686971253fSPavel Begunkov */
6696971253fSPavel Begunkov spin_lock(&ctx->completion_lock);
67059915143SJens Axboe spin_lock_irq(&ctx->timeout_lock);
67159915143SJens Axboe list_for_each_entry_safe(timeout, tmp, &ctx->timeout_list, list) {
67259915143SJens Axboe struct io_kiocb *req = cmd_to_io_kiocb(timeout);
67359915143SJens Axboe
674ba3cdb6fSPavel Begunkov if (io_match_task(req, tsk, cancel_all) &&
675ba3cdb6fSPavel Begunkov io_kill_timeout(req, -ECANCELED))
67659915143SJens Axboe canceled++;
67759915143SJens Axboe }
67859915143SJens Axboe spin_unlock_irq(&ctx->timeout_lock);
6796971253fSPavel Begunkov spin_unlock(&ctx->completion_lock);
68059915143SJens Axboe return canceled != 0;
68159915143SJens Axboe }
682