io-uring.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pavel Begunkov <asml.silence@gmail.com>
To: Jens Axboe <axboe@kernel.dk>, io-uring <io-uring@vger.kernel.org>
Subject: Re: [PATCH] io_uring: trigger worker exit when ring is exiting
Date: Thu, 2 Sep 2021 19:04:03 +0100	[thread overview]
Message-ID: <0a7dbae3-e48e-bf8c-1959-59195cad4bcf@gmail.com> (raw)
In-Reply-To: <045c35f3-fb30-8016-5a7e-fc0c26f2c400@kernel.dk>

On 9/2/21 6:56 PM, Jens Axboe wrote:
> 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.

> Signed-off-by: Jens Axboe <axboe@kernel.dk>
> 
> ---
> 
> diff --git a/fs/io-wq.c b/fs/io-wq.c
> index d80e4a735677..60cd841c6c57 100644
> --- a/fs/io-wq.c
> +++ b/fs/io-wq.c
> @@ -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)
> diff --git a/fs/io_uring.c b/fs/io_uring.c
> index 2bde732a1183..9936ebaa8180 100644
> --- a/fs/io_uring.c
> +++ b/fs/io_uring.c
> @@ -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
> 

-- 
Pavel Begunkov

  reply	other threads:[~2021-09-02 18:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-02 17:56 [PATCH] io_uring: trigger worker exit when ring is exiting Jens Axboe
2021-09-02 18:04 ` Pavel Begunkov [this message]
2021-09-02 18:08   ` Jens Axboe
2021-09-02 18:19     ` 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=0a7dbae3-e48e-bf8c-1959-59195cad4bcf@gmail.com \
    --to=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=io-uring@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;
as well as URLs for NNTP newsgroup(s).