From patchwork Thu Oct 8 08:29:29 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 54647 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f199.google.com (mail-wi0-f199.google.com [209.85.212.199]) by patches.linaro.org (Postfix) with ESMTPS id A6FE922FF8 for ; Thu, 8 Oct 2015 08:44:15 +0000 (UTC) Received: by wicgb1 with SMTP id gb1sf6899524wic.3 for ; Thu, 08 Oct 2015 01:44:14 -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=WYtnyXPLzGKZoRPXdFcxIql8M0vRGqj/02LMBLxKJS4=; b=UiYbGt+3kgB0AMJW6OHcbkjDhtCOnGBQX1YU+azSUkD30c/fRPJbyRUl9ocSXWmwGt arPrHvbtq/KtFfZxG4OE8omwRS5FxvKglnO9LGW05auB84NgRfTGBfXUiLnH8P18TTh6 bLM/EiNaHfpjMY/T5fTEgfV+6XKEPmsdFTMpJSbnAK/R1xEAKfpujyiMMJAgZH7U/uun Fdh/S3NtdB0gc1W1mH7rx8rR6ZGp+QWp5aRLxXhX2Yr2MazmFnMA26BXMVpfwQLK5c1c D7Bppa2lWd1Zy0GL69CSo+OLIXnJTTI2OhlWQWC2I2fqGbYbIZXvXPQKKVSRIgq1REOl JXlQ== X-Gm-Message-State: ALoCoQl+n06N4ghWs42FpgOCL+/PjtGK5rqvzdzLyPFqV8AriIPEiap2sCzgj7KgaFqYg0bscicG X-Received: by 10.112.151.9 with SMTP id um9mr1115477lbb.19.1444293854747; Thu, 08 Oct 2015 01:44:14 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.20.230 with SMTP id 99ls159989lfu.32.gmail; Thu, 08 Oct 2015 01:44:14 -0700 (PDT) X-Received: by 10.112.17.105 with SMTP id n9mr3040522lbd.78.1444293854310; Thu, 08 Oct 2015 01:44:14 -0700 (PDT) Received: from mail-lb0-f169.google.com (mail-lb0-f169.google.com. [209.85.217.169]) by mx.google.com with ESMTPS id ut6si28824141lbc.67.2015.10.08.01.44.14 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Oct 2015 01:44:14 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.169 as permitted sender) client-ip=209.85.217.169; Received: by lbcao8 with SMTP id ao8so39262379lbc.3 for ; Thu, 08 Oct 2015 01:44:14 -0700 (PDT) X-Received: by 10.25.77.133 with SMTP id a127mr1942909lfb.19.1444293854059; Thu, 08 Oct 2015 01:44:14 -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 w3csp443626lbq; Thu, 8 Oct 2015 01:44:12 -0700 (PDT) X-Received: by 10.66.66.166 with SMTP id g6mr2193182pat.152.1444293852291; Thu, 08 Oct 2015 01:44:12 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id dm1si32953475pbc.117.2015.10.08.01.44.11; Thu, 08 Oct 2015 01:44:12 -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 S1754788AbbJHIoI (ORCPT + 30 others); Thu, 8 Oct 2015 04:44:08 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:60475 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754091AbbJHIoE (ORCPT ); Thu, 8 Oct 2015 04:44:04 -0400 Received: from 172.24.1.48 (EHLO szxeml427-hub.china.huawei.com) ([172.24.1.48]) by szxrg02-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id CTW16117; Thu, 08 Oct 2015 16:29:57 +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:31 +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 , Paul Mackerras , Peter Zijlstra Subject: [PATCH 07/22] perf tools: Attach eBPF program to perf event Date: Thu, 8 Oct 2015 08:29:29 +0000 Message-ID: <1444292984-13135-8-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 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.169 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 is the final patch which makes basic BPF filter work. After applying this patch, users are allowed to use BPF filter like: # perf record --event ./hello_world.o ls A bpf_fd field is appended to 'struct evsel', and setup during the callback function add_bpf_event() for each 'probe_trace_event'. PERF_EVENT_IOC_SET_BPF ioctl is used to attach eBPF program to a newly created perf event. The file descriptor of the eBPF program is passed to perf record using previous patches, and stored into evsel->bpf_fd. It is possible that different perf event are created for one kprobe events for different CPUs. In this case, when trying to call the ioctl, EEXIST will be return. This patch doesn't treat it as an error. Signed-off-by: Wang Nan 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: Paul Mackerras Cc: Peter Zijlstra Cc: Zefan Li Cc: pi3orama@163.com Cc: Arnaldo Carvalho de Melo Link: http://lkml.kernel.org/n/ebpf-6yw9eg0ej3l4jnqhinngkw86@git.kernel.org --- tools/perf/util/evsel.c | 17 +++++++++++++++++ tools/perf/util/evsel.h | 1 + tools/perf/util/parse-events.c | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 8be867c..7d12af5 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -208,6 +208,7 @@ void perf_evsel__init(struct perf_evsel *evsel, evsel->unit = ""; evsel->scale = 1.0; evsel->evlist = NULL; + evsel->bpf_fd = -1; INIT_LIST_HEAD(&evsel->node); INIT_LIST_HEAD(&evsel->config_terms); perf_evsel__object.init(evsel); @@ -1346,6 +1347,22 @@ retry_open: err); goto try_fallback; } + + if (evsel->bpf_fd >= 0) { + int evt_fd = FD(evsel, cpu, thread); + int bpf_fd = evsel->bpf_fd; + + err = ioctl(evt_fd, + PERF_EVENT_IOC_SET_BPF, + bpf_fd); + if (err && errno != EEXIST) { + pr_err("failed to attach bpf fd %d: %s\n", + bpf_fd, strerror(errno)); + err = -EINVAL; + goto out_close; + } + } + set_rlimit = NO_CHANGE; /* diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 02a5fed..a60b5d5 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -121,6 +121,7 @@ struct perf_evsel { char *group_name; bool cmdline_group_boundary; struct list_head config_terms; + int bpf_fd; }; union u64_swap { diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 3c4842c..771b906 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -541,6 +541,7 @@ static int add_bpf_event(struct probe_trace_event *tev, int fd, struct __add_bpf_event_param *param = _param; struct parse_events_evlist *evlist = param->data; struct list_head *list = param->list; + struct perf_evsel *pos; int err; pr_debug("add bpf event %s:%s and attach bpf program %d\n", @@ -560,6 +561,9 @@ static int add_bpf_event(struct probe_trace_event *tev, int fd, return err; } pr_debug("adding %s:%s\n", tev->group, tev->event); + + list_for_each_entry(pos, list, node) + pos->bpf_fd = fd; return 0; }