bpf: Keep target extended until its last freplace link detaches
Subsystems (4)
cover bpf: Keep target extended until its last freplace link detaches
From: Yuan Chen <[email protected]>
is_extended is a plain boolean, but 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 it whenever *any* of them detached, so with two links on one target, detaching one of them re-armed the unbounded tail call loop closed by commit d6083f040d5d ("bpf: Prevent tailcall infinite loop caused by freplace"):
a single BPF_PROG_TEST_RUN on the tail-called target then panics the kernel, reproduced on both bpf-next and master.
Patch 1 replaces the boolean with a count of the freplace links attached to each target prog, so the target stays extended until its last link detaches. Patch 2 adds a selftest covering the sequence.
Yuan Chen (2):
bpf: Keep target extended until its last freplace link detaches selftests/bpf: Verify is_extended with multiple freplace links
include/linux/bpf.h | 4 +- kernel/bpf/arraymap.c | 2 +- kernel/bpf/trampoline.c | 10 ++- .../selftests/bpf/prog_tests/tailcalls.c | 74 +++++++++++++++++++ .../bpf/progs/tailcall_freplace_multi.c | 27 +++++++ 5 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/tailcall_freplace_multi.c
Signature · 1 line
2.54.0
PATCH 1/2 bpf: Keep target extended until its last freplace link detaches
From: Yuan Chen <[email protected]>
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.
--- include/linux/bpf.h | 4 ++-- kernel/bpf/arraymap.c | 2 +- kernel/bpf/trampoline.c | 10 ++++++---- 3 files changed, 9 insertions(+), 7 deletions(-)
include/linux/bpf.h +2 -2
@@ -1773,7 +1773,6 @@ struct bpf_prog_aux {bool xdp_has_frags;bool exception_cb;bool exception_boundary; -bool is_extended;/* true if extended by freplace program */ bool jits_use_priv_stack;bool priv_stack_requested;bool changes_pkt_data;
@@ -1785,7 +1784,8 @@ struct bpf_prog_aux {u8 verdict; } sig;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 */ +u64 freplace_link_cnt;/* counts freplace links extending this prog */ +struct mutex ext_mutex;/* mutex for freplace_link_cnt and prog_array_member_cnt */ struct bpf_arena *arena;void (*recursion_detected)(struct bpf_prog *prog);/* callback if recursion is detected */ /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
kernel/bpf/arraymap.c +1 -1
@@ -974,7 +974,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);
kernel/bpf/trampoline.c +6 -4
@@ -813,7 +813,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;
@@ -827,7 +827,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 ; }
@@ -933,7 +933,7 @@ static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,/* 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 = node->link->prog;
@@ -979,7 +979,9 @@ static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node, tr->extension_prog->bpf_func, NULL); tr->extension_prog =NULL ;guard (mutex)(&tgt_prog->aux->ext_mutex); - tgt_prog->aux->is_extended = false; +if (WARN_ON_ONCE (!tgt_prog->aux->freplace_link_cnt)) +return err; + tgt_prog->aux->freplace_link_cnt--;return err; }bpf_trampoline_remove_prog (tr, node);
Signature · 1 line
2.54.0
Quoted 14 lines · On 23/9/26 17:06, [email protected] wrote:
From: Yuan Chen <[email protected]>
The
is_extended/prog_array_member_cntprotocol introduced by commitd6083f040d5d("bpf: Prevent tailcall infinite loop caused by freplace") keeps a prog extended by a freplace program out ofprog_arraymaps, and vice versa: once a tail call re-enters an extended subprogram, itstail_call_cntresets on every execution and the loop never terminates.But
is_extendedis 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()clearedis_extendedwhenever *any* freplace link detached, so detaching one of two links re-armed the unbounded loop through the remaining one.
Quoted 7 lines
Replace the
is_extendedboolean with a count of the freplace links attached to each target prog, so the target stays extended until its last link detaches. Also renamebpf_freplace_check_tgt_prog()tobpf_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")
You should cc me when posting the next revision.
You can get the cc list by ./scripts/get_maintainer.pl /path/to/file.patch.
Quoted 7 lines
Signed-off-by Yuan Chen <[email protected]>--- include/linux/bpf.h | 4 ++-- kernel/bpf/arraymap.c | 2 +- kernel/bpf/trampoline.c | 10 ++++++---- 3 files changed, 9 insertions(+), 7 deletions(-)
[...]
Quoted 11 lines
kernel/bpf/trampoline.c +1
-1@@ -933,7 +933,7 @@ static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,/* 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 = node->link->prog;
kernel/bpf/trampoline.c static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node, · line 982
Quoted 5 lines
kernel/bpf/trampoline.c +1
-1@@ -979,7 +979,9 @@ static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node, tr->extension_prog->bpf_func, NULL); tr->extension_prog =NULL ;guard (mutex)(&tgt_prog->aux->ext_mutex); - tgt_prog->aux->is_extended = false; +if (WARN_ON_ONCE (!tgt_prog->aux->freplace_link_cnt))
Is this WARN_ON_ONCE() necessary?
Thanks,
Leon
Quoted 5 lines
kernel/bpf/trampoline.c +2
-0+return err; + tgt_prog->aux->freplace_link_cnt--;return err; }bpf_trampoline_remove_prog (tr, node);
[email protected]
AI reviewThis isn't a bug, but would it be worth keeping this fix minimal for backporting and doing the rename as a separate follow-up patch?
Quoted 4 lines
include/linux/bpf.h +0
-0
[ ... ]
Quoted 4 lines
kernel/bpf/arraymap.c +0
-0
[ ... ]
Quoted 14 lines
kernel/bpf/trampoline.c +1
-1@@ -843,7 +843,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;
kernel/bpf/trampoline.c static int bpf_freplace_check_tgt_prog(struct bpf_prog *tgt_prog) · line 862
Quoted 7 lines
kernel/bpf/trampoline.c +1
-1@@ -857,7 +857,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 ; }
This isn't a bug, but would it be worth keeping this fix minimal for backporting and doing the rename as a separate follow-up patch?
The rename from bpf_freplace_check_tgt_prog() to bpf_freplace_link_tgt_prog() is independent of the counting fix: the only functional change in this helper is 'aux->is_extended = true' becoming 'aux->freplace_link_cnt++', which does not require a new name.
Since the patch carries a Fixes: tag for d6083f040d5d, it is a stable-backport candidate, and folding a rename into it adds conflict surface for no behavioural reason.
If the rename stays, does something like bpf_freplace_get_tgt_prog() or bpf_freplace_reserve_tgt_prog() read less ambiguously next to __bpf_trampoline_link_prog(), where 'link' means 'attach a program to a trampoline'?
kernel/bpf/trampoline.c static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node, · line 976
Quoted 10 lines
kernel/bpf/trampoline.c +1
-1@@ -970,7 +970,7 @@ static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,/* 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 = node->link->prog;
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/35842777159
include/linux/bpf.h struct bpf_prog_aux { · line 1787
Quoted 1 line · On Wed, Sep 23, 2026 at 05:06 PM [email protected] <[email protected]>…
include/linux/bpf.h +1
-0+u64 freplace_link_cnt;/* counts freplace links extending this prog */
u64 ? It's bounded by BPF_MAX_SUBPROGS.
pw-bot: cr
PATCH 2/2 selftests/bpf: Verify is_extended with multiple freplace links
In reply to [email protected]From: Yuan Chen <[email protected]>
Extend tc_bpf2bpf with two freplace links, one on entry_tc and one on subprog_tc, and detach the one on the entry: while subprog_tc is still extended, updating entry_tc into a prog_array map must keep failing with -EBUSY, and succeed again once the last link detaches. The test asserts the rejection instead of running the prog, as the update succeeds and the prog loops unbounded on an unfixed kernel.
--- .../selftests/bpf/prog_tests/tailcalls.c | 74 +++++++++++++++++++ .../bpf/progs/tailcall_freplace_multi.c | 27 +++++++ 2 files changed, 101 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/tailcall_freplace_multi.c
tools/testing/selftests/bpf/prog_tests/tailcalls.c +74 -0
@@ -6,6 +6,7 @@#include "tailcall_bpf2bpf_hierarchy2.skel.h" #include "tailcall_bpf2bpf_hierarchy3.skel.h" #include "tailcall_freplace.skel.h" +#include "tailcall_freplace_multi.skel.h" #include "tc_bpf2bpf.skel.h" #include "tailcall_fail.skel.h" #include "tailcall_cgrp_storage_owner.skel.h"
@@ -1655,6 +1656,77 @@ static void test_tailcall_bpf2bpf_freplace(void)tc_bpf2bpf__destroy (tc_skel); } +static void test_tailcall_freplace_multi (void ) +{ +struct tailcall_freplace_multi *freplace_skel =NULL ; +struct bpf_link *link_subprog =NULL , *link_entry =NULL ; +struct tc_bpf2bpf *tc_skel =NULL ; +int tc_prog_fd, map_fd, key =0 , err; + + tc_skel =tc_bpf2bpf__open_and_load (); +if (!ASSERT_OK_PTR (tc_skel,"tc_bpf2bpf__open_and_load" )) +return ; + + tc_prog_fd =bpf_program__fd (tc_skel->progs.entry_tc); + + freplace_skel =tailcall_freplace_multi__open (); +if (!ASSERT_OK_PTR (freplace_skel,"tailcall_freplace_multi__open" )) + goto out; + + err =bpf_program__set_attach_target (freplace_skel->progs.subprog_freplace, + tc_prog_fd,"subprog_tc" ); +if (!ASSERT_OK (err,"set_attach_target subprog_tc" )) + goto out; + + err =bpf_program__set_attach_target (freplace_skel->progs.entry_freplace, + tc_prog_fd,"entry_tc" ); +if (!ASSERT_OK (err,"set_attach_target entry_tc" )) + goto out; + + err =tailcall_freplace_multi__load (freplace_skel); +if (!ASSERT_OK (err,"tailcall_freplace_multi__load" )) + goto out; + + map_fd =bpf_map__fd (freplace_skel->maps.jmp_table); + + link_subprog =bpf_program__attach_freplace (freplace_skel->progs.subprog_freplace, + tc_prog_fd,"subprog_tc" ); +if (!ASSERT_OK_PTR (link_subprog,"attach_freplace subprog_tc" )) + goto out; + + link_entry =bpf_program__attach_freplace (freplace_skel->progs.entry_freplace, + tc_prog_fd,"entry_tc" ); +if (!ASSERT_OK_PTR (link_entry,"attach_freplace entry_tc" )) + goto out; + + err =bpf_map_update_elem (map_fd, &key, &tc_prog_fd,BPF_ANY ); +if (!ASSERT_ERR (err,"update jmp_table with extended prog" )) + goto out; + + err =bpf_link__destroy (link_entry); + link_entry =NULL ; +if (!ASSERT_OK (err,"destroy entry link" )) + goto out; + + err =bpf_map_update_elem (map_fd, &key, &tc_prog_fd,BPF_ANY ); +if (!ASSERT_ERR (err,"update jmp_table with still extended prog" )) + goto out; + + err =bpf_link__destroy (link_subprog); + link_subprog =NULL ; +if (!ASSERT_OK (err,"destroy subprog link" )) + goto out; + + err =bpf_map_update_elem (map_fd, &key, &tc_prog_fd,BPF_ANY ); +ASSERT_OK (err,"update jmp_table" ); + +out: +bpf_link__destroy (link_subprog); +bpf_link__destroy (link_entry); +tailcall_freplace_multi__destroy (freplace_skel); +tc_bpf2bpf__destroy (tc_skel); +} +static void test_tailcall_failure () {RUN_TESTS (tailcall_fail);
@@ -2005,6 +2077,8 @@ void test_tailcalls(void)test_tailcall_freplace ();if (test__start_subtest ("tailcall_bpf2bpf_freplace" ))test_tailcall_bpf2bpf_freplace (); +if (test__start_subtest ("tailcall_freplace_multi" )) +test_tailcall_freplace_multi ();if (test__start_subtest ("tailcall_failure" ))test_tailcall_failure ();if (test__start_subtest ("tailcall_sleepable" ))
tools/testing/selftests/bpf/progs/tailcall_freplace_multi.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 KylinSoft Corporation. */ + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +struct { + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); + __uint(max_entries, 1); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u32)); +} jmp_table SEC(".maps"); + +SEC("freplace") +int subprog_freplace (struct __sk_buff *skb) +{ +bpf_tail_call_static (skb, &jmp_table,0 ); +return 0 ; +} + +SEC("freplace") +int entry_freplace (struct __sk_buff *skb) +{ +return 0 ; +} + +char __license[] SEC("license") = "GPL";
Signature · 1 line
2.54.0