bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Jiri Olsa <jolsa@redhat.com>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
	Jiri Olsa <jolsa@kernel.org>, Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	Yonghong Song <yhs@fb.com>, Martin KaFai Lau <kafai@fb.com>,
	David Miller <davem@redhat.com>,
	John Fastabend <john.fastabend@gmail.com>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	Wenbo Zhang <ethercflow@gmail.com>,
	KP Singh <kpsingh@chromium.org>, Andrii Nakryiko <andriin@fb.com>,
	Brendan Gregg <bgregg@netflix.com>,
	Florent Revest <revest@chromium.org>,
	Al Viro <viro@zeniv.linux.org.uk>
Subject: Re: [PATCH 7/9] bpf: Compile the BTF id whitelist data in vmlinux
Date: Thu, 14 May 2020 15:46:26 -0700	[thread overview]
Message-ID: <CAEf4BzbZ6TYxVTJx3ij1WXy5AvVQio9Ht=tePO+xQf=JLigoog@mail.gmail.com> (raw)
In-Reply-To: <20200514080515.GH3343750@krava>

On Thu, May 14, 2020 at 1:05 AM Jiri Olsa <jolsa@redhat.com> wrote:
>
> On Wed, May 13, 2020 at 11:29:40AM -0700, Alexei Starovoitov wrote:
>
> SNIP
>
> > > diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh
> > > index d09ab4afbda4..dee91c6bf450 100755
> > > --- a/scripts/link-vmlinux.sh
> > > +++ b/scripts/link-vmlinux.sh
> > > @@ -130,16 +130,26 @@ gen_btf()
> > >     info "BTF" ${2}
> > >     LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1}
> > >
> > > -   # Create ${2} which contains just .BTF section but no symbols. Add
> > > +   # Create object which contains just .BTF section but no symbols. Add
> > >     # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
> > >     # deletes all symbols including __start_BTF and __stop_BTF, which will
> > >     # be redefined in the linker script. Add 2>/dev/null to suppress GNU
> > >     # objcopy warnings: "empty loadable segment detected at ..."
> > >     ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
> > > -           --strip-all ${1} ${2} 2>/dev/null
> > > -   # Change e_type to ET_REL so that it can be used to link final vmlinux.
> > > -   # Unlike GNU ld, lld does not allow an ET_EXEC input.
> > > -   printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none
> > > +           --strip-all ${1} 2>/dev/null
> > > +
> > > +   # Create object that contains just .BTF_whitelist_* sections generated
> > > +   # by bpfwl. Same as BTF section, BTF_whitelist_* data will be part of
> > > +   # the vmlinux image, hence SHF_ALLOC.
> > > +   whitelist=.btf.vmlinux.whitelist
> > > +
> > > +   ${BPFWL} ${1} kernel/bpf/helpers-whitelist > ${whitelist}.c
> > > +   ${CC} -c -o ${whitelist}.o ${whitelist}.c
> > > +   ${OBJCOPY} --only-section=.BTF_whitelist* --set-section-flags .BTF=alloc,readonly \
> > > +                --strip-all ${whitelist}.o 2>/dev/null
> > > +
> > > +   # Link BTF and BTF_whitelist objects together
> > > +   ${LD} -r -o ${2} ${1} ${whitelist}.o
> >
> > Thank you for working on it!
> > Looks great to me overall. In the next rev please drop RFC tag.
> >
> > My only concern is this extra linking step. How many extra seconds does it add?
>
> I did not meassure, but I haven't noticed any noticable delay,
> I'll add meassurements to the next post
>
> >
> > Also in patch 3:
> > +               func = func__find(str);
> > +               if (func)
> > +                       func->id = id;
> > which means that if somebody mistyped the name or that kernel function
> > got renamed there will be no warnings or errors.
> > I think it needs to fail the build instead.
>
> it fails later on, when generating the array:
>
>      if (!func->id) {
>              fprintf(stderr, "FAILED: '%s' function not found in BTF data\n",
>                      func->name);
>              return -1;
>      }
>
> but it can clearly fail before that.. I'll change that

I also means that whitelist can't contain functions that can be
conditionally compiled out, right? I guess we can invent some naming
convention to handle that, e.g: ?some_func will mean it's fine if we
didn't find it?

