[PATCH bpf-next 2/3] bpf: Change dirty tracking from packs to chunks

From: Xu Kuohai

Date: Wed Dec 02 2026 - 10:41:17 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.

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