bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: John Fastabend <john.fastabend@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Lorenz Bauer <lmb@cloudflare.com>
Cc: bpf <bpf@vger.kernel.org>, Andrii Nakryiko <andrii@kernel.org>,
	Florent Revest <revest@chromium.org>
Subject: Re: Portability of bpf_tracing.h
Date: Mon, 24 May 2021 12:30:55 -0700	[thread overview]
Message-ID: <60abfeef59a6c_135f6208b8@john-XPS-13-9370.notmuch> (raw)
In-Reply-To: <CAEf4BzYd4GLOQTJOeK_=yAs7+DPC+R7cxynOmd7ZMvcRFG+8SQ@mail.gmail.com>

Andrii Nakryiko wrote:
> On Mon, May 24, 2021 at 8:05 AM Lorenz Bauer <lmb@cloudflare.com> wrote:
> >
> > Hi Andrii,
> >
> > A user of bpf2go [1] recently ran into the problem of PT_REGS not
> > being defined after including bpf_tracing.h. It turns out this is
> > because we by default compile for bpfel / bpfeb so the logic in the
> > header file doesn't kick in. I originally filed [2] as a quick fix,
> > but looking at the issue some more made me wonder how to fit this into
> > bpf2go better.
> >
> > Basically, the convention of bpf2go is that the compiled BPF is
> > checked into the source code repository to facilitate distributing BPF
> > as part of Go packages (as opposed to libbpf tooling which doesn't
> > include generated source). To make this portable, bpf2go by default
> > generates both bpfel and bpfeb variants of the C.
> >
> > However, code using bpf_tracing.h is inherently non-portable since the
> > fields of struct pt_regs differ in name between platforms. It seems
> > like this forces one to compile to each possible __TARGET_ARCH
> > separately. If that is correct, could we extend CO-RE somehow to cover
> > this as well?
> 
> If there are enums/types/fields that we can use to reliably detect the
> platform, then yes, we can have a new set of helpers that would do
> this with CO-RE. Someone will need to investigate how to do that for
> all the platforms we have. It's all about finding something that's
> already in the kernel and can server as a reliably indicator of a
> target architecture.
> 
> >
> > If that isn't possible, I want to avoid compiling and shipping BPF for
> > each possible __TARGET_ARCH_xxx by default. Instead I would like to
> > achieve:
> > * Code that doesn't use bpf_tracing.h is distributed in bpfel and bpfeb variants
> > * Code that uses bpf_tracing.h has to explicitly opt into the
> > supported platforms via a flag to bpf2go
> >
> > The latter point is because the go tooling has to know the target arch
> > to be able to generate the correct Go wrappers. How would you feel
> > about adding something like the following to bpf_tracing.h:
> 
> Well, obviously I'm not a fan of even more magic #defines. But I think
> we can achieve a similar effect with a more "lazy" approach. I.e., if
> user tries to use PT_REGS_xxx macros but doesn't specify the platform
> -- only then it gets compilation errors. There is stuff in
> bpf_tracing.h that doesn't need pt_regs, so we can't just outright do
> #error unconditinally. But we can do something like this:
> 
> #else /* !bpf_target_defined */
> 
> #define PT_REGS_PARM1(x) _Pragma("GCC error \"blah blah something
> user-facing\"")
> 
> ... and so on for all macros
> 
> #endif
> 
> Thoughts?

I don't use bpf_tracing.h, but...

Can we do this similar to feature discovery and simply have the
user space tell us the platform? I at least do this fairly
frequently so have infra in place on my side for user space to
push down feature flags/fields. One of those could be platform then
we just need helpers,

  get_pt_regs_parm() {
    if (bpf_target_defined == is_x86)
     ...
    else if (bpf_target_defined == is_foo)
     ...
    else {
      hard_load_error()
    }
  }
    
I think we are suggesting the same thing? Then user would need to
have bpf_target_Defined set but that should be OK and the other
conditions should all look like dead code.

> 
> 
> While on the topic, I've noticed that we added BPF_SEQ_PRINTF and
> BPF_SNPRINTF into bpf_tracing.h, which seems like not the best fit
> (definitely not for BPF_SNPRINTF). Florent, can you please help moving
> them into bpf_helpers.h, as it's really more generic functionality
> rather than low-level tracing primitives. I think it was put here
> because we needed ___bpf_narg macros, but I'd rather copy/paste them
> into bpf_helpers.h (they won't change at all) and put
> BPF_SNPRINTF/BPF_SEQ_PRINTF into bpf_helpers.h.
> 
> >
> > --- a/tools/lib/bpf/bpf_tracing.h
> > +++ b/tools/lib/bpf/bpf_tracing.h
> > @@ -25,6 +25,9 @@
> >         #define bpf_target_sparc
> >         #define bpf_target_defined
> >  #else
> > +       #if defined(BPF_REQUIRE_EXPLICIT_TARGET_ARCH)
> > +               #error BPF_REQUIRE_EXPLICIT_TARGET_ARCH set and no
> > target arch defined
> > +       #endif
> >         #undef bpf_target_defined
> >  #endif
> >
> > bpf2go would always define BPF_REQUIRE_EXPLICIT_TARGET_ARCH. If the
> > user included bpf_tracing.h they get this error. They can then add
> > -target amd64, etc. and the tooling then defines __TARGET_ARCH_x86_64
> >
> > 1: https://pkg.go.dev/github.com/cilium/ebpf/cmd/bpf2go
> > 2: https://github.com/cilium/ebpf/issues/305
> >
> > --
> > Lorenz Bauer  |  Systems Engineer
> > 6th Floor, County Hall/The Riverside Building, SE1 7PB, UK
> >
> > www.cloudflare.com



  reply	other threads:[~2021-05-24 19:31 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-24 15:05 Portability of bpf_tracing.h Lorenz Bauer
2021-05-24 17:47 ` Andrii Nakryiko
2021-05-24 19:30   ` John Fastabend [this message]
2021-05-25  0:13     ` Andrii Nakryiko
2021-05-26  9:13   ` Lorenz Bauer
2021-05-26 18:34     ` Andrii Nakryiko
2021-05-28  8:29       ` Lorenz Bauer
2021-05-30  0:51         ` Andrii Nakryiko
2021-06-10 14:09           ` Lorenz Bauer
2021-06-10 18:14             ` Alexei Starovoitov

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=60abfeef59a6c_135f6208b8@john-XPS-13-9370.notmuch \
    --to=john.fastabend@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=lmb@cloudflare.com \
    --cc=revest@chromium.org \
    /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).