KVM: selftests: Add struct and helpers to wrap binary stats cache

Add a struct and helpers to manage the binary stats cache, which is
currently used only for VM-scoped stats.  This will allow expanding the
selftests infrastructure to provide support for vCPU-scoped binary stats,
which, except for the ioctl to get the stats FD are identical to VM-scoped
stats.

Defer converting __vm_get_stat() to a scope-agnostic helper to a future
patch, as getting the stats FD from KVM needs to be moved elsewhere
before it can be made completely scope-agnostic.

Link: https://lore.kernel.org/r/20250111005049.1247555-6-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Sean Christopherson
2025-01-10 16:50:45 -08:00
parent b0c3f5df92
commit e65faf71bd
2 changed files with 34 additions and 24 deletions

View File

@@ -657,6 +657,20 @@ userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)
return NULL;
}
static void kvm_stats_release(struct kvm_binary_stats *stats)
{
int ret;
if (!stats->desc)
return;
free(stats->desc);
stats->desc = NULL;
ret = close(stats->fd);
TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
}
__weak void vcpu_arch_free(struct kvm_vcpu *vcpu)
{
@@ -711,13 +725,7 @@ void kvm_vm_release(struct kvm_vm *vmp)
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));
}
kvm_stats_release(&vmp->stats);
}
static void __vm_mem_region_delete(struct kvm_vm *vm,
@@ -2214,34 +2222,33 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header,
*
* Read the data values of a specified stat from the binary stats interface.
*/
void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,
void __vm_get_stat(struct kvm_vm *vm, const char *name, uint64_t *data,
size_t max_elements)
{
struct kvm_binary_stats *stats = &vm->stats;
struct kvm_stats_desc *desc;
size_t size_desc;
int i;
if (!vm->stats_desc) {
vm->stats_fd = vm_get_stats_fd(vm);
read_stats_header(vm->stats_fd, &vm->stats_header);
vm->stats_desc = read_stats_descriptors(vm->stats_fd,
&vm->stats_header);
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);
}
size_desc = get_stats_descriptor_size(&vm->stats_header);
size_desc = get_stats_descriptor_size(&stats->header);
for (i = 0; i < vm->stats_header.num_desc; ++i) {
desc = (void *)vm->stats_desc + (i * size_desc);
for (i = 0; i < stats->header.num_desc; ++i) {
desc = (void *)stats->desc + (i * size_desc);
if (strcmp(desc->name, stat_name))
if (strcmp(desc->name, name))
continue;
read_stat_data(vm->stats_fd, &vm->stats_header, desc,
data, max_elements);
read_stat_data(stats->fd, &stats->header, desc, data, max_elements);
return;
}
TEST_FAIL("Unable to find stat '%s'", stat_name);
TEST_FAIL("Unable to find stat '%s'", name);
}
__weak void kvm_arch_vm_post_create(struct kvm_vm *vm)