Skip to contentSkip to conversation

selftests/bpf: fix flaky bpf_cookie selftest

PATCH Applied Mykyta Yatsenko 3 participants 3 messages Sep 25, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests/bpf: fix flaky bpf_cookie selftest

From: Mykyta Yatsenko <[email protected]>

bpf_cookie can fail on perf_event_open(), when it runs after the task_work selftest. The task_work test causes perf to lower sysctl_perf_event_sample_rate, and bpf_cookie uses sample_freq, which is validated against that sysctl. As a result, perf_event_open() rejects the attr if the (now tighter) limit is exceeded.

From perf_event_open():

if (attr.freq) {
	if (attr.sample_freq > sysctl_perf_event_sample_rate)
		return -EINVAL;
} else {
	if (attr.sample_period & (1ULL << 63))
		return -EINVAL;
}

Switch bpf_cookie to use sample_period, which is not checked against sysctl_perf_event_sample_rate.

---
 tools/testing/selftests/bpf/prog_tests/bpf_cookie.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1 -2
index 4a0670c056ba..75f4dff7d042 100644
@@ -450,8 +450,7 @@ static void pe_subtest(struct test_bpf_cookie *skel)
 	attr.size = sizeof(attr);
 	attr.type = PERF_TYPE_SOFTWARE;
 	attr.config = PERF_COUNT_SW_CPU_CLOCK;
-	attr.freq = 1;
-	attr.sample_freq = 10000;
+	attr.sample_period = 100000;
 	pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
 	if (!ASSERT_GE(pfd, 0, "perf_fd"))
 		goto cleanup;
Signature · 1 line
2.51.0
#1 lore raw
Quoted 19 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

bpf_cookie can fail on perf_event_open(), when it runs after the task_work selftest. The task_work test causes perf to lower sysctl_perf_event_sample_rate, and bpf_cookie uses sample_freq, which is validated against that sysctl. As a result, perf_event_open() rejects the attr if the (now tighter) limit is exceeded.

From perf_event_open():
if (attr.freq) {
if (attr.sample_freq > sysctl_perf_event_sample_rate)
return -EINVAL;

} else {
        if (attr.sample_period & (1ULL << 63))
                return -EINVAL;
}
Quoted 8 lines

Switch bpf_cookie to use sample_period, which is not checked against sysctl_perf_event_sample_rate.

---
 tools/testing/selftests/bpf/prog_tests/bpf_cookie.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

We have:

$ rg '\.freq\s*=' tools/testing/selftests/bpf
tools/testing/selftests/bpf/prog_tests/find_vma.c
34:     attr.freq = 1;

tools/testing/selftests/bpf/prog_tests/perf_branches.c
112: attr.freq = 1;
153: attr.freq = 1;

tools/testing/selftests/bpf/prog_tests/perf_link.c
46: attr.freq = 1;

tools/testing/selftests/bpf/prog_tests/perf_event_stackmap.c
66: .freq = 1,

tools/testing/selftests/bpf/prog_tests/send_signal.c
220: .freq = 1,
232: .freq = 1,

tools/testing/selftests/bpf/prog_tests/unpriv_bpf_disabled.c
285: attr.freq = 1;

tools/testing/selftests/bpf/prog_tests/fill_link_info.c
216: .freq = 1,

tools/testing/selftests/bpf/prog_tests/stacktrace_build_id_nmi.c
11: .freq = 1,

So there are plenty of other cases like this, unfortunately. As I mentioned before, I think we should rethink how we write perf_event-based tests more generally to make them more robust.

But as a mitigation, I applied this patch to bpf-next. That sysctl_perf_event_sample_rate check for attr.freq explains this somewhat random behavior of perf_event_open() syscall.

cc Peter, in case he wasn't aware of this. This looks like a foot gun in perf_event_open() API, tbh.

Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1 -3
index 4a0670c056ba..75f4dff7d042 100644
@@ -450,8 +450,7 @@ static void pe_subtest(struct test_bpf_cookie *skel)
        attr.size = sizeof(attr);
        attr.type = PERF_TYPE_SOFTWARE;
        attr.config = PERF_COUNT_SW_CPU_CLOCK;
-       attr.freq = 1;
-       attr.sample_freq = 10000;
+       attr.sample_period = 100000;
        pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
        if (!ASSERT_GE(pfd, 0, "perf_fd"))
                goto cleanup;
--

2.51.0

#2 lore raw
In reply to Mykyta Yatsenko
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 10 lines · On Thu, 25 Sep 2025 22:52:30 +0100 you wrote:

From: Mykyta Yatsenko <[email protected]>

bpf_cookie can fail on perf_event_open(), when it runs after the task_work selftest. The task_work test causes perf to lower sysctl_perf_event_sample_rate, and bpf_cookie uses sample_freq, which is validated against that sysctl. As a result, perf_event_open() rejects the attr if the (now tighter) limit is exceeded.

[...]

Here is the summary with links:
- [bpf-next,v2] selftests/bpf: fix flaky bpf_cookie selftest

    https://git.kernel.org/bpf/bpf-next/c/105eb5dc7410

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html