KVM: VMX: Handle forced exit due to preemption timer in fastpath
authorSean Christopherson <seanjc@google.com>
Wed, 10 Jan 2024 01:27:02 +0000 (17:27 -0800)
committerSean Christopherson <seanjc@google.com>
Fri, 23 Feb 2024 00:22:36 +0000 (16:22 -0800)
Handle VMX preemption timer VM-Exits due to KVM forcing an exit in the
exit fastpath, i.e. avoid calling back into handle_preemption_timer() for
the same exit.  There is no work to be done for forced exits, as the name
suggests the goal is purely to get control back in KVM.

In addition to shaving a few cycles, this will allow cleanly separating
handle_fastpath_preemption_timer() from handle_preemption_timer(), e.g.
it's not immediately obvious why _apparently_ calling
handle_fastpath_preemption_timer() twice on a "slow" exit is necessary:
the "slow" call is necessary to handle exits from L2, which are excluded
from the fastpath by vmx_vcpu_run().

Link: https://lore.kernel.org/r/20240110012705.506918-4-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/vmx/vmx.c

index a44b09e35fd6ec02fd629b131d176aa9783f9e8d..7bd360b5bfb95e414054bf22c89fc9b373d3c8a4 100644 (file)
@@ -6000,12 +6000,15 @@ static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu)
        if (unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled))
                return EXIT_FASTPATH_REENTER_GUEST;
 
-       if (!vmx->req_immediate_exit) {
-               kvm_lapic_expired_hv_timer(vcpu);
-               return EXIT_FASTPATH_REENTER_GUEST;
-       }
+       /*
+        * If the timer expired because KVM used it to force an immediate exit,
+        * then mission accomplished.
+        */
+       if (vmx->req_immediate_exit)
+               return EXIT_FASTPATH_EXIT_HANDLED;
 
-       return EXIT_FASTPATH_NONE;
+       kvm_lapic_expired_hv_timer(vcpu);
+       return EXIT_FASTPATH_REENTER_GUEST;
 }
 
 static int handle_preemption_timer(struct kvm_vcpu *vcpu)