Skip to contentSkip to conversation

bpftool: Add bpf_cookie to link output

PATCH Superseded Dmitrii Dolgov 3 participants 6 messages Mar 4, 2022 – Mar 9, 2022

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 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 v5:
- Remove unneeded cookie assigns

Changes in v4:
- Fetch cookies only for bpf_perf_link
- Signal about bpf_cookie via the flag, instead of deducing it from the object and link type
- Reset pid_iter_entry to avoid invalid indirect read from stack

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                  |  8 ++++++++
 tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 25 +++++++++++++++++++++++
 tools/bpf/bpftool/skeleton/pid_iter.h     |  2 ++
 4 files changed, 37 insertions(+)
tools/bpf/bpftool/main.h +2 -0
index 0c3840596b5a..1bb76aa1f3b2 100644
@@ -114,6 +114,8 @@ struct obj_ref {
 struct obj_refs {
 	int ref_cnt;
 	struct obj_ref *refs;
+	bool bpf_cookie_set;
+	__u64 bpf_cookie;
 };
 
 struct btf;
tools/bpf/bpftool/pids.c +8 -0
index 7c384d10e95f..6c6e7c90cc3d 100644
@@ -78,6 +78,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->bpf_cookie_set = e->bpf_cookie_set;
+	refs->bpf_cookie = e->bpf_cookie;
 
 	err = hashmap__append(map, u32_as_hash_field(e->id), refs);
 	if (err)
@@ -205,6 +207,9 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id,
 		if (refs->ref_cnt == 0)
 			break;
 
+		if (refs->bpf_cookie_set)
+			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 +239,9 @@ void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix)
 		if (refs->ref_cnt == 0)
 			break;
 
+		if (refs->bpf_cookie_set)
+			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 +25 -0
index f70702fcb224..91366ce33717 100644
@@ -38,6 +38,18 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
 	}
 }
 
