The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: <mikeba@sentinelone.com>,
	"Mike Bazov via B4 Relay"
	<devnull+mikeba.sentinelone.com@kernel.org>,
	<bpf@vger.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>
Subject: Re: [PATCH bpf-next v5 3/5] bpf: Check program recursion per task via bpf_ctx
Date: Tue, 01 Dec 2026 21:12:33 +0000	[thread overview]
Message-ID: <DN9PF7JVRP7H.262PMB9O02SBG@gmail.com> (raw)
In-Reply-To: <20261201-bpf-preempt-v2-r2-v5-3-a6bb30daae35@sentinelone.com>

On Tue, Dec 01, 2026 at 07:32 AM Mike Bazov via B4 Relay <devnull+mikeba.sentinelone.com@kernel.org> 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);


  reply	other threads:[~2026-12-01 21:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 15:32 [PATCH bpf-next v5 0/5] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
2026-12-01 15:32 ` [PATCH bpf-next v5 1/5] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
2026-12-01 15:32 ` [PATCH bpf-next v5 2/5] bpf: Mark programs that keep per-CPU state Mike Bazov via B4 Relay
2026-12-01 15:32 ` [PATCH bpf-next v5 3/5] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
2026-12-01 21:12   ` Alexei Starovoitov [this message]
2026-12-01 15:32 ` [PATCH bpf-next v5 4/5] bpf: Remove the per-CPU recursion counter of programs Mike Bazov via B4 Relay
2026-12-01 15:32 ` [PATCH bpf-next v5 5/5] selftests/bpf: Add preemption recursion test for tracing programs Mike Bazov via B4 Relay
2026-12-01 21:10 ` [PATCH bpf-next v5 0/5] bpf: Check recursion per task for preemptible programs patchwork-bot+netdevbpf

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=DN9PF7JVRP7H.262PMB9O02SBG@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=devnull+mikeba.sentinelone.com@kernel.org \
    --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=mikeba@sentinelone.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