From: Mark Rutland <mark.rutland@arm.com>
To: Rong Tao <rtoax@foxmail.com>
Cc: elver@google.com, tglx@linutronix.de, peterz@infradead.org,
Rong Tao <rongtao@cestc.cn>,
open list <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] stop_machine: Avoid potential race behaviour of multi_stop_data::state
Date: Tue, 17 Oct 2023 11:24:40 +0100 [thread overview]
Message-ID: <ZS5g6I-UtUnihToH@FVFF77S0Q05N> (raw)
In-Reply-To: <tencent_705C16DF25978ACAEBD1E83E228881901006@qq.com>
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
>
next prev parent reply other threads:[~2023-10-17 10:24 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-17 8:50 Rong Tao
2023-10-17 10:24 ` Mark Rutland [this message]
2023-10-17 13:14 ` Peter Zijlstra
2023-10-19 0:13 ` Rong Tao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZS5g6I-UtUnihToH@FVFF77S0Q05N \
--to=mark.rutland@arm.com \
--cc=elver@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=rongtao@cestc.cn \
--cc=rtoax@foxmail.com \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®