Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Peng Hao <flyingpenghao@gmail.com>
To: pbonzini@redhat.com, seanjc@google.com
Cc: kvm@vger.kernel.org
Subject: [PATCH] KVM: x86: Guard against division by zero in __wait_lapic_expire()
Date: Thu,  8 Oct 2026 17:00:11 +0800	[thread overview]
Message-ID: <20261008090012.86252-1-flyingpeng@tencent.com> (raw)

virtual_tsc_khz is zero whenever TSC calibration fails and userspace
never programs a khz via KVM_SET_TSC_KHZ.  In that state
kvm_set_tsc_khz() forces vcpu->arch.tsc_scaling_ratio back to
kvm_caps.default_tsc_scaling_ratio, but a nested guest can still end
up with a non-default tsc_scaling_ratio via the L2 TSC multiplier
path independently of virtual_tsc_khz, which sends
__wait_lapic_expire() into the tsc-scaling branch and
do_div(delay_ns, vcpu->arch.virtual_tsc_khz) with a zero divisor,
crashing the host with #DE.

Bail out before the division, mirroring the existing
"!this_tsc_khz" guard in start_sw_tscdeadline().

Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
 arch/x86/kvm/lapic.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index e1f3cea14765..13e32c4915e2 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1970,6 +1970,10 @@ static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles)
 			nsec_to_cycles(vcpu, timer_advance_ns)));
 	} else {
 		u64 delay_ns = guest_cycles * 1000000ULL;
+
+		if (unlikely(!vcpu->arch.virtual_tsc_khz))
+			return;
+
 		do_div(delay_ns, vcpu->arch.virtual_tsc_khz);
 		ndelay(min_t(u32, delay_ns, timer_advance_ns));
 	}
-- 
2.43.7


                 reply	other threads:[~2026-10-08  9:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261008090012.86252-1-flyingpeng@tencent.com \
    --to=flyingpenghao@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    /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