Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932798AbbHHIP2 (ORCPT ); Sat, 8 Aug 2015 04:15:28 -0400 Received: from terminus.zytor.com ([198.137.202.10]:53825 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753945AbbHHIPH (ORCPT ); Sat, 8 Aug 2015 04:15:07 -0400 Date: Sat, 8 Aug 2015 01:14:27 -0700 From: tip-bot for Wang Nan Message-ID: Cc: jolsa@kernel.org, mingo@kernel.org, acme@redhat.com, namhyung@kernel.org, hekuang@huawei.com, hpa@zytor.com, xiakaixu@huawei.com, a.p.zijlstra@chello.nl, wangnan0@huawei.com, brendan.d.gregg@gmail.com, dsahern@gmail.com, tglx@linutronix.de, lizefan@huawei.com, masami.hiramatsu.pt@hitachi.com, ast@plumgrid.com, daniel@iogearbox.net, linux-kernel@vger.kernel.org Reply-To: daniel@iogearbox.net, linux-kernel@vger.kernel.org, ast@plumgrid.com, masami.hiramatsu.pt@hitachi.com, tglx@linutronix.de, lizefan@huawei.com, brendan.d.gregg@gmail.com, wangnan0@huawei.com, xiakaixu@huawei.com, a.p.zijlstra@chello.nl, dsahern@gmail.com, hpa@zytor.com, hekuang@huawei.com, namhyung@kernel.org, acme@redhat.com, jolsa@kernel.org, mingo@kernel.org In-Reply-To: <1435716878-189507-18-git-send-email-wangnan0@huawei.com> References: <1435716878-189507-18-git-send-email-wangnan0@huawei.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] bpf tools: Relocate eBPF programs Git-Commit-ID: 8a47a6c522c0593a977069b0b1e5a0725ca0e32e X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3977 Lines: 131 Commit-ID: 8a47a6c522c0593a977069b0b1e5a0725ca0e32e Gitweb: http://git.kernel.org/tip/8a47a6c522c0593a977069b0b1e5a0725ca0e32e Author: Wang Nan AuthorDate: Wed, 1 Jul 2015 02:14:05 +0000 Committer: Arnaldo Carvalho de Melo CommitDate: Fri, 7 Aug 2015 10:16:58 -0300 bpf tools: Relocate eBPF programs 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, ... 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 patches: 'bpf tools: Collect symbol table from SHT_SYMTAB section', 'bpf tools: Collect relocation sections from SHT_REL sections' and 'bpf tools: Record map accessing instructions for each program'. 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. This is the final part of map relocation patch. The principle of map relocation is described in commit message of 'bpf tools: Collect symbol table from SHT_SYMTAB section'. Signed-off-by: Wang Nan Acked-by: Alexei Starovoitov Cc: Brendan Gregg Cc: Daniel Borkmann Cc: David Ahern Cc: He Kuang Cc: Jiri Olsa Cc: Kaixu Xia Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1435716878-189507-18-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- 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 54b48de..94f9660 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -630,6 +630,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; @@ -761,6 +811,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: -- 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/