Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752176AbbD3K6G (ORCPT ); Thu, 30 Apr 2015 06:58:06 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:14657 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751271AbbD3Kxk (ORCPT ); Thu, 30 Apr 2015 06:53:40 -0400 From: Wang Nan To: , , , , , , CC: , , , Subject: [RFC PATCH 22/22] perf bpf: attaches eBPF program to perf fd. Date: Thu, 30 Apr 2015 10:52:45 +0000 Message-ID: <1430391165-30267-23-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1430391165-30267-1-git-send-email-wangnan0@huawei.com> References: <1430391165-30267-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.210] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.554209B2.0094,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: 5ebcac85b5c91e88d2492d073e7dd63b Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3838 Lines: 153 This patch does the final work whcih makes eBPF program actually work. It introduces bpf_attach(), which first retrive id field of previous created k(ret)probe events, then use PERF_EVENT_IOC_SET_BPF to attach eBPF program to the events. Signed-off-by: Wang Nan --- tools/perf/util/bpf-loader.c | 82 ++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 2 ++ 2 files changed, 84 insertions(+) diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index 186a3d0..c646ca4 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -15,6 +15,7 @@ #include "bpf-loader.h" #include "probe-event.h" #include "probe-finder.h" // for MAX_PROBES +#include "trace-event.h" #include #include @@ -326,6 +327,8 @@ bpf_perf_prog_free(struct bpf_perf_prog *prog) } if (prog->prog_fd >= 0) close(prog->prog_fd); + if (prog->perf_fd >= 0) + close(prog->perf_fd); free(prog); } @@ -375,6 +378,8 @@ bpf_perf_prog_alloc(struct bpf_obj *obj __maybe_unused, prog->insns_cnt * sizeof(struct bpf_insn)); prog->idx = idx; prog->prog_fd = -1; + prog->event_id = -1; + prog->perf_fd = -1; return prog; out: bpf_perf_prog_free(prog); @@ -964,6 +969,81 @@ static int bpf_probe(void) return err < 0 ? err : 0; } +static int bpf_perf_prog_attach(struct bpf_perf_prog *prog) +{ + struct event_format* format; + struct perf_event_attr attr; + int fd, err; + + format = trace_event__tp_format(prog->pev->group, + prog->pev->event); + if (!format) { + pr_err("bpf: attach: failed to get format of %s/%s\n", + prog->pev->group, prog->pev->event); + return -EINVAL; + } + + pr_debug("bpf: attach %s/%s: id=%d\n", + prog->pev->group, + prog->pev->event, + format->id); + prog->event_id = format->id; + + pevent_free_format(format); + + memset(&attr, '\0', sizeof(attr)); + + attr.type = PERF_TYPE_TRACEPOINT; + attr.sample_type = PERF_SAMPLE_RAW; + attr.sample_period = 1; + attr.wakeup_events = 1; + attr.config = prog->event_id; + + fd = sys_perf_event_open(&attr, -1, 0, -1, 0); + if (fd < 0) { + pr_err("bpf: open event %d failed\n", prog->event_id); + return -errno; + } + prog->perf_fd = fd; + + err = ioctl(fd, PERF_EVENT_IOC_ENABLE, 0); + if (err) { + pr_err("bpf attach: enable failed: %s\n", + strerror(errno)); + return -errno; + } + + err = ioctl(fd, PERF_EVENT_IOC_SET_BPF, prog->prog_fd); + if (err) { + pr_err("bpf attach: set bpf: %s\n", strerror(errno)); + return -errno; + } + pr_debug("bpf: attach %s to event %d\n", prog->name, + prog->event_id); + + return 0; +} + +static int bpf_attach(void) +{ + struct bpf_obj *obj; + int err; + + list_for_each_entry(obj, &bpf_obj_list, list) { + struct bpf_perf_prog *prog; + + list_for_each_entry(prog, &obj->progs_list, list) { + err = bpf_perf_prog_attach(prog); + if (err) { + pr_err("bpf: attach: faied to attach %s\n", + prog->name); + return err; + } + } + } + return 0; +} + int bpf__run(void) { int err; @@ -971,6 +1051,8 @@ int bpf__run(void) pr_debug("bpf: probing\n"); if ((err = bpf_probe())) return err; + if ((err = bpf_attach())) + return err; pr_info("BPF is running. Use Ctrl-c to stop.\n"); while(1) diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h index dfdc3ca..baa4404 100644 --- a/tools/perf/util/bpf-loader.h +++ b/tools/perf/util/bpf-loader.h @@ -32,6 +32,8 @@ struct bpf_perf_prog { /* fd of loaded eBPF program */ int prog_fd; + int event_id; + int perf_fd; }; struct bpf_obj { -- 1.8.3.4 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/