Skip to contentSkip to conversation

TSC scaling support for KVM v2

PATCH Superseded Joerg Roedel 1 participant 7 messages Mar 15, 2011

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

cover TSC scaling support for KVM v2

Hi,

this is version 2 of the TSC scaling patch-set. The main difference to version 1 is that the scaling factor is not switched in the leightweight exit path anymore. This is possible because production hardware will apply the scaling factor only when the CPU is in guest mode.

Beside that change I addressed the comments that came up in the review of the first version. An important change here is that I changed the usage of the adjust_tsc_offset() function and calculate all adjustment values only from the guest tsc. This change should make it suitable to the changes Zachary wants to implement.

Comments on this patches appreciated.

Thanks,

Joerg

Diffstat:

 Documentation/kvm/api.txt        |   22 ++++++++
 arch/x86/include/asm/kvm_host.h  |   10 ++++
 arch/x86/include/asm/msr-index.h |    1 +
 arch/x86/kvm/svm.c               |  110 +++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/vmx.c               |   12 ++++
 arch/x86/kvm/x86.c               |   86 ++++++++++++++++++++++++++---
 include/linux/kvm.h              |    5 ++
 7 files changed, 236 insertions(+), 10 deletions(-)

Shortlog:

Joerg Roedel (6):
KVM: SVM: Implement infrastructure for TSC_RATE_MSR
KVM: X86: Let kvm-clock report the right tsc frequency
KVM: X86: Make tsc_delta calculation a function of guest tsc
KVM: SVM: Propagate requested TSC frequency on vcpu init
KVM: X86: Delegate tsc-offset calculation to architecture code
KVM: X86: Implement userspace interface to set virtual_tsc_khz

#1 lore raw

PATCH 1/6 KVM: SVM: Implement infrastructure for TSC_RATE_MSR

This patch enhances the kvm_amd module with functions to support the TSC_RATE_MSR which can be used to set a given tsc frequency for the guest vcpu.

---
 arch/x86/include/asm/msr-index.h |    1 +
 arch/x86/kvm/svm.c               |   44 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 44 insertions(+), 1 deletions(-)
arch/x86/include/asm/msr-index.h +1 -0
index 5bfafb6..fdac548 100644
@@ -106,6 +106,7 @@
    complete list. */
 
 #define MSR_AMD64_PATCH_LEVEL		0x0000008b
+#define MSR_AMD64_TSC_RATIO		0xc0000104
 #define MSR_AMD64_NB_CFG		0xc001001f
 #define MSR_AMD64_PATCH_LOADER		0xc0010020
 #define MSR_AMD64_OSVW_ID_LENGTH	0xc0010140
arch/x86/kvm/svm.c +43 -1

80 lines not on this page · raw message

Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/6 KVM: X86: Let kvm-clock report the right tsc frequency

In reply to Joerg Roedel

This patch changes the kvm_guest_time_update function to use TSC frequency the guest actually has for updating its clock.

---
 arch/x86/include/asm/kvm_host.h |    2 ++
 arch/x86/kvm/svm.c              |    8 ++++++++
 arch/x86/kvm/vmx.c              |    6 ++++++
 arch/x86/kvm/x86.c              |   11 +++++++++--
 4 files changed, 25 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
index c8af099..a07df14 100644
@@ -588,6 +588,8 @@ struct kvm_x86_ops {
 
 	void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset);
 
+	bool (*use_virtual_tsc_khz)(struct kvm_vcpu *vcpu);
+
 	void (*get_exit_info)(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2);
 	const struct trace_print_flags *exit_reasons_str;
 };
arch/x86/kvm/svm.c +8 -0
index 72373b6..b91547a 100644
@@ -914,6 +914,13 @@ static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
 	mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
 }
 
