From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Xu Kuohai" <xukuohai@huaweicloud.com>, <bpf@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Cc: "Daniel Borkmann" <daniel@iogearbox.net>,
"John Fastabend" <john.fastabend@gmail.com>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Eduard Zingerman" <eddyz87@gmail.com>,
"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Song Liu" <song@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Jiri Olsa" <jolsa@kernel.org>,
"Emil Tsalapatis" <emil@etsalapatis.com>
Subject: Re: [PATCH bpf-next 3/3] bpf: Move branch predictor flush out of pack_mutex
Date: Thu, 03 Dec 2026 03:40:40 +0000 [thread overview]
Message-ID: <DNASAX68PUUX.3L29FR0XD30E4@gmail.com> (raw)
In-Reply-To: <26a5c907d23cecb956f46f4bb07d229e85e6b907.1796223449.git.xukuohai@huawei.com>
On Wed, Dec 02, 2026 at 07:13 AM Xu Kuohai <xukuohai@huaweicloud.com> 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);
pw-bot: cr
next prev parent reply other threads:[~2026-12-03 3:40 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 15:13 [PATCH bpf-next 0/3] bpf: Move IPIs out of pack_mutex and reduce IBPB flushes Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 1/3] bpf: Move bpf_arch_text_invalidate() out of pack_mutex Xu Kuohai
2026-12-03 3:38 ` Alexei Starovoitov
2026-12-03 7:16 ` Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 2/3] bpf: Change dirty tracking from packs to chunks Xu Kuohai
2026-12-02 15:55 ` sashiko-bot
2026-12-03 7:21 ` Xu Kuohai
2026-12-02 15:13 ` [PATCH bpf-next 3/3] bpf: Move branch predictor flush out of pack_mutex Xu Kuohai
2026-12-02 15:57 ` sashiko-bot
2026-12-03 7:23 ` Xu Kuohai
2026-12-03 3:40 ` Alexei Starovoitov [this message]
2026-12-03 11:05 ` Xu Kuohai
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DNASAX68PUUX.3L29FR0XD30E4@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=memxor@gmail.com \
--cc=song@kernel.org \
--cc=xukuohai@huaweicloud.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.