mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] stop_machine: Use non-atomic read multi_stop_data::state clearly
       [not found] <cover.1697811778.git.rongtao@cestc.cn>
@ 2023-10-20 14:43 ` Rong Tao
  2023-10-24 10:46   ` Mark Rutland
  2023-10-20 14:43 ` [PATCH 2/2] stop_machine: Apply smp_store_release() to multi_stop_data::state Rong Tao
  1 sibling, 1 reply; 7+ messages in thread
From: Rong Tao @ 2023-10-20 14:43 UTC (permalink / raw)
  To: mark.rutland, elver, linux-kernel, peterz, rongtao, rtoax, tglx

From: Rong Tao <rongtao@cestc.cn>

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().

Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
 kernel/stop_machine.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
index cedb17ba158a..268c2e581698 100644
--- a/kernel/stop_machine.c
+++ b/kernel/stop_machine.c
@@ -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
-- 
2.41.0


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

* [PATCH 2/2] stop_machine: Apply smp_store_release() to multi_stop_data::state
       [not found] <cover.1697811778.git.rongtao@cestc.cn>
  2023-10-20 14:43 ` [PATCH 1/2] stop_machine: Use non-atomic read multi_stop_data::state clearly Rong Tao
@ 2023-10-20 14:43 ` Rong Tao
  2023-10-24 11:01   ` Mark Rutland
  1 sibling, 1 reply; 7+ messages in thread
From: Rong Tao @ 2023-10-20 14:43 UTC (permalink / raw)
  To: mark.rutland, elver, linux-kernel, peterz, rongtao, rtoax, tglx

From: Rong Tao <rongtao@cestc.cn>

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

Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
 kernel/stop_machine.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
index 268c2e581698..cdf4a3fe0348 100644
--- a/kernel/stop_machine.c
+++ b/kernel/stop_machine.c
@@ -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. */
-- 
2.41.0


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

* Re: [PATCH 1/2] stop_machine: Use non-atomic read multi_stop_data::state clearly
  2023-10-20 14:43 ` [PATCH 1/2] stop_machine: Use non-atomic read multi_stop_data::state clearly Rong Tao
@ 2023-10-24 10:46   ` Mark Rutland
  2023-10-25  0:37     ` Rong Tao
  2023-10-27 11:49     ` Rong Tao
  0 siblings, 2 replies; 7+ messages in thread
From: Mark Rutland @ 2023-10-24 10:46 UTC (permalink / raw)
  To: Rong Tao; +Cc: elver, linux-kernel, peterz, rongtao, tglx

On Fri, Oct 20, 2023 at 10:43:33PM +0800, Rong Tao wrote:
> From: Rong Tao <rongtao@cestc.cn>
> 
> 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:

Acked-by: Mark Rutland <mark.rutland@arm.com>

Mark.

> Signed-off-by: Rong Tao <rongtao@cestc.cn>
> ---
>  kernel/stop_machine.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
> index cedb17ba158a..268c2e581698 100644
> --- a/kernel/stop_machine.c
> +++ b/kernel/stop_machine.c
> @@ -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
> -- 
> 2.41.0
> 

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

* Re: [PATCH 2/2] stop_machine: Apply smp_store_release() to multi_stop_data::state
  2023-10-20 14:43 ` [PATCH 2/2] stop_machine: Apply smp_store_release() to multi_stop_data::state Rong Tao
@ 2023-10-24 11:01   ` Mark Rutland
  2023-10-25  0:59     ` Rong Tao
  0 siblings, 1 reply; 7+ messages in thread
From: Mark Rutland @ 2023-10-24 11:01 UTC (permalink / raw)
  To: Rong Tao, peterz; +Cc: elver, linux-kernel, peterz, rongtao, tglx

On Fri, Oct 20, 2023 at 10:43:34PM +0800, Rong Tao wrote:
> From: Rong Tao <rongtao@cestc.cn>
> 
> Replace smp_wmb()+WRITE_ONCE() with smp_store_release() and add comment.
> 
> Signed-off-by: Rong Tao <rongtao@cestc.cn>
> ---
>  kernel/stop_machine.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
> index 268c2e581698..cdf4a3fe0348 100644
> --- a/kernel/stop_machine.c
> +++ b/kernel/stop_machine.c
> @@ -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.

>  }
>  
>  /* Last one to ack a state moves to the next state. */
> -- 
> 2.41.0
> 

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

* Re: [PATCH 1/2] stop_machine: Use non-atomic read multi_stop_data::state clearly
  2023-10-24 10:46   ` Mark Rutland
