Skip to contentSkip to conversation

riscv: Add qspinlock/qrwlock

PATCH Superseded [email protected] 2 participants 10 messages Mar 28, 2021

Subsystems (8)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

v5 cover riscv: Add qspinlock/qrwlock

From: Guo Ren <[email protected]>

Current riscv is still using baby spinlock implementation. It'll cause fairness and cache line bouncing problems. Many people are involved and pay the efforts to improve it:

- The first version of patch was made in 2019.1:
https://lore.kernel.org/linux-riscv/20190211043829.30096-1-michaeljclark at mac.com/#r

- The second version was made in 2020.11:
https://lore.kernel.org/linux-riscv/1606225437-22948-2-git-send-email-guoren at kernel.org/

- A good discussion at Platform HSC.2021-03-08:
https://drive.google.com/drive/folders/1ooqdnIsYx7XKor5O1XTtM6D1CHp4hc0p

Hope your comments and Tested-by or Co-developed-by or Reviewed-by ...

Let's kick the qspinlock into riscv right now (Also for the architecture which hasn't xchg16 atomic instruction.)

Change V5:
- Fixup #endif comment typo by Waiman
- Remove cmpxchg coding convention patches which will get into a separate patchset later by Arnd's advice
- Try to involve more architectures in the discussion

Change V4:
- Remove custom sub-word xchg implementation
- Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32 in locking/qspinlock

Change V3:
- Coding convention by Peter Zijlstra's advices

Change V2:
- Coding convention in cmpxchg.h
- Re-implement short xchg
- Remove char & cmpxchg implementations

Guo Ren (6):
locking/qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32
csky: Convert custom spinlock/rwlock to generic qspinlock/qrwlock powerpc/qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32
openrisc: qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32
sparc: qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32
xtensa: qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32

Michael Clark (1):
riscv: Convert custom spinlock/rwlock to generic qspinlock/qrwlock

 arch/csky/Kconfig                       |   2 +
 arch/csky/include/asm/Kbuild            |   2 +
 arch/csky/include/asm/spinlock.h        |  82 +--------------
 arch/csky/include/asm/spinlock_types.h  |  16 +--
 arch/openrisc/Kconfig                   |   1 +
 arch/powerpc/Kconfig                    |   1 +
 arch/riscv/Kconfig                      |   3 +
 arch/riscv/include/asm/Kbuild           |   3 +
 arch/riscv/include/asm/spinlock.h       | 126 +-----------------------
 arch/riscv/include/asm/spinlock_types.h |  15 +--
 arch/sparc/Kconfig                      |   1 +
 arch/xtensa/Kconfig                     |   1 +
 kernel/Kconfig.locks                    |   3 +
 kernel/locking/qspinlock.c              |  46 +++++----
 14 files changed, 49 insertions(+), 253 deletions(-)
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH v5 1/7 locking/qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32

From: Guo Ren <[email protected]>

Some architectures don't have sub-word swap atomic instruction, they only have the full word's one.

The sub-word swap only improve the performance when:
NR_CPUS < 16K
* 0- 7: locked byte
* 8: pending
* 9-15: not used
* 16-17: tail index
* 18-31: tail cpu (+1)

The 9-15 bits are wasted to use xchg16 in xchg_tail.

Please let architecture select xchg16/xchg32 to implement xchg_tail.

Cc Peter Zijlstra <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Waiman Long <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Anup Patel <[email protected]>
---
 kernel/Kconfig.locks       |  3 +++
 kernel/locking/qspinlock.c | 46 +++++++++++++++++++++-----------------
 2 files changed, 28 insertions(+), 21 deletions(-)
kernel/Kconfig.locks +3 -0
index 3de8fd11873b..d02f1261f73f 100644
@@ -239,6 +239,9 @@ config LOCK_SPIN_ON_OWNER
 config ARCH_USE_QUEUED_SPINLOCKS
 	bool
 
+config ARCH_USE_QUEUED_SPINLOCKS_XCHG32
+	bool
+
 config QUEUED_SPINLOCKS
 	def_bool y if ARCH_USE_QUEUED_SPINLOCKS
 	depends on SMP
kernel/locking/qspinlock.c +25 -21

64 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#2 lore raw

PATCH v5 2/7 riscv: Convert custom spinlock/rwlock to generic qspinlock/qrwlock

In reply to [email protected]

From: Michael Clark <[email protected]>

Update the RISC-V port to use the generic qspinlock and qrwlock.

This patch requires support for xchg_xtail for full-word which are added by a previous patch:

Guo added select ARCH_USE_QUEUED_SPINLOCKS_XCHG32 in Kconfig

