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=1796407436; b=hA7/hgbj6Nx0KtAUBMLI/5zGdqbZBNPuNiKNmUjVp76DEuFoMMxdIB5Yms7eLoh4HmQ1 z1EgX+9vpoezLpaZP/nl1KRYOKi2BpjoqBoEt5DL+nQYDxI9rIDdep3/Nyc/aVmci9/TP M4yzbtliRt40NSh2uYDAj8q7DfVFfAv/vrNWl/x6hhXmdRWsWWdMyD9Dpx2YSboKMJsxV 5aWfJyuCnSUnQemaa4Ktvc3Ry9Zf8/I1MEhuLIaUPqoo9Ux208mZJGfk6nFV3k9qMmMWt bi8xDBm2vrn9eZkKOzRQXp4c7stKxLG1LFQWuLkJh0Ck4/b0czoBJWCPbqanJ1r5mVw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407436; 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=163f8a1w5V2K7q5dqH3kKbwnkFofJ6htPCT64sd02Z0=; b=mFAcWTUzbdY6t3EfcYMaf/ygDsVA0G7q15AxVTZ9/zU5v5ht7OJSKY+TWeJYwvmdrQCN Im7pfrK8wGpURR+O5X52Xh5GyLtZWi1o/xpmEDGEVBIMSs/ShlM6ZO/Kd6wteIH3YRnlD syI8RAITc8CihrgXpzivhBxNX2vKQ93HJi9zx73kb7uuEN4Y2zAz6oUiEOnuqNpvGderT Dm3ACBHpZtzZewTWKfJ6CTZHOR3Wq0Jt0G0N/8BpRsivHAmTo02Twc+OXoKo5QaY3WuXe QRDFTI1fN29QNJv9t8AZQ6phBEOdZ8mJjxsqnvzq7xWSOhdEcJsAP3vosAUYfA2Bcbg== 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="K1/Luvej"; 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 C510260A3836 for ; Fri, 4 Dec 2026 18:03:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C510260A3836 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=K1/Luvej DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407435; 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=163f8a1w5V2K7q5dqH3kKbwnkFofJ6htPCT64sd02Z0=; b=K1/LuvejGOBGt8DUZFdZ3tPtyn6aalrnrjnw9QkjKrEVedhXgK+ra5qXsRMSB9BrvRTP+A ZKOqGEZIOmtIubNDcnLgUakInbjZFGs4NsfbRwa3VBW1wrPRrMTxC7yXiC7cDXq6ifsBn0 W+TxemGj/0EnSNz9rPMtY0h1dNArg8o= Received: from mx-prod-mc-01.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-388-sz45Pkd4MbSyUIE-5XamJw-1; Fri, 4 Dec 2026 18:03:50 +0000 X-MC-Unique: sz45Pkd4MbSyUIE-5XamJw-1 X-Mimecast-MFC-AGG-ID: sz45Pkd4MbSyUIE-5XamJw_1796407429 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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5202818F52C8; Fri, 4 Dec 2026 18:03:49 +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 9C20E18019CD; Fri, 4 Dec 2026 18:03:47 +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 05/17] KVM: x86: Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC Date: Fri, 4 Dec 2026 19:03:25 +0100 Message-ID: <20261204180337.2168031-6-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 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] 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 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 Cc: David Woodhouse Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Link: https://patch.msgid.link/20261028183358.1798566-3-seanjc@google.com Signed-off-by: Paolo Bonzini Signed-off-by: Paolo Bonzini --- 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 1fdb3f396d72..61d70cf48bf0 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1763,6 +1763,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) & @@ -1771,10 +1788,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); @@ -2769,8 +2784,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) @@ -2807,7 +2821,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); @@ -3096,6 +3109,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); @@ -3103,8 +3119,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