xref: /linux/io_uring/timeout.c (revision 40162f8314a98948e0791b580024bbc899d2ee81)
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 <linux/time_namespace.h>
7 
8 #include <trace/events/io_uring.h>
9 
10 #include <uapi/linux/io_uring.h>
11 
12 #include "io_uring.h"
13 #include "refs.h"
14 #include "cancel.h"
15 #include "timeout.h"
16 
17 struct io_timeout {
18 	struct file			*file;
19 	u32				off;
20 	u32				target_seq;
21 	u32				repeats;
22 	struct list_head		list;
23 	/* head of the link, used by linked timeouts only */
24 	struct io_kiocb			*head;
25 	/* for linked completions */
26 	struct io_kiocb			*prev;
27 };
28 
29 struct io_timeout_rem {
30 	struct file			*file;
31 	u64				addr;
32 
33 	/* timeout update */
34 	ktime_t				time;
35 	u32				flags;
36 	bool				ltimeout;
37 };
38 
39 static clockid_t io_flags_to_clock(unsigned flags)
40 {
41 	switch (flags & IORING_TIMEOUT_CLOCK_MASK) {
42 	case IORING_TIMEOUT_BOOTTIME:
43 		return CLOCK_BOOTTIME;
44 	case IORING_TIMEOUT_REALTIME:
45 		return CLOCK_REALTIME;
46 	default:
47 		/* can't happen, vetted at prep time */
48 		WARN_ON_ONCE(1);
49 		fallthrough;
50 	case 0:
51 		return CLOCK_MONOTONIC;
52 	}
53 }
54 
55 static int io_parse_user_time(ktime_t *time, u64 arg, unsigned flags)
56 {
57 	struct timespec64 ts;
58 
59 	if (flags & IORING_TIMEOUT_IMMEDIATE_ARG) {
60 		*time = ns_to_ktime(arg);
61 		if (*time < 0)
62 			return -EINVAL;
63 		goto out;
64 	}
65 
66 	if (get_timespec64(&ts, u64_to_user_ptr(arg)))
67 		return -EFAULT;
68 	if (ts.tv_sec < 0 || ts.tv_nsec < 0)
69 		return -EINVAL;
70 	*time = timespec64_to_ktime(ts);
71 out:
72 	if (flags & IORING_TIMEOUT_ABS)
73 		*time = timens_ktime_to_host(io_flags_to_clock(flags), *time);
74 	return 0;
75 }
76 
77 static struct io_kiocb *__io_disarm_linked_timeout(struct io_kiocb *req,
78 						   struct io_kiocb *link);
79 
80 static inline bool io_is_timeout_noseq(struct io_kiocb *req)
81 {
82 	struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
83 	struct io_timeout_data *data = req->async_data;
84 
85 	return !timeout->off || data->flags & IORING_TIMEOUT_MULTISHOT;
86 }
87 
88 static inline void io_put_req(struct io_kiocb *req)
89 {
90 	if (req_ref_put_and_test(req)) {
91 		io_queue_next(req);
92 		io_free_req(req);
93 	}
94 }
95 
96 static inline bool io_timeout_finish(struct io_timeout *timeout,
97 				     struct io_timeout_data *data)
98 {
99 	if (!(data->flags & IORING_TIMEOUT_MULTISHOT))
100 		return true;
101 
102 	if (!timeout->off || (timeout->repeats && --timeout->repeats))
103 		return false;
104 
105 	return true;
106 }
107 
108 static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer);
109 
110 static void io_timeout_complete(struct io_tw_req tw_req, io_tw_token_t tw)
111 {
112 	struct io_kiocb *req = tw_req.req;
113 	struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
114 	struct io_timeout_data *data = req->async_data;
115 	struct io_ring_ctx *ctx = req->ctx;
116 
117 	if (!io_timeout_finish(timeout, data)) {
118 		if (!tw.cancel &&
119 		    io_req_post_cqe(req, -ETIME, IORING_CQE_F_MORE)) {
120 			/* re-arm timer */
121 			raw_spin_lock_irq(&ctx->timeout_lock);
122 			list_add(&timeout->list, ctx->timeout_list.prev);
123 			hrtimer_start(&data->timer, data->time, data->mode);
124 			raw_spin_unlock_irq(&ctx->timeout_lock);
125 			return;
126 		}
127 	}
128 
129 	io_req_task_complete(tw_req, tw);
130 }
131 
132 static __cold bool io_flush_killed_timeouts(struct list_head *list, int err)
133 {
134 	if (list_empty(list))
135 		return false;
136 
137 	while (!list_empty(list)) {
138 		struct io_timeout *timeout;
139 		struct io_kiocb *req;
140 
141 		timeout = list_first_entry(list, struct io_timeout, list);
142 		list_del_init(&timeout->list);
143 		req = cmd_to_io_kiocb(timeout);
144 		if (err)
145 			req_set_fail(req);
146 		io_req_queue_tw_complete(req, err);
147 	}
148 
149 	return true;
150 }
151 
152 static void io_kill_timeout(struct io_kiocb *req, struct list_head *list)
153 	__must_hold(&req->ctx->timeout_lock)
154 {
155 	struct io_timeout_data *io = req->async_data;
156 
157 	if (hrtimer_try_to_cancel(&io->timer) != -1) {
158 		struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
159 
160 		atomic_set(&req->ctx->cq_timeouts,
161 			atomic_read(&req->ctx->cq_timeouts) + 1);
162 		list_move_tail(&timeout->list, list);
163 	}
164 }
165 
166 __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
167 {
168 	struct io_timeout *timeout, *tmp;
169 	LIST_HEAD(list);
170 	u32 seq;
171 
172 	raw_spin_lock_irq(&ctx->timeout_lock);
173 	seq = READ_ONCE(ctx->cached_cq_tail) - atomic_read(&ctx->cq_timeouts);
174 
175 	/*
176 	 * A fired timeout bumps ->cq_timeouts before its CQE is posted, which
177 	 * leaves the sequence behind the last flush until it is. No event has
178 	 * happened then, see io_timeout().
179 	 */
180 	if ((s32)(seq - ctx->cq_last_tm_flush) < 0)
181 		seq = ctx->cq_last_tm_flush;
182 
183 	list_for_each_entry_safe(timeout, tmp, &ctx->timeout_list, list) {
184 		struct io_kiocb *req = cmd_to_io_kiocb(timeout);
185 		u32 events_needed, events_got;
186 
187 		if (io_is_timeout_noseq(req))
188 			break;
189 
190 		/*
191 		 * Since seq can easily wrap around over time, subtract
192 		 * the last seq at which timeouts were flushed before comparing.
193 		 * Assuming not more than 2^31-1 events have happened since,
194 		 * these subtractions won't have wrapped, so we can check if
195 		 * target is in [last_seq, current_seq] by comparing the two.
196 		 */
197 		events_needed = timeout->target_seq - ctx->cq_last_tm_flush;
198 		events_got = seq - ctx->cq_last_tm_flush;
199 		if (events_got < events_needed)
200 			break;
201 
202 		io_kill_timeout(req, &list);
203 	}
204 	ctx->cq_last_tm_flush = seq;
205 	raw_spin_unlock_irq(&ctx->timeout_lock);
206 	io_flush_killed_timeouts(&list, 0);
207 }
208 
209 static void io_req_tw_fail_links(struct io_tw_req tw_req, io_tw_token_t tw)
210 {
211 	struct io_kiocb *link = tw_req.req;
212 
213 	io_tw_lock(link->ctx, tw);
214 	while (link) {
215 		struct io_kiocb *nxt = link->link;
216 		long res = -ECANCELED;
217 
218 		if (link->flags & REQ_F_FAIL)
219 			res = link->cqe.res;
220 		link->link = NULL;
221 		io_req_set_res(link, res, 0);
222 		io_req_task_complete((struct io_tw_req){link}, tw);
223 		link = nxt;
224 	}
225 }
226 
227 static void io_fail_links(struct io_kiocb *req)
228 	__must_hold(&req->ctx->completion_lock)
229 {
230 	struct io_kiocb *link = req->link;
231 	bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES;
232 
233 	if (!link)
234 		return;
235 
236 	while (link) {
237 		if (ignore_cqes)
238 			link->flags |= REQ_F_CQE_SKIP;
239 		else
240 			link->flags &= ~REQ_F_CQE_SKIP;
241 		trace_io_uring_fail_link(req, link);
242 		link = link->link;
243 	}
244 
245 	link = req->link;
246 	link->io_task_work.func = io_req_tw_fail_links;
247 	io_req_task_work_add(link);
248 	req->link = NULL;
249 }
250 
251 static inline void io_remove_next_linked(struct io_kiocb *req)
252 {
253 	struct io_kiocb *nxt = req->link;
254 
255 	req->link = nxt->link;
256 	nxt->link = NULL;
257 }
258 
259 void io_disarm_next(struct io_kiocb *req)
260 	__must_hold(&req->ctx->completion_lock)
261 {
262 	struct io_kiocb *link = NULL;
263 
264 	if (req->flags & REQ_F_ARM_LTIMEOUT) {
265 		link = req->link;
266 		req->flags &= ~REQ_F_ARM_LTIMEOUT;
267 		if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
268 			io_remove_next_linked(req);
269 			io_req_queue_tw_complete(link, -ECANCELED);
270 		}
271 	} else if (req->flags & REQ_F_LINK_TIMEOUT) {
272 		struct io_ring_ctx *ctx = req->ctx;
273 
274 		raw_spin_lock_irq(&ctx->timeout_lock);
275 		if (req->link && req->link->opcode == IORING_OP_LINK_TIMEOUT)
276 			link = __io_disarm_linked_timeout(req, req->link);
277 
278 		raw_spin_unlock_irq(&ctx->timeout_lock);
279 		if (link)
280 			io_req_queue_tw_complete(link, -ECANCELED);
281 	}
282 	if (unlikely((req->flags & REQ_F_FAIL) &&
283 		     !(req->flags & REQ_F_HARDLINK)))
284 		io_fail_links(req);
285 }
286 
287 static struct io_kiocb *__io_disarm_linked_timeout(struct io_kiocb *req,
288 						   struct io_kiocb *link)
289 	__must_hold(&req->ctx->completion_lock)
290 	__must_hold(&req->ctx->timeout_lock)
291 {
292 	struct io_timeout_data *io = link->async_data;
293 	struct io_timeout *timeout = io_kiocb_to_cmd(link, struct io_timeout);
294 
295 	io_remove_next_linked(req);
296 
297 	/* If this is NULL, then timer already claimed it and will complete it */
298 	if (!timeout->head)
299 		return NULL;
300 	timeout->head = NULL;
301 	if (hrtimer_try_to_cancel(&io->timer) != -1) {
302 		list_del(&timeout->list);
303 		return link;
304 	}
305 
306 	return NULL;
307 }
308 
309 static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
310 {
311 	struct io_timeout_data *data = container_of(timer,
312 						struct io_timeout_data, timer);
313 	struct io_kiocb *req = data->req;
314 	struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
315 	struct io_ring_ctx *ctx = req->ctx;
316 	unsigned long flags;
317 
318 	raw_spin_lock_irqsave(&ctx->timeout_lock, flags);
319 	list_del_init(&timeout->list);
320 	atomic_set(&ctx->cq_timeouts,
321 		atomic_read(&ctx->cq_timeouts) + 1);
322 	raw_spin_unlock_irqrestore(&ctx->timeout_lock, flags);
323 
324 	if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
325 		req_set_fail(req);
326 
327 	io_req_set_res(req, -ETIME, 0);
328 	req->io_task_work.func = io_timeout_complete;
329 	io_req_task_work_add(req);
330 	return HRTIMER_NORESTART;
331 }
332 
333 static struct io_kiocb *io_timeout_find(struct io_ring_ctx *ctx,
334 					struct io_cancel_data *cd)
335 	__must_hold(&ctx->timeout_lock)
336 {
337 	struct io_timeout *timeout;
338 
339 	list_for_each_entry(timeout, &ctx->timeout_list, list) {
340 		struct io_kiocb *req = cmd_to_io_kiocb(timeout);
341 
342 		if (io_cancel_req_match(req, cd))
343 			return req;
344 	}
345 	return NULL;
346 }
347 
348 static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
349 					   struct io_cancel_data *cd)
350 	__must_hold(&ctx->timeout_lock)
351 {
352 	struct io_kiocb *req = io_timeout_find(ctx, cd);
353 	struct io_timeout *timeout;
354 	struct io_timeout_data *io;
355 
356 	if (!req)
357 		return ERR_PTR(-ENOENT);
358 
359 	io = req->async_data;
360 	if (hrtimer_try_to_cancel(&io->timer) == -1)
361 		return ERR_PTR(-EALREADY);
362 	timeout = io_kiocb_to_cmd(req, struct io_timeout);
363 	list_del_init(&timeout->list);
364 	return req;
365 }
366 
367 int io_timeout_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
368 	__must_hold(&ctx->completion_lock)
369 {
370 	struct io_kiocb *req;
371 
372 	raw_spin_lock_irq(&ctx->timeout_lock);
373 	req = io_timeout_extract(ctx, cd);
374 	if (!IS_ERR(req))
375 		atomic_set(&ctx->cq_timeouts,
376 			   atomic_read(&ctx->cq_timeouts) + 1);
377 	raw_spin_unlock_irq(&ctx->timeout_lock);
378 
379 	if (IS_ERR(req))
380 		return PTR_ERR(req);
381 	io_req_task_queue_fail(req, -ECANCELED);
382 	return 0;
383 }
384 
385 static void io_req_task_link_timeout(struct io_tw_req tw_req, io_tw_token_t tw)
386 {
387 	struct io_kiocb *req = tw_req.req;
388 	struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
389 	struct io_kiocb *prev = timeout->prev;
390 	int ret;
391 
392 	if (prev) {
393 		/*
394 		 * splice the linked timeout out of prev's chain if the regular
395 		 * completion path didn't already do it. io-wq cancellation
396 		 * walks the chain with only ->timeout_lock held.
397 		 */
398 		raw_spin_lock_irq(&req->ctx->timeout_lock);
399 		if (prev->link == req)
400 			prev->link = req->link;
401 		req->link = NULL;
402 		raw_spin_unlock_irq(&req->ctx->timeout_lock);
403 
404 		if (!tw.cancel) {
405 			struct io_cancel_data cd = {
406 				.ctx		= req->ctx,
407 				.data		= prev->cqe.user_data,
408 			};
409 
410 			ret = io_try_cancel(req->tctx, &cd, 0);
411 			/* a number of requests for futex and waitid */
412 			if (ret > 0)
413 				ret = 0;
414 		} else {
415 			ret = -ECANCELED;
416 		}
417 		io_req_set_res(req, ret ?: -ETIME, 0);
418 		io_req_task_complete(tw_req, tw);
419 		io_put_req(prev);
420 	} else {
421 		io_req_set_res(req, -ETIME, 0);
422 		io_req_task_complete(tw_req, tw);
423 	}
424 }
425 
426 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
427 {
428 	struct io_timeout_data *data = container_of(timer,
429 						struct io_timeout_data, timer);
430 	struct io_kiocb *prev, *req = data->req;
431 	struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
432 	struct io_ring_ctx *ctx = req->ctx;
433 	unsigned long flags;
434 
435 	raw_spin_lock_irqsave(&ctx->timeout_lock, flags);
436 	prev = timeout->head;
437 	timeout->head = NULL;
438 
439 	/*
440 	 * We don't expect the list to be empty, that will only happen if we
441 	 * race with the completion of the linked work. Splice of prev is
442 	 * done in io_req_task_link_timeout(), if needed.
443 	 */
444 	if (prev) {
445 		if (!req_ref_inc_not_zero(prev)) {
446 			io_remove_next_linked(prev);
447 			prev = NULL;
448 		}
449 	}
450 	list_del(&timeout->list);
451 	timeout->prev = prev;
452 	raw_spin_unlock_irqrestore(&ctx->timeout_lock, flags);
453 
454 	req->io_task_work.func = io_req_task_link_timeout;
455 	io_req_task_work_add(req);
456 	return HRTIMER_NORESTART;
457 }
458 
459 static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
460 {
461 	return io_flags_to_clock(data->flags);
462 }
463 
464 static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
465 				    ktime_t ts, enum hrtimer_mode mode)
466 	__must_hold(&ctx->timeout_lock)
467 {
468 	struct io_timeout_data *io;
469 	struct io_timeout *timeout;
470 	struct io_kiocb *req = NULL;
471 
472 	list_for_each_entry(timeout, &ctx->ltimeout_list, list) {
473 		struct io_kiocb *tmp = cmd_to_io_kiocb(timeout);
474 
475 		if (user_data == tmp->cqe.user_data) {
476 			req = tmp;
477 			break;
478 		}
479 	}
480 	if (!req)
481 		return -ENOENT;
482 
483 	io = req->async_data;
484 	if (hrtimer_try_to_cancel(&io->timer) == -1)
485 		return -EALREADY;
486 	if (mode == HRTIMER_MODE_ABS)
487 		ts = timens_ktime_to_host(io_timeout_get_clock(io), ts);
488 	hrtimer_setup(&io->timer, io_link_timeout_fn, io_timeout_get_clock(io), mode);
489 	hrtimer_start(&io->timer, ts, mode);
490 	return 0;
491 }
492 
493 static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
494 			     ktime_t time, enum hrtimer_mode mode)
495 	__must_hold(&ctx->timeout_lock)
496 {
497 	struct io_cancel_data cd = { .ctx = ctx, .data = user_data, };
498 	struct io_kiocb *req = io_timeout_find(ctx, &cd);
499 	struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
500 	struct io_timeout_data *data;
501 
502 	if (!req)
503 		return -ENOENT;
504 
505 	data = req->async_data;
506 	/* multishot requests only make sense with rel values */
507 	if ((data->flags & IORING_TIMEOUT_MULTISHOT) && mode == HRTIMER_MODE_ABS)
508 		return -EINVAL;
509 
510 	req = io_timeout_extract(ctx, &cd);
511 	if (IS_ERR(req))
512 		return PTR_ERR(req);
513 	/* for a multishot request this isn't a sequence, keep the repeats */
514 	if (!(data->flags & IORING_TIMEOUT_MULTISHOT))
515 		timeout->off = 0; /* noseq */
516 	if (mode == HRTIMER_MODE_ABS)
517 		time = timens_ktime_to_host(io_timeout_get_clock(data), time);
518 	data->time = time;
519 
520 	list_add_tail(&timeout->list, &ctx->timeout_list);
521 	hrtimer_setup(&data->timer, io_timeout_fn, io_timeout_get_clock(data), mode);
522 	hrtimer_start(&data->timer, data->time, mode);
523 	return 0;
524 }
525 
526 int io_timeout_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
527 {
528 	struct io_timeout_rem *tr = io_kiocb_to_cmd(req, struct io_timeout_rem);
529 	int ret;
530 
531 	if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
532 		return -EINVAL;
533 	if (sqe->addr3 || sqe->__pad2[0])
534 		return -EINVAL;
535 	if (sqe->buf_index || sqe->len || sqe->splice_fd_in)
536 		return -EINVAL;
537 
538 	tr->ltimeout = false;
539 	tr->addr = READ_ONCE(sqe->addr);
540 	tr->flags = READ_ONCE(sqe->timeout_flags);
541 	if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
542 		if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
543 			return -EINVAL;
544 		if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
545 			tr->ltimeout = true;
546 		if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK |
547 				  IORING_TIMEOUT_ABS |
548 				  IORING_TIMEOUT_IMMEDIATE_ARG))
549 			return -EINVAL;
550 		/*
551 		 * The clock is that of the timeout to update, an absolute time
552 		 * is converted to the host's view once that has been found.
553 		 */
554 		ret = io_parse_user_time(&tr->time, READ_ONCE(sqe->addr2),
555 					 tr->flags & ~IORING_TIMEOUT_ABS);
556 		if (ret)
557 			return ret;
558 	} else if (tr->flags) {
559 		/* timeout removal doesn't support flags */
560 		return -EINVAL;
561 	}
562 
563 	return 0;
564 }
565 
566 static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
567 {
568 	return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
569 					    : HRTIMER_MODE_REL;
570 }
571 
572 /*
573  * Remove or update an existing timeout command
574  */
575 int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
576 {
577 	struct io_timeout_rem *tr = io_kiocb_to_cmd(req, struct io_timeout_rem);
578 	struct io_ring_ctx *ctx = req->ctx;
579 	int ret;
580 
581 	if (!(tr->flags & IORING_TIMEOUT_UPDATE)) {
582 		struct io_cancel_data cd = { .ctx = ctx, .data = tr->addr, };
583 
584 		spin_lock(&ctx->completion_lock);
585 		ret = io_timeout_cancel(ctx, &cd);
586 		spin_unlock(&ctx->completion_lock);
587 	} else {
588 		enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
589 
590 		raw_spin_lock_irq(&ctx->timeout_lock);
591 		if (tr->ltimeout)
592 			ret = io_linked_timeout_update(ctx, tr->addr, tr->time, mode);
593 		else
594 			ret = io_timeout_update(ctx, tr->addr, tr->time, mode);
595 		raw_spin_unlock_irq(&ctx->timeout_lock);
596 	}
597 
598 	if (ret < 0)
599 		req_set_fail(req);
600 	io_req_set_res(req, ret, 0);
601 	return IOU_COMPLETE;
602 }
603 
604 static int __io_timeout_prep(struct io_kiocb *req,
605 			     const struct io_uring_sqe *sqe,
606 			     bool is_timeout_link)
607 {
608 	struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
609 	struct io_timeout_data *data;
610 	unsigned flags;
611 	u32 off = READ_ONCE(sqe->off);
612 	int ret;
613 
614 	if (sqe->addr3 || sqe->__pad2[0])
615 		return -EINVAL;
616 	if (sqe->buf_index || sqe->len != 1 || sqe->splice_fd_in)
617 		return -EINVAL;
618 	if (off && is_timeout_link)
619 		return -EINVAL;
620 	flags = READ_ONCE(sqe->timeout_flags);
621 	if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
622 		      IORING_TIMEOUT_ETIME_SUCCESS |
623 		      IORING_TIMEOUT_MULTISHOT |
624 		      IORING_TIMEOUT_IMMEDIATE_ARG))
625 		return -EINVAL;
626 	/* more than one clock specified is invalid, obviously */
627 	if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
628 		return -EINVAL;
629 	/* multishot requests only make sense with rel values */
630 	if (!(~flags & (IORING_TIMEOUT_MULTISHOT | IORING_TIMEOUT_ABS)))
631 		return -EINVAL;
632 
633 	INIT_LIST_HEAD(&timeout->list);
634 	timeout->off = off;
635 	if (unlikely(off && !(req->ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)))
636 		req->ctx->int_flags |= IO_RING_F_OFF_TIMEOUT_USED;
637 	/*
638 	 * for multishot reqs w/ fixed nr of repeats, repeats tracks the
639 	 * remaining nr
640 	 */
641 	timeout->repeats = 0;
642 	if ((flags & IORING_TIMEOUT_MULTISHOT) && off > 0)
643 		timeout->repeats = off;
644 
645 	if (WARN_ON_ONCE(req_has_async_data(req)))
646 		return -EFAULT;
647 	data = io_uring_alloc_async_data(NULL, req);
648 	if (!data)
649 		return -ENOMEM;
650 	data->req = req;
651 	data->flags = flags;
652 
653 	ret = io_parse_user_time(&data->time, READ_ONCE(sqe->addr), flags);
654 	if (ret)
655 		return ret;
656 
657 	data->mode = io_translate_timeout_mode(flags);
658 
659 	if (is_timeout_link) {
660 		struct io_submit_link *link = &req->ctx->submit_state.link;
661 
662 		if (!link->head)
663 			return -EINVAL;
664 		if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
665 			return -EINVAL;
666 		timeout->head = link->last;
667 		link->last->flags |= REQ_F_ARM_LTIMEOUT;
668 		hrtimer_setup(&data->timer, io_link_timeout_fn, io_timeout_get_clock(data),
669 			      data->mode);
670 	} else {
671 		hrtimer_setup(&data->timer, io_timeout_fn, io_timeout_get_clock(data), data->mode);
672 	}
673 	return 0;
674 }
675 
676 int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
677 {
678 	return __io_timeout_prep(req, sqe, false);
679 }
680 
681 int io_link_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
682 {
683 	return __io_timeout_prep(req, sqe, true);
684 }
685 
686 int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
687 {
688 	struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
689 	struct io_ring_ctx *ctx = req->ctx;
690 	struct io_timeout_data *data = req->async_data;
691 	struct io_timeout *first;
692 	struct list_head *entry;
693 	u32 tail, base, off = timeout->off;
694 
695 	raw_spin_lock_irq(&ctx->timeout_lock);
696 
697 	/* the list is searched under ->timeout_lock, see io_wq_cancel_seen() */
698 	if (__io_wq_cancel_seen(req, issue_flags)) {
699 		raw_spin_unlock_irq(&ctx->timeout_lock);
700 		io_req_task_queue_fail(req, -ECANCELED);
701 		return IOU_ISSUE_SKIP_COMPLETE;
702 	}
703 
704 	/*
705 	 * sqe->off holds how many events that need to occur for this
706 	 * timeout event to be satisfied. If it isn't set, then this is
707 	 * a pure timeout request, sequence isn't used.
708 	 */
709 	if (io_is_timeout_noseq(req)) {
710 		entry = ctx->timeout_list.prev;
711 		goto add;
712 	}
713 
714 	tail = data_race(ctx->cached_cq_tail) - atomic_read(&ctx->cq_timeouts);
715 
716 	/*
717 	 * Update the last seq here in case io_flush_timeouts() hasn't, it only
718 	 * gets called once sequences are in use. Leave it alone if there are
719 	 * timeouts waiting for one. CQEs may have been posted since the last
720 	 * flush, and a target that has been reached must not fall behind it.
721 	 */
722 	first = list_first_entry_or_null(&ctx->timeout_list, struct io_timeout,
723 					 list);
724 	if (!first || io_is_timeout_noseq(cmd_to_io_kiocb(first)))
725 		ctx->cq_last_tm_flush = tail;
726 	base = ctx->cq_last_tm_flush;
727 
728 	/*
729 	 * A fired timeout bumps ->cq_timeouts before its CQE is posted, which
730 	 * leaves the sequence behind the last flush until it is. Nothing else
731 	 * separates the two then, so count from the flush.
732 	 */
733 	if ((s32)(tail - base) < 0)
734 		tail = base;
735 	timeout->target_seq = tail + off;
736 
737 	/*
738 	 * Insertion sort, ensuring the first entry in the list is always
739 	 * the one we need first.
740 	 */
741 	list_for_each_prev(entry, &ctx->timeout_list) {
742 		struct io_timeout *nextt = list_entry(entry, struct io_timeout, list);
743 		struct io_kiocb *nxt = cmd_to_io_kiocb(nextt);
744 
745 		if (io_is_timeout_noseq(nxt))
746 			continue;
747 		/* no target is behind the last flush, see io_flush_timeouts() */
748 		if (timeout->target_seq - base >= nextt->target_seq - base)
749 			break;
750 	}
751 add:
752 	list_add(&timeout->list, entry);
753 	hrtimer_start(&data->timer, data->time, data->mode);
754 	raw_spin_unlock_irq(&ctx->timeout_lock);
755 	return IOU_ISSUE_SKIP_COMPLETE;
756 }
757 
758 void io_queue_linked_timeout(struct io_kiocb *req)
759 {
760 	struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
761 	struct io_ring_ctx *ctx = req->ctx;
762 
763 	raw_spin_lock_irq(&ctx->timeout_lock);
764 	/*
765 	 * If the back reference is NULL, then our linked request finished
766 	 * before we got a chance to setup the timer
767 	 */
768 	if (timeout->head) {
769 		struct io_timeout_data *data = req->async_data;
770 
771 		hrtimer_start(&data->timer, data->time, data->mode);
772 		list_add_tail(&timeout->list, &ctx->ltimeout_list);
773 	}
774 	raw_spin_unlock_irq(&ctx->timeout_lock);
775 	/* drop submission reference */
776 	io_put_req(req);
777 }
778 
779 static bool io_match_task(struct io_kiocb *head, struct io_uring_task *tctx,
780 			  bool cancel_all)
781 	__must_hold(&head->ctx->timeout_lock)
782 {
783 	struct io_kiocb *req;
784 
785 	if (tctx && head->tctx != tctx)
786 		return false;
787 	if (cancel_all)
788 		return true;
789 
790 	io_for_each_link(req, head) {
791 		if (req->flags & REQ_F_INFLIGHT)
792 			return true;
793 	}
794 	return false;
795 }
796 
797 __cold unsigned int io_timeouts_armed(struct io_ring_ctx *ctx,
798 				      struct io_uring_task *tctx)
799 {
800 	struct io_timeout *timeout;
801 	unsigned int nr = 0;
802 
803 	guard(mutex)(&ctx->uring_lock);
804 	raw_spin_lock_irq(&ctx->timeout_lock);
805 	list_for_each_entry(timeout, &ctx->timeout_list, list) {
806 		struct io_kiocb *req = cmd_to_io_kiocb(timeout);
807 
808 		if (req->tctx == tctx)
809 			nr += io_linked_nr(req);
810 	}
811 	raw_spin_unlock_irq(&ctx->timeout_lock);
812 	return nr;
813 }
814 
815 /* Returns true if we found and killed one or more timeouts */
816 __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, struct io_uring_task *tctx,
817 			     bool cancel_all)
818 {
819 	struct io_timeout *timeout, *tmp;
820 	LIST_HEAD(list);
821 
822 	/*
823 	 * completion_lock is needed for io_match_task(). Take it before
824 	 * timeout_lockfirst to keep locking ordering.
825 	 */
826 	spin_lock(&ctx->completion_lock);
827 	raw_spin_lock_irq(&ctx->timeout_lock);
828 	list_for_each_entry_safe(timeout, tmp, &ctx->timeout_list, list) {
829 		struct io_kiocb *req = cmd_to_io_kiocb(timeout);
830 
831 		if (io_match_task(req, tctx, cancel_all))
832 			io_kill_timeout(req, &list);
833 	}
834 	raw_spin_unlock_irq(&ctx->timeout_lock);
835 	spin_unlock(&ctx->completion_lock);
836 
837 	return io_flush_killed_timeouts(&list, -ECANCELED);
838 }
839