xref: /linux/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c (revision 5a8cd539ac19f7a68e68e1d25ef9ca2ff55b8500)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include <linux/bpf.h>
4 #include <linux/btf.h>
5 #include <linux/btf_ids.h>
6 #include <linux/delay.h>
7 #include <linux/error-injection.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/percpu-defs.h>
11 #include <linux/sysfs.h>
12 #include <linux/tracepoint.h>
13 #include <linux/net.h>
14 #include <linux/socket.h>
15 #include <linux/nsproxy.h>
16 #include <linux/inet.h>
17 #include <linux/in.h>
18 #include <linux/in6.h>
19 #include <linux/un.h>
20 #include <linux/filter.h>
21 #include <linux/rcupdate_trace.h>
22 #include <net/sock.h>
23 #include <linux/namei.h>
24 #include "bpf_testmod.h"
25 #include "bpf_testmod_kfunc.h"
26 
27 #define CREATE_TRACE_POINTS
28 #include "bpf_testmod-events.h"
29 
30 #define CONNECT_TIMEOUT_SEC 1
31 
32 typedef int (*func_proto_typedef)(long);
33 typedef int (*func_proto_typedef_nested1)(func_proto_typedef);
34 typedef int (*func_proto_typedef_nested2)(func_proto_typedef_nested1);
35 
36 DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
37 long bpf_testmod_test_struct_arg_result;
38 static DEFINE_MUTEX(sock_lock);
39 static struct socket *sock;
40 
41 struct bpf_testmod_struct_arg_1 {
42 	int a;
43 };
44 struct bpf_testmod_struct_arg_2 {
45 	long a;
46 	long b;
47 };
48 
49 struct bpf_testmod_struct_arg_3 {
50 	int a;
51 	int b[];
52 };
53 
54 struct bpf_testmod_struct_arg_4 {
55 	u64 a;
56 	int b;
57 };
58 
59 struct bpf_testmod_struct_arg_5 {
60 	char a;
61 	short b;
62 	int c;
63 	long d;
64 };
65 
66 union bpf_testmod_union_arg_1 {
67 	char a;
68 	short b;
69 	struct bpf_testmod_struct_arg_1 arg;
70 };
71 
72 union bpf_testmod_union_arg_2 {
73 	int a;
74 	long b;
75 	struct bpf_testmod_struct_arg_2 arg;
76 };
77 
78 __bpf_hook_start();
79 
80 noinline int
bpf_testmod_test_struct_arg_1(struct bpf_testmod_struct_arg_2 a,int b,int c)81 bpf_testmod_test_struct_arg_1(struct bpf_testmod_struct_arg_2 a, int b, int c) {
82 	bpf_testmod_test_struct_arg_result = a.a + a.b  + b + c;
83 	return bpf_testmod_test_struct_arg_result;
84 }
85 
86 noinline int
bpf_testmod_test_struct_arg_2(int a,struct bpf_testmod_struct_arg_2 b,int c)87 bpf_testmod_test_struct_arg_2(int a, struct bpf_testmod_struct_arg_2 b, int c) {
88 	bpf_testmod_test_struct_arg_result = a + b.a + b.b + c;
89 	return bpf_testmod_test_struct_arg_result;
90 }
91 
92 noinline int
bpf_testmod_test_struct_arg_3(int a,int b,struct bpf_testmod_struct_arg_2 c)93 bpf_testmod_test_struct_arg_3(int a, int b, struct bpf_testmod_struct_arg_2 c) {
94 	bpf_testmod_test_struct_arg_result = a + b + c.a + c.b;
95 	return bpf_testmod_test_struct_arg_result;
96 }
97 
98 noinline int
bpf_testmod_test_struct_arg_4(struct bpf_testmod_struct_arg_1 a,int b,int c,int d,struct bpf_testmod_struct_arg_2 e)99 bpf_testmod_test_struct_arg_4(struct bpf_testmod_struct_arg_1 a, int b,
100 			      int c, int d, struct bpf_testmod_struct_arg_2 e) {
101 	bpf_testmod_test_struct_arg_result = a.a + b + c + d + e.a + e.b;
102 	return bpf_testmod_test_struct_arg_result;
103 }
104 
105 noinline int
bpf_testmod_test_struct_arg_5(void)106 bpf_testmod_test_struct_arg_5(void) {
107 	bpf_testmod_test_struct_arg_result = 1;
108 	return bpf_testmod_test_struct_arg_result;
109 }
110 
111 noinline int
bpf_testmod_test_struct_arg_6(struct bpf_testmod_struct_arg_3 * a)112 bpf_testmod_test_struct_arg_6(struct bpf_testmod_struct_arg_3 *a) {
113 	bpf_testmod_test_struct_arg_result = a->b[0];
114 	return bpf_testmod_test_struct_arg_result;
115 }
116 
117 noinline int
bpf_testmod_test_struct_arg_7(u64 a,void * b,short c,int d,void * e,struct bpf_testmod_struct_arg_4 f)118 bpf_testmod_test_struct_arg_7(u64 a, void *b, short c, int d, void *e,
119 			      struct bpf_testmod_struct_arg_4 f)
120 {
121 	bpf_testmod_test_struct_arg_result = a + (long)b + c + d +
122 		(long)e + f.a + f.b;
123 	return bpf_testmod_test_struct_arg_result;
124 }
125 
126 noinline int
bpf_testmod_test_struct_arg_8(u64 a,void * b,short c,int d,void * e,struct bpf_testmod_struct_arg_4 f,int g)127 bpf_testmod_test_struct_arg_8(u64 a, void *b, short c, int d, void *e,
128 			      struct bpf_testmod_struct_arg_4 f, int g)
129 {
130 	bpf_testmod_test_struct_arg_result = a + (long)b + c + d +
131 		(long)e + f.a + f.b + g;
132 	return bpf_testmod_test_struct_arg_result;
133 }
134 
135 noinline int
bpf_testmod_test_struct_arg_9(u64 a,void * b,short c,int d,void * e,char f,short g,struct bpf_testmod_struct_arg_5 h,long i)136 bpf_testmod_test_struct_arg_9(u64 a, void *b, short c, int d, void *e, char f,
137 			      short g, struct bpf_testmod_struct_arg_5 h, long i)
138 {
139 	bpf_testmod_test_struct_arg_result = a + (long)b + c + d + (long)e +
140 		f + g + h.a + h.b + h.c + h.d + i;
141 	return bpf_testmod_test_struct_arg_result;
142 }
143 
144 noinline int
bpf_testmod_test_union_arg_1(union bpf_testmod_union_arg_1 a,int b,int c)145 bpf_testmod_test_union_arg_1(union bpf_testmod_union_arg_1 a, int b, int c)
146 {
147 	bpf_testmod_test_struct_arg_result = a.arg.a + b + c;
148 	return bpf_testmod_test_struct_arg_result;
149 }
150 
151 noinline int
bpf_testmod_test_union_arg_2(int a,union bpf_testmod_union_arg_2 b)152 bpf_testmod_test_union_arg_2(int a, union bpf_testmod_union_arg_2 b)
153 {
154 	bpf_testmod_test_struct_arg_result = a + b.arg.a + b.arg.b;
155 	return bpf_testmod_test_struct_arg_result;
156 }
157 
158 noinline int
bpf_testmod_test_arg_ptr_to_struct(struct bpf_testmod_struct_arg_1 * a)159 bpf_testmod_test_arg_ptr_to_struct(struct bpf_testmod_struct_arg_1 *a) {
160 	bpf_testmod_test_struct_arg_result = a->a;
161 	return bpf_testmod_test_struct_arg_result;
162 }
163 
164 #ifdef __SIZEOF_INT128__
165 noinline __int128
bpf_testmod_test_int128_ret(int a)166 bpf_testmod_test_int128_ret(int a)
167 {
168 	bpf_testmod_test_struct_arg_result = a;
169 	return (__int128)a;
170 }
171 
172 /*
173  * The __int128 'a' is the first argument on purpose. On arm64 a 16-byte
174  * argument must start in an even-numbered register pair, so placing it
175  * after a single-register scalar would leave a padding register (x1)
176  * unused. pahole maps parameters to registers positionally and would then
177  * see the following argument in an "unexpected" register and skip BTF
178  * encoding of the whole function, making it unattachable. Keeping the
179  * __int128 first (x0:x1) avoids the padding while still exercising the
180  * trampoline packing of a 128-bit argument together with the trailing
181  * int and long arguments.
182  */
183 noinline long
bpf_testmod_test_int128_arg(__int128 a,int b,long c)184 bpf_testmod_test_int128_arg(__int128 a, int b, long c)
185 {
186 	bpf_testmod_test_struct_arg_result = (long)a + b + c;
187 	return bpf_testmod_test_struct_arg_result;
188 }
189 #endif
190 
bpf_testmod_looooooooooooooooooooooooooooooong_name(void)191 __weak noinline void bpf_testmod_looooooooooooooooooooooooooooooong_name(void)
192 {
193 }
194 
195 __bpf_kfunc void
bpf_testmod_test_mod_kfunc(int i)196 bpf_testmod_test_mod_kfunc(int i)
197 {
198 	*(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
199 }
200 
bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq * it,s64 value,int cnt)201 __bpf_kfunc int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt)
202 {
203 	it->cnt = cnt;
204 
205 	if (cnt < 0)
206 		return -EINVAL;
207 
208 	it->value = value;
209 
210 	return 0;
211 }
212 
bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq * it)213 __bpf_kfunc s64 *bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq* it)
214 {
215 	if (it->cnt <= 0)
216 		return NULL;
217 
218 	it->cnt--;
219 
220 	return &it->value;
221 }
222 
bpf_iter_testmod_seq_value(int val,struct bpf_iter_testmod_seq * it__iter)223 __bpf_kfunc s64 bpf_iter_testmod_seq_value(int val, struct bpf_iter_testmod_seq* it__iter)
224 {
225 	if (it__iter->cnt < 0)
226 		return 0;
227 
228 	return val + it__iter->value;
229 }
230 
bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq * it)231 __bpf_kfunc void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it)
232 {
233 	it->cnt = 0;
234 }
235 
bpf_kfunc_common_test(void)236 __bpf_kfunc void bpf_kfunc_common_test(void)
237 {
238 }
239 
bpf_kfunc_arena_arg_test(u64 * val__arena)240 __bpf_kfunc u64 bpf_kfunc_arena_arg_test(u64 *val__arena)
241 {
242 	u64 old;
243 
244 	old = *val__arena;
245 	*val__arena = old + 1;
246 	return old;
247 }
248 
bpf_kfunc_arena_cap_test(u64 * val__arena)249 __bpf_kfunc u64 bpf_kfunc_arena_cap_test(u64 *val__arena)
250 {
251 	return (u64)val__arena;
252 }
253 
bpf_kfunc_arena_cap_nullable_test(u64 * val__arena__nullable)254 __bpf_kfunc u64 bpf_kfunc_arena_cap_nullable_test(u64 *val__arena__nullable)
255 {
256 	return (u64)val__arena__nullable;
257 }
258 
bpf_kfunc_arena_args5_test(u64 * a__arena,u64 * b__arena,u64 * c__arena,u64 * d__arena,u64 * e__arena__nullable)259 __bpf_kfunc u64 bpf_kfunc_arena_args5_test(u64 *a__arena, u64 *b__arena,
260 					   u64 *c__arena, u64 *d__arena,
261 					   u64 *e__arena__nullable)
262 {
263 	return *a__arena + *b__arena + *c__arena + *d__arena +
264 	       (e__arena__nullable ? *e__arena__nullable : 0);
265 }
266 
bpf_kfunc_arena_stack_arg_test(u64 a,u64 b,u64 c,u64 d,u64 e,u64 * f__arena)267 __bpf_kfunc u64 bpf_kfunc_arena_stack_arg_test(u64 a, u64 b, u64 c, u64 d, u64 e,
268 						u64 *f__arena)
269 {
270 	return a + b + c + d + e + *f__arena;
271 }
272 
bpf_kfunc_arena_mixed_test(u64 * a__arena,u64 * b__arena__nullable)273 __bpf_kfunc u64 bpf_kfunc_arena_mixed_test(u64 *a__arena, u64 *b__arena__nullable)
274 {
275 	return *a__arena + (b__arena__nullable ? *b__arena__nullable : 0);
276 }
277 
bpf_kfunc_dynptr_test(struct bpf_dynptr * ptr,struct bpf_dynptr * ptr__nullable)278 __bpf_kfunc void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr,
279 				       struct bpf_dynptr *ptr__nullable)
280 {
281 }
282 
bpf_kfunc_nested_acquire_nonzero_offset_test(struct sk_buff_head * ptr)283 __bpf_kfunc struct sk_buff *bpf_kfunc_nested_acquire_nonzero_offset_test(struct sk_buff_head *ptr)
284 {
285 	return NULL;
286 }
287 
bpf_kfunc_nested_acquire_zero_offset_test(struct sock_common * ptr)288 __bpf_kfunc struct sk_buff *bpf_kfunc_nested_acquire_zero_offset_test(struct sock_common *ptr)
289 {
290 	return NULL;
291 }
292 
bpf_kfunc_nested_release_test(struct sk_buff * ptr)293 __bpf_kfunc void bpf_kfunc_nested_release_test(struct sk_buff *ptr)
294 {
295 }
296 
bpf_kfunc_trusted_vma_test(struct vm_area_struct * ptr)297 __bpf_kfunc void bpf_kfunc_trusted_vma_test(struct vm_area_struct *ptr)
298 {
299 }
300 
bpf_kfunc_trusted_task_test(struct task_struct * ptr)301 __bpf_kfunc void bpf_kfunc_trusted_task_test(struct task_struct *ptr)
302 {
303 }
304 
bpf_kfunc_trusted_num_test(int * ptr)305 __bpf_kfunc void bpf_kfunc_trusted_num_test(int *ptr)
306 {
307 }
308 
bpf_kfunc_rcu_task_test(struct task_struct * ptr)309 __bpf_kfunc void bpf_kfunc_rcu_task_test(struct task_struct *ptr)
310 {
311 }
312 
bpf_kfunc_ret_rcu_test(void)313 __bpf_kfunc struct task_struct *bpf_kfunc_ret_rcu_test(void)
314 {
315 	return NULL;
316 }
317 
bpf_kfunc_ret_rcu_test_nostruct(int rdonly_buf_size)318 __bpf_kfunc int *bpf_kfunc_ret_rcu_test_nostruct(int rdonly_buf_size)
319 {
320 	return NULL;
321 }
322 
323 static struct prog_test_member trusted_ptr;
324 
bpf_kfunc_get_default_trusted_ptr_test(void)325 __bpf_kfunc struct prog_test_member *bpf_kfunc_get_default_trusted_ptr_test(void)
326 {
327 	return &trusted_ptr;
328 }
329 
bpf_kfunc_put_default_trusted_ptr_test(struct prog_test_member * trusted_ptr)330 __bpf_kfunc void bpf_kfunc_put_default_trusted_ptr_test(struct prog_test_member *trusted_ptr)
331 {
332 	/*
333 	 * This BPF kfunc doesn't actually have any put/KF_ACQUIRE
334 	 * semantics. We're simply wanting to simulate a BPF kfunc that takes a
335 	 * struct prog_test_member pointer as an argument.
336 	 */
337 }
338 
339 __bpf_kfunc struct bpf_testmod_ctx *
bpf_testmod_ctx_create(int * err)340 bpf_testmod_ctx_create(int *err)
341 {
342 	struct bpf_testmod_ctx *ctx;
343 
344 	ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
345 	if (!ctx) {
346 		*err = -ENOMEM;
347 		return NULL;
348 	}
349 	refcount_set(&ctx->usage, 1);
350 
351 	return ctx;
352 }
353 
testmod_free_cb(struct rcu_head * head)354 static void testmod_free_cb(struct rcu_head *head)
355 {
356 	struct bpf_testmod_ctx *ctx;
357 
358 	ctx = container_of(head, struct bpf_testmod_ctx, rcu);
359 	kfree(ctx);
360 }
361 
bpf_testmod_ctx_release(struct bpf_testmod_ctx * ctx)362 __bpf_kfunc void bpf_testmod_ctx_release(struct bpf_testmod_ctx *ctx)
363 {
364 	if (!ctx)
365 		return;
366 	if (refcount_dec_and_test(&ctx->usage))
367 		call_rcu(&ctx->rcu, testmod_free_cb);
368 }
369 
bpf_testmod_ctx_release_dtor(void * ctx)370 __bpf_kfunc void bpf_testmod_ctx_release_dtor(void *ctx)
371 {
372 	bpf_testmod_ctx_release(ctx);
373 }
374 CFI_NOSEAL(bpf_testmod_ctx_release_dtor);
375 
376 static struct bpf_testmod_ops3 *st_ops3;
377 
bpf_testmod_test_3(void)378 static int bpf_testmod_test_3(void)
379 {
380 	return 0;
381 }
382 
bpf_testmod_test_4(void)383 static int bpf_testmod_test_4(void)
384 {
385 	return 0;
386 }
387 
bpf_testmod_ops3__test_arena(u64 * ptr__arena)388 static int bpf_testmod_ops3__test_arena(u64 *ptr__arena)
389 {
390 	return 0;
391 }
392 
bpf_testmod_ops3__test_arena_nullable(u64 * ptr__arena__nullable)393 static int bpf_testmod_ops3__test_arena_nullable(u64 *ptr__arena__nullable)
394 {
395 	return 0;
396 }
397 
bpf_testmod_ops3__test_arena_stack(u64 a,u64 b,u64 c,u64 d,u64 e,u64 f,u64 g,u64 h,u64 * ptr__arena)398 static int bpf_testmod_ops3__test_arena_stack(u64 a, u64 b, u64 c, u64 d,
399 					      u64 e, u64 f, u64 g, u64 h,
400 					      u64 *ptr__arena)
401 {
402 	return 0;
403 }
404 
bpf_testmod_ops3__test_arena_multislot(struct bpf_testmod_arena_pair p,u64 * ptr__arena)405 static int bpf_testmod_ops3__test_arena_multislot(struct bpf_testmod_arena_pair p,
406 						  u64 *ptr__arena)
407 {
408 	return 0;
409 }
410 
411 static struct bpf_testmod_ops3 __bpf_testmod_ops3 = {
412 	.test_1 = bpf_testmod_test_3,
413 	.test_2 = bpf_testmod_test_4,
414 	.test_arena = bpf_testmod_ops3__test_arena,
415 	.test_arena_nullable = bpf_testmod_ops3__test_arena_nullable,
416 	.test_arena_stack = bpf_testmod_ops3__test_arena_stack,
417 	.test_arena_multislot = bpf_testmod_ops3__test_arena_multislot,
418 };
419 
bpf_testmod_test_struct_ops3(void)420 static void bpf_testmod_test_struct_ops3(void)
421 {
422 	if (st_ops3)
423 		st_ops3->test_1();
424 }
425 
bpf_testmod_ops3_call_test_1(void)426 __bpf_kfunc void bpf_testmod_ops3_call_test_1(void)
427 {
428 	st_ops3->test_1();
429 }
430 
bpf_testmod_ops3_call_test_2(void)431 __bpf_kfunc void bpf_testmod_ops3_call_test_2(void)
432 {
433 	st_ops3->test_2();
434 }
435 
bpf_testmod_ops3_call_test_arena(u64 * ptr__arena)436 __bpf_kfunc int bpf_testmod_ops3_call_test_arena(u64 *ptr__arena)
437 {
438 	return st_ops3->test_arena(ptr__arena);
439 }
440 
bpf_testmod_ops3_call_test_arena_nullable(u64 * ptr__arena__nullable)441 __bpf_kfunc int bpf_testmod_ops3_call_test_arena_nullable(u64 *ptr__arena__nullable)
442 {
443 	return st_ops3->test_arena_nullable(ptr__arena__nullable);
444 }
445 
bpf_testmod_ops3_call_test_arena_stack(u64 * ptr__arena)446 __bpf_kfunc int bpf_testmod_ops3_call_test_arena_stack(u64 *ptr__arena)
447 {
448 	return st_ops3->test_arena_stack(1, 2, 3, 4, 5, 6, 7, 8, ptr__arena);
449 }
450 
bpf_testmod_ops3_call_test_arena_multislot(u64 * ptr__arena)451 __bpf_kfunc int bpf_testmod_ops3_call_test_arena_multislot(u64 *ptr__arena)
452 {
453 	struct bpf_testmod_arena_pair p = { .a = 11, .b = 22 };
454 
455 	return st_ops3->test_arena_multislot(p, ptr__arena);
456 }
457 
458 struct bpf_testmod_btf_type_tag_1 {
459 	int a;
460 };
461 
462 struct bpf_testmod_btf_type_tag_2 {
463 	struct bpf_testmod_btf_type_tag_1 __user *p;
464 };
465 
466 struct bpf_testmod_btf_type_tag_3 {
467 	struct bpf_testmod_btf_type_tag_1 __percpu *p;
468 };
469 
470 noinline int
bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user * arg)471 bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user *arg) {
472 	BTF_TYPE_EMIT(func_proto_typedef);
473 	BTF_TYPE_EMIT(func_proto_typedef_nested1);
474 	BTF_TYPE_EMIT(func_proto_typedef_nested2);
475 	return arg->a;
476 }
477 
478 noinline int
bpf_testmod_test_btf_type_tag_user_2(struct bpf_testmod_btf_type_tag_2 * arg)479 bpf_testmod_test_btf_type_tag_user_2(struct bpf_testmod_btf_type_tag_2 *arg) {
480 	return arg->p->a;
481 }
482 
483 noinline int
bpf_testmod_test_btf_type_tag_percpu_1(struct bpf_testmod_btf_type_tag_1 __percpu * arg)484 bpf_testmod_test_btf_type_tag_percpu_1(struct bpf_testmod_btf_type_tag_1 __percpu *arg) {
485 	return arg->a;
486 }
487 
488 noinline int
bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 * arg)489 bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
490 	return arg->p->a;
491 }
492 
bpf_testmod_loop_test(int n)493 noinline int bpf_testmod_loop_test(int n)
494 {
495 	/* Make sum volatile, so smart compilers, such as clang, will not
496 	 * optimize the code by removing the loop.
497 	 */
498 	volatile int sum = 0;
499 	int i;
500 
501 	/* the primary goal of this test is to test LBR. Create a lot of
502 	 * branches in the function, so we can catch it easily.
503 	 */
504 	for (i = 0; i < n; i++)
505 		sum += i;
506 	return sum;
507 }
508 
bpf_testmod_return_ptr(int arg)509 __weak noinline struct file *bpf_testmod_return_ptr(int arg)
510 {
511 	static struct file f = {};
512 
513 	switch (arg) {
514 	case 1: return (void *)EINVAL;		/* user addr */
515 	case 2: return (void *)0xcafe4a11;	/* user addr */
516 	case 3: return (void *)-EINVAL;		/* canonical, but invalid */
517 	case 4: return (void *)(1ull << 60);	/* non-canonical and invalid */
518 	case 5: return (void *)~(1ull << 30);	/* trigger extable */
519 	case 6: return &f;			/* valid addr */
520 	case 7: return (void *)((long)&f | 1);	/* kernel tricks */
521 #ifdef CONFIG_X86_64
522 	case 8: return (void *)VSYSCALL_ADDR;   /* vsyscall page address */
523 #endif
524 	default: return NULL;
525 	}
526 }
527 
bpf_testmod_fentry_test1(int a)528 noinline int bpf_testmod_fentry_test1(int a)
529 {
530 	trace_bpf_testmod_fentry_test1_tp(a);
531 
532 	return a + 1;
533 }
534 
bpf_testmod_fentry_test2(int a,u64 b)535 noinline int bpf_testmod_fentry_test2(int a, u64 b)
536 {
537 	trace_bpf_testmod_fentry_test2_tp(a, b);
538 
539 	return a + b;
540 }
541 
bpf_testmod_fentry_test3(char a,int b,u64 c)542 noinline int bpf_testmod_fentry_test3(char a, int b, u64 c)
543 {
544 	return a + b + c;
545 }
546 
bpf_testmod_fentry_test7(u64 a,void * b,short c,int d,void * e,char f,int g)547 noinline int bpf_testmod_fentry_test7(u64 a, void *b, short c, int d,
548 				      void *e, char f, int g)
549 {
550 	return a + (long)b + c + d + (long)e + f + g;
551 }
552 
bpf_testmod_fentry_test11(u64 a,void * b,short c,int d,void * e,char f,int g,unsigned int h,long i,__u64 j,unsigned long k)553 noinline int bpf_testmod_fentry_test11(u64 a, void *b, short c, int d,
554 				       void *e, char f, int g,
555 				       unsigned int h, long i, __u64 j,
556 				       unsigned long k)
557 {
558 	return a + (long)b + c + d + (long)e + f + g + h + i + j + k;
559 }
560 
bpf_testmod_stacktrace_test(void)561 noinline void bpf_testmod_stacktrace_test(void)
562 {
563 	/* used for stacktrace test as attach function */
564 	asm volatile ("");
565 }
566 
bpf_testmod_stacktrace_test_3(void)567 noinline void bpf_testmod_stacktrace_test_3(void)
568 {
569 	bpf_testmod_stacktrace_test();
570 	asm volatile ("");
571 }
572 
bpf_testmod_stacktrace_test_2(void)573 noinline void bpf_testmod_stacktrace_test_2(void)
574 {
575 	bpf_testmod_stacktrace_test_3();
576 	asm volatile ("");
577 }
578 
bpf_testmod_stacktrace_test_1(void)579 noinline void bpf_testmod_stacktrace_test_1(void)
580 {
581 	bpf_testmod_stacktrace_test_2();
582 	asm volatile ("");
583 }
584 
585 int bpf_testmod_fentry_ok;
586 
bpf_testmod_trampoline_count_test(void)587 noinline int bpf_testmod_trampoline_count_test(void)
588 {
589 	return 0;
590 }
591 
592 noinline ssize_t
bpf_testmod_test_read(struct file * file,struct kobject * kobj,const struct bin_attribute * bin_attr,char * buf,loff_t off,size_t len)593 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
594 		      const struct bin_attribute *bin_attr,
595 		      char *buf, loff_t off, size_t len)
596 {
597 	struct bpf_testmod_test_read_ctx ctx = {
598 		.buf = buf,
599 		.off = off,
600 		.len = len,
601 	};
602 	struct bpf_testmod_struct_arg_1 struct_arg1 = {10}, struct_arg1_2 = {-1};
603 	struct bpf_testmod_struct_arg_2 struct_arg2 = {2, 3};
604 	struct bpf_testmod_struct_arg_3 *struct_arg3;
605 	struct bpf_testmod_struct_arg_4 struct_arg4 = {21, 22};
606 	struct bpf_testmod_struct_arg_5 struct_arg5 = {23, 24, 25, 26};
607 	union bpf_testmod_union_arg_1 union_arg1 = { .arg = {1} };
608 	union bpf_testmod_union_arg_2 union_arg2 = { .arg = {2, 3} };
609 	int i = 1;
610 
611 	while (bpf_testmod_return_ptr(i))
612 		i++;
613 
614 	(void)bpf_testmod_test_struct_arg_1(struct_arg2, 1, 4);
615 	(void)bpf_testmod_test_struct_arg_2(1, struct_arg2, 4);
616 	(void)bpf_testmod_test_struct_arg_3(1, 4, struct_arg2);
617 	(void)bpf_testmod_test_struct_arg_4(struct_arg1, 1, 2, 3, struct_arg2);
618 	(void)bpf_testmod_test_struct_arg_5();
619 	(void)bpf_testmod_test_struct_arg_7(16, (void *)17, 18, 19,
620 					    (void *)20, struct_arg4);
621 	(void)bpf_testmod_test_struct_arg_8(16, (void *)17, 18, 19,
622 					    (void *)20, struct_arg4, 23);
623 	(void)bpf_testmod_test_struct_arg_9(16, (void *)17, 18, 19, (void *)20,
624 					    21, 22, struct_arg5, 27);
625 
626 	(void)bpf_testmod_test_union_arg_1(union_arg1, 4, 5);
627 	(void)bpf_testmod_test_union_arg_2(6, union_arg2);
628 
629 	(void)bpf_testmod_test_arg_ptr_to_struct(&struct_arg1_2);
630 
631 #ifdef __SIZEOF_INT128__
632 	(void)bpf_testmod_test_int128_ret(i);
633 	(void)bpf_testmod_test_int128_arg((__int128)1, 2, 3);
634 #endif
635 
636 	(void)trace_bpf_testmod_test_raw_tp_null_tp(NULL);
637 
638 	bpf_testmod_test_struct_ops3();
639 
640 	struct_arg3 = kmalloc((sizeof(struct bpf_testmod_struct_arg_3) +
641 				sizeof(int)), GFP_KERNEL);
642 	if (struct_arg3 != NULL) {
643 		struct_arg3->b[0] = 1;
644 		(void)bpf_testmod_test_struct_arg_6(struct_arg3);
645 		kfree(struct_arg3);
646 	}
647 
648 	/* This is always true. Use the check to make sure the compiler
649 	 * doesn't remove bpf_testmod_loop_test.
650 	 */
651 	if (bpf_testmod_loop_test(101) > 100)
652 		trace_bpf_testmod_test_read(current, &ctx);
653 
654 	trace_bpf_testmod_test_nullable_bare_tp(NULL);
655 
656 	/* Magic number to enable writable tp */
657 	if (len == 64) {
658 		struct bpf_testmod_test_writable_ctx writable = {
659 			.val = 1024,
660 		};
661 		trace_bpf_testmod_test_writable_bare_tp(&writable);
662 		if (writable.early_ret)
663 			return snprintf(buf, len, "%d\n", writable.val);
664 	}
665 
666 	if (bpf_testmod_fentry_test1(1) != 2 ||
667 	    bpf_testmod_fentry_test2(2, 3) != 5 ||
668 	    bpf_testmod_fentry_test3(4, 5, 6) != 15 ||
669 	    bpf_testmod_fentry_test7(16, (void *)17, 18, 19, (void *)20,
670 			21, 22) != 133 ||
671 	    bpf_testmod_fentry_test11(16, (void *)17, 18, 19, (void *)20,
672 			21, 22, 23, 24, 25, 26) != 231)
673 		goto out;
674 
675 	bpf_testmod_trampoline_count_test();
676 
677 	bpf_testmod_stacktrace_test_1();
678 
679 	bpf_testmod_fentry_ok = 1;
680 out:
681 	return -EIO; /* always fail */
682 }
683 EXPORT_SYMBOL(bpf_testmod_test_read);
684 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
685 
686 noinline ssize_t
bpf_testmod_test_write(struct file * file,struct kobject * kobj,const struct bin_attribute * bin_attr,char * buf,loff_t off,size_t len)687 bpf_testmod_test_write(struct file *file, struct kobject *kobj,
688 		      const struct bin_attribute *bin_attr,
689 		      char *buf, loff_t off, size_t len)
690 {
691 	struct bpf_testmod_test_write_ctx ctx = {
692 		.buf = buf,
693 		.off = off,
694 		.len = len,
695 	};
696 
697 	trace_bpf_testmod_test_write_bare_tp(current, &ctx);
698 
699 	return -EIO; /* always fail */
700 }
701 EXPORT_SYMBOL(bpf_testmod_test_write);
702 ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
703 
bpf_fentry_shadow_test(int a)704 noinline int bpf_fentry_shadow_test(int a)
705 {
706 	return a + 2;
707 }
708 EXPORT_SYMBOL_GPL(bpf_fentry_shadow_test);
709 
710 __bpf_hook_end();
711 
712 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
713 	.attr = { .name = "bpf_testmod", .mode = 0666, },
714 	.read = bpf_testmod_test_read,
715 	.write = bpf_testmod_test_write,
716 };
717 
718 /* bpf_testmod_uprobe sysfs attribute is so far enabled for x86_64 only,
719  * please see test_uretprobe_regs_change test
720  */
721 #ifdef __x86_64__
722 
723 static int
uprobe_handler(struct uprobe_consumer * self,struct pt_regs * regs,__u64 * data)724 uprobe_handler(struct uprobe_consumer *self, struct pt_regs *regs, __u64 *data)
725 {
726 	regs->cx = 0x87654321feebdaed;
727 	return 0;
728 }
729 
730 static int
uprobe_ret_handler(struct uprobe_consumer * self,unsigned long func,struct pt_regs * regs,__u64 * data)731 uprobe_ret_handler(struct uprobe_consumer *self, unsigned long func,
732 		   struct pt_regs *regs, __u64 *data)
733 
734 {
735 	regs->ax  = 0x12345678deadbeef;
736 	regs->r11 = (u64) -1;
737 	return 0;
738 }
739 
740 struct testmod_uprobe {
741 	struct path path;
742 	struct uprobe *uprobe;
743 	struct uprobe_consumer consumer;
744 };
745 
746 static DEFINE_MUTEX(testmod_uprobe_mutex);
747 
748 static struct testmod_uprobe uprobe = {
749 	.consumer.handler = uprobe_handler,
750 	.consumer.ret_handler = uprobe_ret_handler,
751 };
752 
testmod_register_uprobe(loff_t offset)753 static int testmod_register_uprobe(loff_t offset)
754 {
755 	int err = -EBUSY;
756 
757 	if (uprobe.uprobe)
758 		return -EBUSY;
759 
760 	mutex_lock(&testmod_uprobe_mutex);
761 
762 	if (uprobe.uprobe)
763 		goto out;
764 
765 	err = kern_path("/proc/self/exe", LOOKUP_FOLLOW, &uprobe.path);
766 	if (err)
767 		goto out;
768 
769 	uprobe.uprobe = uprobe_register(d_real_inode(uprobe.path.dentry),
770 					offset, 0, &uprobe.consumer);
771 	if (IS_ERR(uprobe.uprobe)) {
772 		err = PTR_ERR(uprobe.uprobe);
773 		path_put(&uprobe.path);
774 		uprobe.uprobe = NULL;
775 	}
776 out:
777 	mutex_unlock(&testmod_uprobe_mutex);
778 	return err;
779 }
780 
testmod_unregister_uprobe(void)781 static void testmod_unregister_uprobe(void)
782 {
783 	mutex_lock(&testmod_uprobe_mutex);
784 
785 	if (uprobe.uprobe) {
786 		uprobe_unregister_nosync(uprobe.uprobe, &uprobe.consumer);
787 		uprobe_unregister_sync();
788 		path_put(&uprobe.path);
789 		uprobe.uprobe = NULL;
790 	}
791 
792 	mutex_unlock(&testmod_uprobe_mutex);
793 }
794 
795 static ssize_t
bpf_testmod_uprobe_write(struct file * file,struct kobject * kobj,const struct bin_attribute * bin_attr,char * buf,loff_t off,size_t len)796 bpf_testmod_uprobe_write(struct file *file, struct kobject *kobj,
797 			 const struct bin_attribute *bin_attr,
798 			 char *buf, loff_t off, size_t len)
799 {
800 	unsigned long offset = 0;
801 	int err = 0;
802 
803 	if (kstrtoul(buf, 0, &offset))
804 		return -EINVAL;
805 
806 	if (offset)
807 		err = testmod_register_uprobe(offset);
808 	else
809 		testmod_unregister_uprobe();
810 
811 	return err ?: strlen(buf);
812 }
813 
814 static struct bin_attribute bin_attr_bpf_testmod_uprobe_file __ro_after_init = {
815 	.attr = { .name = "bpf_testmod_uprobe", .mode = 0666, },
816 	.write = bpf_testmod_uprobe_write,
817 };
818 
register_bpf_testmod_uprobe(void)819 static int register_bpf_testmod_uprobe(void)
820 {
821 	return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_uprobe_file);
822 }
823 
unregister_bpf_testmod_uprobe(void)824 static void unregister_bpf_testmod_uprobe(void)
825 {
826 	testmod_unregister_uprobe();
827 	sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_uprobe_file);
828 }
829 
830 #else
register_bpf_testmod_uprobe(void)831 static int register_bpf_testmod_uprobe(void)
832 {
833 	return 0;
834 }
835 
unregister_bpf_testmod_uprobe(void)836 static void unregister_bpf_testmod_uprobe(void) { }
837 #endif
838 
839 BTF_KFUNCS_START(bpf_testmod_common_kfunc_ids)
840 BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
841 BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
842 BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY)
843 BTF_ID_FLAGS(func, bpf_iter_testmod_seq_value)
844 BTF_ID_FLAGS(func, bpf_kfunc_common_test)
845 BTF_ID_FLAGS(func, bpf_kfunc_arena_arg_test)
846 BTF_ID_FLAGS(func, bpf_kfunc_arena_cap_test)
847 BTF_ID_FLAGS(func, bpf_kfunc_arena_cap_nullable_test)
848 BTF_ID_FLAGS(func, bpf_kfunc_arena_args5_test)
849 BTF_ID_FLAGS(func, bpf_kfunc_arena_stack_arg_test)
850 BTF_ID_FLAGS(func, bpf_kfunc_arena_mixed_test)
851 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1)
852 BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test)
853 BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_nonzero_offset_test, KF_ACQUIRE)
854 BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_zero_offset_test, KF_ACQUIRE)
855 BTF_ID_FLAGS(func, bpf_kfunc_nested_release_test, KF_RELEASE)
856 BTF_ID_FLAGS(func, bpf_kfunc_trusted_vma_test)
857 BTF_ID_FLAGS(func, bpf_kfunc_trusted_task_test)
858 BTF_ID_FLAGS(func, bpf_kfunc_trusted_num_test)
859 BTF_ID_FLAGS(func, bpf_kfunc_rcu_task_test, KF_RCU)
860 BTF_ID_FLAGS(func, bpf_kfunc_ret_rcu_test, KF_RET_NULL | KF_RCU_PROTECTED)
861 BTF_ID_FLAGS(func, bpf_kfunc_ret_rcu_test_nostruct, KF_RET_NULL | KF_RCU_PROTECTED)
862 BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL)
863 BTF_ID_FLAGS(func, bpf_testmod_ctx_release, KF_RELEASE)
864 BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_1)
865 BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_2)
866 BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_arena)
867 BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_arena_nullable)
868 BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_arena_stack)
869 BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_arena_multislot)
870 BTF_ID_FLAGS(func, bpf_kfunc_get_default_trusted_ptr_test);
871 BTF_ID_FLAGS(func, bpf_kfunc_put_default_trusted_ptr_test);
872 BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids)
873 
874 BTF_ID_LIST(bpf_testmod_dtor_ids)
875 BTF_ID(struct, bpf_testmod_ctx)
876 BTF_ID(func, bpf_testmod_ctx_release_dtor)
877 
878 static const struct btf_kfunc_id_set bpf_testmod_common_kfunc_set = {
879 	.owner = THIS_MODULE,
880 	.set   = &bpf_testmod_common_kfunc_ids,
881 };
882 
bpf_kfunc_call_test1(struct sock * sk,u32 a,u64 b,u32 c,u64 d)883 __bpf_kfunc u64 bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d)
884 {
885 	return a + b + c + d;
886 }
887 
bpf_kfunc_call_test2(struct sock * sk,u32 a,u32 b)888 __bpf_kfunc int bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b)
889 {
890 	return a + b;
891 }
892 
bpf_kfunc_call_test3(struct sock * sk)893 __bpf_kfunc struct sock *bpf_kfunc_call_test3(struct sock *sk)
894 {
895 	return sk;
896 }
897 
bpf_kfunc_call_test4(signed char a,short b,int c,long d)898 __bpf_kfunc long noinline bpf_kfunc_call_test4(signed char a, short b, int c, long d)
899 {
900 	/*
901 	 * Make val as volatile to avoid compiler optimizations.
902 	 * Verify that negative signed values remain negative after
903 	 * sign-extension (JIT must sign-extend, not zero-extend).
904 	 */
905 	volatile long val;
906 
907 	/* val will be positive, if JIT does zero-extension instead of sign-extension */
908 	val = a;
909 	if (val >= 0)
910 		return 1;
911 
912 	val = b;
913 	if (val >= 0)
914 		return 2;
915 
916 	val = c;
917 	if (val >= 0)
918 		return 3;
919 
920 	/*
921 	 * Provoke the compiler to assume that the caller has sign-extended a,
922 	 * b and c on platforms where this is required (e.g. s390x).
923 	 */
924 	return (long)a + (long)b + (long)c + d;
925 }
926 
bpf_kfunc_call_test5(u8 a,u16 b,u32 c)927 __bpf_kfunc int bpf_kfunc_call_test5(u8 a, u16 b, u32 c)
928 {
929 	/*
930 	 * Make val as volatile to avoid compiler optimizations on the below checks
931 	 * In C, assigning u8/u16/u32 to long performs zero-extension.
932 	 */
933 	volatile long val = a;
934 
935 	/* Check zero-extension */
936 	if (val != (unsigned long)a)
937 		return 1;
938 	/* Check no sign-extension */
939 	if (val < 0)
940 		return 2;
941 
942 	val = b;
943 	if (val != (unsigned long)b)
944 		return 3;
945 	if (val < 0)
946 		return 4;
947 
948 	val = c;
949 	if (val != (unsigned long)c)
950 		return 5;
951 	if (val < 0)
952 		return 6;
953 
954 	return 0;
955 }
956 
bpf_kfunc_call_stack_arg(u64 a,u64 b,u64 c,u64 d,u64 e,u64 f,u64 g,u64 h,u64 i,u64 j)957 __bpf_kfunc u64 bpf_kfunc_call_stack_arg(u64 a, u64 b, u64 c, u64 d,
958 					 u64 e, u64 f, u64 g, u64 h,
959 					 u64 i, u64 j)
960 {
961 	return a + b + c + d + e + f + g + h + i + j;
962 }
963 
bpf_kfunc_call_stack_arg_ptr(u64 a,u64 b,u64 c,u64 d,u64 e,u64 f,u64 g,u64 h,u64 i,struct prog_test_pass1 * p)964 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_ptr(u64 a, u64 b, u64 c, u64 d, u64 e,
965 					     u64 f, u64 g, u64 h, u64 i,
966 					     struct prog_test_pass1 *p)
967 {
968 	return a + b + c + d + e + f + g + h + i + p->x0 + p->x1;
969 }
970 
bpf_kfunc_call_stack_arg_mix(u64 a,u64 b,u64 c,u64 d,u64 e,u64 f,u64 g,struct prog_test_pass1 * p,u64 h,struct prog_test_pass1 * q)971 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_mix(u64 a, u64 b, u64 c, u64 d, u64 e,
972 					     u64 f, u64 g,
973 					     struct prog_test_pass1 *p, u64 h,
974 					     struct prog_test_pass1 *q)
975 {
976 	return a + b + c + d + e + f + g + p->x0 + h + q->x1;
977 }
978 
bpf_kfunc_call_stack_arg_dynptr(u64 a,u64 b,u64 c,u64 d,u64 e,u64 f,u64 g,u64 h,u64 i,struct bpf_dynptr * ptr)979 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_dynptr(u64 a, u64 b, u64 c, u64 d, u64 e,
980 					       u64 f, u64 g, u64 h, u64 i,
981 					       struct bpf_dynptr *ptr)
982 {
983 	const struct bpf_dynptr_kern *kern_ptr = (void *)ptr;
984 
985 	return a + b + c + d + e + f + g + h + i + (kern_ptr->size & 0xFFFFFF);
986 }
987 
bpf_kfunc_call_stack_arg_mem(u64 a,u64 b,u64 c,u64 d,u64 e,void * mem,int mem__sz)988 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_mem(u64 a, u64 b, u64 c, u64 d, u64 e,
989 					     void *mem, int mem__sz)
990 {
991 	const unsigned char *p = mem;
992 	u64 sum = a + b + c + d + e;
993 	int i;
994 
995 	for (i = 0; i < mem__sz; i++)
996 		sum += p[i];
997 	return sum;
998 }
999 
bpf_kfunc_call_stack_arg_iter(u64 a,u64 b,u64 c,u64 d,u64 e,u64 f,u64 g,u64 h,u64 i,struct bpf_iter_testmod_seq * it__iter)1000 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_iter(u64 a, u64 b, u64 c, u64 d, u64 e,
1001 					      u64 f, u64 g, u64 h, u64 i,
1002 					      struct bpf_iter_testmod_seq *it__iter)
1003 {
1004 	return a + b + c + d + e + f + g + h + i + it__iter->value;
1005 }
1006 
bpf_kfunc_call_stack_arg_const_str(u64 a,u64 b,u64 c,u64 d,u64 e,u64 f,u64 g,u64 h,u64 i,const char * str__str)1007 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_const_str(u64 a, u64 b, u64 c, u64 d, u64 e,
1008 						   u64 f, u64 g, u64 h, u64 i,
1009 						   const char *str__str)
1010 {
1011 	return a + b + c + d + e + f + g + h + i;
1012 }
1013 
bpf_kfunc_call_stack_arg_timer(u64 a,u64 b,u64 c,u64 d,u64 e,u64 f,u64 g,u64 h,u64 i,struct bpf_timer * timer)1014 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_timer(u64 a, u64 b, u64 c, u64 d, u64 e,
1015 					       u64 f, u64 g, u64 h, u64 i,
1016 					       struct bpf_timer *timer)
1017 {
1018 	return a + b + c + d + e + f + g + h + i;
1019 }
1020 
bpf_kfunc_call_stack_arg_big(u64 a,u64 b,u64 c,u64 d,u64 e,struct prog_test_big_arg s)1021 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_big(u64 a, u64 b, u64 c, u64 d, u64 e,
1022 					     struct prog_test_big_arg s)
1023 {
1024 	return a + b + c + d + e + s.a + s.b;
1025 }
1026 
1027 static struct prog_test_ref_kfunc prog_test_struct = {
1028 	.a = 42,
1029 	.b = 108,
1030 	.next = &prog_test_struct,
1031 	.cnt = REFCOUNT_INIT(1),
1032 };
1033 
1034 __bpf_kfunc struct prog_test_ref_kfunc *
bpf_kfunc_call_test_acquire(unsigned long * scalar_ptr)1035 bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr)
1036 {
1037 	refcount_inc(&prog_test_struct.cnt);
1038 	return &prog_test_struct;
1039 }
1040 
bpf_kfunc_call_test_offset(struct prog_test_ref_kfunc * p)1041 __bpf_kfunc void bpf_kfunc_call_test_offset(struct prog_test_ref_kfunc *p)
1042 {
1043 	WARN_ON_ONCE(1);
1044 }
1045 
1046 __bpf_kfunc struct prog_test_member *
bpf_kfunc_call_memb_acquire(void)1047 bpf_kfunc_call_memb_acquire(void)
1048 {
1049 	WARN_ON_ONCE(1);
1050 	return NULL;
1051 }
1052 
bpf_kfunc_call_memb1_release(struct prog_test_member1 * p)1053 __bpf_kfunc void bpf_kfunc_call_memb1_release(struct prog_test_member1 *p)
1054 {
1055 	WARN_ON_ONCE(1);
1056 }
1057 
__bpf_kfunc_call_test_get_mem(struct prog_test_ref_kfunc * p,const int size)1058 static int *__bpf_kfunc_call_test_get_mem(struct prog_test_ref_kfunc *p, const int size)
1059 {
1060 	if (size > 2 * sizeof(int))
1061 		return NULL;
1062 
1063 	return (int *)p;
1064 }
1065 
bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc * p,const int rdwr_buf_size)1066 __bpf_kfunc int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p,
1067 						  const int rdwr_buf_size)
1068 {
1069 	return __bpf_kfunc_call_test_get_mem(p, rdwr_buf_size);
1070 }
1071 
bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc * p,const int rdonly_buf_size)1072 __bpf_kfunc int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p,
1073 						    const int rdonly_buf_size)
1074 {
1075 	return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
1076 }
1077 
1078 /* the next 2 ones can't be really used for testing expect to ensure
1079  * that the verifier rejects the call.
1080  * Acquire functions must return struct pointers, so these ones are
1081  * failing.
1082  */
bpf_kfunc_call_test_acq_rdonly_mem(struct prog_test_ref_kfunc * p,const int rdonly_buf_size)1083 __bpf_kfunc int *bpf_kfunc_call_test_acq_rdonly_mem(struct prog_test_ref_kfunc *p,
1084 						    const int rdonly_buf_size)
1085 {
1086 	return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
1087 }
1088 
bpf_kfunc_call_int_mem_release(int * p)1089 __bpf_kfunc void bpf_kfunc_call_int_mem_release(int *p)
1090 {
1091 }
1092 
bpf_kfunc_call_test_pass_ctx(struct __sk_buff * skb)1093 __bpf_kfunc void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb)
1094 {
1095 }
1096 
bpf_kfunc_call_test_pass1(struct prog_test_pass1 * p)1097 __bpf_kfunc void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p)
1098 {
1099 }
1100 
bpf_kfunc_call_test_pass2(struct prog_test_pass2 * p)1101 __bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p)
1102 {
1103 }
1104 
bpf_kfunc_call_test_fail1(struct prog_test_fail1 * p)1105 __bpf_kfunc void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p)
1106 {
1107 }
1108 
bpf_kfunc_call_test_fail2(struct prog_test_fail2 * p)1109 __bpf_kfunc void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p)
1110 {
1111 }
1112 
bpf_kfunc_call_test_fail3(struct prog_test_fail3 * p)1113 __bpf_kfunc void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p)
1114 {
1115 }
1116 
bpf_kfunc_call_test_mem_len_pass1(void * mem,int mem__sz)1117 __bpf_kfunc void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz)
1118 {
1119 }
1120 
bpf_kfunc_call_test_mem_len_fail1(void * mem,int len)1121 __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len)
1122 {
1123 }
1124 
bpf_kfunc_call_test_mem_len_fail2(u64 * mem,int len)1125 __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len)
1126 {
1127 }
1128 
bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc * p)1129 __bpf_kfunc void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p)
1130 {
1131 	/* p != NULL, but p->cnt could be 0 */
1132 }
1133 
bpf_kfunc_call_test_destructive(void)1134 __bpf_kfunc void bpf_kfunc_call_test_destructive(void)
1135 {
1136 }
1137 
bpf_kfunc_call_test_static_unused_arg(u32 arg,u32 unused)1138 __bpf_kfunc static u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused)
1139 {
1140 	return arg;
1141 }
1142 
bpf_kfunc_call_test_sleepable(void)1143 __bpf_kfunc void bpf_kfunc_call_test_sleepable(void)
1144 {
1145 }
1146 
1147 struct bpf_kfunc_rcu_tasks_trace_data {
1148 	struct rcu_head rcu;
1149 	int *done;
1150 };
1151 
bpf_kfunc_rcu_tasks_trace_cb(struct rcu_head * rhp)1152 static void bpf_kfunc_rcu_tasks_trace_cb(struct rcu_head *rhp)
1153 {
1154 	struct bpf_kfunc_rcu_tasks_trace_data *data;
1155 
1156 	data = container_of(rhp, struct bpf_kfunc_rcu_tasks_trace_data, rcu);
1157 	WRITE_ONCE(*data->done, 1);
1158 	kfree(data);
1159 }
1160 
bpf_kfunc_call_test_call_rcu_tasks_trace(int * done)1161 __bpf_kfunc int bpf_kfunc_call_test_call_rcu_tasks_trace(int *done)
1162 {
1163 	struct bpf_kfunc_rcu_tasks_trace_data *data;
1164 
1165 	data = kmalloc(sizeof(*data), GFP_ATOMIC);
1166 	if (!data)
1167 		return -ENOMEM;
1168 	data->done = done;
1169 	call_rcu_tasks_trace(&data->rcu, bpf_kfunc_rcu_tasks_trace_cb);
1170 	return 0;
1171 }
1172 
bpf_kfunc_init_sock(struct init_sock_args * args)1173 __bpf_kfunc int bpf_kfunc_init_sock(struct init_sock_args *args)
1174 {
1175 	int proto;
1176 	int err;
1177 
1178 	mutex_lock(&sock_lock);
1179 
1180 	if (sock) {
1181 		pr_err("%s called without releasing old sock", __func__);
1182 		err = -EPERM;
1183 		goto out;
1184 	}
1185 
1186 	switch (args->af) {
1187 	case AF_INET:
1188 	case AF_INET6:
1189 		proto = args->type == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP;
1190 		break;
1191 	case AF_UNIX:
1192 		proto = PF_UNIX;
1193 		break;
1194 	default:
1195 		pr_err("invalid address family %d\n", args->af);
1196 		err = -EINVAL;
1197 		goto out;
1198 	}
1199 
1200 	err = sock_create_kern(current->nsproxy->net_ns, args->af, args->type,
1201 			       proto, &sock);
1202 
1203 	if (!err)
1204 		/* Set timeout for call to kernel_connect() to prevent it from hanging,
1205 		 * and consider the connection attempt failed if it returns
1206 		 * -EINPROGRESS.
1207 		 */
1208 		sock->sk->sk_sndtimeo = CONNECT_TIMEOUT_SEC * HZ;
1209 out:
1210 	mutex_unlock(&sock_lock);
1211 
1212 	return err;
1213 }
1214 
bpf_kfunc_close_sock(void)1215 __bpf_kfunc void bpf_kfunc_close_sock(void)
1216 {
1217 	mutex_lock(&sock_lock);
1218 
1219 	if (sock) {
1220 		sock_release(sock);
1221 		sock = NULL;
1222 	}
1223 
1224 	mutex_unlock(&sock_lock);
1225 }
1226 
bpf_kfunc_call_kernel_connect(struct addr_args * args)1227 __bpf_kfunc int bpf_kfunc_call_kernel_connect(struct addr_args *args)
1228 {
1229 	int err;
1230 
1231 	if (args->addrlen > sizeof(args->addr))
1232 		return -EINVAL;
1233 
1234 	mutex_lock(&sock_lock);
1235 
1236 	if (!sock) {
1237 		pr_err("%s called without initializing sock", __func__);
1238 		err = -EPERM;
1239 		goto out;
1240 	}
1241 
1242 	err = kernel_connect(sock, (struct sockaddr_unsized *)&args->addr,
1243 			     args->addrlen, 0);
1244 out:
1245 	mutex_unlock(&sock_lock);
1246 
1247 	return err;
1248 }
1249 
bpf_kfunc_call_kernel_bind(struct addr_args * args)1250 __bpf_kfunc int bpf_kfunc_call_kernel_bind(struct addr_args *args)
1251 {
1252 	int err;
1253 
1254 	if (args->addrlen > sizeof(args->addr))
1255 		return -EINVAL;
1256 
1257 	mutex_lock(&sock_lock);
1258 
1259 	if (!sock) {
1260 		pr_err("%s called without initializing sock", __func__);
1261 		err = -EPERM;
1262 		goto out;
1263 	}
1264 
1265 	err = kernel_bind(sock, (struct sockaddr_unsized *)&args->addr, args->addrlen);
1266 out:
1267 	mutex_unlock(&sock_lock);
1268 
1269 	return err;
1270 }
1271 
bpf_kfunc_call_kernel_listen(void)1272 __bpf_kfunc int bpf_kfunc_call_kernel_listen(void)
1273 {
1274 	int err;
1275 
1276 	mutex_lock(&sock_lock);
1277 
1278 	if (!sock) {
1279 		pr_err("%s called without initializing sock", __func__);
1280 		err = -EPERM;
1281 		goto out;
1282 	}
1283 
1284 	err = kernel_listen(sock, 128);
1285 out:
1286 	mutex_unlock(&sock_lock);
1287 
1288 	return err;
1289 }
1290 
bpf_kfunc_call_kernel_sendmsg(struct sendmsg_args * args)1291 __bpf_kfunc int bpf_kfunc_call_kernel_sendmsg(struct sendmsg_args *args)
1292 {
1293 	struct msghdr msg = {
1294 		.msg_name	= &args->addr.addr,
1295 		.msg_namelen	= args->addr.addrlen,
1296 	};
1297 	struct kvec iov;
1298 	int err;
1299 
1300 	if (args->addr.addrlen > sizeof(args->addr.addr) ||
1301 	    args->msglen > sizeof(args->msg))
1302 		return -EINVAL;
1303 
1304 	iov.iov_base = args->msg;
1305 	iov.iov_len  = args->msglen;
1306 
1307 	mutex_lock(&sock_lock);
1308 
1309 	if (!sock) {
1310 		pr_err("%s called without initializing sock", __func__);
1311 		err = -EPERM;
1312 		goto out;
1313 	}
1314 
1315 	err = kernel_sendmsg(sock, &msg, &iov, 1, args->msglen);
1316 	args->addr.addrlen = msg.msg_namelen;
1317 out:
1318 	mutex_unlock(&sock_lock);
1319 
1320 	return err;
1321 }
1322 
bpf_kfunc_call_sock_sendmsg(struct sendmsg_args * args)1323 __bpf_kfunc int bpf_kfunc_call_sock_sendmsg(struct sendmsg_args *args)
1324 {
1325 	struct msghdr msg = {
1326 		.msg_name	= &args->addr.addr,
1327 		.msg_namelen	= args->addr.addrlen,
1328 	};
1329 	struct kvec iov;
1330 	int err;
1331 
1332 	if (args->addr.addrlen > sizeof(args->addr.addr) ||
1333 	    args->msglen > sizeof(args->msg))
1334 		return -EINVAL;
1335 
1336 	iov.iov_base = args->msg;
1337 	iov.iov_len  = args->msglen;
1338 
1339 	iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iov, 1, args->msglen);
1340 	mutex_lock(&sock_lock);
1341 
1342 	if (!sock) {
1343 		pr_err("%s called without initializing sock", __func__);
1344 		err = -EPERM;
1345 		goto out;
1346 	}
1347 
1348 	err = sock_sendmsg(sock, &msg);
1349 	args->addr.addrlen = msg.msg_namelen;
1350 out:
1351 	mutex_unlock(&sock_lock);
1352 
1353 	return err;
1354 }
1355 
bpf_kfunc_call_kernel_getsockname(struct addr_args * args)1356 __bpf_kfunc int bpf_kfunc_call_kernel_getsockname(struct addr_args *args)
1357 {
1358 	int err;
1359 
1360 	mutex_lock(&sock_lock);
1361 
1362 	if (!sock) {
1363 		pr_err("%s called without initializing sock", __func__);
1364 		err = -EPERM;
1365 		goto out;
1366 	}
1367 
1368 	err = kernel_getsockname(sock, (struct sockaddr *)&args->addr);
1369 	if (err < 0)
1370 		goto out;
1371 
1372 	args->addrlen = err;
1373 	err = 0;
1374 out:
1375 	mutex_unlock(&sock_lock);
1376 
1377 	return err;
1378 }
1379 
bpf_kfunc_call_kernel_getpeername(struct addr_args * args)1380 __bpf_kfunc int bpf_kfunc_call_kernel_getpeername(struct addr_args *args)
1381 {
1382 	int err;
1383 
1384 	mutex_lock(&sock_lock);
1385 
1386 	if (!sock) {
1387 		pr_err("%s called without initializing sock", __func__);
1388 		err = -EPERM;
1389 		goto out;
1390 	}
1391 
1392 	err = kernel_getpeername(sock, (struct sockaddr *)&args->addr);
1393 	if (err < 0)
1394 		goto out;
1395 
1396 	args->addrlen = err;
1397 	err = 0;
1398 out:
1399 	mutex_unlock(&sock_lock);
1400 
1401 	return err;
1402 }
1403 
1404 static DEFINE_MUTEX(st_ops_mutex);
1405 static struct bpf_testmod_st_ops *st_ops;
1406 
bpf_kfunc_st_ops_test_prologue(struct st_ops_args * args)1407 __bpf_kfunc int bpf_kfunc_st_ops_test_prologue(struct st_ops_args *args)
1408 {
1409 	int ret = -1;
1410 
1411 	mutex_lock(&st_ops_mutex);
1412 	if (st_ops && st_ops->test_prologue)
1413 		ret = st_ops->test_prologue(args);
1414 	mutex_unlock(&st_ops_mutex);
1415 
1416 	return ret;
1417 }
1418 
bpf_kfunc_st_ops_test_epilogue(struct st_ops_args * args)1419 __bpf_kfunc int bpf_kfunc_st_ops_test_epilogue(struct st_ops_args *args)
1420 {
1421 	int ret = -1;
1422 
1423 	mutex_lock(&st_ops_mutex);
1424 	if (st_ops && st_ops->test_epilogue)
1425 		ret = st_ops->test_epilogue(args);
1426 	mutex_unlock(&st_ops_mutex);
1427 
1428 	return ret;
1429 }
1430 
bpf_kfunc_st_ops_test_pro_epilogue(struct st_ops_args * args)1431 __bpf_kfunc int bpf_kfunc_st_ops_test_pro_epilogue(struct st_ops_args *args)
1432 {
1433 	int ret = -1;
1434 
1435 	mutex_lock(&st_ops_mutex);
1436 	if (st_ops && st_ops->test_pro_epilogue)
1437 		ret = st_ops->test_pro_epilogue(args);
1438 	mutex_unlock(&st_ops_mutex);
1439 
1440 	return ret;
1441 }
1442 
bpf_kfunc_st_ops_inc10(struct st_ops_args * args)1443 __bpf_kfunc int bpf_kfunc_st_ops_inc10(struct st_ops_args *args)
1444 {
1445 	args->a += 10;
1446 	return args->a;
1447 }
1448 
1449 __bpf_kfunc int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id);
1450 __bpf_kfunc int bpf_kfunc_multi_st_ops_test_1_assoc(struct st_ops_args *args, struct bpf_prog_aux *aux);
1451 
1452 __bpf_kfunc int bpf_kfunc_implicit_arg(int a, struct bpf_prog_aux *aux);
1453 __bpf_kfunc int bpf_kfunc_implicit_arg_legacy(int a, int b, struct bpf_prog_aux *aux);
1454 __bpf_kfunc int bpf_kfunc_implicit_arg_legacy_impl(int a, int b, struct bpf_prog_aux *aux);
1455 
1456 /* hook targets */
bpf_testmod_test_hardirq_fn(void)1457 noinline void bpf_testmod_test_hardirq_fn(void) { barrier(); }
bpf_testmod_test_softirq_fn(void)1458 noinline void bpf_testmod_test_softirq_fn(void) { barrier(); }
1459 
1460 /* Tasklet for SoftIRQ context */
ctx_check_tasklet_fn(struct tasklet_struct * t)1461 static void ctx_check_tasklet_fn(struct tasklet_struct *t)
1462 {
1463 	bpf_testmod_test_softirq_fn();
1464 }
1465 
1466 DECLARE_TASKLET(ctx_check_tasklet, ctx_check_tasklet_fn);
1467 
1468 /* IRQ Work for HardIRQ context */
ctx_check_irq_fn(struct irq_work * work)1469 static void ctx_check_irq_fn(struct irq_work *work)
1470 {
1471 	bpf_testmod_test_hardirq_fn();
1472 	tasklet_schedule(&ctx_check_tasklet);
1473 }
1474 
1475 static struct irq_work ctx_check_irq = IRQ_WORK_INIT_HARD(ctx_check_irq_fn);
1476 
1477 /* The kfunc trigger */
bpf_kfunc_trigger_ctx_check(void)1478 __bpf_kfunc void bpf_kfunc_trigger_ctx_check(void)
1479 {
1480 	irq_work_queue(&ctx_check_irq);
1481 }
1482 
1483 BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids)
BTF_ID_FLAGS(func,bpf_testmod_test_mod_kfunc,KF_SPINLOCK_SAFE)1484 BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc, KF_SPINLOCK_SAFE)
1485 BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
1486 BTF_ID_FLAGS(func, bpf_kfunc_call_test2)
1487 BTF_ID_FLAGS(func, bpf_kfunc_call_test3)
1488 BTF_ID_FLAGS(func, bpf_kfunc_call_test4)
1489 BTF_ID_FLAGS(func, bpf_kfunc_call_test5)
1490 BTF_ID_FLAGS(func, bpf_kfunc_call_stack_arg)
1491 BTF_ID_FLAGS(func, bpf_kfunc_call_stack_arg_ptr)
1492 BTF_ID_FLAGS(func, bpf_kfunc_call_stack_arg_mix)
1493 BTF_ID_FLAGS(func, bpf_kfunc_call_stack_arg_dynptr)
1494 BTF_ID_FLAGS(func, bpf_kfunc_call_stack_arg_mem)
1495 BTF_ID_FLAGS(func, bpf_kfunc_call_stack_arg_iter)
1496 BTF_ID_FLAGS(func, bpf_kfunc_call_stack_arg_const_str)
1497 BTF_ID_FLAGS(func, bpf_kfunc_call_stack_arg_timer)
1498 BTF_ID_FLAGS(func, bpf_kfunc_call_stack_arg_big)
1499 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1)
1500 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2)
1501 BTF_ID_FLAGS(func, bpf_kfunc_call_test_acquire, KF_ACQUIRE | KF_RET_NULL)
1502 BTF_ID_FLAGS(func, bpf_kfunc_call_memb_acquire, KF_ACQUIRE | KF_RET_NULL)
1503 BTF_ID_FLAGS(func, bpf_kfunc_call_memb1_release, KF_RELEASE)
1504 BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdwr_mem, KF_RET_NULL)
1505 BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdonly_mem, KF_RET_NULL)
1506 BTF_ID_FLAGS(func, bpf_kfunc_call_test_acq_rdonly_mem, KF_ACQUIRE | KF_RET_NULL)
1507 BTF_ID_FLAGS(func, bpf_kfunc_call_int_mem_release, KF_RELEASE)
1508 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx)
1509 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1)
1510 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2)
1511 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1)
1512 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2)
1513 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail3)
1514 BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_RCU)
1515 BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE)
1516 BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg)
1517 BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset)
1518 BTF_ID_FLAGS(func, bpf_kfunc_call_test_sleepable, KF_SLEEPABLE)
1519 BTF_ID_FLAGS(func, bpf_kfunc_call_test_call_rcu_tasks_trace)
1520 BTF_ID_FLAGS(func, bpf_kfunc_init_sock, KF_SLEEPABLE)
1521 BTF_ID_FLAGS(func, bpf_kfunc_close_sock, KF_SLEEPABLE)
1522 BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_connect, KF_SLEEPABLE)
1523 BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_bind, KF_SLEEPABLE)
1524 BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_listen, KF_SLEEPABLE)
1525 BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_sendmsg, KF_SLEEPABLE)
1526 BTF_ID_FLAGS(func, bpf_kfunc_call_sock_sendmsg, KF_SLEEPABLE)
1527 BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_getsockname, KF_SLEEPABLE)
1528 BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_getpeername, KF_SLEEPABLE)
1529 BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_prologue, KF_SLEEPABLE)
1530 BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_epilogue, KF_SLEEPABLE)
1531 BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_pro_epilogue, KF_SLEEPABLE)
1532 BTF_ID_FLAGS(func, bpf_kfunc_st_ops_inc10)
1533 BTF_ID_FLAGS(func, bpf_kfunc_multi_st_ops_test_1)
1534 BTF_ID_FLAGS(func, bpf_kfunc_multi_st_ops_test_1_assoc, KF_IMPLICIT_ARGS)
1535 BTF_ID_FLAGS(func, bpf_kfunc_implicit_arg, KF_IMPLICIT_ARGS)
1536 BTF_ID_FLAGS(func, bpf_kfunc_implicit_arg_legacy, KF_IMPLICIT_ARGS)
1537 BTF_ID_FLAGS(func, bpf_kfunc_implicit_arg_legacy_impl)
1538 BTF_ID_FLAGS(func, bpf_kfunc_trigger_ctx_check)
1539 BTF_KFUNCS_END(bpf_testmod_check_kfunc_ids)
1540 
1541 static int bpf_testmod_ops_init(struct btf *btf)
1542 {
1543 	return 0;
1544 }
1545 
bpf_testmod_ops_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)1546 static bool bpf_testmod_ops_is_valid_access(int off, int size,
1547 					    enum bpf_access_type type,
1548 					    const struct bpf_prog *prog,
1549 					    struct bpf_insn_access_aux *info)
1550 {
1551 	return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
1552 }
1553 
bpf_testmod_ops_init_member(const struct btf_type * t,const struct btf_member * member,void * kdata,const void * udata)1554 static int bpf_testmod_ops_init_member(const struct btf_type *t,
1555 				       const struct btf_member *member,
1556 				       void *kdata, const void *udata)
1557 {
1558 	if (member->offset == offsetof(struct bpf_testmod_ops, data) * 8) {
1559 		/* For data fields, this function has to copy it and return
1560 		 * 1 to indicate that the data has been handled by the
1561 		 * struct_ops type, or the verifier will reject the map if
1562 		 * the value of the data field is not zero.
1563 		 */
1564 		((struct bpf_testmod_ops *)kdata)->data = ((struct bpf_testmod_ops *)udata)->data;
1565 		return 1;
1566 	}
1567 	return 0;
1568 }
1569 
1570 static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
1571 	.owner = THIS_MODULE,
1572 	.set   = &bpf_testmod_check_kfunc_ids,
1573 };
1574 
1575 static const struct bpf_verifier_ops bpf_testmod_verifier_ops = {
1576 	.get_func_proto	 = bpf_base_func_proto,
1577 	.is_valid_access = bpf_testmod_ops_is_valid_access,
1578 };
1579 
1580 static const struct bpf_verifier_ops bpf_testmod_verifier_ops3 = {
1581 	.is_valid_access = bpf_testmod_ops_is_valid_access,
1582 };
1583 
bpf_dummy_reg(void * kdata,struct bpf_link * link)1584 static int bpf_dummy_reg(void *kdata, struct bpf_link *link)
1585 {
1586 	struct bpf_testmod_ops *ops = kdata;
1587 
1588 	if (ops->test_1)
1589 		ops->test_1();
1590 	/* Some test cases (ex. struct_ops_maybe_null) may not have test_2
1591 	 * initialized, so we need to check for NULL.
1592 	 */
1593 	if (ops->test_2)
1594 		ops->test_2(4, ops->data);
1595 
1596 	return 0;
1597 }
1598 
bpf_dummy_unreg(void * kdata,struct bpf_link * link)1599 static void bpf_dummy_unreg(void *kdata, struct bpf_link *link)
1600 {
1601 }
1602 
bpf_testmod_test_1(void)1603 static int bpf_testmod_test_1(void)
1604 {
1605 	return 0;
1606 }
1607 
bpf_testmod_test_2(int a,int b)1608 static void bpf_testmod_test_2(int a, int b)
1609 {
1610 }
1611 
bpf_testmod_tramp(int value)1612 static int bpf_testmod_tramp(int value)
1613 {
1614 	return 0;
1615 }
1616 
bpf_testmod_ops__test_maybe_null(int dummy,struct task_struct * task__nullable)1617 static int bpf_testmod_ops__test_maybe_null(int dummy,
1618 					    struct task_struct *task__nullable)
1619 {
1620 	return 0;
1621 }
1622 
bpf_testmod_ops__test_refcounted(int dummy,struct task_struct * task__ref)1623 static int bpf_testmod_ops__test_refcounted(int dummy,
1624 					    struct task_struct *task__ref)
1625 {
1626 	return 0;
1627 }
1628 
bpf_testmod_ops__test_refcounted_multi(int dummy,struct task_struct * task__nullable,struct task_struct * task__ref)1629 static int bpf_testmod_ops__test_refcounted_multi(int dummy, struct task_struct *task__nullable,
1630 						  struct task_struct *task__ref)
1631 {
1632 	return 0;
1633 }
1634 
1635 static struct task_struct *
bpf_testmod_ops__test_return_ref_kptr(int dummy,struct task_struct * task__ref,struct cgroup * cgrp)1636 bpf_testmod_ops__test_return_ref_kptr(int dummy, struct task_struct *task__ref,
1637 				      struct cgroup *cgrp)
1638 {
1639 	return NULL;
1640 }
1641 
1642 static struct bpf_testmod_ops __bpf_testmod_ops = {
1643 	.test_1 = bpf_testmod_test_1,
1644 	.test_2 = bpf_testmod_test_2,
1645 	.test_maybe_null = bpf_testmod_ops__test_maybe_null,
1646 	.test_refcounted = bpf_testmod_ops__test_refcounted,
1647 	.test_refcounted_multi = bpf_testmod_ops__test_refcounted_multi,
1648 	.test_return_ref_kptr = bpf_testmod_ops__test_return_ref_kptr,
1649 };
1650 
1651 struct bpf_struct_ops bpf_bpf_testmod_ops = {
1652 	.verifier_ops = &bpf_testmod_verifier_ops,
1653 	.init = bpf_testmod_ops_init,
1654 	.init_member = bpf_testmod_ops_init_member,
1655 	.reg = bpf_dummy_reg,
1656 	.unreg = bpf_dummy_unreg,
1657 	.cfi_stubs = &__bpf_testmod_ops,
1658 	.name = "bpf_testmod_ops",
1659 	.owner = THIS_MODULE,
1660 };
1661 
bpf_dummy_reg2(void * kdata,struct bpf_link * link)1662 static int bpf_dummy_reg2(void *kdata, struct bpf_link *link)
1663 {
1664 	struct bpf_testmod_ops2 *ops = kdata;
1665 
1666 	ops->test_1();
1667 	return 0;
1668 }
1669 
1670 static struct bpf_testmod_ops2 __bpf_testmod_ops2 = {
1671 	.test_1 = bpf_testmod_test_1,
1672 };
1673 
1674 struct bpf_struct_ops bpf_testmod_ops2 = {
1675 	.verifier_ops = &bpf_testmod_verifier_ops,
1676 	.init = bpf_testmod_ops_init,
1677 	.init_member = bpf_testmod_ops_init_member,
1678 	.reg = bpf_dummy_reg2,
1679 	.unreg = bpf_dummy_unreg,
1680 	.cfi_stubs = &__bpf_testmod_ops2,
1681 	.name = "bpf_testmod_ops2",
1682 	.owner = THIS_MODULE,
1683 };
1684 
st_ops3_reg(void * kdata,struct bpf_link * link)1685 static int st_ops3_reg(void *kdata, struct bpf_link *link)
1686 {
1687 	int err = 0;
1688 
1689 	mutex_lock(&st_ops_mutex);
1690 	if (st_ops3) {
1691 		pr_err("st_ops has already been registered\n");
1692 		err = -EEXIST;
1693 		goto unlock;
1694 	}
1695 	st_ops3 = kdata;
1696 
1697 unlock:
1698 	mutex_unlock(&st_ops_mutex);
1699 	return err;
1700 }
1701 
st_ops3_unreg(void * kdata,struct bpf_link * link)1702 static void st_ops3_unreg(void *kdata, struct bpf_link *link)
1703 {
1704 	mutex_lock(&st_ops_mutex);
1705 	st_ops3 = NULL;
1706 	mutex_unlock(&st_ops_mutex);
1707 }
1708 
test_1_recursion_detected(struct bpf_prog * prog)1709 static void test_1_recursion_detected(struct bpf_prog *prog)
1710 {
1711 	struct bpf_prog_stats *stats;
1712 
1713 	stats = this_cpu_ptr(prog->stats);
1714 	printk("bpf_testmod: oh no, recursing into test_1, recursion_misses %llu",
1715 	       u64_stats_read(&stats->misses));
1716 }
1717 
st_ops3_check_member(const struct btf_type * t,const struct btf_member * member,const struct bpf_prog * prog)1718 static int st_ops3_check_member(const struct btf_type *t,
1719 				const struct btf_member *member,
1720 				const struct bpf_prog *prog)
1721 {
1722 	u32 moff = __btf_member_bit_offset(t, member) / 8;
1723 
1724 	switch (moff) {
1725 	case offsetof(struct bpf_testmod_ops3, test_1):
1726 		prog->aux->priv_stack_requested = true;
1727 		prog->aux->recursion_detected = test_1_recursion_detected;
1728 		fallthrough;
1729 	default:
1730 		break;
1731 	}
1732 	return 0;
1733 }
1734 
1735 struct bpf_struct_ops bpf_testmod_ops3 = {
1736 	.verifier_ops = &bpf_testmod_verifier_ops3,
1737 	.init = bpf_testmod_ops_init,
1738 	.init_member = bpf_testmod_ops_init_member,
1739 	.reg = st_ops3_reg,
1740 	.unreg = st_ops3_unreg,
1741 	.check_member = st_ops3_check_member,
1742 	.cfi_stubs = &__bpf_testmod_ops3,
1743 	.name = "bpf_testmod_ops3",
1744 	.owner = THIS_MODULE,
1745 };
1746 
bpf_test_mod_st_ops__test_prologue(struct st_ops_args * args)1747 static int bpf_test_mod_st_ops__test_prologue(struct st_ops_args *args)
1748 {
1749 	return 0;
1750 }
1751 
bpf_test_mod_st_ops__test_epilogue(struct st_ops_args * args)1752 static int bpf_test_mod_st_ops__test_epilogue(struct st_ops_args *args)
1753 {
1754 	return 0;
1755 }
1756 
bpf_test_mod_st_ops__test_pro_epilogue(struct st_ops_args * args)1757 static int bpf_test_mod_st_ops__test_pro_epilogue(struct st_ops_args *args)
1758 {
1759 	return 0;
1760 }
1761 
1762 static int bpf_cgroup_from_id_id;
1763 static int bpf_cgroup_release_id;
1764 
st_ops_gen_prologue_with_kfunc(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)1765 static int st_ops_gen_prologue_with_kfunc(struct bpf_insn *insn_buf, bool direct_write,
1766 					  const struct bpf_prog *prog)
1767 {
1768 	struct bpf_insn *insn = insn_buf;
1769 
1770 	/* r8 = r1; // r8 will be "u64 *ctx".
1771 	 * r1 = 0;
1772 	 * r0 = bpf_cgroup_from_id(r1);
1773 	 * if r0 != 0 goto pc+5;
1774 	 * r6 = r8[0]; // r6 will be "struct st_ops *args".
1775 	 * r7 = r6->a;
1776 	 * r7 += 1000;
1777 	 * r6->a = r7;
1778 	 * goto pc+2;
1779 	 * r1 = r0;
1780 	 * bpf_cgroup_release(r1);
1781 	 * r1 = r8;
1782 	 */
1783 	*insn++ = BPF_MOV64_REG(BPF_REG_8, BPF_REG_1);
1784 	*insn++ = BPF_MOV64_IMM(BPF_REG_1, 0);
1785 	*insn++ = BPF_CALL_KFUNC(0, bpf_cgroup_from_id_id);
1786 	*insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 5);
1787 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_8, 0);
1788 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_6, offsetof(struct st_ops_args, a));
1789 	*insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, 1000);
1790 	*insn++ = BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_7, offsetof(struct st_ops_args, a));
1791 	*insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 2);
1792 	*insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_0);
1793 	*insn++ = BPF_CALL_KFUNC(0, bpf_cgroup_release_id);
1794 	*insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_8);
1795 	*insn++ = prog->insnsi[0];
1796 
1797 	return insn - insn_buf;
1798 }
1799 
st_ops_gen_epilogue_with_kfunc(struct bpf_insn * insn_buf,const struct bpf_prog * prog,s16 ctx_stack_off)1800 static int st_ops_gen_epilogue_with_kfunc(struct bpf_insn *insn_buf, const struct bpf_prog *prog,
1801 					  s16 ctx_stack_off)
1802 {
1803 	struct bpf_insn *insn = insn_buf;
1804 
1805 	/* r1 = 0;
1806 	 * r6 = 0;
1807 	 * r0 = bpf_cgroup_from_id(r1);
1808 	 * if r0 != 0 goto pc+6;
1809 	 * r1 = stack[ctx_stack_off]; // r1 will be "u64 *ctx"
1810 	 * r1 = r1[0]; // r1 will be "struct st_ops *args"
1811 	 * r6 = r1->a;
1812 	 * r6 += 10000;
1813 	 * r1->a = r6;
1814 	 * goto pc+2
1815 	 * r1 = r0;
1816 	 * bpf_cgroup_release(r1);
1817 	 * r0 = r6;
1818 	 * r0 *= 2;
1819 	 * BPF_EXIT;
1820 	 */
1821 	*insn++ = BPF_MOV64_IMM(BPF_REG_1, 0);
1822 	*insn++ = BPF_MOV64_IMM(BPF_REG_6, 0);
1823 	*insn++ = BPF_CALL_KFUNC(0, bpf_cgroup_from_id_id);
1824 	*insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 6);
1825 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_FP, ctx_stack_off);
1826 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
1827 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, offsetof(struct st_ops_args, a));
1828 	*insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 10000);
1829 	*insn++ = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, offsetof(struct st_ops_args, a));
1830 	*insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 2);
1831 	*insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_0);
1832 	*insn++ = BPF_CALL_KFUNC(0, bpf_cgroup_release_id);
1833 	*insn++ = BPF_MOV64_REG(BPF_REG_0, BPF_REG_6);
1834 	*insn++ = BPF_ALU64_IMM(BPF_MUL, BPF_REG_0, 2);
1835 	*insn++ = BPF_EXIT_INSN();
1836 
1837 	return insn - insn_buf;
1838 }
1839 
1840 #define KFUNC_PRO_EPI_PREFIX "test_kfunc_"
st_ops_gen_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)1841 static int st_ops_gen_prologue(struct bpf_insn *insn_buf, bool direct_write,
1842 			       const struct bpf_prog *prog)
1843 {
1844 	struct bpf_insn *insn = insn_buf;
1845 
1846 	if (strcmp(prog->aux->attach_func_name, "test_prologue") &&
1847 	    strcmp(prog->aux->attach_func_name, "test_pro_epilogue"))
1848 		return 0;
1849 
1850 	if (!strncmp(prog->aux->name, KFUNC_PRO_EPI_PREFIX, strlen(KFUNC_PRO_EPI_PREFIX)))
1851 		return st_ops_gen_prologue_with_kfunc(insn_buf, direct_write, prog);
1852 
1853 	/* r6 = r1[0]; // r6 will be "struct st_ops *args". r1 is "u64 *ctx".
1854 	 * r7 = r6->a;
1855 	 * r7 += 1000;
1856 	 * r6->a = r7;
1857 	 */
1858 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0);
1859 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_6, offsetof(struct st_ops_args, a));
1860 	*insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, 1000);
1861 	*insn++ = BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_7, offsetof(struct st_ops_args, a));
1862 	*insn++ = prog->insnsi[0];
1863 
1864 	return insn - insn_buf;
1865 }
1866 
st_ops_gen_epilogue(struct bpf_insn * insn_buf,const struct bpf_prog * prog,s16 ctx_stack_off)1867 static int st_ops_gen_epilogue(struct bpf_insn *insn_buf, const struct bpf_prog *prog,
1868 			       s16 ctx_stack_off)
1869 {
1870 	struct bpf_insn *insn = insn_buf;
1871 
1872 	if (strcmp(prog->aux->attach_func_name, "test_epilogue") &&
1873 	    strcmp(prog->aux->attach_func_name, "test_pro_epilogue"))
1874 		return 0;
1875 
1876 	if (!strncmp(prog->aux->name, KFUNC_PRO_EPI_PREFIX, strlen(KFUNC_PRO_EPI_PREFIX)))
1877 		return st_ops_gen_epilogue_with_kfunc(insn_buf, prog, ctx_stack_off);
1878 
1879 	/* r1 = stack[ctx_stack_off]; // r1 will be "u64 *ctx"
1880 	 * r1 = r1[0]; // r1 will be "struct st_ops *args"
1881 	 * r6 = r1->a;
1882 	 * r6 += 10000;
1883 	 * r1->a = r6;
1884 	 * r0 = r6;
1885 	 * r0 *= 2;
1886 	 * BPF_EXIT;
1887 	 */
1888 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_FP, ctx_stack_off);
1889 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
1890 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, offsetof(struct st_ops_args, a));
1891 	*insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 10000);
1892 	*insn++ = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, offsetof(struct st_ops_args, a));
1893 	*insn++ = BPF_MOV64_REG(BPF_REG_0, BPF_REG_6);
1894 	*insn++ = BPF_ALU64_IMM(BPF_MUL, BPF_REG_0, 2);
1895 	*insn++ = BPF_EXIT_INSN();
1896 
1897 	return insn - insn_buf;
1898 }
1899 
st_ops_btf_struct_access(struct bpf_verifier_log * log,const struct bpf_reg_state * reg,int off,int size)1900 static int st_ops_btf_struct_access(struct bpf_verifier_log *log,
1901 				    const struct bpf_reg_state *reg,
1902 				    int off, int size)
1903 {
1904 	if (off < 0 || off + size > sizeof(struct st_ops_args))
1905 		return -EACCES;
1906 	return 0;
1907 }
1908 
1909 static const struct bpf_verifier_ops st_ops_verifier_ops = {
1910 	.is_valid_access = bpf_testmod_ops_is_valid_access,
1911 	.btf_struct_access = st_ops_btf_struct_access,
1912 	.gen_prologue = st_ops_gen_prologue,
1913 	.gen_epilogue = st_ops_gen_epilogue,
1914 	.get_func_proto = bpf_base_func_proto,
1915 };
1916 
1917 static struct bpf_testmod_st_ops st_ops_cfi_stubs = {
1918 	.test_prologue = bpf_test_mod_st_ops__test_prologue,
1919 	.test_epilogue = bpf_test_mod_st_ops__test_epilogue,
1920 	.test_pro_epilogue = bpf_test_mod_st_ops__test_pro_epilogue,
1921 };
1922 
st_ops_reg(void * kdata,struct bpf_link * link)1923 static int st_ops_reg(void *kdata, struct bpf_link *link)
1924 {
1925 	int err = 0;
1926 
1927 	mutex_lock(&st_ops_mutex);
1928 	if (st_ops) {
1929 		pr_err("st_ops has already been registered\n");
1930 		err = -EEXIST;
1931 		goto unlock;
1932 	}
1933 	st_ops = kdata;
1934 
1935 unlock:
1936 	mutex_unlock(&st_ops_mutex);
1937 	return err;
1938 }
1939 
st_ops_unreg(void * kdata,struct bpf_link * link)1940 static void st_ops_unreg(void *kdata, struct bpf_link *link)
1941 {
1942 	mutex_lock(&st_ops_mutex);
1943 	st_ops = NULL;
1944 	mutex_unlock(&st_ops_mutex);
1945 }
1946 
st_ops_init(struct btf * btf)1947 static int st_ops_init(struct btf *btf)
1948 {
1949 	struct btf *kfunc_btf;
1950 
1951 	bpf_cgroup_from_id_id = bpf_find_btf_id("bpf_cgroup_from_id", BTF_KIND_FUNC, &kfunc_btf);
1952 	bpf_cgroup_release_id = bpf_find_btf_id("bpf_cgroup_release", BTF_KIND_FUNC, &kfunc_btf);
1953 	if (bpf_cgroup_from_id_id < 0 || bpf_cgroup_release_id < 0)
1954 		return -EINVAL;
1955 
1956 	return 0;
1957 }
1958 
st_ops_init_member(const struct btf_type * t,const struct btf_member * member,void * kdata,const void * udata)1959 static int st_ops_init_member(const struct btf_type *t,
1960 			      const struct btf_member *member,
1961 			      void *kdata, const void *udata)
1962 {
1963 	return 0;
1964 }
1965 
1966 static struct bpf_struct_ops testmod_st_ops = {
1967 	.verifier_ops = &st_ops_verifier_ops,
1968 	.init = st_ops_init,
1969 	.init_member = st_ops_init_member,
1970 	.reg = st_ops_reg,
1971 	.unreg = st_ops_unreg,
1972 	.cfi_stubs = &st_ops_cfi_stubs,
1973 	.name = "bpf_testmod_st_ops",
1974 	.owner = THIS_MODULE,
1975 };
1976 
1977 struct hlist_head multi_st_ops_list;
1978 static DEFINE_SPINLOCK(multi_st_ops_lock);
1979 
multi_st_ops_init(struct btf * btf)1980 static int multi_st_ops_init(struct btf *btf)
1981 {
1982 	spin_lock_init(&multi_st_ops_lock);
1983 	INIT_HLIST_HEAD(&multi_st_ops_list);
1984 
1985 	return 0;
1986 }
1987 
multi_st_ops_init_member(const struct btf_type * t,const struct btf_member * member,void * kdata,const void * udata)1988 static int multi_st_ops_init_member(const struct btf_type *t,
1989 				    const struct btf_member *member,
1990 				    void *kdata, const void *udata)
1991 {
1992 	return 0;
1993 }
1994 
multi_st_ops_find_nolock(u32 id)1995 static struct bpf_testmod_multi_st_ops *multi_st_ops_find_nolock(u32 id)
1996 {
1997 	struct bpf_testmod_multi_st_ops *st_ops;
1998 
1999 	hlist_for_each_entry(st_ops, &multi_st_ops_list, node) {
2000 		if (st_ops->id == id)
2001 			return st_ops;
2002 	}
2003 
2004 	return NULL;
2005 }
2006 
2007 /* Call test_1() of the struct_ops map identified by the id */
bpf_kfunc_multi_st_ops_test_1(struct st_ops_args * args,u32 id)2008 int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id)
2009 {
2010 	struct bpf_testmod_multi_st_ops *st_ops;
2011 	unsigned long flags;
2012 	int ret = -1;
2013 
2014 	spin_lock_irqsave(&multi_st_ops_lock, flags);
2015 	st_ops = multi_st_ops_find_nolock(id);
2016 	if (st_ops)
2017 		ret = st_ops->test_1(args);
2018 	spin_unlock_irqrestore(&multi_st_ops_lock, flags);
2019 
2020 	return ret;
2021 }
2022 
2023 /* Call test_1() of the associated struct_ops map */
bpf_kfunc_multi_st_ops_test_1_assoc(struct st_ops_args * args,struct bpf_prog_aux * aux)2024 int bpf_kfunc_multi_st_ops_test_1_assoc(struct st_ops_args *args, struct bpf_prog_aux *aux)
2025 {
2026 	struct bpf_testmod_multi_st_ops *st_ops;
2027 	int ret = -1;
2028 
2029 	st_ops = (struct bpf_testmod_multi_st_ops *)bpf_prog_get_assoc_struct_ops(aux);
2030 	if (st_ops)
2031 		ret = st_ops->test_1(args);
2032 
2033 	return ret;
2034 }
2035 
bpf_kfunc_implicit_arg(int a,struct bpf_prog_aux * aux)2036 int bpf_kfunc_implicit_arg(int a, struct bpf_prog_aux *aux)
2037 {
2038 	if (aux && a > 0)
2039 		return a;
2040 	return -EINVAL;
2041 }
2042 
bpf_kfunc_implicit_arg_legacy(int a,int b,struct bpf_prog_aux * aux)2043 int bpf_kfunc_implicit_arg_legacy(int a, int b, struct bpf_prog_aux *aux)
2044 {
2045 	if (aux)
2046 		return a + b;
2047 	return -EINVAL;
2048 }
2049 
bpf_kfunc_implicit_arg_legacy_impl(int a,int b,struct bpf_prog_aux * aux)2050 int bpf_kfunc_implicit_arg_legacy_impl(int a, int b, struct bpf_prog_aux *aux)
2051 {
2052 	return bpf_kfunc_implicit_arg_legacy(a, b, aux);
2053 }
2054 
multi_st_ops_reg(void * kdata,struct bpf_link * link)2055 static int multi_st_ops_reg(void *kdata, struct bpf_link *link)
2056 {
2057 	struct bpf_testmod_multi_st_ops *st_ops =
2058 		(struct bpf_testmod_multi_st_ops *)kdata;
2059 	unsigned long flags;
2060 	int err = 0;
2061 	u32 id;
2062 
2063 	if (!st_ops->test_1)
2064 		return -EINVAL;
2065 
2066 	id = bpf_struct_ops_id(kdata);
2067 
2068 	spin_lock_irqsave(&multi_st_ops_lock, flags);
2069 	if (multi_st_ops_find_nolock(id)) {
2070 		pr_err("multi_st_ops(id:%d) has already been registered\n", id);
2071 		err = -EEXIST;
2072 		goto unlock;
2073 	}
2074 
2075 	st_ops->id = id;
2076 	hlist_add_head(&st_ops->node, &multi_st_ops_list);
2077 unlock:
2078 	spin_unlock_irqrestore(&multi_st_ops_lock, flags);
2079 
2080 	return err;
2081 }
2082 
multi_st_ops_unreg(void * kdata,struct bpf_link * link)2083 static void multi_st_ops_unreg(void *kdata, struct bpf_link *link)
2084 {
2085 	struct bpf_testmod_multi_st_ops *st_ops;
2086 	unsigned long flags;
2087 	u32 id;
2088 
2089 	id = bpf_struct_ops_id(kdata);
2090 
2091 	spin_lock_irqsave(&multi_st_ops_lock, flags);
2092 	st_ops = multi_st_ops_find_nolock(id);
2093 	if (st_ops)
2094 		hlist_del(&st_ops->node);
2095 	spin_unlock_irqrestore(&multi_st_ops_lock, flags);
2096 }
2097 
bpf_testmod_multi_st_ops__test_1(struct st_ops_args * args)2098 static int bpf_testmod_multi_st_ops__test_1(struct st_ops_args *args)
2099 {
2100 	return 0;
2101 }
2102 
2103 static struct bpf_testmod_multi_st_ops multi_st_ops_cfi_stubs = {
2104 	.test_1 = bpf_testmod_multi_st_ops__test_1,
2105 };
2106 
2107 struct bpf_struct_ops testmod_multi_st_ops = {
2108 	.verifier_ops = &bpf_testmod_verifier_ops,
2109 	.init = multi_st_ops_init,
2110 	.init_member = multi_st_ops_init_member,
2111 	.reg = multi_st_ops_reg,
2112 	.unreg = multi_st_ops_unreg,
2113 	.cfi_stubs = &multi_st_ops_cfi_stubs,
2114 	.name = "bpf_testmod_multi_st_ops",
2115 	.owner = THIS_MODULE,
2116 };
2117 
2118 extern int bpf_fentry_test1(int a);
2119 
2120 BTF_KFUNCS_START(bpf_testmod_trampoline_count_ids)
2121 BTF_ID_FLAGS(func, bpf_testmod_trampoline_count_test)
2122 BTF_KFUNCS_END(bpf_testmod_trampoline_count_ids)
2123 
2124 static const struct
2125 btf_kfunc_id_set bpf_testmod_trampoline_count_fmodret_set = {
2126 	.owner = THIS_MODULE,
2127 	.set = &bpf_testmod_trampoline_count_ids,
2128 };
2129 
bpf_testmod_init(void)2130 static int bpf_testmod_init(void)
2131 {
2132 	const struct btf_id_dtor_kfunc bpf_testmod_dtors[] = {
2133 		{
2134 			.btf_id		= bpf_testmod_dtor_ids[0],
2135 			.kfunc_btf_id	= bpf_testmod_dtor_ids[1]
2136 		},
2137 	};
2138 	void **tramp;
2139 	int ret;
2140 
2141 	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &bpf_testmod_common_kfunc_set);
2142 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
2143 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_testmod_kfunc_set);
2144 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_testmod_kfunc_set);
2145 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_testmod_kfunc_set);
2146 	ret = ret ?: register_btf_fmodret_id_set(&bpf_testmod_trampoline_count_fmodret_set);
2147 	ret = ret ?: register_bpf_struct_ops(&bpf_bpf_testmod_ops, bpf_testmod_ops);
2148 	ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops2, bpf_testmod_ops2);
2149 	ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops3, bpf_testmod_ops3);
2150 	ret = ret ?: register_bpf_struct_ops(&testmod_st_ops, bpf_testmod_st_ops);
2151 	ret = ret ?: register_bpf_struct_ops(&testmod_multi_st_ops, bpf_testmod_multi_st_ops);
2152 	ret = ret ?: register_btf_id_dtor_kfuncs(bpf_testmod_dtors,
2153 						 ARRAY_SIZE(bpf_testmod_dtors),
2154 						 THIS_MODULE);
2155 	if (ret < 0)
2156 		return ret;
2157 	if (bpf_fentry_test1(0) < 0)
2158 		return -EINVAL;
2159 	sock = NULL;
2160 	mutex_init(&sock_lock);
2161 	ret = sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
2162 	if (ret < 0)
2163 		return ret;
2164 	ret = register_bpf_testmod_uprobe();
2165 	if (ret < 0)
2166 		return ret;
2167 
2168 	/* Ensure nothing is between tramp_1..tramp_40 */
2169 	BUILD_BUG_ON(offsetof(struct bpf_testmod_ops, tramp_1) + 40 * sizeof(long) !=
2170 		     offsetofend(struct bpf_testmod_ops, tramp_40));
2171 	tramp = (void **)&__bpf_testmod_ops.tramp_1;
2172 	while (tramp <= (void **)&__bpf_testmod_ops.tramp_40)
2173 		*tramp++ = bpf_testmod_tramp;
2174 
2175 	return 0;
2176 }
2177 
bpf_testmod_exit(void)2178 static void bpf_testmod_exit(void)
2179 {
2180         /* Need to wait for all references to be dropped because
2181          * bpf_kfunc_call_test_release() which currently resides in kernel can
2182          * be called after bpf_testmod is unloaded. Once release function is
2183          * moved into the module this wait can be removed.
2184          */
2185 	while (refcount_read(&prog_test_struct.cnt) > 1)
2186 		msleep(20);
2187 
2188 	/* Clean up irqwork and tasklet */
2189 	irq_work_sync(&ctx_check_irq);
2190 	tasklet_kill(&ctx_check_tasklet);
2191 
2192 	bpf_kfunc_close_sock();
2193 	sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
2194 	unregister_bpf_testmod_uprobe();
2195 }
2196 
2197 module_init(bpf_testmod_init);
2198 module_exit(bpf_testmod_exit);
2199 
2200 MODULE_AUTHOR("Andrii Nakryiko");
2201 MODULE_DESCRIPTION("BPF selftests module");
2202 MODULE_LICENSE("Dual BSD/GPL");
2203