Skip to content

Commit 89850fc

Browse files
committed
io_uring: run timeouts from task_work
This is in preparation to making the completion lock work outside of hard/soft IRQ context. Add a timeout_lock to handle the ordering of timeout completions or cancelations with the timeouts actually triggering. Signed-off-by: Jens Axboe <[email protected]>
1 parent 62906e8 commit 89850fc

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

fs/io_uring.c

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ struct io_ring_ctx {
409409
struct {
410410
spinlock_t completion_lock;
411411

412+
spinlock_t timeout_lock;
413+
412414
/*
413415
* ->iopoll_list is protected by the ctx->uring_lock for
414416
* io_uring instances that don't use IORING_SETUP_SQPOLL.
@@ -1188,6 +1190,7 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
11881190
mutex_init(&ctx->uring_lock);
11891191
init_waitqueue_head(&ctx->cq_wait);
11901192
spin_lock_init(&ctx->completion_lock);
1193+
spin_lock_init(&ctx->timeout_lock);
11911194
INIT_LIST_HEAD(&ctx->iopoll_list);
11921195
INIT_LIST_HEAD(&ctx->defer_list);
11931196
INIT_LIST_HEAD(&ctx->timeout_list);
@@ -1328,6 +1331,7 @@ static void io_queue_async_work(struct io_kiocb *req)
13281331

13291332
static void io_kill_timeout(struct io_kiocb *req, int status)
13301333
__must_hold(&req->ctx->completion_lock)
1334+
__must_hold(&req->ctx->timeout_lock)
13311335
{
13321336
struct io_timeout_data *io = req->async_data;
13331337

@@ -1355,9 +1359,12 @@ static void io_queue_deferred(struct io_ring_ctx *ctx)
13551359
}
13561360

13571361
static void io_flush_timeouts(struct io_ring_ctx *ctx)
1362+
__must_hold(&ctx->completion_lock)
13581363
{
13591364
u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1365+
unsigned long flags;
13601366

1367+
spin_lock_irqsave(&ctx->timeout_lock, flags);
13611368
while (!list_empty(&ctx->timeout_list)) {
13621369
u32 events_needed, events_got;
13631370
struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
@@ -1382,6 +1389,7 @@ static void io_flush_timeouts(struct io_ring_ctx *ctx)
13821389
io_kill_timeout(req, 0);
13831390
}
13841391
ctx->cq_last_tm_flush = seq;
1392+
spin_unlock_irqrestore(&ctx->timeout_lock, flags);
13851393
}
13861394

13871395
static void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
@@ -5455,6 +5463,20 @@ static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
54555463
return 0;
54565464
}
54575465

5466+
static void io_req_task_timeout(struct io_kiocb *req)
5467+
{
5468+
struct io_ring_ctx *ctx = req->ctx;
5469+
5470+
spin_lock_irq(&ctx->completion_lock);
5471+
io_cqring_fill_event(ctx, req->user_data, -ETIME, 0);
5472+
io_commit_cqring(ctx);
5473+
spin_unlock_irq(&ctx->completion_lock);
5474+
5475+
io_cqring_ev_posted(ctx);
5476+
req_set_fail(req);
5477+
io_put_req(req);
5478+
}
5479+
54585480
static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
54595481
{
54605482
struct io_timeout_data *data = container_of(timer,
@@ -5463,24 +5485,20 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
54635485
struct io_ring_ctx *ctx = req->ctx;
54645486
unsigned long flags;
54655487

5466-
spin_lock_irqsave(&ctx->completion_lock, flags);
5488+
spin_lock_irqsave(&ctx->timeout_lock, flags);
54675489
list_del_init(&req->timeout.list);
54685490
atomic_set(&req->ctx->cq_timeouts,
54695491
atomic_read(&req->ctx->cq_timeouts) + 1);
5492+
spin_unlock_irqrestore(&ctx->timeout_lock, flags);
54705493

5471-
io_cqring_fill_event(ctx, req->user_data, -ETIME, 0);
5472-
io_commit_cqring(ctx);
5473-
spin_unlock_irqrestore(&ctx->completion_lock, flags);
5474-
5475-
io_cqring_ev_posted(ctx);
5476-
req_set_fail(req);
5477-
io_put_req(req);
5494+
req->io_task_work.func = io_req_task_timeout;
5495+
io_req_task_work_add(req);
54785496
return HRTIMER_NORESTART;
54795497
}
54805498

54815499
static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
54825500
__u64 user_data)
5483-
__must_hold(&ctx->completion_lock)
5501+
__must_hold(&ctx->timeout_lock)
54845502
{
54855503
struct io_timeout_data *io;
54865504
struct io_kiocb *req;
@@ -5502,7 +5520,7 @@ static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
55025520
}
55035521

55045522
static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5505-
__must_hold(&ctx->completion_lock)
5523+
__must_hold(&ctx->timeout_lock)
55065524
{
55075525
struct io_kiocb *req = io_timeout_extract(ctx, user_data);
55085526

@@ -5517,7 +5535,7 @@ static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
55175535

55185536
static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
55195537
struct timespec64 *ts, enum hrtimer_mode mode)
5520-
__must_hold(&ctx->completion_lock)
5538+
__must_hold(&ctx->timeout_lock)
55215539
{
55225540
struct io_kiocb *req = io_timeout_extract(ctx, user_data);
55235541
struct io_timeout_data *data;
@@ -5576,13 +5594,15 @@ static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
55765594
struct io_ring_ctx *ctx = req->ctx;
55775595
int ret;
55785596

5579-
spin_lock_irq(&ctx->completion_lock);
5597+
spin_lock_irq(&ctx->timeout_lock);
55805598
if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
55815599
ret = io_timeout_cancel(ctx, tr->addr);
55825600
else
55835601
ret = io_timeout_update(ctx, tr->addr, &tr->ts,
55845602
io_translate_timeout_mode(tr->flags));
5603+
spin_unlock_irq(&ctx->timeout_lock);
55855604

5605+
spin_lock_irq(&ctx->completion_lock);
55865606
io_cqring_fill_event(ctx, req->user_data, ret, 0);
55875607
io_commit_cqring(ctx);
55885608
spin_unlock_irq(&ctx->completion_lock);
@@ -5637,7 +5657,7 @@ static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
56375657
struct list_head *entry;
56385658
u32 tail, off = req->timeout.off;
56395659

5640-
spin_lock_irq(&ctx->completion_lock);
5660+
spin_lock_irq(&ctx->timeout_lock);
56415661

56425662
/*
56435663
* sqe->off holds how many events that need to occur for this
@@ -5676,7 +5696,7 @@ static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
56765696
list_add(&req->timeout.list, entry);
56775697
data->timer.function = io_timeout_fn;
56785698
hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
5679-
spin_unlock_irq(&ctx->completion_lock);
5699+
spin_unlock_irq(&ctx->timeout_lock);
56805700
return 0;
56815701
}
56825702

@@ -5730,7 +5750,9 @@ static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
57305750
spin_lock_irqsave(&ctx->completion_lock, flags);
57315751
if (ret != -ENOENT)
57325752
goto done;
5753+
spin_lock(&ctx->timeout_lock);
57335754
ret = io_timeout_cancel(ctx, sqe_addr);
5755+
spin_unlock(&ctx->timeout_lock);
57345756
if (ret != -ENOENT)
57355757
goto done;
57365758
ret = io_poll_cancel(ctx, sqe_addr, false);
@@ -5772,7 +5794,9 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
57725794
spin_lock_irq(&ctx->completion_lock);
57735795
if (ret != -ENOENT)
57745796
goto done;
5797+
spin_lock(&ctx->timeout_lock);
57755798
ret = io_timeout_cancel(ctx, sqe_addr);
5799+
spin_unlock(&ctx->timeout_lock);
57765800
if (ret != -ENOENT)
57775801
goto done;
57785802
ret = io_poll_cancel(ctx, sqe_addr, false);
@@ -8801,12 +8825,14 @@ static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
88018825
int canceled = 0;
88028826

88038827
spin_lock_irq(&ctx->completion_lock);
8828+
spin_lock(&ctx->timeout_lock);
88048829
list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
88058830
if (io_match_task(req, tsk, cancel_all)) {
88068831
io_kill_timeout(req, -ECANCELED);
88078832
canceled++;
88088833
}
88098834
}
8835+
spin_unlock(&ctx->timeout_lock);
88108836
if (canceled != 0)
88118837
io_commit_cqring(ctx);
88128838
spin_unlock_irq(&ctx->completion_lock);

0 commit comments

Comments
 (0)