public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH] io-wq: simplify code in __io_worker_busy
@ 2021-04-02 10:18 Hao Xu
  2021-04-02 10:38 ` Pavel Begunkov
  0 siblings, 1 reply; 10+ messages in thread
From: Hao Xu @ 2021-04-02 10:18 UTC (permalink / raw)
  To: Jens Axboe; +Cc: io-uring, Pavel Begunkov, Joseph Qi

leverage xor to simplify code in __io_worker_busy

Signed-off-by: Hao Xu <[email protected]>
---
 fs/io-wq.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/fs/io-wq.c b/fs/io-wq.c
index 7434eb40ca8c..f77e4704d7c7 100644
--- a/fs/io-wq.c
+++ b/fs/io-wq.c
@@ -292,16 +292,11 @@ static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
 	worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
 	work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
 	if (worker_bound != work_bound) {
+		int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;
 		io_wqe_dec_running(worker);
-		if (work_bound) {
-			worker->flags |= IO_WORKER_F_BOUND;
-			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
-			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
-		} else {
-			worker->flags &= ~IO_WORKER_F_BOUND;
-			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
-			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
-		}
+		worker->flags ^= IO_WORKER_F_BOUND;
+		wqe->acct[index].nr_workers--;
+		wqe->acct[index^1].nr_workers++;
 		io_wqe_inc_running(worker);
 	 }
 }
-- 
1.8.3.1


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

* Re: [PATCH] io-wq: simplify code in __io_worker_busy
  2021-04-02 10:18 [PATCH] io-wq: simplify code in __io_worker_busy Hao Xu
@ 2021-04-02 10:38 ` Pavel Begunkov
  2021-04-04 19:17   ` Jens Axboe
  2021-04-05  7:36   ` [PATCH v2] " Hao Xu
  0 siblings, 2 replies; 10+ messages in thread
From: Pavel Begunkov @ 2021-04-02 10:38 UTC (permalink / raw)
  To: Hao Xu, Jens Axboe; +Cc: io-uring, Joseph Qi

On 02/04/2021 11:18, Hao Xu wrote:
> leverage xor to simplify code in __io_worker_busy

I don't like hard-coded ^1 because if indexes change it may break.
One option is to leave it to the compiler:

idx = bound : WQ_BOUND ? WQ_UNBOUND;
compl_idx = bound : WQ_UNBOUND ? WQ_BOUND;

Or add a BUILD_BUG_ON() checking that WQ_BOUND and WQ_UNBOUND
are mod 2 complementary.


> 
> Signed-off-by: Hao Xu <[email protected]>
> ---
>  fs/io-wq.c | 13 ++++---------
>  1 file changed, 4 insertions(+), 9 deletions(-)
> 
> diff --git a/fs/io-wq.c b/fs/io-wq.c
> index 7434eb40ca8c..f77e4704d7c7 100644
> --- a/fs/io-wq.c
> +++ b/fs/io-wq.c
> @@ -292,16 +292,11 @@ static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
>  	worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
>  	work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
>  	if (worker_bound != work_bound) {
> +		int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;
>  		io_wqe_dec_running(worker);
> -		if (work_bound) {
> -			worker->flags |= IO_WORKER_F_BOUND;
> -			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
> -			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
> -		} else {
> -			worker->flags &= ~IO_WORKER_F_BOUND;
> -			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
> -			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
> -		}
> +		worker->flags ^= IO_WORKER_F_BOUND;
> +		wqe->acct[index].nr_workers--;
> +		wqe->acct[index^1].nr_workers++;
>  		io_wqe_inc_running(worker);
>  	 }
>  }
> 

-- 
Pavel Begunkov

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

* Re: [PATCH] io-wq: simplify code in __io_worker_busy
  2021-04-02 10:38 ` Pavel Begunkov
@ 2021-04-04 19:17   ` Jens Axboe
  2021-04-05  7:53     ` [PATCH v3] " Hao Xu
  2021-04-05  7:36   ` [PATCH v2] " Hao Xu
  1 sibling, 1 reply; 10+ messages in thread
From: Jens Axboe @ 2021-04-04 19:17 UTC (permalink / raw)
  To: Pavel Begunkov, Hao Xu; +Cc: io-uring, Joseph Qi

