Skip to contentSkip to conversation

make tsc stable over migration and machine start

PATCH Superseded Glauber Costa 2 participants 3 messages Feb 1, 2011 – Feb 2, 2011 KVM

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 make tsc stable over migration and machine start

If the machine is stopped, we should not record two different tsc values upon a save operation. The same problem happens with kvmclock.

But kvmclock is taking a different diretion, being now seen as a separate device. Since this is unlikely to happen with the tsc, I am taking the approach here of simply registering a handler for state change, and using a per-CPUState variable that prevents double updates for the TSC.

---
 target-i386/cpu.h |    1 +
 target-i386/kvm.c |   19 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletions(-)
target-i386/cpu.h +1 -0
index 6d619e8..7f1c4f8 100644
@@ -732,6 +732,7 @@ typedef struct CPUX86State {
     uint32_t sipi_vector;
     uint32_t cpuid_kvm_features;
     uint32_t cpuid_svm_features;
+    uint8_t  update_tsc;
     
     /* in order to simplify APIC support, we leave this pointer to the
        user */
target-i386/kvm.c +18 -1
index ecb8405..c3925be 100644
@@ -302,6 +302,16 @@ void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
 
 static int _kvm_arch_init_vcpu(CPUState *env);
 
+static void cpu_update_state(void *opaque, int running, int reason)
+{
+    CPUState *env = opaque;
+
+    if (!running) {
+        env->update_tsc = 1;
+    }
+}
+
+
 int kvm_arch_init_vcpu(CPUState *env)
 {
     int r;
@@ -444,6 +454,8 @@ int kvm_arch_init_vcpu(CPUState *env)
     }
 #endif
 
+    qemu_add_vm_change_state_handler(cpu_update_state, env);
+
     return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
 }
 
@@ -1093,7 +1105,12 @@ static int kvm_get_msrs(CPUState *env)
 	msrs[n++].index = MSR_STAR;
     if (kvm_has_msr_hsave_pa(env))
         msrs[n++].index = MSR_VM_HSAVE_PA;
