Re: union: was: Re: [PATCH printk v1 05/18] printk: Add non-BKL console basic infrastructure

From: John Ogness
Date: Tue Mar 28 2023 - 05:45:02 EST


On 2023-03-28, Petr Mladek <pmladek@xxxxxxxx> wrote:
>> A compilation check would be nice. Is that possible?
>
> I think the following might do the trick:
>
> static_assert(sizeof(struct cons_state) == sizeof(atomic_long_t));

I never realized the kernel code was allowed to have that. But it is
everywhere! :-) Thanks. I've added and tested the following:

/*
* The nbcon_state struct is used to easily create and interpret values that
* are stored in the console.nbcon_state variable. Make sure this struct stays
* within the size boundaries of that atomic variable's underlying type in
* order to avoid any accidental truncation.
*/
static_assert(sizeof(struct nbcon_state) <= sizeof(long));

Note that I am checking against sizeof(long), the underlying variable
type. We probably shouldn't assume sizeof(atomic_long_t) is always
sizeof(long).

John