Skip to content

Commit

Permalink
selftests/bpf: test bpf_for within spin lock section
Browse files Browse the repository at this point in the history
Add a selftest to ensure BPF for loops within critical sections are
accepted by the verifier.

Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
  • Loading branch information
etsal authored and Kernel Patches Daemon committed Jan 4, 2025
1 parent 7f94a21 commit 1c15586
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tools/testing/selftests/bpf/progs/verifier_spin_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,30 @@ l1_%=: exit; \
: __clobber_all);
}

SEC("tc")
__description("spin_lock: loop within a locked region")
__success __failure_unpriv __msg_unpriv("")
__retval(0)
int bpf_loop_inside_locked_region(void)
{
const int zero = 0;
struct val *val;
int i, j = 0;

val = bpf_map_lookup_elem(&map_spin_lock, &zero);
if (!val)
return -1;

bpf_spin_lock(&val->l);
bpf_for(i, 0, 10) {
j++;
/* Silence "unused variable" warnings. */
if (j == 10)
break;
}
bpf_spin_unlock(&val->l);

return 0;
}

char _license[] SEC("license") = "GPL";

0 comments on commit 1c15586

Please sign in to comment.