From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: mfe@pengutronix.de, Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/5] malloc: add __free() cleanup handlers
Date: Tue, 22 Sep 2026 12:47:55 +0200 [thread overview]
Message-ID: <20260922104813.317584-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20260922104813.317584-1-a.fatoum@pengutronix.de>
The cleanup infrastructure was added to barebox a while back in
commit 332e3542e5ed ("Port Linux __cleanup() based guard
infrastructure"), but without any DEFINE_FREE() handlers, so nothing
could be passed to __free() so far.
Let's start with malloc and kmalloc, so buffers can be tied to the
scope they are used in:
void *buf __free(free) = malloc(size);
void *foo __free(kfree) = kzalloc(size, GFP_KERNEL);
[k]free_sensitive() looks up the usable size of the buffer before
zeroing it, so it gets the same test rather than just a NULL check
to avoid an error pointer dereference inside the allocator.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
include/linux/slab.h | 5 +++++
include/malloc.h | 5 +++++
2 files changed, 10 insertions(+)
diff --git a/include/linux/slab.h b/include/linux/slab.h
index 93ce25a58299..9ccb8c6a0101 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -4,6 +4,8 @@
#define _LINUX_SLAB_H
#include <dma.h>
+#include <linux/cleanup.h>
+#include <linux/err.h>
#include <linux/overflow.h>
#include <linux/string.h>
#include <linux/gfp.h>
@@ -116,4 +118,7 @@ static inline char *kstrdup(const char *str, gfp_t flags)
#define kstrdup_const(str, flags) strdup_const(str)
+DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T))
+DEFINE_FREE(kfree_sensitive, void *, if (!IS_ERR_OR_NULL(_T)) kfree_sensitive(_T))
+
#endif /* _LINUX_SLAB_H */
diff --git a/include/malloc.h b/include/malloc.h
index 82fa2bb39c8e..b7f1aaac95d2 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -2,7 +2,9 @@
#ifndef __MALLOC_H
#define __MALLOC_H
+#include <linux/cleanup.h>
#include <linux/compiler.h>
+#include <linux/err.h>
#include <types.h>
#define MALLOC_SHIFT_MAX 30
@@ -81,6 +83,9 @@ static inline int mem_malloc_is_initialized(void)
}
#endif
+DEFINE_FREE(free, void *, if (!IS_ERR_OR_NULL(_T)) free(_T))
+DEFINE_FREE(free_sensitive, void *, if (!IS_ERR_OR_NULL(_T)) free_sensitive(_T))
+
static inline bool want_init_on_alloc(void)
{
return IS_ENABLED(CONFIG_INIT_ON_ALLOC_DEFAULT_ON);
--
2.47.3
next prev parent reply other threads:[~2026-09-22 10:49 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-22 10:47 [PATCH 0/5] " Ahmad Fatoum
2026-09-22 10:47 ` [PATCH 1/5] commands: rksecure: exit if invalid integer passed to -b Ahmad Fatoum
2026-09-22 10:47 ` [PATCH 2/5] firmware-zynqmp: don't ignore the kstrtouint() result in parse_reg() Ahmad Fatoum
2026-09-22 10:47 ` [PATCH 3/5] include: compiler: make __must_check a yes-op Ahmad Fatoum
2026-09-22 10:47 ` Ahmad Fatoum [this message]
2026-09-22 10:47 ` [PATCH 5/5] test: self: add cleanup selftest Ahmad Fatoum
2026-09-23 6:33 ` [PATCH 0/5] malloc: add __free() cleanup handlers Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260922104813.317584-5-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=mfe@pengutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox