Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Tony Ambardar <tony.ambardar@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf@vger.kernel.org, linux-kselftest@vger.kernel.org,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	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 v5 4/8] libbpf: Support BTF.ext loading and output in either endianness
Date: Mon, 16 Sep 2024 01:21:11 -0700	[thread overview]
Message-ID: <Zufqd4+QdT6l92tT@kodidev-ubuntu> (raw)
In-Reply-To: <CAEf4Bzbe_p9JXsT2TkyfACzJOaAMC21T_T-rLhmwT9tJ=A2_7w@mail.gmail.com>

On Wed, Sep 04, 2024 at 12:48:36PM -0700, Andrii Nakryiko wrote:
> On Tue, Sep 3, 2024 at 12:33 AM Tony Ambardar <tony.ambardar@gmail.com> wrote:
> >
> > Support for handling BTF data of either endianness was added in [1], but
> > did not include BTF.ext data for lack of use cases. Later, support for
> > static linking [2] provided a use case, but this feature and later ones
> > were restricted to native-endian usage.
> >
> > Add support for BTF.ext handling in either endianness. Convert BTF.ext data
> > to native endianness when read into memory for further processing, and
> > support raw data access that restores the original byte-order for output.
> > Add internal header functions for byte-swapping func, line, and core info
> > records.
> >
> > Add new API functions btf_ext__endianness() and btf_ext__set_endianness()
> > for query and setting byte-order, as already exist for BTF data.
> >
> > [1] 3289959b97ca ("libbpf: Support BTF loading and raw data output in both endianness")
> > [2] 8fd27bf69b86 ("libbpf: Add BPF static linker BTF and BTF.ext support")
> >
> > Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com>
> > ---
> >  tools/lib/bpf/btf.c             | 238 +++++++++++++++++++++++++++++---
> >  tools/lib/bpf/btf.h             |   3 +
> >  tools/lib/bpf/libbpf.map        |   2 +
> >  tools/lib/bpf/libbpf_internal.h |  28 ++++
> >  4 files changed, 255 insertions(+), 16 deletions(-)
> >
> > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> > index 5f094c1f4388..c11dfc81d007 100644
> > --- a/tools/lib/bpf/btf.c
> > +++ b/tools/lib/bpf/btf.c
> > @@ -3023,25 +3023,140 @@ static int btf_ext_setup_core_relos(struct btf_ext *btf_ext)
> >         return btf_ext_setup_info(btf_ext, &param);
> >  }
> >
> > -static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
> > +/* Swap byte-order of BTF.ext header with any endianness */
> > +static void btf_ext_bswap_hdr(struct btf_ext *btf_ext, __u32 hdr_len)
> >  {
> > -       const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
> > +       struct btf_ext_header *h = btf_ext->hdr;
> >
> > -       if (data_size < offsetofend(struct btf_ext_header, hdr_len) ||
> > -           data_size < hdr->hdr_len) {
> > -               pr_debug("BTF.ext header not found\n");
> > +       h->magic = bswap_16(h->magic);
> > +       h->hdr_len = bswap_32(h->hdr_len);
> > +       h->func_info_off = bswap_32(h->func_info_off);
> > +       h->func_info_len = bswap_32(h->func_info_len);
> > +       h->line_info_off = bswap_32(h->line_info_off);
> > +       h->line_info_len = bswap_32(h->line_info_len);
> > +
> > +       if (hdr_len < offsetofend(struct btf_ext_header, core_relo_len))
> > +               return;
> > +
> > +       h->core_relo_off = bswap_32(h->core_relo_off);
> > +       h->core_relo_len = bswap_32(h->core_relo_len);
> > +}
> > +
> > +/* Swap metadata byte-order of generic info subsection */
> > +static int info_subsec_bswap_metadata(const struct btf_ext *btf_ext, struct btf_ext_info *ext_info)
> > +{
> > +       const bool is_native = btf_ext->swapped_endian;
> > +       __u32 left, *rs, rec_size, num_info;
> > +       struct btf_ext_info_sec *si;
> > +
> > +       if (ext_info->len == 0)
> > +               return 0;
> > +
> > +       rs = ext_info->info - sizeof(__u32);    /* back up to record size */
> > +       rec_size = is_native ? *rs : bswap_32(*rs);
> > +       if (rec_size != ext_info->rec_size)
> >                 return -EINVAL;
> > +       *rs = bswap_32(*rs);
> > +
> > +       si = ext_info->info;                    /* sec info #1 */
> > +       left = ext_info->len;
> > +       while (left > 0) {
> > +               num_info = is_native ? si->num_info : bswap_32(si->num_info);
> > +               si->sec_name_off = bswap_32(si->sec_name_off);
> > +               si->num_info = bswap_32(si->num_info);
> > +               si = (void *)si->data + rec_size * num_info;
> > +               left -= offsetof(struct btf_ext_info_sec, data) +
> > +                       rec_size * num_info;
> >         }
> >
> > +       return 0;
> > +}
> > +
> > +/* Swap byte order of info subsection metadata and records in the correct
> > + * order depending on whether or not data is in native endianness.
> > + */
> > +#define ORDER_INFO_BSWAP(btf_ext, ext_info, info_t, swap_fn)           \
> > +{                                                                      \
> > +       const bool is_native = btf_ext->swapped_endian;                 \
> > +       struct btf_ext_info_sec *si;                                    \
> > +       int c, err;                                                     \
> > +       info_t *i;                                                      \
> > +       if (is_native)                                                  \
> > +               for_each_btf_ext_sec(ext_info, si)                      \
> > +                       for_each_btf_ext_rec(ext_info, si, c, i)        \
> > +                               swap_fn(i);                             \
> > +       err = info_subsec_bswap_metadata(btf_ext, ext_info);            \
> > +       if (err) {                                                      \
> > +               pr_warn(#info_t " record size mismatch!\n");            \
> > +               return err;                                             \
> > +       }                                                               \
> > +       if (!is_native)                                                 \
> > +               for_each_btf_ext_sec(ext_info, si)                      \
> > +                       for_each_btf_ext_rec(ext_info, si, c, i)        \
> > +                               swap_fn(i);                             \
> > +}
> > +
> 
> Nope-nope-nope, I'm never landing something like this :)
> 
> Ok, if there is no clean swapping and setup separation we can come up
> with, let's still do a decent job at trying to keep all this coherent
> and simple.
> 
> How about this? btf_ext_bswap_hdr() stays, it seems fine.
> 
> btf_ext_setup_func_info(), btf_ext_setup_line_info(), and
> btf_ext_setup_core_relos() all call into generic btf_ext_setup_info,
> right? We teach btf_ext_setup_info() to work with both endianness (but
> not swap data at all). The only two fields that we need to byte-swap
> on the fly are record_size and sinfo->num_info, so it's minimal
> changes to btf_ext_setup_info().
> 
> Oh, and while we are at it, if data is in non-native endianness,
> btf_ext_setup_info() should return failure if any of the record sizes
> are not *exactly* matching our expectations. This should be a trivial
> addition as we have never extended those records, I believe.
> 
> This will take care of correctness checking regardless of endianness.
> 
> Now, forget about for_each_btf_ext_{sec,rec}(), it will be too fragile
> to make them work for inverted endianness. But we know now that the
> data is correct, right? So, fine, let's have a bit of duplication (but
> without any error checking) to go over raw .BTF.ext data and swap all
> the records and all metadata fields. This logic now will be used after
> btf_ext_setup_*() steps, and in btf_ext_raw_data(). For
> btf_ext_raw_data() you'll also btf_ext_bswap_hdr() at the very end,
> right?
> 
> How does that sound? Am I missing something big again?
> 
> And fine, let's use callbacks for different record types to keep this
> simple. I dislike callbacks, in principle, but sometimes they are the
> simplest way forward, unfortunately (a proper iterator would be
> better, but that's another story and I don't want to get into that
> implementation task just yet).

Hi Andrii,

Sorry, this took longer than expected to get back to, but hopefully we're
nearly there.

In principle this all works, but so did first swapping info metadata and
data together per my v4 patch. Calling various "setup" functions before
byte-swapping seems confusing too, and warrants some reorganization, also
to cleanly pass the current byte-order state into info parsing functions.
On balance, I do agree there's an advantage to reusing the existing info
validation checks and dropping the extra ones I had to add, so this seems
a worthwhile approach to try. I'll follow up with a v6 shortly.

> 
> > +/*
> > + * Swap endianness of the whole info segment in a BTF.ext data section:
> > + *   - requires BTF.ext header data in native byte order
> > + *   - only support info structs from BTF version 1
> > + */
> > +static int btf_ext_bswap_info(struct btf_ext *btf_ext)
> > +{
> > +       const struct btf_ext_header *h = btf_ext->hdr;
> > +       struct btf_ext_info ext = {};
> > +
> > +       /* Swap func_info subsection byte-order */
> > +       ext.info = (void *)h + h->hdr_len + h->func_info_off + sizeof(__u32);
> > +       ext.len = h->func_info_len - (h->func_info_len ? sizeof(__u32) : 0);
> > +       ext.rec_size = sizeof(struct bpf_func_info);
> > +
> > +       ORDER_INFO_BSWAP(btf_ext, &ext, struct bpf_func_info, bpf_func_info_bswap);
> 
> You shouldn't have bent over backwards just to use
> for_each_btf_ext_{sec,rec}() macros. Just because I proposed it
> (initially, without actually coding anything) doesn't mean it's the
> best and final solution :)
> 
> > +
> > +       /* Swap line_info subsection byte-order */
> > +       ext.info = (void *)h + h->hdr_len + h->line_info_off + sizeof(__u32);
> > +       ext.len = h->line_info_len - (h->line_info_len ? sizeof(__u32) : 0);
> > +       ext.rec_size = sizeof(struct bpf_line_info);
> > +
> > +       ORDER_INFO_BSWAP(btf_ext, &ext, struct bpf_line_info, bpf_line_info_bswap);
> > +
> > +       /* Swap core_relo subsection byte-order (if present) */
> > +       if (h->hdr_len < offsetofend(struct btf_ext_header, core_relo_len))
> > +               return 0;
> > +
> > +       ext.info = (void *)h + h->hdr_len + h->core_relo_off + sizeof(__u32);
> > +       ext.len = h->core_relo_len - (h->core_relo_len ? sizeof(__u32) : 0);
> > +       ext.rec_size = sizeof(struct bpf_core_relo);
> > +
> > +       ORDER_INFO_BSWAP(btf_ext, &ext, struct bpf_core_relo, bpf_core_relo_bswap);
> > +
> > +       return 0;
> > +}
> > +#undef ORDER_INFO_BSWAP
> 
> [...]

  reply	other threads:[~2024-09-16  8:21 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-03  7:32 [PATCH bpf-next v5 0/8] libbpf, selftests/bpf: Support cross-endian usage Tony Ambardar
2024-09-03  7:32 ` [PATCH bpf-next v5 1/8] libbpf: Improve log message formatting Tony Ambardar
2024-09-03  7:32 ` [PATCH bpf-next v5 2/8] libbpf: Fix header comment typos for BTF.ext Tony Ambardar
2024-09-03  7:33 ` [PATCH bpf-next v5 3/8] libbpf: Fix output .symtab byte-order during linking Tony Ambardar
2024-09-03  7:33 ` [PATCH bpf-next v5 4/8] libbpf: Support BTF.ext loading and output in either endianness Tony Ambardar
2024-09-04 19:48   ` Andrii Nakryiko
2024-09-16  8:21     ` Tony Ambardar [this message]
2024-09-03  7:33 ` [PATCH bpf-next v5 5/8] libbpf: Support opening bpf objects of " Tony Ambardar
2024-09-04 19:50   ` Andrii Nakryiko
2024-09-03  7:33 ` [PATCH bpf-next v5 6/8] libbpf: Support linking " Tony Ambardar
2024-09-04 19:51   ` Andrii Nakryiko
2024-09-03  7:33 ` [PATCH bpf-next v5 7/8] libbpf: Support creating light skeleton " Tony Ambardar
2024-09-03 19:57   ` Alexei Starovoitov
2024-09-03 23:52     ` Tony Ambardar
2024-09-03  7:33 ` [PATCH bpf-next v5 8/8] selftests/bpf: Support cross-endian building Tony Ambardar
2024-09-04  5:52   ` Yonghong Song

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=Zufqd4+QdT6l92tT@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox