From: Christian Marangi <ansuelsmth@gmail.com>
Date: Mon, 14 Apr 2025 18:04:25 +0200
Subject: [PATCH 1/2] mm: permit to declare custom execmem alloc/free function

Permit to declare custom execmem alloc/free function that bypass the
execmem API. This works by making the alloc/free function weak
permitting an arch to declare a replacement for them.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Co-authored-by: Shiji Yang <yangshiji66@outlook.com>
---
 include/linux/moduleloader.h |  4 ++++
 mm/execmem.c                 | 14 ++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

--- a/include/linux/moduleloader.h
+++ b/include/linux/moduleloader.h
@@ -122,4 +122,8 @@ void module_arch_cleanup(struct module *
 /* Any cleanup before freeing mod->module_init */
 void module_arch_freeing_init(struct module *mod);
 
+enum execmem_type;
+void *arch_execmem_alloc(enum execmem_type type, size_t size);
+void arch_execmem_free(void *ptr);
+
 #endif
--- a/mm/execmem.c
+++ b/mm/execmem.c
@@ -52,14 +52,19 @@ static void *__execmem_alloc(struct exec
 	return kasan_reset_tag(p);
 }
 
-void *execmem_alloc(enum execmem_type type, size_t size)
+void *__weak arch_execmem_alloc(enum execmem_type type, size_t size)
 {
 	struct execmem_range *range = &execmem_info->ranges[type];
 
 	return __execmem_alloc(range, size);
 }
 
-void execmem_free(void *ptr)
+void *execmem_alloc(enum execmem_type type, size_t size)
+{
+	return arch_execmem_alloc(type, size);
+}
+
+void __weak arch_execmem_free(void *ptr)
 {
 	/*
 	 * This memory may be RO, and freeing RO memory in an interrupt is not
@@ -69,6 +74,11 @@ void execmem_free(void *ptr)
 	vfree(ptr);
 }
 
+void execmem_free(void *ptr)
+{
+	arch_execmem_free(ptr);
+}
+
 static bool execmem_validate(struct execmem_info *info)
 {
 	struct execmem_range *r = &info->ranges[EXECMEM_DEFAULT];
