bpftool: Add bpf_cookie to link output
PATCH v6 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 v6:
- Remove unnecessary initialization of fields in pid_iter_entry
- Changing bpf_cookie_set to has_bpf_cookie
- Small code cleanup (casting bpf_cookie when needed, removing __always_inline, etc.)
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 | 22 ++++++++++++++++++++++ tools/bpf/bpftool/skeleton/pid_iter.h | 2 ++ 4 files changed, 34 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 has_bpf_cookie; +__u64 bpf_cookie; };struct btf ;
tools/bpf/bpftool/pids.c +8 -0
@@ -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->has_bpf_cookie = e->has_bpf_cookie; + 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->has_bpf_cookie) +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->has_bpf_cookie) +printf ("\n\tbpf_cookie %llu" , (unsigned long long ) 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 +22 -0
@@ -38,6 +38,17 @@ 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 __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 +80,19 @@ 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); + +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.has_bpf_cookie = 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 has_bpf_cookie;char comm[16 ]; };
Signature · 1 line
2.32.0
Quoted 11 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.Implemented using the pid iterator BPF program to actually fetch
bpf_cookies, which allows constraining code changes only to bpftool.
Quoted 8 lines
$ bpftool link 1: type 7 prog 5 bpf_cookie 123 pids bootstrap(81)Signed-off-by Dmitrii Dolgov <[email protected]>Acked-by Yonghong Song <[email protected]>---
Quentin, any opinion on this feature? The implementation seems straightforward enough. We'll need to not forget to expand the support to other types that support bpf_cookies (and multi-attach kprobes and fentries will be problematic, potentially), but this might be useful for debugging purposes.
Quoted 29 lines
Changes in v6:
- Remove unnecessary initialization of fields inpid_iter_entry
- Changingbpf_cookie_settohas_bpf_cookie
- Small code cleanup (castingbpf_cookiewhen needed, removing__always_inline, etc.)Changes in v5:
- Remove unneeded cookie assignsChanges 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 | 8 ++++++++ tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 22 ++++++++++++++++++++++ tools/bpf/bpftool/skeleton/pid_iter.h | 2 ++ 4 files changed, 34 insertions(+)
[...]
Quoted 22 lines · Andrii Nakryiko
Quoted 11 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.Implemented using the pid iterator BPF program to actually fetch
bpf_cookies, which allows constraining code changes only to bpftool.Quoted 8 lines
$ bpftool link 1: type 7 prog 5 bpf_cookie 123 pids bootstrap(81)Signed-off-by Dmitrii Dolgov <[email protected]>Acked-by Yonghong Song <[email protected]>---
Quoted 5 lines
Quentin, any opinion on this feature? The implementation seems straightforward enough. We'll need to not forget to expand the support to other types that support
bpf_cookies(and multi-attach kprobes and fentries will be problematic, potentially), but this might be useful for debugging purposes.
No strong opinion. I'm generally in favour of adding more useful info to bpftool's output; I've not found myself in need for the bpf_cookie so far, but if it's helpful for debugging, then it makes sense to me that bpftool be the tool to provide the info. The change looks clean indeed. Agreed also that this will require us to think of updating bpftool when new types gain support for the cookies. What would be the problem with multi-attach, the kprobes/fentries would have several cookies?
Quentin
Quoted 30 lines · Quentin Monnet
Quoted 22 lines · Andrii Nakryiko
Quoted 11 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.Implemented using the pid iterator BPF program to actually fetch
bpf_cookies, which allows constraining code changes only to bpftool.Quoted 8 lines
$ bpftool link 1: type 7 prog 5 bpf_cookie 123 pids bootstrap(81)Signed-off-by Dmitrii Dolgov <[email protected]>Acked-by Yonghong Song <[email protected]>---
Quoted 5 lines
Quentin, any opinion on this feature? The implementation seems straightforward enough. We'll need to not forget to expand the support to other types that support
bpf_cookies(and multi-attach kprobes and fentries will be problematic, potentially), but this might be useful for debugging purposes.
Quoted 4 lines
No strong opinion. I'm generally in favour of adding more useful info to bpftool's output; I've not found myself in need for the
bpf_cookieso far, but if it's helpful for debugging, then it makes sense to me that bpftool be the tool to provide the info. The change looks clean
Can I get your ack for this change, then?
Quoted 4 lines
indeed. Agreed also that this will require us to think of updating bpftool when new types gain support for the cookies. What would be the problem with multi-attach, the kprobes/fentries would have several cookies?
Yes, multi-attach links will have one cookie for each attach target, so there will be, in general, a multitude of cookie values.
Quoted 2 lines
Quentin
Quentin Monnet
Acked-byLe 15 mars 2022 18:33:02 GMT+00:00, Andrii Nakryiko <[email protected]> a écrit :
Quoted 31 lines · Quentin Monnet
Quoted 30 lines · Andrii Nakryiko
Quoted 27 lines · Dmitrii Dolgov
Quoted 11 lines
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.Implemented using the pid iterator BPF program to actually fetch
bpf_cookies, which allows constraining code changes only to bpftool.Quoted 8 lines
$ bpftool link 1: type 7 prog 5 bpf_cookie 123 pids bootstrap(81)Signed-off-by Dmitrii Dolgov <[email protected]>Acked-by Yonghong Song <[email protected]>---
Quentin, any opinion on this feature? The implementation seems straightforward enough. We'll need to not forget to expand the support to other types that support
bpf_cookies(and multi-attach kprobes and fentries will be problematic, potentially), but this might be useful for debugging purposes.
Quoted 6 lines
Quoted 4 lines
No strong opinion. I'm generally in favour of adding more useful info to bpftool's output; I've not found myself in need for the
bpf_cookieso far, but if it's helpful for debugging, then it makes sense to me that bpftool be the tool to provide the info. The change looks cleanCan I get your ack for this change, then?
Quoted 8 lines
Quoted 5 lines
indeed. Agreed also that this will require us to think of updating bpftool when new types gain support for the cookies. What would be the problem with multi-attach, the kprobes/fentries would have several cookies?
Yes, multi-attach links will have one cookie for each attach target, so there will be, in general, a multitude of cookie values.
OK thank you
Quant in
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 8 lines · On Wed, 9 Mar 2022 17:31:12 +0100 you wrote:
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.[...]
Here is the summary with links:
- [bpf-next,v6] bpftool: Add bpf_cookie to link output
https://git.kernel.org/bpf/bpf-next/c/cbdaf71f7e65
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
2022-03-09 17:31 UTC+0100 ~ Dmitrii Dolgov <[email protected]>
Quoted 74 lines
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.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]>Acked-by Yonghong Song <[email protected]>---
Changes in v6:
- Remove unnecessary initialization of fields inpid_iter_entry
- Changingbpf_cookie_settohas_bpf_cookie
- Small code cleanup (castingbpf_cookiewhen needed, removing__always_inline, etc.)Changes in v5:
- Remove unneeded cookie assignsChanges 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 | 8 ++++++++ tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 22 ++++++++++++++++++++++ tools/bpf/bpftool/skeleton/pid_iter.h | 2 ++ 4 files changed, 34 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 has_bpf_cookie; +__u64 bpf_cookie; };struct btf ;tools/bpf/bpftool/pids.c +2
-0@@ -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->has_bpf_cookie = e->has_bpf_cookie; + 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 212
Quoted 6 lines
tools/bpf/bpftool/pids.c +3
-0@@ -205,6 +207,9 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id,if (refs->ref_cnt ==0 )break ; +if (refs->has_bpf_cookie) +jsonw_lluint_field (json_writer,"bpf_cookie" , refs->bpf_cookie); +
Thinking again about this patch, shouldn't the JSON output for the cookie(s) be an array if we expect to have several cookies for multi-attach links in the future?
Quentin
Quoted 28 lines · Quentin Monnet
2022-03-09 17:31 UTC+0100 ~ Dmitrii Dolgov <[email protected]>
Quoted 26 lines
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.[...]
tools/bpf/bpftool/pids.c +2
-0@@ -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->has_bpf_cookie = e->has_bpf_cookie; + 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 ;
Quoted 7 lines
tools/bpf/bpftool/pids.c void emit_obj_refs_json(struct hashmap *map, __u32 id, · line 212
Quoted 3 lines
tools/bpf/bpftool/pids.c +3
-0+if (refs->has_bpf_cookie) +jsonw_lluint_field (json_writer,"bpf_cookie" , refs->bpf_cookie); +Thinking again about this patch, shouldn't the JSON output for the cookie(s) be an array if we expect to have several cookies for multi-attach links in the future?
Interesting point. My impression is that this could be done together with the other changes about making multi-attach links possible (I didn't miss anything, it's not yet implemented, right?). On the other hand I'm planning to prepare few more patches in similar direction -- so if everyone agrees it has to be extended to an array now, I can tackle this as well.
2022-03-26 10:08 UTC+0100 ~ Dmitry Dolgov <[email protected]>
Quoted 36 lines
Quoted 28 lines · Quentin Monnet
2022-03-09 17:31 UTC+0100 ~ Dmitrii Dolgov <[email protected]>
Quoted 26 lines
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.[...]
tools/bpf/bpftool/pids.c +2
-0@@ -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->has_bpf_cookie = e->has_bpf_cookie; + 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 ;Quoted 7 lines
tools/bpf/bpftool/pids.c void emit_obj_refs_json(struct hashmap *map, __u32 id, · line 212
Quoted 3 lines
tools/bpf/bpftool/pids.c +3
-0+if (refs->has_bpf_cookie) +jsonw_lluint_field (json_writer,"bpf_cookie" , refs->bpf_cookie); +Thinking again about this patch, shouldn't the JSON output for the cookie(s) be an array if we expect to have several cookies for multi-attach links in the future?
Quoted 6 lines
Interesting point. My impression is that this could be done together with the other changes about making multi-attach links possible (I didn't miss anything, it's not yet implemented, right?). On the other hand I'm planning to prepare few more patches in similar direction -- so if everyone agrees it has to be extended to an array now, I can tackle this as well.
Correct, it's not implemented yet for multi-attach links. My concern here is to avoid changing the JSON structure in the future (to avoid breaking changes for tools that would process the JSON). If we know we're likely to have several cookies in the future, it may be worth using an array “from start” (since no version has been tagged yet after you added support for the cookie).
Quentin
Quoted 8 lines · Quentin Monnet
Correct, it's not implemented yet for multi-attach links. My concern here is to avoid changing the JSON structure in the future (to avoid breaking changes for tools that would process the JSON). If we know we're likely to have several cookies in the future, it may be worth using an array “from start” (since no version has been tagged yet after you added support for the cookie).
Yep, sounds convincing enough to me, will prepare the chance shortly.
Quoted 45 lines · Quentin Monnet
2022-03-26 10:08 UTC+0100 ~ Dmitry Dolgov <[email protected]>
Quoted 36 lines
Quoted 28 lines · Quentin Monnet
2022-03-09 17:31 UTC+0100 ~ Dmitrii Dolgov <[email protected]>
Quoted 26 lines
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.[...]
tools/bpf/bpftool/pids.c +2
-0@@ -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->has_bpf_cookie = e->has_bpf_cookie; + 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 ;Quoted 7 lines
tools/bpf/bpftool/pids.c void emit_obj_refs_json(struct hashmap *map, __u32 id, · line 212
Quoted 3 lines
tools/bpf/bpftool/pids.c +3
-0+if (refs->has_bpf_cookie) +jsonw_lluint_field (json_writer,"bpf_cookie" , refs->bpf_cookie); +Thinking again about this patch, shouldn't the JSON output for the cookie(s) be an array if we expect to have several cookies for multi-attach links in the future?
Quoted 6 lines
Interesting point. My impression is that this could be done together with the other changes about making multi-attach links possible (I didn't miss anything, it's not yet implemented, right?). On the other hand I'm planning to prepare few more patches in similar direction -- so if everyone agrees it has to be extended to an array now, I can tackle this as well.
Quoted 6 lines
Correct, it's not implemented yet for multi-attach links. My concern here is to avoid changing the JSON structure in the future (to avoid breaking changes for tools that would process the JSON). If we know we're likely to have several cookies in the future, it may be worth using an array “from start” (since no version has been tagged yet after you added support for the cookie).
The problem with multi-cookie links (like KPROBE_MULTI that was merged a week ago) is that cookies by themselves are not that helpful. Also, internally we change their order to be sorted according to resolved kernel function addresses. So just an array of cookies are not enough, but asking kernel to preserve all the addresses just for the sake of reporting them in bpftool seems to much.
So in general, with multi-cookie links, it's not clear what you should report at all.
Quoted 2 lines
Quentin