Skip to contentSkip to conversation

barriers: introduce smp_mb__release_acquire and update documentation

PATCH Superseded Will Deacon 4 participants 19 messages Sep 15, 2015 – Sep 22, 2015

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 barriers: introduce smp_mb__release_acquire and update documentation

As much as we'd like to live in a world where RELEASE -> ACQUIRE is always cheaply ordered and can be used to construct UNLOCK -> LOCK definitions with similar guarantees, the grim reality is that this isn't even possible on x86 (thanks to Paul for bringing us crashing down to Earth).

This patch handles the issue by introducing a new barrier macro, smp_mb__release_acquire, that can be placed between a RELEASE and a subsequent ACQUIRE operation in order to upgrade them to a full memory barrier. At the moment, it doesn't have any users, so its existence serves mainly as a documentation aid.

Documentation/memory-barriers.txt is updated to describe more clearly the ACQUIRE and RELEASE ordering in this area and to show an example of the new barrier in action.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>

---

Following our discussion at [1], I thought I'd try to write something down...

[1] http://lkml.kernel.org/r/[email protected]

 Documentation/memory-barriers.txt  | 23 ++++++++++++++++++++++-
 arch/powerpc/include/asm/barrier.h |  1 +
 arch/x86/include/asm/barrier.h     |  2 ++
 include/asm-generic/barrier.h      |  4 ++++
 4 files changed, 29 insertions(+), 1 deletion(-)
Documentation/memory-barriers.txt +22 -1
index 2ba8461b0631..46a85abb77c6 100644
@@ -459,11 +459,18 @@ And a couple of implicit varieties:
      RELEASE on that same variable are guaranteed to be visible.  In other
      words, within a given variable's critical section, all accesses of all
      previous critical sections for that variable are guaranteed to have
-     completed.
+     completed.  If the RELEASE and ACQUIRE operations act on independent
+     variables, an smp_mb__release_acquire() barrier can be placed between
+     them to upgrade the sequence to a full barrier.
 
      This means that ACQUIRE acts as a minimal "acquire" operation and
      RELEASE acts as a minimal "release" operation.
 
+A subset of the atomic operations described in atomic_ops.txt have ACQUIRE
+and RELEASE variants in addition to fully-ordered and relaxed definitions.
+For compound atomics performing both a load and a store, ACQUIRE semantics
+apply only to the load and RELEASE semantics only to the store portion of
+the operation.
 
 Memory barriers are only required where there's a possibility of interaction
 between two CPUs or between a CPU and a device.  If it can be guaranteed that
@@ -1895,6 +1902,20 @@ the RELEASE would simply complete, thereby avoiding the deadlock.
 	a sleep-unlock race, but the locking primitive needs to resolve
 	such races properly in any case.
 
+If necessary, ordering can be enforced by use of an
+smp_mb__release_acquire() barrier:
+
+	*A = a;
+	RELEASE M
+	smp_mb__release_acquire();
+	ACQUIRE N
+	*B = b;
+
+in which case, the only permitted sequences are:
+
+	STORE *A, RELEASE M, ACQUIRE N, STORE *B
+	STORE *A, ACQUIRE N, RELEASE M, STORE *B
+
 Locks and semaphores may not provide any guarantee of ordering on UP compiled
 systems, and so cannot be counted on in such a situation to actually achieve
 anything at all - especially with respect to I/O accesses - unless combined
arch/powerpc/include/asm/barrier.h +1 -0
index 0eca6efc0631..919624634d0a 100644
@@ -87,6 +87,7 @@ do {									\
 	___p1;								\
 })
 
+#define smp_mb__release_acquire()   smp_mb()
 #define smp_mb__before_atomic()     smp_mb()
 #define smp_mb__after_atomic()      smp_mb()
 #define smp_mb__before_spinlock()   smp_mb()
arch/x86/include/asm/barrier.h +2 -0
index 0681d2532527..1c61ad251e0e 100644
@@ -85,6 +85,8 @@ do {									\
 	___p1;								\
 })
 
+#define smp_mb__release_acquire()	smp_mb()
+
 #endif
 
 /* Atomic operations are already serializing on x86 */
include/asm-generic/barrier.h +4 -0
index b42afada1280..61ae95199397 100644
@@ -119,5 +119,9 @@ do {									\
 	___p1;								\
 })
 
+#ifndef smp_mb__release_acquire
+#define smp_mb__release_acquire()	do { } while (0)
+#endif
+
 #endif /* !__ASSEMBLY__ */
 #endif /* __ASM_GENERIC_BARRIER_H */
Signature · 1 line
2.1.4
#1 lore raw
Quoted 5 lines · Will Deacon

As much as we'd like to live in a world where RELEASE -> ACQUIRE is always cheaply ordered and can be used to construct UNLOCK -> LOCK definitions with similar guarantees, the grim reality is that this isn't even possible on x86 (thanks to Paul for bringing us crashing down to Earth).

"It is a service that I provide." ;-)

Quoted 6 lines

This patch handles the issue by introducing a new barrier macro, smp_mb__release_acquire, that can be placed between a RELEASE and a subsequent ACQUIRE operation in order to upgrade them to a full memory barrier. At the moment, it doesn't have any users, so its existence serves mainly as a documentation aid.

Quoted 7 lines

Documentation/memory-barriers.txt is updated to describe more clearly the ACQUIRE and RELEASE ordering in this area and to show an example of the new barrier in action.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>

Some questions and comments below.

Thanx, Paul

Quoted 63 lines

---

Following our discussion at [1], I thought I'd try to write something down...

[1] http://lkml.kernel.org/r/[email protected]

 Documentation/memory-barriers.txt  | 23 ++++++++++++++++++++++-
 arch/powerpc/include/asm/barrier.h |  1 +
 arch/x86/include/asm/barrier.h     |  2 ++
 include/asm-generic/barrier.h      |  4 ++++
 4 files changed, 29 insertions(+), 1 deletion(-)
Documentation/memory-barriers.txt +22 -1
index 2ba8461b0631..46a85abb77c6 100644
@@ -459,11 +459,18 @@ And a couple of implicit varieties:
      RELEASE on that same variable are guaranteed to be visible.  In other
      words, within a given variable's critical section, all accesses of all
      previous critical sections for that variable are guaranteed to have
-     completed.
+     completed.  If the RELEASE and ACQUIRE operations act on independent
+     variables, an smp_mb__release_acquire() barrier can be placed between
+     them to upgrade the sequence to a full barrier.
 
      This means that ACQUIRE acts as a minimal "acquire" operation and
      RELEASE acts as a minimal "release" operation.
 
+A subset of the atomic operations described in atomic_ops.txt have ACQUIRE
+and RELEASE variants in addition to fully-ordered and relaxed definitions.
+For compound atomics performing both a load and a store, ACQUIRE semantics
+apply only to the load and RELEASE semantics only to the store portion of
+the operation.
 
 Memory barriers are only required where there's a possibility of interaction
 between two CPUs or between a CPU and a device.  If it can be guaranteed that
@@ -1895,6 +1902,20 @@ the RELEASE would simply complete, thereby avoiding the deadlock.
 	a sleep-unlock race, but the locking primitive needs to resolve
 	such races properly in any case.
 
