All of lore.kernel.org
 help / color / mirror / Atom feed
From: Song Liu <songliubraving@fb.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: "bpf@vger.kernel.org" <bpf@vger.kernel.org>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Martin Lau" <kafai@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" <netdev@vger.kernel.org>
Subject: Re: [PATCH bpf-next v2 2/8] libbpf: Add typeless ksym support to gen_loader
Date: Thu, 14 Oct 2021 16:39:43 +0000	[thread overview]
Message-ID: <F745CD84-E520-4DCB-B9F1-0C4F0014CBFF@fb.com> (raw)
In-Reply-To: <20211013073348.1611155-3-memxor@gmail.com>



> On Oct 13, 2021, at 12:33 AM, Kumar Kartikeya Dwivedi <memxor@gmail.com> wrote:
> 
> This uses the bpf_kallsyms_lookup_name helper added in previous patches
> to relocate typeless ksyms. The return value ENOENT can be ignored, and
> the value written to 'res' can be directly stored to the insn, as it is
> overwritten to 0 on lookup failure. For repeating symbols, we can simply
> copy the previously populated bpf_insn.
> 
> Also, we need to take care to not close fds for typeless ksym_desc, so
> reuse the 'off' member's space to add a marker for typeless ksym and use
> that to skip them in cleanup_relos.
> 
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
[...]
> }
> 
> +/* Expects:
> + * BPF_REG_8 - pointer to instruction
> + */
> +static void emit_relo_ksym_typeless(struct bpf_gen *gen,
> +				    struct ksym_relo_desc *relo, int insn)

This function has quite some duplicated logic as emit_relo_ksym_btf().
I guess we can somehow reuse the code here. Say, we pull changes from
3/8 first to handle weak type. Then we extend the function to handle 
typeless. Would this work? 

> +{
> +	struct ksym_desc *kdesc;
> +
> +	kdesc = get_ksym_desc(gen, relo);
> +	if (!kdesc)
> +		return;
> +	/* try to copy from existing ldimm64 insn */
> +	if (kdesc->ref > 1) {
> +		move_blob2blob(gen, insn + offsetof(struct bpf_insn, imm), 4,
> +			       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;
> +	}
> +	/* remember insn offset, so we can copy ksym addr later */
> +	kdesc->insn = insn;
> +	/* skip typeless ksym_desc in fd closing loop in cleanup_relos */
> +	kdesc->typeless = true;
> +	emit_bpf_kallsyms_lookup_name(gen, relo);
> +	emit(gen, BPF_JMP_IMM(BPF_JEQ, BPF_REG_7, -ENOENT, 1));
> +	emit_check_err(gen);
> +	/* store lower half of addr into insn[insn_idx].imm */
> +	emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_9, offsetof(struct bpf_insn, imm)));
> +	/* store upper half of addr into insn[insn_idx + 1].imm */
> +	emit(gen, BPF_ALU64_IMM(BPF_RSH, BPF_REG_9, 32));
> +	emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_9,
> +		      sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm)));
> +log:
> +	if (!gen->log_level)
> +		return;
> +	emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_8,
> +			      offsetof(struct bpf_insn, imm)));
> +	emit(gen, BPF_LDX_MEM(BPF_H, BPF_REG_9, BPF_REG_8, sizeof(struct bpf_insn) +
> +			      offsetof(struct bpf_insn, imm)));
> +	debug_regs(gen, BPF_REG_7, BPF_REG_9, " var t=0 w=%d (%s:count=%d): imm[0]: %%d, imm[1]: %%d",
> +		   relo->is_weak, relo->name, kdesc->ref);
> +	emit(gen, BPF_LDX_MEM(BPF_B, BPF_REG_9, BPF_REG_8, offsetofend(struct bpf_insn, code)));
> +	debug_regs(gen, BPF_REG_9, -1, " var t=0 w=%d (%s:count=%d): insn.reg",
> +		   relo->is_weak, relo->name, kdesc->ref);
> 
[...]

> +++ b/tools/lib/bpf/libbpf.c
> @@ -6355,17 +6355,14 @@ static int bpf_program__record_externs(struct bpf_program *prog)
> 		case RELO_EXTERN_VAR:
> 			if (ext->type != EXT_KSYM)
> 				continue;
> -			if (!ext->ksym.type_id) {
> -				pr_warn("typeless ksym %s is not supported yet\n",
> -					ext->name);
> -				return -ENOTSUP;
> -			}
> -			bpf_gen__record_extern(obj->gen_loader, ext->name, ext->is_weak,
> +			bpf_gen__record_extern(obj->gen_loader, ext->name,
> +					       ext->is_weak, !ext->ksym.type_id,
> 					       BTF_KIND_VAR, relo->insn_idx);
> 			break;
> 		case RELO_EXTERN_FUNC:
> -			bpf_gen__record_extern(obj->gen_loader, ext->name, ext->is_weak,
> -					       BTF_KIND_FUNC, relo->insn_idx);
> +			bpf_gen__record_extern(obj->gen_loader, ext->name,
> +					       ext->is_weak, 0, BTF_KIND_FUNC,

nit: Prefer use "false" for bool arguments. 

> +					       relo->insn_idx);
> 			break;
> 		default:
> 			continue;
> -- 
> 2.33.0
> 


  reply	other threads:[~2021-10-14 16:40 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-13  7:33 [PATCH bpf-next v2 0/8] Typeless/weak ksym for gen_loader + misc fixups Kumar Kartikeya Dwivedi
2021-10-13  7:33 ` [PATCH bpf-next v2 1/8] bpf: Add bpf_kallsyms_lookup_name helper Kumar Kartikeya Dwivedi
2021-10-13  7:33 ` [PATCH bpf-next v2 2/8] libbpf: Add typeless ksym support to gen_loader Kumar Kartikeya Dwivedi
2021-10-14 16:39   ` Song Liu [this message]
2021-10-14 17:53     ` Kumar Kartikeya Dwivedi
2021-10-14 19:12       ` Song Liu
2021-10-13  7:33 ` [PATCH bpf-next v2 3/8] libbpf: Add weak " Kumar Kartikeya Dwivedi
2021-10-14 16:44   ` Song Liu
2021-10-13  7:33 ` [PATCH bpf-next v2 4/8] libbpf: Ensure that BPF syscall fds are never 0, 1, or 2 Kumar Kartikeya Dwivedi
2021-10-14 16:55   ` Song Liu
2021-10-13  7:33 ` [PATCH bpf-next v2 5/8] libbpf: Use O_CLOEXEC uniformly when opening fds Kumar Kartikeya Dwivedi
2021-10-13  7:33 ` [PATCH bpf-next v2 6/8] selftests/bpf: Add weak/typeless ksym test for light skeleton Kumar Kartikeya Dwivedi
2021-10-13  7:33 ` [PATCH bpf-next v2 7/8] selftests/bpf: Fix fd cleanup in sk_lookup test Kumar Kartikeya Dwivedi
2021-10-13  7:33 ` [PATCH bpf-next v2 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=F745CD84-E520-4DCB-B9F1-0C4F0014CBFF@fb.com \
    --to=songliubraving@fb.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=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.