Skip to content

Commit a5b3271

Browse files
committed
KVM: x86: Remove unnecessary caching of KVM's PV CPUID base
Now that KVM only searches for KVM's PV CPUID base when userspace sets guest CPUID, drop the cache and simply do the search every time. Practically speaking, this is a nop except for situations where userspace sets CPUID _after_ running the vCPU, which is anything but a hot path, e.g. QEMU does so only when hotplugging a vCPU. And on the flip side, caching guest CPUID information, especially information that is used to query/modify _other_ CPUID state, is inherently dangerous as it's all too easy to use stale information, i.e. KVM should only cache CPUID state when the performance and/or programming benefits justify it. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 63d8c70 commit a5b3271

File tree

2 files changed

+8
-27
lines changed

2 files changed

+8
-27
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,6 @@ struct kvm_vcpu_arch {
854854

855855
int cpuid_nent;
856856
struct kvm_cpuid_entry2 *cpuid_entries;
857-
struct kvm_hypervisor_cpuid kvm_cpuid;
858857
bool is_amd_compatible;
859858

860859
/*

arch/x86/kvm/cpuid.c

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,7 @@ static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2
189189

190190
/*
191191
* Apply runtime CPUID updates to the incoming CPUID entries to avoid
192-
* false positives due mismatches on KVM-owned feature flags. Note,
193-
* runtime CPUID updates may consume other CPUID-driven vCPU state,
194-
* e.g. KVM or Xen CPUID bases. Updating runtime state before full
195-
* CPUID processing is functionally correct only because any change in
196-
* CPUID is disallowed, i.e. using stale data is ok because the below
197-
* checks will reject the change.
192+
* false positives due mismatches on KVM-owned feature flags.
198193
*
199194
* Note! @e2 and @nent track the _old_ CPUID entries!
200195
*/
@@ -252,28 +247,16 @@ static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcpu *vcp
252247
vcpu->arch.cpuid_nent, sig);
253248
}
254249

255-
static struct kvm_cpuid_entry2 *__kvm_find_kvm_cpuid_features(struct kvm_cpuid_entry2 *entries,
256-
int nent, u32 kvm_cpuid_base)
257-
{
258-
return cpuid_entry2_find(entries, nent, kvm_cpuid_base | KVM_CPUID_FEATURES,
259-
KVM_CPUID_INDEX_NOT_SIGNIFICANT);
260-
}
261-
262-
static struct kvm_cpuid_entry2 *kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu)
263-
{
264-
u32 base = vcpu->arch.kvm_cpuid.base;
265-
266-
if (!base)
267-
return NULL;
268-
269-
return __kvm_find_kvm_cpuid_features(vcpu->arch.cpuid_entries,
270-
vcpu->arch.cpuid_nent, base);
271-
}
272-
273250
static u32 kvm_apply_cpuid_pv_features_quirk(struct kvm_vcpu *vcpu)
274251
{
275-
struct kvm_cpuid_entry2 *best = kvm_find_kvm_cpuid_features(vcpu);
252+
struct kvm_hypervisor_cpuid kvm_cpuid;
253+
struct kvm_cpuid_entry2 *best;
254+
255+
kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE);
256+
if (!kvm_cpuid.base)
257+
return 0;
276258

259+
best = kvm_find_cpuid_entry(vcpu, kvm_cpuid.base | KVM_CPUID_FEATURES);
277260
if (!best)
278261
return 0;
279262

@@ -504,7 +487,6 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
504487
if (r)
505488
goto err;
506489

507-
vcpu->arch.kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE);
508490
#ifdef CONFIG_KVM_XEN
509491
vcpu->arch.xen.cpuid = kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE);
510492
#endif

0 commit comments

Comments
 (0)