Re: [PATCH][next] perf/x86: Avoid multiple -Wflex-array-member-not-at-end warnings

From: Peter Zijlstra

Date: Thu May 07 2026 - 06:15:43 EST


On Thu, May 07, 2026 at 10:10:12AM +0200, Peter Zijlstra wrote:
> On Mon, May 04, 2026 at 01:50:50PM -0600, Gustavo A. R. Silva wrote:
> > > https://lore.kernel.org/all/20240806113254.GG12673@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/
> >
> > Feel free to apply it.
>
> Bah, stupid warning :/
>
> There really is no sane way to tell the compiler that the code is fine
> and it should just STFU already? I mean, it is directly followed by an
> array of the right type to fill that flex thing.
>
> Anyway, I suppose this all very much relies on the structure not getting
> randomized, so let me stick __no_randomize_layout on it at the very
> least.

Ideally GCC would get fixed to allow something like so:

struct bar;

struct foo {
...
struct bar entries[];
};

struct ponies {
...
struct foo my_foo __sized_by(my_bars);
struct bar my_bars[16];
...
};

Such that the __sized_by() applies to foo::entries and ensures the
member is no longer considered unsized.

But I suspect this needs the whole of the flexarray insanity fixed;
because for some idiotic reason it is allowed for offsetof(struct foo,
entries) != sizeof(struct foo), and if that is the case, then everything
goes sideways in a hurry.

Note that the kernel very much does not rely on that weird behaviour,
since it always allocates sizeof(foo) + count * sizeof(foo::entries[0])
like. So we always have sufficient space at the end of the object.

And then I'm sure fixing flexarray is considered breaking ABI and so
we're left up a creek without no paddles on.

This is a giant shitshow, is what.