KVM: Implement nested TSC scaling
v2 cover KVM: Implement nested TSC scaling
KVM currently supports hardware-assisted TSC scaling but only for L1; the feature is not exposed to nested guests. This patch series adds support for nested TSC scaling and allows both L1 and L2 to be scaled with different scaling factors. That is achieved by "merging" the 01 and 02 values together.
Most of the logic in this series is implemented in common code (by doing the necessary restructurings), however the patches add support for VMX only. Adding support for SVM should be easy at this point and Maxim Levitsky has volunteered to do this (thanks!).
Changelog:
v2:
- Applied most (all?) of Maxim's feedback
- Added a mul_s64_u64_shr function in math64.h
- Added a separate kvm_scale_tsc_l1 function instead of passing an argument to kvm_scale_tsc
- Implemented the 02 fields calculations in common code
- Moved all of write_l1_tsc_offset's logic to common code
- Added a check for whether the TSC is stable in patch 10
- Used a random L1 factor and a negative offset in patch 10
Ilias Stamatis (10):
math64.h: Add mul_s64_u64_shr()
KVM: X86: Store L1's TSC scaling ratio in 'struct kvm_vcpu_arch'
KVM: X86: Add kvm_scale_tsc_l1() and kvm_compute_tsc_offset_l1()
KVM: VMX: Add a TSC multiplier field in VMCS12
KVM: X86: Add functions for retrieving L2 TSC fields from common code
KVM: X86: Add functions that calculate the 02 TSC offset and
multiplier
KVM: X86: Move write_l1_tsc_offset() logic to common code and rename
it
KVM: VMX: Set the TSC offset and multiplier on nested entry and exit
KVM: VMX: Expose TSC scaling to L2
KVM: selftests: x86: Add vmx_nested_tsc_scaling_test
arch/x86/include/asm/kvm-x86-ops.h | 4 +- arch/x86/include/asm/kvm_host.h | 13 +- arch/x86/kvm/svm/svm.c | 29 ++- arch/x86/kvm/vmx/nested.c | 19 +- arch/x86/kvm/vmx/vmcs12.c | 1 + arch/x86/kvm/vmx/vmcs12.h | 4 +- arch/x86/kvm/vmx/vmx.c | 42 +-- arch/x86/kvm/x86.c | 93 +++++-- include/linux/math64.h | 19 ++ tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/vmx_nested_tsc_scaling_test.c | 242 ++++++++++++++++++ 12 files changed, 409 insertions(+), 59 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/vmx_nested_tsc_scaling_test.c
Signature · 1 line
2.17.1
PATCH v2 1/10 math64.h: Add mul_s64_u64_shr()
This function is needed for the nested TSC scaling code where we need to multiply the signed TSC offset with the unsigned TSC multiplier.
--- include/linux/math64.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
include/linux/math64.h +19 -0
@@ -3,6 +3,7 @@#define _LINUX_MATH64_H#include <linux/types.h> +#include <linux/math.h> #include <vdso/math64.h> #include <asm/div64.h>
@@ -234,6 +235,24 @@ static inline u64 mul_u64_u64_shr(u64 a, u64 b, unsigned int shift)#endif +#ifndef mul_s64_u64_shr +static inline u64 mul_s64_u64_shr (s64 a,u64 b,unsigned int shift) +{ +u64 ret; + +/* +* Extract the sign before the multiplication and put it back +* afterwards if needed. +*/ + ret =mul_u64_u64_shr (abs (a), b, shift); + +if (a <0 ) + ret = -((s64 ) ret); + +return ret; +} +#endif /* mul_s64_u64_shr */ +#ifndef mul_u64_u32_divstatic inline u64 mul_u64_u32_div (u64 a,u32 mul,u32 divisor) {
Signature · 1 line
2.17.1
PATCH v2 2/10 KVM: X86: Store L1's TSC scaling ratio in 'struct kvm_vcpu_arch'
In reply to Ilias StamatisStore L1's scaling ratio in that struct like we already do for L1's TSC offset. This allows for easy save/restore when we enter and then exit the nested guest.
--- arch/x86/include/asm/kvm_host.h | 5 +++-- arch/x86/kvm/x86.c | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -2
@@ -707,7 +707,7 @@ struct kvm_vcpu_arch { } st;u64 l1_tsc_offset; -u64 tsc_offset; +u64 tsc_offset;/* current tsc offset */ u64 last_guest_tsc;u64 last_host_tsc;u64 tsc_offset_adjustment;
@@ -721,7 +721,8 @@ struct kvm_vcpu_arch {u32 virtual_tsc_khz;s64 ia32_tsc_adjust_msr;u64 msr_ia32_power_ctl; -u64 tsc_scaling_ratio; +u64 l1_tsc_scaling_ratio; +u64 tsc_scaling_ratio;/* current scaling ratio */ atomic_t nmi_queued;/* unprocessed asynchronous NMIs */ unsigned nmi_pending;/* NMI queued after currently running handler */
arch/x86/kvm/x86.c +4 -2
@@ -2185,6 +2185,7 @@ static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)/* Guest TSC same frequency as host TSC? */ if (!scale) { + vcpu->arch.l1_tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;return 0 ; }
@@ -2211,7 +2212,7 @@ static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)return -1 ; } - vcpu->arch.tsc_scaling_ratio = ratio; + vcpu->arch.l1_tsc_scaling_ratio = vcpu->arch.tsc_scaling_ratio = ratio;return 0 ; }
@@ -2223,6 +2224,7 @@ static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)/* tsc_khz can be zero if TSC calibration fails */ if (user_tsc_khz ==0 ) {/* set tsc_scaling_ratio to a safe value */ + vcpu->arch.l1_tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;return -1 ; }
@@ -2459,7 +2461,7 @@ static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,static inline void adjust_tsc_offset_host (struct kvm_vcpu *vcpu,s64 adjustment) { -if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio) +if (vcpu->arch.l1_tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)WARN_ON (adjustment <0 ); adjustment =kvm_scale_tsc (vcpu, (u64 ) adjustment);adjust_tsc_offset_guest (vcpu, adjustment);
Signature · 1 line
2.17.1
PATCH v2 5/10 KVM: X86: Add functions for retrieving L2 TSC fields from common code
In reply to Ilias StamatisIn order to implement as much of the nested TSC scaling logic as possible in common code, we need these vendor callbacks for retrieving the TSC offset and the TSC multiplier that L1 has set for L2.
--- arch/x86/include/asm/kvm-x86-ops.h | 2 ++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/svm/svm.c | 14 ++++++++++++++ arch/x86/kvm/vmx/vmx.c | 25 +++++++++++++++++++++++++ 4 files changed, 43 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +2 -0
@@ -87,6 +87,8 @@ KVM_X86_OP(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) KVM_X86_OP_NULL(has_wbinvd_exit) +KVM_X86_OP(get_l2_tsc_offset) +KVM_X86_OP(get_l2_tsc_multiplier) KVM_X86_OP(write_l1_tsc_offset) KVM_X86_OP(get_exit_info) KVM_X86_OP(check_intercept)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1305,6 +1305,8 @@ struct kvm_x86_ops {bool (*has_wbinvd_exit)(void ); +u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu); +u64 (*get_l2_tsc_multiplier)(struct kvm_vcpu *vcpu);/* Returns actual tsc_offset set in active VMCS */ u64 (*write_l1_tsc_offset)(struct kvm_vcpu *vcpu,u64 offset);
arch/x86/kvm/svm/svm.c +14 -0
@@ -1082,6 +1082,18 @@ static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) seg->base =0 ; } +static u64 svm_get_l2_tsc_offset (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +return svm->nested.ctl.tsc_offset; +} + +static u64 svm_get_l2_tsc_multiplier (struct kvm_vcpu *vcpu) +{ +return kvm_default_tsc_scaling_ratio; +} +static u64 svm_write_l1_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) {struct vcpu_svm *svm =to_svm (vcpu);
@@ -4526,6 +4538,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .has_wbinvd_exit = svm_has_wbinvd_exit, + .get_l2_tsc_offset = svm_get_l2_tsc_offset, + .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier, .write_l1_tsc_offset = svm_write_l1_tsc_offset, .load_mmu_pgd = svm_load_mmu_pgd,
arch/x86/kvm/vmx/vmx.c +25 -0
@@ -1787,6 +1787,29 @@ static void setup_msrs(struct vcpu_vmx *vmx) vmx->guest_uret_msrs_loaded = false; } +static u64 vmx_get_l2_tsc_offset (struct kvm_vcpu *vcpu) +{ +struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); +u64 offset =0 ; + +if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING ) + offset = vmcs12->tsc_offset; + +return offset; +} + +static u64 vmx_get_l2_tsc_multiplier (struct kvm_vcpu *vcpu) +{ +struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); +u64 multiplier = kvm_default_tsc_scaling_ratio; + +if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING && + vmcs12->secondary_vm_exec_control &SECONDARY_EXEC_TSC_SCALING ) + multiplier = vmcs12->tsc_multiplier; + +return multiplier; +} +static u64 vmx_write_l1_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) {struct vmcs12 *vmcs12 =get_vmcs12 (vcpu);
@@ -7700,6 +7723,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, + .get_l2_tsc_offset = vmx_get_l2_tsc_offset, + .get_l2_tsc_multiplier = vmx_get_l2_tsc_multiplier, .write_l1_tsc_offset = vmx_write_l1_tsc_offset, .load_mmu_pgd = vmx_load_mmu_pgd,
Signature · 1 line
2.17.1
PATCH v2 4/10 KVM: VMX: Add a TSC multiplier field in VMCS12
In reply to Ilias StamatisThis is required for supporting nested TSC scaling.
--- arch/x86/kvm/vmx/vmcs12.c | 1 + arch/x86/kvm/vmx/vmcs12.h | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmcs12.c +1 -0
@@ -37,6 +37,7 @@ const unsigned short vmcs_field_to_offset_table[] = {FIELD64 (VM_ENTRY_MSR_LOAD_ADDR , vm_entry_msr_load_addr),FIELD64 (PML_ADDRESS , pml_address),FIELD64 (TSC_OFFSET , tsc_offset), +FIELD64 (TSC_MULTIPLIER , tsc_multiplier),FIELD64 (VIRTUAL_APIC_PAGE_ADDR , virtual_apic_page_addr),FIELD64 (APIC_ACCESS_ADDR , apic_access_addr), FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
arch/x86/kvm/vmx/vmcs12.h +3 -1
@@ -70,7 +70,8 @@ struct __packed vmcs12 {u64 eptp_list_address;u64 pml_address;u64 encls_exiting_bitmap; -u64 padding64[2 ];/* room for future expansion */ +u64 tsc_multiplier; +u64 padding64[1 ];/* room for future expansion */ /* * To allow migration of L1 (complete with its L2 guests) between * machines of different natural widths (32 or 64 bit), we cannot have
@@ -258,6 +259,7 @@ static inline void vmx_check_vmcs12_offsets(void)CHECK_OFFSET (eptp_list_address,304 );CHECK_OFFSET (pml_address,312 );CHECK_OFFSET (encls_exiting_bitmap,320 ); +CHECK_OFFSET (tsc_multiplier,328 );CHECK_OFFSET (cr0_guest_host_mask,344 );CHECK_OFFSET (cr4_guest_host_mask,352 );CHECK_OFFSET (cr0_read_shadow,360 );
Signature · 1 line
2.17.1
PATCH v2 3/10 KVM: X86: Add kvm_scale_tsc_l1() and kvm_compute_tsc_offset_l1()
In reply to Ilias StamatisThe existing kvm_scale_tsc() scales the TSC using the current TSC scaling ratio. That used to be the same as L1's scaling ratio but now with nested TSC scaling support it is no longer the case.
This patch adds a new kvm_scale_tsc_l1() function that scales the TSC using L1's scaling ratio. The existing kvm_scale_tsc() can still be used for scaling L2 TSC values.
Additionally, this patch renames the kvm_compute_tsc_offset() function to kvm_compute_tsc_offset_l1() and has the function treat its TSC argument as an L1 TSC value. All existing code uses this function passing L1 values to it.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 41 ++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1789,6 +1789,7 @@ static inline bool kvm_is_supported_user_return_msr(u32 msr) }u64 kvm_scale_tsc (struct kvm_vcpu *vcpu,u64 tsc); +u64 kvm_scale_tsc_l1 (struct kvm_vcpu *vcpu,u64 tsc);u64 kvm_read_l1_tsc (struct kvm_vcpu *vcpu,u64 host_tsc);unsigned long kvm_get_linear_rip (struct kvm_vcpu *vcpu);
arch/x86/kvm/x86.c +28 -13
98 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v2 6/10 KVM: X86: Add functions that calculate the 02 TSC offset and multiplier
In reply to Ilias StamatisWhen L2 is entered we need to "merge" the TSC multiplier and TSC offset values of 01 and 12 together.
The merging is done using the following equations:
offset_02 = ((offset_01 * mult_12) >> shift_bits) + offset_12 mult_02 = (mult_01 * mult_12) >> shift_bits
Where shift_bits is kvm_tsc_scaling_ratio_frac_bits.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1793,6 +1793,8 @@ static inline bool kvm_is_supported_user_return_msr(u32 msr)u64 kvm_scale_tsc (struct kvm_vcpu *vcpu,u64 tsc);u64 kvm_scale_tsc_l1 (struct kvm_vcpu *vcpu,u64 tsc);u64 kvm_read_l1_tsc (struct kvm_vcpu *vcpu,u64 host_tsc); +void kvm_set_02_tsc_offset (struct kvm_vcpu *vcpu); +void kvm_set_02_tsc_multiplier (struct kvm_vcpu *vcpu);unsigned long kvm_get_linear_rip (struct kvm_vcpu *vcpu);bool kvm_is_linear_rip (struct kvm_vcpu *vcpu,unsigned long linear_rip);
arch/x86/kvm/x86.c +29 -0
@@ -2346,6 +2346,35 @@ u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) }EXPORT_SYMBOL_GPL (kvm_read_l1_tsc); +void kvm_set_02_tsc_offset (struct kvm_vcpu *vcpu) +{ +u64 l2_offset =static_call (kvm_x86_get_l2_tsc_offset)(vcpu); +u64 l2_multiplier =static_call (kvm_x86_get_l2_tsc_multiplier)(vcpu); + +if (l2_multiplier != kvm_default_tsc_scaling_ratio) { + vcpu->arch.tsc_offset =mul_s64_u64_shr ( + (s64 ) vcpu->arch.l1_tsc_offset, + l2_multiplier, + kvm_tsc_scaling_ratio_frac_bits); + } + + vcpu->arch.tsc_offset += l2_offset; +} +EXPORT_SYMBOL_GPL (kvm_set_02_tsc_offset); + +void kvm_set_02_tsc_multiplier (struct kvm_vcpu *vcpu) +{ +u64 l2_multiplier =static_call (kvm_x86_get_l2_tsc_multiplier)(vcpu); + +if (l2_multiplier != kvm_default_tsc_scaling_ratio) { + vcpu->arch.tsc_scaling_ratio =mul_u64_u64_shr ( + vcpu->arch.l1_tsc_scaling_ratio, + l2_multiplier, + kvm_tsc_scaling_ratio_frac_bits); + } +} +EXPORT_SYMBOL_GPL (kvm_set_02_tsc_multiplier); +static void kvm_vcpu_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) { vcpu->arch.l1_tsc_offset = offset;
Signature · 1 line
2.17.1
PATCH v2 7/10 KVM: X86: Move write_l1_tsc_offset() logic to common code and rename it
In reply to Ilias StamatisThe write_l1_tsc_offset() callback has a misleading name. It does not set L1's TSC offset, it rather updates the current TSC offset which might be different if a nested guest is executing. Additionally, both the vmx and svm implementations use the same logic for calculating the current TSC before writing it to hardware.
This patch renames the function and moves the common logic to the caller. The vmx/svm-specific code now merely sets the given offset to the corresponding hardware structure.
--- arch/x86/include/asm/kvm-x86-ops.h | 2 +- arch/x86/include/asm/kvm_host.h | 3 +-- arch/x86/kvm/svm/svm.c | 21 ++++----------------- arch/x86/kvm/vmx/vmx.c | 23 +++-------------------- arch/x86/kvm/x86.c | 17 ++++++++++++++++- 5 files changed, 25 insertions(+), 41 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -1
@@ -89,7 +89,7 @@ KVM_X86_OP(load_mmu_pgd) KVM_X86_OP_NULL(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier) -KVM_X86_OP(write_l1_tsc_offset) +KVM_X86_OP(write_tsc_offset) KVM_X86_OP(get_exit_info) KVM_X86_OP(check_intercept) KVM_X86_OP(handle_exit_irqoff)
arch/x86/include/asm/kvm_host.h +1 -2
@@ -1307,8 +1307,7 @@ struct kvm_x86_ops {u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);u64 (*get_l2_tsc_multiplier)(struct kvm_vcpu *vcpu); -/* Returns actual tsc_offset set in active VMCS */ -u64 (*write_l1_tsc_offset)(struct kvm_vcpu *vcpu,u64 offset); +void (*write_tsc_offset)(struct kvm_vcpu *vcpu,u64 offset);/* * Retrieve somewhat arbitrary exit information. Intended to be used
arch/x86/kvm/svm/svm.c +4 -17
@@ -1094,26 +1094,13 @@ static u64 svm_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)return kvm_default_tsc_scaling_ratio; } -static u64 svm_write_l1_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) +static void svm_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) {struct vcpu_svm *svm =to_svm (vcpu); -u64 g_tsc_offset =0 ; - -if (is_guest_mode (vcpu)) { -/* Write L1's TSC offset. */ - g_tsc_offset = svm->vmcb->control.tsc_offset - - svm->vmcb01.ptr->control.tsc_offset; - svm->vmcb01.ptr->control.tsc_offset = offset; - } - -trace_kvm_write_tsc_offset (vcpu->vcpu_id, - svm->vmcb->control.tsc_offset - g_tsc_offset, - offset); - - svm->vmcb->control.tsc_offset = offset + g_tsc_offset; + svm->vmcb01.ptr->control.tsc_offset = vcpu->arch.l1_tsc_offset; + svm->vmcb->control.tsc_offset = offset;vmcb_mark_dirty (svm->vmcb,VMCB_INTERCEPTS ); -return svm->vmcb->control.tsc_offset; }/* Evaluate instruction intercepts that depend on guest CPUID features. */
@@ -4540,7 +4527,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .get_l2_tsc_offset = svm_get_l2_tsc_offset, .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier, - .write_l1_tsc_offset = svm_write_l1_tsc_offset, + .write_tsc_offset = svm_write_tsc_offset, .load_mmu_pgd = svm_load_mmu_pgd,
arch/x86/kvm/vmx/vmx.c +3 -20
@@ -1810,26 +1810,9 @@ static u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)return multiplier; } -static u64 vmx_write_l1_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) +static void vmx_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) { -struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); -u64 g_tsc_offset =0 ; - -/* -* We're here if L1 chose not to trap WRMSR to TSC. According -* to the spec, this should set L1's TSC; The offset that L1 -* set for L2 remains unchanged, and still needs to be added -* to the newly set TSC to get L2's TSC. -*/ -if (is_guest_mode (vcpu) && - (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING )) - g_tsc_offset = vmcs12->tsc_offset; - -trace_kvm_write_tsc_offset (vcpu->vcpu_id, - vcpu->arch.tsc_offset - g_tsc_offset, - offset); -vmcs_write64 (TSC_OFFSET , offset + g_tsc_offset); -return offset + g_tsc_offset; +vmcs_write64 (TSC_OFFSET , offset); }/*
@@ -7725,7 +7708,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .get_l2_tsc_offset = vmx_get_l2_tsc_offset, .get_l2_tsc_multiplier = vmx_get_l2_tsc_multiplier, - .write_l1_tsc_offset = vmx_write_l1_tsc_offset, + .write_tsc_offset = vmx_write_tsc_offset, .load_mmu_pgd = vmx_load_mmu_pgd,
arch/x86/kvm/x86.c +16 -1
@@ -2377,8 +2377,23 @@ EXPORT_SYMBOL_GPL(kvm_set_02_tsc_multiplier);static void kvm_vcpu_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) { +trace_kvm_write_tsc_offset (vcpu->vcpu_id, + vcpu->arch.l1_tsc_offset, + offset); + vcpu->arch.l1_tsc_offset = offset; - vcpu->arch.tsc_offset =static_call (kvm_x86_write_l1_tsc_offset)(vcpu, offset); + vcpu->arch.tsc_offset = offset; + +if (is_guest_mode (vcpu)) { +/* +* We're here if L1 chose not to trap WRMSR to TSC and +* according to the spec this should set L1's TSC (as opposed +* to setting L1's offset for L2). +*/ +kvm_set_02_tsc_offset (vcpu); + } + +static_call (kvm_x86_write_tsc_offset)(vcpu, vcpu->arch.tsc_offset); }static inline bool kvm_check_tsc_unstable (void )
Signature · 1 line
2.17.1
PATCH v2 8/10 KVM: VMX: Set the TSC offset and multiplier on nested entry and exit
In reply to Ilias StamatisNow that nested TSC scaling is supported we need to calculate the correct 02 values for both the offset and the multiplier using the corresponding functions. On L2's exit the L1 values are restored.
--- arch/x86/kvm/vmx/nested.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/nested.c +9 -4
@@ -3354,8 +3354,9 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, }enter_guest_mode (vcpu); -if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING ) - vcpu->arch.tsc_offset += vmcs12->tsc_offset; + +kvm_set_02_tsc_offset (vcpu); +kvm_set_02_tsc_multiplier (vcpu);if (prepare_vmcs02 (vcpu, vmcs12, &entry_failure_code)) { exit_reason.basic =EXIT_REASON_INVALID_STATE ;
@@ -4463,8 +4464,12 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,if (nested_cpu_has_preemption_timer (vmcs12))hrtimer_cancel (&to_vmx (vcpu)->nested.preemption_timer); -if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING ) - vcpu->arch.tsc_offset -= vmcs12->tsc_offset; +if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING ) { + vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset; + +if (vmcs12->secondary_vm_exec_control &SECONDARY_EXEC_TSC_SCALING ) + vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; + }if (likely (!vmx->fail)) {sync_vmcs02_to_vmcs12 (vcpu, vmcs12);
Signature · 1 line
2.17.1
PATCH v2 9/10 KVM: VMX: Expose TSC scaling to L2
In reply to Ilias StamatisExpose the TSC scaling feature to nested guests.
--- arch/x86/kvm/vmx/nested.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +4 -2
@@ -2277,7 +2277,8 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |SECONDARY_EXEC_APIC_REGISTER_VIRT | - SECONDARY_EXEC_ENABLE_VMFUNC); +SECONDARY_EXEC_ENABLE_VMFUNC | + SECONDARY_EXEC_TSC_SCALING);if (nested_cpu_has (vmcs12,CPU_BASED_ACTIVATE_SECONDARY_CONTROLS )) exec_control |= vmcs12->secondary_vm_exec_control;
@@ -6479,7 +6480,8 @@ void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)SECONDARY_EXEC_RDRAND_EXITING |SECONDARY_EXEC_ENABLE_INVPCID |SECONDARY_EXEC_RDSEED_EXITING | -SECONDARY_EXEC_XSAVES ; +SECONDARY_EXEC_XSAVES | +SECONDARY_EXEC_TSC_SCALING ;/* * We can emulate "VMCS shadowing," even if the hardware
Signature · 1 line
2.17.1
PATCH v2 10/10 KVM: selftests: x86: Add vmx_nested_tsc_scaling_test
In reply to Ilias StamatisTest that nested TSC scaling works as expected with both L1 and L2 scaled.
--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/vmx_nested_tsc_scaling_test.c | 242 ++++++++++++++++++ 3 files changed, 244 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/vmx_nested_tsc_scaling_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -29,6 +29,7 @@ /x86_64/vmx_preemption_timer_test /x86_64/vmx_set_nested_state_test /x86_64/vmx_tsc_adjust_test +/x86_64/vmx_nested_tsc_scaling_test /x86_64/xapic_ipi_test /x86_64/xen_shinfo_test /x86_64/xen_vmcall_test
tools/testing/selftests/kvm/Makefile +1 -0
@@ -60,6 +60,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/vmx_close_while_nested_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_dirty_log_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_set_nested_state_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_tsc_adjust_test +TEST_GEN_PROGS_x86_64 += x86_64/vmx_nested_tsc_scaling_test TEST_GEN_PROGS_x86_64 += x86_64/xapic_ipi_test TEST_GEN_PROGS_x86_64 += x86_64/xss_msr_test TEST_GEN_PROGS_x86_64 += x86_64/debug_regs
tools/testing/selftests/kvm/x86_64/vmx_nested_tsc_scaling_test.c new file +242 -0
242 lines not on this page · raw message
Signature · 1 line
2.17.1
Sean Christopherson
maintainerRe: [PATCH v2 01/10] math64.h: Add mul_s64_u64_shr()
In reply to Ilias StamatisQuoted 1 line · Ilias Stamatis
This function is needed for the nested TSC scaling code where we need to
s/the nested/KVM's nested virtualization. Non-KVM folk will have no idea what "nested" means without context. They may know not what nested virtualization is, but this will at least give them a starting point for asking the right questions.
Quoted 1 line
multiply the signed TSC offset with the unsigned TSC multiplier.
Sean Christopherson
maintainerRe: [PATCH v2 02/10] KVM: X86: Store L1's TSC scaling ratio in 'struct kvm_vcpu_arch'
In reply to Ilias StamatisQuoted 1 line · Ilias Stamatis
Store L1's scaling ratio in that struct like we already do for L1's TSC
s/that struct/kvm_vcpu_arch. Forcing the reader to look at the subject to understand the changelog is annoying, especially when it saves all of a handful of characters. E.g. I often read patches without the subject in scope.
Quoted 6 lines
offset. This allows for easy save/restore when we enter and then exit the nested guest.
Signed-off-by Ilias Stamatis <[email protected]>Reviewed-by Maxim Levitsky <[email protected]>---
...
arch/x86/kvm/x86.c static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) · line 2189
Quoted 10 lines
arch/x86/kvm/x86.c +1
-0@@ -2185,6 +2185,7 @@ static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)/* Guest TSC same frequency as host TSC? */ if (!scale) { + vcpu->arch.l1_tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Looks like these are always set as a pair, maybe add a helper, e.g.
static void kvm_set_l1_tsc_scaling_ratio(u64 ratio)
{
vcpu->arch.l1_tsc_scaling_ratio = ratio;
vcpu->arch.tsc_scaling_ratio = ratio;
}Quoted 31 lines
arch/x86/kvm/x86.c +3
-2return 0 ; }@@ -2211,7 +2212,7 @@ static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)return -1 ; } - vcpu->arch.tsc_scaling_ratio = ratio; + vcpu->arch.l1_tsc_scaling_ratio = vcpu->arch.tsc_scaling_ratio = ratio;return 0 ; }@@ -2223,6 +2224,7 @@ static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)/* tsc_khz can be zero if TSC calibration fails */ if (user_tsc_khz ==0 ) {/* set tsc_scaling_ratio to a safe value */ + vcpu->arch.l1_tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;return -1 ; }@@ -2459,7 +2461,7 @@ static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,static inline void adjust_tsc_offset_host (struct kvm_vcpu *vcpu,s64 adjustment) { -if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio) +if (vcpu->arch.l1_tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)WARN_ON (adjustment <0 ); adjustment =kvm_scale_tsc (vcpu, (u64 ) adjustment);adjust_tsc_offset_guest (vcpu, adjustment);Signature · 1 line
2.17.1
Sean Christopherson
maintainerRe: [PATCH v2 03/10] KVM: X86: Add kvm_scale_tsc_l1() and kvm_compute_tsc_offset_l1()
In reply to Ilias StamatisQuoted 24 lines · Ilias Stamatis
The existing
kvm_scale_tsc()scales the TSC using the current TSC scaling ratio. That used to be the same as L1's scaling ratio but now with nested TSC scaling support it is no longer the case.This patch adds a new
kvm_scale_tsc_l1()function that scales the TSC using L1's scaling ratio. The existingkvm_scale_tsc()can still be used for scaling L2 TSC values.Additionally, this patch renames the
kvm_compute_tsc_offset()function tokvm_compute_tsc_offset_l1()and has the function treat its TSC argument as an L1 TSC value. All existing code uses this function passing L1 values to it.Signed-off-by Ilias Stamatis <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 41 ++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 13 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h static inline bool kvm_is_supported_user_return_msr(u32 msr) · line 1793
Quoted 5 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -1789,6 +1789,7 @@ static inline bool kvm_is_supported_user_return_msr(u32 msr) }u64 kvm_scale_tsc (struct kvm_vcpu *vcpu,u64 tsc); +u64 kvm_scale_tsc_l1 (struct kvm_vcpu *vcpu,u64 tsc);u64 kvm_read_l1_tsc (struct kvm_vcpu *vcpu,u64 host_tsc);
I don't really care which version is used, but we should be consistent, i.e. choose kvm_<action>_tsc_l1 or kvm_<action>_tsc_l1, not both. The easy choice is the former since it's already there.
Quoted 10 lines
arch/x86/include/asm/kvm_host.h +0
-0unsigned long kvm_get_linear_rip (struct kvm_vcpu *vcpu);arch/x86/kvm/x86.c +0
-1@@ -2319,18 +2319,30 @@ u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) }EXPORT_SYMBOL_GPL (kvm_scale_tsc); -static u64 kvm_compute_tsc_offset (struct kvm_vcpu *vcpu,u64 target_tsc)
arch/x86/kvm/x86.c u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) · line 2331
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+u64 kvm_scale_tsc_l1 (struct kvm_vcpu *vcpu,u64 tsc) +{ +u64 _tsc = tsc; +u64 ratio = vcpu->arch.l1_tsc_scaling_ratio; + +if (ratio != kvm_default_tsc_scaling_ratio) + _tsc =__scale_tsc (ratio, tsc); + +return _tsc; +}
Just make the ratio a param. This is complete copy+paste of kvm_scale_tsc(), with 3 characters added. And all of the callers are already in an L1-specific function or have L1 vs. L2 awareness. IMO, that makes the code less magical, too, as I don't have to dive into a helper to see that it reads l1_tsc_scaling_ratio versus tsc_scaling_ratio.
Quoted 65 lines
arch/x86/kvm/x86.c +10
-7+EXPORT_SYMBOL_GPL (kvm_scale_tsc_l1); + +static u64 kvm_compute_tsc_offset_l1 (struct kvm_vcpu *vcpu,u64 target_tsc) {u64 tsc; - tsc =kvm_scale_tsc (vcpu,rdtsc ()); + tsc =kvm_scale_tsc_l1 (vcpu,rdtsc ());return target_tsc - tsc; }u64 kvm_read_l1_tsc (struct kvm_vcpu *vcpu,u64 host_tsc) { -return vcpu->arch.l1_tsc_offset +kvm_scale_tsc (vcpu, host_tsc); +return vcpu->arch.l1_tsc_offset +kvm_scale_tsc_l1 (vcpu, host_tsc); }EXPORT_SYMBOL_GPL (kvm_read_l1_tsc);@@ -2363,7 +2375,7 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data)bool synchronizing = false;raw_spin_lock_irqsave (&kvm->arch.tsc_write_lock, flags); - offset =kvm_compute_tsc_offset (vcpu, data); + offset =kvm_compute_tsc_offset_l1 (vcpu, data); ns =get_kvmclock_base_ns (); elapsed = ns - kvm->arch.last_tsc_nsec;@@ -2402,7 +2414,7 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data) }else {u64 delta =nsec_to_cycles (vcpu, elapsed); data += delta; - offset =kvm_compute_tsc_offset (vcpu, data); + offset =kvm_compute_tsc_offset_l1 (vcpu, data); } matched = true; already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);@@ -2463,7 +2475,7 @@ static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment) {if (vcpu->arch.l1_tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)WARN_ON (adjustment <0 ); - adjustment =kvm_scale_tsc (vcpu, (u64 ) adjustment); + adjustment =kvm_scale_tsc_l1 (vcpu, (u64 ) adjustment);adjust_tsc_offset_guest (vcpu, adjustment); }@@ -2846,7 +2858,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)/* With all the info we got, fill in the values */ if (kvm_has_tsc_control) - tgt_tsc_khz =kvm_scale_tsc (v, tgt_tsc_khz); + tgt_tsc_khz =kvm_scale_tsc_l1 (v, tgt_tsc_khz);if (unlikely (vcpu->hw_tsc_khz != tgt_tsc_khz)) { kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,@@ -3235,7 +3247,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (msr_info->host_initiated) {kvm_synchronize_tsc (vcpu, data); }else { -u64 adj =kvm_compute_tsc_offset (vcpu, data) - vcpu->arch.l1_tsc_offset; +u64 adj =kvm_compute_tsc_offset_l1 (vcpu, data) - vcpu->arch.l1_tsc_offset;adjust_tsc_offset_guest (vcpu, adj); vcpu->arch.ia32_tsc_adjust_msr += adj; }
arch/x86/kvm/x86.c int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 3552
Quoted 8 lines
arch/x86/kvm/x86.c +1
-4@@ -3537,10 +3549,13 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)* return L1's TSC value to ensure backwards-compatible * behavior for migration. */ -u64 tsc_offset = msr_info->host_initiated ? vcpu->arch.l1_tsc_offset : - vcpu->arch.tsc_offset; - - msr_info->data =kvm_scale_tsc (vcpu,rdtsc ()) + tsc_offset; +if (msr_info->host_initiated) {
Unnecessary curly braces.
Quoted 21 lines
arch/x86/kvm/x86.c +7
-1+ msr_info->data =kvm_scale_tsc_l1 (vcpu,rdtsc ()) + + vcpu->arch.l1_tsc_offset; + }else { + msr_info->data =kvm_scale_tsc (vcpu,rdtsc ()) + + vcpu->arch.tsc_offset; + }break ; }case MSR_MTRRcap:@@ -4123,7 +4138,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)mark_tsc_unstable ("KVM discovered backwards TSC" );if (kvm_check_tsc_unstable ()) { -u64 offset =kvm_compute_tsc_offset (vcpu, +u64 offset =kvm_compute_tsc_offset_l1 (vcpu, vcpu->arch.last_guest_tsc);kvm_vcpu_write_tsc_offset (vcpu, offset); vcpu->arch.tsc_catchup =1 ;Signature · 1 line
2.17.1
Sean Christopherson
maintainerRe: [PATCH v2 06/10] KVM: X86: Add functions that calculate the 02 TSC offset and multiplier
In reply to Ilias StamatisQuoted 33 lines · Ilias Stamatis
When L2 is entered we need to "merge" the TSC multiplier and TSC offset values of 01 and 12 together.
The merging is done using the following equations:
offset_02= ((offset_01*mult_12) >>shift_bits) +offset_12mult_02= (mult_01*mult_12) >>shift_bitsWhere
shift_bitsiskvm_tsc_scaling_ratio_frac_bits.Signed-off-by Ilias Stamatis <[email protected]>--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)arch/x86/include/asm/kvm_host.h +2
-0@@ -1793,6 +1793,8 @@ static inline bool kvm_is_supported_user_return_msr(u32 msr)u64 kvm_scale_tsc (struct kvm_vcpu *vcpu,u64 tsc);u64 kvm_scale_tsc_l1 (struct kvm_vcpu *vcpu,u64 tsc);u64 kvm_read_l1_tsc (struct kvm_vcpu *vcpu,u64 host_tsc); +void kvm_set_02_tsc_offset (struct kvm_vcpu *vcpu); +void kvm_set_02_tsc_multiplier (struct kvm_vcpu *vcpu);unsigned long kvm_get_linear_rip (struct kvm_vcpu *vcpu);bool kvm_is_linear_rip (struct kvm_vcpu *vcpu,unsigned long linear_rip);arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) · line 2349
Quoted 4 lines
arch/x86/kvm/x86.c +1
-0@@ -2346,6 +2346,35 @@ u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) }EXPORT_SYMBOL_GPL (kvm_read_l1_tsc); +void kvm_set_02_tsc_offset (struct kvm_vcpu *vcpu)
I dislike like the "02" nomenclature. "02" is used specifically to refer to vmcs02 and vmcb02, whereas these helpers touch KVM's software model, not the CPU struct. Can't this simply be "l2"?
Quoted 6 lines
arch/x86/kvm/x86.c +6
-0+{ +u64 l2_offset =static_call (kvm_x86_get_l2_tsc_offset)(vcpu); +u64 l2_multiplier =static_call (kvm_x86_get_l2_tsc_multiplier)(vcpu); + +if (l2_multiplier != kvm_default_tsc_scaling_ratio) { + vcpu->arch.tsc_offset = mul_s64_u64_shr(
arch/x86/kvm/x86.c u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) · line 2365
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+ (s64 ) vcpu->arch.l1_tsc_offset, + l2_multiplier, + kvm_tsc_scaling_ratio_frac_bits); + } + + vcpu->arch.tsc_offset += l2_offset; +} +EXPORT_SYMBOL_GPL (kvm_set_02_tsc_offset); + +void kvm_set_02_tsc_multiplier (struct kvm_vcpu *vcpu)
I normally like splitting patches gratuitously, but in this case I think it would be better to combine this with the VMX usage in patch 08. It's impossible to properly review this patch without looking at its callers.
arch/x86/kvm/x86.c u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) · line 2367
Quoted 2 lines
arch/x86/kvm/x86.c +2
-0+{ +u64 l2_multiplier =static_call (kvm_x86_get_l2_tsc_multiplier)(vcpu);
Case in point, calling back into vendor code to get the L2 multiplier is silly, just have the caller provide it explicitly.
arch/x86/kvm/x86.c u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) · line 2368
Quoted 1 line
arch/x86/kvm/x86.c +1
-0+if (l2_multiplier != kvm_default_tsc_scaling_ratio) {
Why does this check against the default ratio instead of L1's ratio? If L1 is running a non-default ratio, but L2 is running a default ratio, won't this result in KVM leaving vcpu->arch.tsc_scaling_ratio at L1's ratio? Or is there scaling ratio magic I don't understand (which is likely...)? If there's magic, can you add a comment?
Same feedback for the check in the offset version.
Quoted 14 lines
arch/x86/kvm/x86.c +8
-0+ vcpu->arch.tsc_scaling_ratio =mul_u64_u64_shr ( + vcpu->arch.l1_tsc_scaling_ratio, + l2_multiplier, + kvm_tsc_scaling_ratio_frac_bits); + } +} +EXPORT_SYMBOL_GPL (kvm_set_02_tsc_multiplier); +static void kvm_vcpu_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) { vcpu->arch.l1_tsc_offset = offset;Signature · 1 line
2.17.1
Sean Christopherson
maintainerRe: [PATCH v2 07/10] KVM: X86: Move write_l1_tsc_offset() logic to common code and rename it
In reply to Ilias StamatisQuoted 5 lines · Ilias Stamatis
The
write_l1_tsc_offset()callback has a misleading name. It does not set L1's TSC offset, it rather updates the current TSC offset which might be different if a nested guest is executing. Additionally, both the vmx and svm implementations use the same logic for calculating the current TSC before writing it to hardware.
I don't disagree, but the current name as the advantage of clarifying (well, hinting) that the offset is L1's offset. That hint is lost in this refactoring. Maybe rename "u64 offset" to "u64 l1_tsc_offset"?
Quoted 1 line
This patch renames the function and moves the common logic to the
Use imperative mood instead of "This patch. FromDocumentation/process/submitting-patches.rst:
Describe your changes in imperative mood, e.g. "make xyzzy do frotz" instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy to do frotz", as if you are giving orders to the codebase to change its behaviour.
Quoted 131 lines
caller. The vmx/svm-specific code now merely sets the given offset to the corresponding hardware structure.
Signed-off-by Ilias Stamatis <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 2 +- arch/x86/include/asm/kvm_host.h | 3 +-- arch/x86/kvm/svm/svm.c | 21 ++++----------------- arch/x86/kvm/vmx/vmx.c | 23 +++-------------------- arch/x86/kvm/x86.c | 17 ++++++++++++++++- 5 files changed, 25 insertions(+), 41 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-1@@ -89,7 +89,7 @@ KVM_X86_OP(load_mmu_pgd) KVM_X86_OP_NULL(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier) -KVM_X86_OP(write_l1_tsc_offset) +KVM_X86_OP(write_tsc_offset) KVM_X86_OP(get_exit_info) KVM_X86_OP(check_intercept) KVM_X86_OP(handle_exit_irqoff)arch/x86/include/asm/kvm_host.h +1
-2@@ -1307,8 +1307,7 @@ struct kvm_x86_ops {u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);u64 (*get_l2_tsc_multiplier)(struct kvm_vcpu *vcpu); -/* Returns actual tsc_offset set in active VMCS */ -u64 (*write_l1_tsc_offset)(struct kvm_vcpu *vcpu,u64 offset); +void (*write_tsc_offset)(struct kvm_vcpu *vcpu,u64 offset);/* * Retrieve somewhat arbitrary exit information. Intended to be used arch/x86/kvm/svm/svm.c +4
-17@@ -1094,26 +1094,13 @@ static u64 svm_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)return kvm_default_tsc_scaling_ratio; } -static u64 svm_write_l1_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) +static void svm_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) {struct vcpu_svm *svm =to_svm (vcpu); -u64 g_tsc_offset =0 ; - -if (is_guest_mode (vcpu)) { -/* Write L1's TSC offset. */ - g_tsc_offset = svm->vmcb->control.tsc_offset - - svm->vmcb01.ptr->control.tsc_offset; - svm->vmcb01.ptr->control.tsc_offset = offset; - } - -trace_kvm_write_tsc_offset (vcpu->vcpu_id, - svm->vmcb->control.tsc_offset - g_tsc_offset, - offset); - - svm->vmcb->control.tsc_offset = offset + g_tsc_offset; + svm->vmcb01.ptr->control.tsc_offset = vcpu->arch.l1_tsc_offset; + svm->vmcb->control.tsc_offset = offset;vmcb_mark_dirty (svm->vmcb,VMCB_INTERCEPTS ); -return svm->vmcb->control.tsc_offset; }/* Evaluate instruction intercepts that depend on guest CPUID features. */ @@ -4540,7 +4527,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .get_l2_tsc_offset = svm_get_l2_tsc_offset, .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier, - .write_l1_tsc_offset = svm_write_l1_tsc_offset, + .write_tsc_offset = svm_write_tsc_offset, .load_mmu_pgd = svm_load_mmu_pgd,arch/x86/kvm/vmx/vmx.c +3
-20@@ -1810,26 +1810,9 @@ static u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)return multiplier; } -static u64 vmx_write_l1_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) +static void vmx_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) { -struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); -u64 g_tsc_offset =0 ; - -/* -* We're here if L1 chose not to trap WRMSR to TSC. According -* to the spec, this should set L1's TSC; The offset that L1 -* set for L2 remains unchanged, and still needs to be added -* to the newly set TSC to get L2's TSC. -*/ -if (is_guest_mode (vcpu) && - (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING )) - g_tsc_offset = vmcs12->tsc_offset; - -trace_kvm_write_tsc_offset (vcpu->vcpu_id, - vcpu->arch.tsc_offset - g_tsc_offset, - offset); -vmcs_write64 (TSC_OFFSET , offset + g_tsc_offset); -return offset + g_tsc_offset; +vmcs_write64 (TSC_OFFSET , offset); }/* @@ -7725,7 +7708,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .get_l2_tsc_offset = vmx_get_l2_tsc_offset, .get_l2_tsc_multiplier = vmx_get_l2_tsc_multiplier, - .write_l1_tsc_offset = vmx_write_l1_tsc_offset, + .write_tsc_offset = vmx_write_tsc_offset, .load_mmu_pgd = vmx_load_mmu_pgd,arch/x86/kvm/x86.c +0
-0@@ -2377,8 +2377,23 @@ EXPORT_SYMBOL_GPL(kvm_set_02_tsc_multiplier);static void kvm_vcpu_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset)
arch/x86/kvm/x86.c EXPORT_SYMBOL_GPL(kvm_set_02_tsc_multiplier); · line 2387
Quoted 10 lines
arch/x86/kvm/x86.c +7
-1{ +trace_kvm_write_tsc_offset (vcpu->vcpu_id, + vcpu->arch.l1_tsc_offset, + offset); + vcpu->arch.l1_tsc_offset = offset; - vcpu->arch.tsc_offset =static_call (kvm_x86_write_l1_tsc_offset)(vcpu, offset); + vcpu->arch.tsc_offset = offset; + +if (is_guest_mode (vcpu)) {
Unnecessary curly braces.
arch/x86/kvm/x86.c EXPORT_SYMBOL_GPL(kvm_set_02_tsc_multiplier); · line 2392
Quoted 5 lines
arch/x86/kvm/x86.c +5
-0+/* +* We're here if L1 chose not to trap WRMSR to TSC and +* according to the spec this should set L1's TSC (as opposed +* to setting L1's offset for L2). +*/
While we're shuffling code, can we improve this comment? It works for the WRMSR case, but makes no sense in the context of host TSC adjustments. It's not at all clear to me that it's even correct or relevant in those cases.
arch/x86/kvm/x86.c EXPORT_SYMBOL_GPL(kvm_set_02_tsc_multiplier); · line 2393
Quoted 1 line
arch/x86/kvm/x86.c +1
-0+kvm_set_02_tsc_offset (vcpu);
I really dislike that kvm_set_02_tsc_offset() consumes a bunch of variables _and_ sets arch.tsc_offset, e.g. it's not at all obvious that moving this call around will break L2. Even more bizarre is that arch.tsc_offset is conditionally consumed. Oh, and kvm_set_02_tsc_offset() is not idempotent since it can do a RMW on arch.tsc_offset.
The below static_call() dependency doesn't appear to be easily solved, but we can at least strongly hint that vcpu->arch.tsc_offset is set. For everything else, I think we can clean things up by doing this (with the vendor calls providing the L2 variables directly):
void kvm_set_l2_tsc_offset(struct kvm_vcpu *vcpu, u64 l2_offset,
u64 l2_multiplier)
{
u64 l1_offset = vcpu->arch.l1_tsc_offset;
if (l2_multiplier != kvm_default_tsc_scaling_ratio)
l2_offset += mul_s64_u64_shr((s64)l1_tsc_offset, l2_multiplier,
kvm_tsc_scaling_ratio_frac_bits);
vcpu->arch.tsc_offset = l2_offset;
}EXPORT_SYMBOL_GPL(kvm_get_l2_tsc_offset);
static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
{
trace_kvm_write_tsc_offset(vcpu->vcpu_id,
vcpu->arch.l1_tsc_offset,
offset);
vcpu->arch.l1_tsc_offset = offset;
if (is_guest_mode(vcpu))
kvm_set_l2_tsc_offset(vcpu,
static_call(kvm_x86_get_l2_tsc_offset)(vcpu),
static_call(kvm_x86_get_l2_tsc_multiplier)(vcpu));
else
vcpu->arch.tsc_offset = offset;
static_call(kvm_x86_write_tsc_offset)(vcpu, vcpu->arch.tsc_offset);
}An alternative would be to explicitly track L1 and L2, and _never_ track the current TSC values. I.e. always compute the correct value on the fly. I think the only hot path is the TSC deadline timer, and AFAICT that always runs with L1's timer. Speaking of which, at the end of this series, vmx_set_hv_timer() uses L1's TSC but the current scaling ratio; that can't be right.
Quoted 9 lines
arch/x86/kvm/x86.c +3
-0+ } + +static_call (kvm_x86_write_tsc_offset)(vcpu, vcpu->arch.tsc_offset); }static inline bool kvm_check_tsc_unstable (void )Signature · 1 line
2.17.1
Sean Christopherson
maintainerRe: [PATCH v2 08/10] KVM: VMX: Set the TSC offset and multiplier on nested entry and exit
In reply to Ilias StamatisQuoted 14 lines · Ilias Stamatis
Now that nested TSC scaling is supported we need to calculate the correct 02 values for both the offset and the multiplier using the corresponding functions. On L2's exit the L1 values are restored.
Signed-off-by Ilias Stamatis <[email protected]>--- arch/x86/kvm/vmx/nested.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/nested.c +0
-0
arch/x86/kvm/vmx/nested.c enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, · line 3359
Quoted 8 lines
arch/x86/kvm/vmx/nested.c +3
-2@@ -3354,8 +3354,9 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, }enter_guest_mode (vcpu); -if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING ) - vcpu->arch.tsc_offset += vmcs12->tsc_offset; + +kvm_set_02_tsc_offset (vcpu); +kvm_set_02_tsc_multiplier (vcpu);
Please move this code into prepare_vmcs02() to co-locate it with the relevant vmcs02 logic. If there's something in prepare_vmcs02() that consumes vcpu->arch.tsc_offset() other than the obvious VMWRITE, I vote to move things around to fix that rather than create this weird split.
Quoted 20 lines
arch/x86/kvm/vmx/nested.c +6
-2if (prepare_vmcs02 (vcpu, vmcs12, &entry_failure_code)) { exit_reason.basic =EXIT_REASON_INVALID_STATE ;@@ -4463,8 +4464,12 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,if (nested_cpu_has_preemption_timer (vmcs12))hrtimer_cancel (&to_vmx (vcpu)->nested.preemption_timer); -if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING ) - vcpu->arch.tsc_offset -= vmcs12->tsc_offset; +if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING ) { + vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset; + +if (vmcs12->secondary_vm_exec_control &SECONDARY_EXEC_TSC_SCALING ) + vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; + }if (likely (!vmx->fail)) {sync_vmcs02_to_vmcs12 (vcpu, vmcs12);Signature · 1 line
2.17.1
Re: [PATCH v2 02/10] KVM: X86: Store L1's TSC scaling ratio in 'struct kvm_vcpu_arch'
In reply to Sean ChristophersonQuoted 29 lines · Sean Christopherson
Quoted 1 line · Ilias Stamatis
Store L1's scaling ratio in that struct like we already do for L1's TSC
s/that struct/kvm_vcpu_arch. Forcing the reader to look at the subject to understand the changelog is annoying, especially when it saves all of a handful of characters. E.g. I often read patches without the subject in scope.
Quoted 6 lines
offset. This allows for easy save/restore when we enter and then exit the nested guest.
Signed-off-by Ilias Stamatis <[email protected]>Reviewed-by Maxim Levitsky <[email protected]>---
...
arch/x86/kvm/x86.c static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) · line 2189
Quoted 10 lines
arch/x86/kvm/x86.c +1
-0@@ -2185,6 +2185,7 @@ static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)/* Guest TSC same frequency as host TSC? */ if (!scale) { + vcpu->arch.l1_tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;Looks like these are always set as a pair, maybe add a helper, e.g.
Quoted 6 lines
static void
kvm_set_l1_tsc_scaling_ratio(u64 ratio){ vcpu->arch.l1_tsc_scaling_ratio = ratio; vcpu->arch.tsc_scaling_ratio = ratio; }
Hmm, they are not *always* set as a pair. Plus wouldn't this name be a bit misleading suggesting that L1's scaling ratio is updated but implicitly changing the current ratio too?
I'm not sure a helper function would add much
value here.
Quoted 31 lines
Quoted 31 lines
arch/x86/kvm/x86.c +3
-3return 0 ; }@@ -2211,7 +2212,7 @@ static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)return -1 ; } - vcpu->arch.tsc_scaling_ratio = ratio; + vcpu->arch.l1_tsc_scaling_ratio = vcpu->arch.tsc_scaling_ratio = ratio;return 0 ; }@@ -2223,6 +2224,7 @@ static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)/* tsc_khz can be zero if TSC calibration fails */ if (user_tsc_khz ==0 ) {/* set tsc_scaling_ratio to a safe value */ + vcpu->arch.l1_tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;return -1 ; }@@ -2459,7 +2461,7 @@ static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,static inline void adjust_tsc_offset_host (struct kvm_vcpu *vcpu,s64 adjustment) { -if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio) +if (vcpu->arch.l1_tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)WARN_ON (adjustment <0 ); adjustment =kvm_scale_tsc (vcpu, (u64 ) adjustment);adjust_tsc_offset_guest (vcpu, adjustment); --2.17.1
Re: [PATCH v2 03/10] KVM: X86: Add kvm_scale_tsc_l1() and kvm_compute_tsc_offset_l1()
In reply to Sean ChristophersonQuoted 27 lines · Sean Christopherson
Quoted 26 lines · Ilias Stamatis
The existing
kvm_scale_tsc()scales the TSC using the current TSC scaling ratio. That used to be the same as L1's scaling ratio but now with nested TSC scaling support it is no longer the case.This patch adds a new
kvm_scale_tsc_l1()function that scales the TSC using L1's scaling ratio. The existingkvm_scale_tsc()can still be used for scaling L2 TSC values.Additionally, this patch renames the
kvm_compute_tsc_offset()function tokvm_compute_tsc_offset_l1()and has the function treat its TSC argument as an L1 TSC value. All existing code uses this function passing L1 values to it.Signed-off-by Ilias Stamatis <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 41 ++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 13 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0@@ -1789,6 +1789,7 @@ static inline bool kvm_is_supported_user_return_msr(u32 msr) }
Quoted 7 lines
arch/x86/include/asm/kvm_host.h static inline bool kvm_is_supported_user_return_msr(u32 msr) · line 1793
Quoted 3 lines
arch/x86/include/asm/kvm_host.h +1
-0u64 kvm_scale_tsc (struct kvm_vcpu *vcpu,u64 tsc); +u64 kvm_scale_tsc_l1 (struct kvm_vcpu *vcpu,u64 tsc);u64 kvm_read_l1_tsc (struct kvm_vcpu *vcpu,u64 host_tsc);I don't really care which version is used, but we should be consistent, i.e. choose kvm_<action>_tsc_l1 or kvm_<action>_tsc_l1, not both. The easy choice is the former since it's already there.
OK
Quoted 19 lines
Quoted 19 lines
arch/x86/include/asm/kvm_host.h +0
-0unsigned long kvm_get_linear_rip (struct kvm_vcpu *vcpu);arch/x86/kvm/x86.c +8
-1@@ -2319,18 +2319,30 @@ u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) }EXPORT_SYMBOL_GPL (kvm_scale_tsc); -static u64 kvm_compute_tsc_offset (struct kvm_vcpu *vcpu,u64 target_tsc) +u64 kvm_scale_tsc_l1 (struct kvm_vcpu *vcpu,u64 tsc) +{ +u64 _tsc = tsc; +u64 ratio = vcpu->arch.l1_tsc_scaling_ratio; + +if (ratio != kvm_default_tsc_scaling_ratio) + _tsc =__scale_tsc (ratio, tsc); +
Quoted 9 lines
arch/x86/kvm/x86.c u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) · line 2331
Quoted 2 lines
arch/x86/kvm/x86.c +2
-0+return _tsc; +}Just make the ratio a param. This is complete copy+paste of
kvm_scale_tsc(), with 3 characters added. And all of the callers are already in an L1-specific function or have L1 vs. L2 awareness. IMO, that makes the code less magical, too, as I don't have to dive into a helper to see that it readsl1_tsc_scaling_ratioversustsc_scaling_ratio.
That's how I did it initially but changed it into a separate function after receiving feedback on v1. I'm neutral, I don't mind changing it back.
More
opinions?
Quoted 97 lines
Quoted 65 lines
arch/x86/kvm/x86.c +10
-7+EXPORT_SYMBOL_GPL (kvm_scale_tsc_l1); + +static u64 kvm_compute_tsc_offset_l1 (struct kvm_vcpu *vcpu,u64 target_tsc) {u64 tsc; - tsc =kvm_scale_tsc (vcpu,rdtsc ()); + tsc =kvm_scale_tsc_l1 (vcpu,rdtsc ());return target_tsc - tsc; }u64 kvm_read_l1_tsc (struct kvm_vcpu *vcpu,u64 host_tsc) { -return vcpu->arch.l1_tsc_offset +kvm_scale_tsc (vcpu, host_tsc); +return vcpu->arch.l1_tsc_offset +kvm_scale_tsc_l1 (vcpu, host_tsc); }EXPORT_SYMBOL_GPL (kvm_read_l1_tsc);@@ -2363,7 +2375,7 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data)bool synchronizing = false;raw_spin_lock_irqsave (&kvm->arch.tsc_write_lock, flags); - offset =kvm_compute_tsc_offset (vcpu, data); + offset =kvm_compute_tsc_offset_l1 (vcpu, data); ns =get_kvmclock_base_ns (); elapsed = ns - kvm->arch.last_tsc_nsec;@@ -2402,7 +2414,7 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data) }else {u64 delta =nsec_to_cycles (vcpu, elapsed); data += delta; - offset =kvm_compute_tsc_offset (vcpu, data); + offset =kvm_compute_tsc_offset_l1 (vcpu, data); } matched = true; already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);@@ -2463,7 +2475,7 @@ static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment) {if (vcpu->arch.l1_tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)WARN_ON (adjustment <0 ); - adjustment =kvm_scale_tsc (vcpu, (u64 ) adjustment); + adjustment =kvm_scale_tsc_l1 (vcpu, (u64 ) adjustment);adjust_tsc_offset_guest (vcpu, adjustment); }@@ -2846,7 +2858,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)/* With all the info we got, fill in the values */ if (kvm_has_tsc_control) - tgt_tsc_khz =kvm_scale_tsc (v, tgt_tsc_khz); + tgt_tsc_khz =kvm_scale_tsc_l1 (v, tgt_tsc_khz);if (unlikely (vcpu->hw_tsc_khz != tgt_tsc_khz)) { kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,@@ -3235,7 +3247,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (msr_info->host_initiated) {kvm_synchronize_tsc (vcpu, data); }else { -u64 adj =kvm_compute_tsc_offset (vcpu, data) - vcpu->arch.l1_tsc_offset; +u64 adj =kvm_compute_tsc_offset_l1 (vcpu, data) - vcpu->arch.l1_tsc_offset;adjust_tsc_offset_guest (vcpu, adj); vcpu->arch.ia32_tsc_adjust_msr += adj; }arch/x86/kvm/x86.c int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 3552
Quoted 8 lines
arch/x86/kvm/x86.c +1
-4@@ -3537,10 +3549,13 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)* return L1's TSC value to ensure backwards-compatible * behavior for migration. */ -u64 tsc_offset = msr_info->host_initiated ? vcpu->arch.l1_tsc_offset : - vcpu->arch.tsc_offset; - - msr_info->data =kvm_scale_tsc (vcpu,rdtsc ()) + tsc_offset; +if (msr_info->host_initiated) {Unnecessary curly braces.
Quoted 21 lines
arch/x86/kvm/x86.c +7
-2+ msr_info->data =kvm_scale_tsc_l1 (vcpu,rdtsc ()) + + vcpu->arch.l1_tsc_offset; + }else { + msr_info->data =kvm_scale_tsc (vcpu,rdtsc ()) + + vcpu->arch.tsc_offset; + }break ; }case MSR_MTRRcap:@@ -4123,7 +4138,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)mark_tsc_unstable ("KVM discovered backwards TSC" );if (kvm_check_tsc_unstable ()) { -u64 offset =kvm_compute_tsc_offset (vcpu, +u64 offset =kvm_compute_tsc_offset_l1 (vcpu, vcpu->arch.last_guest_tsc);kvm_vcpu_write_tsc_offset (vcpu, offset); vcpu->arch.tsc_catchup =1 ; --2.17.1
Re: [PATCH v2 06/10] KVM: X86: Add functions that calculate the 02 TSC offset and multiplier
In reply to Sean ChristophersonQuoted 71 lines · Sean Christopherson
Quoted 33 lines · Ilias Stamatis
When L2 is entered we need to "merge" the TSC multiplier and TSC offset values of 01 and 12 together.
The merging is done using the following equations:
offset_02= ((offset_01*mult_12) >>shift_bits) +offset_12mult_02= (mult_01*mult_12) >>shift_bitsWhere
shift_bitsiskvm_tsc_scaling_ratio_frac_bits.Signed-off-by Ilias Stamatis <[email protected]>--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)arch/x86/include/asm/kvm_host.h +2
-0@@ -1793,6 +1793,8 @@ static inline bool kvm_is_supported_user_return_msr(u32 msr)u64 kvm_scale_tsc (struct kvm_vcpu *vcpu,u64 tsc);u64 kvm_scale_tsc_l1 (struct kvm_vcpu *vcpu,u64 tsc);u64 kvm_read_l1_tsc (struct kvm_vcpu *vcpu,u64 host_tsc); +void kvm_set_02_tsc_offset (struct kvm_vcpu *vcpu); +void kvm_set_02_tsc_multiplier (struct kvm_vcpu *vcpu);unsigned long kvm_get_linear_rip (struct kvm_vcpu *vcpu);bool kvm_is_linear_rip (struct kvm_vcpu *vcpu,unsigned long linear_rip);arch/x86/kvm/x86.c +0
-0arch/x86/kvm/x86.c u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) · line 2349
Quoted 4 lines
arch/x86/kvm/x86.c +1
-0@@ -2346,6 +2346,35 @@ u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) }EXPORT_SYMBOL_GPL (kvm_read_l1_tsc); +void kvm_set_02_tsc_offset (struct kvm_vcpu *vcpu)I dislike like the "02" nomenclature. "02" is used specifically to refer to vmcs02 and vmcb02, whereas these helpers touch KVM's software model, not the CPU struct. Can't this simply be "l2"?
Quoted 6 lines
arch/x86/kvm/x86.c +6
-0+{ +u64 l2_offset =static_call (kvm_x86_get_l2_tsc_offset)(vcpu); +u64 l2_multiplier =static_call (kvm_x86_get_l2_tsc_multiplier)(vcpu); + +if (l2_multiplier != kvm_default_tsc_scaling_ratio) { + vcpu->arch.tsc_offset = mul_s64_u64_shr(arch/x86/kvm/x86.c u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) · line 2365
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+ (s64 ) vcpu->arch.l1_tsc_offset, + l2_multiplier, + kvm_tsc_scaling_ratio_frac_bits); + } + + vcpu->arch.tsc_offset += l2_offset; +} +EXPORT_SYMBOL_GPL (kvm_set_02_tsc_offset); + +void kvm_set_02_tsc_multiplier (struct kvm_vcpu *vcpu)I normally like splitting patches gratuitously, but in this case I think it would be better to combine this with the VMX usage in patch 08. It's impossible to properly review this patch without looking at its callers.
arch/x86/kvm/x86.c u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) · line 2367
Quoted 2 lines
arch/x86/kvm/x86.c +2
-0+{ +u64 l2_multiplier =static_call (kvm_x86_get_l2_tsc_multiplier)(vcpu);Case in point, calling back into vendor code to get the L2 multiplier is silly, just have the caller provide it explicitly.
Quoted 8 lines
arch/x86/kvm/x86.c u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) · line 2368
Quoted 1 line
arch/x86/kvm/x86.c +1
-0+if (l2_multiplier != kvm_default_tsc_scaling_ratio) {Why does this check against the default ratio instead of L1's ratio? If L1 is running a non-default ratio, but L2 is running a default ratio, won't this result in KVM leaving
vcpu->arch.tsc_scaling_ratioat L1's ratio? Or is there scaling ratio magic I don't understand (which is likely...)? If there's magic, can you add a comment?
Think of the "default ratio" as a ratio of 1, ie L2 is not scaled (from L1's perspective). So yes, as you say if L1 is running at a non-default ratio, but L2 is running at default ratio (not scaled), this results in KVM leaving arch.tsc_scaling_ratio at L1's ratio (as it should).
I am not sure a comment is needed here.
Having said that, theoretically we could omit this check completely and still get the correct result. But in reality because of the computer math involved there will be a small precision error and the tsc_scaling_ratio ratio won't end up being exactly the same as the l1_tsc_scaling_ratio.
I will implement the rest of your feedback, thanks.
Quoted 17 lines
Same feedback for the check in the offset version.
Quoted 14 lines
arch/x86/kvm/x86.c +8
-1+ vcpu->arch.tsc_scaling_ratio =mul_u64_u64_shr ( + vcpu->arch.l1_tsc_scaling_ratio, + l2_multiplier, + kvm_tsc_scaling_ratio_frac_bits); + } +} +EXPORT_SYMBOL_GPL (kvm_set_02_tsc_multiplier); +static void kvm_vcpu_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) { vcpu->arch.l1_tsc_offset = offset; --2.17.1
Re: [PATCH v2 07/10] KVM: X86: Move write_l1_tsc_offset() logic to common code and rename it
In reply to Sean ChristophersonQuoted 151 lines · Sean Christopherson
Quoted 5 lines · Ilias Stamatis
The
write_l1_tsc_offset()callback has a misleading name. It does not set L1's TSC offset, it rather updates the current TSC offset which might be different if a nested guest is executing. Additionally, both the vmx and svm implementations use the same logic for calculating the current TSC before writing it to hardware.I don't disagree, but the current name as the advantage of clarifying (well, hinting) that the offset is L1's offset. That hint is lost in this refactoring. Maybe rename "u64 offset" to "u64
l1_tsc_offset"?Quoted 1 line
This patch renames the function and moves the common logic to the
Use imperative mood instead of "This patch. From
Documentation/process/submitting-patches.rst:Describe your changes in imperative mood, e.g. "make xyzzy do frotz" instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy to do frotz", as if you are giving orders to the codebase to change its behaviour.
Quoted 130 lines
caller. The vmx/svm-specific code now merely sets the given offset to the corresponding hardware structure.
Signed-off-by Ilias Stamatis <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 2 +- arch/x86/include/asm/kvm_host.h | 3 +-- arch/x86/kvm/svm/svm.c | 21 ++++----------------- arch/x86/kvm/vmx/vmx.c | 23 +++-------------------- arch/x86/kvm/x86.c | 17 ++++++++++++++++- 5 files changed, 25 insertions(+), 41 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-1@@ -89,7 +89,7 @@ KVM_X86_OP(load_mmu_pgd) KVM_X86_OP_NULL(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier) -KVM_X86_OP(write_l1_tsc_offset) +KVM_X86_OP(write_tsc_offset) KVM_X86_OP(get_exit_info) KVM_X86_OP(check_intercept) KVM_X86_OP(handle_exit_irqoff)arch/x86/include/asm/kvm_host.h +1
-2@@ -1307,8 +1307,7 @@ struct kvm_x86_ops {u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);u64 (*get_l2_tsc_multiplier)(struct kvm_vcpu *vcpu); -/* Returns actual tsc_offset set in active VMCS */ -u64 (*write_l1_tsc_offset)(struct kvm_vcpu *vcpu,u64 offset); +void (*write_tsc_offset)(struct kvm_vcpu *vcpu,u64 offset);/* * Retrieve somewhat arbitrary exit information. Intended to be used arch/x86/kvm/svm/svm.c +4
-17@@ -1094,26 +1094,13 @@ static u64 svm_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)return kvm_default_tsc_scaling_ratio; } -static u64 svm_write_l1_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) +static void svm_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) {struct vcpu_svm *svm =to_svm (vcpu); -u64 g_tsc_offset =0 ; - -if (is_guest_mode (vcpu)) { -/* Write L1's TSC offset. */ - g_tsc_offset = svm->vmcb->control.tsc_offset - - svm->vmcb01.ptr->control.tsc_offset; - svm->vmcb01.ptr->control.tsc_offset = offset; - } - -trace_kvm_write_tsc_offset (vcpu->vcpu_id, - svm->vmcb->control.tsc_offset - g_tsc_offset, - offset); - - svm->vmcb->control.tsc_offset = offset + g_tsc_offset; + svm->vmcb01.ptr->control.tsc_offset = vcpu->arch.l1_tsc_offset; + svm->vmcb->control.tsc_offset = offset;vmcb_mark_dirty (svm->vmcb,VMCB_INTERCEPTS ); -return svm->vmcb->control.tsc_offset; }/* Evaluate instruction intercepts that depend on guest CPUID features. */ @@ -4540,7 +4527,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .get_l2_tsc_offset = svm_get_l2_tsc_offset, .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier, - .write_l1_tsc_offset = svm_write_l1_tsc_offset, + .write_tsc_offset = svm_write_tsc_offset, .load_mmu_pgd = svm_load_mmu_pgd,arch/x86/kvm/vmx/vmx.c +3
-20@@ -1810,26 +1810,9 @@ static u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)return multiplier; } -static u64 vmx_write_l1_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) +static void vmx_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) { -struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); -u64 g_tsc_offset =0 ; - -/* -* We're here if L1 chose not to trap WRMSR to TSC. According -* to the spec, this should set L1's TSC; The offset that L1 -* set for L2 remains unchanged, and still needs to be added -* to the newly set TSC to get L2's TSC. -*/ -if (is_guest_mode (vcpu) && - (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING )) - g_tsc_offset = vmcs12->tsc_offset; - -trace_kvm_write_tsc_offset (vcpu->vcpu_id, - vcpu->arch.tsc_offset - g_tsc_offset, - offset); -vmcs_write64 (TSC_OFFSET , offset + g_tsc_offset); -return offset + g_tsc_offset; +vmcs_write64 (TSC_OFFSET , offset); }/* @@ -7725,7 +7708,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .get_l2_tsc_offset = vmx_get_l2_tsc_offset, .get_l2_tsc_multiplier = vmx_get_l2_tsc_multiplier, - .write_l1_tsc_offset = vmx_write_l1_tsc_offset, + .write_tsc_offset = vmx_write_tsc_offset, .load_mmu_pgd = vmx_load_mmu_pgd,arch/x86/kvm/x86.c +0
-0@@ -2377,8 +2377,23 @@ EXPORT_SYMBOL_GPL(kvm_set_02_tsc_multiplier);
Quoted 13 lines
arch/x86/kvm/x86.c EXPORT_SYMBOL_GPL(kvm_set_02_tsc_multiplier); · line 2387
Quoted 11 lines
arch/x86/kvm/x86.c +7
-1static void kvm_vcpu_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) { +trace_kvm_write_tsc_offset (vcpu->vcpu_id, + vcpu->arch.l1_tsc_offset, + offset); + vcpu->arch.l1_tsc_offset = offset; - vcpu->arch.tsc_offset =static_call (kvm_x86_write_l1_tsc_offset)(vcpu, offset); + vcpu->arch.tsc_offset = offset; + +if (is_guest_mode (vcpu)) {Unnecessary curly braces.
Really? We are supposed to have a 6-lines body without brackets? I'm not opposing, I'm just surprised that that's the coding standard.
Quoted 11 lines
arch/x86/kvm/x86.c EXPORT_SYMBOL_GPL(kvm_set_02_tsc_multiplier); · line 2392
Quoted 6 lines
arch/x86/kvm/x86.c +5
-0+/* +* We're here if L1 chose not to trap WRMSR to TSC and +* according to the spec this should set L1's TSC (as opposed +* to setting L1's offset for L2). +*/ While we're shuffling code, can we improve this comment? It works for the WRMSR case, but makes no sense in the context of host TSC adjustments. It's not at all clear to me that it's even correct or relevant in those cases.
Do you suggest removing it completely or how do you want it to be? I don't mind deleting it.
Quoted 27 lines
arch/x86/kvm/x86.c EXPORT_SYMBOL_GPL(kvm_set_02_tsc_multiplier); · line 2393
Quoted 1 line
arch/x86/kvm/x86.c +1
-0+kvm_set_02_tsc_offset (vcpu);I really dislike that
kvm_set_02_tsc_offset()consumes a bunch of variables _and_ sets arch.tsc_offset, e.g. it's not at all obvious that moving this call around will break L2. Even more bizarre is that arch.tsc_offset is conditionally consumed. Oh, andkvm_set_02_tsc_offset()is not idempotent since it can do a RMW on arch.tsc_offset.The below
static_call()dependency doesn't appear to be easily solved, but we can at least strongly hint thatvcpu->arch.tsc_offsetis set. For everything else, I think we can clean things up by doing this (with the vendor calls providing the L2 variables directly):void kvm_set_l2_tsc_offset(struct
kvm_vcpu*vcpu, u64l2_offset,u64 l2_multiplier) { u64 l1_offset = vcpu->arch.l1_tsc_offset; if (l2_multiplier != kvm_default_tsc_scaling_ratio) l2_offset += mul_s64_u64_shr((s64)l1_tsc_offset, l2_multiplier, kvm_tsc_scaling_ratio_frac_bits); vcpu->arch.tsc_offset = l2_offset; }
EXPORT_SYMBOL_GPL(kvm_get_l2_tsc_offset);static void
kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Quoted 17 lines
{
trace_kvm_write_tsc_offset(vcpu->vcpu_id,
vcpu->arch.l1_tsc_offset,
offset);
vcpu->arch.l1_tsc_offset = offset;
if (is_guest_mode(vcpu))
kvm_set_l2_tsc_offset(vcpu,
static_call(kvm_x86_get_l2_tsc_offset)(vcpu),
static_call(kvm_x86_get_l2_tsc_multiplier)(vcpu));
else
vcpu->arch.tsc_offset = offset;
static_call(kvm_x86_write_tsc_offset)(vcpu, vcpu->arch.tsc_offset);
}OK, I will change it to what you suggest above.
Quoted 7 lines
An alternative would be to explicitly track L1 and L2, and _never_ track the current TSC values. I.e. always compute the correct value on the fly. I think the only hot path is the TSC deadline timer, and AFAICT that always runs with L1's timer. Speaking of which, at the end of this series,
vmx_set_hv_timer()uses L1's TSC but the current scaling ratio; that can't be right.
You are right, good catch, I will add this to patch 2.
I think let's leave the vcpu->arch.tsc_scaling_ratio variable as is for now.
Quoted 9 lines
Quoted 9 lines
arch/x86/kvm/x86.c +3
-1+ } + +static_call (kvm_x86_write_tsc_offset)(vcpu, vcpu->arch.tsc_offset); }static inline bool kvm_check_tsc_unstable (void ) --2.17.1
Re: [PATCH v2 08/10] KVM: VMX: Set the TSC offset and multiplier on nested entry and exit
In reply to Sean ChristophersonQuoted 17 lines · Sean Christopherson
Quoted 16 lines · Ilias Stamatis
Now that nested TSC scaling is supported we need to calculate the correct 02 values for both the offset and the multiplier using the corresponding functions. On L2's exit the L1 values are restored.
Signed-off-by Ilias Stamatis <[email protected]>--- arch/x86/kvm/vmx/nested.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/nested.c +0
-0@@ -3354,8 +3354,9 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, }
Quoted 12 lines
arch/x86/kvm/vmx/nested.c enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, · line 3359
Quoted 6 lines
arch/x86/kvm/vmx/nested.c +3
-2enter_guest_mode (vcpu); -if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING ) - vcpu->arch.tsc_offset += vmcs12->tsc_offset; + +kvm_set_02_tsc_offset (vcpu); +kvm_set_02_tsc_multiplier (vcpu);Please move this code into
prepare_vmcs02()to co-locate it with the relevant vmcs02 logic. If there's something inprepare_vmcs02()that consumesvcpu->arch.tsc_offset()other than the obvious VMWRITE, I vote to move things around to fix that rather than create this weird split.
Agreed. It makes more sense.
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx/nested.c +0
-0if (prepare_vmcs02 (vcpu, vmcs12, &entry_failure_code)) { exit_reason.basic =EXIT_REASON_INVALID_STATE ;@@ -4463,8 +4464,12 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,if (nested_cpu_has_preemption_timer (vmcs12))hrtimer_cancel (&to_vmx (vcpu)->nested.preemption_timer);
Quoted 8 lines
arch/x86/kvm/vmx/nested.c void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, · line 4472
Quoted 8 lines
arch/x86/kvm/vmx/nested.c +6
-2-if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING ) - vcpu->arch.tsc_offset -= vmcs12->tsc_offset; +if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING ) { + vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset; + +if (vmcs12->secondary_vm_exec_control &SECONDARY_EXEC_TSC_SCALING ) + vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; + }
I guess these need to stay where they are though.
And thinking about it the two if conditions are unnecessary really.
Thanks for the reviews!
Ilias
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx/nested.c +0
-1if (likely (!vmx->fail)) {sync_vmcs02_to_vmcs12 (vcpu, vmcs12); --2.17.1
Sean Christopherson
maintainerRe: [PATCH v2 03/10] KVM: X86: Add kvm_scale_tsc_l1() and kvm_compute_tsc_offset_l1()
In reply to Stamatis, IliasQuoted 29 lines · Ilias
Quoted 18 lines · Sean Christopherson
Quoted 17 lines · Ilias Stamatis
arch/x86/kvm/x86.c +8
-1@@ -2319,18 +2319,30 @@ u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) }EXPORT_SYMBOL_GPL (kvm_scale_tsc); -static u64 kvm_compute_tsc_offset (struct kvm_vcpu *vcpu,u64 target_tsc) +u64 kvm_scale_tsc_l1 (struct kvm_vcpu *vcpu,u64 tsc) +{ +u64 _tsc = tsc; +u64 ratio = vcpu->arch.l1_tsc_scaling_ratio; + +if (ratio != kvm_default_tsc_scaling_ratio) + _tsc =__scale_tsc (ratio, tsc); +Quoted 9 lines
arch/x86/kvm/x86.c u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) · line 2331
Quoted 2 lines
arch/x86/kvm/x86.c +2
-0+return _tsc; +}Just make the ratio a param. This is complete copy+paste of
kvm_scale_tsc(), with 3 characters added. And all of the callers are already in an L1-specific function or have L1 vs. L2 awareness. IMO, that makes the code less magical, too, as I don't have to dive into a helper to see that it readsl1_tsc_scaling_ratioversustsc_scaling_ratio.
Quoted 2 lines
That's how I did it initially but changed it into a separate function after receiving feedback on v1. I'm neutral, I don't mind changing it back.
Ah, I see the conundrum. The vendor code isn't straightforward because of all the enabling checks against vmcs12 controls.
Given that, I don't terribly mind the callbacks, but I do think the connection between the computation and the VMWRITE needs to be more explicit.
Poking around the code, the other thing that would help would be to get rid of the awful decache_tsc_multiplier(). That helper was added to paper over the completely broken logic of commit ff2c3a180377 ("KVM: VMX: Setup TSC scaling ratio when a vcpu is loaded"). Its use in vmx_vcpu_load_vmcs() is basically "write the VMCS if we forgot to earlier", which is all kinds of wrong.
If we get rid of that stupidity as prep work at the beginning of this series, and have the "setters" return the computed value, the nested VMX code can consume the value directly instead of having the subtle dependency on the helpers.
vmcs_write64(TSC_OFFSET, kvm_calc_l2_tsc_offset(vcpu)); if (kvm_has_tsc_control) vmcs_write64(TSC_MULTIPLIER, kvm_calc_l2_tsc_multiplier(vcpu));
Side topic, the checks against the vmcs12 controls are wrong. Specifically, when checking a secondary execution control, KVM needs to first check that the secondary control is enabled in the primary control. But, we helpers for that. The primary control should use its helper, too. And while you're at it, drop the local variable in the getter. I.e.:
arch/x86/kvm/vmx/vmx.c +4 -5
@@ -1801,13 +1801,12 @@ static u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu)static u64 vmx_get_l2_tsc_multiplier (struct kvm_vcpu *vcpu) {struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); -u64 multiplier = kvm_default_tsc_scaling_ratio; -if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETTING && - vmcs12->secondary_vm_exec_control &SECONDARY_EXEC_TSC_SCALING ) - multiplier = vmcs12->tsc_multiplier; +if (nested_cpu_has (vmcs12,CPU_BASED_USE_TSC_OFFSETTING ) && +nested_cpu_has2 (vmcs12,SECONDARY_EXEC_TSC_SCALING )) +return vmcs12->tsc_multiplier; -return multiplier; +return kvm_default_tsc_scaling_ratio; }
Side topic #2: I now see why the x86.c helpers skip the math if the multiplier is kvm_default_tsc_scaling_ratio.
Sean Christopherson
maintainerRe: [PATCH v2 07/10] KVM: X86: Move write_l1_tsc_offset() logic to common code and rename it
In reply to Stamatis, IliasQuoted 22 lines · Ilias
Quoted 7 lines · Sean Christopherson
Quoted 6 lines · Ilias Stamatis
arch/x86/kvm/x86.c +0
-0@@ -2377,8 +2377,23 @@ EXPORT_SYMBOL_GPL(kvm_set_02_tsc_multiplier);Quoted 13 lines
arch/x86/kvm/x86.c EXPORT_SYMBOL_GPL(kvm_set_02_tsc_multiplier); · line 2387
Quoted 11 lines
arch/x86/kvm/x86.c +7
-1static void kvm_vcpu_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset) { +trace_kvm_write_tsc_offset (vcpu->vcpu_id, + vcpu->arch.l1_tsc_offset, + offset); + vcpu->arch.l1_tsc_offset = offset; - vcpu->arch.tsc_offset =static_call (kvm_x86_write_l1_tsc_offset)(vcpu, offset); + vcpu->arch.tsc_offset = offset; + +if (is_guest_mode (vcpu)) {Unnecessary curly braces.
Quoted 2 lines
Really? We are supposed to have a 6-lines body without brackets? I'm not opposing, I'm just surprised that that's the coding standard.
Comments don't (technically) count. I usually avoid the ambiguity by putting the comment above the if statement. That also helps with indentation, e.g.
/* * This is a comment. */ if (is_guest_mode(vcpu)) kvm_set_02_tsc_offset(vcpu);
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
arch/x86/kvm/x86.c +5
-0+/* +* We're here if L1 chose not to trap WRMSR to TSC and +* according to the spec this should set L1's TSC (as opposed +* to setting L1's offset for L2). +*/
Quoted 7 lines
Quoted 4 lines
While we're shuffling code, can we improve this comment? It works for the WRMSR case, but makes no sense in the context of host TSC adjustments. It's not at all clear to me that it's even correct or relevant in those cases.
Do you suggest removing it completely or how do you want it to be? I don't mind deleting it.
Heh, I'd happily write the comment, except I have no idea what the logic is in the non-WRMSR case. I do think we need a comment, IMO none of paths that lead to changing the TSC offset while L2 is active are obvious.
Re: [PATCH v2 03/10] KVM: X86: Add kvm_scale_tsc_l1() and kvm_compute_tsc_offset_l1()
In reply to Sean ChristophersonQuoted 36 lines · Sean Christopherson
Quoted 29 lines · Ilias
Quoted 18 lines · Sean Christopherson
Quoted 17 lines · Ilias Stamatis
arch/x86/kvm/x86.c +8
-1@@ -2319,18 +2319,30 @@ u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) }EXPORT_SYMBOL_GPL (kvm_scale_tsc); -static u64 kvm_compute_tsc_offset (struct kvm_vcpu *vcpu,u64 target_tsc) +u64 kvm_scale_tsc_l1 (struct kvm_vcpu *vcpu,u64 tsc) +{ +u64 _tsc = tsc; +u64 ratio = vcpu->arch.l1_tsc_scaling_ratio; + +if (ratio != kvm_default_tsc_scaling_ratio) + _tsc =__scale_tsc (ratio, tsc); +Quoted 9 lines
arch/x86/kvm/x86.c u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) · line 2331
Quoted 2 lines
arch/x86/kvm/x86.c +2
-0+return _tsc; +}Just make the ratio a param. This is complete copy+paste of
kvm_scale_tsc(), with 3 characters added. And all of the callers are already in an L1-specific function or have L1 vs. L2 awareness. IMO, that makes the code less magical, too, as I don't have to dive into a helper to see that it readsl1_tsc_scaling_ratioversustsc_scaling_ratio.Quoted 2 lines
That's how I did it initially but changed it into a separate function after receiving feedback on v1. I'm neutral, I don't mind changing it back.
Ah, I see the conundrum. The vendor code isn't straightforward because of all the enabling checks against vmcs12 controls.
Quoted 9 lines
Given that, I don't terribly mind the callbacks, but I do think the connection between the computation and the VMWRITE needs to be more explicit.
Poking around the code, the other thing that would help would be to get rid of the awful
decache_tsc_multiplier(). That helper was added to paper over the completely broken logic of commitff2c3a180377("KVM: VMX: Setup TSC scaling ratio when a vcpu is loaded"). Its use invmx_vcpu_load_vmcs()is basically "write the VMCS if we forgot to earlier", which is all kinds of wrong.
I am going to add a patch that removes decache_tsc_multiplier() and vmx->current_tsc_ratio as the latter is useless since vcpu->arch.tsc_scaling_ratio is already the current ratio. And without it decache_tsc_multiplier() becomes an one-liner that is pointless to have; we can do vmcs_write64() directly.
Nevertheless, I am not going to move the code outside of vmx_vcpu_load_vmcs(). Granted, a better place for setting the multiplier in hardware would be set_tsc_khz(). But this function is inside x86.c so it would require yet another vendor callback to be added, move the svm code too, etc, etc.
Much more refactoring can be done in KVM code in general but I don't think it has to be part of this series. I am going to send the v3 patches tomorrow.
Ilias