RISC-V: KVM: Sanitize TLB on both previous and next host CPU
Subsystems (3)
PATCH 1/1 RISC-V: KVM: Sanitize TLB on both previous and next host CPU
Currently, KVM RISC-V only sanitize TLB on the next host CPU after VCPU migration but the VCPU might have left stale TLBs on the previous host CPU which may affect other VCPUs of the same Guest.
To address the above issue, sanitize TLB on previous host CPU before the VCPU runs on the next host CPU. This can be achieved by introducing per-VCPU sanitize TLB mask where each VCPU sanitizes TLB before running if current host CPU is set in it's sanitize TLB mask. The VCPU must also kick other VCPU of same Guest after setting previous host CPU in their sanitize TLB mask.
--- arch/riscv/include/asm/kvm_host.h | 4 ++++ arch/riscv/kvm/tlb.c | 29 +++++++++++++++++++++++++---- arch/riscv/kvm/vcpu.c | 2 ++ 3 files changed, 31 insertions(+), 4 deletions(-)
arch/riscv/include/asm/kvm_host.h +4 -0
@@ -9,6 +9,7 @@#ifndef __RISCV_KVM_HOST_H__#define __RISCV_KVM_HOST_H__ +#include <linux/cpumask.h> #include <linux/types.h> #include <linux/kvm.h> #include <linux/kvm_types.h>
@@ -186,6 +187,9 @@ struct kvm_vcpu_arch {/* ISA feature bits (similar to MISA) */ DECLARE_BITMAP (isa,RISCV_ISA_EXT_MAX ); +/* Sanitize TLB host CPU mask */ +cpumask_t tlb_sanitize_mask; +/* Vendor, Arch, and Implementation details */ unsigned long mvendorid;unsigned long marchid;
arch/riscv/kvm/tlb.c +25 -4
@@ -207,11 +207,32 @@ void kvm_riscv_local_hfence_vvma_all(unsigned long vmid)void kvm_riscv_local_tlb_sanitize (struct kvm_vcpu *vcpu) { -unsigned long vmid; +struct kvm_vcpu *tvcpu; +unsigned long i, vmid; -if (!kvm_riscv_gstage_vmid_bits () || - vcpu->arch.last_exit_cpu == vcpu->cpu) -return ; +if (!cpumask_test_cpu (vcpu->cpu, &vcpu->arch.tlb_sanitize_mask)) { +if (!kvm_riscv_gstage_vmid_bits () || + vcpu->arch.last_exit_cpu == vcpu->cpu) +return ; + } + +/* +* The VCPU might have left stale TLBs on the previous host CPU so +* mark previous host CPU in the tlb_sanitize_mask of other VCPUs +* and force VM-exit for other VCPUs using kvm_vcpu_kick(). +*/ +if (vcpu->arch.last_exit_cpu >-1 && vcpu->arch.last_exit_cpu != vcpu->cpu) { +kvm_for_each_vcpu (i, tvcpu, vcpu->kvm) { +if (tvcpu == vcpu) +continue ; +cpumask_set_cpu (vcpu->arch.last_exit_cpu, + &tvcpu->arch.tlb_sanitize_mask); +kvm_vcpu_kick (tvcpu); + } + } + +/* Clear current host CPU from the tlb_sanitize_mask */ +cpumask_clear_cpu (vcpu->cpu, &vcpu->arch.tlb_sanitize_mask);/* * On RISC-V platforms with hardware VMID support, we share same
arch/riscv/kvm/vcpu.c +2 -0
@@ -107,6 +107,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu, bool kvm_sbi_reset) vcpu->arch.last_exit_cpu =-1 ; +cpumask_clear (&vcpu->arch.tlb_sanitize_mask); +kvm_riscv_vcpu_context_reset (vcpu, kvm_sbi_reset);kvm_riscv_vcpu_fp_reset (vcpu);
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] RISC-V: KVM: unconditional kvm_vcpu_kick() in kvm_riscv_local_tlb_sanitize() - [High] RISC-V: KVM…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] RISC-V: KVM: unconditional kvm_vcpu_kick() in kvm_riscv_local_tlb_sanitize()
- [High] RISC-V: KVM: deferred and async tlb_sanitize_mask update in kvm_riscv_local_tlb_sanitize()
- [Medium] RISC-V: KVM: kvm_riscv_reset_vcpu() clears last_exit_cpu before TLB sanitize
- [Medium] RISC-V: KVM: HFENCE requests consumed before kvm_riscv_local_tlb_sanitize()
--
commit fb6737229a46ed1835b0561db70c3203e3250bc6
Author: Anup Patel <[email protected]>
RISC-V: KVM: Sanitize TLB on both previous and next host CPU
This commit adds a per-vCPU cpumask to track host CPUs that need TLB sanitization after a vCPU migrates, kicking sibling vCPUs when setting their masks.
Quoted 10 lines
arch/riscv/kvm/tlb.c +1
-1@@ -207,11 +207,32 @@ void kvm_riscv_local_hfence_vvma_all(unsigned long vmid)void kvm_riscv_local_tlb_sanitize (struct kvm_vcpu *vcpu) { -unsigned long vmid; +struct kvm_vcpu *tvcpu;
arch/riscv/kvm/tlb.c void kvm_riscv_local_hfence_vvma_all(unsigned long vmid) · line 217
Quoted 10 lines
arch/riscv/kvm/tlb.c +6
-3+unsigned long i, vmid; -if (!kvm_riscv_gstage_vmid_bits () || - vcpu->arch.last_exit_cpu == vcpu->cpu) -return ; +if (!cpumask_test_cpu (vcpu->cpu, &vcpu->arch.tlb_sanitize_mask)) { +if (!kvm_riscv_gstage_vmid_bits () || + vcpu->arch.last_exit_cpu == vcpu->cpu) +return ; + }
[Severity: High]
Can a vCPU still run with stale TLB entries left by a sibling vCPU on the same host CPU when the sibling has not yet entered guest mode on another CPU?
tlb_sanitize_mask is only updated when the migrating vCPU reaches kvm_riscv_local_tlb_sanitize() on a new host CPU:
CPU0 CPU1
vcpu1 runs in guest mode
vcpu1 exits (last_exit_cpu = 0)
vcpu1 schedules out
vcpu0 runs in guest mode
(populates CPU0 TLB)
vcpu0 exits (last_exit_cpu = 0)
vcpu0 blocks in WFI or schedules out
vcpu1 schedules back on CPU0kvm_riscv_local_tlb_sanitize(vcpu1)
tlb_sanitize_mask has bit 0 clear
last_exit_cpu == vcpu1->cpu (0 == 0)
returns without flushing (vcpu0 has not entered guest mode)
vcpu1 enters guest mode <-- uses vcpu0's stale TLB entries
If vcpu0 and vcpu1 time-share CPU0 without migrating to another CPU, last_exit_cpu == vcpu->cpu holds for both and CPU0's TLB is never sanitized across switches between them.
Also, when vcpu0 does migrate to CPU1 while vcpu1 is running in guest mode on CPU0, kvm_vcpu_kick() calls __kvm_vcpu_kick(tvcpu, false), which sends an asynchronous reschedule IPI and returns immediately. vcpu0 then enters guest mode on CPU1 before vcpu1 has exited guest mode and flushed CPU0's TLB.
[Severity: Medium]
Can a pending HFENCE or TLB flush request for a vCPU's previous host CPU be lost if the vCPU migrates to another CPU and then migrates back before reaching kvm_riscv_local_tlb_sanitize()?
In kvm_arch_vcpu_ioctl_run(), kvm_riscv_check_vcpu_requests() runs with preemption enabled before kvm_riscv_local_tlb_sanitize():
CPU0 CPU1
vcpu0 exits (last_exit_cpu = 0)
(KVM_REQ_HFENCE queued on vcpu0)
vcpu0 schedules on CPU1 kvm_riscv_check_vcpu_requests(vcpu0)
clears KVM_REQ_HFENCE flushes TLB on CPU1
(preempted or guest entry aborts)
vcpu0 schedules back on CPU0 kvm_riscv_local_tlb_sanitize(vcpu0)
tlb_sanitize_mask has bit 0 clear
last_exit_cpu == vcpu0->cpu (0 == 0)
returns without flushing <-- HFENCE on CPU0 lost
Quoted 5 lines
arch/riscv/kvm/tlb.c +5
-0+ +/* +* The VCPU might have left stale TLBs on the previous host CPU so +* mark previous host CPU in the tlb_sanitize_mask of other VCPUs +* and force VM-exit for other VCPUs using kvm_vcpu_kick().
arch/riscv/kvm/tlb.c void kvm_riscv_local_hfence_vvma_all(unsigned long vmid) · line 232
Quoted 10 lines
arch/riscv/kvm/tlb.c +10
-0+*/ +if (vcpu->arch.last_exit_cpu >-1 && vcpu->arch.last_exit_cpu != vcpu->cpu) { +kvm_for_each_vcpu (i, tvcpu, vcpu->kvm) { +if (tvcpu == vcpu) +continue ; +cpumask_set_cpu (vcpu->arch.last_exit_cpu, + &tvcpu->arch.tlb_sanitize_mask); +kvm_vcpu_kick (tvcpu); + } + }
[Severity: High]
Does calling kvm_vcpu_kick(tvcpu) unconditionally for every other vCPU in the VM force unnecessary VM-exits and spurious WFI wakeups on every vCPU migration?
Only a sibling vCPU currently executing on vcpu->arch.last_exit_cpu can observe stale TLB entries on that physical CPU. Kicking every tvcpu sends reschedule IPIs and forces VM-exits on sibling vCPUs running on unrelated host CPUs (which exit and re-enter without flushing because their CPU is not set in tlb_sanitize_mask). For sibling vCPUs sleeping in guest WFI, kvm_vcpu_kick() calls kvm_vcpu_wake_up(), which wakes them from kvm_vcpu_block() even though they are already outside guest mode, inflating halt_wakeup stats and shrinking tvcpu->halt_poll_ns.
In addition, because vcpu->arch.last_exit_cpu is set in every sibling vCPU's tlb_sanitize_mask and cpumask_clear_cpu() only clears it on the single vCPU that sanitizes that CPU, the remaining N - 2 vCPUs retain the bit and perform redundant HFENCE flushes whenever they later run on that host CPU.
arch/riscv/kvm/vcpu.c static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu, bool kvm_sbi_reset) · line 112
Quoted 11 lines
arch/riscv/kvm/vcpu.c +2
-0@@ -107,6 +107,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu, bool kvm_sbi_reset) vcpu->arch.last_exit_cpu =-1 ; +cpumask_clear (&vcpu->arch.tlb_sanitize_mask); +kvm_riscv_vcpu_context_reset (vcpu, kvm_sbi_reset);
[Severity: Medium]
Does resetting vcpu->arch.last_exit_cpu to -1 in kvm_riscv_reset_vcpu() drop the pre-reset host CPU before kvm_riscv_local_tlb_sanitize() can propagate it to sibling vCPUs?
When a vCPU is reset at runtime via KVM_REQ_VCPU_RESET (from SBI HSM hart_start) or KVM_MP_STATE_INIT_RECEIVED, kvm_riscv_reset_vcpu() runs before kvm_riscv_local_tlb_sanitize(). Because last_exit_cpu is already -1 when the restarted vCPU reaches kvm_riscv_local_tlb_sanitize(), the previous host CPU is never set in sibling vCPUs' tlb_sanitize_mask, and clearing vcpu->arch.tlb_sanitize_mask also drops any pending sanitization requests already queued for this vCPU.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1