KVM: selftests: Get VM's binary stats FD when opening VM

Get and cache a VM's binary stats FD when the VM is opened, as opposed to
waiting until the stats are first used.  Opening the stats FD outside of
__vm_get_stat() will allow converting it to a scope-agnostic helper.

Note, this doesn't interfere with kvm_binary_stats_test's testcase that
verifies a stats FD can be used after its own VM's FD is closed, as the
cached FD is also closed during kvm_vm_free().

Link: https://lore.kernel.org/r/20250111005049.1247555-7-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Sean Christopherson
2025-01-10 16:50:46 -08:00
parent e65faf71bd
commit ea7179f995

View File

@@ -196,6 +196,11 @@ static void vm_open(struct kvm_vm *vm)
vm->fd = __kvm_ioctl(vm->kvm_fd, KVM_CREATE_VM, (void *)vm->type);
TEST_ASSERT(vm->fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm->fd));
if (kvm_has_cap(KVM_CAP_BINARY_STATS_FD))
vm->stats.fd = vm_get_stats_fd(vm);
else
vm->stats.fd = -1;
}
const char *vm_guest_mode_string(uint32_t i)
@@ -661,14 +666,17 @@ static void kvm_stats_release(struct kvm_binary_stats *stats)
{
int ret;
if (!stats->desc)
if (stats->fd < 0)
return;
free(stats->desc);
stats->desc = NULL;
if (stats->desc) {
free(stats->desc);
stats->desc = NULL;
}
ret = close(stats->fd);
TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
stats->fd = -1;
}
__weak void vcpu_arch_free(struct kvm_vcpu *vcpu)
@@ -2231,7 +2239,6 @@ void __vm_get_stat(struct kvm_vm *vm, const char *name, uint64_t *data,
int i;
if (!stats->desc) {
stats->fd = vm_get_stats_fd(vm);
read_stats_header(stats->fd, &stats->header);
stats->desc = read_stats_descriptors(stats->fd, &stats->header);
}