Skip to contentSkip to conversation

paravirt clock series.

PATCH In review Gerd Hoffmann 2 participants 9 messages May 8, 2008 – May 21, 2008

Subsystems (6)
x86 KVM virtio core KVM x86 +2 more subsystems
lore
Share & tools
lore
#0 lore raw

cover paravirt clock series.

Respin of the paravirt clock patch series.

On the host side the kvm paravirt clock is made compatible with the xen clock.

On the guest side some xen code has been factored out into a separate source file shared by both kvm and xen clock implementations.

This time it should work ok for kvm smp guests ;)

cheers,
Gerd

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone

#1 lore raw

PATCH 1/4 Add helper functions for paravirtual clocksources.

The helper functions are intended to be used by both xen and kvm paravirtual clock sources. Following patches of this series put them into use. They are based on the xen code.

Cc Jeremy Fitzhardinge <[email protected]>
---
 arch/x86/Kconfig          |    4 +
 arch/x86/kernel/Makefile  |    1 +
 arch/x86/kernel/pvclock.c |  148 +++++++++++++++++++++++++++++++++++++++++++++
 include/asm-x86/pvclock.h |    6 ++
 4 files changed, 159 insertions(+), 0 deletions(-)
 create mode 100644 arch/x86/kernel/pvclock.c
 create mode 100644 include/asm-x86/pvclock.h
arch/x86/Kconfig +4 -0
index 845ea2b..b12e188 100644
@@ -415,6 +415,10 @@ config PARAVIRT
 	  over full virtualization.  However, when run without a hypervisor
 	  the kernel is theoretically slower and slightly larger.
 
+config PARAVIRT_CLOCK
+	bool
+	default n
+
 endif
 
 config MEMTEST_BOOTPARAM
arch/x86/kernel/Makefile +1 -0
index bbdacb3..5d8e086 100644
@@ -82,6 +82,7 @@ obj-$(CONFIG_VMI)		+= vmi_32.o vmiclock_32.o
 obj-$(CONFIG_KVM_GUEST)		+= kvm.o
 obj-$(CONFIG_KVM_CLOCK)		+= kvmclock.o
 obj-$(CONFIG_PARAVIRT)		+= paravirt.o paravirt_patch_$(BITS).o
+obj-$(CONFIG_PARAVIRT_CLOCK)	+= pvclock.o
 
 ifdef CONFIG_INPUT_PCSPKR
 obj-y				+= pcspeaker.o
arch/x86/kernel/pvclock.c new file +148 -0

148 lines not on this page · raw message

include/asm-x86/pvclock.h new file +6 -0
new file mode 100644
index 0000000..2b9812f
@@ -0,0 +1,6 @@
+#include <linux/clocksource.h>
+#include <asm/kvm_para.h>
+cycle_t pvclock_clocksource_read(struct kvm_vcpu_time_info *src);
+void pvclock_read_wallclock(struct kvm_wall_clock *wall,
+			    struct kvm_vcpu_time_info *vcpu,
+			    struct timespec *ts);
Signature · 8 lines
1.5.4.1


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
#2 lore raw

PATCH 2/4 Make xen use the generic paravirt clocksource code.

In reply to Gerd Hoffmann

This patch switches the xen paravirt clock over to use the generic paravirt clock code.

Cc Jeremy Fitzhardinge <[email protected]>
---
 arch/x86/xen/Kconfig |    1 +
 arch/x86/xen/time.c  |  110 +++++---------------------------------------------
 2 files changed, 12 insertions(+), 99 deletions(-)
arch/x86/xen/Kconfig +1 -0
index 2e641be..3a4f16a 100644
@@ -5,6 +5,7 @@
 config XEN
 	bool "Xen guest support"
 	select PARAVIRT
+	select PARAVIRT_CLOCK
 	depends on X86_32
 	depends on X86_CMPXCHG && X86_TSC && !(X86_VISWS || X86_VOYAGER)
 	help
arch/x86/xen/time.c +11 -99

146 lines not on this page · raw message

Signature · 8 lines
1.5.4.1


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
#3 lore raw

PATCH 3/4 kvm/host: fix paravirt clocksource to be compatible with xen.

