]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: x86: Don't snapshot "max" TSC if host TSC is constant
authorSean Christopherson <seanjc@google.com>
Fri, 25 Feb 2022 14:53:03 +0000 (14:53 +0000)
committerPaolo Bonzini <pbonzini@redhat.com>
Sat, 2 Apr 2022 09:41:20 +0000 (05:41 -0400)
Don't snapshot tsc_khz into max_tsc_khz during KVM initialization if the
host TSC is constant, in which case the actual TSC frequency will never
change and thus capturing the "max" TSC during initialization is
unnecessary, KVM can simply use tsc_khz during VM creation.

On CPUs with constant TSC, but not a hardware-specified TSC frequency,
snapshotting max_tsc_khz and using that to set a VM's default TSC
frequency can lead to KVM thinking it needs to manually scale the guest's
TSC if refining the TSC completes after KVM snapshots tsc_khz.  The
actual frequency never changes, only the kernel's calculation of what
that frequency is changes.  On systems without hardware TSC scaling, this
either puts KVM into "always catchup" mode (extremely inefficient), or
prevents creating VMs altogether.

Ideally, KVM would not be able to race with TSC refinement, or would have
a hook into tsc_refine_calibration_work() to get an alert when refinement
is complete.  Avoiding the race altogether isn't practical as refinement
takes a relative eternity; it's deliberately put on a work queue outside
of the normal boot sequence to avoid unnecessarily delaying boot.

Adding a hook is doable, but somewhat gross due to KVM's ability to be
built as a module.  And if the TSC is constant, which is likely the case
for every VMX/SVM-capable CPU produced in the last decade, the race can
be hit if and only if userspace is able to create a VM before TSC
refinement completes; refinement is slow, but not that slow.

For now, punt on a proper fix, as not taking a snapshot can help some
uses cases and not taking a snapshot is arguably correct irrespective of
the race with refinement.

[ dwmw2: Rebase on top of KVM-wide default_tsc_khz to ensure that all
         vCPUs get the same frequency even if we hit the race. ]

Cc: Suleiman Souhlal <suleiman@google.com>
Cc: Anton Romanov <romanton@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Message-Id: <20220225145304.36166-3-dwmw2@infradead.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/x86.c

index c069b97a8523dea99076bb53f56960dba9dc9a3f..dad13fd6b61e39f4d9b334edc567920027854931 100644 (file)
@@ -8814,22 +8814,22 @@ static int kvmclock_cpu_online(unsigned int cpu)
 
 static void kvm_timer_init(void)
 {
-       max_tsc_khz = tsc_khz;
-
        if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-#ifdef CONFIG_CPU_FREQ
-               struct cpufreq_policy *policy;
-               int cpu;
-
-               cpu = get_cpu();
-               policy = cpufreq_cpu_get(cpu);
-               if (policy) {
-                       if (policy->cpuinfo.max_freq)
-                               max_tsc_khz = policy->cpuinfo.max_freq;
-                       cpufreq_cpu_put(policy);
+               max_tsc_khz = tsc_khz;
+
+               if (IS_ENABLED(CONFIG_CPU_FREQ)) {
+                       struct cpufreq_policy *policy;
+                       int cpu;
+
+                       cpu = get_cpu();
+                       policy = cpufreq_cpu_get(cpu);
+                       if (policy) {
+                               if (policy->cpuinfo.max_freq)
+                                       max_tsc_khz = policy->cpuinfo.max_freq;
+                               cpufreq_cpu_put(policy);
+                       }
+                       put_cpu();
                }
-               put_cpu();
-#endif
                cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
                                          CPUFREQ_TRANSITION_NOTIFIER);
        }
@@ -11737,7 +11737,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        pvclock_update_vm_gtod_copy(kvm);
        raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
 
-       kvm->arch.default_tsc_khz = max_tsc_khz;
+       kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz;
        kvm->arch.guest_can_read_msr_platform_info = true;
        kvm->arch.enable_pmu = enable_pmu;