From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: <mikeba@sentinelone.com>, <bpf@vger.kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>
Cc: "Martin KaFai Lau" <martin.lau@linux.dev>,
"Eduard Zingerman" <eddyz87@gmail.com>,
"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Song Liu" <song@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Jiri Olsa" <jolsa@kernel.org>,
"John Fastabend" <john.fastabend@gmail.com>,
"Emil Tsalapatis" <emil@etsalapatis.com>,
"Ihor Solodrai" <ihor.solodrai@linux.dev>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Masami Hiramatsu" <mhiramat@kernel.org>,
"Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>,
"JP Kobryn" <jp.kobryn@linux.dev>,
<linux-trace-kernel@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf-next v4 3/6] bpf: Link the async run context without writing to it
Date: Tue, 01 Dec 2026 03:49:49 +0000 [thread overview]
Message-ID: <DN938U8IT4I1.QQIHQPWJ0X86@gmail.com> (raw)
In-Reply-To: <20261130-bpf-preempt-v2-r2-v4-3-bd28389dfd64@sentinelone.com>
On Mon, Nov 30, 2026 at 03:54 AM Mike Bazov via B4 Relay <devnull+mikeba.sentinelone.com@kernel.org> wrote:
> Set current->bpf_ctx directly instead, so that the frame is never
> written. No functional change.
This doens't look right. Consider:
task A, cpu 0
X runs, marked, preemption is off
irq
softirq on irq exit
bpf_timer_cb() or bpf_rcu_run_callback()
the callback hits the hook of X
the chain is X -> bpf_async_run_ctx -> NULL
X runs, overwrites the per-cpu data of the first run
preempt_disable() doesn't hold softirqs back.
A softirq that hits the hook of X without a callback is refused.
bpf_in_async_cb() only needs a way to recognize the frame.
Put the frame on the stack and link it like the other frames.
Then this patch and its trade-off in the cover letter go away.
Pls fold it into patch 4.
I built it together with patch 4 and didn't run it. The first hunk
repeats struct bpf_run_ctx of patch 4, the define is the new part:
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4f9c2bf..e42705b 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2800,7 +2801,19 @@ 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() */
+ 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;
@@ -3497,11 +3567,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);
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 7b3e10a..aea624c 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);
next prev parent reply other threads:[~2026-12-01 3:49 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 11:54 [PATCH bpf-next v4 0/6] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 1/6] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
2026-11-30 12:07 ` sashiko-bot
2026-11-30 11:54 ` [PATCH bpf-next v4 2/6] bpf: Mark programs that keep per-CPU state Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 3/6] bpf: Link the async run context without writing to it Mike Bazov via B4 Relay
2026-12-01 3:49 ` Alexei Starovoitov [this message]
2026-11-30 11:54 ` [PATCH bpf-next v4 4/6] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
2026-11-30 12:26 ` sashiko-bot
2026-11-30 11:54 ` [PATCH bpf-next v4 5/6] bpf: Remove the per-CPU recursion counter of programs Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 6/6] selftests/bpf: Add preemption recursion test for tracing programs Mike Bazov via B4 Relay
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DN938U8IT4I1.QQIHQPWJ0X86@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=andrii@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