From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796407435; b=TznIMbdwCukt+ookWs5zNHfHrVWhita2iQADhhORwRKMBAtV3w/YzA+9uthRsSV5zEko vBZXugB41bP50XdmFLKU9bg19sg3x0e+K1YoiZ1nxAE6HNVfJW9nKkqyHU53HtuCb5yQ8 YZosOqy4dRDj9i56/KiL6JAZtSYEr4X4mC+MsIPtWZy3qAetWbhGC6nlwfM38LZXtalFd tlRUoZ/g9BOjMUIGmPuRGy6fsO4THyYiyBBR3cwHSnNe6BPEv9und5T0Rxcvr7NYdwMfS iMlfsVyxfY9ZbU3aNA+0DeMLcUhhit7CVN7e1bkBDEnIf/5L0FWnOlNDlJ8iLjEOKzw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407435; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Scanned-By; bh=H9KsNInQ+3TsjqsLs9U+8rY+sVJpAXxppoV/YbRxHa4=; b=yfWYxSfWMGQHjhhxeG5pE0UouDTYLoTgeOPxbv0CRGUa+fpJHfowviL5AmMwm7W691OT 32GCe4KIuNXBoRdIVo3kIm+J+xbJCuODSoBWz7lUru/8BpdPuk+s+WQ2q/mbdoklUIMVN eZ3+glD03eXtRhzRk9S0cGak6/otk4fF+yXYHFrq+M4BoJZoYfHUeCvKurr1TofJkizcI h5l2yWmc/m4PM+PbHUqkbPI1VJXw2x6QMUaXSlHGd5s/lMT9Y3maMse8e77LwoIQnlK99 MKh0cY7p3ifGZWVe5M/AMVMH1enTSDwiLnDtJKCCfUeDilqPW+SFhvBr12H5LxwgS0w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=IAkTpo4l; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 554DF606AEBC for ; Fri, 4 Dec 2026 18:03:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 554DF606AEBC Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=IAkTpo4l DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407434; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=H9KsNInQ+3TsjqsLs9U+8rY+sVJpAXxppoV/YbRxHa4=; b=IAkTpo4lQAwMX7oQeIxboqMKO0EiCT9GOcJ8pTAgnVcHK8xwQQkR23MgVV/EvzrNL0mlEk drHLO2wT8ghfWOQKUbBKPPFy9fmDNWDkkjXHOY/MCIrcXLqgHCBthvaOtgRHqEuRGDfGdv fDwOdan9uG0X22+oGgCITszv4rMCk3U= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-My6A-ys-O0C0sm15aW_Mpg-1; Fri, 4 Dec 2026 18:03:48 +0000 X-MC-Unique: My6A-ys-O0C0sm15aW_Mpg-1 X-Mimecast-MFC-AGG-ID: My6A-ys-O0C0sm15aW_Mpg_1796407427 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3C11C193A3F0; Fri, 4 Dec 2026 18:03:47 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8A58418019CB; Fri, 4 Dec 2026 18:03:45 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , Stefan Teodorescu , David Woodhouse , stable@vger.kernel.org Subject: [PATCH 6.6.y 04/17] KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline wraps Date: Fri, 4 Dec 2026 19:03:24 +0100 Message-ID: <20261204180337.2168031-5-pbonzini@redhat.com> In-Reply-To: <20261204180337.2168031-1-pbonzini@redhat.com> References: <20261204180337.2168031-1-pbonzini@redhat.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 From: Sean Christopherson [ 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 Cc: David Woodhouse Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Link: https://patch.msgid.link/20261028183358.1798566-2-seanjc@google.com Signed-off-by: Paolo Bonzini Signed-off-by: Paolo Bonzini --- 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 346e6688109a..1fdb3f396d72 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1977,6 +1977,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; @@ -1995,19 +2015,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)); @@ -2044,10 +2059,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; } @@ -2055,7 +2068,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; /* @@ -2086,8 +2098,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