Skip to contentSkip to conversation

Hotplug / TSC cleanup and fixing

PATCH Superseded Zachary Amsden 4 participants 30 messages Sep 29, 2009 – Oct 12, 2009

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Greatly simplified version of the patches; allocate and dealloc all required structs at module load and unload time.

Clarification: per_cpu(var, cpu) does indeed work for not-present CPUs;
the allocations for module specific per-cpu variables are done at module load and unload time, while the per_cpu chunks are setup for all possible CPUs at boot time.

#1 lore raw

PATCH v2 1/4 Code motion. Separate timer intialization into an indepedent function.

---
 arch/x86/kvm/x86.c |   23 +++++++++++++++--------
 1 files changed, 15 insertions(+), 8 deletions(-)
arch/x86/kvm/x86.c +15 -8
index fedac9d..15d2ace 100644
@@ -3116,9 +3116,22 @@ static struct notifier_block kvmclock_cpufreq_notifier_block = {
         .notifier_call  = kvmclock_cpufreq_notifier
 };
 
+static void kvm_timer_init(void)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu)
+		per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
+	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
+		tsc_khz_ref = tsc_khz;
+		cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
+					  CPUFREQ_TRANSITION_NOTIFIER);
+	}
+}
+
 int kvm_arch_init(void *opaque)
 {
-	int r, cpu;
+	int r;
 	struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
 
 	if (kvm_x86_ops) {
@@ -3150,13 +3163,7 @@ int kvm_arch_init(void *opaque)
 	kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
 			PT_DIRTY_MASK, PT64_NX_MASK, 0);
 
-	for_each_possible_cpu(cpu)
-		per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
-	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-		tsc_khz_ref = tsc_khz;
-		cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
-					  CPUFREQ_TRANSITION_NOTIFIER);
-	}
+	kvm_timer_init();
 
 	return 0;
 
Signature · 1 line
1.6.4.4
#2 lore raw

PATCH v2 2/4 Kill the confusing tsc_ref_khz and ref_freq variables.

They are globals, not clearly protected by any ordering or locking, and vulnerable to various startup races.

Instead, for variable TSC machines, register the cpufreq notifier and get the TSC frequency directly from the cpufreq machinery. Not only is it always right, it is also perfectly accurate, as no error prone measurement is required.

On such machines, when a new CPU online is brought online, it isn't clear what frequency it will start with, and it may not correspond to the reference, thus in hardware_enable we clear the cpu_tsc_khz variable to zero and make sure it is set before running on a VCPU.

---
 arch/x86/kvm/x86.c |   30 ++++++++++++++++++++----------
 1 files changed, 20 insertions(+), 10 deletions(-)
arch/x86/kvm/x86.c +20 -10
index 15d2ace..9cbd53a 100644
@@ -1326,6 +1326,8 @@ out:
 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
 	kvm_x86_ops->vcpu_load(vcpu, cpu);
+	if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0))
+		per_cpu(cpu_tsc_khz, cpu) = cpufreq_quick_get(cpu);
 	kvm_request_guest_time_update(vcpu);
 }
 
@@ -3061,9 +3063,6 @@ static void bounce_off(void *info)
 	/* nothing */
 }
 
-static unsigned int  ref_freq;
-static unsigned long tsc_khz_ref;
-
 static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
 				     void *data)
 {
@@ -3072,14 +3071,11 @@ static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long va
 	struct kvm_vcpu *vcpu;
 	int i, send_ipi = 0;
 
-	if (!ref_freq)
-		ref_freq = freq->old;
-
 	if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
 		return 0;
 	if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
 		return 0;
-	per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
+	per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
 
 	spin_lock(&kvm_lock);
 	list_for_each_entry(kvm, &vm_list, vm_list) {
@@ -3120,12 +3116,18 @@ static void kvm_timer_init(void)
 {
 	int cpu;
 
-	for_each_possible_cpu(cpu)
-		per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
 	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-		tsc_khz_ref = tsc_khz;
 		cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
 					  CPUFREQ_TRANSITION_NOTIFIER);
+		for_each_online_cpu(cpu)
+			per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu);
+	} else {
+		for_each_possible_cpu(cpu)
+			per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
+	}
+	for_each_possible_cpu(cpu) {
+		printk(KERN_DEBUG "kvm: cpu %d = %ld khz\n",
+			cpu, per_cpu(cpu_tsc_khz, cpu));
 	}
 }
 
@@ -4698,6 +4700,14 @@ int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
 
 int kvm_arch_hardware_enable(void *garbage)
 {
+	/*
+	 * Since this may be called from a hotplug notifcation,
+	 * we can't get the CPU frequency directly.
+	 */
+	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
+		int cpu = raw_smp_processor_id();
+		per_cpu(cpu_tsc_khz, cpu) = 0;
+	}
 	return kvm_x86_ops->hardware_enable(garbage);
 }
 
Signature · 1 line
1.6.4.4
#3 lore raw

PATCH v2 3/4 Fix printk name error in svm.c

---
 arch/x86/kvm/svm.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/svm.c +3 -2
index 9a4daca..d1036ce 100644
@@ -330,13 +330,14 @@ static int svm_hardware_enable(void *garbage)
 		return -EBUSY;
 
 	if (!has_svm()) {
-		printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
+		printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
+		       me);
 		return -EINVAL;
 	}
 	svm_data = per_cpu(svm_data, me);
 
 	if (!svm_data) {
-		printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
+		printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
 		       me);
 		return -EINVAL;
 	}
Signature · 1 line
1.6.4.4
#4 lore raw

PATCH v2 4/4 Fix hotplug of CPUs for KVM.

Both VMX and SVM require per-cpu memory allocation, which is done at module init time, for only online cpus.

Backend was not allocating enough structure for all possible CPUs, so new CPUs coming online could not be hardware enabled.

---
 arch/x86/kvm/svm.c  |    4 ++--
 arch/x86/kvm/vmx.c  |    6 ++++--
 virt/kvm/kvm_main.c |    3 ---
 3 files changed, 6 insertions(+), 7 deletions(-)
arch/x86/kvm/svm.c +2 -2
index d1036ce..02a4269 100644
@@ -482,7 +482,7 @@ static __init int svm_hardware_setup(void)
 		kvm_enable_efer_bits(EFER_SVME);
 	}
 
