Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751954AbbD3K6h (ORCPT ); Thu, 30 Apr 2015 06:58:37 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:14623 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751199AbbD3Kxh (ORCPT ); Thu, 30 Apr 2015 06:53:37 -0400 From: Wang Nan To: , , , , , , CC: , , , Subject: [RFC PATCH 18/22] perf bpf: load eBPF programs into kernel. Date: Thu, 30 Apr 2015 10:52:41 +0000 Message-ID: <1430391165-30267-19-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.554209AF.00DB,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: dc9a79858c2c294042c716ffc07cd037 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3143 Lines: 121 This patch use bpf system call to load all eBPF programs into kernel. Their fds are stored into prog_fd field in 'struct bpf_perf_prog'. Signed-off-by: Wang Nan --- tools/perf/util/bpf-loader.c | 58 ++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 3 +++ 2 files changed, 61 insertions(+) diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index fe623df..0395483 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -324,6 +324,8 @@ bpf_perf_prog_free(struct bpf_perf_prog *prog) clear_perf_probe_event(prog->pev); bzero(prog->pev, sizeof(*prog->pev)); } + if (prog->prog_fd >= 0) + close(prog->prog_fd); free(prog); } @@ -372,6 +374,7 @@ bpf_perf_prog_alloc(struct bpf_obj *obj __maybe_unused, memcpy(prog->insns, data, prog->insns_cnt * sizeof(struct bpf_insn)); prog->idx = idx; + prog->prog_fd = -1; return prog; out: bpf_perf_prog_free(prog); @@ -841,6 +844,59 @@ bpf_obj_relocate(struct bpf_obj *obj) return 0; } +static __u64 ptr_to_u64(void *ptr) +{ + return (__u64) (unsigned long) ptr; +} + +static int +bpf_perf_prog_load(struct bpf_obj *obj, struct bpf_perf_prog *prog) +{ + int fd; + union bpf_attr attr; +#define LOG_BUF_SIZE 65536 + static char bpf_log_buf[LOG_BUF_SIZE]; + + bzero(&attr, sizeof(attr)); + + attr.prog_type = BPF_PROG_TYPE_KPROBE; + attr.insn_cnt = prog->insns_cnt; + attr.insns = ptr_to_u64((void *) prog->insns); + attr.license = ptr_to_u64((void *) obj->license); + attr.log_buf = ptr_to_u64(bpf_log_buf); + attr.log_size = LOG_BUF_SIZE; + attr.log_level = 1; + attr.kern_version = obj->kern_version; + + bpf_log_buf[0] = 0; + fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); + + if (fd < 0) { + pr_err("bpf: load: failed to load program %s:\n" + "-- BEGIN DUMP LOG ---\n%s\n-- END LOG --\n", + prog->name, bpf_log_buf); + return fd; + } + pr_debug("bpf load: load program %s as %d\n", prog->name, fd); + prog->prog_fd = fd; + + return 0; +} + +static int +bpf_obj_load_progs(struct bpf_obj *obj) +{ + struct bpf_perf_prog *prog; + int err; + + list_for_each_entry(prog, &obj->progs_list, list) { + err = bpf_perf_prog_load(obj, prog); + if (err) + return err; + } + return 0; +} + int bpf__load(const char *path) { struct bpf_obj *obj; @@ -873,6 +929,8 @@ int bpf__load(const char *path) goto out; if ((err = bpf_obj_relocate(obj))) goto out; + if ((err = bpf_obj_load_progs(obj))) + goto out; list_add(&obj->list, &bpf_obj_list); return 0; diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h index 756ac2e..dfdc3ca 100644 --- a/tools/perf/util/bpf-loader.h +++ b/tools/perf/util/bpf-loader.h @@ -29,6 +29,9 @@ struct bpf_perf_prog { size_t insns_cnt; struct perf_probe_event *pev; + + /* fd of loaded eBPF program */ + int prog_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/