add support for writable bare tracepoint
Subsystems (8)
v5 cover add support for writable bare tracepoint
From: Hou Tao <[email protected]>
Hi,
The patchset series supports writable context for bare tracepoint.
The main idea comes from patchset "writable contexts for bpf raw tracepoints" [1], but it only supports normal tracepoint with associated trace event under tracefs. Now we have one use case in which we add bare tracepoint in VFS layer, and update file::f_mode for specific files. The reason using bare tracepoint is that it doesn't form a ABI and we can change it freely. So add support for it in BPF.
Comments are always welcome.
[1]: https://lore.kernel.org/lkml/[email protected]
Change log:
v5:
* rebased on bpf-next
* patch 1: add Acked-by tag
* patch 2: handle invalid section name, make prefixes array being const
v4: https://www.spinics.net/lists/bpf/msg47021.html
* rebased on bpf-next
* update patch 2 to add support for writable raw tracepoint attachment in attach_raw_tp().
* update patch 3 to add Acked-by tag
v3: https://www.spinics.net/lists/bpf/msg46824.html
* use raw_tp.w instead of raw_tp_writable as section name of writable tp
* use ASSERT_XXX() instead of CHECK()
* define a common macro for "/sys/kernel/bpf_testmod"
v2: https://www.spinics.net/lists/bpf/msg46356.html
* rebase on bpf-next tree
* address comments from Yonghong Song
* rename bpf_testmode_test_writable_ctx::ret as early_ret to reflect its purpose better.
v1: https://www.spinics.net/lists/bpf/msg46221.html
Hou Tao (3):
bpf: support writable context for bare tracepoint
libbpf: support detecting and attaching of writable tracepoint program bpf/selftests: add test for writable bare tracepoint
include/trace/bpf_probe.h | 19 +++++++--- tools/lib/bpf/libbpf.c | 26 +++++++++++--- .../bpf/bpf_testmod/bpf_testmod-events.h | 15 ++++++++ .../selftests/bpf/bpf_testmod/bpf_testmod.c | 10 ++++++ .../selftests/bpf/bpf_testmod/bpf_testmod.h | 5 +++ .../selftests/bpf/prog_tests/module_attach.c | 35 +++++++++++++++++++ .../selftests/bpf/progs/test_module_attach.c | 14 ++++++++ tools/testing/selftests/bpf/test_progs.c | 4 +-- tools/testing/selftests/bpf/test_progs.h | 2 ++ 9 files changed, 119 insertions(+), 11 deletions(-)
Signature · 1 line
2.20.1
PATCH v5 1/3 bpf: support writable context for bare tracepoint
From: Hou Tao <[email protected]>
Commit 9df1c28bb752 ("bpf: add writable context for raw tracepoints") supports writable context for tracepoint, but it misses the support for bare tracepoint which has no associated trace event.
Bare tracepoint is defined by DECLARE_TRACE(), so adding a corresponding DECLARE_TRACE_WRITABLE() macro to generate a definition in __bpf_raw_tp_map section for bare tracepoint in a similar way to DEFINE_TRACE_WRITABLE().
--- include/trace/bpf_probe.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
include/trace/bpf_probe.h +15 -4
@@ -93,8 +93,7 @@ __section("__bpf_raw_tp_map") = { \#define FIRST (x, ...) x -#undef DEFINE_EVENT_WRITABLE -#define DEFINE_EVENT_WRITABLE (template, call, proto, args, size) \ +#define __CHECK_WRITABLE_BUF_SIZE (call, proto, args, size) \ static inline void bpf_test_buffer_##call(void) \ { \/* BUILD_BUG_ON() is ignored if the code is completely eliminated, but \
@@ -103,8 +102,12 @@ static inline void bpf_test_buffer_##call(void) \*/ \FIRST (proto); \ (void )BUILD_BUG_ON_ZERO (size !=sizeof (*FIRST (args))); \ -} \ -__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size) +} + +#undef DEFINE_EVENT_WRITABLE +#define DEFINE_EVENT_WRITABLE (template, call, proto, args, size) \ + __CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \ + __DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size)#undef DEFINE_EVENT#define DEFINE_EVENT (template, call, proto, args) \
@@ -119,9 +122,17 @@ __DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size) __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \ __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0) +#undef DECLARE_TRACE_WRITABLE +#define DECLARE_TRACE_WRITABLE (call, proto, args, size) \ + __CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \ + __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \ + __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size) +#include TRACE_INCLUDE (TRACE_INCLUDE_FILE ) +#undef DECLARE_TRACE_WRITABLE#undef DEFINE_EVENT_WRITABLE +#undef __CHECK_WRITABLE_BUF_SIZE#undef __DEFINE_EVENT#undef FIRST
Signature · 1 line
2.20.1
PATCH v5 2/3 libbpf: support detecting and attaching of writable tracepoint program
In reply to Hou TaoFrom: Hou Tao <[email protected]>
Program on writable tracepoint is BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, but its attachment is the same as BPF_PROG_TYPE_RAW_TRACEPOINT.
--- tools/lib/bpf/libbpf.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
tools/lib/bpf/libbpf.c +21 -5
@@ -8029,6 +8029,8 @@ static const struct bpf_sec_def section_defs[] = {SEC_DEF ("tp/" ,TRACEPOINT ,0 ,SEC_NONE , attach_tp),SEC_DEF ("raw_tracepoint/" ,RAW_TRACEPOINT ,0 ,SEC_NONE , attach_raw_tp),SEC_DEF ("raw_tp/" ,RAW_TRACEPOINT ,0 ,SEC_NONE , attach_raw_tp), +SEC_DEF ("raw_tracepoint.w/" ,RAW_TRACEPOINT_WRITABLE ,0 ,SEC_NONE , attach_raw_tp), +SEC_DEF ("raw_tp.w/" ,RAW_TRACEPOINT_WRITABLE ,0 ,SEC_NONE , attach_raw_tp),SEC_DEF ("tp_btf/" ,TRACING ,BPF_TRACE_RAW_TP ,SEC_ATTACH_BTF , attach_trace),SEC_DEF ("fentry/" ,TRACING ,BPF_TRACE_FENTRY ,SEC_ATTACH_BTF , attach_trace), SEC_DEF("fmod_ret/", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
@@ -9786,12 +9788,26 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prstatic struct bpf_link *attach_raw_tp (const struct bpf_program *prog,long cookie) { -const char *tp_name; +static const char *const prefixes[] = { +"raw_tp/" , +"raw_tracepoint/" , +"raw_tp.w/" , +"raw_tracepoint.w/" , + }; +size_t i; +const char *tp_name =NULL ; -if (str_has_pfx (prog->sec_name,"raw_tp/" )) - tp_name = prog->sec_name +sizeof ("raw_tp/" ) -1 ; -else - tp_name = prog->sec_name +sizeof ("raw_tracepoint/" ) -1 ; +for (i =0 ; i <ARRAY_SIZE (prefixes); i++) { +if (str_has_pfx (prog->sec_name, prefixes[i])) { + tp_name = prog->sec_name +strlen (prefixes[i]); +break ; + } + } +if (!tp_name) { +pr_warn ("prog '%s': invalid section name '%s'\n" , + prog->name, prog->sec_name); +return libbpf_err_ptr (-EINVAL ); + }return bpf_program__attach_raw_tracepoint (prog, tp_name); }
Signature · 1 line
2.20.1
PATCH v5 3/3 bpf/selftests: add test for writable bare tracepoint
In reply to Hou TaoFrom: Hou Tao <[email protected]>
Add a writable bare tracepoint in bpf_testmod module, and trigger its calling when reading /sys/kernel/bpf_testmod with a specific buffer length. The reading will return the value in writable context if the early return flag is enabled in writable context.
--- .../bpf/bpf_testmod/bpf_testmod-events.h | 15 ++++++++ .../selftests/bpf/bpf_testmod/bpf_testmod.c | 10 ++++++ .../selftests/bpf/bpf_testmod/bpf_testmod.h | 5 +++ .../selftests/bpf/prog_tests/module_attach.c | 35 +++++++++++++++++++ .../selftests/bpf/progs/test_module_attach.c | 14 ++++++++ tools/testing/selftests/bpf/test_progs.c | 4 +-- tools/testing/selftests/bpf/test_progs.h | 2 ++ 7 files changed, 83 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h +15 -0
@@ -34,6 +34,21 @@ DECLARE_TRACE(bpf_testmod_test_write_bare,TP_ARGS (task, ctx) ); +#undef BPF_TESTMOD_DECLARE_TRACE +#ifdef DECLARE_TRACE_WRITABLE +#define BPF_TESTMOD_DECLARE_TRACE (call, proto, args, size) \ + DECLARE_TRACE_WRITABLE(call, PARAMS(proto), PARAMS(args), size) +#else +#define BPF_TESTMOD_DECLARE_TRACE (call, proto, args, size) \ + DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) +#endif + +BPF_TESTMOD_DECLARE_TRACE (bpf_testmod_test_writable_bare, + TP_PROTO(struct bpf_testmod_test_writable_ctx *ctx), +TP_ARGS (ctx), +sizeof (struct bpf_testmod_test_writable_ctx ) +); +#endif /* _BPF_TESTMOD_EVENTS_H */ #undef TRACE_INCLUDE_PATH
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +10 -0
@@ -42,6 +42,16 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,if (bpf_testmod_loop_test (101 ) >100 )trace_bpf_testmod_test_read (current, &ctx); +/* Magic number to enable writable tp */ +if (len ==64 ) { +struct bpf_testmod_test_writable_ctx writable = { + .val =1024 , + }; +trace_bpf_testmod_test_writable_bare (&writable); +if (writable.early_ret) +return snprintf (buf, len,"%d\n" , writable.val); + } +return -EIO ;/* always fail */ }EXPORT_SYMBOL (bpf_testmod_test_read);
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +5 -0
@@ -17,4 +17,9 @@ struct bpf_testmod_test_write_ctx { size_t len; }; +struct bpf_testmod_test_writable_ctx { +bool early_ret; +int val; +}; +#endif /* _BPF_TESTMOD_H */
tools/testing/selftests/bpf/prog_tests/module_attach.c +35 -0
@@ -2,10 +2,36 @@/* Copyright (c) 2020 Facebook */ #include <test_progs.h> +#include <stdbool.h> #include "test_module_attach.skel.h" static int duration; +static int trigger_module_test_writable (int *val) +{ +int fd, err; +char buf[65 ]; +ssize_t rd; + + fd =open (BPF_TESTMOD_TEST_FILE ,O_RDONLY ); + err = -errno; +if (!ASSERT_GE (fd,0 ,"testmode_file_open" )) +return err; + + rd =read (fd, buf,sizeof (buf) -1 ); + err = -errno; +if (!ASSERT_GT (rd,0 ,"testmod_file_rd_val" )) { +close (fd); +return err; + } + + buf[rd] ='\0' ; + *val =strtol (buf,NULL ,0 ); +close (fd); + +return 0 ; +} +static int delete_module (const char *name,int flags) {return syscall (__NR_delete_module, name, flags);
@@ -19,6 +45,7 @@ void test_module_attach(void)struct test_module_attach__bss *bss;struct bpf_link *link;int err; +int writable_val =0 ; skel =test_module_attach__open ();if (CHECK (!skel,"skel_open" ,"failed to open skeleton\n" ))
@@ -51,6 +78,14 @@ void test_module_attach(void)ASSERT_EQ (bss->fexit_ret, -EIO ,"fexit_tet" );ASSERT_EQ (bss->fmod_ret_read_sz,READ_SZ ,"fmod_ret" ); + bss->raw_tp_writable_bare_early_ret = true; + bss->raw_tp_writable_bare_out_val =0xf1f2f3f4 ; +ASSERT_OK (trigger_module_test_writable (&writable_val), +"trigger_writable" ); +ASSERT_EQ (bss->raw_tp_writable_bare_in_val,1024 ,"writable_test_in" ); +ASSERT_EQ (bss->raw_tp_writable_bare_out_val, writable_val, +"writable_test_out" ); +test_module_attach__detach (skel);/* attach fentry/fexit and make sure it get's module reference */
tools/testing/selftests/bpf/progs/test_module_attach.c +14 -0
@@ -27,6 +27,20 @@ int BPF_PROG(handle_raw_tp_bare,return 0 ; } +int raw_tp_writable_bare_in_val =0 ; +int raw_tp_writable_bare_early_ret =0 ; +int raw_tp_writable_bare_out_val =0 ; + +SEC("raw_tp.w/bpf_testmod_test_writable_bare") +int BPF_PROG (handle_raw_tp_writable_bare , +struct bpf_testmod_test_writable_ctx *writable) +{ + raw_tp_writable_bare_in_val = writable->val; + writable->early_ret = raw_tp_writable_bare_early_ret; + writable->val = raw_tp_writable_bare_out_val; +return 0 ; +} +__u32 tp_btf_read_sz =0 ;SEC ("tp_btf/bpf_testmod_test_read" )
tools/testing/selftests/bpf/test_progs.c +2 -2
@@ -747,7 +747,7 @@ int trigger_module_test_read(int read_sz) {int fd, err; - fd =open ("/sys/kernel/bpf_testmod" ,O_RDONLY ); + fd =open (BPF_TESTMOD_TEST_FILE ,O_RDONLY ); err = -errno;if (!ASSERT_GE (fd,0 ,"testmod_file_open" ))return err;
@@ -769,7 +769,7 @@ int trigger_module_test_write(int write_sz)memset (buf,'a' , write_sz); buf[write_sz-1 ] ='\0' ; - fd =open ("/sys/kernel/bpf_testmod" ,O_WRONLY ); + fd =open (BPF_TESTMOD_TEST_FILE ,O_WRONLY ); err = -errno;if (!ASSERT_GE (fd,0 ,"testmod_file_open" )) {free (buf);
tools/testing/selftests/bpf/test_progs.h +2 -0
@@ -301,3 +301,5 @@ int trigger_module_test_write(int write_sz);#else #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"#endif + +#define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
Signature · 1 line
2.20.1
Steven Rostedt
maintainerQuoted 7 lines · On Mon, 4 Oct 2021 17:48:54 +0800 Hou Tao <[email protected]> wrote:
The main idea comes from patchset "writable contexts for bpf raw tracepoints" [1], but it only supports normal tracepoint with associated trace event under tracefs. Now we have one use case in which we add bare tracepoint in VFS layer, and update
file::f_modefor specific files. The reason using bare tracepoint is that it doesn't form a ABI and we can change it freely. So add support for it in BPF.
Are the VFS maintainers against adding a trace event with just a pointer as an interface?
That is, it only gives you a pointer to what is passed in, but does not give you anything else to form any API against it.
This way, not only does BPF have access to this information, so do the other tracers, through the new eprobe interface:
(I just realized we are missing updates to the Documentation directory).
event probes allows one to attach to an existing trace event, and then create a new trace event that can read through pointers. It uses the same interface that kprobes has.
Just adding trace events to VFS that only have pointers would allow all of BPF, perf and ftrace access as eprobes could then get the data you are looking for.
-- Steve
Hi Steven,
Quoted 10 lines · Steven Rostedt
Quoted 7 lines · On Mon, 4 Oct 2021 17:48:54 +0800 Hou Tao <[email protected]> wrote:
The main idea comes from patchset "writable contexts for bpf raw tracepoints" [1], but it only supports normal tracepoint with associated trace event under tracefs. Now we have one use case in which we add bare tracepoint in VFS layer, and update
file::f_modefor specific files. The reason using bare tracepoint is that it doesn't form a ABI and we can change it freely. So add support for it in BPF.
Quoted 2 lines
Are the VFS maintainers against adding a trace event with just a pointer as an interface?
Not tried yet, but considering that VFS maintainer refused to have tracepoint in VFS layer, I'm not sure it is worth trying.
Quoted 5 lines
That is, it only gives you a pointer to what is passed in, but does not give you anything else to form any API against it.
This way, not only does BPF have access to this information, so do the other tracers, through the new eprobe interface:
Or in a opposite way can eprobe add support for bare tracepoint ?
Quoted 15 lines
(I just realized we are missing updates to the Documentation directory).
event probes allows one to attach to an existing trace event, and then create a new trace event that can read through pointers. It uses the same interface that kprobes has.
Just adding trace events to VFS that only have pointers would allow all of BPF, perf and ftrace access as eprobes could then get the data you are looking for.
-- Steve
.
Steven Rostedt
maintainerQuoted 2 lines · On Sat, 9 Oct 2021 20:07:10 +0800 Hou Tao <[email protected]> wrote:
Not tried yet, but considering that VFS maintainer refused to have tracepoint in VFS layer, I'm not sure it is worth trying.
The reason they refuse to is because it shows data that can become an API. But if that data is just a pointer, then it's not possible to become an API no more than a RAW tracepoint that BPF can hook to.
Try asking.
Quoted 6 lines
Quoted 5 lines
That is, it only gives you a pointer to what is passed in, but does not give you anything else to form any API against it.
This way, not only does BPF have access to this information, so do the other tracers, through the new eprobe interface:Or in a opposite way can eprobe add support for bare tracepoint ?
If there's a way to expose the parameters of a bare tracepoint, then it should not be difficult to do so. Heck, we can just have "bare tracepoints" be events, that do nothing but repeat their pointer parameters, as that's basically all they do anyway. There's nothing fundamentally different between a "bare tracepoint" and a trace event that just defines the fields as the same as the parameters to a tracepoint, except now you have another way to know what the parameters are.
-- Steve
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 14 lines · On Mon, 4 Oct 2021 17:48:54 +0800 you wrote:
From: Hou Tao <[email protected]>
Hi,
The patchset series supports writable context for bare tracepoint.
The main idea comes from patchset "writable contexts for bpf raw tracepoints" [1], but it only supports normal tracepoint with associated trace event under tracefs. Now we have one use case in which we add bare tracepoint in VFS layer, and update
file::f_modefor specific files. The reason using bare tracepoint is that it doesn't form a ABI and we can change it freely. So add support for it in BPF.
Quoted 1 line
[...]
Here is the summary with links:
- [bpf-next,v5,1/3] bpf: support writable context for bare tracepoint
https://git.kernel.org/bpf/bpf-next/c/65223741ae1b - [bpf-next,v5,2/3] libbpf: support detecting and attaching of writable tracepoint program https://git.kernel.org/bpf/bpf-next/c/ccaf12d6215a - [bpf-next,v5,3/3] bpf/selftests: add test for writable bare tracepoint https://git.kernel.org/bpf/bpf-next/c/fa7f17d066bd
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html