Skip to content

Commit 9d3489b

Browse files
axboegregkh
authored andcommitted
io_uring: backport io_should_terminate_tw()
Parts of commit b6f58a3 upstream. Backport io_should_terminate_tw() helper to judge whether task_work should be run or terminated. Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ab064e0 commit 9d3489b

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

io_uring/io_uring.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,8 +1249,7 @@ static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
12491249
void io_req_task_submit(struct io_kiocb *req, bool *locked)
12501250
{
12511251
io_tw_lock(req->ctx, locked);
1252-
/* req->task == current here, checking PF_EXITING is safe */
1253-
if (likely(!(req->task->flags & PF_EXITING)))
1252+
if (likely(!io_should_terminate_tw()))
12541253
io_queue_sqe(req);
12551254
else
12561255
io_req_complete_failed(req, -EFAULT);
@@ -1773,7 +1772,7 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
17731772
int io_poll_issue(struct io_kiocb *req, bool *locked)
17741773
{
17751774
io_tw_lock(req->ctx, locked);
1776-
if (unlikely(req->task->flags & PF_EXITING))
1775+
if (unlikely(io_should_terminate_tw()))
17771776
return -EFAULT;
17781777
return io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_MULTISHOT);
17791778
}

io_uring/io_uring.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@ static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx)
395395
ctx->submitter_task == current);
396396
}
397397

398+
/*
399+
* Terminate the request if either of these conditions are true:
400+
*
401+
* 1) It's being executed by the original task, but that task is marked
402+
* with PF_EXITING as it's exiting.
403+
* 2) PF_KTHREAD is set, in which case the invoker of the task_work is
404+
* our fallback task_work.
405+
*/
406+
static inline bool io_should_terminate_tw(void)
407+
{
408+
return current->flags & (PF_KTHREAD | PF_EXITING);
409+
}
410+
398411
static inline void io_req_queue_tw_complete(struct io_kiocb *req, s32 res)
399412
{
400413
io_req_set_res(req, res, 0);

io_uring/poll.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,7 @@ static int io_poll_check_events(struct io_kiocb *req, bool *locked)
241241
struct io_ring_ctx *ctx = req->ctx;
242242
int v;
243243

244-
/* req->task == current here, checking PF_EXITING is safe */
245-
if (unlikely(req->task->flags & PF_EXITING))
244+
if (unlikely(io_should_terminate_tw()))
246245
return -ECANCELED;
247246

248247
do {

io_uring/timeout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
275275
int ret = -ENOENT;
276276

277277
if (prev) {
278-
if (!(req->task->flags & PF_EXITING)) {
278+
if (!io_should_terminate_tw()) {
279279
struct io_cancel_data cd = {
280280
.ctx = req->ctx,
281281
.data = prev->cqe.user_data,

0 commit comments

Comments
 (0)