On 9/1/25 03:29, Chen Ridong wrote:


On 2025/8/30 21:30, Gustavo A. R. Silva wrote:
Hi all,

I'm working on enabling -Wflex-array-member-not-at-end in mainline, and
I ran into thousands (yes, 14722 to be precise) of these warnings caused
by an instance of `struct cgroup` in the middle of `struct cgroup_root`.
See below:

620 struct cgroup_root {
     ...
633         /*
634          * The root cgroup. The containing cgroup_root will be destroyed on 
its
635          * release. cgrp->ancestors[0] will be used overflowing into the
636          * following field. cgrp_ancestor_storage must immediately follow.
637          */
638         struct cgroup cgrp;
639
640         /* must follow cgrp for cgrp->ancestors[0], see above */
641         struct cgroup *cgrp_ancestor_storage;
     ...
};

Based on the comments above, it seems that the original code was expecting
cgrp->ancestors[0] and cgrp_ancestor_storage to share the same addres in
memory.

However when I take a look at the pahole output, I see that these two members
are actually misaligned by 56 bytes. See below:

struct cgroup_root {
     ...

     /* --- cacheline 1 boundary (64 bytes) --- */
     struct cgroup              cgrp __attribute__((__aligned__(64))); /*    64 
 2112 */

     /* XXX last struct has 56 bytes of padding */

     /* --- cacheline 34 boundary (2176 bytes) --- */
     struct cgroup *            cgrp_ancestor_storage; /*  2176     8 */

     ...

     /* size: 6400, cachelines: 100, members: 11 */
     /* sum members: 6336, holes: 1, sum holes: 16 */
     /* padding: 48 */
     /* paddings: 1, sum paddings: 56 */
     /* forced alignments: 1, forced holes: 1, sum forced holes: 16 */
} __attribute__((__aligned__(64)));

This is due to the fact that struct cgroup have some tailing padding after
flexible-array member `ancestors` due to alignment to 64 bytes, see below:

struct cgroup {
     ...

     struct cgroup *            ancestors[];          /*  2056     0 */


Instead of using a flexible array member, could we convert this to a pointer 
and handle the memory
allocation explicitly?


Yep, that's always an option. However, I also wanted to see what people
think about the current misalignment between cgrp->ancestors[0] and
cgrp_ancestor_storage I describe above.

And if the heap allocation is an acceptable solution in this case, I'm
happy to go that route.

Thanks
-Gustavo


Reply via email to