Guo fixed up compile error which made by below include sequence:
+#include <asm/qrwlock.h>
+#include <asm/qspinlock.h>

---
 arch/riscv/Kconfig                      |   3 +
 arch/riscv/include/asm/Kbuild           |   3 +
 arch/riscv/include/asm/spinlock.h       | 126 +-----------------------
 arch/riscv/include/asm/spinlock_types.h |  15 +--
 4 files changed, 11 insertions(+), 136 deletions(-)
arch/riscv/Kconfig +3 -0
index 87d7b52f278f..67cc65ba1ea1 100644
@@ -33,6 +33,9 @@ config RISCV
 	select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
 	select ARCH_WANT_FRAME_POINTERS
 	select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
+	select ARCH_USE_QUEUED_RWLOCKS
+	select ARCH_USE_QUEUED_SPINLOCKS
+	select ARCH_USE_QUEUED_SPINLOCKS_XCHG32
 	select CLONE_BACKWARDS
 	select CLINT_TIMER if !MMU
 	select COMMON_CLK
arch/riscv/include/asm/Kbuild +3 -0
index 445ccc97305a..750c1056b90f 100644
@@ -3,5 +3,8 @@ generic-y += early_ioremap.h
 generic-y += extable.h
 generic-y += flat.h
 generic-y += kvm_para.h
+generic-y += mcs_spinlock.h
+generic-y += qrwlock.h
+generic-y += qspinlock.h
 generic-y += user.h
 generic-y += vmlinux.lds.h
arch/riscv/include/asm/spinlock.h +2 -124

131 lines not on this page · raw message

arch/riscv/include/asm/spinlock_types.h +3 -12
index f398e7638dd6..d033a973f287 100644
@@ -6,20 +6,11 @@
 #ifndef _ASM_RISCV_SPINLOCK_TYPES_H
 #define _ASM_RISCV_SPINLOCK_TYPES_H
 
-#ifndef __LINUX_SPINLOCK_TYPES_H
+#if !defined(__LINUX_SPINLOCK_TYPES_H) && !defined(_ASM_RISCV_SPINLOCK_H)
 # error "please don't include this file directly"
 #endif
 
-typedef struct {
-	volatile unsigned int lock;
-} arch_spinlock_t;
-
-#define __ARCH_SPIN_LOCK_UNLOCKED	{ 0 }
-
-typedef struct {
-	volatile unsigned int lock;
-} arch_rwlock_t;
-
-#define __ARCH_RW_LOCK_UNLOCKED		{ 0 }
+#include <asm-generic/qspinlock_types.h>
+#include <asm-generic/qrwlock_types.h>
 
 #endif /* _ASM_RISCV_SPINLOCK_TYPES_H */
Signature · 1 line
2.17.1
author maintainer
#3 lore raw

PATCH v5 3/7 csky: Convert custom spinlock/rwlock to generic qspinlock/qrwlock

In reply to [email protected]

From: Guo Ren <[email protected]>

Update the C-SKY port to use the generic qspinlock and qrwlock.

C-SKY only support ldex.w/stex.w with word(double word) size & align access. So it must select XCHG32 to let qspinlock only use word atomic xchg_tail.

Cc Waiman Long <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Will Deacon <[email protected]>
Cc Arnd Bergmann <[email protected]>
---
 arch/csky/Kconfig                      |  2 +
 arch/csky/include/asm/Kbuild           |  2 +
 arch/csky/include/asm/spinlock.h       | 82 +-------------------------
 arch/csky/include/asm/spinlock_types.h | 16 +----
 4 files changed, 6 insertions(+), 96 deletions(-)
arch/csky/Kconfig +2 -0
index 34e91224adc3..5910eb6ddde2 100644
@@ -8,6 +8,8 @@ config CSKY
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
 	select ARCH_USE_BUILTIN_BSWAP
 	select ARCH_USE_QUEUED_RWLOCKS
+	select ARCH_USE_QUEUED_SPINLOCKS
+	select ARCH_USE_QUEUED_SPINLOCKS_XCHG32
 	select ARCH_WANT_FRAME_POINTERS if !CPU_CK610
 	select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
 	select COMMON_CLK
arch/csky/include/asm/Kbuild +2 -0
index cc24bb8e539f..2a2d09963bb9 100644
@@ -2,6 +2,8 @@
 generic-y += asm-offsets.h
 generic-y += gpio.h
 generic-y += kvm_para.h
+generic-y += mcs_spinlock.h
 generic-y += qrwlock.h
+generic-y += qspinlock.h
 generic-y += user.h
 generic-y += vmlinux.lds.h
arch/csky/include/asm/spinlock.h +1 -81