+If necessary, ordering can be enforced by use of an
+smp_mb__release_acquire() barrier:
+
+	*A = a;
+	RELEASE M
+	smp_mb__release_acquire();
+	ACQUIRE N
+	*B = b;
+
+in which case, the only permitted sequences are:
+
+	STORE *A, RELEASE M, ACQUIRE N, STORE *B
+	STORE *A, ACQUIRE N, RELEASE M, STORE *B
+
 Locks and semaphores may not provide any guarantee of ordering on UP compiled
 systems, and so cannot be counted on in such a situation to actually achieve
 anything at all - especially with respect to I/O accesses - unless combined
arch/powerpc/include/asm/barrier.h +0 -0
index 0eca6efc0631..919624634d0a 100644

arch/powerpc/include/asm/barrier.h do { \ · line 90

Quoted 4 lines
arch/powerpc/include/asm/barrier.h +1 -0
@@ -87,6 +87,7 @@ do {									\
 	___p1;								\
 })
 
+#define smp_mb__release_acquire()   smp_mb()

If we are handling locking the same as atomic acquire and release operations, this could also be placed between the unlock and the lock.

However, independently of the unlock/lock case, this definition and use of smp_mb__release_acquire() does not handle full ordering of a release by one CPU and an acquire of that same variable by another. In that case, we need roughly the same setup as the much-maligned smp_mb__after_unlock_lock(). So, do we care about this case? (RCU does, though not 100% sure about any other subsystems.)

Quoted 21 lines
arch/powerpc/include/asm/barrier.h +0 -0
 #define smp_mb__before_atomic()     smp_mb()
 #define smp_mb__after_atomic()      smp_mb()
 #define smp_mb__before_spinlock()   smp_mb()
arch/x86/include/asm/barrier.h +2 -0
index 0681d2532527..1c61ad251e0e 100644
@@ -85,6 +85,8 @@ do {									\
 	___p1;								\
 })
 
+#define smp_mb__release_acquire()	smp_mb()
+
 #endif
 
 /* Atomic operations are already serializing on x86 */
include/asm-generic/barrier.h +0 -0
index b42afada1280..61ae95199397 100644

include/asm-generic/barrier.h do { \ · line 123

Quoted 5 lines
include/asm-generic/barrier.h +2 -0
@@ -119,5 +119,9 @@ do {									\
 	___p1;								\
 })
 
+#ifndef smp_mb__release_acquire
+#define smp_mb__release_acquire()	do { } while (0)

Doesn't this need to be barrier() in the case where one variable was released and another was acquired?

Quoted 7 lines
include/asm-generic/barrier.h +2 -0
+#endif
+
 #endif /* !__ASSEMBLY__ */
 #endif /* __ASM_GENERIC_BARRIER_H */
Signature · 1 line
2.1.4
maintainer
#2 lore raw
Quoted 8 lines · Paul E. McKenney
Quoted 8 lines
arch/powerpc/include/asm/barrier.h +0 -0
index 0eca6efc0631..919624634d0a 100644
@@ -87,6 +87,7 @@ do {									\
 	___p1;								\
 })
 
Quoted 4 lines

arch/powerpc/include/asm/barrier.h do { \ · line 90

Quoted 1 line
arch/powerpc/include/asm/barrier.h +1 -0
+#define smp_mb__release_acquire()   smp_mb()

If we are handling locking the same as atomic acquire and release operations, this could also be placed between the unlock and the lock.

I think the point was exactly that we need to separate LOCK/UNLOCK from ACQUIRE/RELEASE.

Quoted 7 lines

However, independently of the unlock/lock case, this definition and use of smp_mb__release_acquire() does not handle full ordering of a release by one CPU and an acquire of that same variable by another.

In that case, we need roughly the same setup as the much-maligned smp_mb__after_unlock_lock(). So, do we care about this case? (RCU does, though not 100% sure about any other subsystems.)

Indeed, that is a hole in the definition, that I think we should close.

Quoted 11 lines
Quoted 11 lines
arch/powerpc/include/asm/barrier.h +0 -0
 #define smp_mb__before_atomic()     smp_mb()
 #define smp_mb__after_atomic()      smp_mb()
 #define smp_mb__before_spinlock()   smp_mb()
arch/x86/include/asm/barrier.h +0 -0
index 0681d2532527..1c61ad251e0e 100644
@@ -85,6 +85,8 @@ do {									\
 	___p1;								\
 })
 
Quoted 4 lines

arch/x86/include/asm/barrier.h do { \ · line 91

Quoted 4 lines
arch/x86/include/asm/barrier.h +2 -0
+#define smp_mb__release_acquire()	smp_mb()
+
 #endif
 

All TSO archs would want this.

Quoted 9 lines
Quoted 9 lines
arch/x86/include/asm/barrier.h +0 -0
 /* Atomic operations are already serializing on x86 */
include/asm-generic/barrier.h +0 -0
index b42afada1280..61ae95199397 100644
@@ -119,5 +119,9 @@ do {									\
 	___p1;								\
 })
 
Quoted 5 lines

include/asm-generic/barrier.h do { \ · line 123

Quoted 2 lines
include/asm-generic/barrier.h +2 -0
+#ifndef smp_mb__release_acquire
+#define smp_mb__release_acquire()	do { } while (0)

Doesn't this need to be barrier() in the case where one variable was released and another was acquired?

Yes, I think its very prudent to never let any barrier degrade to less than barrier().

#3 lore raw

Hi Paul, Peter,

Thanks for the comments. More below...

Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Paul E. McKenney
Quoted 8 lines
arch/powerpc/include/asm/barrier.h +0 -0
index 0eca6efc0631..919624634d0a 100644
@@ -87,6 +87,7 @@ do {									\
 	___p1;								\
 })
 
Quoted 7 lines
Quoted 4 lines

arch/powerpc/include/asm/barrier.h do { \ · line 90

Quoted 1 line
arch/powerpc/include/asm/barrier.h +1 -0
+#define smp_mb__release_acquire()   smp_mb()

If we are handling locking the same as atomic acquire and release operations, this could also be placed between the unlock and the lock.

I think the point was exactly that we need to separate LOCK/UNLOCK from ACQUIRE/RELEASE.

Yes, pending the PPC investigation, I'd like to keep this separate for now.

Quoted 9 lines
Quoted 7 lines

However, independently of the unlock/lock case, this definition and use of smp_mb__release_acquire() does not handle full ordering of a release by one CPU and an acquire of that same variable by another.

In that case, we need roughly the same setup as the much-maligned smp_mb__after_unlock_lock(). So, do we care about this case? (RCU does, though not 100% sure about any other subsystems.)

Indeed, that is a hole in the definition, that I think we should close.

I'm struggling to understand the hole, but here's my intuition. If an ACQUIRE on CPUx reads from a RELEASE by CPUy, then I'd expect CPUx to observe all memory accessed performed by CPUy prior to the RELEASE before it observes the RELEASE itself, regardless of this new barrier. I think this matches what we currently have in memory-barriers.txt (i.e. acquire/release are neither transitive or multi-copy atomic).

Do we have use-cases that need these extra guarantees (outside of the single RCU case, which is using smp_mb__after_unlock_lock)? I'd rather not augment smp_mb__release_acquire unless we really have to, so I'd prefer to document that it only applies when the RELEASE and ACQUIRE are performed by the same CPU. Thoughts?

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
arch/powerpc/include/asm/barrier.h +0 -0
 #define smp_mb__before_atomic()     smp_mb()
 #define smp_mb__after_atomic()      smp_mb()
 #define smp_mb__before_spinlock()   smp_mb()
arch/x86/include/asm/barrier.h +0 -0
index 0681d2532527..1c61ad251e0e 100644
@@ -85,6 +85,8 @@ do {									\
 	___p1;								\
 })
 
