Skip to content

Commit ea7179f

Browse files
committed
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>
1 parent e65faf7 commit ea7179f

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ static void vm_open(struct kvm_vm *vm)
196196

197197
vm->fd = __kvm_ioctl(vm->kvm_fd, KVM_CREATE_VM, (void *)vm->type);
198198
TEST_ASSERT(vm->fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm->fd));
199+
200+
if (kvm_has_cap(KVM_CAP_BINARY_STATS_FD))
201+
vm->stats.fd = vm_get_stats_fd(vm);
202+
else
203+
vm->stats.fd = -1;
199204
}
200205

201206
const char *vm_guest_mode_string(uint32_t i)
@@ -661,14 +666,17 @@ static void kvm_stats_release(struct kvm_binary_stats *stats)
661666
{
662667
int ret;
663668

664-
if (!stats->desc)
669+
if (stats->fd < 0)
665670
return;
666671

667-
free(stats->desc);
668-
stats->desc = NULL;
672+
if (stats->desc) {
673+
free(stats->desc);
674+
stats->desc = NULL;
675+
}
669676

670677
ret = close(stats->fd);
671678
TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
679+
stats->fd = -1;
672680
}
673681

674682
__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,
22312239
int i;
22322240

22332241
if (!stats->desc) {
2234-
stats->fd = vm_get_stats_fd(vm);
22352242
read_stats_header(stats->fd, &stats->header);
22362243
stats->desc = read_stats_descriptors(stats->fd, &stats->header);
22372244
}

0 commit comments

Comments
 (0)