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.1.y 08/24] KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline wraps
Date: Fri, 4 Dec 2026 19:04:07 +0100 [thread overview]
Message-ID: <20261204180423.2168641-9-pbonzini@redhat.com> (raw)
In-Reply-To: <20261204180423.2168641-1-pbonzini@redhat.com>
From: Sean Christopherson <seanjc@google.com>
[ Upstream commit 0b135caf3230bc4030c32bb7da5b8d4af64c379c ]
Disable the VMX Preemption Timer, by configuring the deadline to be '0',
if converting from a one-shot/periodic expiry to a deadline would wrap the
TSC, i.e. would result in a deadline that's far in the past, even though
the expiry is firmly in the future. Architecturally, delivering the APIC
timer IRQ way too early is obviously wrong, but the bigger problem is what
can happen if the APIC timer is in periodic mode.
If the guest sets its TSC to a large value, but not _too_ large, puts it
APIC timer into periodic mode, and then programs the maximal period, then
KVM will compute a TSC deadline that is far "earlier" than the current TSC,
but with plenty of headroom for guest TSC to continue counting without the
TSC itself wrapping. Even though the VMX side of things detects that the
timer has already expired, KVM leaves the hypervisor timer armed so that it
can compute the new deadline/expiry from the VM-Exit handler. On the VMX
side of things, KVM handles the "deadline in the past" by forcing the VMX
Preemption Timer to '0', which generates an immediate VM-Exit.
All of that would be "fine", except that KVM handles VMX Preemption Timer
exits in its fastpath, will recompute the same in-the-past deadline, and
constantly re-enter the guest with a timer value of '0'. Architecturally,
the SDM states that such behavior can trigger a VM-Exit *without* fetching
any instructions in the guest.
It is possible for the VMX-preemption timer to expire during VM entry
(e.g., if the value in the VMX-preemption timer-value field is zero). If
this happens (and if the VM entry was not to the wait-for-SIPI state), a
VM exit occurs with its normal priority after any event injection and
before execution of any instruction following VM entry.
Empirically, on bare metal, if the timer expires "during" VM-Entry, the CPU
may or may not recognize IRQs (it's not clear exactly what conditions lead
to IRQs being recognized or not). In the worst case scenario, the host can
experience hard lockups if the tight entry<=>exit loop never opens a window
for host IRQs to be recognized.
The problem is especially noticeable when running as a KVM guest, as KVM
*never* opens a window for host (L1) IRQs, even with non-zero (but small)
VMX Preemption Timer values, as KVM-as-L0 starts the timer fairly early in
the nested VM-Enter flow and so the timer always expires before the nested
VM-Enter gets to the point where it checks for L1 IRQs.
Opportunistically read TSC and ktime in close succession when computing the
target expiration and the TSC deadline, to minimize the divergence between
the two (though in practice, reading the values even a few hundred cycles
apart is a complete non-issue.
Fixes: 26efe2fd92e5 ("KVM: VMX: Handle preemption timer fastpath")
Fixes: 8003c9ae204e ("KVM: LAPIC: add APIC Timer periodic/oneshot mode VMX preemption timer support")
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-2-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
arch/x86/kvm/lapic.c | 37 ++++++++++++++++++++++++-------------
1 file changed, 24 insertions(+), 13 deletions(-)
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index ad98e4f725e6..5bd19eb86dfb 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1810,6 +1810,26 @@ static inline u64 tmict_to_ns(struct kvm_lapic *apic, u32 tmict)
return (u64)tmict * APIC_BUS_CYCLE_NS * (u64)apic->divide_count;
}
+static u64 ns_to_tsc_deadline(struct kvm_lapic *apic, s64 delta)
+{
+ u64 tsc_now = rdtsc();
+ u64 deadline;
+
+ /*
+ * Disable the hypervisor timer, which is deadline-based, if converting
+ * the one-shot/periodic expiry to a TSC deadline wraps the TSC, i.e.
+ * results in a deadline far in the past, even though the expiry itself
+ * is in the future. Alternatively, KVM could track the deadline as a
+ * 128 bit value, but in practice the deadline will only wrap if the
+ * guest is doing weird things with TSC (which is writable via MSR).
+ */
+ if (check_add_overflow(kvm_read_l1_tsc(apic->vcpu, tsc_now),
+ nsec_to_cycles(apic->vcpu, delta), &deadline))
+ return 0;
+
+ return deadline;
+}
+
static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
{
ktime_t now, remaining;
@@ -1828,19 +1848,14 @@ static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_diviso
ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
apic->divide_count, old_divisor);
- apic->lapic_timer.tscdeadline +=
- nsec_to_cycles(apic->vcpu, ns_remaining_new) -
- nsec_to_cycles(apic->vcpu, ns_remaining_old);
apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
+ apic->lapic_timer.tscdeadline = ns_to_tsc_deadline(apic, ns_remaining_new);
}
static bool set_target_expiration(struct kvm_lapic *apic, u32 count_reg)
{
- ktime_t now;
- u64 tscl = rdtsc();
s64 deadline;
- now = ktime_get();
apic->lapic_timer.period =
tmict_to_ns(apic, kvm_lapic_get_reg(apic, APIC_TMICT));
@@ -1877,10 +1892,8 @@ static bool set_target_expiration(struct kvm_lapic *apic, u32 count_reg)
}
}
- apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
- nsec_to_cycles(apic->vcpu, deadline);
- apic->lapic_timer.target_expiration = ktime_add_ns(now, deadline);
-
+ apic->lapic_timer.target_expiration = ktime_add_ns(ktime_get(), deadline);
+ apic->lapic_timer.tscdeadline = ns_to_tsc_deadline(apic, deadline);
return true;
}
@@ -1888,7 +1901,6 @@ static void advance_periodic_target_expiration(struct kvm_lapic *apic)
{
struct kvm_timer *ktimer = &apic->lapic_timer;
ktime_t now = ktime_get();
- u64 tscl = rdtsc();
ktime_t delta;
/*
@@ -1919,8 +1931,7 @@ static void advance_periodic_target_expiration(struct kvm_lapic *apic)
* excessively large due to it an unsigned value.
*/
delta = ktime_sub(ktimer->target_expiration, now);
- ktimer->tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
- nsec_to_cycles(apic->vcpu, delta);
+ ktimer->tscdeadline = ns_to_tsc_deadline(apic, ktime_to_ns(delta));
}
static void start_sw_period(struct kvm_lapic *apic)
--
2.56.1
next prev parent reply other threads:[~2026-12-04 18:04 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 01/24] ptrace: Convert ptrace_attach() to use lock guards Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 02/24] KVM: x86: Grab lapic_timer in a local variable to cleanup periodic code Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 03/24] KVM: x86/mmu: Move round_gfn_for_level() helper into mmu_internal.h Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 04/24] KVM: x86/mmu: Fix wrong gfn range of tlb flushing in kvm_set_pte_rmapp() Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 05/24] KVM: x86/mmu: Fix wrong start gfn of tlb flushing with range Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 06/24] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 07/24] KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out of __rmap_add() Paolo Bonzini
2026-12-04 18:04 ` Paolo Bonzini [this message]
2026-12-04 18:04 ` [PATCH 6.1.y 09/24] KVM: x86: Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 10/24] KVM: x86/mmu: Flush all TLB entries, not a precise range, when yielding Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 11/24] KVM: VMX: Retry APIC-access page reload if invalidation is in-progress Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 12/24] KVM: x86: Use standard mmu_notifier invalidate hooks for APIC access page Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 13/24] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 14/24] KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event channel Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 15/24] KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release() Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 16/24] KVM: WARN if there are dangling MMU invalidations at VM destruction Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 17/24] KVM: WARN if there are in-flight memslot updates when a VM's process exits Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 18/24] KVM: Fix comments that refer to the non-existent install_new_memslots() Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 19/24] KVM: Set mn_active_invalidate_count when the VM's mm is released Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 20/24] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 21/24] x86/fpu: Bail early from guest XFD update if XFD isn't supported Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 22/24] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 23/24] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 24/24] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ Paolo Bonzini
2026-12-04 20:29 ` [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities 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=20261204180423.2168641-9-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®