TSC scaling support for KVM v2
Subsystems (4)
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 | 87 +++++++++++++++++++++++++++--- include/linux/kvm.h | 5 ++ 7 files changed, 237 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
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
@@ -114,6 +114,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
Re: [PATCH 1/6] KVM: SVM: Implement infrastructure for TSC_RATE_MSR
Quoted 16 lines · Joerg Roedel
This patch enhances the
kvm_amdmodule with functions to support theTSC_RATE_MSRwhich can be used to set a given tsc frequency for the guest vcpu.arch/x86/kvm/svm.c +3
-0@@ -1141,6 +1175,9 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)for (i =0 ; i<NR_HOST_SAVE_USER_MSRS ; i++)rdmsrl (host_save_user_msrs[i], svm->host_user_msrs[i]); + +if (static_cpu_has (X86_FEATURE_TSCRATEMSR )&& svm->tsc_scale.enabled) +wrmsrl (MSR_AMD64_TSC_RATIO , svm->tsc_scale.ratio); }static void svm_vcpu_put (struct kvm_vcpu *vcpu)
arch/x86/kvm/svm.c static void svm_vcpu_put(struct kvm_vcpu *vcpu) · line 1204
Quoted 7 lines
arch/x86/kvm/svm.c +3
-0@@ -1161,6 +1198,9 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu)#endif for (i =0 ; i<NR_HOST_SAVE_USER_MSRS ; i++)wrmsrl (host_save_user_msrs[i], svm->host_user_msrs[i]); + +if (static_cpu_has (X86_FEATURE_TSCRATEMSR )&& svm->tsc_scale.enabled) +wrmsr (MSR_AMD64_TSC_RATIO ,0 ,1 ); }
This is a bit suboptimal. We could do something like
static DEFINE_PER_CPU(u64, current_tsc_scale);
static void svm_vcpu_load(...)
{
...
if (svm->tsc_scale != __get_cpu_var(current_tsc_scale)) {
__get_cpu_var(current_tsc_scale) = svm->tsc_scale;
wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_scale);
}
}with no changes to svm_vcpu_put() (note no tsc_scale.enabled either, just put the unity value for disabled).
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/6] KVM: SVM: Implement infrastructure for TSC_RATE_MSR
Quoted 38 lines · Avi Kivity
Quoted 16 lines · Joerg Roedel
This patch enhances the
kvm_amdmodule with functions to support theTSC_RATE_MSRwhich can be used to set a given tsc frequency for the guest vcpu.arch/x86/kvm/svm.c +3
-0@@ -1141,6 +1175,9 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)for (i =0 ; i<NR_HOST_SAVE_USER_MSRS ; i++)rdmsrl (host_save_user_msrs[i], svm->host_user_msrs[i]); + +if (static_cpu_has (X86_FEATURE_TSCRATEMSR )&& svm->tsc_scale.enabled) +wrmsrl (MSR_AMD64_TSC_RATIO , svm->tsc_scale.ratio); }static void svm_vcpu_put (struct kvm_vcpu *vcpu)arch/x86/kvm/svm.c static void svm_vcpu_put(struct kvm_vcpu *vcpu) · line 1204
Quoted 7 lines
arch/x86/kvm/svm.c +3
-0@@ -1161,6 +1198,9 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu)#endif for (i =0 ; i<NR_HOST_SAVE_USER_MSRS ; i++)wrmsrl (host_save_user_msrs[i], svm->host_user_msrs[i]); + +if (static_cpu_has (X86_FEATURE_TSCRATEMSR )&& svm->tsc_scale.enabled) +wrmsr (MSR_AMD64_TSC_RATIO ,0 ,1 ); }This is a bit suboptimal. We could do something like
static
DEFINE_PER_CPU(u64, current_tsc_scale);static void svm_vcpu_load(...) { ... if (svm->tsc_scale != __get_cpu_var(current_tsc_scale)) { __get_cpu_var(current_tsc_scale) = svm->tsc_scale; wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_scale); } }
Quoted 2 lines
with no changes to
svm_vcpu_put()(note no tsc_scale.enabled either, just put the unity value for disabled).
Right, this is a good optimization. Thanks for the hint, I'll change it.
Regards,
Joerg
PATCH 2/6 KVM: X86: Let kvm-clock report the right tsc frequency
In reply to Joerg RoedelThis 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
@@ -590,6 +590,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
@@ -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
@@ -1174,6 +1174,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.
@@ -4498,6 +4503,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
@@ -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
Re: [PATCH 2/6] KVM: X86: Let kvm-clock report the right tsc frequency
arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 593
Quoted 6 lines · Joerg Roedel
This patch changes the
kvm_guest_time_updatefunction to use TSC frequency the guest actually has for updating its clock.arch/x86/include/asm/kvm_host.h +1
-0+bool (*use_virtual_tsc_khz)(struct kvm_vcpu *vcpu);
Just put virtual_tsc_khz into vcpu->arch. If nonzero, consider it enabled.
(we could even set it if we have a constant_tsc; to the real tsc frequency).
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH 3/6 KVM: X86: Make tsc_delta calculation a function of guest tsc
In reply to Joerg RoedelThe 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
@@ -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
PATCH 4/6 KVM: SVM: Propagate requested TSC frequency on vcpu init
In reply to Joerg RoedelThis 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
@@ -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
Re: [PATCH 4/6] KVM: SVM: Propagate requested TSC frequency on vcpu init
arch/x86/kvm/svm.c static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) · line 1128
Quoted 13 lines · Joerg Roedel
This patch implements the propagation of the VM
virtual_tsc_khzinto each vcpu data-structure to enable the tsc-scaling feature.
static voidsvm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset){
structvcpu_svm*svm =to_svm(vcpu);arch/x86/kvm/svm.c +3
-0@@ -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; +
Need to set err.
I'm not really happy about failing on vcpu creation. Prefer to fail when setting the frequency.
Another issue is that we fail when the frequency is out of range, but not when it's wierd enough so we lose a lot of accuracy. I guess it won't matter in practice.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 4/6] KVM: SVM: Propagate requested TSC frequency on vcpu init
Quoted 11 lines · Avi Kivity
Quoted 10 lines · Joerg Roedel
This patch implements the propagation of the VM
virtual_tsc_khzinto each vcpu data-structure to enable the tsc-scaling feature.
static voidsvm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset){
structvcpu_svm*svm =to_svm(vcpu);arch/x86/kvm/svm.c +0
-0@@ -1093,6 +1123,9 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)if (err) goto free_svm;
Quoted 8 lines
arch/x86/kvm/svm.c static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) · line 1128
Quoted 3 lines
arch/x86/kvm/svm.c +3
-0+if (!svm_vcpu_init_tsc (kvm, svm)) + goto uninit; +Need to set err.
I'm not really happy about failing on vcpu creation. Prefer to fail when setting the frequency.
Yes, in theory this shouldn't happen anyway because wrong frequencies are catched when setting them via the ioctl. This is just another check, but it should probably be turned into a BUG_ON in svm_vcpu_init_tsc() because it is a bug that userspace could set an invalid khz value.
Joerg
Re: [PATCH 4/6] KVM: SVM: Propagate requested TSC frequency on vcpu init
Quoted 18 lines · Joerg Roedel
Quoted 17 lines · Avi Kivity
Quoted 6 lines · Joerg Roedel
This patch implements the propagation of the VM
virtual_tsc_khzinto each vcpu data-structure to enable the tsc-scaling feature.
static voidsvm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset){ struct vcpu_svm *svm = to_svm(vcpu);Quoted 7 lines
@@ -1093,6 +1123,9 @@ static struct
kvm_vcpu*svm_create_vcpu(struct kvm *kvm, unsigned int id)
if (err)
gotofree_svm;+ if (!svm_vcpu_init_tsc(kvm, svm))
+ goto uninit;
+Need to set err.
Quoted 7 lines
Quoted 2 lines
I'm not really happy about failing on vcpu creation. Prefer to fail when setting the frequency.
Yes, in theory this shouldn't happen anyway because wrong frequencies are catched when setting them via the ioctl. This is just another check, but it should probably be turned into a
BUG_ONinsvm_vcpu_init_tsc()because it is a bug that userspace could set an invalid khz value.
Yes, I saw later. But BUG_ON() is too dangerous - if the checks don't exactly match a malicious user could cause a DOS.
We can just avoid setting the scale and run with the host tsc frequency.
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH 5/6 KVM: X86: Delegate tsc-offset calculation to architecture code
In reply to Joerg RoedelWith 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
@@ -591,6 +591,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
@@ -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
@@ -1179,6 +1179,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.
@@ -4504,6 +4509,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
@@ -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
PATCH 6/6 KVM: X86: Implement userspace interface to set virtual_tsc_khz
In reply to Joerg RoedelThis 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 | 57 +++++++++++++++++++++++++++++++++++++++ include/linux/kvm.h | 5 +++ 5 files changed, 106 insertions(+), 0 deletions(-)
Documentation/kvm/api.txt +22 -0
@@ -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
@@ -632,6 +632,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
@@ -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 0x000000ffffffffffULLstatic 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 +57 -0
81 lines not on this page · raw message
include/linux/kvm.h +5 -0
@@ -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
Re: [PATCH 6/6] KVM: X86: Implement userspace interface to set virtual_tsc_khz
Quoted 7 lines · Joerg Roedel
This patch implements two new vm-ioctls to get and set the
virtual_tsc_khzif the machine supports tsc-scaling. Setting the tsc-frequency is only possible before userspace creates any vcpu.+4.54
KVM_SET_TSC_KHZ
Documentation/kvm/api.txt struct kvm_assigned_msix_entry { · line 1275
Quoted 9 lines
Documentation/kvm/api.txt +9
-0+ +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.
Should it not be a vcpu ioctl?
KVM_CAP_TSC_CONTROL depends on both kvm and the underlying cpu. I guess it's okay.
Need to return an error if the frequency cannot be accommodated. In theory we need to provide the range of supported frequencies, but we can live without it (and it will be very difficult to provide if we take accuracy into account).
Documentation/kvm/api.txt struct kvm_assigned_msix_entry { · line 1286
Quoted 11 lines
Documentation/kvm/api.txt +11
-0+ +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.
Which error?
(it's important since this is an expected error, unlike most others)
arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp, · line 3602
Quoted 13 lines
arch/x86/kvm/x86.c +9
-0@@ -3580,6 +3591,52 @@ long kvm_arch_vm_ioctl(struct file *filp, r =0 ;break ; } +case KVM_SET_TSC_KHZ : { +u32 user_tsc_khz; + +if (!kvm_has_tsc_control) +break ; + + r = -EFAULT ; +if (copy_from_user (&user_tsc_khz, argp,sizeof (__u32))) + goto out;
Can just use the input value (arg) instead of copy_from_user().
arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp,
Quoted 4 lines
arch/x86/kvm/x86.c +4
-0+ + r = -EINVAL ; +if (user_tsc_khz< kvm_min_guest_tsc_khz || + user_tsc_khz> kvm_max_guest_tsc_khz)
<= and >= are probably safer.
Quoted 5 lines
arch/x86/kvm/x86.c +5
-0+ goto out; + +mutex_lock (&kvm->lock); +/* +* We force the tsc frequency to be set before any
arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp, · line 3621
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+* vcpu is created +*/ +if (atomic_read (&kvm->online_vcpus)>0 ) { +mutex_unlock (&kvm->lock); + goto out; + } + +kvm_arch_set_tsc_khz (kvm, user_tsc_khz); + +mutex_unlock (&kvm->lock);
Making these vcpu ioctls will remove the locking.
Quoted 4 lines
arch/x86/kvm/x86.c +4
-0+ + r =0 ; + goto out; + }
arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp, · line 3635
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+case KVM_GET_TSC_KHZ : { +u32 vtsc_khz = kvm->arch.virtual_tsc_khz; + + r = -EIO ; +if (check_tsc_unstable ()) + goto out; + + r = -EFAULT ; +if (copy_to_user (argp,&vtsc_khz,sizeof (__u32))) + goto out;
And an ordinary return here.
Quoted 5 lines
arch/x86/kvm/x86.c +4
-0+ + r =0 ; + goto out; + }
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 6/6] KVM: X86: Implement userspace interface to set virtual_tsc_khz
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Joerg Roedel
This patch implements two new vm-ioctls to get and set the
virtual_tsc_khzif the machine supports tsc-scaling. Setting the tsc-frequency is only possible before userspace creates any vcpu.
Quoted 12 lines
Documentation/kvm/api.txt struct kvm_assigned_msix_entry { · line 1275
Quoted 10 lines
+4.54
KVM_SET_TSC_KHZDocumentation/kvm/api.txt +9
-0+ +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.Should it not be a vcpu ioctl?
The idea was that a vm ioctl will make sure that all vcpus in one vm have the same tsc frequency. With a vm ioctl we force this. So the tsc fequency is basically a vm capability which is mirrored into each vcpu data structure for performance reasons.
Quoted 4 lines
Need to return an error if the frequency cannot be accommodated. In theory we need to provide the range of supported frequencies, but we can live without it (and it will be very difficult to provide if we take accuracy into account).
Yes, -EINVAL is reported if the frequency can not be accomodated. But userspace can't find out the lower or upper bounds of the valid range.
Quoted 33 lines
Documentation/kvm/api.txt struct kvm_assigned_msix_entry { · line 1286
Quoted 12 lines
Documentation/kvm/api.txt +11
-0+ +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.Which error?
(it's important since this is an expected error, unlike most others)
arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp, · line 3602
Quoted 13 lines
arch/x86/kvm/x86.c +9
-0@@ -3580,6 +3591,52 @@ long kvm_arch_vm_ioctl(struct file *filp, r =0 ;break ; } +case KVM_SET_TSC_KHZ : { +u32 user_tsc_khz; + +if (!kvm_has_tsc_control) +break ; + + r = -EFAULT ; +if (copy_from_user (&user_tsc_khz, argp,sizeof (__u32))) + goto out;Can just use the input value (arg) instead of
copy_from_user().
Quoted 6 lines
arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp,
Quoted 4 lines
arch/x86/kvm/x86.c +4
-0+ + r = -EINVAL ; +if (user_tsc_khz< kvm_min_guest_tsc_khz || + user_tsc_khz> kvm_max_guest_tsc_khz)<= and >= are probably safer.
Right, I'll change it.
Quoted 19 lines
Quoted 6 lines
arch/x86/kvm/x86.c +5
-0+ goto out; + +mutex_lock (&kvm->lock); +/* +* We force the tsc frequency to be set before any arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp, · line 3621
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+* vcpu is created +*/ +if (atomic_read (&kvm->online_vcpus)>0 ) { +mutex_unlock (&kvm->lock); + goto out; + } + +kvm_arch_set_tsc_khz (kvm, user_tsc_khz); + +mutex_unlock (&kvm->lock);Making these vcpu ioctls will remove the locking.
Quoted 16 lines
Quoted 4 lines
arch/x86/kvm/x86.c +4
-0+ + r =0 ; + goto out; + }arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp, · line 3635
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+case KVM_GET_TSC_KHZ : { +u32 vtsc_khz = kvm->arch.virtual_tsc_khz; + + r = -EIO ; +if (check_tsc_unstable ()) + goto out; + + r = -EFAULT ; +if (copy_to_user (argp,&vtsc_khz,sizeof (__u32))) + goto out;And an ordinary return here.
Okay, I'll change that. But I would prefer to keep this as a vm ioctl. A vcpu ioctl might be more flexible but I doubt anybody has a use-case for different tsc_khz values in one VM.
Joerg
Re: [PATCH 6/6] KVM: X86: Implement userspace interface to set virtual_tsc_khz
Quoted 12 lines · Joerg Roedel
Quoted 12 lines
Quoted 11 lines
+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.
Quoted 7 lines
Quoted 1 line
Should it not be a vcpu ioctl?
The idea was that a vm ioctl will make sure that all vcpus in one vm have the same tsc frequency. With a vm ioctl we force this. So the tsc fequency is basically a vm capability which is mirrored into each vcpu data structure for performance reasons.
Yes - it doesn't make sense to have each vcpu run with a different tsc. But we do the same with cpuid, and since the tsc really is a per-cpu thing, then if it simplifies the code I think it's okay to make userspace call it per-cpu.
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
+
+ r = 0;
+ goto out;
+ }
+ caseKVM_GET_TSC_KHZ: {
+ u32vtsc_khz=kvm->arch.virtual_tsc_khz;
+
+ r =-EIO;
+ if (check_tsc_unstable())
+ goto out;
+
+ r =-EFAULT;
+ if (copy_to_user(argp,&vtsc_khz, sizeof(__u32)))
+ goto out;
Quoted 5 lines
Quoted 1 line
And an ordinary return here.
Okay, I'll change that. But I would prefer to keep this as a vm ioctl. A vcpu ioctl might be more flexible but I doubt anybody has a use-case for different
tsc_khzvalues in one VM.
My motivation is simplification.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 6/6] KVM: X86: Implement userspace interface to set virtual_tsc_khz
Quoted 6 lines · Avi Kivity
Quoted 3 lines · Joerg Roedel
Okay, I'll change that. But I would prefer to keep this as a vm ioctl. A vcpu ioctl might be more flexible but I doubt anybody has a use-case for different
tsc_khzvalues in one VM.My motivation is simplification.
Okay, so I'll change that too. Thanks for your feedback.
Joerg