mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
@ 2026-11-17 11:29 Mike Bazov via B4 Relay
  2026-11-17 11:29 ` [PATCH bpf-next v3 1/4] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
                   ` (6 more replies)
  0 siblings, 7 replies; 28+ messages in thread
From: Mike Bazov via B4 Relay @ 2026-11-17 11:29 UTC (permalink / raw)
  To: bpf, 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, linux-kernel,
	Mike Bazov

fentry, fexit, raw_tp and syscall tracepoint programs can be preempted
while they run, but their recursion check is a per-CPU counter:

  task A enters prog on CPU 0       prog->active = 1
  task A is preempted inside prog
  task B enters prog on CPU 0       dropped, counted as recursion miss

Nothing recursed, but B's event is lost. Since 7.0, x86 and arm64 have
only lazy and full preemption, so this happens on stock kernels. Our
fentry/fexit probe on inet_sendmsg() and inet_recvmsg() loses 9,254 of
8 million runs under HTTP load on a 4-CPU VM. With the series it loses
none.

The series checks recursion per task instead: a program is recursing
only if the current task is already inside it.

v2 did that for every program, which breaks a program that uses a
per-CPU map as scratch space: the second task overwrites its buffer,
and nothing counts that. So in v3 the verifier marks the programs that
keep per-CPU state, and those keep the per-CPU counter and behave
exactly as today:

  - per-CPU maps, bpf_this_cpu_ptr(), bpf_per_cpu_ptr()
  - tail calls: the target has no recursion check of its own
  - timed may_goto: its budget is per CPU
  - a private stack

Trade-offs:

- Marked programs are not fixed. A program with a per-CPU map or a
  tail call loses runs to preemption as it does today.
- The verifier can't see everything. A program that keeps per-CPU
  slots of its own, like array[bpf_get_smp_processor_id()], is not
  marked. Today the counter keeps its runs apart. With the series two
  runs can write one slot, and nothing counts that.
- raw_tp is about 2% slower in bench, see the table below. fentry and
  fexit are within noise.
- Tracing, raw_tp and tracepoint programs lose the private stack
  (patch 1). It is already unsafe for them: two raw_tp programs that
  tail-call one handler let two tasks share its stack today. A program
  with two 2000-byte frames no longer loads.
- A marked freplace program can't be attached to an unmarked raw_tp or
  tracepoint program: -EINVAL.

One question: the marking is automatic. A flag that the program's
author sets would avoid the second trade-off, but existing programs
would keep losing runs until they are rebuilt. Which do you prefer?

  1. bpf: Avoid per-CPU private stack in preemptible tracing progs
  2. bpf: Mark programs whose runs must not overlap on a CPU
  3. bpf: Check program recursion per task via bpf_ctx
  4. selftests/bpf: Add preemption recursion test for tracing programs

Testing, x86-64 VM with 4 vCPUs:

The selftest pins two tasks to one CPU and makes B enter the program
while A is preempted inside it, 20 rounds per subtest. With the series
all 19 subtests pass. On bpf-next 7 fail: B is dropped in every round
(fentry, fentry_s, raw_tp, raw_tp_s, tp_syscall), A's stack is
overwritten (two_dispatchers), and the freplace attach is not rejected
(ext_attach).

bench -w2 -d12 -a trig-<x>, M/s, median of 10 runs [min-max], lazy
preemption, in boot order, on bpf-next 0fb18ce8a2e9:

               trig-fentry          trig-fexit           trig-rawtp
  bpf-next     41.65 [40.81-42.14]  38.89 [37.58-39.41]  42.62 [41.95-42.80]
  series       42.40 [41.87-43.04]  39.33 [35.24-41.68]  41.31 [40.48-41.74]
  bpf-next #2  41.64 [40.87-41.74]  39.29 [35.18-40.62]  42.05 [41.24-42.35]
  series #2    42.34 [42.12-42.78]  39.87 [39.46-41.38]  41.53 [40.79-41.75]

Changes in v3:

- Programs with per-CPU state keep the per-CPU counter.
- One pair of helpers does the check for all runners; the syscall
  tracepoint patch is folded into patch 3.
- raw_tp: walk the chain before linking the frame, 5-7% -> 2% slower.
- A marked extension can't be attached to an unmarked target.

v2: https://lore.kernel.org/bpf/20261111-bpf-preempt-v2-r2-v2-0-9d8f6b952ad2@sentinelone.com/
v1: https://lore.kernel.org/bpf/20261025-b4-bpf-trampoline-preempt-v1-0-198dcdc0c6ac@sentinelone.com/

---
Mike Bazov (4):
      bpf: Avoid per-CPU private stack in preemptible tracing progs
      bpf: Mark programs whose runs must not overlap on a CPU
      bpf: Check program recursion per task via bpf_ctx
      selftests/bpf: Add preemption recursion test for tracing programs

 include/linux/bpf.h                                |  98 +++++-
 include/linux/bpf_verifier.h                       |  19 ++
 kernel/bpf/fixups.c                                |   1 +
 kernel/bpf/syscall.c                               |  10 +
 kernel/bpf/trampoline.c                            |  26 +-
 kernel/bpf/verifier.c                              |  53 +++-
 kernel/trace/bpf_trace.c                           |  11 +-
 net/bpf/test_run.c                                 |   9 +-
 net/ipv4/bpf_tcp_ops.c                             |   6 +-
 .../selftests/bpf/prog_tests/may_goto_priv_stack.c |   6 +-
 .../selftests/bpf/prog_tests/timed_may_goto.c      |   6 +-
 .../selftests/bpf/prog_tests/trampoline_preempt.c  | 352 +++++++++++++++++++++
 .../selftests/bpf/progs/may_goto_priv_stack.c      |  14 +-
 .../selftests/bpf/progs/test_global_func3.c        |   9 +
 .../selftests/bpf/progs/timed_may_goto_user.c      |  16 +-
 .../selftests/bpf/progs/trampoline_preempt.c       | 258 +++++++++++++++
 .../selftests/bpf/progs/verifier_private_stack.c   |  81 ++---
 .../testing/selftests/bpf/progs/verifier_unwind.c  |  33 +-
 18 files changed, 899 insertions(+), 109 deletions(-)
---
base-commit: 32b66b94c86f009b49668de7340f2ed65aad2dc5
change-id: 20261111-bpf-preempt-v2-r2-f9f9a0dbd5d4

Best regards,
-- 
Mike Bazov <mikeba@sentinelone.com>



^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH bpf-next v3 1/4] bpf: Avoid per-CPU private stack in preemptible tracing progs
  2026-11-17 11:29 [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
@ 2026-11-17 11:29 ` Mike Bazov via B4 Relay
  2026-11-17 11:29 ` [PATCH bpf-next v3 2/4] bpf: Mark programs whose runs must not overlap on a CPU Mike Bazov via B4 Relay
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 28+ messages in thread
From: Mike Bazov via B4 Relay @ 2026-11-17 11:29 UTC (permalink / raw)
  To: bpf, 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, linux-kernel,
	Mike Bazov

From: Mike Bazov <mikeba@sentinelone.com>

A per-CPU private stack is only safe if a program never runs in two
tasks at once on one CPU. Commit a76ab5731e32 ("bpf: Find eligible
subprogs for private stack support") gave one to tracing, raw_tp and
tracepoint programs, but they run preemptibly, and two tasks can
already be inside one of them on a CPU:

- Two raw_tp dispatchers that tail-call the same handler each pass
  their own per-CPU recursion check, so both tasks run the handler.
- A tracepoint program attached to a syscall tracepoint and to a
  regular tracepoint can be entered by a second task through the
  regular one, which only checks bpf_prog_active.

The second task then overwrites the preempted task's stack. Neither
case can be seen when the program is loaded, so never give these
program types a private stack. The next patches make such overlap the
normal case.

check_recursion() told tracing programs apart by whether they can get a
private stack. Make it check the program type instead, so that
recursive subprograms stay rejected in these program types.

Move the selftests that relied on these program types having a private
stack to kprobe.multi and perf_event programs.

Trade-offs:

- These programs use the kernel stack again.
- A program with two 2000-byte frames no longer loads: "combined stack
  size of 2 calls is 4000. Too large".
- Unwinding on a private stack is no longer tested at run time: raw_tp
  was the only such program type that BPF_PROG_TEST_RUN runs.

Signed-off-by: Mike Bazov <mikeba@sentinelone.com>
---
 kernel/bpf/verifier.c                              | 42 +++++++----
 .../selftests/bpf/prog_tests/may_goto_priv_stack.c |  6 +-
 .../selftests/bpf/prog_tests/timed_may_goto.c      |  6 +-
 .../selftests/bpf/progs/may_goto_priv_stack.c      | 14 +++-
 .../selftests/bpf/progs/test_global_func3.c        |  9 +++
 .../selftests/bpf/progs/timed_may_goto_user.c      | 16 ++++-
 .../selftests/bpf/progs/verifier_private_stack.c   | 81 ++++++++++++----------
 .../testing/selftests/bpf/progs/verifier_unwind.c  | 33 +++++++--
 8 files changed, 146 insertions(+), 61 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 1a8e5a108a68..2ca1469d8dee 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -5331,9 +5331,15 @@ static enum priv_stack_mode bpf_enable_priv_stack(struct bpf_prog *prog)
 	if (!bpf_jit_supports_private_stack())
 		return NO_PRIV_STACK;
 
-	/* bpf_prog_check_recur() checks all prog types that use bpf trampoline
-	 * while kprobe/tp/perf_event/raw_tp don't use trampoline hence checked
-	 * explicitly.
+	/*
+	 * A private stack is per CPU, so a program using it must never run in two
+	 * tasks at once on one CPU. Tracing, raw_tp and tracepoint programs run
+	 * preemptibly, and the same program can be entered by another task on the
+	 * same CPU, e.g. through a second tail-call dispatcher, or as a tracepoint
+	 * program attached to both a syscall and a regular tracepoint. They always
+	 * use the task's kernel stack. Kprobe multi, kprobe session and perf_event
+	 * programs are serialized per CPU by bpf_prog_active, and struct_ops
+	 * programs that request a private stack keep per-CPU recursion protection.
 	 */
 	switch (prog->type) {
 	case BPF_PROG_TYPE_KPROBE:
@@ -5346,14 +5352,10 @@ static enum priv_stack_mode bpf_enable_priv_stack(struct bpf_prog *prog)
 		    prog->expected_attach_type != BPF_TRACE_KPROBE_SESSION)
 			return NO_PRIV_STACK;
 		fallthrough;
-	case BPF_PROG_TYPE_TRACEPOINT:
 	case BPF_PROG_TYPE_PERF_EVENT:
-	case BPF_PROG_TYPE_RAW_TRACEPOINT:
 		return PRIV_STACK_ADAPTIVE;
-	case BPF_PROG_TYPE_TRACING:
-	case BPF_PROG_TYPE_LSM:
 	case BPF_PROG_TYPE_STRUCT_OPS:
-		if (prog->aux->priv_stack_requested || bpf_prog_check_recur(prog))
+		if (prog->aux->priv_stack_requested)
 			return PRIV_STACK_ADAPTIVE;
 		fallthrough;
 	default:
@@ -5363,6 +5365,22 @@ static enum priv_stack_mode bpf_enable_priv_stack(struct bpf_prog *prog)
 	return NO_PRIV_STACK;
 }
 
+/* Progs that run on top of other progs, whose frames are still on the stack */
+static bool bpf_prog_runs_nested(struct bpf_prog *prog)
+{
+	switch (prog->type) {
+	case BPF_PROG_TYPE_KPROBE:
+	case BPF_PROG_TYPE_PERF_EVENT:
+	case BPF_PROG_TYPE_TRACEPOINT:
+	case BPF_PROG_TYPE_RAW_TRACEPOINT:
+		return true;
+	case BPF_PROG_TYPE_TRACING:
+		return bpf_prog_check_recur(prog);
+	default:
+		return false;
+	}
+}
+
 static int round_up_stack_depth(struct bpf_verifier_env *env, int stack_depth)
 {
 	if (env->prog->jit_requested)
@@ -5494,12 +5512,10 @@ static int check_recursion(struct bpf_verifier_env *env, struct bpf_call_graph *
 		}
 		if (env->subprog_info[s].recursive) {
 			/*
-			 * These progs run on top of other progs. Their frames belong in the
-			 * private stack, which has room for one frame of a subprog. A kprobe prog
-			 * that can be attached to a uprobe has no private stack, and is no better.
+			 * These progs run on top of other progs. A private stack has room for one
+			 * frame of a subprog, and the kernel stack under them is already in use.
 			 */
-			if ((bpf_enable_priv_stack(env->prog) != NO_PRIV_STACK ||
-			     env->prog->type == BPF_PROG_TYPE_KPROBE) &&
+			if (bpf_prog_runs_nested(env->prog) &&
 			    !env->prog->aux->priv_stack_requested) {
 				verbose(env, "recursive %s() in a tracing program\n",
 					bpf_subprog_name(env, s));
diff --git a/tools/testing/selftests/bpf/prog_tests/may_goto_priv_stack.c b/tools/testing/selftests/bpf/prog_tests/may_goto_priv_stack.c
index 4f8476d36250..b4aab50b8a77 100644
--- a/tools/testing/selftests/bpf/prog_tests/may_goto_priv_stack.c
+++ b/tools/testing/selftests/bpf/prog_tests/may_goto_priv_stack.c
@@ -24,13 +24,17 @@ void test_may_goto_priv_stack(void)
 		return;
 
 	err = may_goto_priv_stack__attach(skel);
+	if (err == -EOPNOTSUPP) {
+		test__skip();
+		goto out;
+	}
 	if (!ASSERT_OK(err, "attach"))
 		goto out;
 
 	memset(skel->bss->regs_64, 0xff, sizeof(skel->bss->regs_64));
 	memset(skel->bss->regs_128, 0xff, sizeof(skel->bss->regs_128));
 
-	err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.priv_stack_64), &topts);
+	err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.trigger), &topts);
 	if (!ASSERT_OK(err, "test_run"))
 		goto out;
 
diff --git a/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c b/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c
index 3da3f33a2898..ee7b79ef77e0 100644
--- a/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c
+++ b/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c
@@ -131,10 +131,14 @@ static void test_user_ringbuf(struct timed_may_goto_user *skel)
 static void test_priv_stack(struct timed_may_goto_user *skel)
 {
 	LIBBPF_OPTS(bpf_test_run_opts, opts);
-	int fd = bpf_program__fd(skel->progs.priv_stack);
+	int fd = bpf_program__fd(skel->progs.trigger);
 	struct bpf_link *link;
 
 	link = bpf_program__attach(skel->progs.priv_stack);
+	if (!link && errno == EOPNOTSUPP) {
+		test__skip();
+		return;
+	}
 	if (!ASSERT_OK_PTR(link, "attach"))
 		return;
 	ASSERT_OK(bpf_prog_test_run_opts(fd, &opts), "test_run");
diff --git a/tools/testing/selftests/bpf/progs/may_goto_priv_stack.c b/tools/testing/selftests/bpf/progs/may_goto_priv_stack.c
index 5c2aa0aa5055..e11a7f7493a6 100644
--- a/tools/testing/selftests/bpf/progs/may_goto_priv_stack.c
+++ b/tools/testing/selftests/bpf/progs/may_goto_priv_stack.c
@@ -10,11 +10,11 @@ __u64 regs_64[7];
 __u64 regs_128[7];
 
 /*
- * fentry prog with 64 or more bytes of stack uses private stack.
+ * kprobe.multi prog with 64 or more bytes of stack uses private stack.
  * Loop until may_goto expires. r0-r5 should stay zero.
  */
 #define TIMED_MAY_GOTO_PRIV_STACK(size, func)				\
-SEC("fentry/" #func)							\
+SEC("kprobe.multi/" #func)						\
 __naked void priv_stack_##size(void)					\
 {									\
 	asm volatile (							\
@@ -49,4 +49,14 @@ __naked void priv_stack_##size(void)					\
 TIMED_MAY_GOTO_PRIV_STACK(64, bpf_fentry_test1)
 TIMED_MAY_GOTO_PRIV_STACK(128, bpf_fentry_test2)
 
+/*
+ * No tests in here, just to trigger 'bpf_fentry_test*'
+ * through tracing test_run
+ */
+SEC("fentry/bpf_modify_return_test")
+int trigger(void *ctx)
+{
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/test_global_func3.c b/tools/testing/selftests/bpf/progs/test_global_func3.c
index b916355c5e59..a473bed346d2 100644
--- a/tools/testing/selftests/bpf/progs/test_global_func3.c
+++ b/tools/testing/selftests/bpf/progs/test_global_func3.c
@@ -267,6 +267,15 @@ int global_func3_recursion_in_tracing_prog(void *ctx)
 	return depth(again + 7);
 }
 
+SEC("raw_tp")
+__arch_x86_64
+__arch_arm64
+__failure __msg("recursive depth() in a tracing program")
+int global_func3_recursion_in_raw_tp(void *ctx)
+{
+	return depth(again + 7);
+}
+
 __attribute__ ((noinline))
 int redirects(struct __sk_buff *skb, int n)
 {
diff --git a/tools/testing/selftests/bpf/progs/timed_may_goto_user.c b/tools/testing/selftests/bpf/progs/timed_may_goto_user.c
index 8b85815d45d2..ed56b6afb607 100644
--- a/tools/testing/selftests/bpf/progs/timed_may_goto_user.c
+++ b/tools/testing/selftests/bpf/progs/timed_may_goto_user.c
@@ -277,7 +277,7 @@ int drain(void *ctx)
 }
 
 /*
- * A tracing prog with large frames: they are in a private stack.
+ * A kprobe.multi prog with large frames: they are in a private stack.
  */
 int priv_stack_spun, priv_stack_cb_spun;
 
@@ -296,8 +296,8 @@ static int priv_stack_cb(__u64 i, int *spun)
 	return 0;
 }
 
-SEC("fentry/bpf_fentry_test1")
-int BPF_PROG(priv_stack)
+SEC("kprobe.multi/bpf_fentry_test1")
+int BPF_KPROBE(priv_stack)
 {
 	volatile char buf[128] = {};
 	int i, n = buf[zero & 127], spun = 0;
@@ -309,3 +309,13 @@ int BPF_PROG(priv_stack)
 	priv_stack_cb_spun = spun;
 	return 0;
 }
+
+/*
+ * No tests in here, just to trigger 'bpf_fentry_test*'
+ * through tracing test_run
+ */
+SEC("fentry/bpf_modify_return_test")
+int BPF_PROG(trigger)
+{
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_private_stack.c b/tools/testing/selftests/bpf/progs/verifier_private_stack.c
index e7f410a74a21..299ce68cb212 100644
--- a/tools/testing/selftests/bpf/progs/verifier_private_stack.c
+++ b/tools/testing/selftests/bpf/progs/verifier_private_stack.c
@@ -52,7 +52,7 @@ __naked void private_stack_single_prog(void)
 "	::: __clobber_all);
 }
 
-SEC("raw_tp")
+SEC("kprobe.multi")
 __description("No private stack")
 __success
 __arch_x86_64
@@ -82,7 +82,7 @@ __naked static long subprog_6args(long a, long b, long c, long d, long e, long f
 }
 
 /* x86-64 has one register for the private stack and for the sixth argument */
-SEC("raw_tp")
+SEC("kprobe.multi")
 __description("No private stack, stack arguments")
 __success
 __load_if_JITed()
@@ -100,7 +100,7 @@ __naked void no_private_stack_stack_args(void)
 "	::: __clobber_all);
 }
 #else
-SEC("raw_tp")
+SEC("kprobe.multi")
 __description("No private stack, stack arguments")
 __skip("stack_arg is not supported by compiler")
 __success
@@ -127,6 +127,23 @@ __naked void no_private_stack_kprobe(void)
 "	::: __clobber_all);
 }
 
+SEC("fentry/bpf_fentry_test9")
+__description("No private stack, fentry")
+__success
+__arch_x86_64
+__jited("	subq	$0x100, %rsp")
+__arch_arm64
+__jited("	sub	sp, sp, #0x100")
+__naked void no_private_stack_fentry(void)
+{
+	asm volatile ("			\
+	r1 = 42;			\
+	*(u64 *)(r10 - 256) = r1;	\
+	r0 = 0;				\
+	exit;				\
+"	::: __clobber_all);
+}
+
 SEC("uprobe")
 __description("No private stack, uprobe")
 __success
@@ -215,7 +232,7 @@ static unsigned long loop_callback(void)
 	: __clobber_common);
 }
 
-SEC("raw_tp")
+SEC("kprobe.multi")
 __description("Private stack, callback")
 __success
 __arch_x86_64
@@ -273,9 +290,9 @@ __naked void private_stack_callback(void)
 	: __clobber_common);
 }
 
-SEC("fentry/bpf_fentry_test9")
+SEC("perf_event")
 __description("Private stack, exception in main prog")
-__success __retval(0)
+__success
 __arch_x86_64
 __jited("	pushq	%r9")
 __jited("...")
@@ -321,9 +338,9 @@ __used static int subprog_exception(void)
 	return 0;
 }
 
-SEC("fentry/bpf_fentry_test9")
+SEC("perf_event")
 __description("Private stack, exception in subprog")
-__success __retval(0)
+__success
 __arch_x86_64
 __jited("	movq	%rdi, -0x200(%r9)")
 __jited("	pushq	%r9")
@@ -355,20 +372,16 @@ int private_stack_exception_sub_prog(void)
 }
 
 /*
- * Nothing attaches the two fentry progs above, so they are loaded, but never
- * run. BPF_PROG_TEST_RUN does run a raw_tp prog. The default exception callback
- * returns the cookie of bpf_throw().
+ * Nothing attaches the two perf_event progs above, so they are loaded, but
+ * never run. BPF_PROG_TEST_RUN does run a raw_tp prog. The default exception
+ * callback returns the cookie of bpf_throw().
  */
 SEC("raw_tp")
-__description("Private stack, run, exception in main prog")
+__description("No private stack, run, exception in main prog")
 __success __retval(1)
 __arch_x86_64
-__jited("	movq	%rdi, -0x200(%r9)")
-__jited("	movl	$0x1, %edi")
-__jited("	pushq	%r9")
-__jited("...")
-__jited("	callq")
-__naked void private_stack_run_exception_main_prog(void)
+__jited("	subq	$0x200, %rsp")
+__naked void big_stack_run_exception_main_prog(void)
 {
 	asm volatile ("			\
 	r1 = 42;			\
@@ -396,15 +409,11 @@ __naked static void subprog_throw(void)
 }
 
 SEC("raw_tp")
-__description("Private stack, run, exception in subprog")
+__description("No private stack, run, exception in subprog")
 __success __retval(2)
 __arch_x86_64
-__jited("	movq	%rdi, -0x200(%r9)")
-__jited("	pushq	%r9")
-__jited("...")
-__jited("	callq")
-__jited("	popq	%r9")
-__naked void private_stack_run_exception_sub_prog(void)
+__jited("	subq	$0x200, %rsp")
+__naked void big_stack_run_exception_sub_prog(void)
 {
 	asm volatile ("			\
 	r1 = 42;			\
@@ -416,7 +425,7 @@ __naked void private_stack_run_exception_sub_prog(void)
 }
 
 __used
-__naked static void private_stack_subprog_throw(void)
+__naked static void big_stack_subprog_throw(void)
 {
 	asm volatile ("			\
 	r1 = 42;			\
@@ -430,24 +439,22 @@ __naked static void private_stack_subprog_throw(void)
 	: __clobber_all);
 }
 
-/* Only the frame of the main prog counts, and it has nothing pushed at the call */
+/* The subprog that throws has a large frame, the main prog a small one */
 SEC("raw_tp")
-__description("Private stack, run, exception in subprog, no private stack in main prog")
+__description("No private stack, run, exception in subprog with a large stack")
 __success __retval(3)
 __arch_x86_64
 __jited("	subq	$0x8, %rsp")
 __jited("...")
 __jited("func #1")
 __jited("...")
-__jited("	movq	%rdi, -0x200(%r9)")
-__jited("	movl	$0x3, %edi")
-__jited("	pushq	%r9")
+__jited("	subq	$0x200, %rsp")
 __naked void no_private_stack_run_exception_sub_prog(void)
 {
 	asm volatile ("				\
 	r1 = 42;				\
 	*(u64 *)(r10 - 8) = r1;			\
-	call private_stack_subprog_throw;	\
+	call big_stack_subprog_throw;	\
 	r0 = 0;					\
 	exit;					\
 "	::: __clobber_all);
@@ -478,9 +485,9 @@ __noinline static int timer_cb2(void *map, int *key, struct bpf_timer *timer)
 	return 0;
 }
 
-SEC("fentry/bpf_fentry_test9")
+SEC("kprobe.multi")
 __description("Private stack, async callback, not nested")
-__success __retval(0)
+__success
 __arch_x86_64
 __jited("	movabsq	$0x{{.*}}, %r9")
 __arch_arm64
@@ -503,9 +510,9 @@ int private_stack_async_callback_1(void)
 	return 0;
 }
 
-SEC("fentry/bpf_fentry_test9")
+SEC("kprobe.multi")
 __description("Private stack, async callback, potential nesting")
-__success __retval(0)
+__success
 __load_if_JITed()
 __log_level(4) __msg("stack depth max 272")
 __msg("subprog 0 (private_stack_async_callback_2) main {{.*}} stack 8")
@@ -532,7 +539,7 @@ int private_stack_async_callback_2(void)
 	return 0;
 }
 
-SEC("fentry/bpf_fentry_test9")
+SEC("kprobe.multi")
 __description("private stack, max stack depth is private stack")
 __success
 __log_level(4) __msg("stack depth max 256")
diff --git a/tools/testing/selftests/bpf/progs/verifier_unwind.c b/tools/testing/selftests/bpf/progs/verifier_unwind.c
index 596e5e0301e5..c779b455e1db 100644
--- a/tools/testing/selftests/bpf/progs/verifier_unwind.c
+++ b/tools/testing/selftests/bpf/progs/verifier_unwind.c
@@ -1197,9 +1197,9 @@ static unsigned long may_unwind_big_stack(void)
  * x86-64 keeps the pointer to the private stack in r9 and saves it around a call.
  * The value that is read back in the pad is there only when r9 is restored.
  */
-SEC("raw_tp")
+SEC("kprobe.multi")
 __description("unwind: private stack, the callee unwinds")
-__success __retval(47)
+__success
 __load_if_JITed()
 __arch_x86_64
 __jited("	movl	$0x1234, %esi") /* past the load, see above */
@@ -1232,12 +1232,37 @@ __naked int private_stack_unwinds(void)
 }
 
 SEC("raw_tp")
-__description("unwind: private stack, the callee returns")
+__description("unwind: big stack, the callee unwinds")
+__success __retval(47)
+__load_if_JITed()
+__arch_x86_64
+__arch_arm64
+__naked int big_stack_unwinds(void)
+{
+	asm volatile (
+		"*(u64 *)(r10 - 200) = 42;"
+		"r1 = %[one] ll;"
+		"r1 = *(u64 *)(r1 + 0);"
+		"call may_unwind_big_stack;"
+		unwind_goto(1)
+		"exit;"
+		"r1 = *(u64 *)(r10 - 200);"
+		"r0 += r1;"
+		"exit;"
+		:
+		: __imm_addr(one),
+		  __imm_unwind_goto(1)
+		: __clobber_all
+	);
+}
+
+SEC("raw_tp")
+__description("unwind: big stack, the callee returns")
 __success __retval(49)
 __load_if_JITed()
 __arch_x86_64
 __arch_arm64
