Skip to content

Commit 4922a3e

Browse files
RISC-V: Move to generic spinlocks
Our existing spinlocks aren't fair and replacing them has been on the TODO list for a long time. This moves to the recently-introduced ticket spinlocks, which are simple enough that they are likely to be correct and fast on the vast majority of extant implementations. This introduces a horrible hack that allows us to split out the spinlock conversion from the rwlock conversion. We have to do the spinlocks first because qrwlock needs fair spinlocks, but we don't want to pollute the asm-generic code to support the generic spinlocks without qrwlocks. Thus we pollute the RISC-V code, but just until the next commit as it's all going away. Reviewed-by: Arnd Bergmann <[email protected]> Reviewed-by: Guo Ren <[email protected]> Tested-by: Heiko Stuebner <[email protected]> Tested-by: Conor Dooley <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 205bf39 commit 4922a3e

File tree

3 files changed

+10
-45
lines changed

3 files changed

+10
-45
lines changed

arch/riscv/include/asm/Kbuild

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ generic-y += early_ioremap.h
33
generic-y += flat.h
44
generic-y += kvm_para.h
55
generic-y += parport.h
6+
generic-y += qrwlock.h
7+
generic-y += qrwlock_types.h
68
generic-y += user.h
79
generic-y += vmlinux.lds.h

arch/riscv/include/asm/spinlock.h

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,13 @@
77
#ifndef _ASM_RISCV_SPINLOCK_H
88
#define _ASM_RISCV_SPINLOCK_H
99

10+
/* This is horible, but the whole file is going away in the next commit. */
11+
#define __ASM_GENERIC_QRWLOCK_H
12+
1013
#include <linux/kernel.h>
1114
#include <asm/current.h>
1215
#include <asm/fence.h>
13-
14-
/*
15-
* Simple spin lock operations. These provide no fairness guarantees.
16-
*/
17-
18-
/* FIXME: Replace this with a ticket lock, like MIPS. */
19-
20-
#define arch_spin_is_locked(x) (READ_ONCE((x)->lock) != 0)
21-
22-
static inline void arch_spin_unlock(arch_spinlock_t *lock)
23-
{
24-
smp_store_release(&lock->lock, 0);
25-
}
26-
27-
static inline int arch_spin_trylock(arch_spinlock_t *lock)
28-
{
29-
int tmp = 1, busy;
30-
31-
__asm__ __volatile__ (
32-
" amoswap.w %0, %2, %1\n"
33-
RISCV_ACQUIRE_BARRIER
34-
: "=r" (busy), "+A" (lock->lock)
35-
: "r" (tmp)
36-
: "memory");
37-
38-
return !busy;
39-
}
40-
41-
static inline void arch_spin_lock(arch_spinlock_t *lock)
42-
{
43-
while (1) {
44-
if (arch_spin_is_locked(lock))
45-
continue;
46-
47-
if (arch_spin_trylock(lock))
48-
break;
49-
}
50-
}
51-
52-
/***********************************************************/
16+
#include <asm-generic/spinlock.h>
5317

5418
static inline void arch_read_lock(arch_rwlock_t *lock)
5519
{

arch/riscv/include/asm/spinlock_types.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
#ifndef _ASM_RISCV_SPINLOCK_TYPES_H
77
#define _ASM_RISCV_SPINLOCK_TYPES_H
88

9+
/* This is horible, but the whole file is going away in the next commit. */
10+
#define __ASM_GENERIC_QRWLOCK_TYPES_H
11+
912
#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
1013
# error "please don't include this file directly"
1114
#endif
1215

13-
typedef struct {
14-
volatile unsigned int lock;
15-
} arch_spinlock_t;
16-
17-
#define __ARCH_SPIN_LOCK_UNLOCKED { 0 }
16+
#include <asm-generic/spinlock_types.h>
1817

1918
typedef struct {
2019
volatile unsigned int lock;

0 commit comments

Comments
 (0)