bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Alan Maguire <alan.maguire@oracle.com>,
	andrii@kernel.org, jolsa@kernel.org,  acme@redhat.com,
	quentin@isovalent.com
Cc: mykolal@fb.com, ast@kernel.org, daniel@iogearbox.net,
	martin.lau@linux.dev,  song@kernel.org, yonghong.song@linux.dev,
	john.fastabend@gmail.com,  kpsingh@kernel.org, sdf@google.com,
	haoluo@google.com, houtao1@huawei.com,  bpf@vger.kernel.org,
	masahiroy@kernel.org, mcgrof@kernel.org, nathan@kernel.org
Subject: Re: [PATCH v3 bpf-next 10/11] libbpf,bpf: share BTF relocate-related code with kernel
Date: Tue, 14 May 2024 23:56:06 -0700	[thread overview]
Message-ID: <36ddc5553a7edafbb090bd507cf19b437334007c.camel@gmail.com> (raw)
In-Reply-To: <819d1223-a7e7-4b42-b454-d80422fca32d@oracle.com>

On Tue, 2024-05-14 at 17:14 +0100, Alan Maguire wrote:
> On 11/05/2024 02:46, Eduard Zingerman wrote:
> > On Fri, 2024-05-10 at 11:30 +0100, Alan Maguire wrote:
> > 
> > [...]
> > 
> > > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> > > index 821063660d9f..82bd2a275a12 100644
> > > --- a/kernel/bpf/btf.c
> > > +++ b/kernel/bpf/btf.c
> > > @@ -274,6 +274,7 @@ struct btf {
> > >  	u32 start_str_off; /* first string offset (0 for base BTF) */
> > >  	char name[MODULE_NAME_LEN];
> > >  	bool kernel_btf;
> > > +	__u32 *base_map; /* map from distilled base BTF -> vmlinux BTF ids */
> > >  };
> > >  
> > >  enum verifier_phase {
> > > @@ -1735,7 +1736,13 @@ static void btf_free(struct btf *btf)
> > >  	kvfree(btf->types);
> > >  	kvfree(btf->resolved_sizes);
> > >  	kvfree(btf->resolved_ids);
> > > -	kvfree(btf->data);
> > > +	/* only split BTF allocates data, but btf->data is non-NULL for
> > > +	 * vmlinux BTF too.
> > > +	 */
> > > +	if (btf->base_btf)
> > > +		kvfree(btf->data);
> > 
> > Is this correct?
> > I see that btf->data is assigned in three functions:
> > - btf_parse(): allocated via kvmalloc(), does not set btf->base_btf;
> > - btf_parse_base(): not allocated passed from caller, either vmlinux
> >   or module, does not set btf->base_btf;
> > - btf_parse_module(): allocated via kvmalloc(), does set btf->base_btf;
> > 
> > So, the check above seems incorrect for btf_parse(), am I wrong?
> > 
> 
> You're right, we need to check btf->kernel_btf too to ensure we're
> dealing with vmlinux where the btf->data was assigned to __start_BTF.

Maybe add a flag saying if .data needs freeing?
Tbh, following the callgraph to check when conditions are true or
false is a bit time consuming for someone reading the code.

[...]

> > > +static int btf_rewrite_strs(__u32 *str_off, void *ctx)
> > > +{
> > > +	struct btf_rewrite_strs *r = ctx;
> > > +	const char *s;
> > > +	int off;
> > > +
> > > +	if (!*str_off)
> > > +		return 0;
> > > +	if (*str_off >= r->str_start) {
> > > +		*str_off += r->str_diff;
> > > +	} else {
> > > +		s = btf_str_by_offset(r->old_base_btf, *str_off);
> > > +		if (!s)
> > > +			return -ENOENT;
> > > +		if (r->str_map[*str_off]) {
> > > +			off = r->str_map[*str_off];
> > > +		} else {
> > > +			off = btf_find_str(r->btf->base_btf, s);
> > > +			if (off < 0)
> > > +				return off;
> > > +			r->str_map[*str_off] = off;
> > > +		}
> > 
> > If 'str_map' part would be abstracted as local function 'btf__add_str'
> > it should be possible to move btf_rewrite_strs() and btf_set_base_btf()
> > to btf_common.c, right?
> > 
> 
> We can minimize the non-common code alright, but because struct btf is
> locally declared in btf.c we need a few helper functions. I'd propose we
> add (to both btf.c files):
> 
> struct btf_header *btf_header(const struct btf *btf)
> {
>         return btf->hdr;
> }
> 
> void btf_set_base_btf(struct btf *btf, struct btf *base_btf)
> {
>         btf->base_btf = base_btf;
>         btf->start_id = btf__type_cnt(base_btf);
>         btf->start_str_off = base_btf->hdr->str_len;
> }
> 
> ...and use common code for the rest. As you say, we'll also need a
> btf__add_str() for the kernel side. What do you think?

Sounds good, thank you.

[...]

  reply	other threads:[~2024-05-15  6:56 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-10 10:30 [PATCH v3 bpf-next 00/11] bpf: support resilient split BTF Alan Maguire
2024-05-10 10:30 ` [PATCH v3 bpf-next 01/11] libbpf: add btf__distill_base() creating split BTF with distilled base BTF Alan Maguire
2024-05-10 19:14   ` Eduard Zingerman
2024-05-13 17:23     ` Alan Maguire
2024-05-10 10:30 ` [PATCH v3 bpf-next 02/11] selftests/bpf: test distilled base, split BTF generation Alan Maguire
2024-05-10 10:30 ` [PATCH v3 bpf-next 03/11] libbpf: add btf__parse_opts() API for flexible BTF parsing Alan Maguire
2024-05-11  9:40   ` Eduard Zingerman
2024-05-13 16:25     ` Alan Maguire
2024-05-13 16:59       ` Eduard Zingerman
2024-05-10 10:30 ` [PATCH v3 bpf-next 04/11] bpftool: support displaying raw split BTF using base BTF section as base Alan Maguire
2024-05-13 10:57   ` Quentin Monnet
2024-05-10 10:30 ` [PATCH v3 bpf-next 05/11] resolve_btfids: use .BTF.base ELF section as base BTF if -B option is used Alan Maguire
2024-05-10 10:30 ` [PATCH v3 bpf-next 06/11] kbuild, bpf: add module-specific pahole/resolve_btfids flags for distilled base BTF Alan Maguire
2024-05-10 10:30 ` [PATCH v3 bpf-next 07/11] libbpf: split BTF relocation Alan Maguire
2024-05-10 22:26   ` Eduard Zingerman
2024-05-13 17:51     ` Alan Maguire
2024-05-10 10:30 ` [PATCH v3 bpf-next 08/11] selftests/bpf: extend distilled BTF tests to cover " Alan Maguire
2024-05-10 22:46   ` Eduard Zingerman
2024-05-10 10:30 ` [PATCH v3 bpf-next 09/11] module, bpf: store BTF base pointer in struct module Alan Maguire
2024-05-10 10:30 ` [PATCH v3 bpf-next 10/11] libbpf,bpf: share BTF relocate-related code with kernel Alan Maguire
2024-05-11  1:46   ` Eduard Zingerman
2024-05-14 16:14     ` Alan Maguire
2024-05-15  6:56       ` Eduard Zingerman [this message]
2024-05-10 10:30 ` [PATCH v3 bpf-next 11/11] bpftool: support displaying relocated-with-base split BTF Alan Maguire
2024-05-11  9:32   ` Eduard Zingerman
2024-05-13 11:12   ` Quentin Monnet
2024-05-14 16:33     ` Alan Maguire
2024-05-11  9:28 ` [PATCH v3 bpf-next 00/11] bpf: support resilient " Eduard Zingerman

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=36ddc5553a7edafbb090bd507cf19b437334007c.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=acme@redhat.com \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=houtao1@huawei.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=masahiroy@kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=mykolal@fb.com \
    --cc=nathan@kernel.org \
    --cc=quentin@isovalent.com \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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).