KVM: selftests: Close VM's binary stats FD when releasing VM

Close/free a VM's binary stats cache when the VM is released, not when the
VM is fully freed.  When a VM is re-created, e.g. for state save/restore
tests, the stats FD and descriptor points at the old, defunct VM.  The FD
is still valid, in that the underlying stats file won't be freed until the
FD is closed, but reading stats will always pull information from the old
VM.

Note, this is a benign bug in the current code base as none of the tests
that recreate VMs use binary stats.

Fixes: 83f6e109f5 ("KVM: selftests: Cache binary stats metadata for duration of test")
Link: https://lore.kernel.org/r/20250111005049.1247555-3-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Sean Christopherson
2025-01-10 16:50:42 -08:00
parent fd546aba19
commit f7f232a01f

View File

@@ -709,6 +709,15 @@ void kvm_vm_release(struct kvm_vm *vmp)
ret = close(vmp->kvm_fd);
TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
/* Free cached stats metadata and close FD */
if (vmp->stats_desc) {
free(vmp->stats_desc);
vmp->stats_desc = NULL;
ret = close(vmp->stats_fd);
TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
}
}
static void __vm_mem_region_delete(struct kvm_vm *vm,
@@ -748,12 +757,6 @@ void kvm_vm_free(struct kvm_vm *vmp)
if (vmp == NULL)
return;
/* Free cached stats metadata and close FD */
if (vmp->stats_desc) {
free(vmp->stats_desc);
close(vmp->stats_fd);
}
/* Free userspace_mem_regions. */
hash_for_each_safe(vmp->regions.slot_hash, ctr, node, region, slot_node)
__vm_mem_region_delete(vmp, region);