[RFC PATCH 009/105] KVM: selftests: Add __vcpu_run() helper

Sean Christopherson
2022-03-11T05:52:26Z

tools/testing/selftests/kvm/aarch64/vgic_init.c | 6 ++----
tools/testing/selftests/kvm/dirty_log_test.c | 6 ++----
tools/testing/selftests/kvm/include/kvm_util_base.h | 6 ++++++
tools/testing/selftests/kvm/lib/kvm_util.c | 6 ++----
4 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/tools/testing/selftests/kvm/aarch64/vgic_init.c b/tools/testing/selftests/kvm/aarch64/vgic_init.c
index 373c8005c2e7..9a153b2ea3de 100644
--- a/tools/testing/selftests/kvm/aarch64/vgic_init.c
+++ b/tools/testing/selftests/kvm/aarch64/vgic_init.c
@@ -55,10 +55,8 @@ static void guest_code(void)
static int run_vcpu(struct kvm_vm *vm, uint32_t vcpuid)
{
ucall_init(vm, NULL);
- int ret = __vcpu_ioctl(vm, vcpuid, KVM_RUN, NULL);
- if (ret)
- return -errno;
- return 0;
+
+ return __vcpu_run(vm, vcpuid) ? -errno : 0;
}

static struct vm_gic vm_gic_create_with_vcpus(uint32_t gic_dev_type, uint32_t nr_vcpus)
diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
index 01c01d40201f..5752486764c9 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -509,7 +509,7 @@ static void generate_random_array(uint64_t *guest_array, uint64_t size)

static void *vcpu_worker(void *data)
{
- int ret, vcpu_fd;
+ int ret;
struct kvm_vm *vm = data;
uint64_t *guest_array;
uint64_t pages_count = 0;
@@ -517,8 +517,6 @@ static void *vcpu_worker(void *data)
+ sizeof(sigset_t));
sigset_t *sigset = (sigset_t *) &sigmask->sigset;

- vcpu_fd = vcpu_get_fd(vm, VCPU_ID);
-
/*
* SIG_IPI is unblocked atomically while in KVM_RUN. It causes the
* ioctl to return with -EINTR, but it is still pending and we need
@@ -539,7 +537,7 @@ static void *vcpu_worker(void *data)
generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
pages_count += TEST_PAGES_PER_LOOP;
/* Let the guest dirty the random pages */
- ret = ioctl(vcpu_fd, KVM_RUN, NULL);
+ ret = __vcpu_run(vm, VCPU_ID);
if (ret == -1 && errno == EINTR) {
int sig = -1;
sigwait(sigset, &sig);
diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index cebfc667e3db..f4bf77eed00a 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -202,6 +202,12 @@ vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva);
struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid);
void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid);
int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid);
+
+static inline int __vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
+{
+ return __vcpu_ioctl(vm, vcpuid, KVM_RUN, NULL);
+}
+
int vcpu_get_fd(struct kvm_vm *vm, uint32_t vcpuid);
void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid);
struct kvm_reg_list *vcpu_get_reg_list(struct kvm_vm *vm, uint32_t vcpuid);
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index c9bbe69513eb..066314ea880c 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1559,12 +1559,10 @@ void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)

int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
{
- struct vcpu *vcpu = vcpu_find(vm, vcpuid);
int rc;

- TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
do {
- rc = ioctl(vcpu->fd, KVM_RUN, NULL);
+ rc = __vcpu_run(vm, vcpuid);
} while (rc == -1 && errno == EINTR);

assert_on_unhandled_exception(vm, vcpuid);
@@ -1589,7 +1587,7 @@ void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid)
TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);

vcpu->state->immediate_exit = 1;
- ret = ioctl(vcpu->fd, KVM_RUN, NULL);
+ ret = __vcpu_run(vm, vcpuid);
vcpu->state->immediate_exit = 0;

TEST_ASSERT(ret == -1 && errno == EINTR,
--
2.35.1.723.g4982287a31-goog