Skip to contentSkip to conversation

selftests/bpf: fix bpf_cookie failures

PATCH Superseded Sun Jian 4 participants 8 messages Feb 27 – Mar 2

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Sun Jian

author
#0 lore raw

cover selftests/bpf: fix bpf_cookie failures

Patch 1/2 skips kprobe_multi subtests when bpf_testmod isn't available. Patch 2 makes perf_event triggering reliable on slower systems.

Tested:
./test_progs -t bpf_cookie/perf_event -vv (30 runs): 0 failures

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
author
#1 lore raw

PATCH 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.

---
 tools/testing/selftests/bpf/prog_tests/bpf_cookie.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +11 -1
index 75f4dff7d042..50f5e11e6e65 100644
@@ -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;
@@ -451,7 +461,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;
 
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH 2/2 selftests/bpf: bpf_cookie: make perf_event subtest trigger reliably

In reply to Sun Jian

The 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.

Tested:
./test_progs -t bpf_cookie/perf_event -vv (30 runs): 0 failures

---
 .../selftests/bpf/prog_tests/bpf_cookie.c       | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +10 -7
index 50f5e11e6e65..35adc3f6d443 100644
@@ -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)
@@ -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
#5 lore raw

Re: [PATCH 2/2] selftests/bpf: bpf_cookie: make perf_event subtest trigger reliably

The second sentence here doesn't appear to match this commit's diff. The perf_event_open() argument change from (attr, -1, 0, -1, flags) to (attr, 0, -1, -1, flags) was made in the previous commit…
Quoted 3 lines

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.

The second sentence here doesn't appear to match this commit's diff. The perf_event_open() argument change from (attr, -1, 0, -1, flags) to (attr, 0, -1, -1, flags) was made in the previous commit e93098f90837 ("selftests/bpf: bpf_cookie: skip kprobe_multi tests without bpf_testmod"), not in this one.

Should this sentence be dropped from the commit message, or moved to the commit that actually makes the change?

---
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/22495307331

AI-authorship-score: low
AI-authorship-explanation: Focused, minimal fix with idiomatic kernel patterns and natural commit message style consistent with human authorship.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Commit message claims a perf_event_open() scoping change that was actually made in the previous commit, not this one.

#2 lore raw

Re: [PATCH 1/2] selftests/bpf: bpf_cookie: skip kprobe_multi tests without bpf_testmod

In reply to Sun Jian
Quoted 43 lines · Sun Jian

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.

---
 tools/testing/selftests/bpf/prog_tests/bpf_cookie.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +10 -0
index 75f4dff7d042..50f5e11e6e65 100644
@@ -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;

tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void pe_subtest(struct test_bpf_cookie *skel) · line 464

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1 -1
@@ -451,7 +461,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);

This part is not explained in the commit log.

pw-bot: cr

author
#3 lore raw

Re: [PATCH 1/2] selftests/bpf: bpf_cookie: skip kprobe_multi tests without bpf_testmod

Quoted 10 lines · Alexei Starovoitov

tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void pe_subtest(struct test_bpf_cookie *skel) · line 464

Quoted 7 lines · Sun Jian

The kprobe_multi subtests rely on bpf_testmod fentry ksyms.

        attr.type = PERF_TYPE_SOFTWARE;
        attr.config = PERF_COUNT_SW_CPU_CLOCK;
        attr.sample_period = 100000;
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1 -1
-       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);
Quoted 3 lines

This part is not explained in the commit log.

pw-bot: cr

You are right: the perf_event_open() argument change is unrelated to the testmod skip patch
and shouldn't have been in this patch. I'll respin as v2 and move it to the appropriate patch.

Thanks,
Sun Jian

reviewer Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] selftests/bpf: bpf_cookie: make perf_event subtest trigger reliably

In reply to Sun Jian
Quoted 5 lines · Sun Jian

The 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.

Quoted 8 lines

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.

Tested:
./test_progs -t bpf_cookie/perf_event -vv (30 runs): 0 failures

Nit below, but change makes sense.

Quoted 17 lines
---
 .../selftests/bpf/prog_tests/bpf_cookie.c       | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1 -0
index 50f5e11e6e65..35adc3f6d443 100644
@@ -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 437

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +2 -2
@@ -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;

Nit: AFAICT most of these changes are unnecessary, you could just bump the cycle count to 100M and add the barrier. Moving everything to longs and factoring out the loop count is unnecessary.

Quoted 39 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +2 -1
 	cpu_set_t cpu_set;
-	int i, err;
+	long i;
 
+	int err;
 
 	/* generate some branches on cpu 0 */
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +5 -4
 	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)
@@ -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");
 
author
#7 lore raw

Re: [PATCH 2/2] selftests/bpf: bpf_cookie: make perf_event subtest trigger reliably

Quoted 9 lines · Emil Tsalapatis
Quoted 2 lines · Sun Jian

The perf_event subtest relies on SW_CPU_CLOCK sampling to trigger the BPF

Nit below, but change makes sense.

Hi Emil,
Thanks a lot for the review and the Reviewed-by!

Quoted 11 lines

tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void tp_subtest(struct test_bpf_cookie *skel) · line 437

Quoted 6 lines

---

tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +2 -2
-static void burn_cpu(void)
+static void burn_cpu(long loops)
 {
-     volatile int j = 0;
+     long j = 0;

Nit: AFAICT most of these changes are unnecessary, you could just bump the cycle count to 100M and add the barrier. Moving everything to longs and factoring out the loop count is unnecessary.

Good point about keeping the diff small. In v2 I dropped the volatile and added a barrier().

v2: https://lore.kernel.org/lkml/[email protected]/

I kept the loop-count parameter mainly to keep the "long burn to trigger perf_event"
intent explicit at the call site, and to avoid hard-coding a large constant in the helper
if it gets reused later.

Thanks again,
Sun Jian