selftests/bpf: fix bpf_cookie failures
Subsystems (3)
v2 cover selftests/bpf: fix bpf_cookie failures
Fix bpf_cookie failures due to missing bpf_testmod and flaky perf_event triggering.
Tested:
./test_progs -t bpf_cookie/perf_event -vv (30 runs): 0 failures ./test_progs -t bpf_cookie -vv
Sun Jian (2):
selftests/bpf: bpf_cookie: skip kprobe_multi tests without bpf_testmod selftests/bpf: bpf_cookie: make perf_event subtest trigger reliably
.../selftests/bpf/prog_tests/bpf_cookie.c | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-)
base-commit: f4d0ec0aa20d49f09dc01d82894ce80d72de0560
Signature · 1 line
2.43.0
PATCH v2 1/2 selftests/bpf: bpf_cookie: skip kprobe_multi tests without bpf_testmod
The kprobe_multi subtests rely on bpf_testmod fentry ksyms.
When bpf_testmod isn't available, libbpf fails to resolve bpf_testmod_fentry_test* and skeleton load fails with -ESRCH, causing false failures.
Skip these subtests when env.has_testmod is false.
---
Changes in v2:
No functional change.
Drop the unrelated perf_event_open() argument change from this patch (moved to patch 2/2).
v1: <https://lore.kernel.org/lkml/[email protected]/>
--- tools/testing/selftests/bpf/prog_tests/bpf_cookie.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +10 -0
@@ -105,6 +105,11 @@ static void kprobe_multi_link_api_subtest(void)unsigned long long addrs[8 ];__u64 cookies[8 ]; +if (!env.has_testmod) { +test__skip (); +return ; + } +if (!ASSERT_OK (load_kallsyms (),"load_kallsyms" )) goto cleanup;
@@ -192,6 +197,11 @@ static void kprobe_multi_attach_api_subtest(void) };__u64 cookies[8 ]; +if (!env.has_testmod) { +test__skip (); +return ; + } + skel =kprobe_multi__open_and_load ();if (!ASSERT_OK_PTR (skel,"fentry_raw_skel_load" )) goto cleanup;
Signature · 1 line
2.43.0
Re: [PATCH v2 1/2] selftests/bpf: bpf_cookie: skip kprobe_multi tests without bpf_testmod
Quoted 9 lines · Sun Jian
The
kprobe_multisubtests rely onbpf_testmodfentry ksyms.When
bpf_testmodisn't available, libbpf fails to resolve bpf_testmod_fentry_test* and skeleton load fails with-ESRCH, causing false failures.Skip these subtests when env.has_testmod is false.
Signed-off-by Sun Jian <[email protected]>
please include bpf-next in the subject, other than that:
thanks,
jirka
Quoted 46 lines
---
Changes in v2:No functional change.
Drop the unrelated
perf_event_open()argument change from this patch (moved to patch 2/2).v1: <https://lore.kernel.org/lkml/[email protected]/>
--- tools/testing/selftests/bpf/prog_tests/bpf_cookie.c | 10 ++++++++++ 1 file changed, 10 insertions(+)tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +10
-0@@ -105,6 +105,11 @@ static void kprobe_multi_link_api_subtest(void)unsigned long long addrs[8 ];__u64 cookies[8 ]; +if (!env.has_testmod) { +test__skip (); +return ; + } +if (!ASSERT_OK (load_kallsyms (),"load_kallsyms" )) goto cleanup;@@ -192,6 +197,11 @@ static void kprobe_multi_attach_api_subtest(void) };__u64 cookies[8 ]; +if (!env.has_testmod) { +test__skip (); +return ; + } + skel =kprobe_multi__open_and_load ();if (!ASSERT_OK_PTR (skel,"fentry_raw_skel_load" )) goto cleanup;Signature · 1 line
2.43.0
PATCH v2 2/2 selftests/bpf: bpf_cookie: make perf_event subtest trigger reliably
In reply to Sun JianThe perf_event subtest relies on SW_CPU_CLOCK sampling to trigger the BPF program, but the current CPU burn loop can be too short on slower systems and may fail to generate any overflow sample. This leaves pe_res unchanged and makes the test flaky.
Make burn_cpu() take a loop count and use a longer burn only for the perf_event subtest. Also scope perf_event_open() to the current task to avoid wasting samples on unrelated activity.
---
Changes in v2:
Move the perf_event_open() argument change here from patch 1/2.
v1: <https://lore.kernel.org/lkml/[email protected]/>
--- .../selftests/bpf/prog_tests/bpf_cookie.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +11 -8
@@ -6,6 +6,7 @@#include <sys/syscall.h> #include <sys/mman.h> #include <unistd.h> +#include <linux/compiler.h> #include <test_progs.h> #include <network_helpers.h> #include <bpf/btf.h>
@@ -431,11 +432,12 @@ static void tp_subtest(struct test_bpf_cookie *skel)bpf_link__destroy (link3); } -static void burn_cpu (void ) +static void burn_cpu (long loops) { -volatile int j =0 ; +long j =0 ;cpu_set_t cpu_set; -int i, err; +long i; +int err;/* generate some branches on cpu 0 */ CPU_ZERO (&cpu_set);
@@ -443,9 +445,10 @@ static void burn_cpu(void) err =pthread_setaffinity_np (pthread_self (),sizeof (cpu_set), &cpu_set);ASSERT_OK (err,"set_thread_affinity" ); -/* spin the loop for a while (random high number) */ -for (i =0 ; i <1000000 ; ++i) +for (i =0 ; i < loops; ++i) { ++j; +barrier (); + } }static void pe_subtest (struct test_bpf_cookie *skel)
@@ -461,7 +464,7 @@ static void pe_subtest(struct test_bpf_cookie *skel) attr.type =PERF_TYPE_SOFTWARE ; attr.config =PERF_COUNT_SW_CPU_CLOCK ; attr.sample_period =100000 ; - pfd =syscall (__NR_perf_event_open, &attr,-1 ,0 ,-1 ,PERF_FLAG_FD_CLOEXEC ); + pfd =syscall (__NR_perf_event_open, &attr,0 ,-1 ,-1 ,PERF_FLAG_FD_CLOEXEC );if (!ASSERT_GE (pfd,0 ,"perf_fd" )) goto cleanup;
@@ -470,7 +473,7 @@ static void pe_subtest(struct test_bpf_cookie *skel)if (!ASSERT_OK_PTR (link,"link1" )) goto cleanup; -burn_cpu ();/* trigger BPF prog */ +burn_cpu (100000000L );/* trigger BPF prog */ ASSERT_EQ (skel->bss->pe_res,0x100000 ,"pe_res1" );
@@ -489,7 +492,7 @@ static void pe_subtest(struct test_bpf_cookie *skel)if (!ASSERT_OK_PTR (link,"link2" )) goto cleanup; -burn_cpu ();/* trigger BPF prog */ +burn_cpu (100000000L );/* trigger BPF prog */ ASSERT_EQ (skel->bss->pe_res,0x200000 ,"pe_res2" );
Signature · 1 line
2.43.0
Re: [PATCH v2 2/2] selftests/bpf: bpf_cookie: make perf_event subtest trigger reliably
Quoted 34 lines · Sun Jian
The
perf_eventsubtest relies onSW_CPU_CLOCKsampling to trigger the BPF program, but the current CPU burn loop can be too short on slower systems and may fail to generate any overflow sample. This leavespe_resunchanged and makes the test flaky.Make
burn_cpu()take a loop count and use a longer burn only for theperf_eventsubtest. Also scopeperf_event_open()to the current task to avoid wasting samples on unrelated activity.Signed-off-by Sun Jian <[email protected]>---
Changes in v2:Move the
perf_event_open()argument change here from patch 1/2.v1: <https://lore.kernel.org/lkml/[email protected]/>
--- .../selftests/bpf/prog_tests/bpf_cookie.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1
-0@@ -6,6 +6,7 @@#include <sys/syscall.h> #include <sys/mman.h> #include <unistd.h> +#include <linux/compiler.h> #include <test_progs.h> #include <network_helpers.h> #include <bpf/btf.h>
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void tp_subtest(struct test_bpf_cookie *skel) · line 435
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1
-1@@ -431,11 +432,12 @@ static void tp_subtest(struct test_bpf_cookie *skel)bpf_link__destroy (link3); } -static void burn_cpu (void ) +static void burn_cpu (long loops)
nit, there's another burn_cpu in prog_tests/perf_link.c, we could add it to trace_helpers.c or test_progs.c
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +3
-2{ -volatile int j =0 ; +long j =0 ;cpu_set_t cpu_set; -int i, err; +long i; +int err;/* generate some branches on cpu 0 */ CPU_ZERO (&cpu_set);
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void burn_cpu(void) · line 450
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +2
-2@@ -443,9 +445,10 @@ static void burn_cpu(void) err =pthread_setaffinity_np (pthread_self (),sizeof (cpu_set), &cpu_set);ASSERT_OK (err,"set_thread_affinity" ); -/* spin the loop for a while (random high number) */ -for (i =0 ; i <1000000 ; ++i) +for (i =0 ; i < loops; ++i) { ++j; +barrier ();
what's the rationale for barrier call in here,
together with the volatile change above?
thanks,
jirka
Quoted 35 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +4
-3+ } }static void pe_subtest (struct test_bpf_cookie *skel)@@ -461,7 +464,7 @@ static void pe_subtest(struct test_bpf_cookie *skel) attr.type =PERF_TYPE_SOFTWARE ; attr.config =PERF_COUNT_SW_CPU_CLOCK ; attr.sample_period =100000 ; - pfd =syscall (__NR_perf_event_open, &attr,-1 ,0 ,-1 ,PERF_FLAG_FD_CLOEXEC ); + pfd =syscall (__NR_perf_event_open, &attr,0 ,-1 ,-1 ,PERF_FLAG_FD_CLOEXEC );if (!ASSERT_GE (pfd,0 ,"perf_fd" )) goto cleanup;@@ -470,7 +473,7 @@ static void pe_subtest(struct test_bpf_cookie *skel)if (!ASSERT_OK_PTR (link,"link1" )) goto cleanup; -burn_cpu ();/* trigger BPF prog */ +burn_cpu (100000000L );/* trigger BPF prog */ ASSERT_EQ (skel->bss->pe_res,0x100000 ,"pe_res1" );@@ -489,7 +492,7 @@ static void pe_subtest(struct test_bpf_cookie *skel)if (!ASSERT_OK_PTR (link,"link2" )) goto cleanup; -burn_cpu ();/* trigger BPF prog */ +burn_cpu (100000000L );/* trigger BPF prog */ ASSERT_EQ (skel->bss->pe_res,0x200000 ,"pe_res2" );Signature · 1 line
2.43.0
Re: [PATCH v2 2/2] selftests/bpf: bpf_cookie: make perf_event subtest trigger reliably
Quoted 9 lines · Jiri Olsa
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void tp_subtest(struct test_bpf_cookie *skel) · line 435
Quoted 3 lines · Sun Jian
The
perf_eventsubtest relies onSW_CPU_CLOCKsampling to trigger the BPFtools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1
-1-static void burn_cpu (void ) +static void burn_cpu (long loops)nit, there's another
burn_cpuinprog_tests/perf_link.c, we could add it totrace_helpers.cortest_progs.c
happy to refactor into a shared helper if maintainers prefer, but I keep it local to minimize the diff.
Quoted 14 lines
Quoted 14 lines
{
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +3
-2-volatile int j =0 ; +long j =0 ;cpu_set_t cpu_set; -int i, err; +long i; +int err;/* generate some branches on cpu 0 */ CPU_ZERO (&cpu_set);@@ -443,9 +445,10 @@ static void burn_cpu(void) err =pthread_setaffinity_np (pthread_self (),sizeof (cpu_set), &cpu_set);ASSERT_OK (err,"set_thread_affinity" );
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void burn_cpu(void) · line 450
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +2
-2-/* spin the loop for a while (random high number) */ -for (i =0 ; i <1000000 ; ++i) +for (i =0 ; i < loops; ++i) { ++j; +barrier ();what's the rationale for barrier call in here,
together with the volatile change above?
The burn_cpu() loop is only meant to consume CPU time to reliably trigger the SW_CPU_CLOCK perf_event overflow. With an side-effect-free loop, the compiler may optimize the loop away or significantly shrink it under -O2.
The old version relied on volatile to prevent the loop from being optimized, but checkpatch warns against it. Using barrier() achieves the same goal — keep the loop intact as a CPU-burn — while making the intent more explicit.
Thanks,
Sun Jian
Jiri Olsa
Acked-byRe: [PATCH v2 2/2] selftests/bpf: bpf_cookie: make perf_event subtest trigger reliably
Quoted 38 lines · sun jian
Quoted 9 lines · Jiri Olsa
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void tp_subtest(struct test_bpf_cookie *skel) · line 435
Quoted 3 lines · Sun Jian
The
perf_eventsubtest relies onSW_CPU_CLOCKsampling to trigger the BPFtools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1
-1-static void burn_cpu (void ) +static void burn_cpu (long loops)nit, there's another
burn_cpuinprog_tests/perf_link.c, we could add it totrace_helpers.cortest_progs.chappy to refactor into a shared helper if maintainers prefer, but I keep it local to minimize the diff.
Quoted 14 lines
Quoted 14 lines
{
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +3
-2-volatile int j =0 ; +long j =0 ;cpu_set_t cpu_set; -int i, err; +long i; +int err;/* generate some branches on cpu 0 */ CPU_ZERO (&cpu_set);@@ -443,9 +445,10 @@ static void burn_cpu(void) err =pthread_setaffinity_np (pthread_self (),sizeof (cpu_set), &cpu_set);ASSERT_OK (err,"set_thread_affinity" );Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void burn_cpu(void) · line 450
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +2
-2-/* spin the loop for a while (random high number) */ -for (i =0 ; i <1000000 ; ++i) +for (i =0 ; i < loops; ++i) { ++j; +barrier ();what's the rationale for barrier call in here,
together with the volatile change above?
Quoted 7 lines
The
burn_cpu()loop is only meant to consume CPU time to reliably trigger theSW_CPU_CLOCKperf_eventoverflow. With an side-effect-free loop, the compiler may optimize the loop away or significantly shrink it under -O2.The old version relied on volatile to prevent the loop from being optimized, but checkpatch warns against it. Using
barrier()achieves the same goal — keep the loop intact as a CPU-burn — while making the intent more explicit.
ok, would be great to have this in the changelog, other than that:
thanks,
jirka
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 8 lines · On Sat, 28 Feb 2026 15:45:53 +0800 you wrote:
Fix
bpf_cookiefailures due to missingbpf_testmodand flakyperf_eventtriggering.Tested:
./test_progs -t bpf_cookie/perf_event -vv (30 runs): 0 failures ./test_progs -tbpf_cookie-vv[...]
Here is the summary with links:
- [v2,1/2] selftests/bpf: bpf_cookie: skip kprobe_multi tests without bpf_testmod
https://git.kernel.org/bpf/bpf-next/c/74d3305e620b - [v2,2/2] selftests/bpf: bpf_cookie: make perf_event subtest trigger reliably https://git.kernel.org/bpf/bpf-next/c/7f20d371fd87
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html