KVM: selftests: Binary stats fixes and infra updates
Sean Christopherson
maintainerv2 cover KVM: selftests: Binary stats fixes and infra updates
Fix a handful of bugs in the binary stats infrastructure, expand support to vCPU-scoped stats, enumerate all KVM stats in selftests, and use the enumerated stats to assert at compile-time that {vm,vcpu}_get_stat() is getting a stat that actually exists.
Most of the bugs are benign, and AFAICT, none actually cause problems in the current code base. The worst of the bugs is lack of validation that the requested stat actually exists, which is quite annoying if someone fat fingers a stat name, tries to get a vCPU stat on a VM FD, etc.
As called out multiple times in v1, I'm on the fence regarding the last patch (compile-time assertions). My plan is to apply everything except the last patch fairly quickly, so that the vCPU stats stuff in particular can be used in x86 tests, and hold off on the compile-time assertion goo until someone comes along with a strong opinion one way or the other.
v2:
- Update rlimit number of files for all "standard" VMs.
- Account for the vCPU stats fd when updating rlimits.
v1: https://lore.kernel.org/all/[email protected]
Sean Christopherson (9):
KVM: selftests: Fix mostly theoretical leak of VM's binary stats FD
KVM: selftests: Close VM's binary stats FD when releasing VM
KVM: selftests: Assert that __vm_get_stat() actually finds a stat
KVM: selftests: Macrofy vm_get_stat() to auto-generate stat name
string
KVM: selftests: Add struct and helpers to wrap binary stats cache
KVM: selftests: Get VM's binary stats FD when opening VM
KVM: selftests: Adjust number of files rlimit for all "standard" VMs
KVM: selftests: Add infrastructure for getting vCPU binary stats
KVM: selftests: Add compile-time assertions to guard against stats
typos
.../kvm/include/arm64/kvm_util_arch.h | 12 ++ .../testing/selftests/kvm/include/kvm_util.h | 51 ++++++-- .../selftests/kvm/include/kvm_util_types.h | 6 + .../kvm/include/riscv/kvm_util_arch.h | 14 +++ .../kvm/include/s390/kvm_util_arch.h | 113 +++++++++++++++++ .../selftests/kvm/include/x86/kvm_util_arch.h | 52 ++++++++ .../selftests/kvm/kvm_create_max_vcpus.c | 28 +---- tools/testing/selftests/kvm/lib/kvm_util.c | 114 ++++++++++++------ .../kvm/x86/dirty_log_page_splitting_test.c | 6 +- .../selftests/kvm/x86/nx_huge_pages_test.c | 4 +- .../selftests/kvm/x86/xapic_ipi_test.c | 2 + 11 files changed, 324 insertions(+), 78 deletions(-)
base-commit: 10b2c8a67c4b8ec15f9d07d177f63b563418e948
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Sean Christopherson
maintainerPATCH v2 1/9 KVM: selftests: Fix mostly theoretical leak of VM's binary stats FD
When allocating and freeing a VM's cached binary stats info, check for a NULL descriptor, not a '0' file descriptor, as '0' is a legal FD. E.g. in the unlikely scenario the kernel installs the stats FD at entry '0', selftests would reallocate on the next __vm_get_stat() and/or fail to free the stats in kvm_vm_free().
--- tools/testing/selftests/kvm/lib/kvm_util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +2 -2
@@ -749,7 +749,7 @@ void kvm_vm_free(struct kvm_vm *vmp)return ;/* Free cached stats metadata and close FD */ -if (vmp->stats_fd) { +if (vmp->stats_desc) {free (vmp->stats_desc);close (vmp->stats_fd); }
@@ -2218,7 +2218,7 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,size_t size_desc;int i; -if (!vm->stats_fd) { +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,
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Sean Christopherson
maintainerPATCH v2 2/9 KVM: selftests: Close VM's binary stats FD when releasing VM
In reply to Sean ChristophersonClose/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.
--- tools/testing/selftests/kvm/lib/kvm_util.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +9 -6
@@ -709,6 +709,15 @@ void kvm_vm_release(struct kvm_vm *vmp) ret =close (vmp->kvm_fd);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)); + } } static void __vm_mem_region_delete(struct kvm_vm *vm,
@@ -748,12 +757,6 @@ void kvm_vm_free(struct kvm_vm *vmp)if (vmp ==NULL )return ; -/* Free cached stats metadata and close FD */ -if (vmp->stats_desc) { -free (vmp->stats_desc); -close (vmp->stats_fd); - } -/* Free userspace_mem_regions. */ hash_for_each_safe (vmp->regions.slot_hash, ctr, node, region, slot_node)__vm_mem_region_delete (vmp, region);
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Sean Christopherson
maintainerPATCH v2 3/9 KVM: selftests: Assert that __vm_get_stat() actually finds a stat
In reply to Sean ChristophersonFail the test if it attempts to read a stat that doesn't exist, e.g. due to a typo (hooray, strings), or because the test tried to get a stat for the wrong scope. As is, there's no indiciation of failure and @data is left untouched, e.g. holds '0' or random stack data in most cases.
--- tools/testing/selftests/kvm/lib/kvm_util.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +3 -2
@@ -2238,9 +2238,10 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,read_stat_data (vm->stats_fd, &vm->stats_header, desc, data, max_elements); - -break ; +return ; } + +TEST_FAIL ("Unabled to find stat '%s'" , stat_name); }__weak void kvm_arch_vm_post_create (struct kvm_vm *vm)
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Sean Christopherson
maintainerPATCH v2 4/9 KVM: selftests: Macrofy vm_get_stat() to auto-generate stat name string
In reply to Sean ChristophersonTurn vm_get_stat() into a macro that generates a string for the stat name, as opposed to taking a string. This will allow hardening stat usage in the future to generate errors on unknown stats at compile time.
No functional change intended.
--- tools/testing/selftests/kvm/include/kvm_util.h | 14 +++++++------- .../kvm/x86/dirty_log_page_splitting_test.c | 6 +++--- .../testing/selftests/kvm/x86/nx_huge_pages_test.c | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +7 -7
@@ -534,13 +534,13 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header,void __vm_get_stat (struct kvm_vm *vm,const char *stat_name,uint64_t *data,size_t max_elements); -static inline uint64_t vm_get_stat (struct kvm_vm *vm,const char *stat_name) -{ -uint64_t data; - -__vm_get_stat (vm, stat_name, &data,1 ); -return data; -} +#define vm_get_stat (vm, stat) \ +({ \ + uint64_t data; \ + \ + __vm_get_stat(vm, #stat, &data, 1); \ + data; \ +})void vm_create_irqchip (struct kvm_vm *vm);
tools/testing/selftests/kvm/x86/dirty_log_page_splitting_test.c +3 -3
@@ -41,9 +41,9 @@ struct kvm_page_stats {static void get_page_stats (struct kvm_vm *vm,struct kvm_page_stats *stats,const char *stage) { - stats->pages_4k =vm_get_stat (vm,"pages_4k" ); - stats->pages_2m =vm_get_stat (vm,"pages_2m" ); - stats->pages_1g =vm_get_stat (vm,"pages_1g" ); + stats->pages_4k =vm_get_stat (vm, pages_4k); + stats->pages_2m =vm_get_stat (vm, pages_2m); + stats->pages_1g =vm_get_stat (vm, pages_1g); stats->hugepages = stats->pages_2m + stats->pages_1g; pr_debug("\nPage stats after %s: 4K: %ld 2M: %ld 1G: %ld huge: %ld\n",
tools/testing/selftests/kvm/x86/nx_huge_pages_test.c +2 -2
@@ -73,7 +73,7 @@ static void check_2m_page_count(struct kvm_vm *vm, int expected_pages_2m) {int actual_pages_2m; - actual_pages_2m =vm_get_stat (vm,"pages_2m" ); + actual_pages_2m =vm_get_stat (vm, pages_2m); TEST_ASSERT(actual_pages_2m == expected_pages_2m, "Unexpected 2m page count. Expected %d, got %d",
@@ -84,7 +84,7 @@ static void check_split_count(struct kvm_vm *vm, int expected_splits) {int actual_splits; - actual_splits =vm_get_stat (vm,"nx_lpage_splits" ); + actual_splits =vm_get_stat (vm, nx_lpage_splits); TEST_ASSERT(actual_splits == expected_splits, "Unexpected NX huge page split count. Expected %d, got %d",
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Sean Christopherson
maintainerPATCH v2 5/9 KVM: selftests: Add struct and helpers to wrap binary stats cache
In reply to Sean ChristophersonAdd 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.
--- .../testing/selftests/kvm/include/kvm_util.h | 11 +++-- tools/testing/selftests/kvm/lib/kvm_util.c | 47 +++++++++++-------- 2 files changed, 34 insertions(+), 24 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +7 -4
@@ -46,6 +46,12 @@ struct userspace_mem_region {struct hlist_node slot_node; }; +struct kvm_binary_stats { +int fd; +struct kvm_stats_header header; +struct kvm_stats_desc *desc; +}; +struct kvm_vcpu {struct list_head list;uint32_t id;
@@ -99,10 +105,7 @@ struct kvm_vm {struct kvm_vm_arch arch; -/* Cache of information for binary stats interface */ -int stats_fd; -struct kvm_stats_header stats_header; -struct kvm_stats_desc *stats_desc; +struct kvm_binary_stats stats;/* * KVM region slots. These are the default memslots used by page
tools/testing/selftests/kvm/lib/kvm_util.c +27 -20
80 lines not on this page · raw message
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Sean Christopherson
maintainerPATCH v2 6/9 KVM: selftests: Get VM's binary stats FD when opening VM
In reply to Sean ChristophersonGet 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().
--- tools/testing/selftests/kvm/lib/kvm_util.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +11 -4
@@ -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); }
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Sean Christopherson
maintainerPATCH v2 7/9 KVM: selftests: Adjust number of files rlimit for all "standard" VMs
In reply to Sean ChristophersonMove the max vCPUs test's RLIMIT_NOFILE adjustments to common code, and use the new helper to adjust the resource limit for non-barebones VMs by default. x86's recalc_apic_map_test creates 512 vCPUs, and a future change will open the binary stats fd for all vCPUs, which will put the recalc APIC test above some distros' default limit of 1024.
--- .../testing/selftests/kvm/include/kvm_util.h | 2 ++ .../selftests/kvm/kvm_create_max_vcpus.c | 28 +-------------- tools/testing/selftests/kvm/lib/kvm_util.c | 34 +++++++++++++++++++ 3 files changed, 37 insertions(+), 27 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +2 -0
@@ -966,6 +966,8 @@ static inline struct kvm_vm *vm_create_shape_with_one_vcpu(struct vm_shape shapestruct kvm_vcpu *vm_recreate_with_one_vcpu (struct kvm_vm *vm); +void kvm_set_files_rlimit (uint32_t nr_vcpus); +void kvm_pin_this_task_to_pcpu (uint32_t pcpu);void kvm_print_vcpu_pinning_help (void ); void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[],
tools/testing/selftests/kvm/kvm_create_max_vcpus.c +1 -27
@@ -10,7 +10,6 @@#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sys/resource.h> #include "test_util.h"
@@ -39,36 +38,11 @@ int main(int argc, char *argv[]) {int kvm_max_vcpu_id =kvm_check_cap (KVM_CAP_MAX_VCPU_ID );int kvm_max_vcpus =kvm_check_cap (KVM_CAP_MAX_VCPUS ); -/* -* Number of file descriptors reqired, KVM_CAP_MAX_VCPUS for vCPU fds + -* an arbitrary number for everything else. -*/ -int nr_fds_wanted = kvm_max_vcpus +100 ; -struct rlimit rl;pr_info ("KVM_CAP_MAX_VCPU_ID: %d\n" , kvm_max_vcpu_id);pr_info ("KVM_CAP_MAX_VCPUS: %d\n" , kvm_max_vcpus); -/* -* Check that we're allowed to open nr_fds_wanted file descriptors and -* try raising the limits if needed. -*/ -TEST_ASSERT (!getrlimit (RLIMIT_NOFILE , &rl),"getrlimit() failed!" ); - -if (rl.rlim_cur < nr_fds_wanted) { - rl.rlim_cur = nr_fds_wanted; -if (rl.rlim_max < nr_fds_wanted) { -int old_rlim_max = rl.rlim_max; - rl.rlim_max = nr_fds_wanted; - -int r =setrlimit (RLIMIT_NOFILE , &rl); -__TEST_REQUIRE (r >=0 , -"RLIMIT_NOFILE hard limit is too low (%d, wanted %d)" , - old_rlim_max, nr_fds_wanted); - }else { -TEST_ASSERT (!setrlimit (RLIMIT_NOFILE , &rl),"setrlimit() failed!" ); - } - } +kvm_set_files_rlimit (kvm_max_vcpus);/* * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
tools/testing/selftests/kvm/lib/kvm_util.c +34 -0
@@ -12,6 +12,7 @@#include <assert.h> #include <sched.h> #include <sys/mman.h> +#include <sys/resource.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>
@@ -411,6 +412,37 @@ static uint64_t vm_nr_pages_required(enum vm_guest_mode mode,return vm_adjust_num_guest_pages (mode, nr_pages); } +void kvm_set_files_rlimit (uint32_t nr_vcpus) +{ +/* +* Number of file descriptors required, nr_vpucs vCPU fds + an arbitrary +* number for everything else. +*/ +int nr_fds_wanted = nr_vcpus +100 ; +struct rlimit rl; + +/* +* Check that we're allowed to open nr_fds_wanted file descriptors and +* try raising the limits if needed. +*/ +TEST_ASSERT (!getrlimit (RLIMIT_NOFILE , &rl),"getrlimit() failed!" ); + +if (rl.rlim_cur < nr_fds_wanted) { + rl.rlim_cur = nr_fds_wanted; +if (rl.rlim_max < nr_fds_wanted) { +int old_rlim_max = rl.rlim_max; + + rl.rlim_max = nr_fds_wanted; +__TEST_REQUIRE (setrlimit (RLIMIT_NOFILE , &rl) >=0 , +"RLIMIT_NOFILE hard limit is too low (%d, wanted %d)" , + old_rlim_max, nr_fds_wanted); + }else { +TEST_ASSERT (!setrlimit (RLIMIT_NOFILE , &rl),"setrlimit() failed!" ); + } + } + +} +struct kvm_vm *__vm_create (struct vm_shape shape,uint32_t nr_runnable_vcpus,uint64_t nr_extra_pages) {
@@ -420,6 +452,8 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,struct kvm_vm *vm;int i; +kvm_set_files_rlimit (nr_runnable_vcpus); +pr_debug ("%s: mode='%s' type='%d', pages='%ld'\n" , __func__,vm_guest_mode_string (shape.mode), shape.type, nr_pages);
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Sean Christopherson
maintainerPATCH v2 8/9 KVM: selftests: Add infrastructure for getting vCPU binary stats
In reply to Sean ChristophersonNow that the binary stats cache infrastructure is largely scope agnostic, add support for vCPU-scoped stats. Like VM stats, open and cache the stats FD when the vCPU is created so that it's guaranteed to be valid when vcpu_get_stats() is invoked.
Account for the extra per-vCPU file descriptor in kvm_set_files_rlimit(), so that tests that create large VMs don't run afoul of resource limits.
To sanity check that the infrastructure actually works, and to get a bit of bonus coverage, add an assert in x86's xapic_ipi_test to verify that the number of HLTs executed by the test matches the number of HLT exits observed by KVM.
--- .../testing/selftests/kvm/include/kvm_util.h | 20 +++++++----- tools/testing/selftests/kvm/lib/kvm_util.c | 32 ++++++++----------- .../selftests/kvm/x86/xapic_ipi_test.c | 2 ++ 3 files changed, 27 insertions(+), 27 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +12 -8
@@ -61,6 +61,7 @@ struct kvm_vcpu {#ifdef __x86_64__struct kvm_cpuid2 *cpuid;#endif +struct kvm_binary_stats stats;struct kvm_dirty_gfn *dirty_gfns;uint32_t fetch_index;uint32_t dirty_gfns_count;
@@ -534,17 +535,20 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header, struct kvm_stats_desc *desc, uint64_t *data, size_t max_elements); -void __vm_get_stat (struct kvm_vm *vm,const char *stat_name,uint64_t *data, -size_t max_elements); +void kvm_get_stat (struct kvm_binary_stats *stats,const char *name, +uint64_t *data,size_t max_elements); -#define vm_get_stat (vm, stat) \ -({ \ - uint64_t data; \ - \ - __vm_get_stat(vm, #stat, &data, 1); \ - data; \ +#define __get_stat (stats, stat) \ +({ \ + uint64_t data; \ + \ + kvm_get_stat(stats, #stat, &data, 1); \ + data; \ }) +#define vm_get_stat (vm, stat) __get_stat(&(vm)->stats, stat) +#define vcpu_get_stat (vcpu, stat) __get_stat(&(vcpu)->stats, stat) +void vm_create_irqchip (struct kvm_vm *vm); static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
tools/testing/selftests/kvm/lib/kvm_util.c +13 -19
@@ -415,10 +415,11 @@ static uint64_t vm_nr_pages_required(enum vm_guest_mode mode,void kvm_set_files_rlimit (uint32_t nr_vcpus) {/* -* Number of file descriptors required, nr_vpucs vCPU fds + an arbitrary -* number for everything else. +* Each vCPU will open two file descriptors: the vCPU itself and the +* vCPU's binary stats file descriptor. Add an arbitrary amount of +* buffer for all other files a test may open. */ -int nr_fds_wanted = nr_vcpus +100 ; +int nr_fds_wanted = nr_vcpus *2 +100 ;struct rlimit rl;/*
@@ -746,6 +747,8 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct kvm_vcpu *vcpu) ret =close (vcpu->fd);TEST_ASSERT (!ret,__KVM_SYSCALL_ERROR ("close()" , ret)); +kvm_stats_release (&vcpu->stats); +list_del (&vcpu->list);vcpu_arch_free (vcpu);
@@ -1339,6 +1342,11 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)TEST_ASSERT (vcpu->run !=MAP_FAILED ,__KVM_SYSCALL_ERROR ("mmap()" , (int )(unsigned long )MAP_FAILED )); +if (kvm_has_cap (KVM_CAP_BINARY_STATS_FD )) + vcpu->stats.fd =vcpu_get_stats_fd (vcpu); +else + vcpu->stats.fd =-1 ; +/* Add to linked-list of VCPUs. */ list_add (&vcpu->list, &vm->vcpus);
@@ -2251,23 +2259,9 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header, desc->name, size, ret); } -/* -* Read the data of the named stat -* -* Input Args: -* vm - the VM for which the stat should be read -* stat_name - the name of the stat to read -* max_elements - the maximum number of 8-byte values to read into data -* -* Output Args: -* data - the buffer into which stat data should be read -* -* Read the data values of a specified stat from the binary stats interface. -*/ -void __vm_get_stat (struct kvm_vm *vm,const char *name,uint64_t *data, -size_t max_elements) +void kvm_get_stat (struct kvm_binary_stats *stats,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;
tools/testing/selftests/kvm/x86/xapic_ipi_test.c +2 -0
@@ -465,6 +465,8 @@ int main(int argc, char *argv[])cancel_join_vcpu_thread (threads[0 ], params[0 ].vcpu);cancel_join_vcpu_thread (threads[1 ], params[1 ].vcpu); +TEST_ASSERT_EQ (data->hlt_count,vcpu_get_stat (params[0 ].vcpu, halt_exits)); +fprintf (stderr,"Test successful after running for %d seconds.\n" "Sending vCPU sent %lu IPIs to halting vCPU\n"
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v2 8/9] KVM: selftests: Add infrastructure for getting vCPU binary stats
Quoted 132 lines · Sean Christopherson
Now that the binary stats cache infrastructure is largely scope agnostic, add support for vCPU-scoped stats. Like VM stats, open and cache the stats FD when the vCPU is created so that it's guaranteed to be valid when
vcpu_get_stats()is invoked.Account for the extra per-vCPU file descriptor in
kvm_set_files_rlimit(), so that tests that create large VMs don't run afoul of resource limits.To sanity check that the infrastructure actually works, and to get a bit of bonus coverage, add an assert in x86's
xapic_ipi_testto verify that the number of HLTs executed by the test matches the number of HLT exits observed by KVM.Signed-off-by Sean Christopherson <[email protected]>--- .../testing/selftests/kvm/include/kvm_util.h | 20 +++++++----- tools/testing/selftests/kvm/lib/kvm_util.c | 32 ++++++++----------- .../selftests/kvm/x86/xapic_ipi_test.c | 2 ++ 3 files changed, 27 insertions(+), 27 deletions(-)tools/testing/selftests/kvm/include/kvm_util.h +12
-8@@ -61,6 +61,7 @@ struct kvm_vcpu {#ifdef __x86_64__struct kvm_cpuid2 *cpuid;#endif +struct kvm_binary_stats stats;struct kvm_dirty_gfn *dirty_gfns;uint32_t fetch_index;uint32_t dirty_gfns_count;@@ -534,17 +535,20 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header, struct kvm_stats_desc *desc, uint64_t *data, size_t max_elements); -void __vm_get_stat (struct kvm_vm *vm,const char *stat_name,uint64_t *data, -size_t max_elements); +void kvm_get_stat (struct kvm_binary_stats *stats,const char *name, +uint64_t *data,size_t max_elements); -#define vm_get_stat (vm, stat) \ -({ \ - uint64_t data; \ - \ - __vm_get_stat(vm, #stat, &data, 1); \ - data; \ +#define __get_stat (stats, stat) \ +({ \ + uint64_t data; \ + \ + kvm_get_stat(stats, #stat, &data, 1); \ + data; \ }) +#define vm_get_stat (vm, stat) __get_stat(&(vm)->stats, stat) +#define vcpu_get_stat (vcpu, stat) __get_stat(&(vcpu)->stats, stat) +void vm_create_irqchip (struct kvm_vm *vm); static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,tools/testing/selftests/kvm/lib/kvm_util.c +13
-19@@ -415,10 +415,11 @@ static uint64_t vm_nr_pages_required(enum vm_guest_mode mode,void kvm_set_files_rlimit (uint32_t nr_vcpus) {/* -* Number of file descriptors required, nr_vpucs vCPU fds + an arbitrary -* number for everything else. +* Each vCPU will open two file descriptors: the vCPU itself and the +* vCPU's binary stats file descriptor. Add an arbitrary amount of +* buffer for all other files a test may open. */ -int nr_fds_wanted = nr_vcpus +100 ; +int nr_fds_wanted = nr_vcpus *2 +100 ;struct rlimit rl;/* @@ -746,6 +747,8 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct kvm_vcpu *vcpu) ret =close (vcpu->fd);TEST_ASSERT (!ret,__KVM_SYSCALL_ERROR ("close()" , ret)); +kvm_stats_release (&vcpu->stats); +list_del (&vcpu->list);vcpu_arch_free (vcpu);@@ -1339,6 +1342,11 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)TEST_ASSERT (vcpu->run !=MAP_FAILED ,__KVM_SYSCALL_ERROR ("mmap()" , (int )(unsigned long )MAP_FAILED )); +if (kvm_has_cap (KVM_CAP_BINARY_STATS_FD )) + vcpu->stats.fd =vcpu_get_stats_fd (vcpu); +else + vcpu->stats.fd =-1 ; +/* Add to linked-list of VCPUs. */ list_add (&vcpu->list, &vm->vcpus);@@ -2251,23 +2259,9 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header, desc->name, size, ret); } -/* -* Read the data of the named stat -* -* Input Args: -* vm - the VM for which the stat should be read -* stat_name - the name of the stat to read -* max_elements - the maximum number of 8-byte values to read into data -* -* Output Args: -* data - the buffer into which stat data should be read -* -* Read the data values of a specified stat from the binary stats interface. -*/ -void __vm_get_stat (struct kvm_vm *vm,const char *name,uint64_t *data, -size_t max_elements) +void kvm_get_stat (struct kvm_binary_stats *stats,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;tools/testing/selftests/kvm/x86/xapic_ipi_test.c +0
-0
tools/testing/selftests/kvm/x86/xapic_ipi_test.c int main(int argc, char *argv[]) · line 472
Quoted 8 lines
tools/testing/selftests/kvm/x86/xapic_ipi_test.c +2
-0@@ -465,6 +465,8 @@ int main(int argc, char *argv[])cancel_join_vcpu_thread (threads[0 ], params[0 ].vcpu);cancel_join_vcpu_thread (threads[1 ], params[1 ].vcpu); +TEST_ASSERT_EQ (data->hlt_count,vcpu_get_stat (params[0 ].vcpu, halt_exits)); +fprintf (stderr,"Test successful after running for %d seconds.\n" "Sending vCPU sent %lu IPIs to halting vCPU\n"
I have tested this infrastructure with xapic_ipi_test and ipi hlt test [1] on AMD system.
[1]: https://lore.kernel.org/kvm/[email protected]/T/#mda361fc0892e6949d98de2a4a79f68fc362a2893
-Manali
Sean Christopherson
maintainerPATCH v2 9/9 KVM: selftests: Add compile-time assertions to guard against stats typos
In reply to Sean ChristophersonAdd compile-time assertions to the main binary stats accessor to verify the stat is a known KVM stat of the appropriate scope, and "add" all known stats for all architectures.
To build the set of known stats, define enums for each stat, with a completely arbitrary magic value to specify the scope of the stat. With the assert in place, misuse of stat (or usage of a new stat) generates error like so:
In file included from include/x86/kvm_util_arch.h:8,
from include/kvm_util.h:23,
from x86/dirty_log_page_splitting_test.c:16: x86/dirty_log_page_splitting_test.c: In function ‘get_page_stats’:
include/kvm_util.h:563:42: error: ‘VM_STAT_pages_4m’ undeclared
(first use in this function); did you mean ‘VM_STAT_pages_2m’? 563 | #define vm_get_stat(vm, stat) __get_stat(VM, &(vm)->stats, stat)
| ^~
...
x86/dirty_log_page_splitting_test.c:45:27: note: in expansion of macro ‘vm_get_stat’
45 | stats->pages_2m = vm_get_stat(vm, pages_4m);
| ^~~~~~~~~~~Using pre-defined lists of stats doesn't completely eliminate human error, e.g. it's obviously possible to make a typo when adding a state. And while there is also a non-zero cost to maintaining the set of stats, adding stats in KVM is relatively uncommon, and removing stats is extremely rare. On the flip side, providing a list of known stats should make it easier to use stats in test, at which point detecting goofs at compile-time will also be more valuable.
--- .../kvm/include/arm64/kvm_util_arch.h | 12 ++ .../testing/selftests/kvm/include/kvm_util.h | 24 +++- .../selftests/kvm/include/kvm_util_types.h | 6 + .../kvm/include/riscv/kvm_util_arch.h | 14 +++ .../kvm/include/s390/kvm_util_arch.h | 113 ++++++++++++++++++ .../selftests/kvm/include/x86/kvm_util_arch.h | 52 ++++++++ 6 files changed, 218 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/arm64/kvm_util_arch.h +12 -0
@@ -2,6 +2,18 @@#ifndef SELFTEST_KVM_UTIL_ARCH_H #define SELFTEST_KVM_UTIL_ARCH_H +#include "kvm_util_types.h" +struct kvm_vm_arch {}; +enum kvm_arm64_stats { + VCPU_STAT(hvc_exit_stat), + VCPU_STAT(wfe_exit_stat), + VCPU_STAT(wfi_exit_stat), + VCPU_STAT(mmio_exit_user), + VCPU_STAT(mmio_exit_kernel), + VCPU_STAT(signal_exits), + VCPU_STAT(exits), +}; +#endif // SELFTEST_KVM_UTIL_ARCH_H
tools/testing/selftests/kvm/include/kvm_util.h +21 -3
@@ -535,19 +535,37 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header, struct kvm_stats_desc *desc, uint64_t *data, size_t max_elements); +enum kvm_common_stats { + VM_STAT(remote_tlb_flush), + VM_STAT(remote_tlb_flush_requests), + + VCPU_STAT(halt_successfull_poll), + VCPU_STAT(halt_attempted_poll), + VCPU_STAT(halt_poll_invalid), + VCPU_STAT(halt_wakeup), + VCPU_STAT(halt_poll_success_ns), + VCPU_STAT(halt_poll_fail_ns), + VCPU_STAT(halt_wait_ns), + VCPU_STAT(halt_poll_success_hist), + VCPU_STAT(halt_poll_fail_hist), + VCPU_STAT(halt_wait_hist), + VCPU_STAT(blocking), +}; +void kvm_get_stat (struct kvm_binary_stats *stats,const char *name,uint64_t *data,size_t max_elements); -#define __get_stat (stats, stat) \ +#define __get_stat (type, stats, stat) \ ({ \ uint64_t data; \ \ + kvm_static_assert(type##_STAT_##stat == type##_STAT_MAGIC_NUMBER); \ kvm_get_stat(stats, #stat, &data, 1); \ data; \ }) -#define vm_get_stat (vm, stat) __get_stat(&(vm)->stats, stat) -#define vcpu_get_stat (vcpu, stat) __get_stat(&(vcpu)->stats, stat) +#define vm_get_stat (vm, stat) __get_stat(VM, &(vm)->stats, stat) +#define vcpu_get_stat (vcpu, stat) __get_stat(VCPU, &(vcpu)->stats, stat)void vm_create_irqchip (struct kvm_vm *vm);
tools/testing/selftests/kvm/include/kvm_util_types.h +6 -0
@@ -17,4 +17,10 @@typedef uint64_t vm_paddr_t ;/* Virtual Machine (Guest) physical address */ typedef uint64_t vm_vaddr_t ;/* Virtual Machine (Guest) virtual address */ +#define VM_STAT_MAGIC_NUMBER 1 +#define VM_STAT (stat) VM_STAT_##stat = VM_STAT_MAGIC_NUMBER + +#define VCPU_STAT_MAGIC_NUMBER 2 +#define VCPU_STAT (stat) VCPU_STAT_##stat = VCPU_STAT_MAGIC_NUMBER +#endif /* SELFTEST_KVM_UTIL_TYPES_H */
tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h +14 -0
@@ -2,6 +2,20 @@#ifndef SELFTEST_KVM_UTIL_ARCH_H #define SELFTEST_KVM_UTIL_ARCH_H +#include "kvm_util_types.h" +struct kvm_vm_arch {}; +enum kvm_riscv_stats { + VCPU_STAT(ecall_exit_stat), + VCPU_STAT(wfi_exit_stat), + VCPU_STAT(wrs_exit_stat), + VCPU_STAT(mmio_exit_user), + VCPU_STAT(mmio_exit_kernel), + VCPU_STAT(csr_exit_user), + VCPU_STAT(csr_exit_kernel), + VCPU_STAT(signal_exits), + VCPU_STAT(exits), +}; +#endif // SELFTEST_KVM_UTIL_ARCH_H
tools/testing/selftests/kvm/include/s390/kvm_util_arch.h +113 -0
119 lines not on this page · raw message
tools/testing/selftests/kvm/include/x86/kvm_util_arch.h +52 -0
56 lines not on this page · raw message
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Sean Christopherson
maintainerQuoted 11 lines · Sean Christopherson
Fix a handful of bugs in the binary stats infrastructure, expand support to vCPU-scoped stats, enumerate all KVM stats in selftests, and use the enumerated stats to assert at compile-time that {vm,vcpu}_get_stat() is getting a stat that actually exists.
Most of the bugs are benign, and AFAICT, none actually cause problems in the current code base. The worst of the bugs is lack of validation that the requested stat actually exists, which is quite annoying if someone fat fingers a stat name, tries to get a vCPU stat on a VM FD, etc.
[...]
Applied to kvm-x86 selftests (attempt #2). Like the previous attempt, I skipped the compile-time assertions.
[1/9] KVM: selftests: Fix mostly theoretical leak of VM's binary stats FD
https://github.com/kvm-x86/linux/commit/fd546aba1967 [2/9] KVM: selftests: Close VM's binary stats FD when releasing VM https://github.com/kvm-x86/linux/commit/f7f232a01f3d [3/9] KVM: selftests: Assert that __vm_get_stat() actually finds a stat https://github.com/kvm-x86/linux/commit/eead13d493af [4/9] KVM: selftests: Macrofy vm_get_stat() to auto-generate stat name string https://github.com/kvm-x86/linux/commit/b0c3f5df9291 [5/9] KVM: selftests: Add struct and helpers to wrap binary stats cache https://github.com/kvm-x86/linux/commit/e65faf71bd54 [6/9] KVM: selftests: Get VM's binary stats FD when opening VM https://github.com/kvm-x86/linux/commit/ea7179f99514 [7/9] KVM: selftests: Adjust number of files rlimit for all "standard" VMs https://github.com/kvm-x86/linux/commit/9b56532b8a59 [8/9] KVM: selftests: Add infrastructure for getting vCPU binary stats https://github.com/kvm-x86/linux/commit/16fc7cb406a5
[9/9] KVM: selftests: Add compile-time assertions to guard against stats typos
(no commit info)