-	for_each_online_cpu(cpu) {
+	for_each_possible_cpu(cpu) {
 		r = svm_cpu_init(cpu);
 		if (r)
 			goto err;
@@ -516,7 +516,7 @@ static __exit void svm_hardware_unsetup(void)
 {
 	int cpu;
 
-	for_each_online_cpu(cpu)
+	for_each_possible_cpu(cpu)
 		svm_cpu_uninit(cpu);
 
 	__free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
arch/x86/kvm/vmx.c +4 -2
index 3fe0d42..e86f1a6 100644
@@ -1350,15 +1350,17 @@ static void free_kvm_area(void)
 {
 	int cpu;
 
-	for_each_online_cpu(cpu)
+	for_each_possible_cpu(cpu) {
 		free_vmcs(per_cpu(vmxarea, cpu));
+		per_cpu(vmxarea, cpu) = NULL;
+	}
 }
 
 static __init int alloc_kvm_area(void)
 {
 	int cpu;
 
-	for_each_online_cpu(cpu) {
+	for_each_possible_cpu(cpu) {
 		struct vmcs *vmcs;
 
 		vmcs = alloc_vmcs_cpu(cpu);
virt/kvm/kvm_main.c +0 -3
index e27b7a9..2cd8bc2 100644
@@ -1716,9 +1716,6 @@ static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
 {
 	int cpu = (long)v;
 
-	if (!kvm_usage_count)
-		return NOTIFY_OK;
-
 	val &= ~CPU_TASKS_FROZEN;
 	switch (val) {
 	case CPU_DYING:
Signature · 1 line
1.6.4.4
#29 lore raw

Re: [PATCH v2: kvm 2/4] Kill the confusing tsc_ref_khz and ref_freq variables.

In reply to Zachary Amsden
Quoted 16 lines · Zachary Amsden

They are globals, not clearly protected by any ordering or locking, and vulnerable to various startup races.

Instead, for variable TSC machines, register the cpufreq notifier and get the TSC frequency directly from the cpufreq machinery. Not only is it always right, it is also perfectly accurate, as no error prone measurement is required.

On such machines, when a new CPU online is brought online, it isn't clear what frequency it will start with, and it may not correspond to the reference, thus in hardware_enable we clear the cpu_tsc_khz variable to zero and make sure it is set before running on a VCPU.

CPUFREQ_TRANSITION_NOTIFIER);

arch/x86/kvm/x86.c +1 -0
+		for_each_online_cpu(cpu)

arch/x86/kvm/x86.c static void kvm_timer_init(void) · line 3132

Quoted 10 lines
arch/x86/kvm/x86.c +8 -0
+			per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu);
+	} else {
+		for_each_possible_cpu(cpu)
+			per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
+	}
+	for_each_possible_cpu(cpu) {
+		printk(KERN_DEBUG "kvm: cpu %d = %ld khz\n",
+			cpu, per_cpu(cpu_tsc_khz, cpu));
  	}
  }

Leftover debug code?

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw

Re: [PATCH v2: kvm 4/4] Fix hotplug of CPUs for KVM.

In reply to Zachary Amsden
Quoted 12 lines · Zachary Amsden

Both VMX and SVM require per-cpu memory allocation, which is done at module init time, for only online cpus.

Backend was not allocating enough structure for all possible CPUs, so new CPUs coming online could not be hardware enabled.

virt/kvm/kvm_main.c +0 -0
index e27b7a9..2cd8bc2 100644

virt/kvm/kvm_main.c static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, · line 1721

Quoted 9 lines
virt/kvm/kvm_main.c +0 -3
@@ -1716,9 +1716,6 @@ static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  {
  	int cpu = (long)v;
 
-	if (!kvm_usage_count)
-		return NOTIFY_OK;
-
  	val&= ~CPU_TASKS_FROZEN;
  	switch (val) {
  	case CPU_DYING:

I still don't see how this bit can work. Maybe if we move the notification registration to the point where kvm_usage_count is bumped.

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw

Re: [PATCH v2: kvm 4/4] Fix hotplug of CPUs for KVM.

Quoted 16 lines · Avi Kivity
Quoted 15 lines · Zachary Amsden

Both VMX and SVM require per-cpu memory allocation, which is done at module init time, for only online cpus.

Backend was not allocating enough structure for all possible CPUs, so new CPUs coming online could not be hardware enabled.

virt/kvm/kvm_main.c +0 -0
index e27b7a9..2cd8bc2 100644
@@ -1716,9 +1716,6 @@ static int kvm_cpu_hotplug(struct 
 notifier_block *notifier, unsigned long val,
  {
      int cpu = (long)v;
 
Quoted 8 lines
Quoted 6 lines
virt/kvm/kvm_main.c +0 -3
-    if (!kvm_usage_count)
-        return NOTIFY_OK;
-
      val&= ~CPU_TASKS_FROZEN;
      switch (val) {

case CPU_DYING:

I still don't see how this bit can work. Maybe if we move the notification registration to the point where kvm_usage_count is bumped.

That was stray junk in the patch. Let me rediff...

#7 lore raw

PATCH v4 1/4 Code motion. Separate timer intialization into an indepedent function.

In reply to Avi Kivity
---
 arch/x86/kvm/x86.c |   23 +++++++++++++++--------
 1 files changed, 15 insertions(+), 8 deletions(-)
arch/x86/kvm/x86.c +15 -8
index fedac9d..15d2ace 100644
@@ -3116,9 +3116,22 @@ static struct notifier_block kvmclock_cpufreq_notifier_block = {
         .notifier_call  = kvmclock_cpufreq_notifier
 };
 
+static void kvm_timer_init(void)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu)
+		per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
+	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
+		tsc_khz_ref = tsc_khz;
+		cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
+					  CPUFREQ_TRANSITION_NOTIFIER);
+	}
+}
+
 int kvm_arch_init(void *opaque)
 {
-	int r, cpu;
+	int r;
 	struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
 
 	if (kvm_x86_ops) {
@@ -3150,13 +3163,7 @@ int kvm_arch_init(void *opaque)
 	kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
 			PT_DIRTY_MASK, PT64_NX_MASK, 0);
 
-	for_each_possible_cpu(cpu)
-		per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
-	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-		tsc_khz_ref = tsc_khz;
-		cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
-					  CPUFREQ_TRANSITION_NOTIFIER);
-	}
+	kvm_timer_init();
 
 	return 0;
 
Signature · 1 line
1.6.4.4
#8 lore raw

PATCH v4 2/4 Kill the confusing tsc_ref_khz and ref_freq variables.

They are globals, not clearly protected by any ordering or locking, and vulnerable to various startup races.

Instead, for variable TSC machines, register the cpufreq notifier and get the TSC frequency directly from the cpufreq machinery. Not only is it always right, it is also perfectly accurate, as no error prone measurement is required.

On such machines, when a new CPU online is brought online, it isn't clear what frequency it will start with, and it may not correspond to the reference, thus in hardware_enable we clear the cpu_tsc_khz variable to zero and make sure it is set before running on a VCPU.

---
 arch/x86/kvm/x86.c |   26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)
arch/x86/kvm/x86.c +16 -10
index 15d2ace..de4ce8f 100644
@@ -1326,6 +1326,8 @@ out:
 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
 	kvm_x86_ops->vcpu_load(vcpu, cpu);
+	if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0))
+		per_cpu(cpu_tsc_khz, cpu) = cpufreq_quick_get(cpu);
 	kvm_request_guest_time_update(vcpu);
 }
 
