Skip to contentSkip to conversation

KVM paravirt clocksource - Take 3 out of <put your number here>

DISCUSSION Glauber de Oliveira Costa 5 participants 16 messages Nov 6, 2007 – Nov 7, 2007

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

This is a new version of kvm paravirt clock implementation

This time, the clockevents part was completely wiped out. Not that I'm dropping it: As said in my last message, I'm starting to take the path of having a specialized irq chip, as for avi-san's suggestion.

However, last iteration made clocksources and clockevents a lot more independent, and so, turning it into a clocksource-only implementation was just a matter of deleting code - that can be later added almost as-is. It also favours people willing to use the clocksource, but going towards userspace HPET emulation, or things like this.

The goal is to have this part included independently of the other bits.

Quoted 1 line

From last release:

* no more clockevents (for a while, hang on!)
* per-vcpu hv_clock area, which led to...
* no more purely tsc timing.

If you have a new concern with this version, or I failed to address a previous concern of yours, just voice it!

#3 lore raw

kvmclock implementation, the guest part.

In reply to Glauber de Oliveira Costa

This is the guest part of kvm clock implementation It does not do tsc-only timing, as tsc can have deltas between cpus, and it did not seem worthy to me to keep adjusting them.

We do use it, however, for fine-grained adjustment.

Other than that, time comes from the host.

---
 arch/i386/Kconfig           |   10 +++
 arch/x86/kernel/Makefile_32 |    1 +
 arch/x86/kernel/kvmclock.c  |  164 +++++++++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/setup_32.c  |    5 ++
 4 files changed, 180 insertions(+), 0 deletions(-)
 create mode 100644 arch/x86/kernel/kvmclock.c
arch/i386/Kconfig +10 -0
index b4437ce..a3b45f1 100644
@@ -257,6 +257,16 @@ config VMI
 	  at the moment), by linking the kernel to a GPL-ed ROM module
 	  provided by the hypervisor.
 
+config KVM_CLOCK
+	bool "KVM paravirtualized clock"
+	select PARAVIRT
+	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, as time of day, and
+	  timer expiration.
+
 source "arch/x86/lguest/Kconfig"
 
 endif
arch/x86/kernel/Makefile_32 +1 -0
index b9d6798..df76d8c 100644
@@ -43,6 +43,7 @@ obj-$(CONFIG_K8_NB)		+= k8.o
 obj-$(CONFIG_MGEODE_LX)		+= geode_32.o mfgpt_32.o
 
 obj-$(CONFIG_VMI)		+= vmi_32.o vmiclock_32.o
+obj-$(CONFIG_KVM_CLOCK)		+= kvmclock.o
 obj-$(CONFIG_PARAVIRT)		+= paravirt_32.o
 obj-y				+= pcspeaker.o
 
arch/x86/kernel/kvmclock.c new file +164 -0

164 lines not on this page · raw message

arch/x86/kernel/setup_32.c +5 -0
index cc0e914..a6cfd47 100644
@@ -44,6 +44,7 @@
 #include <linux/crash_dump.h>
 #include <linux/dmi.h>
 #include <linux/pfn.h>
+#include <linux/kvm_para.h>
 
 #include <video/edid.h>
 
@@ -617,6 +618,10 @@ void __init setup_arch(char **cmdline_p)
 
 	max_low_pfn = setup_memory();
 
+#ifdef CONFIG_KVM_CLOCK
+	kvmclock_init();
+#endif
+
 #ifdef CONFIG_VMI
 	/*
 	 * Must be after max_low_pfn is determined, and before kernel
Signature · 1 line
1.5.0.6
#1 lore raw

include files for kvmclock

In reply to Glauber de Oliveira Costa

This patch introduces the include files for kvm clock. They'll be needed for both guest and host part.

---
 include/asm-x86/kvm_para.h |   23 +++++++++++++++++++++++
 include/linux/kvm.h        |    1 +
 include/linux/kvm_para.h   |   20 ++++++++++++++++++++
 3 files changed, 44 insertions(+), 0 deletions(-)
include/asm-x86/kvm_para.h +23 -0
index c6f3fd8..af9fb75 100644
@@ -10,15 +10,38 @@
  * paravirtualization, the appropriate feature bit should be checked.
  */
 #define KVM_CPUID_FEATURES	0x40000001
+#define KVM_FEATURE_CLOCKEVENTS 0
+#define KVM_FEATURE_CLOCKSOURCE 1
+
 
 #ifdef __KERNEL__
 #include <asm/processor.h>
+extern void kvmclock_init(void);
+
+union kvm_hv_clock {
+	struct {
+		u64 tsc_mult;
+		u64 now_ns;
+		/* That's the wall clock, not the water closet */
+		u64 wc_sec;
+		u64 wc_nsec;
+		u64 last_tsc;
+		/* At first, we could use the tsc value as a marker, but Jeremy
+		 * well noted that it will cause us locking problems in 32-bit
+		 * sys, so we have a special version field */
+		u32 version;
+	};
+	char page_align[PAGE_SIZE];
+};
+
 
 /* This instruction is vmcall.  On non-VT architectures, it will generate a
  * trap that we will then rewrite to the appropriate instruction.
  */
 #define KVM_HYPERCALL ".byte 0x0f,0x01,0xc1"
 