+static bool svm_use_virtual_tsc_khz(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+
+	return svm->tsc_scale.enabled;
+}
+
 static void init_vmcb(struct vcpu_svm *svm)
 {
 	struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3996,6 +4003,7 @@ static struct kvm_x86_ops svm_x86_ops = {
 
 	.write_tsc_offset = svm_write_tsc_offset,
 	.adjust_tsc_offset = svm_adjust_tsc_offset,
+	.use_virtual_tsc_khz = svm_use_virtual_tsc_khz,
 
 	.set_tdp_cr3 = set_tdp_cr3,
 };
arch/x86/kvm/vmx.c +6 -0
index d8475a2..1425ae8 100644
@@ -1163,6 +1163,11 @@ static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
 	vmcs_write64(TSC_OFFSET, offset + adjustment);
 }
 
+static bool vmx_use_virtual_tsc_khz(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
 /*
  * Reads an msr value (of 'msr_index') into 'pdata'.
  * Returns 0 on success, non-0 otherwise.
@@ -4438,6 +4443,7 @@ static struct kvm_x86_ops vmx_x86_ops = {
 
 	.write_tsc_offset = vmx_write_tsc_offset,
 	.adjust_tsc_offset = vmx_adjust_tsc_offset,
+	.use_virtual_tsc_khz = vmx_use_virtual_tsc_khz,
 
 	.set_tdp_cr3 = vmx_set_cr3,
 };
arch/x86/kvm/x86.c +9 -2
index f1e4025..50daff2 100644
@@ -982,6 +982,14 @@ static inline int kvm_tsc_changes_freq(void)
 	return ret;
 }
 
+static u64 vcpu_tsc_khz(struct kvm_vcpu *vcpu)
+{
+	if (kvm_x86_ops->use_virtual_tsc_khz(vcpu))
+		return vcpu->kvm->arch.virtual_tsc_khz;
+	else
+		return __this_cpu_read(cpu_tsc_khz);
+}
+
 static inline u64 nsec_to_cycles(u64 nsec)
 {
 	u64 ret;
@@ -1075,8 +1083,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
 	local_irq_save(flags);
 	kvm_get_msr(v, MSR_IA32_TSC, &tsc_timestamp);
 	kernel_ns = get_kernel_ns();
-	this_tsc_khz = __this_cpu_read(cpu_tsc_khz);
-
+	this_tsc_khz = vcpu_tsc_khz(v);
 	if (unlikely(this_tsc_khz == 0)) {
 		local_irq_restore(flags);
 		kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Signature · 1 line
1.7.1
#3 lore raw

PATCH 3/6 KVM: X86: Make tsc_delta calculation a function of guest tsc

In reply to Joerg Roedel

The calculation of the tsc_delta value to ensure a forward-going tsc for the guest is a function of the host-tsc. This works as long as the guests tsc_khz is equal to the hosts tsc_khz. With tsc-scaling hardware support this is not longer true and the tsc_delta needs to be calculated using guest_tsc values.

---
 arch/x86/kvm/x86.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +7 -2
index 50daff2..fbeb5ae 100644
@@ -2127,8 +2127,13 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 	kvm_x86_ops->vcpu_load(vcpu, cpu);
 	if (unlikely(vcpu->cpu != cpu) || check_tsc_unstable()) {
 		/* Make sure TSC doesn't go backwards */
-		s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
-				native_read_tsc() - vcpu->arch.last_host_tsc;
+		s64 tsc_delta;
+		u64 tsc;
+
+		kvm_get_msr(vcpu, MSR_IA32_TSC, &tsc);
+		tsc_delta = !vcpu->arch.last_guest_tsc ? 0 :
+			     tsc - vcpu->arch.last_guest_tsc;
+
 		if (tsc_delta < 0)
 			mark_tsc_unstable("KVM discovered backwards TSC");
 		if (check_tsc_unstable()) {
Signature · 1 line
1.7.1
#4 lore raw

PATCH 4/6 KVM: SVM: Propagate requested TSC frequency on vcpu init

In reply to Joerg Roedel

This patch implements the propagation of the VM virtual_tsc_khz into each vcpu data-structure to enable the tsc-scaling feature.

---
 arch/x86/kvm/svm.c |   33 +++++++++++++++++++++++++++++++++
 1 files changed, 33 insertions(+), 0 deletions(-)
arch/x86/kvm/svm.c +33 -0
index b91547a..ae5ac88 100644
@@ -888,6 +888,36 @@ static u64 svm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
 	return _tsc;
 }
 
+static bool svm_vcpu_init_tsc(struct kvm *kvm, struct vcpu_svm *svm)
+{
+	u64 ratio;
+	u64 khz;
+
+	/* TSC scaling supported? */
+	if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR))
+		return true;
+
+	/* Guest tsc same frequency as host tsc? */
+	if (kvm->arch.virtual_tsc_khz == tsc_khz)
+		return true;
+
+	khz = kvm->arch.virtual_tsc_khz;
+
+	if (khz == 0)
+		return false;
+
+	/* TSC scaling required  - calculate ratio */
+	ratio = (u64)khz << 32;
+	do_div(ratio, tsc_khz);
+	if (ratio == 0 || ratio & TSC_RATIO_RSVD)
+		return false;
+
+	svm->tsc_scale.ratio   = ratio;
+	svm->tsc_scale.enabled = true;
+
+	return true;
+}
+
 static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