Quoted 6 lines
Quoted 4 lines

arch/x86/include/asm/barrier.h do { \ · line 91

Quoted 4 lines
arch/x86/include/asm/barrier.h +2 -0
+#define smp_mb__release_acquire()	smp_mb()
+
 #endif
 

All TSO archs would want this.

If we look at all architectures that implement smp_store_release without an smp_mb already, we get:

ia64
powerpc
s390
sparc
x86

so it should be enough to provide those with definitions. I'll do that once we've settled on the documentation bits.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
arch/x86/include/asm/barrier.h +0 -0
 /* Atomic operations are already serializing on x86 */
include/asm-generic/barrier.h +0 -0
index b42afada1280..61ae95199397 100644
@@ -119,5 +119,9 @@ do {									\
 	___p1;								\
 })
 
Quoted 8 lines
Quoted 5 lines

include/asm-generic/barrier.h do { \ · line 123

Quoted 2 lines
include/asm-generic/barrier.h +2 -0
+#ifndef smp_mb__release_acquire
+#define smp_mb__release_acquire()	do { } while (0)

Doesn't this need to be barrier() in the case where one variable was released and another was acquired?

Yes, I think its very prudent to never let any barrier degrade to less than barrier().

Hey, I just copied read_barrier_depends from the same file! Both smp_load_acquire and smp_store_release should already provide at least barrier(), so the above should be sufficient.

Will

maintainer
#4 lore raw
Quoted 8 lines · Will Deacon
Quoted 1 line

Indeed, that is a hole in the definition, that I think we should close.

I'm struggling to understand the hole, but here's my intuition. If an ACQUIRE on CPUx reads from a RELEASE by CPUy, then I'd expect CPUx to observe all memory accessed performed by CPUy prior to the RELEASE before it observes the RELEASE itself, regardless of this new barrier. I think this matches what we currently have in memory-barriers.txt (i.e. acquire/release are neither transitive or multi-copy atomic).

Ah agreed. I seem to have gotten my brain in a tangle.

Basically where a program order release+acquire relies on an address dependency, a cross cpu release+acquire relies on causality. If we observe the release, we must also observe everything prior to it etc.

#5 lore raw
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Will Deacon
Quoted 1 line

Indeed, that is a hole in the definition, that I think we should close.

I'm struggling to understand the hole, but here's my intuition. If an ACQUIRE on CPUx reads from a RELEASE by CPUy, then I'd expect CPUx to observe all memory accessed performed by CPUy prior to the RELEASE before it observes the RELEASE itself, regardless of this new barrier. I think this matches what we currently have in memory-barriers.txt (i.e. acquire/release are neither transitive or multi-copy atomic).

Quoted 5 lines

Ah agreed. I seem to have gotten my brain in a tangle.

Basically where a program order release+acquire relies on an address dependency, a cross cpu release+acquire relies on causality. If we observe the release, we must also observe everything prior to it etc.

Yes, and crucially, the "everything prior to it" only encompasses accesses made by the releasing CPU itself (in the absence of other barriers and synchronisation).

Given that we managed to get confused, it doesn't hurt to call this out explicitly in the doc, so I can add the following extra text.

Will

--->8

Documentation/memory-barriers.txt +6 -2
index 46a85abb77c6..794d102d06df 100644
@@ -1902,8 +1902,8 @@ the RELEASE would simply complete, thereby avoiding the deadlock.
 	a sleep-unlock race, but the locking primitive needs to resolve
 	such races properly in any case.
 
-If necessary, ordering can be enforced by use of an
-smp_mb__release_acquire() barrier:
+Where the RELEASE and ACQUIRE operations are performed by the same CPU,
+ordering can be enforced by use of an smp_mb__release_acquire() barrier:
 
 	*A = a;
 	RELEASE M
@@ -1916,6 +1916,10 @@ in which case, the only permitted sequences are:
 	STORE *A, RELEASE M, ACQUIRE N, STORE *B
 	STORE *A, ACQUIRE N, RELEASE M, STORE *B
 
+Note that smp_mb__release_acquire() has no effect on ACQUIRE or RELEASE
+operations performed by other CPUs, even if they are to the same variable.
+In cases where transitivity is required, smp_mb() should be used explicitly.
+
 Locks and semaphores may not provide any guarantee of ordering on UP compiled
 systems, and so cannot be counted on in such a situation to actually achieve
 anything at all - especially with respect to I/O accesses - unless combined
#16 lore raw
In reply to Will Deacon

Hi Will,

Quoted 57 lines · Will Deacon

As much as we'd like to live in a world where RELEASE -> ACQUIRE is always cheaply ordered and can be used to construct UNLOCK -> LOCK definitions with similar guarantees, the grim reality is that this isn't even possible on x86 (thanks to Paul for bringing us crashing down to Earth).

This patch handles the issue by introducing a new barrier macro, smp_mb__release_acquire, that can be placed between a RELEASE and a subsequent ACQUIRE operation in order to upgrade them to a full memory barrier. At the moment, it doesn't have any users, so its existence serves mainly as a documentation aid.

Documentation/memory-barriers.txt is updated to describe more clearly the ACQUIRE and RELEASE ordering in this area and to show an example of the new barrier in action.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>

---

Following our discussion at [1], I thought I'd try to write something down...

[1] http://lkml.kernel.org/r/[email protected]

 Documentation/memory-barriers.txt  | 23 ++++++++++++++++++++++-
 arch/powerpc/include/asm/barrier.h |  1 +
 arch/x86/include/asm/barrier.h     |  2 ++
 include/asm-generic/barrier.h      |  4 ++++
 4 files changed, 29 insertions(+), 1 deletion(-)
Documentation/memory-barriers.txt +8 -1
index 2ba8461b0631..46a85abb77c6 100644
@@ -459,11 +459,18 @@ And a couple of implicit varieties:
      RELEASE on that same variable are guaranteed to be visible.  In other
      words, within a given variable's critical section, all accesses of all
      previous critical sections for that variable are guaranteed to have
-     completed.
+     completed.  If the RELEASE and ACQUIRE operations act on independent
+     variables, an smp_mb__release_acquire() barrier can be placed between
+     them to upgrade the sequence to a full barrier.
 
      This means that ACQUIRE acts as a minimal "acquire" operation and
      RELEASE acts as a minimal "release" operation.
 
+A subset of the atomic operations described in atomic_ops.txt have ACQUIRE
+and RELEASE variants in addition to fully-ordered and relaxed definitions.
+For compound atomics performing both a load and a store, ACQUIRE semantics
+apply only to the load and RELEASE semantics only to the store portion of
+the operation.
 
 Memory barriers are only required where there's a possibility of interaction
 between two CPUs or between a CPU and a device.  If it can be guaranteed that

Documentation/memory-barriers.txt the RELEASE would simply complete, thereby avoiding the deadlock. · line 1910

Quoted 9 lines
Documentation/memory-barriers.txt +6 -0
@@ -1895,6 +1902,20 @@ the RELEASE would simply complete, thereby avoiding the deadlock.
 	a sleep-unlock race, but the locking primitive needs to resolve
 	such races properly in any case.
 
+If necessary, ordering can be enforced by use of an
+smp_mb__release_acquire() barrier:
+
+	*A = a;
+	RELEASE M
+	smp_mb__release_acquire();

Should this barrier be placed after the ACQUIRE? Because we do actually want(?) and allow RELEASE and ACQUIRE operations to reorder in this case, like your following example, right?

Regards,
Boqun

Quoted 8 lines
Documentation/memory-barriers.txt +8 -0
+	ACQUIRE N
+	*B = b;
+
+in which case, the only permitted sequences are:
+
+	STORE *A, RELEASE M, ACQUIRE N, STORE *B
+	STORE *A, ACQUIRE N, RELEASE M, STORE *B
+
#17 lore raw
Quoted 1 line · Boqun Feng

Hi Will,

Hello,

Quoted 11 lines · Will Deacon

Documentation/memory-barriers.txt the RELEASE would simply complete, thereby avoiding the deadlock. · line 1910

Quoted 6 lines
Documentation/memory-barriers.txt +6 -0
+If necessary, ordering can be enforced by use of an
+smp_mb__release_acquire() barrier:
+
+	*A = a;
+	RELEASE M
+	smp_mb__release_acquire();

Should this barrier be placed after the ACQUIRE? Because we do actually want(?) and allow RELEASE and ACQUIRE operations to reorder in this case, like your following example, right?

I think it's a lot simpler to keep it where it is, in all honesty. The relaxation for the RELEASE/ACQUIRE access ordering is mainly there to allow architectures building those operations out of explicit barriers to get away without a definition of smp_mb__release_acquire.

Will

#18 lore raw
Quoted 17 lines · Will Deacon
Quoted 1 line · Boqun Feng

Hi Will,

Hello,

Quoted 11 lines · Will Deacon

Documentation/memory-barriers.txt the RELEASE would simply complete, thereby avoiding the deadlock. · line 1910

Quoted 6 lines
Documentation/memory-barriers.txt +6 -0
+If necessary, ordering can be enforced by use of an
+smp_mb__release_acquire() barrier:
+
+	*A = a;
+	RELEASE M
+	smp_mb__release_acquire();

Should this barrier be placed after the ACQUIRE? Because we do actually want(?) and allow RELEASE and ACQUIRE operations to reorder in this case, like your following example, right?

Quoted 5 lines

I think it's a lot simpler to keep it where it is, in all honesty. The relaxation for the RELEASE/ACQUIRE access ordering is mainly there to allow architectures building those operations out of explicit barriers to get away without a definition of smp_mb__release_acquire.

Fair enough, and plus there is actually no user(even potential user) of this for now, it may be too early to argue where the barrier should be put.

Regards,
Boqun

#6 lore raw
In reply to Will Deacon
Quoted 13 lines · Will Deacon
Quoted 12 lines · Peter Zijlstra
Quoted 8 lines · Will Deacon
Quoted 1 line

Indeed, that is a hole in the definition, that I think we should close.

I'm struggling to understand the hole, but here's my intuition. If an ACQUIRE on CPUx reads from a RELEASE by CPUy, then I'd expect CPUx to observe all memory accessed performed by CPUy prior to the RELEASE before it observes the RELEASE itself, regardless of this new barrier. I think this matches what we currently have in memory-barriers.txt (i.e. acquire/release are neither transitive or multi-copy atomic).

Ah agreed. I seem to have gotten my brain in a tangle.

Quoted 8 lines
Quoted 3 lines

Basically where a program order release+acquire relies on an address dependency, a cross cpu release+acquire relies on causality. If we observe the release, we must also observe everything prior to it etc.

Yes, and crucially, the "everything prior to it" only encompasses accesses made by the releasing CPU itself (in the absence of other barriers and synchronisation).

Just want to make sure I understand you correctly, do you mean that in the following case:

CPU 1 CPU 2 CPU 3
============== ============================ ===============
{ A = 0, B = 0 }

WRITE_ONCE(A,1);	r1 = READ_ONCE(A);		r2 = smp_load_acquire(&B);
			smp_store_release(&B, 1);	r3 = READ_ONCE(A);

r1 == 1 && r2 == 1 && r3 == 0 is not prohibitted?

However, according to the discussion of Paul and Peter:

https://lkml.org/lkml/2015/9/15/707

I think that's prohibitted on architectures except s390 for sure. And for s390, we are waiting for the maintainers to verify this. If s390 also prohibits this, then a release-acquire pair(on different CPUs) to the same variable does guarantee transitivity.

Did I misunderstand you or miss something here?

Quoted 23 lines

Given that we managed to get confused, it doesn't hurt to call this out explicitly in the doc, so I can add the following extra text.

Will

--->8

Documentation/memory-barriers.txt +2 -2
index 46a85abb77c6..794d102d06df 100644
@@ -1902,8 +1902,8 @@ the RELEASE would simply complete, thereby avoiding the deadlock.
 	a sleep-unlock race, but the locking primitive needs to resolve
 	such races properly in any case.
 
-If necessary, ordering can be enforced by use of an
-smp_mb__release_acquire() barrier:
+Where the RELEASE and ACQUIRE operations are performed by the same CPU,
+ordering can be enforced by use of an smp_mb__release_acquire() barrier:
 
 	*A = a;
 	RELEASE M

Documentation/memory-barriers.txt in which case, the only permitted sequences are: · line 1922

Quoted 7 lines
Documentation/memory-barriers.txt +4 -0
@@ -1916,6 +1916,10 @@ in which case, the only permitted sequences are:
 	STORE *A, RELEASE M, ACQUIRE N, STORE *B
 	STORE *A, ACQUIRE N, RELEASE M, STORE *B
 
+Note that smp_mb__release_acquire() has no effect on ACQUIRE or RELEASE
+operations performed by other CPUs, even if they are to the same variable.
+In cases where transitivity is required, smp_mb() should be used explicitly.
+

Then, IIRC, the memory order effect of RELEASE+ACQUIRE should be:

If an ACQUIRE loads the value of stored by a RELEASE, then on the CPU executing the ACQUIRE operation, all the memory operations after the ACQUIRE operation will perceive all the memory operations before the RELEASE operation on the CPU executing the RELEASE operation.

This could cover both the "on the same CPU" and "on different CPUs" cases.

Of course, this may has nothing to do with smp_mb__release_acquire(), but I think we can take this chance to document the memory order effect of RELEASE+ACQUIRE well.

Regards,
Boqun

Quoted 8 lines
Documentation/memory-barriers.txt +0 -1
 Locks and semaphores may not provide any guarantee of ordering on UP compiled
 systems, and so cannot be counted on in such a situation to actually achieve
 anything at all - especially with respect to I/O accesses - unless combined
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#7 lore raw
Quoted 78 lines · Boqun Feng
Quoted 13 lines · Will Deacon
Quoted 12 lines · Peter Zijlstra
Quoted 8 lines · Will Deacon
Quoted 1 line

Indeed, that is a hole in the definition, that I think we should close.

I'm struggling to understand the hole, but here's my intuition. If an ACQUIRE on CPUx reads from a RELEASE by CPUy, then I'd expect CPUx to observe all memory accessed performed by CPUy prior to the RELEASE before it observes the RELEASE itself, regardless of this new barrier. I think this matches what we currently have in memory-barriers.txt (i.e. acquire/release are neither transitive or multi-copy atomic).

Ah agreed. I seem to have gotten my brain in a tangle.

Quoted 8 lines
Quoted 3 lines

Basically where a program order release+acquire relies on an address dependency, a cross cpu release+acquire relies on causality. If we observe the release, we must also observe everything prior to it etc.

Yes, and crucially, the "everything prior to it" only encompasses accesses made by the releasing CPU itself (in the absence of other barriers and synchronisation).

Just want to make sure I understand you correctly, do you mean that in the following case:

CPU 1 CPU 2 CPU 3
============== ============================ ===============
{ A = 0, B = 0 }

WRITE_ONCE(A,1);	r1 = READ_ONCE(A);		r2 = smp_load_acquire(&B);
			smp_store_release(&B, 1);	r3 = READ_ONCE(A);

r1 == 1 && r2 == 1 && r3 == 0 is not prohibitted?

However, according to the discussion of Paul and Peter:

https://lkml.org/lkml/2015/9/15/707

I think that's prohibitted on architectures except s390 for sure. And for s390, we are waiting for the maintainers to verify this. If s390 also prohibits this, then a release-acquire pair(on different CPUs) to the same variable does guarantee transitivity.

Did I misunderstand you or miss something here?

Quoted 23 lines

Given that we managed to get confused, it doesn't hurt to call this out explicitly in the doc, so I can add the following extra text.

Will

--->8

Documentation/memory-barriers.txt +2 -2
index 46a85abb77c6..794d102d06df 100644
@@ -1902,8 +1902,8 @@ the RELEASE would simply complete, thereby avoiding the deadlock.
 	a sleep-unlock race, but the locking primitive needs to resolve
 	such races properly in any case.
 
-If necessary, ordering can be enforced by use of an
-smp_mb__release_acquire() barrier:
+Where the RELEASE and ACQUIRE operations are performed by the same CPU,
+ordering can be enforced by use of an smp_mb__release_acquire() barrier:
 
 	*A = a;
 	RELEASE M

Documentation/memory-barriers.txt in which case, the only permitted sequences are: · line 1922

Quoted 7 lines
Documentation/memory-barriers.txt +4 -0
@@ -1916,6 +1916,10 @@ in which case, the only permitted sequences are:
 	STORE *A, RELEASE M, ACQUIRE N, STORE *B
 	STORE *A, ACQUIRE N, RELEASE M, STORE *B
 
+Note that smp_mb__release_acquire() has no effect on ACQUIRE or RELEASE
+operations performed by other CPUs, even if they are to the same variable.
+In cases where transitivity is required, smp_mb() should be used explicitly.
+

Then, IIRC, the memory order effect of RELEASE+ACQUIRE should be:

Quoted 5 lines

If an ACQUIRE loads the value of stored by a RELEASE, then on the CPU executing the ACQUIRE operation, all the memory operations after the ACQUIRE operation will perceive all the memory operations before the RELEASE operation on the CPU executing the RELEASE operation.

Ah.. I think I lost my mind while writting this. Should be:

If an ACQUIRE loads the value of stored by a RELEASE, then after the ACQUIRE operation, the CPU executing the ACQUIRE operation will perceive all the memory operations that have been perceived by the CPU executing the RELEASE operation before the RELEASE operation.

Which means a release+acquire pair to the same variable guarantees transitivity.

Sorry for the misleading paragraph..

Regards,
Boqun

Quoted 19 lines

This could cover both the "on the same CPU" and "on different CPUs" cases.

Of course, this may has nothing to do with smp_mb__release_acquire(), but I think we can take this chance to document the memory order effect of RELEASE+ACQUIRE well.

Regards,
Boqun

Quoted 8 lines
Documentation/memory-barriers.txt +0 -1
 Locks and semaphores may not provide any guarantee of ordering on UP compiled
 systems, and so cannot be counted on in such a situation to actually achieve
 anything at all - especially with respect to I/O accesses - unless combined
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#8 lore raw
In reply to Boqun Feng
Quoted 36 lines · Boqun Feng
Quoted 13 lines · Will Deacon
Quoted 12 lines · Peter Zijlstra
Quoted 8 lines · Will Deacon
Quoted 1 line

Indeed, that is a hole in the definition, that I think we should close.

I'm struggling to understand the hole, but here's my intuition. If an ACQUIRE on CPUx reads from a RELEASE by CPUy, then I'd expect CPUx to observe all memory accessed performed by CPUy prior to the RELEASE before it observes the RELEASE itself, regardless of this new barrier. I think this matches what we currently have in memory-barriers.txt (i.e. acquire/release are neither transitive or multi-copy atomic).

Ah agreed. I seem to have gotten my brain in a tangle.

Quoted 8 lines
Quoted 3 lines

Basically where a program order release+acquire relies on an address dependency, a cross cpu release+acquire relies on causality. If we observe the release, we must also observe everything prior to it etc.

Yes, and crucially, the "everything prior to it" only encompasses accesses made by the releasing CPU itself (in the absence of other barriers and synchronisation).

Just want to make sure I understand you correctly, do you mean that in the following case:

CPU 1 CPU 2 CPU 3
============== ============================ ===============
{ A = 0, B = 0 }

WRITE_ONCE(A,1);	r1 = READ_ONCE(A);		r2 = smp_load_acquire(&B);
			smp_store_release(&B, 1);	r3 = READ_ONCE(A);

r1 == 1 && r2 == 1 && r3 == 0 is not prohibitted?

However, according to the discussion of Paul and Peter:

Quoted 8 lines

https://lkml.org/lkml/2015/9/15/707

I think that's prohibitted on architectures except s390 for sure. And for s390, we are waiting for the maintainers to verify this. If s390 also prohibits this, then a release-acquire pair(on different CPUs) to the same variable does guarantee transitivity.

Did I misunderstand you or miss something here?

That certainly works on arm and arm64, so if it works everywhere else too, then we can strengthen this (but see below).

Quoted 19 lines
Quoted 19 lines
Documentation/memory-barriers.txt +2 -2
index 46a85abb77c6..794d102d06df 100644
@@ -1902,8 +1902,8 @@ the RELEASE would simply complete, thereby avoiding the deadlock.
 	a sleep-unlock race, but the locking primitive needs to resolve
 	such races properly in any case.
 
-If necessary, ordering can be enforced by use of an
-smp_mb__release_acquire() barrier:
+Where the RELEASE and ACQUIRE operations are performed by the same CPU,
+ordering can be enforced by use of an smp_mb__release_acquire() barrier:
 
 	*A = a;
 	RELEASE M
@@ -1916,6 +1916,10 @@ in which case, the only permitted sequences are:
 	STORE *A, RELEASE M, ACQUIRE N, STORE *B
 	STORE *A, ACQUIRE N, RELEASE M, STORE *B
 
Quoted 6 lines

Documentation/memory-barriers.txt in which case, the only permitted sequences are: · line 1922

Quoted 4 lines
Documentation/memory-barriers.txt +4 -0
+Note that smp_mb__release_acquire() has no effect on ACQUIRE or RELEASE
+operations performed by other CPUs, even if they are to the same variable.
+In cases where transitivity is required, smp_mb() should be used explicitly.
+

Then, IIRC, the memory order effect of RELEASE+ACQUIRE should be:

[updated from your reply]

Quoted 7 lines

If an ACQUIRE loads the value of stored by a RELEASE, then after the ACQUIRE operation, the CPU executing the ACQUIRE operation will perceive all the memory operations that have been perceived by the CPU executing the RELEASE operation before the RELEASE operation.

Which means a release+acquire pair to the same variable guarantees transitivity.

Almost, but on arm64 at least, "all the memory operations" above doesn't include reads by other CPUs. I'm struggling to figure out whether that's actually an issue.

Will

#9 lore raw
Quoted 82 lines · Will Deacon
Quoted 36 lines · Boqun Feng
Quoted 13 lines · Will Deacon
Quoted 12 lines · Peter Zijlstra
Quoted 8 lines · Will Deacon
Quoted 1 line

Indeed, that is a hole in the definition, that I think we should close.

I'm struggling to understand the hole, but here's my intuition. If an ACQUIRE on CPUx reads from a RELEASE by CPUy, then I'd expect CPUx to observe all memory accessed performed by CPUy prior to the RELEASE before it observes the RELEASE itself, regardless of this new barrier. I think this matches what we currently have in memory-barriers.txt (i.e. acquire/release are neither transitive or multi-copy atomic).

Ah agreed. I seem to have gotten my brain in a tangle.

Quoted 8 lines
Quoted 3 lines

Basically where a program order release+acquire relies on an address dependency, a cross cpu release+acquire relies on causality. If we observe the release, we must also observe everything prior to it etc.

Yes, and crucially, the "everything prior to it" only encompasses accesses made by the releasing CPU itself (in the absence of other barriers and synchronisation).

Just want to make sure I understand you correctly, do you mean that in the following case:

CPU 1 CPU 2 CPU 3
============== ============================ ===============
{ A = 0, B = 0 }

WRITE_ONCE(A,1);	r1 = READ_ONCE(A);		r2 = smp_load_acquire(&B);
			smp_store_release(&B, 1);	r3 = READ_ONCE(A);

r1 == 1 && r2 == 1 && r3 == 0 is not prohibitted?

However, according to the discussion of Paul and Peter:

Quoted 8 lines

https://lkml.org/lkml/2015/9/15/707

I think that's prohibitted on architectures except s390 for sure. And for s390, we are waiting for the maintainers to verify this. If s390 also prohibits this, then a release-acquire pair(on different CPUs) to the same variable does guarantee transitivity.

Did I misunderstand you or miss something here?

That certainly works on arm and arm64, so if it works everywhere else too, then we can strengthen this (but see below).

Quoted 19 lines
Quoted 19 lines
Documentation/memory-barriers.txt +2 -2
index 46a85abb77c6..794d102d06df 100644
@@ -1902,8 +1902,8 @@ the RELEASE would simply complete, thereby avoiding the deadlock.
 	a sleep-unlock race, but the locking primitive needs to resolve
 	such races properly in any case.
 
-If necessary, ordering can be enforced by use of an
-smp_mb__release_acquire() barrier:
+Where the RELEASE and ACQUIRE operations are performed by the same CPU,
+ordering can be enforced by use of an smp_mb__release_acquire() barrier:
 
 	*A = a;
 	RELEASE M
@@ -1916,6 +1916,10 @@ in which case, the only permitted sequences are:
 	STORE *A, RELEASE M, ACQUIRE N, STORE *B
 	STORE *A, ACQUIRE N, RELEASE M, STORE *B
 
Quoted 6 lines

Documentation/memory-barriers.txt in which case, the only permitted sequences are: · line 1922

Quoted 4 lines
Documentation/memory-barriers.txt +4 -0
+Note that smp_mb__release_acquire() has no effect on ACQUIRE or RELEASE
+operations performed by other CPUs, even if they are to the same variable.
+In cases where transitivity is required, smp_mb() should be used explicitly.
+

Then, IIRC, the memory order effect of RELEASE+ACQUIRE should be:

[updated from your reply]

Quoted 5 lines

If an ACQUIRE loads the value of stored by a RELEASE, then after the ACQUIRE operation, the CPU executing the ACQUIRE operation will perceive all the memory operations that have been perceived by the CPU executing the RELEASE operation before the RELEASE operation.

Quoted 7 lines
Quoted 2 lines

Which means a release+acquire pair to the same variable guarantees transitivity.

Almost, but on arm64 at least, "all the memory operations" above doesn't include reads by other CPUs. I'm struggling to figure out whether that's actually an issue.

Ah.. that's indeed an issue! for example:

CPU 0 CPU 1 CPU 2
===================== ========================== ================
{a = 0, b = 0, c = 0}
r1 = READ_ONCE(a); WRITE_ONCE(b, 1); r3 = smp_load_acquire(&c);
smp_rmb(); smp_store_release(&c, 1); WRITE_ONCE(a, 1);
r2 = READ_ONCE(b)

where r1 == 1 && r2 == 0 && r3 == 1 is actually not prohibitted, at least on POWER.

However, I think that doens't mean a release+acquire pair to the same variable doesn't guarantee transitivity, because the transitivity is actually broken at the smp_rmb(). But yes, my document is incorrect. How about:

If an ACQUIRE loads the value of stored by a RELEASE, then after the ACQUIRE operation, the CPU executing the ACQUIRE operation will perceive all the memory operations that have been perceived by the CPU executing the RELEASE operation *transitively* before the RELEASE operation. ("transitively before" means that a memory operation is either executed on the same CPU before the other, or guaranteed executed before the other by a transitive barrier).

Which means a release+acquire pair to the same variable guarantees transitivity.

Maybe we can avoid to use term "transitively before" here, but it's not bad to distinguish different kinds of "before"s.

Regards,
Boqun

#10 lore raw
Quoted 100 lines · Boqun Feng
Quoted 82 lines · Will Deacon
Quoted 36 lines · Boqun Feng
Quoted 13 lines · Will Deacon
Quoted 12 lines · Peter Zijlstra
Quoted 8 lines · Will Deacon
Quoted 1 line

Indeed, that is a hole in the definition, that I think we should close.

I'm struggling to understand the hole, but here's my intuition. If an ACQUIRE on CPUx reads from a RELEASE by CPUy, then I'd expect CPUx to observe all memory accessed performed by CPUy prior to the RELEASE before it observes the RELEASE itself, regardless of this new barrier. I think this matches what we currently have in memory-barriers.txt (i.e. acquire/release are neither transitive or multi-copy atomic).

Ah agreed. I seem to have gotten my brain in a tangle.

Quoted 8 lines
Quoted 3 lines

Basically where a program order release+acquire relies on an address dependency, a cross cpu release+acquire relies on causality. If we observe the release, we must also observe everything prior to it etc.

Yes, and crucially, the "everything prior to it" only encompasses accesses made by the releasing CPU itself (in the absence of other barriers and synchronisation).

Just want to make sure I understand you correctly, do you mean that in the following case:

CPU 1 CPU 2 CPU 3
============== ============================ ===============
{ A = 0, B = 0 }

WRITE_ONCE(A,1);	r1 = READ_ONCE(A);		r2 = smp_load_acquire(&B);
			smp_store_release(&B, 1);	r3 = READ_ONCE(A);

r1 == 1 && r2 == 1 && r3 == 0 is not prohibitted?

However, according to the discussion of Paul and Peter:

Quoted 8 lines

https://lkml.org/lkml/2015/9/15/707

I think that's prohibitted on architectures except s390 for sure. And for s390, we are waiting for the maintainers to verify this. If s390 also prohibits this, then a release-acquire pair(on different CPUs) to the same variable does guarantee transitivity.

Did I misunderstand you or miss something here?

That certainly works on arm and arm64, so if it works everywhere else too, then we can strengthen this (but see below).

Quoted 19 lines
Quoted 19 lines
Documentation/memory-barriers.txt +2 -2
index 46a85abb77c6..794d102d06df 100644
@@ -1902,8 +1902,8 @@ the RELEASE would simply complete, thereby avoiding the deadlock.
 	a sleep-unlock race, but the locking primitive needs to resolve
 	such races properly in any case.
 
-If necessary, ordering can be enforced by use of an
-smp_mb__release_acquire() barrier:
+Where the RELEASE and ACQUIRE operations are performed by the same CPU,
+ordering can be enforced by use of an smp_mb__release_acquire() barrier:
 
 	*A = a;
 	RELEASE M
@@ -1916,6 +1916,10 @@ in which case, the only permitted sequences are:
 	STORE *A, RELEASE M, ACQUIRE N, STORE *B
 	STORE *A, ACQUIRE N, RELEASE M, STORE *B
 
Quoted 6 lines

Documentation/memory-barriers.txt in which case, the only permitted sequences are: · line 1922

Quoted 4 lines
Documentation/memory-barriers.txt +4 -0
+Note that smp_mb__release_acquire() has no effect on ACQUIRE or RELEASE
+operations performed by other CPUs, even if they are to the same variable.
+In cases where transitivity is required, smp_mb() should be used explicitly.
+

Then, IIRC, the memory order effect of RELEASE+ACQUIRE should be:

[updated from your reply]

Quoted 5 lines

If an ACQUIRE loads the value of stored by a RELEASE, then after the ACQUIRE operation, the CPU executing the ACQUIRE operation will perceive all the memory operations that have been perceived by the CPU executing the RELEASE operation before the RELEASE operation.

Quoted 7 lines
Quoted 2 lines

Which means a release+acquire pair to the same variable guarantees transitivity.

Almost, but on arm64 at least, "all the memory operations" above doesn't include reads by other CPUs. I'm struggling to figure out whether that's actually an issue.

Ah.. that's indeed an issue! for example:

CPU 0 CPU 1 CPU 2
===================== ========================== ================
{a = 0, b = 0, c = 0}
r1 = READ_ONCE(a); WRITE_ONCE(b, 1); r3 = smp_load_acquire(&c);
smp_rmb(); smp_store_release(&c, 1); WRITE_ONCE(a, 1);
r2 = READ_ONCE(b)

Quoted 3 lines

where r1 == 1 && r2 == 0 && r3 == 1 is actually not prohibitted, at least on POWER.

Oops.. I use wrong litmus here.. so this is prohibitted on POWER. Sorry for the misleading. How about the behavior of that on arm and arm64?

If prohibitted too, please ignore below.

Apologies again for that..

Regards,
Boqun

Quoted 22 lines

However, I think that doens't mean a release+acquire pair to the same variable doesn't guarantee transitivity, because the transitivity is actually broken at the smp_rmb(). But yes, my document is incorrect. How about:

If an ACQUIRE loads the value of stored by a RELEASE, then after the ACQUIRE operation, the CPU executing the ACQUIRE operation will perceive all the memory operations that have been perceived by the CPU executing the RELEASE operation *transitively* before the RELEASE operation. ("transitively before" means that a memory operation is either executed on the same CPU before the other, or guaranteed executed before the other by a transitive barrier).

Which means a release+acquire pair to the same variable guarantees transitivity.

Maybe we can avoid to use term "transitively before" here, but it's not bad to distinguish different kinds of "before"s.

Regards,
Boqun

#11 lore raw
Quoted 25 lines · Boqun Feng
Quoted 24 lines · Boqun Feng
Quoted 6 lines · Will Deacon
Quoted 5 lines · Boqun Feng

If an ACQUIRE loads the value of stored by a RELEASE, then after the ACQUIRE operation, the CPU executing the ACQUIRE operation will perceive all the memory operations that have been perceived by the CPU executing the RELEASE operation before the RELEASE operation.

Quoted 7 lines
Quoted 2 lines

Which means a release+acquire pair to the same variable guarantees transitivity.

Almost, but on arm64 at least, "all the memory operations" above doesn't include reads by other CPUs. I'm struggling to figure out whether that's actually an issue.

Ah.. that's indeed an issue! for example:

CPU 0 CPU 1 CPU 2
===================== ========================== ================
{a = 0, b = 0, c = 0}
r1 = READ_ONCE(a); WRITE_ONCE(b, 1); r3 = smp_load_acquire(&c);
smp_rmb(); smp_store_release(&c, 1); WRITE_ONCE(a, 1);
r2 = READ_ONCE(b)

Quoted 6 lines
Quoted 3 lines

where r1 == 1 && r2 == 0 && r3 == 1 is actually not prohibitted, at least on POWER.

Oops.. I use wrong litmus here.. so this is prohibitted on POWER. Sorry for the misleading. How about the behavior of that on arm and arm64?

That explicit test is forbidden on arm/arm64 because of the smp_rmb(), but if you rewrite it as (LDAR is acquire, STLR is release):

  {
  0:X1=x; 0:X3=y;
  1:X1=y; 1:X2=z;
  2:X1=z; 2:X3=x;
  }
   P0           | P1           | P2                ;
   LDAR W0,[X1] | MOV W0,#1    | LDAR W0,[X1]      ;
   LDR W2,[X3]  | STR W0,[X1]  | MOV W2,#1         ;
                | STLR W0,[X2] | STR W2,[X3]       ;

  Observed
      0:X0=1; 0:X2=0; 2:X0=1;

then it is permitted on arm64. Note that herd currently claims that this is forbidden, but I'm talking to the authors about getting that fixed :)

Will

#12 lore raw
Quoted 37 lines · Will Deacon
Quoted 25 lines · Boqun Feng
Quoted 24 lines · Boqun Feng
Quoted 6 lines · Will Deacon
Quoted 5 lines · Boqun Feng

If an ACQUIRE loads the value of stored by a RELEASE, then after the ACQUIRE operation, the CPU executing the ACQUIRE operation will perceive all the memory operations that have been perceived by the CPU executing the RELEASE operation before the RELEASE operation.

Quoted 7 lines
Quoted 2 lines

Which means a release+acquire pair to the same variable guarantees transitivity.

Almost, but on arm64 at least, "all the memory operations" above doesn't include reads by other CPUs. I'm struggling to figure out whether that's actually an issue.

Ah.. that's indeed an issue! for example:

CPU 0 CPU 1 CPU 2
===================== ========================== ================
{a = 0, b = 0, c = 0}
r1 = READ_ONCE(a); WRITE_ONCE(b, 1); r3 = smp_load_acquire(&c);
smp_rmb(); smp_store_release(&c, 1); WRITE_ONCE(a, 1);
r2 = READ_ONCE(b)

Quoted 6 lines
Quoted 3 lines

where r1 == 1 && r2 == 0 && r3 == 1 is actually not prohibitted, at least on POWER.

Oops.. I use wrong litmus here.. so this is prohibitted on POWER. Sorry for the misleading. How about the behavior of that on arm and arm64?

That explicit test is forbidden on arm/arm64 because of the smp_rmb(), but if you rewrite it as (LDAR is acquire, STLR is release):

Quoted 13 lines
  {
  0:X1=x; 0:X3=y;
  1:X1=y; 1:X2=z;
  2:X1=z; 2:X3=x;
  }
   P0           | P1           | P2                ;
   LDAR W0,[X1] | MOV W0,#1    | LDAR W0,[X1]      ;
   LDR W2,[X3]  | STR W0,[X1]  | MOV W2,#1         ;
                | STLR W0,[X2] | STR W2,[X3]       ;

  Observed
      0:X0=1; 0:X2=0; 2:X0=1;

X0 is W0, etc. Right?

Quoted 4 lines

then it is permitted on arm64. Note that herd currently claims that this is forbidden, but I'm talking to the authors about getting that fixed :)

Good to know ;-) I think this actually means two things:

