Skip to content

Commit d748817

Browse files
eddyz87intel-lab-lkp
authored andcommitted
selftests/bpf: watchdog timer for test_progs
This commit provides a watchdog timer that sets a limit of how long a single sub-test could run: - if sub-test runs for 10 seconds, the name of the test is printed (currently the name of the test is printed only after it finishes); - if sub-test runs for 120 seconds, the running thread is terminated with SIGSEGV (to trigger crash_handler() and get a stack trace). Specifically: - the timer is armed on each call to run_one_test(); - re-armed at each call to test__start_subtest(); - is stopped when exiting run_one_test(). Default timeout could be overridden using '-w' or '--watchdog-timeout' options. Value 0 can be used to turn the timer off. Here is an example execution: $ ./ssh-exec.sh ./test_progs -w 5 -t \ send_signal/send_signal_perf_thread_remote,send_signal/send_signal_nmi_thread_remote WATCHDOG: test case send_signal/send_signal_nmi_thread_remote executes for 5 seconds, terminating with SIGSEGV Caught signal torvalds#11! Stack trace: ./test_progs(crash_handler+0x1f)[0x9049ef] /lib64/libc.so.6(+0x40d00)[0x7f1f1184fd00] /lib64/libc.so.6(read+0x4a)[0x7f1f1191cc4a] ./test_progs[0x720dd3] ./test_progs[0x71ef7a] ./test_progs(test_send_signal+0x1db)[0x71edeb] ./test_progs[0x9066c5] ./test_progs(main+0x5ed)[0x9054ad] /lib64/libc.so.6(+0x2a088)[0x7f1f11839088] /lib64/libc.so.6(__libc_start_main+0x8b)[0x7f1f1183914b] ./test_progs(_start+0x25)[0x527385] torvalds#292 send_signal:FAIL test_send_signal_common:PASS:reading pipe 0 nsec test_send_signal_common:PASS:reading pipe error: size 0 0 nsec test_send_signal_common:PASS:incorrect result 0 nsec test_send_signal_common:PASS:pipe_write 0 nsec test_send_signal_common:PASS:setpriority 0 nsec Timer is implemented using timer_{create,start} librt API. Internally librt uses pthreads for SIGEV_THREAD timers, so this change adds a background timer thread to the test process. Because of this a few checks in tests 'bpf_iter' and 'iters' need an update to account for an extra thread. For parallelized scenario the watchdog is also created for each worker fork. If one of the workers gets stuck, it would be terminated by a watchdog. In theory, this might lead to a scenario when all worker threads are exhausted, however this should not be a problem for server_main(), as it would exit with some of the tests not run. Signed-off-by: Eduard Zingerman <[email protected]>
1 parent 47e2c45 commit d748817

File tree

4 files changed

+116
-6
lines changed

4 files changed

+116
-6
lines changed

tools/testing/selftests/bpf/prog_tests/bpf_iter.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ static void *run_test_task_tid(void *arg)
265265

266266
linfo.task.tid = 0;
267267
linfo.task.pid = getpid();
268-
/* This includes the parent thread, this thread,
268+
/* This includes the parent thread, this thread, watchdog timer thread
269269
* and the do_nothing_wait thread
270270
*/
271-
test_task_common(&opts, 2, 1);
271+
test_task_common(&opts, 3, 1);
272272

273273
test_task_common_nocheck(NULL, &num_unknown_tid, &num_known_tid);
274274
ASSERT_GT(num_unknown_tid, 2, "check_num_unknown_tid");
@@ -297,7 +297,7 @@ static void test_task_pid(void)
297297
opts.link_info = &linfo;
298298
opts.link_info_len = sizeof(linfo);
299299

300-
test_task_common(&opts, 1, 1);
300+
test_task_common(&opts, 2, 1);
301301
}
302302

303303
static void test_task_pidfd(void)
@@ -315,7 +315,7 @@ static void test_task_pidfd(void)
315315
opts.link_info = &linfo;
316316
opts.link_info_len = sizeof(linfo);
317317

318-
test_task_common(&opts, 1, 1);
318+
test_task_common(&opts, 2, 1);
319319

320320
close(pidfd);
321321
}

