Skip to content

Commit 6e0b817

Browse files
sean-jcsmb49
authored andcommitted
KVM: x86: Snapshot if a vCPU's vendor model is AMD vs. Intel compatible
BugLink: https://bugs.launchpad.net/bugs/2067959 commit fd706c9b1674e2858766bfbf7430534c2b26fbef upstream. Add kvm_vcpu_arch.is_amd_compatible to cache if a vCPU's vendor model is compatible with AMD, i.e. if the vCPU vendor is AMD or Hygon, along with helpers to check if a vCPU is compatible AMD vs. Intel. To handle Intel vs. AMD behavior related to masking the LVTPC entry, KVM will need to check for vendor compatibility on every PMI injection, i.e. querying for AMD will soon be a moderately hot path. Note! This subtly (or maybe not-so-subtly) makes "Intel compatible" KVM's default behavior, both if userspace omits (or never sets) CPUID 0x0 and if userspace sets a completely unknown vendor. One could argue that KVM should treat such vCPUs as not being compatible with Intel *or* AMD, but that would add useless complexity to KVM. KVM needs to do *something* in the face of vendor specific behavior, and so unless KVM conjured up a magic third option, choosing to treat unknown vendors as neither Intel nor AMD means that checks on AMD compatibility would yield Intel behavior, and checks for Intel compatibility would yield AMD behavior. And that's far worse as it would effectively yield random behavior depending on whether KVM checked for AMD vs. Intel vs. !AMD vs. !Intel. And practically speaking, all x86 CPUs follow either Intel or AMD architecture, i.e. "supporting" an unknown third architecture adds no value. Deliberately don't convert any of the existing guest_cpuid_is_intel() checks, as the Intel side of things is messier due to some flows explicitly checking for exactly vendor==Intel, versus some flows assuming anything that isn't "AMD compatible" gets Intel behavior. The Intel code will be cleaned up in the future. Cc: [email protected] Signed-off-by: Sean Christopherson <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Portia Stephens <[email protected]> Signed-off-by: Stefan Bader <[email protected]>
1 parent 9312abc commit 6e0b817

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-2
lines changed

arch/x86/include/asm/kvm_host.h

+1
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ struct kvm_vcpu_arch {
731731

732732
int cpuid_nent;
733733
struct kvm_cpuid_entry2 *cpuid_entries;
734+
bool is_amd_compatible;
734735

735736
u64 reserved_gpa_bits;
736737
int maxphyaddr;

arch/x86/kvm/cpuid.c

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
189189

190190
kvm_update_pv_runtime(vcpu);
191191

192+
vcpu->arch.is_amd_compatible = guest_cpuid_is_amd_or_hygon(vcpu);
192193
vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
193194
vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
194195

arch/x86/kvm/cpuid.h

+10
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ static inline bool guest_cpuid_is_intel(struct kvm_vcpu *vcpu)
121121
return best && is_guest_vendor_intel(best->ebx, best->ecx, best->edx);
122122
}
123123

124+
static inline bool guest_cpuid_is_amd_compatible(struct kvm_vcpu *vcpu)
125+
{
126+
return vcpu->arch.is_amd_compatible;
127+
}
128+
129+
static inline bool guest_cpuid_is_intel_compatible(struct kvm_vcpu *vcpu)
130+
{
131+
return !guest_cpuid_is_amd_compatible(vcpu);
132+
}
133+
124134
static inline int guest_cpuid_family(struct kvm_vcpu *vcpu)
125135
{
126136
struct kvm_cpuid_entry2 *best;

arch/x86/kvm/mmu/mmu.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4351,7 +4351,7 @@ static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
43514351
context->root_level, is_efer_nx(context),
43524352
guest_can_use_gbpages(vcpu),
43534353
is_cr4_pse(context),
4354-
guest_cpuid_is_amd_or_hygon(vcpu));
4354+
guest_cpuid_is_amd_compatible(vcpu));
43554355
}
43564356

43574357
static void

arch/x86/kvm/x86.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3108,7 +3108,7 @@ static void kvmclock_sync_fn(struct work_struct *work)
31083108
static bool can_set_mci_status(struct kvm_vcpu *vcpu)
31093109
{
31103110
/* McStatusWrEn enabled? */
3111-
if (guest_cpuid_is_amd_or_hygon(vcpu))
3111+
if (guest_cpuid_is_amd_compatible(vcpu))
31123112
return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
31133113

31143114
return false;

0 commit comments

Comments
 (0)