arm64: module: improve module VA range selection
Subsystems (4)
Mark Rutland
reviewerv3 cover arm64: module: improve module VA range selection
This series (based on v6.4-rc3) aims to make arm64's module allocation code more robust. To that end:
* Redundant code for KASAN && !KASAN_VMALLOC is removed, as this combination is no longer possible.
* Module PLT support is mandated, always allowing for the use of a 2G module region. Practically speaking, this is already the case for almost all users as module PLT support is mandated by both KASLR (which most distros including Android, enable this), and by the workaround for ARM erratum 843419 (required by Cortex-A53).
* The module VA region selection is moved to module.c, making it self-contained and easier to follow.
* The default module VA region is expanded to 2G. This ensures that there is sufficient space for the full region using PLTs even when KASLR is disabled or no seed provided.
* The module VA range init code is updated to log when the kernel is too large to support the use of a 128M or 2G module region, to enable debugging of these cases. Contemporary kernels built with debug options can be bigger than 128M, and a kernel bigger than 2G is unlikely but theoretically possible. Adding this logging should help to debug or filter away reports for such cases.
This should allow for loading of very large modules, as Shanker reported was an issue:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
https://lore.kernel.org/linux-arm-kernel/[email protected]/
... and as Ard had an alternative series for:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
Since v1 [1]:
* Log the number of pages in range
* Remove unused kasan_mod_shadow_end
* Only randomize when kaslr_enabled()
* Simplify control-flow
Since v2 [2]:
* Apply Ard's Reviewed-by tags
* Fix typos
* Rebave to v6.4-rc3 (trivial)
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/[email protected]/
Thanks,
Mark.
Mark Rutland (6):
arm64: module: remove old !KASAN_VMALLOC logic
arm64: kasan: remove !KASAN_VMALLOC remnants
arm64: kaslr: split kaslr/module initialization
arm64: module: move module randomization to module.c
arm64: module: mandate MODULE_PLTS
arm64: module: rework module VA range selection
Documentation/arm64/memory.rst | 8 +- arch/arm64/Kconfig | 28 +---- arch/arm64/include/asm/memory.h | 16 +-- arch/arm64/include/asm/module.h | 8 -- arch/arm64/include/asm/module.lds.h | 2 - arch/arm64/kernel/Makefile | 3 +- arch/arm64/kernel/ftrace.c | 8 +- arch/arm64/kernel/kaslr.c | 83 +++------------ arch/arm64/kernel/module.c | 159 +++++++++++++++++++++------- arch/arm64/kernel/setup.c | 2 + arch/arm64/mm/kasan_init.c | 17 +-- 11 files changed, 159 insertions(+), 175 deletions(-)
Signature · 7 lines
2.30.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Mark Rutland
reviewerPATCH v3 1/6 arm64: module: remove old !KASAN_VMALLOC logic
Historically, KASAN could be selected with or without KASAN_VMALLOC, and we had to be very careful where to place modules when KASAN_VMALLOC was not selected.
However, since commit:
f6f37d9320a11e90 ("arm64: select KASAN_VMALLOC for SW/HW_TAGS modes")
Selecting CONFIG_KASAN on arm64 will also select CONFIG_KASAN_VMALLOC, and so the logic for handling CONFIG_KASAN without CONFIG_KASAN_VMALLOC is redundant and can be removed.
Note: the "kasan.vmalloc={on,off}" option which only exists for HW_TAGS changes whether the vmalloc region is given non-match-all tags, and does not affect the page table manipulation code.
The VM_DEFER_KMEMLEAK flag was only necessary for !CONFIG_KASAN_VMALLOC as described in its introduction in commit:
60115fa54ad7b913 ("mm: defer kmemleak object creation of module_alloc()")
... and therefore it can also be removed.
Remove the redundant logic for !CONFIG_KASAN_VMALLOC. At the same time, add the missing braces around the multi-line conditional block in arch/arm64/kernel/module.c.
--- arch/arm64/kernel/kaslr.c | 10 ---------- arch/arm64/kernel/module.c | 23 +++-------------------- 2 files changed, 3 insertions(+), 30 deletions(-)
arch/arm64/kernel/kaslr.c +0 -10
@@ -48,16 +48,6 @@ static int __init kaslr_init(void)pr_info ("KASLR enabled\n" ); -/* -* KASAN without KASAN_VMALLOC does not expect the module region to -* intersect the vmalloc region, since shadow memory is allocated for -* each module at load time, whereas the vmalloc region will already be -* shadowed by KASAN zero pages. -*/ -BUILD_BUG_ON ((IS_ENABLED (CONFIG_KASAN_GENERIC ) || -IS_ENABLED (CONFIG_KASAN_SW_TAGS )) && - !IS_ENABLED (CONFIG_KASAN_VMALLOC )); - seed =get_random_u32 ();if (IS_ENABLED (CONFIG_RANDOMIZE_MODULE_REGION_FULL )) {
arch/arm64/kernel/module.c +3 -20
@@ -32,33 +32,16 @@ void *module_alloc(unsigned long size)if (IS_ENABLED (CONFIG_ARM64_MODULE_PLTS )) gfp_mask |= __GFP_NOWARN; -if (IS_ENABLED (CONFIG_KASAN_GENERIC ) || -IS_ENABLED (CONFIG_KASAN_SW_TAGS )) -/* don't exceed the static module region - see below */ - module_alloc_end =MODULES_END ; - p =__vmalloc_node_range (size,MODULE_ALIGN , module_alloc_base, - module_alloc_end, gfp_mask,PAGE_KERNEL ,VM_DEFER_KMEMLEAK , + module_alloc_end, gfp_mask,PAGE_KERNEL ,0 ,NUMA_NO_NODE ,__builtin_return_address (0 )); -if (!p &&IS_ENABLED (CONFIG_ARM64_MODULE_PLTS ) && - (IS_ENABLED (CONFIG_KASAN_VMALLOC ) || - (!IS_ENABLED (CONFIG_KASAN_GENERIC ) && - !IS_ENABLED (CONFIG_KASAN_SW_TAGS )))) -/* -* KASAN without KASAN_VMALLOC can only deal with module -* allocations being served from the reserved module region, -* since the remainder of the vmalloc region is already -* backed by zero shadow pages, and punching holes into it -* is non-trivial. Since the module region is not randomized -* when KASAN is enabled without KASAN_VMALLOC, it is even -* less likely that the module region gets exhausted, so we -* can simply omit this fallback in that case. -*/ +if (!p &&IS_ENABLED (CONFIG_ARM64_MODULE_PLTS )) { p =__vmalloc_node_range (size,MODULE_ALIGN , module_alloc_base, module_alloc_base +SZ_2G ,GFP_KERNEL ,PAGE_KERNEL ,0 ,NUMA_NO_NODE ,__builtin_return_address (0 )); + }if (p && (kasan_alloc_module_shadow (p, size, gfp_mask) <0 )) {vfree (p);
Signature · 7 lines
2.30.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Mark Rutland
reviewerPATCH v3 2/6 arm64: kasan: remove !KASAN_VMALLOC remnants
In reply to Mark RutlandHistorically, KASAN could be selected with or without KASAN_VMALLOC, but since commit:
f6f37d9320a11e90 ("arm64: select KASAN_VMALLOC for SW/HW_TAGS modes")
... we can never select KASAN without KASAN_VMALLOC on arm64, and thus arm64 code for KASAN && !KASAN_VMALLOC is redundant and can be removed.
Remove the redundant code kasan_init.c
--- arch/arm64/mm/kasan_init.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-)
arch/arm64/mm/kasan_init.c +4 -13
@@ -214,7 +214,7 @@ static void __init clear_pgds(unsigned long start,static void __init kasan_init_shadow (void ) {u64 kimg_shadow_start, kimg_shadow_end; -u64 mod_shadow_start, mod_shadow_end; +u64 mod_shadow_start;u64 vmalloc_shadow_end;phys_addr_t pa_start, pa_end;u64 i;
@@ -223,7 +223,6 @@ static void __init kasan_init_shadow(void) kimg_shadow_end =PAGE_ALIGN ((u64 )kasan_mem_to_shadow (KERNEL_END )); mod_shadow_start = (u64 )kasan_mem_to_shadow ((void *)MODULES_VADDR ); - mod_shadow_end = (u64 )kasan_mem_to_shadow ((void *)MODULES_END ); vmalloc_shadow_end = (u64 )kasan_mem_to_shadow ((void *)VMALLOC_END );
@@ -246,17 +245,9 @@ static void __init kasan_init_shadow(void)kasan_populate_early_shadow (kasan_mem_to_shadow ((void *)PAGE_END ), (void *)mod_shadow_start); -if (IS_ENABLED (CONFIG_KASAN_VMALLOC )) { -BUILD_BUG_ON (VMALLOC_START !=MODULES_END ); -kasan_populate_early_shadow ((void *)vmalloc_shadow_end, - (void *)KASAN_SHADOW_END ); - }else { -kasan_populate_early_shadow ((void *)kimg_shadow_end, - (void *)KASAN_SHADOW_END ); -if (kimg_shadow_start > mod_shadow_end) -kasan_populate_early_shadow ((void *)mod_shadow_end, - (void *)kimg_shadow_start); - } +BUILD_BUG_ON (VMALLOC_START !=MODULES_END ); +kasan_populate_early_shadow ((void *)vmalloc_shadow_end, + (void *)KASAN_SHADOW_END );for_each_mem_range (i, &pa_start, &pa_end) {void *start = (void *)__phys_to_virt (pa_start);
Signature · 7 lines
2.30.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Mark Rutland
reviewerPATCH v3 3/6 arm64: kaslr: split kaslr/module initialization
In reply to Mark RutlandCurrently kaslr_init() handles a mixture of detecting/announcing whether KASLR is enabled, and randomizing the module region depending on whether KASLR is enabled.
To make it easier to rework the module region initialization, split the KASLR initialization into two steps:
* kaslr_init() determines whether KASLR should be enabled, and announces this choice, recording this to a new global boolean variable. This is called from setup_arch() just before the existing call to kaslr_requires_kpti() so that this will always provide the expected result.
* kaslr_module_init() randomizes the module region when required. This is called as a subsys_initcall, where we previously called kaslr_init().
As a bonus, moving the KASLR reporting earlier makes it easier to spot and permits it to be logged via earlycon, making it easier to debug any issues that could be triggered by KASLR.
Booting a v6.4-rc1 kernel with this patch applied, the log looks like:
| EFI stub: Booting Linux Kernel... | EFI stub: Generating empty DTB | EFI stub: Exiting boot services... | [ 0.000000] Booting Linux on physical CPU 0x0000000000 [0x000f0510] | [ 0.000000] Linux version 6.4.0-rc1-00006-g4763a8f8aeb3 (mark@lakrids) (aarch64-linux-gcc (GCC) 12.1.0, GNU ld (GNU Binutils) 2.38) #2 SMP PREEMPT Tue May 9 11:03:37 BST 2023 | [ 0.000000] KASLR enabled | [ 0.000000] earlycon: pl11 at MMIO 0x0000000009000000 (options '') | [ 0.000000] printk: bootconsole [pl11] enabled
--- arch/arm64/include/asm/memory.h | 14 +++++++----- arch/arm64/kernel/kaslr.c | 39 +++++++++++++++++++++------------ arch/arm64/kernel/setup.c | 2 ++ 3 files changed, 35 insertions(+), 20 deletions(-)
arch/arm64/include/asm/memory.h +8 -6
@@ -204,15 +204,17 @@ static inline unsigned long kaslr_offset(void)return kimage_vaddr -KIMAGE_VADDR ; } +#ifdef CONFIG_RANDOMIZE_BASE +void kaslr_init (void );static inline bool kaslr_enabled (void ) { -/* -* The KASLR offset modulo MIN_KIMG_ALIGN is taken from the physical -* placement of the image rather than from the seed, so a displacement -* of less than MIN_KIMG_ALIGN means that no seed was provided. -*/ -return kaslr_offset () >=MIN_KIMG_ALIGN ; +extern bool __kaslr_is_enabled; +return __kaslr_is_enabled; } +#else +static inline void kaslr_init (void ) { } +static inline bool kaslr_enabled (void ) {return false; } +#endif /* * Allow all memory at the discovery stage. We will clip it later.
arch/arm64/kernel/kaslr.c +25 -14
@@ -25,28 +25,39 @@ u16 __initdata memstart_offset_seed;struct arm64_ftr_override kaslr_feature_override__initdata ; -static int __init kaslr_init (void ) -{ -u64 module_range; -u32 seed; - -/* -* Set a reasonable default for module_alloc_base in case -* we end up running with module randomization disabled. -*/ - module_alloc_base = (u64 )_etext -MODULES_VSIZE ; +bool __ro_after_init __kaslr_is_enabled = false; +void __init kaslr_init (void ) +{if (kaslr_feature_override.val & kaslr_feature_override.mask &0xf ) {pr_info ("KASLR disabled on command line\n" ); -return 0 ; +return ; } -if (!kaslr_enabled ()) { +/* +* The KASLR offset modulo MIN_KIMG_ALIGN is taken from the physical +* placement of the image rather than from the seed, so a displacement +* of less than MIN_KIMG_ALIGN means that no seed was provided. +*/ +if (kaslr_offset () <MIN_KIMG_ALIGN ) {pr_warn ("KASLR disabled due to lack of seed\n" ); -return 0 ; +return ; }pr_info ("KASLR enabled\n" ); + __kaslr_is_enabled = true; +} + +int kaslr_module_init (void ) +{ +u64 module_range; +u32 seed; + +/* +* Set a reasonable default for module_alloc_base in case +* we end up running with module randomization disabled. +*/ + module_alloc_base = (u64 )_etext -MODULES_VSIZE ; seed =get_random_u32 ();
@@ -80,4 +91,4 @@ static int __init kaslr_init(void)return 0 ; } -subsys_initcall (kaslr_init) +subsys_initcall (kaslr_module_init)
arch/arm64/kernel/setup.c +2 -0
@@ -296,6 +296,8 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p) *cmdline_p = boot_command_line; +kaslr_init (); +/* * If know now we are going to need KPTI then use non-global * mappings from the start, avoiding the cost of rewriting
Signature · 7 lines
2.30.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Mark Rutland
reviewerPATCH v3 4/6 arm64: module: move module randomization to module.c
In reply to Mark RutlandWhen CONFIG_RANDOMIZE_BASE=y, module_alloc_base is a variable which is configured by kaslr_module_init() in kaslr.c, and otherwise it is an expression defined in module.h.
As kaslr_module_init() is no longer tightly coupled with the KASLR initialization code, we can centralize this in module.c.
This patch moves kaslr_module_init() to module.c, making module_alloc_base a static variable, and removing redundant includes from kaslr.c. For the defintion of struct arm64_ftr_override we must include <asm/cpufeature.h>, which was previously included transitively via another header.
There should be no functional change as a result of this patch.
--- arch/arm64/include/asm/module.h | 6 ---- arch/arm64/kernel/kaslr.c | 60 ++------------------------------- arch/arm64/kernel/module.c | 48 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 64 deletions(-)
arch/arm64/include/asm/module.h +0 -6
@@ -30,12 +30,6 @@ u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs,u64 module_emit_veneer_for_adrp (struct module *mod,Elf64_Shdr *sechdrs,void *loc,u64 val); -#ifdef CONFIG_RANDOMIZE_BASE -extern u64 module_alloc_base; -#else -#define module_alloc_base ((u64)_etext - MODULES_VSIZE) -#endif -struct plt_entry {/* * A program that conforms to the AArch64 Procedure Call Standard
arch/arm64/kernel/kaslr.c +2 -58
73 lines not on this page · raw message
arch/arm64/kernel/module.c +48 -0
61 lines not on this page · raw message
Signature · 7 lines
2.30.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Mark Rutland
reviewerPATCH v3 5/6 arm64: module: mandate MODULE_PLTS
In reply to Mark RutlandContemporary kernels and modules can be relatively large, especially when common debug options are enabled. Using GCC 12.1.0, a v6.3-rc7 defconfig kernel is ~38M, and with PROVE_LOCKING + KASAN_INLINE enabled this expands to ~117M. Shanker reports [1] that the NVIDIA GPU driver alone can consume 110M of module space in some configurations.
Both KASLR and ARM64_ERRATUM_843419 select MODULE_PLTS, so anyone wanting a kernel to have KASLR or run on Cortex-A53 will have MODULE_PLTS selected. This is the case in defconfig and distribution kernels (e.g. Debian, Android, etc).
Practically speaking, this means we're very likely to need MODULE_PLTS and while it's almost guaranteed that MODULE_PLTS will be selected, it is possible to disable support, and we have to maintain some awkward special cases for such unusual configurations.
This patch removes the MODULE_PLTS config option, with the support code always enabled if MODULES is selected. This results in a slight simplification, and will allow for further improvement in subsequent patches.
For any config which currently selects MODULE_PLTS, there will be no functional change as a result of this patch.
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
--- arch/arm64/Kconfig | 28 +++------------------------- arch/arm64/include/asm/module.h | 2 -- arch/arm64/include/asm/module.lds.h | 2 -- arch/arm64/kernel/Makefile | 3 +-- arch/arm64/kernel/ftrace.c | 8 +++----- arch/arm64/kernel/module.c | 25 ++++++++++++------------- 6 files changed, 19 insertions(+), 49 deletions(-)
arch/arm64/Kconfig +3 -25
@@ -207,6 +207,7 @@ config ARM64 select HAVE_IOREMAP_PROT select HAVE_IRQ_TIME_ACCOUNTING select HAVE_KVM + select HAVE_MOD_ARCH_SPECIFIC select HAVE_NMI select HAVE_PERF_EVENTS select HAVE_PERF_REGS
@@ -577,7 +578,6 @@ config ARM64_ERRATUM_845719 config ARM64_ERRATUM_843419 bool "Cortex-A53: 843419: A load or store might access an incorrect address" default y - select ARM64_MODULE_PLTS if MODULES help This option links the kernel with '--fix-cortex-a53-843419' and enables PLT support to replace certain ADRP instructions, which can
@@ -2107,26 +2107,6 @@ config ARM64_SME register state capable of holding two dimensional matrix tiles to enable various matrix operations. -config ARM64_MODULE_PLTS - bool "Use PLTs to allow module memory to spill over into vmalloc area" - depends on MODULES - select HAVE_MOD_ARCH_SPECIFIC - help - Allocate PLTs when loading modules so that jumps and calls whose - targets are too far away for their relative offsets to be encoded - in the instructions themselves can be bounced via veneers in the - module's PLT. This allows modules to be allocated in the generic - vmalloc area after the dedicated module memory area has been - exhausted. - - When running with address space randomization (KASLR), the module - region itself may be too far away for ordinary relative jumps and - calls, and so in that case, module PLTs are required and cannot be - disabled. - - Specific errata workaround(s) might also force module PLTs to be - enabled (ARM64_ERRATUM_843419). - config ARM64_PSEUDO_NMI bool "Support for NMI-like interrupts" select ARM_GIC_V3
@@ -2167,7 +2147,6 @@ config RELOCATABLE config RANDOMIZE_BASE bool "Randomize the address of the kernel image" - select ARM64_MODULE_PLTS if MODULES select RELOCATABLE help Randomizes the virtual address at which the kernel image is
@@ -2198,9 +2177,8 @@ config RANDOMIZE_MODULE_REGION_FULL When this option is not set, the module region will be randomized over a limited range that contains the [_stext, _etext] interval of the core kernel, so branch relocations are almost always in range unless - ARM64_MODULE_PLTS is enabled and the region is exhausted. In this - particular case of region exhaustion, modules might be able to fall - back to a larger 2GB area. + the region is exhausted. In this particular case of region + exhaustion, modules might be able to fall back to a larger 2GB area. config CC_HAVE_STACKPROTECTOR_SYSREG def_bool $(cc-option,-mstack-protector-guard=sysreg -mstack-protector-guard-reg=sp_el0 -mstack-protector-guard-offset=0)
arch/arm64/include/asm/module.h +0 -2
@@ -7,7 +7,6 @@#include <asm-generic/module.h> -#ifdef CONFIG_ARM64_MODULE_PLTSstruct mod_plt_sec {int plt_shndx;int plt_num_entries;
@@ -21,7 +20,6 @@ struct mod_arch_specific {/* for CONFIG_DYNAMIC_FTRACE */ struct plt_entry *ftrace_trampolines; }; -#endif u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs, void *loc, const Elf64_Rela *rela,
arch/arm64/include/asm/module.lds.h +0 -2
@@ -1,9 +1,7 @@ SECTIONS { -#ifdef CONFIG_ARM64_MODULE_PLTS .plt 0 : { BYTE(0) } .init.plt 0 : { BYTE(0) } .text.ftrace_trampoline 0 : { BYTE(0) } -#endif#ifdef CONFIG_KASAN_SW_TAGS /*
arch/arm64/kernel/Makefile +1 -2
@@ -42,8 +42,7 @@ obj-$(CONFIG_COMPAT) += sigreturn32.o obj-$(CONFIG_COMPAT_ALIGNMENT_FIXUPS) += compat_alignment.o obj-$(CONFIG_KUSER_HELPERS) += kuser32.o obj-$(CONFIG_FUNCTION_TRACER) += ftrace.o entry-ftrace.o -obj-$(CONFIG_MODULES) += module.o -obj-$(CONFIG_ARM64_MODULE_PLTS) += module-plts.o +obj-$(CONFIG_MODULES) += module.o module-plts.o obj-$(CONFIG_PERF_EVENTS) += perf_regs.o perf_callchain.o obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o obj-$(CONFIG_CPU_PM) += sleep.o suspend.o
arch/arm64/kernel/ftrace.c +3 -5
@@ -197,7 +197,7 @@ int ftrace_update_ftrace_func(ftrace_func_t func)static struct plt_entry *get_ftrace_plt (struct module *mod) { -#ifdef CONFIG_ARM64_MODULE_PLTS +#ifdef CONFIG_MODULES struct plt_entry *plt = mod->arch.ftrace_trampolines;return &plt[FTRACE_PLT_IDX ];
@@ -249,7 +249,7 @@ static bool ftrace_find_callable_addr(struct dyn_ftrace *rec,* must use a PLT to reach it. We can only place PLTs for modules, and * only when module PLT support is built-in. */ -if (!IS_ENABLED (CONFIG_ARM64_MODULE_PLTS )) +if (!IS_ENABLED (CONFIG_MODULES ))return false;/*
@@ -431,10 +431,8 @@ int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,* * Note: 'mod' is only set at module load time. */ -if (!IS_ENABLED (CONFIG_DYNAMIC_FTRACE_WITH_ARGS ) && -IS_ENABLED (CONFIG_ARM64_MODULE_PLTS ) && mod) { +if (!IS_ENABLED (CONFIG_DYNAMIC_FTRACE_WITH_ARGS ) && mod)return aarch64_insn_patch_text_nosync ((void *)pc, new); - }if (!ftrace_find_callable_addr (rec, mod, &addr))return -EINVAL ;
arch/arm64/kernel/module.c +12 -13
@@ -73,25 +73,26 @@ subsys_initcall(kaslr_module_init)void *module_alloc (unsigned long size) {u64 module_alloc_end = module_alloc_base +MODULES_VSIZE ; -gfp_t gfp_mask =GFP_KERNEL ;void *p; -/* Silence the initial allocation */ -if (IS_ENABLED (CONFIG_ARM64_MODULE_PLTS )) - gfp_mask |= __GFP_NOWARN; - +/* +* Where possible, prefer to allocate within direct branch range of the +* kernel such that no PLTs are necessary. This may fail, so we pass +* __GFP_NOWARN to silence the resulting warning. +*/ p =__vmalloc_node_range (size,MODULE_ALIGN , module_alloc_base, - module_alloc_end, gfp_mask,PAGE_KERNEL ,0 , -NUMA_NO_NODE ,__builtin_return_address (0 )); + module_alloc_end,GFP_KERNEL | __GFP_NOWARN, +PAGE_KERNEL ,0 ,NUMA_NO_NODE , +__builtin_return_address (0 )); -if (!p &&IS_ENABLED (CONFIG_ARM64_MODULE_PLTS )) { +if (!p) { p =__vmalloc_node_range (size,MODULE_ALIGN , module_alloc_base, module_alloc_base +SZ_2G ,GFP_KERNEL ,PAGE_KERNEL ,0 ,NUMA_NO_NODE ,__builtin_return_address (0 )); } -if (p && (kasan_alloc_module_shadow (p, size, gfp_mask) <0 )) { +if (p && (kasan_alloc_module_shadow (p, size,GFP_KERNEL ) <0 )) {vfree (p);return NULL ; }
@@ -479,9 +480,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,case R_AARCH64_CALL26 : ovf =reloc_insn_imm (RELOC_OP_PREL , loc, val,2 ,26 ,AARCH64_INSN_IMM_26 ); - -if (IS_ENABLED (CONFIG_ARM64_MODULE_PLTS ) && - ovf == -ERANGE ) { +if (ovf == -ERANGE ) { val =module_emit_plt_entry (me, sechdrs, loc, &rel[i], sym);if (!val)return -ENOEXEC ;
@@ -518,7 +517,7 @@ static int module_init_ftrace_plt(const Elf_Ehdr *hdr,const Elf_Shdr *sechdrs, struct module *mod) { -#if defined(CONFIG_ARM64_MODULE_PLTS) && defined(CONFIG_DYNAMIC_FTRACE) +#if defined(CONFIG_DYNAMIC_FTRACE) const Elf_Shdr *s;struct plt_entry *plts;
Signature · 7 lines
2.30.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Mark Rutland
reviewerPATCH v3 6/6 arm64: module: rework module VA range selection
In reply to Mark RutlandCurrently, the modules region is 128M in size, which is a problem for some large modules. Shanker reports [1] that the NVIDIA GPU driver alone can consume 110M of module space in some configurations. We'd like to make the modules region a full 2G such that we can always make use of a 2G range.
It's possible to build kernel images which are larger than 128M in some configurations, such as when many debug options are selected and many drivers are built in. In these configurations, we can't legitimately select a base for a 128M module region, though we currently select a value for which allocation will fail. It would be nicer to have a diagnostic message in this case.
Similarly, in theory it's possible to build a kernel image which is larger than 2G and which cannot support modules. While this isn't likely to be the case for any realistic kernel deplyed in the field, it would be nice if we could print a diagnostic in this case.
This patch reworks the module VA range selection to use a 2G range, and improves handling of cases where we cannot select legitimate module regions. We now attempt to select a 128M region and a 2G region:
* The 128M region is selected such that modules can use direct branches (with JUMP26/CALL26 relocations) to branch to kernel code and other modules, and so that modules can reference data and text (using PREL32 relocations) anywhere in the kernel image and other modules.
This region covers the entire kernel image (rather than just the text) to ensure that all PREL32 relocations are in range even when the kernel data section is absurdly large. Where we cannot allocate from this region, we'll fall back to the full 2G region.
* The 2G region is selected such that modules can use direct branches with PLTs to branch to kernel code and other modules, and so that modules can use reference data and text (with PREL32 relocations) in the kernel image and other modules.
This region covers the entire kernel image, and the 128M region (if one is selected).
The two module regions are randomized independently while ensuring the constraints described above.
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
--- Documentation/arm64/memory.rst | 8 +- arch/arm64/include/asm/memory.h | 2 +- arch/arm64/kernel/module.c | 139 +++++++++++++++++++++----------- 3 files changed, 99 insertions(+), 50 deletions(-)
Documentation/arm64/memory.rst +4 -4
@@ -33,8 +33,8 @@ AArch64 Linux memory layout with 4KB pages + 4 levels (48-bit):: 0000000000000000 0000ffffffffffff 256TB user ffff000000000000 ffff7fffffffffff 128TB kernel logical memory map [ffff600000000000 ffff7fffffffffff] 32TB [kasan shadow region] - ffff800000000000 ffff800007ffffff 128MB modules - ffff800008000000 fffffbffefffffff 124TB vmalloc + ffff800000000000 ffff80007fffffff 2GB modules + ffff800080000000 fffffbffefffffff 124TB vmalloc fffffbfff0000000 fffffbfffdffffff 224MB fixed mappings (top down) fffffbfffe000000 fffffbfffe7fffff 8MB [guard region] fffffbfffe800000 fffffbffff7fffff 16MB PCI I/O space
@@ -50,8 +50,8 @@ AArch64 Linux memory layout with 64KB pages + 3 levels (52-bit with HW support): 0000000000000000 000fffffffffffff 4PB user fff0000000000000 ffff7fffffffffff ~4PB kernel logical memory map [fffd800000000000 ffff7fffffffffff] 512TB [kasan shadow region] - ffff800000000000 ffff800007ffffff 128MB modules - ffff800008000000 fffffbffefffffff 124TB vmalloc + ffff800000000000 ffff80007fffffff 2GB modules + ffff800080000000 fffffbffefffffff 124TB vmalloc fffffbfff0000000 fffffbfffdffffff 224MB fixed mappings (top down) fffffbfffe000000 fffffbfffe7fffff 8MB [guard region] fffffbfffe800000 fffffbffff7fffff 16MB PCI I/O space
arch/arm64/include/asm/memory.h +1 -1
@@ -46,7 +46,7 @@#define KIMAGE_VADDR (MODULES_END)#define MODULES_END (MODULES_VADDR + MODULES_VSIZE)#define MODULES_VADDR (_PAGE_END(VA_BITS_MIN)) -#define MODULES_VSIZE (SZ_128M) +#define MODULES_VSIZE (SZ_2G)#define VMEMMAP_START (-(UL(1) << (VA_BITS - VMEMMAP_SHIFT)))#define VMEMMAP_END (VMEMMAP_START + VMEMMAP_SIZE)#define PCI_IO_END (VMEMMAP_START - SZ_8M)
arch/arm64/kernel/module.c +94 -45
172 lines not on this page · raw message
Signature · 7 lines
2.30.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Hi Mark,
I've reviewed and verified on NVIDIA server platforms w/ and w/o KASLR feature is disabled.
root@localhost:~# dmesg | grep -E "Modules:|KASLR" [ 0.000000] KASLR disabled on command line [ 5.961360] Modules: 1223 pages in range for non-PLT usage [ 5.961362] Modules: 31943 pages in range for PLT usage
Looks good to me.
Quoted 77 lines · Mark Rutland
External email: Use caution opening links or attachments
This series (based on v6.4-rc3) aims to make arm64's module allocation code more robust. To that end:
* Redundant code for KASAN && !KASAN_VMALLOC is removed, as this combination is no longer possible.
* Module PLT support is mandated, always allowing for the use of a 2G module region. Practically speaking, this is already the case for almost all users as module PLT support is mandated by both KASLR (which most distros including Android, enable this), and by the workaround for ARM erratum 843419 (required by Cortex-A53).
* The module VA region selection is moved to
module.c, making it self-contained and easier to follow.* The default module VA region is expanded to 2G. This ensures that there is sufficient space for the full region using PLTs even when KASLR is disabled or no seed provided.
* The module VA range init code is updated to log when the kernel is too large to support the use of a 128M or 2G module region, to enable debugging of these cases. Contemporary kernels built with debug options can be bigger than 128M, and a kernel bigger than 2G is unlikely but theoretically possible. Adding this logging should help to debug or filter away reports for such cases.
This should allow for loading of very large modules, as Shanker reported was an issue:
https://lore.kernel.org/linux-arm-kernel/[email protected]/ https://lore.kernel.org/linux-arm-kernel/[email protected]/... and as Ard had an alternative series for:
https://lore.kernel.org/linux-arm-kernel/[email protected]/Since v1 [1]:
* Log the number of pages in range
* Remove unusedkasan_mod_shadow_end
* Only randomize whenkaslr_enabled()
* Simplify control-flowSince v2 [2]:
* Apply Ard's Reviewed-by tags
* Fix typos
* Rebave to v6.4-rc3 (trivial)[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/[email protected]/Thanks,
Mark.Mark Rutland (6):
arm64: module: remove old !KASAN_VMALLOC logic
arm64: kasan: remove !KASAN_VMALLOC remnants
arm64: kaslr: split kaslr/module initialization
arm64: module: move module randomization tomodule.c
arm64: module: mandateMODULE_PLTS
arm64: module: rework module VA range selectionDocumentation/arm64/memory.rst | 8 +- arch/arm64/Kconfig | 28 +---- arch/arm64/include/asm/memory.h | 16 +-- arch/arm64/include/asm/module.h | 8 -- arch/arm64/include/asm/module.lds.h | 2 - arch/arm64/kernel/Makefile | 3 +- arch/arm64/kernel/ftrace.c | 8 +- arch/arm64/kernel/kaslr.c | 83 +++------------ arch/arm64/kernel/module.c | 159 +++++++++++++++++++++------- arch/arm64/kernel/setup.c | 2 + arch/arm64/mm/kasan_init.c | 17 +-- 11 files changed, 159 insertions(+), 175 deletions(-)
Quoted 3 lines
--
2.30.2
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 6 lines · Mark Rutland
This series (based on v6.4-rc3) aims to make arm64's module allocation code more robust. To that end:
* Redundant code for KASAN && !KASAN_VMALLOC is removed, as this combination is no longer possible.
Quoted 7 lines
* Module PLT support is mandated, always allowing for the use of a 2G module region. Practically speaking, this is already the case for almost all users as module PLT support is mandated by both KASLR (which most distros including Android, enable this), and by the workaround for ARM erratum 843419 (required by Cortex-A53).
[...]
Applied to arm64 (for-next/module-alloc), thanks!
[1/6] arm64: module: remove old !KASAN_VMALLOC logic
https://git.kernel.org/arm64/c/8339f7d8e178 [2/6] arm64: kasan: remove !KASAN_VMALLOC remnants https://git.kernel.org/arm64/c/55123afffe93 [3/6] arm64: kaslr: split kaslr/module initialization https://git.kernel.org/arm64/c/6e13b6b923b3 [4/6] arm64: module: move module randomization to module.c https://git.kernel.org/arm64/c/e46b7103aef3 [5/6] arm64: module: mandate MODULE_PLTS https://git.kernel.org/arm64/c/ea3752ba9685 [6/6] arm64: module: rework module VA range selection https://git.kernel.org/arm64/c/3e35d303ab7d
Signature · 7 lines
Catalin _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel