From: sashiko-bot@kernel.org
To: "Xu Kuohai" <xukuohai@huaweicloud.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 3/3] bpf: Move branch predictor flush out of pack_mutex
Date: Wed, 02 Dec 2026 15:57:35 +0000 [thread overview]
Message-ID: <sashiko-outbox-246166@kernel.org> (raw)
In-Reply-To: <26a5c907d23cecb956f46f4bb07d229e85e6b907.1796223449.git.xukuohai@huawei.com>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] bpf: stale dirty_map read in bpf_prog_pack_alloc() on clean
packs
--
commit 34ea7e29e7698a27be8a5df803db4e9f53918487
Author: Xu Kuohai <xukuohai@huawei.com>
bpf: Move branch predictor flush out of pack_mutex
This commit moves branch predictor flushing out of pack_mutex by replacing
pack->arch_flush_needed with pack_dirty_seq and pack_flush_seq generation
counters, serializing flushes under pack_flush_mutex.
> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> index 920c8cdaf2679..1ecf39885bfb0 100644
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
[ ... ]
> @@ -1380,20 +1433,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;
> }
>
> @@ -1435,12 +1486,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);
> }
[Severity: Medium]
Does bpf_prog_pack_alloc() read stale bits from pack->dirty_map when a pack
has already been flushed?
bpf_prog_pack_flush() advances pack_flush_seq without clearing
pack->dirty_map, and stale bits are only zeroed lazily in
bpf_prog_pack_free() when a program is later freed from that pack while
!bpf_prog_pack_is_dirty(pack). Until then, the pack search loop in
bpf_prog_pack_alloc() inspects pack->dirty_map without checking
bpf_prog_pack_is_dirty(pack):
kernel/bpf/core.c:bpf_prog_pack_alloc() {
...
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;
...
}
For example, alloc_new_pack() sets all bits in pack->dirty_map to 1. Once
the first cBPF allocation from a new pack triggers a flush, that pack is
clean (!bpf_prog_pack_is_dirty(pack)), yet its dirty_map remains all 1s
until a program in that pack is freed. If an older pack earlier in
pack_list has a chunk freed in the meantime (making it genuinely dirty), a
subsequent cBPF allocation records the dirty older pack as fallback_pack,
scans all 32,768 stale bits of the clean pack's dirty_map without finding a
0 bit, falls back to the dirty pack, and triggers an unnecessary broadcast
IBPB flush. Similarly, eBPF allocations mistake stale dirty_map bits in
clean packs for dirty chunks and place eBPF programs into clean packs.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796223449.git.xukuohai@huawei.com?part=3
prev parent reply other threads:[~2026-12-02 15:57 UTC|newest]
Thread overview: 6+ 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-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-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 [this message]
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=sashiko-outbox-246166@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=xukuohai@huaweicloud.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox