perf tools: Support uBPF script
Subsystems (3)
RFC PATCH 2/13 tools: Add ubpf feature test
In reply to Wang NanAdd test-ubpf.c to find libubpf, which can be found from [1].
ubpf is user space BPF engine. Following commits will utilize ubpf to make perf dynamically run some profiling scripts in user space.
[1] https://github.com/iovisor/ubpf.git
--- tools/build/Makefile.feature | 6 ++++-- tools/build/feature/Makefile | 6 +++++- tools/build/feature/test-ubpf.c | 11 +++++++++++ tools/lib/bpf/Makefile | 4 ++-- 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 tools/build/feature/test-ubpf.c
tools/build/Makefile.feature +4 -2
@@ -56,7 +56,8 @@ FEATURE_TESTS_BASIC := \ zlib \ lzma \ get_cpuid \ - bpf + bpf \ + ubpf # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list # of all feature tests
@@ -96,7 +97,8 @@ FEATURE_DISPLAY ?= \ zlib \ lzma \ get_cpuid \ - bpf + bpf \ + ubpf # Set FEATURE_CHECK_(C|LD)FLAGS-all for all FEATURE_TESTS features. # If in the future we need per-feature checks/flags for features not
tools/build/feature/Makefile +5 -1
@@ -37,7 +37,8 @@ FILES= \ test-zlib.bin \ test-lzma.bin \ test-bpf.bin \ - test-get_cpuid.bin + test-get_cpuid.bin \ + test-ubpf.bin FILES := $(addprefix $(OUTPUT),$(FILES))
@@ -182,6 +183,9 @@ $(OUTPUT)test-get_cpuid.bin: $(OUTPUT)test-bpf.bin: $(BUILD) +$(OUTPUT)test-ubpf.bin: + $(BUILD) -lubpf + -include $(OUTPUT)*.d ###############################
tools/build/feature/test-ubpf.c new file +11 -0
@@ -0,0 +1,11 @@ +#include <stdlib.h> +#include <ubpf.h> + +int main (void ) +{ +struct ubpf_vm *vm; + + vm =ubpf_create (); +ubpf_destroy (vm); +return 0 ; +}
tools/lib/bpf/Makefile +2 -2
@@ -65,8 +65,8 @@ ifndef VERBOSE endif FEATURE_USER = .libbpf -FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf -FEATURE_DISPLAY = libelf bpf +FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf ubpf +FEATURE_DISPLAY = libelf bpf ubpf INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
Signature · 1 line
1.8.3.4
RFC PATCH 1/13 bpf tools: Add map related BPF helper
In reply to Wang NanAdd BPF map helpers so user can read from bpf map in user space. Following commits will introduce user space BPF, bpf_map will be used to pass information between kernel and user BPF programs.
--- tools/lib/bpf/bpf.c | 24 ++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 2 ++ 2 files changed, 26 insertions(+)
tools/lib/bpf/bpf.c +24 -0
@@ -97,3 +97,27 @@ int bpf_map_update_elem(int fd, void *key, void *value,return sys_bpf (BPF_MAP_UPDATE_ELEM , &attr,sizeof (attr)); } + +int bpf_map_lookup_elem (int fd,void *key,void *value) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.map_fd = fd; + attr.key =ptr_to_u64 (key); + attr.value =ptr_to_u64 (value); + +return sys_bpf (BPF_MAP_LOOKUP_ELEM , &attr,sizeof (attr)); +} + +int bpf_map_get_next_key (int fd,void *key,void *next_key) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.map_fd = fd; + attr.key =ptr_to_u64 (key); + attr.next_key =ptr_to_u64 (next_key); + +return sys_bpf (BPF_MAP_GET_NEXT_KEY , &attr,sizeof (attr)); +}
tools/lib/bpf/bpf.h +2 -0
@@ -22,4 +22,6 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns,int bpf_map_update_elem (int fd,void *key,void *value,u64 flags); +int bpf_map_lookup_elem (int fd,void *key,void *value); +int bpf_map_get_next_key (int fd,void *key,void *next_key);#endif
Signature · 1 line
1.8.3.4
RFC PATCH 7/13 bpf tools: Load ubpf program
In reply to Wang NanIn bpf_program__load(), load ubpf program according to its engine type.
API is improvemented to hold 'struct ubpf_vm *'.
--- tools/lib/bpf/libbpf.c | 66 +++++++++++++++++++++++++++++++++++++++----------- tools/lib/bpf/libbpf.h | 11 +++++++-- 2 files changed, 61 insertions(+), 16 deletions(-)
tools/lib/bpf/libbpf.c +52 -14
@@ -81,6 +81,7 @@ static const char *libbpf_strerror_table[NR_ERRNO] = { [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", [ERRCODE_OFFSET(NOUBPF)] = "UBPF support is not compiled", + [ERRCODE_OFFSET(LOADUBPF)] = "Failed to load user space BPF program", };int libbpf_strerror (int err,char *buf,size_t size)
@@ -949,6 +950,31 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)return 0 ; } +#ifdef HAVE_UBPF_SUPPORT +static int +load_ubpf_program (struct bpf_insn *insns,int insns_cnt, +struct ubpf_vm **pvm) +{ +struct ubpf_vm *vm =ubpf_create (); +char *message; +int err; + +if (!vm) { +pr_warning ("Failed to create ubpf vm\n" ); +return -LIBBPF_ERRNO__LOADUBPF ; + } + + err =ubpf_load (vm, insns, insns_cnt *sizeof (insns[0 ]), &message); +if (err <0 ) { +pr_warning ("Failed to load ubpf program: %s\n" , message); +return -LIBBPF_ERRNO__LOADUBPF ; + } + + *pvm = vm; +return 0 ; +} +#endif +static int load_program (struct bpf_insn *insns,int insns_cnt,char *license,u32 kern_version,int *pfd)
@@ -1002,11 +1028,12 @@ bpf_program__load(struct bpf_program *prog, char *license, u32 kern_version) { int err = 0, fd, i; +#ifdef HAVE_UBPF_SUPPORT +struct ubpf_vm *vm; +#endif if (prog->engine ==ENGINE_UNKNOWN ) prog->engine =ENGINE_KBPF ; -if (prog->engine !=ENGINE_KBPF ) -return -EINVAL ;if (prog->instances.nr <0 || !prog->instances.array) {if (prog->preprocessor) {
@@ -1029,10 +1056,15 @@ bpf_program__load(struct bpf_program *prog,pr_warning ("Program '%s' is inconsistent: nr(%d) != 1\n" , prog->section_name, prog->instances.nr); } - err =load_program (prog->insns, prog->insns_cnt, - license, kern_version, &fd); + +if_engine (prog, + (err =load_program (prog->insns, prog->insns_cnt, + license, kern_version, &fd)), + (err =load_ubpf_program (prog->insns, prog->insns_cnt, + &vm))); +if (!err) - prog->instan_fd (0 ) = fd; +set_instance (prog,0 , fd, vm); goto out; }
@@ -1052,15 +1084,21 @@ bpf_program__load(struct bpf_program *prog,if (!result.new_insn_ptr || !result.new_insn_cnt) {pr_debug ("Skip loading the %dth instance of program '%s'\n" , i, prog->section_name); - prog->instan_fd (i) =-1 ; -if (result.pfd) - *result.pfd =-1 ; + +if_engine (prog, prog->instan_fd (i) =-1 , + prog->instan_vm (i) =NULL ); +if (result.ptr) +if_engine (prog, *result.pfd =-1 , *result.pvm =NULL );continue ; } - err =load_program (result.new_insn_ptr, - result.new_insn_cnt, - license, kern_version, &fd); +if_engine (prog, + (err =load_program (result.new_insn_ptr, + result.new_insn_cnt, + license, kern_version, &fd)), + (err =load_ubpf_program (result.new_insn_ptr, + result.new_insn_cnt, + &vm)));if (err) { pr_warning("Loading the %dth instance of program '%s' failed\n",
@@ -1068,9 +1106,9 @@ bpf_program__load(struct bpf_program *prog, goto out; } -if (result.pfd) - *result.pfd = fd; - prog->instan_fd (i) = fd; +if (result.ptr) +if_engine (prog, *result.pfd = fd, *result.pvm = vm); +set_instance (prog, i, fd, vm); } out:if (err)
tools/lib/bpf/libbpf.h +9 -2
@@ -27,6 +27,7 @@ enum libbpf_errno {LIBBPF_ERRNO__PROG2BIG ,/* Program too big */ LIBBPF_ERRNO__KVER ,/* Incorrect kernel version */ LIBBPF_ERRNO__NOUBPF ,/* UBPF support is not compiled */ +LIBBPF_ERRNO__LOADUBPF ,/* Failed to load user space BPF program */ __LIBBPF_ERRNO__END, };
@@ -123,6 +124,8 @@ struct bpf_insn;* bpf_program__nth_fd(prog, 0). */ +struct ubpf_vm ; +struct bpf_prog_prep_result {/* * If not NULL, load new instruction array.
@@ -131,8 +134,12 @@ struct bpf_prog_prep_result {struct bpf_insn *new_insn_ptr;int new_insn_cnt; -/* If not NULL, result fd is set to it */ -int *pfd; +/* If not NULL, result is set to it */ +union { +void *ptr; +int *pfd; +struct ubpf_vm **pvm; + }; };/*
Signature · 1 line
1.8.3.4
RFC PATCH 9/13 bpf tools: Register extern functions for ubpf programs
In reply to Wang NanIntroduce libbpf_register_ubpf_func(), allow caller (perf) to register extern functions. Registered functions are stored into an array and registered to each ubpf_vm when loading.
--- tools/lib/bpf/libbpf.c | 38 +++++++++++++++++++++++++++++++++++++- tools/lib/bpf/libbpf.h | 4 ++++ 2 files changed, 41 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +37 -1
@@ -951,19 +951,48 @@ static int bpf_object__collect_reloc(struct bpf_object *obj) }#ifdef HAVE_UBPF_SUPPORT +static struct { +const char *name; +void *func; +} ubpf_funcs[MAX_UBPF_FUNC ]; + +int libbpf_register_ubpf_func (unsigned int idx,const char *name,void *func) +{ +if (idx >=MAX_UBPF_FUNC ) +return -E2BIG ; +if (!func) +return -EINVAL ; + + ubpf_funcs[idx].name = name; + ubpf_funcs[idx].func = func; +return 0 ; +} +static int load_ubpf_program (struct bpf_insn *insns,int insns_cnt,struct ubpf_vm **pvm) {struct ubpf_vm *vm =ubpf_create ();char *message; -int err; +int i, err;if (!vm) {pr_warning ("Failed to create ubpf vm\n" );return -LIBBPF_ERRNO__LOADUBPF ; } +for (i =0 ; i <MAX_UBPF_FUNC ; i++) { +if (ubpf_funcs[i].func) { + err =ubpf_register (vm, i, ubpf_funcs[i].name, + ubpf_funcs[i].func); +if (err) { +pr_warning ("Failed to register %s\n" , + ubpf_funcs[i].name); +return -LIBBPF_ERRNO__LOADUBPF ; + } + } + } + err =ubpf_load (vm, insns, insns_cnt *sizeof (insns[0 ]), &message);if (err <0 ) {pr_warning ("Failed to load ubpf program: %s\n" , message);
@@ -973,6 +1002,13 @@ load_ubpf_program(struct bpf_insn *insns, int insns_cnt, *pvm = vm;return 0 ; } +#else +int libbpf_register_ubpf_func (unsigned int idx__maybe_unused , +const char *name__maybe_unused , +void *func__maybe_unused ) +{ +return -LIBBPF_ERRNO__NOUBPF ; +}#endif static int
tools/lib/bpf/libbpf.h +4 -0
@@ -45,6 +45,10 @@ void libbpf_set_print(libbpf_print_fn_t warn,libbpf_print_fn_t info, libbpf_print_fn_t debug); +#define MAX_UBPF_FUNC 64 + +int libbpf_register_ubpf_func (unsigned int idx,const char *name,void *func); +/* Hide internal to user */ struct bpf_object ;
Signature · 1 line
1.8.3.4
RFC PATCH 11/13 perf bpf: Accept ubpf programs
In reply to Wang NanThis patch allows user passing a UBPF function through bpf file with section name leading with 'UBPF;'. For example:
#define SEC(NAME) __attribute__((section(NAME), used)) SEC("UBPF;perf_record_exit")
void record_exit(int *samples)
{
char fmt[] = "Hello! receive %d samples\n";
ubpf_printf(fmt, *samples);}
--- tools/perf/util/bpf-loader.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
tools/perf/util/bpf-loader.c +22 -0
@@ -305,6 +305,19 @@ config_bpf_program(struct bpf_program *prog)return PTR_ERR (config_str); } +if (strncmp (config_str,"UBPF;" ,5 ) ==0 ) { +pr_debug ("bpf: load a UBPF program %s\n" , config_str); + err =bpf_program__set_ubpf (prog); +if (err) { +char errbuf[BUFSIZ ]; + +libbpf_strerror (err, errbuf,sizeof (errbuf)); +pr_warning ("Set %s to ubpf failed: %s\n" , config_str, errbuf); +return err; + } +return 0 ; + } + priv =calloc (sizeof (*priv),1 );if (!priv) {pr_debug ("bpf: failed to alloc priv\n" );
@@ -607,6 +620,9 @@ int bpf__probe(struct bpf_object *obj)if (err) goto out; +if (bpf_program__is_ubpf (prog)) +continue ; + err =bpf_program__get_private (prog, (void **)&priv);if (err || !priv) goto out;
@@ -650,6 +666,9 @@ int bpf__unprobe(struct bpf_object *obj)bpf_object__for_each_program (prog, obj) {int i; +if (bpf_program__is_ubpf (prog)) +continue ; + err =bpf_program__get_private (prog, (void **)&priv);if (err || !priv)continue ;
@@ -707,6 +726,9 @@ int bpf__foreach_tev(struct bpf_object *obj,struct bpf_prog_priv *priv;int i, fd; +if (bpf_program__is_ubpf (prog)) +continue ; + err =bpf_program__get_private (prog, (void **)&priv);if (err || !priv) {
Signature · 1 line
1.8.3.4
RFC PATCH 13/13 perf tests: Add UBPF test case
In reply to Wang NanIntroduce a BPF script use UBPF, test compiling, helper and hook. Validate passing information through helper and hooks.
--- tools/perf/tests/Build | 8 ++++ tools/perf/tests/bpf-script-test-ubpf.c | 68 +++++++++++++++++++++++++++++ tools/perf/tests/bpf.c | 77 ++++++++++++++++++++++++++++++++- tools/perf/tests/llvm.c | 4 ++ tools/perf/tests/llvm.h | 2 + 5 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 tools/perf/tests/bpf-script-test-ubpf.c
tools/perf/tests/Build +8 -0
@@ -32,6 +32,7 @@ perf-y += parse-no-sample-id-all.o perf-y += kmod-path.o perf-y += thread-map.o perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o llvm-src-prologue.o llvm-src-relocation.o +perf-y += llvm-src-ubpf.o perf-y += bpf.o perf-y += topology.o perf-y += cpumap.o
@@ -67,6 +68,13 @@ $(OUTPUT)tests/llvm-src-relocation.c: tests/bpf-script-test-relocation.c tests/B $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ $(Q)echo ';' >> $@ +$(OUTPUT)tests/llvm-src-ubpf.c: tests/bpf-script-test-ubpf.c tests/Build + $(call rule_mkdir) + $(Q)echo '#include <tests/llvm.h>' > $@ + $(Q)echo 'const char test_llvm__bpf_test_ubpf[] =' >> $@ + $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ + $(Q)echo ';' >> $@ + ifeq ($(ARCH),$(filter $(ARCH),x86 arm arm64)) perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o endif
tools/perf/tests/bpf-script-test-ubpf.c new file +68 -0
68 lines not on this page · raw message
tools/perf/tests/bpf.c +75 -2
145 lines not on this page · raw message
tools/perf/tests/llvm.c +4 -0
@@ -48,6 +48,10 @@ static struct { .desc = "Compile source for BPF relocation test", .should_load_fail = true, }, + [LLVM_TESTCASE_BPF_UBPF] = { + .source = test_llvm__bpf_test_ubpf, + .desc = "Compile source for UBPF test", + }, };int
tools/perf/tests/llvm.h +2 -0
@@ -8,12 +8,14 @@ extern const char test_llvm__bpf_base_prog[];extern const char test_llvm__bpf_test_kbuild_prog[];extern const char test_llvm__bpf_test_prologue_prog[];extern const char test_llvm__bpf_test_relocation[]; +extern const char test_llvm__bpf_test_ubpf[];enum test_llvm__testcase {LLVM_TESTCASE_BASE ,LLVM_TESTCASE_KBUILD ,LLVM_TESTCASE_BPF_PROLOGUE ,LLVM_TESTCASE_BPF_RELOCATION , +LLVM_TESTCASE_BPF_UBPF , __LLVM_TESTCASE_MAX, };
Signature · 1 line
1.8.3.4
RFC PATCH 4/13 bpf tools: Replace fd array to union array
In reply to Wang NanFollowing commits will add new types for instance of a bpf program. For kernel bpf program, a program instance is fd. For ubpf program, the instance of a program is a 'struct ubpf_vm'. This patch promote original 'fds' array to 'union prog_instance' array, prepare for further extending.
--- tools/lib/bpf/libbpf.c | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-)
tools/lib/bpf/libbpf.c +24 -20
@@ -138,6 +138,10 @@ int libbpf_strerror(int err, char *buf, size_t size)# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ#endif +union prog_instance { +int fd; +}; +/* * bpf_prog should be a better name but it has been used in * linux/filter.h.
@@ -157,7 +161,7 @@ struct bpf_program {struct {int nr; -int *fds; +union prog_instance *array; } instances;bpf_program_prep_t preprocessor;
@@ -229,14 +233,14 @@ static void bpf_program__unload(struct bpf_program *prog)*/ if (prog->instances.nr >0 ) {for (i =0 ; i < prog->instances.nr; i++) -zclose (prog->instances.fds[i]); +zclose (prog->instances.array[i].fd); }else if (prog->instances.nr !=-1 ) {pr_warning ("Internal error: instances.nr is %d\n" , prog->instances.nr); } prog->instances.nr =-1 ; -zfree (&prog->instances.fds); +zfree (&prog->instances.array); }static void bpf_program__exit (struct bpf_program *prog)
@@ -287,7 +291,7 @@ bpf_program__init(void *data, size_t size, char *name, int idx,memcpy (prog->insns, data, prog->insns_cnt *sizeof (struct bpf_insn )); prog->idx = idx; - prog->instances.fds =NULL ; + prog->instances.array =NULL ; prog->instances.nr =-1 ;return 0 ;
@@ -937,20 +941,20 @@ bpf_program__load(struct bpf_program *prog, {int err =0 , fd, i; -if (prog->instances.nr <0 || !prog->instances.fds) { +if (prog->instances.nr <0 || !prog->instances.array) {if (prog->preprocessor) {pr_warning ("Internal error: can't load program '%s'\n" , prog->section_name);return -LIBBPF_ERRNO__INTERNAL ; } - prog->instances.fds =malloc (sizeof (int )); -if (!prog->instances.fds) { -pr_warning ("Not enough memory for BPF fds\n" ); + prog->instances.array =malloc (sizeof (union prog_instance )); +if (!prog->instances.array) { +pr_warning ("Not enough memory for BPF instance\n" );return -ENOMEM ; } prog->instances.nr =1 ; - prog->instances.fds[0 ] =-1 ; + prog->instances.array[0 ].fd =-1 ; }if (!prog->preprocessor) {
@@ -961,7 +965,7 @@ bpf_program__load(struct bpf_program *prog, err =load_program (prog->insns, prog->insns_cnt, license, kern_version, &fd);if (!err) - prog->instances.fds[0 ] = fd; + prog->instances.array[0 ].fd = fd; goto out; }
@@ -981,7 +985,7 @@ bpf_program__load(struct bpf_program *prog,if (!result.new_insn_ptr || !result.new_insn_cnt) {pr_debug ("Skip loading the %dth instance of program '%s'\n" , i, prog->section_name); - prog->instances.fds[i] =-1 ; + prog->instances.array[i].fd =-1 ;if (result.pfd) *result.pfd =-1 ;continue ;
@@ -999,7 +1003,7 @@ bpf_program__load(struct bpf_program *prog,if (result.pfd) *result.pfd = fd; - prog->instances.fds[i] = fd; + prog->instances.array[i].fd = fd; } out:if (err)
@@ -1269,27 +1273,27 @@ int bpf_program__fd(struct bpf_program *prog)int bpf_program__set_prep (struct bpf_program *prog,int nr_instances,bpf_program_prep_t prep) { -int *instances_fds; +union prog_instance *array;if (nr_instances <=0 || !prep)return -EINVAL ; -if (prog->instances.nr >0 || prog->instances.fds) { +if (prog->instances.nr >0 || prog->instances.array) {pr_warning ("Can't set pre-processor after loading\n" );return -EINVAL ; } - instances_fds =malloc (sizeof (int ) * nr_instances); -if (!instances_fds) { -pr_warning ("alloc memory failed for fds\n" ); + array =malloc (sizeof (array[0 ]) * nr_instances); +if (!array) { +pr_warning ("alloc memory failed for instances\n" );return -ENOMEM ; }/* fill all fd with -1 */ -memset (instances_fds,-1 ,sizeof (int ) * nr_instances); +memset (array,-1 ,sizeof (array[0 ]) * nr_instances); prog->instances.nr = nr_instances; - prog->instances.fds = instances_fds; + prog->instances.array = array; prog->preprocessor = prep;return 0 ; }
@@ -1304,7 +1308,7 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)return -EINVAL ; } - fd = prog->instances.fds[n]; + fd = prog->instances.array[n].fd;if (fd <0 ) {pr_warning ("%dth instance of program '%s' is invalid\n" , n, prog->section_name);
Signature · 1 line
1.8.3.4
RFC PATCH 6/13 bpf tools: Introduce ubpf_vm to program instance union
In reply to Wang NanAdd 'struct ubpf_vm *' into prog_instance union. Introduce if_engine() macro to merge common code.
--- tools/lib/bpf/libbpf.c | 76 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 8 deletions(-)
tools/lib/bpf/libbpf.c +68 -8
@@ -141,6 +141,9 @@ int libbpf_strerror(int err, char *buf, size_t size)union prog_instance {int fd; +#ifdef HAVE_UBPF_SUPPORT +struct ubpf_vm *vm; +#endif };/*
@@ -176,6 +179,56 @@ struct bpf_program {bpf_program_clear_priv_t clear_priv; }; +#ifdef HAVE_UBPF_SUPPORT +# define __if_engine (p, d, k, u) \ + do { \ + switch (p->engine) { \ + default: \ + case ENGINE_UNKNOWN: { \ + d; \ + } \ + case ENGINE_KBPF: { \ + k; break; \ + } \ + case ENGINE_UBPF: { \ + u; break; \ + } \ + } \ + } while(0) + +/* +* ubpf_destroy() doesn't accept NULL input. This wrapper makes +* it similar to zclose. +*/ +# define __ubpf_destroy (vm) do { \ + if (vm) \ + ubpf_destroy(vm); \ + (vm) = NULL; \ +} while(0) + +#else +# define __if_engine (p, d, k, u) \ + do { k; } while(0) +#endif + +#define instan_fd (i) instances.array[i].fd +#define instan_vm (i) instances.array[i].vm + +#define if_engine (p, k, u) __if_engine(p, do { } while(0), k, u) +#define set_instance (p, i, k, u) \ + if_engine(p, \ + p->instan_fd(i) = k, \ + p->instan_vm(i) = u) + +static inline void init_instance_array (struct bpf_program *prog) +{ +size_t size =sizeof (prog->instances.array[0 ]) * prog->instances.nr; + +if_engine (prog, +memset (prog->instances.array,-1 , size), +memset (prog->instances.array,0 , size)); +} +struct bpf_map {int fd;char *name;
@@ -239,7 +292,9 @@ static void bpf_program__unload(struct bpf_program *prog)*/ if (prog->instances.nr >0 ) {for (i =0 ; i < prog->instances.nr; i++) -zclose (prog->instances.array[i].fd); +if_engine (prog, +zclose (prog->instan_fd (i)), +__ubpf_destroy (prog->instan_vm (i))); }else if (prog->instances.nr !=-1 ) {pr_warning ("Internal error: instances.nr is %d\n" , prog->instances.nr);
@@ -966,7 +1021,7 @@ bpf_program__load(struct bpf_program *prog,return -ENOMEM ; } prog->instances.nr =1 ; - prog->instances.array[0 ].fd =-1 ; +set_instance (prog,0 ,-1 ,NULL ); }if (!prog->preprocessor) {
@@ -977,7 +1032,7 @@ bpf_program__load(struct bpf_program *prog, err =load_program (prog->insns, prog->insns_cnt, license, kern_version, &fd);if (!err) - prog->instances.array[0 ].fd = fd; + prog->instan_fd (0 ) = fd; goto out; }
@@ -997,7 +1052,7 @@ bpf_program__load(struct bpf_program *prog,if (!result.new_insn_ptr || !result.new_insn_cnt) {pr_debug ("Skip loading the %dth instance of program '%s'\n" , i, prog->section_name); - prog->instances.array[i].fd =-1 ; + prog->instan_fd (i) =-1 ;if (result.pfd) *result.pfd =-1 ;continue ;
@@ -1015,7 +1070,7 @@ bpf_program__load(struct bpf_program *prog,if (result.pfd) *result.pfd = fd; - prog->instances.array[i].fd = fd; + prog->instan_fd (i) = fd; } out:if (err)
@@ -1301,12 +1356,11 @@ int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,return -ENOMEM ; } -/* fill all fd with -1 */ -memset (array,-1 ,sizeof (array[0 ]) * nr_instances); - prog->instances.nr = nr_instances; prog->instances.array = array; prog->preprocessor = prep; + +init_instance_array (prog);return 0 ; }
@@ -1314,6 +1368,12 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n) {int fd; +if (prog->engine !=ENGINE_KBPF ) { +pr_warning ("Can't get fd from program %s: engine not KBPF or not loaded\n" , + prog->section_name); +return -EINVAL ; + } +if (n >= prog->instances.nr || n <0 ) {pr_warning ("Can't get the %dth fd from program %s: only %d instances\n" , n, prog->section_name, prog->instances.nr);
Signature · 1 line
1.8.3.4
RFC PATCH 3/13 bpf tools: Add ubpf include and makefile options
In reply to Wang NanPrepare to use libubpf. Add ubpf.h into libbpf.c. Add corresponding makefile options.
Allow ignoring UBPF when building libbpf by setting NO_UBPF=1 for libbpf building.
Allow perf controling ubpf using NO_UBPF=1.
--- tools/lib/bpf/Makefile | 16 ++++++++++++++-- tools/lib/bpf/libbpf.c | 3 +++ tools/perf/Makefile.perf | 2 ++ tools/perf/config/Makefile | 7 +++++++ 4 files changed, 26 insertions(+), 2 deletions(-)
tools/lib/bpf/Makefile +14 -2
@@ -64,9 +64,17 @@ ifndef VERBOSE VERBOSE = 0 endif +ifndef NO_UBPF + FEATURE_UBPF := ubpf + CFLAGS_UBPF := -DHAVE_UBPF_SUPPORT +else + FEATURE_UBPF := + CFLAGS_UBPF := +endif + FEATURE_USER = .libbpf -FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf ubpf -FEATURE_DISPLAY = libelf bpf ubpf +FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf $(FEATURE_UBPF) +FEATURE_DISPLAY = libelf bpf $(FEATURE_UBPF) INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
@@ -120,6 +128,10 @@ ifeq ($(feature-libelf-getphdrnum), 1) override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT endif +ifeq ($(feature-ubpf), 1) + override CFLAGS += $(CFLAGS_UBPF) +endif + # Append required CFLAGS override CFLAGS += $(EXTRA_WARNINGS) override CFLAGS += -Werror -Wall
tools/lib/bpf/libbpf.c +3 -0
@@ -21,6 +21,9 @@#include <libelf.h> #include <gelf.h> +#ifdef HAVE_UBPF_SUPPORT +# include <ubpf.h> +#endif #include "libbpf.h" #include "bpf.h"
tools/perf/Makefile.perf +2 -0
@@ -81,6 +81,8 @@ include ../scripts/utilities.mak # # Define NO_LIBBPF if you do not want BPF support # +# Define NO_UBPF if you do not want user space BPF support +# # Define FEATURES_DUMP to provide features detection dump file # and bypass the feature detection
tools/perf/config/Makefile +7 -0
@@ -315,6 +315,13 @@ ifndef NO_LIBELF ifeq ($(feature-bpf), 1) CFLAGS += -DHAVE_LIBBPF_SUPPORT $(call detected,CONFIG_LIBBPF) + ifndef NO_UBPF + ifeq ($(feature-ubpf), 1) + CFLAGS += -DHAVE_UBPF_SUPPORT + EXTLIBS += -lubpf + $(call detected,CONFIG_UBPF) + endif + endif endif ifndef NO_DWARF
Signature · 1 line
1.8.3.4
RFC PATCH 10/13 perf tools: Register basic UBPF helpers
In reply to Wang NanReigster basic extern functions for ubpf programs.
--- tools/perf/perf.c | 3 ++ tools/perf/util/Build | 1 + tools/perf/util/ubpf-helpers-list.h | 7 ++++ tools/perf/util/ubpf-helpers.c | 66 +++++++++++++++++++++++++++++++++++++ tools/perf/util/ubpf-helpers.h | 21 ++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 tools/perf/util/ubpf-helpers-list.h create mode 100644 tools/perf/util/ubpf-helpers.c create mode 100644 tools/perf/util/ubpf-helpers.h
tools/perf/perf.c +3 -0
@@ -16,6 +16,7 @@#include "util/parse-events.h" #include <subcmd/parse-options.h> #include "util/bpf-loader.h" +#include "util/ubpf-helpers.h" #include "util/debug.h" #include <api/fs/tracing_path.h> #include <pthread.h>
@@ -618,6 +619,8 @@ int main(int argc, const char **argv)perf_debug_setup (); +register_ubpf_helpers (); +while (1 ) {static int done_help;int was_alias =run_argv (&argc, &argv);
tools/perf/util/Build +1 -0
@@ -87,6 +87,7 @@ libperf-y += mem-events.o libperf-$(CONFIG_LIBBPF) += bpf-loader.o libperf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o +libperf-$(CONFIG_UBPF) += ubpf-helpers.o libperf-$(CONFIG_LIBELF) += symbol-elf.o libperf-$(CONFIG_LIBELF) += probe-file.o libperf-$(CONFIG_LIBELF) += probe-event.o
tools/perf/util/ubpf-helpers-list.h new file +7 -0
@@ -0,0 +1,7 @@ +DEF_UBPF_HELPER(int, ubpf_memcmp, (void *s1, void *s2, unsigned int n)) +DEF_UBPF_HELPER(void, ubpf_memcpy, (void *d, void *s, unsigned int size)) +DEF_UBPF_HELPER(int, ubpf_strcmp, (char *s1, char *s2)) +DEF_UBPF_HELPER(int, ubpf_printf, (char *fmt, ...)) +DEF_UBPF_HELPER(int, ubpf_map_lookup_elem, (void *map_desc, void *key, void *value)) +DEF_UBPF_HELPER(int, ubpf_map_update_elem, (void *map_desc, void *key, void *value, unsigned long long flags)) +DEF_UBPF_HELPER(int, ubpf_map_get_next_key, (void *map_desc, void *key, void *next_key))
tools/perf/util/ubpf-helpers.c new file +66 -0
66 lines not on this page · raw message
tools/perf/util/ubpf-helpers.h new file +21 -0
@@ -0,0 +1,21 @@ +/* +* Copyright (C) 2016, Wang Nan <[email protected]> +* Copyright (C) 2016, Huawei Inc. +*/ +#ifndef __UBPF_HELPERS_H +#define __UBPF_HELPERS_H + +#define DEF_UBPF_HELPER (type, name, param) UBPF_FUNC_##name, +enum { + UBPF_FUNC_unspec =0 , +#include "ubpf-helpers-list.h" + UBPF_FUNC_MAX +}; +#undef DEF_UBPF_HELPER + +#ifdef HAVE_UBPF_SUPPORT +void register_ubpf_helpers (void ); +#else +inline static void register_ubpf_helpers (void ) {}; +#endif +#endif
Signature · 1 line
1.8.3.4
RFC PATCH 12/13 perf record: Add UBPF hooks at beginning and end of perf record
In reply to Wang NanLike tracepoint in kernel code, UBPF hooks can be added in perf code and trigger UBPF programs passed by BPF scripts. The first two UBPF hooks added are record start/end. UBPF scripts can initial BPF maps in record start, and report result when record finished.
--- tools/perf/builtin-record.c | 4 ++ tools/perf/util/Build | 2 +- tools/perf/util/ubpf-hooks-list.h | 10 +++++ tools/perf/util/ubpf-hooks.c | 81 +++++++++++++++++++++++++++++++++++++++ tools/perf/util/ubpf-hooks.h | 35 +++++++++++++++++ 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 tools/perf/util/ubpf-hooks-list.h create mode 100644 tools/perf/util/ubpf-hooks.c create mode 100644 tools/perf/util/ubpf-hooks.h
tools/perf/builtin-record.c +4 -0
@@ -35,6 +35,7 @@#include "util/llvm-utils.h" #include "util/bpf-loader.h" #include "util/trigger.h" +#include "util/ubpf-hooks.h" #include "asm/bug.h" #include <unistd.h>
@@ -824,6 +825,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)auxtrace_snapshot_ready ();switch_output_ready (); +ubpf_hook_perf_record_start (0 );for (;;) {unsigned long long hits = rec->samples;
@@ -888,6 +890,8 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) disabled = true; } } +ubpf_hook_perf_record_end (0 , rec->samples); +auxtrace_snapshot_off ();switch_output_off ();
tools/perf/util/Build +1 -1
@@ -87,7 +87,7 @@ libperf-y += mem-events.o libperf-$(CONFIG_LIBBPF) += bpf-loader.o libperf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o -libperf-$(CONFIG_UBPF) += ubpf-helpers.o +libperf-$(CONFIG_UBPF) += ubpf-helpers.o ubpf-hooks.o libperf-$(CONFIG_LIBELF) += symbol-elf.o libperf-$(CONFIG_LIBELF) += probe-file.o libperf-$(CONFIG_LIBELF) += probe-event.o
tools/perf/util/ubpf-hooks-list.h new file +10 -0
@@ -0,0 +1,10 @@ +UBPF_HOOK(perf_record_start,,) + +UBPF_HOOK (perf_record_end, +__UBPF_HOOK_ARG ( +__arg (int, samples) + ), +__UBPF_HOOK_ASSIGN ( + __entry.samples = samples; + ) + )
tools/perf/util/ubpf-hooks.c new file +81 -0
81 lines not on this page · raw message
tools/perf/util/ubpf-hooks.h new file +35 -0
@@ -0,0 +1,35 @@ +/* +* Copyright (C) 2016, Wang Nan <[email protected]> +* Copyright (C) 2016, Huawei Inc. +*/ +#ifndef __PERF_UBPF_HOOKS_H +#define __PERF_UBPF_HOOKS_H + +#include <linux/compiler.h> + +#define UBPF_HOOK_BREAKABLE 1 + +#ifdef HAVE_UBPF_SUPPORT + +#define __arg (t, n) , t n +#define __UBPF_HOOK_ARG (args) args +#define UBPF_HOOK (n, args, assign) int ubpf_hook_##n(unsigned long flags args); +#include "ubpf-hooks-list.h" +#undef UBPF_HOOK +#undef __UBPF_HOOK_ARG +#undef __arg + +#else + +#define __arg (t, n) , t n __maybe_unused +#define __UBPF_HOOK_ARG (args) args +#define UBPF_HOOK (n, args, assign) \ +static inline int ubpf_hook_##n(unsigned long flags __maybe_unused args) {return 0;}; +#include "ubpf-hooks-list.h" +#undef UBPF_HOOK +#undef __UBPF_HOOK_ARG +#undef __arg + +#endif + +#endif
Signature · 1 line
1.8.3.4
RFC PATCH 8/13 bpf tools: Add API for fetching ubpf_vm
In reply to Wang NanIntroduce bpf_program__vm and bpf_program__nth_vm for fetching ubpf program instance.
--- tools/lib/bpf/libbpf.c | 38 ++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 2 ++ 2 files changed, 40 insertions(+)
tools/lib/bpf/libbpf.c +38 -0
@@ -1375,6 +1375,11 @@ int bpf_program__fd(struct bpf_program *prog)return bpf_program__nth_fd (prog,0 ); } +struct ubpf_vm *bpf_program__vm (struct bpf_program *prog) +{ +return bpf_program__nth_vm (prog,0 ); +} +int bpf_program__set_prep (struct bpf_program *prog,int nr_instances,bpf_program_prep_t prep) {
@@ -1429,6 +1434,32 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n) }#ifdef HAVE_UBPF_SUPPORT +struct ubpf_vm *bpf_program__nth_vm (struct bpf_program *prog,int n) +{ +struct ubpf_vm *vm; + +if (prog->engine !=ENGINE_UBPF ) { +pr_warning ("Can't get ubpf_vm from program %s: engine not UBPF or not loaded\n" , + prog->section_name); +return ERR_PTR (-EINVAL ); + } + +if (n >= prog->instances.nr || n <0 ) { +pr_warning ("Can't get the %dth vm from program %s: only %d instances\n" , + n, prog->section_name, prog->instances.nr); +return ERR_PTR (-EINVAL ); + } + + vm = prog->instances.array[n].vm; +if (!vm) { +pr_warning ("%dth instance of program '%s' is invalid\n" , + n, prog->section_name); +return ERR_PTR (-ENOENT ); + } + +return vm; +} +int bpf_program__set_ubpf (struct bpf_program *prog) {if (prog->engine !=ENGINE_UNKNOWN ) {
@@ -1454,6 +1485,13 @@ bool bpf_program__is_ubpf(struct bpf_program *prog __maybe_unused) {return false; } + +struct ubpf_vm * +bpf_program__nth_vm (struct bpf_program *prog__maybe_unused , +int n__maybe_unused ) +{ +return ERR_PTR (-LIBBPF_ERRNO__NOUBPF ); +}#endif int bpf_map__get_fd (struct bpf_map *map)
tools/lib/bpf/libbpf.h +2 -0
@@ -92,6 +92,7 @@ int bpf_program__set_ubpf(struct bpf_program *prog);bool bpf_program__is_ubpf (struct bpf_program *prog);int bpf_program__fd (struct bpf_program *prog); +struct ubpf_vm *bpf_program__vm (struct bpf_program *prog);struct bpf_insn ;
@@ -162,6 +163,7 @@ int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, bpf_program_prep_t prep);int bpf_program__nth_fd (struct bpf_program *prog,int n); +struct ubpf_vm *bpf_program__nth_vm (struct bpf_program *prog,int n);/* * We don't need __attribute__((packed)) now since it is
Signature · 1 line
1.8.3.4
RFC PATCH 5/13 bpf tools: Save engine type in bpf_program
In reply to Wang NanAdd an 'engine' field in bpf_program to indicate whether a program is a ubpf program or kernel bpf program. For compatibility, the default engine is kernel bpf, unless explicitly set to ubpf using bpf_program__set_ubpf().
--- tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 4 ++++ 2 files changed, 44 insertions(+)
tools/lib/bpf/libbpf.c +40 -0
@@ -80,6 +80,7 @@ static const char *libbpf_strerror_table[NR_ERRNO] = { [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", + [ERRCODE_OFFSET(NOUBPF)] = "UBPF support is not compiled", };int libbpf_strerror (int err,char *buf,size_t size)
@@ -147,6 +148,11 @@ union prog_instance {* linux/filter.h. */ struct bpf_program { +enum { +ENGINE_UNKNOWN , +ENGINE_KBPF , +ENGINE_UBPF , + } engine;/* Index in elf obj file, for relocation use. */ int idx;char *section_name;
@@ -291,6 +297,7 @@ bpf_program__init(void *data, size_t size, char *name, int idx,memcpy (prog->insns, data, prog->insns_cnt *sizeof (struct bpf_insn )); prog->idx = idx; + prog->engine =ENGINE_UNKNOWN ; prog->instances.array =NULL ; prog->instances.nr =-1 ;
@@ -941,6 +948,11 @@ bpf_program__load(struct bpf_program *prog, {int err =0 , fd, i; +if (prog->engine ==ENGINE_UNKNOWN ) + prog->engine =ENGINE_KBPF ; +if (prog->engine !=ENGINE_KBPF ) +return -EINVAL ; +if (prog->instances.nr <0 || !prog->instances.array) {if (prog->preprocessor) { pr_warning("Internal error: can't load program '%s'\n",
@@ -1318,6 +1330,34 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)return fd; } +#ifdef HAVE_UBPF_SUPPORT +int bpf_program__set_ubpf (struct bpf_program *prog) +{ +if (prog->engine !=ENGINE_UNKNOWN ) { +pr_warning ("Can't set program %s to ubpf\n" , + prog->section_name); +return -EINVAL ; + } + prog->engine =ENGINE_UBPF ; +return 0 ; +} + +bool bpf_program__is_ubpf (struct bpf_program *prog) +{ +return prog->engine ==ENGINE_UBPF ; +} +#else +int bpf_program__set_ubpf (struct bpf_program *prog__maybe_unused ) +{ +return -LIBBPF_ERRNO__NOUBPF ; +} + +bool bpf_program__is_ubpf (struct bpf_program *prog__maybe_unused ) +{ +return false; +} +#endif +int bpf_map__get_fd (struct bpf_map *map) {if (!map)
tools/lib/bpf/libbpf.h +4 -0
@@ -26,6 +26,7 @@ enum libbpf_errno {LIBBPF_ERRNO__VERIFY ,/* Kernel verifier blocks program loading */ LIBBPF_ERRNO__PROG2BIG ,/* Program too big */ LIBBPF_ERRNO__KVER ,/* Incorrect kernel version */ +LIBBPF_ERRNO__NOUBPF ,/* UBPF support is not compiled */ __LIBBPF_ERRNO__END, };
@@ -86,6 +87,9 @@ int bpf_program__get_private(struct bpf_program *prog,const char *bpf_program__title (struct bpf_program *prog,bool needs_copy); +int bpf_program__set_ubpf (struct bpf_program *prog); +bool bpf_program__is_ubpf (struct bpf_program *prog); +int bpf_program__fd (struct bpf_program *prog);struct bpf_insn ;
Signature · 1 line
1.8.3.4
cover perf tools: Support uBPF script
This patch set allows to perf invoke some user space BPF scripts on some point. uBPF scripts and kernel BPF scripts reside in one BPF object. They communicate with each other with BPF maps. uBPF scripts can invoke helper functions provided by perf.
At least following new features can be achieved based on uBPF support:
1) Report statistical result:
Like DTrace, perf print statistical report before quit. No need to extract data using 'perf report'. Statistical method is controled by user.
2) Control perf's behavior:
Dynamically adjust period of different events. Policy is defined by user.
uBPF library is required before compile. It can be found from github:
https://github.com/iovisor/ubpf.git
Following is an example:
Using BPF script attached at the bottom of this commit message, one can print histogram of write size before perf exit like this:
# ~/perf record -a -e ./test_ubpf.c &
[1] 16800
# dd if=/dev/zero of=/dev/null bs=512 count=5000 5000+0 records in 5000+0 records out 2560000 bytes (2.6 MB) copied, 0.00552838 s, 463 MB/s # dd if=/dev/zero of=/dev/null bs=2048 count=5000 5000+0 records in 5000+0 records out 10240000 bytes (10 MB) copied, 0.0188971 s, 542 MB/s # fg ^C <--- *Press Ctrl-c* 2^^0: 47 2^^1: 13 2^^2: 4 2^^3: 130 2^^4: 11 2^^5: 1051 2^^6: 486 2^^7: 4863 2^^8: 0 2^^9: 5003 2^^10: 4 2^^11: 5003 2^^12: 1 2^^13: 0 2^^14: 0 2^^15: 0 2^^16: 0 2^^17: 0 2^^18: 0 2^^19: 0 2^^20: 0 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.788 MB perf.data ]
Here is test_ubpf.c.
/************ BEGIN ***************/
#include <uapi/linux/bpf.h>
#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;};
#define BPF_ANY 0
static void *(*map_lookup_elem)(struct bpf_map_def *, void *) =
(void *)BPF_FUNC_map_lookup_elem;
static inline unsigned int log2(unsigned int v)
{
unsigned int r;
unsigned int shift;
r = (v > 0xFFFF) << 4; v >>= r;
shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
shift = (v > 0xF) << 2; v >>= shift; r |= shift;
shift = (v > 0x3) << 1; v >>= shift; r |= shift;
r |= (v >> 1);
return r;}
static inline unsigned int log2l(unsigned long v)
{
unsigned int hi = v >> 32;
if (hi)
return log2(hi) + 32;
else
return log2(v);}
struct bpf_map_def SEC("maps") my_hist_map = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(long),
.max_entries = 21,
};
SEC("sys_write=sys_write count")
int sys_write(void *ctx, int err, long write_size)
{
long *value;
int key = 0;
if (err)
return 0;
key = log2l(write_size);
if (key > 20)
key = 20;
value = map_lookup_elem(&my_hist_map, &key);
if (!value)
return 0;
__sync_fetch_and_add(value, 1);
return 0; }
char _license[] SEC("license") = "GPL"; u32 _version SEC("version") = LINUX_VERSION_CODE;
/* Following ugly magic numbers can be find from tools/perf/util/ubpf-helpers-list.h */
static int (*ubpf_memcmp)(void *s1, void *s2, unsigned int n) = (void *)1;
static void (*ubpf_memcpy)(void *d, void *s, unsigned int size) = (void *)2;
static int (*ubpf_strcmp)(void *s1, void *s2) = (void *)3;
static int (*ubpf_printf)(char *fmt, ...) = (void *)4;
static int (*ubpf_map_lookup_elem)(void *map_desc, void *key, void *value) = (void *)5;
static int (*ubpf_map_update_elem)(void *map_desc, void *key, void *value, unsigned long long flags) = (void *)6; static int (*ubpf_map_get_next_key)(void *map_desc, void *key, void *value) = (void *)7;
SEC("UBPF;perf_record_end")
int perf_record_end(int samples)
{
int i, key;
long value;
char fmt[] = "2^^%d: %d\n";
for (i = 0; i < 21; i++) {
ubpf_map_lookup_elem(&my_hist_map, &i, &value);
ubpf_printf(fmt, i, value);
}
return 0; }
/************** END ***************/
Wang Nan (13):
bpf tools: Add map related BPF helper
tools: Add ubpf feature test
bpf tools: Add ubpf include and makefile options
bpf tools: Replace fd array to union array
bpf tools: Save engine type in bpf_program
bpf tools: Introduce ubpf_vm to program instance union bpf tools: Load ubpf program
bpf tools: Add API for fetching ubpf_vm
bpf tools: Register extern functions for ubpf programs perf tools: Register basic UBPF helpers
perf bpf: Accept ubpf programs
perf record: Add UBPF hooks at beginning and end of perf record perf tests: Add UBPF test case
tools/build/Makefile.feature | 6 +- tools/build/feature/Makefile | 6 +- tools/build/feature/test-ubpf.c | 11 ++ tools/lib/bpf/Makefile | 16 +- tools/lib/bpf/bpf.c | 24 +++ tools/lib/bpf/bpf.h | 2 + tools/lib/bpf/libbpf.c | 281 ++++++++++++++++++++++++++++---- tools/lib/bpf/libbpf.h | 21 ++- tools/perf/Makefile.perf | 2 + tools/perf/builtin-record.c | 4 + tools/perf/config/Makefile | 7 + tools/perf/perf.c | 3 + tools/perf/tests/Build | 8 + tools/perf/tests/bpf-script-test-ubpf.c | 68 ++++++++ tools/perf/tests/bpf.c | 77 ++++++++- tools/perf/tests/llvm.c | 4 + tools/perf/tests/llvm.h | 2 + tools/perf/util/Build | 1 + tools/perf/util/bpf-loader.c | 22 +++ tools/perf/util/ubpf-helpers-list.h | 7 + tools/perf/util/ubpf-helpers.c | 66 ++++++++ tools/perf/util/ubpf-helpers.h | 21 +++ tools/perf/util/ubpf-hooks-list.h | 10 ++ tools/perf/util/ubpf-hooks.c | 81 +++++++++ tools/perf/util/ubpf-hooks.h | 35 ++++ 25 files changed, 745 insertions(+), 40 deletions(-) create mode 100644 tools/build/feature/test-ubpf.c create mode 100644 tools/perf/tests/bpf-script-test-ubpf.c create mode 100644 tools/perf/util/ubpf-helpers-list.h create mode 100644 tools/perf/util/ubpf-helpers.c create mode 100644 tools/perf/util/ubpf-helpers.h create mode 100644 tools/perf/util/ubpf-hooks-list.h create mode 100644 tools/perf/util/ubpf-hooks.c create mode 100644 tools/perf/util/ubpf-hooks.h
Signature · 1 line
1.8.3.4
Re: [RFC PATCH 02/13] tools: Add ubpf feature test
In reply to Wang NanEm Wed, Apr 20, 2016 at 06:01:42PM +0000, Wang Nan escreveu:
Quoted 6 lines
Add
test-ubpf.cto find libubpf, which can be found from [1].ubpf is user space BPF engine. Following commits will utilize ubpf to make perf dynamically run some profiling scripts in user space.
Ok, but the rule we've been following is that if a library is not generally available in distros, we don't try to build it, because it will fail in almost all cases, causing confusion and adding overhead to the build for no reason.
So, please make it behave like babeltrace, as described in the changeset below, i.e. to build with UBPF, one will need to do:
$ make UBPF=1
I'm applying the patches and testing, but will need the above to proceed wrt pushing it upstream.
- Arnaldo
commit 6ab2b762befd192b90704c5c7898f5abf8ebb387
Author: Jiri Olsa <[email protected]> Date: Sat Mar 28 11:30:30 2015 +0100
perf build: Disable libbabeltrace check by default
Disabling libbabeltrace check by default and replacing the NO_LIBBABELTRACE make variable with LIBBABELTRACE.
Users wanting the libbabeltrace feature need to build via:
$ make LIBBABELTRACE=1
The reason for this is that the libababeltrace interface we use (version 1.3) hasn't been packaged/released yet, thus the failing feature check only slows down build and confuses other (non CTF) developers.
Quoted 96 lines
Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Jiri Olsa <[email protected]>Cc Li Zefan <[email protected]>--- tools/build/Makefile.feature | 6 ++++-- tools/build/feature/Makefile | 6 +++++- tools/build/feature/test-ubpf.c | 11 +++++++++++ tools/lib/bpf/Makefile | 4 ++-- 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 tools/build/feature/test-ubpf.ctools/build/Makefile.feature +4
-2@@ -56,7 +56,8 @@ FEATURE_TESTS_BASIC := \ zlib \ lzma \ get_cpuid \ - bpf + bpf \ + ubpf # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list # of all feature tests@@ -96,7 +97,8 @@ FEATURE_DISPLAY ?= \ zlib \ lzma \ get_cpuid \ - bpf + bpf \ + ubpf # Set FEATURE_CHECK_(C|LD)FLAGS-all for all FEATURE_TESTS features. # If in the future we need per-feature checks/flags for features nottools/build/feature/Makefile +5
-1@@ -37,7 +37,8 @@ FILES= \ test-zlib.bin \ test-lzma.bin \ test-bpf.bin \ - test-get_cpuid.bin + test-get_cpuid.bin \ + test-ubpf.bin FILES := $(addprefix $(OUTPUT),$(FILES))@@ -182,6 +183,9 @@ $(OUTPUT)test-get_cpuid.bin: $(OUTPUT)test-bpf.bin: $(BUILD) +$(OUTPUT)test-ubpf.bin: + $(BUILD) -lubpf + -include $(OUTPUT)*.d ###############################tools/build/feature/test-ubpf.c new file +11
-0@@ -0,0 +1,11 @@ +#include <stdlib.h> +#include <ubpf.h> + +int main (void ) +{ +struct ubpf_vm *vm; + + vm =ubpf_create (); +ubpf_destroy (vm); +return 0 ; +}tools/lib/bpf/Makefile +2
-2@@ -65,8 +65,8 @@ ifndef VERBOSE endif FEATURE_USER = .libbpf -FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf -FEATURE_DISPLAY = libelf bpf +FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf ubpf +FEATURE_DISPLAY = libelf bpf ubpf INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [RFC PATCH 04/13] bpf tools: Replace fd array to union array
In reply to Wang NanEm Wed, Apr 20, 2016 at 06:01:44PM +0000, Wang Nan escreveu:
Quoted 32 lines
Following commits will add new types for instance of a bpf program. For kernel bpf program, a program instance is fd. For ubpf program, the instance of a program is a 'struct
ubpf_vm'. This patch promote original 'fds' array to 'unionprog_instance' array, prepare for further extending.Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Jiri Olsa <[email protected]>Cc Li Zefan <[email protected]>--- tools/lib/bpf/libbpf.c | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-)tools/lib/bpf/libbpf.c +4
-0@@ -138,6 +138,10 @@ int libbpf_strerror(int err, char *buf, size_t size)# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ#endif +union prog_instance { +int fd; +}; +/* * bpf_prog should be a better name but it has been used in * linux/filter.h.
tools/lib/bpf/libbpf.c struct bpf_program { · line 165
Quoted 6 lines
tools/lib/bpf/libbpf.c +1
-1@@ -157,7 +161,7 @@ struct bpf_program {struct {int nr; -int *fds; +union prog_instance *array; } instances;
Why not just do:
struct {
int nr;
union {
int *fds;
};
} instances;And then do no further changes and when going to the ubpf (haven't looked at the actual patch, proposing this based on your above description), just transform it into:
struct {
int nr;
union {
int *fds;
struct ubpf_vm *vms;
};
} instances;Or plain use a void pointer and use:
struct {
int nr;
void *entries;
} instances;
if (prog->instances.nr > 0) {
int *fds = prog->instances.entries;
for (i = 0; i < prog->instances.nr; i++)
zclose(fd[i]);
}Quoted 127 lines
tools/lib/bpf/libbpf.c +19
-19bpf_program_prep_t preprocessor;@@ -229,14 +233,14 @@ static void bpf_program__unload(struct bpf_program *prog)*/ if (prog->instances.nr >0 ) {for (i =0 ; i < prog->instances.nr; i++) -zclose (prog->instances.fds[i]); +zclose (prog->instances.array[i].fd); }else if (prog->instances.nr !=-1 ) {pr_warning ("Internal error: instances.nr is %d\n" , prog->instances.nr); } prog->instances.nr =-1 ; -zfree (&prog->instances.fds); +zfree (&prog->instances.array); }static void bpf_program__exit (struct bpf_program *prog)@@ -287,7 +291,7 @@ bpf_program__init(void *data, size_t size, char *name, int idx,memcpy (prog->insns, data, prog->insns_cnt *sizeof (struct bpf_insn )); prog->idx = idx; - prog->instances.fds =NULL ; + prog->instances.array =NULL ; prog->instances.nr =-1 ;return 0 ;@@ -937,20 +941,20 @@ bpf_program__load(struct bpf_program *prog, {int err =0 , fd, i; -if (prog->instances.nr <0 || !prog->instances.fds) { +if (prog->instances.nr <0 || !prog->instances.array) {if (prog->preprocessor) {pr_warning ("Internal error: can't load program '%s'\n" , prog->section_name);return -LIBBPF_ERRNO__INTERNAL ; } - prog->instances.fds =malloc (sizeof (int )); -if (!prog->instances.fds) { -pr_warning ("Not enough memory for BPF fds\n" ); + prog->instances.array =malloc (sizeof (union prog_instance )); +if (!prog->instances.array) { +pr_warning ("Not enough memory for BPF instance\n" );return -ENOMEM ; } prog->instances.nr =1 ; - prog->instances.fds[0 ] =-1 ; + prog->instances.array[0 ].fd =-1 ; }if (!prog->preprocessor) {@@ -961,7 +965,7 @@ bpf_program__load(struct bpf_program *prog, err =load_program (prog->insns, prog->insns_cnt, license, kern_version, &fd);if (!err) - prog->instances.fds[0 ] = fd; + prog->instances.array[0 ].fd = fd; goto out; }@@ -981,7 +985,7 @@ bpf_program__load(struct bpf_program *prog,if (!result.new_insn_ptr || !result.new_insn_cnt) {pr_debug ("Skip loading the %dth instance of program '%s'\n" , i, prog->section_name); - prog->instances.fds[i] =-1 ; + prog->instances.array[i].fd =-1 ;if (result.pfd) *result.pfd =-1 ;continue ;@@ -999,7 +1003,7 @@ bpf_program__load(struct bpf_program *prog,if (result.pfd) *result.pfd = fd; - prog->instances.fds[i] = fd; + prog->instances.array[i].fd = fd; } out:if (err)@@ -1269,27 +1273,27 @@ int bpf_program__fd(struct bpf_program *prog)int bpf_program__set_prep (struct bpf_program *prog,int nr_instances,bpf_program_prep_t prep) { -int *instances_fds; +union prog_instance *array;if (nr_instances <=0 || !prep)return -EINVAL ; -if (prog->instances.nr >0 || prog->instances.fds) { +if (prog->instances.nr >0 || prog->instances.array) {pr_warning ("Can't set pre-processor after loading\n" );return -EINVAL ; } - instances_fds =malloc (sizeof (int ) * nr_instances); -if (!instances_fds) { -pr_warning ("alloc memory failed for fds\n" ); + array =malloc (sizeof (array[0 ]) * nr_instances); +if (!array) { +pr_warning ("alloc memory failed for instances\n" );return -ENOMEM ; }/* fill all fd with -1 */ -memset (instances_fds,-1 ,sizeof (int ) * nr_instances); +memset (array,-1 ,sizeof (array[0 ]) * nr_instances); prog->instances.nr = nr_instances; - prog->instances.fds = instances_fds; + prog->instances.array = array; prog->preprocessor = prep;return 0 ; }@@ -1304,7 +1308,7 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)return -EINVAL ; } - fd = prog->instances.fds[n]; + fd = prog->instances.array[n].fd;if (fd <0 ) {pr_warning ("%dth instance of program '%s' is invalid\n" , n, prog->section_name);Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [RFC PATCH 05/13] bpf tools: Save engine type in bpf_program
In reply to Wang NanEm Wed, Apr 20, 2016 at 06:01:45PM +0000, Wang Nan escreveu:
Quoted 49 lines
Add an 'engine' field in
bpf_programto indicate whether a program is a ubpf program or kernel bpf program. For compatibility, the default engine is kernel bpf, unless explicitly set to ubpf usingbpf_program__set_ubpf().Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Jiri Olsa <[email protected]>Cc Li Zefan <[email protected]>--- tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 4 ++++ 2 files changed, 44 insertions(+)tools/lib/bpf/libbpf.c +7
-0@@ -80,6 +80,7 @@ static const char *libbpf_strerror_table[NR_ERRNO] = { [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", + [ERRCODE_OFFSET(NOUBPF)] = "UBPF support is not compiled", };int libbpf_strerror (int err,char *buf,size_t size)@@ -147,6 +148,11 @@ union prog_instance {* linux/filter.h. */ struct bpf_program { +enum { +ENGINE_UNKNOWN , +ENGINE_KBPF , +ENGINE_UBPF , + } engine;/* Index in elf obj file, for relocation use. */ int idx;char *section_name;@@ -291,6 +297,7 @@ bpf_program__init(void *data, size_t size, char *name, int idx,memcpy (prog->insns, data, prog->insns_cnt *sizeof (struct bpf_insn )); prog->idx = idx; + prog->engine =ENGINE_UNKNOWN ; prog->instances.array =NULL ; prog->instances.nr =-1 ;
tools/lib/bpf/libbpf.c bpf_program__load(struct bpf_program *prog, · line 953
Quoted 6 lines
tools/lib/bpf/libbpf.c +3
-0@@ -941,6 +948,11 @@ bpf_program__load(struct bpf_program *prog, {int err =0 , fd, i; +if (prog->engine ==ENGINE_UNKNOWN ) + prog->engine =ENGINE_KBPF ; +if (prog->engine !=ENGINE_KBPF )
else if (prog->engine != ENGINE_KBPF)
Nitpicking a bit :-)
Quoted 64 lines
tools/lib/bpf/libbpf.c +30
-0+return -EINVAL ; +if (prog->instances.nr <0 || !prog->instances.array) {if (prog->preprocessor) { pr_warning("Internal error: can't load program '%s'\n",@@ -1318,6 +1330,34 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)return fd; } +#ifdef HAVE_UBPF_SUPPORT +int bpf_program__set_ubpf (struct bpf_program *prog) +{ +if (prog->engine !=ENGINE_UNKNOWN ) { +pr_warning ("Can't set program %s to ubpf\n" , + prog->section_name); +return -EINVAL ; + } + prog->engine =ENGINE_UBPF ; +return 0 ; +} + +bool bpf_program__is_ubpf (struct bpf_program *prog) +{ +return prog->engine ==ENGINE_UBPF ; +} +#else +int bpf_program__set_ubpf (struct bpf_program *prog__maybe_unused ) +{ +return -LIBBPF_ERRNO__NOUBPF ; +} + +bool bpf_program__is_ubpf (struct bpf_program *prog__maybe_unused ) +{ +return false; +} +#endif +int bpf_map__get_fd (struct bpf_map *map) {if (!map)tools/lib/bpf/libbpf.h +4
-0@@ -26,6 +26,7 @@ enum libbpf_errno {LIBBPF_ERRNO__VERIFY ,/* Kernel verifier blocks program loading */ LIBBPF_ERRNO__PROG2BIG ,/* Program too big */ LIBBPF_ERRNO__KVER ,/* Incorrect kernel version */ +LIBBPF_ERRNO__NOUBPF ,/* UBPF support is not compiled */ __LIBBPF_ERRNO__END, };@@ -86,6 +87,9 @@ int bpf_program__get_private(struct bpf_program *prog,const char *bpf_program__title (struct bpf_program *prog,bool needs_copy); +int bpf_program__set_ubpf (struct bpf_program *prog); +bool bpf_program__is_ubpf (struct bpf_program *prog); +int bpf_program__fd (struct bpf_program *prog);struct bpf_insn ;Signature · 1 line
1.8.3.4
Re: [RFC PATCH 05/13] bpf tools: Save engine type in bpf_program
发自我的 iPhone
Quoted 4 lines · 在 2016年4月21日,上午3:23,Arnaldo Carvalho de Melo <[email protected]> 写道:
Em Wed, Apr 20, 2016 at 06:01:45PM +0000, Wang Nan escreveu:
Quoted 1 line
[SNIP]
Quoted 9 lines
tools/lib/bpf/libbpf.c bpf_program__load(struct bpf_program *prog, · line 953
Quoted 4 lines
tools/lib/bpf/libbpf.c +3
-0+if (prog->engine ==ENGINE_UNKNOWN ) + prog->engine =ENGINE_KBPF ; +if (prog->engine !=ENGINE_KBPF )else if (prog->engine != ENGINE_KBPF)Nitpicking a bit :-)
We set it to ENGINE_KBPF in previous "if",
so can't use "else".
Do this because I want the engine is KBPF
if not explicitly set to UNPF.
Thank you.
Quoted 64 lines
Quoted 64 lines
tools/lib/bpf/libbpf.c +30
-0+return -EINVAL ; +if (prog->instances.nr <0 || !prog->instances.array) {if (prog->preprocessor) { pr_warning("Internal error: can't load program '%s'\n",@@ -1318,6 +1330,34 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)return fd; } +#ifdef HAVE_UBPF_SUPPORT +int bpf_program__set_ubpf (struct bpf_program *prog) +{ +if (prog->engine !=ENGINE_UNKNOWN ) { +pr_warning ("Can't set program %s to ubpf\n" , + prog->section_name); +return -EINVAL ; + } + prog->engine =ENGINE_UBPF ; +return 0 ; +} + +bool bpf_program__is_ubpf (struct bpf_program *prog) +{ +return prog->engine ==ENGINE_UBPF ; +} +#else +int bpf_program__set_ubpf (struct bpf_program *prog__maybe_unused ) +{ +return -LIBBPF_ERRNO__NOUBPF ; +} + +bool bpf_program__is_ubpf (struct bpf_program *prog__maybe_unused ) +{ +return false; +} +#endif +int bpf_map__get_fd (struct bpf_map *map) {if (!map)tools/lib/bpf/libbpf.h +4
-0@@ -26,6 +26,7 @@ enum libbpf_errno {LIBBPF_ERRNO__VERIFY ,/* Kernel verifier blocks program loading */ LIBBPF_ERRNO__PROG2BIG ,/* Program too big */ LIBBPF_ERRNO__KVER ,/* Incorrect kernel version */ +LIBBPF_ERRNO__NOUBPF ,/* UBPF support is not compiled */ __LIBBPF_ERRNO__END, };@@ -86,6 +87,9 @@ int bpf_program__get_private(struct bpf_program *prog,const char *bpf_program__title (struct bpf_program *prog,bool needs_copy); +int bpf_program__set_ubpf (struct bpf_program *prog); +bool bpf_program__is_ubpf (struct bpf_program *prog); +int bpf_program__fd (struct bpf_program *prog);struct bpf_insn ;Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [RFC PATCH 06/13] bpf tools: Introduce ubpf_vm to program instance union
In reply to Wang NanEm Wed, Apr 20, 2016 at 06:01:46PM +0000, Wang Nan escreveu:
Quoted 37 lines
Add 'struct
ubpf_vm*' intoprog_instanceunion. Introduceif_engine()macro to merge common code.Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Jiri Olsa <[email protected]>Cc Li Zefan <[email protected]>--- tools/lib/bpf/libbpf.c | 76 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 8 deletions(-)tools/lib/bpf/libbpf.c +9
-0@@ -141,6 +141,9 @@ int libbpf_strerror(int err, char *buf, size_t size)union prog_instance {int fd; +#ifdef HAVE_UBPF_SUPPORT +struct ubpf_vm *vm; +#endif };/* @@ -176,6 +179,56 @@ struct bpf_program {bpf_program_clear_priv_t clear_priv; }; +#ifdef HAVE_UBPF_SUPPORT +# define __if_engine (p, d, k, u) \ + do { \ + switch (p->engine) { \ + default: \ + case ENGINE_UNKNOWN: { \
tools/lib/bpf/libbpf.c struct bpf_program { · line 197
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ d; \ + } \ +case ENGINE_KBPF : { \ + k;break ; \ + } \ +case ENGINE_UBPF : { \ + u;break ; \ + } \ + } \ + }while (0 )
These macro tricks are getting unecessarily overly complicated :-\
Quoted 5 lines
tools/lib/bpf/libbpf.c +5
-0+ +/* +* ubpf_destroy() doesn't accept NULL input. This wrapper makes +* it similar to zclose. +*/
tools/lib/bpf/libbpf.c struct bpf_program { · line 212
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+# define __ubpf_destroy (vm) do { \ + if (vm) \ + ubpf_destroy(vm); \ + (vm) = NULL; \ +} while(0) + +#else +# define __if_engine (p, d, k, u) \ + do { k; } while(0) +#endif
Why use just one letter parameters, give them proper names
tools/lib/bpf/libbpf.c struct bpf_program { · line 221
Quoted 9 lines
tools/lib/bpf/libbpf.c +9
-0+ +#define instan_fd (i) instances.array[i].fd +#define instan_vm (i) instances.array[i].vm + +#define if_engine (p, k, u) __if_engine(p, do { } while(0), k, u) +#define set_instance (p, i, k, u) \ + if_engine(p, \ + p->instan_fd(i) = k, \ + p->instan_vm(i) = u)
If you had used a void pointer for instances->entries you wouldn't have to play such tricks, right?
Quoted 17 lines
tools/lib/bpf/libbpf.c +10
-0+ +static inline void init_instance_array (struct bpf_program *prog) +{ +size_t size =sizeof (prog->instances.array[0 ]) * prog->instances.nr; + +if_engine (prog, +memset (prog->instances.array,-1 , size), +memset (prog->instances.array,0 , size)); +} +struct
bpf_map{tools/lib/bpf/libbpf.c +0
-0int fd;char *name;
tools/lib/bpf/libbpf.c static void bpf_program__unload(struct bpf_program *prog) · line 297
Quoted 7 lines
tools/lib/bpf/libbpf.c +3
-1@@ -239,7 +292,9 @@ static void bpf_program__unload(struct bpf_program *prog)*/ if (prog->instances.nr >0 ) {for (i =0 ; i < prog->instances.nr; i++) -zclose (prog->instances.array[i].fd); +if_engine (prog, +zclose (prog->instan_fd (i)), +__ubpf_destroy (prog->instan_vm (i)));
So if we have more types of instances this will become a switch_engine()?
tools/lib/bpf/libbpf.c bpf_program__load(struct bpf_program *prog, · line 1024
Quoted 9 lines
tools/lib/bpf/libbpf.c +1
-1}else if (prog->instances.nr !=-1 ) {pr_warning ("Internal error: instances.nr is %d\n" , prog->instances.nr);@@ -966,7 +1021,7 @@ bpf_program__load(struct bpf_program *prog,return -ENOMEM ; } prog->instances.nr =1 ; - prog->instances.array[0 ].fd =-1 ; +set_instance (prog,0 ,-1 ,NULL );
And here we would go on adding more and more values? Why not have some
struct bpf_engine {
void (*init)(struct bpf_program *prog);
void (*fini)(struct bpf_program *prog);
}One for the kernel "engine", the other for the userspace one?
Quoted 60 lines
tools/lib/bpf/libbpf.c +11
-6}if (!prog->preprocessor) {@@ -977,7 +1032,7 @@ bpf_program__load(struct bpf_program *prog, err =load_program (prog->insns, prog->insns_cnt, license, kern_version, &fd);if (!err) - prog->instances.array[0 ].fd = fd; + prog->instan_fd (0 ) = fd; goto out; }@@ -997,7 +1052,7 @@ bpf_program__load(struct bpf_program *prog,if (!result.new_insn_ptr || !result.new_insn_cnt) {pr_debug ("Skip loading the %dth instance of program '%s'\n" , i, prog->section_name); - prog->instances.array[i].fd =-1 ; + prog->instan_fd (i) =-1 ;if (result.pfd) *result.pfd =-1 ;continue ;@@ -1015,7 +1070,7 @@ bpf_program__load(struct bpf_program *prog,if (result.pfd) *result.pfd = fd; - prog->instances.array[i].fd = fd; + prog->instan_fd (i) = fd; } out:if (err)@@ -1301,12 +1356,11 @@ int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,return -ENOMEM ; } -/* fill all fd with -1 */ -memset (array,-1 ,sizeof (array[0 ]) * nr_instances); - prog->instances.nr = nr_instances; prog->instances.array = array; prog->preprocessor = prep; + +init_instance_array (prog);return 0 ; }@@ -1314,6 +1368,12 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n) {int fd; +if (prog->engine !=ENGINE_KBPF ) { +pr_warning ("Can't get fd from program %s: engine not KBPF or not loaded\n" , + prog->section_name); +return -EINVAL ; + } +if (n >= prog->instances.nr || n <0 ) {pr_warning ("Can't get the %dth fd from program %s: only %d instances\n" , n, prog->section_name, prog->instances.nr);Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [RFC PATCH 07/13] bpf tools: Load ubpf program
In reply to Wang NanEm Wed, Apr 20, 2016 at 06:01:47PM +0000, Wang Nan escreveu:
Quoted 76 lines
In
bpf_program__load(), load ubpf program according to its engine type.API is improvemented to hold 'struct
ubpf_vm*'.Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Jiri Olsa <[email protected]>Cc Li Zefan <[email protected]>--- tools/lib/bpf/libbpf.c | 66 +++++++++++++++++++++++++++++++++++++++----------- tools/lib/bpf/libbpf.h | 11 +++++++-- 2 files changed, 61 insertions(+), 16 deletions(-)tools/lib/bpf/libbpf.c +29
-2@@ -81,6 +81,7 @@ static const char *libbpf_strerror_table[NR_ERRNO] = { [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", [ERRCODE_OFFSET(NOUBPF)] = "UBPF support is not compiled", + [ERRCODE_OFFSET(LOADUBPF)] = "Failed to load user space BPF program", };int libbpf_strerror (int err,char *buf,size_t size)@@ -949,6 +950,31 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)return 0 ; } +#ifdef HAVE_UBPF_SUPPORT +static int +load_ubpf_program (struct bpf_insn *insns,int insns_cnt, +struct ubpf_vm **pvm) +{ +struct ubpf_vm *vm =ubpf_create (); +char *message; +int err; + +if (!vm) { +pr_warning ("Failed to create ubpf vm\n" ); +return -LIBBPF_ERRNO__LOADUBPF ; + } + + err =ubpf_load (vm, insns, insns_cnt *sizeof (insns[0 ]), &message); +if (err <0 ) { +pr_warning ("Failed to load ubpf program: %s\n" , message); +return -LIBBPF_ERRNO__LOADUBPF ; + } + + *pvm = vm; +return 0 ; +} +#endif +static int load_program (struct bpf_insn *insns,int insns_cnt,char *license,u32 kern_version,int *pfd)@@ -1002,11 +1028,12 @@ bpf_program__load(struct bpf_program *prog, char *license, u32 kern_version) { int err = 0, fd, i; +#ifdef HAVE_UBPF_SUPPORT +struct ubpf_vm *vm; +#endif if (prog->engine ==ENGINE_UNKNOWN ) prog->engine =ENGINE_KBPF ; -if (prog->engine !=ENGINE_KBPF ) -return -EINVAL ;if (prog->instances.nr <0 || !prog->instances.array) {if (prog->preprocessor) {@@ -1029,10 +1056,15 @@ bpf_program__load(struct bpf_program *prog, pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
tools/lib/bpf/libbpf.c bpf_program__load(struct bpf_program *prog, · line 1064
Quoted 10 lines
tools/lib/bpf/libbpf.c +6
-2prog->section_name, prog->instances.nr); } - err =load_program (prog->insns, prog->insns_cnt, - license, kern_version, &fd); + +if_engine (prog, + (err =load_program (prog->insns, prog->insns_cnt, + license, kern_version, &fd)), + (err =load_ubpf_program (prog->insns, prog->insns_cnt, + &vm)));
Same thing here, with that struct bpf_engine fops you would just do:
err = engine->load(prog, arg);
Quoted 72 lines
tools/lib/bpf/libbpf.c +17
-10+if (!err) - prog->instan_fd (0 ) = fd; +set_instance (prog,0 , fd, vm); goto out; }@@ -1052,15 +1084,21 @@ bpf_program__load(struct bpf_program *prog,if (!result.new_insn_ptr || !result.new_insn_cnt) {pr_debug ("Skip loading the %dth instance of program '%s'\n" , i, prog->section_name); - prog->instan_fd (i) =-1 ; -if (result.pfd) - *result.pfd =-1 ; + +if_engine (prog, prog->instan_fd (i) =-1 , + prog->instan_vm (i) =NULL ); +if (result.ptr) +if_engine (prog, *result.pfd =-1 , *result.pvm =NULL );continue ; } - err =load_program (result.new_insn_ptr, - result.new_insn_cnt, - license, kern_version, &fd); +if_engine (prog, + (err =load_program (result.new_insn_ptr, + result.new_insn_cnt, + license, kern_version, &fd)), + (err =load_ubpf_program (result.new_insn_ptr, + result.new_insn_cnt, + &vm)));if (err) { pr_warning("Loading the %dth instance of program '%s' failed\n",@@ -1068,9 +1106,9 @@ bpf_program__load(struct bpf_program *prog, goto out; } -if (result.pfd) - *result.pfd = fd; - prog->instan_fd (i) = fd; +if (result.ptr) +if_engine (prog, *result.pfd = fd, *result.pvm = vm); +set_instance (prog, i, fd, vm); } out:if (err)tools/lib/bpf/libbpf.h +3
-0@@ -27,6 +27,7 @@ enum libbpf_errno {LIBBPF_ERRNO__PROG2BIG ,/* Program too big */ LIBBPF_ERRNO__KVER ,/* Incorrect kernel version */ LIBBPF_ERRNO__NOUBPF ,/* UBPF support is not compiled */ +LIBBPF_ERRNO__LOADUBPF ,/* Failed to load user space BPF program */ __LIBBPF_ERRNO__END, };@@ -123,6 +124,8 @@ struct bpf_insn;* bpf_program__nth_fd(prog, 0). */ +struct ubpf_vm ; +struct bpf_prog_prep_result {/* * If not NULL, load new instruction array. @@ -131,8 +134,12 @@ struct bpf_prog_prep_result {struct bpf_insn *new_insn_ptr;int new_insn_cnt;
tools/lib/bpf/libbpf.h struct bpf_prog_prep_result { · line 143
Quoted 10 lines
tools/lib/bpf/libbpf.h +6
-2-/* If not NULL, result fd is set to it */ -int *pfd; +/* If not NULL, result is set to it */ +union { +void *ptr; +int *pfd; +struct ubpf_vm **pvm; + }; };
see? Here you use anonymous unions, but after all I wrote, I think that you really need a fops struct, like we have so many in the kernel sources, see struct inode_operations, file_operations, etc, and then that bpf_program->instance.entries needs just to be a void pointer.
- Arnaldo
Quoted 149 lines · Wang Nan
This patch set allows to perf invoke some user space BPF scripts on some point. uBPF scripts and kernel BPF scripts reside in one BPF object. They communicate with each other with BPF maps. uBPF scripts can invoke helper functions provided by perf.
At least following new features can be achieved based on uBPF support:
1) Report statistical result:
Like DTrace, perf print statistical report before quit. No need to extract data using 'perf report'. Statistical method is controled by user.2) Control perf's behavior:
Dynamically adjust period of different events. Policy is defined by user.uBPF library is required before compile. It can be found from github:
https://github.com/iovisor/ubpf.git
Following is an example:
Using BPF script attached at the bottom of this commit message, one can print histogram of write size before perf exit like this:
# ~/perf record -a -e ./test_ubpf.c &
[1] 16800# dd if=/dev/zero of=/dev/null bs=512 count=5000 5000+0 records in 5000+0 records out 2560000 bytes (2.6 MB) copied, 0.00552838 s, 463 MB/s # dd if=/dev/zero of=/dev/null bs=2048 count=5000 5000+0 records in 5000+0 records out 10240000 bytes (10 MB) copied, 0.0188971 s, 542 MB/s # fg ^C <--- *Press Ctrl-c* 2^^0: 47 2^^1: 13 2^^2: 4 2^^3: 130 2^^4: 11 2^^5: 1051 2^^6: 486 2^^7: 4863 2^^8: 0 2^^9: 5003 2^^10: 4 2^^11: 5003 2^^12: 1 2^^13: 0 2^^14: 0 2^^15: 0 2^^16: 0 2^^17: 0 2^^18: 0 2^^19: 0 2^^20: 0 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.788 MB perf.data ]Here is
test_ubpf.c./************ BEGIN ***************/
#include <uapi/linux/bpf.h>
#define SEC(NAME)__attribute__((section(NAME), used))struct bpf_map_def { unsigned int type; unsigned int key_size; unsigned int value_size; unsigned int max_entries;};
#define
BPF_ANY0static void *(*map_lookup_elem)(struct
bpf_map_def*, void *) =(void *)BPF_FUNC_map_lookup_elem;static inline unsigned int log2(unsigned int v)
{ unsigned int r; unsigned int shift; r = (v > 0xFFFF) << 4; v >>= r; shift = (v > 0xFF) << 3; v >>= shift; r |= shift; shift = (v > 0xF) << 2; v >>= shift; r |= shift; shift = (v > 0x3) << 1; v >>= shift; r |= shift; r |= (v >> 1); return r;}
static inline unsigned int log2l(unsigned long v)
{ unsigned int hi = v >> 32; if (hi) return log2(hi) + 32; else return log2(v);}
struct
bpf_map_defSEC("maps")my_hist_map= {
.type =BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(long),
.max_entries = 21,
};
SEC("sys_write=sys_write count")
intsys_write(void *ctx, int err, long write_size){ long *value; int key = 0; if (err) return 0; key = log2l(write_size); if (key > 20) key = 20; value = map_lookup_elem(&my_hist_map, &key); if (!value) return 0; __sync_fetch_and_add(value, 1); return 0;}
char _license[]SEC("license")= "GPL"; u32_versionSEC("version")=LINUX_VERSION_CODE;/* Following ugly magic numbers can be find from
tools/perf/util/ubpf-helpers-list.h*/
static int (*ubpf_memcmp)(void *s1, void *s2, unsigned int n) = (void *)1;
static void (*ubpf_memcpy)(void *d, void *s, unsigned int size) = (void *)2;
static int (*ubpf_strcmp)(void *s1, void *s2) = (void *)3;
static int (*ubpf_printf)(char *fmt, ...) = (void *)4;
static int (*ubpf_map_lookup_elem)(void*map_desc, void *key, void *value) = (void *)5;
static int (*ubpf_map_update_elem)(void*map_desc, void *key, void *value, unsigned long long flags) = (void *)6; static int (*ubpf_map_get_next_key)(void*map_desc, void *key, void *value) = (void *)7;
SEC("UBPF;perf_record_end")
intperf_record_end(int samples){ int i, key; long value; char fmt[] = "2^^%d: %d\n"; for (i = 0; i < 21; i++) { ubpf_map_lookup_elem(&my_hist_map, &i, &value); ubpf_printf(fmt, i, value); } return 0;
Quoted 1 line
}
Interesting!
If bpf is used for both kernel and user side programs, we can allow almost arbitrary C code for the user side.
There is no need to be limited to a fixed set of helpers.
There is no verifier in user space either.
Just call 'printf("string")' directly.
Wouldn't even need to change interpreter.
Also ubpf was written from scratch with apache2, while perf is gpl, so you can just link kernel/bpf/core.o directly instead of using external libraries.
I really meant link .o file compiled for kernel.
Advertize dummy kfree/kmalloc and it will link fine, since perf
will only be calling __bpf_prog_run() which is 99% indepdendent from kernel. I used to do exactly that long ago while performance tunning the interpreter. Another option is to fork the interpreter for perf, but I don't like it at all. Compiling the same bpf/core.c once for kernel and once for perf is another option, but imo linking core.o is easier.
In general this set and overall bpf in user space makes sense only if we allow much more flexible C code for user space.
If it's limited to ubpf_* helpers, that will quickly become suboptimal. Another alternative is to use luajit for user space scripting like we do in bcc. That gives full flexibility with good performance. If we can do 'restricted C into bpf' for kernel and 'full C into bpf' for user space that would be a great model. Note llvm doesn't care how C looks like. You can call any function in C and use loops.
Quoted 20 lines · Alexei Starovoitov
Quoted 19 lines · Wang Nan
This patch set allows to perf invoke some user space BPF scripts on some point. uBPF scripts and kernel BPF scripts reside in one BPF object. They communicate with each other with BPF maps. uBPF scripts can invoke helper functions provided by perf.
At least following new features can be achieved based on uBPF support:
1) Report statistical result:
Like DTrace, perf print statistical report before quit. No need to extract data using 'perf report'. Statistical method is controled by user.2) Control perf's behavior:
Dynamically adjust period of different events. Policy is defined by user.uBPF library is required before compile. It can be found from github:
[SNIP]
Quoted 6 lines
Interesting!
If bpf is used for both kernel and user side programs, we can allow almost arbitrary C code for the user side.
There is no need to be limited to a fixed set of helpers. There is no verifier in user space either.
Just call 'printf("string")' directly.
Calling 'printf("string")' would be cool, but it still need some extra work: .rodata section should be extracted, programs should be relocated to it.
Quoted 11 lines
Wouldn't even need to change interpreter.
Also ubpf was written from scratch with apache2, while perf is gpl, so you can just linkkernel/bpf/core.odirectly instead of using external libraries.
I really meant link .o file compiled for kernel.
Advertize dummy kfree/kmalloc and it will link fine, since perf
will only be calling__bpf_prog_run()which is 99% indepdendent from kernel. I used to do exactly that long ago while performance tunning the interpreter. Another option is to fork the interpreter for perf, but I don't like it at all. Compiling the samebpf/core.conce for kernel and once for perf is another option, but imo linking core.o is easier.
I just realized we can't link apache2 static library into perf. (is that true?)
Current perf building doesn't support directly linking like this, because such linking makes perf rely on kernel building, so we can't build perf before building kernel any more.
One possible solution: providing a kernel build dir to perf builder and find the corresponding '.o' file from it:
$ make KBUILD_DIR=/kernel/build/dir
/lib/uname -r/build can be made as default position.
JIT compiler can also be linked this way.
Another possible solution: using macro trick to allow building bpf/core.c in perf building. It is possible and simpler, but we could be broken by kernel modification.
Quoted 3 lines
In general this set and overall bpf in user space makes sense only if we allow much more flexible C code for user space.
If it's limited to ubpf_* helpers, that will quickly become suboptimal.
Yes. I tried to reimplement tracex2 in sample but find it is not an easy work. However, in two of my usecase(reporting and controlling), only reporting require flexible C code. Even we have full featured C, doing statistical still require relative complex code, because the lacking of data structure support such as associate array (dict in python). For controling (for example, dynamically period adjustments; output perf.data when something unusual detected), uBPF programs describe rules and invoke actions (actions should be provided by perf helpers), similary to their kernel side counterparts.
So I think no matter uBPF can be 'full C', we should consider making strong and flexiblity ubpf helpers. Basic reporting method, such as histogram, should be provided by a perf helper directly, no need to be rewritten by uBPF. We can even make a ubpf helper to bridge uBPF and lua scripts, then invokes lua scripts at perf hooks. For example:
const char lua_script[] SEC("UBPF-lua;perf_record_exit") "<lua script>";
Thank you.