provide asm-generic spinlock
cover provide asm-generic spinlock
Let's introduce a simple generic version that should be sufficient for our purposes.
David Hildenbrand (3):
lib: provide generic spinlock
ppc64: use asm-generic spinlock
x86: use asm-generic spinlock
lib/asm-generic/spinlock.h | 16 +++++++++++++++- lib/ppc64/asm/spinlock.h | 9 ++------- lib/ppc64/spinlock.c | 18 ------------------ lib/x86/asm/spinlock.h | 9 ++------- lib/x86/smp.c | 16 ---------------- powerpc/Makefile.ppc64 | 1 - 6 files changed, 19 insertions(+), 50 deletions(-) delete mode 100644 lib/ppc64/spinlock.c
Signature · 1 line
2.9.3
PATCH 1/3 lib: provide generic spinlock
Let's provide a basic lock implementation that should work on most architectures.
--- lib/asm-generic/spinlock.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
lib/asm-generic/spinlock.h +15 -1
@@ -1,4 +1,18 @@#ifndef _ASM_GENERIC_SPINLOCK_H_#define _ASM_GENERIC_SPINLOCK_H_ -#error need architecture specific asm/spinlock.h + +struct spinlock { +unsigned int v; +}; + +static inline void spin_lock (struct spinlock *lock) +{ +while (!__sync_bool_compare_and_swap (&lock->v,0 ,1 )); +} + +static inline void spin_unlock (struct spinlock *lock) +{ +__sync_bool_compare_and_swap (&lock->v,1 ,0 ); +} +#endif
Signature · 1 line
2.9.3
PATCH 2/3 ppc64: use asm-generic spinlock
In reply to David HildenbrandSince the ppc64 implementation is currently only a defunc dummy implementation, let's replace it by the generic one.
--- lib/ppc64/asm/spinlock.h | 9 ++------- lib/ppc64/spinlock.c | 18 ------------------ powerpc/Makefile.ppc64 | 1 - 3 files changed, 2 insertions(+), 26 deletions(-) delete mode 100644 lib/ppc64/spinlock.c
lib/ppc64/asm/spinlock.h +2 -7
@@ -1,11 +1,6 @@ +#include <asm-generic/spinlock.h> +#ifndef _ASMPPC64_SPINLOCK_H_#define _ASMPPC64_SPINLOCK_H_ -struct spinlock { -int v; -}; - -extern void spin_lock (struct spinlock *lock); -extern void spin_unlock (struct spinlock *lock); -#endif /* _ASMPPC64_SPINLOCK_H_ */
lib/ppc64/spinlock.c deleted +0 -18
@@ -1,18 +0,0 @@ -/* -* ppc64 (dummy) spinlock implementation -* -* This code is free software; you can redistribute it and/or modify it -* under the terms of the GNU Library General Public License version 2. -*/ - -#include <asm/spinlock.h> - -void spin_lock (struct spinlock *lock) -{ - lock->v =1 ; -} - -void spin_unlock (struct spinlock *lock) -{ - lock->v =0 ; -}
powerpc/Makefile.ppc64 +0 -1
@@ -15,7 +15,6 @@ endif cstart.o = $(TEST_DIR)/cstart64.o reloc.o = $(TEST_DIR)/reloc64.o -cflatobjs += lib/ppc64/spinlock.o # ppc64 specific tests tests --
2.9.3
PATCH 3/3 x86: use asm-generic spinlock
In reply to David Hildenbrand--- lib/x86/asm/spinlock.h | 9 ++------- lib/x86/smp.c | 16 ---------------- 2 files changed, 2 insertions(+), 23 deletions(-)
lib/x86/asm/spinlock.h +2 -7
@@ -1,11 +1,6 @@ +#include <asm-generic/spinlock.h> +#ifndef __ASM_SPINLOCK_H#define __ASM_SPINLOCK_H -struct spinlock { -int v; -}; - -void spin_lock (struct spinlock *lock); -void spin_unlock (struct spinlock *lock); -#endif
lib/x86/smp.c +0 -16
@@ -43,22 +43,6 @@ asm (#endif ); -void spin_lock (struct spinlock *lock) -{ -int v =1 ; - -do { - asmvolatile ("xchg %1, %0" :"+m" (lock->v),"+r" (v)); - }while (v); - asmvolatile ("" : : :"memory" ); -} - -void spin_unlock (struct spinlock *lock) -{ - asmvolatile ("" : : :"memory" ); - lock->v =0 ; -} -int cpu_count (void ) {return _cpu_count;
Signature · 1 line
2.9.3
Re: [kvm-unit-tests PATCH v1 2/3] ppc64: use asm-generic spinlock
In reply to David HildenbrandQuoted 16 lines · David Hildenbrand
Since the ppc64 implementation is currently only a defunc dummy implementation, let's replace it by the generic one.
Signed-off-by David Hildenbrand <[email protected]>--- lib/ppc64/asm/spinlock.h | 9 ++------- lib/ppc64/spinlock.c | 18 ------------------ powerpc/Makefile.ppc64 | 1 - 3 files changed, 2 insertions(+), 26 deletions(-) delete mode 100644 lib/ppc64/spinlock.clib/ppc64/asm/spinlock.h +0
-0
lib/ppc64/asm/spinlock.h line 1
Quoted 1 line
lib/ppc64/asm/spinlock.h +1
-0@@ -1,11 +1,6 @@ +#include <asm-generic/spinlock.h>
In case you respin this series: I think it would be nicer to put the #include within the header guards below.
lib/ppc64/asm/spinlock.h line 6
Quoted 11 lines
lib/ppc64/asm/spinlock.h +0
-7#ifndef _ASMPPC64_SPINLOCK_H_#define _ASMPPC64_SPINLOCK_H_ -struct spinlock { -int v; -}; - -extern void spin_lock (struct spinlock *lock); -extern void spin_unlock (struct spinlock *lock); -#endif /* _ASMPPC64_SPINLOCK_H_ */
Re: [kvm-unit-tests PATCH v1 1/3] lib: provide generic spinlock
In reply to David HildenbrandQuoted 23 lines · David Hildenbrand
Let's provide a basic lock implementation that should work on most architectures.
Signed-off-by David Hildenbrand <[email protected]>--- lib/asm-generic/spinlock.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)lib/asm-generic/spinlock.h +6
-1@@ -1,4 +1,18 @@#ifndef _ASM_GENERIC_SPINLOCK_H_#define _ASM_GENERIC_SPINLOCK_H_ -#error need architecture specific asm/spinlock.h + +struct spinlock { +unsigned int v; +}; + +static inline void spin_lock (struct spinlock *lock)
lib/asm-generic/spinlock.h line 18
Quoted 10 lines
lib/asm-generic/spinlock.h +9
-0+{ +while (!__sync_bool_compare_and_swap (&lock->v,0 ,1 )); +} + +static inline void spin_unlock (struct spinlock *lock) +{ +__sync_bool_compare_and_swap (&lock->v,1 ,0 ); +} +#endif
Re: [kvm-unit-tests PATCH v1 1/3] lib: provide generic spinlock
In reply to David Hildenbrand2017-05-12 12:20+0200, David Hildenbrand:
Quoted 20 lines
Let's provide a basic lock implementation that should work on most architectures.
Signed-off-by David Hildenbrand <[email protected]>--- lib/asm-generic/spinlock.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)lib/asm-generic/spinlock.h +4
-1@@ -1,4 +1,18 @@#ifndef _ASM_GENERIC_SPINLOCK_H_#define _ASM_GENERIC_SPINLOCK_H_ -#error need architecture specific asm/spinlock.h + +struct spinlock { +unsigned int v; +};
lib/asm-generic/spinlock.h line 16
Quoted 10 lines
lib/asm-generic/spinlock.h +10
-0+ +static inline void spin_lock (struct spinlock *lock) +{ +while (!__sync_bool_compare_and_swap (&lock->v,0 ,1 )); +} + +static inline void spin_unlock (struct spinlock *lock) +{ +__sync_bool_compare_and_swap (&lock->v,1 ,0 ); +}
x86 would be better with __sync_lock_test_and_set() and __sync_lock_release() as they generate the same code we have now, instead of two locked cmpxchgs.
GCC mentions that some targets might have problems with that, but they seem to fall back to boolean value and compare-and-swap.
Any reason to avoid "while(__sync_lock_test_and_set(&lock->v, 1));"?
Thanks.
Paolo Bonzini
maintainerRe: [kvm-unit-tests PATCH v1 1/3] lib: provide generic spinlock
Quoted 27 lines · Radim Krčmář
2017-05-12 12:20+0200, David Hildenbrand:
Quoted 26 lines
Let's provide a basic lock implementation that should work on most architectures.
Signed-off-by David Hildenbrand <[email protected]>--- lib/asm-generic/spinlock.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)lib/asm-generic/spinlock.h +10
-1@@ -1,4 +1,18 @@#ifndef _ASM_GENERIC_SPINLOCK_H_#define _ASM_GENERIC_SPINLOCK_H_ -#error need architecture specific asm/spinlock.h + +struct spinlock { +unsigned int v; +}; + +static inline void spin_lock (struct spinlock *lock) +{ +while (!__sync_bool_compare_and_swap (&lock->v,0 ,1 )); +} +
Quoted 8 lines
lib/asm-generic/spinlock.h line 16
Quoted 4 lines
lib/asm-generic/spinlock.h +4
-0+static inline void spin_unlock (struct spinlock *lock) +{ +__sync_bool_compare_and_swap (&lock->v,1 ,0 ); +}x86 would be better with
__sync_lock_test_and_set()and__sync_lock_release()as they generate the same code we have now, instead of two locked cmpxchgs.
I agree these are a better match.
Paolo
Quoted 7 lines
GCC mentions that some targets might have problems with that, but they seem to fall back to boolean value and compare-and-swap.
Any reason to avoid "
while(__sync_lock_test_and_set(&lock->v, 1));"?Thanks.
Re: [kvm-unit-tests PATCH v1 1/3] lib: provide generic spinlock
In reply to Radim KrčmářQuoted 27 lines · Radim Krčmář
2017-05-12 12:20+0200, David Hildenbrand:
Quoted 26 lines
Let's provide a basic lock implementation that should work on most architectures.
Signed-off-by David Hildenbrand <[email protected]>--- lib/asm-generic/spinlock.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)lib/asm-generic/spinlock.h +10
-1@@ -1,4 +1,18 @@#ifndef _ASM_GENERIC_SPINLOCK_H_#define _ASM_GENERIC_SPINLOCK_H_ -#error need architecture specific asm/spinlock.h + +struct spinlock { +unsigned int v; +}; + +static inline void spin_lock (struct spinlock *lock) +{ +while (!__sync_bool_compare_and_swap (&lock->v,0 ,1 )); +} +
Quoted 8 lines
lib/asm-generic/spinlock.h line 16
Quoted 4 lines
lib/asm-generic/spinlock.h +4
-0+static inline void spin_unlock (struct spinlock *lock) +{ +__sync_bool_compare_and_swap (&lock->v,1 ,0 ); +}x86 would be better with
__sync_lock_test_and_set()and__sync_lock_release()as they generate the same code we have now, instead of two locked cmpxchgs.
Both should work, however using your pair looks nicer and also seems to work for x86, powerpc and s390x (at least GCC doesn't spit fire).
So I'll resend, thanks!
Quoted 5 lines
GCC mentions that some targets might have problems with that, but they seem to fall back to boolean value and compare-and-swap.
Any reason to avoid "
while(__sync_lock_test_and_set(&lock->v, 1));"?
I haven't found anything speaking against it. There are even multiple articles suggesting to either use __sync_lock_test_and_set or __sync_bool_compare_and_swap in a loop for exactly that purpose.
Quoted 3 lines
Thanks.
Signature · 3 lines
Thanks, David