[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Tue, 1 Feb 2022 19:26:25 -0800
From: Nick Desaulniers <ndesaulniers@...gle.com>
To: Ard Biesheuvel <ardb@...nel.org>
Cc: Jonathan Corbet <corbet@....net>,
Josh Poimboeuf <jpoimboe@...hat.com>,
Borislav Petkov <bp@...en8.de>,
Peter Zijlstra <peterz@...radead.org>, llvm@...ts.linux.dev,
Kees Cook <keescook@...omium.org>,
Alan Stern <stern@...land.harvard.edu>,
Andrea Parri <parri.andrea@...il.com>,
Will Deacon <will@...nel.org>,
Boqun Feng <boqun.feng@...il.com>,
Nicholas Piggin <npiggin@...il.com>,
David Howells <dhowells@...hat.com>,
Jade Alglave <j.alglave@....ac.uk>,
Luc Maranget <luc.maranget@...ia.fr>,
"Paul E. McKenney" <paulmck@...nel.org>,
Akira Yokosawa <akiyks@...il.com>,
Daniel Lustig <dlustig@...dia.com>,
Joel Fernandes <joel@...lfernandes.org>,
"Gustavo A. R. Silva" <gustavoars@...nel.org>,
Len Baker <len.baker@....com>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
linux-arch <linux-arch@...r.kernel.org>,
Linux Doc Mailing List <linux-doc@...r.kernel.org>
Subject: Re: [PATCH] docs/memory-barriers.txt: volatile is not a barrier() substitute
On Tue, Feb 1, 2022 at 2:15 PM Ard Biesheuvel <ardb@...nel.org> wrote:
>
> On Tue, 1 Feb 2022 at 20:40, Nick Desaulniers <ndesaulniers@...gle.com> wrote:
> >
> > On Tue, Feb 1, 2022 at 1:32 AM Ard Biesheuvel <ardb@...nel.org> wrote:
> > >
> > > On Mon, 31 Jan 2022 at 23:53, Nick Desaulniers <ndesaulniers@...gle.com> wrote:
> > > >
> > > > + around an asm statement so long as clobbers are not violated. For example,
> > > > +
> > > > + asm volatile ("");
> > > > + flag = true;
> > > > +
> > > > + May be modified by the compiler to:
> > > > +
> > > > + flag = true;
> > > > + asm volatile ("");
> > > > +
> > > > + Marking an asm statement as volatile is not a substitute for barrier(),
> > > > + and is implicit for asm goto statements and asm statements that do not
> > > > + have outputs (like the above example). Prefer either:
> > > > +
> > > > + asm ("":::"memory");
> > > > + flag = true;
> > > > +
> > > > + Or:
> > > > +
> > > > + asm ("");
> > > > + barrier();
> > > > + flag = true;
> > > > +
> > >
> > > I would expect the memory clobber to only hazard against the
> > > assignment of flag if it results in a store, but looking at your
> > > Godbolt example, this appears to apply even if flag is kept in a
> > > register.
> > >
> > > Is that behavior documented/codified anywhere? Or are we relying on
> > > compiler implementation details here?
> >
> > https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Volatile
> > "Note that the compiler can move even volatile asm instructions
> > relative to other code, including across jump instructions."
> >
>
> That doesn't really answer my question. We are documenting here that
> asm volatile does not prevent reordering but non-volatile asm with a
> "memory" clobber does, and even prevents reordering of instructions
> that do not modify memory to begin with.
>
> Why is it justified to rely on this undocumented behavior?
I see your point. You're right, I couldn't find anywhere where such
behavior was specified. So the suggestion to use barrier() would rely
on unspecified behavior and should not be suggested.
Probably worth still mentioning that `volatile` qualifying an asm
statement doesn't prevent such reordering in this document somehow,
and perhaps that it's (currently) unspecified whether a barrier() can
prevent re-ordering with regards to non-memory-modifying instructions.
--
Thanks,
~Nick Desaulniers
Powered by blists - more mailing lists