@@ -3061,9 +3063,6 @@ static void bounce_off(void *info)
 	/* nothing */
 }
 
-static unsigned int  ref_freq;
-static unsigned long tsc_khz_ref;
-
 static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
 				     void *data)
 {
@@ -3072,14 +3071,11 @@ static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long va
 	struct kvm_vcpu *vcpu;
 	int i, send_ipi = 0;
 
-	if (!ref_freq)
-		ref_freq = freq->old;
-
 	if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
 		return 0;
 	if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
 		return 0;
-	per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
+	per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
 
 	spin_lock(&kvm_lock);
 	list_for_each_entry(kvm, &vm_list, vm_list) {
@@ -3120,12 +3116,14 @@ static void kvm_timer_init(void)
 {
 	int cpu;
 
-	for_each_possible_cpu(cpu)
-		per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
 	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-		tsc_khz_ref = tsc_khz;
 		cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
 					  CPUFREQ_TRANSITION_NOTIFIER);
+		for_each_online_cpu(cpu)
+			per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu);
+	} else {
+		for_each_possible_cpu(cpu)
+			per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
 	}
 }
 
@@ -4698,6 +4696,14 @@ int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
 
 int kvm_arch_hardware_enable(void *garbage)
 {
+	/*
+	 * Since this may be called from a hotplug notifcation,
+	 * we can't get the CPU frequency directly.
+	 */
+	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
+		int cpu = raw_smp_processor_id();
+		per_cpu(cpu_tsc_khz, cpu) = 0;
+	}
 	return kvm_x86_ops->hardware_enable(garbage);
 }
 
Signature · 1 line
1.6.4.4
#9 lore raw

PATCH v4 3/4 Fix printk name error in svm.c

---
 arch/x86/kvm/svm.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/svm.c +3 -2
index 9a4daca..d1036ce 100644
@@ -330,13 +330,14 @@ static int svm_hardware_enable(void *garbage)
 		return -EBUSY;
 
 	if (!has_svm()) {
-		printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
+		printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
+		       me);
 		return -EINVAL;
 	}
 	svm_data = per_cpu(svm_data, me);
 
 	if (!svm_data) {
-		printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
+		printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
 		       me);
 		return -EINVAL;
 	}
Signature · 1 line
1.6.4.4
#10 lore raw

PATCH v4 4/4 Fix hotplug of CPUs for KVM.

Both VMX and SVM require per-cpu memory allocation, which is done at module init time, for only online cpus.

Backend was not allocating enough structure for all possible CPUs, so new CPUs coming online could not be hardware enabled.

---
 arch/x86/kvm/svm.c |    4 ++--
 arch/x86/kvm/vmx.c |    6 ++++--
 2 files changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/svm.c +2 -2
index d1036ce..02a4269 100644
@@ -482,7 +482,7 @@ static __init int svm_hardware_setup(void)
 		kvm_enable_efer_bits(EFER_SVME);
 	}
 
