From nobody Sat Feb 7 12:40:29 2026 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5528B355021 for ; Wed, 7 Jan 2026 17:02:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767805332; cv=none; b=Dg8Ff2nj2pNlU3CYvuryAGuYAn/rx3U4M3DV1opPjXLzL/BWtKAs0hwML7fLGYXIxCBaWakgE6FFKN52slWK8vUMextjJZMAJMfHxWXKXFuWHTCTP1EgVCwbLf2Go1i6YYxR5CYdgudIF8VhuVcTaRWVechAxxFydC07sRMiNOA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767805332; c=relaxed/simple; bh=tWFeLnDDz/em1zg/E8BbEXwCSep7COgZ1jMf56WRGho=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=mlTtv6VTB0jYIJng3zM0tt41CwkdKrPgL3Y4wDDQSXPr8HXjnxzueoq8z0vQONTWIRy8R11GDvDXNsBkRuijQlNTpZoqmNiaG7tXtcwQnqZxYkO26EXs8ExI+AvLI54PUqrtiRJ5gOn58hPlJVBCZYzAYprn253bG3RprVH1WZ8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=BSIwDP13; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="BSIwDP13" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-47aa03d3326so19832045e9.3 for ; Wed, 07 Jan 2026 09:02:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1767805327; x=1768410127; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=L49E8GK0L61Xb+Ow/6X+q4Ipvp5Im1cS6u2eq6FB63A=; b=BSIwDP13V9ZT/6Ty4pe8Lo2nBLjfq8yruRndt7Ac3Fy8AewH0VdfHMW0yuLJHBIQ5F 2da3wM/wTqoEqrIoa2tdMuELM4UTUtdOX2zdvGZIpjjgvPuqCv8GSC3Olr1nKRQGAXsl +5IHXf7qOokkWifqL2bRmwxKc8mFvxiz1MXtLBoAyALgK33eCkrpdjAER+infMPfT5QN NkN6L9e7MWX+OHnZcYuOy6WUn3iwj2L0zopuadpwD6z4dgGsqlwPdRnVvCBDnU4VLIKQ DNEl4ZBVphVc2tiK33cO6XAdafPKn9f3B1tEDnJOynIO9nQrxKubgMEFlEOMnbh1ST4Q ArmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767805327; x=1768410127; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=L49E8GK0L61Xb+Ow/6X+q4Ipvp5Im1cS6u2eq6FB63A=; b=LPfmsUc8ExCdwO6j7EXIK/U0AOHA8GguvMMyqeHVb0wkHLBZIZfZJRtajWPcbJqwny RYgWUivpJDtUsMFV2HiblrHBAHdRlVOiLxmR087+Q8ZYybQXxlMpxn/cloeapSCBIZcE 8tnOuLNrvlsybFHVnDwP+WqUsbp6BYvNmRLdvcYC7VnC08pXN1AavN9KIfuzKLSo6rQK TPU8MXT9iLsQ3d/PhoSidbODh7v1lHWOq9xljXUuinrCzmahNBdpW7VKyNnRfsccFL63 Ap+todocyjUt632rePbQyGwZ2oj29ZM5yX0vdySX8vPGkaDU7A24azfoIv29ER1i7W99 03rA== X-Forwarded-Encrypted: i=1; AJvYcCWkTNKXQya7eWYwoy703j3QsMFcE3BpdeQXBVrq0/CAkKmxb9+fWuTASvakTOgdaeBfsXBHlUeZzCubniI=@vger.kernel.org X-Gm-Message-State: AOJu0YzPPkI95EseBBXN/tKqRV7WF/yngJas9wZK8x8PU91ks0pMtwZb BzTmhFwFt9/qo/NGiPshXexgDRjG8szIt7zlb+dYHOT5t+1Ba8Sv8nS/2ZqVsTeVDrc= X-Gm-Gg: AY/fxX4NuvpZYkYF739Es95F00rbQmhjxBW2GxiqIBETLC8z3fDSYREjFULglntwkKf Ag/5JDBHBrn7slaLwV3KDon3vUQFMM5CPTbsBMI67Mo0KmmeqAqFPjv620NWeW9S1kUpAfKjmIZ sDo2iTeVLYwupDh8GDDTXxo6R+b08SkwpaJdm6Dxo3GwzYAJm7OcLnQVsZViPRg+3RQMOlWrQC1 l14VNS/y88Ims9QzkBBJIUExWPK6IX0R7deEFChMFxrjXtG78FPSa8z6YH+qGP/bcrnfZHxEoo6 gp8hrE9BQ9pz1H5EPc3WvqjxqRIwId436vSTEdvWn2Yd6FfLOBeGuy5iLDDChCrrWFFKxzoX2Bs kYjN6et8BFMQSWHCDPri2rUYb90XGpFH37iRU7ESpP/PO1uOzdcypsYtQwh+vyUqACNR9zPmRcQ pCBuBgOseI3hB004aojhZXcyJzLi/N8hI= X-Google-Smtp-Source: AGHT+IEg3eq4eSlFYkLOWLPuVP+Buf7OIMrTZ/OdJp8hfUfEblFjlugkX6dHGbDV+Go/PXS4KGbfpQ== X-Received: by 2002:a05:600c:3b28:b0:477:8a2a:123e with SMTP id 5b1f17b1804b1-47d84b41bbfmr39523105e9.33.1767805326694; Wed, 07 Jan 2026 09:02:06 -0800 (PST) Received: from blackdock.suse.cz (nat2.prg.suse.com. [195.250.132.146]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d7f6f0e15sm103041885e9.10.2026.01.07.09.02.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jan 2026 09:02:06 -0800 (PST) From: =?UTF-8?q?Michal=20Koutn=C3=BD?= To: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Cc: =?UTF-8?q?Michal=20Koutn=C3=BD?= , "Gustavo A. R. Silva" , David Laight , "Gustavo A. R. Silva" , Tejun Heo , Johannes Weiner , Kees Cook Subject: [PATCH v2] cgroup: Eliminate cgrp_ancestor_storage in cgroup_root Date: Wed, 7 Jan 2026 17:59:41 +0100 Message-ID: <20260107165942.95340-1-mkoutny@suse.com> X-Mailer: git-send-email 2.52.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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@embedd= edor.com/ Reported-by: "Gustavo A. R. Silva" Closes: https://lore.kernel.org/r/b3eb050d-9451-4b60-b06c-ace7dab57497@embe= ddedor.com/ Cc: David Laight Acked-by: Gustavo A. R. Silva Signed-off-by: Michal Koutn=C3=BD --- include/linux/cgroup-defs.h | 25 ++++++++++++++----------- kernel/cgroup/cgroup.c | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) Changes from v1 (https://lore.kernel.org/r/20251217162744.352391-1-mkoutny@= suse.com - drop __counted_by patches (2--4), too intrusive rework (Michal) - utilize DECLARE_FLEX_ARRAY (Gustavo) - trailers diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index b760a3c470a56..f7cc60de00583 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -626,7 +626,13 @@ struct cgroup { #endif =20 /* 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); + }; + }; }; =20 /* @@ -647,16 +653,6 @@ struct cgroup_root { struct list_head root_list; struct rcu_head rcu; /* Must be near the top */ =20 - /* - * 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; =20 @@ -668,6 +664,13 @@ struct cgroup_root { =20 /* 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; }; =20 /* diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index e717208cfb185..554a02ee298ba 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -5847,7 +5847,7 @@ static struct cgroup *cgroup_create(struct cgroup *pa= rent, const char *name, int ret; =20 /* allocate the cgroup and its ID, 0 is reserved for the root */ - cgrp =3D kzalloc(struct_size(cgrp, ancestors, (level + 1)), GFP_KERNEL); + cgrp =3D kzalloc(struct_size(cgrp, _low_ancestors, level), GFP_KERNEL); if (!cgrp) return ERR_PTR(-ENOMEM); =20 --=20 2.52.0