bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: bpf@vger.kernel.org, "Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Martin KaFai Lau" <kafai@fb.com>,
	"Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
	"Jesper Dangaard Brouer" <brouer@redhat.com>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	netdev@vger.kernel.org
Subject: Re: [PATCH bpf-next v5 3/8] libbpf: Add weak ksym support to gen_loader
Date: Thu, 28 Oct 2021 17:22:44 -0700	[thread overview]
Message-ID: <20211029002244.3m63qmwrmykarvt6@ast-mbp.dhcp.thefacebook.com> (raw)
In-Reply-To: <20211028063501.2239335-4-memxor@gmail.com>

On Thu, Oct 28, 2021 at 12:04:56PM +0530, Kumar Kartikeya Dwivedi wrote:
> This extends existing ksym relocation code to also support relocating
> weak ksyms. Care needs to be taken to zero out the src_reg (currently
> BPF_PSEUOD_BTF_ID, always set for gen_loader by bpf_object__relocate_data)
> when the BTF ID lookup fails at runtime.  This is not a problem for
> libbpf as it only sets ext->is_set when BTF ID lookup succeeds (and only
> proceeds in case of failure if ext->is_weak, leading to src_reg
> remaining as 0 for weak unresolved ksym).
> 
> A pattern similar to emit_relo_kfunc_btf is followed of first storing
> the default values and then jumping over actual stores in case of an
> error. For src_reg adjustment, we also need to perform it when copying
> the populated instruction, so depending on if copied insn[0].imm is 0 or
> not, we decide to jump over the adjustment.
> 
> We cannot reach that point unless the ksym was weak and resolved and
> zeroed out, as the emit_check_err will cause us to jump to cleanup
> label, so we do not need to recheck whether the ksym is weak before
> doing the adjustment after copying BTF ID and BTF FD.
> 
> This is consistent with how libbpf relocates weak ksym. Logging
> statements are added to show the relocation result and aid debugging.
> 
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> ---
>  tools/lib/bpf/gen_loader.c | 35 ++++++++++++++++++++++++++++++++---
>  1 file changed, 32 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c
> index 11172a868180..1c404752e565 100644
> --- a/tools/lib/bpf/gen_loader.c
> +++ b/tools/lib/bpf/gen_loader.c
> @@ -13,6 +13,7 @@
>  #include "hashmap.h"
>  #include "bpf_gen_internal.h"
>  #include "skel_internal.h"
> +#include <asm/byteorder.h>
>  
>  #define MAX_USED_MAPS	64
>  #define MAX_USED_PROGS	32
> @@ -776,12 +777,24 @@ static void emit_relo_ksym_typeless(struct bpf_gen *gen,
>  	emit_ksym_relo_log(gen, relo, kdesc->ref);
>  }
>  
> +static __u32 src_reg_mask(void)
> +{
> +#if defined(__LITTLE_ENDIAN_BITFIELD)
> +	return 0x0f; /* src_reg,dst_reg,... */
> +#elif defined(__BIG_ENDIAN_BITFIELD)
> +	return 0xf0; /* dst_reg,src_reg,... */
> +#else
> +#error "Unsupported bit endianness, cannot proceed"
> +#endif
> +}
> +
>  /* Expects:
>   * BPF_REG_8 - pointer to instruction
>   */
>  static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insn)
>  {
>  	struct ksym_desc *kdesc;
> +	__u32 reg_mask;
>  
>  	kdesc = get_ksym_desc(gen, relo);
>  	if (!kdesc)
> @@ -792,19 +805,35 @@ static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo,
>  			       kdesc->insn + offsetof(struct bpf_insn, imm));
>  		move_blob2blob(gen, insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm), 4,
>  			       kdesc->insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm));
> -		goto log;
> +		emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_9, BPF_REG_8, offsetof(struct bpf_insn, imm)));

Thanks a lot for working on this. I've applied the set.

The above load is redundant, right? BPF_REG_0 already has that value
and could have been used in the JNE below, right?

> +		/* jump over src_reg adjustment if imm is not 0 */
> +		emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_9, 0, 3));
> +		goto clear_src_reg;

Is there a test for this part of the code?
It's only for weak && unresolved && multi referenced ksym, right?
Or bpf_link_fops2 test_ksyms_weak.c fits this category?

>  	}
>  	/* remember insn offset, so we can copy BTF ID and FD later */
>  	kdesc->insn = insn;
>  	emit_bpf_find_by_name_kind(gen, relo);
> -	emit_check_err(gen);
> +	if (!relo->is_weak)
> +		emit_check_err(gen);
> +	/* set default values as 0 */
> +	emit(gen, BPF_ST_MEM(BPF_W, BPF_REG_8, offsetof(struct bpf_insn, imm), 0));
> +	emit(gen, BPF_ST_MEM(BPF_W, BPF_REG_8, sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm), 0));
> +	/* skip success case stores if ret < 0 */
> +	emit(gen, BPF_JMP_IMM(BPF_JSLT, BPF_REG_7, 0, 4));
>  	/* store btf_id into insn[insn_idx].imm */
>  	emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_7, offsetof(struct bpf_insn, imm)));
>  	/* store btf_obj_fd into insn[insn_idx + 1].imm */
>  	emit(gen, BPF_ALU64_IMM(BPF_RSH, BPF_REG_7, 32));
>  	emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_7,
>  			      sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm)));

The double store (first with zeros and then with real values) doesn't look pretty.
I think an extra jump over two stores would have been cleaner.

  reply	other threads:[~2021-10-29  0:22 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-28  6:34 [PATCH bpf-next v5 0/8] Typeless/weak ksym for gen_loader + misc fixups Kumar Kartikeya Dwivedi
2021-10-28  6:34 ` [PATCH bpf-next v5 1/8] bpf: Add bpf_kallsyms_lookup_name helper Kumar Kartikeya Dwivedi
2021-10-28 18:10   ` Andrii Nakryiko
2021-10-28  6:34 ` [PATCH bpf-next v5 2/8] libbpf: Add typeless ksym support to gen_loader Kumar Kartikeya Dwivedi
2021-10-28  6:34 ` [PATCH bpf-next v5 3/8] libbpf: Add weak " Kumar Kartikeya Dwivedi
2021-10-29  0:22   ` Alexei Starovoitov [this message]
2021-10-30 15:12     ` Kumar Kartikeya Dwivedi
2021-10-28  6:34 ` [PATCH bpf-next v5 4/8] libbpf: Ensure that BPF syscall fds are never 0, 1, or 2 Kumar Kartikeya Dwivedi
2021-10-28 18:00   ` Andrii Nakryiko
2021-10-28  6:34 ` [PATCH bpf-next v5 5/8] libbpf: Use O_CLOEXEC uniformly when opening fds Kumar Kartikeya Dwivedi
2021-10-28 18:04   ` Andrii Nakryiko
2021-10-28  6:34 ` [PATCH bpf-next v5 6/8] selftests/bpf: Add weak/typeless ksym test for light skeleton Kumar Kartikeya Dwivedi
2021-10-28 18:06   ` Andrii Nakryiko
2021-10-28  6:35 ` [PATCH bpf-next v5 7/8] selftests/bpf: Fix fd cleanup in sk_lookup test Kumar Kartikeya Dwivedi
2021-10-28  6:35 ` [PATCH bpf-next v5 8/8] selftests/bpf: Fix memory leak in test_ima Kumar Kartikeya Dwivedi

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=20211029002244.3m63qmwrmykarvt6@ast-mbp.dhcp.thefacebook.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=kafai@fb.com \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=toke@redhat.com \
    --cc=yhs@fb.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).