On 4/2/21 4:38 AM, Pavel Begunkov wrote:
> On 02/04/2021 11:18, Hao Xu wrote:
>> leverage xor to simplify code in __io_worker_busy
> 
> I don't like hard-coded ^1 because if indexes change it may break.
> One option is to leave it to the compiler:
> 
> idx = bound : WQ_BOUND ? WQ_UNBOUND;
> compl_idx = bound : WQ_UNBOUND ? WQ_BOUND;
> 
> Or add a BUILD_BUG_ON() checking that WQ_BOUND and WQ_UNBOUND
> are mod 2 complementary.

Was going to suggest just that, just add a BUILD_BUG_ON() to catch
any changes there. The code is way cleaner with the XOR trick.

Hao, can you resend with that?

-- 
Jens Axboe


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

* [PATCH v2] io-wq: simplify code in __io_worker_busy
  2021-04-02 10:38 ` Pavel Begunkov
  2021-04-04 19:17   ` Jens Axboe
@ 2021-04-05  7:36   ` Hao Xu
  1 sibling, 0 replies; 10+ messages in thread
From: Hao Xu @ 2021-04-05  7:36 UTC (permalink / raw)
  To: Jens Axboe; +Cc: io-uring, Pavel Begunkov, Joseph Qi

leverage xor to simplify code in __io_worker_busy

Signed-off-by: Hao Xu <[email protected]>
---

I thought about [IO_WQ_ACCT_UNBOUND + IO_WQ_ACCT_BOUND - index], but it
is not antithetical, so I finally choose to calculate two indexes
respectively.

 fs/io-wq.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/fs/io-wq.c b/fs/io-wq.c
index 433c4d3c3c1c..7ec2948838ca 100644
--- a/fs/io-wq.c
+++ b/fs/io-wq.c
@@ -292,16 +292,12 @@ static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
 	worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
 	work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
 	if (worker_bound != work_bound) {
+		int index0 = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;
+		int index1 = work_bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND;
 		io_wqe_dec_running(worker);
-		if (work_bound) {
-			worker->flags |= IO_WORKER_F_BOUND;
-			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
-			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
-		} else {
-			worker->flags &= ~IO_WORKER_F_BOUND;
-			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
-			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
-		}
+		worker->flags ^= IO_WORKER_F_BOUND;
+		wqe->acct[index0].nr_workers--;
+		wqe->acct[index1].nr_workers++;
 		io_wqe_inc_running(worker);
 	 }
 }
-- 
1.8.3.1


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

* [PATCH v3] io-wq: simplify code in __io_worker_busy
  2021-04-04 19:17   ` Jens Axboe
@ 2021-04-05  7:53     ` Hao Xu
  2021-04-05 21:46       ` Jens Axboe
  0 siblings, 1 reply; 10+ messages in thread
From: Hao Xu @ 2021-04-05  7:53 UTC (permalink / raw)
  To: Jens Axboe; +Cc: io-uring, Pavel Begunkov, Joseph Qi

leverage xor to simplify code in __io_worker_busy

Signed-off-by: Hao Xu <[email protected]>
---
 fs/io-wq.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/fs/io-wq.c b/fs/io-wq.c
index 433c4d3c3c1c..8d8324eba2ec 100644
--- a/fs/io-wq.c
+++ b/fs/io-wq.c
@@ -276,10 +276,12 @@ static void io_wqe_dec_running(struct io_worker *worker)
  */
 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
 			     struct io_wq_work *work)
-	__must_hold(wqe->lock)
+	__must_hold(w qe->lock)
 {
 	bool worker_bound, work_bound;
 
+	BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1);
+
 	if (worker->flags & IO_WORKER_F_FREE) {
 		worker->flags &= ~IO_WORKER_F_FREE;
 		hlist_nulls_del_init_rcu(&worker->nulls_node);
@@ -292,16 +294,11 @@ static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
 	worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
 	work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
 	if (worker_bound != work_bound) {
+		int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;
 		io_wqe_dec_running(worker);
-		if (work_bound) {
-			worker->flags |= IO_WORKER_F_BOUND;
-			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
-			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
-		} else {
-			worker->flags &= ~IO_WORKER_F_BOUND;
-			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
-			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
-		}
+		worker->flags ^= IO_WORKER_F_BOUND;
+		wqe->acct[index].nr_workers--;
+		wqe->acct[index ^ 1].nr_workers++;
 		io_wqe_inc_running(worker);
 	 }
 }
-- 
1.8.3.1


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

