From: Mike Bazov via B4 Relay <devnull+mike.bazov.gmail.com@kernel.org>
To: bpf@vger.kernel.org, Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
John Fastabend <john.fastabend@gmail.com>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
JP Kobryn <jp.kobryn@linux.dev>,
linux-trace-kernel@vger.kernel.org,
linux-kernel@vger.kernel.org, Mike Bazov <mike.bazov@gmail.com>
Subject: [PATCH bpf-next 2/4] bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT
Date: Wed, 02 Dec 2026 15:15:14 +0200 [thread overview]
Message-ID: <20261202-followup-rt-stops-v1-2-22562be9ddb0@gmail.com> (raw)
In-Reply-To: <20261202-followup-rt-stops-v1-0-22562be9ddb0@gmail.com>
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
next prev parent reply other threads:[~2026-12-02 13:15 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 13:15 [PATCH bpf-next 0/4] bpf: Per-task recursion follow-ups for struct_ops and PREEMPT_RT Mike Bazov via B4 Relay
2026-12-02 13:15 ` [PATCH bpf-next 1/4] bpf: Refuse struct_ops programs with a private stack in prog arrays Mike Bazov via B4 Relay
2026-12-02 13:15 ` Mike Bazov via B4 Relay [this message]
2026-12-02 13:59 ` [PATCH bpf-next 2/4] bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT bot+bpf-ci
2026-12-02 13:15 ` [PATCH bpf-next 3/4] selftests/bpf: Test private-stack struct_ops progs in prog arrays Mike Bazov via B4 Relay
2026-12-02 13:15 ` [PATCH bpf-next 4/4] selftests/bpf: Expect marked programs to drop B on PREEMPT_RT Mike Bazov via B4 Relay
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202-followup-rt-stops-v1-2-22562be9ddb0@gmail.com \
--to=devnull+mike.bazov.gmail.com@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=jp.kobryn@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mathieu.desnoyers@efficios.com \
--cc=memxor@gmail.com \
--cc=mhiramat@kernel.org \
--cc=mike.bazov@gmail.com \
--cc=rostedt@goodmis.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®