Skip to contentSkip to conversation

BPF link force-detach support

PATCH Superseded Andrii Nakryiko 4 participants 19 messages Jul 29, 2020 – Aug 10, 2020

Subsystems (9)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover BPF link force-detach support

This patch set adds new BPF link operation, LINK_DETACH, allowing processes with BPF link FD to force-detach it from respective BPF hook, similarly how BPF link is auto-detached when such BPF hook (e.g., cgroup, net_device, netns, etc) is removed. This facility allows admin to forcefully undo BPF link attachment, while process that created BPF link in the first place is left intact.

Once force-detached, BPF link stays valid in the kernel as long as there is at least one FD open against it. It goes into defunct state, just like auto-detached BPF link.

bpftool also got link detach command to allow triggering this in non-programmatic fashion.

Cc Toke Høiland-Jørgensen <[email protected]>

Andrii Nakryiko (5):
bpf: add support for forced LINK_DETACH command
libbpf: add bpf_link detach APIs
selftests/bpf: add link detach tests for cgroup, netns, and xdp
bpf_links
tools/bpftool: add link detach subcommand
tools/bpftool: add documentation and bash-completion for link detach

 include/linux/bpf.h                           |  1 +
 include/uapi/linux/bpf.h                      |  5 ++
 kernel/bpf/cgroup.c                           | 15 +++++-
 kernel/bpf/net_namespace.c                    |  8 +++
 kernel/bpf/syscall.c                          | 26 ++++++++++
 net/core/dev.c                                | 11 +++-
 .../bpftool/Documentation/bpftool-link.rst    |  8 +++
 tools/bpf/bpftool/bash-completion/bpftool     |  4 +-
 tools/bpf/bpftool/link.c                      | 35 ++++++++++++-
 tools/include/uapi/linux/bpf.h                |  5 ++
 tools/lib/bpf/bpf.c                           | 10 ++++
 tools/lib/bpf/bpf.h                           |  2 +
 tools/lib/bpf/libbpf.c                        |  5 ++
 tools/lib/bpf/libbpf.h                        |  1 +
 tools/lib/bpf/libbpf.map                      |  2 +
 .../selftests/bpf/prog_tests/cgroup_link.c    | 20 +++++++-
 .../selftests/bpf/prog_tests/sk_lookup.c      | 51 +++++++++----------
 .../selftests/bpf/prog_tests/xdp_link.c       | 14 +++++
 tools/testing/selftests/bpf/testing_helpers.c | 14 +++++
 tools/testing/selftests/bpf/testing_helpers.h |  3 ++
 20 files changed, 206 insertions(+), 34 deletions(-)
Signature · 1 line
2.24.1
#1 lore raw

PATCH 1/5 bpf: add support for forced LINK_DETACH command

Add LINK_DETACH command to force-detach bpf_link without destroying it. It has the same behavior as auto-detaching of bpf_link due to cgroup dying for bpf_cgroup_link or net_device being destroyed for bpf_xdp_link. In such case, bpf_link is still a valid kernel object, but is defuncts and doesn't hold BPF program attached to corresponding BPF hook. This functionality allows users with enough access rights to manually force-detach attached bpf_link without killing respective owner process.

This patch implements LINK_DETACH for cgroup, xdp, and netns links, mostly re-using existing link release handling code.

---
 include/linux/bpf.h        |  1 +
 include/uapi/linux/bpf.h   |  5 +++++
 kernel/bpf/cgroup.c        | 15 ++++++++++++++-
 kernel/bpf/net_namespace.c |  8 ++++++++
 kernel/bpf/syscall.c       | 26 ++++++++++++++++++++++++++
 net/core/dev.c             | 11 ++++++++++-
 6 files changed, 64 insertions(+), 2 deletions(-)
include/linux/bpf.h +1 -0
index 40c5e206ecf2..cef4ef0d2b4e 100644
@@ -793,6 +793,7 @@ struct bpf_link {
 struct bpf_link_ops {
 	void (*release)(struct bpf_link *link);
 	void (*dealloc)(struct bpf_link *link);
+	int (*detach)(struct bpf_link *link);
 	int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
 			   struct bpf_prog *old_prog);
 	void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
include/uapi/linux/bpf.h +5 -0
index eb5e0c38eb2c..b134e679e9db 100644
@@ -117,6 +117,7 @@ enum bpf_cmd {
 	BPF_LINK_GET_NEXT_ID,
 	BPF_ENABLE_STATS,
 	BPF_ITER_CREATE,
+	BPF_LINK_DETACH,
 };
 
 enum bpf_map_type {
@@ -634,6 +635,10 @@ union bpf_attr {
 		__u32		old_prog_fd;
 	} link_update;
 
+	struct {
+		__u32		link_fd;
+	} link_detach;
+
 	struct { /* struct used by BPF_ENABLE_STATS command */
 		__u32		type;
 	} enable_stats;
kernel/bpf/cgroup.c +14 -1
index 957cce1d5168..83ff127ef7ae 100644
@@ -814,6 +814,7 @@ static void bpf_cgroup_link_release(struct bpf_link *link)
 {
 	struct bpf_cgroup_link *cg_link =
 		container_of(link, struct bpf_cgroup_link, link);
+	struct cgroup *cg;
 
 	/* link might have been auto-detached by dying cgroup already,
 	 * in that case our work is done here
@@ -832,8 +833,12 @@ static void bpf_cgroup_link_release(struct bpf_link *link)
 	WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link,
 				    cg_link->type));
 
+	cg = cg_link->cgroup;
+	cg_link->cgroup = NULL;
+
 	mutex_unlock(&cgroup_mutex);
-	cgroup_put(cg_link->cgroup);
+
+	cgroup_put(cg);
 }
 
 static void bpf_cgroup_link_dealloc(struct bpf_link *link)
@@ -844,6 +849,13 @@ static void bpf_cgroup_link_dealloc(struct bpf_link *link)
 	kfree(cg_link);
 }
 
+static int bpf_cgroup_link_detach(struct bpf_link *link)
+{
+	bpf_cgroup_link_release(link);
+
+	return 0;
+}
+
 static void bpf_cgroup_link_show_fdinfo(const struct bpf_link *link,
 					struct seq_file *seq)
 {
@@ -883,6 +895,7 @@ static int bpf_cgroup_link_fill_link_info(const struct bpf_link *link,
 static const struct bpf_link_ops bpf_cgroup_link_lops = {
 	.release = bpf_cgroup_link_release,
 	.dealloc = bpf_cgroup_link_dealloc,
+	.detach = bpf_cgroup_link_detach,
 	.update_prog = cgroup_bpf_replace,
 	.show_fdinfo = bpf_cgroup_link_show_fdinfo,
 	.fill_link_info = bpf_cgroup_link_fill_link_info,
kernel/bpf/net_namespace.c +8 -0
index 71405edd667c..542f275bf252 100644
@@ -142,9 +142,16 @@ static void bpf_netns_link_release(struct bpf_link *link)
 	bpf_prog_array_free(old_array);
 
 out_unlock:
+	net_link->net = NULL;
 	mutex_unlock(&netns_bpf_mutex);
 }
 
+static int bpf_netns_link_detach(struct bpf_link *link)
+{
+	bpf_netns_link_release(link);
+	return 0;
+}
+
 static void bpf_netns_link_dealloc(struct bpf_link *link)
 {
 	struct bpf_netns_link *net_link =
@@ -228,6 +235,7 @@ static void bpf_netns_link_show_fdinfo(const struct bpf_link *link,
 static const struct bpf_link_ops bpf_netns_link_ops = {
 	.release = bpf_netns_link_release,
 	.dealloc = bpf_netns_link_dealloc,
+	.detach = bpf_netns_link_detach,
 	.update_prog = bpf_netns_link_update_prog,
 	.fill_link_info = bpf_netns_link_fill_info,
 	.show_fdinfo = bpf_netns_link_show_fdinfo,
kernel/bpf/syscall.c +26 -0
index cd3d599e9e90..2f343ce15747 100644
@@ -3991,6 +3991,29 @@ static int link_update(union bpf_attr *attr)
 	return ret;
 }
 
+#define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
+
+static int link_detach(union bpf_attr *attr)
+{
+	struct bpf_link *link;
+	int ret;
+
+	if (CHECK_ATTR(BPF_LINK_DETACH))
+		return -EINVAL;
+
+	link = bpf_link_get_from_fd(attr->link_detach.link_fd);
+	if (IS_ERR(link))
+		return PTR_ERR(link);
+
+	if (link->ops->detach)
+		ret = link->ops->detach(link);
+	else
+		ret = -EOPNOTSUPP;
+
+	bpf_link_put(link);
+	return ret;
+}
+
 static int bpf_link_inc_not_zero(struct bpf_link *link)
 {
 	return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? 0 : -ENOENT;
@@ -4240,6 +4263,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
 	case BPF_ITER_CREATE:
 		err = bpf_iter_create(&attr);
 		break;
+	case BPF_LINK_DETACH:
+		err = link_detach(&attr);
+		break;
 	default:
 		err = -EINVAL;
 		break;
net/core/dev.c +10 -1
index a2a57988880a..c8b911b10187 100644
@@ -8979,12 +8979,20 @@ static void bpf_xdp_link_release(struct bpf_link *link)
 	/* if racing with net_device's tear down, xdp_link->dev might be
 	 * already NULL, in which case link was already auto-detached
 	 */
-	if (xdp_link->dev)
+	if (xdp_link->dev) {
 		WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link));
+		xdp_link->dev = NULL;
+	}
 
 	rtnl_unlock();
 }
 
