Skip to contentSkip to conversation

stop_machine: Use non-atomic read multi_stop_data::state clearly

PATCH In review Rong Tao 2 participants 7 messages Oct 20, 2023 – Oct 27, 2023 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Rong Tao

author
#1 lore raw

PATCH 1/2 stop_machine: Use non-atomic read multi_stop_data::state clearly

From: Rong Tao <[email protected]>

commit b1fc58333575 ("stop_machine: Avoid potential race behaviour") solved the race behaviour problem, to better show that race behaviour does not exist, pass the 'curstate' directly to ack_state() instead of refetching msdata->state in ack_state().

---
 kernel/stop_machine.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/stop_machine.c +4 -3
index cedb17ba158a..268c2e581698 100644
@@ -188,10 +188,11 @@ static void set_state(struct multi_stop_data *msdata,
 }
 
 /* Last one to ack a state moves to the next state. */
-static void ack_state(struct multi_stop_data *msdata)
+static void ack_state(struct multi_stop_data *msdata,
+		      enum multi_stop_state curstate)
 {
 	if (atomic_dec_and_test(&msdata->thread_ack))
-		set_state(msdata, msdata->state + 1);
+		set_state(msdata, curstate + 1);
 }
 
 notrace void __weak stop_machine_yield(const struct cpumask *cpumask)
@@ -242,7 +243,7 @@ static int multi_cpu_stop(void *data)
 			default:
 				break;
 			}
-			ack_state(msdata);
+			ack_state(msdata, curstate);
 		} else if (curstate > MULTI_STOP_PREPARE) {
 			/*
 			 * At this stage all other CPUs we depend on must spin
Signature · 1 line
2.41.0
Acked-by on v1 1/2
#2 lore raw
Quoted 7 lines · Rong Tao

From: Rong Tao <[email protected]>

commit b1fc58333575 ("stop_machine: Avoid potential race behaviour") solved the race behaviour problem, to better show that race behaviour does not exist, pass the 'curstate' directly to ack_state() instead of refetching msdata->state in ack_state().

I'd prefer if we make this:

| stop_machine: pass curstate to ack_state()
| 
| The multi_cpu_stop() state machine uses multi_stop_data::state to hold
| the current state, and this is read and written atomically except in
| ack_state(), which performs a non-atomic read.
| 
| As ack_state() only performs this non-atomic read when there is a single
| writer, this is benign, but it makes reasoning about the state machine a
| little harder.
| 
| Remove the non-atomic read and pass the (atomically read) curstate in
| instead. This makes it clear that we do not expect any racy writes, and
| avoids a redundant load.

With that wording:

Mark.

Quoted 35 lines
---
 kernel/stop_machine.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/stop_machine.c +4 -3
index cedb17ba158a..268c2e581698 100644
@@ -188,10 +188,11 @@ static void set_state(struct multi_stop_data *msdata,
 }
 
 /* Last one to ack a state moves to the next state. */
-static void ack_state(struct multi_stop_data *msdata)
+static void ack_state(struct multi_stop_data *msdata,
+		      enum multi_stop_state curstate)
 {
 	if (atomic_dec_and_test(&msdata->thread_ack))
-		set_state(msdata, msdata->state + 1);
+		set_state(msdata, curstate + 1);
 }
 
 notrace void __weak stop_machine_yield(const struct cpumask *cpumask)
@@ -242,7 +243,7 @@ static int multi_cpu_stop(void *data)
 			default:
 				break;
 			}