88 lines not on this page · raw message

arch/csky/include/asm/spinlock_types.h +1 -15
index 8ff0f6ff3a00..757594760e65 100644
@@ -7,21 +7,7 @@
 # error "please don't include this file directly"
 #endif
 
-#define TICKET_NEXT	16
-
-typedef struct {
-	union {
-		u32 lock;
-		struct __raw_tickets {
-			/* little endian */
-			u16 owner;
-			u16 next;
-		} tickets;
-	};
-} arch_spinlock_t;
-
-#define __ARCH_SPIN_LOCK_UNLOCKED	{ { 0 } }
-
+#include <asm-generic/qspinlock_types.h>
 #include <asm-generic/qrwlock_types.h>
 
 #endif /* __ASM_CSKY_SPINLOCK_TYPES_H */
Signature · 1 line
2.17.1
author maintainer
#4 lore raw

PATCH v5 4/7 powerpc/qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32

In reply to [email protected]

From: Guo Ren <[email protected]>

We don't have native hw xchg16 instruction, so let qspinlock generic code to deal with it.

Using the full-word atomic xchg instructions implement xchg16 has the semantic risk for atomic operations.

This patch cancels the dependency of on qspinlock generic code on architecture's xchg16.

Cc Michael Ellerman <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
---
 arch/powerpc/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/Kconfig +1 -0
index 386ae12d8523..69ec4ade6521 100644
@@ -151,6 +151,7 @@ config PPC
 	select ARCH_USE_CMPXCHG_LOCKREF		if PPC64
 	select ARCH_USE_QUEUED_RWLOCKS		if PPC_QUEUED_SPINLOCKS
 	select ARCH_USE_QUEUED_SPINLOCKS	if PPC_QUEUED_SPINLOCKS
+	select ARCH_USE_QUEUED_SPINLOCKS_XCHG32	if PPC_QUEUED_SPINLOCKS
 	select ARCH_WANT_IPC_PARSE_VERSION
 	select ARCH_WANT_IRQS_OFF_ACTIVATE_MM
 	select ARCH_WANT_LD_ORPHAN_WARN
Signature · 1 line
2.17.1
#5 lore raw

Re: [PATCH v5 4/7] powerpc/qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32

Le 28/03/2021 à 08:30, guoren at kernel.org a écrit :

Quoted 4 lines

From: Guo Ren <[email protected]>

We don't have native hw xchg16 instruction, so let qspinlock generic code to deal with it.

We have lharx/sthcx pair on some versions of powerpc.

See https://patchwork.ozlabs.org/project/linuxppc-dev/patch/20201107032328.2454582-1-npiggin at gmail.com/

Christophe

Quoted 28 lines

Using the full-word atomic xchg instructions implement xchg16 has the semantic risk for atomic operations.

This patch cancels the dependency of on qspinlock generic code on architecture's xchg16.

Cc Michael Ellerman <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
---
  arch/powerpc/Kconfig | 1 +
  1 file changed, 1 insertion(+)
arch/powerpc/Kconfig +1 -0
index 386ae12d8523..69ec4ade6521 100644
@@ -151,6 +151,7 @@ config PPC
  	select ARCH_USE_CMPXCHG_LOCKREF		if PPC64
  	select ARCH_USE_QUEUED_RWLOCKS		if PPC_QUEUED_SPINLOCKS
  	select ARCH_USE_QUEUED_SPINLOCKS	if PPC_QUEUED_SPINLOCKS
+	select ARCH_USE_QUEUED_SPINLOCKS_XCHG32	if PPC_QUEUED_SPINLOCKS
  	select ARCH_WANT_IPC_PARSE_VERSION
  	select ARCH_WANT_IRQS_OFF_ACTIVATE_MM
  	select ARCH_WANT_LD_ORPHAN_WARN
author maintainer
#6 lore raw

Re: [PATCH v5 4/7] powerpc/qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32

Quoted 4 lines · Christophe Leroy

Le 28/03/2021 à 08:30, guoren at kernel.org a écrit :

Quoted 8 lines
Quoted 4 lines

From: Guo Ren <[email protected]>

We don't have native hw xchg16 instruction, so let qspinlock generic code to deal with it.

We have lharx/sthcx pair on some versions of powerpc.

See https://patchwork.ozlabs.org/project/linuxppc-dev/patch/20201107032328.2454582-1-npiggin at gmail.com/

Got it, thx for the information.

Quoted 31 lines

Christophe

Quoted 28 lines

Using the full-word atomic xchg instructions implement xchg16 has the semantic risk for atomic operations.

This patch cancels the dependency of on qspinlock generic code on architecture's xchg16.

