Skip to content

Commit 16fc7cb

Browse files
committed
KVM: selftests: Add infrastructure for getting vCPU binary stats
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_test to verify that the number of HLTs executed by the test matches the number of HLT exits observed by KVM. Tested-by: Manali Shukla <Manali.Shukla@amd.com> Link: https://lore.kernel.org/r/20250111005049.1247555-9-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 9b56532 commit 16fc7cb

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

tools/testing/selftests/kvm/include/kvm_util.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct kvm_vcpu {
6161
#ifdef __x86_64__
6262
struct kvm_cpuid2 *cpuid;
6363
#endif
64+
struct kvm_binary_stats stats;
6465
struct kvm_dirty_gfn *dirty_gfns;
6566
uint32_t fetch_index;
6667
uint32_t dirty_gfns_count;
@@ -534,17 +535,20 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header,
534535
struct kvm_stats_desc *desc, uint64_t *data,
535536
size_t max_elements);
536537

537-
void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,
538-
size_t max_elements);
538+
void kvm_get_stat(struct kvm_binary_stats *stats, const char *name,
539+
uint64_t *data, size_t max_elements);
539540

540-
#define vm_get_stat(vm, stat) \
541-
({ \
542-
uint64_t data; \
543-
\
544-
__vm_get_stat(vm, #stat, &data, 1); \
545-
data; \
541+
#define __get_stat(stats, stat) \
542+
({ \
543+
uint64_t data; \
544+
\
545+
kvm_get_stat(stats, #stat, &data, 1); \
546+
data; \
546547
})
547548

549+
#define vm_get_stat(vm, stat) __get_stat(&(vm)->stats, stat)
550+
#define vcpu_get_stat(vcpu, stat) __get_stat(&(vcpu)->stats, stat)
551+
548552
void vm_create_irqchip(struct kvm_vm *vm);
549553

550554
static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,11 @@ static uint64_t vm_nr_pages_required(enum vm_guest_mode mode,
415415
void kvm_set_files_rlimit(uint32_t nr_vcpus)
416416
{
417417
/*
418-
* Number of file descriptors required, nr_vpucs vCPU fds + an arbitrary
419-
* number for everything else.
418+
* Each vCPU will open two file descriptors: the vCPU itself and the
419+
* vCPU's binary stats file descriptor. Add an arbitrary amount of
420+
* buffer for all other files a test may open.
420421
*/
421-
int nr_fds_wanted = nr_vcpus + 100;
422+
int nr_fds_wanted = nr_vcpus * 2 + 100;
422423
struct rlimit rl;
423424

424425
/*
@@ -746,6 +747,8 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
746747
ret = close(vcpu->fd);
747748
TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
748749

750+
kvm_stats_release(&vcpu->stats);
751+
749752
list_del(&vcpu->list);
750753

751754
vcpu_arch_free(vcpu);
@@ -1339,6 +1342,11 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
13391342
TEST_ASSERT(vcpu->run != MAP_FAILED,
13401343
__KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED));
13411344

1345+
if (kvm_has_cap(KVM_CAP_BINARY_STATS_FD))
1346+
vcpu->stats.fd = vcpu_get_stats_fd(vcpu);
1347+
else
1348+
vcpu->stats.fd = -1;
1349+
13421350
/* Add to linked-list of VCPUs. */
13431351
list_add(&vcpu->list, &vm->vcpus);
13441352

@@ -2251,23 +2259,9 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header,
22512259
desc->name, size, ret);
22522260
}
22532261

2254-
/*
2255-
* Read the data of the named stat
2256-
*
2257-
* Input Args:
2258-
* vm - the VM for which the stat should be read
2259-
* stat_name - the name of the stat to read
2260-
* max_elements - the maximum number of 8-byte values to read into data
2261-
*
2262-
* Output Args:
2263-
* data - the buffer into which stat data should be read
2264-
*
2265-
* Read the data values of a specified stat from the binary stats interface.
2266-
*/
2267-
void __vm_get_stat(struct kvm_vm *vm, const char *name, uint64_t *data,
2268-
size_t max_elements)
2262+
void kvm_get_stat(struct kvm_binary_stats *stats, const char *name,
2263+
uint64_t *data, size_t max_elements)
22692264
{
2270-
struct kvm_binary_stats *stats = &vm->stats;
22712265
struct kvm_stats_desc *desc;
22722266
size_t size_desc;
22732267
int i;

tools/testing/selftests/kvm/x86/xapic_ipi_test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ int main(int argc, char *argv[])
465465
cancel_join_vcpu_thread(threads[0], params[0].vcpu);
466466
cancel_join_vcpu_thread(threads[1], params[1].vcpu);
467467

468+
TEST_ASSERT_EQ(data->hlt_count, vcpu_get_stat(params[0].vcpu, halt_exits));
469+
468470
fprintf(stderr,
469471
"Test successful after running for %d seconds.\n"
470472
"Sending vCPU sent %lu IPIs to halting vCPU\n"

0 commit comments

Comments
 (0)