* [PATCH bpf-next v2 1/2] bpf: Invalidate freed prog pack memory without holding pack_mutex
2026-12-03 14:09 [PATCH bpf-next v2 0/2] bpf: Move IPIs out of pack_mutex and reduce IBPB flushes Xu Kuohai
@ 2026-12-03 14:09 ` Xu Kuohai
2026-12-03 14:09 ` [PATCH bpf-next v2 2/2] bpf: Reduce branch predictor flush and move it out of pack_mutex Xu Kuohai
1 sibling, 0 replies; 5+ messages in thread
From: Xu Kuohai @ 2026-12-03 14:09 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,
Puranjay Mohan, Pawan Gupta
From: Puranjay Mohan <puranjay@kernel.org>
bpf_prog_pack_free() calls bpf_arch_text_invalidate() with pack_mutex
held. On arm64 it interrupts all other CPUs and waits for them:
bpf_prog_free_deferred
bpf_prog_free_images
bpf_jit_free
bpf_jit_binary_pack_free
bpf_prog_pack_free
bpf_arch_text_invalidate
aarch64_insn_set
__text_poke
flush_icache_range
kick_all_cpus_sync
When many progs are freed at once, every prog load waits in
bpf_prog_pack_alloc() for all of these broadcasts. On arm64 hosts with
many CPUs this showed up as hung task reports with sk_attach_filter()
blocked on pack_mutex for minutes.
pack_mutex protects pack_list, the bitmaps and arch_flush_needed, not
the text. Until bitmap_clear(), the bits of the range are set, so it
cannot be allocated again and the pack cannot be freed. Invalidate it
before taking pack_mutex. A ptr that is in no pack, which only a caller
bug can pass, is now written to before the lookup warns about it.
Tested in arm64 KVM guests with 16 and 64 cpus on a 72 cpu host. cpus/2
processes attach a cBPF filter to 256 sockets and close them in a loop
for 20 sec. One more process attaches a filter every msec. Median of 5
runs.
Sockets attached and closed in 20 sec, in millions (higher is better):
before after
16 cpus 1.22 3.79
64 cpus 0.42 3.38
setsockopt(SO_ATTACH_FILTER) of the process that attaches every msec,
in msec (lower is better):
16 cpus 64 cpus
before after before after
avg 1.32 0.27 7.21 0.56
p99 5.39 5.52 79.7 9.89
max 447 159 1598 253
Fixes: 1dad391daef1 ("bpf, arm64: use bpf_prog_pack for memory management")
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
kernel/bpf/core.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 8230dcdf14f4..4c908133011d 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -1354,12 +1354,16 @@ void bpf_prog_pack_free(void *ptr, u32 size)
unsigned int nbits;
unsigned long pos;
- mutex_lock(&pack_mutex);
if (size > BPF_PROG_PACK_SIZE) {
bpf_jit_free_exec(ptr);
- goto out;
+ return;
}
+ /* The caller owns the chunks until bitmap_clear() */
+ WARN_ONCE(bpf_arch_text_invalidate(ptr, size),
+ "bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n");
+
+ mutex_lock(&pack_mutex);
list_for_each_entry(tmp, &pack_list, list) {
if (ptr >= tmp->ptr && (tmp->ptr + BPF_PROG_PACK_SIZE) > ptr) {
pack = tmp;
@@ -1373,9 +1377,6 @@ void bpf_prog_pack_free(void *ptr, u32 size)
nbits = BPF_PROG_SIZE_TO_NBITS(size);
pos = ((unsigned long)ptr - (unsigned long)pack->ptr) >> BPF_PROG_CHUNK_SHIFT;
- WARN_ONCE(bpf_arch_text_invalidate(ptr, size),
- "bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n");
-
bitmap_clear(pack->bitmap, pos, nbits);
if (static_branch_unlikely(&bpf_pred_flush_enabled))
--
2.47.3
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH bpf-next v2 2/2] bpf: Reduce branch predictor flush and move it out of pack_mutex
2026-12-03 14:09 [PATCH bpf-next v2 0/2] bpf: Move IPIs out of pack_mutex and reduce IBPB flushes Xu Kuohai
2026-12-03 14:09 ` [PATCH bpf-next v2 1/2] bpf: Invalidate freed prog pack memory without holding pack_mutex Xu Kuohai
@ 2026-12-03 14:09 ` Xu Kuohai
2026-12-03 14:55 ` sashiko-bot
2026-12-03 15:15 ` bot+bpf-ci
1 sibling, 2 replies; 5+ messages in thread
From: Xu Kuohai @ 2026-12-03 14:09 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,
Puranjay Mohan, Pawan Gupta
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.
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@gmail.com>
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
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
^ permalink raw reply related [flat|nested] 5+ messages in thread