From patchwork Wed Oct 14 12:41:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 54950 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 A1B6F23012 for ; Wed, 14 Oct 2015 12:51:58 +0000 (UTC) Received: by wijq8 with SMTP id q8sf26146782wij.1 for ; Wed, 14 Oct 2015 05:51:58 -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=W1nxHW6aQnqCJ5pCU+oiv4rnlk+QIBVCEF5h3zfbyKA=; b=janECKkWi6aUHwORGuSjdhbFCMdpNB12/BoEo6XY7nRO2oLgKf+iFmO+Xzidst967C QyGaq50sOQefTAgWfFMdyDI9UAfv/VbjuRjbBKyiPkTGrvyjYjEsCz9cRwIIGYhBzCW6 31PXJ0S8QXPMWk3QzkEeTE/OsX/E0zmcHG76b6+j2178KEiOrcW+GZfSbDbG720Uc68k z55wc6bvIffImLfhnTTGcoNpwDck5kZrdq/wufsaEHPa0Xmf9rRQ+huE6s1f9Dd2PnDR nNmD5yZDwemx6ZUKZFdqHcjuCyE4gF+4GKa76h0ImBmALjR4aQDTH+HBYyVO7jWh/Tb0 S9Sw== X-Gm-Message-State: ALoCoQlg4S2mZCYy7Ddkw2bY7gEI+QQVgVzBa9oGcveWSfuA502xMFPhfaQgXFoYGr+tSCwiGcbS X-Received: by 10.180.105.98 with SMTP id gl2mr5515911wib.0.1444827117971; Wed, 14 Oct 2015 05:51:57 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.143.209 with SMTP id r200ls36396lfd.6.gmail; Wed, 14 Oct 2015 05:51:57 -0700 (PDT) X-Received: by 10.112.55.2 with SMTP id n2mr1549963lbp.59.1444827117788; Wed, 14 Oct 2015 05:51:57 -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 t9si5468821lfd.41.2015.10.14.05.51.57 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Oct 2015 05:51:57 -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 lbwr8 with SMTP id r8so45189564lbw.2 for ; Wed, 14 Oct 2015 05:51:57 -0700 (PDT) X-Received: by 10.112.168.228 with SMTP id zz4mr1527870lbb.73.1444827117646; Wed, 14 Oct 2015 05:51:57 -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 w3csp2714063lbq; Wed, 14 Oct 2015 05:51:53 -0700 (PDT) X-Received: by 10.68.219.133 with SMTP id po5mr3581152pbc.135.1444827109345; Wed, 14 Oct 2015 05:51:49 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id dg5si13142288pbb.15.2015.10.14.05.51.48; Wed, 14 Oct 2015 05:51:49 -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 S1753724AbbJNMvn (ORCPT + 30 others); Wed, 14 Oct 2015 08:51:43 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:50234 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753526AbbJNMmp (ORCPT ); Wed, 14 Oct 2015 08:42:45 -0400 Received: from 172.24.1.49 (EHLO szxeml425-hub.china.huawei.com) ([172.24.1.49]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id CWT05942; Wed, 14 Oct 2015 20:42:24 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml425-hub.china.huawei.com (10.82.67.180) with Microsoft SMTP Server id 14.3.235.1; Wed, 14 Oct 2015 20:42:14 +0800 From: Wang Nan To: , , CC: , , , , , , , , , , , , Wang Nan , Arnaldo Carvalho de Melo Subject: [PATCH 07/31] perf tools: Attach eBPF program to perf event Date: Wed, 14 Oct 2015 12:41:18 +0000 Message-ID: <1444826502-49291-8-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1444826502-49291-1-git-send-email-wangnan0@huawei.com> References: <1444826502-49291-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 | 6 ++++++ 3 files changed, 24 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 61c7a47..5087023 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -542,6 +542,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", @@ -562,6 +563,11 @@ static int add_bpf_event(struct probe_trace_event *tev, int fd, } pr_debug("adding %s:%s\n", tev->group, tev->event); + list_for_each_entry(pos, &new_evsels, node) { + pr_debug("adding %s:%s to %p\n", + tev->group, tev->event, pos); + pos->bpf_fd = fd; + } list_splice(&new_evsels, list); return 0; }