+static int bpf_xdp_link_detach(struct bpf_link *link)
+{
+	bpf_xdp_link_release(link);
+	return 0;
+}
+
 static void bpf_xdp_link_dealloc(struct bpf_link *link)
 {
 	struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
@@ -9066,6 +9074,7 @@ static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
 static const struct bpf_link_ops bpf_xdp_link_lops = {
 	.release = bpf_xdp_link_release,
 	.dealloc = bpf_xdp_link_dealloc,
+	.detach = bpf_xdp_link_detach,
 	.show_fdinfo = bpf_xdp_link_show_fdinfo,
 	.fill_link_info = bpf_xdp_link_fill_link_info,
 	.update_prog = bpf_xdp_link_update,
Signature · 1 line
2.24.1
#2 lore raw

Re: [PATCH bpf-next 1/5] bpf: add support for forced LINK_DETACH command

Quoted 12 lines · Andrii Nakryiko

Add LINK_DETACH command to force-detach bpf_link without destroying it. It has the same behavior as auto-detaching of bpf_link due to cgroup dying for bpf_cgroup_link or net_device being destroyed for bpf_xdp_link. In such case, bpf_link is still a valid kernel object, but is defuncts and doesn't hold BPF program attached to corresponding BPF hook. This functionality allows users with enough access rights to manually force-detach attached bpf_link without killing respective owner process.

Quoted 4 lines

This patch implements LINK_DETACH for cgroup, xdp, and netns links, mostly re-using existing link release handling code.

The code looks good to me. My only question is, do we need both bpf_link_ops->detach and bpf_link_ops->release?

Thanks,
Song

[...]

#3 lore raw

Re: [PATCH bpf-next 1/5] bpf: add support for forced LINK_DETACH command

Quoted 13 lines · Song Liu
Quoted 13 lines · Andrii Nakryiko

Add LINK_DETACH command to force-detach bpf_link without destroying it. It has the same behavior as auto-detaching of bpf_link due to cgroup dying for bpf_cgroup_link or net_device being destroyed for bpf_xdp_link. In such case, bpf_link is still a valid kernel object, but is defuncts and doesn't hold BPF program attached to corresponding BPF hook. This functionality allows users with enough access rights to manually force-detach attached bpf_link without killing respective owner process.

Quoted 7 lines
Quoted 4 lines

This patch implements LINK_DETACH for cgroup, xdp, and netns links, mostly re-using existing link release handling code.

The code looks good to me. My only question is, do we need both bpf_link_ops->detach and bpf_link_ops->release?

I think so. release() is mandatory for final clean up, after the last FD was closed, so every type of bpf_link has to implement this. detach() is optional, though, and potentially can do different things than release(). It just so happens right now that three bpf_linkl types can re-use release as-is (with minimal change to netns release specifically for detach use case). So I think having two is better and more flexible.

Quoted 5 lines

Thanks,
Song

[...]

Acked-by
#4 lore raw

Re: [PATCH bpf-next 1/5] bpf: add support for forced LINK_DETACH command

Quoted 26 lines · Andrii Nakryiko
Quoted 13 lines · Song Liu
Quoted 13 lines · Andrii Nakryiko

Add LINK_DETACH command to force-detach bpf_link without destroying it. It has the same behavior as auto-detaching of bpf_link due to cgroup dying for bpf_cgroup_link or net_device being destroyed for bpf_xdp_link. In such case, bpf_link is still a valid kernel object, but is defuncts and doesn't hold BPF program attached to corresponding BPF hook. This functionality allows users with enough access rights to manually force-detach attached bpf_link without killing respective owner process.

Quoted 7 lines
Quoted 4 lines

This patch implements LINK_DETACH for cgroup, xdp, and netns links, mostly re-using existing link release handling code.

The code looks good to me. My only question is, do we need both bpf_link_ops->detach and bpf_link_ops->release?

Quoted 7 lines

I think so. release() is mandatory for final clean up, after the last FD was closed, so every type of bpf_link has to implement this. detach() is optional, though, and potentially can do different things than release(). It just so happens right now that three bpf_linkl types can re-use release as-is (with minimal change to netns release specifically for detach use case). So I think having two is better and more flexible.

I see. Thanks for the explanation.

#5 lore raw

PATCH 2/5 libbpf: add bpf_link detach APIs

In reply to Andrii Nakryiko

Add low-level bpf_link_detach() API. Also add higher-level bpf_link__detach() one.

---
 tools/include/uapi/linux/bpf.h |  5 +++++
 tools/lib/bpf/bpf.c            | 10 ++++++++++
 tools/lib/bpf/bpf.h            |  2 ++
 tools/lib/bpf/libbpf.c         |  5 +++++
 tools/lib/bpf/libbpf.h         |  1 +
 tools/lib/bpf/libbpf.map       |  2 ++
 6 files changed, 25 insertions(+)
tools/include/uapi/linux/bpf.h +5 -0
index eb5e0c38eb2c..b134e679e9db 100644
@@ -117,6 +117,7 @@ enum bpf_cmd {
 	BPF_LINK_GET_NEXT_ID,
 	BPF_ENABLE_STATS,
 	BPF_ITER_CREATE,
+	BPF_LINK_DETACH,
 };
 
 enum bpf_map_type {
@@ -634,6 +635,10 @@ union bpf_attr {
 		__u32		old_prog_fd;
 	} link_update;
 
+	struct {
+		__u32		link_fd;
+	} link_detach;
+
 	struct { /* struct used by BPF_ENABLE_STATS command */
 		__u32		type;
 	} enable_stats;
tools/lib/bpf/bpf.c +10 -0
index e1bdf214f75f..eab14c97c15d 100644
@@ -603,6 +603,16 @@ int bpf_link_create(int prog_fd, int target_fd,
 	return sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
 }
 
+int bpf_link_detach(int link_fd)
+{
+	union bpf_attr attr;
+
+	memset(&attr, 0, sizeof(attr));
+	attr.link_detach.link_fd = link_fd;
+
+	return sys_bpf(BPF_LINK_DETACH, &attr, sizeof(attr));
+}
+
 int bpf_link_update(int link_fd, int new_prog_fd,
 		    const struct bpf_link_update_opts *opts)
 {
tools/lib/bpf/bpf.h +2 -0
index 6d367e01d05e..28855fd5b5f4 100644
@@ -178,6 +178,8 @@ LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
 			       enum bpf_attach_type attach_type,
 			       const struct bpf_link_create_opts *opts);
 
+LIBBPF_API int bpf_link_detach(int link_fd);
+
 struct bpf_link_update_opts {
 	size_t sz; /* size of this struct for forward/backward compatibility */
 	__u32 flags;	   /* extra flags */
tools/lib/bpf/libbpf.c +5 -0
index 54830d603fee..6c5c14f639e8 100644
@@ -7748,6 +7748,11 @@ struct bpf_link *bpf_link__open(const char *path)
 	return link;
 }
 
+int bpf_link__detach(struct bpf_link *link)
+{
+	return bpf_link_detach(link->fd) ? -errno : 0;
+}
+
 int bpf_link__pin(struct bpf_link *link, const char *path)
 {
 	int err;
tools/lib/bpf/libbpf.h +1 -0
index 9924385462ab..3ed1399bfbbc 100644
@@ -229,6 +229,7 @@ LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
 					struct bpf_program *prog);
 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
+LIBBPF_API int bpf_link__detach(struct bpf_link *link);
 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
 
 LIBBPF_API struct bpf_link *
tools/lib/bpf/libbpf.map +2 -0
index ca49a6a7e5b2..099863411f7d 100644
@@ -273,6 +273,8 @@ LIBBPF_0.0.9 {
 
 LIBBPF_0.1.0 {
 	global:
+		bpf_link__detach;
+		bpf_link_detach;
 		bpf_map__ifindex;
 		bpf_map__key_size;
 		bpf_map__map_flags;
Signature · 1 line
2.24.1
Acked-by on v1 2/5
#6 lore raw

Re: [PATCH bpf-next 2/5] libbpf: add bpf_link detach APIs

Quoted 8 lines · Andrii Nakryiko

Add low-level bpf_link_detach() API. Also add higher-level bpf_link__detach() one.

Quoted 2 lines

---

[...]

tools/lib/bpf/libbpf.map LIBBPF_0.0.9 { · line 277

Quoted 5 lines

LIBBPF_0.1.0 {
global:

tools/lib/bpf/libbpf.map +2 -0
+		bpf_link__detach;
+		bpf_link_detach;

I didn't realize capital_letter < '_' < small_letter until just now. :)

Quoted 6 lines
		bpf_map__ifindex;
		bpf_map__key_size;
		bpf_map__map_flags;
Signature · 1 line
2.24.1
#7 lore raw

PATCH 3/5 selftests/bpf: add link detach tests for cgroup, netns, and xdp bpf_links

In reply to Andrii Nakryiko

Add bpf_link__detach() testing to selftests for cgroup, netns, and xdp bpf_links.

---
 .../selftests/bpf/prog_tests/cgroup_link.c    | 20 +++++++-
 .../selftests/bpf/prog_tests/sk_lookup.c      | 51 +++++++++----------
 .../selftests/bpf/prog_tests/xdp_link.c       | 14 +++++
 tools/testing/selftests/bpf/testing_helpers.c | 14 +++++
 tools/testing/selftests/bpf/testing_helpers.h |  3 ++
 5 files changed, 73 insertions(+), 29 deletions(-)
tools/testing/selftests/bpf/prog_tests/cgroup_link.c +19 -1
index 6e04f8d1d15b..4d9b514b3fd9 100644
@@ -2,6 +2,7 @@
 
 #include <test_progs.h>
 #include "cgroup_helpers.h"
+#include "testing_helpers.h"
 #include "test_cgroup_link.skel.h"
 
 static __u32 duration = 0;
@@ -37,7 +38,8 @@ void test_cgroup_link(void)
 	int last_cg = ARRAY_SIZE(cgs) - 1, cg_nr = ARRAY_SIZE(cgs);
 	DECLARE_LIBBPF_OPTS(bpf_link_update_opts, link_upd_opts);
 	struct bpf_link *links[ARRAY_SIZE(cgs)] = {}, *tmp_link;
-	__u32 prog_ids[ARRAY_SIZE(cgs)], prog_cnt = 0, attach_flags;
+	__u32 prog_ids[ARRAY_SIZE(cgs)], prog_cnt = 0, attach_flags, prog_id;
+	struct bpf_link_info info;
 	int i = 0, err, prog_fd;
 	bool detach_legacy = false;
 
@@ -219,6 +221,22 @@ void test_cgroup_link(void)
 	/* BPF programs should still get called */
 	ping_and_check(0, cg_nr);
 
+	prog_id = link_info_prog_id(links[0], &info);
+	CHECK(prog_id == 0, "link_info", "failed\n");
+	CHECK(info.cgroup.cgroup_id == 0, "cgroup_id", "unexpected %llu\n", info.cgroup.cgroup_id);
+
+	err = bpf_link__detach(links[0]);
+	if (CHECK(err, "link_detach", "failed %d\n", err))
+		goto cleanup;
+
+	/* cgroup_id should be zero in link_info */
+	prog_id = link_info_prog_id(links[0], &info);
+	CHECK(prog_id == 0, "link_info", "failed\n");
+	CHECK(info.cgroup.cgroup_id != 0, "cgroup_id", "unexpected %llu\n", info.cgroup.cgroup_id);
+
+	/* First BPF program shouldn't be called anymore */
+	ping_and_check(0, cg_nr - 1);
+
 	/* leave cgroup and remove them, don't detach programs */
 	cleanup_cgroup_environment();
 
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +23 -28

81 lines not on this page · raw message

tools/testing/selftests/bpf/prog_tests/xdp_link.c +14 -0
index 52cba6795d40..6f814999b395 100644
@@ -131,6 +131,20 @@ void test_xdp_link(void)
 	CHECK(link_info.xdp.ifindex != IFINDEX_LO, "link_ifindex",
 	      "got %u != exp %u\n", link_info.xdp.ifindex, IFINDEX_LO);
 
+	err = bpf_link__detach(link);
+	if (CHECK(err, "link_detach", "failed %d\n", err))
+		goto cleanup;
+
+	memset(&link_info, 0, sizeof(link_info));
+	err = bpf_obj_get_info_by_fd(bpf_link__fd(link), &link_info, &link_info_len);
+	if (CHECK(err, "link_info", "failed: %d\n", err))
+		goto cleanup;
+	CHECK(link_info.prog_id != id1, "link_prog_id",
+	      "got %u != exp %u\n", link_info.prog_id, id1);
+	/* ifindex should be zeroed out */
+	CHECK(link_info.xdp.ifindex != 0, "link_ifindex",
+	      "got %u != exp %u\n", link_info.xdp.ifindex, 0);
+
 cleanup:
 	test_xdp_link__destroy(skel1);
 	test_xdp_link__destroy(skel2);
tools/testing/selftests/bpf/testing_helpers.c +14 -0
index 0af6337a8962..800d503e5cb4 100644
@@ -64,3 +64,17 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
 
 	return 0;
 }
+
+__u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info)
+{
+	__u32 info_len = sizeof(*info);
+	int err;
+
+	memset(info, 0, sizeof(*info));
+	err = bpf_obj_get_info_by_fd(bpf_link__fd(link), info, &info_len);
+	if (err) {
+		printf("failed to get link info: %d\n", -errno);
+		return 0;
+	}
+	return info->prog_id;
+}
tools/testing/selftests/bpf/testing_helpers.h +3 -0
index 923b51762759..d4f8e749611b 100644
@@ -1,5 +1,8 @@
 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
 /* Copyright (C) 2020 Facebook, Inc. */
 #include <stdbool.h>
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
 
 int parse_num_list(const char *s, bool **set, int *set_len);
+__u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info);
Signature · 1 line
2.24.1
#9 lore raw

PATCH 4/5 tools/bpftool: add `link detach` subcommand

In reply to Andrii Nakryiko

Add ability to force-detach BPF link. Also add missing error message, if specified link ID is wrong.

---
 tools/bpf/bpftool/link.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
tools/bpf/bpftool/link.c +34 -1
index 326b8fdf0243..278befa34ed6 100644
@@ -22,6 +22,8 @@ static const char * const link_type_name[] = {
 
 static int link_parse_fd(int *argc, char ***argv)
 {
+	int fd;
+
 	if (is_prefix(**argv, "id")) {
 		unsigned int id;
 		char *endptr;
@@ -35,7 +37,10 @@ static int link_parse_fd(int *argc, char ***argv)
 		}
 		NEXT_ARGP();
 
-		return bpf_link_get_fd_by_id(id);
+		fd = bpf_link_get_fd_by_id(id);
+		if (fd < 0)
+			p_err("failed to get link with ID %d: %d", id, -errno);
+		return fd;
 	} else if (is_prefix(**argv, "pinned")) {
 		char *path;
 
@@ -316,6 +321,32 @@ static int do_pin(int argc, char **argv)
 	return err;
 }
 
+static int do_detach(int argc, char **argv)
+{
+	int err, fd;
+
+	if (argc != 2)
+		return BAD_ARG();
+
+	fd = link_parse_fd(&argc, &argv);
+	if (fd < 0)
+		return 1;
+
+	err = bpf_link_detach(fd);
+	if (err)
+		err = -errno;
+	close(fd);
+	if (err) {
+		p_err("failed link detach: %d", err);
+		return 1;
+	}
+
+	if (json_output)
+		jsonw_null(json_wtr);
+
+	return 0;
+}
+
 static int do_help(int argc, char **argv)
 {
 	if (json_output) {
@@ -326,6 +357,7 @@ static int do_help(int argc, char **argv)
 	fprintf(stderr,
 		"Usage: %1$s %2$s { show | list }   [LINK]\n"
 		"       %1$s %2$s pin        LINK  FILE\n"
+		"       %1$s %2$s detach     LINK\n"
 		"       %1$s %2$s help\n"
 		"\n"
 		"       " HELP_SPEC_LINK "\n"
@@ -341,6 +373,7 @@ static const struct cmd cmds[] = {
 	{ "list",	do_show },
 	{ "help",	do_help },
 	{ "pin",	do_pin },
+	{ "detach",	do_detach },
 	{ 0 }
 };
 
Signature · 1 line
2.24.1
Acked-by on v1 4/5
#10 lore raw

Re: [PATCH bpf-next 4/5] tools/bpftool: add `link detach` subcommand

Quoted 8 lines · Andrii Nakryiko

Add ability to force-detach BPF link. Also add missing error message, if specified link ID is wrong.

With two nitpicks below.

[...]

Quoted 8 lines

static int link_parse_fd(int *argc, char ***argv)
{

tools/bpf/bpftool/link.c +2 -0
+	int fd;
+
	if (is_prefix(**argv, "id")) {
		unsigned int id;
		char *endptr;

tools/bpf/bpftool/link.c static int link_parse_fd(int *argc, char ***argv) · line 42

Quoted 7 lines
tools/bpf/bpftool/link.c +3 -1
@@ -35,7 +37,10 @@ static int link_parse_fd(int *argc, char ***argv)
 		}
 		NEXT_ARGP();
 
-		return bpf_link_get_fd_by_id(id);
+		fd = bpf_link_get_fd_by_id(id);
+		if (fd < 0)
+			p_err("failed to get link with ID %d: %d", id, -errno);

How about we print strerror(errno) to match the rest of link.c?

[...]

Quoted 7 lines
tools/bpf/bpftool/link.c +7 -0
+static int do_detach(int argc, char **argv)
+{
+	int err, fd;
+
+	if (argc != 2)
+		return BAD_ARG();
+

tools/bpf/bpftool/link.c static int do_pin(int argc, char **argv) · line 340

Quoted 10 lines
tools/bpf/bpftool/link.c +10 -0
+	fd = link_parse_fd(&argc, &argv);
+	if (fd < 0)
+		return 1;
+
+	err = bpf_link_detach(fd);
+	if (err)
+		err = -errno;
+	close(fd);
+	if (err) {
+		p_err("failed link detach: %d", err);

And strerror(err) here.

#11 lore raw

Re: [PATCH bpf-next 4/5] tools/bpftool: add `link detach` subcommand

Quoted 27 lines · Song Liu
Quoted 9 lines · Andrii Nakryiko

Add ability to force-detach BPF link. Also add missing error message, if specified link ID is wrong.

With two nitpicks below.

[...]

Quoted 11 lines

static int link_parse_fd(int *argc, char ***argv)
{

tools/bpf/bpftool/link.c +2 -0
+     int fd;
+
      if (is_prefix(**argv, "id")) {
              unsigned int id;
              char *endptr;
@@ -35,7 +37,10 @@ static int link_parse_fd(int *argc, char ***argv)
              }
              NEXT_ARGP();
 
Quoted 6 lines

tools/bpf/bpftool/link.c static int link_parse_fd(int *argc, char ***argv) · line 42

Quoted 4 lines
tools/bpf/bpftool/link.c +3 -1
-             return bpf_link_get_fd_by_id(id);
+             fd = bpf_link_get_fd_by_id(id);
+             if (fd < 0)
+                     p_err("failed to get link with ID %d: %d", id, -errno);

How about we print strerror(errno) to match the rest of link.c?

sure, will do, was lazy :)

Quoted 24 lines

[...]

Quoted 7 lines
tools/bpf/bpftool/link.c +7 -0
+static int do_detach(int argc, char **argv)
+{
+     int err, fd;
+
+     if (argc != 2)
+             return BAD_ARG();
+

tools/bpf/bpftool/link.c static int do_pin(int argc, char **argv) · line 340

Quoted 10 lines
tools/bpf/bpftool/link.c +10 -0
+     fd = link_parse_fd(&argc, &argv);
+     if (fd < 0)
+             return 1;
+
+     err = bpf_link_detach(fd);
+     if (err)
+             err = -errno;
+     close(fd);
+     if (err) {
+             p_err("failed link detach: %d", err);

And strerror(err) here.

#12 lore raw

PATCH 5/5 tools/bpftool: add documentation and bash-completion for `link detach`

In reply to Andrii Nakryiko

Add info on link detach sub-command to man page. Add detach to bash-completion as well.

---
 tools/bpf/bpftool/Documentation/bpftool-link.rst | 8 ++++++++
 tools/bpf/bpftool/bash-completion/bpftool        | 4 ++--
 2 files changed, 10 insertions(+), 2 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-link.rst +8 -0
index 38b0949a185b..4a52e7a93339 100644
@@ -21,6 +21,7 @@ LINK COMMANDS
 
 |	**bpftool** **link { show | list }** [*LINK*]
 |	**bpftool** **link pin** *LINK* *FILE*
+|	**bpftool** **link detach *LINK*
 |	**bpftool** **link help**
 |
 |	*LINK* := { **id** *LINK_ID* | **pinned** *FILE* }
@@ -49,6 +50,13 @@ DESCRIPTION
 		  contain a dot character ('.'), which is reserved for future
 		  extensions of *bpffs*.
 
+	**bpftool link detach** *LINK*
+		  Force-detach link *LINK*. BPF link and its underlying BPF
+		  program will stay valid, but they will be detached from the
+		  respective BPF hook and BPF link will transition into
+		  a defunct state until last open file descriptor for that
+		  link is closed.
+
 	**bpftool link help**
 		  Print short help message.
 
tools/bpf/bpftool/bash-completion/bpftool +2 -2
index 257fa310ea2b..f53ed2f1a4aa 100644
@@ -1122,7 +1122,7 @@ _bpftool()
             ;;
         link)
             case $command in
-                show|list|pin)
+                show|list|pin|detach)
                     case $prev in
                         id)
                             _bpftool_get_link_ids
@@ -1139,7 +1139,7 @@ _bpftool()
                     COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
                     return 0
                     ;;
-                pin)
+                pin|detach)
                     if [[ $prev == "$command" ]]; then
                         COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
                     else
Signature · 1 line
2.24.1
Acked-by on v1 5/5
#13 lore raw

Re: [PATCH bpf-next 5/5] tools/bpftool: add documentation and bash-completion for `link detach`

Quoted 8 lines · Andrii Nakryiko

Add info on link detach sub-command to man page. Add detach to bash-completion as well.

With one nitpick below.

Quoted 1 line

---

[...]

tools/bpf/bpftool/Documentation/bpftool-link.rst DESCRIPTION · line 57

Quoted 9 lines
tools/bpf/bpftool/Documentation/bpftool-link.rst +5 -0
@@ -49,6 +50,13 @@ DESCRIPTION
 		  contain a dot character ('.'), which is reserved for future
 		  extensions of *bpffs*.
 
+	**bpftool link detach** *LINK*
+		  Force-detach link *LINK*. BPF link and its underlying BPF
+		  program will stay valid, but they will be detached from the
+		  respective BPF hook and BPF link will transition into
+		  a defunct state until last open file descriptor for that

Shall we say "a defunct state when the last open file descriptor for that..."?

Quoted 5 lines
tools/bpf/bpftool/Documentation/bpftool-link.rst +2 -0
+		  link is closed.
+
 	**bpftool link help**
 		  Print short help message.
 

[...]

#14 lore raw

Re: [PATCH bpf-next 5/5] tools/bpftool: add documentation and bash-completion for `link detach`

Quoted 22 lines · Song Liu
Quoted 9 lines · Andrii Nakryiko

Add info on link detach sub-command to man page. Add detach to bash-completion as well.

With one nitpick below.

Quoted 1 line

---

[...]

Quoted 4 lines
tools/bpf/bpftool/Documentation/bpftool-link.rst +0 -0
@@ -49,6 +50,13 @@ DESCRIPTION
                contain a dot character ('.'), which is reserved for future
                extensions of *bpffs*.
 
Quoted 7 lines

tools/bpf/bpftool/Documentation/bpftool-link.rst DESCRIPTION · line 57

Quoted 5 lines
tools/bpf/bpftool/Documentation/bpftool-link.rst +5 -0
+     **bpftool link detach** *LINK*
+               Force-detach link *LINK*. BPF link and its underlying BPF
+               program will stay valid, but they will be detached from the
+               respective BPF hook and BPF link will transition into
+               a defunct state until last open file descriptor for that

Shall we say "a defunct state when the last open file descriptor for that..."?

No-no, it is in defunc state between LINK_DETACH and last FD being closed. Once last FD is closed, BPF link will get destructed and freed in kernel. So I think until is more precise here?

Quoted 9 lines
Quoted 7 lines
tools/bpf/bpftool/Documentation/bpftool-link.rst +2 -0
+               link is closed.
+
      **bpftool link help**
                Print short help message.
 

[...]

#15 lore raw

Re: [PATCH bpf-next 5/5] tools/bpftool: add documentation and bash-completion for `link detach`

Quoted 36 lines · Andrii Nakryiko
Quoted 22 lines · Song Liu
Quoted 9 lines · Andrii Nakryiko

Add info on link detach sub-command to man page. Add detach to bash-completion as well.

With one nitpick below.

Quoted 1 line

---

[...]

Quoted 4 lines
tools/bpf/bpftool/Documentation/bpftool-link.rst +0 -0
@@ -49,6 +50,13 @@ DESCRIPTION
               contain a dot character ('.'), which is reserved for future
               extensions of *bpffs*.
 
Quoted 7 lines

tools/bpf/bpftool/Documentation/bpftool-link.rst DESCRIPTION · line 57

Quoted 5 lines
tools/bpf/bpftool/Documentation/bpftool-link.rst +5 -0
+     **bpftool link detach** *LINK*
+               Force-detach link *LINK*. BPF link and its underlying BPF
+               program will stay valid, but they will be detached from the
+               respective BPF hook and BPF link will transition into
+               a defunct state until last open file descriptor for that

Shall we say "a defunct state when the last open file descriptor for that..."?

Quoted 3 lines

No-no, it is in defunc state between LINK_DETACH and last FD being closed. Once last FD is closed, BPF link will get destructed and freed in kernel. So I think until is more precise here?

Ah, I see. I misunderstood "defunct state". Please ignore the comment.

Thanks,
Song

#16 lore raw
In reply to Andrii Nakryiko
Quoted 7 lines · Andrii Nakryiko

This patch set adds new BPF link operation, LINK_DETACH, allowing processes with BPF link FD to force-detach it from respective BPF hook, similarly how BPF link is auto-detached when such BPF hook (e.g., cgroup, net_device, netns, etc) is removed. This facility allows admin to forcefully undo BPF link attachment, while process that created BPF link in the first place is left intact.

Quoted 6 lines

Once force-detached, BPF link stays valid in the kernel as long as there is at least one FD open against it. It goes into defunct state, just like auto-detached BPF link.

bpftool also got link detach command to allow triggering this in non-programmatic fashion.

I know this was already merged, but just wanted to add a belated 'thanks for adding this'!

Quoted 1 line
Cc Toke Høiland-Jørgensen <[email protected]>

BTW, I've noticed that you tend to drop Ccs on later versions of your patch series (had to go and lookup v2 of this to check that it was in fact merged). Is that intentional? :)

-Toke

#17 lore raw
Quoted 14 lines · Toke Høiland-Jørgensen
Quoted 11 lines · Andrii Nakryiko

This patch set adds new BPF link operation, LINK_DETACH, allowing processes with BPF link FD to force-detach it from respective BPF hook, similarly how BPF link is auto-detached when such BPF hook (e.g., cgroup, net_device, netns, etc) is removed. This facility allows admin to forcefully undo BPF link attachment, while process that created BPF link in the first place is left intact.

Once force-detached, BPF link stays valid in the kernel as long as there is at least one FD open against it. It goes into defunct state, just like auto-detached BPF link.

Quoted 6 lines
Quoted 2 lines

bpftool also got link detach command to allow triggering this in non-programmatic fashion.

I know this was already merged, but just wanted to add a belated 'thanks for adding this'!

You are welcome!

Quoted 5 lines
Quoted 1 line
Cc Toke Høiland-Jørgensen <[email protected]>

BTW, I've noticed that you tend to drop Ccs on later versions of your patch series (had to go and lookup v2 of this to check that it was in fact merged). Is that intentional? :)

Hm.. not sure about whether I tend to do that. But in this it was intentional and I dropped you from CC because I've seen enough reminders about your vacation, didn't need more ;)

In general, though, I try to keep CC list short, otherwise vger blocks my patches. People directly CC'd get them, but they never appear on bpf@vger mailing list. So it probably happened a few times where I started off with longer CC and had to drop people from it just to get my patches into patchworks.

Quoted 3 lines

-Toke

#18 lore raw
Quoted 7 lines · Andrii Nakryiko
Quoted 3 lines

BTW, I've noticed that you tend to drop Ccs on later versions of your patch series (had to go and lookup v2 of this to check that it was in fact merged). Is that intentional? :)

Hm.. not sure about whether I tend to do that. But in this it was intentional and I dropped you from CC because I've seen enough reminders about your vacation, didn't need more ;)

Haha, that's fair ;)

Quoted 5 lines

In general, though, I try to keep CC list short, otherwise vger blocks my patches. People directly CC'd get them, but they never appear on bpf@vger mailing list. So it probably happened a few times where I started off with longer CC and had to drop people from it just to get my patches into patchworks.

Ah, I have had that happen to me (patches not showing up on vger), but had no idea that could be related to a long Cc list. Good to know! And thanks for the explanation - it's not a huge issue for me (I do generally keep up with netdev@ and bpf@ although as you can no doubt imagine I'm a wee bit behind right now), was just wondering...

-Toke