Skip to content

Commit a59768d

Browse files
committed
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: 83f6e10 ("KVM: selftests: Cache binary stats metadata for duration of test") Link: https://lore.kernel.org/r/20241220013906.3518334-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent b68ec5b commit a59768d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,15 @@ void kvm_vm_release(struct kvm_vm *vmp)
709709

710710
ret = close(vmp->kvm_fd);
711711
TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
712+
713+
/* Free cached stats metadata and close FD */
714+
if (vmp->stats_desc) {
715+
free(vmp->stats_desc);
716+
vmp->stats_desc = NULL;
717+
718+
ret = close(vmp->stats_fd);
719+
TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
720+
}
712721
}
713722

714723
static void __vm_mem_region_delete(struct kvm_vm *vm,
@@ -748,12 +757,6 @@ void kvm_vm_free(struct kvm_vm *vmp)
748757
if (vmp == NULL)
749758
return;
750759

751-
/* Free cached stats metadata and close FD */
752-
if (vmp->stats_desc) {
753-
free(vmp->stats_desc);
754-
close(vmp->stats_fd);
755-
}
756-
757760
/* Free userspace_mem_regions. */
758761
hash_for_each_safe(vmp->regions.slot_hash, ctr, node, region, slot_node)
759762
__vm_mem_region_delete(vmp, region);

0 commit comments

Comments
 (0)