Skip to contentSkip to conversation

bpftool: Add bpf_cookie to link output

RFC PATCH Superseded Dmitrii Dolgov 2 participants 4 messages Feb 18, 2022 – Feb 24, 2022

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v3 1/1 bpftool: Add bpf_cookie to link output

Commit 82e6b1eee6a8 ("bpf: Allow to specify user-provided bpf_cookie for BPF perf links") introduced the concept of user specified bpf_cookie, which could be accessed by BPF programs using bpf_get_attach_cookie(). For troubleshooting purposes it is convenient to expose bpf_cookie via bpftool as well, so there is no need to meddle with the target BPF program itself.

Implemented using the pid iterator BPF program to actually fetch bpf_cookies, which allows constraining code changes only to bpftool.

$ bpftool link
1: type 7  prog 5
        bpf_cookie 123
        pids bootstrap(81)

---
Changes in v3:
- Use pid iterator to fetch bpf_cookie

Changes in v2:
- Display bpf_cookie in bpftool link command instead perf

Previous discussion: https://lore.kernel.org/bpf/[email protected]/

 tools/bpf/bpftool/main.h                  |  2 ++
 tools/bpf/bpftool/pids.c                  | 15 +++++++++++++--
 tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 16 ++++++++++++++++
 tools/bpf/bpftool/skeleton/pid_iter.h     |  1 +
 4 files changed, 32 insertions(+), 2 deletions(-)
tools/bpf/bpftool/main.h +2 -0
index 0c3840596b5a..c0042bd56139 100644
@@ -114,6 +114,8 @@ struct obj_ref {
 struct obj_refs {
 	int ref_cnt;
 	struct obj_ref *refs;
+	enum bpf_obj_type type;
+	__u64 bpf_cookie;
 };
 
 struct btf;
tools/bpf/bpftool/pids.c +13 -2
index 7c384d10e95f..d4db4049d94b 100644
@@ -28,7 +28,8 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id, json_writer_t *json_write
 
 #include "pid_iter.skel.h"
 
-static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
+static void add_ref(struct hashmap *map, struct pid_iter_entry *e,
+				enum bpf_obj_type type)
 {
 	struct hashmap_entry *entry;
 	struct obj_refs *refs;
@@ -55,6 +56,8 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
 		ref->pid = e->pid;
 		memcpy(ref->comm, e->comm, sizeof(ref->comm));
 		refs->ref_cnt++;
+		refs->type = type;
+		refs->bpf_cookie = e->bpf_cookie;
 
 		return;
 	}
@@ -78,6 +81,8 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
 	ref->pid = e->pid;
 	memcpy(ref->comm, e->comm, sizeof(ref->comm));
 	refs->ref_cnt = 1;
+	refs->type = type;
+	refs->bpf_cookie = e->bpf_cookie;
 
 	err = hashmap__append(map, u32_as_hash_field(e->id), refs);
 	if (err)
@@ -161,7 +166,7 @@ int build_obj_refs_table(struct hashmap **map, enum bpf_obj_type type)
 
 		e = (void *)buf;
 		for (i = 0; i < ret; i++, e++) {
-			add_ref(*map, e);
+			add_ref(*map, e, type);
 		}
 	}
 	err = 0;
@@ -205,6 +210,9 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id,
 		if (refs->ref_cnt == 0)
 			break;
 
+		if (refs->type == BPF_OBJ_LINK)
+			jsonw_lluint_field(json_writer, "bpf_cookie", refs->bpf_cookie);
+
 		jsonw_name(json_writer, "pids");
 		jsonw_start_array(json_writer);
 		for (i = 0; i < refs->ref_cnt; i++) {
@@ -234,6 +242,9 @@ void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix)
 		if (refs->ref_cnt == 0)
 			break;
 
+		if (refs->type == BPF_OBJ_LINK)
+			printf("\n\tbpf_cookie %llu", refs->bpf_cookie);
+
 		printf("%s", prefix);
 		for (i = 0; i < refs->ref_cnt; i++) {
 			struct obj_ref *ref = &refs->refs[i];
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +16 -0
index f70702fcb224..afdfdfbf305d 100644
@@ -38,6 +38,17 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
 	}
 }
 
+static __always_inline __u64 get_bpf_cookie(struct bpf_link *link)
+{
+	struct bpf_perf_link *perf_link;
+	struct perf_event *event;
+
+	perf_link = container_of(link, struct bpf_perf_link, link);
+	event = BPF_CORE_READ(perf_link, perf_file, private_data);
+	return BPF_CORE_READ(event, bpf_cookie);
+}
+
+
 SEC("iter/task_file")
 int iter(struct bpf_iter__task_file *ctx)
 {
@@ -71,6 +82,11 @@ int iter(struct bpf_iter__task_file *ctx)
 
 	e.pid = task->tgid;
 	e.id = get_obj_id(file->private_data, obj_type);
+	e.bpf_cookie = 0;
+
+	if (obj_type == BPF_OBJ_LINK)
+		e.bpf_cookie = get_bpf_cookie((struct bpf_link *) file->private_data);
+
 	bpf_probe_read_kernel_str(&e.comm, sizeof(e.comm),
 				  task->group_leader->comm);
 	bpf_seq_write(ctx->meta->seq, &e, sizeof(e));
tools/bpf/bpftool/skeleton/pid_iter.h +1 -0
index 5692cf257adb..a631640f6fe4 100644
@@ -7,6 +7,7 @@ struct pid_iter_entry {
 	__u32 id;
 	int pid;
 	char comm[16];
+	__u64 bpf_cookie;
 };
 
 #endif
Signature · 1 line
2.32.0
#1 lore raw
Quoted 10 lines · Dmitrii Dolgov

Commit 82e6b1eee6a8 ("bpf: Allow to specify user-provided bpf_cookie for BPF perf links") introduced the concept of user specified bpf_cookie, which could be accessed by BPF programs using bpf_get_attach_cookie(). For troubleshooting purposes it is convenient to expose bpf_cookie via bpftool as well, so there is no need to meddle with the target BPF program itself.

Implemented using the pid iterator BPF program to actually fetch bpf_cookies, which allows constraining code changes only to bpftool.

I have to admit, with pid iterator it looks a bit clumsy. Would love to know if there is a better way.

#2 lore raw
In reply to Dmitrii Dolgov
Quoted 112 lines · Dmitrii Dolgov

Commit 82e6b1eee6a8 ("bpf: Allow to specify user-provided bpf_cookie for BPF perf links") introduced the concept of user specified bpf_cookie, which could be accessed by BPF programs using bpf_get_attach_cookie(). For troubleshooting purposes it is convenient to expose bpf_cookie via bpftool as well, so there is no need to meddle with the target BPF program itself.

Implemented using the pid iterator BPF program to actually fetch bpf_cookies, which allows constraining code changes only to bpftool.

$ bpftool link
1: type 7  prog 5
        bpf_cookie 123
        pids bootstrap(81)

---
Changes in v3:
- Use pid iterator to fetch bpf_cookie

Changes in v2:
- Display bpf_cookie in bpftool link command instead perf

Previous discussion: https://lore.kernel.org/bpf/[email protected]/

 tools/bpf/bpftool/main.h                  |  2 ++
 tools/bpf/bpftool/pids.c                  | 15 +++++++++++++--
 tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 16 ++++++++++++++++
 tools/bpf/bpftool/skeleton/pid_iter.h     |  1 +
 4 files changed, 32 insertions(+), 2 deletions(-)
tools/bpf/bpftool/main.h +2 -0
index 0c3840596b5a..c0042bd56139 100644
@@ -114,6 +114,8 @@ struct obj_ref {
 struct obj_refs {
        int ref_cnt;
        struct obj_ref *refs;
+       enum bpf_obj_type type;
+       __u64 bpf_cookie;
 };
 
 struct btf;
tools/bpf/bpftool/pids.c +13 -2
index 7c384d10e95f..d4db4049d94b 100644
@@ -28,7 +28,8 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id, json_writer_t *json_write
 
 #include "pid_iter.skel.h"
 
-static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
+static void add_ref(struct hashmap *map, struct pid_iter_entry *e,
+                               enum bpf_obj_type type)
 {
        struct hashmap_entry *entry;
        struct obj_refs *refs;
@@ -55,6 +56,8 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
                ref->pid = e->pid;
                memcpy(ref->comm, e->comm, sizeof(ref->comm));
                refs->ref_cnt++;
+               refs->type = type;
+               refs->bpf_cookie = e->bpf_cookie;
 
                return;
        }
@@ -78,6 +81,8 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
        ref->pid = e->pid;
        memcpy(ref->comm, e->comm, sizeof(ref->comm));
        refs->ref_cnt = 1;
+       refs->type = type;
+       refs->bpf_cookie = e->bpf_cookie;
 
        err = hashmap__append(map, u32_as_hash_field(e->id), refs);
        if (err)
@@ -161,7 +166,7 @@ int build_obj_refs_table(struct hashmap **map, enum bpf_obj_type type)
 
                e = (void *)buf;
                for (i = 0; i < ret; i++, e++) {
-                       add_ref(*map, e);
+                       add_ref(*map, e, type);
                }
        }
        err = 0;
@@ -205,6 +210,9 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id,
                if (refs->ref_cnt == 0)
                        break;
 
+               if (refs->type == BPF_OBJ_LINK)
+                       jsonw_lluint_field(json_writer, "bpf_cookie", refs->bpf_cookie);
+
                jsonw_name(json_writer, "pids");
                jsonw_start_array(json_writer);
                for (i = 0; i < refs->ref_cnt; i++) {
@@ -234,6 +242,9 @@ void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix)
                if (refs->ref_cnt == 0)
                        break;
 
+               if (refs->type == BPF_OBJ_LINK)
+                       printf("\n\tbpf_cookie %llu", refs->bpf_cookie);
+
                printf("%s", prefix);
                for (i = 0; i < refs->ref_cnt; i++) {
                        struct obj_ref *ref = &refs->refs[i];
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +0 -0
index f70702fcb224..afdfdfbf305d 100644
@@ -38,6 +38,17 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
        }

tools/bpf/bpftool/skeleton/pid_iter.bpf.c static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type) · line 48

