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