>
> >
> > If additional linking step takes another 20 seconds it could be a reason
> > to move the search to run-time.
> > We already have that with struct bpf_func_proto->btf_id[].
> > Whitelist could be something similar.
> > I think this mechanism will be reused for unstable helpers and other
> > func->btf_id mappings, so 'bpfwl' name would change eventually.
> > It's not white list specific. It generates a mapping of names to btf_ids.
> > Doing it at build time vs run-time is a trade off and it doesn't have
> > an obvious answer.
>
> I was thinking of putting the names in __init section and generate the BTF
> ids on kernel start, but the build time generation seemed more convenient..
> let's see the linking times with 'real size' whitelist and we can reconsider
>

Being able to record such places where to put BTF ID in code would be
really nice, as Alexei mentioned. There are many potential use cases
where it would be good to have BTF IDs just put into arbitrary
variables/arrays. This would trigger compilation error, if someone
screws up the name, or function is renamed, or if function can be
compiled out under some configuration. E.g., assuming some reasonable
implementation of the macro

static const u32 d_path_whitelist[] = {
    BTF_ID_FUNC(vfs_fallocate),
#ifdef CONFIG_WHATEVER
    BTF_ID_FUNC(do_truncate),
#endif
};

Would be nice and very explicit. Given this is not going to be sorted,
you won't be able to use binary search, but if whitelists are
generally small, it should be fine as is. If not, hashmap could be
built in runtime and would be, probably, faster than binary search for
longer sets of BTF IDs.

I wonder if we can do some assembly magic with generating extra
symbols and/or relocations to achieve this? What do you think? Is it
doable/desirable/better?


> thanks,
> jirka
>

  reply	other threads:[~2020-05-14 22:46 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-06 13:29 [RFCv2 0/9] bpf: Add d_path helper Jiri Olsa
2020-05-06 13:29 ` [PATCH 1/9] " Jiri Olsa
2020-05-14 22:06   ` Andrii Nakryiko
2020-05-15 14:59     ` Jiri Olsa
2020-05-06 13:29 ` [PATCH 2/9] bpf: Add d_path whitelist Jiri Olsa
2020-05-06 13:29 ` [PATCH 3/9] bpf: Add bpfwl tool to construct bpf whitelists Jiri Olsa
2020-05-14 22:20   ` Andrii Nakryiko
2020-05-15 14:58     ` Jiri Olsa
2020-05-06 13:29 ` [PATCH 4/9] bpf: Allow nested BTF object to be refferenced by BTF object + offset Jiri Olsa
2020-05-14 22:32   ` Andrii Nakryiko
2020-05-06 13:29 ` [PATCH 5/9] bpf: Add support to check on BTF id whitelist for d_path helper Jiri Olsa
2020-05-06 13:29 ` [PATCH 6/9] bpf: Compile bpfwl tool at kernel compilation start Jiri Olsa
2020-05-14 22:38   ` Andrii Nakryiko
2020-05-15 14:57     ` Jiri Olsa
2020-05-06 13:29 ` [PATCH 7/9] bpf: Compile the BTF id whitelist data in vmlinux Jiri Olsa
2020-05-13 18:29   ` Alexei Starovoitov
2020-05-14  8:05     ` Jiri Olsa
2020-05-14 22:46       ` Andrii Nakryiko [this message]
2020-05-15 14:57         ` Jiri Olsa
2020-05-28 17:23         ` Jiri Olsa
2020-05-29 20:48           ` Andrii Nakryiko
2020-05-31 15:10             ` Jiri Olsa
2020-06-01 19:06               ` Andrii Nakryiko
2020-06-02  8:16                 ` Jiri Olsa
2020-05-06 13:29 ` [PATCH 8/9] selftests/bpf: Add test for d_path helper Jiri Olsa
2020-05-14 22:48   ` Andrii Nakryiko
2020-05-15 14:57     ` Jiri Olsa
2020-05-06 13:29 ` [PATCH 9/9] selftests/bpf: Add verifier " Jiri Olsa

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='CAEf4BzbZ6TYxVTJx3ij1WXy5AvVQio9Ht=tePO+xQf=JLigoog@mail.gmail.com' \
    --to=andrii.nakryiko@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bgregg@netflix.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@redhat.com \
    --cc=ethercflow@gmail.com \
    --cc=hawk@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=jolsa@redhat.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --cc=netdev@vger.kernel.org \
    --cc=revest@chromium.org \
    --cc=viro@zeniv.linux.org.uk \
    --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).