-    msrs[n++].index = MSR_IA32_TSC;
+
+    if (env->update_tsc) {
+        msrs[n++].index = MSR_IA32_TSC;
+        env->update_tsc = 0;
+    }
+
 #ifdef TARGET_X86_64
     if (lm_capable_kernel) {
         msrs[n++].index = MSR_CSTAR;
Signature · 1 line
1.7.2.3
#1 lore raw
Quoted 19 lines · Glauber Costa

If the machine is stopped, we should not record two different tsc values upon a save operation. The same problem happens with kvmclock.

But kvmclock is taking a different diretion, being now seen as a separate device. Since this is unlikely to happen with the tsc, I am taking the approach here of simply registering a handler for state change, and using a per-CPUState variable that prevents double updates for the TSC.

---
 target-i386/cpu.h |    1 +
 target-i386/kvm.c |   19 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletions(-)
target-i386/cpu.h +0 -0
index 6d619e8..7f1c4f8 100644

target-i386/cpu.h typedef struct CPUX86State { · line 735

Quoted 4 lines
target-i386/cpu.h +1 -0
@@ -732,6 +732,7 @@ typedef struct CPUX86State {
     uint32_t sipi_vector;
     uint32_t cpuid_kvm_features;
     uint32_t cpuid_svm_features;
+    uint8_t  update_tsc;

bool please.

Quoted 11 lines
target-i386/cpu.h +0 -0
     /* in order to simplify APIC support, we leave this pointer to the
        user */
target-i386/kvm.c +0 -0
index ecb8405..c3925be 100644
@@ -302,6 +302,16 @@ void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
 
 static int _kvm_arch_init_vcpu(CPUState *env);
 

target-i386/kvm.c void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status, · line 314

Quoted 10 lines
target-i386/kvm.c +10 -0
+static void cpu_update_state(void *opaque, int running, int reason)
+{
+    CPUState *env = opaque;
+
+    if (!running) {
+        env->update_tsc = 1;
+    }
+}
+
+

Additional blank line.

Quoted 16 lines
target-i386/kvm.c +2 -0
 int kvm_arch_init_vcpu(CPUState *env)
 {
     int r;
@@ -444,6 +454,8 @@ int kvm_arch_init_vcpu(CPUState *env)
     }
 #endif
 
+    qemu_add_vm_change_state_handler(cpu_update_state, env);
+
     return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
 }
 
@@ -1093,7 +1105,12 @@ static int kvm_get_msrs(CPUState *env)
 	msrs[n++].index = MSR_STAR;
     if (kvm_has_msr_hsave_pa(env))
         msrs[n++].index = MSR_VM_HSAVE_PA;

target-i386/kvm.c static int kvm_get_msrs(CPUState *env) · line 1116

Quoted 10 lines
target-i386/kvm.c +6 -1
-    msrs[n++].index = MSR_IA32_TSC;
+
+    if (env->update_tsc) {
+        msrs[n++].index = MSR_IA32_TSC;
+        env->update_tsc = 0;
+    }
+
 #ifdef TARGET_X86_64
     if (lm_capable_kernel) {
         msrs[n++].index = MSR_CSTAR;

Not quite the logic I'm using for kvmclock:

cpu_update_state()

	if running
		tsc_valid = false;

kvm_get_msrs()

	...
	if (!tsc_valid)
		read_tsc
		tsc_valid = !vm_running;

That ensure we always read the tsc while the VM is running, and not only after it was stopped (might otherwise be "surprising" when once visualizing the MSRs).

Jan

#2 lore raw
Quoted 63 lines · Jan Kiszka
Quoted 19 lines · Glauber Costa

If the machine is stopped, we should not record two different tsc values upon a save operation. The same problem happens with kvmclock.

But kvmclock is taking a different diretion, being now seen as a separate device. Since this is unlikely to happen with the tsc, I am taking the approach here of simply registering a handler for state change, and using a per-CPUState variable that prevents double updates for the TSC.

---
 target-i386/cpu.h |    1 +
 target-i386/kvm.c |   19 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletions(-)
target-i386/cpu.h +0 -0
index 6d619e8..7f1c4f8 100644

target-i386/cpu.h typedef struct CPUX86State { · line 735

Quoted 4 lines
target-i386/cpu.h +1 -0
@@ -732,6 +732,7 @@ typedef struct CPUX86State {
     uint32_t sipi_vector;
     uint32_t cpuid_kvm_features;
     uint32_t cpuid_svm_features;
+    uint8_t  update_tsc;

bool please.

Quoted 11 lines
target-i386/cpu.h +0 -0
     /* in order to simplify APIC support, we leave this pointer to the
        user */
target-i386/kvm.c +0 -0
index ecb8405..c3925be 100644
@@ -302,6 +302,16 @@ void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
 
 static int _kvm_arch_init_vcpu(CPUState *env);
 

target-i386/kvm.c void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status, · line 314

Quoted 10 lines
target-i386/kvm.c +10 -0
+static void cpu_update_state(void *opaque, int running, int reason)
+{
+    CPUState *env = opaque;
+
+    if (!running) {
+        env->update_tsc = 1;
+    }
+}
+
+

Additional blank line.

Quoted 12 lines
target-i386/kvm.c +2 -0
 int kvm_arch_init_vcpu(CPUState *env)
 {
     int r;
@@ -444,6 +454,8 @@ int kvm_arch_init_vcpu(CPUState *env)
     }
 #endif
 
+    qemu_add_vm_change_state_handler(cpu_update_state, env);
+
     return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
 }
 
Quoted 16 lines
Quoted 4 lines
target-i386/kvm.c +0 -0
@@ -1093,7 +1105,12 @@ static int kvm_get_msrs(CPUState *env)
 	msrs[n++].index = MSR_STAR;
     if (kvm_has_msr_hsave_pa(env))
         msrs[n++].index = MSR_VM_HSAVE_PA;

target-i386/kvm.c static int kvm_get_msrs(CPUState *env) · line 1116

Quoted 10 lines
target-i386/kvm.c +6 -1
-    msrs[n++].index = MSR_IA32_TSC;
+
+    if (env->update_tsc) {
+        msrs[n++].index = MSR_IA32_TSC;
+        env->update_tsc = 0;
+    }
+
 #ifdef TARGET_X86_64
     if (lm_capable_kernel) {
         msrs[n++].index = MSR_CSTAR;

Not quite the logic I'm using for kvmclock:

Ok. I have all the interest in keeping the same logic. I will respin.

Quoted 16 lines

cpu_update_state()

	if running
		tsc_valid = false;

kvm_get_msrs()

	...
	if (!tsc_valid)
		read_tsc
		tsc_valid = !vm_running;

That ensure we always read the tsc while the VM is running, and not only after it was stopped (might otherwise be "surprising" when once visualizing the MSRs).

Jan