-	for_each_online_cpu(cpu) {
+	for_each_possible_cpu(cpu) {
 		r = svm_cpu_init(cpu);
 		if (r)
 			goto err;
@@ -516,7 +516,7 @@ static __exit void svm_hardware_unsetup(void)
 {
 	int cpu;
 
-	for_each_online_cpu(cpu)
+	for_each_possible_cpu(cpu)
 		svm_cpu_uninit(cpu);
 
 	__free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
arch/x86/kvm/vmx.c +4 -2
index 3fe0d42..e86f1a6 100644
@@ -1350,15 +1350,17 @@ static void free_kvm_area(void)
 {
 	int cpu;
 
-	for_each_online_cpu(cpu)
+	for_each_possible_cpu(cpu) {
 		free_vmcs(per_cpu(vmxarea, cpu));
+		per_cpu(vmxarea, cpu) = NULL;
+	}
 }
 
 static __init int alloc_kvm_area(void)
 {
 	int cpu;
 
-	for_each_online_cpu(cpu) {
+	for_each_possible_cpu(cpu) {
 		struct vmcs *vmcs;
 
 		vmcs = alloc_vmcs_cpu(cpu);
Signature · 1 line
1.6.4.4
#23 lore raw

Re: [PATCH v4: kvm 1/4] Code motion. Separate timer intialization into an indepedent function.

In reply to Zachary Amsden
Quoted 2 lines · Zachary Amsden

Looks good.

Is anything preventing us from unifying the constant_tsc and !same paths? We could just do a quick check in the notifier, see the tsc frequency hasn't changed, and return.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#11 lore raw

Re: [PATCH v4: kvm 4/4] Fix hotplug of CPUs for KVM.

In reply to Zachary Amsden
Quoted 7 lines · Zachary Amsden

Both VMX and SVM require per-cpu memory allocation, which is done at module init time, for only online cpus.

Backend was not allocating enough structure for all possible CPUs, so new CPUs coming online could not be hardware enabled.

Applied all, thanks.

#24 lore raw

Re: [PATCH v4: kvm 1/4] Code motion. Separate timer intialization into an indepedent function.

In reply to Avi Kivity
Quoted 7 lines · Avi Kivity
Quoted 1 line · Zachary Amsden

Looks good.

Is anything preventing us from unifying the constant_tsc and !same paths? We could just do a quick check in the notifier, see the tsc frequency hasn't changed, and return.

Actually, yes. On constant_tsc processors, the processor frequency may still change, however the TSC frequency does not change with it.

I actually have both of these kinds of processors (freq changes with constant TSC and freq changes with variable TSC) so I was able to test both of these cases.

Zach

#25 lore raw

Re: [PATCH v4: kvm 1/4] Code motion. Separate timer intialization into an indepedent function.

Quoted 6 lines · Zachary Amsden
Quoted 1 line

Is anything preventing us from unifying the constant_tsc and !same paths? We could just do a quick check in the notifier, see the tsc frequency hasn't changed, and return.

Actually, yes. On constant_tsc processors, the processor frequency may still change, however the TSC frequency does not change with it.

I actually have both of these kinds of processors (freq changes with constant TSC and freq changes with variable TSC) so I was able to test both of these cases.

If the API allows us to query the tsc frequency, it would simply return the same values in all cases, which we'd ignore.

Signature · 1 line
error compiling committee.c: too many arguments to function
#26 lore raw

Re: [PATCH v4: kvm 1/4] Code motion. Separate timer intialization into an indepedent function.

Quoted 3 lines · Avi Kivity

On 09/30/2009 05:51 PM, Zachary Amsden wrote:

If the API allows us to query the tsc frequency, it would simply return the same values in all cases, which we'd ignore.

The API only allows querying the processor frequency. In the constant_tsc case, the highest processor frequency is likely going to be the actual TSC frequency, but I don't think it's a guarantee; theoretically, it could be faster on normal hardware ... or slower on overclocked hardware with an externally clocked TSC.

Zach

#27 lore raw

Re: [PATCH v4: kvm 1/4] Code motion. Separate timer intialization into an indepedent function.

Quoted 6 lines · Zachary Amsden
Quoted 3 lines · Avi Kivity

On 09/30/2009 05:51 PM, Zachary Amsden wrote:

If the API allows us to query the tsc frequency, it would simply return the same values in all cases, which we'd ignore.

The API only allows querying the processor frequency. In the constant_tsc case, the highest processor frequency is likely going to be the actual TSC frequency, but I don't think it's a guarantee; theoretically, it could be faster on normal hardware ... or slower on overclocked hardware with an externally clocked TSC.

Well we could add a new API then (or a new tscfreq notifier). Those conditionals don't belong in client code.

Signature · 1 line
error compiling committee.c: too many arguments to function
#28 lore raw

Re: [PATCH v4: kvm 1/4] Code motion. Separate timer intialization into an indepedent function.

Quoted 9 lines · Avi Kivity
Quoted 6 lines · Zachary Amsden
Quoted 3 lines · Avi Kivity

On 09/30/2009 05:51 PM, Zachary Amsden wrote:

If the API allows us to query the tsc frequency, it would simply return the same values in all cases, which we'd ignore.

The API only allows querying the processor frequency. In the constant_tsc case, the highest processor frequency is likely going to be the actual TSC frequency, but I don't think it's a guarantee; theoretically, it could be faster on normal hardware ... or slower on overclocked hardware with an externally clocked TSC.

Well we could add a new API then (or a new tscfreq notifier). Those conditionals don't belong in client code.

It's possible... but it's also possible to run without cpufreq enabled, which won't work properly unless the cpufreq code is aware of the measured tsc_khz... this could be a little ugly architecture wise given the big melting pot of generic code and vendor / arch specific code here.

Since we're already very hardware dependent and one of the few clients who care, it seems okay to leave it as is for now.

Zach

#12 lore raw

Re: [PATCH v4: kvm 2/4] Kill the confusing tsc_ref_khz and ref_freq variables.

In reply to Zachary Amsden
Quoted 59 lines · Zachary Amsden

They are globals, not clearly protected by any ordering or locking, and vulnerable to various startup races.

Instead, for variable TSC machines, register the cpufreq notifier and get the TSC frequency directly from the cpufreq machinery. Not only is it always right, it is also perfectly accurate, as no error prone measurement is required.

On such machines, when a new CPU online is brought online, it isn't clear what frequency it will start with, and it may not correspond to the reference, thus in hardware_enable we clear the cpu_tsc_khz variable to zero and make sure it is set before running on a VCPU.

---
 arch/x86/kvm/x86.c |   26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)
arch/x86/kvm/x86.c +3 -7
index 15d2ace..de4ce8f 100644
@@ -1326,6 +1326,8 @@ out:
 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
 	kvm_x86_ops->vcpu_load(vcpu, cpu);
+	if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0))
+		per_cpu(cpu_tsc_khz, cpu) = cpufreq_quick_get(cpu);
 	kvm_request_guest_time_update(vcpu);
 }
 
@@ -3061,9 +3063,6 @@ static void bounce_off(void *info)
 	/* nothing */
 }
 
-static unsigned int  ref_freq;
-static unsigned long tsc_khz_ref;
-
 static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
 				     void *data)
 {
@@ -3072,14 +3071,11 @@ static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long va
 	struct kvm_vcpu *vcpu;
 	int i, send_ipi = 0;
 
-	if (!ref_freq)
-		ref_freq = freq->old;
-
 	if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
 		return 0;
 	if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
 		return 0;
-	per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
+	per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
 
 	spin_lock(&kvm_lock);
 	list_for_each_entry(kvm, &vm_list, vm_list) {
@@ -3120,12 +3116,14 @@ static void kvm_timer_init(void)
 {

arch/x86/kvm/x86.c static void kvm_timer_init(void) · line 3123

Quoted 10 lines
arch/x86/kvm/x86.c +2 -3
 	int cpu;
 
-	for_each_possible_cpu(cpu)
-		per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
 	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-		tsc_khz_ref = tsc_khz;
 		cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
 					  CPUFREQ_TRANSITION_NOTIFIER);
+		for_each_online_cpu(cpu)
+			per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu);

This doesn't build for !CONFIG_CPU_FREQ.

Jan

#13 lore raw

Re: [PATCH v4: kvm 2/4] Kill the confusing tsc_ref_khz and ref_freq variables.

Quoted 63 lines · Jan Kiszka
Quoted 61 lines · Zachary Amsden

They are globals, not clearly protected by any ordering or locking, and vulnerable to various startup races.

Instead, for variable TSC machines, register the cpufreq notifier and get the TSC frequency directly from the cpufreq machinery. Not only is it always right, it is also perfectly accurate, as no error prone measurement is required.

On such machines, when a new CPU online is brought online, it isn't clear what frequency it will start with, and it may not correspond to the reference, thus in hardware_enable we clear the cpu_tsc_khz variable to zero and make sure it is set before running on a VCPU.

---
  arch/x86/kvm/x86.c |   26 ++++++++++++++++----------
  1 files changed, 16 insertions(+), 10 deletions(-)
arch/x86/kvm/x86.c +3 -7
index 15d2ace..de4ce8f 100644
@@ -1326,6 +1326,8 @@ out:
  void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  {
  	kvm_x86_ops->vcpu_load(vcpu, cpu);
+	if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0))
+		per_cpu(cpu_tsc_khz, cpu) = cpufreq_quick_get(cpu);
  	kvm_request_guest_time_update(vcpu);
  }
 
