xref: /linux/tools/testing/selftests/bpf/progs/struct_ops_assoc.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <vmlinux.h>
4 #include <bpf/bpf_tracing.h>
5 #include "bpf_misc.h"
6 #include "../test_kmods/bpf_testmod.h"
7 #include "../test_kmods/bpf_testmod_kfunc.h"
8 
9 char _license[] SEC("license") = "GPL";
10 
11 int test_pid;
12 
13 /* Programs associated with st_ops_map_a */
14 
15 #define MAP_A_MAGIC 1234
16 int test_err_a;
17 
18 SEC("struct_ops")
19 int BPF_PROG(test_1_a, struct st_ops_args *args)
20 {
21 	return MAP_A_MAGIC;
22 }
23 
24 SEC("tp_btf/sys_enter")
25 int BPF_PROG(sys_enter_prog_a, struct pt_regs *regs, long id)
26 {
27 	struct st_ops_args args = {};
28 	struct task_struct *task;
29 	int ret;
30 
31 	task = bpf_get_current_task_btf();
32 	if (!test_pid || task->pid != test_pid)
33 		return 0;
34 
35 	ret = bpf_kfunc_multi_st_ops_test_1_assoc(&args);
36 	if (ret != MAP_A_MAGIC)
37 		test_err_a++;
38 
39 	return 0;
40 }
41 
42 SEC("syscall")
43 int syscall_prog_a(void *ctx)
44 {
45 	struct st_ops_args args = {};
46 	int ret;
47 
48 	ret = bpf_kfunc_multi_st_ops_test_1_assoc(&args);
49 	if (ret != MAP_A_MAGIC)
50 		test_err_a++;
51 
52 	return 0;
53 }
54 
55 SEC(".struct_ops.link")
56 struct bpf_testmod_multi_st_ops st_ops_map_a = {
57 	.test_1 = (void *)test_1_a,
58 };
59 
60 /* Programs associated with st_ops_map_b */
61 
62 #define MAP_B_MAGIC 5678
63 int test_err_b;
64 
65 SEC("struct_ops")
66 int BPF_PROG(test_1_b, struct st_ops_args *args)
67 {
68 	return MAP_B_MAGIC;
69 }
70 
71 SEC("tp_btf/sys_enter")
72 int BPF_PROG(sys_enter_prog_b, struct pt_regs *regs, long id)
73 {
74 	struct st_ops_args args = {};
75 	struct task_struct *task;
76 	int ret;
77 
78 	task = bpf_get_current_task_btf();
79 	if (!test_pid || task->pid != test_pid)
80 		return 0;
81 
82 	ret = bpf_kfunc_multi_st_ops_test_1_assoc(&args);
83 	if (ret != MAP_B_MAGIC)
84 		test_err_b++;
85 
86 	return 0;
87 }
88 
89 SEC("syscall")
90 int syscall_prog_b(void *ctx)
91 {
92 	struct st_ops_args args = {};
93 	int ret;
94 
95 	ret = bpf_kfunc_multi_st_ops_test_1_assoc(&args);
96 	if (ret != MAP_B_MAGIC)
97 		test_err_b++;
98 
99 	return 0;
100 }
101 
102 SEC(".struct_ops.link")
103 struct bpf_testmod_multi_st_ops st_ops_map_b = {
104 	.test_1 = (void *)test_1_b,
105 };
106