Commit 2ef1fe7a authored by Vineet Gupta's avatar Vineet Gupta Committed by Luis Henriques

ARC: add smp barriers around atomics per Documentation/atomic_ops.txt

commit 2576c28e upstream.

 - arch_spin_lock/unlock were lacking the ACQUIRE/RELEASE barriers
   Since ARCv2 only provides load/load, store/store and all/all, we need
   the full barrier

 - LLOCK/SCOND based atomics, bitops, cmpxchg, which return modified
   values were lacking the explicit smp barriers.

 - Non LLOCK/SCOND varaints don't need the explicit barriers since that
   is implicity provided by the spin locks used to implement the
   critical section (the spin lock barriers in turn are also fixed in
   this commit as explained above

Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: default avatarVineet Gupta <vgupta@synopsys.com>
Signed-off-by: default avatarLuis Henriques <luis.henriques@canonical.com>
parent dcfd7615
...@@ -45,6 +45,12 @@ static inline int atomic_##op##_return(int i, atomic_t *v) \ ...@@ -45,6 +45,12 @@ static inline int atomic_##op##_return(int i, atomic_t *v) \
{ \ { \
unsigned int temp; \ unsigned int temp; \
\ \
/* \
* Explicit full memory barrier needed before/after as \
* LLOCK/SCOND thmeselves don't provide any such semantics \
*/ \
smp_mb(); \
\
__asm__ __volatile__( \ __asm__ __volatile__( \
"1: llock %0, [%1] \n" \ "1: llock %0, [%1] \n" \
" " #asm_op " %0, %0, %2 \n" \ " " #asm_op " %0, %0, %2 \n" \
...@@ -54,6 +60,8 @@ static inline int atomic_##op##_return(int i, atomic_t *v) \ ...@@ -54,6 +60,8 @@ static inline int atomic_##op##_return(int i, atomic_t *v) \
: "r"(&v->counter), "ir"(i) \ : "r"(&v->counter), "ir"(i) \
: "cc"); \ : "cc"); \
\ \
smp_mb(); \
\
return temp; \ return temp; \
} }
...@@ -107,6 +115,9 @@ static inline int atomic_##op##_return(int i, atomic_t *v) \ ...@@ -107,6 +115,9 @@ static inline int atomic_##op##_return(int i, atomic_t *v) \
unsigned long flags; \ unsigned long flags; \
unsigned long temp; \ unsigned long temp; \
\ \
/* \
* spin lock/unlock provides the needed smp_mb() before/after \
*/ \
atomic_ops_lock(flags); \ atomic_ops_lock(flags); \
temp = v->counter; \ temp = v->counter; \
temp c_op i; \ temp c_op i; \
...@@ -144,9 +155,19 @@ ATOMIC_OP(and, &=, and) ...@@ -144,9 +155,19 @@ ATOMIC_OP(and, &=, and)
#define __atomic_add_unless(v, a, u) \ #define __atomic_add_unless(v, a, u) \
({ \ ({ \
int c, old; \ int c, old; \
\
/* \
* Explicit full memory barrier needed before/after as \
* LLOCK/SCOND thmeselves don't provide any such semantics \
*/ \
smp_mb(); \
\
c = atomic_read(v); \ c = atomic_read(v); \
while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c)\ while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c)\
c = old; \ c = old; \
\
smp_mb(); \
\
c; \ c; \
}) })
......
...@@ -105,6 +105,12 @@ static inline int test_and_set_bit(unsigned long nr, volatile unsigned long *m) ...@@ -105,6 +105,12 @@ static inline int test_and_set_bit(unsigned long nr, volatile unsigned long *m)
if (__builtin_constant_p(nr)) if (__builtin_constant_p(nr))
nr &= 0x1f; nr &= 0x1f;
/*
* Explicit full memory barrier needed before/after as
* LLOCK/SCOND themselves don't provide any such semantics
*/
smp_mb();
__asm__ __volatile__( __asm__ __volatile__(
"1: llock %0, [%2] \n" "1: llock %0, [%2] \n"
" bset %1, %0, %3 \n" " bset %1, %0, %3 \n"
...@@ -114,6 +120,8 @@ static inline int test_and_set_bit(unsigned long nr, volatile unsigned long *m) ...@@ -114,6 +120,8 @@ static inline int test_and_set_bit(unsigned long nr, volatile unsigned long *m)
: "r"(m), "ir"(nr) : "r"(m), "ir"(nr)
: "cc"); : "cc");
smp_mb();
return (old & (1 << nr)) != 0; return (old & (1 << nr)) != 0;
} }
...@@ -127,6 +135,8 @@ test_and_clear_bit(unsigned long nr, volatile unsigned long *m) ...@@ -127,6 +135,8 @@ test_and_clear_bit(unsigned long nr, volatile unsigned long *m)
if (__builtin_constant_p(nr)) if (__builtin_constant_p(nr))
nr &= 0x1f; nr &= 0x1f;
smp_mb();
__asm__ __volatile__( __asm__ __volatile__(
"1: llock %0, [%2] \n" "1: llock %0, [%2] \n"
" bclr %1, %0, %3 \n" " bclr %1, %0, %3 \n"
...@@ -136,6 +146,8 @@ test_and_clear_bit(unsigned long nr, volatile unsigned long *m) ...@@ -136,6 +146,8 @@ test_and_clear_bit(unsigned long nr, volatile unsigned long *m)
: "r"(m), "ir"(nr) : "r"(m), "ir"(nr)
: "cc"); : "cc");
smp_mb();
return (old & (1 << nr)) != 0; return (old & (1 << nr)) != 0;
} }
...@@ -149,6 +161,8 @@ test_and_change_bit(unsigned long nr, volatile unsigned long *m) ...@@ -149,6 +161,8 @@ test_and_change_bit(unsigned long nr, volatile unsigned long *m)
if (__builtin_constant_p(nr)) if (__builtin_constant_p(nr))
nr &= 0x1f; nr &= 0x1f;
smp_mb();
__asm__ __volatile__( __asm__ __volatile__(
"1: llock %0, [%2] \n" "1: llock %0, [%2] \n"
" bxor %1, %0, %3 \n" " bxor %1, %0, %3 \n"
...@@ -158,6 +172,8 @@ test_and_change_bit(unsigned long nr, volatile unsigned long *m) ...@@ -158,6 +172,8 @@ test_and_change_bit(unsigned long nr, volatile unsigned long *m)
: "r"(m), "ir"(nr) : "r"(m), "ir"(nr)
: "cc"); : "cc");
smp_mb();
return (old & (1 << nr)) != 0; return (old & (1 << nr)) != 0;
} }
...@@ -256,6 +272,9 @@ test_and_clear_bit(unsigned long nr, volatile unsigned long *m) ...@@ -256,6 +272,9 @@ test_and_clear_bit(unsigned long nr, volatile unsigned long *m)
if (__builtin_constant_p(nr)) if (__builtin_constant_p(nr))
nr &= 0x1f; nr &= 0x1f;
/*
* spin lock/unlock provide the needed smp_mb() before/after
*/
bitops_lock(flags); bitops_lock(flags);
old = *m; old = *m;
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#define __ASM_ARC_CMPXCHG_H #define __ASM_ARC_CMPXCHG_H
#include <linux/types.h> #include <linux/types.h>
#include <asm/barrier.h>
#include <asm/smp.h> #include <asm/smp.h>
#ifdef CONFIG_ARC_HAS_LLSC #ifdef CONFIG_ARC_HAS_LLSC
...@@ -19,6 +21,12 @@ __cmpxchg(volatile void *ptr, unsigned long expected, unsigned long new) ...@@ -19,6 +21,12 @@ __cmpxchg(volatile void *ptr, unsigned long expected, unsigned long new)
{ {
unsigned long prev; unsigned long prev;
/*
* Explicit full memory barrier needed before/after as
* LLOCK/SCOND thmeselves don't provide any such semantics
*/
smp_mb();
__asm__ __volatile__( __asm__ __volatile__(
"1: llock %0, [%1] \n" "1: llock %0, [%1] \n"
" brne %0, %2, 2f \n" " brne %0, %2, 2f \n"
...@@ -31,6 +39,8 @@ __cmpxchg(volatile void *ptr, unsigned long expected, unsigned long new) ...@@ -31,6 +39,8 @@ __cmpxchg(volatile void *ptr, unsigned long expected, unsigned long new)
"r"(new) /* can't be "ir". scond can't take LIMM for "b" */ "r"(new) /* can't be "ir". scond can't take LIMM for "b" */
: "cc", "memory"); /* so that gcc knows memory is being written here */ : "cc", "memory"); /* so that gcc knows memory is being written here */
smp_mb();
return prev; return prev;
} }
...@@ -43,6 +53,9 @@ __cmpxchg(volatile void *ptr, unsigned long expected, unsigned long new) ...@@ -43,6 +53,9 @@ __cmpxchg(volatile void *ptr, unsigned long expected, unsigned long new)
int prev; int prev;
volatile unsigned long *p = ptr; volatile unsigned long *p = ptr;
/*
* spin lock/unlock provide the needed smp_mb() before/after
*/
atomic_ops_lock(flags); atomic_ops_lock(flags);
prev = *p; prev = *p;
if (prev == expected) if (prev == expected)
...@@ -78,12 +91,16 @@ static inline unsigned long __xchg(unsigned long val, volatile void *ptr, ...@@ -78,12 +91,16 @@ static inline unsigned long __xchg(unsigned long val, volatile void *ptr,
switch (size) { switch (size) {
case 4: case 4:
smp_mb();
__asm__ __volatile__( __asm__ __volatile__(
" ex %0, [%1] \n" " ex %0, [%1] \n"
: "+r"(val) : "+r"(val)
: "r"(ptr) : "r"(ptr)
: "memory"); : "memory");
smp_mb();
return val; return val;
} }
return __xchg_bad_pointer(); return __xchg_bad_pointer();
......
...@@ -22,24 +22,46 @@ static inline void arch_spin_lock(arch_spinlock_t *lock) ...@@ -22,24 +22,46 @@ static inline void arch_spin_lock(arch_spinlock_t *lock)
{ {
unsigned int tmp = __ARCH_SPIN_LOCK_LOCKED__; unsigned int tmp = __ARCH_SPIN_LOCK_LOCKED__;
/*
* This smp_mb() is technically superfluous, we only need the one
* after the lock for providing the ACQUIRE semantics.
* However doing the "right" thing was regressing hackbench
* so keeping this, pending further investigation
*/
smp_mb();
__asm__ __volatile__( __asm__ __volatile__(
"1: ex %0, [%1] \n" "1: ex %0, [%1] \n"
" breq %0, %2, 1b \n" " breq %0, %2, 1b \n"
: "+&r" (tmp) : "+&r" (tmp)
: "r"(&(lock->slock)), "ir"(__ARCH_SPIN_LOCK_LOCKED__) : "r"(&(lock->slock)), "ir"(__ARCH_SPIN_LOCK_LOCKED__)
: "memory"); : "memory");
/*
* ACQUIRE barrier to ensure load/store after taking the lock
* don't "bleed-up" out of the critical section (leak-in is allowed)
* http://www.spinics.net/lists/kernel/msg2010409.html
*
* ARCv2 only has load-load, store-store and all-all barrier
* thus need the full all-all barrier
*/
smp_mb();
} }
static inline int arch_spin_trylock(arch_spinlock_t *lock) static inline int arch_spin_trylock(arch_spinlock_t *lock)
{ {
unsigned int tmp = __ARCH_SPIN_LOCK_LOCKED__; unsigned int tmp = __ARCH_SPIN_LOCK_LOCKED__;
smp_mb();
__asm__ __volatile__( __asm__ __volatile__(
"1: ex %0, [%1] \n" "1: ex %0, [%1] \n"
: "+r" (tmp) : "+r" (tmp)
: "r"(&(lock->slock)) : "r"(&(lock->slock))
: "memory"); : "memory");
smp_mb();
return (tmp == __ARCH_SPIN_LOCK_UNLOCKED__); return (tmp == __ARCH_SPIN_LOCK_UNLOCKED__);
} }
...@@ -47,12 +69,22 @@ static inline void arch_spin_unlock(arch_spinlock_t *lock) ...@@ -47,12 +69,22 @@ static inline void arch_spin_unlock(arch_spinlock_t *lock)
{ {
unsigned int tmp = __ARCH_SPIN_LOCK_UNLOCKED__; unsigned int tmp = __ARCH_SPIN_LOCK_UNLOCKED__;
/*
* RELEASE barrier: given the instructions avail on ARCv2, full barrier
* is the only option
*/
smp_mb();
__asm__ __volatile__( __asm__ __volatile__(
" ex %0, [%1] \n" " ex %0, [%1] \n"
: "+r" (tmp) : "+r" (tmp)
: "r"(&(lock->slock)) : "r"(&(lock->slock))
: "memory"); : "memory");
/*
* superfluous, but keeping for now - see pairing version in
* arch_spin_lock above
*/
smp_mb(); smp_mb();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment