bpf: show attached name for progs without btf name
Subsystems (8)
cover bpf: show attached name for progs without btf name
With progs.debug we can get the attached name of the bpf prog which is attached to a kernel function. But we can't get bpf progs which are attached to others, like a cgroup, sockmap and etc. This patchset means to extend the attached name to other types.
The member attach_func_name in struct bpf_prog_aux is renamed to attach_name in patch #1, to avoid possible confusion. Then in patch #2 and #3 I extend the attached name to bpf progs which are attached to a cgroup or a sockmap. If this solution is acceptable, I will extend it to other attach types in the next step.
Yafang Shao (3):
bpf: rename attach_func_name to attach_name
bpf: set attached cgroup name in attach_name
bpf: set attached sockmap id in attach_name
include/linux/bpf.h | 3 +- kernel/bpf/bpf_iter.c | 2 +- kernel/bpf/bpf_lsm.c | 2 +- kernel/bpf/btf.c | 2 +- kernel/bpf/cgroup.c | 8 + kernel/bpf/preload/iterators/iterators.bpf.c | 4 +- kernel/bpf/preload/iterators/iterators.skel.h | 488 +++++++++--------- kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 4 +- net/core/sock_map.c | 8 +- 10 files changed, 276 insertions(+), 247 deletions(-)
Signature · 1 line
2.17.1
RFC PATCH 1/3 bpf: rename attach_func_name to attach_name
The member attach_func_name in struct bpf_prog_aux is renamed to attach_name, in order to show a non-function name.
This is a preparation of the followup patches.
--- include/linux/bpf.h | 3 +- kernel/bpf/bpf_iter.c | 2 +- kernel/bpf/bpf_lsm.c | 2 +- kernel/bpf/btf.c | 2 +- kernel/bpf/preload/iterators/iterators.bpf.c | 4 +- kernel/bpf/preload/iterators/iterators.skel.h | 488 +++++++++--------- kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 4 +- 8 files changed, 261 insertions(+), 246 deletions(-)
include/linux/bpf.h +1 -2
@@ -878,8 +878,7 @@ struct bpf_prog_aux {struct hlist_node tramp_hlist;/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */ const struct btf_type *attach_func_proto; -/* function name for valid attach_btf_id */ -const char *attach_func_name; +const char *attach_name;struct bpf_prog **func;void *jit_data;/* JIT specific data. arch dependent */ struct bpf_jit_poke_descriptor *poke_tab;
kernel/bpf/bpf_iter.c +1 -1
@@ -328,7 +328,7 @@ static void cache_btf_id(struct bpf_iter_target_info *tinfo,bool bpf_iter_prog_supported (struct bpf_prog *prog) { -const char *attach_fname = prog->aux->attach_func_name; +const char *attach_fname = prog->aux->attach_name;u32 prog_btf_id = prog->aux->attach_btf_id;const char *prefix =BPF_ITER_FUNC_PREFIX ;struct bpf_iter_target_info *tinfo;
kernel/bpf/bpf_lsm.c +1 -1
@@ -46,7 +46,7 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,if (!btf_id_set_contains (&bpf_lsm_hooks, prog->aux->attach_btf_id)) {bpf_log (vlog,"attach_btf_id %u points to wrong type name %s\n" , - prog->aux->attach_btf_id, prog->aux->attach_func_name); + prog->aux->attach_btf_id, prog->aux->attach_name);return -EINVAL ; }
kernel/bpf/btf.c +1 -1
@@ -4834,7 +4834,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,const struct btf_type *t = prog->aux->attach_func_proto;struct bpf_prog *tgt_prog = prog->aux->dst_prog;struct btf *btf =bpf_prog_get_target_btf (prog); -const char *tname = prog->aux->attach_func_name; +const char *tname = prog->aux->attach_name;struct bpf_verifier_log *log = info->log;const struct btf_param *args;u32 nr_args, arg;
kernel/bpf/preload/iterators/iterators.bpf.c +2 -2
@@ -40,7 +40,7 @@ struct btf {struct bpf_prog_aux {__u32 id;char name[16 ]; -const char *attach_func_name; +const char *attach_name;struct bpf_prog *dst_prog;struct bpf_func_info *func_info;struct btf *btf;
@@ -107,7 +107,7 @@ int dump_bpf_prog(struct bpf_iter__bpf_prog *ctx)BPF_SEQ_PRINTF (seq,"%4u %-16s %s %s\n" , aux->id,get_name (aux->btf, aux->func_info[0 ].type_id, aux->name), - aux->attach_func_name, aux->dst_prog->aux->name); + aux->attach_name, aux->dst_prog->aux->name);return 0 ; } char LICENSE[] SEC("license") = "GPL";
kernel/bpf/preload/iterators/iterators.skel.h +252 -236
558 lines not on this page · raw message
kernel/bpf/syscall.c +1 -1
@@ -3047,7 +3047,7 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr) }if (prog->type ==BPF_PROG_TYPE_TRACING && prog->expected_attach_type ==BPF_TRACE_RAW_TP ) { - tp_name = prog->aux->attach_func_name; + tp_name = prog->aux->attach_name;break ; } err =bpf_tracing_prog_attach (prog,0 ,0 );
kernel/bpf/verifier.c +2 -2
@@ -13524,7 +13524,7 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env) } prog->aux->attach_func_proto = func_proto; - prog->aux->attach_func_name = mname; + prog->aux->attach_name = mname; env->ops = st_ops->verifier_ops;return 0 ;
@@ -13849,7 +13849,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)/* store info about the attachment target that will be used later */ prog->aux->attach_func_proto = tgt_info.tgt_type; - prog->aux->attach_func_name = tgt_info.tgt_name; + prog->aux->attach_name = tgt_info.tgt_name;if (tgt_prog) { prog->aux->saved_dst_prog_type = tgt_prog->type;
Signature · 1 line
2.17.1
RFC PATCH 2/3 bpf: set attached cgroup name in attach_name
In reply to Yafang ShaoSet the cgroup path when a bpf prog is attached to a cgroup, and unset it when the bpf prog is detached.
Below is the result after this change,
$ cat progs.debug id name attached 5 dump_bpf_map bpf_iter_bpf_map 7 dump_bpf_prog bpf_iter_bpf_prog 17 bpf_sockmap cgroup:/ 19 bpf_redir_proxy
--- kernel/bpf/cgroup.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/bpf/cgroup.c +8 -0
@@ -440,6 +440,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};enum cgroup_bpf_attach_type atype; +char cgrp_path[64 ] ="cgroup:" ;struct bpf_prog_list *pl;struct list_head *progs;int err;
@@ -508,6 +509,11 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,else static_branch_inc (&cgroup_bpf_enabled_key[atype]);bpf_cgroup_storages_link (new_storage, cgrp, type); + +cgroup_name (cgrp, cgrp_path +strlen ("cgroup:" ),64 ); + cgrp_path[63 ] ='\0' ; + prog->aux->attach_name =kstrdup (cgrp_path,GFP_KERNEL ); +return 0 ; cleanup:
@@ -735,6 +741,8 @@ static int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,if (old_prog)bpf_prog_put (old_prog);static_branch_dec (&cgroup_bpf_enabled_key[atype]); +kfree (prog->aux->attach_name); +return 0 ; cleanup:
Signature · 1 line
2.17.1
Re: [PATCH RFC 2/3] bpf: set attached cgroup name in attach_name
Quoted 30 lines · Yafang Shao
Set the cgroup path when a bpf prog is attached to a cgroup, and unset it when the bpf prog is detached.
Below is the result after this change,
$ cat progs.debug id name attached 5 dump_bpf_map bpf_iter_bpf_map 7 dump_bpf_prog bpf_iter_bpf_prog 17 bpf_sockmap cgroup:/ 19 bpf_redir_proxySigned-off-by Yafang Shao <[email protected]>--- kernel/bpf/cgroup.c | 8 ++++++++ 1 file changed, 8 insertions(+)kernel/bpf/cgroup.c +1
-0@@ -440,6 +440,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};enum cgroup_bpf_attach_type atype; +char cgrp_path[64 ] ="cgroup:" ;struct bpf_prog_list *pl;struct list_head *progs;int err;
kernel/bpf/cgroup.c static int __cgroup_bpf_attach(struct cgroup *cgrp, · line 516
Quoted 8 lines
kernel/bpf/cgroup.c +5
-0@@ -508,6 +509,11 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,else static_branch_inc (&cgroup_bpf_enabled_key[atype]);bpf_cgroup_storages_link (new_storage, cgrp, type); + +cgroup_name (cgrp, cgrp_path +strlen ("cgroup:" ),64 ); + cgrp_path[63 ] ='\0' ; + prog->aux->attach_name =kstrdup (cgrp_path,GFP_KERNEL ); +
This is pure debug code. We cannot have it in the kernel. Not even under #ifdef.
Please do such debug code on a side as your own bpf program. For example by kprobe-ing in this function and keeping the path in a bpf map or send it to user space via ringbuf.
Or enable cgroup tracepoint and monitor cgroup_mkdir with full path. Record it in user space or in bpf map, etc.
Also please read Documentation/bpf/bpf_devel_QA.rst bpf patches should be based on bpf-next tree. These patches are not.
Re: [PATCH RFC 2/3] bpf: set attached cgroup name in attach_name
Quoted 44 lines · Alexei Starovoitov
Quoted 30 lines · Yafang Shao
Set the cgroup path when a bpf prog is attached to a cgroup, and unset it when the bpf prog is detached.
Below is the result after this change,
$ cat progs.debug id name attached 5 dump_bpf_map bpf_iter_bpf_map 7 dump_bpf_prog bpf_iter_bpf_prog 17 bpf_sockmap cgroup:/ 19 bpf_redir_proxySigned-off-by Yafang Shao <[email protected]>--- kernel/bpf/cgroup.c | 8 ++++++++ 1 file changed, 8 insertions(+)kernel/bpf/cgroup.c +1
-0@@ -440,6 +440,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};enum cgroup_bpf_attach_type atype; +char cgrp_path[64 ] ="cgroup:" ;struct bpf_prog_list *pl;struct list_head *progs;int err;kernel/bpf/cgroup.c static int __cgroup_bpf_attach(struct cgroup *cgrp, · line 516
Quoted 8 lines
kernel/bpf/cgroup.c +5
-0@@ -508,6 +509,11 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,else static_branch_inc (&cgroup_bpf_enabled_key[atype]);bpf_cgroup_storages_link (new_storage, cgrp, type); + +cgroup_name (cgrp, cgrp_path +strlen ("cgroup:" ),64 ); + cgrp_path[63 ] ='\0' ; + prog->aux->attach_name =kstrdup (cgrp_path,GFP_KERNEL ); +This is pure debug code. We cannot have it in the kernel. Not even under #ifdef.
Quoted 6 lines
Please do such debug code on a side as your own bpf program. For example by kprobe-ing in this function and keeping the path in a bpf map or send it to user space via ringbuf.
Or enable cgroup tracepoint and monitorcgroup_mkdirwith full path. Record it in user space or in bpf map, etc.
It is another possible solution to hook the related kernel functions or tracepoints, but it may be a little complicated to track all the bpf attach types, for example we also want to track BPF_PROG_TYPE_SK_MSG[1], BPF_PROG_TYPE_FLOW_DISSECTOR and etc. While the attach_name provides us a generic way to get how the bpf progs are attached, which can't be got by bpftool.
It is not for debug-only purpose, while it gives us a better way to maintain all the bpf progs running on a single host.
Quoted 3 lines
Also please read
Documentation/bpf/bpf_devel_QA.rstbpf patches should be based on bpf-next tree. These patches are not.
My local bpf-next repo is a little old.
Next time I will pull the newest bpf-next code before sending bpf patches. Thanks for the information.
[1]. https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
Signature · 2 lines
Thanks Yafang
Re: [PATCH RFC 2/3] bpf: set attached cgroup name in attach_name
Quoted 54 lines · Yafang Shao
Quoted 44 lines · Alexei Starovoitov
Quoted 30 lines · Yafang Shao
Set the cgroup path when a bpf prog is attached to a cgroup, and unset it when the bpf prog is detached.
Below is the result after this change,
$ cat progs.debug id name attached 5 dump_bpf_map bpf_iter_bpf_map 7 dump_bpf_prog bpf_iter_bpf_prog 17 bpf_sockmap cgroup:/ 19 bpf_redir_proxySigned-off-by Yafang Shao <[email protected]>--- kernel/bpf/cgroup.c | 8 ++++++++ 1 file changed, 8 insertions(+)kernel/bpf/cgroup.c +1
-0@@ -440,6 +440,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};enum cgroup_bpf_attach_type atype; +char cgrp_path[64 ] ="cgroup:" ;struct bpf_prog_list *pl;struct list_head *progs;int err;kernel/bpf/cgroup.c static int __cgroup_bpf_attach(struct cgroup *cgrp, · line 516
Quoted 8 lines
kernel/bpf/cgroup.c +5
-0@@ -508,6 +509,11 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,else static_branch_inc (&cgroup_bpf_enabled_key[atype]);bpf_cgroup_storages_link (new_storage, cgrp, type); + +cgroup_name (cgrp, cgrp_path +strlen ("cgroup:" ),64 ); + cgrp_path[63 ] ='\0' ; + prog->aux->attach_name =kstrdup (cgrp_path,GFP_KERNEL ); +This is pure debug code. We cannot have it in the kernel. Not even under #ifdef.
Quoted 6 lines
Please do such debug code on a side as your own bpf program. For example by kprobe-ing in this function and keeping the path in a bpf map or send it to user space via ringbuf.
Or enable cgroup tracepoint and monitorcgroup_mkdirwith full path. Record it in user space or in bpf map, etc.
Quoted 6 lines
It is another possible solution to hook the related kernel functions or tracepoints, but it may be a little complicated to track all the bpf attach types, for example we also want to track BPF_PROG_TYPE_SK_MSG[1],
BPF_PROG_TYPE_FLOW_DISSECTORand etc. While theattach_nameprovides us a generic way to get how the bpf progs are attached, which can't be got by bpftool.
bpftool can certainly print such details.
See how it's using task_file iterator.
It can be extended to look into cgroups and sockmap, and for each program print "sockmap:%d", map->id if so desired.
Re: [PATCH RFC 2/3] bpf: set attached cgroup name in attach_name
Quoted 63 lines · Alexei Starovoitov
Quoted 54 lines · Yafang Shao
Quoted 44 lines · Alexei Starovoitov
Quoted 30 lines · Yafang Shao
Set the cgroup path when a bpf prog is attached to a cgroup, and unset it when the bpf prog is detached.
Below is the result after this change,
$ cat progs.debug id name attached 5 dump_bpf_map bpf_iter_bpf_map 7 dump_bpf_prog bpf_iter_bpf_prog 17 bpf_sockmap cgroup:/ 19 bpf_redir_proxySigned-off-by Yafang Shao <[email protected]>--- kernel/bpf/cgroup.c | 8 ++++++++ 1 file changed, 8 insertions(+)kernel/bpf/cgroup.c +1
-0@@ -440,6 +440,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};enum cgroup_bpf_attach_type atype; +char cgrp_path[64 ] ="cgroup:" ;struct bpf_prog_list *pl;struct list_head *progs;int err;kernel/bpf/cgroup.c static int __cgroup_bpf_attach(struct cgroup *cgrp, · line 516
Quoted 8 lines
kernel/bpf/cgroup.c +5
-0@@ -508,6 +509,11 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,else static_branch_inc (&cgroup_bpf_enabled_key[atype]);bpf_cgroup_storages_link (new_storage, cgrp, type); + +cgroup_name (cgrp, cgrp_path +strlen ("cgroup:" ),64 ); + cgrp_path[63 ] ='\0' ; + prog->aux->attach_name =kstrdup (cgrp_path,GFP_KERNEL ); +This is pure debug code. We cannot have it in the kernel. Not even under #ifdef.
Quoted 6 lines
Please do such debug code on a side as your own bpf program. For example by kprobe-ing in this function and keeping the path in a bpf map or send it to user space via ringbuf.
Or enable cgroup tracepoint and monitorcgroup_mkdirwith full path. Record it in user space or in bpf map, etc.Quoted 6 lines
It is another possible solution to hook the related kernel functions or tracepoints, but it may be a little complicated to track all the bpf attach types, for example we also want to track BPF_PROG_TYPE_SK_MSG[1],
BPF_PROG_TYPE_FLOW_DISSECTORand etc. While theattach_nameprovides us a generic way to get how the bpf progs are attached, which can't be got by bpftool.
Quoted 4 lines
bpftool can certainly print such details.
See how it's usingtask_fileiterator.
It can be extended to look into cgroups and sockmap, and for each program print "sockmap:%d",map->idif so desired.
Thanks for the explanation. I will analyze it.
Signature · 2 lines
Thanks Yafang
Re: [PATCH RFC 2/3] bpf: set attached cgroup name in attach_name
In reply to Alexei StarovoitovQuoted 63 lines · Alexei Starovoitov
Quoted 54 lines · Yafang Shao
Quoted 44 lines · Alexei Starovoitov
Quoted 30 lines · Yafang Shao
Set the cgroup path when a bpf prog is attached to a cgroup, and unset it when the bpf prog is detached.
Below is the result after this change,
$ cat progs.debug id name attached 5 dump_bpf_map bpf_iter_bpf_map 7 dump_bpf_prog bpf_iter_bpf_prog 17 bpf_sockmap cgroup:/ 19 bpf_redir_proxySigned-off-by Yafang Shao <[email protected]>--- kernel/bpf/cgroup.c | 8 ++++++++ 1 file changed, 8 insertions(+)kernel/bpf/cgroup.c +1
-0@@ -440,6 +440,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};enum cgroup_bpf_attach_type atype; +char cgrp_path[64 ] ="cgroup:" ;struct bpf_prog_list *pl;struct list_head *progs;int err;kernel/bpf/cgroup.c static int __cgroup_bpf_attach(struct cgroup *cgrp, · line 516
Quoted 8 lines
kernel/bpf/cgroup.c +5
-0@@ -508,6 +509,11 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,else static_branch_inc (&cgroup_bpf_enabled_key[atype]);bpf_cgroup_storages_link (new_storage, cgrp, type); + +cgroup_name (cgrp, cgrp_path +strlen ("cgroup:" ),64 ); + cgrp_path[63 ] ='\0' ; + prog->aux->attach_name =kstrdup (cgrp_path,GFP_KERNEL ); +This is pure debug code. We cannot have it in the kernel. Not even under #ifdef.
Quoted 6 lines
Please do such debug code on a side as your own bpf program. For example by kprobe-ing in this function and keeping the path in a bpf map or send it to user space via ringbuf.
Or enable cgroup tracepoint and monitorcgroup_mkdirwith full path. Record it in user space or in bpf map, etc.Quoted 6 lines
It is another possible solution to hook the related kernel functions or tracepoints, but it may be a little complicated to track all the bpf attach types, for example we also want to track BPF_PROG_TYPE_SK_MSG[1],
BPF_PROG_TYPE_FLOW_DISSECTORand etc. While theattach_nameprovides us a generic way to get how the bpf progs are attached, which can't be got by bpftool.
Quoted 4 lines
bpftool can certainly print such details.
See how it's usingtask_fileiterator.
It can be extended to look into cgroups and sockmap, and for each program print "sockmap:%d",map->idif so desired.
I have read through the task_file code, but I haven't found a direct way to get the attached cgroups or maps of a specified prog. It is easy to look into a cgroup or sockmap, but the key point here is which is the proper cgroup or sockmap.
There are some possible ways to get the attached cgroup or sockmap.
- add new member into struct bpf_prog_aux
For example,
struct bpf_prog_aux{
union {
struct cgroup *attached_cgrp;
struct bpf_map *attached_map;
};
};
Then we can easily get the related map or cgroup by extendingtask_file iterator.
- build a table for attached maps, cgroups and etc
Like we did for pinned files.
Then we can compare the prog with the members stored in this table
one by one, but that seems a little heavy.
There may be some other ways.
I'm not sure if I clearly understand what you mean. Hopefully you could help clarify it.
Signature · 2 lines
Thanks Yafang
Re: [PATCH RFC 2/3] bpf: set attached cgroup name in attach_name
Quoted 71 lines · Yafang Shao
Quoted 63 lines · Alexei Starovoitov
Quoted 54 lines · Yafang Shao
Quoted 44 lines · Alexei Starovoitov
Quoted 30 lines · Yafang Shao
Set the cgroup path when a bpf prog is attached to a cgroup, and unset it when the bpf prog is detached.
Below is the result after this change,
$ cat progs.debug id name attached 5 dump_bpf_map bpf_iter_bpf_map 7 dump_bpf_prog bpf_iter_bpf_prog 17 bpf_sockmap cgroup:/ 19 bpf_redir_proxySigned-off-by Yafang Shao <[email protected]>--- kernel/bpf/cgroup.c | 8 ++++++++ 1 file changed, 8 insertions(+)kernel/bpf/cgroup.c +1
-0@@ -440,6 +440,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};enum cgroup_bpf_attach_type atype; +char cgrp_path[64 ] ="cgroup:" ;struct bpf_prog_list *pl;struct list_head *progs;int err;kernel/bpf/cgroup.c static int __cgroup_bpf_attach(struct cgroup *cgrp, · line 516
Quoted 8 lines
kernel/bpf/cgroup.c +5
-0@@ -508,6 +509,11 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,else static_branch_inc (&cgroup_bpf_enabled_key[atype]);bpf_cgroup_storages_link (new_storage, cgrp, type); + +cgroup_name (cgrp, cgrp_path +strlen ("cgroup:" ),64 ); + cgrp_path[63 ] ='\0' ; + prog->aux->attach_name =kstrdup (cgrp_path,GFP_KERNEL ); +This is pure debug code. We cannot have it in the kernel. Not even under #ifdef.
Quoted 6 lines
Please do such debug code on a side as your own bpf program. For example by kprobe-ing in this function and keeping the path in a bpf map or send it to user space via ringbuf.
Or enable cgroup tracepoint and monitorcgroup_mkdirwith full path. Record it in user space or in bpf map, etc.Quoted 6 lines
It is another possible solution to hook the related kernel functions or tracepoints, but it may be a little complicated to track all the bpf attach types, for example we also want to track BPF_PROG_TYPE_SK_MSG[1],
BPF_PROG_TYPE_FLOW_DISSECTORand etc. While theattach_nameprovides us a generic way to get how the bpf progs are attached, which can't be got by bpftool.Quoted 4 lines
bpftool can certainly print such details.
See how it's usingtask_fileiterator.
It can be extended to look into cgroups and sockmap, and for each program print "sockmap:%d",map->idif so desired.
Quoted 7 lines
I have read through the
task_filecode, but I haven't found a direct way to get the attached cgroups or maps of a specified prog. It is easy to look into a cgroup or sockmap, but the key point here is which is the proper cgroup or sockmap.
There are some possible ways to get the attached cgroup or sockmap.- add new member into struct
bpf_prog_aux
No. Please stop proposing kernel changes for your debug needs.
Quoted 8 lines
For example,
struct bpf_prog_aux{
union {
struct cgroup *attached_cgrp;
struct bpf_map *attached_map;
};
};
Then we can easily get the related map or cgroup by extendingQuoted 8 lines
task_fileiterator.- build a table for attached maps, cgroups and etc
Like we did for pinned files.
Then we can compare the prog with the members stored in this table
one by one, but that seems a little heavy.There may be some other ways.
- iterate bpf maps
- find sockmap
- do equivalent of sock_map_progs(), but open coded inside bpf prog
- read progs, print them.
Re: [PATCH RFC 2/3] bpf: set attached cgroup name in attach_name
Quoted 98 lines · Alexei Starovoitov
Quoted 71 lines · Yafang Shao
Quoted 63 lines · Alexei Starovoitov
Quoted 54 lines · Yafang Shao
Quoted 44 lines · Alexei Starovoitov
Quoted 30 lines · Yafang Shao
Set the cgroup path when a bpf prog is attached to a cgroup, and unset it when the bpf prog is detached.
Below is the result after this change,
$ cat progs.debug id name attached 5 dump_bpf_map bpf_iter_bpf_map 7 dump_bpf_prog bpf_iter_bpf_prog 17 bpf_sockmap cgroup:/ 19 bpf_redir_proxySigned-off-by Yafang Shao <[email protected]>--- kernel/bpf/cgroup.c | 8 ++++++++ 1 file changed, 8 insertions(+)kernel/bpf/cgroup.c +1
-0@@ -440,6 +440,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {};enum cgroup_bpf_attach_type atype; +char cgrp_path[64 ] ="cgroup:" ;struct bpf_prog_list *pl;struct list_head *progs;int err;kernel/bpf/cgroup.c static int __cgroup_bpf_attach(struct cgroup *cgrp, · line 516
Quoted 8 lines
kernel/bpf/cgroup.c +5
-0@@ -508,6 +509,11 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,else static_branch_inc (&cgroup_bpf_enabled_key[atype]);bpf_cgroup_storages_link (new_storage, cgrp, type); + +cgroup_name (cgrp, cgrp_path +strlen ("cgroup:" ),64 ); + cgrp_path[63 ] ='\0' ; + prog->aux->attach_name =kstrdup (cgrp_path,GFP_KERNEL ); +This is pure debug code. We cannot have it in the kernel. Not even under #ifdef.
Quoted 6 lines
Please do such debug code on a side as your own bpf program. For example by kprobe-ing in this function and keeping the path in a bpf map or send it to user space via ringbuf.
Or enable cgroup tracepoint and monitorcgroup_mkdirwith full path. Record it in user space or in bpf map, etc.Quoted 6 lines
It is another possible solution to hook the related kernel functions or tracepoints, but it may be a little complicated to track all the bpf attach types, for example we also want to track BPF_PROG_TYPE_SK_MSG[1],
BPF_PROG_TYPE_FLOW_DISSECTORand etc. While theattach_nameprovides us a generic way to get how the bpf progs are attached, which can't be got by bpftool.Quoted 4 lines
bpftool can certainly print such details.
See how it's usingtask_fileiterator.
It can be extended to look into cgroups and sockmap, and for each program print "sockmap:%d",map->idif so desired.Quoted 7 lines
I have read through the
task_filecode, but I haven't found a direct way to get the attached cgroups or maps of a specified prog. It is easy to look into a cgroup or sockmap, but the key point here is which is the proper cgroup or sockmap.
There are some possible ways to get the attached cgroup or sockmap.- add new member into struct
bpf_prog_auxNo. Please stop proposing kernel changes for your debug needs.
Quoted 15 lines
For example, struct bpf_prog_aux{ union { struct cgroup *attached_cgrp; struct bpf_map *attached_map; }; }; Then we can easily get the related map or cgroup by extending
task_fileiterator.- build a table for attached maps, cgroups and etc
Like we did for pinned files.
Then we can compare the prog with the members stored in this table
one by one, but that seems a little heavy.
Quoted 6 lines
Quoted 1 line
There may be some other ways.
- iterate bpf maps
- find sockmap
- do equivalent ofsock_map_progs(), but open coded inside bpf prog
- read progs, print them.
That's similar to the second way I proposed above.
The trouble is it may be too heavy to iterate the target objects, for example, there may be thousands of cgroups on a single host, and if we want to know which cgroup is attached by a bpf prog we have to iterate all the cgroups until find it.
But it seems there is no better choice.
Anyway thanks for the advice.
Signature · 2 lines
Thanks Yafang
RFC PATCH 3/3 bpf: set attached sockmap id in attach_name
In reply to Yafang ShaoSet the attached name when a bpf prog is attached to a sockmap, and unset it when the bpf prog is detached.
Below is the result after this change,
$ cat progs.debug id name attached 5 dump_bpf_map bpf_iter_bpf_map 7 dump_bpf_prog bpf_iter_bpf_prog 17 bpf_sockmap cgroup:/ 19 bpf_redir_proxy sockmap:9
$ cat maps.debug id name max_entries 3 iterator.rodata 1 9 ltcp_map 65535
--- net/core/sock_map.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
net/core/sock_map.c +7 -1
@@ -1411,6 +1411,7 @@ static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, {struct sk_psock_progs *progs =sock_map_progs (map);struct bpf_prog **pprog; +char sockmap_info[16 ];if (!progs)return -EOPNOTSUPP ;
@@ -1438,8 +1439,13 @@ static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,return -EOPNOTSUPP ; } -if (old) +if (old) { +kfree (prog->aux->attach_name);return psock_replace_prog (pprog, prog, old); + } + +snprintf (sockmap_info,16 ,"sockmap:%d" , map->id); + prog->aux->attach_name =kstrdup (sockmap_info,GFP_KERNEL );psock_set_prog (pprog, prog);return 0 ;
Signature · 1 line
2.17.1