[RFC PATCH 21/24] KVM: nSVM: Service local TLB flushes before nested transitions
From: Yosry Ahmed
Date: Wed Mar 26 2025 - 15:47:51 EST
KVM does not track TLB flush requests for L1 vs. L2. Hence, service
local flush that target the current context before switching to a new
one. Since the current ASID is identified through the current VMCB
(nested or not), service the flushes before every VMCB switch.
This is conceptually similar to how nVMX calls
kvm_service_local_tlb_flush_requests() in
nested_vmx_enter_non_root_mode() and nested_vmx_vmexit(), with the
following differences:
1. VMX tracks the current VPID based on is_guest_mode(), so local TLB
flushes are serviced before enter_guest_mode() and
leave_guest_mode(). On the other hand, SVM tracks the current ASID
based on the current VMCB, so the TLB flushes are serviced before an
VMCB switch.
2. nVMX only enters and leaves guest mode in
nested_vmx_enter_non_root_mode() and nested_vmx_vmexit(). Other paths
like vmx_set_nested_state() and vmx_leave_nested() call into these
two functions. On the other hand, nSVM open codes the switch in
functions like svm_set_nested_state() and svm_leave_nested(), so
servicing the flush in svm_switch_svm() is probably most reliable.
Reviewed-by: Maxim Levitsky <mlevitsk@xxxxxxxxxx>
Signed-off-by: Yosry Ahmed <yosry.ahmed@xxxxxxxxx>
---
arch/x86/kvm/svm/svm.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 3e33ac876eb32..3649707c61d3e 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -1439,6 +1439,12 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)
{
+ /*
+ * The current ASID is identified through the VMCB. Perform any pending
+ * TLB flushes for the current VMCB before switching to a new one.
+ */
+ kvm_service_local_tlb_flush_requests(&svm->vcpu);
+
svm->current_vmcb = target_vmcb;
svm->vmcb = target_vmcb->ptr;
}
--
2.49.0.395.g12beb8f557-goog