1. ACQUIRE doesn't provide transitivity itself

and

2. We still need the term like "transitively before".

Regards,
Boqun

#13 lore raw
In reply to Will Deacon
Quoted 51 lines · Will Deacon
Quoted 25 lines · Boqun Feng
Quoted 24 lines · Boqun Feng
Quoted 6 lines · Will Deacon
Quoted 5 lines · Boqun Feng

If an ACQUIRE loads the value of stored by a RELEASE, then after the ACQUIRE operation, the CPU executing the ACQUIRE operation will perceive all the memory operations that have been perceived by the CPU executing the RELEASE operation before the RELEASE operation.

Quoted 7 lines
Quoted 2 lines

Which means a release+acquire pair to the same variable guarantees transitivity.

Almost, but on arm64 at least, "all the memory operations" above doesn't include reads by other CPUs. I'm struggling to figure out whether that's actually an issue.

Ah.. that's indeed an issue! for example:

CPU 0 CPU 1 CPU 2
===================== ========================== ================
{a = 0, b = 0, c = 0}
r1 = READ_ONCE(a); WRITE_ONCE(b, 1); r3 = smp_load_acquire(&c);
smp_rmb(); smp_store_release(&c, 1); WRITE_ONCE(a, 1);
r2 = READ_ONCE(b)