@@ -1093,6 +1123,9 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
 	if (err)
 		goto free_svm;
 
+	if (!svm_vcpu_init_tsc(kvm, svm))
+		goto uninit;
+
 	err = -ENOMEM;
 	page = alloc_page(GFP_KERNEL);
 	if (!page)
Signature · 1 line
1.7.1
#5 lore raw

PATCH 5/6 KVM: X86: Delegate tsc-offset calculation to architecture code

In reply to Joerg Roedel

With TSC scaling in SVM the tsc-offset needs to be calculated differently. This patch propagates this calculation into the architecture specific modules so that this complexity can be handled there.

---
 arch/x86/include/asm/kvm_host.h |    1 +
 arch/x86/kvm/svm.c              |   10 ++++++++++
 arch/x86/kvm/vmx.c              |    6 ++++++
 arch/x86/kvm/x86.c              |   10 +++++-----
 4 files changed, 22 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
index a07df14..16db838 100644
@@ -589,6 +589,7 @@ struct kvm_x86_ops {
 	void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset);
 
 	bool (*use_virtual_tsc_khz)(struct kvm_vcpu *vcpu);
+	u64 (*compute_tsc_offset)(struct kvm_vcpu *vcpu, u64 target_tsc);
 
 	void (*get_exit_info)(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2);
 	const struct trace_print_flags *exit_reasons_str;
arch/x86/kvm/svm.c +10 -0
index ae5ac88..b8c6d28 100644
@@ -951,6 +951,15 @@ static bool svm_use_virtual_tsc_khz(struct kvm_vcpu *vcpu)
 	return svm->tsc_scale.enabled;
 }
 
+static u64 svm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
+{
+	u64 tsc;
+
+	tsc = svm_scale_tsc(vcpu, native_read_tsc());
+
+	return target_tsc - tsc;
+}
+
 static void init_vmcb(struct vcpu_svm *svm)
 {
 	struct vmcb_control_area *control = &svm->vmcb->control;
@@ -4037,6 +4046,7 @@ static struct kvm_x86_ops svm_x86_ops = {
 	.write_tsc_offset = svm_write_tsc_offset,
 	.adjust_tsc_offset = svm_adjust_tsc_offset,
 	.use_virtual_tsc_khz = svm_use_virtual_tsc_khz,
+	.compute_tsc_offset = svm_compute_tsc_offset,
 
 	.set_tdp_cr3 = set_tdp_cr3,
 };
arch/x86/kvm/vmx.c +6 -0
index 1425ae8..9e518a3 100644
@@ -1168,6 +1168,11 @@ static bool vmx_use_virtual_tsc_khz(struct kvm_vcpu *vcpu)
 	return false;
 }
 
