selftests/bpf: Add struct_ops prog private stack tests
authorYonghong Song <yonghong.song@linux.dev>
Tue, 12 Nov 2024 16:39:38 +0000 (08:39 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 13 Nov 2024 00:26:25 +0000 (16:26 -0800)
commitbecfe32b57c7d323fbd94c1a2c6d7eba918ddde8
tree5dcd3ec67066d92d5e15e0165efcdea90375de30
parent5bd36da1e37e7a78e8b38efd287de6e1394b7d6e
selftests/bpf: Add struct_ops prog private stack tests

Add three tests for struct_ops using private stack.
  ./test_progs -t struct_ops_private_stack
  #336/1   struct_ops_private_stack/private_stack:OK
  #336/2   struct_ops_private_stack/private_stack_fail:OK
  #336/3   struct_ops_private_stack/private_stack_recur:OK
  #336     struct_ops_private_stack:OK

The following is a snippet of a struct_ops check_member() implementation:

u32 moff = __btf_member_bit_offset(t, member) / 8;
switch (moff) {
case offsetof(struct bpf_testmod_ops3, test_1):
         prog->aux->priv_stack_requested = true;
                prog->aux->recursion_detected = test_1_recursion_detected;
         fallthrough;
default:
         break;
}
return 0;

The first test is with nested two different callback functions where the
first prog has more than 512 byte stack size (including subprogs) with
private stack enabled.

The second test is a negative test where the second prog has more than 512
byte stack size without private stack enabled.

The third test is the same callback function recursing itself. At run time,
the jit trampoline recursion check kicks in to prevent the recursion. The
recursion_detected() callback function is implemented by the bpf_testmod,
the following message in dmesg
  bpf_testmod: oh no, recursing into test_1, recursion_misses 1
demonstrates the callback function is indeed triggered when recursion miss
happens.

Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20241112163938.2225528-1-yonghong.song@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/struct_ops_private_stack.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/struct_ops_private_stack_recur.c [new file with mode: 0644]