Skip to content

Commit bbb7d49

Browse files
WangNan0acmel
authored andcommitted
perf test: Test the BPF prologue adding infrastructure
This patch introduces a new BPF script to test the BPF prologue adding routines. The new script probes at null_lseek, which is the function pointer used when we try to lseek on '/dev/null'. The null_lseek function is chosen because it is used by function pointers, so we don't need to consider inlining and LTO. By extracting file->f_mode, bpf-script-test-prologue.c should know whether the file is writable or readonly. According to llseek_loop() and bpf-script-test-prologue.c, one fourth of total lseeks should be collected. Committer note: Testing it: # perf test -v BPF <SNIP> Kernel build dir is set to /lib/modules/4.3.0+/build set env: KBUILD_DIR=/lib/modules/4.3.0+/build unset env: KBUILD_OPTS include option is set to -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.9.2/include -I/home/git/linux/arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -I/home/git/linux/include -Iinclude -I/home/git/linux/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/git/linux/include/uapi -Iinclude/generated/uapi -include /home/git/linux/include/linux/kconfig.h set env: NR_CPUS=4 set env: LINUX_VERSION_CODE=0x40300 set env: CLANG_EXEC=/usr/libexec/icecc/bin/clang set env: CLANG_OPTIONS=-xc set env: KERNEL_INC_OPTIONS= -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.9.2/include -I/home/git/linux/arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -I/home/git/linux/include -Iinclude -I/home/git/linux/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/git/linux/include/uapi -Iinclude/generated/uapi -include /home/git/linux/include/linux/kconfig.h set env: WORKING_DIR=/lib/modules/4.3.0+/build set env: CLANG_SOURCE=- llvm compiling command template: echo '/* * bpf-script-test-prologue.c * Test BPF prologue */ #ifndef LINUX_VERSION_CODE # 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> #define FMODE_READ 0x1 #define FMODE_WRITE 0x2 static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = (void *) 6; SEC("func=null_lseek file->f_mode offset orig") int bpf_func__null_lseek(void *ctx, int err, unsigned long f_mode, unsigned long offset, unsigned long orig) { if (err) return 0; if (f_mode & FMODE_WRITE) return 0; if (offset & 1) return 0; if (orig == SEEK_CUR) return 0; return 1; } char _license[] SEC("license") = "GPL"; int _version SEC("version") = LINUX_VERSION_CODE; ' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o - libbpf: loading object '[bpf_prologue_test]' from buffer libbpf: section .strtab, size 135, link 0, flags 0, type=3 libbpf: section .text, size 0, link 0, flags 6, type=1 libbpf: section .data, size 0, link 0, flags 3, type=1 libbpf: section .bss, size 0, link 0, flags 3, type=8 libbpf: section func=null_lseek file->f_mode offset orig, size 112, link 0, flags 6, type=1 libbpf: found program func=null_lseek file->f_mode offset orig libbpf: section license, size 4, link 0, flags 3, type=1 libbpf: license of [bpf_prologue_test] is GPL libbpf: section version, size 4, link 0, flags 3, type=1 libbpf: kernel version of [bpf_prologue_test] is 40300 libbpf: section .symtab, size 168, link 1, flags 0, type=2 bpf: config program 'func=null_lseek file->f_mode offset orig' symbol:null_lseek file:(null) line:0 offset:0 return:0 lazy:(null) parsing arg: file->f_mode into file, f_mode(1) parsing arg: offset into offset parsing arg: orig into orig bpf: config 'func=null_lseek file->f_mode offset orig' is ok Looking at the vmlinux_path (7 entries long) Using /lib/modules/4.3.0+/build/vmlinux for symbols Open Debuginfo file: /lib/modules/4.3.0+/build/vmlinux Try to find probe point from debuginfo. Matched function: null_lseek Probe point found: null_lseek+0 Searching 'file' variable in context. Converting variable file into trace event. converting f_mode in file f_mode type is unsigned int. Searching 'offset' variable in context. Converting variable offset into trace event. offset type is long long int. Searching 'orig' variable in context. Converting variable orig into trace event. orig type is int. Found 1 probe_trace_events. Opening /sys/kernel/debug/tracing//kprobe_events write=1 Writing event: p:perf_bpf_probe/func _text+4840528 f_mode=+68(%di):u32 offset=%si:s64 orig=%dx:s32 libbpf: don't need create maps for [bpf_prologue_test] prologue: pass validation prologue: slow path prologue: fetch arg 0, base reg is %di prologue: arg 0: offset 68 prologue: fetch arg 1, base reg is %si prologue: fetch arg 2, base reg is %dx add bpf event perf_bpf_probe:func and attach bpf program 3 adding perf_bpf_probe:func adding perf_bpf_probe:func to 0x51672c0 mmap size 1052672B Opening /sys/kernel/debug/tracing//kprobe_events write=1 Opening /sys/kernel/debug/tracing//uprobe_events write=1 Parsing probe_events: p:perf_bpf_probe/func _text+4840528 f_mode=+68(%di):u32 offset=%si:s64 orig=%dx:s32 Group:perf_bpf_probe Event:func probe:p Writing event: -:perf_bpf_probe/func test child finished with 0 ---- end ---- Test BPF filter: Ok # Signed-off-by: Wang Nan <wangnan0@huawei.com> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Zefan Li <lizefan@huawei.com> Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1447675815-166222-13-git-send-email-wangnan0@huawei.com [ Added tools/perf/tests/llvm-src-prologue.c to .gitignore ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent a08357d commit bbb7d49

6 files changed

Lines changed: 84 additions & 1 deletion

File tree

‎tools/perf/tests/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
llvm-src-base.c
22
llvm-src-kbuild.c
3+
llvm-src-prologue.c

‎tools/perf/tests/Build‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ perf-y += sample-parsing.o
3131
perf-y += parse-no-sample-id-all.o
3232
perf-y += kmod-path.o
3333
perf-y += thread-map.o
34-
perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o
34+
perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o llvm-src-prologue.o
3535
perf-y += bpf.o
3636
perf-y += topology.o
3737

@@ -49,6 +49,13 @@ $(OUTPUT)tests/llvm-src-kbuild.c: tests/bpf-script-test-kbuild.c
4949
$(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
5050
$(Q)echo ';' >> $@
5151

52+
$(OUTPUT)tests/llvm-src-prologue.c: tests/bpf-script-test-prologue.c
53+
$(call rule_mkdir)
54+
$(Q)echo '#include <tests/llvm.h>' > $@
55+
$(Q)echo 'const char test_llvm__bpf_test_prologue_prog[] =' >> $@
56+
$(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
57+
$(Q)echo ';' >> $@
58+
5259
ifeq ($(ARCH),$(filter $(ARCH),x86 arm arm64))
5360
perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
5461
endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* bpf-script-test-prologue.c
3+
* Test BPF prologue
4+
*/
5+
#ifndef LINUX_VERSION_CODE
6+
# error Need LINUX_VERSION_CODE
7+
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
8+
#endif
9+
#define SEC(NAME) __attribute__((section(NAME), used))
10+
11+
#include <uapi/linux/fs.h>
12+
13+
#define FMODE_READ 0x1
14+
#define FMODE_WRITE 0x2
15+
16+
static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
17+
(void *) 6;
18+
19+
SEC("func=null_lseek file->f_mode offset orig")
20+
int bpf_func__null_lseek(void *ctx, int err, unsigned long f_mode,
21+
unsigned long offset, unsigned long orig)
22+
{
23+
if (err)
24+
return 0;
25+
if (f_mode & FMODE_WRITE)
26+
return 0;
27+
if (offset & 1)
28+
return 0;
29+
if (orig == SEEK_CUR)
30+
return 0;
31+
return 1;
32+
}
33+
34+
char _license[] SEC("license") = "GPL";
35+
int _version SEC("version") = LINUX_VERSION_CODE;

‎tools/perf/tests/bpf.c‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ static int epoll_pwait_loop(void)
1919
return 0;
2020
}
2121

22+
#ifdef HAVE_BPF_PROLOGUE
23+
24+
static int llseek_loop(void)
25+
{
26+
int fds[2], i;
27+
28+
fds[0] = open("/dev/null", O_RDONLY);
29+
fds[1] = open("/dev/null", O_RDWR);
30+
31+
if (fds[0] < 0 || fds[1] < 0)
32+
return -1;
33+
34+
for (i = 0; i < NR_ITERS; i++) {
35+
lseek(fds[i % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
36+
lseek(fds[(i + 1) % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
37+
}
38+
close(fds[0]);
39+
close(fds[1]);
40+
return 0;
41+
}
42+
43+
#endif
44+
2245
static struct {
2346
enum test_llvm__testcase prog_id;
2447
const char *desc;
@@ -37,6 +60,17 @@ static struct {
3760
&epoll_pwait_loop,
3861
(NR_ITERS + 1) / 2,
3962
},
63+
#ifdef HAVE_BPF_PROLOGUE
64+
{
65+
LLVM_TESTCASE_BPF_PROLOGUE,
66+
"Test BPF prologue generation",
67+
"[bpf_prologue_test]",
68+
"fix kbuild first",
69+
"check your vmlinux setting?",
70+
&llseek_loop,
71+
(NR_ITERS + 1) / 4,
72+
},
73+
#endif
4074
};
4175

4276
static int do_test(struct bpf_object *obj, int (*func)(void),

‎tools/perf/tests/llvm.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ static struct {
4444
.source = test_llvm__bpf_test_kbuild_prog,
4545
.desc = "Test kbuild searching",
4646
},
47+
[LLVM_TESTCASE_BPF_PROLOGUE] = {
48+
.source = test_llvm__bpf_test_prologue_prog,
49+
.desc = "Test BPF prologue generation",
50+
},
4751
};
4852

4953

‎tools/perf/tests/llvm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
extern const char test_llvm__bpf_base_prog[];
88
extern const char test_llvm__bpf_test_kbuild_prog[];
9+
extern const char test_llvm__bpf_test_prologue_prog[];
910

1011
enum test_llvm__testcase {
1112
LLVM_TESTCASE_BASE,
1213
LLVM_TESTCASE_KBUILD,
14+
LLVM_TESTCASE_BPF_PROLOGUE,
1315
__LLVM_TESTCASE_MAX,
1416
};
1517

0 commit comments

Comments
 (0)