llvm.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@kernel.org>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: "Paul Heidekrüger" <paul.heidekrueger@in.tum.de>,
	will@kernel.org, peterz@infradead.org, boqun.feng@gmail.com,
	parri.andrea@gmail.com, linux-kernel@vger.kernel.org,
	llvm@lists.linux.dev, elver@google.com,
	charalampos.mainas@gmail.com, pramod.bhatotia@in.tum.de
Subject: Re: Potentially Broken Address Dependency via test_bit() When Compiling With Clang
Date: Thu, 4 Nov 2021 11:16:28 -0700	[thread overview]
Message-ID: <20211104181628.GH641268@paulmck-ThinkPad-P17-Gen-1> (raw)
In-Reply-To: <20211102190138.GA1497378@rowland.harvard.edu>

On Tue, Nov 02, 2021 at 03:01:38PM -0400, Alan Stern wrote:
> On Tue, Nov 02, 2021 at 07:35:57PM +0100, Paul Heidekrüger wrote:
> > On Thu, Oct 28, 2021 at 10:34:46AM -0400, Alan Stern wrote:
> > > On Thu, Oct 28, 2021 at 02:37:47PM +0200, Paul Heidekrüger wrote:
> > > > On Wed, Oct 27, 2021 at 10:27:20AM -0400, Alan Stern wrote:
> > > > > On Wed, Oct 27, 2021 at 12:19:48PM +0200, Paul Heidekrüger wrote:
> > > 
> > > > > > Address dependency in source code, lines 373 - 375 in fs/afs/addr_list.c:
> > > > > > 
> > > > > > > [...]
> > > > > > >   index = READ_ONCE(ac->alist->preferred);
> > > > > > >   if (test_bit(index, &set))
> > > > > > >     goto selected;
> > > > > > > [...]
> > > > > > 
> > > > > > where test_bit() expands to the following in 
> > > > > > include/asm-generic/bitops/non-atomic.h, lines 115 - 122:
> > > > > > 
> > > > > > > static __always_inline int
> > > > > > > arch_test_bit(unsigned int nr, const volatile unsigned long *addr)
> > > > > > > {
> > > > > > >   return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
> > > > > > > }
> > > > > > > #define test_bit arch_test_bit
> > > 
> > > > > However, I can't follow the IR code.  Can you please explain in ordinary 
> > > > > English how the LLVM compiler manages to lose track of this dependency?
> > > > > 
> > > > > Alan Stern
> > > > 
> > > > Here's what we think might be going on:
> > > > - In 'arch_test_bit()', 'addr[BIT_WORD(nr)]' expands to 'addr[(nr) / 64]'.
> > > > - Since 'addr' points to an 'unsigned long', any other result than '0' for
> > > >   '(nr) / 64' would be out of bounds and therefore undefined.
> > > > - We assume LLVM is able to figure this out and use it to get rid of the
> > > >   address computation all together.
> > > 
> > > Ah, that explains it.  Yes, when set is a single unsigned long (or an 
> > > array of length 1), the address dependency is only syntactic, not 
> > > semantic.  As a result, we should expect that compilers will sometimes 
> > > not preserve it.
> > 
> > In LKMM's explanation.txt, lines 450 - 453 state:
> > 
> > > A read event and another memory access event are linked by an address
> > > dependency if the value obtained by the read affects the location
> > > accessed by the other event.
> > 
> > If we understand correctly, the ambiguity you're pointing out is that by
> > looking at 'addr[BIT_WORD(nr)]', one could deduce an address dependency by
> > seeing an array subscript operator, using a value which can be traced back to a
> > 'READ_ONCE()' (syntactic).
> 
> Exactly.  The syntax of the expression apparently indicates that the 
> address to be dereferenced depends on the value of nr.
> 
> > However, since 'addr' points to an 'unsigned long', the 'READ_ONCE()' never
> > affects the location accessed in 'arch_test_bit()' as the offset computed in
> > the subscript operator can only be, as clang identified, '0' (semantic).
> 
> Again correct.  Although the address to be dereferenced may seem to 
> depend on nr, in fact it doesn't because in any valid execution nr must 
> not contain any value other than 0 (and the compiler knows this).
> 
> > > The danger, of course, is that people relying on an ordering prescribed
> > > by the LKMM may get fooled because (unbeknownst to them) the dependency
> > > in question is not semantic.
> > 
> > Do you think this would warrant a change to LKMM's explanation.txt to make this
> > more explicit?
> 
> It wouldn't hurt.  Note that explanation.txt does already include a 
> section talking about address dependencies from marked loads to plain 
> reads (line 2260 et seq).  It also talks about the possibility of the 
> compiler undermining the memory model in this regard (line 2308).
> 
> However, it doesn't mention the difference between syntactic and 
> semantic dependencies.  If you would like to submit a patch adding an 
> explicit description of this, please feel free to do so.
> 
> > > It would be great if a static checker
> > > could detect such things -- but this would require some way for us to
> > > inform the checker about when the code does rely on a dependency
> > > ordering.
> > 
> > The compiler passes we're working on will hopefully be able to do exactly that,
> > not taking into account the programmer's intent of course.
> 
> Good luck!

Most of the current proposals involve explicitly telling the compiler
of the programmer's intent.  But if you can make something that is
generally accepted, and that can figure out things on its own, that
would be extremely cool!

							Thanx, Paul

      reply	other threads:[~2021-11-04 18:16 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-27 10:19 Potentially Broken Address Dependency via test_bit() When Compiling With Clang Paul Heidekrüger
2021-10-27 11:56 ` David Laight
2021-10-27 12:17 ` Peter Zijlstra
2021-10-27 12:24   ` Marco Elver
2021-10-27 12:34   ` Peter Zijlstra
2021-10-27 14:27 ` Alan Stern
2021-10-28 12:37   ` Paul Heidekrüger
2021-10-28 14:34     ` Alan Stern
2021-11-02 18:35       ` Paul Heidekrüger
2021-11-02 19:01         ` Alan Stern
2021-11-04 18:16           ` Paul E. McKenney [this message]

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=20211104181628.GH641268@paulmck-ThinkPad-P17-Gen-1 \
    --to=paulmck@kernel.org \
    --cc=boqun.feng@gmail.com \
    --cc=charalampos.mainas@gmail.com \
    --cc=elver@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=parri.andrea@gmail.com \
    --cc=paul.heidekrueger@in.tum.de \
    --cc=peterz@infradead.org \
    --cc=pramod.bhatotia@in.tum.de \
    --cc=stern@rowland.harvard.edu \
    --cc=will@kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).