KVM: VMX: nVMX: Make eVMCS enablement more robust
Subsystems (5)
v2 cover KVM: VMX: nVMX: Make eVMCS enablement more robust
Changes since v1:
- Simplify PATCH4 by not using evmcs_get_supported_ctls() and doing more macro magic to avoid having two evmcs_check_vmcs_conf{u32, u64} variants [Sean]
This is a continuation of "KVM: VMX: Support updated eVMCSv1 revision + use vmcs_config for L1 VMX MSRs" work:
https://lore.kernel.org/kvm/[email protected]/
and a preparation to enabling new eVMCS features for Hyper-V on KVM, namely nested TSC scaling.
Future proof KVM against two scenarios:
- nVMX: A new feature which doesn't have a corresponding eVMCSv1 field gets implemented in KVM but EVMCS1_UNSUPPORTED_* defines are left unchanged.
- VMX: A new feature supported by KVM but currently missing in eVMCSv1 gets implemented in a future Hyper-V version breaking KVM.
Note: 'vmx/evmcs.{c,h}' are renamed to 'vmx/hyperv.{c,h}' in
https://lore.kernel.org/kvm/[email protected]/
Vitaly Kuznetsov (4):
KVM: nVMX: Sanitize primary processor-based VM-execution controls with
eVMCS too
KVM: nVMX: Invert 'unsupported by eVMCSv1' check
KVM: nVMX: Prepare to sanitize tertiary execution controls with eVMCS
KVM: VMX: Resurrect vmcs_conf sanitization for KVM-on-Hyper-V
arch/x86/kvm/vmx/evmcs.c | 88 +++++++++++++++++++++++++++++-------- arch/x86/kvm/vmx/evmcs.h | 93 +++++++++++++++++++++++++++++++++------- arch/x86/kvm/vmx/vmx.c | 5 +++ 3 files changed, 152 insertions(+), 34 deletions(-)
Signature · 1 line
2.38.1
PATCH v2 1/4 KVM: nVMX: Sanitize primary processor-based VM-execution controls with eVMCS too
The only unsupported primary processor-based VM-execution control at the moment is CPU_BASED_ACTIVATE_TERTIARY_CONTROLS and KVM doesn't expose it in nested VMX feature MSRs anyway (see nested_vmx_setup_ctls_msrs()) but in preparation to inverting "unsupported with eVMCS" checks (and for completeness) it's better to sanitize MSR_IA32_VMX_PROCBASED_CTLS/ MSR_IA32_VMX_TRUE_PROCBASED_CTLS too.
No functional change intended.
--- arch/x86/kvm/vmx/evmcs.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/x86/kvm/vmx/evmcs.c +12 -0
@@ -368,6 +368,7 @@ enum evmcs_revision {enum evmcs_ctrl_type {EVMCS_EXIT_CTRLS ,EVMCS_ENTRY_CTRLS , +EVMCS_EXEC_CTRL ,EVMCS_2NDEXEC ,EVMCS_PINCTRL ,EVMCS_VMFUNC ,
@@ -381,6 +382,9 @@ static const u32 evmcs_unsupported_ctrls[NR_EVMCS_CTRLS][NR_EVMCS_REVISIONS] = { [EVMCS_ENTRY_CTRLS] = { [EVMCSv1_LEGACY] = EVMCS1_UNSUPPORTED_VMENTRY_CTRL, }, + [EVMCS_EXEC_CTRL] = { + [EVMCSv1_LEGACY] = EVMCS1_UNSUPPORTED_EXEC_CTRL, + }, [EVMCS_2NDEXEC] = { [EVMCSv1_LEGACY] = EVMCS1_UNSUPPORTED_2NDEXEC, },
@@ -441,6 +445,10 @@ void nested_evmcs_filter_control_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 * unsupported_ctrls |=VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; ctl_high &= ~unsupported_ctrls;break ; +case MSR_IA32_VMX_PROCBASED_CTLS : +case MSR_IA32_VMX_TRUE_PROCBASED_CTLS : + ctl_high &= ~evmcs_get_unsupported_ctls (EVMCS_EXEC_CTRL ); +break ;case MSR_IA32_VMX_PROCBASED_CTLS2 : ctl_high &= ~evmcs_get_unsupported_ctls (EVMCS_2NDEXEC );break ;
@@ -468,6 +476,10 @@ int nested_evmcs_check_controls(struct vmcs12 *vmcs12) vmcs12->pin_based_vm_exec_control)))return -EINVAL ; +if (CC (!nested_evmcs_is_valid_controls (EVMCS_EXEC_CTRL , + vmcs12->cpu_based_vm_exec_control))) +return -EINVAL ; +if (CC (!nested_evmcs_is_valid_controls (EVMCS_2NDEXEC , vmcs12->secondary_vm_exec_control)))return -EINVAL ;
Signature · 1 line
2.38.1
PATCH v2 2/4 KVM: nVMX: Invert 'unsupported by eVMCSv1' check
In reply to Vitaly KuznetsovWhen a new feature gets implemented in KVM, EVMCS1_UNSUPPORTED_* defines need to be adjusted to avoid the situation when the feature is exposed to the guest but there's no corresponding eVMCS field[s] for it. This is not obvious and fragile. Invert 'unsupported by eVMCSv1' check and make it 'supported by eVMCSv1' instead, this way it's much harder to make a mistake. New features will get added to EVMCS1_SUPPORTED_* defines when the corresponding fields are added to eVMCS definition.
No functional change intended. EVMCS1_SUPPORTED_* defines are composed by taking KVM_{REQUIRED,OPTIONAL}_VMX_ defines and filtering out what was previously known as EVMCS1_UNSUPPORTED_*.
From all the controls, SECONDARY_EXEC_TSC_SCALING requires special handling as it's actually present in eVMCSv1 definition but is not currently supported for Hyper-V-on-KVM, just for KVM-on-Hyper-V. As evmcs_supported_ctrls will be used for both scenarios, just add it there instead of EVMCS1_SUPPORTED_2NDEXEC.
--- arch/x86/kvm/vmx/evmcs.c | 42 +++++++++---------- arch/x86/kvm/vmx/evmcs.h | 90 +++++++++++++++++++++++++++++++++------- 2 files changed, 96 insertions(+), 36 deletions(-)
arch/x86/kvm/vmx/evmcs.c +21 -21
@@ -375,32 +375,32 @@ enum evmcs_ctrl_type { NR_EVMCS_CTRLS, }; -static const u32 evmcs_unsupported_ctrls[NR_EVMCS_CTRLS ][NR_EVMCS_REVISIONS ] = { +static const u32 evmcs_supported_ctrls[NR_EVMCS_CTRLS ][NR_EVMCS_REVISIONS ] = { [EVMCS_EXIT_CTRLS ] = { - [EVMCSv1_LEGACY] =EVMCS1_UNSUPPORTED_VMEXIT_CTRL , + [EVMCSv1_LEGACY] =EVMCS1_SUPPORTED_VMEXIT_CTRL , }, [EVMCS_ENTRY_CTRLS ] = { - [EVMCSv1_LEGACY] =EVMCS1_UNSUPPORTED_VMENTRY_CTRL , + [EVMCSv1_LEGACY] =EVMCS1_SUPPORTED_VMENTRY_CTRL , }, [EVMCS_EXEC_CTRL ] = { - [EVMCSv1_LEGACY] =EVMCS1_UNSUPPORTED_EXEC_CTRL , + [EVMCSv1_LEGACY] =EVMCS1_SUPPORTED_EXEC_CTRL , }, [EVMCS_2NDEXEC ] = { - [EVMCSv1_LEGACY] =EVMCS1_UNSUPPORTED_2NDEXEC , + [EVMCSv1_LEGACY] =EVMCS1_SUPPORTED_2NDEXEC & ~SECONDARY_EXEC_TSC_SCALING , }, [EVMCS_PINCTRL ] = { - [EVMCSv1_LEGACY] =EVMCS1_UNSUPPORTED_PINCTRL , + [EVMCSv1_LEGACY] =EVMCS1_SUPPORTED_PINCTRL , }, [EVMCS_VMFUNC ] = { - [EVMCSv1_LEGACY] =EVMCS1_UNSUPPORTED_VMFUNC , + [EVMCSv1_LEGACY] =EVMCS1_SUPPORTED_VMFUNC , }, }; -static u32 evmcs_get_unsupported_ctls (enum evmcs_ctrl_type ctrl_type) +static u32 evmcs_get_supported_ctls (enum evmcs_ctrl_type ctrl_type) {enum evmcs_revision evmcs_rev = EVMCSv1_LEGACY; -return evmcs_unsupported_ctrls[ctrl_type][evmcs_rev]; +return evmcs_supported_ctrls[ctrl_type][evmcs_rev]; }static bool evmcs_has_perf_global_ctrl (struct kvm_vcpu *vcpu)
@@ -424,7 +424,7 @@ void nested_evmcs_filter_control_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 * {u32 ctl_low = (u32)*pdata;u32 ctl_high = (u32)(*pdata >>32 ); -u32 unsupported_ctrls; +u32 supported_ctrls;/* * Hyper-V 2016 and 2019 try using these features even when eVMCS
@@ -433,31 +433,31 @@ void nested_evmcs_filter_control_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *switch (msr_index) {case MSR_IA32_VMX_EXIT_CTLS :case MSR_IA32_VMX_TRUE_EXIT_CTLS : - unsupported_ctrls =evmcs_get_unsupported_ctls (EVMCS_EXIT_CTRLS ); + supported_ctrls =evmcs_get_supported_ctls (EVMCS_EXIT_CTRLS );if (!evmcs_has_perf_global_ctrl (vcpu)) - unsupported_ctrls |=VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; - ctl_high &= ~unsupported_ctrls; + supported_ctrls &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; + ctl_high &= supported_ctrls;break ;case MSR_IA32_VMX_ENTRY_CTLS :case MSR_IA32_VMX_TRUE_ENTRY_CTLS : - unsupported_ctrls =evmcs_get_unsupported_ctls (EVMCS_ENTRY_CTRLS ); + supported_ctrls =evmcs_get_supported_ctls (EVMCS_ENTRY_CTRLS );if (!evmcs_has_perf_global_ctrl (vcpu)) - unsupported_ctrls |=VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; - ctl_high &= ~unsupported_ctrls; + supported_ctrls &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; + ctl_high &= supported_ctrls;break ;case MSR_IA32_VMX_PROCBASED_CTLS :case MSR_IA32_VMX_TRUE_PROCBASED_CTLS : - ctl_high &= ~evmcs_get_unsupported_ctls (EVMCS_EXEC_CTRL ); + ctl_high &=evmcs_get_supported_ctls (EVMCS_EXEC_CTRL );break ;case MSR_IA32_VMX_PROCBASED_CTLS2 : - ctl_high &= ~evmcs_get_unsupported_ctls (EVMCS_2NDEXEC ); + ctl_high &=evmcs_get_supported_ctls (EVMCS_2NDEXEC );break ;case MSR_IA32_VMX_TRUE_PINBASED_CTLS :case MSR_IA32_VMX_PINBASED_CTLS : - ctl_high &= ~evmcs_get_unsupported_ctls (EVMCS_PINCTRL ); + ctl_high &=evmcs_get_supported_ctls (EVMCS_PINCTRL );break ;case MSR_IA32_VMX_VMFUNC : - ctl_low &= ~evmcs_get_unsupported_ctls (EVMCS_VMFUNC ); + ctl_low &=evmcs_get_supported_ctls (EVMCS_VMFUNC );break ; }
@@ -467,7 +467,7 @@ void nested_evmcs_filter_control_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *static bool nested_evmcs_is_valid_controls (enum evmcs_ctrl_type ctrl_type,u32 val) { -return !(val &evmcs_get_unsupported_ctls (ctrl_type)); +return !(val & ~evmcs_get_supported_ctls (ctrl_type)); }int nested_evmcs_check_controls (struct vmcs12 *vmcs12)
arch/x86/kvm/vmx/evmcs.h +75 -15
97 lines not on this page · raw message
Signature · 1 line
2.38.1
PATCH v2 3/4 KVM: nVMX: Prepare to sanitize tertiary execution controls with eVMCS
In reply to Vitaly KuznetsovIn preparation to restoring vmcs_conf sanitization for KVM-on-Hyper-V, (and for completeness) add tertiary VM-execution controls to 'evmcs_supported_ctrls'.
No functional change intended as KVM doesn't yet expose MSR_IA32_VMX_PROCBASED_CTLS3 to its guests.
--- arch/x86/kvm/vmx/evmcs.c | 4 ++++ arch/x86/kvm/vmx/evmcs.h | 2 ++ 2 files changed, 6 insertions(+)
arch/x86/kvm/vmx/evmcs.c +4 -0
@@ -370,6 +370,7 @@ enum evmcs_ctrl_type {EVMCS_ENTRY_CTRLS ,EVMCS_EXEC_CTRL ,EVMCS_2NDEXEC , +EVMCS_3RDEXEC ,EVMCS_PINCTRL ,EVMCS_VMFUNC , NR_EVMCS_CTRLS,
@@ -388,6 +389,9 @@ static const u32 evmcs_supported_ctrls[NR_EVMCS_CTRLS][NR_EVMCS_REVISIONS] = { [EVMCS_2NDEXEC] = { [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_2NDEXEC & ~SECONDARY_EXEC_TSC_SCALING, }, + [EVMCS_3RDEXEC] = { + [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_3RDEXEC, + }, [EVMCS_PINCTRL] = { [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_PINCTRL, },
arch/x86/kvm/vmx/evmcs.h +2 -0
@@ -96,6 +96,8 @@ DECLARE_STATIC_KEY_FALSE(enable_evmcs); SECONDARY_EXEC_NOTIFY_VM_EXITING | \ SECONDARY_EXEC_ENCLS_EXITING) +#define EVMCS1_SUPPORTED_3RDEXEC (0ULL) +#define EVMCS1_SUPPORTED_VMEXIT_CTRL \ (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | \ VM_EXIT_SAVE_DEBUG_CONTROLS | \
Signature · 1 line
2.38.1
PATCH v2 4/4 KVM: VMX: Resurrect vmcs_conf sanitization for KVM-on-Hyper-V
In reply to Vitaly KuznetsovCommit 9bcb90650e31 ("KVM: VMX: Get rid of eVMCS specific VMX controls sanitization") dropped 'vmcs_conf' sanitization for KVM-on-Hyper-V because there's no known Hyper-V version which would expose a feature unsupported in eVMCS in VMX feature MSRs. This works well for all currently existing Hyper-V version, however, future Hyper-V versions may add features which are supported by KVM and are currently missing in eVMCSv1 definition (e.g. APIC virtualization, PML,...). When this happens, existing KVMs will get broken. With the inverted 'unsupported by eVMCSv1' checks, we can resurrect vmcs_conf sanitization and make KVM future proof.
--- arch/x86/kvm/vmx/evmcs.c | 34 ++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/evmcs.h | 1 + arch/x86/kvm/vmx/vmx.c | 5 +++++ 3 files changed, 40 insertions(+)
arch/x86/kvm/vmx/evmcs.c +34 -0
@@ -1,5 +1,7 @@// SPDX-License-Identifier: GPL-2.0 +#define pr_fmt (fmt) "kvm/hyper-v: " fmt +#include <linux/errno.h> #include <linux/smp.h>
@@ -511,6 +513,38 @@ int nested_evmcs_check_controls(struct vmcs12 *vmcs12)return 0 ; } +#if IS_ENABLED (CONFIG_HYPERV ) +/* +* KVM on Hyper-V always uses the latest known eVMCSv1 revision, the assumption +* is: in case a feature has corresponding fields in eVMCS described and it was +* exposed in VMX feature MSRs, KVM is free to use it. Warn if KVM meets a +* feature which has no corresponding eVMCS field, this likely means that KVM +* needs to be updated. +*/ +#define evmcs_check_vmcs_conf (field, ctrl) \ + do { \ + typeof(vmcs_conf->field) unsupported; \ + \ + unsupported = vmcs_conf->field & ~EVMCS1_SUPPORTED_ ## ctrl; \ + if (unsupported) { \ + pr_warn_once(#field " unsupported with eVMCS: 0x%llx\n",\ + (u64)unsupported); \ + vmcs_conf->field &= EVMCS1_SUPPORTED_ ## ctrl; \ + } \ + } \ + while (0) + +__init void evmcs_sanitize_exec_ctrls (struct vmcs_config *vmcs_conf) +{ +evmcs_check_vmcs_conf (cpu_based_exec_ctrl,EXEC_CTRL ); +evmcs_check_vmcs_conf (pin_based_exec_ctrl,PINCTRL ); + evmcs_check_vmcs_conf(cpu_based_2nd_exec_ctrl, 2NDEXEC); + evmcs_check_vmcs_conf(cpu_based_3rd_exec_ctrl, 3RDEXEC); +evmcs_check_vmcs_conf (vmentry_ctrl,VMENTRY_CTRL ); +evmcs_check_vmcs_conf (vmexit_ctrl,VMEXIT_CTRL ); +} +#endif +int nested_enable_evmcs (struct kvm_vcpu *vcpu,uint16_t *vmcs_version) {
arch/x86/kvm/vmx/evmcs.h +1 -0
@@ -271,6 +271,7 @@ static inline void evmcs_load(u64 phys_addr) vp_ap->enlighten_vmentry =1 ; } +__init void evmcs_sanitize_exec_ctrls (struct vmcs_config *vmcs_conf);#else /* !IS_ENABLED(CONFIG_HYPERV) */ static __always_inline void evmcs_write64(unsigned long field, u64 value) {}static inline void evmcs_write32 (unsigned long field,u32 value) {}
arch/x86/kvm/vmx/vmx.c +5 -0
@@ -2721,6 +2721,11 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf, vmcs_conf->vmentry_ctrl = _vmentry_control; vmcs_conf->misc = misc_msr; +#if IS_ENABLED (CONFIG_HYPERV ) +if (enlightened_vmcs) +evmcs_sanitize_exec_ctrls (vmcs_conf); +#endif +return 0 ; }
Signature · 1 line
2.38.1
Queued (for 6.3), thanks.
Paolo