From: "GUO Ren (XuanTie)" <guoren@kernel.org>
To: Anup Patel <anup@brainfault.org>,
Atish Patra <atish.patra@linux.dev>,
Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Alexandre Ghiti <alex@ghiti.fr>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Randy Dunlap <rdunlap@infradead.org>,
Fangyu Yu <fangyu.yu@linux.alibaba.com>,
CHEN Jiankang <yubin.cjk@alibaba-inc.com>
Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
linux-doc@vger.kernel.org,
"GUO Ren (XuanTie)" <guoren@kernel.org>
Subject: [PATCH RFC v4 4/4] RISC-V: paravirt: Support nopvspin to disable PARAVIRT_SPINLOCKS
Date: Mon, 21 Sep 2026 12:27:04 +0000 [thread overview]
Message-ID: <20260921-pvqspinlock-v4-4-409a22aed6ef@kernel.org> (raw)
In-Reply-To: <20260921-pvqspinlock-v4-0-409a22aed6ef@kernel.org>
VM guests should fall back to a Test-and-Set spinlock when
PARAVIRT_SPINLOCKS is disabled, because fair locks suffer from severe
lock-holder preemption issues. The virt_spin_lock_key shortcuts
queued_spin_lock_slowpath(), allowing virt_spin_lock() to hijack it.
See commit 43b3f02899f7 ("locking/qspinlock/x86: Fix performance
regression under unaccelerated VMs").
Add a static key controlling whether virt_spin_lock() is called, and
add nopvspin support mirroring x86.
Signed-off-by: GUO Ren (XuanTie) <guoren@kernel.org>
---
Documentation/admin-guide/kernel-parameters.txt | 2 +-
arch/riscv/include/asm/qspinlock.h | 24 ++++++++++++++++++++++++
arch/riscv/kernel/qspinlock_paravirt.c | 8 ++++++++
3 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 33cd30996e47..d32bb7db9f91 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -4632,7 +4632,7 @@ Kernel parameters
as generic guest with no PV drivers. Currently support
XEN HVM, KVM, HYPER_V and VMWARE guest.
- nopvspin [X86,XEN,KVM,EARLY]
+ nopvspin [X86,RISCV,XEN,KVM,EARLY]
Disables the qspinlock slow path using PV optimizations
which allow the hypervisor to 'idle' the guest on lock
contention.
diff --git a/arch/riscv/include/asm/qspinlock.h b/arch/riscv/include/asm/qspinlock.h
index 330b714edc44..9c7108baa40a 100644
--- a/arch/riscv/include/asm/qspinlock.h
+++ b/arch/riscv/include/asm/qspinlock.h
@@ -12,6 +12,8 @@
/* How long a lock should spin before we consider blocking */
#define SPIN_THRESHOLD (1 << 15)
+extern bool nopvspin;
+
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);
@@ -29,5 +31,27 @@ static inline void queued_spin_unlock(struct qspinlock *lock)
#endif /* CONFIG_PARAVIRT_SPINLOCKS */
#include <asm-generic/qspinlock.h>
+#include <asm/jump_label.h>
+
+/*
+ * KVM guests fall back to a Test-and-Set spinlock because fair locks suffer
+ * from severe lock-holder-preemption issues. When virt_spin_lock_key is
+ * enabled, virt_spin_lock() shortcuts queued_spin_lock_slowpath() and hijacks
+ * the lock acquisition.
+ */
+DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key);
+
+#define virt_spin_lock rv_virt_spin_lock
+static inline bool rv_virt_spin_lock(struct qspinlock *lock)
+{
+ if (!static_branch_likely(&virt_spin_lock_key))
+ return false;
+
+ do {
+ smp_cond_load_relaxed((s32 *)&lock->val, VAL == 0);
+ } while (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) != 0);
+
+ return true;
+}
#endif /* _ASM_RISCV_QSPINLOCK_H */
diff --git a/arch/riscv/kernel/qspinlock_paravirt.c b/arch/riscv/kernel/qspinlock_paravirt.c
index 04b13994e971..28c6c78d7e84 100644
--- a/arch/riscv/kernel/qspinlock_paravirt.c
+++ b/arch/riscv/kernel/qspinlock_paravirt.c
@@ -50,6 +50,8 @@ EXPORT_STATIC_CALL(pv_queued_spin_lock_slowpath);
DEFINE_STATIC_CALL(pv_queued_spin_unlock, native_queued_spin_unlock);
EXPORT_STATIC_CALL(pv_queued_spin_unlock);
+DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key);
+
bool __init pv_qspinlock_init(void)
{
if (num_possible_cpus() == 1)
@@ -58,6 +60,12 @@ bool __init pv_qspinlock_init(void)
if (!sbi_probe_extension(SBI_EXT_PVLOCK))
return false;
+ if (nopvspin) {
+ static_branch_enable(&virt_spin_lock_key);
+ pr_info("virt_spin_lock enabled by nopvspin\n");
+ return true;
+ }
+
pr_info("PV qspinlocks enabled\n");
__pv_init_lock_hash();
--
2.43.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
WARNING: multiple messages have this Message-ID (diff)
From: "GUO Ren (XuanTie)" <guoren@kernel.org>
To: Anup Patel <anup@brainfault.org>,
Atish Patra <atish.patra@linux.dev>,
Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Alexandre Ghiti <alex@ghiti.fr>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Randy Dunlap <rdunlap@infradead.org>,
Fangyu Yu <fangyu.yu@linux.alibaba.com>,
CHEN Jiankang <yubin.cjk@alibaba-inc.com>
Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
linux-doc@vger.kernel.org,
"GUO Ren (XuanTie)" <guoren@kernel.org>
Subject: [PATCH RFC v4 4/4] RISC-V: paravirt: Support nopvspin to disable PARAVIRT_SPINLOCKS
Date: Mon, 21 Sep 2026 12:27:04 +0000 [thread overview]
Message-ID: <20260921-pvqspinlock-v4-4-409a22aed6ef@kernel.org> (raw)
In-Reply-To: <20260921-pvqspinlock-v4-0-409a22aed6ef@kernel.org>
VM guests should fall back to a Test-and-Set spinlock when
PARAVIRT_SPINLOCKS is disabled, because fair locks suffer from severe
lock-holder preemption issues. The virt_spin_lock_key shortcuts
queued_spin_lock_slowpath(), allowing virt_spin_lock() to hijack it.
See commit 43b3f02899f7 ("locking/qspinlock/x86: Fix performance
regression under unaccelerated VMs").
Add a static key controlling whether virt_spin_lock() is called, and
add nopvspin support mirroring x86.
Signed-off-by: GUO Ren (XuanTie) <guoren@kernel.org>
---
Documentation/admin-guide/kernel-parameters.txt | 2 +-
arch/riscv/include/asm/qspinlock.h | 24 ++++++++++++++++++++++++
arch/riscv/kernel/qspinlock_paravirt.c | 8 ++++++++
3 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 33cd30996e47..d32bb7db9f91 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -4632,7 +4632,7 @@ Kernel parameters
as generic guest with no PV drivers. Currently support
XEN HVM, KVM, HYPER_V and VMWARE guest.
- nopvspin [X86,XEN,KVM,EARLY]
+ nopvspin [X86,RISCV,XEN,KVM,EARLY]
Disables the qspinlock slow path using PV optimizations
which allow the hypervisor to 'idle' the guest on lock
contention.
diff --git a/arch/riscv/include/asm/qspinlock.h b/arch/riscv/include/asm/qspinlock.h
index 330b714edc44..9c7108baa40a 100644
--- a/arch/riscv/include/asm/qspinlock.h
+++ b/arch/riscv/include/asm/qspinlock.h
@@ -12,6 +12,8 @@
/* How long a lock should spin before we consider blocking */
#define SPIN_THRESHOLD (1 << 15)
+extern bool nopvspin;
+
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);
@@ -29,5 +31,27 @@ static inline void queued_spin_unlock(struct qspinlock *lock)
#endif /* CONFIG_PARAVIRT_SPINLOCKS */
#include <asm-generic/qspinlock.h>
+#include <asm/jump_label.h>
+
+/*
+ * KVM guests fall back to a Test-and-Set spinlock because fair locks suffer
+ * from severe lock-holder-preemption issues. When virt_spin_lock_key is
+ * enabled, virt_spin_lock() shortcuts queued_spin_lock_slowpath() and hijacks
+ * the lock acquisition.
+ */
+DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key);
+
+#define virt_spin_lock rv_virt_spin_lock
+static inline bool rv_virt_spin_lock(struct qspinlock *lock)
+{
+ if (!static_branch_likely(&virt_spin_lock_key))
+ return false;
+
+ do {
+ smp_cond_load_relaxed((s32 *)&lock->val, VAL == 0);
+ } while (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) != 0);
+
+ return true;
+}
#endif /* _ASM_RISCV_QSPINLOCK_H */
diff --git a/arch/riscv/kernel/qspinlock_paravirt.c b/arch/riscv/kernel/qspinlock_paravirt.c
index 04b13994e971..28c6c78d7e84 100644
--- a/arch/riscv/kernel/qspinlock_paravirt.c
+++ b/arch/riscv/kernel/qspinlock_paravirt.c
@@ -50,6 +50,8 @@ EXPORT_STATIC_CALL(pv_queued_spin_lock_slowpath);
DEFINE_STATIC_CALL(pv_queued_spin_unlock, native_queued_spin_unlock);
EXPORT_STATIC_CALL(pv_queued_spin_unlock);
+DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key);
+
bool __init pv_qspinlock_init(void)
{
if (num_possible_cpus() == 1)
@@ -58,6 +60,12 @@ bool __init pv_qspinlock_init(void)
if (!sbi_probe_extension(SBI_EXT_PVLOCK))
return false;
+ if (nopvspin) {
+ static_branch_enable(&virt_spin_lock_key);
+ pr_info("virt_spin_lock enabled by nopvspin\n");
+ return true;
+ }
+
pr_info("PV qspinlocks enabled\n");
__pv_init_lock_hash();
--
2.43.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
WARNING: multiple messages have this Message-ID (diff)
From: "GUO Ren (XuanTie)" <guoren@kernel.org>
To: Anup Patel <anup@brainfault.org>,
Atish Patra <atish.patra@linux.dev>,
Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Alexandre Ghiti <alex@ghiti.fr>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Randy Dunlap <rdunlap@infradead.org>,
Fangyu Yu <fangyu.yu@linux.alibaba.com>,
CHEN Jiankang <yubin.cjk@alibaba-inc.com>
Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
linux-doc@vger.kernel.org,
"GUO Ren (XuanTie)" <guoren@kernel.org>
Subject: [PATCH RFC v4 4/4] RISC-V: paravirt: Support nopvspin to disable PARAVIRT_SPINLOCKS
Date: Mon, 21 Sep 2026 12:27:04 +0000 [thread overview]
Message-ID: <20260921-pvqspinlock-v4-4-409a22aed6ef@kernel.org> (raw)
In-Reply-To: <20260921-pvqspinlock-v4-0-409a22aed6ef@kernel.org>
VM guests should fall back to a Test-and-Set spinlock when
PARAVIRT_SPINLOCKS is disabled, because fair locks suffer from severe
lock-holder preemption issues. The virt_spin_lock_key shortcuts
queued_spin_lock_slowpath(), allowing virt_spin_lock() to hijack it.
See commit 43b3f02899f7 ("locking/qspinlock/x86: Fix performance
regression under unaccelerated VMs").
Add a static key controlling whether virt_spin_lock() is called, and
add nopvspin support mirroring x86.
Signed-off-by: GUO Ren (XuanTie) <guoren@kernel.org>
---
Documentation/admin-guide/kernel-parameters.txt | 2 +-
arch/riscv/include/asm/qspinlock.h | 24 ++++++++++++++++++++++++
arch/riscv/kernel/qspinlock_paravirt.c | 8 ++++++++
3 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 33cd30996e47..d32bb7db9f91 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -4632,7 +4632,7 @@ Kernel parameters
as generic guest with no PV drivers. Currently support
XEN HVM, KVM, HYPER_V and VMWARE guest.
- nopvspin [X86,XEN,KVM,EARLY]
+ nopvspin [X86,RISCV,XEN,KVM,EARLY]
Disables the qspinlock slow path using PV optimizations
which allow the hypervisor to 'idle' the guest on lock
contention.
diff --git a/arch/riscv/include/asm/qspinlock.h b/arch/riscv/include/asm/qspinlock.h
index 330b714edc44..9c7108baa40a 100644
--- a/arch/riscv/include/asm/qspinlock.h
+++ b/arch/riscv/include/asm/qspinlock.h
@@ -12,6 +12,8 @@
/* How long a lock should spin before we consider blocking */
#define SPIN_THRESHOLD (1 << 15)
+extern bool nopvspin;
+
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);
@@ -29,5 +31,27 @@ static inline void queued_spin_unlock(struct qspinlock *lock)
#endif /* CONFIG_PARAVIRT_SPINLOCKS */
#include <asm-generic/qspinlock.h>
+#include <asm/jump_label.h>
+
+/*
+ * KVM guests fall back to a Test-and-Set spinlock because fair locks suffer
+ * from severe lock-holder-preemption issues. When virt_spin_lock_key is
+ * enabled, virt_spin_lock() shortcuts queued_spin_lock_slowpath() and hijacks
+ * the lock acquisition.
+ */
+DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key);
+
+#define virt_spin_lock rv_virt_spin_lock
+static inline bool rv_virt_spin_lock(struct qspinlock *lock)
+{
+ if (!static_branch_likely(&virt_spin_lock_key))
+ return false;
+
+ do {
+ smp_cond_load_relaxed((s32 *)&lock->val, VAL == 0);
+ } while (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) != 0);
+
+ return true;
+}
#endif /* _ASM_RISCV_QSPINLOCK_H */
diff --git a/arch/riscv/kernel/qspinlock_paravirt.c b/arch/riscv/kernel/qspinlock_paravirt.c
index 04b13994e971..28c6c78d7e84 100644
--- a/arch/riscv/kernel/qspinlock_paravirt.c
+++ b/arch/riscv/kernel/qspinlock_paravirt.c
@@ -50,6 +50,8 @@ EXPORT_STATIC_CALL(pv_queued_spin_lock_slowpath);
DEFINE_STATIC_CALL(pv_queued_spin_unlock, native_queued_spin_unlock);
EXPORT_STATIC_CALL(pv_queued_spin_unlock);
+DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key);
+
bool __init pv_qspinlock_init(void)
{
if (num_possible_cpus() == 1)
@@ -58,6 +60,12 @@ bool __init pv_qspinlock_init(void)
if (!sbi_probe_extension(SBI_EXT_PVLOCK))
return false;
+ if (nopvspin) {
+ static_branch_enable(&virt_spin_lock_key);
+ pr_info("virt_spin_lock enabled by nopvspin\n");
+ return true;
+ }
+
pr_info("PV qspinlocks enabled\n");
__pv_init_lock_hash();
--
2.43.0
next prev parent reply other threads:[~2026-09-21 12:27 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-21 12:27 [PATCH RFC v4 0/4] RISC-V: Add PARAVIRT_SPINLOCKS support GUO Ren (XuanTie)
2026-09-21 12:27 ` GUO Ren (XuanTie)
2026-09-21 12:27 ` GUO Ren (XuanTie)
2026-09-21 12:27 ` [PATCH RFC v4 1/4] RISC-V: paravirt: Add pvqspinlock KVM backend GUO Ren (XuanTie)
2026-09-21 12:27 ` GUO Ren (XuanTie)
2026-09-21 12:27 ` GUO Ren (XuanTie)
2026-09-21 12:35 ` sashiko-bot
2026-09-21 12:27 ` [PATCH RFC v4 2/4] RISC-V: paravirt: Add pvqspinlock frontend GUO Ren (XuanTie)
2026-09-21 12:27 ` GUO Ren (XuanTie)
2026-09-21 12:27 ` GUO Ren (XuanTie)
2026-09-21 12:41 ` sashiko-bot
2026-09-21 12:27 ` [PATCH RFC v4 3/4] RISC-V: paravirt: pvqspinlock: Add trace point for pv_kick/wait GUO Ren (XuanTie)
2026-09-21 12:27 ` GUO Ren (XuanTie)
2026-09-21 12:27 ` GUO Ren (XuanTie)
2026-09-21 12:39 ` sashiko-bot
2026-09-21 12:27 ` GUO Ren (XuanTie) [this message]
2026-09-21 12:27 ` [PATCH RFC v4 4/4] RISC-V: paravirt: Support nopvspin to disable PARAVIRT_SPINLOCKS GUO Ren (XuanTie)
2026-09-21 12:27 ` GUO Ren (XuanTie)
2026-09-21 12:41 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260921-pvqspinlock-v4-4-409a22aed6ef@kernel.org \
--to=guoren@kernel.org \
--cc=alex@ghiti.fr \
--cc=anup@brainfault.org \
--cc=aou@eecs.berkeley.edu \
--cc=atish.patra@linux.dev \
--cc=corbet@lwn.net \
--cc=fangyu.yu@linux.alibaba.com \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=rdunlap@infradead.org \
--cc=skhan@linuxfoundation.org \
--cc=yubin.cjk@alibaba-inc.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.