Re: [PATCH bpf-next 3/3] bpf: Move branch predictor flush out of pack_mutex

From: Xu Kuohai

Date: Thu Dec 03 2026 - 06:06:03 EST


On 12/3/2026 11:40 AM, Alexei Starovoitov wrote:
On Wed, Dec 02, 2026 at 07:13 AM Xu Kuohai <xukuohai@xxxxxxxxxxxxxxx> wrote:
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:

- 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);

This is quite heavy and it takes back what patch 2 gains.
One free during the flush keeps the whole dirty_map of the pack.
Say pack_flush_seq is 4:

cBPF alloc free in the same pack
count = pack->dirty_seq = 5
seq = pack_dirty_seq = 5
IBPB on all CPUs is_dirty: 5 > 4, no bitmap_zero()
pack->dirty_seq = 6
pack_flush_seq = 5
is_dirty: 6 > 5, no bitmap_zero()
pack->dirty_seq = 7
next alloc, no clean area
count = 7, IBPB again

The wait for IPIs is preemptible, so the window can be long.

can we set dirty bits in alloc and clear them before IBPB ?
cBPF allocs hold pack_flush_mutex, so they see clean bits only after IBPB.
Frees and eBPF allocs don't wait for it.
No need for generations, barriers, bpf_pack_find_area(), fallback_pack.
A small cBPF alloc has to wait for a flush in flight though.
eBPF is back to first fit, as before commit b72e29e0f7ee.

Against bpf-next, in place of patches 2 and 3. core.c builds, not booted:

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 3653a2e..27cf96c 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,16 @@ out:
void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool was_classic)
{
+ /* Flush only for cBPF as it may contain a crafted gadget */
+ bool need_clean = was_classic && static_branch_unlikely(&bpf_pred_flush_enabled);
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;
+ if (need_clean)
+ mutex_lock(&pack_flush_mutex);
mutex_lock(&pack_mutex);
if (size > BPF_PROG_PACK_SIZE) {
/*
@@ -1277,7 +1289,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 +1307,20 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool
}
goto out;
}
+ if (need_clean) {
+ 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;
+ }
+ }
+
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))
+ if (pos < BPF_PROG_CHUNK_COUNT)
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;
- }
- }
-
- /* No preferred pack found */
- if (fallback_pack) {
- pack = fallback_pack;
- pos = fallback_pos;
- goto found_free_area;
}
pack = alloc_new_pack(bpf_fill_ill_insns);
@@ -1330,21 +1330,23 @@ 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 */
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;
}
@@ -1378,8 +1380,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);


Great idea, this is very clean and elegant, thanks for the suggestion!
I'll switch to it in v2.

pw-bot: cr