linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: Miklos Szeredi <miklos@szeredi.hu>,
	"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Cc: dhowells@redhat.com, linux-kernel@vger.kernel.org,
	linux-arch@vger.kernel.org
Subject: Re: memory barrier question
Date: Thu, 16 Sep 2010 12:55:36 +0100	[thread overview]
Message-ID: <3777.1284638136@redhat.com> (raw)
In-Reply-To: <E1Ovt6F-0005CS-RU@pomaz-ex.szeredi.hu>

Miklos Szeredi <miklos@szeredi.hu> wrote:

> Consider the following example:
> 
> Start:
> 	p = NULL;
> 	x = 0;
> 
> CPU1:
> 	atomic_inc(&x);
> 	p = &x;
> 
> CPU2:
> 	if (p)
> 		z = atomic_read(p);
> 
> Is it possible to end up with z == 0?

I think so.  I'm not sure that you can assume that CPU1 does its two
'operations' in the same order.  You can guarantee that the read of x,
increment, and write of x will be done in an order, and that no one else will
see an intermediate state, but you can't guarantee that CPU2 will see x
changed before p is changed.

In Documentation/memory-barriers.txt, it says:

	The following also do _not_ imply memory barriers, and so may require
	explicit memory barriers under some circumstances
	(smp_mb__before_atomic_dec() for instance):

		atomic_add();
		atomic_sub();
		atomic_inc();
		atomic_dec();

so you need _two_ memory barriers, e.g.:

	CPU1:
		atomic_inc(&x);
		smp_mb__after_atomic_inc()
		p = &x;

	CPU2:
		q = p;
		smp_rmb();
		if (q)
			z = atomic_read(q);

Note that atomic_inc() may imply a suitable memory barrier on some arches, and
so has special variant barrier functions of its own.

> What if there's a lock/unlock before setting "p"?

If there's a lock+unlock between, then this counts as a full memory barrier:

	CPU1:
		atomic_inc(&x);
		spin_lock(&foo);
		spin_unlock(&foo);
		p = &x;

but you still need the matching smp_rmb() on CPU2.

> What if there's a write barrier before setting "p"?

That's fine, but you still need the matching smp_rmb() on CPU2.

David

  parent reply	other threads:[~2010-09-16 11:55 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 [this message]
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
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=3777.1284638136@redhat.com \
    --to=dhowells@redhat.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    --cc=paulmck@linux.vnet.ibm.com \
    /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).