linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
[parent not found: <200609082230.22225.oliver@neukum.org>]
[parent not found: <200609081929.33027.oliver@neukum.org>]
* Uses for memory barriers
@ 2006-09-07 21:25 Alan Stern
  2006-09-07 22:10 ` linux-os (Dick Johnson)
                   ` (2 more replies)
  0 siblings, 3 replies; 92+ messages in thread
From: Alan Stern @ 2006-09-07 21:25 UTC (permalink / raw)
  To: Paul E. McKenney; +Cc: David Howells, Kernel development list

Paul:

Here's something I had been thinking about back in July but never got 
around to discussing:  Under what circumstances would one ever want to use 
"mb()" rather than "rmb()" or "wmb()"?

The canonical application for memory barriers is where one CPU writes two 
locations and another reads them, to make certain that the ordering is 
preserved (assume everything is initially equal to 0):

	CPU 0			CPU 1
	-----			-----
	a = 1;			y = b;
	wmb();			rmb();
	b = 1;			x = a;
				assert(x==1 || y==0);

In this situation the first CPU only needs wmb() and the second only needs 
rmb().  So when would we need a full mb()?...

The obvious extension of the canonical example is to have CPU 0 write
one location and read another, while CPU 1 reads and writes the same
locations.  Example:

	CPU 0			CPU 1
	-----			-----
	while (y==0) relax();	y = -1;
	a = 1;			b = 1;
	mb();			mb();
	y = b;			x = a;
				while (y < 0) relax();
				assert(x==1 || y==1);	//???

Apart from the extra stuff needed to make sure that CPU 1 sees the proper
value stored in y by CPU 0, this is just like the first example except for
the pattern of reads and writes.  Naively one would think that if the
first half of the assertion fails, so x==0, then CPU 1 must have completed
all of the first four lines above by the time CPU 0 completed its mb().  
Hence the assignment to b would have to be visible to CPU 0, since the
read of b occurs after the mb().  But of course we know that naive 
reasoning isn't always right when it comes to the operation of memory 
caches.

The opposite approach would use reads followed by writes:

	CPU 0			CPU 1
	-----			-----
	while (x==0) relax();	x = -1;
	x = a;			y = b;
	mb();			mb();
	b = 1;			a = 1;
				while (x < 0) relax();
				assert(x==0 || y==0);	//???

Similar reasoning can be applied here.  However IIRC, you decided that
neither of these assertions is actually guaranteed to hold.  If that's the
case, then it looks like mb() is useless for coordinating two CPUs.

Am I correct?  Or are there some easily-explained situations where mb()  
really should be used for inter-CPU synchronization?

Alan


^ permalink raw reply	[flat|nested] 92+ messages in thread

end of thread, other threads:[~2006-10-24 17:51 UTC | newest]

