[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <1432704004-171454-20-git-send-email-wangnan0@huawei.com>
Date: Wed, 27 May 2015 05:19:54 +0000
From: Wang Nan <wangnan0@...wei.com>
To: <paulus@...ba.org>, <a.p.zijlstra@...llo.nl>, <mingo@...hat.com>,
<acme@...nel.org>, <namhyung@...nel.org>, <jolsa@...nel.org>,
<dsahern@...il.com>, <daniel@...earbox.net>,
<brendan.d.gregg@...il.com>, <masami.hiramatsu.pt@...achi.com>
CC: <lizefan@...wei.com>, <linux-kernel@...r.kernel.org>,
<pi3orama@....com>
Subject: [RFC PATCH v4 19/29] bpf tools: Load eBPF programs in object files into kernel
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 <wangnan0@...wei.com>
---
tools/lib/bpf/libbpf.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 75 insertions(+)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index f2071ae..02fc880 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -99,6 +99,8 @@ struct bpf_program {
int map_idx;
} *reloc_desc;
int nr_reloc;
+
+ int fd;
};
struct bpf_object {
@@ -130,11 +132,20 @@ struct bpf_object {
};
#define obj_elf_valid(o) ((o)->efile.fd >= 0)
+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);
@@ -194,6 +205,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:
@@ -672,6 +684,64 @@ 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;
+
+ 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:
+ 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) {
@@ -731,6 +801,9 @@ int bpf_object__unload(struct bpf_object *obj)
zclose(obj->maps_fds[i]);
zfree(&obj->maps_fds);
+ for (i = 0; i < obj->nr_programs; i++)
+ bpf_program__unload(&obj->programs[i]);
+
return 0;
}
@@ -743,6 +816,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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists