From nobody Fri Sep 25 09:19:45 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B04B63F4122; Mon, 14 Sep 2026 22:32:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789425141; cv=none; b=r/KtdkavxFH5tj2BMR2/mRXeHu5mSWCStOeRgzyTUh0DGUfCtDt315CuA0obxO6VSwOM7pFu21Fz+W/5kf7i3W4KdQVmAlj9D6kteWMiGQ4PWY9f2zmIqDfr7dlufIGvhJqFgWgoXXd9f4bZKuNyn7/oJI/2ceHgpGblv++5SOw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789425141; c=relaxed/simple; bh=cc1KgfUAzVgKTtRm7QSgi2oe8h4u5bnXQnDl9nlpys0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PBg4rZwTFiOpLux5ieASZdV5K/bN6e9W5urll/Oc+F+wG75Tadz+jCmcyGDXJ0Ymt/KDfulywsyt96MMLo4JF2J361Q9w6Rt6W6Eqj+YMxhR8Y7UHTdyo6IT7rUg5bltwa6Taz+0IsLWdlrQB1seoJIt/FM3OxciUl5pIts8E5g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GYHbVuIv; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GYHbVuIv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D5021F00893; Mon, 14 Sep 2026 22:32:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789425139; bh=3GXZPrWtCIjbM7MzBizS35UrFZBS63lQ6m5ygI5gZN4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=GYHbVuIvpBANoOBSPEKv4Z894y1qDNv39u2mQR4BwX1FRY0/TRyT0RimsmUh4RbPd Lti5s6H+YXVHDVyCidrK4YidWwvUinzMRMRkHg0VIF1uudcW6lght2qsSviRUZn8Zm zBGm8Gosu3tkEeBiYS5+FDJmlrz5JJrz5GUfGq/3bCJdn7QIN4RtOgRCyYM9aTsNW2 vdoSnTfRDNBzf/gpS34uNYDDaqtatllMo5EHQPp4Ne6RADmK+BkwDc37RbFJVJU0c/ lexiv0GSYMeH0ZT0ABvZl4F1NFN+EAKg0VHiDxu70BqHxJsmB7gwKVz/AqzV94BqKx zhlJEa4SOnnng== From: Kees Cook To: Greg Kroah-Hartman Cc: Kees Cook , Lorenzo Stoakes , Andrew Morton , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Zi Yan , linux-mm@kvack.org, "Rafael J. Wysocki" , Danilo Krummrich , "Gustavo A. R. Silva" , linux-kernel@vger.kernel.org, driver-core@lists.linux.dev, linux-hardening@vger.kernel.org Subject: [PATCH 1/2] mm: Move default_gfp() into gfp_types.h Date: Mon, 14 Sep 2026 15:32:16 -0700 Message-Id: <20260914223219.901347-1-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914223212.i.829-kees@kernel.org> References: <20260914223212.i.829-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1894; i=kees@kernel.org; h=from:subject; bh=cc1KgfUAzVgKTtRm7QSgi2oe8h4u5bnXQnDl9nlpys0=; b=owGbwMvMwCVmps19z/KJym7G02pJDFkrSj9Gy66X3ffw3tOPNgwW0a1HC59oy1+taxXUiX//0 ehkgrN2RykLgxgXg6yYIkuQnXuci8fb9nD3uYowc1iZQIYwcHEKwERUWhj+xzFGvQyQjN0Y6PAr 8NFkz6PnRFirGGs0/O9OP379teIqNoZ/uj8Pqwp96cn9rV39JO8oY5H9tkDlnKJu9iWZMqem16a zAQA= X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Using default_gfp() only depends on the GFP_KERNEL value. Move default_gfp() into gfp_types.h so that future use in devres.h doesn't require pulling in all of gfp.h. Signed-off-by: Kees Cook Reviewed-by: Vlastimil Babka (SUSE) Reviewed-by: Zi Yan --- Cc: Lorenzo Stoakes Cc: Andrew Morton Cc: David Hildenbrand Cc: "Liam R. Howlett" Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Suren Baghdasaryan Cc: Michal Hocko Cc: Brendan Jackman Cc: Johannes Weiner Cc: Zi Yan Cc: --- include/linux/gfp.h | 4 ---- include/linux/gfp_types.h | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 872bc53f32ec..b62e2fd53cb4 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -13,10 +13,6 @@ struct vm_area_struct; struct mempolicy; =20 -/* Helper macro to avoid gfp flags if they are the default one */ -#define __default_gfp(a,b,...) b -#define default_gfp(...) __default_gfp(,##__VA_ARGS__,GFP_KERNEL) - static inline bool gfpflags_allow_blocking(const gfp_t gfp_flags) { return !!(gfp_flags & __GFP_DIRECT_RECLAIM); diff --git a/include/linux/gfp_types.h b/include/linux/gfp_types.h index 190191411009..b1cd143f3c42 100644 --- a/include/linux/gfp_types.h +++ b/include/linux/gfp_types.h @@ -382,4 +382,8 @@ enum { __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM) #define GFP_TRANSHUGE (GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM) =20 +/* Helper macro to avoid gfp flags if they are the default one */ +#define __default_gfp(a,b,...) b +#define default_gfp(...) __default_gfp(,##__VA_ARGS__,GFP_KERNEL) + #endif /* __LINUX_GFP_TYPES_H */ --=20 2.34.1 From nobody Fri Sep 25 09:19:45 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D66773F6610; Mon, 14 Sep 2026 22:32:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789425142; cv=none; b=sNzS/M0s8SzkM+H7V2QzyWTsIxSU8AQlQSABugFlCWaQ2gOwhv+7yUcgvx1wdB410Jt6+JspeJvw+vKTDYUiXJnh61GqZyomLljuKkpg3gx8Jx4hj9Gb1P4x/i5+6z7//g9W4gXk43hVySM2vbO6q8upwilaiwcXxNYc5ap4yeU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789425142; c=relaxed/simple; bh=bLnfQpX9WteOicTADYo7gVjLmdCzHpRn4PMQO2GTuCM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FbVfocRli5YnjYvNPpqpDXi3XY5qS34H/uhH3vlQJrsOtrO5N4EIiPexW/1o9aHj2bhoicIuFE8QMpaWVepQ5ftSUlFItfQI1H+9RCqQqqLbrjzo/H/q/xIW5QZCyJg0/CdRYmgR7mYTgMjkxhWhtGg+LB0SxuXYFjZVRrAJ+co= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nRlV+Ypv; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nRlV+Ypv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 726D81F00898; Mon, 14 Sep 2026 22:32:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789425139; bh=Ps1Pf9HY55XJc6hUxZI1FWG+aZ4DlvyxQbmpujBPK50=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=nRlV+YpvxHQVtVRWHIbygpj+OZVvjJfHQQnKrKTOJhBpvRhGTy4ZjBujWyve0WKGA w435enXlS2HHNW1DJmrDvB02MD6uRMC6FSFxadfIWa4zRR51XZvQaQIMcvWDnDANdK t6MJ+Lyt4Agr05qu9eBxp3Rnm5l6crb30IfsRXAQZzW6+6Lkipmre5C0QlWeXisZfk 8GjeaR5kxEVJ3pl+ciP2lL9/G5ZTVB3ItA5KJXcFFls+TjRVi0i9raU19+EJQaJ8va 4j2eI+QyO/ZBjqiE7S4f+jixrGcNW+DqjWKSXbZULTLTyuTiEciTjWbik/faUHtNBt juvas0SA0gB0g== From: Kees Cook To: Greg Kroah-Hartman Cc: Kees Cook , "Rafael J. Wysocki" , Danilo Krummrich , "Gustavo A. R. Silva" , driver-core@lists.linux.dev, linux-hardening@vger.kernel.org, Lorenzo Stoakes , Andrew Morton , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Zi Yan , David Hildenbrand , "Liam R. Howlett" , Mike Rapoport , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 2/2] devres: Provide kmalloc_obj*-style API for devm_kmalloc Date: Mon, 14 Sep 2026 15:32:17 -0700 Message-Id: <20260914223219.901347-2-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914223212.i.829-kees@kernel.org> References: <20260914223212.i.829-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5602; i=kees@kernel.org; h=from:subject; bh=bLnfQpX9WteOicTADYo7gVjLmdCzHpRn4PMQO2GTuCM=; b=owGbwMvMwCVmps19z/KJym7G02pJDFkrSj8WbJVbIJ/P8uXHLbbr6tu0z805uPEx7//mnV9eT GtRNGZ72FHKwiDGxSArpsgSZOce5+Lxtj3cfa4izBxWJpAhDFycAjCRpTMZGZ7Vnl9ktbGh5Md7 u7/cSyXbtJLZ7+UYRtnb/53SLrJg6QSG/8XWj8wN7068lWp5hqnkg7LBmYTp7plVikKWzI0Vkz/ asgMA X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Mirroring the type-based allocation now in use for the kmalloc family of APIs, provide the same for the devm_kmalloc family of APIs. This will allow for future plumbing of type information (e.g. alignment) into the underlying allocations. This immediately gains type check on return values (instead of the prior "void *" return type). Signed-off-by: Kees Cook --- Cc: Greg Kroah-Hartman Cc: "Rafael J. Wysocki" Cc: Danilo Krummrich Cc: "Gustavo A. R. Silva" Cc: Cc: --- include/linux/device/devres.h | 102 ++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/include/linux/device/devres.h b/include/linux/device/devres.h index 14ab9159bdda..7656c9ecd4da 100644 --- a/include/linux/device/devres.h +++ b/include/linux/device/devres.h @@ -62,6 +62,108 @@ static inline void *devm_kcalloc(struct device *dev, si= ze_t n, size_t size, gfp_ { return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO); } + +/** + * __devm_alloc_objs - Device-managed allocation of objects of a given type + * @DEVM_ALLOC: which device-managed size-based allocator to use. + * @DEV: device to scope the allocation lifetime to. + * @GFP: GFP flags for the allocation. + * @TYPE: type to allocate space for. + * @COUNT: how many @TYPE objects to allocate. + * + * Device-managed counterpart of __alloc_objs(). + * @DEVM_ALLOC takes (@DEV, size, gfp). + * + * Returns: Newly allocated pointer to (first) @TYPE of @COUNT-many alloca= ted + * @TYPE objects, or NULL on failure. + */ +#define __devm_alloc_objs(DEVM_ALLOC, DEV, GFP, TYPE, COUNT) \ +({ \ + const size_t __obj_size =3D size_mul(sizeof(TYPE), COUNT); \ + (TYPE *)DEVM_ALLOC(DEV, __obj_size, GFP); \ +}) + +/** + * devm_kmalloc_obj - Device-managed allocation of a single typed object + * @DEV: Device to scope the allocation lifetime to. + * @VAR_OR_TYPE: Variable or type to allocate. + * @GFP: Optional GFP flags (defaults to %GFP_KERNEL). + * + * Device-managed counterpart of kmalloc_obj(): the allocation is sized fr= om + * @VAR_OR_TYPE and returned as a pointer to that type, not void *. + * + * Returns: newly allocated pointer to a @VAR_OR_TYPE on success, or NULL = on + * failure. + */ +#define devm_kmalloc_obj(DEV, VAR_OR_TYPE, ...) \ + __devm_alloc_objs(devm_kmalloc, DEV, default_gfp(__VA_ARGS__), \ + typeof(VAR_OR_TYPE), 1) + +/** + * devm_kmalloc_objs - Device-managed allocation of an array of a typed ob= ject + * @DEV: Device to scope the allocation lifetime to. + * @VAR_OR_TYPE: Variable or type to allocate an array of. + * @COUNT: How many elements in the array. + * @GFP: Optional GFP flags (defaults to %GFP_KERNEL). + * + * Returns: newly allocated pointer to an array of @VAR_OR_TYPE on success= , or + * NULL on failure (including when the total size overflows). + */ +#define devm_kmalloc_objs(DEV, VAR_OR_TYPE, COUNT, ...) \ + __devm_alloc_objs(devm_kmalloc, DEV, default_gfp(__VA_ARGS__), \ + typeof(VAR_OR_TYPE), COUNT) + +/** + * __devm_alloc_flex - Device-managed allocation of a trailing-flex-array = object + * @DEVM_ALLOC: which device-managed allocator to use. + * @DEV: device to scope the allocation lifetime to. + * @GFP: GFP flags for the allocation. + * @TYPE: type of structure to allocate space for. + * @FAM: name of the flexible array member of @TYPE. + * @COUNT: how many @FAM elements to allocate space for. + * + * Device-managed counterpart of __alloc_flex(). + * @DEVM_ALLOC takes (@DEV, size, gfp). + * + * Returns: Newly allocated pointer to @TYPE with @COUNT-many trailing @FAM + * elements, or NULL on failure. + */ +#define __devm_alloc_flex(DEVM_ALLOC, DEV, GFP, TYPE, FAM, COUNT) \ +({ \ + const size_t __count =3D (COUNT); \ + const size_t __obj_size =3D struct_size_t(TYPE, FAM, __count); \ + TYPE *__obj_ptr =3D DEVM_ALLOC(DEV, __obj_size, GFP); \ + if (__obj_ptr) \ + __set_flex_counter(__obj_ptr->FAM, __count); \ + __obj_ptr; \ +}) + +/** + * devm_kmalloc_flex - Device-managed allocation of a flexible-array struc= ture + * @DEV: Device to scope the allocation lifetime to. + * @VAR_OR_TYPE: Variable or type to allocate (with its flexible array). + * @FAM: The name of the flexible array member of the structure. + * @COUNT: How many flexible array member elements to allocate. + * @GFP: Optional GFP flags (defaults to %GFP_KERNEL). + * + * Returns: newly allocated pointer to @VAR_OR_TYPE on success, or NULL on + * failure. If @FAM has been annotated with __counted_by(), its counter is= set + * to @COUNT. + */ +#define devm_kmalloc_flex(DEV, VAR_OR_TYPE, FAM, COUNT, ...) \ + __devm_alloc_flex(devm_kmalloc, DEV, default_gfp(__VA_ARGS__), \ + typeof(VAR_OR_TYPE), FAM, COUNT) + +/* All devm_kzalloc aliases for devm_kmalloc_(obj|objs|flex). */ +#define devm_kzalloc_obj(DEV, P, ...) \ + __devm_alloc_objs(devm_kzalloc, DEV, default_gfp(__VA_ARGS__), typeof(P),= 1) +#define devm_kzalloc_objs(DEV, P, COUNT, ...) \ + __devm_alloc_objs(devm_kzalloc, DEV, default_gfp(__VA_ARGS__), \ + typeof(P), COUNT) +#define devm_kzalloc_flex(DEV, P, FAM, COUNT, ...) \ + __devm_alloc_flex(devm_kzalloc, DEV, default_gfp(__VA_ARGS__), \ + typeof(P), FAM, COUNT) + static inline __realloc_size(3, 4) void * __must_check devm_krealloc_array(struct device *dev, void *p, size_t new_n, size_t new_= size, gfp_t flags) { --=20 2.34.1