KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core
Subsystems (3)
PATCH v6 1/1 KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core
Currently, kvm_arch_vcpu_load() unconditionally restores guest CSRs and HGATP. However, when a VCPU is loaded back on the same physical CPU, and no other KVM VCPU has run on this CPU since it was last put, the hardware CSRs are still valid.
This patch optimizes the vcpu_load path by skipping the expensive CSR writes if all the following conditions are met:
1. It is being reloaded on the same CPU (vcpu->arch.last_exit_cpu == cpu).
2. The CSRs are not dirty (!vcpu->arch.csr_dirty).
3. No other VCPU used this CPU (vcpu == __this_cpu_read(kvm_former_vcpu)).
To ensure this fast-path doesn't break corner cases:
- Live migration and VCPU reset are naturally safe. KVM initializes last_exit_cpu to -1, which guarantees the fast-path won't trigger.
- A new 'csr_dirty' flag is introduced to track runtime userspace interventions. If userspace modifies guest configurations (e.g., hedeleg via KVM_SET_GUEST_DEBUG, or CSRs via KVM_SET_ONE_REG) while the VCPU is preempted, the flag is set to skip fast path.
Note that kvm_riscv_vcpu_aia_load() is kept outside the skip logic to ensure IMSIC/AIA interrupt states are always properly synchronized.
---
v5 -> v6:
As suggested by Andrew Jones, checking 'last_exit_cpu' first (most likely to fail on busy hosts) and placing the expensive __this_cpu_read() last, skipping __this_cpu_write() in kvm_arch_vcpu_put() if kvm_former_vcpu is already set to the current VCPU.
v4 -> v5:
- Dropped the 'vcpu->scheduled_out' check as Andrew Jones pointed out, relying on 'last_exit_cpu', 'former_vcpu', and '!csr_dirty' is sufficient and safe. This expands the optimization to cover many userspace exits (e.g., MMIO) as well.
- Added a block comment in kvm_arch_vcpu_load() to warn future developers about maintaining the 'csr_dirty' dependency, as Andrew's suggestion to reduce fragility.
- Removed unnecessary single-line comments and fixed indentation nits.
v3 -> v4:
- Addressed Anup Patel's review regarding hardware state inconsistency.
- Introduced 'csr_dirty' flag to track dynamic userspace CSR/CONFIG modifications (KVM_SET_ONE_REG, KVM_SET_GUEST_DEBUG), forcing a full restore when debugging or modifying states at userspace.
- Kept kvm_riscv_vcpu_aia_load() out of the skip block to resolve IMSIC VS-file instability.
v2 -> v3:
v2 was missing a critical check because I generated the patch from my wrong (experimental) branch. This is fixed in v3. Sorry for my trouble.
v1 -> v2:
Apply the logic to aia csr load. Thanks for Andrew Jones's advice.
--- arch/riscv/include/asm/kvm_host.h | 3 +++ arch/riscv/kvm/vcpu.c | 22 ++++++++++++++++++++++ arch/riscv/kvm/vcpu_onereg.c | 2 ++ 3 files changed, 27 insertions(+)
arch/riscv/include/asm/kvm_host.h +3 -0
@@ -273,6 +273,9 @@ struct kvm_vcpu_arch {/* 'static' configurations which are set only once */ struct kvm_vcpu_config cfg; +/* Indicates modified guest CSRs */ +bool csr_dirty; +/* SBI steal-time accounting */ struct {gpa_t shmem;
arch/riscv/kvm/vcpu.c +22 -0
@@ -24,6 +24,8 @@#define CREATE_TRACE_POINTS #include "trace.h" +static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_former_vcpu); +const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),STATS_DESC_COUNTER (VCPU , ecall_exit_stat),
@@ -537,6 +539,8 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, vcpu->arch.cfg.hedeleg |=BIT (EXC_BREAKPOINT ); } + vcpu->arch.csr_dirty = true; +return 0 ; }
@@ -581,6 +585,20 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;struct kvm_vcpu_config *cfg = &vcpu->arch.cfg; +/* +* If VCPU is being reloaded on the same physical CPU and no +* other KVM VCPU has run on this CPU since it was last put, +* we can skip the expensive CSR and HGATP writes. +* +* Note: If a new CSR is added to this fast-path skip block, +* make sure that 'csr_dirty' is set to true in any +* ioctl (e.g., KVM_SET_ONE_REG) that modifies it. +*/ +if (vcpu->arch.last_exit_cpu == cpu && !vcpu->arch.csr_dirty && + vcpu ==__this_cpu_read (kvm_former_vcpu)) + goto csr_restore_done; + + vcpu->arch.csr_dirty = false;if (kvm_riscv_nacl_sync_csr_available ()) { nsh =nacl_shmem ();nacl_csr_write (nsh,CSR_VSSTATUS , csr->vsstatus);
@@ -624,6 +642,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_riscv_mmu_update_hgatp (vcpu); +csr_restore_done:kvm_riscv_vcpu_timer_restore (vcpu);kvm_riscv_vcpu_host_fp_save (&vcpu->arch.host_context);
@@ -645,6 +664,9 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)void *nsh;struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; +if (vcpu !=__this_cpu_read (kvm_former_vcpu)) +__this_cpu_write (kvm_former_vcpu, vcpu); + vcpu->cpu =-1 ;kvm_riscv_vcpu_aia_put (vcpu);
arch/riscv/kvm/vcpu_onereg.c +2 -0
@@ -652,6 +652,8 @@ static int kvm_riscv_vcpu_set_reg_csr(struct kvm_vcpu *vcpu,if (rc)return rc; + vcpu->arch.csr_dirty = true; +return 0 ; }
Signature · 7 lines
2.43.0 -- kvm-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kvm-riscv
2026-02-26T20:38:02+08:00, Jinyu Tang <[email protected]>:
Quoted 23 lines
Currently,
kvm_arch_vcpu_load()unconditionally restores guest CSRs and HGATP. However, when a VCPU is loaded back on the same physical CPU, and no other KVM VCPU has run on this CPU since it was last put, the hardware CSRs are still valid.This patch optimizes the
vcpu_loadpath by skipping the expensive CSR writes if all the following conditions are met:
1. It is being reloaded on the same CPU (vcpu->arch.last_exit_cpu== cpu).
2. The CSRs are not dirty (!vcpu->arch.csr_dirty).
3. No other VCPU used this CPU (vcpu ==__this_cpu_read(kvm_former_vcpu)).To ensure this fast-path doesn't break corner cases:
- Live migration and VCPU reset are naturally safe. KVM initializeslast_exit_cputo -1, which guarantees the fast-path won't trigger.
- A new 'csr_dirty' flag is introduced to track runtime userspace interventions. If userspace modifies guest configurations (e.g., hedeleg viaKVM_SET_GUEST_DEBUG, or CSRs viaKVM_SET_ONE_REG) while the VCPU is preempted, the flag is set to skip fast path.Note that
kvm_riscv_vcpu_aia_load()is kept outside the skip logic to ensure IMSIC/AIA interrupt states are always properly synchronized.
Quoted 8 lines
Signed-off-by Jinyu Tang <[email protected]>---
v3 -> v4:
- Introduced 'csr_dirty' flag to track dynamic userspace CSR/CONFIG modifications (KVM_SET_ONE_REG,KVM_SET_GUEST_DEBUG), forcing a full restore when debugging or modifying states at userspace.
- Keptkvm_riscv_vcpu_aia_load()out of the skip block to resolve IMSIC VS-file instability.
Excluding AIA is disturbing as we're writing only vsiselect, hviprio1, and hviprio2... It seems to me that it should be fine to optimize the AIA CSRs too.
Wasn't the issue that you originally didn't track csr_dirty, and the bug just manifested through IMSICs?
Quoted 7 lines
arch/riscv/kvm/vcpu.c +2
-0@@ -581,6 +585,20 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;struct kvm_vcpu_config *cfg = &vcpu->arch.cfg; +/* +* If VCPU is being reloaded on the same physical CPU and no
arch/riscv/kvm/vcpu.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 599
Quoted 10 lines
arch/riscv/kvm/vcpu.c +10
-0+* other KVM VCPU has run on this CPU since it was last put, +* we can skip the expensive CSR and HGATP writes. +* +* Note: If a new CSR is added to this fast-path skip block, +* make sure that 'csr_dirty' is set to true in any +* ioctl (e.g., KVM_SET_ONE_REG) that modifies it. +*/ +if (vcpu->arch.last_exit_cpu == cpu && !vcpu->arch.csr_dirty && + vcpu ==__this_cpu_read (kvm_former_vcpu)) + goto csr_restore_done;
I see a small optimization if we set the per-cpu variable here, instead of doing that in kvm_arch_vcpu_put:
if (vcpu != __this_cpu_read(kvm_former_vcpu)) __this_cpu_write(kvm_former_vcpu, vcpu); else if (vcpu->arch.last_exit_cpu == cpu && !vcpu->arch.csr_dirty) goto csr_restore_done;
This means we never have to read the per-cpu twice in the get/put
sequence: faster put at the cost of slightly slower get.
Thanks.