]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: selftests: Refresh vCPU CPUID cache in __vcpu_get_cpuid_entry()
authorSean Christopherson <seanjc@google.com>
Thu, 28 Nov 2024 01:33:35 +0000 (17:33 -0800)
committerSean Christopherson <seanjc@google.com>
Wed, 18 Dec 2024 22:19:29 +0000 (14:19 -0800)
Refresh selftests' CPUID cache in the vCPU structure when querying a CPUID
entry so that tests don't consume stale data when KVM modifies CPUID as a
side effect to a completely unrelated change.  E.g. KVM adjusts OSXSAVE in
response to CR4.OSXSAVE changes.

Unnecessarily invoking KVM_GET_CPUID is suboptimal, but vcpu->cpuid exists
to simplify selftests development, not for performance reasons.  And,
unfortunately, trying to handle the side effects in tests or other flows
is unpleasant, e.g. selftests could manually refresh if KVM_SET_SREGS is
successful, but that would still leave a gap with respect to guest CR4
changes.

Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
Link: https://lore.kernel.org/r/20241128013424.4096668-9-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/include/x86/processor.h

index 71aa290c583f6c689c6ed313c8dace3584ed19ad..b6753e27dfb6f9aac694d131e2d5f5feac89d53e 100644 (file)
@@ -1010,12 +1010,19 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)
 
 void vcpu_init_cpuid(struct kvm_vcpu *vcpu, const struct kvm_cpuid2 *cpuid);
 
+static inline void vcpu_get_cpuid(struct kvm_vcpu *vcpu)
+{
+       vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid);
+}
+
 static inline struct kvm_cpuid_entry2 *__vcpu_get_cpuid_entry(struct kvm_vcpu *vcpu,
                                                              uint32_t function,
                                                              uint32_t index)
 {
        TEST_ASSERT(vcpu->cpuid, "Must do vcpu_init_cpuid() first (or equivalent)");
 
+       vcpu_get_cpuid(vcpu);
+
        return (struct kvm_cpuid_entry2 *)get_cpuid_entry(vcpu->cpuid,
                                                          function, index);
 }
@@ -1036,7 +1043,7 @@ static inline int __vcpu_set_cpuid(struct kvm_vcpu *vcpu)
                return r;
 
        /* On success, refresh the cache to pick up adjustments made by KVM. */
-       vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid);
+       vcpu_get_cpuid(vcpu);
        return 0;
 }
 
@@ -1046,12 +1053,7 @@ static inline void vcpu_set_cpuid(struct kvm_vcpu *vcpu)
        vcpu_ioctl(vcpu, KVM_SET_CPUID2, vcpu->cpuid);
 
        /* Refresh the cache to pick up adjustments made by KVM. */
-       vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid);
-}
-
-static inline void vcpu_get_cpuid(struct kvm_vcpu *vcpu)
-{
-       vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid);
+       vcpu_get_cpuid(vcpu);
 }
 
 void vcpu_set_cpuid_property(struct kvm_vcpu *vcpu,