Add support to directly attach BPF program to ftrace
Subsystems (10)
cover Add support to directly attach BPF program to ftrace
Hi,
These patches make it possible to attach BPF programs directly to tracepoints using ftrace (/sys/kernel/debug/tracing) without needing the process doing the attach to be alive. This has the following benefits:
1. Simplified Security: In Android, we have finer-grained security controls to specific ftrace trace events using SELinux labels. We control precisely who is allowed to enable an ftrace event already. By adding a node to ftrace for attaching BPF programs, we can use the same mechanism to further control who is allowed to attach to a trace event.
2. Process lifetime: In Android we are adding usecases where a tracing program needs to be attached all the time to a tracepoint, for the full life time of the system. Such as to gather statistics where there no need for a detach for the full system lifetime. With perf or bpf(2)'s BPF_RAW_TRACEPOINT_OPEN, this means keeping a process alive all the time. However, in Android our BPF loader currently (for hardeneded security) involves just starting a process at boot time, doing the BPF program loading, and then pinning them to /sys/fs/bpf. We don't keep this process alive all the time. It is more suitable to do a one-shot attach of the program using ftrace and not need to have a process alive all the time anymore for this. Such process also needs elevated privileges since tracepoint program loading currently requires CAP_SYS_ADMIN anyway so by design Android's bpfloader runs once at init and exits.
This series add a new bpf file to /sys/kernel/debug/tracing/events/X/Y/bpf The following commands can be written into it:
attach:<fd> Attaches BPF prog fd to tracepoint
detach:<fd> Detaches BPF prog fd to tracepoint
Reading the bpf file will show all the attached programs to the tracepoint.
Joel Fernandes (Google) (4):
Move bpf_raw_tracepoint functionality into bpf_trace.c trace/bpf: Add support for attach/detach of ftrace events to BPF lib/bpf: Add support for ftrace event attach and detach selftests/bpf: Add test for ftrace-based BPF attach/detach
include/linux/bpf_trace.h | 16 ++ include/linux/trace_events.h | 1 + kernel/bpf/syscall.c | 69 +----- kernel/trace/bpf_trace.c | 225 ++++++++++++++++++ kernel/trace/trace.h | 1 + kernel/trace/trace_events.c | 8 + tools/lib/bpf/bpf.c | 53 +++++ tools/lib/bpf/bpf.h | 4 + tools/lib/bpf/libbpf.map | 2 + .../raw_tp_writable_test_ftrace_run.c | 89 +++++++ 10 files changed, 410 insertions(+), 58 deletions(-)
create mode 100644 tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_ftrace_run.c
--
2.22.0.410.gd8fdbe21b5-goog
RFC PATCH 1/4 Move bpf_raw_tracepoint functionality into bpf_trace.c
In preparation to use raw tracepoints for BPF directly from ftrace, move the bpf_raw_tracepoint functionality into bpf_trace.c
--- include/linux/bpf_trace.h | 10 ++++++ kernel/bpf/syscall.c | 69 ++++++------------------------------- kernel/trace/bpf_trace.c | 56 ++++++++++++++++++++++++++++++ kernel/trace/trace_events.c | 3 ++ 4 files changed, 80 insertions(+), 58 deletions(-)
include/linux/bpf_trace.h +10 -0
@@ -4,4 +4,14 @@#include <trace/events/xdp.h> +#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd + +struct bpf_raw_tracepoint { +struct bpf_raw_event_map *btp; +struct bpf_prog *prog; +}; + +struct bpf_raw_tracepoint *bpf_raw_tracepoint_open (char *tp_name,int prog_fd); +void bpf_raw_tracepoint_close (struct bpf_raw_tracepoint *tp); +#endif /* __LINUX_BPF_TRACE_H__ */
kernel/bpf/syscall.c +11 -58
103 lines not on this page · raw message
kernel/trace/bpf_trace.c +56 -0
65 lines not on this page · raw message
kernel/trace/trace_events.c +3 -0
@@ -2017,6 +2017,9 @@ event_create_dir(struct dentry *parent, struct trace_event_file *file)trace_create_file ("trigger" ,0644 , file->dir, file, &event_trigger_fops); + +trace_create_file ("bpf_attach" ,0644 , file->dir, file, + &bpf_attach_trigger_fops); }#ifdef CONFIG_HIST_TRIGGERS
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
RFC PATCH 2/4 trace/bpf: Add support for attach/detach of ftrace events to BPF
In reply to Joel Fernandes (Google)Add a new bpf file to each trace event. The following commands can be written into it:
attach:<fd> Attaches BPF prog fd to tracepoint
detach:<fd> Detaches BPF prog fd to tracepoint
Reading the bpf file will show all the attached programs to the tracepoint.
--- include/linux/bpf_trace.h | 6 ++ include/linux/trace_events.h | 1 + kernel/trace/bpf_trace.c | 169 +++++++++++++++++++++++++++++++++++ kernel/trace/trace.h | 1 + kernel/trace/trace_events.c | 9 +- 5 files changed, 184 insertions(+), 2 deletions(-)
include/linux/bpf_trace.h +6 -0
@@ -9,6 +9,12 @@struct bpf_raw_tracepoint {struct bpf_raw_event_map *btp;struct bpf_prog *prog; +/* +* Multiple programs can be attached to a tracepoint, +* All of these are linked to each other and can be reached +* from the event's bpf_attach file in tracefs. +*/ +struct list_head event_attached; };struct bpf_raw_tracepoint *bpf_raw_tracepoint_open (char *tp_name,int prog_fd);
include/linux/trace_events.h +1 -0
@@ -371,6 +371,7 @@ struct trace_event_file {struct trace_array *tr;struct trace_subsystem_dir *system;struct list_head triggers; +struct list_head bpf_attached;/* * 32 bit flags:
kernel/trace/bpf_trace.c +169 -0
172 lines not on this page · raw message
kernel/trace/trace.h +1 -0
@@ -1582,6 +1582,7 @@ extern struct list_head ftrace_events;extern const struct file_operations event_trigger_fops;extern const struct file_operations event_hist_fops; +extern const struct file_operations event_bpf_attach_fops;#ifdef CONFIG_HIST_TRIGGERS extern int register_trigger_hist_cmd (void );
kernel/trace/trace_events.c +7 -2
@@ -2018,8 +2018,10 @@ event_create_dir(struct dentry *parent, struct trace_event_file *file)trace_create_file ("trigger" ,0644 , file->dir, file, &event_trigger_fops); -trace_create_file ("bpf_attach" ,0644 , file->dir, file, - &bpf_attach_trigger_fops); +#ifdef CONFIG_BPF_EVENTS +trace_create_file ("bpf" ,0644 , file->dir, file, + &event_bpf_attach_fops); +#endif }#ifdef CONFIG_HIST_TRIGGERS
@@ -2267,6 +2269,9 @@ trace_create_new_event(struct trace_event_call *call,atomic_set (&file->sm_ref,0 );atomic_set (&file->tm_ref,0 );INIT_LIST_HEAD (&file->triggers); +#ifdef CONFIG_BPF_EVENTS +INIT_LIST_HEAD (&file->bpf_attached); +#endif list_add (&file->list, &tr->events);return file;
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
RFC PATCH 3/4 lib/bpf: Add support for ftrace event attach and detach
In reply to Joel Fernandes (Google)Add the needed library support in this commit.
--- tools/lib/bpf/bpf.c | 53 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 4 +++ tools/lib/bpf/libbpf.map | 2 ++ 3 files changed, 59 insertions(+)
tools/lib/bpf/bpf.c +53 -0
@@ -24,6 +24,9 @@#include <stdlib.h> #include <string.h> #include <memory.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> #include <unistd.h> #include <asm/unistd.h> #include <linux/bpf.h>
@@ -57,6 +60,8 @@#define min (x, y) ((x) < (y) ? (x) : (y))#endif +#define TRACEFS "/sys/kernel/debug/tracing" +static inline __u64 ptr_to_u64 (const void *ptr) {return (__u64 ) (unsigned long ) ptr;
@@ -658,6 +663,54 @@ int bpf_raw_tracepoint_open(const char *name, int prog_fd)return sys_bpf (BPF_RAW_TRACEPOINT_OPEN , &attr,sizeof (attr)); } +int bpf_raw_tracepoint_ftrace_attach (const char *subsys,const char *name, +int prog_fd) +{ +char buf[256 ]; +int len, ret, tfd; + +sprintf (buf,"%s/events/%s/%s/bpf" ,TRACEFS , subsys, name); + tfd =open (buf,O_WRONLY ); +if (tfd <0 ) +return tfd; + +sprintf (buf,"attach:%d" , prog_fd); + len =strlen (buf); + ret =write (tfd, buf, len); + +if (ret <0 ) + goto err; +if (ret != len) + ret =-1 ; +err: +close (tfd); +return ret; +} + +int bpf_raw_tracepoint_ftrace_detach (const char *subsys,const char *name, +int prog_fd) +{ +char buf[256 ]; +int len, ret, tfd; + +sprintf (buf,"%s/events/%s/%s/bpf" ,TRACEFS , subsys, name); + tfd =open (buf,O_WRONLY ); +if (tfd <0 ) +return tfd; + +sprintf (buf,"detach:%d" , prog_fd); + len =strlen (buf); + ret =write (tfd, buf, len); + +if (ret <0 ) + goto err; +if (ret != len) + ret =-1 ; +err: +close (tfd); +return ret; +} +int bpf_load_btf (void *btf,__u32 btf_size,char *log_buf,__u32 log_buf_size,bool do_log) {
tools/lib/bpf/bpf.h +4 -0
@@ -163,6 +163,10 @@ LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags, __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt); LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd); +LIBBPF_API int bpf_raw_tracepoint_ftrace_attach(const char *subsys, +const char *name,int prog_fd); +LIBBPF_API int bpf_raw_tracepoint_ftrace_detach(const char *subsys, +const char *name,int prog_fd); LIBBPF_API int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf,__u32 log_buf_size,bool do_log); LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
tools/lib/bpf/libbpf.map +2 -0
@@ -163,4 +163,6 @@ LIBBPF_0.0.3 { bpf_map__is_internal; bpf_map_freeze; btf__finalize_data; + bpf_raw_tracepoint_ftrace_attach; + bpf_raw_tracepoint_ftrace_detach; } LIBBPF_0.0.2;
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
RFC PATCH 4/4 selftests/bpf: Add test for ftrace-based BPF attach/detach
In reply to Joel Fernandes (Google)Here we add support for testing the attach and detach of a BPF program to a tracepoint through tracefs.
--- .../raw_tp_writable_test_ftrace_run.c | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_ftrace_run.c
tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_ftrace_run.c new file +89 -0
@@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include <linux/nbd.h> + +void test_raw_tp_writable_test_ftrace_run (void ) +{ +__u32 duration =0 ; +char error[4096 ]; +int ret; + +const struct bpf_insn trace_program[] = { +BPF_LDX_MEM (BPF_DW ,BPF_REG_6 ,BPF_REG_1 ,0 ), +BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_6 ,0 ), +BPF_MOV64_IMM (BPF_REG_0 ,42 ), +BPF_STX_MEM (BPF_W ,BPF_REG_6 ,BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), + }; + +struct bpf_load_program_attr load_attr = { + .prog_type =BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE , + .license ="GPL v2" , + .insns = trace_program, + .insns_cnt =sizeof (trace_program) /sizeof (struct bpf_insn ), + .log_level =2 , + }; + +int bpf_fd =bpf_load_program_xattr (&load_attr, error,sizeof (error)); + +if (CHECK (bpf_fd <0 ,"bpf_raw_tracepoint_writable loaded" , +"failed: %d errno %d\n" , bpf_fd, errno)) +return ; + +const struct bpf_insn skb_program[] = { +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), + }; + +struct bpf_load_program_attr skb_load_attr = { + .prog_type =BPF_PROG_TYPE_SOCKET_FILTER , + .license ="GPL v2" , + .insns = skb_program, + .insns_cnt =sizeof (skb_program) /sizeof (struct bpf_insn ), + }; + +int filter_fd = +bpf_load_program_xattr (&skb_load_attr, error,sizeof (error)); +if (CHECK (filter_fd <0 ,"test_program_loaded" ,"failed: %d errno %d\n" , + filter_fd, errno)) + goto out_bpffd; + + ret =bpf_raw_tracepoint_ftrace_attach ("bpf_test_run" , +"bpf_test_finish" , + bpf_fd); +if (CHECK (ret <0 ,"bpf_raw_tracepoint_ftrace_attach" , +"failed: %d errno %d\n" , ret, errno)) + goto out_filterfd; + +char test_skb[128 ] = { +0 , + }; + +__u32 prog_ret; +int err =bpf_prog_test_run (filter_fd,1 , test_skb,sizeof (test_skb),0 , +0 , &prog_ret,0 ); +CHECK (err !=42 ,"test_run" , +"tracepoint did not modify return value\n" ); +CHECK (prog_ret !=0 ,"test_run_ret" , +"socket_filter did not return 0\n" ); + + ret =bpf_raw_tracepoint_ftrace_detach ("bpf_test_run" , +"bpf_test_finish" , + bpf_fd); +if (CHECK (ret <0 ,"bpf_raw_tracepoint_ftrace_detach" , +"failed: %d errno %d\n" , ret, errno)) + goto out_filterfd; + + err =bpf_prog_test_run (filter_fd,1 , test_skb,sizeof (test_skb),0 ,0 , + &prog_ret,0 ); +CHECK (err !=0 ,"test_run_notrace" , +"test_run failed with %d errno %d\n" , err, errno); +CHECK (prog_ret !=0 ,"test_run_ret_notrace" , +"socket_filter did not return 0\n" ); + +out_filterfd: +close (filter_fd); +out_bpffd: +close (bpf_fd); +}
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
Quoted 1 line · Joel Fernandes (Google)
Hi,
why are you cc-ing the whole world for this patch set?
I'll reply to all as well, but I suspect a bunch of folks consider it spam. Please read Documentation/bpf/bpf_devel_QA.rst
Also, I think, netdev@vger rejects emails with 80+ characters in cc as spam, so I'm not sure this set reached public mailing lists.
Quoted 23 lines
These patches make it possible to attach BPF programs directly to tracepoints using ftrace (
/sys/kernel/debug/tracing) without needing the process doing the attach to be alive. This has the following benefits:1. Simplified Security: In Android, we have finer-grained security controls to specific ftrace trace events using SELinux labels. We control precisely who is allowed to enable an ftrace event already. By adding a node to ftrace for attaching BPF programs, we can use the same mechanism to further control who is allowed to attach to a trace event.
2. Process lifetime: In Android we are adding usecases where a tracing program needs to be attached all the time to a tracepoint, for the full life time of the system. Such as to gather statistics where there no need for a detach for the full system lifetime. With perf or bpf(2)'s
BPF_RAW_TRACEPOINT_OPEN, this means keeping a process alive all the time. However, in Android our BPF loader currently (for hardeneded security) involves just starting a process at boot time, doing the BPF program loading, and then pinning them to/sys/fs/bpf. We don't keep this process alive all the time. It is more suitable to do a one-shot attach of the program using ftrace and not need to have a process alive all the time anymore for this. Such process also needs elevated privileges since tracepoint program loading currently requiresCAP_SYS_ADMINanyway so by design Android's bpfloader runs once at init and exits.
Quoted 4 lines
This series add a new bpf file to
/sys/kernel/debug/tracing/events/X/Y/bpfThe following commands can be written into it:
attach:<fd> Attaches BPF prog fd to tracepoint
detach:<fd> Detaches BPF prog fd to tracepoint
Looks like, to detach a program the user needs to read a text file, parse bpf prog id from text into binary. Then call fd_from_id bpf syscall, get a binary FD, convert it back to text and write as a text back into this file. I think this is just a single example why text based apis are not accepted in bpf anymore.
Through the patch set you call it ftrace. As far as I can see, this set has zero overlap with ftrace. There is no ftrace-bpf connection here at all that we discussed in the past Steven. It's all quite confusing.
I suggest android to solve sticky raw_tracepoint problem with user space deamon. The reasons, you point out why user daemon cannot be used, sound weak to me. Another acceptable solution would be to introduce pinning of raw_tp objects. bpf progs and maps can be pinned in bpffs already. Pinning raw_tp would be natural extension.
Quoted 4 lines · Alexei Starovoitov
Quoted 1 line · Joel Fernandes (Google)
Hi,
why are you cc-ing the whole world for this patch set?
Well, the whole world happens to be interested in BPF on Android.
Quoted 2 lines
I'll reply to all as well, but I suspect a bunch of folks consider it spam. Please read
Documentation/bpf/bpf_devel_QA.rst
Ok, I'll read it.
Quoted 2 lines
Also, I think, netdev@vger rejects emails with 80+ characters in cc as spam, so I'm not sure this set reached public mailing lists.
Certainly the CC list here is not added to folks who consider it spam. All the folks added have been interested in BPF on Android at various points of time. Is this CC list really that large? It has around 24 email addresses or so. I can trim it a bit if needed. Also, you sound like as if people are screaming at me to stop emailing them, certainly that's not the case and no one has told me it is spam.
And, it did reach the public archive btw:
https://lore.kernel.org/netdev/[email protected]/T/#m1460ba463b78312e38b68b8c118f673d2ead9446
Quoted 28 lines
Quoted 23 lines
These patches make it possible to attach BPF programs directly to tracepoints using ftrace (
/sys/kernel/debug/tracing) without needing the process doing the attach to be alive. This has the following benefits:1. Simplified Security: In Android, we have finer-grained security controls to specific ftrace trace events using SELinux labels. We control precisely who is allowed to enable an ftrace event already. By adding a node to ftrace for attaching BPF programs, we can use the same mechanism to further control who is allowed to attach to a trace event.
2. Process lifetime: In Android we are adding usecases where a tracing program needs to be attached all the time to a tracepoint, for the full life time of the system. Such as to gather statistics where there no need for a detach for the full system lifetime. With perf or bpf(2)'s
BPF_RAW_TRACEPOINT_OPEN, this means keeping a process alive all the time. However, in Android our BPF loader currently (for hardeneded security) involves just starting a process at boot time, doing the BPF program loading, and then pinning them to/sys/fs/bpf. We don't keep this process alive all the time. It is more suitable to do a one-shot attach of the program using ftrace and not need to have a process alive all the time anymore for this. Such process also needs elevated privileges since tracepoint program loading currently requiresCAP_SYS_ADMINanyway so by design Android's bpfloader runs once at init and exits.Quoted 4 lines
This series add a new bpf file to
/sys/kernel/debug/tracing/events/X/Y/bpfThe following commands can be written into it:
attach:<fd> Attaches BPF prog fd to tracepoint
detach:<fd> Detaches BPF prog fd to tracepoint
Quoted 5 lines
Looks like, to detach a program the user needs to read a text file, parse bpf prog id from text into binary. Then call
fd_from_idbpf syscall, get a binary FD, convert it back to text and write as a text back into this file. I think this is just a single example why text based apis are not accepted in bpf anymore.
This can also be considered a tracefs API.
And we can certainly change the detach to accept program ids as well if that's easier. 'detach:prog:<prog_id>' and 'detach:fd:<fd>'.
By the way, I can also list the set of cumbersome steps needed to attach a BPF program using perf and I bet it will be longer ;-)
Quoted 3 lines
Through the patch set you call it ftrace. As far as I can see, this set has zero overlap with ftrace. There is no ftrace-bpf connection here at all that we discussed in the past Steven. It's all quite confusing.
It depends on what you mean by ftrace, may be I can call it 'trace events' or something if it is less ambiguious. All of this has been collectively called ftrace before.
I am not sure if you you are making sense actually, trace_events mechanism is a part of ftrace. See the documentation: Documentation/trace/ftrace.rst. Even the documentation file name has the word ftrace in it.
I have also spoken to Steven before about this, I don't think he ever told me there is no connection so again I am a bit lost at your comments.
Quoted 2 lines
I suggest android to solve sticky
raw_tracepointproblem with user space deamon. The reasons, you point out why user daemon cannot be used, sound weak to me.
I don't think it is weak. It seems overkill to have a daemon for a trace event that is say supposed to be attached to all the time for the lifetime of the system. Why should there be a daemon consuming resources if it is active all the time?
In Android, we are very careful about spawning useless processes and leaving them alive for the lifetime of the system - for no good reason. Our security teams also don't like this, and they can comment more.
Quoted 3 lines
Another acceptable solution would be to introduce pinning of
raw_tpobjects. bpf progs and maps can be pinned in bpffs already. Pinningraw_tpwould be natural extension.
I don't think the pinning solves the security problem, it just solves the process lifetime problem. Currently, attaching trace events through perf requires CAP_SYS_ADMIN. However, with ftrace events, we already control security of events by labeling the nodes in tracefs and granting access to the labeled context through the selinux policies. Having a 'bpf' node in tracefs for events, and granting access to the labels is a natural extension.
I also thought about the pinning idea before, but we also want to add support for not just raw tracepoints, but also regular tracepoints (events if you will). I am hesitant to add a new BPF API just for creating regular tracepoints and then pinning those as well.
I don't see why a new bpf node for a trace event is a bad idea, really. tracefs is how we deal with trace events on Android. We do it in production systems. This is a natural extension to that and fits with the security model well.
thanks,
- Joel
Quoted 5 lines · Joel Fernandes
I also thought about the pinning idea before, but we also want to add support for not just raw tracepoints, but also regular tracepoints (events if you will). I am hesitant to add a new BPF API just for creating regular tracepoints and then pinning those as well.
and they should be done through the pinning as well.
Quoted 1 line
I don't see why a new bpf node for a trace event is a bad idea, really.
See the patches for kprobe/uprobe FD-based api and the reasons behind it.
tldr: text is racy, doesn't scale, poor security, etc.
Quoted 1 line
tracefs is how we deal with trace events on Android.
android has made mistakes in the past as well.
Quoted 2 lines
This is a natural extension to that and fits with the security model well.
I think it's the opposite.
I'm absolutely against text based apis.
Quoted 8 lines · Alexei Starovoitov
Quoted 5 lines · Joel Fernandes
I also thought about the pinning idea before, but we also want to add support for not just raw tracepoints, but also regular tracepoints (events if you will). I am hesitant to add a new BPF API just for creating regular tracepoints and then pinning those as well.
and they should be done through the pinning as well.
Hmm ok, I will give it some more thought.
Quoted 4 lines
Quoted 1 line
I don't see why a new bpf node for a trace event is a bad idea, really.
See the patches for kprobe/uprobe FD-based api and the reasons behind it.
tldr: text is racy, doesn't scale, poor security, etc.
Is it possible to use perf without CAP_SYS_ADMIN and control security at the per-event level? We are selective about who can access which event, using selinux. That's how our ftrace-based tracers work. Its fine grained per-event control. That's where I was going with the tracefs approach since we get that granularity using the file system.
Thanks.
Quoted 11 lines · Joel Fernandes
Quoted 8 lines · Alexei Starovoitov
Quoted 5 lines · Joel Fernandes
I also thought about the pinning idea before, but we also want to add support for not just raw tracepoints, but also regular tracepoints (events if you will). I am hesitant to add a new BPF API just for creating regular tracepoints and then pinning those as well.
and they should be done through the pinning as well.
Hmm ok, I will give it some more thought.
I think I can make the new BPF API + pinning approach work, I will try to work on something like this and post it soon.
Also, I had a question below if you don't mind taking a look:
thanks Alexei!
Quoted 13 lines
Quoted 4 lines
Quoted 1 line
I don't see why a new bpf node for a trace event is a bad idea, really.
See the patches for kprobe/uprobe FD-based api and the reasons behind it.
tldr: text is racy, doesn't scale, poor security, etc.Is it possible to use perf without
CAP_SYS_ADMINand control security at the per-event level? We are selective about who can access which event, using selinux. That's how our ftrace-based tracers work. Its fine grained per-event control. That's where I was going with the tracefs approach since we get that granularity using the file system.Thanks.
Quoted 11 lines · Joel Fernandes
Quoted 10 lines · Joel Fernandes
Quoted 8 lines · Alexei Starovoitov
Quoted 5 lines · Joel Fernandes
I also thought about the pinning idea before, but we also want to add support for not just raw tracepoints, but also regular tracepoints (events if you will). I am hesitant to add a new BPF API just for creating regular tracepoints and then pinning those as well.
and they should be done through the pinning as well.
Quoted 19 lines
Quoted 1 line
Hmm ok, I will give it some more thought.
I think I can make the new BPF API + pinning approach work, I will try to work on something like this and post it soon.
Also, I had a question below if you don't mind taking a look:
thanks Alexei!
Quoted 10 lines
Quoted 4 lines
Quoted 1 line
I don't see why a new bpf node for a trace event is a bad idea, really.
See the patches for kprobe/uprobe FD-based api and the reasons behind it.
tldr: text is racy, doesn't scale, poor security, etc.Is it possible to use perf without
CAP_SYS_ADMINand control security at the per-event level? We are selective about who can access which event, using selinux. That's how our ftrace-based tracers work. Its fine grained per-event control. That's where I was going with the tracefs approach since we get that granularity using the file system.
android's choice of selinux is not a factor in deciding kernel apis. It's completely separate discusion wether disallowing particular tracepoints for given user make sense at all.
Just because you can hack it in via selinux blocking particular /sys/debug/tracing/ directory and convince yourself that it's somehow makes android more secure. It doesn't mean that all new api should fit into this model.
I think allowing one tracepoint and disallowing another is pointless from security point of view. Tracing bpf program can do bpf_probe_read of anything.
On Tue, Jul 16, 2019 at 06:24:07PM -0700, Alexei Starovoitov wrote:
[snip]
Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
Quoted 1 line
I don't see why a new bpf node for a trace event is a bad idea, really.
See the patches for kprobe/uprobe FD-based api and the reasons behind it.
tldr: text is racy, doesn't scale, poor security, etc.Is it possible to use perf without
CAP_SYS_ADMINand control security at the per-event level? We are selective about who can access which event, using selinux. That's how our ftrace-based tracers work. Its fine grained per-event control. That's where I was going with the tracefs approach since we get that granularity using the file system.
Quoted 7 lines
android's choice of selinux is not a factor in deciding kernel apis. It's completely separate discusion wether disallowing particular tracepoints for given user make sense at all.
Just because you can hack it in via selinux blocking particular/sys/debug/tracing/directory and convince yourself that it's somehow makes android more secure. It doesn't mean that all new api should fit into this model.
Its not like a hack, it is just control of which tracefs node can be accessed and which cannot be since the tracing can run on production systems out in the field and there are several concerns to address like security, privacy etc. It is not just for debugging usecases. We do collect traces out in the field where these issues are real and cannot be ignored.
SELinux model is deny everything, and then selectively grant access to what is needed. The VFS and security LSM hooks provide this control quite well. I am not sure if such control is possible through perf hence I asked the question.
Quoted 3 lines
I think allowing one tracepoint and disallowing another is pointless from security point of view. Tracing bpf program can do
bpf_probe_readof anything.
I think the assumption here is the user controls the program instructions at runtime, but that's not the case. The BPF program we are loading is not dynamically generated, it is built at build time and it is loaded from a secure verified partition, so even though it can do bpf_probe_read, it is still not something that the user can change. And, we are planning to make it even more secure by making it kernel verify the program at load time as well (you were on some discussions about that a few months ago).
thanks,
- Joel
On Wed, Jul 17, 2019 at 6:01 AM Joel Fernandes <[email protected]> wrote:
I trimmed cc. some emails were bouncing.
Quoted 9 lines
Quoted 3 lines
I think allowing one tracepoint and disallowing another is pointless from security point of view. Tracing bpf program can do
bpf_probe_readof anything.I think the assumption here is the user controls the program instructions at runtime, but that's not the case. The BPF program we are loading is not dynamically generated, it is built at build time and it is loaded from a secure verified partition, so even though it can do
bpf_probe_read, it is still not something that the user can change.
so you're saying that by having a set of signed bpf programs which instructions are known to be non-malicious and allowed set of tracepoints to attach via selinux whitelist, such setup will be safe? Have you considered how mix and match will behave?
Quoted 3 lines
And, we are planning to make it
even more secure by making it kernel verify the program at load time as well (you were on some discussions about that a few months ago).
It sounds like api decisions for this sticky raw_tp feature are driven by security choices which are not actually secure. I'm suggesting to avoid bringing up point of security as a reason for this api design, since it's making the opposite effect.
Hi Alexei,
Quoted 3 lines · Alexei Starovoitov
On Wed, Jul 17, 2019 at 6:01 AM Joel Fernandes <[email protected]> wrote:
I trimmed cc. some emails were bouncing.
Ok, thanks.
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines
I think allowing one tracepoint and disallowing another is pointless from security point of view. Tracing bpf program can do
bpf_probe_readof anything.I think the assumption here is the user controls the program instructions at runtime, but that's not the case. The BPF program we are loading is not dynamically generated, it is built at build time and it is loaded from a secure verified partition, so even though it can do
bpf_probe_read, it is still not something that the user can change.
Quoted 4 lines
so you're saying that by having a set of signed bpf programs which instructions are known to be non-malicious and allowed set of tracepoints to attach via selinux whitelist, such setup will be safe? Have you considered how mix and match will behave?
Do you mean the effect of mixing tracepoints and programs? I have not considered this. I am Ok with further enforcing of this (only certain tracepoints can be attached to certain programs) if needed. What do you think? We could have a new bpf(2) syscall attribute specify which tracepoint is expected, or similar.
I wanted to walk you through our 2 usecases we are working on:
1. timeinstate: By hooking 2 programs onto sched_switch and cpu_frequency tracepoints, we are able to collect CPU power per-UID (specific app). Connor O'Brien is working on that.
2. inode to file path mapping: By hooking onto VFS tracepoints we are adding to the android kernels, we can collect data when the kernel resolves a file path to a inode/device number. A BPF map stores the inode/dev number (key) and the path (value). We have usecases where we need a high speed lookup of this without having to scan all the files in the filesystem.
For the first usecase, the BPF program will be loaded and attached to the scheduler and cpufreq tracepoints at boot time and will stay attached forever. This is why I was saying having a daemon to stay alive all the time is pointless. However, if since you are completely against using tracefs which it sounds like, then we can do a daemon that is always alive.
For the second usecase, the program attach is needed on-demand unlike the first usecase, and then after the usecase completes, it is detached to avoid overhead.
For the second usecase, privacy is important and we want the data to not be available to any process. So we want to make sure only selected processes can attach to that tracepoint. This is the reason why I was doing working on these patches which use the tracefs as well, since we get that level of control.
As you can see, I was trying to solve the sticky tracepoint problem in usecase 1 and the privacy problem in usecase 2.
I had some discussions today at office and we think we can use the daemon approach to solve both these problems as well which I think would make you happy as well.
What do you think about all of this? Any other feedback?
Quoted 8 lines
Quoted 3 lines
And, we are planning to make it
even more secure by making it kernel verify the program at load time as well (you were on some discussions about that a few months ago).It sounds like api decisions for this sticky
raw_tpfeature are driven by security choices which are not actually secure. I'm suggesting to avoid bringing up point of security as a reason for this api design, since it's making the opposite effect.
Ok, that's a fair point.
thanks,
- Joel
Quoted 24 lines · Joel Fernandes
Hi Alexei,
Quoted 3 lines · Alexei Starovoitov
On Wed, Jul 17, 2019 at 6:01 AM Joel Fernandes <[email protected]> wrote:
I trimmed cc. some emails were bouncing.
Ok, thanks.
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines
I think allowing one tracepoint and disallowing another is pointless from security point of view. Tracing bpf program can do
bpf_probe_readof anything.I think the assumption here is the user controls the program instructions at runtime, but that's not the case. The BPF program we are loading is not dynamically generated, it is built at build time and it is loaded from a secure verified partition, so even though it can do
bpf_probe_read, it is still not something that the user can change.Quoted 4 lines
so you're saying that by having a set of signed bpf programs which instructions are known to be non-malicious and allowed set of tracepoints to attach via selinux whitelist, such setup will be safe? Have you considered how mix and match will behave?
Quoted 7 lines
Do you mean the effect of mixing tracepoints and programs? I have not considered this. I am Ok with further enforcing of this (only certain tracepoints can be attached to certain programs) if needed. What do you think? We could have a new bpf(2) syscall attribute specify which tracepoint is expected, or similar.
I wanted to walk you through our 2 usecases we are working on:
thanks for sharing the use case details. Appreciate it.
Quoted 9 lines
1. timeinstate: By hooking 2 programs onto
sched_switchandcpu_frequencytracepoints, we are able to collect CPU power per-UID (specific app). Connor O'Brien is working on that.2. inode to file path mapping: By hooking onto VFS tracepoints we are adding to the android kernels, we can collect data when the kernel resolves a file path to a inode/device number. A BPF map stores the inode/dev number (key) and the path (value). We have usecases where we need a high speed lookup of this without having to scan all the files in the filesystem.
Can you share the link to vfs tracepoints you're adding? Sounds like you're not going to attempt to upstream them knowing Al's stance towards them?
May be there is a way we can do the feature you need, but w/o tracepoints?
Quoted 5 lines
For the first usecase, the BPF program will be loaded and attached to the scheduler and cpufreq tracepoints at boot time and will stay attached forever. This is why I was saying having a daemon to stay alive all the time is pointless. However, if since you are completely against using tracefs which it sounds like, then we can do a daemon that is always alive.
As I said earlier this use case can be solved by pinning raw_tp object into bpffs. Such patches are welcomed.
Quoted 9 lines
For the second usecase, the program attach is needed on-demand unlike the first usecase, and then after the usecase completes, it is detached to avoid overhead.
For the second usecase, privacy is important and we want the data to not be available to any process. So we want to make sure only selected processes can attach to that tracepoint. This is the reason why I was doing working on these patches which use the tracefs as well, since we get that level of control.
It's hard to recommend anything w/o seeing the actual tracepoints you're adding to vfs and type of data bpf program extracts from there.
Sounds like it's some sort of cache of inode->file name ?
If so, why is it privacy related?
Quoted 21 lines · Alexei Starovoitov
Quoted 21 lines
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines
I think allowing one tracepoint and disallowing another is pointless from security point of view. Tracing bpf program can do
bpf_probe_readof anything.I think the assumption here is the user controls the program instructions at runtime, but that's not the case. The BPF program we are loading is not dynamically generated, it is built at build time and it is loaded from a secure verified partition, so even though it can do
bpf_probe_read, it is still not something that the user can change.Quoted 4 lines
so you're saying that by having a set of signed bpf programs which instructions are known to be non-malicious and allowed set of tracepoints to attach via selinux whitelist, such setup will be safe? Have you considered how mix and match will behave?
Do you mean the effect of mixing tracepoints and programs? I have not considered this. I am Ok with further enforcing of this (only certain tracepoints can be attached to certain programs) if needed. What do you think? We could have a new bpf(2) syscall attribute specify which tracepoint is expected, or similar.
Quoted 3 lines
Quoted 1 line
I wanted to walk you through our 2 usecases we are working on:
thanks for sharing the use case details. Appreciate it.
No problem and thanks for your thoughts.
Quoted 10 lines
Quoted 9 lines
1. timeinstate: By hooking 2 programs onto
sched_switchandcpu_frequencytracepoints, we are able to collect CPU power per-UID (specific app). Connor O'Brien is working on that.2. inode to file path mapping: By hooking onto VFS tracepoints we are adding to the android kernels, we can collect data when the kernel resolves a file path to a inode/device number. A BPF map stores the inode/dev number (key) and the path (value). We have usecases where we need a high speed lookup of this without having to scan all the files in the filesystem.
Quoted 4 lines
Can you share the link to vfs tracepoints you're adding? Sounds like you're not going to attempt to upstream them knowing Al's stance towards them?
May be there is a way we can do the feature you need, but w/o tracepoints?
Yes, given Al's stance I understand the patch is not upstreamable. The patch is here:
For tracepoint:
https://android.googlesource.com/kernel/common/+/27d3bfe20558d279041af403a887e7bdbdcc6f24%5E%21/
For bpf program:
https://android.googlesource.com/platform/system/bpfprogs/+/908f6cd718fab0de7a944f84628c56f292efeb17%5E%21/
I intended to submit the tracepoint only for the Android kernels, however if there is an upstream solution to this then that's even better since upstream can benefit. Were you thinking of a BPF helper function to get this data?
Quoted 9 lines
Quoted 6 lines
For the first usecase, the BPF program will be loaded and attached to the scheduler and cpufreq tracepoints at boot time and will stay attached forever. This is why I was saying having a daemon to stay alive all the time is pointless. However, if since you are completely against using tracefs which it sounds like, then we can do a daemon that is always alive.
As I said earlier this use case can be solved by pinning
raw_tpobject into bpffs. Such patches are welcomed.
Ok will think more about it.
Quoted 10 lines
Quoted 9 lines
For the second usecase, the program attach is needed on-demand unlike the first usecase, and then after the usecase completes, it is detached to avoid overhead.
For the second usecase, privacy is important and we want the data to not be available to any process. So we want to make sure only selected processes can attach to that tracepoint. This is the reason why I was doing working on these patches which use the tracefs as well, since we get that level of control.
Quoted 3 lines
It's hard to recommend anything w/o seeing the actual tracepoints you're adding to vfs and type of data bpf program extracts from there.
Sounds like it's some sort of cache ofinode->filename ?
Yes, that's what it is.
Quoted 1 line
If so, why is it privacy related?
The reasoning is the file paths could reveal user activity (such as an app that opens a document) and Android has requirements to control/restrict that.
thanks,
- Joel
Quoted 43 lines · Joel Fernandes
Quoted 21 lines · Alexei Starovoitov
Quoted 21 lines
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines
I think allowing one tracepoint and disallowing another is pointless from security point of view. Tracing bpf program can do
bpf_probe_readof anything.I think the assumption here is the user controls the program instructions at runtime, but that's not the case. The BPF program we are loading is not dynamically generated, it is built at build time and it is loaded from a secure verified partition, so even though it can do
bpf_probe_read, it is still not something that the user can change.Quoted 4 lines
so you're saying that by having a set of signed bpf programs which instructions are known to be non-malicious and allowed set of tracepoints to attach via selinux whitelist, such setup will be safe? Have you considered how mix and match will behave?
Do you mean the effect of mixing tracepoints and programs? I have not considered this. I am Ok with further enforcing of this (only certain tracepoints can be attached to certain programs) if needed. What do you think? We could have a new bpf(2) syscall attribute specify which tracepoint is expected, or similar.
Quoted 3 lines
Quoted 1 line
I wanted to walk you through our 2 usecases we are working on:
thanks for sharing the use case details. Appreciate it.
No problem and thanks for your thoughts.
Quoted 10 lines
Quoted 9 lines
1. timeinstate: By hooking 2 programs onto
sched_switchandcpu_frequencytracepoints, we are able to collect CPU power per-UID (specific app). Connor O'Brien is working on that.2. inode to file path mapping: By hooking onto VFS tracepoints we are adding to the android kernels, we can collect data when the kernel resolves a file path to a inode/device number. A BPF map stores the inode/dev number (key) and the path (value). We have usecases where we need a high speed lookup of this without having to scan all the files in the filesystem.
Quoted 4 lines
Can you share the link to vfs tracepoints you're adding? Sounds like you're not going to attempt to upstream them knowing Al's stance towards them?
May be there is a way we can do the feature you need, but w/o tracepoints?
Quoted 4 lines
Yes, given Al's stance I understand the patch is not upstreamable. The patch is here:
For tracepoint:
https://android.googlesource.com/kernel/common/+/27d3bfe20558d279041af403a887e7bdbdcc6f24%5E%21/
this is way more than tracepoint.
Quoted 2 lines
For bpf program:
https://android.googlesource.com/platform/system/bpfprogs/+/908f6cd718fab0de7a944f84628c56f292efeb17%5E%21/
what is unsafe_bpf_map_update_elem() in there? The verifier comment sounds odd.
Could you describe the issue you see with the verifier?
Quoted 3 lines
I intended to submit the tracepoint only for the Android kernels, however if there is an upstream solution to this then that's even better since upstream can benefit. Were you thinking of a BPF helper function to get this data?
I think the best way to evaluate the patches is whether they are upstreamable or not. If they're not (like this case), it means that there is something wrong with their design and if android decides to go with such approach it will only create serious issues long term. Starting with the whole idea of dev+inode -> filepath cache.
dev+inode is not a unique identifier of the file.
In some filesystems two different files may have the same ino integer value. Have you looked at 'struct file_handle' ? and name_to_handle_at ?
I think fhandle is the only way to get unique identifier of the file.
Could you please share more details why android needs this cache of dev+ino->path? I guess something uses ino to find paths?
Sort of faster version of 'find -inum' ?
On Fri, Jul 26, 2019 at 11:39:56AM -0700, Alexei Starovoitov wrote:
[snip]
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 9 lines
1. timeinstate: By hooking 2 programs onto
sched_switchandcpu_frequencytracepoints, we are able to collect CPU power per-UID (specific app). Connor O'Brien is working on that.2. inode to file path mapping: By hooking onto VFS tracepoints we are adding to the android kernels, we can collect data when the kernel resolves a file path to a inode/device number. A BPF map stores the inode/dev number (key) and the path (value). We have usecases where we need a high speed lookup of this without having to scan all the files in the filesystem.
Can you share the link to vfs tracepoints you're adding? Sounds like you're not going to attempt to upstream them knowing Al's stance towards them?
May be there is a way we can do the feature you need, but w/o tracepoints?
Quoted 6 lines
Quoted 4 lines
Yes, given Al's stance I understand the patch is not upstreamable. The patch is here:
For tracepoint:
https://android.googlesource.com/kernel/common/+/27d3bfe20558d279041af403a887e7bdbdcc6f24%5E%21/this is way more than tracepoint.
True there is some code that calls the tracepoint. I want to optimize it more but lets see I am ready to think more about it before doing it this way, based on your suggestions.
Quoted 6 lines
Quoted 2 lines
For bpf program:
https://android.googlesource.com/platform/system/bpfprogs/+/908f6cd718fab0de7a944f84628c56f292efeb17%5E%21/what is
unsafe_bpf_map_update_elem()in there? The verifier comment sounds odd.
Could you describe the issue you see with the verifier?
Will dig out the verifier issue I was seeing. I was just trying to get a prototype working so I did not go into verifier details much.
Quoted 4 lines
Quoted 3 lines
I intended to submit the tracepoint only for the Android kernels, however if there is an upstream solution to this then that's even better since upstream can benefit. Were you thinking of a BPF helper function to get this data?
Quoted 9 lines
I think the best way to evaluate the patches is whether they are upstreamable or not. If they're not (like this case), it means that there is something wrong with their design and if android decides to go with such approach it will only create serious issues long term. Starting with the whole idea of dev+inode -> filepath cache.
dev+inode is not a unique identifier of the file.
In some filesystems two different files may have the same ino integer value. Have you looked at 'structfile_handle' ? andname_to_handle_at?
I think fhandle is the only way to get unique identifier of the file.
Could you please share more details why android needs this cache of dev+ino->path?
I will follow-up with you on this by email off the list, thanks.
thanks,
- Joel
Quoted 2 lines · Joel Fernandes
On Fri, Jul 26, 2019 at 11:39:56AM -0700, Alexei Starovoitov wrote:
[snip]
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
For bpf program:
https://android.googlesource.com/platform/system/bpfprogs/+/908f6cd718fab0de7a944f84628c56f292efeb17%5E%21/what is
unsafe_bpf_map_update_elem()in there? The verifier comment sounds odd.
Could you describe the issue you see with the verifier?Will dig out the verifier issue I was seeing. I was just trying to get a prototype working so I did not go into verifier details much.
This is actually slightly old code, the actual function name is bpf_map_update_elem_unsafe() .
https://android.googlesource.com/platform/system/bpf/+/refs/heads/master/progs/include/bpf_helpers.h#39
This function came about because someone added a DEFINE_BPF_MAP macro which defines BPF map accessors based on the type of the key and value. So that's the "safe" variant:
https://android.googlesource.com/platform/system/bpf/+/refs/heads/master/progs/include/bpf_helpers.h#54
(added in commit
https://android.googlesource.com/platform/system/bpf/+/6564b8eac46fc27dde807a39856386d98d2471c3)
So the "safe" variant of the bpf_map_update_elem for us became a map specific version with a prototype:
static inline __always_inline __unused int bpf_##the_map##_update_elem(TypeOfKey* k, TypeOfValue* v, unsigned long long flags)
Since I had not upgraded my BPF program to the "safe" variant, I had to use the internal "unsafe" variant of the API (if that makes sense..).
thanks Alexei!
- Joel
Steven Rostedt
maintainerQuoted 1 line · On Tue, 16 Jul 2019 15:26:52 -0700 Alexei Starovoitov…
I'm absolutely against text based apis.
I guess you don't use /proc ;-)
-- Steve
Steven Rostedt
maintainerQuoted 4 lines · On Tue, 16 Jul 2019 17:30:50 -0400 Joel Fernandes <[email protected]>…
I don't see why a new bpf node for a trace event is a bad idea, really. tracefs is how we deal with trace events on Android. We do it in production systems. This is a natural extension to that and fits with the security model well.
What I would like to see is a way to have BPF inject data into the ftrace ring buffer directly. There's a bpf_trace_printk() that I find a bit of a hack (especially since it hooks into trace_printk() which is only for debugging purposes). Have a dedicated bpf ftrace ring buffer event that can be triggered is what I am looking for. Then comes the issue of what ring buffer to place it in, as ftrace can have multiple ring buffer instances. But these instances are defined by the tracefs instances directory. Having a way to associate a bpf program to a specific event in a specific tracefs directory could allow for ways to trigger writing into the correct ftrace buffer.
But looking over the patches, I see what Alexei means that there's no overlap with ftrace and these patches except for the tracefs directory itself (which is part of the ftrace infrastructure). And the trace events are technically part of the ftrace infrastructure too. I see the tracefs interface being used, but I don't see how the bpf programs being added affect the ftrace ring buffer or other parts of ftrace. And I'm guessing that's what is confusing Alexei.
-- Steve
Quoted 8 lines · Steven Rostedt
Quoted 4 lines · On Tue, 16 Jul 2019 17:30:50 -0400 Joel Fernandes <[email protected]>…
I don't see why a new bpf node for a trace event is a bad idea, really. tracefs is how we deal with trace events on Android. We do it in production systems. This is a natural extension to that and fits with the security model well.
Quoted 10 lines
What I would like to see is a way to have BPF inject data into the ftrace ring buffer directly. There's a
bpf_trace_printk()that I find a bit of a hack (especially since it hooks intotrace_printk()which is only for debugging purposes). Have a dedicated bpf ftrace ring buffer event that can be triggered is what I am looking for. Then comes the issue of what ring buffer to place it in, as ftrace can have multiple ring buffer instances. But these instances are defined by the tracefs instances directory. Having a way to associate a bpf program to a specific event in a specific tracefs directory could allow for ways to trigger writing into the correct ftrace buffer.
But his problem is with doing the association of a BPF program with tracefs itself. How would you attach a BPF program with tracefs without doing a text based approach? His problem is with the text based approach per his last email.
Quoted 7 lines
But looking over the patches, I see what Alexei means that there's no overlap with ftrace and these patches except for the tracefs directory itself (which is part of the ftrace infrastructure). And the trace events are technically part of the ftrace infrastructure too. I see the tracefs interface being used, but I don't see how the bpf programs being added affect the ftrace ring buffer or other parts of ftrace. And I'm guessing that's what is confusing Alexei.
In a follow-up patch which I am still writing, I am using the trace ring buffer as temporary storage since I am formatting the trace event into it. This patch you are replying to is just for raw tracepoint and yes, I agree this one does not use the ring buffer, but a future addition to it does. So I don't think the association of this patch series with ftrace is going to be an issue IMO.
thanks,
- Joel
Quoted 8 lines · Steven Rostedt
Quoted 4 lines · On Tue, 16 Jul 2019 17:30:50 -0400 Joel Fernandes <[email protected]>…
I don't see why a new bpf node for a trace event is a bad idea, really. tracefs is how we deal with trace events on Android. We do it in production systems. This is a natural extension to that and fits with the security model well.
Quoted 10 lines
What I would like to see is a way to have BPF inject data into the ftrace ring buffer directly. There's a
bpf_trace_printk()that I find a bit of a hack (especially since it hooks intotrace_printk()which is only for debugging purposes). Have a dedicated bpf ftrace ring buffer event that can be triggered is what I am looking for. Then comes the issue of what ring buffer to place it in, as ftrace can have multiple ring buffer instances. But these instances are defined by the tracefs instances directory. Having a way to associate a bpf program to a specific event in a specific tracefs directory could allow for ways to trigger writing into the correct ftrace buffer.
bpf can write anything (including full skb) into perf ring buffer. I don't think there is a use case yet to write into ftrace ring buffer. But I can be convinced otherwise :)
Quoted 7 lines
But looking over the patches, I see what Alexei means that there's no overlap with ftrace and these patches except for the tracefs directory itself (which is part of the ftrace infrastructure). And the trace events are technically part of the ftrace infrastructure too. I see the tracefs interface being used, but I don't see how the bpf programs being added affect the ftrace ring buffer or other parts of ftrace. And I'm guessing that's what is confusing Alexei.
yep.
What I really like to see some day is proper integration of real ftrace and bpf. So that bpf progs can be invoked from some of the ftrace machinery. And the other way around.
Like I'd love to be able to start ftracing all functions from bpf and stop it from bpf.
The use case is kernel debugging. I'd like to examine a bunch of condition and start ftracing the execution. Then later decide wether this collection of ip addresses is interesting to analyze and post process it quickly while still inside bpf program.