linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Miklos Szeredi <miklos@szeredi.hu>
Cc: James.Bottomley@HansenPartnership.com, benh@kernel.crashing.org,
	dhowells@redhat.com, linux-kernel@vger.kernel.org,
	linux-arch@vger.kernel.org
Subject: Re: memory barrier question
Date: Mon, 20 Sep 2010 11:25:04 -0700	[thread overview]
Message-ID: <20100920182504.GD2408@linux.vnet.ibm.com> (raw)
In-Reply-To: <E1OxioY-0006nd-M9@pomaz-ex.szeredi.hu>

On Mon, Sep 20, 2010 at 06:01:58PM +0200, Miklos Szeredi wrote:
> On Sun, 19 Sep 2010, Paul E. McKenney wrote:
> > > > Again, please put at least an ACCESS_ONCE() in.  Trivial to do now,
> > > > possibly saving much pain and headache later on.
> > > 
> > > OK, lost you here.  ACCESS_ONCE() is only needed in certain situations
> > > (like list traversal) because some compilers can reload cached values
> > > across an explicit barrier (which isn't here).
> > 
> > ACCESS_ONCE() also tells the compiler not to try to guess.
> 
> If the code is written like this:
> 
> 	if (ACCESS_ONCE(dentry->d_inode)) {
> 		blah = dentry->d_inode->i_some_field
> 		...
> 	}
> 
> does the compiler guarantee anything or does it need a full compiler
> barrier to prevent reordering?

From what I understand, this could do strange things.  The compiler
is forced to access dentry->d_inode for the "if" check, but would be
free to use some previously fetched value for the assignment to "blah".
Unless of course this code was under a lock that prevented any
change to dentry->d_inode.

If the code is to execute in a lockless manner, I would instead suggest
something like the following:

	p = ACCESS_ONCE(dentry->d_inode);
	if (p) {
		blah = p->i_some_field
		...
	}

This would force the compiler to actually fetch dentry->d_inode
and only then dereference it.

This would -not- constrain the CPU in any way, but the only CPU that
I know of that misbehaves in this case is DEC Alpha.

So my version of the above code would do what you expect on most CPUs,
but really could fail on DEC Alpha.  If you don't believe me, please feel
free to take a look at http://www.openvms.compaq.com/wizard/wiz_2637.html.

But do we really care about Alpha anymore?  (I can see it now... The
Alpha portion of the kernel tree moves to staging...)

> Because that pattern is, again, pretty much all over the place.  Yeah
> it can be rewritten but that's not always feasable since it's
> difficult to audit, would possibly need extra function arguments,
> etc...

Again, the pattern is OK if you are preventing the pointer from changing.

							Thanx, Paul

  reply	other threads:[~2010-09-20 18:25 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-15 14:36 memory barrier question Miklos Szeredi
2010-09-15 14:36 ` Miklos Szeredi
2010-09-15 19:12 ` Rafael J. Wysocki
2010-09-16 11:55 ` David Howells
2010-09-16 13:42   ` Miklos Szeredi
2010-09-16 13:42     ` Miklos Szeredi
2010-09-16 14:30     ` David Howells
2010-09-16 15:03       ` Paul E. McKenney
2010-09-16 15:03         ` Paul E. McKenney
2010-09-16 16:06         ` Miklos Szeredi
2010-09-16 16:06           ` Miklos Szeredi
2010-09-16 16:37           ` Paul E. McKenney
2010-09-16 16:56             ` Miklos Szeredi
2010-09-16 17:09               ` James Bottomley
2010-09-16 17:17                 ` Miklos Szeredi
2010-09-16 17:40                   ` James Bottomley
2010-09-17 21:49                   ` Benjamin Herrenschmidt
2010-09-17 23:12                     ` Paul E. McKenney
2010-09-19  2:47                       ` Benjamin Herrenschmidt
2010-09-19 15:26                         ` Paul E. McKenney
2010-09-19 20:15                           ` Miklos Szeredi
2010-09-19 21:59                             ` Paul E. McKenney
2010-09-20  0:58                               ` James Bottomley
2010-09-20  1:29                                 ` Paul E. McKenney
2010-09-20 16:01                                   ` Miklos Szeredi
2010-09-20 18:25                                     ` Paul E. McKenney [this message]
2010-09-20 18:57                                       ` Paul E. McKenney
2010-09-20 20:26                                       ` Michael Cree
2010-09-20 20:40                                         ` Paul E. McKenney
2010-09-21 14:59                                           ` Paul E. McKenney
2010-09-22 18:41                                             ` Paul E. McKenney
2010-09-18  1:12                     ` Alan Cox
2010-09-16 16:50           ` Jamie Lokier
2010-09-16 16:18   ` Peter Zijlstra
2010-09-16 17:59     ` David Howells
  -- strict thread matches above, loose matches on Subject: below --
2010-09-20 10:34 George Spelvin
2010-09-20 10:34 ` George Spelvin

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=20100920182504.GD2408@linux.vnet.ibm.com \
    --to=paulmck@linux.vnet.ibm.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=benh@kernel.crashing.org \
    --cc=dhowells@redhat.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    /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).