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 v4 5/6] bpf: Remove the per-CPU recursion counter of programs
Date: Mon, 30 Nov 2026 13:54:05 +0200 [thread overview]
Message-ID: <20261130-bpf-preempt-v2-r2-v4-5-bd28389dfd64@sentinelone.com> (raw)
In-Reply-To: <20261130-bpf-preempt-v2-r2-v4-0-bd28389dfd64@sentinelone.com>
From: Mike Bazov <mikeba@sentinelone.com>
The previous patch was the last user of prog->active: the recursion of
every program is checked per task now. Remove the counter, its
bpf_prog_get_recursion_context() and bpf_prog_put_recursion_context()
helpers, and the per-CPU memory that each program allocated for it.
Signed-off-by: Mike Bazov <mikeba@sentinelone.com>
---
include/linux/bpf.h | 36 ------------------------------------
kernel/bpf/core.c | 12 ------------
2 files changed, 48 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index b24f26a48b77..7acb8b0ec576 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2055,7 +2055,6 @@ struct bpf_prog_aux {
struct bpf_map __rcu *st_ops_assoc;
};
-#define BPF_NR_CONTEXTS 4 /* normal, softirq, hardirq, NMI */
struct bpf_prog {
u16 pages; /* Number of allocated pages */
@@ -2086,7 +2085,6 @@ struct bpf_prog {
u8 tag[BPF_TAG_SIZE];
};
struct bpf_prog_stats __percpu *stats;
- u8 __percpu *active; /* u8[BPF_NR_CONTEXTS] for recursion protection */
unsigned int (*bpf_func)(const void *ctx,
const struct bpf_insn *insn);
struct bpf_prog_aux *aux; /* Auxiliary fields */
@@ -2378,40 +2376,6 @@ struct bpf_struct_ops_common_value {
enum bpf_struct_ops_state state;
};
-static inline bool bpf_prog_get_recursion_context(struct bpf_prog *prog)
-{
-#ifdef CONFIG_ARM64
- u8 rctx = interrupt_context_level();
- u8 *active = this_cpu_ptr(prog->active);
- u32 val;
-
- preempt_disable();
- active[rctx]++;
- val = le32_to_cpu(*(__le32 *)active);
- preempt_enable();
- if (val != BIT(rctx * 8))
- return false;
-
- return true;
-#else
- return this_cpu_inc_return(*(int __percpu *)(prog->active)) == 1;
-#endif
-}
-
-static inline void bpf_prog_put_recursion_context(struct bpf_prog *prog)
-{
-#ifdef CONFIG_ARM64
- u8 rctx = interrupt_context_level();
- u8 *active = this_cpu_ptr(prog->active);
-
- preempt_disable();
- active[rctx]--;
- preempt_enable();
-#else
- this_cpu_dec(*(int __percpu *)(prog->active));
-#endif
-}
-
static inline bool is_tracing_multi(enum bpf_attach_type type)
{
return type == BPF_TRACE_FENTRY_MULTI || type == BPF_TRACE_FEXIT_MULTI ||
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index facb0d6d2e8d..32a267656c0e 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -118,14 +118,6 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag
vfree(fp);
return NULL;
}
- fp->active = __alloc_percpu_gfp(sizeof(u8[BPF_NR_CONTEXTS]), 4,
- bpf_memcg_flags(GFP_KERNEL | gfp_extra_flags));
- if (!fp->active) {
- vfree(fp);
- kfree(aux);
- return NULL;
- }
-
fp->pages = size / PAGE_SIZE;
fp->aux = aux;
fp->aux->main_prog_aux = aux;
@@ -161,7 +153,6 @@ struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
prog->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags);
if (!prog->stats) {
- free_percpu(prog->active);
kfree(prog->aux);
vfree(prog);
return NULL;
@@ -282,7 +273,6 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
*/
fp_old->aux = NULL;
fp_old->stats = NULL;
- fp_old->active = NULL;
__bpf_prog_free(fp_old);
}
@@ -302,7 +292,6 @@ void __bpf_prog_free(struct bpf_prog *fp)
kfree(fp->aux);
}
free_percpu(fp->stats);
- free_percpu(fp->active);
vfree(fp);
}
@@ -1816,7 +1805,6 @@ static void bpf_prog_clone_free(struct bpf_prog *fp)
*/
fp->aux = NULL;
fp->stats = NULL;
- fp->active = NULL;
__bpf_prog_free(fp);
}
--
2.43.0
next prev parent reply other threads:[~2026-11-30 11:54 UTC|newest]
Thread overview: 7+ 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 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-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 11:54 ` Mike Bazov via B4 Relay [this message]
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=20261130-bpf-preempt-v2-r2-v4-5-bd28389dfd64@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®