Received: by 10.223.185.111 with SMTP id b44csp59564wrg; Fri, 9 Mar 2018 00:51:59 -0800 (PST) X-Google-Smtp-Source: AG47ELvQYnGDVPQ4HwkcDJDIt0HtHVpxLgrU7QsJ0sTI9YdgJwP3mSgv2qSpQ73jt+q5ytFzpzf+ X-Received: by 10.98.59.218 with SMTP id w87mr29512122pfj.37.1520585518893; Fri, 09 Mar 2018 00:51:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520585518; cv=none; d=google.com; s=arc-20160816; b=YNlDdXQOgsVoXmoLOvlmyPgNFZbnk6OGuGRcXMk9nMovmUm7IMcNXnOelcNKNWh/s5 gxe9/p1nm8nXrnX9T/cQZPtRMyo/CtPNjQtXtIJ8MBqxuiPyUcepo0kgOF7vLFYkqcRQ PYUYOy79EdleBDqxJTPX2Hw6zgO6mAnjLAv6EgfZvxHy0S8Ls+Y7awgRCk8RUZ+FYWUj SyD4jdj5cZV4Bq+M6A1OpHk/iVw6H1CfsLU2ehXr1uGbVbYw/XuKPneG2Lcy6c+X9Yta 1jeX34KPZQrSc1HEDqqImrSBa8ZXXzY7f2AGyE09Ws3oSEECg40O6nq/i3wKr2E/cG7C RTrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=yonKASJueOYcJLy8fq7IpgPPfePje6wS7Ix+mjTFD1k=; b=u+gm4hmhkuLfbtpuWjHfDHwPsb/BYLjZUDZxt5Dwp1GjUFCnQXfxubyNrXDKAS5gg4 p+ICj8ywucr7yvTxs+Yut9060Lv/a3xwwArpueED0fXtfxJW+/YQUk8feiPEA6Upcm8z HBRnH2IgblHlyulErbLmzxpjCVWz6Ftg9jGuBPh6OeOvOiD/O05OBsQtCUqZmiuMhJP5 aflU9S1L8WCd+G74P33P9jas1hSYiNKSyu0c6IKta6f1LYWGNG8sxKfdaV2nv7ISGxV3 Ifoem1cVYlmaemAW/Os6XRiwNJ+FpBTU/0GTawIEb07R6TCoRvAyZRw5okkokTnXaMGw hcIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GRSj7p8W; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 75si486272pfp.353.2018.03.09.00.51.43; Fri, 09 Mar 2018 00:51:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GRSj7p8W; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751572AbeCIIuk (ORCPT + 99 others); Fri, 9 Mar 2018 03:50:40 -0500 Received: from mail-it0-f66.google.com ([209.85.214.66]:38823 "EHLO mail-it0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750942AbeCIIui (ORCPT ); Fri, 9 Mar 2018 03:50:38 -0500 Received: by mail-it0-f66.google.com with SMTP id j7-v6so1991565ita.3 for ; Fri, 09 Mar 2018 00:50:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=yonKASJueOYcJLy8fq7IpgPPfePje6wS7Ix+mjTFD1k=; b=GRSj7p8WGJgVDV9AO3NUnlWm44PwrrQ073okaL/4hTX0OiLPUwLLW0610TbPwO+3ZD JMlyJTMKhnPktE5HWgfm4Kb40whjejU7nRjsK1czQVuoWxDJ1MF7ettaK3FGHMsweiKm yMaasENABBWLCK6uk46XoZR3OjaiSxcHFuqRc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=yonKASJueOYcJLy8fq7IpgPPfePje6wS7Ix+mjTFD1k=; b=huiuQvDetsJBbJn0TIYd/uiJ3DaEou0sC2mhaUZ1Sf0lDWk/aqRgiMNj+stX8aklh3 U42Y5jT6MOu7RFPTmKZgs8bAgakyvl3W6LjffMhk8yjKJu5m18iqCxb+aUeTtL1Ne5Z+ J5Vfp6kNnU29EUdOv2UVdYCHP68tTvaaG4MFSZpfLuW87fn3ASOoF4vjsUgZueLC4qf+ CTli4bjE3BwHhwYQP2kmKpOELvztsIieFwN7GA1vD+BC99LNXa8LLF0c2xoLjpLTun/V 4eGbU1KWNcINUBdEY9/7CUePaZailpykQK6X7Y3D1DXnjhELz9y++SBEIfnnbXVD1w0x h3Ng== X-Gm-Message-State: AElRT7GHZNxKzSH5wYEGecM+Xz9lnuicj3Nlg2TATydU8WerQMMWKtu8 wISb/wKiJMWpFqfuhQ8Nf8K49kw0adS9AGaRZkJiXg== X-Received: by 2002:a24:d916:: with SMTP id p22-v6mr2450390itg.106.1520585437939; Fri, 09 Mar 2018 00:50:37 -0800 (PST) MIME-Version: 1.0 Received: by 10.107.138.209 with HTTP; Fri, 9 Mar 2018 00:50:36 -0800 (PST) In-Reply-To: <20180309084151.2379-3-alexander.sverdlin@nokia.com> References: <20180309084151.2379-1-alexander.sverdlin@nokia.com> <20180309084151.2379-3-alexander.sverdlin@nokia.com> From: Ard Biesheuvel Date: Fri, 9 Mar 2018 08:50:36 +0000 Message-ID: Subject: Re: [PATCH v3 2/2] ARM: ftrace: Add MODULE_PLTS support To: Alexander Sverdlin Cc: linux-arm-kernel , Ingo Molnar , Linux Kernel Mailing List , Steven Rostedt , Russell King Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Alexander, On 9 March 2018 at 08:41, Alexander Sverdlin wrote: > 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 > [] (unwind_backtrace) from [] (show_stack+0x11/0x14) > [] (show_stack) from [] (dump_stack+0x81/0xa8) > [] (dump_stack) from [] (warn_slowpath_common+0x69/0x90) > [] (warn_slowpath_common) from [] (warn_slowpath_null+0x17/0x1c) > [] (warn_slowpath_null) from [] (__arm_gen_branch+0x83/0x8c) > [] (__arm_gen_branch) from [] (ftrace_make_nop+0xf/0x24) > [] (ftrace_make_nop) from [] (ftrace_process_locs+0x27b/0x3e8) > [] (ftrace_process_locs) from [] (load_module+0x11e9/0x1a44) > [] (load_module) from [] (SyS_finit_module+0x59/0x84) > [] (SyS_finit_module) from [] (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 > [] (unwind_backtrace) from [] (show_stack+0x11/0x14) > [] (show_stack) from [] (dump_stack+0x81/0xa8) > [] (dump_stack) from [] (warn_slowpath_common+0x69/0x90) > [] (warn_slowpath_common) from [] (warn_slowpath_null+0x17/0x1c) > [] (warn_slowpath_null) from [] (ftrace_bug+0x1b1/0x234) > [] (ftrace_bug) from [] (ftrace_process_locs+0x285/0x3e8) > [] (ftrace_process_locs) from [] (load_module+0x11e9/0x1a44) > [] (load_module) from [] (SyS_finit_module+0x59/0x84) > [] (SyS_finit_module) from [] (ret_fast_syscall+0x1/0x18) > ---[ end trace e1b64ced7a89adcd ]--- > ftrace failed to modify [] 0xe9ef7006 > actual: 02:f0:3b:fa > ftrace record flags: 0 > (0) expected tramp: c0314265 > > Signed-off-by: Alexander Sverdlin > --- > arch/arm/include/asm/ftrace.h | 3 +++ > arch/arm/include/asm/module.h | 1 + > arch/arm/kernel/ftrace.c | 62 ++++++++++++++++++++++++++++++++++++------- > arch/arm/kernel/module-plts.c | 53 +++++++++++++++++++++++++++--------- > 4 files changed, 97 insertions(+), 22 deletions(-) > > diff --git a/arch/arm/include/asm/ftrace.h b/arch/arm/include/asm/ftrace.h > index 9e842ff..faeb6b1 100644 > --- a/arch/arm/include/asm/ftrace.h > +++ b/arch/arm/include/asm/ftrace.h > @@ -19,6 +19,9 @@ struct dyn_arch_ftrace { > #ifdef CONFIG_OLD_MCOUNT > bool old_mcount; > #endif > +#ifdef CONFIG_ARM_MODULE_PLTS > + struct module *mod; > +#endif > }; > > static inline unsigned long ftrace_call_adjust(unsigned long addr) > diff --git a/arch/arm/include/asm/module.h b/arch/arm/include/asm/module.h > index 6996405..e3d7a51 100644 > --- a/arch/arm/include/asm/module.h > +++ b/arch/arm/include/asm/module.h > @@ -30,6 +30,7 @@ struct plt_entries { > > struct mod_plt_sec { > struct elf32_shdr *plt; > + struct plt_entries *plt_ent; > int plt_count; > }; > > diff --git a/arch/arm/kernel/ftrace.c b/arch/arm/kernel/ftrace.c > index 5617932..b55355f 100644 > --- a/arch/arm/kernel/ftrace.c > +++ b/arch/arm/kernel/ftrace.c > @@ -98,6 +98,19 @@ int ftrace_arch_code_modify_post_process(void) > > static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr) > { > + s32 offset = addr - pc; > + s32 blim = 0xfe000008; > + s32 flim = 0x02000004; > + > + if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) { > + blim = 0xff000004; > + flim = 0x01000002; > + } > + > + if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) && > + (offset < blim || offset > flim)) > + return 0; > + > return arm_gen_branch_link(pc, addr); > } > > @@ -166,10 +179,22 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) > { > unsigned long new, old; > unsigned long ip = rec->ip; > + unsigned long aaddr = adjust_address(rec, addr); > > old = ftrace_nop_replace(rec); > > - new = ftrace_call_replace(ip, adjust_address(rec, addr)); > + new = ftrace_call_replace(ip, aaddr); > + > +#ifdef CONFIG_ARM_MODULE_PLTS > + if (!new) { > + struct module *mod = rec->arch.mod; > + > + if (mod) { > + aaddr = get_module_plt(mod, ip, aaddr); > + new = ftrace_call_replace(ip, aaddr); > + } > + } > +#endif > > return ftrace_modify_code(rec->ip, old, new, true); > } > @@ -199,20 +224,39 @@ int ftrace_make_nop(struct module *mod, > unsigned long new; > int ret; > > - old = ftrace_call_replace(ip, adjust_address(rec, addr)); > - new = ftrace_nop_replace(rec); > - ret = ftrace_modify_code(ip, old, new, true); > +#ifdef CONFIG_ARM_MODULE_PLTS > + /* mod is only supplied during module loading */ > + if (!mod) > + mod = rec->arch.mod; > + else > + rec->arch.mod = mod; > +#endif > > -#ifdef CONFIG_OLD_MCOUNT > - if (ret == -EINVAL && addr == MCOUNT_ADDR) { > - rec->arch.old_mcount = true; > + for (;;) { > + unsigned long aaddr = adjust_address(rec, addr); > + > + old = ftrace_call_replace(ip, aaddr); > + > +#ifdef CONFIG_ARM_MODULE_PLTS > + if (!old && mod) { > + aaddr = get_module_plt(mod, ip, aaddr); > + old = ftrace_call_replace(ip, aaddr); > + } > +#endif > > - old = ftrace_call_replace(ip, adjust_address(rec, addr)); > new = ftrace_nop_replace(rec); > ret = ftrace_modify_code(ip, old, new, true); > - } > + > +#ifdef CONFIG_OLD_MCOUNT > + if (ret == -EINVAL && !rec->arch.old_mcount) { > + rec->arch.old_mcount = true; > + continue; > + } > #endif > > + break; > + } > + > return ret; > } > > diff --git a/arch/arm/kernel/module-plts.c b/arch/arm/kernel/module-plts.c > index f272711..a216256 100644 > --- a/arch/arm/kernel/module-plts.c > +++ b/arch/arm/kernel/module-plts.c > @@ -7,6 +7,7 @@ > */ > > #include > +#include > #include > #include > #include > @@ -22,6 +23,15 @@ > (PLT_ENT_STRIDE - 8)) > #endif > > +static u32 fixed_plts[] = { > + FTRACE_ADDR, > + MCOUNT_ADDR, > +#ifdef CONFIG_OLD_MCOUNT > + (unsigned long)ftrace_caller_old, > + (unsigned long)mcount, > +#endif > +}; > + Do we need this stuff in the PLT loader if ftrace is disabled? > static bool in_init(const struct module *mod, unsigned long loc) > { > return loc - (u32)mod->init_layout.base < mod->init_layout.size; > @@ -31,26 +41,43 @@ u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val) > { > struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core : > &mod->arch.init; > + int idx; > + struct plt_entries *plt; > + > + /* Pre-allocate entries in the first plt */ > + if (!pltsec->plt_count) { > + plt = (struct plt_entries *)pltsec->plt->sh_addr; > + for (idx = 0; idx < ARRAY_SIZE(plt->ldr); ++idx) > + plt->ldr[idx] = PLT_ENT_LDR; > + memcpy(plt->lit, fixed_plts, sizeof(fixed_plts)); > + pltsec->plt_count = ARRAY_SIZE(fixed_plts); > + /* > + * cache the address, > + * ELF header is available only during module load > + */ > + pltsec->plt_ent = plt; > + } > + plt = pltsec->plt_ent; > > - struct plt_entries *plt = (struct plt_entries *)pltsec->plt->sh_addr; > - int idx = 0; > + idx = ARRAY_SIZE(fixed_plts); > + while (idx) > + if (plt->lit[--idx] == val) > + return (u32)&plt->ldr[idx]; > Same here. > /* > * Look for an existing entry pointing to 'val'. Given that the > * relocations are sorted, this will be the last entry we allocated. > * (if one exists). > */ > - if (pltsec->plt_count > 0) { > - plt += (pltsec->plt_count - 1) / PLT_ENT_COUNT; > - idx = (pltsec->plt_count - 1) % PLT_ENT_COUNT; > + plt += (pltsec->plt_count - 1) / PLT_ENT_COUNT; > + idx = (pltsec->plt_count - 1) % PLT_ENT_COUNT; > > - if (plt->lit[idx] == val) > - return (u32)&plt->ldr[idx]; > + if (plt->lit[idx] == val) > + return (u32)&plt->ldr[idx]; > > - idx = (idx + 1) % PLT_ENT_COUNT; > - if (!idx) > - plt++; > - } > + idx = (idx + 1) % PLT_ENT_COUNT; > + if (!idx) > + plt++; > > pltsec->plt_count++; > BUG_ON(pltsec->plt_count * PLT_ENT_SIZE > pltsec->plt->sh_size); > @@ -182,8 +209,8 @@ static unsigned int count_plts(const Elf32_Sym *syms, Elf32_Addr base, > 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; > + unsigned long core_plts = ARRAY_SIZE(fixed_plts); > + unsigned long init_plts = ARRAY_SIZE(fixed_plts); > Elf32_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum; > Elf32_Sym *syms = NULL; > > -- > 2.4.6 > > > _______________________________________________ > linux-arm-kernel mailing list > linux-arm-kernel@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-arm-kernel