Skip to contentSkip to conversation

fix kvmclock bug - memory corruption

PATCH Applied Glauber Costa 4 participants 10 messages May 4, 2010 – May 7, 2010 KVM

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover fix kvmclock bug - memory corruption

This patch series fixes I bug I just found with kvmclock, when I booted into a kernel without kvmclock enabled.

Since I am setting msrs, I took the oportunity to use yet another function from upstream qemu (patch 1).

Enjoy

Glauber Costa (2):
replace set_msr_entry with kvm_msr_entry
turn off kvmclock when resetting cpu

 qemu-kvm-x86.c    |   58 ++++++++++++++++++++++++++++++++---------------------
 target-i386/kvm.c |    3 ++
 2 files changed, 38 insertions(+), 23 deletions(-)
#1 lore raw

PATCH 1/2 replace set_msr_entry with kvm_msr_entry

this is yet another function that upstream qemu implements, so we can just use its implementation.

---
 qemu-kvm-x86.c    |   39 ++++++++++++++++-----------------------
 target-i386/kvm.c |    3 +++
 2 files changed, 19 insertions(+), 23 deletions(-)
qemu-kvm-x86.c +16 -23
index 748ff69..439c31a 100644
@@ -693,13 +693,6 @@ int kvm_arch_qemu_create_context(void)
     return 0;
 }
 
-static void set_msr_entry(struct kvm_msr_entry *entry, uint32_t index,
-                          uint64_t data)
-{
-    entry->index = index;
-    entry->data  = data;
-}
-
 /* returns 0 on success, non-0 on failure */
 static int get_msr_entry(struct kvm_msr_entry *entry, CPUState *env)
 {
@@ -960,19 +953,19 @@ void kvm_arch_load_regs(CPUState *env, int level)
     /* msrs */
     n = 0;
     /* Remember to increase msrs size if you add new registers below */
-    set_msr_entry(&msrs[n++], MSR_IA32_SYSENTER_CS,  env->sysenter_cs);
-    set_msr_entry(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp);
-    set_msr_entry(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
+    kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_CS,  env->sysenter_cs);
+    kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp);
+    kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
     if (kvm_has_msr_star)
-	set_msr_entry(&msrs[n++], MSR_STAR,              env->star);
+	kvm_msr_entry_set(&msrs[n++], MSR_STAR,              env->star);
     if (kvm_has_vm_hsave_pa)
-        set_msr_entry(&msrs[n++], MSR_VM_HSAVE_PA, env->vm_hsave);
+        kvm_msr_entry_set(&msrs[n++], MSR_VM_HSAVE_PA, env->vm_hsave);
 #ifdef TARGET_X86_64
     if (lm_capable_kernel) {
-        set_msr_entry(&msrs[n++], MSR_CSTAR,             env->cstar);
-        set_msr_entry(&msrs[n++], MSR_KERNELGSBASE,      env->kernelgsbase);
-        set_msr_entry(&msrs[n++], MSR_FMASK,             env->fmask);
-        set_msr_entry(&msrs[n++], MSR_LSTAR  ,           env->lstar);
+        kvm_msr_entry_set(&msrs[n++], MSR_CSTAR,             env->cstar);
+        kvm_msr_entry_set(&msrs[n++], MSR_KERNELGSBASE,      env->kernelgsbase);
+        kvm_msr_entry_set(&msrs[n++], MSR_FMASK,             env->fmask);
+        kvm_msr_entry_set(&msrs[n++], MSR_LSTAR  ,           env->lstar);
     }
 #endif
     if (level == KVM_PUT_FULL_STATE) {
@@ -983,20 +976,20 @@ void kvm_arch_load_regs(CPUState *env, int level)
          * huge jump-backs that would occur without any writeback at all.
          */
         if (smp_cpus == 1 || env->tsc != 0) {
-            set_msr_entry(&msrs[n++], MSR_IA32_TSC, env->tsc);
+            kvm_msr_entry_set(&msrs[n++], MSR_IA32_TSC, env->tsc);
         }
-        set_msr_entry(&msrs[n++], MSR_KVM_SYSTEM_TIME, env->system_time_msr);
-        set_msr_entry(&msrs[n++], MSR_KVM_WALL_CLOCK, env->wall_clock_msr);
+        kvm_msr_entry_set(&msrs[n++], MSR_KVM_SYSTEM_TIME, env->system_time_msr);
+        kvm_msr_entry_set(&msrs[n++], MSR_KVM_WALL_CLOCK, env->wall_clock_msr);
     }
 #ifdef KVM_CAP_MCE
     if (env->mcg_cap) {
         if (level == KVM_PUT_RESET_STATE)
-            set_msr_entry(&msrs[n++], MSR_MCG_STATUS, env->mcg_status);
+            kvm_msr_entry_set(&msrs[n++], MSR_MCG_STATUS, env->mcg_status);
         else if (level == KVM_PUT_FULL_STATE) {
-            set_msr_entry(&msrs[n++], MSR_MCG_STATUS, env->mcg_status);
-            set_msr_entry(&msrs[n++], MSR_MCG_CTL, env->mcg_ctl);
+            kvm_msr_entry_set(&msrs[n++], MSR_MCG_STATUS, env->mcg_status);
+            kvm_msr_entry_set(&msrs[n++], MSR_MCG_CTL, env->mcg_ctl);
             for (i = 0; i < (env->mcg_cap & 0xff); i++)
-                set_msr_entry(&msrs[n++], MSR_MC0_CTL + i, env->mce_banks[i]);
+                kvm_msr_entry_set(&msrs[n++], MSR_MC0_CTL + i, env->mce_banks[i]);
         }
     }
 #endif
