Skip to content

Commit 0c8bbf9

Browse files
Alexei Starovoitovanakryiko
authored andcommitted
selftests/bpf: Test may_goto
Add tests for may_goto instruction via cond_break macro. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: John Fastabend <john.fastabend@gmail.com> Tested-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20240306031929.42666-5-alexei.starovoitov@gmail.com
1 parent 0637580 commit 0c8bbf9

2 files changed

Lines changed: 101 additions & 3 deletions

File tree

tools/testing/selftests/bpf/DENYLIST.s390x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
exceptions # JIT does not support calling kfunc bpf_throw (exceptions)
44
get_stack_raw_tp # user_stack corrupted user stack (no backchain userspace)
55
stacktrace_build_id # compare_map_keys stackid_hmap vs. stackmap err -2 errno 2 (?)
6+
verifier_iterating_callbacks

tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
2-
3-
#include <linux/bpf.h>
4-
#include <bpf/bpf_helpers.h>
52
#include "bpf_misc.h"
3+
#include "bpf_experimental.h"
64

75
struct {
86
__uint(type, BPF_MAP_TYPE_ARRAY);
@@ -239,4 +237,103 @@ int bpf_loop_iter_limit_nested(void *unused)
239237
return 1000 * a + b + c;
240238
}
241239

240+
#define ARR_SZ 1000000
241+
int zero;
242+
char arr[ARR_SZ];
243+
244+
SEC("socket")
245+
__success __retval(0xd495cdc0)
246+
int cond_break1(const void *ctx)
247+
{
248+
unsigned long i;
249+
unsigned int sum = 0;
250+
251+
for (i = zero; i < ARR_SZ; cond_break, i++)
252+
sum += i;
253+
for (i = zero; i < ARR_SZ; i++) {
254+
barrier_var(i);
255+
sum += i + arr[i];
256+
cond_break;
257+
}
258+
259+
return sum;
260+
}
261+
262+
SEC("socket")
263+
__success __retval(999000000)
264+
int cond_break2(const void *ctx)
265+
{
266+
int i, j;
267+
int sum = 0;
268+
269+
for (i = zero; i < 1000; cond_break, i++)
270+
for (j = zero; j < 1000; j++) {
271+
sum += i + j;
272+
cond_break;
273+
}
274+
275+
return sum;
276+
}
277+
278+
static __noinline int loop(void)
279+
{
280+
int i, sum = 0;
281+
282+
for (i = zero; i <= 1000000; i++, cond_break)
283+
sum += i;
284+
285+
return sum;
286+
}
287+
288+
SEC("socket")
289+
__success __retval(0x6a5a2920)
290+
int cond_break3(const void *ctx)
291+
{
292+
return loop();
293+
}
294+
295+
SEC("socket")
296+
__success __retval(1)
297+
int cond_break4(const void *ctx)
298+
{
299+
int cnt = zero;
300+
301+
for (;;) {
302+
/* should eventually break out of the loop */
303+
cond_break;
304+
cnt++;
305+
}
306+
/* if we looped a bit, it's a success */
307+
return cnt > 1 ? 1 : 0;
308+
}
309+
310+
static __noinline int static_subprog(void)
311+
{
312+
int cnt = zero;
313+
314+
for (;;) {
315+
cond_break;
316+
cnt++;
317+
}
318+
319+
return cnt;
320+
}
321+
322+
SEC("socket")
323+
__success __retval(1)
324+
int cond_break5(const void *ctx)
325+
{
326+
int cnt1 = zero, cnt2;
327+
328+
for (;;) {
329+
cond_break;
330+
cnt1++;
331+
}
332+
333+
cnt2 = static_subprog();
334+
335+
/* main and subprog have to loop a bit */
336+
return cnt1 > 1 && cnt2 > 1 ? 1 : 0;
337+
}
338+
242339
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)