Skip to content

Commit edd03fc

Browse files
puranjaymohanAlexei Starovoitov
authored andcommitted
selftests: bpf: use __stderr in stream error tests
Start using __stderr directly in the bpf programs to test the reporting of may_goto timeout detection and spin_lock dead lock detection. Signed-off-by: Puranjay Mohan <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 744eeb2 commit edd03fc

File tree

2 files changed

+17
-82
lines changed

2 files changed

+17
-82
lines changed

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

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
33
#include <test_progs.h>
44
#include <sys/mman.h>
5-
#include <regex.h>
65

76
#include "stream.skel.h"
87
#include "stream_fail.skel.h"
@@ -18,87 +17,6 @@ void test_stream_success(void)
1817
return;
1918
}
2019

21-
struct {
22-
int prog_off;
23-
const char *errstr;
24-
} stream_error_arr[] = {
25-
{
26-
offsetof(struct stream, progs.stream_cond_break),
27-
"ERROR: Timeout detected for may_goto instruction\n"
28-
"CPU: [0-9]+ UID: 0 PID: [0-9]+ Comm: .*\n"
29-
"Call trace:\n"
30-
"([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n"
31-
"|[ \t]+[^\n]+\n)*",
32-
},
33-
{
34-
offsetof(struct stream, progs.stream_deadlock),
35-
"ERROR: AA or ABBA deadlock detected for bpf_res_spin_lock\n"
36-
"Attempted lock = (0x[0-9a-fA-F]+)\n"
37-
"Total held locks = 1\n"
38-
"Held lock\\[ 0\\] = \\1\n" // Lock address must match
39-
"CPU: [0-9]+ UID: 0 PID: [0-9]+ Comm: .*\n"
40-
"Call trace:\n"
41-
"([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n"
42-
"|[ \t]+[^\n]+\n)*",
43-
},
44-
};
45-
46-
static int match_regex(const char *pattern, const char *string)
47-
{
48-
int err, rc;
49-
regex_t re;
50-
51-
err = regcomp(&re, pattern, REG_EXTENDED | REG_NEWLINE);
52-
if (err)
53-
return -1;
54-
rc = regexec(&re, string, 0, NULL, 0);
55-
regfree(&re);
56-
return rc == 0 ? 1 : 0;
57-
}
58-
59-
void test_stream_errors(void)
60-
{
61-
LIBBPF_OPTS(bpf_test_run_opts, opts);
62-
LIBBPF_OPTS(bpf_prog_stream_read_opts, ropts);
63-
struct stream *skel;
64-
int ret, prog_fd;
65-
char buf[1024];
66-
67-
skel = stream__open_and_load();
68-
if (!ASSERT_OK_PTR(skel, "stream__open_and_load"))
69-
return;
70-
71-
for (int i = 0; i < ARRAY_SIZE(stream_error_arr); i++) {
72-
struct bpf_program **prog;
73-
74-
prog = (struct bpf_program **)(((char *)skel) + stream_error_arr[i].prog_off);
75-
prog_fd = bpf_program__fd(*prog);
76-
ret = bpf_prog_test_run_opts(prog_fd, &opts);
77-
ASSERT_OK(ret, "ret");
78-
ASSERT_OK(opts.retval, "retval");
79-
80-
#if !defined(__x86_64__) && !defined(__s390x__) && !defined(__aarch64__)
81-
ASSERT_TRUE(1, "Timed may_goto unsupported, skip.");
82-
if (i == 0) {
83-
ret = bpf_prog_stream_read(prog_fd, 2, buf, sizeof(buf), &ropts);
84-
ASSERT_EQ(ret, 0, "stream read");
85-
continue;
86-
}
87-
#endif
88-
89-
ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDERR, buf, sizeof(buf), &ropts);
90-
ASSERT_GT(ret, 0, "stream read");
91-
ASSERT_LE(ret, 1023, "len for buf");
92-
buf[ret] = '\0';
93-
94-
ret = match_regex(stream_error_arr[i].errstr, buf);
95-
if (!ASSERT_TRUE(ret == 1, "regex match"))
96-
fprintf(stderr, "Output from stream:\n%s\n", buf);
97-
}
98-
99-
stream__destroy(skel);
100-
}
101-
10220
void test_stream_syscall(void)
10321
{
10422
LIBBPF_OPTS(bpf_test_run_opts, opts);

tools/testing/selftests/bpf/progs/stream.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ int stream_exhaust(void *ctx)
3737
}
3838

3939
SEC("syscall")
40+
__arch_x86_64
41+
__arch_arm64
42+
__arch_s390x
4043
__success __retval(0)
44+
__stderr("ERROR: Timeout detected for may_goto instruction")
45+
__stderr("CPU: {{[0-9]+}} UID: 0 PID: {{[0-9]+}} Comm: {{.*}}")
46+
__stderr("Call trace:\n"
47+
"{{([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n"
48+
"|[ \t]+[^\n]+\n)*}}")
4149
int stream_cond_break(void *ctx)
4250
{
4351
while (can_loop)
@@ -47,6 +55,15 @@ int stream_cond_break(void *ctx)
4755

4856
SEC("syscall")
4957
__success __retval(0)
58+
__stderr("ERROR: AA or ABBA deadlock detected for bpf_res_spin_lock")
59+
__stderr("{{Attempted lock = (0x[0-9a-fA-F]+)\n"
60+
"Total held locks = 1\n"
61+
"Held lock\\[ 0\\] = \\1}}")
62+
__stderr("...")
63+
__stderr("CPU: {{[0-9]+}} UID: 0 PID: {{[0-9]+}} Comm: {{.*}}")
64+
__stderr("Call trace:\n"
65+
"{{([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n"
66+
"|[ \t]+[^\n]+\n)*}}")
5067
int stream_deadlock(void *ctx)
5168
{
5269
struct bpf_res_spin_lock *lock, *nlock;

0 commit comments

Comments
 (0)