]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
RISC-V: KVM: Disable vstimecmp before exiting to user-space
authorAnup Patel <apatel@ventanamicro.com>
Mon, 7 Jul 2025 03:53:43 +0000 (09:23 +0530)
committerAnup Patel <anup@brainfault.org>
Fri, 11 Jul 2025 13:00:17 +0000 (18:30 +0530)
If VS-timer expires when no VCPU running on a host CPU then WFI
executed by such host CPU will be effective NOP resulting in no
power savings. This is as-per RISC-V Privileged specificaiton
which says: "WFI is also required to resume execution for locally
enabled interrupts pending at any privilege level, regardless of
the global interrupt enable at each privilege level."

To address the above issue, vstimecmp CSR must be set to -1UL over
here when VCPU is scheduled-out or exits to user space.

Reviewed-by: Atish Patra <atishp@rivosinc.com>
Tested-by: Atish Patra <atishp@rivosinc.com>
Tested-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Fixes: 8f5cb44b1bae ("RISC-V: KVM: Support sstc extension")
Fixes: cea8896bd936 ("RISC-V: KVM: Fix kvm_riscv_vcpu_timer_pending() for Sstc")
Reported-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Closes: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/2112578
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Nutty Liu <liujingqi@lanxincomputing.com>
Link: https://lore.kernel.org/r/20250707035345.17494-2-apatel@ventanamicro.com
Signed-off-by: Anup Patel <anup@brainfault.org>
arch/riscv/kvm/vcpu_timer.c

index ff672fa71fccf28953e037d827a49f4ac38fe50e..85a7262115e13f5e3e4a07a14a771f6926c9159d 100644 (file)
@@ -345,8 +345,24 @@ void kvm_riscv_vcpu_timer_save(struct kvm_vcpu *vcpu)
        /*
         * The vstimecmp CSRs are saved by kvm_riscv_vcpu_timer_sync()
         * upon every VM exit so no need to save here.
+        *
+        * If VS-timer expires when no VCPU running on a host CPU then
+        * WFI executed by such host CPU will be effective NOP resulting
+        * in no power savings. This is because as-per RISC-V Privileged
+        * specificaiton: "WFI is also required to resume execution for
+        * locally enabled interrupts pending at any privilege level,
+        * regardless of the global interrupt enable at each privilege
+        * level."
+        *
+        * To address the above issue, vstimecmp CSR must be set to -1UL
+        * over here when VCPU is scheduled-out or exits to user space.
         */
 
+       csr_write(CSR_VSTIMECMP, -1UL);
+#if defined(CONFIG_32BIT)
+       csr_write(CSR_VSTIMECMPH, -1UL);
+#endif
+
        /* timer should be enabled for the remaining operations */
        if (unlikely(!t->init_done))
                return;