Skip to contentSkip to conversation

provide asm-generic spinlock

PATCH Applied David Hildenbrand 2 participants 5 messages May 15, 2017 – May 19, 2017 KVM

lore
Share & tools
lore
#0 lore raw

v2 cover provide asm-generic spinlock

Let's introduce a simple generic version that should be sufficient for our purposes.

v1 -> v2:
- use __sync_lock_test_and_set() and __sync_lock_release() -- dropped Thomas' R-b due to the change
- move includes into #define section

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   |  7 +------
 lib/ppc64/spinlock.c       | 18 ------------------
 lib/x86/asm/spinlock.h     |  7 +------
 lib/x86/smp.c              | 16 ----------------
 powerpc/Makefile.ppc64     |  1 -
 6 files changed, 17 insertions(+), 48 deletions(-)
 delete mode 100644 lib/ppc64/spinlock.c
Signature · 1 line
2.9.3
#1 lore raw

PATCH v2 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
index 3141744..31fa510 100644
@@ -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_lock_test_and_set(&lock->v, 1));
+}
+
+static inline void spin_unlock(struct spinlock *lock)
+{
+	__sync_lock_release(&lock->v);
+}
+
 #endif
Signature · 1 line
2.9.3
#2 lore raw

PATCH v2 2/3 ppc64: use asm-generic spinlock

In reply to David Hildenbrand

Since the ppc64 implementation is currently only a defunc dummy implementation, let's replace it by the generic one.

---
 lib/ppc64/asm/spinlock.h |  7 +------
 lib/ppc64/spinlock.c     | 18 ------------------
 powerpc/Makefile.ppc64   |  1 -
 3 files changed, 1 insertion(+), 25 deletions(-)
 delete mode 100644 lib/ppc64/spinlock.c
lib/ppc64/asm/spinlock.h +1 -6
index 002cdb1..f59eed1 100644
@@ -1,11 +1,6 @@
 #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);
+#include <asm-generic/spinlock.h>
 
 #endif /* _ASMPPC64_SPINLOCK_H_ */
lib/ppc64/spinlock.c deleted +0 -18
deleted file mode 100644
index 1b26ee1..0000000
@@ -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
index 3da3a83..17ece66 100644
@@ -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

#3 lore raw

PATCH v2 3/3 x86: use asm-generic spinlock

In reply to David Hildenbrand
---
 lib/x86/asm/spinlock.h |  7 +------
 lib/x86/smp.c          | 16 ----------------
 2 files changed, 1 insertion(+), 22 deletions(-)
lib/x86/asm/spinlock.h +1 -6
index 4b0cb33..692020c 100644
@@ -1,11 +1,6 @@
 #ifndef __ASM_SPINLOCK_H
 #define __ASM_SPINLOCK_H
 
-struct spinlock {
-    int v;
-};
-
-void spin_lock(struct spinlock *lock);
-void spin_unlock(struct spinlock *lock);
+#include <asm-generic/spinlock.h>
 
 #endif
lib/x86/smp.c +0 -16
index 1eb49f2..4bdbeae 100644
@@ -43,22 +43,6 @@ asm (
 #endif
      );
 
-void spin_lock(struct spinlock *lock)
-{
-    int v = 1;
-
-    do {
-	asm volatile ("xchg %1, %0" : "+m"(lock->v), "+r"(v));
-    } while (v);
-    asm volatile ("" : : : "memory");
-}
-
-void spin_unlock(struct spinlock *lock)
-{
-    asm volatile ("" : : : "memory");
-    lock->v = 0;
-}
-
 int cpu_count(void)
 {
     return _cpu_count;
Signature · 1 line
2.9.3