+static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
+{
+	return target_tsc - native_read_tsc();
+}
+
 /*
  * Reads an msr value (of 'msr_index') into 'pdata'.
  * Returns 0 on success, non-0 otherwise.
@@ -4444,6 +4449,7 @@ static struct kvm_x86_ops vmx_x86_ops = {
 	.write_tsc_offset = vmx_write_tsc_offset,
 	.adjust_tsc_offset = vmx_adjust_tsc_offset,
 	.use_virtual_tsc_khz = vmx_use_virtual_tsc_khz,
+	.compute_tsc_offset = vmx_compute_tsc_offset,
 
 	.set_tdp_cr3 = vmx_set_cr3,
 };
arch/x86/kvm/x86.c +5 -5
index fbeb5ae..aecd926 100644
@@ -990,7 +990,7 @@ static u64 vcpu_tsc_khz(struct kvm_vcpu *vcpu)
 		return __this_cpu_read(cpu_tsc_khz);
 }
 
-static inline u64 nsec_to_cycles(u64 nsec)
+static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
 {
 	u64 ret;
 
@@ -998,7 +998,7 @@ static inline u64 nsec_to_cycles(u64 nsec)
 	if (kvm_tsc_changes_freq())
 		printk_once(KERN_WARNING
 		 "kvm: unreliable cycle conversion on adjustable rate TSC\n");
-	ret = nsec * __this_cpu_read(cpu_tsc_khz);
+	ret = nsec * vcpu_tsc_khz(vcpu);
 	do_div(ret, USEC_PER_SEC);
 	return ret;
 }
@@ -1029,7 +1029,7 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, u64 data)
 	s64 sdiff;
 
 	raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
-	offset = data - native_read_tsc();
+	offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
 	ns = get_kernel_ns();
 	elapsed = ns - kvm->arch.last_tsc_nsec;
 	sdiff = data - kvm->arch.last_tsc_write;
@@ -1045,13 +1045,13 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, u64 data)
 	 * In that case, for a reliable TSC, we can match TSC offsets,
 	 * or make a best guest using elapsed value.
 	 */
