fix kvmclock bug - memory corruption (v2)
v2 cover fix kvmclock bug - memory corruption (v2)
Hi,
Avi, in this patch, I am resetting all msrs upon CPU reset. Hope it is better. Patch 1 is yet another cleanup.
Glauber Costa (2):
change header for kvm_get_msr_list
turn off kvmclock when resetting cpu
qemu-kvm-x86.c | 27 +++++++++++++++++++++++++-- qemu-kvm.h | 1 - 2 files changed, 25 insertions(+), 3 deletions(-)
PATCH v2 1/2 change header for kvm_get_msr_list
We don't use kvm_context anymore. So remove it. Also, the function is not used anywhere else, so make it static.
--- qemu-kvm-x86.c | 4 ++-- qemu-kvm.h | 1 - 2 files changed, 2 insertions(+), 3 deletions(-)
qemu-kvm-x86.c +2 -2
@@ -393,7 +393,7 @@ void kvm_show_code(CPUState *env)/* * Returns available msr list. User must free. */ -struct kvm_msr_list *kvm_get_msr_list (kvm_context_t kvm) +static struct kvm_msr_list *kvm_get_msr_list (void ) {struct kvm_msr_list sizer, *msrs;int r;
@@ -670,7 +670,7 @@ int kvm_arch_qemu_create_context(void)if (kvm_shadow_memory)kvm_set_shadow_pages (kvm_context, kvm_shadow_memory); - kvm_msr_list =kvm_get_msr_list (kvm_context); + kvm_msr_list =kvm_get_msr_list ();if (!kvm_msr_list)return -1 ;for (i =0 ; i < kvm_msr_list->nmsrs; ++i) {
qemu-kvm.h +0 -1
@@ -97,7 +97,6 @@ int handle_io_window(kvm_context_t kvm);int try_push_interrupts (kvm_context_t kvm);#if defined(__x86_64__) || defined(__i386__) -struct kvm_msr_list *kvm_get_msr_list (kvm_context_t );int kvm_get_msrs (CPUState *env,struct kvm_msr_entry *msrs,int n);int kvm_set_msrs (CPUState *env,struct kvm_msr_entry *msrs,int n); int kvm_get_mce_cap_supported(kvm_context_t, uint64_t *mce_cap,
Signature · 1 line
1.6.2.2
PATCH v2 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 writting 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.
It is also necessary to reset other msrs, so this patch resets everything that kvm exports through its MSR list.
This patch fixes the issue for me.
--- qemu-kvm-x86.c | 23 +++++++++++++++++++++++ 1 files changed, 23 insertions(+), 0 deletions(-)
qemu-kvm-x86.c +23 -0
@@ -1417,8 +1417,31 @@ void kvm_arch_push_nmi(void *opaque) }#endif /* KVM_CAP_USER_NMI */ +static int kvm_reset_msrs (CPUState *env) +{ +struct { +struct kvm_msrs info; +struct kvm_msr_entry entries[100 ]; + } msr_data; +int n; +struct kvm_msr_entry *msrs = msr_data.entries; + +if (!kvm_msr_list) +return -1 ; + +for (n =0 ; n < kvm_msr_list->nmsrs; n++) { +kvm_msr_entry_set (&msrs[n], kvm_msr_list->indices[n],0 ); + } + + msr_data.info.nmsrs = n; + +return kvm_vcpu_ioctl (env,KVM_SET_MSRS , &msr_data); +} + +void kvm_arch_cpu_reset (CPUState *env) { +kvm_reset_msrs (env);kvm_arch_reset_vcpu (env);kvm_reset_mpstate (env); }
Signature · 1 line
1.6.2.2
Re: [PATCH v2 2/2] turn off kvmclock when resetting cpu
Quoted 19 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 anotherO.S,we'll simply have the hypervisor writting into a random memory position into the guest. Neat, uh?
Quoted 8 lines
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.It is also necessary to reset other msrs, so this patch resets everything that kvm exports through its MSR list.
Does that include the TSC?
Zach
Re: [PATCH v2 2/2] turn off kvmclock when resetting cpu
Quoted 25 lines · Zachary Amsden
Quoted 24 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 anotherO.S,we'll simply have the hypervisor writting 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.
Quoted 4 lines
Quoted 2 lines
It is also necessary to reset other msrs, so this patch resets everything that kvm exports through its MSR list.
Does that include the TSC?
AFAIK, yes.
Quoted 5 lines · Glauber Costa
Hi,
Avi, in this patch, I am resetting all msrs upon CPU reset. Hope it is better. Patch 1 is yet another cleanup.
Looks good.
Signature · 1 line
error compiling committee.c: too many arguments to function
Marcelo Tosatti
AppliedQuoted 8 lines · Glauber Costa
Hi,
Avi, in this patch, I am resetting all msrs upon CPU reset. Hope it is better. Patch 1 is yet another cleanup.
Glauber Costa (2):
change header forkvm_get_msr_list
turn off kvmclock when resetting cpu
Applied, thanks.