-			ack_state(msdata);
+			ack_state(msdata, curstate);
 		} else if (curstate > MULTI_STOP_PREPARE) {
 			/*
 			 * At this stage all other CPUs we depend on must spin
Signature · 1 line
2.41.0
author
#3 lore raw

Thanks for your advice, Mark.

Your commit information is clearer and easier to understand, I will use it in the next patch version, thank you.

Rong Tao

author
#4 lore raw
In reply to Mark Rutland
Quoted 24 lines · Mark Rutland
Quoted 7 lines · Rong Tao

From: Rong Tao <[email protected]>

commit b1fc58333575 ("stop_machine: Avoid potential race behaviour") solved the race behaviour problem, to better show that race behaviour does not exist, pass the 'curstate' directly to ack_state() instead of refetching msdata->state in ack_state().

I'd prefer if we make this:

| stop_machine: pass curstate to ack_state()
|
| The multi_cpu_stop() state machine uses multi_stop_data::state to hold
| the current state, and this is read and written atomically except in
| ack_state(), which performs a non-atomic read.
|
| As ack_state() only performs this non-atomic read when there is a single
| writer, this is benign, but it makes reasoning about the state machine a
| little harder.
|
| Remove the non-atomic read and pass the (atomically read) curstate in
| instead. This makes it clear that we do not expect any racy writes, and
| avoids a redundant load.
Quoted 5 lines

With that wording:

Mark.

Hi, Mark, I just submit a single patch [0] individually, not as a patchset.

please review. thank you.

Rong Tao

[0] https://lore.kernel.org/lkml/[email protected]/

Quoted 36 lines
Quoted 36 lines
---
  kernel/stop_machine.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
kernel/stop_machine.c +4 -3
index cedb17ba158a..268c2e581698 100644
@@ -188,10 +188,11 @@ static void set_state(struct multi_stop_data *msdata,
  }
 
  /* Last one to ack a state moves to the next state. */
-static void ack_state(struct multi_stop_data *msdata)
+static void ack_state(struct multi_stop_data *msdata,
+		      enum multi_stop_state curstate)
  {
  	if (atomic_dec_and_test(&msdata->thread_ack))
-		set_state(msdata, msdata->state + 1);
+		set_state(msdata, curstate + 1);
  }
 
  notrace void __weak stop_machine_yield(const struct cpumask *cpumask)
@@ -242,7 +243,7 @@ static int multi_cpu_stop(void *data)
  			default:
  				break;
  			}
-			ack_state(msdata);
+			ack_state(msdata, curstate);
  		} else if (curstate > MULTI_STOP_PREPARE) {
  			/*
  			 * At this stage all other CPUs we depend on must spin
Signature · 1 line
2.41.0
author
#5 lore raw

PATCH 2/2 stop_machine: Apply smp_store_release() to multi_stop_data::state

In reply to a message not in the archive

From: Rong Tao <[email protected]>

Replace smp_wmb()+WRITE_ONCE() with smp_store_release() and add comment.

---
 kernel/stop_machine.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/stop_machine.c +4 -2
index 268c2e581698..cdf4a3fe0348 100644
@@ -183,8 +183,10 @@ static void set_state(struct multi_stop_data *msdata,
 {
 	/* Reset ack counter. */
 	atomic_set(&msdata->thread_ack, msdata->num_threads);
-	smp_wmb();
-	WRITE_ONCE(msdata->state, newstate);
+	/* This smp_store_release() pair with READ_ONCE() in multi_cpu_stop().
+	 * Avoid potential access multi_stop_data::state race behaviour.
+	 */
+	smp_store_release(&msdata->state, newstate);
 }
 
 /* Last one to ack a state moves to the next state. */
Signature · 1 line
2.41.0
#6 lore raw

Re: [PATCH 2/2] stop_machine: Apply smp_store_release() to multi_stop_data::state

Quoted 14 lines · Rong Tao

From: Rong Tao <[email protected]>

Replace smp_wmb()+WRITE_ONCE() with smp_store_release() and add comment.

---
 kernel/stop_machine.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/stop_machine.c +0 -0
index 268c2e581698..cdf4a3fe0348 100644

