From: Delyan Kratunov <delyank@fb.com>
To: "andrii.nakryiko@gmail.com" <andrii.nakryiko@gmail.com>
Cc: "daniel@iogearbox.net" <daniel@iogearbox.net>,
"ast@kernel.org" <ast@kernel.org>,
"andrii@kernel.org" <andrii@kernel.org>,
"bpf@vger.kernel.org" <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next v2 1/1] bpftool: bpf skeletons assert type sizes
Date: Tue, 15 Feb 2022 17:27:27 +0000 [thread overview]
Message-ID: <8c8820379a241322535ce0821bdb9f6c05c91290.camel@fb.com> (raw)
In-Reply-To: <CAEf4BzYZ7r3hpUsEQvkF-fpJhHdt0OXAxJxPvPDN-f4088bM6A@mail.gmail.com>
On Mon, 2022-02-14 at 21:11 -0800, Andrii Nakryiko wrote:
> So doing it right after each section really pollutes the layout of the
> skeleton's struct and hurts readability a lot.
>
> How about adding all those _Static_asserts in <skeleton__elf_bytes()
> function, after the huge binary dump, to get it out of sight?
I can just add a `void __attribute__((unused)) skeleton__assert_sizes()` at the
end? Or a `struct skeleton__type_asserts`? It feels weird to just put them in
elf_bytes, they don't belong there.
> I think
> if we are doing asserts, we might as well validate that not just
> sizes, but also each variable's offset within the section is right.
Sure, can do.
> _Static_assert(sizeof(s->data->in1) == 4, "invalid size of in1");
> _Static_assert(offsetof(typeof(*skel->data), in1) == 0, "invalid
> offset of in1");
> ...
> _Static_assert(sizeof(s->data_read_mostly->read_mostly_var) == 4,
> "invalid size of read_mostly_var");
> _Static_assert(offsetof(typeof(*skel->data_read_mostly),
> read_mostly_var) == 0, "invalid offset of read_mostly_var");
>
> (void)s; /* avoid unused variable warning */
>
> WDYT?
That's fine by me, I have no objections. I'll see if a function or a struct is
more readable.
I suspect `SIZE_ASSERT(data, in1, 4); OFFSET_ASSERT(data, in1, 0);` is probably
most readable but I hate that I'd have to include the macros inline (to emit the
skeleton type name).
> > return 0;
> > }
> >
> > @@ -756,6 +779,12 @@ static int do_skeleton(int argc, char **argv)
> > \n\
> > #include <bpf/skel_internal.h> \n\
> > \n\
> > + #ifdef __cplusplus \n\
> > + #define BPF_STATIC_ASSERT static_assert \n\
> > + #else \n\
> > + #define BPF_STATIC_ASSERT _Static_assert \n\
> > + #endif \n\
>
> Maybe just:
>
> #ifdef __cplusplus
> #define _Static_assert static_assert
> #endif
>
> ? Or that doesn't work?
It does work, it's just less explicit. I'd be happy to remove the macro
expansion on the C path though, it would make diagnostics shorter.
> Also any such macro has to be #undef in this file, otherwise it will
> "leak" into the user's code (as this is just a header file included in
> user's .c files).
My bad, just thought of that too.
--
To summarize, structurally I'll do this:
1. Put them all in one place. (tbd what type)
2. Put them at the end of the file.
3. Add offsets.
4. Fix up the macro usage.
next prev parent reply other threads:[~2022-02-15 17:27 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-15 0:26 [PATCH bpf-next v2 0/1] Avoid size mismatches in skeletons Delyan Kratunov
2022-02-15 0:26 ` [PATCH bpf-next v2 1/1] bpftool: bpf skeletons assert type sizes Delyan Kratunov
2022-02-15 5:11 ` Andrii Nakryiko
2022-02-15 17:27 ` Delyan Kratunov [this message]
2022-02-15 17:55 ` Andrii Nakryiko
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=8c8820379a241322535ce0821bdb9f6c05c91290.camel@fb.com \
--to=delyank@fb.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
/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).