From: Ard Biesheuvel In order to permit the module loader to allocate MOD_TEXT and MOD_INIT_TEXT from the same chunk of memory, add an API function to execmem that splits an existing execmem cache allocation in two. This will be used on arm64 to avoid .text and .init.text being placed far away from each other. Signed-off-by: Ard Biesheuvel --- include/linux/execmem.h | 9 ++++ mm/execmem.c | 44 ++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 7de229134e30..59bd862cb061 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -178,6 +178,15 @@ void execmem_free(void *ptr); DEFINE_FREE(execmem, void *, if (_T) execmem_free(_T)); +/** + * execmem_split - allocate space from an existing execmem cache allocation + * @ptr - the existing execmem cache allocation + * @size - the size to carve out from the existing allocation + * + * Return: the address of the carved out allocation, or %NULL on failure. + */ +void *execmem_split(void *ptr, size_t size); + #ifdef CONFIG_MMU /** * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory diff --git a/mm/execmem.c b/mm/execmem.c index 084a207e4278..6db0c1d82a9d 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -439,6 +439,33 @@ static bool execmem_cache_free(void *ptr) return true; } +static void *execmem_cache_split(void *ptr, size_t size) +{ + struct maple_tree *busy_areas = &execmem_cache.busy_areas; + unsigned long addr = (unsigned long)ptr; + MA_STATE(mas, busy_areas, addr, addr); + void *area; + int err; + + guard(mutex)(&execmem_cache.mutex); + + area = mas_walk(&mas); + if (!area) + return ERR_PTR(-ENOENT); + + if (size >= mas_range_len(&mas)) + return ERR_PTR(-EINVAL); + + addr += mas_range_len(&mas) - size; + mas_set_range(&mas, addr, mas.last); + + err = mas_store_gfp(&mas, (void *)addr, GFP_KERNEL); + if (err) + return ERR_PTR(err); + + return (void *)addr; +} + #else /* CONFIG_ARCH_HAS_EXECMEM_ROX */ /* * when ROX cache is not used the permissions defined by architectures for @@ -459,6 +486,11 @@ static bool execmem_cache_free(void *ptr) { return false; } + +static void *execmem_cache_split(void *ptr, size_t size) +{ + return ERR_PTR(-ENOENT); +} #endif /* CONFIG_ARCH_HAS_EXECMEM_ROX */ void *execmem_alloc(enum execmem_type type, size_t size) @@ -511,6 +543,18 @@ bool execmem_is_rox(enum execmem_type type) return !!(execmem_info->ranges[type].flags & EXECMEM_ROX_CACHE); } +void *execmem_split(void *ptr, size_t size) +{ + if (!ptr || !size) + return NULL; + + ptr = execmem_cache_split(ptr, size); + if (!IS_ERR(ptr)) + return ptr; + VM_WARN_ON(ptr != ERR_PTR(-ENOMEM)); + return NULL; +} + static bool execmem_validate(struct execmem_info *info) { struct execmem_range *r = &info->ranges[EXECMEM_DEFAULT]; -- 2.55.0.860.g4b6b3295ed-goog From: Ard Biesheuvel Only recent arm64 systems are guaranteed to be able to manipulate the permissions on live kernel mappings that may use huge mappings. Older ones can only do so on live mappings that are mapped down to pages. In order to make execmem caches work on arm64 despite this distinction, allow huge vmappings to be omitted when allocating the caches. Signed-off-by: Ard Biesheuvel --- include/linux/execmem.h | 2 ++ mm/execmem.c | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 59bd862cb061..b04926f3fff2 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -48,10 +48,12 @@ enum execmem_type { * enum execmem_range_flags - options for executable memory allocations * @EXECMEM_KASAN_SHADOW: allocate kasan shadow * @EXECMEM_ROX_CACHE: allocations should use ROX cache of huge pages + * @EXECMEM_NO_HUGE_VMAP: cache allocations must avoid huge vmappings */ enum execmem_range_flags { EXECMEM_KASAN_SHADOW = (1 << 0), EXECMEM_ROX_CACHE = (1 << 1), + EXECMEM_NO_HUGE_VMAP = (1 << 2), }; #ifdef CONFIG_ARCH_HAS_EXECMEM_ROX diff --git a/mm/execmem.c b/mm/execmem.c index 6db0c1d82a9d..5790d4a8532a 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -276,7 +276,8 @@ static void *__execmem_cache_alloc(struct execmem_range *range, size_t size) static void *execmem_cache_populate_alloc(struct execmem_range *range, size_t size) { - unsigned long vm_flags = VM_ALLOW_HUGE_VMAP; + unsigned long vm_flags = (range->flags & EXECMEM_NO_HUGE_VMAP) + ? 0 : VM_ALLOW_HUGE_VMAP; struct mutex *mutex = &execmem_cache.mutex; struct vm_struct *vm; size_t alloc_size; -- 2.55.0.860.g4b6b3295ed-goog From: Ard Biesheuvel Reorder enum mod_mem_type so that MOD_TEXT appears right before MOD_INIT_TEXT. This will result in MOD_INIT_TEXT being allocated right after MOD_TEXT when the allocation logic iterates over the memory types in enum declaration order. In a subsequent patch, this will be taken advantage of, by allocating .text and .init.text together, and freeing .init.text by truncating the allocation. Doing so without this reordering would likely result in more fragmentation, as the truncated .text allocation would be followed by .rodata and .data/.bss of the same module. Signed-off-by: Ard Biesheuvel --- include/linux/module.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index 7566815fabbe..0336a46733dd 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -316,10 +316,10 @@ struct mod_tree_node { }; enum mod_mem_type { - MOD_TEXT = 0, - MOD_DATA, + MOD_DATA = 0, MOD_RODATA, MOD_RO_AFTER_INIT, + MOD_TEXT, MOD_INIT_TEXT, MOD_INIT_DATA, MOD_INIT_RODATA, -- 2.55.0.860.g4b6b3295ed-goog From: Ard Biesheuvel When execmem ROX caches are used for module text and inittext, place them adjacently in memory, by allocating space for both initially, and splitting off the space for MOD_INIT_TEXT as needed. This avoids the corner case on arm64, where .init.text being placed far from .text results in a lot of complexity wrt indirect branches and PLTs that we'd prefer to avoid. Signed-off-by: Ard Biesheuvel --- kernel/module/main.c | 22 ++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 46dd8d25a605..2d6213496359 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1342,7 +1342,7 @@ 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; + void *ptr = NULL; mod->mem[type].size = size; @@ -1351,11 +1351,25 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type) else execmem_type = EXECMEM_MODULE_TEXT; - ptr = execmem_alloc_rw(execmem_type, size); + bool is_rox = execmem_is_rox(execmem_type); + if (is_rox) { + /* + * Special case for MOD_TEXT / MOD_INIT_TEXT: allocate the + * latter by splitting off required space from the former + * so that they are always placed close together. + */ + if (type == MOD_TEXT) + size += PAGE_ALIGN(mod->mem[MOD_INIT_TEXT].size); + else if (type == MOD_INIT_TEXT) + ptr = execmem_split(mod->mem[MOD_TEXT].base, size); + } + + if (!ptr) + ptr = execmem_alloc_rw(execmem_type, size); if (!ptr) return -ENOMEM; - mod->mem[type].is_rox = execmem_is_rox(execmem_type); + mod->mem[type].is_rox = is_rox; /* * The pointer to these blocks of memory are stored on the module @@ -1368,7 +1382,7 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type) * *do* eventually get freed, but let's just keep things simple * and avoid *any* false positives. */ - if (!mod->mem[type].is_rox) + if (!is_rox) kmemleak_not_leak(ptr); memset(ptr, 0, size); -- 2.55.0.860.g4b6b3295ed-goog From: Ard Biesheuvel Allow permission changes on huge vmappings in cases where no splitting is needed (i.e., the region is aligned sufficiently), or when the system has support for splitting live mappings. Signed-off-by: Ard Biesheuvel --- arch/arm64/mm/pageattr.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c index bbe98ac9ad8c..20ff9cb273c1 100644 --- a/arch/arm64/mm/pageattr.c +++ b/arch/arm64/mm/pageattr.c @@ -169,8 +169,6 @@ static int change_memory_common(unsigned long addr, int numpages, * we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). - * Disallow VM_ALLOW_HUGE_VMAP mappings to guarantee that only page - * mappings are updated and splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely * covered by precisely one VM area that has the VM_ALLOC flag set. @@ -179,7 +177,16 @@ static int change_memory_common(unsigned long addr, int numpages, if (!area || ((unsigned long)kasan_reset_tag((void *)end) > (unsigned long)kasan_reset_tag(area->addr) + area->size) || - ((area->flags & (VM_ALLOC | VM_ALLOW_HUGE_VMAP)) != VM_ALLOC)) + !(area->flags & VM_ALLOC)) + return -EINVAL; + + /* + * Disallow VM_ALLOW_HUGE_VMAP mappings unless the region is PMD + * aligned, or splitting live huge mappings is supported. + */ + if ((area->flags & VM_ALLOW_HUGE_VMAP) && + ((start % PMD_SIZE) || (size % PMD_SIZE)) && + WARN_ON_ONCE(!system_supports_bbml2_noabort())) return -EINVAL; if (!numpages) -- 2.55.0.860.g4b6b3295ed-goog From: Ard Biesheuvel Wire up the existing support for the execmem ROX cache on arm64, so that it will be used for module .text and .init.text regions. This will be relied upon by a subsequent patch in order to allocate those regions from a single chunk of memory. Signed-off-by: Ard Biesheuvel --- arch/arm64/Kconfig | 1 + arch/arm64/mm/init.c | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index b3afe0688919..080b97b0bbd5 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -27,6 +27,7 @@ config ARM64 select ARCH_HAS_DMA_OPS if XEN select ARCH_HAS_DMA_PREP_COHERENT select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI + select ARCH_HAS_EXECMEM_ROX select ARCH_HAS_FAST_MULTIPLIER select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_GCOV_PROFILE_ALL diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 97987f850a33..e6c98045046e 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -531,7 +531,8 @@ struct execmem_info __init *execmem_arch_setup(void) [EXECMEM_DEFAULT] = { .start = start, .end = end, - .pgprot = PAGE_KERNEL, + .pgprot = PAGE_KERNEL_ROX, + .flags = EXECMEM_ROX_CACHE, .alignment = 1, .fallback_start = fallback_start, .fallback_end = fallback_end, @@ -548,9 +549,25 @@ struct execmem_info __init *execmem_arch_setup(void) .pgprot = PAGE_KERNEL, .alignment = 1, }, + [EXECMEM_MODULE_DATA] = { + .start = start, + .end = end, + .pgprot = PAGE_KERNEL, + .alignment = 1, + .fallback_start = fallback_start, + .fallback_end = fallback_end, + }, }, }; + if (!system_supports_bbml2_noabort()) + execmem_info.ranges[EXECMEM_DEFAULT].flags |= EXECMEM_NO_HUGE_VMAP; + return &execmem_info; } + +void execmem_fill_trapping_insns(void *ptr, size_t size) +{ + memset32(ptr, AARCH64_BREAK_FAULT, size / sizeof(__le32)); +} #endif /* CONFIG_EXECMEM */ -- 2.55.0.860.g4b6b3295ed-goog From: Ard Biesheuvel This reverts commit a7ed7b9d0ebb038db9963d574da0311cab0b666a, which is no longer needed now that the corner case where .init.text is placed out of direct branching range from .text of the same module can no longer occur. Signed-off-by: Ard Biesheuvel --- arch/arm64/include/asm/module.h | 1 - arch/arm64/include/asm/module.lds.h | 1 - arch/arm64/kernel/ftrace.c | 13 +++---------- arch/arm64/kernel/module-plts.c | 12 +----------- arch/arm64/kernel/module.c | 11 ----------- 5 files changed, 4 insertions(+), 34 deletions(-) diff --git a/arch/arm64/include/asm/module.h b/arch/arm64/include/asm/module.h index fb9b88eebeb1..79550b22ba19 100644 --- a/arch/arm64/include/asm/module.h +++ b/arch/arm64/include/asm/module.h @@ -19,7 +19,6 @@ struct mod_arch_specific { /* for CONFIG_DYNAMIC_FTRACE */ struct plt_entry *ftrace_trampolines; - struct plt_entry *init_ftrace_trampolines; }; u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs, diff --git a/arch/arm64/include/asm/module.lds.h b/arch/arm64/include/asm/module.lds.h index 0b3aacd22c59..0c3aea98e116 100644 --- a/arch/arm64/include/asm/module.lds.h +++ b/arch/arm64/include/asm/module.lds.h @@ -2,7 +2,6 @@ SECTIONS { .plt 0 : { BYTE(0) } .init.plt 0 : { BYTE(0) } .text.ftrace_trampoline 0 : { BYTE(0) } - .init.text.ftrace_trampoline 0 : { BYTE(0) } #ifdef CONFIG_KASAN_SW_TAGS /* diff --git a/arch/arm64/kernel/ftrace.c b/arch/arm64/kernel/ftrace.c index 5a1554a44162..b56da24c1be4 100644 --- a/arch/arm64/kernel/ftrace.c +++ b/arch/arm64/kernel/ftrace.c @@ -258,17 +258,10 @@ int ftrace_update_ftrace_func(ftrace_func_t func) return ftrace_modify_code(pc, 0, new, false); } -static struct plt_entry *get_ftrace_plt(struct module *mod, unsigned long addr) +static struct plt_entry *get_ftrace_plt(struct module *mod) { #ifdef CONFIG_MODULES - struct plt_entry *plt = NULL; - - if (within_module_mem_type(addr, mod, MOD_INIT_TEXT)) - plt = mod->arch.init_ftrace_trampolines; - else if (within_module_mem_type(addr, mod, MOD_TEXT)) - plt = mod->arch.ftrace_trampolines; - else - return NULL; + struct plt_entry *plt = mod->arch.ftrace_trampolines; return &plt[FTRACE_PLT_IDX]; #else @@ -339,7 +332,7 @@ static bool ftrace_find_callable_addr(struct dyn_ftrace *rec, if (WARN_ON(!mod)) return false; - plt = get_ftrace_plt(mod, pc); + plt = get_ftrace_plt(mod); if (!plt) { pr_err("ftrace: no module PLT for %ps\n", (void *)*addr); return false; diff --git a/arch/arm64/kernel/module-plts.c b/arch/arm64/kernel/module-plts.c index 7afd370da9f4..bde32979c06a 100644 --- a/arch/arm64/kernel/module-plts.c +++ b/arch/arm64/kernel/module-plts.c @@ -283,7 +283,7 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, unsigned long core_plts = 0; unsigned long init_plts = 0; Elf64_Sym *syms = NULL; - Elf_Shdr *pltsec, *tramp = NULL, *init_tramp = NULL; + Elf_Shdr *pltsec, *tramp = NULL; int i; /* @@ -298,9 +298,6 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, else if (!strcmp(secstrings + sechdrs[i].sh_name, ".text.ftrace_trampoline")) tramp = sechdrs + i; - else if (!strcmp(secstrings + sechdrs[i].sh_name, - ".init.text.ftrace_trampoline")) - init_tramp = sechdrs + i; else if (sechdrs[i].sh_type == SHT_SYMTAB) syms = (Elf64_Sym *)sechdrs[i].sh_addr; } @@ -366,12 +363,5 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, tramp->sh_size = NR_FTRACE_PLTS * sizeof(struct plt_entry); } - if (init_tramp) { - init_tramp->sh_type = SHT_NOBITS; - init_tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC; - init_tramp->sh_addralign = __alignof__(struct plt_entry); - init_tramp->sh_size = NR_FTRACE_PLTS * sizeof(struct plt_entry); - } - return 0; } diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c index 24adb581af0e..6e5b488a219e 100644 --- a/arch/arm64/kernel/module.c +++ b/arch/arm64/kernel/module.c @@ -466,17 +466,6 @@ static int module_init_ftrace_plt(const Elf_Ehdr *hdr, __init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR); mod->arch.ftrace_trampolines = plts; - - s = find_section(hdr, sechdrs, ".init.text.ftrace_trampoline"); - if (!s) - return -ENOEXEC; - - plts = (void *)s->sh_addr; - - __init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR); - - mod->arch.init_ftrace_trampolines = plts; - #endif return 0; } -- 2.55.0.860.g4b6b3295ed-goog From: Ard Biesheuvel It is no longer possible for .init.text to end up being placed out of direct branching range of the .text section of the same module, so the PLT array of core and init code can be combined again. It also means there is no longer a need to allocate PLT entries for cross-section calls within the same module, and so the upper bound for the number of needed entries can be lowered again as well. Signed-off-by: Ard Biesheuvel --- arch/arm64/include/asm/module.h | 1 - arch/arm64/include/asm/module.lds.h | 1 - arch/arm64/kernel/module-plts.c | 35 +++++--------------- 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/arch/arm64/include/asm/module.h b/arch/arm64/include/asm/module.h index 79550b22ba19..9328fadb4a95 100644 --- a/arch/arm64/include/asm/module.h +++ b/arch/arm64/include/asm/module.h @@ -15,7 +15,6 @@ struct mod_plt_sec { struct mod_arch_specific { struct mod_plt_sec core; - struct mod_plt_sec init; /* for CONFIG_DYNAMIC_FTRACE */ struct plt_entry *ftrace_trampolines; diff --git a/arch/arm64/include/asm/module.lds.h b/arch/arm64/include/asm/module.lds.h index 0c3aea98e116..24fc9401c514 100644 --- a/arch/arm64/include/asm/module.lds.h +++ b/arch/arm64/include/asm/module.lds.h @@ -1,6 +1,5 @@ SECTIONS { .plt 0 : { BYTE(0) } - .init.plt 0 : { BYTE(0) } .text.ftrace_trampoline 0 : { BYTE(0) } #ifdef CONFIG_KASAN_SW_TAGS diff --git a/arch/arm64/kernel/module-plts.c b/arch/arm64/kernel/module-plts.c index bde32979c06a..e30876b90e30 100644 --- a/arch/arm64/kernel/module-plts.c +++ b/arch/arm64/kernel/module-plts.c @@ -70,8 +70,7 @@ u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs, void *loc, const Elf64_Rela *rela, Elf64_Sym *sym) { - struct mod_plt_sec *pltsec = !within_module_init((unsigned long)loc, mod) ? - &mod->arch.core : &mod->arch.init; + struct mod_plt_sec *pltsec = &mod->arch.core; struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr; int i = pltsec->plt_num_entries; int j = i - 1; @@ -101,8 +100,7 @@ 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) { - struct mod_plt_sec *pltsec = !within_module_init((unsigned long)loc, mod) ? - &mod->arch.core : &mod->arch.init; + struct mod_plt_sec *pltsec = &mod->arch.core; struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr; int i = pltsec->plt_num_entries++; u32 br; @@ -169,17 +167,17 @@ static unsigned int count_plts(Elf64_Sym *syms, Elf64_Rela *rela, int num, case R_AARCH64_CALL26: /* * We only have to consider branch targets that resolve - * to symbols that are defined in a different section. + * to symbols that are not defined in the same module. * This is not simply a heuristic, it is a fundamental * limitation, since there is no guaranteed way to emit * PLT entries sufficiently close to the branch if the * section size exceeds the range of a branch * instruction. So ignore relocations against defined - * symbols if they live in the same section as the + * symbols if they live in the same module as the * relocation target. */ s = syms + ELF64_R_SYM(rela[i].r_info); - if (s->st_shndx == dstidx) + if (s->st_shndx != SHN_UNDEF) break; /* @@ -281,7 +279,6 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, char *secstrings, struct module *mod) { unsigned long core_plts = 0; - unsigned long init_plts = 0; Elf64_Sym *syms = NULL; Elf_Shdr *pltsec, *tramp = NULL; int i; @@ -293,8 +290,6 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, for (i = 0; i < ehdr->e_shnum; i++) { if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt")) mod->arch.core.plt_shndx = i; - else if (!strcmp(secstrings + sechdrs[i].sh_name, ".init.plt")) - mod->arch.init.plt_shndx = i; else if (!strcmp(secstrings + sechdrs[i].sh_name, ".text.ftrace_trampoline")) tramp = sechdrs + i; @@ -302,8 +297,8 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, syms = (Elf64_Sym *)sechdrs[i].sh_addr; } - if (!mod->arch.core.plt_shndx || !mod->arch.init.plt_shndx) { - pr_err("%s: module PLT section(s) missing\n", mod->name); + if (!mod->arch.core.plt_shndx) { + pr_err("%s: module PLT section missing\n", mod->name); return -ENOEXEC; } if (!syms) { @@ -332,12 +327,8 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, if (nents) sort(rels, nents, sizeof(Elf64_Rela), cmp_rela, NULL); - if (!module_init_layout_section(secstrings + dstsec->sh_name)) - core_plts += count_plts(syms, rels, numrels, - sechdrs[i].sh_info, dstsec); - else - init_plts += count_plts(syms, rels, numrels, - sechdrs[i].sh_info, dstsec); + core_plts += count_plts(syms, rels, numrels, sechdrs[i].sh_info, + dstsec); } pltsec = sechdrs + mod->arch.core.plt_shndx; @@ -348,14 +339,6 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, mod->arch.core.plt_num_entries = 0; mod->arch.core.plt_max_entries = core_plts; - pltsec = sechdrs + mod->arch.init.plt_shndx; - pltsec->sh_type = SHT_NOBITS; - pltsec->sh_flags = SHF_EXECINSTR | SHF_ALLOC; - pltsec->sh_addralign = L1_CACHE_BYTES; - pltsec->sh_size = (init_plts + 1) * sizeof(struct plt_entry); - mod->arch.init.plt_num_entries = 0; - mod->arch.init.plt_max_entries = init_plts; - if (tramp) { tramp->sh_type = SHT_NOBITS; tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC; -- 2.55.0.860.g4b6b3295ed-goog From: Ard Biesheuvel Instead of emitting the ftrace trampoline PLT into a separate ELF section, allocate them at the start of the ordinary PLT array, so that no special sections are needed. Signed-off-by: Ard Biesheuvel --- arch/arm64/include/asm/module.lds.h | 1 - arch/arm64/kernel/module-plts.c | 18 +++++------------- arch/arm64/kernel/module.c | 9 +-------- 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/arch/arm64/include/asm/module.lds.h b/arch/arm64/include/asm/module.lds.h index 24fc9401c514..95540e956b41 100644 --- a/arch/arm64/include/asm/module.lds.h +++ b/arch/arm64/include/asm/module.lds.h @@ -1,6 +1,5 @@ SECTIONS { .plt 0 : { BYTE(0) } - .text.ftrace_trampoline 0 : { BYTE(0) } #ifdef CONFIG_KASAN_SW_TAGS /* diff --git a/arch/arm64/kernel/module-plts.c b/arch/arm64/kernel/module-plts.c index e30876b90e30..6900d2242f1b 100644 --- a/arch/arm64/kernel/module-plts.c +++ b/arch/arm64/kernel/module-plts.c @@ -275,12 +275,14 @@ static int partition_branch_plt_relas(Elf64_Sym *syms, Elf64_Rela *rela, return i; } +static const int ftrace_plts = IS_ENABLED(CONFIG_DYNAMIC_FTRACE) ? NR_FTRACE_PLTS : 0; + int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, char *secstrings, struct module *mod) { - unsigned long core_plts = 0; + unsigned long core_plts = ftrace_plts; Elf64_Sym *syms = NULL; - Elf_Shdr *pltsec, *tramp = NULL; + Elf_Shdr *pltsec; int i; /* @@ -290,9 +292,6 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, for (i = 0; i < ehdr->e_shnum; i++) { if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt")) mod->arch.core.plt_shndx = i; - else if (!strcmp(secstrings + sechdrs[i].sh_name, - ".text.ftrace_trampoline")) - tramp = sechdrs + i; else if (sechdrs[i].sh_type == SHT_SYMTAB) syms = (Elf64_Sym *)sechdrs[i].sh_addr; } @@ -336,15 +335,8 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, pltsec->sh_flags = SHF_EXECINSTR | SHF_ALLOC; pltsec->sh_addralign = L1_CACHE_BYTES; pltsec->sh_size = (core_plts + 1) * sizeof(struct plt_entry); - mod->arch.core.plt_num_entries = 0; + mod->arch.core.plt_num_entries = ftrace_plts; mod->arch.core.plt_max_entries = core_plts; - if (tramp) { - tramp->sh_type = SHT_NOBITS; - tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC; - tramp->sh_addralign = __alignof__(struct plt_entry); - tramp->sh_size = NR_FTRACE_PLTS * sizeof(struct plt_entry); - } - return 0; } diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c index 6e5b488a219e..620c2b70115e 100644 --- a/arch/arm64/kernel/module.c +++ b/arch/arm64/kernel/module.c @@ -454,14 +454,7 @@ static int module_init_ftrace_plt(const Elf_Ehdr *hdr, struct module *mod) { #if defined(CONFIG_DYNAMIC_FTRACE) - const Elf_Shdr *s; - struct plt_entry *plts; - - s = find_section(hdr, sechdrs, ".text.ftrace_trampoline"); - if (!s) - return -ENOEXEC; - - plts = (void *)s->sh_addr; + struct plt_entry *plts = (void *)sechdrs[mod->arch.core.plt_shndx].sh_addr; __init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR); -- 2.55.0.860.g4b6b3295ed-goog