[PATCH] RISC-V: KVM: Add kvm-riscv.wfi_trap_policy to control VS-mode WFI trapping
Anup Patel
anup at brainfault.org
Tue Jul 28 08:53:49 PDT 2026
On Thu, Jul 9, 2026 at 5:26 PM Yuhang.chen <yhchen312 at gmail.com> wrote:
>
> 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 at iscas.ac.cn>
> Signed-off-by: Quan Zhou <zhouquan at iscas.ac.cn>
> Signed-off-by: Yuhang.chen <yhchen312 at 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.
> +
This would mean that for the "notrap" policy the VCPU would never
sleep and increase host CPU utilization even when VCPU is idle.
Is this expected behaviour ?
Regards,
Anup
More information about the linux-riscv
mailing list