Skip to contentSkip to conversation

io_uring: trigger worker exit when ring is exiting

PATCH In review Jens Axboe 2 participants 4 messages Sep 2, 2021

Subsystems (2)
lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

PATCH 1/1 io_uring: trigger worker exit when ring is exiting

If a task exits normally, then the teardown of async workers happens quickly. But if it simply closes the ring fd, then the async teardown ends up waiting for workers to timeout if they are sleeping, which can then take up to 5 seconds (by default). This isn't a big issue as this happens off the workqueue path, but let's be nicer and ensure that we exit as quick as possible.

---

fs/io-wq.c +10 -0
index d80e4a735677..60cd841c6c57 100644
@@ -1130,7 +1130,17 @@ static bool io_task_work_match(struct callback_head *cb, void *data)
 
 void io_wq_exit_start(struct io_wq *wq)
 {
+	int node;
+
 	set_bit(IO_WQ_BIT_EXIT, &wq->state);
+
+	rcu_read_lock();
+	for_each_node(node) {
+		struct io_wqe *wqe = wq->wqes[node];
+
+		io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
+	}
+	rcu_read_unlock();
 }
 
 static void io_wq_exit_workers(struct io_wq *wq)
fs/io_uring.c +5 -0
index 2bde732a1183..9936ebaa8180 100644
@@ -9158,6 +9158,7 @@ static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
 
 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
 {
+	struct io_uring_task *tctx = current->io_uring;
 	unsigned long index;
 	struct creds *creds;
 
@@ -9175,6 +9176,10 @@ static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
 	/* if we failed setting up the ctx, we might not have any rings */
 	io_iopoll_try_reap_events(ctx);
 
+	/* trigger exit, if it hasn't been done already */
+	if (tctx->io_wq)
+		io_wq_exit_start(tctx->io_wq);
+
 	INIT_WORK(&ctx->exit_work, io_ring_exit_work);
 	/*
 	 * Use system_unbound_wq to avoid spawning tons of event kworkers
Signature · 1 line
Jens Axboe
#1 lore raw
Quoted 6 lines · Jens Axboe

If a task exits normally, then the teardown of async workers happens quickly. But if it simply closes the ring fd, then the async teardown ends up waiting for workers to timeout if they are sleeping, which can then take up to 5 seconds (by default). This isn't a big issue as this happens off the workqueue path, but let's be nicer and ensure that we exit as quick as possible.

ring = io_uring_init();
...
io_uring_close(&ring); // triggers io_ring_ctx_wait_and_kill() rint2 = io_uring_init();
...

It looks IO_WQ_BIT_EXIT there will be troublesome.

Quoted 50 lines

---

fs/io-wq.c +10 -0
index d80e4a735677..60cd841c6c57 100644
@@ -1130,7 +1130,17 @@ static bool io_task_work_match(struct callback_head *cb, void *data)
 
 void io_wq_exit_start(struct io_wq *wq)
 {
+	int node;
+
 	set_bit(IO_WQ_BIT_EXIT, &wq->state);
+
+	rcu_read_lock();
+	for_each_node(node) {
+		struct io_wqe *wqe = wq->wqes[node];
+
+		io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
+	}
+	rcu_read_unlock();
 }
 
 static void io_wq_exit_workers(struct io_wq *wq)
fs/io_uring.c +5 -0
index 2bde732a1183..9936ebaa8180 100644
@@ -9158,6 +9158,7 @@ static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
 
 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
 {
+	struct io_uring_task *tctx = current->io_uring;
 	unsigned long index;
 	struct creds *creds;
 
@@ -9175,6 +9176,10 @@ static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
 	/* if we failed setting up the ctx, we might not have any rings */
 	io_iopoll_try_reap_events(ctx);
 
+	/* trigger exit, if it hasn't been done already */
+	if (tctx->io_wq)
+		io_wq_exit_start(tctx->io_wq);
+
 	INIT_WORK(&ctx->exit_work, io_ring_exit_work);
 	/*
 	 * Use system_unbound_wq to avoid spawning tons of event kworkers
Signature · 1 line
Pavel Begunkov
author maintainer
#2 lore raw
Quoted 8 lines · Pavel Begunkov
Quoted 6 lines · Jens Axboe

If a task exits normally, then the teardown of async workers happens quickly. But if it simply closes the ring fd, then the async teardown ends up waiting for workers to timeout if they are sleeping, which can then take up to 5 seconds (by default). This isn't a big issue as this happens off the workqueue path, but let's be nicer and ensure that we exit as quick as possible.

Quoted 7 lines

ring = io_uring_init();
...
io_uring_close(&ring); // triggers io_ring_ctx_wait_and_kill() rint2 = io_uring_init();
...

It looks IO_WQ_BIT_EXIT there will be troublesome.

I wonder if we can get by with just a wakeup. Let me test...

Signature · 1 line
Jens Axboe
author maintainer
#3 lore raw
Quoted 15 lines · Jens Axboe
Quoted 14 lines · Pavel Begunkov
Quoted 6 lines · Jens Axboe

If a task exits normally, then the teardown of async workers happens quickly. But if it simply closes the ring fd, then the async teardown ends up waiting for workers to timeout if they are sleeping, which can then take up to 5 seconds (by default). This isn't a big issue as this happens off the workqueue path, but let's be nicer and ensure that we exit as quick as possible.

ring = io_uring_init();
...
io_uring_close(&ring); // triggers io_ring_ctx_wait_and_kill() rint2 = io_uring_init();
...

Quoted 3 lines
Quoted 1 line

It looks IO_WQ_BIT_EXIT there will be troublesome.

I wonder if we can get by with just a wakeup. Let me test...

Nah not enough. Would be nice to fix though, pretty annoying that the threads sit there for 5 seconds doing nothing.

Signature · 1 line
Jens Axboe