In reply to Gerd Hoffmann
---
 arch/x86/kvm/x86.c |   63 +++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 53 insertions(+), 10 deletions(-)
arch/x86/kvm/x86.c +53 -10
index 979f983..6906d54 100644
@@ -493,7 +493,7 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
 {
 	static int version;
 	struct kvm_wall_clock wc;
-	struct timespec wc_ts;
+	struct timespec now,sys,boot;
 
 	if (!wall_clock)
 		return;
@@ -502,9 +502,16 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
 
 	kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
 
-	wc_ts = current_kernel_time();
-	wc.wc_sec = wc_ts.tv_sec;
-	wc.wc_nsec = wc_ts.tv_nsec;
+#if 0
+	/* Hmm, getboottime() isn't exported to modules ... */
+	getboottime(&boot);
+#else
+	now = current_kernel_time();
+	ktime_get_ts(&sys);
+	boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
+#endif
+	wc.wc_sec = boot.tv_sec;
+	wc.wc_nsec = boot.tv_nsec;
 	wc.wc_version = version;
 
 	kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
@@ -537,20 +544,58 @@ static void kvm_write_guest_time(struct kvm_vcpu *v)
 	/*
 	 * The interface expects us to write an even number signaling that the
 	 * update is finished. Since the guest won't see the intermediate
-	 * state, we just write "2" at the end
+	 * state, we just increase by 2 at the end.
 	 */
-	vcpu->hv_clock.version = 2;
+	vcpu->hv_clock.version += 2;
 
 	shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
 
 	memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
-		sizeof(vcpu->hv_clock));
+	       sizeof(vcpu->hv_clock));
 
 	kunmap_atomic(shared_kaddr, KM_USER0);
 
 	mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
 }
 
+static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
+{
+	uint32_t quotient, remainder;
+
+	__asm__ ( "divl %4"
+		  : "=a" (quotient), "=d" (remainder)
+		  : "0" (0), "1" (dividend), "r" (divisor) );
+	return quotient;
+}
+
+static void kvm_set_time_scale(uint32_t tsc_khz, struct kvm_vcpu_time_info *hv_clock)
+{
+	uint64_t nsecs = 1000000000LL;
+	int32_t  shift = 0;
+	uint64_t tps64;
+	uint32_t tps32;
+
+	tps64 = tsc_khz * 1000LL;
+	while (tps64 > nsecs*2) {
+		tps64 >>= 1;
+		shift--;
+	}
+
+	tps32 = (uint32_t)tps64;
+	while (tps32 <= (uint32_t)nsecs) {
+		tps32 <<= 1;
+		shift++;
+	}
+
+	hv_clock->tsc_shift = shift;
+	hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
+
+#if 0
+	printk(KERN_DEBUG "%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
+	       __FUNCTION__, tsc_khz, hv_clock->tsc_shift,
+	       hv_clock->tsc_to_system_mul);
+#endif
+}
 
 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 {
@@ -599,9 +644,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		/* ...but clean it before doing the actual write */
 		vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
 
-		vcpu->arch.hv_clock.tsc_to_system_mul =
-					clocksource_khz2mult(tsc_khz, 22);
-		vcpu->arch.hv_clock.tsc_shift = 22;
+		kvm_set_time_scale(tsc_khz, &vcpu->arch.hv_clock);
 
 		down_read(&current->mm->mmap_sem);
 		vcpu->arch.time_page =
Signature · 8 lines
1.5.4.1


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
#8 lore raw

PATCH 4/4 kvm/guest: fix paravirt clocksource to be compartible with xen.

In reply to Gerd Hoffmann

This patch switches the kvm clocksource code over to use the paravirt clock helpers, thereby making it compatible with xen.

---
 arch/x86/Kconfig           |    1 +
 arch/x86/kernel/kvmclock.c |   84 ++++++++++++++++---------------------------
 2 files changed, 32 insertions(+), 53 deletions(-)
arch/x86/Kconfig +1 -0
index b12e188..30feb9f 100644
@@ -388,6 +388,7 @@ config VMI
 config KVM_CLOCK
 	bool "KVM paravirtualized clock"
 	select PARAVIRT
+	select PARAVIRT_CLOCK
 	depends on !(X86_VISWS || X86_VOYAGER)
 	help
 	  Turning on this option will allow you to run a paravirtualized clock
arch/x86/kernel/kvmclock.c +31 -53

155 lines not on this page · raw message

Signature · 8 lines
1.5.4.1


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
#4 lore raw

Re: [kvm-devel] [PATCH 3/4] kvm/host: fix paravirt clocksource to be compatible with xen.

In reply to Gerd Hoffmann
Quoted 10 lines · Gerd Hoffmann
---
 arch/x86/kvm/x86.c |   63 +++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 53 insertions(+), 10 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 979f983..6906d54 100644

arch/x86/kvm/x86.c static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) · line 497

