1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2022 ARM Limited.
7 #define _POSIX_C_SOURCE 199309L
20 #include <sys/epoll.h>
21 #include <sys/prctl.h>
22 #include <sys/types.h>
25 #include <asm/hwcap.h>
27 #include "../../kselftest.h"
41 static struct child_data *children;
42 static int num_children;
43 static bool terminate;
45 static void drain_output(bool flush);
47 static int num_processors(void)
49 long nproc = sysconf(_SC_NPROCESSORS_CONF);
51 perror("Unable to read number of processors\n");
58 static void child_start(struct child_data *child, const char *program)
60 int ret, pipefd[2], i;
61 struct epoll_event ev;
65 ksft_exit_fail_msg("Failed to create stdout pipe: %s (%d)\n",
66 strerror(errno), errno);
70 ksft_exit_fail_msg("fork() failed: %s (%d)\n",
71 strerror(errno), errno);
75 * In child, replace stdout with the pipe, errors to
76 * stderr from here as kselftest prints to stdout.
78 ret = dup2(pipefd[1], 1);
80 fprintf(stderr, "dup2() %d\n", errno);
85 * Very dumb mechanism to clean open FDs other than
86 * stdio. We don't want O_CLOEXEC for the pipes...
88 for (i = 3; i < 8192; i++)
91 ret = execl(program, program, NULL);
92 fprintf(stderr, "execl(%s) failed: %d (%s)\n",
93 program, errno, strerror(errno));
98 * In parent, remember the child and close our copy of the
99 * write side of stdout.
102 child->stdout = pipefd[0];
103 child->output = NULL;
104 child->exited = false;
105 child->output_seen = false;
107 ev.events = EPOLLIN | EPOLLHUP;
110 ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, child->stdout, &ev);
112 ksft_exit_fail_msg("%s EPOLL_CTL_ADD failed: %s (%d)\n",
113 child->name, strerror(errno), errno);
117 * Keep output flowing during child startup so logs
118 * are more timely, can help debugging.
124 static void child_output(struct child_data *child, uint32_t events,
127 char read_data[1024];
129 int ret, len, cur_work, cur_read;
131 if (events & EPOLLIN) {
132 ret = read(child->stdout, read_data, sizeof(read_data));
134 ksft_print_msg("%s: read() failed: %s (%d)\n",
135 child->name, strerror(errno), errno);
140 child->output_seen = true;
142 /* Pick up any partial read */
144 strncpy(work, child->output, sizeof(work) - 1);
145 cur_work = strnlen(work, sizeof(work));
147 child->output = NULL;
153 while (cur_read < len) {
154 work[cur_work] = read_data[cur_read++];
156 if (work[cur_work] == '\n') {
157 work[cur_work] = '\0';
158 ksft_print_msg("%s: %s\n", child->name, work);
166 work[cur_work] = '\0';
167 ret = asprintf(&child->output, "%s", work);
169 ksft_exit_fail_msg("Out of memory\n");
173 if (events & EPOLLHUP) {
174 close(child->stdout);
179 if (flush && child->output) {
180 ksft_print_msg("%s: %s<EOF>\n", child->name, child->output);
182 child->output = NULL;
186 static void child_tickle(struct child_data *child)
188 if (child->output_seen && !child->exited)
189 kill(child->pid, SIGUSR2);
192 static void child_stop(struct child_data *child)
195 kill(child->pid, SIGTERM);
198 static void child_cleanup(struct child_data *child)
204 if (!child->exited) {
206 ret = waitpid(child->pid, &status, 0);
207 if (ret == -1 && errno == EINTR)
211 ksft_print_msg("waitpid(%d) failed: %s (%d)\n",
212 child->pid, strerror(errno),
217 } while (!WIFEXITED(status));
218 child->exit_status = WEXITSTATUS(status);
221 if (!child->output_seen) {
222 ksft_print_msg("%s no output seen\n", child->name);
226 if (child->exit_status != 0) {
227 ksft_print_msg("%s exited with error code %d\n",
228 child->name, child->exit_status);
232 ksft_test_result(!fail, "%s\n", child->name);
235 static void handle_child_signal(int sig, siginfo_t *info, void *context)
240 for (i = 0; i < num_children; i++) {
241 if (children[i].pid == info->si_pid) {
242 children[i].exited = true;
243 children[i].exit_status = info->si_status;
250 ksft_print_msg("SIGCHLD for unknown PID %d with status %d\n",
251 info->si_pid, info->si_status);
254 static void handle_exit_signal(int sig, siginfo_t *info, void *context)
258 ksft_print_msg("Got signal, exiting...\n");
263 * This should be redundant, the main loop should clean up
264 * after us, but for safety stop everything we can here.
266 for (i = 0; i < num_children; i++)
267 child_stop(&children[i]);
270 static void start_fpsimd(struct child_data *child, int cpu, int copy)
274 child_start(child, "./fpsimd-test");
276 ret = asprintf(&child->name, "FPSIMD-%d-%d", cpu, copy);
278 ksft_exit_fail_msg("asprintf() failed\n");
280 ksft_print_msg("Started %s\n", child->name);
283 static void start_sve(struct child_data *child, int vl, int cpu)
287 ret = prctl(PR_SVE_SET_VL, vl | PR_SVE_VL_INHERIT);
289 ksft_exit_fail_msg("Failed to set SVE VL %d\n", vl);
291 child_start(child, "./sve-test");
293 ret = asprintf(&child->name, "SVE-VL-%d-%d", vl, cpu);
295 ksft_exit_fail_msg("asprintf() failed\n");
297 ksft_print_msg("Started %s\n", child->name);
300 static void start_ssve(struct child_data *child, int vl, int cpu)
304 ret = prctl(PR_SME_SET_VL, vl | PR_SME_VL_INHERIT);
306 ksft_exit_fail_msg("Failed to set SME VL %d\n", ret);
308 child_start(child, "./ssve-test");
310 ret = asprintf(&child->name, "SSVE-VL-%d-%d", vl, cpu);
312 ksft_exit_fail_msg("asprintf() failed\n");
314 ksft_print_msg("Started %s\n", child->name);
317 static void start_za(struct child_data *child, int vl, int cpu)
321 ret = prctl(PR_SME_SET_VL, vl | PR_SVE_VL_INHERIT);
323 ksft_exit_fail_msg("Failed to set SME VL %d\n", ret);
325 child_start(child, "./za-test");
327 ret = asprintf(&child->name, "ZA-VL-%d-%d", vl, cpu);
329 ksft_exit_fail_msg("asprintf() failed\n");
331 ksft_print_msg("Started %s\n", child->name);
334 static void probe_vls(int vls[], int *vl_count, int set_vl)
341 for (vq = SVE_VQ_MAX; vq > 0; --vq) {
342 vl = prctl(set_vl, vq * 16);
344 ksft_exit_fail_msg("SET_VL failed: %s (%d)\n",
345 strerror(errno), errno);
347 vl &= PR_SVE_VL_LEN_MASK;
349 vq = sve_vq_from_vl(vl);
356 /* Handle any pending output without blocking */
357 static void drain_output(bool flush)
359 struct epoll_event ev;
363 ret = epoll_wait(epoll_fd, &ev, 1, 0);
367 ksft_print_msg("epoll_wait() failed: %s (%d)\n",
368 strerror(errno), errno);
372 child_output(ev.data.ptr, ev.events, flush);
376 static const struct option options[] = {
377 { "timeout", required_argument, NULL, 't' },
381 int main(int argc, char **argv)
385 int cpus, tests, i, j, c;
386 int sve_vl_count, sme_vl_count, fpsimd_per_cpu;
387 int sve_vls[MAX_VLS], sme_vls[MAX_VLS];
388 struct epoll_event ev;
391 while ((c = getopt_long(argc, argv, "t:", options, NULL)) != -1) {
394 ret = sscanf(optarg, "%d", &timeout);
396 ksft_exit_fail_msg("Failed to parse timeout %s\n",
400 ksft_exit_fail_msg("Unknown argument\n");
404 cpus = num_processors();
407 if (getauxval(AT_HWCAP) & HWCAP_SVE) {
408 probe_vls(sve_vls, &sve_vl_count, PR_SVE_SET_VL);
409 tests += sve_vl_count * cpus;
414 if (getauxval(AT_HWCAP2) & HWCAP2_SME) {
415 probe_vls(sme_vls, &sme_vl_count, PR_SME_SET_VL);
416 tests += sme_vl_count * cpus * 2;
421 /* Force context switching if we only have FPSIMD */
422 if (!sve_vl_count && !sme_vl_count)
426 tests += cpus * fpsimd_per_cpu;
429 ksft_set_plan(tests);
431 ksft_print_msg("%d CPUs, %d SVE VLs, %d SME VLs\n",
432 cpus, sve_vl_count, sme_vl_count);
435 ksft_print_msg("Will run for %ds\n", timeout);
437 ksft_print_msg("Will run until terminated\n");
439 children = calloc(sizeof(*children), tests);
441 ksft_exit_fail_msg("Unable to allocate child data\n");
443 ret = epoll_create1(EPOLL_CLOEXEC);
445 ksft_exit_fail_msg("epoll_create1() failed: %s (%d)\n",
446 strerror(errno), ret);
449 /* Get signal handers ready before we start any children */
450 memset(&sa, 0, sizeof(sa));
451 sa.sa_sigaction = handle_exit_signal;
452 sa.sa_flags = SA_RESTART | SA_SIGINFO;
453 sigemptyset(&sa.sa_mask);
454 ret = sigaction(SIGINT, &sa, NULL);
456 ksft_print_msg("Failed to install SIGINT handler: %s (%d)\n",
457 strerror(errno), errno);
458 ret = sigaction(SIGTERM, &sa, NULL);
460 ksft_print_msg("Failed to install SIGTERM handler: %s (%d)\n",
461 strerror(errno), errno);
462 sa.sa_sigaction = handle_child_signal;
463 ret = sigaction(SIGCHLD, &sa, NULL);
465 ksft_print_msg("Failed to install SIGCHLD handler: %s (%d)\n",
466 strerror(errno), errno);
468 for (i = 0; i < cpus; i++) {
469 for (j = 0; j < fpsimd_per_cpu; j++)
470 start_fpsimd(&children[num_children++], i, j);
472 for (j = 0; j < sve_vl_count; j++)
473 start_sve(&children[num_children++], sve_vls[j], i);
475 for (j = 0; j < sme_vl_count; j++) {
476 start_ssve(&children[num_children++], sme_vls[j], i);
477 start_za(&children[num_children++], sme_vls[j], i);
482 /* Did we get a signal asking us to exit? */
487 * Timeout is counted in seconds with no output, the
488 * tests print during startup then are silent when
489 * running so this should ensure they all ran enough
490 * to install the signal handler, this is especially
491 * useful in emulation where we will both be slow and
492 * likely to have a large set of VLs.
494 ret = epoll_wait(epoll_fd, &ev, 1, 1000);
498 ksft_exit_fail_msg("epoll_wait() failed: %s (%d)\n",
499 strerror(errno), errno);
504 child_output(ev.data.ptr, ev.events, false);
508 /* Otherwise epoll_wait() timed out */
510 for (i = 0; i < num_children; i++)
511 child_tickle(&children[i]);
513 /* Negative timeout means run indefinitely */
520 ksft_print_msg("Finishing up...\n");
522 for (i = 0; i < tests; i++)
523 child_stop(&children[i]);
527 for (i = 0; i < tests; i++)
528 child_cleanup(&children[i]);