+#define KVM_HCALL_REGISTER_CLOCK	1
+
 /* For KVM hypercalls, a three-byte sequence of either the vmrun or the vmmrun
  * instruction.  The hypervisor may replace it with something else but only the
  * instructions are guaranteed to be supported.
include/linux/kvm.h +1 -0
index 71d33d6..7ac8786 100644
@@ -359,6 +359,7 @@ struct kvm_signal_mask {
 #define KVM_CAP_MMU_SHADOW_CACHE_CONTROL 2
 #define KVM_CAP_USER_MEMORY 3
 #define KVM_CAP_SET_TSS_ADDR 4
+#define KVM_CAP_CLK	  5
 
 /*
  * ioctls for VM fds
include/linux/kvm_para.h +20 -0
index e4db25f..567a192 100644
@@ -11,8 +11,28 @@
 
 /* Return values for hypercalls */
 #define KVM_ENOSYS		1000
+#define KVM_ENODEV		1019
+#define KVM_EINVAL		1022
 
 #ifdef __KERNEL__
+#define KVM_HCALL_REGISTER_CLOCK	1
+
+union kvm_hv_clock {
+	struct {
+		u64 tsc_mult;
+		u64 now_ns;
+		/* That's the wall clock, not the water closet */
+		u64 wc_sec;
+		u64 wc_nsec;
+		u64 last_tsc;
+		/* At first, we could use the tsc value as a marker, but Jeremy
+		 * well noted that it will cause us locking problems in 32-bit
+		 * sys, so we have a special version field */
+		u32 version;
+	};
+	char page_align[PAGE_SIZE];
+};
+
 /*
  * hypercalls use architecture specific
  */
Signature · 1 line
1.5.0.6
#2 lore raw

kvmclock - the host part.

This is the host part of kvm clocksource implementation. As it does not include clockevents, it is a fairly simple implementation. We only have to register a per-vcpu area, and start writting to it periodically.

---
 drivers/kvm/irq.c      |    1 +
 drivers/kvm/kvm_main.c |    2 +
 drivers/kvm/svm.c      |    1 +
 drivers/kvm/vmx.c      |    1 +
 drivers/kvm/x86.c      |   59 ++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/kvm/x86.h      |   13 ++++++++++
 6 files changed, 77 insertions(+), 0 deletions(-)
drivers/kvm/irq.c +1 -0
index 22bfeee..0344879 100644
@@ -92,6 +92,7 @@ void kvm_vcpu_kick_request(struct kvm_vcpu *vcpu, int request)
 
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
+	vcpu->time_needs_update = 1;
 	kvm_inject_apic_timer_irqs(vcpu);
 	/* TODO: PIT, RTC etc. */
 }
drivers/kvm/kvm_main.c +2 -0
index 0b8edca..5834573 100644
@@ -20,6 +20,7 @@
 #include "x86_emulate.h"
 #include "irq.h"
 
+#include <linux/clocksource.h>
 #include <linux/kvm.h>
 #include <linux/module.h>
 #include <linux/errno.h>
@@ -1242,6 +1243,7 @@ static long kvm_dev_ioctl(struct file *filp,
 		case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
 		case KVM_CAP_USER_MEMORY:
 		case KVM_CAP_SET_TSS_ADDR:
+		case KVM_CAP_CLK:
 			r = 1;
 			break;
 		default:
drivers/kvm/svm.c +1 -0
index 95a3489..cb8c19d 100644
@@ -617,6 +617,7 @@ static void svm_free_vcpu(struct kvm_vcpu *vcpu)
 
 	__free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
 	kvm_vcpu_uninit(vcpu);
+	release_clock(vcpu);
 	kmem_cache_free(kvm_vcpu_cache, svm);
 }
 
drivers/kvm/vmx.c +1 -0
index da3a339..b5edeed 100644
@@ -2501,6 +2501,7 @@ static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
 	kfree(vmx->host_msrs);
 	kfree(vmx->guest_msrs);
 	kvm_vcpu_uninit(vcpu);
+	release_clock(vcpu);
 	kmem_cache_free(kvm_vcpu_cache, vmx);
 }
 
drivers/kvm/x86.c +59 -0

90 lines not on this page · raw message

drivers/kvm/x86.h +13 -0
index 663b822..7a951cc 100644
@@ -15,6 +15,7 @@
 
 #include <linux/types.h>
 #include <linux/mm.h>
+#include <linux/highmem.h>
 
 #include <linux/kvm.h>
 #include <linux/kvm_para.h>
@@ -83,8 +84,20 @@ struct kvm_vcpu {
 	/* emulate context */
 
 	struct x86_emulate_ctxt emulate_ctxt;
+
+	union kvm_hv_clock hv_clock;
+	int time_needs_update;
+	struct page *clock_page;
+	void *clock_addr; /* address in host space */
+	u64 clock_gfn; /* guest frame number, physical addr */
+
 };
 
+static inline void release_clock(struct kvm_vcpu *vcpu)
+{
+	if (vcpu->clock_page)
+		kunmap(vcpu->clock_page);
+}
 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code);
 
 static inline void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Signature · 1 line
1.5.0.6
#7 lore raw

Re: [kvm-devel] include files for kvmclock

In reply to Glauber de Oliveira Costa
Quoted 2 lines · Glauber de Oliveira Costa

This patch introduces the include files for kvm clock. They'll be needed for both guest and host part.

And of course, this was my test files by mistake ;-) Oh god... ;-)

Patches aren't numbered but this one should go first. And please just ignore, the replica of the hv_clock union definition in the patch bellow. It should all go in asm/kvm_para.h

The other two patches are fine, and can be applied in any order, so it's not worth resending. I'll grab your comments first.

Quoted 109 lines
---
 include/asm-x86/kvm_para.h |   23 +++++++++++++++++++++++
 include/linux/kvm.h        |    1 +
 include/linux/kvm_para.h   |   20 ++++++++++++++++++++
 3 files changed, 44 insertions(+), 0 deletions(-)
include/asm-x86/kvm_para.h +23 -0
index c6f3fd8..af9fb75 100644
@@ -10,15 +10,38 @@
  * paravirtualization, the appropriate feature bit should be checked.
  */
 #define KVM_CPUID_FEATURES     0x40000001
+#define KVM_FEATURE_CLOCKEVENTS 0
+#define KVM_FEATURE_CLOCKSOURCE 1
+
 
 #ifdef __KERNEL__
 #include <asm/processor.h>
+extern void kvmclock_init(void);
+
+union kvm_hv_clock {
+       struct {
+               u64 tsc_mult;
+               u64 now_ns;
+               /* That's the wall clock, not the water closet */
+               u64 wc_sec;
+               u64 wc_nsec;
+               u64 last_tsc;
+               /* At first, we could use the tsc value as a marker, but Jeremy
+                * well noted that it will cause us locking problems in 32-bit
+                * sys, so we have a special version field */
+               u32 version;
+       };
+       char page_align[PAGE_SIZE];
+};
+
 
 /* This instruction is vmcall.  On non-VT architectures, it will generate a
  * trap that we will then rewrite to the appropriate instruction.
  */
 #define KVM_HYPERCALL ".byte 0x0f,0x01,0xc1"
 
+#define KVM_HCALL_REGISTER_CLOCK       1
+
 /* For KVM hypercalls, a three-byte sequence of either the vmrun or the vmmrun
  * instruction.  The hypervisor may replace it with something else but only the
  * instructions are guaranteed to be supported.
include/linux/kvm.h +1 -0
index 71d33d6..7ac8786 100644
@@ -359,6 +359,7 @@ struct kvm_signal_mask {
 #define KVM_CAP_MMU_SHADOW_CACHE_CONTROL 2
 #define KVM_CAP_USER_MEMORY 3
 #define KVM_CAP_SET_TSS_ADDR 4
+#define KVM_CAP_CLK      5
 
 /*
  * ioctls for VM fds
include/linux/kvm_para.h +20 -1
index e4db25f..567a192 100644
@@ -11,8 +11,28 @@
 
 /* Return values for hypercalls */
 #define KVM_ENOSYS             1000
+#define KVM_ENODEV             1019
+#define KVM_EINVAL             1022
 
 #ifdef __KERNEL__
+#define KVM_HCALL_REGISTER_CLOCK       1
+
+union kvm_hv_clock {
+       struct {
+               u64 tsc_mult;
+               u64 now_ns;
+               /* That's the wall clock, not the water closet */
+               u64 wc_sec;
+               u64 wc_nsec;
+               u64 last_tsc;
+               /* At first, we could use the tsc value as a marker, but Jeremy
+                * well noted that it will cause us locking problems in 32-bit
+                * sys, so we have a special version field */
+               u32 version;
+       };
+       char page_align[PAGE_SIZE];
+};
+
 /*
  * hypercalls use architecture specific
  */
--

1.5.0.6

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel

Signature · 11 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
#11 lore raw

Re: include files for kvmclock

In reply to Glauber de Oliveira Costa
Quoted 18 lines · Glauber de Oliveira Costa

This patch introduces the include files for kvm clock. They'll be needed for both guest and host part.

---
 include/asm-x86/kvm_para.h |   23 +++++++++++++++++++++++
 include/linux/kvm.h        |    1 +
 include/linux/kvm_para.h   |   20 ++++++++++++++++++++
 3 files changed, 44 insertions(+), 0 deletions(-)
include/asm-x86/kvm_para.h +0 -0
index c6f3fd8..af9fb75 100644
@@ -10,15 +10,38 @@
  * paravirtualization, the appropriate feature bit should be checked.
  */
 #define KVM_CPUID_FEATURES	0x40000001

include/asm-x86/kvm_para.h line 22

Quoted 10 lines
include/asm-x86/kvm_para.h +6 -0
+#define KVM_FEATURE_CLOCKEVENTS 0
+#define KVM_FEATURE_CLOCKSOURCE 1
+
 
 #ifdef __KERNEL__
 #include <asm/processor.h>
+extern void kvmclock_init(void);
+
+union kvm_hv_clock {
 

Why two copies of this structure?

Quoted 16 lines
include/asm-x86/kvm_para.h +15 -0
+	struct {
+		u64 tsc_mult;
+		u64 now_ns;
+		/* That's the wall clock, not the water closet */
+		u64 wc_sec;
+		u64 wc_nsec;
+		u64 last_tsc;
+		/* At first, we could use the tsc value as a marker, but Jeremy
+		 * well noted that it will cause us locking problems in 32-bit
+		 * sys, so we have a special version field */
+		u32 version;
+	};
+	char page_align[PAGE_SIZE];
+};
+
 

[...]

Quoted 11 lines
include/asm-x86/kvm_para.h +10 -0
+
+union kvm_hv_clock {
+	struct {
+		u64 tsc_mult;
+		u64 now_ns;
+		/* That's the wall clock, not the water closet */
+		u64 wc_sec;
+		u64 wc_nsec;
+		u64 last_tsc;
+		/* At first, we could use the tsc value as a marker, but Jeremy

include/asm-x86/kvm_para.h

Quoted 10 lines
include/asm-x86/kvm_para.h +7 -0
+		 * well noted that it will cause us locking problems in 32-bit
+		 * sys, so we have a special version field */
+		u32 version;
+	};
+	char page_align[PAGE_SIZE];
+};
+
 /*
  * hypercalls use architecture specific
  */

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/

#12 lore raw

Re: include files for kvmclock

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jeremy Fitzhardinge escreveu:

Quoted 23 lines
Quoted 22 lines · Glauber de Oliveira Costa

This patch introduces the include files for kvm clock. They'll be needed for both guest and host part.

---
 include/asm-x86/kvm_para.h |   23 +++++++++++++++++++++++
 include/linux/kvm.h        |    1 +
 include/linux/kvm_para.h   |   20 ++++++++++++++++++++
 3 files changed, 44 insertions(+), 0 deletions(-)
include/asm-x86/kvm_para.h +3 -0
index c6f3fd8..af9fb75 100644
@@ -10,15 +10,38 @@
  * paravirtualization, the appropriate feature bit should be checked.
  */
 #define KVM_CPUID_FEATURES	0x40000001
+#define KVM_FEATURE_CLOCKEVENTS 0
+#define KVM_FEATURE_CLOCKSOURCE 1
+
 
Quoted 9 lines

include/asm-x86/kvm_para.h line 37

Quoted 6 lines
include/asm-x86/kvm_para.h +3 -0
 #ifdef __KERNEL__
 #include <asm/processor.h>
+extern void kvmclock_init(void);
+
+union kvm_hv_clock {
 

Why two copies of this structure?

It's called silly mistake. ;-)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Remi - http://enigmail.mozdev.org

iD8DBQFHMPGvjYI8LaFUWXMRAgt2AJ9NKgq2LCueUidH56ZgUYA+5wBhGwCfdqQB otFP1/SFowaANQ8FojEtJUE=
=8Xqp
-----END PGP SIGNATURE-----

#4 lore raw

Re: kvmclock - the host part.

In reply to Glauber de Oliveira Costa
Quoted 19 lines · Glauber de Oliveira Costa

This is the host part of kvm clocksource implementation. As it does not include clockevents, it is a fairly simple implementation. We only have to register a per-vcpu area, and start writting to it periodically.

---
 drivers/kvm/irq.c      |    1 +
 drivers/kvm/kvm_main.c |    2 +
 drivers/kvm/svm.c      |    1 +
 drivers/kvm/vmx.c      |    1 +
 drivers/kvm/x86.c      |   59 ++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/kvm/x86.h      |   13 ++++++++++
 6 files changed, 77 insertions(+), 0 deletions(-)
drivers/kvm/irq.c +0 -0
index 22bfeee..0344879 100644

drivers/kvm/irq.c void kvm_vcpu_kick_request(struct kvm_vcpu *vcpu, int request) · line 96

Quoted 5 lines
drivers/kvm/irq.c +1 -0
@@ -92,6 +92,7 @@ void kvm_vcpu_kick_request(struct kvm_vcpu *vcpu, int request)
 
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
+	vcpu->time_needs_update = 1;
 

Why here and not in __vcpu_run()? It isn't timer irq related.

drivers/kvm/irq.c static long kvm_dev_ioctl(struct file *filp, · line 1247

Quoted 6 lines
drivers/kvm/irq.c +1 -0
@@ -1242,6 +1243,7 @@ static long kvm_dev_ioctl(struct file *filp,
 		case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
 		case KVM_CAP_USER_MEMORY:
 		case KVM_CAP_SET_TSS_ADDR:
+		case KVM_CAP_CLK:
 

It's just a clock source now, right? so _CLOCK_SOURCE.

drivers/kvm/irq.c static long kvm_dev_ioctl(struct file *filp,

Quoted 13 lines
drivers/kvm/irq.c +11 -0
+static void kvm_write_guest_time(struct kvm_vcpu *vcpu)
+{
+	struct timespec ts;
+	void *clock_addr;
+
+
+	if (!vcpu->clock_page)
+		return;
+
+	/* Updates version to the next odd number, indicating we're writing */
+	vcpu->hv_clock.version++;

No one can actually see this as you're updating a private structure. You need to copy it to guestspace.

drivers/kvm/irq.c

Quoted 11 lines
drivers/kvm/irq.c +10 -0
+	/* Updating the tsc count is the first thing we do */
+	kvm_get_msr(vcpu, MSR_IA32_TIME_STAMP_COUNTER, &vcpu->hv_clock.last_tsc);
+	ktime_get_ts(&ts);
+	vcpu->hv_clock.now_ns = ts.tv_nsec + (NSEC_PER_SEC * (u64)ts.tv_sec);
+	vcpu->hv_clock.wc_sec = get_seconds();
+	vcpu->hv_clock.version++;
+
+	clock_addr = vcpu->clock_addr;
+	memcpy(clock_addr, &vcpu->hv_clock, sizeof(vcpu->hv_clock));
+	mark_page_dirty(vcpu->kvm, vcpu->clock_gfn);

Just use kvm_write_guest().

Quoted 13 lines
drivers/kvm/irq.c +5 -0
+
+	vcpu->time_needs_update = 0;
+}
+
 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
 {
 	unsigned long nr, a0, a1, a2, a3, ret;
@@ -1648,7 +1674,33 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
 		a3 &= 0xFFFFFFFF;
 	}
 
+	ret = 0;
 	switch (nr) {

drivers/kvm/irq.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1688

Quoted 10 lines
drivers/kvm/irq.c +9 -0
+	case  KVM_HCALL_REGISTER_CLOCK: {
+		struct kvm_vcpu *dst_vcpu;
+
+		if (!((a1 < KVM_MAX_VCPUS) && (vcpu->kvm->vcpus[a1]))) {
+			ret = -KVM_EINVAL;
+			break;
+		}
+
+		dst_vcpu = vcpu->kvm->vcpus[a1];
 

What if !dst_vcpu? What about locking?

Suggest simply using vcpu. Every guest cpu can register its own clocksource.

drivers/kvm/irq.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1690

Quoted 2 lines
drivers/kvm/irq.c +1 -0
+		dst_vcpu->clock_page = gfn_to_page(vcpu->kvm, a0 >> PAGE_SHIFT);
 

Shift right? Why?

drivers/kvm/irq.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1693

Quoted 3 lines
drivers/kvm/irq.c +2 -0
+
+		if (!dst_vcpu->clock_page) {
 

IIRC gfn_to_page() never returns NULL, need a different check.

drivers/kvm/irq.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1700

Quoted 8 lines
drivers/kvm/irq.c +7 -0
+			ret = -KVM_EINVAL;
+			break;
+		}
+		dst_vcpu->clock_gfn = a0 >> PAGE_SHIFT;
+
+		dst_vcpu->hv_clock.tsc_mult = clocksource_khz2mult(tsc_khz, 22);
+		dst_vcpu->clock_addr = kmap(dst_vcpu->clock_page);

kmap() is bad since the page can move due to swapping. kvm_write_guest() is your friend.

drivers/kvm/irq.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1705

Quoted 6 lines
drivers/kvm/irq.c +5 -0
+static inline void release_clock(struct kvm_vcpu *vcpu)
+{
+	if (vcpu->clock_page)
+		kunmap(vcpu->clock_page);
+}

While it's a static inline, please prefix with kvm_ in case one day it isn't.

Signature · 8 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
#8 lore raw

Re: [kvm-devel] include files for kvmclock

In reply to Glauber de Oliveira Costa
Quoted 8 lines · Glauber de Oliveira Costa

include/asm-x86/kvm_para.h line 27

Quoted 8 lines
include/asm-x86/kvm_para.h +7 -0
+union kvm_hv_clock {
+       struct {
+               u64 tsc_mult;
+               u64 now_ns;
+               /* That's the wall clock, not the water closet */
+               u64 wc_sec;
+               u64 wc_nsec;

Do we really need 128-bit time? you must be planning to live forever.

Signature · 8 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
#9 lore raw

Re: [kvm-devel] include files for kvmclock

Quoted 12 lines · Avi Kivity
Quoted 9 lines · Glauber de Oliveira Costa

include/asm-x86/kvm_para.h line 27

Quoted 9 lines
include/asm-x86/kvm_para.h +7 -0
+union kvm_hv_clock {
+       struct {
+               u64 tsc_mult;
+               u64 now_ns;
+               /* That's the wall clock, not the water closet */
+               u64 wc_sec;
+               u64 wc_nsec;
Quoted 2 lines

Do we really need 128-bit time? you must be planning to live forever.

Well, he's planning on having lots of very small nanoseconds.

J

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/

#13 lore raw

Re: [kvm-devel] include files for kvmclock

In reply to Glauber de Oliveira Costa

Hi, Glauber

This is interesting facility. :-)

Quoted 8 lines
include/linux/kvm_para.h +8 -0
+#define KVM_HCALL_REGISTER_CLOCK	1
+
+union kvm_hv_clock {
+	struct {
+		u64 tsc_mult;
+		u64 now_ns;
+		/* That's the wall clock, not the water closet */
+		u64 wc_sec;

include/linux/kvm_para.h line 35

Quoted 10 lines
include/linux/kvm_para.h +10 -0
+		u64 wc_nsec;
+		u64 last_tsc;
+		/* At first, we could use the tsc value as a marker, but Jeremy
+		 * well noted that it will cause us locking problems in 32-bit
+		 * sys, so we have a special version field */
+		u32 version;
+	};
+	char page_align[PAGE_SIZE];
+};
+

Why does kvm_hv_clock need page_align?
And also the kvm_hv_clock is alloced with kvm_vcpu,
so the align is not enough, isn't it?
I thik __atribute__((__aligne__(PAGE_SIZE)))) is better than it.

Best Regards,

Akio Takebe

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/

#10 lore raw

Re: [kvm-devel] include files for kvmclock

In reply to Jeremy Fitzhardinge

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jeremy Fitzhardinge escreveu:

Quoted 12 lines
Quoted 11 lines · Avi Kivity
Quoted 9 lines · Glauber de Oliveira Costa
Quoted 9 lines
include/asm-x86/kvm_para.h +7 -0
+union kvm_hv_clock {
+       struct {
+               u64 tsc_mult;
+               u64 now_ns;
+               /* That's the wall clock, not the water closet */
+               u64 wc_sec;
+               u64 wc_nsec;
Quoted 6 lines
Quoted 2 lines

Do we really need 128-bit time? you must be planning to live forever.

Well, he's planning on having lots of very small nanoseconds.

J

The wc_nsec is legacy, and should be gone. It's not really used in current code. However, you gave me a very good idea. Living forever would be awesome! Where can I apply ?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Remi - http://enigmail.mozdev.org

iD8DBQFHMbRfjYI8LaFUWXMRAjsQAJ4vDBW0M48fMaL9sl6XfN0+Pd82egCgutwe 9rR7+H8MUQznyinlJc76kbo=
=b3wx
-----END PGP SIGNATURE-----

#5 lore raw

Re: kvmclock - the host part.

In reply to Avi Kivity

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Avi Kivity escreveu:

Quoted 21 lines
Quoted 20 lines · Glauber de Oliveira Costa

This is the host part of kvm clocksource implementation. As it does not include clockevents, it is a fairly simple implementation. We only have to register a per-vcpu area, and start writting to it periodically.

---
 drivers/kvm/irq.c      |    1 +
 drivers/kvm/kvm_main.c |    2 +
 drivers/kvm/svm.c      |    1 +
 drivers/kvm/vmx.c      |    1 +
 drivers/kvm/x86.c      |   59 ++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/kvm/x86.h      |   13 ++++++++++
 6 files changed, 77 insertions(+), 0 deletions(-)
drivers/kvm/irq.c +0 -0
index 22bfeee..0344879 100644
@@ -92,6 +92,7 @@ void kvm_vcpu_kick_request(struct kvm_vcpu *vcpu, int request)
 
Quoted 6 lines

drivers/kvm/irq.c void kvm_vcpu_kick_request(struct kvm_vcpu *vcpu, int request)

Quoted 4 lines
drivers/kvm/irq.c +1 -0
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
+	vcpu->time_needs_update = 1;
 

Why here and not in __vcpu_run()? It isn't timer irq related.

Because my plan was exactly, updating it at each timer interrupt. There's a trade off between
updating every run (hopefully more precision, but more overhead), versus updating at timer irqs, or other events.

What would you prefer?

Quoted 8 lines

drivers/kvm/irq.c static long kvm_dev_ioctl(struct file *filp, · line 1247

Quoted 6 lines
drivers/kvm/irq.c +1 -0
@@ -1242,6 +1243,7 @@ static long kvm_dev_ioctl(struct file *filp,
 		case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
 		case KVM_CAP_USER_MEMORY:
 		case KVM_CAP_SET_TSS_ADDR:
+		case KVM_CAP_CLK:
 

It's just a clock source now, right? so _CLOCK_SOURCE.

Right.

Quoted 7 lines
Quoted 7 lines
drivers/kvm/irq.c +7 -0
+static void kvm_write_guest_time(struct kvm_vcpu *vcpu)
+{
+	struct timespec ts;
+	void *clock_addr;
+
+
+	if (!vcpu->clock_page)
Quoted 8 lines

drivers/kvm/irq.c

Quoted 5 lines
drivers/kvm/irq.c +4 -0
+		return;
+
+	/* Updates version to the next odd number, indicating we're writing */
+	vcpu->hv_clock.version++;

No one can actually see this as you're updating a private structure. You need to copy it to guestspace.

That's true, I'm just copying it at the end, the whole thing. thanks.

Quoted 5 lines
Quoted 5 lines
drivers/kvm/irq.c +5 -0
+	/* Updating the tsc count is the first thing we do */
+	kvm_get_msr(vcpu, MSR_IA32_TIME_STAMP_COUNTER, &vcpu->hv_clock.last_tsc);
+	ktime_get_ts(&ts);
+	vcpu->hv_clock.now_ns = ts.tv_nsec + (NSEC_PER_SEC * (u64)ts.tv_sec);
+	vcpu->hv_clock.wc_sec = get_seconds();
Quoted 8 lines

drivers/kvm/irq.c

Quoted 6 lines
drivers/kvm/irq.c +5 -0
+	vcpu->hv_clock.version++;
+
+	clock_addr = vcpu->clock_addr;
+	memcpy(clock_addr, &vcpu->hv_clock, sizeof(vcpu->hv_clock));
+	mark_page_dirty(vcpu->kvm, vcpu->clock_gfn);

Just use kvm_write_guest().

Too slow. Updating guest time, even only in timer interrupts, was a too frequent operation, and the kmap / kunmap (atomic) at every iteration deemed the whole thing
unusable.

Quoted 26 lines
Quoted 13 lines
drivers/kvm/irq.c +5 -0
+
+	vcpu->time_needs_update = 0;
+}
+
 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
 {
 	unsigned long nr, a0, a1, a2, a3, ret;
@@ -1648,7 +1674,33 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
 		a3 &= 0xFFFFFFFF;
 	}
 
+	ret = 0;
 	switch (nr) {

drivers/kvm/irq.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1688

Quoted 10 lines
drivers/kvm/irq.c +9 -0
+	case  KVM_HCALL_REGISTER_CLOCK: {
+		struct kvm_vcpu *dst_vcpu;
+
+		if (!((a1 < KVM_MAX_VCPUS) && (vcpu->kvm->vcpus[a1]))) {
+			ret = -KVM_EINVAL;
+			break;
+		}
+
+		dst_vcpu = vcpu->kvm->vcpus[a1];
 

What if !dst_vcpu? What about locking?

Quoted 1 line

Suggest simply using vcpu. Every guest cpu can register its own

Earlier version had a check for !dst_vcpu, you are absolutely right.

Locking was not a problem in practice, because these operations are done
serialized, by the same cpu.

This hypercall is called by cpu_up, which, at least in the beginning, it's called by cpu0. And that's why each vcpu cannot register its own. (And why we don't need locking).

Well, theorectically each vcpu do can register its own clocksource, it will just be a little bit more complicated, we have to fire out an IPI, and have the other cpu to catch it, and call the hypercall.

But I honestly don't like it.
Usually, the cpu leaves start_secondary with a clock already registered, so the kernel relies on it.

Quoted 5 lines

drivers/kvm/irq.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1690

Quoted 3 lines
drivers/kvm/irq.c +1 -0
+		dst_vcpu->clock_page = gfn_to_page(vcpu->kvm, a0 >> PAGE_SHIFT);
 

Shift right? Why?

a0 is not a gfn, but a physical address.

Quoted 5 lines

drivers/kvm/irq.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1693

Quoted 3 lines
drivers/kvm/irq.c +2 -0
+
+		if (!dst_vcpu->clock_page) {
 

IIRC gfn_to_page() never returns NULL, need a different check.

You are right. I developed part of it in an older version of kvm, where reality was:

struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)

{
        struct kvm_memory_slot *slot;

        gfn = unalias_gfn(kvm, gfn);
        slot = __gfn_to_memslot(kvm, gfn);
        if (!slot)
                return NULL;
        return slot->phys_mem[gfn - slot->base_gfn];
}

Will update.

Quoted 11 lines

drivers/kvm/irq.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1700

Quoted 8 lines
drivers/kvm/irq.c +7 -0
+			ret = -KVM_EINVAL;
+			break;
+		}
+		dst_vcpu->clock_gfn = a0 >> PAGE_SHIFT;
+
+		dst_vcpu->hv_clock.tsc_mult = clocksource_khz2mult(tsc_khz, 22);
+		dst_vcpu->clock_addr = kmap(dst_vcpu->clock_page);

kmap() is bad since the page can move due to swapping. kvm_write_guest() is your friend.

Yeah , right, but again: It will be slow to the point of making the whole thing not worthy. So what alternatives do we get?

Quoted 11 lines

drivers/kvm/irq.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1705

Quoted 6 lines
drivers/kvm/irq.c +5 -0
+static inline void release_clock(struct kvm_vcpu *vcpu)
+{
+	if (vcpu->clock_page)
+		kunmap(vcpu->clock_page);
+}

While it's a static inline, please prefix with kvm_ in case one day it isn't.

Okay, will do.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Remi - http://enigmail.mozdev.org

iD8DBQFHMbjDjYI8LaFUWXMRAvcnAKCZOtPqHAxvcUkAfSaOezPWq1ib2wCg1TNz fT1rt86/j25K/6lmFsRmbI0=
=nSkW
-----END PGP SIGNATURE-----

#14 lore raw

Re: [kvm-devel] include files for kvmclock

In reply to Akio Takebe

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Akio Takebe escreveu:

Quoted 15 lines

Hi, Glauber

This is interesting facility. :-)

Quoted 11 lines
include/linux/kvm_para.h +11 -0
+#define KVM_HCALL_REGISTER_CLOCK	1
+
+union kvm_hv_clock {
+	struct {
+		u64 tsc_mult;
+		u64 now_ns;
+		/* That's the wall clock, not the water closet */
+		u64 wc_sec;
+		u64 wc_nsec;
+		u64 last_tsc;
+		/* At first, we could use the tsc value as a marker, but Jeremy
Quoted 8 lines

include/asm-x86/kvm_para.h line 36

Quoted 7 lines
include/asm-x86/kvm_para.h +7 -0
+		 * well noted that it will cause us locking problems in 32-bit
+		 * sys, so we have a special version field */
+		u32 version;
+	};
+	char page_align[PAGE_SIZE];
+};
+

Why does kvm_hv_clock need page_align?

Each vcpu will register a page on its own. In the guest side, it will be an array of pages. So, we make it page sized.

Quoted 1 line

And also the kvm_hv_clock is alloced with kvm_vcpu,

There's no requirements on the host part at all. So it doesn't really matter. In the next version, I may make it even a simple pointer.

Quoted 2 lines

so the align is not enough, isn't it?
I thik __atribute__((__aligne__(PAGE_SIZE)))) is better than it.

It deals with the start of the structure, but not with its size. See the guest part: Where it matters, I do use it.

Thanks

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Remi - http://enigmail.mozdev.org

iD8DBQFHMbqyjYI8LaFUWXMRAgfOAKCTeKF3cWbhILYSXY+MjtXo8B87EwCeNNhn z9RDYaCWHIxsqlciMF0i27w=
=EIEM
-----END PGP SIGNATURE-----

#15 lore raw

Re: [kvm-devel] include files for kvmclock

Hi,

Quoted 4 lines
Quoted 1 line

Why does kvm_hv_clock need page_align?

Each vcpu will register a page on its own. In the guest side, it will be an array of pages. So, we make it page sized.

Quoted 8 lines
Quoted 1 line

And also the kvm_hv_clock is alloced with kvm_vcpu,

There's no requirements on the host part at all. So it doesn't really matter. In the next version, I may make it even a simple pointer.

Quoted 2 lines

so the align is not enough, isn't it?
I thik __atribute__((__aligne__(PAGE_SIZE)))) is better than it.

It deals with the start of the structure, but not with its size. See the guest part: Where it matters, I do use it.

Oh, I confused the guest/host part, but I could understand it. Thank you for your explanation.

Best Regards,

Akio Takebe

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/

#6 lore raw

Re: kvmclock - the host part.

In reply to Glauber de Oliveira Costa
Quoted 9 lines · Glauber de Oliveira Costa
Quoted 7 lines

drivers/kvm/irq.c void kvm_vcpu_kick_request(struct kvm_vcpu *vcpu, int request) · line 95

Quoted 5 lines

void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu) {

drivers/kvm/irq.c +1 -0
+	vcpu->time_needs_update = 1;

Why here and not in __vcpu_run()? It isn't timer irq related.

Because my plan was exactly, updating it at each timer interrupt.

I think kvm_inject_pending_timer_irqs() is called every __vcpu_run(), so your cunning plan has been foiled.

Did you mean each guest interrupt of host interrupt?

Quoted 6 lines

There's a trade off between
updating every run (hopefully more precision, but more overhead), versus updating at timer irqs, or other events.

What would you prefer?

I think that we should update it every time a heavyweight exit has been taken. That takes care of the tradeoff quite nicely -- heavyweight exits are already dog slow.

Quoted 16 lines
Quoted 16 lines
Quoted 4 lines

drivers/kvm/x86.c int kvm_emulate_halt(struct kvm_vcpu *vcpu) · line 1652

Quoted 10 lines
drivers/kvm/x86.c +10 -0
+	/* Updating the tsc count is the first thing we do */
+	kvm_get_msr(vcpu, MSR_IA32_TIME_STAMP_COUNTER, &vcpu->hv_clock.last_tsc);
+	ktime_get_ts(&ts);
+	vcpu->hv_clock.now_ns = ts.tv_nsec + (NSEC_PER_SEC * (u64)ts.tv_sec);
+	vcpu->hv_clock.wc_sec = get_seconds();
+	vcpu->hv_clock.version++;
+
+	clock_addr = vcpu->clock_addr;
+	memcpy(clock_addr, &vcpu->hv_clock, sizeof(vcpu->hv_clock));
+	mark_page_dirty(vcpu->kvm, vcpu->clock_gfn);

Just use kvm_write_guest().

Quoted 5 lines

Too slow. Updating guest time, even only in timer interrupts, was a too frequent operation, and the kmap / kunmap (atomic) at every iteration deemed the whole thing
unusable.

kvm_write_guest() will eventually be a copy_to_user(), so you need not fear the overhead.

Quoted 16 lines
Quoted 16 lines
Quoted 4 lines
drivers/kvm/x86.c +1 -0
+	ret = 0;

drivers/kvm/x86.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1687

Quoted 10 lines
drivers/kvm/x86.c +9 -0
 	switch (nr) {
+	case  KVM_HCALL_REGISTER_CLOCK: {
+		struct kvm_vcpu *dst_vcpu;
+
+		if (!((a1 < KVM_MAX_VCPUS) && (vcpu->kvm->vcpus[a1]))) {
+			ret = -KVM_EINVAL;
+			break;
+		}
+
+		dst_vcpu = vcpu->kvm->vcpus[a1];

What if !dst_vcpu? What about locking?

Quoted 7 lines
Quoted 2 lines

Suggest simply using vcpu. Every guest cpu can register its own

Earlier version had a check for !dst_vcpu, you are absolutely right.

Locking was not a problem in practice, because these operations are done
serialized, by the same cpu.

Think evil guest that cares not for the well-being of the host.

Quoted 9 lines

This hypercall is called by cpu_up, which, at least in the beginning, it's called by cpu0. And that's why each vcpu cannot register its own. (And why we don't need locking).

Well, theorectically each vcpu do can register its own clocksource, it will just be a little bit more complicated, we have to fire out an IPI, and have the other cpu to catch it, and call the hypercall.

Can it not be done via the processor startup sequence? Then there's no need for ipis and locking.

I imagine a normal guest initializes the apic in the same way.

Quoted 5 lines

But I honestly don't like it.
Usually, the cpu leaves start_secondary with a clock already registered, so the kernel relies on it.

Quoted 7 lines
Quoted 5 lines

drivers/kvm/x86.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 1688

Quoted 3 lines
drivers/kvm/x86.c +1 -0
+		dst_vcpu->clock_page = gfn_to_page(vcpu->kvm, a0 >> PAGE_SHIFT);

Shift right? Why?

a0 is not a gfn, but a physical address.

What if the guest wants to place it in address 5GB? That's unlikely for Linux and Windows, but let's do it right anyway.

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