Quoted 6 lines
Quoted 3 lines

where r1 == 1 && r2 == 0 && r3 == 1 is actually not prohibitted, at least on POWER.

Oops.. I use wrong litmus here.. so this is prohibitted on POWER. Sorry for the misleading. How about the behavior of that on arm and arm64?

That explicit test is forbidden on arm/arm64 because of the smp_rmb(), but if you rewrite it as (LDAR is acquire, STLR is release):

  {
  0:X1=x; 0:X3=y;
  1:X1=y; 1:X2=z;
  2:X1=z; 2:X3=x;
  }
   P0           | P1           | P2                ;
   LDAR W0,[X1] | MOV W0,#1    | LDAR W0,[X1]      ;
   LDR W2,[X3]  | STR W0,[X1]  | MOV W2,#1         ;
                | STLR W0,[X2] | STR W2,[X3]       ;

  Observed
      0:X0=1; 0:X2=0; 2:X0=1;
Quoted 2 lines

then it is permitted on arm64. Note that herd currently claims that this is forbidden, but I'm talking to the authors about getting that fixed :)

But a pure store-release/load-acquire chain would be forbidden in hardware as well as by herd, correct?

Thanx, Paul

#14 lore raw

Hi Paul,

Quoted 38 lines · Paul E. McKenney
Quoted 37 lines · Will Deacon
Quoted 11 lines · Boqun Feng
Quoted 10 lines · Boqun Feng

