Commit f17cf567 authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: selftests: Use __KVM_SYSCALL_ERROR() to handle non-KVM syscall errors

Use __KVM_SYSCALL_ERROR() to report and pretty print non-KVM syscall and
ioctl errors, e.g. for mmap(), munmap(), uffd ioctls, etc...
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent f9725f89
...@@ -630,8 +630,7 @@ static void kvm_routing_and_irqfd_check(struct kvm_vm *vm, ...@@ -630,8 +630,7 @@ static void kvm_routing_and_irqfd_check(struct kvm_vm *vm,
for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++) { for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++) {
fd[f] = eventfd(0, 0); fd[f] = eventfd(0, 0);
TEST_ASSERT(fd[f] != -1, TEST_ASSERT(fd[f] != -1, __KVM_SYSCALL_ERROR("eventfd()", fd[f]));
"eventfd failed, errno: %i\n", errno);
} }
for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++) { for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++) {
...@@ -647,7 +646,7 @@ static void kvm_routing_and_irqfd_check(struct kvm_vm *vm, ...@@ -647,7 +646,7 @@ static void kvm_routing_and_irqfd_check(struct kvm_vm *vm,
val = 1; val = 1;
ret = write(fd[f], &val, sizeof(uint64_t)); ret = write(fd[f], &val, sizeof(uint64_t));
TEST_ASSERT(ret == sizeof(uint64_t), TEST_ASSERT(ret == sizeof(uint64_t),
"Write to KVM_IRQFD failed with ret: %d\n", ret); __KVM_SYSCALL_ERROR("write()", ret));
} }
for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++) for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++)
......
...@@ -223,6 +223,7 @@ static void setup_demand_paging(struct kvm_vm *vm, ...@@ -223,6 +223,7 @@ static void setup_demand_paging(struct kvm_vm *vm,
struct uffdio_api uffdio_api; struct uffdio_api uffdio_api;
struct uffdio_register uffdio_register; struct uffdio_register uffdio_register;
uint64_t expected_ioctls = ((uint64_t) 1) << _UFFDIO_COPY; uint64_t expected_ioctls = ((uint64_t) 1) << _UFFDIO_COPY;
int ret;
PER_PAGE_DEBUG("Userfaultfd %s mode, faults resolved with %s\n", PER_PAGE_DEBUG("Userfaultfd %s mode, faults resolved with %s\n",
is_minor ? "MINOR" : "MISSING", is_minor ? "MINOR" : "MISSING",
...@@ -242,19 +243,18 @@ static void setup_demand_paging(struct kvm_vm *vm, ...@@ -242,19 +243,18 @@ static void setup_demand_paging(struct kvm_vm *vm,
} }
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
TEST_ASSERT(uffd >= 0, "uffd creation failed, errno: %d", errno); TEST_ASSERT(uffd >= 0, __KVM_SYSCALL_ERROR("userfaultfd()", uffd));
uffdio_api.api = UFFD_API; uffdio_api.api = UFFD_API;
uffdio_api.features = 0; uffdio_api.features = 0;
TEST_ASSERT(ioctl(uffd, UFFDIO_API, &uffdio_api) != -1, ret = ioctl(uffd, UFFDIO_API, &uffdio_api);
"ioctl UFFDIO_API failed: %" PRIu64, TEST_ASSERT(ret != -1, __KVM_SYSCALL_ERROR("UFFDIO_API", ret));
(uint64_t)uffdio_api.api);
uffdio_register.range.start = (uint64_t)hva; uffdio_register.range.start = (uint64_t)hva;
uffdio_register.range.len = len; uffdio_register.range.len = len;
uffdio_register.mode = uffd_mode; uffdio_register.mode = uffd_mode;
TEST_ASSERT(ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) != -1, ret = ioctl(uffd, UFFDIO_REGISTER, &uffdio_register);
"ioctl UFFDIO_REGISTER failed"); TEST_ASSERT(ret != -1, __KVM_SYSCALL_ERROR("UFFDIO_REGISTER", ret));
TEST_ASSERT((uffdio_register.ioctls & expected_ioctls) == TEST_ASSERT((uffdio_register.ioctls & expected_ioctls) ==
expected_ioctls, "missing userfaultfd ioctls"); expected_ioctls, "missing userfaultfd ioctls");
......
...@@ -517,17 +517,15 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct vcpu *vcpu) ...@@ -517,17 +517,15 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct vcpu *vcpu)
if (vcpu->dirty_gfns) { if (vcpu->dirty_gfns) {
ret = munmap(vcpu->dirty_gfns, vm->dirty_ring_size); ret = munmap(vcpu->dirty_gfns, vm->dirty_ring_size);
TEST_ASSERT(ret == 0, "munmap of VCPU dirty ring failed, " TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
"rc: %i errno: %i", ret, errno);
vcpu->dirty_gfns = NULL; vcpu->dirty_gfns = NULL;
} }
ret = munmap(vcpu->state, vcpu_mmap_sz()); ret = munmap(vcpu->state, vcpu_mmap_sz());
TEST_ASSERT(ret == 0, "munmap of VCPU fd failed, rc: %i " TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
"errno: %i", ret, errno);
ret = close(vcpu->fd); ret = close(vcpu->fd);
TEST_ASSERT(ret == 0, "Close of VCPU fd failed, rc: %i " TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
"errno: %i", ret, errno);
list_del(&vcpu->list); list_del(&vcpu->list);
free(vcpu); free(vcpu);
...@@ -542,12 +540,10 @@ void kvm_vm_release(struct kvm_vm *vmp) ...@@ -542,12 +540,10 @@ void kvm_vm_release(struct kvm_vm *vmp)
vm_vcpu_rm(vmp, vcpu); vm_vcpu_rm(vmp, vcpu);
ret = close(vmp->fd); ret = close(vmp->fd);
TEST_ASSERT(ret == 0, "Close of vm fd failed,\n" TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
" vmp->fd: %i rc: %i errno: %i", vmp->fd, ret, errno);
ret = close(vmp->kvm_fd); ret = close(vmp->kvm_fd);
TEST_ASSERT(ret == 0, "Close of /dev/kvm fd failed,\n" TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
" vmp->kvm_fd: %i rc: %i errno: %i", vmp->kvm_fd, ret, errno);
} }
static void __vm_mem_region_delete(struct kvm_vm *vm, static void __vm_mem_region_delete(struct kvm_vm *vm,
...@@ -567,7 +563,7 @@ static void __vm_mem_region_delete(struct kvm_vm *vm, ...@@ -567,7 +563,7 @@ static void __vm_mem_region_delete(struct kvm_vm *vm,
sparsebit_free(&region->unused_phy_pages); sparsebit_free(&region->unused_phy_pages);
ret = munmap(region->mmap_start, region->mmap_size); ret = munmap(region->mmap_start, region->mmap_size);
TEST_ASSERT(ret == 0, "munmap failed, rc: %i errno: %i", ret, errno); TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
free(region); free(region);
} }
...@@ -607,14 +603,13 @@ int kvm_memfd_alloc(size_t size, bool hugepages) ...@@ -607,14 +603,13 @@ int kvm_memfd_alloc(size_t size, bool hugepages)
memfd_flags |= MFD_HUGETLB; memfd_flags |= MFD_HUGETLB;
fd = memfd_create("kvm_selftest", memfd_flags); fd = memfd_create("kvm_selftest", memfd_flags);
TEST_ASSERT(fd != -1, "memfd_create() failed, errno: %i (%s)", TEST_ASSERT(fd != -1, __KVM_SYSCALL_ERROR("memfd_create()", fd));
errno, strerror(errno));
r = ftruncate(fd, size); r = ftruncate(fd, size);
TEST_ASSERT(!r, "ftruncate() failed, errno: %i (%s)", errno, strerror(errno)); TEST_ASSERT(!r, __KVM_SYSCALL_ERROR("ftruncate()", r));
r = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, size); r = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, size);
TEST_ASSERT(!r, "fallocate() failed, errno: %i (%s)", errno, strerror(errno)); TEST_ASSERT(!r, __KVM_SYSCALL_ERROR("fallocate()", r));
return fd; return fd;
} }
...@@ -880,8 +875,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, ...@@ -880,8 +875,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
vm_mem_backing_src_alias(src_type)->flag, vm_mem_backing_src_alias(src_type)->flag,
region->fd, 0); region->fd, 0);
TEST_ASSERT(region->mmap_start != MAP_FAILED, TEST_ASSERT(region->mmap_start != MAP_FAILED,
"test_malloc failed, mmap_start: %p errno: %i", __KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED));
region->mmap_start, errno);
TEST_ASSERT(!is_backing_src_hugetlb(src_type) || TEST_ASSERT(!is_backing_src_hugetlb(src_type) ||
region->mmap_start == align_ptr_up(region->mmap_start, backing_src_pagesz), region->mmap_start == align_ptr_up(region->mmap_start, backing_src_pagesz),
...@@ -929,7 +923,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, ...@@ -929,7 +923,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
vm_mem_backing_src_alias(src_type)->flag, vm_mem_backing_src_alias(src_type)->flag,
region->fd, 0); region->fd, 0);
TEST_ASSERT(region->mmap_alias != MAP_FAILED, TEST_ASSERT(region->mmap_alias != MAP_FAILED,
"mmap of alias failed, errno: %i", errno); __KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED));
/* Align host alias address */ /* Align host alias address */
region->host_alias = align_ptr_up(region->mmap_alias, alignment); region->host_alias = align_ptr_up(region->mmap_alias, alignment);
...@@ -1115,8 +1109,8 @@ void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid) ...@@ -1115,8 +1109,8 @@ void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid)
vcpu_mmap_sz(), sizeof(*vcpu->state)); vcpu_mmap_sz(), sizeof(*vcpu->state));
vcpu->state = (struct kvm_run *) mmap(NULL, vcpu_mmap_sz(), vcpu->state = (struct kvm_run *) mmap(NULL, vcpu_mmap_sz(),
PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0); PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0);
TEST_ASSERT(vcpu->state != MAP_FAILED, "mmap vcpu_state failed, " TEST_ASSERT(vcpu->state != MAP_FAILED,
"vcpu id: %u errno: %i", vcpuid, errno); __KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED));
/* Add to linked-list of VCPUs. */ /* Add to linked-list of VCPUs. */
list_add(&vcpu->list, &vm->vcpus); list_add(&vcpu->list, &vm->vcpus);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment