Re: [PATCH -next] x86/mm/pat: fix a data race in cpa_inc_4k_install

From: Peter Zijlstra
Date: Tue Jan 21 2020 - 04:51:08 EST


On Tue, Jan 21, 2020 at 09:19:18AM +0100, Marco Elver wrote:
> On Tue, 21 Jan 2020 at 08:27, Borislav Petkov <bp@xxxxxxxxx> wrote:
> >
> > On Mon, Jan 20, 2020 at 11:12:00PM -0500, Qian Cai wrote:
> > > diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
> > > index 20823392f4f2..31e4a73ae70e 100644
> > > --- a/arch/x86/mm/pat/set_memory.c
> > > +++ b/arch/x86/mm/pat/set_memory.c
> > > @@ -128,7 +128,7 @@ static inline void cpa_inc_2m_checked(void)
> > >
> > > static inline void cpa_inc_4k_install(void)
> > > {
> > > - cpa_4k_install++;
> > > + WRITE_ONCE(cpa_4k_install, READ_ONCE(cpa_4k_install) + 1);
>
> As I said in my email that you also copied to the message, this is
> just a stats counter. For the general case, I think we reached
> consensus that such accesses should intentionally remain data races:
> https://lore.kernel.org/linux-fsdevel/CAHk-=wg5CkOEF8DTez1Qu0XTEFw_oHhxN98bDnFqbY7HL5AB2g@xxxxxxxxxxxxxx/T/#u
>
> Either you can use the data_race() macro, making this
> 'data_race(cpa_4k_install++)' -- this effectively documents the
> intentional data race --

Yes, that sounds useful.

But this patch, as presented, is just plain wrong. It doesn't fix
anything.