Skip to content

Commit e09ee51

Browse files
isilenceaxboe
authored andcommitted
io_uring: fix exiting io_req_task_work_add leaks
If one entered io_req_task_work_add() not seeing PF_EXITING, it will set a ->task_state bit and try task_work_add(), which may fail by that moment. If that happens the function would try to cancel the request. However, in a meanwhile there might come other io_req_task_work_add() callers, which will see the bit set and leave their requests in the list, which will never be executed. Don't propagate an error, but clear the bit first and then fallback all requests that we can splice from the list. The callback functions have to be able to deal with PF_EXITING, so poll and apoll was modified via changing io_poll_rewait(). Fixes: 7cbf172 ("io_uring: provide FIFO ordering for task_work") Reported-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Link: https://lore.kernel.org/r/060002f19f1fdbd130ba24aef818ea4d3080819b.1625142209.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 5b0a6ac commit e09ee51

1 file changed

Lines changed: 24 additions & 46 deletions

File tree

fs/io_uring.c

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,17 +1952,13 @@ static void tctx_task_work(struct callback_head *cb)
19521952
ctx_flush_and_put(ctx);
19531953
}
19541954

1955-
static int io_req_task_work_add(struct io_kiocb *req)
1955+
static void io_req_task_work_add(struct io_kiocb *req)
19561956
{
19571957
struct task_struct *tsk = req->task;
19581958
struct io_uring_task *tctx = tsk->io_uring;
19591959
enum task_work_notify_mode notify;
1960-
struct io_wq_work_node *node, *prev;
1960+
struct io_wq_work_node *node;
19611961
unsigned long flags;
1962-
int ret = 0;
1963-
1964-
if (unlikely(tsk->flags & PF_EXITING))
1965-
return -ESRCH;
19661962

19671963
WARN_ON_ONCE(!tctx);
19681964

@@ -1973,7 +1969,9 @@ static int io_req_task_work_add(struct io_kiocb *req)
19731969
/* task_work already pending, we're done */
19741970
if (test_bit(0, &tctx->task_state) ||
19751971
test_and_set_bit(0, &tctx->task_state))
1976-
return 0;
1972+
return;
1973+
if (unlikely(tsk->flags & PF_EXITING))
1974+
goto fail;
19771975

19781976
/*
19791977
* SQPOLL kernel thread doesn't need notification, just a wakeup. For
@@ -1982,36 +1980,24 @@ static int io_req_task_work_add(struct io_kiocb *req)
19821980
* will do the job.
19831981
*/
19841982
notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
1985-
19861983
if (!task_work_add(tsk, &tctx->task_work, notify)) {
19871984
wake_up_process(tsk);
1988-
return 0;
1985+
return;
19891986
}
1990-
1991-
/*
1992-
* Slow path - we failed, find and delete work. if the work is not
1993-
* in the list, it got run and we're fine.
1994-
*/
1987+
fail:
1988+
clear_bit(0, &tctx->task_state);
19951989
spin_lock_irqsave(&tctx->task_lock, flags);
1996-
wq_list_for_each(node, prev, &tctx->task_list) {
1997-
if (&req->io_task_work.node == node) {
1998-
wq_list_del(&tctx->task_list, node, prev);
1999-
ret = 1;
2000-
break;
2001-
}
2002-
}
1990+
node = tctx->task_list.first;
1991+
INIT_WQ_LIST(&tctx->task_list);
20031992
spin_unlock_irqrestore(&tctx->task_lock, flags);
2004-
clear_bit(0, &tctx->task_state);
2005-
return ret;
2006-
}
20071993

2008-
static void io_req_task_work_add_fallback(struct io_kiocb *req,
2009-
io_req_tw_func_t cb)
2010-
{
2011-
req->io_task_work.func = cb;
2012-
if (llist_add(&req->io_task_work.fallback_node,
2013-
&req->ctx->fallback_llist))
2014-
schedule_delayed_work(&req->ctx->fallback_work, 1);
1994+
while (node) {
1995+
req = container_of(node, struct io_kiocb, io_task_work.node);
1996+
node = node->next;
1997+
if (llist_add(&req->io_task_work.fallback_node,
1998+
&req->ctx->fallback_llist))
1999+
schedule_delayed_work(&req->ctx->fallback_work, 1);
2000+
}
20152001
}
20162002

20172003
static void io_req_task_cancel(struct io_kiocb *req)
@@ -2041,17 +2027,13 @@ static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
20412027
{
20422028
req->result = ret;
20432029
req->io_task_work.func = io_req_task_cancel;
2044-
2045-
if (unlikely(io_req_task_work_add(req)))
2046-
io_req_task_work_add_fallback(req, io_req_task_cancel);
2030+
io_req_task_work_add(req);
20472031
}
20482032

20492033
static void io_req_task_queue(struct io_kiocb *req)
20502034
{
20512035
req->io_task_work.func = io_req_task_submit;
2052-
2053-
if (unlikely(io_req_task_work_add(req)))
2054-
io_req_task_queue_fail(req, -ECANCELED);
2036+
io_req_task_work_add(req);
20552037
}
20562038

20572039
static inline void io_queue_next(struct io_kiocb *req)
@@ -2165,8 +2147,7 @@ static inline void io_put_req(struct io_kiocb *req)
21652147
static void io_free_req_deferred(struct io_kiocb *req)
21662148
{
21672149
req->io_task_work.func = io_free_req;
2168-
if (unlikely(io_req_task_work_add(req)))
2169-
io_req_task_work_add_fallback(req, io_free_req);
2150+
io_req_task_work_add(req);
21702151
}
21712152

21722153
static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
@@ -4823,8 +4804,6 @@ struct io_poll_table {
48234804
static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
48244805
__poll_t mask, io_req_tw_func_t func)
48254806
{
4826-
int ret;
4827-
48284807
/* for instances that support it check for an event match first: */
48294808
if (mask && !(mask & poll->events))
48304809
return 0;
@@ -4842,11 +4821,7 @@ static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
48424821
* of executing it. We can't safely execute it anyway, as we may not
48434822
* have the needed state needed for it anyway.
48444823
*/
4845-
ret = io_req_task_work_add(req);
4846-
if (unlikely(ret)) {
4847-
WRITE_ONCE(poll->canceled, true);
4848-
io_req_task_work_add_fallback(req, func);
4849-
}
4824+
io_req_task_work_add(req);
48504825
return 1;
48514826
}
48524827

@@ -4855,6 +4830,9 @@ static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
48554830
{
48564831
struct io_ring_ctx *ctx = req->ctx;
48574832

4833+
if (unlikely(req->task->flags & PF_EXITING))
4834+
WRITE_ONCE(poll->canceled, true);
4835+
48584836
if (!req->result && !READ_ONCE(poll->canceled)) {
48594837
struct poll_table_struct pt = { ._key = poll->events };
48604838

0 commit comments

Comments
 (0)