@@ -3061,9 +3063,6 @@ static void bounce_off(void *info)
  	/* nothing */
  }
 
-static unsigned int  ref_freq;
-static unsigned long tsc_khz_ref;
-
  static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  				     void *data)
  {
@@ -3072,14 +3071,11 @@ static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long va
  	struct kvm_vcpu *vcpu;
  	int i, send_ipi = 0;
 
-	if (!ref_freq)
-		ref_freq = freq->old;
-
  	if (val == CPUFREQ_PRECHANGE&&  freq->old>  freq->new)
  		return 0;
  	if (val == CPUFREQ_POSTCHANGE&&  freq->old<  freq->new)
  		return 0;
-	per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
+	per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
 
  	spin_lock(&kvm_lock);
  	list_for_each_entry(kvm,&vm_list, vm_list) {
@@ -3120,12 +3116,14 @@ static void kvm_timer_init(void)
  {
  	int cpu;
 
Quoted 11 lines

arch/x86/kvm/x86.c static void kvm_timer_init(void) · line 3129

Quoted 9 lines
arch/x86/kvm/x86.c +2 -3
-	for_each_possible_cpu(cpu)
-		per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
  	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-		tsc_khz_ref = tsc_khz;
  		cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
  					  CPUFREQ_TRANSITION_NOTIFIER);
+		for_each_online_cpu(cpu)
+			per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu);
 

This doesn't build for !CONFIG_CPU_FREQ.

And did it before?

#15 lore raw

Re: [PATCH v4: kvm 2/4] Kill the confusing tsc_ref_khz and ref_freq variables.

In reply to Jan Kiszka
Quoted 89 lines · Jan Kiszka
Quoted 87 lines · Zachary Amsden
Quoted 85 lines · Jan Kiszka
Quoted 69 lines · Zachary Amsden

They are globals, not clearly protected by any ordering or locking, and vulnerable to various startup races.

Instead, for variable TSC machines, register the cpufreq notifier and get
the TSC frequency directly from the cpufreq machinery. Not only is it always right, it is also perfectly accurate, as no error prone measurement
is required.

On such machines, when a new CPU online is brought online, it isn't clear what
frequency it will start with, and it may not correspond to the reference, thus
in hardware_enable we clear the cpu_tsc_khz variable to zero and make sure
it is set before running on a VCPU.

---
   arch/x86/kvm/x86.c |   26 ++++++++++++++++----------
   1 files changed, 16 insertions(+), 10 deletions(-)
arch/x86/kvm/x86.c +2 -3
index 15d2ace..de4ce8f 100644
@@ -1326,6 +1326,8 @@ out:
   void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
   {
       kvm_x86_ops->vcpu_load(vcpu, cpu);
+    if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0))
+        per_cpu(cpu_tsc_khz, cpu) = cpufreq_quick_get(cpu);
       kvm_request_guest_time_update(vcpu);
   }
 
@@ -3061,9 +3063,6 @@ static void bounce_off(void *info)
       /* nothing */
   }
 
