Skip to content

Commit 01e4ae4

Browse files
olsajirianakryiko
authored andcommitted
selftests/bpf: Add test for missed counts of perf event link kprobe
Adding test that puts kprobe on bpf_fentry_test1 that calls bpf_kfunc_common_test kfunc, which has also kprobe on. The latter won't get triggered due to kprobe recursion check and kprobe missed counter is incremented. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Hou Tao <houtao1@huawei.com> Link: https://lore.kernel.org/bpf/20230920213145.1941596-8-jolsa@kernel.org
1 parent b563b9b commit 01e4ae4

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ __bpf_kfunc void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it)
138138
it->cnt = 0;
139139
}
140140

141+
__bpf_kfunc void bpf_kfunc_common_test(void)
142+
{
143+
}
144+
141145
struct bpf_testmod_btf_type_tag_1 {
142146
int a;
143147
};
@@ -343,6 +347,7 @@ BTF_SET8_START(bpf_testmod_common_kfunc_ids)
343347
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
344348
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
345349
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY)
350+
BTF_ID_FLAGS(func, bpf_kfunc_common_test)
346351
BTF_SET8_END(bpf_testmod_common_kfunc_ids)
347352

348353
static const struct btf_kfunc_id_set bpf_testmod_common_kfunc_set = {

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,6 @@ void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p);
104104
void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p);
105105
void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p);
106106
void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len);
107+
108+
void bpf_kfunc_common_test(void) __ksym;
107109
#endif /* _BPF_TESTMOD_KFUNC_H */
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include "missed_kprobe.skel.h"
4+
5+
/*
6+
* Putting kprobe on bpf_fentry_test1 that calls bpf_kfunc_common_test
7+
* kfunc, which has also kprobe on. The latter won't get triggered due
8+
* to kprobe recursion check and kprobe missed counter is incremented.
9+
*/
10+
static void test_missed_perf_kprobe(void)
11+
{
12+
LIBBPF_OPTS(bpf_test_run_opts, topts);
13+
struct bpf_link_info info = {};
14+
struct missed_kprobe *skel;
15+
__u32 len = sizeof(info);
16+
int err, prog_fd;
17+
18+
skel = missed_kprobe__open_and_load();
19+
if (!ASSERT_OK_PTR(skel, "missed_kprobe__open_and_load"))
20+
goto cleanup;
21+
22+
err = missed_kprobe__attach(skel);
23+
if (!ASSERT_OK(err, "missed_kprobe__attach"))
24+
goto cleanup;
25+
26+
prog_fd = bpf_program__fd(skel->progs.trigger);
27+
err = bpf_prog_test_run_opts(prog_fd, &topts);
28+
ASSERT_OK(err, "test_run");
29+
ASSERT_EQ(topts.retval, 0, "test_run");
30+
31+
err = bpf_link_get_info_by_fd(bpf_link__fd(skel->links.test2), &info, &len);
32+
if (!ASSERT_OK(err, "bpf_link_get_info_by_fd"))
33+
goto cleanup;
34+
35+
ASSERT_EQ(info.type, BPF_LINK_TYPE_PERF_EVENT, "info.type");
36+
ASSERT_EQ(info.perf_event.type, BPF_PERF_EVENT_KPROBE, "info.perf_event.type");
37+
ASSERT_EQ(info.perf_event.kprobe.missed, 1, "info.perf_event.kprobe.missed");
38+
39+
cleanup:
40+
missed_kprobe__destroy(skel);
41+
}
42+
43+
void test_missed(void)
44+
{
45+
if (test__start_subtest("perf_kprobe"))
46+
test_missed_perf_kprobe();
47+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include "vmlinux.h"
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include "../bpf_testmod/bpf_testmod_kfunc.h"
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
/*
10+
* No tests in here, just to trigger 'bpf_fentry_test*'
11+
* through tracing test_run
12+
*/
13+
SEC("fentry/bpf_modify_return_test")
14+
int BPF_PROG(trigger)
15+
{
16+
return 0;
17+
}
18+
19+
SEC("kprobe/bpf_fentry_test1")
20+
int test1(struct pt_regs *ctx)
21+
{
22+
bpf_kfunc_common_test();
23+
return 0;
24+
}
25+
26+
SEC("kprobe/bpf_kfunc_common_test")
27+
int test2(struct pt_regs *ctx)
28+
{
29+
return 0;
30+
}

0 commit comments

Comments
 (0)