target-i386/kvm.c +3 -0
index 5239eaf..56740bd 100644
@@ -552,6 +552,8 @@ static int kvm_put_sregs(CPUState *env)
     return kvm_vcpu_ioctl(env, KVM_SET_SREGS, &sregs);
 }
 
+#endif 
+
 static void kvm_msr_entry_set(struct kvm_msr_entry *entry,
                               uint32_t index, uint64_t value)
 {
@@ -559,6 +561,7 @@ static void kvm_msr_entry_set(struct kvm_msr_entry *entry,
     entry->data = value;
 }
 
+#ifdef KVM_UPSTREAM
 static int kvm_put_msrs(CPUState *env, int level)
 {
     struct {
Signature · 1 line
1.6.2.2
#2 lore raw

PATCH 2/2 turn off kvmclock when resetting cpu

Currently, in the linux kernel, we reset kvmclock if we are rebooting into a crash kernel through kexec. The rationale, is that a new kernel won't follow the same memory addresses, and the memory where kvmclock is located in the first kernel, will be something else in the second one.

We don't do it in normal reboots, because the second kernel ends up registering kvmclock again, which has the effect of turning off the first instance.

This is, however, totally wrong. This assumes we're booting into a kernel that also has kvmclock enabled. If by some reason we reboot into something that doesn't do kvmclock including but not limited to:
* rebooting into an older kernel without kvmclock support,
* rebooting with no-kvmclock,
* rebootint into another O.S,

we'll simply have the hypervisor writing into a random memory position into the guest. Neat, uh?

Moreover, I believe the fix belongs in qemu, since it is the entity more prepared to detect all kinds of reboots (by means of a cpu_reset), not to mention the presence of misbehaving guests, that can forget to turn kvmclock off.

This patch fixes the issue for me.

---
 qemu-kvm-x86.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
qemu-kvm-x86.c +19 -0
index 439c31a..4b94e04 100644
@@ -1417,8 +1417,27 @@ void kvm_arch_push_nmi(void *opaque)
 }
 #endif /* KVM_CAP_USER_NMI */
 
+static int kvm_turn_off_clock(CPUState *env)
+{
+    struct {
+        struct kvm_msrs info;
+        struct kvm_msr_entry entries[100];
+    } msr_data;
+
+    struct kvm_msr_entry *msrs = msr_data.entries;
+    int n = 0;
+
+    kvm_msr_entry_set(&msrs[n++], MSR_KVM_SYSTEM_TIME, 0);
+    kvm_msr_entry_set(&msrs[n++], MSR_KVM_WALL_CLOCK, 0);
+    msr_data.info.nmsrs = n;
+
+    return kvm_vcpu_ioctl(env, KVM_SET_MSRS, &msr_data);
+}
+
+
 void kvm_arch_cpu_reset(CPUState *env)
 {
+    kvm_turn_off_clock(env);
     kvm_arch_reset_vcpu(env);
     kvm_reset_mpstate(env);
 }
Signature · 1 line
1.6.2.2
Acked-by on the v1 series
#9 lore raw
In reply to Glauber Costa
Quoted 8 lines · Glauber Costa

This patch series fixes I bug I just found with kvmclock, when I booted into a kernel without kvmclock enabled.

Since I am setting msrs, I took the oportunity to use yet another function from upstream qemu (patch 1).

Enjoy

Quoted 9 lines

Glauber Costa (2):
replace set_msr_entry with kvm_msr_entry
turn off kvmclock when resetting cpu

  qemu-kvm-x86.c    |   58 ++++++++++++++++++++++++++++++++---------------------
  target-i386/kvm.c |    3 ++
  2 files changed, 38 insertions(+), 23 deletions(-)
#3 lore raw

Re: [PATCH 2/2] turn off kvmclock when resetting cpu

In reply to Glauber Costa
Quoted 42 lines · Glauber Costa

Currently, in the linux kernel, we reset kvmclock if we are rebooting into a crash kernel through kexec. The rationale, is that a new kernel won't follow the same memory addresses, and the memory where kvmclock is located in the first kernel, will be something else in the second one.

We don't do it in normal reboots, because the second kernel ends up registering kvmclock again, which has the effect of turning off the first instance.

This is, however, totally wrong. This assumes we're booting into a kernel that also has kvmclock enabled. If by some reason we reboot into something that doesn't do kvmclock including but not limited to:
* rebooting into an older kernel without kvmclock support,
* rebooting with no-kvmclock,
* rebootint into another O.S,

we'll simply have the hypervisor writing into a random memory position into the guest. Neat, uh?

Moreover, I believe the fix belongs in qemu, since it is the entity more prepared to detect all kinds of reboots (by means of a cpu_reset), not to mention the presence of misbehaving guests, that can forget to turn kvmclock off.

This patch fixes the issue for me.

---
  qemu-kvm-x86.c |   19 +++++++++++++++++++
  1 files changed, 19 insertions(+), 0 deletions(-)
qemu-kvm-x86.c +3 -0
index 439c31a..4b94e04 100644
@@ -1417,8 +1417,27 @@ void kvm_arch_push_nmi(void *opaque)
  }
  #endif /* KVM_CAP_USER_NMI */
 
+static int kvm_turn_off_clock(CPUState *env)
+{
+    struct {

qemu-kvm-x86.c void kvm_arch_push_nmi(void *opaque) · line 1432

Quoted 10 lines
qemu-kvm-x86.c +9 -0
+        struct kvm_msrs info;
+        struct kvm_msr_entry entries[100];
+    } msr_data;
+
+    struct kvm_msr_entry *msrs = msr_data.entries;
+    int n = 0;
+
+    kvm_msr_entry_set(&msrs[n++], MSR_KVM_SYSTEM_TIME, 0);
+    kvm_msr_entry_set(&msrs[n++], MSR_KVM_WALL_CLOCK, 0);
 

This fails if the kernel doesn't support those MSRs. Moreover, you need to use the new MSRs as well if we are ever to succeed in deprecating the old ones.

qemu-kvm-x86.c void kvm_arch_push_nmi(void *opaque)

Quoted 7 lines
qemu-kvm-x86.c +6 -0
+    msr_data.info.nmsrs = n;
+
+    return kvm_vcpu_ioctl(env, KVM_SET_MSRS,&msr_data);
+}
+
+
 

How about a different approach? Query the supported MSRs (KVM_GET_MSR_LIST or thereabout) and reset them (with special cases for the TSC, and the old clock MSRs when the new ones are present)?

Long term we need a kernel reset function, but this will do for now.

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

Re: [PATCH 1/2] replace set_msr_entry with kvm_msr_entry

In reply to Glauber Costa
Quoted 4 lines · Glauber Costa

this is yet another function that upstream qemu implements, so we can just use its implementation.

Applied, thanks.

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

Re: [PATCH 2/2] turn off kvmclock when resetting cpu

In reply to Avi Kivity
Quoted 10 lines · Avi Kivity

qemu-kvm-x86.c void kvm_arch_push_nmi(void *opaque) · line 1437

Quoted 6 lines
qemu-kvm-x86.c +6 -0
+    msr_data.info.nmsrs = n;
+
+    return kvm_vcpu_ioctl(env, KVM_SET_MSRS,&msr_data);
+}
+
+

