Commit c82a5c5c authored by Chao Gao's avatar Chao Gao Committed by Paolo Bonzini

KVM: x86: Do compatibility checks when onlining CPU

Do compatibility checks when enabling hardware to effectively add
compatibility checks when onlining a CPU.  Abort enabling, i.e. the
online process, if the (hotplugged) CPU is incompatible with the known
good setup.

At init time, KVM does compatibility checks to ensure that all online
CPUs support hardware virtualization and a common set of features. But
KVM uses hotplugged CPUs without such compatibility checks. On Intel
CPUs, this leads to #GP if the hotplugged CPU doesn't support VMX, or
VM-Entry failure if the hotplugged CPU doesn't support all features
enabled by KVM.

Note, this is little more than a NOP on SVM, as SVM already checks for
full SVM support during hardware enabling.

Opportunistically add a pr_err() if setup_vmcs_config() fails, and
tweak all error messages to output which CPU failed.
Signed-off-by: default avatarChao Gao <chao.gao@intel.com>
Co-developed-by: default avatarSean Christopherson <seanjc@google.com>
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Acked-by: default avatarKai Huang <kai.huang@intel.com>
Message-Id: <20221130230934.1014142-41-seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent d83420c2
...@@ -521,11 +521,12 @@ static void svm_init_osvw(struct kvm_vcpu *vcpu) ...@@ -521,11 +521,12 @@ static void svm_init_osvw(struct kvm_vcpu *vcpu)
static bool kvm_is_svm_supported(void) static bool kvm_is_svm_supported(void)
{ {
int cpu = raw_smp_processor_id();
const char *msg; const char *msg;
u64 vm_cr; u64 vm_cr;
if (!cpu_has_svm(&msg)) { if (!cpu_has_svm(&msg)) {
pr_err("SVM not supported, %s\n", msg); pr_err("SVM not supported by CPU %d, %s\n", cpu, msg);
return false; return false;
} }
...@@ -536,7 +537,7 @@ static bool kvm_is_svm_supported(void) ...@@ -536,7 +537,7 @@ static bool kvm_is_svm_supported(void)
rdmsrl(MSR_VM_CR, vm_cr); rdmsrl(MSR_VM_CR, vm_cr);
if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) { if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) {
pr_err("SVM disabled (by BIOS) in MSR_VM_CR\n"); pr_err("SVM disabled (by BIOS) in MSR_VM_CR on CPU %d\n", cpu);
return false; return false;
} }
...@@ -587,9 +588,6 @@ static int svm_hardware_enable(void) ...@@ -587,9 +588,6 @@ static int svm_hardware_enable(void)
if (efer & EFER_SVME) if (efer & EFER_SVME)
return -EBUSY; return -EBUSY;
if (!kvm_is_svm_supported())
return -EINVAL;
sd = per_cpu_ptr(&svm_data, me); sd = per_cpu_ptr(&svm_data, me);
sd->asid_generation = 1; sd->asid_generation = 1;
sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
......
...@@ -2747,14 +2747,16 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, ...@@ -2747,14 +2747,16 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
static bool kvm_is_vmx_supported(void) static bool kvm_is_vmx_supported(void)
{ {
int cpu = raw_smp_processor_id();
if (!cpu_has_vmx()) { if (!cpu_has_vmx()) {
pr_err("CPU doesn't support VMX\n"); pr_err("VMX not supported by CPU %d\n", cpu);
return false; return false;
} }
if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) || if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
!this_cpu_has(X86_FEATURE_VMX)) { !this_cpu_has(X86_FEATURE_VMX)) {
pr_err("VMX not enabled (by BIOS) in MSR_IA32_FEAT_CTL\n"); pr_err("VMX not enabled (by BIOS) in MSR_IA32_FEAT_CTL on CPU %d\n", cpu);
return false; return false;
} }
...@@ -2763,18 +2765,21 @@ static bool kvm_is_vmx_supported(void) ...@@ -2763,18 +2765,21 @@ static bool kvm_is_vmx_supported(void)
static int vmx_check_processor_compat(void) static int vmx_check_processor_compat(void)
{ {
int cpu = raw_smp_processor_id();
struct vmcs_config vmcs_conf; struct vmcs_config vmcs_conf;
struct vmx_capability vmx_cap; struct vmx_capability vmx_cap;
if (!kvm_is_vmx_supported()) if (!kvm_is_vmx_supported())
return -EIO; return -EIO;
if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) {
pr_err("Failed to setup VMCS config on CPU %d\n", cpu);
return -EIO; return -EIO;
}
if (nested) if (nested)
nested_vmx_setup_ctls_msrs(&vmcs_conf, vmx_cap.ept); nested_vmx_setup_ctls_msrs(&vmcs_conf, vmx_cap.ept);
if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config))) {
pr_err("CPU %d feature inconsistency!\n", smp_processor_id()); pr_err("Inconsistent VMCS config on CPU %d\n", cpu);
return -EIO; return -EIO;
} }
return 0; return 0;
......
...@@ -11980,6 +11980,11 @@ int kvm_arch_hardware_enable(void) ...@@ -11980,6 +11980,11 @@ int kvm_arch_hardware_enable(void)
bool stable, backwards_tsc = false; bool stable, backwards_tsc = false;
kvm_user_return_msr_cpu_online(); kvm_user_return_msr_cpu_online();
ret = kvm_x86_check_processor_compatibility();
if (ret)
return ret;
ret = static_call(kvm_x86_hardware_enable)(); ret = static_call(kvm_x86_hardware_enable)();
if (ret != 0) if (ret != 0)
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment