All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tony Ambardar <tony.ambardar@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	bpf@vger.kernel.org, linux-kselftest@vger.kernel.org,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>, Mykola Lysenko <mykolal@fb.com>,
	Shuah Khan <shuah@kernel.org>,
	Ilya Leoshkevich <iii@linux.ibm.com>,
	Quentin Monnet <qmo@kernel.org>
Subject: Re: [PATCH bpf-next v4 7/8] libbpf: Support creating light skeleton of either endianness
Date: Sat, 31 Aug 2024 23:00:39 -0700	[thread overview]
Message-ID: <ZtQDB8NICr3khymH@kodidev-ubuntu> (raw)
In-Reply-To: <CAEf4Bzb++vMkc=Q44QhQ5BXJcnUWahVtBUgQBUwkdVvC1QYKnQ@mail.gmail.com>

On Fri, Aug 30, 2024 at 02:30:46PM -0700, Andrii Nakryiko wrote:
> On Fri, Aug 30, 2024 at 12:30 AM Tony Ambardar <tony.ambardar@gmail.com> wrote:
> >
> > Track target endianness in 'struct bpf_gen' and process in-memory data in
> > native byte-order, but on finalization convert the embedded loader BPF
> > insns to target endianness.
> >
> > The light skeleton also includes a target-accessed data blob which is
> > heterogeneous and thus difficult to convert to target byte-order on
> > finalization. Add support functions to convert data to target endianness
> > as it is added to the blob.
> >
> > Also add additional debug logging for data blob structure details and
> > skeleton loading.
> >
> > Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com>
> > ---
> >  tools/lib/bpf/bpf_gen_internal.h |   1 +
> >  tools/lib/bpf/gen_loader.c       | 187 +++++++++++++++++++++++--------
> >  tools/lib/bpf/libbpf.c           |   1 +
> >  tools/lib/bpf/skel_internal.h    |   3 +-
> >  4 files changed, 147 insertions(+), 45 deletions(-)
> >
> > diff --git a/tools/lib/bpf/bpf_gen_internal.h b/tools/lib/bpf/bpf_gen_internal.h
> > index fdf44403ff36..6ff963a491d9 100644
> > --- a/tools/lib/bpf/bpf_gen_internal.h
> > +++ b/tools/lib/bpf/bpf_gen_internal.h
> > @@ -34,6 +34,7 @@ struct bpf_gen {
> >         void *data_cur;
> >         void *insn_start;
> >         void *insn_cur;
> > +       bool swapped_endian;
> >         ssize_t cleanup_label;
> >         __u32 nr_progs;
> >         __u32 nr_maps;
> > diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c
> > index cf3323fd47b8..4374399bc3f8 100644
> > --- a/tools/lib/bpf/gen_loader.c
> > +++ b/tools/lib/bpf/gen_loader.c
> > @@ -401,6 +401,15 @@ int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps)
> >                 opts->insns_sz = gen->insn_cur - gen->insn_start;
> >                 opts->data = gen->data_start;
> >                 opts->data_sz = gen->data_cur - gen->data_start;
> > +
> > +               /* use target endianness for embedded loader */
> > +               if (gen->swapped_endian) {
> > +                       struct bpf_insn *insn = (struct bpf_insn *)opts->insns;
> > +                       int insn_cnt = opts->insns_sz / sizeof(struct bpf_insn);
> > +
> > +                       for (i = 0; i < insn_cnt; i++)
> > +                               bpf_insn_bswap(insn++);
> > +               }
> >         }
> >         return gen->error;
> >  }
> > @@ -414,6 +423,31 @@ void bpf_gen__free(struct bpf_gen *gen)
> >         free(gen);
> >  }
> >
> > +/*
> > + * Fields of bpf_attr are set to values in native byte-order before being
> > + * written to the target-bound data blob, and may need endian conversion.
> > + * This macro allows providing the correct value in situ more simply than
> > + * writing a separate converter for *all fields* of *all records* included
> > + * in union bpf_attr. Note that sizeof(rval) should match the assignment
> > + * target to avoid runtime problems.
> > + */
> > +#define tgt_endian(rval) ({                                    \
> > +       typeof(rval) _val;                                      \
> > +       if (!gen->swapped_endian)                               \
> 
> if/else has to have balanced branches w.r.t. {}. Either both should
> have it or both shouldn't. In this case both should have it.
> 
> > +               _val = (rval);                                  \
> > +       else {                                                  \
> > +               switch (sizeof(rval)) {                         \
> > +               case 1: _val = (rval); break;                   \
> > +               case 2: _val = bswap_16(rval); break;           \
> > +               case 4: _val = bswap_32(rval); break;           \
> > +               case 8: _val = bswap_64(rval); break;           \
> > +               default:_val = (rval);                          \
> > +                       pr_warn("unsupported bswap size!\n");   \
> 
> this is a weird formatting,  but you can also just unconditionally
> assign _val, and only swap it if gen->swapped_endian
> 
> typeof(rval) _val = (rval);
> 
> if (gen->swapped_endian) {
>     switch (...) {
>         case 1: ...
>         ...
>         case 8: ...
>         default: pr_warn("...");
>     }
> }
> 
> _val;
> 
> 
> seems simpler and cleaner, imo
> 

Yes, agreed. Will update.

> > +               }                                               \
> > +       }                                                       \
> > +       _val;                                                   \
> > +})
> > +
> 
> 
> for the rest, Alexei, can you please review and give your ack?

  parent reply	other threads:[~2024-09-01  6:00 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-30  7:29 [PATCH bpf-next v4 0/8] libbpf, selftests/bpf: Support cross-endian usage Tony Ambardar
2024-08-30  7:29 ` [PATCH bpf-next v4 1/8] libbpf: Improve log message formatting Tony Ambardar
2024-08-30  7:29 ` [PATCH bpf-next v4 2/8] libbpf: Fix header comment typos for BTF.ext Tony Ambardar
2024-08-30  7:29 ` [PATCH bpf-next v4 3/8] libbpf: Fix output .symtab byte-order during linking Tony Ambardar
2024-08-30 22:15   ` Eduard Zingerman
2024-09-01  5:59     ` Tony Ambardar
2024-08-30  7:29 ` [PATCH bpf-next v4 4/8] libbpf: Support BTF.ext loading and output in either endianness Tony Ambardar
2024-08-30 21:14   ` Andrii Nakryiko
2024-09-02  8:19     ` Tony Ambardar
2024-09-04 19:55       ` Andrii Nakryiko
2024-08-31  0:15   ` Eduard Zingerman
2024-09-16  8:20     ` Tony Ambardar
2024-08-30  7:29 ` [PATCH bpf-next v4 5/8] libbpf: Support opening bpf objects of " Tony Ambardar
2024-08-30 21:25   ` Andrii Nakryiko
2024-08-31  1:26     ` Eduard Zingerman
2024-09-01  6:05       ` Tony Ambardar
2024-09-01  6:03     ` Tony Ambardar
2024-08-31  1:16   ` Eduard Zingerman
2024-09-01  6:04     ` Tony Ambardar
2024-08-30  7:29 ` [PATCH bpf-next v4 6/8] libbpf: Support linking " Tony Ambardar
2024-08-30 21:25   ` Andrii Nakryiko
2024-09-01  6:02     ` Tony Ambardar
2024-08-30  7:29 ` [PATCH bpf-next v4 7/8] libbpf: Support creating light skeleton " Tony Ambardar
2024-08-30 21:30   ` Andrii Nakryiko
2024-08-31  1:24     ` Alexei Starovoitov
2024-09-01  6:02       ` Tony Ambardar
2024-09-01  6:00     ` Tony Ambardar [this message]
2024-08-30  7:29 ` [PATCH bpf-next v4 8/8] selftests/bpf: Support cross-endian building Tony Ambardar

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=ZtQDB8NICr3khymH@kodidev-ubuntu \
    --to=tony.ambardar@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=iii@linux.ibm.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mykolal@fb.com \
    --cc=qmo@kernel.org \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --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 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.