linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Wang Nan <wangnan0@huawei.com>
To: <paulus@samba.org>, <a.p.zijlstra@chello.nl>, <mingo@redhat.com>,
	<acme@kernel.org>, <namhyung@kernel.org>, <jolsa@kernel.org>,
	<dsahern@gmail.com>, <ast@plumgrid.com>, <daniel@iogearbox.net>,
	<brendan.d.gregg@gmail.com>, <masami.hiramatsu.pt@hitachi.com>
Cc: <lizefan@huawei.com>, <linux-kernel@vger.kernel.org>, <pi3orama@163.com>
Subject: [RFC PATCH v3 18/37] bpf tools: Record map accessing instructions for each program
Date: Sun, 17 May 2015 10:56:43 +0000	[thread overview]
Message-ID: <1431860222-61636-19-git-send-email-wangnan0@huawei.com> (raw)
In-Reply-To: <1431860222-61636-1-git-send-email-wangnan0@huawei.com>

This patch records the indics of instructions which are needed to be
relocated. Those information are saved in 'reloc_desc' field in
'struct bpf_program'. In loading phase (this patch takes effect in
opening phase), the collected instructions will be replaced by
map loading instructions.

Since we are going to close the ELF file and clear all data at the end
of 'opening' phase, ELF information will no longer be valid in
'loading' phase. We have to locate the instructions before maps are
loaded, instead of directly modifying the instruction.

'struct bpf_map_def' is introduce in this patch to let us know how many
maps defined in the object.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
---
 tools/lib/bpf/libbpf.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h |  10 ++++
 2 files changed, 138 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 8fd29a9..ded96cb 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -10,6 +10,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <inttypes.h>
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -87,6 +88,12 @@ struct bpf_program {
 	char *section_name;
 	struct bpf_insn *insns;
 	size_t insns_cnt;
+
+	struct {
+		int insn_idx;
+		int map_idx;
+	} *reloc_desc;
+	int nr_reloc;
 };
 
 struct bpf_object {
@@ -131,6 +138,12 @@ static void bpf_clear_program(struct bpf_program *prog)
 		free(prog->insns);
 		prog->insns = NULL;
 	}
+	if (prog->reloc_desc) {
+		free(prog->reloc_desc);
+		prog->reloc_desc = NULL;
+	}
+
+	prog->nr_reloc = 0;
 	prog->insns_cnt = 0;
 	prog->idx = -1;
 }
@@ -521,6 +534,119 @@ out:
 	return err;
 }
 
