From: Sasha Levin <sashal@kernel.org>
To: patches@lists.linux.dev, stable@vger.kernel.org
Cc: Mykyta Yatsenko <yatsenko@meta.com>,
Andrii Nakryiko <andrii@kernel.org>,
Sasha Levin <sashal@kernel.org>,
jolsa@kernel.org, chen.dylane@linux.dev, memxor@gmail.com,
ast@kernel.org, bpf@vger.kernel.org
Subject: [PATCH AUTOSEL 6.17-6.12] selftests/bpf: Fix flaky bpf_cookie selftest
Date: Thu, 9 Oct 2025 11:55:22 -0400 [thread overview]
Message-ID: <20251009155752.773732-56-sashal@kernel.org> (raw)
In-Reply-To: <20251009155752.773732-1-sashal@kernel.org>
From: Mykyta Yatsenko <yatsenko@meta.com>
[ Upstream commit 105eb5dc74109a9f53c2f26c9a918d9347a73595 ]
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.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250925215230.265501-1-mykyta.yatsenko5@gmail.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
LLM Generated explanations, may be completely bogus:
YES
- What it fixes and why it matters
- The perf-event subtest in bpf_cookie can fail with -EINVAL when run
after tests that cause perf to throttle sampling (e.g., task_work
stress), because the test uses frequency mode and sets
`attr.sample_freq` above the current global limit. The kernel
validates frequency mode against `sysctl_perf_event_sample_rate` and
rejects it when exceeded (kernel/events/core.c:13403). In contrast,
period mode only rejects if the high bit is set
(kernel/events/core.c:13406), so it avoids this global-throttling
pitfall.
- Perf can dynamically lower `sysctl_perf_event_sample_rate` under
high overhead (see assignment in kernel/events/core.c:654), so this
flakiness can affect real test runs on slower systems or after heavy
tests.
- Specific code change
- In `tools/testing/selftests/bpf/prog_tests/bpf_cookie.c:453-454`,
the test currently sets:
- `attr.freq = 1;`
- `attr.sample_freq = 10000;`
- The commit switches to period mode by replacing those with:
- `attr.sample_period = 100000;`
- This removes reliance on `sysctl_perf_event_sample_rate` entirely
for this test, eliminating the spurious -EINVAL from
`perf_event_open()` and making the selftest deterministic.
- Scope, risk, and stable criteria
- Selftests-only change; no kernel runtime code touched.
- Minimal and contained (1 insertion, 2 deletions in a single file).
- No API or architectural changes; uses long-supported perf_event_attr
fields.
- Purpose is purely to fix test flakiness, not to add features.
- Low regression risk: switching from frequency to period mode is
semantically equivalent for this test’s goal (ensuring perf samples
fire to trigger the attached BPF program during `burn_cpu()`), while
avoiding global sysctl dependency.
- The issue exists in this stable tree: the local file still uses
`attr.freq`/`attr.sample_freq` at
`tools/testing/selftests/bpf/prog_tests/bpf_cookie.c:453-454`.
- Additional context
- The upstream kernel already contains this exact fix (commit
105eb5dc74109 “selftests/bpf: Fix flaky bpf_cookie selftest”).
- Earlier attempts at hardening tests by lowering frequency (e.g., to
1000) still risk hitting the dynamic throttle; period mode is the
robust approach.
Given this is a small, targeted selftest flakiness fix with negligible
risk and clear benefit to stable testing reliability, it is suitable for
backporting.
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
index 4a0670c056bad..75f4dff7d0422 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
@@ -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
next prev parent reply other threads:[~2025-10-09 15:59 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20251009155752.773732-1-sashal@kernel.org>
2025-10-09 15:54 ` [PATCH AUTOSEL 6.17-5.4] bpf: Don't use %pK through printk Sasha Levin
2025-10-09 15:54 ` [PATCH AUTOSEL 6.17-6.1] bpftool: Fix -Wuninitialized-const-pointer warnings with clang >= 21 Sasha Levin
2025-10-09 15:54 ` [PATCH AUTOSEL 6.17-6.12] bpf: Use tnums for JEQ/JNE is_branch_taken logic Sasha Levin
2025-10-09 15:54 ` [PATCH AUTOSEL 6.17-6.16] selftests/bpf: Fix incorrect array size calculation Sasha Levin
2025-10-09 15:54 ` [PATCH AUTOSEL 6.17-6.12] selftests/bpf: Fix selftest verifier_arena_large failure Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.1] bpf: Clear pfmemalloc flag when freeing all fragments Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-5.4] selftests/bpf: Fix bpf_prog_detach2 usage in test_lirc_mode2 Sasha Levin
2025-10-09 15:55 ` Sasha Levin [this message]
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17] selftests: drv-net: Pull data before parsing headers Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.12] libbpf: Fix USDT SIB argument handling causing unrecognized register error Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.12] bpftool: Add CET-aware symbol matching for x86_64 architectures Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.6] bpf: Do not limit bpf_cgroup_from_id to current's namespace Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.1] selftests/bpf: Upon failures, exit with code 1 in test_xsk.sh Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.16] selftests/bpf: Fix arena_spin_lock selftest failure Sasha Levin
2025-10-09 15:56 ` [PATCH AUTOSEL 6.17-6.6] riscv: bpf: Fix uninitialized symbol 'retval_off' Sasha Levin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251009155752.773732-56-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=chen.dylane@linux.dev \
--cc=jolsa@kernel.org \
--cc=memxor@gmail.com \
--cc=patches@lists.linux.dev \
--cc=stable@vger.kernel.org \
--cc=yatsenko@meta.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox