Skip to content

Commit ef97bb0

Browse files
olsajiriKernel Patches Daemon
authored andcommitted
selftests/bpf: Add stacktrace ips test for kprobe_multi/kretprobe_multi
Adding test that attaches kprobe/kretprobe multi and verifies the ORC stacktrace matches expected functions. It skips the test for if kernels built with frame pointer unwinder. Signed-off-by: Jiri Olsa <[email protected]>
1 parent 6127bdc commit ef97bb0

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include "stacktrace_ips.skel.h"
4+
5+
#ifdef __x86_64__
6+
static int check_stacktrace_ips(int fd, __u32 key, int cnt, ...)
7+
{
8+
__u64 ips[PERF_MAX_STACK_DEPTH];
9+
struct ksyms *ksyms = NULL;
10+
int i, err = 0;
11+
va_list args;
12+
13+
/* sorted by addr */
14+
ksyms = load_kallsyms_local();
15+
if (!ASSERT_OK_PTR(ksyms, "load_kallsyms_local"))
16+
return -1;
17+
18+
/* unlikely, but... */
19+
if (!ASSERT_LT(cnt, PERF_MAX_STACK_DEPTH, "check_max"))
20+
return -1;
21+
22+
err = bpf_map_lookup_elem(fd, &key, ips);
23+
if (err)
24+
goto out;
25+
26+
va_start(args, cnt);
27+
28+
for (i = 0; i < cnt; i++) {
29+
unsigned long val;
30+
struct ksym *ksym;
31+
32+
if (!ASSERT_NEQ(ips[i], 0, "ip_not_zero"))
33+
break;
34+
val = va_arg(args, unsigned long);
35+
ksym = ksym_search_local(ksyms, ips[i]);
36+
if (!ASSERT_OK_PTR(ksym, "ksym_search_local"))
37+
break;
38+
ASSERT_EQ(ksym->addr, val, "stack_cmp");
39+
}
40+
41+
va_end(args);
42+
43+
out:
44+
free_kallsyms_local(ksyms);
45+
return err;
46+
}
47+
48+
static void test_stacktrace_ips_kprobe_multi(bool retprobe)
49+
{
50+
LIBBPF_OPTS(bpf_kprobe_multi_opts, opts,
51+
.retprobe = retprobe
52+
);
53+
LIBBPF_OPTS(bpf_test_run_opts, topts);
54+
struct stacktrace_ips *skel;
55+
int prog_fd, err;
56+
57+
skel = stacktrace_ips__open_and_load();
58+
if (!ASSERT_OK_PTR(skel, "stacktrace_ips__open_and_load"))
59+
return;
60+
61+
if (!skel->kconfig->CONFIG_UNWINDER_ORC) {
62+
test__skip();
63+
goto cleanup;
64+
}
65+
66+
skel->links.kprobe_multi_stack_test = bpf_program__attach_kprobe_multi_opts(
67+
skel->progs.kprobe_multi_stack_test,
68+
"bpf_testmod_stacktrace_test", &opts);
69+
if (!ASSERT_OK_PTR(skel->links.kprobe_multi_stack_test, "bpf_program__attach_kprobe_multi_opts"))
70+
goto cleanup;
71+
72+
prog_fd = bpf_program__fd(skel->progs.trigger);
73+
err = bpf_prog_test_run_opts(prog_fd, &topts);
74+
ASSERT_OK(err, "test_run");
75+
ASSERT_EQ(topts.retval, 0, "test_run");
76+
77+
trigger_module_test_read(1);
78+
79+
load_kallsyms();
80+
81+
check_stacktrace_ips(bpf_map__fd(skel->maps.stackmap), skel->bss->stack_key, 3,
82+
ksym_get_addr("bpf_testmod_stacktrace_test_3"),
83+
ksym_get_addr("bpf_testmod_stacktrace_test_2"),
84+
ksym_get_addr("bpf_testmod_stacktrace_test_1"),
85+
ksym_get_addr("bpf_testmod_test_read"));
86+
87+
cleanup:
88+
stacktrace_ips__destroy(skel);
89+
}
90+
91+
static void __test_stacktrace_ips(void)
92+
{
93+
if (test__start_subtest("kprobe_multi"))
94+
test_stacktrace_ips_kprobe_multi(false);
95+
if (test__start_subtest("kretprobe_multi"))
96+
test_stacktrace_ips_kprobe_multi(true);
97+
}
98+
#else
99+
static void __test_stacktrace_ips(void)
100+
{
101+
test__skip();
102+
}
103+
#endif
104+
105+
void test_stacktrace_ips(void)
106+
{
107+
__test_stacktrace_ips();
108+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (c) 2018 Facebook
3+
4+
#include <vmlinux.h>
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
8+
#ifndef PERF_MAX_STACK_DEPTH
9+
#define PERF_MAX_STACK_DEPTH 127
10+
#endif
11+
12+
typedef __u64 stack_trace_t[PERF_MAX_STACK_DEPTH];
13+
14+
struct {
15+
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
16+
__uint(max_entries, 16384);
17+
__type(key, __u32);
18+
__type(value, stack_trace_t);
19+
} stackmap SEC(".maps");
20+
21+
extern bool CONFIG_UNWINDER_ORC __kconfig __weak;
22+
23+
/*
24+
* This function is here to have CONFIG_UNWINDER_ORC
25+
* used and added to object BTF.
26+
*/
27+
int unused(void)
28+
{
29+
return CONFIG_UNWINDER_ORC ? 0 : 1;
30+
}
31+
32+
__u32 stack_key;
33+
34+
/*
35+
* No tests in here, just to trigger 'bpf_fentry_test*'
36+
* through tracing test_run.
37+
*/
38+
SEC("fentry/bpf_modify_return_test")
39+
int BPF_PROG(trigger)
40+
{
41+
return 0;
42+
}
43+
44+
SEC("kprobe.multi")
45+
int kprobe_multi_stack_test(struct pt_regs *ctx)
46+
{
47+
stack_key = bpf_get_stackid(ctx, &stackmap, 0);
48+
return 0;
49+
}
50+
51+
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/test_kmods/bpf_testmod.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,30 @@ noinline int bpf_testmod_fentry_test11(u64 a, void *b, short c, int d,
417417
return a + (long)b + c + d + (long)e + f + g + h + i + j + k;
418418
}
419419

420+
noinline void bpf_testmod_stacktrace_test(void)
421+
{
422+
/* used for stacktrace test as attach function */
423+
asm volatile ("");
424+
}
425+
426+
noinline void bpf_testmod_stacktrace_test_3(void)
427+
{
428+
bpf_testmod_stacktrace_test();
429+
asm volatile ("");
430+
}
431+
432+
noinline void bpf_testmod_stacktrace_test_2(void)
433+
{
434+
bpf_testmod_stacktrace_test_3();
435+
asm volatile ("");
436+
}
437+
438+
noinline void bpf_testmod_stacktrace_test_1(void)
439+
{
440+
bpf_testmod_stacktrace_test_2();
441+
asm volatile ("");
442+
}
443+
420444
int bpf_testmod_fentry_ok;
421445

422446
noinline ssize_t
@@ -497,6 +521,8 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
497521
21, 22, 23, 24, 25, 26) != 231)
498522
goto out;
499523

524+
bpf_testmod_stacktrace_test_1();
525+
500526
bpf_testmod_fentry_ok = 1;
501527
out:
502528
return -EIO; /* always fail */

0 commit comments

Comments
 (0)