xref: /linux/tools/testing/selftests/bpf/progs/kprobe_multi.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bpf.h>
3 #include <bpf/bpf_helpers.h>
4 #include <bpf/bpf_tracing.h>
5 #include <stdbool.h>
6 
7 char _license[] SEC("license") = "GPL";
8 
9 extern const void bpf_fentry_test1 __ksym;
10 extern const void bpf_fentry_test2 __ksym;
11 extern const void bpf_fentry_test3 __ksym;
12 extern const void bpf_fentry_test4 __ksym;
13 extern const void bpf_fentry_test5 __ksym;
14 extern const void bpf_fentry_test6 __ksym;
15 extern const void bpf_fentry_test7 __ksym;
16 extern const void bpf_fentry_test8 __ksym;
17 
18 int pid = 0;
19 bool test_cookie = false;
20 
21 __u64 kprobe_test1_result = 0;
22 __u64 kprobe_test2_result = 0;
23 __u64 kprobe_test3_result = 0;
24 __u64 kprobe_test4_result = 0;
25 __u64 kprobe_test5_result = 0;
26 __u64 kprobe_test6_result = 0;
27 __u64 kprobe_test7_result = 0;
28 __u64 kprobe_test8_result = 0;
29 
30 __u64 kretprobe_test1_result = 0;
31 __u64 kretprobe_test2_result = 0;
32 __u64 kretprobe_test3_result = 0;
33 __u64 kretprobe_test4_result = 0;
34 __u64 kretprobe_test5_result = 0;
35 __u64 kretprobe_test6_result = 0;
36 __u64 kretprobe_test7_result = 0;
37 __u64 kretprobe_test8_result = 0;
38 
39 extern bool CONFIG_X86_KERNEL_IBT __kconfig __weak;
40 
41 static void kprobe_multi_check(void *ctx, bool is_return)
42 {
43 	if (bpf_get_current_pid_tgid() >> 32 != pid)
44 		return;
45 
46 	__u64 cookie = test_cookie ? bpf_get_attach_cookie(ctx) : 0;
47 	__u64 addr = bpf_get_func_ip(ctx) - (CONFIG_X86_KERNEL_IBT ? 4 : 0);
48 
49 #define SET(__var, __addr, __cookie) ({			\
50 	if (((const void *) addr == __addr) &&		\
51 	     (!test_cookie || (cookie == __cookie)))	\
52 		__var = 1;				\
53 })
54 
55 	if (is_return) {
56 		SET(kretprobe_test1_result, &bpf_fentry_test1, 8);
57 		SET(kretprobe_test2_result, &bpf_fentry_test2, 2);
58 		SET(kretprobe_test3_result, &bpf_fentry_test3, 7);
59 		SET(kretprobe_test4_result, &bpf_fentry_test4, 6);
60 		SET(kretprobe_test5_result, &bpf_fentry_test5, 5);
61 		SET(kretprobe_test6_result, &bpf_fentry_test6, 4);
62 		SET(kretprobe_test7_result, &bpf_fentry_test7, 3);
63 		SET(kretprobe_test8_result, &bpf_fentry_test8, 1);
64 	} else {
65 		SET(kprobe_test1_result, &bpf_fentry_test1, 1);
66 		SET(kprobe_test2_result, &bpf_fentry_test2, 7);
67 		SET(kprobe_test3_result, &bpf_fentry_test3, 2);
68 		SET(kprobe_test4_result, &bpf_fentry_test4, 3);
69 		SET(kprobe_test5_result, &bpf_fentry_test5, 4);
70 		SET(kprobe_test6_result, &bpf_fentry_test6, 5);
71 		SET(kprobe_test7_result, &bpf_fentry_test7, 6);
72 		SET(kprobe_test8_result, &bpf_fentry_test8, 8);
73 	}
74 
75 #undef SET
76 }
77 
78 /*
79  * No tests in here, just to trigger 'bpf_fentry_test*'
80  * through tracing test_run
81  */
82 SEC("fentry/bpf_modify_return_test")
83 int BPF_PROG(trigger)
84 {
85 	return 0;
86 }
87 
88 SEC("kprobe.multi/bpf_fentry_tes??")
89 int test_kprobe(struct pt_regs *ctx)
90 {
91 	kprobe_multi_check(ctx, false);
92 	return 0;
93 }
94 
95 SEC("kretprobe.multi/bpf_fentry_test*")
96 int test_kretprobe(struct pt_regs *ctx)
97 {
98 	kprobe_multi_check(ctx, true);
99 	return 0;
100 }
101 
102 SEC("kprobe.multi")
103 int test_kprobe_manual(struct pt_regs *ctx)
104 {
105 	kprobe_multi_check(ctx, false);
106 	return 0;
107 }
108 
109 SEC("kretprobe.multi")
110 int test_kretprobe_manual(struct pt_regs *ctx)
111 {
112 	kprobe_multi_check(ctx, true);
113 	return 0;
114 }
115