mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] stop_machine: Avoid potential race behaviour of multi_stop_data::state
@ 2023-10-17  8:50 Rong Tao
  2023-10-17 10:24 ` Mark Rutland
  0 siblings, 1 reply; 4+ messages in thread
From: Rong Tao @ 2023-10-17  8:50 UTC (permalink / raw)
  To: mark.rutland, elver, tglx, peterz, Rong Tao, open list

From: Rong Tao <rongtao@cestc.cn>

In commit b1fc58333575 ("stop_machine: Avoid potential race behaviour")
fix both multi_cpu_stop() and set_state() access multi_stop_data::state,
We should ensure that multi_stop_data::state is accessed using the rwonce
method.

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

diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
index cedb17ba158a..73de9ab77132 100644
--- a/kernel/stop_machine.c
+++ b/kernel/stop_machine.c
@@ -191,7 +191,7 @@ static void set_state(struct multi_stop_data *msdata,
 static void ack_state(struct multi_stop_data *msdata)
 {
 	if (atomic_dec_and_test(&msdata->thread_ack))
-		set_state(msdata, msdata->state + 1);
+		set_state(msdata, READ_ONCE(msdata->state) + 1);
 }
 
 notrace void __weak stop_machine_yield(const struct cpumask *cpumask)
-- 
2.42.0


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

* Re: [PATCH] stop_machine: Avoid potential race behaviour of multi_stop_data::state
  2023-10-17  8:50 [PATCH] stop_machine: Avoid potential race behaviour of multi_stop_data::state Rong Tao
@ 2023-10-17 10:24 ` Mark Rutland
  2023-10-17 13:14   ` Peter Zijlstra
  2023-10-19  0:13   ` Rong Tao
  0 siblings, 2 replies; 4+ messages in thread
From: Mark Rutland @ 2023-10-17 10:24 UTC (permalink / raw)
  To: Rong Tao; +Cc: elver, tglx, peterz, Rong Tao, open list

On Tue, Oct 17, 2023 at 04:50:52PM +0800, Rong Tao wrote:
> From: Rong Tao <rongtao@cestc.cn>
> 
> In commit b1fc58333575 ("stop_machine: Avoid potential race behaviour")
> fix both multi_cpu_stop() and set_state() access multi_stop_data::state,
> We should ensure that multi_stop_data::state is accessed using the rwonce
> method.
> 
> Signed-off-by: Rong Tao <rongtao@cestc.cn>
> ---
>  kernel/stop_machine.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
> index cedb17ba158a..73de9ab77132 100644
> --- a/kernel/stop_machine.c
> +++ b/kernel/stop_machine.c
> @@ -191,7 +191,7 @@ static void set_state(struct multi_stop_data *msdata,
>  static void ack_state(struct multi_stop_data *msdata)
>  {
>  	if (atomic_dec_and_test(&msdata->thread_ack))
> -		set_state(msdata, msdata->state + 1);
> +		set_state(msdata, READ_ONCE(msdata->state) + 1);

IIUC this is bening, as the state machine only ever has a single writer to
msdata->state (though which thread is the writer may change per iteration of
the loop).

At this point we know that the active thread *is* the writer, and so no other
threads can write to msdata->state, so there is no race and reading that
non-atomically is fine.

I'm not opposed to making this use READ_ONCE(), but I don't think that it's
strictly necessary to do so.

That said, if we really want to avoid the non-atomic read, it's probably better
to have multi_cpu_stop() pass curstate as a paramter to ack_state. That or fold
ack_state() into multi_cpu_stop() and use curstate directly.

Mark.

>  }
>  
>  notrace void __weak stop_machine_yield(const struct cpumask *cpumask)
> -- 
> 2.42.0
> 

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

* Re: [PATCH] stop_machine: Avoid potential race behaviour of multi_stop_data::state
  2023-10-17 10:24 ` Mark Rutland
@ 2023-10-17 13:14   ` Peter Zijlstra
  2023-10-19  0:13   ` Rong Tao
  1 sibling, 0 replies; 4+ messages in thread
From: Peter Zijlstra @ 2023-10-17 13:14 UTC (permalink / raw)
  To: Mark Rutland; +Cc: Rong Tao, elver, tglx, Rong Tao, open list

On Tue, Oct 17, 2023 at 11:24:40AM +0100, Mark Rutland wrote:

> That said, if we really want to avoid the non-atomic read, it's probably better
> to have multi_cpu_stop() pass curstate as a paramter to ack_state. That or fold
> ack_state() into multi_cpu_stop() and use curstate directly.

Right. Also, that smp_wmb() needs a comment. Or perhaps
smp_store_release().

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

* Re: [PATCH] stop_machine: Avoid potential race behaviour of multi_stop_data::state
  2023-10-17 10:24 ` Mark Rutland
  2023-10-17 13:14   ` Peter Zijlstra
@ 2023-10-19  0:13   ` Rong Tao
  1 sibling, 0 replies; 4+ messages in thread
From: Rong Tao @ 2023-10-19  0:13 UTC (permalink / raw)
  To: mark.rutland, peterz; +Cc: elver, linux-kernel, rongtao, rtoax, tglx

Thanks, Mark and Peter, I just submit v2, please review.

Rong Tao


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

end of thread, other threads:[~2023-10-19  0:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-17  8:50 [PATCH] stop_machine: Avoid potential race behaviour of multi_stop_data::state Rong Tao
2023-10-17 10:24 ` Mark Rutland
2023-10-17 13:14   ` Peter Zijlstra
2023-10-19  0:13   ` Rong Tao

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

all inboxes | Powered by JetHome®