Thread overview: 92+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20060911190005.GA1295@us.ibm.com>
2006-09-12 18:08 ` Uses for memory barriers Alan Stern
2006-09-12 20:23   ` Paul E. McKenney
2006-09-14 14:58     ` Alan Stern
2006-09-15  5:16       ` Paul E. McKenney
2006-09-15 19:48         ` Alan Stern
2006-09-16  4:19           ` Paul E. McKenney
2006-09-16 15:28             ` Alan Stern
2006-09-18 19:13               ` Paul E. McKenney
2006-09-18 20:13                 ` Alan Stern
2006-09-19  0:47                   ` Paul E. McKenney
2006-09-19 16:04                     ` Alan Stern
2006-09-19 16:38                       ` Nick Piggin
2006-09-19 17:40                         ` Alan Stern
2006-09-19 17:51                           ` Nick Piggin
2006-09-19 18:19                             ` Paul E. McKenney
2006-09-19 18:48                               ` Nick Piggin
2006-09-19 19:36                                 ` Paul E. McKenney
2006-09-19 19:48                                   ` Nick Piggin
2006-09-19 20:01                                     ` Paul E. McKenney
2006-09-19 20:38                                       ` Alan Stern
2006-09-21  1:43                                         ` Paul E. McKenney
2006-09-19 18:16                       ` Paul E. McKenney
2006-09-20 19:39                         ` Alan Stern
2006-09-21  1:34                           ` Paul E. McKenney
2006-09-21 20:59                             ` Alan Stern
2006-09-22  5:02                               ` Paul E. McKenney
2006-09-22 20:38                                 ` Alan Stern
2006-09-27 21:06                                 ` Alan Stern
2006-09-30  1:11                                   ` Paul E. McKenney
2006-09-30 21:01                                     ` Alan Stern
2006-10-02  0:06                                       ` Paul E. McKenney
2006-10-02 15:44                                         ` Alan Stern
2006-10-04 15:35                                           ` Paul E. McKenney
2006-10-04 18:04                                             ` Alan Stern
2006-10-13 16:51                                               ` Paul E. McKenney
2006-10-13 18:30                                                 ` Alan Stern
2006-10-13 22:39                                                   ` Paul E. McKenney
2006-10-14  2:27                                                     ` Alan Stern
2006-10-17  1:24                                                       ` Paul E. McKenney
2006-10-17 15:29                                                         ` Alan Stern
2006-10-17 17:27                                                           ` Paul E. McKenney
2006-10-17 19:42                                                             ` Alan Stern
2006-10-17 20:15                                                               ` Paul E. McKenney
2006-10-17 21:21                                                                 ` Alan Stern
2006-10-17 22:58                                                                   ` Paul E. McKenney
2006-10-18 19:05                                                                     ` Alan Stern
2006-10-18 23:01                                                                       ` Paul E. McKenney
2006-10-19 16:44                                                                         ` Alan Stern
2006-10-19 19:21                                                                           ` Paul E. McKenney
2006-10-19 20:55                                                                             ` Alan Stern
2006-10-19 22:46                                                                               ` Paul E. McKenney
2006-10-20 16:54                                                                                 ` Alan Stern
2006-10-21  0:59                                                                                   ` Paul E. McKenney
2006-10-21 19:47                                                                                     ` Alan Stern
2006-10-21 22:52                                                                                       ` Paul E. McKenney
2006-10-22  2:18                                                                                         ` Alan Stern
2006-10-23  5:32                                                                                           ` Paul E. McKenney
2006-10-23 14:07                                                                                             ` Alan Stern
2006-10-24 17:52                                                                                               ` Paul E. McKenney
     [not found] <200609082230.22225.oliver@neukum.org>
2006-09-08 21:26 ` Alan Stern
2006-09-08 21:46   ` Oliver Neukum
2006-09-08 22:25     ` Alan Stern
2006-09-08 22:49       ` Oliver Neukum
2006-09-09  2:25         ` Alan Stern
2006-09-11 16:21           ` Paul E. McKenney
2006-09-11 16:50             ` Alan Stern
2006-09-11 17:23               ` Segher Boessenkool
2006-09-11 19:04                 ` Paul E. McKenney
2006-09-11 19:03               ` Paul E. McKenney
2006-09-11 17:21             ` Segher Boessenkool
2006-09-11 19:48             ` Oliver Neukum
2006-09-11 20:29               ` Paul E. McKenney
2006-09-12  9:01             ` David Howells
2006-09-12 10:22               ` Oliver Neukum
2006-09-12 14:55                 ` Paul E. McKenney
2006-09-12 15:07                   ` Oliver Neukum
2006-09-12 16:12                     ` Paul E. McKenney
2006-09-12 17:50                     ` Segher Boessenkool
2006-09-12 14:42               ` Paul E. McKenney
2006-09-12  8:57           ` David Howells
     [not found] <200609081929.33027.oliver@neukum.org>
2006-09-08 18:06 ` Alan Stern
2006-09-08 18:22   ` Oliver Neukum
2006-09-07 21:25 Alan Stern
2006-09-07 22:10 ` linux-os (Dick Johnson)
2006-09-08 18:39   ` Alan Stern
2006-09-08  0:14 ` Paul E. McKenney
2006-09-08 15:55   ` Alan Stern
2006-09-08 18:57     ` Paul E. McKenney
2006-09-08 21:23       ` Alan Stern
2006-09-09  0:44         ` Paul E. McKenney
2006-09-11 16:05           ` Alan Stern
2006-09-08  5:52 ` David Schwartz

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).