-__naked int private_stack_returns(void)
+__naked int big_stack_returns(void)
 {
 	asm volatile (
 		"*(u64 *)(r10 - 200) = 42;"

-- 
2.43.0



^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH bpf-next v3 2/4] bpf: Mark programs whose runs must not overlap on a CPU
  2026-11-17 11:29 [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
  2026-11-17 11:29 ` [PATCH bpf-next v3 1/4] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
@ 2026-11-17 11:29 ` Mike Bazov via B4 Relay
  2026-11-19  5:04   ` JP Kobryn (CrowdStrike)
  2026-11-17 11:29 ` [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 28+ messages in thread
From: Mike Bazov via B4 Relay @ 2026-11-17 11:29 UTC (permalink / raw)
  To: bpf, 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, linux-kernel,
	Mike Bazov

From: Mike Bazov <mikeba@sentinelone.com>

The per-CPU recursion counter of tracing and raw tracepoint programs
also keeps a second task out of a program while another task is
preempted inside it on the same CPU. Programs rely on that:

- A per-CPU map value that a program looked up stays its own until the
  run ends, which is how programs use these maps as scratch space.
- The budget of timed may_goto is per-CPU memory that the first insns
  of a run reset, see commit ec274bb4665c ("bpf: One may_goto time
  slice per prog invocation"). A second run on the CPU gives the first
  one more time.
- A private stack is per CPU. After the previous patch, struct_ops
  programs that request one are the only programs with this check that
  still have it.

The next patch checks recursion per task, which lets a second task in.
For these programs that would turn a counted, dropped run into silently
corrupted data, or into a may_goto loop that another task keeps
renewing. Mark them at load time, so that they keep the per-CPU counter.

Set prog->recur_per_cpu when the verifier sees:

- a helper called with a per-CPU map, also through an inner map,
- the address of a per-CPU array value loaded directly,
- bpf_this_cpu_ptr() or bpf_per_cpu_ptr(),
- a tail call: the target runs without its own recursion check,
- timed may_goto,
- a private stack, in a struct_ops program that requests one.

Only verified code counts: a per-CPU map referenced in a branch that a
constant in .rodata disables doesn't mark the program.

No functional change: nothing reads the flag yet.

Trade-offs, once the next patch reads it:

- A program that keeps per-CPU slots of its own, e.g. in a regular
  array indexed by bpf_get_smp_processor_id(), is not marked.
- Marked programs keep losing runs to preemption.

Signed-off-by: Mike Bazov <mikeba@sentinelone.com>
---
 include/linux/bpf.h   |  3 ++-
 kernel/bpf/fixups.c   |  1 +
 kernel/bpf/verifier.c | 11 +++++++++++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index a5ad08b7937c..7873f0ef2817 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2063,7 +2063,8 @@ struct bpf_prog {
 				call_get_func_ip:1, /* Do we call get_func_ip() */
 				call_session_cookie:1, /* Do we call bpf_session_cookie() */
 				tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */
-				sleepable:1;	/* BPF program is sleepable */
+				sleepable:1,	/* BPF program is sleepable */
+				recur_per_cpu:1; /* Runs must not overlap on a CPU */
 	enum bpf_prog_type	type;		/* Type of BPF program */
 	enum bpf_attach_type	expected_attach_type; /* For some prog types */
 	u32			len;		/* Number of filter blocks */
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 4d9b67981520..82ad0d3bf465 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -2591,6 +2591,7 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
 				if (!tmg)
 					return -ENOMEM;
 				prog->aux->timed_may_goto = tmg;
+				prog->recur_per_cpu = true;
 			}
 			prog->jit_required = 1;
 			cnt = may_goto_expand_timed(insn_buf, &jmp, tmg, reg, save_off);
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 2ca1469d8dee..3d54520df3e3 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -6050,6 +6050,7 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
 		if (round_up_stack_depth(env, si[s].stack_depth) >= BPF_PRIV_STACK_MIN_SIZE) {
 			si[s].priv_stack_mode = PRIV_STACK_ADAPTIVE;
 			env->prog->aux->jits_use_priv_stack = true;
+			env->prog->recur_per_cpu = true;
 		}
 	}
 
@@ -12916,6 +12917,10 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 	if (err)
 		return err;
 
+	/* A per-CPU value stays the run's own only while no other run starts on the CPU */
+	if (meta.map.ptr && bpf_map_is_percpu_map(meta.map.ptr->map_type))
+		env->prog->recur_per_cpu = true;
+
 	err = record_func_map(env, &meta, func_id, insn_idx);
 	if (err)
 		return err;
@@ -12942,6 +12947,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 
 	switch (func_id) {
 	case BPF_FUNC_tail_call:
+		/* The target runs without a recursion check of its own */
+		env->prog->recur_per_cpu = true;
 		err = check_resource_leak(env, false, true, "tail_call");
 		if (err)
 			return err;
@@ -13048,6 +13055,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 		struct bpf_reg_state *reg = &regs[BPF_REG_1];
 		const struct btf_type *type;
 
+		env->prog->recur_per_cpu = true;
+
 		if (reg->type & MEM_RCU) {
 			type = btf_type_by_id(reg->btf, reg->btf_id);
 			if (!type || !btf_type_is_struct(type)) {
@@ -19690,6 +19699,8 @@ static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
 		__mark_reg_known(dst_reg, aux->map_off);
 		dst_reg->type = PTR_TO_MAP_VALUE;
 		dst_reg->map_ptr = map;
+		if (bpf_map_is_percpu_map(map->map_type))
+			env->prog->recur_per_cpu = true;
 		WARN_ON_ONCE(map->map_type != BPF_MAP_TYPE_INSN_ARRAY &&
 			     map->max_entries != 1);
 		/* We want reg->id to be same (0) as map_value is not distinct */

-- 
2.43.0



^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx
  2026-11-17 11:29 [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
  2026-11-17 11:29 ` [PATCH bpf-next v3 1/4] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
  2026-11-17 11:29 ` [PATCH bpf-next v3 2/4] bpf: Mark programs whose runs must not overlap on a CPU Mike Bazov via B4 Relay
@ 2026-11-17 11:29 ` Mike Bazov via B4 Relay
  2026-11-17 12:20   ` bot+bpf-ci
                     ` (2 more replies)
  2026-11-17 11:29 ` [PATCH bpf-next v3 4/4] selftests/bpf: Add preemption recursion test for tracing programs Mike Bazov via B4 Relay
                   ` (3 subsequent siblings)
  6 siblings, 3 replies; 28+ messages in thread
From: Mike Bazov via B4 Relay @ 2026-11-17 11:29 UTC (permalink / raw)
  To: bpf, 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, linux-kernel,
	Mike Bazov

From: Mike Bazov <mikeba@sentinelone.com>

Trampoline programs (fentry, fexit, fsession, fmod_ret and their
sleepable variants), raw tracepoint programs (raw_tp, raw_tp.s, tp_btf)
and tracepoint programs on syscall tracepoints run preemptibly, but
their recursion check is a per-CPU, per-program counter. When a task is
preempted inside a program, the counter stays raised, and every other
task that enters the program on that CPU is dropped and counted as a
recursion miss, although nothing recursed. A program can't avoid that:
the counter is checked before its first insn.

Since commit 7dadeaa6e851 ("sched: Further restrict the preemption
modes") x86 and arm64 have only lazy and full preemption, so this
happens in normal operation. A security agent that pairs fentry and
fexit programs on inet_sendmsg() and inet_recvmsg() loses one half of a
pair with every drop. With 200,000 HTTP requests and four CPU-bound
tasks on a 4-CPU VM, 9,254 of 7,966,760 runs of these programs were
dropped on bpf-next, and none of 7,975,426 with this series.

Check recursion per task instead. The runners already link a
stack-allocated bpf_run_ctx into current->bpf_ctx. Move saved_run_ctx
into struct bpf_run_ctx, so every frame links to the outer one, and
record in each frame the program it runs. A program is recursing if a
frame in the current task's chain already runs it. Interrupts and NMIs
run on top of the interrupted task's chain, so recursion through them
is still caught.

bpf_prog_enter_run_ctx() and bpf_prog_exit_run_ctx() do the check for
all runners. The syscall tracepoint runner linked one frame around its
program array and now links one per program. bpf_set_run_ctx() accepts
a NULL ctx, which io_uring passes to hide the BPF context; the frames
below are not walked while it is hidden.

Programs marked by the previous patch keep the per-CPU counter and
behave exactly as before, so no counted drop is traded for corrupted
per-CPU data or a renewed may_goto budget. An extension (freplace) runs
under the check of its target, so a marked one can't be attached to a
target that is checked per task.

Trade-offs:

- An unmarked program that keeps per-CPU slots of its own, see the
  previous patch, can have two runs use one slot, and nothing counts
  that.
- Marked programs lose runs to preemption as before.
- bench trig-rawtp is about 2% slower: 42.6 and 42.1 M/s on bpf-next,
  41.3 and 41.5 M/s with the series. trig-fentry and trig-fexit are
  not slower.
- Attaching a marked extension to an unmarked raw_tp, tp_btf or
  tracepoint program now fails with -EINVAL.

Signed-off-by: Mike Bazov <mikeba@sentinelone.com>
---
 include/linux/bpf.h          | 95 ++++++++++++++++++++++++++++++++++++++------
 include/linux/bpf_verifier.h | 19 +++++++++
 kernel/bpf/syscall.c         | 10 +++++
 kernel/bpf/trampoline.c      | 26 +++++-------
 kernel/trace/bpf_trace.c     | 11 ++---
 net/bpf/test_run.c           |  9 +----
 net/ipv4/bpf_tcp_ops.c       |  6 +--
 7 files changed, 129 insertions(+), 47 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 7873f0ef2817..97cbcd4b0d67 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2783,7 +2783,16 @@ int bpf_prog_array_copy(struct bpf_prog_array *old_array,
 			struct bpf_prog_array **new_array);
 struct bpf_prog *bpf_prog_dummy(void);
 
-struct bpf_run_ctx {};
+/*
+ * A frame in the chain of the programs that a task is running, from
+ * current->bpf_ctx to the outermost one.
+ */
+struct bpf_run_ctx {
+	/* The next outer frame, NULL in the outermost one */
+	struct bpf_run_ctx *saved_run_ctx;
+	/* Set while a program whose recursion is checked per task runs in the frame */
+	const struct bpf_prog *prog;
+};
 
 struct bpf_cg_run_ctx {
 	struct bpf_run_ctx run_ctx;
@@ -2801,7 +2810,6 @@ struct bpf_tramp_run_ctx {
 	struct bpf_run_ctx run_ctx;
 	u64 bpf_cookie;
 	int retval;
-	struct bpf_run_ctx *saved_run_ctx;
 };
 
 static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
@@ -2809,8 +2817,17 @@ static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
 	struct bpf_run_ctx *old_ctx = NULL;
 
 #ifdef CONFIG_BPF_SYSCALL
-	old_ctx = current->bpf_ctx;
-	current->bpf_ctx = new_ctx;
+	old_ctx = READ_ONCE(current->bpf_ctx);
+	if (new_ctx) {
+		new_ctx->saved_run_ctx = old_ctx;
+		new_ctx->prog = NULL;
+	}
+	/* Initialize the frame before interrupts and NMIs on this CPU see it. */
+	barrier();
+	WRITE_ONCE(current->bpf_ctx, new_ctx);
+#else
+	if (new_ctx)
+		new_ctx->saved_run_ctx = NULL;
 #endif
 	return old_ctx;
 }
@@ -2818,8 +2835,64 @@ static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
 static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
 {
 #ifdef CONFIG_BPF_SYSCALL
-	current->bpf_ctx = old_ctx;
+	WRITE_ONCE(current->bpf_ctx, old_ctx);
+	/* Unlink the frame before its stack storage can be reused. */
+	barrier();
+#endif
+}
+
+/*
+ * Enter @prog, unless that would be recursion:
+ *
+ * - A program that keeps per-CPU state (prog->recur_per_cpu) takes the per-CPU
+ *   counter. No other run of it starts on this CPU until it exits.
+ * - Any other program recurses only if the current task is already inside it:
+ *   a frame in the task's chain runs it. Interrupts and NMIs run on top of the
+ *   chain of the task they interrupt, so they are covered too.
+ *
+ * Returns false if @prog must not run. Call bpf_prog_exit_run_ctx() either way.
+ *
+ * The chain is walked before the frame is linked, so a run that must not start
+ * links nothing. Always inline: the callers are notrace.
+ */
+static __always_inline bool bpf_prog_enter_run_ctx(struct bpf_prog *prog,
+						   struct bpf_run_ctx *run_ctx)
+{
+	struct bpf_run_ctx *old_ctx = NULL;
+
+#ifdef CONFIG_BPF_SYSCALL
+	old_ctx = READ_ONCE(current->bpf_ctx);
+#endif
+	run_ctx->saved_run_ctx = old_ctx;
+	if (prog->recur_per_cpu) {
+		run_ctx->prog = NULL;
+		if (!bpf_prog_get_recursion_context(prog))
+			return false;
+	} else {
+		for (struct bpf_run_ctx *ctx = old_ctx; ctx; ctx = ctx->saved_run_ctx) {
+			if (READ_ONCE(ctx->prog) == prog)
+				return false;
+		}
+		run_ctx->prog = prog;
+	}
+#ifdef CONFIG_BPF_SYSCALL
+	/* Initialize the frame before interrupts and NMIs on this CPU see it. */
+	barrier();
+	WRITE_ONCE(current->bpf_ctx, run_ctx);
 #endif
+	return true;
+}
+
+/*
+ * Also correct after an enter that returned false: the frame was not linked,
+ * so resetting to its saved_run_ctx changes nothing.
+ */
+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->recur_per_cpu)
+		bpf_prog_put_recursion_context(prog);
 }
 
 /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
@@ -3492,7 +3565,6 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
 {
 	const struct bpf_prog_array_item *item;
 	struct bpf_prog *prog;
-	struct bpf_run_ctx *old_run_ctx;
 	struct bpf_trace_run_ctx run_ctx;
 	u32 ret = 1;
 
@@ -3503,7 +3575,6 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
 
 	run_ctx.is_uprobe = false;
 
-	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
 	item = &array->items[0];
 	while ((prog = READ_ONCE(item->prog))) {
 		/* Skip dummy_bpf_prog placeholder (len == 0) */
@@ -3512,15 +3583,14 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
 			continue;
 		}
 
-		if (unlikely(!bpf_prog_get_recursion_context(prog))) {
+		run_ctx.bpf_cookie = item->bpf_cookie;
+		if (unlikely(!bpf_prog_enter_run_ctx(prog, &run_ctx.run_ctx))) {
 			bpf_prog_inc_misses_counter(prog);
-			bpf_prog_put_recursion_context(prog);
+			bpf_prog_exit_run_ctx(prog, &run_ctx.run_ctx);
 			item++;
 			continue;
 		}
 
-		run_ctx.bpf_cookie = item->bpf_cookie;
-
 		if (!prog->sleepable) {
 			guard(rcu)();
 			ret &= run_prog(prog, ctx);
@@ -3528,10 +3598,9 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
 			ret &= run_prog(prog, ctx);
 		}
 
-		bpf_prog_put_recursion_context(prog);
+		bpf_prog_exit_run_ctx(prog, &run_ctx.run_ctx);
 		item++;
 	}
-	bpf_reset_run_ctx(old_run_ctx);
 	migrate_enable();
 	return ret;
 }
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index f45c18b3cfa2..086d59588d81 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1827,6 +1827,25 @@ static inline bool bpf_prog_check_recur(const struct bpf_prog *prog)
 	}
 }
 
+/* Is the recursion of @prog checked per task, so that two tasks can run it at once on a CPU? */
+static inline bool bpf_prog_recur_per_task(const struct bpf_prog *prog)
+{
+	if (prog->recur_per_cpu)
+		return false;
+
+	switch (resolve_prog_type(prog)) {
+	case BPF_PROG_TYPE_RAW_TRACEPOINT:
+	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
+	/* It may be attached to a syscall tracepoint */
+	case BPF_PROG_TYPE_TRACEPOINT:
+		return true;
+	case BPF_PROG_TYPE_TRACING:
+		return bpf_prog_check_recur(prog);
+	default:
+		return false;
+	}
+}
+
 #define BPF_REG_TRUSTED_MODIFIERS (MEM_ALLOC | PTR_TRUSTED | NON_OWN_REF)
 
 static inline bool bpf_type_has_unsafe_modifiers(u32 type)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ec8e78792911..a05eda7c1eeb 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3849,6 +3849,16 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
 		err = -EINVAL;
 		goto out_unlock;
 	}
+	/*
+	 * An extension runs under the recursion check of its target. One that
+	 * keeps per-CPU state relies on its runs not overlapping on a CPU, which
+	 * a target whose recursion is checked per task doesn't provide.
+	 */
+	if (prog->type == BPF_PROG_TYPE_EXT && prog->recur_per_cpu &&
+	    bpf_prog_recur_per_task(tgt_prog)) {
+		err = -EINVAL;
+		goto out_unlock;
+	}
 	/*
 	 * The size of these contexts depends on what the program is attached
 	 * to, or run with. That is compared with max_ctx_offset of the program,
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index 21d460866dfe..195379cd81ab 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -1342,9 +1342,7 @@ static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tram
 {
 	rcu_read_lock_dont_migrate();
 
-	run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
-
-	if (unlikely(!bpf_prog_get_recursion_context(prog))) {
+	if (unlikely(!bpf_prog_enter_run_ctx(prog, &run_ctx->run_ctx))) {
 		bpf_prog_inc_misses_counter(prog);
 		if (prog->aux->recursion_detected)
 			prog->aux->recursion_detected(prog);
@@ -1385,10 +1383,9 @@ static void notrace __bpf_prog_exit_recur(struct bpf_prog *prog, u64 start,
 					  struct bpf_tramp_run_ctx *run_ctx)
 	__releases(RCU)
 {
-	bpf_reset_run_ctx(run_ctx->saved_run_ctx);
+	bpf_prog_exit_run_ctx(prog, &run_ctx->run_ctx);
 
 	update_prog_stats(prog, start);
-	bpf_prog_put_recursion_context(prog);
 	rcu_read_unlock_migrate();
 }
 
@@ -1401,7 +1398,7 @@ static u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog,
 	 */
 	rcu_read_lock_dont_migrate();
 
-	run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
+	bpf_set_run_ctx(&run_ctx->run_ctx);
 
 	return NO_START_TIME;
 }
@@ -1410,7 +1407,7 @@ static void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start,
 					       struct bpf_tramp_run_ctx *run_ctx)
 	__releases(RCU)
 {
-	bpf_reset_run_ctx(run_ctx->saved_run_ctx);
+	bpf_reset_run_ctx(run_ctx->run_ctx.saved_run_ctx);
 
 	rcu_read_unlock_migrate();
 }
@@ -1422,9 +1419,7 @@ u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog,
 	migrate_disable();
 	might_fault();
 
-	run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
-
-	if (unlikely(!bpf_prog_get_recursion_context(prog))) {
+	if (unlikely(!bpf_prog_enter_run_ctx(prog, &run_ctx->run_ctx))) {
 		bpf_prog_inc_misses_counter(prog);
 		if (prog->aux->recursion_detected)
 			prog->aux->recursion_detected(prog);
@@ -1436,10 +1431,9 @@ u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog,
 void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start,
 					     struct bpf_tramp_run_ctx *run_ctx)
 {
-	bpf_reset_run_ctx(run_ctx->saved_run_ctx);
+	bpf_prog_exit_run_ctx(prog, &run_ctx->run_ctx);
 
 	update_prog_stats(prog, start);
-	bpf_prog_put_recursion_context(prog);
 	migrate_enable();
 	rcu_read_unlock_trace();
 }
@@ -1451,7 +1445,7 @@ static u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog,
 	migrate_disable();
 	might_fault();
 
-	run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
+	bpf_set_run_ctx(&run_ctx->run_ctx);
 
 	return bpf_prog_start_time();
 }
@@ -1459,7 +1453,7 @@ static u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog,
 static void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start,
 					      struct bpf_tramp_run_ctx *run_ctx)
 {
-	bpf_reset_run_ctx(run_ctx->saved_run_ctx);
+	bpf_reset_run_ctx(run_ctx->run_ctx.saved_run_ctx);
 
 	update_prog_stats(prog, start);
 	migrate_enable();
@@ -1472,7 +1466,7 @@ static u64 notrace __bpf_prog_enter(struct bpf_prog *prog,
 {
 	rcu_read_lock_dont_migrate();
 
-	run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
+	bpf_set_run_ctx(&run_ctx->run_ctx);
 
 	return bpf_prog_start_time();
 }
@@ -1481,7 +1475,7 @@ static void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start,
 				    struct bpf_tramp_run_ctx *run_ctx)
 	__releases(RCU)
 {
-	bpf_reset_run_ctx(run_ctx->saved_run_ctx);
+	bpf_reset_run_ctx(run_ctx->run_ctx.saved_run_ctx);
 
 	update_prog_stats(prog, start);
 	rcu_read_unlock_migrate();
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index b4ff9d4e86b3..4cb8a51ba999 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2167,7 +2167,6 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
 	struct srcu_ctr __percpu *scp = NULL;
 	struct bpf_prog *prog = link->link.prog;
 	bool sleepable = prog->sleepable;
-	struct bpf_run_ctx *old_run_ctx;
 	struct bpf_trace_run_ctx run_ctx;
 
 	if (sleepable) {
@@ -2177,19 +2176,15 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
 		rcu_read_lock_dont_migrate();
 	}
 
-	if (unlikely(!bpf_prog_get_recursion_context(prog))) {
+	run_ctx.bpf_cookie = link->cookie;
+	if (unlikely(!bpf_prog_enter_run_ctx(prog, &run_ctx.run_ctx))) {
 		bpf_prog_inc_misses_counter(prog);
 		goto out;
 	}
 
-	run_ctx.bpf_cookie = link->cookie;
-	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
-
 	(void)bpf_prog_run(prog, args);
-
-	bpf_reset_run_ctx(old_run_ctx);
 out:
-	bpf_prog_put_recursion_context(prog);
+	bpf_prog_exit_run_ctx(prog, &run_ctx.run_ctx);
 
 	if (sleepable) {
 		migrate_enable();
diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index cc3236e3c0c4..8b8c4e9ded9d 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -748,9 +748,6 @@ __bpf_prog_test_run_raw_tp(void *data)
 	struct bpf_raw_tp_test_run_info *info = data;
 	struct srcu_ctr __percpu *scp = NULL;
 	struct bpf_trace_run_ctx run_ctx = {};
-	struct bpf_run_ctx *old_run_ctx;
-
-	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
 
 	if (info->prog->sleepable) {
 		scp = rcu_read_lock_tasks_trace();
@@ -759,7 +756,7 @@ __bpf_prog_test_run_raw_tp(void *data)
 		rcu_read_lock();
 	}
 
-	if (unlikely(!bpf_prog_get_recursion_context(info->prog))) {
+	if (unlikely(!bpf_prog_enter_run_ctx(info->prog, &run_ctx.run_ctx))) {
 		bpf_prog_inc_misses_counter(info->prog);
 		goto out;
 	}
@@ -767,7 +764,7 @@ __bpf_prog_test_run_raw_tp(void *data)
 	info->retval = bpf_prog_run(info->prog, info->ctx);
 
 out:
-	bpf_prog_put_recursion_context(info->prog);
+	bpf_prog_exit_run_ctx(info->prog, &run_ctx.run_ctx);
 
 	if (info->prog->sleepable) {
 		migrate_enable();
@@ -775,8 +772,6 @@ __bpf_prog_test_run_raw_tp(void *data)
 	} else {
 		rcu_read_unlock();
 	}
-
-	bpf_reset_run_ctx(old_run_ctx);
 }
 
 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
diff --git a/net/ipv4/bpf_tcp_ops.c b/net/ipv4/bpf_tcp_ops.c
index 2ba73dd6c52c..e1ca34e63de0 100644
--- a/net/ipv4/bpf_tcp_ops.c
+++ b/net/ipv4/bpf_tcp_ops.c
@@ -207,11 +207,11 @@ BPF_CALL_0(bpf_tcp_ops_get_retval)
 	 * return value chained across the bpf_tcp_ops attached to the cgroup
 	 * and is this program's saved_run_ctx.
 	 */
-	if (WARN_ON_ONCE(!ctx->saved_run_ctx))
+	if (WARN_ON_ONCE(!ctx->run_ctx.saved_run_ctx))
 		return 0;
 
-	return container_of(ctx->saved_run_ctx, struct bpf_tramp_run_ctx,
-			    run_ctx)->retval;
+	return container_of(ctx->run_ctx.saved_run_ctx,
+			    struct bpf_tramp_run_ctx, run_ctx)->retval;
 }
 
 const struct bpf_func_proto bpf_tcp_ops_get_retval_proto = {

-- 
2.43.0



^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH bpf-next v3 4/4] selftests/bpf: Add preemption recursion test for tracing programs
  2026-11-17 11:29 [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
                   ` (2 preceding siblings ...)
  2026-11-17 11:29 ` [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
@ 2026-11-17 11:29 ` Mike Bazov via B4 Relay
  2026-11-19  5:09   ` JP Kobryn (CrowdStrike)
  2026-11-19  5:02 ` [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs JP Kobryn (CrowdStrike)
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 28+ messages in thread
From: Mike Bazov via B4 Relay @ 2026-11-17 11:29 UTC (permalink / raw)
  To: bpf, 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, linux-kernel,
	Mike Bazov

From: Mike Bazov <mikeba@sentinelone.com>

Pin two threads to one CPU. Thread B is SCHED_FIFO and waits for a
signal that thread A sends from inside the program under test, so B
preempts A there and enters the same program on the same CPU. Each
subtest runs 20 rounds and checks in every round that A was inside the
program when B ran. It is skipped if that never happens, as on a kernel
that doesn't preempt kernel code.

- fentry, fentry_s, raw_tp, raw_tp_s, tp_syscall: a program without
  per-CPU state. Both threads must run it in every round, without
  recursion misses.
- *_percpu, percpu_var, this_cpu_ptr, tail_call, may_goto: a program
  with a per-CPU map, a per-CPU variable, bpf_this_cpu_ptr(), a tail
  call or timed may_goto. B must be dropped and counted in every
  round. A constant selects the per-CPU state that the handler
  touches; with none selected, the same code doesn't mark the program.
- two_dispatchers: two raw tracepoint programs tail-call one handler.
  Both threads run it at once, and A checks that the stack buffer it
  filled before B ran is intact.
- recursion_raw_tp, recursion_two_hops, recursion_test_run: true
  recursion is still dropped and counted: directly, through a second
  program, and from BPF_PROG_TEST_RUN.
- ext_attach: an extension that uses a per-CPU map can't be attached
  to a raw tracepoint program that doesn't, while one without can.

Signed-off-by: Mike Bazov <mikeba@sentinelone.com>
---
 .../selftests/bpf/prog_tests/trampoline_preempt.c  | 352 +++++++++++++++++++++
 .../selftests/bpf/progs/trampoline_preempt.c       | 258 +++++++++++++++
 2 files changed, 610 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
new file mode 100644
index 000000000000..f282fdba5d16
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
@@ -0,0 +1,352 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <pthread.h>
+#include <sched.h>
+#include <signal.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <test_progs.h>
+#include "trampoline_preempt.skel.h"
+
+#define NR_ROUNDS 20
+
+/* The kernel's own "not supported" error, which the libc headers don't have */
+#ifndef ENOTSUPP
+#define ENOTSUPP 524
+#endif
+
+/*
+ * Threads A and B are pinned to one CPU. B is SCHED_FIFO and waits for a signal:
+ *
+ *   A: prctl() -> enters the program under test
+ *                 the program sends B the signal
+ *   B:            wakes up and preempts A, which is still inside the program
+ *                 prctl() -> enters the same program on the same CPU
+ *
+ * A program whose recursion is checked per task runs for B too. A program that
+ * keeps per-CPU state keeps the per-CPU check: B's run is dropped and counted
+ * as a recursion miss.
+ */
+
+/* The per-CPU state that a program touches, see progs/trampoline_preempt.c */
+enum { NO_PERCPU, PERCPU_MAP, PERCPU_VAR, THIS_CPU_PTR };
+
+enum expect {
+	B_RUNS,		/* B runs the program, without recursion misses */
+	B_DROPPED,	/* B's runs are dropped and counted as recursion misses */
+	B_RUNS_HANDLER,	/* two_dispatchers: dropped by one dispatcher, runs through the other */
+};
+
+/* What threads A and B share */
+struct ctx {
+	struct trampoline_preempt *skel;
+	sigset_t sigset;
+	int cpu, err_a, err_b;
+	int overlaps;	/* rounds in which B found A still inside the program */
+};
+
+/* Only timed may_goto keeps its budget per CPU, and only these JITs have it */
+static bool has_timed_may_goto(void)
+{
+#if defined(__x86_64__) || defined(__aarch64__) || defined(__s390x__) || \
+	defined(__powerpc64__) || (defined(__riscv) && __riscv_xlen == 64) || \
+	(defined(__loongarch__) && __loongarch_grlen == 64)
+	return is_jit_enabled();
+#else
+	return false;
+#endif
+}
+
+static int pin_to_cpu(int cpu)
+{
+	cpu_set_t set;
+
+	CPU_ZERO(&set);
+	CPU_SET(cpu, &set);
+	return sched_setaffinity(0, sizeof(set), &set) ? -errno : 0;
+}
+
+/* A enters the program under test, which wakes B from inside it */
+static void *worker_a(void *arg)
+{
+	struct ctx *c = arg;
+	int i;
+
+	c->err_a = pin_to_cpu(c->cpu);
+	c->skel->bss->tid_a = syscall(SYS_gettid);
+	for (i = 0; i < NR_ROUNDS && !c->err_a; i++) {
+		/* Passes every hook that the programs under test are attached to */
+		if (prctl(PR_SET_NAME, "worker_a"))
+			c->err_a = -errno;
+	}
+	return NULL;
+}
+
+/* B is SCHED_FIFO on A's CPU: A runs only while B waits, and B preempts A as soon as A wakes it */
+static void *worker_b(void *arg)
+{
+	struct sched_param sp = { .sched_priority = 1 };
+	struct timespec ts = { .tv_sec = 5 };	/* give up if the signal never comes */
+	struct ctx *c = arg;
+	int i;
+
+	c->err_b = pin_to_cpu(c->cpu);
+	if (!c->err_b && sched_setscheduler(0, SCHED_FIFO, &sp))
+		c->err_b = -errno;
+	c->skel->bss->tid_b = syscall(SYS_gettid);
+	for (i = 0; i < NR_ROUNDS && !c->err_b; i++) {
+		while (sigtimedwait(&c->sigset, NULL, &ts) < 0) {
+			if (errno != EINTR) {
+				c->err_b = -errno;
+				return NULL;
+			}
+		}
+		/* B preempted A only if A is still inside the program */
+		if (*(volatile int *)&c->skel->bss->a_inside)
+			c->overlaps++;
+		/* B's own run of the program, while A is preempted inside it */
+		if (prctl(PR_SET_NAME, "worker_b"))
+			c->err_b = -errno;
+	}
+	return NULL;
+}
+
+/* Load and attach only programs @a, @b and @c. NULL after a failure or a skip. */
+static struct trampoline_preempt *load_progs(const char *a, const char *b, const char *c,
+					      int percpu)
+{
+	struct trampoline_preempt *skel = trampoline_preempt__open();
+	struct bpf_program *p;
+	const char *name;
+	int err;
+
+	if (!ASSERT_OK_PTR(skel, "open"))
+		return NULL;
+	bpf_object__for_each_program(p, skel->obj) {
+		name = bpf_program__name(p);
+		bpf_program__set_autoload(p, !strcmp(name, a) || (b && !strcmp(name, b)) ||
+					     (c && !strcmp(name, c)));
+	}
+	skel->bss->sig = SIGUSR1;
+	skel->rodata->percpu = percpu;
+	err = trampoline_preempt__load(skel);
+	/* The kernel or its JIT doesn't support one of the programs */
+	if (err == -EOPNOTSUPP || err == -ENOTSUPP)
+		test__skip();
+	else if (ASSERT_OK(err, "load") && ASSERT_OK(trampoline_preempt__attach(skel), "attach"))
+		return skel;
+	trampoline_preempt__destroy(skel);
+	return NULL;
+}
+
+static const struct subtest {
+	const char *name;
+	const char *prog;	/* A enters it; its recursion misses are checked */
+	int percpu;		/* the per-CPU state that the program touches */
+	enum expect expect;
+	const char *prog_b;	/* two_dispatchers: B reaches the handler through this one */
+	const char *handler;	/* two_dispatchers: the target of both tail calls */
+} subtests[] = {
+	/* Each kind of program, without per-CPU state and with a per-CPU map lookup */
+	{ "fentry",		"on_fentry",		NO_PERCPU,	B_RUNS },
+	{ "fentry_percpu",	"on_fentry",		PERCPU_MAP,	B_DROPPED },
+	{ "fentry_s",		"on_fentry_s",		NO_PERCPU,	B_RUNS },
+	{ "fentry_s_percpu",	"on_fentry_s",		PERCPU_MAP,	B_DROPPED },
+	{ "raw_tp",		"on_raw_tp",		NO_PERCPU,	B_RUNS },
+	{ "raw_tp_percpu",	"on_raw_tp",		PERCPU_MAP,	B_DROPPED },
+	{ "raw_tp_s",		"on_raw_tp_s",		NO_PERCPU,	B_RUNS },
+	{ "raw_tp_s_percpu",	"on_raw_tp_s",		PERCPU_MAP,	B_DROPPED },
+	{ "tp_syscall",		"on_tp_syscall",	NO_PERCPU,	B_RUNS },
+	{ "tp_syscall_percpu",	"on_tp_syscall",	PERCPU_MAP,	B_DROPPED },
+	/* The other things that keep a program on the per-CPU check */
+	{ "percpu_var",		"on_raw_tp",		PERCPU_VAR,	B_DROPPED },
+	{ "this_cpu_ptr",	"on_raw_tp",		THIS_CPU_PTR,	B_DROPPED },
+	{ "tail_call",		"on_tail_call",		NO_PERCPU,	B_DROPPED },
+	{ "may_goto",		"on_may_goto",		NO_PERCPU,	B_DROPPED },
+	/* Both threads are inside the handler at once, and A's stack must stay intact */
+	{ "two_dispatchers",	"dispatcher_a",		NO_PERCPU,	B_RUNS_HANDLER,
+	  "dispatcher_b", "handler" },
+};
+
+static void run_subtest(const struct subtest *t)
+{
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info), zero = 0;
+	struct ctx c = {};
+	sigset_t oldset;
+	pthread_t ta, tb;
+	int err, fd;
+
+	if (!strcmp(t->name, "may_goto") && !has_timed_may_goto()) {
+		test__skip();
+		return;
+	}
+	c.skel = load_progs(t->prog, t->prog_b, t->handler, t->percpu);
+	if (!c.skel)
+		return;
+	if (t->handler) {
+		/* Both dispatchers tail-call the handler through the prog array */
+		fd = bpf_program__fd(bpf_object__find_program_by_name(c.skel->obj, t->handler));
+		err = bpf_map_update_elem(bpf_map__fd(c.skel->maps.prog_array), &zero, &fd,
+					  BPF_ANY);
+		if (!ASSERT_OK(err, "prog_array_update"))
+			goto out;
+	}
+
+	/* Both threads go on this CPU. B takes the signal with sigtimedwait(), so block it. */
+	c.cpu = sched_getcpu();
+	sigemptyset(&c.sigset);
+	sigaddset(&c.sigset, SIGUSR1);
+	if (!ASSERT_OK(pthread_sigmask(SIG_BLOCK, &c.sigset, &oldset), "sigmask"))
+		goto out;
+	if (ASSERT_OK(pthread_create(&tb, NULL, worker_b, &c), "pthread_b")) {
+		/* A's program needs tid_b to wake B */
+		while (!c.skel->bss->tid_b)
+			usleep(1000);
+		if (ASSERT_OK(pthread_create(&ta, NULL, worker_a, &c), "pthread_a"))
+			pthread_join(ta, NULL);
+		pthread_join(tb, NULL);
+	}
+	pthread_sigmask(SIG_SETMASK, &oldset, NULL);
+
+	/* B may not be allowed to become SCHED_FIFO */
+	if (c.err_b == -EPERM || c.err_b == -EACCES) {
+		test__skip();
+		goto out;
+	}
+	ASSERT_OK(c.err_a, "worker_a");
+	ASSERT_OK(c.err_b, "worker_b");
+	ASSERT_EQ(c.skel->bss->sig_err, 0, "sig_err");
+	if (!c.overlaps) {
+		/* B never preempted A inside the program, so nothing was tested */
+		test__skip();
+		goto out;
+	}
+	ASSERT_EQ(c.overlaps, NR_ROUNDS, "overlaps");
+	ASSERT_EQ(c.skel->bss->runs_a, NR_ROUNDS, "runs_a");
+	ASSERT_EQ(c.skel->bss->runs_b, t->expect == B_DROPPED ? 0 : NR_ROUNDS, "runs_b");
+	if (t->handler)
+		ASSERT_EQ(c.skel->bss->stack_corrupt, 0, "stack_corrupt");
+	fd = bpf_program__fd(bpf_object__find_program_by_name(c.skel->obj, t->prog));
+	err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
+	if (!ASSERT_OK(err, "prog_info"))
+		goto out;
+	/* A program on sys_enter also misses B's other syscalls: at least one miss per round */
+	if (t->expect == B_RUNS)
+		ASSERT_EQ(info.recursion_misses, 0, "recursion_misses");
+	else
+		ASSERT_GE(info.recursion_misses, NR_ROUNDS, "recursion_misses");
+out:
+	trampoline_preempt__destroy(c.skel);
+}
+
+/*
+ * The program calls bpf_printk(), which fires the bpf_trace_printk tracepoint
+ * that it is attached to: the nested run must be rejected as recursion. With a
+ * hop, a second program runs in between, so the check has to look past the
+ * frame right below.
+ */
+static const struct recursion {
+	const char *name, *prog, *hop;
+	bool test_run;	/* Start the outer run with BPF_PROG_TEST_RUN of @prog */
+} recursions[] = {
+	{ "recursion_raw_tp", "recur_raw_tp" },
+	{ "recursion_two_hops", "recur_two_hops", "recur_hop" },
+	{ "recursion_test_run", "recur_raw_tp", .test_run = true },
+};
+
+static void run_recursion(const struct recursion *t)
+{
+	struct trampoline_preempt *skel = load_progs(t->prog, t->hop, "recur_trigger", NO_PERCPU);
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info);
+	int err, fd, run_fd;
+
+	if (!skel)
+		return;
+	fd = bpf_program__fd(bpf_object__find_program_by_name(skel->obj, t->prog));
+	/* The outer run: of @prog itself, or of a program that fires its tracepoint */
+	run_fd = t->test_run ? fd : bpf_program__fd(skel->progs.recur_trigger);
+	skel->bss->recur_tid = syscall(SYS_gettid);
+	err = bpf_prog_test_run_opts(run_fd, &opts);
+	skel->bss->recur_tid = 0;
+	/* The outer run got in; the nested one was dropped and counted */
+	if (ASSERT_OK(err, "test_run")) {
+		ASSERT_EQ(skel->bss->recur_runs, 1, "recur_runs");
+		ASSERT_EQ(skel->bss->hop_runs, t->hop ? 1 : 0, "hop_runs");
+		err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
+		if (ASSERT_OK(err, "prog_info"))
+			ASSERT_EQ(info.recursion_misses, 1, "recursion_misses");
+	}
+	trampoline_preempt__destroy(skel);
+}
+
+/*
+ * An extension runs under the recursion check of its target. A target that
+ * keeps no per-CPU state is checked per task, so an extension that does keep
+ * such state can't be attached to it.
+ */
+static void run_ext_subtest(void)
+{
+	static const struct {
+		const char *prog;
+		int err;
+	} exts[] = {
+		{ "ext_plain", 0 },		/* no per-CPU state: attaches */
+		{ "ext_percpu", -EINVAL },	/* a per-CPU map lookup: rejected */
+	};
+	struct trampoline_preempt *tgt_skel, *skel = NULL;
+	struct bpf_program *ext, *p;
+	struct bpf_link *link;
+	int i, err, tgt_fd;
+
+	tgt_skel = trampoline_preempt__open();
+	if (!ASSERT_OK_PTR(tgt_skel, "open_tgt"))
+		return;
+	bpf_object__for_each_program(p, tgt_skel->obj)
+		bpf_program__set_autoload(p, p == tgt_skel->progs.ext_target);
+	if (!ASSERT_OK(trampoline_preempt__load(tgt_skel), "load_tgt"))
+		goto out;
+	tgt_fd = bpf_program__fd(tgt_skel->progs.ext_target);
+
+	for (i = 0; i < ARRAY_SIZE(exts); i++) {
+		skel = trampoline_preempt__open();
+		if (!ASSERT_OK_PTR(skel, "open_ext"))
+			goto out;
+		ext = bpf_object__find_program_by_name(skel->obj, exts[i].prog);
+		bpf_object__for_each_program(p, skel->obj)
+			bpf_program__set_autoload(p, p == ext);
+		err = bpf_program__set_attach_target(ext, tgt_fd, "ext_target");
+		if (!ASSERT_OK(err, "set_attach_target"))
+			goto out;
+		if (!ASSERT_OK(trampoline_preempt__load(skel), "load_ext"))
+			goto out;
+
+		link = bpf_program__attach_freplace(ext, tgt_fd, "ext_target");
+		ASSERT_EQ(libbpf_get_error(link), exts[i].err, "attach_ext");
+		bpf_link__destroy(link);
+		trampoline_preempt__destroy(skel);
+		skel = NULL;
+	}
+out:
+	trampoline_preempt__destroy(skel);
+	trampoline_preempt__destroy(tgt_skel);
+}
+
+void test_trampoline_preempt(void)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(subtests); i++) {
+		if (test__start_subtest(subtests[i].name))
+			run_subtest(&subtests[i]);
+	}
+	for (i = 0; i < ARRAY_SIZE(recursions); i++) {
+		if (test__start_subtest(recursions[i].name))
+			run_recursion(&recursions[i]);
+	}
+	if (test__start_subtest("ext_attach"))
+		run_ext_subtest();
+}
diff --git a/tools/testing/selftests/bpf/progs/trampoline_preempt.c b/tools/testing/selftests/bpf/progs/trampoline_preempt.c
new file mode 100644
index 000000000000..ce596528c8a5
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/trampoline_preempt.c
@@ -0,0 +1,258 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <asm/unistd.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_compiler.h"
+#include <bpf_may_goto.h>
+
+/* The programs of prog_tests/trampoline_preempt.c. The comment there describes the test. */
+
+char _license[] SEC("license") = "GPL";
+
+struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
+void bpf_task_release(struct task_struct *p) __ksym;
+int bpf_send_signal_task(struct task_struct *task, int sig, enum pid_type type, u64 value) __ksym;
+
+/* The target of the tail calls: empty for tail_call, the handler for two_dispatchers */
+struct {
+	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+} prog_array SEC(".maps");
+
+/* recur_two_hops deletes from it, which runs recur_hop */
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 1);
+	__type(key, int);
+	__type(value, int);
+} recur_map SEC(".maps");
+
+/* The three kinds of per-CPU state: a map, a variable, and a kernel variable for the helper */
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, int);
+	__type(value, __u64);
+} percpu_map SEC(".maps");
+
+__u64 percpu_var SEC(".percpu");
+extern const struct rq runqueues __ksym;
+
+/* The per-CPU state that a program touches. The test picks one before the load. */
+enum { NO_PERCPU, PERCPU_MAP, PERCPU_VAR, THIS_CPU_PTR };
+const volatile int percpu;
+
+/* Set by the test */
+int tid_a, tid_b, sig, recur_tid;
+int loops;	/* 0, read at run time so that the may_goto loop is not optimized away */
+
+/* Read by the test */
+int runs_a, runs_b, sig_err, stack_corrupt, recur_runs, hop_runs;
+int a_inside;	/* A is inside the program: B reads it to see that it preempted A there */
+
+static void signal_worker_b(void)
+{
+	struct task_struct *task = bpf_task_from_pid(tid_b);
+
+	if (!task || bpf_send_signal_task(task, sig, PIDTYPE_PID, 0))
+		sig_err = 1;
+	if (task)
+		bpf_task_release(task);
+}
+
+/*
+ * The body of every program under test: count the run of A or of B. A also
+ * wakes B from here, so B preempts A inside the program and enters it too.
+ */
+static __always_inline int record_run(void)
+{
+	__u32 tid = (__u32)bpf_get_current_pid_tgid();
+	int zero = 0;
+
+	/* The verifier walks only the selected branch, so only that one marks the program */
+	switch (percpu) {
+	case PERCPU_MAP:
+		bpf_map_lookup_elem(&percpu_map, &zero);
+		break;
+	case PERCPU_VAR:
+		percpu_var++;
+		break;
+	case THIS_CPU_PTR:
+		bpf_this_cpu_ptr(&runqueues);
+		break;
+	}
+
+	if (tid == tid_a) {
+		runs_a++;
+		/* volatile: both stores must stay, B reads a_inside between them */
+		*(volatile int *)&a_inside = 1;
+		signal_worker_b();
+		*(volatile int *)&a_inside = 0;
+	} else if (tid == tid_b) {
+		runs_b++;
+	}
+	return 0;
+}
+
+/* Both threads call prctl(PR_SET_NAME), which passes each of the hooks below */
+
+SEC("fentry/__set_task_comm")
+int BPF_PROG(on_fentry)
+{
+	return record_run();
+}
+
+SEC("fentry.s/" SYS_PREFIX "sys_prctl")
+int BPF_PROG(on_fentry_s)
+{
+	return record_run();
+}
+
+SEC("raw_tp/task_rename")
+int BPF_PROG(on_raw_tp)
+{
+	return record_run();
+}
+
+SEC("raw_tp.s/sys_enter")
+int BPF_PROG(on_raw_tp_s, struct pt_regs *regs, long id)
+{
+	return id == __NR_prctl ? record_run() : 0;
+}
+
+SEC("tp/syscalls/sys_enter_prctl")
+int on_tp_syscall(void *ctx)
+{
+	return record_run();
+}
+
+/* The prog array is empty, so the tail call does nothing */
+SEC("raw_tp/task_rename")
+int on_tail_call(void *ctx)
+{
+	bpf_tail_call(ctx, &prog_array, 0);
+	return record_run();
+}
+
+/* can_loop is a may_goto. With timed may_goto its time budget is per CPU. */
+SEC("raw_tp/task_rename")
+int on_may_goto(void *ctx)
+{
+	int i;
+
+	for (i = loops; i < 1 && can_loop; i++)
+		;
+	return record_run();
+}
+
+/* Two dispatchers tail-call one handler: A enters through dispatcher_a, B through dispatcher_b */
+SEC("raw_tp/task_rename")
+int dispatcher_a(void *ctx)
+{
+	if ((__u32)bpf_get_current_pid_tgid() == tid_a)
+		bpf_tail_call(ctx, &prog_array, 0);
+	return 0;
+}
+
+SEC("raw_tp/sys_enter")
+int BPF_PROG(dispatcher_b, struct pt_regs *regs, long id)
+{
+	if (id == __NR_prctl && (__u32)bpf_get_current_pid_tgid() == tid_b)
+		bpf_tail_call(ctx, &prog_array, 0);
+	return 0;
+}
+
+/* The handler keeps 64 bytes of stack across the point where B runs it too */
+SEC("raw_tp")
+int handler(void *ctx)
+{
+	__u32 tid = (__u32)bpf_get_current_pid_tgid();
+	__u64 buf[8];
+
+	__pragma_loop_unroll
+	for (int i = 0; i < 8; i++)
+		buf[i] = ((__u64)tid << 32) | i;
+	/* Make the compiler keep buf on the stack across record_run() */
+	asm volatile("" : "+m"(buf));
+
+	record_run();
+
+	asm volatile("" : "+m"(buf));
+	/* Don't trust a copy of tid that may have been spilled to the stack under test */
+	tid = (__u32)bpf_get_current_pid_tgid();
+	__pragma_loop_unroll
+	for (int i = 0; i < 8; i++) {
+		if (buf[i] != (((__u64)tid << 32) | i))
+			stack_corrupt = 1;
+	}
+	return 0;
+}
+
+/* bpf_printk() fires the bpf_trace_printk tracepoint that the program is attached to */
+SEC("raw_tp/bpf_trace_printk")
+int recur_raw_tp(void *ctx)
+{
+	/* Other tasks print too: only the runs of the test's thread count */
+	if ((__u32)bpf_get_current_pid_tgid() != recur_tid)
+		return 0;
+	recur_runs++;
+	bpf_printk("recur");
+	return 0;
+}
+
+/* The test runs it with BPF_PROG_TEST_RUN to fire the tracepoint once */
+SEC("syscall")
+int recur_trigger(void *ctx)
+{
+	bpf_printk("trigger");
+	return 0;
+}
+
+/* The same through a second program: recur_two_hops -> recur_hop -> recur_two_hops */
+SEC("raw_tp/bpf_trace_printk")
+int recur_two_hops(void *ctx)
+{
+	int zero = 0;
+
+	if ((__u32)bpf_get_current_pid_tgid() != recur_tid)
+		return 0;
+	recur_runs++;
+	bpf_map_delete_elem(&recur_map, &zero);
+	return 0;
+}
+
+SEC("fentry/htab_map_delete_elem")
+int BPF_PROG(recur_hop, struct bpf_map *map)
+{
+	if (map != (struct bpf_map *)&recur_map || (__u32)bpf_get_current_pid_tgid() != recur_tid)
+		return 0;
+	hop_runs++;
+	bpf_printk("hop");
+	return 0;
+}
+
+/* For ext_attach: a target without per-CPU state, and an extension without and with it */
+SEC("raw_tp/task_rename")
+int ext_target(struct bpf_raw_tracepoint_args *ctx)
+{
+	return 0;
+}
+
+SEC("freplace")
+int ext_plain(struct bpf_raw_tracepoint_args *ctx)
+{
+	return 0;
+}
+
+SEC("freplace")
+int ext_percpu(struct bpf_raw_tracepoint_args *ctx)
+{
+	int zero = 0;
+
+	bpf_map_lookup_elem(&percpu_map, &zero);
+	return 0;
+}

-- 
2.43.0



^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx
  2026-11-17 11:29 ` [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
@ 2026-11-17 12:20   ` bot+bpf-ci
  2026-11-19  5:05   ` JP Kobryn (CrowdStrike)
  2026-11-24 22:04   ` Andrii Nakryiko
  2 siblings, 0 replies; 28+ messages in thread
From: bot+bpf-ci @ 2026-11-17 12:20 UTC (permalink / raw)
  To: mikeba, bpf, ast, daniel, andrii
  Cc: martin.lau, eddyz87, memxor, song, yonghong.song, jolsa,
	john.fastabend, emil, ihor.solodrai, rostedt, mhiramat,
	mathieu.desnoyers, jp.kobryn, linux-trace-kernel, linux-kernel,
	mikeba, ast, andrii, daniel, martin.lau, eddyz87, yonghong.song,
	mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 4346 bytes --]

> bpf: Check program recursion per task via bpf_ctx

TL;DR: Commit message only: the trade-off list omits that attaching a marked
freplace extension to an fmod_ret target now also fails with -EINVAL, a
userspace-visible change.

> bpf: Check program recursion per task via bpf_ctx
>
> Trampoline programs (fentry, fexit, fsession, fmod_ret and their
> sleepable variants), raw tracepoint programs (raw_tp, raw_tp.s, tp_btf)
> and tracepoint programs on syscall tracepoints run preemptibly, but
> their recursion check is a per-CPU, per-program counter. When a task is
> preempted inside a program, the counter stays raised, and every other
> task that enters the program on that CPU is dropped and counted as a
> recursion miss, although nothing recursed. A program can't avoid that:
> the counter is checked before its first insn.
>
> Since commit 7dadeaa6e851 ("sched: Further restrict the preemption
> modes") x86 and arm64 have only lazy and full preemption, so this
> happens in normal operation. A security agent that pairs fentry and
> fexit programs on inet_sendmsg() and inet_recvmsg() loses one half of a
> pair with every drop. With 200,000 HTTP requests and four CPU-bound
> tasks on a 4-CPU VM, 9,254 of 7,966,760 runs of these programs were
> dropped on bpf-next, and none of 7,975,426 with this series.
>
> Check recursion per task instead. The runners already link a
> stack-allocated bpf_run_ctx into current->bpf_ctx. Move saved_run_ctx
> into struct bpf_run_ctx, so every frame links to the outer one, and
> record in each frame the program it runs. A program is recursing if a
> frame in the current task's chain already runs it. Interrupts and NMIs
> run on top of the interrupted task's chain, so recursion through them
> is still caught.
>
> bpf_prog_enter_run_ctx() and bpf_prog_exit_run_ctx() do the check for
> all runners. The syscall tracepoint runner linked one frame around its
> program array and now links one per program. bpf_set_run_ctx() accepts
> a NULL ctx, which io_uring passes to hide the BPF context; the frames
> below are not walked while it is hidden.
>
> Programs marked by the previous patch keep the per-CPU counter and
> behave exactly as before, so no counted drop is traded for corrupted
> per-CPU data or a renewed may_goto budget. An extension (freplace) runs
> under the check of its target, so a marked one can't be attached to a
> target that is checked per task.
>
> Trade-offs:
>
> - An unmarked program that keeps per-CPU slots of its own, see the
>   previous patch, can have two runs use one slot, and nothing counts
>   that.
> - Marked programs lose runs to preemption as before.
> - bench trig-rawtp is about 2% slower: 42.6 and 42.1 M/s on bpf-next,
>   41.3 and 41.5 M/s with the series. trig-fentry and trig-fexit are
>   not slower.
> - Attaching a marked extension to an unmarked raw_tp, tp_btf or
>   tracepoint program now fails with -EINVAL.
>
> Signed-off-by: Mike Bazov <mikeba@sentinelone.com>
>

The trade-off list says a marked extension fails with -EINVAL when
attached to an unmarked raw_tp, tp_btf or tracepoint program.  Is the list
complete?

bpf_prog_recur_per_task() returns bpf_prog_check_recur(prog) for
BPF_PROG_TYPE_TRACING:

	case BPF_PROG_TYPE_TRACING:
		return bpf_prog_check_recur(prog);

That is true for every expected_attach_type except BPF_TRACE_ITER, so it
is also true for BPF_MODIFY_RETURN.  bpf_check_attach_target() only rejects
an extension when the target is fentry, fexit, fsession or one of their
_MULTI variants, so a non-sleepable fmod_ret program with a global subprog
is a valid freplace target.

An extension that uses a per-CPU map, a tail call, timed may_goto or
bpf_this_cpu_ptr()/bpf_per_cpu_ptr() could be attached to such a target
before this patch.  It now hits the new check in bpf_tracing_prog_attach():

	if (prog->type == BPF_PROG_TYPE_EXT && prog->recur_per_cpu &&
	    bpf_prog_recur_per_task(tgt_prog)) {
		err = -EINVAL;
		goto out_unlock;
	}

Should the commit message list fmod_ret targets next to raw_tp, tp_btf and
tracepoint, since this is a userspace-visible attach failure?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44076814540

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-17 11:29 [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
                   ` (3 preceding siblings ...)
  2026-11-17 11:29 ` [PATCH bpf-next v3 4/4] selftests/bpf: Add preemption recursion test for tracing programs Mike Bazov via B4 Relay
@ 2026-11-19  5:02 ` JP Kobryn (CrowdStrike)
       [not found]   ` <CAPnbwjpmRCR3F4V3StUz=702AdWZzHHZjP68GepnYdYykjoC8g@mail.gmail.com>
  2026-11-24  6:21 ` Alexei Starovoitov
  2026-11-25  2:06 ` JP Kobryn (CrowdStrike)
  6 siblings, 1 reply; 28+ messages in thread
From: JP Kobryn (CrowdStrike) @ 2026-11-19  5:02 UTC (permalink / raw)
  To: mikeba, bpf, 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, linux-trace-kernel, linux-kernel,
	Martin Kelly, Andrey Grodzovsky, tyler.wilson

Hi Mike,
On 11/17/26 3:29 AM, Mike Bazov via B4 Relay wrote:
> fentry, fexit, raw_tp and syscall tracepoint programs can be preempted
> while they run, but their recursion check is a per-CPU counter:
> 
>   task A enters prog on CPU 0       prog->active = 1
>   task A is preempted inside prog
>   task B enters prog on CPU 0       dropped, counted as recursion miss
> 
> Nothing recursed, but B's event is lost. Since 7.0, x86 and arm64 have
> only lazy and full preemption, so this happens on stock kernels. Our
> fentry/fexit probe on inet_sendmsg() and inet_recvmsg() loses 9,254 of
> 8 million runs under HTTP load on a 4-CPU VM. With the series it loses
> none.
> 
> The series checks recursion per task instead: a program is recursing
> only if the current task is already inside it.
> 
> v2 did that for every program, which breaks a program that uses a
> per-CPU map as scratch space: the second task overwrites its buffer,
> and nothing counts that. So in v3 the verifier marks the programs that
> keep per-CPU state, and those keep the per-CPU counter and behave
> exactly as today:
> 
>   - per-CPU maps, bpf_this_cpu_ptr(), bpf_per_cpu_ptr()
>   - tail calls: the target has no recursion check of its own
>   - timed may_goto: its budget is per CPU
>   - a private stack
> 
> Trade-offs:
> 
> - Marked programs are not fixed. A program with a per-CPU map or a
>   tail call loses runs to preemption as it does today.
> - The verifier can't see everything. A program that keeps per-CPU
>   slots of its own, like array[bpf_get_smp_processor_id()], is not
>   marked. Today the counter keeps its runs apart. With the series two
>   runs can write one slot, and nothing counts that.
> - raw_tp is about 2% slower in bench, see the table below. fentry and
>   fexit are within noise.
> - Tracing, raw_tp and tracepoint programs lose the private stack
>   (patch 1). It is already unsafe for them: two raw_tp programs that
>   tail-call one handler let two tasks share its stack today. A program
>   with two 2000-byte frames no longer loads.
> - A marked freplace program can't be attached to an unmarked raw_tp or
>   tracepoint program: -EINVAL.
> 
> One question: the marking is automatic. A flag that the program's
> author sets would avoid the second trade-off, but existing programs
> would keep losing runs until they are rebuilt. Which do you prefer?
> 
>   1. bpf: Avoid per-CPU private stack in preemptible tracing progs
>   2. bpf: Mark programs whose runs must not overlap on a CPU
>   3. bpf: Check program recursion per task via bpf_ctx
>   4. selftests/bpf: Add preemption recursion test for tracing programs
> 
> Testing, x86-64 VM with 4 vCPUs:
> 
> The selftest pins two tasks to one CPU and makes B enter the program
> while A is preempted inside it, 20 rounds per subtest. With the series
> all 19 subtests pass. On bpf-next 7 fail: B is dropped in every round
> (fentry, fentry_s, raw_tp, raw_tp_s, tp_syscall), A's stack is
> overwritten (two_dispatchers), and the freplace attach is not rejected
> (ext_attach).
> 
> bench -w2 -d12 -a trig-<x>, M/s, median of 10 runs [min-max], lazy
> preemption, in boot order, on bpf-next 0fb18ce8a2e9:
> 
>                trig-fentry          trig-fexit           trig-rawtp
>   bpf-next     41.65 [40.81-42.14]  38.89 [37.58-39.41]  42.62 [41.95-42.80]
>   series       42.40 [41.87-43.04]  39.33 [35.24-41.68]  41.31 [40.48-41.74]
>   bpf-next #2  41.64 [40.87-41.74]  39.29 [35.18-40.62]  42.05 [41.24-42.35]
>   series #2    42.34 [42.12-42.78]  39.87 [39.46-41.38]  41.53 [40.79-41.75]

We tested this series and wanted to share the data. Overall unmarked LSM/fentry
hooks previously affected went to zero. Marked hooks remained about the same or
regressed slightly. Normalized data:

category		before	after
raw_tp (marked)		240	213
fexit (marked)		61	135
fentry (unmarked)	3	0
other (unmarked)	805	0

It's nice to see the series progressing since we are affected by the same issue.
See some comments on the individual patches.

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 2/4] bpf: Mark programs whose runs must not overlap on a CPU
  2026-11-17 11:29 ` [PATCH bpf-next v3 2/4] bpf: Mark programs whose runs must not overlap on a CPU Mike Bazov via B4 Relay
@ 2026-11-19  5:04   ` JP Kobryn (CrowdStrike)
  2026-11-19 11:23     ` [EXT.MSG] " Mike Bazov
  0 siblings, 1 reply; 28+ messages in thread
From: JP Kobryn (CrowdStrike) @ 2026-11-19  5:04 UTC (permalink / raw)
  To: mikeba, bpf, 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, linux-trace-kernel, linux-kernel,
	Martin Kelly, Andrey Grodzovsky, tyler.wilson

On 11/17/26 3:29 AM, Mike Bazov via B4 Relay wrote:
> From: Mike Bazov <mikeba@sentinelone.com>
> 
> The per-CPU recursion counter of tracing and raw tracepoint programs
> also keeps a second task out of a program while another task is
> preempted inside it on the same CPU. Programs rely on that:
> 
> - A per-CPU map value that a program looked up stays its own until the
>   run ends, which is how programs use these maps as scratch space.
> - The budget of timed may_goto is per-CPU memory that the first insns
>   of a run reset, see commit ec274bb4665c ("bpf: One may_goto time
>   slice per prog invocation"). A second run on the CPU gives the first
>   one more time.
> - A private stack is per CPU. After the previous patch, struct_ops
>   programs that request one are the only programs with this check that
>   still have it.
> 
> The next patch checks recursion per task, which lets a second task in.
> For these programs that would turn a counted, dropped run into silently
> corrupted data, or into a may_goto loop that another task keeps
> renewing. Mark them at load time, so that they keep the per-CPU counter.
> 
> Set prog->recur_per_cpu when the verifier sees:
> 
> - a helper called with a per-CPU map, also through an inner map,
> - the address of a per-CPU array value loaded directly,
> - bpf_this_cpu_ptr() or bpf_per_cpu_ptr(),
> - a tail call: the target runs without its own recursion check,
> - timed may_goto,
> - a private stack, in a struct_ops program that requests one.

This feels brittle. As code evolves, it may be hard to keep track of these.
Would explicitly opting in avoid unexpected regressions?

If the marking approach is accepted, would it make sense to make it visible
via bpftool?

> 
> Only verified code counts: a per-CPU map referenced in a branch that a
> constant in .rodata disables doesn't mark the program.
> 
> No functional change: nothing reads the flag yet.
> 
> Trade-offs, once the next patch reads it:
> 
> - A program that keeps per-CPU slots of its own, e.g. in a regular
>   array indexed by bpf_get_smp_processor_id(), is not marked.
> - Marked programs keep losing runs to preemption.
> 
> Signed-off-by: Mike Bazov <mikeba@sentinelone.com>
> ---
>  include/linux/bpf.h   |  3 ++-
>  kernel/bpf/fixups.c   |  1 +
>  kernel/bpf/verifier.c | 11 +++++++++++
>  3 files changed, 14 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index a5ad08b7937c..7873f0ef2817 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -2063,7 +2063,8 @@ struct bpf_prog {
>  				call_get_func_ip:1, /* Do we call get_func_ip() */
>  				call_session_cookie:1, /* Do we call bpf_session_cookie() */
>  				tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */
> -				sleepable:1;	/* BPF program is sleepable */
> +				sleepable:1,	/* BPF program is sleepable */
> +				recur_per_cpu:1; /* Runs must not overlap on a CPU */
>  	enum bpf_prog_type	type;		/* Type of BPF program */
>  	enum bpf_attach_type	expected_attach_type; /* For some prog types */
>  	u32			len;		/* Number of filter blocks */
> diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
> index 4d9b67981520..82ad0d3bf465 100644
> --- a/kernel/bpf/fixups.c
> +++ b/kernel/bpf/fixups.c
> @@ -2591,6 +2591,7 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
>  				if (!tmg)
>  					return -ENOMEM;
>  				prog->aux->timed_may_goto = tmg;
> +				prog->recur_per_cpu = true;
>  			}
>  			prog->jit_required = 1;
>  			cnt = may_goto_expand_timed(insn_buf, &jmp, tmg, reg, save_off);
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 2ca1469d8dee..3d54520df3e3 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -6050,6 +6050,7 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
>  		if (round_up_stack_depth(env, si[s].stack_depth) >= BPF_PRIV_STACK_MIN_SIZE) {
>  			si[s].priv_stack_mode = PRIV_STACK_ADAPTIVE;
>  			env->prog->aux->jits_use_priv_stack = true;
> +			env->prog->recur_per_cpu = true;
>  		}
>  	}
>  
> @@ -12916,6 +12917,10 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
>  	if (err)
>  		return err;
>  
> +	/* A per-CPU value stays the run's own only while no other run starts on the CPU */
> +	if (meta.map.ptr && bpf_map_is_percpu_map(meta.map.ptr->map_type))
> +		env->prog->recur_per_cpu = true;
> +
>  	err = record_func_map(env, &meta, func_id, insn_idx);
>  	if (err)
>  		return err;
> @@ -12942,6 +12947,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
>  
>  	switch (func_id) {
>  	case BPF_FUNC_tail_call:
> +		/* The target runs without a recursion check of its own */
> +		env->prog->recur_per_cpu = true;
>  		err = check_resource_leak(env, false, true, "tail_call");
>  		if (err)
>  			return err;
> @@ -13048,6 +13055,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
>  		struct bpf_reg_state *reg = &regs[BPF_REG_1];
>  		const struct btf_type *type;
>  
> +		env->prog->recur_per_cpu = true;
> +
>  		if (reg->type & MEM_RCU) {
>  			type = btf_type_by_id(reg->btf, reg->btf_id);
>  			if (!type || !btf_type_is_struct(type)) {
> @@ -19690,6 +19699,8 @@ static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
>  		__mark_reg_known(dst_reg, aux->map_off);
>  		dst_reg->type = PTR_TO_MAP_VALUE;
>  		dst_reg->map_ptr = map;
> +		if (bpf_map_is_percpu_map(map->map_type))
> +			env->prog->recur_per_cpu = true;
>  		WARN_ON_ONCE(map->map_type != BPF_MAP_TYPE_INSN_ARRAY &&
>  			     map->max_entries != 1);
>  		/* We want reg->id to be same (0) as map_value is not distinct */
> 


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx
  2026-11-17 11:29 ` [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
  2026-11-17 12:20   ` bot+bpf-ci
@ 2026-11-19  5:05   ` JP Kobryn (CrowdStrike)
  2026-11-19 11:35     ` [EXT.MSG] " Mike Bazov
  2026-11-23 12:49     ` Mike Bazov
  2026-11-24 22:04   ` Andrii Nakryiko
  2 siblings, 2 replies; 28+ messages in thread
From: JP Kobryn (CrowdStrike) @ 2026-11-19  5:05 UTC (permalink / raw)
  To: mikeba, bpf, 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, linux-trace-kernel, linux-kernel,
	Martin Kelly, Andrey Grodzovsky, tyler.wilson

On 11/17/26 3:29 AM, Mike Bazov via B4 Relay wrote:
> From: Mike Bazov <mikeba@sentinelone.com>
> 
> Trampoline programs (fentry, fexit, fsession, fmod_ret and their
> sleepable variants), raw tracepoint programs (raw_tp, raw_tp.s, tp_btf)
> and tracepoint programs on syscall tracepoints run preemptibly, but
> their recursion check is a per-CPU, per-program counter. When a task is
> preempted inside a program, the counter stays raised, and every other
> task that enters the program on that CPU is dropped and counted as a
> recursion miss, although nothing recursed. A program can't avoid that:
> the counter is checked before its first insn.
> 
> Since commit 7dadeaa6e851 ("sched: Further restrict the preemption
> modes") x86 and arm64 have only lazy and full preemption, so this
> happens in normal operation. A security agent that pairs fentry and
> fexit programs on inet_sendmsg() and inet_recvmsg() loses one half of a
> pair with every drop. With 200,000 HTTP requests and four CPU-bound
> tasks on a 4-CPU VM, 9,254 of 7,966,760 runs of these programs were
> dropped on bpf-next, and none of 7,975,426 with this series.
> 
> Check recursion per task instead. The runners already link a
> stack-allocated bpf_run_ctx into current->bpf_ctx. Move saved_run_ctx
> into struct bpf_run_ctx, so every frame links to the outer one, and
> record in each frame the program it runs. A program is recursing if a
> frame in the current task's chain already runs it. Interrupts and NMIs
> run on top of the interrupted task's chain, so recursion through them
> is still caught.

There are some questions outstanding from previous revs.
Was a limit on chain length considered?
Also, did you try the array-based approach mentioned and compare perf?

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 4/4] selftests/bpf: Add preemption recursion test for tracing programs
  2026-11-17 11:29 ` [PATCH bpf-next v3 4/4] selftests/bpf: Add preemption recursion test for tracing programs Mike Bazov via B4 Relay
@ 2026-11-19  5:09   ` JP Kobryn (CrowdStrike)
  2026-11-19 12:13     ` [EXT.MSG] " Mike Bazov
  0 siblings, 1 reply; 28+ messages in thread
From: JP Kobryn (CrowdStrike) @ 2026-11-19  5:09 UTC (permalink / raw)
  To: mikeba, bpf, 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, linux-trace-kernel, linux-kernel

On 11/17/26 3:29 AM, Mike Bazov via B4 Relay wrote:
> From: Mike Bazov <mikeba@sentinelone.com>
> 
> Pin two threads to one CPU. Thread B is SCHED_FIFO and waits for a
> signal that thread A sends from inside the program under test, so B
> preempts A there and enters the same program on the same CPU. Each
> subtest runs 20 rounds and checks in every round that A was inside the
> program when B ran. It is skipped if that never happens, as on a kernel
> that doesn't preempt kernel code.
> 
> - fentry, fentry_s, raw_tp, raw_tp_s, tp_syscall: a program without
>   per-CPU state. Both threads must run it in every round, without
>   recursion misses.
> - *_percpu, percpu_var, this_cpu_ptr, tail_call, may_goto: a program
>   with a per-CPU map, a per-CPU variable, bpf_this_cpu_ptr(), a tail
>   call or timed may_goto. B must be dropped and counted in every
>   round. A constant selects the per-CPU state that the handler
>   touches; with none selected, the same code doesn't mark the program.
> - two_dispatchers: two raw tracepoint programs tail-call one handler.
>   Both threads run it at once, and A checks that the stack buffer it
>   filled before B ran is intact.
> - recursion_raw_tp, recursion_two_hops, recursion_test_run: true
>   recursion is still dropped and counted: directly, through a second
>   program, and from BPF_PROG_TEST_RUN.
> - ext_attach: an extension that uses a per-CPU map can't be attached
>   to a raw tracepoint program that doesn't, while one without can.
> 
> Signed-off-by: Mike Bazov <mikeba@sentinelone.com>
> ---
>  .../selftests/bpf/prog_tests/trampoline_preempt.c  | 352 +++++++++++++++++++++
>  .../selftests/bpf/progs/trampoline_preempt.c       | 258 +++++++++++++++
>  2 files changed, 610 insertions(+)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
> new file mode 100644
> index 000000000000..f282fdba5d16
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
> @@ -0,0 +1,352 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#define _GNU_SOURCE
> +#include <pthread.h>
> +#include <sched.h>
> +#include <signal.h>
> +#include <sys/prctl.h>
> +#include <sys/syscall.h>
> +#include <unistd.h>
> +#include <test_progs.h>
> +#include "trampoline_preempt.skel.h"
> +
> +#define NR_ROUNDS 20
> +
> +/* The kernel's own "not supported" error, which the libc headers don't have */
> +#ifndef ENOTSUPP
> +#define ENOTSUPP 524
> +#endif
> +
> +/*
> + * Threads A and B are pinned to one CPU. B is SCHED_FIFO and waits for a signal:
> + *
> + *   A: prctl() -> enters the program under test
> + *                 the program sends B the signal
> + *   B:            wakes up and preempts A, which is still inside the program
> + *                 prctl() -> enters the same program on the same CPU
> + *
> + * A program whose recursion is checked per task runs for B too. A program that
> + * keeps per-CPU state keeps the per-CPU check: B's run is dropped and counted
> + * as a recursion miss.
> + */
> +
> +/* The per-CPU state that a program touches, see progs/trampoline_preempt.c */
> +enum { NO_PERCPU, PERCPU_MAP, PERCPU_VAR, THIS_CPU_PTR };
> +
> +enum expect {
> +	B_RUNS,		/* B runs the program, without recursion misses */
> +	B_DROPPED,	/* B's runs are dropped and counted as recursion misses */
> +	B_RUNS_HANDLER,	/* two_dispatchers: dropped by one dispatcher, runs through the other */
> +};
> +
> +/* What threads A and B share */
> +struct ctx {
> +	struct trampoline_preempt *skel;
> +	sigset_t sigset;
> +	int cpu, err_a, err_b;
> +	int overlaps;	/* rounds in which B found A still inside the program */
> +};
> +
> +/* Only timed may_goto keeps its budget per CPU, and only these JITs have it */
> +static bool has_timed_may_goto(void)
> +{
> +#if defined(__x86_64__) || defined(__aarch64__) || defined(__s390x__) || \
> +	defined(__powerpc64__) || (defined(__riscv) && __riscv_xlen == 64) || \
> +	(defined(__loongarch__) && __loongarch_grlen == 64)
> +	return is_jit_enabled();

Any better precedent for checking this?

> +#else
> +	return false;
> +#endif
> +}
> +
> +static int pin_to_cpu(int cpu)
> +{
> +	cpu_set_t set;
> +
> +	CPU_ZERO(&set);
> +	CPU_SET(cpu, &set);
> +	return sched_setaffinity(0, sizeof(set), &set) ? -errno : 0;
> +}
> +
> +/* A enters the program under test, which wakes B from inside it */
> +static void *worker_a(void *arg)
> +{
> +	struct ctx *c = arg;
> +	int i;
> +
> +	c->err_a = pin_to_cpu(c->cpu);
> +	c->skel->bss->tid_a = syscall(SYS_gettid);
> +	for (i = 0; i < NR_ROUNDS && !c->err_a; i++) {
> +		/* Passes every hook that the programs under test are attached to */
> +		if (prctl(PR_SET_NAME, "worker_a"))
> +			c->err_a = -errno;
> +	}
> +	return NULL;
> +}
> +
> +/* B is SCHED_FIFO on A's CPU: A runs only while B waits, and B preempts A as soon as A wakes it */
> +static void *worker_b(void *arg)
> +{
> +	struct sched_param sp = { .sched_priority = 1 };
> +	struct timespec ts = { .tv_sec = 5 };	/* give up if the signal never comes */
> +	struct ctx *c = arg;
> +	int i;
> +
> +	c->err_b = pin_to_cpu(c->cpu);
> +	if (!c->err_b && sched_setscheduler(0, SCHED_FIFO, &sp))
> +		c->err_b = -errno;
> +	c->skel->bss->tid_b = syscall(SYS_gettid);
> +	for (i = 0; i < NR_ROUNDS && !c->err_b; i++) {
> +		while (sigtimedwait(&c->sigset, NULL, &ts) < 0) {
> +			if (errno != EINTR) {
> +				c->err_b = -errno;
> +				return NULL;
> +			}
> +		}
> +		/* B preempted A only if A is still inside the program */
> +		if (*(volatile int *)&c->skel->bss->a_inside)
> +			c->overlaps++;
> +		/* B's own run of the program, while A is preempted inside it */
> +		if (prctl(PR_SET_NAME, "worker_b"))
> +			c->err_b = -errno;
> +	}
> +	return NULL;
> +}
> +
> +/* Load and attach only programs @a, @b and @c. NULL after a failure or a skip. */
> +static struct trampoline_preempt *load_progs(const char *a, const char *b, const char *c,
> +					      int percpu)
> +{
> +	struct trampoline_preempt *skel = trampoline_preempt__open();
> +	struct bpf_program *p;
> +	const char *name;
> +	int err;
> +
> +	if (!ASSERT_OK_PTR(skel, "open"))
> +		return NULL;
> +	bpf_object__for_each_program(p, skel->obj) {
> +		name = bpf_program__name(p);
> +		bpf_program__set_autoload(p, !strcmp(name, a) || (b && !strcmp(name, b)) ||
> +					     (c && !strcmp(name, c)));
> +	}
> +	skel->bss->sig = SIGUSR1;
> +	skel->rodata->percpu = percpu;
> +	err = trampoline_preempt__load(skel);
> +	/* The kernel or its JIT doesn't support one of the programs */
> +	if (err == -EOPNOTSUPP || err == -ENOTSUPP)
> +		test__skip();
> +	else if (ASSERT_OK(err, "load") && ASSERT_OK(trampoline_preempt__attach(skel), "attach"))
> +		return skel;
> +	trampoline_preempt__destroy(skel);
> +	return NULL;
> +}
> +
> +static const struct subtest {
> +	const char *name;
> +	const char *prog;	/* A enters it; its recursion misses are checked */
> +	int percpu;		/* the per-CPU state that the program touches */
> +	enum expect expect;
> +	const char *prog_b;	/* two_dispatchers: B reaches the handler through this one */
> +	const char *handler;	/* two_dispatchers: the target of both tail calls */
> +} subtests[] = {
> +	/* Each kind of program, without per-CPU state and with a per-CPU map lookup */
> +	{ "fentry",		"on_fentry",		NO_PERCPU,	B_RUNS },
> +	{ "fentry_percpu",	"on_fentry",		PERCPU_MAP,	B_DROPPED },
> +	{ "fentry_s",		"on_fentry_s",		NO_PERCPU,	B_RUNS },
> +	{ "fentry_s_percpu",	"on_fentry_s",		PERCPU_MAP,	B_DROPPED },

Is it worth covering fexit, fmod_ret?

> +	{ "raw_tp",		"on_raw_tp",		NO_PERCPU,	B_RUNS },
> +	{ "raw_tp_percpu",	"on_raw_tp",		PERCPU_MAP,	B_DROPPED },
> +	{ "raw_tp_s",		"on_raw_tp_s",		NO_PERCPU,	B_RUNS },
> +	{ "raw_tp_s_percpu",	"on_raw_tp_s",		PERCPU_MAP,	B_DROPPED },
> +	{ "tp_syscall",		"on_tp_syscall",	NO_PERCPU,	B_RUNS },
> +	{ "tp_syscall_percpu",	"on_tp_syscall",	PERCPU_MAP,	B_DROPPED },
> +	/* The other things that keep a program on the per-CPU check */
> +	{ "percpu_var",		"on_raw_tp",		PERCPU_VAR,	B_DROPPED },
> +	{ "this_cpu_ptr",	"on_raw_tp",		THIS_CPU_PTR,	B_DROPPED },
> +	{ "tail_call",		"on_tail_call",		NO_PERCPU,	B_DROPPED },
> +	{ "may_goto",		"on_may_goto",		NO_PERCPU,	B_DROPPED },
> +	/* Both threads are inside the handler at once, and A's stack must stay intact */
> +	{ "two_dispatchers",	"dispatcher_a",		NO_PERCPU,	B_RUNS_HANDLER,
> +	  "dispatcher_b", "handler" },
> +};

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [EXT.MSG] Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
       [not found]   ` <CAPnbwjpmRCR3F4V3StUz=702AdWZzHHZjP68GepnYdYykjoC8g@mail.gmail.com>
@ 2026-11-19 11:22     ` Mike Bazov
  2026-11-21  3:01       ` JP Kobryn (CrowdStrike)
  0 siblings, 1 reply; 28+ messages in thread
From: Mike Bazov @ 2026-11-19 11:22 UTC (permalink / raw)
  To: JP Kobryn (CrowdStrike)
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	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, linux-trace-kernel, linux-kernel,
	Martin Kelly, Andrey Grodzovsky, tyler.wilson

Thanks for testing, JP.

> fexit (marked)                61      135

Marked programs take the same per-CPU counter as before, so I expect no
change there. How many runs are behind these numbers, and how much do
they vary from run to run?

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [EXT.MSG] Re: [PATCH bpf-next v3 2/4] bpf: Mark programs whose runs must not overlap on a CPU
  2026-11-19  5:04   ` JP Kobryn (CrowdStrike)
@ 2026-11-19 11:23     ` Mike Bazov
  0 siblings, 0 replies; 28+ messages in thread
From: Mike Bazov @ 2026-11-19 11:23 UTC (permalink / raw)
  To: JP Kobryn (CrowdStrike)
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	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, linux-trace-kernel, linux-kernel,
	Martin Kelly, Andrey Grodzovsky, tyler.wilson

> Would explicitly opting in avoid unexpected regressions?

Yes: a new kind of per-CPU state needs a new mark, and an opt-in flag
avoids that. But existing programs keep losing runs until they are
rebuilt with it. That is the question at the end of the cover letter,
and I'll do what the maintainers prefer.

> would it make sense to make it visible via bpftool?

i think so. I'd add it once the marking is agreed.

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [EXT.MSG] Re: [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx
  2026-11-19  5:05   ` JP Kobryn (CrowdStrike)
@ 2026-11-19 11:35     ` Mike Bazov
  2026-11-23 12:49     ` Mike Bazov
  1 sibling, 0 replies; 28+ messages in thread
From: Mike Bazov @ 2026-11-19 11:35 UTC (permalink / raw)
  To: JP Kobryn (CrowdStrike)
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	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, linux-trace-kernel, linux-kernel,
	Martin Kelly, Andrey Grodzovsky, tyler.wilson

> Was a limit on chain length considered?

My proposal in the v1 thread had a cap of 8. If it turns out to be
needed, I can add one.

> did you try the array-based approach mentioned and compare perf?

Not measured. I asked in the v2 thread whether it is worth doing.
I can measure an array against it and post the numbers.

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [EXT.MSG] Re: [PATCH bpf-next v3 4/4] selftests/bpf: Add preemption recursion test for tracing programs
  2026-11-19  5:09   ` JP Kobryn (CrowdStrike)
@ 2026-11-19 12:13     ` Mike Bazov
  0 siblings, 0 replies; 28+ messages in thread
From: Mike Bazov @ 2026-11-19 12:13 UTC (permalink / raw)
  To: JP Kobryn (CrowdStrike)
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	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, linux-trace-kernel, linux-kernel

> > +   return is_jit_enabled();
>
> Any better precedent for checking this?

I found none. Unfortunately user space can't ask the kernel for it.

> > +   { "fentry_s_percpu",    "on_fentry_s",          PERCPU_MAP,     B_DROPPED },
>
> Is it worth covering fexit, fmod_ret?

They enter through the same __bpf_prog_enter_recur() as fentry, but it
is two rows each, so adding them is minor.

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [EXT.MSG] Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-19 11:22     ` [EXT.MSG] " Mike Bazov
@ 2026-11-21  3:01       ` JP Kobryn (CrowdStrike)
  0 siblings, 0 replies; 28+ messages in thread
From: JP Kobryn (CrowdStrike) @ 2026-11-21  3:01 UTC (permalink / raw)
  To: Mike Bazov
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	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, linux-trace-kernel, linux-kernel,
	Martin Kelly, Andrey Grodzovsky, tyler.wilson

On 11/19/26 3:22 AM, Mike Bazov wrote:
> Thanks for testing, JP.
> 
>> fexit (marked)                61      135
> 
> Marked programs take the same per-CPU counter as before, so I expect no
> change there. How many runs are behind these numbers, and how much do
> they vary from run to run?

That was 3 runs taken over a 30 min sapling window of a specific workload.
We changed from a timed window to the full duration of the workload and on
5 new runs, we got mixed results. In one run, we got the inverse where the
miss count was higher before the patch. But this is only in terms of marked
fexit. I think this is noise from system services. We're in the process of
building up a new harness for this issue so I expect by next rev we have
more consistency.

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [EXT.MSG] Re: [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx
  2026-11-19  5:05   ` JP Kobryn (CrowdStrike)
  2026-11-19 11:35     ` [EXT.MSG] " Mike Bazov
@ 2026-11-23 12:49     ` Mike Bazov
  1 sibling, 0 replies; 28+ messages in thread
From: Mike Bazov @ 2026-11-23 12:49 UTC (permalink / raw)
  To: JP Kobryn (CrowdStrike)
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	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, linux-trace-kernel, linux-kernel,
	Martin Kelly, Andrey Grodzovsky, tyler.wilson

Done. The array is not faster than walking the chain

bench -w2 -d12, M/s, median of 10 runs [min-max], lazy preemption,
x86-64 VM with 4 vCPUs, bpf-next 32b66b94c86f, in boot order.

The existing benchmarks:

               trig-fentry          trig-rawtp
  bpf-next     39.36 [33.73-39.72]  40.02 [37.00-40.78]
  series       40.10 [39.43-41.41]  39.63 [38.99-40.06]
  array        38.97 [38.19-39.17]  39.19 [38.58-39.90]
  bpf-next #2  39.74 [39.17-40.12]  41.12 [40.14-41.68]
  series #2    40.68 [39.68-41.58]  39.81 [39.33-40.49]
  array #2     39.89 [39.48-40.29]  39.49 [38.85-40.34]

I added two benchmarks to bench, with three programs nested: the
measured program starts inside the third one.

  driver (raw_tp)          calls bpf_ktime_get_boot_ns()
    fentry on that helper  calls bpf_ktime_get_tai_ns()
      fentry on that one   runs the loop that fires the measured program

               trig-fentry-nest3    trig-rawtp-nest3
  bpf-next     39.88 [39.42-40.52]  41.44 [40.93-42.68]
  series       41.25 [39.95-41.95]  39.62 [38.62-40.50]
  array        39.59 [38.15-41.53]  38.22 [36.37-39.02]
  bpf-next #2  40.62 [39.79-41.13]  42.84 [41.96-43.50]
  series #2    41.48 [40.09-41.94]  41.10 [39.70-41.63]
  array #2     41.06 [39.73-41.80]  39.67 [39.07-40.08]

The array is patches 1 and 2 plus this in place of patch 3, with two
fields in task_struct

const struct bpf_prog *bpf_recur_progs[4];
unsigned int bpf_recur_depth;

 static inline bool bpf_prog_get_recursion_context(struct bpf_prog *prog)
 {
+ struct task_struct *tsk = current;
+ unsigned int depth, i;
+ bool ok;
+
+ if (prog->recur_per_cpu)
+ return bpf_prog_get_recursion_percpu(prog); /* the old body */
+
+ depth = READ_ONCE(tsk->bpf_recur_depth);
+ ok = depth < ARRAY_SIZE(tsk->bpf_recur_progs);
+ for (i = 0; ok && i < depth; i++)
+ ok = READ_ONCE(tsk->bpf_recur_progs[i]) != prog;
+ WRITE_ONCE(tsk->bpf_recur_depth, depth + 1);
+ if (ok)
+ WRITE_ONCE(tsk->bpf_recur_progs[depth], prog);
+ return ok;
 }

 static inline void bpf_prog_put_recursion_context(struct bpf_prog *prog)
 {
+ if (prog->recur_per_cpu) {
+ bpf_prog_put_recursion_percpu(prog); /* the old body */
+ return;
+ }
+ WRITE_ONCE(current->bpf_recur_depth,
+   READ_ONCE(current->bpf_recur_depth) - 1);
 }

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-17 11:29 [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
                   ` (4 preceding siblings ...)
  2026-11-19  5:02 ` [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs JP Kobryn (CrowdStrike)
@ 2026-11-24  6:21 ` Alexei Starovoitov
  2026-11-24 16:10   ` Mike Bazov
  2026-11-24 21:47   ` Andrii Nakryiko
  2026-11-25  2:06 ` JP Kobryn (CrowdStrike)
  6 siblings, 2 replies; 28+ messages in thread
From: Alexei Starovoitov @ 2026-11-24  6:21 UTC (permalink / raw)
  To: mikeba, bpf, 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, linux-kernel

On Tue, Nov 17, 2026 at 03:29 AM Mike Bazov via B4 Relay <devnull+mikeba.sentinelone.com@kernel.org> wrote:
> - Marked programs are not fixed. A program with a per-CPU map or a
>   tail call loses runs to preemption as it does today.

I suspect users will hate the behavior introdcued by patch 2+3,
since adding a per-cpu map will suddenly make the prog to miss events.
But the idea has legs.

Earlier I proposed to insert bpf_preempt_disable() by the verifier.
No need for that. bpf_prog_exit_run_ctx() can do it when per-cpu access
is detected. Like below:

> One question: the marking is automatic. A flag that the program's
> author sets would avoid the second trade-off, but existing programs
> would keep losing runs until they are rebuilt. Which do you prefer?

No new uapi.

>   series #2    42.34 [42.12-42.78]  39.87 [39.46-41.38]  41.53 [40.79-41.75]

Good enough. let's keep the walk. No need for the array.

+static __always_inline bool bpf_prog_enter_run_ctx(struct bpf_prog *prog,
+						   struct bpf_run_ctx *run_ctx)
+{
+	struct bpf_run_ctx *ctx, *old_ctx = NULL;
+
+#ifdef CONFIG_BPF_SYSCALL
+	old_ctx = READ_ONCE(current->bpf_ctx);
 #endif
+	run_ctx->saved_run_ctx = old_ctx;
+	run_ctx->prog = prog;
+	if (prog->percpu_state) {
+		if (!prog->sleepable)
+			preempt_disable_notrace();
+		/* sleepable prog should be using explicit bpf_preempt_disable() */
+	}
+	for (ctx = old_ctx; ctx; ctx = ctx->saved_run_ctx) {
+		if (ctx->prog == prog)
+			return false;
+	}
+#ifdef CONFIG_BPF_SYSCALL
+	/* Initialize the frame before interrupts and NMIs on this CPU see it. */
+	barrier();
+	WRITE_ONCE(current->bpf_ctx, run_ctx);
+#endif
+	return true;
+}
+
+/* After an enter that returned false the frame is not linked, and the reset changes nothing */
+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)
+		return;
+	if (!prog->sleepable)
+		preempt_enable_notrace();
 }

I think this should minimize the breakage and fix the issue.

pw-bot: cr

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-24  6:21 ` Alexei Starovoitov
@ 2026-11-24 16:10   ` Mike Bazov
  2026-11-24 20:26     ` Alexei Starovoitov
  2026-11-24 21:47   ` Andrii Nakryiko
  1 sibling, 1 reply; 28+ messages in thread
From: Mike Bazov @ 2026-11-24 16:10 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, Daniel Borkmann, Andrii Nakryiko, 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, linux-kernel

On Tue, Nov 24, 2026 at 06:21 AM Alexei Starovoitov wrote:
> I think this should minimize the breakage and fix the issue.

Thanks, I'll do v4 that way. One question:

Timed may_goto sets the mark, so a may_goto loop would run with
preemption off for up to 250 ms, which you ruled out on v1 ("with
may_goto they can take 1/4 sec"). Move count and timestamp per
invocation, in bpf_run_ctx, so that may_goto needs no mark?

Thanks,
Mike

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-24 16:10   ` Mike Bazov
@ 2026-11-24 20:26     ` Alexei Starovoitov
  2026-11-25 12:03       ` Mike Bazov
  0 siblings, 1 reply; 28+ messages in thread
From: Alexei Starovoitov @ 2026-11-24 20:26 UTC (permalink / raw)
  To: Mike Bazov
  Cc: bpf, Daniel Borkmann, Andrii Nakryiko, 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, linux-kernel

On Tue, Nov 24, 2026 at 08:10 AM Mike Bazov <mikeba@sentinelone.com> wrote:
> Timed may_goto sets the mark, so a may_goto loop would run with
> preemption off for up to 250 ms, which you ruled out on v1 ("with
> may_goto they can take 1/4 sec"). Move count and timestamp per
> invocation, in bpf_run_ctx, so that may_goto needs no mark?

Well, since you brought it up, let's fix may_goto overwrite issue
as part of this series.

may_goto's count and timestamp cannot really move to bpf_run_ctx,
since xdp, tc, perf_event progs and async callbacks
don't have it. In bpf_timer_cb() current->bpf_ctx is NULL or belongs to
the interrupted prog. 

Keep count and timestamp per-cpu. Call bpf_timed_may_goto_save() in
bpf_prog_enter_run_ctx() and bpf_timed_may_goto_restore() in
bpf_prog_exit_run_ctx(), like bpf_timer_cb() does.
Then the preempted run gets its count and timestamp back.
For progs that enter this way skip this store in bpf_do_misc_fixups():

  insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_0, owner_off, 0);

With it cur->owner == saved is false and restore does nothing.


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-24  6:21 ` Alexei Starovoitov
  2026-11-24 16:10   ` Mike Bazov
@ 2026-11-24 21:47   ` Andrii Nakryiko
  2026-11-25  2:00     ` Alexei Starovoitov
  1 sibling, 1 reply; 28+ messages in thread
From: Andrii Nakryiko @ 2026-11-24 21:47 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: mikeba, bpf, Daniel Borkmann, Andrii Nakryiko, 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, linux-kernel

On Mon, Nov 23, 2026 at 10:21 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Tue, Nov 17, 2026 at 03:29 AM Mike Bazov via B4 Relay <devnull+mikeba.sentinelone.com@kernel.org> wrote:
> > - Marked programs are not fixed. A program with a per-CPU map or a
> >   tail call loses runs to preemption as it does today.
>
> I suspect users will hate the behavior introdcued by patch 2+3,
> since adding a per-cpu map will suddenly make the prog to miss events.
> But the idea has legs.
>
> Earlier I proposed to insert bpf_preempt_disable() by the verifier.
> No need for that. bpf_prog_exit_run_ctx() can do it when per-cpu access
> is detected. Like below:
>
> > One question: the marking is automatic. A flag that the program's
> > author sets would avoid the second trade-off, but existing programs
> > would keep losing runs until they are rebuilt. Which do you prefer?
>
> No new uapi.
>
> >   series #2    42.34 [42.12-42.78]  39.87 [39.46-41.38]  41.53 [40.79-41.75]
>
> Good enough. let's keep the walk. No need for the array.
>
> +static __always_inline bool bpf_prog_enter_run_ctx(struct bpf_prog *prog,
> +                                                  struct bpf_run_ctx *run_ctx)
> +{
> +       struct bpf_run_ctx *ctx, *old_ctx = NULL;
> +
> +#ifdef CONFIG_BPF_SYSCALL
> +       old_ctx = READ_ONCE(current->bpf_ctx);
>  #endif
> +       run_ctx->saved_run_ctx = old_ctx;
> +       run_ctx->prog = prog;
> +       if (prog->percpu_state) {
> +               if (!prog->sleepable)
> +                       preempt_disable_notrace();
> +               /* sleepable prog should be using explicit bpf_preempt_disable() */
> +       }
> +       for (ctx = old_ctx; ctx; ctx = ctx->saved_run_ctx) {

do you mind checking if unlikely() around ctx improves performance?

> +               if (ctx->prog == prog)
> +                       return false;
> +       }
> +#ifdef CONFIG_BPF_SYSCALL
> +       /* Initialize the frame before interrupts and NMIs on this CPU see it. */
> +       barrier();
> +       WRITE_ONCE(current->bpf_ctx, run_ctx);
> +#endif
> +       return true;
> +}
> +
> +/* After an enter that returned false the frame is not linked, and the reset changes nothing */
> +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)
> +               return;
> +       if (!prog->sleepable)
> +               preempt_enable_notrace();
>  }
>
> I think this should minimize the breakage and fix the issue.
>

So basically to opt out from this behavior BPF program would need to
stop using any per-CPU maps and just implement its own poor man's
per-CPU storage? And even using SEC(".percpu") would still be detected
as per-CPU usage, right?

Should we give a way to opt-out of this protection explicitly while
still using per-CPU maps?

> pw-bot: cr
>

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx
  2026-11-17 11:29 ` [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
  2026-11-17 12:20   ` bot+bpf-ci
  2026-11-19  5:05   ` JP Kobryn (CrowdStrike)
@ 2026-11-24 22:04   ` Andrii Nakryiko
  2026-11-25 14:12     ` Mike Bazov
  2 siblings, 1 reply; 28+ messages in thread
From: Andrii Nakryiko @ 2026-11-24 22:04 UTC (permalink / raw)
  To: mikeba
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	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, linux-kernel

On Tue, Nov 17, 2026 at 3:29 AM Mike Bazov via B4 Relay
<devnull+mikeba.sentinelone.com@kernel.org> wrote:
>
> From: Mike Bazov <mikeba@sentinelone.com>
>
> Trampoline programs (fentry, fexit, fsession, fmod_ret and their
> sleepable variants), raw tracepoint programs (raw_tp, raw_tp.s, tp_btf)
> and tracepoint programs on syscall tracepoints run preemptibly, but
> their recursion check is a per-CPU, per-program counter. When a task is
> preempted inside a program, the counter stays raised, and every other
> task that enters the program on that CPU is dropped and counted as a
> recursion miss, although nothing recursed. A program can't avoid that:
> the counter is checked before its first insn.
>
> Since commit 7dadeaa6e851 ("sched: Further restrict the preemption
> modes") x86 and arm64 have only lazy and full preemption, so this
> happens in normal operation. A security agent that pairs fentry and
> fexit programs on inet_sendmsg() and inet_recvmsg() loses one half of a
> pair with every drop. With 200,000 HTTP requests and four CPU-bound
> tasks on a 4-CPU VM, 9,254 of 7,966,760 runs of these programs were
> dropped on bpf-next, and none of 7,975,426 with this series.
>
> Check recursion per task instead. The runners already link a
> stack-allocated bpf_run_ctx into current->bpf_ctx. Move saved_run_ctx
> into struct bpf_run_ctx, so every frame links to the outer one, and
> record in each frame the program it runs. A program is recursing if a
> frame in the current task's chain already runs it. Interrupts and NMIs
> run on top of the interrupted task's chain, so recursion through them
> is still caught.
>
> bpf_prog_enter_run_ctx() and bpf_prog_exit_run_ctx() do the check for
> all runners. The syscall tracepoint runner linked one frame around its
> program array and now links one per program. bpf_set_run_ctx() accepts
> a NULL ctx, which io_uring passes to hide the BPF context; the frames
> below are not walked while it is hidden.
>
> Programs marked by the previous patch keep the per-CPU counter and
> behave exactly as before, so no counted drop is traded for corrupted
> per-CPU data or a renewed may_goto budget. An extension (freplace) runs
> under the check of its target, so a marked one can't be attached to a
> target that is checked per task.
>
> Trade-offs:
>
> - An unmarked program that keeps per-CPU slots of its own, see the
>   previous patch, can have two runs use one slot, and nothing counts
>   that.
> - Marked programs lose runs to preemption as before.
> - bench trig-rawtp is about 2% slower: 42.6 and 42.1 M/s on bpf-next,
>   41.3 and 41.5 M/s with the series. trig-fentry and trig-fexit are
>   not slower.
> - Attaching a marked extension to an unmarked raw_tp, tp_btf or
>   tracepoint program now fails with -EINVAL.
>
> Signed-off-by: Mike Bazov <mikeba@sentinelone.com>
> ---
>  include/linux/bpf.h          | 95 ++++++++++++++++++++++++++++++++++++++------
>  include/linux/bpf_verifier.h | 19 +++++++++
>  kernel/bpf/syscall.c         | 10 +++++
>  kernel/bpf/trampoline.c      | 26 +++++-------
>  kernel/trace/bpf_trace.c     | 11 ++---
>  net/bpf/test_run.c           |  9 +----
>  net/ipv4/bpf_tcp_ops.c       |  6 +--
>  7 files changed, 129 insertions(+), 47 deletions(-)
>
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 7873f0ef2817..97cbcd4b0d67 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -2783,7 +2783,16 @@ int bpf_prog_array_copy(struct bpf_prog_array *old_array,
>                         struct bpf_prog_array **new_array);
>  struct bpf_prog *bpf_prog_dummy(void);
>
> -struct bpf_run_ctx {};
> +/*
> + * A frame in the chain of the programs that a task is running, from
> + * current->bpf_ctx to the outermost one.
> + */
> +struct bpf_run_ctx {
> +       /* The next outer frame, NULL in the outermost one */
> +       struct bpf_run_ctx *saved_run_ctx;
> +       /* Set while a program whose recursion is checked per task runs in the frame */
> +       const struct bpf_prog *prog;
> +};
>
>  struct bpf_cg_run_ctx {
>         struct bpf_run_ctx run_ctx;
> @@ -2801,7 +2810,6 @@ struct bpf_tramp_run_ctx {
>         struct bpf_run_ctx run_ctx;
>         u64 bpf_cookie;
>         int retval;
> -       struct bpf_run_ctx *saved_run_ctx;
>  };
>
>  static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
> @@ -2809,8 +2817,17 @@ static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
>         struct bpf_run_ctx *old_ctx = NULL;
>
>  #ifdef CONFIG_BPF_SYSCALL
> -       old_ctx = current->bpf_ctx;
> -       current->bpf_ctx = new_ctx;
> +       old_ctx = READ_ONCE(current->bpf_ctx);
> +       if (new_ctx) {
> +               new_ctx->saved_run_ctx = old_ctx;
> +               new_ctx->prog = NULL;
> +       }

[...]

>  /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
> @@ -3492,7 +3565,6 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
>  {
>         const struct bpf_prog_array_item *item;
>         struct bpf_prog *prog;
> -       struct bpf_run_ctx *old_run_ctx;
>         struct bpf_trace_run_ctx run_ctx;
>         u32 ret = 1;
>
> @@ -3503,7 +3575,6 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
>
>         run_ctx.is_uprobe = false;
>
> -       old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
>         item = &array->items[0];
>         while ((prog = READ_ONCE(item->prog))) {
>                 /* Skip dummy_bpf_prog placeholder (len == 0) */
> @@ -3512,15 +3583,14 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
>                         continue;
>                 }
>
> -               if (unlikely(!bpf_prog_get_recursion_context(prog))) {
> +               run_ctx.bpf_cookie = item->bpf_cookie;
> +               if (unlikely(!bpf_prog_enter_run_ctx(prog, &run_ctx.run_ctx))) {

it feels like bpf_prog_enter_run_ctx() is doing too much and the logic
becomes convoluted... we had old_run_ctx above after setting current
run_ctx, why can't we pass that *old* run_ctx and our prog pointer
into bpf_prog_enter_run_ctx() to check that our prog is not already
running (and do that recursion context accounting). And before that
you already optimistically set run_ctx.bpf_cookie, you can just as
much set program pointer.

So in short, bpf_set_run_ctx() sets up chain of run_ctx, and then in
the inner loop we set up bpf_cookie + current prog (sort of
speculatively), and then bpf_prog_enter_run_cxt() checks if we can run
prog or not.

Am I missing some complications that would prevent that separation of concerns?

>                         bpf_prog_inc_misses_counter(prog);
> -                       bpf_prog_put_recursion_context(prog);
> +                       bpf_prog_exit_run_ctx(prog, &run_ctx.run_ctx);
>                         item++;
>                         continue;
>                 }
>
> -               run_ctx.bpf_cookie = item->bpf_cookie;
> -
>                 if (!prog->sleepable) {
>                         guard(rcu)();
>                         ret &= run_prog(prog, ctx);
> @@ -3528,10 +3598,9 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
>                         ret &= run_prog(prog, ctx);
>                 }
>
> -               bpf_prog_put_recursion_context(prog);
> +               bpf_prog_exit_run_ctx(prog, &run_ctx.run_ctx);
>                 item++;
>         }
> -       bpf_reset_run_ctx(old_run_ctx);

same here, what's wrong with doing reset just once, all we really need
is to update bpf_cookie and prog pointer inside that while() loop over
bpf_prog_array items, right? the chaining itself doesn't really change
and shouldn't be done for each item separately.

>         migrate_enable();
>         return ret;

[...]

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-24 21:47   ` Andrii Nakryiko
@ 2026-11-25  2:00     ` Alexei Starovoitov
  2026-11-25  5:35       ` Andrii Nakryiko
  0 siblings, 1 reply; 28+ messages in thread
From: Alexei Starovoitov @ 2026-11-25  2:00 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: mikeba, bpf, Daniel Borkmann, Andrii Nakryiko, 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, linux-kernel

On Tue, Nov 24, 2026 at 01:47 PM Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
>> +       for (ctx = old_ctx; ctx; ctx = ctx->saved_run_ctx) {
>
> do you mind checking if unlikely() around ctx improves performance?

Mike,
pls try it on respin.

> So basically to opt out from this behavior BPF program would need to
> stop using any per-CPU maps and just implement its own poor man's
> per-CPU storage?

poor mans's per-cpu with bpf_get_smp_processor_id() shouldn't work
as an opt-out. See section 6.

> And even using SEC(".percpu") would still be detected
> as per-CPU usage, right?

This one is pretty much brand new, so there is no data to make
a decision. I can see arguments both ways.

> Should we give a way to opt-out of this protection explicitly while
> still using per-CPU maps?

After looking at the data, I think we shouldn't allow it.

An opt-out is for a prog that protects its per-cpu data on its own.
In the v1 thread your example was stack_trace_scratch in wprof.
It doesn't work. wprof is broken today, without this series.
An event gets args or a stack of another task. wprof 8dd41e18 on
bpf-next, corrupt events / all events:

  -U                             what's wrong  lazy      full
  -----------------------------  ------------  --------  ---------
  u:func (arg:0, arg:1:str)      args          1928/16M  9111/17M
  u:func (..., stack)            stack         683/15M   3419/15M
  raw_tp:sys_enter (arg:id) and  args          3491/48M  16673/44M
  tp:syscalls:sys_enter_openat

utrace_scratch has no protection.
stack_trace_cur handles nesting, but not preemption:

  task A                task B                task C
  idx 0, st_cnt = 1
  preempted
                        idx 1, st_cnt = 2
                        preempted
  put, st_cnt = 1
                                              idx 1    <- slot of B

With -S offcpu task C is wprof_task_switch. Then the utrace event has
a record of kind ST_OFFCPU.
uprobes have no protection. That's what an opt-out gives to raw_tp, tp
and fentry progs:

  -U, lazy                 today     preempt off  opt-out
  -----------------------  --------  -----------  --------
  raw_tp only, args        0         0            2849/45M
  raw_tp only, stack       0         0            4081/27M
  raw_tp and tp, args      3491/48M  0            3166/54M

The series doesn't touch uprobes, so wprof needs its own fix for them.
The default mode of wprof, without -U, is fine.

Now the bigger question: do we need preempt_disable() at all?
We do. Without it progs of 17 out of 69 projects in [1] corrupt their
data. 12 of them have progs that are clean today.

Three kernels, bpf-next a58375b858fa plus patches 1-3:

  today        prog->active
  preempt off  walk + preempt_disable() for marked non-sleepable progs
  walk         walk only

Objects from [1] with small test loaders, 4 vCPUs, PREEMPT_RCU, 6-20 sec,
SCHED_OTHER tasks only. The load saturates the cpus. These are not
production rates.
The numbers are corrupt events / all events.

1. Clean today, corrupt with walk

The per-cpu buffer has one entry prog, so prog->active protects it.
"today" and "preempt off" have 0 corrupt events in each row.

  prog                            default  walk, lazy  walk, full
  ------------------------------  -------  ----------  ----------
  tetragon 1.8 event_execve       yes      25/172K     243/174K
  tetragon 1.8 tp on syscalls/*   policy   88/6.0M     478/6.8M
  tetragon 1.7 generic raw_tp     policy   40/290K     225/271K
  runtime-enforcer                yes      8/1.3M      252/1.0M
  pulsar sched_process_exec       yes      24/1.8M     188/1.5M
  kubescape fork                  yes      79/2.5M     475/2.7M
  coolbpf proctrace               yes      3/1.3M      398/1.4M
  inspektor-gadget finit_module   yes      5/38K       3/38K
  ptcpdump mount                  option   36/9.3M     101/8.9M
  wprof -U raw_tp only, args      option   2849/45M    16140/44M
  wprof -U raw_tp only, stack     option   4081/27M    20194/26M

  prog                       what breaks
  -------------------------  ------------------------------------------
  tetragon event_execve      pid, uid, argv of another task.
                             execve_map keeps the wrong pid.
  tetragon tp on syscalls/*  the signal goes to a task that doesn't
                             match the policy. Sigkill is the same code.
  runtime-enforcer           fmod_ret returns -EPERM for an allowed exec
  the rest                   path, argv, args or stack of another task

bpftool prog profile breaks too. Cycles per run of a tp_btf prog, lazy:
1540 today, 1236 with preempt off, 387 with walk. With full preemption
the sum is negative.
Same pattern in the source, not run: perf stat -b, bpfsnoop -g and
--output-lbr, exec and setsockopt in the fentry mode of datadog CWS.

2. Corrupt today

Several progs share the buffer. prog->active is per prog, so it doesn't
help. Lazy preemption:

  prog                        today      walk       preempt off
  --------------------------  ---------  ---------  -----------
  tracee syscalls             2153/9.3M  3619/22M   0
  tracee exec                 180/1.1M   181/1.5M   0
  datadog CWS, fentry mode    2268/7.0M  4065/12M   0
  elastic ebpf, no cgrp path  315/13M    290/14M    0
  skywalking-rover            196/21M    345/30M    0
  inspektor-gadget traceloop  28/7.5M    238/12M    0
  tetragon 1.7 fentry         2199/19M   3100/18M   0
  falco libs 0.25.4           2700/56M   4019/82M   879/72M

So you're right, for these the guarantee was never there.
For most of them walk is as bad per event as today.
preempt off fixes a bug that they have now.
Same pattern: open, exec, rename, http of inspektor-gadget and kubescape.
In the source, not run: systing, dynolog, coolbpf ifc engine.

3. What prog->active costs

Same loads, lazy preemption:

  lost                                today      walk     preempt off
  ----------------------------------  ---------  -------  -----------
  tetragon, match without a signal    616K/4.9M  57/4.2M  0
  runtime-enforcer, denied exec runs  175/336K   2/440K   0
  falco libs 0.26.1, lost events      16.5M      2.7M     0.5M

walk is better than today here. preempt off is better than both.
The 0.5M of falco come from progs that are not marked, see 6.

4. How precise is the mark

About 2000 progs run in the affected runners. 930 are marked.
From the source: 420 corrupt their data without preempt_disable(),
230 of them are datadog in fentry mode. 350 are marked for nothing:
the hook is atomic, the map is read only, or the prog has its own guard.
The rest lose a counter update at worst, or have a bug of their own.

5. Sleepable

No prog has fentry.s, fexit.s, raw_tp.s or tp.s. Two loaders set
BPF_F_SLEEPABLE: tracexec and datadog shared-libraries. Their progs have
no per-cpu state. So nothing in [1] needs protection for sleepable progs
in these runners.
tracexec has a pool of 2 * ncpu entries. Without prog->active it
overflows and cuts argv in 0.14% (lazy) and 5% (full) of execs.
Today it loses 42% and 66% of execs in the same load.

6. What the mark misses

array[bpf_get_smp_processor_id()] without a tail call.
That's the 879 in the falco row, and the batch of closed connections in
datadog tracer-fentry up to 7.78. Both releases don't load on bpf-next
for other reasons.
bpftrace is not in [1]. createScratchBuffer() indexes a global array by
cpu id for each object above on_stack_limit, 32 bytes by default.
str() and large printf() go there:

  tracepoint:syscalls:sys_enter_openat { printf("%s", str(args.filename)) }

From the source this is clean today and breaks with or without
preempt off. Not run. Procmon and Sysmon do the same on raw_tp/sys_enter.
A mark for bpf_get_smp_processor_id() fixes them. It marks up to 350 more
progs in [1], and most of them are safe.

7. The case for no protection

lsm lost prog->active in 6.2 (271de525e1d7). uprobe is preemptible since
6.0 (8c7dcb84e3b7). About 330 progs of 21 projects in such runners use
a per-cpu scratch without a guard today.
But projects started to notice after 7.0. obi, falco, fact and tetragon
added guards in Aug-Oct 2026. falco saw "one crash every 10 minutes per
agent" on 7.x [2].

What a prog can do on its own is limited. bpf_preempt_disable() cannot
span a tail call. A depth counter needs LIFO order. A heap keyed by task,
a slot with an owner and ringbuf reserve work.

The progs that do it right, falco 0.26 and tetragon 1.8 fentry,
do tail calls. An opt-out for per-cpu maps doesn't remove their mark.
A false mark costs scheduling latency only.

Not covered: PREEMPT_RT, may_goto.

[1] https://github.com/4ast/bpf-corpus
[2] https://github.com/falcosecurity/falco/issues/3955

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-17 11:29 [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
                   ` (5 preceding siblings ...)
  2026-11-24  6:21 ` Alexei Starovoitov
@ 2026-11-25  2:06 ` JP Kobryn (CrowdStrike)
  2026-11-25  4:02   ` Alexei Starovoitov
  6 siblings, 1 reply; 28+ messages in thread
From: JP Kobryn (CrowdStrike) @ 2026-11-25  2:06 UTC (permalink / raw)
  To: mikeba, 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, linux-trace-kernel, linux-kernel, bpf,
	Martin Kelly, Andrey Grodzovsky, tyler.wilson

On 11/17/26 3:29 AM, Mike Bazov via B4 Relay wrote:
> fentry, fexit, raw_tp and syscall tracepoint programs can be preempted
> while they run, but their recursion check is a per-CPU counter:
> 
>   task A enters prog on CPU 0       prog->active = 1
>   task A is preempted inside prog
>   task B enters prog on CPU 0       dropped, counted as recursion miss
> 
> Nothing recursed, but B's event is lost. Since 7.0, x86 and arm64 have
> only lazy and full preemption, so this happens on stock kernels. Our
> fentry/fexit probe on inet_sendmsg() and inet_recvmsg() loses 9,254 of
> 8 million runs under HTTP load on a 4-CPU VM. With the series it loses
> none.
> 
> The series checks recursion per task instead: a program is recursing
> only if the current task is already inside it.
> 
> v2 did that for every program, which breaks a program that uses a
> per-CPU map as scratch space: the second task overwrites its buffer,
> and nothing counts that. So in v3 the verifier marks the programs that
> keep per-CPU state, and those keep the per-CPU counter and behave
> exactly as today:
> 
>   - per-CPU maps, bpf_this_cpu_ptr(), bpf_per_cpu_ptr()
>   - tail calls: the target has no recursion check of its own
>   - timed may_goto: its budget is per CPU
>   - a private stack
> 
> Trade-offs:
> 
> - Marked programs are not fixed. A program with a per-CPU map or a
>   tail call loses runs to preemption as it does today.
> - The verifier can't see everything. A program that keeps per-CPU
>   slots of its own, like array[bpf_get_smp_processor_id()], is not
>   marked. Today the counter keeps its runs apart. With the series two
>   runs can write one slot, and nothing counts that.
> - raw_tp is about 2% slower in bench, see the table below. fentry and
>   fexit are within noise.
> - Tracing, raw_tp and tracepoint programs lose the private stack
>   (patch 1). It is already unsafe for them: two raw_tp programs that
>   tail-call one handler let two tasks share its stack today. A program
>   with two 2000-byte frames no longer loads.
I see how removing private stacks would avoid the corner case issues 
mentioned. In the common case though, programs with a combined stack 
size above the limit will start being rejected after this change (as
you noted). But for programs that still load, they'll lose the safety
of the  overflow/underflow checks private stacks include.

I explored an idea for making private stacks concurrency-safe regardless
of entry path or recursion model. The concept is changing from per-prog
per-cpu private stacks to a per-prog per-cpu pool of private stacks. The
pool consists of a fixed number of slots. A free slot is acquired at
invocation time and released on exit. This allows concurrent tasks on
the same CPU to each get their own private stack. So it would fix the
existing corruption and eliminate the need for marking private stacks.
Private stacks could then be used with the new per-task per-prog
recursion guard. The pool size is limited so exhaustions are counted.

The patch below is a proof of concept scoped only to x86_64 trampolines.
It was validated with the bpftrace prog that precedes the patch, along
with jited assembly dump to inspect the new private frame API calls.
Overhead measured was roughly 60ns, but this was just a first take and I
believe it can be optimized especially in the JIT area. If it makes
sense, this could be integrated into the series or sent as a follow-up.

kfunc:vmlinux:__x64_sys_getpgid
/comm == "bpf-test-main" || comm == "bpf-test-waker"/
{
    @entered = count();
    @entered_tid[tid] = count();

    if (comm == "bpf-test-main") {
        @main_active[cpu] = 1;
    } else if (@main_active[cpu])
        @overlap_entries = count();
    }

    $i = 0;
    $last = nsecs;
    // bounded work to increase preemption window
    while ($i < 10000) {
        $last = nsecs;
        $i++;
    }

    if (comm == "bpf-test-main") {
        @main_active[cpu] = 0;
    }

    @last_ts[tid] = $last;
    @completed_tid[tid] = count();
}

-- 8< --
From: "JP Kobryn (CrowdStrike)" <jp.kobryn@linux.dev>
Date: Fri, 20 Nov 2026 21:23:55 -0800
Subject: [PATCH bpf-next] bpf, x86: concurrency-safe private stacks

Private stacks currently exist on a per-prog per-cpu basis. Since the
proposed recursion guard allows different progs to run concurrently
(in the scheduler sense) on the same CPU, corruption would occur on
the private stacks.

This patch is an idea for concurrency-safe private stacks. Achieve
this safety by changing to a new paradigm. Instead of a per-prog
per-cpu basis, use a new per-prog per-cpu pool of stacks. Programs
acquire an available "slot" on invocation. The slot serves as an index
into the pool of private stacks. This gives the program the ability
to use separate private stacks per-invocation up to a fixed limit.
When available slots are exhausted, a counter is incremented and the
program is skipped. Note this is not the same counter as the existing
recursion_misses.

Signed-off-by: JP Kobryn (CrowdStrike) <jp.kobryn@linux.dev>
---
 arch/x86/net/bpf_jit_comp.c  | 136 +++++++------------
 include/linux/bpf.h          |  37 +++++-
 include/linux/bpf_verifier.h |   4 +
 kernel/bpf/Makefile          |   2 +-
 kernel/bpf/core.c            |   1 +
 kernel/bpf/fixups.c          |   1 +
 kernel/bpf/priv_stack.c      | 247 +++++++++++++++++++++++++++++++++++
 kernel/bpf/trampoline.c      |   4 +
 kernel/bpf/verifier.c        |  15 +++
 9 files changed, 352 insertions(+), 95 deletions(-)
 create mode 100644 kernel/bpf/priv_stack.c

diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 458ddcf87c48..f63150a69e9f 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -2039,22 +2039,39 @@ static void emit_shiftx(u8 **pprog, u32 dst_reg, u8 src_reg, bool is64, u8 op)
 	*pprog = prog;
 }
 
-static void emit_priv_frame_ptr(u8 **pprog, void __percpu *priv_frame_ptr)
+static int emit_priv_frame_ptr(u8 **pprog, struct bpf_prog *bpf_prog, u8 *ip)
 {
-	u8 *prog = *pprog;
-
-	/* movabs r9, priv_frame_ptr */
-	emit_mov_imm64(&prog, X86_REG_R9, (__force long) priv_frame_ptr >> 32,
-		       (u32) (__force long) priv_frame_ptr);
+#ifdef CONFIG_BPF_SYSCALL
+	u8 *prog = *pprog, *start = prog;
+	u64 prog_addr = (u64)bpf_prog;
 
-#ifdef CONFIG_SMP
-	/* add <r9>, gs:[<off>] */
-	EMIT2(0x65, 0x4c);
-	EMIT3(0x03, 0x0c, 0x25);
-	EMIT((u32)(unsigned long)&this_cpu_off, 4);
-#endif
+	/* Save the return value and arguments 1-5 */
+	EMIT1(0x50);		/* push rax */
+	EMIT1(0x57);		/* push rdi */
+	EMIT1(0x56);		/* push rsi */
+	EMIT1(0x52);		/* push rdx */
+	EMIT1(0x51);		/* push rcx */
+	EMIT2(0x41, 0x50);	/* push r8 */
 
+	/*
+	 * Pass this function's prog pointer to bpf_priv_stack_frame() in
+	 * order to obtain the private frame pointer. Store it in R9.
+	 */
+	emit_mov_imm64(&prog, BPF_REG_1, prog_addr >> 32, prog_addr);
+	if (emit_rsb_call(&prog, bpf_priv_stack_frame, ip + (prog - start)))
+		return -EINVAL;
+	emit_mov_reg(&prog, true, X86_REG_R9, BPF_REG_0);
+
+	/* Restore the registers */
+	EMIT2(0x41, 0x58);	/* pop r8 */
+	EMIT1(0x59);		/* pop rcx */
+	EMIT1(0x5a);		/* pop rdx */
+	EMIT1(0x5e);		/* pop rsi */
+	EMIT1(0x5f);		/* pop rdi */
+	EMIT1(0x58);		/* pop rax */
 	*pprog = prog;
+#endif
+	return 0;
 }
 
 #define INSN_SZ_DIFF (((addrs[i] - addrs[i - 1]) - (prog - temp)))
@@ -2076,10 +2093,6 @@ static int jmp_distance(const int *addrs, int i, int off, int moved)
 #define LOAD_TAIL_CALL_CNT_PTR(stack)				\
 	__LOAD_TCC_PTR(BPF_TAIL_CALL_CNT_PTR_STACK_OFF(stack))
 
-/* Memory size/value to protect private stack overflow/underflow */
-#define PRIV_STACK_GUARD_SZ    8
-#define PRIV_STACK_GUARD_VAL   0xEB9F12345678eb9fULL
-
 static int emit_spectre_bhb_barrier(u8 **pprog, u8 *ip,
 				    struct bpf_prog *bpf_prog)
 {
@@ -2391,10 +2404,9 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 	int insn_cnt = bpf_prog->len;
 	bool seen_exit = false;
 	u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
-	void __percpu *priv_frame_ptr = NULL;
+	bool use_priv_stack = bpf_prog->aux->priv_stack_offset;
 	u16 out_stack_arg_cnt, outgoing_rsp;
 	u64 arena_vm_start = 0, user_vm_start = 0;
-	void __percpu *priv_stack_ptr;
 	int i, excnt = 0;
 	int ilen, moved, proglen = 0;
 	u8 *ip, *prog = temp;
@@ -2410,11 +2422,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 
 	stack_depth = bpf_prog->aux->stack_depth;
 	out_stack_arg_cnt = bpf_out_stack_arg_cnt(env, bpf_prog);
-	priv_stack_ptr = bpf_prog->aux->priv_stack_ptr;
-	if (priv_stack_ptr) {
-		priv_frame_ptr = priv_stack_ptr + PRIV_STACK_GUARD_SZ + round_up(stack_depth, 8);
+	if (use_priv_stack)
 		stack_depth = 0;
-	}
 
 	/*
 	 * Follow x86-64 calling convention for both BPF-to-BPF and
@@ -2525,15 +2534,18 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 	 */
 	if (bpf_prog->aux->exception_boundary)
 		bpf_prog->aux->stack_arg_sp_adjust = outgoing_rsp +
-						     (priv_frame_ptr || arena_sp_save ? 8 : 0);
+						     (use_priv_stack || arena_sp_save ? 8 : 0);
 	emit_sub_rsp(&prog, outgoing_rsp);
 
 	if (arena_vm_start)
 		emit_mov_imm64(&prog, X86_REG_R12,
 			       arena_vm_start >> 32, (u32) arena_vm_start);
 
-	if (priv_frame_ptr)
-		emit_priv_frame_ptr(&prog, priv_frame_ptr);
+	if (use_priv_stack) {
+		err = emit_priv_frame_ptr(&prog, bpf_prog, image + (prog - temp));
+		if (err)
+			return err;
+	}
 
 	ilen = prog - temp;
 	if (rw_image)
@@ -2559,7 +2571,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 		u32 jmp_op = BPF_OP(insn->code);
 		s32 cmp_imm;
 
-		if (priv_frame_ptr) {
+		if (use_priv_stack) {
 			if (src_reg == BPF_REG_FP)
 				src_reg = X86_REG_R9;
 
@@ -3384,14 +3396,14 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 					ip += prog - start;
 				}
 			}
-			if (priv_frame_ptr) {
+			if (use_priv_stack) {
 				push_r9(&prog);
 				ip += 2;
 			}
 			ip += x86_call_depth_emit_accounting(&prog, func, ip);
 			if (emit_call(&prog, func, ip))
 				return -EINVAL;
-			if (priv_frame_ptr)
+			if (use_priv_stack)
 				pop_r9(&prog);
 			if (arena_sp_args)
 				emit_sub_rsp(&prog, 8);
@@ -3416,19 +3428,19 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			 * of a subprog there is no need to pass
 			 * tail_call_cnt_ptr in rax.
 			 */
-			if (priv_frame_ptr) {
+			if (use_priv_stack) {
 				push_r9(&prog);
 				ip += 2;
 			}
 			if (emit_indirect_call(&prog, insn->dst_reg, ip))
 				return -EINVAL;
-			if (priv_frame_ptr)
+			if (use_priv_stack)
 				pop_r9(&prog);
 			break;
 
 			/* unwind_goto. The verifier expanded may_goto. */
 		case BPF_JMP | BPF_JCOND:
-			if (!priv_frame_ptr) {
+			if (!use_priv_stack) {
 				err = add_unwind_exentry(bpf_prog, image, rw_image, &excnt,
 							 addrs[i - 1], addrs[i + insn->off]);
 				if (err)
@@ -4737,35 +4749,6 @@ int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_func
 	return emit_bpf_dispatcher(&prog, 0, num_funcs - 1, funcs, image, buf);
 }
 
-static void priv_stack_init_guard(void __percpu *priv_stack_ptr, int alloc_size)
-{
-	int cpu, underflow_idx = (alloc_size - PRIV_STACK_GUARD_SZ) >> 3;
-	u64 *stack_ptr;
-
-	for_each_possible_cpu(cpu) {
-		stack_ptr = per_cpu_ptr(priv_stack_ptr, cpu);
-		stack_ptr[0] = PRIV_STACK_GUARD_VAL;
-		stack_ptr[underflow_idx] = PRIV_STACK_GUARD_VAL;
-	}
-}
-
-static void priv_stack_check_guard(void __percpu *priv_stack_ptr, int alloc_size,
-				   struct bpf_prog *prog)
-{
-	int cpu, underflow_idx = (alloc_size - PRIV_STACK_GUARD_SZ) >> 3;
-	u64 *stack_ptr;
-
-	for_each_possible_cpu(cpu) {
-		stack_ptr = per_cpu_ptr(priv_stack_ptr, cpu);
-		if (stack_ptr[0] != PRIV_STACK_GUARD_VAL ||
-		    stack_ptr[underflow_idx] != PRIV_STACK_GUARD_VAL) {
-			pr_err("BPF private stack overflow/underflow detected for prog %sx\n",
-			       bpf_jit_get_prog_name(prog));
-			break;
-		}
-	}
-}
-
 struct x64_jit_data {
 	struct bpf_binary_header *rw_header;
 	struct bpf_binary_header *header;
@@ -4782,9 +4765,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
 {
 	struct bpf_binary_header *rw_header = NULL;
 	struct bpf_binary_header *header = NULL;
-	void __percpu *priv_stack_ptr = NULL;
 	struct x64_jit_data *jit_data;
-	int priv_stack_alloc_sz;
 	int proglen, oldproglen = 0;
 	struct jit_context ctx = {};
 	bool extra_pass = false;
@@ -4805,21 +4786,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
 			return prog;
 		prog->aux->jit_data = jit_data;
 	}
-	priv_stack_ptr = prog->aux->priv_stack_ptr;
-	if (!priv_stack_ptr && prog->aux->jits_use_priv_stack) {
-		/* Allocate actual private stack size with verifier-calculated
-		 * stack size plus two memory guards to protect overflow and
-		 * underflow.
-		 */
-		priv_stack_alloc_sz = round_up(prog->aux->stack_depth, 8) +
-				      2 * PRIV_STACK_GUARD_SZ;
-		priv_stack_ptr = __alloc_percpu_gfp(priv_stack_alloc_sz, 8, GFP_KERNEL);
-		if (!priv_stack_ptr)
-			goto out_priv_stack;
-
-		priv_stack_init_guard(priv_stack_ptr, priv_stack_alloc_sz);
-		prog->aux->priv_stack_ptr = priv_stack_ptr;
-	}
 	addrs = jit_data->addrs;
 	if (addrs) {
 		ctx = jit_data->ctx;
@@ -4979,11 +4945,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
 			bpf_prog_fill_jited_linfo(prog, addrs + 1);
 out_addrs:
 		kvfree(addrs);
-		if (!image && priv_stack_ptr) {
-			free_percpu(priv_stack_ptr);
-			prog->aux->priv_stack_ptr = NULL;
-		}
-out_priv_stack:
 		kfree(jit_data);
 		prog->aux->jit_data = NULL;
 	}
@@ -5071,8 +5032,6 @@ void bpf_jit_free(struct bpf_prog *prog)
 	if (prog->jited) {
 		struct x64_jit_data *jit_data = prog->aux->jit_data;
 		struct bpf_binary_header *hdr;
-		void __percpu *priv_stack_ptr;
-		int priv_stack_alloc_sz;
 
 		/*
 		 * If we fail the final pass of JIT (from jit_subprogs),
@@ -5088,13 +5047,6 @@ void bpf_jit_free(struct bpf_prog *prog)
 		prog->bpf_func = (void *)prog->bpf_func - cfi_get_offset();
 		hdr = bpf_jit_binary_pack_hdr(prog);
 		bpf_jit_binary_pack_free(hdr, NULL);
-		priv_stack_ptr = prog->aux->priv_stack_ptr;
-		if (priv_stack_ptr) {
-			priv_stack_alloc_sz = round_up(prog->aux->stack_depth, 8) +
-					      2 * PRIV_STACK_GUARD_SZ;
-			priv_stack_check_guard(priv_stack_ptr, priv_stack_alloc_sz, prog);
-			free_percpu(prog->aux->priv_stack_ptr);
-		}
 		WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(prog));
 	}
 
@@ -5112,7 +5064,7 @@ bool bpf_jit_supports_exceptions(void)
 
 bool bpf_jit_supports_private_stack(void)
 {
-	return false;
+	return IS_ENABLED(CONFIG_BPF_SYSCALL);
 }
 
 bool bpf_jit_supports_arena_stack(void)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index b7fe3629e073..04d99d96e46a 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -44,6 +44,7 @@ struct bpf_verifier_log;
 struct perf_event;
 struct bpf_prog;
 struct bpf_prog_aux;
+struct bpf_priv_stack_pool;
 struct bpf_map;
 struct bpf_arena;
 struct sock;
@@ -1914,6 +1915,9 @@ struct bpf_prog_aux {
 	struct btf *attach_btf;
 	struct bpf_ctx_arg_aux *ctx_arg_info;
 	void __percpu *priv_stack_ptr;
+	/* main_prog_aux owns the pool. subprogs must use main_prog_aux->priv_stack_pool. */
+	struct bpf_priv_stack_pool *priv_stack_pool;
+	u32 priv_stack_offset;
 	struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */
 	struct bpf_prog *dst_prog;
 	struct bpf_trampoline *dst_trampoline;
@@ -2778,11 +2782,37 @@ int bpf_prog_array_copy(struct bpf_prog_array *old_array,
 			struct bpf_prog_array **new_array);
 struct bpf_prog *bpf_prog_dummy(void);
 
+struct bpf_priv_stack_reservation {
+	void *slot_base;
+	struct bpf_priv_stack_pool *pool;
+	unsigned long *busy;
+	unsigned int slot;
+};
+
 struct bpf_run_ctx {
 	struct bpf_run_ctx *outer_run_ctx; /* non-null when nested */
 	struct bpf_prog *prog;
+	struct bpf_priv_stack_reservation priv_stack;
 };
 
+#ifdef CONFIG_BPF_SYSCALL
+void bpf_priv_stack_free(struct bpf_prog_aux *aux);
+void *bpf_priv_stack_frame(struct bpf_prog *prog);
+bool bpf_priv_stack_acquire(struct bpf_prog *prog,
+			struct bpf_priv_stack_reservation *res);
+void bpf_priv_stack_release(struct bpf_priv_stack_reservation *res);
+#else
+static inline bool bpf_priv_stack_acquire(struct bpf_prog *prog,
+			struct bpf_priv_stack_reservation *res)
+{
+	return true;
+}
+
+static inline void bpf_priv_stack_release(struct bpf_priv_stack_reservation *res)
+{
+}
+#endif
+
 struct bpf_cg_run_ctx {
 	struct bpf_run_ctx run_ctx;
 	const struct bpf_prog_array_item *prog_item;
@@ -2808,8 +2838,11 @@ static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx, s
 
 #ifdef CONFIG_BPF_SYSCALL
 	old_ctx = READ_ONCE(current->bpf_ctx);
-	new_ctx->prog = prog;
-	new_ctx->outer_run_ctx = old_ctx;
+	if (new_ctx) {
+		new_ctx->prog = prog;
+		new_ctx->outer_run_ctx = old_ctx;
+		new_ctx->priv_stack = (struct bpf_priv_stack_reservation){};
+	}
 	barrier();
 	WRITE_ONCE(current->bpf_ctx, new_ctx);
 #endif
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index f45c18b3cfa2..178f5dfeeaa5 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1113,6 +1113,7 @@ struct bpf_subprog_info {
 	u32 exit_idx;
 	u32 callx_edge; /* 1 + the first of its env->callx_edges, 0 if none */
 	struct bpf_iarray *jt; /* jump table shared by all gotox of this subprogram */
+	u32 priv_stack_offset; /* frame end within an invocation slot */
 	u16 stack_depth; /* max. stack depth used by this function */
 	u16 stack_extra;
 	u16 arena_stack_depth; /* size of the frame in the stack in arena */
@@ -1812,6 +1813,9 @@ static inline enum bpf_prog_type resolve_prog_type(const struct bpf_prog *prog)
 		prog->aux->saved_dst_prog_type : prog->type;
 }
 
+int bpf_priv_stack_alloc(struct bpf_prog_aux *aux, struct bpf_subprog_info *subprog_info,
+	u32 subprog_cnt);
+
 static inline bool bpf_prog_check_recur(const struct bpf_prog *prog)
 {
 	switch (resolve_prog_type(prog)) {
diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
index d26c62326e58..388362849e65 100644
--- a/kernel/bpf/Makefile
+++ b/kernel/bpf/Makefile
@@ -11,7 +11,7 @@ obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_
 obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
 obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o
 obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o
-obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o check_btf.o flow.o join.o
+obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o check_btf.o flow.o join.o priv_stack.o
 obj-${CONFIG_BPF_LSM}	  += bpf_inode_storage.o
 obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o
 obj-$(CONFIG_BPF_JIT) += trampoline.o
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 2f0c9e246876..f072a923b284 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -290,6 +290,7 @@ void __bpf_prog_free(struct bpf_prog *fp)
 	if (fp->aux) {
 #ifdef CONFIG_BPF_SYSCALL
 		bpf_prog_stream_free(fp);
+		bpf_priv_stack_free(fp->aux);
 #endif
 		mutex_destroy(&fp->aux->used_maps_mutex);
 		mutex_destroy(&fp->aux->dst_mutex);
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 4d9b67981520..4f9a53b1ae1a 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -1765,6 +1765,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
 
 		func[i]->aux->name[0] = 'F';
 		func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
+		func[i]->aux->priv_stack_offset = env->subprog_info[i].priv_stack_offset;
 		if (env->subprog_info[i].priv_stack_mode == PRIV_STACK_ADAPTIVE)
 			func[i]->aux->jits_use_priv_stack = true;
 
diff --git a/kernel/bpf/priv_stack.c b/kernel/bpf/priv_stack.c
new file mode 100644
index 000000000000..05a1ba9bce13
--- /dev/null
+++ b/kernel/bpf/priv_stack.c
@@ -0,0 +1,247 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2026 CrowdStrike
+ *
+ * BPF concurrency-safe private stacks
+ * Guard handling adapted from arch/x86/net/bpf_jit_comp.c
+ */
+#include <linux/atomic.h>
+#include <linux/bitops.h>
+#include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
+#include <linux/cpu.h>
+#include <linux/overflow.h>
+#include <linux/percpu.h>
+#include <linux/sched.h>
+#include <linux/slab.h>
+#include <linux/topology.h>
+#include <linux/vmalloc.h>
+
+#define BPF_PRIV_STACK_GUARD_VAL	0xEB9F12345678eb9fULL
+#define BPF_PRIV_STACK_GUARD_SZ	8
+#define BPF_PRIV_STACK_NR_SLOTS	4
+/* limit private stack slots to single word */
+static_assert(BPF_PRIV_STACK_NR_SLOTS > 0 && BPF_PRIV_STACK_NR_SLOTS <= BITS_PER_LONG);
+
+struct bpf_priv_stack_cpu {
+	unsigned long busy;
+	void *slots;
+};
+
+struct bpf_priv_stack_frame {
+	u32 start;
+	u32 end;
+};
+
+struct bpf_priv_stack_pool {
+	struct bpf_priv_stack_cpu __percpu *priv_stack_cpu;
+	atomic64_t exhaustions;
+	u32 slot_size;
+	u32 nr_frames;
+	struct bpf_priv_stack_frame frames[];
+};
+
+static void priv_stack_pool_free(struct bpf_priv_stack_pool *pool)
+{
+	if (pool->priv_stack_cpu) {
+		int cpu;
+
+		for_each_possible_cpu(cpu)
+			kvfree(per_cpu_ptr(pool->priv_stack_cpu, cpu)->slots);
+		free_percpu(pool->priv_stack_cpu);
+	}
+
+	kfree(pool);
+}
+
+static void __priv_stack_guards(struct bpf_priv_stack_pool *pool, bool init)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu) {
+		struct bpf_priv_stack_cpu *priv_stack_cpu;
+
+		priv_stack_cpu = per_cpu_ptr(pool->priv_stack_cpu, cpu);
+		for (size_t slot = 0; slot < BPF_PRIV_STACK_NR_SLOTS; slot++) {
+			void *slot_base = priv_stack_cpu->slots + (pool->slot_size * slot);
+
+			for (int i = 0; i < pool->nr_frames; i++) {
+				struct bpf_priv_stack_frame *frame;
+				u64 *low, *high;
+
+				frame = &pool->frames[i];
+				if (!frame->end)
+					continue;
+
+				low = slot_base + frame->start;
+				high = slot_base + frame->end;
+
+				if (init) {
+					*low = BPF_PRIV_STACK_GUARD_VAL;
+					*high = BPF_PRIV_STACK_GUARD_VAL;
+				} else if (*low != BPF_PRIV_STACK_GUARD_VAL ||
+					   *high != BPF_PRIV_STACK_GUARD_VAL) {
+					pr_err("BPF private stack guard corrupted: cpu %d slot %zu func %d\n",
+						cpu, slot, i);
+				}
+			}
+		}
+	}
+}
+
+static void priv_stack_guards_init(struct bpf_priv_stack_pool *pool)
+{
+	__priv_stack_guards(pool, true);
+}
+
+static void priv_stack_guards_check(struct bpf_priv_stack_pool *pool)
+{
+	__priv_stack_guards(pool, false);
+}
+
+int bpf_priv_stack_alloc(struct bpf_prog_aux *aux, struct bpf_subprog_info *subprog_info,
+	u32 subprog_cnt)
+{
+	struct bpf_priv_stack_pool *pool;
+	struct bpf_priv_stack_cpu *priv_stack_cpu;
+	gfp_t gfp = bpf_memcg_flags(GFP_KERNEL);
+	u32 size = 0;
+	size_t bytes;
+	int i, cpu, ret;
+
+	/* TODO - arm64 jit private stack pool */
+	if (!IS_ENABLED(CONFIG_X86_64) || !aux->jits_use_priv_stack)
+		return 0;
+
+	pool = kzalloc_flex(*pool, frames, subprog_cnt, gfp);
+	if (!pool)
+		return -ENOMEM;
+
+	for (i = 0; i < subprog_cnt; i++) {
+		struct bpf_subprog_info *sub;
+		u32 frame_size, end;
+
+		sub = &subprog_info[i];
+		if (sub->priv_stack_mode != PRIV_STACK_ADAPTIVE)
+			continue;
+
+		frame_size = round_up((u32)sub->stack_depth, 8) + (BPF_PRIV_STACK_GUARD_SZ * 2);
+		if (check_add_overflow(size, frame_size, &end)) {
+			ret = -E2BIG;
+			goto out_free;
+		}
+
+		pool->frames[i].start = size;
+		pool->frames[i].end = end - BPF_PRIV_STACK_GUARD_SZ;
+		sub->priv_stack_offset = pool->frames[i].end;
+		size = end;
+	}
+
+	if (!size) {
+		kfree(pool);
+		return 0;
+	}
+
+	pool->nr_frames = subprog_cnt;
+	pool->slot_size = size;
+	bytes = (size_t)size * BPF_PRIV_STACK_NR_SLOTS;
+
+	pool->priv_stack_cpu = __alloc_percpu_gfp(sizeof(*priv_stack_cpu), __alignof__(*priv_stack_cpu), gfp);
+	if (!pool->priv_stack_cpu) {
+		ret = -ENOMEM;
+		goto out_free;
+	}
+
+	/* Keep large backing arrays out of the size-limited percpu allocator. */
+	for_each_possible_cpu(cpu) {
+		priv_stack_cpu = per_cpu_ptr(pool->priv_stack_cpu, cpu);
+		priv_stack_cpu->slots = kvzalloc_node(bytes, gfp, cpu_to_node(cpu));
+		if (!priv_stack_cpu->slots) {
+			ret = -ENOMEM;
+			goto out_free;
+		}
+	}
+
+	priv_stack_guards_init(pool);
+	aux->priv_stack_pool = pool;
+	aux->priv_stack_offset = subprog_info[0].priv_stack_offset;
+
+	return 0;
+
+out_free:
+	priv_stack_pool_free(pool);
+	return ret;
+}
+
+void bpf_priv_stack_free(struct bpf_prog_aux *aux)
+{
+	struct bpf_priv_stack_pool *pool = aux->priv_stack_pool;
+
+	if (!pool)
+		return;
+
+	priv_stack_guards_check(pool);
+	priv_stack_pool_free(pool);
+	aux->priv_stack_pool = NULL;
+}
+
+bool notrace bpf_priv_stack_acquire(struct bpf_prog *prog,
+				  struct bpf_priv_stack_reservation *res)
+{
+	struct bpf_priv_stack_cpu *priv_stack_cpu;
+	struct bpf_priv_stack_pool *pool;
+	unsigned long available;
+	unsigned int slot;
+
+	pool = prog->aux->main_prog_aux->priv_stack_pool;
+	if (!pool)
+		return true;
+
+	priv_stack_cpu = this_cpu_ptr(pool->priv_stack_cpu);
+	for (;;) {
+		available = ~READ_ONCE(priv_stack_cpu->busy) & GENMASK(BPF_PRIV_STACK_NR_SLOTS - 1, 0);
+		if (!available) {
+			atomic64_inc(&pool->exhaustions);
+			return false;
+		}
+		slot = __ffs(available);
+		if (!test_and_set_bit_lock(slot, &priv_stack_cpu->busy))
+			break;
+	}
+
+	res->slot_base = priv_stack_cpu->slots + (size_t)slot * pool->slot_size;
+	res->busy = &priv_stack_cpu->busy;
+	res->slot = slot;
+	/* Publish the pool associated with the new reservation. */
+	barrier();
+	WRITE_ONCE(res->pool, pool);
+
+	return true;
+}
+
+void notrace bpf_priv_stack_release(struct bpf_priv_stack_reservation *res)
+{
+	if (!res->pool)
+		return;
+
+	clear_bit_unlock(res->slot, res->busy);
+	res->pool = NULL;
+	res->slot_base = NULL;
+}
+
+/*
+ * Find the invocation's reserved slot and return the prog's
+ * private frame pointer.
+ */
+void *notrace bpf_priv_stack_frame(struct bpf_prog *prog)
+{
+	struct bpf_priv_stack_pool *pool = prog->aux->main_prog_aux->priv_stack_pool;
+	struct bpf_run_ctx *ctx;
+
+	/* Nested run contexts may sit above owning reservation. */
+	for (ctx = READ_ONCE(current->bpf_ctx); READ_ONCE(ctx->priv_stack.pool) != pool;
+	     ctx = ctx->outer_run_ctx)
+		;
+
+	return ctx->priv_stack.slot_base + prog->aux->priv_stack_offset;
+}
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index 44a73766aa98..c8e3f8ca3416 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -1355,6 +1355,9 @@ static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tram
 		}
 	}
 
+	if (!bpf_priv_stack_acquire(prog, &run_ctx->run_ctx.priv_stack))
+		return 0;
+
 	return bpf_prog_start_time();
 }
 
@@ -1393,6 +1396,7 @@ static void notrace __bpf_prog_exit_recur(struct bpf_prog *prog, u64 start,
 	bpf_reset_run_ctx(run_ctx->saved_run_ctx);
 
 	update_prog_stats(prog, start);
+	bpf_priv_stack_release(&run_ctx->run_ctx.priv_stack);
 	rcu_read_unlock_migrate();
 }
 
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 39f1c6eb1131..3ec159520f33 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -5331,6 +5331,16 @@ static enum priv_stack_mode bpf_enable_priv_stack(struct bpf_prog *prog)
 	if (!bpf_jit_supports_private_stack())
 		return NO_PRIV_STACK;
 
+	/* TODO - arm64  and other types */
+	if (IS_ENABLED(CONFIG_X86_64)) {
+		if (prog->sleepable || prog->type != BPF_PROG_TYPE_TRACING)
+			return NO_PRIV_STACK;
+		if (prog->expected_attach_type == BPF_TRACE_FENTRY ||
+		    prog->expected_attach_type == BPF_TRACE_FEXIT)
+			return PRIV_STACK_ADAPTIVE;
+		return NO_PRIV_STACK;
+	}
+
 	/* bpf_prog_check_recur() checks all prog types that use bpf trampoline
 	 * while kprobe/tp/perf_event/raw_tp don't use trampoline hence checked
 	 * explicitly.
@@ -25260,6 +25270,11 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
 								     : false;
 	}
 
+	if (ret == 0 && !bpf_prog_is_offloaded(env->prog->aux) &&
+			env->prog->jit_requested) {
+		ret = bpf_priv_stack_alloc(env->prog->aux, env->subprog_info, env->subprog_cnt);
+	}
+
 	if (ret == 0)
 		ret = bpf_fixup_call_args(env);
 
-- 
2.56.1

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-25  2:06 ` JP Kobryn (CrowdStrike)
@ 2026-11-25  4:02   ` Alexei Starovoitov
  2026-11-25 20:23     ` JP Kobryn (CrowdStrike)
  0 siblings, 1 reply; 28+ messages in thread
From: Alexei Starovoitov @ 2026-11-25  4:02 UTC (permalink / raw)
  To: JP Kobryn (CrowdStrike), mikeba, 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, linux-trace-kernel, linux-kernel, bpf,
	Martin Kelly, Andrey Grodzovsky, tyler.wilson

On Tue, Nov 24, 2026 at 06:06 PM JP Kobryn (CrowdStrike) <jp.kobryn@linux.dev> wrote:
> The patch below is a proof of concept scoped only to x86_64 trampolines.
> It was validated with the bpftrace prog that precedes the patch, along
> with jited assembly dump to inspect the new private frame API calls.
> Overhead measured was roughly 60ns, but this was just a first take and I
> believe it can be optimized especially in the JIT area. If it makes
> sense, this could be integrated into the series or sent as a follow-up.

If you want to redesign private stack please take inspiration
from insert_arena_stack_get(). Much lower overhead and no artificial limits.

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-25  2:00     ` Alexei Starovoitov
@ 2026-11-25  5:35       ` Andrii Nakryiko
  0 siblings, 0 replies; 28+ messages in thread
From: Andrii Nakryiko @ 2026-11-25  5:35 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: mikeba, bpf, Daniel Borkmann, Andrii Nakryiko, 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, linux-kernel

On Tue, Nov 24, 2026 at 6:00 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Tue, Nov 24, 2026 at 01:47 PM Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
> >> +       for (ctx = old_ctx; ctx; ctx = ctx->saved_run_ctx) {
> >
> > do you mind checking if unlikely() around ctx improves performance?
>
> Mike,
> pls try it on respin.
>
> > So basically to opt out from this behavior BPF program would need to
> > stop using any per-CPU maps and just implement its own poor man's
> > per-CPU storage?
>
> poor mans's per-cpu with bpf_get_smp_processor_id() shouldn't work
> as an opt-out. See section 6.

it's hard to weed through that verbiage in section 6, are you
proposing to mark "needs per-cpu protection" if BPF program is calling
bpf_get_smp_processor_id()?

>
> > And even using SEC(".percpu") would still be detected
> > as per-CPU usage, right?
>
> This one is pretty much brand new, so there is no data to make
> a decision. I can see arguments both ways.

.percpu is backed by PERCPU_ARRAY, so we don't need any data to say
that it will trigger the protection, do we?

>
> > Should we give a way to opt-out of this protection explicitly while
> > still using per-CPU maps?
>
> After looking at the data, I think we shouldn't allow it.
>
> An opt-out is for a prog that protects its per-cpu data on its own.
> In the v1 thread your example was stack_trace_scratch in wprof.
> It doesn't work. wprof is broken today, without this series.
> An event gets args or a stack of another task. wprof 8dd41e18 on
> bpf-next, corrupt events / all events:
>
>   -U                             what's wrong  lazy      full
>   -----------------------------  ------------  --------  ---------
>   u:func (arg:0, arg:1:str)      args          1928/16M  9111/17M
>   u:func (..., stack)            stack         683/15M   3419/15M
>   raw_tp:sys_enter (arg:id) and  args          3491/48M  16673/44M
>   tp:syscalls:sys_enter_openat
>
> utrace_scratch has no protection.

yeah, I was lazy :)

> stack_trace_cur handles nesting, but not preemption:
>
>   task A                task B                task C
>   idx 0, st_cnt = 1
>   preempted
>                         idx 1, st_cnt = 2
>                         preempted
>   put, st_cnt = 1
>                                               idx 1    <- slot of B

yeah, this is true, just a stack-like approach doesn't give 100%
guarantee (though in practice it has been good enough so far, though
perhaps I just didn't heavily run into new preemptible kernels), but a
better approach would be to use bitset + find first unset bit
approach. I'll play with it.

>
> With -S offcpu task C is wprof_task_switch. Then the utrace event has
> a record of kind ST_OFFCPU.
> uprobes have no protection. That's what an opt-out gives to raw_tp, tp
> and fentry progs:
>
>   -U, lazy                 today     preempt off  opt-out
>   -----------------------  --------  -----------  --------
>   raw_tp only, args        0         0            2849/45M
>   raw_tp only, stack       0         0            4081/27M
>   raw_tp and tp, args      3491/48M  0            3166/54M
>
> The series doesn't touch uprobes, so wprof needs its own fix for them.
> The default mode of wprof, without -U, is fine.
>
> Now the bigger question: do we need preempt_disable() at all?
> We do. Without it progs of 17 out of 69 projects in [1] corrupt their
> data. 12 of them have progs that are clean today.
>

"Can corrupt in rare circumstances (in practice)", but sure, sure :)

Still, choosing between handling all events and having bugs (fixable)
of handling per-CPU data in my BPF program, I'll choose the former.
Even if we don't want to make that a default, that's fine, but please
at least give me a way to opt out from the kernel's
over-protectiveness :) and not using per-cpu maps is not a way to opt
out, it has its valid uses even with preemption. Think about simple
per-CPU stats/counters updated with atomic operations and aggregated
across CPUs in user space, periodically. E.g., I believe BPF
selftests's bench uses this approach, why would it need preemption
disablement just for this?

[...]

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-24 20:26     ` Alexei Starovoitov
@ 2026-11-25 12:03       ` Mike Bazov
  0 siblings, 0 replies; 28+ messages in thread
From: Mike Bazov @ 2026-11-25 12:03 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, Daniel Borkmann, Andrii Nakryiko, 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, linux-kernel

On Tue, Nov 24, 2026 at 08:26 PM Alexei Starovoitov wrote:
> Keep count and timestamp per-cpu. Call bpf_timed_may_goto_save() in
> bpf_prog_enter_run_ctx() and bpf_timed_may_goto_restore() in
> bpf_prog_exit_run_ctx(), like bpf_timer_cb() does.
> Then the preempted run gets its count and timestamp back.

i tried it, it works when the run that preempts finishes first, as
with bpf_timer_cb(). With preemption it can be preempted too, and the
preempted run can go on before it ends:

  task A, cpu 0                  task B, cpu 0
  loops, preempted
                                 save: A's budget into B's stack,
                                 fresh one in the per-cpu struct
                                 loops, preempted
  goes on with B's fresh budget
  exits: owner is B's, no restore
                                 goes on, exits: restores A's,
                                 which nobody uses any more

A fentry prog spins in a can_loop loop, 3 SCHED_OTHER tasks on one
CPU, 4 runs each:

  kernel              runs  dropped  longest   over 500 ms
  bpf-next            4     8        250 ms    0
  save/restore        12    0        1253 ms   7 of 12
  save/restore        12    0        1752 ms   9 of 12
  save/restore (*)    24    0        2263 ms   22 of 24

  (*) 6 tasks instead of 3

You can see it in the run times: they land near multiples of 250 ms,
one extra 250 ms for every fresh budget a run picked up from another.

An lsm prog has no prog->active, so the same test gives the same
result on plain bpf-next: 1253 ms, 8 of 12 over 500 ms. The overwrite
already exists for all progs that can be preempted (lsm, cgroup,
struct_ops, uprobe, iter, sk_msg, syscall).

A fix for all of them has to change how may_goto keeps its budget:
lsm, cgroup and sk_msg progs never go through bpf_prog_enter_run_ctx().
I'd rather split it out of this series: v4 takes may_goto out of the
mark, so tracing progs behave like lsm ones today, and a separate
series fixes the budget for all prog types. Does that work for you?

Thanks,
Mike

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx
  2026-11-24 22:04   ` Andrii Nakryiko
@ 2026-11-25 14:12     ` Mike Bazov
  0 siblings, 0 replies; 28+ messages in thread
From: Mike Bazov @ 2026-11-25 14:12 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	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, linux-kernel

On Tue, Nov 24, 2026 at 2:04 PM Andrii Nakryiko wrote:
> Am I missing some complications that would prevent that separation of concerns?

Not really. I can do that in v4, it simplifies things.

Thanks,
Mike

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
  2026-11-25  4:02   ` Alexei Starovoitov
@ 2026-11-25 20:23     ` JP Kobryn (CrowdStrike)
  0 siblings, 0 replies; 28+ messages in thread
From: JP Kobryn (CrowdStrike) @ 2026-11-25 20:23 UTC (permalink / raw)
  To: Alexei Starovoitov, mikeba, 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, linux-trace-kernel, linux-kernel, bpf,
	Martin Kelly, Andrey Grodzovsky, tyler.wilson

On 11/24/26 8:02 PM, Alexei Starovoitov wrote:
> On Tue, Nov 24, 2026 at 06:06 PM JP Kobryn (CrowdStrike) <jp.kobryn@linux.dev> wrote:
>> The patch below is a proof of concept scoped only to x86_64 trampolines.
>> It was validated with the bpftrace prog that precedes the patch, along
>> with jited assembly dump to inspect the new private frame API calls.
>> Overhead measured was roughly 60ns, but this was just a first take and I
>> believe it can be optimized especially in the JIT area. If it makes
>> sense, this could be integrated into the series or sent as a follow-up.
> 
> If you want to redesign private stack please take inspiration
> from insert_arena_stack_get(). Much lower overhead and no artificial limits.

Thanks for taking a look. Since it looks like v4 will conditionally disable
preemption for run context entry to exit, this concept does not have to be
tied to this series.

We'll pursue the private stack work separately, but will continue testing
revisions of this series.

^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2026-11-25 20:23 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 11:29 [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
2026-11-17 11:29 ` [PATCH bpf-next v3 1/4] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
2026-11-17 11:29 ` [PATCH bpf-next v3 2/4] bpf: Mark programs whose runs must not overlap on a CPU Mike Bazov via B4 Relay
2026-11-19  5:04   ` JP Kobryn (CrowdStrike)
2026-11-19 11:23     ` [EXT.MSG] " Mike Bazov
2026-11-17 11:29 ` [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
2026-11-17 12:20   ` bot+bpf-ci
2026-11-19  5:05   ` JP Kobryn (CrowdStrike)
2026-11-19 11:35     ` [EXT.MSG] " Mike Bazov
2026-11-23 12:49     ` Mike Bazov
2026-11-24 22:04   ` Andrii Nakryiko
2026-11-25 14:12     ` Mike Bazov
2026-11-17 11:29 ` [PATCH bpf-next v3 4/4] selftests/bpf: Add preemption recursion test for tracing programs Mike Bazov via B4 Relay
2026-11-19  5:09   ` JP Kobryn (CrowdStrike)
2026-11-19 12:13     ` [EXT.MSG] " Mike Bazov
2026-11-19  5:02 ` [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs JP Kobryn (CrowdStrike)
     [not found]   ` <CAPnbwjpmRCR3F4V3StUz=702AdWZzHHZjP68GepnYdYykjoC8g@mail.gmail.com>
2026-11-19 11:22     ` [EXT.MSG] " Mike Bazov
2026-11-21  3:01       ` JP Kobryn (CrowdStrike)
2026-11-24  6:21 ` Alexei Starovoitov
2026-11-24 16:10   ` Mike Bazov
2026-11-24 20:26     ` Alexei Starovoitov
2026-11-25 12:03       ` Mike Bazov
2026-11-24 21:47   ` Andrii Nakryiko
2026-11-25  2:00     ` Alexei Starovoitov
2026-11-25  5:35       ` Andrii Nakryiko
2026-11-25  2:06 ` JP Kobryn (CrowdStrike)
2026-11-25  4:02   ` Alexei Starovoitov
2026-11-25 20:23     ` JP Kobryn (CrowdStrike)

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®