Loading...
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
8#define _GNU_SOURCE
9
10#include <fcntl.h>
11#include <pthread.h>
12#include <semaphore.h>
13#include <stdint.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <sys/wait.h>
17
18#include <test_util.h>
19
20#include "kvm_util.h"
21
22#define VCPU_NUM 4
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
27
28sem_t *sem;
29
30static void guest_code(void)
31{
32 for (;;)
33 ; /* Some busy work */
34 printf("Should not be reached.\n");
35}
36
37static void *run_vcpu(void *arg)
38{
39 struct kvm_vcpu *vcpu = arg;
40 struct kvm_run *run = vcpu->run;
41
42 vcpu_run(vcpu);
43
44 TEST_ASSERT(false, "%s: exited with reason %d: %s\n",
45 __func__, run->exit_reason,
46 exit_reason_str(run->exit_reason));
47 pthread_exit(NULL);
48}
49
50static void *sleeping_thread(void *arg)
51{
52 int fd;
53
54 while (true) {
55 fd = open("/dev/null", O_RDWR);
56 close(fd);
57 }
58 TEST_ASSERT(false, "%s: exited\n", __func__);
59 pthread_exit(NULL);
60}
61
62static inline void check_create_thread(pthread_t *thread, pthread_attr_t *attr,
63 void *(*f)(void *), void *arg)
64{
65 int r;
66
67 r = pthread_create(thread, attr, f, arg);
68 TEST_ASSERT(r == 0, "%s: failed to create thread", __func__);
69}
70
71static inline void check_set_affinity(pthread_t thread, cpu_set_t *cpu_set)
72{
73 int r;
74
75 r = pthread_setaffinity_np(thread, sizeof(cpu_set_t), cpu_set);
76 TEST_ASSERT(r == 0, "%s: failed set affinity", __func__);
77}
78
79static inline void check_join(pthread_t thread, void **retval)
80{
81 int r;
82
83 r = pthread_join(thread, retval);
84 TEST_ASSERT(r == 0, "%s: failed to join thread", __func__);
85}
86
87static void run_test(uint32_t run)
88{
89 struct kvm_vcpu *vcpu;
90 struct kvm_vm *vm;
91 cpu_set_t cpu_set;
92 pthread_t threads[VCPU_NUM];
93 pthread_t throw_away;
94 void *b;
95 uint32_t i, j;
96
97 CPU_ZERO(&cpu_set);
98 for (i = 0; i < VCPU_NUM; i++)
99 CPU_SET(i, &cpu_set);
100
101 vm = vm_create(VCPU_NUM);
102
103 pr_debug("%s: [%d] start vcpus\n", __func__, run);
104 for (i = 0; i < VCPU_NUM; ++i) {
105 vcpu = vm_vcpu_add(vm, i, guest_code);
106
107 check_create_thread(&threads[i], NULL, run_vcpu, vcpu);
108 check_set_affinity(threads[i], &cpu_set);
109
110 for (j = 0; j < SLEEPING_THREAD_NUM; ++j) {
111 check_create_thread(&throw_away, NULL, sleeping_thread,
112 (void *)NULL);
113 check_set_affinity(throw_away, &cpu_set);
114 }
115 }
116 pr_debug("%s: [%d] all threads launched\n", __func__, run);
117 sem_post(sem);
118 for (i = 0; i < VCPU_NUM; ++i)
119 check_join(threads[i], &b);
120 /* Should not be reached */
121 TEST_ASSERT(false, "%s: [%d] child escaped the ninja\n", __func__, run);
122}
123
124void wait_for_child_setup(pid_t pid)
125{
126 /*
127 * Wait for the child to post to the semaphore, but wake up periodically
128 * to check if the child exited prematurely.
129 */
130 for (;;) {
131 const struct timespec wait_period = { .tv_sec = 1 };
132 int status;
133
134 if (!sem_timedwait(sem, &wait_period))
135 return;
136
137 /* Child is still running, keep waiting. */
138 if (pid != waitpid(pid, &status, WNOHANG))
139 continue;
140
141 /*
142 * Child is no longer running, which is not expected.
143 *
144 * If it exited with a non-zero status, we explicitly forward
145 * the child's status in case it exited with KSFT_SKIP.
146 */
147 if (WIFEXITED(status))
148 exit(WEXITSTATUS(status));
149 else
150 TEST_ASSERT(false, "Child exited unexpectedly");
151 }
152}
153
154int main(int argc, char **argv)
155{
156 uint32_t i;
157 int s, r;
158 pid_t pid;
159
160 sem = sem_open("vm_sem", O_CREAT | O_EXCL, 0644, 0);
161 sem_unlink("vm_sem");
162
163 for (i = 0; i < FORK_NUM; ++i) {
164 pid = fork();
165 TEST_ASSERT(pid >= 0, "%s: unable to fork", __func__);
166 if (pid == 0)
167 run_test(i); /* This function always exits */
168
169 pr_debug("%s: [%d] waiting semaphore\n", __func__, i);
170 wait_for_child_setup(pid);
171 r = (rand() % DELAY_US_MAX) + 1;
172 pr_debug("%s: [%d] waiting %dus\n", __func__, i, r);
173 usleep(r);
174 r = waitpid(pid, &s, WNOHANG);
175 TEST_ASSERT(r != pid,
176 "%s: [%d] child exited unexpectedly status: [%d]",
177 __func__, i, s);
178 pr_debug("%s: [%d] killing child\n", __func__, i);
179 kill(pid, SIGKILL);
180 }
181
182 sem_destroy(sem);
183 exit(0);
184}
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
8#define _GNU_SOURCE
9
10#include <fcntl.h>
11#include <pthread.h>
12#include <semaphore.h>
13#include <stdint.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <sys/wait.h>
17
18#include <test_util.h>
19
20#include "kvm_util.h"
21
22#define VCPU_NUM 4
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
27
28sem_t *sem;
29
30/* Arguments for the pthreads */
31struct payload {
32 struct kvm_vm *vm;
33 uint32_t index;
34};
35
36static void guest_code(void)
37{
38 for (;;)
39 ; /* Some busy work */
40 printf("Should not be reached.\n");
41}
42
43static void *run_vcpu(void *arg)
44{
45 struct payload *payload = (struct payload *)arg;
46 struct kvm_run *state = vcpu_state(payload->vm, payload->index);
47
48 vcpu_run(payload->vm, payload->index);
49
50 TEST_ASSERT(false, "%s: exited with reason %d: %s\n",
51 __func__, state->exit_reason,
52 exit_reason_str(state->exit_reason));
53 pthread_exit(NULL);
54}
55
56static void *sleeping_thread(void *arg)
57{
58 int fd;
59
60 while (true) {
61 fd = open("/dev/null", O_RDWR);
62 close(fd);
63 }
64 TEST_ASSERT(false, "%s: exited\n", __func__);
65 pthread_exit(NULL);
66}
67
68static inline void check_create_thread(pthread_t *thread, pthread_attr_t *attr,
69 void *(*f)(void *), void *arg)
70{
71 int r;
72
73 r = pthread_create(thread, attr, f, arg);
74 TEST_ASSERT(r == 0, "%s: failed to create thread", __func__);
75}
76
77static inline void check_set_affinity(pthread_t thread, cpu_set_t *cpu_set)
78{
79 int r;
80
81 r = pthread_setaffinity_np(thread, sizeof(cpu_set_t), cpu_set);
82 TEST_ASSERT(r == 0, "%s: failed set affinity", __func__);
83}
84
85static inline void check_join(pthread_t thread, void **retval)
86{
87 int r;
88
89 r = pthread_join(thread, retval);
90 TEST_ASSERT(r == 0, "%s: failed to join thread", __func__);
91}
92
93static void run_test(uint32_t run)
94{
95 struct kvm_vm *vm;
96 cpu_set_t cpu_set;
97 pthread_t threads[VCPU_NUM];
98 pthread_t throw_away;
99 struct payload payloads[VCPU_NUM];
100 void *b;
101 uint32_t i, j;
102
103 CPU_ZERO(&cpu_set);
104 for (i = 0; i < VCPU_NUM; i++)
105 CPU_SET(i, &cpu_set);
106
107 vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES, O_RDWR);
108 kvm_vm_elf_load(vm, program_invocation_name);
109 vm_create_irqchip(vm);
110
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);
114 payloads[i].vm = vm;
115 payloads[i].index = i;
116
117 check_create_thread(&threads[i], NULL, run_vcpu,
118 (void *)&payloads[i]);
119 check_set_affinity(threads[i], &cpu_set);
120
121 for (j = 0; j < SLEEPING_THREAD_NUM; ++j) {
122 check_create_thread(&throw_away, NULL, sleeping_thread,
123 (void *)NULL);
124 check_set_affinity(throw_away, &cpu_set);
125 }
126 }
127 pr_debug("%s: [%d] all threads launched\n", __func__, run);
128 sem_post(sem);
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);
133}
134
135void wait_for_child_setup(pid_t pid)
136{
137 /*
138 * Wait for the child to post to the semaphore, but wake up periodically
139 * to check if the child exited prematurely.
140 */
141 for (;;) {
142 const struct timespec wait_period = { .tv_sec = 1 };
143 int status;
144
145 if (!sem_timedwait(sem, &wait_period))
146 return;
147
148 /* Child is still running, keep waiting. */
149 if (pid != waitpid(pid, &status, WNOHANG))
150 continue;
151
152 /*
153 * Child is no longer running, which is not expected.
154 *
155 * If it exited with a non-zero status, we explicitly forward
156 * the child's status in case it exited with KSFT_SKIP.
157 */
158 if (WIFEXITED(status))
159 exit(WEXITSTATUS(status));
160 else
161 TEST_ASSERT(false, "Child exited unexpectedly");
162 }
163}
164
165int main(int argc, char **argv)
166{
167 uint32_t i;
168 int s, r;
169 pid_t pid;
170
171 sem = sem_open("vm_sem", O_CREAT | O_EXCL, 0644, 0);
172 sem_unlink("vm_sem");
173
174 for (i = 0; i < FORK_NUM; ++i) {
175 pid = fork();
176 TEST_ASSERT(pid >= 0, "%s: unable to fork", __func__);
177 if (pid == 0)
178 run_test(i); /* This function always exits */
179
180 pr_debug("%s: [%d] waiting semaphore\n", __func__, i);
181 wait_for_child_setup(pid);
182 r = (rand() % DELAY_US_MAX) + 1;
183 pr_debug("%s: [%d] waiting %dus\n", __func__, i, r);
184 usleep(r);
185 r = waitpid(pid, &s, WNOHANG);
186 TEST_ASSERT(r != pid,
187 "%s: [%d] child exited unexpectedly status: [%d]",
188 __func__, i, s);
189 pr_debug("%s: [%d] killing child\n", __func__, i);
190 kill(pid, SIGKILL);
191 }
192
193 sem_destroy(sem);
194 exit(0);
195}