[PATCH bpf-next 1/3] bpf: Move bpf_arch_text_invalidate() out of pack_mutex
From: Xu Kuohai
Date: Wed Dec 02 2026 - 10:41:22 EST
From: Xu Kuohai <xukuohai@xxxxxxxxxx>
bpf_prog_pack_free() holds pack_mutex across bpf_arch_text_invalidate().
On arm64 the invalidation sends an IPI to every CPU, and waits for each
of them to complete. The wait is preemptible. When the freeing task is
preempted there, it keeps the lock, and the waiters, in either the free
or the alloc path, wait until it is scheduled back and releases the
mutex. This can cause a significant lock convoy on busy systems.
Since the bits of the chunk are still set at invalidation time,
nobody can take the chunk, so it is safe to move the invalidation
out of the mutex.
On an arm64 host busy loading seccomp filters for Firecracker
sandboxes, moving the invalidation outside the lock reduced the
p99 startup latency from 2.79 s to 170 ms.
Signed-off-by: Xu Kuohai <xukuohai@xxxxxxxxxx>
---
kernel/bpf/core.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 32a267656c0e..f43c512d3cc5 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -1335,13 +1335,20 @@ void bpf_prog_pack_free(void *ptr, u32 size)
if (WARN_ONCE(!pack, "bpf_prog_pack bug\n"))
goto out;
+ mutex_unlock(&pack_mutex);
- nbits = BPF_PROG_SIZE_TO_NBITS(size);
- pos = ((unsigned long)ptr - (unsigned long)pack->ptr) >> BPF_PROG_CHUNK_SHIFT;
-
+ /*
+ * The bits of this chunk are still set, so nobody can take the chunk
+ * or free the pack, no need to hold the mutex.
+ */
WARN_ONCE(bpf_arch_text_invalidate(ptr, size),
"bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n");
+ mutex_lock(&pack_mutex);
+
+ nbits = BPF_PROG_SIZE_TO_NBITS(size);
+ pos = ((unsigned long)ptr - (unsigned long)pack->ptr) >> BPF_PROG_CHUNK_SHIFT;
+
bitmap_clear(pack->bitmap, pos, nbits);
if (static_branch_unlikely(&bpf_pred_flush_enabled))
--
2.47.3