xref: /linux/tools/testing/selftests/bpf/progs/struct_ops_assoc_reuse.c (revision f17b474e36647c23801ef8fdaf2255ab66dd2973)
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 #define MAP_A_MAGIC 1234
12 int test_err_a;
13 int recur;
14 
15 /*
16  * test_1_a is reused. The kfunc should not be able to get the associated
17  * struct_ops and call test_1 recursively as it is ambiguous.
18  */
19 SEC("struct_ops")
20 int BPF_PROG(test_1_a, struct st_ops_args *args)
21 {
22 	int ret;
23 
24 	if (!recur) {
25 		recur++;
26 		ret = bpf_kfunc_multi_st_ops_test_1_assoc(args);
27 		if (ret != -1)
28 			test_err_a++;
29 		recur--;
30 	}
31 
32 	return MAP_A_MAGIC;
33 }
34 
35 /* Programs associated with st_ops_map_a */
36 
37 SEC("syscall")
38 int syscall_prog_a(void *ctx)
39 {
40 	struct st_ops_args args = {};
41 	int ret;
42 
43 	ret = bpf_kfunc_multi_st_ops_test_1_assoc(&args);
44 	if (ret != MAP_A_MAGIC)
45 		test_err_a++;
46 
47 	return 0;
48 }
49 
50 SEC(".struct_ops.link")
51 struct bpf_testmod_multi_st_ops st_ops_map_a = {
52 	.test_1 = (void *)test_1_a,
53 };
54 
55 /* Programs associated with st_ops_map_b */
56 
57 int test_err_b;
58 
59 SEC("syscall")
60 int syscall_prog_b(void *ctx)
61 {
62 	struct st_ops_args args = {};
63 	int ret;
64 
65 	ret = bpf_kfunc_multi_st_ops_test_1_assoc(&args);
66 	if (ret != MAP_A_MAGIC)
67 		test_err_b++;
68 
69 	return 0;
70 }
71 
72 SEC(".struct_ops.link")
73 struct bpf_testmod_multi_st_ops st_ops_map_b = {
74 	.test_1 = (void *)test_1_a,
75 };
76