+static struct bpf_program *
+bpf_find_prog_by_idx(struct bpf_object *obj, int idx)
+{
+	struct bpf_program *prog;
+	size_t i;
+
+	for (i = 0; i < obj->nr_programs; i++) {
+		prog = &obj->programs[i];
+		if (prog->idx == idx)
+			return prog;
+	}
+	return NULL;
+}
+
+static int
+bpf_program_collect_reloc(struct bpf_object *obj,
+			  GElf_Shdr *shdr,
+			  Elf_Data *data,
+			  struct bpf_program *prog)
+{
+	int i, nrels;
+	size_t nr_maps = obj->maps_buf_sz / sizeof(struct bpf_map_def);
+
+	pr_debug("collecting relocating info for: '%s'\n",
+			prog->section_name);
+	nrels = shdr->sh_size / shdr->sh_entsize;
+	
+	prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
+	if (!prog->reloc_desc) {
+		pr_warning("failed to alloc memory in relocation\n");
+		return -ENOMEM;
+	}
+	prog->nr_reloc = nrels;
+
+	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_warning("relocation: failed to get "
+				   "%d reloc\n", i);
+			return -EINVAL;
+		}
+
+		insn_idx = rel.r_offset / sizeof(struct bpf_insn);
+		pr_debug("relocation: insn_idx=%u\n", insn_idx);
+
+		if (!gelf_getsym(obj->elf.symbols,
+				 GELF_R_SYM(rel.r_info),
+				 &sym)) {
+			pr_warning("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_warning("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 >= nr_maps) {
+			pr_warning("bpf relocation: map_idx %d large than %d\n",
+				   (int)map_idx, (int)nr_maps - 1);
+			return -EINVAL;
+		}
+
+		prog->reloc_desc[i].insn_idx = insn_idx;
+		prog->reloc_desc[i].map_idx = map_idx;
+	}
+	return 0;
+}
+
+static int bpf_obj_collect_reloc(struct bpf_object *obj)
+{
+	int i, err;
+
+	if (!obj_elf_valid(obj)) {
+		pr_warning("Internal error: elf object is closed\n");
+		return -EINVAL;
+	}
+
+	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_program *prog;
+
+		if (shdr->sh_type != SHT_REL) {
+			pr_warning("internal error at %d\n", __LINE__);
+			return -EINVAL;
+		}
+
+		prog = bpf_find_prog_by_idx(obj, idx);
+		if (!prog) {
+			pr_warning("relocation failed: no %d section\n",
+				   idx);
+			return -ENOENT;
+		}
+
+		err = bpf_program_collect_reloc(obj, shdr, data, prog);
+		if (err)
+			return -EINVAL;
+	}
+	return 0;
+}
+
 static int bpf_obj_validate(struct bpf_object *obj)
 {
 	if (obj->kern_version == 0) {
@@ -556,6 +682,8 @@ struct bpf_object *bpf_open_object(const char *path)
 		goto out;
 	if (bpf_obj_elf_collect(obj))
 		goto out;
+	if (bpf_obj_collect_reloc(obj))
+		goto out;
 	if (bpf_obj_validate(obj))
 		goto out;
 
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index e523ae9..3505be7 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -21,4 +21,14 @@ struct bpf_object;
 struct bpf_object *bpf_open_object(const char *path);
 void bpf_close_object(struct bpf_object *object);
 
+/*
+ * packed attribute is unnecessary for 'bpf_map_def'.
+ */
+struct bpf_map_def {
+	unsigned int type;
+	unsigned int key_size;
+	unsigned int value_size;
+	unsigned int max_entries;
+};
+
 #endif
-- 
1.8.3.4


  parent reply	other threads:[~2015-05-17 11:02 UTC|newest]

Thread overview: 108+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-17 10:56 [RFC PATCH v3 00/37] perf tools: introduce 'perf bpf' command to load eBPF programs Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 01/37] perf/events/core: fix race in bpf program unregister Wang Nan
2015-05-18 16:59   ` Alexei Starovoitov
2015-05-17 10:56 ` [RFC PATCH v3 02/37] perf tools: Set vmlinux_path__nr_entries to 0 in vmlinux_path__exit Wang Nan
2015-05-18 17:01   ` Alexei Starovoitov
2015-05-18 20:28     ` Arnaldo Carvalho de Melo
2015-05-20 12:25   ` [tip:perf/core] " tip-bot for Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 03/37] tools lib traceevent: Install libtraceevent.a into libdir Wang Nan
2015-05-18 14:28   ` Jiri Olsa
2015-05-20 12:26   ` [tip:perf/core] " tip-bot for Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 04/37] tools: Change FEATURE_TESTS and FEATURE_DISPLAY to weak binding Wang Nan
2015-05-18 14:30   ` Jiri Olsa
2015-05-20 12:26   ` [tip:perf/core] tools build: " tip-bot for Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 05/37] tools: Add __aligned_u64 to types.h Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 06/37] bpf tools: Introduce 'bpf' library to tools Wang Nan
2015-05-18 17:35   ` Alexei Starovoitov
2015-05-20  3:48     ` Wangnan (F)
2015-05-20  5:24       ` Alexei Starovoitov
2015-05-21  0:24         ` Wangnan (F)
2015-05-21 17:53           ` Alexei Starovoitov
2015-05-21  7:10     ` Wangnan (F)
2015-05-17 10:56 ` [RFC PATCH v3 07/37] bpf tools: Allow caller to set printing function Wang Nan
2015-05-18 17:55   ` Alexei Starovoitov
2015-05-17 10:56 ` [RFC PATCH v3 08/37] bpf tools: Define basic interface Wang Nan
2015-05-18 17:57   ` Alexei Starovoitov
2015-05-22 17:22     ` Jiri Olsa
2015-05-17 10:56 ` [RFC PATCH v3 09/37] bpf tools: Open eBPF object file and do basic validation Wang Nan
2015-05-18 18:06   ` Alexei Starovoitov
2015-05-22 17:22   ` Jiri Olsa
2015-05-22 17:23   ` Jiri Olsa
2015-05-23  1:00     ` Alexei Starovoitov
2015-05-25 13:30       ` Arnaldo Carvalho de Melo
2015-05-26  0:05         ` Wangnan (F)
2015-05-26  0:41           ` Arnaldo Carvalho de Melo
2015-05-17 10:56 ` [RFC PATCH v3 10/37] bpf tools: Check endianess and set swap flag according to EHDR Wang Nan
2015-05-18 18:19   ` Alexei Starovoitov
2015-05-17 10:56 ` [RFC PATCH v3 11/37] bpf tools: Iterate over ELF sections to collect information Wang Nan
2015-05-18 18:21   ` Alexei Starovoitov
2015-05-17 10:56 ` [RFC PATCH v3 12/37] bpf tools: Collect version and license from ELF sections Wang Nan
2015-05-18 18:27   ` Alexei Starovoitov
2015-05-18 20:34     ` Arnaldo Carvalho de Melo
2015-05-18 20:51       ` Alexei Starovoitov
2015-05-17 10:56 ` [RFC PATCH v3 13/37] bpf tools: Collect map definitions from 'maps' section Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 14/37] bpf tools: Collect config string from 'config' section Wang Nan
2015-05-18 18:30   ` Alexei Starovoitov
2015-05-17 10:56 ` [RFC PATCH v3 15/37] bpf tools: Collect symbol table from SHT_SYMTAB section Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 16/37] bpf tools: Collect eBPF programs from their own sections Wang Nan
2015-05-18 12:29   ` Namhyung Kim
2015-05-18 12:47     ` Wangnan (F)
2015-05-18 18:32       ` Alexei Starovoitov
2015-05-17 10:56 ` [RFC PATCH v3 17/37] bpf tools: Collect relocation sections from SHT_REL sections Wang Nan
2015-05-17 10:56 ` Wang Nan [this message]
2015-05-18 18:34   ` [RFC PATCH v3 18/37] bpf tools: Record map accessing instructions for each program Alexei Starovoitov
2015-05-25  7:39     ` Wangnan (F)
2015-05-17 10:56 ` [RFC PATCH v3 19/37] bpf tools: Clear libelf and ELF parsing resrouce to finish opening Wang Nan
2015-05-18 18:36   ` Alexei Starovoitov
2015-05-18 20:35     ` Arnaldo Carvalho de Melo
2015-05-17 10:56 ` [RFC PATCH v3 20/37] bpf tools: Add bpf.c/h for common bpf operations Wang Nan
2015-05-18 18:39   ` Alexei Starovoitov
2015-05-22 17:24   ` Jiri Olsa
2015-05-17 10:56 ` [RFC PATCH v3 21/37] bpf tools: Create eBPF maps defined in an object file Wang Nan
2015-05-18 18:48   ` Alexei Starovoitov
2015-05-18 20:37     ` Arnaldo Carvalho de Melo
2015-05-25  9:23     ` Wangnan (F)
2015-05-22 17:23   ` Jiri Olsa
2015-05-17 10:56 ` [RFC PATCH v3 22/37] bpf tools: Relocate eBPF programs Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 23/37] bpf tools: Introduce bpf_load_program() to bpf.c Wang Nan
2015-05-18 18:52   ` Alexei Starovoitov
2015-05-18 20:37     ` Arnaldo Carvalho de Melo
2015-05-17 10:56 ` [RFC PATCH v3 24/37] bpf tools: Load eBPF programs in object files into kernel Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 25/37] bpf tools: Introduce accessors for struct bpf_program Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 26/37] bpf tools: Introduce accessors for struct bpf_object Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 27/37] perf tools: Add new 'perf bpf' command Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 28/37] perf tools: Make perf depend on libbpf Wang Nan
2015-05-22 17:24   ` Jiri Olsa
2015-05-17 10:56 ` [RFC PATCH v3 29/37] perf bpf: Add 'perf bpf record' subcommand Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 30/37] perf bpf: Add bpf-loader and open ELF object files Wang Nan
2015-05-22 17:24   ` Jiri Olsa
2015-05-25 11:47     ` Wangnan (F)
2015-05-25 13:00     ` Arnaldo Carvalho de Melo
2015-05-17 10:56 ` [RFC PATCH v3 31/37] perf bpf: Collect all eBPF programs Wang Nan
2015-05-17 10:56 ` [RFC PATCH v3 32/37] perf bpf: Parse probe points of eBPF programs during preparation Wang Nan
2015-05-22 17:24   ` Jiri Olsa
2015-05-17 10:56 ` [RFC PATCH v3 33/37] perf bpf: Probe at kprobe points Wang Nan
2015-05-18 19:25   ` Alexei Starovoitov
2015-05-17 10:56 ` [RFC PATCH v3 34/37] perf bpf: Load all eBPF object into kernel Wang Nan
2015-05-17 10:57 ` [RFC PATCH v3 35/37] perf tools: Add a bpf_wrapper global flag Wang Nan
2015-05-17 10:57 ` [RFC PATCH v3 36/37] perf tools: Add bpf_fd field to evsel and config it Wang Nan
2015-05-17 10:57 ` [RFC PATCH v3 37/37] perf tools: Attach eBPF program to perf event Wang Nan
2015-05-18 19:38 ` [RFC PATCH v3 00/37] perf tools: introduce 'perf bpf' command to load eBPF programs Alexei Starovoitov
2015-05-18 20:44   ` Arnaldo Carvalho de Melo
2015-05-18 21:05     ` Alexei Starovoitov
2015-05-18 21:20       ` Arnaldo Carvalho de Melo
2015-05-18 21:45         ` Alexei Starovoitov
2015-05-19 13:44           ` Arnaldo Carvalho de Melo
2015-05-19 16:04             ` Namhyung Kim
2015-05-19 16:40               ` Arnaldo Carvalho de Melo
2015-05-19 20:46                 ` Alexei Starovoitov
2015-05-19 21:10                   ` Arnaldo Carvalho de Melo
2015-05-19 21:42                     ` Alexei Starovoitov
2015-05-19 22:05                       ` Arnaldo Carvalho de Melo
2015-05-20  1:02                         ` Alexei Starovoitov
2015-05-20  1:14                           ` Arnaldo Carvalho de Melo
2015-05-20  0:30                     ` Namhyung Kim
2015-05-20  0:43                       ` Arnaldo Carvalho de Melo
2015-05-26  6:22                   ` Wangnan (F)
2015-05-20  0:23                 ` Namhyung Kim
2015-05-20  0:37                   ` Arnaldo Carvalho de Melo

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=1431860222-61636-19-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=brendan.d.gregg@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=dsahern@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan@huawei.com \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=paulus@samba.org \
    --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;
as well as URLs for NNTP newsgroup(s).