* [PATCH bpf-next 0/4] bpf: Per-task recursion follow-ups for struct_ops and PREEMPT_RT
@ 2026-12-02 13:15 ` Mike Bazov
0 siblings, 0 replies; 12+ messages in thread
From: Mike Bazov @ 2026-12-02 13:15 UTC (permalink / raw)
To: bpf, 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, linux-kernel,
Mike Bazov
Two follow-ups to "bpf: Check recursion per task for preemptible
programs", per Alexei's review [1].
Patch 1 fixes an existing hole: a struct_ops program with a private
stack can be the target of a tail call, which enters it past the
trampoline and its recursion check. The caller is a struct_ops program
of the same op without a private stack, so it has no recursion check,
and two tasks on one CPU can run the target on the same private stack.
Such a program is now refused as a prog array element.
Patch 2 keeps marked programs preemptible on PREEMPT_RT: instead of
disabling preemption, a per-CPU counter refuses another run of the
program on the CPU, and the refused run is counted as a recursion
miss.
1. bpf: Refuse struct_ops programs with a private stack in prog arrays
2. bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT
3. selftests/bpf: Test private-stack struct_ops progs in prog arrays
4. selftests/bpf: Expect marked programs to drop B on PREEMPT_RT
Testing, x86-64, with and without PREEMPT_RT:
With the series, private_stack_tail_call and the trampoline_preempt
subtests pass on both; timer_cb skips on PREEMPT_RT, as before. Without
patch 1, private_stack_tail_call fails: the program with a private
stack goes into the prog array. On PREEMPT_RT without patch 2, the nine
subtests of marked programs fail: B runs after A instead of being
dropped. Without PREEMPT_RT, test_progs shows no new failures.
[1] https://lore.kernel.org/bpf/DN9PF7JVRP7H.262PMB9O02SBG@gmail.com/
---
Mike Bazov (4):
bpf: Refuse struct_ops programs with a private stack in prog arrays
bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT
selftests/bpf: Test private-stack struct_ops progs in prog arrays
selftests/bpf: Expect marked programs to drop B on PREEMPT_RT
include/linux/bpf.h | 33 +++++++++++----
include/linux/filter.h | 7 ----
kernel/bpf/arraymap.c | 1 +
.../bpf/prog_tests/struct_ops_private_stack.c | 34 +++++++++++++++
.../selftests/bpf/prog_tests/trampoline_preempt.c | 35 +++++++++++++---
.../bpf/progs/struct_ops_private_stack_tail_call.c | 49 ++++++++++++++++++++++
6 files changed, 138 insertions(+), 21 deletions(-)
---
base-commit: 23eac23279b6c114ad70a176b6dd546e88a981f8
change-id: 20261202-followup-rt-stops-02dc25401b3d
Best regards,
--
Mike Bazov <mike.bazov@gmail.com>
^ permalink raw reply [flat|nested] 12+ messages in thread* [PATCH bpf-next 1/4] bpf: Refuse struct_ops programs with a private stack in prog arrays
2026-12-02 13:15 ` Mike Bazov
@ 2026-12-02 13:15 ` Mike Bazov
-1 siblings, 0 replies; 12+ messages in thread
From: Mike Bazov via B4 Relay @ 2026-12-02 13:15 UTC (permalink / raw)
To: bpf, 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, linux-kernel,
Mike Bazov
From: Mike Bazov <mike.bazov@gmail.com>
A struct_ops program gets a private stack only if the subsystem requests
one, and then its trampoline runs it with a recursion check, so two runs
of it on one CPU never share the stack. A tail call enters the program
past the trampoline. The caller is a struct_ops program of the same op
and has no private stack, since a program with a tail call gets none,
so it has no recursion check: two tasks on one CPU can be inside it and
both tail-call into the target, which then runs twice on the same
private stack.
Refuse a struct_ops program that uses a private stack as a prog array
element.
Fixes: 5bd36da1e37e ("bpf: Support private stack for struct_ops progs")
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Mike Bazov <mike.bazov@gmail.com>
---
kernel/bpf/arraymap.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 508706d0c36..564a736275a 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -1044,6 +1044,7 @@ static void *prog_fd_array_get_ptr(struct bpf_map *map,
return prog;
if (prog->type == BPF_PROG_TYPE_EXT ||
+ (prog->type == BPF_PROG_TYPE_STRUCT_OPS && prog->aux->jits_use_priv_stack) ||
!bpf_prog_map_compatible(map, prog)) {
bpf_prog_put(prog);
return ERR_PTR(-EINVAL);
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH bpf-next 1/4] bpf: Refuse struct_ops programs with a private stack in prog arrays
@ 2026-12-02 13:15 ` Mike Bazov
0 siblings, 0 replies; 12+ messages in thread
From: Mike Bazov @ 2026-12-02 13:15 UTC (permalink / raw)
To: bpf, 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, linux-kernel,
Mike Bazov
A struct_ops program gets a private stack only if the subsystem requests
one, and then its trampoline runs it with a recursion check, so two runs
of it on one CPU never share the stack. A tail call enters the program
past the trampoline. The caller is a struct_ops program of the same op
and has no private stack, since a program with a tail call gets none,
so it has no recursion check: two tasks on one CPU can be inside it and
both tail-call into the target, which then runs twice on the same
private stack.
Refuse a struct_ops program that uses a private stack as a prog array
element.
Fixes: 5bd36da1e37e ("bpf: Support private stack for struct_ops progs")
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Mike Bazov <mike.bazov@gmail.com>
---
kernel/bpf/arraymap.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 508706d0c36..564a736275a 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -1044,6 +1044,7 @@ static void *prog_fd_array_get_ptr(struct bpf_map *map,
return prog;
if (prog->type == BPF_PROG_TYPE_EXT ||
+ (prog->type == BPF_PROG_TYPE_STRUCT_OPS && prog->aux->jits_use_priv_stack) ||
!bpf_prog_map_compatible(map, prog)) {
bpf_prog_put(prog);
return ERR_PTR(-EINVAL);
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH bpf-next 2/4] bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT
2026-12-02 13:15 ` Mike Bazov
@ 2026-12-02 13:15 ` Mike Bazov
-1 siblings, 0 replies; 12+ messages in thread
From: Mike Bazov via B4 Relay @ 2026-12-02 13:15 UTC (permalink / raw)
To: bpf, 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, linux-kernel,
Mike Bazov
From: Mike Bazov <mike.bazov@gmail.com>
A program with per-CPU state runs with preemption disabled, so that no
other run of it starts on its CPU before it exits. On PREEMPT_RT that
makes the whole program, and everything it calls, non-preemptible.
Keep such a program preemptible on PREEMPT_RT, and refuse another run of
it on the CPU with a per-CPU counter in its stats instead. A refused run
is counted as a recursion miss. Move struct bpf_prog_stats to bpf.h,
where bpf_prog_enter_run_ctx() uses the counter.
Trade-off: on PREEMPT_RT, a marked program drops the runs of other tasks
that start on its CPU while it is preempted inside it.
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Mike Bazov <mike.bazov@gmail.com>
---
include/linux/bpf.h | 33 ++++++++++++++++++++++++---------
include/linux/filter.h | 7 -------
2 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 0fef8e2f43c..7125557fab0 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -37,6 +37,7 @@
#include <linux/xattr.h>
#include <linux/key.h>
#include <linux/ftrace.h>
+#include <linux/u64_stats_sync.h>
#include <asm/rqspinlock.h>
struct bpf_verifier_env;
@@ -2055,6 +2056,14 @@ struct bpf_prog_aux {
struct bpf_map __rcu *st_ops_assoc;
};
+struct bpf_prog_stats {
+ u64_stats_t cnt;
+ u64_stats_t nsecs;
+ u64_stats_t misses;
+ struct u64_stats_sync syncp;
+ int active; /* PREEMPT_RT only, see bpf_prog_enter_run_ctx() */
+} __aligned(2 * sizeof(u64));
+
struct bpf_prog {
u16 pages; /* Number of allocated pages */
u32 jited:1, /* Is our filter JIT'ed? */
@@ -2859,10 +2868,12 @@ static __always_inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
* task they interrupt, so recursion through them is caught too.
*
* A program with per-CPU state (prog->percpu_state) runs with preemption
- * disabled, so that no other run of it starts on this CPU before it exits. A
- * sleepable one has to use bpf_preempt_disable() itself. The frame of a
- * non-sleepable one is linked only while preemption is disabled: a task switch
- * runs the scheduler, and a program there must not find @prog in the chain.
+ * disabled, so that no other run of it starts on this CPU before it exits, and
+ * its frame is linked only while preemption is disabled: a task switch runs the
+ * scheduler, and a program there must not find @prog in the chain. On
+ * PREEMPT_RT it stays preemptible, and a per-CPU counter refuses another run of
+ * it on this CPU instead. A sleepable one has to use bpf_preempt_disable()
+ * itself.
*
* Returns false if @prog must not run. Call bpf_prog_exit_run_ctx() either way.
* Always inline: the callers are notrace.
@@ -2877,10 +2888,12 @@ static __always_inline bool bpf_prog_enter_run_ctx(struct bpf_prog *prog,
#endif
run_ctx->saved_run_ctx = old_ctx;
run_ctx->prog = prog;
- if (prog->percpu_state) {
- if (!prog->sleepable)
+ /* sleepable prog should be using explicit bpf_preempt_disable() */
+ if (prog->percpu_state && !prog->sleepable) {
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT))
preempt_disable_notrace();
- /* sleepable prog should be using explicit bpf_preempt_disable() */
+ else if (this_cpu_inc_return(prog->stats->active) != 1)
+ return false;
}
for (ctx = old_ctx; ctx; ctx = ctx->saved_run_ctx) {
if (ctx->prog == prog)
@@ -2899,10 +2912,12 @@ static __always_inline void bpf_prog_exit_run_ctx(struct bpf_prog *prog,
struct bpf_run_ctx *run_ctx)
{
bpf_reset_run_ctx(run_ctx->saved_run_ctx);
- if (!prog->percpu_state)
+ if (!prog->percpu_state || prog->sleepable)
return;
- if (!prog->sleepable)
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT))
preempt_enable_notrace();
+ else
+ this_cpu_dec(prog->stats->active);
}
/* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 06f128632d5..20e7c52b741 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -807,13 +807,6 @@ struct bpf_binary_header {
u8 image[] __aligned(BPF_IMAGE_ALIGNMENT);
};
-struct bpf_prog_stats {
- u64_stats_t cnt;
- u64_stats_t nsecs;
- u64_stats_t misses;
- struct u64_stats_sync syncp;
-} __aligned(2 * sizeof(u64));
-
/*
* Per-cpu memory of a prog. All may_goto of one invocation of the prog use it. An invocation is
* one call from the kernel into the main prog, with the subprogs and sync callbacks it reaches.
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH bpf-next 2/4] bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT
@ 2026-12-02 13:15 ` Mike Bazov
0 siblings, 0 replies; 12+ messages in thread
From: Mike Bazov @ 2026-12-02 13:15 UTC (permalink / raw)
To: bpf, 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, linux-kernel,
Mike Bazov
A program with per-CPU state runs with preemption disabled, so that no
other run of it starts on its CPU before it exits. On PREEMPT_RT that
makes the whole program, and everything it calls, non-preemptible.
Keep such a program preemptible on PREEMPT_RT, and refuse another run of
it on the CPU with a per-CPU counter in its stats instead. A refused run
is counted as a recursion miss. Move struct bpf_prog_stats to bpf.h,
where bpf_prog_enter_run_ctx() uses the counter.
Trade-off: on PREEMPT_RT, a marked program drops the runs of other tasks
that start on its CPU while it is preempted inside it.
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Mike Bazov <mike.bazov@gmail.com>
---
include/linux/bpf.h | 33 ++++++++++++++++++++++++---------
include/linux/filter.h | 7 -------
2 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 0fef8e2f43c..7125557fab0 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -37,6 +37,7 @@
#include <linux/xattr.h>
#include <linux/key.h>
#include <linux/ftrace.h>
+#include <linux/u64_stats_sync.h>
#include <asm/rqspinlock.h>
struct bpf_verifier_env;
@@ -2055,6 +2056,14 @@ struct bpf_prog_aux {
struct bpf_map __rcu *st_ops_assoc;
};
+struct bpf_prog_stats {
+ u64_stats_t cnt;
+ u64_stats_t nsecs;
+ u64_stats_t misses;
+ struct u64_stats_sync syncp;
+ int active; /* PREEMPT_RT only, see bpf_prog_enter_run_ctx() */
+} __aligned(2 * sizeof(u64));
+
struct bpf_prog {
u16 pages; /* Number of allocated pages */
u32 jited:1, /* Is our filter JIT'ed? */
@@ -2859,10 +2868,12 @@ static __always_inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
* task they interrupt, so recursion through them is caught too.
*
* A program with per-CPU state (prog->percpu_state) runs with preemption
- * disabled, so that no other run of it starts on this CPU before it exits. A
- * sleepable one has to use bpf_preempt_disable() itself. The frame of a
- * non-sleepable one is linked only while preemption is disabled: a task switch
- * runs the scheduler, and a program there must not find @prog in the chain.
+ * disabled, so that no other run of it starts on this CPU before it exits, and
+ * its frame is linked only while preemption is disabled: a task switch runs the
+ * scheduler, and a program there must not find @prog in the chain. On
+ * PREEMPT_RT it stays preemptible, and a per-CPU counter refuses another run of
+ * it on this CPU instead. A sleepable one has to use bpf_preempt_disable()
+ * itself.
*
* Returns false if @prog must not run. Call bpf_prog_exit_run_ctx() either way.
* Always inline: the callers are notrace.
@@ -2877,10 +2888,12 @@ static __always_inline bool bpf_prog_enter_run_ctx(struct bpf_prog *prog,
#endif
run_ctx->saved_run_ctx = old_ctx;
run_ctx->prog = prog;
- if (prog->percpu_state) {
- if (!prog->sleepable)
+ /* sleepable prog should be using explicit bpf_preempt_disable() */
+ if (prog->percpu_state && !prog->sleepable) {
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT))
preempt_disable_notrace();
- /* sleepable prog should be using explicit bpf_preempt_disable() */
+ else if (this_cpu_inc_return(prog->stats->active) != 1)
+ return false;
}
for (ctx = old_ctx; ctx; ctx = ctx->saved_run_ctx) {
if (ctx->prog == prog)
@@ -2899,10 +2912,12 @@ static __always_inline void bpf_prog_exit_run_ctx(struct bpf_prog *prog,
struct bpf_run_ctx *run_ctx)
{
bpf_reset_run_ctx(run_ctx->saved_run_ctx);
- if (!prog->percpu_state)
+ if (!prog->percpu_state || prog->sleepable)
return;
- if (!prog->sleepable)
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT))
preempt_enable_notrace();
+ else
+ this_cpu_dec(prog->stats->active);
}
/* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 06f128632d5..20e7c52b741 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -807,13 +807,6 @@ struct bpf_binary_header {
u8 image[] __aligned(BPF_IMAGE_ALIGNMENT);
};
-struct bpf_prog_stats {
- u64_stats_t cnt;
- u64_stats_t nsecs;
- u64_stats_t misses;
- struct u64_stats_sync syncp;
-} __aligned(2 * sizeof(u64));
-
/*
* Per-cpu memory of a prog. All may_goto of one invocation of the prog use it. An invocation is
* one call from the kernel into the main prog, with the subprogs and sync callbacks it reaches.
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH bpf-next 2/4] bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT
2026-12-02 13:15 ` Mike Bazov
(?)
@ 2026-12-02 13:59 ` bot+bpf-ci
-1 siblings, 0 replies; 12+ messages in thread
From: bot+bpf-ci @ 2026-12-02 13:59 UTC (permalink / raw)
To: mike.bazov, bpf, ast, daniel, andrii
Cc: martin.lau, eddyz87, memxor, song, yonghong.song, jolsa,
john.fastabend, emil, ihor.solodrai, rostedt, mhiramat,
mathieu.desnoyers, jp.kobryn, linux-trace-kernel, linux-kernel,
mike.bazov, ast, andrii, daniel, martin.lau, eddyz87,
yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 2721 bytes --]
> bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT
TL;DR: Not a bug, but comments and an freplace error message elsewhere may now
be stale on PREEMPT_RT. With freplace multi-attach or tail calls, the
extension or target may also run in two tasks on one CPU.
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
[ ... ]
> @@ -2877,10 +2888,12 @@ static __always_inline bool bpf_prog_enter_run_ctx(struct bpf_prog *prog,
> #endif
> run_ctx->saved_run_ctx = old_ctx;
> run_ctx->prog = prog;
> - if (prog->percpu_state) {
> - if (!prog->sleepable)
> + /* sleepable prog should be using explicit bpf_preempt_disable() */
> + if (prog->percpu_state && !prog->sleepable) {
> + if (!IS_ENABLED(CONFIG_PREEMPT_RT))
> preempt_disable_notrace();
> - /* sleepable prog should be using explicit bpf_preempt_disable() */
> + else if (this_cpu_inc_return(prog->stats->active) != 1)
> + return false;
> }
This isn't a bug, but are some comments outside this diff now stale on
PREEMPT_RT, where a marked program is no longer run with preemption
disabled?
In bpf_enable_priv_stack() in kernel/bpf/verifier.c:
* programs are serialized per CPU by bpf_prog_active, and struct_ops
* programs that get a private stack run with preemption disabled, see
* bpf_prog_enter_run_ctx(). A sleepable one can't, so it gets none.
On RT the private stack is now protected only by the per-program counter in
prog->stats->active.
In bpf_tracing_prog_attach() in kernel/bpf/syscall.c, the comment "a target
that is checked per task must run with preemption disabled: marked and not
sleepable" and the message "Freplace prog with per-CPU state needs a target
that runs with preemption disabled." no longer describe a target that passes
this check on RT.
There the extension is kept from overlapping with itself only by the
target's own counter. With several targets (freplace multi-attach), each
target has its own counter, so can the extension run in two tasks on one
CPU?
The same applies to the tail_call marking in check_helper_call() ("The
target runs without a recursion check of its own"). On RT, marking the
dispatcher no longer stops the target from running in another task on the
same CPU through a second dispatcher.
The commit message only gives "drops the runs of other tasks" as the
trade-off. Should these comments and the freplace message be updated
together with bpf_prog_enter_run_ctx()?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/46893049890
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH bpf-next 3/4] selftests/bpf: Test private-stack struct_ops progs in prog arrays
2026-12-02 13:15 ` Mike Bazov
@ 2026-12-02 13:15 ` Mike Bazov
-1 siblings, 0 replies; 12+ messages in thread
From: Mike Bazov via B4 Relay @ 2026-12-02 13:15 UTC (permalink / raw)
To: bpf, 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, linux-kernel,
Mike Bazov
From: Mike Bazov <mike.bazov@gmail.com>
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 <mike.bazov@gmail.com>
---
.../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 <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#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
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH bpf-next 3/4] selftests/bpf: Test private-stack struct_ops progs in prog arrays
@ 2026-12-02 13:15 ` Mike Bazov
0 siblings, 0 replies; 12+ messages in thread
From: Mike Bazov @ 2026-12-02 13:15 UTC (permalink / raw)
To: bpf, 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, linux-kernel,
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 <mike.bazov@gmail.com>
---
.../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 <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH bpf-next 4/4] selftests/bpf: Expect marked programs to drop B on PREEMPT_RT
2026-12-02 13:15 ` Mike Bazov
@ 2026-12-02 13:15 ` Mike Bazov
-1 siblings, 0 replies; 12+ messages in thread
From: Mike Bazov via B4 Relay @ 2026-12-02 13:15 UTC (permalink / raw)
To: bpf, 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, linux-kernel,
Mike Bazov
From: Mike Bazov <mike.bazov@gmail.com>
On PREEMPT_RT a program with per-CPU state stays preemptible, and its
per-CPU counter refuses B's run while A is preempted inside it. Expect B
to be dropped in the subtests of such programs there. In
two_dispatchers, B's own run of the first dispatcher is dropped too, and
B reaches the handler through the second one, which has a counter of its
own, and runs it inside A.
Signed-off-by: Mike Bazov <mike.bazov@gmail.com>
---
.../selftests/bpf/prog_tests/trampoline_preempt.c | 35 ++++++++++++++++++----
1 file changed, 30 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
index 8708239fed5..0234e5dec51 100644
--- a/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
+++ b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
@@ -5,6 +5,7 @@
#include <signal.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
+#include <sys/utsname.h>
#include <unistd.h>
#include <test_progs.h>
#include "trampoline_preempt.skel.h"
@@ -31,8 +32,21 @@ enum { NO_PERCPU, PERCPU_MAP, PERCPU_VAR, THIS_CPU_PTR, CPU_ID };
enum expect {
B_RUNS, /* B runs the program while A is preempted inside it */
B_AFTER, /* B runs the program after A has left it */
+ B_DROPPED, /* B can't run the program while A is inside it */
};
+/*
+ * On PREEMPT_RT a program with per-CPU state stays preemptible, and a per-CPU
+ * counter refuses another run of it: B, which would run it after A, is dropped.
+ */
+static bool preempt_rt(void)
+{
+ struct utsname u;
+
+ /* The version of a PREEMPT_RT kernel says so, see init/Makefile */
+ return !uname(&u) && strstr(u.version, "PREEMPT_RT");
+}
+
/* What threads A and B share */
struct ctx {
struct trampoline_preempt *skel;
@@ -162,10 +176,21 @@ static void run_subtest(const struct subtest *t)
{
struct bpf_prog_info info = {};
__u32 info_len = sizeof(info), zero = 0;
+ enum expect expect = t->expect;
struct ctx c = {};
sigset_t oldset;
pthread_t ta, tb;
- int err, fd;
+ int err, fd, misses = 0;
+
+ /*
+ * B's own run of A's program is dropped. In two_dispatchers B still reaches
+ * the handler through the other dispatcher, which has a counter of its own,
+ * and runs it inside A.
+ */
+ if (expect == B_AFTER && preempt_rt()) {
+ expect = t->handler ? B_RUNS : B_DROPPED;
+ misses = NR_ROUNDS;
+ }
c.skel = load_progs(t->prog, t->prog_b, t->handler, t->percpu);
if (!c.skel)
@@ -203,21 +228,21 @@ static void run_subtest(const struct subtest *t)
ASSERT_OK(c.err_a, "worker_a");
ASSERT_OK(c.err_b, "worker_b");
ASSERT_EQ(c.skel->bss->sig_err, 0, "sig_err");
- if (t->expect == B_RUNS && !c.overlaps) {
+ if (expect == B_RUNS && !c.overlaps) {
/* B never preempted A inside the program, so nothing was tested */
test__skip();
goto out;
}
- ASSERT_EQ(c.overlaps, t->expect == B_RUNS ? NR_ROUNDS : 0, "overlaps");
+ ASSERT_EQ(c.overlaps, expect == B_AFTER ? 0 : NR_ROUNDS, "overlaps");
ASSERT_EQ(c.skel->bss->runs_a, NR_ROUNDS, "runs_a");
- ASSERT_EQ(c.skel->bss->runs_b, NR_ROUNDS, "runs_b");
+ ASSERT_EQ(c.skel->bss->runs_b, expect == B_DROPPED ? 0 : NR_ROUNDS, "runs_b");
if (t->handler)
ASSERT_EQ(c.skel->bss->stack_corrupt, 0, "stack_corrupt");
fd = bpf_program__fd(bpf_object__find_program_by_name(c.skel->obj, t->prog));
err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
if (!ASSERT_OK(err, "prog_info"))
goto out;
- ASSERT_EQ(info.recursion_misses, 0, "recursion_misses");
+ ASSERT_EQ(info.recursion_misses, misses, "recursion_misses");
out:
trampoline_preempt__destroy(c.skel);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH bpf-next 4/4] selftests/bpf: Expect marked programs to drop B on PREEMPT_RT
@ 2026-12-02 13:15 ` Mike Bazov
0 siblings, 0 replies; 12+ messages in thread
From: Mike Bazov @ 2026-12-02 13:15 UTC (permalink / raw)
To: bpf, 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, linux-kernel,
Mike Bazov
On PREEMPT_RT a program with per-CPU state stays preemptible, and its
per-CPU counter refuses B's run while A is preempted inside it. Expect B
to be dropped in the subtests of such programs there. In
two_dispatchers, B's own run of the first dispatcher is dropped too, and
B reaches the handler through the second one, which has a counter of its
own, and runs it inside A.
Signed-off-by: Mike Bazov <mike.bazov@gmail.com>
---
.../selftests/bpf/prog_tests/trampoline_preempt.c | 35 ++++++++++++++++++----
1 file changed, 30 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
index 8708239fed5..0234e5dec51 100644
--- a/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
+++ b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
@@ -5,6 +5,7 @@
#include <signal.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
+#include <sys/utsname.h>
#include <unistd.h>
#include <test_progs.h>
#include "trampoline_preempt.skel.h"
@@ -31,8 +32,21 @@ enum { NO_PERCPU, PERCPU_MAP, PERCPU_VAR, THIS_CPU_PTR, CPU_ID };
enum expect {
B_RUNS, /* B runs the program while A is preempted inside it */
B_AFTER, /* B runs the program after A has left it */
+ B_DROPPED, /* B can't run the program while A is inside it */
};
+/*
+ * On PREEMPT_RT a program with per-CPU state stays preemptible, and a per-CPU
+ * counter refuses another run of it: B, which would run it after A, is dropped.
+ */
+static bool preempt_rt(void)
+{
+ struct utsname u;
+
+ /* The version of a PREEMPT_RT kernel says so, see init/Makefile */
+ return !uname(&u) && strstr(u.version, "PREEMPT_RT");
+}
+
/* What threads A and B share */
struct ctx {
struct trampoline_preempt *skel;
@@ -162,10 +176,21 @@ static void run_subtest(const struct subtest *t)
{
struct bpf_prog_info info = {};
__u32 info_len = sizeof(info), zero = 0;
+ enum expect expect = t->expect;
struct ctx c = {};
sigset_t oldset;
pthread_t ta, tb;
- int err, fd;
+ int err, fd, misses = 0;
+
+ /*
+ * B's own run of A's program is dropped. In two_dispatchers B still reaches
+ * the handler through the other dispatcher, which has a counter of its own,
+ * and runs it inside A.
+ */
+ if (expect == B_AFTER && preempt_rt()) {
+ expect = t->handler ? B_RUNS : B_DROPPED;
+ misses = NR_ROUNDS;
+ }
c.skel = load_progs(t->prog, t->prog_b, t->handler, t->percpu);
if (!c.skel)
@@ -203,21 +228,21 @@ static void run_subtest(const struct subtest *t)
ASSERT_OK(c.err_a, "worker_a");
ASSERT_OK(c.err_b, "worker_b");
ASSERT_EQ(c.skel->bss->sig_err, 0, "sig_err");
- if (t->expect == B_RUNS && !c.overlaps) {
+ if (expect == B_RUNS && !c.overlaps) {
/* B never preempted A inside the program, so nothing was tested */
test__skip();
goto out;
}
- ASSERT_EQ(c.overlaps, t->expect == B_RUNS ? NR_ROUNDS : 0, "overlaps");
+ ASSERT_EQ(c.overlaps, expect == B_AFTER ? 0 : NR_ROUNDS, "overlaps");
ASSERT_EQ(c.skel->bss->runs_a, NR_ROUNDS, "runs_a");
- ASSERT_EQ(c.skel->bss->runs_b, NR_ROUNDS, "runs_b");
+ ASSERT_EQ(c.skel->bss->runs_b, expect == B_DROPPED ? 0 : NR_ROUNDS, "runs_b");
if (t->handler)
ASSERT_EQ(c.skel->bss->stack_corrupt, 0, "stack_corrupt");
fd = bpf_program__fd(bpf_object__find_program_by_name(c.skel->obj, t->prog));
err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
if (!ASSERT_OK(err, "prog_info"))
goto out;
- ASSERT_EQ(info.recursion_misses, 0, "recursion_misses");
+ ASSERT_EQ(info.recursion_misses, misses, "recursion_misses");
out:
trampoline_preempt__destroy(c.skel);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH bpf-next 0/4] bpf: Per-task recursion follow-ups for struct_ops and PREEMPT_RT
2026-12-02 13:15 ` Mike Bazov
` (4 preceding siblings ...)
(?)
@ 2026-12-02 20:00 ` patchwork-bot+netdevbpf
-1 siblings, 0 replies; 12+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-12-02 20:00 UTC (permalink / raw)
To: Mike Bazov
Cc: bpf, ast, daniel, andrii, martin.lau, eddyz87, memxor, song,
yonghong.song, jolsa, john.fastabend, emil, ihor.solodrai,
rostedt, mhiramat, mathieu.desnoyers, jp.kobryn,
linux-trace-kernel, linux-kernel
Hello:
This series was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:
On Wed, 02 Dec 2026 15:15:12 +0200 you wrote:
> Two follow-ups to "bpf: Check recursion per task for preemptible
> programs", per Alexei's review [1].
>
> Patch 1 fixes an existing hole: a struct_ops program with a private
> stack can be the target of a tail call, which enters it past the
> trampoline and its recursion check. The caller is a struct_ops program
> of the same op without a private stack, so it has no recursion check,
> and two tasks on one CPU can run the target on the same private stack.
> Such a program is now refused as a prog array element.
>
> [...]
Here is the summary with links:
- [bpf-next,1/4] bpf: Refuse struct_ops programs with a private stack in prog arrays
https://git.kernel.org/bpf/bpf-next/c/75f2b26fb752
- [bpf-next,2/4] bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT
https://git.kernel.org/bpf/bpf-next/c/f496dc0c687c
- [bpf-next,3/4] selftests/bpf: Test private-stack struct_ops progs in prog arrays
https://git.kernel.org/bpf/bpf-next/c/17ab7b5f7cd1
- [bpf-next,4/4] selftests/bpf: Expect marked programs to drop B on PREEMPT_RT
https://git.kernel.org/bpf/bpf-next/c/d682d1b10033
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 12+ messages in thread