Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / kvm / hardware_disable_test.c
blob94bd6ed24cf3c58a8e633444ce5f16536e450039
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
5 * return notifiers.
6 */
7 #include <fcntl.h>
8 #include <pthread.h>
9 #include <semaphore.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/wait.h>
15 #include <test_util.h>
17 #include "kvm_util.h"
19 #define VCPU_NUM 4
20 #define SLEEPING_THREAD_NUM (1 << 4)
21 #define FORK_NUM (1ULL << 9)
22 #define DELAY_US_MAX 2000
24 sem_t *sem;
26 static void guest_code(void)
28 for (;;)
29 ; /* Some busy work */
30 printf("Should not be reached.\n");
33 static void *run_vcpu(void *arg)
35 struct kvm_vcpu *vcpu = arg;
36 struct kvm_run *run = vcpu->run;
38 vcpu_run(vcpu);
40 TEST_ASSERT(false, "%s: exited with reason %d: %s",
41 __func__, run->exit_reason,
42 exit_reason_str(run->exit_reason));
43 pthread_exit(NULL);
46 static void *sleeping_thread(void *arg)
48 int fd;
50 while (true) {
51 fd = open("/dev/null", O_RDWR);
52 close(fd);
54 TEST_ASSERT(false, "%s: exited", __func__);
55 pthread_exit(NULL);
58 static inline void check_create_thread(pthread_t *thread, pthread_attr_t *attr,
59 void *(*f)(void *), void *arg)
61 int r;
63 r = pthread_create(thread, attr, f, arg);
64 TEST_ASSERT(r == 0, "%s: failed to create thread", __func__);
67 static inline void check_set_affinity(pthread_t thread, cpu_set_t *cpu_set)
69 int r;
71 r = pthread_setaffinity_np(thread, sizeof(cpu_set_t), cpu_set);
72 TEST_ASSERT(r == 0, "%s: failed set affinity", __func__);
75 static inline void check_join(pthread_t thread, void **retval)
77 int r;
79 r = pthread_join(thread, retval);
80 TEST_ASSERT(r == 0, "%s: failed to join thread", __func__);
83 static void run_test(uint32_t run)
85 struct kvm_vcpu *vcpu;
86 struct kvm_vm *vm;
87 cpu_set_t cpu_set;
88 pthread_t threads[VCPU_NUM];
89 pthread_t throw_away;
90 void *b;
91 uint32_t i, j;
93 CPU_ZERO(&cpu_set);
94 for (i = 0; i < VCPU_NUM; i++)
95 CPU_SET(i, &cpu_set);
97 vm = vm_create(VCPU_NUM);
99 pr_debug("%s: [%d] start vcpus\n", __func__, run);
100 for (i = 0; i < VCPU_NUM; ++i) {
101 vcpu = vm_vcpu_add(vm, i, guest_code);
103 check_create_thread(&threads[i], NULL, run_vcpu, vcpu);
104 check_set_affinity(threads[i], &cpu_set);
106 for (j = 0; j < SLEEPING_THREAD_NUM; ++j) {
107 check_create_thread(&throw_away, NULL, sleeping_thread,
108 (void *)NULL);
109 check_set_affinity(throw_away, &cpu_set);
112 pr_debug("%s: [%d] all threads launched\n", __func__, run);
113 sem_post(sem);
114 for (i = 0; i < VCPU_NUM; ++i)
115 check_join(threads[i], &b);
116 /* Should not be reached */
117 TEST_ASSERT(false, "%s: [%d] child escaped the ninja", __func__, run);
120 void wait_for_child_setup(pid_t pid)
123 * Wait for the child to post to the semaphore, but wake up periodically
124 * to check if the child exited prematurely.
126 for (;;) {
127 const struct timespec wait_period = { .tv_sec = 1 };
128 int status;
130 if (!sem_timedwait(sem, &wait_period))
131 return;
133 /* Child is still running, keep waiting. */
134 if (pid != waitpid(pid, &status, WNOHANG))
135 continue;
138 * Child is no longer running, which is not expected.
140 * If it exited with a non-zero status, we explicitly forward
141 * the child's status in case it exited with KSFT_SKIP.
143 if (WIFEXITED(status))
144 exit(WEXITSTATUS(status));
145 else
146 TEST_ASSERT(false, "Child exited unexpectedly");
150 int main(int argc, char **argv)
152 uint32_t i;
153 int s, r;
154 pid_t pid;
156 sem = sem_open("vm_sem", O_CREAT | O_EXCL, 0644, 0);
157 sem_unlink("vm_sem");
159 for (i = 0; i < FORK_NUM; ++i) {
160 pid = fork();
161 TEST_ASSERT(pid >= 0, "%s: unable to fork", __func__);
162 if (pid == 0)
163 run_test(i); /* This function always exits */
165 pr_debug("%s: [%d] waiting semaphore\n", __func__, i);
166 wait_for_child_setup(pid);
167 r = (rand() % DELAY_US_MAX) + 1;
168 pr_debug("%s: [%d] waiting %dus\n", __func__, i, r);
169 usleep(r);
170 r = waitpid(pid, &s, WNOHANG);
171 TEST_ASSERT(r != pid,
172 "%s: [%d] child exited unexpectedly status: [%d]",
173 __func__, i, s);
174 pr_debug("%s: [%d] killing child\n", __func__, i);
175 kill(pid, SIGKILL);
178 sem_destroy(sem);
179 exit(0);