public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next] selftests/bpf: perf_link: avoid failures in concurrent mode
@ 2026-03-05  8:43 Sun Jian
  2026-03-07  0:55 ` Kumar Kartikeya Dwivedi
  2026-03-19  0:10 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Sun Jian @ 2026-03-05  8:43 UTC (permalink / raw)
  To: bpf, linux-kselftest
  Cc: Andrii Nakryiko, Eduard Zingerman, Alexei Starovoitov,
	Daniel Borkmann, Shuah Khan, Sun Jian

perf_link creates a system-wide perf event pinned to CPU 0 (pid=-1, cpu=0)
and also pins the test thread to CPU 0. Under concurrent selftests this
can lead to cross-test interference and CPU 0 contention, making the test
flaky.

Create a per-task perf event instead (pid=0, cpu=-1) and drop CPU pinning
from burn_cpu(). Use barrier() to prevent the burn loop from being
optimized away. Drop the serial_ prefix so the test can run in parallel.
Also remove the stale TODO comment.

Tested:
  ./test_progs -t perf_link -vv
  ./test_progs -j$(nproc) -t perf_link -vv
  for i in $(seq 1 50); do ./test_progs -j$(nproc) -t perf_link; done

Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
---
 .../selftests/bpf/prog_tests/perf_link.c      | 20 +++++--------------
 1 file changed, 5 insertions(+), 15 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/perf_link.c b/tools/testing/selftests/bpf/prog_tests/perf_link.c
index d940ff87fa08..9e3a0d217af8 100644
--- a/tools/testing/selftests/bpf/prog_tests/perf_link.c
+++ b/tools/testing/selftests/bpf/prog_tests/perf_link.c
@@ -1,8 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2021 Facebook */
 #define _GNU_SOURCE
-#include <pthread.h>
-#include <sched.h>
+#include <linux/compiler.h>
 #include <test_progs.h>
 #include "testing_helpers.h"
 #include "test_perf_link.skel.h"
@@ -12,23 +11,14 @@
 
 static void burn_cpu(void)
 {
-	volatile int j = 0;
-	cpu_set_t cpu_set;
-	int i, err;
-
-	/* generate some branches on cpu 0 */
-	CPU_ZERO(&cpu_set);
-	CPU_SET(0, &cpu_set);
-	err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set);
-	ASSERT_OK(err, "set_thread_affinity");
+	int i;
 
 	/* spin the loop for a while (random high number) */
 	for (i = 0; i < 1000000; ++i)
-		++j;
+		barrier();
 }
 
-/* TODO: often fails in concurrent mode */
-void serial_test_perf_link(void)
+void test_perf_link(void)
 {
 	struct test_perf_link *skel = NULL;
 	struct perf_event_attr attr;
@@ -45,7 +35,7 @@ void serial_test_perf_link(void)
 	attr.config = PERF_COUNT_SW_CPU_CLOCK;
 	attr.freq = 1;
 	attr.sample_freq = 1000;
-	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;
 

base-commit: 0b3bb205808195159be633a8cefb602670e856fb
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: perf_link: avoid failures in concurrent mode
  2026-03-05  8:43 [PATCH bpf-next] selftests/bpf: perf_link: avoid failures in concurrent mode Sun Jian
@ 2026-03-07  0:55 ` Kumar Kartikeya Dwivedi
  2026-03-19  0:10 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-03-07  0:55 UTC (permalink / raw)
  To: Sun Jian
  Cc: bpf, linux-kselftest, Andrii Nakryiko, Eduard Zingerman,
	Alexei Starovoitov, Daniel Borkmann, Shuah Khan

On Thu, 5 Mar 2026 at 09:45, Sun Jian <sun.jian.kdev@gmail.com> wrote:
>
> perf_link creates a system-wide perf event pinned to CPU 0 (pid=-1, cpu=0)
> and also pins the test thread to CPU 0. Under concurrent selftests this
> can lead to cross-test interference and CPU 0 contention, making the test
> flaky.
>
> Create a per-task perf event instead (pid=0, cpu=-1) and drop CPU pinning
> from burn_cpu(). Use barrier() to prevent the burn loop from being
> optimized away. Drop the serial_ prefix so the test can run in parallel.
> Also remove the stale TODO comment.
>
> Tested:
>   ./test_progs -t perf_link -vv
>   ./test_progs -j$(nproc) -t perf_link -vv
>   for i in $(seq 1 50); do ./test_progs -j$(nproc) -t perf_link; done
>
> Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
> ---

I tried reproducing the flakiness when removing serial_ prefix without
this patch for a while, but wasn't successful.
That said, the logic makes sense to me, the bit about burn_cpu() being
optimized away can be true, and I think it is an improvement.

Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>

>  [...]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: perf_link: avoid failures in concurrent mode
  2026-03-05  8:43 [PATCH bpf-next] selftests/bpf: perf_link: avoid failures in concurrent mode Sun Jian
  2026-03-07  0:55 ` Kumar Kartikeya Dwivedi
@ 2026-03-19  0:10 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-03-19  0:10 UTC (permalink / raw)
  To: Sun Jian; +Cc: bpf, linux-kselftest, andrii, eddyz87, ast, daniel, shuah

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Thu,  5 Mar 2026 16:43:05 +0800 you wrote:
> perf_link creates a system-wide perf event pinned to CPU 0 (pid=-1, cpu=0)
> and also pins the test thread to CPU 0. Under concurrent selftests this
> can lead to cross-test interference and CPU 0 contention, making the test
> flaky.
> 
> Create a per-task perf event instead (pid=0, cpu=-1) and drop CPU pinning
> from burn_cpu(). Use barrier() to prevent the burn loop from being
> optimized away. Drop the serial_ prefix so the test can run in parallel.
> Also remove the stale TODO comment.
> 
> [...]

Here is the summary with links:
  - [bpf-next] selftests/bpf: perf_link: avoid failures in concurrent mode
    https://git.kernel.org/bpf/bpf-next/c/888329ba6c8b

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-03-19  0:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-05  8:43 [PATCH bpf-next] selftests/bpf: perf_link: avoid failures in concurrent mode Sun Jian
2026-03-07  0:55 ` Kumar Kartikeya Dwivedi
2026-03-19  0:10 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox