Re: [PATCH bpf-next 1/3] bpf: Move bpf_arch_text_invalidate() out of pack_mutex

From: Alexei Starovoitov

Date: Wed Dec 02 2026 - 22:39:07 EST


On Wed, Dec 02, 2026 at 07:13 AM Xu Kuohai <xukuohai@xxxxxxxxxxxxxxx> wrote:
> if (WARN_ONCE(!pack, "bpf_prog_pack bug\n"))
> goto out;
> + mutex_unlock(&pack_mutex);

> + mutex_lock(&pack_mutex);

can we invalidate before the first mutex_lock() ?
Then a free takes pack_mutex once.
Puranjay posted the same fix in that form, but it slipped through the cracks :(
https://lore.kernel.org/bpf/20261103142856.463063-3-puranjay@xxxxxxxxxx/

On top of the series, core.c compiles on x86-64, but I didn't run it:

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 1ecf398..2a21470 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -1454,12 +1454,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;
@@ -1469,16 +1473,6 @@ void bpf_prog_pack_free(void *ptr, u32 size)

if (WARN_ONCE(!pack, "bpf_prog_pack bug\n"))
goto out;
- mutex_unlock(&pack_mutex);
-
- /*
- * The bits of this chunk are still set, so nobody can take the chunk
- * or free the pack, no need to hold the mutex.
- */
- WARN_ONCE(bpf_arch_text_invalidate(ptr, size),
- "bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n");
-
- mutex_lock(&pack_mutex);

nbits = BPF_PROG_SIZE_TO_NBITS(size);
pos = ((unsigned long)ptr - (unsigned long)pack->ptr) >> BPF_PROG_CHUNK_SHIFT;

This fix doesn't depend on patches 2 and 3.