* Re: [PATCH v3] io-wq: simplify code in __io_worker_busy
  2021-04-05  7:53     ` [PATCH v3] " Hao Xu
@ 2021-04-05 21:46       ` Jens Axboe
  2021-04-06  2:18         ` Hao Xu
  2021-04-06  3:08         ` [PATCH v4] " Hao Xu
  0 siblings, 2 replies; 10+ messages in thread
From: Jens Axboe @ 2021-04-05 21:46 UTC (permalink / raw)
  To: Hao Xu; +Cc: io-uring, Pavel Begunkov, Joseph Qi

On 4/5/21 1:53 AM, Hao Xu wrote:
> leverage xor to simplify code in __io_worker_busy
> 
> Signed-off-by: Hao Xu <[email protected]>
> ---
>  fs/io-wq.c | 17 +++++++----------
>  1 file changed, 7 insertions(+), 10 deletions(-)
> 
> diff --git a/fs/io-wq.c b/fs/io-wq.c
> index 433c4d3c3c1c..8d8324eba2ec 100644
> --- a/fs/io-wq.c
> +++ b/fs/io-wq.c
> @@ -276,10 +276,12 @@ static void io_wqe_dec_running(struct io_worker *worker)
>   */
>  static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
>  			     struct io_wq_work *work)
> -	__must_hold(wqe->lock)
> +	__must_hold(w qe->lock)

Looks like something is off there? I see a v2 as well, but this one is
later, so...

-- 
Jens Axboe


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

* Re: [PATCH v3] io-wq: simplify code in __io_worker_busy
  2021-04-05 21:46       ` Jens Axboe
@ 2021-04-06  2:18         ` Hao Xu
  2021-04-06  3:08         ` [PATCH v4] " Hao Xu
  1 sibling, 0 replies; 10+ messages in thread
From: Hao Xu @ 2021-04-06  2:18 UTC (permalink / raw)
  To: Jens Axboe; +Cc: io-uring, Pavel Begunkov, Joseph Qi

在 2021/4/6 上午5:46, Jens Axboe 写道:
> On 4/5/21 1:53 AM, Hao Xu wrote:
>> leverage xor to simplify code in __io_worker_busy
>>
>> Signed-off-by: Hao Xu <[email protected]>
>> ---
>>   fs/io-wq.c | 17 +++++++----------
>>   1 file changed, 7 insertions(+), 10 deletions(-)
>>
>> diff --git a/fs/io-wq.c b/fs/io-wq.c
>> index 433c4d3c3c1c..8d8324eba2ec 100644
>> --- a/fs/io-wq.c
>> +++ b/fs/io-wq.c
>> @@ -276,10 +276,12 @@ static void io_wqe_dec_running(struct io_worker *worker)
>>    */
>>   static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
>>   			     struct io_wq_work *work)
>> -	__must_hold(wqe->lock)
>> +	__must_hold(w qe->lock)
> 
> Looks like something is off there? I see a v2 as well, but this one is
> later, so...
> 
v2 is sent before I saw your comment, actully both v2 and v3 make sense
for me, but now I prefer v3.

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

* [PATCH v4] io-wq: simplify code in __io_worker_busy
  2021-04-05 21:46       ` Jens Axboe
  2021-04-06  2:18         ` Hao Xu
@ 2021-04-06  3:08         ` Hao Xu
  2021-04-06 15:36           ` Jens Axboe
  2021-04-07 18:28           ` Pavel Begunkov
  1 sibling, 2 replies; 10+ messages in thread
From: Hao Xu @ 2021-04-06  3:08 UTC (permalink / raw)
  To: Jens Axboe; +Cc: io-uring, Pavel Begunkov, Joseph Qi

leverage xor to simplify code in __io_worker_busy

Signed-off-by: Hao Xu <[email protected]>
---

Sorry, typo by mistake...

 fs/io-wq.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/fs/io-wq.c b/fs/io-wq.c
