Re: [PATCH 3/5] KVM: selftests: Explicitly free CPUID array at end of Hyper-V CPUID test
From: Vitaly Kuznetsov
Date: Fri Jan 17 2025 - 11:32:20 EST
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.
>
> out:
> kvm_vm_free(vm);
--
Vitaly