-static unsigned int  ref_freq;
-static unsigned long tsc_khz_ref;
-
   static int kvmclock_cpufreq_notifier(struct notifier_block *nb,
 unsigned long val,
                        void *data)
   {
arch/x86/kvm/x86.c +0 -0
@@ -3072,14 +3071,11 @@ static int kvmclock_cpufreq_notifier(struct

notifier_block *nb, unsigned long va

       struct kvm_vcpu *vcpu;
       int i, send_ipi = 0;
arch/x86/kvm/x86.c +0 -4
-    if (!ref_freq)
-        ref_freq = freq->old;
-
       if (val == CPUFREQ_PRECHANGE&&   freq->old>   freq->new)
           return 0;
       if (val == CPUFREQ_POSTCHANGE&&   freq->old<   freq->new)
           return 0;
-    per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref,

ref_freq, freq->new);

arch/x86/kvm/x86.c +1 -0
+    per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
 
       spin_lock(&kvm_lock);
       list_for_each_entry(kvm,&vm_list, vm_list) {
@@ -3120,12 +3116,14 @@ static void kvm_timer_init(void)
   {
       int cpu;
 

arch/x86/kvm/x86.c static void kvm_timer_init(void) · line 3125

Quoted 10 lines
arch/x86/kvm/x86.c +2 -3
-    for_each_possible_cpu(cpu)
-        per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
       if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-        tsc_khz_ref = tsc_khz;
           cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
                         CPUFREQ_TRANSITION_NOTIFIER);
+        for_each_online_cpu(cpu)
+            per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu);
 
 

This doesn't build for !CONFIG_CPU_FREQ.

Quoted 6 lines
Quoted 2 lines

And did it before?

Yes, because cpufreq_get, which is undefined without CONFIG_CPU_FREQ, did not exist so far. One may argue that this is a deficit of the cpufreq API. However, it needs fixing.

I'll send a patch today. I'm going with API deficient and will fix that, but this means I will also require a fix for kvm-kmod :(

Zach

#14 lore raw

Re: [PATCH v4: kvm 2/4] Kill the confusing tsc_ref_khz and ref_freq variables.

In reply to Zachary Amsden
Quoted 84 lines · Zachary Amsden
Quoted 71 lines · Jan Kiszka
Quoted 69 lines · Zachary Amsden

They are globals, not clearly protected by any ordering or locking, and vulnerable to various startup races.

Instead, for variable TSC machines, register the cpufreq notifier and get
the TSC frequency directly from the cpufreq machinery. Not only is it always right, it is also perfectly accurate, as no error prone measurement
is required.

On such machines, when a new CPU online is brought online, it isn't clear what
frequency it will start with, and it may not correspond to the reference, thus
in hardware_enable we clear the cpu_tsc_khz variable to zero and make sure
it is set before running on a VCPU.

---
  arch/x86/kvm/x86.c |   26 ++++++++++++++++----------
  1 files changed, 16 insertions(+), 10 deletions(-)
arch/x86/kvm/x86.c +2 -3
index 15d2ace..de4ce8f 100644
@@ -1326,6 +1326,8 @@ out:
  void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  {
      kvm_x86_ops->vcpu_load(vcpu, cpu);
+    if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0))
+        per_cpu(cpu_tsc_khz, cpu) = cpufreq_quick_get(cpu);
      kvm_request_guest_time_update(vcpu);
  }
 
@@ -3061,9 +3063,6 @@ static void bounce_off(void *info)
      /* nothing */
  }
 
-static unsigned int  ref_freq;
-static unsigned long tsc_khz_ref;
-
  static int kvmclock_cpufreq_notifier(struct notifier_block *nb,
 unsigned long val,
                       void *data)
  {
arch/x86/kvm/x86.c +0 -0
@@ -3072,14 +3071,11 @@ static int kvmclock_cpufreq_notifier(struct

notifier_block *nb, unsigned long va

      struct kvm_vcpu *vcpu;
      int i, send_ipi = 0;
arch/x86/kvm/x86.c +0 -4
-    if (!ref_freq)
-        ref_freq = freq->old;
-
      if (val == CPUFREQ_PRECHANGE&&  freq->old>  freq->new)
          return 0;
      if (val == CPUFREQ_POSTCHANGE&&  freq->old<  freq->new)
          return 0;
-    per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref,

ref_freq, freq->new);

arch/x86/kvm/x86.c +1 -0
+    per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
 
      spin_lock(&kvm_lock);
      list_for_each_entry(kvm,&vm_list, vm_list) {
@@ -3120,12 +3116,14 @@ static void kvm_timer_init(void)
  {
      int cpu;
 
Quoted 11 lines

arch/x86/kvm/x86.c static void kvm_timer_init(void) · line 3129

Quoted 9 lines
arch/x86/kvm/x86.c +2 -3
-    for_each_possible_cpu(cpu)
-        per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
      if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-        tsc_khz_ref = tsc_khz;
          cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
                        CPUFREQ_TRANSITION_NOTIFIER);
+        for_each_online_cpu(cpu)
+            per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu);
 

This doesn't build for !CONFIG_CPU_FREQ.

Quoted 1 line

And did it before?

Yes, because cpufreq_get, which is undefined without CONFIG_CPU_FREQ, did not exist so far. One may argue that this is a deficit of the cpufreq API. However, it needs fixing.

Jan

#16 lore raw

Re: [PATCH v4: kvm 2/4] Kill the confusing tsc_ref_khz and ref_freq variables.

In reply to Zachary Amsden
Quoted 93 lines · Zachary Amsden
Quoted 92 lines · Jan Kiszka
Quoted 90 lines · Zachary Amsden
Quoted 73 lines · Jan Kiszka
Quoted 70 lines · Zachary Amsden

They are globals, not clearly protected by any ordering or locking, and
vulnerable to various startup races.

Instead, for variable TSC machines, register the cpufreq notifier and get
the TSC frequency directly from the cpufreq machinery. Not only is it always right, it is also perfectly accurate, as no error prone measurement
is required.

On such machines, when a new CPU online is brought online, it isn't clear what
frequency it will start with, and it may not correspond to the reference, thus
in hardware_enable we clear the cpu_tsc_khz variable to zero and make sure
it is set before running on a VCPU.

---
   arch/x86/kvm/x86.c |   26 ++++++++++++++++----------
   1 files changed, 16 insertions(+), 10 deletions(-)
arch/x86/kvm/x86.c +2 -3
index 15d2ace..de4ce8f 100644
@@ -1326,6 +1326,8 @@ out:
   void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
   {
       kvm_x86_ops->vcpu_load(vcpu, cpu);
+    if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0))
+        per_cpu(cpu_tsc_khz, cpu) = cpufreq_quick_get(cpu);
       kvm_request_guest_time_update(vcpu);
   }
 
@@ -3061,9 +3063,6 @@ static void bounce_off(void *info)
       /* nothing */
   }
 
