mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mike Bazov via B4 Relay <devnull+mikeba.sentinelone.com@kernel.org>
To: bpf@vger.kernel.org, Alexei Starovoitov <ast@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
	 Eduard Zingerman <eddyz87@gmail.com>,
	 Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Song Liu <song@kernel.org>,
	 Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	 John Fastabend <john.fastabend@gmail.com>,
	 Emil Tsalapatis <emil@etsalapatis.com>,
	 Ihor Solodrai <ihor.solodrai@linux.dev>,
	 Steven Rostedt <rostedt@goodmis.org>,
	 Masami Hiramatsu <mhiramat@kernel.org>,
	 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	 JP Kobryn <jp.kobryn@linux.dev>,
	linux-trace-kernel@vger.kernel.org,
	 linux-kernel@vger.kernel.org,
	Mike Bazov <mikeba@sentinelone.com>
Subject: [PATCH bpf-next v5 3/5] bpf: Check program recursion per task via bpf_ctx
Date: Tue, 01 Dec 2026 17:32:14 +0200	[thread overview]
Message-ID: <20261201-bpf-preempt-v2-r2-v5-3-a6bb30daae35@sentinelone.com> (raw)
In-Reply-To: <20261201-bpf-preempt-v2-r2-v5-0-a6bb30daae35@sentinelone.com>

From: Mike Bazov <mikeba@sentinelone.com>

Trampoline programs, raw tracepoint programs 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.

