Path: blob/master/tools/testing/selftests/kvm/hardware_disable_test.c
38189 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* This test is intended to reproduce a crash that happens when3* kvm_arch_hardware_disable is called and it attempts to unregister the user4* return notifiers.5*/6#include <fcntl.h>7#include <pthread.h>8#include <semaphore.h>9#include <stdint.h>10#include <stdlib.h>11#include <unistd.h>12#include <sys/wait.h>1314#include <test_util.h>1516#include "kvm_util.h"1718#define VCPU_NUM 419#define SLEEPING_THREAD_NUM (1 << 4)20#define FORK_NUM (1ULL << 9)21#define DELAY_US_MAX 20002223sem_t *sem;2425static void guest_code(void)26{27for (;;)28; /* Some busy work */29printf("Should not be reached.\n");30}3132static void *run_vcpu(void *arg)33{34struct kvm_vcpu *vcpu = arg;35struct kvm_run *run = vcpu->run;3637vcpu_run(vcpu);3839TEST_ASSERT(false, "%s: exited with reason %d: %s",40__func__, run->exit_reason,41exit_reason_str(run->exit_reason));42pthread_exit(NULL);43}4445static void *sleeping_thread(void *arg)46{47int fd;4849while (true) {50fd = open("/dev/null", O_RDWR);51close(fd);52}53TEST_ASSERT(false, "%s: exited", __func__);54pthread_exit(NULL);55}5657static inline void check_create_thread(pthread_t *thread, pthread_attr_t *attr,58void *(*f)(void *), void *arg)59{60int r;6162r = pthread_create(thread, attr, f, arg);63TEST_ASSERT(r == 0, "%s: failed to create thread", __func__);64}6566static inline void check_set_affinity(pthread_t thread, cpu_set_t *cpu_set)67{68int r;6970r = pthread_setaffinity_np(thread, sizeof(cpu_set_t), cpu_set);71TEST_ASSERT(r == 0, "%s: failed set affinity", __func__);72}7374static inline void check_join(pthread_t thread, void **retval)75{76int r;7778r = pthread_join(thread, retval);79TEST_ASSERT(r == 0, "%s: failed to join thread", __func__);80}8182static void run_test(uint32_t run)83{84struct kvm_vcpu *vcpu;85struct kvm_vm *vm;86cpu_set_t cpu_set;87pthread_t threads[VCPU_NUM];88pthread_t throw_away;89void *b;90uint32_t i, j;9192CPU_ZERO(&cpu_set);93for (i = 0; i < VCPU_NUM; i++)94CPU_SET(i, &cpu_set);9596vm = vm_create(VCPU_NUM);9798pr_debug("%s: [%d] start vcpus\n", __func__, run);99for (i = 0; i < VCPU_NUM; ++i) {100vcpu = vm_vcpu_add(vm, i, guest_code);101102check_create_thread(&threads[i], NULL, run_vcpu, vcpu);103check_set_affinity(threads[i], &cpu_set);104105for (j = 0; j < SLEEPING_THREAD_NUM; ++j) {106check_create_thread(&throw_away, NULL, sleeping_thread,107(void *)NULL);108check_set_affinity(throw_away, &cpu_set);109}110}111pr_debug("%s: [%d] all threads launched\n", __func__, run);112sem_post(sem);113for (i = 0; i < VCPU_NUM; ++i)114check_join(threads[i], &b);115/* Should not be reached */116TEST_ASSERT(false, "%s: [%d] child escaped the ninja", __func__, run);117}118119void wait_for_child_setup(pid_t pid)120{121/*122* Wait for the child to post to the semaphore, but wake up periodically123* to check if the child exited prematurely.124*/125for (;;) {126const struct timespec wait_period = { .tv_sec = 1 };127int status;128129if (!sem_timedwait(sem, &wait_period))130return;131132/* Child is still running, keep waiting. */133if (pid != waitpid(pid, &status, WNOHANG))134continue;135136/*137* Child is no longer running, which is not expected.138*139* If it exited with a non-zero status, we explicitly forward140* the child's status in case it exited with KSFT_SKIP.141*/142if (WIFEXITED(status))143exit(WEXITSTATUS(status));144else145TEST_ASSERT(false, "Child exited unexpectedly");146}147}148149int main(int argc, char **argv)150{151uint32_t i;152int s, r;153pid_t pid;154155sem = sem_open("vm_sem", O_CREAT | O_EXCL, 0644, 0);156sem_unlink("vm_sem");157158for (i = 0; i < FORK_NUM; ++i) {159pid = fork();160TEST_ASSERT(pid >= 0, "%s: unable to fork", __func__);161if (pid == 0)162run_test(i); /* This function always exits */163164pr_debug("%s: [%d] waiting semaphore\n", __func__, i);165wait_for_child_setup(pid);166r = (rand() % DELAY_US_MAX) + 1;167pr_debug("%s: [%d] waiting %dus\n", __func__, i, r);168usleep(r);169r = waitpid(pid, &s, WNOHANG);170TEST_ASSERT(r != pid,171"%s: [%d] child exited unexpectedly status: [%d]",172__func__, i, s);173pr_debug("%s: [%d] killing child\n", __func__, i);174kill(pid, SIGKILL);175}176177sem_destroy(sem);178exit(0);179}180181182