From: Wang Nan <wangnan0@huawei.com>
To: <ast@plumgrid.com>, <davem@davemloft.net>, <acme@kernel.org>,
<mingo@redhat.com>, <a.p.zijlstra@chello.nl>,
<masami.hiramatsu.pt@hitachi.com>, <jolsa@kernel.org>
Cc: <lizefan@kernel.org>, <linux-kernel@vger.kernel.org>,
<pi3orama@163.com>, <hekuang@huawei.com>
Subject: [RFC PATCH 17/22] perf bpf: relocation programs.
Date: Thu, 30 Apr 2015 10:52:40 +0000 [thread overview]
Message-ID: <1430391165-30267-18-git-send-email-wangnan0@huawei.com> (raw)
In-Reply-To: <1430391165-30267-1-git-send-email-wangnan0@huawei.com>
If an eBPF program access a map, LLVM generates a relocated load
instruction. To enable the usage of that map, relocation must be done
by replacing original instructions by map loading instructions.
This patch do that work by utilizing previous stored relocation section
information in 'struct bpf_obj'.
Signed-off-by: Wang Nan <wangnan0@huawei.com>
---
tools/perf/util/bpf-loader.c | 97 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 97 insertions(+)
diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index 34ccc10..fe623df 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -515,6 +515,17 @@ bpf_find_prog_by_name(struct bpf_obj *obj, const char *name)
return NULL;
}
+static struct bpf_perf_prog *
+bpf_find_prog_by_idx(struct bpf_obj *obj, int idx)
+{
+ struct bpf_perf_prog *prog;
+
+ list_for_each_entry(prog, &obj->progs_list, list)
+ if (prog->idx == idx)
+ return prog;
+ return NULL;
+}
+
/*
* Use config_str to config program. If prog is NULL, find a
* prog based on config_str. config_str should not be NULL.
@@ -746,6 +757,90 @@ bpf_obj_create_maps(struct bpf_obj *obj)
return 0;
}
+static int
+bpf_perf_prog_relocate(struct bpf_obj *obj,
+ GElf_Shdr *shdr,
+ Elf_Data *data,
+ struct bpf_perf_prog *prog)
+{
+ int i, nrels;
+
+ pr_debug("bpf: relocating: %s\n", prog->name);
+ nrels = shdr->sh_size / shdr->sh_entsize;
+
+ for (i = 0; i < nrels; i++) {
+ GElf_Sym sym;
+ GElf_Rel rel;
+ unsigned int insn_idx;
+ struct bpf_insn *insns = prog->insns;
+ size_t map_idx;
+
+ if (!gelf_getrel(data, i, &rel)) {
+ pr_err("bpf: relocation: failed to get %d reloc\n", i);
+ return -EINVAL;
+ }
+
+ insn_idx = rel.r_offset / sizeof(struct bpf_insn);
+ pr_debug("bpf: relocation: insn_idx=%u\n", insn_idx);
+
+ if (!gelf_getsym(obj->elf.symbols,
+ GELF_R_SYM(rel.r_info),
+ &sym)) {
+ pr_err("bpf: relocation: symbol %"PRIx64" not found\n",
+ GELF_R_SYM(rel.r_info));
+ return -EINVAL;
+ }
+
+ if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
+ pr_err("bpf: relocation: invalid relo for "
+ "insns[%d].code 0x%x\n",
+ insn_idx, insns[insn_idx].code);
+ return -EINVAL;
+ }
+
+ map_idx = sym.st_value / sizeof(struct bpf_map_def);
+ if (map_idx >= obj->nr_maps) {
+ pr_err("bpf relocation: map_idx %d large than %d\n",
+ (int)map_idx, (int)obj->nr_maps - 1);
+ return -EINVAL;
+ }
+
+ insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
+ insns[insn_idx].imm = obj->maps_fds[map_idx];
+ }
+ return 0;
+}
+
+static int
+bpf_obj_relocate(struct bpf_obj *obj)
+{
+ int i, err;
+
+ for (i = 0; i < obj->elf.nr_reloc; i++) {
+ GElf_Shdr *shdr = &obj->elf.reloc[i].shdr;
+ Elf_Data *data = obj->elf.reloc[i].data;
+ int idx = shdr->sh_info;
+ struct bpf_perf_prog *prog;
+
+ if (shdr->sh_type != SHT_REL) {
+ pr_err("bpf: internal error\n");
+ return -EINVAL;
+ }
+
+ prog = bpf_find_prog_by_idx(obj, idx);
+ if (!prog) {
+ pr_err("bpf: relocation failed: no %d section\n",
+ idx);
+ return -ENOENT;
+ }
+
+ err = bpf_perf_prog_relocate(obj, shdr, data, prog);
+ if (err)
+ return -EINVAL;
+ }
+ return 0;
+}
+
int bpf__load(const char *path)
{
struct bpf_obj *obj;
@@ -776,6 +871,8 @@ int bpf__load(const char *path)
goto out;
if ((err = bpf_obj_create_maps(obj)))
goto out;
+ if ((err = bpf_obj_relocate(obj)))
+ goto out;
list_add(&obj->list, &bpf_obj_list);
return 0;
--
1.8.3.4
next prev parent reply other threads:[~2015-04-30 10:59 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-30 10:52 [RFC PATCH 00/22] perf tools: introduce 'perf bpf' command to load eBPF programs Wang Nan
2015-04-30 10:52 ` [RFC PATCH 01/22] perf: probe: avoid segfault if passed with '' Wang Nan
2015-05-05 14:09 ` Masami Hiramatsu
2015-05-05 15:26 ` Arnaldo Carvalho de Melo
2015-05-05 16:33 ` Masami Hiramatsu
2015-04-30 10:52 ` [RFC PATCH 02/22] perf: bpf: prepare: add __aligned_u64 to types.h Wang Nan
2015-04-30 10:52 ` [RFC PATCH 03/22] perf: add bpf common operations Wang Nan
2015-04-30 10:52 ` [RFC PATCH 04/22] perf tools: Add new 'perf bpf' command Wang Nan
2015-05-11 6:28 ` Namhyung Kim
2015-04-30 10:52 ` [RFC PATCH 05/22] perf bpf: open eBPF object file and do basic validation Wang Nan
2015-04-30 10:52 ` [RFC PATCH 06/22] perf bpf: check swap according to EHDR Wang Nan
2015-04-30 10:52 ` [RFC PATCH 07/22] perf bpf: iterater over elf sections to collect information Wang Nan
2015-04-30 10:52 ` [RFC PATCH 08/22] perf bpf: collect version and license from ELF Wang Nan
2015-04-30 10:52 ` [RFC PATCH 09/22] perf bpf: collect map definitions Wang Nan
2015-05-11 6:32 ` Namhyung Kim
2015-04-30 10:52 ` [RFC PATCH 10/22] perf bpf: collect config section in object Wang Nan
2015-04-30 10:52 ` [RFC PATCH 11/22] perf bpf: collect symbol table in object files Wang Nan
2015-04-30 10:52 ` [RFC PATCH 12/22] perf bpf: collect bpf programs from " Wang Nan
2015-04-30 10:52 ` [RFC PATCH 13/22] perf bpf: collects relocation sections from object file Wang Nan
2015-04-30 10:52 ` [RFC PATCH 14/22] perf bpf: config eBPF programs based on their names Wang Nan
2015-04-30 10:52 ` [RFC PATCH 15/22] perf bpf: config eBPF programs using config section Wang Nan
2015-04-30 10:52 ` [RFC PATCH 16/22] perf bpf: create maps needed by object file Wang Nan
2015-04-30 10:52 ` Wang Nan [this message]
2015-04-30 10:52 ` [RFC PATCH 18/22] perf bpf: load eBPF programs into kernel Wang Nan
2015-04-30 10:52 ` [RFC PATCH 19/22] perf bpf: dump eBPF program before loading Wang Nan
2015-04-30 10:52 ` [RFC PATCH 20/22] perf bpf: clean elf memory after loading Wang Nan
2015-04-30 10:52 ` [RFC PATCH 21/22] perf bpf: probe at kprobe points Wang Nan
2015-05-05 16:34 ` Masami Hiramatsu
2015-05-06 2:36 ` Wang Nan
2015-04-30 10:52 ` [RFC PATCH 22/22] perf bpf: attaches eBPF program to perf fd Wang Nan
2015-05-01 4:37 ` [RFC PATCH 00/22] perf tools: introduce 'perf bpf' command to load eBPF programs Alexei Starovoitov
2015-05-01 11:06 ` Peter Zijlstra
2015-05-01 11:49 ` Ingo Molnar
2015-05-01 16:56 ` Alexei Starovoitov
2015-05-01 17:06 ` Ingo Molnar
2015-05-05 15:39 ` Arnaldo Carvalho de Melo
2015-05-02 7:19 ` Wang Nan
2015-05-05 3:02 ` Alexei Starovoitov
2015-05-05 4:41 ` Wang Nan
2015-05-05 5:49 ` Alexei Starovoitov
2015-05-05 6:14 ` Wang Nan
2015-05-06 4:46 ` Wang Nan
2015-05-06 4:56 ` Alexei Starovoitov
2015-05-06 5:00 ` Wang Nan
2015-05-01 7:16 ` Ingo Molnar
2015-05-05 21:52 ` Brendan Gregg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1430391165-30267-18-git-send-email-wangnan0@huawei.com \
--to=wangnan0@huawei.com \
--cc=a.p.zijlstra@chello.nl \
--cc=acme@kernel.org \
--cc=ast@plumgrid.com \
--cc=davem@davemloft.net \
--cc=hekuang@huawei.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizefan@kernel.org \
--cc=masami.hiramatsu.pt@hitachi.com \
--cc=mingo@redhat.com \
--cc=pi3orama@163.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®