Skip to contentSkip to conversation

KVM: x86: Reject KVM_CREATE_VCPU when the host TSC frequency is unknown

PATCH Applied Peng Hao 2 participants 2 messages Oct 16 – Nov 13

Subsystems (3)
lore
Share & tools
lore

Peng Hao

author
#0 lore raw

PATCH 1/1 KVM: x86: Reject KVM_CREATE_VCPU when the host TSC frequency is unknown

If host TSC calibration fails, tsc_khz stays zero: tsc_init() marks the TSC unstable, clears X86_FEATURE_TSC_DEADLINE_TIMER and gives up. KVM has been papering over that state since 03ba32cae66e ("VMX: x86: handle host TSC calibration failure"), but the guest cannot actually run:

- kvm_set_tsc_khz() computes thresh_lo == thresh_hi == 0 via adjust_tsc_khz(), so use_scaling is always true and set_tsc_khz() divides by tsc_khz to compute the scaling ratio, i.e. #DE in host context. This one is reachable from KVM_SET_TSC_KHZ alone, no guest cooperation required.
- kvm_guest_time_update() re-requests KVM_REQ_CLOCK_UPDATE and returns 1 because get_cpu_tsc_khz() hands back the (zero) global tsc_khz for CPUs with a constant TSC. vcpu_run() only breaks out of its loop on "r <= 0", so the vCPU thread spins in the host kernel rather than even failing the guest.
- Everything that converts guest cycles to nanoseconds, e.g. the lapic timer advance logic, divides by virtual_tsc_khz, which is zero as well.

Each of those has been growing its own "is the frequency zero?" check, and the list keeps getting longer. Cut it off at the source instead: fail KVM_CREATE_VCPU rather than letting userspace walk into a host #DE or a spinning vCPU thread.

---
 arch/x86/kvm/x86.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/x86/kvm/x86.c +11 -0
index 4b3681796c75..8dd75d60eecf 100644
@@ -9342,6 +9342,17 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	struct page *page;
 	int r;
 
+	/*
+	 * The host TSC frequency is unknown, e.g. because TSC calibration failed
+	 * at boot.  KVM cannot virtualize the TSC in this state, and every
+	 * consumer of the frequency ends up needing its own zero check; fail vCPU
+	 * creation instead of handing userspace a vCPU that cannot run.
+	 */
+	if (!tsc_khz) {
+		pr_err_ratelimited("cannot create vCPU, host TSC frequency is unknown\n");
+		return -EIO;
+	}
+
 	vcpu->arch.last_vmentry_cpu = -1;
 	bitmap_fill(vcpu->arch.regs_avail, NR_VCPU_TOTAL_REGS);
 	bitmap_fill(vcpu->arch.regs_dirty, NR_VCPU_TOTAL_REGS);
Signature · 1 line
2.43.7
maintainer Applied
#1 lore raw
Quoted 18 lines · Peng Hao

If host TSC calibration fails, tsc_khz stays zero: tsc_init() marks the TSC unstable, clears X86_FEATURE_TSC_DEADLINE_TIMER and gives up. KVM has been papering over that state since 03ba32cae66e ("VMX: x86: handle host TSC calibration failure"), but the guest cannot actually run:

- kvm_set_tsc_khz() computes thresh_lo == thresh_hi == 0 via adjust_tsc_khz(), so use_scaling is always true and set_tsc_khz() divides by tsc_khz to compute the scaling ratio, i.e. #DE in host context. This one is reachable from KVM_SET_TSC_KHZ alone, no guest cooperation required.
- kvm_guest_time_update() re-requests KVM_REQ_CLOCK_UPDATE and returns 1 because get_cpu_tsc_khz() hands back the (zero) global tsc_khz for CPUs with a constant TSC. vcpu_run() only breaks out of its loop on "r <= 0", so the vCPU thread spins in the host kernel rather than even failing the guest.
- Everything that converts guest cycles to nanoseconds, e.g. the lapic timer advance logic, divides by virtual_tsc_khz, which is zero as well.

Quoted 1 line

[...]

Applied to kvm-x86 misc, thanks!

[1/1] KVM: x86: Reject KVM_CREATE_VCPU when the host TSC frequency is unknown

      https://github.com/kvm-x86/linux/commit/687fe634b54e

--
https://github.com/kvm-x86/linux/tree/next