public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH] io_uring: unlock sqd->lock before sq thread release CPU
       [not found] <CGME20230524052154epcas5p313d92a9cbf0fa7e9555f8dd00125539e@epcas5p3.samsung.com>
@ 2023-05-24  5:28 ` Wenwen Chen
  2023-05-24 14:08   ` Jens Axboe
  0 siblings, 1 reply; 2+ messages in thread
From: Wenwen Chen @ 2023-05-24  5:28 UTC (permalink / raw)
  To: axboe, asml.silence; +Cc: io-uring, Wenwen Chen, Kanchan Joshi

The sq thread actively releases CPU resources by calling the 
cond_resched() and schedule() interfaces when it is idle. Therefore,
more resources are available for other threads to run.

There exists a problem in sq thread: it does not unlock sqd->lock before
releasing CPU resources every time. This makes other threads pending on
sqd->lock for a long time. For example, the following interfaces all 
require sqd->lock: io_sq_offload_create(), io_register_iowq_max_workers()
and io_ring_exit_work().
       
Before the sq thread releases CPU resources, unlocking sqd->lock will 
provide the user a better experience because it can respond quickly to
user requests.

Signed-off-by: Kanchan Joshi<[email protected]>
Signed-off-by: Wenwen Chen<[email protected]>
---
 io_uring/sqpoll.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/io_uring/sqpoll.c b/io_uring/sqpoll.c
index 9db4bc1f521a..759c80fb4afa 100644
--- a/io_uring/sqpoll.c
+++ b/io_uring/sqpoll.c
@@ -255,7 +255,9 @@ static int io_sq_thread(void *data)
 			sqt_spin = true;
 
 		if (sqt_spin || !time_after(jiffies, timeout)) {
+			mutex_unlock(&sqd->lock);
 			cond_resched();
+			mutex_lock(&sqd->lock);
 			if (sqt_spin)
 				timeout = jiffies + sqd->sq_thread_idle;
 			continue;

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] io_uring: unlock sqd->lock before sq thread release CPU
  2023-05-24  5:28 ` [PATCH] io_uring: unlock sqd->lock before sq thread release CPU Wenwen Chen
@ 2023-05-24 14:08   ` Jens Axboe
  0 siblings, 0 replies; 2+ messages in thread
From: Jens Axboe @ 2023-05-24 14:08 UTC (permalink / raw)
  To: Wenwen Chen, asml.silence; +Cc: io-uring, Kanchan Joshi

On 5/23/23 11:28?PM, Wenwen Chen wrote:
> The sq thread actively releases CPU resources by calling the 
> cond_resched() and schedule() interfaces when it is idle. Therefore,
> more resources are available for other threads to run.
> 
> There exists a problem in sq thread: it does not unlock sqd->lock before
> releasing CPU resources every time. This makes other threads pending on
> sqd->lock for a long time. For example, the following interfaces all 
> require sqd->lock: io_sq_offload_create(), io_register_iowq_max_workers()
> and io_ring_exit_work().
>        
> Before the sq thread releases CPU resources, unlocking sqd->lock will 
> provide the user a better experience because it can respond quickly to
> user requests.
> 
> Signed-off-by: Kanchan Joshi<[email protected]>
> Signed-off-by: Wenwen Chen<[email protected]>
> ---
>  io_uring/sqpoll.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/io_uring/sqpoll.c b/io_uring/sqpoll.c
> index 9db4bc1f521a..759c80fb4afa 100644
> --- a/io_uring/sqpoll.c
> +++ b/io_uring/sqpoll.c
> @@ -255,7 +255,9 @@ static int io_sq_thread(void *data)
>  			sqt_spin = true;
>  
>  		if (sqt_spin || !time_after(jiffies, timeout)) {
> +			mutex_unlock(&sqd->lock);
>  			cond_resched();
> +			mutex_lock(&sqd->lock);
>  			if (sqt_spin)
>  				timeout = jiffies + sqd->sq_thread_idle;
>  			continue;

Since this is the spin case, and we expect (by far) most of these
to NOT need a reschedule, I think we should do:

	if (need_resched()) {
		mutex_unlock(&sqd->lock);
		cond_resched();
		mutex_lock(&sqd->lock);
	}

to make that lock shuffle dependent on the need to reschedule. And
since we're marking the timeout at that point, timeout should be
assigned first as far as I can tell. So in total:

	if (sqt_spin || !time_after(jiffies, timeout)) {
		if (sqt_spin)
 			timeout = jiffies + sqd->sq_thread_idle;
 		if (unlikely(need_resched())) {
			mutex_unlock(&sqd->lock);
			cond_resched();
			mutex_lock(&sqd->lock);
		}
		continue;
	}

would probably be the better fix.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-05-24 14:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20230524052154epcas5p313d92a9cbf0fa7e9555f8dd00125539e@epcas5p3.samsung.com>
2023-05-24  5:28 ` [PATCH] io_uring: unlock sqd->lock before sq thread release CPU Wenwen Chen
2023-05-24 14:08   ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox