Skip to contentSkip to conversation

kvmclock fixes

PATCH Applied Glauber Costa 2 participants 12 messages Mar 5, 2008 – Mar 6, 2008

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

cover kvmclock fixes

Hi,

With this series, I'm introducing the ability to turn off the kvmclock. It is done by writting any thing to the first three bits of the address passed as a parameter to the msr.

For that to work properly, we should make absolutely sure the guest is aligned. If we really care about compatibility here, we can set the KVM_CAP_CLOCKSOURCE to 0, and define a new feature flag to mean that the clock data structures should be aligned. However, as the guest part has not yet made its way to linus, we're in a privileged position of having no users other than the experimental users, and can change it withouth bothering so much.

/* Comments? */

-------------------------------------------------------------------------
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/

#1 lore raw

PATCH 1/4 kvmclock: release time_page if msr is rewritten

If the calling cpu rewrites the system clock msr for any reason, release the page we allocated in the last time

---
 arch/x86/kvm/x86.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +3 -0
index e65a9d6..6abd784 100644
@@ -588,6 +588,9 @@ int kvm_set_msr_common(struct kvm_vcpu *
 		kvm_write_wall_clock(vcpu->kvm, data);
 		break;
 	case MSR_KVM_SYSTEM_TIME: {
+		if (vcpu->arch.time_page)
+			kvm_release_page_dirty(vcpu->arch.time_page);
+
 		vcpu->arch.time = data & PAGE_MASK;
 		vcpu->arch.time_offset = data & ~PAGE_MASK;
 
Signature · 7 lines
1.4.2


-------------------------------------------------------------------------
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/
#2 lore raw

PATCH 2/4 cacheline-align kvmclock structures

Align the kvm_vcpu_time array to the size of a cacheline.

---
 arch/x86/kernel/kvmclock.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/kvmclock.c +1 -1
index b8da3bf..d82406a 100644
@@ -36,7 +36,7 @@ early_param("no-kvmclock", parse_no_kvmc
 struct shared_info shared_info __attribute__((__aligned__(PAGE_SIZE)));
 
 /* The hypervisor will put information about time periodically here */
-static struct kvm_vcpu_time_info hv_clock[NR_CPUS];
+static struct kvm_vcpu_time_info hv_clock[NR_CPUS] __cacheline_aligned;
 #define get_clock(cpu, field) hv_clock[cpu].field
 
 static inline u64 kvm_get_delta(u64 last_tsc)
Signature · 7 lines
1.4.2


-------------------------------------------------------------------------
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/
#3 lore raw

PATCH 3/4 kvmclock: allow it to be turned off

Use the lower 3 lower bits of the system time msr to turn off the clock. This means that all clock registration has to be aligned in a 4-byte boundary

---
 arch/x86/kvm/x86.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +5 -0
index 6abd784..7ce14ce 100644
@@ -591,6 +591,11 @@ int kvm_set_msr_common(struct kvm_vcpu *
 		if (vcpu->arch.time_page)
 			kvm_release_page_dirty(vcpu->arch.time_page);
 
+		/* 4-byte unaligned accesses are invalid */
+		if (data & 0x7) {
+			vcpu->arch.time_page = NULL;
+			break;
+		}
 		vcpu->arch.time = data & PAGE_MASK;
 		vcpu->arch.time_offset = data & ~PAGE_MASK;
 
Signature · 7 lines
1.4.2


-------------------------------------------------------------------------
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/
#4 lore raw

PATCH 4/4 cleanup leftovers

clean this leftover in kvmclock.c

---
 arch/x86/kernel/kvmclock.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
arch/x86/kernel/kvmclock.c +0 -3
index d82406a..8ff12b6 100644
@@ -20,7 +20,6 @@ #include <linux/clocksource.h>
 #include <linux/kvm_para.h>
 #include <asm/arch_hooks.h>
 #include <asm/msr.h>
-#include <xen/interface/xen.h>
 
 #define KVM_SCALE 22
 
@@ -33,8 +32,6 @@ static int parse_no_kvmclock(char *arg)
 }
 early_param("no-kvmclock", parse_no_kvmclock);
 
-struct shared_info shared_info __attribute__((__aligned__(PAGE_SIZE)));
-
 /* The hypervisor will put information about time periodically here */
 static struct kvm_vcpu_time_info hv_clock[NR_CPUS] __cacheline_aligned;
 #define get_clock(cpu, field) hv_clock[cpu].field
Signature · 7 lines
1.4.2


-------------------------------------------------------------------------
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/
Applied
#5 lore raw

Re: [PATCH 4/4] [PATCH] cleanup leftovers

Quoted 3 lines · Glauber Costa

clean this leftover in kvmclock.c

Applied, thanks.

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


-------------------------------------------------------------------------
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: [PATCH 3/4] [PATCH] kvmclock: allow it to be turned off

In reply to Glauber Costa
Quoted 4 lines · Glauber Costa

Use the lower 3 lower bits of the system time msr to turn off the clock. This means that all clock registration has to be aligned in a 4-byte boundary

3 bits -> 8 bytes.

How about just using just bit 0 as an enable bit (not a disable bit). That means the default value of zero means the clock is disabled, and that we have a couple of more bits to enable future features.

Quoted 22 lines
---
 arch/x86/kvm/x86.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +5 -0
index 6abd784..7ce14ce 100644
@@ -591,6 +591,11 @@ int kvm_set_msr_common(struct kvm_vcpu *
 		if (vcpu->arch.time_page)
 			kvm_release_page_dirty(vcpu->arch.time_page);
 
+		/* 4-byte unaligned accesses are invalid */
+		if (data & 0x7) {
+			vcpu->arch.time_page = NULL;
+			break;
+		}
 		vcpu->arch.time = data & PAGE_MASK;
 		vcpu->arch.time_offset = data & ~PAGE_MASK;
 
Signature · 7 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic.


-------------------------------------------------------------------------
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/
#7 lore raw

Re: [PATCH 3/4] [PATCH] kvmclock: allow it to be turned off

Quoted 8 lines · Avi Kivity
Quoted 5 lines · Glauber Costa

Use the lower 3 lower bits of the system time msr to turn off the clock. This means that all clock registration has to be aligned in a 4-byte boundary

3 bits -> 8 bytes.

dohh!! true
/me ashamed.

Quoted 3 lines

How about just using just bit 0 as an enable bit (not a disable bit). That means the default value of zero means the clock is disabled, and that we have a couple of more bits to enable future features.

Apart from the fact that it will break every single guest out there, that's ok. As I said: these things are so early, that maybe we can pay this price. Your call.

Quoted 13 lines
Quoted 13 lines
---
 arch/x86/kvm/x86.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 6abd784..7ce14ce 100644
@@ -591,6 +591,11 @@ int kvm_set_msr_common(struct kvm_vcpu *
         if (vcpu->arch.time_page)
             kvm_release_page_dirty(vcpu->arch.time_page);
 
Quoted 11 lines

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu * · line 601

Quoted 9 lines
arch/x86/kvm/x86.c +5 -0
+        /* 4-byte unaligned accesses are invalid */
+        if (data & 0x7) {
+            vcpu->arch.time_page = NULL;
+            break;
+        }
         vcpu->arch.time = data & PAGE_MASK;
         vcpu->arch.time_offset = data & ~PAGE_MASK;
 

-------------------------------------------------------------------------
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/

#8 lore raw

Re: [PATCH 3/4] [PATCH] kvmclock: allow it to be turned off

Quoted 4 lines · Glauber Costa

Apart from the fact that it will break every single guest out there, that's ok. As I said: these things are so early, that maybe we can pay this price. Your call.

Which guests? kvmclock is only in kvm.git, and I don't think any distro is based on that.

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


-------------------------------------------------------------------------
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/
#9 lore raw

Re: [PATCH 3/4] [PATCH] kvmclock: allow it to be turned off

Quoted 9 lines · Avi Kivity
Quoted 4 lines · Glauber Costa

Apart from the fact that it will break every single guest out there, that's ok. As I said: these things are so early, that maybe we can pay this price. Your call.

Which guests? kvmclock is only in kvm.git, and I don't think any distro is based on that.

My guests at home? ;-)

That's exactly what I meant by "early". Just wanted to make sure you're all ok with that.

-------------------------------------------------------------------------
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/

#10 lore raw

Re: [PATCH 2/4] [PATCH] cacheline-align kvmclock structures

In reply to Glauber Costa
Quoted 13 lines · Glauber Costa

Align the kvm_vcpu_time array to the size of a cacheline.

---
 arch/x86/kernel/kvmclock.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/kvmclock.c +0 -0
index b8da3bf..d82406a 100644

arch/x86/kernel/kvmclock.c early_param("no-kvmclock", parse_no_kvmc · line 42

Quoted 8 lines
arch/x86/kernel/kvmclock.c +1 -1
@@ -36,7 +36,7 @@ early_param("no-kvmclock", parse_no_kvmc
 struct shared_info shared_info __attribute__((__aligned__(PAGE_SIZE)));
 
 /* The hypervisor will put information about time periodically here */
-static struct kvm_vcpu_time_info hv_clock[NR_CPUS];
+static struct kvm_vcpu_time_info hv_clock[NR_CPUS] __cacheline_aligned;
 #define get_clock(cpu, field) hv_clock[cpu].field
 
 static inline u64 kvm_get_delta(u64 last_tsc)

I think this will align only the array itself, not members, so any write will (and the following reads) will cause a cacheline bounce.

Switching to per_cpu() both clarifies the intent and fixes the issue. Still need the 8-byte alignment. Might be best to stick that on the structure declaration, so it applies to all guests.

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


-------------------------------------------------------------------------
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/
Applied
#11 lore raw

Re: [PATCH 1/4] [PATCH] kvmclock: release time_page if msr is rewritten

In reply to Glauber Costa
Quoted 3 lines · Glauber Costa

If the calling cpu rewrites the system clock msr for any reason, release the page we allocated in the last time

Applied, thanks.

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


-------------------------------------------------------------------------
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/