-static unsigned int  ref_freq;
-static unsigned long tsc_khz_ref;
-
   static int kvmclock_cpufreq_notifier(struct notifier_block *nb,
 unsigned long val,
                        void *data)
   {
arch/x86/kvm/x86.c +0 -0
@@ -3072,14 +3071,11 @@ static int kvmclock_cpufreq_notifier(struct

notifier_block *nb, unsigned long va

       struct kvm_vcpu *vcpu;
       int i, send_ipi = 0;
arch/x86/kvm/x86.c +0 -4
-    if (!ref_freq)
-        ref_freq = freq->old;
-
       if (val == CPUFREQ_PRECHANGE&&   freq->old>   freq->new)
           return 0;
       if (val == CPUFREQ_POSTCHANGE&&   freq->old<   freq->new)
           return 0;
-    per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref,

ref_freq, freq->new);

arch/x86/kvm/x86.c +1 -0
+    per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
 
       spin_lock(&kvm_lock);
       list_for_each_entry(kvm,&vm_list, vm_list) {
@@ -3120,12 +3116,14 @@ static void kvm_timer_init(void)
   {
       int cpu;
 
Quoted 13 lines

arch/x86/kvm/x86.c static void kvm_timer_init(void)

Quoted 10 lines
arch/x86/kvm/x86.c +2 -3
-    for_each_possible_cpu(cpu)
-        per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
       if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-        tsc_khz_ref = tsc_khz;
           cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
                         CPUFREQ_TRANSITION_NOTIFIER);
+        for_each_online_cpu(cpu)
+            per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu);
 
 

This doesn't build for !CONFIG_CPU_FREQ.

And did it before?

Quoted 7 lines
Quoted 4 lines

Yes, because cpufreq_get, which is undefined without CONFIG_CPU_FREQ, did not exist so far. One may argue that this is a deficit of the cpufreq API. However, it needs fixing.

I'll send a patch today. I'm going with API deficient and will fix that, but this means I will also require a fix for kvm-kmod :(

That won't be tricky, I will queue up a fix.

BTW, is the KVM prepared for cpufreq_get returning 0?

Jan

#17 lore raw

Re: [PATCH v4: kvm 2/4] Kill the confusing tsc_ref_khz and ref_freq variables.

Quoted 107 lines · Jan Kiszka
Quoted 105 lines · Zachary Amsden
Quoted 103 lines · Jan Kiszka
Quoted 92 lines · Zachary Amsden
Quoted 74 lines · Jan Kiszka
Quoted 70 lines · Zachary Amsden

They are globals, not clearly protected by any ordering or locking, and
vulnerable to various startup races.

Instead, for variable TSC machines, register the cpufreq notifier and get
the TSC frequency directly from the cpufreq machinery. Not only is it always right, it is also perfectly accurate, as no error prone measurement
is required.

On such machines, when a new CPU online is brought online, it isn't clear what
frequency it will start with, and it may not correspond to the reference, thus
in hardware_enable we clear the cpu_tsc_khz variable to zero and make sure
it is set before running on a VCPU.

---
    arch/x86/kvm/x86.c |   26 ++++++++++++++++----------
    1 files changed, 16 insertions(+), 10 deletions(-)
arch/x86/kvm/x86.c +2 -3
index 15d2ace..de4ce8f 100644
@@ -1326,6 +1326,8 @@ out:
    void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
    {
        kvm_x86_ops->vcpu_load(vcpu, cpu);
+    if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0))
+        per_cpu(cpu_tsc_khz, cpu) = cpufreq_quick_get(cpu);
        kvm_request_guest_time_update(vcpu);
    }
 
@@ -3061,9 +3063,6 @@ static void bounce_off(void *info)
        /* nothing */
    }
 
-static unsigned int  ref_freq;
-static unsigned long tsc_khz_ref;
-
    static int kvmclock_cpufreq_notifier(struct notifier_block *nb,
 unsigned long val,
                         void *data)
    {
arch/x86/kvm/x86.c +0 -0
@@ -3072,14 +3071,11 @@ static int kvmclock_cpufreq_notifier(struct

notifier_block *nb, unsigned long va

        struct kvm_vcpu *vcpu;
        int i, send_ipi = 0;
arch/x86/kvm/x86.c +0 -4
-    if (!ref_freq)
-        ref_freq = freq->old;
-
        if (val == CPUFREQ_PRECHANGE&&    freq->old>    freq->new)
            return 0;
        if (val == CPUFREQ_POSTCHANGE&&    freq->old<    freq->new)
            return 0;
-    per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref,

ref_freq, freq->new);

arch/x86/kvm/x86.c +1 -0
+    per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
 
        spin_lock(&kvm_lock);
        list_for_each_entry(kvm,&vm_list, vm_list) {
@@ -3120,12 +3116,14 @@ static void kvm_timer_init(void)
    {
        int cpu;
 
Quoted 15 lines

arch/x86/kvm/x86.c static void kvm_timer_init(void)

Quoted 11 lines
arch/x86/kvm/x86.c +2 -3
-    for_each_possible_cpu(cpu)
-        per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
        if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-        tsc_khz_ref = tsc_khz;
            cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
                          CPUFREQ_TRANSITION_NOTIFIER);
+        for_each_online_cpu(cpu)
+            per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu);
 
 
 

This doesn't build for !CONFIG_CPU_FREQ.

Quoted 3 lines

And did it before?

Yes, because cpufreq_get, which is undefined without CONFIG_CPU_FREQ, did not exist so far. One may argue that this is a deficit of the cpufreq API. However, it needs fixing.

Quoted 7 lines
Quoted 3 lines

I'll send a patch today. I'm going with API deficient and will fix that, but this means I will also require a fix for kvm-kmod :(

That won't be tricky, I will queue up a fix.

BTW, is the KVM prepared for cpufreq_get returning 0?

That will be part of the fix, I think.

#18 lore raw

Re: [PATCH v4: kvm 2/4] Kill the confusing tsc_ref_khz and ref_freq variables.

Quoted 115 lines · Zachary Amsden
Quoted 114 lines · Jan Kiszka
Quoted 112 lines · Zachary Amsden
Quoted 99 lines · Jan Kiszka
Quoted 96 lines · Zachary Amsden
Quoted 93 lines · Jan Kiszka
Quoted 75 lines · Zachary Amsden

They are globals, not clearly protected by any ordering or locking, and
vulnerable to various startup races.

Instead, for variable TSC machines, register the cpufreq notifier and
get
the TSC frequency directly from the cpufreq machinery. Not only is it
always right, it is also perfectly accurate, as no error prone measurement
is required.

On such machines, when a new CPU online is brought online, it isn't clear what
frequency it will start with, and it may not correspond to the reference, thus
in hardware_enable we clear the cpu_tsc_khz variable to zero and make
sure
it is set before running on a VCPU.

---
    arch/x86/kvm/x86.c |   26 ++++++++++++++++----------
    1 files changed, 16 insertions(+), 10 deletions(-)
arch/x86/kvm/x86.c +2 -3
index 15d2ace..de4ce8f 100644
@@ -1326,6 +1326,8 @@ out:
    void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
    {
        kvm_x86_ops->vcpu_load(vcpu, cpu);
+    if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0))
+        per_cpu(cpu_tsc_khz, cpu) = cpufreq_quick_get(cpu);
        kvm_request_guest_time_update(vcpu);
    }
 
