* [PATCH bpf-next 0/3] bpf: Move IPIs out of pack_mutex and reduce IBPB flushes
@ 2026-12-02 15:13 Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 1/3] bpf: Move bpf_arch_text_invalidate() out of pack_mutex Xu Kuohai
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Xu Kuohai @ 2026-12-02 15:13 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis
From: Xu Kuohai <xukuohai@huawei.com>
BPF JIT memory is allocated through the BPF program pack allocator,
which uses a global pack_mutex to protect its packs. While holding this
lock, the allocator currently sends IPI to all CPUs on two paths: IBPB
flush on the x86 alloc path and CPU pipeline flush on the arm64 free
path. Because the caller can be preempted while waiting for the IPI
completion, the lock can be held for a long time, causing heavy lock
contention on busy systems.
This series moves both IPI out of pack_mutex and tracks dirty state per
chunk instead of per pack to reduce IBPB flushes.
Xu Kuohai (3):
bpf: Move bpf_arch_text_invalidate() out of pack_mutex
bpf: Change dirty tracking from packs to chunks
bpf: Move branch predictor flush out of pack_mutex
kernel/bpf/core.c | 153 ++++++++++++++++++++++++++++++++++++++++------
1 file changed, 133 insertions(+), 20 deletions(-)
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf-next 1/3] bpf: Move bpf_arch_text_invalidate() out of pack_mutex
2026-12-02 15:13 [PATCH bpf-next 0/3] bpf: Move IPIs out of pack_mutex and reduce IBPB flushes Xu Kuohai
@ 2026-12-02 15:13 ` Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 2/3] bpf: Change dirty tracking from packs to chunks Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 3/3] bpf: Move branch predictor flush out of pack_mutex Xu Kuohai
2 siblings, 0 replies; 4+ messages in thread
From: Xu Kuohai @ 2026-12-02 15:13 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis
From: Xu Kuohai <xukuohai@huawei.com>
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@huawei.com>
---
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf-next 2/3] bpf: Change dirty tracking from packs to chunks
2026-12-02 15:13 [PATCH bpf-next 0/3] bpf: Move IPIs out of pack_mutex and reduce IBPB flushes Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 1/3] bpf: Move bpf_arch_text_invalidate() out of pack_mutex Xu Kuohai
@ 2026-12-02 15:13 ` Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 3/3] bpf: Move branch predictor flush out of pack_mutex Xu Kuohai
2 siblings, 0 replies; 4+ messages in thread
From: Xu Kuohai @ 2026-12-02 15:13 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis
From: Xu Kuohai <xukuohai@huawei.com>
Currently, predictor dirty tracking in the pack allocator is per pack.
Even if all chunks allocated for a cBPF program are clean, a flush is
still triggered if they come from a dirty pack.
So switch dirty tracking from packs to chunks by maintaining a dirty
bit for each chunk. The dirty bits are initialized to 1 when the pack
is newly added, and cleared when memory is freed back to the pack while
pack->arch_flush_needed is false.
The pack->arch_flush_needed flag is cleared at flush time, so dirty bits
are not changed during the window between the flush and the next free.
But the stale dirty bits in this window are harmless. Even if a cBPF
filter is allocated a dirty chunk, no flush is triggered as long as
pack->arch_flush_needed is false.
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
kernel/bpf/core.c | 73 +++++++++++++++++++++++++++++++++++++++++------
1 file changed, 64 insertions(+), 9 deletions(-)
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index f43c512d3cc5..51818dd5ad50 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -1153,6 +1153,7 @@ struct bpf_prog_pack {
struct list_head list;
void *ptr;
bool arch_flush_needed;
+ unsigned long *dirty_map;
unsigned long bitmap[];
};
@@ -1201,16 +1202,19 @@ static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_ins
struct bpf_prog_pack *pack;
int err;
- pack = kzalloc_flex(*pack, bitmap, BITS_TO_LONGS(BPF_PROG_CHUNK_COUNT));
+ pack = kzalloc_flex(*pack, bitmap, 2 * BITS_TO_LONGS(BPF_PROG_CHUNK_COUNT));
if (!pack)
return NULL;
+ pack->dirty_map = pack->bitmap + BITS_TO_LONGS(BPF_PROG_CHUNK_COUNT);
pack->ptr = bpf_jit_alloc_exec(BPF_PROG_PACK_SIZE);
if (!pack->ptr)
goto out;
bitmap_zero(pack->bitmap, BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE);
- if (static_branch_unlikely(&bpf_pred_flush_enabled))
+ if (static_branch_unlikely(&bpf_pred_flush_enabled)) {
pack->arch_flush_needed = true;
+ bitmap_fill(pack->dirty_map, BPF_PROG_CHUNK_COUNT);
+ }
if (!bpf_jit_mem_is_rox()) {
bpf_fill_ill_insns(pack->ptr, BPF_PROG_PACK_SIZE);
set_vm_flush_reset_perms(pack->ptr);
@@ -1228,11 +1232,48 @@ static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_ins
return NULL;
}
+static bool bpf_pack_chunks_are_dirty(struct bpf_prog_pack *pack, unsigned long pos,
+ unsigned int nbits)
+{
+ return find_next_bit(pack->dirty_map, pos + nbits, pos) < pos + nbits;
+}
+
+static unsigned long
+bpf_pack_find_area(struct bpf_prog_pack *pack, unsigned long pos, u32 nbits, bool clean)
+{
+ unsigned long dirty_pos;
+
+ while (pos < BPF_PROG_CHUNK_COUNT) {
+ if (clean) {
+ /* Find the next window that starts at a clean chunk */
+ pos = find_next_zero_bit(pack->dirty_map, BPF_PROG_CHUNK_COUNT, pos);
+ } else {
+ /* Find the next window that holds a dirty chunk */
+ dirty_pos = find_next_bit(pack->dirty_map, BPF_PROG_CHUNK_COUNT, pos);
+ if (dirty_pos >= BPF_PROG_CHUNK_COUNT)
+ return BPF_PROG_CHUNK_COUNT;
+ pos = dirty_pos - nbits + 1;
+ }
+
+ pos = bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, pos, nbits, 0);
+ if (pos >= BPF_PROG_CHUNK_COUNT)
+ return BPF_PROG_CHUNK_COUNT;
+
+ dirty_pos = find_next_bit(pack->dirty_map, pos + nbits, pos);
+ if (clean == (dirty_pos >= pos + nbits))
+ return pos;
+
+ /* Resume at the first dirty chunk, or move to the following window */
+ pos = clean ? dirty_pos + 1 : pos + nbits;
+ }
+ return BPF_PROG_CHUNK_COUNT;
+}
+
void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool was_classic)
{
unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size);
struct bpf_prog_pack *pack, *fallback_pack = NULL;
- unsigned long pos, fallback_pos = 0;
+ unsigned long pos, dirty_pos, fallback_pos = 0;
void *ptr = NULL;
mutex_lock(&pack_mutex);
@@ -1270,16 +1311,23 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool
if (!static_branch_unlikely(&bpf_pred_flush_enabled))
goto found_free_area;
/*
- * cBPF reuse of a dirty pack triggers a flush, so prefer a
- * clean pack for cBPF. eBPF never flushes, so steer it to a
- * dirty pack and keep clean packs free for cBPF.
+ * cBPF reuse of a dirty chunk triggers a flush, so prefer
+ * clean chunks for cBPF. eBPF never flushes, so steer it to
+ * dirty chunks and keep clean chunks free for cBPF.
*/
- if (was_classic ^ pack->arch_flush_needed)
+ dirty_pos = find_next_bit(pack->dirty_map, pos + nbits, pos);
+ if (was_classic ^ (dirty_pos < pos + nbits))
goto found_free_area;
+
if (!fallback_pack) {
fallback_pack = pack;
fallback_pos = pos;
}
+
+ /* Find clean chunks for cBPF or dirty chunks for eBPF */
+ pos = bpf_pack_find_area(pack, dirty_pos, nbits, was_classic);
+ if (pos < BPF_PROG_CHUNK_COUNT)
+ goto found_free_area;
}
/* No preferred pack found */
@@ -1299,6 +1347,7 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool
/* Flush only for cBPF as it may contain a crafted gadget */
if (static_branch_unlikely(&bpf_pred_flush_enabled) &&
pack->arch_flush_needed &&
+ bpf_pack_chunks_are_dirty(pack, pos, nbits) &&
was_classic) {
struct bpf_prog_pack *p;
@@ -1351,8 +1400,14 @@ void bpf_prog_pack_free(void *ptr, u32 size)
bitmap_clear(pack->bitmap, pos, nbits);
- if (static_branch_unlikely(&bpf_pred_flush_enabled))
- pack->arch_flush_needed = true;
+ if (static_branch_unlikely(&bpf_pred_flush_enabled)) {
+ if (!pack->arch_flush_needed) {
+ /* Clean the stale dirty_map if the pack is clean */
+ bitmap_zero(pack->dirty_map, BPF_PROG_CHUNK_COUNT);
+ pack->arch_flush_needed = true;
+ }
+ bitmap_set(pack->dirty_map, pos, nbits);
+ }
if (bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
BPF_PROG_CHUNK_COUNT, 0) == 0) {
list_del(&pack->list);
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf-next 3/3] bpf: Move branch predictor flush out of pack_mutex
2026-12-02 15:13 [PATCH bpf-next 0/3] bpf: Move IPIs out of pack_mutex and reduce IBPB flushes Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 1/3] bpf: Move bpf_arch_text_invalidate() out of pack_mutex Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 2/3] bpf: Change dirty tracking from packs to chunks Xu Kuohai
@ 2026-12-02 15:13 ` Xu Kuohai
2 siblings, 0 replies; 4+ messages in thread
From: Xu Kuohai @ 2026-12-02 15:13 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis
From: Xu Kuohai <xukuohai@huawei.com>
bpf_prog_pack_alloc() flushes the branch predictors under pack_mutex.
On x86 the flush sends IPI to all CPUs to do IBPB, and waits for
each CPU to complete. This is a heavy operation inside a mutex, so
move it out of the pack_mutex.
With the flush moved out, packs can become dirty while it runs unlocked.
A flush therefore cannot blindly clear dirty flags. Instead, track a
global dirty and flush generation pair:
- Each time a pack is marked dirty, increment the dirty generation and
assign it to the pack.
- When a flush is done, set the flush generation to the dirty generation
observed before the flush.
This removes the need for the flag arch_flush_needed. A pack is dirty
if its dirty generation is greater than the flush generation.
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
kernel/bpf/core.c | 71 ++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 61 insertions(+), 10 deletions(-)
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 51818dd5ad50..07864d56e050 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -1152,7 +1152,7 @@ int bpf_jit_add_poke_descriptor(struct bpf_prog *prog,
struct bpf_prog_pack {
struct list_head list;
void *ptr;
- bool arch_flush_needed;
+ u64 dirty_seq;
unsigned long *dirty_map;
unsigned long bitmap[];
};
@@ -1176,6 +1176,23 @@ DEFINE_STATIC_KEY_FALSE(bpf_pred_flush_enabled);
static DEFINE_MUTEX(pack_mutex);
static LIST_HEAD(pack_list);
+/*
+ * The dirty generation counter. Every event that makes a pack dirty counts
+ * itself here.
+ */
+static atomic64_t pack_dirty_seq;
+
+/*
+ * The flush generation counter, published without a lock. It is set at
+ * flush time, to the dirty generation right before the flush.
+ */
+static atomic64_t pack_flush_seq;
+
+static bool bpf_prog_pack_is_dirty(const struct bpf_prog_pack *pack)
+{
+ return (s64)(pack->dirty_seq - atomic64_read(&pack_flush_seq)) > 0;
+}
+
/* PMD_SIZE is not available in some special config, e.g. ARCH=arm with
* CONFIG_MMU=n. Use PAGE_SIZE in these cases.
*/
@@ -1212,7 +1229,7 @@ static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_ins
bitmap_zero(pack->bitmap, BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE);
if (static_branch_unlikely(&bpf_pred_flush_enabled)) {
- pack->arch_flush_needed = true;
+ pack->dirty_seq = atomic64_inc_return(&pack_dirty_seq);
bitmap_fill(pack->dirty_map, BPF_PROG_CHUNK_COUNT);
}
if (!bpf_jit_mem_is_rox()) {
@@ -1232,6 +1249,40 @@ static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_ins
return NULL;
}
+static DEFINE_MUTEX(pack_flush_mutex);
+
+static void bpf_prog_pack_flush(u64 count)
+{
+ u64 seq;
+
+ /*
+ * Before getting the lock, check if someone else already performed
+ * the flush. The acquire pairs with the xchg barrier of the publish.
+ */
+ if ((s64)(atomic64_read(&pack_flush_seq) - count) >= 0) {
+ smp_acquire__after_ctrl_dep();
+ return;
+ }
+
+ mutex_lock(&pack_flush_mutex);
+
+ /* A flush may have run before we got the mutex */
+ if ((s64)(atomic64_read(&pack_flush_seq) - count) >= 0)
+ goto out;
+
+ seq = atomic64_read(&pack_dirty_seq);
+
+ /* Read the dirty generation before the broadcast starts */
+ smp_mb();
+
+ static_call_cond(bpf_arch_pred_flush)();
+
+ /* xchg is a full barrier. It orders the flush before the publish. */
+ atomic64_xchg(&pack_flush_seq, seq);
+out:
+ mutex_unlock(&pack_flush_mutex);
+}
+
static bool bpf_pack_chunks_are_dirty(struct bpf_prog_pack *pack, unsigned long pos,
unsigned int nbits)
{
@@ -1274,6 +1325,8 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool
unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size);
struct bpf_prog_pack *pack, *fallback_pack = NULL;
unsigned long pos, dirty_pos, fallback_pos = 0;
+ bool flush = false;
+ u64 count = 0;
void *ptr = NULL;
mutex_lock(&pack_mutex);
@@ -1346,20 +1399,18 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool
found_free_area:
/* Flush only for cBPF as it may contain a crafted gadget */
if (static_branch_unlikely(&bpf_pred_flush_enabled) &&
- pack->arch_flush_needed &&
bpf_pack_chunks_are_dirty(pack, pos, nbits) &&
was_classic) {
- struct bpf_prog_pack *p;
-
- static_call_cond(bpf_arch_pred_flush)();
- list_for_each_entry(p, &pack_list, list)
- p->arch_flush_needed = false;
+ flush = true;
+ count = pack->dirty_seq;
}
bitmap_set(pack->bitmap, pos, nbits);
ptr = (void *)(pack->ptr) + (pos << BPF_PROG_CHUNK_SHIFT);
out:
mutex_unlock(&pack_mutex);
+ if (flush)
+ bpf_prog_pack_flush(count);
return ptr;
}
@@ -1401,12 +1452,12 @@ void bpf_prog_pack_free(void *ptr, u32 size)
bitmap_clear(pack->bitmap, pos, nbits);
if (static_branch_unlikely(&bpf_pred_flush_enabled)) {
- if (!pack->arch_flush_needed) {
+ if (!bpf_prog_pack_is_dirty(pack)) {
/* Clean the stale dirty_map if the pack is clean */
bitmap_zero(pack->dirty_map, BPF_PROG_CHUNK_COUNT);
- pack->arch_flush_needed = true;
}
bitmap_set(pack->dirty_map, pos, nbits);
+ pack->dirty_seq = atomic64_inc_return(&pack_dirty_seq);
}
if (bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
BPF_PROG_CHUNK_COUNT, 0) == 0) {
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-02 15:41 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 15:13 [PATCH bpf-next 0/3] bpf: Move IPIs out of pack_mutex and reduce IBPB flushes Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 1/3] bpf: Move bpf_arch_text_invalidate() out of pack_mutex Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 2/3] bpf: Change dirty tracking from packs to chunks Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 3/3] bpf: Move branch predictor flush out of pack_mutex Xu Kuohai
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®