Skip to content

Commit 1c18efd

Browse files
committed
KVM: nVMX: Use KVM-governed feature framework to track "nested VMX enabled"
Track "VMX exposed to L1" via a governed feature flag instead of using a dedicated helper to provide the same functionality. The main goal is to drive convergence between VMX and SVM with respect to querying features that are controllable via module param (SVM likes to cache nested features), avoiding the guest CPUID lookups at runtime is just a bonus and unlikely to provide any meaningful performance benefits. Note, X86_FEATURE_VMX is set in kvm_cpu_caps if and only if "nested" is true, and the CPU obviously supports VMX if KVM+VMX is running. I.e. the check on "nested" is now implicitly down by the kvm_cpu_cap_has() check in kvm_governed_feature_check_and_set(). No functional change intended. Reviewed-by: Yuan Yao <yuan.yao@intel.com> Reviwed-by: Kai Huang <kai.huang@intel.com> Link: https://lore.kernel.org/r/20230815203653.519297-8-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent fe60e8f commit 1c18efd

File tree

4 files changed

+11
-19
lines changed

4 files changed

+11
-19
lines changed

arch/x86/kvm/governed_features.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ BUILD_BUG()
77

88
KVM_GOVERNED_X86_FEATURE(GBPAGES)
99
KVM_GOVERNED_X86_FEATURE(XSAVES)
10+
KVM_GOVERNED_X86_FEATURE(VMX)
1011

1112
#undef KVM_GOVERNED_X86_FEATURE
1213
#undef KVM_GOVERNED_FEATURE

arch/x86/kvm/vmx/nested.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6426,7 +6426,7 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
64266426
vmx = to_vmx(vcpu);
64276427
vmcs12 = get_vmcs12(vcpu);
64286428

6429-
if (nested_vmx_allowed(vcpu) &&
6429+
if (guest_can_use(vcpu, X86_FEATURE_VMX) &&
64306430
(vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
64316431
kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
64326432
kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
@@ -6567,7 +6567,7 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
65676567
if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
65686568
return -EINVAL;
65696569
} else {
6570-
if (!nested_vmx_allowed(vcpu))
6570+
if (!guest_can_use(vcpu, X86_FEATURE_VMX))
65716571
return -EINVAL;
65726572

65736573
if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
@@ -6601,7 +6601,8 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
66016601
return -EINVAL;
66026602

66036603
if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6604-
(!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
6604+
(!guest_can_use(vcpu, X86_FEATURE_VMX) ||
6605+
!vmx->nested.enlightened_vmcs_enabled))
66056606
return -EINVAL;
66066607

66076608
vmx_leave_nested(vcpu);

arch/x86/kvm/vmx/vmx.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,17 +1908,6 @@ static void vmx_write_tsc_multiplier(struct kvm_vcpu *vcpu)
19081908
vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
19091909
}
19101910

1911-
/*
1912-
* nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1913-
* instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1914-
* all guests if the "nested" module option is off, and can also be disabled
1915-
* for a single guest by disabling its VMX cpuid bit.
1916-
*/
1917-
bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
1918-
{
1919-
return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
1920-
}
1921-
19221911
/*
19231912
* Userspace is allowed to set any supported IA32_FEATURE_CONTROL regardless of
19241913
* guest CPUID. Note, KVM allows userspace to set "VMX in SMX" to maintain
@@ -2046,7 +2035,7 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
20462035
[msr_info->index - MSR_IA32_SGXLEPUBKEYHASH0];
20472036
break;
20482037
case KVM_FIRST_EMULATED_VMX_MSR ... KVM_LAST_EMULATED_VMX_MSR:
2049-
if (!nested_vmx_allowed(vcpu))
2038+
if (!guest_can_use(vcpu, X86_FEATURE_VMX))
20502039
return 1;
20512040
if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
20522041
&msr_info->data))
@@ -2354,7 +2343,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
23542343
case KVM_FIRST_EMULATED_VMX_MSR ... KVM_LAST_EMULATED_VMX_MSR:
23552344
if (!msr_info->host_initiated)
23562345
return 1; /* they are read-only */
2357-
if (!nested_vmx_allowed(vcpu))
2346+
if (!guest_can_use(vcpu, X86_FEATURE_VMX))
23582347
return 1;
23592348
return vmx_set_vmx_msr(vcpu, msr_index, data);
23602349
case MSR_IA32_RTIT_CTL:
@@ -7748,13 +7737,15 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
77487737
guest_cpuid_has(vcpu, X86_FEATURE_XSAVE))
77497738
kvm_governed_feature_check_and_set(vcpu, X86_FEATURE_XSAVES);
77507739

7740+
kvm_governed_feature_check_and_set(vcpu, X86_FEATURE_VMX);
7741+
77517742
vmx_setup_uret_msrs(vmx);
77527743

77537744
if (cpu_has_secondary_exec_ctrls())
77547745
vmcs_set_secondary_exec_control(vmx,
77557746
vmx_secondary_exec_control(vmx));
77567747

7757-
if (nested_vmx_allowed(vcpu))
7748+
if (guest_can_use(vcpu, X86_FEATURE_VMX))
77587749
vmx->msr_ia32_feature_control_valid_bits |=
77597750
FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
77607751
FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
@@ -7763,7 +7754,7 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
77637754
~(FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
77647755
FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX);
77657756

7766-
if (nested_vmx_allowed(vcpu))
7757+
if (guest_can_use(vcpu, X86_FEATURE_VMX))
77677758
nested_vmx_cr_fixed1_bits_update(vcpu);
77687759

77697760
if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&

arch/x86/kvm/vmx/vmx.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ struct kvm_vmx {
374374
u64 *pid_table;
375375
};
376376

377-
bool nested_vmx_allowed(struct kvm_vcpu *vcpu);
378377
void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
379378
struct loaded_vmcs *buddy);
380379
int allocate_vpid(void);

0 commit comments

Comments
 (0)