Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1174901

[RFC PATCH v10 17/50] bpf tools: Relocate eBPF programs

From Wang Nan <wangnan0@huawei.com>
Newsgroups linux.kernel
Subject [RFC PATCH v10 17/50] bpf tools: Relocate eBPF programs
Date 2015-07-01 04:20 +0200
Message-ID <pHfN1-3Uy-47@gated-at.bofh.it> (permalink)
References <pHfMZ-3Uy-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


If an eBPF program accesses a map, LLVM generates a load instruction
which loads an absolute address into a register, like this:

  ld_64   r1, <MCOperand Expr:(mymap)>
  ...
  call    2

That ld_64 instruction will be recorded in relocation section.
To enable the usage of that map, relocation must be done by replacing
the immediate value by real map file descriptor so it can be found by
eBPF map functions.

This patch to the relocation work based on information collected by
patch 'bpf tools: Collect relocation sections from SHT_REL sections'.
For each instruction which needs relocation, it inject corresponding
file descriptor to imm field. As a part of protocol, src_reg is set to
BPF_PSEUDO_MAP_FD to notify kernel this is a map loading instruction.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
---
 tools/lib/bpf/libbpf.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index dee6f41..9d42774 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -623,6 +623,56 @@ bpf_object__create_maps(struct bpf_object *obj)
 	return 0;
 }
 
+static int
+bpf_program__relocate(struct bpf_program *prog, int *map_fds)
+{
+	int i;
+
+	if (!prog || !prog->reloc_desc)
+		return 0;
+
+	for (i = 0; i < prog->nr_reloc; i++) {
+		int insn_idx, map_idx;
+		struct bpf_insn *insns = prog->insns;
+
+		insn_idx = prog->reloc_desc[i].insn_idx;
+		map_idx = prog->reloc_desc[i].map_idx;
+
+		if (insn_idx >= (int)prog->insns_cnt) {
+			pr_warning("relocation out of range: '%s'\n",
+				   prog->section_name);
+			return -ERANGE;
+		}
+		insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
+		insns[insn_idx].imm = map_fds[map_idx];
+	}
+
+	zfree(&prog->reloc_desc);
+	prog->nr_reloc = 0;
+	return 0;
+}
+
+
+static int
+bpf_object__relocate(struct bpf_object *obj)
+{
+	struct bpf_program *prog;
+	size_t i;
+	int err;
+
+	for (i = 0; i < obj->nr_programs; i++) {
+		prog = &obj->programs[i];
+
+		err = bpf_program__relocate(prog, obj->map_fds);
+		if (err) {
+			pr_warning("failed to relocate '%s'\n",
+				   prog->section_name);
+			return err;
+		}
+	}
+	return 0;
+}
+
 static int bpf_object__collect_reloc(struct bpf_object *obj)
 {
 	int i, err;
@@ -754,6 +804,8 @@ int bpf_object__load(struct bpf_object *obj)
 	obj->loaded = true;
 	if (bpf_object__create_maps(obj))
 		goto out;
+	if (bpf_object__relocate(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@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[RFC PATCH v10 00/50] perf tools: filtering events using eBPF programs Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 18/50] bpf tools: Introduce bpf_load_program() to bpf.c Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 07/50] bpf tools: Check endianness and make libbpf fail early Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 25/50] perf tools: Call clang to compile C source to object code Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 02/50] tracing, perf: Implement BPF programs attached to uprobes Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 05/50] bpf tools: Open eBPF object file and do basic validation Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 22/50] bpf tools: Link all bpf objects onto a list Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 27/50] perf tools: Auto detecting kernel build directory Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 06/50] bpf tools: Read eBPF object from buffer Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 13/50] bpf tools: Collect relocation sections from SHT_REL sections Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 01/50] bpf: Use correct #ifdef controller for trace_call_bpf() Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 16/50] bpf tools: Create eBPF maps defined in an object file Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  [RFC PATCH v10 17/50] bpf tools: Relocate eBPF programs Wang Nan <wangnan0@huawei.com> - 2015-07-01 04:20 +0200
  Re: [RFC PATCH v10 23/50] perf tools: Make perf depend on libbpf Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-07-08 15:10 +0200
  Re: [RFC PATCH v10 23/50] perf tools: Make perf depend on libbpf Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-07-08 15:10 +0200

csiph-web