Skip to content

Commit fe44dc6

Browse files
olsajiriKernel Patches Daemon
authored andcommitted
selftests/bpf: Add stacktrace test for kprobe multi
Adding stacktrace test for kprobe multi probe. Cc: Feng Yang <[email protected]> Signed-off-by: Jiri Olsa <[email protected]>
1 parent d3f58a7 commit fe44dc6

File tree

2 files changed

+106
-29
lines changed

2 files changed

+106
-29
lines changed

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

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <test_progs.h>
3+
#include "test_stacktrace_map.skel.h"
34

4-
void test_stacktrace_map(void)
5+
static void check_stackmap(int control_map_fd, int stackid_hmap_fd,
6+
int stackmap_fd, int stack_amap_fd)
7+
{
8+
__u32 key, val, duration = 0;
9+
int err, stack_trace_len;
10+
11+
/* disable stack trace collection */
12+
key = 0;
13+
val = 1;
14+
bpf_map_update_elem(control_map_fd, &key, &val, 0);
15+
16+
/* for every element in stackid_hmap, we can find a corresponding one
17+
* in stackmap, and vice versa.
18+
*/
19+
err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
20+
if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
21+
"err %d errno %d\n", err, errno))
22+
return;
23+
24+
err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
25+
if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
26+
"err %d errno %d\n", err, errno))
27+
return;
28+
29+
stack_trace_len = PERF_MAX_STACK_DEPTH * sizeof(__u64);
30+
err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len);
31+
CHECK(err, "compare_stack_ips stackmap vs. stack_amap",
32+
"err %d errno %d\n", err, errno);
33+
}
34+
35+
static void test_stacktrace_map_tp(void)
536
{
637
int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
738
const char *prog_name = "oncpu";
8-
int err, prog_fd, stack_trace_len;
39+
int err, prog_fd;
940
const char *file = "./test_stacktrace_map.bpf.o";
10-
__u32 key, val, duration = 0;
41+
__u32 duration = 0;
1142
struct bpf_program *prog;
1243
struct bpf_object *obj;
1344
struct bpf_link *link;
@@ -44,32 +75,56 @@ void test_stacktrace_map(void)
4475
/* give some time for bpf program run */
4576
sleep(1);
4677

47-
/* disable stack trace collection */
48-
key = 0;
49-
val = 1;
50-
bpf_map_update_elem(control_map_fd, &key, &val, 0);
51-
52-
/* for every element in stackid_hmap, we can find a corresponding one
53-
* in stackmap, and vice versa.
54-
*/
55-
err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
56-
if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
57-
"err %d errno %d\n", err, errno))
58-
goto disable_pmu;
59-
60-
err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
61-
if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
62-
"err %d errno %d\n", err, errno))
63-
goto disable_pmu;
64-
65-
stack_trace_len = PERF_MAX_STACK_DEPTH * sizeof(__u64);
66-
err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len);
67-
if (CHECK(err, "compare_stack_ips stackmap vs. stack_amap",
68-
"err %d errno %d\n", err, errno))
69-
goto disable_pmu;
78+
check_stackmap(control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd);
7079

7180
disable_pmu:
7281
bpf_link__destroy(link);
7382
close_prog:
7483
bpf_object__close(obj);
7584
}
85+
86+
static void test_stacktrace_map_kprobe_multi(bool retprobe)
87+
{
88+
int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
89+
LIBBPF_OPTS(bpf_kprobe_multi_opts, opts,
90+
.retprobe = retprobe
91+
);
92+
LIBBPF_OPTS(bpf_test_run_opts, topts);
93+
struct test_stacktrace_map *skel;
94+
struct bpf_link *link;
95+
int prog_fd, err;
96+
97+
skel = test_stacktrace_map__open_and_load();
98+
if (!ASSERT_OK_PTR(skel, "test_stacktrace_map__open_and_load"))
99+
return;
100+
101+
link = bpf_program__attach_kprobe_multi_opts(skel->progs.kprobe,
102+
"bpf_fentry_test1", &opts);
103+
if (!ASSERT_OK_PTR(link, "bpf_program__attach_kprobe_multi_opts"))
104+
goto cleanup;
105+
106+
prog_fd = bpf_program__fd(skel->progs.trigger);
107+
err = bpf_prog_test_run_opts(prog_fd, &topts);
108+
ASSERT_OK(err, "test_run");
109+
ASSERT_EQ(topts.retval, 0, "test_run");
110+
111+
control_map_fd = bpf_map__fd(skel->maps.control_map);
112+
stackid_hmap_fd = bpf_map__fd(skel->maps.stackid_hmap);
113+
stackmap_fd = bpf_map__fd(skel->maps.stackmap);
114+
stack_amap_fd = bpf_map__fd(skel->maps.stack_amap);
115+
116+
check_stackmap(control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd);
117+
118+
cleanup:
119+
test_stacktrace_map__destroy(skel);
120+
}
121+
122+
void test_stacktrace_map(void)
123+
{
124+
if (test__start_subtest("tp"))
125+
test_stacktrace_map_tp();
126+
if (test__start_subtest("kprobe_multi"))
127+
test_stacktrace_map_kprobe_multi(false);
128+
if (test__start_subtest("kretprobe_multi"))
129+
test_stacktrace_map_kprobe_multi(true);
130+
}

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <vmlinux.h>
55
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
67

78
#ifndef PERF_MAX_STACK_DEPTH
89
#define PERF_MAX_STACK_DEPTH 127
@@ -50,16 +51,15 @@ struct sched_switch_args {
5051
int next_prio;
5152
};
5253

53-
SEC("tracepoint/sched/sched_switch")
54-
int oncpu(struct sched_switch_args *ctx)
54+
static inline void test_stackmap(void *ctx)
5555
{
5656
__u32 max_len = PERF_MAX_STACK_DEPTH * sizeof(__u64);
5757
__u32 key = 0, val = 0, *value_p;
5858
void *stack_p;
5959

6060
value_p = bpf_map_lookup_elem(&control_map, &key);
6161
if (value_p && *value_p)
62-
return 0; /* skip if non-zero *value_p */
62+
return; /* skip if non-zero *value_p */
6363

6464
/* The size of stackmap and stackid_hmap should be the same */
6565
key = bpf_get_stackid(ctx, &stackmap, 0);
@@ -69,7 +69,29 @@ int oncpu(struct sched_switch_args *ctx)
6969
if (stack_p)
7070
bpf_get_stack(ctx, stack_p, max_len, 0);
7171
}
72+
}
73+
74+
SEC("tracepoint/sched/sched_switch")
75+
int oncpu(struct sched_switch_args *ctx)
76+
{
77+
test_stackmap(ctx);
78+
return 0;
79+
}
7280

81+
/*
82+
* No tests in here, just to trigger 'bpf_fentry_test*'
83+
* through tracing test_run.
84+
*/
85+
SEC("fentry/bpf_modify_return_test")
86+
int BPF_PROG(trigger)
87+
{
88+
return 0;
89+
}
90+
91+
SEC("kprobe.multi")
92+
int kprobe(struct pt_regs *ctx)
93+
{
94+
test_stackmap(ctx);
7395
return 0;
7496
}
7597

0 commit comments

Comments
 (0)