tools/testing/selftests/bpf/prog_tests/iters.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ static void subtest_task_iters(void)
192192
syscall(SYS_getpgid);
193193
iters_task__detach(skel);
194194
ASSERT_EQ(skel->bss->procs_cnt, 1, "procs_cnt");
195-
ASSERT_EQ(skel->bss->threads_cnt, thread_num + 1, "threads_cnt");
196-
ASSERT_EQ(skel->bss->proc_threads_cnt, thread_num + 1, "proc_threads_cnt");
195+
ASSERT_EQ(skel->bss->threads_cnt, thread_num + 2, "threads_cnt");
196+
ASSERT_EQ(skel->bss->proc_threads_cnt, thread_num + 2, "proc_threads_cnt");
197197
ASSERT_EQ(skel->bss->invalid_cnt, 0, "invalid_cnt");
198198
pthread_mutex_unlock(&do_nothing_mutex);
199199
for (int i = 0; i < thread_num; i++)

tools/testing/selftests/bpf/test_progs.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <sys/socket.h>
1717
#include <sys/un.h>
1818
#include <bpf/btf.h>
19+
#include <time.h>
1920
#include "json_writer.h"
2021

2122
#include "network_helpers.h"
@@ -179,6 +180,88 @@ int usleep(useconds_t usec)
179180
return syscall(__NR_nanosleep, &ts, NULL);
180181
}
181182

183+
/* Watchdog timer is started by watchdog_start() and stopped by watchdog_stop().
184+
* If timer is active for longer than env.secs_till_notify,
185+
* it prints the name of the current test to the stderr.
186+
* If timer is active for longer than env.secs_till_kill,
187+
* it kills the thread executing the test by sending a SIGSEGV signal to it.
188+
*/
189+
static void watchdog_timer_func(union sigval sigval)
190+
{
191+
struct itimerspec timeout = {};
192+
char test_name[256];
193+
int err;
194+
195+
if (env.subtest_state)
196+
snprintf(test_name, sizeof(test_name), "%s/%s",
197+
env.test->test_name, env.subtest_state->name);
198+
else
199+
snprintf(test_name, sizeof(test_name), "%s",
200+
env.test->test_name);
201+
202+
switch (env.watchdog_state) {
203+
case WD_NOTIFY:
204+
fprintf(env.stderr_saved, "WATCHDOG: test case %s executes for %d seconds...\n",
205+
test_name, env.secs_till_notify);
206+
timeout.it_value.tv_sec = env.secs_till_kill - env.secs_till_notify;
207+
env.watchdog_state = WD_KILL;
208+
err = timer_settime(env.watchdog, 0, &timeout, NULL);
209+
if (err)
210+
fprintf(env.stderr_saved, "Failed to arm watchdog timer\n");
211+
break;
212+
case WD_KILL:
213+
fprintf(env.stderr_saved,
214+
"WATCHDOG: test case %s executes for %d seconds, terminating with SIGSEGV\n",
215+
test_name, env.secs_till_kill);
216+
pthread_kill(env.main_thread, SIGSEGV);
217+
break;
218+
}
219+
}
220+
221+
static void watchdog_start(void)
222+
{
223+
struct itimerspec timeout = {};
224+
int err;
225+
226+
if (env.secs_till_kill == 0)
227+
return;
228+
if (env.secs_till_notify > 0) {
229+
env.watchdog_state = WD_NOTIFY;
230+
timeout.it_value.tv_sec = env.secs_till_notify;
231+
} else {
232+
env.watchdog_state = WD_KILL;
233+
timeout.it_value.tv_sec = env.secs_till_kill;
234+
}
235+
err = timer_settime(env.watchdog, 0, &timeout, NULL);
236+
if (err)
237+
fprintf(env.stderr_saved, "Failed to start watchdog timer\n");
238+
}
239+
240+
static void watchdog_stop(void)
241+
{
242+
struct itimerspec timeout = {};
243+
int err;
244+
245+
env.watchdog_state = WD_NOTIFY;
246+
err = timer_settime(env.watchdog, 0, &timeout, NULL);
247+
if (err)
248+
fprintf(env.stderr_saved, "Failed to stop watchdog timer\n");
249+
}
250+
251+
static void watchdog_init(void)
252+
{
253+
struct sigevent watchdog_sev = {
254+
.sigev_notify = SIGEV_THREAD,
255+
.sigev_notify_function = watchdog_timer_func,
256+
};
257+
int err;
258+
259+
env.main_thread = pthread_self();
260+
err = timer_create(CLOCK_MONOTONIC, &watchdog_sev, &env.watchdog);
261+
if (err)
262+
fprintf(stderr, "Failed to initialize watchdog timer\n");
263+
}
264+
182265
static bool should_run(struct test_selector *sel, int num, const char *name)
183266
{
184267
int i;
@@ -515,6 +598,7 @@ bool test__start_subtest(const char *subtest_name)
515598

516599
env.subtest_state = subtest_state;
517600
stdio_hijack_init(&subtest_state->log_buf, &subtest_state->log_cnt);
601+
watchdog_start();
518602

519603
return true;
520604
}
@@ -780,6 +864,7 @@ enum ARG_KEYS {
780864
ARG_DEBUG = -1,
781865
ARG_JSON_SUMMARY = 'J',
782866
ARG_TRAFFIC_MONITOR = 'm',
867+
ARG_WATCHDOG_TIMEOUT = 'w',
783868
};
784869

