From: "Yuhang.chen" <yhchen312@gmail.com>
To: Anup Patel <anup@brainfault.org>
Cc: 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>,
Quan Zhou <zhouquan@iscas.ac.cn>,
linux-doc@vger.kernel.org, kvm@vger.kernel.org,
kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org,
linux-kernel@vger.kernel.org
Subject: [PATCH] RISC-V: KVM: Add kvm-riscv.wfi_trap_policy to control VS-mode WFI trapping
Date: Thu, 9 Jul 2026 19:56:10 +0800 [thread overview]
Message-ID: <20260709115610.287420-1-yhchen312@gmail.com> (raw)
Add a kernel command-line option, kvm-riscv.wfi_trap_policy=trap|notrap,
that controls whether a WFI executed by a VS-mode guest traps into KVM
(HS-mode) or executes natively.
Measured results (wfi_exit_stat delta / guest wake count over 3 s):
policy WFI_EXITS WAKE_CNT
---------- --------- --------
default 295 294 (== trap, no regression)
trap 295 294
notrap 0 298
Assisted-by: YuanSheng:deepseek-v4-pro
Co-developed-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Yuhang.chen <yhchen312@gmail.com>
---
.../admin-guide/kernel-parameters.txt | 14 ++++++
arch/riscv/kvm/vcpu.c | 44 ++++++++++++++++++-
2 files changed, 57 insertions(+), 1 deletion(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index b5493a7f8f22..ee6603b30033 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3254,6 +3254,20 @@ Kernel parameters
notrap: clear WFI instruction trap
+ kvm-riscv.wfi_trap_policy=
+ [KVM,RISCV] Control when to set the WFI instruction
+ trap for KVM VMs. When set, a VS-mode WFI traps into
+ KVM and is emulated; when clear, the guest executes
+ WFI natively and blocks until a VS-mode interrupt
+ (e.g. the sstc timer) is pending.
+
+ trap: set WFI instruction trap (HSTATUS.VTW=1)
+
+ notrap: clear WFI instruction trap (HSTATUS.VTW=0)
+
+ Defaults to trap, preserving the previous
+ unconditional behavior.
+
kvm_cma_resv_ratio=n [PPC,EARLY]
Reserves given percentage from system memory area for
contiguous memory allocation for KVM hash pagetable
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index c3672513b4e9..d50b47ed280e 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -12,6 +12,7 @@
#include <linux/kdebug.h>
#include <linux/module.h>
#include <linux/percpu.h>
+#include <linux/string.h>
#include <linux/vmalloc.h>
#include <linux/sched/signal.h>
#include <linux/fs.h>
@@ -26,6 +27,42 @@
static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_former_vcpu);
+/*
+ * WFI trap policy for VS-mode guests, controllable through the
+ * kvm-riscv.wfi_trap_policy= kernel command-line option.
+ */
+enum kvm_riscv_wfi_trap_policy {
+ KVM_RISCV_WFI_TRAP, /* Default: trap VS-mode WFI into KVM */
+ KVM_RISCV_WFI_NOTRAP, /* Let VS-mode WFI execute natively */
+};
+
+static enum kvm_riscv_wfi_trap_policy kvm_riscv_wfi_trap_policy __read_mostly =
+ KVM_RISCV_WFI_TRAP;
+
+static int __init early_kvm_riscv_wfi_trap_policy_cfg(char *arg)
+{
+ if (!arg)
+ return -EINVAL;
+
+ if (strcmp(arg, "trap") == 0) {
+ kvm_riscv_wfi_trap_policy = KVM_RISCV_WFI_TRAP;
+ return 0;
+ }
+
+ if (strcmp(arg, "notrap") == 0) {
+ kvm_riscv_wfi_trap_policy = KVM_RISCV_WFI_NOTRAP;
+ return 0;
+ }
+
+ return -EINVAL;
+}
+early_param("kvm-riscv.wfi_trap_policy", early_kvm_riscv_wfi_trap_policy_cfg);
+
+static bool kvm_riscv_vcpu_wfi_should_trap(struct kvm_vcpu *vcpu)
+{
+ return kvm_riscv_wfi_trap_policy == KVM_RISCV_WFI_TRAP;
+}
+
const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {
KVM_GENERIC_VCPU_STATS(),
STATS_DESC_COUNTER(VCPU, ecall_exit_stat),
@@ -73,7 +110,12 @@ static void kvm_riscv_vcpu_context_reset(struct kvm_vcpu *vcpu,
/* Setup reset state of shadow SSTATUS and HSTATUS CSRs */
cntx->sstatus = SR_SPP | SR_SPIE;
- cntx->hstatus |= HSTATUS_VTW;
+ /*
+ * Trap VS-mode WFI into KVM unless the WFI trap policy lets the guest
+ * execute it natively. See kvm_riscv_wfi_trap_policy.
+ */
+ if (kvm_riscv_vcpu_wfi_should_trap(vcpu))
+ cntx->hstatus |= HSTATUS_VTW;
cntx->hstatus |= HSTATUS_SPVP;
cntx->hstatus |= HSTATUS_SPV;
}
--
2.34.1
WARNING: multiple messages have this Message-ID (diff)
From: "Yuhang.chen" <yhchen312@gmail.com>
To: Anup Patel <anup@brainfault.org>
Cc: 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>,
Quan Zhou <zhouquan@iscas.ac.cn>,
linux-doc@vger.kernel.org, kvm@vger.kernel.org,
kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org,
linux-kernel@vger.kernel.org
Subject: [PATCH] RISC-V: KVM: Add kvm-riscv.wfi_trap_policy to control VS-mode WFI trapping
Date: Thu, 9 Jul 2026 19:56:10 +0800 [thread overview]
Message-ID: <20260709115610.287420-1-yhchen312@gmail.com> (raw)
Add a kernel command-line option, kvm-riscv.wfi_trap_policy=trap|notrap,
that controls whether a WFI executed by a VS-mode guest traps into KVM
(HS-mode) or executes natively.
Measured results (wfi_exit_stat delta / guest wake count over 3 s):
policy WFI_EXITS WAKE_CNT
---------- --------- --------
default 295 294 (== trap, no regression)
trap 295 294
notrap 0 298
Assisted-by: YuanSheng:deepseek-v4-pro
Co-developed-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Yuhang.chen <yhchen312@gmail.com>
---
.../admin-guide/kernel-parameters.txt | 14 ++++++
arch/riscv/kvm/vcpu.c | 44 ++++++++++++++++++-
2 files changed, 57 insertions(+), 1 deletion(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index b5493a7f8f22..ee6603b30033 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3254,6 +3254,20 @@ Kernel parameters
notrap: clear WFI instruction trap
+ kvm-riscv.wfi_trap_policy=
+ [KVM,RISCV] Control when to set the WFI instruction
+ trap for KVM VMs. When set, a VS-mode WFI traps into
+ KVM and is emulated; when clear, the guest executes
+ WFI natively and blocks until a VS-mode interrupt
+ (e.g. the sstc timer) is pending.
+
+ trap: set WFI instruction trap (HSTATUS.VTW=1)
+
+ notrap: clear WFI instruction trap (HSTATUS.VTW=0)
+
+ Defaults to trap, preserving the previous
+ unconditional behavior.
+
kvm_cma_resv_ratio=n [PPC,EARLY]
Reserves given percentage from system memory area for
contiguous memory allocation for KVM hash pagetable
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index c3672513b4e9..d50b47ed280e 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -12,6 +12,7 @@
#include <linux/kdebug.h>
#include <linux/module.h>
#include <linux/percpu.h>
+#include <linux/string.h>
#include <linux/vmalloc.h>
#include <linux/sched/signal.h>
#include <linux/fs.h>
@@ -26,6 +27,42 @@
static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_former_vcpu);
+/*
+ * WFI trap policy for VS-mode guests, controllable through the
+ * kvm-riscv.wfi_trap_policy= kernel command-line option.
+ */
+enum kvm_riscv_wfi_trap_policy {
+ KVM_RISCV_WFI_TRAP, /* Default: trap VS-mode WFI into KVM */
+ KVM_RISCV_WFI_NOTRAP, /* Let VS-mode WFI execute natively */
+};
+
+static enum kvm_riscv_wfi_trap_policy kvm_riscv_wfi_trap_policy __read_mostly =
+ KVM_RISCV_WFI_TRAP;
+
+static int __init early_kvm_riscv_wfi_trap_policy_cfg(char *arg)
+{
+ if (!arg)
+ return -EINVAL;
+
+ if (strcmp(arg, "trap") == 0) {
+ kvm_riscv_wfi_trap_policy = KVM_RISCV_WFI_TRAP;
+ return 0;
+ }
+
+ if (strcmp(arg, "notrap") == 0) {
+ kvm_riscv_wfi_trap_policy = KVM_RISCV_WFI_NOTRAP;
+ return 0;
+ }
+
+ return -EINVAL;
+}
+early_param("kvm-riscv.wfi_trap_policy", early_kvm_riscv_wfi_trap_policy_cfg);
+
+static bool kvm_riscv_vcpu_wfi_should_trap(struct kvm_vcpu *vcpu)
+{
+ return kvm_riscv_wfi_trap_policy == KVM_RISCV_WFI_TRAP;
+}
+
const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {
KVM_GENERIC_VCPU_STATS(),
STATS_DESC_COUNTER(VCPU, ecall_exit_stat),
@@ -73,7 +110,12 @@ static void kvm_riscv_vcpu_context_reset(struct kvm_vcpu *vcpu,
/* Setup reset state of shadow SSTATUS and HSTATUS CSRs */
cntx->sstatus = SR_SPP | SR_SPIE;
- cntx->hstatus |= HSTATUS_VTW;
+ /*
+ * Trap VS-mode WFI into KVM unless the WFI trap policy lets the guest
+ * execute it natively. See kvm_riscv_wfi_trap_policy.
+ */
+ if (kvm_riscv_vcpu_wfi_should_trap(vcpu))
+ cntx->hstatus |= HSTATUS_VTW;
cntx->hstatus |= HSTATUS_SPVP;
cntx->hstatus |= HSTATUS_SPV;
}
--
2.34.1
--
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: "Yuhang.chen" <yhchen312@gmail.com>
To: Anup Patel <anup@brainfault.org>
Cc: 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>,
Quan Zhou <zhouquan@iscas.ac.cn>,
linux-doc@vger.kernel.org, kvm@vger.kernel.org,
kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org,
linux-kernel@vger.kernel.org
Subject: [PATCH] RISC-V: KVM: Add kvm-riscv.wfi_trap_policy to control VS-mode WFI trapping
Date: Thu, 9 Jul 2026 19:56:10 +0800 [thread overview]
Message-ID: <20260709115610.287420-1-yhchen312@gmail.com> (raw)
Add a kernel command-line option, kvm-riscv.wfi_trap_policy=trap|notrap,
that controls whether a WFI executed by a VS-mode guest traps into KVM
(HS-mode) or executes natively.
Measured results (wfi_exit_stat delta / guest wake count over 3 s):
policy WFI_EXITS WAKE_CNT
---------- --------- --------
default 295 294 (== trap, no regression)
trap 295 294
notrap 0 298
Assisted-by: YuanSheng:deepseek-v4-pro
Co-developed-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Yuhang.chen <yhchen312@gmail.com>
---
.../admin-guide/kernel-parameters.txt | 14 ++++++
arch/riscv/kvm/vcpu.c | 44 ++++++++++++++++++-
2 files changed, 57 insertions(+), 1 deletion(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index b5493a7f8f22..ee6603b30033 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3254,6 +3254,20 @@ Kernel parameters
notrap: clear WFI instruction trap
+ kvm-riscv.wfi_trap_policy=
+ [KVM,RISCV] Control when to set the WFI instruction
+ trap for KVM VMs. When set, a VS-mode WFI traps into
+ KVM and is emulated; when clear, the guest executes
+ WFI natively and blocks until a VS-mode interrupt
+ (e.g. the sstc timer) is pending.
+
+ trap: set WFI instruction trap (HSTATUS.VTW=1)
+
+ notrap: clear WFI instruction trap (HSTATUS.VTW=0)
+
+ Defaults to trap, preserving the previous
+ unconditional behavior.
+
kvm_cma_resv_ratio=n [PPC,EARLY]
Reserves given percentage from system memory area for
contiguous memory allocation for KVM hash pagetable
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index c3672513b4e9..d50b47ed280e 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -12,6 +12,7 @@
#include <linux/kdebug.h>
#include <linux/module.h>
#include <linux/percpu.h>
+#include <linux/string.h>
#include <linux/vmalloc.h>
#include <linux/sched/signal.h>
#include <linux/fs.h>
@@ -26,6 +27,42 @@
static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_former_vcpu);
+/*
+ * WFI trap policy for VS-mode guests, controllable through the
+ * kvm-riscv.wfi_trap_policy= kernel command-line option.
+ */
+enum kvm_riscv_wfi_trap_policy {
+ KVM_RISCV_WFI_TRAP, /* Default: trap VS-mode WFI into KVM */
+ KVM_RISCV_WFI_NOTRAP, /* Let VS-mode WFI execute natively */
+};
+
+static enum kvm_riscv_wfi_trap_policy kvm_riscv_wfi_trap_policy __read_mostly =
+ KVM_RISCV_WFI_TRAP;
+
+static int __init early_kvm_riscv_wfi_trap_policy_cfg(char *arg)
+{
+ if (!arg)
+ return -EINVAL;
+
+ if (strcmp(arg, "trap") == 0) {
+ kvm_riscv_wfi_trap_policy = KVM_RISCV_WFI_TRAP;
+ return 0;
+ }
+
+ if (strcmp(arg, "notrap") == 0) {
+ kvm_riscv_wfi_trap_policy = KVM_RISCV_WFI_NOTRAP;
+ return 0;
+ }
+
+ return -EINVAL;
+}
+early_param("kvm-riscv.wfi_trap_policy", early_kvm_riscv_wfi_trap_policy_cfg);
+
+static bool kvm_riscv_vcpu_wfi_should_trap(struct kvm_vcpu *vcpu)
+{
+ return kvm_riscv_wfi_trap_policy == KVM_RISCV_WFI_TRAP;
+}
+
const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {
KVM_GENERIC_VCPU_STATS(),
STATS_DESC_COUNTER(VCPU, ecall_exit_stat),
@@ -73,7 +110,12 @@ static void kvm_riscv_vcpu_context_reset(struct kvm_vcpu *vcpu,
/* Setup reset state of shadow SSTATUS and HSTATUS CSRs */
cntx->sstatus = SR_SPP | SR_SPIE;
- cntx->hstatus |= HSTATUS_VTW;
+ /*
+ * Trap VS-mode WFI into KVM unless the WFI trap policy lets the guest
+ * execute it natively. See kvm_riscv_wfi_trap_policy.
+ */
+ if (kvm_riscv_vcpu_wfi_should_trap(vcpu))
+ cntx->hstatus |= HSTATUS_VTW;
cntx->hstatus |= HSTATUS_SPVP;
cntx->hstatus |= HSTATUS_SPV;
}
--
2.34.1
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next reply other threads:[~2026-07-09 11:56 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-09 11:56 Yuhang.chen [this message]
2026-07-09 11:56 ` [PATCH] RISC-V: KVM: Add kvm-riscv.wfi_trap_policy to control VS-mode WFI trapping Yuhang.chen
2026-07-09 11:56 ` Yuhang.chen
2026-07-09 12:09 ` 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=20260709115610.287420-1-yhchen312@gmail.com \
--to=yhchen312@gmail.com \
--cc=alex@ghiti.fr \
--cc=anup@brainfault.org \
--cc=aou@eecs.berkeley.edu \
--cc=atish.patra@linux.dev \
--cc=corbet@lwn.net \
--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=skhan@linuxfoundation.org \
--cc=zhouquan@iscas.ac.cn \
/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.