]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: selftests: Use kvm_ioctl() helpers
authorSean Christopherson <seanjc@google.com>
Tue, 15 Feb 2022 20:39:12 +0000 (12:39 -0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Sat, 11 Jun 2022 14:20:09 +0000 (10:20 -0400)
Use the recently introduced KVM-specific ioctl() helpers instead of open
coding calls to ioctl() just to pretty print the ioctl name.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/testing/selftests/kvm/lib/aarch64/processor.c
tools/testing/selftests/kvm/lib/guest_modes.c
tools/testing/selftests/kvm/lib/kvm_util.c
tools/testing/selftests/kvm/lib/x86_64/processor.c
tools/testing/selftests/kvm/x86_64/get_msr_index_features.c
tools/testing/selftests/kvm/x86_64/mmio_warning_test.c

index 6a041289fa801bc2e52a4d2ad49a80dfd1559c97..d28cc12cea1d87254ac792a6398291d426fcfe08 100644 (file)
@@ -469,8 +469,8 @@ void aarch64_get_supported_page_sizes(uint32_t ipa,
        };
 
        kvm_fd = open_kvm_dev_path_or_exit();
-       vm_fd = ioctl(kvm_fd, KVM_CREATE_VM, ipa);
-       TEST_ASSERT(vm_fd >= 0, "Can't create VM");
+       vm_fd = __kvm_ioctl(kvm_fd, KVM_CREATE_VM, ipa);
+       TEST_ASSERT(vm_fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm_fd));
 
        vcpu_fd = ioctl(vm_fd, KVM_CREATE_VCPU, 0);
        TEST_ASSERT(vcpu_fd >= 0, "Can't create vcpu");
index 8784013b747ca37c3ef0bfb99ce49c5cef3143ea..9ab27b4169bf61d1967a2e4fd56f56e923b97d88 100644 (file)
@@ -65,7 +65,7 @@ void guest_modes_append_default(void)
                struct kvm_s390_vm_cpu_processor info;
 
                kvm_fd = open_kvm_dev_path_or_exit();
-               vm_fd = ioctl(kvm_fd, KVM_CREATE_VM, 0);
+               vm_fd = __kvm_ioctl(kvm_fd, KVM_CREATE_VM, 0);
                kvm_device_access(vm_fd, KVM_S390_VM_CPU_MODEL,
                                  KVM_S390_VM_CPU_PROCESSOR, &info, false);
                close(vm_fd);
index ac8faf0722885bb3ccf348118e43ddc38197c15e..4d2748e8428a70a9c64326c780fb027ef28f7fcb 100644 (file)
@@ -76,9 +76,8 @@ int kvm_check_cap(long cap)
        int kvm_fd;
 
        kvm_fd = open_kvm_dev_path_or_exit();
-       ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, cap);
-       TEST_ASSERT(ret >= 0, "KVM_CHECK_EXTENSION IOCTL failed,\n"
-               "  rc: %i errno: %i", ret, errno);
+       ret = __kvm_ioctl(kvm_fd, KVM_CHECK_EXTENSION, cap);
+       TEST_ASSERT(ret >= 0, KVM_IOCTL_ERROR(KVM_CHECK_EXTENSION, ret));
 
        close(kvm_fd);
 
@@ -104,9 +103,8 @@ static void vm_open(struct kvm_vm *vm)
                exit(KSFT_SKIP);
        }
 
-       vm->fd = ioctl(vm->kvm_fd, KVM_CREATE_VM, vm->type);
-       TEST_ASSERT(vm->fd >= 0, "KVM_CREATE_VM ioctl failed, "
-               "rc: %i errno: %i", vm->fd, errno);
+       vm->fd = __kvm_ioctl(vm->kvm_fd, KVM_CREATE_VM, vm->type);
+       TEST_ASSERT(vm->fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm->fd));
 }
 
 const char *vm_guest_mode_string(uint32_t i)
@@ -1070,8 +1068,7 @@ static int vcpu_mmap_sz(void)
 
        ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
        TEST_ASSERT(ret >= sizeof(struct kvm_run),
-               "%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i",
-               __func__, ret, errno);
+                   KVM_IOCTL_ERROR(KVM_GET_VCPU_MMAP_SIZE, ret));
 
        close(dev_fd);
 
index 723cb49d19940c28a74d53d45901a8ba1649b2fc..852274d64483160c28fb8b151a0ea40d24a641e1 100644 (file)
@@ -638,7 +638,7 @@ void vm_xsave_req_perm(int bit)
        };
 
        kvm_fd = open_kvm_dev_path_or_exit();
-       rc = ioctl(kvm_fd, KVM_GET_DEVICE_ATTR, &attr);
+       rc = __kvm_ioctl(kvm_fd, KVM_GET_DEVICE_ATTR, &attr);
        close(kvm_fd);
        if (rc == -1 && (errno == ENXIO || errno == EINVAL))
                exit(KSFT_SKIP);
@@ -738,7 +738,6 @@ static struct kvm_cpuid2 *allocate_kvm_cpuid2(void)
 struct kvm_cpuid2 *kvm_get_supported_cpuid(void)
 {
        static struct kvm_cpuid2 *cpuid;
-       int ret;
        int kvm_fd;
 
        if (cpuid)
@@ -747,9 +746,7 @@ struct kvm_cpuid2 *kvm_get_supported_cpuid(void)
        cpuid = allocate_kvm_cpuid2();
        kvm_fd = open_kvm_dev_path_or_exit();
 
-       ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_CPUID, cpuid);
-       TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_CPUID failed %d %d\n",
-                   ret, errno);
+       kvm_ioctl(kvm_fd, KVM_GET_SUPPORTED_CPUID, cpuid);
 
        close(kvm_fd);
        return cpuid;
@@ -779,9 +776,8 @@ uint64_t kvm_get_feature_msr(uint64_t msr_index)
        buffer.entry.index = msr_index;
        kvm_fd = open_kvm_dev_path_or_exit();
 
-       r = ioctl(kvm_fd, KVM_GET_MSRS, &buffer.header);
-       TEST_ASSERT(r == 1, "KVM_GET_MSRS IOCTL failed,\n"
-               "  rc: %i errno: %i", r, errno);
+       r = __kvm_ioctl(kvm_fd, KVM_GET_MSRS, &buffer.header);
+       TEST_ASSERT(r == 1, KVM_IOCTL_ERROR(KVM_GET_MSRS, r));
 
        close(kvm_fd);
        return buffer.entry.data;
@@ -946,9 +942,9 @@ static int kvm_get_num_msrs_fd(int kvm_fd)
        int r;
 
        nmsrs.nmsrs = 0;
-       r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, &nmsrs);
-       TEST_ASSERT(r == -1 && errno == E2BIG, "Unexpected result from KVM_GET_MSR_INDEX_LIST probe, r: %i",
-               r);
+       r = __kvm_ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, &nmsrs);
+       TEST_ASSERT(r == -1 && errno == E2BIG,
+                   "Unexpected result from KVM_GET_MSR_INDEX_LIST probe, r: %i", r);
 
        return nmsrs.nmsrs;
 }
@@ -961,19 +957,16 @@ static int kvm_get_num_msrs(struct kvm_vm *vm)
 struct kvm_msr_list *kvm_get_msr_index_list(void)
 {
        struct kvm_msr_list *list;
-       int nmsrs, r, kvm_fd;
+       int nmsrs, kvm_fd;
 
        kvm_fd = open_kvm_dev_path_or_exit();
 
        nmsrs = kvm_get_num_msrs_fd(kvm_fd);
        list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
        list->nmsrs = nmsrs;
-       r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
+       kvm_ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
        close(kvm_fd);
 
-       TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_MSR_INDEX_LIST, r: %i",
-               r);
-
        return list;
 }
 
@@ -1019,9 +1012,7 @@ struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid)
        nmsrs = kvm_get_num_msrs(vm);
        list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
        list->nmsrs = nmsrs;
