From patchwork Fri Aug 18 11:26:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 110374 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp780609qge; Fri, 18 Aug 2017 04:27:11 -0700 (PDT) X-Received: by 10.84.224.135 with SMTP id s7mr9422474plj.238.1503055631424; Fri, 18 Aug 2017 04:27:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503055631; cv=none; d=google.com; s=arc-20160816; b=CfARluKuNcRMXRuLozccW+8q0Gung6zeic+H46XXPWYPQFdtUqIpOT8VCMpkJO0S4H ycQzfMfnO/mYYaTo6muzgXO4/UacYjzg6ssow/JZyTbYr/VaD4UCbEGP9/FVuxUqmYkr 44ZiD3NFSx/ibmIJxfjh2rESlWJV2bvu4wqdbfNR2wHhJJ2ZjWFVF/br7DnfE1asPJvu klm9YjDlk4TKfC908Z3A4D9mlO8vD0Wl9tPZ0izrbHYVoenK3B367TZvr1uvd33AoD5i +DCQW0L+gMBGW/01DLbaHtbrxSTkoBAuR355z3scn7m05rT4uzw/Quabispc0K2BZ/Wk 9RIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Zd+uUf6jlLiM5kkgnO0BGfxwJmYQmdGUiH2aEjP9gHE=; b=qbfBT00pR4ogVi4Qf+tK8jzlIEyh1CpUkvqQyCCv96QG58n2ADddzWTbsnMr8nmj29 ineSF5IWSmzIjV1KL0WPISuclMB5FIjUlKgXncKvocjxBxnr1rQAodhwdVXOb111Zbhs jEBQE9ZsoO3nMpAJTypi21FwyBpKU0u9yVQN3pX+4WdWrL7LZt2PKkgJQaUO/CdfAyem PilQErg+ro5ln3FczypRi87ykDgve9VbFIRfgG1XZ24MaVGfn6PPd6w4SnWOgACeVv0H mkT4sxY8FufLJCZFB2R2OjHl2pNFbhAEgWAcQPyvTe78/TOmZTEoo5ZF5uBTzRBEnqSw n3dA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GQEZBnSH; 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 o1si3767189pld.315.2017.08.18.04.27.11; Fri, 18 Aug 2017 04:27:11 -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=GQEZBnSH; 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 S1751944AbdHRL1J (ORCPT + 26 others); Fri, 18 Aug 2017 07:27:09 -0400 Received: from mail-wr0-f178.google.com ([209.85.128.178]:37134 "EHLO mail-wr0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751814AbdHRL1D (ORCPT ); Fri, 18 Aug 2017 07:27:03 -0400 Received: by mail-wr0-f178.google.com with SMTP id z91so59499088wrc.4 for ; Fri, 18 Aug 2017 04:27:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Zd+uUf6jlLiM5kkgnO0BGfxwJmYQmdGUiH2aEjP9gHE=; b=GQEZBnSHL4TDOD+TWyrNP1aJVU1W/jpRZbO/d/zdJu6Rm4gUR+Z6GZDRfHtGXu0jvs JwuXMsfLivSkPFclSBcHaPXrI2C0gkEjvsVdIVXnHbR0HwBny6F2Va7C+HXY/tLkzgTg sctb0yZR/LU5Drrinj00xSxuDEG5wZLDeCjKI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Zd+uUf6jlLiM5kkgnO0BGfxwJmYQmdGUiH2aEjP9gHE=; b=P4fE0sTNSbjGw+sK8GDsySGEemc79O/6uFHdPmHvWVdkPXHFvndU2PkuUXBdNzeFRK eV3pfHCc2Iz+eODsRfBBgPX+T0FATnBNmcV4jZa8b6OMXKQLYUAjw26H+W6bw9cSxMXm Jntk/YFtmuQ/0uCuAuyQV+mNVVmGO3XYitPNAspik+PC8y8KtwISxaKYtsyrPLrJHoL7 Y55WvkFHXHWuPozj99WBFwyJh8vfHXRX6e9z6RTdsA/RqPNS8IAw8G3Wvt3DFJCxUMlr Rw9puEDwCh7cE8owUG1dXTyF0xKAm6exZYH8K3aXfGbuHI+NuIbskxse99OWD4X0p2dt uWxw== X-Gm-Message-State: AHYfb5jGa69rr+sbSkECaAoz9gMJKj049Rsgs3FhIFuPgvlBqTyMRuUj 9VaFP0dDP8LN79G0NrRwQA== X-Received: by 10.28.127.138 with SMTP id a132mr1397664wmd.91.1503055622381; Fri, 18 Aug 2017 04:27:02 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id f4sm1047111wmh.28.2017.08.18.04.26.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 18 Aug 2017 04:27:01 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Joe Perches , Nicolas Pitre , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Andy Whitcroft , Jessica Yu Subject: [PATCH v2 4/6] init: allow initcall tables to be emitted using relative references Date: Fri, 18 Aug 2017 12:26:22 +0100 Message-Id: <20170818112624.24991-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170818112624.24991-1-ard.biesheuvel@linaro.org> References: <20170818112624.24991-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Allow the initcall tables to be emitted using relative references that are only half the size on 64-bit architectures and don't require fixups at runtime on relocatable kernels. Cc: Petr Mladek Cc: Sergey Senozhatsky Cc: Steven Rostedt Cc: James Morris Cc: "Serge E. Hallyn" Signed-off-by: Ard Biesheuvel --- include/linux/init.h | 64 ++++++++++++++++---- init/main.c | 22 ++----- kernel/printk/printk.c | 4 +- security/security.c | 6 +- 4 files changed, 62 insertions(+), 34 deletions(-) -- 2.11.0 diff --git a/include/linux/init.h b/include/linux/init.h index 94769d687cf0..61022aec47ea 100644 --- a/include/linux/init.h +++ b/include/linux/init.h @@ -110,9 +110,6 @@ typedef int (*initcall_t)(void); typedef void (*exitcall_t)(void); -extern initcall_t __con_initcall_start[], __con_initcall_end[]; -extern initcall_t __security_initcall_start[], __security_initcall_end[]; - /* Used for contructor calls. */ typedef void (*ctor_fn_t)(void); @@ -161,9 +158,57 @@ extern bool initcall_debug; * as KEEP() in the linker script. */ -#define __define_initcall(fn, id) \ +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +#define ___define_initcall(fn, id, __sec) \ + __ADDRESSABLE(fn) \ + asm(".section \"" #__sec ".init\", \"a\"\n" \ + "__initcall_" #fn #id ":\n" \ + ".long " VMLINUX_SYMBOL_STR(fn) " - .\n" \ + ".previous\n") + +struct initcall_entry_t { + int offset; +}; + +static inline +initcall_t initcall_from_entry(const struct initcall_entry_t *entry) +{ + return (initcall_t)((unsigned long)entry + entry->offset); +} +#else +#define ___define_initcall(fn, id, __sec) \ static initcall_t __initcall_##fn##id __used \ - __attribute__((__section__(".initcall" #id ".init"))) = fn; + __attribute__((__section__(#__sec ".init"))) = fn + +struct initcall_entry_t { + initcall_t call; +}; + +static inline +initcall_t initcall_from_entry(const struct initcall_entry_t *entry) +{ + return entry->call; +} +#endif + +extern struct initcall_entry_t __initcall_start[]; +extern struct initcall_entry_t __initcall0_start[]; +extern struct initcall_entry_t __initcall1_start[]; +extern struct initcall_entry_t __initcall2_start[]; +extern struct initcall_entry_t __initcall3_start[]; +extern struct initcall_entry_t __initcall4_start[]; +extern struct initcall_entry_t __initcall5_start[]; +extern struct initcall_entry_t __initcall6_start[]; +extern struct initcall_entry_t __initcall7_start[]; +extern struct initcall_entry_t __initcall_end[]; + +extern struct initcall_entry_t __con_initcall_start[]; +extern struct initcall_entry_t __con_initcall_end[]; + +extern struct initcall_entry_t __security_initcall_start[]; +extern struct initcall_entry_t __security_initcall_end[]; + +#define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id) /* * Early initcalls run before initializing SMP. @@ -202,13 +247,8 @@ extern bool initcall_debug; #define __exitcall(fn) \ static exitcall_t __exitcall_##fn __exit_call = fn -#define console_initcall(fn) \ - static initcall_t __initcall_##fn \ - __used __section(.con_initcall.init) = fn - -#define security_initcall(fn) \ - static initcall_t __initcall_##fn \ - __used __section(.security_initcall.init) = fn +#define console_initcall(fn) ___define_initcall(fn,, .con_initcall) +#define security_initcall(fn) ___define_initcall(fn,, .security_initcall) struct obs_kernel_param { const char *str; diff --git a/init/main.c b/init/main.c index 052481fbe363..174a68ee85c4 100644 --- a/init/main.c +++ b/init/main.c @@ -832,19 +832,7 @@ int __init_or_module do_one_initcall(initcall_t fn) return ret; } - -extern initcall_t __initcall_start[]; -extern initcall_t __initcall0_start[]; -extern initcall_t __initcall1_start[]; -extern initcall_t __initcall2_start[]; -extern initcall_t __initcall3_start[]; -extern initcall_t __initcall4_start[]; -extern initcall_t __initcall5_start[]; -extern initcall_t __initcall6_start[]; -extern initcall_t __initcall7_start[]; -extern initcall_t __initcall_end[]; - -static initcall_t *initcall_levels[] __initdata = { +static struct initcall_entry_t *initcall_levels[] __initdata = { __initcall0_start, __initcall1_start, __initcall2_start, @@ -870,7 +858,7 @@ static char *initcall_level_names[] __initdata = { static void __init do_initcall_level(int level) { - initcall_t *fn; + struct initcall_entry_t *fn; strcpy(initcall_command_line, saved_command_line); parse_args(initcall_level_names[level], @@ -880,7 +868,7 @@ static void __init do_initcall_level(int level) NULL, &repair_env_string); for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++) - do_one_initcall(*fn); + do_one_initcall(initcall_from_entry(fn)); } static void __init do_initcalls(void) @@ -911,10 +899,10 @@ static void __init do_basic_setup(void) static void __init do_pre_smp_initcalls(void) { - initcall_t *fn; + struct initcall_entry_t *fn; for (fn = __initcall_start; fn < __initcall0_start; fn++) - do_one_initcall(*fn); + do_one_initcall(initcall_from_entry(fn)); } /* diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index fc47863f629c..95a964592dc6 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2623,7 +2623,7 @@ EXPORT_SYMBOL(unregister_console); */ void __init console_init(void) { - initcall_t *call; + struct initcall_entry_t *call; /* Setup the default TTY line discipline. */ n_tty_init(); @@ -2634,7 +2634,7 @@ void __init console_init(void) */ call = __con_initcall_start; while (call < __con_initcall_end) { - (*call)(); + initcall_from_entry(call)(); call++; } } diff --git a/security/security.c b/security/security.c index 30132378d103..e878b6f5ce9c 100644 --- a/security/security.c +++ b/security/security.c @@ -44,10 +44,10 @@ static __initdata char chosen_lsm[SECURITY_NAME_MAX + 1] = static void __init do_security_initcalls(void) { - initcall_t *call; - call = __security_initcall_start; + struct initcall_entry_t *call = __security_initcall_start; + while (call < __security_initcall_end) { - (*call) (); + initcall_from_entry(call)(); call++; } }