[RFC PATCH 2/3] seccomp: Use the classic BPF filter cache
From: Xu Kuohai
Date: Fri Nov 20 2026 - 02:16:07 EST
From: Xu Kuohai <xukuohai@xxxxxxxxxx>
Define a classic BPF filter cache in seccomp. This allows identical
seccomp filters to share a single prepared program instead of each
paying the cost of the check, rewrite, migration, and JIT compilation
pipeline.
Signed-off-by: Xu Kuohai <xukuohai@xxxxxxxxxx>
---
kernel/seccomp.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index 01e26bfc6ea1..b1c40ffbc6f2 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -465,6 +465,9 @@ static inline void seccomp_assign_mode(struct task_struct *task,
}
#ifdef CONFIG_SECCOMP_FILTER
+
+static DEFINE_CBPF_CACHE(seccomp_cbpf_cache);
+
/* Returns 1 if the parent is an ancestor of the child. */
static int is_ancestor(struct seccomp_filter *parent,
struct seccomp_filter *child)
@@ -526,7 +529,11 @@ static inline pid_t seccomp_can_sync_threads(void)
static inline void seccomp_filter_free(struct seccomp_filter *filter)
{
if (filter) {
- bpf_prog_destroy(filter->prog);
+ if (filter->prog->orig_prog &&
+ filter->prog->orig_prog->cache_entry)
+ cbpf_cache_put_prog(filter->prog);
+ else
+ bpf_prog_destroy(filter->prog);
kfree(filter);
}
}
@@ -702,7 +709,7 @@ static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
mutex_init(&sfilter->notify_lock);
ret = bpf_prog_create_from_user(&sfilter->prog, fprog,
seccomp_check_filter, save_orig,
- NULL);
+ &seccomp_cbpf_cache);
if (ret < 0) {
kfree(sfilter);
return ERR_PTR(ret);
--
2.43.0