1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
5 #include <bpf/bpf_helpers.h>
8 char _license[] SEC("license") = "GPL";
15 __uint(type, BPF_MAP_TYPE_HASH);
16 __uint(max_entries, 32);
21 /* These should be set by the user program */
22 u32 nested_callback_nr_loops;
26 int callback_selector;
28 /* Making these global variables so that the userspace program
29 * can verify the output through the skeleton
31 int nr_loops_returned;
35 static int callback(__u32 index, void *data)
37 struct callback_ctx *ctx = data;
39 if (index >= stop_index)
47 static int empty_callback(__u32 index, void *data)
52 static int nested_callback2(__u32 index, void *data)
54 nr_loops_returned += bpf_loop(nested_callback_nr_loops, callback, data, 0);
59 static int nested_callback1(__u32 index, void *data)
61 bpf_loop(nested_callback_nr_loops, nested_callback2, data, 0);
65 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
66 int test_prog(void *ctx)
68 struct callback_ctx data = {};
70 if (bpf_get_current_pid_tgid() >> 32 != pid)
73 nr_loops_returned = bpf_loop(nr_loops, callback, &data, 0);
75 if (nr_loops_returned < 0)
76 err = nr_loops_returned;
78 g_output = data.output;
83 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
84 int prog_null_ctx(void *ctx)
86 if (bpf_get_current_pid_tgid() >> 32 != pid)
89 nr_loops_returned = bpf_loop(nr_loops, empty_callback, NULL, 0);
94 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
95 int prog_invalid_flags(void *ctx)
97 struct callback_ctx data = {};
99 if (bpf_get_current_pid_tgid() >> 32 != pid)
102 err = bpf_loop(nr_loops, callback, &data, 1);
107 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
108 int prog_nested_calls(void *ctx)
110 struct callback_ctx data = {};
112 if (bpf_get_current_pid_tgid() >> 32 != pid)
115 nr_loops_returned = 0;
116 bpf_loop(nr_loops, nested_callback1, &data, 0);
118 g_output = data.output;
123 static int callback_set_f0(int i, void *ctx)
129 static int callback_set_0f(int i, void *ctx)
136 * non-constant callback is a corner case for bpf_loop inline logic
138 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
139 int prog_non_constant_callback(void *ctx)
141 if (bpf_get_current_pid_tgid() >> 32 != pid)
144 int (*callback)(int i, void *ctx);
148 if (callback_selector == 0x0F)
149 callback = callback_set_0f;
151 callback = callback_set_f0;
153 bpf_loop(1, callback, NULL, 0);
158 static int stack_check_inner_callback(void *ctx)
163 static int map1_lookup_elem(int key)
165 int *val = bpf_map_lookup_elem(&map1, &key);
167 return val ? *val : -1;
170 static void map1_update_elem(int key, int val)
172 bpf_map_update_elem(&map1, &key, &val, BPF_ANY);
175 static int stack_check_outer_callback(void *ctx)
177 int a = map1_lookup_elem(1);
178 int b = map1_lookup_elem(2);
179 int c = map1_lookup_elem(3);
180 int d = map1_lookup_elem(4);
181 int e = map1_lookup_elem(5);
182 int f = map1_lookup_elem(6);
184 bpf_loop(1, stack_check_inner_callback, NULL, 0);
186 map1_update_elem(1, a + 1);
187 map1_update_elem(2, b + 1);
188 map1_update_elem(3, c + 1);
189 map1_update_elem(4, d + 1);
190 map1_update_elem(5, e + 1);
191 map1_update_elem(6, f + 1);
196 /* Some of the local variables in stack_check and
197 * stack_check_outer_callback would be allocated on stack by
198 * compiler. This test should verify that stack content for these
199 * variables is preserved between calls to bpf_loop (might be an issue
200 * if loop inlining allocates stack slots incorrectly).
202 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
203 int stack_check(void *ctx)
205 if (bpf_get_current_pid_tgid() >> 32 != pid)
208 int a = map1_lookup_elem(7);
209 int b = map1_lookup_elem(8);
210 int c = map1_lookup_elem(9);
211 int d = map1_lookup_elem(10);
212 int e = map1_lookup_elem(11);
213 int f = map1_lookup_elem(12);
215 bpf_loop(1, stack_check_outer_callback, NULL, 0);
217 map1_update_elem(7, a + 1);
218 map1_update_elem(8, b + 1);
219 map1_update_elem(9, c + 1);
220 map1_update_elem(10, d + 1);
221 map1_update_elem(11, e + 1);
222 map1_update_elem(12, f + 1);