Since commit 7dadeaa6e851 ("sched: Further restrict the preemption
modes") x86 and arm64 have only lazy and full preemption, so this
happens in normal operation, and every drop is a lost event.

Check recursion per task instead. The runners already link a
stack-allocated bpf_run_ctx into current->bpf_ctx with
bpf_set_run_ctx(). Move saved_run_ctx into struct bpf_run_ctx, so every
frame links to the outer one, and record in the frame the program that
runs in it. bpf_prog_enter_run_ctx() fills in the frame, refuses a
program that a frame in the chain already runs, and links the frame only
if the program may run; bpf_prog_exit_run_ctx() unlinks it. Interrupts
and NMIs run on top of the chain of the task they interrupt, so
recursion through them is still caught. The syscall tracepoint runner
links a frame per program.

An async callback doesn't run in a hook, and current may be any task.
Its runner now links a frame of its own on the stack, with
BPF_PROG_ASYNC_CB as the program, so that bpf_in_async_cb() still
recognizes it and a program that the callback runs into is still checked
against the frames below. This replaces bpf_async_run_ctx, which all
CPUs shared.

The frame of a marked program is unlinked before preemption is enabled:
enabling preemption can switch tasks, and a program that runs in the
scheduler would otherwise be dropped as recursion.

A program with prog->percpu_state set runs with preemption disabled
instead, so that no other task starts a run of it on the CPU before it
exits. Its per-CPU data stays its own, and the runs of other tasks wait
instead of being dropped. Two marked programs that share a per-CPU map
are also kept apart: no other task runs on the CPU while one of them
runs. The per-program counter never did that. A sleepable program can't
disable preemption: it has to use bpf_preempt_disable() itself, and a
sleepable struct_ops program no longer gets a private stack.

An extension (freplace) runs in the run of its target, so a marked
extension can only be attached to a raw tracepoint or tracepoint program
that runs with preemption disabled: marked and not sleepable.

Trade-offs:

- A marked program can't be preempted until it exits. A program that
  tail-calls is marked, so its targets run that way too.
- A sleepable program with per-CPU state gets no protection.
- On PREEMPT_RT a marked program runs non-preemptibly too, as kprobe
  programs do.
- An unmarked program that keeps per-CPU state the verifier doesn't
  see can have two runs share it, and nothing counts that.
- A run that preempts another one inside a timed may_goto loop can
  renew the other's 250 ms budget, as it already can for lsm programs.
- Attaching a marked extension to a raw tracepoint or tracepoint
  program that is unmarked or sleepable now fails with -EINVAL.

Signed-off-by: Mike Bazov <mikeba@sentinelone.com>
---
 include/linux/bpf.h      | 120 ++++++++++++++++++++++++++++++++++++++++-------
 kernel/bpf/helpers.c     |   5 +-
 kernel/bpf/syscall.c     |  15 ++++++
 kernel/bpf/trampoline.c  |  29 +++++-------
 kernel/bpf/verifier.c    |   5 +-
 kernel/trace/bpf_trace.c |  11 ++---
 net/bpf/test_run.c       |   9 +---
 net/ipv4/bpf_tcp_ops.c   |   6 +--
 8 files changed, 141 insertions(+), 59 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 2dfd963a2703..2172e732ccc2 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2801,7 +2801,22 @@ 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;
+	/*
+	 * The program that runs in the frame, see bpf_prog_enter_run_ctx().
+	 * NULL if the runner records none, BPF_PROG_ASYNC_CB in an async callback.
+	 */
+	const struct bpf_prog *prog;
+};
+
+/* ->prog of the frame of an async callback, see bpf_in_async_cb() */
+#define BPF_PROG_ASYNC_CB	((const struct bpf_prog *)1)
 
 struct bpf_cg_run_ctx {
 	struct bpf_run_ctx run_ctx;
@@ -2819,25 +2834,98 @@ 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)
+/*
+ * Link @new_ctx into the chain of the current task, with @prog as the program
+ * that runs in it. Returns the frame it pointed to before.
+ */
+static __always_inline struct bpf_run_ctx *bpf_set_run_ctx_prog(struct bpf_run_ctx *new_ctx,
+								const struct bpf_prog *prog)
 {
 	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 = prog;
+	}
+	/* 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;
 }
 
-static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
+static __always_inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
+{
+	return bpf_set_run_ctx_prog(new_ctx, NULL);
+}
+
+static __always_inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
+{
+#ifdef CONFIG_BPF_SYSCALL
+	WRITE_ONCE(current->bpf_ctx, old_ctx);
+	/* Unlink the frame before its stack storage can be reused. */
+	barrier();
+#endif
+}
+
+/*
+ * Run @prog in @run_ctx: link the frame into the chain of the current task,
+ * unless a frame in the chain runs @prog already. That would be recursion, and
+ * the frame is not linked. Interrupts and NMIs run on top of the chain of the
+ * task they interrupt, so recursion through them is caught too.
+ *
+ * A program with per-CPU state (prog->percpu_state) runs with preemption
+ * disabled, so that no other run of it starts on this CPU before it exits. A
+ * sleepable one has to use bpf_preempt_disable() itself. The frame of a
+ * non-sleepable one is linked only while preemption is disabled: a task switch
+ * runs the scheduler, and a program there must not find @prog in the chain.
+ *
+ * Returns false if @prog must not run. Call bpf_prog_exit_run_ctx() either way.
+ * 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 *ctx, *old_ctx = NULL;
+
 #ifdef CONFIG_BPF_SYSCALL
-	current->bpf_ctx = old_ctx;
+	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();
 }
 
 /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
@@ -3498,11 +3586,11 @@ static inline bool has_current_bpf_ctx(void)
 }
 
 /* An async callback runs in no hook, and current is not the task that has armed it */
-extern struct bpf_run_ctx bpf_async_run_ctx;
-
 static inline bool bpf_in_async_cb(void)
 {
-	return current->bpf_ctx == &bpf_async_run_ctx;
+	struct bpf_run_ctx *ctx = current->bpf_ctx;
+
+	return ctx && ctx->prog == BPF_PROG_ASYNC_CB;
 }
 
 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
@@ -3519,7 +3607,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;
 
@@ -3530,7 +3617,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) */
@@ -3539,15 +3625,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);
@@ -3555,10 +3640,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/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 7b3e10a4a97e..aea624c85445 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1235,14 +1235,13 @@ static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running);
 
 static void bpf_async_refcount_put(struct bpf_async_cb *cb);
 