-       r = ioctl(vm->kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
-       TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_MSR_INDEX_LIST, r: %i",
-                   r);
+       kvm_ioctl(vm->kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
 
        state = malloc(sizeof(*state) + nmsrs * sizeof(state->msrs.entries[0]));
        r = ioctl(vcpu->fd, KVM_GET_VCPU_EVENTS, &state->events);
@@ -1329,7 +1320,6 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,
 struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void)
 {
        static struct kvm_cpuid2 *cpuid;
-       int ret;
        int kvm_fd;
 
        if (cpuid)
@@ -1338,9 +1328,7 @@ struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void)
        cpuid = allocate_kvm_cpuid2();
        kvm_fd = open_kvm_dev_path_or_exit();
 
-       ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
-       TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_HV_CPUID failed %d %d\n",
-                   ret, errno);
+       kvm_ioctl(kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
 
        close(kvm_fd);
        return cpuid;
index 8aed0db1331d11dc141a4dfbedf331c854514804..4ef60adbe108f36f02683962d18f909e563c581e 100644 (file)
@@ -34,7 +34,7 @@ static int kvm_num_index_msrs(int kvm_fd, int nmsrs)
 
 static void test_get_msr_index(void)
 {
-       int old_res, res, kvm_fd, r;
+       int old_res, res, kvm_fd;
        struct kvm_msr_list *list;
 
        kvm_fd = open_kvm_dev_path_or_exit();
@@ -50,11 +50,8 @@ static void test_get_msr_index(void)
 
        list = malloc(sizeof(*list) + old_res * sizeof(list->indices[0]));
        list->nmsrs = old_res;
-       r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
+       kvm_ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
 
-       TEST_ASSERT(r == 0,
-                   "Unexpected result from KVM_GET_MSR_FEATURE_INDEX_LIST, r: %i",
-                   r);
        TEST_ASSERT(list->nmsrs == old_res, "Expecting nmsrs to be identical");
        free(list);
 
@@ -68,7 +65,7 @@ static int kvm_num_feature_msrs(int kvm_fd, int nmsrs)
 
        list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
        list->nmsrs = nmsrs;
-       r = ioctl(kvm_fd, KVM_GET_MSR_FEATURE_INDEX_LIST, list);
+       r = __kvm_ioctl(kvm_fd, KVM_GET_MSR_FEATURE_INDEX_LIST, list);
        TEST_ASSERT(r == -1 && errno == E2BIG,
                "Unexpected result from KVM_GET_MSR_FEATURE_INDEX_LIST probe, r: %i",
                                r);
@@ -81,15 +78,10 @@ static int kvm_num_feature_msrs(int kvm_fd, int nmsrs)
 struct kvm_msr_list *kvm_get_msr_feature_list(int kvm_fd, int nmsrs)
 {
        struct kvm_msr_list *list;
-       int r;
 
        list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
        list->nmsrs = nmsrs;
-       r = ioctl(kvm_fd, KVM_GET_MSR_FEATURE_INDEX_LIST, list);
-
-       TEST_ASSERT(r == 0,
-               "Unexpected result from KVM_GET_MSR_FEATURE_INDEX_LIST, r: %i",
-               r);
+       kvm_ioctl(kvm_fd, KVM_GET_MSR_FEATURE_INDEX_LIST, list);
 
        return list;
 }
index 9f55ccd169a13745d1897663784bfb7f9801ee09..31ae837fedb18994dd5d62484d8b3e418646409e 100644 (file)
@@ -59,10 +59,10 @@ void test(void)
 
        kvm = open("/dev/kvm", O_RDWR);
        TEST_ASSERT(kvm != -1, "failed to open /dev/kvm");
-       kvmvm = ioctl(kvm, KVM_CREATE_VM, 0);
-       TEST_ASSERT(kvmvm != -1, "KVM_CREATE_VM failed");
+       kvmvm = __kvm_ioctl(kvm, KVM_CREATE_VM, 0);
+       TEST_ASSERT(kvmvm > 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, kvmvm));
        kvmcpu = ioctl(kvmvm, KVM_CREATE_VCPU, 0);
-       TEST_ASSERT(kvmcpu != -1, "KVM_CREATE_VCPU failed");
+       TEST_ASSERT(kvmcpu != -1, KVM_IOCTL_ERROR(KVM_CREATE_VCPU, kvmcpu));
        run = (struct kvm_run *)mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_SHARED,
                                    kvmcpu, 0);
        tc.kvmcpu = kvmcpu;