-	if (sdiff < nsec_to_cycles(5ULL * NSEC_PER_SEC) &&
+	if (sdiff < nsec_to_cycles(vcpu, 5ULL * NSEC_PER_SEC) &&
 	    elapsed < 5ULL * NSEC_PER_SEC) {
 		if (!check_tsc_unstable()) {
 			offset = kvm->arch.last_tsc_offset;
 			pr_debug("kvm: matched tsc offset for %llu\n", data);
 		} else {
-			u64 delta = nsec_to_cycles(elapsed);
+			u64 delta = nsec_to_cycles(vcpu, elapsed);
 			offset += delta;
 			pr_debug("kvm: adjusted tsc offset by %llu\n", delta);
 		}
Signature · 1 line
1.7.1
#6 lore raw

PATCH 6/6 KVM: X86: Implement userspace interface to set virtual_tsc_khz

In reply to Joerg Roedel

This patch implements two new vm-ioctls to get and set the virtual_tsc_khz if the machine supports tsc-scaling. Setting the tsc-frequency is only possible before userspace creates any vcpu.

---
 Documentation/kvm/api.txt       |   22 +++++++++++++++
 arch/x86/include/asm/kvm_host.h |    7 +++++
 arch/x86/kvm/svm.c              |   15 ++++++++++
 arch/x86/kvm/x86.c              |   56 +++++++++++++++++++++++++++++++++++++++
 include/linux/kvm.h             |    5 +++
 5 files changed, 105 insertions(+), 0 deletions(-)
Documentation/kvm/api.txt +22 -0
index ad85797..adc9c23 100644
@@ -1263,6 +1263,28 @@ struct kvm_assigned_msix_entry {
 	__u16 padding[3];
 };
 
+4.54 KVM_SET_TSC_KHZ
+
+Capability: KVM_CAP_TSC_CONTROL
+Architectures: x86
+Type: vm ioctl
+Parameters: __u32 (in)
+Returns: 0 on success, -1 on error
+
+Specifies the tsc frequency for the virtual machine. This IOCTL must be
+used before any vcpu is created. The unit of the frequency is KHz.
+
+4.55 KVM_GET_TSC_KHZ
+
+Capability: KVM_CAP_GET_TSC_KHZ
+Architectures: x86
+Type: vm ioctl
+Parameters: __u32 (out)
+Returns: 0 on success, -1 on error
+
+Returns the tsc frequency of the guest. The unit of the return value is
+KHz. If the host has unstable tsc this ioctl return an error.
+
 5. The kvm_run structure
 
 Application code obtains a pointer to the kvm_run structure by
arch/x86/include/asm/kvm_host.h +7 -0
index 16db838..2471fc9 100644
@@ -630,6 +630,13 @@ u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
 
 extern bool tdp_enabled;
 
+/* control of guest tsc rate supported? */
+extern bool kvm_has_tsc_control;
+/* minimum supported tsc_khz for guests */
+extern u32  kvm_min_guest_tsc_khz;
+/* maximum supported tsc_khz for guests */
+extern u32  kvm_max_guest_tsc_khz;
+
 enum emulation_result {
 	EMULATE_DONE,       /* no further processing */
 	EMULATE_DO_MMIO,      /* kvm_run filled with mmio request */
arch/x86/kvm/svm.c +15 -0
index b8c6d28..ed7d608 100644
@@ -64,6 +64,8 @@ MODULE_LICENSE("GPL");
 #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
 
 #define TSC_RATIO_RSVD          0xffffff0000000000ULL
+#define TSC_RATIO_MIN		0x0000000000000001ULL
+#define TSC_RATIO_MAX		0x000000ffffffffffULL
 
 static bool erratum_383_found __read_mostly;
 
@@ -198,6 +200,7 @@ static int nested_svm_intercept(struct vcpu_svm *svm);
 static int nested_svm_vmexit(struct vcpu_svm *svm);
 static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
 				      bool has_error_code, u32 error_code);
+static u64 __scale_tsc(u64 ratio, u64 tsc);
 
 enum {
 	VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
@@ -799,6 +802,18 @@ static __init int svm_hardware_setup(void)
 	if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
 		kvm_enable_efer_bits(EFER_FFXSR);
 
+	if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
+		u64 min, max;
+
+		kvm_has_tsc_control = true;
+
+		min = max(1ULL,          __scale_tsc(tsc_khz, TSC_RATIO_MIN));
+		max = min(0xffffffffULL, __scale_tsc(tsc_khz, TSC_RATIO_MAX));
+
+		kvm_min_guest_tsc_khz = min;
+		kvm_max_guest_tsc_khz = max;
+	}
+
 	if (nested) {
 		printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
 		kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
arch/x86/kvm/x86.c +56 -0

80 lines not on this page · raw message

include/linux/kvm.h +5 -0
index ea2dc1a..ea16c57 100644
@@ -541,6 +541,8 @@ struct kvm_ppc_pvinfo {
 #define KVM_CAP_PPC_GET_PVINFO 57
 #define KVM_CAP_PPC_IRQ_LEVEL 58
 #define KVM_CAP_ASYNC_PF 59
+#define KVM_CAP_TSC_CONTROL 60
+#define KVM_CAP_GET_TSC_KHZ 61
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -677,6 +679,9 @@ struct kvm_clock_data {
 #define KVM_SET_PIT2              _IOW(KVMIO,  0xa0, struct kvm_pit_state2)
 /* Available with KVM_CAP_PPC_GET_PVINFO */
 #define KVM_PPC_GET_PVINFO	  _IOW(KVMIO,  0xa1, struct kvm_ppc_pvinfo)
+/* Available with KVM_CAP_TSC_CONTROL */
+#define KVM_SET_TSC_KHZ           _IOW(KVMIO,  0xa2, __u32)
+#define KVM_GET_TSC_KHZ           _IOR(KVMIO,  0xa2, __u32)
 
 /*
  * ioctls for vcpu fds
Signature · 1 line
1.7.1