From: Mike Bazov Date: Wed, 02 Dec 2026 15:15:14 +0200 Subject: [PATCH bpf-next 2/4] bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261202-followup-rt-stops-v1-2-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=4547; i=mike.bazov@gmail.com; s=gmail-20261116; h=from:subject:message-id; bh=w7gLrqfdQqjQB7LMDdsxraMMw1+dqlIANy+RZk1f0dU=; b=zwHVh933ApTJJF96b7gdQ8FnQlu1xJFcdl+r/3sTDgvBti3ZCI0zymkWvaID6utVCBhHtb03F 8Dc8wF/7yc2DyhLqkgZIKTFNQE6V75duRYU1AkhxXizquyCovJxmjnz 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 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 Signed-off-by: Mike Bazov --- 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 #include #include +#include #include 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