Skip to content

Commit 87091dd

Browse files
etsalAlexei Starovoitov
authored andcommitted
selftests/bpf: test bpf_for within spin lock section
Add a selftest to ensure BPF for loops within critical sections are accepted by the verifier. Signed-off-by: Emil Tsalapatis (Meta) <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 5128164 commit 87091dd

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,4 +530,30 @@ l1_%=: exit; \
530530
: __clobber_all);
531531
}
532532

533+
SEC("tc")
534+
__description("spin_lock: loop within a locked region")
535+
__success __failure_unpriv __msg_unpriv("")
536+
__retval(0)
537+
int bpf_loop_inside_locked_region(void)
538+
{
539+
const int zero = 0;
540+
struct val *val;
541+
int i, j = 0;
542+
543+
val = bpf_map_lookup_elem(&map_spin_lock, &zero);
544+
if (!val)
545+
return -1;
546+
547+
bpf_spin_lock(&val->l);
548+
bpf_for(i, 0, 10) {
549+
j++;
550+
/* Silence "unused variable" warnings. */
551+
if (j == 10)
552+
break;
553+
}
554+
bpf_spin_unlock(&val->l);
555+
556+
return 0;
557+
}
558+
533559
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)