Skip to contentSkip to conversation

provide disable clock functionality.

PATCH Applied Glauber Costa 3 participants 5 messages Mar 6, 2008 – Mar 8, 2008

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

PATCH 1/2 use per cpu variables instead of a vector

replace hv_clock vector in kvmclock with a percpu variable that is cacheline aligned.

---
 arch/x86/kernel/kvmclock.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kernel/kvmclock.c +5 -4
index 9c4a0f4..7c481a3 100644
@@ -20,6 +20,7 @@ #include <linux/clocksource.h>
 #include <linux/kvm_para.h>
 #include <asm/arch_hooks.h>
 #include <asm/msr.h>
+#include <linux/percpu.h>
 
 #define KVM_SCALE 22
 
@@ -33,8 +34,8 @@ static int parse_no_kvmclock(char *arg)
 early_param("no-kvmclock", parse_no_kvmclock);
 
 /* The hypervisor will put information about time periodically here */
-static struct kvm_vcpu_time_info hv_clock[NR_CPUS];
-#define get_clock(cpu, field) hv_clock[cpu].field
+static DEFINE_PER_CPU_SHARED_ALIGNED(struct kvm_vcpu_time_info, hv_clock);
+#define get_clock(cpu, field) per_cpu(hv_clock, cpu).field
 
 static inline u64 kvm_get_delta(u64 last_tsc)
 {
@@ -124,8 +125,8 @@ static int kvm_register_clock(void)
 {
 	int cpu = smp_processor_id();
 	int low, high;
-	low = (int)__pa(&hv_clock[cpu]);
-	high = ((u64)__pa(&hv_clock[cpu]) >> 32);
+	low = (int)__pa(&per_cpu(hv_clock, cpu));
+	high = ((u64)__pa(&per_cpu(hv_clock, cpu)) >> 32);
 
 	return native_write_msr_safe(MSR_KVM_SYSTEM_TIME, low, high);
 }
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/2 disable kvm clock unless addr's LSB is set.

Use LSB of the address passed through the msr to enable/disable the clock. Setting it to 1 enables it, setting it to 0 disables it.

As the guest data structures are aligned anyway, this won't be a problem, as this bit is free.

Guest is changed accordingly

---
 arch/x86/kernel/kvmclock.c |    2 +-
 arch/x86/kvm/x86.c         |    9 ++++++++-
 2 files changed, 9 insertions(+), 2 deletions(-)
arch/x86/kernel/kvmclock.c +1 -1
index 7c481a3..f654a12 100644
@@ -125,7 +125,7 @@ static int kvm_register_clock(void)
 {
 	int cpu = smp_processor_id();
 	int low, high;
-	low = (int)__pa(&per_cpu(hv_clock, cpu));
+	low = (int)__pa(&per_cpu(hv_clock, cpu)) | 1;
 	high = ((u64)__pa(&per_cpu(hv_clock, cpu)) >> 32);
 
 	return native_write_msr_safe(MSR_KVM_SYSTEM_TIME, low, high);
arch/x86/kvm/x86.c +8 -1
index 6abd784..64beff6 100644
@@ -591,8 +591,15 @@ int kvm_set_msr_common(struct kvm_vcpu *
 		if (vcpu->arch.time_page)
 			kvm_release_page_dirty(vcpu->arch.time_page);
 
+		/* we verify if the enable bit is set... */
+		if (!(data & 1)) {
+			vcpu->arch.time = NULL;
+			break;
+		}
+
 		vcpu->arch.time = data & PAGE_MASK;
-		vcpu->arch.time_offset = data & ~PAGE_MASK;
+		/* ...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);
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

Re: [PATCH 2/2] [PATCH] disable kvm clock unless addr's LSB is set.

Quoted 32 lines · On Thursday 06 March 2008 22:45:44 Glauber Costa wrote:

Use LSB of the address passed through the msr to enable/disable the clock. Setting it to 1 enables it, setting it to 0 disables it.

As the guest data structures are aligned anyway, this won't be a problem, as this bit is free.

Guest is changed accordingly

---
 arch/x86/kernel/kvmclock.c |    2 +-
 arch/x86/kvm/x86.c         |    9 ++++++++-
 2 files changed, 9 insertions(+), 2 deletions(-)
arch/x86/kernel/kvmclock.c +1 -1
index 7c481a3..f654a12 100644
@@ -125,7 +125,7 @@ static int kvm_register_clock(void)
 {
 	int cpu = smp_processor_id();
 	int low, high;
-	low = (int)__pa(&per_cpu(hv_clock, cpu));
+	low = (int)__pa(&per_cpu(hv_clock, cpu)) | 1;
 	high = ((u64)__pa(&per_cpu(hv_clock, cpu)) >> 32);
 
 	return native_write_msr_safe(MSR_KVM_SYSTEM_TIME, low, high);
arch/x86/kvm/x86.c +0 -0
index 6abd784..64beff6 100644

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

Quoted 6 lines
arch/x86/kvm/x86.c +3 -0
@@ -591,8 +591,15 @@ int kvm_set_msr_common(struct kvm_vcpu *
 		if (vcpu->arch.time_page)
 			kvm_release_page_dirty(vcpu->arch.time_page);
 
+		/* we verify if the enable bit is set... */
+		if (!(data & 1)) {
+			vcpu->arch.time = NULL;

This line made compiler complaining...

Thanks
Yang, Sheng

-------------------------------------------------------------------------
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
#4 lore raw
In reply to Glauber Costa
Quoted 4 lines · Glauber Costa

Avi,

Hope this is better

Applied, thanks.

Signature · 7 lines
Any sufficiently difficult bug is indistinguishable from a feature.


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