From: Florian Lehner <dev@der-flo.net>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com,
andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com,
song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org,
sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org,
shuah@kernel.org, davem@davemloft.net, kuba@kernel.org,
hawk@kernel.org, Florian Lehner <dev@der-flo.net>
Subject: [PATCH bpf-next v2 2/2] selftests/bpf: Test LINK_DETACH for perf link
Date: Wed, 4 Mar 2026 22:02:12 +0100 [thread overview]
Message-ID: <20260304210212.235096-3-dev@der-flo.net> (raw)
In-Reply-To: <20260304210212.235096-1-dev@der-flo.net>
Add serial_test_perf_link_detach() to verify that the new LINK_DETACH
support for BPF perf links works correctly. The test creates a link
to a BPF program for a software perf event, confirms the program is
executed, calls bpf_link__detach() to exercise the BPF_LINK_DETACH syscall
path, and then verifies the program is no longer invoked after detach.
Signed-off-by: Florian Lehner <dev@der-flo.net>
---
.../selftests/bpf/prog_tests/perf_link.c | 81 ++++++++++++++++---
1 file changed, 68 insertions(+), 13 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..40e2ad0f843e 100644
--- a/tools/testing/selftests/bpf/prog_tests/perf_link.c
+++ b/tools/testing/selftests/bpf/prog_tests/perf_link.c
@@ -27,30 +27,85 @@ static void burn_cpu(void)
++j;
}
-/* TODO: often fails in concurrent mode */
-void serial_test_perf_link(void)
+static int perf_link_setup(struct test_perf_link **skel, int *pfd)
{
- 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, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
- if (!ASSERT_GE(pfd, 0, "perf_fd"))
+ *pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
+ if (!ASSERT_GE(*pfd, 0, "perf_fd"))
+ return -1;
+
+ *skel = test_perf_link__open_and_load();
+ if (!ASSERT_OK_PTR(*skel, "skel_load"))
+ return -1;
+
+ return 0;
+}
+
+void serial_test_perf_link_detach(void)
+{
+ struct test_perf_link *skel = NULL;
+ int pfd = -1, link_fd = -1, err;
+ int run_cnt_before, run_cnt_after;
+ __u64 timeout_time_ns;
+
+ if (perf_link_setup(&skel, &pfd))
+ 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;
- skel = test_perf_link__open_and_load();
- if (!ASSERT_OK_PTR(skel, "skel_load"))
+ /* 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"))
+ goto cleanup;
+ }
+
+ /* detach via BPF_LINK_DETACH - BPF program should no longer be executed */
+ err = bpf_link_detach(link_fd);
+ if (!ASSERT_OK(err, "link_detach"))
+ goto cleanup;
+
+ /* 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_detached");
+
+cleanup:
+ if (link_fd >= 0)
+ close(link_fd);
+ if (pfd >= 0)
+ close(pfd);
+ test_perf_link__destroy(skel);
+}
+
+/* TODO: often fails in concurrent mode */
+void serial_test_perf_link(void)
+{
+ struct test_perf_link *skel = NULL;
+ 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;
+
+ if (perf_link_setup(&skel, &pfd))
goto cleanup;
link_fd = bpf_link_create(bpf_program__fd(skel->progs.handler), pfd,
--
2.53.0
prev parent reply other threads:[~2026-03-04 21:12 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-04 21:02 [PATCH bpf-next v2 0/2] bpf: Add LINK_DETACH for perf links Florian Lehner
2026-03-04 21:02 ` [PATCH bpf-next v2 1/2] bpf: Add LINK_DETACH support for perf link Florian Lehner
2026-03-04 22:01 ` bot+bpf-ci
2026-03-06 3:38 ` Kumar Kartikeya Dwivedi
2026-03-06 3:59 ` Alexei Starovoitov
2026-03-11 16:48 ` Yonghong Song
2026-03-04 21:02 ` Florian Lehner [this message]
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=20260304210212.235096-3-dev@der-flo.net \
--to=dev@der-flo.net \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=hawk@kernel.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=kuba@kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.