Quoted 6 lines
arch/x86/kvm/x86.c +1 -1
@@ -493,7 +493,7 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
 {
 	static int version;
 	struct kvm_wall_clock wc;
-	struct timespec wc_ts;
+	struct timespec now,sys,boot;
 

Add spaces.

Quoted 11 lines
arch/x86/kvm/x86.c +1 -3
 	if (!wall_clock)
 		return;
@@ -502,9 +502,16 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
 
 	kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
 
-	wc_ts = current_kernel_time();
-	wc.wc_sec = wc_ts.tv_sec;
-	wc.wc_nsec = wc_ts.tv_nsec;
+#if 0

arch/x86/kvm/x86.c static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) · line 515

Quoted 10 lines
arch/x86/kvm/x86.c +9 -0
+	/* Hmm, getboottime() isn't exported to modules ... */
+	getboottime(&boot);
+#else
+	now = current_kernel_time();
+	ktime_get_ts(&sys);
+	boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
+#endif
+	wc.wc_sec = boot.tv_sec;
+	wc.wc_nsec = boot.tv_nsec;
 

Please drop the #if 0.

Quoted 24 lines
arch/x86/kvm/x86.c +0 -0
 	wc.wc_version = version;
 
arch/x86/kvm/x86.c +3 -3
 	kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
@@ -537,20 +544,58 @@ static void kvm_write_guest_time(struct kvm_vcpu *v)
 	/*
 	 * The interface expects us to write an even number signaling that the
 	 * update is finished. Since the guest won't see the intermediate
-	 * state, we just write "2" at the end
+	 * state, we just increase by 2 at the end.
 	 */
-	vcpu->hv_clock.version = 2;
+	vcpu->hv_clock.version += 2;
 
 	shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
 
 	memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
-		sizeof(vcpu->hv_clock));
+	       sizeof(vcpu->hv_clock));
 
 	kunmap_atomic(shared_kaddr, KM_USER0);
 
 	mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
 }
 

arch/x86/kvm/x86.c static void kvm_write_guest_time(struct kvm_vcpu *v) · line 570

Quoted 10 lines
arch/x86/kvm/x86.c +9 -0
+static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
+{
+	uint32_t quotient, remainder;
+
+	__asm__ ( "divl %4"
+		  : "=a" (quotient), "=d" (remainder)
+		  : "0" (0), "1" (dividend), "r" (divisor) );
+	return quotient;	
+}
 

do_div()?

Quoted 20 lines
arch/x86/kvm/x86.c +20 -0
+
+static void kvm_set_time_scale(uint32_t tsc_khz, struct kvm_vcpu_time_info *hv_clock)
+{
+	uint64_t nsecs = 1000000000LL;
+	int32_t  shift = 0;
+	uint64_t tps64;
+	uint32_t tps32;
+
+	tps64 = tsc_khz * 1000LL;
+	while (tps64 > nsecs*2) {
+		tps64 >>= 1;
+		shift--;
+	}
+
+	tps32 = (uint32_t)tps64;
+	while (tps32 <= (uint32_t)nsecs) {
+		tps32 <<= 1;
+		shift++;
+	}
+

arch/x86/kvm/x86.c static void kvm_write_guest_time(struct kvm_vcpu *v) · line 599

Quoted 10 lines
arch/x86/kvm/x86.c +9 -0
+	hv_clock->tsc_shift = shift;
+	hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
+
+#if 0
+	printk(KERN_DEBUG "%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
+	       __FUNCTION__, tsc_khz, hv_clock->tsc_shift,
+	       hv_clock->tsc_to_system_mul);
+#endif
+}
 

