stop_machine: Use non-atomic read multi_stop_data::state clearly
A message that is not in the archive (sent off-list or not yet received): [email protected]
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
@@ -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
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 toack_state()instead of refetchingmsdata->stateinack_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
Signed-off-by Rong Tao <[email protected]>--- kernel/stop_machine.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)kernel/stop_machine.c +4
-3@@ -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
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
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 toack_state()instead of refetchingmsdata->stateinack_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:
Acked-by Mark Rutland <[email protected]>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
Signed-off-by Rong Tao <[email protected]>--- kernel/stop_machine.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)kernel/stop_machine.c +4
-3@@ -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
PATCH 2/2 stop_machine: Apply smp_store_release() to multi_stop_data::state
In reply to a message not in the archiveFrom: 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
@@ -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
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.Signed-off-by Rong Tao <[email protected]>--- kernel/stop_machine.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)kernel/stop_machine.c +0
-0
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
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.Signed-off-by Rong Tao <[email protected]>--- kernel/stop_machine.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)kernel/stop_machine.c +0
-0kernel/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::staterace behaviour." text is all that helpful, and I think we can drop that.In general, it's unusual to pair a
smp_store_release()withREAD_ONCE(), and for that to work it relies on dependency ordering and/or hazarding on the reader side (e.g. theatomic_dec_and_test()is ordered after theREAD_ONCE()since it's an RMW and there's a control dependency, but a plain read could be reordered w.r.t. theREAD_ONCE()). So we probably need to explain that if we're going to comment on thatsmp_store_release().
Quoted 3 lines
Peter, might it be worth replacing the
READ_ONCE()withsmp_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