arm64: module: remove unneeded call to kasan_alloc_module_shadow()
Subsystems (28)
Mike Rapoport
maintainerPATCH v5 1/15 arm64: module: remove unneeded call to kasan_alloc_module_shadow()
From: "Mike Rapoport (IBM)" <[email protected]>
Since commit f6f37d9320a1 ("arm64: select KASAN_VMALLOC for SW/HW_TAGS modes") KASAN_VMALLOC is always enabled when KASAN is on. This means that allocations in module_alloc() will be tracked by KASAN protection for vmalloc() and that kasan_alloc_module_shadow() will be always an empty inline and there is no point in calling it.
Drop meaningless call to kasan_alloc_module_shadow() from module_alloc().
--- arch/arm64/kernel/module.c | 5 ----- 1 file changed, 5 deletions(-)
arch/arm64/kernel/module.c +0 -5
@@ -141,11 +141,6 @@ void *module_alloc(unsigned long size) __func__); } -if (p && (kasan_alloc_module_shadow (p, size,GFP_KERNEL ) <0 )) { -vfree (p); -return NULL ; - } -/* Memory is intended to be executable, reset the pointer tag. */ return kasan_reset_tag (p); }
Signature · 1 line
2.43.0
Mike Rapoport
maintainerPATCH v5 2/15 mips: module: rename MODULE_START to MODULES_VADDR
From: "Mike Rapoport (IBM)" <[email protected]>
and MODULE_END to MODULES_END to match other architectures that define custom address space for modules.
--- arch/mips/include/asm/pgtable-64.h | 4 ++-- arch/mips/kernel/module.c | 4 ++-- arch/mips/mm/fault.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
arch/mips/include/asm/pgtable-64.h +2 -2
@@ -147,8 +147,8 @@#if defined(CONFIG_MODULES ) && defined(KBUILD_64BIT_SYM32 ) && \VMALLOC_START !=CKSSEG /* Load modules into 32bit-compatible segment. */ -#define MODULE_START CKSSEG -#define MODULE_END (FIXADDR_START-2*PAGE_SIZE) +#define MODULES_VADDR CKSSEG +#define MODULES_END (FIXADDR_START-2*PAGE_SIZE)#endif #define pte_ERROR (e) \
arch/mips/kernel/module.c +2 -2
@@ -31,10 +31,10 @@ struct mips_hi16 {static LIST_HEAD (dbe_list);static DEFINE_SPINLOCK (dbe_lock); -#ifdef MODULE_START +#ifdef MODULES_VADDR void *module_alloc (unsigned long size) { -return __vmalloc_node_range (size,1 ,MODULE_START ,MODULE_END , +return __vmalloc_node_range (size,1 ,MODULES_VADDR ,MODULES_END ,GFP_KERNEL ,PAGE_KERNEL ,0 ,NUMA_NO_NODE ,__builtin_return_address (0 )); }
arch/mips/mm/fault.c +2 -2
@@ -83,8 +83,8 @@ static void __do_page_fault(struct pt_regs *regs, unsigned long write,if (unlikely (address >=VMALLOC_START && address <=VMALLOC_END )) goto VMALLOC_FAULT_TARGET; -#ifdef MODULE_START -if (unlikely (address >=MODULE_START && address <MODULE_END )) +#ifdef MODULES_VADDR +if (unlikely (address >=MODULES_VADDR && address <MODULES_END )) goto VMALLOC_FAULT_TARGET;#endif
Signature · 1 line
2.43.0
Mike Rapoport
maintainerPATCH v5 3/15 nios2: define virtual address space for modules
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
nios2 uses kmalloc() to implement module_alloc() because CALL26/PCREL26 cannot reach all of vmalloc address space.
Define module space as 32MiB below the kernel base and switch nios2 to use vmalloc for module allocations.
--- arch/nios2/include/asm/pgtable.h | 5 ++++- arch/nios2/kernel/module.c | 19 ++++--------------- 2 files changed, 8 insertions(+), 16 deletions(-)
arch/nios2/include/asm/pgtable.h +4 -1
@@ -25,7 +25,10 @@#include <asm-generic/pgtable-nopmd.h> #define VMALLOC_START CONFIG_NIOS2_KERNEL_MMU_REGION_BASE -#define VMALLOC_END (CONFIG_NIOS2_KERNEL_REGION_BASE - 1) +#define VMALLOC_END (CONFIG_NIOS2_KERNEL_REGION_BASE - SZ_32M - 1) + +#define MODULES_VADDR (CONFIG_NIOS2_KERNEL_REGION_BASE - SZ_32M) +#define MODULES_END (CONFIG_NIOS2_KERNEL_REGION_BASE - 1)struct mm_struct ;
arch/nios2/kernel/module.c +4 -15
@@ -21,23 +21,12 @@#include <asm/cacheflush.h> -/* -* Modules should NOT be allocated with kmalloc for (obvious) reasons. -* But we do it for now to avoid relocation issues. CALL26/PCREL26 cannot reach -* from 0x80000000 (vmalloc area) to 0xc00000000 (kernel) (kmalloc returns -* addresses in 0xc0000000) -*/ void *module_alloc (unsigned long size) { -if (size ==0 ) -return NULL ; -return kmalloc (size,GFP_KERNEL ); -} - -/* Free memory returned from module_alloc */ -void module_memfree (void *module_region) -{ -kfree (module_region); +return __vmalloc_node_range (size,1 ,MODULES_VADDR ,MODULES_END , +GFP_KERNEL ,PAGE_KERNEL_EXEC , +VM_FLUSH_RESET_PERMS ,NUMA_NO_NODE , +__builtin_return_address (0 )); } int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
Signature · 1 line
2.43.0
Mike Rapoport
maintainerPATCH v5 4/15 sparc: simplify module_alloc()
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
Define MODULES_VADDR and MODULES_END as VMALLOC_START and VMALLOC_END for 32-bit and reduce module_alloc() to
__vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END, ...)
as with the new defines the allocations becames identical for both 32 and 64 bits.
While on it, drop unsed include of <linux/jump_label.h>
--- arch/sparc/include/asm/pgtable_32.h | 2 ++ arch/sparc/kernel/module.c | 25 +------------------------ 2 files changed, 3 insertions(+), 24 deletions(-)
arch/sparc/include/asm/pgtable_32.h +2 -0
@@ -432,6 +432,8 @@ static inline int io_remap_pfn_range(struct vm_area_struct *vma,#define VMALLOC_START _AC(0xfe600000,UL)#define VMALLOC_END _AC(0xffc00000,UL) +#define MODULES_VADDR VMALLOC_START +#define MODULES_END VMALLOC_END/* We provide our own get_unmapped_area to cope with VA holes for userland */ #define HAVE_ARCH_UNMAPPED_AREA
arch/sparc/kernel/module.c +1 -24
@@ -21,35 +21,12 @@#include "entry.h" -#ifdef CONFIG_SPARC64 - -#include <linux/jump_label.h> - -static void *module_map (unsigned long size) +void *module_alloc (unsigned long size) { -if (PAGE_ALIGN (size) >MODULES_LEN ) -return NULL ;return __vmalloc_node_range (size,1 ,MODULES_VADDR ,MODULES_END ,GFP_KERNEL ,PAGE_KERNEL ,0 ,NUMA_NO_NODE ,__builtin_return_address (0 )); } -#else -static void *module_map (unsigned long size) -{ -return vmalloc (size); -} -#endif /* CONFIG_SPARC64 */ - -void *module_alloc (unsigned long size) -{ -void *ret; - - ret =module_map (size); -if (ret) -memset (ret,0 , size); - -return ret; -}/* Make generic code ignore STT_REGISTER dummy undefined symbols. */ int module_frob_arch_sections(Elf_Ehdr *hdr,
Signature · 1 line
2.43.0
Mike Rapoport
maintainerPATCH v5 5/15 module: make module_memory_{alloc,free} more self-contained
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
Move the logic related to the memory allocation and freeing into module_memory_alloc() and module_memory_free().
--- kernel/module/main.c | 64 +++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 25 deletions(-)
kernel/module/main.c +39 -25
@@ -1203,15 +1203,44 @@ static bool mod_mem_use_vmalloc(enum mod_mem_type type)mod_mem_type_is_core_data (type); } -static void *module_memory_alloc (unsigned int size,enum mod_mem_type type) +static int module_memory_alloc (struct module *mod,enum mod_mem_type type) { +unsigned int size =PAGE_ALIGN (mod->mem[type].size); +void *ptr; + + mod->mem[type].size = size; +if (mod_mem_use_vmalloc (type)) -return vzalloc (size); -return module_alloc (size); + ptr =vmalloc (size); +else + ptr =module_alloc (size); + +if (!ptr) +return -ENOMEM ; + +/* +* The pointer to these blocks of memory are stored on the module +* structure and we keep that around so long as the module is +* around. We only free that memory when we unload the module. +* Just mark them as not being a leak then. The .init* ELF +* sections *do* get freed after boot so we *could* treat them +* slightly differently with kmemleak_ignore() and only grey +* them out as they work as typical memory allocations which +* *do* eventually get freed, but let's just keep things simple +* and avoid *any* false positives. +*/ +kmemleak_not_leak (ptr); + +memset (ptr,0 , size); + mod->mem[type].base = ptr; + +return 0 ; } -static void module_memory_free (void *ptr,enum mod_mem_type type) +static void module_memory_free (struct module *mod,enum mod_mem_type type) { +void *ptr = mod->mem[type].base; +if (mod_mem_use_vmalloc (type))vfree (ptr);else
@@ -1229,12 +1258,12 @@ static void free_mod_mem(struct module *mod)/* Free lock-classes; relies on the preceding sync_rcu(). */ lockdep_free_key_range (mod_mem->base, mod_mem->size);if (mod_mem->size) -module_memory_free (mod_mem->base, type); +module_memory_free (mod, type); }/* MOD_DATA hosts mod, so free it at last */ lockdep_free_key_range (mod->mem[MOD_DATA ].base, mod->mem[MOD_DATA ].size); -module_memory_free (mod->mem[MOD_DATA ].base,MOD_DATA ); +module_memory_free (mod,MOD_DATA ); }/* Free a module, remove from lists, etc. */
@@ -2225,7 +2254,6 @@ static int find_module_sections(struct module *mod, struct load_info *info)static int move_module (struct module *mod,struct load_info *info) {int i; -void *ptr;enum mod_mem_type t =0 ;int ret = -ENOMEM ;
@@ -2234,26 +2262,12 @@ static int move_module(struct module *mod, struct load_info *info) mod->mem[type].base =NULL ;continue ; } - mod->mem[type].size =PAGE_ALIGN (mod->mem[type].size); - ptr =module_memory_alloc (mod->mem[type].size, type); -/* -* The pointer to these blocks of memory are stored on the module -* structure and we keep that around so long as the module is -* around. We only free that memory when we unload the module. -* Just mark them as not being a leak then. The .init* ELF -* sections *do* get freed after boot so we *could* treat them -* slightly differently with kmemleak_ignore() and only grey -* them out as they work as typical memory allocations which -* *do* eventually get freed, but let's just keep things simple -* and avoid *any* false positives. -*/ -kmemleak_not_leak (ptr); -if (!ptr) { + + ret =module_memory_alloc (mod, type); +if (ret) { t = type; goto out_enomem; } -memset (ptr,0 , mod->mem[type].size); - mod->mem[type].base = ptr; }/* Transfer each section which specifies SHF_ALLOC */
@@ -2296,7 +2310,7 @@ static int move_module(struct module *mod, struct load_info *info)return 0 ; out_enomem:for (t--; t >=0 ; t--) -module_memory_free (mod->mem[t].base, t); +module_memory_free (mod, t);return ret; }
Signature · 1 line
2.43.0
Mike Rapoport
maintainerPATCH v5 6/15 mm: introduce execmem_alloc() and execmem_free()
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
module_alloc() is used everywhere as a mean to allocate memory for code.
Beside being semantically wrong, this unnecessarily ties all subsystems that need to allocate code, such as ftrace, kprobes and BPF to modules and puts the burden of code allocation to the modules code.
Several architectures override module_alloc() because of various constraints where the executable memory can be located and this causes additional obstacles for improvements of code allocation.
Start splitting code allocation from modules by introducing execmem_alloc() and execmem_free() APIs.
Initially, execmem_alloc() is a wrapper for module_alloc() and execmem_free() is a replacement of module_memfree() to allow updating all call sites to use the new APIs.
Since architectures define different restrictions on placement, permissions, alignment and other parameters for memory that can be used by different subsystems that allocate executable memory, execmem_alloc() takes a type argument, that will be used to identify the calling subsystem and to allow architectures define parameters for ranges suitable for that subsystem.
No functional changes.
--- arch/powerpc/kernel/kprobes.c | 6 ++-- arch/s390/kernel/ftrace.c | 4 +-- arch/s390/kernel/kprobes.c | 4 +-- arch/s390/kernel/module.c | 5 +-- arch/sparc/net/bpf_jit_comp_32.c | 8 ++--- arch/x86/kernel/ftrace.c | 6 ++-- arch/x86/kernel/kprobes/core.c | 4 +-- include/linux/execmem.h | 57 ++++++++++++++++++++++++++++++++ include/linux/moduleloader.h | 3 -- kernel/bpf/core.c | 6 ++-- kernel/kprobes.c | 8 ++--- kernel/module/Kconfig | 1 + kernel/module/main.c | 25 +++++--------- mm/Kconfig | 3 ++ mm/Makefile | 1 + mm/execmem.c | 32 ++++++++++++++++++ 16 files changed, 128 insertions(+), 45 deletions(-) create mode 100644 include/linux/execmem.h create mode 100644 mm/execmem.c
arch/powerpc/kernel/kprobes.c +3 -3
@@ -19,8 +19,8 @@#include <linux/extable.h> #include <linux/kdebug.h> #include <linux/slab.h> -#include <linux/moduleloader.h> #include <linux/set_memory.h> +#include <linux/execmem.h> #include <asm/code-patching.h> #include <asm/cacheflush.h> #include <asm/sstep.h>
@@ -130,7 +130,7 @@ void *alloc_insn_page(void) {void *page; - page =module_alloc (PAGE_SIZE ); + page =execmem_alloc (EXECMEM_KPROBES ,PAGE_SIZE );if (!page)return NULL ;
@@ -142,7 +142,7 @@ void *alloc_insn_page(void) }return page; error: -module_memfree (page); +execmem_free (page);return NULL ; }
arch/s390/kernel/ftrace.c +2 -2
@@ -7,13 +7,13 @@* Author(s): Martin Schwidefsky <[email protected]> */ -#include <linux/moduleloader.h> #include <linux/hardirq.h> #include <linux/uaccess.h> #include <linux/ftrace.h> #include <linux/kernel.h> #include <linux/types.h> #include <linux/kprobes.h> +#include <linux/execmem.h> #include <trace/syscall.h> #include <asm/asm-offsets.h> #include <asm/text-patching.h>
@@ -220,7 +220,7 @@ static int __init ftrace_plt_init(void) {const char *start, *end; - ftrace_plt =module_alloc (PAGE_SIZE ); + ftrace_plt =execmem_alloc (EXECMEM_FTRACE ,PAGE_SIZE );if (!ftrace_plt)panic ("cannot allocate ftrace plt\n" );
arch/s390/kernel/kprobes.c +2 -2
@@ -9,7 +9,6 @@#define pr_fmt (fmt) "kprobes: " fmt -#include <linux/moduleloader.h> #include <linux/kprobes.h> #include <linux/ptrace.h> #include <linux/preempt.h>
@@ -21,6 +20,7 @@#include <linux/slab.h> #include <linux/hardirq.h> #include <linux/ftrace.h> +#include <linux/execmem.h> #include <asm/set_memory.h> #include <asm/sections.h> #include <asm/dis.h>
@@ -38,7 +38,7 @@ void *alloc_insn_page(void) {void *page; - page =module_alloc (PAGE_SIZE ); + page =execmem_alloc (EXECMEM_KPROBES ,PAGE_SIZE );if (!page)return NULL ;set_memory_rox ((unsigned long )page,1 );
arch/s390/kernel/module.c +3 -2
@@ -21,6 +21,7 @@#include <linux/moduleloader.h> #include <linux/bug.h> #include <linux/memory.h> +#include <linux/execmem.h> #include <asm/alternative.h> #include <asm/nospec-branch.h> #include <asm/facility.h>
@@ -76,7 +77,7 @@ void *module_alloc(unsigned long size)#ifdef CONFIG_FUNCTION_TRACER void module_arch_cleanup (struct module *mod) { -module_memfree (mod->arch.trampolines_start); +execmem_free (mod->arch.trampolines_start); }#endif
@@ -510,7 +511,7 @@ static int module_alloc_ftrace_hotpatch_trampolines(struct module *me, size =FTRACE_HOTPATCH_TRAMPOLINES_SIZE (s->sh_size); numpages =DIV_ROUND_UP (size,PAGE_SIZE ); - start =module_alloc (numpages *PAGE_SIZE ); + start =execmem_alloc (EXECMEM_FTRACE , numpages *PAGE_SIZE );if (!start)return -ENOMEM ;set_memory_rox ((unsigned long )start, numpages);
arch/sparc/net/bpf_jit_comp_32.c +4 -4
@@ -1,10 +1,10 @@// SPDX-License-Identifier: GPL-2.0 -#include <linux/moduleloader.h> #include <linux/workqueue.h> #include <linux/netdevice.h> #include <linux/filter.h> #include <linux/cache.h> #include <linux/if_vlan.h> +#include <linux/execmem.h> #include <asm/cacheflush.h> #include <asm/ptrace.h>
@@ -713,7 +713,7 @@ cond_branch: f_offset = addrs[i + filter[i].jf];if (unlikely (proglen + ilen > oldproglen)) {pr_err ("bpb_jit_compile fatal error\n" );kfree (addrs); -module_memfree (image); +execmem_free (image);return ; }memcpy (image + proglen, temp, ilen);
@@ -736,7 +736,7 @@ cond_branch: f_offset = addrs[i + filter[i].jf];break ; }if (proglen == oldproglen) { - image =module_alloc (proglen); + image =execmem_alloc (EXECMEM_BPF , proglen);if (!image) goto out; }
@@ -758,7 +758,7 @@ cond_branch: f_offset = addrs[i + filter[i].jf];void bpf_jit_free (struct bpf_prog *fp) {if (fp->jited) -module_memfree (fp->bpf_func); +execmem_free (fp->bpf_func);bpf_prog_unlock_free (fp); }
arch/x86/kernel/ftrace.c +3 -3
@@ -25,6 +25,7 @@#include <linux/memory.h> #include <linux/vmalloc.h> #include <linux/set_memory.h> +#include <linux/execmem.h> #include <trace/syscall.h>
@@ -261,15 +262,14 @@ void arch_ftrace_update_code(int command)#ifdef CONFIG_X86_64 #ifdef CONFIG_MODULES -#include <linux/moduleloader.h> /* Module allocation simplifies allocating memory for code */ static inline void *alloc_tramp (unsigned long size) { -return module_alloc (size); +return execmem_alloc (EXECMEM_FTRACE , size); }static inline void tramp_free (void *tramp) { -module_memfree (tramp); +execmem_free (tramp); }#else /* Trampolines can only be created if modules are supported */
arch/x86/kernel/kprobes/core.c +2 -2
@@ -40,12 +40,12 @@#include <linux/kgdb.h> #include <linux/ftrace.h> #include <linux/kasan.h> -#include <linux/moduleloader.h> #include <linux/objtool.h> #include <linux/vmalloc.h> #include <linux/pgtable.h> #include <linux/set_memory.h> #include <linux/cfi.h> +#include <linux/execmem.h> #include <asm/text-patching.h> #include <asm/cacheflush.h>
@@ -495,7 +495,7 @@ void *alloc_insn_page(void) {void *page; - page =module_alloc (PAGE_SIZE ); + page =execmem_alloc (EXECMEM_KPROBES ,PAGE_SIZE );if (!page)return NULL ;
include/linux/execmem.h new file +57 -0
57 lines not on this page · raw message
include/linux/moduleloader.h +0 -3
@@ -29,9 +29,6 @@ unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);sections. Returns NULL on failure. */ void *module_alloc (unsigned long size); -/* Free memory returned from module_alloc. */ -void module_memfree (void *module_region); -/* Determines if the section name is an init section (that is only used during * module loading). */
kernel/bpf/core.c +3 -3
@@ -22,7 +22,6 @@#include <linux/skbuff.h> #include <linux/vmalloc.h> #include <linux/random.h> -#include <linux/moduleloader.h> #include <linux/bpf.h> #include <linux/btf.h> #include <linux/objtool.h>
@@ -37,6 +36,7 @@#include <linux/nospec.h> #include <linux/bpf_mem_alloc.h> #include <linux/memcontrol.h> +#include <linux/execmem.h> #include <asm/barrier.h> #include <asm/unaligned.h>
@@ -1050,12 +1050,12 @@ void bpf_jit_uncharge_modmem(u32 size)void *__weak bpf_jit_alloc_exec (unsigned long size) { -return module_alloc (size); +return execmem_alloc (EXECMEM_BPF , size); }void __weak bpf_jit_free_exec (void *addr) { -module_memfree (addr); +execmem_free (addr); } struct bpf_binary_header *
kernel/kprobes.c +4 -4
@@ -26,7 +26,6 @@#include <linux/slab.h> #include <linux/stddef.h> #include <linux/export.h> -#include <linux/moduleloader.h> #include <linux/kallsyms.h> #include <linux/freezer.h> #include <linux/seq_file.h>
@@ -39,6 +38,7 @@#include <linux/jump_label.h> #include <linux/static_call.h> #include <linux/perf_event.h> +#include <linux/execmem.h> #include <asm/sections.h> #include <asm/cacheflush.h>
@@ -113,17 +113,17 @@ enum kprobe_slot_state {void __weak *alloc_insn_page (void ) {/* -* Use module_alloc() so this page is within +/- 2GB of where the +* Use execmem_alloc() so this page is within +/- 2GB of where the * kernel image and loaded module images reside. This is required * for most of the architectures. * (e.g. x86-64 needs this to handle the %rip-relative fixups.) */ -return module_alloc (PAGE_SIZE ); +return execmem_alloc (EXECMEM_KPROBES ,PAGE_SIZE ); }static void free_insn_page (void *page) { -module_memfree (page); +execmem_free (page); }struct kprobe_insn_cache kprobe_insn_slots = {
kernel/module/Kconfig +1 -0
@@ -2,6 +2,7 @@ menuconfig MODULES bool "Enable loadable module support" modules + select EXECMEM help Kernel modules are small pieces of compiled code which can be inserted in the running kernel, rather than being
kernel/module/main.c +8 -17
@@ -57,6 +57,7 @@#include <linux/audit.h> #include <linux/cfi.h> #include <linux/debugfs.h> +#include <linux/execmem.h> #include <uapi/linux/module.h> #include "internal.h"
@@ -1179,16 +1180,6 @@ resolve_symbol_wait(struct module *mod,return ksym; } -void __weak module_memfree (void *module_region) -{ -/* -* This memory may be RO, and freeing RO memory in an interrupt is not -* supported by vmalloc. -*/ -WARN_ON (in_interrupt ()); -vfree (module_region); -} -void __weak module_arch_cleanup (struct module *mod) { }
@@ -1213,7 +1204,7 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type)if (mod_mem_use_vmalloc (type)) ptr =vmalloc (size);else - ptr =module_alloc (size); + ptr =execmem_alloc (EXECMEM_MODULE_TEXT , size);if (!ptr)return -ENOMEM ;
@@ -1244,7 +1235,7 @@ static void module_memory_free(struct module *mod, enum mod_mem_type type)if (mod_mem_use_vmalloc (type))vfree (ptr);else -module_memfree (ptr); +execmem_free (ptr); }static void free_mod_mem (struct module *mod)
@@ -2496,9 +2487,9 @@ static void do_free_init(struct work_struct *w)llist_for_each_safe (pos, n, list) { initfree = container_of(pos, struct mod_initfree, node); -module_memfree (initfree->init_text); -module_memfree (initfree->init_data); -module_memfree (initfree->init_rodata); +execmem_free (initfree->init_text); +execmem_free (initfree->init_data); +execmem_free (initfree->init_rodata);kfree (initfree); } }
@@ -2608,10 +2599,10 @@ static noinline int do_init_module(struct module *mod)* We want to free module_init, but be aware that kallsyms may be * walking this with preempt disabled. In all the failure paths, we * call synchronize_rcu(), but we don't want to slow down the success -* path. module_memfree() cannot be called in an interrupt, so do the +* path. execmem_free() cannot be called in an interrupt, so do the * work and call synchronize_rcu() in a work queue. * -* Note that module_alloc() on most architectures creates W+X page +* Note that execmem_alloc() on most architectures creates W+X page * mappings which won't be cleaned up until do_free_init() runs. Any * code such as mark_rodata_ro() which depends on those mappings to * be cleaned up needs to sync with the queued work by invoking
mm/Kconfig +3 -0
@@ -1241,6 +1241,9 @@ config LOCK_MM_AND_FIND_VMA config IOMMU_MM_DATA bool +config EXECMEM + bool + source "mm/damon/Kconfig" endmenu
mm/Makefile +1 -0
@@ -133,3 +133,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o +obj-$(CONFIG_EXECMEM) += execmem.o
mm/execmem.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2002 Richard Henderson +* Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM. +* Copyright (C) 2023 Luis Chamberlain <[email protected]> +* Copyright (C) 2024 Mike Rapoport IBM. +*/ + +#include <linux/mm.h> +#include <linux/vmalloc.h> +#include <linux/execmem.h> +#include <linux/moduleloader.h> + +static void *__execmem_alloc (size_t size) +{ +return module_alloc (size); +} + +void *execmem_alloc (enum execmem_type type,size_t size) +{ +return __execmem_alloc (size); +} + +void execmem_free (void *ptr) +{ +/* +* This memory may be RO, and freeing RO memory in an interrupt is not +* supported by vmalloc. +*/ +WARN_ON (in_interrupt ()); +vfree (ptr); +}
Signature · 1 line
2.43.0
Mike Rapoport
maintainerPATCH v5 7/15 mm/execmem, arch: convert simple overrides of module_alloc to execmem
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
Several architectures override module_alloc() only to define address range for code allocations different than VMALLOC address space.
Provide a generic implementation in execmem that uses the parameters for address space ranges, required alignment and page protections provided by architectures.
The architectures must fill execmem_info structure and implement execmem_arch_setup() that returns a pointer to that structure. This way the execmem initialization won't be called from every architecture, but rather from a central place, namely a core_initcall() in execmem.
The execmem provides execmem_alloc() API that wraps __vmalloc_node_range() with the parameters defined by the architectures. If an architecture does not implement execmem_arch_setup(), execmem_alloc() will fall back to module_alloc().
--- arch/loongarch/kernel/module.c | 19 +++++++-- arch/mips/kernel/module.c | 20 ++++++++-- arch/nios2/kernel/module.c | 21 +++++++--- arch/parisc/kernel/module.c | 24 +++++++---- arch/riscv/kernel/module.c | 24 +++++++---- arch/sparc/kernel/module.c | 20 ++++++++-- include/linux/execmem.h | 41 +++++++++++++++++++ mm/execmem.c | 73 ++++++++++++++++++++++++++++++++-- 8 files changed, 208 insertions(+), 34 deletions(-)
arch/loongarch/kernel/module.c +16 -3
@@ -18,6 +18,7 @@#include <linux/ftrace.h> #include <linux/string.h> #include <linux/kernel.h> +#include <linux/execmem.h> #include <asm/alternative.h> #include <asm/inst.h> #include <asm/unwind.h>
@@ -490,10 +491,22 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,return 0 ; } -void *module_alloc (unsigned long size) +static struct execmem_info execmem_info__ro_after_init ; + +struct execmem_info __init *execmem_arch_setup (void ) { -return __vmalloc_node_range (size,1 ,MODULES_VADDR ,MODULES_END , -GFP_KERNEL ,PAGE_KERNEL ,0 ,NUMA_NO_NODE ,__builtin_return_address (0 )); + execmem_info = (struct execmem_info ){ + .ranges = { + [EXECMEM_DEFAULT ] = { + .start =MODULES_VADDR , + .end =MODULES_END , + .pgprot =PAGE_KERNEL , + .alignment =1 , + }, + }, + }; + +return &execmem_info; } static void module_init_ftrace_plt(const Elf_Ehdr *hdr,
arch/mips/kernel/module.c +16 -4
@@ -20,6 +20,7 @@#include <linux/kernel.h> #include <linux/spinlock.h> #include <linux/jump_label.h> +#include <linux/execmem.h> #include <asm/jump_label.h> struct mips_hi16 {
@@ -32,11 +33,22 @@ static LIST_HEAD(dbe_list);static DEFINE_SPINLOCK (dbe_lock);#ifdef MODULES_VADDR -void *module_alloc (unsigned long size) +static struct execmem_info execmem_info__ro_after_init ; + +struct execmem_info __init *execmem_arch_setup (void ) { -return __vmalloc_node_range (size,1 ,MODULES_VADDR ,MODULES_END , -GFP_KERNEL ,PAGE_KERNEL ,0 ,NUMA_NO_NODE , -__builtin_return_address (0 )); + execmem_info = (struct execmem_info ){ + .ranges = { + [EXECMEM_DEFAULT ] = { + .start =MODULES_VADDR , + .end =MODULES_END , + .pgprot =PAGE_KERNEL , + .alignment =1 , + }, + }, + }; + +return &execmem_info; }#endif
arch/nios2/kernel/module.c +16 -5
@@ -18,15 +18,26 @@#include <linux/fs.h> #include <linux/string.h> #include <linux/kernel.h> +#include <linux/execmem.h> #include <asm/cacheflush.h> -void *module_alloc (unsigned long size) +static struct execmem_info execmem_info__ro_after_init ; + +struct execmem_info __init *execmem_arch_setup (void ) { -return __vmalloc_node_range (size,1 ,MODULES_VADDR ,MODULES_END , -GFP_KERNEL ,PAGE_KERNEL_EXEC , -VM_FLUSH_RESET_PERMS ,NUMA_NO_NODE , -__builtin_return_address (0 )); + execmem_info = (struct execmem_info ){ + .ranges = { + [EXECMEM_DEFAULT ] = { + .start =MODULES_VADDR , + .end =MODULES_END , + .pgprot =PAGE_KERNEL_EXEC , + .alignment =1 , + }, + }, + }; + +return &execmem_info; } int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
arch/parisc/kernel/module.c +16 -8
@@ -49,6 +49,7 @@#include <linux/bug.h> #include <linux/mm.h> #include <linux/slab.h> +#include <linux/execmem.h> #include <asm/unwind.h> #include <asm/sections.h>
@@ -173,15 +174,22 @@ static inline int reassemble_22(int as22) ((as22 & 0x0003ff) << 3)); } -void *module_alloc (unsigned long size) +static struct execmem_info execmem_info__ro_after_init ; + +struct execmem_info __init *execmem_arch_setup (void ) { -/* using RWX means less protection for modules, but it's -* easier than trying to map the text, data, init_text and -* init_data correctly */ -return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , -GFP_KERNEL , -PAGE_KERNEL_RWX ,0 ,NUMA_NO_NODE , -__builtin_return_address (0 )); + execmem_info = (struct execmem_info ){ + .ranges = { + [EXECMEM_DEFAULT ] = { + .start =VMALLOC_START , + .end =VMALLOC_END , + .pgprot =PAGE_KERNEL_RWX , + .alignment =1 , + }, + }, + }; + +return &execmem_info; }#ifndef CONFIG_64BIT
arch/riscv/kernel/module.c +17 -7
@@ -14,6 +14,7 @@#include <linux/vmalloc.h> #include <linux/sizes.h> #include <linux/pgtable.h> +#include <linux/execmem.h> #include <asm/alternative.h> #include <asm/sections.h>
@@ -906,13 +907,22 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, }#if defined(CONFIG_MMU ) && defined(CONFIG_64BIT ) -void *module_alloc (unsigned long size) -{ -return __vmalloc_node_range (size,1 ,MODULES_VADDR , -MODULES_END ,GFP_KERNEL , -PAGE_KERNEL ,VM_FLUSH_RESET_PERMS , -NUMA_NO_NODE , -__builtin_return_address (0 )); +static struct execmem_info execmem_info__ro_after_init ; + +struct execmem_info __init *execmem_arch_setup (void ) +{ + execmem_info = (struct execmem_info ){ + .ranges = { + [EXECMEM_DEFAULT ] = { + .start =MODULES_VADDR , + .end =MODULES_END , + .pgprot =PAGE_KERNEL , + .alignment =1 , + }, + }, + }; + +return &execmem_info; }#endif
arch/sparc/kernel/module.c +16 -4
@@ -14,6 +14,7 @@#include <linux/string.h> #include <linux/ctype.h> #include <linux/mm.h> +#include <linux/execmem.h> #include <asm/processor.h> #include <asm/spitfire.h>
@@ -21,11 +22,22 @@#include "entry.h" -void *module_alloc (unsigned long size) +static struct execmem_info execmem_info__ro_after_init ; + +struct execmem_info __init *execmem_arch_setup (void ) { -return __vmalloc_node_range (size,1 ,MODULES_VADDR ,MODULES_END , -GFP_KERNEL ,PAGE_KERNEL ,0 ,NUMA_NO_NODE , -__builtin_return_address (0 )); + execmem_info = (struct execmem_info ){ + .ranges = { + [EXECMEM_DEFAULT ] = { + .start =MODULES_VADDR , + .end =MODULES_END , + .pgprot =PAGE_KERNEL , + .alignment =1 , + }, + }, + }; + +return &execmem_info; }/* Make generic code ignore STT_REGISTER dummy undefined symbols. */
include/linux/execmem.h +41 -0
47 lines not on this page · raw message
mm/execmem.c +70 -3
87 lines not on this page · raw message
Signature · 1 line
2.43.0
Mike Rapoport
maintainerPATCH v5 8/15 mm/execmem, arch: convert remaining overrides of module_alloc to execmem
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
Extend execmem parameters to accommodate more complex overrides of module_alloc() by architectures.
This includes specification of a fallback range required by arm, arm64 and powerpc, EXECMEM_MODULE_DATA type required by powerpc, support for allocation of KASAN shadow required by s390 and x86 and support for early initialization of execmem required by x86.
The core implementation of execmem_alloc() takes care of suppressing warnings when the initial allocation fails but there is a fallback range defined.
--- arch/Kconfig | 6 +++ arch/arm/kernel/module.c | 41 ++++++++++------- arch/arm64/kernel/module.c | 67 ++++++++++++++++++---------- arch/arm64/kernel/probes/kprobes.c | 7 --- arch/arm64/net/bpf_jit_comp.c | 11 ----- arch/powerpc/kernel/module.c | 60 ++++++++++++++++--------- arch/s390/kernel/module.c | 54 ++++++++++------------- arch/x86/Kconfig | 1 + arch/x86/kernel/module.c | 70 ++++++++++-------------------- include/linux/execmem.h | 34 +++++++++++++++ include/linux/moduleloader.h | 12 ----- kernel/module/main.c | 26 +++-------- mm/execmem.c | 70 +++++++++++++++++++++++++----- mm/mm_init.c | 2 + 14 files changed, 259 insertions(+), 202 deletions(-)
arch/Kconfig +6 -0
@@ -960,6 +960,12 @@ config ARCH_WANTS_MODULES_DATA_IN_VMALLOC For architectures like powerpc/32 which have constraints on module allocation and need to allocate module data outside of module area. +config ARCH_WANTS_EXECMEM_EARLY + bool + help + For architectures that might allocate executable memory early on + boot, for instance ftrace on x86. + config HAVE_IRQ_EXIT_ON_IRQ_STACK bool help
arch/arm/kernel/module.c +25 -16
54 lines not on this page · raw message
arch/arm64/kernel/module.c +43 -24
90 lines not on this page · raw message
arch/arm64/kernel/probes/kprobes.c +0 -7
@@ -129,13 +129,6 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)return 0 ; } -void *alloc_insn_page (void ) -{ -return __vmalloc_node_range (PAGE_SIZE ,1 ,VMALLOC_START ,VMALLOC_END , -GFP_KERNEL ,PAGE_KERNEL_ROX ,VM_FLUSH_RESET_PERMS , -NUMA_NO_NODE ,__builtin_return_address (0 )); -} -/* arm kprobe: install breakpoint in text */ void __kprobes arch_arm_kprobe (struct kprobe *p) {
arch/arm64/net/bpf_jit_comp.c +0 -11
@@ -1793,17 +1793,6 @@ u64 bpf_jit_alloc_exec_limit(void)return VMALLOC_END -VMALLOC_START ; } -void *bpf_jit_alloc_exec (unsigned long size) -{ -/* Memory is intended to be executable, reset the pointer tag. */ -return kasan_reset_tag (vmalloc (size)); -} - -void bpf_jit_free_exec (void *addr) -{ -return vfree (addr); -} -/* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */ bool bpf_jit_supports_subprog_tailcalls (void ) {
arch/powerpc/kernel/module.c +39 -21
84 lines not on this page · raw message
arch/s390/kernel/module.c +22 -32
63 lines not on this page · raw message
arch/x86/Kconfig +1 -0
@@ -135,6 +135,7 @@ config X86 select ARCH_WANT_OPTIMIZE_DAX_VMEMMAP if X86_64 select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if X86_64 select ARCH_WANTS_THP_SWAP if X86_64 + select ARCH_WANTS_EXECMEM_EARLY if EXECMEM select ARCH_HAS_PARANOID_L1D_FLUSH select BUILDTIME_TABLE_SORT select CLKEVT_I8253
arch/x86/kernel/module.c +23 -47
84 lines not on this page · raw message
include/linux/execmem.h +34 -0
@@ -5,6 +5,14 @@#include <linux/types.h> #include <linux/moduleloader.h> +#if (defined(CONFIG_KASAN_GENERIC ) || defined(CONFIG_KASAN_SW_TAGS )) && \ + !defined(CONFIG_KASAN_VMALLOC ) +#include <linux/kasan.h> +#define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT) +#else +#define MODULE_ALIGN PAGE_SIZE +#endif +/** * enum execmem_type - types of executable memory ranges *
@@ -22,6 +30,7 @@* @EXECMEM_KPROBES: parameters for kprobes * @EXECMEM_FTRACE: parameters for ftrace * @EXECMEM_BPF: parameters for BPF +* @EXECMEM_MODULE_DATA: parameters for module data sections * @EXECMEM_TYPE_MAX: */ enum execmem_type {
@@ -30,22 +39,38 @@ enum execmem_type {EXECMEM_KPROBES ,EXECMEM_FTRACE ,EXECMEM_BPF , +EXECMEM_MODULE_DATA , EXECMEM_TYPE_MAX, }; +/** +* enum execmem_range_flags - options for executable memory allocations +* @EXECMEM_KASAN_SHADOW: allocate kasan shadow +*/ +enum execmem_range_flags { +EXECMEM_KASAN_SHADOW = (1 <<0 ), +}; +/** * struct execmem_range - definition of an address space suitable for code and * related data allocations * @start: address space start * @end: address space end (inclusive) +* @fallback_start: start of the secondary address space range for fallback +* allocations on architectures that require it +* @fallback_end: start of the secondary address space (inclusive) * @pgprot: permissions for memory in this address space * @alignment: alignment required for text allocations +* @flags: options for memory allocations for this range */ struct execmem_range {unsigned long start;unsigned long end; +unsigned long fallback_start; +unsigned long fallback_end;pgprot_t pgprot;unsigned int alignment; +enum execmem_range_flags flags; };/**
@@ -82,6 +107,9 @@ struct execmem_info *execmem_arch_setup(void);* Allocates memory that will contain executable code, either generated or * loaded from kernel modules. * +* Allocates memory that will contain data coupled with executable code, +* like data sections in kernel modules. +* * The memory will have protections defined by architecture for executable * region of the @type. *
@@ -95,4 +123,10 @@ void *execmem_alloc(enum execmem_type type, size_t size);*/ void execmem_free (void *ptr); +#ifdef CONFIG_ARCH_WANTS_EXECMEM_EARLY +void execmem_early_init (void ); +#else +static inline void execmem_early_init (void ) {} +#endif +#endif /* _LINUX_EXECMEM_ALLOC_H */
include/linux/moduleloader.h +0 -12
@@ -25,10 +25,6 @@ int module_frob_arch_sections(Elf_Ehdr *hdr,/* Additional bytes needed by arch in front of individual sections */ unsigned int arch_mod_section_prepend (struct module *mod,unsigned int section); -/* Allocator used for allocating struct module, core sections and init -sections. Returns NULL on failure. */ -void *module_alloc (unsigned long size); -/* Determines if the section name is an init section (that is only used during * module loading). */
@@ -126,12 +122,4 @@ void module_arch_cleanup(struct module *mod);/* Any cleanup before freeing mod->module_init */ void module_arch_freeing_init (struct module *mod); -#if (defined(CONFIG_KASAN_GENERIC ) || defined(CONFIG_KASAN_SW_TAGS )) && \ - !defined(CONFIG_KASAN_VMALLOC ) -#include <linux/kasan.h> -#define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT) -#else -#define MODULE_ALIGN PAGE_SIZE -#endif -#endif
kernel/module/main.c +6 -20
@@ -1188,24 +1188,20 @@ void __weak module_arch_freeing_init(struct module *mod) { } -static bool mod_mem_use_vmalloc (enum mod_mem_type type) -{ -return IS_ENABLED (CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC ) && -mod_mem_type_is_core_data (type); -} -static int module_memory_alloc (struct module *mod,enum mod_mem_type type) {unsigned int size =PAGE_ALIGN (mod->mem[type].size); +enum execmem_type execmem_type;void *ptr; mod->mem[type].size = size; -if (mod_mem_use_vmalloc (type)) - ptr =vmalloc (size); +if (mod_mem_type_is_data (type)) + execmem_type =EXECMEM_MODULE_DATA ;else - ptr =execmem_alloc (EXECMEM_MODULE_TEXT , size); + execmem_type =EXECMEM_MODULE_TEXT ; + ptr =execmem_alloc (execmem_type, size);if (!ptr)return -ENOMEM ;
@@ -1232,10 +1228,7 @@ static void module_memory_free(struct module *mod, enum mod_mem_type type) {void *ptr = mod->mem[type].base; -if (mod_mem_use_vmalloc (type)) -vfree (ptr); -else -execmem_free (ptr); +execmem_free (ptr); }static void free_mod_mem (struct module *mod)
@@ -1630,13 +1623,6 @@ static void free_modinfo(struct module *mod) } } -void *__weak module_alloc (unsigned long size) -{ -return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , -GFP_KERNEL ,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , -NUMA_NO_NODE ,__builtin_return_address (0 )); -} -bool __weak module_init_section (const char *name) {return strstarts (name,".init" );
mm/execmem.c +58 -12
111 lines not on this page · raw message
mm/mm_init.c +2 -0
@@ -27,6 +27,7 @@#include <linux/swap.h> #include <linux/cma.h> #include <linux/crash_dump.h> +#include <linux/execmem.h> #include "internal.h" #include "slab.h" #include "shuffle.h"
@@ -2793,4 +2794,5 @@ void __init mm_core_init(void)pti_init ();kmsan_init_runtime ();mm_cache_init (); +execmem_early_init (); }
Signature · 1 line
2.43.0
Mike Rapoport
maintainerPATCH v5 9/15 riscv: extend execmem_params for generated code allocations
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
The memory allocations for kprobes and BPF on RISC-V are not placed in the modules area and these custom allocations are implemented with overrides of alloc_insn_page() and bpf_jit_alloc_exec().
Slightly reorder execmem_params initialization to support both 32 and 64 bit variants, define EXECMEM_KPROBES and EXECMEM_BPF ranges in riscv::execmem_params and drop overrides of alloc_insn_page() and bpf_jit_alloc_exec().
--- arch/riscv/kernel/module.c | 28 +++++++++++++++++++++++++--- arch/riscv/kernel/probes/kprobes.c | 10 ---------- arch/riscv/net/bpf_jit_core.c | 13 ------------- 3 files changed, 25 insertions(+), 26 deletions(-)
arch/riscv/kernel/module.c +25 -3
@@ -906,19 +906,41 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,return 0 ; } -#if defined(CONFIG_MMU ) && defined(CONFIG_64BIT ) +#ifdef CONFIG_MMU static struct execmem_info execmem_info__ro_after_init ;struct execmem_info __init *execmem_arch_setup (void ) { +unsigned long start, end; + +if (IS_ENABLED (CONFIG_64BIT )) { + start =MODULES_VADDR ; + end =MODULES_END ; + }else { + start =VMALLOC_START ; + end =VMALLOC_END ; + } + execmem_info = (struct execmem_info ){ .ranges = { [EXECMEM_DEFAULT ] = { - .start =MODULES_VADDR , - .end =MODULES_END , + .start = start, + .end = end, .pgprot =PAGE_KERNEL , .alignment =1 , }, + [EXECMEM_KPROBES ] = { + .start =VMALLOC_START , + .end =VMALLOC_END , + .pgprot =PAGE_KERNEL_READ_EXEC , + .alignment =1 , + }, + [EXECMEM_BPF ] = { + .start =BPF_JIT_REGION_START , + .end =BPF_JIT_REGION_END , + .pgprot =PAGE_KERNEL , + .alignment =PAGE_SIZE , + }, }, };
arch/riscv/kernel/probes/kprobes.c +0 -10
@@ -104,16 +104,6 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)return 0 ; } -#ifdef CONFIG_MMU -void *alloc_insn_page (void ) -{ -return __vmalloc_node_range (PAGE_SIZE ,1 ,VMALLOC_START ,VMALLOC_END , -GFP_KERNEL ,PAGE_KERNEL_READ_EXEC , -VM_FLUSH_RESET_PERMS ,NUMA_NO_NODE , -__builtin_return_address (0 )); -} -#endif -/* install breakpoint in text */ void __kprobes arch_arm_kprobe (struct kprobe *p) {
arch/riscv/net/bpf_jit_core.c +0 -13
@@ -219,19 +219,6 @@ u64 bpf_jit_alloc_exec_limit(void)return BPF_JIT_REGION_SIZE ; } -void *bpf_jit_alloc_exec (unsigned long size) -{ -return __vmalloc_node_range (size,PAGE_SIZE ,BPF_JIT_REGION_START , -BPF_JIT_REGION_END ,GFP_KERNEL , -PAGE_KERNEL ,0 ,NUMA_NO_NODE , -__builtin_return_address (0 )); -} - -void bpf_jit_free_exec (void *addr) -{ -return vfree (addr); -} -void *bpf_arch_text_copy (void *dst,void *src,size_t len) {int ret;
Signature · 1 line
2.43.0