Cc Michael Ellerman <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
---
  arch/powerpc/Kconfig | 1 +
  1 file changed, 1 insertion(+)
arch/powerpc/Kconfig +1 -0
index 386ae12d8523..69ec4ade6521 100644
@@ -151,6 +151,7 @@ config PPC
      select ARCH_USE_CMPXCHG_LOCKREF         if PPC64
      select ARCH_USE_QUEUED_RWLOCKS          if PPC_QUEUED_SPINLOCKS
      select ARCH_USE_QUEUED_SPINLOCKS        if PPC_QUEUED_SPINLOCKS
+     select ARCH_USE_QUEUED_SPINLOCKS_XCHG32 if PPC_QUEUED_SPINLOCKS
      select ARCH_WANT_IPC_PARSE_VERSION
      select ARCH_WANT_IRQS_OFF_ACTIVATE_MM
      select ARCH_WANT_LD_ORPHAN_WARN
Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
author maintainer
#7 lore raw

PATCH v5 5/7 openrisc: qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32

In reply to [email protected]

From: Guo Ren <[email protected]>

We don't have native hw xchg16 instruction, so let qspinlock generic code to deal with it.

Using the full-word atomic xchg instructions implement xchg16 has the semantic risk for atomic operations.

This patch cancels the dependency of on qspinlock generic code on architecture's xchg16.

Cc Arnd Bergmann <[email protected]>
Cc Jonas Bonn <[email protected]>
Cc Stefan Kristiansson <[email protected]>
Cc Stafford Horne <[email protected]>
Cc openrisc at lists.librecores.org
---
 arch/openrisc/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/openrisc/Kconfig +1 -0
index 591acc5990dc..b299e409429f 100644
@@ -33,6 +33,7 @@ config OPENRISC
 	select OR1K_PIC
 	select CPU_NO_EFFICIENT_FFS if !OPENRISC_HAVE_INST_FF1
 	select ARCH_USE_QUEUED_SPINLOCKS
+	select ARCH_USE_QUEUED_SPINLOCKS_XCHG32
 	select ARCH_USE_QUEUED_RWLOCKS
 	select OMPIC if SMP
 	select ARCH_WANT_FRAME_POINTERS
Signature · 1 line
2.17.1
author maintainer
#8 lore raw

PATCH v5 6/7 sparc: qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32

In reply to [email protected]

From: Guo Ren <[email protected]>

We don't have native hw xchg16 instruction, so let qspinlock generic code to deal with it.

Using the full-word atomic xchg instructions implement xchg16 has the semantic risk for atomic operations.

This patch cancels the dependency of on qspinlock generic code on architecture's xchg16.

Cc Arnd Bergmann <[email protected]>
Cc David S. Miller <[email protected]>
Cc Rob Gardner <[email protected]>
---
 arch/sparc/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/sparc/Kconfig +1 -0
index 164a5254c91c..1079fe3f058c 100644
@@ -91,6 +91,7 @@ config SPARC64
 	select HAVE_REGS_AND_STACK_ACCESS_API
 	select ARCH_USE_QUEUED_RWLOCKS
 	select ARCH_USE_QUEUED_SPINLOCKS
+	select ARCH_USE_QUEUED_SPINLOCKS_XCHG32
 	select GENERIC_TIME_VSYSCALL
 	select ARCH_CLOCKSOURCE_DATA
 	select ARCH_HAS_PTE_SPECIAL
Signature · 1 line
2.17.1
author maintainer
#9 lore raw

PATCH v5 7/7 xtensa: qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32

In reply to [email protected]

From: Guo Ren <[email protected]>

We don't have native hw xchg16 instruction, so let qspinlock generic code to deal with it.

Using the full-word atomic xchg instructions implement xchg16 has the semantic risk for atomic operations.

This patch cancels the dependency of on qspinlock generic code on architecture's xchg16.

Cc Arnd Bergmann <[email protected]>
Cc Chris Zankel <[email protected]>
Cc Max Filippov <[email protected]>
---
 arch/xtensa/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/xtensa/Kconfig +1 -0
index 9ad6b7b82707..f19d780638f7 100644
@@ -9,6 +9,7 @@ config XTENSA
 	select ARCH_HAS_DMA_SET_UNCACHED if MMU
 	select ARCH_USE_QUEUED_RWLOCKS
 	select ARCH_USE_QUEUED_SPINLOCKS
+	select ARCH_USE_QUEUED_SPINLOCKS_XCHG32
 	select ARCH_WANT_FRAME_POINTERS
 	select ARCH_WANT_IPC_PARSE_VERSION
 	select BUILDTIME_TABLE_SORT
Signature · 1 line
2.17.1