kselftest/arm64: Fix spelling misakes of signal names
[platform/kernel/linux-starfive.git] / tools / testing / selftests / arm64 / fp / fp-stress.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2022 ARM Limited.
4  */
5
6 #define _GNU_SOURCE
7 #define _POSIX_C_SOURCE 199309L
8
9 #include <errno.h>
10 #include <getopt.h>
11 #include <poll.h>
12 #include <signal.h>
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/auxv.h>
20 #include <sys/epoll.h>
21 #include <sys/prctl.h>
22 #include <sys/types.h>
23 #include <sys/uio.h>
24 #include <sys/wait.h>
25 #include <asm/hwcap.h>
26
27 #include "../../kselftest.h"
28
29 #define MAX_VLS 16
30
31 struct child_data {
32         char *name, *output;
33         pid_t pid;
34         int stdout;
35         bool output_seen;
36         bool exited;
37         int exit_status;
38 };
39
40 static int epoll_fd;
41 static struct child_data *children;
42 static int num_children;
43 static bool terminate;
44
45 static void drain_output(bool flush);
46
47 static int num_processors(void)
48 {
49         long nproc = sysconf(_SC_NPROCESSORS_CONF);
50         if (nproc < 0) {
51                 perror("Unable to read number of processors\n");
52                 exit(EXIT_FAILURE);
53         }
54
55         return nproc;
56 }
57
58 static void child_start(struct child_data *child, const char *program)
59 {
60         int ret, pipefd[2], i;
61         struct epoll_event ev;
62
63         ret = pipe(pipefd);
64         if (ret != 0)
65                 ksft_exit_fail_msg("Failed to create stdout pipe: %s (%d)\n",
66                                    strerror(errno), errno);
67
68         child->pid = fork();
69         if (child->pid == -1)
70                 ksft_exit_fail_msg("fork() failed: %s (%d)\n",
71                                    strerror(errno), errno);
72
73         if (!child->pid) {
74                 /*
75                  * In child, replace stdout with the pipe, errors to
76                  * stderr from here as kselftest prints to stdout.
77                  */
78                 ret = dup2(pipefd[1], 1);
79                 if (ret == -1) {
80                         fprintf(stderr, "dup2() %d\n", errno);
81                         exit(EXIT_FAILURE);
82                 }
83
84                 /*
85                  * Very dumb mechanism to clean open FDs other than
86                  * stdio. We don't want O_CLOEXEC for the pipes...
87                  */
88                 for (i = 3; i < 8192; i++)
89                         close(i);
90
91                 ret = execl(program, program, NULL);
92                 fprintf(stderr, "execl(%s) failed: %d (%s)\n",
93                         program, errno, strerror(errno));
94
95                 exit(EXIT_FAILURE);
96         } else {
97                 /*
98                  * In parent, remember the child and close our copy of the
99                  * write side of stdout.
100                  */
101                 close(pipefd[1]);
102                 child->stdout = pipefd[0];
103                 child->output = NULL;
104                 child->exited = false;
105                 child->output_seen = false;
106
107                 ev.events = EPOLLIN | EPOLLHUP;
108                 ev.data.ptr = child;
109
110                 ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, child->stdout, &ev);
111                 if (ret < 0) {
112                         ksft_exit_fail_msg("%s EPOLL_CTL_ADD failed: %s (%d)\n",
113                                            child->name, strerror(errno), errno);
114                 }
115
116                 /*
117                  * Keep output flowing during child startup so logs
118                  * are more timely, can help debugging.
119                  */
120                 drain_output(false);
121         }
122 }
123
124 static void child_output(struct child_data *child, uint32_t events,
125                          bool flush)
126 {
127         char read_data[1024];
128         char work[1024];
129         int ret, len, cur_work, cur_read;
130
131         if (events & EPOLLIN) {
132                 ret = read(child->stdout, read_data, sizeof(read_data));
133                 if (ret < 0) {
134                         ksft_print_msg("%s: read() failed: %s (%d)\n",
135                                        child->name, strerror(errno), errno);
136                         return;
137                 }
138                 len = ret;
139
140                 child->output_seen = true;
141
142                 /* Pick up any partial read */
143                 if (child->output) {
144                         strncpy(work, child->output, sizeof(work) - 1);
145                         cur_work = strnlen(work, sizeof(work));
146                         free(child->output);
147                         child->output = NULL;
148                 } else {
149                         cur_work = 0;
150                 }
151
152                 cur_read = 0;
153                 while (cur_read < len) {
154                         work[cur_work] = read_data[cur_read++];
155
156                         if (work[cur_work] == '\n') {
157                                 work[cur_work] = '\0';
158                                 ksft_print_msg("%s: %s\n", child->name, work);
159                                 cur_work = 0;
160                         } else {
161                                 cur_work++;
162                         }
163                 }
164
165                 if (cur_work) {
166                         work[cur_work] = '\0';
167                         ret = asprintf(&child->output, "%s", work);
168                         if (ret == -1)
169                                 ksft_exit_fail_msg("Out of memory\n");
170                 }
171         }
172
173         if (events & EPOLLHUP) {
174                 close(child->stdout);
175                 child->stdout = -1;
176                 flush = true;
177         }
178
179         if (flush && child->output) {
180                 ksft_print_msg("%s: %s<EOF>\n", child->name, child->output);
181                 free(child->output);
182                 child->output = NULL;
183         }
184 }
185
186 static void child_tickle(struct child_data *child)
187 {
188         if (child->output_seen && !child->exited)
189                 kill(child->pid, SIGUSR2);
190 }
191
192 static void child_stop(struct child_data *child)
193 {
194         if (!child->exited)
195                 kill(child->pid, SIGTERM);
196 }
197
198 static void child_cleanup(struct child_data *child)
199 {
200         pid_t ret;
201         int status;
202         bool fail = false;
203
204         if (!child->exited) {
205                 do {
206                         ret = waitpid(child->pid, &status, 0);
207                         if (ret == -1 && errno == EINTR)
208                                 continue;
209
210                         if (ret == -1) {
211                                 ksft_print_msg("waitpid(%d) failed: %s (%d)\n",
212                                                child->pid, strerror(errno),
213                                                errno);
214                                 fail = true;
215                                 break;
216                         }
217                 } while (!WIFEXITED(status));
218                 child->exit_status = WEXITSTATUS(status);
219         }
220
221         if (!child->output_seen) {
222                 ksft_print_msg("%s no output seen\n", child->name);
223                 fail = true;
224         }
225
226         if (child->exit_status != 0) {
227                 ksft_print_msg("%s exited with error code %d\n",
228                                child->name, child->exit_status);
229                 fail = true;
230         }
231
232         ksft_test_result(!fail, "%s\n", child->name);
233 }
234
235 static void handle_child_signal(int sig, siginfo_t *info, void *context)
236 {
237         int i;
238         bool found = false;
239
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;
244                         found = true;
245                         break;
246                 }
247         }
248
249         if (!found)
250                 ksft_print_msg("SIGCHLD for unknown PID %d with status %d\n",
251                                info->si_pid, info->si_status);
252 }
253
254 static void handle_exit_signal(int sig, siginfo_t *info, void *context)
255 {
256         int i;
257
258         ksft_print_msg("Got signal, exiting...\n");
259
260         terminate = true;
261
262         /*
263          * This should be redundant, the main loop should clean up
264          * after us, but for safety stop everything we can here.
265          */
266         for (i = 0; i < num_children; i++)
267                 child_stop(&children[i]);
268 }
269
270 static void start_fpsimd(struct child_data *child, int cpu, int copy)
271 {
272         int ret;
273
274         child_start(child, "./fpsimd-test");
275
276         ret = asprintf(&child->name, "FPSIMD-%d-%d", cpu, copy);
277         if (ret == -1)
278                 ksft_exit_fail_msg("asprintf() failed\n");
279
280         ksft_print_msg("Started %s\n", child->name);
281 }
282
283 static void start_sve(struct child_data *child, int vl, int cpu)
284 {
285         int ret;
286
287         ret = prctl(PR_SVE_SET_VL, vl | PR_SVE_VL_INHERIT);
288         if (ret < 0)
289                 ksft_exit_fail_msg("Failed to set SVE VL %d\n", vl);
290
291         child_start(child, "./sve-test");
292
293         ret = asprintf(&child->name, "SVE-VL-%d-%d", vl, cpu);
294         if (ret == -1)
295                 ksft_exit_fail_msg("asprintf() failed\n");
296
297         ksft_print_msg("Started %s\n", child->name);
298 }
299
300 static void start_ssve(struct child_data *child, int vl, int cpu)
301 {
302         int ret;
303
304         ret = prctl(PR_SME_SET_VL, vl | PR_SME_VL_INHERIT);
305         if (ret < 0)
306                 ksft_exit_fail_msg("Failed to set SME VL %d\n", ret);
307
308         child_start(child, "./ssve-test");
309
310         ret = asprintf(&child->name, "SSVE-VL-%d-%d", vl, cpu);
311         if (ret == -1)
312                 ksft_exit_fail_msg("asprintf() failed\n");
313
314         ksft_print_msg("Started %s\n", child->name);
315 }
316
317 static void start_za(struct child_data *child, int vl, int cpu)
318 {
319         int ret;
320
321         ret = prctl(PR_SME_SET_VL, vl | PR_SVE_VL_INHERIT);
322         if (ret < 0)
323                 ksft_exit_fail_msg("Failed to set SME VL %d\n", ret);
324
325         child_start(child, "./za-test");
326
327         ret = asprintf(&child->name, "ZA-VL-%d-%d", vl, cpu);
328         if (ret == -1)
329                 ksft_exit_fail_msg("asprintf() failed\n");
330
331         ksft_print_msg("Started %s\n", child->name);
332 }
333
334 static void probe_vls(int vls[], int *vl_count, int set_vl)
335 {
336         unsigned int vq;
337         int vl;
338
339         *vl_count = 0;
340
341         for (vq = SVE_VQ_MAX; vq > 0; --vq) {
342                 vl = prctl(set_vl, vq * 16);
343                 if (vl == -1)
344                         ksft_exit_fail_msg("SET_VL failed: %s (%d)\n",
345                                            strerror(errno), errno);
346
347                 vl &= PR_SVE_VL_LEN_MASK;
348
349                 vq = sve_vq_from_vl(vl);
350
351                 vls[*vl_count] = vl;
352                 *vl_count += 1;
353         }
354 }
355
356 /* Handle any pending output without blocking */
357 static void drain_output(bool flush)
358 {
359         struct epoll_event ev;
360         int ret = 1;
361
362         while (ret > 0) {
363                 ret = epoll_wait(epoll_fd, &ev, 1, 0);
364                 if (ret < 0) {
365                         if (errno == EINTR)
366                                 continue;
367                         ksft_print_msg("epoll_wait() failed: %s (%d)\n",
368                                        strerror(errno), errno);
369                 }
370
371                 if (ret == 1)
372                         child_output(ev.data.ptr, ev.events, flush);
373         }
374 }
375
376 static const struct option options[] = {
377         { "timeout",    required_argument, NULL, 't' },
378         { }
379 };
380
381 int main(int argc, char **argv)
382 {
383         int ret;
384         int timeout = 10;
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;
389         struct sigaction sa;
390
391         while ((c = getopt_long(argc, argv, "t:", options, NULL)) != -1) {
392                 switch (c) {
393                 case 't':
394                         ret = sscanf(optarg, "%d", &timeout);
395                         if (ret != 1)
396                                 ksft_exit_fail_msg("Failed to parse timeout %s\n",
397                                                    optarg);
398                         break;
399                 default:
400                         ksft_exit_fail_msg("Unknown argument\n");
401                 }
402         }
403
404         cpus = num_processors();
405         tests = 0;
406
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;
410         } else {
411                 sve_vl_count = 0;
412         }
413
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;
417         } else {
418                 sme_vl_count = 0;
419         }
420
421         /* Force context switching if we only have FPSIMD */
422         if (!sve_vl_count && !sme_vl_count)
423                 fpsimd_per_cpu = 2;
424         else
425                 fpsimd_per_cpu = 1;
426         tests += cpus * fpsimd_per_cpu;
427
428         ksft_print_header();
429         ksft_set_plan(tests);
430
431         ksft_print_msg("%d CPUs, %d SVE VLs, %d SME VLs\n",
432                        cpus, sve_vl_count, sme_vl_count);
433
434         if (timeout > 0)
435                 ksft_print_msg("Will run for %ds\n", timeout);
436         else
437                 ksft_print_msg("Will run until terminated\n");
438
439         children = calloc(sizeof(*children), tests);
440         if (!children)
441                 ksft_exit_fail_msg("Unable to allocate child data\n");
442
443         ret = epoll_create1(EPOLL_CLOEXEC);
444         if (ret < 0)
445                 ksft_exit_fail_msg("epoll_create1() failed: %s (%d)\n",
446                                    strerror(errno), ret);
447         epoll_fd = ret;
448
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);
455         if (ret < 0)
456                 ksft_print_msg("Failed to install SIGINT handler: %s (%d)\n",
457                                strerror(errno), errno);
458         ret = sigaction(SIGTERM, &sa, NULL);
459         if (ret < 0)
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);
464         if (ret < 0)
465                 ksft_print_msg("Failed to install SIGCHLD handler: %s (%d)\n",
466                                strerror(errno), errno);
467
468         for (i = 0; i < cpus; i++) {
469                 for (j = 0; j < fpsimd_per_cpu; j++)
470                         start_fpsimd(&children[num_children++], i, j);
471
472                 for (j = 0; j < sve_vl_count; j++)
473                         start_sve(&children[num_children++], sve_vls[j], i);
474
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);
478                 }
479         }
480
481         for (;;) {
482                 /* Did we get a signal asking us to exit? */
483                 if (terminate)
484                         break;
485
486                 /*
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.
493                  */
494                 ret = epoll_wait(epoll_fd, &ev, 1, 1000);
495                 if (ret < 0) {
496                         if (errno == EINTR)
497                                 continue;
498                         ksft_exit_fail_msg("epoll_wait() failed: %s (%d)\n",
499                                            strerror(errno), errno);
500                 }
501
502                 /* Output? */
503                 if (ret == 1) {
504                         child_output(ev.data.ptr, ev.events, false);
505                         continue;
506                 }
507
508                 /* Otherwise epoll_wait() timed out */
509
510                 for (i = 0; i < num_children; i++)
511                         child_tickle(&children[i]);
512
513                 /* Negative timeout means run indefinitely */
514                 if (timeout < 0)
515                         continue;
516                 if (--timeout == 0)
517                         break;
518         }
519
520         ksft_print_msg("Finishing up...\n");
521
522         for (i = 0; i < tests; i++)
523                 child_stop(&children[i]);
524
525         drain_output(false);
526
527         for (i = 0; i < tests; i++)
528                 child_cleanup(&children[i]);
529
530         drain_output(true);
531
532         ksft_print_cnts();
533
534         return 0;
535 }