@ 2023-10-25  0:37     ` Rong Tao
  2023-10-27 11:49     ` Rong Tao
  1 sibling, 0 replies; 7+ messages in thread
From: Rong Tao @ 2023-10-25  0:37 UTC (permalink / raw)
  To: mark.rutland; +Cc: elver, linux-kernel, peterz, rongtao, rtoax, tglx

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


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

* Re: [PATCH 2/2] stop_machine: Apply smp_store_release() to multi_stop_data::state
  2023-10-24 11:01   ` Mark Rutland
@ 2023-10-25  0:59     ` Rong Tao
  0 siblings, 0 replies; 7+ messages in thread
From: Rong Tao @ 2023-10-25  0:59 UTC (permalink / raw)
  To: Mark Rutland, peterz; +Cc: elver, linux-kernel, rongtao, tglx


On 10/24/23 7:01 PM, Mark Rutland wrote:
> On Fri, Oct 20, 2023 at 10:43:34PM +0800, Rong Tao wrote:
>> From: Rong Tao <rongtao@cestc.cn>
>>
>> Replace smp_wmb()+WRITE_ONCE() with smp_store_release() and add comment.
>>
>> Signed-off-by: Rong Tao <rongtao@cestc.cn>
>> ---
>>   kernel/stop_machine.c | 6 ++++--
>>   1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
>> index 268c2e581698..cdf4a3fe0348 100644
>> --- a/kernel/stop_machine.c
>> +++ b/kernel/stop_machine.c
>> @@ -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
Thanks, Mark, I'll fix the comment in next patch version.
>
> 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.

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

Rong Tao.

>
> Mark.
>
>>   }
>>   
>>   /* Last one to ack a state moves to the next state. */
>> -- 
>> 2.41.0
>>


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

* Re: [PATCH 1/2] stop_machine: Use non-atomic read multi_stop_data::state clearly
  2023-10-24 10:46   ` Mark Rutland
  2023-10-25  0:37     ` Rong Tao
@ 2023-10-27 11:49     ` Rong Tao
  1 sibling, 0 replies; 7+ messages in thread
From: Rong Tao @ 2023-10-27 11:49 UTC (permalink / raw)
  To: Mark Rutland; +Cc: elver, linux-kernel, peterz, rongtao, tglx


On 10/24/23 6:46 PM, Mark Rutland wrote:
> On Fri, Oct 20, 2023 at 10:43:33PM +0800, Rong Tao wrote:
>> From: Rong Tao <rongtao@cestc.cn>
>>
>> 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:
>
> Acked-by: Mark Rutland <mark.rutland@arm.com>
>
> 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/tencent_FB1D31CEC045E837ABE5B25CC5E37575F405@qq.com/

>
>> Signed-off-by: Rong Tao <rongtao@cestc.cn>
>> ---
>>   kernel/stop_machine.c | 7 ++++---
>>   1 file changed, 4 insertions(+), 3 deletions(-)
>>
>> diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
>> index cedb17ba158a..268c2e581698 100644
>> --- a/kernel/stop_machine.c
>> +++ b/kernel/stop_machine.c
>> @@ -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
>> -- 
>> 2.41.0
>>


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

end of thread, other threads:[~2023-10-27 11:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <cover.1697811778.git.rongtao@cestc.cn>
2023-10-20 14:43 ` [PATCH 1/2] stop_machine: Use non-atomic read multi_stop_data::state clearly Rong Tao
2023-10-24 10:46   ` Mark Rutland
2023-10-25  0:37     ` Rong Tao
2023-10-27 11:49     ` Rong Tao
2023-10-20 14:43 ` [PATCH 2/2] stop_machine: Apply smp_store_release() to multi_stop_data::state Rong Tao
2023-10-24 11:01   ` Mark Rutland
2023-10-25  0:59     ` Rong Tao

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

Powered by JetHome