Commit 3f2757db authored by Sean Christopherson's avatar Sean Christopherson
Browse files

KVM: x86: Harden against unexpected adjustments to kvm_cpu_caps



Add a flag to track when KVM is actively configuring its CPU caps, and
WARN if a cap is set or cleared if KVM isn't in its configuration stage.
Modifying CPU caps after {svm,vmx}_set_cpu_caps() can be fatal to KVM, as
vendor setup code expects the CPU caps to be frozen at that point, e.g.
will do additional configuration based on the caps.

Rename kvm_set_cpu_caps() to kvm_initialize_cpu_caps() to pair with the
new "finalize", and to make it more obvious that KVM's CPU caps aren't
fully configured within the function.

Reviewed-by: default avatarXiaoyao Li <xiaoyao.li@intel.com>
Reviewed-by: default avatarBinbin Wu <binbin.wu@linux.intel.com>
Link: https://patch.msgid.link/20260128014310.3255561-3-seanjc@google.com


Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
parent 95d848dc
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -36,6 +36,9 @@
u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_cpu_caps);

bool kvm_is_configuring_cpu_caps __read_mostly;
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_is_configuring_cpu_caps);

struct cpuid_xstate_sizes {
	u32 eax;
	u32 ebx;
@@ -826,10 +829,13 @@ do { \
/* DS is defined by ptrace-abi.h on 32-bit builds. */
#undef DS

void kvm_set_cpu_caps(void)
void kvm_initialize_cpu_caps(void)
{
	memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps));

	WARN_ON_ONCE(kvm_is_configuring_cpu_caps);
	kvm_is_configuring_cpu_caps = true;

	BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
		     sizeof(boot_cpu_data.x86_capability));

@@ -1288,7 +1294,7 @@ void kvm_set_cpu_caps(void)
		kvm_cpu_cap_clear(X86_FEATURE_RDPID);
	}
}
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_set_cpu_caps);
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_initialize_cpu_caps);

#undef F
#undef SCATTERED_F
+11 −1
Original line number Diff line number Diff line
@@ -8,7 +8,15 @@
#include <uapi/asm/kvm_para.h>

extern u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
void kvm_set_cpu_caps(void);
extern bool kvm_is_configuring_cpu_caps __read_mostly;

void kvm_initialize_cpu_caps(void);

static inline void kvm_finalize_cpu_caps(void)
{
	WARN_ON_ONCE(!kvm_is_configuring_cpu_caps);
	kvm_is_configuring_cpu_caps = false;
}

void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu);
struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2(struct kvm_cpuid_entry2 *entries,
@@ -188,6 +196,7 @@ static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature)
{
	unsigned int x86_leaf = __feature_leaf(x86_feature);

	WARN_ON_ONCE(!kvm_is_configuring_cpu_caps);
	kvm_cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature);
}

@@ -195,6 +204,7 @@ static __always_inline void kvm_cpu_cap_set(unsigned int x86_feature)
{
	unsigned int x86_leaf = __feature_leaf(x86_feature);

	WARN_ON_ONCE(!kvm_is_configuring_cpu_caps);
	kvm_cpu_caps[x86_leaf] |= __feature_bit(x86_feature);
}

+3 −1
Original line number Diff line number Diff line
@@ -5202,7 +5202,7 @@ static __init void svm_adjust_mmio_mask(void)

static __init void svm_set_cpu_caps(void)
{
	kvm_set_cpu_caps();
	kvm_initialize_cpu_caps();

	kvm_caps.supported_perf_cap = 0;

@@ -5284,6 +5284,8 @@ static __init void svm_set_cpu_caps(void)
	 */
	kvm_cpu_cap_clear(X86_FEATURE_BUS_LOCK_DETECT);
	kvm_cpu_cap_clear(X86_FEATURE_MSR_IMM);

	kvm_finalize_cpu_caps();
}

static __init int svm_hardware_setup(void)
+3 −1
Original line number Diff line number Diff line
@@ -7994,7 +7994,7 @@ static __init u64 vmx_get_perf_capabilities(void)

static __init void vmx_set_cpu_caps(void)
{
	kvm_set_cpu_caps();
	kvm_initialize_cpu_caps();

	/* CPUID 0x1 */
	if (nested)
@@ -8051,6 +8051,8 @@ static __init void vmx_set_cpu_caps(void)
		kvm_cpu_cap_clear(X86_FEATURE_SHSTK);
		kvm_cpu_cap_clear(X86_FEATURE_IBT);
	}

	kvm_finalize_cpu_caps();
}

static bool vmx_is_io_intercepted(struct kvm_vcpu *vcpu,