[PATCH bpf-next v2 2/2] bpf: Reduce branch predictor flush and move it out of pack_mutex

From: Xu Kuohai

Date: Thu Dec 03 2026 - 09:36:53 EST


From: Xu Kuohai <xukuohai@xxxxxxxxxx>

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.

Switch dirty tracking from packs to chunks by maintaining a dirty bit
for each chunk. The basic idea is that once a chunk has been used, it
remains dirty until it is freed and a subsequent predictor flush
is performed. The dirty bit is set when a new pack is allocated or
when a chunk is allocated to a BPF program. At predictor flush time,
dirty bits are cleared only for free chunks.

Therefore, pack->dirty_map is a superset of pack->bitmap. Every bit
set in pack->bitmap is also set in pack->dirty_map, while bits not
set in pack->dirty_map are also clear in pack->bitmap. Thus, zero
bits in the dirty map indicate clean and free chunks. This makes it
easy to find clean and free chunks for cBPF programs. eBPF allocations
are back to first fit, they no longer steer to dirty packs, as before
commit b72e29e0f7ee ("bpf: Prefer dirty packs for eBPF allocations").

Additionally, bpf_prog_pack_alloc() flushes branch predictors under
pack_mutex. On x86, this involves sending IPIs to all CPUs to perform
IBPB and waiting for them to complete. This is expensive, so move the
flush out of the global pack_mutex.

Suggested-by: Alexei Starovoitov <alexei.starovoitov@xxxxxxxxx>
Signed-off-by: Xu Kuohai <xukuohai@xxxxxxxxxx>
---
kernel/bpf/core.c | 90 ++++++++++++++++++++++++++---------------------
1 file changed, 50 insertions(+), 40 deletions(-)

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 4c908133011d..143fa134fea5 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -1186,7 +1186,8 @@ int bpf_jit_add_poke_descriptor(struct bpf_prog *prog,
struct bpf_prog_pack {
struct list_head list;
void *ptr;
- bool arch_flush_needed;
+ /* Chunks that were in use since the last predictor flush */
+ unsigned long *dirty_map;
unsigned long bitmap[];
};

@@ -1209,6 +1210,12 @@ DEFINE_STATIC_KEY_FALSE(bpf_pred_flush_enabled);
static DEFINE_MUTEX(pack_mutex);
static LIST_HEAD(pack_list);

+/*
+ * Held by the readers of dirty_map. The map is cleaned before the flush,
+ * so they have to wait until the flush is done. Nests outside of pack_mutex.
+ */
+static DEFINE_MUTEX(pack_flush_mutex);
+
/* PMD_SIZE is not available in some special config, e.g. ARCH=arm with
* CONFIG_MMU=n. Use PAGE_SIZE in these cases.
*/
@@ -1235,16 +1242,16 @@ 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);
+ bitmap_fill(pack->dirty_map, BPF_PROG_CHUNK_COUNT);

- if (static_branch_unlikely(&bpf_pred_flush_enabled))
- pack->arch_flush_needed = true;
if (!bpf_jit_mem_is_rox()) {
bpf_fill_ill_insns(pack->ptr, BPF_PROG_PACK_SIZE);
set_vm_flush_reset_perms(pack->ptr);
@@ -1264,11 +1271,18 @@ static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_ins

void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool was_classic)
{
+ bool need_clean;
unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size);
- struct bpf_prog_pack *pack, *fallback_pack = NULL;
- unsigned long pos, fallback_pos = 0;
+ struct bpf_prog_pack *pack, *p;
+ bool flush = false;
+ unsigned long pos;
void *ptr = NULL;

+ /* Flush only for cBPF as it may contain a crafted gadget */
+ need_clean = was_classic && static_branch_unlikely(&bpf_pred_flush_enabled);
+ if (need_clean)
+ mutex_lock(&pack_flush_mutex);
+
mutex_lock(&pack_mutex);
if (size > BPF_PROG_PACK_SIZE) {
/*
@@ -1277,7 +1291,7 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool
* safe because cBPF programs (the unprivileged attack surface)
* are bounded well below a pack size.
*/
- if (was_classic && static_branch_unlikely(&bpf_pred_flush_enabled))
+ if (need_clean)
pr_warn_once("BPF: Predictors not flushed for allocations greater than BPF_PROG_PACK_SIZE\n");
size = round_up(size, PAGE_SIZE);
ptr = bpf_jit_alloc_exec(size);
@@ -1295,32 +1309,21 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool
}
goto out;
}
- list_for_each_entry(pack, &pack_list, list) {
- pos = bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
- nbits, 0);
- if (pos >= BPF_PROG_CHUNK_COUNT)
- continue;
- /* Flush not enabled, use any pack */
- 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.
- */
- if (was_classic ^ pack->arch_flush_needed)
- goto found_free_area;
- if (!fallback_pack) {
- fallback_pack = pack;
- fallback_pos = pos;
+
+ if (need_clean) {
+ /* Prefer a clean area over a flush */
+ list_for_each_entry(pack, &pack_list, list) {
+ pos = bitmap_find_next_zero_area(pack->dirty_map, BPF_PROG_CHUNK_COUNT, 0,
+ nbits, 0);
+ if (pos < BPF_PROG_CHUNK_COUNT)
+ goto found_clean_area;
}
}

- /* No preferred pack found */
- if (fallback_pack) {
- pack = fallback_pack;
- pos = fallback_pos;
- goto found_free_area;
+ list_for_each_entry(pack, &pack_list, list) {
+ pos = bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0, nbits, 0);
+ if (pos < BPF_PROG_CHUNK_COUNT)
+ goto found_free_area;
}

pack = alloc_new_pack(bpf_fill_ill_insns);
@@ -1330,21 +1333,30 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool
pos = 0;

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 &&
- was_classic) {
- struct bpf_prog_pack *p;
-
- static_call_cond(bpf_arch_pred_flush)();
+ if (need_clean) {
+ /*
+ * The flush below covers all chunks that are free now. Chunks in
+ * use stay dirty, the rest become clean.
+ */
list_for_each_entry(p, &pack_list, list)
- p->arch_flush_needed = false;
+ bitmap_copy(p->dirty_map, p->bitmap, BPF_PROG_CHUNK_COUNT);
+ flush = true;
}
+
+found_clean_area:
+ bitmap_set(pack->dirty_map, pos, nbits);
bitmap_set(pack->bitmap, pos, nbits);
ptr = (void *)(pack->ptr) + (pos << BPF_PROG_CHUNK_SHIFT);

out:
mutex_unlock(&pack_mutex);
+
+ if (flush)
+ static_call_cond(bpf_arch_pred_flush)();
+
+ if (need_clean)
+ mutex_unlock(&pack_flush_mutex);
+
return ptr;
}

@@ -1379,8 +1391,6 @@ 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 (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