Skip to content

Commit 3fd55b5

Browse files
committed
KVM: x86: Pull CPUID capabilities from boot_cpu_data only as needed
Don't memcpy() all of boot_cpu_data.x86_capability, and instead explicitly fill each kvm_cpu_cap_init leaf during kvm_cpu_cap_init(). While clever, copying all kernel capabilities risks over-reporting KVM capabilities, e.g. if KVM added support in __do_cpuid_func(), but neglected to init the supported set of capabilities. Note, explicitly grabbing leafs deliberately keeps Linux-defined leafs as 0! KVM should never advertise Linux-defined leafs; any relevant features that are "real", but scattered, must be gathered in their correct hardware- defined leaf. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 75c489e commit 3fd55b5

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

arch/x86/kvm/cpuid.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -678,21 +678,23 @@ static __always_inline u32 raw_cpuid_get(struct cpuid_reg cpuid)
678678
}
679679

680680
/*
681-
* For kernel-defined leafs, mask the boot CPU's pre-populated value. For KVM-
682-
* defined leafs, explicitly set the leaf, as KVM is the one and only authority.
681+
* For kernel-defined leafs, mask KVM's supported feature set with the kernel's
682+
* capabilities as well as raw CPUID. For KVM-defined leafs, consult only raw
683+
* CPUID, as KVM is the one and only authority (in the kernel).
683684
*/
684685
#define kvm_cpu_cap_init(leaf, mask) \
685686
do { \
686687
const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32); \
687688
const u32 __maybe_unused kvm_cpu_cap_init_in_progress = leaf; \
689+
const u32 *kernel_cpu_caps = boot_cpu_data.x86_capability; \
688690
u32 kvm_cpu_cap_passthrough = 0; \
689691
u32 kvm_cpu_cap_synthesized = 0; \
690692
u32 kvm_cpu_cap_emulated = 0; \
691693
\
694+
kvm_cpu_caps[leaf] = (mask); \
695+
\
692696
if (leaf < NCAPINTS) \
693-
kvm_cpu_caps[leaf] &= (mask); \
694-
else \
695-
kvm_cpu_caps[leaf] = (mask); \
697+
kvm_cpu_caps[leaf] &= kernel_cpu_caps[leaf]; \
696698
\
697699
kvm_cpu_caps[leaf] |= kvm_cpu_cap_passthrough; \
698700
kvm_cpu_caps[leaf] &= (raw_cpuid_get(cpuid) | \
@@ -790,9 +792,6 @@ void kvm_set_cpu_caps(void)
790792
BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
791793
sizeof(boot_cpu_data.x86_capability));
792794

793-
memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability,
794-
sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)));
795-
796795
kvm_cpu_cap_init(CPUID_1_ECX,
797796
F(XMM3) |
798797
F(PCLMULQDQ) |

0 commit comments

Comments
 (0)