]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
cgroup: Eliminate cgrp_ancestor_storage in cgroup_root
authorMichal Koutný <mkoutny@suse.com>
Wed, 7 Jan 2026 16:59:41 +0000 (17:59 +0100)
committerTejun Heo <tj@kernel.org>
Thu, 8 Jan 2026 01:11:03 +0000 (15:11 -1000)
The cgrp_ancestor_storage has two drawbacks:
- it's not guaranteed that the member immediately follows struct cgrp in
  cgroup_root (root cgroup's ancestors[0] might thus point to a padding
  and not in cgrp_ancestor_storage proper),
- this idiom raises warnings with -Wflex-array-member-not-at-end.

Instead of relying on the auxiliary member in cgroup_root, define the
0-th level ancestor inside struct cgroup (needed for static allocation
of cgrp_dfl_root), deeper cgroups would allocate flexible
_low_ancestors[].  Unionized alias through ancestors[] will
transparently join the two ranges.

The above change would still leave the flexible array at the end of
struct cgroup inside cgroup_root, so move cgrp also towards the end of
cgroup_root to resolve the -Wflex-array-member-not-at-end.

Link: https://lore.kernel.org/r/5fb74444-2fbb-476e-b1bf-3f3e279d0ced@embeddedor.com/
Reported-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Closes: https://lore.kernel.org/r/b3eb050d-9451-4b60-b06c-ace7dab57497@embeddedor.com/
Cc: David Laight <david.laight.linux@gmail.com>
Acked-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Michal Koutný <mkoutny@suse.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
include/linux/cgroup-defs.h
kernel/cgroup/cgroup.c

index b760a3c470a5680d89e278e6bfdcdc9ca8f37710..f7cc60de00583f70eab2157302d57246ad92c78a 100644 (file)
@@ -626,7 +626,13 @@ struct cgroup {
 #endif
 
        /* All ancestors including self */
-       struct cgroup *ancestors[];
+       union {
+               DECLARE_FLEX_ARRAY(struct cgroup *, ancestors);
+               struct {
+                       struct cgroup *_root_ancestor;
+                       DECLARE_FLEX_ARRAY(struct cgroup *, _low_ancestors);
+               };
+       };
 };
 
 /*
@@ -647,16 +653,6 @@ struct cgroup_root {
        struct list_head root_list;
        struct rcu_head rcu;    /* Must be near the top */
 
-       /*
-        * The root cgroup. The containing cgroup_root will be destroyed on its
-        * release. cgrp->ancestors[0] will be used overflowing into the
-        * following field. cgrp_ancestor_storage must immediately follow.
-        */
-       struct cgroup cgrp;
-
-       /* must follow cgrp for cgrp->ancestors[0], see above */
-       struct cgroup *cgrp_ancestor_storage;
-
        /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
        atomic_t nr_cgrps;
 
@@ -668,6 +664,13 @@ struct cgroup_root {
 
        /* The name for this hierarchy - may be empty */
        char name[MAX_CGROUP_ROOT_NAMELEN];
+
+       /*
+        * The root cgroup. The containing cgroup_root will be destroyed on its
+        * release. This must be embedded last due to flexible array at the end
+        * of struct cgroup.
+        */
+       struct cgroup cgrp;
 };
 
 /*
index e717208cfb1855488475df6a306587e7eeca638a..554a02ee298ba92173a9da3cdebe15a3c7e7f6e8 100644 (file)
@@ -5847,7 +5847,7 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
        int ret;
 
        /* allocate the cgroup and its ID, 0 is reserved for the root */
-       cgrp = kzalloc(struct_size(cgrp, ancestors, (level + 1)), GFP_KERNEL);
+       cgrp = kzalloc(struct_size(cgrp, _low_ancestors, level), GFP_KERNEL);
        if (!cgrp)
                return ERR_PTR(-ENOMEM);