Skip to content

Commit db38ee8

Browse files
wildea01Russell King
authored andcommitted
ARM: 7983/1: atomics: implement a better __atomic_add_unless for v6+
Looking at perf profiles of multi-threaded hackbench runs, a significant performance hit appears to manifest from the cmpxchg loop used to implement the 32-bit atomic_add_unless function. This can be mitigated by writing a direct implementation of __atomic_add_unless which doesn't require iteration outside of the atomic operation. Signed-off-by: Will Deacon <will.deacon@arm.com> Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
1 parent d98b90e commit db38ee8

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

‎arch/arm/include/asm/atomic.h‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,33 @@ static inline int atomic_cmpxchg(atomic_t *ptr, int old, int new)
141141
return oldval;
142142
}
143143

144+
static inline int __atomic_add_unless(atomic_t *v, int a, int u)
145+
{
146+
int oldval, newval;
147+
unsigned long tmp;
148+
149+
smp_mb();
150+
prefetchw(&v->counter);
151+
152+
__asm__ __volatile__ ("@ atomic_add_unless\n"
153+
"1: ldrex %0, [%4]\n"
154+
" teq %0, %5\n"
155+
" beq 2f\n"
156+
" add %1, %0, %6\n"
157+
" strex %2, %1, [%4]\n"
158+
" teq %2, #0\n"
159+
" bne 1b\n"
160+
"2:"
161+
: "=&r" (oldval), "=&r" (newval), "=&r" (tmp), "+Qo" (v->counter)
162+
: "r" (&v->counter), "r" (u), "r" (a)
163+
: "cc");
164+
165+
if (oldval != u)
166+
smp_mb();
167+
168+
return oldval;
169+
}
170+
144171
#else /* ARM_ARCH_6 */
145172

146173
#ifdef CONFIG_SMP
@@ -189,10 +216,6 @@ static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
189216
return ret;
190217
}
191218

192-
#endif /* __LINUX_ARM_ARCH__ */
193-
194-
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
195-
196219
static inline int __atomic_add_unless(atomic_t *v, int a, int u)
197220
{
198221
int c, old;
@@ -203,6 +226,10 @@ static inline int __atomic_add_unless(atomic_t *v, int a, int u)
203226
return c;
204227
}
205228

229+
#endif /* __LINUX_ARM_ARCH__ */
230+
231+
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
232+
206233
#define atomic_inc(v) atomic_add(1, v)
207234
#define atomic_dec(v) atomic_sub(1, v)
208235

0 commit comments

Comments
 (0)