Skip to content

Commit 47d1be8

Browse files
sean-jcbonzini
authored andcommitted
KVM: selftests: Verify userspace can create "redundant" binary stats files
Verify that KVM doesn't artificially limit KVM_GET_STATS_FD to a single file per VM/vCPU. There's no known use case for getting multiple stats fds, but it should work, and more importantly creating multiple files will make it easier to test that KVM correct manages VM refcounts for stats files. Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20230711230131.648752-6-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 33b0270 commit 47d1be8

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

tools/testing/selftests/kvm/kvm_binary_stats_test.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ static void stats_test(int stats_fd)
185185

186186
int main(int argc, char *argv[])
187187
{
188+
int vm_stats_fds, *vcpu_stats_fds;
188189
int i, j;
189190
struct kvm_vcpu **vcpus;
190191
struct kvm_vm **vms;
@@ -217,18 +218,37 @@ int main(int argc, char *argv[])
217218
vcpus = malloc(sizeof(struct kvm_vcpu *) * max_vm * max_vcpu);
218219
TEST_ASSERT(vcpus, "Allocate memory for storing vCPU pointers");
219220

221+
/*
222+
* Not per-VM as the array is populated, used, and invalidated within a
223+
* single for-loop iteration.
224+
*/
225+
vcpu_stats_fds = calloc(max_vm, sizeof(*vcpu_stats_fds));
226+
TEST_ASSERT(vcpu_stats_fds, "Allocate memory for VM stats fds");
227+
220228
for (i = 0; i < max_vm; ++i) {
221229
vms[i] = vm_create_barebones();
222230
for (j = 0; j < max_vcpu; ++j)
223231
vcpus[i * max_vcpu + j] = __vm_vcpu_add(vms[i], j);
224232
}
225233

226-
/* Check stats read for every VM and VCPU */
234+
/*
235+
* Check stats read for every VM and vCPU, with a variety of testcases.
236+
* Note, stats_test() closes the passed in stats fd.
237+
*/
227238
for (i = 0; i < max_vm; ++i) {
239+
vm_stats_fds = vm_get_stats_fd(vms[i]);
240+
241+
/* Verify userspace can instantiate multiple stats files. */
228242
stats_test(vm_get_stats_fd(vms[i]));
229243

230-
for (j = 0; j < max_vcpu; ++j)
244+
for (j = 0; j < max_vcpu; ++j) {
245+
vcpu_stats_fds[j] = vcpu_get_stats_fd(vcpus[i * max_vcpu + j]);
231246
stats_test(vcpu_get_stats_fd(vcpus[i * max_vcpu + j]));
247+
}
248+
249+
stats_test(vm_stats_fds);
250+
for (j = 0; j < max_vcpu; ++j)
251+
stats_test(vcpu_stats_fds[j]);
232252

233253
ksft_test_result_pass("vm%i\n", i);
234254
}
@@ -237,6 +257,7 @@ int main(int argc, char *argv[])
237257
kvm_vm_free(vms[i]);
238258
free(vms);
239259
free(vcpus);
260+
free(vcpu_stats_fds);
240261

241262
ksft_finished(); /* Print results and exit() accordingly */
242263
}

0 commit comments

Comments
 (0)