From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: "Mauricio Vásquez" <mauricio@kinvolk.io>
Cc: Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Quentin Monnet <quentin@isovalent.com>,
Rafael David Tinoco <rafaeldtinoco@gmail.com>,
Lorenzo Fontana <lorenzo.fontana@elastic.co>,
Leonardo Di Donato <leonardo.didonato@elastic.co>
Subject: Re: [PATCH bpf-next v4 1/8] libbpf: split bpf_core_apply_relo()
Date: Fri, 14 Jan 2022 18:01:49 -0800 [thread overview]
Message-ID: <CAEf4BzYSz99GTNiKMaVPMpOc4Y7YdZLEH1VDy2X4KJkaKbtYfA@mail.gmail.com> (raw)
In-Reply-To: <20220112142709.102423-2-mauricio@kinvolk.io>
On Wed, Jan 12, 2022 at 6:27 AM Mauricio Vásquez <mauricio@kinvolk.io> wrote:
>
> BTFGen needs to run the core relocation logic in order to understand
> what are the types in the target BTF that involved in a given
> relocation.
>
> Currently bpf_core_apply_relo() calculates and **applies** a relocation
> to an instruction. Having both operations in the same function makes it
> difficult to only calculate the relocation without patching the
> instruction. This commit splits that logic in two different phases: (1)
> calculate the relocation and (2) patch the instruction.
>
> For the first phase bpf_core_apply_relo() is renamed to
> bpf_core_calc_relo_res() who is now only on charge of calculating the
outdated name?
> relocation, the second phase uses the already existing
> bpf_core_patch_insn(). bpf_object__relocate_core() uses both of them and
> the BTFGen will use only bpf_core_calc_relo_res().
same?
BTW, this patch set breaks CI ([0]), please investigate
[0] https://github.com/kernel-patches/bpf/runs/4797721812?check_suite_focus=true
>
> Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
> Signed-off-by: Rafael David Tinoco <rafael.tinoco@aquasec.com>
> Signed-off-by: Lorenzo Fontana <lorenzo.fontana@elastic.co>
> Signed-off-by: Leonardo Di Donato <leonardo.didonato@elastic.co>
> ---
> kernel/bpf/btf.c | 13 ++++--
> tools/lib/bpf/libbpf.c | 84 ++++++++++++++++++++++++---------------
> tools/lib/bpf/relo_core.c | 79 +++++++++++-------------------------
> tools/lib/bpf/relo_core.h | 42 +++++++++++++++++---
> 4 files changed, 122 insertions(+), 96 deletions(-)
>
[...]
> @@ -5661,12 +5642,53 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
> if (!prog->load)
> continue;
>
> - err = bpf_core_apply_relo(prog, rec, i, obj->btf, cand_cache);
> + if (prog->obj->gen_loader) {
> + const struct btf_type *local_type;
> + const char *local_name, *spec_str;
> +
> + spec_str = btf__name_by_offset(obj->btf, rec->access_str_off);
> + if (!spec_str)
> + return -EINVAL;
> +
> + local_type = btf__type_by_id(obj->btf, rec->type_id);
> + if (!local_type)
> + return -EINVAL;
> +
> + local_name = btf__name_by_offset(obj->btf, local_type->name_off);
> + if (!local_name)
> + return -EINVAL;
> +
> + pr_debug("record_relo_core: prog %td insn[%d] %s %s %s final insn_idx %d\n",
> + prog - prog->obj->programs, insn_idx,
> + btf_kind_str(local_type), local_name, spec_str, insn_idx);
hmm, maybe let's just drop this pr_debug instead? that's a lot of code
and checks just to emit this debug info.
> + return record_relo_core(prog, rec, insn_idx);
> + }
> +
> + if (rec->insn_off % BPF_INSN_SZ)
> + return -EINVAL;
> + insn_idx = rec->insn_off / BPF_INSN_SZ;
> + /* adjust insn_idx from section frame of reference to the local
> + * program's frame of reference; (sub-)program code is not yet
> + * relocated, so it's enough to just subtract in-section offset
> + */
> + insn_idx = insn_idx - prog->sec_insn_off;
> + if (insn_idx >= prog->insns_cnt)
> + return -EINVAL;
> + insn = &prog->insns[insn_idx];
> +
This validation probably is better to do before prog->obj->gen_loader
check so that we don't silently do something bad in record_relo_core()
if insn_idx is wrong? It doesn't change the rest of the logic, right?
So there shouldn't be any harm or change of behavior.
> + err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res);
> if (err) {
> pr_warn("prog '%s': relo #%d: failed to relocate: %d\n",
> prog->name, i, err);
> goto out;
> }
> +
> + err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res);
> + if (err) {
> + pr_warn("prog '%s': relo #%d: failed to patch insn #%u: %d\n",
> + prog->name, i, insn_idx, err);
> + goto out;
> + }
> }
> }
>
[...]
next prev parent reply other threads:[~2022-01-15 2:02 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-12 14:27 [PATCH bpf-next v4 0/8] libbpf: Implement BTFGen Mauricio Vásquez
2022-01-12 14:27 ` [PATCH bpf-next v4 1/8] libbpf: split bpf_core_apply_relo() Mauricio Vásquez
2022-01-15 2:01 ` Andrii Nakryiko [this message]
2022-01-21 20:40 ` Mauricio Vásquez Bernal
2022-01-12 14:27 ` [PATCH bpf-next v4 2/8] libbpf: Implement changes needed for BTFGen in bpftool Mauricio Vásquez
2022-01-12 18:08 ` Quentin Monnet
2022-01-21 20:44 ` Mauricio Vásquez Bernal
2022-01-15 2:04 ` Andrii Nakryiko
2022-01-12 14:27 ` [PATCH bpf-next v4 3/8] bpftool: Add gen btf command Mauricio Vásquez
2022-01-12 18:08 ` Quentin Monnet
2022-01-21 20:40 ` Mauricio Vásquez Bernal
2022-01-21 21:35 ` Andrii Nakryiko
2022-01-15 2:09 ` Andrii Nakryiko
2022-01-21 20:44 ` Mauricio Vásquez Bernal
2022-01-12 14:27 ` [PATCH bpf-next v4 4/8] bpftool: Implement btf_save_raw() Mauricio Vásquez
2022-01-15 2:10 ` Andrii Nakryiko
2022-01-21 20:45 ` Mauricio Vásquez Bernal
2022-01-12 14:27 ` [PATCH bpf-next v4 5/8] bpftool: Add struct definitions and helpers for BTFGen Mauricio Vásquez
2022-01-12 14:27 ` [PATCH bpf-next v4 6/8] bpftool: Implement btfgen() Mauricio Vásquez
2022-01-12 18:09 ` Quentin Monnet
2022-01-15 2:15 ` Andrii Nakryiko
2022-01-12 14:27 ` [PATCH bpf-next v4 7/8] bpftool: Implement relocations recording for BTFGen Mauricio Vásquez
2022-01-12 18:09 ` Quentin Monnet
2022-01-12 14:27 ` [PATCH bpf-next v4 8/8] bpftool: Implement btfgen_get_btf() Mauricio Vásquez
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=CAEf4BzYSz99GTNiKMaVPMpOc4Y7YdZLEH1VDy2X4KJkaKbtYfA@mail.gmail.com \
--to=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=leonardo.didonato@elastic.co \
--cc=lorenzo.fontana@elastic.co \
--cc=mauricio@kinvolk.io \
--cc=netdev@vger.kernel.org \
--cc=quentin@isovalent.com \
--cc=rafaeldtinoco@gmail.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).