Commit 6b7d7e76 authored by Zachary Amsden's avatar Zachary Amsden Committed by Avi Kivity

KVM: x86: Harden against cpufreq

If cpufreq can't determine the CPU khz, or cpufreq is not compiled in,
we should fallback to the measured TSC khz.
Signed-off-by: default avatarZachary Amsden <zamsden@redhat.com>
Signed-off-by: default avatarMarcelo Tosatti <mtosatti@redhat.com>
parent 565d0998
...@@ -1348,8 +1348,12 @@ out: ...@@ -1348,8 +1348,12 @@ out:
void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
{ {
kvm_x86_ops->vcpu_load(vcpu, cpu); kvm_x86_ops->vcpu_load(vcpu, cpu);
if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
per_cpu(cpu_tsc_khz, cpu) = cpufreq_quick_get(cpu); unsigned long khz = cpufreq_quick_get(cpu);
if (!khz)
khz = tsc_khz;
per_cpu(cpu_tsc_khz, cpu) = khz;
}
kvm_request_guest_time_update(vcpu); kvm_request_guest_time_update(vcpu);
} }
...@@ -3144,8 +3148,12 @@ static void kvm_timer_init(void) ...@@ -3144,8 +3148,12 @@ static void kvm_timer_init(void)
if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
CPUFREQ_TRANSITION_NOTIFIER); CPUFREQ_TRANSITION_NOTIFIER);
for_each_online_cpu(cpu) for_each_online_cpu(cpu) {
per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu); unsigned long khz = cpufreq_get(cpu);
if (!khz)
khz = tsc_khz;
per_cpu(cpu_tsc_khz, cpu) = khz;
}
} else { } else {
for_each_possible_cpu(cpu) for_each_possible_cpu(cpu)
per_cpu(cpu_tsc_khz, cpu) = tsc_khz; per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment