Skip to contentSkip to conversation

kvm: Fix kvmclock initialization on !CONFIG_KVM_GUEST

PATCH In review OGAWA Hirofumi 2 participants 6 messages Aug 15, 2012 – Aug 18, 2012

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

PATCH 1/1 kvm: Fix kvmclock initialization on !CONFIG_KVM_GUEST

If !CONFIG_KVM_GUEST, kvm_smp_prepare_boot_cpu() is not defined. So, kvm_register_clock("primary cpu clock") in kvm_smp_prepare_boot_cpu() is not called.

The detail of problem is hv_clock percpu usage. hv_clock is percpu variable, but kvmclock_init() is called _before_ initializing percpu area, and doesn't update address after initialized percpu area.

So, host kvm modify the memory area _before_ initializing percpu. This became the cause of strange memory corruption on guest OS.

This fixes it by adding kvm_smp_prepare_boot_cpu(). [we might be better to kill the usage before percpu initialization.]

---

 arch/x86/kernel/kvmclock.c |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/x86/kernel/kvmclock.c renamed from arch/x86/kernel/kvmclock.c~kvm-fix-kvmclock-init +17 -0
diff -puN arch/x86/kernel/kvmclock.c~kvm-fix-kvmclock-init arch/x86/kernel/kvmclock.c
@@ -145,6 +145,14 @@ static void kvm_restore_sched_clock_stat
 	kvm_register_clock("primary cpu clock, resume");
 }
 
