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 v4 6/8] libbpf: Support linking bpf objects of either endianness
Date: Sat, 31 Aug 2024 23:02:59 -0700	[thread overview]
Message-ID: <ZtQDk2l6v/WerQ5+@kodidev-ubuntu> (raw)
In-Reply-To: <CAEf4BzbR+3Y7WoYCut-S_S7TEh8rna9033m3Ug9jw2Weff5tNw@mail.gmail.com>

On Fri, Aug 30, 2024 at 02:25:07PM -0700, Andrii Nakryiko wrote:
> On Fri, Aug 30, 2024 at 12:30 AM Tony Ambardar <tony.ambardar@gmail.com> wrote:
> >
> > Allow static linking object files of either endianness, checking that input
> > files have consistent byte-order, and setting output endianness from input.
> >
> > Linking requires in-memory processing of programs, relocations, sections,
> > etc. in native endianness, and output conversion to target byte-order. This
> > is enabled by built-in ELF translation and recent BTF/BTF.ext endianness
> > functions. Further add local functions for swapping byte-order of sections
> > containing BPF insns.
> >
> > Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com>
> > ---
> >  tools/lib/bpf/linker.c | 90 ++++++++++++++++++++++++++++++++++--------
> >  1 file changed, 74 insertions(+), 16 deletions(-)
> >
> 
> [...]
> 
> >
> > +static bool is_exec_sec(struct dst_sec *sec)
> > +{
> > +       if (!sec || sec->ephemeral)
> > +               return false;
> > +       return (sec->shdr->sh_type == SHT_PROGBITS) &&
> > +              (sec->shdr->sh_flags & SHF_EXECINSTR);
> > +}
> > +
> > +static int exec_sec_bswap(void *raw_data, int size)
> > +{
> > +       const int insn_cnt = size / sizeof(struct bpf_insn);
> > +       struct bpf_insn *insn = raw_data;
> > +       int i;
> > +
> > +       if (size % sizeof(struct bpf_insn))
> > +               return -EINVAL;
> 
> this shouldn't be checked here, it should be assumed this is valid and
> was ensured by the caller. And make exec_sec_bswap() a void function,
> please.
> 
> > +       for (i = 0; i < insn_cnt; i++, insn++)
> > +               bpf_insn_bswap(insn);
> > +       return 0;
> > +}
> > +
> >  static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src_sec *src)
> >  {
> >         void *tmp;
> > @@ -1170,6 +1203,10 @@ static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src
> >                 memset(dst->raw_data + dst->sec_sz, 0, dst_align_sz - dst->sec_sz);
> >                 /* now copy src data at a properly aligned offset */
> >                 memcpy(dst->raw_data + dst_align_sz, src->data->d_buf, src->shdr->sh_size);
> > +
> 
> the check for size % sizeof(struct bpf_insn) should be somewhere here
> (if is_exec_sec()), right?
> 
> > +               /* convert added bpf insns to native byte-order */
> > +               if (linker->swapped_endian && is_exec_sec(dst))
> > +                       exec_sec_bswap(dst->raw_data + dst_align_sz, src->shdr->sh_size);
> >         }
> >
> >         dst->sec_sz = dst_final_sz;
> > @@ -2630,6 +2667,10 @@ int bpf_linker__finalize(struct bpf_linker *linker)
> >                 if (!sec->scn)
> >                         continue;
> >
> 
> but no need to check here, we know it's correct, if we got all the way here
> 

I did a pass earlier to reorganize sanity checks and remove redundant
ones, and realized linker_sanity_check_elf() already does what we want
but overlooked dropping this from exec_sec_bswap(). Will do so now. Thanks
for catching!

> > +               /* restore sections with bpf insns to target byte-order */
> > +               if (linker->swapped_endian && is_exec_sec(sec))
> > +                       exec_sec_bswap(sec->raw_data, sec->sec_sz);
> > +
> >                 sec->data->d_buf = sec->raw_data;
> >         }
> >
> > @@ -2698,6 +2739,7 @@ static int emit_elf_data_sec(struct bpf_linker *linker, const char *sec_name,
> >
> >  static int finalize_btf(struct bpf_linker *linker)
> >  {
> > +       enum btf_endianness link_endianness;
> >         LIBBPF_OPTS(btf_dedup_opts, opts);
> >         struct btf *btf = linker->btf;
> >         const void *raw_data;
> > @@ -2742,6 +2784,22 @@ static int finalize_btf(struct bpf_linker *linker)
> >                 return err;
> >         }
> >
> > +       /* Set .BTF and .BTF.ext output byte order */
> > +       link_endianness = linker->elf_hdr->e_ident[EI_DATA] == ELFDATA2MSB ?
> > +                         BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN;
> > +       err = btf__set_endianness(linker->btf, link_endianness);
> > +       if (err) {
> > +               pr_warn("failed to set .BTF output endianness: %d\n", err);
> > +               return err;
> > +       }
> 
> link_endianness is always well-formed enum, there is no need to check
> errors, here and for btf_ext__set_endianness, please drop both

Right, makes sense.

> 
> > +       if (linker->btf_ext) {
> > +               err = btf_ext__set_endianness(linker->btf_ext, link_endianness);
> > +               if (err) {
> > +                       pr_warn("failed to set .BTF.ext output endianness: %d\n", err);
> > +                       return err;
> > +               }
> > +       }
> > +
> >         /* Emit .BTF section */
> >         raw_data = btf__raw_data(linker->btf, &raw_sz);
> >         if (!raw_data)
> > --
> > 2.34.1
> >

  reply	other threads:[~2024-09-01  6:03 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 [this message]
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
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=ZtQDk2l6v/WerQ5+@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