Ah.. that's indeed an issue! for example:

CPU 0 CPU 1 CPU 2
===================== ========================== ================
{a = 0, b = 0, c = 0}
r1 = READ_ONCE(a); WRITE_ONCE(b, 1); r3 = smp_load_acquire(&c);
smp_rmb(); smp_store_release(&c, 1); WRITE_ONCE(a, 1);
r2 = READ_ONCE(b)

Quoted 6 lines
Quoted 3 lines

where r1 == 1 && r2 == 0 && r3 == 1 is actually not prohibitted, at least on POWER.

Oops.. I use wrong litmus here.. so this is prohibitted on POWER. Sorry for the misleading. How about the behavior of that on arm and arm64?

That explicit test is forbidden on arm/arm64 because of the smp_rmb(), but if you rewrite it as (LDAR is acquire, STLR is release):

  {
  0:X1=x; 0:X3=y;
  1:X1=y; 1:X2=z;
  2:X1=z; 2:X3=x;
  }
   P0           | P1           | P2                ;
   LDAR W0,[X1] | MOV W0,#1    | LDAR W0,[X1]      ;
   LDR W2,[X3]  | STR W0,[X1]  | MOV W2,#1         ;
                | STLR W0,[X2] | STR W2,[X3]       ;

  Observed
      0:X0=1; 0:X2=0; 2:X0=1;
