If CPUID.0x16 is present and valid, use the CPU frequency provided by CPUID instead of assuming that the virtual CPU runs at the same frequency as TSC and/or kvmclock. Back before constant TSCs were a thing, treating the TSC and CPU frequencies as one and the same was somewhat reasonable, but now it's nonsensical, especially if the hypervisor explicitly enumerates the CPU frequency. Signed-off-by: Sean Christopherson --- arch/x86/kernel/kvm.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index c1139182121d..c81a24d0efdf 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -50,6 +50,7 @@ #include static unsigned int kvm_tsc_khz_cpuid __initdata; +static unsigned int kvm_cpu_khz_cpuid __initdata; DEFINE_STATIC_KEY_FALSE_RO(kvm_async_pf_enabled); @@ -928,6 +929,11 @@ static unsigned int __init kvm_get_tsc_khz(void) return kvm_tsc_khz_cpuid; } +static unsigned int __init kvm_get_cpu_khz(void) +{ + return kvm_cpu_khz_cpuid; +} + unsigned int kvm_arch_para_features(void) { return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES); @@ -1049,6 +1055,14 @@ static void __init kvm_init_platform(void) #endif } + /* + * Prefer CPUID.0x16 over KVM's PV CPUID when possible, as the base CPU + * frequency isn't necessarily the same as the TSC frequency. + */ + kvm_cpu_khz_cpuid = __cpu_khz_from_cpuid(); + if (kvm_cpu_khz_cpuid) + x86_init.hyper.get_cpu_khz = kvm_get_cpu_khz; + /* * If the TSC counts at a constant frequency across P/T states, counts * in deep C-states, and the TSC hasn't been marked unstable, treat the -- 2.54.0.823.g6e5bcc1fc9-goog