+/* could be used only with BPF_LINK_TYPE_PERF_EVENT links */
+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)
 {
@@ -69,8 +81,21 @@ int iter(struct bpf_iter__task_file *ctx)
 	if (file->f_op != fops)
 		return 0;
 
+	__builtin_memset(&e, 0, sizeof(e));
 	e.pid = task->tgid;
 	e.id = get_obj_id(file->private_data, obj_type);
+	e.bpf_cookie = 0;
+	e.bpf_cookie_set = false;
+
+	if (obj_type == BPF_OBJ_LINK) {
+		struct bpf_link *link = (struct bpf_link *) file->private_data;
+
+		if (BPF_CORE_READ(link, type) == BPF_LINK_TYPE_PERF_EVENT) {
+			e.bpf_cookie_set = true;
+			e.bpf_cookie = get_bpf_cookie(link);
+		}
+	}
+
 	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 +2 -0
index 5692cf257adb..2676cece58d7 100644
@@ -6,6 +6,8 @@
 struct pid_iter_entry {
 	__u32 id;
 	int pid;
+	__u64 bpf_cookie;
+	bool bpf_cookie_set;
 	char comm[16];
 };
 
Signature · 1 line
2.32.0
Acked-by on v5 1/1
#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.

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

LGTM with a few nits below.

Quoted 52 lines

---
Changes in v5:
- Remove unneeded cookie assigns

Changes in v4:
- Fetch cookies only for bpf_perf_link
- Signal about bpf_cookie via the flag, instead of deducing it from the object and link type
- Reset pid_iter_entry to avoid invalid indirect read from stack

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                  |  8 ++++++++
  tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 25 +++++++++++++++++++++++
  tools/bpf/bpftool/skeleton/pid_iter.h     |  2 ++
  4 files changed, 37 insertions(+)
tools/bpf/bpftool/main.h +2 -0
index 0c3840596b5a..1bb76aa1f3b2 100644
@@ -114,6 +114,8 @@ struct obj_ref {
  struct obj_refs {
  	int ref_cnt;
  	struct obj_ref *refs;
+	bool bpf_cookie_set;
+	__u64 bpf_cookie;
  };
 
  struct btf;
tools/bpf/bpftool/pids.c +2 -0
index 7c384d10e95f..6c6e7c90cc3d 100644
@@ -78,6 +78,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->bpf_cookie_set = e->bpf_cookie_set;
+	refs->bpf_cookie = e->bpf_cookie;
 
  	err = hashmap__append(map, u32_as_hash_field(e->id), refs);
  	if (err)

tools/bpf/bpftool/pids.c void emit_obj_refs_json(struct hashmap *map, __u32 id, · line 211

Quoted 5 lines
tools/bpf/bpftool/pids.c +2 -0
@@ -205,6 +207,9 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id,
  		if (refs->ref_cnt == 0)
  			break;
 
+		if (refs->bpf_cookie_set)
+			jsonw_lluint_field(json_writer, "bpf_cookie", refs->bpf_cookie);

The original motivation for 'bpf_cookie' is for kprobe to get function addresses. In that case, printing with llx (0x...) is better than llu since people can easily search it with /proc/kallsyms to get what the
function it attached to. But on the other hand, other use cases might
be simply just wanting an int.

I don't have a strong opinion here. Just to speak out loud so other people can comment on this too.

Quoted 38 lines
tools/bpf/bpftool/pids.c +4 -0
+
  		jsonw_name(json_writer, "pids");
  		jsonw_start_array(json_writer);
  		for (i = 0; i < refs->ref_cnt; i++) {
@@ -234,6 +239,9 @@ void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix)
  		if (refs->ref_cnt == 0)
  			break;
 
+		if (refs->bpf_cookie_set)
+			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 +12 -0
index f70702fcb224..91366ce33717 100644
@@ -38,6 +38,18 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
  	}
  }
 
+/* could be used only with BPF_LINK_TYPE_PERF_EVENT links */
+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)
  {

tools/bpf/bpftool/skeleton/pid_iter.bpf.c int iter(struct bpf_iter__task_file *ctx) · line 88

Quoted 8 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +3 -0
@@ -69,8 +81,21 @@ int iter(struct bpf_iter__task_file *ctx)
  	if (file->f_op != fops)
  		return 0;
 
+	__builtin_memset(&e, 0, sizeof(e));
  	e.pid = task->tgid;
  	e.id = get_obj_id(file->private_data, obj_type);
+	e.bpf_cookie = 0;
+	e.bpf_cookie_set = false;

We already have __builtin_memset(&e, 0, sizeof(e)) in the above, so the above e.bpf_cookie and e.bpf_cookie_set assignment is not necessary.

Quoted 26 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +10 -0
+
+	if (obj_type == BPF_OBJ_LINK) {
+		struct bpf_link *link = (struct bpf_link *) file->private_data;
+
+		if (BPF_CORE_READ(link, type) == BPF_LINK_TYPE_PERF_EVENT) {
+			e.bpf_cookie_set = true;
+			e.bpf_cookie = get_bpf_cookie(link);
+		}
+	}
+
  	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 +2 -0
index 5692cf257adb..2676cece58d7 100644
@@ -6,6 +6,8 @@
  struct pid_iter_entry {
  	__u32 id;
  	int pid;
+	__u64 bpf_cookie;
+	bool bpf_cookie_set;
  	char comm[16];
  };
 
#2 lore raw
Quoted 25 lines · Yonghong Song
Quoted 14 lines
tools/bpf/bpftool/pids.c +2 -0
index 7c384d10e95f..6c6e7c90cc3d 100644
@@ -78,6 +78,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->bpf_cookie_set = e->bpf_cookie_set;
+	refs->bpf_cookie = e->bpf_cookie;
  	err = hashmap__append(map, u32_as_hash_field(e->id), refs);
  	if (err)

tools/bpf/bpftool/pids.c void emit_obj_refs_json(struct hashmap *map, __u32 id, · line 210

Quoted 4 lines
tools/bpf/bpftool/pids.c +2 -0
@@ -205,6 +207,9 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id,
  		if (refs->ref_cnt == 0)
  			break;
+		if (refs->bpf_cookie_set)
+			jsonw_lluint_field(json_writer, "bpf_cookie", refs->bpf_cookie);

The original motivation for 'bpf_cookie' is for kprobe to get function addresses. In that case, printing with llx (0x...) is better than llu since people can easily search it with /proc/kallsyms to get what the function it attached to. But on the other hand, other use cases might be simply just wanting an int.

Quoted 2 lines

I don't have a strong opinion here. Just to speak out loud so other people can comment on this too.

Interesting, I didn't know that. The current implementation of 'bpf_cookie' seems to be quite opaque, with no assumptions about what does it contain, probably it makes sense to keep it like that. But I don't have a strong opinion here either, would love to hear what others think.

Quoted 26 lines
Quoted 26 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +13 -0
index f70702fcb224..91366ce33717 100644
@@ -38,6 +38,18 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
  	}
  }
