Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp539517pxb; Wed, 27 Jan 2021 14:20:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJwS7Z7hLrSWtj0N3R8q4lRz7kzfV7wxBypjHMRYtHkBs7tP0p8FixsZFHEBOY5bolGYcsf7 X-Received: by 2002:a17:906:b51:: with SMTP id v17mr8643913ejg.8.1611786056214; Wed, 27 Jan 2021 14:20:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1611786056; cv=pass; d=google.com; s=arc-20160816; b=iaLdD5CendGNOgK8CsWdqpYk01TuqylZCBxaeEy3Kb8FhE7o5kE4VoVyADXOU/m0B1 hPJ8hn5M1xndf3PK2VetbuhwPMsvbMAUPQ9iRAyANz6jXqVzX9yc9ol2ji4UbxUF0YeV NSmzThxQwdPp0eLY1jP4FDQHt4EfhtjTEkC31DLYqjtmGxB82B+00cteWG1+h+rBDtDA 3jfTZHFwLp5DLStNg5tqAH0/utU9+ZG+cyCfzmTyStrGMFNlN+PCpm0Z/AO5SASydoI2 ooImBPkz3Cj2zwetk1h7XamCDpN0TXmDcehcDzI/60vzNS9pD9Kc/7y/30mWO2WnUwcM UDCg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ebxnuPdTOjVrsQotx7fBU2F6EW8eGbdv3/9kKAXHV6Q=; b=WSkT6YS2G7pB5DBhlnQzRNXFauacpPf912FjPkkNjPsZVp68ZrdPtE4fleDCKi2DvO ThTW7jeFSA9lci+s46oUhK4B+fKYEavqSiy40YiXCzrUEcKjVaXS4phjLdKxwij+a8DX ybcIG9BcMwT0gOaWJc2dr5eiZiIzPkJtFTnrFCOgXZ7SizlKEB1Cn2ObbjX9nKDtH1Qu A4LgqQO2UAVoxHH5mdLgN4NFfpl49YwD5i226FPbHFKnqJLbTvp80Ml6cZs3lE2oDa85 oaiv+bpytYTjBc2TajpS5vbE5lrG18kBRNJlSDJVElAi9OQPN2kdbMeX6X8qVXKA5sk7 BKlg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@nokia.onmicrosoft.com header.s=selector1-nokia-onmicrosoft-com header.b=aiYeh31Q; arc=pass (i=1 spf=pass spfdomain=nokia.com dmarc=pass fromdomain=nokia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nokia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d3si1657325edy.581.2021.01.27.14.20.30; Wed, 27 Jan 2021 14:20:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@nokia.onmicrosoft.com header.s=selector1-nokia-onmicrosoft-com header.b=aiYeh31Q; arc=pass (i=1 spf=pass spfdomain=nokia.com dmarc=pass fromdomain=nokia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nokia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236510AbhA0LN2 (ORCPT + 99 others); Wed, 27 Jan 2021 06:13:28 -0500 Received: from mail-db8eur05on2133.outbound.protection.outlook.com ([40.107.20.133]:19072 "EHLO EUR05-DB8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236543AbhA0LKn (ORCPT ); Wed, 27 Jan 2021 06:10:43 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IGTZTYH2uW9hhqLk8V4n/mudxPW5HQDC3aRMHlycjxGso//XYBuucVuMS614Q3NU0fgvuosQmBywGy4UXOxGc5/HZf2M1lNQvomDHaO3Rzz37UBLS2jz9KrSFH24J/uTnFPBhRr4XYAc6Y4fgqvcqCU4zw3Qz2MiRYf3sPQKC841aCUFTHMHAxmFxdCwlYb2L3sC4zvtfJbTDAEP1uiSjVqALVtx6EgMQnyNQQkpjtVmqdtLnreLYDjbxVHrNHCWoqRQisuDYUWm+oKPak53jhBj8klKec9ecombA0PTGx6Zp5fzZAxYG70kAY7kqqmib49/o+2ie7VOX/A4PPkP8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ebxnuPdTOjVrsQotx7fBU2F6EW8eGbdv3/9kKAXHV6Q=; b=njEVupYZgOwmNQk0Vo8/HJMTORHXrkjfZ/J1rBHdiTfVZWtn8UlVY+X915zHqEfFpezdAMajuFRXGqpjQtRJD+BKMfyFPNAHTkTz1R+jq9QdwL519DObug7C+J0qZSm8zNcqBKGATbfjXDod5w4LxgRSoJgOM7hhBKfs9a1rDJMg2F/jJpTezT0AJ+1i3o6QW3TW/7SXOgzbPgACGKzckMPDvhwYWwBtmJHW6zqDlpdxistBWjpwGQHwHo3U8Bfa37rEdDF3AaI8sJuQxvxWk02TWIDeoMdi9hNTNhgTDKdemz6LATPbqohgEeNr3D+CyTnsjt3t1KfHQ+4FqB0ixw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 131.228.2.8) smtp.rcpttodomain=linaro.org smtp.mailfrom=nokia.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=nokia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nokia.onmicrosoft.com; s=selector1-nokia-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ebxnuPdTOjVrsQotx7fBU2F6EW8eGbdv3/9kKAXHV6Q=; b=aiYeh31QwcRqiY9loA4wb448vcHFXQ+KavjcvwvBC2Gr/t61FYe1pYRhaOsFxg/d7NPQeegAuZgHi05TzmNTv1DHhYcgqR6Wx6YbK48g6UlBMiZGL1+ewAq9ZGoeIePRzOHf4lE5780GUflVlB1jq7alBEa3ck4GaOLWtfYPG9A= Received: from DB6P193CA0020.EURP193.PROD.OUTLOOK.COM (2603:10a6:6:29::30) by VI1PR07MB3325.eurprd07.prod.outlook.com (2603:10a6:802:22::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3805.5; Wed, 27 Jan 2021 11:09:53 +0000 Received: from DB5EUR03FT049.eop-EUR03.prod.protection.outlook.com (2603:10a6:6:29:cafe::d3) by DB6P193CA0020.outlook.office365.com (2603:10a6:6:29::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3805.17 via Frontend Transport; Wed, 27 Jan 2021 11:09:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 131.228.2.8) smtp.mailfrom=nokia.com; linaro.org; dkim=none (message not signed) header.d=none;linaro.org; dmarc=pass action=none header.from=nokia.com; Received-SPF: Pass (protection.outlook.com: domain of nokia.com designates 131.228.2.8 as permitted sender) receiver=protection.outlook.com; client-ip=131.228.2.8; helo=fihe3nok0734.emea.nsn-net.net; Received: from fihe3nok0734.emea.nsn-net.net (131.228.2.8) by DB5EUR03FT049.mail.protection.outlook.com (10.152.20.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.11 via Frontend Transport; Wed, 27 Jan 2021 11:09:53 +0000 Received: from ulegcparamis.emea.nsn-net.net (ulegcparamis.emea.nsn-net.net [10.151.74.146]) by fihe3nok0734.emea.nsn-net.net (GMO) with ESMTP id 10RB9nLr008895; Wed, 27 Jan 2021 11:09:51 GMT From: Alexander A Sverdlin To: Steven Rostedt , Ingo Molnar , Russell King , linux-arm-kernel@lists.infradead.org, Florian Fainelli Cc: Alexander Sverdlin , linux-kernel@vger.kernel.org, Ard Biesheuvel Subject: [PATCH v7 2/2] ARM: ftrace: Add MODULE_PLTS support Date: Wed, 27 Jan 2021 12:09:44 +0100 Message-Id: <20210127110944.41813-3-alexander.sverdlin@nokia.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20210127110944.41813-1-alexander.sverdlin@nokia.com> References: <20210127110944.41813-1-alexander.sverdlin@nokia.com> X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-MS-PublicTrafficType: Email MIME-Version: 1.0 Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: cd285e66-0b73-4b6d-7968-08d8c2b412a2 X-MS-TrafficTypeDiagnostic: VI1PR07MB3325: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:751; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bcgcxQ9DFPmd6o855PUVy1107CURzJmP4+nMxqCtFP6g1BC5mwQSVXCgc8N8+qQ4jbbRq28oncvQtqew8YK6EyJ8N56nE2aRNJaGU3EfWmx0MGn8ufC2E3Xjd8FSgnF4JadRhk80NPt1Ou34E1qHajI+JHnu9/5tdeWNJ1SlIgww19oJxCmQNMzMEj9L0HcwXaxCTbPbx6VlRrE9MP0ZCcRJWaid+PjZE8DChXHpCrSXURuCCivkRJ4+tiYF9ZfOdpFZNg4uXI6XhKRWx/zHaEbHEuiGphGnPHoNQdN4yMH2sj3Ui+icPr5V6PXZrHEzgxrJaMT8dY0wDTeqm58vCBCD1E9D+w3GKzckZEcrQL4RhIeFV1rPVnWaG2ClXwl4as9ysrL3XdxPEFBGO1rgNoJpyb3cPRm/J+AyQ2ueJNcDXkL5EXX+syN0SGfQTuH+8xOg0OXBZ1SRWmWqm5X5iluljAFXfJsJWzmuqYA/nUEzA87TX0SNWDSa3pK7Rwep X-Forefront-Antispam-Report: CIP:131.228.2.8;CTRY:FI;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:fihe3nok0734.emea.nsn-net.net;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(396003)(39860400002)(376002)(136003)(346002)(46966006)(316002)(54906003)(1076003)(2906002)(82310400003)(82740400003)(2616005)(356005)(4326008)(478600001)(70206006)(5660300002)(6666004)(186003)(336012)(26005)(110136005)(86362001)(36756003)(70586007)(81166007)(8936002)(8676002)(83380400001)(47076005)(36610700001);DIR:OUT;SFP:1102; X-OriginatorOrg: nokia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jan 2021 11:09:53.4445 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cd285e66-0b73-4b6d-7968-08d8c2b412a2 X-MS-Exchange-CrossTenant-Id: 5d471751-9675-428d-917b-70f44f9630b0 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5d471751-9675-428d-917b-70f44f9630b0;Ip=[131.228.2.8];Helo=[fihe3nok0734.emea.nsn-net.net] X-MS-Exchange-CrossTenant-AuthSource: DB5EUR03FT049.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR07MB3325 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexander Sverdlin 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 | 46 +++++++++++++++++++++++++++++++++++++++++-- arch/arm/kernel/module-plts.c | 44 +++++++++++++++++++++++++++++++++++++---- 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/arch/arm/include/asm/ftrace.h b/arch/arm/include/asm/ftrace.h index 48ec1d0..a4dbac0 100644 --- a/arch/arm/include/asm/ftrace.h +++ b/arch/arm/include/asm/ftrace.h @@ -15,6 +15,9 @@ extern void __gnu_mcount_nc(void); #ifdef CONFIG_DYNAMIC_FTRACE struct dyn_arch_ftrace { +#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 09b9ad5..cfffae6 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 9a79ef6..fa867a5 100644 --- a/arch/arm/kernel/ftrace.c +++ b/arch/arm/kernel/ftrace.c @@ -70,6 +70,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); } @@ -124,10 +137,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); } @@ -152,12 +177,29 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr) { + unsigned long aaddr = adjust_address(rec, addr); unsigned long ip = rec->ip; unsigned long old; unsigned long new; 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); + +#ifdef CONFIG_ARM_MODULE_PLTS + if (!old && mod) { + aaddr = get_module_plt(mod, ip, aaddr); + old = ftrace_call_replace(ip, aaddr); + } +#endif + new = ftrace_nop_replace(rec); ret = ftrace_modify_code(ip, old, new, true); diff --git a/arch/arm/kernel/module-plts.c b/arch/arm/kernel/module-plts.c index d330e9e..a0524ad 100644 --- a/arch/arm/kernel/module-plts.c +++ b/arch/arm/kernel/module-plts.c @@ -4,6 +4,7 @@ */ #include +#include #include #include #include @@ -20,19 +21,52 @@ (PLT_ENT_STRIDE - 8)) #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) { 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, 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; + + /* 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; - struct plt_entries *plt = (struct plt_entries *)pltsec->plt->sh_addr; - int idx = 0; + prealloc_fixed(pltsec, plt); + + 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 * relocations are sorted, this will be the last entry we allocated. @@ -180,8 +214,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; @@ -236,6 +270,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; @@ -243,6 +278,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.10.2