Skip to content

Commit 9ffa13f

Browse files
isilenceaxboe
authored andcommitted
io_uring: pin context while queueing deferred tw
Unlike normal tw, nothing prevents deferred tw to be executed right after an tw item added to ->work_llist in io_req_local_work_add(). For instance, the waiting task may get waken up by CQ posting or a normal tw. Thus we need to pin the ring for the rest of io_req_local_work_add() Cc: [email protected] Fixes: c0e0d6b ("io_uring: add IORING_SETUP_DEFER_TASKRUN") Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/1a79362b9c10b8523ef70b061d96523650a23344.1672795998.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent af82425 commit 9ffa13f

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

io_uring/io_uring.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,13 +1236,18 @@ static void io_req_local_work_add(struct io_kiocb *req)
12361236
{
12371237
struct io_ring_ctx *ctx = req->ctx;
12381238

1239-
if (!llist_add(&req->io_task_work.node, &ctx->work_llist))
1239+
percpu_ref_get(&ctx->refs);
1240+
1241+
if (!llist_add(&req->io_task_work.node, &ctx->work_llist)) {
1242+
percpu_ref_put(&ctx->refs);
12401243
return;
1244+
}
12411245
/* need it for the following io_cqring_wake() */
12421246
smp_mb__after_atomic();
12431247

12441248
if (unlikely(atomic_read(&req->task->io_uring->in_idle))) {
12451249
io_move_task_work_from_local(ctx);
1250+
percpu_ref_put(&ctx->refs);
12461251
return;
12471252
}
12481253

@@ -1252,6 +1257,7 @@ static void io_req_local_work_add(struct io_kiocb *req)
12521257
if (ctx->has_evfd)
12531258
io_eventfd_signal(ctx);
12541259
__io_cqring_wake(ctx);
1260+
percpu_ref_put(&ctx->refs);
12551261
}
12561262

12571263
void __io_req_task_work_add(struct io_kiocb *req, bool allow_local)

0 commit comments

Comments
 (0)