mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Sean Christopherson <seanjc@google.com>,
	Stefan Teodorescu <fane@google.com>,
	David Woodhouse <dwmw2@infradead.org>,
	stable@vger.kernel.org
Subject: [PATCH 6.12.y 05/17] KVM: x86: Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC
Date: Fri,  4 Dec 2026 19:02:33 +0100	[thread overview]
Message-ID: <20261204180245.2166967-6-pbonzini@redhat.com> (raw)
In-Reply-To: <20261204180245.2166967-1-pbonzini@redhat.com>

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit c876175f4bb87e3e19c6ecdf9b67beb895fe8bda ]

Reset all of the local APIC timer's metadata that's tied to the current
state of the timer on INIT (and RESET) and KVM_SET_LAPIC, *except* for the
TSC deadline when doing KVM_SET_LAPIC, as kvm_timer.tscdeadline subtly
holds/tracks the value of MSR_IA32_TSC_DEADLINE and isn't computed by
__start_apic_timer().  In particular, leaving the target expiration as-is
allows a misbehaving guest to induce a divide error by overflowing a u64.

After INIT (or RESET), the divide count register is '0', which translates
to an actual divisor of '2'.  The other divisors are 1, 4, 8, 16, 32, 64,
and 128, so the guest can scale the target_expiration from 2x-64x after
every INIT (or cut it in half, but that's uninteresting).  After enough
multiplications, things go kablooie.  E.g. setting the target_expiration
to -1u (TMICT is a 32-bit value) and then scaling by 64x via a divisor
of 128 generates a #DE after 6 rounds of INIT:

  Oops: divide error: 0000 [#1] SMP
  CPU: 0 UID: 1000 PID: 604 Comm: div_error Not tainted 7.3.0-rc7-e86da2bfcc430-sink-all #7307 PREEMPTLAZY
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
  RIP: 0010:kvm_lapic_reg_write+0x364/0x7d0 [kvm]
   <TASK>
   kvm_set_msr_common+0x649/0x1540 [kvm]
   vmx_set_msr+0xdb5/0x13a0 [kvm_intel]
   __kvm_set_msr+0xc4/0x2a0 [kvm]
   kvm_set_msr_ignored_check+0x1d/0x140 [kvm]
   __kvm_emulate_wrmsr+0x36/0x170 [kvm]
   vt_handle_exit+0x1e/0x50 [kvm_intel]
   kvm_arch_vcpu_ioctl_run+0x972/0x21c0 [kvm]
   kvm_vcpu_ioctl+0x2c7/0x960 [kvm]
   __x64_sys_ioctl+0x90/0xd0
   do_syscall_64+0x9c/0x4d0
   entry_SYSCALL_64_after_hwframe+0x4b/0x53
  RIP: 0033:0x421d1b
   </TASK>
  Modules linked in: kvm_intel kvm irqbypass

Opportunistically purge the target_expiration and expired_tscdeadline when
the timer mode changes to/from deadline mode, even though that path should
be fine (it doesn't interact with the divide count).

Fixes: c301b909e4b9 ("KVM: LAPIC: Apply change to TDCR right away to the timer")
Reported-by: Stefan Teodorescu <fane@google.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261028183358.1798566-3-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/lapic.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 01f887c7ea45..080758a31494 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1778,6 +1778,23 @@ static void cancel_apic_timer(struct kvm_lapic *apic)
 	atomic_set(&apic->lapic_timer.pending, 0);
 }
 
+static void __kvm_lapic_reset_timer(struct kvm_lapic *apic)
+{
+	struct kvm_timer *ktimer = &apic->lapic_timer;
+
+	cancel_apic_timer(apic);
+
+	ktimer->period = 0;
+	ktimer->target_expiration = 0;
+	ktimer->expired_tscdeadline = 0;
+}
+
+static void kvm_lapic_reset_timer(struct kvm_lapic *apic)
+{
+	__kvm_lapic_reset_timer(apic);
+	apic->lapic_timer.tscdeadline = 0;
+}
+
 static void apic_update_lvtt(struct kvm_lapic *apic)
 {
 	u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
@@ -1786,10 +1803,8 @@ static void apic_update_lvtt(struct kvm_lapic *apic)
 	if (apic->lapic_timer.timer_mode != timer_mode) {
 		if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
 				APIC_LVT_TIMER_TSCDEADLINE)) {
-			cancel_apic_timer(apic);
+			kvm_lapic_reset_timer(apic);
 			kvm_lapic_set_reg(apic, APIC_TMICT, 0);
-			apic->lapic_timer.period = 0;
-			apic->lapic_timer.tscdeadline = 0;
 		}
 		apic->lapic_timer.timer_mode = timer_mode;
 		limit_periodic_timer_frequency(apic);
@@ -2839,8 +2854,7 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
 	if (!apic)
 		return;
 
-	/* Stop the timer in case it's a reset to an active apic */
-	hrtimer_cancel(&apic->lapic_timer.timer);
+	kvm_lapic_reset_timer(apic);
 
 	/* The xAPIC ID is set at RESET even if the APIC was already enabled. */
 	if (!init_event)
@@ -2877,7 +2891,6 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
 	}
 	kvm_apic_update_apicv(vcpu);
 	update_divide_count(apic);
-	atomic_set(&apic->lapic_timer.pending, 0);
 
 	vcpu->arch.pv_eoi.msr_val = 0;
 	apic_update_ppr(apic);
@@ -3182,6 +3195,9 @@ int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
 		kvm_recalculate_apic_map(vcpu->kvm);
 		return r;
 	}
+
+	__kvm_lapic_reset_timer(apic);
+
 	memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s));
 
 	atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
@@ -3189,8 +3205,6 @@ int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
 	kvm_apic_set_version(vcpu);
 
 	apic_update_ppr(apic);
-	cancel_apic_timer(apic);
-	apic->lapic_timer.expired_tscdeadline = 0;
 	apic_update_lvtt(apic);
 	apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
 	update_divide_count(apic);
-- 
2.56.1



  parent reply	other threads:[~2026-12-04 18:03 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:02 [PATCH 6.12.y 00/17] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 01/17] KVM: x86: Grab lapic_timer in a local variable to cleanup periodic code Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 02/17] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 03/17] KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out of __rmap_add() Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 04/17] KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline wraps Paolo Bonzini
2026-12-04 18:02 ` Paolo Bonzini [this message]
2026-12-04 18:02 ` [PATCH 6.12.y 06/17] LoongArch: KVM: Flush TLBs *before* dropping mmu_lock Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 07/17] KVM: x86/mmu: Flush all TLB entries, not a precise range, when yielding Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 08/17] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 09/17] KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event channel Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 10/17] KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release() Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 11/17] KVM: WARN if there are in-flight memslot updates when a VM's process exits Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 12/17] KVM: Set mn_active_invalidate_count when the VM's mm is released Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 13/17] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 14/17] x86/fpu: Bail early from guest XFD update if XFD isn't supported Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 15/17] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 16/17] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 17/17] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204180245.2166967-6-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=dwmw2@infradead.org \
    --cc=fane@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=seanjc@google.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®