public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "m.k.edwards at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/48126] arm_output_sync_loop: misplaced memory barrier, missing clrex / dummy strex
Date: Tue, 24 May 2011 17:05:00 -0000	[thread overview]
Message-ID: <bug-48126-4-12Rkq1JAvo@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-48126-4@http.gcc.gnu.org/bugzilla/>

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48126

--- Comment #4 from Michael K. Edwards <m.k.edwards at gmail dot com> 2011-05-24 16:38:41 UTC ---
OK, that's a clear explanation of why the DMB is necessary in the case where
both the compare and the store succeed (neither branch is taken; at a higher
semantic level, a lock is acquired, if that's what the atomic is being used
for).  For future reference, I would appreciate having those ARM ARM
quotations, along with some indication of how load scheduling interacts with a
branch past a memory barrier.

Suppose that the next instruction after label "2" is a load.  On some ARMv7
implementations, I presume that this load can get issued speculatively as early
as label "1", due to the "bne 2f" branch shadow, which skips the trailing dmb. 
I gather that the intention is that, if this branch is not taken (and thus we
execute through the trailing dmb), the fetch results from the branch shadow
should be discarded, and the load re-issued (with, in a multi-core device, the
appropriate ordering guarantee with respect to the strex).

If this interpretation is more or less right, and the shipping silicon behaves
as intended, then the branch past the dmb may be harmless -- although I might
argue that it wastes memory bandwidth in what is usually the common case
(compare-and-swap succeeds) in exchange for a slight reduction in latency in
the other case.

Yet that's still not quite the documented semantics of the GCC compare-and-swap
primitive, which is supposed to be totally ordered whether or not the swap
succeeds.  When I write a lock-free algorithm, I may well rely on the guarantee
that the value read in the next line of code was actually fetched after the
value fetched by the ldrex.  In fact, I have code that does rely on this
guarantee; if thread A sees that thread B has altered the atomic location, then
it expects to be able to see all data that thread B wrote before issuing its
compare-and-swap.  Here's the problem case:

thread A:                 thread B:

dmb
<speculated load of Y>
                          store Y
                          dmb
                          ldrex X
                          cmp
                          bne (doesn't branch)
                          strex X
ldrex X
cmp
bne (branches)
load Y (speculated above)

Is there something I'm not seeing that prevents this?


  parent reply	other threads:[~2011-05-24 17:02 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-14 23:12 [Bug target/48126] New: " m.k.edwards at gmail dot com
2011-03-17 16:17 ` [Bug target/48126] " marcus.shawcroft at arm dot com
2011-03-17 18:38 ` m.k.edwards at gmail dot com
2011-05-24 14:46 ` marcus.shawcroft at arm dot com
2011-05-24 17:05 ` m.k.edwards at gmail dot com [this message]
2011-06-22 16:40 ` david.gilbert at linaro dot org
2011-06-22 19:01 ` m.k.edwards at gmail dot com
2011-06-23  9:33 ` marcus.shawcroft at arm dot com
2011-06-24 11:29 ` m.k.edwards at gmail dot com
2011-06-24 13:57 ` ramana at gcc dot gnu.org
2011-10-14 14:40 ` [Bug target/48126] arm_output_sync_loop: misplaced memory barrier rsandifo at gcc dot gnu.org
2011-10-14 14:46 ` rsandifo at gcc dot gnu.org
2012-06-08  6:59 ` jye2 at gcc dot gnu.org
2012-06-19  3:01 ` jye2 at gcc dot gnu.org

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=bug-48126-4-12Rkq1JAvo@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.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 read-only IMAP folder(s) and NNTP newsgroup(s).