tracing: attach eBPF programs to tracepoints/syscalls/kprobe
Subsystems (8)
cover tracing: attach eBPF programs to tracepoints/syscalls/kprobe
Hi Ingo, Steven,
This patch set is based on tip/master.
It adds ability to attach eBPF programs to tracepoints, syscalls and kprobes.
Mechanism of attaching:
- load program via bpf() syscall and receive program_fd
- event_fd = open("/sys/kernel/debug/tracing/events/.../filter")
- write 'bpf-123' to event_fd where 123 is program_fd
- program will be attached to particular event and event automatically enabled
- close(event_fd) will detach bpf program from event and event disabled
Program attach point and input arguments:
- programs attached to kprobes receive 'struct pt_regs *' as an input. See tracex4_kern.c that demonstrates how users can write a C program like:
SEC("events/kprobes/sys_write")
int bpf_prog4(struct pt_regs *regs)
{
long write_size = regs->dx;
// here user need to know the proto of sys_write() from kernel
// sources and x64 calling convention to know that register $rdx
// contains 3rd argument to sys_write() which is 'size_t count'it's obviously architecture dependent, but allows building sophisticated user tools on top, that can see from debug info of vmlinux which variables are in which registers or stack locations and fetch it from there. 'perf probe' can potentialy use this hook to generate programs in user space and insert them instead of letting kernel parse string during kprobe creation.
- programs attached to tracepoints and syscalls receive 'struct bpf_context *':
u64 arg1, arg2, ..., arg6;
for syscalls they match syscall arguments.
for tracepoints these args match arguments passed to tracepoint. For example:
trace_sched_migrate_task(p, new_cpu); from sched/core.c
arg1 <- p which is 'struct task_struct *'
arg2 <- new_cpu which is 'unsigned int'
arg3..arg6 = 0
the program can use bpf_fetch_u8/16/32/64/ptr() helpers to walk 'task_struct' or any other kernel data structures.
These helpers are using probe_kernel_read() similar to 'perf probe' which is not 100% safe in both cases, but good enough.
To access task_struct's pid inside 'sched_migrate_task' tracepoint the program can do:
struct task_struct *task = (struct task_struct *)ctx->arg1;
u32 pid = bpf_fetch_u32(&task->pid);
Since struct layout is kernel configuration specific such programs are not portable and require access to kernel headers to be compiled, but in this case we don't need debug info.
llvm with bpf backend will statically compute task->pid offset as a constant based on kernel headers only.
The example of this arbitrary pointer walking is tracex1_kern.c which does skb->dev->name == "lo" filtering.
In all cases the programs are called before trace buffer is allocated to minimize the overhead, since we want to filter huge number of events, but buffer alloc/free and argument copy for every event is too costly. Theoretically we can invoke programs after buffer is allocated, but it doesn't seem needed, since above approach is faster and achieves the same.
Note, tracepoint/syscall and kprobe programs are two different types:BPF_PROG_TYPE_TRACING_FILTER and BPF_PROG_TYPE_KPROBE_FILTER, since they expect different input.
Both use the same set of helper functions:
- map access (lookup/update/delete)
- fetch (probe_kernel_read wrappers)
- memcmp (probe_kernel_read + memcmp)
- dump_stack
- trace_printk
The last two are mainly to debug the programs and to print data for user space consumptions.
Portability:
- kprobe programs are architecture dependent and need user scripting language like ktap/stap/dtrace/perf that will dynamically generate them based on debug info in vmlinux
- tracepoint programs are architecture independent, but if arbitrary pointer walking (with fetch() helpers) is used, they need data struct layout to match. Debug info is not necessary
- for networking use case we need to access 'struct sk_buff' fields in portable way (user space needs to fetch packet length without knowing skb->len offset), so for some frequently used data structures we will add helper functions or pseudo instructions to access them. I've hacked few ways specifically for skb, but abandoned them in favor of more generic type/field infra. That work is still wip. Not part of this set.
Once it's ready tracepoint programs that access common data structs will be kernel independent.
Program return value:
- programs return 0 to discard an event
- and return non-zero to proceed with event (allocate trace buffer, copy arguments there and print it eventually in trace_pipe in traditional way)
Examples:
- dropmon.c - simple kfree_skb() accounting in eBPF assembler, similar to dropmon tool
- tracex1_kern.c - does net/netif_receive_skb event filtering for dev->skb->name == "lo" condition
- tracex2_kern.c - same kfree_skb() accounting like dropmon, but now in C plus computes histogram of all write sizes from sys_write syscall and prints the histogram in userspace
- tracex3_kern.c - most sophisticated example that computes IO latency between block/block_rq_issue and block/block_rq_complete events and prints 'heatmap' using gray shades of text terminal. Useful to analyze disk performance.
- tracex4_kern.c - computes histogram of write sizes from sys_write syscall using kprobe mechanism instead of syscall. Since kprobe is optimized into ftrace the overhead of instrumentation is smaller than in example 2.
The user space tools like ktap/dtrace/systemptap/perf that has access to debug info would probably want to use kprobe attachment point, since kprobe can be inserted anywhere and all registers are avaiable in the program. tracepoint attachments are useful without debug info, so standalone tools like iosnoop will use them.
The main difference vs existing perf_probe/ftrace infra is in kernel aggregation and conditional walking of arbitrary data structures.
Thanks!
Alexei Starovoitov (9):
tracing: attach eBPF programs to tracepoints and syscalls
tracing: allow eBPF programs to call bpf_printk()
tracing: allow eBPF programs to call ktime_get_ns()
samples: bpf: simple tracing example in eBPF assembler
samples: bpf: simple tracing example in C
samples: bpf: counting example for kfree_skb tracepoint and write
syscall
samples: bpf: IO latency analysis (iosnoop/heatmap)
tracing: attach eBPF programs to kprobe/kretprobe
samples: bpf: simple kprobe example
include/linux/ftrace_event.h | 6 + include/trace/bpf_trace.h | 25 ++++ include/trace/ftrace.h | 30 +++++ include/uapi/linux/bpf.h | 11 ++ kernel/trace/Kconfig | 1 + kernel/trace/Makefile | 1 + kernel/trace/bpf_trace.c | 250 ++++++++++++++++++++++++++++++++++++ kernel/trace/trace.h | 3 + kernel/trace/trace_events.c | 41 +++++- kernel/trace/trace_events_filter.c | 80 +++++++++++- kernel/trace/trace_kprobe.c | 11 +- kernel/trace/trace_syscalls.c | 31 +++++ samples/bpf/Makefile | 18 +++ samples/bpf/bpf_helpers.h | 18 +++ samples/bpf/bpf_load.c | 62 ++++++++- samples/bpf/bpf_load.h | 3 + samples/bpf/dropmon.c | 129 +++++++++++++++++++ samples/bpf/tracex1_kern.c | 28 ++++ samples/bpf/tracex1_user.c | 24 ++++ samples/bpf/tracex2_kern.c | 71 ++++++++++ samples/bpf/tracex2_user.c | 95 ++++++++++++++ samples/bpf/tracex3_kern.c | 96 ++++++++++++++ samples/bpf/tracex3_user.c | 146 +++++++++++++++++++++ samples/bpf/tracex4_kern.c | 36 ++++++ samples/bpf/tracex4_user.c | 83 ++++++++++++ 25 files changed, 1290 insertions(+), 9 deletions(-) create mode 100644 include/trace/bpf_trace.h create mode 100644 kernel/trace/bpf_trace.c create mode 100644 samples/bpf/dropmon.c create mode 100644 samples/bpf/tracex1_kern.c create mode 100644 samples/bpf/tracex1_user.c create mode 100644 samples/bpf/tracex2_kern.c create mode 100644 samples/bpf/tracex2_user.c create mode 100644 samples/bpf/tracex3_kern.c create mode 100644 samples/bpf/tracex3_user.c create mode 100644 samples/bpf/tracex4_kern.c create mode 100644 samples/bpf/tracex4_user.c
Signature · 1 line
1.7.9.5
PATCH 1/9 tracing: attach eBPF programs to tracepoints and syscalls
User interface:
fd = open("/sys/kernel/debug/tracing/__event__/filter")
write(fd, "bpf_123")
where 123 is process local FD associated with eBPF program previously loaded. __event__ is static tracepoint event or syscall.
(kprobe support is in next patch)
Once program is successfully attached to tracepoint event, the tracepoint will be auto-enabled
close(fd)
auto-disables tracepoint event and detaches eBPF program from it
eBPF programs can call in-kernel helper functions to:
- lookup/update/delete elements in maps
- memcmp
- dump_stack
- fetch_ptr/u64/u32/u16/u8 values from unsafe address via probe_kernel_read(), so that eBPF program can walk any kernel data structures
--- include/linux/ftrace_event.h | 4 ++ include/trace/bpf_trace.h | 25 +++++++ include/trace/ftrace.h | 30 ++++++++ include/uapi/linux/bpf.h | 8 +++ kernel/trace/Kconfig | 1 + kernel/trace/Makefile | 1 + kernel/trace/bpf_trace.c | 140 ++++++++++++++++++++++++++++++++++++ kernel/trace/trace.h | 3 + kernel/trace/trace_events.c | 33 ++++++++- kernel/trace/trace_events_filter.c | 76 +++++++++++++++++++- kernel/trace/trace_syscalls.c | 31 ++++++++ 11 files changed, 350 insertions(+), 2 deletions(-) create mode 100644 include/trace/bpf_trace.h create mode 100644 kernel/trace/bpf_trace.c
include/linux/ftrace_event.h +4 -0
@@ -248,6 +248,7 @@ enum {TRACE_EVENT_FL_WAS_ENABLED_BIT ,TRACE_EVENT_FL_USE_CALL_FILTER_BIT ,TRACE_EVENT_FL_TRACEPOINT_BIT , + TRACE_EVENT_FL_BPF_BIT, };/*
@@ -270,6 +271,7 @@ enum {TRACE_EVENT_FL_WAS_ENABLED = (1 <<TRACE_EVENT_FL_WAS_ENABLED_BIT ),TRACE_EVENT_FL_USE_CALL_FILTER = (1 <<TRACE_EVENT_FL_USE_CALL_FILTER_BIT ),TRACE_EVENT_FL_TRACEPOINT = (1 <<TRACE_EVENT_FL_TRACEPOINT_BIT ), + TRACE_EVENT_FL_BPF = (1 << TRACE_EVENT_FL_BPF_BIT), }; struct ftrace_event_call {
@@ -544,6 +546,8 @@ event_trigger_unlock_commit_regs(struct ftrace_event_file *file,event_triggers_post_call (file, tt); } +unsigned int trace_filter_call_bpf (struct event_filter *filter,void *ctx); +enum {FILTER_OTHER =0 ,FILTER_STATIC_STRING ,
include/trace/bpf_trace.h new file +25 -0
@@ -0,0 +1,25 @@ +/* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of version 2 of the GNU General Public +* License as published by the Free Software Foundation. +*/ +#ifndef _LINUX_KERNEL_BPF_TRACE_H +#define _LINUX_KERNEL_BPF_TRACE_H + +/* For tracepoint filters argN fields match one to one to arguments +* passed to tracepoint events +* +* For syscall entry filters argN fields match syscall arguments +* For syscall exit filters arg1 is a return value +*/ +struct bpf_context { +u64 arg1; +u64 arg2; +u64 arg3; +u64 arg4; +u64 arg5; +u64 arg6; +}; + +#endif /* _LINUX_KERNEL_BPF_TRACE_H */
include/trace/ftrace.h +30 -0
@@ -17,6 +17,7 @@*/ #include <linux/ftrace_event.h> +#include <trace/bpf_trace.h> /* * DECLARE_EVENT_CLASS can be used to add a generic function
@@ -617,6 +618,24 @@ static inline notrace int ftrace_get_offsets_##call( \#undef __perf_task#define __perf_task (t) (t) +/* zero extend integer, pointer or aggregate type to u64 without warnings */ +#define __CAST_TO_U64 (expr) ({ \ + u64 ret = 0; \ + switch (sizeof(expr)) { \ + case 8: ret = *(u64 *) &expr; break; \ + case 4: ret = *(u32 *) &expr; break; \ + case 2: ret = *(u16 *) &expr; break; \ + case 1: ret = *(u8 *) &expr; break; \ + } \ + ret; }) + +#define __BPF_CAST1 (a,...) __CAST_TO_U64(a) +#define __BPF_CAST2 (a,...) __CAST_TO_U64(a), __BPF_CAST1(__VA_ARGS__) +#define __BPF_CAST3 (a,...) __CAST_TO_U64(a), __BPF_CAST2(__VA_ARGS__) +#define __BPF_CAST4 (a,...) __CAST_TO_U64(a), __BPF_CAST3(__VA_ARGS__) +#define __BPF_CAST5 (a,...) __CAST_TO_U64(a), __BPF_CAST4(__VA_ARGS__) +#define __BPF_CAST6 (a,...) __CAST_TO_U64(a), __BPF_CAST5(__VA_ARGS__) +#undef DECLARE_EVENT_CLASS#define DECLARE_EVENT_CLASS (call, proto, args, tstruct, assign, print) \ \
@@ -632,6 +651,17 @@ ftrace_raw_event_##call(void *__data, proto) \if (ftrace_trigger_soft_disabled (ftrace_file)) \return ; \ \ +if (unlikely (ftrace_file->flags &FTRACE_EVENT_FL_FILTERED ) && \ +unlikely (ftrace_file->flags &TRACE_EVENT_FL_BPF )) { \ + __maybe_unused const u64 z = 0; \ +struct bpf_context __ctx = ((struct bpf_context ) { \ +__BPF_CAST6 (args, z, z, z, z, z) \ + }); \ + \ +if (trace_filter_call_bpf (ftrace_file->filter, &__ctx) ==0 ) \ +return ; \ + } \ + \ __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \ \ entry = ftrace_event_buffer_reserve(&fbuffer, ftrace_file, \
include/uapi/linux/bpf.h +8 -0
@@ -118,6 +118,7 @@ enum bpf_map_type {enum bpf_prog_type {BPF_PROG_TYPE_UNSPEC ,BPF_PROG_TYPE_SOCKET_FILTER , +BPF_PROG_TYPE_TRACING_FILTER , };/* flags for BPF_MAP_UPDATE_ELEM command */
@@ -162,6 +163,13 @@ enum bpf_func_id { BPF_FUNC_map_lookup_elem,/* void *map_lookup_elem(&map, &key) */ BPF_FUNC_map_update_elem,/* int map_update_elem(&map, &key, &value, flags) */ BPF_FUNC_map_delete_elem,/* int map_delete_elem(&map, &key) */ + BPF_FUNC_fetch_ptr,/* void *bpf_fetch_ptr(void *unsafe_ptr) */ + BPF_FUNC_fetch_u64,/* u64 bpf_fetch_u64(void *unsafe_ptr) */ + BPF_FUNC_fetch_u32,/* u32 bpf_fetch_u32(void *unsafe_ptr) */ + BPF_FUNC_fetch_u16,/* u16 bpf_fetch_u16(void *unsafe_ptr) */ + BPF_FUNC_fetch_u8,/* u8 bpf_fetch_u8(void *unsafe_ptr) */ + BPF_FUNC_memcmp,/* int bpf_memcmp(void *unsafe_ptr, void *safe_ptr, int size) */ + BPF_FUNC_dump_stack,/* void bpf_dump_stack(void) */ __BPF_FUNC_MAX_ID, };
kernel/trace/Kconfig +1 -0
@@ -75,6 +75,7 @@ config FTRACE_NMI_ENTER config EVENT_TRACING select CONTEXT_SWITCH_TRACER + select BPF_SYSCALL bool config CONTEXT_SWITCH_TRACER
kernel/trace/Makefile +1 -0
@@ -53,6 +53,7 @@ obj-$(CONFIG_EVENT_TRACING) += trace_event_perf.o endif obj-$(CONFIG_EVENT_TRACING) += trace_events_filter.o obj-$(CONFIG_EVENT_TRACING) += trace_events_trigger.o +obj-$(CONFIG_EVENT_TRACING) += bpf_trace.o obj-$(CONFIG_KPROBE_EVENT) += trace_kprobe.o obj-$(CONFIG_TRACEPOINTS) += power-traces.o ifeq ($(CONFIG_PM),y)
kernel/trace/bpf_trace.c new file +140 -0
140 lines not on this page · raw message
kernel/trace/trace.h +3 -0
@@ -977,12 +977,15 @@ struct ftrace_event_field {int is_signed; }; +struct bpf_prog ; +struct event_filter {int n_preds;/* Number assigned */ int a_preds;/* allocated */ struct filter_pred *preds;struct filter_pred *root;char *filter_string; +struct bpf_prog *prog; };struct event_subsystem {
kernel/trace/trace_events.c +32 -1
@@ -1084,6 +1084,26 @@ event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,return r; } +static int event_filter_release (struct inode *inode,struct file *filp) +{ +struct ftrace_event_file *file; +char buf[2 ] ="0" ; + +mutex_lock (&event_mutex); + file =event_file_data (filp); +if (file) { +if (file->flags &TRACE_EVENT_FL_BPF ) { +/* auto-disable the filter */ +ftrace_event_enable_disable (file,0 ); + +/* if BPF filter was used, clear it on fd close */ +apply_event_filter (file, buf); + } + } +mutex_unlock (&event_mutex); +return 0 ; +} +static ssize_t event_filter_write (struct file *filp,const char __user *ubuf,size_t cnt,loff_t *ppos)
@@ -1107,8 +1127,18 @@ event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,mutex_lock (&event_mutex); file =event_file_data (filp); -if (file) +if (file) { +/* +* note to user space tools: +* write() into debugfs/tracing/events/xxx/filter file +* must be done with the same privilege level as open() +*/ err =apply_event_filter (file, buf); +if (!err && file->flags &TRACE_EVENT_FL_BPF ) +/* once filter is applied, auto-enable it */ +ftrace_event_enable_disable (file,1 ); + } +mutex_unlock (&event_mutex);free_page ((unsigned long ) buf);
@@ -1363,6 +1393,7 @@ static const struct file_operations ftrace_event_filter_fops = { .open = tracing_open_generic, .read = event_filter_read, .write = event_filter_write, + .release = event_filter_release, .llseek = default_llseek, };
kernel/trace/trace_events_filter.c +75 -1
112 lines not on this page · raw message
kernel/trace/trace_syscalls.c +31 -0
@@ -7,6 +7,7 @@#include <linux/ftrace.h> #include <linux/perf_event.h> #include <asm/syscall.h> +#include <trace/bpf_trace.h> #include "trace_output.h" #include "trace.h"
@@ -290,6 +291,20 @@ static int __init syscall_exit_define_fields(struct ftrace_event_call *call)return ret; } +static void populate_bpf_ctx (struct bpf_context *ctx,struct pt_regs *regs) +{ +struct task_struct *task = current; +unsigned long args[6 ]; + +syscall_get_arguments (task, regs,0 ,6 , args); + ctx->arg1 = args[0 ]; + ctx->arg2 = args[1 ]; + ctx->arg3 = args[2 ]; + ctx->arg4 = args[3 ]; + ctx->arg5 = args[4 ]; + ctx->arg6 = args[5 ]; +} +static void ftrace_syscall_enter (void *data,struct pt_regs *regs,long id) {struct trace_array *tr = data;
@@ -319,6 +334,14 @@ static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id)if (!sys_data)return ; +if (ftrace_file->flags &TRACE_EVENT_FL_BPF ) { +struct bpf_context ctx; + +populate_bpf_ctx (&ctx, regs); +if (trace_filter_call_bpf (ftrace_file->filter, &ctx) ==0 ) +return ; + } + size =sizeof (*entry) +sizeof (unsigned long ) * sys_data->nb_args;local_save_flags (irq_flags);
@@ -366,6 +389,14 @@ static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret)if (!sys_data)return ; +if (ftrace_file->flags &TRACE_EVENT_FL_BPF ) { +struct bpf_context ctx = {}; + + ctx.arg1 =syscall_get_return_value (current, regs); +if (trace_filter_call_bpf (ftrace_file->filter, &ctx) ==0 ) +return ; + } +local_save_flags (irq_flags); pc =preempt_count ();
Signature · 1 line
1.7.9.5
PATCH 2/9 tracing: allow eBPF programs to call bpf_printk()
In reply to Alexei StarovoitovDebugging of eBPF programs needs some form of printk from the program, so let programs call limited trace_printk() with %d %u %x %p modifiers only.
--- include/uapi/linux/bpf.h | 1 + kernel/trace/bpf_trace.c | 61 +++++++++++++++++++++++++++++++++++++++++++ kernel/trace/trace_events.c | 8 ++++++ 3 files changed, 70 insertions(+)
include/uapi/linux/bpf.h +1 -0
@@ -170,6 +170,7 @@ enum bpf_func_id { BPF_FUNC_fetch_u8,/* u8 bpf_fetch_u8(void *unsafe_ptr) */ BPF_FUNC_memcmp,/* int bpf_memcmp(void *unsafe_ptr, void *safe_ptr, int size) */ BPF_FUNC_dump_stack,/* void bpf_dump_stack(void) */ + BPF_FUNC_printk,/* int bpf_printk(const char *fmt, int fmt_size, ...) */ __BPF_FUNC_MAX_ID, };
kernel/trace/bpf_trace.c +61 -0
73 lines not on this page · raw message
kernel/trace/trace_events.c +8 -0
@@ -1141,6 +1141,14 @@ event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,mutex_unlock (&event_mutex); +if (file && file->flags &TRACE_EVENT_FL_BPF ) { +/* +* allocate per-cpu printk buffers, since programs +* might be calling bpf_printk +*/ +trace_printk_init_buffers (); + } +free_page ((unsigned long ) buf);if (err <0 )return err;
Signature · 1 line
1.7.9.5
PATCH 3/9 tracing: allow eBPF programs to call ktime_get_ns()
In reply to Alexei Starovoitovbpf_ktime_get_ns() is used by programs to compue time delta between events or as a timestamp
--- include/uapi/linux/bpf.h | 1 + kernel/trace/bpf_trace.c | 10 ++++++++++ 2 files changed, 11 insertions(+)
include/uapi/linux/bpf.h +1 -0
@@ -171,6 +171,7 @@ enum bpf_func_id { BPF_FUNC_memcmp,/* int bpf_memcmp(void *unsafe_ptr, void *safe_ptr, int size) */ BPF_FUNC_dump_stack,/* void bpf_dump_stack(void) */ BPF_FUNC_printk,/* int bpf_printk(const char *fmt, int fmt_size, ...) */ + BPF_FUNC_ktime_get_ns,/* u64 bpf_ktime_get_ns(void) */ __BPF_FUNC_MAX_ID, };
kernel/trace/bpf_trace.c +10 -0
@@ -114,6 +114,11 @@ static u64 bpf_printk(u64 r1, u64 fmt_size, u64 r3, u64 r4, u64 r5) mod_l[2] ? r5 : (u32) r5); } +static u64 bpf_ktime_get_ns (u64 r1,u64 r2,u64 r3,u64 r4,u64 r5) +{ +return ktime_get_ns (); +} +static struct bpf_func_proto tracing_filter_funcs[] = { #define FETCH(SIZE) \ [BPF_FUNC_fetch_##SIZE] = { \
@@ -147,6 +152,11 @@ static struct bpf_func_proto tracing_filter_funcs[] = { .arg1_type = ARG_PTR_TO_STACK, .arg2_type = ARG_CONST_STACK_SIZE, }, + [BPF_FUNC_ktime_get_ns] = { + .func = bpf_ktime_get_ns, + .gpl_only = true, + .ret_type = RET_INTEGER, + }, };static const struct bpf_func_proto *tracing_filter_func_proto (enum bpf_func_id func_id)
Signature · 1 line
1.7.9.5
PATCH 4/9 samples: bpf: simple tracing example in eBPF assembler
In reply to Alexei Starovoitovsimple packet drop monitor:
- in-kernel eBPF program attaches to kfree_skb() event and records number of packet drops at given location
- userspace iterates over the map every second and prints stats
Usage:
$ sudo dropmon location 0xffffffff81695995 count 1 location 0xffffffff816d0da9 count 2
location 0xffffffff81695995 count 2
location 0xffffffff816d0da9 count 2
location 0xffffffff81695995 count 3
location 0xffffffff816d0da9 count 2
$ addr2line -ape ./bld_x64/vmlinux 0xffffffff81695995 0xffffffff816d0da9 0xffffffff81695995: ./bld_x64/../net/ipv4/icmp.c:1038 0xffffffff816d0da9: ./bld_x64/../net/unix/af_unix.c:1231
--- samples/bpf/Makefile | 2 + samples/bpf/dropmon.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 samples/bpf/dropmon.c
samples/bpf/Makefile +2 -0
@@ -6,7 +6,9 @@ hostprogs-y := test_verifier test_maps hostprogs-y += sock_example hostprogs-y += sockex1 hostprogs-y += sockex2 +hostprogs-y += dropmon +dropmon-objs := dropmon.o libbpf.o test_verifier-objs := test_verifier.o libbpf.o test_maps-objs := test_maps.o libbpf.o sock_example-objs := sock_example.o libbpf.o
samples/bpf/dropmon.c new file +129 -0
129 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Re: [PATCH tip 4/9] samples: bpf: simple tracing example in eBPF assembler
Quoted 4 lines · Alexei Starovoitov
simple packet drop monitor:
- in-kernel eBPF program attaches tokfree_skb()event and records number of packet drops at given location
- userspace iterates over the map every second and prints stats
Hmm, this eBPF assembly macros are very interesting. Maybe I can replace current kprobe's argument "fetching methods" with this.
Thank you,
Quoted 173 lines
Usage:
$ sudo dropmon location 0xffffffff81695995 count 1 location 0xffffffff816d0da9 count 2location
0xffffffff81695995count 2
location0xffffffff816d0da9count 2location
0xffffffff81695995count 3
location0xffffffff816d0da9count 2$ addr2line -ape ./bld_x64/vmlinux 0xffffffff81695995 0xffffffff816d0da9 0xffffffff81695995: ./bld_x64/../net/ipv4/icmp.c:1038 0xffffffff816d0da9: ./bld_x64/../net/unix/af_unix.c:1231Signed-off-by Alexei Starovoitov <[email protected]>--- samples/bpf/Makefile | 2 + samples/bpf/dropmon.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 samples/bpf/dropmon.csamples/bpf/Makefile +2
-0@@ -6,7 +6,9 @@ hostprogs-y := test_verifier test_maps hostprogs-y += sock_example hostprogs-y += sockex1 hostprogs-y += sockex2 +hostprogs-y += dropmon +dropmon-objs := dropmon.o libbpf.o test_verifier-objs := test_verifier.o libbpf.o test_maps-objs := test_maps.o libbpf.o sock_example-objs := sock_example.o libbpf.osamples/bpf/dropmon.c new file +129
-0@@ -0,0 +1,129 @@ +/* simple packet drop monitor: +* - in-kernel eBPF program attaches to kfree_skb() event and records number +* of packet drops at given location +* - userspace iterates over the map every second and prints stats +*/ +#include <stdio.h> +#include <unistd.h> +#include <linux/bpf.h> +#include <errno.h> +#include <linux/unistd.h> +#include <string.h> +#include <linux/filter.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <stdbool.h> +#include "libbpf.h" + +#define TRACEPOINT "/sys/kernel/debug/tracing/events/skb/kfree_skb/" + +static int write_to_file (const char *file,const char *str,bool keep_open) +{ +int fd, err; + + fd =open (file,O_WRONLY ); + err =write (fd, str,strlen (str)); + (void ) err; + +if (keep_open) { +return fd; + }else { +close (fd); +return -1 ; + } +} + +static int dropmon (void ) +{ +long long key, next_key, value =0 ; +int prog_fd, map_fd, i; +char fmt[32 ]; + + map_fd =bpf_create_map (BPF_MAP_TYPE_HASH ,sizeof (key),sizeof (value),1024 ); +if (map_fd <0 ) { +printf ("failed to create map '%s'\n" ,strerror (errno)); + goto cleanup; + } + +/* the following eBPF program is equivalent to C: +* int filter(struct bpf_context *ctx) +* { +* long loc = ctx->arg2; +* long init_val = 1; +* long *value; +* +* value = bpf_map_lookup_elem(MAP_ID, &loc); +* if (value) { +* __sync_fetch_and_add(value, 1); +* } else { +* bpf_map_update_elem(MAP_ID, &loc, &init_val, BPF_ANY); +* } +* return 0; +* } +*/ +struct bpf_insn prog[] = { +BPF_LDX_MEM (BPF_DW ,BPF_REG_2 ,BPF_REG_1 ,8 ),/* r2 = *(u64 *)(r1 + 8) */ +BPF_STX_MEM (BPF_DW ,BPF_REG_10 ,BPF_REG_2 ,-8 ),/* *(u64 *)(fp - 8) = r2 */ +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ),/* r2 = fp - 8 */ +BPF_LD_MAP_FD (BPF_REG_1 , map_fd), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_map_lookup_elem), +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,4 ), +BPF_MOV64_IMM (BPF_REG_1 ,1 ),/* r1 = 1 */ +BPF_RAW_INSN (BPF_STX |BPF_XADD |BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,0 ,0 ),/* xadd r0 += r1 */ +BPF_MOV64_IMM (BPF_REG_0 ,0 ),/* r0 = 0 */ +BPF_EXIT_INSN (), +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-16 ,1 ),/* *(u64 *)(fp - 16) = 1 */ +BPF_MOV64_IMM (BPF_REG_4 ,BPF_ANY ), +BPF_MOV64_REG (BPF_REG_3 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_3 ,-16 ),/* r3 = fp - 16 */ +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ),/* r2 = fp - 8 */ +BPF_LD_MAP_FD (BPF_REG_1 , map_fd), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_map_update_elem), +BPF_MOV64_IMM (BPF_REG_0 ,0 ),/* r0 = 0 */ +BPF_EXIT_INSN (), + }; + + prog_fd =bpf_prog_load (BPF_PROG_TYPE_TRACING_FILTER , prog, +sizeof (prog),"GPL" ); +if (prog_fd <0 ) { +printf ("failed to load prog '%s'\n%s" ,strerror (errno), bpf_log_buf); +return -1 ; + } + +sprintf (fmt,"bpf_%d" , prog_fd); + +write_to_file (TRACEPOINT "filter" , fmt, true); + +for (i =0 ; i <10 ; i++) { + key =0 ; +while (bpf_get_next_key (map_fd, &key, &next_key) ==0 ) { +bpf_lookup_elem (map_fd, &next_key, &value); +printf ("location 0x%llx count %lld\n" , next_key, value); + key = next_key; + } +if (key) +printf ("\n" ); +sleep (1 ); + } + +cleanup: +/* maps, programs, tracepoint filters will auto cleanup on process exit */ + +return 0 ; +} + +int main (void ) +{ +FILE *f; + +/* start ping in the background to get some kfree_skb events */ + f =popen ("ping -c5 localhost" ,"r" ); + (void ) f; + +dropmon (); +return 0 ; +}
Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
PATCH 5/9 samples: bpf: simple tracing example in C
In reply to Alexei Starovoitovtracex1_kern.c - C program which will be compiled into eBPF to filter netif_receive_skb events on skb->dev->name == "lo" The programs returns 1 to continue storing an event into trace buffer and returns 0 - to discard an event.
tracex1_user.c - corresponding user space component that forever reads /sys/.../trace_pipe
Usage:
$ sudo tracex1
should see:
writing bpf-4 -> /sys/kernel/debug/tracing/events/net/netif_receive_skb/filter
ping-364 [000] ..s2 8.089771: netif_receive_skb: dev=lo skbaddr=ffff88000dfcc100 len=84 ping-364 [000] ..s2 8.089889: netif_receive_skb: dev=lo skbaddr=ffff88000dfcc900 len=84
Ctrl-C at any time, kernel will auto cleanup
--- samples/bpf/Makefile | 4 +++ samples/bpf/bpf_helpers.h | 18 ++++++++++++++ samples/bpf/bpf_load.c | 59 +++++++++++++++++++++++++++++++++++++++----- samples/bpf/bpf_load.h | 3 +++ samples/bpf/tracex1_kern.c | 28 +++++++++++++++++++++ samples/bpf/tracex1_user.c | 24 ++++++++++++++++++ 6 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 samples/bpf/tracex1_kern.c create mode 100644 samples/bpf/tracex1_user.c
samples/bpf/Makefile +4 -0
@@ -7,6 +7,7 @@ hostprogs-y += sock_example hostprogs-y += sockex1 hostprogs-y += sockex2 hostprogs-y += dropmon +hostprogs-y += tracex1 dropmon-objs := dropmon.o libbpf.o test_verifier-objs := test_verifier.o libbpf.o
@@ -14,17 +15,20 @@ test_maps-objs := test_maps.o libbpf.o sock_example-objs := sock_example.o libbpf.o sockex1-objs := bpf_load.o libbpf.o sockex1_user.o sockex2-objs := bpf_load.o libbpf.o sockex2_user.o +tracex1-objs := bpf_load.o libbpf.o tracex1_user.o # Tell kbuild to always build the programs always := $(hostprogs-y) always += sockex1_kern.o always += sockex2_kern.o +always += tracex1_kern.o HOSTCFLAGS += -I$(objtree)/usr/include HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable HOSTLOADLIBES_sockex1 += -lelf HOSTLOADLIBES_sockex2 += -lelf +HOSTLOADLIBES_tracex1 += -lelf # point this to your LLVM backend with bpf support LLC=$(srctree)/tools/bpf/llvm/bld/Debug+Asserts/bin/llc
samples/bpf/bpf_helpers.h +18 -0
@@ -15,6 +15,24 @@ static int (*bpf_map_update_elem)(void *map, void *key, void *value, (void *) BPF_FUNC_map_update_elem;static int (*bpf_map_delete_elem)(void *map,void *key) = (void *) BPF_FUNC_map_delete_elem; +static void *(*bpf_fetch_ptr)(void *unsafe_ptr) = + (void *) BPF_FUNC_fetch_ptr; +static unsigned long long (*bpf_fetch_u64)(void *unsafe_ptr) = + (void *) BPF_FUNC_fetch_u64; +static unsigned int (*bpf_fetch_u32)(void *unsafe_ptr) = + (void *) BPF_FUNC_fetch_u32; +static unsigned short (*bpf_fetch_u16)(void *unsafe_ptr) = + (void *) BPF_FUNC_fetch_u16; +static unsigned char (*bpf_fetch_u8)(void *unsafe_ptr) = + (void *) BPF_FUNC_fetch_u8; +static int (*bpf_printk)(const char *fmt,int fmt_size, ...) = + (void *) BPF_FUNC_printk; +static int (*bpf_memcmp)(void *unsafe_ptr,void *safe_ptr,int size) = + (void *) BPF_FUNC_memcmp; +static void (*bpf_dump_stack)(void ) = + (void *) BPF_FUNC_dump_stack; +static unsigned long long (*bpf_ktime_get_ns)(void ) = + (void *) BPF_FUNC_ktime_get_ns;/* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions
samples/bpf/bpf_load.c +53 -6
83 lines not on this page · raw message
samples/bpf/bpf_load.h +3 -0
@@ -21,4 +21,7 @@ extern int prog_fd[MAX_PROGS];*/ int load_bpf_file (char *path); +/* forever reads /sys/.../trace_pipe */ +void read_trace_pipe (void ); +#endif
samples/bpf/tracex1_kern.c new file +28 -0
@@ -0,0 +1,28 @@ +#include <linux/skbuff.h> +#include <linux/netdevice.h> +#include <uapi/linux/bpf.h> +#include <trace/bpf_trace.h> +#include "bpf_helpers.h" + +SEC("events/net/netif_receive_skb") +int bpf_prog1 (struct bpf_context *ctx) +{ +/* +* attaches to /sys/kernel/debug/tracing/events/net/netif_receive_skb +* prints events for loobpack device only +*/ +char devname[] ="lo" ; +struct net_device *dev; +struct sk_buff *skb =0 ; + + skb = (struct sk_buff *) ctx->arg1; + dev =bpf_fetch_ptr (&skb->dev); +if (bpf_memcmp (dev->name, devname,2 ) ==0 ) +/* print event using default tracepoint format */ +return 1 ; + +/* drop event */ +return 0 ; +} + +char _license[] SEC("license") = "GPL";
samples/bpf/tracex1_user.c new file +24 -0
@@ -0,0 +1,24 @@ +#include <stdio.h> +#include <linux/bpf.h> +#include "libbpf.h" +#include "bpf_load.h" + +int main (int ac,char **argv) +{ +FILE *f; +char filename[256 ]; + +snprintf (filename,sizeof (filename),"%s_kern.o" , argv[0 ]); + +if (load_bpf_file (filename)) { +printf ("%s" , bpf_log_buf); +return 1 ; + } + + f =popen ("ping -c5 localhost" ,"r" ); + (void ) f; + +read_trace_pipe (); + +return 0 ; +}
Signature · 1 line
1.7.9.5
PATCH 6/9 samples: bpf: counting example for kfree_skb tracepoint and write syscall
In reply to Alexei Starovoitovthis example has two probes in one C file that attach to different tracepoints and use two different maps.
1st probe is the similar to dropmon.c. It attaches to kfree_skb tracepoint and count number of packet drops at different locations
2nd probe attaches to syscalls/sys_enter_write and computes a histogram of different write sizes
Usage:
$ sudo tracex2 writing bpf-8 -> /sys/kernel/debug/tracing/events/skb/kfree_skb/filter writing bpf-10 -> /sys/kernel/debug/tracing/events/syscalls/sys_enter_write/filter location 0xffffffff816959a5 count 1
location 0xffffffff816959a5 count 2
557145+0 records in
557145+0 records out
285258240 bytes (285 MB) copied, 1.02379 s, 279 MB/s
syscall write() stats
byte_size : count distribution
1 -> 1 : 3 | |
2 -> 3 : 0 | |
4 -> 7 : 0 | |
8 -> 15 : 0 | |
16 -> 31 : 2 | |
32 -> 63 : 3 | |
64 -> 127 : 1 | |
128 -> 255 : 1 | |
256 -> 511 : 0 | |
512 -> 1023 : 1118968 |************************************* |Ctrl-C at any time. Kernel will auto cleanup maps and programs
--- samples/bpf/Makefile | 4 ++ samples/bpf/tracex2_kern.c | 71 +++++++++++++++++++++++++++++++++ samples/bpf/tracex2_user.c | 95 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 samples/bpf/tracex2_kern.c create mode 100644 samples/bpf/tracex2_user.c
samples/bpf/Makefile +4 -0
@@ -8,6 +8,7 @@ hostprogs-y += sockex1 hostprogs-y += sockex2 hostprogs-y += dropmon hostprogs-y += tracex1 +hostprogs-y += tracex2 dropmon-objs := dropmon.o libbpf.o test_verifier-objs := test_verifier.o libbpf.o
@@ -16,12 +17,14 @@ sock_example-objs := sock_example.o libbpf.o sockex1-objs := bpf_load.o libbpf.o sockex1_user.o sockex2-objs := bpf_load.o libbpf.o sockex2_user.o tracex1-objs := bpf_load.o libbpf.o tracex1_user.o +tracex2-objs := bpf_load.o libbpf.o tracex2_user.o # Tell kbuild to always build the programs always := $(hostprogs-y) always += sockex1_kern.o always += sockex2_kern.o always += tracex1_kern.o +always += tracex2_kern.o HOSTCFLAGS += -I$(objtree)/usr/include
@@ -29,6 +32,7 @@ HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable HOSTLOADLIBES_sockex1 += -lelf HOSTLOADLIBES_sockex2 += -lelf HOSTLOADLIBES_tracex1 += -lelf +HOSTLOADLIBES_tracex2 += -lelf # point this to your LLVM backend with bpf support LLC=$(srctree)/tools/bpf/llvm/bld/Debug+Asserts/bin/llc
samples/bpf/tracex2_kern.c new file +71 -0
71 lines not on this page · raw message
samples/bpf/tracex2_user.c new file +95 -0
95 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH 7/9 samples: bpf: IO latency analysis (iosnoop/heatmap)
In reply to Alexei StarovoitoveBPF C program attaches to block_rq_issue/block_rq_complete events to calculate IO latency. Then it waits for the first 100 events to compute average latency and uses range [0 .. ave_lat * 2] to record histogram of events in this latency range.
User space reads this histogram map every 2 seconds and prints it as a 'heatmap' using gray shades of text terminal. Black spaces have many events and white spaces have very few events. Left most space is the smallest latency, right most space is the largest latency in the range.
If kernel sees too many events that fall out of histogram range, user space adjusts the range up, so heatmap for next 2 seconds will be more accurate.
Usage:
$ sudo ./tracex3 and do 'sudo dd if=/dev/sda of=/dev/null' in other terminal. Observe IO latencies and how different activity (like 'make kernel') affects it.
Similar experiments can be done for network transmit latencies, syscalls, etc
--- samples/bpf/Makefile | 4 ++ samples/bpf/tracex3_kern.c | 96 +++++++++++++++++++++++++++++ samples/bpf/tracex3_user.c | 146 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 246 insertions(+) create mode 100644 samples/bpf/tracex3_kern.c create mode 100644 samples/bpf/tracex3_user.c
samples/bpf/Makefile +4 -0
@@ -9,6 +9,7 @@ hostprogs-y += sockex2 hostprogs-y += dropmon hostprogs-y += tracex1 hostprogs-y += tracex2 +hostprogs-y += tracex3 dropmon-objs := dropmon.o libbpf.o test_verifier-objs := test_verifier.o libbpf.o
@@ -18,6 +19,7 @@ sockex1-objs := bpf_load.o libbpf.o sockex1_user.o sockex2-objs := bpf_load.o libbpf.o sockex2_user.o tracex1-objs := bpf_load.o libbpf.o tracex1_user.o tracex2-objs := bpf_load.o libbpf.o tracex2_user.o +tracex3-objs := bpf_load.o libbpf.o tracex3_user.o # Tell kbuild to always build the programs always := $(hostprogs-y)
@@ -25,6 +27,7 @@ always += sockex1_kern.o always += sockex2_kern.o always += tracex1_kern.o always += tracex2_kern.o +always += tracex3_kern.o HOSTCFLAGS += -I$(objtree)/usr/include
@@ -33,6 +36,7 @@ HOSTLOADLIBES_sockex1 += -lelf HOSTLOADLIBES_sockex2 += -lelf HOSTLOADLIBES_tracex1 += -lelf HOSTLOADLIBES_tracex2 += -lelf +HOSTLOADLIBES_tracex3 += -lelf # point this to your LLVM backend with bpf support LLC=$(srctree)/tools/bpf/llvm/bld/Debug+Asserts/bin/llc
samples/bpf/tracex3_kern.c new file +96 -0
96 lines not on this page · raw message
samples/bpf/tracex3_user.c new file +146 -0
146 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH 8/9 tracing: attach eBPF programs to kprobe/kretprobe
In reply to Alexei Starovoitovintroduce new type of eBPF programs BPF_PROG_TYPE_KPROBE_FILTER. Such programs are allowed to call the same helper functions as tracing filters, but bpf_context is different:
For tracing filters bpf_context is 6 arguments of tracepoints or syscalls For kprobe filters bpf_context == pt_regs
--- include/linux/ftrace_event.h | 2 ++ include/uapi/linux/bpf.h | 1 + kernel/trace/bpf_trace.c | 39 ++++++++++++++++++++++++++++++++++++ kernel/trace/trace_events_filter.c | 10 ++++++--- kernel/trace/trace_kprobe.c | 11 +++++++++- 5 files changed, 59 insertions(+), 4 deletions(-)
include/linux/ftrace_event.h +2 -0
@@ -249,6 +249,7 @@ enum {TRACE_EVENT_FL_USE_CALL_FILTER_BIT ,TRACE_EVENT_FL_TRACEPOINT_BIT ,TRACE_EVENT_FL_BPF_BIT , + TRACE_EVENT_FL_KPROBE_BIT, };/*
@@ -272,6 +273,7 @@ enum {TRACE_EVENT_FL_USE_CALL_FILTER = (1 <<TRACE_EVENT_FL_USE_CALL_FILTER_BIT ),TRACE_EVENT_FL_TRACEPOINT = (1 <<TRACE_EVENT_FL_TRACEPOINT_BIT ),TRACE_EVENT_FL_BPF = (1 <<TRACE_EVENT_FL_BPF_BIT ), + TRACE_EVENT_FL_KPROBE = (1 << TRACE_EVENT_FL_KPROBE_BIT), }; struct ftrace_event_call {
include/uapi/linux/bpf.h +1 -0
@@ -119,6 +119,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_UNSPEC ,BPF_PROG_TYPE_SOCKET_FILTER ,BPF_PROG_TYPE_TRACING_FILTER , + BPF_PROG_TYPE_KPROBE_FILTER, };/* flags for BPF_MAP_UPDATE_ELEM command */
kernel/trace/bpf_trace.c +39 -0
@@ -209,3 +209,42 @@ static int __init register_tracing_filter_ops(void)return 0 ; }late_initcall (register_tracing_filter_ops); + +/* check access to fields of 'struct pt_regs' from BPF program */ +static bool kprobe_filter_is_valid_access (int off,int size,enum bpf_access_type type) +{ +/* check bounds */ +if (off <0 || off >=sizeof (struct pt_regs )) +return false; + +/* only read is allowed */ +if (type !=BPF_READ ) +return false; + +/* disallow misaligned access */ +if (off % size !=0 ) +return false; + +return true; +} +/* kprobe filter programs are allowed to call the same helper functions +* as tracing filters, but bpf_context is different: +* For tracing filters bpf_context is 6 arguments of tracepoints or syscalls +* For kprobe filters bpf_context == pt_regs +*/ +static struct bpf_verifier_ops kprobe_filter_ops = { + .get_func_proto = tracing_filter_func_proto, + .is_valid_access = kprobe_filter_is_valid_access, +}; + +static struct bpf_prog_type_list kprobe_tl = { + .ops = &kprobe_filter_ops, + .type =BPF_PROG_TYPE_KPROBE_FILTER , +}; + +static int __init register_kprobe_filter_ops (void ) +{ +bpf_register_prog_type (&kprobe_tl); +return 0 ; +} +late_initcall (register_kprobe_filter_ops);
kernel/trace/trace_events_filter.c +7 -3
@@ -1891,7 +1891,8 @@ static int create_filter_start(char *filter_str, bool set_str,return err; } -static int create_filter_bpf (char *filter_str,struct event_filter **filterp) +static int create_filter_bpf (struct ftrace_event_call *call,char *filter_str, +struct event_filter **filterp) {struct event_filter *filter;struct bpf_prog *prog;
@@ -1920,7 +1921,10 @@ static int create_filter_bpf(char *filter_str, struct event_filter **filterp) filter->prog = prog; -if (prog->aux->prog_type !=BPF_PROG_TYPE_TRACING_FILTER ) { +if (((call->flags &TRACE_EVENT_FL_KPROBE ) && + prog->aux->prog_type !=BPF_PROG_TYPE_KPROBE_FILTER ) || + (!(call->flags &TRACE_EVENT_FL_KPROBE ) && + prog->aux->prog_type !=BPF_PROG_TYPE_TRACING_FILTER )) {/* valid fd, but invalid bpf program type */ err = -EINVAL ; goto free_filter;
@@ -2051,7 +2055,7 @@ int apply_event_filter(struct ftrace_event_file *file, char *filter_string)*/ if (memcmp (filter_string,"bpf" ,3 ) ==0 && filter_string[3 ] !=0 && filter_string[4 ] !=0 ) { - err =create_filter_bpf (filter_string, &filter); + err =create_filter_bpf (call, filter_string, &filter);if (!err) file->flags |=TRACE_EVENT_FL_BPF ; }else {
kernel/trace/trace_kprobe.c +10 -1
@@ -19,6 +19,7 @@#include <linux/module.h> #include <linux/uaccess.h> +#include <trace/bpf_trace.h> #include "trace_probe.h"
@@ -930,6 +931,10 @@ __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,if (ftrace_trigger_soft_disabled (ftrace_file))return ; +if (ftrace_file->flags &TRACE_EVENT_FL_BPF ) +if (trace_filter_call_bpf (ftrace_file->filter, regs) ==0 ) +return ; +local_save_flags (irq_flags); pc =preempt_count ();
@@ -978,6 +983,10 @@ __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,if (ftrace_trigger_soft_disabled (ftrace_file))return ; +if (ftrace_file->flags &TRACE_EVENT_FL_BPF ) +if (trace_filter_call_bpf (ftrace_file->filter, regs) ==0 ) +return ; +local_save_flags (irq_flags); pc =preempt_count ();
@@ -1286,7 +1295,7 @@ static int register_kprobe_event(struct trace_kprobe *tk)kfree (call->print_fmt);return -ENODEV ; } - call->flags =0 ; + call->flags =TRACE_EVENT_FL_KPROBE ; call->class->reg = kprobe_register; call->data = tk; ret =trace_add_event_call (call);
Signature · 1 line
1.7.9.5
PATCH 9/9 samples: bpf: simple kprobe example
In reply to Alexei Starovoitovthe logic of the example is similar to tracex2, but syscall 'write' statistics is capturead from kprobe placed at sys_write function instead of through syscall instrumentation.
Also tracex4_kern.c has a different way of doing log2 in C.
Note, unlike tracepoint and syscall programs, kprobe programs receive 'struct pt_regs' as an input. It's responsibility of the program author or higher level dynamic tracing tool to match registers to function arguments. Since pt_regs are architecture dependent, programs are also arch dependent, unlike tracepoint/syscalls programs which are universal.
Usage:
$ sudo tracex4 writing bpf-6 -> /sys/kernel/debug/tracing/events/kprobes/sys_write/filter 2216443+0 records in 2216442+0 records out 1134818304 bytes (1.1 GB) copied, 2.00746 s, 565 MB/s
kprobe sys_write() stats
byte_size : count distribution
1 -> 1 : 0 | |
2 -> 3 : 0 | |
4 -> 7 : 0 | |
8 -> 15 : 0 | |
16 -> 31 : 0 | |
32 -> 63 : 0 | |
64 -> 127 : 1 | |
128 -> 255 : 0 | |
256 -> 511 : 0 | |
512 -> 1023 : 2214734 |************************************* |--- samples/bpf/Makefile | 4 +++ samples/bpf/bpf_load.c | 3 ++ samples/bpf/tracex4_kern.c | 36 +++++++++++++++++++ samples/bpf/tracex4_user.c | 83 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 samples/bpf/tracex4_kern.c create mode 100644 samples/bpf/tracex4_user.c
samples/bpf/Makefile +4 -0
@@ -10,6 +10,7 @@ hostprogs-y += dropmon hostprogs-y += tracex1 hostprogs-y += tracex2 hostprogs-y += tracex3 +hostprogs-y += tracex4 dropmon-objs := dropmon.o libbpf.o test_verifier-objs := test_verifier.o libbpf.o
@@ -20,6 +21,7 @@ sockex2-objs := bpf_load.o libbpf.o sockex2_user.o tracex1-objs := bpf_load.o libbpf.o tracex1_user.o tracex2-objs := bpf_load.o libbpf.o tracex2_user.o tracex3-objs := bpf_load.o libbpf.o tracex3_user.o +tracex4-objs := bpf_load.o libbpf.o tracex4_user.o # Tell kbuild to always build the programs always := $(hostprogs-y)
@@ -28,6 +30,7 @@ always += sockex2_kern.o always += tracex1_kern.o always += tracex2_kern.o always += tracex3_kern.o +always += tracex4_kern.o HOSTCFLAGS += -I$(objtree)/usr/include
@@ -37,6 +40,7 @@ HOSTLOADLIBES_sockex2 += -lelf HOSTLOADLIBES_tracex1 += -lelf HOSTLOADLIBES_tracex2 += -lelf HOSTLOADLIBES_tracex3 += -lelf +HOSTLOADLIBES_tracex4 += -lelf # point this to your LLVM backend with bpf support LLC=$(srctree)/tools/bpf/llvm/bld/Debug+Asserts/bin/llc
samples/bpf/bpf_load.c +3 -0
@@ -25,6 +25,7 @@ int prog_cnt;static int load_and_attach (const char *event,struct bpf_insn *prog,int size) {bool is_socket =strncmp (event,"socket" ,6 ) ==0 ; +bool is_kprobe =strncmp (event,"events/kprobes/" ,15 ) ==0 ;enum bpf_prog_type prog_type;char path[256 ] =DEBUGFS ;char fmt[32 ];
@@ -32,6 +33,8 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)if (is_socket) prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; +else if (is_kprobe) + prog_type =BPF_PROG_TYPE_KPROBE_FILTER ;else prog_type =BPF_PROG_TYPE_TRACING_FILTER ;
samples/bpf/tracex4_kern.c new file +36 -0
@@ -0,0 +1,36 @@ +#include <linux/skbuff.h> +#include <linux/netdevice.h> +#include <uapi/linux/bpf.h> +#include <trace/bpf_trace.h> +#include "bpf_helpers.h" + +static unsigned int log2l (unsigned long long n) +{ +#define S (k) if (n >= (1ull << k)) { i += k; n >>= k; } +int i = -(n ==0 ); +S (32 );S (16 );S (8 );S (4 );S (2 );S (1 ); +return i; +#undef S +} + +struct bpf_map_def SEC("maps") my_hist_map = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(u32), + .value_size = sizeof(long), + .max_entries = 64, +}; + +SEC("events/kprobes/sys_write") +int bpf_prog4 (struct pt_regs *regs) +{ +long write_size = regs->dx;/* $rdx contains 3rd argument to a function */ +long init_val =1 ; +void *value; +u32 index =log2l (write_size); + + value =bpf_map_lookup_elem (&my_hist_map, &index); +if (value) +__sync_fetch_and_add ((long *)value,1 ); +return 0 ; +} +char _license[] SEC("license") = "GPL";
samples/bpf/tracex4_user.c new file +83 -0
83 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Steven Rostedt
maintainerQuoted 3 lines · On Thu, 15 Jan 2015 20:16:01 -0800 Alexei Starovoitov…
Hi Ingo, Steven,
This patch set is based on tip/master.
Note, the tracing code isn't maintained in tip/master, but perf code is.
Using the latest 3.19-rc is probably sufficient for now.
Do you have a git repo somewhere that I can look at? It makes it easier than loading in 9 patches ;-)
Quoted 51 lines
It adds ability to attach eBPF programs to tracepoints, syscalls and kprobes.
Mechanism of attaching:
- load program viabpf()syscall and receiveprogram_fd
-event_fd=open("/sys/kernel/debug/tracing/events/.../filter")
- write 'bpf-123' toevent_fdwhere 123 isprogram_fd
- program will be attached to particular event and event automatically enabled
-close(event_fd)will detach bpf program from event and event disabledProgram attach point and input arguments:
- programs attached to kprobes receive 'structpt_regs*' as an input. Seetracex4_kern.cthat demonstrates how users can write a C program like:
SEC("events/kprobes/sys_write")
intbpf_prog4(struct pt_regs *regs){ long write_size = regs->dx; // here user need to know the proto of sys_write() from kernel // sources and x64 calling convention to know that register $rdx // contains 3rd argument to sys_write() which is 'size_t count'it's obviously architecture dependent, but allows building sophisticated user tools on top, that can see from debug info of vmlinux which variables are in which registers or stack locations and fetch it from there. 'perf probe' can potentialy use this hook to generate programs in user space and insert them instead of letting kernel parse string during kprobe creation.
- programs attached to tracepoints and syscalls receive 'struct
bpf_context*':
u64 arg1, arg2, ..., arg6;
for syscalls they match syscall arguments.
for tracepoints these args match arguments passed to tracepoint. For example:
trace_sched_migrate_task(p, new_cpu); fromsched/core.c
arg1 <- p which is 'structtask_struct*'
arg2 <-new_cpuwhich is 'unsigned int'
arg3..arg6 = 0
the program can use bpf_fetch_u8/16/32/64/ptr() helpers to walk 'task_struct' or any other kernel data structures.
These helpers are usingprobe_kernel_read()similar to 'perf probe' which is not 100% safe in both cases, but good enough.
To access task_struct's pid inside 'sched_migrate_task' tracepoint the program can do:
structtask_struct*task = (structtask_struct*)ctx->arg1;
u32 pid =bpf_fetch_u32(&task->pid);
Since struct layout is kernel configuration specific such programs are not portable and require access to kernel headers to be compiled, but in this case we don't need debug info.
llvm with bpf backend will statically computetask->pidoffset as a constant based on kernel headers only.
The example of this arbitrary pointer walking istracex1_kern.cwhich doesskb->dev->name== "lo" filtering.
Quoted 3 lines
In all cases the programs are called before trace buffer is allocated to minimize the overhead, since we want to filter huge number of events, but buffer alloc/free and argument copy for every event is too costly.
For syscalls this is fine as the parameters are usually set. But there's a lot of tracepoints that we need to know the result of the copied data to decide to filter or not, where the result happens at the TP_fast_assign() part which requires allocating the buffers.
Maybe we should have a way to do the program before and/or after the buffering depending on what to filter on. There's no way to know what the parameters of the tracepoint are without looking at the source.
Quoted 2 lines
Theoretically we can invoke programs after buffer is allocated, but it doesn't seem needed, since above approach is faster and achieves the same.
Again, for syscalls it may not be a problem, but for other tracepoints, I'm not sure we can do that. How do you handle sched_switch for example? The tracepoint only gets two pointers to task structs, you need to then dereference them to get the pid, prio, state and other data.
Quoted 12 lines
Note, tracepoint/syscall and kprobe programs are two different types:
BPF_PROG_TYPE_TRACING_FILTERandBPF_PROG_TYPE_KPROBE_FILTER, since they expect different input.
Both use the same set of helper functions:
- map access (lookup/update/delete)
- fetch (probe_kernel_readwrappers)
- memcmp (probe_kernel_read+ memcmp)
-dump_stack
-trace_printk
The last two are mainly to debug the programs and to print data for user space consumptions.
I have to look at the code, but currently trace_printk() isn't made to be used in production systems.
Quoted 8 lines
Portability:
- kprobe programs are architecture dependent and need user scripting language like ktap/stap/dtrace/perf that will dynamically generate them based on debug info in vmlinux
- tracepoint programs are architecture independent, but if arbitrary pointer walking (withfetch()helpers) is used, they need data struct layout to match. Debug info is not necessary
If the program runs after the buffers are allocated, it could still be architecture independent because ftrace gives the information on how to retrieve the fields.
One last thing. If the ebpf is used for anything but filtering, it should go into the trigger file. The filtering is only a way to say if the event should be recorded or not. But the trigger could do something else (a printk, a stacktrace, etc).
-- Steve
Quoted 91 lines
- for networking use case we need to access 'struct
sk_buff' fields in portable way (user space needs to fetch packet length without knowingskb->lenoffset), so for some frequently used data structures we will add helper functions or pseudo instructions to access them. I've hacked few ways specifically for skb, but abandoned them in favor of more generic type/field infra. That work is still wip. Not part of this set.
Once it's ready tracepoint programs that access common data structs will be kernel independent.Program return value:
- programs return 0 to discard an event
- and return non-zero to proceed with event (allocate trace buffer, copy arguments there and print it eventually intrace_pipein traditional way)Examples:
-dropmon.c- simplekfree_skb()accounting in eBPF assembler, similar to dropmon tool
-tracex1_kern.c- doesnet/netif_receive_skbevent filtering fordev->skb->name== "lo" condition
-tracex2_kern.c- samekfree_skb()accounting like dropmon, but now in C plus computes histogram of all write sizes fromsys_writesyscall and prints the histogram in userspace
-tracex3_kern.c- most sophisticated example that computes IO latency betweenblock/block_rq_issueandblock/block_rq_completeevents and prints 'heatmap' using gray shades of text terminal. Useful to analyze disk performance.
-tracex4_kern.c- computes histogram of write sizes fromsys_writesyscall using kprobe mechanism instead of syscall. Since kprobe is optimized into ftrace the overhead of instrumentation is smaller than in example 2.The user space tools like ktap/dtrace/systemptap/perf that has access to debug info would probably want to use kprobe attachment point, since kprobe can be inserted anywhere and all registers are avaiable in the program. tracepoint attachments are useful without debug info, so standalone tools like iosnoop will use them.
The main difference vs existing perf_probe/ftrace infra is in kernel aggregation and conditional walking of arbitrary data structures.
Thanks!
Alexei Starovoitov (9):
tracing: attach eBPF programs to tracepoints and syscalls
tracing: allow eBPF programs to callbpf_printk()
tracing: allow eBPF programs to callktime_get_ns()
samples: bpf: simple tracing example in eBPF assembler
samples: bpf: simple tracing example in C
samples: bpf: counting example forkfree_skbtracepoint and write
syscall
samples: bpf: IO latency analysis (iosnoop/heatmap)
tracing: attach eBPF programs to kprobe/kretprobe
samples: bpf: simple kprobe exampleinclude/linux/ftrace_event.h | 6 + include/trace/bpf_trace.h | 25 ++++ include/trace/ftrace.h | 30 +++++ include/uapi/linux/bpf.h | 11 ++ kernel/trace/Kconfig | 1 + kernel/trace/Makefile | 1 + kernel/trace/bpf_trace.c | 250 ++++++++++++++++++++++++++++++++++++ kernel/trace/trace.h | 3 + kernel/trace/trace_events.c | 41 +++++- kernel/trace/trace_events_filter.c | 80 +++++++++++- kernel/trace/trace_kprobe.c | 11 +- kernel/trace/trace_syscalls.c | 31 +++++ samples/bpf/Makefile | 18 +++ samples/bpf/bpf_helpers.h | 18 +++ samples/bpf/bpf_load.c | 62 ++++++++- samples/bpf/bpf_load.h | 3 + samples/bpf/dropmon.c | 129 +++++++++++++++++++ samples/bpf/tracex1_kern.c | 28 ++++ samples/bpf/tracex1_user.c | 24 ++++ samples/bpf/tracex2_kern.c | 71 ++++++++++ samples/bpf/tracex2_user.c | 95 ++++++++++++++ samples/bpf/tracex3_kern.c | 96 ++++++++++++++ samples/bpf/tracex3_user.c | 146 +++++++++++++++++++++ samples/bpf/tracex4_kern.c | 36 ++++++ samples/bpf/tracex4_user.c | 83 ++++++++++++ 25 files changed, 1290 insertions(+), 9 deletions(-) create mode 100644 include/trace/bpf_trace.h create mode 100644 kernel/trace/bpf_trace.c create mode 100644 samples/bpf/dropmon.c create mode 100644 samples/bpf/tracex1_kern.c create mode 100644 samples/bpf/tracex1_user.c create mode 100644 samples/bpf/tracex2_kern.c create mode 100644 samples/bpf/tracex2_user.c create mode 100644 samples/bpf/tracex3_kern.c create mode 100644 samples/bpf/tracex3_user.c create mode 100644 samples/bpf/tracex4_kern.c create mode 100644 samples/bpf/tracex4_user.c
Quoted 23 lines · Alexei Starovoitov
Hi Ingo, Steven,
This patch set is based on tip/master.
It adds ability to attach eBPF programs to tracepoints, syscalls and kprobes.Mechanism of attaching:
- load program viabpf()syscall and receiveprogram_fd
-event_fd=open("/sys/kernel/debug/tracing/events/.../filter")
- write 'bpf-123' toevent_fdwhere 123 isprogram_fd
- program will be attached to particular event and event automatically enabled
-close(event_fd)will detach bpf program from event and event disabledProgram attach point and input arguments:
- programs attached to kprobes receive 'structpt_regs*' as an input. Seetracex4_kern.cthat demonstrates how users can write a C program like:
SEC("events/kprobes/sys_write")
intbpf_prog4(struct pt_regs *regs){ long write_size = regs->dx; // here user need to know the proto of sys_write() from kernel // sources and x64 calling convention to know that register $rdx // contains 3rd argument to sys_write() which is 'size_t count'
Quoted 5 lines
it's obviously architecture dependent, but allows building sophisticated user tools on top, that can see from debug info of vmlinux which variables are in which registers or stack locations and fetch it from there. 'perf probe' can potentialy use this hook to generate programs in user space and insert them instead of letting kernel parse string during kprobe creation.
Actually, this program just shows raw pt_regs for handlers, but I guess it is also possible to pass event arguments from perf probe which given by user and perf-probe. If we can write the script as
int bpf_prog4(s64 write_size)
{
...
}
This will be much easier to play with.
Quoted 16 lines
- programs attached to tracepoints and syscalls receive 'struct
bpf_context*':
u64 arg1, arg2, ..., arg6;
for syscalls they match syscall arguments.
for tracepoints these args match arguments passed to tracepoint. For example:
trace_sched_migrate_task(p, new_cpu); fromsched/core.c
arg1 <- p which is 'structtask_struct*'
arg2 <-new_cpuwhich is 'unsigned int'
arg3..arg6 = 0
the program can use bpf_fetch_u8/16/32/64/ptr() helpers to walk 'task_struct' or any other kernel data structures.
These helpers are usingprobe_kernel_read()similar to 'perf probe' which is not 100% safe in both cases, but good enough.
To access task_struct's pid inside 'sched_migrate_task' tracepoint the program can do:
structtask_struct*task = (structtask_struct*)ctx->arg1;
Quoted 8 lines
u32 pid =
bpf_fetch_u32(&task->pid);
Since struct layout is kernel configuration specific such programs are not portable and require access to kernel headers to be compiled, but in this case we don't need debug info.
llvm with bpf backend will statically computetask->pidoffset as a constant based on kernel headers only.
The example of this arbitrary pointer walking istracex1_kern.cwhich doesskb->dev->name== "lo" filtering.
At least I would like to see this way on kprobes event too, since it should be treated as a traceevent.
Quoted 18 lines
In all cases the programs are called before trace buffer is allocated to minimize the overhead, since we want to filter huge number of events, but buffer alloc/free and argument copy for every event is too costly. Theoretically we can invoke programs after buffer is allocated, but it doesn't seem needed, since above approach is faster and achieves the same.
Note, tracepoint/syscall and kprobe programs are two different types:
BPF_PROG_TYPE_TRACING_FILTERandBPF_PROG_TYPE_KPROBE_FILTER, since they expect different input.
Both use the same set of helper functions:
- map access (lookup/update/delete)
- fetch (probe_kernel_readwrappers)
- memcmp (probe_kernel_read+ memcmp)
-dump_stack
-trace_printk
The last two are mainly to debug the programs and to print data for user space consumptions.
Quoted 4 lines
Portability:
- kprobe programs are architecture dependent and need user scripting language like ktap/stap/dtrace/perf that will dynamically generate them based on debug info in vmlinux
If we can use kprobe event as a normal traceevent, user scripting can be architecture independent too. Only perf-probe fills the gap. All other userspace tools can collaborate with perf-probe to setup the events. If so, we can avoid redundant works on debuginfo. That is my point.
Thank you,
Quoted 94 lines
- tracepoint programs are architecture independent, but if arbitrary pointer walking (with
fetch()helpers) is used, they need data struct layout to match. Debug info is not necessary
- for networking use case we need to access 'structsk_buff' fields in portable way (user space needs to fetch packet length without knowingskb->lenoffset), so for some frequently used data structures we will add helper functions or pseudo instructions to access them. I've hacked few ways specifically for skb, but abandoned them in favor of more generic type/field infra. That work is still wip. Not part of this set.
Once it's ready tracepoint programs that access common data structs will be kernel independent.Program return value:
- programs return 0 to discard an event
- and return non-zero to proceed with event (allocate trace buffer, copy arguments there and print it eventually intrace_pipein traditional way)Examples:
-dropmon.c- simplekfree_skb()accounting in eBPF assembler, similar to dropmon tool
-tracex1_kern.c- doesnet/netif_receive_skbevent filtering fordev->skb->name== "lo" condition
-tracex2_kern.c- samekfree_skb()accounting like dropmon, but now in C plus computes histogram of all write sizes fromsys_writesyscall and prints the histogram in userspace
-tracex3_kern.c- most sophisticated example that computes IO latency betweenblock/block_rq_issueandblock/block_rq_completeevents and prints 'heatmap' using gray shades of text terminal. Useful to analyze disk performance.
-tracex4_kern.c- computes histogram of write sizes fromsys_writesyscall using kprobe mechanism instead of syscall. Since kprobe is optimized into ftrace the overhead of instrumentation is smaller than in example 2.The user space tools like ktap/dtrace/systemptap/perf that has access to debug info would probably want to use kprobe attachment point, since kprobe can be inserted anywhere and all registers are avaiable in the program. tracepoint attachments are useful without debug info, so standalone tools like iosnoop will use them.
The main difference vs existing perf_probe/ftrace infra is in kernel aggregation and conditional walking of arbitrary data structures.
Thanks!
Alexei Starovoitov (9):
tracing: attach eBPF programs to tracepoints and syscalls
tracing: allow eBPF programs to callbpf_printk()
tracing: allow eBPF programs to callktime_get_ns()
samples: bpf: simple tracing example in eBPF assembler
samples: bpf: simple tracing example in C
samples: bpf: counting example forkfree_skbtracepoint and write
syscall
samples: bpf: IO latency analysis (iosnoop/heatmap)
tracing: attach eBPF programs to kprobe/kretprobe
samples: bpf: simple kprobe exampleinclude/linux/ftrace_event.h | 6 + include/trace/bpf_trace.h | 25 ++++ include/trace/ftrace.h | 30 +++++ include/uapi/linux/bpf.h | 11 ++ kernel/trace/Kconfig | 1 + kernel/trace/Makefile | 1 + kernel/trace/bpf_trace.c | 250 ++++++++++++++++++++++++++++++++++++ kernel/trace/trace.h | 3 + kernel/trace/trace_events.c | 41 +++++- kernel/trace/trace_events_filter.c | 80 +++++++++++- kernel/trace/trace_kprobe.c | 11 +- kernel/trace/trace_syscalls.c | 31 +++++ samples/bpf/Makefile | 18 +++ samples/bpf/bpf_helpers.h | 18 +++ samples/bpf/bpf_load.c | 62 ++++++++- samples/bpf/bpf_load.h | 3 + samples/bpf/dropmon.c | 129 +++++++++++++++++++ samples/bpf/tracex1_kern.c | 28 ++++ samples/bpf/tracex1_user.c | 24 ++++ samples/bpf/tracex2_kern.c | 71 ++++++++++ samples/bpf/tracex2_user.c | 95 ++++++++++++++ samples/bpf/tracex3_kern.c | 96 ++++++++++++++ samples/bpf/tracex3_user.c | 146 +++++++++++++++++++++ samples/bpf/tracex4_kern.c | 36 ++++++ samples/bpf/tracex4_user.c | 83 ++++++++++++ 25 files changed, 1290 insertions(+), 9 deletions(-) create mode 100644 include/trace/bpf_trace.h create mode 100644 kernel/trace/bpf_trace.c create mode 100644 samples/bpf/dropmon.c create mode 100644 samples/bpf/tracex1_kern.c create mode 100644 samples/bpf/tracex1_user.c create mode 100644 samples/bpf/tracex2_kern.c create mode 100644 samples/bpf/tracex2_user.c create mode 100644 samples/bpf/tracex3_kern.c create mode 100644 samples/bpf/tracex3_user.c create mode 100644 samples/bpf/tracex4_kern.c create mode 100644 samples/bpf/tracex4_user.c
Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Quoted 8 lines · Masami Hiramatsu
If we can write the script as
int
bpf_prog4(s64 write_size){ ... }This will be much easier to play with.
yes. that's the intent for user space to do.
Quoted 5 lines
Quoted 2 lines
The example of this arbitrary pointer walking is
tracex1_kern.cwhich doesskb->dev->name== "lo" filtering.At least I would like to see this way on kprobes event too, since it should be treated as a traceevent.
it's done already... one can do the same skb->dev->name logic in kprobe attached program... so from bpf program point of view, tracepoints and kprobes feature-wise are exactly the same. Only input is different.
Quoted 8 lines
Quoted 3 lines
- kprobe programs are architecture dependent and need user scripting language like ktap/stap/dtrace/perf that will dynamically generate them based on debug info in vmlinux
If we can use kprobe event as a normal traceevent, user scripting can be architecture independent too. Only perf-probe fills the gap. All other userspace tools can collaborate with perf-probe to setup the events. If so, we can avoid redundant works on debuginfo. That is my point.
yes. perf already has infra to read debug info and it can be extended to understand C like script as:
int kprobe:sys_write(int fd, char *buf, size_t count)
{
// do stuff with 'count'
}
perf can be made to parse this text, recognize that it wants to create kprobe on 'sys_write' function. Then based on debuginfo figure out where 'count' is (either register or stack) and generate corresponding bpf program either
using llvm/gcc backends or directly.
perf facility of extracting debug info can be made into library too and used by ktap/dtrace tools for their languages.
User space can innovate in many directions.
and, yes, once we have a scripting language whether
it's C like with perf or else, this language hides architecture depend things from users.
Such scripting language will also hide the kernel
side differences between tracepoint and kprobe.
Just look how ktap scripts look alike for kprobes and tracepoints. Whether ktap syntax becomes part of perf or perf invents its own language, it's going to be good for users regardless. The C examples here are just examples. Something
users can play with already until more user friendly
tools are being worked on.
Quoted 16 lines · Alexei Starovoitov
Quoted 8 lines · Masami Hiramatsu
If we can write the script as
int
bpf_prog4(s64 write_size){ ... }This will be much easier to play with.
yes. that's the intent for user space to do.
Quoted 3 lines
Quoted 2 lines
The example of this arbitrary pointer walking is
tracex1_kern.cwhich doesskb->dev->name== "lo" filtering.
Quoted 7 lines
Quoted 2 lines
At least I would like to see this way on kprobes event too, since it should be treated as a traceevent.
it's done already... one can do the same
skb->dev->namelogic in kprobe attached program... so from bpf program point of view, tracepoints and kprobes feature-wise are exactly the same. Only input is different.
No, I meant that the input should also be same, at least for the first step. I guess it is easy to hook the ring buffer committing and fetch arguments from the event entry.
Quoted 12 lines
Quoted 8 lines
Quoted 3 lines
- kprobe programs are architecture dependent and need user scripting language like ktap/stap/dtrace/perf that will dynamically generate them based on debug info in vmlinux
If we can use kprobe event as a normal traceevent, user scripting can be architecture independent too. Only perf-probe fills the gap. All other userspace tools can collaborate with perf-probe to setup the events. If so, we can avoid redundant works on debuginfo. That is my point.
yes. perf already has infra to read debug info and it can be extended to understand C like script as:
int kprobe:sys_write(int fd, char *buf,size_tcount)
Quoted 8 lines
{ // do stuff with 'count' }perf can be made to parse this text, recognize that it wants to create kprobe on '
sys_write' function. Then based on debuginfo figure out where 'count' is (either register or stack) and generate corresponding bpf program either
using llvm/gcc backends or directly.
And what I expected scenario was
1. setup kprobe traceevent with fd, buf, count by using perf-probe.
2. load bpf module
3. the module processes given event arguments.
Quoted 9 lines
perf facility of extracting debug info can be made into library too and used by ktap/dtrace tools for their languages.
User space can innovate in many directions.
and, yes, once we have a scripting language whether it's C like with perf or else, this language hides architecture depend things from users.
Such scripting language will also hide the kernel side differences between tracepoint and kprobe.
Hmm, it sounds making another systemtap on top of tracepoint and kprobes. Why don't you just reuse the existing facilities (perftools and ftrace) instead of co-exist?
Quoted 1 line
Just look how ktap scripts look alike for kprobes and tracepoints.
Ktap is a good example, it provides only a language parser and a runtime engine. Actually, currently it lacks a feature to execute "perf-probe" helper from script, but it is easy to add such feature.
Jovi, if you hire perf-probe helper, you could do
trace probe:do_sys_open dfd fname flags mode {
...
}
instead of
trace probe:do_sys_open dfd=%di fname=%dx flags=%cx mode=+4($stack) {
...
}
For this usecase, I've made --output option for perf probe
https://lkml.org/lkml/2014/10/31/210
It currently stopped, but easy to resume on the latest perf.
Thank you,
Quoted 5 lines
Whether ktap syntax becomes part of perf or perf invents its own language, it's going to be good for users regardless. The C examples here are just examples. Something users can play with already until more user friendly tools are being worked on.
Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Research Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]