From: Jens Axboe <axboe@kernel.dk>
To: io-uring@vger.kernel.org
Cc: asml.silence@gmail.com, brauner@kernel.org,
linux-fsdevel@vger.kernel.org, Jens Axboe <axboe@kernel.dk>
Subject: [PATCH 4/5] io_uring: wait for cancelations on final ring put
Date: Wed, 9 Apr 2025 07:35:22 -0600 [thread overview]
Message-ID: <20250409134057.198671-5-axboe@kernel.dk> (raw)
In-Reply-To: <20250409134057.198671-1-axboe@kernel.dk>
We still offload the cancelation to a workqueue, as not to introduce
dependencies between the exiting task waiting on cleanup, and that
task needing to run task_work to complete the process.
This means that once the final ring put is done, any request that was
inflight and needed cancelation will be done as well. Notably requests
that hold references to files - once the ring fd close is done, we will
have dropped any of those references too.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
---
include/linux/io_uring_types.h | 2 ++
io_uring/io_uring.c | 17 +++++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h
index b44d201520d8..4d26aef281fb 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -450,6 +450,8 @@ struct io_ring_ctx {
struct io_mapped_region param_region;
/* just one zcrx per ring for now, will move to io_zcrx_ifq eventually */
struct io_mapped_region zcrx_region;
+
+ struct completion *exit_comp;
};
/*
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index ce00b616e138..4b3e3ff774d6 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -2891,6 +2891,7 @@ static __cold void io_ring_exit_work(struct work_struct *work)
struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
unsigned long timeout = jiffies + HZ * 60 * 5;
unsigned long interval = HZ / 20;
+ struct completion *exit_comp;
struct io_tctx_exit exit;
struct io_tctx_node *node;
int ret;
@@ -2955,6 +2956,10 @@ static __cold void io_ring_exit_work(struct work_struct *work)
io_kworker_tw_end();
+ exit_comp = READ_ONCE(ctx->exit_comp);
+ if (exit_comp)
+ complete(exit_comp);
+
init_completion(&exit.completion);
init_task_work(&exit.task_work, io_tctx_exit_cb);
exit.ctx = ctx;
@@ -3017,9 +3022,21 @@ static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
static int io_uring_release(struct inode *inode, struct file *file)
{
struct io_ring_ctx *ctx = file->private_data;
+ DECLARE_COMPLETION_ONSTACK(exit_comp);
file->private_data = NULL;
+ WRITE_ONCE(ctx->exit_comp, &exit_comp);
io_ring_ctx_wait_and_kill(ctx);
+
+ /*
+ * Wait for cancel to run before exiting task
+ */
+ do {
+ if (current->io_uring)
+ io_fallback_tw(current->io_uring, false);
+ cond_resched();
+ } while (wait_for_completion_interruptible(&exit_comp));
+
return 0;
}
--
2.49.0
next prev parent reply other threads:[~2025-04-09 13:41 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-09 13:35 [PATCHSET v3 0/5] Cancel and wait for all requests on exit Jens Axboe
2025-04-09 13:35 ` [PATCH 1/5] fs: gate final fput task_work on PF_NO_TASKWORK Jens Axboe
2025-04-11 13:48 ` Christian Brauner
2025-04-11 14:37 ` Jens Axboe
2025-04-14 10:10 ` Christian Brauner
2025-04-14 14:29 ` Jens Axboe
2025-04-14 17:11 ` Mateusz Guzik
2025-04-14 19:35 ` Jens Axboe
2025-04-09 13:35 ` [PATCH 2/5] io_uring: mark exit side kworkers as task_work capable Jens Axboe
2025-04-11 13:55 ` Christian Brauner
2025-04-11 14:38 ` Jens Axboe
2025-04-09 13:35 ` [PATCH 3/5] io_uring: consider ring dead once the ref is marked dying Jens Axboe
2025-04-09 13:35 ` Jens Axboe [this message]
2025-04-09 13:35 ` [PATCH 5/5] io_uring: switch away from percpu refcounts Jens Axboe
-- strict thread matches above, loose matches on Subject: below --
2025-03-21 19:24 [PATCHSET RFC v2 0/5] Cancel and wait for all requests on exit Jens Axboe
2025-03-21 19:24 ` [PATCH 4/5] io_uring: wait for cancelations on final ring put 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=20250409134057.198671-5-axboe@kernel.dk \
--to=axboe@kernel.dk \
--cc=asml.silence@gmail.com \
--cc=brauner@kernel.org \
--cc=io-uring@vger.kernel.org \
--cc=linux-fsdevel@vger.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