From: Ilya Leoshkevich <iii@linux.ibm.com>
To: Puranjay Mohan <puranjay12@gmail.com>,
Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Stanislav Fomichev <sdf@google.com>,
bpf@vger.kernel.org, quentin@isovalent.com, ast@kernel.org,
daniel@iogearbox.net, memxor@gmail.com
Subject: Re: [RFC] libbbpf/bpftool: Support 32-bit Architectures.
Date: Mon, 20 Feb 2023 17:30:29 +0100 [thread overview]
Message-ID: <20230220163029.3c5hv4rzaybt7jlr@heavy> (raw)
In-Reply-To: <CANk7y0hJc=--b8eV4d6nmjAThuv1njvTtbzpvpp_UmPB6R=6ag@mail.gmail.com>
On Sat, Feb 18, 2023 at 02:23:56PM +0100, Puranjay Mohan wrote:
> On Fri, Feb 17, 2023 at 10:46 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> >
> > On Fri, Feb 17, 2023 at 2:25 AM Puranjay Mohan <puranjay12@gmail.com> wrote:
> > >
> > > Hi,
> > > Thanks for the response.
> > >
> > > On Thu, Feb 16, 2023 at 11:13 PM Andrii Nakryiko
> > > <andrii.nakryiko@gmail.com> wrote:
> > > >
> > > > On Wed, Feb 15, 2023 at 5:48 PM Stanislav Fomichev <sdf@google.com> wrote:
> > > > >
> > > > > On 02/15, Puranjay Mohan wrote:
> > > > > > The BPF selftests fail to compile on 32-bit architectures as the skeleton
> > > > > > generated by bpftool doesn’t take into consideration the size difference
> > > > > > of
> > > > > > variables on 32-bit/64-bit architectures.
> > > > >
> > > > > > As an example,
> > > > > > If a bpf program has a global variable of type: long, its skeleton will
> > > > > > include
> > > > > > a bss map that will have a field for this variable. The long variable in
> > > > > > BPF is
> > > > > > 64-bit. if we are working on a 32-bit machine, the generated skeleton has
> > > > > > to
> > > > > > compile for that machine where long is 32-bit.
> > > > >
> > > > > > A reproducer for this issue:
> > > > > > root@56ec59aa632f:~# cat test.bpf.c
> > > > > > long var;
> > > > >
> > > > > > root@56ec59aa632f:~# clang -target bpf -g -c test.bpf.c
> > > > >
> > > > > > root@56ec59aa632f:~# bpftool btf dump file test.bpf.o format raw
> > > > > > [1] INT 'long int' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED
> > > > > > [2] VAR 'var' type_id=1, linkage=global
> > > > > > [3] DATASEC '.bss' size=0 vlen=1
> > > > > > type_id=2 offset=0 size=8 (VAR 'var')
> > > > >
> > > > > > root@56ec59aa632f:~# bpftool gen skeleton test.bpf.o > skeleton.h
> > > > >
> > > > > > root@56ec59aa632f:~# echo "#include \"skeleton.h\"" > test.c
> > > > >
> > > > > > root@56ec59aa632f:~# gcc test.c
> > > > > > In file included from test.c:1:
> > > > > > skeleton.h: In function 'test_bpf__assert':
> > > > > > skeleton.h:231:2: error: static assertion failed: "unexpected
> > > > > > size of \'var\'"
> > > > > > 231 | _Static_assert(sizeof(s->bss->var) == 8, "unexpected
> > > > > > size of 'var'");
> > > > > > | ^~~~~~~~~~~~~~
> > > > >
> > > > > > One naive solution for this would be to map ‘long’ to ‘long long’ and
> > > > > > ‘unsigned long’ to ‘unsigned long long’. But this doesn’t solve everything
> > > > > > because this problem is also seen with pointers that are 64-bit in BPF and
> > > > > > 32-bit in 32-bit machines.
> > > > >
> > > > > > I want to work on solving this and am looking for ideas to solve it
> > > > > > efficiently.
> > > > > > The main goal is to make libbbpf/bpftool host architecture agnostic.
> > > > >
> > > > > Looks like bpftool needs to be aware of the target architecture. The
> > > > > same way gcc is doing with build-host-target triplet. I don't
> > > > > think this can be solved with a bunch of typedefs? But I've long
> > > > > forgotten how a pure 32-bit machine looks, so I can't give any
> > > > > useful input :-(
> > > >
> > > > Yeah, I'd rather avoid making bpftool aware of target architecture.
> > > > Three is 32 vs 64 bitness, there is also little/big endianness, etc.
> > > >
> > > > So I'd recommend never using "long" (and similar types that depend on
> > > > bitness of the platform, like size_t, etc) for global variables. Also
> > > > don't use pointer types as types of the variable. Stick to __u64,
> > > > __u32, etc.
> > >
> > > I feel if we follow. this convention then it will work out but
> > > currently a lot of selftests use these
> > > architecture dependent variable types and therefore don't even compile
> > > for 32-bit architectures
> > > because of the _Static_asserts in the skeleton.
> > >
> > > Do you suggest replacing all these with __u64, __u32, etc. in the
> > > selftests so that they compile on every architecture?
> >
> > how many changes are we talking about? my initial reaction is that
> > yeah, if this matters for correctness, we should be strict with __u64
> > and __u32 use over long
>
> I can try to compile the selftests on arm32 and count the number of failures.
> It is important for correctness but also for testing the support of
> BPF on non-64 bit
> architectures. If the selftests don't even compile we can't test BPF properly.
Hi,
Does anyone plan looking into fixing selftests on 32-bit arches in the
near future (i.e. getting rid of longs and pointers)? I have an x86 JIT
change that I would like to test, and I'm also running into this issue.
I can try doing this, but I'd like to avoid doing duplicate work.
Best regards,
Ilya
[...]
next prev parent reply other threads:[~2023-02-20 16:30 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-15 14:12 [RFC] libbbpf/bpftool: Support 32-bit Architectures Puranjay Mohan
2023-02-16 1:42 ` Stanislav Fomichev
2023-02-16 22:13 ` Andrii Nakryiko
2023-02-17 10:25 ` Puranjay Mohan
2023-02-17 11:59 ` Quentin Monnet
2023-02-17 21:56 ` Andrii Nakryiko
2023-02-20 11:28 ` Quentin Monnet
2023-02-28 6:31 ` Andrii Nakryiko
2023-02-17 21:45 ` Andrii Nakryiko
2023-02-18 13:23 ` Puranjay Mohan
2023-02-20 16:30 ` Ilya Leoshkevich [this message]
2023-02-20 16:47 ` Puranjay Mohan
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=20230220163029.3c5hv4rzaybt7jlr@heavy \
--to=iii@linux.ibm.com \
--cc=andrii.nakryiko@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=memxor@gmail.com \
--cc=puranjay12@gmail.com \
--cc=quentin@isovalent.com \
--cc=sdf@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox