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

From: Xu Kuohai

Date: Wed Dec 02 2026 - 10:41:21 EST


From: Xu Kuohai <xukuohai@xxxxxxxxxx>

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@xxxxxxxxxx>
---
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