Skip to contentSkip to conversation

io_uring: add support for multishot timeouts

PATCH Superseded David Wei 2 participants 2 messages Mar 23, 2023 – Mar 27, 2023 io_uring

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

David Wei

author
#0 lore raw

PATCH 1/1 io_uring: add support for multishot timeouts

A multishot timeout submission will repeatedly generate completions with the IORING_CQE_F_MORE cflag set. Depending on the value of the off' field in the submission, these timeouts can either repeat indefinitely until cancelled (off' = 0) or for a fixed number of times (`off' > 0).

Only noseq timeouts (i.e. not dependent on the number of I/O completions) are supported.

For the second case, the target_seq' field in struct io_timeout' is re-purposed to track the remaining number of timeouts.

---
 include/uapi/linux/io_uring.h |  1 +
 io_uring/timeout.c            | 35 +++++++++++++++++++++++++++++++++--
 2 files changed, 34 insertions(+), 2 deletions(-)
include/uapi/linux/io_uring.h +1 -0
index 1d59c816a5b8..59b9112adb04 100644
@@ -250,6 +250,7 @@ enum io_uring_op {
 #define IORING_TIMEOUT_REALTIME		(1U << 3)
 #define IORING_LINK_TIMEOUT_UPDATE	(1U << 4)
 #define IORING_TIMEOUT_ETIME_SUCCESS	(1U << 5)
+#define IORING_TIMEOUT_MULTISHOT	(1U << 6)
 #define IORING_TIMEOUT_CLOCK_MASK	(IORING_TIMEOUT_BOOTTIME | IORING_TIMEOUT_REALTIME)
 #define IORING_TIMEOUT_UPDATE_MASK	(IORING_TIMEOUT_UPDATE | IORING_LINK_TIMEOUT_UPDATE)
 /*
io_uring/timeout.c +33 -2
index 826a51bca3e4..219fd30fed5b 100644
@@ -37,8 +37,9 @@ struct io_timeout_rem {
 static inline bool io_is_timeout_noseq(struct io_kiocb *req)
 {
 	struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);
+	struct io_timeout_data *data = req->async_data;
 
-	return !timeout->off;
+	return !timeout->off || data->flags & IORING_TIMEOUT_MULTISHOT;
 }
 
 static inline void io_put_req(struct io_kiocb *req)
@@ -49,6 +50,18 @@ static inline void io_put_req(struct io_kiocb *req)
 	}
 }
 
+static inline bool io_timeout_finish(struct io_timeout *timeout,
+				     struct io_timeout_data *data)
+{
+	if (!(data->flags & IORING_TIMEOUT_MULTISHOT))
+		return true;
+
+	if (!timeout->off || (timeout->target_seq && --timeout->target_seq))
+		return false;
+
+	return true;
+}
+
 static bool io_kill_timeout(struct io_kiocb *req, int status)
 	__must_hold(&req->ctx->timeout_lock)
 {
@@ -202,6 +215,13 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
 	struct io_ring_ctx *ctx = req->ctx;
 	unsigned long flags;
 
+	if (!io_timeout_finish(timeout, data)) {
+		io_aux_cqe(req->ctx, false, req->cqe.user_data, -ETIME,
+			   IORING_CQE_F_MORE, true);
+		hrtimer_forward_now(&data->timer, timespec64_to_ktime(data->ts));
+		return HRTIMER_RESTART;
+	}
+
 	spin_lock_irqsave(&ctx->timeout_lock, flags);
 	list_del_init(&timeout->list);
 	atomic_set(&req->ctx->cq_timeouts,
@@ -470,16 +490,27 @@ static int __io_timeout_prep(struct io_kiocb *req,
 		return -EINVAL;
 	flags = READ_ONCE(sqe->timeout_flags);
 	if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
-		      IORING_TIMEOUT_ETIME_SUCCESS))
+		      IORING_TIMEOUT_ETIME_SUCCESS |
+		      IORING_TIMEOUT_MULTISHOT)) {
 		return -EINVAL;
+	}
 	/* more than one clock specified is invalid, obviously */
 	if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
 		return -EINVAL;
+	/* multishot requests only make sense with rel values */
+	if (!(~flags & (IORING_TIMEOUT_MULTISHOT | IORING_TIMEOUT_ABS)))
+		return -EINVAL;
 
 	INIT_LIST_HEAD(&timeout->list);
 	timeout->off = off;
 	if (unlikely(off && !req->ctx->off_timeout_used))
 		req->ctx->off_timeout_used = true;
+	/*
+	 * for multishot reqs w/ fixed nr of repeats, target_seq tracks the
+	 * remaining nr
+	 */
+	if ((flags & IORING_TIMEOUT_MULTISHOT) && off > 0)
+		timeout->target_seq = off;
 
 	if (WARN_ON_ONCE(req_has_async_data(req)))
 		return -EFAULT;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 7 lines · David Wei

A multishot timeout submission will repeatedly generate completions with the IORING_CQE_F_MORE cflag set. Depending on the value of the off' field in the submission, these timeouts can either repeat indefinitely until cancelled (off' = 0) or for a fixed number of times (`off' > 0).

Only noseq timeouts (i.e. not dependent on the number of I/O completions) are supported.

It's ok, I'm not sure there is anyone using sequences

Quoted 2 lines

For the second case, the target_seq' field in struct io_timeout' is re-purposed to track the remaining number of timeouts.

We have space in struct io_timeout, let's just add another field there.

[...]

io_uring/timeout.c static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) · line 220

Quoted 10 lines
  static bool io_kill_timeout(struct io_kiocb *req, int status)
  	__must_hold(&req->ctx->timeout_lock)
  {
io_uring/timeout.c +3 -0
@@ -202,6 +215,13 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
  	struct io_ring_ctx *ctx = req->ctx;
  	unsigned long flags;
 
+	if (!io_timeout_finish(timeout, data)) {
+		io_aux_cqe(req->ctx, false, req->cqe.user_data, -ETIME,
+			   IORING_CQE_F_MORE, true);

We can't post a cqe from here, it should be a task context, e.g. using tw

Quoted 4 lines
io_uring/timeout.c +4 -0
+		hrtimer_forward_now(&data->timer, timespec64_to_ktime(data->ts));
+		return HRTIMER_RESTART;
+	}
+

[...]

Signature · 1 line
Pavel Begunkov