libbpf: support "module:function" syntax for tracing programs
Subsystems (4)
v2 cover libbpf: support "module:function" syntax for tracing programs
In some situations, it is useful to explicitly specify a kernel module to search for a tracing program target (e.g. when a function of the same name exists in multiple modules or in vmlinux).
This change enables that by allowing the "module:function" syntax for the find_kernel_btf_id function. Thanks to this, the syntax can be used both from a SEC macro (i.e. SEC(fentry/module:function)) and via the bpf_program__set_attach_target API call.
---
Changes in v2:
- stylistic changes (suggested by Andrii)
- added Andrii's ack to the second patch
Viktor Malik (2):
libbpf: support "module:function" syntax for tracing programs selftests/bpf: add tests for the "module:function" syntax
tools/lib/bpf/libbpf.c | 35 ++++++++++++++----- .../selftests/bpf/prog_tests/module_attach.c | 6 ++++ .../selftests/bpf/progs/test_module_attach.c | 23 ++++++++++++ 3 files changed, 55 insertions(+), 9 deletions(-)
Signature · 1 line
2.44.0
PATCH v2 1/2 libbpf: support "module:function" syntax for tracing programs
In some situations, it is useful to explicitly specify a kernel module to search for a tracing program target (e.g. when a function of the same name exists in multiple modules or in vmlinux).
This patch enables that by allowing the "module:function" syntax for the find_kernel_btf_id function. Thanks to this, the syntax can be used both from a SEC macro (i.e. SEC(fentry/module:function)) and via the bpf_program__set_attach_target API call.
--- tools/lib/bpf/libbpf.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-)
tools/lib/bpf/libbpf.c +26 -9
@@ -9859,16 +9859,28 @@ static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name,enum bpf_attach_type attach_type, int *btf_obj_fd, int *btf_type_id) { - int ret, i; + int ret, i, mod_len; +const char *fn_name, *mod_name =NULL ; - ret =find_attach_btf_id (obj->btf_vmlinux, attach_name, attach_type); -if (ret >0 ) { - *btf_obj_fd =0 ;/* vmlinux BTF */ - *btf_type_id = ret; -return 0 ; + fn_name =strchr (attach_name,':' ); +if (fn_name) { + mod_name = attach_name; + mod_len = fn_name - mod_name; + fn_name++; + } + +if (!mod_name ||strncmp (mod_name,"vmlinux" , mod_len) ==0 ) { + ret =find_attach_btf_id (obj->btf_vmlinux, + mod_name ? fn_name : attach_name, + attach_type); +if (ret >0 ) { + *btf_obj_fd =0 ;/* vmlinux BTF */ + *btf_type_id = ret; +return 0 ; + } +if (ret != -ENOENT ) +return ret; } -if (ret != -ENOENT ) -return ret; ret =load_module_btfs (obj);if (ret)
@@ -9877,7 +9889,12 @@ static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name,for (i =0 ; i < obj->btf_module_cnt; i++) {const struct module_btf *mod = &obj->btf_modules[i]; - ret =find_attach_btf_id (mod->btf, attach_name, attach_type); +if (mod_name &&strncmp (mod->name, mod_name, mod_len) !=0 ) +continue ; + + ret =find_attach_btf_id (mod->btf, + mod_name ? fn_name : attach_name, + attach_type);if (ret >0 ) { *btf_obj_fd = mod->fd; *btf_type_id = ret;
Signature · 1 line
2.44.0
PATCH v2 2/2 selftests/bpf: add tests for the "module:function" syntax
In reply to Viktor MalikThe previous patch added support for the "module:function" syntax for tracing programs. This adds tests for explicitly specifying the module name via the SEC macro and via the bpf_program__set_attach_target call.
--- .../selftests/bpf/prog_tests/module_attach.c | 6 +++++ .../selftests/bpf/progs/test_module_attach.c | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+)
tools/testing/selftests/bpf/prog_tests/module_attach.c +6 -0
@@ -51,6 +51,10 @@ void test_module_attach(void) 0, "bpf_testmod_test_read");ASSERT_OK (err,"set_attach_target" ); + err =bpf_program__set_attach_target (skel->progs.handle_fentry_explicit_manual, +0 ,"bpf_testmod:bpf_testmod_test_read" ); +ASSERT_OK (err,"set_attach_target_explicit" ); + err =test_module_attach__load (skel);if (CHECK (err,"skel_load" ,"failed to load skeleton\n" ))return ;
@@ -70,6 +74,8 @@ void test_module_attach(void)ASSERT_EQ (bss->tp_btf_read_sz,READ_SZ ,"tp_btf" );ASSERT_EQ (bss->fentry_read_sz,READ_SZ ,"fentry" );ASSERT_EQ (bss->fentry_manual_read_sz,READ_SZ ,"fentry_manual" ); +ASSERT_EQ (bss->fentry_explicit_read_sz,READ_SZ ,"fentry_explicit" ); +ASSERT_EQ (bss->fentry_explicit_manual_read_sz,READ_SZ ,"fentry_explicit_manual" );ASSERT_EQ (bss->fexit_read_sz,READ_SZ ,"fexit" );ASSERT_EQ (bss->fexit_ret, -EIO ,"fexit_tet" );ASSERT_EQ (bss->fmod_ret_read_sz,READ_SZ ,"fmod_ret" );
tools/testing/selftests/bpf/progs/test_module_attach.c +23 -0
@@ -73,6 +73,29 @@ int BPF_PROG(handle_fentry_manual,return 0 ; } +__u32 fentry_explicit_read_sz =0 ; + +SEC("fentry/bpf_testmod:bpf_testmod_test_read") +int BPF_PROG (handle_fentry_explicit , +struct file *file,struct kobject *kobj, +struct bin_attribute *bin_attr,char *buf,loff_t off,size_t len) +{ + fentry_explicit_read_sz = len; +return 0 ; +} + + +__u32 fentry_explicit_manual_read_sz =0 ; + +SEC("fentry") +int BPF_PROG (handle_fentry_explicit_manual , +struct file *file,struct kobject *kobj, +struct bin_attribute *bin_attr,char *buf,loff_t off,size_t len) +{ + fentry_explicit_manual_read_sz = len; +return 0 ; +} +__u32 fexit_read_sz =0 ;int fexit_ret =0 ;
Signature · 1 line
2.44.0
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 10 lines · On Tue, 30 Apr 2024 11:38:05 +0200 you wrote:
In some situations, it is useful to explicitly specify a kernel module to search for a tracing program target (e.g. when a function of the same name exists in multiple modules or in vmlinux).
This change enables that by allowing the "module:function" syntax for the
find_kernel_btf_idfunction. Thanks to this, the syntax can be used both from a SEC macro (i.e.SEC(fentry/module:function)) and via thebpf_program__set_attach_targetAPI call.[...]
Here is the summary with links:
- [bpf-next,v2,1/2] libbpf: support "module:function" syntax for tracing programs
https://git.kernel.org/bpf/bpf-next/c/8f8a024272f3 - [bpf-next,v2,2/2] selftests/bpf: add tests for the "module:function" syntax https://git.kernel.org/bpf/bpf-next/c/960635887c96
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html