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 struct epoll_event *evs;
44 static int num_children;
45 static bool terminate;
47 static int startup_pipe[2];
49 static int num_processors(void)
51 long nproc = sysconf(_SC_NPROCESSORS_CONF);
53 perror("Unable to read number of processors\n");
60 static void child_start(struct child_data *child, const char *program)
62 int ret, pipefd[2], i;
63 struct epoll_event ev;
67 ksft_exit_fail_msg("Failed to create stdout pipe: %s (%d)\n",
68 strerror(errno), errno);
72 ksft_exit_fail_msg("fork() failed: %s (%d)\n",
73 strerror(errno), errno);
77 * In child, replace stdout with the pipe, errors to
78 * stderr from here as kselftest prints to stdout.
80 ret = dup2(pipefd[1], 1);
82 fprintf(stderr, "dup2() %d\n", errno);
87 * Duplicate the read side of the startup pipe to
88 * FD 3 so we can close everything else.
90 ret = dup2(startup_pipe[0], 3);
92 fprintf(stderr, "dup2() %d\n", errno);
97 * Very dumb mechanism to clean open FDs other than
98 * stdio. We don't want O_CLOEXEC for the pipes...
100 for (i = 4; i < 8192; i++)
104 * Read from the startup pipe, there should be no data
105 * and we should block until it is closed. We just
106 * carry on on error since this isn't super critical.
108 ret = read(3, &i, sizeof(i));
110 fprintf(stderr, "read(startp pipe) failed: %s (%d)\n",
111 strerror(errno), errno);
113 fprintf(stderr, "%d bytes of data on startup pipe\n",
117 ret = execl(program, program, NULL);
118 fprintf(stderr, "execl(%s) failed: %d (%s)\n",
119 program, errno, strerror(errno));
124 * In parent, remember the child and close our copy of the
125 * write side of stdout.
128 child->stdout = pipefd[0];
129 child->output = NULL;
130 child->exited = false;
131 child->output_seen = false;
133 ev.events = EPOLLIN | EPOLLHUP;
136 ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, child->stdout, &ev);
138 ksft_exit_fail_msg("%s EPOLL_CTL_ADD failed: %s (%d)\n",
139 child->name, strerror(errno), errno);
144 static bool child_output_read(struct child_data *child)
146 char read_data[1024];
148 int ret, len, cur_work, cur_read;
150 ret = read(child->stdout, read_data, sizeof(read_data));
155 ksft_print_msg("%s: read() failed: %s (%d)\n",
156 child->name, strerror(errno),
162 child->output_seen = true;
164 /* Pick up any partial read */
166 strncpy(work, child->output, sizeof(work) - 1);
167 cur_work = strnlen(work, sizeof(work));
169 child->output = NULL;
175 while (cur_read < len) {
176 work[cur_work] = read_data[cur_read++];
178 if (work[cur_work] == '\n') {
179 work[cur_work] = '\0';
180 ksft_print_msg("%s: %s\n", child->name, work);
188 work[cur_work] = '\0';
189 ret = asprintf(&child->output, "%s", work);
191 ksft_exit_fail_msg("Out of memory\n");
197 static void child_output(struct child_data *child, uint32_t events,
202 if (events & EPOLLIN) {
204 read_more = child_output_read(child);
208 if (events & EPOLLHUP) {
209 close(child->stdout);
214 if (flush && child->output) {
215 ksft_print_msg("%s: %s<EOF>\n", child->name, child->output);
217 child->output = NULL;
221 static void child_tickle(struct child_data *child)
223 if (child->output_seen && !child->exited)
224 kill(child->pid, SIGUSR2);
227 static void child_stop(struct child_data *child)
230 kill(child->pid, SIGTERM);
233 static void child_cleanup(struct child_data *child)
239 if (!child->exited) {
241 ret = waitpid(child->pid, &status, 0);
242 if (ret == -1 && errno == EINTR)
246 ksft_print_msg("waitpid(%d) failed: %s (%d)\n",
247 child->pid, strerror(errno),
252 } while (!WIFEXITED(status));
253 child->exit_status = WEXITSTATUS(status);
256 if (!child->output_seen) {
257 ksft_print_msg("%s no output seen\n", child->name);
261 if (child->exit_status != 0) {
262 ksft_print_msg("%s exited with error code %d\n",
263 child->name, child->exit_status);
267 ksft_test_result(!fail, "%s\n", child->name);
270 static void handle_child_signal(int sig, siginfo_t *info, void *context)
275 for (i = 0; i < num_children; i++) {
276 if (children[i].pid == info->si_pid) {
277 children[i].exited = true;
278 children[i].exit_status = info->si_status;
285 ksft_print_msg("SIGCHLD for unknown PID %d with status %d\n",
286 info->si_pid, info->si_status);
289 static void handle_exit_signal(int sig, siginfo_t *info, void *context)
293 /* If we're already exiting then don't signal again */
297 ksft_print_msg("Got signal, exiting...\n");
302 * This should be redundant, the main loop should clean up
303 * after us, but for safety stop everything we can here.
305 for (i = 0; i < num_children; i++)
306 child_stop(&children[i]);
309 static void start_fpsimd(struct child_data *child, int cpu, int copy)
313 ret = asprintf(&child->name, "FPSIMD-%d-%d", cpu, copy);
315 ksft_exit_fail_msg("asprintf() failed\n");
317 child_start(child, "./fpsimd-test");
319 ksft_print_msg("Started %s\n", child->name);
322 static void start_sve(struct child_data *child, int vl, int cpu)
326 ret = prctl(PR_SVE_SET_VL, vl | PR_SVE_VL_INHERIT);
328 ksft_exit_fail_msg("Failed to set SVE VL %d\n", vl);
330 ret = asprintf(&child->name, "SVE-VL-%d-%d", vl, cpu);
332 ksft_exit_fail_msg("asprintf() failed\n");
334 child_start(child, "./sve-test");
336 ksft_print_msg("Started %s\n", child->name);
339 static void start_ssve(struct child_data *child, int vl, int cpu)
343 ret = asprintf(&child->name, "SSVE-VL-%d-%d", vl, cpu);
345 ksft_exit_fail_msg("asprintf() failed\n");
347 ret = prctl(PR_SME_SET_VL, vl | PR_SME_VL_INHERIT);
349 ksft_exit_fail_msg("Failed to set SME VL %d\n", ret);
351 child_start(child, "./ssve-test");
353 ksft_print_msg("Started %s\n", child->name);
356 static void start_za(struct child_data *child, int vl, int cpu)
360 ret = prctl(PR_SME_SET_VL, vl | PR_SVE_VL_INHERIT);
362 ksft_exit_fail_msg("Failed to set SME VL %d\n", ret);
364 ret = asprintf(&child->name, "ZA-VL-%d-%d", vl, cpu);
366 ksft_exit_fail_msg("asprintf() failed\n");
368 child_start(child, "./za-test");
370 ksft_print_msg("Started %s\n", child->name);
373 static void probe_vls(int vls[], int *vl_count, int set_vl)
380 for (vq = SVE_VQ_MAX; vq > 0; --vq) {
381 vl = prctl(set_vl, vq * 16);
383 ksft_exit_fail_msg("SET_VL failed: %s (%d)\n",
384 strerror(errno), errno);
386 vl &= PR_SVE_VL_LEN_MASK;
388 vq = sve_vq_from_vl(vl);
395 /* Handle any pending output without blocking */
396 static void drain_output(bool flush)
402 ret = epoll_wait(epoll_fd, evs, tests, 0);
406 ksft_print_msg("epoll_wait() failed: %s (%d)\n",
407 strerror(errno), errno);
410 for (i = 0; i < ret; i++)
411 child_output(evs[i].data.ptr, evs[i].events, flush);
415 static const struct option options[] = {
416 { "timeout", required_argument, NULL, 't' },
420 int main(int argc, char **argv)
425 int sve_vl_count, sme_vl_count, fpsimd_per_cpu;
426 bool all_children_started = false;
428 int sve_vls[MAX_VLS], sme_vls[MAX_VLS];
431 while ((c = getopt_long(argc, argv, "t:", options, NULL)) != -1) {
434 ret = sscanf(optarg, "%d", &timeout);
436 ksft_exit_fail_msg("Failed to parse timeout %s\n",
440 ksft_exit_fail_msg("Unknown argument\n");
444 cpus = num_processors();
447 if (getauxval(AT_HWCAP) & HWCAP_SVE) {
448 probe_vls(sve_vls, &sve_vl_count, PR_SVE_SET_VL);
449 tests += sve_vl_count * cpus;
454 if (getauxval(AT_HWCAP2) & HWCAP2_SME) {
455 probe_vls(sme_vls, &sme_vl_count, PR_SME_SET_VL);
456 tests += sme_vl_count * cpus * 2;
461 /* Force context switching if we only have FPSIMD */
462 if (!sve_vl_count && !sme_vl_count)
466 tests += cpus * fpsimd_per_cpu;
469 ksft_set_plan(tests);
471 ksft_print_msg("%d CPUs, %d SVE VLs, %d SME VLs\n",
472 cpus, sve_vl_count, sme_vl_count);
475 ksft_print_msg("Will run for %ds\n", timeout);
477 ksft_print_msg("Will run until terminated\n");
479 children = calloc(sizeof(*children), tests);
481 ksft_exit_fail_msg("Unable to allocate child data\n");
483 ret = epoll_create1(EPOLL_CLOEXEC);
485 ksft_exit_fail_msg("epoll_create1() failed: %s (%d)\n",
486 strerror(errno), ret);
489 /* Create a pipe which children will block on before execing */
490 ret = pipe(startup_pipe);
492 ksft_exit_fail_msg("Failed to create startup pipe: %s (%d)\n",
493 strerror(errno), errno);
495 /* Get signal handers ready before we start any children */
496 memset(&sa, 0, sizeof(sa));
497 sa.sa_sigaction = handle_exit_signal;
498 sa.sa_flags = SA_RESTART | SA_SIGINFO;
499 sigemptyset(&sa.sa_mask);
500 ret = sigaction(SIGINT, &sa, NULL);
502 ksft_print_msg("Failed to install SIGINT handler: %s (%d)\n",
503 strerror(errno), errno);
504 ret = sigaction(SIGTERM, &sa, NULL);
506 ksft_print_msg("Failed to install SIGTERM handler: %s (%d)\n",
507 strerror(errno), errno);
508 sa.sa_sigaction = handle_child_signal;
509 ret = sigaction(SIGCHLD, &sa, NULL);
511 ksft_print_msg("Failed to install SIGCHLD handler: %s (%d)\n",
512 strerror(errno), errno);
514 evs = calloc(tests, sizeof(*evs));
516 ksft_exit_fail_msg("Failed to allocated %d epoll events\n",
519 for (i = 0; i < cpus; i++) {
520 for (j = 0; j < fpsimd_per_cpu; j++)
521 start_fpsimd(&children[num_children++], i, j);
523 for (j = 0; j < sve_vl_count; j++)
524 start_sve(&children[num_children++], sve_vls[j], i);
526 for (j = 0; j < sme_vl_count; j++) {
527 start_ssve(&children[num_children++], sme_vls[j], i);
528 start_za(&children[num_children++], sme_vls[j], i);
533 * All children started, close the startup pipe and let them
536 close(startup_pipe[0]);
537 close(startup_pipe[1]);
540 /* Did we get a signal asking us to exit? */
545 * Timeout is counted in seconds with no output, the
546 * tests print during startup then are silent when
547 * running so this should ensure they all ran enough
548 * to install the signal handler, this is especially
549 * useful in emulation where we will both be slow and
550 * likely to have a large set of VLs.
552 ret = epoll_wait(epoll_fd, evs, tests, 1000);
556 ksft_exit_fail_msg("epoll_wait() failed: %s (%d)\n",
557 strerror(errno), errno);
562 for (i = 0; i < ret; i++) {
563 child_output(evs[i].data.ptr, evs[i].events,
569 /* Otherwise epoll_wait() timed out */
572 * If the child processes have not produced output they
573 * aren't actually running the tests yet .
575 if (!all_children_started) {
578 for (i = 0; i < num_children; i++)
579 if (children[i].output_seen ||
583 if (seen_children != num_children) {
584 ksft_print_msg("Waiting for %d children\n",
585 num_children - seen_children);
589 all_children_started = true;
592 ksft_print_msg("Sending signals, timeout remaining: %d\n",
595 for (i = 0; i < num_children; i++)
596 child_tickle(&children[i]);
598 /* Negative timeout means run indefinitely */
605 ksft_print_msg("Finishing up...\n");
608 for (i = 0; i < tests; i++)
609 child_stop(&children[i]);
613 for (i = 0; i < tests; i++)
614 child_cleanup(&children[i]);