mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] stop_machine: Avoid potential non-atomic read of multi_stop_data::state
@ 2023-10-19  0:11 Rong Tao
  2023-10-20 10:23 ` Mark Rutland
  0 siblings, 1 reply; 3+ messages in thread
From: Rong Tao @ 2023-10-19  0:11 UTC (permalink / raw)
  To: elver, linux-kernel, mark.rutland, peterz, rongtao, tglx

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,
Pass curstate as a parameter to ack_state(), to avoid the non-atomic read.

And replace smp_wmb()+WRITE_ONCE() with smp_store_release().

Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
v1: stop_machine: Avoid potential race behaviour of multi_stop_data::state
    https://lore.kernel.org/lkml/tencent_705C16DF25978ACAEBD1E83E228881901006@qq.com/
---
 kernel/stop_machine.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
index cedb17ba158a..35a122ce2cbd 100644
--- a/kernel/stop_machine.c
+++ b/kernel/stop_machine.c
@@ -183,15 +183,15 @@ 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);
+	smp_store_release(&msdata->state, newstate);
 }
 
 /* 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 +242,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.42.0


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

end of thread, other threads:[~2023-10-20 10:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-19  0:11 [PATCH v2] stop_machine: Avoid potential non-atomic read of multi_stop_data::state Rong Tao
2023-10-20 10:23 ` Mark Rutland
2023-10-20 10:48   ` Peter Zijlstra

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®