Skip to contentSkip to conversation

bpf_link observability APIs

PATCH Superseded Andrii Nakryiko 6 participants 25 messages Apr 28, 2020 – Apr 29, 2020

Subsystems (9)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover bpf_link observability APIs

This patch series adds various observability APIs to bpf_link:
- each bpf_link now gets ID, similar to bpf_map and bpf_prog, by which user-space can iterate over all existing bpf_links and create limited FD from ID;
- allows to get extra object information with bpf_link general and

    type-specific information;
  - implements `bpf link show` command which lists all active bpf_links in the
    system;

- implements bpf link pin allowing to pin bpf_link by ID or from other pinned path.

v1->v2:
- simplified bpftool link show implementation (Quentin);
- fixed formatting of bpftool-link.rst (Quentin);
- fixed attach type printing logic (Quentin);
rfc->v1:
- dropped read-only bpf_links (Alexei);
- fixed bug in bpf_link_cleanup() not removing ID;
- fixed bpftool link pinning search logic;
- added bash-completion and man page.

Andrii Nakryiko (10):
bpf: refactor bpf_link update handling
bpf: allocate ID for bpf_link
bpf: support GET_FD_BY_ID and GET_NEXT_ID for bpf_link
bpf: add support for BPF_OBJ_GET_INFO_BY_FD for bpf_link
libbpf: add low-level APIs for new bpf_link commands selftests/bpf: test bpf_link's get_next_id, get_fd_by_id, and
get_obj_info
bpftool: expose attach_type-to-string array to non-cgroup code
bpftool: add bpf_link show and pin support
bpftool: add bpftool-link manpage
bpftool: add link bash completions

 include/linux/bpf-cgroup.h                    |  14 -
 include/linux/bpf.h                           |  28 +-
 include/linux/bpf_types.h                     |   6 +
 include/uapi/linux/bpf.h                      |  31 ++
 kernel/bpf/btf.c                              |   2 +
 kernel/bpf/cgroup.c                           |  89 ++++-
 kernel/bpf/syscall.c                          | 363 ++++++++++++++----
 kernel/bpf/verifier.c                         |   2 +
 kernel/cgroup/cgroup.c                        |  27 --
 .../bpftool/Documentation/bpftool-link.rst    | 118 ++++++
 tools/bpf/bpftool/bash-completion/bpftool     |  39 ++
 tools/bpf/bpftool/cgroup.c                    |  48 +--
 tools/bpf/bpftool/common.c                    |   2 +
 tools/bpf/bpftool/link.c                      | 333 ++++++++++++++++
 tools/bpf/bpftool/main.c                      |   6 +-
 tools/bpf/bpftool/main.h                      |  37 ++
 tools/include/uapi/linux/bpf.h                |  31 ++
 tools/lib/bpf/bpf.c                           |  19 +-
 tools/lib/bpf/bpf.h                           |   4 +-
 tools/lib/bpf/libbpf.map                      |   6 +
 .../selftests/bpf/prog_tests/bpf_obj_id.c     | 110 +++++-
 .../testing/selftests/bpf/progs/test_obj_id.c |  14 +-
 22 files changed, 1145 insertions(+), 184 deletions(-)
 create mode 100644 tools/bpf/bpftool/Documentation/bpftool-link.rst
 create mode 100644 tools/bpf/bpftool/link.c
Signature · 1 line
2.24.1
#1 lore raw

PATCH v2 1/10 bpf: refactor bpf_link update handling

Make bpf_link update support more generic by making it into another bpf_link_ops methods. This allows generic syscall handling code to be agnostic to various conditionally compiled features (e.g., the case of CONFIG_CGROUP_BPF). This also allows to keep link type-specific code to remain static within respective code base. Refactor existing bpf_cgroup_link code and take advantage of this.

---
 include/linux/bpf-cgroup.h | 12 ------------
 include/linux/bpf.h        |  3 ++-
 kernel/bpf/cgroup.c        | 30 ++++++++++++++++++++++++++++--
 kernel/bpf/syscall.c       | 11 ++++-------
 kernel/cgroup/cgroup.c     | 27 ---------------------------
 5 files changed, 34 insertions(+), 49 deletions(-)
include/linux/bpf-cgroup.h +0 -12
index c11b413d5b1a..d2d969669564 100644
@@ -100,8 +100,6 @@ int __cgroup_bpf_attach(struct cgroup *cgrp,
 int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
 			struct bpf_cgroup_link *link,
 			enum bpf_attach_type type);
-int __cgroup_bpf_replace(struct cgroup *cgrp, struct bpf_cgroup_link *link,
-			 struct bpf_prog *new_prog);
 int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
 		       union bpf_attr __user *uattr);
 
@@ -112,8 +110,6 @@ int cgroup_bpf_attach(struct cgroup *cgrp,
 		      u32 flags);
 int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
 		      enum bpf_attach_type type);
-int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *old_prog,
-		       struct bpf_prog *new_prog);
 int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
 		     union bpf_attr __user *uattr);
 
@@ -354,7 +350,6 @@ int cgroup_bpf_prog_query(const union bpf_attr *attr,
 #else
 
 struct bpf_prog;
-struct bpf_link;
 struct cgroup_bpf {};
 static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
 static inline void cgroup_bpf_offline(struct cgroup *cgrp) {}
@@ -378,13 +373,6 @@ static inline int cgroup_bpf_link_attach(const union bpf_attr *attr,
 	return -EINVAL;
 }
 
-static inline int cgroup_bpf_replace(struct bpf_link *link,
-				     struct bpf_prog *old_prog,
-				     struct bpf_prog *new_prog)
-{
-	return -EINVAL;
-}
-
 static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
 					union bpf_attr __user *uattr)
 {
include/linux/bpf.h +2 -1
index 10960cfabea4..81c8620cb4c4 100644
@@ -1093,7 +1093,8 @@ struct bpf_link {
 struct bpf_link_ops {
 	void (*release)(struct bpf_link *link);
 	void (*dealloc)(struct bpf_link *link);
-
+	int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
+			   struct bpf_prog *old_prog);
 };
 
 void bpf_link_init(struct bpf_link *link, const struct bpf_link_ops *ops,
kernel/bpf/cgroup.c +28 -2
index fc7c7002fd37..0b284da05d0f 100644
@@ -557,8 +557,9 @@ static void replace_effective_prog(struct cgroup *cgrp,
  *
  * Must be called with cgroup_mutex held.
  */
-int __cgroup_bpf_replace(struct cgroup *cgrp, struct bpf_cgroup_link *link,
-			 struct bpf_prog *new_prog)
+static int __cgroup_bpf_replace(struct cgroup *cgrp,
+				struct bpf_cgroup_link *link,
+				struct bpf_prog *new_prog)
 {
 	struct list_head *progs = &cgrp->bpf.progs[link->type];
 	struct bpf_prog *old_prog;
@@ -583,6 +584,30 @@ int __cgroup_bpf_replace(struct cgroup *cgrp, struct bpf_cgroup_link *link,
 	return 0;
 }
 
+static int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *new_prog,
+			      struct bpf_prog *old_prog)
+{
+	struct bpf_cgroup_link *cg_link;
+	int ret;
+
+	cg_link = container_of(link, struct bpf_cgroup_link, link);
+
+	mutex_lock(&cgroup_mutex);
+	/* link might have been auto-released by dying cgroup, so fail */
+	if (!cg_link->cgroup) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+	if (old_prog && link->prog != old_prog) {
+		ret = -EPERM;
+		goto out_unlock;
+	}
+	ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog);
+out_unlock:
+	mutex_unlock(&cgroup_mutex);
+	return ret;
+}
+
 static struct bpf_prog_list *find_detach_entry(struct list_head *progs,
 					       struct bpf_prog *prog,
 					       struct bpf_cgroup_link *link,
@@ -811,6 +836,7 @@ static void bpf_cgroup_link_dealloc(struct bpf_link *link)
 const struct bpf_link_ops bpf_cgroup_link_lops = {
 	.release = bpf_cgroup_link_release,
 	.dealloc = bpf_cgroup_link_dealloc,
+	.update_prog = cgroup_bpf_replace,
 };
 
 int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
kernel/bpf/syscall.c +4 -7
index 7626b8024471..f5358e1462eb 100644
@@ -3645,13 +3645,10 @@ static int link_update(union bpf_attr *attr)
 		goto out_put_progs;
 	}
 
-#ifdef CONFIG_CGROUP_BPF
-	if (link->ops == &bpf_cgroup_link_lops) {
-		ret = cgroup_bpf_replace(link, old_prog, new_prog);
-		goto out_put_progs;
-	}
-#endif
-	ret = -EINVAL;
+	if (link->ops->update_prog)
+		ret = link->ops->update_prog(link, new_prog, old_prog);
+	else
+		ret = EINVAL;
 
 out_put_progs:
 	if (old_prog)
kernel/cgroup/cgroup.c +0 -27
index 06b5ea9d899d..557a9b9d2244 100644
@@ -6508,33 +6508,6 @@ int cgroup_bpf_attach(struct cgroup *cgrp,
 	return ret;
 }
 
-int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *old_prog,
-		       struct bpf_prog *new_prog)
-{
-	struct bpf_cgroup_link *cg_link;
-	int ret;
-
-	if (link->ops != &bpf_cgroup_link_lops)
-		return -EINVAL;
-
-	cg_link = container_of(link, struct bpf_cgroup_link, link);
-
-	mutex_lock(&cgroup_mutex);
-	/* link might have been auto-released by dying cgroup, so fail */
-	if (!cg_link->cgroup) {
-		ret = -EINVAL;
-		goto out_unlock;
-	}
-	if (old_prog && link->prog != old_prog) {
-		ret = -EPERM;
-		goto out_unlock;
-	}
-	ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog);
-out_unlock:
-	mutex_unlock(&cgroup_mutex);
-	return ret;
-}
-
 int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
 		      enum bpf_attach_type type)
 {
Signature · 1 line
2.24.1
#2 lore raw

PATCH v2 2/10 bpf: allocate ID for bpf_link

In reply to Andrii Nakryiko

Generate ID for each bpf_link using IDR, similarly to bpf_map and bpf_prog. bpf_link creation, initialization, attachment, and exposing to user-space through FD and ID is a complicated multi-step process, abstract it away through bpf_link_primer and bpf_link_prime(), bpf_link_settle(), and bpf_link_cleanup() internal API. They guarantee that until bpf_link is properly attached, user-space won't be able to access partially-initialized bpf_link either from FD or ID. All this allows to simplify bpf_link attachment and error handling code.

---
 include/linux/bpf.h      |  17 +++--
 include/uapi/linux/bpf.h |   1 +
 kernel/bpf/cgroup.c      |  14 ++--
 kernel/bpf/syscall.c     | 143 +++++++++++++++++++++++++++------------
 4 files changed, 118 insertions(+), 57 deletions(-)
include/linux/bpf.h +13 -4
index 81c8620cb4c4..875d1f0af803 100644
@@ -1085,11 +1085,19 @@ int bpf_prog_new_fd(struct bpf_prog *prog);
 
 struct bpf_link {
 	atomic64_t refcnt;
+	u32 id;
 	const struct bpf_link_ops *ops;
 	struct bpf_prog *prog;
 	struct work_struct work;
 };
 
+struct bpf_link_primer {
+	struct bpf_link *link;
+	struct file *file;
+	int fd;
+	u32 id;
+};
+
 struct bpf_link_ops {
 	void (*release)(struct bpf_link *link);
 	void (*dealloc)(struct bpf_link *link);
@@ -1097,10 +1105,11 @@ struct bpf_link_ops {
 			   struct bpf_prog *old_prog);
 };
 
-void bpf_link_init(struct bpf_link *link, const struct bpf_link_ops *ops,
-		   struct bpf_prog *prog);
-void bpf_link_cleanup(struct bpf_link *link, struct file *link_file,
-		      int link_fd);
+void bpf_link_init(struct bpf_link *link,
+		   const struct bpf_link_ops *ops, struct bpf_prog *prog);
+int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
+int bpf_link_settle(struct bpf_link_primer *primer);
+void bpf_link_cleanup(struct bpf_link_primer *primer);
 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);
include/uapi/linux/bpf.h +1 -0
index 4a6c47f3febe..6121aa487465 100644
@@ -523,6 +523,7 @@ union bpf_attr {
 			__u32		prog_id;
 			__u32		map_id;
 			__u32		btf_id;
+			__u32		link_id;
 		};
 		__u32		next_id;
 		__u32		open_flags;
kernel/bpf/cgroup.c +6 -8
index 0b284da05d0f..beb8307b16c6 100644
@@ -841,10 +841,10 @@ const struct bpf_link_ops bpf_cgroup_link_lops = {
 
 int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 {
+	struct bpf_link_primer link_primer;
 	struct bpf_cgroup_link *link;
-	struct file *link_file;
 	struct cgroup *cgrp;
-	int err, link_fd;
+	int err;
 
 	if (attr->link_create.flags)
 		return -EINVAL;
@@ -862,22 +862,20 @@ int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	link->cgroup = cgrp;
 	link->type = attr->link_create.attach_type;
 
-	link_file = bpf_link_new_file(&link->link, &link_fd);
-	if (IS_ERR(link_file)) {
+	err  = bpf_link_prime(&link->link, &link_primer);
+	if (err) {
 		kfree(link);
-		err = PTR_ERR(link_file);
 		goto out_put_cgroup;
 	}
 
 	err = cgroup_bpf_attach(cgrp, NULL, NULL, link, link->type,
 				BPF_F_ALLOW_MULTI);
 	if (err) {
-		bpf_link_cleanup(&link->link, link_file, link_fd);
+		bpf_link_cleanup(&link_primer);
 		goto out_put_cgroup;
 	}
 
-	fd_install(link_fd, link_file);
-	return link_fd;
+	return bpf_link_settle(&link_primer);
 
 out_put_cgroup:
 	cgroup_put(cgrp);
kernel/bpf/syscall.c +98 -45

262 lines not on this page · raw message

Signature · 1 line
2.24.1
#3 lore raw

Re: [PATCH v2 bpf-next 02/10] bpf: allocate ID for bpf_link

kernel/bpf/syscall.c static const struct file_operations bpf_link_fops = { · line 2373

Quoted 4 lines · Andrii Nakryiko
kernel/bpf/syscall.c +4 -0
+int bpf_link_settle(struct bpf_link_primer *primer)
+{
+	/* make bpf_link fetchable by ID */
+	WRITE_ONCE(primer->link->id, primer->id);

what does WRITE_ONCE serve here?
bpf_link_settle can only be called at the end of attach. If attach is slow than parallel get_fd_by_id can get an new FD instance for link with zero id.
In such case deref of link->id will race with above assignment? But I don't see READ_ONCE in patch 3.
It's under link_idr_lock there.
How about grabbing link_idr_lock here as well ? otherwise it's still racy since WRITE_ONCE is not paired.

The mix of spin_lock_irqsave(&link_idr_lock)
and spin_lock_bh(&link_idr_lock) looks weird.
We do the same for map_idr because maps have complicated freeing logic, but prog_idr is consistent.
If you see the need for irqsave variant then please use it in all cases.

#4 lore raw

Re: [PATCH v2 bpf-next 02/10] bpf: allocate ID for bpf_link

Quoted 8 lines · Alexei Starovoitov

kernel/bpf/syscall.c static const struct file_operations bpf_link_fops = { · line 2373

Quoted 4 lines · Andrii Nakryiko
kernel/bpf/syscall.c +4 -0
+int bpf_link_settle(struct bpf_link_primer *primer)
+{
+     /* make bpf_link fetchable by ID */
+     WRITE_ONCE(primer->link->id, primer->id);

what does WRITE_ONCE serve here?

To prevent compiler reordering this write with fd_install. So that by the time FD is exposed to user-space, link has properly set ID.

Quoted 4 lines

bpf_link_settle can only be called at the end of attach. If attach is slow than parallel get_fd_by_id can get an new FD instance for link with zero id.
In such case deref of link->id will race with above assignment?

Yes, it does race, but it can either see zero and assume bpf_link is not ready (which is fine to do) or will see correct link ID and will proceed to create new FD for it. By the time we do context switch back to user-space and return link FD, ID will definitely be visible due to context switch and associated memory barriers. If anyone is guessing FD and trying to create FD_BY_ID before LINK_CREATE syscall returns -- then returning failure due to link ID not yet set is totally fine, IMO.

Quoted 2 lines

But I don't see READ_ONCE in patch 3.
It's under link_idr_lock there.

It doesn't need READ_ONCE because it does read under spinlock, so compiler can't re-order it with code outside of spinlock.

Quoted 2 lines

How about grabbing link_idr_lock here as well ? otherwise it's still racy since WRITE_ONCE is not paired.

As indicated above, seems unnecessary? But I also don't object strongly, I don't expect this lock for links to be a major bottleneck or anything like that.

Quoted 6 lines

The mix of spin_lock_irqsave(&link_idr_lock)
and spin_lock_bh(&link_idr_lock) looks weird.
We do the same for map_idr because maps have complicated freeing logic, but prog_idr is consistent.
If you see the need for irqsave variant then please use it in all cases.

No, my bad, I don't see any need to intermix them. I'll stick to spin_lock_bh, thanks for catching!

#5 lore raw

Re: [PATCH v2 bpf-next 02/10] bpf: allocate ID for bpf_link

Quoted 11 lines · Andrii Nakryiko
Quoted 8 lines · Alexei Starovoitov

kernel/bpf/syscall.c static const struct file_operations bpf_link_fops = { · line 2373

Quoted 4 lines · Andrii Nakryiko
kernel/bpf/syscall.c +4 -0
+int bpf_link_settle(struct bpf_link_primer *primer)
+{
+     /* make bpf_link fetchable by ID */
+     WRITE_ONCE(primer->link->id, primer->id);

what does WRITE_ONCE serve here?

Quoted 2 lines

To prevent compiler reordering this write with fd_install. So that by the time FD is exposed to user-space, link has properly set ID.

if you wanted memory barrier then it should have been barrier(), but that wouldn't be enough, since patch 2 and 3 race to read and write that 32-bit int.

Quoted 14 lines
Quoted 4 lines

bpf_link_settle can only be called at the end of attach. If attach is slow than parallel get_fd_by_id can get an new FD instance for link with zero id.
In such case deref of link->id will race with above assignment?

Yes, it does race, but it can either see zero and assume bpf_link is not ready (which is fine to do) or will see correct link ID and will proceed to create new FD for it. By the time we do context switch back to user-space and return link FD, ID will definitely be visible due to context switch and associated memory barriers. If anyone is guessing FD and trying to create FD_BY_ID before LINK_CREATE syscall returns -- then returning failure due to link ID not yet set is totally fine, IMO.

Quoted 5 lines
Quoted 2 lines

But I don't see READ_ONCE in patch 3.
It's under link_idr_lock there.

It doesn't need READ_ONCE because it does read under spinlock, so compiler can't re-order it with code outside of spinlock.

spin_lock in patch 3 doesn't guarantee that link->id deref in that patch will be atomic.
So WRITE_ONCE in patch 2 into link->id is still racy with plain read in patch 3.
Just wait and see kmsan complaining about it.

Quoted 6 lines
Quoted 2 lines

How about grabbing link_idr_lock here as well ? otherwise it's still racy since WRITE_ONCE is not paired.

As indicated above, seems unnecessary? But I also don't object strongly, I don't expect this lock for links to be a major bottleneck or anything like that.

Either READ_ONCE has to be paired with WRITE_ONCE (or even better smp_load_acquire with smp_store_release) or use spin_lock.

Quoted 9 lines
Quoted 6 lines

The mix of spin_lock_irqsave(&link_idr_lock)
and spin_lock_bh(&link_idr_lock) looks weird.
We do the same for map_idr because maps have complicated freeing logic, but prog_idr is consistent.
If you see the need for irqsave variant then please use it in all cases.

No, my bad, I don't see any need to intermix them. I'll stick to spin_lock_bh, thanks for catching!

I think that should be fine.
Please double check that situation described in
commit 930651a75bf1 ("bpf: do not disable/enable BH in bpf_map_free_id()") doesn't apply to link_idr.

#6 lore raw

Re: [PATCH v2 bpf-next 02/10] bpf: allocate ID for bpf_link

Quoted 34 lines · Alexei Starovoitov
Quoted 11 lines · Andrii Nakryiko
Quoted 8 lines · Alexei Starovoitov

kernel/bpf/syscall.c static const struct file_operations bpf_link_fops = { · line 2373

Quoted 4 lines · Andrii Nakryiko
kernel/bpf/syscall.c +4 -0
+int bpf_link_settle(struct bpf_link_primer *primer)
+{
+     /* make bpf_link fetchable by ID */
+     WRITE_ONCE(primer->link->id, primer->id);

what does WRITE_ONCE serve here?

Quoted 2 lines

To prevent compiler reordering this write with fd_install. So that by the time FD is exposed to user-space, link has properly set ID.

if you wanted memory barrier then it should have been barrier(), but that wouldn't be enough, since patch 2 and 3 race to read and write that 32-bit int.

Quoted 14 lines
Quoted 4 lines

bpf_link_settle can only be called at the end of attach. If attach is slow than parallel get_fd_by_id can get an new FD instance for link with zero id.
In such case deref of link->id will race with above assignment?

Yes, it does race, but it can either see zero and assume bpf_link is not ready (which is fine to do) or will see correct link ID and will proceed to create new FD for it. By the time we do context switch back to user-space and return link FD, ID will definitely be visible due to context switch and associated memory barriers. If anyone is guessing FD and trying to create FD_BY_ID before LINK_CREATE syscall returns -- then returning failure due to link ID not yet set is totally fine, IMO.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

But I don't see READ_ONCE in patch 3.
It's under link_idr_lock there.

It doesn't need READ_ONCE because it does read under spinlock, so compiler can't re-order it with code outside of spinlock.

spin_lock in patch 3 doesn't guarantee that link->id deref in that patch will be atomic.

What do you mean by "atomic" here? Are you saying that we can get torn read on u32 on some architectures? If that was the case, neither WRITE_ONCE/READ_ONCE nor smp_write_release/smp_load_acquire would help. But I don't think that's the case, we have code in verifier that does similar racy u32 write/read (it uses READ_ONCE/WRITE_ONCE) and seems to be working fine.

Quoted 7 lines

So WRITE_ONCE in patch 2 into link->id is still racy with plain read in patch 3.
Just wait and see kmsan complaining about it.

Quoted 3 lines
Quoted 2 lines

How about grabbing link_idr_lock here as well ? otherwise it's still racy since WRITE_ONCE is not paired.

Quoted 7 lines
Quoted 3 lines

As indicated above, seems unnecessary? But I also don't object strongly, I don't expect this lock for links to be a major bottleneck or anything like that.

Either READ_ONCE has to be paired with WRITE_ONCE (or even better smp_load_acquire with smp_store_release) or use spin_lock.

Sure, let me use smp_load_acquite/smp_store_release.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

The mix of spin_lock_irqsave(&link_idr_lock)
and spin_lock_bh(&link_idr_lock) looks weird.
We do the same for map_idr because maps have complicated freeing logic, but prog_idr is consistent.
If you see the need for irqsave variant then please use it in all cases.

Quoted 7 lines
Quoted 2 lines

No, my bad, I don't see any need to intermix them. I'll stick to spin_lock_bh, thanks for catching!

I think that should be fine.
Please double check that situation described in
commit 930651a75bf1 ("bpf: do not disable/enable BH in bpf_map_free_id()") doesn't apply to link_idr.

If I understand what was the problem for BPF maps, we were taking lock and trying to disable softirqs while softirqs were already disabled by caller. This doesn't seem to be the case for links, as far as I can tell. So I'll just go with spin_lock_bh() everywhere for consistency.

#7 lore raw

Re: [PATCH v2 bpf-next 02/10] bpf: allocate ID for bpf_link

Quoted 39 lines · Andrii Nakryiko
Quoted 37 lines · Alexei Starovoitov
Quoted 11 lines · Andrii Nakryiko
Quoted 8 lines · Alexei Starovoitov

kernel/bpf/syscall.c static const struct file_operations bpf_link_fops = { · line 2373

Quoted 4 lines · Andrii Nakryiko
kernel/bpf/syscall.c +4 -0
+int bpf_link_settle(struct bpf_link_primer *primer)
+{
+     /* make bpf_link fetchable by ID */
+     WRITE_ONCE(primer->link->id, primer->id);

what does WRITE_ONCE serve here?

Quoted 2 lines

To prevent compiler reordering this write with fd_install. So that by the time FD is exposed to user-space, link has properly set ID.

if you wanted memory barrier then it should have been barrier(), but that wouldn't be enough, since patch 2 and 3 race to read and write that 32-bit int.

Quoted 17 lines
Quoted 4 lines

bpf_link_settle can only be called at the end of attach. If attach is slow than parallel get_fd_by_id can get an new FD instance for link with zero id.
In such case deref of link->id will race with above assignment?

Yes, it does race, but it can either see zero and assume bpf_link is not ready (which is fine to do) or will see correct link ID and will proceed to create new FD for it. By the time we do context switch back to user-space and return link FD, ID will definitely be visible due to context switch and associated memory barriers. If anyone is guessing FD and trying to create FD_BY_ID before LINK_CREATE syscall returns -- then returning failure due to link ID not yet set is totally fine, IMO.

Quoted 2 lines

But I don't see READ_ONCE in patch 3.
It's under link_idr_lock there.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

It doesn't need READ_ONCE because it does read under spinlock, so compiler can't re-order it with code outside of spinlock.

spin_lock in patch 3 doesn't guarantee that link->id deref in that patch will be atomic.

What do you mean by "atomic" here? Are you saying that we can get torn read on u32 on some architectures?

compiler doesn't guarantee that plain 32-bit load/store will stay 32-bit even on 64-bit archs.

Quoted 3 lines

If that was the case, neither
WRITE_ONCE/READ_ONCE nor smp_write_release/smp_load_acquire would help.

what do you mean? They will. That's the point of these macros.

Quoted 3 lines

But I don't think that's the case, we have code in verifier that does similar racy u32 write/read (it uses READ_ONCE/WRITE_ONCE) and seems to be working fine.

you mean in btf_resolve_helper_id() ?
What kind of race do you see there?

Quoted 11 lines
Quoted 11 lines

So WRITE_ONCE in patch 2 into link->id is still racy with plain read in patch 3.
Just wait and see kmsan complaining about it.

Quoted 6 lines
Quoted 2 lines

How about grabbing link_idr_lock here as well ? otherwise it's still racy since WRITE_ONCE is not paired.

As indicated above, seems unnecessary? But I also don't object strongly, I don't expect this lock for links to be a major bottleneck or anything like that.

Quoted 5 lines
Quoted 3 lines

Either READ_ONCE has to be paired with WRITE_ONCE (or even better smp_load_acquire with smp_store_release) or use spin_lock.

Sure, let me use smp_load_acquite/smp_store_release.

Since there're locks in other places I would use spin_lock_bh to update id as well.

Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

The mix of spin_lock_irqsave(&link_idr_lock)
and spin_lock_bh(&link_idr_lock) looks weird.
We do the same for map_idr because maps have complicated freeing logic, but prog_idr is consistent.
If you see the need for irqsave variant then please use it in all cases.

Quoted 7 lines
Quoted 2 lines

No, my bad, I don't see any need to intermix them. I'll stick to spin_lock_bh, thanks for catching!

I think that should be fine.
Please double check that situation described in
commit 930651a75bf1 ("bpf: do not disable/enable BH in bpf_map_free_id()") doesn't apply to link_idr.

Quoted 4 lines

If I understand what was the problem for BPF maps, we were taking lock and trying to disable softirqs while softirqs were already disabled by caller. This doesn't seem to be the case for links, as far as I can tell. So I'll just go with spin_lock_bh() everywhere for consistency.

Sounds good.

#8 lore raw

Re: [PATCH v2 bpf-next 02/10] bpf: allocate ID for bpf_link

Quoted 50 lines · Alexei Starovoitov
Quoted 39 lines · Andrii Nakryiko
Quoted 37 lines · Alexei Starovoitov
Quoted 11 lines · Andrii Nakryiko
Quoted 8 lines · Alexei Starovoitov

kernel/bpf/syscall.c static const struct file_operations bpf_link_fops = { · line 2373

Quoted 4 lines · Andrii Nakryiko
kernel/bpf/syscall.c +4 -0
+int bpf_link_settle(struct bpf_link_primer *primer)
+{
+     /* make bpf_link fetchable by ID */
+     WRITE_ONCE(primer->link->id, primer->id);

what does WRITE_ONCE serve here?

Quoted 2 lines

To prevent compiler reordering this write with fd_install. So that by the time FD is exposed to user-space, link has properly set ID.

if you wanted memory barrier then it should have been barrier(), but that wouldn't be enough, since patch 2 and 3 race to read and write that 32-bit int.

Quoted 17 lines
Quoted 4 lines

bpf_link_settle can only be called at the end of attach. If attach is slow than parallel get_fd_by_id can get an new FD instance for link with zero id.
In such case deref of link->id will race with above assignment?

Yes, it does race, but it can either see zero and assume bpf_link is not ready (which is fine to do) or will see correct link ID and will proceed to create new FD for it. By the time we do context switch back to user-space and return link FD, ID will definitely be visible due to context switch and associated memory barriers. If anyone is guessing FD and trying to create FD_BY_ID before LINK_CREATE syscall returns -- then returning failure due to link ID not yet set is totally fine, IMO.

Quoted 2 lines

But I don't see READ_ONCE in patch 3.
It's under link_idr_lock there.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

It doesn't need READ_ONCE because it does read under spinlock, so compiler can't re-order it with code outside of spinlock.

spin_lock in patch 3 doesn't guarantee that link->id deref in that patch will be atomic.

What do you mean by "atomic" here? Are you saying that we can get torn read on u32 on some architectures?

Quoted 8 lines

compiler doesn't guarantee that plain 32-bit load/store will stay 32-bit even on 64-bit archs.

Quoted 3 lines

If that was the case, neither
WRITE_ONCE/READ_ONCE nor smp_write_release/smp_load_acquire would help.

what do you mean? They will. That's the point of these macros.

According to Documentation/memory-barriers.txt, smp_load_acquire/smp_store_release are about ordering and memory barriers, not about guaranteeing atomicity of reading value. Especially READ_ONCE/WRITE_ONCE which are volatile read/write, not atomic read/write. But nevertheless, I'll do lock and this will become moot.

Quoted 7 lines
Quoted 4 lines

But I don't think that's the case, we have code in verifier that does similar racy u32 write/read (it uses READ_ONCE/WRITE_ONCE) and seems to be working fine.

you mean in btf_resolve_helper_id() ?
What kind of race do you see there?

Two CPUs reading/writing to same variable without lock? Value starts at 0 (meaning "not yet ready") and eventually becoming valid and final non-zero value. Even if they race, and one CPU reads 0 while another CPU already set it to non-zero, it's fine. In verifier's case it will be eventually overwritten with the same resolved btf id. In case of bpf_link, GET_FD_BY_ID would pretend link doesn't exist yet and return error. Seems similar enough to me.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

So WRITE_ONCE in patch 2 into link->id is still racy with plain read in patch 3.
Just wait and see kmsan complaining about it.

Quoted 6 lines
Quoted 2 lines

How about grabbing link_idr_lock here as well ? otherwise it's still racy since WRITE_ONCE is not paired.

As indicated above, seems unnecessary? But I also don't object strongly, I don't expect this lock for links to be a major bottleneck or anything like that.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Either READ_ONCE has to be paired with WRITE_ONCE (or even better smp_load_acquire with smp_store_release) or use spin_lock.

Sure, let me use smp_load_acquite/smp_store_release.

Since there're locks in other places I would use spin_lock_bh to update id as well.

Sure, I'll do spin_lock_bh.

Quoted 24 lines
Quoted 18 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

The mix of spin_lock_irqsave(&link_idr_lock)
and spin_lock_bh(&link_idr_lock) looks weird.
We do the same for map_idr because maps have complicated freeing logic, but prog_idr is consistent.
If you see the need for irqsave variant then please use it in all cases.

Quoted 7 lines
Quoted 2 lines

No, my bad, I don't see any need to intermix them. I'll stick to spin_lock_bh, thanks for catching!

I think that should be fine.
Please double check that situation described in
commit 930651a75bf1 ("bpf: do not disable/enable BH in bpf_map_free_id()") doesn't apply to link_idr.

Quoted 4 lines

If I understand what was the problem for BPF maps, we were taking lock and trying to disable softirqs while softirqs were already disabled by caller. This doesn't seem to be the case for links, as far as I can tell. So I'll just go with spin_lock_bh() everywhere for consistency.

Sounds good.

#9 lore raw

Re: [PATCH v2 bpf-next 02/10] bpf: allocate ID for bpf_link

Quoted 8 lines · Andrii Nakryiko
Quoted 8 lines

compiler doesn't guarantee that plain 32-bit load/store will stay 32-bit even on 64-bit archs.

Quoted 3 lines

If that was the case, neither
WRITE_ONCE/READ_ONCE nor smp_write_release/smp_load_acquire would help.

Quoted 8 lines
Quoted 1 line

what do you mean? They will. That's the point of these macros.

According to Documentation/memory-barriers.txt, smp_load_acquire/smp_store_release are about ordering and memory barriers, not about guaranteeing atomicity of reading value. Especially READ_ONCE/WRITE_ONCE which are volatile read/write, not atomic read/write. But nevertheless, I'll do lock and this will become moot.

May be that's something for Paul to clarify in the doc? smp_load_acquire() is READ_ONCE() + smp_mb() unoptimized in general case. And READ_ONCE + barrier on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

But I don't think that's the case, we have code in verifier that does similar racy u32 write/read (it uses READ_ONCE/WRITE_ONCE) and seems to be working fine.

you mean in btf_resolve_helper_id() ?
What kind of race do you see there?

Quoted 7 lines

Two CPUs reading/writing to same variable without lock? Value starts at 0 (meaning "not yet ready") and eventually becoming valid and final non-zero value. Even if they race, and one CPU reads 0 while another CPU already set it to non-zero, it's fine. In verifier's case it will be eventually overwritten with the same resolved btf id. In case of bpf_link, GET_FD_BY_ID would pretend link doesn't exist yet and return error. Seems similar enough to me.

ahh. similar in the sense that only one value is written. it's either zero or whatever_that_id. Right.

#10 lore raw

PATCH v2 3/10 bpf: support GET_FD_BY_ID and GET_NEXT_ID for bpf_link

In reply to Andrii Nakryiko

Add support to look up bpf_link by ID and iterate over all existing bpf_links in the system. GET_FD_BY_ID code handles not-yet-ready bpf_link by checking that its ID hasn't been set to non-zero value yet. Setting bpf_link's ID is done as the very last step in finalizing bpf_link, together with installing FD. This approach allows users of bpf_link in kernel code to not worry about races between user-space and kernel code that hasn't finished attaching and initializing bpf_link.

---
 include/uapi/linux/bpf.h |  2 ++
 kernel/bpf/syscall.c     | 49 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)
include/uapi/linux/bpf.h +2 -0
index 6121aa487465..7e6541fceade 100644
@@ -113,6 +113,8 @@ enum bpf_cmd {
 	BPF_MAP_DELETE_BATCH,
 	BPF_LINK_CREATE,
 	BPF_LINK_UPDATE,
+	BPF_LINK_GET_FD_BY_ID,
+	BPF_LINK_GET_NEXT_ID,
 };
 
 enum bpf_map_type {
kernel/bpf/syscall.c +49 -0

61 lines not on this page · raw message

Signature · 1 line
2.24.1
#11 lore raw

PATCH v2 4/10 bpf: add support for BPF_OBJ_GET_INFO_BY_FD for bpf_link

In reply to Andrii Nakryiko

Add ability to fetch bpf_link details through BPF_OBJ_GET_INFO_BY_FD command. Also enhance show_fdinfo to potentially include bpf_link type-specific information (similarly to obj_info).

Also introduce enum bpf_link_type stored in bpf_link itself and expose it in UAPI. bpf_link_tracing also now will store and return bpf_attach_type.

---
 include/linux/bpf-cgroup.h     |   2 -
 include/linux/bpf.h            |  10 +-
 include/linux/bpf_types.h      |   6 ++
 include/uapi/linux/bpf.h       |  28 ++++++
 kernel/bpf/btf.c               |   2 +
 kernel/bpf/cgroup.c            |  45 ++++++++-
 kernel/bpf/syscall.c           | 164 +++++++++++++++++++++++++++++----
 kernel/bpf/verifier.c          |   2 +
 tools/include/uapi/linux/bpf.h |  31 +++++++
 9 files changed, 266 insertions(+), 24 deletions(-)
include/linux/bpf-cgroup.h +0 -2
index d2d969669564..ab95824a1d99 100644
@@ -57,8 +57,6 @@ struct bpf_cgroup_link {
 	enum bpf_attach_type type;
 };
 
-extern const struct bpf_link_ops bpf_cgroup_link_lops;
-
 struct bpf_prog_list {
 	struct list_head node;
 	struct bpf_prog *prog;
include/linux/bpf.h +9 -1
index 875d1f0af803..701c4387c711 100644
@@ -1026,9 +1026,11 @@ extern const struct file_operations bpf_prog_fops;
 	extern const struct bpf_verifier_ops _name ## _verifier_ops;
 #define BPF_MAP_TYPE(_id, _ops) \
 	extern const struct bpf_map_ops _ops;
+#define BPF_LINK_TYPE(_id, _name)
 #include <linux/bpf_types.h>
 #undef BPF_PROG_TYPE
 #undef BPF_MAP_TYPE
+#undef BPF_LINK_TYPE
 
 extern const struct bpf_prog_ops bpf_offload_prog_ops;
 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
@@ -1086,6 +1088,7 @@ int bpf_prog_new_fd(struct bpf_prog *prog);
 struct bpf_link {
 	atomic64_t refcnt;
 	u32 id;
+	enum bpf_link_type type;
 	const struct bpf_link_ops *ops;
 	struct bpf_prog *prog;
 	struct work_struct work;
@@ -1103,9 +1106,14 @@ struct bpf_link_ops {
 	void (*dealloc)(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);
+	int (*fill_link_info)(const struct bpf_link *link,
+			      struct bpf_link_info *info,
+			      const struct bpf_link_info *uinfo,
+			      u32 info_len);
 };
 
-void bpf_link_init(struct bpf_link *link,
+void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
 		   const struct bpf_link_ops *ops, struct bpf_prog *prog);
 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
 int bpf_link_settle(struct bpf_link_primer *primer);
include/linux/bpf_types.h +6 -0
index ba0c2d56f8a3..8345cdf553b8 100644
@@ -118,3 +118,9 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_STACK, stack_map_ops)
 #if defined(CONFIG_BPF_JIT)
 BPF_MAP_TYPE(BPF_MAP_TYPE_STRUCT_OPS, bpf_struct_ops_map_ops)
 #endif
+
+BPF_LINK_TYPE(BPF_LINK_TYPE_RAW_TRACEPOINT, raw_tracepoint)
+BPF_LINK_TYPE(BPF_LINK_TYPE_TRACING, tracing)
+#ifdef CONFIG_CGROUP_BPF
+BPF_LINK_TYPE(BPF_LINK_TYPE_CGROUP, cgroup)
+#endif
include/uapi/linux/bpf.h +28 -0
index 7e6541fceade..0eccafae55bb 100644
@@ -222,6 +222,15 @@ enum bpf_attach_type {
 
 #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
 
+enum bpf_link_type {
+	BPF_LINK_TYPE_UNSPEC = 0,
+	BPF_LINK_TYPE_RAW_TRACEPOINT = 1,
+	BPF_LINK_TYPE_TRACING = 2,
+	BPF_LINK_TYPE_CGROUP = 3,
+
+	MAX_BPF_LINK_TYPE,
+};
+
 /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command
  *
  * NONE(default): No further bpf programs allowed in the subtree.
@@ -3612,6 +3621,25 @@ struct bpf_btf_info {
 	__u32 id;
 } __attribute__((aligned(8)));
 
+struct bpf_link_info {
+	__u32 type;
+	__u32 id;
+	__u32 prog_id;
+	union {
+		struct {
+			__aligned_u64 tp_name; /* in/out: tp_name buffer ptr */
+			__u32 tp_name_len;     /* in/out: tp_name buffer len */
+		} raw_tracepoint;
+		struct {
+			__u32 attach_type;
+		} tracing;
+		struct {
+			__u64 cgroup_id;
+			__u32 attach_type;
+		} cgroup;
+	};
+} __attribute__((aligned(8)));
+
 /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
  * by user and intended to be used by socket (e.g. to bind to, depends on
  * attach attach type).
kernel/bpf/btf.c +2 -0
index d65c6912bdaf..a2cfba89a8e1 100644
@@ -3482,6 +3482,7 @@ extern char __weak __stop_BTF[];
 extern struct btf *btf_vmlinux;
 
 #define BPF_MAP_TYPE(_id, _ops)
+#define BPF_LINK_TYPE(_id, _name)
 static union {
 	struct bpf_ctx_convert {
 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
@@ -3508,6 +3509,7 @@ static u8 bpf_ctx_convert_map[] = {
 	0, /* avoid empty array */
 };
 #undef BPF_MAP_TYPE
+#undef BPF_LINK_TYPE
 
 static const struct btf_member *
 btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,
kernel/bpf/cgroup.c +43 -2

60 lines not on this page · raw message

kernel/bpf/syscall.c +145 -19

260 lines not on this page · raw message

kernel/bpf/verifier.c +2 -0
index 91728e0f27eb..2b337e32aa94 100644
@@ -28,9 +28,11 @@ static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
 	[_id] = & _name ## _verifier_ops,
 #define BPF_MAP_TYPE(_id, _ops)
+#define BPF_LINK_TYPE(_id, _name)
 #include <linux/bpf_types.h>
 #undef BPF_PROG_TYPE
 #undef BPF_MAP_TYPE
+#undef BPF_LINK_TYPE
 };
 
 /* bpf_check() is a static code analyzer that walks eBPF program
tools/include/uapi/linux/bpf.h +31 -0
index 4a6c47f3febe..0eccafae55bb 100644
@@ -113,6 +113,8 @@ enum bpf_cmd {
 	BPF_MAP_DELETE_BATCH,
 	BPF_LINK_CREATE,
 	BPF_LINK_UPDATE,
+	BPF_LINK_GET_FD_BY_ID,
+	BPF_LINK_GET_NEXT_ID,
 };
 
 enum bpf_map_type {
@@ -220,6 +222,15 @@ enum bpf_attach_type {
 
 #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
 
+enum bpf_link_type {
+	BPF_LINK_TYPE_UNSPEC = 0,
+	BPF_LINK_TYPE_RAW_TRACEPOINT = 1,
+	BPF_LINK_TYPE_TRACING = 2,
+	BPF_LINK_TYPE_CGROUP = 3,
+
+	MAX_BPF_LINK_TYPE,
+};
+
 /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command
  *
  * NONE(default): No further bpf programs allowed in the subtree.
@@ -523,6 +534,7 @@ union bpf_attr {
 			__u32		prog_id;
 			__u32		map_id;
 			__u32		btf_id;
+			__u32		link_id;
 		};
 		__u32		next_id;
 		__u32		open_flags;
@@ -3609,6 +3621,25 @@ struct bpf_btf_info {
 	__u32 id;
 } __attribute__((aligned(8)));
 
+struct bpf_link_info {
+	__u32 type;
+	__u32 id;
+	__u32 prog_id;
+	union {
+		struct {
+			__aligned_u64 tp_name; /* in/out: tp_name buffer ptr */
+			__u32 tp_name_len;     /* in/out: tp_name buffer len */
+		} raw_tracepoint;
+		struct {
+			__u32 attach_type;
+		} tracing;
+		struct {
+			__u64 cgroup_id;
+			__u32 attach_type;
+		} cgroup;
+	};
+} __attribute__((aligned(8)));
+
 /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
  * by user and intended to be used by socket (e.g. to bind to, depends on
  * attach attach type).
Signature · 1 line
2.24.1
#12 lore raw

Re: [PATCH v2 bpf-next 04/10] bpf: add support for BPF_OBJ_GET_INFO_BY_FD for bpf_link

Quoted 113 lines · Andrii Nakryiko

Add ability to fetch bpf_link details through BPF_OBJ_GET_INFO_BY_FD command. Also enhance show_fdinfo to potentially include bpf_link type-specific information (similarly to obj_info).

Also introduce enum bpf_link_type stored in bpf_link itself and expose it in UAPI. bpf_link_tracing also now will store and return bpf_attach_type.

---
 include/linux/bpf-cgroup.h     |   2 -
 include/linux/bpf.h            |  10 +-
 include/linux/bpf_types.h      |   6 ++
 include/uapi/linux/bpf.h       |  28 ++++++
 kernel/bpf/btf.c               |   2 +
 kernel/bpf/cgroup.c            |  45 ++++++++-
 kernel/bpf/syscall.c           | 164 +++++++++++++++++++++++++++++----
 kernel/bpf/verifier.c          |   2 +
 tools/include/uapi/linux/bpf.h |  31 +++++++
 9 files changed, 266 insertions(+), 24 deletions(-)
include/linux/bpf-cgroup.h +0 -2
index d2d969669564..ab95824a1d99 100644
@@ -57,8 +57,6 @@ struct bpf_cgroup_link {
 	enum bpf_attach_type type;
 };
 
-extern const struct bpf_link_ops bpf_cgroup_link_lops;
-
 struct bpf_prog_list {
 	struct list_head node;
 	struct bpf_prog *prog;
include/linux/bpf.h +9 -1
index 875d1f0af803..701c4387c711 100644
@@ -1026,9 +1026,11 @@ extern const struct file_operations bpf_prog_fops;
 	extern const struct bpf_verifier_ops _name ## _verifier_ops;
 #define BPF_MAP_TYPE(_id, _ops) \
 	extern const struct bpf_map_ops _ops;
+#define BPF_LINK_TYPE(_id, _name)
 #include <linux/bpf_types.h>
 #undef BPF_PROG_TYPE
 #undef BPF_MAP_TYPE
+#undef BPF_LINK_TYPE
 
 extern const struct bpf_prog_ops bpf_offload_prog_ops;
 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
@@ -1086,6 +1088,7 @@ int bpf_prog_new_fd(struct bpf_prog *prog);
 struct bpf_link {
 	atomic64_t refcnt;
 	u32 id;
+	enum bpf_link_type type;
 	const struct bpf_link_ops *ops;
 	struct bpf_prog *prog;
 	struct work_struct work;
@@ -1103,9 +1106,14 @@ struct bpf_link_ops {
 	void (*dealloc)(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);
+	int (*fill_link_info)(const struct bpf_link *link,
+			      struct bpf_link_info *info,
+			      const struct bpf_link_info *uinfo,
+			      u32 info_len);
 };
 
-void bpf_link_init(struct bpf_link *link,
+void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
 		   const struct bpf_link_ops *ops, struct bpf_prog *prog);
 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
 int bpf_link_settle(struct bpf_link_primer *primer);
include/linux/bpf_types.h +6 -0
index ba0c2d56f8a3..8345cdf553b8 100644
@@ -118,3 +118,9 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_STACK, stack_map_ops)
 #if defined(CONFIG_BPF_JIT)
 BPF_MAP_TYPE(BPF_MAP_TYPE_STRUCT_OPS, bpf_struct_ops_map_ops)
 #endif
+
+BPF_LINK_TYPE(BPF_LINK_TYPE_RAW_TRACEPOINT, raw_tracepoint)
+BPF_LINK_TYPE(BPF_LINK_TYPE_TRACING, tracing)
+#ifdef CONFIG_CGROUP_BPF
+BPF_LINK_TYPE(BPF_LINK_TYPE_CGROUP, cgroup)
+#endif
include/uapi/linux/bpf.h +11 -0
index 7e6541fceade..0eccafae55bb 100644
@@ -222,6 +222,15 @@ enum bpf_attach_type {
 
 #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
 
+enum bpf_link_type {
+	BPF_LINK_TYPE_UNSPEC = 0,
+	BPF_LINK_TYPE_RAW_TRACEPOINT = 1,
+	BPF_LINK_TYPE_TRACING = 2,
+	BPF_LINK_TYPE_CGROUP = 3,
+
+	MAX_BPF_LINK_TYPE,
+};
+
 /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command
  *
  * NONE(default): No further bpf programs allowed in the subtree.
@@ -3612,6 +3621,25 @@ struct bpf_btf_info {
 	__u32 id;
 } __attribute__((aligned(8)));
 
+struct bpf_link_info {
+	__u32 type;

include/uapi/linux/bpf.h struct bpf_btf_info { · line 3635

Quoted 10 lines
include/uapi/linux/bpf.h +10 -0
+	__u32 id;
+	__u32 prog_id;
+	union {
+		struct {
+			__aligned_u64 tp_name; /* in/out: tp_name buffer ptr */
+			__u32 tp_name_len;     /* in/out: tp_name buffer len */
+		} raw_tracepoint;
+		struct {
+			__u32 attach_type;
+		} tracing;

On the RFC I asked whether we could get the attach target here as well. You said you'd look into it; what happened to that? :)

-Toke

#13 lore raw

Re: [PATCH v2 bpf-next 04/10] bpf: add support for BPF_OBJ_GET_INFO_BY_FD for bpf_link

Quoted 114 lines · Toke Høiland-Jørgensen
Quoted 111 lines · Andrii Nakryiko

Add ability to fetch bpf_link details through BPF_OBJ_GET_INFO_BY_FD command. Also enhance show_fdinfo to potentially include bpf_link type-specific information (similarly to obj_info).

Also introduce enum bpf_link_type stored in bpf_link itself and expose it in UAPI. bpf_link_tracing also now will store and return bpf_attach_type.

---
 include/linux/bpf-cgroup.h     |   2 -
 include/linux/bpf.h            |  10 +-
 include/linux/bpf_types.h      |   6 ++
 include/uapi/linux/bpf.h       |  28 ++++++
 kernel/bpf/btf.c               |   2 +
 kernel/bpf/cgroup.c            |  45 ++++++++-
 kernel/bpf/syscall.c           | 164 +++++++++++++++++++++++++++++----
 kernel/bpf/verifier.c          |   2 +
 tools/include/uapi/linux/bpf.h |  31 +++++++
 9 files changed, 266 insertions(+), 24 deletions(-)
include/linux/bpf-cgroup.h +0 -2
index d2d969669564..ab95824a1d99 100644
@@ -57,8 +57,6 @@ struct bpf_cgroup_link {
      enum bpf_attach_type type;
 };
 
-extern const struct bpf_link_ops bpf_cgroup_link_lops;
-
 struct bpf_prog_list {
      struct list_head node;
      struct bpf_prog *prog;
include/linux/bpf.h +9 -1
index 875d1f0af803..701c4387c711 100644
@@ -1026,9 +1026,11 @@ extern const struct file_operations bpf_prog_fops;
      extern const struct bpf_verifier_ops _name ## _verifier_ops;
 #define BPF_MAP_TYPE(_id, _ops) \
      extern const struct bpf_map_ops _ops;
+#define BPF_LINK_TYPE(_id, _name)
 #include <linux/bpf_types.h>
 #undef BPF_PROG_TYPE
 #undef BPF_MAP_TYPE
+#undef BPF_LINK_TYPE
 
 extern const struct bpf_prog_ops bpf_offload_prog_ops;
 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
@@ -1086,6 +1088,7 @@ int bpf_prog_new_fd(struct bpf_prog *prog);
 struct bpf_link {
      atomic64_t refcnt;
      u32 id;
+     enum bpf_link_type type;
      const struct bpf_link_ops *ops;
      struct bpf_prog *prog;
      struct work_struct work;
@@ -1103,9 +1106,14 @@ struct bpf_link_ops {
      void (*dealloc)(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);
+     int (*fill_link_info)(const struct bpf_link *link,
+                           struct bpf_link_info *info,
+                           const struct bpf_link_info *uinfo,
+                           u32 info_len);
 };
 
-void bpf_link_init(struct bpf_link *link,
+void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
                 const struct bpf_link_ops *ops, struct bpf_prog *prog);
 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
 int bpf_link_settle(struct bpf_link_primer *primer);
include/linux/bpf_types.h +6 -0
index ba0c2d56f8a3..8345cdf553b8 100644
@@ -118,3 +118,9 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_STACK, stack_map_ops)
 #if defined(CONFIG_BPF_JIT)
 BPF_MAP_TYPE(BPF_MAP_TYPE_STRUCT_OPS, bpf_struct_ops_map_ops)
 #endif
+
+BPF_LINK_TYPE(BPF_LINK_TYPE_RAW_TRACEPOINT, raw_tracepoint)
+BPF_LINK_TYPE(BPF_LINK_TYPE_TRACING, tracing)
+#ifdef CONFIG_CGROUP_BPF
+BPF_LINK_TYPE(BPF_LINK_TYPE_CGROUP, cgroup)
+#endif
include/uapi/linux/bpf.h +9 -0
index 7e6541fceade..0eccafae55bb 100644
@@ -222,6 +222,15 @@ enum bpf_attach_type {
 
 #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
 
+enum bpf_link_type {
+     BPF_LINK_TYPE_UNSPEC = 0,
+     BPF_LINK_TYPE_RAW_TRACEPOINT = 1,
+     BPF_LINK_TYPE_TRACING = 2,
+     BPF_LINK_TYPE_CGROUP = 3,
+
+     MAX_BPF_LINK_TYPE,
+};
+
 /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command
  *
  * NONE(default): No further bpf programs allowed in the subtree.
@@ -3612,6 +3621,25 @@ struct bpf_btf_info {
      __u32 id;
 } __attribute__((aligned(8)));
 
Quoted 16 lines

include/uapi/linux/bpf.h struct bpf_btf_info { · line 3635

Quoted 12 lines
include/uapi/linux/bpf.h +12 -0
+struct bpf_link_info {
+     __u32 type;
+     __u32 id;
+     __u32 prog_id;
+     union {
+             struct {
+                     __aligned_u64 tp_name; /* in/out: tp_name buffer ptr */
+                     __u32 tp_name_len;     /* in/out: tp_name buffer len */
+             } raw_tracepoint;
+             struct {
+                     __u32 attach_type;
+             } tracing;

On the RFC I asked whether we could get the attach target here as well. You said you'd look into it; what happened to that? :)

Right, sorry, forgot to mention this. I did take a look, but tracing links are quite diverse, so I didn't see one clear way to structure such "target" information, so I'd say we should push it into a separate patch/discussion. E.g., fentry/fexit/fmod_exit are attached to kernel functions (how do we represent that), freplace are attached to another BPF program (this is a bit clearer how to represent, but how do we combine that with fentry/fexit info?). LSM is also attached to kernel function, but who knows, maybe we want slightly more extended semantics for it. Either way, I don't see one best way to structure this information and would want to avoid blocking on this for this series. Also bpf_link_info is extensible, so it's not a problem to extend it in follow up patches.

Does it make sense?

Quoted 2 lines

-Toke

#14 lore raw

Re: [PATCH v2 bpf-next 04/10] bpf: add support for BPF_OBJ_GET_INFO_BY_FD for bpf_link

Quoted 145 lines · Andrii Nakryiko
Quoted 114 lines · Toke Høiland-Jørgensen
Quoted 111 lines · Andrii Nakryiko

Add ability to fetch bpf_link details through BPF_OBJ_GET_INFO_BY_FD command. Also enhance show_fdinfo to potentially include bpf_link type-specific information (similarly to obj_info).

Also introduce enum bpf_link_type stored in bpf_link itself and expose it in UAPI. bpf_link_tracing also now will store and return bpf_attach_type.

---
 include/linux/bpf-cgroup.h     |   2 -
 include/linux/bpf.h            |  10 +-
 include/linux/bpf_types.h      |   6 ++
 include/uapi/linux/bpf.h       |  28 ++++++
 kernel/bpf/btf.c               |   2 +
 kernel/bpf/cgroup.c            |  45 ++++++++-
 kernel/bpf/syscall.c           | 164 +++++++++++++++++++++++++++++----
 kernel/bpf/verifier.c          |   2 +
 tools/include/uapi/linux/bpf.h |  31 +++++++
 9 files changed, 266 insertions(+), 24 deletions(-)
include/linux/bpf-cgroup.h +0 -2
index d2d969669564..ab95824a1d99 100644
@@ -57,8 +57,6 @@ struct bpf_cgroup_link {
      enum bpf_attach_type type;
 };
 
-extern const struct bpf_link_ops bpf_cgroup_link_lops;
-
 struct bpf_prog_list {
      struct list_head node;
      struct bpf_prog *prog;
include/linux/bpf.h +9 -1
index 875d1f0af803..701c4387c711 100644
@@ -1026,9 +1026,11 @@ extern const struct file_operations bpf_prog_fops;
      extern const struct bpf_verifier_ops _name ## _verifier_ops;
 #define BPF_MAP_TYPE(_id, _ops) \
      extern const struct bpf_map_ops _ops;
+#define BPF_LINK_TYPE(_id, _name)
 #include <linux/bpf_types.h>
 #undef BPF_PROG_TYPE
 #undef BPF_MAP_TYPE
+#undef BPF_LINK_TYPE
 
 extern const struct bpf_prog_ops bpf_offload_prog_ops;
 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
@@ -1086,6 +1088,7 @@ int bpf_prog_new_fd(struct bpf_prog *prog);
 struct bpf_link {
      atomic64_t refcnt;
      u32 id;
+     enum bpf_link_type type;
      const struct bpf_link_ops *ops;
      struct bpf_prog *prog;
      struct work_struct work;
@@ -1103,9 +1106,14 @@ struct bpf_link_ops {
      void (*dealloc)(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);
+     int (*fill_link_info)(const struct bpf_link *link,
+                           struct bpf_link_info *info,
+                           const struct bpf_link_info *uinfo,
+                           u32 info_len);
 };
 
-void bpf_link_init(struct bpf_link *link,
+void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
                 const struct bpf_link_ops *ops, struct bpf_prog *prog);
 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
 int bpf_link_settle(struct bpf_link_primer *primer);
include/linux/bpf_types.h +6 -0
index ba0c2d56f8a3..8345cdf553b8 100644
@@ -118,3 +118,9 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_STACK, stack_map_ops)
 #if defined(CONFIG_BPF_JIT)
 BPF_MAP_TYPE(BPF_MAP_TYPE_STRUCT_OPS, bpf_struct_ops_map_ops)
 #endif
+
+BPF_LINK_TYPE(BPF_LINK_TYPE_RAW_TRACEPOINT, raw_tracepoint)
+BPF_LINK_TYPE(BPF_LINK_TYPE_TRACING, tracing)
+#ifdef CONFIG_CGROUP_BPF
+BPF_LINK_TYPE(BPF_LINK_TYPE_CGROUP, cgroup)
+#endif
include/uapi/linux/bpf.h +9 -0
index 7e6541fceade..0eccafae55bb 100644
@@ -222,6 +222,15 @@ enum bpf_attach_type {
 
 #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
 
+enum bpf_link_type {
+     BPF_LINK_TYPE_UNSPEC = 0,
+     BPF_LINK_TYPE_RAW_TRACEPOINT = 1,
+     BPF_LINK_TYPE_TRACING = 2,
+     BPF_LINK_TYPE_CGROUP = 3,
+
+     MAX_BPF_LINK_TYPE,
+};
+
 /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command
  *
  * NONE(default): No further bpf programs allowed in the subtree.
@@ -3612,6 +3621,25 @@ struct bpf_btf_info {
      __u32 id;
 } __attribute__((aligned(8)));
 
Quoted 16 lines

include/uapi/linux/bpf.h struct bpf_btf_info { · line 3635

Quoted 12 lines
include/uapi/linux/bpf.h +12 -0
+struct bpf_link_info {
+     __u32 type;
+     __u32 id;
+     __u32 prog_id;
+     union {
+             struct {
+                     __aligned_u64 tp_name; /* in/out: tp_name buffer ptr */
+                     __u32 tp_name_len;     /* in/out: tp_name buffer len */
+             } raw_tracepoint;
+             struct {
+                     __u32 attach_type;
+             } tracing;

On the RFC I asked whether we could get the attach target here as well. You said you'd look into it; what happened to that? :)

Right, sorry, forgot to mention this. I did take a look, but tracing links are quite diverse, so I didn't see one clear way to structure such "target" information, so I'd say we should push it into a separate patch/discussion. E.g., fentry/fexit/fmod_exit are attached to kernel functions (how do we represent that), freplace are attached to another BPF program (this is a bit clearer how to represent, but how do we combine that with fentry/fexit info?). LSM is also attached to kernel function, but who knows, maybe we want slightly more extended semantics for it. Either way, I don't see one best way to structure this information and would want to avoid blocking on this for this series. Also bpf_link_info is extensible, so it's not a problem to extend it in follow up patches.

Quoted 1 line

Does it make sense?

Yup, fair enough, I can live with deferring this to a later series :)

-Toke

#15 lore raw

Re: [PATCH v2 bpf-next 04/10] bpf: add support for BPF_OBJ_GET_INFO_BY_FD for bpf_link

In reply to Andrii Nakryiko
I love your patch! Yet something to improve:

Hi Andrii,

I love your patch! Yet something to improve:

[auto build test ERROR on bpf-next/master]
[also build test ERROR on bpf/master cgroup/for-next net/master net-next/master v5.7-rc3 next-20200428] [if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Andrii-Nakryiko/bpf_link-observability-APIs/20200428-215720
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: sh-randconfig-a001-20200428 (attached as .config)
compiler: sh4-linux-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day GCC_VERSION=9.3.0 make.cross ARCH=sh

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

sh4-linux-ld: kernel/bpf/syscall.o: in function `bpf_raw_tp_link_fill_link_info':

>> kernel/bpf/syscall.c:2570: undefined reference to `__get_user_unknown'

vim +2570 kernel/bpf/syscall.c

2547
2548 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,

  2549						  struct bpf_link_info *info,
  2550						  const struct bpf_link_info *uinfo,
  2551						  u32 info_len)
  2552	{
  2553		struct bpf_raw_tp_link *raw_tp_link =
  2554			container_of(link, struct bpf_raw_tp_link, link);
  2555		u64 ubuf_ptr;
  2556		char __user *ubuf = u64_to_user_ptr(uinfo->raw_tracepoint.tp_name);
  2557		const char *tp_name = raw_tp_link->btp->tp->name;
  2558		size_t tp_len;
  2559		u32 ulen;
  2560	
  2561		if (get_user(ulen, &uinfo->raw_tracepoint.tp_name_len))
  2562			return -EFAULT;
  2563		if (get_user(ubuf_ptr, &uinfo->raw_tracepoint.tp_name))
  2564			return -EFAULT;
  2565		ubuf = u64_to_user_ptr(ubuf_ptr);
  2566	
  2567		if (ulen && !ubuf)
  2568			return -EINVAL;
  2569		if (!ubuf)
> 2570			return 0;

2571
2572 tp_len = strlen(raw_tp_link->btp->tp->name);
2573 info->raw_tracepoint.tp_name_len = tp_len + 1;
2574 info->raw_tracepoint.tp_name = (u64)(unsigned long)ubuf;
2575
2576 if (ulen >= tp_len + 1) {
2577 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2578 return -EFAULT;
2579 } else {
2580 char zero = '\0';
2581
2582 if (copy_to_user(ubuf, tp_name, ulen - 1))
2583 return -EFAULT;
2584 if (put_user(zero, ubuf + ulen - 1))
2585 return -EFAULT;
2586 return -ENOSPC;
2587 }
2588
2589 return 0;
2590 }
2591

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#16 lore raw

PATCH v2 5/10 libbpf: add low-level APIs for new bpf_link commands

In reply to Andrii Nakryiko

Add low-level API calls for bpf_link_get_next_id() and bpf_link_get_fd_by_id().

---
 tools/lib/bpf/bpf.c      | 19 +++++++++++++++++--
 tools/lib/bpf/bpf.h      |  4 +++-
 tools/lib/bpf/libbpf.map |  6 ++++++
 3 files changed, 26 insertions(+), 3 deletions(-)
tools/lib/bpf/bpf.c +17 -2
index 5cc1b0785d18..8f2f0958d446 100644
@@ -721,6 +721,11 @@ int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id)
 	return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID);
 }
 
+int bpf_link_get_next_id(__u32 start_id, __u32 *next_id)
+{
+	return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID);
+}
+
 int bpf_prog_get_fd_by_id(__u32 id)
 {
 	union bpf_attr attr;
@@ -751,13 +756,23 @@ int bpf_btf_get_fd_by_id(__u32 id)
 	return sys_bpf(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr));
 }
 
-int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
+int bpf_link_get_fd_by_id(__u32 id)
+{
+	union bpf_attr attr;
+
+	memset(&attr, 0, sizeof(attr));
+	attr.link_id = id;
+
+	return sys_bpf(BPF_LINK_GET_FD_BY_ID, &attr, sizeof(attr));
+}
+
+int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)
 {
 	union bpf_attr attr;
 	int err;
 
 	memset(&attr, 0, sizeof(attr));
-	attr.info.bpf_fd = prog_fd;
+	attr.info.bpf_fd = bpf_fd;
 	attr.info.info_len = *info_len;
 	attr.info.info = ptr_to_u64(info);
 
tools/lib/bpf/bpf.h +3 -1
index 46d47afdd887..335b457b3a25 100644
@@ -216,10 +216,12 @@ LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
 LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
 LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
 LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
+LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
 LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
 LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
-LIBBPF_API int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len);
+LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
+LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
 LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
 			      __u32 query_flags, __u32 *attach_flags,
 			      __u32 *prog_ids, __u32 *prog_cnt);
tools/lib/bpf/libbpf.map +6 -0
index bb8831605b25..7cd49aa38005 100644
@@ -254,3 +254,9 @@ LIBBPF_0.0.8 {
 		bpf_program__set_lsm;
 		bpf_set_link_xdp_fd_opts;
 } LIBBPF_0.0.7;
+
+LIBBPF_0.0.9 {
+	global:
+		bpf_link_get_fd_by_id;
+		bpf_link_get_next_id;
+} LIBBPF_0.0.8;
Signature · 1 line
2.24.1
#17 lore raw

PATCH v2 6/10 selftests/bpf: test bpf_link's get_next_id, get_fd_by_id, and get_obj_info

In reply to Andrii Nakryiko

Extend bpf_obj_id selftest to verify bpf_link's observability APIs.

---
 .../selftests/bpf/prog_tests/bpf_obj_id.c     | 110 ++++++++++++++++--
 .../testing/selftests/bpf/progs/test_obj_id.c |  14 +--
 2 files changed, 104 insertions(+), 20 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_obj_id.c +101 -9
index f10029821e16..7afa4160416f 100644
@@ -1,26 +1,30 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <test_progs.h>
 
+#define nr_iters 2
+
 void test_bpf_obj_id(void)
 {
 	const __u64 array_magic_value = 0xfaceb00c;
 	const __u32 array_key = 0;
-	const int nr_iters = 2;
 	const char *file = "./test_obj_id.o";
 	const char *expected_prog_name = "test_obj_id";
 	const char *expected_map_name = "test_map_id";
 	const __u64 nsec_per_sec = 1000000000;
 
-	struct bpf_object *objs[nr_iters];
+	struct bpf_object *objs[nr_iters] = {};
+	struct bpf_link *links[nr_iters] = {};
+	struct bpf_program *prog;
 	int prog_fds[nr_iters], map_fds[nr_iters];
 	/* +1 to test for the info_len returned by kernel */
 	struct bpf_prog_info prog_infos[nr_iters + 1];
 	struct bpf_map_info map_infos[nr_iters + 1];
+	struct bpf_link_info link_infos[nr_iters + 1];
 	/* Each prog only uses one map. +1 to test nr_map_ids
 	 * returned by kernel.
 	 */
 	__u32 map_ids[nr_iters + 1];
-	char jited_insns[128], xlated_insns[128], zeros[128];
+	char jited_insns[128], xlated_insns[128], zeros[128], tp_name[128];
 	__u32 i, next_id, info_len, nr_id_found, duration = 0;
 	struct timespec real_time_ts, boot_time_ts;
 	int err = 0;
@@ -36,14 +40,15 @@ void test_bpf_obj_id(void)
 	CHECK(err >= 0 || errno != ENOENT,
 	      "get-fd-by-notexist-map-id", "err %d errno %d\n", err, errno);
 
-	for (i = 0; i < nr_iters; i++)
-		objs[i] = NULL;
+	err = bpf_link_get_fd_by_id(0);
+	CHECK(err >= 0 || errno != ENOENT,
+	      "get-fd-by-notexist-link-id", "err %d errno %d\n", err, errno);
 
 	/* Check bpf_obj_get_info_by_fd() */
 	bzero(zeros, sizeof(zeros));
 	for (i = 0; i < nr_iters; i++) {
 		now = time(NULL);
-		err = bpf_prog_load(file, BPF_PROG_TYPE_SOCKET_FILTER,
+		err = bpf_prog_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT,
 				    &objs[i], &prog_fds[i]);
 		/* test_obj_id.o is a dumb prog. It should never fail
 		 * to load.
@@ -60,6 +65,17 @@ void test_bpf_obj_id(void)
 		if (CHECK_FAIL(err))
 			goto done;
 
+		prog = bpf_object__find_program_by_title(objs[i],
+							 "raw_tp/sys_enter");
+		if (CHECK_FAIL(!prog))
+			goto done;
+		links[i] = bpf_program__attach(prog);
+		err = libbpf_get_error(links[i]);
+		if (CHECK(err, "prog_attach", "prog #%d, err %d\n", i, err)) {
+			links[i] = NULL;
+			goto done;
+		}
+
 		/* Check getting map info */
 		info_len = sizeof(struct bpf_map_info) * 2;
 		bzero(&map_infos[i], info_len);
@@ -107,7 +123,7 @@ void test_bpf_obj_id(void)
 		load_time = (real_time_ts.tv_sec - boot_time_ts.tv_sec)
 			+ (prog_infos[i].load_time / nsec_per_sec);
 		if (CHECK(err ||
-			  prog_infos[i].type != BPF_PROG_TYPE_SOCKET_FILTER ||
+			  prog_infos[i].type != BPF_PROG_TYPE_RAW_TRACEPOINT ||
 			  info_len != sizeof(struct bpf_prog_info) ||
 			  (env.jit_enabled && !prog_infos[i].jited_prog_len) ||
 			  (env.jit_enabled &&
@@ -120,7 +136,11 @@ void test_bpf_obj_id(void)
 			  *(int *)(long)prog_infos[i].map_ids != map_infos[i].id ||
 			  strcmp((char *)prog_infos[i].name, expected_prog_name),
 			  "get-prog-info(fd)",
-			  "err %d errno %d i %d type %d(%d) info_len %u(%zu) jit_enabled %d jited_prog_len %u xlated_prog_len %u jited_prog %d xlated_prog %d load_time %lu(%lu) uid %u(%u) nr_map_ids %u(%u) map_id %u(%u) name %s(%s)\n",
+			  "err %d errno %d i %d type %d(%d) info_len %u(%zu) "
+			  "jit_enabled %d jited_prog_len %u xlated_prog_len %u "
+			  "jited_prog %d xlated_prog %d load_time %lu(%lu) "
+			  "uid %u(%u) nr_map_ids %u(%u) map_id %u(%u) "
+			  "name %s(%s)\n",
 			  err, errno, i,
 			  prog_infos[i].type, BPF_PROG_TYPE_SOCKET_FILTER,
 			  info_len, sizeof(struct bpf_prog_info),
@@ -135,6 +155,33 @@ void test_bpf_obj_id(void)
 			  *(int *)(long)prog_infos[i].map_ids, map_infos[i].id,
 			  prog_infos[i].name, expected_prog_name))
 			goto done;
+
+		/* Check getting link info */
+		info_len = sizeof(struct bpf_link_info) * 2;
+		bzero(&link_infos[i], info_len);
+		link_infos[i].raw_tracepoint.tp_name = (__u64)&tp_name;
+		link_infos[i].raw_tracepoint.tp_name_len = sizeof(tp_name);
+		err = bpf_obj_get_info_by_fd(bpf_link__fd(links[i]),
+					     &link_infos[i], &info_len);
+		if (CHECK(err ||
+			  link_infos[i].type != BPF_LINK_TYPE_RAW_TRACEPOINT ||
+			  link_infos[i].prog_id != prog_infos[i].id ||
+			  link_infos[i].raw_tracepoint.tp_name != (__u64)&tp_name ||
+			  strcmp((char *)link_infos[i].raw_tracepoint.tp_name,
+				 "sys_enter") ||
+			  info_len != sizeof(struct bpf_link_info),
+			  "get-link-info(fd)",
+			  "err %d errno %d info_len %u(%zu) type %d(%d) id %d "
+			  "prog_id %d (%d) tp_name %s(%s)\n",
+			  err, errno,
+			  info_len, sizeof(struct bpf_link_info),
+			  link_infos[i].type, BPF_LINK_TYPE_RAW_TRACEPOINT,
+			  link_infos[i].id,
+			  link_infos[i].prog_id, prog_infos[i].id,
+			  (char *)link_infos[i].raw_tracepoint.tp_name,
+			  "sys_enter"))
+			goto done;
+
 	}
 
 	/* Check bpf_prog_get_next_id() */
@@ -247,7 +294,52 @@ void test_bpf_obj_id(void)
 	      "nr_id_found %u(%u)\n",
 	      nr_id_found, nr_iters);
 
+	/* Check bpf_link_get_next_id() */
+	nr_id_found = 0;
+	next_id = 0;
+	while (!bpf_link_get_next_id(next_id, &next_id)) {
+		struct bpf_link_info link_info;
+		int link_fd, cmp_res;
+
+		info_len = sizeof(link_info);
+		memset(&link_info, 0, info_len);
+
+		link_fd = bpf_link_get_fd_by_id(next_id);
+		if (link_fd < 0 && errno == ENOENT)
+			/* The bpf_link is in the dead row */
+			continue;
+		if (CHECK(link_fd < 0, "get-link-fd(next_id)",
+			  "link_fd %d next_id %u errno %d\n",
+			  link_fd, next_id, errno))
+			break;
+
+		for (i = 0; i < nr_iters; i++)
+			if (link_infos[i].id == next_id)
+				break;
+
+		if (i == nr_iters)
+			continue;
+
+		nr_id_found++;
+
+		err = bpf_obj_get_info_by_fd(link_fd, &link_info, &info_len);
+		cmp_res = memcmp(&link_info, &link_infos[i],
+				offsetof(struct bpf_link_info, raw_tracepoint));
+		CHECK(err || info_len != sizeof(link_info) || cmp_res,
+		      "check get-link-info(next_id->fd)",
+		      "err %d errno %d info_len %u(%zu) memcmp %d\n",
+		      err, errno, info_len, sizeof(struct bpf_link_info),
+		      cmp_res);
+
+		close(link_fd);
+	}
+	CHECK(nr_id_found != nr_iters,
+	      "check total link id found by get_next_id",
+	      "nr_id_found %u(%u)\n", nr_id_found, nr_iters);
+
 done:
-	for (i = 0; i < nr_iters; i++)
+	for (i = 0; i < nr_iters; i++) {
+		bpf_link__destroy(links[i]);
 		bpf_object__close(objs[i]);
+	}
 }
tools/testing/selftests/bpf/progs/test_obj_id.c +3 -11
index 98b9de2fafd0..ded71b3ff6b4 100644
@@ -3,16 +3,8 @@
  */
 #include <stddef.h>
 #include <linux/bpf.h>
-#include <linux/pkt_cls.h>
 #include <bpf/bpf_helpers.h>
 
-/* It is a dumb bpf program such that it must have no
- * issue to be loaded since testing the verifier is
- * not the focus here.
- */
-
-int _version SEC("version") = 1;
-
 struct {
 	__uint(type, BPF_MAP_TYPE_ARRAY);
 	__uint(max_entries, 1);
@@ -20,13 +12,13 @@ struct {
 	__type(value, __u64);
 } test_map_id SEC(".maps");
 
-SEC("test_obj_id_dummy")
-int test_obj_id(struct __sk_buff *skb)
+SEC("raw_tp/sys_enter")
+int test_obj_id(void *ctx)
 {
 	__u32 key = 0;
 	__u64 *value;
 
 	value = bpf_map_lookup_elem(&test_map_id, &key);
 
-	return TC_ACT_OK;
+	return 0;
 }
Signature · 1 line
2.24.1
#18 lore raw

PATCH v2 7/10 bpftool: expose attach_type-to-string array to non-cgroup code

In reply to Andrii Nakryiko

Move attach_type_strings into main.h for access in non-cgroup code. bpf_attach_type is used for non-cgroup attach types quite widely now. So also complete missing string translations for non-cgroup attach types.

---
 tools/bpf/bpftool/cgroup.c | 48 ++++++++++++--------------------------
 tools/bpf/bpftool/main.h   | 32 +++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 33 deletions(-)
tools/bpf/bpftool/cgroup.c +15 -33
index 62c6a1d7cd18..1693c802bb20 100644
@@ -31,42 +31,20 @@
 
 static unsigned int query_flags;
 
-static const char * const attach_type_strings[] = {
-	[BPF_CGROUP_INET_INGRESS] = "ingress",
-	[BPF_CGROUP_INET_EGRESS] = "egress",
-	[BPF_CGROUP_INET_SOCK_CREATE] = "sock_create",
-	[BPF_CGROUP_SOCK_OPS] = "sock_ops",
-	[BPF_CGROUP_DEVICE] = "device",
-	[BPF_CGROUP_INET4_BIND] = "bind4",
-	[BPF_CGROUP_INET6_BIND] = "bind6",
-	[BPF_CGROUP_INET4_CONNECT] = "connect4",
-	[BPF_CGROUP_INET6_CONNECT] = "connect6",
-	[BPF_CGROUP_INET4_POST_BIND] = "post_bind4",
-	[BPF_CGROUP_INET6_POST_BIND] = "post_bind6",
-	[BPF_CGROUP_UDP4_SENDMSG] = "sendmsg4",
-	[BPF_CGROUP_UDP6_SENDMSG] = "sendmsg6",
-	[BPF_CGROUP_SYSCTL] = "sysctl",
-	[BPF_CGROUP_UDP4_RECVMSG] = "recvmsg4",
-	[BPF_CGROUP_UDP6_RECVMSG] = "recvmsg6",
-	[BPF_CGROUP_GETSOCKOPT] = "getsockopt",
-	[BPF_CGROUP_SETSOCKOPT] = "setsockopt",
-	[__MAX_BPF_ATTACH_TYPE] = NULL,
-};
-
 static enum bpf_attach_type parse_attach_type(const char *str)
 {
 	enum bpf_attach_type type;
 
 	for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
-		if (attach_type_strings[type] &&
-		    is_prefix(str, attach_type_strings[type]))
+		if (attach_type_name[type] &&
+		    is_prefix(str, attach_type_name[type]))
 			return type;
 	}
 
 	return __MAX_BPF_ATTACH_TYPE;
 }
 
-static int show_bpf_prog(int id, const char *attach_type_str,
+static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
 			 const char *attach_flags_str,
 			 int level)
 {
@@ -86,18 +64,22 @@ static int show_bpf_prog(int id, const char *attach_type_str,
 	if (json_output) {
 		jsonw_start_object(json_wtr);
 		jsonw_uint_field(json_wtr, "id", info.id);
-		jsonw_string_field(json_wtr, "attach_type",
-				   attach_type_str);
+		if (attach_type < ARRAY_SIZE(attach_type_name))
+			jsonw_string_field(json_wtr, "attach_type",
+					   attach_type_name[attach_type]);
+		else
+			jsonw_uint_field(json_wtr, "attach_type", attach_type);
 		jsonw_string_field(json_wtr, "attach_flags",
 				   attach_flags_str);
 		jsonw_string_field(json_wtr, "name", info.name);
 		jsonw_end_object(json_wtr);
 	} else {
-		printf("%s%-8u %-15s %-15s %-15s\n", level ? "    " : "",
-		       info.id,
-		       attach_type_str,
-		       attach_flags_str,
-		       info.name);
+		printf("%s%-8u ", level ? "    " : "", info.id);
+		if (attach_type < ARRAY_SIZE(attach_type_name))
+			printf("%-15s", attach_type_name[attach_type]);
+		else
+			printf("type %-10u", attach_type);
+		printf(" %-15s %-15s\n", attach_flags_str, info.name);
 	}
 
 	close(prog_fd);
@@ -171,7 +153,7 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
 	}
 
 	for (iter = 0; iter < prog_cnt; iter++)
-		show_bpf_prog(prog_ids[iter], attach_type_strings[type],
+		show_bpf_prog(prog_ids[iter], type,
 			      attach_flags_str, level);
 
 	return 0;
tools/bpf/bpftool/main.h +32 -0
index 86f14ce26fd7..99d84bd1d5b2 100644
@@ -83,6 +83,38 @@ static const char * const prog_type_name[] = {
 	[BPF_PROG_TYPE_EXT]			= "ext",
 };
 
+static const char * const attach_type_name[__MAX_BPF_ATTACH_TYPE] = {
+	[BPF_CGROUP_INET_INGRESS] = "ingress",
+	[BPF_CGROUP_INET_EGRESS] = "egress",
+	[BPF_CGROUP_INET_SOCK_CREATE] = "sock_create",
+	[BPF_CGROUP_SOCK_OPS] = "sock_ops",
+	[BPF_CGROUP_DEVICE] = "device",
+	[BPF_CGROUP_INET4_BIND] = "bind4",
+	[BPF_CGROUP_INET6_BIND] = "bind6",
+	[BPF_CGROUP_INET4_CONNECT] = "connect4",
+	[BPF_CGROUP_INET6_CONNECT] = "connect6",
+	[BPF_CGROUP_INET4_POST_BIND] = "post_bind4",
+	[BPF_CGROUP_INET6_POST_BIND] = "post_bind6",
+	[BPF_CGROUP_UDP4_SENDMSG] = "sendmsg4",
+	[BPF_CGROUP_UDP6_SENDMSG] = "sendmsg6",
+	[BPF_CGROUP_SYSCTL] = "sysctl",
+	[BPF_CGROUP_UDP4_RECVMSG] = "recvmsg4",
+	[BPF_CGROUP_UDP6_RECVMSG] = "recvmsg6",
+	[BPF_CGROUP_GETSOCKOPT] = "getsockopt",
+	[BPF_CGROUP_SETSOCKOPT] = "setsockopt",
+
+	[BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser",
+	[BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict",
+	[BPF_SK_MSG_VERDICT] = "sk_msg_verdict",
+	[BPF_LIRC_MODE2] = "lirc_mode2",
+	[BPF_FLOW_DISSECTOR] = "flow_dissector",
+	[BPF_TRACE_RAW_TP] = "raw_tp",
+	[BPF_TRACE_FENTRY] = "fentry",
+	[BPF_TRACE_FEXIT] = "fexit",
+	[BPF_MODIFY_RETURN] = "mod_ret",
+	[BPF_LSM_MAC] = "lsm_mac",
+};
+
 extern const char * const map_type_name[];
 extern const size_t map_type_name_size;
 
Signature · 1 line
2.24.1
Reviewed-by on v2 7/10
#19 lore raw

Re: [PATCH v2 bpf-next 07/10] bpftool: expose attach_type-to-string array to non-cgroup code

2020-04-27 22:49 UTC-0700 ~ Andrii Nakryiko <[email protected]>

Quoted 7 lines

Move attach_type_strings into main.h for access in non-cgroup code. bpf_attach_type is used for non-cgroup attach types quite widely now. So also complete missing string translations for non-cgroup attach types.

---

Thank you Andrii

#20 lore raw

PATCH v2 8/10 bpftool: add bpf_link show and pin support

In reply to Andrii Nakryiko

Add bpftool link show and bpftool link pin commands.

Example plain output for link show (with showing pinned paths):

[vmuser@archvm bpf]$ sudo ~/local/linux/tools/bpf/bpftool/bpftool -f link 1: tracing prog 12
prog_type tracing attach_type fentry
pinned /sys/fs/bpf/my_test_link
pinned /sys/fs/bpf/my_test_link2
2: tracing prog 13
prog_type tracing attach_type fentry
3: tracing prog 14
prog_type tracing attach_type fentry
4: tracing prog 15
prog_type tracing attach_type fentry
5: tracing prog 16
prog_type tracing attach_type fentry
6: tracing prog 17
prog_type tracing attach_type fentry
7: raw_tracepoint prog 21
tp 'sys_enter'
8: cgroup prog 25
cgroup_id 584 attach_type egress
9: cgroup prog 25
cgroup_id 599 attach_type egress
10: cgroup prog 25
cgroup_id 614 attach_type egress
11: cgroup prog 25
cgroup_id 629 attach_type egress

---
 tools/bpf/bpftool/common.c |   2 +
 tools/bpf/bpftool/link.c   | 333 +++++++++++++++++++++++++++++++++++++
 tools/bpf/bpftool/main.c   |   6 +-
 tools/bpf/bpftool/main.h   |   5 +
 4 files changed, 345 insertions(+), 1 deletion(-)
 create mode 100644 tools/bpf/bpftool/link.c
tools/bpf/bpftool/common.c +2 -0
index f2223dbdfb0a..c47bdc65de8e 100644
@@ -262,6 +262,8 @@ int get_fd_type(int fd)
 		return BPF_OBJ_MAP;
 	else if (strstr(buf, "bpf-prog"))
 		return BPF_OBJ_PROG;
+	else if (strstr(buf, "bpf-link"))
+		return BPF_OBJ_LINK;
 
 	return BPF_OBJ_UNKNOWN;
 }
tools/bpf/bpftool/link.c new file +333 -0

333 lines not on this page · raw message

tools/bpf/bpftool/main.c +5 -1
index 466c269eabdd..1413a154806e 100644
@@ -30,6 +30,7 @@ bool verifier_logs;
 bool relaxed_maps;
 struct pinned_obj_table prog_table;
 struct pinned_obj_table map_table;
+struct pinned_obj_table link_table;
 
 static void __noreturn clean_and_exit(int i)
 {
@@ -58,7 +59,7 @@ static int do_help(int argc, char **argv)
 		"       %s batch file FILE\n"
 		"       %s version\n"
 		"\n"
-		"       OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen | struct_ops }\n"
+		"       OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops }\n"
 		"       " HELP_SPEC_OPTIONS "\n"
 		"",
 		bin_name, bin_name, bin_name);
@@ -215,6 +216,7 @@ static const struct cmd cmds[] = {
 	{ "batch",	do_batch },
 	{ "prog",	do_prog },
 	{ "map",	do_map },
+	{ "link",	do_link },
 	{ "cgroup",	do_cgroup },
 	{ "perf",	do_perf },
 	{ "net",	do_net },
@@ -364,6 +366,7 @@ int main(int argc, char **argv)
 
 	hash_init(prog_table.table);
 	hash_init(map_table.table);
+	hash_init(link_table.table);
 
 	opterr = 0;
 	while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
@@ -422,6 +425,7 @@ int main(int argc, char **argv)
 	if (show_pinned) {
 		delete_pinned_obj_table(&prog_table);
 		delete_pinned_obj_table(&map_table);
+		delete_pinned_obj_table(&link_table);
 	}
 
 	return ret;
tools/bpf/bpftool/main.h +5 -0
index 99d84bd1d5b2..9b1fb81a8331 100644
@@ -50,6 +50,8 @@
 	"\t            {-m|--mapcompat} | {-n|--nomount} }"
 #define HELP_SPEC_MAP							\
 	"MAP := { id MAP_ID | pinned FILE | name MAP_NAME }"
+#define HELP_SPEC_LINK							\
+	"LINK := { id LINK_ID | pinned FILE }"
 
 static const char * const prog_type_name[] = {
 	[BPF_PROG_TYPE_UNSPEC]			= "unspec",
@@ -122,6 +124,7 @@ enum bpf_obj_type {
 	BPF_OBJ_UNKNOWN,
 	BPF_OBJ_PROG,
 	BPF_OBJ_MAP,
+	BPF_OBJ_LINK,
 };
 
 extern const char *bin_name;
@@ -134,6 +137,7 @@ extern bool verifier_logs;
 extern bool relaxed_maps;
 extern struct pinned_obj_table prog_table;
 extern struct pinned_obj_table map_table;
+extern struct pinned_obj_table link_table;
 
 void __printf(1, 2) p_err(const char *fmt, ...);
 void __printf(1, 2) p_info(const char *fmt, ...);
@@ -185,6 +189,7 @@ int do_pin_fd(int fd, const char *name);
 
 int do_prog(int argc, char **arg);
 int do_map(int argc, char **arg);
+int do_link(int argc, char **arg);
 int do_event_pipe(int argc, char **argv);
 int do_cgroup(int argc, char **arg);
 int do_perf(int argc, char **arg);
Signature · 1 line
2.24.1
Reviewed-by on v2 8/10
#21 lore raw

Re: [PATCH v2 bpf-next 08/10] bpftool: add bpf_link show and pin support

2020-04-27 22:49 UTC-0700 ~ Andrii Nakryiko <[email protected]>

Quoted 30 lines

Add bpftool link show and bpftool link pin commands.

Example plain output for link show (with showing pinned paths):

[vmuser@archvm bpf]$ sudo ~/local/linux/tools/bpf/bpftool/bpftool -f link 1: tracing prog 12
prog_type tracing attach_type fentry
pinned /sys/fs/bpf/my_test_link
pinned /sys/fs/bpf/my_test_link2
2: tracing prog 13
prog_type tracing attach_type fentry
3: tracing prog 14
prog_type tracing attach_type fentry
4: tracing prog 15
prog_type tracing attach_type fentry
5: tracing prog 16
prog_type tracing attach_type fentry
6: tracing prog 17
prog_type tracing attach_type fentry
7: raw_tracepoint prog 21
tp 'sys_enter'
8: cgroup prog 25
cgroup_id 584 attach_type egress
9: cgroup prog 25
cgroup_id 599 attach_type egress
10: cgroup prog 25
cgroup_id 614 attach_type egress
11: cgroup prog 25
cgroup_id 629 attach_type egress

Quoted 2 lines

---

#22 lore raw

PATCH v2 9/10 bpftool: add bpftool-link manpage

In reply to Andrii Nakryiko

Add bpftool-link manpage with information and examples of link-related commands.

---
 .../bpftool/Documentation/bpftool-link.rst    | 118 ++++++++++++++++++
 1 file changed, 118 insertions(+)
 create mode 100644 tools/bpf/bpftool/Documentation/bpftool-link.rst
tools/bpf/bpftool/Documentation/bpftool-link.rst new file +118 -0
new file mode 100644
index 000000000000..ee6500d6e6e4
@@ -0,0 +1,118 @@
+================
+bpftool-link
+================
+-------------------------------------------------------------------------------
+tool for inspection and simple manipulation of eBPF links
+-------------------------------------------------------------------------------
+
+:Manual section: 8
+
+SYNOPSIS
+========
+
+	**bpftool** [*OPTIONS*] **link** *COMMAND*
+
+	*OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] | { **-f** | **--bpffs** } }
+
+	*COMMANDS* := { **show** | **list** | **pin** | **help** }
+
+LINK COMMANDS
+=============
+
+|	**bpftool** **link { show | list }** [*LINK*]
+|	**bpftool** **link pin** *LINK* *FILE*
+|	**bpftool** **link help**
+|
+|	*LINK* := { **id** *LINK_ID* | **pinned** *FILE* }
+
+
+DESCRIPTION
+===========
+	**bpftool link { show | list }** [*LINK*]
+		  Show information about active links. If *LINK* is
+		  specified show information only about given link,
+		  otherwise list all links currently active on the system.
+
+		  Output will start with link ID followed by link type and
+		  zero or more named attributes, some of which depend on type
+		  of link.
+
+	**bpftool link pin** *LINK* *FILE*
+		  Pin link *LINK* as *FILE*.
+
+		  Note: *FILE* must be located in *bpffs* mount. It must not
+		  contain a dot character ('.'), which is reserved for future
+		  extensions of *bpffs*.
+
+	**bpftool link help**
+		  Print short help message.
+
+OPTIONS
+=======
+	-h, --help
+		  Print short generic help message (similar to **bpftool help**).
+
+	-V, --version
+		  Print version number (similar to **bpftool version**).
+
+	-j, --json
+		  Generate JSON output. For commands that cannot produce JSON, this
+		  option has no effect.
+
+	-p, --pretty
+		  Generate human-readable JSON output. Implies **-j**.
+
+	-f, --bpffs
+		  When showing BPF links, show file names of pinned
+		  links.
+
+	-n, --nomount
+		  Do not automatically attempt to mount any virtual file system
+		  (such as tracefs or BPF virtual file system) when necessary.
+
+	-d, --debug
+		  Print all logs available, even debug-level information. This
+		  includes logs from libbpf.
+
+EXAMPLES
+========
+**# bpftool link show**
+
+::
+
+    10: cgroup  prog 25
+            cgroup_id 614  attach_type egress
+
+**# bpftool --json --pretty link show**
+
+::
+
+    [{
+            "type": "cgroup",
+            "prog_id": 25,
+            "cgroup_id": 614,
+            "attach_type": "egress"
+        }
+    ]
+
+|
+| **# bpftool link pin id 10 /sys/fs/bpf/link**
+| **# ls -l /sys/fs/bpf/**
+
+::
+
+    -rw------- 1 root root 0 Apr 23 21:39 link
+
+
+SEE ALSO
+========
+	**bpf**\ (2),
+	**bpf-helpers**\ (7),
+	**bpftool**\ (8),
+	**bpftool-prog\ (8),
+	**bpftool-map**\ (8),
+	**bpftool-cgroup**\ (8),
+	**bpftool-feature**\ (8),
+	**bpftool-net**\ (8),
+	**bpftool-perf**\ (8),
+	**bpftool-btf**\ (8)
Signature · 1 line
2.24.1
#24 lore raw

PATCH v2 10/10 bpftool: add link bash completions

In reply to Andrii Nakryiko

Extend bpftool's bash-completion script to handle new link command and its sub-commands.

---
 tools/bpf/bpftool/bash-completion/bpftool | 39 +++++++++++++++++++++++
 1 file changed, 39 insertions(+)
tools/bpf/bpftool/bash-completion/bpftool +39 -0
index 45ee99b159e2..c033c3329f73 100644
@@ -98,6 +98,12 @@ _bpftool_get_btf_ids()
         command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
 }
 
+_bpftool_get_link_ids()
+{
+    COMPREPLY+=( $( compgen -W "$( bpftool -jp link 2>&1 | \
+        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
+}
+
 _bpftool_get_obj_map_names()
 {
     local obj
@@ -1082,6 +1088,39 @@ _bpftool()
                     ;;
             esac
             ;;
+        link)
+            case $command in
+                show|list|pin)
+                    case $prev in
+                        id)
+                            _bpftool_get_link_ids
+                            return 0
+                            ;;
+                    esac
+                    ;;
+            esac
+
+            local LINK_TYPE='id pinned'
+            case $command in
+                show|list)
+                    [[ $prev != "$command" ]] && return 0
+                    COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
+                    return 0
+                    ;;
+                pin)
+                    if [[ $prev == "$command" ]]; then
+                        COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
+                    else
+                        _filedir
+                    fi
+                    return 0
+                    ;;
+                *)
+                    [[ $prev == $object ]] && \
+                        COMPREPLY=( $( compgen -W 'help pin show list' -- "$cur" ) )
+                    ;;
+            esac
+            ;;
     esac
 } &&
 complete -F _bpftool bpftool
Signature · 1 line
2.24.1