Skip to content

Commit 3f2757d

Browse files
committed
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: Xiaoyao Li <xiaoyao.li@intel.com> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Link: https://patch.msgid.link/20260128014310.3255561-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 95d848d commit 3f2757d

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

arch/x86/kvm/cpuid.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
3737
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_cpu_caps);
3838

39+
bool kvm_is_configuring_cpu_caps __read_mostly;
40+
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_is_configuring_cpu_caps);
41+
3942
struct cpuid_xstate_sizes {
4043
u32 eax;
4144
u32 ebx;
@@ -826,10 +829,13 @@ do { \
826829
/* DS is defined by ptrace-abi.h on 32-bit builds. */
827830
#undef DS
828831

829-
void kvm_set_cpu_caps(void)
832+
void kvm_initialize_cpu_caps(void)
830833
{
831834
memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps));
832835

836+
WARN_ON_ONCE(kvm_is_configuring_cpu_caps);
837+
kvm_is_configuring_cpu_caps = true;
838+
833839
BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
834840
sizeof(boot_cpu_data.x86_capability));
835841

@@ -1288,7 +1294,7 @@ void kvm_set_cpu_caps(void)
12881294
kvm_cpu_cap_clear(X86_FEATURE_RDPID);
12891295
}
12901296
}
1291-
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_set_cpu_caps);
1297+
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_initialize_cpu_caps);
12921298

12931299
#undef F
12941300
#undef SCATTERED_F

arch/x86/kvm/cpuid.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
#include <uapi/asm/kvm_para.h>
99

1010
extern u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
11-
void kvm_set_cpu_caps(void);
11+
extern bool kvm_is_configuring_cpu_caps __read_mostly;
12+
13+
void kvm_initialize_cpu_caps(void);
14+
15+
static inline void kvm_finalize_cpu_caps(void)
16+
{
17+
WARN_ON_ONCE(!kvm_is_configuring_cpu_caps);
18+
kvm_is_configuring_cpu_caps = false;
19+
}
1220

1321
void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu);
1422
struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2(struct kvm_cpuid_entry2 *entries,
@@ -188,13 +196,15 @@ static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature)
188196
{
189197
unsigned int x86_leaf = __feature_leaf(x86_feature);
190198

199+
WARN_ON_ONCE(!kvm_is_configuring_cpu_caps);
191200
kvm_cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature);
192201
}
193202

194203
static __always_inline void kvm_cpu_cap_set(unsigned int x86_feature)
195204
{
196205
unsigned int x86_leaf = __feature_leaf(x86_feature);
197206

207+
WARN_ON_ONCE(!kvm_is_configuring_cpu_caps);
198208
kvm_cpu_caps[x86_leaf] |= __feature_bit(x86_feature);
199209
}
200210

arch/x86/kvm/svm/svm.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5202,7 +5202,7 @@ static __init void svm_adjust_mmio_mask(void)
52025202

52035203
static __init void svm_set_cpu_caps(void)
52045204
{
5205-
kvm_set_cpu_caps();
5205+
kvm_initialize_cpu_caps();
52065206

52075207
kvm_caps.supported_perf_cap = 0;
52085208

@@ -5284,6 +5284,8 @@ static __init void svm_set_cpu_caps(void)
52845284
*/
52855285
kvm_cpu_cap_clear(X86_FEATURE_BUS_LOCK_DETECT);
52865286
kvm_cpu_cap_clear(X86_FEATURE_MSR_IMM);
5287+
5288+
kvm_finalize_cpu_caps();
52875289
}
52885290

52895291
static __init int svm_hardware_setup(void)

arch/x86/kvm/vmx/vmx.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7994,7 +7994,7 @@ static __init u64 vmx_get_perf_capabilities(void)
79947994

79957995
static __init void vmx_set_cpu_caps(void)
79967996
{
7997-
kvm_set_cpu_caps();
7997+
kvm_initialize_cpu_caps();
79987998

79997999
/* CPUID 0x1 */
80008000
if (nested)
@@ -8051,6 +8051,8 @@ static __init void vmx_set_cpu_caps(void)
80518051
kvm_cpu_cap_clear(X86_FEATURE_SHSTK);
80528052
kvm_cpu_cap_clear(X86_FEATURE_IBT);
80538053
}
8054+
8055+
kvm_finalize_cpu_caps();
80548056
}
80558057

80568058
static bool vmx_is_io_intercepted(struct kvm_vcpu *vcpu,

0 commit comments

Comments
 (0)