xref: /linux/tools/testing/selftests/bpf/progs/dummy_st_ops_success.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021. Huawei Technologies Co., Ltd */
3 #include "vmlinux.h"
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 
7 char _license[] SEC("license") = "GPL";
8 
9 SEC("struct_ops/test_1")
10 int BPF_PROG(test_1, struct bpf_dummy_ops_state *state)
11 {
12 	int ret;
13 
14 	/* Check that 'state' nullable status is detected correctly.
15 	 * If 'state' argument would be assumed non-null by verifier
16 	 * the code below would be deleted as dead (which it shouldn't).
17 	 * Hide it from the compiler behind 'asm' block to avoid
18 	 * unnecessary optimizations.
19 	 */
20 	asm volatile (
21 		"if %[state] != 0 goto +2;"
22 		"r0 = 0xf2f3f4f5;"
23 		"exit;"
24 	::[state]"p"(state));
25 
26 	ret = state->val;
27 	state->val = 0x5a;
28 	return ret;
29 }
30 
31 __u64 test_2_args[5];
32 
33 SEC("struct_ops/test_2")
34 int BPF_PROG(test_2, struct bpf_dummy_ops_state *state, int a1, unsigned short a2,
35 	     char a3, unsigned long a4)
36 {
37 	test_2_args[0] = state->val;
38 	test_2_args[1] = a1;
39 	test_2_args[2] = a2;
40 	test_2_args[3] = a3;
41 	test_2_args[4] = a4;
42 	return 0;
43 }
44 
45 SEC("struct_ops.s/test_sleepable")
46 int BPF_PROG(test_sleepable, struct bpf_dummy_ops_state *state)
47 {
48 	return 0;
49 }
50 
51 SEC(".struct_ops")
52 struct bpf_dummy_ops dummy_1 = {
53 	.test_1 = (void *)test_1,
54 	.test_2 = (void *)test_2,
55 	.test_sleepable = (void *)test_sleepable,
56 };
57