+#if defined(CONFIG_SMP) && !defined(CONFIG_KVM_GUEST)
+static void __init kvm_smp_prepare_boot_cpu(void)
+{
+	WARN_ON(kvm_register_clock("primary cpu clock"));
+	native_smp_prepare_boot_cpu();
+}
+#endif
+
 #ifdef CONFIG_X86_LOCAL_APIC
 static void __cpuinit kvm_setup_secondary_clock(void)
 {
@@ -194,6 +202,12 @@ void __init kvmclock_init(void)
 	printk(KERN_INFO "kvm-clock: Using msrs %x and %x",
 		msr_kvm_system_time, msr_kvm_wall_clock);
 
+	/*
+	 * This is temporary register until percpu is initialized.
+	 * After percpu was initialized, we register real hv_clock via
+	 * kvm_smp_prepare_boot_cpu().
+	 * FIXME: it wouldn't be good to use before percpu is initialized.
+	 */
 	if (kvm_register_clock("boot clock"))
 		return;
 	pv_time_ops.sched_clock = kvm_clock_read;
@@ -210,6 +224,9 @@ void __init kvmclock_init(void)
 #ifdef CONFIG_KEXEC
 	machine_ops.crash_shutdown  = kvm_crash_shutdown;
 #endif
+#if defined(CONFIG_SMP) && !defined(CONFIG_KVM_GUEST)
+	smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
+#endif
 	kvm_get_preset_lpj();
 	clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
 	pv_info.paravirt_enabled = 1;

_

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#1 lore raw
Quoted 9 lines · OGAWA Hirofumi

If !CONFIG_KVM_GUEST, kvm_smp_prepare_boot_cpu() is not defined. So, kvm_register_clock("primary cpu clock") in kvm_smp_prepare_boot_cpu() is not called.

The detail of problem is hv_clock percpu usage. hv_clock is percpu variable, but kvmclock_init() is called _before_ initializing percpu area, and doesn't update address after initialized percpu area.

Quoted 8 lines

So, host kvm modify the memory area _before_ initializing percpu. This became the cause of strange memory corruption on guest OS.

This fixes it by adding kvm_smp_prepare_boot_cpu(). [we might be better to kill the usage before percpu initialization.]

The distinction between CONFIG_KVM_CLOCK and CONFIG_KVM_GUEST is not so clear anymore, as this bug demonstrates.

There is no point in having a separate config option, therefore i propose to merge the two (see other reply) instead.

#2 lore raw
Quoted 17 lines · Marcelo Tosatti
Quoted 16 lines · OGAWA Hirofumi

If !CONFIG_KVM_GUEST, kvm_smp_prepare_boot_cpu() is not defined. So, kvm_register_clock("primary cpu clock") in kvm_smp_prepare_boot_cpu() is not called.

The detail of problem is hv_clock percpu usage. hv_clock is percpu variable, but kvmclock_init() is called _before_ initializing percpu area, and doesn't update address after initialized percpu area.

So, host kvm modify the memory area _before_ initializing percpu. This became the cause of strange memory corruption on guest OS.

This fixes it by adding kvm_smp_prepare_boot_cpu(). [we might be better to kill the usage before percpu initialization.]

Quoted 7 lines
Quoted 1 line

The distinction between CONFIG_KVM_CLOCK and CONFIG_KVM_GUEST is not so clear anymore, as this bug demonstrates.

There is no point in having a separate config option, therefore i propose to merge the two (see other reply) instead.

Yes, it was an another option to fix this. As note, the wrong percpu usage (use it before initialization) is still true even if merged KVM_CLOCK.

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#3 lore raw
Quoted 24 lines · OGAWA Hirofumi
Quoted 22 lines · Marcelo Tosatti
Quoted 9 lines · OGAWA Hirofumi

If !CONFIG_KVM_GUEST, kvm_smp_prepare_boot_cpu() is not defined. So, kvm_register_clock("primary cpu clock") in kvm_smp_prepare_boot_cpu() is not called.

The detail of problem is hv_clock percpu usage. hv_clock is percpu variable, but kvmclock_init() is called _before_ initializing percpu area, and doesn't update address after initialized percpu area.

Quoted 8 lines

So, host kvm modify the memory area _before_ initializing percpu. This became the cause of strange memory corruption on guest OS.

This fixes it by adding kvm_smp_prepare_boot_cpu(). [we might be better to kill the usage before percpu initialization.]

The distinction between CONFIG_KVM_CLOCK and CONFIG_KVM_GUEST is not so clear anymore, as this bug demonstrates.

Quoted 6 lines
Quoted 2 lines

There is no point in having a separate config option, therefore i propose to merge the two (see other reply) instead.

Yes, it was an another option to fix this. As note, the wrong percpu usage (use it before initialization) is still true even if merged KVM_CLOCK.

Its fine, i believe, because there is a percpu area for the "boot processor" (see __per_cpu_offset at arch/x86/kernel/setup_percpu.c) before proper initialization.

Can you please confirm the proposed config merge fixes the problem for you?

Quoted 3 lines

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#4 lore raw
Quoted 7 lines · Marcelo Tosatti
Quoted 3 lines

Yes, it was an another option to fix this. As note, the wrong percpu usage (use it before initialization) is still true even if merged KVM_CLOCK.

Its fine, i believe, because there is a percpu area for the "boot processor" (see __per_cpu_offset at arch/x86/kernel/setup_percpu.c) before proper initialization.

Yes. I thought to use early_per_cpu() or similar though, finally I decided to make a patch with minimum logic change.

Quoted 2 lines

Can you please confirm the proposed config merge fixes the problem for you?

kvm-clock: Using msrs 4b564d01 and 4b564d00
kvm-clock: cpu 0, msr 0:1e80f01, boot clock
...
kvm-clock: cpu 0, msr 0:3e3d2f01, primary cpu clock
...
kvm-clock: cpu 1, msr 0:3e5d2f01, secondary cpu clock

I confirmed kvm-clock of boot-cpu switched address properly with your patch (msr xxx:yyy is gpa). This fix the problem.

- hypervisor.
+ hypervisor. It includes a paravirtualized clock, so that instead

BTW, in the patch, above line is having the trailing space - "instead ".

+	  of relying on a PIT (or probably other) emulation by the
+	  underlying device model, the host provides the guest with
+	  timing infrastructure such as time of day, and system time

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#5 lore raw

PATCH 1/1 x86: KVM guest: merge CONFIG_KVM_CLOCK into CONFIG_KVM_GUEST

In reply to OGAWA Hirofumi

The distinction between CONFIG_KVM_CLOCK and CONFIG_KVM_GUEST is not so clear anymore, as demonstrated by recent bugs caused by poor handling of on/off combinations of these options.

Merge CONFIG_KVM_CLOCK into CONFIG_KVM_GUEST.

arch/x86/Kconfig +8 -13
index 8ec3a1a..e0f3164 100644
@@ -573,23 +573,18 @@ config PARAVIRT_TIME_ACCOUNTING
 
 source "arch/x86/xen/Kconfig"
 
-config KVM_CLOCK
-	bool "KVM paravirtualized clock"
-	select PARAVIRT
-	select PARAVIRT_CLOCK
-	---help---
-	  Turning on this option will allow you to run a paravirtualized clock
-	  when running over the KVM hypervisor. Instead of relying on a PIT
-	  (or probably other) emulation by the underlying device model, the host
-	  provides the guest with timing infrastructure such as time of day, and
-	  system time
-
 config KVM_GUEST
-	bool "KVM Guest support"
+	bool "KVM Guest support (including kvmclock)"
+	select PARAVIRT
 	select PARAVIRT
+	select PARAVIRT_CLOCK
+	default y if PARAVIRT_GUEST
 	---help---
 	  This option enables various optimizations for running under the KVM
-	  hypervisor.
+	  hypervisor. It includes a paravirtualized clock, so that instead 
+	  of relying on a PIT (or probably other) emulation by the
+	  underlying device model, the host provides the guest with
+	  timing infrastructure such as time of day, and system time
 
 source "arch/x86/lguest/Kconfig"
 
arch/x86/include/asm/kvm_para.h +2 -2
index 20f5697..eb3e9d8 100644
@@ -102,14 +102,14 @@ struct kvm_vcpu_pv_apf_data {
 extern void kvmclock_init(void);
 extern int kvm_register_clock(char *txt);
 
-#ifdef CONFIG_KVM_CLOCK
+#ifdef CONFIG_KVM_GUEST
 bool kvm_check_and_clear_guest_paused(void);
 #else
 static inline bool kvm_check_and_clear_guest_paused(void)
 {
 	return false;
 }
-#endif /* CONFIG_KVMCLOCK */
+#endif /* CONFIG_KVM_GUEST */
 
 /* This instruction is vmcall.  On non-VT architectures, it will generate a
  * trap that we will then rewrite to the appropriate instruction.
arch/x86/kernel/Makefile +1 -2
index 8215e56..7203298 100644
@@ -81,8 +81,7 @@ obj-$(CONFIG_DEBUG_RODATA_TEST)	+= test_rodata.o
 obj-$(CONFIG_DEBUG_NX_TEST)	+= test_nx.o
 obj-$(CONFIG_DEBUG_NMI_SELFTEST) += nmi_selftest.o
 
-obj-$(CONFIG_KVM_GUEST)		+= kvm.o
-obj-$(CONFIG_KVM_CLOCK)		+= kvmclock.o
+obj-$(CONFIG_KVM_GUEST)		+= kvm.o kvmclock.o
 obj-$(CONFIG_PARAVIRT)		+= paravirt.o paravirt_patch_$(BITS).o
 obj-$(CONFIG_PARAVIRT_SPINLOCKS)+= paravirt-spinlocks.o
 obj-$(CONFIG_PARAVIRT_CLOCK)	+= pvclock.o
arch/x86/kernel/kvm.c +0 -2
index c1d61ee..84a90b6 100644
@@ -396,9 +396,7 @@ void kvm_disable_steal_time(void)
 #ifdef CONFIG_SMP
 static void __init kvm_smp_prepare_boot_cpu(void)
 {
-#ifdef CONFIG_KVM_CLOCK
 	WARN_ON(kvm_register_clock("primary cpu clock"));
-#endif
 	kvm_guest_cpu_init();
 	native_smp_prepare_boot_cpu();
 }
arch/x86/kernel/setup.c +1 -1
index f4b9b80..b3386ae 100644
@@ -957,7 +957,7 @@ void __init setup_arch(char **cmdline_p)
 	initmem_init();
 	memblock_find_dma_reserve();
 
-#ifdef CONFIG_KVM_CLOCK
+#ifdef CONFIG_KVM_GUEST
 	kvmclock_init();
 #endif