Re: [PATCH v9 4/5] KVM: selftests: Add selftest for KVM statistics data binary interface

Fuad Tabba
2021-06-15T08:03:54Z

Hi Jing,

> +int main(int argc, char *argv[])
> +{
> + int max_vm = DEFAULT_NUM_VM, max_vcpu = DEFAULT_NUM_VCPU, ret, i, j;
> + struct kvm_vm **vms;
> +
> + /* Get the number of VMs and VCPUs that would be created for testing. */
> + if (argc > 1) {
> + max_vm = strtol(argv[1], NULL, 0);
> + if (max_vm <= 0)
> + max_vm = DEFAULT_NUM_VM;
> + }
> + if (argc > 2) {
> + max_vcpu = strtol(argv[2], NULL, 0);
> + if (max_vcpu <= 0)
> + max_vcpu = DEFAULT_NUM_VCPU;
> + }
> +
> + /* Check the extension for binary stats */
> + ret = kvm_check_cap(KVM_CAP_BINARY_STATS_FD);
> + TEST_ASSERT(ret >= 0,
> + "Binary form statistics interface is not supported");

kvm_check_cap returns the value of KVM_CHECK_EXTENSION, which is 0 if
unsupported (-ERROR on an error). The assertion should be for ret > 0.

Made that change locally, and tested it with various configurations
(vhe, nvhe), as well as kernel versions (with and without
KVM_CAP_BINARY_STATS_FD), and it passes (or fails as expected).
Without that fix and with a kernel that doesn't support
KVM_CAP_BINARY_STATS_FD, it passes that assertion, but fails later at
vcpu_stats_test().

With that fixed:
Tested-by: Fuad Tabba <tabba@google.com> #arm64

Cheers,
/fuad

> +
> + /* Create VMs and VCPUs */
> + vms = malloc(sizeof(vms[0]) * max_vm);
> + TEST_ASSERT(vms, "Allocate memory for storing VM pointers");
> + for (i = 0; i < max_vm; ++i) {
> + vms[i] = vm_create(VM_MODE_DEFAULT,
> + DEFAULT_GUEST_PHY_PAGES, O_RDWR);
> + for (j = 0; j < max_vcpu; ++j)
> + vm_vcpu_add(vms[i], j);
> + }
> +
> + /* Check stats read for every VM and VCPU */
> + for (i = 0; i < max_vm; ++i) {
> + vm_stats_test(vms[i]);
> + for (j = 0; j < max_vcpu; ++j)
> + vcpu_stats_test(vms[i], j);
> + }
> +
> + for (i = 0; i < max_vm; ++i)
> + kvm_vm_free(vms[i]);
> + free(vms);
> + return 0;
> +}
> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> index 5c70596dd1b9..83c02cb0ae1e 100644
> --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> @@ -2286,3 +2286,15 @@ unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size)
> n = DIV_ROUND_UP(size, vm_guest_mode_params[mode].page_size);
> return vm_adjust_num_guest_pages(mode, n);
> }
> +
> +int vm_get_stats_fd(struct kvm_vm *vm)
> +{
> + return ioctl(vm->fd, KVM_GET_STATS_FD, NULL);
> +}
> +
> +int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid)
> +{
> + struct vcpu *vcpu = vcpu_find(vm, vcpuid);
> +
> + return ioctl(vcpu->fd, KVM_GET_STATS_FD, NULL);
> +}
> --
> 2.32.0.272.g935e593368-goog
>