1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* Copyright 2023 Mike Galbraith <efault-AT-gmx.de> */
3*49cdfc7eSAndroid Build Coastguard Worker /* Copyright 2023 Wei Gao <[email protected]> */
4*49cdfc7eSAndroid Build Coastguard Worker /*\
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * [Description]
7*49cdfc7eSAndroid Build Coastguard Worker *
8*49cdfc7eSAndroid Build Coastguard Worker * Thread starvation test. On fauluty kernel the test timeouts.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Original reproducer taken from:
11*49cdfc7eSAndroid Build Coastguard Worker * https://lore.kernel.org/lkml/[email protected]/
12*49cdfc7eSAndroid Build Coastguard Worker */
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_clocks.h"
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker static char *str_loop;
28*49cdfc7eSAndroid Build Coastguard Worker static long loop = 1000000;
29*49cdfc7eSAndroid Build Coastguard Worker static char *str_timeout;
30*49cdfc7eSAndroid Build Coastguard Worker static int timeout;
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker #define CALLIBRATE_LOOPS 120000000
33*49cdfc7eSAndroid Build Coastguard Worker
callibrate(void)34*49cdfc7eSAndroid Build Coastguard Worker static int callibrate(void)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker int i;
37*49cdfc7eSAndroid Build Coastguard Worker struct timespec start, stop;
38*49cdfc7eSAndroid Build Coastguard Worker long long diff;
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < CALLIBRATE_LOOPS; i++)
41*49cdfc7eSAndroid Build Coastguard Worker __asm__ __volatile__ ("" : "+g" (i) : :);
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOCK_GETTIME(CLOCK_MONOTONIC_RAW, &start);
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < CALLIBRATE_LOOPS; i++)
46*49cdfc7eSAndroid Build Coastguard Worker __asm__ __volatile__ ("" : "+g" (i) : :);
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOCK_GETTIME(CLOCK_MONOTONIC_RAW, &stop);
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker diff = tst_timespec_diff_us(stop, start);
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "CPU did %i loops in %llius", CALLIBRATE_LOOPS, diff);
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker return diff;
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
wait_for_pid(pid_t pid)57*49cdfc7eSAndroid Build Coastguard Worker static int wait_for_pid(pid_t pid)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker int status, ret;
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker again:
62*49cdfc7eSAndroid Build Coastguard Worker ret = waitpid(pid, &status, 0);
63*49cdfc7eSAndroid Build Coastguard Worker if (ret == -1) {
64*49cdfc7eSAndroid Build Coastguard Worker if (errno == EINTR)
65*49cdfc7eSAndroid Build Coastguard Worker goto again;
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker return -1;
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker if (WIFSIGNALED(status))
71*49cdfc7eSAndroid Build Coastguard Worker return 0;
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker return -1;
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker
setup(void)76*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
77*49cdfc7eSAndroid Build Coastguard Worker {
78*49cdfc7eSAndroid Build Coastguard Worker cpu_set_t mask;
79*49cdfc7eSAndroid Build Coastguard Worker int cpu = 0;
80*49cdfc7eSAndroid Build Coastguard Worker long ncpus = tst_ncpus_conf();
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker CPU_ZERO(&mask);
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker /* Restrict test to a single cpu */
85*49cdfc7eSAndroid Build Coastguard Worker if (sched_getaffinity(0, sizeof(mask), &mask) < 0)
86*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "sched_getaffinity() failed");
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker if (CPU_COUNT(&mask) == 0)
89*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "No cpus available");
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker while (CPU_ISSET(cpu, &mask) == 0 && cpu < ncpus)
92*49cdfc7eSAndroid Build Coastguard Worker cpu++;
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker CPU_ZERO(&mask);
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker CPU_SET(cpu, &mask);
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Setting affinity to CPU %d", cpu);
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker if (sched_setaffinity(0, sizeof(mask), &mask) < 0)
101*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "sched_setaffinity() failed");
102*49cdfc7eSAndroid Build Coastguard Worker
103*49cdfc7eSAndroid Build Coastguard Worker if (tst_parse_long(str_loop, &loop, 1, LONG_MAX))
104*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "Invalid number of loop number '%s'", str_loop);
105*49cdfc7eSAndroid Build Coastguard Worker
106*49cdfc7eSAndroid Build Coastguard Worker if (tst_parse_int(str_timeout, &timeout, 1, INT_MAX))
107*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "Invalid number of timeout '%s'", str_timeout);
108*49cdfc7eSAndroid Build Coastguard Worker else
109*49cdfc7eSAndroid Build Coastguard Worker timeout = callibrate() / 1000;
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard Worker tst_set_max_runtime(timeout);
112*49cdfc7eSAndroid Build Coastguard Worker }
113*49cdfc7eSAndroid Build Coastguard Worker
handler(int sig LTP_ATTRIBUTE_UNUSED)114*49cdfc7eSAndroid Build Coastguard Worker static void handler(int sig LTP_ATTRIBUTE_UNUSED)
115*49cdfc7eSAndroid Build Coastguard Worker {
116*49cdfc7eSAndroid Build Coastguard Worker if (loop > 0)
117*49cdfc7eSAndroid Build Coastguard Worker --loop;
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker
child(void)120*49cdfc7eSAndroid Build Coastguard Worker static void child(void)
121*49cdfc7eSAndroid Build Coastguard Worker {
122*49cdfc7eSAndroid Build Coastguard Worker pid_t ppid = getppid();
123*49cdfc7eSAndroid Build Coastguard Worker
124*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAIT(0);
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker while (1)
127*49cdfc7eSAndroid Build Coastguard Worker SAFE_KILL(ppid, SIGUSR1);
128*49cdfc7eSAndroid Build Coastguard Worker }
129*49cdfc7eSAndroid Build Coastguard Worker
do_test(void)130*49cdfc7eSAndroid Build Coastguard Worker static void do_test(void)
131*49cdfc7eSAndroid Build Coastguard Worker {
132*49cdfc7eSAndroid Build Coastguard Worker pid_t child_pid;
133*49cdfc7eSAndroid Build Coastguard Worker
134*49cdfc7eSAndroid Build Coastguard Worker child_pid = SAFE_FORK();
135*49cdfc7eSAndroid Build Coastguard Worker
136*49cdfc7eSAndroid Build Coastguard Worker if (!child_pid)
137*49cdfc7eSAndroid Build Coastguard Worker child();
138*49cdfc7eSAndroid Build Coastguard Worker
139*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGNAL(SIGUSR1, handler);
140*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAKE(0);
141*49cdfc7eSAndroid Build Coastguard Worker
142*49cdfc7eSAndroid Build Coastguard Worker while (loop)
143*49cdfc7eSAndroid Build Coastguard Worker sleep(1);
144*49cdfc7eSAndroid Build Coastguard Worker
145*49cdfc7eSAndroid Build Coastguard Worker SAFE_KILL(child_pid, SIGTERM);
146*49cdfc7eSAndroid Build Coastguard Worker
147*49cdfc7eSAndroid Build Coastguard Worker if (!tst_remaining_runtime())
148*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Scheduller starvation reproduced.");
149*49cdfc7eSAndroid Build Coastguard Worker else
150*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Haven't reproduced scheduller starvation.");
151*49cdfc7eSAndroid Build Coastguard Worker
152*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS_SILENT(wait_for_pid(child_pid));
153*49cdfc7eSAndroid Build Coastguard Worker }
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
156*49cdfc7eSAndroid Build Coastguard Worker .test_all = do_test,
157*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
158*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
159*49cdfc7eSAndroid Build Coastguard Worker .options = (struct tst_option[]) {
160*49cdfc7eSAndroid Build Coastguard Worker {"l:", &str_loop, "Number of loops (default 2000000)"},
161*49cdfc7eSAndroid Build Coastguard Worker {"t:", &str_timeout, "Max timeout (default 240s)"},
162*49cdfc7eSAndroid Build Coastguard Worker {}
163*49cdfc7eSAndroid Build Coastguard Worker },
164*49cdfc7eSAndroid Build Coastguard Worker .needs_checkpoints = 1,
165*49cdfc7eSAndroid Build Coastguard Worker };
166