From: Mike Bazov Load two bpf_testmod test_1 programs, which request a private stack: one with 400 bytes of stack, which gets it, and one under BPF_PRIV_STACK_MIN_SIZE, which doesn't. The small one goes into a prog array first, so that the big one is compatible with the array; the big one is then refused with -EINVAL. Signed-off-by: Mike Bazov --- .../bpf/prog_tests/struct_ops_private_stack.c | 34 +++++++++++++++ .../bpf/progs/struct_ops_private_stack_tail_call.c | 49 ++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c index 103179ff873..84ce5623467 100644 --- a/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c @@ -5,6 +5,7 @@ #include "struct_ops_private_stack_fail.skel.h" #include "struct_ops_private_stack_recur.skel.h" #include "struct_ops_private_stack_large.skel.h" +#include "struct_ops_private_stack_tail_call.skel.h" #if defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__) || \ (defined(__riscv) && __riscv_xlen == 64) @@ -108,6 +109,37 @@ static void test_private_stack_large(void) struct_ops_private_stack_large__destroy(skel); } +/* + * A tail call enters its target past the trampoline, which keeps two runs of a + * prog off its private stack on a CPU, so a prog with a private stack can't be + * a tail call target. The small one has none and goes in first, so that the + * big one would be compatible with the prog array. + */ +static void test_private_stack_tail_call(void) +{ + struct struct_ops_private_stack_tail_call *skel; + int map_fd, fd, err; + __u32 key; + + skel = struct_ops_private_stack_tail_call__open_and_load(); + if (!ASSERT_OK_PTR(skel, "struct_ops_private_stack_tail_call__open_and_load")) + return; + + map_fd = bpf_map__fd(skel->maps.prog_array); + + key = 0; + fd = bpf_program__fd(skel->progs.test_1_small_stack); + err = bpf_map_update_elem(map_fd, &key, &fd, BPF_ANY); + ASSERT_OK(err, "update_small_stack"); + + key = 1; + fd = bpf_program__fd(skel->progs.test_1_big_stack); + err = bpf_map_update_elem(map_fd, &key, &fd, BPF_ANY); + ASSERT_EQ(err, -EINVAL, "update_big_stack"); + + struct_ops_private_stack_tail_call__destroy(skel); +} + static void __test_struct_ops_private_stack(void) { if (test__start_subtest("private_stack")) @@ -118,6 +150,8 @@ static void __test_struct_ops_private_stack(void) test_private_stack_recur(); if (test__start_subtest("private_stack_large")) test_private_stack_large(); + if (test__start_subtest("private_stack_tail_call")) + test_private_stack_tail_call(); } #else static void __test_struct_ops_private_stack(void) diff --git a/tools/testing/selftests/bpf/progs/struct_ops_private_stack_tail_call.c b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_tail_call.c new file mode 100644 index 00000000000..69dc65090e3 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_tail_call.c @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include "../test_kmods/bpf_testmod.h" + +char _license[] SEC("license") = "GPL"; + +int val; + +/* 400 bytes of stack: test_1 requests a private stack, and this one gets it */ +SEC("struct_ops") +int BPF_PROG(test_1_big_stack) +{ + volatile int a[100] = {}; + + a[10] = 1; + val = a[10]; + return 0; +} + +/* Under BPF_PRIV_STACK_MIN_SIZE: no private stack */ +SEC("struct_ops") +int BPF_PROG(test_1_small_stack) +{ + volatile int a[4] = {}; + + a[1] = 2; + val = a[1]; + return 0; +} + +SEC(".struct_ops") +struct bpf_testmod_ops3 testmod_big = { + .test_1 = (void *)test_1_big_stack, +}; + +SEC(".struct_ops") +struct bpf_testmod_ops3 testmod_small = { + .test_1 = (void *)test_1_small_stack, +}; + +struct { + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); + __uint(max_entries, 2); + __type(key, __u32); + __type(value, __u32); +} prog_array SEC(".maps"); -- 2.43.0