Skip to contentSkip to conversation

KVM: x86: Ensure deadline timer has truly expired before posting its IRQ

PATCH Applied Sean Christopherson 3 participants 3 messages Mar 5, 2021 – Mar 12, 2021

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

PATCH 1/1 KVM: x86: Ensure deadline timer has truly expired before posting its IRQ

When posting a deadline timer interrupt, open code the checks guarding __kvm_wait_lapic_expire() in order to skip the lapic_timer_int_injected() check in kvm_wait_lapic_expire(). The injection check will always fail since the interrupt has not yet be injected. Moving the call after injection would also be wrong as that wouldn't actually delay delivery of the IRQ if it is indeed sent via posted interrupt.

Fixes 010fd37fddf6 ("KVM: LAPIC: Reduce world switch latency caused by timer_advance_ns")
---
 arch/x86/kvm/lapic.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +10 -1
index 45d40bfacb7c..cb8ebfaccfb6 100644
@@ -1642,7 +1642,16 @@ static void apic_timer_expired(struct kvm_lapic *apic, bool from_timer_fn)
 	}
 
 	if (kvm_use_posted_timer_interrupt(apic->vcpu)) {
-		kvm_wait_lapic_expire(vcpu);
+		/*
+		 * Ensure the guest's timer has truly expired before posting an
+		 * interrupt.  Open code the relevant checks to avoid querying
+		 * lapic_timer_int_injected(), which will be false since the
+		 * interrupt isn't yet injected.  Waiting until after injecting
+		 * is not an option since that won't help a posted interrupt.
+		 */
+		if (vcpu->arch.apic->lapic_timer.expired_tscdeadline &&
+		    vcpu->arch.apic->lapic_timer.timer_advance_ns)
+			__kvm_wait_lapic_expire(vcpu);
 		kvm_apic_inject_pending_timer_irqs(apic);
 		return;
 	}
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
maintainer Applied
#1 lore raw
Quoted 27 lines · Sean Christopherson

When posting a deadline timer interrupt, open code the checks guarding __kvm_wait_lapic_expire() in order to skip the lapic_timer_int_injected() check in kvm_wait_lapic_expire(). The injection check will always fail since the interrupt has not yet be injected. Moving the call after injection would also be wrong as that wouldn't actually delay delivery of the IRQ if it is indeed sent via posted interrupt.

Fixes 010fd37fddf6 ("KVM: LAPIC: Reduce world switch latency caused by timer_advance_ns")
---
  arch/x86/kvm/lapic.c | 11 ++++++++++-
  1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +3 -1
index 45d40bfacb7c..cb8ebfaccfb6 100644
@@ -1642,7 +1642,16 @@ static void apic_timer_expired(struct kvm_lapic *apic, bool from_timer_fn)
  	}
 
  	if (kvm_use_posted_timer_interrupt(apic->vcpu)) {
-		kvm_wait_lapic_expire(vcpu);
+		/*
+		 * Ensure the guest's timer has truly expired before posting an
+		 * interrupt.  Open code the relevant checks to avoid querying

arch/x86/kvm/lapic.c static void apic_timer_expired(struct kvm_lapic *apic, bool from_timer_fn) · line 1657

Quoted 10 lines
arch/x86/kvm/lapic.c +7 -0
+		 * lapic_timer_int_injected(), which will be false since the
+		 * interrupt isn't yet injected.  Waiting until after injecting
+		 * is not an option since that won't help a posted interrupt.
+		 */
+		if (vcpu->arch.apic->lapic_timer.expired_tscdeadline &&
+		    vcpu->arch.apic->lapic_timer.timer_advance_ns)
+			__kvm_wait_lapic_expire(vcpu);
  		kvm_apic_inject_pending_timer_irqs(apic);
  		return;
  	}

Queued, thanks.

Paolo

#2 lore raw
In reply to Sean Christopherson
Quoted 24 lines · Sean Christopherson

When posting a deadline timer interrupt, open code the checks guarding __kvm_wait_lapic_expire() in order to skip the lapic_timer_int_injected() check in kvm_wait_lapic_expire(). The injection check will always fail since the interrupt has not yet be injected. Moving the call after injection would also be wrong as that wouldn't actually delay delivery of the IRQ if it is indeed sent via posted interrupt.

Fixes 010fd37fddf6 ("KVM: LAPIC: Reduce world switch latency caused by timer_advance_ns")
---
 arch/x86/kvm/lapic.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +0 -1
index 45d40bfacb7c..cb8ebfaccfb6 100644
@@ -1642,7 +1642,16 @@ static void apic_timer_expired(struct kvm_lapic *apic, bool from_timer_fn)
        }
 
        if (kvm_use_posted_timer_interrupt(apic->vcpu)) {
-               kvm_wait_lapic_expire(vcpu);

arch/x86/kvm/lapic.c static void apic_timer_expired(struct kvm_lapic *apic, bool from_timer_fn) · line 1654

Quoted 10 lines
arch/x86/kvm/lapic.c +10 -0
+               /*
+                * Ensure the guest's timer has truly expired before posting an
+                * interrupt.  Open code the relevant checks to avoid querying
+                * lapic_timer_int_injected(), which will be false since the
+                * interrupt isn't yet injected.  Waiting until after injecting
+                * is not an option since that won't help a posted interrupt.
+                */
+               if (vcpu->arch.apic->lapic_timer.expired_tscdeadline &&
+                   vcpu->arch.apic->lapic_timer.timer_advance_ns)
+                       __kvm_wait_lapic_expire(vcpu);

Thanks for the fixing.

Wanpeng