提交 7da1a660 编写于 作者: A Alex Sverdlin 提交者: Zheng Zengkai

ARM: 9079/1: ftrace: Add MODULE_PLTS support

stable inclusion
from stable-5.10.69
commit f91d25a7c89e2f246a541603430ce2420bab58c5
bugzilla: 182675 https://gitee.com/openeuler/kernel/issues/I4I3ED

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=f91d25a7c89e2f246a541603430ce2420bab58c5

-------------------------------------------------

commit 79f32b22 upstream

Teach ftrace_make_call() and ftrace_make_nop() about PLTs.
Teach PLT code about FTRACE and all its callbacks.
Otherwise the following might happen:

------------[ cut here ]------------
WARNING: CPU: 14 PID: 2265 at .../arch/arm/kernel/insn.c:14 __arm_gen_branch+0x83/0x8c()
...
Hardware name: LSI Axxia AXM55XX
[<c0314a49>] (unwind_backtrace) from [<c03115e9>] (show_stack+0x11/0x14)
[<c03115e9>] (show_stack) from [<c0519f51>] (dump_stack+0x81/0xa8)
[<c0519f51>] (dump_stack) from [<c032185d>] (warn_slowpath_common+0x69/0x90)
[<c032185d>] (warn_slowpath_common) from [<c03218f3>] (warn_slowpath_null+0x17/0x1c)
[<c03218f3>] (warn_slowpath_null) from [<c03143cf>] (__arm_gen_branch+0x83/0x8c)
[<c03143cf>] (__arm_gen_branch) from [<c0314337>] (ftrace_make_nop+0xf/0x24)
[<c0314337>] (ftrace_make_nop) from [<c038ebcb>] (ftrace_process_locs+0x27b/0x3e8)
[<c038ebcb>] (ftrace_process_locs) from [<c0378d79>] (load_module+0x11e9/0x1a44)
[<c0378d79>] (load_module) from [<c037974d>] (SyS_finit_module+0x59/0x84)
[<c037974d>] (SyS_finit_module) from [<c030e981>] (ret_fast_syscall+0x1/0x18)
---[ end trace e1b64ced7a89adcc ]---
------------[ cut here ]------------
WARNING: CPU: 14 PID: 2265 at .../kernel/trace/ftrace.c:1979 ftrace_bug+0x1b1/0x234()
...
Hardware name: LSI Axxia AXM55XX
[<c0314a49>] (unwind_backtrace) from [<c03115e9>] (show_stack+0x11/0x14)
[<c03115e9>] (show_stack) from [<c0519f51>] (dump_stack+0x81/0xa8)
[<c0519f51>] (dump_stack) from [<c032185d>] (warn_slowpath_common+0x69/0x90)
[<c032185d>] (warn_slowpath_common) from [<c03218f3>] (warn_slowpath_null+0x17/0x1c)
[<c03218f3>] (warn_slowpath_null) from [<c038e87d>] (ftrace_bug+0x1b1/0x234)
[<c038e87d>] (ftrace_bug) from [<c038ebd5>] (ftrace_process_locs+0x285/0x3e8)
[<c038ebd5>] (ftrace_process_locs) from [<c0378d79>] (load_module+0x11e9/0x1a44)
[<c0378d79>] (load_module) from [<c037974d>] (SyS_finit_module+0x59/0x84)
[<c037974d>] (SyS_finit_module) from [<c030e981>] (ret_fast_syscall+0x1/0x18)
---[ end trace e1b64ced7a89adcd ]---
ftrace failed to modify [<e9ef7006>] 0xe9ef7006
actual: 02:f0:3b:fa
ftrace record flags: 0
(0) expected tramp: c0314265
Signed-off-by: NAlexander Sverdlin <alexander.sverdlin@nokia.com>
Signed-off-by: NRussell King <rmk+kernel@armlinux.org.uk>
Signed-off-by: NFlorian Fainelli <f.fainelli@gmail.com>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>

Conflicts:
	arch/arm/include/asm/module.h
	arch/arm/kernel/module-plts.c
	arch/arm/kernel/ftrace.c

[ hf: To support both the ftrace and livepatch scenarios, roll back some
  modifications of the commit ee3cc1bededf ("arm/module: Use plt section
  indices for relocations"). ]
Signed-off-by: NLi Huafei <lihuafei1@huawei.com>
Reviewed-by: NYang Jihong <yangjihong1@huawei.com>
Signed-off-by: NChen Jun <chenjun102@huawei.com>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 7908b135
...@@ -15,6 +15,9 @@ extern void __gnu_mcount_nc(void); ...@@ -15,6 +15,9 @@ extern void __gnu_mcount_nc(void);
#ifdef CONFIG_DYNAMIC_FTRACE #ifdef CONFIG_DYNAMIC_FTRACE
struct dyn_arch_ftrace { struct dyn_arch_ftrace {
#ifdef CONFIG_ARM_MODULE_PLTS
struct module *mod;
#endif
}; };
static inline unsigned long ftrace_call_adjust(unsigned long addr) static inline unsigned long ftrace_call_adjust(unsigned long addr)
......
...@@ -29,6 +29,8 @@ struct plt_entries { ...@@ -29,6 +29,8 @@ struct plt_entries {
}; };
struct mod_plt_sec { struct mod_plt_sec {
struct elf32_shdr *plt;
struct plt_entries *plt_ent;
int plt_shndx; int plt_shndx;
int plt_count; int plt_count;
}; };
......
...@@ -68,9 +68,10 @@ int ftrace_arch_code_modify_post_process(void) ...@@ -68,9 +68,10 @@ int ftrace_arch_code_modify_post_process(void)
return 0; return 0;
} }
static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr) static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr,
bool warn)
{ {
return arm_gen_branch_link(pc, addr, true); return arm_gen_branch_link(pc, addr, warn);
} }
static int ftrace_modify_code(unsigned long pc, unsigned long old, static int ftrace_modify_code(unsigned long pc, unsigned long old,
...@@ -104,14 +105,14 @@ int ftrace_update_ftrace_func(ftrace_func_t func) ...@@ -104,14 +105,14 @@ int ftrace_update_ftrace_func(ftrace_func_t func)
int ret; int ret;
pc = (unsigned long)&ftrace_call; pc = (unsigned long)&ftrace_call;
new = ftrace_call_replace(pc, (unsigned long)func); new = ftrace_call_replace(pc, (unsigned long)func, true);
ret = ftrace_modify_code(pc, 0, new, false); ret = ftrace_modify_code(pc, 0, new, false);
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
if (!ret) { if (!ret) {
pc = (unsigned long)&ftrace_regs_call; pc = (unsigned long)&ftrace_regs_call;
new = ftrace_call_replace(pc, (unsigned long)func); new = ftrace_call_replace(pc, (unsigned long)func, true);
ret = ftrace_modify_code(pc, 0, new, false); ret = ftrace_modify_code(pc, 0, new, false);
} }
...@@ -124,10 +125,22 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) ...@@ -124,10 +125,22 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
{ {
unsigned long new, old; unsigned long new, old;
unsigned long ip = rec->ip; unsigned long ip = rec->ip;
unsigned long aaddr = adjust_address(rec, addr);
struct module *mod = NULL;
#ifdef CONFIG_ARM_MODULE_PLTS
mod = rec->arch.mod;
#endif
old = ftrace_nop_replace(rec); old = ftrace_nop_replace(rec);
new = ftrace_call_replace(ip, adjust_address(rec, addr)); new = ftrace_call_replace(ip, aaddr, !mod);
#ifdef CONFIG_ARM_MODULE_PLTS
if (!new && mod) {
aaddr = get_module_plt(mod, NULL, ip, aaddr);
new = ftrace_call_replace(ip, aaddr, true);
}
#endif
return ftrace_modify_code(rec->ip, old, new, true); return ftrace_modify_code(rec->ip, old, new, true);
} }
...@@ -140,9 +153,9 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, ...@@ -140,9 +153,9 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
unsigned long new, old; unsigned long new, old;
unsigned long ip = rec->ip; unsigned long ip = rec->ip;
old = ftrace_call_replace(ip, adjust_address(rec, old_addr)); old = ftrace_call_replace(ip, adjust_address(rec, old_addr), true);
new = ftrace_call_replace(ip, adjust_address(rec, addr)); new = ftrace_call_replace(ip, adjust_address(rec, addr), true);
return ftrace_modify_code(rec->ip, old, new, true); return ftrace_modify_code(rec->ip, old, new, true);
} }
...@@ -152,12 +165,29 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, ...@@ -152,12 +165,29 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
int ftrace_make_nop(struct module *mod, int ftrace_make_nop(struct module *mod,
struct dyn_ftrace *rec, unsigned long addr) struct dyn_ftrace *rec, unsigned long addr)
{ {
unsigned long aaddr = adjust_address(rec, addr);
unsigned long ip = rec->ip; unsigned long ip = rec->ip;
unsigned long old; unsigned long old;
unsigned long new; unsigned long new;
int ret; int ret;
old = ftrace_call_replace(ip, adjust_address(rec, addr)); #ifdef CONFIG_ARM_MODULE_PLTS
/* mod is only supplied during module loading */
if (!mod)
mod = rec->arch.mod;
else
rec->arch.mod = mod;
#endif
old = ftrace_call_replace(ip, aaddr,
!IS_ENABLED(CONFIG_ARM_MODULE_PLTS) || !mod);
#ifdef CONFIG_ARM_MODULE_PLTS
if (!old && mod) {
aaddr = get_module_plt(mod, NULL, ip, aaddr);
old = ftrace_call_replace(ip, aaddr, true);
}
#endif
new = ftrace_nop_replace(rec); new = ftrace_nop_replace(rec);
ret = ftrace_modify_code(ip, old, new, true); ret = ftrace_modify_code(ip, old, new, true);
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
*/ */
#include <linux/elf.h> #include <linux/elf.h>
#include <linux/ftrace.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/sort.h> #include <linux/sort.h>
...@@ -20,21 +21,55 @@ ...@@ -20,21 +21,55 @@
(PLT_ENT_STRIDE - 8)) (PLT_ENT_STRIDE - 8))
#endif #endif
static const u32 fixed_plts[] = {
#ifdef CONFIG_FUNCTION_TRACER
FTRACE_ADDR,
MCOUNT_ADDR,
#endif
};
static bool in_init(const struct module *mod, unsigned long loc) static bool in_init(const struct module *mod, unsigned long loc)
{ {
return loc - (u32)mod->init_layout.base < mod->init_layout.size; return loc - (u32)mod->init_layout.base < mod->init_layout.size;
} }
static void prealloc_fixed(struct mod_plt_sec *pltsec, struct plt_entries *plt)
{
int i;
if (!ARRAY_SIZE(fixed_plts) || pltsec->plt_count)
return;
pltsec->plt_count = ARRAY_SIZE(fixed_plts);
for (i = 0; i < ARRAY_SIZE(plt->ldr); ++i)
plt->ldr[i] = PLT_ENT_LDR;
BUILD_BUG_ON(sizeof(fixed_plts) > sizeof(plt->lit));
memcpy(plt->lit, fixed_plts, sizeof(fixed_plts));
}
u32 get_module_plt(struct module *mod, Elf32_Shdr *sechdrs, u32 get_module_plt(struct module *mod, Elf32_Shdr *sechdrs,
unsigned long loc, Elf32_Addr val) unsigned long loc, Elf32_Addr val)
{ {
struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core : struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
&mod->arch.init; &mod->arch.init;
Elf32_Shdr *plt_shdr = sechdrs ? &sechdrs[pltsec->plt_shndx] :
pltsec->plt;
struct plt_entries *plt;
int idx;
/* cache the address, ELF header is available only during module load */
if (!pltsec->plt_ent)
pltsec->plt_ent = (struct plt_entries *)plt_shdr->sh_addr;
plt = pltsec->plt_ent;
struct plt_entries *plt = prealloc_fixed(pltsec, plt);
(struct plt_entries *)sechdrs[pltsec->plt_shndx].sh_addr;
int idx = 0; for (idx = 0; idx < ARRAY_SIZE(fixed_plts); ++idx)
if (plt->lit[idx] == val)
return (u32)&plt->ldr[idx];
idx = 0;
/* /*
* Look for an existing entry pointing to 'val'. Given that the * Look for an existing entry pointing to 'val'. Given that the
* relocations are sorted, this will be the last entry we allocated. * relocations are sorted, this will be the last entry we allocated.
...@@ -183,11 +218,10 @@ static unsigned int count_plts(const Elf32_Sym *syms, Elf32_Addr base, ...@@ -183,11 +218,10 @@ static unsigned int count_plts(const Elf32_Sym *syms, Elf32_Addr base,
int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
char *secstrings, struct module *mod) char *secstrings, struct module *mod)
{ {
unsigned long core_plts = 0; unsigned long core_plts = ARRAY_SIZE(fixed_plts);
unsigned long init_plts = 0; unsigned long init_plts = ARRAY_SIZE(fixed_plts);
Elf32_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum; Elf32_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum;
Elf32_Sym *syms = NULL; Elf32_Sym *syms = NULL;
Elf32_Shdr *core_pltsec, *init_pltsec;
int i = 0; int i = 0;
/* /*
...@@ -195,11 +229,13 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, ...@@ -195,11 +229,13 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
* and for initialization code. * and for initialization code.
*/ */
for (s = sechdrs; s < sechdrs_end; ++s, ++i) { for (s = sechdrs; s < sechdrs_end; ++s, ++i) {
if (strcmp(".plt", secstrings + s->sh_name) == 0) if (strcmp(".plt", secstrings + s->sh_name) == 0) {
mod->arch.core.plt = s;
mod->arch.core.plt_shndx = i; mod->arch.core.plt_shndx = i;
else if (strcmp(".init.plt", secstrings + s->sh_name) == 0) } else if (strcmp(".init.plt", secstrings + s->sh_name) == 0) {
mod->arch.init.plt = s;
mod->arch.init.plt_shndx = i; mod->arch.init.plt_shndx = i;
else if (s->sh_type == SHT_SYMTAB) } else if (s->sh_type == SHT_SYMTAB)
syms = (Elf32_Sym *)s->sh_addr; syms = (Elf32_Sym *)s->sh_addr;
} }
...@@ -235,23 +271,23 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, ...@@ -235,23 +271,23 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
numrels, s->sh_info); numrels, s->sh_info);
} }
core_pltsec = sechdrs + mod->arch.core.plt_shndx; mod->arch.core.plt->sh_type = SHT_NOBITS;
core_pltsec->sh_type = SHT_NOBITS; mod->arch.core.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
core_pltsec->sh_flags = SHF_EXECINSTR | SHF_ALLOC; mod->arch.core.plt->sh_addralign = L1_CACHE_BYTES;
core_pltsec->sh_addralign = L1_CACHE_BYTES; mod->arch.core.plt->sh_size = round_up(core_plts * PLT_ENT_SIZE,
core_pltsec->sh_size = round_up(core_plts * PLT_ENT_SIZE, sizeof(struct plt_entries));
sizeof(struct plt_entries));
mod->arch.core.plt_count = 0; mod->arch.core.plt_count = 0;
mod->arch.core.plt_ent = NULL;
init_pltsec = sechdrs + mod->arch.init.plt_shndx; mod->arch.init.plt->sh_type = SHT_NOBITS;
init_pltsec->sh_type = SHT_NOBITS; mod->arch.init.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
init_pltsec->sh_flags = SHF_EXECINSTR | SHF_ALLOC; mod->arch.init.plt->sh_addralign = L1_CACHE_BYTES;
init_pltsec->sh_addralign = L1_CACHE_BYTES; mod->arch.init.plt->sh_size = round_up(init_plts * PLT_ENT_SIZE,
init_pltsec->sh_size = round_up(init_plts * PLT_ENT_SIZE, sizeof(struct plt_entries));
sizeof(struct plt_entries));
mod->arch.init.plt_count = 0; mod->arch.init.plt_count = 0;
mod->arch.init.plt_ent = NULL;
pr_debug("%s: plt=%x, init.plt=%x\n", __func__, pr_debug("%s: plt=%x, init.plt=%x\n", __func__,
core_pltsec->sh_size, init_pltsec->sh_size); mod->arch.core.plt->sh_size, mod->arch.init.plt->sh_size);
return 0; return 0;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册