mirror of
https://github.com/torvalds/linux.git
synced 2026-04-18 14:53:58 -04:00
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> Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/r/20260305084306.283983-1-sun.jian.kdev@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
92 lines
2.2 KiB
C
92 lines
2.2 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/* Copyright (c) 2021 Facebook */
|
|
#define _GNU_SOURCE
|
|
#include <linux/compiler.h>
|
|
#include <test_progs.h>
|
|
#include "testing_helpers.h"
|
|
#include "test_perf_link.skel.h"
|
|
|
|
#define BURN_TIMEOUT_MS 100
|
|
#define BURN_TIMEOUT_NS BURN_TIMEOUT_MS * 1000000
|
|
|
|
static void burn_cpu(void)
|
|
{
|
|
int i;
|
|
|
|
/* spin the loop for a while (random high number) */
|
|
for (i = 0; i < 1000000; ++i)
|
|
barrier();
|
|
}
|
|
|
|
void test_perf_link(void)
|
|
{
|
|
struct test_perf_link *skel = NULL;
|
|
struct perf_event_attr attr;
|
|
int pfd = -1, link_fd = -1, err;
|
|
int run_cnt_before, run_cnt_after;
|
|
struct bpf_link_info info;
|
|
__u32 info_len = sizeof(info);
|
|
__u64 timeout_time_ns;
|
|
|
|
/* create perf event */
|
|
memset(&attr, 0, sizeof(attr));
|
|
attr.size = sizeof(attr);
|
|
attr.type = PERF_TYPE_SOFTWARE;
|
|
attr.config = PERF_COUNT_SW_CPU_CLOCK;
|
|
attr.freq = 1;
|
|
attr.sample_freq = 1000;
|
|
pfd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, PERF_FLAG_FD_CLOEXEC);
|
|
if (!ASSERT_GE(pfd, 0, "perf_fd"))
|
|
goto cleanup;
|
|
|
|
skel = test_perf_link__open_and_load();
|
|
if (!ASSERT_OK_PTR(skel, "skel_load"))
|
|
goto cleanup;
|
|
|
|
link_fd = bpf_link_create(bpf_program__fd(skel->progs.handler), pfd,
|
|
BPF_PERF_EVENT, NULL);
|
|
if (!ASSERT_GE(link_fd, 0, "link_fd"))
|
|
goto cleanup;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
err = bpf_link_get_info_by_fd(link_fd, &info, &info_len);
|
|
if (!ASSERT_OK(err, "link_get_info"))
|
|
goto cleanup;
|
|
|
|
ASSERT_EQ(info.type, BPF_LINK_TYPE_PERF_EVENT, "link_type");
|
|
ASSERT_GT(info.id, 0, "link_id");
|
|
ASSERT_GT(info.prog_id, 0, "link_prog_id");
|
|
|
|
/* ensure we get at least one perf_event prog execution */
|
|
timeout_time_ns = get_time_ns() + BURN_TIMEOUT_NS;
|
|
while (true) {
|
|
burn_cpu();
|
|
if (skel->bss->run_cnt > 0)
|
|
break;
|
|
if (!ASSERT_LT(get_time_ns(), timeout_time_ns, "run_cnt_timeout"))
|
|
break;
|
|
}
|
|
|
|
/* perf_event is still active, but we close link and BPF program
|
|
* shouldn't be executed anymore
|
|
*/
|
|
close(link_fd);
|
|
link_fd = -1;
|
|
|
|
/* make sure there are no stragglers */
|
|
kern_sync_rcu();
|
|
|
|
run_cnt_before = skel->bss->run_cnt;
|
|
burn_cpu();
|
|
run_cnt_after = skel->bss->run_cnt;
|
|
|
|
ASSERT_EQ(run_cnt_before, run_cnt_after, "run_cnt_before_after");
|
|
|
|
cleanup:
|
|
if (link_fd >= 0)
|
|
close(link_fd);
|
|
if (pfd >= 0)
|
|
close(pfd);
|
|
test_perf_link__destroy(skel);
|
|
}
|