From patchwork Wed Sep 23 11:22:27 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 54043 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f198.google.com (mail-wi0-f198.google.com [209.85.212.198]) by patches.linaro.org (Postfix) with ESMTPS id 02C1722E14 for ; Wed, 23 Sep 2015 11:26:14 +0000 (UTC) Received: by wicuu12 with SMTP id uu12sf22689627wic.2 for ; Wed, 23 Sep 2015 04:26:13 -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=D7TWrfOKGe5SmUj3VllhRmoij8VRJfd88E6CQ3ol/SY=; b=HE23/e7DSK05iKX37NguPmi7qDrBwqB9oj3cJmCIwlw8g2iTKBIT+2g0aVOal89ucF IZBqs3dAJkg2gEmxEmnVJuG9G7aKLNoVDAth7ZvjX/+e0Brw3xLuZDyo1WmaMFO1hT8k 7COErgQFEeR1uUo/8We1zOpRDjZIDjMIrOUUVg/7owvixaS9a7yu6vL2W6jP6sv0geYX ZsBpWpxSWe0p97bn16lUxRf2NF50X5syAaWEirsMYs9lHMN89v6P0DtIaskiWreBI3vt Ar2VamWWnL0mltcQvWXs32R0i6tEoNO6WpvQvR6HZbhY5UNks4PbeRZARn+vAy8BgFF2 Lcww== X-Gm-Message-State: ALoCoQlgez6EG12QMBKjRzDBGh6Q4Fqhguh/pOJwmX6Mo2VNRxPZck1OKebWSZ5+zg6JJrxSXhR1 X-Received: by 10.194.109.233 with SMTP id hv9mr5204242wjb.1.1443007573298; Wed, 23 Sep 2015 04:26:13 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.158.7 with SMTP id h7ls98158lfe.66.gmail; Wed, 23 Sep 2015 04:26:13 -0700 (PDT) X-Received: by 10.112.150.201 with SMTP id uk9mr9606183lbb.102.1443007573117; Wed, 23 Sep 2015 04:26:13 -0700 (PDT) Received: from mail-la0-f42.google.com (mail-la0-f42.google.com. [209.85.215.42]) by mx.google.com with ESMTPS id v3si1655224lbw.9.2015.09.23.04.26.13 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Sep 2015 04:26:13 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.42 as permitted sender) client-ip=209.85.215.42; Received: by lahg1 with SMTP id g1so46439003lah.1 for ; Wed, 23 Sep 2015 04:26:13 -0700 (PDT) X-Received: by 10.152.5.170 with SMTP id t10mr11093002lat.112.1443007572982; Wed, 23 Sep 2015 04:26:12 -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 w3csp1044432lbq; Wed, 23 Sep 2015 04:26:11 -0700 (PDT) X-Received: by 10.68.68.233 with SMTP id z9mr37095156pbt.132.1443007571742; Wed, 23 Sep 2015 04:26:11 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ak1si9872455pad.190.2015.09.23.04.26.08; Wed, 23 Sep 2015 04:26:11 -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 S1754684AbbIWL0E (ORCPT + 30 others); Wed, 23 Sep 2015 07:26:04 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:56842 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754458AbbIWLXr (ORCPT ); Wed, 23 Sep 2015 07:23:47 -0400 Received: from 172.24.1.48 (EHLO szxeml426-hub.china.huawei.com) ([172.24.1.48]) by szxrg03-dlp.huawei.com (MOS 4.4.3-GA FastPath queued) with ESMTP id BNW12487; Wed, 23 Sep 2015 19:23:07 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml426-hub.china.huawei.com (10.82.67.181) with Microsoft SMTP Server id 14.3.235.1; Wed, 23 Sep 2015 19:23:00 +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 , Zefan Li Subject: [PATCH 06/22] perf tools: Attach eBPF program to perf event Date: Wed, 23 Sep 2015 11:22:27 +0000 Message-ID: <1443007363-124182-7-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1443007363-124182-1-git-send-email-wangnan0@huawei.com> References: <1443007363-124182-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.0A010202.56028B9C.0035, 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: 3d7de5bc628fa7dae99042137e0b0f19 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.215.42 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 5889004..3f69d72 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); @@ -1343,6 +1344,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 7906666..96f1294 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -120,6 +120,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 c3505fb..37cb196 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -527,6 +527,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", @@ -546,6 +547,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; }