Skip to contentSkip to conversation

bpf: add bpf_link_new_file that doesn't install FD

PATCH Applied Andrii Nakryiko 4 participants 4 messages Mar 9, 2020 – Mar 11, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: add bpf_link_new_file that doesn't install FD

Add bpf_link_new_file() API for cases when we need to ensure anon_inode is successfully created before we proceed with expensive BPF program attachment procedure, which will require equally (if not more so) expensive and potentially failing compensation detachment procedure just because anon_inode creation failed. This API allows to simplify code by ensuring first that anon_inode is created and after BPF program is attached proceed with fd_install() that can't fail.

After anon_inode file is created, link can't be just kfree()'d anymore, because its destruction will be performed by deferred file_operations->release call. For this, bpf_link API required specifying two separate operations:
release() and dealloc(), former performing detachment only, while the latter frees memory used by bpf_link itself. dealloc() needs to be specified, because struct bpf_link is frequently embedded into link type-specific container struct (e.g., struct bpf_raw_tp_link), so bpf_link itself doesn't know how to properly free the memory. In case when anon_inode file was successfully created, but subsequent BPF attachment failed, bpf_link needs to be marked as "defunct", so that file's release() callback will perform only memory deallocation, but no detachment.

Convert raw tracepoint and tracing attachment to new API and eliminate detachment from error handling path.

---
 include/linux/bpf.h  |   3 ++
 kernel/bpf/syscall.c | 122 +++++++++++++++++++++++++++++++------------
 2 files changed, 91 insertions(+), 34 deletions(-)
include/linux/bpf.h +3 -0
index 94a329b9da81..4fd91b7c95ea 100644
@@ -1070,13 +1070,16 @@ struct bpf_link;
 
 struct bpf_link_ops {
 	void (*release)(struct bpf_link *link);
+	void (*dealloc)(struct bpf_link *link);
 };
 
 void bpf_link_init(struct bpf_link *link, const struct bpf_link_ops *ops,
 		   struct bpf_prog *prog);
+void bpf_link_defunct(struct bpf_link *link);
 void bpf_link_inc(struct bpf_link *link);
 void bpf_link_put(struct bpf_link *link);
 int bpf_link_new_fd(struct bpf_link *link);
+struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 
 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
kernel/bpf/syscall.c +88 -34

202 lines not on this page · raw message

Signature · 1 line
2.17.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 20 lines · Andrii Nakryiko

Add bpf_link_new_file() API for cases when we need to ensure anon_inode is successfully created before we proceed with expensive BPF program attachment procedure, which will require equally (if not more so) expensive and potentially failing compensation detachment procedure just because anon_inode creation failed. This API allows to simplify code by ensuring first that anon_inode is created and after BPF program is attached proceed with fd_install() that can't fail.

After anon_inode file is created, link can't be just kfree()'d anymore, because its destruction will be performed by deferred file_operations->release call. For this, bpf_link API required specifying two separate operations:
release() and dealloc(), former performing detachment only, while the latter frees memory used by bpf_link itself. dealloc() needs to be specified, because struct bpf_link is frequently embedded into link type-specific container struct (e.g., struct bpf_raw_tp_link), so bpf_link itself doesn't know how to properly free the memory. In case when anon_inode file was successfully created, but subsequent BPF attachment failed, bpf_link needs to be marked as "defunct", so that file's release() callback will perform only memory deallocation, but no detachment.

Quoted 5 lines

Convert raw tracepoint and tracing attachment to new API and eliminate detachment from error handling path.

---

LGTM

maintainer Applied
#2 lore raw
In reply to Andrii Nakryiko
Quoted 20 lines · Andrii Nakryiko

Add bpf_link_new_file() API for cases when we need to ensure anon_inode is successfully created before we proceed with expensive BPF program attachment procedure, which will require equally (if not more so) expensive and potentially failing compensation detachment procedure just because anon_inode creation failed. This API allows to simplify code by ensuring first that anon_inode is created and after BPF program is attached proceed with fd_install() that can't fail.

After anon_inode file is created, link can't be just kfree()'d anymore, because its destruction will be performed by deferred file_operations->release call. For this, bpf_link API required specifying two separate operations:
release() and dealloc(), former performing detachment only, while the latter frees memory used by bpf_link itself. dealloc() needs to be specified, because struct bpf_link is frequently embedded into link type-specific container struct (e.g., struct bpf_raw_tp_link), so bpf_link itself doesn't know how to properly free the memory. In case when anon_inode file was successfully created, but subsequent BPF attachment failed, bpf_link needs to be marked as "defunct", so that file's release() callback will perform only memory deallocation, but no detachment.

Quoted 4 lines

Convert raw tracepoint and tracing attachment to new API and eliminate detachment from error handling path.

Applied, but ...

[...]

Quoted 14 lines
kernel/bpf/syscall.c +6 -3
@@ -2337,20 +2374,24 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog)
  	}
  	bpf_link_init(&link->link, &bpf_tracing_link_lops, prog);
 
-	err = bpf_trampoline_link_prog(prog);
-	if (err)
-		goto out_free_link;
+	link_file = bpf_link_new_file(&link->link, &link_fd);
+	if (IS_ERR(link_file)) {
+		kfree(link);
+		err = PTR_ERR(link_file);
+		goto out_put_prog;
+	}
 

kernel/bpf/syscall.c static int bpf_tracing_prog_attach(struct bpf_prog *prog) · line 2388

