perf clang: Builtin clang and perfhook support
Subsystems (3)
cover perf clang: Builtin clang and perfhook support
This is version 2 of perf builtin clang patch series. Compare to v1, add an exciting feature: jit compiling perf hook functions. This features allows script writer report result through BPF map in a customized way.
At the end of this cover letter lists an example shows how to capture and report a syscall histogram.
This patchset is based on current perf/core.
In this patchset:
Patch 1 - 4 are bugfixes left in my local tree. Patch 5 - 7 are preparation in libbpf. Patch 8 - 9 introduce perf hook to perf.
Patch 10 - 21 are patches support builtin clang. Some of then are already
collected by Arnaldo in tmp.perf/builtin-clang. They are slightly adjusted in this v2 series.
Patch 22 - 29 add JIT compiling to builting clang and add them to perf hook. Patch 30 - 34 are easy of use improvements. 1) builtin clang defines macros
and helpers by default using builtin include headers. Default headers can be turned off by -UBUILTIN_CLANG_DEFAULT_INCLUDE.
2) allow JITted perf hooks access more functions in libc.
Example: build syscall histogram, exclude syscalls issued by perf itself.
Please note following improvements:
1. Don't need define many bpf helpers by hand.
See bpf_map_lookup_elem and bpf_get_current_pid_tgid.
2. See how pid of git is passed to BPF script attached to raw_syscalls:sys_enter.
$ cat ./count_syscalls.c typedef unsigned long u64;
#define BPF_MAP_TYPE_HASH 1
#define BPF_MAP_TYPE_ARRAY 2
enum GVAL {
G_perf_pid,
NR_GVALS
};
struct bpf_map_def SEC("maps") GVALS = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(u64),
.max_entries = NR_GVALS,
};
struct bpf_map_def SEC("maps") syscall_counter = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(u64),
.value_size = sizeof(u64),
.max_entries = 512,
};
SEC("raw_syscalls:sys_enter")
int func(void *ctx)
{
int key = G_perf_pid;
u64 id = *((u64 *)(ctx + 8));
int self_pid = bpf_get_current_pid_tgid() & 0xffffffff;
int *perf_pid = bpf_map_lookup_elem(&GVALS, &key);
u64 *counter;
if (!perf_pid)
return 0;
if (*perf_pid == self_pid)
return 0;
counter = bpf_map_lookup_elem(&syscall_counter, &id);
if (!counter) {
u64 value = 1;
bpf_map_update_elem(&syscall_counter, &id, &value, 0);
return 0;
}
__sync_fetch_and_add(counter, 1);
return 0;
}
SEC("perfhook:record_start")
void record_start(void *ctx)
{
int perf_pid = getpid(), key = G_perf_pid;
printf("Start count, perfpid=%d\n", perf_pid);
jit_helper__map_update_elem(ctx, &GVALS, &key, &perf_pid, 0);
}
SEC("perfhook:record_end")
void record_end(void *ctx)
{
u64 key = -1, value;
while (!jit_helper__map_get_next_key(ctx, &syscall_counter, &key, &key)) {
jit_helper__map_lookup_elem(ctx, &syscall_counter, &key, &value);
printf("syscall %ld\tcount: %ld\n", (long)key, (long)value);
}
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;$ sudo -s # ulimit -l unlimited # perf record -e ./count_syscalls.c echo "Haha" Start count, perfpid=25209 Haha [ perf record: Woken up 1 times to write data ] syscall 8 count: 6 syscall 11 count: 1 syscall 4 count: 6 syscall 21 count: 1 syscall 5 count: 3 syscall 231 count: 1 syscall 45 count: 3 syscall 0 count: 24 syscall 257 count: 1 syscall 59 count: 4 syscall 23 count: 9 syscall 78 count: 2 syscall 41 count: 4 syscall 72 count: 8 syscall 10 count: 3 syscall 321 count: 1 syscall 298 count: 7 syscall 16 count: 21 syscall 9 count: 16 syscall 1 count: 114 syscall 12 count: 3 syscall 14 count: 35 syscall 158 count: 1 syscall 2 count: 15 syscall 7 count: 18 syscall 3 count: 11 [ perf record: Captured and wrote 0.011 MB perf.data ]
Eric Leblond (1):
tools lib bpf: fix maps resolution
Wang Nan (33):
perf tools: Fix kernel version error in ubuntu
perf record: Fix segfault when running with suid and kptr_restrict is
1
tools perf: Add missing struct defeinition in probe_event.h tools lib bpf: Add missing bpf map functions
tools lib bpf: Add private field for bpf_object
tools lib bpf: Retrive bpf_map through offset of bpf_map_def perf tools: Introduce perf hooks
perf tools: Pass context to perf hook functions
perf llvm: Extract helpers in llvm-utils.c
tools build: Add feature detection for LLVM
tools build: Add feature detection for clang
perf build: Add clang and llvm compile and linking support perf clang: Add builtin clang support ant test case
perf clang: Use real file system for #include
perf clang: Allow passing CFLAGS to builtin clang
perf clang: Update test case to use real BPF script
perf clang: Support compile IR to BPF object and add testcase perf clang: Compile BPF script use builtin clang support perf clang: Pass full path to builtin clang
perf clang: Pass CFLAGS to builtin clang
perf clang jit: Wrap llvm::Module using PerfModule
perf clang jit: Insignt BPF and JIT functions in a Module perf clang jit: add PerfModule::doJIT to JIT perfhook functions. perf clang jit: Export functions for jitted code
perf clang jit: Actually JIT and hook in bpf loader
perf clang jit: Collect the lowest address in maps section as map_base perf clang jit: Access BPF map
perf clang jit: Allow jitted perf hook access BPF maps perf clang: Link BPF functions declaration into perf
perf clang: Declare BPF functions for BPF scripts automatically perf clang: Include helpers to BPF scripts
perf clang builtin: Define hook helpers by default
perf clang git: Export getpid() to perf hook
tools/build/feature/Makefile | 18 + tools/build/feature/test-clang.cpp | 21 ++ tools/build/feature/test-llvm.cpp | 8 + tools/lib/bpf/bpf.c | 56 +++ tools/lib/bpf/bpf.h | 7 + tools/lib/bpf/libbpf.c | 177 ++++++--- tools/lib/bpf/libbpf.h | 13 + tools/perf/Makefile.config | 62 +++- tools/perf/Makefile.perf | 23 +- tools/perf/builtin-record.c | 11 + tools/perf/tests/Build | 4 +- tools/perf/tests/bpf-script-example.c | 30 +- tools/perf/tests/bpf-script-test-kbuild.c | 2 + tools/perf/tests/bpf-script-test-prologue.c | 6 +- tools/perf/tests/bpf-script-test-relocation.c | 17 +- tools/perf/tests/builtin-test.c | 13 + tools/perf/tests/clang.c | 50 +++ tools/perf/tests/llvm.h | 7 + tools/perf/tests/make | 2 + tools/perf/tests/perf-hooks.c | 48 +++ tools/perf/tests/tests.h | 4 + tools/perf/util/Build | 5 + tools/perf/util/bpf-loader.c | 102 +++++- tools/perf/util/bpf-loader.h | 19 + tools/perf/util/c++/Build | 4 + tools/perf/util/c++/bpf-funcs-str.c | 228 ++++++++++++ tools/perf/util/c++/bpf-helper-str.c | 24 ++ tools/perf/util/c++/clang-bpf-includes.h | 13 + tools/perf/util/c++/clang-c.h | 63 ++++ tools/perf/util/c++/clang-test.cpp | 99 +++++ tools/perf/util/c++/clang.cpp | 498 ++++++++++++++++++++++++++ tools/perf/util/c++/clang.h | 61 ++++ tools/perf/util/jit-helpers.c | 60 ++++ tools/perf/util/jit-helpers.h | 30 ++ tools/perf/util/llvm-utils.c | 76 +++- tools/perf/util/llvm-utils.h | 15 +- tools/perf/util/perf-hooks-list.h | 3 + tools/perf/util/perf-hooks.c | 88 +++++ tools/perf/util/perf-hooks.h | 39 ++ tools/perf/util/probe-event.h | 2 + tools/perf/util/symbol.c | 2 +- tools/perf/util/util-cxx.h | 26 ++ tools/perf/util/util.c | 55 ++- 43 files changed, 1991 insertions(+), 100 deletions(-) create mode 100644 tools/build/feature/test-clang.cpp create mode 100644 tools/build/feature/test-llvm.cpp create mode 100644 tools/perf/tests/clang.c create mode 100644 tools/perf/tests/perf-hooks.c create mode 100644 tools/perf/util/c++/Build create mode 100644 tools/perf/util/c++/bpf-funcs-str.c create mode 100644 tools/perf/util/c++/bpf-helper-str.c create mode 100644 tools/perf/util/c++/clang-bpf-includes.h create mode 100644 tools/perf/util/c++/clang-c.h create mode 100644 tools/perf/util/c++/clang-test.cpp create mode 100644 tools/perf/util/c++/clang.cpp create mode 100644 tools/perf/util/c++/clang.h create mode 100644 tools/perf/util/jit-helpers.c create mode 100644 tools/perf/util/jit-helpers.h create mode 100644 tools/perf/util/perf-hooks-list.h create mode 100644 tools/perf/util/perf-hooks.c create mode 100644 tools/perf/util/perf-hooks.h create mode 100644 tools/perf/util/util-cxx.h
Signature · 1 line
2.10.1
PATCH 1/34 perf tools: Fix kernel version error in ubuntu
On ubuntu the internal kernel version code is different from what can be retrived from uname:
$ uname -r 4.4.0-47-generic $ cat /lib/modules/`uname -r`/build/include/generated/uapi/linux/version.h #define LINUX_VERSION_CODE 263192 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) $ cat /lib/modules/`uname -r`/build/include/generated/utsrelease.h #define UTS_RELEASE "4.4.0-47-generic" #define UTS_UBUNTU_RELEASE_ABI 47 $ cat /proc/version_signature Ubuntu 4.4.0-47.68-generic 4.4.24
The macro LINUX_VERSION_CODE is set to 4.4.24 (263192 == 0x40418), but uname -r reports 4.4.0.
This mismatch causes LINUX_VERSION_CODE macro passed to BPF script become an incorrect value, results in magic failure in BPF loading:
$ sudo ./buildperf/perf record -e ./tools/perf/tests/bpf-script-example.c ls
event syntax error: './tools/perf/tests/bpf-script-example.c'
\___ Failed to load program for unknown reasonAccording to Ubuntu document (https://wiki.ubuntu.com/Kernel/FAQ), the correct kernel version can be retrived through /proc/version_signature, which is ubuntu specific.
This patch checks the existance of /proc/version_signature, and returns version number through parsing this file instead of uname. Version string is untouched (value returns from uname) because uname -r is required to be consistence with path of kbuild directory in /lib/module.
--- tools/perf/util/util.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-)
tools/perf/util/util.c +53 -2
@@ -637,12 +637,63 @@ bool find_process(const char *name)return ret ? false : true; } +static int +fetch_ubuntu_kernel_version (unsigned int *puint) +{ +ssize_t len; +size_t line_len =0 ; +char *ptr, *line =NULL ; +int version, patchlevel, sublevel, err; +FILE *vsig =fopen ("/proc/version_signature" ,"r" ); + +if (!vsig) { +pr_debug ("Open /proc/version_signature failed: %s\n" , +strerror (errno)); +return -1 ; + } + + len =getline (&line, &line_len, vsig); +fclose (vsig); + err =-1 ; +if (len <=0 ) { +pr_debug ("Reading from /proc/version_signature failed: %s\n" , +strerror (errno)); + goto errout; + } + + ptr =strrchr (line,' ' ); +if (!ptr) { +pr_debug ("Parsing /proc/version_signature failed: %s\n" , line); + goto errout; + } + + err =sscanf (ptr +1 ,"%d.%d.%d" , + &version, &patchlevel, &sublevel); +if (err !=3 ) { +pr_debug ("Unable to get kernel version from /proc/version_signature '%s'\n" , + line); + goto errout; + } + +if (puint) + *puint = (version <<16 ) + (patchlevel <<8 ) + sublevel; + err =0 ; +errout: +free (line); +return err; +} +int fetch_kernel_version (unsigned int *puint,char *str,size_t str_size) {struct utsname utsname;int version, patchlevel, sublevel, err; +bool int_ver_ready = false; + +if (access ("/proc/version_signature" ,R_OK ) ==0 ) +if (!fetch_ubuntu_kernel_version (puint)) + int_ver_ready = true;if (uname (&utsname))return -1 ;
@@ -656,12 +707,12 @@ fetch_kernel_version(unsigned int *puint, char *str, &version, &patchlevel, &sublevel);if (err !=3 ) { -pr_debug ("Unablt to get kernel version from uname '%s'\n" , +pr_debug ("Unable to get kernel version from uname '%s'\n" , utsname.release);return -1 ; } -if (puint) +if (puint && !int_ver_ready) *puint = (version <<16 ) + (patchlevel <<8 ) + sublevel;return 0 ; }
Signature · 1 line
2.10.1
tip-bot for Wang Nan
merge bot[tip:perf/core] perf tools: Fix kernel version error in ubuntu
perf tools: Fix kernel version error in ubuntu
Commit-ID: d18acd15c6dfb669f0463afa31ac5343594d2fe2 Gitweb: http://git.kernel.org/tip/d18acd15c6dfb669f0463afa31ac5343594d2fe2 Author: Wang Nan <[email protected]> AuthorDate: Tue, 15 Nov 2016 04:05:44 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Fri, 25 Nov 2016 11:00:30 -0300
perf tools: Fix kernel version error in ubuntu
On ubuntu the internal kernel version code is different from what can be retrived from uname:
$ uname -r 4.4.0-47-generic $ cat /lib/modules/`uname -r`/build/include/generated/uapi/linux/version.h #define LINUX_VERSION_CODE 263192 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) $ cat /lib/modules/`uname -r`/build/include/generated/utsrelease.h #define UTS_RELEASE "4.4.0-47-generic" #define UTS_UBUNTU_RELEASE_ABI 47 $ cat /proc/version_signature Ubuntu 4.4.0-47.68-generic 4.4.24
The macro LINUX_VERSION_CODE is set to 4.4.24 (263192 == 0x40418), but uname -r reports 4.4.0.
This mismatch causes LINUX_VERSION_CODE macro passed to BPF script become an incorrect value, results in magic failure in BPF loading:
$ sudo ./buildperf/perf record -e ./tools/perf/tests/bpf-script-example.c ls
event syntax error: './tools/perf/tests/bpf-script-example.c'
\___ Failed to load program for unknown reasonAccording to Ubuntu document (https://wiki.ubuntu.com/Kernel/FAQ), the correct kernel version can be retrived through /proc/version_signature, which is ubuntu specific.
This patch checks the existance of /proc/version_signature, and returns version number through parsing this file instead of uname. Version string is untouched (value returns from uname) because uname -r is required to be consistence with path of kbuild directory in /lib/module.
--- tools/perf/util/util.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-)
tools/perf/util/util.c +53 -2
@@ -637,12 +637,63 @@ bool find_process(const char *name)return ret ? false : true; } +static int +fetch_ubuntu_kernel_version (unsigned int *puint) +{ +ssize_t len; +size_t line_len =0 ; +char *ptr, *line =NULL ; +int version, patchlevel, sublevel, err; +FILE *vsig =fopen ("/proc/version_signature" ,"r" ); + +if (!vsig) { +pr_debug ("Open /proc/version_signature failed: %s\n" , +strerror (errno)); +return -1 ; + } + + len =getline (&line, &line_len, vsig); +fclose (vsig); + err =-1 ; +if (len <=0 ) { +pr_debug ("Reading from /proc/version_signature failed: %s\n" , +strerror (errno)); + goto errout; + } + + ptr =strrchr (line,' ' ); +if (!ptr) { +pr_debug ("Parsing /proc/version_signature failed: %s\n" , line); + goto errout; + } + + err =sscanf (ptr +1 ,"%d.%d.%d" , + &version, &patchlevel, &sublevel); +if (err !=3 ) { +pr_debug ("Unable to get kernel version from /proc/version_signature '%s'\n" , + line); + goto errout; + } + +if (puint) + *puint = (version <<16 ) + (patchlevel <<8 ) + sublevel; + err =0 ; +errout: +free (line); +return err; +} +int fetch_kernel_version (unsigned int *puint,char *str,size_t str_size) {struct utsname utsname;int version, patchlevel, sublevel, err; +bool int_ver_ready = false; + +if (access ("/proc/version_signature" ,R_OK ) ==0 ) +if (!fetch_ubuntu_kernel_version (puint)) + int_ver_ready = true;if (uname (&utsname))return -1 ;
@@ -656,12 +707,12 @@ fetch_kernel_version(unsigned int *puint, char *str, &version, &patchlevel, &sublevel);if (err !=3 ) { -pr_debug ("Unablt to get kernel version from uname '%s'\n" , +pr_debug ("Unable to get kernel version from uname '%s'\n" , utsname.release);return -1 ; } -if (puint) +if (puint && !int_ver_ready) *puint = (version <<16 ) + (patchlevel <<8 ) + sublevel;return 0 ; }
PATCH 2/34 perf record: Fix segfault when running with suid and kptr_restrict is 1
In reply to Wang NanBefore this patch perf panic if kptr_restrict set to 1 and perf is owned by root with suid set:
$ whoami wangnan $ ls -l ./perf -rwsr-xr-x 1 root root 19781908 Sep 21 19:29 /home/wangnan/perf $ cat /proc/sys/kernel/kptr_restrict 1 $ cat /proc/sys/kernel/perf_event_paranoid -1 $ ./perf record -a Segmentation fault (core dumped)
The reason is perf assumes it is allowed to read kptr from /proc/kallsyms when euid is root, but in fact kernel doesn't allow it reading kptr when euid and uid are not match with each other:
$ cp /bin/cat . $ sudo chown root:root ./cat $ sudo chmod u+s ./cat $ cat /proc/kallsyms | grep do_fork 0000000000000000 T _do_fork <--- kptr is hidden even euid is root $ sudo cat /proc/kallsyms | grep do_fork ffffffff81080230 T _do_fork
See lib/vsprintf.c for kernel side code.
This patch fixes this problem by checking both uid and euid.
--- tools/perf/util/symbol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/symbol.c +1 -1
@@ -1962,7 +1962,7 @@ static bool symbol__read_kptr_restrict(void)char line[8 ];if (fgets (line,sizeof (line), fp) !=NULL ) - value = (geteuid () !=0 ) ? + value = ((geteuid () !=0 ) || (getuid () !=0 )) ? (atoi (line) !=0 ) : (atoi (line) ==2 );
Signature · 1 line
2.10.1
tip-bot for Wang Nan
merge bot[tip:perf/core] perf record: Fix segfault when running with suid and kptr_restrict is 1
perf record: Fix segfault when running with suid and kptr_restrict is 1
Commit-ID: 3dbe46c5245f61328797738c6a0a6cd4bf921f61 Gitweb: http://git.kernel.org/tip/3dbe46c5245f61328797738c6a0a6cd4bf921f61 Author: Wang Nan <[email protected]> AuthorDate: Tue, 15 Nov 2016 04:05:45 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Fri, 25 Nov 2016 11:11:10 -0300
perf record: Fix segfault when running with suid and kptr_restrict is 1
Before this patch perf panics if kptr_restrict is set to 1 and perf is owned by root with suid set:
$ whoami wangnan $ ls -l ./perf -rwsr-xr-x 1 root root 19781908 Sep 21 19:29 /home/wangnan/perf $ cat /proc/sys/kernel/kptr_restrict 1 $ cat /proc/sys/kernel/perf_event_paranoid -1 $ ./perf record -a Segmentation fault (core dumped) $
The reason is that perf assumes it is allowed to read kptr from /proc/kallsyms when euid is root, but in fact the kernel doesn't allow reading kptr when euid and uid do not match with each other:
$ cp /bin/cat . $ sudo chown root:root ./cat $ sudo chmod u+s ./cat $ cat /proc/kallsyms | grep do_fork 0000000000000000 T _do_fork <--- kptr is hidden even euid is root $ sudo cat /proc/kallsyms | grep do_fork ffffffff81080230 T _do_fork
See lib/vsprintf.c for kernel side code.
This patch fixes this problem by checking both uid and euid.
--- tools/perf/util/symbol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/symbol.c +1 -1
@@ -1962,7 +1962,7 @@ static bool symbol__read_kptr_restrict(void)char line[8 ];if (fgets (line,sizeof (line), fp) !=NULL ) - value = (geteuid () !=0 ) ? + value = ((geteuid () !=0 ) || (getuid () !=0 )) ? (atoi (line) !=0 ) : (atoi (line) ==2 );
PATCH 3/34 tools perf: Add missing struct defeinition in probe_event.h
In reply to Wang NanCommit 0b3c2264ae30 ("perf symbols: Fix kallsyms perf test on ppc64le") referes struct symbol in probe_event.h, but forget include its definition. Gcc will complain for it.
--- tools/perf/util/probe-event.h | 2 ++ 1 file changed, 2 insertions(+)
tools/perf/util/probe-event.h +2 -0
@@ -18,6 +18,8 @@ struct probe_conf {extern struct probe_conf probe_conf;extern bool probe_event_dry_run; +struct symbol ; +/* kprobe-tracer and uprobe-tracer tracing point */ struct probe_trace_point {char *realname;/* function real name (if needed) */
Signature · 1 line
2.10.1
tip-bot for Wang Nan
merge bot[tip:perf/core] perf tools: Add missing struct definition in probe_event.h
perf tools: Add missing struct definition in probe_event.h
Commit-ID: d6be16719e0b65f586ae4a301f02407422e6b5dd Gitweb: http://git.kernel.org/tip/d6be16719e0b65f586ae4a301f02407422e6b5dd Author: Wang Nan <[email protected]> AuthorDate: Tue, 15 Nov 2016 04:05:46 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Fri, 25 Nov 2016 11:25:46 -0300
perf tools: Add missing struct definition in probe_event.h
Commit 0b3c2264ae30 ("perf symbols: Fix kallsyms perf test on ppc64le") refers struct symbol in probe_event.h, but forgets to include its definition. Gcc will complain about it when that definition is not added, by sheer luck, by some other header included before probe_event.h.
--- tools/perf/util/probe-event.h | 2 ++ 1 file changed, 2 insertions(+)
tools/perf/util/probe-event.h +2 -0
@@ -18,6 +18,8 @@ struct probe_conf {extern struct probe_conf probe_conf;extern bool probe_event_dry_run; +struct symbol ; +/* kprobe-tracer and uprobe-tracer tracing point */ struct probe_trace_point {char *realname;/* function real name (if needed) */
PATCH 4/34 tools lib bpf: fix maps resolution
In reply to Wang NanFrom: Eric Leblond <[email protected]>
It is not correct to assimilate the elf data of the maps section to an array of map definition. In fact the sizes differ. The offset provided in the symbol section has to be used instead.
This patch fixes a bug causing a elf with two maps not to load correctly.
Wang Nan added:
This patch requires a name for each BPF map, so array of BPF maps is not allowed. This restriction is reasonable, because kernel verifier forbid indexing BPF map from such array unless the index is a fixed value, but if the index is fixed why not merging it into name?
For example:
Program like this:
...
unsigned long cpu = get_smp_processor_id(); int *pval = map_lookup_elem(&map_array[cpu], &key);
...
Generates bytecode like this:
0: (b7) r1 = 0
1: (63) *(u32 *)(r10 -4) = r1
2: (b7) r1 = 680997
3: (63) *(u32 *)(r10 -8) = r1
4: (85) call 8
5: (67) r0 <<= 4
6: (18) r1 = 0x112dd000
8: (0f) r0 += r1
9: (bf) r2 = r10
10: (07) r2 += -4
11: (bf) r1 = r0
12: (85) call 1
Where instruction 8 is the computation, 8 and 11 render r1 to an invalid value for function map_lookup_elem, causes verifier report error.
[Merge bpf_object__init_maps_name into bpf_object__init_maps
Fix segfault for buggy BPF script
Validate obj->maps
]
Cc: Alexei Starovoitov <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Li Zefan <[email protected]>
--- tools/lib/bpf/libbpf.c | 142 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 98 insertions(+), 44 deletions(-)
tools/lib/bpf/libbpf.c +98 -44
@@ -185,6 +185,7 @@ struct bpf_program {struct bpf_map {int fd;char *name; +size_t offset;struct bpf_map_def def;void *priv;bpf_map_clear_priv_t clear_priv;
@@ -513,57 +514,106 @@ bpf_object__init_kversion(struct bpf_object *obj, }static int -bpf_object__init_maps (struct bpf_object *obj,void *data, -size_t size) +bpf_object__validate_maps (struct bpf_object *obj) { -size_t nr_maps;int i; - nr_maps = size /sizeof (struct bpf_map_def ); -if (!data || !nr_maps) { -pr_debug ("%s doesn't need map definition\n" , - obj->path); +/* +* If there's only 1 map, the only error case should have been +* catched in bpf_object__init_maps(). +*/ +if (!obj->maps || !obj->nr_maps || (obj->nr_maps ==1 ))return 0 ; - } -pr_debug ("maps in %s: %zd bytes\n" , obj->path, size); +for (i =1 ; i < obj->nr_maps; i++) { +const struct bpf_map *a = &obj->maps[i -1 ]; +const struct bpf_map *b = &obj->maps[i]; - obj->maps =calloc (nr_maps,sizeof (obj->maps[0 ])); -if (!obj->maps) { -pr_warning ("alloc maps for object failed\n" ); -return -ENOMEM ; +if (b->offset - a->offset <sizeof (struct bpf_map_def )) { +pr_warning ("corrupted map section in %s: map \"%s\" too small\n" , + obj->path, a->name); +return -EINVAL ; + } } - obj->nr_maps = nr_maps; - -for (i =0 ; i < nr_maps; i++) { -struct bpf_map_def *def = &obj->maps[i].def; +return 0 ; +} -/* -* fill all fd with -1 so won't close incorrect -* fd (fd=0 is stdin) when failure (zclose won't close -* negative fd)). -*/ - obj->maps[i].fd =-1 ; +static int compare_bpf_map (const void *_a,const void *_b) +{ +const struct bpf_map *a = _a; +const struct bpf_map *b = _b; -/* Save map definition into obj->maps */ - *def = ((struct bpf_map_def *)data)[i]; - } -return 0 ; +return a->offset - b->offset; }static int -bpf_object__init_maps_name (struct bpf_object *obj) +bpf_object__init_maps (struct bpf_object *obj) { -int i; +int i, map_idx, nr_maps =0 ; +Elf_Scn *scn; +Elf_Data *data;Elf_Data *symbols = obj->efile.symbols; -if (!symbols || obj->efile.maps_shndx <0 ) +if (obj->efile.maps_shndx <0 ) +return -EINVAL ; +if (!symbols) +return -EINVAL ; + + scn =elf_getscn (obj->efile.elf, obj->efile.maps_shndx); +if (scn) + data =elf_getdata (scn,NULL ); +if (!scn || !data) { +pr_warning ("failed to get Elf_Data from map section %d\n" , + obj->efile.maps_shndx);return -EINVAL ; + } +/* +* Count number of maps. Each map has a name. +* Array of maps is not supported: only the first element is +* considered. +* +* TODO: Detect array of map and report error. +*/ for (i =0 ; i < symbols->d_size /sizeof (GElf_Sym); i++) {GElf_Sym sym; -size_t map_idx; + +if (!gelf_getsym (symbols, i, &sym)) +continue ; +if (sym.st_shndx != obj->efile.maps_shndx) +continue ; + nr_maps++; + } + +/* Alloc obj->maps and fill nr_maps. */ +pr_debug ("maps in %s: %d maps in %zd bytes\n" , obj->path, + nr_maps, data->d_size); + +if (!nr_maps) +return 0 ; + + obj->maps =calloc (nr_maps,sizeof (obj->maps[0 ])); +if (!obj->maps) { +pr_warning ("alloc maps for object failed\n" ); +return -ENOMEM ; + } + obj->nr_maps = nr_maps; + +/* +* fill all fd with -1 so won't close incorrect +* fd (fd=0 is stdin) when failure (zclose won't close +* negative fd)). +*/ +for (i =0 ; i < nr_maps; i++) + obj->maps[i].fd =-1 ; + +/* +* Fill obj->maps using data in "maps" section. +*/ +for (i =0 , map_idx =0 ; i < symbols->d_size /sizeof (GElf_Sym); i++) { +GElf_Sym sym;const char *map_name; +struct bpf_map_def *def;if (!gelf_getsym (symbols, i, &sym))continue ;
@@ -573,21 +623,27 @@ bpf_object__init_maps_name(struct bpf_object *obj) map_name =elf_strptr (obj->efile.elf, obj->efile.strtabidx, sym.st_name); - map_idx = sym.st_value /sizeof (struct bpf_map_def ); -if (map_idx >= obj->nr_maps) { -pr_warning ("index of map \"%s\" is buggy: %zu > %zu\n" , - map_name, map_idx, obj->nr_maps); -continue ; + obj->maps[map_idx].offset = sym.st_value; +if (sym.st_value +sizeof (struct bpf_map_def ) > data->d_size) { +pr_warning ("corrupted maps section in %s: last map \"%s\" too small\n" , + obj->path, map_name); +return -EINVAL ; } + obj->maps[map_idx].name =strdup (map_name);if (!obj->maps[map_idx].name) {pr_warning ("failed to alloc map name\n" );return -ENOMEM ; } -pr_debug ("map %zu is \"%s\"\n" , map_idx, +pr_debug ("map %d is \"%s\"\n" , map_idx, obj->maps[map_idx].name); + def = (struct bpf_map_def *)(data->d_buf + sym.st_value); + obj->maps[map_idx].def = *def; + map_idx++; } -return 0 ; + +qsort (obj->maps, obj->nr_maps,sizeof (obj->maps[0 ]), compare_bpf_map); +return bpf_object__validate_maps (obj); }static int bpf_object__elf_collect (struct bpf_object *obj)
@@ -645,11 +701,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj) err =bpf_object__init_kversion (obj, data->d_buf, data->d_size); - else if (strcmp(name, "maps") == 0) { - err = bpf_object__init_maps(obj, data->d_buf, - data->d_size); + else if (strcmp(name, "maps") == 0) obj->efile.maps_shndx = idx; - } else if (sh.sh_type == SHT_SYMTAB) { + else if (sh.sh_type == SHT_SYMTAB) {if (obj->efile.symbols) {pr_warning ("bpf: multiple SYMTAB in %s\n" , obj->path);
@@ -698,7 +752,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)return LIBBPF_ERRNO__FORMAT ; }if (obj->efile.maps_shndx >=0 ) - err =bpf_object__init_maps_name (obj); + err =bpf_object__init_maps (obj); out:return err; }
@@ -807,7 +861,7 @@ bpf_object__create_maps(struct bpf_object *obj)zclose (obj->maps[j].fd);return err; } -pr_debug ("create map: fd=%d\n" , *pfd); +pr_debug ("create map %s: fd=%d\n" , obj->maps[i].name, *pfd); }return 0 ;
Signature · 1 line
2.10.1
tip-bot for Eric Leblond
merge bot[tip:perf/core] tools lib bpf: Fix maps resolution
tools lib bpf: Fix maps resolution
Commit-ID: 4708bbda5cb2f6cdc331744597527143f46394d5 Gitweb: http://git.kernel.org/tip/4708bbda5cb2f6cdc331744597527143f46394d5 Author: Eric Leblond <[email protected]> AuthorDate: Tue, 15 Nov 2016 04:05:47 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Fri, 25 Nov 2016 11:27:33 -0300
tools lib bpf: Fix maps resolution
It is not correct to assimilate the elf data of the maps section to an array of map definition. In fact the sizes differ. The offset provided in the symbol section has to be used instead.
This patch fixes a bug causing a elf with two maps not to load correctly.
Wang Nan added:
This patch requires a name for each BPF map, so array of BPF maps is not allowed. This restriction is reasonable, because kernel verifier forbid indexing BPF map from such array unless the index is a fixed value, but if the index is fixed why not merging it into name?
For example:
Program like this:
...
unsigned long cpu = get_smp_processor_id(); int *pval = map_lookup_elem(&map_array[cpu], &key);
...
Generates bytecode like this:
0: (b7) r1 = 0
1: (63) *(u32 *)(r10 -4) = r1
2: (b7) r1 = 680997
3: (63) *(u32 *)(r10 -8) = r1
4: (85) call 8
5: (67) r0 <<= 4
6: (18) r1 = 0x112dd000
8: (0f) r0 += r1
9: (bf) r2 = r10
10: (07) r2 += -4
11: (bf) r1 = r0
12: (85) call 1
Where instruction 8 is the computation, 8 and 11 render r1 to an invalid value for function map_lookup_elem, causes verifier report error.
[ Merge bpf_object__init_maps_name into bpf_object__init_maps.
Fix segfault for buggy BPF script Validate obj->maps ]
--- tools/lib/bpf/libbpf.c | 142 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 98 insertions(+), 44 deletions(-)
tools/lib/bpf/libbpf.c +98 -44
@@ -185,6 +185,7 @@ struct bpf_program {struct bpf_map {int fd;char *name; +size_t offset;struct bpf_map_def def;void *priv;bpf_map_clear_priv_t clear_priv;
@@ -513,57 +514,106 @@ bpf_object__init_kversion(struct bpf_object *obj, }static int -bpf_object__init_maps (struct bpf_object *obj,void *data, -size_t size) +bpf_object__validate_maps (struct bpf_object *obj) { -size_t nr_maps;int i; - nr_maps = size /sizeof (struct bpf_map_def ); -if (!data || !nr_maps) { -pr_debug ("%s doesn't need map definition\n" , - obj->path); +/* +* If there's only 1 map, the only error case should have been +* catched in bpf_object__init_maps(). +*/ +if (!obj->maps || !obj->nr_maps || (obj->nr_maps ==1 ))return 0 ; - } -pr_debug ("maps in %s: %zd bytes\n" , obj->path, size); +for (i =1 ; i < obj->nr_maps; i++) { +const struct bpf_map *a = &obj->maps[i -1 ]; +const struct bpf_map *b = &obj->maps[i]; - obj->maps =calloc (nr_maps,sizeof (obj->maps[0 ])); -if (!obj->maps) { -pr_warning ("alloc maps for object failed\n" ); -return -ENOMEM ; +if (b->offset - a->offset <sizeof (struct bpf_map_def )) { +pr_warning ("corrupted map section in %s: map \"%s\" too small\n" , + obj->path, a->name); +return -EINVAL ; + } } - obj->nr_maps = nr_maps; - -for (i =0 ; i < nr_maps; i++) { -struct bpf_map_def *def = &obj->maps[i].def; +return 0 ; +} -/* -* fill all fd with -1 so won't close incorrect -* fd (fd=0 is stdin) when failure (zclose won't close -* negative fd)). -*/ - obj->maps[i].fd =-1 ; +static int compare_bpf_map (const void *_a,const void *_b) +{ +const struct bpf_map *a = _a; +const struct bpf_map *b = _b; -/* Save map definition into obj->maps */ - *def = ((struct bpf_map_def *)data)[i]; - } -return 0 ; +return a->offset - b->offset; }static int -bpf_object__init_maps_name (struct bpf_object *obj) +bpf_object__init_maps (struct bpf_object *obj) { -int i; +int i, map_idx, nr_maps =0 ; +Elf_Scn *scn; +Elf_Data *data;Elf_Data *symbols = obj->efile.symbols; -if (!symbols || obj->efile.maps_shndx <0 ) +if (obj->efile.maps_shndx <0 ) +return -EINVAL ; +if (!symbols) +return -EINVAL ; + + scn =elf_getscn (obj->efile.elf, obj->efile.maps_shndx); +if (scn) + data =elf_getdata (scn,NULL ); +if (!scn || !data) { +pr_warning ("failed to get Elf_Data from map section %d\n" , + obj->efile.maps_shndx);return -EINVAL ; + } +/* +* Count number of maps. Each map has a name. +* Array of maps is not supported: only the first element is +* considered. +* +* TODO: Detect array of map and report error. +*/ for (i =0 ; i < symbols->d_size /sizeof (GElf_Sym); i++) {GElf_Sym sym; -size_t map_idx; + +if (!gelf_getsym (symbols, i, &sym)) +continue ; +if (sym.st_shndx != obj->efile.maps_shndx) +continue ; + nr_maps++; + } + +/* Alloc obj->maps and fill nr_maps. */ +pr_debug ("maps in %s: %d maps in %zd bytes\n" , obj->path, + nr_maps, data->d_size); + +if (!nr_maps) +return 0 ; + + obj->maps =calloc (nr_maps,sizeof (obj->maps[0 ])); +if (!obj->maps) { +pr_warning ("alloc maps for object failed\n" ); +return -ENOMEM ; + } + obj->nr_maps = nr_maps; + +/* +* fill all fd with -1 so won't close incorrect +* fd (fd=0 is stdin) when failure (zclose won't close +* negative fd)). +*/ +for (i =0 ; i < nr_maps; i++) + obj->maps[i].fd =-1 ; + +/* +* Fill obj->maps using data in "maps" section. +*/ +for (i =0 , map_idx =0 ; i < symbols->d_size /sizeof (GElf_Sym); i++) { +GElf_Sym sym;const char *map_name; +struct bpf_map_def *def;if (!gelf_getsym (symbols, i, &sym))continue ;
@@ -573,21 +623,27 @@ bpf_object__init_maps_name(struct bpf_object *obj) map_name =elf_strptr (obj->efile.elf, obj->efile.strtabidx, sym.st_name); - map_idx = sym.st_value /sizeof (struct bpf_map_def ); -if (map_idx >= obj->nr_maps) { -pr_warning ("index of map \"%s\" is buggy: %zu > %zu\n" , - map_name, map_idx, obj->nr_maps); -continue ; + obj->maps[map_idx].offset = sym.st_value; +if (sym.st_value +sizeof (struct bpf_map_def ) > data->d_size) { +pr_warning ("corrupted maps section in %s: last map \"%s\" too small\n" , + obj->path, map_name); +return -EINVAL ; } + obj->maps[map_idx].name =strdup (map_name);if (!obj->maps[map_idx].name) {pr_warning ("failed to alloc map name\n" );return -ENOMEM ; } -pr_debug ("map %zu is \"%s\"\n" , map_idx, +pr_debug ("map %d is \"%s\"\n" , map_idx, obj->maps[map_idx].name); + def = (struct bpf_map_def *)(data->d_buf + sym.st_value); + obj->maps[map_idx].def = *def; + map_idx++; } -return 0 ; + +qsort (obj->maps, obj->nr_maps,sizeof (obj->maps[0 ]), compare_bpf_map); +return bpf_object__validate_maps (obj); }static int bpf_object__elf_collect (struct bpf_object *obj)
@@ -645,11 +701,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj) err =bpf_object__init_kversion (obj, data->d_buf, data->d_size); - else if (strcmp(name, "maps") == 0) { - err = bpf_object__init_maps(obj, data->d_buf, - data->d_size); + else if (strcmp(name, "maps") == 0) obj->efile.maps_shndx = idx; - } else if (sh.sh_type == SHT_SYMTAB) { + else if (sh.sh_type == SHT_SYMTAB) {if (obj->efile.symbols) {pr_warning ("bpf: multiple SYMTAB in %s\n" , obj->path);
@@ -698,7 +752,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)return LIBBPF_ERRNO__FORMAT ; }if (obj->efile.maps_shndx >=0 ) - err =bpf_object__init_maps_name (obj); + err =bpf_object__init_maps (obj); out:return err; }
@@ -807,7 +861,7 @@ bpf_object__create_maps(struct bpf_object *obj)zclose (obj->maps[j].fd);return err; } -pr_debug ("create map: fd=%d\n" , *pfd); +pr_debug ("create map %s: fd=%d\n" , obj->maps[i].name, *pfd); }return 0 ;
PATCH 5/34 tools lib bpf: Add missing bpf map functions
In reply to Wang NanAdd more BPF map operations to libbpf.
--- tools/lib/bpf/bpf.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 7 +++++++ 2 files changed, 63 insertions(+)
tools/lib/bpf/bpf.c +56 -0
@@ -110,3 +110,59 @@ 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_delete_elem (int fd,void *key) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.map_fd = fd; + attr.key =ptr_to_u64 (key); + +return sys_bpf (BPF_MAP_DELETE_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)); +} + +int bpf_map_pin (int fd,const char *pathname) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.pathname =ptr_to_u64 ((void *)pathname); + attr.bpf_fd = fd; + +return sys_bpf (BPF_OBJ_PIN , &attr,sizeof (attr)); +} + +int bpf_map_get (const char *pathname) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.pathname =ptr_to_u64 ((void *)pathname); + +return sys_bpf (BPF_OBJ_GET , &attr,sizeof (attr)); +}
tools/lib/bpf/bpf.h +7 -0
@@ -35,4 +35,11 @@ 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_delete_elem (int fd,void *key); +int bpf_map_get_next_key (int fd,void *key,void *next_key); +int bpf_map_pin (int fd,const char *pathname); +int bpf_map_get (const char *pathname); +#endif
Signature · 1 line
2.10.1
Re: [PATCH 05/34] tools lib bpf: Add missing bpf map functions
Quoted 65 lines · Wang Nan
Add more BPF map operations to libbpf.
Signed-off-by Wang Nan <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Li Zefan <[email protected]>--- tools/lib/bpf/bpf.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 7 +++++++ 2 files changed, 63 insertions(+)tools/lib/bpf/bpf.c +46
-0@@ -110,3 +110,59 @@ 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_delete_elem (int fd,void *key) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.map_fd = fd; + attr.key =ptr_to_u64 (key); + +return sys_bpf (BPF_MAP_DELETE_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)); +} + +int bpf_map_pin (int fd,const char *pathname) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.pathname =ptr_to_u64 ((void *)pathname); + attr.bpf_fd = fd; + +return sys_bpf (BPF_OBJ_PIN , &attr,sizeof (attr)); +}
tools/lib/bpf/bpf.c int bpf_map_update_elem(int fd, void *key, void *value, · line 168
Quoted 10 lines
tools/lib/bpf/bpf.c +10
-0+ +int bpf_map_get (const char *pathname) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.pathname =ptr_to_u64 ((void *)pathname); + +return sys_bpf (BPF_OBJ_GET , &attr,sizeof (attr)); +}
bpf_map_{pin,get} should be rename to bpf_obj_{pin,get} since they can be used on BPF program. Thanks to Joe Stringer.
Quoted 16 lines
tools/lib/bpf/bpf.h +7
-0@@ -35,4 +35,11 @@ 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_delete_elem (int fd,void *key); +int bpf_map_get_next_key (int fd,void *key,void *next_key); +int bpf_map_pin (int fd,const char *pathname); +int bpf_map_get (const char *pathname); +#endif
Re: [PATCH 05/34] tools lib bpf: Add missing bpf map functions
Em Thu, Nov 17, 2016 at 11:23:58AM +0800, Wangnan (F) escreveu:
Quoted 72 lines · Wang Nan
Quoted 69 lines
Add more BPF map operations to libbpf.
Signed-off-by Wang Nan <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Li Zefan <[email protected]>--- tools/lib/bpf/bpf.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 7 +++++++ 2 files changed, 63 insertions(+)tools/lib/bpf/bpf.c +51
-0@@ -110,3 +110,59 @@ 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_delete_elem (int fd,void *key) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.map_fd = fd; + attr.key =ptr_to_u64 (key); + +return sys_bpf (BPF_MAP_DELETE_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)); +} + +int bpf_map_pin (int fd,const char *pathname) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.pathname =ptr_to_u64 ((void *)pathname); + attr.bpf_fd = fd; + +return sys_bpf (BPF_OBJ_PIN , &attr,sizeof (attr)); +} + +int bpf_map_get (const char *pathname) +{ +union bpf_attr attr; +
Quoted 8 lines
tools/lib/bpf/bpf.c int bpf_map_update_elem(int fd, void *key, void *value, · line 168
Quoted 5 lines
tools/lib/bpf/bpf.c +5
-0+bzero (&attr,sizeof (attr)); + attr.pathname =ptr_to_u64 ((void *)pathname); + +return sys_bpf (BPF_OBJ_GET , &attr,sizeof (attr)); +}bpf_map_{pin,get} should be rename to bpf_obj_{pin,get} since they can be used on BPF program. Thanks to Joe Stringer.
Ok, and keep:
+int bpf_map_lookup_elem(int fd, void *key, void *value); +int bpf_map_delete_elem(int fd, void *key);
+int bpf_map_get_next_key(int fd, void *key, void *next_key);
as-is?
I'll push te first four patches in this series, will continue after clarifying this one.
- Arnaldo
Quoted 16 lines
Quoted 15 lines
tools/lib/bpf/bpf.h +7
-0@@ -35,4 +35,11 @@ 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_delete_elem (int fd,void *key); +int bpf_map_get_next_key (int fd,void *key,void *next_key); +int bpf_map_pin (int fd,const char *pathname); +int bpf_map_get (const char *pathname); +#endif
PATCH 6/34 tools lib bpf: Add private field for bpf_object
In reply to Wang NanLike other classes defined in libbpf.h (like map and program), allow 'object' class has its own private data.
--- tools/lib/bpf/libbpf.c | 23 +++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 5 +++++ 2 files changed, 28 insertions(+)
tools/lib/bpf/libbpf.c +23 -0
@@ -229,6 +229,10 @@ struct bpf_object {* all objects. */ struct list_head list; + +void *priv; +bpf_object_clear_priv_t clear_priv; +char path[]; };#define obj_elf_valid (o) ((o)->efile.elf)
@@ -1229,6 +1233,9 @@ void bpf_object__close(struct bpf_object *obj)if (!obj)return ; +if (obj->clear_priv) + obj->clear_priv (obj, obj->priv); +bpf_object__elf_finish (obj);bpf_object__unload (obj);
@@ -1282,6 +1289,22 @@ unsigned int bpf_object__kversion(struct bpf_object *obj)return obj ? obj->kern_version :0 ; } +int bpf_object__set_priv (struct bpf_object *obj,void *priv, +bpf_object_clear_priv_t clear_priv) +{ +if (obj->priv && obj->clear_priv) + obj->clear_priv (obj, obj->priv); + + obj->priv = priv; + obj->clear_priv = clear_priv; +return 0 ; +} + +void *bpf_object__priv (struct bpf_object *obj) +{ +return obj ? obj->priv :ERR_PTR (-EINVAL ); +} +struct bpf_program *bpf_program__next (struct bpf_program *prev,struct bpf_object *obj) {
tools/lib/bpf/libbpf.h +5 -0
@@ -79,6 +79,11 @@ struct bpf_object *bpf_object__next(struct bpf_object *prev); (pos) !=NULL ; \ (pos) = (tmp), (tmp) = bpf_object__next(tmp)) +typedef void (*bpf_object_clear_priv_t )(struct bpf_object *,void *); +int bpf_object__set_priv (struct bpf_object *obj,void *priv, +bpf_object_clear_priv_t clear_priv); +void *bpf_object__priv (struct bpf_object *prog); +/* Accessors of bpf_program. */ struct bpf_program ; struct bpf_program *bpf_program__next(struct bpf_program *prog,
Signature · 1 line
2.10.1
PATCH 7/34 tools lib bpf: Retrive bpf_map through offset of bpf_map_def
In reply to Wang NanAdd a new API to libbpf, caller is able to get bpf_map through the offset of bpf_map_def to 'maps' section.
The API will be used to help jitted perf hook code find fd of a map.
--- tools/lib/bpf/libbpf.c | 12 ++++++++++++ tools/lib/bpf/libbpf.h | 8 ++++++++ 2 files changed, 20 insertions(+)
tools/lib/bpf/libbpf.c +12 -0
@@ -1524,3 +1524,15 @@ bpf_object__find_map_by_name(struct bpf_object *obj, const char *name) }return NULL ; } + +struct bpf_map * +bpf_object__find_map_by_offset (struct bpf_object *obj,size_t offset) +{ +int i; + +for (i =0 ; i < obj->nr_maps; i++) { +if (obj->maps[i].offset == offset) +return &obj->maps[i]; + } +return ERR_PTR (-ENOENT ); +}
tools/lib/bpf/libbpf.h +8 -0
@@ -24,6 +24,7 @@#include <stdio.h> #include <stdbool.h> #include <linux/err.h> +#include <sys/types.h> // for size_t enum libbpf_errno { __LIBBPF_ERRNO__START =4000 ,
@@ -200,6 +201,13 @@ struct bpf_map;struct bpf_map *bpf_object__find_map_by_name (struct bpf_object *obj,const char *name); +/* +* Get bpf_map through the offset of corresponding struct bpf_map_def +* in the bpf object file. +*/ +struct bpf_map * +bpf_object__find_map_by_offset (struct bpf_object *obj,size_t offset); +struct bpf_map *bpf_map__next (struct bpf_map *map,struct bpf_object *obj);#define bpf_map__for_each (pos, obj) \
Signature · 1 line
2.10.1
PATCH 8/34 perf tools: Introduce perf hooks
In reply to Wang NanPerf hooks allow hooking user code at perf events. They can be used for manipulation of BPF maps, taking snapshot and reporting results. In this patch two perf hook points are introduced: record_start and record_end.
To avoid buggy user actions, a SIGSEGV signal handler is introduced into 'perf record'. It turns off perf hook if it causes a segfault and report an error to help debugging.
A test case for perf hook is introduced.
Test result:
$ ./buildperf/perf test -v hook 50: Test perf hooks : --- start --- test child forked, pid 10311 SIGSEGV is observed as expected, try to recover. Fatal error (SEGFAULT) in perf hook 'test' test child finished with 0 ---- end ---- Test perf hooks: Ok
--- tools/perf/builtin-record.c | 11 +++++ tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 4 ++ tools/perf/tests/perf-hooks.c | 44 ++++++++++++++++++++ tools/perf/tests/tests.h | 1 + tools/perf/util/Build | 2 + tools/perf/util/perf-hooks-list.h | 3 ++ tools/perf/util/perf-hooks.c | 84 +++++++++++++++++++++++++++++++++++++++ tools/perf/util/perf-hooks.h | 37 +++++++++++++++++ 9 files changed, 187 insertions(+) create mode 100644 tools/perf/tests/perf-hooks.c create mode 100644 tools/perf/util/perf-hooks-list.h create mode 100644 tools/perf/util/perf-hooks.c create mode 100644 tools/perf/util/perf-hooks.h
tools/perf/builtin-record.c +11 -0
@@ -37,6 +37,7 @@#include "util/llvm-utils.h" #include "util/bpf-loader.h" #include "util/trigger.h" +#include "util/perf-hooks.h" #include "asm/bug.h" #include <unistd.h>
@@ -206,6 +207,12 @@ static void sig_handler(int sig) done =1 ; } +static void sigsegv_handler (int sig) +{ +perf_hooks__recover (); +sighandler_dump_stack (sig); +} +static void record__sig_exit (void ) {if (signr ==-1 )
@@ -833,6 +840,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)signal (SIGCHLD , sig_handler);signal (SIGINT , sig_handler);signal (SIGTERM , sig_handler); +signal (SIGSEGV , sigsegv_handler);if (rec->opts.auxtrace_snapshot_mode || rec->switch_output) {signal (SIGUSR2 , snapshot_sig_handler);
@@ -970,6 +978,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)trigger_ready (&auxtrace_snapshot_trigger);trigger_ready (&switch_output_trigger); +perf_hooks__invoke_record_start ();for (;;) {unsigned long long hits = rec->samples;
@@ -1114,6 +1123,8 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) } } +perf_hooks__invoke_record_end (); +if (!err && !quiet) {char samples[128 ]; const char *postfix = rec->timestamp_filename ?
tools/perf/tests/Build +1 -0
@@ -42,6 +42,7 @@ perf-y += backward-ring-buffer.o perf-y += sdt.o perf-y += is_printable_array.o perf-y += bitmap.o +perf-y += perf-hooks.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir)
tools/perf/tests/builtin-test.c +4 -0
@@ -230,6 +230,10 @@ static struct test generic_tests[] = { .func = test__bitmap_print, }, { + .desc = "Test perf hooks", + .func = test__perf_hooks, + }, + { .func = NULL, }, };
tools/perf/tests/perf-hooks.c new file +44 -0
44 lines not on this page · raw message
tools/perf/tests/tests.h +1 -0
@@ -91,6 +91,7 @@ int test__cpu_map_print(int subtest);int test__sdt_event (int subtest);int test__is_printable_array (int subtest);int test__bitmap_print (int subtest); +int test__perf_hooks (int subtest);#if defined(__arm__) || defined(__aarch64__)#ifdef HAVE_DWARF_UNWIND_SUPPORT
tools/perf/util/Build +2 -0
@@ -123,6 +123,8 @@ libperf-$(CONFIG_LIBELF) += genelf.o libperf-$(CONFIG_DWARF) += genelf_debug.o endif +libperf-y += perf-hooks.o + CFLAGS_config.o += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))" # avoid compiler warnings in 32-bit mode CFLAGS_genelf_debug.o += -Wno-packed
tools/perf/util/perf-hooks-list.h new file +3 -0
@@ -0,0 +1,3 @@ +PERF_HOOK(record_start) +PERF_HOOK(record_end) +PERF_HOOK(test)
tools/perf/util/perf-hooks.c new file +84 -0
84 lines not on this page · raw message
tools/perf/util/perf-hooks.h new file +37 -0
@@ -0,0 +1,37 @@ +#ifndef PERF_UTIL_PERF_HOOKS_H +#define PERF_UTIL_PERF_HOOKS_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void (*perf_hook_func_t )(void ); +struct perf_hook_desc { +const char *const hook_name; +perf_hook_func_t *const p_hook_func; +}; + +extern void perf_hooks__invoke (const struct perf_hook_desc *); +extern void perf_hooks__recover (void ); + +#define PERF_HOOK (name) \ +extern struct perf_hook_desc __perf_hook_desc_##name; \ +static inline void perf_hooks__invoke_##name(void) \ +{ \ + perf_hooks__invoke(&__perf_hook_desc_##name); \ +} + +#include "perf-hooks-list.h" +#undef PERF_HOOK + +extern int +perf_hooks__set_hook (const char *hook_name, +perf_hook_func_t hook_func); + +extern perf_hook_func_t +perf_hooks__get_hook (const char *hook_name); + +#ifdef __cplusplus +} +#endif +#endif
Signature · 1 line
2.10.1
PATCH 9/34 perf tools: Pass context to perf hook functions
In reply to Wang NanPass a pointer to perf hook functions so they receive context information created durnig setup.
--- tools/perf/tests/perf-hooks.c | 14 +++++++++----- tools/perf/util/perf-hooks.c | 10 +++++++--- tools/perf/util/perf-hooks.h | 6 ++++-- 3 files changed, 20 insertions(+), 10 deletions(-)
tools/perf/tests/perf-hooks.c +9 -5
@@ -15,13 +15,13 @@ static void sigsegv_handler(int sig __maybe_unused)exit (-1 ); } -static int hook_flags; -static void the_hook (void ) +static void the_hook (void *_hook_flags) { +int *hook_flags = _hook_flags;int *p =NULL ; - hook_flags =1234 ; + *hook_flags =1234 ;/* Generate a segfault, test perf_hooks__recover */ *p =0 ;
@@ -29,13 +29,17 @@ static void the_hook(void)int test__perf_hooks (int subtest__maybe_unused ) { +int hook_flags =0 ; +signal (SIGSEGV , sigsegv_handler); -perf_hooks__set_hook ("test" , the_hook); +perf_hooks__set_hook ("test" , the_hook, &hook_flags);perf_hooks__invoke_test ();/* hook is triggered? */ -if (hook_flags !=1234 ) +if (hook_flags !=1234 ) { +pr_debug ("Setting failed: %d (%p)\n" , hook_flags, &hook_flags);return TEST_FAIL ; + }/* the buggy hook is removed? */ if (perf_hooks__get_hook ("test" ))
tools/perf/util/perf-hooks.c +7 -3
@@ -27,7 +27,7 @@ void perf_hooks__invoke(const struct perf_hook_desc *desc) *(current_perf_hook->p_hook_func) =NULL ; }else { current_perf_hook = desc; - (**desc->p_hook_func)(); + (**desc->p_hook_func)(desc->hook_ctx); } current_perf_hook =NULL ; }
@@ -41,7 +41,9 @@ void perf_hooks__recover(void)#define PERF_HOOK (name) \ perf_hook_func_t __perf_hook_func_##name = NULL; \ struct perf_hook_desc __perf_hook_desc_##name = \ - {.hook_name = #name, .p_hook_func = &__perf_hook_func_##name}; + {.hook_name = #name, \ + .p_hook_func = &__perf_hook_func_##name, \ + .hook_ctx = NULL};#include "perf-hooks-list.h" #undef PERF_HOOK
@@ -54,7 +56,8 @@ static struct perf_hook_desc *perf_hooks[] = {#undef PERF_HOOKint perf_hooks__set_hook (const char *hook_name, -perf_hook_func_t hook_func) +perf_hook_func_t hook_func, +void *hook_ctx) {unsigned int i;
@@ -65,6 +68,7 @@ int perf_hooks__set_hook(const char *hook_name,if (*(perf_hooks[i]->p_hook_func))pr_warning ("Overwrite existing hook: %s\n" , hook_name); *(perf_hooks[i]->p_hook_func) = hook_func; + perf_hooks[i]->hook_ctx = hook_ctx;return 0 ; }return -ENOENT ;
tools/perf/util/perf-hooks.h +4 -2
@@ -5,10 +5,11 @@extern "C" {#endif -typedef void (*perf_hook_func_t )(void ); +typedef void (*perf_hook_func_t )(void *ctx);struct perf_hook_desc {const char *const hook_name;perf_hook_func_t *const p_hook_func; +void *hook_ctx; };extern void perf_hooks__invoke (const struct perf_hook_desc *);
@@ -26,7 +27,8 @@ static inline void perf_hooks__invoke_##name(void) \extern int perf_hooks__set_hook (const char *hook_name, -perf_hook_func_t hook_func); +perf_hook_func_t hook_func, +void *hook_ctx);extern perf_hook_func_t perf_hooks__get_hook (const char *hook_name);
Signature · 1 line
2.10.1
PATCH 10/34 perf llvm: Extract helpers in llvm-utils.c
In reply to Wang NanFollowing commits will use builtin clang to compile BPF script. llvm__get_kbuild_opts() and llvm__get_nr_cpus() are extracted to help building '-DKERNEL_VERSION_CODE' and '-D__NR_CPUS__' macros.
Doing object dumping in bpf loader, so futher builtin clang compiling needn't consider it.
--- tools/perf/util/bpf-loader.c | 4 +++ tools/perf/util/llvm-utils.c | 76 +++++++++++++++++++++++++++++++++----------- tools/perf/util/llvm-utils.h | 6 ++++ 3 files changed, 68 insertions(+), 18 deletions(-)
tools/perf/util/bpf-loader.c +4 -0
@@ -90,6 +90,10 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)if (err)return ERR_PTR (-BPF_LOADER_ERRNO__COMPILE ); obj =bpf_object__open_buffer (obj_buf, obj_buf_sz, filename); + +if (!IS_ERR (obj) && llvm_param.dump_obj) +llvm__dump_obj (filename, obj_buf, obj_buf_sz); +free (obj_buf); }else obj =bpf_object__open (filename);
tools/perf/util/llvm-utils.c +58 -18
128 lines not on this page · raw message
tools/perf/util/llvm-utils.h +6 -0
@@ -50,4 +50,10 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, size_t *p_obj_buf_sz);/* This function is for test__llvm() use only */ int llvm__search_clang (void ); + +/* Following functions are reused by builtin clang support */ +void llvm__get_kbuild_opts (char **kbuild_dir,char **kbuild_include_opts); +int llvm__get_nr_cpus (void ); + +void llvm__dump_obj (const char *path,void *obj_buf,size_t size);#endif
Signature · 1 line
2.10.1
PATCH 11/34 tools build: Add feature detection for LLVM
In reply to Wang NanCheck if basic LLVM compiling environment is ready.
Use llvm-config to detect include and library directories. Avoid using 'llvm-config --cxxflags' because its result contain some unwanted flags like --sysroot (if LLVM is built by yocto).
Use '?=' to set LLVM_CONFIG, so explicitly passing LLVM_CONFIG to make would override it.
Use 'llvm-config --libs BPF' to check if BPF backend is compiled in. Since now BPF bytecode is the only required backend, no need to waste time linking llvm and clang if BPF backend is missing. This also introduce an implicit requirement that LLVM should be new enough. Old LLVM doesn't support BPF backend.
--- tools/build/feature/Makefile | 8 ++++++++ tools/build/feature/test-llvm.cpp | 8 ++++++++ 2 files changed, 16 insertions(+) create mode 100644 tools/build/feature/test-llvm.cpp
tools/build/feature/Makefile +8 -0
@@ -55,6 +55,7 @@ FILES := $(addprefix $(OUTPUT),$(FILES)) CC := $(CROSS_COMPILE)gcc -MD CXX := $(CROSS_COMPILE)g++ -MD PKG_CONFIG := $(CROSS_COMPILE)pkg-config +LLVM_CONFIG ?= llvm-config all: $(FILES)
@@ -229,6 +230,13 @@ $(OUTPUT)test-cxx.bin: $(OUTPUT)test-jvmti.bin: $(BUILD) +$(OUTPUT)test-llvm.bin: + $(BUILDXX) -std=gnu++11 \ + -I$(shell $(LLVM_CONFIG) --includedir) \ + -L$(shell $(LLVM_CONFIG) --libdir) \ + $(shell $(LLVM_CONFIG) --libs Core BPF) \ + $(shell $(LLVM_CONFIG) --system-libs) + -include $(OUTPUT)*.d ###############################
tools/build/feature/test-llvm.cpp new file +8 -0
@@ -0,0 +1,8 @@ +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/raw_ostream.h" +int main() +{ + llvm::errs() << "Hello World!\n"; + llvm::llvm_shutdown(); + return 0; +}
Signature · 1 line
2.10.1
PATCH 12/34 tools build: Add feature detection for clang
In reply to Wang NanCheck if basic clang compiling environment is ready.
Doesn't like 'llvm-config --libs' which can returns llvm libraries in right order and duplicates some libraries if necessary, there's no correspondence for clang libraries (-lclangxxx). to avoid extra complexity and to avoid new clang breaking libraries ordering, use --start-group and --end-group.
In this test case, manually identify required clang libs and hope it to be stable. Putting all clang libraries here is possible (use make's wildcard), but then feature checking becomes very slow.
--- tools/build/feature/Makefile | 10 ++++++++++ tools/build/feature/test-clang.cpp | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tools/build/feature/test-clang.cpp
tools/build/feature/Makefile +10 -0
@@ -237,6 +237,16 @@ $(OUTPUT)test-llvm.bin: $(shell $(LLVM_CONFIG) --libs Core BPF) \ $(shell $(LLVM_CONFIG) --system-libs) +$(OUTPUT)test-clang.bin: + $(BUILDXX) -std=gnu++11 \ + -I$(shell $(LLVM_CONFIG) --includedir) \ + -L$(shell $(LLVM_CONFIG) --libdir) \ + -Wl,--start-group -lclangBasic -lclangDriver \ + -lclangFrontend -lclangEdit -lclangLex \ + -lclangAST -Wl,--end-group \ + $(shell $(LLVM_CONFIG) --libs Core option) \ + $(shell $(LLVM_CONFIG) --system-libs) + -include $(OUTPUT)*.d ###############################
tools/build/feature/test-clang.cpp new file +21 -0
@@ -0,0 +1,21 @@ +#include "clang/Basic/VirtualFileSystem.h" +#include "clang/Driver/Driver.h" +#include "clang/Frontend/TextDiagnosticPrinter.h" +#include "llvm/ADT/IntrusiveRefCntPtr.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/raw_ostream.h" + +using namespace clang; +using namespace clang::driver; + +int main() +{ + IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); + + DiagnosticsEngine Diags(DiagID, &*DiagOpts); + Driver TheDriver("test", "bpf-pc-linux", Diags); + + llvm::llvm_shutdown(); + return 0; +}
Signature · 1 line
2.10.1
PATCH 13/34 perf build: Add clang and llvm compile and linking support
In reply to Wang NanAdd necessary c++ flags and link libraries to support builtin clang and LLVM. Add all llvm and clang libraries, so don't need to worry about clang changes its libraries setting. However, linking perf would take much longer than usual.
--- tools/perf/Makefile.config | 35 +++++++++++++++++++++++++++++++++++ tools/perf/Makefile.perf | 23 ++++++++++++++++++++++- tools/perf/tests/make | 2 ++ 3 files changed, 59 insertions(+), 1 deletion(-)
tools/perf/Makefile.config +35 -0
@@ -136,6 +136,7 @@ endif # Treat warnings as errors unless directed not to ifneq ($(WERROR),0) CFLAGS += -Werror + CXXFLAGS += -Werror endif ifndef DEBUG
@@ -182,6 +183,13 @@ CFLAGS += -Wall CFLAGS += -Wextra CFLAGS += -std=gnu99 +CXXFLAGS += -std=gnu++11 -fno-exceptions -fno-rtti +CXXFLAGS += -Wall +CXXFLAGS += -fno-omit-frame-pointer +CXXFLAGS += -ggdb3 +CXXFLAGS += -funwind-tables +CXXFLAGS += -Wno-strict-aliasing + # Enforce a non-executable stack, as we may regress (again) in the future by # adding assembler files missing the .GNU-stack linker note. LDFLAGS += -Wl,-z,noexecstack
@@ -783,6 +791,33 @@ ifndef NO_JVMTI endif endif +USE_CXX = 0 +USE_CLANGLLVM = 0 +ifdef LIBCLANGLLVM + $(call feature_check,cxx) + ifneq ($(feature-cxx), 1) + msg := $(warning No g++ found, disable clang and llvm support. Please install g++) + else + $(call feature_check,llvm) + ifneq ($(feature-llvm), 1) + msg := $(warning No libLLVM found, disable clang and llvm support. Please install llvm-dev) + else + $(call feature_check,clang) + ifneq ($(feature-clang), 1) + msg := $(warning No libclang found, disable clang and llvm support. Please install libclang-dev) + else + CFLAGS += -DHAVE_LIBCLANGLLVM_SUPPORT + CXXFLAGS += -DHAVE_LIBCLANGLLVM_SUPPORT -I$(shell $(LLVM_CONFIG) --includedir) + $(call detected,CONFIG_CXX) + $(call detected,CONFIG_CLANGLLVM) + USE_CXX = 1 + USE_LLVM = 1 + USE_CLANG = 1 + endif + endif + endif +endif + # Among the variables below, these: # perfexecdir # template_dir
tools/perf/Makefile.perf +22 -1
@@ -88,6 +88,10 @@ include ../scripts/utilities.mak # and bypass the feature detection # # Define NO_JVMTI if you do not want jvmti agent built +# +# Define LIBCLANGLLVM if you DO want builtin clang and llvm support. +# When selected, pass LLVM_CONFIG=/path/to/llvm-config to `make' if +# llvm-config is not in $PATH. # As per kernel Makefile, avoid funny character set dependencies unexport LC_ALL
@@ -143,6 +147,7 @@ endef $(call allow-override,CC,$(CROSS_COMPILE)gcc) $(call allow-override,AR,$(CROSS_COMPILE)ar) $(call allow-override,LD,$(CROSS_COMPILE)ld) +$(call allow-override,CXX,$(CROSS_COMPILE)g++) LD += $(EXTRA_LDFLAGS)
@@ -151,6 +156,7 @@ HOSTLD ?= ld HOSTAR ?= ar PKG_CONFIG = $(CROSS_COMPILE)pkg-config +LLVM_CONFIG ?= llvm-config RM = rm -f LN = ln -f
@@ -338,6 +344,21 @@ endif LIBS = -Wl,--whole-archive $(PERFLIBS) -Wl,--no-whole-archive -Wl,--start-group $(EXTLIBS) -Wl,--end-group +ifeq ($(USE_CLANG), 1) + CLANGLIBS_LIST = AST Basic CodeGen Driver Frontend Lex Tooling Edit Sema Analysis Parse Serialization + LIBCLANG = $(foreach l,$(CLANGLIBS_LIST),$(wildcard $(shell $(LLVM_CONFIG) --libdir)/libclang$(l).a)) + LIBS += -Wl,--start-group $(LIBCLANG) -Wl,--end-group +endif + +ifeq ($(USE_LLVM), 1) + LIBLLVM = $(shell $(LLVM_CONFIG) --libs all) $(shell $(LLVM_CONFIG) --system-libs) + LIBS += -L$(shell $(LLVM_CONFIG) --libdir) $(LIBLLVM) +endif + +ifeq ($(USE_CXX), 1) + LIBS += -lstdc++ +endif + export INSTALL SHELL_PATH ### Build rules
@@ -356,7 +377,7 @@ strip: $(PROGRAMS) $(OUTPUT)perf PERF_IN := $(OUTPUT)perf-in.o -export srctree OUTPUT RM CC LD AR CFLAGS V BISON FLEX AWK +export srctree OUTPUT RM CC CXX LD AR CFLAGS CXXFLAGS V BISON FLEX AWK export HOSTCC HOSTLD HOSTAR include $(srctree)/tools/build/Makefile.include
tools/perf/tests/make +2 -0
@@ -83,6 +83,7 @@ make_no_libbpf := NO_LIBBPF=1 make_no_libcrypto := NO_LIBCRYPTO=1 make_with_babeltrace:= LIBBABELTRACE=1 make_no_sdt := NO_SDT=1 +make_with_clangllvm := LIBCLANGLLVM=1 make_tags := tags make_cscope := cscope make_help := help
@@ -139,6 +140,7 @@ run += make_no_libbionic run += make_no_auxtrace run += make_no_libbpf run += make_with_babeltrace +run += make_with_clangllvm run += make_help run += make_doc run += make_perf_o
Signature · 1 line
2.10.1
PATCH 14/34 perf clang: Add builtin clang support ant test case
In reply to Wang NanAdd basic clang support in clang.cpp and test__clang() testcase. The first testcase checks if builtin clang is able to generate LLVM IR.
tests/clang.c is a proxy. Real testcase resides in utils/c++/clang-test.cpp in c++ and exports C interface to perf test subsystem.
Test result:
$ perf test -v clang 51: Test builtin clang support : 51.1: Test builtin clang compile C source to IR : --- start --- test child forked, pid 13215 test child finished with 0 ---- end ---- Test builtin clang support subtest 0: Ok
--- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 9 ++++ tools/perf/tests/clang.c | 42 +++++++++++++++++ tools/perf/tests/tests.h | 3 ++ tools/perf/util/Build | 2 + tools/perf/util/c++/Build | 2 + tools/perf/util/c++/clang-c.h | 16 +++++++ tools/perf/util/c++/clang-test.cpp | 31 ++++++++++++ tools/perf/util/c++/clang.cpp | 96 ++++++++++++++++++++++++++++++++++++++ tools/perf/util/c++/clang.h | 16 +++++++ 10 files changed, 218 insertions(+) create mode 100644 tools/perf/tests/clang.c create mode 100644 tools/perf/util/c++/Build create mode 100644 tools/perf/util/c++/clang-c.h create mode 100644 tools/perf/util/c++/clang-test.cpp create mode 100644 tools/perf/util/c++/clang.cpp create mode 100644 tools/perf/util/c++/clang.h
tools/perf/tests/Build +1 -0
@@ -43,6 +43,7 @@ perf-y += sdt.o perf-y += is_printable_array.o perf-y += bitmap.o perf-y += perf-hooks.o +perf-y += clang.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir)
tools/perf/tests/builtin-test.c +9 -0
@@ -234,6 +234,15 @@ static struct test generic_tests[] = { .func = test__perf_hooks, }, { + .desc = "Test builtin clang support", + .func = test__clang, + .subtest = { + .skip_if_fail = true, + .get_nr = test__clang_subtest_get_nr, + .get_desc = test__clang_subtest_get_desc, + } + }, + { .func = NULL, }, };
tools/perf/tests/clang.c new file +42 -0
42 lines not on this page · raw message
tools/perf/tests/tests.h +3 -0
@@ -92,6 +92,9 @@ int test__sdt_event(int subtest);int test__is_printable_array (int subtest);int test__bitmap_print (int subtest);int test__perf_hooks (int subtest); +int test__clang (int subtest); +const char *test__clang_subtest_get_desc (int subtest); +int test__clang_subtest_get_nr (void );#if defined(__arm__) || defined(__aarch64__)#ifdef HAVE_DWARF_UNWIND_SUPPORT
tools/perf/util/Build +2 -0
@@ -125,6 +125,8 @@ endif libperf-y += perf-hooks.o +libperf-$(CONFIG_CXX) += c++/ + CFLAGS_config.o += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))" # avoid compiler warnings in 32-bit mode CFLAGS_genelf_debug.o += -Wno-packed
tools/perf/util/c++/Build new file +2 -0
@@ -0,0 +1,2 @@ +libperf-$(CONFIG_CLANGLLVM) += clang.o +libperf-$(CONFIG_CLANGLLVM) += clang-test.o
tools/perf/util/c++/clang-c.h new file +16 -0
@@ -0,0 +1,16 @@ +#ifndef PERF_UTIL_CLANG_C_H +#define PERF_UTIL_CLANG_C_H + +#ifdef __cplusplus +extern "C" { +#endif + +extern void perf_clang__init (void ); +extern void perf_clang__cleanup (void ); + +extern int test__clang_to_IR (void ); + +#ifdef __cplusplus +} +#endif +#endif
tools/perf/util/c++/clang-test.cpp new file +31 -0
@@ -0,0 +1,31 @@ +#include "clang.h" +#include "clang-c.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/LLVMContext.h" + +class perf_clang_scope { +public: + explicit perf_clang_scope() {perf_clang__init();} + ~perf_clang_scope() {perf_clang__cleanup();} +}; + +extern "C" { + +int test__clang_to_IR(void) +{ + perf_clang_scope _scope; + + std::unique_ptr<llvm::Module> M = + perf::getModuleFromSource("perf-test.c", + "int myfunc(void) {return 1;}"); + + if (!M) + return -1; + + for (llvm::Function& F : *M) + if (F.getName() == "myfunc") + return 0; + return -1; +} + +}
tools/perf/util/c++/clang.cpp new file +96 -0
96 lines not on this page · raw message
tools/perf/util/c++/clang.h new file +16 -0
@@ -0,0 +1,16 @@ +#ifndef PERF_UTIL_CLANG_H +#define PERF_UTIL_CLANG_H + +#include "llvm/ADT/StringRef.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include <memory> +namespace perf { + +using namespace llvm; + +std::unique_ptr<Module> +getModuleFromSource(StringRef Name, StringRef Content); + +} +#endif
Signature · 1 line
2.10.1
PATCH 15/34 perf clang: Use real file system for #include
In reply to Wang NanUtilize clang's OverlayFileSystem facility, allow CompilerInstance to access real file system.
With this patch '#include' directive can be used.
Add a new getModuleFromSource for real file.
--- tools/perf/util/c++/clang.cpp | 44 +++++++++++++++++++++++++++++++------------ tools/perf/util/c++/clang.h | 3 +++ 2 files changed, 35 insertions(+), 12 deletions(-)
tools/perf/util/c++/clang.cpp +32 -12
@@ -15,6 +15,7 @@ #include "clang/Tooling/Tooling.h" #include "llvm/IR/Module.h" #include "llvm/Option/Option.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/ManagedStatic.h" #include <memory>
@@ -27,14 +28,6 @@ static std::unique_ptr<llvm::LLVMContext> LLVMCtx; using namespace clang; -static vfs::InMemoryFileSystem * -buildVFS(StringRef& Name, StringRef& Content) -{ - vfs::InMemoryFileSystem *VFS = new vfs::InMemoryFileSystem(true); - VFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content)); - return VFS; -} - static CompilerInvocation * createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags) {
@@ -60,17 +53,17 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags) return CI; } -std::unique_ptr<llvm::Module> -getModuleFromSource(StringRef Name, StringRef Content) +static std::unique_ptr<llvm::Module> +getModuleFromSource(StringRef Path, + IntrusiveRefCntPtr<vfs::FileSystem> VFS) { CompilerInstance Clang; Clang.createDiagnostics(); - IntrusiveRefCntPtr<vfs::FileSystem> VFS = buildVFS(Name, Content); Clang.setVirtualFileSystem(&*VFS); IntrusiveRefCntPtr<CompilerInvocation> CI = - createCompilerInvocation(Name, Clang.getDiagnostics()); + createCompilerInvocation(Path, Clang.getDiagnostics()); Clang.setInvocation(&*CI); std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
@@ -80,6 +73,33 @@ getModuleFromSource(StringRef Name, StringRef Content) return Act->takeModule(); } +std::unique_ptr<llvm::Module> +getModuleFromSource(StringRef Name, StringRef Content) +{ + using namespace vfs; + + llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS( + new OverlayFileSystem(getRealFileSystem())); + llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS( + new InMemoryFileSystem(true)); + + /* + * pushOverlay helps setting working dir for MemFS. Must call + * before addFile. + */ + OverlayFS->pushOverlay(MemFS); + MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content)); + + return getModuleFromSource(Name, OverlayFS); +} + +std::unique_ptr<llvm::Module> +getModuleFromSource(StringRef Path) +{ + IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem()); + return getModuleFromSource(Path, VFS); +} + } extern "C" {
tools/perf/util/c++/clang.h +3 -0
@@ -12,5 +12,8 @@ using namespace llvm; std::unique_ptr<Module> getModuleFromSource(StringRef Name, StringRef Content); +std::unique_ptr<Module> +getModuleFromSource(StringRef Path); + }#endif
Signature · 1 line
2.10.1
PATCH 16/34 perf clang: Allow passing CFLAGS to builtin clang
In reply to Wang NanImprove getModuleFromSource() API to accept a cflags list. This feature will be used to pass LINUX_VERSION_CODE and -I flags.
--- tools/perf/util/c++/clang-test.cpp | 5 +++-- tools/perf/util/c++/clang.cpp | 21 +++++++++++++-------- tools/perf/util/c++/clang.h | 8 ++++++-- 3 files changed, 22 insertions(+), 12 deletions(-)
tools/perf/util/c++/clang-test.cpp +3 -2
@@ -16,8 +16,9 @@ int test__clang_to_IR(void) perf_clang_scope _scope; std::unique_ptr<llvm::Module> M = - perf::getModuleFromSource("perf-test.c", - "int myfunc(void) {return 1;}"); + perf::getModuleFromSource({"-DRESULT=1"}, + "perf-test.c", + "int myfunc(void) {return RESULT;}"); if (!M) return -1;
tools/perf/util/c++/clang.cpp +13 -8
@@ -29,7 +29,8 @@ static std::unique_ptr<llvm::LLVMContext> LLVMCtx; using namespace clang; static CompilerInvocation * -createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags) +createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path, + DiagnosticsEngine& Diags) { llvm::opt::ArgStringList CCArgs { "-cc1",
@@ -45,6 +46,8 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags) "-Wno-unused-value", "-Wno-pointer-sign", "-x", "c"}; + + CCArgs.append(CFlags.begin(), CFlags.end()); CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs); FrontendOptions& Opts = CI->getFrontendOpts();
@@ -54,8 +57,8 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags) } static std::unique_ptr<llvm::Module> -getModuleFromSource(StringRef Path, - IntrusiveRefCntPtr<vfs::FileSystem> VFS) +getModuleFromSource(llvm::opt::ArgStringList CFlags, + StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS) { CompilerInstance Clang; Clang.createDiagnostics();
@@ -63,7 +66,8 @@ getModuleFromSource(StringRef Path, Clang.setVirtualFileSystem(&*VFS); IntrusiveRefCntPtr<CompilerInvocation> CI = - createCompilerInvocation(Path, Clang.getDiagnostics()); + createCompilerInvocation(std::move(CFlags), Path, + Clang.getDiagnostics()); Clang.setInvocation(&*CI); std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
@@ -74,7 +78,8 @@ getModuleFromSource(StringRef Path, } std::unique_ptr<llvm::Module> -getModuleFromSource(StringRef Name, StringRef Content) +getModuleFromSource(llvm::opt::ArgStringList CFlags, + StringRef Name, StringRef Content) { using namespace vfs;
@@ -90,14 +95,14 @@ getModuleFromSource(StringRef Name, StringRef Content) OverlayFS->pushOverlay(MemFS); MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content)); - return getModuleFromSource(Name, OverlayFS); + return getModuleFromSource(std::move(CFlags), Name, OverlayFS); } std::unique_ptr<llvm::Module> -getModuleFromSource(StringRef Path) +getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path) { IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem()); - return getModuleFromSource(Path, VFS); + return getModuleFromSource(std::move(CFlags), Path, VFS); } }
tools/perf/util/c++/clang.h +6 -2
@@ -4,16 +4,20 @@#include "llvm/ADT/StringRef.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" +#include "llvm/Option/Option.h" #include <memory> +namespace perf { using namespace llvm; std::unique_ptr<Module> -getModuleFromSource(StringRef Name, StringRef Content); +getModuleFromSource(opt::ArgStringList CFlags, + StringRef Name, StringRef Content); std::unique_ptr<Module> -getModuleFromSource(StringRef Path); +getModuleFromSource(opt::ArgStringList CFlags, + StringRef Path); }#endif
Signature · 1 line
2.10.1
PATCH 17/34 perf clang: Update test case to use real BPF script
In reply to Wang NanAllow C++ code to use util.h and tests/llvm.h. Let 'perf test' compile a real BPF script.
--- tools/perf/Makefile.config | 27 +++++++++++++++------------ tools/perf/tests/llvm.h | 7 +++++++ tools/perf/util/c++/clang-test.cpp | 17 ++++++++++++++--- tools/perf/util/util-cxx.h | 26 ++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 tools/perf/util/util-cxx.h
tools/perf/Makefile.config +15 -12
@@ -212,24 +212,27 @@ ifeq ($(DEBUG),0) endif endif -CFLAGS += -I$(src-perf)/util/include -CFLAGS += -I$(src-perf)/arch/$(ARCH)/include -CFLAGS += -I$(srctree)/tools/include/uapi -CFLAGS += -I$(srctree)/tools/include/ -CFLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/uapi -CFLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/ -CFLAGS += -I$(srctree)/tools/arch/$(ARCH)/ +INC_FLAGS += -I$(src-perf)/util/include +INC_FLAGS += -I$(src-perf)/arch/$(ARCH)/include +INC_FLAGS += -I$(srctree)/tools/include/uapi +INC_FLAGS += -I$(srctree)/tools/include/ +INC_FLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/uapi +INC_FLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/ +INC_FLAGS += -I$(srctree)/tools/arch/$(ARCH)/ # $(obj-perf) for generated common-cmds.h # $(obj-perf)/util for generated bison/flex headers ifneq ($(OUTPUT),) -CFLAGS += -I$(obj-perf)/util -CFLAGS += -I$(obj-perf) +INC_FLAGS += -I$(obj-perf)/util +INC_FLAGS += -I$(obj-perf) endif -CFLAGS += -I$(src-perf)/util -CFLAGS += -I$(src-perf) -CFLAGS += -I$(srctree)/tools/lib/ +INC_FLAGS += -I$(src-perf)/util +INC_FLAGS += -I$(src-perf) +INC_FLAGS += -I$(srctree)/tools/lib/ + +CFLAGS += $(INC_FLAGS) +CXXFLAGS += $(INC_FLAGS) CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
tools/perf/tests/llvm.h +7 -0
@@ -1,6 +1,10 @@ #ifndef PERF_TEST_LLVM_H #define PERF_TEST_LLVM_H +#ifdef __cplusplus +extern "C" { +#endif + #include <stddef.h> /* for size_t */ #include <stdbool.h> /* for bool */
@@ -20,4 +24,7 @@ enum test_llvm__testcase {int test_llvm__fetch_bpf_obj (void **p_obj_buf,size_t *p_obj_buf_sz,enum test_llvm__testcase index,bool force,bool *should_load_fail); +#ifdef __cplusplus +} +#endif #endif
tools/perf/util/c++/clang-test.cpp +14 -3
@@ -3,6 +3,10 @@ #include "llvm/IR/Function.h" #include "llvm/IR/LLVMContext.h" +#include <util-cxx.h> +#include <tests/llvm.h> +#include <string> + class perf_clang_scope { public: explicit perf_clang_scope() {perf_clang__init();}
@@ -14,17 +18,24 @@ extern "C" { int test__clang_to_IR(void) { perf_clang_scope _scope; + unsigned int kernel_version; + + if (fetch_kernel_version(&kernel_version, NULL, 0)) + return -1; + + std::string cflag_kver("-DLINUX_VERSION_CODE=" + + std::to_string(kernel_version)); std::unique_ptr<llvm::Module> M = - perf::getModuleFromSource({"-DRESULT=1"}, + perf::getModuleFromSource({cflag_kver.c_str()}, "perf-test.c", - "int myfunc(void) {return RESULT;}"); + test_llvm__bpf_base_prog); if (!M) return -1; for (llvm::Function& F : *M) - if (F.getName() == "myfunc") + if (F.getName() == "bpf_func__SyS_epoll_wait") return 0; return -1; }
tools/perf/util/util-cxx.h new file +26 -0
@@ -0,0 +1,26 @@ +/* +* Support C++ source use utilities defined in util.h +*/ + +#ifndef PERF_UTIL_UTIL_CXX_H +#define PERF_UTIL_UTIL_CXX_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* +* Now 'new' is the only C++ keyword found in util.h: +* in tools/include/linux/rbtree.h +* +* Other keywords, like class and delete, should be +* redefined if necessary. +*/ +#define new _new +#include "util.h" +#undef new + +#ifdef __cplusplus +} +#endif +#endif
Signature · 1 line
2.10.1
PATCH 18/34 perf clang: Support compile IR to BPF object and add testcase
In reply to Wang NangetBPFObjectFromModule() is introduced to compile LLVM IR(Module) to BPF object. Add new testcase for it.
Test result:
$ ./buildperf/perf test -v clang 51: Test builtin clang support : 51.1: Test builtin clang compile C source to IR : --- start --- test child forked, pid 21822 test child finished with 0 ---- end ---- Test builtin clang support subtest 0: Ok 51.2: Test builtin clang compile C source to ELF object : --- start --- test child forked, pid 21823 test child finished with 0 ---- end ---- Test builtin clang support subtest 1: Ok
--- tools/perf/tests/clang.c | 6 ++++- tools/perf/util/c++/clang-c.h | 1 + tools/perf/util/c++/clang-test.cpp | 31 +++++++++++++++++++++----- tools/perf/util/c++/clang.cpp | 45 ++++++++++++++++++++++++++++++++++++++ tools/perf/util/c++/clang.h | 3 +++ 5 files changed, 79 insertions(+), 7 deletions(-)
tools/perf/tests/clang.c +5 -1
@@ -12,6 +12,10 @@ static struct { .func = test__clang_to_IR, .desc = "Test builtin clang compile C source to IR", }, + { + .func = test__clang_to_obj, + .desc = "Test builtin clang compile C source to ELF object", + },#endif };
@@ -33,7 +37,7 @@ int test__clang(int i __maybe_unused) return TEST_SKIP; }#else -int test__clang (int i__maybe_unused ) +int test__clang (int i) {if (i <0 || i >= (int )ARRAY_SIZE (clang_testcase_table))return TEST_FAIL ;
tools/perf/util/c++/clang-c.h +1 -0
@@ -9,6 +9,7 @@ extern void perf_clang__init(void);extern void perf_clang__cleanup (void );extern int test__clang_to_IR (void ); +extern int test__clang_to_obj (void ); #ifdef __cplusplus }
tools/perf/util/c++/clang-test.cpp +25 -6
@@ -13,15 +13,13 @@ public: ~perf_clang_scope() {perf_clang__cleanup();} }; -extern "C" { - -int test__clang_to_IR(void) +static std::unique_ptr<llvm::Module> +__test__clang_to_IR(void) { - perf_clang_scope _scope; unsigned int kernel_version; if (fetch_kernel_version(&kernel_version, NULL, 0)) - return -1; + return std::unique_ptr<llvm::Module>(nullptr); std::string cflag_kver("-DLINUX_VERSION_CODE=" + std::to_string(kernel_version));
@@ -30,14 +28,35 @@ int test__clang_to_IR(void) perf::getModuleFromSource({cflag_kver.c_str()}, "perf-test.c", test_llvm__bpf_base_prog); + return M; +} + +extern "C" { +int test__clang_to_IR(void) +{ + perf_clang_scope _scope; + auto M = __test__clang_to_IR(); if (!M) return -1; - for (llvm::Function& F : *M) if (F.getName() == "bpf_func__SyS_epoll_wait") return 0; return -1; } +int test__clang_to_obj(void) +{ + perf_clang_scope _scope; + + auto M = __test__clang_to_IR(); + if (!M) + return -1; + + auto Buffer = perf::getBPFObjectFromModule(&*M); + if (!Buffer) + return -1; + return 0; +} + }
tools/perf/util/c++/clang.cpp +45 -0
67 lines not on this page · raw message
tools/perf/util/c++/clang.h +3 -0
@@ -19,5 +19,8 @@ std::unique_ptr<Module> getModuleFromSource(opt::ArgStringList CFlags, StringRef Path); +std::unique_ptr<llvm::SmallVectorImpl<char>> +getBPFObjectFromModule(llvm::Module *Module); + }#endif
Signature · 1 line
2.10.1
PATCH 19/34 perf clang: Compile BPF script use builtin clang support
In reply to Wang NanAfter this patch, perf utilizes builtin clang support to build BPF script, no longer depend on external clang.
Test:
$ type clang -bash: type: clang: not found $ cat ~/.perfconfig $ echo '#define LINUX_VERSION_CODE 0x040700' > ./test.c $ cat ./tools/perf/tests/bpf-script-example.c >> ./test.c $ ./perf record -v --dry-run -e ./test.c 2>&1 | grep builtin bpf: builtin compiling successful
Can't pass cflags so unable to include kernel headers now. Will be fixed by following commits.
--- tools/perf/util/bpf-loader.c | 15 +++++++++++---- tools/perf/util/c++/clang-c.h | 26 ++++++++++++++++++++++++++ tools/perf/util/c++/clang.cpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-)
tools/perf/util/bpf-loader.c +11 -4
@@ -14,11 +14,11 @@#include "debug.h" #include "bpf-loader.h" #include "bpf-prologue.h" -#include "llvm-utils.h" #include "probe-event.h" #include "probe-finder.h" // for MAX_PROBES #include "parse-events.h" #include "llvm-utils.h" +#include "c++/clang-c.h" #define DEFINE_PRINT_FN (name, level) \ static int libbpf_##name(const char *fmt, ...) \
@@ -86,9 +86,16 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)void *obj_buf;size_t obj_buf_sz; - err =llvm__compile_bpf (filename, &obj_buf, &obj_buf_sz); -if (err) -return ERR_PTR (-BPF_LOADER_ERRNO__COMPILE ); +perf_clang__init (); + err =perf_clang__compile_bpf (filename, &obj_buf, &obj_buf_sz); +perf_clang__cleanup (); +if (err) { +pr_warning ("bpf: builtin compiling failed: %d, try external compiler\n" , err); + err =llvm__compile_bpf (filename, &obj_buf, &obj_buf_sz); +if (err) +return ERR_PTR (-BPF_LOADER_ERRNO__COMPILE ); + }else +pr_debug ("bpf: builtin compiling successful\n" ); obj =bpf_object__open_buffer (obj_buf, obj_buf_sz, filename);if (!IS_ERR (obj) && llvm_param.dump_obj)
tools/perf/util/c++/clang-c.h +26 -0
@@ -1,16 +1,42 @@#ifndef PERF_UTIL_CLANG_C_H #define PERF_UTIL_CLANG_C_H +#include <stddef.h> /* for size_t */ +#include <util-cxx.h> /* for __maybe_unused */ +#ifdef __cplusplusextern "C" {#endif +#ifdef HAVE_LIBCLANGLLVM_SUPPORT extern void perf_clang__init (void );extern void perf_clang__cleanup (void );extern int test__clang_to_IR (void );extern int test__clang_to_obj (void ); +extern int perf_clang__compile_bpf (const char *filename, +void **p_obj_buf, +size_t *p_obj_buf_sz); +#else + + +static inline void perf_clang__init (void ) { } +static inline void perf_clang__cleanup (void ) { } + +static inline int test__clang_to_IR (void ) {return -1 ; } +static inline int test__clang_to_obj (void ) {return -1 ;} + +static inline int +perf_clang__compile_bpf (const char *filename__maybe_unused , +void **p_obj_buf__maybe_unused , +size_t *p_obj_buf_sz__maybe_unused ) +{ +return -ENOTSUP ; +} + +#endif + #ifdef __cplusplus }#endif
tools/perf/util/c++/clang.cpp +29 -0
@@ -163,4 +163,33 @@ void perf_clang__cleanup(void) perf::LLVMCtx.reset(nullptr); llvm::llvm_shutdown(); } + +int perf_clang__compile_bpf(const char *filename, + void **p_obj_buf, + size_t *p_obj_buf_sz) +{ + using namespace perf; + + if (!p_obj_buf || !p_obj_buf_sz) + return -EINVAL; + + llvm::opt::ArgStringList CFlags; + auto M = getModuleFromSource(std::move(CFlags), filename); + if (!M) + return -EINVAL; + auto O = getBPFObjectFromModule(&*M); + if (!O) + return -EINVAL; + + size_t size = O->size_in_bytes(); + void *buffer; + + buffer = malloc(size); + if (!buffer) + return -ENOMEM; + memcpy(buffer, O->data(), size); + *p_obj_buf = buffer; + *p_obj_buf_sz = size; + return 0; +} }
Signature · 1 line
2.10.1
PATCH 20/34 perf clang: Pass full path to builtin clang
In reply to Wang NanIf clang changes its working directory, relative path passed to perf_clang__compile_bpf() becomes invalid. Before running clang, convert it to absolute path so file can be found even working directory is changed.
--- tools/perf/util/c++/clang.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/util/c++/clang.cpp +6 -2
@@ -13,6 +13,7 @@ #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Tooling/Tooling.h" +#include "llvm/ADT/SmallString.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/Option/Option.h"
@@ -164,7 +165,7 @@ void perf_clang__cleanup(void) llvm::llvm_shutdown(); } -int perf_clang__compile_bpf(const char *filename, +int perf_clang__compile_bpf(const char *_filename, void **p_obj_buf, size_t *p_obj_buf_sz) {
@@ -173,8 +174,11 @@ int perf_clang__compile_bpf(const char *filename, if (!p_obj_buf || !p_obj_buf_sz) return -EINVAL; + llvm::SmallString<PATH_MAX> FileName(_filename); + llvm::sys::fs::make_absolute(FileName); + llvm::opt::ArgStringList CFlags; - auto M = getModuleFromSource(std::move(CFlags), filename); + auto M = getModuleFromSource(std::move(CFlags), FileName.data()); if (!M) return -EINVAL; auto O = getBPFObjectFromModule(&*M);
Signature · 1 line
2.10.1
PATCH 21/34 perf clang: Pass CFLAGS to builtin clang
In reply to Wang NanPass -DLINUX_VERSION_CODE, -D__NR_CPUS__, llvm.clang-opt config options and CFLAGS detected by kbuild detector to builtin clang so BPF scripts can use kernel headers and user defined options like external clang compiler.
Test:
# perf record -v --dry-run -e tools/perf/tests/bpf-script-test-kbuild.c ls 2>&1 | grep built bpf: builtin compiling successful
Committer notes:
Before installing the required clang/llvm devel files to have it builtin:
# perf record -v --dry-run -e tools/perf/tests/bpf-script-test-kbuild.c ls 2>&1 | grep built bpf: builtin compiling failed: -95, try external compiler
I.e. it falls back to using the external compiler.
--- tools/perf/util/c++/clang.cpp | 105 ++++++++++++++++++++++++++++++++++++++++-- tools/perf/util/llvm-utils.h | 9 +++- 2 files changed, 109 insertions(+), 5 deletions(-)
tools/perf/util/c++/clang.cpp +101 -4
@@ -27,6 +27,8 @@ #include "clang.h" #include "clang-c.h" +#include "llvm-utils.h" +#include "util-cxx.h" namespace perf {
@@ -147,6 +149,101 @@ getBPFObjectFromModule(llvm::Module *Module) return std::move(Buffer); } +class ClangOptions { + llvm::SmallString<PATH_MAX> FileName; + llvm::SmallString<64> KVerDef; + llvm::SmallString<64> NRCpusDef; + char *kbuild_dir; + char *kbuild_include_opts; + char *clang_opt; +public: + ClangOptions(const char *filename) : FileName(filename), + KVerDef(""), + NRCpusDef(""), + kbuild_dir(NULL), + kbuild_include_opts(NULL), + clang_opt(NULL) + { + llvm::sys::fs::make_absolute(FileName); + + unsigned int kver; + if (!fetch_kernel_version(&kver, NULL, 0)) + KVerDef = "-DLINUX_VERSION_CODE=" + std::to_string(kver); + + int nr_cpus = llvm__get_nr_cpus(); + if (nr_cpus > 0) + NRCpusDef = "-D__NR_CPUS__=" + std::to_string(nr_cpus); + + if (llvm_param.clang_opt) + clang_opt = strdup(llvm_param.clang_opt); + + llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts); + if (!kbuild_dir || !kbuild_include_opts) { + free(kbuild_dir); + free(kbuild_include_opts); + kbuild_dir = kbuild_include_opts = NULL; + } + } + + ~ClangOptions() + { + free(kbuild_dir); + free(kbuild_include_opts); + free(clang_opt); + } + + static void fillCFlagsFromString(opt::ArgStringList &CFlags, char *s, bool check = false) + { + if (!s) + return; + + SmallVector<StringRef, 0> Terms; + StringRef Opts(s); + Opts.split(Terms, ' '); + + for (auto i = Terms.begin(); i != Terms.end(); i++) + s[i->end() - Opts.begin()] = '\0'; + + for (auto i = Terms.begin(); i != Terms.end(); i++) { + if (!check) { + CFlags.push_back(i->begin()); + continue; + } + + if (i->startswith("-I")) + CFlags.push_back(i->begin()); + else if (i->startswith("-D")) + CFlags.push_back(i->begin()); + else if (*i == "-include") { + CFlags.push_back((i++)->begin()); + /* Let clang report this error */ + if (i == Terms.end()) + break; + CFlags.push_back(i->begin()); + } + } + } + + void getCFlags(opt::ArgStringList &CFlags) + { + CFlags.push_back(KVerDef.c_str()); + CFlags.push_back(NRCpusDef.c_str()); + + fillCFlagsFromString(CFlags, clang_opt); + fillCFlagsFromString(CFlags, kbuild_include_opts, true); + + if (kbuild_dir) { + CFlags.push_back("-working-directory"); + CFlags.push_back(kbuild_dir); + } + } + + const char *getFileName(void) + { + return FileName.c_str(); + } +}; + } extern "C" {
@@ -174,11 +271,11 @@ int perf_clang__compile_bpf(const char *_filename, if (!p_obj_buf || !p_obj_buf_sz) return -EINVAL; - llvm::SmallString<PATH_MAX> FileName(_filename); - llvm::sys::fs::make_absolute(FileName); - + ClangOptions Opts(_filename); llvm::opt::ArgStringList CFlags; - auto M = getModuleFromSource(std::move(CFlags), FileName.data()); + + Opts.getCFlags(CFlags); + auto M = getModuleFromSource(std::move(CFlags), Opts.getFileName()); if (!M) return -EINVAL; auto O = getBPFObjectFromModule(&*M);
tools/perf/util/llvm-utils.h +8 -1
@@ -5,7 +5,11 @@ #ifndef __LLVM_UTILS_H #define __LLVM_UTILS_H -#include "debug.h" +#ifdef __cplusplus +extern "C" { +#endif + +#include <errno.h> struct llvm_param { /* Path of clang executable */
@@ -56,4 +60,7 @@ void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts);int llvm__get_nr_cpus (void );void llvm__dump_obj (const char *path,void *obj_buf,size_t size); +#ifdef __cplusplus +} +#endif #endif
Signature · 1 line
2.10.1
PATCH 22/34 perf clang jit: Wrap llvm::Module using PerfModule
In reply to Wang NanUse PerfModule wrap llvm::Module and return perf::PerfModule in APIs to replace llvm::Module. Following commits are going to add new functions to PerfModule.
getBPFObjectFromModule is merged to a method of perf::PerfModule.
--- tools/perf/util/c++/clang-test.cpp | 10 +++++----- tools/perf/util/c++/clang.cpp | 20 +++++++++++++------- tools/perf/util/c++/clang.h | 22 ++++++++++++++++------ 3 files changed, 34 insertions(+), 18 deletions(-)
tools/perf/util/c++/clang-test.cpp +5 -5
@@ -13,18 +13,18 @@ public: ~perf_clang_scope() {perf_clang__cleanup();} }; -static std::unique_ptr<llvm::Module> +static std::unique_ptr<perf::PerfModule> __test__clang_to_IR(void) { unsigned int kernel_version; if (fetch_kernel_version(&kernel_version, NULL, 0)) - return std::unique_ptr<llvm::Module>(nullptr); + return std::unique_ptr<perf::PerfModule>(nullptr); std::string cflag_kver("-DLINUX_VERSION_CODE=" + std::to_string(kernel_version)); - std::unique_ptr<llvm::Module> M = + std::unique_ptr<perf::PerfModule> M = perf::getModuleFromSource({cflag_kver.c_str()}, "perf-test.c", test_llvm__bpf_base_prog);
@@ -39,7 +39,7 @@ int test__clang_to_IR(void) auto M = __test__clang_to_IR(); if (!M) return -1; - for (llvm::Function& F : *M) + for (llvm::Function& F : *(M->getModule())) if (F.getName() == "bpf_func__SyS_epoll_wait") return 0; return -1;
@@ -53,7 +53,7 @@ int test__clang_to_obj(void) if (!M) return -1; - auto Buffer = perf::getBPFObjectFromModule(&*M); + auto Buffer = M->toBPFObject(); if (!Buffer) return -1; return 0;
tools/perf/util/c++/clang.cpp +13 -7
@@ -64,7 +64,7 @@ createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path, return CI; } -static std::unique_ptr<llvm::Module> +static std::unique_ptr<PerfModule> getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
@@ -80,12 +80,12 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx)); if (!Clang.ExecuteAction(*Act)) - return std::unique_ptr<llvm::Module>(nullptr); + return std::unique_ptr<PerfModule>(nullptr); - return Act->takeModule(); + return std::unique_ptr<PerfModule>(new PerfModule(std::move(Act->takeModule()))); } -std::unique_ptr<llvm::Module> +std::unique_ptr<PerfModule> getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Name, StringRef Content) {
@@ -106,15 +106,21 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, return getModuleFromSource(std::move(CFlags), Name, OverlayFS); } -std::unique_ptr<llvm::Module> +std::unique_ptr<PerfModule> getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path) { IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem()); return getModuleFromSource(std::move(CFlags), Path, VFS); } +PerfModule::PerfModule(std::unique_ptr<llvm::Module>&& M) : Module(std::move(M)) +{ + +} + + std::unique_ptr<llvm::SmallVectorImpl<char>> -getBPFObjectFromModule(llvm::Module *Module) +PerfModule::toBPFObject(void) { using namespace llvm;
@@ -278,7 +284,7 @@ int perf_clang__compile_bpf(const char *_filename, auto M = getModuleFromSource(std::move(CFlags), Opts.getFileName()); if (!M) return -EINVAL; - auto O = getBPFObjectFromModule(&*M); + auto O = M->toBPFObject(); if (!O) return -EINVAL;
tools/perf/util/c++/clang.h +16 -6
@@ -11,16 +11,26 @@ namespace perf { using namespace llvm; -std::unique_ptr<Module> +class PerfModule { +private: + std::unique_ptr<llvm::Module> Module; +public: + inline llvm::Module *getModule(void) + { +return Module.get (); + } + + PerfModule(std::unique_ptr<llvm::Module>&& M); + + std::unique_ptr<llvm::SmallVectorImpl<char>> toBPFObject(void); +}; + +std::unique_ptr<PerfModule> getModuleFromSource(opt::ArgStringList CFlags, StringRef Name, StringRef Content); -std::unique_ptr<Module> +std::unique_ptr<PerfModule> getModuleFromSource(opt::ArgStringList CFlags, StringRef Path); - -std::unique_ptr<llvm::SmallVectorImpl<char>> -getBPFObjectFromModule(llvm::Module *Module); - }#endif
Signature · 1 line
2.10.1
PATCH 23/34 perf clang jit: Insignt BPF and JIT functions in a Module
In reply to Wang NanIdentify BPF functions, JIT functions and maps during init. Functions in section starting with "perfhook:" are JIT functions. They will be JIT compiled and hooked at perfhooks.
During init of PerfModule, mark JIT functions as AvailableExternallyLinkage. LLVM skips functions with linkage like this so they won't be compiled into BPF objects.
--- tools/perf/util/c++/clang.cpp | 32 ++++++++++++++++++++++++++++++++ tools/perf/util/c++/clang.h | 7 +++++++ 2 files changed, 39 insertions(+)
tools/perf/util/c++/clang.cpp +32 -0
@@ -115,15 +115,47 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path) PerfModule::PerfModule(std::unique_ptr<llvm::Module>&& M) : Module(std::move(M)) { + for (llvm::Function& F : *Module) { + if (F.getLinkage() != llvm::GlobalValue::ExternalLinkage) + continue; + + if (StringRef(F.getSection()).startswith("perfhook:")) + JITFunctions.insert(&F); + else + BPFFunctions.insert(&F); + } + for (auto V = Module->global_begin(); V != Module->global_end(); V++) { + llvm::GlobalVariable *GV = &*V; + if (StringRef(GV->getSection()) == llvm::StringRef("maps")) + Maps.insert(GV); + } } +void PerfModule::prepareBPF(void) +{ + for (llvm::Function *F : JITFunctions) + F->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); + for (llvm::Function *F : BPFFunctions) + F->setLinkage(llvm::GlobalValue::ExternalLinkage); + +} + +void PerfModule::prepareJIT(void) +{ + for (llvm::Function *F : BPFFunctions) + F->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); + for (llvm::Function *F : JITFunctions) + F->setLinkage(llvm::GlobalValue::ExternalLinkage); + +} std::unique_ptr<llvm::SmallVectorImpl<char>> PerfModule::toBPFObject(void) { using namespace llvm; + prepareBPF(); std::string TargetTriple("bpf-pc-linux"); std::string Error; const Target* Target = TargetRegistry::lookupTarget(TargetTriple, Error);
tools/perf/util/c++/clang.h +7 -0
@@ -6,6 +6,7 @@#include "llvm/IR/Module.h" #include "llvm/Option/Option.h" #include <memory> +#include <set> namespace perf {
@@ -14,6 +15,12 @@ using namespace llvm;class PerfModule { private: std::unique_ptr<llvm::Module> Module; + + std::set<llvm::GlobalVariable *> Maps; + std::set<llvm::Function *> BPFFunctions; + std::set<llvm::Function *> JITFunctions; +void prepareBPF (void ); +void prepareJIT (void ); public: inline llvm::Module *getModule(void) {
Signature · 1 line
2.10.1
PATCH 24/34 perf clang jit: add PerfModule::doJIT to JIT perfhook functions
In reply to Wang NanPerfModule::doJIT JIT compile perfhook functions and saves result into a map. Add a test case for it.
At this stage perfhook functions can do no useful things because they can't invoke external functions and can't return value. Following commits are going to make improvment.
Don't hook functions right after jitted because bpf_object is unavailable during jitting but it should be the context of jitted functions.
--- tools/perf/tests/bpf-script-example.c | 8 ++++ tools/perf/tests/clang.c | 4 ++ tools/perf/util/c++/clang-c.h | 2 + tools/perf/util/c++/clang-test.cpp | 32 +++++++++++++++- tools/perf/util/c++/clang.cpp | 71 +++++++++++++++++++++++++++++++++++ tools/perf/util/c++/clang.h | 13 +++++++ 6 files changed, 128 insertions(+), 2 deletions(-)
tools/perf/tests/bpf-script-example.c +8 -0
@@ -46,3 +46,11 @@ int bpf_func__SyS_epoll_wait(void *ctx) } char _license[] SEC("license") = "GPL"; int _version SEC("version") = LINUX_VERSION_CODE; + +#ifdef TEST_PERF_HOOK +SEC("perfhook:test") +void hook_test (void ) +{ +return ; +} +#endif
tools/perf/tests/clang.c +4 -0
@@ -16,6 +16,10 @@ static struct { .func = test__clang_to_obj, .desc = "Test builtin clang compile C source to ELF object", }, + { + .func = test__clang_jit, + .desc = "Test builtin clang compile mixed BPF and native code", + },#endif };
tools/perf/util/c++/clang-c.h +2 -0
@@ -14,6 +14,7 @@ extern void perf_clang__cleanup(void);extern int test__clang_to_IR (void );extern int test__clang_to_obj (void ); +extern int test__clang_jit (void ); extern int perf_clang__compile_bpf(const char *filename, void **p_obj_buf,
@@ -26,6 +27,7 @@ static inline void perf_clang__cleanup(void) { }static inline int test__clang_to_IR (void ) {return -1 ; }static inline int test__clang_to_obj (void ) {return -1 ;} +static inline int test__clang_jit (void ) {return -1 ;} static inline int perf_clang__compile_bpf(const char *filename __maybe_unused,
tools/perf/util/c++/clang-test.cpp +30 -2
@@ -5,6 +5,7 @@ #include <util-cxx.h> #include <tests/llvm.h> +#include <perf-hooks.h> #include <string> class perf_clang_scope {
@@ -14,7 +15,7 @@ public: }; static std::unique_ptr<perf::PerfModule> -__test__clang_to_IR(void) +__test__clang_to_IR(bool perfhook) { unsigned int kernel_version;
@@ -23,14 +24,22 @@ __test__clang_to_IR(void) std::string cflag_kver("-DLINUX_VERSION_CODE=" + std::to_string(kernel_version)); + std::string cflag_perfhook(perfhook ? "-DTEST_PERF_HOOK=1" : ""); std::unique_ptr<perf::PerfModule> M = - perf::getModuleFromSource({cflag_kver.c_str()}, + perf::getModuleFromSource({cflag_kver.c_str(), + cflag_perfhook.c_str()}, "perf-test.c", test_llvm__bpf_base_prog); return M; } +static std::unique_ptr<perf::PerfModule> +__test__clang_to_IR(void) +{ + return __test__clang_to_IR(false); +} + extern "C" { int test__clang_to_IR(void) {
@@ -59,4 +68,23 @@ int test__clang_to_obj(void) return 0; } +int test__clang_jit(void) +{ + perf_clang_scope _scope; + + auto M = __test__clang_to_IR(true); + if (!M) + return -1; + + if (M->doJIT()) + return -1; + + std::unique_ptr<perf::PerfModule::HookMap> hooks(M->copyJITResult()); + for (auto i : *hooks) + perf_hooks__set_hook(i.first.c_str(), i.second, NULL); + + perf_hooks__invoke_test(); + return 0; +} + }
tools/perf/util/c++/clang.cpp +71 -0
103 lines not on this page · raw message
tools/perf/util/c++/clang.h +13 -0
@@ -7,18 +7,26 @@#include "llvm/Option/Option.h" #include <memory> #include <set> +#include <map> + +#include "util/perf-hooks.h" namespace perf { using namespace llvm;class PerfModule { +public: + typedef std::map<std::string, perf_hook_func_t> HookMap; private: std::unique_ptr<llvm::Module> Module; std::set<llvm::GlobalVariable *> Maps; std::set<llvm::Function *> BPFFunctions; std::set<llvm::Function *> JITFunctions; + +HookMap JITResult; +void prepareBPF (void );void prepareJIT (void ); public:
@@ -26,10 +34,15 @@ class PerfModule { {return Module.get (); } +inline HookMap *copyJITResult (void ) + { + return new HookMap(JITResult); + } PerfModule(std::unique_ptr<llvm::Module>&& M); std::unique_ptr<llvm::SmallVectorImpl<char>> toBPFObject(void); +int doJIT (void ); }; std::unique_ptr<PerfModule>
Signature · 1 line
2.10.1
PATCH 25/34 perf clang jit: Export functions for jitted code
In reply to Wang NanAfter this patch functions attached on perf hooks is allowed to invoke external functions. Add a testcase for this feature.
--- tools/perf/tests/Build | 2 +- tools/perf/tests/bpf-script-example.c | 4 ++++ tools/perf/util/c++/clang-c.h | 2 ++ tools/perf/util/c++/clang-test.cpp | 9 +++++++++ tools/perf/util/c++/clang.cpp | 17 ++++++++++++++++- 5 files changed, 32 insertions(+), 2 deletions(-)
tools/perf/tests/Build +1 -1
@@ -49,7 +49,7 @@ $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir) $(Q)echo '#include <tests/llvm.h>' > $@ $(Q)echo 'const char test_llvm__bpf_base_prog[] =' >> $@ - $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ + $(Q)sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ $(Q)echo ';' >> $@ $(OUTPUT)tests/llvm-src-kbuild.c: tests/bpf-script-test-kbuild.c tests/Build
tools/perf/tests/bpf-script-example.c +4 -0
@@ -48,9 +48,13 @@ char _license[] SEC("license") = "GPL"; int _version SEC("version") = LINUX_VERSION_CODE;#ifdef TEST_PERF_HOOK +extern int printf (const char *fmt, ...); +extern void test__clang_callback (int x); SEC("perfhook:test")void hook_test (void ) { +printf ("Hello, hook_test\n" ); +test__clang_callback (1234 );return ; }#endif
tools/perf/util/c++/clang-c.h +2 -0
@@ -16,6 +16,8 @@ extern int test__clang_to_IR(void);extern int test__clang_to_obj (void );extern int test__clang_jit (void ); +extern void test__clang_callback (int x); +extern int perf_clang__compile_bpf (const char *filename,void **p_obj_buf,size_t *p_obj_buf_sz);
tools/perf/util/c++/clang-test.cpp +9 -0
@@ -68,6 +68,13 @@ int test__clang_to_obj(void) return 0; } +static int callback_flag; + +void test__clang_callback(int x) +{ + callback_flag = x; +} + int test__clang_jit(void) { perf_clang_scope _scope;
@@ -84,6 +91,8 @@ int test__clang_jit(void) perf_hooks__set_hook(i.first.c_str(), i.second, NULL); perf_hooks__invoke_test(); + if (callback_flag != 1234) + return -1; return 0; }
tools/perf/util/c++/clang.cpp +16 -1
@@ -30,6 +30,8 @@ #include "llvm/Target/TargetOptions.h" #include <memory> #include <vector> +#include <set> +#include <tuple> #include "clang.h" #include "clang-c.h"
@@ -194,6 +196,15 @@ PerfModule::toBPFObject(void) return std::move(Buffer); } +static std::map<const std::string, const void *> exported_funcs = +{ +#define EXPORT(f) {#f, (const void *)&f} + EXPORT(test__clang_callback), + EXPORT(printf), + EXPORT(puts), +#undef EXPORT +}; + /* * Use a global memory manager so allocated code and data won't be released * when object destroy.
@@ -220,7 +231,11 @@ int PerfModule::doJIT(void) auto Resolver = createLambdaResolver( [](const std::string &Name) { - return RuntimeDyld::SymbolInfo(nullptr); + auto i = exported_funcs.find(Name); + if (i == exported_funcs.end()) + return RuntimeDyld::SymbolInfo(nullptr); + return RuntimeDyld::SymbolInfo((uint64_t)(i->second), + JITSymbolFlags::Exported); }, [](const std::string &Name) { return RuntimeDyld::SymbolInfo(nullptr);
Signature · 1 line
2.10.1
PATCH 26/34 perf clang jit: Actually JIT and hook in bpf loader
In reply to Wang NanMakes perf_clang__compile_bpf() actually uses clang jit to compile perf hooks. Returns a map through perf_clang__compile_bpf(), and set hooks after bpf_object is created.
After this path jitting takes actions for bpf loader. For example:
$ cat ./test.c
/******************************************************/
#define SEC(name) __attribute__((section(name), used))
SEC("dofork=_do_fork")
int dofork(void *ctx)
{
return 0;
}
extern int printf(const char *fmt, ...);
SEC("perfhook:record_start")
void record_start(void)
{
printf("Welcom to perf record\n");
}
SEC("perfhook:record_end")
void record_end(void)
{
printf("Goodbye, perf record\n");
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
/******************************************************/
$ perf record -e ./test.c sleep 1
Welcom to perf record
[ perf record: Woken up 1 times to write data ]
Goodbye, perf record
[ perf record: Captured and wrote 0.014 MB perf.data ]--- tools/perf/util/bpf-loader.c | 11 ++++++++++- tools/perf/util/c++/clang-c.h | 18 ++++++++++++++++-- tools/perf/util/c++/clang.cpp | 28 +++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 4 deletions(-)
tools/perf/util/bpf-loader.c +10 -1
@@ -85,9 +85,11 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)int err;void *obj_buf;size_t obj_buf_sz; +jitted_funcs_map_t jitted_funcs_map;perf_clang__init (); - err =perf_clang__compile_bpf (filename, &obj_buf, &obj_buf_sz); + err =perf_clang__compile_bpf (filename, &obj_buf, + &obj_buf_sz, &jitted_funcs_map);perf_clang__cleanup ();if (err) {pr_warning ("bpf: builtin compiling failed: %d, try external compiler\n" , err);
@@ -101,6 +103,13 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)if (!IS_ERR (obj) && llvm_param.dump_obj)llvm__dump_obj (filename, obj_buf, obj_buf_sz); +/* +* Call perf_clang__hook_jitted_func even IS_ERR(obj) to make sure +* the C++ map pointer is deleted. +*/ +if (jitted_funcs_map) +perf_clang__hook_jitted_func (jitted_funcs_map, obj,IS_ERR (obj)); +free (obj_buf); }else obj =bpf_object__open (filename);
tools/perf/util/c++/clang-c.h +16 -2
@@ -8,6 +8,7 @@extern "C" {#endif +typedef void *jitted_funcs_map_t ;#ifdef HAVE_LIBCLANGLLVM_SUPPORT extern void perf_clang__init (void );extern void perf_clang__cleanup (void );
@@ -20,7 +21,11 @@ extern void test__clang_callback(int x);extern int perf_clang__compile_bpf (const char *filename,void **p_obj_buf, -size_t *p_obj_buf_sz); +size_t *p_obj_buf_sz, +jitted_funcs_map_t *p_funcs_map); + +extern int +perf_clang__hook_jitted_func (jitted_funcs_map_t map,void *ctx,bool is_err);#else
@@ -34,7 +39,16 @@ static inline int test__clang_jit(void) { return -1;}static inline int perf_clang__compile_bpf (const char *filename__maybe_unused ,void **p_obj_buf__maybe_unused , -size_t *p_obj_buf_sz__maybe_unused ) +size_t *p_obj_buf_sz__maybe_unused , +jitted_funcs_map_t *p_funcs_map__maybe_unused ) +{ +return -ENOTSUP ; +} + +static inline int +perf_clang__hook_jitted_func (jitted_funcs_map_t map__maybe_unused , +void *ctx__maybe_unused , +bool is_err__maybe_unused ) {return -ENOTSUP ; }
tools/perf/util/c++/clang.cpp +27 -1
@@ -388,7 +388,8 @@ void perf_clang__cleanup(void) int perf_clang__compile_bpf(const char *_filename, void **p_obj_buf, - size_t *p_obj_buf_sz) + size_t *p_obj_buf_sz, + jitted_funcs_map_t *p_funcs_map) { using namespace perf;
@@ -415,6 +416,31 @@ int perf_clang__compile_bpf(const char *_filename, memcpy(buffer, O->data(), size); *p_obj_buf = buffer; *p_obj_buf_sz = size; + + if (M->doJIT()) + return -1; + + if (p_funcs_map) + *p_funcs_map = (jitted_funcs_map_t)(M->copyJITResult()); + return 0; +} + +int perf_clang__hook_jitted_func(jitted_funcs_map_t map, void *ctx, bool is_err) +{ + std::unique_ptr<perf::PerfModule::HookMap> + hook_map((perf::PerfModule::HookMap *)map); + + /* Do nothing but ensure map is deleted */ + if (is_err) + return -1; + + for (auto i : *hook_map) { + const char *hook_name = i.first.c_str(); + perf_hook_func_t hook_func = i.second; + + if (perf_hooks__set_hook(hook_name, hook_func, ctx)) + return -1; + } return 0; } }
Signature · 1 line
2.10.1
PATCH 27/34 perf clang jit: Collect the lowest address in maps section as map_base
In reply to Wang NanDuring jitting, find the lowest address in maps section and store its value to _map_base. Pass its value out through perf_clang__compile_bpf(). map_base is useful for jitted functions accessing BPF maps.
--- tools/perf/util/bpf-loader.c | 39 +++++++++++++++++++++++++++++++++++++-- tools/perf/util/c++/clang-c.h | 6 ++++-- tools/perf/util/c++/clang.cpp | 15 +++++++++++++-- tools/perf/util/c++/clang.h | 5 +++++ 4 files changed, 59 insertions(+), 6 deletions(-)
tools/perf/util/bpf-loader.c +37 -2
@@ -47,6 +47,10 @@ struct bpf_prog_priv {int *type_mapping; }; +struct bpf_obj_priv { +void *map_base; +}; +static bool libbpf_initialized; struct bpf_object *
@@ -70,9 +74,20 @@ bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)return obj; } +static void +clear_obj_priv (struct bpf_object *obj__maybe_unused , +void *_priv) +{ +struct bpf_obj_priv *priv = _priv; + +free (priv); +} +struct bpf_object *bpf__prepare_load (const char *filename,bool source) {struct bpf_object *obj; +void *map_base =NULL ; +int err;if (!libbpf_initialized) { libbpf_set_print(libbpf_warning,
@@ -82,14 +97,14 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source) }if (source) { -int err;void *obj_buf;size_t obj_buf_sz;jitted_funcs_map_t jitted_funcs_map;perf_clang__init (); err =perf_clang__compile_bpf (filename, &obj_buf, - &obj_buf_sz, &jitted_funcs_map); + &obj_buf_sz, &jitted_funcs_map, + &map_base);perf_clang__cleanup ();if (err) {pr_warning ("bpf: builtin compiling failed: %d, try external compiler\n" , err);
@@ -119,7 +134,27 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)return obj; } +if (map_base) { +struct bpf_obj_priv *priv =calloc (sizeof (*priv),1 ); + +if (!priv) { +pr_debug ("bpf: failed to alloc priv for object\n" ); + err = -ENOMEM ; + goto errout; + } + priv->map_base = map_base; + + err =bpf_object__set_priv (obj, priv, clear_obj_priv); +if (err) { +pr_debug ("Failed to set priv for object '%s'\n" , filename); + goto errout; + } + } +return obj; +errout: +bpf_object__close (obj); +return ERR_PTR (err); }void bpf__clear (void )
tools/perf/util/c++/clang-c.h +4 -2
@@ -22,7 +22,8 @@ extern void test__clang_callback(int x);extern int perf_clang__compile_bpf (const char *filename,void **p_obj_buf,size_t *p_obj_buf_sz, -jitted_funcs_map_t *p_funcs_map); +jitted_funcs_map_t *p_funcs_map, +void **p_map_base);extern int perf_clang__hook_jitted_func (jitted_funcs_map_t map,void *ctx,bool is_err);
@@ -40,7 +41,8 @@ static inline int perf_clang__compile_bpf(const char *filename __maybe_unused, void **p_obj_buf __maybe_unused, size_t *p_obj_buf_sz __maybe_unused, - jitted_funcs_map_t *p_funcs_map __maybe_unused) + jitted_funcs_map_t *p_funcs_map __maybe_unused, + void **p_map_base __maybe_unused) { return -ENOTSUP; }
tools/perf/util/c++/clang.cpp +13 -2
@@ -122,7 +122,7 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path) return getModuleFromSource(std::move(CFlags), Path, VFS); } -PerfModule::PerfModule(std::unique_ptr<llvm::Module>&& M) : Module(std::move(M)) +PerfModule::PerfModule(std::unique_ptr<llvm::Module>&& M) : Module(std::move(M)), _map_base(NULL) { for (llvm::Function& F : *Module) { if (F.getLinkage() != llvm::GlobalValue::ExternalLinkage)
@@ -247,6 +247,13 @@ int PerfModule::doJIT(void) &JITMemoryManager, std::move(Resolver)); + void *map_base = NULL; + for (llvm::GlobalValue *map : Maps) { + JITSymbol sym = CompileLayer.findSymbol(map->getName().str(), true); + void *address = (void *)(intptr_t)sym.getAddress(); + if (!map_base || address < map_base) + map_base = address; + } for (Function *F : JITFunctions) { JITSymbol sym = CompileLayer.findSymbol(F->getName().str(), true);
@@ -266,6 +273,7 @@ int PerfModule::doJIT(void) << hook << ", only one is used\n"; JITResult[hook] = func; } + _map_base = map_base; return 0; }
@@ -389,7 +397,8 @@ void perf_clang__cleanup(void) int perf_clang__compile_bpf(const char *_filename, void **p_obj_buf, size_t *p_obj_buf_sz, - jitted_funcs_map_t *p_funcs_map) + jitted_funcs_map_t *p_funcs_map, + void **p_map_base) { using namespace perf;
@@ -422,6 +431,8 @@ int perf_clang__compile_bpf(const char *_filename, if (p_funcs_map) *p_funcs_map = (jitted_funcs_map_t)(M->copyJITResult()); + if (p_map_base) + *p_map_base = M->getMapBase(); return 0; }
tools/perf/util/c++/clang.h +5 -0
@@ -26,6 +26,7 @@ class PerfModule { std::set<llvm::Function *> JITFunctions;HookMap JITResult; +void *_map_base;void prepareBPF (void );void prepareJIT (void );
@@ -38,6 +39,10 @@ class PerfModule { { return new HookMap(JITResult); } +inline void *getMapBase (void ) + { +return _map_base; + } PerfModule(std::unique_ptr<llvm::Module>&& M);
Signature · 1 line
2.10.1
PATCH 28/34 perf clang jit: Access BPF map
In reply to Wang Nan--- tools/perf/util/bpf-loader.c | 37 +++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 19 +++++++++++++++++++ 2 files changed, 56 insertions(+)
tools/perf/util/bpf-loader.c +37 -0
@@ -19,6 +19,7 @@#include "parse-events.h" #include "llvm-utils.h" #include "c++/clang-c.h" +#include "asm/bug.h" // for WARN_ONCE #define DEFINE_PRINT_FN (name, level) \ static int libbpf_##name(const char *fmt, ...) \
@@ -1644,6 +1645,28 @@ int bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused)return 0 ; } +int bpf__map_fd (struct bpf_object *obj,void *jit_map) +{ +struct bpf_obj_priv *priv =bpf_object__priv (obj); +struct bpf_map *map; +size_t map_offset; +void *map_base; + +if (IS_ERR (priv)) +return PTR_ERR (priv); +if (!priv) +return -EINVAL ; + + map_base = priv->map_base; + map_offset = jit_map - map_base; + map =bpf_object__find_map_by_offset (obj, map_offset); +WARN_ONCE (IS_ERR (map),"can't find map offset %zu from '%s'\n" , + map_offset,bpf_object__name (obj)); +if (IS_ERR (map)) +return -ENOENT ; +return bpf_map__fd (map); +} +#define ERRNO_OFFSET (e) ((e) - __BPF_LOADER_ERRNO__START)#define ERRCODE_OFFSET (c) ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)#define NR_ERRNO (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
@@ -1825,3 +1848,17 @@ int bpf__strerror_setup_stdout(struct perf_evlist *evlist __maybe_unused,bpf__strerror_end (buf, size);return 0 ; } + +int bpf__strerror_map_fd (struct bpf_object *obj,void *jit_map, +int err,char *buf,size_t size) +{ +struct bpf_obj_priv *priv =bpf_object__priv (obj); +ptrdiff_t offset = priv ? jit_map - priv->map_base : jit_map -NULL ; + +bpf__strerror_head (err, buf, size); +bpf__strerror_entry (EINVAL ,"No map in BPF object %s" ,bpf_object__name (obj)); +bpf__strerror_entry (ENOENT ,"Can't find map offset %lx in BPF object %s" , + (unsigned long )offset,bpf_object__name (obj)); +bpf__strerror_end (buf, size); +return 0 ; +}
tools/perf/util/bpf-loader.h +19 -0
@@ -84,6 +84,9 @@ int bpf__setup_stdout(struct perf_evlist *evlist);int bpf__strerror_setup_stdout (struct perf_evlist *evlist,int err,char *buf,size_t size); +int bpf__map_fd (struct bpf_object *obj,void *jit_map); +int bpf__strerror_map_fd (struct bpf_object *obj,void *jit_map, +int err,char *buf,size_t size);#else static inline struct bpf_object * bpf__prepare_load(const char *filename __maybe_unused,
@@ -136,6 +139,13 @@ bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused) }static inline int +bpf__map_fd (struct bpf_object *obj__maybe_unused , +void *map_ptr__maybe_unused ) +{ +return -ENOTSUP ; +} + +static inline int __bpf_strerror (char *buf,size_t size) {if (!size)
@@ -196,5 +206,14 @@ bpf__strerror_setup_stdout(struct perf_evlist *evlist __maybe_unused, {return __bpf_strerror (buf, size); } + +static inline int +bpf__strerror_map_fd (struct bpf_object *obj__maybe_unused , +void *jit_map__maybe_unused , +int err__maybe_unused , +char *buf,size_t size); +{ +return __bpf_strerror (buf, size); +}#endif #endif
Signature · 1 line
2.10.1
PATCH 29/34 perf clang jit: Allow jitted perf hook access BPF maps
In reply to Wang NanNewly introduced jit-helpers.[ch] defines a series of helpers which helps jitted perf hook functions accessing BPF maps defined in their BPF scripts. The helpers fetches fd of 'struct bpf_map' from 'struct bpf_object' and the address of 'struct bpf_map_def' in jitted file. 'struct bpf_object' is the context passed to hooks.
Example:
$ cat ./test.c
/*******************************************************/
#define SEC(name) __attribute__((section(name), used))
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_MAP_TYPE_PERF_EVENT_ARRAY 4
#define BPF_FUNC_map_lookup_elem 1
static void *(*bpf_map_lookup_elem)(void *map, void *key) =
(void *) BPF_FUNC_map_lookup_elem;
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;
};
struct bpf_map_def SEC("maps") counter = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 1,
};
extern int jit_helper__map_update_elem(void *ctx, struct bpf_map_def *map,
void *key, void *value, unsigned long flags);
extern int jit_helper__map_lookup_elem(void *ctx, struct bpf_map_def *map,
void *key, void *value);
SEC("sys_close=SyS_close")
int sys_close(void *ctx)
{
int key = 0;
int *value;
value = bpf_map_lookup_elem(&counter, &key);
if (!value)
return 0;
__sync_fetch_and_add(value, 1);
return 0;
}
extern int printf(const char *fmt, ...);
SEC("perfhook:record_start")
void record_start(void *ctx)
{
int key = 0;
int value = 100000000;
printf("Welcom to perf record\n");
jit_helper__map_update_elem(ctx, &counter, &key, &value, 0);
} SEC("perfhook:record_end")
void record_end(void *ctx)
{
int key = 0;
int value;
jit_helper__map_lookup_elem(ctx, &counter, &key, &value);
printf("Goodbye, perf record, value=%d\n", value);
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
/*******************************************************/
$ sudo perf record -e ./test.c echo Hehe
Welcom to perf record
Hehe
[ perf record: Woken up 1 times to write data ]
Goodbye, perf record, value=100000644
[ perf record: Captured and wrote 0.014 MB perf.data ]--- tools/perf/util/Build | 1 + tools/perf/util/c++/clang.cpp | 10 +++++++- tools/perf/util/jit-helpers.c | 60 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/jit-helpers.h | 30 ++++++++++++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tools/perf/util/jit-helpers.c create mode 100644 tools/perf/util/jit-helpers.h
tools/perf/util/Build +1 -0
@@ -124,6 +124,7 @@ libperf-$(CONFIG_DWARF) += genelf_debug.o endif libperf-y += perf-hooks.o +libperf-y += jit-helpers.o libperf-$(CONFIG_CXX) += c++/
tools/perf/util/c++/clang.cpp +9 -1
@@ -38,6 +38,7 @@ #include "llvm-utils.h" #include "util-cxx.h" #include "perf-hooks.h" +#include "jit-helpers.h" namespace perf {
@@ -196,12 +197,19 @@ PerfModule::toBPFObject(void) return std::move(Buffer); } +#define __stringify_1(x) #x +#define __stringify(x) __stringify_1(x) static std::map<const std::string, const void *> exported_funcs = { -#define EXPORT(f) {#f, (const void *)&f} +#define EXPORT(f) {__stringify(f), (const void *)&f} EXPORT(test__clang_callback), EXPORT(printf), EXPORT(puts), + EXPORT(JIT_HELPER_FUNC_NAME(map_update_elem)), + EXPORT(JIT_HELPER_FUNC_NAME(map_lookup_elem)), + EXPORT(JIT_HELPER_FUNC_NAME(map_get_next_key)), + EXPORT(JIT_HELPER_FUNC_NAME(map_pin)), + EXPORT(JIT_HELPER_FUNC_NAME(map_get)), #undef EXPORT };
tools/perf/util/jit-helpers.c new file +60 -0
60 lines not on this page · raw message
tools/perf/util/jit-helpers.h new file +30 -0
@@ -0,0 +1,30 @@ +#ifndef JIT_HELPERS_H +#define JIT_HELPERS_H + +#include <stdint.h> +#include <util/perf-hooks.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define JIT_HELPER_FUNC_NAME (name) jit_helper__##name + +#define JIT_HELPER (type, name, ...) \ +type JIT_HELPER_FUNC_NAME(name)(__VA_ARGS__) + +#define JIT_BPF_MAP_HELPER (name, ...) \ + JIT_HELPER(int, map_##name, void *ctx, void *map, ##__VA_ARGS__) + +extern JIT_BPF_MAP_HELPER(update_elem, void *key, void *value, uint64_t flags); +extern JIT_BPF_MAP_HELPER(lookup_elem, void *key, void *value); +extern JIT_BPF_MAP_HELPER(get_next_key, void *key, void *next_key); +extern JIT_BPF_MAP_HELPER(pin, const char *pathname); + +extern JIT_HELPER(int, map_get, const char *pathname); + +#ifdef __cplusplus +} +#endif + +#endif
Signature · 1 line
2.10.1
PATCH 30/34 perf clang: Link BPF functions declaration into perf
In reply to Wang NanUse a shell script to generate BPF functions declarations from kernel source code, embed the generated header into a C string. Following commits will utilizes clang's virtual file system to automatically include this header to all BPF scripts.
The generated header is wrapped by a BUILTIN_CLANG_NO_DEFAULT_INCLUDE. This macro will be used by following commits to allow user disable this and other builtin includes.
--- tools/perf/util/c++/Build | 1 + tools/perf/util/c++/bpf-funcs-str.c | 228 +++++++++++++++++++++++++++++++ tools/perf/util/c++/clang-bpf-includes.h | 12 ++ 3 files changed, 241 insertions(+) create mode 100644 tools/perf/util/c++/bpf-funcs-str.c create mode 100644 tools/perf/util/c++/clang-bpf-includes.h
tools/perf/util/c++/Build +1 -0
@@ -1,2 +1,3 @@ libperf-$(CONFIG_CLANGLLVM) += clang.o libperf-$(CONFIG_CLANGLLVM) += clang-test.o +libperf-$(CONFIG_CLANGLLVM) += bpf-funcs-str.o
tools/perf/util/c++/bpf-funcs-str.c new file +228 -0
228 lines not on this page · raw message
tools/perf/util/c++/clang-bpf-includes.h new file +12 -0
@@ -0,0 +1,12 @@ +#ifndef CLANG_BPF_INCLUDS_H +#define CLANG_BPF_INCLUDS_H +#ifdef __cplusplus +extern "C" { +#endif + +extern const char clang_builtin_bpf_funcs_str[]; + +#ifdef __cplusplus +} +#endif +#endif
Signature · 1 line
2.10.1
PATCH 31/34 perf clang: Declare BPF functions for BPF scripts automatically
In reply to Wang NanUse Clang's OverlayFileSystem, add '-include' options to make builtin clang define BPF functions. After this patch BPF script writer needn't define BPF functions by their own.
Add -DBUILTIN_CLANG_DEFAULT_INCLUDE to builtin clang so when adopting builtin clang BPF functions can be automatically defined, and keep undefined when using external clang. Passing a -UBUILTIN_CLANG_DEFAULT_INCLUDE to cancel this defaudefinition.
Test cases are updated to avoid redefinition of these functions.
--- tools/perf/tests/bpf-script-example.c | 18 +++++++++------ tools/perf/tests/bpf-script-test-prologue.c | 2 ++ tools/perf/tests/bpf-script-test-relocation.c | 18 +++++++++------ tools/perf/util/c++/clang.cpp | 33 ++++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 15 deletions(-)
tools/perf/tests/bpf-script-example.c +11 -7
@@ -8,13 +8,6 @@#endif #define BPF_ANY 0#define BPF_MAP_TYPE_ARRAY 2 -#define BPF_FUNC_map_lookup_elem 1 -#define BPF_FUNC_map_update_elem 2 - -static void *(*bpf_map_lookup_elem)(void *map,void *key) = - (void *) BPF_FUNC_map_lookup_elem; -static void *(*bpf_map_update_elem)(void *map,void *key,void *value,int flags) = - (void *) BPF_FUNC_map_update_elem;struct bpf_map_def {unsigned int type;
@@ -24,6 +17,17 @@ struct bpf_map_def { };#define SEC (NAME ) __attribute__((section(NAME), used)) + +#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE +#define BPF_FUNC_map_lookup_elem 1 +#define BPF_FUNC_map_update_elem 2 + +static void *(*bpf_map_lookup_elem)(void *map,void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static void *(*bpf_map_update_elem)(void *map,void *key,void *value,int flags) = + (void *) BPF_FUNC_map_update_elem; +#endif + struct bpf_map_def SEC("maps") flip_table = { .type = BPF_MAP_TYPE_ARRAY, .key_size = sizeof(int),
tools/perf/tests/bpf-script-test-prologue.c +2 -0
@@ -13,8 +13,10 @@#define FMODE_READ 0x1#define FMODE_WRITE 0x2 +#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE static void (*bpf_trace_printk)(const char *fmt,int fmt_size, ...) = (void *)6 ; +#endif SEC("func=null_lseek file->f_mode offset orig") int bpf_func__null_lseek(void *ctx, int err, unsigned long f_mode,
tools/perf/tests/bpf-script-test-relocation.c +11 -7
@@ -8,13 +8,6 @@#endif #define BPF_ANY 0#define BPF_MAP_TYPE_ARRAY 2 -#define BPF_FUNC_map_lookup_elem 1 -#define BPF_FUNC_map_update_elem 2 - -static void *(*bpf_map_lookup_elem)(void *map,void *key) = - (void *) BPF_FUNC_map_lookup_elem; -static void *(*bpf_map_update_elem)(void *map,void *key,void *value,int flags) = - (void *) BPF_FUNC_map_update_elem;struct bpf_map_def {unsigned int type;
@@ -24,6 +17,17 @@ struct bpf_map_def { };#define SEC (NAME ) __attribute__((section(NAME), used)) + +#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE +#define BPF_FUNC_map_lookup_elem 1 +#define BPF_FUNC_map_update_elem 2 + +static void *(*bpf_map_lookup_elem)(void *map,void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static void *(*bpf_map_update_elem)(void *map,void *key,void *value,int flags) = + (void *) BPF_FUNC_map_update_elem; +#endif + struct bpf_map_def SEC("maps") my_table = { .type = BPF_MAP_TYPE_ARRAY, .key_size = sizeof(int),
tools/perf/util/c++/clang.cpp +32 -1
@@ -39,9 +39,17 @@ #include "util-cxx.h" #include "perf-hooks.h" #include "jit-helpers.h" +#include "clang-bpf-includes.h" namespace perf { +static struct BPFHeader { + llvm::StringRef Path; + llvm::StringRef Content; +} BPFHeaders[] = { + {"/virtual/bpf-funcs.h", clang_builtin_bpf_funcs_str}, +}; + static std::unique_ptr<llvm::LLVMContext> LLVMCtx; using namespace clang;
@@ -66,6 +74,11 @@ createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path, "-x", "c"}; CCArgs.append(CFlags.begin(), CFlags.end()); + for (BPFHeader &h : BPFHeaders) { + CCArgs.append(1, "-include"); + CCArgs.append(1, h.Path.begin()); + } + CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs); FrontendOptions& Opts = CI->getFrontendOpts();
@@ -74,6 +87,22 @@ createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path, return CI; } +static IntrusiveRefCntPtr<vfs::FileSystem> +addBPFHeaders(IntrusiveRefCntPtr<vfs::FileSystem> VFS) +{ + using namespace vfs; + + llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS( + new OverlayFileSystem(VFS)); + llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS( + new InMemoryFileSystem(true)); + OverlayFS->pushOverlay(MemFS); + + for (BPFHeader &h : BPFHeaders) + MemFS->addFile(h.Path, 0, llvm::MemoryBuffer::getMemBuffer(h.Content)); + return OverlayFS; +} + static std::unique_ptr<PerfModule> getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS)
@@ -81,7 +110,8 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, CompilerInstance Clang; Clang.createDiagnostics(); - Clang.setVirtualFileSystem(&*VFS); + IntrusiveRefCntPtr<vfs::FileSystem> OverlayVFS = addBPFHeaders(VFS); + Clang.setVirtualFileSystem(&*OverlayVFS); IntrusiveRefCntPtr<CompilerInvocation> CI = createCompilerInvocation(std::move(CFlags), Path,
@@ -364,6 +394,7 @@ public: { CFlags.push_back(KVerDef.c_str()); CFlags.push_back(NRCpusDef.c_str()); + CFlags.push_back("-DBUILTIN_CLANG_DEFAULT_INCLUDE"); fillCFlagsFromString(CFlags, clang_opt); fillCFlagsFromString(CFlags, kbuild_include_opts, true);
Signature · 1 line
2.10.1
PATCH 32/34 perf clang: Include helpers to BPF scripts
In reply to Wang NanAutomatically include some commonly used macros and struct definitions into BPF scripts. Script writers are no longer required to define 'SEC' and 'struct bpf_map_def' in each of their scripts.
--- tools/perf/tests/bpf-script-example.c | 2 +- tools/perf/tests/bpf-script-test-kbuild.c | 2 ++ tools/perf/tests/bpf-script-test-prologue.c | 4 +++- tools/perf/tests/bpf-script-test-relocation.c | 3 +-- tools/perf/util/c++/Build | 1 + tools/perf/util/c++/bpf-helper-str.c | 15 +++++++++++++++ tools/perf/util/c++/clang-bpf-includes.h | 1 + tools/perf/util/c++/clang.cpp | 1 + 8 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 tools/perf/util/c++/bpf-helper-str.c
tools/perf/tests/bpf-script-example.c +1 -1
@@ -9,6 +9,7 @@#define BPF_ANY 0#define BPF_MAP_TYPE_ARRAY 2 +#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE struct bpf_map_def { unsigned int type; unsigned int key_size;
@@ -18,7 +19,6 @@ struct bpf_map_def {#define SEC (NAME ) __attribute__((section(NAME), used)) -#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE #define BPF_FUNC_map_lookup_elem 1#define BPF_FUNC_map_update_elem 2
tools/perf/tests/bpf-script-test-kbuild.c +2 -0
@@ -6,7 +6,9 @@# error Need LINUX_VERSION_CODE# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'#endif +#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE #define SEC (NAME ) __attribute__((section(NAME), used)) +#endif #include <uapi/linux/fs.h> #include <uapi/asm/ptrace.h>
tools/perf/tests/bpf-script-test-prologue.c +3 -1
@@ -6,7 +6,6 @@# error Need LINUX_VERSION_CODE# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'#endif -#define SEC (NAME ) __attribute__((section(NAME), used))#include <uapi/linux/fs.h>
@@ -14,6 +13,9 @@#define FMODE_WRITE 0x2#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE + +#define SEC (NAME ) __attribute__((section(NAME), used)) +static void (*bpf_trace_printk)(const char *fmt,int fmt_size, ...) = (void *)6 ;#endif
tools/perf/tests/bpf-script-test-relocation.c +1 -2
@@ -9,6 +9,7 @@#define BPF_ANY 0#define BPF_MAP_TYPE_ARRAY 2 +#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE struct bpf_map_def { unsigned int type; unsigned int key_size;
@@ -17,8 +18,6 @@ struct bpf_map_def { };#define SEC (NAME ) __attribute__((section(NAME), used)) - -#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE #define BPF_FUNC_map_lookup_elem 1#define BPF_FUNC_map_update_elem 2
tools/perf/util/c++/Build +1 -0
@@ -1,3 +1,4 @@ libperf-$(CONFIG_CLANGLLVM) += clang.o libperf-$(CONFIG_CLANGLLVM) += clang-test.o libperf-$(CONFIG_CLANGLLVM) += bpf-funcs-str.o +libperf-$(CONFIG_CLANGLLVM) += bpf-helper-str.o
tools/perf/util/c++/bpf-helper-str.c new file +15 -0
@@ -0,0 +1,15 @@ +#include "clang-bpf-includes.h" +const char clang_builtin_bpf_helper_str[] = +"#ifdef BUILTIN_CLANG_DEFAULT_INCLUDE\n" +"#ifndef BPF_HELPER_DEFINED\n" +"#define BPF_HELPER_DEFINED\n" +"struct bpf_map_def {\n" +" unsigned int type;\n" +" unsigned int key_size;\n" +" unsigned int value_size;\n" +" unsigned int max_entries;\n" +"};\n" +"#define SEC(NAME) __attribute__((section(NAME), used))\n" +"#endif\n" +"#endif" +;
tools/perf/util/c++/clang-bpf-includes.h +1 -0
@@ -5,6 +5,7 @@ extern "C" {#endif extern const char clang_builtin_bpf_funcs_str[]; +extern const char clang_builtin_bpf_helper_str[]; #ifdef __cplusplus }
tools/perf/util/c++/clang.cpp +1 -0
@@ -48,6 +48,7 @@ static struct BPFHeader { llvm::StringRef Content; } BPFHeaders[] = { {"/virtual/bpf-funcs.h", clang_builtin_bpf_funcs_str}, + {"/virtual/bpf-helper.h", clang_builtin_bpf_helper_str}, }; static std::unique_ptr<llvm::LLVMContext> LLVMCtx;
Signature · 1 line
2.10.1
PATCH 33/34 perf clang builtin: Define hook helpers by default
In reply to Wang NanAppend prototypes of helpers appears in exported_funcs to default include file.
--- tools/perf/util/c++/bpf-helper-str.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/perf/util/c++/bpf-helper-str.c +8 -0
@@ -10,6 +10,14 @@ const char clang_builtin_bpf_helper_str[] =" unsigned int max_entries;\n" "};\n" "#define SEC(NAME) __attribute__((section(NAME), used))\n" +"extern void test__clang_callback(int);\n" +"extern int printf(const char *, ...);\n" +"extern int puts(const char *);\n" +"extern int jit_helper__map_update_elem(void *, void *, void *, void *, unsigned long);\n" +"extern int jit_helper__map_lookup_elem(void *, void *, void *, void *);\n" +"extern int jit_helper__map_get_next_key(void *, void *, void *, void *);\n" +"extern int jit_helper__map_pin(void *, void *, const char *);\n" +"extern int jit_helper__map_get(const char *);\n" "#endif\n" "#endif" ;
Signature · 1 line
2.10.1
PATCH 34/34 perf clang git: Export getpid() to perf hook
In reply to Wang NanAfter this patch perf hooks can retrive pid of perf itself by calling getpid. It is important for excluding event from perf.
--- tools/perf/util/c++/bpf-helper-str.c | 1 + tools/perf/util/c++/clang.cpp | 1 + 2 files changed, 2 insertions(+)
tools/perf/util/c++/bpf-helper-str.c +1 -0
@@ -13,6 +13,7 @@ const char clang_builtin_bpf_helper_str[] ="extern void test__clang_callback(int);\n" "extern int printf(const char *, ...);\n" "extern int puts(const char *);\n" +"extern int getpid(void);\n" "extern int jit_helper__map_update_elem(void *, void *, void *, void *, unsigned long);\n" "extern int jit_helper__map_lookup_elem(void *, void *, void *, void *);\n" "extern int jit_helper__map_get_next_key(void *, void *, void *, void *);\n"
tools/perf/util/c++/clang.cpp +1 -0
@@ -236,6 +236,7 @@ static std::map<const std::string, const void *> exported_funcs = EXPORT(test__clang_callback), EXPORT(printf), EXPORT(puts), + EXPORT(getpid), EXPORT(JIT_HELPER_FUNC_NAME(map_update_elem)), EXPORT(JIT_HELPER_FUNC_NAME(map_lookup_elem)), EXPORT(JIT_HELPER_FUNC_NAME(map_get_next_key)),
Signature · 1 line
2.10.1
Quoted 25 lines · Wang Nan
$ sudo -s # ulimit -l unlimited # perf record -e ./count_syscalls.c echo "Haha" Start count, perfpid=25209 Haha [ perf record: Woken up 1 times to write data ] syscall 8 count: 6 syscall 11 count: 1 syscall 4 count: 6 syscall 21 count: 1 syscall 5 count: 3 syscall 231 count: 1 syscall 45 count: 3 syscall 0 count: 24 syscall 257 count: 1 syscall 59 count: 4 syscall 23 count: 9 syscall 78 count: 2 syscall 41 count: 4 syscall 72 count: 8 syscall 10 count: 3 syscall 321 count: 1 syscall 298 count: 7 syscall 16 count: 21 syscall 9 count: 16
Quoted 8 lines
syscall 1 count: 114 syscall 12 count: 3 syscall 14 count: 35 syscall 158 count: 1 syscall 2 count: 15 syscall 7 count: 18 syscall 3 count: 11 [ perf record: Captured and wrote 0.011 MB perf.data ]
Note that this example counts system wide syscall histogram, not only 'echo' proc. The in-kernel BPF script doesn't know pid of 'echo' so can't filter base on it. I'm planning adding more perf hook points to pass information like this.
Thank you.