Skip to contentSkip to conversation

io-wq: simplify code in __io_worker_busy

PATCH Superseded Hao Xu 3 participants 10 messages Apr 2, 2021 – Apr 7, 2021

Subsystems (2)
lore
Share & tools
lore

Hao Xu

author
#0 lore raw

PATCH 1/1 io-wq: simplify code in __io_worker_busy

leverage xor to simplify code in __io_worker_busy

---
 fs/io-wq.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
fs/io-wq.c +4 -9
index 7434eb40ca8c..f77e4704d7c7 100644
@@ -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);
 	 }
 }
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 1 line · Hao Xu

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.

Quoted 32 lines
---
 fs/io-wq.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
fs/io-wq.c +4 -9
index 7434eb40ca8c..f77e4704d7c7 100644
@@ -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);
 	 }
 }
Signature · 1 line
Pavel Begunkov
maintainer
#2 lore raw
Quoted 11 lines · Pavel Begunkov
Quoted 1 line · Hao Xu

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?

Signature · 1 line
Jens Axboe
author
#3 lore raw

PATCH v3 1/1 io-wq: simplify code in __io_worker_busy

leverage xor to simplify code in __io_worker_busy

---
 fs/io-wq.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
fs/io-wq.c +7 -10
index 433c4d3c3c1c..8d8324eba2ec 100644
@@ -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);
 	 }
 }
Signature · 1 line
1.8.3.1
maintainer
#4 lore raw
Quoted 12 lines · Hao Xu

leverage xor to simplify code in __io_worker_busy

---
 fs/io-wq.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
fs/io-wq.c +0 -0
index 433c4d3c3c1c..8d8324eba2ec 100644

fs/io-wq.c static void io_wqe_dec_running(struct io_worker *worker) · line 279

Quoted 5 lines
fs/io-wq.c +1 -1
@@ -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...

Signature · 1 line
Jens Axboe
author
#5 lore raw
Quoted 8 lines · Jens Axboe
Quoted 7 lines · Hao Xu

leverage xor to simplify code in __io_worker_busy

---
  fs/io-wq.c | 17 +++++++----------
  1 file changed, 7 insertions(+), 10 deletions(-)
Quoted 14 lines

fs/io-wq.c static void io_wqe_dec_running(struct io_worker *worker) · line 279

Quoted 10 lines
fs/io-wq.c +1 -1
index 433c4d3c3c1c..8d8324eba2ec 100644
@@ -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.

author
#6 lore raw

PATCH v4 1/1 io-wq: simplify code in __io_worker_busy

In reply to Jens Axboe

leverage xor to simplify code in __io_worker_busy

---

Sorry, typo by mistake...

 fs/io-wq.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
fs/io-wq.c +6 -9
index 2dd2d4b1e538..fa2383cb4d50 100644
@@ -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);
 	 }
 }
Signature · 1 line
1.8.3.1
maintainer Applied
#7 lore raw
Quoted 1 line · Hao Xu

leverage xor to simplify code in __io_worker_busy

Applied, thanks.

Signature · 1 line
Jens Axboe
#8 lore raw
In reply to Hao Xu
Quoted 24 lines · Hao Xu

leverage xor to simplify code in __io_worker_busy

---

Sorry, typo by mistake...

 fs/io-wq.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
fs/io-wq.c +2 -0
index 2dd2d4b1e538..fa2383cb4d50 100644
@@ -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);

fs/io-wq.c static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker, · line 346

Quoted 4 lines
fs/io-wq.c +1 -0
@@ -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

Quoted 17 lines
fs/io-wq.c +3 -9
 		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);
 	 }
 }
Signature · 1 line
Pavel Begunkov
author
#9 lore raw

PATCH v2 1/1 io-wq: simplify code in __io_worker_busy

In reply to Pavel Begunkov

leverage xor to simplify code in __io_worker_busy

---

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(-)
fs/io-wq.c +5 -9
index 433c4d3c3c1c..7ec2948838ca 100644
@@ -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);
 	 }
 }
Signature · 1 line
1.8.3.1