riscv: Add Native/Paravirt/CNA qspinlock support
Subsystems (10)
[email protected]
maintainerv10 cover riscv: Add Native/Paravirt/CNA qspinlock support
From: Guo Ren <[email protected]>
Hello everyone,
I'm back after one year. This is the tenth version of riscv qspinlock.
patch[1 - 8]: Native qspinlock
patch[9 -17]: Paravirt qspinlock
patch[18-19]: Compact NUMA-awared (CNA) qspinlock
This series based on Andrew Jones' pv-time patches and Alex Kogan's CNA qspinlock patches. I merge them into sg2042-master branch, then you could directly try it:
https://github.com/guoren83/linux/tree/qspinlock_v10_pvlock_cna_qspinlock_v15
Use sophgo_mango_ubuntu_defconfig for sg2042 64 cores board.
Native qspinlock
================
This time we've proved the qspinlock on th1520 [1] & sg2042 [2], which gives stability and performance improvement. All T-HEAD processors have a strong LR/SC forward progress guarantee than the requirements of the ISA, which could satisfy the xchg_tail of native_qspinlock. Now, qspinlock has been run with us for more than 1 year, and we have enough confidence to enable it for all the T-HEAD processors. Of causes, we found a livelock problem with the qspinlock lock torture test from the CPU store merge buffer delay mechanism, which caused the queued spinlock becomes a dead ring and RCU warning to come out. We introduce a custom WRITE_ONCE to solve this. Do we need explicit ISA instruction to signal it? Or let hardware handle this.
We've tested the patch on SOPHGO sg2042 & th1520 and passed the stress test on Fedora & Ubuntu & OpenEuler ... Here is the performance comparison between qspinlock and ticket_lock on sg2042 (64 cores):
sysbench test=threads threads=32 yields=100 lock=8 (+13.8%):
queued_spinlock 0.5109/0.00
ticket_spinlock 0.5814/0.00
perf futex/hash (+6.7%):
queued_spinlock 1444393 operations/sec (+- 0.09%) ticket_spinlock 1353215 operations/sec (+- 0.15%)
perf futex/wake-parallel (+8.6%):
queued_spinlock (waking 1/64 threads) in 0.0253 ms (+-2.90%) ticket_spinlock (waking 1/64 threads) in 0.0275 ms (+-3.12%)
perf futex/requeue (+4.2%):
queued_spinlock Requeued 64 of 64 threads in 0.0785 ms (+-0.55%) ticket_spinlock Requeued 64 of 64 threads in 0.0818 ms (+-4.12%)
System Benchmarks (+6.4%)
queued_spinlock:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 628613745.4 53865.8
Double-Precision Whetstone 55.0 182422.8 33167.8
Execl Throughput 43.0 13116.6 3050.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 7762306.2 19601.8
File Copy 256 bufsize 500 maxblocks 1655.0 3417556.8 20649.9
File Copy 4096 bufsize 8000 maxblocks 5800.0 7427995.7 12806.9
Pipe Throughput 12440.0 23058600.5 18535.9
Pipe-based Context Switching 4000.0 2835617.7 7089.0
Process Creation 126.0 12537.3 995.0
Shell Scripts (1 concurrent) 42.4 57057.4 13456.9
Shell Scripts (8 concurrent) 6.0 7367.1 12278.5
System Call Overhead 15000.0 33308301.3 22205.5
========
System Benchmarks Index Score 12426.1 ticket_spinlock:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 626541701.9 53688.2
Double-Precision Whetstone 55.0 181921.0 33076.5
Execl Throughput 43.0 12625.1 2936.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 6553792.9 16550.0
File Copy 256 bufsize 500 maxblocks 1655.0 3189231.6 19270.3
File Copy 4096 bufsize 8000 maxblocks 5800.0 7221277.0 12450.5
Pipe Throughput 12440.0 20594018.7 16554.7
Pipe-based Context Switching 4000.0 2571117.7 6427.8
Process Creation 126.0 10798.4 857.0
Shell Scripts (1 concurrent) 42.4 57227.5 13497.1
Shell Scripts (8 concurrent) 6.0 7329.2 12215.3
System Call Overhead 15000.0 30766778.4 20511.2
========
System Benchmarks Index Score 11670.7The qspinlock has a significant improvement on SOPHGO SG2042 64 cores platform than the ticket_lock.
Paravirt qspinlock
==================
Based on Andrew Jones' "Add skeleton for pv-time support" patches to unify the paravirt framework.
We implemented kvm_kick_cpu/kvm_wait_cpu and add tracepoints to observe the behaviors. Also, introduce a new SBI extension SBI_EXT_PVLOCK (0xAB0401). If the name and number are approved, I will send a formal proposal to the SBI spec.
Compact NUMA-awared (CNA) qspinlock
===================================
Based on Alex Kogan's "Add NUMA-awareness to qspinlock" patches.
The multi sg2042 chips could compose 2/4 NUMA nodes, and these pathes are the preparation for the next test. I hope "CNA v.s. native" could have a better improvement than "qspinlock v.s. ticket_lock." We tested it on sg2042 hardware with "numa_spinlock=on" to ensure the software is bug-free before the next multi-nodes hardware comes out.
Changlog:
V10:
- Using an alternative framework instead of static_key_branch in the asm/spinlock.h.
- Fixup store merge buffer problem, which causes qspinlock lock torture test livelock.
- Add paravirt qspinlock support, include KVM backend
- Add Compact NUMA-awared qspinlock support
V9:
https://lore.kernel.org/linux-riscv/[email protected]/
- Cleanup generic ticket-lock code, (Using smp_mb__after_spinlock as RCsc)
- Add qspinlock and combo-lock for riscv
- Add qspinlock to openrisc
- Use generic header in csky
- Optimize cmpxchg & atomic code
V8:
https://lore.kernel.org/linux-riscv/[email protected]/
- Coding convention ticket fixup
- Move combo spinlock into riscv and simply asm-generic/spinlock.h
- Fixup xchg16 with wrong return value
- Add csky qspinlock
- Add combo & qspinlock & ticket-lock comparison
- Clean up unnecessary riscv acquire and release definitions
- Enable ARCH_INLINE_READ*/WRITE*/SPIN* for riscv & csky
V7:
https://lore.kernel.org/linux-riscv/[email protected]/
- Add combo spinlock (ticket & queued) support
- Rename ticket_spinlock.h
- Remove unnecessary atomic_read in ticket_spin_value_unlocked
V6:
https://lore.kernel.org/linux-riscv/[email protected]/
- Fixup Clang compile problem Reported-by: kernel test robot
- Cleanup asm-generic/spinlock.h
- Remove changelog in patch main comment part, suggested by Conor.Dooley
- Remove "default y if NUMA" in Kconfig
V5:
https://lore.kernel.org/linux-riscv/[email protected]/
- Update comment with RISC-V forward guarantee feature.
- Back to V3 direction and optimize asm code.
V4:
https://lore.kernel.org/linux-riscv/[email protected]/
- Remove custom sub-word xchg implementation
- Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32 in locking/qspinlock
V3:
https://lore.kernel.org/linux-riscv/[email protected]/
- Coding convention by Peter Zijlstra's advices
V2:
https://lore.kernel.org/linux-riscv/[email protected]/
- Coding convention in cmpxchg.h
- Re-implement short xchg
- Remove char & cmpxchg implementations
V1:
https://lore.kernel.org/linux-riscv/[email protected]/
- Using cmpxchg loop to implement sub-word atomic
Guo Ren (19):
asm-generic: ticket-lock: Reuse arch_spinlock_t of qspinlock
asm-generic: ticket-lock: Move into ticket_spinlock.h
riscv: qspinlock: errata: Add ERRATA_THEAD_WRITE_ONCE fixup
riscv: qspinlock: Add basic queued_spinlock support
riscv: qspinlock: Introduce combo spinlock
riscv: qspinlock: Allow force qspinlock from the command line
riscv: qspinlock: errata: Introduce ERRATA_THEAD_QSPINLOCK
riscv: qspinlock: Use new static key for controlling call of
virt_spin_lock()
RISC-V: paravirt: pvqspinlock: Add paravirt qspinlock skeleton
RISC-V: paravirt: pvqspinlock: KVM: Add paravirt qspinlock skeleton
RISC-V: paravirt: pvqspinlock: KVM: Implement
kvm_sbi_ext_pvlock_kick_cpu()
RISC-V: paravirt: pvqspinlock: Add nopvspin kernel parameter
RISC-V: paravirt: pvqspinlock: Remove unnecessary definitions of
cmpxchg & xchg
RISC-V: paravirt: pvqspinlock: Add xchg8 & cmpxchg_small support
RISC-V: paravirt: pvqspinlock: Add SBI implementation
RISC-V: paravirt: pvqspinlock: Add kconfig entry
RISC-V: paravirt: pvqspinlock: Add trace point for pv_kick/wait locking/qspinlock: Move pv_ops into x86 directory locking/qspinlock: riscv: Add Compact NUMA-aware lock support
.../admin-guide/kernel-parameters.txt | 5 +- arch/riscv/Kconfig | 54 ++++ arch/riscv/Kconfig.errata | 32 ++ arch/riscv/errata/thead/errata.c | 44 +++ arch/riscv/include/asm/Kbuild | 2 +- arch/riscv/include/asm/cmpxchg.h | 286 ++++++++++++------ arch/riscv/include/asm/cpufeature.h | 2 + arch/riscv/include/asm/errata_list.h | 33 +- arch/riscv/include/asm/hwcap.h | 1 + arch/riscv/include/asm/kvm_vcpu_sbi.h | 1 + arch/riscv/include/asm/paravirt.h | 20 ++ arch/riscv/include/asm/qspinlock.h | 34 +++ arch/riscv/include/asm/qspinlock_paravirt.h | 7 + arch/riscv/include/asm/rwonce.h | 24 ++ arch/riscv/include/asm/sbi.h | 14 + arch/riscv/include/asm/spinlock.h | 122 ++++++++ arch/riscv/include/asm/vendorid_list.h | 15 + arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kernel/cpufeature.c | 26 ++ arch/riscv/kernel/paravirt.c | 86 ++++++ arch/riscv/kernel/sbi.c | 2 +- arch/riscv/kernel/setup.c | 22 ++ .../kernel/trace_events_filter_paravirt.h | 60 ++++ arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu_sbi.c | 4 + arch/riscv/kvm/vcpu_sbi_pvlock.c | 57 ++++ arch/x86/include/asm/qspinlock.h | 3 +- arch/x86/kernel/alternative.c | 6 +- include/asm-generic/rwonce.h | 2 + include/asm-generic/spinlock.h | 87 +----- include/asm-generic/spinlock_types.h | 12 +- include/asm-generic/ticket_spinlock.h | 103 +++++++ kernel/locking/qspinlock_cna.h | 14 +- 33 files changed, 971 insertions(+), 211 deletions(-) create mode 100644 arch/riscv/include/asm/qspinlock.h create mode 100644 arch/riscv/include/asm/qspinlock_paravirt.h create mode 100644 arch/riscv/include/asm/rwonce.h create mode 100644 arch/riscv/include/asm/spinlock.h create mode 100644 arch/riscv/kernel/trace_events_filter_paravirt.h create mode 100644 arch/riscv/kvm/vcpu_sbi_pvlock.c create mode 100644 include/asm-generic/ticket_spinlock.h
Signature · 1 line
2.36.1
[email protected]
maintainerPATCH v10 1/19 asm-generic: ticket-lock: Reuse arch_spinlock_t of qspinlock
From: Guo Ren <[email protected]>
The arch_spinlock_t of qspinlock has contained the atomic_t val, which satisfies the ticket-lock requirement. Thus, unify the arch_spinlock_t into qspinlock_types.h. This is the preparation for the next combo spinlock.
--- include/asm-generic/spinlock.h | 14 +++++++------- include/asm-generic/spinlock_types.h | 12 ++---------- 2 files changed, 9 insertions(+), 17 deletions(-)
include/asm-generic/spinlock.h +7 -7
@@ -32,7 +32,7 @@ static __always_inline void arch_spin_lock(arch_spinlock_t *lock) { -u32 val =atomic_fetch_add (1 <<16 , lock); +u32 val =atomic_fetch_add (1 <<16 , &lock->val);u16 ticket = val >>16 ;if (ticket == (u16 )val)
@@ -46,31 +46,31 @@ static __always_inline void arch_spin_lock(arch_spinlock_t *lock)* have no outstanding writes due to the atomic_fetch_add() the extra * orderings are free. */ -atomic_cond_read_acquire (lock, ticket == (u16 )VAL ); +atomic_cond_read_acquire (&lock->val, ticket == (u16 )VAL );smp_mb (); } static __always_inline bool arch_spin_trylock(arch_spinlock_t *lock) { -u32 old =atomic_read (lock); +u32 old =atomic_read (&lock->val);if ((old >>16 ) != (old &0xffff ))return false; -return atomic_try_cmpxchg (lock, &old, old + (1 <<16 ));/* SC, for RCsc */ +return atomic_try_cmpxchg (&lock->val, &old, old + (1 <<16 ));/* SC, for RCsc */ } static __always_inline void arch_spin_unlock(arch_spinlock_t *lock) {u16 *ptr = (u16 *)lock +IS_ENABLED (CONFIG_CPU_BIG_ENDIAN ); -u32 val =atomic_read (lock); +u32 val =atomic_read (&lock->val);smp_store_release (ptr, (u16 )val +1 ); } static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock) { -u32 val = lock.counter; +u32 val = lock.val.counter;return ((val >>16 ) == (val &0xffff )); }
@@ -84,7 +84,7 @@ static __always_inline int arch_spin_is_locked(arch_spinlock_t *lock) static __always_inline int arch_spin_is_contended(arch_spinlock_t *lock) { -u32 val =atomic_read (lock); +u32 val =atomic_read (&lock->val);return (s16)((val >>16 ) - (val &0xffff )) >1 ; }
include/asm-generic/spinlock_types.h +2 -10
@@ -3,15 +3,7 @@#ifndef __ASM_GENERIC_SPINLOCK_TYPES_H#define __ASM_GENERIC_SPINLOCK_TYPES_H -#include <linux/types.h> -typedef atomic_t arch_spinlock_t ; - -/* -* qrwlock_types depends on arch_spinlock_t, so we must typedef that before the -* include. -*/ -#include <asm/qrwlock_types.h> - -#define __ARCH_SPIN_LOCK_UNLOCKED ATOMIC_INIT(0) +#include <asm-generic/qspinlock_types.h> +#include <asm-generic/qrwlock_types.h> #endif /* __ASM_GENERIC_SPINLOCK_TYPES_H */
Signature · 1 line
2.36.1
[email protected]
maintainerPATCH v10 2/19 asm-generic: ticket-lock: Move into ticket_spinlock.h
In reply to [email protected]From: Guo Ren <[email protected]>
Move ticket-lock definition into an independent file. This is the preparation for the next combo spinlock of riscv.
--- include/asm-generic/spinlock.h | 87 +--------------------- include/asm-generic/ticket_spinlock.h | 103 ++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 86 deletions(-) create mode 100644 include/asm-generic/ticket_spinlock.h
include/asm-generic/spinlock.h +1 -86
@@ -1,94 +1,9 @@/* SPDX-License-Identifier: GPL-2.0 */ -/* -* 'Generic' ticket-lock implementation. -* -* It relies on atomic_fetch_add() having well defined forward progress -* guarantees under contention. If your architecture cannot provide this, stick -* to a test-and-set lock. -* -* It also relies on atomic_fetch_add() being safe vs smp_store_release() on a -* sub-word of the value. This is generally true for anything LL/SC although -* you'd be hard pressed to find anything useful in architecture specifications -* about this. If your architecture cannot do this you might be better off with -* a test-and-set. -* -* It further assumes atomic_*_release() + atomic_*_acquire() is RCpc and hence -* uses atomic_fetch_add() which is RCsc to create an RCsc hot path, along with -* a full fence after the spin to upgrade the otherwise-RCpc -* atomic_cond_read_acquire(). -* -* The implementation uses smp_cond_load_acquire() to spin, so if the -* architecture has WFE like instructions to sleep instead of poll for word -* modifications be sure to implement that (see ARM64 for example). -* -*/ -#ifndef __ASM_GENERIC_SPINLOCK_H#define __ASM_GENERIC_SPINLOCK_H -#include <linux/atomic.h> -#include <asm-generic/spinlock_types.h> - -static __always_inline void arch_spin_lock(arch_spinlock_t *lock) -{ -u32 val =atomic_fetch_add (1 <<16 , &lock->val); -u16 ticket = val >>16 ; - -if (ticket == (u16 )val) -return ; - -/* -* atomic_cond_read_acquire() is RCpc, but rather than defining a -* custom cond_read_rcsc() here we just emit a full fence. We only -* need the prior reads before subsequent writes ordering from -* smb_mb(), but as atomic_cond_read_acquire() just emits reads and we -* have no outstanding writes due to the atomic_fetch_add() the extra -* orderings are free. -*/ -atomic_cond_read_acquire (&lock->val, ticket == (u16 )VAL ); -smp_mb (); -} - -static __always_inline bool arch_spin_trylock(arch_spinlock_t *lock) -{ -u32 old =atomic_read (&lock->val); - -if ((old >>16 ) != (old &0xffff )) -return false; - -return atomic_try_cmpxchg (&lock->val, &old, old + (1 <<16 ));/* SC, for RCsc */ -} - -static __always_inline void arch_spin_unlock(arch_spinlock_t *lock) -{ -u16 *ptr = (u16 *)lock +IS_ENABLED (CONFIG_CPU_BIG_ENDIAN ); -u32 val =atomic_read (&lock->val); - -smp_store_release (ptr, (u16 )val +1 ); -} - -static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock) -{ -u32 val = lock.val.counter; - -return ((val >>16 ) == (val &0xffff )); -} - -static __always_inline int arch_spin_is_locked(arch_spinlock_t *lock) -{ -arch_spinlock_t val =READ_ONCE (*lock); - -return !arch_spin_value_unlocked (val); -} - -static __always_inline int arch_spin_is_contended(arch_spinlock_t *lock) -{ -u32 val =atomic_read (&lock->val); - -return (s16)((val >>16 ) - (val &0xffff )) >1 ; -} - +#include <asm-generic/ticket_spinlock.h> #include <asm/qrwlock.h> #endif /* __ASM_GENERIC_SPINLOCK_H */
include/asm-generic/ticket_spinlock.h new file +103 -0
103 lines not on this page · raw message
Signature · 1 line
2.36.1
[email protected]
maintainerPATCH v10 3/19 riscv: qspinlock: errata: Add ERRATA_THEAD_WRITE_ONCE fixup
In reply to [email protected]From: Guo Ren <[email protected]>
The early version of T-Head C9xx cores has a store merge buffer delay problem. The store merge buffer could improve the store queue performance by merging multi-store requests, but when there are not continued store requests, the prior single store request would be waiting in the store queue for a long time. That would cause significant problems for communication between multi-cores. This problem was found on sg2042 & th1520 platforms with the qspinlock lock torture test.
So appending a fence w.o could immediately flush the store merge buffer and let other cores see the write result.
This will apply the WRITE_ONCE errata to handle the non-standard behavior via appending a fence w.o instruction for WRITE_ONCE().
--- arch/riscv/Kconfig.errata | 19 +++++++++++++++++++ arch/riscv/errata/thead/errata.c | 20 ++++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 13 ------------- arch/riscv/include/asm/rwonce.h | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/vendorid_list.h | 14 ++++++++++++++ include/asm-generic/rwonce.h | 2 ++ 6 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 arch/riscv/include/asm/rwonce.h
arch/riscv/Kconfig.errata +19 -0
@@ -77,4 +77,23 @@ config ERRATA_THEAD_PMU If you don't know what to do here, say "Y". +config ERRATA_THEAD_WRITE_ONCE + bool "Apply T-Head WRITE_ONCE errata" + depends on ERRATA_THEAD + default y + help + The early version of T-Head C9xx cores has a store merge buffer + delay problem. The store merge buffer could improve the store queue + performance by merging multi-store requests, but when there are no + continued store requests, the prior single store request would be + waiting in the store queue for a long time. That would cause + significant problems for communication between multi-cores. Appending + a fence w.o could immediately flush the store merge buffer and let + other cores see the write result. + + This will apply the WRITE_ONCE errata to handle the non-standard + behavior via appending a fence w.o instruction for WRITE_ONCE(). + + If you don't know what to do here, say "Y". + endmenu # "CPU errata selection"
arch/riscv/errata/thead/errata.c +20 -0
@@ -69,6 +69,23 @@ static bool errata_probe_pmu(unsigned int stage,return true; } +static bool errata_probe_write_once (unsigned int stage, +unsigned long arch_id,unsigned long impid) +{ +if (!IS_ENABLED (CONFIG_ERRATA_THEAD_WRITE_ONCE )) +return false; + +/* target-c9xx cores report arch_id and impid as 0 */ +if (arch_id !=0 || impid !=0 ) +return false; + +if (stage ==RISCV_ALTERNATIVES_EARLY_BOOT || + stage ==RISCV_ALTERNATIVES_MODULE ) +return true; + +return false; +} +static u32 thead_errata_probe (unsigned int stage,unsigned long archid,unsigned long impid) {
@@ -83,6 +100,9 @@ static u32 thead_errata_probe(unsigned int stage,if (errata_probe_pmu (stage, archid, impid)) cpu_req_errata |=BIT (ERRATA_THEAD_PMU ); +if (errata_probe_write_once (stage, archid, impid)) + cpu_req_errata |=BIT (ERRATA_THEAD_WRITE_ONCE ); +return cpu_req_errata; }
arch/riscv/include/asm/errata_list.h +0 -13
@@ -11,19 +11,6 @@#include <asm/hwcap.h> #include <asm/vendorid_list.h> -#ifdef CONFIG_ERRATA_SIFIVE -#define ERRATA_SIFIVE_CIP_453 0 -#define ERRATA_SIFIVE_CIP_1200 1 -#define ERRATA_SIFIVE_NUMBER 2 -#endif - -#ifdef CONFIG_ERRATA_THEAD -#define ERRATA_THEAD_PBMT 0 -#define ERRATA_THEAD_CMO 1 -#define ERRATA_THEAD_PMU 2 -#define ERRATA_THEAD_NUMBER 3 -#endif -#ifdef __ASSEMBLY__#define ALT_INSN_FAULT (x) \
arch/riscv/include/asm/rwonce.h new file +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_RWONCE_H +#define __ASM_RWONCE_H + +#include <linux/compiler_types.h> +#include <asm/alternative-macros.h> +#include <asm/vendorid_list.h> + +#define __WRITE_ONCE (x, val) \ +do { \ + *(volatile typeof(x) *)&(x) = (val); \ + asm volatile(ALTERNATIVE( \ + __nops(1), \ + "fence w, o\n\t", \ + THEAD_VENDOR_ID, \ + ERRATA_THEAD_WRITE_ONCE, \ + CONFIG_ERRATA_THEAD_WRITE_ONCE) \ + : : : "memory"); \ +} while (0) + +#include <asm-generic/rwonce.h> + +#endif /* __ASM_RWONCE_H */
arch/riscv/include/asm/vendorid_list.h +14 -0
@@ -8,4 +8,18 @@#define SIFIVE_VENDOR_ID 0x489#define THEAD_VENDOR_ID 0x5b7 +#ifdef CONFIG_ERRATA_SIFIVE +#define ERRATA_SIFIVE_CIP_453 0 +#define ERRATA_SIFIVE_CIP_1200 1 +#define ERRATA_SIFIVE_NUMBER 2 +#endif + +#ifdef CONFIG_ERRATA_THEAD +#define ERRATA_THEAD_PBMT 0 +#define ERRATA_THEAD_CMO 1 +#define ERRATA_THEAD_PMU 2 +#define ERRATA_THEAD_WRITE_ONCE 3 +#define ERRATA_THEAD_NUMBER 4 +#endif +#endif
include/asm-generic/rwonce.h +2 -0
@@ -50,10 +50,12 @@__READ_ONCE (x); \ }) +#ifndef __WRITE_ONCE#define __WRITE_ONCE (x, val) \ do { \ *(volatile typeof(x) *)&(x) = (val); \ } while (0) +#endif #define WRITE_ONCE (x, val) \ do { \
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 4/19 riscv: qspinlock: Add basic queued_spinlock support
In reply to [email protected]From: Guo Ren <[email protected]>
The requirements of qspinlock have been documented by commit:a8ad07e5240c ("asm-generic: qspinlock: Indicate the use of mixed-size atomics").
Although RISC-V ISA gives out a weaker forward guarantee LR/SC, which doesn't satisfy the requirements of qspinlock above, it won't prevent some riscv vendors from implementing a strong fwd guarantee LR/SC in microarchitecture to match xchg_tail requirement. T-HEAD C9xx processor is the one.
We've tested the patch on SOPHGO sg2042 & th1520 and passed the stress test on Fedora & Ubuntu & OpenEuler ... Here is the performance comparison between qspinlock and ticket_lock on sg2042 (64 cores):
sysbench test=threads threads=32 yields=100 lock=8 (+13.8%):
queued_spinlock 0.5109/0.00
ticket_spinlock 0.5814/0.00
perf futex/hash (+6.7%):
queued_spinlock 1444393 operations/sec (+- 0.09%) ticket_spinlock 1353215 operations/sec (+- 0.15%)
perf futex/wake-parallel (+8.6%):
queued_spinlock (waking 1/64 threads) in 0.0253 ms (+-2.90%) ticket_spinlock (waking 1/64 threads) in 0.0275 ms (+-3.12%)
perf futex/requeue (+4.2%):
queued_spinlock Requeued 64 of 64 threads in 0.0785 ms (+-0.55%) ticket_spinlock Requeued 64 of 64 threads in 0.0818 ms (+-4.12%)
System Benchmarks (+6.4%)
queued_spinlock:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 628613745.4 53865.8
Double-Precision Whetstone 55.0 182422.8 33167.8
Execl Throughput 43.0 13116.6 3050.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 7762306.2 19601.8
File Copy 256 bufsize 500 maxblocks 1655.0 3417556.8 20649.9
File Copy 4096 bufsize 8000 maxblocks 5800.0 7427995.7 12806.9
Pipe Throughput 12440.0 23058600.5 18535.9
Pipe-based Context Switching 4000.0 2835617.7 7089.0
Process Creation 126.0 12537.3 995.0
Shell Scripts (1 concurrent) 42.4 57057.4 13456.9
Shell Scripts (8 concurrent) 6.0 7367.1 12278.5
System Call Overhead 15000.0 33308301.3 22205.5
========
System Benchmarks Index Score 12426.1 ticket_spinlock:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 626541701.9 53688.2
Double-Precision Whetstone 55.0 181921.0 33076.5
Execl Throughput 43.0 12625.1 2936.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 6553792.9 16550.0
File Copy 256 bufsize 500 maxblocks 1655.0 3189231.6 19270.3
File Copy 4096 bufsize 8000 maxblocks 5800.0 7221277.0 12450.5
Pipe Throughput 12440.0 20594018.7 16554.7
Pipe-based Context Switching 4000.0 2571117.7 6427.8
Process Creation 126.0 10798.4 857.0
Shell Scripts (1 concurrent) 42.4 57227.5 13497.1
Shell Scripts (8 concurrent) 6.0 7329.2 12215.3
System Call Overhead 15000.0 30766778.4 20511.2
========
System Benchmarks Index Score 11670.7The qspinlock has a significant improvement on SOPHGO SG2042 64 cores platform than the ticket_lock.
--- arch/riscv/Kconfig | 16 ++++++++++++++++ arch/riscv/include/asm/Kbuild | 3 ++- arch/riscv/include/asm/cmpxchg.h | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/spinlock.h | 17 +++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/spinlock.h
arch/riscv/Kconfig +16 -0
@@ -438,6 +438,22 @@ config NODES_SHIFT Specify the maximum number of NUMA Nodes available on the target system. Increases memory reserved to accommodate various tables. +choice + prompt "RISC-V spinlock type" + default RISCV_TICKET_SPINLOCKS + +config RISCV_TICKET_SPINLOCKS + bool "Using ticket spinlock" + +config RISCV_QUEUED_SPINLOCKS + bool "Using queued spinlock" + depends on SMP && MMU + select ARCH_USE_QUEUED_SPINLOCKS + help + Make sure your micro arch LL/SC has a strong forward progress guarantee. + Otherwise, stay at ticket-lock. +endchoice + config RISCV_ALTERNATIVE bool depends on !XIP_KERNEL
arch/riscv/include/asm/Kbuild +2 -1
@@ -2,10 +2,11 @@ generic-y += early_ioremap.h generic-y += flat.h generic-y += kvm_para.h +generic-y += mcs_spinlock.h generic-y += parport.h -generic-y += spinlock.h generic-y += spinlock_types.h generic-y += qrwlock.h generic-y += qrwlock_types.h +generic-y += qspinlock.h generic-y += user.h generic-y += vmlinux.lds.h
arch/riscv/include/asm/cmpxchg.h +24 -0
@@ -11,12 +11,36 @@#include <asm/barrier.h> #include <asm/fence.h> +static inline ulong __xchg16_relaxed (ulong new,void *ptr) +{ +ulong ret, tmp; +ulong shif = ((ulong )ptr &2 ) ?16 :0 ; +ulong mask =0xffff << shif; +ulong *__ptr = (ulong *)((ulong )ptr & ~2 ); + + __asm__ __volatile__ ( +"0: lr.w %0, %2\n" +" and %1, %0, %z3\n" +" or %1, %1, %z4\n" +" sc.w %1, %1, %2\n" +" bnez %1, 0b\n" + :"=&r" (ret),"=&r" (tmp),"+A" (*__ptr) + :"rJ" (~mask),"rJ" (new << shif) + :"memory" ); + +return (ulong)((ret & mask) >> shif); +} +#define __xchg_relaxed (ptr, new, size) \ ({ \ __typeof__(ptr) __ptr = (ptr); \ __typeof__(new) __new = (new); \ __typeof__(*(ptr)) __ret; \ switch (size) { \ + case 2: \ + __ret = (__typeof__(*(ptr))) \ + __xchg16_relaxed((ulong)__new, __ptr); \ + break; \ case 4: \ __asm__ __volatile__ ( \ " amoswap.w %0, %2, %1\n" \
arch/riscv/include/asm/spinlock.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_RISCV_SPINLOCK_H +#define __ASM_RISCV_SPINLOCK_H + +#ifdef CONFIG_QUEUED_SPINLOCKS +#define _Q_PENDING_LOOPS (1 << 9) +#endif + +#ifdef CONFIG_QUEUED_SPINLOCKS +#include <asm/qspinlock.h> +#include <asm/qrwlock.h> +#else +#include <asm-generic/spinlock.h> +#endif + +#endif /* __ASM_RISCV_SPINLOCK_H */
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 5/19 riscv: qspinlock: Introduce combo spinlock
In reply to [email protected]From: Guo Ren <[email protected]>
Combo spinlock could support queued and ticket in one Linux Image and select them during boot time via errata mechanism. Here is the func size (Bytes) comparison table below:
TYPE : COMBO | TICKET | QUEUEDarch_spin_lock : 106 | 60 | 50arch_spin_unlock : 54 | 36 | 26arch_spin_trylock : 110 | 72 | 54arch_spin_is_locked : 48 | 34 | 20arch_spin_is_contended : 56 | 40 | 24rch_spin_value_unlocked : 48 | 34 | 24
One example of disassemble combo arch_spin_unlock:
0xffffffff8000409c <+14>: nop # detour slot 0xffffffff800040a0 <+18>: fence rw,w # queued spinlock start 0xffffffff800040a4 <+22>: sb zero,0(a4) # queued spinlock end 0xffffffff800040a8 <+26>: ld s0,8(sp) 0xffffffff800040aa <+28>: addi sp,sp,16 0xffffffff800040ac <+30>: ret 0xffffffff800040ae <+32>: lw a5,0(a4) # ticket spinlock start 0xffffffff800040b0 <+34>: sext.w a5,a5 0xffffffff800040b2 <+36>: fence rw,w 0xffffffff800040b6 <+40>: addiw a5,a5,1 0xffffffff800040b8 <+42>: slli a5,a5,0x30 0xffffffff800040ba <+44>: srli a5,a5,0x30 0xffffffff800040bc <+46>: sh a5,0(a4) # ticket spinlock end 0xffffffff800040c0 <+50>: ld s0,8(sp) 0xffffffff800040c2 <+52>: addi sp,sp,16 0xffffffff800040c4 <+54>: ret
The qspinlock is smaller and faster than ticket-lock when all are in fast-path, and combo spinlock could provide a compatible Linux Image for different micro-arch design (weak/strict fwd guarantee) processors.
--- arch/riscv/Kconfig | 9 +++- arch/riscv/include/asm/hwcap.h | 1 + arch/riscv/include/asm/spinlock.h | 87 ++++++++++++++++++++++++++++++- arch/riscv/kernel/cpufeature.c | 10 ++++ 4 files changed, 104 insertions(+), 3 deletions(-)
arch/riscv/Kconfig +8 -1
@@ -440,7 +440,7 @@ config NODES_SHIFT choice prompt "RISC-V spinlock type" - default RISCV_TICKET_SPINLOCKS + default RISCV_COMBO_SPINLOCKS config RISCV_TICKET_SPINLOCKS bool "Using ticket spinlock"
@@ -452,6 +452,13 @@ config RISCV_QUEUED_SPINLOCKS help Make sure your micro arch LL/SC has a strong forward progress guarantee. Otherwise, stay at ticket-lock. + +config RISCV_COMBO_SPINLOCKS + bool "Using combo spinlock" + depends on SMP && MMU + select ARCH_USE_QUEUED_SPINLOCKS + help + Select queued spinlock or ticket-lock via errata. endchoice config RISCV_ALTERNATIVE
arch/riscv/include/asm/hwcap.h +1 -0
@@ -54,6 +54,7 @@#define RISCV_ISA_EXT_ZIFENCEI 41#define RISCV_ISA_EXT_ZIHPM 42 +#define RISCV_ISA_EXT_XTICKETLOCK 63#define RISCV_ISA_EXT_MAX 64#define RISCV_ISA_EXT_NAME_LEN_MAX 32
arch/riscv/include/asm/spinlock.h +85 -2
96 lines not on this page · raw message
arch/riscv/kernel/cpufeature.c +10 -0
@@ -324,6 +324,16 @@ void __init riscv_fill_hwcap(void)set_bit (RISCV_ISA_EXT_ZICSR , isainfo->isa);set_bit (RISCV_ISA_EXT_ZIFENCEI , isainfo->isa); +#ifdef CONFIG_RISCV_COMBO_SPINLOCKS +/* +* The RISC-V Linux used queued spinlock at first; then, we used ticket_lock +* as default or queued spinlock by choice. Because ticket_lock would dirty +* spinlock value, the only way is to change from queued_spinlock to +* ticket_spinlock, but can not be vice. +*/ +set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa); +#endif +/* * These ones were as they were part of the base ISA when the * port & dt-bindings were upstreamed, and so can be set
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 6/19 riscv: qspinlock: Allow force qspinlock from the command line
In reply to [email protected]From: Guo Ren <[email protected]>
Allow cmdline to force the kernel to use queued_spinlock when CONFIG_RISCV_COMBO_SPINLOCKS=y.
--- Documentation/admin-guide/kernel-parameters.txt | 3 +++ arch/riscv/include/asm/cpufeature.h | 2 ++ arch/riscv/kernel/cpufeature.c | 15 ++++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +3 -0
@@ -4666,6 +4666,9 @@ quiet [KNL] Disable most log messages + qspinlock [RISCV] Forces kernel to use queued_spinlock when + CONFIG_RISCV_COMBO_SPINLOCKS=y. + r128= [HW,DRM] radix_hcall_invalidate=on [PPC/PSERIES]
arch/riscv/include/asm/cpufeature.h +2 -0
@@ -30,4 +30,6 @@ DECLARE_PER_CPU(long, misaligned_access_speed);/* Per-cpu ISA extensions. */ extern struct riscv_isainfo hart_isa[NR_CPUS ]; +extern bool force_qspinlock; +#endif
arch/riscv/kernel/cpufeature.c +14 -1
@@ -99,6 +99,17 @@ static bool riscv_isa_extension_check(int id)return true; } +#ifdef CONFIG_QUEUED_SPINLOCKS +bool force_qspinlock = false; +static int __init force_queued_spinlock (char *p) +{ + force_qspinlock = true; +pr_info ("Force kernel to use queued_spinlock" ); +return 0 ; +} +early_param ("qspinlock" , force_queued_spinlock); +#endif +void __init riscv_fill_hwcap (void ) {struct device_node *node;
@@ -331,7 +342,9 @@ void __init riscv_fill_hwcap(void)* spinlock value, the only way is to change from queued_spinlock to * ticket_spinlock, but can not be vice. */ -set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa); +if (!force_qspinlock) { +set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa); + }#endif /*
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 7/19 riscv: qspinlock: errata: Introduce ERRATA_THEAD_QSPINLOCK
In reply to [email protected]From: Guo Ren <[email protected]>
According to qspinlock requirements, RISC-V gives out a weak LR/SC forward progress guarantee which does not satisfy qspinlock. But many vendors could produce stronger forward guarantee LR/SC to ensure the xchg_tail could be finished in time on any kind of hart. T-HEAD is the vendor which implements strong forward guarantee LR/SC instruction pairs, so enable qspinlock for T-HEAD with errata help.
T-HEAD early version of processors has the merge buffer delay problem, so we need ERRATA_WRITEONCE to support qspinlock.
--- arch/riscv/Kconfig.errata | 13 +++++++++++++ arch/riscv/errata/thead/errata.c | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 20 ++++++++++++++++++++ arch/riscv/include/asm/vendorid_list.h | 3 ++- arch/riscv/kernel/cpufeature.c | 3 ++- 5 files changed, 61 insertions(+), 2 deletions(-)
arch/riscv/Kconfig.errata +13 -0
@@ -96,4 +96,17 @@ config ERRATA_THEAD_WRITE_ONCE If you don't know what to do here, say "Y". +config ERRATA_THEAD_QSPINLOCK + bool "Apply T-Head queued spinlock errata" + depends on ERRATA_THEAD + default y + help + The T-HEAD C9xx processors implement strong fwd guarantee LR/SC to + match the xchg_tail requirement of qspinlock. + + This will apply the QSPINLOCK errata to handle the non-standard + behavior via using qspinlock instead of ticket_lock. + + If you don't know what to do here, say "Y". + endmenu # "CPU errata selection"
arch/riscv/errata/thead/errata.c +24 -0
@@ -86,6 +86,27 @@ static bool errata_probe_write_once(unsigned int stage,return false; } +static bool errata_probe_qspinlock (unsigned int stage, +unsigned long arch_id,unsigned long impid) +{ +if (!IS_ENABLED (CONFIG_ERRATA_THEAD_QSPINLOCK )) +return false; + +/* +* The queued_spinlock torture would get in livelock without +* ERRATA_THEAD_WRITE_ONCE fixup for the early versions of T-HEAD +* processors. +*/ +if (arch_id ==0 && impid ==0 && + !IS_ENABLED (CONFIG_ERRATA_THEAD_WRITE_ONCE )) +return false; + +if (stage ==RISCV_ALTERNATIVES_EARLY_BOOT ) +return true; + +return false; +} +static u32 thead_errata_probe (unsigned int stage,unsigned long archid,unsigned long impid) {
@@ -103,6 +124,9 @@ static u32 thead_errata_probe(unsigned int stage,if (errata_probe_write_once (stage, archid, impid)) cpu_req_errata |=BIT (ERRATA_THEAD_WRITE_ONCE ); +if (errata_probe_qspinlock (stage, archid, impid)) + cpu_req_errata |=BIT (ERRATA_THEAD_QSPINLOCK ); +return cpu_req_errata; }
arch/riscv/include/asm/errata_list.h +20 -0
@@ -141,6 +141,26 @@ asm volatile(ALTERNATIVE( \ : "=r" (__ovl) : \ : "memory") +static __always_inline bool +riscv_has_errata_thead_qspinlock (void ) +{ +if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) { +asm_volatile_goto ( +ALTERNATIVE ( +"j %l[l_no]" ,"nop" , +THEAD_VENDOR_ID , +ERRATA_THEAD_QSPINLOCK , +CONFIG_ERRATA_THEAD_QSPINLOCK ) + : : : : l_no); + }else { + goto l_no; + } + +return true; +l_no: +return false; +} +#endif /* __ASSEMBLY__ */ #endif
arch/riscv/include/asm/vendorid_list.h +2 -1
@@ -19,7 +19,8 @@#define ERRATA_THEAD_CMO 1#define ERRATA_THEAD_PMU 2#define ERRATA_THEAD_WRITE_ONCE 3 -#define ERRATA_THEAD_NUMBER 4 +#define ERRATA_THEAD_QSPINLOCK 4 +#define ERRATA_THEAD_NUMBER 5#endif #endif
arch/riscv/kernel/cpufeature.c +2 -1
@@ -342,7 +342,8 @@ void __init riscv_fill_hwcap(void)* spinlock value, the only way is to change from queued_spinlock to * ticket_spinlock, but can not be vice. */ -if (!force_qspinlock) { +if (!force_qspinlock && + !riscv_has_errata_thead_qspinlock ()) {set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa); }#endif
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 8/19 riscv: qspinlock: Use new static key for controlling call of virt_spin_lock()
In reply to [email protected]From: Guo Ren <[email protected]>
Add a static key controlling whether virt_spin_lock() should be called or not. When running on bare metal set the new key to false.
The KVM guests fall back to a Test-and-Set spinlock, because fair locks have horrible lock 'holder' preemption issues. The virt_spin_lock_key would shortcut for the queued_spin_lock_slowpath() function that allow virt_spin_lock to hijack it.
--- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/sbi.h | 8 ++++++++ arch/riscv/include/asm/spinlock.h | 22 ++++++++++++++++++++++ arch/riscv/kernel/cpufeature.c | 4 +++- arch/riscv/kernel/sbi.c | 2 +- arch/riscv/kernel/setup.c | 19 +++++++++++++++++++ 6 files changed, 54 insertions(+), 2 deletions(-)
arch/riscv/Kconfig +1 -0
@@ -20,6 +20,7 @@ config RISCV select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE select ARCH_HAS_BINFMT_FLAT select ARCH_HAS_CURRENT_STACK_POINTER + select ARCH_HAS_CPU_FINALIZE_INIT select ARCH_HAS_DEBUG_VIRTUAL if MMU select ARCH_HAS_DEBUG_VM_PGTABLE select ARCH_HAS_DEBUG_WX
arch/riscv/include/asm/sbi.h +8 -0
@@ -51,6 +51,13 @@ enum sbi_ext_base_fid { SBI_EXT_BASE_GET_MIMPID, }; +enum sbi_ext_base_impl_id { +SBI_EXT_BASE_IMPL_ID_BBL =0 , +SBI_EXT_BASE_IMPL_ID_OPENSBI , +SBI_EXT_BASE_IMPL_ID_XVISOR , +SBI_EXT_BASE_IMPL_ID_KVM , +}; +enum sbi_ext_time_fid {SBI_EXT_TIME_SET_TIMER =0 , };
@@ -286,6 +293,7 @@ int sbi_console_getchar(void);long sbi_get_mvendorid (void );long sbi_get_marchid (void );long sbi_get_mimpid (void ); +long sbi_get_firmware_id (void );void sbi_set_timer (uint64_t stime_value);void sbi_shutdown (void );void sbi_send_ipi (unsigned int cpu);
arch/riscv/include/asm/spinlock.h +22 -0
@@ -4,6 +4,28 @@#define __ASM_RISCV_SPINLOCK_H#ifdef CONFIG_QUEUED_SPINLOCKS +/* +* The KVM guests fall back to a Test-and-Set spinlock, because fair locks +* have horrible lock 'holder' preemption issues. The virt_spin_lock_key +* would shortcut for the queued_spin_lock_slowpath() function that allow +* virt_spin_lock to hijack it. +*/ +DECLARE_STATIC_KEY_TRUE (virt_spin_lock_key); + +#define virt_spin_lock virt_spin_lock +static inline bool virt_spin_lock (struct qspinlock *lock) +{ +if (!static_branch_likely (&virt_spin_lock_key)) +return false; + +do { +while (atomic_read (&lock->val) !=0 ) +cpu_relax (); + }while (atomic_cmpxchg (&lock->val,0 , _Q_LOCKED_VAL) !=0 ); + +return true; +} +#define _Q_PENDING_LOOPS (1 << 9)#endif
arch/riscv/kernel/cpufeature.c +3 -1
@@ -21,6 +21,7 @@#include <asm/hwcap.h> #include <asm/patch.h> #include <asm/processor.h> +#include <asm/sbi.h> #include <asm/vector.h> #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
@@ -343,7 +344,8 @@ void __init riscv_fill_hwcap(void)* ticket_spinlock, but can not be vice. */ if (!force_qspinlock && - !riscv_has_errata_thead_qspinlock ()) { + !riscv_has_errata_thead_qspinlock () && + (sbi_get_firmware_id () !=SBI_EXT_BASE_IMPL_ID_KVM )) {set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa); }#endif
arch/riscv/kernel/sbi.c +1 -1
@@ -555,7 +555,7 @@ static inline long sbi_get_spec_version(void)return __sbi_base_ecall (SBI_EXT_BASE_GET_SPEC_VERSION ); } -static inline long sbi_get_firmware_id (void ) +long sbi_get_firmware_id (void ) {return __sbi_base_ecall (SBI_EXT_BASE_GET_IMP_ID ); }
arch/riscv/kernel/setup.c +19 -0
@@ -26,6 +26,7 @@#include <asm/alternative.h> #include <asm/cacheflush.h> #include <asm/cpu_ops.h> +#include <asm/cpufeature.h> #include <asm/early_ioremap.h> #include <asm/pgtable.h> #include <asm/setup.h>
@@ -264,6 +265,19 @@ static void __init parse_dtb(void)#endif } +#ifdef CONFIG_QUEUED_SPINLOCKS +DEFINE_STATIC_KEY_TRUE (virt_spin_lock_key); + +static void __init virt_spin_lock_init (void ) +{ +if (sbi_get_firmware_id () !=SBI_EXT_BASE_IMPL_ID_KVM || + force_qspinlock) +static_branch_disable (&virt_spin_lock_key); +} +#else +static void __init virt_spin_lock_init (void ) {} +#endif +extern void __init init_rt_signal_env (void );void __init setup_arch (char **cmdline_p)
@@ -313,6 +327,11 @@ void __init setup_arch(char **cmdline_p)riscv_noncoherent_supported (); } +void __init arch_cpu_finalize_init (void ) +{ +virt_spin_lock_init (); +} +static int __init topology_init (void ) {int i, ret;
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 9/19 RISC-V: paravirt: pvqspinlock: Add paravirt qspinlock skeleton
In reply to [email protected]From: Guo Ren <[email protected]>
Using static_call to switch between:
native_queued_spin_lock_slowpath() __pv_queued_spin_lock_slowpath()
native_queued_spin_unlock() __pv_queued_spin_unlock()
Finish the pv_wait implementation, but pv_kick needs the SBI definition of the next patches.
--- arch/riscv/include/asm/Kbuild | 1 - arch/riscv/include/asm/paravirt.h | 20 +++++++++ arch/riscv/include/asm/qspinlock.h | 29 ++++++++++++ arch/riscv/include/asm/qspinlock_paravirt.h | 7 +++ arch/riscv/include/asm/spinlock.h | 2 +- arch/riscv/kernel/paravirt.c | 50 +++++++++++++++++++++ arch/riscv/kernel/setup.c | 3 ++ 7 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/include/asm/qspinlock.h create mode 100644 arch/riscv/include/asm/qspinlock_paravirt.h
arch/riscv/include/asm/Kbuild +0 -1
@@ -7,6 +7,5 @@ generic-y += parport.h generic-y += spinlock_types.h generic-y += qrwlock.h generic-y += qrwlock_types.h -generic-y += qspinlock.h generic-y += user.h generic-y += vmlinux.lds.h
arch/riscv/include/asm/paravirt.h +20 -0
@@ -26,4 +26,24 @@ int __init pv_time_init(void);#endif // CONFIG_PARAVIRT +#ifdef CONFIG_PARAVIRT_SPINLOCKS + +void pv_wait (u8 *ptr,u8 val); +void pv_kick (int cpu); + +void dummy_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val); +void dummy_queued_spin_unlock (struct qspinlock *lock); + +DECLARE_STATIC_CALL (pv_queued_spin_lock_slowpath, dummy_queued_spin_lock_slowpath); +DECLARE_STATIC_CALL (pv_queued_spin_unlock, dummy_queued_spin_unlock); + +void __init pv_qspinlock_init (void ); + +static inline bool pv_is_native_spin_unlock (void ) +{ +return false; +} + +#endif /* CONFIG_PARAVIRT_SPINLOCKS */ +#endif
arch/riscv/include/asm/qspinlock.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_RISCV_QSPINLOCK_H +#define _ASM_RISCV_QSPINLOCK_H + +#ifdef CONFIG_PARAVIRT_SPINLOCKS +#include <asm/paravirt.h> + +/* How long a lock should spin before we consider blocking */ +#define SPIN_THRESHOLD (1 << 15) + +void native_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val); +void __pv_init_lock_hash (void ); +void __pv_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val); + +static inline void queued_spin_lock_slowpath (struct qspinlock *lock,u32 val) +{ +static_call (pv_queued_spin_lock_slowpath)(lock, val); +} + +#define queued_spin_unlock queued_spin_unlock +static inline void queued_spin_unlock (struct qspinlock *lock) +{ +static_call (pv_queued_spin_unlock)(lock); +} +#endif /* CONFIG_PARAVIRT_SPINLOCKS */ + +#include <asm-generic/qspinlock.h> + +#endif /* _ASM_RISCV_QSPINLOCK_H */
arch/riscv/include/asm/qspinlock_paravirt.h new file +7 -0
@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_RISCV_QSPINLOCK_PARAVIRT_H +#define _ASM_RISCV_QSPINLOCK_PARAVIRT_H + +void __pv_queued_spin_unlock (struct qspinlock *lock); + +#endif /* _ASM_RISCV_QSPINLOCK_PARAVIRT_H */
arch/riscv/include/asm/spinlock.h +1 -1
@@ -39,7 +39,7 @@ static inline bool virt_spin_lock(struct qspinlock *lock)#undef arch_spin_trylock#undef arch_spin_unlock -#include <asm-generic/qspinlock.h> +#include <asm/qspinlock.h> #include <asm/hwcap.h> #undef arch_spin_is_locked
arch/riscv/kernel/paravirt.c +50 -0
53 lines not on this page · raw message
arch/riscv/kernel/setup.c +3 -0
@@ -329,6 +329,9 @@ void __init setup_arch(char **cmdline_p)void __init arch_cpu_finalize_init (void ) { +#ifdef CONFIG_PARAVIRT_SPINLOCKS +pv_qspinlock_init (); +#endif virt_spin_lock_init (); }
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 10/19 RISC-V: paravirt: pvqspinlock: KVM: Add paravirt qspinlock skeleton
In reply to [email protected]From: Guo Ren <[email protected]>
Add the files functions needed to support the SBI PVLOCK (paravirt qspinlock kick_cpu) extension. This is a preparation for the next core implementation of kick_cpu.
--- arch/riscv/include/asm/kvm_vcpu_sbi.h | 1 + arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu_sbi.c | 4 +++ arch/riscv/kvm/vcpu_sbi_pvlock.c | 38 +++++++++++++++++++++++++++ 5 files changed, 45 insertions(+) create mode 100644 arch/riscv/kvm/vcpu_sbi_pvlock.c
arch/riscv/include/asm/kvm_vcpu_sbi.h +1 -0
@@ -71,6 +71,7 @@ extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_srst;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_hsm;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor; +extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_pvlock;#ifdef CONFIG_RISCV_PMU_SBI extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_pmu;
arch/riscv/include/uapi/asm/kvm.h +1 -0
@@ -141,6 +141,7 @@ enum KVM_RISCV_SBI_EXT_ID {KVM_RISCV_SBI_EXT_PMU ,KVM_RISCV_SBI_EXT_EXPERIMENTAL ,KVM_RISCV_SBI_EXT_VENDOR , +KVM_RISCV_SBI_EXT_PVLOCK , KVM_RISCV_SBI_EXT_MAX, };
arch/riscv/kvm/Makefile +1 -0
@@ -25,6 +25,7 @@ kvm-$(CONFIG_RISCV_SBI_V01) += vcpu_sbi_v01.o kvm-y += vcpu_sbi_base.o kvm-y += vcpu_sbi_replace.o kvm-y += vcpu_sbi_hsm.o +kvm-y += vcpu_sbi_pvlock.o kvm-y += vcpu_timer.o kvm-$(CONFIG_RISCV_PMU_SBI) += vcpu_pmu.o vcpu_sbi_pmu.o kvm-y += aia.o
arch/riscv/kvm/vcpu_sbi.c +4 -0
@@ -74,6 +74,10 @@ static const struct kvm_riscv_sbi_extension_entry sbi_ext[] = { .ext_idx = KVM_RISCV_SBI_EXT_VENDOR, .ext_ptr = &vcpu_sbi_ext_vendor, }, + { + .ext_idx = KVM_RISCV_SBI_EXT_PVLOCK, + .ext_ptr = &vcpu_sbi_ext_pvlock, + }, };void kvm_riscv_vcpu_sbi_forward (struct kvm_vcpu *vcpu,struct kvm_run *run)
arch/riscv/kvm/vcpu_sbi_pvlock.c new file +38 -0
@@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (c), 2023 Alibaba Cloud +* +* Authors: +* Guo Ren <[email protected]> +*/ + +#include <linux/errno.h> +#include <linux/err.h> +#include <linux/kvm_host.h> +#include <asm/sbi.h> +#include <asm/kvm_vcpu_sbi.h> + +static int kvm_sbi_ext_pvlock_handler (struct kvm_vcpu *vcpu,struct kvm_run *run, +struct kvm_vcpu_sbi_return *retdata) +{ +int ret =0 ; +struct kvm_cpu_context *cp = &vcpu->arch.guest_context; +unsigned long funcid = cp->a6; + +switch (funcid) { +case SBI_EXT_PVLOCK_KICK_CPU : +break ; +default : + ret =SBI_ERR_NOT_SUPPORTED ; + } + + retdata->err_val = ret; + +return 0 ; +} + +const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_pvlock = { + .extid_start =SBI_EXT_PVLOCK , + .extid_end =SBI_EXT_PVLOCK , + .handler = kvm_sbi_ext_pvlock_handler, +};
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 11/19 RISC-V: paravirt: pvqspinlock: KVM: Implement kvm_sbi_ext_pvlock_kick_cpu()
In reply to [email protected]From: Guo Ren <[email protected]>
We only need to call the kvm_vcpu_kick() and bring target_vcpu from the halt state. No irq raised, no other request, just a pure vcpu_kick.
--- arch/riscv/kvm/vcpu_sbi_pvlock.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/riscv/kvm/vcpu_sbi_pvlock.c +19 -0
@@ -12,6 +12,24 @@#include <asm/sbi.h> #include <asm/kvm_vcpu_sbi.h> +static int kvm_sbi_ext_pvlock_kick_cpu (struct kvm_vcpu *vcpu) +{ +struct kvm_cpu_context *cp = &vcpu->arch.guest_context; +struct kvm *kvm = vcpu->kvm; +struct kvm_vcpu *target; + + target =kvm_get_vcpu_by_id (kvm, cp->a0); +if (!target) +return SBI_ERR_INVALID_PARAM ; + +kvm_vcpu_kick (target); + +if (READ_ONCE (target->ready)) +kvm_vcpu_yield_to (target); + +return SBI_SUCCESS ; +} +static int kvm_sbi_ext_pvlock_handler (struct kvm_vcpu *vcpu,struct kvm_run *run,struct kvm_vcpu_sbi_return *retdata) {
@@ -21,6 +39,7 @@ static int kvm_sbi_ext_pvlock_handler(struct kvm_vcpu *vcpu, struct kvm_run *runswitch (funcid) {case SBI_EXT_PVLOCK_KICK_CPU : + ret =kvm_sbi_ext_pvlock_kick_cpu (vcpu);break ;default : ret =SBI_ERR_NOT_SUPPORTED ;
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 12/19 RISC-V: paravirt: pvqspinlock: Add nopvspin kernel parameter
In reply to [email protected]From: Guo Ren <[email protected]>
Disables the qspinlock slow path using PV optimizations which allow the hypervisor to 'idle' the guest on lock contention.
--- Documentation/admin-guide/kernel-parameters.txt | 2 +- arch/riscv/kernel/paravirt.c | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +1 -1
@@ -3820,7 +3820,7 @@ as generic guest with no PV drivers. Currently support XEN HVM, KVM, HYPER_V and VMWARE guest. - nopvspin [X86,XEN,KVM] + nopvspin [X86,XEN,KVM,RISC-V] Disables the qspinlock slow path using PV optimizations which allow the hypervisor to 'idle' the guest on lock contention.
arch/riscv/kernel/paravirt.c +13 -0
@@ -165,8 +165,21 @@ DEFINE_STATIC_CALL(pv_queued_spin_unlock, native_queued_spin_unlock);EXPORT_SYMBOL (__SCK__pv_queued_spin_lock_slowpath);EXPORT_SYMBOL (__SCK__pv_queued_spin_unlock); +static bool nopvspin; +static __init int parse_nopvspin (char *arg) +{ + nopvspin = true; +return 0 ; +} +early_param ("nopvspin" , parse_nopvspin); +void __init pv_qspinlock_init (void ) { +if (nopvspin) { +pr_info ("PV qspinlocks disabled\n" ); +return ; + } +if (num_possible_cpus () ==1 )return ;
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 13/19 RISC-V: paravirt: pvqspinlock: Remove unnecessary definitions of cmpxchg & xchg
In reply to [email protected]From: Guo Ren <[email protected]>
The custom xchg/cmpxchg_release macro definitions have no difference from the common code from the binary view. The xchg32/64 macro definitions have been abandoned in Linux. Thus, remove all of them.
This is a preparation for the next cmpxchg_small & xchg8 patches.
--- arch/riscv/include/asm/cmpxchg.h | 93 -------------------------------- 1 file changed, 93 deletions(-)
arch/riscv/include/asm/cmpxchg.h +0 -93
@@ -103,41 +103,6 @@ static inline ulong __xchg16_relaxed(ulong new, void *ptr) _x_, sizeof(*(ptr))); \ }) -#define __xchg_release (ptr, new, size) \ -({ \ - __typeof__(ptr) __ptr = (ptr); \ - __typeof__(new) __new = (new); \ - __typeof__(*(ptr)) __ret; \ - switch (size) { \ - case 4: \ - __asm__ __volatile__ ( \ - RISCV_RELEASE_BARRIER \ - " amoswap.w %0, %2, %1\n" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ - break; \ - case 8: \ - __asm__ __volatile__ ( \ - RISCV_RELEASE_BARRIER \ - " amoswap.d %0, %2, %1\n" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) - -#define arch_xchg_release (ptr, x) \ -({ \ - __typeof__(*(ptr)) _x_ = (x); \ - (__typeof__(*(ptr))) __xchg_release((ptr), \ - _x_, sizeof(*(ptr))); \ -}) -#define __arch_xchg (ptr, new, size) \ ({ \ __typeof__(ptr) __ptr = (ptr); \
@@ -170,18 +135,6 @@ static inline ulong __xchg16_relaxed(ulong new, void *ptr) (__typeof__ (*(ptr )))__arch_xchg ((ptr), _x_,sizeof (*(ptr))); \ }) -#define xchg32 (ptr, x) \ -({ \ - BUILD_BUG_ON(sizeof(*(ptr)) != 4); \ - arch_xchg((ptr), (x)); \ -}) - -#define xchg64 (ptr, x) \ -({ \ - BUILD_BUG_ON(sizeof(*(ptr)) != 8); \ - arch_xchg((ptr), (x)); \ -}) -/* * Atomic compare and exchange. Compare OLD with MEM, if identical, * store NEW in MEM. Return the initial value in MEM. Success is
@@ -277,52 +230,6 @@ static inline ulong __xchg16_relaxed(ulong new, void *ptr) _o_, _n_, sizeof(*(ptr))); \ }) -#define __cmpxchg_release (ptr, old, new, size) \ -({ \ - __typeof__(ptr) __ptr = (ptr); \ - __typeof__(*(ptr)) __old = (old); \ - __typeof__(*(ptr)) __new = (new); \ - __typeof__(*(ptr)) __ret; \ - register unsigned int __rc; \ - switch (size) { \ - case 4: \ - __asm__ __volatile__ ( \ - RISCV_RELEASE_BARRIER \ - "0: lr.w %0, %2\n" \ - " bne %0, %z3, 1f\n" \ - " sc.w %1, %z4, %2\n" \ - " bnez %1, 0b\n" \ - "1:\n" \ - : "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr) \ - : "rJ" ((long)__old), "rJ" (__new) \ - : "memory"); \ - break; \ - case 8: \ - __asm__ __volatile__ ( \ - RISCV_RELEASE_BARRIER \ - "0: lr.d %0, %2\n" \ - " bne %0, %z3, 1f\n" \ - " sc.d %1, %z4, %2\n" \ - " bnez %1, 0b\n" \ - "1:\n" \ - : "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr) \ - : "rJ" (__old), "rJ" (__new) \ - : "memory"); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) - -#define arch_cmpxchg_release (ptr, o, n) \ -({ \ - __typeof__(*(ptr)) _o_ = (o); \ - __typeof__(*(ptr)) _n_ = (n); \ - (__typeof__(*(ptr))) __cmpxchg_release((ptr), \ - _o_, _n_, sizeof(*(ptr))); \ -}) -#define __cmpxchg (ptr, old, new, size) \ ({ \ __typeof__(ptr) __ptr = (ptr); \
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 14/19 RISC-V: paravirt: pvqspinlock: Add xchg8 & cmpxchg_small support
In reply to [email protected]From: Guo Ren <[email protected]>
The pvqspinlock needs additional sub-word atomic operations. Here is the list:
- xchg8 (RCsc)
- cmpxchg8/16_relaxed
- cmpxchg8/16_release (Rcpc)
- cmpxchg8_acquire (RCpc)
- cmpxchg8 (RCsc)
Although paravirt qspinlock doesn't have the native_qspinlock fairness, giving a strong forward progress guarantee to these atomic semantics could prevent unnecessary tries, which would cause cache line bouncing.
--- arch/riscv/include/asm/cmpxchg.h | 177 +++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+)
arch/riscv/include/asm/cmpxchg.h +177 -0
@@ -103,12 +103,37 @@ static inline ulong __xchg16_relaxed(ulong new, void *ptr) _x_, sizeof(*(ptr))); \ }) +static inline ulong __xchg8 (ulong new,void *ptr) +{ +ulong ret, tmp; +ulong shif = ((ulong )ptr &3 ) *8 ; +ulong mask =0xff << shif; +ulong *__ptr = (ulong *)((ulong )ptr & ~3 ); + + __asm__ __volatile__ ( +"0: lr.w %0, %2\n" +" and %1, %0, %z3\n" +" or %1, %1, %z4\n" +" sc.w.rl %1, %1, %2\n" +" bnez %1, 0b\n" +"fence w, rw\n" + :"=&r" (ret),"=&r" (tmp),"+A" (*__ptr) + :"rJ" (~mask),"rJ" (new << shif) + :"memory" ); + +return (ulong)((ret & mask) >> shif); +} +#define __arch_xchg (ptr, new, size) \ ({ \ __typeof__(ptr) __ptr = (ptr); \ __typeof__(new) __new = (new); \ __typeof__(*(ptr)) __ret; \ switch (size) { \ + case 1: \ + __ret = (__typeof__(*(ptr))) \ + __xchg8((ulong)__new, __ptr); \ + break; \ case 4: \ __asm__ __volatile__ ( \ " amoswap.w.aqrl %0, %2, %1\n" \
@@ -140,6 +165,51 @@ static inline ulong __xchg16_relaxed(ulong new, void *ptr)* store NEW in MEM. Return the initial value in MEM. Success is * indicated by comparing RETURN with OLD. */ +static inline ulong __cmpxchg_small_relaxed (void *ptr,ulong old, +ulong new,ulong size) +{ +ulong shift; +ulong ret, mask, temp; +volatile ulong *ptr32; + +/* Mask inputs to the correct size. */ + mask =GENMASK ((size *BITS_PER_BYTE ) -1 ,0 ); + old &= mask; + new &= mask; + +/* +* Calculate a shift & mask that correspond to the value we wish to +* compare & exchange within the naturally aligned 4 byte integer +* that includes it. +*/ + shift = (ulong )ptr &0x3 ; + shift *=BITS_PER_BYTE ; + old <<= shift; + new <<= shift; + mask <<= shift; + +/* +* Calculate a pointer to the naturally aligned 4 byte integer that +* includes our byte of interest, and load its value. +*/ + ptr32 = (volatile ulong *)((ulong )ptr & ~0x3 ); + + __asm__ __volatile__ ( +"0: lr.w %0, %2\n" +" and %1, %0, %z3\n" +" bne %1, %z5, 1f\n" +" and %1, %0, %z4\n" +" or %1, %1, %z6\n" +" sc.w %1, %1, %2\n" +" bnez %1, 0b\n" +"1:\n" + :"=&r" (ret),"=&r" (temp),"+A" (*ptr32) + :"rJ" (mask),"rJ" (~mask),"rJ" (old),"rJ" (new) + :"memory" ); + +return (ret & mask) >> shift; +} +#define __cmpxchg_relaxed (ptr, old, new, size) \ ({ \ __typeof__(ptr) __ptr = (ptr); \
@@ -148,6 +218,11 @@ static inline ulong __xchg16_relaxed(ulong new, void *ptr) __typeof__(*(ptr)) __ret; \ registerunsigned int __rc; \switch (size) { \ +case 1 : \ + __ret = (__typeof__ (*(ptr ))) \ +__cmpxchg_small_relaxed (__ptr, (ulong )__old, \ + (ulong )__new, (ulong )size); \ +break ; \case 4 : \ __asm__ __volatile__ ( \ "0: lr.w %0, %2\n" \
@@ -184,6 +259,52 @@ static inline ulong __xchg16_relaxed(ulong new, void *ptr) _o_, _n_, sizeof(*(ptr))); \ }) +static inline ulong __cmpxchg_small_acquire (void *ptr,ulong old, +ulong new,ulong size) +{ +ulong shift; +ulong ret, mask, temp; +volatile ulong *ptr32; + +/* Mask inputs to the correct size. */ + mask =GENMASK ((size *BITS_PER_BYTE ) -1 ,0 ); + old &= mask; + new &= mask; + +/* +* Calculate a shift & mask that correspond to the value we wish to +* compare & exchange within the naturally aligned 4 byte integer +* that includes it. +*/ + shift = (ulong )ptr &0x3 ; + shift *=BITS_PER_BYTE ; + old <<= shift; + new <<= shift; + mask <<= shift; + +/* +* Calculate a pointer to the naturally aligned 4 byte integer that +* includes our byte of interest, and load its value. +*/ + ptr32 = (volatile ulong *)((ulong )ptr & ~0x3 ); + + __asm__ __volatile__ ( +"0: lr.w %0, %2\n" +" and %1, %0, %z3\n" +" bne %1, %z5, 1f\n" +" and %1, %0, %z4\n" +" or %1, %1, %z6\n" +" sc.w %1, %1, %2\n" +" bnez %1, 0b\n" +RISCV_ACQUIRE_BARRIER +"1:\n" + :"=&r" (ret),"=&r" (temp),"+A" (*ptr32) + :"rJ" (mask),"rJ" (~mask),"rJ" (old),"rJ" (new) + :"memory" ); + +return (ret & mask) >> shift; +} +#define __cmpxchg_acquire (ptr, old, new, size) \ ({ \ __typeof__(ptr) __ptr = (ptr); \
@@ -192,6 +313,12 @@ static inline ulong __xchg16_relaxed(ulong new, void *ptr) __typeof__(*(ptr)) __ret; \ registerunsigned int __rc; \switch (size) { \ +case 1 : \ +case 2 : \ + __ret = (__typeof__ (*(ptr ))) \ +__cmpxchg_small_acquire (__ptr, (ulong )__old, \ + (ulong )__new, (ulong )size); \ +break ; \case 4 : \ __asm__ __volatile__ ( \ "0: lr.w %0, %2\n" \
@@ -230,6 +357,51 @@ static inline ulong __xchg16_relaxed(ulong new, void *ptr) _o_, _n_, sizeof(*(ptr))); \ }) +static inline ulong __cmpxchg_small (void *ptr,ulong old, +ulong new,ulong size) +{ +ulong shift; +ulong ret, mask, temp; +volatile ulong *ptr32; + +/* Mask inputs to the correct size. */ + mask =GENMASK ((size *BITS_PER_BYTE ) -1 ,0 ); + old &= mask; + new &= mask; + +/* +* Calculate a shift & mask that correspond to the value we wish to +* compare & exchange within the naturally aligned 4 byte integer +* that includes it. +*/ + shift = (ulong )ptr &0x3 ; + shift *=BITS_PER_BYTE ; + old <<= shift; + new <<= shift; + mask <<= shift; + +/* +* Calculate a pointer to the naturally aligned 4 byte integer that +* includes our byte of interest, and load its value. +*/ + ptr32 = (volatile ulong *)((ulong )ptr & ~0x3 ); + + __asm__ __volatile__ ( +"0: lr.w %0, %2\n" +" and %1, %0, %z3\n" +" bne %1, %z5, 1f\n" +" and %1, %0, %z4\n" +" or %1, %1, %z6\n" +" sc.w.rl %1, %1, %2\n" +" bnez %1, 0b\n" +" fence w, rw\n" +"1:\n" + :"=&r" (ret),"=&r" (temp),"+A" (*ptr32) + :"rJ" (mask),"rJ" (~mask),"rJ" (old),"rJ" (new) + :"memory" ); + +return (ret & mask) >> shift; +}#define __cmpxchg (ptr, old, new, size) \ ({ \ __typeof__(ptr) __ptr = (ptr); \
@@ -238,6 +410,11 @@ static inline ulong __xchg16_relaxed(ulong new, void *ptr) __typeof__(*(ptr)) __ret; \ registerunsigned int __rc; \switch (size) { \ +case 1 : \ + __ret = (__typeof__ (*(ptr ))) \ +__cmpxchg_small (__ptr, (ulong )__old, \ + (ulong )__new, (ulong )size); \ +break ; \case 4 : \ __asm__ __volatile__ ( \ "0: lr.w %0, %2\n" \
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 15/19 RISC-V: paravirt: pvqspinlock: Add SBI implementation
In reply to [email protected]From: Guo Ren <[email protected]>
Implement pv_kick with SBI implementation, and add SBI_EXT_PVLOCK extension detection.
--- arch/riscv/include/asm/sbi.h | 6 ++++++ arch/riscv/kernel/paravirt.c | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-)
arch/riscv/include/asm/sbi.h +6 -0
@@ -31,6 +31,7 @@ enum sbi_ext_id {SBI_EXT_SRST =0x53525354 ,SBI_EXT_PMU =0x504D55 ,SBI_EXT_STA =0x535441 , +SBI_EXT_PVLOCK =0xAB0401 ,/* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000,
@@ -244,6 +245,11 @@ enum sbi_pmu_ctr_type {/* Flags defined for counter stop function */ #define SBI_PMU_STOP_FLAG_RESET (1 << 0) +/* SBI PVLOCK (kick cpu out of wfi) */ +enum sbi_ext_pvlock_fid { +SBI_EXT_PVLOCK_KICK_CPU =0 , +}; +/* SBI STA (steal-time accounting) extension */ enum sbi_ext_sta_fid {SBI_EXT_STA_STEAL_TIME_SET_SHMEM =0 ,
arch/riscv/kernel/paravirt.c +6 -1
@@ -136,6 +136,8 @@ int __init pv_time_init(void)void pv_kick (int cpu) { +sbi_ecall (SBI_EXT_PVLOCK ,SBI_EXT_PVLOCK_KICK_CPU , +cpuid_to_hartid_map (cpu),0 ,0 ,0 ,0 ,0 );return ; }
@@ -150,7 +152,7 @@ void pv_wait(u8 *ptr, u8 val)if (READ_ONCE (*ptr) != val) goto out; -/* wait_for_interrupt(); */ +wait_for_interrupt (); out:local_irq_restore (flags); }
@@ -186,6 +188,9 @@ void __init pv_qspinlock_init(void)if (sbi_get_firmware_id () !=SBI_EXT_BASE_IMPL_ID_KVM )return ; +if (!sbi_probe_extension (SBI_EXT_PVLOCK )) +return ; +pr_info ("PV qspinlocks enabled\n" );__pv_init_lock_hash ();
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 16/19 RISC-V: paravirt: pvqspinlock: Add kconfig entry
In reply to [email protected]From: Guo Ren <[email protected]>
Add kconfig entry for paravirt_spinlock, an unfair qspinlock virtualization-friendly backend, by halting the virtual CPU rather than spinning.
--- arch/riscv/Kconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/riscv/Kconfig +12 -0
@@ -770,6 +770,7 @@ config RELOCATABLE config PARAVIRT bool "Enable paravirtualization code" depends on RISCV_SBI + select PARAVIRT_SPINLOCKS default y help This changes the kernel so it can modify itself when it is run
@@ -788,6 +789,17 @@ config PARAVIRT_TIME_ACCOUNTING If in doubt, say N here. +config PARAVIRT_SPINLOCKS + bool "Paravirtualization layer for spinlocks" + depends on PARAVIRT && SMP + help + Paravirtualized spinlocks allow a unfair qspinlock to replace the + test-set kvm-guest virt spinlock implementation with something + virtualization-friendly, for example, halt the virtual CPU rather + than spinning. + + If you are unsure how to answer this question, answer Y. + endmenu # "Kernel features" menu "Boot options"
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 17/19 RISC-V: paravirt: pvqspinlock: Add trace point for pv_kick/wait
In reply to [email protected]From: Guo Ren <[email protected]>
Add trace point for pv_kick/wait, here is the output:
entries-in-buffer/entries-written: 33927/33927 #P:12
_-----=> irqs-off/BH-disabled
/ _----=> need-resched
| / _---=> hardirq/softirq
|| / _--=> preempt-depth
||| / _-=> migrate-disable
|||| / delay
TASK-PID CPU# ||||| TIMESTAMP FUNCTION
| | | ||||| | |
sh-100 [001] d..2. 28.312294: pv_wait: cpu 1 out of wfi
<idle>-0 [000] d.h4. 28.322030: pv_kick: cpu 0 kick target cpu 1
sh-100 [001] d..2. 30.982631: pv_wait: cpu 1 out of wfi
<idle>-0 [000] d.h4. 30.993289: pv_kick: cpu 0 kick target cpu 1
sh-100 [002] d..2. 44.987573: pv_wait: cpu 2 out of wfi
<idle>-0 [000] d.h4. 44.989000: pv_kick: cpu 0 kick target cpu 2
<idle>-0 [003] d.s3. 51.593978: pv_kick: cpu 3 kick target cpu 4
rcu_sched-15 [004] d..2. 51.595192: pv_wait: cpu 4 out of wfi
lock_torture_wr-115 [004] ...2. 52.656482: pv_kick: cpu 4 kick target cpu 2
lock_torture_wr-113 [002] d..2. 52.659146: pv_wait: cpu 2 out of wfi
lock_torture_wr-114 [008] d..2. 52.659507: pv_wait: cpu 8 out of wfi
lock_torture_wr-114 [008] d..2. 52.663503: pv_wait: cpu 8 out of wfi
lock_torture_wr-113 [002] ...2. 52.666128: pv_kick: cpu 2 kick target cpu 8
lock_torture_wr-114 [008] d..2. 52.667261: pv_wait: cpu 8 out of wfi
lock_torture_wr-114 [009] .n.2. 53.141515: pv_kick: cpu 9 kick target cpu 11
lock_torture_wr-113 [002] d..2. 53.143339: pv_wait: cpu 2 out of wfi
lock_torture_wr-116 [007] d..2. 53.143412: pv_wait: cpu 7 out of wfi
lock_torture_wr-118 [000] d..2. 53.143457: pv_wait: cpu 0 out of wfi
lock_torture_wr-115 [008] d..2. 53.143481: pv_wait: cpu 8 out of wfi
lock_torture_wr-117 [011] d..2. 53.143522: pv_wait: cpu 11 out of wfi
lock_torture_wr-117 [011] ...2. 53.143987: pv_kick: cpu 11 kick target cpu 8
lock_torture_wr-115 [008] ...2. 53.144269: pv_kick: cpu 8 kick target cpu 7--- arch/riscv/kernel/paravirt.c | 8 +++ .../kernel/trace_events_filter_paravirt.h | 60 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 arch/riscv/kernel/trace_events_filter_paravirt.h
arch/riscv/kernel/paravirt.c +8 -0
@@ -134,10 +134,16 @@ int __init pv_time_init(void)#ifdef CONFIG_PARAVIRT_SPINLOCKS #include <asm/qspinlock_paravirt.h> +#define CREATE_TRACE_POINTS +#include "trace_events_filter_paravirt.h" +void pv_kick (int cpu) {sbi_ecall (SBI_EXT_PVLOCK ,SBI_EXT_PVLOCK_KICK_CPU ,cpuid_to_hartid_map (cpu),0 ,0 ,0 ,0 ,0 ); + +trace_pv_kick (smp_processor_id (), cpu); +return ; }
@@ -153,6 +159,8 @@ void pv_wait(u8 *ptr, u8 val) goto out;wait_for_interrupt (); + +trace_pv_wait (smp_processor_id ()); out:local_irq_restore (flags); }
arch/riscv/kernel/trace_events_filter_paravirt.h new file +60 -0
60 lines not on this page · raw message
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 18/19 locking/qspinlock: Move pv_ops into x86 directory
In reply to [email protected]From: Guo Ren <[email protected]>
The pv_ops belongs to x86 custom infrastructure and cleans up the cna_configure_spin_lock_slowpath() with standard code. This is preparation for riscv support CNA qspoinlock.
--- arch/x86/include/asm/qspinlock.h | 3 ++- arch/x86/kernel/alternative.c | 6 +++++- kernel/locking/qspinlock_cna.h | 14 ++++++-------- 3 files changed, 13 insertions(+), 10 deletions(-)
arch/x86/include/asm/qspinlock.h +2 -1
@@ -28,7 +28,8 @@ static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lo }#ifdef CONFIG_NUMA_AWARE_SPINLOCKS -extern void cna_configure_spin_lock_slowpath (void ); +extern bool cna_configure_spin_lock_slowpath (void ); +extern void __cna_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val);#endif #ifdef CONFIG_PARAVIRT_SPINLOCKS
arch/x86/kernel/alternative.c +5 -1
@@ -1538,7 +1538,11 @@ void __init alternative_instructions(void)paravirt_set_cap ();#if defined(CONFIG_NUMA_AWARE_SPINLOCKS ) -cna_configure_spin_lock_slowpath (); +if (pv_ops.lock.queued_spin_lock_slowpath == native_queued_spin_lock_slowpath) { +if (cna_configure_spin_lock_slowpath ()) + pv_ops.lock.queued_spin_lock_slowpath = + __cna_queued_spin_lock_slowpath; + }#endif /*
kernel/locking/qspinlock_cna.h +6 -8
@@ -406,20 +406,18 @@ void __cna_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);* multiple NUMA nodes in native environment, unless the user has * overridden this default behavior by setting the numa_spinlock flag. */ -void __init cna_configure_spin_lock_slowpath (void ) +bool __init cna_configure_spin_lock_slowpath (void ) {if (numa_spinlock_flag <0 ) -return ; +return false; -if (numa_spinlock_flag ==0 && (nr_node_ids <2 || - pv_ops.lock.queued_spin_lock_slowpath != - native_queued_spin_lock_slowpath)) -return ; +if (numa_spinlock_flag ==0 && nr_node_ids <2 ) +return false;cna_init_nodes (); - pv_ops.lock.queued_spin_lock_slowpath = __cna_queued_spin_lock_slowpath; -pr_info ("Enabling CNA spinlock\n" ); + +return true; }
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
maintainerPATCH v10 19/19 locking/qspinlock: riscv: Add Compact NUMA-aware lock support
In reply to [email protected]From: Guo Ren <[email protected]>
Connect riscv to Compact NUMA-aware lock (CNA), which uses PRARAVIRT_SPINLOCKS static_call hooks. See numa_spinlock= of Documentation/admin-guide/kernel-parameters.txt for trying.
--- arch/riscv/Kconfig | 18 ++++++++++++++++++ arch/riscv/include/asm/qspinlock.h | 5 +++++ arch/riscv/kernel/paravirt.c | 12 +++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-)
arch/riscv/Kconfig +18 -0
@@ -800,6 +800,24 @@ config PARAVIRT_SPINLOCKS If you are unsure how to answer this question, answer Y. +config NUMA_AWARE_SPINLOCKS + bool "Numa-aware spinlocks" + depends on NUMA + depends on QUEUED_SPINLOCKS + depends on 64BIT + # For now, we depend on PARAVIRT_SPINLOCKS to make the patching work. + depends on PARAVIRT_SPINLOCKS + default y + help + Introduce NUMA (Non Uniform Memory Access) awareness into + the slow path of spinlocks. + + In this variant of qspinlock, the kernel will try to keep the lock + on the same node, thus reducing the number of remote cache misses, + while trading some of the short term fairness for better performance. + + Say N if you want absolute first come first serve fairness. + endmenu # "Kernel features" menu "Boot options"
arch/riscv/include/asm/qspinlock.h +5 -0
@@ -12,6 +12,11 @@ void native_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);void __pv_init_lock_hash (void );void __pv_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val); +#ifdef CONFIG_NUMA_AWARE_SPINLOCKS +bool cna_configure_spin_lock_slowpath (void ); +void __cna_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val); +#endif +static inline void queued_spin_lock_slowpath (struct qspinlock *lock,u32 val) {static_call (pv_queued_spin_lock_slowpath)(lock, val);
arch/riscv/kernel/paravirt.c +11 -1
@@ -193,8 +193,10 @@ void __init pv_qspinlock_init(void)if (num_possible_cpus () ==1 )return ; -if (sbi_get_firmware_id () !=SBI_EXT_BASE_IMPL_ID_KVM ) +if (sbi_get_firmware_id () !=SBI_EXT_BASE_IMPL_ID_KVM ) { + goto cna_qspinlock;return ; + }if (!sbi_probe_extension (SBI_EXT_PVLOCK ))return ;
@@ -204,5 +206,13 @@ void __init pv_qspinlock_init(void)static_call_update (pv_queued_spin_lock_slowpath, __pv_queued_spin_lock_slowpath);static_call_update (pv_queued_spin_unlock, __pv_queued_spin_unlock); +return ; + +cna_qspinlock: +#ifdef CONFIG_NUMA_AWARE_SPINLOCKS +if (cna_configure_spin_lock_slowpath ()) +static_call_update (pv_queued_spin_lock_slowpath, + __cna_queued_spin_lock_slowpath); +#endif }#endif
Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH V10 07/19] riscv: qspinlock: errata: Introduce ERRATA_THEAD_QSPINLOCK
In reply to [email protected]Hey Guo Ren,
Quoted 31 lines · On Wed, Aug 02, 2023 at 12:46:49PM -0400, [email protected] wrote:
From: Guo Ren <[email protected]>
According to qspinlock requirements, RISC-V gives out a weak LR/SC forward progress guarantee which does not satisfy qspinlock. But many vendors could produce stronger forward guarantee LR/SC to ensure the
xchg_tailcould be finished in time on any kind of hart. T-HEAD is the vendor which implements strong forward guarantee LR/SC instruction pairs, so enable qspinlock for T-HEAD with errata help.T-HEAD early version of processors has the merge buffer delay problem, so we need
ERRATA_WRITEONCEto support qspinlock.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>--- arch/riscv/Kconfig.errata | 13 +++++++++++++ arch/riscv/errata/thead/errata.c | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 20 ++++++++++++++++++++ arch/riscv/include/asm/vendorid_list.h | 3 ++- arch/riscv/kernel/cpufeature.c | 3 ++- 5 files changed, 61 insertions(+), 2 deletions(-)arch/riscv/Kconfig.errata +0
-0@@ -96,4 +96,17 @@ config ERRATA_THEAD_WRITE_ONCE If you don't know what to do here, say "Y".
arch/riscv/Kconfig.errata config ERRATA_THEAD_WRITE_ONCE · line 108
Quoted 10 lines
arch/riscv/Kconfig.errata +10
-0+config ERRATA_THEAD_QSPINLOCK + bool "Apply T-Head queued spinlock errata" + depends on ERRATA_THEAD + default y + help + The T-HEAD C9xx processors implement strong fwd guarantee LR/SC to + match the xchg_tail requirement of qspinlock. + + This will apply the QSPINLOCK errata to handle the non-standard + behavior via using qspinlock instead of ticket_lock.
Whatever about the acceptability of anything else in this series, having _stronger_ guarantees is not an erratum, is it? We should not abuse the errata stuff for this IMO.
arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 347
Quoted 12 lines
arch/riscv/kernel/cpufeature.c +2
-1@@ -342,7 +342,8 @@ void __init riscv_fill_hwcap(void)* spinlock value, the only way is to change from queued_spinlock to * ticket_spinlock, but can not be vice. */ -if (!force_qspinlock) { +if (!force_qspinlock && + !riscv_has_errata_thead_qspinlock ()) {set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa);
Is this a generic vendor extension (lol @ that misnomer) or is it an erratum? Make your mind up please. As has been said on other series, NAK to using march/vendor/imp IDs for feature probing.
I've got some thoughts on other parts of this series too, but I'm not going to spend time on it unless the locking people and Palmer ascent to this series.
Cheers,
Conor.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Guo Ren
maintainerRe: [PATCH V10 07/19] riscv: qspinlock: errata: Introduce ERRATA_THEAD_QSPINLOCK
Quoted 50 lines · Conor Dooley
Hey Guo Ren,
Quoted 31 lines · On Wed, Aug 02, 2023 at 12:46:49PM -0400, [email protected] wrote:
From: Guo Ren <[email protected]>
According to qspinlock requirements, RISC-V gives out a weak LR/SC forward progress guarantee which does not satisfy qspinlock. But many vendors could produce stronger forward guarantee LR/SC to ensure the
xchg_tailcould be finished in time on any kind of hart. T-HEAD is the vendor which implements strong forward guarantee LR/SC instruction pairs, so enable qspinlock for T-HEAD with errata help.T-HEAD early version of processors has the merge buffer delay problem, so we need
ERRATA_WRITEONCEto support qspinlock.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>--- arch/riscv/Kconfig.errata | 13 +++++++++++++ arch/riscv/errata/thead/errata.c | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 20 ++++++++++++++++++++ arch/riscv/include/asm/vendorid_list.h | 3 ++- arch/riscv/kernel/cpufeature.c | 3 ++- 5 files changed, 61 insertions(+), 2 deletions(-)arch/riscv/Kconfig.errata +0
-0@@ -96,4 +96,17 @@ config ERRATA_THEAD_WRITE_ONCE If you don't know what to do here, say "Y".arch/riscv/Kconfig.errata config ERRATA_THEAD_WRITE_ONCE · line 108
Quoted 10 lines
arch/riscv/Kconfig.errata +10
-0+config ERRATA_THEAD_QSPINLOCK + bool "Apply T-Head queued spinlock errata" + depends on ERRATA_THEAD + default y + help + The T-HEAD C9xx processors implement strong fwd guarantee LR/SC to + match the xchg_tail requirement of qspinlock. + + This will apply the QSPINLOCK errata to handle the non-standard + behavior via using qspinlock instead of ticket_lock.Whatever about the acceptability of anything else in this series, having _stronger_ guarantees is not an erratum, is it? We should not abuse the errata stuff for this IMO.
Quoted 16 lines
arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 347
Quoted 12 lines
arch/riscv/kernel/cpufeature.c +2
-1@@ -342,7 +342,8 @@ void __init riscv_fill_hwcap(void)* spinlock value, the only way is to change from queued_spinlock to * ticket_spinlock, but can not be vice. */ -if (!force_qspinlock) { +if (!force_qspinlock && + !riscv_has_errata_thead_qspinlock ()) {set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa);Is this a generic vendor extension (lol @ that misnomer) or is it an erratum? Make your mind up please. As has been said on other series, NAK to using march/vendor/imp IDs for feature probing.
The RISCV_ISA_EXT_XTICKETLOCK is a feature extension number, and it's set by default for forward-compatible. We also define a vendor extension (riscv_has_errata_thead_qspinlock) to force all our processors to use qspinlock; others still stay on ticket_lock.
The only possible changing direction is from qspinlock to ticket_lock because ticket_lock would dirty the lock value, which prevents changing to qspinlock next. So startup with qspinlock and change to ticket_lock before smp up. You also could use cmdline to try qspinlock (force_qspinlock).
Quoted 7 lines
I've got some thoughts on other parts of this series too, but I'm not going to spend time on it unless the locking people and Palmer ascent to this series.
Cheers,
Conor.
Signature · 2 lines
Best Regards Guo Ren
Re: [PATCH V10 07/19] riscv: qspinlock: errata: Introduce ERRATA_THEAD_QSPINLOCK
Quoted 21 lines · Guo Ren
Quoted 19 lines · Conor Dooley
Quoted 7 lines · On Wed, Aug 02, 2023 at 12:46:49PM -0400, [email protected] wrote:
From: Guo Ren <[email protected]>
arch/riscv/kernel/cpufeature.c +0
-0arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 347
Quoted 7 lines
arch/riscv/kernel/cpufeature.c +2
-1@@ -342,7 +342,8 @@ void __init riscv_fill_hwcap(void)* spinlock value, the only way is to change from queued_spinlock to * ticket_spinlock, but can not be vice. */ -if (!force_qspinlock) { +if (!force_qspinlock && + !riscv_has_errata_thead_qspinlock ()) {set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa);Is this a generic vendor extension (lol @ that misnomer) or is it an erratum? Make your mind up please. As has been said on other series, NAK to using march/vendor/imp IDs for feature probing.
Quoted 1 line
The
RISCV_ISA_EXT_XTICKETLOCKis a feature extension number,
No, that is not what "ISA_EXT" means, nor what the X in "XTICKETLOCK" would imply.
The comment above these reads:
These macros represent the logical IDs of each multi-letter RISC-V ISA extension and are used in the ISA bitmap.
Quoted 4 lines
and it's
set by default for forward-compatible. We also define a vendor extension (riscv_has_errata_thead_qspinlock) to force all our processors to use qspinlock; others still stay onticket_lock.
No, "riscv_has_errata_thead_qspinlock()" would be an _erratum_, not a vendor extension. We need to have a discussion about how to support non-standard extensions etc, not abuse errata. That discussion has been started on the v0.7.1 vector patches, but has not made progress yet.
Quoted 5 lines
The only possible changing direction is from qspinlock to
ticket_lockbecauseticket_lockwould dirty the lock value, which prevents changing to qspinlock next. So startup with qspinlock and change toticket_lockbefore smp up. You also could use cmdline to try qspinlock (force_qspinlock).
I don't see what the relevance of this is, sorry. I am only commenting on how you are deciding that the hardware is capable of using qspinlocks, I don't intend getting into the detail unless the powers that be deem this series worthwhile, as I mentioned:
Quoted 3 lines
Quoted 3 lines
I've got some thoughts on other parts of this series too, but I'm not going to spend time on it unless the locking people and Palmer ascent to this series.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Guo Ren
maintainerRe: [PATCH V10 07/19] riscv: qspinlock: errata: Introduce ERRATA_THEAD_QSPINLOCK
Quoted 37 lines · Conor Dooley
Quoted 21 lines · Guo Ren
Quoted 19 lines · Conor Dooley
Quoted 7 lines · On Wed, Aug 02, 2023 at 12:46:49PM -0400, [email protected] wrote:
From: Guo Ren <[email protected]>
arch/riscv/kernel/cpufeature.c +0
-0arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 347
Quoted 7 lines
arch/riscv/kernel/cpufeature.c +2
-1@@ -342,7 +342,8 @@ void __init riscv_fill_hwcap(void)* spinlock value, the only way is to change from queued_spinlock to * ticket_spinlock, but can not be vice. */ -if (!force_qspinlock) { +if (!force_qspinlock && + !riscv_has_errata_thead_qspinlock ()) {set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa);Is this a generic vendor extension (lol @ that misnomer) or is it an erratum? Make your mind up please. As has been said on other series, NAK to using march/vendor/imp IDs for feature probing.
Quoted 1 line
The
RISCV_ISA_EXT_XTICKETLOCKis a feature extension number,No, that is not what "
ISA_EXT" means, nor what the X in "XTICKETLOCK" would imply.The comment above these reads:
These macros represent the logical IDs of each multi-letter RISC-V ISA extension and are used in the ISA bitmap.Quoted 4 lines
and it's
set by default for forward-compatible. We also define a vendor extension (riscv_has_errata_thead_qspinlock) to force all our processors to use qspinlock; others still stay onticket_lock.
Quoted 4 lines
No, "
riscv_has_errata_thead_qspinlock()" would be an _erratum_, not a vendor extension. We need to have a discussion about how to support non-standard extensions etc, not abuse errata. That discussion has been started on the v0.7.1 vector patches, but has not made progress yet.
You convinced me, yes, I abuse errata here. I would change to Linux standard static_key mechanism next.
Quoted 7 lines
Quoted 6 lines
The only possible changing direction is from qspinlock to
ticket_lockbecauseticket_lockwould dirty the lock value, which prevents changing to qspinlock next. So startup with qspinlock and change toticket_lockbefore smp up. You also could use cmdline to try qspinlock (force_qspinlock).
Quoted 8 lines
I don't see what the relevance of this is, sorry. I am only commenting on how you are deciding that the hardware is capable of using qspinlocks, I don't intend getting into the detail unless the powers that be deem this series worthwhile, as I mentioned:
Quoted 3 lines
Quoted 3 lines
I've got some thoughts on other parts of this series too, but I'm not going to spend time on it unless the locking people and Palmer ascent to this series.
--
Best Regards
Guo Ren
Re: [PATCH V10 07/19] riscv: qspinlock: errata: Introduce ERRATA_THEAD_QSPINLOCK
In reply to [email protected]Quoted 33 lines · On Wed, Aug 2, 2023, at 12:46 PM, [email protected] wrote:
From: Guo Ren <[email protected]>
According to qspinlock requirements, RISC-V gives out a weak LR/SC forward progress guarantee which does not satisfy qspinlock. But many vendors could produce stronger forward guarantee LR/SC to ensure the
xchg_tailcould be finished in time on any kind of hart. T-HEAD is the vendor which implements strong forward guarantee LR/SC instruction pairs, so enable qspinlock for T-HEAD with errata help.T-HEAD early version of processors has the merge buffer delay problem, so we need
ERRATA_WRITEONCEto support qspinlock.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>--- arch/riscv/Kconfig.errata | 13 +++++++++++++ arch/riscv/errata/thead/errata.c | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 20 ++++++++++++++++++++ arch/riscv/include/asm/vendorid_list.h | 3 ++- arch/riscv/kernel/cpufeature.c | 3 ++- 5 files changed, 61 insertions(+), 2 deletions(-)arch/riscv/Kconfig.errata +2
-0@@ -96,4 +96,17 @@ config ERRATA_THEAD_WRITE_ONCE If you don't know what to do here, say "Y". +config ERRATA_THEAD_QSPINLOCK + bool "Apply T-Head queued spinlock errata"
arch/riscv/Kconfig.errata config ERRATA_THEAD_WRITE_ONCE · line 110
Quoted 10 lines
arch/riscv/Kconfig.errata +10
-0+ depends on ERRATA_THEAD + default y + help + The T-HEAD C9xx processors implement strong fwd guarantee LR/SC to + match the xchg_tail requirement of qspinlock. + + This will apply the QSPINLOCK errata to handle the non-standard + behavior via using qspinlock instead of ticket_lock. + + If you don't know what to do here, say "Y".
If this is to be applied, I would like to see a detailed explanation somewhere, preferably with citations, of:
(a) The memory model requirements for qspinlock
(b) Why, with arguments, RISC-V does not architecturally meet (a)
(c) Why, with arguments, T-HEAD C9xx meets (a)
(d) Why at least one other architecture which defines ARCH_USE_QUEUED_SPINLOCKS meets (a)
As far as I can tell, the RISC-V guarantees concerning constrained LR/SC loops (livelock freedom but no starvation freedom) are exactly the same as those in Armv8 (as of 0487F.c) for equivalent loops, and xchg_tail compiles to a constrained LR/SC loop with guaranteed eventual success (with -O1). Clearly you disagree; I would like to see your perspective.
-s
Quoted 113 lines
arch/riscv/Kconfig.errata +1
-0+ endmenu # "CPU errata selection"arch/riscv/errata/thead/errata.c +24
-0@@ -86,6 +86,27 @@ static bool errata_probe_write_once(unsigned int stage,return false; } +static bool errata_probe_qspinlock (unsigned int stage, +unsigned long arch_id,unsigned long impid) +{ +if (!IS_ENABLED (CONFIG_ERRATA_THEAD_QSPINLOCK )) +return false; + +/* +* The queued_spinlock torture would get in livelock without +* ERRATA_THEAD_WRITE_ONCE fixup for the early versions of T-HEAD +* processors. +*/ +if (arch_id ==0 && impid ==0 && + !IS_ENABLED (CONFIG_ERRATA_THEAD_WRITE_ONCE )) +return false; + +if (stage ==RISCV_ALTERNATIVES_EARLY_BOOT ) +return true; + +return false; +} +static u32 thead_errata_probe (unsigned int stage,unsigned long archid,unsigned long impid) {@@ -103,6 +124,9 @@ static u32 thead_errata_probe(unsigned int stage,if (errata_probe_write_once (stage, archid, impid)) cpu_req_errata |=BIT (ERRATA_THEAD_WRITE_ONCE ); +if (errata_probe_qspinlock (stage, archid, impid)) + cpu_req_errata |=BIT (ERRATA_THEAD_QSPINLOCK ); +return cpu_req_errata; }arch/riscv/include/asm/errata_list.h +20
-0@@ -141,6 +141,26 @@ asm volatile(ALTERNATIVE( \ : "=r" (__ovl) : \ : "memory") +static __always_inline bool +riscv_has_errata_thead_qspinlock (void ) +{ +if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) { +asm_volatile_goto ( +ALTERNATIVE ( +"j %l[l_no]" ,"nop" , +THEAD_VENDOR_ID , +ERRATA_THEAD_QSPINLOCK , +CONFIG_ERRATA_THEAD_QSPINLOCK ) + : : : : l_no); + }else { + goto l_no; + } + +return true; +l_no: +return false; +} +#endif /* __ASSEMBLY__ */ #endif arch/riscv/include/asm/vendorid_list.h +2
-1@@ -19,7 +19,8 @@#define ERRATA_THEAD_CMO 1#define ERRATA_THEAD_PMU 2#define ERRATA_THEAD_WRITE_ONCE 3 -#define ERRATA_THEAD_NUMBER 4 +#define ERRATA_THEAD_QSPINLOCK 4 +#define ERRATA_THEAD_NUMBER 5#endif #endif arch/riscv/kernel/cpufeature.c +2
-1@@ -342,7 +342,8 @@ void __init riscv_fill_hwcap(void)* spinlock value, the only way is to change from queued_spinlock to * ticket_spinlock, but can not be vice. */ -if (!force_qspinlock) { +if (!force_qspinlock && + !riscv_has_errata_thead_qspinlock ()) {set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa); }#endif Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Guo Ren
maintainerRe: [PATCH V10 07/19] riscv: qspinlock: errata: Introduce ERRATA_THEAD_QSPINLOCK
Quoted 48 lines · Stefan O'Rear
Quoted 33 lines · On Wed, Aug 2, 2023, at 12:46 PM, [email protected] wrote:
From: Guo Ren <[email protected]>
According to qspinlock requirements, RISC-V gives out a weak LR/SC forward progress guarantee which does not satisfy qspinlock. But many vendors could produce stronger forward guarantee LR/SC to ensure the
xchg_tailcould be finished in time on any kind of hart. T-HEAD is the vendor which implements strong forward guarantee LR/SC instruction pairs, so enable qspinlock for T-HEAD with errata help.T-HEAD early version of processors has the merge buffer delay problem, so we need
ERRATA_WRITEONCEto support qspinlock.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>--- arch/riscv/Kconfig.errata | 13 +++++++++++++ arch/riscv/errata/thead/errata.c | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 20 ++++++++++++++++++++ arch/riscv/include/asm/vendorid_list.h | 3 ++- arch/riscv/kernel/cpufeature.c | 3 ++- 5 files changed, 61 insertions(+), 2 deletions(-)arch/riscv/Kconfig.errata +2
-0@@ -96,4 +96,17 @@ config ERRATA_THEAD_WRITE_ONCE If you don't know what to do here, say "Y". +config ERRATA_THEAD_QSPINLOCK + bool "Apply T-Head queued spinlock errata"arch/riscv/Kconfig.errata config ERRATA_THEAD_WRITE_ONCE · line 110
Quoted 10 lines
arch/riscv/Kconfig.errata +10
-0+ depends on ERRATA_THEAD + default y + help + The T-HEAD C9xx processors implement strong fwd guarantee LR/SC to + match the xchg_tail requirement of qspinlock. + + This will apply the QSPINLOCK errata to handle the non-standard + behavior via using qspinlock instead of ticket_lock. + + If you don't know what to do here, say "Y".If this is to be applied, I would like to see a detailed explanation somewhere, preferably with citations, of:
Quoted 1 line
(a) The memory model requirements for qspinlock
These were written in commit: a8ad07e5240 ("asm-generic: qspinlock: Indicate the use of mixed-size atomics"). For riscv, the most controversial point is xchg_tail() implementation for native queued spinlock.
Quoted 1 line
(b) Why, with arguments, RISC-V does not architecturally meet (a)
In the spec "Eventual Success of Store-Conditional Instructions":
"By contrast, if other harts or devices continue to write to that reservation set, it is not guaranteed that any hart will exit its LR/SC loop."
1. The arch_spinlock_t is 32-bit width, and it contains LOCK_PENDING part and IDX_TAIL part.
- LOCK: lock holder
- PENDING: next waiter (Only once per contended situation)
- IDX: nested context (normal, hwirq, softirq, nmi)
- TAIL: last contended cpu The xchg_tail operate on IDX_TAIL part, so there is no guarantee on "NO" "other harts or devices continue to write to that reservation set".
2. When you do lock torture test, you may see a long contended ring queue:
xchg_tail
+-----> CPU4 (big core)
|
CPU3 (lock holder) -> CPU1 (mcs queued) -> CPU2 (mcs queued) ----+-----> CPU0 (little core)
| |
| +-----> CPU5 (big core)
| |
+--locktorture release lock (spin_unlock) and spin_lock again --+-----> CPU3 (big core) If CPU0 doesn't have a strong fwd guarantee, xhg_tail is consistently failed.
Quoted 3 lines
(c) Why, with arguments, T-HEAD C9xx meets (a)
(d) Why at least one other architecture which definesARCH_USE_QUEUED_SPINLOCKSmeets (a)
I can't give the C9xx microarch implementation detail. But many open-source riscv cores have provided strong forward progress guarantee LR/SC implementation [1] [2]. But I would say these implementations are too rude, which makes LR send a cacheline unique interconnect request. It satisfies xchg_tail but not cmpxchg & cond_load. CPU vendors should carefully consider your LR/SC fwd guarantee implementation.
[1]: https://github.com/riscv-boom/riscv-boom/blob/v3.0.0/src/main/scala/lsu/dcache.scala#L650
[2]: https://github.com/OpenXiangShan/XiangShan/blob/v1.0/src/main/scala/xiangshan/cache/MainPipe.scala#L470
Quoted 6 lines
As far as I can tell, the RISC-V guarantees concerning constrained LR/SC loops (livelock freedom but no starvation freedom) are exactly the same as those in Armv8 (as of
0487F.c) for equivalent loops, andxchg_tailcompiles to a constrained LR/SC loop with guaranteed eventual success (with -O1). Clearly you disagree; I would like to see your perspective.
For Armv8, I would use LSE for the lock-contended scenario. Ref this commit 0ea366f5e1b6: ("arm64: atomics: prefetch the destination word for write prior to stxr").
Quoted 116 lines
-s
Quoted 113 lines
arch/riscv/Kconfig.errata +1
-0+ endmenu # "CPU errata selection"arch/riscv/errata/thead/errata.c +24
-0@@ -86,6 +86,27 @@ static bool errata_probe_write_once(unsigned int stage,return false; } +static bool errata_probe_qspinlock (unsigned int stage, +unsigned long arch_id,unsigned long impid) +{ +if (!IS_ENABLED (CONFIG_ERRATA_THEAD_QSPINLOCK )) +return false; + +/* +* The queued_spinlock torture would get in livelock without +* ERRATA_THEAD_WRITE_ONCE fixup for the early versions of T-HEAD +* processors. +*/ +if (arch_id ==0 && impid ==0 && + !IS_ENABLED (CONFIG_ERRATA_THEAD_WRITE_ONCE )) +return false; + +if (stage ==RISCV_ALTERNATIVES_EARLY_BOOT ) +return true; + +return false; +} +static u32 thead_errata_probe (unsigned int stage,unsigned long archid,unsigned long impid) {@@ -103,6 +124,9 @@ static u32 thead_errata_probe(unsigned int stage,if (errata_probe_write_once (stage, archid, impid)) cpu_req_errata |=BIT (ERRATA_THEAD_WRITE_ONCE ); +if (errata_probe_qspinlock (stage, archid, impid)) + cpu_req_errata |=BIT (ERRATA_THEAD_QSPINLOCK ); +return cpu_req_errata; }arch/riscv/include/asm/errata_list.h +20
-0@@ -141,6 +141,26 @@ asm volatile(ALTERNATIVE( \ : "=r" (__ovl) : \ : "memory") +static __always_inline bool +riscv_has_errata_thead_qspinlock (void ) +{ +if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) { +asm_volatile_goto ( +ALTERNATIVE ( +"j %l[l_no]" ,"nop" , +THEAD_VENDOR_ID , +ERRATA_THEAD_QSPINLOCK , +CONFIG_ERRATA_THEAD_QSPINLOCK ) + : : : : l_no); + }else { + goto l_no; + } + +return true; +l_no: +return false; +} +#endif /* __ASSEMBLY__ */ #endif arch/riscv/include/asm/vendorid_list.h +2
-1@@ -19,7 +19,8 @@#define ERRATA_THEAD_CMO 1#define ERRATA_THEAD_PMU 2#define ERRATA_THEAD_WRITE_ONCE 3 -#define ERRATA_THEAD_NUMBER 4 +#define ERRATA_THEAD_QSPINLOCK 4 +#define ERRATA_THEAD_NUMBER 5#endif #endif arch/riscv/kernel/cpufeature.c +2
-1@@ -342,7 +342,8 @@ void __init riscv_fill_hwcap(void)* spinlock value, the only way is to change from queued_spinlock to * ticket_spinlock, but can not be vice. */ -if (!force_qspinlock) { +if (!force_qspinlock && + !riscv_has_errata_thead_qspinlock ()) {set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa); }#endif Signature · 7 lines
2.36.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Waiman Long
reviewerRe: [PATCH V10 04/19] riscv: qspinlock: Add basic queued_spinlock support
In reply to [email protected]Quoted 7 lines · On 8/2/23 12:46, [email protected] wrote:
\arch/riscv/include/asm/spinlock.h new file +0
-0
arch/riscv/include/asm/spinlock.h line 10
Quoted 10 lines
arch/riscv/include/asm/spinlock.h new file +10
-0@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_RISCV_SPINLOCK_H +#define __ASM_RISCV_SPINLOCK_H + +#ifdef CONFIG_QUEUED_SPINLOCKS +#define _Q_PENDING_LOOPS (1 << 9) +#endif + +#ifdef CONFIG_QUEUED_SPINLOCKS
You can merge the two "#ifdef CONFIG_QUEUED_SPINLOCKS" into single one to avoid the duplication.
Cheers,
Longman
arch/riscv/include/asm/spinlock.h line 17
Quoted 7 lines
arch/riscv/include/asm/spinlock.h new file +7
-0+#include <asm/qspinlock.h> +#include <asm/qrwlock.h> +#else +#include <asm-generic/spinlock.h> +#endif + +#endif /* __ASM_RISCV_SPINLOCK_H */
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Waiman Long
reviewerRe: [PATCH V10 05/19] riscv: qspinlock: Introduce combo spinlock
In reply to [email protected]Quoted 89 lines · On 8/2/23 12:46, [email protected] wrote:
From: Guo Ren <[email protected]>
Combo spinlock could support queued and ticket in one Linux Image and select them during boot time via errata mechanism. Here is the func size (Bytes) comparison table below:
TYPE : COMBO | TICKET | QUEUED
arch_spin_lock: 106 | 60 | 50arch_spin_unlock: 54 | 36 | 26arch_spin_trylock: 110 | 72 | 54arch_spin_is_locked: 48 | 34 | 20arch_spin_is_contended: 56 | 40 | 24rch_spin_value_unlocked: 48 | 34 | 24One example of disassemble combo
arch_spin_unlock:0xffffffff8000409c <+14>: nop # detour slot 0xffffffff800040a0 <+18>: fence rw,w # queued spinlock start 0xffffffff800040a4 <+22>: sb zero,0(a4) # queued spinlock end 0xffffffff800040a8 <+26>: ld s0,8(sp) 0xffffffff800040aa <+28>: addi sp,sp,16 0xffffffff800040ac <+30>: ret 0xffffffff800040ae <+32>: lw a5,0(a4) # ticket spinlock start 0xffffffff800040b0 <+34>: sext.w a5,a5 0xffffffff800040b2 <+36>: fence rw,w 0xffffffff800040b6 <+40>: addiw a5,a5,1 0xffffffff800040b8 <+42>: slli a5,a5,0x30 0xffffffff800040ba <+44>: srli a5,a5,0x30 0xffffffff800040bc <+46>: sh a5,0(a4) # ticket spinlock end 0xffffffff800040c0 <+50>: ld s0,8(sp) 0xffffffff800040c2 <+52>: addi sp,sp,16 0xffffffff800040c4 <+54>: retThe qspinlock is smaller and faster than ticket-lock when all are in fast-path, and combo spinlock could provide a compatible Linux Image for different micro-arch design (weak/strict fwd guarantee) processors.
Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>--- arch/riscv/Kconfig | 9 +++- arch/riscv/include/asm/hwcap.h | 1 + arch/riscv/include/asm/spinlock.h | 87 ++++++++++++++++++++++++++++++- arch/riscv/kernel/cpufeature.c | 10 ++++ 4 files changed, 104 insertions(+), 3 deletions(-)arch/riscv/Kconfig +8
-1@@ -440,7 +440,7 @@ config NODES_SHIFT choice prompt "RISC-V spinlock type" - default RISCV_TICKET_SPINLOCKS + default RISCV_COMBO_SPINLOCKS config RISCV_TICKET_SPINLOCKS bool "Using ticket spinlock"@@ -452,6 +452,13 @@ config RISCV_QUEUED_SPINLOCKS help Make sure your micro arch LL/SC has a strong forward progress guarantee. Otherwise, stay at ticket-lock. + +config RISCV_COMBO_SPINLOCKS + bool "Using combo spinlock" + depends on SMP && MMU + select ARCH_USE_QUEUED_SPINLOCKS + help + Select queued spinlock or ticket-lock via errata. endchoice config RISCV_ALTERNATIVEarch/riscv/include/asm/hwcap.h +1
-0@@ -54,6 +54,7 @@#define RISCV_ISA_EXT_ZIFENCEI 41#define RISCV_ISA_EXT_ZIHPM 42 +#define RISCV_ISA_EXT_XTICKETLOCK 63#define RISCV_ISA_EXT_MAX 64#define RISCV_ISA_EXT_NAME_LEN_MAX 32arch/riscv/include/asm/spinlock.h +0
-0
arch/riscv/include/asm/spinlock.h line 9
Quoted 3 lines
arch/riscv/include/asm/spinlock.h +0
-0@@ -7,11 +7,94 @@#define _Q_PENDING_LOOPS (1 << 9)#endif
I see why you separated the _Q_PENDING_LOOPS out.
Quoted 9 lines
arch/riscv/include/asm/spinlock.h +9
-0+#ifdef CONFIG_RISCV_COMBO_SPINLOCKS +#include <asm-generic/ticket_spinlock.h> + +#undef arch_spin_is_locked +#undef arch_spin_is_contended +#undef arch_spin_value_unlocked +#undef arch_spin_lock +#undef arch_spin_trylock +#undef arch_spin_unlock
arch/riscv/include/asm/spinlock.h line 28
Quoted 10 lines
arch/riscv/include/asm/spinlock.h +10
-0+ +#include <asm-generic/qspinlock.h> +#include <asm/hwcap.h> + +#undef arch_spin_is_locked +#undef arch_spin_is_contended +#undef arch_spin_value_unlocked +#undef arch_spin_lock +#undef arch_spin_trylock +#undef arch_spin_unlock
Perhaps you can add a macro like __no_arch_spinlock_redefine to disable the various arch_spin_* definition in qspinlock.h and ticket_spinlock.h.
Quoted 79 lines
arch/riscv/include/asm/spinlock.h +66
-2+ +#define COMBO_DETOUR \ + asm_volatile_goto(ALTERNATIVE( \ + "nop", \ + "j %l[ticket_spin_lock]", \ + 0, \ + RISCV_ISA_EXT_XTICKETLOCK, \ + CONFIG_RISCV_COMBO_SPINLOCKS) \ + : : : : ticket_spin_lock); + +static __always_inline void arch_spin_lock(arch_spinlock_t *lock) +{ +COMBO_DETOUR +queued_spin_lock (lock ); +return ; +ticket_spin_lock: +ticket_spin_lock (lock); +} + +static __always_inline bool arch_spin_trylock(arch_spinlock_t *lock) +{ +COMBO_DETOUR + return queued_spin_trylock(lock); +ticket_spin_lock: +return ticket_spin_trylock (lock); +} + +static __always_inline void arch_spin_unlock(arch_spinlock_t *lock) +{ +COMBO_DETOUR +queued_spin_unlock (lock ); +return ; +ticket_spin_lock: +ticket_spin_unlock (lock); +} + +static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock) +{ +COMBO_DETOUR + return queued_spin_value_unlocked(lock); +ticket_spin_lock: +return ticket_spin_value_unlocked (lock); +} + +static __always_inline int arch_spin_is_locked(arch_spinlock_t *lock) +{ +COMBO_DETOUR + return queued_spin_is_locked(lock); +ticket_spin_lock: +return ticket_spin_is_locked (lock); +} + +static __always_inline int arch_spin_is_contended(arch_spinlock_t *lock) +{ +COMBO_DETOUR + return queued_spin_is_contended(lock); +ticket_spin_lock: +return ticket_spin_is_contended (lock); +} +#else /* CONFIG_RISCV_COMBO_SPINLOCKS */ +#ifdef CONFIG_QUEUED_SPINLOCKS #include <asm/qspinlock.h> -#include <asm/qrwlock.h> #else -#include <asm-generic/spinlock.h> +#include <asm-generic/ticket_spinlock.h> #endif +#endif /* CONFIG_RISCV_COMBO_SPINLOCKS */ + +#include <asm/qrwlock.h> +#endif /* __ASM_RISCV_SPINLOCK_H */ arch/riscv/kernel/cpufeature.c +0
-0
arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 332
Quoted 9 lines
arch/riscv/kernel/cpufeature.c +6
-0@@ -324,6 +324,16 @@ void __init riscv_fill_hwcap(void)set_bit (RISCV_ISA_EXT_ZICSR , isainfo->isa);set_bit (RISCV_ISA_EXT_ZIFENCEI , isainfo->isa); +#ifdef CONFIG_RISCV_COMBO_SPINLOCKS +/* +* The RISC-V Linux used queued spinlock at first; then, we used ticket_lock +* as default or queued spinlock by choice. Because ticket_lock would dirty +* spinlock value, the only way is to change from queued_spinlock to +* ticket_spinlock, but can not be vice.
The phrase "but can not be vice" is confusing. I think you mean "but not vice versa". Right?
Cheers,
Longman
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Waiman Long
reviewerRe: [PATCH V10 18/19] locking/qspinlock: Move pv_ops into x86 directory
In reply to [email protected]Quoted 5 lines · On 8/2/23 12:47, [email protected] wrote:
From: Guo Ren <[email protected]>
The
pv_opsbelongs to x86 custom infrastructure and cleans up thecna_configure_spin_lock_slowpath()with standard code. This is preparation for riscv support CNA qspoinlock.
CNA qspinlock has not been merged into mainline yet. I will suggest you drop the last 2 patches for now. Of course, you can provide benchmark data to boost the case for the inclusion of the CNA qspinlock patch into the mainline.
Cheers,
Longman
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Guo Ren
maintainerRe: [PATCH V10 04/19] riscv: qspinlock: Add basic queued_spinlock support
In reply to Waiman LongQuoted 15 lines · Waiman Long
Quoted 12 lines · On 8/2/23 12:46, [email protected] wrote:
\arch/riscv/include/asm/spinlock.h new file +5
-0@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_RISCV_SPINLOCK_H +#define __ASM_RISCV_SPINLOCK_H +
Quoted 8 lines
arch/riscv/include/asm/spinlock.h line 10
Quoted 5 lines
arch/riscv/include/asm/spinlock.h new file +5
-0+#ifdef CONFIG_QUEUED_SPINLOCKS +#define _Q_PENDING_LOOPS (1 << 9) +#endif + +#ifdef CONFIG_QUEUED_SPINLOCKS You can merge the two "#ifdef
CONFIG_QUEUED_SPINLOCKS" into single one to avoid the duplication.
Okay.
Quoted 12 lines
Cheers,
LongmanQuoted 7 lines
arch/riscv/include/asm/spinlock.h new file +7
-0+#include <asm/qspinlock.h> +#include <asm/qrwlock.h> +#else +#include <asm-generic/spinlock.h> +#endif + +#endif /* __ASM_RISCV_SPINLOCK_H */
Signature · 2 lines
Best Regards Guo Ren
Guo Ren
maintainerRe: [PATCH V10 05/19] riscv: qspinlock: Introduce combo spinlock
In reply to Waiman LongQuoted 86 lines · Waiman Long
Quoted 84 lines · On 8/2/23 12:46, [email protected] wrote:
From: Guo Ren <[email protected]>
Combo spinlock could support queued and ticket in one Linux Image and select them during boot time via errata mechanism. Here is the func size (Bytes) comparison table below:
TYPE : COMBO | TICKET | QUEUED
arch_spin_lock: 106 | 60 | 50arch_spin_unlock: 54 | 36 | 26arch_spin_trylock: 110 | 72 | 54arch_spin_is_locked: 48 | 34 | 20arch_spin_is_contended: 56 | 40 | 24rch_spin_value_unlocked: 48 | 34 | 24One example of disassemble combo
arch_spin_unlock:0xffffffff8000409c <+14>: nop # detour slot 0xffffffff800040a0 <+18>: fence rw,w # queued spinlock start 0xffffffff800040a4 <+22>: sb zero,0(a4) # queued spinlock end 0xffffffff800040a8 <+26>: ld s0,8(sp) 0xffffffff800040aa <+28>: addi sp,sp,16 0xffffffff800040ac <+30>: ret 0xffffffff800040ae <+32>: lw a5,0(a4) # ticket spinlock start 0xffffffff800040b0 <+34>: sext.w a5,a5 0xffffffff800040b2 <+36>: fence rw,w 0xffffffff800040b6 <+40>: addiw a5,a5,1 0xffffffff800040b8 <+42>: slli a5,a5,0x30 0xffffffff800040ba <+44>: srli a5,a5,0x30 0xffffffff800040bc <+46>: sh a5,0(a4) # ticket spinlock end 0xffffffff800040c0 <+50>: ld s0,8(sp) 0xffffffff800040c2 <+52>: addi sp,sp,16 0xffffffff800040c4 <+54>: retThe qspinlock is smaller and faster than ticket-lock when all are in fast-path, and combo spinlock could provide a compatible Linux Image for different micro-arch design (weak/strict fwd guarantee) processors.
Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>--- arch/riscv/Kconfig | 9 +++- arch/riscv/include/asm/hwcap.h | 1 + arch/riscv/include/asm/spinlock.h | 87 ++++++++++++++++++++++++++++++- arch/riscv/kernel/cpufeature.c | 10 ++++ 4 files changed, 104 insertions(+), 3 deletions(-)arch/riscv/Kconfig +8
-1@@ -440,7 +440,7 @@ config NODES_SHIFT choice prompt "RISC-V spinlock type" - default RISCV_TICKET_SPINLOCKS + default RISCV_COMBO_SPINLOCKS config RISCV_TICKET_SPINLOCKS bool "Using ticket spinlock"@@ -452,6 +452,13 @@ config RISCV_QUEUED_SPINLOCKS help Make sure your micro arch LL/SC has a strong forward progress guarantee. Otherwise, stay at ticket-lock. + +config RISCV_COMBO_SPINLOCKS + bool "Using combo spinlock" + depends on SMP && MMU + select ARCH_USE_QUEUED_SPINLOCKS + help + Select queued spinlock or ticket-lock via errata. endchoice config RISCV_ALTERNATIVEarch/riscv/include/asm/hwcap.h +1
-0@@ -54,6 +54,7 @@#define RISCV_ISA_EXT_ZIFENCEI 41#define RISCV_ISA_EXT_ZIHPM 42 +#define RISCV_ISA_EXT_XTICKETLOCK 63#define RISCV_ISA_EXT_MAX 64#define RISCV_ISA_EXT_NAME_LEN_MAX 32
Quoted 10 lines
arch/riscv/include/asm/spinlock.h line 9
Quoted 8 lines
arch/riscv/include/asm/spinlock.h +0
-0@@ -7,11 +7,94 @@#define _Q_PENDING_LOOPS (1 << 9)#endif I see why you separated the
_Q_PENDING_LOOPSout.
haha, yes, I even forget this, :).
Quoted 15 lines
Quoted 15 lines
arch/riscv/include/asm/spinlock.h +13
-0+#ifdef CONFIG_RISCV_COMBO_SPINLOCKS +#include <asm-generic/ticket_spinlock.h> + +#undef arch_spin_is_locked +#undef arch_spin_is_contended +#undef arch_spin_value_unlocked +#undef arch_spin_lock +#undef arch_spin_trylock +#undef arch_spin_unlock + +#include <asm-generic/qspinlock.h> +#include <asm/hwcap.h> +
Quoted 8 lines
arch/riscv/include/asm/spinlock.h line 18
Quoted 6 lines
arch/riscv/include/asm/spinlock.h +6
-0+#undef arch_spin_is_locked +#undef arch_spin_is_contended +#undef arch_spin_value_unlocked +#undef arch_spin_lock +#undef arch_spin_trylock +#undef arch_spin_unlockPerhaps you can add a macro like
__no_arch_spinlock_redefineto disable the various arch_spin_* definition inqspinlock.handticket_spinlock.h.
That's great; I will try it in the next version.
Quoted 82 lines
Quoted 82 lines
arch/riscv/include/asm/spinlock.h +66
-2+ +#define COMBO_DETOUR \ + asm_volatile_goto(ALTERNATIVE( \ + "nop", \ + "j %l[ticket_spin_lock]", \ + 0, \ + RISCV_ISA_EXT_XTICKETLOCK, \ + CONFIG_RISCV_COMBO_SPINLOCKS) \ + : : : : ticket_spin_lock); + +static __always_inline void arch_spin_lock(arch_spinlock_t *lock) +{ +COMBO_DETOUR +queued_spin_lock (lock ); +return ; +ticket_spin_lock: +ticket_spin_lock (lock); +} + +static __always_inline bool arch_spin_trylock(arch_spinlock_t *lock) +{ +COMBO_DETOUR + return queued_spin_trylock(lock); +ticket_spin_lock: +return ticket_spin_trylock (lock); +} + +static __always_inline void arch_spin_unlock(arch_spinlock_t *lock) +{ +COMBO_DETOUR +queued_spin_unlock (lock ); +return ; +ticket_spin_lock: +ticket_spin_unlock (lock); +} + +static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock) +{ +COMBO_DETOUR + return queued_spin_value_unlocked(lock); +ticket_spin_lock: +return ticket_spin_value_unlocked (lock); +} + +static __always_inline int arch_spin_is_locked(arch_spinlock_t *lock) +{ +COMBO_DETOUR + return queued_spin_is_locked(lock); +ticket_spin_lock: +return ticket_spin_is_locked (lock); +} + +static __always_inline int arch_spin_is_contended(arch_spinlock_t *lock) +{ +COMBO_DETOUR + return queued_spin_is_contended(lock); +ticket_spin_lock: +return ticket_spin_is_contended (lock); +} +#else /* CONFIG_RISCV_COMBO_SPINLOCKS */ +#ifdef CONFIG_QUEUED_SPINLOCKS #include <asm/qspinlock.h> -#include <asm/qrwlock.h> #else -#include <asm-generic/spinlock.h> +#include <asm-generic/ticket_spinlock.h> #endif +#endif /* CONFIG_RISCV_COMBO_SPINLOCKS */ + +#include <asm/qrwlock.h> +#endif /* __ASM_RISCV_SPINLOCK_H */ arch/riscv/kernel/cpufeature.c +0
-0@@ -324,6 +324,16 @@ void __init riscv_fill_hwcap(void)set_bit (RISCV_ISA_EXT_ZICSR , isainfo->isa);set_bit (RISCV_ISA_EXT_ZIFENCEI , isainfo->isa);
Quoted 9 lines
arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 332
Quoted 6 lines
arch/riscv/kernel/cpufeature.c +6
-0+#ifdef CONFIG_RISCV_COMBO_SPINLOCKS +/* +* The RISC-V Linux used queued spinlock at first; then, we used ticket_lock +* as default or queued spinlock by choice. Because ticket_lock would dirty +* spinlock value, the only way is to change from queued_spinlock to +* ticket_spinlock, but can not be vice. The phrase "but can not be vice" is confusing. I think you mean "but not vice versa". Right?
Yes, thx for the grammar correction.
Quoted 4 lines
Cheers,
Longman
Signature · 2 lines
Best Regards Guo Ren
Guo Ren
maintainerRe: [PATCH V10 18/19] locking/qspinlock: Move pv_ops into x86 directory
In reply to Waiman LongQuoted 4 lines · Waiman Long
Quoted 2 lines · On 8/2/23 12:47, [email protected] wrote:
From: Guo Ren <[email protected]>
Quoted 8 lines
Quoted 3 lines
The
pv_opsbelongs to x86 custom infrastructure and cleans up thecna_configure_spin_lock_slowpath()with standard code. This is preparation for riscv support CNA qspoinlock.CNA qspinlock has not been merged into mainline yet. I will suggest you drop the last 2 patches for now. Of course, you can provide benchmark data to boost the case for the inclusion of the CNA qspinlock patch into the mainline.
Yes, my lazy, I would separate paravirt and CNA from this series.
Quoted 4 lines
Cheers,
Longman
Signature · 2 lines
Best Regards Guo Ren
Waiman Long
reviewerRe: [PATCH V10 18/19] locking/qspinlock: Move pv_ops into x86 directory
Quoted 4 lines · Guo Ren
Quoted 3 lines · Waiman Long
Quoted 2 lines · On 8/2/23 12:47, [email protected] wrote:
From: Guo Ren <[email protected]>
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
The
pv_opsbelongs to x86 custom infrastructure and cleans up thecna_configure_spin_lock_slowpath()with standard code. This is preparation for riscv support CNA qspoinlock.CNA qspinlock has not been merged into mainline yet. I will suggest you drop the last 2 patches for now. Of course, you can provide benchmark data to boost the case for the inclusion of the CNA qspinlock patch into the mainline.
Yes, my lazy, I would separate paravirt and CNA from this series.
paravirt is OK, it is just that CNA hasn't been merged yet.
Cheers,
Longman
Quoted 5 lines
Quoted 4 lines
Cheers,
Longman
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH V10 07/19] riscv: qspinlock: errata: Introduce ERRATA_THEAD_QSPINLOCK
In reply to Stefan O'RearQuoted 48 lines · On Sun, 06 Aug 2023 22:23:34 PDT (-0700), [email protected] wrote:
Quoted 33 lines · On Wed, Aug 2, 2023, at 12:46 PM, [email protected] wrote:
From: Guo Ren <[email protected]>
According to qspinlock requirements, RISC-V gives out a weak LR/SC forward progress guarantee which does not satisfy qspinlock. But many vendors could produce stronger forward guarantee LR/SC to ensure the
xchg_tailcould be finished in time on any kind of hart. T-HEAD is the vendor which implements strong forward guarantee LR/SC instruction pairs, so enable qspinlock for T-HEAD with errata help.T-HEAD early version of processors has the merge buffer delay problem, so we need
ERRATA_WRITEONCEto support qspinlock.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>--- arch/riscv/Kconfig.errata | 13 +++++++++++++ arch/riscv/errata/thead/errata.c | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 20 ++++++++++++++++++++ arch/riscv/include/asm/vendorid_list.h | 3 ++- arch/riscv/kernel/cpufeature.c | 3 ++- 5 files changed, 61 insertions(+), 2 deletions(-)arch/riscv/Kconfig.errata +2
-0@@ -96,4 +96,17 @@ config ERRATA_THEAD_WRITE_ONCE If you don't know what to do here, say "Y". +config ERRATA_THEAD_QSPINLOCK + bool "Apply T-Head queued spinlock errata"arch/riscv/Kconfig.errata config ERRATA_THEAD_WRITE_ONCE · line 110
Quoted 10 lines
arch/riscv/Kconfig.errata +10
-0+ depends on ERRATA_THEAD + default y + help + The T-HEAD C9xx processors implement strong fwd guarantee LR/SC to + match the xchg_tail requirement of qspinlock. + + This will apply the QSPINLOCK errata to handle the non-standard + behavior via using qspinlock instead of ticket_lock. + + If you don't know what to do here, say "Y".If this is to be applied, I would like to see a detailed explanation somewhere, preferably with citations, of:
Quoted 5 lines
(a) The memory model requirements for qspinlock
(b) Why, with arguments, RISC-V does not architecturally meet (a)
(c) Why, with arguments, T-HEAD C9xx meets (a)
(d) Why at least one other architecture which definesARCH_USE_QUEUED_SPINLOCKSmeets (a)
I agree.
Just having a magic fence that makes qspinlocks stop livelocking on some processors is going to lead to a mess -- I'd argue this means those processors just don't provide the forward progress guarantee, but we'd really need something written down about what this new custom instruction aliasing as a fence does.
Quoted 5 lines
As far as I can tell, the RISC-V guarantees concerning constrained LR/SC loops (livelock freedom but no starvation freedom) are exactly the same as those in Armv8 (as of
0487F.c) for equivalent loops, andxchg_tailcompiles to a constrained LR/SC loop with guaranteed eventual success (with -O1). Clearly you disagree; I would like to see your perspective.
It sounds to me like this processor might be quite broken: if it's permanently holding stores in a buffer we're going to have more issues than just qspinlock, pretty much anything concurrent is going to have issues -- and that's not just in the kernel, there's concurrent userspace code as well.
Quoted 115 lines
-s
Quoted 113 lines
arch/riscv/Kconfig.errata +1
-0+ endmenu # "CPU errata selection"arch/riscv/errata/thead/errata.c +24
-0@@ -86,6 +86,27 @@ static bool errata_probe_write_once(unsigned int stage,return false; } +static bool errata_probe_qspinlock (unsigned int stage, +unsigned long arch_id,unsigned long impid) +{ +if (!IS_ENABLED (CONFIG_ERRATA_THEAD_QSPINLOCK )) +return false; + +/* +* The queued_spinlock torture would get in livelock without +* ERRATA_THEAD_WRITE_ONCE fixup for the early versions of T-HEAD +* processors. +*/ +if (arch_id ==0 && impid ==0 && + !IS_ENABLED (CONFIG_ERRATA_THEAD_WRITE_ONCE )) +return false; + +if (stage ==RISCV_ALTERNATIVES_EARLY_BOOT ) +return true; + +return false; +} +static u32 thead_errata_probe (unsigned int stage,unsigned long archid,unsigned long impid) {@@ -103,6 +124,9 @@ static u32 thead_errata_probe(unsigned int stage,if (errata_probe_write_once (stage, archid, impid)) cpu_req_errata |=BIT (ERRATA_THEAD_WRITE_ONCE ); +if (errata_probe_qspinlock (stage, archid, impid)) + cpu_req_errata |=BIT (ERRATA_THEAD_QSPINLOCK ); +return cpu_req_errata; }arch/riscv/include/asm/errata_list.h +20
-0@@ -141,6 +141,26 @@ asm volatile(ALTERNATIVE( \ : "=r" (__ovl) : \ : "memory") +static __always_inline bool +riscv_has_errata_thead_qspinlock (void ) +{ +if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) { +asm_volatile_goto ( +ALTERNATIVE ( +"j %l[l_no]" ,"nop" , +THEAD_VENDOR_ID , +ERRATA_THEAD_QSPINLOCK , +CONFIG_ERRATA_THEAD_QSPINLOCK ) + : : : : l_no); + }else { + goto l_no; + } + +return true; +l_no: +return false; +} +#endif /* __ASSEMBLY__ */ #endif arch/riscv/include/asm/vendorid_list.h +2
-1@@ -19,7 +19,8 @@#define ERRATA_THEAD_CMO 1#define ERRATA_THEAD_PMU 2#define ERRATA_THEAD_WRITE_ONCE 3 -#define ERRATA_THEAD_NUMBER 4 +#define ERRATA_THEAD_QSPINLOCK 4 +#define ERRATA_THEAD_NUMBER 5#endif #endif arch/riscv/kernel/cpufeature.c +2
-2@@ -342,7 +342,8 @@ void __init riscv_fill_hwcap(void)* spinlock value, the only way is to change from queued_spinlock to * ticket_spinlock, but can not be vice. */ -if (!force_qspinlock) { +if (!force_qspinlock && + !riscv_has_errata_thead_qspinlock ()) {set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa); }#endif --2.36.1
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Waiman Long
reviewerRe: [PATCH V10 07/19] riscv: qspinlock: errata: Introduce ERRATA_THEAD_QSPINLOCK
Quoted 50 lines · Palmer Dabbelt
Quoted 49 lines · On Sun, 06 Aug 2023 22:23:34 PDT (-0700), [email protected] wrote:
Quoted 39 lines · On Wed, Aug 2, 2023, at 12:46 PM, [email protected] wrote:
From: Guo Ren <[email protected]>
According to qspinlock requirements, RISC-V gives out a weak LR/SC forward progress guarantee which does not satisfy qspinlock. But many vendors could produce stronger forward guarantee LR/SC to ensure the
xchg_tailcould be finished in time on any kind of hart. T-HEAD is the vendor which implements strong forward guarantee LR/SC instruction pairs, so enable qspinlock for T-HEAD with errata help.T-HEAD early version of processors has the merge buffer delay problem, so we need
ERRATA_WRITEONCEto support qspinlock.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>---
arch/riscv/Kconfig.errata | 13 +++++++++++++ arch/riscv/errata/thead/errata.c | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 20 ++++++++++++++++++++arch/riscv/include/asm/vendorid_list.h| 3 ++- arch/riscv/kernel/cpufeature.c | 3 ++-5 files changed, 61 insertions(+), 2 deletions(-)arch/riscv/Kconfig.errata +0
-0@@ -96,4 +96,17 @@ config ERRATA_THEAD_WRITE_ONCEIf you don't know what to do here, say "Y".
+config
ERRATA_THEAD_QSPINLOCK
+ bool "Apply T-Head queued spinlock errata"
+ depends onERRATA_THEAD
+ default y
+ help
+ The T-HEAD C9xx processors implement strong fwd guarantee LR/SC to
+ match thexchg_tailrequirement of qspinlock.arch/riscv/Kconfig.errata config ERRATA_THEAD_WRITE_ONCE · line 110
Quoted 5 lines
arch/riscv/Kconfig.errata +5
-0+ + This will apply the QSPINLOCK errata to handle the non-standard + behavior via using qspinlock instead of ticket_lock. + + If you don't know what to do here, say "Y".If this is to be applied, I would like to see a detailed explanation somewhere, preferably with citations, of:
Quoted 1 line
Quoted 1 line
(a) The memory model requirements for qspinlock
The part of qspinlock that causes problem with many RISC architectures is its use of a 16-bit xchg() function call which many RISC architectures cannot do it natively and have to be emulated with hopefully some forward progress guarantee. Except that one call, the other atomic operations are all 32 bit in size.
Cheers,
Longman
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Guo Ren
maintainerRe: [PATCH V10 07/19] riscv: qspinlock: errata: Introduce ERRATA_THEAD_QSPINLOCK
In reply to Palmer DabbeltQuoted 56 lines · Palmer Dabbelt
Quoted 48 lines · On Sun, 06 Aug 2023 22:23:34 PDT (-0700), [email protected] wrote:
Quoted 33 lines · On Wed, Aug 2, 2023, at 12:46 PM, [email protected] wrote:
From: Guo Ren <[email protected]>
According to qspinlock requirements, RISC-V gives out a weak LR/SC forward progress guarantee which does not satisfy qspinlock. But many vendors could produce stronger forward guarantee LR/SC to ensure the
xchg_tailcould be finished in time on any kind of hart. T-HEAD is the vendor which implements strong forward guarantee LR/SC instruction pairs, so enable qspinlock for T-HEAD with errata help.T-HEAD early version of processors has the merge buffer delay problem, so we need
ERRATA_WRITEONCEto support qspinlock.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>--- arch/riscv/Kconfig.errata | 13 +++++++++++++ arch/riscv/errata/thead/errata.c | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 20 ++++++++++++++++++++ arch/riscv/include/asm/vendorid_list.h | 3 ++- arch/riscv/kernel/cpufeature.c | 3 ++- 5 files changed, 61 insertions(+), 2 deletions(-)arch/riscv/Kconfig.errata +2
-0@@ -96,4 +96,17 @@ config ERRATA_THEAD_WRITE_ONCE If you don't know what to do here, say "Y". +config ERRATA_THEAD_QSPINLOCK + bool "Apply T-Head queued spinlock errata"arch/riscv/Kconfig.errata config ERRATA_THEAD_WRITE_ONCE · line 110
Quoted 10 lines
arch/riscv/Kconfig.errata +10
-0+ depends on ERRATA_THEAD + default y + help + The T-HEAD C9xx processors implement strong fwd guarantee LR/SC to + match the xchg_tail requirement of qspinlock. + + This will apply the QSPINLOCK errata to handle the non-standard + behavior via using qspinlock instead of ticket_lock. + + If you don't know what to do here, say "Y".If this is to be applied, I would like to see a detailed explanation somewhere, preferably with citations, of:
Quoted 5 lines
(a) The memory model requirements for qspinlock
(b) Why, with arguments, RISC-V does not architecturally meet (a)
(c) Why, with arguments, T-HEAD C9xx meets (a)
(d) Why at least one other architecture which definesARCH_USE_QUEUED_SPINLOCKSmeets (a)
Quoted 7 lines
I agree.
Just having a magic fence that makes qspinlocks stop livelocking on some processors is going to lead to a mess -- I'd argue this means those processors just don't provide the forward progress guarantee, but we'd really need something written down about what this new custom instruction aliasing as a fence does.
The "magic fence" is not related to the LR/SC forward progress guarantee, and it's our processors' store buffer hardware problem that needs to be fixed. Not only is qspinlock suffering on it, but also kernel/locking/osq_lock.c.
This is about this patch:
https://lore.kernel.org/linux-riscv/[email protected]/
I've written down the root cause of that patch: The "new custom fence instruction" triggers the store buffer flush.
Only the normal store instruction has the problem, and atomic stores (including LR/SC) are okay.
Quoted 7 lines
Quoted 6 lines
As far as I can tell, the RISC-V guarantees concerning constrained LR/SC loops (livelock freedom but no starvation freedom) are exactly the same as those in Armv8 (as of
0487F.c) for equivalent loops, andxchg_tailcompiles to a constrained LR/SC loop with guaranteed eventual success (with -O1). Clearly you disagree; I would like to see your perspective.
Quoted 5 lines
It sounds to me like this processor might be quite broken: if it's permanently holding stores in a buffer we're going to have more issues than just qspinlock, pretty much anything concurrent is going to have issues -- and that's not just in the kernel, there's concurrent userspace code as well.
Yes, the userspace scenarios are our worries because modifying the userspace atomic library is impossible. Now, we are stress-testing various userspace parallel applications, especially userspace queued spinlock, on the 128-core hardware platform.
We haven't detected the problem in the userspace, and it could be because of timer interrupts, which breaks the store buffer waiting.
Quoted 116 lines
Quoted 116 lines
-s
Quoted 113 lines
arch/riscv/Kconfig.errata +1
-0+ endmenu # "CPU errata selection"arch/riscv/errata/thead/errata.c +24
-0@@ -86,6 +86,27 @@ static bool errata_probe_write_once(unsigned int stage,return false; } +static bool errata_probe_qspinlock (unsigned int stage, +unsigned long arch_id,unsigned long impid) +{ +if (!IS_ENABLED (CONFIG_ERRATA_THEAD_QSPINLOCK )) +return false; + +/* +* The queued_spinlock torture would get in livelock without +* ERRATA_THEAD_WRITE_ONCE fixup for the early versions of T-HEAD +* processors. +*/ +if (arch_id ==0 && impid ==0 && + !IS_ENABLED (CONFIG_ERRATA_THEAD_WRITE_ONCE )) +return false; + +if (stage ==RISCV_ALTERNATIVES_EARLY_BOOT ) +return true; + +return false; +} +static u32 thead_errata_probe (unsigned int stage,unsigned long archid,unsigned long impid) {@@ -103,6 +124,9 @@ static u32 thead_errata_probe(unsigned int stage,if (errata_probe_write_once (stage, archid, impid)) cpu_req_errata |=BIT (ERRATA_THEAD_WRITE_ONCE ); +if (errata_probe_qspinlock (stage, archid, impid)) + cpu_req_errata |=BIT (ERRATA_THEAD_QSPINLOCK ); +return cpu_req_errata; }arch/riscv/include/asm/errata_list.h +20
-0@@ -141,6 +141,26 @@ asm volatile(ALTERNATIVE( \ : "=r" (__ovl) : \ : "memory") +static __always_inline bool +riscv_has_errata_thead_qspinlock (void ) +{ +if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) { +asm_volatile_goto ( +ALTERNATIVE ( +"j %l[l_no]" ,"nop" , +THEAD_VENDOR_ID , +ERRATA_THEAD_QSPINLOCK , +CONFIG_ERRATA_THEAD_QSPINLOCK ) + : : : : l_no); + }else { + goto l_no; + } + +return true; +l_no: +return false; +} +#endif /* __ASSEMBLY__ */ #endif arch/riscv/include/asm/vendorid_list.h +2
-1@@ -19,7 +19,8 @@#define ERRATA_THEAD_CMO 1#define ERRATA_THEAD_PMU 2#define ERRATA_THEAD_WRITE_ONCE 3 -#define ERRATA_THEAD_NUMBER 4 +#define ERRATA_THEAD_QSPINLOCK 4 +#define ERRATA_THEAD_NUMBER 5#endif #endif arch/riscv/kernel/cpufeature.c +2
-2@@ -342,7 +342,8 @@ void __init riscv_fill_hwcap(void)* spinlock value, the only way is to change from queued_spinlock to * ticket_spinlock, but can not be vice. */ -if (!force_qspinlock) { +if (!force_qspinlock && + !riscv_has_errata_thead_qspinlock ()) {set_bit (RISCV_ISA_EXT_XTICKETLOCK , isainfo->isa); }#endif --2.36.1
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Signature · 2 lines
Best Regards Guo Ren