Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932082AbbEQK6l (ORCPT ); Sun, 17 May 2015 06:58:41 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:61492 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753303AbbEQK6M (ORCPT ); Sun, 17 May 2015 06:58:12 -0400 From: Wang Nan To: , , , , , , , , , , CC: , , Subject: [RFC PATCH v3 24/37] bpf tools: Load eBPF programs in object files into kernel Date: Sun, 17 May 2015 10:56:49 +0000 Message-ID: <1431860222-61636-25-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1431860222-61636-1-git-send-email-wangnan0@huawei.com> References: <1431860222-61636-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 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3546 Lines: 151 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. Signed-off-by: Wang Nan --- tools/lib/bpf/libbpf.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 5e25ea7..e8ef78e 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -95,6 +95,8 @@ struct bpf_program { int map_idx; } *reloc_desc; int nr_reloc; + + int fd; }; struct bpf_object { @@ -128,10 +130,24 @@ struct bpf_object { }; #define obj_elf_valid(o) ((o)->elf.fd >= 0) +static void bpf_unload_program(struct bpf_program *prog) +{ + if (!prog) + return; + + if (prog->fd >= 0) { + close(prog->fd); + prog->fd = -1; + } +} + static void bpf_clear_program(struct bpf_program *prog) { if (!prog) return; + + bpf_unload_program(prog); + if (prog->section_name) { free(prog->section_name); prog->section_name = NULL; @@ -200,6 +216,7 @@ bpf_program_alloc(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: @@ -756,6 +773,59 @@ static int bpf_obj_collect_reloc(struct bpf_object *obj) return 0; } +static int +bpf_obj_load_prog(struct bpf_object *obj, struct bpf_program *prog) +{ + int fd, err; + char *log_buf; + + 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, obj->license, + obj->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" + "-- BEGIN DUMP LOG ---\n%s\n-- END LOG --\n", + prog->section_name, log_buf); + +out: + if (log_buf) + free(log_buf); + return err; +} + +static int +bpf_obj_load_progs(struct bpf_object *obj) +{ + size_t i; + int err; + + for (i = 0; i < obj->nr_programs; i++) { + err = bpf_obj_load_prog(obj, &obj->programs[i]); + if (err) + return err; + } + return 0; +} + static int bpf_obj_validate(struct bpf_object *obj) { if (obj->kern_version == 0) { @@ -819,6 +889,13 @@ int bpf_unload_object(struct bpf_object *obj) free(obj->maps_fds); } + if (obj->programs) { + size_t i; + + for (i = 0; i < obj->nr_programs; i++) + bpf_unload_program(&obj->programs[i]); + } + return 0; } @@ -831,6 +908,8 @@ int bpf_load_object(struct bpf_object *obj) goto out; if (bpf_obj_relocate(obj)) goto out; + if (bpf_obj_load_progs(obj)) + goto out; return 0; out: -- 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/