@@ -3061,9 +3063,6 @@ static void bounce_off(void *info)
        /* nothing */
    }
 
-static unsigned int  ref_freq;
-static unsigned long tsc_khz_ref;
-
    static int kvmclock_cpufreq_notifier(struct notifier_block *nb,
 unsigned long val,
                         void *data)
    {
arch/x86/kvm/x86.c +0 -0
@@ -3072,14 +3071,11 @@ static int kvmclock_cpufreq_notifier(struct

notifier_block *nb, unsigned long va

        struct kvm_vcpu *vcpu;
        int i, send_ipi = 0;
arch/x86/kvm/x86.c +0 -4
-    if (!ref_freq)
-        ref_freq = freq->old;
-
        if (val == CPUFREQ_PRECHANGE&&    freq->old>    freq->new)
            return 0;
        if (val == CPUFREQ_POSTCHANGE&&    freq->old<    freq->new)
            return 0;
-    per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref,

ref_freq, freq->new);

arch/x86/kvm/x86.c +1 -1
+    per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
 
        spin_lock(&kvm_lock);
        list_for_each_entry(kvm,&vm_list, vm_list) {
@@ -3120,12 +3116,14 @@ static void kvm_timer_init(void)
    {
        int cpu;
 
-    for_each_possible_cpu(cpu)

arch/x86/kvm/x86.c static void kvm_timer_init(void) · line 3126

Quoted 10 lines
arch/x86/kvm/x86.c +2 -2
-        per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
        if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
-        tsc_khz_ref = tsc_khz;
 
 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
                          CPUFREQ_TRANSITION_NOTIFIER);
+        for_each_online_cpu(cpu)
+            per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu);
 
 

This doesn't build for !CONFIG_CPU_FREQ.

Quoted 8 lines
Quoted 3 lines

And did it before?

Yes, because cpufreq_get, which is undefined without CONFIG_CPU_FREQ, did not exist so far. One may argue that this is a deficit of the cpufreq API. However, it needs fixing.

I'll send a patch today. I'm going with API deficient and will fix that, but this means I will also require a fix for kvm-kmod :(

Quoted 7 lines
Quoted 4 lines

That won't be tricky, I will queue up a fix.

BTW, is the KVM prepared for cpufreq_get returning 0?

That will be part of the fix, I think.

That's good, specifically as it looks like cpufreq_get can return 0 even in the CONFIG_CPU_FREQ case, at least theoretically.

Jan

PS:

external-module-compat-comm.h +7 -0
index 47fdc86..de8ab23 100644
@@ -993,3 +993,10 @@ unsigned long kvm_vma_kernel_pagesize(struct vm_area_struct *vma)
 	}					\
 })
 #endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33) && !defined(CONFIG_CPU_FREQ)
+static inline unsigned int cpufreq_get(unsigned int cpu)
+{
+	return 0;
+}
+#endif
#19 lore raw

PATCH 1/1 cpufreq: Make cpufreq_get always defined

In reply to Jan Kiszka

Even if not using cpufreq, there is little justification for exporting cpufreq_quick_get and not cpufreq_get, especially when it is so easy and harmless to do.

include/linux/cpufreq.h +9 -4
index 44717eb..684fc4b 100644
@@ -291,17 +291,22 @@ struct global_attr {
 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
 int cpufreq_update_policy(unsigned int cpu);
 
-/* query the current CPU frequency (in kHz). If zero, cpufreq couldn't detect it */
-unsigned int cpufreq_get(unsigned int cpu);
-
-/* query the last known CPU freq (in kHz). If zero, cpufreq couldn't detect it */
 #ifdef CONFIG_CPU_FREQ
+/* query the last known CPU freq (in kHz). If zero, cpufreq doesn't know it */
 unsigned int cpufreq_quick_get(unsigned int cpu);
+
+/* query the current CPU frequency (in kHz). If zero, cpufreq doesn't know it */
+unsigned int cpufreq_get(unsigned int cpu);
 #else
 static inline unsigned int cpufreq_quick_get(unsigned int cpu)
 {
 	return 0;
 }
+
+static inline unsigned int cpufreq_get(unsigned int cpu)
+{
+	return 0;
+}
 #endif
 
 
#20 lore raw

PATCH 1/1 KVM: 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.

arch/x86/kvm/x86.c +12 -4
index 11a6f2f..6604f4c 100644
@@ -1345,8 +1345,12 @@ out:
 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
 	kvm_x86_ops->vcpu_load(vcpu, cpu);
-	if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0))
-		per_cpu(cpu_tsc_khz, cpu) = cpufreq_quick_get(cpu);
+	if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
+		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);
 }
 
@@ -3140,8 +3144,12 @@ static void kvm_timer_init(void)
 	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
 		cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
 					  CPUFREQ_TRANSITION_NOTIFIER);
-		for_each_online_cpu(cpu)
-			per_cpu(cpu_tsc_khz, cpu) = cpufreq_get(cpu);
+		for_each_online_cpu(cpu) {
+			unsigned long khz = cpufreq_get(cpu);
+			if (!khz)
+				khz = tsc_khz; 
+			per_cpu(cpu_tsc_khz, cpu) = khz;
+		}
 	} else {
 		for_each_possible_cpu(cpu)
 			per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
#21 lore raw

PATCH 1/1 kvm-kmod cpufreq_get fix

Define cpufreq_get for kernels which don't export it when not config'd.

external-module-compat-comm.h +9 -0
index 47fdc86..6fe7d87 100644
@@ -993,3 +993,12 @@ unsigned long kvm_vma_kernel_pagesize(struct vm_area_struct *vma)
 	}					\
 })
 #endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
+#ifndef  CONFIG_CPU_FREQ
+static inline unsigned int cpufreq_get(unsigned int cpu)
+{
+	return 0;
+}
+#endif
+#endif