How about a different approach? Query the supported MSRs (KVM_GET_MSR_LIST or thereabout) and reset them (with special cases for the TSC, and the old clock MSRs when the new ones are present)?

I didn't went that route because I was unsure that every one of them would be resetable by writing 0 on it.

And if we are going to special case the most part of it, then there is no point in doing it.

If you think it is doable to special case just the tsc, then I am fine.

#5 lore raw

Re: [PATCH 2/2] turn off kvmclock when resetting cpu

Quoted 14 lines · Glauber Costa
Quoted 11 lines · Avi Kivity

qemu-kvm-x86.c void kvm_arch_push_nmi(void *opaque) · line 1437

Quoted 7 lines
qemu-kvm-x86.c +6 -0
+    msr_data.info.nmsrs = n;
+
+    return kvm_vcpu_ioctl(env, KVM_SET_MSRS,&msr_data);
+}
+
+

How about a different approach? Query the supported MSRs (KVM_GET_MSR_LIST or thereabout) and reset them (with special cases for the TSC, and the old clock MSRs when the new ones are present)?

Quoted 3 lines

I didn't went that route because I was unsure that every one of them would be resetable by writing 0 on it.

There are probably others. We should reset them correctly anyway.

It's probably done by generic qemu code so it works.

Quoted 5 lines

