Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932753AbbHHILQ (ORCPT ); Sat, 8 Aug 2015 04:11:16 -0400 Received: from terminus.zytor.com ([198.137.202.10]:53658 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932287AbbHHILK (ORCPT ); Sat, 8 Aug 2015 04:11:10 -0400 Date: Sat, 8 Aug 2015 01:10:34 -0700 From: tip-bot for Wang Nan Message-ID: Cc: xiakaixu@huawei.com, linux-kernel@vger.kernel.org, masami.hiramatsu.pt@hitachi.com, jolsa@kernel.org, ast@plumgrid.com, tglx@linutronix.de, hekuang@huawei.com, namhyung@kernel.org, daniel@iogearbox.net, acme@redhat.com, lizefan@huawei.com, a.p.zijlstra@chello.nl, wangnan0@huawei.com, hpa@zytor.com, brendan.d.gregg@gmail.com, mingo@kernel.org, dsahern@gmail.com Reply-To: ast@plumgrid.com, jolsa@kernel.org, namhyung@kernel.org, hekuang@huawei.com, tglx@linutronix.de, linux-kernel@vger.kernel.org, xiakaixu@huawei.com, masami.hiramatsu.pt@hitachi.com, dsahern@gmail.com, mingo@kernel.org, brendan.d.gregg@gmail.com, daniel@iogearbox.net, acme@redhat.com, lizefan@huawei.com, wangnan0@huawei.com, hpa@zytor.com, a.p.zijlstra@chello.nl In-Reply-To: <1435716878-189507-7-git-send-email-wangnan0@huawei.com> References: <1435716878-189507-7-git-send-email-wangnan0@huawei.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] bpf tools: Read eBPF object from buffer Git-Commit-ID: 6c956392b0486c170e382476ab9dd97a696fd169 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5468 Lines: 180 Commit-ID: 6c956392b0486c170e382476ab9dd97a696fd169 Gitweb: http://git.kernel.org/tip/6c956392b0486c170e382476ab9dd97a696fd169 Author: Wang Nan AuthorDate: Wed, 1 Jul 2015 02:13:54 +0000 Committer: Arnaldo Carvalho de Melo CommitDate: Fri, 7 Aug 2015 10:16:56 -0300 bpf tools: Read eBPF object from buffer To support dynamic compiling, this patch allows caller to pass a in-memory buffer to libbpf by bpf_object__open_buffer(). libbpf calls elf_memory() to open it as ELF object file. Because __bpf_object__open() collects all required data and won't need that buffer anymore, libbpf uses that buffer directly instead of clone a new buffer. Caller of libbpf can free that buffer or use it do other things after bpf_object__open_buffer() return. Signed-off-by: Wang Nan Acked-by: 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: Peter Zijlstra Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1435716878-189507-7-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/lib/bpf/libbpf.c | 62 ++++++++++++++++++++++++++++++++++++++++---------- tools/lib/bpf/libbpf.h | 2 ++ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 9e44608..36dfbc1 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -84,6 +84,8 @@ struct bpf_object { */ struct { int fd; + void *obj_buf; + size_t obj_buf_sz; Elf *elf; GElf_Ehdr ehdr; } efile; @@ -91,7 +93,9 @@ struct bpf_object { }; #define obj_elf_valid(o) ((o)->efile.elf) -static struct bpf_object *bpf_object__new(const char *path) +static struct bpf_object *bpf_object__new(const char *path, + void *obj_buf, + size_t obj_buf_sz) { struct bpf_object *obj; @@ -103,6 +107,16 @@ static struct bpf_object *bpf_object__new(const char *path) strcpy(obj->path, path); obj->efile.fd = -1; + + /* + * Caller of this function should also calls + * bpf_object__elf_finish() after data collection to return + * obj_buf to user. If not, we should duplicate the buffer to + * avoid user freeing them before elf finish. + */ + obj->efile.obj_buf = obj_buf; + obj->efile.obj_buf_sz = obj_buf_sz; + return obj; } @@ -116,6 +130,8 @@ static void bpf_object__elf_finish(struct bpf_object *obj) obj->efile.elf = NULL; } zclose(obj->efile.fd); + obj->efile.obj_buf = NULL; + obj->efile.obj_buf_sz = 0; } static int bpf_object__elf_init(struct bpf_object *obj) @@ -128,16 +144,26 @@ static int bpf_object__elf_init(struct bpf_object *obj) return -EEXIST; } - obj->efile.fd = open(obj->path, O_RDONLY); - if (obj->efile.fd < 0) { - pr_warning("failed to open %s: %s\n", obj->path, - strerror(errno)); - return -errno; + if (obj->efile.obj_buf_sz > 0) { + /* + * obj_buf should have been validated by + * bpf_object__open_buffer(). + */ + obj->efile.elf = elf_memory(obj->efile.obj_buf, + obj->efile.obj_buf_sz); + } else { + obj->efile.fd = open(obj->path, O_RDONLY); + if (obj->efile.fd < 0) { + pr_warning("failed to open %s: %s\n", obj->path, + strerror(errno)); + return -errno; + } + + obj->efile.elf = elf_begin(obj->efile.fd, + LIBBPF_ELF_C_READ_MMAP, + NULL); } - obj->efile.elf = elf_begin(obj->efile.fd, - LIBBPF_ELF_C_READ_MMAP, - NULL); if (!obj->efile.elf) { pr_warning("failed to open %s as ELF file\n", obj->path); @@ -167,7 +193,7 @@ errout: } static struct bpf_object * -__bpf_object__open(const char *path) +__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz) { struct bpf_object *obj; @@ -176,7 +202,7 @@ __bpf_object__open(const char *path) return NULL; } - obj = bpf_object__new(path); + obj = bpf_object__new(path, obj_buf, obj_buf_sz); if (!obj) return NULL; @@ -198,7 +224,19 @@ struct bpf_object *bpf_object__open(const char *path) pr_debug("loading %s\n", path); - return __bpf_object__open(path); + return __bpf_object__open(path, NULL, 0); +} + +struct bpf_object *bpf_object__open_buffer(void *obj_buf, + size_t obj_buf_sz) +{ + /* param validation */ + if (!obj_buf || obj_buf_sz <= 0) + return NULL; + + pr_debug("loading object from buffer\n"); + + return __bpf_object__open("[buffer]", obj_buf, obj_buf_sz); } void bpf_object__close(struct bpf_object *obj) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index ec3301c..dc966dd 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -26,6 +26,8 @@ void libbpf_set_print(libbpf_print_fn_t warn, struct bpf_object; struct bpf_object *bpf_object__open(const char *path); +struct bpf_object *bpf_object__open_buffer(void *obj_buf, + size_t obj_buf_sz); void bpf_object__close(struct bpf_object *object); #endif -- 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/