101e4ae47SJiri Olsa // SPDX-License-Identifier: GPL-2.0
201e4ae47SJiri Olsa #include <test_progs.h>
301e4ae47SJiri Olsa #include "missed_kprobe.skel.h"
459e83c01SJiri Olsa #include "missed_kprobe_recursion.skel.h"
585981e0fSJiri Olsa #include "missed_tp_recursion.skel.h"
601e4ae47SJiri Olsa 
701e4ae47SJiri Olsa /*
801e4ae47SJiri Olsa  * Putting kprobe on bpf_fentry_test1 that calls bpf_kfunc_common_test
901e4ae47SJiri Olsa  * kfunc, which has also kprobe on. The latter won't get triggered due
1001e4ae47SJiri Olsa  * to kprobe recursion check and kprobe missed counter is incremented.
1101e4ae47SJiri Olsa  */
test_missed_perf_kprobe(void)1201e4ae47SJiri Olsa static void test_missed_perf_kprobe(void)
1301e4ae47SJiri Olsa {
1401e4ae47SJiri Olsa 	LIBBPF_OPTS(bpf_test_run_opts, topts);
1501e4ae47SJiri Olsa 	struct bpf_link_info info = {};
1601e4ae47SJiri Olsa 	struct missed_kprobe *skel;
1701e4ae47SJiri Olsa 	__u32 len = sizeof(info);
1801e4ae47SJiri Olsa 	int err, prog_fd;
1901e4ae47SJiri Olsa 
2001e4ae47SJiri Olsa 	skel = missed_kprobe__open_and_load();
2101e4ae47SJiri Olsa 	if (!ASSERT_OK_PTR(skel, "missed_kprobe__open_and_load"))
2201e4ae47SJiri Olsa 		goto cleanup;
2301e4ae47SJiri Olsa 
2401e4ae47SJiri Olsa 	err = missed_kprobe__attach(skel);
2501e4ae47SJiri Olsa 	if (!ASSERT_OK(err, "missed_kprobe__attach"))
2601e4ae47SJiri Olsa 		goto cleanup;
2701e4ae47SJiri Olsa 
2801e4ae47SJiri Olsa 	prog_fd = bpf_program__fd(skel->progs.trigger);
2901e4ae47SJiri Olsa 	err = bpf_prog_test_run_opts(prog_fd, &topts);
3001e4ae47SJiri Olsa 	ASSERT_OK(err, "test_run");
3101e4ae47SJiri Olsa 	ASSERT_EQ(topts.retval, 0, "test_run");
3201e4ae47SJiri Olsa 
3301e4ae47SJiri Olsa 	err = bpf_link_get_info_by_fd(bpf_link__fd(skel->links.test2), &info, &len);
3401e4ae47SJiri Olsa 	if (!ASSERT_OK(err, "bpf_link_get_info_by_fd"))
3501e4ae47SJiri Olsa 		goto cleanup;
3601e4ae47SJiri Olsa 
3701e4ae47SJiri Olsa 	ASSERT_EQ(info.type, BPF_LINK_TYPE_PERF_EVENT, "info.type");
3801e4ae47SJiri Olsa 	ASSERT_EQ(info.perf_event.type, BPF_PERF_EVENT_KPROBE, "info.perf_event.type");
3901e4ae47SJiri Olsa 	ASSERT_EQ(info.perf_event.kprobe.missed, 1, "info.perf_event.kprobe.missed");
4001e4ae47SJiri Olsa 
4101e4ae47SJiri Olsa cleanup:
4201e4ae47SJiri Olsa 	missed_kprobe__destroy(skel);
4301e4ae47SJiri Olsa }
4401e4ae47SJiri Olsa 
get_missed_count(int fd)4559e83c01SJiri Olsa static __u64 get_missed_count(int fd)
4659e83c01SJiri Olsa {
4759e83c01SJiri Olsa 	struct bpf_prog_info info = {};
4859e83c01SJiri Olsa 	__u32 len = sizeof(info);
4959e83c01SJiri Olsa 	int err;
5059e83c01SJiri Olsa 
5159e83c01SJiri Olsa 	err = bpf_prog_get_info_by_fd(fd, &info, &len);
5259e83c01SJiri Olsa 	if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd"))
5359e83c01SJiri Olsa 		return (__u64) -1;
5459e83c01SJiri Olsa 	return info.recursion_misses;
5559e83c01SJiri Olsa }
5659e83c01SJiri Olsa 
5759e83c01SJiri Olsa /*
5859e83c01SJiri Olsa  * Putting kprobe.multi on bpf_fentry_test1 that calls bpf_kfunc_common_test
5959e83c01SJiri Olsa  * kfunc which has 3 perf event kprobes and 1 kprobe.multi attached.
6059e83c01SJiri Olsa  *
6159e83c01SJiri Olsa  * Because fprobe (kprobe.multi attach layear) does not have strict recursion
6259e83c01SJiri Olsa  * check the kprobe's bpf_prog_active check is hit for test2-5.
6359e83c01SJiri Olsa  */
test_missed_kprobe_recursion(void)6459e83c01SJiri Olsa static void test_missed_kprobe_recursion(void)
6559e83c01SJiri Olsa {
6659e83c01SJiri Olsa 	LIBBPF_OPTS(bpf_test_run_opts, topts);
6759e83c01SJiri Olsa 	struct missed_kprobe_recursion *skel;
6859e83c01SJiri Olsa 	int err, prog_fd;
6959e83c01SJiri Olsa 
7059e83c01SJiri Olsa 	skel = missed_kprobe_recursion__open_and_load();
7159e83c01SJiri Olsa 	if (!ASSERT_OK_PTR(skel, "missed_kprobe_recursion__open_and_load"))
7259e83c01SJiri Olsa 		goto cleanup;
7359e83c01SJiri Olsa 
7459e83c01SJiri Olsa 	err = missed_kprobe_recursion__attach(skel);
7559e83c01SJiri Olsa 	if (!ASSERT_OK(err, "missed_kprobe_recursion__attach"))
7659e83c01SJiri Olsa 		goto cleanup;
7759e83c01SJiri Olsa 
7859e83c01SJiri Olsa 	prog_fd = bpf_program__fd(skel->progs.trigger);
7959e83c01SJiri Olsa 	err = bpf_prog_test_run_opts(prog_fd, &topts);
8059e83c01SJiri Olsa 	ASSERT_OK(err, "test_run");
8159e83c01SJiri Olsa 	ASSERT_EQ(topts.retval, 0, "test_run");
8259e83c01SJiri Olsa 
8359e83c01SJiri Olsa 	ASSERT_EQ(get_missed_count(bpf_program__fd(skel->progs.test1)), 0, "test1_recursion_misses");
84*08a7078fSAndrii Nakryiko 	ASSERT_GE(get_missed_count(bpf_program__fd(skel->progs.test2)), 1, "test2_recursion_misses");
85*08a7078fSAndrii Nakryiko 	ASSERT_GE(get_missed_count(bpf_program__fd(skel->progs.test3)), 1, "test3_recursion_misses");
86*08a7078fSAndrii Nakryiko 	ASSERT_GE(get_missed_count(bpf_program__fd(skel->progs.test4)), 1, "test4_recursion_misses");
87*08a7078fSAndrii Nakryiko 	ASSERT_GE(get_missed_count(bpf_program__fd(skel->progs.test5)), 1, "test5_recursion_misses");
8859e83c01SJiri Olsa 
8959e83c01SJiri Olsa cleanup:
9059e83c01SJiri Olsa 	missed_kprobe_recursion__destroy(skel);
9159e83c01SJiri Olsa }
9259e83c01SJiri Olsa 
9385981e0fSJiri Olsa /*
9485981e0fSJiri Olsa  * Putting kprobe on bpf_fentry_test1 that calls bpf_printk and invokes
9585981e0fSJiri Olsa  * bpf_trace_printk tracepoint. The bpf_trace_printk tracepoint has test[234]
9685981e0fSJiri Olsa  * programs attached to it.
9785981e0fSJiri Olsa  *
9885981e0fSJiri Olsa  * Because kprobe execution goes through bpf_prog_active check, programs
9985981e0fSJiri Olsa  * attached to the tracepoint will fail the recursion check and increment
10085981e0fSJiri Olsa  * the recursion_misses stats.
10185981e0fSJiri Olsa  */
test_missed_tp_recursion(void)10285981e0fSJiri Olsa static void test_missed_tp_recursion(void)
10385981e0fSJiri Olsa {
10485981e0fSJiri Olsa 	LIBBPF_OPTS(bpf_test_run_opts, topts);
10585981e0fSJiri Olsa 	struct missed_tp_recursion *skel;
10685981e0fSJiri Olsa 	int err, prog_fd;
10785981e0fSJiri Olsa 
10885981e0fSJiri Olsa 	skel = missed_tp_recursion__open_and_load();
10985981e0fSJiri Olsa 	if (!ASSERT_OK_PTR(skel, "missed_tp_recursion__open_and_load"))
11085981e0fSJiri Olsa 		goto cleanup;
11185981e0fSJiri Olsa 
11285981e0fSJiri Olsa 	err = missed_tp_recursion__attach(skel);
11385981e0fSJiri Olsa 	if (!ASSERT_OK(err, "missed_tp_recursion__attach"))
11485981e0fSJiri Olsa 		goto cleanup;
11585981e0fSJiri Olsa 
11685981e0fSJiri Olsa 	prog_fd = bpf_program__fd(skel->progs.trigger);
11785981e0fSJiri Olsa 	err = bpf_prog_test_run_opts(prog_fd, &topts);
11885981e0fSJiri Olsa 	ASSERT_OK(err, "test_run");
11985981e0fSJiri Olsa 	ASSERT_EQ(topts.retval, 0, "test_run");
12085981e0fSJiri Olsa 
12185981e0fSJiri Olsa 	ASSERT_EQ(get_missed_count(bpf_program__fd(skel->progs.test1)), 0, "test1_recursion_misses");
12285981e0fSJiri Olsa 	ASSERT_EQ(get_missed_count(bpf_program__fd(skel->progs.test2)), 1, "test2_recursion_misses");
12385981e0fSJiri Olsa 	ASSERT_EQ(get_missed_count(bpf_program__fd(skel->progs.test3)), 1, "test3_recursion_misses");
12485981e0fSJiri Olsa 	ASSERT_EQ(get_missed_count(bpf_program__fd(skel->progs.test4)), 1, "test4_recursion_misses");
12585981e0fSJiri Olsa 
12685981e0fSJiri Olsa cleanup:
12785981e0fSJiri Olsa 	missed_tp_recursion__destroy(skel);
12885981e0fSJiri Olsa }
12985981e0fSJiri Olsa 
test_missed(void)13001e4ae47SJiri Olsa void test_missed(void)
13101e4ae47SJiri Olsa {
13201e4ae47SJiri Olsa 	if (test__start_subtest("perf_kprobe"))
13301e4ae47SJiri Olsa 		test_missed_perf_kprobe();
13459e83c01SJiri Olsa 	if (test__start_subtest("kprobe_recursion"))
13559e83c01SJiri Olsa 		test_missed_kprobe_recursion();
13685981e0fSJiri Olsa 	if (test__start_subtest("tp_recursion"))
13785981e0fSJiri Olsa 		test_missed_tp_recursion();
13801e4ae47SJiri Olsa }
139