[PATCH bpf-next 2/4] bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT

From: Mike Bazov via B4 Relay

Date: Wed Dec 02 2026 - 08:16:21 EST


From: Mike Bazov <mike.bazov@xxxxxxxxx>

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@xxxxxxxxxx>
Signed-off-by: Mike Bazov <mike.bazov@xxxxxxxxx>
---
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