xref: /linux/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
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 <stdbool.h>
6 
7 char _license[] SEC("license") = "GPL";
8 
9 int pid = 0;
10 
11 __u64 test_kprobe_1_result = 0;
12 __u64 test_kprobe_2_result = 0;
13 __u64 test_kprobe_3_result = 0;
14 
15 /*
16  * No tests in here, just to trigger 'bpf_fentry_test*'
17  * through tracing test_run
18  */
19 SEC("fentry/bpf_modify_return_test")
20 int BPF_PROG(trigger)
21 {
22 	return 0;
23 }
24 
25 static int check_cookie(struct pt_regs *ctx, __u64 val, __u64 *result)
26 {
27 	__u64 *cookie;
28 
29 	if (bpf_get_current_pid_tgid() >> 32 != pid)
30 		return 1;
31 
32 	cookie = bpf_session_cookie(ctx);
33 
34 	if (bpf_session_is_return(ctx))
35 		*result = *cookie == val ? val : 0;
36 	else
37 		*cookie = val;
38 	return 0;
39 }
40 
41 SEC("kprobe.session/bpf_fentry_test1")
42 int test_kprobe_1(struct pt_regs *ctx)
43 {
44 	return check_cookie(ctx, 1, &test_kprobe_1_result);
45 }
46 
47 SEC("kprobe.session/bpf_fentry_test1")
48 int test_kprobe_2(struct pt_regs *ctx)
49 {
50 	return check_cookie(ctx, 2, &test_kprobe_2_result);
51 }
52 
53 SEC("kprobe.session/bpf_fentry_test1")
54 int test_kprobe_3(struct pt_regs *ctx)
55 {
56 	return check_cookie(ctx, 3, &test_kprobe_3_result);
57 }
58