pr_debug() or something?

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 648

Quoted 12 lines
arch/x86/kvm/x86.c +0 -0
 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)

{

arch/x86/kvm/x86.c +1 -3
@@ -599,9 +644,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		/* ...but clean it before doing the actual write */
 		vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
 
-		vcpu->arch.hv_clock.tsc_to_system_mul =
-					clocksource_khz2mult(tsc_khz, 22);
-		vcpu->arch.hv_clock.tsc_shift = 22;
+		kvm_set_time_scale(tsc_khz, &vcpu->arch.hv_clock);
 

What if the tsc frequency changes later on? we need to adjust the multiplier, no?

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

Re: [PATCH 3/4] kvm/host: fix paravirt clocksource to be compatible with xen.

Quoted 3 lines · Avi Kivity
Quoted 1 line

+ struct timespec now,sys,boot;

Add spaces.

Done.

Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/x86.c +4 -0
+#if 0
+    /* Hmm, getboottime() isn't exported to modules ... */
+    getboottime(&boot);
+#else
Quoted 8 lines

arch/x86/kvm/x86.c static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) · line 514

Quoted 6 lines
arch/x86/kvm/x86.c +6 -0
+    now = current_kernel_time();
+    ktime_get_ts(&sys);
+    boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
+#endif
+    wc.wc_sec = boot.tv_sec;
+    wc.wc_nsec = boot.tv_nsec;

Please drop the #if 0.

Done, and added a comment for the calculation.

Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/x86.c +4 -0
+static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
+{
+    uint32_t quotient, remainder;
+
Quoted 8 lines

arch/x86/kvm/x86.c static void kvm_write_guest_time(struct kvm_vcpu *v) · line 569

Quoted 6 lines
arch/x86/kvm/x86.c +5 -0
+    __asm__ ( "divl %4"
+          : "=a" (quotient), "=d" (remainder)
+          : "0" (0), "1" (dividend), "r" (divisor) );
+    return quotient;   
+}

do_div()?

No, this one does something else. Already tried to get rid of that one before ;)

Quoted 1 line

pr_debug() or something?

Done.

Quoted 4 lines
Quoted 2 lines

+ kvm_set_time_scale(tsc_khz, &vcpu->arch.hv_clock);

What if the tsc frequency changes later on? we need to adjust the multiplier, no?

We better do that, yes.

New patch series prepared and tested, will be posted in a moment ...

cheers,
Gerd

Signature · 6 lines
http://kraxel.fedorapeople.org/xenner/

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#6 lore raw

Re: [kvm-devel] [PATCH 3/4] kvm/host: fix paravirt clocksource to be compatible with xen.

Quoted 15 lines · Gerd Hoffmann
Quoted 14 lines

arch/x86/kvm/x86.c static void kvm_write_guest_time(struct kvm_vcpu *v) · line 569

Quoted 12 lines
arch/x86/kvm/x86.c +9 -0
+static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
+{
+    uint32_t quotient, remainder;
+
+    __asm__ ( "divl %4"
+          : "=a" (quotient), "=d" (remainder)
+          : "0" (0), "1" (dividend), "r" (divisor) );
+    return quotient;   
+}

do_div()?

Quoted 4 lines

No, this one does something else. Already tried to get rid of that one before ;)

There is actually a simpler way of coding this:

return dividend / divisor;

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

Re: [kvm-devel] [PATCH 3/4] kvm/host: fix paravirt clocksource to be compatible with xen.

In reply to Gerd Hoffmann
Quoted 15 lines · Gerd Hoffmann
Quoted 14 lines

arch/x86/kvm/x86.c static void kvm_write_guest_time(struct kvm_vcpu *v) · line 569

Quoted 12 lines
arch/x86/kvm/x86.c +9 -0
+static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
+{
+    uint32_t quotient, remainder;
+
+    __asm__ ( "divl %4"
+          : "=a" (quotient), "=d" (remainder)
+          : "0" (0), "1" (dividend), "r" (divisor) );
+    return quotient;   
+}

do_div()?

Quoted 4 lines

No, this one does something else. Already tried to get rid of that one before ;)

Ah yes, it calculates (dividend << 32) / divisor.

Signature · 1 line
error compiling committee.c: too many arguments to function