bpf: Roll back freplace link state when bpf_arch_text_poke() fails
PATCH v2 1/1 bpf: Roll back freplace link state when bpf_arch_text_poke() fails
From: Yuan Chen <[email protected]>
A freplace attach claims the target prog by bumping tgt_prog->aux->freplace_link_cnt and setting tr->extension_prog. bpf_arch_text_poke() then makes the extension take effect. If the poke fails, the claims are never released: the attach unwinds through bpf_link_cleanup(), which clears link->prog, so bpf_trampoline_unlink_prog() never runs.
Drop the link count under ext_mutex on the error path, and set tr->extension_prog only after the poke succeeded. The count is still bumped before the poke: it blocks prog_array updates while the entry is patched.
--- kernel/bpf/trampoline.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
Changes in v2:
- Add Fixes tag for tr->extension_prog, whose assignment before the poke dates back to be8704ff07d2, as suggested by Leon Hwang.
- Collect Acked-by from Leon Hwang.
kernel/bpf/trampoline.c +10 -3
@@ -973,10 +973,17 @@ static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node, err =bpf_freplace_link_tgt_prog (tgt_prog);if (err)return err; + err =bpf_arch_text_poke (tr->func.addr,BPF_MOD_NOP , +BPF_MOD_JUMP ,NULL , + node->link->prog->bpf_func); +if (err) { +/* Undo the claim from bpf_freplace_link_tgt_prog(). */ +guard (mutex)(&tgt_prog->aux->ext_mutex); + tgt_prog->aux->freplace_link_cnt--; +return err; + } tr->extension_prog = node->link->prog; -return bpf_arch_text_poke (tr->func.addr,BPF_MOD_NOP , -BPF_MOD_JUMP ,NULL , - node->link->prog->bpf_func); +return 0 ; } err =bpf_trampoline_add_prog (tr, node, cnt);if (err)
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 10 lines · On Mon, 28 Sep 2026 17:11:21 +0800 you wrote:
From: Yuan Chen <[email protected]>
A freplace attach claims the target prog by bumping
tgt_prog->aux->freplace_link_cntand settingtr->extension_prog.bpf_arch_text_poke()then makes the extension take effect. If the poke fails, the claims are never released: the attach unwinds throughbpf_link_cleanup(), which clearslink->prog, sobpf_trampoline_unlink_prog()never runs.[...]
Here is the summary with links:
- [bpf-next,v2] bpf: Roll back freplace link state when bpf_arch_text_poke() fails
https://git.kernel.org/bpf/bpf-next/c/5ec398d9ec8f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html