[PATCH 16/31] io_uring/timeout: Switch to use hrtimer_setup()

Nam Cao posted 31 patches 1 year, 3 months ago
[PATCH 16/31] io_uring/timeout: Switch to use hrtimer_setup()
Posted by Nam Cao 1 year, 3 months ago
There is a newly introduced hrtimer_setup() which will replace
hrtimer_init(). This new function is similar to the old one, except that it
also sanity-checks and initializes the timer's callback function.

Switch to use the new function.

This new function is also used to initialize the callback function in
.prep() (the callback function depends on whether it is IORING_OP_TIMEOUT
or IORING_OP_LINK_TIMEOUT). Thus, callback function setup in io_timeout()
and io_queue_linked_timeout() are now redundant, therefore remove them.

Signed-off-by: Nam Cao <namcao@linutronix.de>
---
Cc: Jens Axboe <axboe@kernel.dk>
---
 io_uring/timeout.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/io_uring/timeout.c b/io_uring/timeout.c
index 2ffe5e1dc68a..a4165e54238b 100644
--- a/io_uring/timeout.c
+++ b/io_uring/timeout.c
@@ -388,8 +388,7 @@ static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
 	io = req->async_data;
 	if (hrtimer_try_to_cancel(&io->timer) == -1)
 		return -EALREADY;
-	hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
-	io->timer.function = io_link_timeout_fn;
+	hrtimer_setup(&io->timer, io_link_timeout_fn, io_timeout_get_clock(io), mode);
 	hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
 	return 0;
 }
@@ -409,8 +408,7 @@ static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
 	timeout->off = 0; /* noseq */
 	data = req->async_data;
 	list_add_tail(&timeout->list, &ctx->timeout_list);
-	hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
-	data->timer.function = io_timeout_fn;
+	hrtimer_setup(&data->timer, io_timeout_fn, io_timeout_get_clock(data), mode);
 	hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
 	return 0;
 }
@@ -537,7 +535,6 @@ static int __io_timeout_prep(struct io_kiocb *req,
 		return -EINVAL;
 
 	data->mode = io_translate_timeout_mode(flags);
-	hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
 
 	if (is_timeout_link) {
 		struct io_submit_link *link = &req->ctx->submit_state.link;
@@ -548,6 +545,10 @@ static int __io_timeout_prep(struct io_kiocb *req,
 			return -EINVAL;
 		timeout->head = link->last;
 		link->last->flags |= REQ_F_ARM_LTIMEOUT;
+		hrtimer_setup(&data->timer, io_link_timeout_fn, io_timeout_get_clock(data),
+			      data->mode);
+	} else {
+		hrtimer_setup(&data->timer, io_timeout_fn, io_timeout_get_clock(data), data->mode);
 	}
 	return 0;
 }
@@ -607,7 +608,6 @@ int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
 	}
 add:
 	list_add(&timeout->list, entry);
-	data->timer.function = io_timeout_fn;
 	hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
 	spin_unlock_irq(&ctx->timeout_lock);
 	return IOU_ISSUE_SKIP_COMPLETE;
@@ -626,7 +626,6 @@ void io_queue_linked_timeout(struct io_kiocb *req)
 	if (timeout->head) {
 		struct io_timeout_data *data = req->async_data;
 
-		data->timer.function = io_link_timeout_fn;
 		hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
 				data->mode);
 		list_add_tail(&timeout->list, &ctx->ltimeout_list);
-- 
2.39.5
Re: [PATCH 16/31] io_uring/timeout: Switch to use hrtimer_setup()
Posted by Pavel Begunkov 9 months, 1 week ago
On 10/28/24 07:31, Nam Cao wrote:
> There is a newly introduced hrtimer_setup() which will replace
> hrtimer_init(). This new function is similar to the old one, except that it
> also sanity-checks and initializes the timer's callback function.
> 
> Switch to use the new function.
> 
> This new function is also used to initialize the callback function in
> .prep() (the callback function depends on whether it is IORING_OP_TIMEOUT
> or IORING_OP_LINK_TIMEOUT). Thus, callback function setup in io_timeout()
> and io_queue_linked_timeout() are now redundant, therefore remove them.

Next time do the basic courtesy of CC'ing io_uring mailing list if
you're sending io_uring patches, so that people don't have to guess
months later why there is an unknown patch in the tree and where the
hell did it came from.

-- 
Pavel Begunkov