Cancel the hrtimer used to emulate a VMX Preemption Timer for L2 when
freeing nested state instead of precisely canceling the timer if and only
if the vCPU is/was in L2.  hrtimer_cancel() bails early if the timer isn't
running, and KVM initializes the timer when allocating nested state, i.e.
canceling the timer even if it's not fully in-use is glorified nop.

Canceling the timer even if L1 is active will allow KVM to leave the vCPU
in L2 without having to retain an "is_guest_mode()" check just to cancel
timer.

Signed-off-by: Sean Christopherson <[email protected]>
---
 arch/x86/kvm/vmx/nested.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 1b30ad7731ee..553915015a75 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -339,6 +339,8 @@ static void free_nested(struct kvm_vcpu *vcpu)
 
        kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
 
+       hrtimer_cancel(&vmx->nested.preemption_timer);
+
        vmx->nested.vmxon = false;
        vmx->nested.smm.vmxon = false;
        vmx->nested.vmxon_ptr = INVALID_GPA;
@@ -401,7 +403,6 @@ void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
 
        vcpu_load(vcpu);
        if (is_guest_mode(vcpu)) {
-               hrtimer_cancel(&vmx->nested.preemption_timer);
                leave_guest_mode(vcpu);
                vmx_switch_vmcs(vcpu, &vmx->vmcs01);
        }
-- 
2.56.0.307.g5fe7bd680c-goog


Reply via email to