From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796227056; b=LtAuAwHRPt6iE2hdqS2V/ycQkzERLxk6gTl/uIvI3AnXQhPUxs4rkqaUtAoDCOKkN1no sKRqEH+gksaSb/3owGLxVOYWT8VVJWTr6Khu3pyGrI97AnxTrskDKEsA1NSgxFMz6mRMs 50oc0jKF0iy30bmmnfmkIva5Hs/KDwY4NNw/Kipr3dr0rVjp9X7LW914tWc8tGdRu0pY4 Bh94lJ+H4bEOnLiReIUGuh2vpbM9hGj9/SU+Xy74wGoVVUWOmcktxRiHoITM2BRtlHVNA fbJOjpqYm20E3aPJI9HQD0oDTEKFN8GGIaJBEYUW9qCwz0VWBuVDDR5chST7QlfKI+Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796227056; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=Og/hnEM+hOkO+WexC6Jnu4PGWZ5z5jDJYySoFQ+t3KU=; b=JSNmFNivYwNrVTtCijt5tdXmbhoTmYfmmqXFGLGPxQV1mJ0w9/d55lSztZ4TdE7f4NT4 H5VwQG4gIHub3VM2+vNyPPskoS8WXfzTh6ysPITnBb5jKyyNm6uWCubpl9xTbCj4b/eUs H+ibGh5i8vQJC9wTq/m9859GXS2h/HhTzPMoZ3FKO2gO6dMG4+654edcGa6RZDA/A6tru aaA6zqWqsoQUcncz9kIP/KKWkyR+OghTfecxfWRkN0ivQZ9c6mZ/e6wm8M7XqGh5EbJrr ulYFanAYhlAN9Gmq0L9oVVwJb56bwRK2xcEegcXjJg2J/1YPQF5g62shydIiH0/sE0Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Bau2p6vQ; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4961606A31A for ; Wed, 2 Dec 2026 15:57:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Bau2p6vQ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3FF941F0092F; Wed, 2 Dec 2026 15:57:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796227056; bh=Og/hnEM+hOkO+WexC6Jnu4PGWZ5z5jDJYySoFQ+t3KU=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=Bau2p6vQ3uFlJv9+q5G/9RYsbUKVh6q1HrZK+bcdyyCG0e6xs1QG+iZZ1NAaDt9N8 9vswDdgrxK4kkT8Eh55a/LqwiNyb5WbEfVbIZLHQc9+qoccY5RKG+LAgO53KB1dCXk mIulbQLgnBxLQgg/5MxZ9PCSLBabRWhmzCjj8r4qRe0ecBkMa13zuAHJZGM7KydR3R +W56jpWg3WHDSOdDKo4b3Nw35kXMLFUpOonENnYpsQFIUoBSPFbAs47n2BD2zZ9Vb7 ADR9yjPcHNtkZJmjGwX2du3fXFQ50hyI9oH7hRARGhYBAwGyLK5dgXm4gxIsPor5T7 P3dtr7F5wjWZw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH bpf-next 3/3] bpf: Move branch predictor flush out of pack_mutex Reply-To: sashiko-reviews@lists.linux.dev To: "Xu Kuohai" , bpf@vger.kernel.org In-Reply-To: <26a5c907d23cecb956f46f4bb07d229e85e6b907.1796223449.git.xukuohai@huawei.com> References: <26a5c907d23cecb956f46f4bb07d229e85e6b907.1796223449.git.xukuohai@huawei.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 15:57:35 +0000 X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 =3D false; > + flush =3D true; > + count =3D pack->dirty_seq; > } > bitmap_set(pack->bitmap, pos, nbits); > ptr =3D (void *)(pack->ptr) + (pos << BPF_PROG_CHUNK_SHIFT); > =20 > out: > mutex_unlock(&pack_mutex); > + if (flush) > + bpf_prog_pack_flush(count); > return ptr; > } > =20 > @@ -1435,12 +1486,12 @@ void bpf_prog_pack_free(void *ptr, u32 size) > bitmap_clear(pack->bitmap, pos, nbits); > =20 > 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 =3D true; > } > bitmap_set(pack->dirty_map, pos, nbits); > + pack->dirty_seq =3D 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 =3D 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 =3D pack; fallback_pos =3D pos; } /* Find clean chunks for cBPF or dirty chunks for eBPF */ pos =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/cover.1796223449.gi= t.xukuohai@huawei.com?part=3D3