-struct bpf_run_ctx bpf_async_run_ctx;
-
 static void bpf_run_async_cb(struct bpf_prog *prog, bpf_callback_t callback_fn,
 			     struct bpf_map *map, void *key, void *value)
 {
-	struct bpf_run_ctx *old_ctx = bpf_set_run_ctx(&bpf_async_run_ctx);
+	struct bpf_run_ctx run_ctx, *old_ctx;
 	struct bpf_timed_may_goto saved;
 
+	old_ctx = bpf_set_run_ctx_prog(&run_ctx, BPF_PROG_ASYNC_CB);
 	bpf_timed_may_goto_save(prog, &saved);
 	callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
 	bpf_timed_may_goto_restore(prog, &saved);
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 0bdb4288beb2..0525dc376c9a 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3878,6 +3878,21 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
 		err = -EINVAL;
 		goto out_unlock;
 	}
+	/*
+	 * An extension runs in the run of its target. One that keeps per-CPU
+	 * state relies on its runs not overlapping on a CPU, so a target that
+	 * is checked per task must run with preemption disabled: marked and
+	 * not sleepable.
+	 */
+	if (prog->type == BPF_PROG_TYPE_EXT && prog->percpu_state &&
+	    (tgt_prog->type == BPF_PROG_TYPE_RAW_TRACEPOINT ||
+	     tgt_prog->type == BPF_PROG_TYPE_TRACEPOINT) &&
+	    (!tgt_prog->percpu_state || tgt_prog->sleepable)) {
+		bpf_log(log,
+			"Freplace prog with per-CPU state needs a target that runs with preemption disabled.\n");
+		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 96454443441b..619297eae8c1 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -1341,9 +1341,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);
@@ -1384,10 +1382,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);
-
+	/* Before the exit, which can enable preemption: time off the CPU is not run time */
 	update_prog_stats(prog, start);
-	bpf_prog_put_recursion_context(prog);
+	bpf_prog_exit_run_ctx(prog, &run_ctx->run_ctx);
 	rcu_read_unlock_migrate();
 }
 
@@ -1400,7 +1397,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;
 }
@@ -1409,7 +1406,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();
 }
@@ -1421,9 +1418,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);
@@ -1435,10 +1430,8 @@ 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);
-
 	update_prog_stats(prog, start);
-	bpf_prog_put_recursion_context(prog);
+	bpf_prog_exit_run_ctx(prog, &run_ctx->run_ctx);
 	migrate_enable();
 	rcu_read_unlock_trace();
 }
@@ -1450,7 +1443,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();
 }
@@ -1458,7 +1451,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();
@@ -1471,7 +1464,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();
 }
@@ -1480,7 +1473,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/bpf/verifier.c b/kernel/bpf/verifier.c
index dd35abb37a23..2e3be7c11221 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -5434,7 +5434,8 @@ static enum priv_stack_mode bpf_enable_priv_stack(struct bpf_prog *prog)
 	 * 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.
+	 * programs that get a private stack run with preemption disabled, see
+	 * bpf_prog_enter_run_ctx(). A sleepable one can't, so it gets none.
 	 */
 	switch (prog->type) {
 	case BPF_PROG_TYPE_KPROBE:
@@ -5450,7 +5451,7 @@ static enum priv_stack_mode bpf_enable_priv_stack(struct bpf_prog *prog)
 	case BPF_PROG_TYPE_PERF_EVENT:
 		return PRIV_STACK_ADAPTIVE;
 	case BPF_PROG_TYPE_STRUCT_OPS:
-		if (prog->aux->priv_stack_requested)
+		if (prog->aux->priv_stack_requested && !prog->sleepable)
 			return PRIV_STACK_ADAPTIVE;
 		fallthrough;
 	default:
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 7b9bd6f560a5..2031c3727320 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2125,7 +2125,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) {
@@ -2135,19 +2134,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 926b29d36883..6b8ad08fa54d 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 (bpf_in_async_cb() || WARN_ON_ONCE(!ctx->saved_run_ctx))
+	if (bpf_in_async_cb() || 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



  parent reply	other threads:[~2026-12-01 15:33 UTC|newest]

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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201-bpf-preempt-v2-r2-v5-3-a6bb30daae35@sentinelone.com \
    --to=devnull+mikeba.sentinelone.com@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=jp.kobryn@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=memxor@gmail.com \
    --cc=mhiramat@kernel.org \
    --cc=mikeba@sentinelone.com \
    --cc=rostedt@goodmis.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®