linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [patch RFC] mm/slab: introduce KZALLOC_FREE() cleanup-ed allocation macro
@ 2024-03-15 13:22 Jiri Pirko
  2024-03-15 21:39 ` David Rientjes
  2024-03-21 16:26 ` Re " Przemek Kitszel
  0 siblings, 2 replies; 6+ messages in thread
From: Jiri Pirko @ 2024-03-15 13:22 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-mm, cl, penberg, rientjes, iamjoonsoo.kim, akpm, vbabka,
	roman.gushchin, 42.hyeyoo, peterz, mingo, danielj

From: Jiri Pirko <jiri@nvidia.com>

With introduction of __free() macro using cleanup infrastructure, it
will very likely become quite common to see following pattern:
	type *var __free(kfree) = kzalloc(sizeof(*var), GFP_KERNEL);

To follow the CLASS() flow from cleanup.h, introduce a simple macro
KZALLOC_FREE() to wrap this over and allow the same flow.

Show an example usage in gpio-sim driver.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
---
 drivers/gpio/gpio-sim.c | 3 +--
 include/linux/slab.h    | 3 +++
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
index c4106e37e6db..997237b3d80c 100644
--- a/drivers/gpio/gpio-sim.c
+++ b/drivers/gpio/gpio-sim.c
@@ -1496,8 +1496,7 @@ gpio_sim_config_make_device_group(struct config_group *group, const char *name)
 {
 	int id;
 
-	struct gpio_sim_device *dev __free(kfree) = kzalloc(sizeof(*dev),
-							    GFP_KERNEL);
+	KZALLOC_FREE(struct gpio_sim_device *, dev, GFP_KERNEL);
 	if (!dev)
 		return ERR_PTR(-ENOMEM);
 
diff --git a/include/linux/slab.h b/include/linux/slab.h
index b5f5ee8308d0..baee6acd58d3 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -711,6 +711,9 @@ static inline __alloc_size(1) void *kzalloc(size_t size, gfp_t flags)
 	return kmalloc(size, flags | __GFP_ZERO);
 }
 
+#define KZALLOC_FREE(_type, var, _gfp_t)				\
+	_type var __free(kfree) = kzalloc(sizeof(*var), _gfp_t)
+
 /**
  * kzalloc_node - allocate zeroed memory from a particular memory node.
  * @size: how many bytes of memory are required.
-- 
2.44.0



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-03-27  7:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-15 13:22 [patch RFC] mm/slab: introduce KZALLOC_FREE() cleanup-ed allocation macro Jiri Pirko
2024-03-15 21:39 ` David Rientjes
2024-03-16 15:41   ` Jiri Pirko
2024-03-21 16:26 ` Re " Przemek Kitszel
2024-03-25 19:00   ` Dan Williams
2024-03-27  7:23     ` Przemek Kitszel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox