atomics: powerpc: Implement relaxed/acquire/release variants of some atomics
Subsystems (4)
PATCH v5 1/6 powerpc: atomic: Make _return atomics and *{cmp}xchg fully ordered
In reply to Boqun FengThis patch fixes two problems to make value-returning atomics and {cmp}xchg fully ordered on PPC.
According to memory-barriers.txt:
Quoted 4 lines
Any atomic operation that modifies some state in memory and returns information about the state (old or new) implies an SMP-conditional general memory barrier (
smp_mb()) on each side of the actual operation ...
which means these operations should be fully ordered. However on PPC, PPC_ATOMIC_ENTRY_BARRIER is the barrier before the actual operation, which is currently "lwsync" if SMP=y. The leading "lwsync" can not guarantee fully ordered atomics, according to Paul Mckenney:
https://lkml.org/lkml/2015/10/14/970
To fix this, we define PPC_ATOMIC_ENTRY_BARRIER as "sync" to guarantee the fully-ordered semantics.
This also makes futex atomics fully ordered, which can avoid possible memory ordering problems if userspace code relies on futex system call for fully ordered semantics.
Another thing to fix is that xchg, cmpxchg and their atomic{64}_ versions are currently RELEASE+ACQUIRE, which are not fully ordered.
So also replace PPC_RELEASE_BARRIER and PPC_ACQUIRE_BARRIER with PPC_ATOMIC_ENTRY_BARRIER and PPC_ATOMIC_EXIT_BARRIER in __{cmp,}xchg_{u32,u64} respectively to guarantee fully ordered semantics of atomic{,64}_{cmp,}xchg() and {cmp,}xchg(), as a complement of commit b97021f85517 ("powerpc: Fix atomic_xxx_return barrier semantics").
---
Michael, I also change PPC_ATOMIC_ENTRY_BARRIER as "sync" if SMP=y in this version , which is different from the previous one, so request for a new ack. Thank you ;-)
arch/powerpc/include/asm/cmpxchg.h | 16 ++++++++-------- arch/powerpc/include/asm/synch.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-)
arch/powerpc/include/asm/cmpxchg.h +8 -8
@@ -18,12 +18,12 @@ __xchg_u32(volatile void *p, unsigned long val)unsigned long prev; __asm__ __volatile__( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: lwarx %0,0,%2 \n" PPC405_ERR77(0,%2)" stwcx. %3,0,%2 \n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER : "=&r" (prev), "+m" (*(volatile unsigned int *)p) : "r" (p), "r" (val) : "cc", "memory");
@@ -61,12 +61,12 @@ __xchg_u64(volatile void *p, unsigned long val)unsigned long prev; __asm__ __volatile__( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: ldarx %0,0,%2 \n" PPC405_ERR77(0,%2)" stdcx. %3,0,%2 \n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER : "=&r" (prev), "+m" (*(volatile unsigned long *)p) : "r" (p), "r" (val) : "cc", "memory");
@@ -151,14 +151,14 @@ __cmpxchg_u32(volatile unsigned int *p, unsigned long old, unsigned long new)unsigned int prev; __asm__ __volatile__ ( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: lwarx %0,0,%2 # __cmpxchg_u32\n\ cmpw 0,%0,%3\n\ bne- 2f\n" PPC405_ERR77(0,%2)" stwcx. %4,0,%2\n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER "\n\ 2:" : "=&r" (prev), "+m" (*p)
@@ -197,13 +197,13 @@ __cmpxchg_u64(volatile unsigned long *p, unsigned long old, unsigned long new)unsigned long prev; __asm__ __volatile__ ( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: ldarx %0,0,%2 # __cmpxchg_u64\n\ cmpd 0,%0,%3\n\ bne- 2f\n\ stdcx. %4,0,%2\n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER "\n\ 2:" : "=&r" (prev), "+m" (*p)
arch/powerpc/include/asm/synch.h +1 -1
@@ -44,7 +44,7 @@ static inline void isync(void)MAKE_LWSYNC_SECTION_ENTRY (97 , __lwsync_fixup);#define PPC_ACQUIRE_BARRIER "\n" stringify_in_c(__PPC_ACQUIRE_BARRIER)#define PPC_RELEASE_BARRIER stringify_in_c(LWSYNC) "\n" -#define PPC_ATOMIC_ENTRY_BARRIER "\n" stringify_in_c(LWSYNC) "\n" +#define PPC_ATOMIC_ENTRY_BARRIER "\n" stringify_in_c(sync) "\n"#define PPC_ATOMIC_EXIT_BARRIER "\n" stringify_in_c(sync) "\n"#else #define PPC_ACQUIRE_BARRIER
Signature · 1 line
2.6.2
v5 cover atomics: powerpc: Implement relaxed/acquire/release variants of some atomics
Hi all,
This is v5 of the series.
Link for v1: https://lkml.org/lkml/2015/8/27/798
Link for v2: https://lkml.org/lkml/2015/9/16/527
Link for v3: https://lkml.org/lkml/2015/10/12/368
Link for v4: https://lkml.org/lkml/2015/10/14/670
Changes since v4:
* define PPC_ATOMIC_ENTRY_BARRIER as "sync" (Paul E. Mckenney)
* remove PPC-specific __atomic_op_fence().
Relaxed/acquire/release variants of atomic operations {add,sub}_return and {cmp,}xchg are introduced by commit:
"atomics: add acquire/release/relaxed variants of some atomic operations"
and {inc,dec}_return has been introduced by commit:
"locking/asm-generic: Add _{relaxed|acquire|release}() variants for inc/dec atomics"
Both of these are in the current locking/core branch of the tip tree.
By default, the generic code will implement a relaxed variant as a full ordered atomic operation and release/acquire a variant as a relaxed variant with a necessary general barrier before or after.
On PPC, which has a weak memory order model, a relaxed variant can be implemented more lightweightly than a full ordered one. Further more, release and acquire variants can be implemented with arch-specific lightweight barriers.
Besides, cmpxchg, xchg and their atomic_ versions are only RELEASE+ACQUIRE rather that fully ordered in current PPC implementation, which is incorrect according to memory-barriers.txt. Further more, PPC_ATOMIC_ENTRY_BARRIER, the leading barrier of fully ordered atomics, should be "sync" rather than "lwsync" if SMP=y, to guarantee fully ordered semantics.
Therefore this patchset fixes the order guarantee of cmpxchg, xchg and value-returning atomics on PPC and implements the relaxed/acquire/release variants based on PPC memory model and specific barriers, Some trivial tests for these new variants are also included in this series, because some of these variants are not used in kernel for now, I think is a good idea to at least generate the code for these variants somewhere.
The patchset consists of 6 parts:
1. Make value-returning atomics, futex atomics, xchg and cmpxchg fully
ordered
2. Add trivial tests for the new variants in lib/atomic64_test.c
3. Allow architectures to define their own __atomic_op_*() helpers
to build other variants based on relaxed.
4. Implement atomic{,64}_{add,sub,inc,dec}_return_* variants
5. Implement xchg_* and atomic{,64}_xchg_* variants
6. Implement cmpxchg_* atomic{,64}_cmpxchg_* variants
This patchset is based on current locking/core branch of the tip tree and all patches are built and boot tested for little endian pseries, and also tested by 0day.
Looking forward to any suggestion, question and comment ;-)
Regards,
Boqun
PATCH v5 2/6 atomics: Add test for atomic operations with _relaxed variants
Some atomic operations now have _relaxed/acquire/release variants, this patch then adds some trivial tests for two purpose:
1. test the behavior of these new operations in single-CPU
environment.
2. make their code generated before we actually use them somewhere,
so that we can examine their assembly code.
--- lib/atomic64_test.c | 120 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 79 insertions(+), 41 deletions(-)
lib/atomic64_test.c +79 -41
@@ -27,6 +27,65 @@ do { \ (unsigned long long)r); \ } while (0) +/* + * Test for a atomic operation family, + * @test should be a macro accepting parameters (bit, op, ...) + */ + +#define FAMILY_TEST(test, bit, op, args...) \ +do { \ + test(bit, op, ##args); \ + test(bit, op##_acquire, ##args); \ + test(bit, op##_release, ##args); \ + test(bit, op##_relaxed, ##args); \ +} while (0) + +#define TEST_RETURN(bit, op, c_op, val) \ +do { \ + atomic##bit##_set(&v, v0); \ + r = v0; \ + r c_op val; \ + BUG_ON(atomic##bit##_##op(val, &v) != r); \ + BUG_ON(atomic##bit##_read(&v) != r); \ +} while (0) + +#define RETURN_FAMILY_TEST(bit, op, c_op, val) \ +do { \ + FAMILY_TEST(TEST_RETURN, bit, op, c_op, val); \ +} while (0) + +#define TEST_ARGS(bit, op, init, ret, expect, args...) \ +do { \ + atomic##bit##_set(&v, init); \ + BUG_ON(atomic##bit##_##op(&v, ##args) != ret); \ + BUG_ON(atomic##bit##_read(&v) != expect); \ +} while (0) + +#define XCHG_FAMILY_TEST(bit, init, new) \ +do { \ + FAMILY_TEST(TEST_ARGS, bit, xchg, init, init, new, new); \ +} while (0) + +#define CMPXCHG_FAMILY_TEST(bit, init, new, wrong) \ +do { \ + FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \ + init, init, new, init, new); \ + FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \ + init, init, init, wrong, new); \ +} while (0) + +#define INC_RETURN_FAMILY_TEST(bit, i) \ +do { \ + FAMILY_TEST(TEST_ARGS, bit, inc_return, \ + i, (i) + one, (i) + one); \ +} while (0) + +#define DEC_RETURN_FAMILY_TEST(bit, i) \ +do { \ + FAMILY_TEST(TEST_ARGS, bit, dec_return, \ + i, (i) - one, (i) - one); \ +} while (0) + static __init void test_atomic(void) { int v0 = 0xaaa31337;
@@ -45,6 +104,18 @@ static __init void test_atomic(void) TEST(, and, &=, v1); TEST(, xor, ^=, v1); TEST(, andnot, &= ~, v1); + + RETURN_FAMILY_TEST(, add_return, +=, onestwos); + RETURN_FAMILY_TEST(, add_return, +=, -one); + RETURN_FAMILY_TEST(, sub_return, -=, onestwos); + RETURN_FAMILY_TEST(, sub_return, -=, -one); + + INC_RETURN_FAMILY_TEST(, v0); + DEC_RETURN_FAMILY_TEST(, v0); + + XCHG_FAMILY_TEST(, v0, v1); + CMPXCHG_FAMILY_TEST(, v0, v1, onestwos); + }#define INIT (c) do { atomic64_set(&v, c); r = c; } while (0)
@@ -74,25 +145,10 @@ static __init void test_atomic64(void) TEST(64, xor, ^=, v1); TEST(64, andnot, &= ~, v1); -INIT (v0); - r += onestwos; -BUG_ON (atomic64_add_return (onestwos, &v) != r); -BUG_ON (v.counter != r); - -INIT (v0); - r += -one; -BUG_ON (atomic64_add_return (-one, &v) != r); -BUG_ON (v.counter != r); - -INIT (v0); - r -= onestwos; -BUG_ON (atomic64_sub_return (onestwos, &v) != r); -BUG_ON (v.counter != r); - -INIT (v0); - r -= -one; -BUG_ON (atomic64_sub_return (-one, &v) != r); -BUG_ON (v.counter != r); + RETURN_FAMILY_TEST(64, add_return, +=, onestwos); + RETURN_FAMILY_TEST(64, add_return, +=, -one); + RETURN_FAMILY_TEST(64, sub_return, -=, onestwos); + RETURN_FAMILY_TEST(64, sub_return, -=, -one);INIT (v0);atomic64_inc (&v);
@@ -100,33 +156,15 @@ static __init void test_atomic64(void)BUG_ON (v.counter != r);INIT (v0); - r += one; -BUG_ON (atomic64_inc_return (&v) != r); -BUG_ON (v.counter != r); - -INIT (v0);atomic64_dec (&v); r -= one;BUG_ON (v.counter != r); -INIT (v0); - r -= one; -BUG_ON (atomic64_dec_return (&v) != r); -BUG_ON (v.counter != r); - -INIT (v0); -BUG_ON (atomic64_xchg (&v, v1) != v0); - r = v1; -BUG_ON (v.counter != r); - -INIT (v0); -BUG_ON (atomic64_cmpxchg (&v, v0, v1) != v0); - r = v1; -BUG_ON (v.counter != r); +INC_RETURN_FAMILY_TEST (64 , v0); +DEC_RETURN_FAMILY_TEST (64 , v0); -INIT (v0); -BUG_ON (atomic64_cmpxchg (&v, v2, v1) != v0); -BUG_ON (v.counter != r); +XCHG_FAMILY_TEST (64 , v0, v1); +CMPXCHG_FAMILY_TEST (64 , v0, v1, v2);INIT (v0);BUG_ON (atomic64_add_unless (&v, one, v0));
Signature · 1 line
2.6.2
PATCH v5 3/6 atomics: Allow architectures to define their own __atomic_op_* helpers
In reply to Boqun FengSome architectures may have their special barriers for acquire, release and fence semantics, so that general memory barriers(smp_mb__*_atomic()) in the default __atomic_op_*() may be too strong, so allow architectures to define their own helpers which can overwrite the default helpers.
--- include/linux/atomic.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
include/linux/atomic.h +10 -0
@@ -43,20 +43,29 @@ static inline int atomic_read_ctrl(const atomic_t *v)* The idea here is to build acquire/release variants by adding explicit * barriers on top of the relaxed variant. In the case where the relaxed * variant is already fully ordered, no additional barriers are needed. +* +* Besides, if an arch has a special barrier for acquire/release, it could +* implement its own __atomic_op_* and use the same framework for building +* variants */ +#ifndef __atomic_op_acquire #define __atomic_op_acquire(op, args...) \ ({ \ typeof(op##_relaxed(args)) __ret = op##_relaxed(args); \ smp_mb__after_atomic(); \ __ret; \ }) +#endif +#ifndef __atomic_op_release #define __atomic_op_release(op, args...) \ ({ \ smp_mb__before_atomic(); \ op##_relaxed(args); \ }) +#endif +#ifndef __atomic_op_fence #define __atomic_op_fence(op, args...) \ ({ \ typeof(op##_relaxed(args)) __ret; \
@@ -65,6 +74,7 @@ static inline int atomic_read_ctrl(const atomic_t *v)smp_mb__after_atomic (); \ __ret; \ }) +#endif /* atomic_add_return_relaxed */ #ifndef atomic_add_return_relaxed
Signature · 1 line
2.6.2
PATCH v5 4/6 powerpc: atomic: Implement atomic{,64}_*_return_* variants
In reply to Boqun FengOn powerpc, acquire and release semantics can be achieved with lightweight barriers("lwsync" and "ctrl+isync"), which can be used to implement __atomic_op_{acquire,release}.
For release semantics, since we only need to ensure all memory accesses that issue before must take effects before the -store- part of the atomics, "lwsync" is what we only need. On the platform without "lwsync", "sync" should be used. Therefore, smp_lwsync() is used here.
For acquire semantics, "lwsync" is what we only need for the similar reason. However on the platform without "lwsync", we can use "isync" rather than "sync" as an acquire barrier. Therefore in __atomic_op_acquire() we use PPC_ACQUIRE_BARRIER, which is barrier() on UP, "lwsync" if available and "isync" otherwise.
Implement atomic{,64}_{add,sub,inc,dec}_return_relaxed, and build other variants with these helpers.
--- arch/powerpc/include/asm/atomic.h | 107 +++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 42 deletions(-)
arch/powerpc/include/asm/atomic.h +65 -42
@@ -12,6 +12,24 @@#define ATOMIC_INIT (i) { (i) } +/* +* Since *_return_relaxed and {cmp}xchg_relaxed are implemented with +* a "bne-" instruction at the end, so an isync is enough as a acquire barrier +* on the platform without lwsync. +*/ +#define __atomic_op_acquire(op, args...) \ +({ \ + typeof(op##_relaxed(args)) __ret = op##_relaxed(args); \ + __asm__ __volatile__(PPC_ACQUIRE_BARRIER "" : : : "memory"); \ + __ret; \ +}) + +#define __atomic_op_release(op, args...) \ +({ \ + smp_lwsync(); \ + op##_relaxed(args); \ +}) +static __inline__int atomic_read (const atomic_t *v) {int t;
@@ -42,27 +60,27 @@ static __inline__ void atomic_##op(int a, atomic_t *v) \ : "cc"); \ } \ -#define ATOMIC_OP_RETURN (op, asm_op) \ -static __inline__ int atomic_##op##_return(int a, atomic_t *v) \ +#define ATOMIC_OP_RETURN_RELAXED (op, asm_op) \ +static inline int atomic_##op##_return_relaxed(int a, atomic_t *v) \ { \ int t; \ \ __asm__ __volatile__( \ - PPC_ATOMIC_ENTRY_BARRIER \ -"1: lwarx %0,0,%2 # atomic_" #op "_return\n" \ - #asm_op " %0,%1,%0\n" \ - PPC405_ERR77(0,%2) \ -" stwcx. %0,0,%2 \n" \ +"1: lwarx %0,0,%3 # atomic_" #op "_return_relaxed\n" \ + #asm_op " %0,%2,%0\n" \ + PPC405_ERR77(0, %3) \ +" stwcx. %0,0,%3\n" \ " bne- 1b\n" \ - PPC_ATOMIC_EXIT_BARRIER \ - : "=&r" (t) \ + : "=&r" (t), "+m" (v->counter) \ : "r" (a), "r" (&v->counter) \ - : "cc", "memory"); \ + : "cc"); \ \ return t; \ } -#define ATOMIC_OPS (op, asm_op) ATOMIC_OP(op, asm_op) ATOMIC_OP_RETURN(op, asm_op) +#define ATOMIC_OPS (op, asm_op) \ + ATOMIC_OP(op, asm_op) \ + ATOMIC_OP_RETURN_RELAXED(op, asm_op) ATOMIC_OPS(add, add)ATOMIC_OPS (sub, subf)
@@ -71,8 +89,11 @@ ATOMIC_OP(and, and) ATOMIC_OP(or, or) ATOMIC_OP(xor, xor) +#define atomic_add_return_relaxed atomic_add_return_relaxed +#define atomic_sub_return_relaxed atomic_sub_return_relaxed +#undef ATOMIC_OPS -#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP_RETURN_RELAXED#undef ATOMIC_OP#define atomic_add_negative (a, v) (atomic_add_return((a), (v)) < 0)
@@ -92,21 +113,19 @@ static __inline__ void atomic_inc(atomic_t *v) : "cc", "xer"); } -static __inline__int atomic_inc_return (atomic_t *v) +static __inline__int atomic_inc_return_relaxed (atomic_t *v) {int t; __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER -"1: lwarx %0,0,%1 # atomic_inc_return\n\ +"1: lwarx %0,0,%1 # atomic_inc_return_relaxed\n\ addic %0,%0,1\n" PPC405_ERR77(0,%1) " stwcx. %0,0,%1 \n\ bne- 1b" -PPC_ATOMIC_EXIT_BARRIER : "=&r" (t) : "r" (&v->counter) - : "cc", "xer", "memory"); + : "cc", "xer");return t; }
@@ -136,25 +155,26 @@ static __inline__ void atomic_dec(atomic_t *v) : "cc", "xer"); } -static __inline__int atomic_dec_return (atomic_t *v) +static __inline__int atomic_dec_return_relaxed (atomic_t *v) {int t; __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER -"1: lwarx %0,0,%1 # atomic_dec_return\n\ +"1: lwarx %0,0,%1 # atomic_dec_return_relaxed\n\ addic %0,%0,-1\n" PPC405_ERR77(0,%1) " stwcx. %0,0,%1\n\ bne- 1b" -PPC_ATOMIC_EXIT_BARRIER : "=&r" (t) : "r" (&v->counter) - : "cc", "xer", "memory"); + : "cc", "xer");return t; } +#define atomic_inc_return_relaxed atomic_inc_return_relaxed +#define atomic_dec_return_relaxed atomic_dec_return_relaxed +#define atomic_cmpxchg (v, o, n) (cmpxchg(&((v)->counter), (o), (n)))#define atomic_xchg (v, new) (xchg(&((v)->counter), new))
@@ -285,26 +305,27 @@ static __inline__ void atomic64_##op(long a, atomic64_t *v) \ : "cc"); \ } -#define ATOMIC64_OP_RETURN (op, asm_op) \ -static __inline__ long atomic64_##op##_return(long a, atomic64_t *v) \ +#define ATOMIC64_OP_RETURN_RELAXED (op, asm_op) \ +static inline long \ +atomic64_##op##_return_relaxed(long a, atomic64_t *v) \ { \ long t; \ \ __asm__ __volatile__( \ - PPC_ATOMIC_ENTRY_BARRIER \ -"1: ldarx %0,0,%2 # atomic64_" #op "_return\n" \ - #asm_op " %0,%1,%0\n" \ -" stdcx. %0,0,%2 \n" \ +"1: ldarx %0,0,%3 # atomic64_" #op "_return_relaxed\n" \ + #asm_op " %0,%2,%0\n" \ +" stdcx. %0,0,%3\n" \ " bne- 1b\n" \ - PPC_ATOMIC_EXIT_BARRIER \ - : "=&r" (t) \ + : "=&r" (t), "+m" (v->counter) \ : "r" (a), "r" (&v->counter) \ - : "cc", "memory"); \ + : "cc"); \ \ return t; \ } -#define ATOMIC64_OPS (op, asm_op) ATOMIC64_OP(op, asm_op) ATOMIC64_OP_RETURN(op, asm_op) +#define ATOMIC64_OPS (op, asm_op) \ + ATOMIC64_OP(op, asm_op) \ + ATOMIC64_OP_RETURN_RELAXED(op, asm_op) ATOMIC64_OPS(add, add)ATOMIC64_OPS (sub, subf)
@@ -312,8 +333,11 @@ ATOMIC64_OP(and, and) ATOMIC64_OP(or, or) ATOMIC64_OP(xor, xor) -#undef ATOMIC64_OPS -#undef ATOMIC64_OP_RETURN +#define atomic64_add_return_relaxed atomic64_add_return_relaxed +#define atomic64_sub_return_relaxed atomic64_sub_return_relaxed + +#undef ATOPIC64_OPS +#undef ATOMIC64_OP_RETURN_RELAXED#undef ATOMIC64_OP#define atomic64_add_negative (a, v) (atomic64_add_return((a), (v)) < 0)
@@ -332,20 +356,18 @@ static __inline__ void atomic64_inc(atomic64_t *v) : "cc", "xer"); } -static __inline__long atomic64_inc_return (atomic64_t *v) +static __inline__long atomic64_inc_return_relaxed (atomic64_t *v) {long t; __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER "1: ldarx %0,0,%1 # atomic64_inc_return\n\ addic %0,%0,1\n\ stdcx. %0,0,%1 \n\ bne- 1b" -PPC_ATOMIC_EXIT_BARRIER :"=&r" (t) :"r" (&v->counter) - :"cc" ,"xer" ,"memory" ); + :"cc" ,"xer" );return t; }
@@ -374,24 +396,25 @@ static __inline__ void atomic64_dec(atomic64_t *v) : "cc", "xer"); } -static __inline__long atomic64_dec_return (atomic64_t *v) +static __inline__long atomic64_dec_return_relaxed (atomic64_t *v) {long t; __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER "1: ldarx %0,0,%1 # atomic64_dec_return\n\ addic %0,%0,-1\n\ stdcx. %0,0,%1\n\ bne- 1b" -PPC_ATOMIC_EXIT_BARRIER :"=&r" (t) :"r" (&v->counter) - :"cc" ,"xer" ,"memory" ); + :"cc" ,"xer" );return t; } +#define atomic64_inc_return_relaxed atomic64_inc_return_relaxed +#define atomic64_dec_return_relaxed atomic64_dec_return_relaxed +#define atomic64_sub_and_test (a, v) (atomic64_sub_return((a), (v)) == 0)#define atomic64_dec_and_test (v) (atomic64_dec_return((v)) == 0)
Signature · 1 line
2.6.2
PATCH v5 5/6 powerpc: atomic: Implement xchg_* and atomic{,64}_xchg_* variants
In reply to Boqun FengImplement xchg_relaxed and atomic{,64}_xchg_relaxed, based on these _relaxed variants, release/acquire variants and fully ordered versions can be built.
Note that xchg_relaxed and atomic_{,64}_xchg_relaxed are not compiler barriers.
--- arch/powerpc/include/asm/atomic.h | 2 ++ arch/powerpc/include/asm/cmpxchg.h | 69 +++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 39 deletions(-)
arch/powerpc/include/asm/atomic.h +2 -0
@@ -177,6 +177,7 @@ static __inline__ int atomic_dec_return_relaxed(atomic_t *v)#define atomic_cmpxchg (v, o, n) (cmpxchg(&((v)->counter), (o), (n)))#define atomic_xchg (v, new) (xchg(&((v)->counter), new)) +#define atomic_xchg_relaxed (v, new) xchg_relaxed(&((v)->counter), (new))/** * __atomic_add_unless - add unless the number is a given value
@@ -444,6 +445,7 @@ static __inline__ long atomic64_dec_if_positive(atomic64_t *v)#define atomic64_cmpxchg (v, o, n) (cmpxchg(&((v)->counter), (o), (n)))#define atomic64_xchg (v, new) (xchg(&((v)->counter), new)) +#define atomic64_xchg_relaxed (v, new) xchg_relaxed(&((v)->counter), (new))/** * atomic64_add_unless - add unless the number is a given value
arch/powerpc/include/asm/cmpxchg.h +30 -39
156 lines not on this page · raw message
Signature · 1 line
2.6.2
PATCH v5 6/6 powerpc: atomic: Implement cmpxchg{,64}_* and atomic{,64}_cmpxchg_* variants
In reply to Boqun FengImplement cmpxchg{,64}_relaxed and atomic{,64}_cmpxchg_relaxed, based on which _release variants can be built.
To avoid superfluous barriers in _acquire variants, we implement these operations with assembly code rather use __atomic_op_acquire() to build them automatically.
For the same reason, we keep the assembly implementation of fully ordered cmpxchg operations.
However, we don't do the similar for _release, because that will require putting barriers in the middle of ll/sc loops, which is probably a bad idea.
Note cmpxchg{,64}_relaxed and atomic{,64}_cmpxchg_relaxed are not compiler barriers.
--- arch/powerpc/include/asm/atomic.h | 10 +++ arch/powerpc/include/asm/cmpxchg.h | 149 ++++++++++++++++++++++++++++++++++++- 2 files changed, 158 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/atomic.h +10 -0
@@ -176,6 +176,11 @@ static __inline__ int atomic_dec_return_relaxed(atomic_t *v)#define atomic_dec_return_relaxed atomic_dec_return_relaxed#define atomic_cmpxchg (v, o, n) (cmpxchg(&((v)->counter), (o), (n))) +#define atomic_cmpxchg_relaxed (v, o, n) \ + cmpxchg_relaxed(&((v)->counter), (o), (n)) +#define atomic_cmpxchg_acquire (v, o, n) \ + cmpxchg_acquire(&((v)->counter), (o), (n)) +#define atomic_xchg (v, new) (xchg(&((v)->counter), new))#define atomic_xchg_relaxed (v, new) xchg_relaxed(&((v)->counter), (new))
@@ -444,6 +449,11 @@ static __inline__ long atomic64_dec_if_positive(atomic64_t *v) }#define atomic64_cmpxchg (v, o, n) (cmpxchg(&((v)->counter), (o), (n))) +#define atomic64_cmpxchg_relaxed (v, o, n) \ + cmpxchg_relaxed(&((v)->counter), (o), (n)) +#define atomic64_cmpxchg_acquire (v, o, n) \ + cmpxchg_acquire(&((v)->counter), (o), (n)) +#define atomic64_xchg (v, new) (xchg(&((v)->counter), new))#define atomic64_xchg_relaxed (v, new) xchg_relaxed(&((v)->counter), (new))
arch/powerpc/include/asm/cmpxchg.h +148 -1
179 lines not on this page · raw message
Signature · 1 line
2.6.2
Re: [PATCH tip/locking/core v5 1/6] powerpc: atomic: Make _return atomics and *{cmp}xchg fully ordered
In reply to Boqun FengQuoted 27 lines · Boqun Feng
This patch fixes two problems to make value-returning atomics and {cmp}xchg fully ordered on PPC.
According to memory-barriers.txt:
Quoted 4 lines
Any atomic operation that modifies some state in memory and returns information about the state (old or new) implies an SMP-conditional general memory barrier (
smp_mb()) on each side of the actual operation ...which means these operations should be fully ordered. However on PPC,
PPC_ATOMIC_ENTRY_BARRIERis the barrier before the actual operation, which is currently "lwsync" if SMP=y. The leading "lwsync" can not guarantee fully ordered atomics, according to Paul Mckenney:https://lkml.org/lkml/2015/10/14/970
To fix this, we define
PPC_ATOMIC_ENTRY_BARRIERas "sync" to guarantee the fully-ordered semantics.This also makes futex atomics fully ordered, which can avoid possible memory ordering problems if userspace code relies on futex system call for fully ordered semantics.
Another thing to fix is that xchg, cmpxchg and their atomic{64}_ versions are currently RELEASE+ACQUIRE, which are not fully ordered.
Quoted 7 lines
So also replace
PPC_RELEASE_BARRIERandPPC_ACQUIRE_BARRIERwithPPC_ATOMIC_ENTRY_BARRIERandPPC_ATOMIC_EXIT_BARRIERin __{cmp,}xchg_{u32,u64} respectively to guarantee fully ordered semantics of atomic{,64}_{cmp,}xchg() and {cmp,}xchg(), as a complement of commitb97021f85517("powerpc: Fixatomic_xxx_returnbarrier semantics").Cc <[email protected]> # 3.4+
Hmm.. I use the same Cc tag as v4, seems my git(2.6.2) send-email has a weird behavior of composing Cc address?
I will resend this one soon, sorry ;-(
Regards,
Boqun
Quoted 94 lines
Signed-off-by Boqun Feng <[email protected]>---
Michael, I also change
PPC_ATOMIC_ENTRY_BARRIERas "sync" if SMP=y in this version , which is different from the previous one, so request for a new ack. Thank you ;-)arch/powerpc/include/asm/cmpxchg.h | 16 ++++++++-------- arch/powerpc/include/asm/synch.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-)arch/powerpc/include/asm/cmpxchg.h +8
-8@@ -18,12 +18,12 @@ __xchg_u32(volatile void *p, unsigned long val)unsigned long prev; __asm__ __volatile__( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: lwarx %0,0,%2 \n" PPC405_ERR77(0,%2)" stwcx. %3,0,%2 \n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER : "=&r" (prev), "+m" (*(volatile unsigned int *)p) : "r" (p), "r" (val) : "cc", "memory");@@ -61,12 +61,12 @@ __xchg_u64(volatile void *p, unsigned long val)unsigned long prev; __asm__ __volatile__( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: ldarx %0,0,%2 \n" PPC405_ERR77(0,%2)" stdcx. %3,0,%2 \n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER : "=&r" (prev), "+m" (*(volatile unsigned long *)p) : "r" (p), "r" (val) : "cc", "memory");@@ -151,14 +151,14 @@ __cmpxchg_u32(volatile unsigned int *p, unsigned long old, unsigned long new)unsigned int prev; __asm__ __volatile__ ( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: lwarx %0,0,%2 # __cmpxchg_u32\n\ cmpw 0,%0,%3\n\ bne- 2f\n" PPC405_ERR77(0,%2)" stwcx. %4,0,%2\n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER "\n\ 2:" : "=&r" (prev), "+m" (*p)@@ -197,13 +197,13 @@ __cmpxchg_u64(volatile unsigned long *p, unsigned long old, unsigned long new)unsigned long prev; __asm__ __volatile__ ( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: ldarx %0,0,%2 # __cmpxchg_u64\n\ cmpd 0,%0,%3\n\ bne- 2f\n\ stdcx. %4,0,%2\n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER "\n\ 2:" : "=&r" (prev), "+m" (*p)arch/powerpc/include/asm/synch.h +1
-1@@ -44,7 +44,7 @@ static inline void isync(void)MAKE_LWSYNC_SECTION_ENTRY (97 , __lwsync_fixup);#define PPC_ACQUIRE_BARRIER "\n" stringify_in_c(__PPC_ACQUIRE_BARRIER)#define PPC_RELEASE_BARRIER stringify_in_c(LWSYNC) "\n" -#define PPC_ATOMIC_ENTRY_BARRIER "\n" stringify_in_c(LWSYNC) "\n" +#define PPC_ATOMIC_ENTRY_BARRIER "\n" stringify_in_c(sync) "\n"#define PPC_ATOMIC_EXIT_BARRIER "\n" stringify_in_c(sync) "\n"#else #define PPC_ACQUIRE_BARRIER Signature · 1 line
2.6.2
PATCH RESEND v5 1/6 powerpc: atomic: Make _return atomics and *{cmp}xchg fully ordered
In reply to Boqun FengThis patch fixes two problems to make value-returning atomics and {cmp}xchg fully ordered on PPC.
According to memory-barriers.txt:
Quoted 4 lines
Any atomic operation that modifies some state in memory and returns information about the state (old or new) implies an SMP-conditional general memory barrier (
smp_mb()) on each side of the actual operation ...
which means these operations should be fully ordered. However on PPC, PPC_ATOMIC_ENTRY_BARRIER is the barrier before the actual operation, which is currently "lwsync" if SMP=y. The leading "lwsync" can not guarantee fully ordered atomics, according to Paul Mckenney:
https://lkml.org/lkml/2015/10/14/970
To fix this, we define PPC_ATOMIC_ENTRY_BARRIER as "sync" to guarantee the fully-ordered semantics.
This also makes futex atomics fully ordered, which can avoid possible memory ordering problems if userspace code relies on futex system call for fully ordered semantics.
Another thing to fix is that xchg, cmpxchg and their atomic{64}_ versions are currently RELEASE+ACQUIRE, which are not fully ordered.
So also replace PPC_RELEASE_BARRIER and PPC_ACQUIRE_BARRIER with PPC_ATOMIC_ENTRY_BARRIER and PPC_ATOMIC_EXIT_BARRIER in __{cmp,}xchg_{u32,u64} respectively to guarantee fully ordered semantics of atomic{,64}_{cmp,}xchg() and {cmp,}xchg(), as a complement of commit b97021f85517 ("powerpc: Fix atomic_xxx_return barrier semantics").
---
Michael, I also change PPC_ATOMIC_ENTRY_BARRIER as "sync" if SMP=y in this version , which is different from the previous one, so request for a new ack. Thank you ;-)
arch/powerpc/include/asm/cmpxchg.h | 16 ++++++++-------- arch/powerpc/include/asm/synch.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-)
arch/powerpc/include/asm/cmpxchg.h +8 -8
@@ -18,12 +18,12 @@ __xchg_u32(volatile void *p, unsigned long val)unsigned long prev; __asm__ __volatile__( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: lwarx %0,0,%2 \n" PPC405_ERR77(0,%2)" stwcx. %3,0,%2 \n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER : "=&r" (prev), "+m" (*(volatile unsigned int *)p) : "r" (p), "r" (val) : "cc", "memory");
@@ -61,12 +61,12 @@ __xchg_u64(volatile void *p, unsigned long val)unsigned long prev; __asm__ __volatile__( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: ldarx %0,0,%2 \n" PPC405_ERR77(0,%2)" stdcx. %3,0,%2 \n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER : "=&r" (prev), "+m" (*(volatile unsigned long *)p) : "r" (p), "r" (val) : "cc", "memory");
@@ -151,14 +151,14 @@ __cmpxchg_u32(volatile unsigned int *p, unsigned long old, unsigned long new)unsigned int prev; __asm__ __volatile__ ( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: lwarx %0,0,%2 # __cmpxchg_u32\n\ cmpw 0,%0,%3\n\ bne- 2f\n" PPC405_ERR77(0,%2)" stwcx. %4,0,%2\n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER "\n\ 2:" : "=&r" (prev), "+m" (*p)
@@ -197,13 +197,13 @@ __cmpxchg_u64(volatile unsigned long *p, unsigned long old, unsigned long new)unsigned long prev; __asm__ __volatile__ ( -PPC_RELEASE_BARRIER +PPC_ATOMIC_ENTRY_BARRIER "1: ldarx %0,0,%2 # __cmpxchg_u64\n\ cmpd 0,%0,%3\n\ bne- 2f\n\ stdcx. %4,0,%2\n\ bne- 1b" -PPC_ACQUIRE_BARRIER +PPC_ATOMIC_EXIT_BARRIER "\n\ 2:" : "=&r" (prev), "+m" (*p)
arch/powerpc/include/asm/synch.h +1 -1
@@ -44,7 +44,7 @@ static inline void isync(void)MAKE_LWSYNC_SECTION_ENTRY (97 , __lwsync_fixup);#define PPC_ACQUIRE_BARRIER "\n" stringify_in_c(__PPC_ACQUIRE_BARRIER)#define PPC_RELEASE_BARRIER stringify_in_c(LWSYNC) "\n" -#define PPC_ATOMIC_ENTRY_BARRIER "\n" stringify_in_c(LWSYNC) "\n" +#define PPC_ATOMIC_ENTRY_BARRIER "\n" stringify_in_c(sync) "\n"#define PPC_ATOMIC_EXIT_BARRIER "\n" stringify_in_c(sync) "\n"#else #define PPC_ACQUIRE_BARRIER
Signature · 1 line
2.6.2
Michael Ellerman
reviewerRe: [RESEND, tip/locking/core, v5, 1/6] powerpc: atomic: Make _return atomics and *{cmp}xchg fully ordered
Quoted 2 lines · Boqun Feng
This patch fixes two problems to make value-returning atomics and {cmp}xchg fully ordered on PPC.
Hi Boqun,
Can you please split this into two patches. One that does the cmpxchg change and one that changes PPC_ATOMIC_ENTRY_BARRIER.
Also given how pervasive this change is I'd like to take it via the powerpc next tree, so can you please send this patch (which will be two after you split it) as powerpc patches. And the rest can go via tip?
cheers
Re: [RESEND, tip/locking/core, v5, 1/6] powerpc: atomic: Make _return atomics and *{cmp}xchg fully ordered
Quoted 9 lines · Michael Ellerman
Quoted 2 lines · Boqun Feng
This patch fixes two problems to make value-returning atomics and {cmp}xchg fully ordered on PPC.
Hi Boqun,
Can you please split this into two patches. One that does the cmpxchg change and one that changes
PPC_ATOMIC_ENTRY_BARRIER.
OK, make sense ;-)
Quoted 4 lines
Also given how pervasive this change is I'd like to take it via the powerpc next tree, so can you please send this patch (which will be two after you split it) as powerpc patches. And the rest can go via tip?
One problem is that patch 5 will remove __xchg_u32 and __xchg_64 entirely, which are modified in this patch(patch 1), so there will be some conflicts if two branch get merged, I think.
Alternative way is that all this series go to powerpc next tree as most of the dependent patches are already there. I just need to remove inc/dec related code and resend them when appropriate. Besides, I can pull patch 2 out and send it as a tip patch because it's general code and no one depends on this in this series.
To summerize:
patch 1(split to two), 3, 4(remove inc/dec implementation), 5, 6 sent as powerpc patches for powerpc next, patch 2(unmodified) sent as tip patch for locking/core.
Peter and Michael, this works for you both?
Regards,
Re: [RESEND, tip/locking/core, v5, 1/6] powerpc: atomic: Make _return atomics and *{cmp}xchg fully ordered
Quoted 28 lines · Boqun Feng
Quoted 9 lines · Michael Ellerman
Quoted 2 lines · Boqun Feng
This patch fixes two problems to make value-returning atomics and {cmp}xchg fully ordered on PPC.
Hi Boqun,
Can you please split this into two patches. One that does the cmpxchg change and one that changes
PPC_ATOMIC_ENTRY_BARRIER.OK, make sense ;-)
Quoted 4 lines
Also given how pervasive this change is I'd like to take it via the powerpc next tree, so can you please send this patch (which will be two after you split it) as powerpc patches. And the rest can go via tip?
One problem is that patch 5 will remove
__xchg_u32and__xchg_64entirely, which are modified in this patch(patch 1), so there will be some conflicts if two branch get merged, I think.Alternative way is that all this series go to powerpc next tree as most of the dependent patches are already there. I just need to remove inc/dec related code and resend them when appropriate. Besides, I can pull patch 2 out and send it as a tip patch because it's general code and no one depends on this in this series.
Quoted 8 lines
To summerize:
patch 1(split to two), 3, 4(remove inc/dec implementation), 5, 6 sent as powerpc patches for powerpc next, patch 2(unmodified) sent as tip patch for locking/core.
Peter and Michael, this works for you both?
Thoughts? ;-)
Regards,
Boqun
Re: [RESEND, tip/locking/core, v5, 1/6] powerpc: atomic: Make _return atomics and *{cmp}xchg fully ordered
Quoted 31 lines · Boqun Feng
Quoted 30 lines · Boqun Feng
Quoted 9 lines · Michael Ellerman
Quoted 2 lines · Boqun Feng
This patch fixes two problems to make value-returning atomics and {cmp}xchg fully ordered on PPC.
Hi Boqun,
Can you please split this into two patches. One that does the cmpxchg change and one that changes
PPC_ATOMIC_ENTRY_BARRIER.OK, make sense ;-)
Quoted 4 lines
Also given how pervasive this change is I'd like to take it via the powerpc next tree, so can you please send this patch (which will be two after you split it) as powerpc patches. And the rest can go via tip?
One problem is that patch 5 will remove
__xchg_u32and__xchg_64entirely, which are modified in this patch(patch 1), so there will be some conflicts if two branch get merged, I think.Alternative way is that all this series go to powerpc next tree as most of the dependent patches are already there. I just need to remove inc/dec related code and resend them when appropriate. Besides, I can pull patch 2 out and send it as a tip patch because it's general code and no one depends on this in this series.
To summerize:
Quoted 9 lines
Quoted 6 lines
patch 1(split to two), 3, 4(remove inc/dec implementation), 5, 6 sent as powerpc patches for powerpc next, patch 2(unmodified) sent as tip patch for locking/core.
Peter and Michael, this works for you both?
Thoughts? ;-)
Peter and Michael, I will split patch 1 to two and send them as patches for powerpc next first. The rest of this can wait util we are on the same page of where they'd better go.
Regards,
Boqun
Re: [RESEND, tip/locking/core, v5, 1/6] powerpc: atomic: Make _return atomics and *{cmp}xchg fully ordered
Quoted 10 lines · Boqun Feng
Quoted 10 lines · Boqun Feng
Quoted 8 lines
To summerize:
patch 1(split to two), 3, 4(remove inc/dec implementation), 5, 6 sent as powerpc patches for powerpc next, patch 2(unmodified) sent as tip patch for locking/core.
Peter and Michael, this works for you both?
Quoted 7 lines
Quoted 2 lines
Thoughts? ;-)
Peter and Michael, I will split patch 1 to two and send them as patches for powerpc next first. The rest of this can wait util we are on the same page of where they'd better go.
I'm about to send patch 2(adding trivial tests) as a patch for the tip tree, and rest of this series will be patches for powerpc next.
Will, AFAIK, you are currently working on variants on arm64, right? I wonder whether you depend on patch 3 (allow archictures to provide self-defined __atomic_op_*), if so I can also send patch 3 as a patch for tip tree and wait until it merged into powerpc next to send the rest.
Thanks and Best Regards,
Boqun
Re: [RESEND, tip/locking/core, v5, 1/6] powerpc: atomic: Make _return atomics and *{cmp}xchg fully ordered
Quoted 5 lines · Boqun Feng
Will, AFAIK, you are currently working on variants on arm64, right? I wonder whether you depend on patch 3 (allow archictures to provide self-defined __atomic_op_*), if so I can also send patch 3 as a patch for tip tree and wait until it merged into powerpc next to send the rest.
The arm64 patches are all queued in the arm64 tree and have been sitting in -next for a while. They don't dependent on anything else.
Will