From: Mike Bazov Date: Wed, 02 Dec 2026 15:15:15 +0200 Subject: [PATCH bpf-next 3/4] selftests/bpf: Test private-stack struct_ops progs in prog arrays MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261202-followup-rt-stops-v1-3-22562be9ddb0@gmail.com> References: <20261202-followup-rt-stops-v1-0-22562be9ddb0@gmail.com> In-Reply-To: <20261202-followup-rt-stops-v1-0-22562be9ddb0@gmail.com> To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Eduard Zingerman , Kumar Kartikeya Dwivedi , Song Liu , Yonghong Song , Jiri Olsa , John Fastabend , Emil Tsalapatis , Ihor Solodrai , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , JP Kobryn , linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, Mike Bazov X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796217344; l=4242; i=mike.bazov@gmail.com; s=gmail-20261116; h=from:subject:message-id; bh=OV0DAx9zI4jng0yZeAoZGlSyYQdcWr5kChjmBeNtIZA=; b=q7lYCgaC1yztpaPkS+zklMODkoft6BVx51hmyabvim1ehdapOOdHLGpW0K8fVxgDzwvBw5+Fk 9G5JuuTSMGTCP31WINN9+q2RMX+xTrW1MiVzYOOKFlzJuZ6ROxnT+oZ X-Developer-Key: i=mike.bazov@gmail.com; a=ed25519; pk=Rr9fagn0pNA+bs8tgc6DXZpRAhl/maU6BeqkttIR7DA= X-Endpoint-Received: by B4 Relay for mike.bazov@gmail.com/gmail-20261116 with auth_id=1544 List-Id: B4 Relay Submissions 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