kernel/stop_machine.c static void set_state(struct multi_stop_data *msdata, · line 189

Quoted 9 lines
kernel/stop_machine.c +4 -2
@@ -183,8 +183,10 @@ static void set_state(struct multi_stop_data *msdata,
 {
 	/* Reset ack counter. */
 	atomic_set(&msdata->thread_ack, msdata->num_threads);
-	smp_wmb();
-	WRITE_ONCE(msdata->state, newstate);
+	/* This smp_store_release() pair with READ_ONCE() in multi_cpu_stop().
+	 * Avoid potential access multi_stop_data::state race behaviour.
+	 */
+	smp_store_release(&msdata->state, newstate);

This doesn't match coding style:

	/*
	 * Block comments should look like this, with a leading '/*' line
	 * before the text and a traling '*/' line afterwards.
	 */

See https://www.kernel.org/doc/html/v4.10/process/coding-style.html#commenting

I don't think the "Avoid potential access multi_stop_data::state race behaviour." text is all that helpful, and I think we can drop that.

In general, it's unusual to pair a smp_store_release() with READ_ONCE(), and for that to work it relies on dependency ordering and/or hazarding on the reader side (e.g. the atomic_dec_and_test() is ordered after the READ_ONCE() since it's an RMW and there's a control dependency, but a plain read could be reordered w.r.t. the READ_ONCE()). So we probably need to explain that if we're going to comment on that smp_store_release().

Peter, might it be worth replacing the READ_ONCE() with smp_load_acquire() at the same time? I know it's not strictly necessary given the ordering we have today, but it would at least be obvious.

Mark.

Quoted 6 lines
kernel/stop_machine.c +0 -0
 }
 
 /* Last one to ack a state moves to the next state. */
Signature · 1 line
2.41.0
author
#7 lore raw

Re: [PATCH 2/2] stop_machine: Apply smp_store_release() to multi_stop_data::state

Quoted 26 lines · Mark Rutland
Quoted 14 lines · Rong Tao

From: Rong Tao <[email protected]>

Replace smp_wmb()+WRITE_ONCE() with smp_store_release() and add comment.

---
  kernel/stop_machine.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
kernel/stop_machine.c +0 -0
index 268c2e581698..cdf4a3fe0348 100644

kernel/stop_machine.c static void set_state(struct multi_stop_data *msdata, · line 189

Quoted 9 lines
kernel/stop_machine.c +4 -2
@@ -183,8 +183,10 @@ static void set_state(struct multi_stop_data *msdata,
  {
  	/* Reset ack counter. */
  	atomic_set(&msdata->thread_ack, msdata->num_threads);
-	smp_wmb();
-	WRITE_ONCE(msdata->state, newstate);
+	/* This smp_store_release() pair with READ_ONCE() in multi_cpu_stop().
+	 * Avoid potential access multi_stop_data::state race behaviour.
+	 */
+	smp_store_release(&msdata->state, newstate);

This doesn't match coding style:

Quoted 6 lines
	/*
	 * Block comments should look like this, with a leading '/*' line
	 * before the text and a traling '*/' line afterwards.
	 */

See https://www.kernel.org/doc/html/v4.10/process/coding-style.html#commenting

Thanks, Mark, I'll fix the comment in next patch version.

Quoted 11 lines

I don't think the "Avoid potential access multi_stop_data::state race behaviour." text is all that helpful, and I think we can drop that.

In general, it's unusual to pair a smp_store_release() with READ_ONCE(), and for that to work it relies on dependency ordering and/or hazarding on the reader side (e.g. the atomic_dec_and_test() is ordered after the READ_ONCE() since it's an RMW and there's a control dependency, but a plain read could be reordered w.r.t. the READ_ONCE()). So we probably need to explain that if we're going to comment on that smp_store_release().

Quoted 3 lines

Peter, might it be worth replacing the READ_ONCE() with smp_load_acquire() at the same time? I know it's not strictly necessary given the ordering we have today, but it would at least be obvious.

After I wait for Peter to reply to this message, I will write a patch based on Peter's suggestion.

Rong Tao.

Quoted 9 lines

Mark.

Quoted 6 lines
kernel/stop_machine.c +0 -0
  }
 
  /* Last one to ack a state moves to the next state. */
Signature · 1 line
2.41.0