locking/rwsem: Remove arch specific rwsem files
Subsystems (12)
Waiman Long
reviewerPATCH 1/1 locking/rwsem: Remove arch specific rwsem files
As the generic rwsem-xadd code is using the appropriate acquire and release versions of the atomic operations, the arch specific rwsem.h files will not be that much faster than the generic code as long as the atomic functions are properly implemented. So we can remove those arch specific rwsem.h and stop building asm/rwsem.h to reduce maintenance effort.
Currently, only x86, alpha and ia64 have implemented architecture specific fast paths. I don't have access to alpha and ia64 systems for testing, but they are legacy systems that are not likely to be updated to the latest kernel anyway.
By using a rwsem microbenchmark, the total locking rates on a 4-socket 56-core 112-thread x86-64 system before and after the patch were as follows (mixed means equal # of read and write locks):
Before Patch After Patch
# of Threads wlock rlock mixed wlock rlock mixed
------------ ----- ----- ----- ----- ----- -----
1 27,373 29,409 28,170 28,773 30,164 29,276
2 7,697 14,922 1,703 7,435 15,167 1,729
4 6,987 14,285 1,490 7,181 14,438 1,330
8 6,650 13,652 761 6,918 13,796 718
16 6,434 15,729 713 6,554 16,030 625
32 5,590 15,312 552 6,124 15,344 471
64 5,980 15,478 61 5,668 15,509 58There were some run-to-run variations for the multi-thread tests. For x86-64, using the generic C code fast path seems to be a liitle bit faster than the assembly version especially for read-lock and when lock contention is low. Looking at the assembly version of the fast paths, there are assembly to/from C code wrappers that save and restore all the callee-clobbered registers (7 registers on x86-64). The assembly generated from the generic C code doesn't need to do that. That may explain the slight performance gain here.
The generic asm rwsem.h can also be merged into kernel/locking/rwsem.h as no other code other than those under kernel/locking needs to access the internal rwsem macros and functions.
--- MAINTAINERS | 1 - arch/alpha/include/asm/rwsem.h | 211 ----------------------------------- arch/arm/include/asm/Kbuild | 1 - arch/arm64/include/asm/Kbuild | 1 - arch/hexagon/include/asm/Kbuild | 1 - arch/ia64/include/asm/rwsem.h | 172 ----------------------------- arch/powerpc/include/asm/Kbuild | 1 - arch/s390/include/asm/Kbuild | 1 - arch/sh/include/asm/Kbuild | 1 - arch/sparc/include/asm/Kbuild | 1 - arch/x86/include/asm/rwsem.h | 237 ---------------------------------------- arch/x86/lib/Makefile | 1 - arch/x86/lib/rwsem.S | 156 -------------------------- arch/xtensa/include/asm/Kbuild | 1 - include/asm-generic/rwsem.h | 140 ------------------------ include/linux/rwsem.h | 4 +- kernel/locking/percpu-rwsem.c | 2 + kernel/locking/rwsem.h | 130 ++++++++++++++++++++++ 18 files changed, 133 insertions(+), 929 deletions(-) delete mode 100644 arch/alpha/include/asm/rwsem.h delete mode 100644 arch/ia64/include/asm/rwsem.h delete mode 100644 arch/x86/include/asm/rwsem.h delete mode 100644 arch/x86/lib/rwsem.S delete mode 100644 include/asm-generic/rwsem.h
MAINTAINERS +0 -1
@@ -8926,7 +8926,6 @@ F: arch/*/include/asm/spinlock*.h F: include/linux/rwlock*.h F: include/linux/mutex*.h F: include/linux/rwsem*.h -F: arch/*/include/asm/rwsem.h F: include/linux/seqlock.h F: lib/locking*.[ch] F: kernel/locking/
arch/alpha/include/asm/rwsem.h deleted +0 -211
211 lines not on this page · raw message
arch/arm/include/asm/Kbuild +0 -1
@@ -12,7 +12,6 @@ generic-y += mm-arch-hooks.h generic-y += msi.h generic-y += parport.h generic-y += preempt.h -generic-y += rwsem.h generic-y += seccomp.h generic-y += segment.h generic-y += serial.h
arch/arm64/include/asm/Kbuild +0 -1
@@ -16,7 +16,6 @@ generic-y += mm-arch-hooks.h generic-y += msi.h generic-y += qrwlock.h generic-y += qspinlock.h -generic-y += rwsem.h generic-y += segment.h generic-y += serial.h generic-y += set_memory.h
arch/hexagon/include/asm/Kbuild +0 -1
@@ -26,7 +26,6 @@ generic-y += mm-arch-hooks.h generic-y += pci.h generic-y += percpu.h generic-y += preempt.h -generic-y += rwsem.h generic-y += sections.h generic-y += segment.h generic-y += serial.h
arch/ia64/include/asm/rwsem.h deleted +0 -172
172 lines not on this page · raw message
arch/powerpc/include/asm/Kbuild +0 -1
@@ -9,6 +9,5 @@ generic-y += irq_work.h generic-y += local64.h generic-y += mcs_spinlock.h generic-y += preempt.h -generic-y += rwsem.h generic-y += vtime.h generic-y += msi.h
arch/s390/include/asm/Kbuild +0 -1
@@ -21,7 +21,6 @@ generic-y += local64.h generic-y += mcs_spinlock.h generic-y += mm-arch-hooks.h generic-y += preempt.h -generic-y += rwsem.h generic-y += trace_clock.h generic-y += unaligned.h generic-y += word-at-a-time.h
arch/sh/include/asm/Kbuild +0 -1
@@ -16,7 +16,6 @@ generic-y += mm-arch-hooks.h generic-y += parport.h generic-y += percpu.h generic-y += preempt.h -generic-y += rwsem.h generic-y += serial.h generic-y += sizes.h generic-y += trace_clock.h
arch/sparc/include/asm/Kbuild +0 -1
@@ -17,7 +17,6 @@ generic-y += mm-arch-hooks.h generic-y += module.h generic-y += msi.h generic-y += preempt.h -generic-y += rwsem.h generic-y += serial.h generic-y += trace_clock.h generic-y += word-at-a-time.h
arch/x86/include/asm/rwsem.h deleted +0 -237
237 lines not on this page · raw message
arch/x86/lib/Makefile +0 -1
@@ -23,7 +23,6 @@ obj-$(CONFIG_SMP) += msr-smp.o cache-smp.o lib-y := delay.o misc.o cmdline.o cpu.o lib-y += usercopy_$(BITS).o usercopy.o getuser.o putuser.o lib-y += memcpy_$(BITS).o -lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o insn-eval.o lib-$(CONFIG_RANDOMIZE_BASE) += kaslr.o lib-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
arch/x86/lib/rwsem.S deleted +0 -156
156 lines not on this page · raw message
arch/xtensa/include/asm/Kbuild +0 -1
@@ -23,7 +23,6 @@ generic-y += mm-arch-hooks.h generic-y += param.h generic-y += percpu.h generic-y += preempt.h -generic-y += rwsem.h generic-y += sections.h generic-y += topology.h generic-y += trace_clock.h
include/asm-generic/rwsem.h deleted +0 -140
140 lines not on this page · raw message
include/linux/rwsem.h +1 -3
@@ -57,15 +57,13 @@ struct rw_semaphore {extern struct rw_semaphore *rwsem_wake (struct rw_semaphore *);extern struct rw_semaphore *rwsem_downgrade_wake (struct rw_semaphore *sem); -/* Include the arch specific part */ -#include <asm/rwsem.h> -/* In all implementations count != 0 means locked */ static inline int rwsem_is_locked (struct rw_semaphore *sem) {return atomic_long_read (&sem->count) !=0 ; } +#define RWSEM_UNLOCKED_VALUE 0L#define __RWSEM_INIT_COUNT (name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)#endif
kernel/locking/percpu-rwsem.c +2 -0
@@ -7,6 +7,8 @@#include <linux/sched.h> #include <linux/errno.h> +#include "rwsem.h" +int __percpu_init_rwsem (struct percpu_rw_semaphore *sem,const char *name,struct lock_class_key *rwsem_key) {
kernel/locking/rwsem.h +130 -0
139 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Waiman Long
reviewerQuoted 36 lines · Waiman Long
As the generic rwsem-xadd code is using the appropriate acquire and release versions of the atomic operations, the arch specific
rwsem.hfiles will not be that much faster than the generic code as long as the atomic functions are properly implemented. So we can remove those arch specificrwsem.hand stop buildingasm/rwsem.hto reduce maintenance effort.Currently, only x86, alpha and ia64 have implemented architecture specific fast paths. I don't have access to alpha and ia64 systems for testing, but they are legacy systems that are not likely to be updated to the latest kernel anyway.
By using a rwsem microbenchmark, the total locking rates on a 4-socket 56-core 112-thread x86-64 system before and after the patch were as follows (mixed means equal # of read and write locks):
Before Patch After Patch
# of Threads wlock rlock mixed wlock rlock mixed ------------ ----- ----- ----- ----- ----- ----- 1 27,373 29,409 28,170 28,773 30,164 29,276 2 7,697 14,922 1,703 7,435 15,167 1,729 4 6,987 14,285 1,490 7,181 14,438 1,330 8 6,650 13,652 761 6,918 13,796 718 16 6,434 15,729 713 6,554 16,030 625 32 5,590 15,312 552 6,124 15,344 471 64 5,980 15,478 61 5,668 15,509 58There were some run-to-run variations for the multi-thread tests. For x86-64, using the generic C code fast path seems to be a liitle bit faster than the assembly version especially for read-lock and when lock contention is low. Looking at the assembly version of the fast paths, there are assembly to/from C code wrappers that save and restore all the callee-clobbered registers (7 registers on x86-64). The assembly generated from the generic C code doesn't need to do that. That may explain the slight performance gain here.
Quoted 5 lines
The generic asm
rwsem.hcan also be merged intokernel/locking/rwsem.has no other code other than those under kernel/locking needs to access the internal rwsem macros and functions.Signed-off-by Waiman Long <[email protected]>
I have decided to break the rwsem patchset that I sent out on last Thursday into 3 parts. This patch is part 0 as it touches a number of arch specific files and so have the widest distribution. I would like to get it merged first. Part 1 will be patches 1-10 (except 4) of my original rwsem patchset. This part moves things around, adds more debugging capability and lays the ground work for the next part. Part 2 will contains the remaining patches which are the real beef of the whole patchset.
Cheers,
Longman
Quoted 43 lines · Waiman Long
Quoted 36 lines · Waiman Long
As the generic rwsem-xadd code is using the appropriate acquire and release versions of the atomic operations, the arch specific
rwsem.hfiles will not be that much faster than the generic code as long as the atomic functions are properly implemented. So we can remove those arch specificrwsem.hand stop buildingasm/rwsem.hto reduce maintenance effort.Currently, only x86, alpha and ia64 have implemented architecture specific fast paths. I don't have access to alpha and ia64 systems for testing, but they are legacy systems that are not likely to be updated to the latest kernel anyway.
By using a rwsem microbenchmark, the total locking rates on a 4-socket 56-core 112-thread x86-64 system before and after the patch were as follows (mixed means equal # of read and write locks):
Before Patch After Patch
# of Threads wlock rlock mixed wlock rlock mixed ------------ ----- ----- ----- ----- ----- ----- 1 27,373 29,409 28,170 28,773 30,164 29,276 2 7,697 14,922 1,703 7,435 15,167 1,729 4 6,987 14,285 1,490 7,181 14,438 1,330 8 6,650 13,652 761 6,918 13,796 718 16 6,434 15,729 713 6,554 16,030 625 32 5,590 15,312 552 6,124 15,344 471 64 5,980 15,478 61 5,668 15,509 58There were some run-to-run variations for the multi-thread tests. For x86-64, using the generic C code fast path seems to be a liitle bit faster than the assembly version especially for read-lock and when lock contention is low. Looking at the assembly version of the fast paths, there are assembly to/from C code wrappers that save and restore all the callee-clobbered registers (7 registers on x86-64). The assembly generated from the generic C code doesn't need to do that. That may explain the slight performance gain here.
Quoted 5 lines
The generic asm
rwsem.hcan also be merged intokernel/locking/rwsem.has no other code other than those under kernel/locking needs to access the internal rwsem macros and functions.Signed-off-by Waiman Long <[email protected]>
Quoted 8 lines
I have decided to break the rwsem patchset that I sent out on last Thursday into 3 parts. This patch is part 0 as it touches a number of arch specific files and so have the widest distribution. I would like to get it merged first. Part 1 will be patches 1-10 (except 4) of my original rwsem patchset. This part moves things around, adds more debugging capability and lays the ground work for the next part. Part 2 will contains the remaining patches which are the real beef of the whole patchset.
Sounds good to me - I've merged this patch, will push it out after testing.
Thanks,
Ingo
Quoted 2 lines · Ingo Molnar
Sounds good to me - I've merged this patch, will push it out after testing.
Based on Peter's feedback I'm delaying this - performance testing on at least one key ll/sc arch would be nice indeed.
Thanks,
Ingo
Quoted 8 lines · Ingo Molnar
Quoted 2 lines · Ingo Molnar
Sounds good to me - I've merged this patch, will push it out after testing.
Based on Peter's feedback I'm delaying this - performance testing on at least one key ll/sc arch would be nice indeed.
Once Waiman has posted a new version, I can take it for a spin on some arm64 boxen if he shares his workload.
Will
Quoted 4 lines · Will Deacon
Quoted 3 lines · Ingo Molnar
* Ingo Molnar <[email protected]> wrote:
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Sounds good to me - I've merged this patch, will push it out after testing.
Based on Peter's feedback I'm delaying this - performance testing on at least one key ll/sc arch would be nice indeed.
Once Waiman has posted a new version, I can take it for a spin on some arm64 boxen if he shares his workload.
Cool, thanks!
Ingo
Waiman Long
reviewerQuoted 10 lines · Ingo Molnar
Quoted 2 lines · Ingo Molnar
Sounds good to me - I've merged this patch, will push it out after testing.
Based on Peter's feedback I'm delaying this - performance testing on at least one key ll/sc arch would be nice indeed.
Thanks,
Ingo
Yes, I will twist the generic code to generate better code.
As I said in the commit log, only x86, ia64 and alpha provide assembly code to replace the generic C code. The ll/sc archs that I have access to (ARM64, ppc) are all using the generic C code anyway. I actually had done some performance measurement on both those platforms and didn't see any performance difference. I didn't include them as they were using generic code before. I will rerun the tests after I twisted the generic C code.
Thanks,
Longman
Peter Zijlstra
maintainerQuoted 54 lines · Waiman Long
kernel/locking/rwsem.h +39
-0@@ -32,6 +32,26 @@# define DEBUG_RWSEMS_WARN_ON (c)#endif +/* +* R/W semaphores originally for PPC using the stuff in lib/rwsem.c. +* Adapted largely from include/asm-i386/rwsem.h +* by Paul Mackerras <[email protected]>. +*/ + +/* +* the semaphore definition +*/ +#ifdef CONFIG_64BIT +# define RWSEM_ACTIVE_MASK 0xffffffffL +#else +# define RWSEM_ACTIVE_MASK 0x0000ffffL +#endif + +#define RWSEM_ACTIVE_BIAS 0x00000001L +#define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1) +#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS +#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS) +#ifdef CONFIG_RWSEM_SPIN_ON_OWNER /* * All writes to owner are protected by WRITE_ONCE() to make sure that @@ -132,3 +152,113 @@ static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem) { }#endif + +#ifdef CONFIG_RWSEM_XCHGADD_ALGORITHM +/* +* lock for reading +*/ +static inline void __down_read (struct rw_semaphore *sem) +{ +if (unlikely (atomic_long_inc_return_acquire (&sem->count) <=0 )) +rwsem_down_read_failed (sem); +} + +static inline int __down_read_killable (struct rw_semaphore *sem) +{ +if (unlikely (atomic_long_inc_return_acquire (&sem->count) <=0 )) { +if (IS_ERR (rwsem_down_read_failed_killable (sem))) +return -EINTR ; + } + +return 0 ;
kernel/locking/rwsem.h static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem) · line 183
Quoted 10 lines
kernel/locking/rwsem.h +10
-0+} + +static inline int __down_read_trylock (struct rw_semaphore *sem) +{ +long tmp; + +while ((tmp =atomic_long_read (&sem->count)) >=0 ) { +if (tmp ==atomic_long_cmpxchg_acquire (&sem->count, tmp, + tmp +RWSEM_ACTIVE_READ_BIAS )) { +return 1 ;
That really wants to be:
if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp, tmp + RWSEM_ACTIVE_READ_BIAS))
Quoted 23 lines
kernel/locking/rwsem.h +23
-0+ } + } +return 0 ; +} + +/* +* lock for writing +*/ +static inline void __down_write (struct rw_semaphore *sem) +{ +long tmp; + + tmp =atomic_long_add_return_acquire (RWSEM_ACTIVE_WRITE_BIAS , + &sem->count); +if (unlikely (tmp !=RWSEM_ACTIVE_WRITE_BIAS )) +rwsem_down_write_failed (sem); +} + +static inline int __down_write_killable (struct rw_semaphore *sem) +{ +long tmp; + + tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS,
kernel/locking/rwsem.h static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem) · line 216
Quoted 10 lines
kernel/locking/rwsem.h +10
-0+ &sem->count); +if (unlikely (tmp !=RWSEM_ACTIVE_WRITE_BIAS )) +if (IS_ERR (rwsem_down_write_failed_killable (sem))) +return -EINTR ; +return 0 ; +} + +static inline int __down_write_trylock (struct rw_semaphore *sem) +{ +long tmp;
tmp = RWSEM_UNLOCKED_VALUE;
kernel/locking/rwsem.h static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem) · line 220
Quoted 4 lines
kernel/locking/rwsem.h +4
-0+ + tmp =atomic_long_cmpxchg_acquire (&sem->count,RWSEM_UNLOCKED_VALUE , +RWSEM_ACTIVE_WRITE_BIAS ); +return tmp ==RWSEM_UNLOCKED_VALUE ;
return atomic_long_try_cmpxchg_acquire(&sem->count, &tmp, RWSEM_ACTIVE_WRITE_BIAS);
Quoted 44 lines
kernel/locking/rwsem.h +44
-0+} + +/* +* unlock after reading +*/ +static inline void __up_read (struct rw_semaphore *sem) +{ +long tmp; + + tmp =atomic_long_dec_return_release (&sem->count); +if (unlikely (tmp <-1 && (tmp &RWSEM_ACTIVE_MASK ) ==0 )) +rwsem_wake (sem); +} + +/* +* unlock after writing +*/ +static inline void __up_write (struct rw_semaphore *sem) +{ +if (unlikely (atomic_long_sub_return_release (RWSEM_ACTIVE_WRITE_BIAS , + &sem->count) <0 )) +rwsem_wake (sem); +} + +/* +* downgrade write lock to read lock +*/ +static inline void __downgrade_write (struct rw_semaphore *sem) +{ +long tmp; + +/* +* When downgrading from exclusive to shared ownership, +* anything inside the write-locked region cannot leak +* into the read side. In contrast, anything in the +* read-locked region is ok to be re-ordered into the +* write side. As such, rely on RELEASE semantics. +*/ + tmp =atomic_long_add_return_release (-RWSEM_WAITING_BIAS , &sem->count); +if (tmp <0 ) +rwsem_downgrade_wake (sem); +} + +#endif /* CONFIG_RWSEM_XCHGADD_ALGORITHM */
Peter Zijlstra
maintainerQuoted 12 lines · Peter Zijlstra
kernel/locking/rwsem.h static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem) · line 183
Quoted 8 lines · Waiman Long
kernel/locking/rwsem.h +8
-0+static inline int __down_read_trylock (struct rw_semaphore *sem) +{ +long tmp; + +while ((tmp =atomic_long_read (&sem->count)) >=0 ) { +if (tmp ==atomic_long_cmpxchg_acquire (&sem->count, tmp, + tmp +RWSEM_ACTIVE_READ_BIAS )) { +return 1 ;That really wants to be:
Quoted 7 lines
if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp, tmp + RWSEM_ACTIVE_READ_BIAS))kernel/locking/rwsem.h static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem) · line 174
Quoted 4 lines
kernel/locking/rwsem.h +4
-0+ } + } +return 0 ; +}
Also, the is the one case where LL/SC can actually do 'better'. Do you have benchmarks for say PowerPC or ARM64 ?
Peter Zijlstra
maintainerQuoted 16 lines · Peter Zijlstra
Quoted 15 lines · Peter Zijlstra
kernel/locking/rwsem.h static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem) · line 183
Quoted 8 lines · Waiman Long
kernel/locking/rwsem.h +8
-0+static inline int __down_read_trylock (struct rw_semaphore *sem) +{ +long tmp; + +while ((tmp =atomic_long_read (&sem->count)) >=0 ) { +if (tmp ==atomic_long_cmpxchg_acquire (&sem->count, tmp, + tmp +RWSEM_ACTIVE_READ_BIAS )) { +return 1 ;That really wants to be:
if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp, tmp + RWSEM_ACTIVE_READ_BIAS))
Quoted 7 lines
Quoted 4 lines
kernel/locking/rwsem.h static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem) · line 174
Quoted 4 lines
kernel/locking/rwsem.h +4
-0+ } + } +return 0 ; +}Also, the is the one case where LL/SC can actually do 'better'. Do you have benchmarks for say PowerPC or ARM64 ?
Ah, I see they already used asm-generic/rwsem.h which has similar code to the above.
Peter Zijlstra
maintainerkernel/locking/rwsem.h static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem) · line 187
Quoted 12 lines · Waiman Long
kernel/locking/rwsem.h +12
-0+static inline int __down_read_trylock (struct rw_semaphore *sem) +{ +long tmp; + +while ((tmp =atomic_long_read (&sem->count)) >=0 ) { +if (tmp ==atomic_long_cmpxchg_acquire (&sem->count, tmp, + tmp +RWSEM_ACTIVE_READ_BIAS )) { +return 1 ; + } + } +return 0 ; +}
So the orignal x86 implementation reads:
static inline bool __down_read_trylock(struct rw_semaphore *sem)
{
long result, tmp;
asm volatile("# beginning __down_read_trylock\n\t"
" mov %[count],%[result]\n\t"
"1:\n\t"
" mov %[result],%[tmp]\n\t"
" add %[inc],%[tmp]\n\t"
" jle 2f\n\t"
LOCK_PREFIX " cmpxchg %[tmp],%[count]\n\t"
" jnz 1b\n\t"
"2:\n\t"
"# ending __down_read_trylock\n\t"
: [count] "+m" (sem->count), [result] "=&a" (result),
[tmp] "=&r" (tmp)
: [inc] "i" (RWSEM_ACTIVE_READ_BIAS)
: "memory", "cc");
return result >= 0;}
you replace that with:
int __down_read_trylock1(unsigned long *l)
{
long tmp;
while ((tmp = READ_ONCE(*l)) >= 0) {
if (tmp == cmpxchg(l, tmp, tmp + 1))
return 1;
}
return 0;}
which generates:
0000000000000000 <__down_read_trylock1>:
0: eb 17 jmp 19 <__down_read_trylock1+0x19> 2: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1) 8: 48 8d 4a 01 lea 0x1(%rdx),%rcx c: 48 89 d0 mov %rdx,%rax f: f0 48 0f b1 0f lock cmpxchg %rcx,(%rdi) 14: 48 39 c2 cmp %rax,%rdx 17: 74 0f je 28 <__down_read_trylock1+0x28> 19: 48 8b 17 mov (%rdi),%rdx 1c: 48 85 d2 test %rdx,%rdx 1f: 79 e7 jns 8 <__down_read_trylock1+0x8> 21: 31 c0 xor %eax,%eax 23: c3 retq 24: 0f 1f 40 00 nopl 0x0(%rax) 28: b8 01 00 00 00 mov $0x1,%eax 2d: c3 retq
Which is clearly worse. Now we can write that as:
int __down_read_trylock2(unsigned long *l)
{
long tmp = READ_ONCE(*l);
while (tmp >= 0) {
if (try_cmpxchg(l, &tmp, tmp + 1))
return 1;
}
return 0;}
which generates:
0000000000000030 <__down_read_trylock2>:
30: 48 8b 07 mov (%rdi),%rax 33: 48 85 c0 test %rax,%rax 36: 78 18 js 50 <__down_read_trylock2+0x20> 38: 48 8d 50 01 lea 0x1(%rax),%rdx 3c: f0 48 0f b1 17 lock cmpxchg %rdx,(%rdi) 41: 75 f0 jne 33 <__down_read_trylock2+0x3> 43: b8 01 00 00 00 mov $0x1,%eax 48: c3 retq 49: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 50: 31 c0 xor %eax,%eax 52: c3 retq
Which is a lot better; but not quite there yet.
I've tried quite a bit, but I can't seem to get GCC to generate the:
add $1,%rdx jle
required; stuff like:
new = old + 1; if (new <= 0)
generates:
lea 0x1(%rax),%rdx test %rdx, %rdx jle
Ah well, have fun :-)
typedef unsigned char u8; typedef unsigned short u16; typedef unsigned int u32; typedef unsigned long long u64; typedef signed char s8; typedef signed short s16; typedef signed int s32; typedef signed long long s64; typedef _Bool bool;
# define CC_SET(c) "\n\t/* output condition code " #c "*/\n" # define CC_OUT(c) "=@cc" #c
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
extern void __cmpxchg_wrong_size(void);
#define __raw_cmpxchg(ptr, old, new, size, lock) \
({ \
__typeof__(*(ptr)) __ret; \
__typeof__(*(ptr)) __old = (old); \
__typeof__(*(ptr)) __new = (new); \
switch (size) { \
case 1: \
{ \
volatile u8 *__ptr = (volatile u8 *)(ptr); \
asm volatile(lock "cmpxchgb %2,%1" \
: "=a" (__ret), "+m" (*__ptr) \
: "q" (__new), "0" (__old) \
: "memory"); \
break; \
} \
case 2: \
{ \
volatile u16 *__ptr = (volatile u16 *)(ptr); \
asm volatile(lock "cmpxchgw %2,%1" \
: "=a" (__ret), "+m" (*__ptr) \
: "r" (__new), "0" (__old) \
: "memory"); \
break; \
} \
case 4: \
{ \
volatile u32 *__ptr = (volatile u32 *)(ptr); \
asm volatile(lock "cmpxchgl %2,%1" \
: "=a" (__ret), "+m" (*__ptr) \
: "r" (__new), "0" (__old) \
: "memory"); \
break; \
} \
case 8: \
{ \
volatile u64 *__ptr = (volatile u64 *)(ptr); \
asm volatile(lock "cmpxchgq %2,%1" \
: "=a" (__ret), "+m" (*__ptr) \
: "r" (__new), "0" (__old) \
: "memory"); \
break; \
} \
default: \
__cmpxchg_wrong_size(); \
} \
__ret; \
})#define __cmpxchg(ptr, old, new, size) \ __raw_cmpxchg((ptr), (old), (new), (size), LOCK_PREFIX)
#define cmpxchg(ptr, old, new) \ __cmpxchg(ptr, old, new, sizeof(*(ptr)))
#define __raw_try_cmpxchg(_ptr, _pold, _new, size, lock) \
({ \
bool success; \
__typeof__(_ptr) _old = (__typeof__(_ptr))(_pold); \
__typeof__(*(_ptr)) __old = *_old; \
__typeof__(*(_ptr)) __new = (_new); \
switch (size) { \
case 1: \
{ \
volatile u8 *__ptr = (volatile u8 *)(_ptr); \
asm volatile(lock "cmpxchgb %[new], %[ptr]" \
CC_SET(z) \
: CC_OUT(z) (success), \
[ptr] "+m" (*__ptr), \
[old] "+a" (__old) \
: [new] "q" (__new) \
: "memory"); \
break; \
} \
case 2: \
{ \
volatile u16 *__ptr = (volatile u16 *)(_ptr); \
asm volatile(lock "cmpxchgw %[new], %[ptr]" \
CC_SET(z) \
: CC_OUT(z) (success), \
[ptr] "+m" (*__ptr), \
[old] "+a" (__old) \
: [new] "r" (__new) \
: "memory"); \
break; \
} \
case 4: \
{ \
volatile u32 *__ptr = (volatile u32 *)(_ptr); \
asm volatile(lock "cmpxchgl %[new], %[ptr]" \
CC_SET(z) \
: CC_OUT(z) (success), \
[ptr] "+m" (*__ptr), \
[old] "+a" (__old) \
: [new] "r" (__new) \
: "memory"); \
break; \
} \
case 8: \
{ \
volatile u64 *__ptr = (volatile u64 *)(_ptr); \
asm volatile(lock "cmpxchgq %[new], %[ptr]" \
CC_SET(z) \
: CC_OUT(z) (success), \
[ptr] "+m" (*__ptr), \
[old] "+a" (__old) \
: [new] "r" (__new) \
: "memory"); \
break; \
} \
default: \
__cmpxchg_wrong_size(); \
} \
if (unlikely(!success)) \
*_old = __old; \
likely(success); \
})#define LOCK_PREFIX "lock; "
#define __try_cmpxchg(ptr, pold, new, size) \ __raw_try_cmpxchg((ptr), (pold), (new), (size), LOCK_PREFIX)
#define try_cmpxchg(ptr, pold, new) \ __try_cmpxchg((ptr), (pold), (new), sizeof(*(ptr)))
#define READ_ONCE(x) (*(volatile typeof(x) *)(&x))
int __down_read_trylock1(unsigned long *l)
{
long tmp;
while ((tmp = READ_ONCE(*l)) >= 0) {
if (tmp == cmpxchg(l, tmp, tmp + 1))
return 1;
}
return 0;
}int __down_read_trylock2(unsigned long *l)
{
long tmp = READ_ONCE(*l);
while (tmp >= 0) {
if (try_cmpxchg(l, &tmp, tmp + 1))
return 1;
}
return 0;
}int __down_read_trylock3(unsigned long *l)
{
long new, old = READ_ONCE(*l);
for (;;) {
new = old + 1;
if (new <= 0)
return 0;
if (try_cmpxchg(l, &old, new))
return 1;
}
}_______________________________________________
linux-arm-kernel mailing list [email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Waiman Long
reviewerQuoted 39 lines · Peter Zijlstra
Which is clearly worse. Now we can write that as:
int
__down_read_trylock2(unsigned long *l){ long tmp = READ_ONCE(*l); while (tmp >= 0) { if (try_cmpxchg(l, &tmp, tmp + 1)) return 1; } return 0;}
which generates:
0000000000000030 <
__down_read_trylock2>:30: 48 8b 07 mov (%rdi),%rax 33: 48 85 c0 test %rax,%rax 36: 78 18 js 50 <__down_read_trylock2+0x20> 38: 48 8d 50 01 lea 0x1(%rax),%rdx 3c: f0 48 0f b1 17 lock cmpxchg %rdx,(%rdi) 41: 75 f0 jne 33 <__down_read_trylock2+0x3> 43: b8 01 00 00 00 mov $0x1,%eax 48: c3 retq 49: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 50: 31 c0 xor %eax,%eax 52: c3 retqWhich is a lot better; but not quite there yet.
I've tried quite a bit, but I can't seem to get GCC to generate the:
add $1,%rdx jlerequired; stuff like:
Quoted 8 lines
new = old + 1; if (new <= 0)generates:
lea 0x1(%rax),%rdx test %rdx, %rdx jle
Thanks for the suggested code snippet. So you want to replace "lea 0x1(%rax), %rdx" by "add $1,%rdx"?
I think the compiler is doing that so as to use the address generation unit for addition instead of using the ALU. That will leave the ALU available for doing other arithmetic operation in parallel. I don't think it is a good idea to override the compiler and force it to use ALU. So I am not going to try doing that. It is only 1 or 2 more of codes anyway.
Cheers,
Longman
Peter Zijlstra
maintainerQuoted 52 lines · Waiman Long
Quoted 39 lines · Peter Zijlstra
Which is clearly worse. Now we can write that as:
int
__down_read_trylock2(unsigned long *l){ long tmp = READ_ONCE(*l); while (tmp >= 0) { if (try_cmpxchg(l, &tmp, tmp + 1)) return 1; } return 0;}
which generates:
0000000000000030 <
__down_read_trylock2>:30: 48 8b 07 mov (%rdi),%rax 33: 48 85 c0 test %rax,%rax 36: 78 18 js 50 <__down_read_trylock2+0x20> 38: 48 8d 50 01 lea 0x1(%rax),%rdx 3c: f0 48 0f b1 17 lock cmpxchg %rdx,(%rdi) 41: 75 f0 jne 33 <__down_read_trylock2+0x3> 43: b8 01 00 00 00 mov $0x1,%eax 48: c3 retq 49: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 50: 31 c0 xor %eax,%eax 52: c3 retqWhich is a lot better; but not quite there yet.
I've tried quite a bit, but I can't seem to get GCC to generate the:
add $1,%rdx jlerequired; stuff like:
Quoted 8 lines
new = old + 1; if (new <= 0)generates:
lea 0x1(%rax),%rdx test %rdx, %rdx jleThanks for the suggested code snippet. So you want to replace "lea 0x1(%rax), %rdx" by "add $1,%rdx"?
Quoted 6 lines
I think the compiler is doing that so as to use the address generation unit for addition instead of using the ALU. That will leave the ALU available for doing other arithmetic operation in parallel. I don't think it is a good idea to override the compiler and force it to use ALU. So I am not going to try doing that. It is only 1 or 2 more of codes anyway.
Yeah, I was trying to see what I could make it do.. #2 really should be good enough, but you know how it is once you're poking at it :-)