Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: Guard against division by zero in __wait_lapic_expire()
@ 2026-10-08  9:00 Peng Hao
  0 siblings, 0 replies; only message in thread
From: Peng Hao @ 2026-10-08  9:00 UTC (permalink / raw)
  To: pbonzini, seanjc; +Cc: kvm

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-10-08  9:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-08  9:00 [PATCH] KVM: x86: Guard against division by zero in __wait_lapic_expire() Peng Hao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox