KVM: RISC-V: Add CPU PM notifier for non-retention idle states
Subsystems (3)
v2 cover KVM: RISC-V: Add CPU PM notifier for non-retention idle states
This series adds CPU_PM notifier support to save and restore hypervisor CSR state when CPUs enter non-retention idle states where the CPU domain is powered off.
Patch 1 fixes a VCPU cache invalidation bug that could cause incorrect CSR restoration after power state transitions.
Patch 2 implements the CPU_PM notifier to handle CSR save/restore during CPU idle, completing KVM's power management coverage alongside existing CPU hotplug and system suspend mechanisms.
---
Changes in v2:
- Add patch 1 force restore VCPU after power state transitions (sashiko)
- Added CPU_PM_ENTER_FAILED event handling (sashiko)
- Optimized HGEIE save/restore with saved_value field (sashiko)
- Fixed HIE CSR restoration in lightweight idle resume path (sashiko)
- Link to v1: https://patch.msgid.link/[email protected]
---
To: Anup Patel <[email protected]>
To: Atish Patra <[email protected]>
To: Paul Walmsley <[email protected]>
To: Palmer Dabbelt <[email protected]>
To: Albert Ou <[email protected]>
To: Alexandre Ghiti <[email protected]>
To: Radim Krčmář <[email protected]>
To: Andrew Jones <[email protected]>
To: Nutty Liu <[email protected]>
To: Jinyu Tang <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
Yong-Xuan Wang (2):
KVM: RISC-V: Clear former VCPU cache on virtualization disable
KVM: RISC-V: Add CPU PM notifier for non-retention idle states
arch/riscv/include/asm/kvm_aia.h | 15 +++- arch/riscv/include/asm/kvm_host.h | 2 + arch/riscv/kvm/aia.c | 144 +++++++++++++++++++++----------------- arch/riscv/kvm/main.c | 100 +++++++++++++++++++++----- arch/riscv/kvm/vcpu.c | 11 +++ 5 files changed, 189 insertions(+), 83 deletions(-)
---
base-commit: 52738352a6f29279e15285fcb7b50241ef867e27
change-id: 20260624-kvm-cpu-pm-94141aecd5fa
PATCH v2 1/2 KVM: RISC-V: Clear former VCPU cache on virtualization disable
When a CPU is taken offline or enters deep idle states, hypervisor CSR state is lost. The kvm_former_vcpu fast-path optimization caches the last VCPU that ran on each CPU to avoid expensive CSR restoration when the same VCPU is re-scheduled on the same CPU. However, if this cache is not cleared when CSR state is lost, the next VCPU entry will incorrectly skip CSR restoration, leading to corrupt hypervisor state.
Add kvm_riscv_clear_former_vcpu() to invalidate the per-CPU cache and call it from kvm_arch_disable_virtualization_cpu() to ensure proper CSR restoration after CPU offline or system suspend events.
--- arch/riscv/include/asm/kvm_host.h | 2 ++ arch/riscv/kvm/main.c | 2 ++ arch/riscv/kvm/vcpu.c | 11 +++++++++++ 3 files changed, 15 insertions(+)
arch/riscv/include/asm/kvm_host.h +2 -0
@@ -319,6 +319,8 @@ bool kvm_riscv_vcpu_stopped(struct kvm_vcpu *vcpu);void kvm_riscv_vcpu_record_steal_time (struct kvm_vcpu *vcpu); +void kvm_riscv_clear_former_vcpu (void ); +/* Flags representing implementation specific details */ DECLARE_STATIC_KEY_FALSE (kvm_riscv_vsstage_tlb_no_gpa);
arch/riscv/kvm/main.c +2 -0
@@ -69,6 +69,8 @@ void kvm_arch_disable_virtualization_cpu(void)csr_write (CSR_HEDELEG ,0 );csr_write (CSR_HIDELEG ,0 ); +kvm_riscv_clear_former_vcpu (); +kvm_riscv_nacl_disable (); }
arch/riscv/kvm/vcpu.c +11 -0
@@ -26,6 +26,17 @@ static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_former_vcpu); +void kvm_riscv_clear_former_vcpu (void ) +{ +/* +* Clear the per-CPU former VCPU pointer because hypervisor CSR state +* will be lost. This ensures that the next VCPU entry will properly +* restore all CSRs instead of incorrectly skipping CSR restoration +* via the fast-path optimization. +*/ +__this_cpu_write (kvm_former_vcpu,NULL ); +} +const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),STATS_DESC_COUNTER (VCPU , ecall_exit_stat),
Signature · 1 line
2.43.7
PATCH v2 2/2 KVM: RISC-V: Add CPU PM notifier for non-retention idle states
In reply to Yong-Xuan WangRegister a CPU_PM notifier to restore hypervisor CSR content during CPU non-retention idle states. When a CPU enters a deep idle state that powers off the CPU domain, hypervisor CSRs and VS CSRs lose their state and must be saved before entry and restored after exit.
To avoid conflicts with KVM's existing syscore suspend/resume handlers, the notifier checks system_state and skips processing during system-wide suspend/resume operations, where the syscore mechanism in virt/kvm/kvm_main.c already handles the state transitions.
This completes KVM's power management coverage for RISC-V:
- CPU hotplug: handled by kvm_online_cpu/kvm_offline_cpu (cpuhp callbacks)
- System suspend: handled by kvm_suspend/kvm_resume (syscore ops)
- CPU idle (retention): no action needed, CSRs are retained
- CPU idle (non-retention): handled by this CPU_PM notifier
--- arch/riscv/include/asm/kvm_aia.h | 15 +++- arch/riscv/kvm/aia.c | 144 ++++++++++++++++++++++----------------- arch/riscv/kvm/main.c | 98 +++++++++++++++++++++----- 3 files changed, 174 insertions(+), 83 deletions(-)
arch/riscv/include/asm/kvm_aia.h +13 -2
@@ -165,8 +165,19 @@ int kvm_riscv_aia_alloc_hgei(int cpu, struct kvm_vcpu *owner, void __iomem **hgei_va, phys_addr_t *hgei_pa);void kvm_riscv_aia_free_hgei (int cpu,int hgei); -void kvm_riscv_aia_enable (void ); -void kvm_riscv_aia_disable (void ); +/** +* kvm_riscv_aia_enable() - Enable AIA support on current CPU +* @full_cleanup: true = full hardware init (CPU hotplug/module load), +* false = lightweight CSR restore (CPU non-retention idle resume) +*/ +void kvm_riscv_aia_enable (bool full_cleanup); + +/** +* kvm_riscv_aia_disable() - Disable AIA support on current CPU +* @full_cleanup: true = full hardware cleanup (CPU hotplug/module exit), +* false = lightweight CSR save (CPU non-retention idle entry) +*/ +void kvm_riscv_aia_disable (bool full_cleanup);int kvm_riscv_aia_init (void );void kvm_riscv_aia_exit (void );
arch/riscv/kvm/aia.c +81 -63
195 lines not on this page · raw message
arch/riscv/kvm/main.c +80 -18
145 lines not on this page · raw message
Signature · 1 line
2.43.7
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] IMSIC guest interrupt files (VS-files) are permanently lost during non-retentive idle.…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] IMSIC guest interrupt files (VS-files) are permanently lost during non-retentive idle.
- [High] Nested Acceleration (NACL) is permanently disabled after a CPU deep idle cycle, and doubly disabled on CPU offline.
- [High] The CPU PM notifier unconditionally restores KVM hardware state on idle exit and creates a dangerous race during module unload.
- [High] Aborted system suspend bypasses state restoration, causing silent KVM hardware state loss.
--
commit 7c6e618531e4da8059b75390623f67b52d8838ff
Author: Yong-Xuan Wang <[email protected]>
KVM: RISC-V: Add CPU PM notifier for non-retention idle states
This commit adds a CPU PM notifier to save and restore hypervisor CSRs during non-retention idle states. It checks system_state to avoid conflicts with KVM's existing syscore suspend/resume handlers.
Quoted 4 lines
arch/riscv/kvm/aia.c +0
-0
[ ... ]
Quoted 62 lines
arch/riscv/kvm/aia.c +29
-25@@ -609,42 +620,49 @@ void kvm_riscv_aia_disable(void)if (__riscv_isa_extension_available (NULL ,RISCV_ISA_EXT_SSCOFPMF ))csr_clear (CSR_HVIEN ,BIT (IRQ_PMU_OVF )); -/* Disable per-CPU SGEI interrupt */ -csr_clear (CSR_HIE ,BIT (IRQ_S_GEXT )); -disable_percpu_irq (hgei_parent_irq); -csr_write (CSR_HVICTL ,aia_hvictl_value (false)); - -raw_spin_lock_irqsave (&hgctrl->lock, flags); - -for (i =0 ; i <= hgctrl->nr_hgei; i++) { - vcpu = hgctrl->owners[i]; -if (!vcpu) -continue ; +if (full_cleanup) { +/* Disable per-CPU SGEI interrupt */ +csr_clear (CSR_HIE ,BIT (IRQ_S_GEXT )); +disable_percpu_irq (hgei_parent_irq); + } -/* -* We release hgctrl->lock before notifying IMSIC -* so that we don't have lock ordering issues. -*/ -raw_spin_unlock_irqrestore (&hgctrl->lock, flags); +csr_write (CSR_HVICTL ,aia_hvictl_value (false)); -/* Notify IMSIC */ -kvm_riscv_vcpu_aia_imsic_release (vcpu); +if (full_cleanup) { +raw_spin_lock_irqsave (&hgctrl->lock, flags); -/* -* Wakeup VCPU if it was blocked so that it can -* run on other HARTs -*/ -if (csr_read (CSR_HGEIE ) &BIT (i)) { -csr_clear (CSR_HGEIE ,BIT (i)); -kvm_vcpu_kick (vcpu); +for (i =0 ; i <= hgctrl->nr_hgei; i++) { + vcpu = hgctrl->owners[i]; +if (!vcpu) +continue ; + +/* +* We release hgctrl->lock before notifying IMSIC +* so that we don't have lock ordering issues. +*/ +raw_spin_unlock_irqrestore (&hgctrl->lock, flags); + +/* Notify IMSIC */ +kvm_riscv_vcpu_aia_imsic_release (vcpu); + +/* +* Wakeup VCPU if it was blocked so that it can +* run on other HARTs +*/ +if (csr_read (CSR_HGEIE ) &BIT (i)) { +csr_clear (CSR_HGEIE ,BIT (i)); +kvm_vcpu_kick (vcpu);
arch/riscv/kvm/aia.c void kvm_riscv_aia_disable(void) · line 664
Quoted 10 lines
arch/riscv/kvm/aia.c +6
-1+ } + +raw_spin_lock_irqsave (&hgctrl->lock, flags); } -raw_spin_lock_irqsave (&hgctrl->lock, flags); +raw_spin_unlock_irqrestore (&hgctrl->lock, flags); + }else { + hgctrl->saved_value =csr_read (CSR_HGEIE ); }
[Severity: Critical]
Does skipping kvm_riscv_vcpu_aia_imsic_release() on the CPU_PM_ENTER path cause IMSIC guest interrupt files (VS-files) to be permanently lost?
Since KVM RISC-V AIA uses lazy release for VS-files, vCPUs can remain attached to the hardware IMSIC even when preempted. When a CPU enters deep idle, the IMSIC hardware powers off, wiping the VS-file state.
This else branch skips saving the active VS-files to software, which means the state is destroyed by power loss and not recovered upon wake. This could cause running VMs to hang or drop devices.
Quoted 10 lines
arch/riscv/kvm/aia.c +0
-2-raw_spin_unlock_irqrestore (&hgctrl->lock, flags); -put_cpu_ptr (&aia_hgei); }arch/riscv/kvm/main.c +0
-0
[ ... ]
Quoted 18 lines
arch/riscv/kvm/main.c +2
-7@@ -33,14 +34,9 @@ long kvm_arch_dev_ioctl(struct file *filp,return -EINVAL ; } -int kvm_arch_enable_virtualization_cpu (void ) +/* Initialize hypervisor CSRs - called during CPU online and non-retention idle resume */ +static void kvm_riscv_csr_enable (void ) { -int rc; - - rc =kvm_riscv_nacl_enable (); -if (rc) -return rc; -csr_write (CSR_HEDELEG ,0 );csr_write (CSR_HIDELEG ,0 );
arch/riscv/kvm/main.c int kvm_arch_enable_virtualization_cpu(void) · line 47
Quoted 8 lines
arch/riscv/kvm/main.c +0
-4@@ -48,16 +44,11 @@ int kvm_arch_enable_virtualization_cpu(void)csr_write (CSR_HCOUNTEREN ,0x02 );csr_write (CSR_HVIP ,0 ); - -kvm_riscv_aia_enable (); - -return 0 ; }
[Severity: High]
Should this function call kvm_riscv_nacl_enable() to pair with the disable call in kvm_riscv_csr_disable()?
Without it, the Nested Acceleration (NACL) shared memory feature is permanently broken on a CPU once it enters deep idle, leading to silent failures or state corruption for nested virtualization upon wake-up.
arch/riscv/kvm/main.c void kvm_arch_disable_virtualization_cpu(void) · line 66
Quoted 13 lines
arch/riscv/kvm/main.c +2
-3-void kvm_arch_disable_virtualization_cpu (void ) +/* Clear hypervisor CSRs - called during CPU offline and non-retention idle entry */ +static void kvm_riscv_csr_disable (void ) { -kvm_riscv_aia_disable (); -/* * After clearing the hideleg CSR, the host kernel will receive * spurious interrupts if hvip CSR has pending interrupts and the @@ -74,6 +65,59 @@ void kvm_arch_disable_virtualization_cpu(void)kvm_riscv_nacl_disable (); }
[Severity: High]
Does leaving kvm_riscv_nacl_disable() inside this refactored function cause unintended side effects?
Because kvm_riscv_csr_disable() is now called by the CPU_PM_ENTER path, NACL is explicitly torn down on every deep idle entry. Additionally, it causes a double-disable of NACL during CPU offline operations since kvm_arch_disable_virtualization_cpu() calls kvm_riscv_nacl_disable() immediately after this function.
[ ... ]
arch/riscv/kvm/main.c void kvm_arch_disable_virtualization_cpu(void) · line 98
Quoted 10 lines
arch/riscv/kvm/main.c +10
-0+static int kvm_riscv_cpu_pm_notifier (struct notifier_block *self,unsigned long cmd,void *v) +{ +/* +* To avoid redundant disable/enable operations (since KVM's +* syscore ops will handle the state during system suspend), +* ignore CPU_PM events when a system suspend/resume is in +* progress. +*/ +if (system_state ==SYSTEM_SUSPEND ) +return NOTIFY_DONE ;
[Severity: High]
Could this check cause silent KVM hardware state loss if the suspend cycle is aborted?
The kernel enters SYSTEM_SUSPEND early in the suspend process. If a CPU enters deep idle while devices are still suspending, this notifier ignores it, and the non-retentive idle zeroes out KVM CSRs.
If device suspend fails, the system aborts the suspend cycle and resumes to SYSTEM_RUNNING without ever reaching KVM's syscore suspend/resume hooks. The CPU wakes up with lost KVM state, and VMs resume execution with missing interrupts and CSR configurations.
arch/riscv/kvm/main.c void kvm_arch_disable_virtualization_cpu(void) · line 105
Quoted 7 lines
arch/riscv/kvm/main.c +7
-0+ +switch (cmd) { +case CPU_PM_EXIT : +case CPU_PM_ENTER_FAILED : +kvm_riscv_csr_enable (); +kvm_riscv_aia_enable (false); +return NOTIFY_OK ;
[Severity: High]
Does this unconditionally restore hypervisor CSRs regardless of whether KVM hardware virtualization is actually enabled on the CPU?
Host CPUs might run with active hypervisor CSRs (like IRQ_S_GEXT in CSR_HIE enabled) even when KVM has no active VMs or is actively being unloaded. This can lead to kernel panics if a spurious interrupt hits the hypervisor while KVM is not meant to handle it.
Quoted 10 lines
arch/riscv/kvm/main.c +10
-0+case CPU_PM_ENTER : +kvm_riscv_aia_disable (false); +kvm_riscv_csr_disable (); +return NOTIFY_OK ; +default : +break ; + } + +return NOTIFY_DONE ; +}
[ ... ]
arch/riscv/kvm/main.c static void __exit riscv_kvm_exit(void) · line 259
Quoted 11 lines
arch/riscv/kvm/main.c +4
-0@@ -192,6 +250,10 @@ static void __exit riscv_kvm_exit(void) {kvm_exit (); +/* Unregister CPU PM notifier */ +if (IS_ENABLED (CONFIG_CPU_PM )) +cpu_pm_unregister_notifier (&kvm_riscv_cpu_pm_nb); +kvm_riscv_teardown (); }module_exit (riscv_kvm_exit);
[Severity: High]
Is there a race condition here since the CPU PM notifier is unregistered after kvm_exit()?
During riscv_kvm_exit(), kvm_exit() disables hardware virtualization. However, if a CPU enters and exits deep idle within this teardown window before the notifier is unregistered, the PM notifier will spuriously reactivate hypervisor CSRs just before the KVM module is removed.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2