RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries
Subsystems (3)
PATCH v3 1/1 RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries
From: Hui Min Mina Chou <[email protected]>
If multiple VCPUs of the same Guest/VM run on the same Host CPU, hfence.vvma only flushes that Host CPU’s VS-stage TLB. Other Host CPUs may retain stale VS-stage entries. When a VCPU later migrates to a different Host CPU, it can hit these stale GVA to GPA mappings, causing unexpected faults in the Guest.
To fix this, kvm_riscv_gstage_vmid_sanitize() is extended to flush both G-stage and VS-stage TLBs whenever a VCPU migrates to a different Host CPU. This ensures that no stale VS-stage mappings remain after VCPU migration.
---
v3:
- Resolved build warning; updated header declaration and call side to kvm_riscv_local_tlb_sanitize
v2:
- Updated Fixes commit to 92e450507d56
- Renamed function to kvm_riscv_local_tlb_sanitize
arch/riscv/include/asm/kvm_vmid.h | 2 +- arch/riscv/kvm/vcpu.c | 2 +- arch/riscv/kvm/vmid.c | 8 +++++++- 3 files changed, 9 insertions(+), 3 deletions(-)
arch/riscv/include/asm/kvm_vmid.h +1 -1
@@ -22,6 +22,6 @@ unsigned long kvm_riscv_gstage_vmid_bits(void);int kvm_riscv_gstage_vmid_init (struct kvm *kvm);bool kvm_riscv_gstage_vmid_ver_changed (struct kvm_vmid *vmid);void kvm_riscv_gstage_vmid_update (struct kvm_vcpu *vcpu); -void kvm_riscv_gstage_vmid_sanitize (struct kvm_vcpu *vcpu); +void kvm_riscv_local_tlb_sanitize (struct kvm_vcpu *vcpu);#endif
arch/riscv/kvm/vcpu.c +1 -1
@@ -968,7 +968,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)* Note: This should be done after G-stage VMID has been * updated using kvm_riscv_gstage_vmid_ver_changed() */ -kvm_riscv_gstage_vmid_sanitize (vcpu); +kvm_riscv_local_tlb_sanitize (vcpu);trace_kvm_entry (vcpu);
arch/riscv/kvm/vmid.c +7 -1
@@ -125,7 +125,7 @@ void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu)kvm_make_request (KVM_REQ_UPDATE_HGATP , v); } -void kvm_riscv_gstage_vmid_sanitize (struct kvm_vcpu *vcpu) +void kvm_riscv_local_tlb_sanitize (struct kvm_vcpu *vcpu) {unsigned long vmid;
@@ -146,4 +146,10 @@ void kvm_riscv_gstage_vmid_sanitize(struct kvm_vcpu *vcpu) vmid =READ_ONCE (vcpu->kvm->arch.vmid.vmid);kvm_riscv_local_hfence_gvma_vmid_all (vmid); + +/* +* Flush VS-stage TLBs entry after VCPU migration to avoid using +* stale entries. +*/ +kvm_riscv_local_hfence_vvma_all (vmid); }
Signature · 1 line
2.34.1
Anup Patel
maintainerQuoted 9 lines · Hui Min Mina Chou
From: Hui Min Mina Chou <[email protected]>
If multiple VCPUs of the same Guest/VM run on the same Host CPU, hfence.vvma only flushes that Host CPU’s VS-stage TLB. Other Host CPUs may retain stale VS-stage entries. When a VCPU later migrates to a different Host CPU, it can hit these stale GVA to GPA mappings, causing unexpected faults in the Guest.
Quoted 8 lines
To fix this,
kvm_riscv_gstage_vmid_sanitize()is extended to flush both G-stage and VS-stage TLBs whenever a VCPU migrates to a different Host CPU. This ensures that no stale VS-stage mappings remain after VCPU migration.Fixes 92e450507d56 ("RISC-V: KVM: Cleanup stale TLB entries when host CPU changes")Signed-off-by Hui Min Mina Chou <[email protected]>Signed-off-by Ben Zong-You Xie <[email protected]>Reviewed-by Radim Krčmář <[email protected]>
My comments on v2 are not addressed.
Regards,
Anup
Quoted 66 lines
---
v3:
- Resolved build warning; updated header declaration and call side tokvm_riscv_local_tlb_sanitizev2:
- Updated Fixes commit to92e450507d56
- Renamed function tokvm_riscv_local_tlb_sanitizearch/riscv/include/asm/kvm_vmid.h | 2 +- arch/riscv/kvm/vcpu.c | 2 +- arch/riscv/kvm/vmid.c | 8 +++++++- 3 files changed, 9 insertions(+), 3 deletions(-)arch/riscv/include/asm/kvm_vmid.h +1
-1@@ -22,6 +22,6 @@ unsigned long kvm_riscv_gstage_vmid_bits(void);int kvm_riscv_gstage_vmid_init (struct kvm *kvm);bool kvm_riscv_gstage_vmid_ver_changed (struct kvm_vmid *vmid);void kvm_riscv_gstage_vmid_update (struct kvm_vcpu *vcpu); -void kvm_riscv_gstage_vmid_sanitize (struct kvm_vcpu *vcpu); +void kvm_riscv_local_tlb_sanitize (struct kvm_vcpu *vcpu);#endif arch/riscv/kvm/vcpu.c +1
-1@@ -968,7 +968,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)* Note: This should be done after G-stage VMID has been * updated using kvm_riscv_gstage_vmid_ver_changed() */ -kvm_riscv_gstage_vmid_sanitize (vcpu); +kvm_riscv_local_tlb_sanitize (vcpu);trace_kvm_entry (vcpu);arch/riscv/kvm/vmid.c +7
-2@@ -125,7 +125,7 @@ void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu)kvm_make_request (KVM_REQ_UPDATE_HGATP , v); } -void kvm_riscv_gstage_vmid_sanitize (struct kvm_vcpu *vcpu) +void kvm_riscv_local_tlb_sanitize (struct kvm_vcpu *vcpu) {unsigned long vmid;@@ -146,4 +146,10 @@ void kvm_riscv_gstage_vmid_sanitize(struct kvm_vcpu *vcpu) vmid =READ_ONCE (vcpu->kvm->arch.vmid.vmid);kvm_riscv_local_hfence_gvma_vmid_all (vmid); + +/* +* Flush VS-stage TLBs entry after VCPU migration to avoid using +* stale entries. +*/ +kvm_riscv_local_hfence_vvma_all (vmid); } --2.34.1