From patchwork Thu Oct 8 08:29:44 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 54637 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f200.google.com (mail-lb0-f200.google.com [209.85.217.200]) by patches.linaro.org (Postfix) with ESMTPS id B364F22FF8 for ; Thu, 8 Oct 2015 08:38:05 +0000 (UTC) Received: by lbbti1 with SMTP id ti1sf19957958lbb.3 for ; Thu, 08 Oct 2015 01:38:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-type:sender:precedence :list-id:x-original-sender:x-original-authentication-results :mailing-list:list-post:list-help:list-archive:list-unsubscribe; bh=lUttY6eIhDg1iOfMeGZQRjoEkC6SIEWIKe4u0GXw+Gs=; b=kM4rEBxgobn6bsFFbp4tGiti9ADoRcQ4/kImV9Ez0W3zejPqz8PllrP0GazCtfH9Qj REjTibugYvoh8lLI4vGxu/E8Hqtm3H3QHKGzVFoqVyFMRGTozYsVlbcdSe04jfJricAZ 2UjTsmLVY9KeC11COyxpFt8ypI5ms9P/j1+Wi2B/utgatMwxn9D7vSuTQGbal6fAZSO0 TA2koSaPUoLsU1DcDjSqBn0xFdNtzTPHsLKGxhI8uMaVLnAInHUBJ7ue7Qw1vboEUDBY FEQEMqUSqDJ7N3OQQ5zDEj44jHsS5eQlFbO51IionKwJYX2NnUL3FTSh/vWnmyyrDhwg MfpA== X-Gm-Message-State: ALoCoQlvN/yfEPYPJMyMsEskG64O5rlAxNJp0mC7OT3YSTkjgjM0NLhwAaB6ck2qRGl6MkcjM2MG X-Received: by 10.112.132.6 with SMTP id oq6mr1119845lbb.1.1444293484702; Thu, 08 Oct 2015 01:38:04 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.19.36 with SMTP id j36ls170748lfi.50.gmail; Thu, 08 Oct 2015 01:38:04 -0700 (PDT) X-Received: by 10.112.147.39 with SMTP id th7mr2950311lbb.82.1444293484531; Thu, 08 Oct 2015 01:38:04 -0700 (PDT) Received: from mail-lb0-f177.google.com (mail-lb0-f177.google.com. [209.85.217.177]) by mx.google.com with ESMTPS id yj8si28792553lbb.100.2015.10.08.01.38.04 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Oct 2015 01:38:04 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.177 as permitted sender) client-ip=209.85.217.177; Received: by lbwr8 with SMTP id r8so38431681lbw.2 for ; Thu, 08 Oct 2015 01:38:04 -0700 (PDT) X-Received: by 10.112.146.104 with SMTP id tb8mr3015259lbb.35.1444293484278; Thu, 08 Oct 2015 01:38:04 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp440846lbq; Thu, 8 Oct 2015 01:38:03 -0700 (PDT) X-Received: by 10.66.230.137 with SMTP id sy9mr6924546pac.154.1444293483074; Thu, 08 Oct 2015 01:38:03 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id sn2si59329137pac.146.2015.10.08.01.38.02; Thu, 08 Oct 2015 01:38:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754504AbbJHIe7 (ORCPT + 30 others); Thu, 8 Oct 2015 04:34:59 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:51313 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754193AbbJHIe4 (ORCPT ); Thu, 8 Oct 2015 04:34:56 -0400 Received: from 172.24.1.49 (EHLO szxeml427-hub.china.huawei.com) ([172.24.1.49]) by szxrg03-dlp.huawei.com (MOS 4.4.3-GA FastPath queued) with ESMTP id BOO15959; Thu, 08 Oct 2015 16:30:51 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml427-hub.china.huawei.com (10.82.67.182) with Microsoft SMTP Server id 14.3.235.1; Thu, 8 Oct 2015 16:29:46 +0800 From: Wang Nan To: CC: , , , Wang Nan , Alexei Starovoitov , Brendan Gregg , Daniel Borkmann , David Ahern , He Kuang , Jiri Olsa , Kaixu Xia , Masami Hiramatsu , Namhyung Kim , Peter Zijlstra Subject: [PATCH 22/22] perf test: Test BPF prologue Date: Thu, 8 Oct 2015 08:29:44 +0000 Message-ID: <1444292984-13135-23-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1444292984-13135-1-git-send-email-wangnan0@huawei.com> References: <1444292984-13135-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020204.56162A84.0151, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: feb08d4f2e71e6e1aad311859a313f7d Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: wangnan0@huawei.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.177 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , This patch introduces a new BPF script to test BPF prologue. The new script probes at null_lseek, which is the function pointer when we try to lseek on '/dev/null'. null_lseek is chosen because it is a function pointer, so we don't need to consider inlining and LTP. 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 forth of total lseeks should be collected. This patch improve test__bpf so it can run multiple BPF programs on different test functions. Signed-off-by: Wang Nan Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Brendan Gregg Cc: Daniel Borkmann Cc: David Ahern Cc: He Kuang Cc: Jiri Olsa Cc: Kaixu Xia Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/n/ebpf-6yw9eg0ej3l4jnqhinngkw86@git.kernel.org --- tools/perf/tests/Build | 9 ++- tools/perf/tests/bpf-script-test-prologue.c | 35 ++++++++++++ tools/perf/tests/bpf.c | 89 +++++++++++++++++++++++------ tools/perf/tests/llvm.c | 5 ++ tools/perf/tests/llvm.h | 8 +++ 5 files changed, 128 insertions(+), 18 deletions(-) create mode 100644 tools/perf/tests/bpf-script-test-prologue.c diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index 23e2201..1d69493 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -31,7 +31,7 @@ perf-y += sample-parsing.o perf-y += parse-no-sample-id-all.o perf-y += kmod-path.o perf-y += thread-map.o -perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o +perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o llvm-src-prologue.o perf-y += bpf.o perf-y += topology.o @@ -49,6 +49,13 @@ $(OUTPUT)tests/llvm-src-kbuild.c: tests/bpf-script-test-kbuild.c $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ $(Q)echo ';' >> $@ +$(OUTPUT)tests/llvm-src-prologue.c: tests/bpf-script-test-prologue.c + $(call rule_mkdir) + $(Q)echo '#include ' > $@ + $(Q)echo 'const char test_llvm__bpf_test_prologue_prog[] =' >> $@ + $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ + $(Q)echo ';' >> $@ + ifeq ($(ARCH),$(filter $(ARCH),x86 arm arm64)) perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o endif diff --git a/tools/perf/tests/bpf-script-test-prologue.c b/tools/perf/tests/bpf-script-test-prologue.c new file mode 100644 index 0000000..7230e62 --- /dev/null +++ b/tools/perf/tests/bpf-script-test-prologue.c @@ -0,0 +1,35 @@ +/* + * 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 + +#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; diff --git a/tools/perf/tests/bpf.c b/tools/perf/tests/bpf.c index 4dd701b..5a6290a 100644 --- a/tools/perf/tests/bpf.c +++ b/tools/perf/tests/bpf.c @@ -19,11 +19,35 @@ static int epoll_pwait_loop(void) return 0; } -static struct bpf_object *prepare_bpf(void *obj_buf, size_t obj_buf_sz) +#ifdef HAVE_BPF_PROLOGUE + +static int llseek_loop(void) +{ + int fds[2], i; + + fds[0] = open("/dev/null", O_RDONLY); + fds[1] = open("/dev/null", O_RDWR); + + if (fds[0] < 0 || fds[1] < 0) + return -1; + + for (i = 0; i < NR_ITERS; i++) { + lseek(fds[i % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET); + lseek(fds[(i + 1) % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET); + } + close(fds[0]); + close(fds[1]); + return 0; +} + +#endif + +static struct bpf_object *prepare_bpf(const char *name, void *obj_buf, + size_t obj_buf_sz) { struct bpf_object *obj; - obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, "[buffer]"); + obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name); if (IS_ERR(obj)) { fprintf(stderr, " (compile failed)"); return NULL; @@ -31,7 +55,7 @@ static struct bpf_object *prepare_bpf(void *obj_buf, size_t obj_buf_sz) return obj; } -static int do_test(struct bpf_object *obj) +static int do_test(struct bpf_object *obj, int (*func)(void), int expect) { struct record_opts opts = { .target = { @@ -101,7 +125,7 @@ static int do_test(struct bpf_object *obj) } perf_evlist__enable(evlist); - epoll_pwait_loop(); + (*func)(); perf_evlist__disable(evlist); for (i = 0; i < evlist->nr_mmaps; i++) { @@ -115,8 +139,8 @@ static int do_test(struct bpf_object *obj) } } - if (count != (NR_ITERS + 1) / 2) { - fprintf(stderr, " (filter result incorrect)"); + if (count != expect) { + fprintf(stderr, " (filter result incorrect: %d != %d)", count, expect); err = -EBADF; } @@ -128,33 +152,32 @@ out: return 0; } -int test__bpf(void) +static int __test__bpf(int index, const char *name, + const char *message_compile, + const char *message_load, + int (*func)(void), int expect) { int err; void *obj_buf; size_t obj_buf_sz; struct bpf_object *obj; - if (geteuid() != 0) { - fprintf(stderr, " (try run as root)"); - return TEST_SKIP; - } - - test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz, LLVM_TESTCASE_BASE); - + test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz, index); if (!obj_buf || !obj_buf_sz) { if (verbose == 0) - fprintf(stderr, " (fix 'perf test LLVM' first)"); + fprintf(stderr, " (%s)", message_compile); return TEST_SKIP; } - obj = prepare_bpf(obj_buf, obj_buf_sz); + obj = prepare_bpf(name, obj_buf, obj_buf_sz); if (!obj) { err = -EINVAL; + if ((verbose == 0) && (message_load[0] != '\0')) + fprintf(stderr, " (%s)", message_load); goto out; } - err = do_test(obj); + err = do_test(obj, func, expect); if (err) goto out; out: @@ -164,6 +187,38 @@ out: return 0; } +int test__bpf(void) +{ + int err; + + if (geteuid() != 0) { + fprintf(stderr, " (try run as root)"); + return TEST_SKIP; + } + + err = __test__bpf(LLVM_TESTCASE_BASE, + "[basic_bpf_test]", + "fix 'perf test LLVM' first", + "load bpf object failed", + &epoll_pwait_loop, + (NR_ITERS + 1) / 2); + if (err) + return err; + +#ifdef HAVE_BPF_PROLOGUE + err = __test__bpf(LLVM_TESTCASE_BPF_PROLOGUE, + "[bpf_prologue_test]", + "fix kbuild first", + "check your vmlinux setting?", + &llseek_loop, + (NR_ITERS + 1) / 4); + return err; +#else + fprintf(stderr, " (skip BPF prologue test)"); + return TEST_OK; +#endif +} + #else int test__bpf(void) { diff --git a/tools/perf/tests/llvm.c b/tools/perf/tests/llvm.c index 75cd99f..e722e8a 100644 --- a/tools/perf/tests/llvm.c +++ b/tools/perf/tests/llvm.c @@ -22,6 +22,11 @@ struct llvm_testcase { [LLVM_TESTCASE_KBUILD] = {.source = test_llvm__bpf_test_kbuild_prog, .errmsg = "llvm.kbuild-dir can be fixed", .tried = false}, + /* Don't output if this one fail. */ + [LLVM_TESTCASE_BPF_PROLOGUE] = { + .source = test_llvm__bpf_test_prologue_prog, + .errmsg = "failed for unknown reason", + .tried = false}, {.source = NULL} }; diff --git a/tools/perf/tests/llvm.h b/tools/perf/tests/llvm.h index 78ec01d..c00c1be 100644 --- a/tools/perf/tests/llvm.h +++ b/tools/perf/tests/llvm.h @@ -10,10 +10,18 @@ struct test_llvm__bpf_result { extern const char test_llvm__bpf_prog[]; extern const char test_llvm__bpf_test_kbuild_prog[]; +extern const char test_llvm__bpf_test_prologue_prog[]; enum test_llvm__testcase { LLVM_TESTCASE_BASE, LLVM_TESTCASE_KBUILD, + /* + * We must put LLVM_TESTCASE_BPF_PROLOGUE after + * LLVM_TESTCASE_KBUILD, so if kbuild test failed, + * don't need to try this one, because it depend on + * kernel header. + */ + LLVM_TESTCASE_BPF_PROLOGUE, NR_LLVM_TESTCASES, }; void test_llvm__fetch_bpf_obj(void **p_obj_buf, size_t *p_obj_buf_sz, int index);