And if we are going to special case the most part of it, then there is no point in doing it.

If you think it is doable to special case just the tsc, then I am fine.

I think if we have the following sequence

clear all msrs
qemu reset
kvm specific msr reset

Then we'd be fine.

Oh, and tsc needs to be reset to 0 as well - it isn't a special case.

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

Re: [PATCH 2/2] turn off kvmclock when resetting cpu

Quoted 26 lines · Avi Kivity
Quoted 11 lines · Glauber Costa
Quoted 9 lines · Avi Kivity

qemu-kvm-x86.c void kvm_arch_push_nmi(void *opaque) · line 1437

Quoted 6 lines
qemu-kvm-x86.c +6 -0
+    msr_data.info.nmsrs = n;
+
+    return kvm_vcpu_ioctl(env, KVM_SET_MSRS,&msr_data);
+}
+
+

How about a different approach? Query the supported MSRs (KVM_GET_MSR_LIST or thereabout) and reset them (with special cases for the TSC, and the old clock MSRs when the new ones are present)?

Quoted 2 lines

I didn't went that route because I was unsure that every one of them would be resetable by writing 0 on it.

There are probably others. We should reset them correctly anyway.

It's probably done by generic qemu code so it works.

Quoted 4 lines

And if we are going to special case the most part of it, then there is no point in doing it.

If you think it is doable to special case just the tsc, then I am fine.

I think if we have the following sequence

Quoted 7 lines

clear all msrs
qemu reset
kvm specific msr reset

Then we'd be fine.

Oh, and tsc needs to be reset to 0 as well - it isn't a special case.

This means a guest running on a perfectly synchronized tsc host will not see a sync tsc. Simply because we can't possible reset all tscs at the same time.

#7 lore raw

Re: [PATCH 2/2] turn off kvmclock when resetting cpu

In reply to Glauber Costa
Quoted 50 lines · Glauber Costa

Currently, in the linux kernel, we reset kvmclock if we are rebooting into a crash kernel through kexec. The rationale, is that a new kernel won't follow the same memory addresses, and the memory where kvmclock is located in the first kernel, will be something else in the second one.

We don't do it in normal reboots, because the second kernel ends up registering kvmclock again, which has the effect of turning off the first instance.

This is, however, totally wrong. This assumes we're booting into a kernel that also has kvmclock enabled. If by some reason we reboot into something that doesn't do kvmclock including but not limited to:
* rebooting into an older kernel without kvmclock support,
* rebooting with no-kvmclock,
* rebootint into another O.S,

we'll simply have the hypervisor writing into a random memory position into the guest. Neat, uh?

Moreover, I believe the fix belongs in qemu, since it is the entity more prepared to detect all kinds of reboots (by means of a cpu_reset), not to mention the presence of misbehaving guests, that can forget to turn kvmclock off.

This patch fixes the issue for me.

---
 qemu-kvm-x86.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
qemu-kvm-x86.c +11 -0
index 439c31a..4b94e04 100644
@@ -1417,8 +1417,27 @@ void kvm_arch_push_nmi(void *opaque)
 }
 #endif /* KVM_CAP_USER_NMI */
 
+static int kvm_turn_off_clock(CPUState *env)
+{
+    struct {
+        struct kvm_msrs info;
+        struct kvm_msr_entry entries[100];
+    } msr_data;
+
+    struct kvm_msr_entry *msrs = msr_data.entries;
+    int n = 0;
+
+    kvm_msr_entry_set(&msrs[n++], MSR_KVM_SYSTEM_TIME, 0);

qemu-kvm-x86.c void kvm_arch_push_nmi(void *opaque) · line 1440

Quoted 10 lines
qemu-kvm-x86.c +8 -0
+    kvm_msr_entry_set(&msrs[n++], MSR_KVM_WALL_CLOCK, 0);
+    msr_data.info.nmsrs = n;
+
+    return kvm_vcpu_ioctl(env, KVM_SET_MSRS, &msr_data);
+}
+
+
 void kvm_arch_cpu_reset(CPUState *env)
 {
+    kvm_turn_off_clock(env);

Better to zero env->system_time_msr and wall_clock_msr here, and modify kvm_arch_load_regs to write those MSRs on KVM_PUT_RESET_STATE (to be conformant with the new writeback scheme).