Received: by 10.213.65.68 with SMTP id h4csp444143imn; Tue, 13 Mar 2018 09:13:57 -0700 (PDT) X-Google-Smtp-Source: AG47ELscdIvXUTWhFF068KSiU9BVQfl3dud8TpuegYFUz1UMT9Rdke/Rbk9qqs6i83/PsRe5rvfU X-Received: by 10.99.114.86 with SMTP id c22mr929172pgn.162.1520957637668; Tue, 13 Mar 2018 09:13:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520957637; cv=none; d=google.com; s=arc-20160816; b=iV8vgvxCOQDzc9D1hbELDSHTPQqzsWe/p6pO2cUwuQE68qpVp0CNcR4iy8iEbtPFaI tn3GrEzOHUOMB1NhZfDWLy6TJHNnvl3NEdO2qfS7xrsXvhDQzWfd3xVR+9MWvrUBQvdu vZhoHF/WeeW2kRoIx0YicI+uB2Pv2IixffFAFEOq9GwCiVilLLl8fFVR3B/bxW0lPgsl zjMxtGpo1C1CxzEeTbmcZac2IfV+1lyDyiJIpywcCPK8K+bJba6I1H0KSeq+h39WL1bW ubcSKbt9PZAb0rjKt0+U2YRHlKCu9bqpyumE0PxZP69w9fq7TWG4tpGBeqxVcTXS9OW+ /REw== 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=1+iEN35XNKwH203i6O4vv3cAOjgZvm9TiuxfKFXaf84=; b=slGo5F76osVo0L9IB1AlwaPe8SzDapDtjkmL3msyqjzpSYgnuKybbC4jFDn17J8zj6 8nXlFJa/9+YiV5oK6qzI/6MuDQWbPEvN0J5oWozda0kA18rD79GFeegWt6B+ZuUZG70i lQ4DEPQ5LY35Y3lV6lGRbvhiifuzo4CKsXknSLT4pn9LWuQWc51EnqQwHORkQio/dp9V w6PnbybCRnWDRt4TL4DXYLCy3vqCOsEYnB8DFZ/qlAukHqZ4OyN8bFUKJqPlgjz4LRNj JekqhR3N3A3hOOLot0H7oXl1nz3asqtq+Z95quV4kWC4plYnCrhkJzdWyB4FTKYQw4Rn dJ6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XH1bQ0sg; 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 t11-v6si294933plo.225.2018.03.13.09.13.43; Tue, 13 Mar 2018 09:13:57 -0700 (PDT) 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=XH1bQ0sg; 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 S934047AbeCMQMk (ORCPT + 99 others); Tue, 13 Mar 2018 12:12:40 -0400 Received: from mail-it0-f66.google.com ([209.85.214.66]:52158 "EHLO mail-it0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932850AbeCMQMg (ORCPT ); Tue, 13 Mar 2018 12:12:36 -0400 Received: by mail-it0-f66.google.com with SMTP id u66-v6so802800ith.1 for ; Tue, 13 Mar 2018 09:12:36 -0700 (PDT) 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=1+iEN35XNKwH203i6O4vv3cAOjgZvm9TiuxfKFXaf84=; b=XH1bQ0sgR7XQnxdu9/5uIk+8ep+PMRgIVb7UI2Pii+S1sCT+V4h12MiEwsgxaptyM7 mdhu4rQL40TasbyUsFy1eprIxnfKXT9G0Uunvns9iWLGe8JLb/qzu4FBxXfy4CkfK/hZ ALqWhIhBfyr3TA4c5b9c2qEG03Apo1XXMAzfU= 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=1+iEN35XNKwH203i6O4vv3cAOjgZvm9TiuxfKFXaf84=; b=a81rlUhjo6DgG0kCgD+I+ZVL1wIr0Gy+vKkOYeGw90XY2UDjEtl32ENnVubOishz24 XsQypIRCrepNLNc+HqPAZR2ehddLf8LoMcZoGUTUCNoCoQ4L1Aytk0vjoZozJoUlniix aM3cLVA5UI2WgrlY4yo1MkS0bbK9nZo1PAoGqXSHh8yGuBpVWaWCiKbfryLYW3u5IcEL rFLKn0anO1yWkgF7o6O86UOnoYbyxj16eddqLk0er6OtEbHqnFt9hN9FQX9wGOe8m3yc ef8cn/9hWGxCdzNoS1ujiTXNjot0XB3PRA72jUA/U32+Na/dh+P5AktOfHyGlMLAhJIm H9zQ== X-Gm-Message-State: AElRT7ErMuPXgrkuSip2o6c/G/Za7/ms0+0UR0xLShr46LyvGZm/f0R3 cmkpVGpv9mgZGUiCOpJ9nzB5xNs3c+tcJnw5r99KgA== X-Received: by 10.36.145.199 with SMTP id i190mr1289122ite.50.1520957555990; Tue, 13 Mar 2018 09:12:35 -0700 (PDT) MIME-Version: 1.0 Received: by 10.107.138.209 with HTTP; Tue, 13 Mar 2018 09:12:34 -0700 (PDT) In-Reply-To: <20180313135314.18780-3-alexander.sverdlin@nokia.com> References: <20180313135314.18780-1-alexander.sverdlin@nokia.com> <20180313135314.18780-3-alexander.sverdlin@nokia.com> From: Ard Biesheuvel Date: Tue, 13 Mar 2018 16:12:34 +0000 Message-ID: Subject: Re: [PATCH v4 2/2] ARM: ftrace: Add MODULE_PLTS support To: Alexander Sverdlin Cc: linux-arm-kernel , Linux Kernel Mailing List , Russell King , Steven Rostedt , Ingo Molnar 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 On 13 March 2018 at 13:53, 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 | 47 +++++++++++++++++++++++++++++--- > 4 files changed, 100 insertions(+), 13 deletions(-) > ... > diff --git a/arch/arm/kernel/module-plts.c b/arch/arm/kernel/module-plts.c > index f272711..0951270 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,18 +23,54 @@ > (PLT_ENT_STRIDE - 8)) > #endif > > +static const u32 fixed_plts[] = { > +#ifdef CONFIG_FUNCTION_TRACER > + FTRACE_ADDR, > + MCOUNT_ADDR, > +#ifdef CONFIG_OLD_MCOUNT > + (unsigned long)ftrace_caller_old, > + (unsigned long)mcount, > +#endif > +#endif > +}; > + > static bool in_init(const struct module *mod, unsigned long loc) > { > 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)) > + return; > + > + for (i = 0; i < ARRAY_SIZE(plt->ldr); ++i) > + plt->ldr[i] = PLT_ENT_LDR; > + memcpy(plt->lit, fixed_plts, sizeof(fixed_plts)); This is slightly dodgy. You are assuming that sizeof(plt->lit) >= sizeof(fixed_plts), which may change depending on configuration or future changes. Could you add a BUILD_BUG_ON() here to ensure that this remains the case? > + pltsec->plt_count = ARRAY_SIZE(fixed_plts); > +} > + > 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; > + struct plt_entries *plt; > + int idx; > > - struct plt_entries *plt = (struct plt_entries *)pltsec->plt->sh_addr; > - int idx = 0; > + /* cache the address, ELF header is available only during module load */ > + if (!pltsec->plt_ent) > + pltsec->plt_ent = (struct plt_entries *)pltsec->plt->sh_addr; > + plt = pltsec->plt_ent; > + Where is plt_ent ever used? > + if (!pltsec->plt_count) > + prealloc_fixed(pltsec, plt); > + Please move the if () check into prealloc_fixed(), and only keep the loop below > + idx = ARRAY_SIZE(fixed_plts); > + while (idx) > + if (plt->lit[--idx] == val) > + return (u32)&plt->ldr[idx]; Please use a normal for loop here and iterate upward starting at 0 > > /* > * Look for an existing entry pointing to 'val'. Given that the > @@ -182,8 +219,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; > > @@ -238,6 +275,7 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, > mod->arch.core.plt->sh_size = round_up(core_plts * PLT_ENT_SIZE, > sizeof(struct plt_entries)); > mod->arch.core.plt_count = 0; > + mod->arch.core.plt_ent = NULL; > > mod->arch.init.plt->sh_type = SHT_NOBITS; > mod->arch.init.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC; > @@ -245,6 +283,7 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, > mod->arch.init.plt->sh_size = round_up(init_plts * PLT_ENT_SIZE, > sizeof(struct plt_entries)); > mod->arch.init.plt_count = 0; > + mod->arch.init.plt_ent = NULL; > > pr_debug("%s: plt=%x, init.plt=%x\n", __func__, > mod->arch.core.plt->sh_size, mod->arch.init.plt->sh_size); > -- > 2.4.6 >