index 2dd2d4b1e538..fa2383cb4d50 100644
--- a/fs/io-wq.c
+++ b/fs/io-wq.c
@@ -329,6 +329,8 @@ static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
 {
 	bool worker_bound, work_bound;
 
+	BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1);
+
 	if (worker->flags & IO_WORKER_F_FREE) {
 		worker->flags &= ~IO_WORKER_F_FREE;
 		hlist_nulls_del_init_rcu(&worker->nulls_node);
@@ -341,16 +343,11 @@ static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
 	worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
 	work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
 	if (worker_bound != work_bound) {
+		int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;
 		io_wqe_dec_running(worker);
-		if (work_bound) {
-			worker->flags |= IO_WORKER_F_BOUND;
-			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
-			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
-		} else {
-			worker->flags &= ~IO_WORKER_F_BOUND;
-			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
-			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
-		}
+		worker->flags ^= IO_WORKER_F_BOUND;
+		wqe->acct[index].nr_workers--;
+		wqe->acct[index ^ 1].nr_workers++;
 		io_wqe_inc_running(worker);
 	 }
 }
-- 
1.8.3.1


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

* Re: [PATCH v4] io-wq: simplify code in __io_worker_busy
  2021-04-06  3:08         ` [PATCH v4] " Hao Xu
@ 2021-04-06 15:36           ` Jens Axboe
  2021-04-07 18:28           ` Pavel Begunkov
  1 sibling, 0 replies; 10+ messages in thread
From: Jens Axboe @ 2021-04-06 15:36 UTC (permalink / raw)
  To: Hao Xu; +Cc: io-uring, Pavel Begunkov, Joseph Qi

On 4/5/21 9:08 PM, Hao Xu wrote:
> leverage xor to simplify code in __io_worker_busy

Applied, thanks.

-- 
Jens Axboe


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

* Re: [PATCH v4] io-wq: simplify code in __io_worker_busy
  2021-04-06  3:08         ` [PATCH v4] " Hao Xu
  2021-04-06 15:36           ` Jens Axboe
@ 2021-04-07 18:28           ` Pavel Begunkov
  1 sibling, 0 replies; 10+ messages in thread
From: Pavel Begunkov @ 2021-04-07 18:28 UTC (permalink / raw)
  To: Hao Xu, Jens Axboe; +Cc: io-uring, Joseph Qi

On 06/04/2021 04:08, Hao Xu wrote:
> leverage xor to simplify code in __io_worker_busy
> 
> Signed-off-by: Hao Xu <[email protected]>
> ---
> 
> Sorry, typo by mistake...
> 
>  fs/io-wq.c | 15 ++++++---------
>  1 file changed, 6 insertions(+), 9 deletions(-)
> 
> diff --git a/fs/io-wq.c b/fs/io-wq.c
> index 2dd2d4b1e538..fa2383cb4d50 100644
> --- a/fs/io-wq.c
> +++ b/fs/io-wq.c
> @@ -329,6 +329,8 @@ static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
>  {
>  	bool worker_bound, work_bound;
>  
> +	BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1);
> +
>  	if (worker->flags & IO_WORKER_F_FREE) {
>  		worker->flags &= ~IO_WORKER_F_FREE;
>  		hlist_nulls_del_init_rcu(&worker->nulls_node);
> @@ -341,16 +343,11 @@ static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
>  	worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
>  	work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
>  	if (worker_bound != work_bound) {
> +		int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;

Jens, if you'll be at it, can you fold in \n here?
Let's keep static analysis happy

>  		io_wqe_dec_running(worker);
> -		if (work_bound) {
> -			worker->flags |= IO_WORKER_F_BOUND;
> -			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
> -			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
> -		} else {
> -			worker->flags &= ~IO_WORKER_F_BOUND;
> -			wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
> -			wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
> -		}
> +		worker->flags ^= IO_WORKER_F_BOUND;
> +		wqe->acct[index].nr_workers--;
> +		wqe->acct[index ^ 1].nr_workers++;
>  		io_wqe_inc_running(worker);
>  	 }
>  }
> 

-- 
Pavel Begunkov

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

end of thread, other threads:[~2021-04-07 18:32 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-02 10:18 [PATCH] io-wq: simplify code in __io_worker_busy Hao Xu
2021-04-02 10:38 ` Pavel Begunkov
2021-04-04 19:17   ` Jens Axboe
2021-04-05  7:53     ` [PATCH v3] " Hao Xu
2021-04-05 21:46       ` Jens Axboe
2021-04-06  2:18         ` Hao Xu
2021-04-06  3:08         ` [PATCH v4] " Hao Xu
2021-04-06 15:36           ` Jens Axboe
2021-04-07 18:28           ` Pavel Begunkov
2021-04-05  7:36   ` [PATCH v2] " Hao Xu

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