+/* could be used only with BPF_LINK_TYPE_PERF_EVENT links */
+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)
  {
@@ -69,8 +81,21 @@ int iter(struct bpf_iter__task_file *ctx)
  	if (file->f_op != fops)
  		return 0;
+	__builtin_memset(&e, 0, sizeof(e));
Quoted 8 lines

tools/bpf/bpftool/skeleton/pid_iter.bpf.c int iter(struct bpf_iter__task_file *ctx) · line 88

Quoted 4 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +2 -0
  	e.pid = task->tgid;
  	e.id = get_obj_id(file->private_data, obj_type);
+	e.bpf_cookie = 0;
+	e.bpf_cookie_set = false;

We already have __builtin_memset(&e, 0, sizeof(e)) in the above, so the above e.bpf_cookie and e.bpf_cookie_set assignment is not necessary.

Good point, will remote this.

#3 lore raw
Quoted 27 lines · Dmitry Dolgov
Quoted 25 lines · Yonghong Song
Quoted 14 lines
tools/bpf/bpftool/pids.c +2 -0
index 7c384d10e95f..6c6e7c90cc3d 100644
@@ -78,6 +78,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->bpf_cookie_set = e->bpf_cookie_set;
+   refs->bpf_cookie = e->bpf_cookie;
    err = hashmap__append(map, u32_as_hash_field(e->id), refs);
    if (err)

tools/bpf/bpftool/pids.c void emit_obj_refs_json(struct hashmap *map, __u32 id, · line 210

Quoted 4 lines
tools/bpf/bpftool/pids.c +2 -0
@@ -205,6 +207,9 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id,
            if (refs->ref_cnt == 0)
                    break;
+           if (refs->bpf_cookie_set)
+                   jsonw_lluint_field(json_writer, "bpf_cookie", refs->bpf_cookie);

The original motivation for 'bpf_cookie' is for kprobe to get function addresses. In that case, printing with llx (0x...) is better than llu since people can easily search it with /proc/kallsyms to get what the function it attached to. But on the other hand, other use cases might be simply just wanting an int.

Quoted 8 lines
Quoted 2 lines

I don't have a strong opinion here. Just to speak out loud so other people can comment on this too.

Interesting, I didn't know that. The current implementation of 'bpf_cookie' seems to be quite opaque, with no assumptions about what does it contain, probably it makes sense to keep it like that. But I don't have a strong opinion here either, would love to hear what others think.

There is no assumption that it's going to be an address. I actually expect that usually it will be a small index into an additional array of configuration values. So keeping it decimal makes more sense to me.

Quoted 37 lines
Quoted 27 lines
Quoted 27 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +13 -0
index f70702fcb224..91366ce33717 100644
@@ -38,6 +38,18 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
    }
  }
+/* could be used only with BPF_LINK_TYPE_PERF_EVENT links */
+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)
  {
@@ -69,8 +81,21 @@ int iter(struct bpf_iter__task_file *ctx)
    if (file->f_op != fops)
            return 0;
+   __builtin_memset(&e, 0, sizeof(e));
Quoted 8 lines

tools/bpf/bpftool/skeleton/pid_iter.bpf.c int iter(struct bpf_iter__task_file *ctx) · line 88

Quoted 4 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +2 -0
    e.pid = task->tgid;
    e.id = get_obj_id(file->private_data, obj_type);
+   e.bpf_cookie = 0;
+   e.bpf_cookie_set = false;

We already have __builtin_memset(&e, 0, sizeof(e)) in the above, so the above e.bpf_cookie and e.bpf_cookie_set assignment is not necessary.

Good point, will remote this.

#4 lore raw
In reply to Dmitrii Dolgov
Quoted 79 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 v5:
- Remove unneeded cookie assigns

Changes in v4:
- Fetch cookies only for bpf_perf_link
- Signal about bpf_cookie via the flag, instead of deducing it from the object and link type
- Reset pid_iter_entry to avoid invalid indirect read from stack

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                  |  8 ++++++++
 tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 25 +++++++++++++++++++++++
 tools/bpf/bpftool/skeleton/pid_iter.h     |  2 ++
 4 files changed, 37 insertions(+)
tools/bpf/bpftool/main.h +2 -0
index 0c3840596b5a..1bb76aa1f3b2 100644
@@ -114,6 +114,8 @@ struct obj_ref {
 struct obj_refs {
        int ref_cnt;
        struct obj_ref *refs;
+       bool bpf_cookie_set;
+       __u64 bpf_cookie;
 };
 
 struct btf;
tools/bpf/bpftool/pids.c +5 -0
index 7c384d10e95f..6c6e7c90cc3d 100644
@@ -78,6 +78,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->bpf_cookie_set = e->bpf_cookie_set;
+       refs->bpf_cookie = e->bpf_cookie;
 
        err = hashmap__append(map, u32_as_hash_field(e->id), refs);
        if (err)
@@ -205,6 +207,9 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id,
                if (refs->ref_cnt == 0)
                        break;
 
+               if (refs->bpf_cookie_set)
+                       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++) {

tools/bpf/bpftool/pids.c void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix) · line 243

Quoted 5 lines
tools/bpf/bpftool/pids.c +2 -0
@@ -234,6 +239,9 @@ void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix)
                if (refs->ref_cnt == 0)
                        break;
 