Quoted 10 lines
kernel/bpf/syscall.c +5 -5
-	link_fd = bpf_link_new_fd(&link->link);
-	if (link_fd < 0) {
-		WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
-		err = link_fd;
-		goto out_free_link;
+	err = bpf_trampoline_link_prog(prog);
+	if (err) {
+		bpf_link_defunct(&link->link);
+		fput(link_file);
+		put_unused_fd(link_fd);

Given the tear-down in error case requires 3 manual steps here, I think this begs for a small helper.

Quoted 6 lines
kernel/bpf/syscall.c +3 -0
+		goto out_put_prog;
  	}
+
+	fd_install(link_fd, link_file);
  	return link_fd;
 

[...]

Quoted 4 lines
kernel/bpf/syscall.c +0 -0
@@ -2431,28 +2481,32 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
  		goto out_put_prog;
  	}
 

[...]

kernel/bpf/syscall.c static int bpf_raw_tracepoint_open(const union bpf_attr *attr) · line 2503

Quoted 11 lines
kernel/bpf/syscall.c +5 -5
-	link_fd = bpf_link_new_fd(&raw_tp->link);
-	if (link_fd < 0) {
-		bpf_probe_unregister(raw_tp->btp, prog);
-		err = link_fd;
-		goto out_free_tp;
+	err = bpf_probe_register(link->btp, prog);
+	if (err) {
+		bpf_link_defunct(&link->link);
+		fput(link_file);
+		put_unused_fd(link_fd);

Especially since you need it in multiple places; please follow-up.

Quoted 12 lines
kernel/bpf/syscall.c +3 -2
+		goto out_put_btp;
  	}
+
+	fd_install(link_fd, link_file);
  	return link_fd;
 
-out_free_tp:
-	kfree(raw_tp);
  out_put_btp:
  	bpf_put_raw_tracepoint(btp);
  out_put_prog:
#3 lore raw
Quoted 44 lines · Daniel Borkmann
Quoted 20 lines · Andrii Nakryiko

Add bpf_link_new_file() API for cases when we need to ensure anon_inode is successfully created before we proceed with expensive BPF program attachment procedure, which will require equally (if not more so) expensive and potentially failing compensation detachment procedure just because anon_inode creation failed. This API allows to simplify code by ensuring first that anon_inode is created and after BPF program is attached proceed with fd_install() that can't fail.

After anon_inode file is created, link can't be just kfree()'d anymore, because its destruction will be performed by deferred file_operations->release call. For this, bpf_link API required specifying two separate operations:
release() and dealloc(), former performing detachment only, while the latter frees memory used by bpf_link itself. dealloc() needs to be specified, because struct bpf_link is frequently embedded into link type-specific container struct (e.g., struct bpf_raw_tp_link), so bpf_link itself doesn't know how to properly free the memory. In case when anon_inode file was successfully created, but subsequent BPF attachment failed, bpf_link needs to be marked as "defunct", so that file's release() callback will perform only memory deallocation, but no detachment.

Quoted 4 lines

Convert raw tracepoint and tracing attachment to new API and eliminate detachment from error handling path.

Applied, but ...

[...]

Quoted 14 lines
kernel/bpf/syscall.c +6 -3
@@ -2337,20 +2374,24 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog)
      }
      bpf_link_init(&link->link, &bpf_tracing_link_lops, prog);
 
-     err = bpf_trampoline_link_prog(prog);
-     if (err)
-             goto out_free_link;
+     link_file = bpf_link_new_file(&link->link, &link_fd);
+     if (IS_ERR(link_file)) {
+             kfree(link);
+             err = PTR_ERR(link_file);
+             goto out_put_prog;
+     }
 
Quoted 13 lines

kernel/bpf/syscall.c static int bpf_tracing_prog_attach(struct bpf_prog *prog) · line 2388

Quoted 10 lines
kernel/bpf/syscall.c +5 -5
-     link_fd = bpf_link_new_fd(&link->link);
-     if (link_fd < 0) {
-             WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
-             err = link_fd;
-             goto out_free_link;
+     err = bpf_trampoline_link_prog(prog);
+     if (err) {
+             bpf_link_defunct(&link->link);
+             fput(link_file);
+             put_unused_fd(link_fd);

Given the tear-down in error case requires 3 manual steps here, I think this begs for a small helper.

Sounds good, will follow up. Thanks for applying!

Quoted 40 lines
Quoted 7 lines
kernel/bpf/syscall.c +3 -0
+             goto out_put_prog;
      }
+
+     fd_install(link_fd, link_file);
      return link_fd;
 

[...]

Quoted 4 lines
kernel/bpf/syscall.c +0 -0
@@ -2431,28 +2481,32 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
              goto out_put_prog;
      }
 

[...]

kernel/bpf/syscall.c static int bpf_raw_tracepoint_open(const union bpf_attr *attr) · line 2503

Quoted 11 lines
kernel/bpf/syscall.c +5 -5
-     link_fd = bpf_link_new_fd(&raw_tp->link);
-     if (link_fd < 0) {
-             bpf_probe_unregister(raw_tp->btp, prog);
-             err = link_fd;
-             goto out_free_tp;
+     err = bpf_probe_register(link->btp, prog);
+     if (err) {
+             bpf_link_defunct(&link->link);
+             fput(link_file);
+             put_unused_fd(link_fd);

Especially since you need it in multiple places; please follow-up.

Quoted 12 lines
kernel/bpf/syscall.c +3 -2
+             goto out_put_btp;
      }
+
+     fd_install(link_fd, link_file);
      return link_fd;
 
-out_free_tp:
-     kfree(raw_tp);
  out_put_btp:
      bpf_put_raw_tracepoint(btp);
  out_put_prog: