Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: 83f6e109f562 ("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>

+9 -6
+9 -6
tools/testing/selftests/kvm/lib/kvm_util.c
··· 709 709 710 710 ret = close(vmp->kvm_fd); 711 711 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 + } 712 721 } 713 722 714 723 static void __vm_mem_region_delete(struct kvm_vm *vm, ··· 756 747 757 748 if (vmp == NULL) 758 749 return; 759 - 760 - /* Free cached stats metadata and close FD */ 761 - if (vmp->stats_desc) { 762 - free(vmp->stats_desc); 763 - close(vmp->stats_fd); 764 - } 765 750 766 751 /* Free userspace_mem_regions. */ 767 752 hash_for_each_safe(vmp->regions.slot_hash, ctr, node, region, slot_node)