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,
memxor@gmail.com, song@kernel.org, yonghong.song@linux.dev,
jolsa@kernel.org, shuah@kernel.org, davem@davemloft.net,
kuba@kernel.org, hawk@kernel.org, sdf@fomichev.me,
sun.jian.kdev@gmail.com, Florian Lehner <dev@der-flo.net>
Subject: [PATCH bpf-next 2/2 v3] selftests/bpf: Test LINK_DETACH for perf link
Date: Fri, 1 May 2026 18:09:01 +0200 [thread overview]
Message-ID: <20260501160901.224134-3-dev@der-flo.net> (raw)
In-Reply-To: <20260501160901.224134-1-dev@der-flo.net>
Add 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 | 79 ++++++++++++++++---
1 file changed, 67 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/perf_link.c b/tools/testing/selftests/bpf/prog_tests/perf_link.c
index 9e3a0d217af8..b75112c1b67d 100644
--- a/tools/testing/selftests/bpf/prog_tests/perf_link.c
+++ b/tools/testing/selftests/bpf/prog_tests/perf_link.c
@@ -18,29 +18,84 @@ static void burn_cpu(void)
barrier();
}
-void 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, 0, -1, -1, PERF_FLAG_FD_CLOEXEC);
- if (!ASSERT_GE(pfd, 0, "perf_fd"))
+ *pfd = syscall(__NR_perf_event_open, &attr, 0, -1, -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 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);
+}
+
+void 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
next prev parent reply other threads:[~2026-05-01 16:16 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-01 16:08 [PATCH bpf-next 0/2 v3] bpf: Add LINK_DETACH for perf links Florian Lehner
2026-05-01 16:09 ` [PATCH bpf-next 1/2 v3] bpf: Add LINK_DETACH support for perf link Florian Lehner
2026-05-01 16:52 ` bot+bpf-ci
2026-05-01 16:59 ` sashiko-bot
2026-05-01 16:09 ` Florian Lehner [this message]
2026-05-01 17:11 ` [PATCH bpf-next 2/2 v3] selftests/bpf: Test LINK_DETACH " sashiko-bot
2026-05-03 13:14 ` Jiri Olsa
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=20260501160901.224134-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=hawk@kernel.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=sun.jian.kdev@gmail.com \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox