All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: Only reset TSC Deadline Timer in apic_timer_expired on KVM_RUN
@ 2026-07-15 23:42 Venkatesh Srinivas
  2026-07-16  1:14 ` James Houghton
  2026-07-20  8:37 ` Chao Gao
  0 siblings, 2 replies; 4+ messages in thread
From: Venkatesh Srinivas @ 2026-07-15 23:42 UTC (permalink / raw)
  To: kvm
  Cc: stable, Venkatesh Srinivas, David Matlack, Sean Christopherson,
	Jim Mattson, James Houghton

Fixes a lost timer interrupt issue if:
1) We are on an Intel platform w/ a VMX preemption timer && APICv
2) A VMM calls KVM_GET_LAPIC before KVM_GET_MSRS to save vCPU state
3) The thread executing the two calls migrates CPUs between the two calls

After migration across CPUs, KVM_GET_MSRS calls vcpu_load, posting the
interrupt and clearing the MSR:
vcpu_load() ->
  kvm_arch_vcpu_load() ->
    kvm_lapic_restart_hv_timer() ->
      start_hv_timer() ->
        apic_timer_expired() ->
          kvm_apic_inject_pending_timer_irqs()
            . post interrupt into the LAPIC state
            . clear IA32_TSCDEADLINE

The saved LAPIC state will be missing the pending interrupt and the saved
MSR will be zero. Oops.

Fix by only posting an interrupt when we're attempting to enter the guest
(vcpu->wants_to_run == true), not for vcpu_load from other paths.

Assisted-by: gemini:gemini-3.1-pro-preview
Debugged-by: David Matlack <dmatlack@google.com>
Debugged-by: Sean Christopherson <seanjc@google.com>
Debugged-by: Jim Mattson <jmattson@google.com>
Debugged-by: James Houghton <jthoughton@google.com>
Signed-off-by: Venkatesh Srinivas <venkateshs@chromium.org>
---
 arch/x86/kvm/lapic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 6f30bbdddb5a..74dfdbab4ab9 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -2052,7 +2052,7 @@ static void apic_timer_expired(struct kvm_lapic *apic, bool from_timer_fn)
 	if (apic_lvtt_tscdeadline(apic) || ktimer->hv_timer_in_use)
 		ktimer->expired_tscdeadline = ktimer->tscdeadline;
 
-	if (!from_timer_fn && apic->apicv_active) {
+	if (!from_timer_fn && apic->apicv_active && vcpu->wants_to_run) {
 		WARN_ON(kvm_get_running_vcpu() != vcpu);
 		kvm_apic_inject_pending_timer_irqs(apic);
 		return;
-- 
2.55.0.141.g00534a21ce-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-07-20 13:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-15 23:42 [PATCH] KVM: x86: Only reset TSC Deadline Timer in apic_timer_expired on KVM_RUN Venkatesh Srinivas
2026-07-16  1:14 ` James Houghton
2026-07-20 13:51   ` Paolo Bonzini
2026-07-20  8:37 ` Chao Gao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.