Quoted 10 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +8 -0
 }
 
+static __always_inline __u64 get_bpf_cookie(struct bpf_link *link)
+{
+       struct bpf_perf_link *perf_link;
+       struct perf_event *event;
+
+       perf_link = container_of(link, struct bpf_perf_link, link);
+       event = BPF_CORE_READ(perf_link, perf_file, private_data);
+       return BPF_CORE_READ(event, bpf_cookie);

not every bpf_link is bpf_perf_link, you can't do it for every instance of bpf_link.

Quoted 33 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +8 -0
+}
+
+
 SEC("iter/task_file")
 int iter(struct bpf_iter__task_file *ctx)
 {
@@ -71,6 +82,11 @@ int iter(struct bpf_iter__task_file *ctx)
 
        e.pid = task->tgid;
        e.id = get_obj_id(file->private_data, obj_type);
+       e.bpf_cookie = 0;
+
+       if (obj_type == BPF_OBJ_LINK)
+               e.bpf_cookie = get_bpf_cookie((struct bpf_link *) file->private_data);
+
        bpf_probe_read_kernel_str(&e.comm, sizeof(e.comm),
                                  task->group_leader->comm);
        bpf_seq_write(ctx->meta->seq, &e, sizeof(e));
tools/bpf/bpftool/skeleton/pid_iter.h +1 -1
index 5692cf257adb..a631640f6fe4 100644
@@ -7,6 +7,7 @@ struct pid_iter_entry {
        __u32 id;
        int pid;
        char comm[16];
+       __u64 bpf_cookie;
 };
 
 #endif
--

2.32.0

#3 lore raw
Quoted 14 lines · Andrii Nakryiko

tools/bpf/bpftool/skeleton/pid_iter.bpf.c static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type) · line 48

Quoted 9 lines · Dmitrii Dolgov
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +8 -0
+static __always_inline __u64 get_bpf_cookie(struct bpf_link *link)
+{
+       struct bpf_perf_link *perf_link;
+       struct perf_event *event;
+
+       perf_link = container_of(link, struct bpf_perf_link, link);
+       event = BPF_CORE_READ(perf_link, perf_file, private_data);
+       return BPF_CORE_READ(event, bpf_cookie);

not every bpf_link is bpf_perf_link, you can't do it for every instance of bpf_link.

Right, thanks. I was somehow testing it only with perf links, will fix.