On Fri, Jan 17, 2025, Vitaly Kuznetsov wrote: > Sean Christopherson <seanjc@xxxxxxxxxx> writes: > > > Explicitly free the array of CPUID entries at the end of the Hyper-V CPUID > > test, mainly in anticipation of moving management of the array into the > > main test helper. > > > > Cc: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx> > > Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx> > > --- > > tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 1 + > > 1 file changed, 1 insertion(+) > > > > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c > > index 9a0fcc713350..09f9874d7705 100644 > > --- a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c > > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c > > @@ -164,6 +164,7 @@ int main(int argc, char *argv[]) > > > > hv_cpuid_entries = kvm_get_supported_hv_cpuid(); > > test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX)); > > + free((void *)hv_cpuid_entries); > > vcpu_get_supported_hv_cpuid() allocates memory for the resulting array > each time, however, kvm_get_supported_hv_cpuid() was designed after > what's now kvm_get_supported_cpuid() (afair) so it has an optimization > to ask KVM just once: > > static struct kvm_cpuid2 *cpuid; > int kvm_fd; > > if (cpuid) > return cpuid; > > cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES); > kvm_fd = open_kvm_dev_path_or_exit(); > ... > > and it seems that if we free hv_cpuid_entries here, next time we call > kvm_get_supported_hv_cpuid() an already freed memory will be returned. > This doesn't matter in in this patch as we're about to quit anyway but > with the next one in the series it becomes problematic. Ow. I totally missed that. I'll drop this patch, and then adjust the next one to do: /* * Note, the CPUID array returned by the system-scoped helper is a one- * time allocation, i.e. must not be freed. */ if (vcpu) free((void *)hv_cpuid_entries); I'll post a v2 once I've actually tested. Thanks!