arch atomic 'cleanup'
Subsystems (16)
Peter Zijlstra
maintainercover arch atomic 'cleanup'
This series continues the arch atomic rework started with the smp_mb__ interface cleanup.
In this series we (mostly) reduce the atomic implementations by eliminating repetition through use of CPP macros.
A future series will use these macros to implement more atomic ops. With these macros we can end up with more atomic ops while the total LoC still shrinks.
Furthermore, rewrite the asm-generic/atomic implementations to require less and provide more.
This series is compile tested on a number of archs, but only boot tested on x86_64.
XXX: someone should try and unify all the hash locked atomic implementations, there's about 4 of them in the tree.
--- arch/alpha/include/asm/atomic.h | 225 ++++-------- arch/arc/include/asm/atomic.h | 194 +++------- arch/arm/include/asm/atomic.h | 313 ++++++----------- arch/arm64/include/asm/atomic.h | 213 ++++------- arch/avr32/include/asm/atomic.h | 96 ++--- arch/cris/include/arch-v10/arch/system.h | 2 arch/cris/include/asm/atomic.h | 57 +-- arch/hexagon/include/asm/atomic.h | 68 ++- arch/ia64/include/asm/atomic.h | 182 ++++----- arch/m32r/include/asm/atomic.h | 151 +++----- arch/m68k/include/asm/atomic.h | 119 +++--- arch/metag/include/asm/atomic_lnkget.h | 121 ++---- arch/metag/include/asm/atomic_lock1.h | 76 +--- arch/mips/include/asm/atomic.h | 565 ++++++++++--------------------- arch/mn10300/include/asm/atomic.h | 129 ++----- arch/parisc/include/asm/atomic.h | 121 ++++-- arch/powerpc/include/asm/atomic.h | 216 ++++------- arch/sh/include/asm/atomic-grb.h | 121 ++---- arch/sh/include/asm/atomic-irq.h | 62 +-- arch/sh/include/asm/atomic-llsc.h | 101 ++--- arch/sparc/include/asm/atomic_32.h | 19 - arch/sparc/include/asm/atomic_64.h | 49 +- arch/sparc/include/asm/barrier_32.h | 1 arch/sparc/include/asm/processor.h | 2 arch/sparc/kernel/smp_64.c | 2 arch/sparc/lib/atomic32.c | 25 - arch/sparc/lib/atomic_64.S | 175 ++++----- arch/sparc/lib/ksyms.c | 25 - arch/x86/include/asm/atomic.h | 15 arch/xtensa/include/asm/atomic.h | 231 ++++-------- include/asm-generic/atomic.h | 174 ++++----- include/asm-generic/atomic64.h | 20 - lib/atomic64.c | 83 +--- 33 files changed, 1609 insertions(+), 2344 deletions(-)
Peter Zijlstra
maintainerPATCH 1/20 x86: Kill atomic_or_long()
There are no users, kill it.
--- arch/x86/include/asm/atomic.h | 15 --------------- 1 file changed, 15 deletions(-)
arch/x86/include/asm/atomic.h +0 -15
@@ -219,21 +219,6 @@ static inline short int atomic_inc_shortreturn *v; } -#ifdef CONFIG_X86_64 -/** -* atomic_or_long - OR of two long integers -* @v1: pointer to type unsigned long -* @v2: pointer to type unsigned long -* -* Atomically ORs @v1 and @v2 -* Returns the result of the OR -*/ -static inline void atomic_or_long (unsigned long *v1,unsigned long v2) -{ - asm(LOCK_PREFIX "orq %1, %0" :"+m" (*v1) :"r" (v2)); -} -#endif -/* These are x86-specific, used by some header files */ #define atomic_clear_mask (mask, addr) \ asm volatile(LOCK_PREFIX "andl %0,%1" \
Peter Zijlstra
maintainerPATCH 2/20 arch,alpha: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/alpha/include/asm/atomic.h | 225 +++++++++++++++------------------------- 1 file changed, 86 insertions(+), 139 deletions(-)
arch/alpha/include/asm/atomic.h +86 -139
@@ -29,145 +29,92 @@* branch back to restart the operation. */ -static __inline__void atomic_add (int i,atomic_t * v) -{ -unsigned long temp; - __asm__ __volatile__( -"1: ldl_l %0,%1\n" -" addl %0,%2,%0\n" -" stl_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter)); -} - -static __inline__void atomic64_add (long i,atomic64_t * v) -{ -unsigned long temp; - __asm__ __volatile__( -"1: ldq_l %0,%1\n" -" addq %0,%2,%0\n" -" stq_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter)); -} - -static __inline__void atomic_sub (int i,atomic_t * v) -{ -unsigned long temp; - __asm__ __volatile__( -"1: ldl_l %0,%1\n" -" subl %0,%2,%0\n" -" stl_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter)); -} - -static __inline__void atomic64_sub (long i,atomic64_t * v) -{ -unsigned long temp; - __asm__ __volatile__( -"1: ldq_l %0,%1\n" -" subq %0,%2,%0\n" -" stq_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter)); -} - - -/* -* Same as above, but return the result value -*/ -static inline int atomic_add_return (int i,atomic_t *v) -{ -long temp, result; -smp_mb (); - __asm__ __volatile__( -"1: ldl_l %0,%1\n" -" addl %0,%3,%2\n" -" addl %0,%3,%0\n" -" stl_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter),"=&r" (result) - :"Ir" (i),"m" (v->counter) :"memory" ); -smp_mb (); -return result; -} - -static __inline__long atomic64_add_return (long i,atomic64_t * v) -{ -long temp, result; -smp_mb (); - __asm__ __volatile__( -"1: ldq_l %0,%1\n" -" addq %0,%3,%2\n" -" addq %0,%3,%0\n" -" stq_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter),"=&r" (result) - :"Ir" (i),"m" (v->counter) :"memory" ); -smp_mb (); -return result; -} - -static __inline__long atomic_sub_return (int i,atomic_t * v) -{ -long temp, result; -smp_mb (); - __asm__ __volatile__( -"1: ldl_l %0,%1\n" -" subl %0,%3,%2\n" -" subl %0,%3,%0\n" -" stl_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter),"=&r" (result) - :"Ir" (i),"m" (v->counter) :"memory" ); -smp_mb (); -return result; -} - -static __inline__long atomic64_sub_return (long i,atomic64_t * v) -{ -long temp, result; -smp_mb (); - __asm__ __volatile__( -"1: ldq_l %0,%1\n" -" subq %0,%3,%2\n" -" subq %0,%3,%0\n" -" stq_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter),"=&r" (result) - :"Ir" (i),"m" (v->counter) :"memory" ); -smp_mb (); -return result; -} +#define ATOMIC_OP (op) \ +static __inline__ void atomic_##op(int i, atomic_t * v) \ +{ \ + unsigned long temp; \ + __asm__ __volatile__( \ + "1: ldl_l %0,%1\n" \ + " " #op "l %0,%2,%0\n" \ + " stl_c %0,%1\n" \ + " beq %0,2f\n" \ + ".subsection 2\n" \ + "2: br 1b\n" \ + ".previous" \ + :"=&r" (temp), "=m" (v->counter) \ + :"Ir" (i), "m" (v->counter)); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + long temp, result; \ + smp_mb(); \ + __asm__ __volatile__( \ + "1: ldl_l %0,%1\n" \ + " " #op "l %0,%3,%2\n" \ + " " #op "l %0,%3,%0\n" \ + " stl_c %0,%1\n" \ + " beq %0,2f\n" \ + ".subsection 2\n" \ + "2: br 1b\n" \ + ".previous" \ + :"=&r" (temp), "=m" (v->counter), "=&r" (result) \ + :"Ir" (i), "m" (v->counter) : "memory"); \ + smp_mb(); \ + return result; \ +} + +#define ATOMIC64_OP (op) \ +static __inline__ void atomic64_##op(long i, atomic64_t * v) \ +{ \ + unsigned long temp; \ + __asm__ __volatile__( \ + "1: ldq_l %0,%1\n" \ + " " #op "q %0,%2,%0\n" \ + " stq_c %0,%1\n" \ + " beq %0,2f\n" \ + ".subsection 2\n" \ + "2: br 1b\n" \ + ".previous" \ + :"=&r" (temp), "=m" (v->counter) \ + :"Ir" (i), "m" (v->counter)); \ +} \ + +#define ATOMIC64_OP_RETURN (op) \ +static __inline__ long atomic64_##op##_return(long i, atomic64_t * v) \ +{ \ + long temp, result; \ + smp_mb(); \ + __asm__ __volatile__( \ + "1: ldq_l %0,%1\n" \ + " " #op "q %0,%3,%2\n" \ + " " #op "q %0,%3,%0\n" \ + " stq_c %0,%1\n" \ + " beq %0,2f\n" \ + ".subsection 2\n" \ + "2: br 1b\n" \ + ".previous" \ + :"=&r" (temp), "=m" (v->counter), "=&r" (result) \ + :"Ir" (i), "m" (v->counter) : "memory"); \ + smp_mb(); \ + return result; \ +} + +#define ATOMIC_OPS (opg) \ + ATOMIC_OP(opg) \ + ATOMIC_OP_RETURN(opg) \ + ATOMIC64_OP(opg) \ + ATOMIC64_OP_RETURN(opg) + +ATOMIC_OPS(add) +ATOMIC_OPS(sub) + +#undef ATOMIC_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OP +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP#define atomic64_cmpxchg (v, old, new) (cmpxchg(&((v)->counter), old, new))#define atomic64_xchg (v, new) (xchg(&((v)->counter), new))
Peter Zijlstra
maintainerPATCH 3/20 arch,arc: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/arc/include/asm/atomic.h | 194 ++++++++++++++---------------------------- 1 file changed, 68 insertions(+), 126 deletions(-)
arch/arc/include/asm/atomic.h +68 -126
@@ -25,79 +25,36 @@#define atomic_set (v, i) (((v)->counter) = (i)) -static inline void atomic_add (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" add %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp)/* Early clobber, to prevent reg reuse */ - :"r" (&v->counter),"ir" (i) - :"cc" ); -} - -static inline void atomic_sub (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" sub %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (&v->counter),"ir" (i) - :"cc" ); -} - -/* add and also return the new value */ -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" add %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (&v->counter),"ir" (i) - :"cc" ); - -return temp; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" sub %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (&v->counter),"ir" (i) - :"cc" ); - -return temp; -} - -static inline void atomic_clear_mask (unsigned long mask,unsigned long *addr) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" bic %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (addr),"ir" (mask) - :"cc" ); +#define ATOMIC_OP(op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned int temp; \ + \ + __asm__ __volatile__( \ + "1: llock %0, [%1] \n" \ + " " #asm_op " %0, %0, %2 \n" \ + " scond %0, [%1] \n" \ + " bnz 1b \n" \ + : "=&r"(temp) /* Early clobber, to prevent reg reuse */ \ + : "r"(&v->counter), "ir"(i) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned int temp; \ + \ + __asm__ __volatile__( \ + "1: llock %0, [%1] \n" \ + " " #asm_op " %0, %0, %2 \n" \ + " scond %0, [%1] \n" \ + " bnz 1b \n" \ + : "=&r"(temp) \ + : "r"(&v->counter), "ir"(i) \ + : "cc"); \ + \ + return temp; \ }#else /* !CONFIG_ARC_HAS_LLSC */
@@ -126,6 +83,7 @@ static inline void atomic_set(atomic_t * v->counter = i; atomic_ops_unlock(flags); } +#endif /*
@@ -133,63 +91,47 @@ static inline void atomic_set(atomic_t ** Locking would change to irq-disabling only (UP) and spinlocks (SMP) */ -static inline void atomic_add (int i,atomic_t *v) -{ -unsigned long flags; - -atomic_ops_lock (flags); - v->counter += i; -atomic_ops_unlock (flags); -} - -static inline void atomic_sub (int i,atomic_t *v) -{ -unsigned long flags; - -atomic_ops_lock (flags); - v->counter -= i; -atomic_ops_unlock (flags); -} - -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long flags; -unsigned long temp; - -atomic_ops_lock (flags); - temp = v->counter; - temp += i; - v->counter = temp; -atomic_ops_unlock (flags); - -return temp; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long flags; -unsigned long temp; - -atomic_ops_lock (flags); - temp = v->counter; - temp -= i; - v->counter = temp; -atomic_ops_unlock (flags); - -return temp; -} - -static inline void atomic_clear_mask (unsigned long mask,unsigned long *addr) -{ -unsigned long flags; - -atomic_ops_lock (flags); - *addr &= ~mask; -atomic_ops_unlock (flags); +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + \ + atomic_ops_lock(flags); \ + v->counter c_op i; \ + atomic_ops_unlock(flags); \ +} + +#define ATOMIC_OP_RETURN (op, c_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + unsigned long temp; \ + \ + atomic_ops_lock(flags); \ + temp = v->counter; \ + temp c_op i; \ + v->counter = temp; \ + atomic_ops_unlock(flags); \ + \ + return temp; \ }#endif /* !CONFIG_ARC_HAS_LLSC */ +#define ATOMIC_OPS (op, c_op, asm_op) \ + ATOMIC_OP(op, c_op, asm_op) \ + ATOMIC_OP_RETURN(op, c_op, asm_op) + +ATOMIC_OPS(add, +=, add) +ATOMIC_OPS(sub, -=, sub) +ATOMIC_OP(and, &=, and) + +#define atomic_clear_mask (mask, v) atomic_and(~(mask), (v)) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP +/** * __atomic_add_unless - add unless the number is a given value * @v: pointer of type atomic_t
Peter Zijlstra
maintainerPATCH 4/20 arch,arm: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires the asm_op because of eor.
--- arch/arm/include/asm/atomic.h | 313 +++++++++++++++++------------------------- 1 file changed, 127 insertions(+), 186 deletions(-)
arch/arm/include/asm/atomic.h +127 -186
@@ -37,84 +37,47 @@* store exclusive to ensure that these are atomic. We may loop * to ensure that the update happens. */ -static inline void atomic_add (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - -prefetchw (&v->counter); - __asm__ __volatile__("@ atomic_add\n" -"1: ldrex %0, [%3]\n" -" add %0, %0, %4\n" -" strex %1, %0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"Ir" (i) - :"cc" ); -} - -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - -smp_mb (); -prefetchw (&v->counter); - - __asm__ __volatile__("@ atomic_add_return\n" -"1: ldrex %0, [%3]\n" -" add %0, %0, %4\n" -" strex %1, %0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"Ir" (i) - :"cc" ); - -smp_mb (); - -return result; -} - -static inline void atomic_sub (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - -prefetchw (&v->counter); - __asm__ __volatile__("@ atomic_sub\n" -"1: ldrex %0, [%3]\n" -" sub %0, %0, %4\n" -" strex %1, %0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"Ir" (i) - :"cc" ); -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - -smp_mb (); -prefetchw (&v->counter); - - __asm__ __volatile__("@ atomic_sub_return\n" -"1: ldrex %0, [%3]\n" -" sub %0, %0, %4\n" -" strex %1, %0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"Ir" (i) - :"cc" ); -smp_mb (); - -return result; +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + prefetchw(&v->counter); \ + __asm__ __volatile__("@ atomic_" #op "\n" \ +"1: ldrex %0, [%3]\n" \ +" " #asm_op " %0, %0, %4\n" \ +" strex %1, %0, [%3]\n" \ +" teq %1, #0\n" \ +" bne 1b" \ + : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ + : "r" (&v->counter), "Ir" (i) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + smp_mb(); \ + prefetchw(&v->counter); \ + \ + __asm__ __volatile__("@ atomic_" #op "_return\n" \ +"1: ldrex %0, [%3]\n" \ +" " #asm_op " %0, %0, %4\n" \ +" strex %1, %0, [%3]\n" \ +" teq %1, #0\n" \ +" bne 1b" \ + : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ + : "r" (&v->counter), "Ir" (i) \ + : "cc"); \ + \ + smp_mb(); \ + \ + return result; \ }static inline int atomic_cmpxchg (atomic_t *ptr,int old,int new)
@@ -174,33 +137,29 @@ static inline int __atomic_add_unless(at#error SMP not supported on pre-ARMv6 CPUs#endif -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long flags; -int val; - -raw_local_irq_save (flags); - val = v->counter; - v->counter = val += i; -raw_local_irq_restore (flags); - -return val; +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + v->counter c_op i; \ + raw_local_irq_restore(flags); \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + int val; \ + \ + raw_local_irq_save(flags); \ + v->counter c_op i; \ + val = v->counter; \ + raw_local_irq_restore(flags); \ + \ + return val; \ } -#define atomic_add (i, v) (void) atomic_add_return(i, v) - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long flags; -int val; - -raw_local_irq_save (flags); - val = v->counter; - v->counter = val -= i; -raw_local_irq_restore (flags); - -return val; -} -#define atomic_sub (i, v) (void) atomic_sub_return(i, v)static inline int atomic_cmpxchg (atomic_t *v,int old,int new) {
@@ -228,6 +187,17 @@ static inline int __atomic_add_unless(at#endif /* __LINUX_ARM_ARCH__ */ +#define ATOMIC_OPS (op, c_op, asm_op) \ + ATOMIC_OP(op, c_op, asm_op) \ + ATOMIC_OP_RETURN(op, c_op, asm_op) + +ATOMIC_OPS(add, +=, add) +ATOMIC_OPS(sub, -=, sub) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP +#define atomic_xchg (v, new) (xchg(&((v)->counter), new))#define atomic_inc (v) atomic_add(1, v)
@@ -300,89 +270,60 @@ static inline void atomic64_set(atomic64 }#endif -static inline void atomic64_add (long long i,atomic64_t *v) -{ -long long result; -unsigned long tmp; - -prefetchw (&v->counter); - __asm__ __volatile__("@ atomic64_add\n" -"1: ldrexd %0, %H0, [%3]\n" -" adds %Q0, %Q0, %Q4\n" -" adc %R0, %R0, %R4\n" -" strexd %1, %0, %H0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"r" (i) - :"cc" ); -} - -static inline long long atomic64_add_return (long long i,atomic64_t *v) -{ -long long result; -unsigned long tmp; - -smp_mb (); -prefetchw (&v->counter); - - __asm__ __volatile__("@ atomic64_add_return\n" -"1: ldrexd %0, %H0, [%3]\n" -" adds %Q0, %Q0, %Q4\n" -" adc %R0, %R0, %R4\n" -" strexd %1, %0, %H0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"r" (i) - :"cc" ); - -smp_mb (); - -return result; -} - -static inline void atomic64_sub (long long i,atomic64_t *v) -{ -long long result; -unsigned long tmp; - -prefetchw (&v->counter); - __asm__ __volatile__("@ atomic64_sub\n" -"1: ldrexd %0, %H0, [%3]\n" -" subs %Q0, %Q0, %Q4\n" -" sbc %R0, %R0, %R4\n" -" strexd %1, %0, %H0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"r" (i) - :"cc" ); -} - -static inline long long atomic64_sub_return (long long i,atomic64_t *v) -{ -long long result; -unsigned long tmp; - -smp_mb (); -prefetchw (&v->counter); - - __asm__ __volatile__("@ atomic64_sub_return\n" -"1: ldrexd %0, %H0, [%3]\n" -" subs %Q0, %Q0, %Q4\n" -" sbc %R0, %R0, %R4\n" -" strexd %1, %0, %H0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"r" (i) - :"cc" ); - -smp_mb (); - -return result; -} +#define ATOMIC64_OP (op, op1, op2) \ +static inline void atomic64_##op(long long i, atomic64_t *v) \ +{ \ + long long result; \ + unsigned long tmp; \ + \ + prefetchw(&v->counter); \ + __asm__ __volatile__("@ atomic64_" #op "\n" \ +"1: ldrexd %0, %H0, [%3]\n" \ +" " #op1 " %Q0, %Q0, %Q4\n" \ +" " #op2 " %R0, %R0, %R4\n" \ +" strexd %1, %0, %H0, [%3]\n" \ +" teq %1, #0\n" \ +" bne 1b" \ + : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ + : "r" (&v->counter), "r" (i) \ + : "cc"); \ +} \ + +#define ATOMIC64_OP_RETURN (op, op1, op2) \ +static inline long long atomic64_##op##_return(long long i, atomic64_t *v) \ +{ \ + long long result; \ + unsigned long tmp; \ + \ + smp_mb(); \ + prefetchw(&v->counter); \ + \ + __asm__ __volatile__("@ atomic64_" #op "_return\n" \ +"1: ldrexd %0, %H0, [%3]\n" \ +" " #op1 " %Q0, %Q0, %Q4\n" \ +" " #op2 " %R0, %R0, %R4\n" \ +" strexd %1, %0, %H0, [%3]\n" \ +" teq %1, #0\n" \ +" bne 1b" \ + : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ + : "r" (&v->counter), "r" (i) \ + : "cc"); \ + \ + smp_mb(); \ + \ + return result; \ +} + +#define ATOMIC64_OPS (op, op1, op2) \ + ATOMIC64_OP(op, op1, op2) \ + ATOMIC64_OP_RETURN(op, op1, op2) + +ATOMIC64_OPS(add, adds, adc) +ATOMIC64_OPS(sub, subs, sbc) + +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OPstatic inline long long atomic64_cmpxchg (atomic64_t *ptr,long long old,long long new)
Peter Zijlstra
maintainerPATCH 5/20 arch,arm64: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires the asm_op due to eor.
--- arch/arm64/include/asm/atomic.h | 213 ++++++++++++++++------------------------ 1 file changed, 88 insertions(+), 125 deletions(-)
arch/arm64/include/asm/atomic.h +88 -125
@@ -43,69 +43,51 @@* store exclusive to ensure that these are atomic. We may loop * to ensure that the update happens. */ -static inline void atomic_add (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - - asmvolatile ("// atomic_add\n" -"1: ldxr %w0, %2\n" -" add %w0, %w0, %w3\n" -" stxr %w1, %w0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i)); -} - -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - - asmvolatile ("// atomic_add_return\n" -"1: ldxr %w0, %2\n" -" add %w0, %w0, %w3\n" -" stlxr %w1, %w0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i) - :"memory" ); -smp_mb (); -return result; -} - -static inline void atomic_sub (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - - asmvolatile ("// atomic_sub\n" -"1: ldxr %w0, %2\n" -" sub %w0, %w0, %w3\n" -" stxr %w1, %w0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i)); -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - - asmvolatile ("// atomic_sub_return\n" -"1: ldxr %w0, %2\n" -" sub %w0, %w0, %w3\n" -" stlxr %w1, %w0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i) - :"memory" ); - -smp_mb (); -return result; -} +#define ATOMIC_OP (op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + asm volatile("// atomic_" #op "\n" \ +"1: ldxr %w0, %2\n" \ +" " #asm_op " %w0, %w0, %w3\n" \ +" stxr %w1, %w0, %2\n" \ +" cbnz %w1, 1b" \ + : "=&r" (result), "=&r" (tmp), "+Q" (v->counter) \ + : "Ir" (i)); \ +} \ + +#define ATOMIC_OP_RETURN (op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + asm volatile("// atomic_" #op "_return\n" \ +"1: ldxr %w0, %2\n" \ +" " #asm_op " %w0, %w0, %w3\n" \ +" stlxr %w1, %w0, %2\n" \ +" cbnz %w1, 1b" \ + : "=&r" (result), "=&r" (tmp), "+Q" (v->counter) \ + : "Ir" (i) \ + : "memory"); \ + \ + smp_mb(); \ + return result; \ +} + +#define ATOMIC_OPS (op, asm_op) \ + ATOMIC_OP(op, asm_op) \ + ATOMIC_OP_RETURN(op, asm_op) + +ATOMIC_OPS(add, add) +ATOMIC_OPS(sub, sub) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OPstatic inline int atomic_cmpxchg (atomic_t *ptr,int old,int new) {
@@ -160,69 +142,50 @@ static inline int __atomic_add_unless(at#define atomic64_read (v) (*(volatile long long *)&(v)->counter)#define atomic64_set (v,i) (((v)->counter) = (i)) -static inline void atomic64_add (u64 i,atomic64_t *v) -{ -long result; -unsigned long tmp; - - asmvolatile ("// atomic64_add\n" -"1: ldxr %0, %2\n" -" add %0, %0, %3\n" -" stxr %w1, %0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i)); -} - -static inline long atomic64_add_return (long i,atomic64_t *v) -{ -long result; -unsigned long tmp; - - asmvolatile ("// atomic64_add_return\n" -"1: ldxr %0, %2\n" -" add %0, %0, %3\n" -" stlxr %w1, %0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i) - :"memory" ); - -smp_mb (); -return result; -} - -static inline void atomic64_sub (u64 i,atomic64_t *v) -{ -long result; -unsigned long tmp; - - asmvolatile ("// atomic64_sub\n" -"1: ldxr %0, %2\n" -" sub %0, %0, %3\n" -" stxr %w1, %0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i)); -} - -static inline long atomic64_sub_return (long i,atomic64_t *v) -{ -long result; -unsigned long tmp; - - asmvolatile ("// atomic64_sub_return\n" -"1: ldxr %0, %2\n" -" sub %0, %0, %3\n" -" stlxr %w1, %0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i) - :"memory" ); - -smp_mb (); -return result; -} +#define ATOMIC64_OP (op, asm_op) \ +static inline void atomic64_##op(long i, atomic64_t *v) \ +{ \ + long result; \ + unsigned long tmp; \ + \ + asm volatile("// atomic64_" #op "\n" \ +"1: ldxr %0, %2\n" \ +" " #asm_op " %0, %0, %3\n" \ +" stxr %w1, %0, %2\n" \ +" cbnz %w1, 1b" \ + : "=&r" (result), "=&r" (tmp), "+Q" (v->counter) \ + : "Ir" (i)); \ +} \ + +#define ATOMIC64_OP_RETURN (op, asm_op) \ +static inline long atomic64_##op##_return(long i, atomic64_t *v) \ +{ \ + long result; \ + unsigned long tmp; \ + \ + asm volatile("// atomic64_" #op "_return\n" \ +"1: ldxr %0, %2\n" \ +" " #asm_op " %0, %0, %3\n" \ +" stlxr %w1, %0, %2\n" \ +" cbnz %w1, 1b" \ + : "=&r" (result), "=&r" (tmp), "+Q" (v->counter) \ + : "Ir" (i) \ + : "memory"); \ + \ + smp_mb(); \ + return result; \ +} + +#define ATOMIC64_OPS (op, asm_op) \ + ATOMIC64_OP(op, asm_op) \ + ATOMIC64_OP_RETURN(op, asm_op) + +ATOMIC64_OPS(add, add) +ATOMIC64_OPS(sub, sub) + +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OPstatic inline long atomic64_cmpxchg (atomic64_t *ptr,long old,long new) {
Peter Zijlstra
maintainerPATCH 7/20 arch,cris: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/cris/include/arch-v10/arch/system.h | 2 - arch/cris/include/asm/atomic.h | 57 +++++++++++++------------------ 2 files changed, 25 insertions(+), 34 deletions(-)
arch/cris/include/arch-v10/arch/system.h +0 -2
@@ -36,8 +36,6 @@ static inline unsigned long _get_base(chreturn 0 ; } -#define nop () __asm__ __volatile__ ("nop"); -#define xchg (ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))#define tas (ptr) (xchg((ptr),1))
arch/cris/include/asm/atomic.h +25 -32
68 lines not on this page · raw message
Peter Zijlstra
maintainerPATCH 8/20 arch,hexagon: Fold atomic_ops
In reply to Peter ZijlstraOK, no LoC saved in this case because the !return variants were defined in terms of the return ops. Still do it because this also prepares for easy addition of new ops.
--- arch/hexagon/include/asm/atomic.h | 68 ++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 31 deletions(-)
arch/hexagon/include/asm/atomic.h +37 -31
@@ -81,41 +81,47 @@ static inline int atomic_cmpxchg(atomic_return __oldval; } -static inline int atomic_add_return (int i,atomic_t *v) -{ -int output; - - __asm__ __volatile__ ( -"1: %0 = memw_locked(%1);\n" -" %0 = add(%0,%2);\n" -" memw_locked(%1,P3)=%0;\n" -" if !P3 jump 1b;\n" - :"=&r" (output) - :"r" (&v->counter),"r" (i) - :"memory" ,"p3" - ); -return output; - +#define ATOMIC_OP (op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int output; \ + \ + __asm__ __volatile__ ( \ + "1: %0 = memw_locked(%1);\n" \ + " %0 = "#op "(%0,%2);\n" \ + " memw_locked(%1,P3)=%0;\n" \ + " if !P3 jump 1b;\n" \ + : "=&r" (output) \ + : "r" (&v->counter), "r" (i) \ + : "memory", "p3" \ + ); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int output; \ + \ + __asm__ __volatile__ ( \ + "1: %0 = memw_locked(%1);\n" \ + " %0 = "#op "(%0,%2);\n" \ + " memw_locked(%1,P3)=%0;\n" \ + " if !P3 jump 1b;\n" \ + : "=&r" (output) \ + : "r" (&v->counter), "r" (i) \ + : "memory", "p3" \ + ); \ + return output; \ } -#define atomic_add (i, v) atomic_add_return(i, (v)) +#define ATOMIC_OPS (op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int output; - __asm__ __volatile__ ( -"1: %0 = memw_locked(%1);\n" -" %0 = sub(%0,%2);\n" -" memw_locked(%1,P3)=%0\n" -" if !P3 jump 1b;\n" - :"=&r" (output) - :"r" (&v->counter),"r" (i) - :"memory" ,"p3" - ); -return output; -} +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -#define atomic_sub (i, v) atomic_sub_return(i, (v)) +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP/** * __atomic_add_unless - add unless the number is a given value
Peter Zijlstra
maintainerPATCH 9/20 arch,ia64: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/ia64/include/asm/atomic.h | 182 ++++++++++++++++++----------------------- 1 file changed, 83 insertions(+), 99 deletions(-)
arch/ia64/include/asm/atomic.h +83 -99
@@ -27,61 +27,93 @@#define atomic_set (v,i) (((v)->counter) = (i))#define atomic64_set (v,i) (((v)->counter) = (i)) -static __inline__int -ia64_atomic_add (int i,atomic_t *v) -{ -__s32 old, new; -CMPXCHG_BUGCHECK_DECL - - do { -CMPXCHG_BUGCHECK (v); - old =atomic_read (v); - new = old + i; - }while (ia64_cmpxchg (acq, v, old, new,sizeof (atomic_t)) != old); -return new; +#define ATOMIC_OP (op, c_op) \ +static __inline__ int \ +ia64_atomic_##op (int i, atomic_t *v) \ +{ \ + __s32 old, new; \ + CMPXCHG_BUGCHECK_DECL \ + \ + do { \ + CMPXCHG_BUGCHECK(v); \ + old = atomic_read(v); \ + new = old c_op i; \ + } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old); \ + return new; \ } -static __inline__long -ia64_atomic64_add (__s64 i,atomic64_t *v) -{ -__s64 old, new; -CMPXCHG_BUGCHECK_DECL +ATOMIC_OP(add, +) +ATOMIC_OP(sub, -) - do { -CMPXCHG_BUGCHECK (v); - old =atomic64_read (v); - new = old + i; - }while (ia64_cmpxchg (acq, v, old, new,sizeof (atomic64_t)) != old); -return new; -} +#undef ATOMIC_OP -static __inline__int -ia64_atomic_sub (int i,atomic_t *v) -{ -__s32 old, new; -CMPXCHG_BUGCHECK_DECL +#define atomic_add_return(i,v) \ +({ \ +int __ia64_aar_i = (i); \ + (__builtin_constant_p (i) \ + && ( (__ia64_aar_i ==1 ) || (__ia64_aar_i ==4 ) \ + || (__ia64_aar_i ==8 ) || (__ia64_aar_i ==16 ) \ + || (__ia64_aar_i ==-1 ) || (__ia64_aar_i ==-4 ) \ + || (__ia64_aar_i ==-8 ) || (__ia64_aar_i ==-16 ))) \ + ?ia64_fetch_and_add (__ia64_aar_i, &(v)->counter) \ + :ia64_atomic_add (__ia64_aar_i, v); \ +}) - do { -CMPXCHG_BUGCHECK (v); - old =atomic_read (v); - new = old - i; - }while (ia64_cmpxchg (acq, v, old, new,sizeof (atomic_t)) != old); -return new; +#define atomic_sub_return(i,v) \ +({ \ +int __ia64_asr_i = (i); \ + (__builtin_constant_p (i) \ + && ( (__ia64_asr_i ==1 ) || (__ia64_asr_i ==4 ) \ + || (__ia64_asr_i ==8 ) || (__ia64_asr_i ==16 ) \ + || (__ia64_asr_i ==-1 ) || (__ia64_asr_i ==-4 ) \ + || (__ia64_asr_i ==-8 ) || (__ia64_asr_i ==-16 ))) \ + ?ia64_fetch_and_add (-__ia64_asr_i, &(v)->counter) \ + :ia64_atomic_sub (__ia64_asr_i, v); \ +}) + +#define ATOMIC64_OP (op, c_op) \ +static __inline__ long \ +ia64_atomic64_##op (__s64 i, atomic64_t *v) \ +{ \ + __s64 old, new; \ + CMPXCHG_BUGCHECK_DECL \ + \ + do { \ + CMPXCHG_BUGCHECK(v); \ + old = atomic64_read(v); \ + new = old c_op i; \ + } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old); \ + return new; \ } -static __inline__long -ia64_atomic64_sub (__s64 i,atomic64_t *v) -{ -__s64 old, new; -CMPXCHG_BUGCHECK_DECL +ATOMIC64_OP(add, +) +ATOMIC64_OP(sub, -) - do { -CMPXCHG_BUGCHECK (v); - old =atomic64_read (v); - new = old - i; - }while (ia64_cmpxchg (acq, v, old, new,sizeof (atomic64_t)) != old); -return new; -} +#undef ATOMIC64_OP + +#define atomic64_add_return(i,v) \ +({ \ +long __ia64_aar_i = (i); \ + (__builtin_constant_p (i) \ + && ( (__ia64_aar_i ==1 ) || (__ia64_aar_i ==4 ) \ + || (__ia64_aar_i ==8 ) || (__ia64_aar_i ==16 ) \ + || (__ia64_aar_i ==-1 ) || (__ia64_aar_i ==-4 ) \ + || (__ia64_aar_i ==-8 ) || (__ia64_aar_i ==-16 ))) \ + ?ia64_fetch_and_add (__ia64_aar_i, &(v)->counter) \ + :ia64_atomic64_add (__ia64_aar_i, v); \ +}) + +#define atomic64_sub_return(i,v) \ +({ \ +long __ia64_asr_i = (i); \ + (__builtin_constant_p (i) \ + && ( (__ia64_asr_i ==1 ) || (__ia64_asr_i ==4 ) \ + || (__ia64_asr_i ==8 ) || (__ia64_asr_i ==16 ) \ + || (__ia64_asr_i ==-1 ) || (__ia64_asr_i ==-4 ) \ + || (__ia64_asr_i ==-8 ) || (__ia64_asr_i ==-16 ))) \ + ?ia64_fetch_and_add (-__ia64_asr_i, &(v)->counter) \ + :ia64_atomic64_sub (__ia64_asr_i, v); \ +})#define atomic_cmpxchg (v, old, new) (cmpxchg(&((v)->counter), old, new))#define atomic_xchg (v, new) (xchg(&((v)->counter), new))
@@ -123,30 +155,6 @@ static __inline__ long atomic64_add_unle#define atomic64_inc_not_zero (v) atomic64_add_unless((v), 1, 0) -#define atomic_add_return (i,v) \ -({ \ - int __ia64_aar_i = (i); \ - (__builtin_constant_p(i) \ - && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \ - || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \ - || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \ - || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \ - ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \ - : ia64_atomic_add(__ia64_aar_i, v); \ -}) - -#define atomic64_add_return (i,v) \ -({ \ - long __ia64_aar_i = (i); \ - (__builtin_constant_p(i) \ - && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \ - || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \ - || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \ - || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \ - ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \ - : ia64_atomic64_add(__ia64_aar_i, v); \ -}) -/* * Atomically add I to V and return TRUE if the resulting value is * negative.
@@ -163,30 +171,6 @@ atomic64_add_negative (__s64 i, atomic64return atomic64_add_return (i, v) <0 ; } -#define atomic_sub_return (i,v) \ -({ \ - int __ia64_asr_i = (i); \ - (__builtin_constant_p(i) \ - && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \ - || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \ - || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \ - || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \ - ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \ - : ia64_atomic_sub(__ia64_asr_i, v); \ -}) - -#define atomic64_sub_return (i,v) \ -({ \ - long __ia64_asr_i = (i); \ - (__builtin_constant_p(i) \ - && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \ - || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \ - || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \ - || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \ - ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \ - : ia64_atomic64_sub(__ia64_asr_i, v); \ -}) -#define atomic_dec_return (v) atomic_sub_return(1, (v))#define atomic_inc_return (v) atomic_add_return(1, (v))#define atomic64_dec_return (v) atomic64_sub_return(1, (v))
@@ -199,13 +183,13 @@ atomic64_add_negative (__s64 i, atomic64#define atomic64_dec_and_test (v) (atomic64_sub_return(1, (v)) == 0)#define atomic64_inc_and_test (v) (atomic64_add_return(1, (v)) == 0) -#define atomic_add (i,v) atomic_add_return((i), (v)) -#define atomic_sub (i,v) atomic_sub_return((i), (v)) +#define atomic_add (i,v) (void)atomic_add_return((i), (v)) +#define atomic_sub (i,v) (void)atomic_sub_return((i), (v))#define atomic_inc (v) atomic_add(1, (v))#define atomic_dec (v) atomic_sub(1, (v)) -#define atomic64_add (i,v) atomic64_add_return((i), (v)) -#define atomic64_sub (i,v) atomic64_sub_return((i), (v)) +#define atomic64_add (i,v) (void)atomic64_add_return((i), (v)) +#define atomic64_sub (i,v) (void)atomic64_sub_return((i), (v))#define atomic64_inc (v) atomic64_add(1, (v))#define atomic64_dec (v) atomic64_sub(1, (v))
Peter Zijlstra
maintainerPATCH 10/20 arch,m32r: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/m32r/include/asm/atomic.h | 151 ++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 90 deletions(-)
arch/m32r/include/asm/atomic.h +61 -90
@@ -39,85 +39,64 @@*/ #define atomic_set (v,i) (((v)->counter) = (i)) -/** -* atomic_add_return - add integer to atomic variable and return it -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v and return (@i + @v). -*/ -static __inline__int atomic_add_return (int i,atomic_t *v) -{ -unsigned long flags; -int result; - -local_irq_save (flags); - __asm__ __volatile__ ( -"# atomic_add_return \n\t" - DCACHE_CLEAR("%0", "r4", "%1") -M32R_LOCK " %0, @%1; \n\t" -"add %0, %2; \n\t" -M32R_UNLOCK " %0, @%1; \n\t" - : "=&r" (result) - : "r" (&v->counter), "r" (i) - : "memory" -#ifdef CONFIG_CHIP_M32700_TS1 - ,"r4" -#endif /* CONFIG_CHIP_M32700_TS1 */ - ); -local_irq_restore (flags); - -return result; -} - -/** -* atomic_sub_return - subtract integer from atomic variable and return it -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v and return (@v - @i). -*/ -static __inline__int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long flags; -int result; - -local_irq_save (flags); - __asm__ __volatile__ ( -"# atomic_sub_return \n\t" - DCACHE_CLEAR("%0", "r4", "%1") -M32R_LOCK " %0, @%1; \n\t" -"sub %0, %2; \n\t" -M32R_UNLOCK " %0, @%1; \n\t" - : "=&r" (result) - : "r" (&v->counter), "r" (i) - : "memory"#ifdef CONFIG_CHIP_M32700_TS1 - ,"r4" -#endif /* CONFIG_CHIP_M32700_TS1 */ - ); -local_irq_restore (flags); - -return result; -} - -/** -* atomic_add - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v. -*/ -#define atomic_add (i,v) ((void) atomic_add_return((i), (v))) - -/** -* atomic_sub - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v. -*/ -#define atomic_sub (i,v) ((void) atomic_sub_return((i), (v))) +#define __ATOMIC_CLOBBER , "r4" +#else +#define __ATOMIC_CLOBBER +#endif + +#define ATOMIC_OP (op) \ +static __inline__ void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + int result; \ + \ + local_irq_save(flags); \ + __asm__ __volatile__ ( \ + "# atomic_" #op " \n\t" \ + DCACHE_CLEAR("%0", "r4", "%1") \ + M32R_LOCK" %0, @%1; \n\t" \ + #op " %0, %2; \n\t" \ + M32R_UNLOCK" %0, @%1; \n\t" \ + : "=&r" (result) \ + : "r" (&v->counter), "r" (i) \ + : "memory" \ + __ATOMIC_CLOBBER \ + ); \ + local_irq_restore(flags); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static __inline__ int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + int result; \ + \ + local_irq_save(flags); \ + __asm__ __volatile__ ( \ + "# atomic_" #op "_return \n\t" \ + DCACHE_CLEAR("%0", "r4", "%1") \ + M32R_LOCK" %0, @%1; \n\t" \ + #op " %0, %2; \n\t" \ + M32R_UNLOCK" %0, @%1; \n\t" \ + : "=&r" (result) \ + : "r" (&v->counter), "r" (i) \ + : "memory" \ + __ATOMIC_CLOBBER \ + ); \ + local_irq_restore(flags); \ + \ + return result; \ +} + +#define ATOMIC_OPS (op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) + +ATOMIC_OPS(add) +ATOMIC_OPS(sub) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP/** * atomic_sub_and_test - subtract value from variable and test result
@@ -151,9 +130,7 @@ static __inline__ int atomic_inc_return( : "=&r" (result) : "r" (&v->counter) : "memory" -#ifdef CONFIG_CHIP_M32700_TS1 - , "r4" -#endif /* CONFIG_CHIP_M32700_TS1 */ + __ATOMIC_CLOBBER );local_irq_restore (flags);
@@ -181,9 +158,7 @@ static __inline__ int atomic_dec_return( : "=&r" (result) : "r" (&v->counter) : "memory" -#ifdef CONFIG_CHIP_M32700_TS1 - , "r4" -#endif /* CONFIG_CHIP_M32700_TS1 */ + __ATOMIC_CLOBBER );local_irq_restore (flags);
@@ -280,9 +255,7 @@ static __inline__ void atomic_clear_mask : "=&r" (tmp) : "r" (addr), "r" (~mask) : "memory" -#ifdef CONFIG_CHIP_M32700_TS1 - ,"r5" -#endif /* CONFIG_CHIP_M32700_TS1 */ + __ATOMIC_CLOBBER );local_irq_restore (flags); }
@@ -302,9 +275,7 @@ static __inline__ void atomic_set_mask(u : "=&r" (tmp) : "r" (addr), "r" (mask) : "memory" -#ifdef CONFIG_CHIP_M32700_TS1 - ,"r5" -#endif /* CONFIG_CHIP_M32700_TS1 */ + __ATOMIC_CLOBBER );local_irq_restore (flags); }
Peter Zijlstra
maintainerPATCH 12/20 arch,metag: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/metag/include/asm/atomic_lnkget.h | 121 ++++++++++++--------------------- arch/metag/include/asm/atomic_lock1.h | 76 ++++++++------------ 2 files changed, 77 insertions(+), 120 deletions(-)
arch/metag/include/asm/atomic_lnkget.h +46 -75
@@ -27,85 +27,56 @@ static inline int atomic_read(const atomreturn temp; } -static inline void atomic_add (int i,atomic_t *v) -{ -int temp; - - asmvolatile ( -"1: LNKGETD %0, [%1]\n" -" ADD %0, %0, %2\n" -" LNKSETD [%1], %0\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp) - :"da" (&v->counter),"bd" (i) - :"cc" ); +#define ATOMIC_OP (op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int temp; \ + \ + asm volatile ( \ + "1: LNKGETD %0, [%1]\n" \ + " " #op " %0, %0, %2\n" \ + " LNKSETD [%1], %0\n" \ + " DEFR %0, TXSTAT\n" \ + " ANDT %0, %0, #HI(0x3f000000)\n" \ + " CMPT %0, #HI(0x02000000)\n" \ + " BNZ 1b\n" \ + : "=&d" (temp) \ + : "da" (&v->counter), "bd" (i) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int result, temp; \ + \ + smp_mb(); \ + \ + asm volatile ( \ + "1: LNKGETD %1, [%2]\n" \ + " " #op " %1, %1, %3\n" \ + " LNKSETD [%2], %1\n" \ + " DEFR %0, TXSTAT\n" \ + " ANDT %0, %0, #HI(0x3f000000)\n" \ + " CMPT %0, #HI(0x02000000)\n" \ + " BNZ 1b\n" \ + : "=&d" (temp), "=&da" (result) \ + : "da" (&v->counter), "bd" (i) \ + : "cc"); \ + \ + smp_mb(); \ + \ + return result; \ } -static inline void atomic_sub (int i,atomic_t *v) -{ -int temp; - - asmvolatile ( -"1: LNKGETD %0, [%1]\n" -" SUB %0, %0, %2\n" -" LNKSETD [%1], %0\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp) - :"da" (&v->counter),"bd" (i) - :"cc" ); -} - -static inline int atomic_add_return (int i,atomic_t *v) -{ -int result, temp; - -smp_mb (); - - asmvolatile ( -"1: LNKGETD %1, [%2]\n" -" ADD %1, %1, %3\n" -" LNKSETD [%2], %1\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp),"=&da" (result) - :"da" (&v->counter),"bd" (i) - :"cc" ); +#define ATOMIC_OPS (op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) -smp_mb (); +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -return result; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int result, temp; - -smp_mb (); - - asmvolatile ( -"1: LNKGETD %1, [%2]\n" -" SUB %1, %1, %3\n" -" LNKSETD [%2], %1\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp),"=&da" (result) - :"da" (&v->counter),"bd" (i) - :"cc" ); - -smp_mb (); - -return result; -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OPstatic inline void atomic_clear_mask (unsigned int mask,atomic_t *v) {
arch/metag/include/asm/atomic_lock1.h +31 -45
86 lines not on this page · raw message
Peter Zijlstra
maintainerPATCH 13/20 arch,mips: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/mips/include/asm/atomic.h | 565 +++++++++++++---------------------------- 1 file changed, 191 insertions(+), 374 deletions(-)
arch/mips/include/asm/atomic.h +191 -374
@@ -40,195 +40,103 @@*/ #define atomic_set (v, i) ((v)->counter = (i)) -/* -* atomic_add - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v. -*/ -static __inline__void atomic_add (int i,atomic_t * v) -{ -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -int temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: ll %0, %1 # atomic_add \n" -" addu %0, %2 \n" -" sc %0, %1 \n" -" beqzl %0, 1b \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -int temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" ll %0, %1 # atomic_add \n" -" addu %0, %2 \n" -" sc %0, %1 \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!temp)); - }else { -unsigned long flags; - -raw_local_irq_save (flags); - v->counter += i; -raw_local_irq_restore (flags); - } +#define ATOMIC_OP (op, c_op, asm_op) \ +static __inline__ void atomic_##op(int i, atomic_t * v) \ +{ \ + if (kernel_uses_llsc && R10000_LLSC_WAR) { \ + int temp; \ + \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + "1: ll %0, %1 # atomic_" #op " \n" \ + " " #asm_op " %0, %2 \n" \ + " sc %0, %1 \n" \ + " beqzl %0, 1b \n" \ + " .set mips0 \n" \ + : "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } else if (kernel_uses_llsc) { \ + int temp; \ + \ + do { \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + " ll %0, %1 # atomic_" #op "\n" \ + " " #asm_op " %0, %2 \n" \ + " sc %0, %1 \n" \ + " .set mips0 \n" \ + : "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } while (unlikely(!temp)); \ + } else { \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + v->counter c_op i; \ + raw_local_irq_restore(flags); \ + } \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static __inline__ int atomic_##op##_return(int i, atomic_t * v) \ +{ \ + int result; \ + \ + smp_mb__before_llsc(); \ + \ + if (kernel_uses_llsc && R10000_LLSC_WAR) { \ + int temp; \ + \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + "1: ll %1, %2 # atomic_" #op "_return \n" \ + " " #asm_op " %0, %1, %3 \n" \ + " sc %0, %2 \n" \ + " beqzl %0, 1b \n" \ + " addu %0, %1, %3 \n" \ + " .set mips0 \n" \ + : "=&r" (result), "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } else if (kernel_uses_llsc) { \ + int temp; \ + \ + do { \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + " ll %1, %2 # atomic_" #op "_return \n" \ + " " #asm_op " %0, %1, %3 \n" \ + " sc %0, %2 \n" \ + " .set mips0 \n" \ + : "=&r" (result), "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } while (unlikely(!result)); \ + \ + result = temp + i; \ + } else { \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + result = v->counter; \ + result c_op i; \ + v->counter = result; \ + raw_local_irq_restore(flags); \ + } \ + \ + smp_llsc_mb(); \ + \ + return result; \ } -/* -* atomic_sub - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v. -*/ -static __inline__void atomic_sub (int i,atomic_t * v) -{ -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -int temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: ll %0, %1 # atomic_sub \n" -" subu %0, %2 \n" -" sc %0, %1 \n" -" beqzl %0, 1b \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -int temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" ll %0, %1 # atomic_sub \n" -" subu %0, %2 \n" -" sc %0, %1 \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!temp)); - }else { -unsigned long flags; - -raw_local_irq_save (flags); - v->counter -= i; -raw_local_irq_restore (flags); - } -} - -/* -* Same as above, but return the result value -*/ -static __inline__int atomic_add_return (int i,atomic_t * v) -{ -int result; - -smp_mb__before_llsc (); - -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -int temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: ll %1, %2 # atomic_add_return \n" -" addu %0, %1, %3 \n" -" sc %0, %2 \n" -" beqzl %0, 1b \n" -" addu %0, %1, %3 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -int temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" ll %1, %2 # atomic_add_return \n" -" addu %0, %1, %3 \n" -" sc %0, %2 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!result)); - - result = temp + i; - }else { -unsigned long flags; - -raw_local_irq_save (flags); - result = v->counter; - result += i; - v->counter = result; -raw_local_irq_restore (flags); - } - -smp_llsc_mb (); - -return result; -} - -static __inline__int atomic_sub_return (int i,atomic_t * v) -{ -int result; - -smp_mb__before_llsc (); - -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -int temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: ll %1, %2 # atomic_sub_return \n" -" subu %0, %1, %3 \n" -" sc %0, %2 \n" -" beqzl %0, 1b \n" -" subu %0, %1, %3 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter) - :"memory" ); - - result = temp - i; - }else if (kernel_uses_llsc) { -int temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" ll %1, %2 # atomic_sub_return \n" -" subu %0, %1, %3 \n" -" sc %0, %2 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!result)); - - result = temp - i; - }else { -unsigned long flags; - -raw_local_irq_save (flags); - result = v->counter; - result -= i; - v->counter = result; -raw_local_irq_restore (flags); - } - -smp_llsc_mb (); - -return result; -} +#define ATOMIC_OPS (op, c_op, asm_op) \ + ATOMIC_OP(op, c_op, asm_op) \ + ATOMIC_OP_RETURN(op, c_op, asm_op) + +ATOMIC_OPS(add, +=, addu) +ATOMIC_OPS(sub, -=, subu) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP/* * atomic_sub_if_positive - conditionally subtract integer from atomic variable
@@ -407,195 +315,104 @@ static __inline__ int __atomic_add_unles*/ #define atomic64_set (v, i) ((v)->counter = (i)) -/* -* atomic64_add - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic64_t -* -* Atomically adds @i to @v. -*/ -static __inline__void atomic64_add (long i,atomic64_t * v) -{ -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -long temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: lld %0, %1 # atomic64_add \n" -" daddu %0, %2 \n" -" scd %0, %1 \n" -" beqzl %0, 1b \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -long temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" lld %0, %1 # atomic64_add \n" -" daddu %0, %2 \n" -" scd %0, %1 \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!temp)); - }else { -unsigned long flags; - -raw_local_irq_save (flags); - v->counter += i; -raw_local_irq_restore (flags); - } +#define ATOMIC64_OP (op, c_op, asm_op) \ +static __inline__ void atomic64_##op(long i, atomic64_t * v) \ +{ \ + if (kernel_uses_llsc && R10000_LLSC_WAR) { \ + long temp; \ + \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + "1: lld %0, %1 # atomic64_" #op " \n" \ + " " #asm_op " %0, %2 \n" \ + " scd %0, %1 \n" \ + " beqzl %0, 1b \n" \ + " .set mips0 \n" \ + : "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } else if (kernel_uses_llsc) { \ + long temp; \ + \ + do { \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + " lld %0, %1 # atomic64_" #op "\n" \ + " " #asm_op " %0, %2 \n" \ + " scd %0, %1 \n" \ + " .set mips0 \n" \ + : "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } while (unlikely(!temp)); \ + } else { \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + v->counter c_op i; \ + raw_local_irq_restore(flags); \ + } \ +} \ + +#define ATOMIC64_OP_RETURN (op, c_op, asm_op) \ +static __inline__ long atomic64_##op##_return(long i, atomic64_t * v) \ +{ \ + long result; \ + \ + smp_mb__before_llsc(); \ + \ + if (kernel_uses_llsc && R10000_LLSC_WAR) { \ + long temp; \ + \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + "1: lld %1, %2 # atomic64_" #op "_return\n" \ + " " #asm_op " %0, %1, %3 \n" \ + " scd %0, %2 \n" \ + " beqzl %0, 1b \n" \ + " " #asm_op " %0, %1, %3 \n" \ + " .set mips0 \n" \ + : "=&r" (result), "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } else if (kernel_uses_llsc) { \ + long temp; \ + \ + do { \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + " lld %1, %2 # atomic64_" #op "_return\n" \ + " " #asm_op " %0, %1, %3 \n" \ + " scd %0, %2 \n" \ + " .set mips0 \n" \ + : "=&r" (result), "=&r" (temp), "=m" (v->counter) \ + : "Ir" (i), "m" (v->counter) \ + : "memory"); \ + } while (unlikely(!result)); \ + \ + result = temp + i; \ + } else { \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + result = v->counter; \ + result c_op i; \ + v->counter = result; \ + raw_local_irq_restore(flags); \ + } \ + \ + smp_llsc_mb(); \ + \ + return result; \ } -/* -* atomic64_sub - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic64_t -* -* Atomically subtracts @i from @v. -*/ -static __inline__void atomic64_sub (long i,atomic64_t * v) -{ -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -long temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: lld %0, %1 # atomic64_sub \n" -" dsubu %0, %2 \n" -" scd %0, %1 \n" -" beqzl %0, 1b \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -long temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" lld %0, %1 # atomic64_sub \n" -" dsubu %0, %2 \n" -" scd %0, %1 \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!temp)); - }else { -unsigned long flags; - -raw_local_irq_save (flags); - v->counter -= i; -raw_local_irq_restore (flags); - } -} - -/* -* Same as above, but return the result value -*/ -static __inline__long atomic64_add_return (long i,atomic64_t * v) -{ -long result; - -smp_mb__before_llsc (); - -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -long temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: lld %1, %2 # atomic64_add_return \n" -" daddu %0, %1, %3 \n" -" scd %0, %2 \n" -" beqzl %0, 1b \n" -" daddu %0, %1, %3 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -long temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" lld %1, %2 # atomic64_add_return \n" -" daddu %0, %1, %3 \n" -" scd %0, %2 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter) - :"memory" ); - }while (unlikely (!result)); - - result = temp + i; - }else { -unsigned long flags; - -raw_local_irq_save (flags); - result = v->counter; - result += i; - v->counter = result; -raw_local_irq_restore (flags); - } - -smp_llsc_mb (); - -return result; -} - -static __inline__long atomic64_sub_return (long i,atomic64_t * v) -{ -long result; - -smp_mb__before_llsc (); - -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -long temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: lld %1, %2 # atomic64_sub_return \n" -" dsubu %0, %1, %3 \n" -" scd %0, %2 \n" -" beqzl %0, 1b \n" -" dsubu %0, %1, %3 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter) - :"memory" ); - }else if (kernel_uses_llsc) { -long temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" lld %1, %2 # atomic64_sub_return \n" -" dsubu %0, %1, %3 \n" -" scd %0, %2 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter) - :"memory" ); - }while (unlikely (!result)); - - result = temp - i; - }else { -unsigned long flags; - -raw_local_irq_save (flags); - result = v->counter; - result -= i; - v->counter = result; -raw_local_irq_restore (flags); - } - -smp_llsc_mb (); - -return result; -} +#define ATOMIC64_OPS (op, c_op, asm_op) \ + ATOMIC64_OP(op, c_op, asm_op) \ + ATOMIC64_OP_RETURN(op, c_op, asm_op) + +ATOMIC64_OPS(add, +=, daddu) +ATOMIC64_OPS(sub, -=, dsubu) + +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OP/* * atomic64_sub_if_positive - conditionally subtract integer from atomic variable
Peter Zijlstra
maintainerPATCH 14/20 arch,mn10300: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/mn10300/include/asm/atomic.h | 129 ++++++++++++-------------------------- 1 file changed, 44 insertions(+), 85 deletions(-)
arch/mn10300/include/asm/atomic.h +44 -85
@@ -33,7 +33,6 @@* @v: pointer of type atomic_t * * Atomically reads the value of @v. Note that the guaranteed -* useful range of an atomic_t is only 24 bits. */ #define atomic_read (v) (ACCESS_ONCE((v)->counter))
@@ -43,100 +42,60 @@* @i: required value * * Atomically sets the value of @v to @i. Note that the guaranteed -* useful range of an atomic_t is only 24 bits. */ #define atomic_set (v, i) (((v)->counter) = (i)) -/** -* atomic_add_return - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v and returns the result -* Note that the guaranteed useful range of an atomic_t is only 24 bits. -*/ -static inline int atomic_add_return (int i,atomic_t *v) -{ -int retval; -#ifdef CONFIG_SMP -int status; - - asmvolatile ( -"1: mov %4,(_AAR,%3) \n" -" mov (_ADR,%3),%1 \n" -" add %5,%1 \n" -" mov %1,(_ADR,%3) \n" -" mov (_ADR,%3),%0 \n" /* flush */ -" mov (_ASR,%3),%0 \n" -" or %0,%0 \n" -" bne 1b \n" - :"=&r" (status),"=&r" (retval),"=m" (v->counter) - :"a" (ATOMIC_OPS_BASE_ADDR ),"r" (&v->counter),"r" (i) - :"memory" ,"cc" ); - -#else -unsigned long flags; - - flags =arch_local_cli_save (); - retval = v->counter; - retval += i; - v->counter = retval; -arch_local_irq_restore (flags); -#endif +#define ATOMIC_OP (op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int retval, status; \ + \ + asm volatile( \ + "1: mov %4,(_AAR,%3) \n" \ + " mov (_ADR,%3),%1 \n" \ + " " #op " %5,%1 \n" \ + " mov %1,(_ADR,%3) \n" \ + " mov (_ADR,%3),%0 \n"/* flush */ \ + " mov (_ASR,%3),%0 \n" \ + " or %0,%0 \n" \ + " bne 1b \n" \ + : "=&r"(status), "=&r"(retval), "=m"(v->counter) \ + : "a"(ATOMIC_OPS_BASE_ADDR), "r"(&v->counter), "r"(i) \ + : "memory", "cc"); \ +} \ + +#define ATOMIC_OP_RETURN(op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int retval, status; \ + \ + asm volatile( \ + "1: mov %4,(_AAR,%3) \n" \ + " mov (_ADR,%3),%1 \n" \ + " " #op " %5,%1 \n" \ + " mov %1,(_ADR,%3) \n" \ + " mov (_ADR,%3),%0 \n"/* flush */ \ + " mov (_ASR,%3),%0 \n" \ + " or %0,%0 \n" \ + " bne 1b \n" \ + : "=&r"(status), "=&r"(retval), "=m"(v->counter) \ + : "a"(ATOMIC_OPS_BASE_ADDR), "r"(&v->counter), "r"(i) \ + : "memory", "cc"); \ return retval; } -/** -* atomic_sub_return - subtract integer from atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v and returns the result -* Note that the guaranteed useful range of an atomic_t is only 24 bits. -*/ -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int retval; -#ifdef CONFIG_SMP -int status; - - asmvolatile ( -"1: mov %4,(_AAR,%3) \n" -" mov (_ADR,%3),%1 \n" -" sub %5,%1 \n" -" mov %1,(_ADR,%3) \n" -" mov (_ADR,%3),%0 \n" /* flush */ -" mov (_ASR,%3),%0 \n" -" or %0,%0 \n" -" bne 1b \n" - :"=&r" (status),"=&r" (retval),"=m" (v->counter) - :"a" (ATOMIC_OPS_BASE_ADDR ),"r" (&v->counter),"r" (i) - :"memory" ,"cc" ); - -#else -unsigned long flags; - flags =arch_local_cli_save (); - retval = v->counter; - retval -= i; - v->counter = retval; -arch_local_irq_restore (flags); -#endif -return retval; -} +#define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) -static inline int atomic_add_negative (int i,atomic_t *v) -{ -return atomic_add_return (i, v) <0 ; -} +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -static inline void atomic_add (int i,atomic_t *v) -{ -atomic_add_return (i, v); -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP -static inline void atomic_sub (int i,atomic_t *v) +static inline int atomic_add_negative(int i, atomic_t *v) { -atomic_sub_return (i, v); + return atomic_add_return(i, v) < 0; } static inline void atomic_inc(atomic_t *v)
Peter Zijlstra
maintainerPATCH 15/20 arch,parisc: Fold atomic_ops
In reply to Peter ZijlstraOK, no LoC saved in this case because sub was defined in terms of add. Still do it because this also prepares for easy addition of new ops.
--- arch/parisc/include/asm/atomic.h | 121 +++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 48 deletions(-)
arch/parisc/include/asm/atomic.h +73 -48
@@ -55,24 +55,7 @@ extern arch_spinlock_t __atomic_hash[ATO* are atomic, so a reader never sees inconsistent values. */ -/* It's possible to reduce all atomic operations to either -* __atomic_add_return, atomic_set and atomic_read (the latter -* is there only for consistency). -*/ - -static __inline__int __atomic_add_return (int i,atomic_t *v) -{ -int ret; -unsigned long flags; -_atomic_spin_lock_irqsave (v, flags); - - ret = (v->counter += i); - -_atomic_spin_unlock_irqrestore (v, flags); -return ret; -} - -static __inline__void atomic_set (atomic_t *v,int i) +static __inline__void atomic_set (atomic_t *v,int i) {unsigned long flags;_atomic_spin_lock_irqsave (v, flags);
@@ -115,16 +98,43 @@ static __inline__ int __atomic_add_unlesreturn c; } +#define ATOMIC_OP (op, c_op) \ +static __inline__ void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + \ + _atomic_spin_lock_irqsave(v, flags); \ + v->counter c_op i; \ + _atomic_spin_unlock_irqrestore(v, flags); \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op) \ +static __inline__ int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + int ret; \ + \ + _atomic_spin_lock_irqsave(v, flags); \ + ret = (v->counter c_op i); \ + _atomic_spin_unlock_irqrestore(v, flags); \ + \ + return ret; \ +} + +#define ATOMIC_OPS (op, c_op) ATOMIC_OP(op, c_op) ATOMIC_OP_RETURN(op, c_op) + +ATOMIC_OPS(add, +=) +ATOMIC_OPS(sub, -=) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP -#define atomic_add (i,v) ((void)(__atomic_add_return( (i),(v)))) -#define atomic_sub (i,v) ((void)(__atomic_add_return(-((int) (i)),(v)))) -#define atomic_inc (v) ((void)(__atomic_add_return( 1,(v)))) -#define atomic_dec (v) ((void)(__atomic_add_return( -1,(v)))) - -#define atomic_add_return (i,v) (__atomic_add_return( (i),(v))) -#define atomic_sub_return (i,v) (__atomic_add_return(-(i),(v))) -#define atomic_inc_return (v) (__atomic_add_return( 1,(v))) -#define atomic_dec_return (v) (__atomic_add_return( -1,(v))) +#define atomic_inc(v) (atomic_add( 1,(v))) +#define atomic_dec(v) (atomic_add( -1,(v))) + +#define atomic_inc_return (v) (atomic_add_return( 1,(v))) +#define atomic_dec_return (v) (atomic_add_return( -1,(v)))#define atomic_add_negative (a, v) (atomic_add_return((a), (v)) < 0)
@@ -148,18 +158,37 @@ static __inline__ int __atomic_add_unles#define ATOMIC64_INIT (i) { (i) } -static __inline__s64 -__atomic64_add_return (s64 i,atomic64_t *v) -{ -s64 ret; -unsigned long flags; -_atomic_spin_lock_irqsave (v, flags); - - ret = (v->counter += i); - -_atomic_spin_unlock_irqrestore (v, flags); -return ret; -} +#define ATOMIC64_OP (op, c_op) \ +static __inline__ void atomic64_##op(s64 i, atomic64_t *v) \ +{ \ + unsigned long flags; \ + \ + _atomic_spin_lock_irqsave(v, flags); \ + v->counter c_op i; \ + _atomic_spin_unlock_irqrestore(v, flags); \ +} \ + +#define ATOMIC64_OP_RETURN (op, c_op) \ +static __inline__ s64 atomic64_##op##_return(s64 i, atomic64_t *v) \ +{ \ + unsigned long flags; \ + s64 ret; \ + \ + _atomic_spin_lock_irqsave(v, flags); \ + ret = (v->counter c_op i); \ + _atomic_spin_unlock_irqrestore(v, flags); \ + \ + return ret; \ +} + +#define ATOMIC64_OPS (op, c_op) ATOMIC64_OP(op, c_op) ATOMIC64_OP_RETURN(op, c_op) + +ATOMIC64_OPS(add, +=) +ATOMIC64_OPS(sub, -=) + +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OPstatic __inline__void atomic64_set (atomic64_t *v,s64 i)
@@ -178,15 +207,11 @@ atomic64_read(const atomic64_t *v)return (*(volatile long *)&(v)->counter); } -#define atomic64_add (i,v) ((void)(__atomic64_add_return( ((s64)(i)),(v)))) -#define atomic64_sub (i,v) ((void)(__atomic64_add_return(-((s64)(i)),(v)))) -#define atomic64_inc (v) ((void)(__atomic64_add_return( 1,(v)))) -#define atomic64_dec (v) ((void)(__atomic64_add_return( -1,(v)))) - -#define atomic64_add_return (i,v) (__atomic64_add_return( ((s64)(i)),(v))) -#define atomic64_sub_return (i,v) (__atomic64_add_return(-((s64)(i)),(v))) -#define atomic64_inc_return (v) (__atomic64_add_return( 1,(v))) -#define atomic64_dec_return (v) (__atomic64_add_return( -1,(v))) +#define atomic64_inc (v) (atomic64_add( 1,(v))) +#define atomic64_dec (v) (atomic64_add( -1,(v))) + +#define atomic64_inc_return (v) (atomic64_add_return( 1,(v))) +#define atomic64_dec_return (v) (atomic64_add_return( -1,(v)))#define atomic64_add_negative (a, v) (atomic64_add_return((a), (v)) < 0)
Peter Zijlstra
maintainerPATCH 16/20 arch,powerpc: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
Requires asm_op because PPC asm is weird :-)
--- arch/powerpc/include/asm/atomic.h | 216 +++++++++++++++----------------------- 1 file changed, 86 insertions(+), 130 deletions(-)
arch/powerpc/include/asm/atomic.h +86 -130
@@ -26,76 +26,53 @@ static __inline__ void atomic_set(atomic __asm__ __volatile__("stw%U0%X0 %1,%0" :"=m" (v->counter) :"r" (i)); } -static __inline__void atomic_add (int a,atomic_t *v) -{ -int t; - - __asm__ __volatile__( -"1: lwarx %0,0,%3 # atomic_add\n\ -add %0,%2,%0\n" - PPC405_ERR77(0,%3) -" stwcx. %0,0,%3 \n\ - bne- 1b" - : "=&r" (t), "+m" (v->counter) - : "r" (a), "r" (&v->counter) - : "cc"); -} - -static __inline__int atomic_add_return (int a,atomic_t *v) -{ -int t; - - __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER -"1: lwarx %0,0,%2 # atomic_add_return\n\ -add %0,%1,%0\n" - PPC405_ERR77(0,%2) -" stwcx. %0,0,%2 \n\ -bne- 1b" -PPC_ATOMIC_EXIT_BARRIER - : "=&r" (t) - : "r" (a), "r" (&v->counter) - : "cc", "memory"); - - return t; -} +#define ATOMIC_OP (op, asm_op) \ +static __inline__ void atomic_##op(int a, atomic_t *v) \ +{ \ + int t; \ + \ + __asm__ __volatile__( \ +"1: lwarx %0,0,%3 # atomic_" #op "\n" \ + #asm_op " %0,%2,%0\n" \ + PPC405_ERR77(0,%3) \ +" stwcx. %0,0,%3 \n" \ +" bne- 1b\n" \ + : "=&r" (t), "+m" (v->counter) \ + : "r" (a), "r" (&v->counter) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN (op, asm_op) \ +static __inline__ int atomic_##op##_return(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" \ +" bne- 1b\n" \ + PPC_ATOMIC_EXIT_BARRIER \ + : "=&r" (t) \ + : "r" (a), "r" (&v->counter) \ + : "cc", "memory"); \ + \ + return t; \ +} + +#define ATOMIC_OPS (op, asm_op) ATOMIC_OP(op, asm_op) ATOMIC_OP_RETURN(op, asm_op) + +ATOMIC_OPS(add, add) +ATOMIC_OPS(sub, subf) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP#define atomic_add_negative (a, v) (atomic_add_return((a), (v)) < 0) -static __inline__void atomic_sub (int a,atomic_t *v) -{ -int t; - - __asm__ __volatile__( -"1: lwarx %0,0,%3 # atomic_sub\n\ -subf %0,%2,%0\n" - PPC405_ERR77(0,%3) -" stwcx. %0,0,%3 \n\ - bne- 1b" - : "=&r" (t), "+m" (v->counter) - : "r" (a), "r" (&v->counter) - : "cc"); -} - -static __inline__int atomic_sub_return (int a,atomic_t *v) -{ -int t; - - __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER -"1: lwarx %0,0,%2 # atomic_sub_return\n\ -subf %0,%1,%0\n" - PPC405_ERR77(0,%2) -" stwcx. %0,0,%2 \n\ -bne- 1b" -PPC_ATOMIC_EXIT_BARRIER - : "=&r" (t) - : "r" (a), "r" (&v->counter) - : "cc", "memory"); - - return t; -} -static __inline__void atomic_inc (atomic_t *v) {int t;
@@ -289,72 +266,51 @@ static __inline__ void atomic64_set(atom __asm__ __volatile__("std%U0%X0 %1,%0" :"=m" (v->counter) :"r" (i)); } -static __inline__void atomic64_add (long a,atomic64_t *v) -{ -long t; - - __asm__ __volatile__( -"1: ldarx %0,0,%3 # atomic64_add\n\ -add %0,%2,%0\n\ -stdcx. %0,0,%3 \n\ -bne- 1b" - :"=&r" (t),"+m" (v->counter) - :"r" (a),"r" (&v->counter) - :"cc" ); -} - -static __inline__long atomic64_add_return (long a,atomic64_t *v) -{ -long t; - - __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER -"1: ldarx %0,0,%2 # atomic64_add_return\n\ -add %0,%1,%0\n\ -stdcx. %0,0,%2 \n\ -bne- 1b" -PPC_ATOMIC_EXIT_BARRIER - :"=&r" (t) - :"r" (a),"r" (&v->counter) - :"cc" ,"memory" ); - -return t; -} +#define ATOMIC64_OP (op, asm_op) \ +static __inline__ void atomic64_##op(long a, atomic64_t *v) \ +{ \ + long t; \ + \ + __asm__ __volatile__( \ +"1: ldarx %0,0,%3 # atomic64_" #op "\n" \ + #asm_op " %0,%2,%0\n" \ +" stdcx. %0,0,%3 \n" \ +" bne- 1b\n" \ + : "=&r" (t), "+m" (v->counter) \ + : "r" (a), "r" (&v->counter) \ + : "cc"); \ +} \ + +#define ATOMIC64_OP_RETURN (op, asm_op) \ +static __inline__ long atomic64_##op##_return(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" \ +" bne- 1b\n" \ + PPC_ATOMIC_EXIT_BARRIER \ + : "=&r" (t) \ + : "r" (a), "r" (&v->counter) \ + : "cc", "memory"); \ + \ + return t; \ +} + +#define ATOMIC64_OPS (op, asm_op) ATOMIC64_OP(op, asm_op) ATOMIC64_OP_RETURN(op, asm_op) + +ATOMIC64_OPS(add, add) +ATOMIC64_OPS(add, subf) + +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OP#define atomic64_add_negative (a, v) (atomic64_add_return((a), (v)) < 0) -static __inline__void atomic64_sub (long a,atomic64_t *v) -{ -long t; - - __asm__ __volatile__( -"1: ldarx %0,0,%3 # atomic64_sub\n\ -subf %0,%2,%0\n\ -stdcx. %0,0,%3 \n\ -bne- 1b" - :"=&r" (t),"+m" (v->counter) - :"r" (a),"r" (&v->counter) - :"cc" ); -} - -static __inline__long atomic64_sub_return (long a,atomic64_t *v) -{ -long t; - - __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER -"1: ldarx %0,0,%2 # atomic64_sub_return\n\ -subf %0,%1,%0\n\ -stdcx. %0,0,%2 \n\ -bne- 1b" -PPC_ATOMIC_EXIT_BARRIER - :"=&r" (t) - :"r" (a),"r" (&v->counter) - :"cc" ,"memory" ); - -return t; -} -static __inline__void atomic64_inc (atomic64_t *v) {long t;
Peter Zijlstra
maintainerPATCH 17/20 arch,sh: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/sh/include/asm/atomic-grb.h | 121 ++++++++++++++------------------------ arch/sh/include/asm/atomic-irq.h | 62 ++++++++----------- arch/sh/include/asm/atomic-llsc.h | 101 ++++++++++++------------------- 3 files changed, 113 insertions(+), 171 deletions(-)
arch/sh/include/asm/atomic-grb.h +46 -75
@@ -1,85 +1,56 @@ #ifndef __ASM_SH_ATOMIC_GRB_H #define __ASM_SH_ATOMIC_GRB_H -static inline void atomic_add (int i,atomic_t *v) -{ -int tmp; - - __asm__ __volatile__ ( -" .align 2 \n\t" -" mova 1f, r0 \n\t" /* r0 = end point */ -" mov r15, r1 \n\t" /* r1 = saved sp */ -" mov #-6, r15 \n\t" /* LOGIN: r15 = size */ -" mov.l @%1, %0 \n\t" /* load old value */ -" add %2, %0 \n\t" /* add */ -" mov.l %0, @%1 \n\t" /* store new value */ -"1: mov r1, r15 \n\t" /* LOGOUT */ - :"=&r" (tmp), -"+r" (v) - :"r" (i) - :"memory" ,"r0" ,"r1" ); +#define ATOMIC_OP(op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int tmp; \ + \ + __asm__ __volatile__ ( \ + " .align 2 \n\t" \ + " mova 1f, r0 \n\t" /* r0 = end point */ \ + " mov r15, r1 \n\t" /* r1 = saved sp */ \ + " mov #-6, r15 \n\t" /* LOGIN: r15 = size */ \ + " mov.l @%1, %0 \n\t" /* load old value */ \ + " " #op " %2, %0 \n\t" /* $op */ \ + " mov.l %0, @%1 \n\t" /* store new value */ \ + "1: mov r1, r15 \n\t" /* LOGOUT */ \ + : "=&r" (tmp), \ + "+r" (v) \ + : "r" (i) \ + : "memory" , "r0", "r1"); \ +} \ + +#define ATOMIC_OP_RETURN(op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int tmp; \ + \ + __asm__ __volatile__ ( \ + " .align 2 \n\t" \ + " mova 1f, r0 \n\t" /* r0 = end point */ \ + " mov r15, r1 \n\t" /* r1 = saved sp */ \ + " mov #-6, r15 \n\t" /* LOGIN: r15 = size */ \ + " mov.l @%1, %0 \n\t" /* load old value */ \ + " " #op " %2, %0 \n\t" /* $op */ \ + " mov.l %0, @%1 \n\t" /* store new value */ \ + "1: mov r1, r15 \n\t" /* LOGOUT */ \ + : "=&r" (tmp), \ + "+r" (v) \ + : "r" (i) \ + : "memory" , "r0", "r1"); \ + \ + return tmp; \ } -static inline void atomic_sub (int i,atomic_t *v) -{ -int tmp; - - __asm__ __volatile__ ( -" .align 2 \n\t" -" mova 1f, r0 \n\t" /* r0 = end point */ -" mov r15, r1 \n\t" /* r1 = saved sp */ -" mov #-6, r15 \n\t" /* LOGIN: r15 = size */ -" mov.l @%1, %0 \n\t" /* load old value */ -" sub %2, %0 \n\t" /* sub */ -" mov.l %0, @%1 \n\t" /* store new value */ -"1: mov r1, r15 \n\t" /* LOGOUT */ - :"=&r" (tmp), -"+r" (v) - :"r" (i) - :"memory" ,"r0" ,"r1" ); -} - -static inline int atomic_add_return (int i,atomic_t *v) -{ -int tmp; +#define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) - __asm__ __volatile__ ( -" .align 2 \n\t" -" mova 1f, r0 \n\t" /* r0 = end point */ -" mov r15, r1 \n\t" /* r1 = saved sp */ -" mov #-6, r15 \n\t" /* LOGIN: r15 = size */ -" mov.l @%1, %0 \n\t" /* load old value */ -" add %2, %0 \n\t" /* add */ -" mov.l %0, @%1 \n\t" /* store new value */ -"1: mov r1, r15 \n\t" /* LOGOUT */ - :"=&r" (tmp), -"+r" (v) - :"r" (i) - :"memory" ,"r0" ,"r1" ); +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -return tmp; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int tmp; - - __asm__ __volatile__ ( -" .align 2 \n\t" -" mova 1f, r0 \n\t" /* r0 = end point */ -" mov r15, r1 \n\t" /* r1 = saved sp */ -" mov #-6, r15 \n\t" /* LOGIN: r15 = size */ -" mov.l @%1, %0 \n\t" /* load old value */ -" sub %2, %0 \n\t" /* sub */ -" mov.l %0, @%1 \n\t" /* store new value */ -"1: mov r1, r15 \n\t" /* LOGOUT */ - :"=&r" (tmp), -"+r" (v) - :"r" (i) - :"memory" ,"r0" ,"r1" ); - -return tmp; -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP static inline void atomic_clear_mask(unsigned int mask, atomic_t *v) {
arch/sh/include/asm/atomic-irq.h +26 -36
75 lines not on this page · raw message
arch/sh/include/asm/atomic-llsc.h +41 -60
119 lines not on this page · raw message
Peter Zijlstra
maintainerPATCH 6/20 arch,avr32: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires the asm_op because of eor.
--- arch/avr32/include/asm/atomic.h | 96 ++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 52 deletions(-)
arch/avr32/include/asm/atomic.h +44 -52
@@ -22,58 +22,52 @@#define atomic_read (v) (*(volatile int *)&(v)->counter)#define atomic_set (v, i) (((v)->counter) = i) -/* -* atomic_sub_return - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v. Returns the resulting value. -*/ -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int result; - - asmvolatile ( -"/* atomic_sub_return */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" -" sub %0, %3\n" -" stcond %1, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"rKs21" (i) - :"cc" ); - -return result; +#define ATOMIC_OP(op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int result; \ + \ + asm volatile( \ + "/* atomic_" #op " */\n" \ + "1: ssrf 5\n" \ + " ld.w %0, %2\n" \ + " " #asm_op " %0, %3\n" \ + " stcond %1, %0\n" \ + " brne 1b" \ + : "=&r"(result), "=o"(v->counter) \ + : "m"(v->counter), "rKs21"(i) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN(op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int result; \ + \ + asm volatile( \ + "/* atomic_" #op "_return */\n" \ + "1: ssrf 5\n" \ + " ld.w %0, %2\n" \ + " " #asm_op " %0, %3\n" \ + " stcond %1, %0\n" \ + " brne 1b" \ + : "=&r"(result), "=o"(v->counter) \ + : "m"(v->counter), "rKs21"(i) \ + : "cc"); \ + \ + return result; \ } -/* -* atomic_add_return - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v. Returns the resulting value. -*/ -static inline int atomic_add_return (int i,atomic_t *v) -{ -int result; - -if (__builtin_constant_p (i) && (i >=-1048575 ) && (i <=1048576 )) - result =atomic_sub_return (-i, v); -else - asmvolatile ( -"/* atomic_add_return */\n" -"1: ssrf 5\n" -" ld.w %0, %1\n" -" add %0, %3\n" -" stcond %2, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"r" (i) - :"cc" ,"memory" ); - -return result; -} +#define ATOMIC_OPS (op, asm_op) \ + ATOMIC_OP(op, asm_op) \ + ATOMIC_OP_RETURN(op, asm_op) + +ATOMIC_OPS(add, add) +ATOMIC_OPS(sub, sub) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP/* * atomic_sub_unless - sub unless the number is a given value
@@ -168,8 +162,6 @@ static inline int atomic_sub_if_positive#define atomic_xchg (v, new) (xchg(&((v)->counter), new))#define atomic_cmpxchg (v, o, n) (cmpxchg(&((v)->counter), (o), (n))) -#define atomic_sub (i, v) (void)atomic_sub_return(i, v) -#define atomic_add (i, v) (void)atomic_add_return(i, v)#define atomic_dec (v) atomic_sub(1, (v))#define atomic_inc (v) atomic_add(1, (v))
Peter Zijlstra
maintainerPATCH 11/20 arch,m68k: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires asm_op due to eor.
--- arch/m68k/include/asm/atomic.h | 119 ++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 64 deletions(-)
arch/m68k/include/asm/atomic.h +55 -64
@@ -30,15 +30,62 @@#define ASM_DI "di"#endif -static inline void atomic_add (int i,atomic_t *v) -{ - __asm__ __volatile__("addl %1,%0" : "+m" (*v) : ASM_DI (i)); -} +#ifdef CONFIG_RMW_INSNS -static inline void atomic_sub (int i,atomic_t *v) -{ - __asm__ __volatile__("subl %1,%0" : "+m" (*v) : ASM_DI (i)); -} +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + __asm__ __volatile__(#asm_op "l %1,%0" : "+m" (*v) : ASM_DI (i));\ +} \ + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int t, tmp; \ + \ + __asm__ __volatile__( \ + "1: movel %2,%1\n" \ + " " #asm_op "l %3,%1\n" \ + " casl %2,%1,%0\n" \ + " jne 1b" \ + : "+m" (*v), "=&d" (t), "=&d" (tmp) \ + : "g" (i), "2" (atomic_read(v))); \ + return t; \ +} + +#else + +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + __asm__ __volatile__(#asm_op "l %1,%0" : "+m" (*v) : ASM_DI (i));\ +} \ + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t * v) \ +{ \ + unsigned long flags; \ + int t; \ + \ + local_irq_save(flags); \ + t = (v->counter c_op i); \ + local_irq_restore(flags); \ + \ + return t; \ +} + +#endif /* CONFIG_RMW_INSNS */ + +#define ATOMIC_OPS (op, c_op, asm_op) \ + ATOMIC_OP(op, c_op, asm_op) \ + ATOMIC_OP_RETURN(op, c_op, asm_op) + +ATOMIC_OPS(add, +=, add) +ATOMIC_OPS(sub, -=, sub) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OPstatic inline void atomic_inc (atomic_t *v) {
@@ -76,67 +123,11 @@ static inline int atomic_inc_and_test(at#ifdef CONFIG_RMW_INSNS -static inline int atomic_add_return (int i,atomic_t *v) -{ -int t, tmp; - - __asm__ __volatile__( -"1: movel %2,%1\n" -" addl %3,%1\n" -" casl %2,%1,%0\n" -" jne 1b" - :"+m" (*v),"=&d" (t),"=&d" (tmp) - :"g" (i),"2" (atomic_read (v))); -return t; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int t, tmp; - - __asm__ __volatile__( -"1: movel %2,%1\n" -" subl %3,%1\n" -" casl %2,%1,%0\n" -" jne 1b" - :"+m" (*v),"=&d" (t),"=&d" (tmp) - :"g" (i),"2" (atomic_read (v))); -return t; -} -#define atomic_cmpxchg (v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))#define atomic_xchg (v, new) (xchg(&((v)->counter), new))#else /* !CONFIG_RMW_INSNS */ -static inline int atomic_add_return (int i,atomic_t * v) -{ -unsigned long flags; -int t; - -local_irq_save (flags); - t =atomic_read (v); - t += i; -atomic_set (v, t); -local_irq_restore (flags); - -return t; -} - -static inline int atomic_sub_return (int i,atomic_t * v) -{ -unsigned long flags; -int t; - -local_irq_save (flags); - t =atomic_read (v); - t -= i; -atomic_set (v, t); -local_irq_restore (flags); - -return t; -} -static inline int atomic_cmpxchg (atomic_t *v,int old,int new) {unsigned long flags;
Peter Zijlstra
maintainerPATCH 20/20 arch: Rewrite generic atomic support
In reply to Peter ZijlstraRewrite generic atomic support to only require cmpxchg(), generate all other primitives from that.
Furthermore reduce the endless repetition for all these primitives to a few CPP macros. This way we get more for less lines.
--- include/asm-generic/atomic.h | 174 ++++++++++++++++++----------------------- include/asm-generic/atomic64.h | 20 +++- lib/atomic64.c | 83 ++++++++----------- 3 files changed, 130 insertions(+), 147 deletions(-)
include/asm-generic/atomic.h +78 -96
@@ -19,13 +19,81 @@#include <asm/barrier.h> #ifdef CONFIG_SMP -/* Force people to define core atomics */ -# if !defined(atomic_add_return) || !defined(atomic_sub_return) || \ - !defined(atomic_clear_mask) || !defined(atomic_set_mask) -# error "SMP requires a little arch-specific magic" -# endif + +/* we can build all atomic primitives from cmpxchg */ + +#define ATOMIC_OP (op, c_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int c, old; \ + \ + c = v->counter; \ + while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \ + c = old; \ +} + +#define ATOMIC_OP_RETURN (op, c_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int c, old; \ + \ + c = v->counter; \ + while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \ + c = old; \ + \ + return c c_op i; \ +} + +#else + +#include <linux/irqflags.h> + +#define ATOMIC_OP (op, c_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + v->counter c_op i; \ + raw_local_irq_restore(flags); \ +} + +#define ATOMIC_OP_RETURN (op, c_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + int ret; \ + \ + raw_local_irq_save(flags); \ + ret = (v->counter c_op i); \ + raw_local_irq_restore(flags); \ + \ + return ret; \ +} + +#endif /* CONFIG_SMP */ + +#ifndef atomic_add_return +ATOMIC_OP_RETURN(add, +) +#endif + +#ifndef atomic_sub_return +ATOMIC_OP_RETURN(sub, -) +#endif + +#ifndef atomic_clear_mask +ATOMIC_OP(and, &) +#define atomic_clear_mask(i, v) atomic_and(~(i), (v)) +#endif + +#ifndef atomic_set_mask +ATOMIC_OP(or, |) +#define atomic_set_mask(i, v) atomic_or((i), (v)) #endif +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP +/* * Atomic operations that C can't guarantee us. Useful for * resource counting etc..
@@ -33,8 +101,6 @@#define ATOMIC_INIT (i) { (i) } -#ifdef __KERNEL__ -/** * atomic_read - read atomic variable * @v: pointer of type atomic_t
@@ -56,52 +122,6 @@#include <linux/irqflags.h> -/** -* atomic_add_return - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v and returns the result -*/ -#ifndef atomic_add_return -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long flags; -int temp; - -raw_local_irq_save (flags);/* Don't trace it in an irqsoff handler */ - temp = v->counter; - temp += i; - v->counter = temp; -raw_local_irq_restore (flags); - -return temp; -} -#endif - -/** -* atomic_sub_return - subtract integer from atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v and returns the result -*/ -#ifndef atomic_sub_return -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long flags; -int temp; - -raw_local_irq_save (flags);/* Don't trace it in an irqsoff handler */ - temp = v->counter; - temp -= i; - v->counter = temp; -raw_local_irq_restore (flags); - -return temp; -} -#endif -static inline int atomic_add_negative (int i,atomic_t *v) {return atomic_add_return (i, v) <0 ;
@@ -139,49 +159,11 @@ static inline void atomic_dec(atomic_t *static inline int __atomic_add_unless (atomic_t *v,int a,int u) { -int c, old; - c =atomic_read (v); -while (c != u && (old =atomic_cmpxchg (v, c, c + a)) != c) - c = old; -return c; -} - -/** -* atomic_clear_mask - Atomically clear bits in atomic variable -* @mask: Mask of the bits to be cleared -* @v: pointer of type atomic_t -* -* Atomically clears the bits set in @mask from @v -*/ -#ifndef atomic_clear_mask -static inline void atomic_clear_mask (unsigned long mask,atomic_t *v) -{ -unsigned long flags; - - mask = ~mask; -raw_local_irq_save (flags);/* Don't trace it in a irqsoff handler */ - v->counter &= mask; -raw_local_irq_restore (flags); -} -#endif - -/** -* atomic_set_mask - Atomically set bits in atomic variable -* @mask: Mask of the bits to be set -* @v: pointer of type atomic_t -* -* Atomically sets the bits set in @mask in @v -*/ -#ifndef atomic_set_mask -static inline void atomic_set_mask (unsigned int mask,atomic_t *v) -{ -unsigned long flags; - -raw_local_irq_save (flags);/* Don't trace it in a irqsoff handler */ - v->counter |= mask; -raw_local_irq_restore (flags); +int c, old; + c =atomic_read (v); +while (c != u && (old =atomic_cmpxchg (v, c, c + a)) != c) + c = old; +return c; } -#endif -#endif /* __KERNEL__ */ #endif /* __ASM_GENERIC_ATOMIC_H */
include/asm-generic/atomic64.h +16 -4
@@ -20,10 +20,22 @@ typedef struct {extern long long atomic64_read (const atomic64_t *v);extern void atomic64_set (atomic64_t *v,long long i); -extern void atomic64_add (long long a,atomic64_t *v); -extern long long atomic64_add_return (long long a,atomic64_t *v); -extern void atomic64_sub (long long a,atomic64_t *v); -extern long long atomic64_sub_return (long long a,atomic64_t *v); + +#define ATOMIC64_OP (op) \ +extern void atomic64_##op(long long a, atomic64_t *v); + +#define ATOMIC64_OP_RETURN (op) \ +extern long long atomic64_##op##_return(long long a, atomic64_t *v); + +#define ATOMIC64_OPS (op) ATOMIC64_OP(op) ATOMIC_OP_RETURN(op) + +ATOMIC64_OPS(add) +ATOMIC64_OPS(sub) + +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OP +extern long long atomic64_dec_if_positive (atomic64_t *v);extern long long atomic64_cmpxchg (atomic64_t *v,long long o,long long n);extern long long atomic64_xchg (atomic64_t *v,long long new);
lib/atomic64.c +36 -47
89 lines not on this page · raw message
Peter Zijlstra
maintainerPATCH 18/20 arch,sparc: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/sparc/include/asm/atomic_32.h | 19 +-- arch/sparc/include/asm/atomic_64.h | 49 +++++----- arch/sparc/include/asm/barrier_32.h | 1 arch/sparc/include/asm/processor.h | 2 arch/sparc/kernel/smp_64.c | 2 arch/sparc/lib/atomic32.c | 25 +++-- arch/sparc/lib/atomic_64.S | 175 +++++++++++++++--------------------- arch/sparc/lib/ksyms.c | 25 +++-- 8 files changed, 140 insertions(+), 158 deletions(-)
arch/sparc/include/asm/atomic_32.h +9 -10
@@ -20,7 +20,7 @@#define ATOMIC_INIT (i) { (i) } -extern int __atomic_add_return (int ,atomic_t *); +extern int atomic_add_return (int ,atomic_t *);extern int atomic_cmpxchg (atomic_t *,int ,int );#define atomic_xchg (v, new) (xchg(&((v)->counter), new))extern int __atomic_add_unless (atomic_t *,int ,int );
@@ -28,15 +28,14 @@ extern void atomic_set(atomic_t *, int);#define atomic_read (v) (*(volatile int *)&(v)->counter) -#define atomic_add (i, v) ((void)__atomic_add_return( (int)(i), (v))) -#define atomic_sub (i, v) ((void)__atomic_add_return(-(int)(i), (v))) -#define atomic_inc (v) ((void)__atomic_add_return( 1, (v))) -#define atomic_dec (v) ((void)__atomic_add_return( -1, (v))) - -#define atomic_add_return (i, v) (__atomic_add_return( (int)(i), (v))) -#define atomic_sub_return (i, v) (__atomic_add_return(-(int)(i), (v))) -#define atomic_inc_return (v) (__atomic_add_return( 1, (v))) -#define atomic_dec_return (v) (__atomic_add_return( -1, (v))) +#define atomic_add (i, v) ((void)atomic_add_return( (int)(i), (v))) +#define atomic_sub (i, v) ((void)atomic_add_return(-(int)(i), (v))) +#define atomic_inc (v) ((void)atomic_add_return( 1, (v))) +#define atomic_dec (v) ((void)atomic_add_return( -1, (v))) + +#define atomic_sub_return (i, v) (atomic_add_return(-(int)(i), (v))) +#define atomic_inc_return (v) (atomic_add_return( 1, (v))) +#define atomic_dec_return (v) (atomic_add_return( -1, (v)))#define atomic_add_negative (a, v) (atomic_add_return((a), (v)) < 0)
arch/sparc/include/asm/atomic_64.h +25 -24
71 lines not on this page · raw message
arch/sparc/include/asm/barrier_32.h +0 -1
@@ -1,7 +1,6 @@#ifndef __SPARC_BARRIER_H#define __SPARC_BARRIER_H -#include <asm/processor.h> /* for nop() */ #include <asm-generic/barrier.h> #endif /* !(__SPARC_BARRIER_H) */
arch/sparc/include/asm/processor.h +0 -2
@@ -6,6 +6,4 @@#include <asm/processor_32.h> #endif -#define nop () __asm__ __volatile__ ("nop") -#endif
arch/sparc/kernel/smp_64.c +1 -1
@@ -1148,7 +1148,7 @@ static unsigned long penguins_are_doing_void smp_capture (void ) { -int result =atomic_add_ret (1 , &smp_capture_depth); +int result =atomic_add_return (1 , &smp_capture_depth);if (result ==1 ) {int ncpus =num_online_cpus ();
arch/sparc/lib/atomic32.c +15 -10
@@ -27,18 +27,23 @@ static DEFINE_SPINLOCK(dummy);#endif /* SMP */ -int __atomic_add_return (int i,atomic_t *v) -{ -int ret; -unsigned long flags; -spin_lock_irqsave (ATOMIC_HASH (v), flags); +#define ATOMIC_OP (op, cop) \ +int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int ret; \ + unsigned long flags; \ + spin_lock_irqsave(ATOMIC_HASH(v), flags); \ + \ + ret = (v->counter cop i); \ + \ + spin_unlock_irqrestore(ATOMIC_HASH(v), flags); \ + return ret; \ +} \ +EXPORT_SYMBOL(atomic_##op##_return); - ret = (v->counter += i); +ATOMIC_OP(add, +=) -spin_unlock_irqrestore (ATOMIC_HASH (v), flags); -return ret; -} -EXPORT_SYMBOL (__atomic_add_return); +#undef ATOMIC_OP int atomic_cmpxchg(atomic_t *v, int old, int new) {
arch/sparc/lib/atomic_64.S +73 -102
182 lines not on this page · raw message
arch/sparc/lib/ksyms.c +17 -8
@@ -99,14 +99,23 @@ EXPORT_SYMBOL(___copy_in_user);EXPORT_SYMBOL (__clear_user);/* Atomic counter implementation. */ -EXPORT_SYMBOL (atomic_add); -EXPORT_SYMBOL (atomic_add_ret); -EXPORT_SYMBOL (atomic_sub); -EXPORT_SYMBOL (atomic_sub_ret); -EXPORT_SYMBOL (atomic64_add); -EXPORT_SYMBOL (atomic64_add_ret); -EXPORT_SYMBOL (atomic64_sub); -EXPORT_SYMBOL (atomic64_sub_ret); +#define ATOMIC_OP (op) \ +EXPORT_SYMBOL(atomic_##op); \ +EXPORT_SYMBOL(atomic64_##op); + +#define ATOMIC_OP_RETURN (op) \ +EXPORT_SYMBOL(atomic_##op##_return); \ +EXPORT_SYMBOL(atomic64_##op##_return); + +#define ATOMIC_OPS (op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) + +ATOMIC_OPS(add) +ATOMIC_OPS(sub) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP +EXPORT_SYMBOL (atomic64_dec_if_positive);/* Atomic bit operations. */
Peter Zijlstra
maintainerPATCH 19/20 arch,xtensa: Fold atomic_ops
In reply to Peter ZijlstraMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/xtensa/include/asm/atomic.h | 231 +++++++++++++-------------------------- 1 file changed, 81 insertions(+), 150 deletions(-)
arch/xtensa/include/asm/atomic.h +81 -150
@@ -58,165 +58,96 @@*/ #define atomic_set (v,i) ((v)->counter = (i)) -/** -* atomic_add - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v. -*/ -static inline void atomic_add (int i,atomic_t * v) -{#if XCHAL_HAVE_S32C1I -unsigned long tmp; -int result; - - __asm__ __volatile__( -"1: l32i %1, %3, 0\n" -" wsr %1, scompare1\n" -" add %0, %1, %2\n" -" s32c1i %0, %3, 0\n" -" bne %0, %1, 1b\n" - :"=&a" (result),"=&a" (tmp) - :"a" (i),"a" (v) - :"memory" - ); -#else -unsigned int vval; - - __asm__ __volatile__( - " rsil a15, "__stringify(LOCKLEVEL)"\n" -" l32i %0, %2, 0\n" -" add %0, %0, %1\n" -" s32i %0, %2, 0\n" -" wsr a15, ps\n" -" rsync\n" - : "=&a" (vval) - : "a" (i), "a" (v) - : "a15", "memory" - ); -#endif +#define ATOMIC_OP (op) \ +static inline void atomic_##op(int i, atomic_t * v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + __asm__ __volatile__( \ + "1: l32i %1, %3, 0\n" \ + " wsr %1, scompare1\n" \ + " " #op " %0, %1, %2\n" \ + " s32c1i %0, %3, 0\n" \ + " bne %0, %1, 1b\n" \ + : "=&a" (result), "=&a" (tmp) \ + : "a" (i), "a" (v) \ + : "memory" \ + ); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static inline int atomic_##op##_return(int i, atomic_t * v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + __asm__ __volatile__( \ + "1: l32i %1, %3, 0\n" \ + " wsr %1, scompare1\n" \ + " " #op " %0, %1, %2\n" \ + " s32c1i %0, %3, 0\n" \ + " bne %0, %1, 1b\n" \ + " " #op " %0, %0, %2\n" \ + : "=&a" (result), "=&a" (tmp) \ + : "a" (i), "a" (v) \ + : "memory" \ + ); \ + \ + return result; \ } -/** -* atomic_sub - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v. -*/ -static inline void atomic_sub (int i,atomic_t *v) -{ -#if XCHAL_HAVE_S32C1I -unsigned long tmp; -int result; +#else /* XCHAL_HAVE_S32C1I */ - __asm__ __volatile__( -"1: l32i %1, %3, 0\n" -" wsr %1, scompare1\n" -" sub %0, %1, %2\n" -" s32c1i %0, %3, 0\n" -" bne %0, %1, 1b\n" - :"=&a" (result),"=&a" (tmp) - :"a" (i),"a" (v) - :"memory" - ); -#else -unsigned int vval; - - __asm__ __volatile__( - " rsil a15, "__stringify(LOCKLEVEL)"\n" -" l32i %0, %2, 0\n" -" sub %0, %0, %1\n" -" s32i %0, %2, 0\n" -" wsr a15, ps\n" -" rsync\n" - : "=&a" (vval) - : "a" (i), "a" (v) - : "a15", "memory" - ); -#endif +#define ATOMIC_OP (op) \ +static inline void atomic_##op(int i, atomic_t * v) \ +{ \ + unsigned int vval; \ + \ + __asm__ __volatile__( \ + " rsil a15, "__stringify(LOCKLEVEL)"\n"\ + " l32i %0, %2, 0\n" \ + " " #op " %0, %0, %1\n" \ + " s32i %0, %2, 0\n" \ + " wsr a15, ps\n" \ + " rsync\n" \ + : "=&a" (vval) \ + : "a" (i), "a" (v) \ + : "a15", "memory" \ + ); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static inline int atomic_##op##_return(int i, atomic_t * v) \ +{ \ + unsigned int vval; \ + \ + __asm__ __volatile__( \ + " rsil a15,"__stringify(LOCKLEVEL)"\n" \ + " l32i %0, %2, 0\n" \ + " " #op " %0, %0, %1\n" \ + " s32i %0, %2, 0\n" \ + " wsr a15, ps\n" \ + " rsync\n" \ + : "=&a" (vval) \ + : "a" (i), "a" (v) \ + : "a15", "memory" \ + ); \ + \ + return vval; \ } -/* -* We use atomic_{add|sub}_return to define other functions. -*/ - -static inline int atomic_add_return (int i,atomic_t * v) -{ -#if XCHAL_HAVE_S32C1I -unsigned long tmp; -int result; +#endif /* XCHAL_HAVE_S32C1I */ - __asm__ __volatile__( -"1: l32i %1, %3, 0\n" -" wsr %1, scompare1\n" -" add %0, %1, %2\n" -" s32c1i %0, %3, 0\n" -" bne %0, %1, 1b\n" -" add %0, %0, %2\n" - :"=&a" (result),"=&a" (tmp) - :"a" (i),"a" (v) - :"memory" - ); - -return result; -#else -unsigned int vval; - - __asm__ __volatile__( - " rsil a15,"__stringify(LOCKLEVEL)"\n" -" l32i %0, %2, 0\n" -" add %0, %0, %1\n" -" s32i %0, %2, 0\n" -" wsr a15, ps\n" -" rsync\n" - : "=&a" (vval) - : "a" (i), "a" (v) - : "a15", "memory" - ); +#define ATOMIC_OPS (op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) -return vval; -#endif -} +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -static inline int atomic_sub_return (int i,atomic_t * v) -{ -#if XCHAL_HAVE_S32C1I -unsigned long tmp; -int result; - - __asm__ __volatile__( -"1: l32i %1, %3, 0\n" -" wsr %1, scompare1\n" -" sub %0, %1, %2\n" -" s32c1i %0, %3, 0\n" -" bne %0, %1, 1b\n" -" sub %0, %0, %2\n" - :"=&a" (result),"=&a" (tmp) - :"a" (i),"a" (v) - :"memory" - ); - -return result; -#else -unsigned int vval; - - __asm__ __volatile__( - " rsil a15,"__stringify(LOCKLEVEL)"\n" -" l32i %0, %2, 0\n" -" sub %0, %0, %1\n" -" s32i %0, %2, 0\n" -" wsr a15, ps\n" -" rsync\n" - : "=&a" (vval) - : "a" (i), "a" (v) - : "a15", "memory" - ); - -return vval; -#endif -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP/** * atomic_sub_and_test - subtract value from variable and test result
Geert Uytterhoeven
maintainerRe: [PATCH 07/20] arch,cris: Fold atomic_ops
In reply to Peter ZijlstraHi Peter,
Quoted 20 lines · Peter Zijlstra
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Cc Geert Uytterhoeven <[email protected]>Cc Jesper Nilsson <[email protected]>Cc Linus Torvalds <[email protected]>Cc Mikael Starvik <[email protected]>Signed-off-by Peter Zijlstra <[email protected]>--- arch/cris/include/arch-v10/arch/system.h | 2 - arch/cris/include/asm/atomic.h | 57 +++++++++++++------------------ 2 files changed, 25 insertions(+), 34 deletions(-)Index:
linux-2.6/arch/cris/include/arch-v10/arch/system.h
===================================================================
arch/cris/include/arch-v10/arch/system.h static inline unsigned long _get_base(ch · line 40
Quoted 5 lines
arch/cris/include/arch-v10/arch/system.h +0
-2@@ -36,8 +36,6 @@ static inline unsigned long _get_base(chreturn 0 ; } -#define nop () __asm__ __volatile__ ("nop"); -
Is this change supposed to be part of this series?
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Re: [PATCH 20/20] arch: Rewrite generic atomic support
In reply to Peter ZijlstraQuoted 8 lines
-/**
- *atomic_add_return- add integer to atomic variable
- * @i: integer value to add
- * @v: pointer of typeatomic_t
- *
- * Atomically adds @i to @v and returns the result
- */
For people not starign at kernel code every days these sparse comments is a help. If anything they should be improved rather than deleted.
Sam
Peter Zijlstra
maintainerRe: [PATCH 07/20] arch,cris: Fold atomic_ops
In reply to Geert UytterhoevenQuoted 26 lines · Geert Uytterhoeven
Hi Peter,
Quoted 23 lines · Peter Zijlstra
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Cc Geert Uytterhoeven <[email protected]>Cc Jesper Nilsson <[email protected]>Cc Linus Torvalds <[email protected]>Cc Mikael Starvik <[email protected]>Signed-off-by Peter Zijlstra <[email protected]>--- arch/cris/include/arch-v10/arch/system.h | 2 - arch/cris/include/asm/atomic.h | 57 +++++++++++++------------------ 2 files changed, 25 insertions(+), 34 deletions(-)Index:
linux-2.6/arch/cris/include/arch-v10/arch/system.h
===================================================================arch/cris/include/arch-v10/arch/system.h +0
-0@@ -36,8 +36,6 @@ static inline unsigned long _get_base(chreturn 0 ; }
Quoted 4 lines
arch/cris/include/arch-v10/arch/system.h static inline unsigned long _get_base(ch · line 40
Quoted 2 lines
arch/cris/include/arch-v10/arch/system.h +0
-2-#define nop () __asm__ __volatile__ ("nop"); -Is this change supposed to be part of this series?
Uhm, lemme double check that. Good spotting.
Nope, it builds just fine without that hunk, *poof* gone.
Thanks!
David Miller
maintainerRe: [PATCH 07/20] arch,cris: Fold atomic_ops
From: Peter Zijlstra <[email protected]>
Date: Thu, 8 May 2014 18:06:06 +0200
Quoted 27 lines · Geert Uytterhoeven
Quoted 26 lines
Hi Peter,
Quoted 23 lines · Peter Zijlstra
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Cc Geert Uytterhoeven <[email protected]>Cc Jesper Nilsson <[email protected]>Cc Linus Torvalds <[email protected]>Cc Mikael Starvik <[email protected]>Signed-off-by Peter Zijlstra <[email protected]>--- arch/cris/include/arch-v10/arch/system.h | 2 - arch/cris/include/asm/atomic.h | 57 +++++++++++++------------------ 2 files changed, 25 insertions(+), 34 deletions(-)Index:
linux-2.6/arch/cris/include/arch-v10/arch/system.h
===================================================================arch/cris/include/arch-v10/arch/system.h +0
-0@@ -36,8 +36,6 @@ static inline unsigned long _get_base(chreturn 0 ; }
Quoted 8 lines
Quoted 4 lines
arch/cris/include/arch-v10/arch/system.h static inline unsigned long _get_base(ch · line 40
Quoted 2 lines
arch/cris/include/arch-v10/arch/system.h +0
-2-#define nop () __asm__ __volatile__ ("nop"); -Is this change supposed to be part of this series?
Uhm, lemme double check that. Good spotting.
Nope, it builds just fine without that hunk, *poof* gone.
You did this for sparc too.
Peter Zijlstra
maintainerRe: [PATCH 07/20] arch,cris: Fold atomic_ops
Quoted 1 line · David Miller
You did this for sparc too.
I did indeed.
And sparc32 and sparc64 compile file without these spurious hunks.
I'm sure I needed that to make it compile at one point, but I suspect they're remnants from the smp_mb__ shuffle which needed include file changes.
But since then I've redone those and solved the compile issues differently, but these hunks stuck around...
I've gone over the other patches, cris and sparc appear to be the only ones so affected.
Just to make sure; these two hunks just went into the bit-bucket:
arch/sparc/include/asm/barrier_32.h +0 -1
@@ -1,7 +1,6 @@#ifndef __SPARC_BARRIER_H#define __SPARC_BARRIER_H -#include <asm/processor.h> /* for nop() */ #include <asm-generic/barrier.h> #endif /* !(__SPARC_BARRIER_H) */
arch/sparc/include/asm/processor.h +0 -2
@@ -6,6 +6,4 @@#include <asm/processor_32.h> #endif -#define nop () __asm__ __volatile__ ("nop") -#endif
Peter Zijlstra
maintainerRe: [PATCH 20/20] arch: Rewrite generic atomic support
In reply to Sam RavnborgQuoted 10 lines · Sam Ravnborg
Quoted 8 lines
-/**
- *atomic_add_return- add integer to atomic variable
- * @i: integer value to add
- * @v: pointer of typeatomic_t
- *
- * Atomically adds @i to @v and returns the result
- */For people not starign at kernel code every days these sparse comments is a help. If anything they should be improved rather than deleted.
Does something like this help? Its a bit tedious to have to repeat the comment while we just collapsed all the implementations.
include/asm-generic/atomic.h +17 -0
@@ -18,6 +18,23 @@#include <asm/cmpxchg.h> #include <asm/barrier.h> +/* +* atomic_$op() - $op integer to atomic variable +* @i: integer value to $op +* @v: pointer to the atomic variable +* +* Atomically $ops @i to @v. Does not strictly guarantee a memory-barrier, use +* smp_mb__{before,after}_atomic(). +*/ + +/* +* atomic_$op_return() - $op interer to atomic variable and returns the result +* @i: integer value to $op +* @v: pointer to the atomic variable +* +* Atomically $ops @i to @v. Does imply a full memory barrier. +*/ +#ifdef CONFIG_SMP /* we can build all atomic primitives from cmpxchg */
Re: [PATCH 04/20] arch,arm: Fold atomic_ops
In reply to Peter ZijlstraQuoted 16 lines · Peter Zijlstra
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires the
asm_opbecause of eor.Cc Albin Tonnerre <[email protected]>Cc Catalin Marinas <[email protected]>Cc Chen Gang <[email protected]>Cc Linus Torvalds <[email protected]>Cc Nicolas Pitre <[email protected]>Cc Russell King <[email protected]>Cc Victor Kamensky <[email protected]>Cc Will Deacon <[email protected]>Signed-off-by Peter Zijlstra <[email protected]>
Quoted 3 lines
--- arch/arm/include/asm/atomic.h | 313 +++++++++++++++++------------------------- 1 file changed, 127 insertions(+), 186 deletions(-)
Will
Re: [PATCH 05/20] arch,arm64: Fold atomic_ops
In reply to Peter ZijlstraQuoted 13 lines · Peter Zijlstra
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires the
asm_opdue to eor.Cc Catalin Marinas <[email protected]>Cc Chen Gang <[email protected]>Cc Linus Torvalds <[email protected]>Cc Russell King <[email protected]>Cc Will Deacon <[email protected]>Signed-off-by Peter Zijlstra <[email protected]>
Quoted 3 lines
--- arch/arm64/include/asm/atomic.h | 213 ++++++++++++++++------------------------ 1 file changed, 88 insertions(+), 125 deletions(-)
Will
Re: [PATCH 07/20] arch,cris: Fold atomic_ops
In reply to Peter ZijlstraFrom: Peter Zijlstra <[email protected]>
Date: Thu, 8 May 2014 20:17:34 +0200
Quoted 11 lines · David Miller
Quoted 1 line
You did this for sparc too.
I did indeed.
And sparc32 and sparc64 compile file without these spurious hunks.
I'm sure I needed that to make it compile at one point, but I suspect they're remnants from the
smp_mb__shuffle which needed include file changes.
Quoted 7 lines
But since then I've redone those and solved the compile issues differently, but these hunks stuck around...
I've gone over the other patches, cris and sparc appear to be the only ones so affected.
Just to make sure; these two hunks just went into the bit-bucket:
Thanks, with those changes:
Jesper Nilsson
Acked-byRe: [PATCH 07/20] arch,cris: Fold atomic_ops
In reply to Peter ZijlstraQuoted 11 lines · Peter Zijlstra
Quoted 1 line · David Miller
You did this for sparc too.
I did indeed.
And sparc32 and sparc64 compile file without these spurious hunks.
I'm sure I needed that to make it compile at one point, but I suspect they're remnants from the
smp_mb__shuffle which needed include file changes.
Quoted 5 lines
But since then I've redone those and solved the compile issues differently, but these hunks stuck around...
I've gone over the other patches, cris and sparc appear to be the only ones so affected.
With those changes, here's my
/^JN - Jesper Nilsson
Signature · 1 line
Jesper Nilsson -- [email protected]
Geert Uytterhoeven
maintainerRe: [PATCH 11/20] arch,m68k: Fold atomic_ops
In reply to Peter ZijlstraHi Peter,
Quoted 9 lines · Peter Zijlstra
arch/m68k/include/asm/atomic.h +4
-4+#ifdef CONFIG_RMW_INSNS -static inline void atomic_sub (int i,atomic_t *v) -{ - __asm__ __volatile__("subl %1,%0" : "+m" (*v) : ASM_DI (i)); -} +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \
arch/m68k/include/asm/atomic.h
Quoted 10 lines
arch/m68k/include/asm/atomic.h +9
-0+ __asm__ __volatile__(#asm_op "l %1,%0" : "+m" (*v) : ASM_DI (i));\ +} \ +#else + +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + __asm__ __volatile__(#asm_op "l %1,%0" : "+m" (*v) : ASM_DI (i));\ +} \
These two chunks are identical, so please move them out of the #ifdef.
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Peter Zijlstra
maintainerRe: [PATCH 11/20] arch,m68k: Fold atomic_ops
Quoted 1 line · Geert Uytterhoeven
These two chunks are identical, so please move them out of the #ifdef.
More lines saved!
---
Subject: arch,m68k: Fold atomic_ops
From: Peter Zijlstra <[email protected]>
Date: Sun Mar 23 19:06:34 CET 2014
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires asm_op due to eor.
--- arch/m68k/include/asm/atomic.h | 113 +++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 64 deletions(-)
arch/m68k/include/asm/atomic.h +49 -64
@@ -30,15 +30,56 @@#define ASM_DI "di"#endif -static inline void atomic_add (int i,atomic_t *v) -{ - __asm__ __volatile__("addl %1,%0" : "+m" (*v) : ASM_DI (i)); -} +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + __asm__ __volatile__(#asm_op "l %1,%0" : "+m" (*v) : ASM_DI (i));\ +} \ -static inline void atomic_sub (int i,atomic_t *v) -{ - __asm__ __volatile__("subl %1,%0" : "+m" (*v) : ASM_DI (i)); -} +#ifdef CONFIG_RMW_INSNS + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int t, tmp; \ + \ + __asm__ __volatile__( \ + "1: movel %2,%1\n" \ + " " #asm_op "l %3,%1\n" \ + " casl %2,%1,%0\n" \ + " jne 1b" \ + : "+m" (*v), "=&d" (t), "=&d" (tmp) \ + : "g" (i), "2" (atomic_read(v))); \ + return t; \ +} + +#else + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t * v) \ +{ \ + unsigned long flags; \ + int t; \ + \ + local_irq_save(flags); \ + t = (v->counter c_op i); \ + local_irq_restore(flags); \ + \ + return t; \ +} + +#endif /* CONFIG_RMW_INSNS */ + +#define ATOMIC_OPS (op, c_op, asm_op) \ + ATOMIC_OP(op, c_op, asm_op) \ + ATOMIC_OP_RETURN(op, c_op, asm_op) + +ATOMIC_OPS(add, +=, add) +ATOMIC_OPS(sub, -=, sub) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OPstatic inline void atomic_inc (atomic_t *v) {
@@ -76,67 +117,11 @@ static inline int atomic_inc_and_test(at#ifdef CONFIG_RMW_INSNS -static inline int atomic_add_return (int i,atomic_t *v) -{ -int t, tmp; - - __asm__ __volatile__( -"1: movel %2,%1\n" -" addl %3,%1\n" -" casl %2,%1,%0\n" -" jne 1b" - :"+m" (*v),"=&d" (t),"=&d" (tmp) - :"g" (i),"2" (atomic_read (v))); -return t; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int t, tmp; - - __asm__ __volatile__( -"1: movel %2,%1\n" -" subl %3,%1\n" -" casl %2,%1,%0\n" -" jne 1b" - :"+m" (*v),"=&d" (t),"=&d" (tmp) - :"g" (i),"2" (atomic_read (v))); -return t; -} -#define atomic_cmpxchg (v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))#define atomic_xchg (v, new) (xchg(&((v)->counter), new))#else /* !CONFIG_RMW_INSNS */ -static inline int atomic_add_return (int i,atomic_t * v) -{ -unsigned long flags; -int t; - -local_irq_save (flags); - t =atomic_read (v); - t += i; -atomic_set (v, t); -local_irq_restore (flags); - -return t; -} - -static inline int atomic_sub_return (int i,atomic_t * v) -{ -unsigned long flags; -int t; - -local_irq_save (flags); - t =atomic_read (v); - t -= i; -atomic_set (v, t); -local_irq_restore (flags); - -return t; -} -static inline int atomic_cmpxchg (atomic_t *v,int old,int new) {unsigned long flags;
Re: [PATCH 03/20] arch,arc: Fold atomic_ops
In reply to Peter ZijlstraHi Peter,
Minor point below, otherwise looks good.
Quoted 215 lines · Peter Zijlstra
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Cc Linus Torvalds <[email protected]>Cc Vineet Gupta <[email protected]>Signed-off-by Peter Zijlstra <[email protected]>--- arch/arc/include/asm/atomic.h | 194 ++++++++++++++---------------------------- 1 file changed, 68 insertions(+), 126 deletions(-)arch/arc/include/asm/atomic.h +54
-126@@ -25,79 +25,36 @@#define atomic_set (v, i) (((v)->counter) = (i)) -static inline void atomic_add (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" add %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp)/* Early clobber, to prevent reg reuse */ - :"r" (&v->counter),"ir" (i) - :"cc" ); -} - -static inline void atomic_sub (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" sub %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (&v->counter),"ir" (i) - :"cc" ); -} - -/* add and also return the new value */ -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" add %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (&v->counter),"ir" (i) - :"cc" ); - -return temp; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" sub %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (&v->counter),"ir" (i) - :"cc" ); - -return temp; -} - -static inline void atomic_clear_mask (unsigned long mask,unsigned long *addr) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" bic %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (addr),"ir" (mask) - :"cc" ); +#define ATOMIC_OP(op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned int temp; \ + \ + __asm__ __volatile__( \ + "1: llock %0, [%1] \n" \ + " " #asm_op " %0, %0, %2 \n" \ + " scond %0, [%1] \n" \ + " bnz 1b \n" \ + : "=&r"(temp) /* Early clobber, to prevent reg reuse */ \ + : "r"(&v->counter), "ir"(i) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned int temp; \ + \ + __asm__ __volatile__( \ + "1: llock %0, [%1] \n" \ + " " #asm_op " %0, %0, %2 \n" \ + " scond %0, [%1] \n" \ + " bnz 1b \n" \ + : "=&r"(temp) \ + : "r"(&v->counter), "ir"(i) \ + : "cc"); \ + \ + return temp; \ }#else /* !CONFIG_ARC_HAS_LLSC */ @@ -126,6 +83,7 @@ static inline void atomic_set(atomic_t * v->counter = i; atomic_ops_unlock(flags); } +#endif /* @@ -133,63 +91,47 @@ static inline void atomic_set(atomic_t ** Locking would change to irq-disabling only (UP) and spinlocks (SMP) */ -static inline void atomic_add (int i,atomic_t *v) -{ -unsigned long flags; - -atomic_ops_lock (flags); - v->counter += i; -atomic_ops_unlock (flags); -} - -static inline void atomic_sub (int i,atomic_t *v) -{ -unsigned long flags; - -atomic_ops_lock (flags); - v->counter -= i; -atomic_ops_unlock (flags); -} - -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long flags; -unsigned long temp; - -atomic_ops_lock (flags); - temp = v->counter; - temp += i; - v->counter = temp; -atomic_ops_unlock (flags); - -return temp; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long flags; -unsigned long temp; - -atomic_ops_lock (flags); - temp = v->counter; - temp -= i; - v->counter = temp; -atomic_ops_unlock (flags); - -return temp; -} - -static inline void atomic_clear_mask (unsigned long mask,unsigned long *addr) -{ -unsigned long flags; - -atomic_ops_lock (flags); - *addr &= ~mask; -atomic_ops_unlock (flags); +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + \ + atomic_ops_lock(flags); \ + v->counter c_op i; \ + atomic_ops_unlock(flags); \ +} + +#define ATOMIC_OP_RETURN (op, c_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + unsigned long temp; \ + \ + atomic_ops_lock(flags); \ + temp = v->counter; \ + temp c_op i; \ + v->counter = temp; \ + atomic_ops_unlock(flags); \ + \ + return temp; \ }#endif /* !CONFIG_ARC_HAS_LLSC */
arch/arc/include/asm/atomic.h static inline void atomic_set(atomic_t * · line 129
Quoted 10 lines
arch/arc/include/asm/atomic.h +9
-0+#define ATOMIC_OPS (op, c_op, asm_op) \ + ATOMIC_OP(op, c_op, asm_op) \ + ATOMIC_OP_RETURN(op, c_op, asm_op) + +ATOMIC_OPS(add, +=, add) +ATOMIC_OPS(sub, -=, sub) +ATOMIC_OP(and, &=, and) + +#define atomic_clear_mask (mask, v) atomic_and(~(mask), (v))
Given that ARC has instruction to do just that, can we keep below instead.
ATOMIC_OP(clear_mask, ~=, bic)
(see asm version of atomic_clear_mask)
-Vineet
Quoted 11 lines
arch/arc/include/asm/atomic.h +5
-0+ +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP +/** * __atomic_add_unless - add unless the number is a given value * @v: pointer of type atomic_t
Re: [PATCH 11/20] arch,m68k: Fold atomic_ops
In reply to Peter ZijlstraQuoted 13 lines · Peter Zijlstra
Quoted 1 line · Geert Uytterhoeven
These two chunks are identical, so please move them out of the #ifdef.
More lines saved!
---
Subject: arch,m68k: Foldatomic_ops
From: Peter Zijlstra <[email protected]>
Date: Sun Mar 23 19:06:34 CET 2014Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
Quoted 7 lines
This also prepares for easy addition of new ops.
Requires
asm_opdue to eor.Cc Geert Uytterhoeven <[email protected]>Cc Linus Torvalds <[email protected]>Signed-off-by Peter Zijlstra <[email protected]>
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Peter Zijlstra
maintainerRe: [PATCH 03/20] arch,arc: Fold atomic_ops
In reply to Vineet GuptaQuoted 11 lines · Vineet Gupta
Hi Peter,
Minor point below, otherwise looks good.
Quoted 1 line
+#define
atomic_clear_mask(mask, v)atomic_and(~(mask), (v))Given that ARC has instruction to do just that, can we keep below instead.
ATOMIC_OP(clear_mask, ~=, bic)(see asm version of
atomic_clear_mask)
Ah, yes I did notices that.
The thing is, I was going to kill clear_mask in the next set.
I'm going to introduce: atomic_{or,and,xor}() and replace all atomic_set_mask() with atomic_or() and atomic_clear_mask() with atomic_and(~(mask)).
One of the 'problems' is that atomic_{set,clear}_mask() are not available on all archs and its a 'weird' interface in so far as that most our other primitives are or/and based, I don't think we have nand.
But if people really want I suppose I could also add atomic_nand().
Re: [PATCH 06/20] arch,avr32: Fold atomic_ops
In reply to Peter ZijlstraQuoted 2 lines · Peter Zijlstra
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
The add and sub atomic operations are not 100% the same. Sub has more constraints on the integer size than add. Sub only takes a signed 21-bit integer, while add can do 32-bit additions IIRC correctly the instructions for AVR32.
This is why you see in atomic_sub_return() that i is typed as "rKs21", while in atomic_add_return, i is typed "r".
Your change limits both atomic operations to work only on signed 21-bit integers.
Quoted 45 lines
This also prepares for easy addition of new ops.
Requires the
asm_opbecause of eor.Cc Haavard Skinnemoen <[email protected]>Cc Hans-Christian Egtvedt <[email protected]>Cc Linus Torvalds <[email protected]>Signed-off-by Peter Zijlstra <[email protected]>--- arch/avr32/include/asm/atomic.h | 96 ++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 52 deletions(-)arch/avr32/include/asm/atomic.h +4
-23@@ -22,58 +22,52 @@#define atomic_read (v) (*(volatile int *)&(v)->counter)#define atomic_set (v, i) (((v)->counter) = i) -/* -* atomic_sub_return - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v. Returns the resulting value. -*/ -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int result; - - asmvolatile ( -"/* atomic_sub_return */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" -" sub %0, %3\n" -" stcond %1, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"rKs21" (i) - :"cc" ); - -return result; +#define ATOMIC_OP (op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int result; \
arch/avr32/include/asm/atomic.h line 38
Quoted 10 lines
arch/avr32/include/asm/atomic.h +10
-0+ \ + asm volatile( \ + "/* atomic_" #op " */\n" \ + "1: ssrf 5\n" \ + " ld.w %0, %2\n" \ + " " #asm_op " %0, %3\n" \ + " stcond %1, %0\n" \ + " brne 1b" \ + : "=&r"(result), "=o"(v->counter) \ + : "m"(v->counter), "rKs21"(i) \
All variable i integers are limited to signed 21-bit here.
Quoted 7 lines
arch/avr32/include/asm/atomic.h +7
-0+ : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN (op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int result; \
arch/avr32/include/asm/atomic.h line 55
Quoted 10 lines
arch/avr32/include/asm/atomic.h +10
-0+ \ + asm volatile( \ + "/* atomic_" #op "_return */\n" \ + "1: ssrf 5\n" \ + " ld.w %0, %2\n" \ + " " #asm_op " %0, %3\n" \ + " stcond %1, %0\n" \ + " brne 1b" \ + : "=&r"(result), "=o"(v->counter) \ + : "m"(v->counter), "rKs21"(i) \
Same here.
Quoted 8 lines
arch/avr32/include/asm/atomic.h +3
-3+ : "cc"); \ + \ +return result; \ } -/* -* atomic_add_return - add integer to atomic variable -* @i: integer value to add
arch/avr32/include/asm/atomic.h line 62
Quoted 10 lines
arch/avr32/include/asm/atomic.h +0
-10-* @v: pointer of type atomic_t -* -* Atomically adds @i to @v. Returns the resulting value. -*/ -static inline int atomic_add_return (int i,atomic_t *v) -{ -int result; - -if (__builtin_constant_p (i) && (i >=-1048575 ) && (i <=1048576 )) - result =atomic_sub_return (-i, v);
I do not recall why we did it like this any more, I would assume both sub and add to be single cycle instructions.
Quoted 28 lines
arch/avr32/include/asm/atomic.h +10
-14-else - asmvolatile ( -"/* atomic_add_return */\n" -"1: ssrf 5\n" -" ld.w %0, %1\n" -" add %0, %3\n" -" stcond %2, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"r" (i) - :"cc" ,"memory" ); - -return result; -} +#define ATOMIC_OPS (op, asm_op) \ + ATOMIC_OP(op, asm_op) \ + ATOMIC_OP_RETURN(op, asm_op) + +ATOMIC_OPS(add, add) +ATOMIC_OPS(sub, sub) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP/* * atomic_sub_unless - sub unless the number is a given value
arch/avr32/include/asm/atomic.h static inline int atomic_sub_if_positive · line 166
Quoted 7 lines
arch/avr32/include/asm/atomic.h +0
-2@@ -168,8 +162,6 @@ static inline int atomic_sub_if_positive#define atomic_xchg (v, new) (xchg(&((v)->counter), new))#define atomic_cmpxchg (v, o, n) (cmpxchg(&((v)->counter), (o), (n))) -#define atomic_sub (i, v) (void)atomic_sub_return(i, v) -#define atomic_add (i, v) (void)atomic_add_return(i, v)#define atomic_dec (v) atomic_sub(1, (v))#define atomic_inc (v) atomic_add(1, (v))
I like the simplification, do you manage to preprocessor both instruction and instruction argument limit?
Signature · 2 lines
Best regards, Hans-Christian Egtvedt
Peter Zijlstra
maintainerRe: [PATCH 06/20] arch,avr32: Fold atomic_ops
Quoted 9 lines · Hans-Christian Egtvedt
Quoted 2 lines · Peter Zijlstra
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
The add and sub atomic operations are not 100% the same. Sub has more constraints on the integer size than add. Sub only takes a signed 21-bit integer, while add can do 32-bit additions IIRC correctly the instructions for AVR32.
Quoted 5 lines
This is why you see in
atomic_sub_return()that i is typed as "rKs21", while inatomic_add_return, i is typed "r".Your change limits both atomic operations to work only on signed 21-bit integers.
Urgh, fail on me for not seeing that.
Quoted 5 lines
arch/avr32/include/asm/atomic.h line 62
Quoted 2 lines
arch/avr32/include/asm/atomic.h +0
-2-if (__builtin_constant_p (i) && (i >=-1048575 ) && (i <=1048576 )) - result =atomic_sub_return (-i, v);I do not recall why we did it like this any more, I would assume both sub and add to be single cycle instructions.
Right and if its a constant the negate is compile time too.
OK, so if I only generate add and provide inline stubs to implement sub with add this should be good again, right?
Any other instructions I should be careful with? I take it the bit ops are full 32 bits again?
Peter Zijlstra
maintainerRe: [PATCH 06/20] arch,avr32: Fold atomic_ops
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Hans-Christian Egtvedt
arch/avr32/include/asm/atomic.h line 62
Quoted 2 lines
arch/avr32/include/asm/atomic.h +0
-2-if (__builtin_constant_p (i) && (i >=-1048575 ) && (i <=1048576 )) - result =atomic_sub_return (-i, v);I do not recall why we did it like this any more, I would assume both sub and add to be single cycle instructions.
Similarly, can I rip out atomic_sub_unless() ?
Peter Zijlstra
maintainerRe: [PATCH 06/20] arch,avr32: Fold atomic_ops
Quoted 10 lines · Peter Zijlstra
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Hans-Christian Egtvedt
Review
Quoted 2 lines
(quoted diff) +0
-2- if (__builtin_constant_p(i) && (i >= -1048575) && (i <= 1048576)) - result = atomic_sub_return(-i, v);I do not recall why we did it like this any more, I would assume both sub and add to be single cycle instructions.
Similarly, can I rip out
atomic_sub_unless()?
Something like so?
---
Subject: arch,avr32: Fold atomic_ops
From: Peter Zijlstra <[email protected]>
Date: Wed Apr 9 21:51:29 CEST 2014
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires the asm_op because of eor.
The avr32 sub instruction is limited to 21 bits, Hans-Christian cannot recall the reason for the weird __builtin_constant_p() tests so take all that out and use a straight fwd negate add instead of subtract.
--- arch/avr32/include/asm/atomic.h | 145 +++++++++++++++------------------------- 1 file changed, 55 insertions(+), 90 deletions(-)
arch/avr32/include/asm/atomic.h +55 -90
@@ -22,117 +22,84 @@#define atomic_read (v) (*(volatile int *)&(v)->counter)#define atomic_set (v, i) (((v)->counter) = i) -/* -* atomic_sub_return - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v. Returns the resulting value. -*/ -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int result; - - asmvolatile ( -"/* atomic_sub_return */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" -" sub %0, %3\n" -" stcond %1, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"rKs21" (i) - :"cc" ); - -return result; +#define ATOMIC_OP(op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int result; \ + \ + asm volatile( \ + "/* atomic_" #op " */\n" \ + "1: ssrf 5\n" \ + " ld.w %0, %2\n" \ + " " #asm_op " %0, %3\n" \ + " stcond %1, %0\n" \ + " brne 1b" \ + : "=&r"(result), "=o"(v->counter) \ + : "m"(v->counter), "r"(i) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN(op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int result; \ + \ + asm volatile( \ + "/* atomic_" #op "_return */\n" \ + "1: ssrf 5\n" \ + " ld.w %0, %2\n" \ + " " #asm_op " %0, %3\n" \ + " stcond %1, %0\n" \ + " brne 1b" \ + : "=&r"(result), "=o"(v->counter) \ + : "m"(v->counter), "r"(i) \ + : "cc"); \ + \ + return result; \ } +#define ATOMIC_OPS (op, asm_op) \ + ATOMIC_OP(op, asm_op) \ + ATOMIC_OP_RETURN(op, asm_op) + +ATOMIC_OPS(add, add) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP +/* -* atomic_add_return - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v. Returns the resulting value. +* The 'sub' instruction is limited to 21 bits, use negate add instead. */ -static inline int atomic_add_return (int i,atomic_t *v) -{ -int result; - -if (__builtin_constant_p (i) && (i >=-1048575 ) && (i <=1048576 )) - result =atomic_sub_return (-i, v); -else - asmvolatile ( -"/* atomic_add_return */\n" -"1: ssrf 5\n" -" ld.w %0, %1\n" -" add %0, %3\n" -" stcond %2, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"r" (i) - :"cc" ,"memory" ); - -return result; -} +#define atomic_sub (i, v) atomic_add(-(i), (v)) +#define atomic_sub_return (i, v) atomic_add_return(-(i), (v))/* -* atomic_sub_unless - sub unless the number is a given value +* __atomic_add_unless - add unless the number is a given value * @v: pointer of type atomic_t -* @a: the amount to subtract from v... +* @a: the amount to add to v... * @u: ...unless v is equal to u. * -* Atomically subtract @a from @v, so long as it was not @u. +* Atomically adds @a to @v, so long as it was not @u. * Returns the old value of @v. */ -static inline void atomic_sub_unless (atomic_t *v,int a,int u) +static inline int __atomic_add_unless (atomic_t *v,int a,int u) { -int tmp; +int tmp, old =atomic_read (v); asmvolatile ( -"/* atomic_sub_unless */\n" +"/* __atomic_add_unless */\n" "1: ssrf 5\n" " ld.w %0, %2\n" " cp.w %0, %4\n" " breq 1f\n" -" sub %0, %3\n" +" add %0, %3\n" " stcond %1, %0\n" " brne 1b\n" "1:" :"=&r" (tmp),"=o" (v->counter) - :"m" (v->counter),"rKs21" (a),"rKs21" (u) + :"m" (v->counter),"r" (a),"ir" (u) :"cc" ,"memory" ); -} - -/* -* __atomic_add_unless - add unless the number is a given value -* @v: pointer of type atomic_t -* @a: the amount to add to v... -* @u: ...unless v is equal to u. -* -* Atomically adds @a to @v, so long as it was not @u. -* Returns the old value of @v. -*/ -static inline int __atomic_add_unless (atomic_t *v,int a,int u) -{ -int tmp, old =atomic_read (v); - -if (__builtin_constant_p (a) && (a >=-1048575 ) && (a <=1048576 )) -atomic_sub_unless (v, -a, u); -else { - asmvolatile ( -"/* __atomic_add_unless */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" -" cp.w %0, %4\n" -" breq 1f\n" -" add %0, %3\n" -" stcond %1, %0\n" -" brne 1b\n" -"1:" - :"=&r" (tmp),"=o" (v->counter) - :"m" (v->counter),"r" (a),"ir" (u) - :"cc" ,"memory" ); - }return old; }
@@ -168,8 +135,6 @@ static inline int atomic_sub_if_positive#define atomic_xchg (v, new) (xchg(&((v)->counter), new))#define atomic_cmpxchg (v, o, n) (cmpxchg(&((v)->counter), (o), (n))) -#define atomic_sub (i, v) (void)atomic_sub_return(i, v) -#define atomic_add (i, v) (void)atomic_add_return(i, v)#define atomic_dec (v) atomic_sub(1, (v))#define atomic_inc (v) atomic_add(1, (v))
Re: [PATCH 08/20] arch,hexagon: Fold atomic_ops
In reply to Peter ZijlstraQuoted 4 lines · Peter Zijlstra
OK, no LoC saved in this case because the !return variants were defined in terms of the return ops. Still do it because this also prepares for easy addition of new ops.
Quoted 8 lines
Cc Linus Torvalds <[email protected]>Cc Richard Kuo <[email protected]>Cc Vineet Gupta <[email protected]>Signed-off-by Peter Zijlstra <[email protected]>--- arch/hexagon/include/asm/atomic.h | 68 ++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 31 deletions(-)
Signature · 3 lines
Sent by an employee of the Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
Re: [PATCH 12/20] arch,metag: Fold atomic_ops
In reply to Peter ZijlstraQuoted 8 lines · Peter Zijlstra
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Cc James Hogan <[email protected]>Cc Linus Torvalds <[email protected]>Signed-off-by Peter Zijlstra <[email protected]>
Looks good and disassembly is unchanged for both lnkget/lnkset and lock1.
Let me know if you want this patch taken separately through the metag tree.
Thanks
James
Quoted 234 lines
--- arch/metag/include/asm/atomic_lnkget.h | 121 ++++++++++++--------------------- arch/metag/include/asm/atomic_lock1.h | 76 ++++++++------------ 2 files changed, 77 insertions(+), 120 deletions(-)Index:
linux-2.6/arch/metag/include/asm/atomic_lnkget.h
===================================================================arch/metag/include/asm/atomic_lnkget.h +46
-75@@ -27,85 +27,56 @@ static inline int atomic_read(const atomreturn temp; } -static inline void atomic_add (int i,atomic_t *v) -{ -int temp; - - asmvolatile ( -"1: LNKGETD %0, [%1]\n" -" ADD %0, %0, %2\n" -" LNKSETD [%1], %0\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp) - :"da" (&v->counter),"bd" (i) - :"cc" ); +#define ATOMIC_OP (op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int temp; \ + \ + asm volatile ( \ + "1: LNKGETD %0, [%1]\n" \ + " " #op " %0, %0, %2\n" \ + " LNKSETD [%1], %0\n" \ + " DEFR %0, TXSTAT\n" \ + " ANDT %0, %0, #HI(0x3f000000)\n" \ + " CMPT %0, #HI(0x02000000)\n" \ + " BNZ 1b\n" \ + : "=&d" (temp) \ + : "da" (&v->counter), "bd" (i) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int result, temp; \ + \ + smp_mb(); \ + \ + asm volatile ( \ + "1: LNKGETD %1, [%2]\n" \ + " " #op " %1, %1, %3\n" \ + " LNKSETD [%2], %1\n" \ + " DEFR %0, TXSTAT\n" \ + " ANDT %0, %0, #HI(0x3f000000)\n" \ + " CMPT %0, #HI(0x02000000)\n" \ + " BNZ 1b\n" \ + : "=&d" (temp), "=&da" (result) \ + : "da" (&v->counter), "bd" (i) \ + : "cc"); \ + \ + smp_mb(); \ + \ + return result; \ } -static inline void atomic_sub (int i,atomic_t *v) -{ -int temp; - - asmvolatile ( -"1: LNKGETD %0, [%1]\n" -" SUB %0, %0, %2\n" -" LNKSETD [%1], %0\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp) - :"da" (&v->counter),"bd" (i) - :"cc" ); -} - -static inline int atomic_add_return (int i,atomic_t *v) -{ -int result, temp; - -smp_mb (); - - asmvolatile ( -"1: LNKGETD %1, [%2]\n" -" ADD %1, %1, %3\n" -" LNKSETD [%2], %1\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp),"=&da" (result) - :"da" (&v->counter),"bd" (i) - :"cc" ); +#define ATOMIC_OPS (op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) -smp_mb (); +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -return result; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int result, temp; - -smp_mb (); - - asmvolatile ( -"1: LNKGETD %1, [%2]\n" -" SUB %1, %1, %3\n" -" LNKSETD [%2], %1\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp),"=&da" (result) - :"da" (&v->counter),"bd" (i) - :"cc" ); - -smp_mb (); - -return result; -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OPstatic inline void atomic_clear_mask (unsigned int mask,atomic_t *v) {Index:
linux-2.6/arch/metag/include/asm/atomic_lock1.h
===================================================================arch/metag/include/asm/atomic_lock1.h +31
-45@@ -37,55 +37,41 @@ static inline int atomic_set(atomic_t *vreturn i; } -static inline void atomic_add (int i,atomic_t *v) -{ -unsigned long flags; - -__global_lock1 (flags); -fence (); - v->counter += i; -__global_unlock1 (flags); +#define ATOMIC_OP (op, c_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + \ + __global_lock1(flags); \ + fence(); \ + v->counter c_op i; \ + __global_unlock1(flags); \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long result; \ + unsigned long flags; \ + \ + __global_lock1(flags); \ + result = v->counter; \ + result c_op i; \ + fence(); \ + v->counter = result; \ + __global_unlock1(flags); \ + \ + return result; \ } -static inline void atomic_sub (int i,atomic_t *v) -{ -unsigned long flags; - -__global_lock1 (flags); -fence (); - v->counter -= i; -__global_unlock1 (flags); -} - -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long result; -unsigned long flags; +#define ATOMIC_OPS (op, c_op) ATOMIC_OP(op, c_op) ATOMIC_OP_RETURN(op, c_op) -__global_lock1 (flags); - result = v->counter; - result += i; -fence (); - v->counter = result; -__global_unlock1 (flags); +ATOMIC_OPS(add, +=) +ATOMIC_OPS(sub, -=) -return result; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long result; -unsigned long flags; - -__global_lock1 (flags); - result = v->counter; - result -= i; -fence (); - v->counter = result; -__global_unlock1 (flags); - -return result; -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OPstatic inline void atomic_clear_mask (unsigned int mask,atomic_t *v) {
Re: [PATCH 06/20] arch,avr32: Fold atomic_ops
In reply to Peter ZijlstraQuoted 21 lines · Peter Zijlstra
Quoted 9 lines · Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Hans-Christian Egtvedt
arch/avr32/include/asm/atomic.h line 62
Quoted 2 lines
arch/avr32/include/asm/atomic.h +0
-2-if (__builtin_constant_p (i) && (i >=-1048575 ) && (i <=1048576 )) - result =atomic_sub_return (-i, v);I do not recall why we did it like this any more, I would assume both sub and add to be single cycle instructions.
Similarly, can I rip out
atomic_sub_unless()?Something like so?
---
Subject: arch,avr32: Foldatomic_ops
From: Peter Zijlstra <[email protected]>
Date: Wed Apr 9 21:51:29 CEST 2014Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
Quoted 7 lines
This also prepares for easy addition of new ops.
Requires the
asm_opbecause of eor.The avr32 sub instruction is limited to 21 bits, Hans-Christian cannot recall the reason for the weird
__builtin_constant_p()tests so take all that out and use a straight fwd negate add instead of subtract.
Probably found the reason why we want to use sub with the signed 21-bit limit, it uses one less register than the add instruction that can add up to 32-bit values.
Both instructions are 32-bit, to use a 16-bit instruction the immediate is very small; 4 bit.
sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate. add 32-bit, type II, adds two register values together.
So by simplifying you loose this optimization.
Quoted 193 lines
Cc Hans-Christian Egtvedt <[email protected]>Cc Linus Torvalds <[email protected]>Cc Haavard Skinnemoen <[email protected]>Signed-off-by Peter Zijlstra <[email protected]>--- arch/avr32/include/asm/atomic.h | 145 +++++++++++++++------------------------- 1 file changed, 55 insertions(+), 90 deletions(-)arch/avr32/include/asm/atomic.h +55
-90@@ -22,117 +22,84 @@#define atomic_read (v) (*(volatile int *)&(v)->counter)#define atomic_set (v, i) (((v)->counter) = i) -/* -* atomic_sub_return - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v. Returns the resulting value. -*/ -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int result; - - asmvolatile ( -"/* atomic_sub_return */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" -" sub %0, %3\n" -" stcond %1, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"rKs21" (i) - :"cc" ); - -return result; +#define ATOMIC_OP(op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int result; \ + \ + asm volatile( \ + "/* atomic_" #op " */\n" \ + "1: ssrf 5\n" \ + " ld.w %0, %2\n" \ + " " #asm_op " %0, %3\n" \ + " stcond %1, %0\n" \ + " brne 1b" \ + : "=&r"(result), "=o"(v->counter) \ + : "m"(v->counter), "r"(i) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN(op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int result; \ + \ + asm volatile( \ + "/* atomic_" #op "_return */\n" \ + "1: ssrf 5\n" \ + " ld.w %0, %2\n" \ + " " #asm_op " %0, %3\n" \ + " stcond %1, %0\n" \ + " brne 1b" \ + : "=&r"(result), "=o"(v->counter) \ + : "m"(v->counter), "r"(i) \ + : "cc"); \ + \ + return result; \ } +#define ATOMIC_OPS (op, asm_op) \ + ATOMIC_OP(op, asm_op) \ + ATOMIC_OP_RETURN(op, asm_op) + +ATOMIC_OPS(add, add) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP +/* -* atomic_add_return - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v. Returns the resulting value. +* The 'sub' instruction is limited to 21 bits, use negate add instead. */ -static inline int atomic_add_return (int i,atomic_t *v) -{ -int result; - -if (__builtin_constant_p (i) && (i >=-1048575 ) && (i <=1048576 )) - result =atomic_sub_return (-i, v); -else - asmvolatile ( -"/* atomic_add_return */\n" -"1: ssrf 5\n" -" ld.w %0, %1\n" -" add %0, %3\n" -" stcond %2, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"r" (i) - :"cc" ,"memory" ); - -return result; -} +#define atomic_sub (i, v) atomic_add(-(i), (v)) +#define atomic_sub_return (i, v) atomic_add_return(-(i), (v))/* -* atomic_sub_unless - sub unless the number is a given value +* __atomic_add_unless - add unless the number is a given value * @v: pointer of type atomic_t -* @a: the amount to subtract from v... +* @a: the amount to add to v... * @u: ...unless v is equal to u. * -* Atomically subtract @a from @v, so long as it was not @u. +* Atomically adds @a to @v, so long as it was not @u. * Returns the old value of @v. */ -static inline void atomic_sub_unless (atomic_t *v,int a,int u) +static inline int __atomic_add_unless (atomic_t *v,int a,int u) { -int tmp; +int tmp, old =atomic_read (v); asmvolatile ( -"/* atomic_sub_unless */\n" +"/* __atomic_add_unless */\n" "1: ssrf 5\n" " ld.w %0, %2\n" " cp.w %0, %4\n" " breq 1f\n" -" sub %0, %3\n" +" add %0, %3\n" " stcond %1, %0\n" " brne 1b\n" "1:" :"=&r" (tmp),"=o" (v->counter) - :"m" (v->counter),"rKs21" (a),"rKs21" (u) + :"m" (v->counter),"r" (a),"ir" (u) :"cc" ,"memory" ); -} - -/* -* __atomic_add_unless - add unless the number is a given value -* @v: pointer of type atomic_t -* @a: the amount to add to v... -* @u: ...unless v is equal to u. -* -* Atomically adds @a to @v, so long as it was not @u. -* Returns the old value of @v. -*/ -static inline int __atomic_add_unless (atomic_t *v,int a,int u) -{ -int tmp, old =atomic_read (v); - -if (__builtin_constant_p (a) && (a >=-1048575 ) && (a <=1048576 )) -atomic_sub_unless (v, -a, u); -else { - asmvolatile ( -"/* __atomic_add_unless */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" -" cp.w %0, %4\n" -" breq 1f\n" -" add %0, %3\n" -" stcond %1, %0\n" -" brne 1b\n" -"1:" - :"=&r" (tmp),"=o" (v->counter) - :"m" (v->counter),"r" (a),"ir" (u) - :"cc" ,"memory" ); - }return old; }@@ -168,8 +135,6 @@ static inline int atomic_sub_if_positive#define atomic_xchg (v, new) (xchg(&((v)->counter), new))#define atomic_cmpxchg (v, o, n) (cmpxchg(&((v)->counter), (o), (n))) -#define atomic_sub (i, v) (void)atomic_sub_return(i, v) -#define atomic_add (i, v) (void)atomic_add_return(i, v)#define atomic_dec (v) atomic_sub(1, (v))#define atomic_inc (v) atomic_add(1, (v))
Signature · 2 lines
mvh Hans-Christian Egtvedt
Peter Zijlstra
maintainerRe: [PATCH 06/20] arch,avr32: Fold atomic_ops
Quoted 11 lines · Hans-Christian Egtvedt
Probably found the reason why we want to use sub with the signed 21-bit limit, it uses one less register than the add instruction that can add up to 32-bit values.
Both instructions are 32-bit, to use a 16-bit instruction the immediate is very small; 4 bit.
sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate. add 32-bit, type II, adds two register values together.
So by simplifying you loose this optimization.
OK, let me try if I can come up with anything sane for this.
Re, adding atomic_{or,and,xor}() those should all use the same bits as add, right, except for the special case using sub.
Re: [PATCH 06/20] arch,avr32: Fold atomic_ops
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Hans-Christian Egtvedt
Probably found the reason why we want to use sub with the signed 21-bit limit, it uses one less register than the add instruction that can add up to 32-bit values.
Both instructions are 32-bit, to use a 16-bit instruction the immediate is very small; 4 bit.
sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate. add 32-bit, type II, adds two register values together.
Quoted 6 lines
Quoted 1 line
So by simplifying you loose this optimization.
OK, let me try if I can come up with anything sane for this.
Re, adding atomic_{or,and,xor}() those should all use the same bits as add, right, except for the special case using sub.
OR and XOR (EOR in the manual) instruction will fall into type II or III, both 32-bit, and (x)ors two registers together, difference is which way they can do a shift on one of the registers.
So yes, identical as add.
Signature · 1 line
Hans-Christian Egtvedt
Re: [PATCH 14/20] arch,mn10300: Fold atomic_ops
In reply to Peter ZijlstraQuoted 1 line · Peter Zijlstra
+#define
ATOMIC_OPS(op)ATOMIC_OP(op)ATOMIC_OP_RETURN(op)
Can ATOMIC_OP() just be an alias for ATOMIC_OP_RETURN() where that is appropriate? I suspect several arches (MN10300 included) are going to return the value *anyway*.
David
Peter Zijlstra
maintainerRe: [PATCH 14/20] arch,mn10300: Fold atomic_ops
Quoted 7 lines · David Howells
Quoted 1 line · Peter Zijlstra
+#define
ATOMIC_OPS(op)ATOMIC_OP(op)ATOMIC_OP_RETURN(op)Can
ATOMIC_OP()just be an alias forATOMIC_OP_RETURN()where that is appropriate? I suspect several arches (MN10300 included) are going to return the value *anyway*.
I was going to introduce a few new atomic ops that will not have _return() equivalents. So relying on whatever code is generated by ATOMIC_OP_RETURN() is going to be painful.
Peter Zijlstra
maintainerRe: [PATCH 06/20] arch,avr32: Fold atomic_ops
In reply to Hans-Christian EgtvedtQuoted 9 lines · Hans-Christian Egtvedt
Probably found the reason why we want to use sub with the signed 21-bit limit, it uses one less register than the add instruction that can add up to 32-bit values.
Both instructions are 32-bit, to use a 16-bit instruction the immediate is very small; 4 bit.
sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate. add 32-bit, type II, adds two register values together.
How about something like so? It fixes the problem that your atomic_sub*() can only deal with a 21bit offset, while the general kernel interface assumes it can deal with the full 32 bits.
It also adds the above as a comment, hopefully explaining the 21bit magic to the next person stumbling over this.
--- arch/avr32/include/asm/atomic.h | 121 +++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 61 deletions(-)
arch/avr32/include/asm/atomic.h +60 -61
@@ -22,30 +22,43 @@#define atomic_read (v) (*(volatile int *)&(v)->counter)#define atomic_set (v, i) (((v)->counter) = i) +#define ATOMIC_OP_RETURN(op, asm_op, asm_con) \ +static inline int __atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int result; \ + \ + asm volatile( \ + "/* atomic_" #op "_return */\n" \ + "1: ssrf 5\n" \ + " ld.w %0, %2\n" \ + " " #asm_op " %0, %3\n" \ + " stcond %1, %0\n" \ + " brne 1b" \ + : "=&r" (result), "=o" (v->counter) \ + : "m" (v->counter), #asm_con (i) \ + : "cc"); \ + \ + return result; \ +} + +ATOMIC_OP_RETURN(sub, sub, rKs21) +ATOMIC_OP_RETURN(add, add, i) + +#undef ATOMIC_OP_RETURN +/* -* atomic_sub_return - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t +* Probably found the reason why we want to use sub with the signed 21-bit +* limit, it uses one less register than the add instruction that can add up to +* 32-bit values. * -* Atomically subtracts @i from @v. Returns the resulting value. +* Both instructions are 32-bit, to use a 16-bit instruction the immediate is +* very small; 4 bit. +* +* sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate. +* add 32-bit, type II, adds two register values together. */ -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int result; - - asmvolatile ( -"/* atomic_sub_return */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" -" sub %0, %3\n" -" stcond %1, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"rKs21" (i) - :"cc" ); - -return result; -} +#define IS_21BIT_CONST (i) \ + (__builtin_constant_p(i) && ((i) >= -1048575) && ((i) <= 1048576))/* * atomic_add_return - add integer to atomic variable
@@ -56,51 +69,25 @@ static inline int atomic_sub_return(int*/ static inline int atomic_add_return (int i,atomic_t *v) { -int result; +if (IS_21BIT_CONST (i)) +return __atomic_sub_return (-i, v); -if (__builtin_constant_p (i) && (i >=-1048575 ) && (i <=1048576 )) - result =atomic_sub_return (-i, v); -else - asmvolatile ( -"/* atomic_add_return */\n" -"1: ssrf 5\n" -" ld.w %0, %1\n" -" add %0, %3\n" -" stcond %2, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"r" (i) - :"cc" ,"memory" ); - -return result; +return __atomic_add_return (i, v); }/* -* atomic_sub_unless - sub unless the number is a given value +* atomic_sub_return - subtract the atomic variable +* @i: integer value to subtract * @v: pointer of type atomic_t -* @a: the amount to subtract from v... -* @u: ...unless v is equal to u. * -* Atomically subtract @a from @v, so long as it was not @u. -* Returns the old value of @v. -*/ -static inline void atomic_sub_unless (atomic_t *v,int a,int u) +* Atomically subtracts @i from @v. Returns the resulting value. +*/ +static inline int atomic_sub_return (int i,atomic_t *v) { -int tmp; +if (IS_21BIT_CONST (i)) +return __atomic_sub_return (i, v); - asmvolatile ( -"/* atomic_sub_unless */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" -" cp.w %0, %4\n" -" breq 1f\n" -" sub %0, %3\n" -" stcond %1, %0\n" -" brne 1b\n" -"1:" - :"=&r" (tmp),"=o" (v->counter) - :"m" (v->counter),"rKs21" (a),"rKs21" (u) - :"cc" ,"memory" ); +return __atomic_add_return (-i, v); }/*
@@ -116,9 +103,21 @@ static inline int __atomic_add_unless(at {int tmp, old =atomic_read (v); -if (__builtin_constant_p (a) && (a >=-1048575 ) && (a <=1048576 )) -atomic_sub_unless (v, -a, u); -else { +if (IS_21BIT_CONST (a)) { + asmvolatile ( +"/* __atomic_sub_unless */\n" +"1: ssrf 5\n" +" ld.w %0, %2\n" +" cp.w %0, %4\n" +" breq 1f\n" +" sub %0, %3\n" +" stcond %1, %0\n" +" brne 1b\n" +"1:" + :"=&r" (tmp),"=o" (v->counter) + :"m" (v->counter),"rKs21" (-a),"rKs21" (u) + :"cc" ,"memory" ); + }else { asmvolatile ("/* __atomic_add_unless */\n" "1: ssrf 5\n"
Re: [PATCH 06/20] arch,avr32: Fold atomic_ops
Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Hans-Christian Egtvedt
Probably found the reason why we want to use sub with the signed 21-bit limit, it uses one less register than the add instruction that can add up to 32-bit values.
Both instructions are 32-bit, to use a 16-bit instruction the immediate is very small; 4 bit.
sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate. add 32-bit, type II, adds two register values together.
Quoted 6 lines
How about something like so? It fixes the problem that your atomic_sub*() can only deal with a 21bit offset, while the general kernel interface assumes it can deal with the full 32 bits.
It also adds the above as a comment, hopefully explaining the 21bit magic to the next person stumbling over this.
Looks great, I only have one comment.
Quoted 58 lines
--- arch/avr32/include/asm/atomic.h | 121 +++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 61 deletions(-)arch/avr32/include/asm/atomic.h +32
-13@@ -22,30 +22,43 @@#define atomic_read (v) (*(volatile int *)&(v)->counter)#define atomic_set (v, i) (((v)->counter) = i) +#define ATOMIC_OP_RETURN(op, asm_op, asm_con) \ +static inline int __atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int result; \ + \ + asm volatile( \ + "/* atomic_" #op "_return */\n" \ + "1: ssrf 5\n" \ + " ld.w %0, %2\n" \ + " " #asm_op " %0, %3\n" \ + " stcond %1, %0\n" \ + " brne 1b" \ + : "=&r" (result), "=o" (v->counter) \ + : "m" (v->counter), #asm_con (i) \ + : "cc"); \ + \ + return result; \ +} + +ATOMIC_OP_RETURN(sub, sub, rKs21) +ATOMIC_OP_RETURN(add, add, i) + +#undef ATOMIC_OP_RETURN +/* -* atomic_sub_return - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t +* Probably found the reason why we want to use sub with the signed 21-bit +* limit, it uses one less register than the add instruction that can add up to +* 32-bit values. * -* Atomically subtracts @i from @v. Returns the resulting value. +* Both instructions are 32-bit, to use a 16-bit instruction the immediate is +* very small; 4 bit. +* +* sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate. +* add 32-bit, type II, adds two register values together. */ -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int result; - - asmvolatile ( -"/* atomic_sub_return */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" - " sub %0, %3\n"
arch/avr32/include/asm/atomic.h line 61
Quoted 10 lines
arch/avr32/include/asm/atomic.h +2
-8- " stcond %1, %0\n" - " brne 1b" - : "=&r"(result), "=o"(v->counter) - : "m"(v->counter), "rKs21"(i) - : "cc"); - -return result; -} +#define IS_21BIT_CONST (i) \ + (__builtin_constant_p(i) && ((i) >= -1048575) && ((i) <= 1048576))
Perhaps undef this macro once we're done using it?
Quoted 81 lines
arch/avr32/include/asm/atomic.h +19
-40/* * atomic_add_return - add integer to atomic variable @@ -56,51 +69,25 @@ static inline int atomic_sub_return(int*/ static inline int atomic_add_return (int i,atomic_t *v) { -int result; +if (IS_21BIT_CONST (i)) +return __atomic_sub_return (-i, v); -if (__builtin_constant_p (i) && (i >=-1048575 ) && (i <=1048576 )) - result =atomic_sub_return (-i, v); -else - asmvolatile ( -"/* atomic_add_return */\n" -"1: ssrf 5\n" -" ld.w %0, %1\n" -" add %0, %3\n" -" stcond %2, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"r" (i) - :"cc" ,"memory" ); - -return result; +return __atomic_add_return (i, v); }/* -* atomic_sub_unless - sub unless the number is a given value +* atomic_sub_return - subtract the atomic variable +* @i: integer value to subtract * @v: pointer of type atomic_t -* @a: the amount to subtract from v... -* @u: ...unless v is equal to u. * -* Atomically subtract @a from @v, so long as it was not @u. -* Returns the old value of @v. -*/ -static inline void atomic_sub_unless (atomic_t *v,int a,int u) +* Atomically subtracts @i from @v. Returns the resulting value. +*/ +static inline int atomic_sub_return (int i,atomic_t *v) { -int tmp; +if (IS_21BIT_CONST (i)) +return __atomic_sub_return (i, v); - asmvolatile ( -"/* atomic_sub_unless */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" -" cp.w %0, %4\n" -" breq 1f\n" -" sub %0, %3\n" -" stcond %1, %0\n" -" brne 1b\n" -"1:" - :"=&r" (tmp),"=o" (v->counter) - :"m" (v->counter),"rKs21" (a),"rKs21" (u) - :"cc" ,"memory" ); +return __atomic_add_return (-i, v); }/* @@ -116,9 +103,21 @@ static inline int __atomic_add_unless(at {int tmp, old =atomic_read (v); -if (__builtin_constant_p (a) && (a >=-1048575 ) && (a <=1048576 )) -atomic_sub_unless (v, -a, u); -else { +if (IS_21BIT_CONST (a)) { + asmvolatile ( +"/* __atomic_sub_unless */\n" +"1: ssrf 5\n" +" ld.w %0, %2\n" +" cp.w %0, %4\n" +" breq 1f\n" + " sub %0, %3\n"
arch/avr32/include/asm/atomic.h static inline int __atomic_add_unless(at · line 123
Quoted 10 lines
arch/avr32/include/asm/atomic.h +7
-0+ " stcond %1, %0\n" + " brne 1b\n" + "1:" + : "=&r"(tmp), "=o"(v->counter) + : "m"(v->counter), "rKs21"(-a), "rKs21"(u) + : "cc", "memory"); + }else { asmvolatile ("/* __atomic_add_unless */\n" "1: ssrf 5\n"
+#undef IS_21BIT_CONST
Signature · 1 line
Hans-Christian Egtvedt
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,x86: Kill atomic_or_long()
In reply to Peter Zijlstralocking,x86: Kill atomic_or_long()
Commit-ID: f6b4ecee0eb7bfa66ae8d5652105ed4da53209a3 Gitweb: http://git.kernel.org/tip/f6b4ecee0eb7bfa66ae8d5652105ed4da53209a3 Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 23 Apr 2014 17:02:18 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:02 +0200
locking,x86: Kill atomic_or_long()
There are no users, kill it.
--- arch/x86/include/asm/atomic.h | 15 --------------- 1 file changed, 15 deletions(-)
arch/x86/include/asm/atomic.h +0 -15
@@ -219,21 +219,6 @@ static inline short int atomic_inc_short(short int *v)return *v; } -#ifdef CONFIG_X86_64 -/** -* atomic_or_long - OR of two long integers -* @v1: pointer to type unsigned long -* @v2: pointer to type unsigned long -* -* Atomically ORs @v1 and @v2 -* Returns the result of the OR -*/ -static inline void atomic_or_long (unsigned long *v1,unsigned long v2) -{ - asm(LOCK_PREFIX "orq %1, %0" :"+m" (*v1) :"r" (v2)); -} -#endif -/* These are x86-specific, used by some header files */ #define atomic_clear_mask (mask, addr) \ asm volatile(LOCK_PREFIX "andl %0,%1" \
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,alpha: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,alpha: Fold atomic_ops
Commit-ID: b93c7b8c5b281bf3646d6c5b6e05249b98cc5ab7 Gitweb: http://git.kernel.org/tip/b93c7b8c5b281bf3646d6c5b6e05249b98cc5ab7 Author: Peter Zijlstra <[email protected]> AuthorDate: Sun, 23 Mar 2014 16:25:53 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:03 +0200
locking,arch,alpha: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/alpha/include/asm/atomic.h | 213 +++++++++++++++------------------------- 1 file changed, 80 insertions(+), 133 deletions(-)
arch/alpha/include/asm/atomic.h +80 -133
@@ -29,145 +29,92 @@* branch back to restart the operation. */ -static __inline__void atomic_add (int i,atomic_t * v) -{ -unsigned long temp; - __asm__ __volatile__( -"1: ldl_l %0,%1\n" -" addl %0,%2,%0\n" -" stl_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter)); -} - -static __inline__void atomic64_add (long i,atomic64_t * v) -{ -unsigned long temp; - __asm__ __volatile__( -"1: ldq_l %0,%1\n" -" addq %0,%2,%0\n" -" stq_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter)); -} - -static __inline__void atomic_sub (int i,atomic_t * v) -{ -unsigned long temp; - __asm__ __volatile__( -"1: ldl_l %0,%1\n" -" subl %0,%2,%0\n" -" stl_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter)); +#define ATOMIC_OP (op) \ +static __inline__ void atomic_##op(int i, atomic_t * v) \ +{ \ + unsigned long temp; \ + __asm__ __volatile__( \ + "1: ldl_l %0,%1\n" \ + " " #op "l %0,%2,%0\n" \ + " stl_c %0,%1\n" \ + " beq %0,2f\n" \ + ".subsection 2\n" \ + "2: br 1b\n" \ + ".previous" \ + :"=&r" (temp), "=m" (v->counter) \ + :"Ir" (i), "m" (v->counter)); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + long temp, result; \ + smp_mb(); \ + __asm__ __volatile__( \ + "1: ldl_l %0,%1\n" \ + " " #op "l %0,%3,%2\n" \ + " " #op "l %0,%3,%0\n" \ + " stl_c %0,%1\n" \ + " beq %0,2f\n" \ + ".subsection 2\n" \ + "2: br 1b\n" \ + ".previous" \ + :"=&r" (temp), "=m" (v->counter), "=&r" (result) \ + :"Ir" (i), "m" (v->counter) : "memory"); \ + smp_mb(); \ + return result; \ } -static __inline__void atomic64_sub (long i,atomic64_t * v) -{ -unsigned long temp; - __asm__ __volatile__( -"1: ldq_l %0,%1\n" -" subq %0,%2,%0\n" -" stq_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter)); -} - - -/* -* Same as above, but return the result value -*/ -static inline int atomic_add_return (int i,atomic_t *v) -{ -long temp, result; -smp_mb (); - __asm__ __volatile__( -"1: ldl_l %0,%1\n" -" addl %0,%3,%2\n" -" addl %0,%3,%0\n" -" stl_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter),"=&r" (result) - :"Ir" (i),"m" (v->counter) :"memory" ); -smp_mb (); -return result; +#define ATOMIC64_OP (op) \ +static __inline__ void atomic64_##op(long i, atomic64_t * v) \ +{ \ + unsigned long temp; \ + __asm__ __volatile__( \ + "1: ldq_l %0,%1\n" \ + " " #op "q %0,%2,%0\n" \ + " stq_c %0,%1\n" \ + " beq %0,2f\n" \ + ".subsection 2\n" \ + "2: br 1b\n" \ + ".previous" \ + :"=&r" (temp), "=m" (v->counter) \ + :"Ir" (i), "m" (v->counter)); \ +} \ + +#define ATOMIC64_OP_RETURN (op) \ +static __inline__ long atomic64_##op##_return(long i, atomic64_t * v) \ +{ \ + long temp, result; \ + smp_mb(); \ + __asm__ __volatile__( \ + "1: ldq_l %0,%1\n" \ + " " #op "q %0,%3,%2\n" \ + " " #op "q %0,%3,%0\n" \ + " stq_c %0,%1\n" \ + " beq %0,2f\n" \ + ".subsection 2\n" \ + "2: br 1b\n" \ + ".previous" \ + :"=&r" (temp), "=m" (v->counter), "=&r" (result) \ + :"Ir" (i), "m" (v->counter) : "memory"); \ + smp_mb(); \ + return result; \ } -static __inline__long atomic64_add_return (long i,atomic64_t * v) -{ -long temp, result; -smp_mb (); - __asm__ __volatile__( -"1: ldq_l %0,%1\n" -" addq %0,%3,%2\n" -" addq %0,%3,%0\n" -" stq_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter),"=&r" (result) - :"Ir" (i),"m" (v->counter) :"memory" ); -smp_mb (); -return result; -} +#define ATOMIC_OPS (opg) \ + ATOMIC_OP(opg) \ + ATOMIC_OP_RETURN(opg) \ + ATOMIC64_OP(opg) \ + ATOMIC64_OP_RETURN(opg) -static __inline__long atomic_sub_return (int i,atomic_t * v) -{ -long temp, result; -smp_mb (); - __asm__ __volatile__( -"1: ldl_l %0,%1\n" -" subl %0,%3,%2\n" -" subl %0,%3,%0\n" -" stl_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter),"=&r" (result) - :"Ir" (i),"m" (v->counter) :"memory" ); -smp_mb (); -return result; -} +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -static __inline__long atomic64_sub_return (long i,atomic64_t * v) -{ -long temp, result; -smp_mb (); - __asm__ __volatile__( -"1: ldq_l %0,%1\n" -" subq %0,%3,%2\n" -" subq %0,%3,%0\n" -" stq_c %0,%1\n" -" beq %0,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (temp),"=m" (v->counter),"=&r" (result) - :"Ir" (i),"m" (v->counter) :"memory" ); -smp_mb (); -return result; -} +#undef ATOMIC_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OP +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP#define atomic64_cmpxchg (v, old, new) (cmpxchg(&((v)->counter), old, new))#define atomic64_xchg (v, new) (xchg(&((v)->counter), new))
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,arc: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,arc: Fold atomic_ops
Commit-ID: f7d11e93ee97a37da1947b7c4e1794705a6f360c Gitweb: http://git.kernel.org/tip/f7d11e93ee97a37da1947b7c4e1794705a6f360c Author: Peter Zijlstra <[email protected]> AuthorDate: Sun, 23 Mar 2014 16:29:31 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:03 +0200
locking,arch,arc: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/arc/include/asm/atomic.h | 184 +++++++++++++++--------------------------- 1 file changed, 63 insertions(+), 121 deletions(-)
arch/arc/include/asm/atomic.h +63 -121
@@ -25,79 +25,36 @@#define atomic_set (v, i) (((v)->counter) = (i)) -static inline void atomic_add (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" add %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp)/* Early clobber, to prevent reg reuse */ - :"r" (&v->counter),"ir" (i) - :"cc" ); -} - -static inline void atomic_sub (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" sub %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (&v->counter),"ir" (i) - :"cc" ); -} - -/* add and also return the new value */ -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" add %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (&v->counter),"ir" (i) - :"cc" ); - -return temp; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" sub %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (&v->counter),"ir" (i) - :"cc" ); - -return temp; -} - -static inline void atomic_clear_mask (unsigned long mask,unsigned long *addr) -{ -unsigned int temp; - - __asm__ __volatile__( -"1: llock %0, [%1] \n" -" bic %0, %0, %2 \n" -" scond %0, [%1] \n" -" bnz 1b \n" - :"=&r" (temp) - :"r" (addr),"ir" (mask) - :"cc" ); +#define ATOMIC_OP(op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned int temp; \ + \ + __asm__ __volatile__( \ + "1: llock %0, [%1] \n" \ + " " #asm_op " %0, %0, %2 \n" \ + " scond %0, [%1] \n" \ + " bnz 1b \n" \ + : "=&r"(temp) /* Early clobber, to prevent reg reuse */ \ + : "r"(&v->counter), "ir"(i) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned int temp; \ + \ + __asm__ __volatile__( \ + "1: llock %0, [%1] \n" \ + " " #asm_op " %0, %0, %2 \n" \ + " scond %0, [%1] \n" \ + " bnz 1b \n" \ + : "=&r"(temp) \ + : "r"(&v->counter), "ir"(i) \ + : "cc"); \ + \ + return temp; \ }#else /* !CONFIG_ARC_HAS_LLSC */
@@ -126,6 +83,7 @@ static inline void atomic_set(atomic_t *v, int i) v->counter = i; atomic_ops_unlock(flags); } +#endif /*
@@ -133,62 +91,46 @@ static inline void atomic_set(atomic_t *v, int i)* Locking would change to irq-disabling only (UP) and spinlocks (SMP) */ -static inline void atomic_add (int i,atomic_t *v) -{ -unsigned long flags; - -atomic_ops_lock (flags); - v->counter += i; -atomic_ops_unlock (flags); +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + \ + atomic_ops_lock(flags); \ + v->counter c_op i; \ + atomic_ops_unlock(flags); \ } -static inline void atomic_sub (int i,atomic_t *v) -{ -unsigned long flags; - -atomic_ops_lock (flags); - v->counter -= i; -atomic_ops_unlock (flags); +#define ATOMIC_OP_RETURN (op, c_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + unsigned long temp; \ + \ + atomic_ops_lock(flags); \ + temp = v->counter; \ + temp c_op i; \ + v->counter = temp; \ + atomic_ops_unlock(flags); \ + \ + return temp; \ } -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long flags; -unsigned long temp; - -atomic_ops_lock (flags); - temp = v->counter; - temp += i; - v->counter = temp; -atomic_ops_unlock (flags); - -return temp; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long flags; -unsigned long temp; - -atomic_ops_lock (flags); - temp = v->counter; - temp -= i; - v->counter = temp; -atomic_ops_unlock (flags); +#endif /* !CONFIG_ARC_HAS_LLSC */ -return temp; -} +#define ATOMIC_OPS (op, c_op, asm_op) \ + ATOMIC_OP(op, c_op, asm_op) \ + ATOMIC_OP_RETURN(op, c_op, asm_op) -static inline void atomic_clear_mask (unsigned long mask,unsigned long *addr) -{ -unsigned long flags; +ATOMIC_OPS(add, +=, add) +ATOMIC_OPS(sub, -=, sub) +ATOMIC_OP(and, &=, and) -atomic_ops_lock (flags); - *addr &= ~mask; -atomic_ops_unlock (flags); -} +#define atomic_clear_mask (mask, v) atomic_and(~(mask), (v)) -#endif /* !CONFIG_ARC_HAS_LLSC */ +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP/** * __atomic_add_unless - add unless the number is a given value
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,avr32: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,avr32: Fold atomic_ops
Commit-ID: d325209b6000dcd13404ee946d2292e15a56718c Gitweb: http://git.kernel.org/tip/d325209b6000dcd13404ee946d2292e15a56718c Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 9 Apr 2014 21:51:29 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:05 +0200
locking,arch,avr32: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires the asm_op because of eor.
AVR32 is a bit special in that its ADD/SUB instructions are not symmetric. Its SUB instruction allows for an 21bit immediate.
--- arch/avr32/include/asm/atomic.h | 123 ++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 61 deletions(-)
arch/avr32/include/asm/atomic.h +62 -61
@@ -22,30 +22,43 @@#define atomic_read (v) (*(volatile int *)&(v)->counter)#define atomic_set (v, i) (((v)->counter) = i) +#define ATOMIC_OP_RETURN(op, asm_op, asm_con) \ +static inline int __atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int result; \ + \ + asm volatile( \ + "/* atomic_" #op "_return */\n" \ + "1: ssrf 5\n" \ + " ld.w %0, %2\n" \ + " " #asm_op " %0, %3\n" \ + " stcond %1, %0\n" \ + " brne 1b" \ + : "=&r" (result), "=o" (v->counter) \ + : "m" (v->counter), #asm_con (i) \ + : "cc"); \ + \ + return result; \ +} + +ATOMIC_OP_RETURN(sub, sub, rKs21) +ATOMIC_OP_RETURN(add, add, r) + +#undef ATOMIC_OP_RETURN +/* -* atomic_sub_return - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t +* Probably found the reason why we want to use sub with the signed 21-bit +* limit, it uses one less register than the add instruction that can add up to +* 32-bit values. * -* Atomically subtracts @i from @v. Returns the resulting value. +* Both instructions are 32-bit, to use a 16-bit instruction the immediate is +* very small; 4 bit. +* +* sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate. +* add 32-bit, type II, adds two register values together. */ -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int result; - - asmvolatile ( -"/* atomic_sub_return */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" -" sub %0, %3\n" -" stcond %1, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"rKs21" (i) - :"cc" ); - -return result; -} +#define IS_21BIT_CONST (i) \ + (__builtin_constant_p(i) && ((i) >= -1048575) && ((i) <= 1048576))/* * atomic_add_return - add integer to atomic variable
@@ -56,51 +69,25 @@ static inline int atomic_sub_return(int i, atomic_t *v)*/ static inline int atomic_add_return (int i,atomic_t *v) { -int result; - -if (__builtin_constant_p (i) && (i >=-1048575 ) && (i <=1048576 )) - result =atomic_sub_return (-i, v); -else - asmvolatile ( -"/* atomic_add_return */\n" -"1: ssrf 5\n" -" ld.w %0, %1\n" -" add %0, %3\n" -" stcond %2, %0\n" -" brne 1b" - :"=&r" (result),"=o" (v->counter) - :"m" (v->counter),"r" (i) - :"cc" ,"memory" ); +if (IS_21BIT_CONST (i)) +return __atomic_sub_return (-i, v); -return result; +return __atomic_add_return (i, v); }/* -* atomic_sub_unless - sub unless the number is a given value +* atomic_sub_return - subtract the atomic variable +* @i: integer value to subtract * @v: pointer of type atomic_t -* @a: the amount to subtract from v... -* @u: ...unless v is equal to u. * -* Atomically subtract @a from @v, so long as it was not @u. -* Returns the old value of @v. -*/ -static inline void atomic_sub_unless (atomic_t *v,int a,int u) +* Atomically subtracts @i from @v. Returns the resulting value. +*/ +static inline int atomic_sub_return (int i,atomic_t *v) { -int tmp; +if (IS_21BIT_CONST (i)) +return __atomic_sub_return (i, v); - asmvolatile ( -"/* atomic_sub_unless */\n" -"1: ssrf 5\n" -" ld.w %0, %2\n" -" cp.w %0, %4\n" -" breq 1f\n" -" sub %0, %3\n" -" stcond %1, %0\n" -" brne 1b\n" -"1:" - :"=&r" (tmp),"=o" (v->counter) - :"m" (v->counter),"rKs21" (a),"rKs21" (u) - :"cc" ,"memory" ); +return __atomic_add_return (-i, v); }/*
@@ -116,9 +103,21 @@ static inline int __atomic_add_unless(atomic_t *v, int a, int u) {int tmp, old =atomic_read (v); -if (__builtin_constant_p (a) && (a >=-1048575 ) && (a <=1048576 )) -atomic_sub_unless (v, -a, u); -else { +if (IS_21BIT_CONST (a)) { + asmvolatile ( +"/* __atomic_sub_unless */\n" +"1: ssrf 5\n" +" ld.w %0, %2\n" +" cp.w %0, %4\n" +" breq 1f\n" +" sub %0, %3\n" +" stcond %1, %0\n" +" brne 1b\n" +"1:" + :"=&r" (tmp),"=o" (v->counter) + :"m" (v->counter),"rKs21" (-a),"rKs21" (u) + :"cc" ,"memory" ); + }else { asmvolatile ("/* __atomic_add_unless */\n" "1: ssrf 5\n"
@@ -137,6 +136,8 @@ static inline int __atomic_add_unless(atomic_t *v, int a, int u)return old; } +#undef IS_21BIT_CONST +/* * atomic_sub_if_positive - conditionally subtract integer from atomic variable * @i: integer value to subtract
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,hexagon: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,hexagon: Fold atomic_ops
Commit-ID: 50f853e38b0b90a5703ab14b70e20eb5a8ccd5de Gitweb: http://git.kernel.org/tip/50f853e38b0b90a5703ab14b70e20eb5a8ccd5de Author: Peter Zijlstra <[email protected]> AuthorDate: Sun, 23 Mar 2014 18:20:26 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:06 +0200
locking,arch,hexagon: Fold atomic_ops
OK, no LoC saved in this case because the !return variants were defined in terms of the return ops. Still do it because this also prepares for easy addition of new ops.
--- arch/hexagon/include/asm/atomic.h | 68 +++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 31 deletions(-)
arch/hexagon/include/asm/atomic.h +37 -31
@@ -94,41 +94,47 @@ static inline int atomic_cmpxchg(atomic_t *v, int old, int new)return __oldval; } -static inline int atomic_add_return (int i,atomic_t *v) -{ -int output; - - __asm__ __volatile__ ( -"1: %0 = memw_locked(%1);\n" -" %0 = add(%0,%2);\n" -" memw_locked(%1,P3)=%0;\n" -" if !P3 jump 1b;\n" - :"=&r" (output) - :"r" (&v->counter),"r" (i) - :"memory" ,"p3" - ); -return output; - +#define ATOMIC_OP (op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int output; \ + \ + __asm__ __volatile__ ( \ + "1: %0 = memw_locked(%1);\n" \ + " %0 = "#op "(%0,%2);\n" \ + " memw_locked(%1,P3)=%0;\n" \ + " if !P3 jump 1b;\n" \ + : "=&r" (output) \ + : "r" (&v->counter), "r" (i) \ + : "memory", "p3" \ + ); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int output; \ + \ + __asm__ __volatile__ ( \ + "1: %0 = memw_locked(%1);\n" \ + " %0 = "#op "(%0,%2);\n" \ + " memw_locked(%1,P3)=%0;\n" \ + " if !P3 jump 1b;\n" \ + : "=&r" (output) \ + : "r" (&v->counter), "r" (i) \ + : "memory", "p3" \ + ); \ + return output; \ } -#define atomic_add (i, v) atomic_add_return(i, (v)) +#define ATOMIC_OPS (op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int output; - __asm__ __volatile__ ( -"1: %0 = memw_locked(%1);\n" -" %0 = sub(%0,%2);\n" -" memw_locked(%1,P3)=%0\n" -" if !P3 jump 1b;\n" - :"=&r" (output) - :"r" (&v->counter),"r" (i) - :"memory" ,"p3" - ); -return output; -} +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -#define atomic_sub (i, v) atomic_sub_return(i, (v)) +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP/** * __atomic_add_unless - add unless the number is a given value
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,cris: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,cris: Fold atomic_ops
Commit-ID: 7179e30ef66a5bae91592ae7fbacf3df6c627dd6 Gitweb: http://git.kernel.org/tip/7179e30ef66a5bae91592ae7fbacf3df6c627dd6 Author: Peter Zijlstra <[email protected]> AuthorDate: Sun, 23 Mar 2014 18:19:25 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:06 +0200
locking,arch,cris: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/cris/include/asm/atomic.h | 57 ++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 32 deletions(-)
arch/cris/include/asm/atomic.h +25 -32
@@ -22,43 +22,36 @@/* These should be written in asm but we do it in C for now. */ -static inline void atomic_add (int i,volatile atomic_t *v) -{ -unsigned long flags; -cris_atomic_save (v, flags); - v->counter += i; -cris_atomic_restore (v, flags); +#define ATOMIC_OP (op, c_op) \ +static inline void atomic_##op(int i, volatile atomic_t *v) \ +{ \ + unsigned long flags; \ + cris_atomic_save(v, flags); \ + v->counter c_op i; \ + cris_atomic_restore(v, flags); \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op) \ +static inline int atomic_##op##_return(int i, volatile atomic_t *v) \ +{ \ + unsigned long flags; \ + int retval; \ + cris_atomic_save(v, flags); \ + retval = (v->counter c_op i); \ + cris_atomic_restore(v, flags); \ + return retval; \ } -static inline void atomic_sub (int i,volatile atomic_t *v) -{ -unsigned long flags; -cris_atomic_save (v, flags); - v->counter -= i; -cris_atomic_restore (v, flags); -} +#define ATOMIC_OPS (op, c_op) ATOMIC_OP(op, c_op) ATOMIC_OP_RETURN(op, c_op) -static inline int atomic_add_return (int i,volatile atomic_t *v) -{ -unsigned long flags; -int retval; -cris_atomic_save (v, flags); - retval = (v->counter += i); -cris_atomic_restore (v, flags); -return retval; -} +ATOMIC_OPS(add, +=) +ATOMIC_OPS(sub, -=) -#define atomic_add_negative (a, v) (atomic_add_return((a), (v)) < 0) +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP -static inline int atomic_sub_return (int i,volatile atomic_t *v) -{ -unsigned long flags; -int retval; -cris_atomic_save (v, flags); - retval = (v->counter -= i); -cris_atomic_restore (v, flags); -return retval; -} +#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0) static inline int atomic_sub_and_test(int i, volatile atomic_t *v) {
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,arm: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,arm: Fold atomic_ops
Commit-ID: aee9a55452f0371258e18b41649ce650ff344090 Gitweb: http://git.kernel.org/tip/aee9a55452f0371258e18b41649ce650ff344090 Author: Peter Zijlstra <[email protected]> AuthorDate: Sun, 23 Mar 2014 16:38:18 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:04 +0200
locking,arch,arm: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires the asm_op because of eor.
--- arch/arm/include/asm/atomic.h | 305 +++++++++++++++++------------------------- 1 file changed, 123 insertions(+), 182 deletions(-)
arch/arm/include/asm/atomic.h +123 -182
@@ -37,84 +37,47 @@* store exclusive to ensure that these are atomic. We may loop * to ensure that the update happens. */ -static inline void atomic_add (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - -prefetchw (&v->counter); - __asm__ __volatile__("@ atomic_add\n" -"1: ldrex %0, [%3]\n" -" add %0, %0, %4\n" -" strex %1, %0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"Ir" (i) - :"cc" ); -} -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - -smp_mb (); -prefetchw (&v->counter); - - __asm__ __volatile__("@ atomic_add_return\n" -"1: ldrex %0, [%3]\n" -" add %0, %0, %4\n" -" strex %1, %0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"Ir" (i) - :"cc" ); - -smp_mb (); - -return result; -} - -static inline void atomic_sub (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - -prefetchw (&v->counter); - __asm__ __volatile__("@ atomic_sub\n" -"1: ldrex %0, [%3]\n" -" sub %0, %0, %4\n" -" strex %1, %0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"Ir" (i) - :"cc" ); -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - -smp_mb (); -prefetchw (&v->counter); - - __asm__ __volatile__("@ atomic_sub_return\n" -"1: ldrex %0, [%3]\n" -" sub %0, %0, %4\n" -" strex %1, %0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"Ir" (i) - :"cc" ); - -smp_mb (); - -return result; +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + prefetchw(&v->counter); \ + __asm__ __volatile__("@ atomic_" #op "\n" \ +"1: ldrex %0, [%3]\n" \ +" " #asm_op " %0, %0, %4\n" \ +" strex %1, %0, [%3]\n" \ +" teq %1, #0\n" \ +" bne 1b" \ + : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ + : "r" (&v->counter), "Ir" (i) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + smp_mb(); \ + prefetchw(&v->counter); \ + \ + __asm__ __volatile__("@ atomic_" #op "_return\n" \ +"1: ldrex %0, [%3]\n" \ +" " #asm_op " %0, %0, %4\n" \ +" strex %1, %0, [%3]\n" \ +" teq %1, #0\n" \ +" bne 1b" \ + : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ + : "r" (&v->counter), "Ir" (i) \ + : "cc"); \ + \ + smp_mb(); \ + \ + return result; \ }static inline int atomic_cmpxchg (atomic_t *ptr,int old,int new)
@@ -174,33 +137,29 @@ static inline int __atomic_add_unless(atomic_t *v, int a, int u)#error SMP not supported on pre-ARMv6 CPUs#endif -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long flags; -int val; - -raw_local_irq_save (flags); - val = v->counter; - v->counter = val += i; -raw_local_irq_restore (flags); - -return val; -} -#define atomic_add (i, v) (void) atomic_add_return(i, v) - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long flags; -int val; - -raw_local_irq_save (flags); - val = v->counter; - v->counter = val -= i; -raw_local_irq_restore (flags); - -return val; +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + v->counter c_op i; \ + raw_local_irq_restore(flags); \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + int val; \ + \ + raw_local_irq_save(flags); \ + v->counter c_op i; \ + val = v->counter; \ + raw_local_irq_restore(flags); \ + \ + return val; \ } -#define atomic_sub (i, v) (void) atomic_sub_return(i, v)static inline int atomic_cmpxchg (atomic_t *v,int old,int new) {
@@ -228,6 +187,17 @@ static inline int __atomic_add_unless(atomic_t *v, int a, int u)#endif /* __LINUX_ARM_ARCH__ */ +#define ATOMIC_OPS (op, c_op, asm_op) \ + ATOMIC_OP(op, c_op, asm_op) \ + ATOMIC_OP_RETURN(op, c_op, asm_op) + +ATOMIC_OPS(add, +=, add) +ATOMIC_OPS(sub, -=, sub) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP +#define atomic_xchg (v, new) (xchg(&((v)->counter), new))#define atomic_inc (v) atomic_add(1, v)
@@ -300,89 +270,60 @@ static inline void atomic64_set(atomic64_t *v, long long i) }#endif -static inline void atomic64_add (long long i,atomic64_t *v) -{ -long long result; -unsigned long tmp; - -prefetchw (&v->counter); - __asm__ __volatile__("@ atomic64_add\n" -"1: ldrexd %0, %H0, [%3]\n" -" adds %Q0, %Q0, %Q4\n" -" adc %R0, %R0, %R4\n" -" strexd %1, %0, %H0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"r" (i) - :"cc" ); -} - -static inline long long atomic64_add_return (long long i,atomic64_t *v) -{ -long long result; -unsigned long tmp; - -smp_mb (); -prefetchw (&v->counter); - - __asm__ __volatile__("@ atomic64_add_return\n" -"1: ldrexd %0, %H0, [%3]\n" -" adds %Q0, %Q0, %Q4\n" -" adc %R0, %R0, %R4\n" -" strexd %1, %0, %H0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"r" (i) - :"cc" ); - -smp_mb (); - -return result; -} - -static inline void atomic64_sub (long long i,atomic64_t *v) -{ -long long result; -unsigned long tmp; - -prefetchw (&v->counter); - __asm__ __volatile__("@ atomic64_sub\n" -"1: ldrexd %0, %H0, [%3]\n" -" subs %Q0, %Q0, %Q4\n" -" sbc %R0, %R0, %R4\n" -" strexd %1, %0, %H0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"r" (i) - :"cc" ); +#define ATOMIC64_OP (op, op1, op2) \ +static inline void atomic64_##op(long long i, atomic64_t *v) \ +{ \ + long long result; \ + unsigned long tmp; \ + \ + prefetchw(&v->counter); \ + __asm__ __volatile__("@ atomic64_" #op "\n" \ +"1: ldrexd %0, %H0, [%3]\n" \ +" " #op1 " %Q0, %Q0, %Q4\n" \ +" " #op2 " %R0, %R0, %R4\n" \ +" strexd %1, %0, %H0, [%3]\n" \ +" teq %1, #0\n" \ +" bne 1b" \ + : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ + : "r" (&v->counter), "r" (i) \ + : "cc"); \ +} \ + +#define ATOMIC64_OP_RETURN (op, op1, op2) \ +static inline long long atomic64_##op##_return(long long i, atomic64_t *v) \ +{ \ + long long result; \ + unsigned long tmp; \ + \ + smp_mb(); \ + prefetchw(&v->counter); \ + \ + __asm__ __volatile__("@ atomic64_" #op "_return\n" \ +"1: ldrexd %0, %H0, [%3]\n" \ +" " #op1 " %Q0, %Q0, %Q4\n" \ +" " #op2 " %R0, %R0, %R4\n" \ +" strexd %1, %0, %H0, [%3]\n" \ +" teq %1, #0\n" \ +" bne 1b" \ + : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ + : "r" (&v->counter), "r" (i) \ + : "cc"); \ + \ + smp_mb(); \ + \ + return result; \ } -static inline long long atomic64_sub_return (long long i,atomic64_t *v) -{ -long long result; -unsigned long tmp; - -smp_mb (); -prefetchw (&v->counter); - - __asm__ __volatile__("@ atomic64_sub_return\n" -"1: ldrexd %0, %H0, [%3]\n" -" subs %Q0, %Q0, %Q4\n" -" sbc %R0, %R0, %R4\n" -" strexd %1, %0, %H0, [%3]\n" -" teq %1, #0\n" -" bne 1b" - :"=&r" (result),"=&r" (tmp),"+Qo" (v->counter) - :"r" (&v->counter),"r" (i) - :"cc" ); +#define ATOMIC64_OPS (op, op1, op2) \ + ATOMIC64_OP(op, op1, op2) \ + ATOMIC64_OP_RETURN(op, op1, op2) -smp_mb (); +ATOMIC64_OPS(add, adds, adc) +ATOMIC64_OPS(sub, subs, sbc) -return result; -} +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OPstatic inline long long atomic64_cmpxchg (atomic64_t *ptr,long long old,long long new)
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,m32r: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,m32r: Fold atomic_ops
Commit-ID: c9ebe21b204f95e3aba84ee91c8b9347d73806f1 Gitweb: http://git.kernel.org/tip/c9ebe21b204f95e3aba84ee91c8b9347d73806f1 Author: Peter Zijlstra <[email protected]> AuthorDate: Sun, 23 Mar 2014 19:02:22 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:08 +0200
locking,arch,m32r: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/m32r/include/asm/atomic.h | 143 ++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 86 deletions(-)
arch/m32r/include/asm/atomic.h +57 -86
@@ -39,85 +39,64 @@*/ #define atomic_set (v,i) (((v)->counter) = (i)) -/** -* atomic_add_return - add integer to atomic variable and return it -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v and return (@i + @v). -*/ -static __inline__int atomic_add_return (int i,atomic_t *v) -{ -unsigned long flags; -int result; - -local_irq_save (flags); - __asm__ __volatile__ ( -"# atomic_add_return \n\t" - DCACHE_CLEAR("%0", "r4", "%1") -M32R_LOCK " %0, @%1; \n\t" -"add %0, %2; \n\t" -M32R_UNLOCK " %0, @%1; \n\t" - : "=&r" (result) - : "r" (&v->counter), "r" (i) - : "memory"#ifdef CONFIG_CHIP_M32700_TS1 - ,"r4" -#endif /* CONFIG_CHIP_M32700_TS1 */ - ); -local_irq_restore (flags); - -return result; +#define __ATOMIC_CLOBBER , "r4" +#else +#define __ATOMIC_CLOBBER +#endif + +#define ATOMIC_OP (op) \ +static __inline__ void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + int result; \ + \ + local_irq_save(flags); \ + __asm__ __volatile__ ( \ + "# atomic_" #op " \n\t" \ + DCACHE_CLEAR("%0", "r4", "%1") \ + M32R_LOCK" %0, @%1; \n\t" \ + #op " %0, %2; \n\t" \ + M32R_UNLOCK" %0, @%1; \n\t" \ + : "=&r" (result) \ + : "r" (&v->counter), "r" (i) \ + : "memory" \ + __ATOMIC_CLOBBER \ + ); \ + local_irq_restore(flags); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static __inline__ int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + int result; \ + \ + local_irq_save(flags); \ + __asm__ __volatile__ ( \ + "# atomic_" #op "_return \n\t" \ + DCACHE_CLEAR("%0", "r4", "%1") \ + M32R_LOCK" %0, @%1; \n\t" \ + #op " %0, %2; \n\t" \ + M32R_UNLOCK" %0, @%1; \n\t" \ + : "=&r" (result) \ + : "r" (&v->counter), "r" (i) \ + : "memory" \ + __ATOMIC_CLOBBER \ + ); \ + local_irq_restore(flags); \ + \ + return result; \ } -/** -* atomic_sub_return - subtract integer from atomic variable and return it -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v and return (@v - @i). -*/ -static __inline__int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long flags; -int result; - -local_irq_save (flags); - __asm__ __volatile__ ( -"# atomic_sub_return \n\t" - DCACHE_CLEAR("%0", "r4", "%1") -M32R_LOCK " %0, @%1; \n\t" -"sub %0, %2; \n\t" -M32R_UNLOCK " %0, @%1; \n\t" - : "=&r" (result) - : "r" (&v->counter), "r" (i) - : "memory" -#ifdef CONFIG_CHIP_M32700_TS1 - ,"r4" -#endif /* CONFIG_CHIP_M32700_TS1 */ - ); -local_irq_restore (flags); - -return result; -} +#define ATOMIC_OPS (op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) -/** -* atomic_add - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v. -*/ -#define atomic_add (i,v) ((void) atomic_add_return((i), (v))) +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -/** -* atomic_sub - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v. -*/ -#define atomic_sub (i,v) ((void) atomic_sub_return((i), (v))) +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP/** * atomic_sub_and_test - subtract value from variable and test result
@@ -151,9 +130,7 @@ static __inline__ int atomic_inc_return(atomic_t *v) : "=&r" (result) : "r" (&v->counter) : "memory" -#ifdef CONFIG_CHIP_M32700_TS1 - , "r4" -#endif /* CONFIG_CHIP_M32700_TS1 */ + __ATOMIC_CLOBBER );local_irq_restore (flags);
@@ -181,9 +158,7 @@ static __inline__ int atomic_dec_return(atomic_t *v) : "=&r" (result) : "r" (&v->counter) : "memory" -#ifdef CONFIG_CHIP_M32700_TS1 - , "r4" -#endif /* CONFIG_CHIP_M32700_TS1 */ + __ATOMIC_CLOBBER );local_irq_restore (flags);
@@ -280,9 +255,7 @@ static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t *addr) : "=&r" (tmp) : "r" (addr), "r" (~mask) : "memory" -#ifdef CONFIG_CHIP_M32700_TS1 - ,"r5" -#endif /* CONFIG_CHIP_M32700_TS1 */ + __ATOMIC_CLOBBER );local_irq_restore (flags); }
@@ -302,9 +275,7 @@ static __inline__ void atomic_set_mask(unsigned long mask, atomic_t *addr) : "=&r" (tmp) : "r" (addr), "r" (mask) : "memory" -#ifdef CONFIG_CHIP_M32700_TS1 - ,"r5" -#endif /* CONFIG_CHIP_M32700_TS1 */ + __ATOMIC_CLOBBER );local_irq_restore (flags); }
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,m68k: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,m68k: Fold atomic_ops
Commit-ID: d839bae4269aea46bff4133066a411cfba5c7c46 Gitweb: http://git.kernel.org/tip/d839bae4269aea46bff4133066a411cfba5c7c46 Author: Peter Zijlstra <[email protected]> AuthorDate: Sun, 23 Mar 2014 19:06:34 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:08 +0200
locking,arch,m68k: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires asm_op due to eor.
--- arch/m68k/include/asm/atomic.h | 109 ++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 62 deletions(-)
arch/m68k/include/asm/atomic.h +47 -62
@@ -30,16 +30,57 @@#define ASM_DI "di"#endif -static inline void atomic_add (int i,atomic_t *v) -{ - __asm__ __volatile__("addl %1,%0" : "+m" (*v) : ASM_DI (i)); +#define ATOMIC_OP (op, c_op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + __asm__ __volatile__(#asm_op "l %1,%0" : "+m" (*v) : ASM_DI (i));\ +} \ + +#ifdef CONFIG_RMW_INSNS + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int t, tmp; \ + \ + __asm__ __volatile__( \ + "1: movel %2,%1\n" \ + " " #asm_op "l %3,%1\n" \ + " casl %2,%1,%0\n" \ + " jne 1b" \ + : "+m" (*v), "=&d" (t), "=&d" (tmp) \ + : "g" (i), "2" (atomic_read(v))); \ + return t; \ } -static inline void atomic_sub (int i,atomic_t *v) -{ - __asm__ __volatile__("subl %1,%0" : "+m" (*v) : ASM_DI (i)); +#else + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t * v) \ +{ \ + unsigned long flags; \ + int t; \ + \ + local_irq_save(flags); \ + t = (v->counter c_op i); \ + local_irq_restore(flags); \ + \ + return t; \ } +#endif /* CONFIG_RMW_INSNS */ + +#define ATOMIC_OPS (op, c_op, asm_op) \ + ATOMIC_OP(op, c_op, asm_op) \ + ATOMIC_OP_RETURN(op, c_op, asm_op) + +ATOMIC_OPS(add, +=, add) +ATOMIC_OPS(sub, -=, sub) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP +static inline void atomic_inc (atomic_t *v) { __asm__ __volatile__("addql #1,%0" :"+m" (*v));
@@ -76,67 +117,11 @@ static inline int atomic_inc_and_test(atomic_t *v)#ifdef CONFIG_RMW_INSNS -static inline int atomic_add_return (int i,atomic_t *v) -{ -int t, tmp; - - __asm__ __volatile__( -"1: movel %2,%1\n" -" addl %3,%1\n" -" casl %2,%1,%0\n" -" jne 1b" - :"+m" (*v),"=&d" (t),"=&d" (tmp) - :"g" (i),"2" (atomic_read (v))); -return t; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int t, tmp; - - __asm__ __volatile__( -"1: movel %2,%1\n" -" subl %3,%1\n" -" casl %2,%1,%0\n" -" jne 1b" - :"+m" (*v),"=&d" (t),"=&d" (tmp) - :"g" (i),"2" (atomic_read (v))); -return t; -} -#define atomic_cmpxchg (v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))#define atomic_xchg (v, new) (xchg(&((v)->counter), new))#else /* !CONFIG_RMW_INSNS */ -static inline int atomic_add_return (int i,atomic_t * v) -{ -unsigned long flags; -int t; - -local_irq_save (flags); - t =atomic_read (v); - t += i; -atomic_set (v, t); -local_irq_restore (flags); - -return t; -} - -static inline int atomic_sub_return (int i,atomic_t * v) -{ -unsigned long flags; -int t; - -local_irq_save (flags); - t =atomic_read (v); - t -= i; -atomic_set (v, t); -local_irq_restore (flags); - -return t; -} -static inline int atomic_cmpxchg (atomic_t *v,int old,int new) {unsigned long flags;
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,metag: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,metag: Fold atomic_ops
Commit-ID: d6dfe2509da935a15583cace7cd3837b1e8addef Gitweb: http://git.kernel.org/tip/d6dfe2509da935a15583cace7cd3837b1e8addef Author: Peter Zijlstra <[email protected]> AuthorDate: Sun, 23 Mar 2014 19:08:25 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:09 +0200
locking,arch,metag: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/metag/include/asm/atomic_lnkget.h | 121 +++++++++++++-------------------- arch/metag/include/asm/atomic_lock1.h | 76 +++++++++------------ 2 files changed, 77 insertions(+), 120 deletions(-)
arch/metag/include/asm/atomic_lnkget.h +46 -75
@@ -27,85 +27,56 @@ static inline int atomic_read(const atomic_t *v)return temp; } -static inline void atomic_add (int i,atomic_t *v) -{ -int temp; - - asmvolatile ( -"1: LNKGETD %0, [%1]\n" -" ADD %0, %0, %2\n" -" LNKSETD [%1], %0\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp) - :"da" (&v->counter),"bd" (i) - :"cc" ); +#define ATOMIC_OP (op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int temp; \ + \ + asm volatile ( \ + "1: LNKGETD %0, [%1]\n" \ + " " #op " %0, %0, %2\n" \ + " LNKSETD [%1], %0\n" \ + " DEFR %0, TXSTAT\n" \ + " ANDT %0, %0, #HI(0x3f000000)\n" \ + " CMPT %0, #HI(0x02000000)\n" \ + " BNZ 1b\n" \ + : "=&d" (temp) \ + : "da" (&v->counter), "bd" (i) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int result, temp; \ + \ + smp_mb(); \ + \ + asm volatile ( \ + "1: LNKGETD %1, [%2]\n" \ + " " #op " %1, %1, %3\n" \ + " LNKSETD [%2], %1\n" \ + " DEFR %0, TXSTAT\n" \ + " ANDT %0, %0, #HI(0x3f000000)\n" \ + " CMPT %0, #HI(0x02000000)\n" \ + " BNZ 1b\n" \ + : "=&d" (temp), "=&da" (result) \ + : "da" (&v->counter), "bd" (i) \ + : "cc"); \ + \ + smp_mb(); \ + \ + return result; \ } -static inline void atomic_sub (int i,atomic_t *v) -{ -int temp; +#define ATOMIC_OPS (op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) - asmvolatile ( -"1: LNKGETD %0, [%1]\n" -" SUB %0, %0, %2\n" -" LNKSETD [%1], %0\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp) - :"da" (&v->counter),"bd" (i) - :"cc" ); -} +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -static inline int atomic_add_return (int i,atomic_t *v) -{ -int result, temp; - -smp_mb (); - - asmvolatile ( -"1: LNKGETD %1, [%2]\n" -" ADD %1, %1, %3\n" -" LNKSETD [%2], %1\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp),"=&da" (result) - :"da" (&v->counter),"bd" (i) - :"cc" ); - -smp_mb (); - -return result; -} - -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int result, temp; - -smp_mb (); - - asmvolatile ( -"1: LNKGETD %1, [%2]\n" -" SUB %1, %1, %3\n" -" LNKSETD [%2], %1\n" -" DEFR %0, TXSTAT\n" -" ANDT %0, %0, #HI(0x3f000000)\n" -" CMPT %0, #HI(0x02000000)\n" -" BNZ 1b\n" - :"=&d" (temp),"=&da" (result) - :"da" (&v->counter),"bd" (i) - :"cc" ); - -smp_mb (); - -return result; -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OPstatic inline void atomic_clear_mask (unsigned int mask,atomic_t *v) {
arch/metag/include/asm/atomic_lock1.h +31 -45
86 lines not on this page · raw message
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,mips: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,mips: Fold atomic_ops
Commit-ID: ef31563e950c60bb41b97c2b61c32de874f3c949 Gitweb: http://git.kernel.org/tip/ef31563e950c60bb41b97c2b61c32de874f3c949 Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 26 Mar 2014 17:56:43 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:09 +0200
locking,arch,mips: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/mips/include/asm/atomic.h | 557 ++++++++++++++--------------------------- 1 file changed, 187 insertions(+), 370 deletions(-)
arch/mips/include/asm/atomic.h +187 -370
@@ -40,195 +40,103 @@*/ #define atomic_set (v, i) ((v)->counter = (i)) -/* -* atomic_add - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v. -*/ -static __inline__void atomic_add (int i,atomic_t * v) -{ -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -int temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: ll %0, %1 # atomic_add \n" -" addu %0, %2 \n" -" sc %0, %1 \n" -" beqzl %0, 1b \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -int temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" ll %0, %1 # atomic_add \n" -" addu %0, %2 \n" -" sc %0, %1 \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!temp)); - }else { -unsigned long flags; - -raw_local_irq_save (flags); - v->counter += i; -raw_local_irq_restore (flags); - } -} - -/* -* atomic_sub - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v. -*/ -static __inline__void atomic_sub (int i,atomic_t * v) -{ -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -int temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: ll %0, %1 # atomic_sub \n" -" subu %0, %2 \n" -" sc %0, %1 \n" -" beqzl %0, 1b \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -int temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" ll %0, %1 # atomic_sub \n" -" subu %0, %2 \n" -" sc %0, %1 \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!temp)); - }else { -unsigned long flags; - -raw_local_irq_save (flags); - v->counter -= i; -raw_local_irq_restore (flags); - } -} - -/* -* Same as above, but return the result value -*/ -static __inline__int atomic_add_return (int i,atomic_t * v) -{ -int result; - -smp_mb__before_llsc (); - -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -int temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: ll %1, %2 # atomic_add_return \n" -" addu %0, %1, %3 \n" -" sc %0, %2 \n" -" beqzl %0, 1b \n" -" addu %0, %1, %3 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -int temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" ll %1, %2 # atomic_add_return \n" -" addu %0, %1, %3 \n" -" sc %0, %2 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!result)); - - result = temp + i; - }else { -unsigned long flags; - -raw_local_irq_save (flags); - result = v->counter; - result += i; - v->counter = result; -raw_local_irq_restore (flags); - } - -smp_llsc_mb (); - -return result; +#define ATOMIC_OP (op, c_op, asm_op) \ +static __inline__ void atomic_##op(int i, atomic_t * v) \ +{ \ + if (kernel_uses_llsc && R10000_LLSC_WAR) { \ + int temp; \ + \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + "1: ll %0, %1 # atomic_" #op " \n" \ + " " #asm_op " %0, %2 \n" \ + " sc %0, %1 \n" \ + " beqzl %0, 1b \n" \ + " .set mips0 \n" \ + : "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } else if (kernel_uses_llsc) { \ + int temp; \ + \ + do { \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + " ll %0, %1 # atomic_" #op "\n" \ + " " #asm_op " %0, %2 \n" \ + " sc %0, %1 \n" \ + " .set mips0 \n" \ + : "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } while (unlikely(!temp)); \ + } else { \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + v->counter c_op i; \ + raw_local_irq_restore(flags); \ + } \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op, asm_op) \ +static __inline__ int atomic_##op##_return(int i, atomic_t * v) \ +{ \ + int result; \ + \ + smp_mb__before_llsc(); \ + \ + if (kernel_uses_llsc && R10000_LLSC_WAR) { \ + int temp; \ + \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + "1: ll %1, %2 # atomic_" #op "_return \n" \ + " " #asm_op " %0, %1, %3 \n" \ + " sc %0, %2 \n" \ + " beqzl %0, 1b \n" \ + " addu %0, %1, %3 \n" \ + " .set mips0 \n" \ + : "=&r" (result), "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } else if (kernel_uses_llsc) { \ + int temp; \ + \ + do { \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + " ll %1, %2 # atomic_" #op "_return \n" \ + " " #asm_op " %0, %1, %3 \n" \ + " sc %0, %2 \n" \ + " .set mips0 \n" \ + : "=&r" (result), "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } while (unlikely(!result)); \ + \ + result = temp + i; \ + } else { \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + result = v->counter; \ + result c_op i; \ + v->counter = result; \ + raw_local_irq_restore(flags); \ + } \ + \ + smp_llsc_mb(); \ + \ + return result; \ } -static __inline__int atomic_sub_return (int i,atomic_t * v) -{ -int result; +#define ATOMIC_OPS (op, c_op, asm_op) \ + ATOMIC_OP(op, c_op, asm_op) \ + ATOMIC_OP_RETURN(op, c_op, asm_op) -smp_mb__before_llsc (); +ATOMIC_OPS(add, +=, addu) +ATOMIC_OPS(sub, -=, subu) -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -int temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: ll %1, %2 # atomic_sub_return \n" -" subu %0, %1, %3 \n" -" sc %0, %2 \n" -" beqzl %0, 1b \n" -" subu %0, %1, %3 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter) - :"memory" ); - - result = temp - i; - }else if (kernel_uses_llsc) { -int temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" ll %1, %2 # atomic_sub_return \n" -" subu %0, %1, %3 \n" -" sc %0, %2 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!result)); - - result = temp - i; - }else { -unsigned long flags; - -raw_local_irq_save (flags); - result = v->counter; - result -= i; - v->counter = result; -raw_local_irq_restore (flags); - } - -smp_llsc_mb (); - -return result; -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP/* * atomic_sub_if_positive - conditionally subtract integer from atomic variable
@@ -407,195 +315,104 @@ static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)*/ #define atomic64_set (v, i) ((v)->counter = (i)) -/* -* atomic64_add - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic64_t -* -* Atomically adds @i to @v. -*/ -static __inline__void atomic64_add (long i,atomic64_t * v) -{ -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -long temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: lld %0, %1 # atomic64_add \n" -" daddu %0, %2 \n" -" scd %0, %1 \n" -" beqzl %0, 1b \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -long temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" lld %0, %1 # atomic64_add \n" -" daddu %0, %2 \n" -" scd %0, %1 \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!temp)); - }else { -unsigned long flags; - -raw_local_irq_save (flags); - v->counter += i; -raw_local_irq_restore (flags); - } +#define ATOMIC64_OP (op, c_op, asm_op) \ +static __inline__ void atomic64_##op(long i, atomic64_t * v) \ +{ \ + if (kernel_uses_llsc && R10000_LLSC_WAR) { \ + long temp; \ + \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + "1: lld %0, %1 # atomic64_" #op " \n" \ + " " #asm_op " %0, %2 \n" \ + " scd %0, %1 \n" \ + " beqzl %0, 1b \n" \ + " .set mips0 \n" \ + : "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } else if (kernel_uses_llsc) { \ + long temp; \ + \ + do { \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + " lld %0, %1 # atomic64_" #op "\n" \ + " " #asm_op " %0, %2 \n" \ + " scd %0, %1 \n" \ + " .set mips0 \n" \ + : "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } while (unlikely(!temp)); \ + } else { \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + v->counter c_op i; \ + raw_local_irq_restore(flags); \ + } \ +} \ + +#define ATOMIC64_OP_RETURN (op, c_op, asm_op) \ +static __inline__ long atomic64_##op##_return(long i, atomic64_t * v) \ +{ \ + long result; \ + \ + smp_mb__before_llsc(); \ + \ + if (kernel_uses_llsc && R10000_LLSC_WAR) { \ + long temp; \ + \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + "1: lld %1, %2 # atomic64_" #op "_return\n" \ + " " #asm_op " %0, %1, %3 \n" \ + " scd %0, %2 \n" \ + " beqzl %0, 1b \n" \ + " " #asm_op " %0, %1, %3 \n" \ + " .set mips0 \n" \ + : "=&r" (result), "=&r" (temp), "+m" (v->counter) \ + : "Ir" (i)); \ + } else if (kernel_uses_llsc) { \ + long temp; \ + \ + do { \ + __asm__ __volatile__( \ + " .set arch=r4000 \n" \ + " lld %1, %2 # atomic64_" #op "_return\n" \ + " " #asm_op " %0, %1, %3 \n" \ + " scd %0, %2 \n" \ + " .set mips0 \n" \ + : "=&r" (result), "=&r" (temp), "=m" (v->counter) \ + : "Ir" (i), "m" (v->counter) \ + : "memory"); \ + } while (unlikely(!result)); \ + \ + result = temp + i; \ + } else { \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + result = v->counter; \ + result c_op i; \ + v->counter = result; \ + raw_local_irq_restore(flags); \ + } \ + \ + smp_llsc_mb(); \ + \ + return result; \ } -/* -* atomic64_sub - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic64_t -* -* Atomically subtracts @i from @v. -*/ -static __inline__void atomic64_sub (long i,atomic64_t * v) -{ -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -long temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: lld %0, %1 # atomic64_sub \n" -" dsubu %0, %2 \n" -" scd %0, %1 \n" -" beqzl %0, 1b \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -long temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" lld %0, %1 # atomic64_sub \n" -" dsubu %0, %2 \n" -" scd %0, %1 \n" -" .set mips0 \n" - :"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }while (unlikely (!temp)); - }else { -unsigned long flags; - -raw_local_irq_save (flags); - v->counter -= i; -raw_local_irq_restore (flags); - } -} - -/* -* Same as above, but return the result value -*/ -static __inline__long atomic64_add_return (long i,atomic64_t * v) -{ -long result; +#define ATOMIC64_OPS (op, c_op, asm_op) \ + ATOMIC64_OP(op, c_op, asm_op) \ + ATOMIC64_OP_RETURN(op, c_op, asm_op) -smp_mb__before_llsc (); +ATOMIC64_OPS(add, +=, daddu) +ATOMIC64_OPS(sub, -=, dsubu) -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -long temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: lld %1, %2 # atomic64_add_return \n" -" daddu %0, %1, %3 \n" -" scd %0, %2 \n" -" beqzl %0, 1b \n" -" daddu %0, %1, %3 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"+m" (v->counter) - :"Ir" (i)); - }else if (kernel_uses_llsc) { -long temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" lld %1, %2 # atomic64_add_return \n" -" daddu %0, %1, %3 \n" -" scd %0, %2 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter) - :"memory" ); - }while (unlikely (!result)); - - result = temp + i; - }else { -unsigned long flags; - -raw_local_irq_save (flags); - result = v->counter; - result += i; - v->counter = result; -raw_local_irq_restore (flags); - } - -smp_llsc_mb (); - -return result; -} - -static __inline__long atomic64_sub_return (long i,atomic64_t * v) -{ -long result; - -smp_mb__before_llsc (); - -if (kernel_uses_llsc &&R10000_LLSC_WAR ) { -long temp; - - __asm__ __volatile__( -" .set arch=r4000 \n" -"1: lld %1, %2 # atomic64_sub_return \n" -" dsubu %0, %1, %3 \n" -" scd %0, %2 \n" -" beqzl %0, 1b \n" -" dsubu %0, %1, %3 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter) - :"memory" ); - }else if (kernel_uses_llsc) { -long temp; - -do { - __asm__ __volatile__( -" .set arch=r4000 \n" -" lld %1, %2 # atomic64_sub_return \n" -" dsubu %0, %1, %3 \n" -" scd %0, %2 \n" -" .set mips0 \n" - :"=&r" (result),"=&r" (temp),"=m" (v->counter) - :"Ir" (i),"m" (v->counter) - :"memory" ); - }while (unlikely (!result)); - - result = temp - i; - }else { -unsigned long flags; - -raw_local_irq_save (flags); - result = v->counter; - result -= i; - v->counter = result; -raw_local_irq_restore (flags); - } - -smp_llsc_mb (); - -return result; -} +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OP/* * atomic64_sub_if_positive - conditionally subtract integer from atomic variable
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,mn10300: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,mn10300: Fold atomic_ops
Commit-ID: e69a0ef76627005e3e83d0e086e6bb1d247bb65b Gitweb: http://git.kernel.org/tip/e69a0ef76627005e3e83d0e086e6bb1d247bb65b Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 26 Mar 2014 17:59:04 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:10 +0200
locking,arch,mn10300: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/mn10300/include/asm/atomic.h | 125 +++++++++++++------------------------- 1 file changed, 42 insertions(+), 83 deletions(-)
arch/mn10300/include/asm/atomic.h +42 -83
@@ -33,7 +33,6 @@* @v: pointer of type atomic_t * * Atomically reads the value of @v. Note that the guaranteed -* useful range of an atomic_t is only 24 bits. */ #define atomic_read (v) (ACCESS_ONCE((v)->counter))
@@ -43,102 +42,62 @@* @i: required value * * Atomically sets the value of @v to @i. Note that the guaranteed -* useful range of an atomic_t is only 24 bits. */ #define atomic_set (v, i) (((v)->counter) = (i)) -/** -* atomic_add_return - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v and returns the result -* Note that the guaranteed useful range of an atomic_t is only 24 bits. -*/ -static inline int atomic_add_return (int i,atomic_t *v) -{ -int retval; -#ifdef CONFIG_SMP -int status; - - asmvolatile ( -"1: mov %4,(_AAR,%3) \n" -" mov (_ADR,%3),%1 \n" -" add %5,%1 \n" -" mov %1,(_ADR,%3) \n" -" mov (_ADR,%3),%0 \n" /* flush */ -" mov (_ASR,%3),%0 \n" -" or %0,%0 \n" -" bne 1b \n" - :"=&r" (status),"=&r" (retval),"=m" (v->counter) - :"a" (ATOMIC_OPS_BASE_ADDR ),"r" (&v->counter),"r" (i) - :"memory" ,"cc" ); - -#else -unsigned long flags; +#define ATOMIC_OP (op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int retval, status; \ + \ + asm volatile( \ + "1: mov %4,(_AAR,%3) \n" \ + " mov (_ADR,%3),%1 \n" \ + " " #op " %5,%1 \n" \ + " mov %1,(_ADR,%3) \n" \ + " mov (_ADR,%3),%0 \n"/* flush */ \ + " mov (_ASR,%3),%0 \n" \ + " or %0,%0 \n" \ + " bne 1b \n" \ + : "=&r"(status), "=&r"(retval), "=m"(v->counter) \ + : "a"(ATOMIC_OPS_BASE_ADDR), "r"(&v->counter), "r"(i) \ + : "memory", "cc"); \ +} - flags =arch_local_cli_save (); - retval = v->counter; - retval += i; - v->counter = retval; -arch_local_irq_restore (flags); -#endif -return retval; +#define ATOMIC_OP_RETURN(op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int retval, status; \ + \ + asm volatile( \ + "1: mov %4,(_AAR,%3) \n" \ + " mov (_ADR,%3),%1 \n" \ + " " #op " %5,%1 \n" \ + " mov %1,(_ADR,%3) \n" \ + " mov (_ADR,%3),%0 \n"/* flush */ \ + " mov (_ASR,%3),%0 \n" \ + " or %0,%0 \n" \ + " bne 1b \n" \ + : "=&r"(status), "=&r"(retval), "=m"(v->counter) \ + : "a"(ATOMIC_OPS_BASE_ADDR), "r"(&v->counter), "r"(i) \ + : "memory", "cc"); \ + return retval; \ } -/** -* atomic_sub_return - subtract integer from atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v and returns the result -* Note that the guaranteed useful range of an atomic_t is only 24 bits. -*/ -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int retval; -#ifdef CONFIG_SMP -int status; +#define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) - asmvolatile ( -"1: mov %4,(_AAR,%3) \n" -" mov (_ADR,%3),%1 \n" -" sub %5,%1 \n" -" mov %1,(_ADR,%3) \n" -" mov (_ADR,%3),%0 \n" /* flush */ -" mov (_ASR,%3),%0 \n" -" or %0,%0 \n" -" bne 1b \n" - :"=&r" (status),"=&r" (retval),"=m" (v->counter) - :"a" (ATOMIC_OPS_BASE_ADDR ),"r" (&v->counter),"r" (i) - :"memory" ,"cc" ); +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -#else -unsigned long flags; - flags =arch_local_cli_save (); - retval = v->counter; - retval -= i; - v->counter = retval; -arch_local_irq_restore (flags); -#endif -return retval; -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP static inline int atomic_add_negative(int i, atomic_t *v) { return atomic_add_return(i, v) < 0; } -static inline void atomic_add (int i,atomic_t *v) -{ -atomic_add_return (i, v); -} - -static inline void atomic_sub (int i,atomic_t *v) -{ -atomic_sub_return (i, v); -} - static inline void atomic_inc(atomic_t *v) {atomic_add_return (1 , v);
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,arm64: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,arm64: Fold atomic_ops
Commit-ID: 92ba1f530b4f90db78eb45f4b6598e75939146bd Gitweb: http://git.kernel.org/tip/92ba1f530b4f90db78eb45f4b6598e75939146bd Author: Peter Zijlstra <[email protected]> AuthorDate: Sun, 23 Mar 2014 16:57:20 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:04 +0200
locking,arch,arm64: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires the asm_op due to eor.
--- arch/arm64/include/asm/atomic.h | 197 ++++++++++++++++------------------------ 1 file changed, 80 insertions(+), 117 deletions(-)
arch/arm64/include/asm/atomic.h +80 -117
@@ -43,69 +43,51 @@* store exclusive to ensure that these are atomic. We may loop * to ensure that the update happens. */ -static inline void atomic_add (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - - asmvolatile ("// atomic_add\n" -"1: ldxr %w0, %2\n" -" add %w0, %w0, %w3\n" -" stxr %w1, %w0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i)); -} - -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - - asmvolatile ("// atomic_add_return\n" -"1: ldxr %w0, %2\n" -" add %w0, %w0, %w3\n" -" stlxr %w1, %w0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i) - :"memory" ); - -smp_mb (); -return result; -} - -static inline void atomic_sub (int i,atomic_t *v) -{ -unsigned long tmp; -int result; - asmvolatile ("// atomic_sub\n" -"1: ldxr %w0, %2\n" -" sub %w0, %w0, %w3\n" -" stxr %w1, %w0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i)); +#define ATOMIC_OP (op, asm_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + asm volatile("// atomic_" #op "\n" \ +"1: ldxr %w0, %2\n" \ +" " #asm_op " %w0, %w0, %w3\n" \ +" stxr %w1, %w0, %2\n" \ +" cbnz %w1, 1b" \ + : "=&r" (result), "=&r" (tmp), "+Q" (v->counter) \ + : "Ir" (i)); \ +} \ + +#define ATOMIC_OP_RETURN (op, asm_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + asm volatile("// atomic_" #op "_return\n" \ +"1: ldxr %w0, %2\n" \ +" " #asm_op " %w0, %w0, %w3\n" \ +" stlxr %w1, %w0, %2\n" \ +" cbnz %w1, 1b" \ + : "=&r" (result), "=&r" (tmp), "+Q" (v->counter) \ + : "Ir" (i) \ + : "memory"); \ + \ + smp_mb(); \ + return result; \ } -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long tmp; -int result; +#define ATOMIC_OPS (op, asm_op) \ + ATOMIC_OP(op, asm_op) \ + ATOMIC_OP_RETURN(op, asm_op) - asmvolatile ("// atomic_sub_return\n" -"1: ldxr %w0, %2\n" -" sub %w0, %w0, %w3\n" -" stlxr %w1, %w0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i) - :"memory" ); +ATOMIC_OPS(add, add) +ATOMIC_OPS(sub, sub) -smp_mb (); -return result; -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OPstatic inline int atomic_cmpxchg (atomic_t *ptr,int old,int new) {
@@ -160,69 +142,50 @@ static inline int __atomic_add_unless(atomic_t *v, int a, int u)#define atomic64_read (v) (*(volatile long *)&(v)->counter)#define atomic64_set (v,i) (((v)->counter) = (i)) -static inline void atomic64_add (u64 i,atomic64_t *v) -{ -long result; -unsigned long tmp; - - asmvolatile ("// atomic64_add\n" -"1: ldxr %0, %2\n" -" add %0, %0, %3\n" -" stxr %w1, %0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i)); +#define ATOMIC64_OP (op, asm_op) \ +static inline void atomic64_##op(long i, atomic64_t *v) \ +{ \ + long result; \ + unsigned long tmp; \ + \ + asm volatile("// atomic64_" #op "\n" \ +"1: ldxr %0, %2\n" \ +" " #asm_op " %0, %0, %3\n" \ +" stxr %w1, %0, %2\n" \ +" cbnz %w1, 1b" \ + : "=&r" (result), "=&r" (tmp), "+Q" (v->counter) \ + : "Ir" (i)); \ +} \ + +#define ATOMIC64_OP_RETURN (op, asm_op) \ +static inline long atomic64_##op##_return(long i, atomic64_t *v) \ +{ \ + long result; \ + unsigned long tmp; \ + \ + asm volatile("// atomic64_" #op "_return\n" \ +"1: ldxr %0, %2\n" \ +" " #asm_op " %0, %0, %3\n" \ +" stlxr %w1, %0, %2\n" \ +" cbnz %w1, 1b" \ + : "=&r" (result), "=&r" (tmp), "+Q" (v->counter) \ + : "Ir" (i) \ + : "memory"); \ + \ + smp_mb(); \ + return result; \ } -static inline long atomic64_add_return (long i,atomic64_t *v) -{ -long result; -unsigned long tmp; +#define ATOMIC64_OPS (op, asm_op) \ + ATOMIC64_OP(op, asm_op) \ + ATOMIC64_OP_RETURN(op, asm_op) - asmvolatile ("// atomic64_add_return\n" -"1: ldxr %0, %2\n" -" add %0, %0, %3\n" -" stlxr %w1, %0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i) - :"memory" ); +ATOMIC64_OPS(add, add) +ATOMIC64_OPS(sub, sub) -smp_mb (); -return result; -} - -static inline void atomic64_sub (u64 i,atomic64_t *v) -{ -long result; -unsigned long tmp; - - asmvolatile ("// atomic64_sub\n" -"1: ldxr %0, %2\n" -" sub %0, %0, %3\n" -" stxr %w1, %0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i)); -} - -static inline long atomic64_sub_return (long i,atomic64_t *v) -{ -long result; -unsigned long tmp; - - asmvolatile ("// atomic64_sub_return\n" -"1: ldxr %0, %2\n" -" sub %0, %0, %3\n" -" stlxr %w1, %0, %2\n" -" cbnz %w1, 1b" - :"=&r" (result),"=&r" (tmp),"+Q" (v->counter) - :"Ir" (i) - :"memory" ); - -smp_mb (); -return result; -} +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OPstatic inline long atomic64_cmpxchg (atomic64_t *ptr,long old,long new) {
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,powerpc: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,powerpc: Fold atomic_ops
Commit-ID: af095dd60bdc52b11c186c3151e8e38d6faa094c Gitweb: http://git.kernel.org/tip/af095dd60bdc52b11c186c3151e8e38d6faa094c Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 26 Mar 2014 18:11:31 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:11 +0200
locking,arch,powerpc: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
Requires asm_op because PPC asm is weird :-)
--- arch/powerpc/include/asm/atomic.h | 198 +++++++++++++++----------------------- 1 file changed, 77 insertions(+), 121 deletions(-)
arch/powerpc/include/asm/atomic.h +77 -121
@@ -26,76 +26,53 @@ static __inline__ void atomic_set(atomic_t *v, int i) __asm__ __volatile__("stw%U0%X0 %1,%0" :"=m" (v->counter) :"r" (i)); } -static __inline__void atomic_add (int a,atomic_t *v) -{ -int t; - - __asm__ __volatile__( -"1: lwarx %0,0,%3 # atomic_add\n\ -add %0,%2,%0\n" - PPC405_ERR77(0,%3) -" stwcx. %0,0,%3 \n\ - bne- 1b" - : "=&r" (t), "+m" (v->counter) - : "r" (a), "r" (&v->counter) - : "cc"); +#define ATOMIC_OP (op, asm_op) \ +static __inline__ void atomic_##op(int a, atomic_t *v) \ +{ \ + int t; \ + \ + __asm__ __volatile__( \ +"1: lwarx %0,0,%3 # atomic_" #op "\n" \ + #asm_op " %0,%2,%0\n" \ + PPC405_ERR77(0,%3) \ +" stwcx. %0,0,%3 \n" \ +" bne- 1b\n" \ + : "=&r" (t), "+m" (v->counter) \ + : "r" (a), "r" (&v->counter) \ + : "cc"); \ +} \ + +#define ATOMIC_OP_RETURN (op, asm_op) \ +static __inline__ int atomic_##op##_return(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" \ +" bne- 1b\n" \ + PPC_ATOMIC_EXIT_BARRIER \ + : "=&r" (t) \ + : "r" (a), "r" (&v->counter) \ + : "cc", "memory"); \ + \ + return t; \ } -static __inline__int atomic_add_return (int a,atomic_t *v) -{ -int t; +#define ATOMIC_OPS (op, asm_op) ATOMIC_OP(op, asm_op) ATOMIC_OP_RETURN(op, asm_op) - __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER -"1: lwarx %0,0,%2 # atomic_add_return\n\ -add %0,%1,%0\n" - PPC405_ERR77(0,%2) -" stwcx. %0,0,%2 \n\ -bne- 1b" -PPC_ATOMIC_EXIT_BARRIER - : "=&r" (t) - : "r" (a), "r" (&v->counter) - : "cc", "memory"); +ATOMIC_OPS(add, add) +ATOMIC_OPS(sub, subf) - return t; -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP#define atomic_add_negative (a, v) (atomic_add_return((a), (v)) < 0) -static __inline__void atomic_sub (int a,atomic_t *v) -{ -int t; - - __asm__ __volatile__( -"1: lwarx %0,0,%3 # atomic_sub\n\ -subf %0,%2,%0\n" - PPC405_ERR77(0,%3) -" stwcx. %0,0,%3 \n\ - bne- 1b" - : "=&r" (t), "+m" (v->counter) - : "r" (a), "r" (&v->counter) - : "cc"); -} - -static __inline__int atomic_sub_return (int a,atomic_t *v) -{ -int t; - - __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER -"1: lwarx %0,0,%2 # atomic_sub_return\n\ -subf %0,%1,%0\n" - PPC405_ERR77(0,%2) -" stwcx. %0,0,%2 \n\ -bne- 1b" -PPC_ATOMIC_EXIT_BARRIER - : "=&r" (t) - : "r" (a), "r" (&v->counter) - : "cc", "memory"); - - return t; -} -static __inline__void atomic_inc (atomic_t *v) {int t;
@@ -289,71 +266,50 @@ static __inline__ void atomic64_set(atomic64_t *v, long i) __asm__ __volatile__("std%U0%X0 %1,%0" :"=m" (v->counter) :"r" (i)); } -static __inline__void atomic64_add (long a,atomic64_t *v) -{ -long t; - - __asm__ __volatile__( -"1: ldarx %0,0,%3 # atomic64_add\n\ -add %0,%2,%0\n\ -stdcx. %0,0,%3 \n\ -bne- 1b" - :"=&r" (t),"+m" (v->counter) - :"r" (a),"r" (&v->counter) - :"cc" ); +#define ATOMIC64_OP (op, asm_op) \ +static __inline__ void atomic64_##op(long a, atomic64_t *v) \ +{ \ + long t; \ + \ + __asm__ __volatile__( \ +"1: ldarx %0,0,%3 # atomic64_" #op "\n" \ + #asm_op " %0,%2,%0\n" \ +" stdcx. %0,0,%3 \n" \ +" bne- 1b\n" \ + : "=&r" (t), "+m" (v->counter) \ + : "r" (a), "r" (&v->counter) \ + : "cc"); \ } -static __inline__long atomic64_add_return (long a,atomic64_t *v) -{ -long t; - - __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER -"1: ldarx %0,0,%2 # atomic64_add_return\n\ -add %0,%1,%0\n\ -stdcx. %0,0,%2 \n\ -bne- 1b" -PPC_ATOMIC_EXIT_BARRIER - :"=&r" (t) - :"r" (a),"r" (&v->counter) - :"cc" ,"memory" ); - -return t; +#define ATOMIC64_OP_RETURN (op, asm_op) \ +static __inline__ long atomic64_##op##_return(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" \ +" bne- 1b\n" \ + PPC_ATOMIC_EXIT_BARRIER \ + : "=&r" (t) \ + : "r" (a), "r" (&v->counter) \ + : "cc", "memory"); \ + \ + return t; \ } -#define atomic64_add_negative (a, v) (atomic64_add_return((a), (v)) < 0) - -static __inline__void atomic64_sub (long a,atomic64_t *v) -{ -long t; - - __asm__ __volatile__( -"1: ldarx %0,0,%3 # atomic64_sub\n\ -subf %0,%2,%0\n\ -stdcx. %0,0,%3 \n\ -bne- 1b" - :"=&r" (t),"+m" (v->counter) - :"r" (a),"r" (&v->counter) - :"cc" ); -} +#define ATOMIC64_OPS (op, asm_op) ATOMIC64_OP(op, asm_op) ATOMIC64_OP_RETURN(op, asm_op) -static __inline__long atomic64_sub_return (long a,atomic64_t *v) -{ -long t; +ATOMIC64_OPS(add, add) +ATOMIC64_OPS(sub, subf) - __asm__ __volatile__( -PPC_ATOMIC_ENTRY_BARRIER -"1: ldarx %0,0,%2 # atomic64_sub_return\n\ -subf %0,%1,%0\n\ -stdcx. %0,0,%2 \n\ -bne- 1b" -PPC_ATOMIC_EXIT_BARRIER - :"=&r" (t) - :"r" (a),"r" (&v->counter) - :"cc" ,"memory" ); +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OP -return t; -} +#define atomic64_add_negative (a, v) (atomic64_add_return((a), (v)) < 0)static __inline__void atomic64_inc (atomic64_t *v) {
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,sh: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,sh: Fold atomic_ops
Commit-ID: c6470150dff9aff682063890c9b8eac71b695def Gitweb: http://git.kernel.org/tip/c6470150dff9aff682063890c9b8eac71b695def Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 26 Mar 2014 18:12:45 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:12 +0200
locking,arch,sh: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/sh/include/asm/atomic-grb.h | 119 ++++++++++++++------------------------ arch/sh/include/asm/atomic-irq.h | 62 +++++++++----------- arch/sh/include/asm/atomic-llsc.h | 101 +++++++++++++------------------- 3 files changed, 112 insertions(+), 170 deletions(-)
arch/sh/include/asm/atomic-grb.h +45 -74
@@ -1,85 +1,56 @@ #ifndef __ASM_SH_ATOMIC_GRB_H #define __ASM_SH_ATOMIC_GRB_H -static inline void atomic_add (int i,atomic_t *v) -{ -int tmp; - - __asm__ __volatile__ ( -" .align 2 \n\t" -" mova 1f, r0 \n\t" /* r0 = end point */ -" mov r15, r1 \n\t" /* r1 = saved sp */ -" mov #-6, r15 \n\t" /* LOGIN: r15 = size */ -" mov.l @%1, %0 \n\t" /* load old value */ -" add %2, %0 \n\t" /* add */ -" mov.l %0, @%1 \n\t" /* store new value */ -"1: mov r1, r15 \n\t" /* LOGOUT */ - :"=&r" (tmp), -"+r" (v) - :"r" (i) - :"memory" ,"r0" ,"r1" ); -} - -static inline void atomic_sub (int i,atomic_t *v) -{ -int tmp; - - __asm__ __volatile__ ( -" .align 2 \n\t" -" mova 1f, r0 \n\t" /* r0 = end point */ -" mov r15, r1 \n\t" /* r1 = saved sp */ -" mov #-6, r15 \n\t" /* LOGIN: r15 = size */ -" mov.l @%1, %0 \n\t" /* load old value */ -" sub %2, %0 \n\t" /* sub */ -" mov.l %0, @%1 \n\t" /* store new value */ -"1: mov r1, r15 \n\t" /* LOGOUT */ - :"=&r" (tmp), -"+r" (v) - :"r" (i) - :"memory" ,"r0" ,"r1" ); -} - -static inline int atomic_add_return (int i,atomic_t *v) -{ -int tmp; +#define ATOMIC_OP(op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int tmp; \ + \ + __asm__ __volatile__ ( \ + " .align 2 \n\t" \ + " mova 1f, r0 \n\t" /* r0 = end point */ \ + " mov r15, r1 \n\t" /* r1 = saved sp */ \ + " mov #-6, r15 \n\t" /* LOGIN: r15 = size */ \ + " mov.l @%1, %0 \n\t" /* load old value */ \ + " " #op " %2, %0 \n\t" /* $op */ \ + " mov.l %0, @%1 \n\t" /* store new value */ \ + "1: mov r1, r15 \n\t" /* LOGOUT */ \ + : "=&r" (tmp), \ + "+r" (v) \ + : "r" (i) \ + : "memory" , "r0", "r1"); \ +} \ - __asm__ __volatile__ ( -" .align 2 \n\t" -" mova 1f, r0 \n\t" /* r0 = end point */ -" mov r15, r1 \n\t" /* r1 = saved sp */ -" mov #-6, r15 \n\t" /* LOGIN: r15 = size */ -" mov.l @%1, %0 \n\t" /* load old value */ -" add %2, %0 \n\t" /* add */ -" mov.l %0, @%1 \n\t" /* store new value */ -"1: mov r1, r15 \n\t" /* LOGOUT */ - :"=&r" (tmp), -"+r" (v) - :"r" (i) - :"memory" ,"r0" ,"r1" ); - -return tmp; +#define ATOMIC_OP_RETURN(op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int tmp; \ + \ + __asm__ __volatile__ ( \ + " .align 2 \n\t" \ + " mova 1f, r0 \n\t" /* r0 = end point */ \ + " mov r15, r1 \n\t" /* r1 = saved sp */ \ + " mov #-6, r15 \n\t" /* LOGIN: r15 = size */ \ + " mov.l @%1, %0 \n\t" /* load old value */ \ + " " #op " %2, %0 \n\t" /* $op */ \ + " mov.l %0, @%1 \n\t" /* store new value */ \ + "1: mov r1, r15 \n\t" /* LOGOUT */ \ + : "=&r" (tmp), \ + "+r" (v) \ + : "r" (i) \ + : "memory" , "r0", "r1"); \ + \ + return tmp; \ } -static inline int atomic_sub_return (int i,atomic_t *v) -{ -int tmp; +#define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) - __asm__ __volatile__ ( -" .align 2 \n\t" -" mova 1f, r0 \n\t" /* r0 = end point */ -" mov r15, r1 \n\t" /* r1 = saved sp */ -" mov #-6, r15 \n\t" /* LOGIN: r15 = size */ -" mov.l @%1, %0 \n\t" /* load old value */ -" sub %2, %0 \n\t" /* sub */ -" mov.l %0, @%1 \n\t" /* store new value */ -"1: mov r1, r15 \n\t" /* LOGOUT */ - :"=&r" (tmp), -"+r" (v) - :"r" (i) - :"memory" ,"r0" ,"r1" ); +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -return tmp; -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP static inline void atomic_clear_mask(unsigned int mask, atomic_t *v) {
arch/sh/include/asm/atomic-irq.h +26 -36
75 lines not on this page · raw message
arch/sh/include/asm/atomic-llsc.h +41 -60
119 lines not on this page · raw message
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,sparc: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,sparc: Fold atomic_ops
Commit-ID: 4f3316c2b5fe2062c26c9b66915b5a5c80c60a5c Gitweb: http://git.kernel.org/tip/4f3316c2b5fe2062c26c9b66915b5a5c80c60a5c Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 26 Mar 2014 18:29:28 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:13 +0200
locking,arch,sparc: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/sparc/include/asm/atomic_32.h | 17 ++-- arch/sparc/include/asm/atomic_64.h | 45 +++++----- arch/sparc/kernel/smp_64.c | 2 +- arch/sparc/lib/atomic32.c | 29 ++++--- arch/sparc/lib/atomic_64.S | 163 +++++++++++++++---------------------- arch/sparc/lib/ksyms.c | 25 ++++-- 6 files changed, 133 insertions(+), 148 deletions(-)
arch/sparc/include/asm/atomic_32.h +8 -9
@@ -20,7 +20,7 @@#define ATOMIC_INIT (i) { (i) } -int __atomic_add_return (int ,atomic_t *); +int atomic_add_return (int ,atomic_t *);int atomic_cmpxchg (atomic_t *,int ,int );#define atomic_xchg (v, new) (xchg(&((v)->counter), new))int __atomic_add_unless (atomic_t *,int ,int );
@@ -28,15 +28,14 @@ void atomic_set(atomic_t *, int);#define atomic_read (v) (*(volatile int *)&(v)->counter) -#define atomic_add (i, v) ((void)__atomic_add_return( (int)(i), (v))) -#define atomic_sub (i, v) ((void)__atomic_add_return(-(int)(i), (v))) -#define atomic_inc (v) ((void)__atomic_add_return( 1, (v))) -#define atomic_dec (v) ((void)__atomic_add_return( -1, (v))) +#define atomic_add (i, v) ((void)atomic_add_return( (int)(i), (v))) +#define atomic_sub (i, v) ((void)atomic_add_return(-(int)(i), (v))) +#define atomic_inc (v) ((void)atomic_add_return( 1, (v))) +#define atomic_dec (v) ((void)atomic_add_return( -1, (v))) -#define atomic_add_return (i, v) (__atomic_add_return( (int)(i), (v))) -#define atomic_sub_return (i, v) (__atomic_add_return(-(int)(i), (v))) -#define atomic_inc_return (v) (__atomic_add_return( 1, (v))) -#define atomic_dec_return (v) (__atomic_add_return( -1, (v))) +#define atomic_sub_return (i, v) (atomic_add_return(-(int)(i), (v))) +#define atomic_inc_return (v) (atomic_add_return( 1, (v))) +#define atomic_dec_return (v) (atomic_add_return( -1, (v)))#define atomic_add_negative (a, v) (atomic_add_return((a), (v)) < 0)
arch/sparc/include/asm/atomic_64.h +23 -22
69 lines not on this page · raw message
arch/sparc/kernel/smp_64.c +1 -1
@@ -1138,7 +1138,7 @@ static unsigned long penguins_are_doing_time;void smp_capture (void ) { -int result =atomic_add_ret (1 , &smp_capture_depth); +int result =atomic_add_return (1 , &smp_capture_depth);if (result ==1 ) {int ncpus =num_online_cpus ();
arch/sparc/lib/atomic32.c +17 -12
@@ -27,18 +27,23 @@ static DEFINE_SPINLOCK(dummy);#endif /* SMP */ -int __atomic_add_return (int i,atomic_t *v) -{ -int ret; -unsigned long flags; -spin_lock_irqsave (ATOMIC_HASH (v), flags); - - ret = (v->counter += i); - -spin_unlock_irqrestore (ATOMIC_HASH (v), flags); -return ret; -} -EXPORT_SYMBOL (__atomic_add_return); +#define ATOMIC_OP (op, cop) \ +int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int ret; \ + unsigned long flags; \ + spin_lock_irqsave(ATOMIC_HASH(v), flags); \ + \ + ret = (v->counter cop i); \ + \ + spin_unlock_irqrestore(ATOMIC_HASH(v), flags); \ + return ret; \ +} \ +EXPORT_SYMBOL(atomic_##op##_return); + +ATOMIC_OP(add, +=) + +#undef ATOMIC_OP int atomic_cmpxchg(atomic_t *v, int old, int new) {
arch/sparc/lib/atomic_64.S +67 -96
176 lines not on this page · raw message
arch/sparc/lib/ksyms.c +17 -8
@@ -99,14 +99,23 @@ EXPORT_SYMBOL(___copy_in_user);EXPORT_SYMBOL (__clear_user);/* Atomic counter implementation. */ -EXPORT_SYMBOL (atomic_add); -EXPORT_SYMBOL (atomic_add_ret); -EXPORT_SYMBOL (atomic_sub); -EXPORT_SYMBOL (atomic_sub_ret); -EXPORT_SYMBOL (atomic64_add); -EXPORT_SYMBOL (atomic64_add_ret); -EXPORT_SYMBOL (atomic64_sub); -EXPORT_SYMBOL (atomic64_sub_ret); +#define ATOMIC_OP (op) \ +EXPORT_SYMBOL(atomic_##op); \ +EXPORT_SYMBOL(atomic64_##op); + +#define ATOMIC_OP_RETURN (op) \ +EXPORT_SYMBOL(atomic_##op##_return); \ +EXPORT_SYMBOL(atomic64_##op##_return); + +#define ATOMIC_OPS (op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) + +ATOMIC_OPS(add) +ATOMIC_OPS(sub) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP +EXPORT_SYMBOL (atomic64_dec_if_positive);/* Atomic bit operations. */
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,xtensa: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,xtensa: Fold atomic_ops
Commit-ID: d4608dd5b4ec13855680b89f719d8d4b2da92411 Gitweb: http://git.kernel.org/tip/d4608dd5b4ec13855680b89f719d8d4b2da92411 Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 26 Mar 2014 18:31:12 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:14 +0200
locking,arch,xtensa: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/xtensa/include/asm/atomic.h | 233 ++++++++++++++------------------------- 1 file changed, 82 insertions(+), 151 deletions(-)
arch/xtensa/include/asm/atomic.h +82 -151
@@ -58,165 +58,96 @@*/ #define atomic_set (v,i) ((v)->counter = (i)) -/** -* atomic_add - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v. -*/ -static inline void atomic_add (int i,atomic_t * v) -{#if XCHAL_HAVE_S32C1I -unsigned long tmp; -int result; - - __asm__ __volatile__( -"1: l32i %1, %3, 0\n" -" wsr %1, scompare1\n" -" add %0, %1, %2\n" -" s32c1i %0, %3, 0\n" -" bne %0, %1, 1b\n" - :"=&a" (result),"=&a" (tmp) - :"a" (i),"a" (v) - :"memory" - ); -#else -unsigned int vval; - - __asm__ __volatile__( - " rsil a15, "__stringify(LOCKLEVEL)"\n" -" l32i %0, %2, 0\n" -" add %0, %0, %1\n" -" s32i %0, %2, 0\n" -" wsr a15, ps\n" -" rsync\n" - : "=&a" (vval) - : "a" (i), "a" (v) - : "a15", "memory" - ); -#endif -} - -/** -* atomic_sub - subtract the atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v. -*/ -static inline void atomic_sub (int i,atomic_t *v) -{ -#if XCHAL_HAVE_S32C1I -unsigned long tmp; -int result; - - __asm__ __volatile__( -"1: l32i %1, %3, 0\n" -" wsr %1, scompare1\n" -" sub %0, %1, %2\n" -" s32c1i %0, %3, 0\n" -" bne %0, %1, 1b\n" - :"=&a" (result),"=&a" (tmp) - :"a" (i),"a" (v) - :"memory" - ); -#else -unsigned int vval; - - __asm__ __volatile__( - " rsil a15, "__stringify(LOCKLEVEL)"\n" -" l32i %0, %2, 0\n" -" sub %0, %0, %1\n" -" s32i %0, %2, 0\n" -" wsr a15, ps\n" -" rsync\n" - : "=&a" (vval) - : "a" (i), "a" (v) - : "a15", "memory" - ); -#endif +#define ATOMIC_OP (op) \ +static inline void atomic_##op(int i, atomic_t * v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + __asm__ __volatile__( \ + "1: l32i %1, %3, 0\n" \ + " wsr %1, scompare1\n" \ + " " #op " %0, %1, %2\n" \ + " s32c1i %0, %3, 0\n" \ + " bne %0, %1, 1b\n" \ + : "=&a" (result), "=&a" (tmp) \ + : "a" (i), "a" (v) \ + : "memory" \ + ); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static inline int atomic_##op##_return(int i, atomic_t * v) \ +{ \ + unsigned long tmp; \ + int result; \ + \ + __asm__ __volatile__( \ + "1: l32i %1, %3, 0\n" \ + " wsr %1, scompare1\n" \ + " " #op " %0, %1, %2\n" \ + " s32c1i %0, %3, 0\n" \ + " bne %0, %1, 1b\n" \ + " " #op " %0, %0, %2\n" \ + : "=&a" (result), "=&a" (tmp) \ + : "a" (i), "a" (v) \ + : "memory" \ + ); \ + \ + return result; \ } -/* -* We use atomic_{add|sub}_return to define other functions. -*/ - -static inline int atomic_add_return (int i,atomic_t * v) -{ -#if XCHAL_HAVE_S32C1I -unsigned long tmp; -int result; - - __asm__ __volatile__( -"1: l32i %1, %3, 0\n" -" wsr %1, scompare1\n" -" add %0, %1, %2\n" -" s32c1i %0, %3, 0\n" -" bne %0, %1, 1b\n" -" add %0, %0, %2\n" - :"=&a" (result),"=&a" (tmp) - :"a" (i),"a" (v) - :"memory" - ); - -return result; -#else -unsigned int vval; - - __asm__ __volatile__( - " rsil a15,"__stringify(LOCKLEVEL)"\n" -" l32i %0, %2, 0\n" -" add %0, %0, %1\n" -" s32i %0, %2, 0\n" -" wsr a15, ps\n" -" rsync\n" - : "=&a" (vval) - : "a" (i), "a" (v) - : "a15", "memory" - ); - -return vval; -#endif +#else /* XCHAL_HAVE_S32C1I */ + +#define ATOMIC_OP (op) \ +static inline void atomic_##op(int i, atomic_t * v) \ +{ \ + unsigned int vval; \ + \ + __asm__ __volatile__( \ + " rsil a15, "__stringify(LOCKLEVEL)"\n"\ + " l32i %0, %2, 0\n" \ + " " #op " %0, %0, %1\n" \ + " s32i %0, %2, 0\n" \ + " wsr a15, ps\n" \ + " rsync\n" \ + : "=&a" (vval) \ + : "a" (i), "a" (v) \ + : "a15", "memory" \ + ); \ +} \ + +#define ATOMIC_OP_RETURN (op) \ +static inline int atomic_##op##_return(int i, atomic_t * v) \ +{ \ + unsigned int vval; \ + \ + __asm__ __volatile__( \ + " rsil a15,"__stringify(LOCKLEVEL)"\n" \ + " l32i %0, %2, 0\n" \ + " " #op " %0, %0, %1\n" \ + " s32i %0, %2, 0\n" \ + " wsr a15, ps\n" \ + " rsync\n" \ + : "=&a" (vval) \ + : "a" (i), "a" (v) \ + : "a15", "memory" \ + ); \ + \ + return vval; \ } -static inline int atomic_sub_return (int i,atomic_t * v) -{ -#if XCHAL_HAVE_S32C1I -unsigned long tmp; -int result; +#endif /* XCHAL_HAVE_S32C1I */ - __asm__ __volatile__( -"1: l32i %1, %3, 0\n" -" wsr %1, scompare1\n" -" sub %0, %1, %2\n" -" s32c1i %0, %3, 0\n" -" bne %0, %1, 1b\n" -" sub %0, %0, %2\n" - :"=&a" (result),"=&a" (tmp) - :"a" (i),"a" (v) - :"memory" - ); +#define ATOMIC_OPS (op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) -return result; -#else -unsigned int vval; - - __asm__ __volatile__( - " rsil a15,"__stringify(LOCKLEVEL)"\n" -" l32i %0, %2, 0\n" -" sub %0, %0, %1\n" -" s32i %0, %2, 0\n" -" wsr a15, ps\n" -" rsync\n" - : "=&a" (vval) - : "a" (i), "a" (v) - : "a15", "memory" - ); +ATOMIC_OPS(add) +ATOMIC_OPS(sub) -return vval; -#endif -} +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP/** * atomic_sub_and_test - subtract value from variable and test result
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch: Rewrite generic atomic support
In reply to Peter Zijlstralocking,arch: Rewrite generic atomic support
Commit-ID: 560cb12a4080a48b84da8b96878cafbd193c4d64 Gitweb: http://git.kernel.org/tip/560cb12a4080a48b84da8b96878cafbd193c4d64 Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 23 Apr 2014 16:12:30 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:14 +0200
locking,arch: Rewrite generic atomic support
Rewrite generic atomic support to only require cmpxchg(), generate all other primitives from that.
Furthermore reduce the endless repetition for all these primitives to a few CPP macros. This way we get more for less lines.
--- include/asm-generic/atomic.h | 192 ++++++++++++++++++++--------------------- include/asm-generic/atomic64.h | 20 ++++- lib/atomic64.c | 83 ++++++++---------- 3 files changed, 148 insertions(+), 147 deletions(-)
include/asm-generic/atomic.h +96 -96
@@ -18,14 +18,100 @@#include <asm/cmpxchg.h> #include <asm/barrier.h> +/* +* atomic_$op() - $op integer to atomic variable +* @i: integer value to $op +* @v: pointer to the atomic variable +* +* Atomically $ops @i to @v. Does not strictly guarantee a memory-barrier, use +* smp_mb__{before,after}_atomic(). +*/ + +/* +* atomic_$op_return() - $op interer to atomic variable and returns the result +* @i: integer value to $op +* @v: pointer to the atomic variable +* +* Atomically $ops @i to @v. Does imply a full memory barrier. +*/ +#ifdef CONFIG_SMP -/* Force people to define core atomics */ -# if !defined(atomic_add_return) || !defined(atomic_sub_return) || \ - !defined(atomic_clear_mask) || !defined(atomic_set_mask) -# error "SMP requires a little arch-specific magic" -# endif + +/* we can build all atomic primitives from cmpxchg */ + +#define ATOMIC_OP (op, c_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + int c, old; \ + \ + c = v->counter; \ + while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \ + c = old; \ +} + +#define ATOMIC_OP_RETURN (op, c_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + int c, old; \ + \ + c = v->counter; \ + while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \ + c = old; \ + \ + return c c_op i; \ +} + +#else + +#include <linux/irqflags.h> + +#define ATOMIC_OP (op, c_op) \ +static inline void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + \ + raw_local_irq_save(flags); \ + v->counter = v->counter c_op i; \ + raw_local_irq_restore(flags); \ +} + +#define ATOMIC_OP_RETURN (op, c_op) \ +static inline int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + int ret; \ + \ + raw_local_irq_save(flags); \ + ret = (v->counter = v->counter c_op i); \ + raw_local_irq_restore(flags); \ + \ + return ret; \ +} + +#endif /* CONFIG_SMP */ + +#ifndef atomic_add_return +ATOMIC_OP_RETURN(add, +) +#endif + +#ifndef atomic_sub_return +ATOMIC_OP_RETURN(sub, -) +#endif + +#ifndef atomic_clear_mask +ATOMIC_OP(and, &) +#define atomic_clear_mask(i, v) atomic_and(~(i), (v)) #endif +#ifndef atomic_set_mask +#define CONFIG_ARCH_HAS_ATOMIC_OR +ATOMIC_OP(or, |) +#define atomic_set_mask(i, v) atomic_or((i), (v)) +#endif + +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP +/* * Atomic operations that C can't guarantee us. Useful for * resource counting etc..
@@ -33,8 +119,6 @@#define ATOMIC_INIT (i) { (i) } -#ifdef __KERNEL__ -/** * atomic_read - read atomic variable * @v: pointer of type atomic_t
@@ -56,52 +140,6 @@#include <linux/irqflags.h> -/** -* atomic_add_return - add integer to atomic variable -* @i: integer value to add -* @v: pointer of type atomic_t -* -* Atomically adds @i to @v and returns the result -*/ -#ifndef atomic_add_return -static inline int atomic_add_return (int i,atomic_t *v) -{ -unsigned long flags; -int temp; - -raw_local_irq_save (flags);/* Don't trace it in an irqsoff handler */ - temp = v->counter; - temp += i; - v->counter = temp; -raw_local_irq_restore (flags); - -return temp; -} -#endif - -/** -* atomic_sub_return - subtract integer from atomic variable -* @i: integer value to subtract -* @v: pointer of type atomic_t -* -* Atomically subtracts @i from @v and returns the result -*/ -#ifndef atomic_sub_return -static inline int atomic_sub_return (int i,atomic_t *v) -{ -unsigned long flags; -int temp; - -raw_local_irq_save (flags);/* Don't trace it in an irqsoff handler */ - temp = v->counter; - temp -= i; - v->counter = temp; -raw_local_irq_restore (flags); - -return temp; -} -#endif -static inline int atomic_add_negative (int i,atomic_t *v) {return atomic_add_return (i, v) <0 ;
@@ -139,49 +177,11 @@ static inline void atomic_dec(atomic_t *v)static inline int __atomic_add_unless (atomic_t *v,int a,int u) { -int c, old; - c =atomic_read (v); -while (c != u && (old =atomic_cmpxchg (v, c, c + a)) != c) - c = old; -return c; -} - -/** -* atomic_clear_mask - Atomically clear bits in atomic variable -* @mask: Mask of the bits to be cleared -* @v: pointer of type atomic_t -* -* Atomically clears the bits set in @mask from @v -*/ -#ifndef atomic_clear_mask -static inline void atomic_clear_mask (unsigned long mask,atomic_t *v) -{ -unsigned long flags; - - mask = ~mask; -raw_local_irq_save (flags);/* Don't trace it in a irqsoff handler */ - v->counter &= mask; -raw_local_irq_restore (flags); +int c, old; + c =atomic_read (v); +while (c != u && (old =atomic_cmpxchg (v, c, c + a)) != c) + c = old; +return c; } -#endif - -/** -* atomic_set_mask - Atomically set bits in atomic variable -* @mask: Mask of the bits to be set -* @v: pointer of type atomic_t -* -* Atomically sets the bits set in @mask in @v -*/ -#ifndef atomic_set_mask -static inline void atomic_set_mask (unsigned int mask,atomic_t *v) -{ -unsigned long flags; - -raw_local_irq_save (flags);/* Don't trace it in a irqsoff handler */ - v->counter |= mask; -raw_local_irq_restore (flags); -} -#endif -#endif /* __KERNEL__ */ #endif /* __ASM_GENERIC_ATOMIC_H */
include/asm-generic/atomic64.h +16 -4
@@ -20,10 +20,22 @@ typedef struct {extern long long atomic64_read (const atomic64_t *v);extern void atomic64_set (atomic64_t *v,long long i); -extern void atomic64_add (long long a,atomic64_t *v); -extern long long atomic64_add_return (long long a,atomic64_t *v); -extern void atomic64_sub (long long a,atomic64_t *v); -extern long long atomic64_sub_return (long long a,atomic64_t *v); + +#define ATOMIC64_OP (op) \ +extern void atomic64_##op(long long a, atomic64_t *v); + +#define ATOMIC64_OP_RETURN (op) \ +extern long long atomic64_##op##_return(long long a, atomic64_t *v); + +#define ATOMIC64_OPS (op) ATOMIC64_OP(op) ATOMIC64_OP_RETURN(op) + +ATOMIC64_OPS(add) +ATOMIC64_OPS(sub) + +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OP +extern long long atomic64_dec_if_positive (atomic64_t *v);extern long long atomic64_cmpxchg (atomic64_t *v,long long o,long long n);extern long long atomic64_xchg (atomic64_t *v,long long new);
lib/atomic64.c +36 -47
89 lines not on this page · raw message
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,parisc: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,parisc: Fold atomic_ops
Commit-ID: 15e3f6d782fc6ff7e004b40642ad895b91ae78bf Gitweb: http://git.kernel.org/tip/15e3f6d782fc6ff7e004b40642ad895b91ae78bf Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 26 Mar 2014 18:04:44 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:11 +0200
locking,arch,parisc: Fold atomic_ops
OK, no LoC saved in this case because sub was defined in terms of add. Still do it because this also prepares for easy addition of new ops.
--- arch/parisc/include/asm/atomic.h | 113 ++++++++++++++++++++++++--------------- 1 file changed, 69 insertions(+), 44 deletions(-)
arch/parisc/include/asm/atomic.h +69 -44
@@ -55,24 +55,7 @@ extern arch_spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] __lock_aligned;* are atomic, so a reader never sees inconsistent values. */ -/* It's possible to reduce all atomic operations to either -* __atomic_add_return, atomic_set and atomic_read (the latter -* is there only for consistency). -*/ - -static __inline__int __atomic_add_return (int i,atomic_t *v) -{ -int ret; -unsigned long flags; -_atomic_spin_lock_irqsave (v, flags); - - ret = (v->counter += i); - -_atomic_spin_unlock_irqrestore (v, flags); -return ret; -} - -static __inline__void atomic_set (atomic_t *v,int i) +static __inline__void atomic_set (atomic_t *v,int i) {unsigned long flags;_atomic_spin_lock_irqsave (v, flags);
@@ -115,16 +98,43 @@ static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)return c; } +#define ATOMIC_OP (op, c_op) \ +static __inline__ void atomic_##op(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + \ + _atomic_spin_lock_irqsave(v, flags); \ + v->counter c_op i; \ + _atomic_spin_unlock_irqrestore(v, flags); \ +} \ + +#define ATOMIC_OP_RETURN (op, c_op) \ +static __inline__ int atomic_##op##_return(int i, atomic_t *v) \ +{ \ + unsigned long flags; \ + int ret; \ + \ + _atomic_spin_lock_irqsave(v, flags); \ + ret = (v->counter c_op i); \ + _atomic_spin_unlock_irqrestore(v, flags); \ + \ + return ret; \ +} + +#define ATOMIC_OPS (op, c_op) ATOMIC_OP(op, c_op) ATOMIC_OP_RETURN(op, c_op) + +ATOMIC_OPS(add, +=) +ATOMIC_OPS(sub, -=) + +#undef ATOMIC_OPS +#undef ATOMIC_OP_RETURN +#undef ATOMIC_OP -#define atomic_add (i,v) ((void)(__atomic_add_return( (i),(v)))) -#define atomic_sub (i,v) ((void)(__atomic_add_return(-((int) (i)),(v)))) -#define atomic_inc (v) ((void)(__atomic_add_return( 1,(v)))) -#define atomic_dec (v) ((void)(__atomic_add_return( -1,(v)))) +#define atomic_inc(v) (atomic_add( 1,(v))) +#define atomic_dec(v) (atomic_add( -1,(v))) -#define atomic_add_return (i,v) (__atomic_add_return( (i),(v))) -#define atomic_sub_return (i,v) (__atomic_add_return(-(i),(v))) -#define atomic_inc_return (v) (__atomic_add_return( 1,(v))) -#define atomic_dec_return (v) (__atomic_add_return( -1,(v))) +#define atomic_inc_return (v) (atomic_add_return( 1,(v))) +#define atomic_dec_return (v) (atomic_add_return( -1,(v)))#define atomic_add_negative (a, v) (atomic_add_return((a), (v)) < 0)
@@ -148,18 +158,37 @@ static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)#define ATOMIC64_INIT (i) { (i) } -static __inline__s64 -__atomic64_add_return (s64 i,atomic64_t *v) -{ -s64 ret; -unsigned long flags; -_atomic_spin_lock_irqsave (v, flags); +#define ATOMIC64_OP (op, c_op) \ +static __inline__ void atomic64_##op(s64 i, atomic64_t *v) \ +{ \ + unsigned long flags; \ + \ + _atomic_spin_lock_irqsave(v, flags); \ + v->counter c_op i; \ + _atomic_spin_unlock_irqrestore(v, flags); \ +} \ + +#define ATOMIC64_OP_RETURN (op, c_op) \ +static __inline__ s64 atomic64_##op##_return(s64 i, atomic64_t *v) \ +{ \ + unsigned long flags; \ + s64 ret; \ + \ + _atomic_spin_lock_irqsave(v, flags); \ + ret = (v->counter c_op i); \ + _atomic_spin_unlock_irqrestore(v, flags); \ + \ + return ret; \ +} - ret = (v->counter += i); +#define ATOMIC64_OPS (op, c_op) ATOMIC64_OP(op, c_op) ATOMIC64_OP_RETURN(op, c_op) -_atomic_spin_unlock_irqrestore (v, flags); -return ret; -} +ATOMIC64_OPS(add, +=) +ATOMIC64_OPS(sub, -=) + +#undef ATOMIC64_OPS +#undef ATOMIC64_OP_RETURN +#undef ATOMIC64_OPstatic __inline__void atomic64_set (atomic64_t *v,s64 i)
@@ -178,15 +207,11 @@ atomic64_read(const atomic64_t *v)return (*(volatile long *)&(v)->counter); } -#define atomic64_add (i,v) ((void)(__atomic64_add_return( ((s64)(i)),(v)))) -#define atomic64_sub (i,v) ((void)(__atomic64_add_return(-((s64)(i)),(v)))) -#define atomic64_inc (v) ((void)(__atomic64_add_return( 1,(v)))) -#define atomic64_dec (v) ((void)(__atomic64_add_return( -1,(v)))) +#define atomic64_inc (v) (atomic64_add( 1,(v))) +#define atomic64_dec (v) (atomic64_add( -1,(v))) -#define atomic64_add_return (i,v) (__atomic64_add_return( ((s64)(i)),(v))) -#define atomic64_sub_return (i,v) (__atomic64_add_return(-((s64)(i)),(v))) -#define atomic64_inc_return (v) (__atomic64_add_return( 1,(v))) -#define atomic64_dec_return (v) (__atomic64_add_return( -1,(v))) +#define atomic64_inc_return (v) (atomic64_add_return( 1,(v))) +#define atomic64_dec_return (v) (atomic64_add_return( -1,(v)))#define atomic64_add_negative (a, v) (atomic64_add_return((a), (v)) < 0)
tip-bot for Peter Zijlstra
merge bot[tip:locking/arch] locking,arch,ia64: Fold atomic_ops
In reply to Peter Zijlstralocking,arch,ia64: Fold atomic_ops
Commit-ID: 08be2dab191431f23f5f98ba2db76513d0d853e7 Gitweb: http://git.kernel.org/tip/08be2dab191431f23f5f98ba2db76513d0d853e7 Author: Peter Zijlstra <[email protected]> AuthorDate: Sun, 23 Mar 2014 18:20:30 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:07 +0200
locking,arch,ia64: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
--- arch/ia64/include/asm/atomic.h | 188 +++++++++++++++++++---------------------- 1 file changed, 86 insertions(+), 102 deletions(-)
arch/ia64/include/asm/atomic.h +86 -102
@@ -27,62 +27,94 @@#define atomic_set (v,i) (((v)->counter) = (i))#define atomic64_set (v,i) (((v)->counter) = (i)) -static __inline__int -ia64_atomic_add (int i,atomic_t *v) -{ -__s32 old, new; -CMPXCHG_BUGCHECK_DECL - - do { -CMPXCHG_BUGCHECK (v); - old =atomic_read (v); - new = old + i; - }while (ia64_cmpxchg (acq, v, old, new,sizeof (atomic_t)) != old); -return new; +#define ATOMIC_OP (op, c_op) \ +static __inline__ int \ +ia64_atomic_##op (int i, atomic_t *v) \ +{ \ + __s32 old, new; \ + CMPXCHG_BUGCHECK_DECL \ + \ + do { \ + CMPXCHG_BUGCHECK(v); \ + old = atomic_read(v); \ + new = old c_op i; \ + } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old); \ + return new; \ } -static __inline__long -ia64_atomic64_add (__s64 i,atomic64_t *v) -{ -__s64 old, new; -CMPXCHG_BUGCHECK_DECL - - do { -CMPXCHG_BUGCHECK (v); - old =atomic64_read (v); - new = old + i; - }while (ia64_cmpxchg (acq, v, old, new,sizeof (atomic64_t)) != old); -return new; -} +ATOMIC_OP(add, +) +ATOMIC_OP(sub, -) -static __inline__int -ia64_atomic_sub (int i,atomic_t *v) -{ -__s32 old, new; -CMPXCHG_BUGCHECK_DECL - - do { -CMPXCHG_BUGCHECK (v); - old =atomic_read (v); - new = old - i; - }while (ia64_cmpxchg (acq, v, old, new,sizeof (atomic_t)) != old); -return new; -} +#undef ATOMIC_OP -static __inline__long -ia64_atomic64_sub (__s64 i,atomic64_t *v) -{ -__s64 old, new; -CMPXCHG_BUGCHECK_DECL - - do { -CMPXCHG_BUGCHECK (v); - old =atomic64_read (v); - new = old - i; - }while (ia64_cmpxchg (acq, v, old, new,sizeof (atomic64_t)) != old); -return new; +#define atomic_add_return(i,v) \ +({ \ +int __ia64_aar_i = (i); \ + (__builtin_constant_p (i) \ + && ( (__ia64_aar_i ==1 ) || (__ia64_aar_i ==4 ) \ + || (__ia64_aar_i ==8 ) || (__ia64_aar_i ==16 ) \ + || (__ia64_aar_i ==-1 ) || (__ia64_aar_i ==-4 ) \ + || (__ia64_aar_i ==-8 ) || (__ia64_aar_i ==-16 ))) \ + ?ia64_fetch_and_add (__ia64_aar_i, &(v)->counter) \ + :ia64_atomic_add (__ia64_aar_i, v); \ +}) + +#define atomic_sub_return(i,v) \ +({ \ +int __ia64_asr_i = (i); \ + (__builtin_constant_p (i) \ + && ( (__ia64_asr_i ==1 ) || (__ia64_asr_i ==4 ) \ + || (__ia64_asr_i ==8 ) || (__ia64_asr_i ==16 ) \ + || (__ia64_asr_i ==-1 ) || (__ia64_asr_i ==-4 ) \ + || (__ia64_asr_i ==-8 ) || (__ia64_asr_i ==-16 ))) \ + ?ia64_fetch_and_add (-__ia64_asr_i, &(v)->counter) \ + :ia64_atomic_sub (__ia64_asr_i, v); \ +}) + +#define ATOMIC64_OP (op, c_op) \ +static __inline__ long \ +ia64_atomic64_##op (__s64 i, atomic64_t *v) \ +{ \ + __s64 old, new; \ + CMPXCHG_BUGCHECK_DECL \ + \ + do { \ + CMPXCHG_BUGCHECK(v); \ + old = atomic64_read(v); \ + new = old c_op i; \ + } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old); \ + return new; \ } +ATOMIC64_OP(add, +) +ATOMIC64_OP(sub, -) + +#undef ATOMIC64_OP + +#define atomic64_add_return(i,v) \ +({ \ +long __ia64_aar_i = (i); \ + (__builtin_constant_p (i) \ + && ( (__ia64_aar_i ==1 ) || (__ia64_aar_i ==4 ) \ + || (__ia64_aar_i ==8 ) || (__ia64_aar_i ==16 ) \ + || (__ia64_aar_i ==-1 ) || (__ia64_aar_i ==-4 ) \ + || (__ia64_aar_i ==-8 ) || (__ia64_aar_i ==-16 ))) \ + ?ia64_fetch_and_add (__ia64_aar_i, &(v)->counter) \ + :ia64_atomic64_add (__ia64_aar_i, v); \ +}) + +#define atomic64_sub_return(i,v) \ +({ \ +long __ia64_asr_i = (i); \ + (__builtin_constant_p (i) \ + && ( (__ia64_asr_i ==1 ) || (__ia64_asr_i ==4 ) \ + || (__ia64_asr_i ==8 ) || (__ia64_asr_i ==16 ) \ + || (__ia64_asr_i ==-1 ) || (__ia64_asr_i ==-4 ) \ + || (__ia64_asr_i ==-8 ) || (__ia64_asr_i ==-16 ))) \ + ?ia64_fetch_and_add (-__ia64_asr_i, &(v)->counter) \ + :ia64_atomic64_sub (__ia64_asr_i, v); \ +}) +#define atomic_cmpxchg (v, old, new) (cmpxchg(&((v)->counter), old, new))#define atomic_xchg (v, new) (xchg(&((v)->counter), new))
@@ -123,30 +155,6 @@ static __inline__ long atomic64_add_unless(atomic64_t *v, long a, long u)#define atomic64_inc_not_zero (v) atomic64_add_unless((v), 1, 0) -#define atomic_add_return (i,v) \ -({ \ - int __ia64_aar_i = (i); \ - (__builtin_constant_p(i) \ - && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \ - || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \ - || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \ - || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \ - ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \ - : ia64_atomic_add(__ia64_aar_i, v); \ -}) - -#define atomic64_add_return (i,v) \ -({ \ - long __ia64_aar_i = (i); \ - (__builtin_constant_p(i) \ - && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \ - || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \ - || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \ - || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \ - ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \ - : ia64_atomic64_add(__ia64_aar_i, v); \ -}) -/* * Atomically add I to V and return TRUE if the resulting value is * negative.
@@ -163,30 +171,6 @@ atomic64_add_negative (__s64 i, atomic64_t *v)return atomic64_add_return (i, v) <0 ; } -#define atomic_sub_return (i,v) \ -({ \ - int __ia64_asr_i = (i); \ - (__builtin_constant_p(i) \ - && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \ - || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \ - || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \ - || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \ - ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \ - : ia64_atomic_sub(__ia64_asr_i, v); \ -}) - -#define atomic64_sub_return (i,v) \ -({ \ - long __ia64_asr_i = (i); \ - (__builtin_constant_p(i) \ - && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \ - || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \ - || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \ - || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \ - ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \ - : ia64_atomic64_sub(__ia64_asr_i, v); \ -}) -#define atomic_dec_return (v) atomic_sub_return(1, (v))#define atomic_inc_return (v) atomic_add_return(1, (v))#define atomic64_dec_return (v) atomic64_sub_return(1, (v))
@@ -199,13 +183,13 @@ atomic64_add_negative (__s64 i, atomic64_t *v)#define atomic64_dec_and_test (v) (atomic64_sub_return(1, (v)) == 0)#define atomic64_inc_and_test (v) (atomic64_add_return(1, (v)) == 0) -#define atomic_add (i,v) atomic_add_return((i), (v)) -#define atomic_sub (i,v) atomic_sub_return((i), (v)) +#define atomic_add (i,v) (void)atomic_add_return((i), (v)) +#define atomic_sub (i,v) (void)atomic_sub_return((i), (v))#define atomic_inc (v) atomic_add(1, (v))#define atomic_dec (v) atomic_sub(1, (v)) -#define atomic64_add (i,v) atomic64_add_return((i), (v)) -#define atomic64_sub (i,v) atomic64_sub_return((i), (v)) +#define atomic64_add (i,v) (void)atomic64_add_return((i), (v)) +#define atomic64_sub (i,v) (void)atomic64_sub_return((i), (v))#define atomic64_inc (v) atomic64_add(1, (v))#define atomic64_dec (v) atomic64_sub(1, (v))
Hans-Christian Egtvedt
Acked-byRe: [tip:locking/arch] locking,arch,avr32: Fold atomic_ops
In reply to tip-bot for Peter ZijlstraQuoted 12 lines · tip-bot for Peter Zijlstra
Commit-ID: d325209b6000dcd13404ee946d2292e15a56718c Gitweb: http://git.kernel.org/tip/d325209b6000dcd13404ee946d2292e15a56718c Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 9 Apr 2014 21:51:29 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:05 +0200locking,arch,avr32: Fold
atomic_opsMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
Quoted 6 lines
This also prepares for easy addition of new ops.
Requires the
asm_opbecause of eor.AVR32 is a bit special in that its ADD/SUB instructions are not symmetric. Its SUB instruction allows for an 21bit immediate.
Fantastic, looks great.
Do you have any plans for merge route? I'll happily do it through the avr32 tree, but if it is part of another series, it makes most sense to do it there.
Quoted 8 lines
Signed-off-by Peter Zijlstra <[email protected]>Cc Hans-Christian Egtvedt <[email protected]>Cc Haavard Skinnemoen <[email protected]>Cc Hans-Christian Egtvedt <[email protected]>Cc Linus Torvalds <[email protected]>Cc Paul E. McKenney <[email protected]>Signed-off-by Ingo Molnar <[email protected]>
Quoted 3 lines
--- arch/avr32/include/asm/atomic.h | 123 ++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 61 deletions(-)
<snipp diff>
Signature · 2 lines
mvh Hans-Christian Egtvedt
Peter Zijlstra
maintainerRe: [tip:locking/arch] locking,arch,avr32: Fold atomic_ops
Quoted 17 lines · Hans-Christian Egtvedt
Quoted 16 lines · tip-bot for Peter Zijlstra
Commit-ID: d325209b6000dcd13404ee946d2292e15a56718c Gitweb: http://git.kernel.org/tip/d325209b6000dcd13404ee946d2292e15a56718c Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 9 Apr 2014 21:51:29 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:05 +0200locking,arch,avr32: Fold
atomic_opsMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires the
asm_opbecause of eor.
Quoted 8 lines
Quoted 2 lines
AVR32 is a bit special in that its ADD/SUB instructions are not symmetric. Its SUB instruction allows for an 21bit immediate.
Fantastic, looks great.
Do you have any plans for merge route? I'll happily do it through the avr32 tree, but if it is part of another series, it makes most sense to do it there.
I was thinking to keep the lot together in tip/locking/arch.
Once that lands, I'll need to clean up and send pass 3, which _hopefully_ will be the last, although I had vague plans for a 4th as well.
Re: [tip:locking/arch] locking,arch,avr32: Fold atomic_ops
Quoted 27 lines · Peter Zijlstra
Quoted 17 lines · Hans-Christian Egtvedt
Quoted 16 lines · tip-bot for Peter Zijlstra
Commit-ID: d325209b6000dcd13404ee946d2292e15a56718c Gitweb: http://git.kernel.org/tip/d325209b6000dcd13404ee946d2292e15a56718c Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 9 Apr 2014 21:51:29 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 14 Aug 2014 12:48:05 +0200locking,arch,avr32: Fold
atomic_opsMany of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC.
This also prepares for easy addition of new ops.
Requires the
asm_opbecause of eor.Quoted 8 lines
Quoted 2 lines
AVR32 is a bit special in that its ADD/SUB instructions are not symmetric. Its SUB instruction allows for an 21bit immediate.
Fantastic, looks great.
Do you have any plans for merge route? I'll happily do it through the avr32 tree, but if it is part of another series, it makes most sense to do it there.
Quoted 5 lines
I was thinking to keep the lot together in tip/locking/arch.
Once that lands, I'll need to clean up and send pass 3, which _hopefully_ will be the last, although I had vague plans for a 4th as well.
OK, you have my ack, so go ahead and merge when you feel fit.
Signature · 2 lines
mvh Hans-Christian Egtvedt
Hi Peter,
Quoted 6 lines · Peter Zijlstra
This series continues the arch atomic rework started with the
smp_mb__interface cleanup.In this series we (mostly) reduce the atomic implementations by eliminating repetition through use of CPP macros.
Quoted 8 lines
A future series will use these macros to implement more atomic ops. With these macros we can end up with more atomic ops while the total LoC still shrinks.
Furthermore, rewrite the asm-generic/atomic implementations to require less and provide more.
This series is compile tested on a number of archs, but only boot tested on
x86_64.
What's the status on this series? I'm currently fleshing out an extension to the atomic API that allows more flexible acquire/release semantics and it doesn't make sense for me to copy-paste a bunch of code when I could build it on top of this instead.
Also, do you have any other patches pending in this area? I've lost track...
Cheers,
Will
Peter Zijlstra
maintainerQuoted 15 lines · Will Deacon
Hi Peter,
Quoted 12 lines · Peter Zijlstra
This series continues the arch atomic rework started with the
smp_mb__interface cleanup.In this series we (mostly) reduce the atomic implementations by eliminating repetition through use of CPP macros.
A future series will use these macros to implement more atomic ops. With these macros we can end up with more atomic ops while the total LoC still shrinks.
Furthermore, rewrite the asm-generic/atomic implementations to require less and provide more.
Quoted 7 lines
Quoted 2 lines
This series is compile tested on a number of archs, but only boot tested on
x86_64.What's the status on this series? I'm currently fleshing out an extension to the atomic API that allows more flexible acquire/release semantics and it doesn't make sense for me to copy-paste a bunch of code when I could build it on top of this instead.
Its in tip/locking/arch and I suppose its headed for the next merge window.
Quoted 1 line
Also, do you have any other patches pending in this area? I've lost track...
I do, I have (hopefully) one more series sweeping all archs introducing:
atomic_{or,and,xor,nand}() and then a few patches killing atomic_{set,clear}_mask(). I just need to find time to write changelogs and compile test them..
After that I need to go write that gcc-ll/sc proposal so we can pretend to do generic composite atomic ops in C.
Oh, and like you pointed out, we need to do an audit of the barrier semantics on some of our atomic functions like add_unless etc.
In other word, plenty to keep entertained.
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Will Deacon
Quoted 14 lines · Peter Zijlstra
This series continues the arch atomic rework started with the
smp_mb__interface cleanup.In this series we (mostly) reduce the atomic implementations by eliminating repetition through use of CPP macros.
A future series will use these macros to implement more atomic ops. With these macros we can end up with more atomic ops while the total LoC still shrinks.
Furthermore, rewrite the asm-generic/atomic implementations to require less and provide more.
This series is compile tested on a number of archs, but only boot tested on
x86_64.
Quoted 7 lines
Quoted 4 lines
What's the status on this series? I'm currently fleshing out an extension to the atomic API that allows more flexible acquire/release semantics and it doesn't make sense for me to copy-paste a bunch of code when I could build it on top of this instead.
Its in tip/locking/arch and I suppose its headed for the next merge window.
Cracking, thanks for that.
Quoted 7 lines
Quoted 1 line
Also, do you have any other patches pending in this area? I've lost track...
I do, I have (hopefully) one more series sweeping all archs introducing:
atomic_{or,and,xor,nand}() and then a few patches killing atomic_{set,clear}_mask(). I just need to find time to write changelogs and compile test them..
Quoted 7 lines
After that I need to go write that gcc-ll/sc proposal so we can pretend to do generic composite atomic ops in C.
Oh, and like you pointed out, we need to do an audit of the barrier semantics on some of our atomic functions like
add_unlessetc.In other word, plenty to keep entertained.
Yup. If you need a hand, please shout and we can probably divide some of this between us.
Cheers,
Will
Quoted 19 lines · Peter Zijlstra
Quoted 18 lines · Will Deacon
Hi Peter,
Quoted 14 lines · Peter Zijlstra
This series continues the arch atomic rework started with the
smp_mb__interface cleanup.In this series we (mostly) reduce the atomic implementations by eliminating repetition through use of CPP macros.
A future series will use these macros to implement more atomic ops. With these macros we can end up with more atomic ops while the total LoC still shrinks.
Furthermore, rewrite the asm-generic/atomic implementations to require less and provide more.
This series is compile tested on a number of archs, but only boot tested on
x86_64.
Quoted 8 lines
Quoted 5 lines
What's the status on this series? I'm currently fleshing out an extension to the atomic API that allows more flexible acquire/release semantics and it doesn't make sense for me to copy-paste a bunch of code when I could build it on top of this instead.
Its in tip/locking/arch and I suppose its headed for the next merge window.
Correct, there are no known regressions with the tip:locking/arch tree so I plan to send those changes to Linus in the merge window.
Thanks,
Ingo