[PATCH v2 26/31] mm: Add __free() support for __free_page()

Xu Yilun
2026-03-27T16:24:09Z

Allow for the declaration of struct page * variables that trigger
__free_page() when they go out of scope.

A example usage would be in the following patch:

static struct pci_tsm *tdx_tsm_link_pf0_probe(...)
{
...

struct page *in_msg_page __free(__free_page) =
alloc_page(GFP_KERNEL | __GFP_ZERO);
if (!in_msg_page)
return NULL;

struct page *out_msg_page __free(__free_page) =
alloc_page(GFP_KERNEL | __GFP_ZERO);
if (!out_msg_page)
return NULL;

...

tlink->in_msg = no_free_ptr(in_msg_page);
tlink->out_msg = no_free_ptr(out_msg_page);

...
}

Signed-off-by: Xu Yilun <yilun.xu@linux.intel.com>
Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
---
include/linux/gfp.h | 1 +
1 file changed, 1 insertion(+)

diff --git a/include/linux/gfp.h b/include/linux/gfp.h
index 51ef13ed756e..d37e5564234e 100644
--- a/include/linux/gfp.h
+++ b/include/linux/gfp.h
@@ -391,6 +391,7 @@ extern void free_pages_nolock(struct page *page, unsigned int order);
extern void free_pages(unsigned long addr, unsigned int order);

#define __free_page(page) __free_pages((page), 0)
+DEFINE_FREE(__free_page, struct page *, if (_T) __free_page(_T))
#define free_page(addr) free_pages((addr), 0)

void page_alloc_init_cpuhp(void);
--
2.25.1