bpftool: Add bpf_cookie to link output
RFC PATCH v4 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 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 | 10 +++++++++ tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 25 +++++++++++++++++++++++ tools/bpf/bpftool/skeleton/pid_iter.h | 2 ++ 4 files changed, 39 insertions(+)
tools/bpf/bpftool/main.h +2 -0
@@ -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 +10 -0
@@ -55,6 +55,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->bpf_cookie_set = e->bpf_cookie_set; + refs->bpf_cookie = e->bpf_cookie;return ; }
@@ -78,6 +80,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 +209,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 +241,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
@@ -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
@@ -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
Quoted 6 lines · Dmitrii Dolgov
Commit
82e6b1eee6a8("bpf: Allow to specify user-providedbpf_cookiefor BPF perf links") introduced the concept of user specifiedbpf_cookie, which could be accessed by BPF programs usingbpf_get_attach_cookie(). For troubleshooting purposes it is convenient to exposebpf_cookievia bpftool as well, so there is no need to meddle with the target BPF program itself.
Do you still need RFC tag? It looks like we have a consensus with this bpf_iter approach, right?
Please also add "bpf-next" to the tag for clarity purpose.
Quoted 49 lines
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)Signed-off-by Dmitrii Dolgov <[email protected]>---
Changes in v4:
- Fetch cookies only forbpf_perf_link
- Signal aboutbpf_cookievia the flag, instead of deducing it from the object and link type
- Resetpid_iter_entryto avoid invalid indirect read from stackChanges in v3:
- Use pid iterator to fetchbpf_cookieChanges in v2:
- Displaybpf_cookiein bpftool link command instead perfPrevious discussion: https://lore.kernel.org/bpf/[email protected]/
tools/bpf/bpftool/main.h | 2 ++ tools/bpf/bpftool/pids.c | 10 +++++++++ tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 25 +++++++++++++++++++++++ tools/bpf/bpftool/skeleton/pid_iter.h | 2 ++ 4 files changed, 39 insertions(+)tools/bpf/bpftool/main.h +2
-0@@ -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 +0
-0
tools/bpf/bpftool/pids.c static void add_ref(struct hashmap *map, struct pid_iter_entry *e) · line 59
Quoted 5 lines
tools/bpf/bpftool/pids.c +2
-0@@ -55,6 +55,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->bpf_cookie_set = e->bpf_cookie_set; + refs->bpf_cookie = e->bpf_cookie;
Do we need here? It is weird that we overwrite the bpf_cookie with every new 'pid' reference.
When you create a link, the cookie is fixed for that link. You could pin that link in bpffs e.g., /sys/fs/bpf/link1 and other programs can then
get a reference to the link1, but they should still have the same cookie. Is that right?
Quoted 14 lines
tools/bpf/bpftool/pids.c +2
-0return ; }@@ -78,6 +80,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 +209,9 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id,if (refs->ref_cnt ==0 )
[...]
Quoted 11 lines · Yonghong Song
Quoted 6 lines · Dmitrii Dolgov
Commit
82e6b1eee6a8("bpf: Allow to specify user-providedbpf_cookiefor BPF perf links") introduced the concept of user specifiedbpf_cookie, which could be accessed by BPF programs usingbpf_get_attach_cookie(). For troubleshooting purposes it is convenient to exposebpf_cookievia bpftool as well, so there is no need to meddle with the target BPF program itself.
Quoted 4 lines
Do you still need RFC tag? It looks like we have a consensus with this
bpf_iterapproach, right?Please also add "bpf-next" to the tag for clarity purpose.
Yeah, you're right, it seems there is no need for RFC tag any more. Will add "bpf-next" tag as well, thanks for the suggestion.
Quoted 14 lines
tools/bpf/bpftool/pids.c static void add_ref(struct hashmap *map, struct pid_iter_entry *e) · line 59
Quoted 10 lines
tools/bpf/bpftool/pids.c +2
-0@@ -55,6 +55,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->bpf_cookie_set = e->bpf_cookie_set; + refs->bpf_cookie = e->bpf_cookie;Do we need here? It is weird that we overwrite the
bpf_cookiewith every new 'pid' reference.
Quoted 4 lines
When you create a link, the cookie is fixed for that link. You could pin that link in bpffs e.g.,
/sys/fs/bpf/link1and other programs can then get a reference to the link1, but they should still have the same cookie. Is that right?
Right, I have the same understanding about a single fixed cookie per link. But in this particular case the implementation uses hashmap__for_each_key_entry (which is essentially a loop with a condition inside) and inside it returns as soon as the first entry was found. So I guess it will not override the cookie with every new reference, do I see it correct?
Quoted 38 lines · Dmitry Dolgov
Quoted 11 lines · Yonghong Song
Quoted 6 lines · Dmitrii Dolgov
Commit
82e6b1eee6a8("bpf: Allow to specify user-providedbpf_cookiefor BPF perf links") introduced the concept of user specifiedbpf_cookie, which could be accessed by BPF programs usingbpf_get_attach_cookie(). For troubleshooting purposes it is convenient to exposebpf_cookievia bpftool as well, so there is no need to meddle with the target BPF program itself.Quoted 4 lines
Do you still need RFC tag? It looks like we have a consensus with this
bpf_iterapproach, right?Please also add "bpf-next" to the tag for clarity purpose.
Yeah, you're right, it seems there is no need for RFC tag any more. Will add "bpf-next" tag as well, thanks for the suggestion.
Quoted 14 lines
tools/bpf/bpftool/pids.c static void add_ref(struct hashmap *map, struct pid_iter_entry *e) · line 59
Quoted 10 lines
tools/bpf/bpftool/pids.c +2
-0@@ -55,6 +55,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->bpf_cookie_set = e->bpf_cookie_set; + refs->bpf_cookie = e->bpf_cookie;Do we need here? It is weird that we overwrite the
bpf_cookiewith every new 'pid' reference.Quoted 4 lines
When you create a link, the cookie is fixed for that link. You could pin that link in bpffs e.g.,
/sys/fs/bpf/link1and other programs can then get a reference to the link1, but they should still have the same cookie. Is that right?
Quoted 6 lines
Right, I have the same understanding about a single fixed cookie per link. But in this particular case the implementation uses
hashmap__for_each_key_entry(which is essentially a loop with a condition inside) and inside it returns as soon as the first entry was found. So I guess it will not override the cookie with every new reference, do I see it correct?
They are not return if pid is not the same.
Let us say the same link is used for pid1 and pid2.
The pid1 case will have refs->bpf_cookie[_set] set properly. The pid2 case will trigger the above code, and since for the same link, cookie is fixed, so the above code is not really needed.
Quoted 28 lines · Yonghong Song
Quoted 20 lines
Quoted 15 lines
tools/bpf/bpftool/pids.c static void add_ref(struct hashmap *map, struct pid_iter_entry *e) · line 59
Quoted 11 lines
tools/bpf/bpftool/pids.c +2
-0@@ -55,6 +55,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->bpf_cookie_set = e->bpf_cookie_set; + refs->bpf_cookie = e->bpf_cookie;Do we need here? It is weird that we overwrite the
bpf_cookiewith every new 'pid' reference.Quoted 4 lines
When you create a link, the cookie is fixed for that link. You could pin that link in bpffs e.g.,
/sys/fs/bpf/link1and other programs can then get a reference to the link1, but they should still have the same cookie. Is that right?Quoted 6 lines
Right, I have the same understanding about a single fixed cookie per link. But in this particular case the implementation uses
hashmap__for_each_key_entry(which is essentially a loop with a condition inside) and inside it returns as soon as the first entry was found. So I guess it will not override the cookie with every new reference, do I see it correct?
Quoted 6 lines
They are not return if pid is not the same.
Let us say the same link is used for pid1 and pid2.
The pid1 case will have refs->bpf_cookie[_set] set properly. The pid2 case will trigger the above code, and since for the same link, cookie is fixed, so the above code is not really needed.
Oh, I see, thanks for clarification! Will post a new version soon.