Linux io-uring development
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@kernel.dk>
To: io-uring@vger.kernel.org
Cc: Chris Mason <mason@kernel.org>, Jens Axboe <axboe@kernel.dk>
Subject: [PATCH 09/10] io_uring: drop cached task refs on ring release
Date: Wed, 25 Nov 2026 11:43:18 -0700	[thread overview]
Message-ID: <20261125184439.58077-10-axboe@kernel.dk> (raw)
In-Reply-To: <20261125184439.58077-1-axboe@kernel.dk>

From: Chris Mason <mason@kernel.org>

An exiting task can leak its own task_struct, if its exit releases a
ring that still has a request of that task in flight.

If a task frees its own request, io_put_task() doesn't put the task
reference but keeps it in tctx->cached_refs. An exiting task empties
that cache for the last time in io_uring_cancel_generic(). So whatever
frees requests in that task later on has to call
io_uring_drop_tctx_refs() afterwards, as tctx_task_work_run() does. The
cancel pass in io_ring_ctx_wait_and_kill() doesn't. With a timeout armed
on a DEFER_TASKRUN ring:

    do_exit()
      io_uring_files_cancel()
        io_uring_cancel_generic()
          io_uring_drop_tctx_refs()
          io_tctx_cancel_done() is true, the timeout stays armed
      exit_files()
        io_uring_release()
          io_ring_ctx_wait_and_kill()
            io_ring_ctx_cancel()
              io_kill_timeouts()
              io_cancel_local_task_work()
                io_put_task()
                  tctx->cached_refs++

Commit e775f93f2ab9 ("io_uring: ensure that cached task references are
always put on exit") added that call to io_ring_ctx_wait_and_kill().
Commit 02bac94bd8ef ("io_uring: don't iopoll from
io_ring_ctx_wait_and_kill()") removed it together with the completions
at release, and commit 89baa3154a72 ("io_uring: run cancelations
synchronously on ring release") brought only the completions back.

The fix used here is to restore that call, after the task that releases
the ring has run its cancelations.

Fixes: 89baa3154a72 ("io_uring: run cancelations synchronously on ring release")
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
---
 io_uring/io_uring.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 6808229edf07..d8491f86e480 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -2702,6 +2702,9 @@ static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
 	if (!(current->flags & PF_IO_WORKER)) {
 		io_ring_ctx_cancel(ctx);
 		io_ring_ctx_wait_inflight(ctx);
+		/* exit is past the drop in io_uring_cancel_generic() */
+		if (current->io_uring)
+			io_uring_drop_tctx_refs(current);
 	}
 
 	INIT_WORK(&ctx->exit_work, io_ring_exit_work);
-- 
2.55.0


  parent reply	other threads:[~2026-11-25 18:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 18:43 [PATCHSET 0/10] Various fixes Jens Axboe
2026-11-25 18:43 ` [PATCH 01/10] io_uring/net: don't copy msg_name on a recvmsg retry Jens Axboe
2026-11-25 18:43 ` [PATCH 02/10] io_uring/kbuf: clear REQ_F_BL_NO_RECYCLE on buffer put Jens Axboe
2026-11-25 18:43 ` [PATCH 03/10] io_uring: free the ring after a grace period Jens Axboe
2026-11-25 18:43 ` [PATCH 04/10] io_uring: free the ring from an RCU work Jens Axboe
2026-11-25 18:43 ` [PATCH 05/10] io_uring/filetable: don't install O_PATH files Jens Axboe
2026-11-25 18:43 ` [PATCH 06/10] io_uring/timeout: hold timeout_lock for the link splice Jens Axboe
2026-11-25 18:43 ` [PATCH 07/10] io_uring/cancel: always walk links under timeout_lock Jens Axboe
2026-11-25 18:43 ` [PATCH 08/10] io_uring/net: don't count the buffers of a send bundle Jens Axboe
2026-11-25 18:43 ` Jens Axboe [this message]
2026-11-25 18:43 ` [PATCH 10/10] io_uring/net: restore the cmsg buffer on recvmsg retry Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125184439.58077-10-axboe@kernel.dk \
    --to=axboe@kernel.dk \
    --cc=io-uring@vger.kernel.org \
    --cc=mason@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox