xref: /linux/tools/testing/selftests/bpf/progs/kprobe_multi_session.c (revision 001821b0e79716c4e17c71d8e053a23599a7a508)
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 #include "bpf_kfuncs.h"
7 
8 #define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0]))
9 
10 char _license[] SEC("license") = "GPL";
11 
12 extern const void bpf_fentry_test1 __ksym;
13 extern const void bpf_fentry_test2 __ksym;
14 extern const void bpf_fentry_test3 __ksym;
15 extern const void bpf_fentry_test4 __ksym;
16 extern const void bpf_fentry_test5 __ksym;
17 extern const void bpf_fentry_test6 __ksym;
18 extern const void bpf_fentry_test7 __ksym;
19 extern const void bpf_fentry_test8 __ksym;
20 
21 int pid = 0;
22 
23 __u64 kprobe_session_result[8];
24 
25 static int session_check(void *ctx)
26 {
27 	unsigned int i;
28 	__u64 addr;
29 	const void *kfuncs[] = {
30 		&bpf_fentry_test1,
31 		&bpf_fentry_test2,
32 		&bpf_fentry_test3,
33 		&bpf_fentry_test4,
34 		&bpf_fentry_test5,
35 		&bpf_fentry_test6,
36 		&bpf_fentry_test7,
37 		&bpf_fentry_test8,
38 	};
39 
40 	if (bpf_get_current_pid_tgid() >> 32 != pid)
41 		return 1;
42 
43 	addr = bpf_get_func_ip(ctx);
44 
45 	for (i = 0; i < ARRAY_SIZE(kfuncs); i++) {
46 		if (kfuncs[i] == (void *) addr) {
47 			kprobe_session_result[i]++;
48 			break;
49 		}
50 	}
51 
52 	/*
53 	 * Force probes for function bpf_fentry_test[5-8] not to
54 	 * install and execute the return probe
55 	 */
56 	if (((const void *) addr == &bpf_fentry_test5) ||
57 	    ((const void *) addr == &bpf_fentry_test6) ||
58 	    ((const void *) addr == &bpf_fentry_test7) ||
59 	    ((const void *) addr == &bpf_fentry_test8))
60 		return 1;
61 
62 	return 0;
63 }
64 
65 /*
66  * No tests in here, just to trigger 'bpf_fentry_test*'
67  * through tracing test_run
68  */
69 SEC("fentry/bpf_modify_return_test")
70 int BPF_PROG(trigger)
71 {
72 	return 0;
73 }
74 
75 SEC("kprobe.session/bpf_fentry_test*")
76 int test_kprobe(struct pt_regs *ctx)
77 {
78 	return session_check(ctx);
79 }
80