tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

membar_datadep_consumer



Any objections to introducing membar_datadep_consumer?

Semantics:  Any load afterward with a data dependency on a load
beforehand is guaranteed to be ordered so.  Control dependencies are
not ordered.

Implementation:  mb on Alpha, noop everywhere else for now.  Define
__HAVE_MEMBAR_DATADEP_CONSUMER in XYZ/types.h if XYZ has it; otherwise
<sys/atomic.h> automatically defines it as a noop.

I have been sitting on the attached patch for ages and plan to
introduce its use where appropriate when I have time, with testing on
a multiprocessor Alpha known to reorder data-dependent loads.
Index: common/lib/libc/arch/alpha/atomic/membar_ops.S
===================================================================
RCS file: /cvsroot/src/common/lib/libc/arch/alpha/atomic/membar_ops.S,v
retrieving revision 1.6
diff -p -u -r1.6 membar_ops.S
--- common/lib/libc/arch/alpha/atomic/membar_ops.S	25 May 2008 15:56:11 -0000	1.6
+++ common/lib/libc/arch/alpha/atomic/membar_ops.S	7 Jan 2015 18:56:50 -0000
@@ -87,3 +87,5 @@ ATOMIC_OP_ALIAS(membar_exit,_membar_sync
 STRONG_ALIAS(_membar_exit,_membar_sync)
 ATOMIC_OP_ALIAS(membar_consumer,_membar_sync)
 STRONG_ALIAS(_membar_consumer,_membar_sync)
+ATOMIC_OP_ALIAS(membar_datadep_consumer,_membar_sync)
+STRONG_ALIAS(_membar_datadep_consumer,_membar_sync)
Index: lib/libc/atomic/membar_ops.3
===================================================================
RCS file: /cvsroot/src/lib/libc/atomic/membar_ops.3,v
retrieving revision 1.3
diff -p -u -r1.3 membar_ops.3
--- lib/libc/atomic/membar_ops.3	28 Apr 2011 11:56:26 -0000	1.3
+++ lib/libc/atomic/membar_ops.3	7 Jan 2015 18:56:56 -0000
@@ -27,7 +27,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd February 11, 2007
+.Dd November 20, 2014
 .Dt MEMBAR_OPS 3
 .Os
 .Sh NAME
@@ -52,6 +52,8 @@
 .Ft void
 .Fn membar_consumer "void"
 .Ft void
+.Fn membar_datadep_consumer "void"
+.Ft void
 .Fn membar_sync "void"
 .Sh DESCRIPTION
 The
@@ -83,6 +85,40 @@ before any stores after the memory barri
 .It Fn membar_consumer
 All loads preceding the memory barrier will complete before any loads
 after the memory barrier complete.
+.It Fn membar_datadep_consumer
+Same as
+.Fn membar_consumer ,
+but limited to loads of addresses dependent on prior loads, or
+.Sq data-dependent
+loads:
+.Bd -literal -offset indent
+int **pp, *p, v;
+
+p = *pp;
+membar_datadep_consumer();
+v = *p;
+consume(v);
+.Ed
+.Pp
+Does not guarantee ordering of loads in branches, or
+.Sq control-dependent
+loads -- you must use
+.Fn membar_consumer
+instead:
+.Bd -literal -offset indent
+int *ok, *p, v;
+
+if (*ok) {
+	membar_consumer();
+	v = *p;
+	consume(v);
+}
+.Ed
+.Pp
+Most CPUs do not reorder data-dependent loads (i.e., most CPUs
+guarantee that cached values are not stale in that case), so
+.Fn membar_datadep_consumer
+is a no-op on those CPUs.
 .It Fn membar_sync
 All loads and stores preceding the memory barrier will complete and
 reach global visibility before any loads and stores after the memory
@@ -95,3 +131,7 @@ The
 .Nm membar_ops
 functions first appeared in
 .Nx 5.0 .
+The data-dependent load barrier,
+.Fn membar_datadep_consumer ,
+first appeared in
+.Nx 7.0 .
Index: sys/arch/alpha/include/types.h
===================================================================
RCS file: /cvsroot/src/sys/arch/alpha/include/types.h,v
retrieving revision 1.49
diff -p -u -r1.49 types.h
--- sys/arch/alpha/include/types.h	25 Jan 2012 18:09:13 -0000	1.49
+++ sys/arch/alpha/include/types.h	7 Jan 2015 18:56:56 -0000
@@ -70,6 +70,7 @@ typedef	volatile int		__cpu_simple_lock_
 
 #define	__HAVE_NEW_STYLE_BUS_H
 #define	__HAVE_ATOMIC_OPERATIONS
+#define	__HAVE_MEMBAR_DATADEP_CONSUMER
 #define	__HAVE_CPU_COUNTER
 #define	__HAVE_SYSCALL_INTERN
 #define	__HAVE_MINIMAL_EMUL
Index: sys/sys/atomic.h
===================================================================
RCS file: /cvsroot/src/sys/sys/atomic.h,v
retrieving revision 1.12
diff -p -u -r1.12 atomic.h
--- sys/sys/atomic.h	21 Feb 2014 15:52:53 -0000	1.12
+++ sys/sys/atomic.h	7 Jan 2015 18:56:57 -0000
@@ -156,6 +156,12 @@ void		membar_producer(void);
 void		membar_consumer(void);
 void		membar_sync(void);
 
+#ifdef	__HAVE_MEMBAR_DATADEP_CONSUMER
+void		membar_datadep_consumer(void);
+#else
+#define	membar_datadep_consumer()	((void)0)
+#endif
+
 __END_DECLS
 
 #endif /* ! _SYS_ATOMIC_H_ */


Home | Main Index | Thread Index | Old Index