Quoted 5 lines
Quoted 2 lines

then it is permitted on arm64. Note that herd currently claims that this is forbidden, but I'm talking to the authors about getting that fixed :)

But a pure store-release/load-acquire chain would be forbidden in hardware as well as by herd, correct?

Yup, and since that's likely the common use-case, I think that's precisely the scenario where it makes sense for us to require transitivity in the kernel.

Will

#15 lore raw
Quoted 44 lines · Will Deacon

Hi Paul,

Quoted 41 lines · Paul E. McKenney
Quoted 39 lines · Will Deacon
Quoted 11 lines · Boqun Feng
Quoted 10 lines · Boqun Feng

Ah.. that's indeed an issue! for example:

CPU 0 CPU 1 CPU 2
===================== ========================== ================
{a = 0, b = 0, c = 0}
r1 = READ_ONCE(a); WRITE_ONCE(b, 1); r3 = smp_load_acquire(&c);
smp_rmb(); smp_store_release(&c, 1); WRITE_ONCE(a, 1);
r2 = READ_ONCE(b)

Quoted 6 lines
Quoted 3 lines

where r1 == 1 && r2 == 0 && r3 == 1 is actually not prohibitted, at least on POWER.

Oops.. I use wrong litmus here.. so this is prohibitted on POWER. Sorry for the misleading. How about the behavior of that on arm and arm64?

That explicit test is forbidden on arm/arm64 because of the smp_rmb(), but if you rewrite it as (LDAR is acquire, STLR is release):

  {
  0:X1=x; 0:X3=y;
  1:X1=y; 1:X2=z;
  2:X1=z; 2:X3=x;
  }
   P0           | P1           | P2                ;
   LDAR W0,[X1] | MOV W0,#1    | LDAR W0,[X1]      ;
   LDR W2,[X3]  | STR W0,[X1]  | MOV W2,#1         ;
                | STLR W0,[X2] | STR W2,[X3]       ;

  Observed
      0:X0=1; 0:X2=0; 2:X0=1;

then it is permitted on arm64. Note that herd currently claims that this is forbidden, but I'm talking to the authors about getting that fixed :)

Quoted 6 lines
Quoted 2 lines

But a pure store-release/load-acquire chain would be forbidden in hardware as well as by herd, correct?

Yup, and since that's likely the common use-case, I think that's precisely the scenario where it makes sense for us to require transitivity in the kernel.

Agreed. And again I believe that we need to err on the side of restricting what the developer can expect.

Thanx, Paul