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