bpf: Fix WARNING in bpf_trampoline_multi_detach
Subsystems (4)
cover bpf: Fix WARNING in bpf_trampoline_multi_detach
Since tracing_multi link does not set ftrace_managed, it would fail to release the tracing_multi link when attaching tracing_multi link and then attaching fentry link.
[ 3.714215] WARNING: kernel/bpf/trampoline.c:1727 at bpf_trampoline_multi_detach+0x20b/0x240, CPU#1: test_progs/97 ... [ 3.733170] bpf_tracing_multi_link_release+0x14/0x30 [ 3.733890] bpf_link_free+0x58/0x130 [ 3.734414] bpf_link_release+0x23/0x30
Fix it by setting 'ftrace_managed = true' in register_fentry_multi().
Note: the reason of targeting bpf-next tree is I'm planning to add tracing_multi link support for bpf progs, that would rely on the fix and have code conflict with the test.
Leon Hwang (2):
bpf: Mark tracing_multi trampolines as ftrace managed selftests/bpf: Test fentry link after tracing_multi link
kernel/bpf/trampoline.c | 1 + .../selftests/bpf/prog_tests/tracing_multi.c | 69 +++++++++++++++++++ .../progs/tracing_multi_intersect_attach.c | 8 +++ 3 files changed, 78 insertions(+)
Signature · 1 line
2.55.0
PATCH 1/2 bpf: Mark tracing_multi trampolines as ftrace managed
Since tracing_multi link does not set ftrace_managed, it would fail to release the tracing_multi link when attaching tracing_multi link and then attaching fentry link.
[ 3.714215] WARNING: kernel/bpf/trampoline.c:1727 at bpf_trampoline_multi_detach+0x20b/0x240, CPU#1: test_progs/97 ... [ 3.733170] bpf_tracing_multi_link_release+0x14/0x30 [ 3.733890] bpf_link_free+0x58/0x130 [ 3.734414] bpf_link_release+0x23/0x30
Fix it by setting 'ftrace_managed = true' in register_fentry_multi().
--- kernel/bpf/trampoline.c | 1 + 1 file changed, 1 insertion(+)
kernel/bpf/trampoline.c +1 -0
@@ -1536,6 +1536,7 @@ static int register_fentry_multi(struct bpf_trampoline *tr, struct bpf_tramp_imaif (bpf_trampoline_use_jmp (tr->flags)) addr =ftrace_jmp_set (addr); + tr->func.ftrace_managed = true;ftrace_hash_add (data->reg, data->entry, ip, addr); tr->cur_image = im;return 0 ;
Signature · 1 line
2.55.0
PATCH 2/2 selftests/bpf: Test fentry link after tracing_multi link
In reply to Leon HwangVerify that there's no any issue to attach tracing_multi link, then attach fentry link.
--- .../selftests/bpf/prog_tests/tracing_multi.c | 69 +++++++++++++++++++ .../progs/tracing_multi_intersect_attach.c | 8 +++ 2 files changed, 77 insertions(+)
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +69 -0
@@ -460,6 +460,73 @@ static void test_intersect(void)tracing_multi_intersect__destroy (skel); } +static void test_fentry_after_multi (void ) +{ +static const char *const funcs[] = { +"bpf_fentry_test1" , + }; +struct bpf_link *fentry_link =NULL , *multi_link =NULL ; +struct tracing_multi_intersect *skel =NULL ; +LIBBPF_OPTS (bpf_tracing_multi_opts, opts); +LIBBPF_OPTS (bpf_test_run_opts, topts); +__u32 *ids =NULL ; +int err; + + skel =tracing_multi_intersect__open_and_load (); +if (!ASSERT_OK_PTR (skel,"tracing_multi_intersect__open_and_load" )) +return ; + + skel->bss->pid =getpid (); + + ids =get_ids (funcs,ARRAY_SIZE (funcs),NULL ); +if (!ASSERT_OK_PTR (ids,"get_ids" )) + goto cleanup; + + opts.ids = ids; + opts.cnt =ARRAY_SIZE (funcs); + multi_link =bpf_program__attach_tracing_multi (skel->progs.fentry_1,NULL , &opts); +if (!ASSERT_OK_PTR (multi_link,"attach_multi" )) + goto cleanup; + + fentry_link =bpf_program__attach (skel->progs.fentry); +if (!ASSERT_OK_PTR (fentry_link,"attach_fentry" )) + goto cleanup; + + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.fentry_1), &topts); +if (!ASSERT_OK (err,"test_run" )) + goto cleanup; +ASSERT_EQ (skel->bss->test_result_fentry_1,1 ,"multi_fentry" ); +ASSERT_EQ (skel->bss->test_result_fentry,1 ,"fentry" ); + + err =bpf_link__destroy (fentry_link); + fentry_link =NULL ; +if (!ASSERT_OK (err,"destroy_fentry" )) + goto cleanup; + + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.fentry_1), &topts); +if (!ASSERT_OK (err,"test_run_multi" )) + goto cleanup; +ASSERT_EQ (skel->bss->test_result_fentry_1,2 ,"multi_fentry_only" ); +ASSERT_EQ (skel->bss->test_result_fentry,1 ,"fentry_detached" ); + + err =bpf_link__destroy (multi_link); + multi_link =NULL ; +if (!ASSERT_OK (err,"destroy_multi" )) + goto cleanup; + + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.fentry_1), &topts); +if (!ASSERT_OK (err,"test_run_detached" )) + goto cleanup; +ASSERT_EQ (skel->bss->test_result_fentry_1,2 ,"multi_fentry_detached" ); +ASSERT_EQ (skel->bss->test_result_fentry,1 ,"fentry_still_detached" ); + +cleanup: +bpf_link__destroy (fentry_link); +bpf_link__destroy (multi_link); +free (ids); +tracing_multi_intersect__destroy (skel); +} +static void test_session (void ) {LIBBPF_OPTS (bpf_test_run_opts, topts);
@@ -957,4 +1024,6 @@ void test_tracing_multi_test(void)if (test__start_subtest ("attach_api_fails" ))test_attach_api_fails ();RUN_TESTS (tracing_multi_verifier); +if (test__start_subtest ("fentry_after_multi" )) +test_fentry_after_multi (); }
tools/testing/selftests/bpf/progs/tracing_multi_intersect_attach.c +8 -0
@@ -11,6 +11,14 @@ __u64 test_result_fentry_1 = 0;__u64 test_result_fentry_2 =0 ;__u64 test_result_fexit_1 =0 ;__u64 test_result_fexit_2 =0 ; +__u64 test_result_fentry =0 ; + +SEC("fentry/bpf_fentry_test1") +int BPF_PROG (fentry ,int a) +{ + test_result_fentry += a ==1 ; +return 0 ; +} SEC("fentry.multi")int BPF_PROG (fentry_1 )
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next 2/2] selftests/bpf: Test fentry link after tracing_multi link
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Test Flakiness due to Missing PID Filtering in BPF `fentry` Program --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Test Flakiness due to Missing PID Filtering in BPF fentry Program --
--- Patch [2]: [PATCH bpf-next 2/2] selftests/bpf: Test fentry link after tracing_multi link --- commit 7dc95a7a3d3a0ca96e45e5ad2ebebe5157ae17ed
Author: Leon Hwang <[email protected]>
selftests/bpf: Test fentry link after tracing_multi link
This commit adds a selftest to verify that an fentry link can be attached safely after a tracing_multi link. It introduces a new fentry BPF program and a corresponding user-space test case.
Quoted 4 lines
tools/testing/selftests/bpf/progs/tracing_multi_intersect_attach.c +0
-0
[ ... ]
tools/testing/selftests/bpf/progs/tracing_multi_intersect_attach.c __u64 test_result_fentry_1 = 0; · line 19
Quoted 10 lines
tools/testing/selftests/bpf/progs/tracing_multi_intersect_attach.c +6
-0@@ -11,6 +11,14 @@ __u64 test_result_fentry_1 = 0;__u64 test_result_fentry_2 =0 ;__u64 test_result_fexit_1 =0 ;__u64 test_result_fexit_2 =0 ; +__u64 test_result_fentry =0 ; + +SEC("fentry/bpf_fentry_test1") +int BPF_PROG (fentry ,int a) +{ + test_result_fentry += a ==1 ;
[Severity: Medium]
Can this cause test flakiness during parallel selftest runs?
Because bpf_fentry_test1 is a global kernel function widely used across various BPF selftests, parallel test executions might trigger this program. Since the counter is incremented blindly without filtering by the executing process PID, could parallel executions increase test_result_fentry above 1 and cause the userland assertion ASSERT_EQ(skel->bss->test_result_fentry, 1) to sporadically fail?
Quoted 5 lines
tools/testing/selftests/bpf/progs/tracing_multi_intersect_attach.c +2
-0+return 0 ; +} SEC("fentry.multi")int BPF_PROG (fentry_1 )
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH bpf-next 2/2] selftests/bpf: Test fentry link after tracing_multi link
Quoted 35 lines · On 2026/7/10 23:04, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Test Flakiness due to Missing PID Filtering in BPFfentryProgram ----- Patch [2]: [PATCH bpf-next 2/2] selftests/bpf: Test fentry link after
tracing_multilink --- commit7dc95a7a3d3a0ca96e45e5ad2ebebe5157ae17ed
Author: Leon Hwang <[email protected]>selftests/bpf: Test fentry link after
tracing_multilinkThis commit adds a selftest to verify that an fentry link can be attached safely after a
tracing_multilink. It introduces a new fentry BPF program and a corresponding user-space test case.Quoted 4 lines
tools/testing/selftests/bpf/progs/tracing_multi_intersect_attach.c +0
-0[ ... ]
tools/testing/selftests/bpf/progs/tracing_multi_intersect_attach.c __u64 test_result_fentry_1 = 0; · line 19
Quoted 10 lines
tools/testing/selftests/bpf/progs/tracing_multi_intersect_attach.c +6
-0@@ -11,6 +11,14 @@ __u64 test_result_fentry_1 = 0;__u64 test_result_fentry_2 =0 ;__u64 test_result_fexit_1 =0 ;__u64 test_result_fexit_2 =0 ; +__u64 test_result_fentry =0 ; + +SEC("fentry/bpf_fentry_test1") +int BPF_PROG (fentry ,int a) +{ + test_result_fentry += a ==1 ;[Severity: Medium]
Can this cause test flakiness during parallel selftest runs?
Quoted 7 lines
Because
bpf_fentry_test1is a global kernel function widely used across various BPF selftests, parallel test executions might trigger this program. Since the counter is incremented blindly without filtering by the executing process PID, could parallel executions increasetest_result_fentryabove 1 and cause the userland assertionASSERT_EQ(skel->bss->test_result_fentry, 1)to sporadically fail?
Correct.
I should use tracing_multi_arg_check() here, which would filter the PID.
Thanks,
Leon
Quoted 6 lines
Quoted 5 lines
tools/testing/selftests/bpf/progs/tracing_multi_intersect_attach.c +2
-0+return 0 ; +} SEC("fentry.multi")int BPF_PROG (fentry_1 )
Re: [PATCH bpf-next 1/2] bpf: Mark tracing_multi trampolines as ftrace managed
In reply to Leon HwangQuoted 23 lines · Leon Hwang
Since
tracing_multilink does not setftrace_managed, it would fail to release thetracing_multilink when attachingtracing_multilink and then attaching fentry link.[ 3.714215] WARNING: kernel/bpf/trampoline.c:1727 at bpf_trampoline_multi_detach+0x20b/0x240, CPU#1: test_progs/97 ... [ 3.733170] bpf_tracing_multi_link_release+0x14/0x30 [ 3.733890] bpf_link_free+0x58/0x130 [ 3.734414] bpf_link_release+0x23/0x30Fix it by setting '
ftrace_managed= true' inregister_fentry_multi().Fixes aef4dfa790b2 ("bpf: Add bpf_trampoline_multi_attach/detach functions")Signed-off-by Leon Hwang <[email protected]>--- kernel/bpf/trampoline.c | 1 + 1 file changed, 1 insertion(+)kernel/bpf/trampoline.c +0
-0
kernel/bpf/trampoline.c static int register_fentry_multi(struct bpf_trampoline *tr, struct bpf_tramp_ima · line 1539
Quoted 4 lines
kernel/bpf/trampoline.c +1
-0@@ -1536,6 +1536,7 @@ static int register_fentry_multi(struct bpf_trampoline *tr, struct bpf_tramp_imaif (bpf_trampoline_use_jmp (tr->flags)) addr =ftrace_jmp_set (addr); + tr->func.ftrace_managed = true;
I wonder we could set tr->func.ftrace_managed early in bpf_trampoline_get, but we could do that as follow up
thanks,
jirka
Quoted 6 lines
kernel/bpf/trampoline.c +0
-0ftrace_hash_add (data->reg, data->entry, ip, addr); tr->cur_image = im;return 0 ;Signature · 1 line
2.55.0
Re: [PATCH bpf-next 1/2] bpf: Mark tracing_multi trampolines as ftrace managed
Quoted 27 lines · Jiri Olsa
Quoted 26 lines · Leon Hwang
Since
tracing_multilink does not setftrace_managed, it would fail to release thetracing_multilink when attachingtracing_multilink and then attaching fentry link.[ 3.714215] WARNING: kernel/bpf/trampoline.c:1727 at bpf_trampoline_multi_detach+0x20b/0x240, CPU#1: test_progs/97 ... [ 3.733170] bpf_tracing_multi_link_release+0x14/0x30 [ 3.733890] bpf_link_free+0x58/0x130 [ 3.734414] bpf_link_release+0x23/0x30Fix it by setting '
ftrace_managed= true' inregister_fentry_multi().Fixes aef4dfa790b2 ("bpf: Add bpf_trampoline_multi_attach/detach functions")Signed-off-by Leon Hwang <[email protected]>--- kernel/bpf/trampoline.c | 1 + 1 file changed, 1 insertion(+)kernel/bpf/trampoline.c +0
-0@@ -1536,6 +1536,7 @@ static int register_fentry_multi(struct bpf_trampoline *tr, struct bpf_tramp_imaif (bpf_trampoline_use_jmp (tr->flags)) addr =ftrace_jmp_set (addr);
Quoted 4 lines
kernel/bpf/trampoline.c static int register_fentry_multi(struct bpf_trampoline *tr, struct bpf_tramp_ima · line 1539
Quoted 1 line
kernel/bpf/trampoline.c +1
-0+ tr->func.ftrace_managed = true;I wonder we could set
tr->func.ftrace_managedearly inbpf_trampoline_get, but we could do that as follow up
Makes sense. Let me try it when adding tracing_multi link support for bpf progs.
Quoted 2 lines
Acked-by Jiri Olsa <[email protected]>
Thanks for your review.
Leon
Quoted 11 lines
thanks,
jirkaQuoted 6 lines
kernel/bpf/trampoline.c +0
-0ftrace_hash_add (data->reg, data->entry, ip, addr); tr->cur_image = im;return 0 ;Signature · 1 line
2.55.0