[RFC PATCH 18/22] perf bpf: load eBPF programs into kernel.

From: Wang Nan
Date: Thu Apr 30 2015 - 06:58:38 EST


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 <wangnan0@xxxxxxxxxx>
---
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@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/