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=1796387562; b=X+x2aUm1RWyBbMTmjSp3rxceGQ38iPP62DNyQKhOsndPigy7jtbIEOTkkZBqChIO//9q sfI4Osm/lerUHhfVToA+I2RjWeIKH1bKFttakF2vUfCLhIozZBpQ82lMu2ZvueRLOj+88 hUT6jDdcx2PrHgNUbIjHX55PGq8H+QHbxm3wQZFdZQmplQpUh9jJzr1TkV7IASjdBvqDc WeqK97gIfVzdBT66swwRWWy5Mlo9nCvKRpi73BvfVyRzQLL8auFbkw+33Wfw8nc9pUY0k hyDy1AR6ag1+l3F6MBeKfVP8T3Xl7AELnt46/YJU0e2epKID5RnAp6YFM5pP0+z58XA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387562; 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=YZpAuPAA7m4tFAf7hEKL2BJOjEAR/BKvn6PyXcnJJBk=; b=tkOPNSuBxwO2IyRKISA/UyTD01JWGcH9Xbl5/nDNeJ74nOS5IJw0QpVdmditaKlxoIdv YaUZmEZk1+QFKcRPzbgyUlVS+9JkC55uo1xm4rM/WCMRbpoHW5M1RohwG+0hhE26CuLOY PG2U1YuMDQYrGfBUSf1yIH7rd9/mlxctK0+LBJZljgF8kOdl1hEJZyhBFajErQpdSWHwc 4mqG4zCkFl5huBsV2KNpSnfRkZuPnCSbLmoa3k8iIyvvgOrLP6EV7pY+zOaxRu40cO0qQ N09ZGQKVFVvPyDgV/Cbe86zw/8wklxIXABUi+7FKoG6er/J83eNM6fSb6xVzR8DSsdw== 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=x6uI0GC6; 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 5880B606A322; Fri, 4 Dec 2026 12:32:42 +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=x6uI0GC6 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C72361F000C2; Fri, 4 Dec 2026 12:32:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387562; bh=YZpAuPAA7m4tFAf7hEKL2BJOjEAR/BKvn6PyXcnJJBk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=x6uI0GC6xV/K9YCg8bc8Y5DfNeAJVV9YLvXFv+Zl6l5o/C7AIZXoqhZ2RYeIoX2XO al5D0WyhqV7cQBO3jiZ770a0bkkil/qWdIHY1ZDkbghI7zRBAuytajhaE3VjfurqVQ 2El1S5eseHzL8FOmwHVR7jQNIZ+WziclODcgVRNs= 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.1 0296/1059] bpf: Keep target extended until its last freplace link detaches Date: Fri, 4 Dec 2026 18:57:07 +0900 Message-ID: <20261204100651.945492051@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-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 the bpf_prog_aux::is_extended bool with a freplace_link_cnt counter, adapting the struct bpf_prog_aux context in bpf.h and using the bpf_tramp_link API names. ] 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 0ce4f1eb92d10..53d397b3d54cd 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1210,9 +1210,8 @@ struct bpf_prog_aux { bool sleepable; bool tail_call_reachable; bool xdp_has_frags; - bool is_extended; /* true if extended by freplace program */ 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 */ @@ -1235,6 +1234,7 @@ struct bpf_prog_aux { int cgroup_atype; /* enum cgroup_bpf_attach_type */ struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]; char name[BPF_OBJ_NAME_LEN]; + u16 freplace_link_cnt; /* counts freplace links extending this prog */ #ifdef CONFIG_SECURITY void *security; #endif diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 6fc07a983dd0c..8fa8c0d846003 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -901,7 +901,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 3473e86a112a0..75f4173a0c24a 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -546,7 +546,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; @@ -560,7 +560,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; } @@ -587,7 +587,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; @@ -642,7 +642,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