Skip to content

Commit 5284e1b

Browse files
stevecapperlinarowildea01
authored andcommitted
arm64: xchg: Implement cmpxchg_double
The arm64 architecture has the ability to exclusively load and store a pair of registers from an address (ldxp/stxp). Also the SLUB can take advantage of a cmpxchg_double implementation to avoid taking some locks. This patch provides an implementation of cmpxchg_double for 64-bit pairs, and activates the logic required for the SLUB to use these functions (HAVE_ALIGNED_STRUCT_PAGE and HAVE_CMPXCHG_DOUBLE). Also definitions of this_cpu_cmpxchg_8 and this_cpu_cmpxchg_double_8 are wired up to cmpxchg_local and cmpxchg_double_local (rather than the stock implementations that perform non-atomic operations with interrupts disabled) as they are used by the SLUB. On a Juno platform running on only the A57s I get quite a noticeable performance improvement with 5 runs of hackbench on v3.17: Baseline | With Patch -----------------+----------- Mean 119.2312 | 106.1782 StdDev 0.4919 | 0.4494 (times taken to complete `./hackbench 100 process 1000', in seconds) Signed-off-by: Steve Capper <[email protected]> Signed-off-by: Will Deacon <[email protected]>
1 parent 70ddb63 commit 5284e1b

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

arch/arm64/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ config ARM64
3434
select GENERIC_TIME_VSYSCALL
3535
select HANDLE_DOMAIN_IRQ
3636
select HARDIRQS_SW_RESEND
37+
select HAVE_ALIGNED_STRUCT_PAGE if SLUB
3738
select HAVE_ARCH_AUDITSYSCALL
3839
select HAVE_ARCH_JUMP_LABEL
3940
select HAVE_ARCH_KGDB
4041
select HAVE_ARCH_TRACEHOOK
4142
select HAVE_BPF_JIT
4243
select HAVE_C_RECORDMCOUNT
4344
select HAVE_CC_STACKPROTECTOR
45+
select HAVE_CMPXCHG_DOUBLE
4446
select HAVE_DEBUG_BUGVERBOSE
4547
select HAVE_DEBUG_KMEMLEAK
4648
select HAVE_DMA_API_DEBUG

arch/arm64/include/asm/cmpxchg.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define __ASM_CMPXCHG_H
2020

2121
#include <linux/bug.h>
22+
#include <linux/mmdebug.h>
2223

2324
#include <asm/barrier.h>
2425

@@ -152,6 +153,51 @@ static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
152153
return oldval;
153154
}
154155

156+
#define system_has_cmpxchg_double() 1
157+
158+
static inline int __cmpxchg_double(volatile void *ptr1, volatile void *ptr2,
159+
unsigned long old1, unsigned long old2,
160+
unsigned long new1, unsigned long new2, int size)
161+
{
162+
unsigned long loop, lost;
163+
164+
switch (size) {
165+
case 8:
166+
VM_BUG_ON((unsigned long *)ptr2 - (unsigned long *)ptr1 != 1);
167+
do {
168+
asm volatile("// __cmpxchg_double8\n"
169+
" ldxp %0, %1, %2\n"
170+
" eor %0, %0, %3\n"
171+
" eor %1, %1, %4\n"
172+
" orr %1, %0, %1\n"
173+
" mov %w0, #0\n"
174+
" cbnz %1, 1f\n"
175+
" stxp %w0, %5, %6, %2\n"
176+
"1:\n"
177+
: "=&r"(loop), "=&r"(lost), "+Q" (*(u64 *)ptr1)
178+
: "r" (old1), "r"(old2), "r"(new1), "r"(new2));
179+
} while (loop);
180+
break;
181+
default:
182+
BUILD_BUG();
183+
}
184+
185+
return !lost;
186+
}
187+
188+
static inline int __cmpxchg_double_mb(volatile void *ptr1, volatile void *ptr2,
189+
unsigned long old1, unsigned long old2,
190+
unsigned long new1, unsigned long new2, int size)
191+
{
192+
int ret;
193+
194+
smp_mb();
195+
ret = __cmpxchg_double(ptr1, ptr2, old1, old2, new1, new2, size);
196+
smp_mb();
197+
198+
return ret;
199+
}
200+
155201
static inline unsigned long __cmpxchg_mb(volatile void *ptr, unsigned long old,
156202
unsigned long new, int size)
157203
{
@@ -182,6 +228,31 @@ static inline unsigned long __cmpxchg_mb(volatile void *ptr, unsigned long old,
182228
__ret; \
183229
})
184230

231+
#define cmpxchg_double(ptr1, ptr2, o1, o2, n1, n2) \
232+
({\
233+
int __ret;\
234+
__ret = __cmpxchg_double_mb((ptr1), (ptr2), (unsigned long)(o1), \
235+
(unsigned long)(o2), (unsigned long)(n1), \
236+
(unsigned long)(n2), sizeof(*(ptr1)));\
237+
__ret; \
238+
})
239+
240+
#define cmpxchg_double_local(ptr1, ptr2, o1, o2, n1, n2) \
241+
({\
242+
int __ret;\
243+
__ret = __cmpxchg_double((ptr1), (ptr2), (unsigned long)(o1), \
244+
(unsigned long)(o2), (unsigned long)(n1), \
245+
(unsigned long)(n2), sizeof(*(ptr1)));\
246+
__ret; \
247+
})
248+
249+
#define this_cpu_cmpxchg_8(ptr, o, n) \
250+
cmpxchg_local(raw_cpu_ptr(&(ptr)), o, n)
251+
252+
#define this_cpu_cmpxchg_double_8(ptr1, ptr2, o1, o2, n1, n2) \
253+
cmpxchg_double_local(raw_cpu_ptr(&(ptr1)), raw_cpu_ptr(&(ptr2)), \
254+
o1, o2, n1, n2)
255+
185256
#define cmpxchg64(ptr,o,n) cmpxchg((ptr),(o),(n))
186257
#define cmpxchg64_local(ptr,o,n) cmpxchg_local((ptr),(o),(n))
187258

0 commit comments

Comments
 (0)