From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932554AbbFIFwK (ORCPT ); Tue, 9 Jun 2015 01:52:10 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:40700 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932101AbbFIFvW (ORCPT ); Tue, 9 Jun 2015 01:51:22 -0400 From: Wang Nan To: , , , , , , , , , , CC: , , , , Subject: [RFC PATCH v6 18/32] bpf tools: Load eBPF programs in object files into kernel Date: Tue, 9 Jun 2015 05:50:22 +0000 Message-ID: <1433829036-23687-19-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1433829036-23687-1-git-send-email-wangnan0@huawei.com> References: <1433829036-23687-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.200] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020204.55767ECD.0099,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: 4e34d69f97d6f48beb64fe9006b46132 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch utilizes previous introduced bpf_load_program to load programs in the ELF file into kernel. Result is stored in 'fd' field in 'struct bpf_program'. During loading, it allocs a log buffer and free it before return. Note that that buffer is not passed to bpf_load_program() if the first loading try is successful. Doesn't use a statically allocated log buffer to avoid potention multi-thread problem. Instructions collected during opening is cleared after loading. Signed-off-by: Wang Nan Acked-by: Alexei Starovoitov --- tools/lib/bpf/libbpf.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index dbec69f..9b56832 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -96,6 +96,8 @@ struct bpf_program { int map_idx; } *reloc_desc; int nr_reloc; + + int fd; }; struct bpf_object { @@ -135,11 +137,20 @@ struct bpf_object { }; #define obj_elf_valid(o) ((o)->efile.elf) +static void bpf_program__unload(struct bpf_program *prog) +{ + if (!prog) + return; + + zclose(prog->fd); +} + static void bpf_program__clear(struct bpf_program *prog) { if (!prog) return; + bpf_program__unload(prog); zfree(&prog->section_name); zfree(&prog->insns); zfree(&prog->reloc_desc); @@ -199,6 +210,7 @@ bpf_program__new(struct bpf_object *obj, void *data, size_t size, memcpy(prog->insns, data, prog->insns_cnt * sizeof(struct bpf_insn)); prog->idx = idx; + prog->fd = -1; return prog; out: @@ -709,6 +721,69 @@ static int bpf_object__collect_reloc(struct bpf_object *obj) return 0; } +static int +bpf_program__load(struct bpf_program *prog, + char *license, u32 kern_version) +{ + int fd, err; + char *log_buf; + + if (!prog->insns || !prog->insns_cnt) + return -EINVAL; + + log_buf = malloc(BPF_LOG_BUF_SIZE); + if (!log_buf) + pr_warning("Alloc log buffer for bpf loader error, continue without log\n"); + + fd = bpf_load_program(BPF_PROG_TYPE_KPROBE, prog->insns, + prog->insns_cnt, license, + kern_version, log_buf, + BPF_LOG_BUF_SIZE); + + if (fd >= 0) { + prog->fd = fd; + pr_debug("load bpf program '%s': fd = %d\n", + prog->section_name, prog->fd); + err = 0; + goto out; + } + + err = -EINVAL; + pr_warning("load bpf program '%s' failed: %s\n", + prog->section_name, strerror(errno)); + + if (log_buf) { + pr_warning("bpf: load: failed to load program '%s':\n", + prog->section_name); + pr_warning("-- BEGIN DUMP LOG ---\n"); + pr_warning("%s\n", log_buf); + pr_warning("-- END LOG --\n"); + } + +out: + zfree(&prog->insns); + prog->insns_cnt = 0; + free(log_buf); + return err; +} + +static int +bpf_object__load_progs(struct bpf_object *obj) +{ + size_t i; + int err; + + for (i = 0; i < obj->nr_programs; i++) { + err = bpf_program__load(&obj->programs[i], + obj->license, + obj->kern_version); + if (err) + return err; + } + return 0; +} + + static int bpf_object__validate(struct bpf_object *obj) { if (obj->kern_version == 0) { @@ -786,6 +861,9 @@ int bpf_object__unload(struct bpf_object *obj) zfree(&obj->map_fds); obj->nr_map_fds = 0; + for (i = 0; i < obj->nr_programs; i++) + bpf_program__unload(&obj->programs[i]); + return 0; } @@ -804,6 +882,8 @@ int bpf_object__load(struct bpf_object *obj) goto out; if (bpf_object__relocate(obj)) goto out; + if (bpf_object__load_progs(obj)) + goto out; return 0; out: -- 1.8.3.4