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=1796384891; b=17uSzdxF99pAzFl9ogW2TMPceAu8afOln2mm98/D05Jm+ZSjJTVmyKCl9ai/oceOkTEa Grogoiort7O3m419xrWQBzwT7Z6j9DSvq8Cbq4+LbyLp9ICvvUkK055XAp55yNaN1Bj/l +IcVvyrYOEjey7qAIRufZ9mS6vTWB9DL5n6rmK3Djw7CdZy5yDzMKcN/SpHeqADbsW1kJ AAdPeekp3Es64f9KsY8wF+pyivNNEEFHvVx4g6sC4wt+TrzUcDJFCi+Tm7B23mt5YtPy1 dKA6eveKkfdPGsNbBqjOzhC2z+sYu/5+qmaC/gQivmj/a7zy0HH8Oe6MSHzoiBcjB2w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796384891; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=8y0z/ChtBFi7lJ4TUqllaQpeq65nTVGgfd1dijAZwMU=; b=HqACQh0pHeiSZnjXkQ6WX4WxxcLzYWalVPr3pOYaiAZyoAW/8FQA2kCflNMrrNg2q+c9 6zd44S3QX8COaEvBycuYItBe7eS+n/k7OgfOQurkA7XvMxOv45wk4FtqPUBoq6jWV51Oj GjFhGtKc5mOQali6Su4wNmOZn29lp4V7rTFBavV1dLFSrF3CYzUmSfdJrAQPU/1Uh3S2X uM9kT+5Arr9smgLHnMH9JwplSDMpj30b/idms9447pBz090nszwibr6ZTXTc5hgeLsZLL /QMJonnpedxZcZ62JsT4jyCfvN8ufBzXyekTwtE2bZGawYobXoCDbzqVzzt5kjTN9zA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=BWxSf4+4; 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 C9F28606A322; Fri, 4 Dec 2026 11:48:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=BWxSf4+4 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C6AF1F00897; Fri, 4 Dec 2026 11:48:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796384891; bh=8y0z/ChtBFi7lJ4TUqllaQpeq65nTVGgfd1dijAZwMU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=BWxSf4+4DRZcp0XGXSesxj43BWDn+mX1jn3+rqzBg53fchgOaGH21yF4Pf5SHQN57 Oeirr23sQBRkhLcst1uCf76UBV4hm/j0bxLT+hHtTmv4SeCOxSP4wdJql2A8eGKpRi D8JtGChp5E91gkjGAhuZjXbI3+W0oblictKIRs/Q= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Yuan Chen , Jiri Olsa , Kumar Kartikeya Dwivedi , Sasha Levin Subject: [PATCH 6.6 0338/1167] bpf: Keep target extended until its last freplace link detaches Date: Fri, 4 Dec 2026 18:56:05 +0900 Message-ID: <20261204100710.799574290@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Yuan Chen [ Upstream commit c43c348a84460b003fa757dd43d8c89848ae220d ] The is_extended / prog_array_member_cnt protocol introduced by commit d6083f040d5d ("bpf: Prevent tailcall infinite loop caused by freplace") keeps a prog extended by a freplace program out of prog_array maps, and vice versa: once a tail call re-enters an extended subprogram, its tail_call_cnt resets on every execution and the loop never terminates. But is_extended is a plain boolean, while one target prog can carry several freplace links at the same time, one on its entry and one on a global subprogram. __bpf_trampoline_unlink_prog() cleared is_extended whenever *any* freplace link detached, so detaching one of two links re-armed the unbounded loop through the remaining one. Replace the is_extended boolean with a count of the freplace links attached to each target prog, so the target stays extended until its last link detaches. Also rename bpf_freplace_check_tgt_prog() to bpf_freplace_link_tgt_prog(), as the helper has never been a pure check: it reserves the target prog on success. Fixes: d6083f040d5d ("bpf: Prevent tailcall infinite loop caused by freplace") Signed-off-by: Yuan Chen Acked-by: Jiri Olsa Link: https://lore.kernel.org/bpf/20260924023737.1140521-2-chenyuan_fl@163.com Signed-off-by: Kumar Kartikeya Dwivedi [ sashal: Replaced bool is_extended with u16 freplace_link_cnt in the same struct slot, and adjusted hunk context for the older bpf_tramp_link/link->link.prog naming. ] Signed-off-by: Sasha Levin --- include/linux/bpf.h | 4 ++-- kernel/bpf/arraymap.c | 2 +- kernel/bpf/trampoline.c | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 64fd49b68725c..79e7005de178c 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1452,9 +1452,9 @@ struct bpf_prog_aux { bool tail_call_reachable; bool xdp_has_frags; bool changes_pkt_data; - bool is_extended; /* true if extended by freplace program */ + u16 freplace_link_cnt; /* counts freplace links extending this prog */ u64 prog_array_member_cnt; /* counts how many times as member of prog_array */ - struct mutex ext_mutex; /* mutex for is_extended and prog_array_member_cnt */ + struct mutex ext_mutex; /* mutex for freplace_link_cnt and prog_array_member_cnt */ /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */ const struct btf_type *attach_func_proto; /* function name for valid attach_btf_id */ diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index ca135fa407b85..30d81ee37b859 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -921,7 +921,7 @@ static void *prog_fd_array_get_ptr(struct bpf_map *map, } mutex_lock(&prog->aux->ext_mutex); - is_extended = prog->aux->is_extended; + is_extended = prog->aux->freplace_link_cnt > 0; if (!is_extended) prog->aux->prog_array_member_cnt++; mutex_unlock(&prog->aux->ext_mutex); diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 4eebb9e22d26e..7747708f13a63 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -515,7 +515,7 @@ static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog) } } -static int bpf_freplace_check_tgt_prog(struct bpf_prog *tgt_prog) +static int bpf_freplace_link_tgt_prog(struct bpf_prog *tgt_prog) { struct bpf_prog_aux *aux = tgt_prog->aux; @@ -529,7 +529,7 @@ static int bpf_freplace_check_tgt_prog(struct bpf_prog *tgt_prog) */ return -EBUSY; - aux->is_extended = true; + aux->freplace_link_cnt++; return 0; } @@ -556,7 +556,7 @@ static int __bpf_trampoline_link_prog(struct bpf_tramp_link *link, /* Cannot attach extension if fentry/fexit are in use. */ if (cnt) return -EBUSY; - err = bpf_freplace_check_tgt_prog(tgt_prog); + err = bpf_freplace_link_tgt_prog(tgt_prog); if (err) return err; tr->extension_prog = link->link.prog; @@ -611,7 +611,7 @@ static int __bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, tr->extension_prog->bpf_func, NULL); tr->extension_prog = NULL; guard(mutex)(&tgt_prog->aux->ext_mutex); - tgt_prog->aux->is_extended = false; + tgt_prog->aux->freplace_link_cnt--; return err; } hlist_del_init(&link->tramp_hlist); -- 2.53.0