1 // SPDX-License-Identifier: GPL-2.0-only
3 * This test is intended to reproduce a crash that happens when
4 * kvm_arch_hardware_disable is called and it attempts to unregister the user
12 #include <semaphore.h>
18 #include <test_util.h>
23 #define SLEEPING_THREAD_NUM (1 << 4)
24 #define FORK_NUM (1ULL << 9)
25 #define DELAY_US_MAX 2000
26 #define GUEST_CODE_PIO_PORT 4
30 /* Arguments for the pthreads */
36 static void guest_code(void)
39 ; /* Some busy work */
40 printf("Should not be reached.\n");
43 static void *run_vcpu(void *arg)
45 struct payload *payload = (struct payload *)arg;
46 struct kvm_run *state = vcpu_state(payload->vm, payload->index);
48 vcpu_run(payload->vm, payload->index);
50 TEST_ASSERT(false, "%s: exited with reason %d: %s\n",
51 __func__, state->exit_reason,
52 exit_reason_str(state->exit_reason));
56 static void *sleeping_thread(void *arg)
61 fd = open("/dev/null", O_RDWR);
64 TEST_ASSERT(false, "%s: exited\n", __func__);
68 static inline void check_create_thread(pthread_t *thread, pthread_attr_t *attr,
69 void *(*f)(void *), void *arg)
73 r = pthread_create(thread, attr, f, arg);
74 TEST_ASSERT(r == 0, "%s: failed to create thread", __func__);
77 static inline void check_set_affinity(pthread_t thread, cpu_set_t *cpu_set)
81 r = pthread_setaffinity_np(thread, sizeof(cpu_set_t), cpu_set);
82 TEST_ASSERT(r == 0, "%s: failed set affinity", __func__);
85 static inline void check_join(pthread_t thread, void **retval)
89 r = pthread_join(thread, retval);
90 TEST_ASSERT(r == 0, "%s: failed to join thread", __func__);
93 static void run_test(uint32_t run)
97 pthread_t threads[VCPU_NUM];
99 struct payload payloads[VCPU_NUM];
104 for (i = 0; i < VCPU_NUM; i++)
105 CPU_SET(i, &cpu_set);
107 vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES, O_RDWR);
108 kvm_vm_elf_load(vm, program_invocation_name, 0, 0);
109 vm_create_irqchip(vm);
111 pr_debug("%s: [%d] start vcpus\n", __func__, run);
112 for (i = 0; i < VCPU_NUM; ++i) {
113 vm_vcpu_add_default(vm, i, guest_code);
115 payloads[i].index = i;
117 check_create_thread(&threads[i], NULL, run_vcpu,
118 (void *)&payloads[i]);
119 check_set_affinity(threads[i], &cpu_set);
121 for (j = 0; j < SLEEPING_THREAD_NUM; ++j) {
122 check_create_thread(&throw_away, NULL, sleeping_thread,
124 check_set_affinity(throw_away, &cpu_set);
127 pr_debug("%s: [%d] all threads launched\n", __func__, run);
129 for (i = 0; i < VCPU_NUM; ++i)
130 check_join(threads[i], &b);
131 /* Should not be reached */
132 TEST_ASSERT(false, "%s: [%d] child escaped the ninja\n", __func__, run);
135 int main(int argc, char **argv)
141 sem = sem_open("vm_sem", O_CREAT | O_EXCL, 0644, 0);
142 sem_unlink("vm_sem");
144 for (i = 0; i < FORK_NUM; ++i) {
146 TEST_ASSERT(pid >= 0, "%s: unable to fork", __func__);
148 run_test(i); /* This function always exits */
150 pr_debug("%s: [%d] waiting semaphore\n", __func__, i);
152 r = (rand() % DELAY_US_MAX) + 1;
153 pr_debug("%s: [%d] waiting %dus\n", __func__, i, r);
155 r = waitpid(pid, &s, WNOHANG);
156 TEST_ASSERT(r != pid,
157 "%s: [%d] child exited unexpectedly status: [%d]",
159 pr_debug("%s: [%d] killing child\n", __func__, i);