Re: [PATCH bpf-next v4 3/6] bpf: Link the async run context without writing to it
From: Alexei Starovoitov
Date: Mon Nov 30 2026 - 22:49:58 EST
On Mon, Nov 30, 2026 at 03:54 AM Mike Bazov via B4 Relay <devnull+mikeba.sentinelone.com@xxxxxxxxxx> 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);