Skip to content

Commit cd5f3d2

Browse files
committed
KVM: selftests: Disallow "get supported CPUID" before REQ_XCOMP_GUEST_PERM
Disallow using kvm_get_supported_cpuid() and thus caching KVM's supported CPUID info before enabling XSAVE-managed features that are off-by-default and must be enabled by ARCH_REQ_XCOMP_GUEST_PERM. Caching the supported CPUID before all XSAVE features are enabled can result in false negatives due to testing features that were cached before they were enabled. Signed-off-by: Sean Christopherson <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 2ceade1 commit cd5f3d2

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

tools/testing/selftests/kvm/lib/x86_64/processor.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,21 +601,24 @@ void vcpu_arch_free(struct kvm_vcpu *vcpu)
601601
free(vcpu->cpuid);
602602
}
603603

604+
/* Do not use kvm_supported_cpuid directly except for validity checks. */
605+
static void *kvm_supported_cpuid;
606+
604607
const struct kvm_cpuid2 *kvm_get_supported_cpuid(void)
605608
{
606-
static struct kvm_cpuid2 *cpuid;
607609
int kvm_fd;
608610

609-
if (cpuid)
610-
return cpuid;
611+
if (kvm_supported_cpuid)
612+
return kvm_supported_cpuid;
611613

612-
cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES);
614+
kvm_supported_cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES);
613615
kvm_fd = open_kvm_dev_path_or_exit();
614616

615-
kvm_ioctl(kvm_fd, KVM_GET_SUPPORTED_CPUID, cpuid);
617+
kvm_ioctl(kvm_fd, KVM_GET_SUPPORTED_CPUID,
618+
(struct kvm_cpuid2 *)kvm_supported_cpuid);
616619

617620
close(kvm_fd);
618-
return cpuid;
621+
return kvm_supported_cpuid;
619622
}
620623

621624
static uint32_t __kvm_cpu_has(const struct kvm_cpuid2 *cpuid,
@@ -684,6 +687,9 @@ void __vm_xsave_require_permission(int bit, const char *name)
684687
.addr = (unsigned long) &bitmask
685688
};
686689

690+
TEST_ASSERT(!kvm_supported_cpuid,
691+
"kvm_get_supported_cpuid() cannot be used before ARCH_REQ_XCOMP_GUEST_PERM");
692+
687693
kvm_fd = open_kvm_dev_path_or_exit();
688694
rc = __kvm_ioctl(kvm_fd, KVM_GET_DEVICE_ATTR, &attr);
689695
close(kvm_fd);

0 commit comments

Comments
 (0)