Skip to contentSkip to conversation

KVM: x86: Guard against division by zero in __wait_lapic_expire()

PATCH New Peng Hao 1 participant 1 message Oct 8

Subsystems (3)
lore
Share & tools
lore

Peng Hao

author
#0 lore raw

PATCH 1/1 KVM: x86: Guard against division by zero in __wait_lapic_expire()

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().

---
 arch/x86/kvm/lapic.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kvm/lapic.c +4 -0
index e1f3cea14765..13e32c4915e2 100644
@@ -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));
 	}
Signature · 1 line
2.43.7