Re: [PATCH bpf-next v5 3/5] bpf: Check program recursion per task via bpf_ctx

From: Alexei Starovoitov

Date: Tue Dec 01 2026 - 16:12:38 EST


On Tue, Dec 01, 2026 at 07:32 AM Mike Bazov via B4 Relay <devnull+mikeba.sentinelone.com@xxxxxxxxxx> wrote:
> - On PREEMPT_RT a marked program runs non-preemptibly too, as kprobe
> programs do.

Applied, but let's keep a per-cpu counter for marked progs on RT.
Pls send a followup with something like:

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 648a8b9..0f9398e 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? */
@@ -2863,10 +2872,13 @@ 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)
@@ -2885,10 +2897,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 06f1286..20e7c52 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.

> + * 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.

There is an existing bug here,
when the struct_ops prog is the target of a tail call.

Pls follow up with a fix like:

diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 508706d..7b294c8 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -1043,7 +1043,9 @@ static void *prog_fd_array_get_ptr(struct bpf_map *map,
if (IS_ERR(prog))
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);