Skip to content

Commit 4365a45

Browse files
committed
KVM: nSVM: Use KVM-governed feature framework to track "TSC scaling enabled"
Track "TSC scaling exposed to L1" via a governed feature flag instead of using a dedicated bit/flag in vcpu_svm. Note, this fixes a benign bug where KVM would mark TSC scaling as exposed to L1 even if overall nested SVM supported is disabled, i.e. KVM would let L1 write MSR_AMD64_TSC_RATIO even when KVM didn't advertise TSCRATEMSR support to userspace. Reviewed-by: Yuan Yao <yuan.yao@intel.com> Link: https://lore.kernel.org/r/20230815203653.519297-10-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 7a6a6a3 commit 4365a45

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

arch/x86/kvm/governed_features.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ KVM_GOVERNED_X86_FEATURE(GBPAGES)
99
KVM_GOVERNED_X86_FEATURE(XSAVES)
1010
KVM_GOVERNED_X86_FEATURE(VMX)
1111
KVM_GOVERNED_X86_FEATURE(NRIPS)
12+
KVM_GOVERNED_X86_FEATURE(TSCRATEMSR)
1213

1314
#undef KVM_GOVERNED_X86_FEATURE
1415
#undef KVM_GOVERNED_FEATURE

arch/x86/kvm/svm/nested.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
695695

696696
vmcb02->control.tsc_offset = vcpu->arch.tsc_offset;
697697

698-
if (svm->tsc_scaling_enabled &&
698+
if (guest_can_use(vcpu, X86_FEATURE_TSCRATEMSR) &&
699699
svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio)
700700
nested_svm_update_tsc_ratio_msr(vcpu);
701701

arch/x86/kvm/svm/svm.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,7 +2795,8 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
27952795

27962796
switch (msr_info->index) {
27972797
case MSR_AMD64_TSC_RATIO:
2798-
if (!msr_info->host_initiated && !svm->tsc_scaling_enabled)
2798+
if (!msr_info->host_initiated &&
2799+
!guest_can_use(vcpu, X86_FEATURE_TSCRATEMSR))
27992800
return 1;
28002801
msr_info->data = svm->tsc_ratio_msr;
28012802
break;
@@ -2937,7 +2938,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
29372938
switch (ecx) {
29382939
case MSR_AMD64_TSC_RATIO:
29392940

2940-
if (!svm->tsc_scaling_enabled) {
2941+
if (!guest_can_use(vcpu, X86_FEATURE_TSCRATEMSR)) {
29412942

29422943
if (!msr->host_initiated)
29432944
return 1;
@@ -2959,7 +2960,8 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
29592960

29602961
svm->tsc_ratio_msr = data;
29612962

2962-
if (svm->tsc_scaling_enabled && is_guest_mode(vcpu))
2963+
if (guest_can_use(vcpu, X86_FEATURE_TSCRATEMSR) &&
2964+
is_guest_mode(vcpu))
29632965
nested_svm_update_tsc_ratio_msr(vcpu);
29642966

29652967
break;
@@ -4260,8 +4262,8 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
42604262
kvm_governed_feature_set(vcpu, X86_FEATURE_XSAVES);
42614263

42624264
kvm_governed_feature_check_and_set(vcpu, X86_FEATURE_NRIPS);
4265+
kvm_governed_feature_check_and_set(vcpu, X86_FEATURE_TSCRATEMSR);
42634266

4264-
svm->tsc_scaling_enabled = tsc_scaling && guest_cpuid_has(vcpu, X86_FEATURE_TSCRATEMSR);
42654267
svm->lbrv_enabled = lbrv && guest_cpuid_has(vcpu, X86_FEATURE_LBRV);
42664268

42674269
svm->v_vmload_vmsave_enabled = vls && guest_cpuid_has(vcpu, X86_FEATURE_V_VMSAVE_VMLOAD);

arch/x86/kvm/svm/svm.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ struct vcpu_svm {
259259
bool soft_int_injected;
260260

261261
/* optional nested SVM features that are enabled for this guest */
262-
bool tsc_scaling_enabled : 1;
263262
bool v_vmload_vmsave_enabled : 1;
264263
bool lbrv_enabled : 1;
265264
bool pause_filter_enabled : 1;

0 commit comments

Comments
 (0)