All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Andrii Nakryiko <andrii@kernel.org>, bpf <bpf@vger.kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Kernel Team <kernel-team@fb.com>,
	Alan Maguire <alan.maguire@oracle.com>,
	Dave Marchevsky <davemarchevsky@fb.com>
Subject: Re: program local storage. Was: [PATCH bpf-next 1/7] libbpf: add BPF-side of USDT support
Date: Fri, 1 Apr 2022 09:56:58 -0700	[thread overview]
Message-ID: <CAEf4BzYHLohBniijHJFS3FA8Ety0LYPuh8r5THSpdiWyCr2MgA@mail.gmail.com> (raw)
In-Reply-To: <CAADnVQKuxEj63pGfHgB04n2BBNja+2NuRJXMjZrvx-4SB8ZX8A@mail.gmail.com>

On Thu, Mar 31, 2022 at 5:38 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Thu, Mar 31, 2022 at 1:13 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> >
> > On Thu, Mar 31, 2022 at 11:34 AM Alexei Starovoitov
> > <alexei.starovoitov@gmail.com> wrote:
> > >
> > > On Thu, Mar 24, 2022 at 10:30 PM Andrii Nakryiko <andrii@kernel.org> wrote:
> > > > +
> > > > +struct __bpf_usdt_arg_spec {
> > > > +       __u64 val_off;
> > > > +       enum __bpf_usdt_arg_type arg_type;
> > > > +       short reg_off;
> > > > +       bool arg_signed;
> > > > +       char arg_bitshift;
> > > > +};
> > > > +
> > > > +/* should match USDT_MAX_ARG_CNT in usdt.c exactly */
> > > > +#define BPF_USDT_MAX_ARG_CNT 12
> > > > +struct __bpf_usdt_spec {
> > > > +       struct __bpf_usdt_arg_spec args[BPF_USDT_MAX_ARG_CNT];
> > > > +       __u64 usdt_cookie;
> > > > +       short arg_cnt;
> > > > +};
> > > > +
> > > > +__weak struct {
> > > > +       __uint(type, BPF_MAP_TYPE_ARRAY);
> > > > +       __uint(max_entries, BPF_USDT_MAX_SPEC_CNT);
> > > > +       __type(key, int);
> > > > +       __type(value, struct __bpf_usdt_spec);
> > > > +} __bpf_usdt_specs SEC(".maps");
> > > > +
> > > > +__weak struct {
> > > > +       __uint(type, BPF_MAP_TYPE_HASH);
> > > > +       __uint(max_entries, BPF_USDT_MAX_IP_CNT);
> > > > +       __type(key, long);
> > > > +       __type(value, struct __bpf_usdt_spec);
> > > > +} __bpf_usdt_specs_ip_to_id SEC(".maps");
> > > ...
> > >
> > > > +
> > > > +/* Fetch USDT argument *arg* (zero-indexed) and put its value into *res.
> > > > + * Returns 0 on success; negative error, otherwise.
> > > > + * On error *res is guaranteed to be set to zero.
> > > > + */
> > > > +__hidden __weak
> > > > +int bpf_usdt_arg(struct pt_regs *ctx, int arg, long *res)
> > > > +{
> > > > +       struct __bpf_usdt_spec *spec;
> > > > +       struct __bpf_usdt_arg_spec *arg_spec;
> > > > +       unsigned long val;
> > > > +       int err, spec_id;
> > > > +
> > > > +       *res = 0;
> > > > +
> > > > +       spec_id = __bpf_usdt_spec_id(ctx);
> > > > +       if (spec_id < 0)
> > > > +               return -ESRCH;
> > > > +
> > > > +       spec = bpf_map_lookup_elem(&__bpf_usdt_specs, &spec_id);
> > > > +       if (!spec)
> > > > +               return -ESRCH;
> > > > +
> > > > +       if (arg >= spec->arg_cnt)
> > > > +               return -ENOENT;
> > > > +
> > > > +       arg_spec = &spec->args[arg];
> > > > +       switch (arg_spec->arg_type) {
> > >
> > > Without bpf_cookie in the kernel each arg access is two lookups.
> > > With bpf_cookie it's a single lookup in an array that is fast.
> > > Multiply that cost by number of args.
> > > Not a huge cost, but we can do better long term.
> > >
> > > How about annotating bpf_cookie with PTR_TO_BTF_ID at prog load time.
> > > So that bpf_get_attach_cookie() returns PTR_TO_BTF_ID instead of long.
> > > This way bpf_get_attach_cookie() can return
> > > "struct __bpf_usdt_spec *".
> > >
> > > At attach time libbpf will provide populated 'struct __bpf_usdt_spec'
> > > to the kernel and the kernel will copy the struct's data
> > > in the bpf_link.
> > > At detach time that memory is freed.
> > >
> > > Advantages:
> > > - saves an array lookup at runtime
> > > - no need to provide size for __bpf_usdt_specs map.
> > >   That map is no longer needed.
> > >   users don't need to worry about maxing out BPF_USDT_MAX_SPEC_CNT.
> > > - libbpf doesn't need to populate __bpf_usdt_specs map
> > >   libbpf doesn't need to allocate spec_id-s.
> > >   libbpf will keep struct __bpf_usdt_spec per uprobe and
> > >   pass it to the kernel at attach time to store in bpf_link.
> > >
> > > "cookie as ptr_to_btf_id" is a generic mechanism to provide a
> > > blob of data to the bpf prog instead of a single "long".
> > > That blob can be read/write too.
> > > It can be used as per-program + per-attach point scratch area.
> > > Similar to task/inode local storage...
> > > That would be (prog, attach_point) local storage.
> > >
> > > Thoughts?
> >
> > Well, I'm not concerned about ARRAY lookup, as it is inlined and very
> > fast. Sizing maps is hard and annoying, true, but I think we should
> > eventually just have resizable or dynamically-sized BPF maps, which
> > will be useful in a lot of other contexts.
>
> Yes. dynamically sized bpf maps would be great.
> That's orthogonal.
>
> > We've had a discussion about a cookie that's bigger than 8 bytes with
> > Daniel. I argued for simplicity and I still like it. If you think we
> > should add blobs per attachment, it's fine, but let's keep it separate
> > from the BPF cookie.
>
> Well, Daniel was right.
> This USDT work is first real use of bpf_cookie and
> it clearly demonstrates that bpf_cookie alone as 8-byte long
> is not enough. The bpf progs have to do map lookup.
> I bet the majority of bpf_cookie use cases will include map lookup.

Well, yeah, absolutely, just how I argued when adding the BPF cookie.
Map lookup was the idea from the very beginning. It was clear that
only for most trivial use cases having u64 by itself would be enough,
it was always the goal to use u64 as a look up key into whatever
additional map (including hashmap) or global var array necessary.

> In the case of USDT we were able to get away with array lookup
> which is cheap, but we won't be that lucky next time.

Retsnoop is the second real use case for BPF cookie and there I just
have a global var array. Works good as well. I think you are
micro-optimizing around map look up in this case. Resizability/sizing
the map is a bigger concern (not even necessarily a real problem) in
practice, not the map lookup overhead.

> Hash lookup will be more costly and dynamically sized map
> won't help the performance consideration.

See above, I personally haven't been concerned with optimizing away
hash map lookup. In my local benchmarking hash lookup costs 14ns vs
4ns or ARRAY map lookup and 3ns global var array lookup. Yes, a bit
slower, but not a huge deal. Given that uprobe activation takes on the
order of 500ns, adding 14ns for single hashmap lookup seem ok to me.

But to be clear, I think it would be great to have this ability to
pre-allocate more storage per attachment. But with BPF cookie I wanted
something simple to use both from BPF program side and set up from
user-space side. And I think this feature provides that.

Whatever we do with variable-sized per-attach storage won't be as
straightforward. So it's worthwhile to consider it, but I still stand
by BPF cookie's approach, which overall is more flexible. E.g., I can
use the same cookie as a key into multiple independent maps, as
necessary. I can utilize all the fancy spinlocks, timers, dynptrs,
kptrs, etc inside map values without any extra verifier machinery and
extra user-space setup. All the building blocks are at my disposal.

But maybe what you want should be a special kind of local storage map
where key (bpf_link, presumably) will be implicitly taken from
bpf_run_ctx. Or something along those lines, so that one can utilize
all the generic map_value features.

>
> It would be ok to keep ptr_to_btf_id separate from cookie only if
> it won't sacrifice performance. The way cookie is shaping up
> as part of bpf_run_ctx gives hope that they can stay separate.

Yep, I think so.

>
> > As for the PTR_TO_BTF_ID, I'm a bit confused, as kernel doesn't know
> > __bpf_usdt_spec type, it's not part of vmlinux BTF, so you are
> > proposing to have PTR_TO_BTF_ID that points to user-provided type?
>
> Yes. It will be pointing to prog's BTF.
>
> > I'm
> > not sure I see how exactly that will work from the verifier's
> > standpoint, tbh. At least I don't see how verifier can allow more than
> > just giving direct memory access to a memory buffer.
>
> It's a longer discussion, but user provided BTF doesn't mean
> that it should be limited to scalars only.
> Such struct can contain pointers too. Not on day one probably.
> kptr and dynptr can be and should be allowed in user's BTFs eventually.

I see. It seems like this will mean that this BTF ID will have to be
provided during program load, not attach then? Assuming we have direct
memory access to the cookie contents.

Alternatively I can see how we can use dynptr to expose this memory to
user-space. Though probably without BTF info. In any case, I agree
it's bigger and separate discussion.

>
> > But then each
> > uprobe attachment can have differently-sized blob, so statically
> > verifying that during program load time is impossible.
>
> In this USDT case the __bpf_usdt_spec is fixed size for all attach points.
> One ptr_to_btf_id as a cookie per program is a minor limitation.
> I don't see a need to support different ptr_to_btf_id-s
> in different attach points.
> USDT use case doesn't need it at least.
>
> > In any case, I don't think we should wait for any extra kernel
> > functionality to add USDT support. If we have some of those and they
> > bring noticeable benefits, we can opportunistically use them, if the
> > kernel is recent enough.
>
> Of course! It's not a blocker for libbpf usdt feature.
> That's why this discussion is a separate thread.

Ok, wasn't sure so wanted to double check. I guess I missed the new
thread factor. As I mentioned above, I think this feature should still
be separate and complementary to BPF cookie (especially that BPF
cookie is UAPI anyways). And there are a bunch of ways we can go about
it, with pros and cons each. Sounds like something that can be
discussed at LSF/MM/BPF?

  reply	other threads:[~2022-04-01 16:57 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-25  5:29 [PATCH bpf-next 0/7] Add libbpf support for USDTs Andrii Nakryiko
2022-03-25  5:29 ` [PATCH bpf-next 1/7] libbpf: add BPF-side of USDT support Andrii Nakryiko
2022-03-30  3:10   ` Hengqi Chen
2022-03-30 15:22     ` Hengqi Chen
2022-03-31  5:44       ` Andrii Nakryiko
2022-03-30 15:36     ` Hengqi Chen
2022-03-31  5:48       ` Andrii Nakryiko
2022-03-31  5:44     ` Andrii Nakryiko
2022-03-31 11:30   ` Alan Maguire
2022-03-31 18:49     ` Andrii Nakryiko
2022-03-31 20:52       ` Andrii Nakryiko
2022-03-31 18:34   ` program local storage. Was: " Alexei Starovoitov
2022-03-31 20:13     ` Andrii Nakryiko
2022-04-01  0:38       ` Alexei Starovoitov
2022-04-01 16:56         ` Andrii Nakryiko [this message]
2022-03-25  5:29 ` [PATCH bpf-next 2/7] libbpf: wire up USDT API and bpf_link integration Andrii Nakryiko
2022-03-30  3:24   ` Hengqi Chen
2022-03-31  5:56     ` Andrii Nakryiko
2022-03-31 12:13   ` Alan Maguire
2022-03-31 19:02     ` Andrii Nakryiko
2022-03-25  5:29 ` [PATCH bpf-next 3/7] libbpf: add USDT notes parsing and resolution logic Andrii Nakryiko
2022-03-31 13:37   ` Alan Maguire
2022-03-31 19:13     ` Andrii Nakryiko
2022-03-25  5:29 ` [PATCH bpf-next 4/7] libbpf: wire up spec management and other arch-independent USDT logic Andrii Nakryiko
2022-03-31 14:49   ` Alan Maguire
2022-03-31 19:16     ` Andrii Nakryiko
2022-03-25  5:29 ` [PATCH bpf-next 5/7] libbpf: add x86-specific USDT arg spec parsing logic Andrii Nakryiko
2022-03-31 15:13   ` Alan Maguire
2022-03-31 19:20     ` Andrii Nakryiko
2022-03-25  5:29 ` [PATCH bpf-next 6/7] selftests/bpf: add basic USDT selftests Andrii Nakryiko
2022-03-31 15:54   ` Alan Maguire
2022-03-31 19:28     ` Andrii Nakryiko
2022-03-25  5:29 ` [PATCH bpf-next 7/7] selftests/bpf: add urandom_read shared lib and USDTs Andrii Nakryiko
2022-03-31 22:13   ` Alan Maguire
2022-04-01 16:59     ` Andrii Nakryiko

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=CAEf4BzYHLohBniijHJFS3FA8Ety0LYPuh8r5THSpdiWyCr2MgA@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=alan.maguire@oracle.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davemarchevsky@fb.com \
    --cc=kernel-team@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.