785870
static const struct argp_option opts[] = {
@@ -810,6 +895,8 @@ static const struct argp_option opts[] = {
810895
{ "traffic-monitor", ARG_TRAFFIC_MONITOR, "NAMES", 0,
811896
"Monitor network traffic of tests with name matching the pattern (supports '*' wildcard)." },
812897
#endif
898+
{ "watchdog-timeout", ARG_WATCHDOG_TIMEOUT, "SECONDS", 0,
899+
"Kill the process if tests are not making progress for specified number of seconds." },
813900
{},
814901
};
815902

@@ -1035,6 +1122,16 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
10351122
true);
10361123
break;
10371124
#endif
1125+
case ARG_WATCHDOG_TIMEOUT:
1126+
env->secs_till_kill = atoi(arg);
1127+
if (env->secs_till_kill < 0) {
1128+
fprintf(stderr, "Invalid watchdog timeout: %s.\n", arg);
1129+
return -EINVAL;
1130+
}
1131+
if (env->secs_till_kill < env->secs_till_notify) {
1132+
env->secs_till_notify = 0;
1133+
}
1134+
break;
10381135
default:
10391136
return ARGP_ERR_UNKNOWN;
10401137
}
@@ -1263,10 +1360,12 @@ static void run_one_test(int test_num)
12631360

12641361
stdio_hijack(&state->log_buf, &state->log_cnt);
12651362

1363+
watchdog_start();
12661364
if (test->run_test)
12671365
test->run_test();
12681366
else if (test->run_serial_test)
12691367
test->run_serial_test();
1368+
watchdog_stop();
12701369

12711370
/* ensure last sub-test is finalized properly */
12721371
if (env.subtest_state)
@@ -1707,6 +1806,7 @@ static int worker_main_send_subtests(int sock, struct test_state *state)
17071806
static int worker_main(int sock)
17081807
{
17091808
save_netns();
1809+
watchdog_init();
17101810

17111811
while (true) {
17121812
/* receive command */
@@ -1816,6 +1916,8 @@ int main(int argc, char **argv)
18161916

18171917
sigaction(SIGSEGV, &sigact, NULL);
18181918

1919+
env.secs_till_notify = 10;
1920+
env.secs_till_kill = 120;
18191921
err = argp_parse(&argp, argc, argv, 0, NULL, &env);
18201922
if (err)
18211923
return err;
@@ -1824,6 +1926,8 @@ int main(int argc, char **argv)
18241926
if (err)
18251927
return err;
18261928

1929+
watchdog_init();
1930+
18271931
/* Use libbpf 1.0 API mode */
18281932
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
18291933
libbpf_set_print(libbpf_print_fn);

tools/testing/selftests/bpf/test_progs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ struct test_env {
131131
pid_t *worker_pids; /* array of worker pids */
132132
int *worker_socks; /* array of worker socks */
133133
int *worker_current_test; /* array of current running test for each worker */
134+
135+
pthread_t main_thread;
136+
int secs_till_notify;
137+
int secs_till_kill;
138+
timer_t watchdog; /* watch for stalled tests/subtests */
139+
enum { WD_NOTIFY, WD_KILL } watchdog_state;
134140
};
135141

136142
#define MAX_LOG_TRUNK_SIZE 8192

0 commit comments

Comments
 (0)