+               if (refs->bpf_cookie_set)
+                       printf("\n\tbpf_cookie %llu", refs->bpf_cookie);

__u64 is not always %llu on all architectures. Best to cast it to (unsigned long long) here to avoid compilation warnings.

Quoted 9 lines
tools/bpf/bpftool/pids.c +1 -0
+
                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..91366ce33717 100644

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

Quoted 5 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +2 -0
@@ -38,6 +38,18 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
        }
 }
 
+/* could be used only with BPF_LINK_TYPE_PERF_EVENT links */
+static __always_inline __u64 get_bpf_cookie(struct bpf_link *link)

no need for __always_inline

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

Quoted 10 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +10 -0
+{
+       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);
+}
+
+

nit: why double empty line?

Quoted 30 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +13 -0
 SEC("iter/task_file")
 int iter(struct bpf_iter__task_file *ctx)
 {
@@ -69,8 +81,21 @@ int iter(struct bpf_iter__task_file *ctx)
        if (file->f_op != fops)
                return 0;
 
+       __builtin_memset(&e, 0, sizeof(e));
        e.pid = task->tgid;
        e.id = get_obj_id(file->private_data, obj_type);
+       e.bpf_cookie = 0;
+       e.bpf_cookie_set = false;
+
+       if (obj_type == BPF_OBJ_LINK) {
+               struct bpf_link *link = (struct bpf_link *) file->private_data;
+
+               if (BPF_CORE_READ(link, type) == BPF_LINK_TYPE_PERF_EVENT) {
+                       e.bpf_cookie_set = true;
+                       e.bpf_cookie = get_bpf_cookie(link);
+               }
+       }
+
        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 +0 -0
index 5692cf257adb..2676cece58d7 100644

tools/bpf/bpftool/skeleton/pid_iter.h line 10

Quoted 5 lines
tools/bpf/bpftool/skeleton/pid_iter.h +2 -0
@@ -6,6 +6,8 @@
 struct pid_iter_entry {
        __u32 id;
        int pid;
+       __u64 bpf_cookie;
+       bool bpf_cookie_set;

naming nit: either "bpf_cookie_is_set" or "has_bpf_cookie"? "bpf_cookie_set" is read either as a verb or as "a set of bpf_cookies"

Quoted 6 lines
tools/bpf/bpftool/skeleton/pid_iter.h +0 -1
        char comm[16];
 };
 
--

2.32.0

#5 lore raw
Quoted 35 lines · Andrii Nakryiko

tools/bpf/bpftool/pids.c void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix) · line 243

Quoted 7 lines · Dmitrii Dolgov
tools/bpf/bpftool/pids.c +2 -0
@@ -234,6 +239,9 @@ void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix)
                if (refs->ref_cnt == 0)
                        break;
 
+               if (refs->bpf_cookie_set)
+                       printf("\n\tbpf_cookie %llu", refs->bpf_cookie);

__u64 is not always %llu on all architectures. Best to cast it to (unsigned long long) here to avoid compilation warnings.

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

Quoted 6 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +2 -0
@@ -38,6 +38,18 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
        }
 }
 
+/* could be used only with BPF_LINK_TYPE_PERF_EVENT links */
+static __always_inline __u64 get_bpf_cookie(struct bpf_link *link)

no need for __always_inline

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

Quoted 10 lines
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +10 -0
+{
+       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);
+}
+
+

nit: why double empty line?

Quoted 13 lines

tools/bpf/bpftool/skeleton/pid_iter.h line 10

Quoted 10 lines
tools/bpf/bpftool/skeleton/pid_iter.h +2 -0
index 5692cf257adb..2676cece58d7 100644
@@ -6,6 +6,8 @@
 struct pid_iter_entry {
        __u32 id;
        int pid;
+       __u64 bpf_cookie;
+       bool bpf_cookie_set;

naming nit: either "bpf_cookie_is_set" or "has_bpf_cookie"? "bpf_cookie_set" is read either as a verb or as "a set of bpf_cookies"

Yep, makes sense (this and the rest of the comments above), will post a new version with the cumulative changes soon.