Skip to contentSkip to conversation

bpf: Add LINK_DETACH for iter and perf links

PATCH Superseded Florian Lehner 3 participants 5 messages Aug 1, 2025 – Aug 6, 2025

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Add LINK_DETACH for iter and perf links

73b11c2a introduced LINK_DETACH and implemented it for some link types, like xdp, netns and others.

This patch implements LINK_DETACH for perf and iter links, re-using existing link release handling code.

---
 kernel/bpf/bpf_iter.c | 7 +++++++
 kernel/bpf/syscall.c  | 7 +++++++
 2 files changed, 14 insertions(+)
kernel/bpf/bpf_iter.c +7 -0
index 0cbcae727079..823dad09735d 100644
@@ -397,6 +397,12 @@ static void bpf_iter_link_release(struct bpf_link *link)
 		iter_link->tinfo->reg_info->detach_target(&iter_link->aux);
 }
 
+static int bpf_iter_link_detach(struct bpf_link *link)
+{
+	bpf_iter_link_release(link);
+	return 0;
+}
+
 static void bpf_iter_link_dealloc(struct bpf_link *link)
 {
 	struct bpf_iter_link *iter_link =
@@ -490,6 +496,7 @@ static int bpf_iter_link_fill_link_info(const struct bpf_link *link,
 
 static const struct bpf_link_ops bpf_iter_link_lops = {
 	.release = bpf_iter_link_release,
+	.detach = bpf_iter_link_detach,
 	.dealloc = bpf_iter_link_dealloc,
 	.update_prog = bpf_iter_link_replace,
 	.show_fdinfo = bpf_iter_link_show_fdinfo,
kernel/bpf/syscall.c +7 -0
index e63039817af3..e89694f6874a 100644
@@ -3733,6 +3733,12 @@ static void bpf_perf_link_release(struct bpf_link *link)
 	fput(perf_link->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)
 {
 	struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
@@ -4027,6 +4033,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,
Signature · 1 line
2.50.1
maintainer
#1 lore raw
Quoted 30 lines · Florian Lehner

73b11c2a introduced LINK_DETACH and implemented it for some link types, like xdp, netns and others.

This patch implements LINK_DETACH for perf and iter links, re-using existing link release handling code.

---
  kernel/bpf/bpf_iter.c | 7 +++++++
  kernel/bpf/syscall.c  | 7 +++++++
  2 files changed, 14 insertions(+)
kernel/bpf/bpf_iter.c +6 -0
index 0cbcae727079..823dad09735d 100644
@@ -397,6 +397,12 @@ static void bpf_iter_link_release(struct bpf_link *link)
  		iter_link->tinfo->reg_info->detach_target(&iter_link->aux);
  }
 
+static int bpf_iter_link_detach(struct bpf_link *link)
+{
+	bpf_iter_link_release(link);
+	return 0;
+}
+
  static void bpf_iter_link_dealloc(struct bpf_link *link)
  {
  	struct bpf_iter_link *iter_link =

kernel/bpf/bpf_iter.c static int bpf_iter_link_fill_link_info(const struct bpf_link *link, · line 499

Quoted 4 lines
kernel/bpf/bpf_iter.c +1 -0
@@ -490,6 +496,7 @@ static int bpf_iter_link_fill_link_info(const struct bpf_link *link,
 
  static const struct bpf_link_ops bpf_iter_link_lops = {
  	.release = bpf_iter_link_release,
+	.detach = bpf_iter_link_detach,

Not sure how useful for this one. For bpf_iter programs,
the loaded prog will expect certain bpt_iter (e.g., bpf_map_elem, bpf_map, ...). So even if you have detach, you won't be able to attach to a different bpf_iter flavor.

Do you have a use case for this one?

Quoted 21 lines
kernel/bpf/bpf_iter.c +0 -0
  	.dealloc = bpf_iter_link_dealloc,
  	.update_prog = bpf_iter_link_replace,
  	.show_fdinfo = bpf_iter_link_show_fdinfo,
kernel/bpf/syscall.c +6 -0
index e63039817af3..e89694f6874a 100644
@@ -3733,6 +3733,12 @@ static void bpf_perf_link_release(struct bpf_link *link)
  	fput(perf_link->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)
  {
  	struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);

kernel/bpf/syscall.c static void bpf_perf_link_show_fdinfo(const struct bpf_link *link, · line 4036

Quoted 4 lines
kernel/bpf/syscall.c +1 -0
@@ -4027,6 +4033,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,

This one may be possible. You might be able to e.g., try a different bpf_cookie, or different perf event.

Quoted 3 lines
kernel/bpf/syscall.c +0 -0
  	.dealloc = bpf_perf_link_dealloc,
  	.fill_link_info = bpf_perf_link_fill_link_info,
  	.show_fdinfo = bpf_perf_link_show_fdinfo,
#2 lore raw
Quoted 8 lines · Yonghong Song
Quoted 5 lines · Florian Lehner

73b11c2a introduced LINK_DETACH and implemented it for some link types, like xdp, netns and others.

This patch implements LINK_DETACH for perf and iter links, re-using existing link release handling code.

[..]

Quoted 13 lines

kernel/bpf/bpf_iter.c static int bpf_iter_link_fill_link_info(const struct bpf_link *link, · line 498

Quoted 7 lines
  static void bpf_iter_link_dealloc(struct bpf_link *link)
  {
  	struct bpf_iter_link *iter_link =
kernel/bpf/bpf_iter.c +1 -0
@@ -490,6 +496,7 @@ static int bpf_iter_link_fill_link_info(const struct bpf_link *link,
  static const struct bpf_link_ops bpf_iter_link_lops = {
  	.release = bpf_iter_link_release,
+	.detach = bpf_iter_link_detach,

Not sure how useful for this one. For bpf_iter programs,
the loaded prog will expect certain bpt_iter (e.g., bpf_map_elem, bpf_map, ...). So even if you have detach, you won't be able to attach to a different bpf_iter flavor.

Quoted 2 lines

Do you have a use case for this one?

A key reason for adding this was to enable the temporary disabling and re-enabling of an attached BPF program while keeping the same bpf_iter flavor. If you don't think this is a strong enough use case, I'm open to removing this from the patch.

Quoted 11 lines

kernel/bpf/syscall.c static void bpf_perf_link_show_fdinfo(const struct bpf_link *link, · line 4035

Quoted 7 lines
kernel/bpf/syscall.c +1 -0
  static void bpf_perf_link_dealloc(struct bpf_link *link)
  {
  	struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
@@ -4027,6 +4033,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,

This one may be possible. You might be able to e.g., try a different bpf_cookie, or different perf event.

The primary use case for this feature is to allow for the temporary disabling of uprobes that are attached using bpf_perf_links.

maintainer Acked-by
#3 lore raw
Quoted 37 lines · Florian Lehner
Quoted 7 lines · Yonghong Song
Quoted 5 lines · Florian Lehner

73b11c2a introduced LINK_DETACH and implemented it for some link types, like xdp, netns and others.

This patch implements LINK_DETACH for perf and iter links, re-using existing link release handling code.

[..]

Quoted 12 lines

kernel/bpf/bpf_iter.c static int bpf_iter_link_fill_link_info(const struct bpf_link *link, · line 498

Quoted 7 lines
   static void bpf_iter_link_dealloc(struct bpf_link *link)
   {
   	struct bpf_iter_link *iter_link =
kernel/bpf/bpf_iter.c +1 -0
@@ -490,6 +496,7 @@ static int bpf_iter_link_fill_link_info(const struct bpf_link *link,
   static const struct bpf_link_ops bpf_iter_link_lops = {
   	.release = bpf_iter_link_release,
+	.detach = bpf_iter_link_detach,

Not sure how useful for this one. For bpf_iter programs,
the loaded prog will expect certain bpt_iter (e.g., bpf_map_elem, bpf_map, ...). So even if you have detach, you won't be able to attach to a different bpf_iter flavor.

Quoted 2 lines

Do you have a use case for this one?

A key reason for adding this was to enable the temporary disabling and re-enabling of an attached BPF program while keeping the same bpf_iter flavor. If you don't think this is a strong enough use case, I'm open to removing this from the patch.

Quoted 10 lines

kernel/bpf/syscall.c static void bpf_perf_link_show_fdinfo(const struct bpf_link *link, · line 4035

Quoted 7 lines
kernel/bpf/syscall.c +1 -0
   static void bpf_perf_link_dealloc(struct bpf_link *link)
   {
   	struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
@@ -4027,6 +4033,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,

This one may be possible. You might be able to e.g., try a different bpf_cookie, or different perf event.

Quoted 2 lines

The primary use case for this feature is to allow for the temporary disabling of uprobes that are attached using bpf_perf_links.

Okay, you patch looks good to me.

#4 lore raw
In reply to Florian Lehner
Quoted 43 lines · Florian Lehner
Quoted 8 lines · Yonghong Song
Quoted 5 lines · Florian Lehner

73b11c2a introduced LINK_DETACH and implemented it for some link types, like xdp, netns and others.

This patch implements LINK_DETACH for perf and iter links, re-using existing link release handling code.

[..]

Quoted 13 lines

kernel/bpf/bpf_iter.c static int bpf_iter_link_fill_link_info(const struct bpf_link *link, · line 498

Quoted 7 lines
  static void bpf_iter_link_dealloc(struct bpf_link *link)
  {
    struct bpf_iter_link *iter_link =
kernel/bpf/bpf_iter.c +1 -0
@@ -490,6 +496,7 @@ static int bpf_iter_link_fill_link_info(const struct bpf_link *link,
  static const struct bpf_link_ops bpf_iter_link_lops = {
    .release = bpf_iter_link_release,
+   .detach = bpf_iter_link_detach,

Not sure how useful for this one. For bpf_iter programs,
the loaded prog will expect certain bpt_iter (e.g., bpf_map_elem, bpf_map, ...). So even if you have detach, you won't be able to attach to a different bpf_iter flavor.

Quoted 2 lines

Do you have a use case for this one?

A key reason for adding this was to enable the temporary disabling and re-enabling of an attached BPF program while keeping the same bpf_iter flavor. If you don't think this is a strong enough use case, I'm open to removing this from the patch.

Quoted 11 lines

kernel/bpf/syscall.c static void bpf_perf_link_show_fdinfo(const struct bpf_link *link, · line 4035

Quoted 7 lines
kernel/bpf/syscall.c +1 -0
  static void bpf_perf_link_dealloc(struct bpf_link *link)
  {
    struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
@@ -4027,6 +4033,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,

This one may be possible. You might be able to e.g., try a different bpf_cookie, or different perf event.

Quoted 2 lines

The primary use case for this feature is to allow for the temporary disabling of uprobes that are attached using bpf_perf_links.

I guess the use case makes sense, but pls provide corresponding libbpf and selftest that demonstrates such usage.

--
pw-bot: cr