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 1/2] bpf: Add LINK_DETACH support for perf link
Date: Wed, 4 Mar 2026 22:02:11 +0100 [thread overview]
Message-ID: <20260304210212.235096-2-dev@der-flo.net> (raw)
In-Reply-To: <20260304210212.235096-1-dev@der-flo.net>
Add bpf_perf_link_detach() to allow detaching a BPF program from its
perf event via BPF_LINK_DETACH while keeping the link file descriptor
alive. This mirrors the existing behavior for xdp and cgroup links
and enables temporarily disabling uprobes or other perf event-based
programs that are attached via bpf_perf_links without closing the link.
bpf_perf_link_release() is made idempotent using xchg() so that closing
the link fd after an explicit LINK_DETACH does not call
perf_event_free_bpf_prog() a second time. bpf_perf_link_fill_link_info()
and bpf_perf_link_show_fdinfo() gain NULL guards so that querying an
already-detached link returns an empty result rather than dereferencing
a stale pointer.
Signed-off-by: Florian Lehner <dev@der-flo.net>
---
kernel/bpf/syscall.c | 30 ++++++++++++++++++++++++++----
1 file changed, 26 insertions(+), 4 deletions(-)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 274039e36465..7d567cd20a3f 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3863,10 +3863,23 @@ struct bpf_perf_link {
static void bpf_perf_link_release(struct bpf_link *link)
{
struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
- struct perf_event *event = perf_link->perf_file->private_data;
+ struct perf_event *event;
+ struct file *perf_file;
+
+ /* bpf_perf_link_detach() may have already released perf_file */
+ perf_file = xchg(&perf_link->perf_file, NULL);
+ if (!perf_file)
+ return;
+ event = perf_file->private_data;
perf_event_free_bpf_prog(event);
- fput(perf_link->perf_file);
+ fput(perf_file);
+}
+
+static int bpf_perf_link_detach(struct bpf_link *link)
+{
+ bpf_perf_link_release(link);
+ return 0;
}
static void bpf_perf_link_dealloc(struct bpf_link *link)
@@ -4075,9 +4088,13 @@ static int bpf_perf_link_fill_link_info(const struct bpf_link *link,
{
struct bpf_perf_link *perf_link;
const struct perf_event *event;
+ struct file *perf_file;
perf_link = container_of(link, struct bpf_perf_link, link);
- event = perf_get_event(perf_link->perf_file);
+ perf_file = READ_ONCE(perf_link->perf_file);
+ if (!perf_file)
+ return 0;
+ event = perf_get_event(perf_file);
if (IS_ERR(event))
return PTR_ERR(event);
@@ -4143,9 +4160,13 @@ static void bpf_perf_link_show_fdinfo(const struct bpf_link *link,
{
struct bpf_perf_link *perf_link;
const struct perf_event *event;
+ struct file *perf_file;
perf_link = container_of(link, struct bpf_perf_link, link);
- event = perf_get_event(perf_link->perf_file);
+ perf_file = READ_ONCE(perf_link->perf_file);
+ if (!perf_file)
+ return;
+ event = perf_get_event(perf_file);
if (IS_ERR(event))
return;
@@ -4163,6 +4184,7 @@ static void bpf_perf_link_show_fdinfo(const struct bpf_link *link,
static const struct bpf_link_ops bpf_perf_link_lops = {
.release = bpf_perf_link_release,
+ .detach = bpf_perf_link_detach,
.dealloc = bpf_perf_link_dealloc,
.fill_link_info = bpf_perf_link_fill_link_info,
.show_fdinfo = bpf_perf_link_show_fdinfo,
--
2.53.0
next 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 ` Florian Lehner [this message]
2026-03-04 22:01 ` [PATCH bpf-next v2 1/2] bpf: Add LINK_DETACH support for perf link 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 ` [PATCH bpf-next v2 2/2] selftests/bpf: Test LINK_DETACH " Florian Lehner
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-2-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox