Skip to contentSkip to conversation

devres: Provide kmalloc_obj*-style API for devm_kmalloc

PATCH In review Kees Cook 10 participants 17 messages Sep 14 – Nov 12

Subsystems (7)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

cover devres: Provide kmalloc_obj*-style API for devm_kmalloc

Hi,

As a continuation of the type-based allocator work, this introduces the wrappers for devm_kmalloc()-family to use the _obj/_objs/_flex API that the kmalloc()-family has moved to. Along with the coming changes to scripts/coccinelle/api/kmalloc_objs.cocci to support devm_kmalloc() conversions, this will result in the following uses:

            6644    devm_kzalloc_obj()
            1714    devm_kzalloc_objs()
             296    devm_kzalloc_flex()
              89    devm_kmalloc_objs()
              83    devm_kmalloc_obj()
               2    devm_kmalloc_flex()

which totals out in diffstat to:

 5908 files changed, 10013 insertions(+), 12082 deletions(-)

-Kees

Kees Cook (2):
mm: Move default_gfp() into gfp_types.h
devres: Provide kmalloc_obj*-style API for devm_kmalloc

 include/linux/device/devres.h | 102 ++++++++++++++++++++++++++++++++++
 include/linux/gfp.h           |   4 --
 include/linux/gfp_types.h     |   4 ++
 3 files changed, 106 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 mm: Move default_gfp() into gfp_types.h

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.

---
Cc: Lorenzo Stoakes <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: "Liam R. Howlett" <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Brendan Jackman <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: <[email protected]>

---
 include/linux/gfp.h       | 4 ----
 include/linux/gfp_types.h | 4 ++++
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/gfp.h +0 -4
index 872bc53f32ec..b62e2fd53cb4 100644
@@ -13,10 +13,6 @@
 struct vm_area_struct;
 struct mempolicy;
 
-/* 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);
include/linux/gfp_types.h +4 -0
index 190191411009..b1cd143f3c42 100644
@@ -382,4 +382,8 @@ enum {
 			 __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM)
 #define GFP_TRANSHUGE	(GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM)
 
+/* 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 */
Signature · 1 line
2.34.1
reviewer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] mm: Move default_gfp() into gfp_types.h

Quoted 18 lines · Kees Cook

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.

---
Cc: Lorenzo Stoakes <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: "Liam R. Howlett" <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Brendan Jackman <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: <[email protected]>

Quoted 5 lines
---
 include/linux/gfp.h       | 4 ----
 include/linux/gfp_types.h | 4 ++++
 2 files changed, 4 insertions(+), 4 deletions(-)

Makes sense.

Signature · 2 lines
Best Regards,
Yan, Zi
maintainer Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] mm: Move default_gfp() into gfp_types.h

In reply to Kees Cook
Quoted 5 lines · Kees Cook

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.

Incidentally I was thinking if default_gfp(...) usage should be extended also for the legacy kmalloc() API. Because it's going to still have users, buffers are needed for many things that are not objects (structs). Mike is converting many unnecessary page allocator usages to kmalloc(PAGE_SIZE, ...) often with GFP_KERNEL, so that would be neater too...

Quoted 46 lines

---
Cc: Lorenzo Stoakes <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: "Liam R. Howlett" <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Brendan Jackman <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: <[email protected]>

---
 include/linux/gfp.h       | 4 ----
 include/linux/gfp_types.h | 4 ++++
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/gfp.h +0 -4
index 872bc53f32ec..b62e2fd53cb4 100644
@@ -13,10 +13,6 @@
 struct vm_area_struct;
 struct mempolicy;
 
-/* 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);
include/linux/gfp_types.h +4 -0
index 190191411009..b1cd143f3c42 100644
@@ -382,4 +382,8 @@ enum {
 			 __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM)
 #define GFP_TRANSHUGE	(GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM)
 
+/* 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 */
author
#5 lore raw

PATCH 2/2 devres: Provide kmalloc_obj*-style API for devm_kmalloc

In reply to Kees Cook

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).

---
Cc: Greg Kroah-Hartman <[email protected]>
Cc: "Rafael J. Wysocki" <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 include/linux/device/devres.h | 102 ++++++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)
include/linux/device/devres.h +102 -0
index 14ab9159bdda..7656c9ecd4da 100644
@@ -62,6 +62,108 @@ static inline void *devm_kcalloc(struct device *dev, size_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 allocated
+ * @TYPE objects, or NULL on failure.
+ */
+#define __devm_alloc_objs(DEVM_ALLOC, DEV, GFP, TYPE, COUNT)		\
+({									\
+	const size_t __obj_size = 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 from
+ * @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 object
+ * @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 = (COUNT);					\
+	const size_t __obj_size = struct_size_t(TYPE, FAM, __count);	\
+	TYPE *__obj_ptr = 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 structure
+ * @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)
 {
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 2/2
#6 lore raw
Quoted 14 lines · Kees Cook

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).

---
Cc: Greg Kroah-Hartman <[email protected]>
Cc: "Rafael J. Wysocki" <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

Quoted 3 lines
---
 include/linux/device/devres.h | 102 ++++++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)

I'm guessing you want this to go through your tree? If so, that's great:

Otherwise let us know and we can take it through the driver core tree.

thanks,

greg k-h

author
#7 lore raw
Quoted 20 lines · Greg Kroah-Hartman
Quoted 14 lines · Kees Cook

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).

---
Cc: Greg Kroah-Hartman <[email protected]>
Cc: "Rafael J. Wysocki" <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

Quoted 3 lines
---
 include/linux/device/devres.h | 102 ++++++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)
Quoted 4 lines

I'm guessing you want this to go through your tree? If so, that's great:

Awesome; thank you! I'll get it into -next via the hardening tree.

Signature · 1 line
Kees Cook
maintainer Reviewed-by on v1 2/2
#8 lore raw
In reply to Kees Cook
Quoted 7 lines · Kees Cook

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).

maintainer
#9 lore raw
In reply to Kees Cook

Hi Kees,

Quoted 114 lines · Kees Cook

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).

---
Cc: Greg Kroah-Hartman <[email protected]>
Cc: "Rafael J. Wysocki" <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 include/linux/device/devres.h | 102 ++++++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)
include/linux/device/devres.h +88 -0
index 14ab9159bdda..7656c9ecd4da 100644
@@ -62,6 +62,108 @@ static inline void *devm_kcalloc(struct device *dev, size_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 allocated
+ * @TYPE objects, or NULL on failure.
+ */
+#define __devm_alloc_objs(DEVM_ALLOC, DEV, GFP, TYPE, COUNT)		\
+({									\
+	const size_t __obj_size = 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 from
+ * @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 object
+ * @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 = (COUNT);					\
+	const size_t __obj_size = struct_size_t(TYPE, FAM, __count);	\
+	TYPE *__obj_ptr = 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 structure
+ * @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.
+ */

include/linux/device/devres.h static inline void *devm_kcalloc(struct device *dev, size_t n, size_t size, gfp_ · line 162

Quoted 10 lines
include/linux/device/devres.h +10 -0
+#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)

I was recently made aware by Geert of an unfortunate side effect of the default GPF_KERNEL argument. I've fixed a memory leak in the imx8-isi driver by using devm_kzalloc_objs() as follows:

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +2 -1
index 0ae5f35e017c..9373a5256224 100644
@@ -488,7 +488,8 @@ static int mxc_isi_probe(struct platform_device *pdev)
 
 	isi->pdata = of_device_get_match_data(dev);
 
-	isi->pipes = kzalloc_objs(isi->pipes[0], isi->pdata->num_channels);
+	isi->pipes = devm_kzalloc_objs(dev, isi->pipes[0],
+				       isi->pdata->num_channels);
 	if (!isi->pipes)
 		return -ENOMEM;
 

So far, so good. However, the following code also compiles:

	isi->pipes = devm_kzalloc_obj(dev, isi->pipes[0],
				      isi->pdata->num_channels);

(note the *obj* instead of *objs*) because the third argument is interpreted as gfp flags. It's clearly a bug, but one that the compiler doesn't catch. The typo is easy to make.

Sparse catches the issue:

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c:491:22: warning: incorrect type in argument 3 (different base types)
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c:491:22: expected restricted gfp_t [usertype] gfp
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c:491:22: got unsigned int const num_channels

I'm however not sure if build bots report sparse warnings or only errors.

Is this an issue you're aware of, and do you think it should be addressed ?

Quoted 7 lines
include/linux/device/devres.h +4 -0
+#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)
 {
Signature · 3 lines
Regards,

Laurent Pinchart
#10 lore raw

[CC += Linus]

Hi Laurent, Linus,

Quoted 24 lines

Date: 2026-11-12 02:40:39+0200
From: Laurent Pinchart <[email protected]>

Hi Kees,

Quoted 18 lines · Kees Cook

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).

---
Cc: Greg Kroah-Hartman <[email protected]>
Cc: "Rafael J. Wysocki" <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 include/linux/device/devres.h | 102 ++++++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)

[...]

Quoted 44 lines

Review

Quoted 10 lines
(quoted diff) +10 -0
+#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)

I was recently made aware by Geert of an unfortunate side effect of the default GPF_KERNEL argument. I've fixed a memory leak in the imx8-isi driver by using devm_kzalloc_objs() as follows:

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +2 -1
index 0ae5f35e017c..9373a5256224 100644
@@ -488,7 +488,8 @@ static int mxc_isi_probe(struct platform_device *pdev)
 
 	isi->pdata = of_device_get_match_data(dev);
 
-	isi->pipes = kzalloc_objs(isi->pipes[0], isi->pdata->num_channels);
+	isi->pipes = devm_kzalloc_objs(dev, isi->pipes[0],
+				       isi->pdata->num_channels);
 	if (!isi->pipes)
 		return -ENOMEM;
 

So far, so good. However, the following code also compiles:

	isi->pipes = devm_kzalloc_obj(dev, isi->pipes[0],
				      isi->pdata->num_channels);

(note the *obj* instead of *objs*) because the third argument is interpreted as gfp flags. It's clearly a bug, but one that the compiler doesn't catch. The typo is easy to make.

Sparse catches the issue:

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c:491:22: warning: incorrect type in argument 3 (different base types)
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c:491:22: expected restricted gfp_t [usertype] gfp
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c:491:22: got unsigned int const num_channels

Quoted 5 lines

I'm however not sure if build bots report sparse warnings or only errors.

Is this an issue you're aware of, and do you think it should be addressed ?

Indeed, this is a known problem of these macros. I reported it in March:
<https://lore.kernel.org/lkml/abhGS0n_RsUG97Ni@devuan/>

The link contains a detailed report of the issue, and why it happens.

There is not a single cause of this bug, but rather a combination of several of them.

I suggest removing kmalloc_obj() entirely, and using kmalloc_objs() always, with a second argument of 1 when we want a single object. This would remove the risk of that typo. That is, instead of:

    int *p = kmalloc_objs(int, 42);
    int *q = kmalloc_obj(int);

we should do

    int *p = kmalloc_objs(int, 42);
    int *q = kmalloc_objs(int, 1);

I also suggest having default arguments without variadic macros. The way to do that is using empty arguments. Linus complained that it hurts his eyes, but it really makes these macros much safer.

That is, instead of

	int *p = kmalloc_objs(int, 42);

we should do

	int *p = kmalloc_objs(int, 42,);

By having an explicitly empty third parameter, we'd be asking explicitly for the default value, which the compiler can verify, compared to having to guess whether we asked for it or we just forgot about its existence.

Any of these two changes would already reduce the chances of this bug significantly, but if we apply both, it's almost impossible to have it.

Another issue with that macro is accepting an expression instead of a type name. Consider the following mistake:

    p = kmalloc_objs(42, *p);

The compiler can't catch that we swapped the order of arguments accidentally, and will allocate a wrong number of objects (whatever happens to be in *p, if already initialized, else we might be lucky and get an uninitialized diagnostic). If we require that the first parameter is a type name, it will preclude that problem.

Please let me know if any of these changes is wanted, and I can prepare some patches.

Have a lovely day!
Alex

Quoted 14 lines
Quoted 8 lines
(quoted diff) +4 -0
+#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)
 {
Signature · 3 lines
Regards,

Laurent Pinchart
Signature · 1 line
<https://www.alejandro-colomar.es>
#11 lore raw
Quoted 8 lines · Alejandro Colomar

I suggest removing kmalloc_obj() entirely, and using kmalloc_objs() always, with a second argument of 1 when we want a single object. This would remove the risk of that typo.

I also suggest having default arguments without variadic macros. The way to do that is using empty arguments. Linus complained that it hurts his eyes, but it really makes these macros much safer.

No to both. These are both "make the code worse to catch errors that the compiler should have been able to catch anyway".

If you *really* want to catch them, make "gfp_t" be a 'struct'. That way even compilers without things like the restricted types that sparse has will catch at least that one.

I did that many many years (argh, decades - I'm old) ago to catch all the 'pte_t' etc issues when rewriting the MM layer, and it works very well.

It's going to be somewhat painful, but ti might be worth it. People who only use GFP_xyz flags to pass them around wouldn't notice.

Linus

author
#12 lore raw
Quoted 18 lines · Linus Torvalds
Quoted 8 lines · Alejandro Colomar

I suggest removing kmalloc_obj() entirely, and using kmalloc_objs() always, with a second argument of 1 when we want a single object. This would remove the risk of that typo.

I also suggest having default arguments without variadic macros. The way to do that is using empty arguments. Linus complained that it hurts his eyes, but it really makes these macros much safer.

No to both. These are both "make the code worse to catch errors that the compiler should have been able to catch anyway".

If you *really* want to catch them, make "gfp_t" be a 'struct'. That way even compilers without things like the restricted types that sparse has will catch at least that one.

Quoted 6 lines

I did that many many years (argh, decades - I'm old) ago to catch all the 'pte_t' etc issues when rewriting the MM layer, and it works very well.

It's going to be somewhat painful, but ti might be worth it. People who only use GFP_xyz flags to pass them around wouldn't notice.

One thing Justin was poking at was adding a "strict" mode to typedef that gets us a new native type that follows all the same rules as its underlying type, but refuses implicit casting. That way you get stuff like gfp_t or pid_t that still works sanely but refuses to take values from non-matching types (i.e. it would catch this case). It's handy for the Overflow Behavior Type work too (for similar reasons). e.g.:

typedef __distinct unsigned int pid_t;

or something like that. Then you don't need the accessors like we have for the struct-style wrappers.

-Kees

Signature · 1 line
Kees Cook
#13 lore raw
Quoted 2 lines · Kees Cook

One thing Justin was poking at was adding a "strict" mode to typedef

Honestly, the sparse setup is superior. It does that 'strict' thing (except it calls it 'nocast') _and_ it has 'bitwise' that is a nocast that also disallows non-bit type checks (which is *very* useful for things like "this is a big-endian value: you can only do bitwise checks against other big-endian values).

So the compiler addition may be worth it, but even the "poking at" is weaker than what sparse already does.

And the 'struct' model is honestly superior to both when it works - but can add extra overhead (but can also end up being nicer than both:
instead of casting with special "I really mean it" casts, you can derefence the struct and get its value that way)

Linus

#14 lore raw

Hi,

Quoted 9 lines · Linus Torvalds
Quoted 2 lines · Kees Cook

One thing Justin was poking at was adding a "strict" mode to typedef

Honestly, the sparse setup is superior. It does that 'strict' thing (except it calls it 'nocast') _and_ it has 'bitwise' that is a nocast that also disallows non-bit type checks (which is *very* useful for things like "this is a big-endian value: you can only do bitwise checks against other big-endian values).

Cool. I wasn't super familiar with how sparse handled nocast and bitwise but your insights led me to look into it deeper. I'll take these learnings over to clang when I get around to implementing strict types :)

Quoted 3 lines

So the compiler addition may be worth it, but even the "poking at" is weaker than what sparse already does.

Right but I suppose the benefit is that compiler-enforced things are caught vastly more often than sparse-enforced things. Plus, like I said above, maybe I can aim to reach relative feature parity with sparse's own analysis on this type of stuff.

FYI, there's some prior design with strong typedefs [1] in the form of a wg14 proposal. I've also been keeping a close eye on that stuff as it develops because it's essentially strict/distinct types with a different name.

[1]: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3320.htm

Justin

#15 lore raw
In reply to Linus Torvalds

Hi Linus,

Quoted 10 lines

Date: 2026-11-12 08:34:13-0800
From: Linus Torvalds <[email protected]>

Quoted 5 lines · Alejandro Colomar

I suggest removing kmalloc_obj() entirely, and using kmalloc_objs() always, with a second argument of 1 when we want a single object. This would remove the risk of that typo.

Quoted 6 lines
Quoted 3 lines

I also suggest having default arguments without variadic macros. The way to do that is using empty arguments. Linus complained that it hurts his eyes, but it really makes these macros much safer.

No to both. These are both "make the code worse to catch errors that the compiler should have been able to catch anyway".

I don't agree that the code is worse. It's _different_, just like Rust forces one to write different code in order to make certain bugs trivial to reject. But I don't think having to write malloc_objs(T, 1) is objectively worse than malloc_obj(T).

But let's agree to disagree.

Quoted 3 lines

If you *really* want to catch them, make "gfp_t" be a 'struct'. That way even compilers without things like the restricted types that sparse has will catch at least that one.

Yes, that's a third alternative, which Kees and I discussed some months ago in Prague as a mitigation.

Quoted 6 lines

I did that many many years (argh, decades - I'm old) ago to catch all the 'pte_t' etc issues when rewriting the MM layer, and it works very well.

It's going to be somewhat painful, but ti might be worth it. People who only use GFP_xyz flags to pass them around wouldn't notice.

Indeed, I don't volunteer for writing this patch. :) But yeah, it would also work.

Have a lovely night!
Alex

Signature · 1 line
<https://www.alejandro-colomar.es>
#16 lore raw
Quoted 3 lines · Alejandro Colomar

I don't agree that the code is worse. It's _different_, just like Rust forces one to write different code

different *is* worse. We do not want to have random strange syntax that differs from all normal rules for some little unimportant detail when I already outlined something that has standard C syntax and is superior from a technical angle.

Code needs to be legible, and that means that it should follow a pattern that people are used to, not have random single-byte markers.

The goal is to be readable to *HUMANS*. And no, removing the (more common) function to allocate just a single object in favor of using a "1" is not human-friendly. And adding a pointless "," to make a compiler think it's not a missing argument is not human-friendly.

And honestly, I don't think Rust is a great example of human-friendly either.

Linus