From patchwork Sat Aug 19 18:10:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 110456 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp2383628qge; Sat, 19 Aug 2017 11:11:08 -0700 (PDT) X-Received: by 10.84.132.78 with SMTP id 72mr13373121ple.66.1503166268112; Sat, 19 Aug 2017 11:11:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503166268; cv=none; d=google.com; s=arc-20160816; b=O5it4+aiWbgptI/mu5CFE9rkKf5Fov9Zw5NtwwWPQOqfOnnEl6KxtWzEN4xaIy2fub a0wOeePvjtfL61/pmhQh2B5fp4BGgKP8imMPuTqvq5XHFb+bbNGSlHbIeeGget852fYJ uKy6udxahYgwA/GP4M39GckXp3gfZjAs2+7Vbrf/aJ9C0GPwwCASYcdpA2K2s4xJI/Ct W7cDX+m2Vx75e2hC0ZHksRQwPx18MtScPGj7VoieOXF+ecOQ6H6jJCYn1cfFR6hQyMfh zj4qIeyHSR0LYHVXumn0ZBn9JZ0YMNcsdsGBNxke8Mp/srOj40wlNwN2byYEUnbDtHqT lBdA== 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=WpAB7m7CyEA9tmJ5QoBPu6wS8zGT6hzTjbT0E6oPCtY=; b=zstPFYViazbVPpAGftTp0jqf4yw+2+sSrmiFUcXZmonePdwyB9QlwpOZ5cKGg1ozaZ e5x+7UgoxrA6KgUPyH6jmjgW2++52Jc7Kbeb4iLpsNCa2YVtzXI4MTSlNAYx0tB3MrR4 +23TIH6gg4Ld56z1+pFzv7Of+yz8ndKR9Gxt44Z0zwhwGqBxGca/YweM1zy2CKmg5lkc Phs+/mLL37WkcKgdsjlBTJ0PTgddg4WIWEuTg5S++yLDpIiOJePmhodU/R0+8Pk8UmkC LNXNF9moau6b2AWHRAkbS/yU0J6cy/UUCsfQS4kQ9HwycXSw3dirkIy53RQxhHReQVSa 0UGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KHnpfLpe; 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 c32si5548733plj.478.2017.08.19.11.11.07; Sat, 19 Aug 2017 11:11:08 -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=KHnpfLpe; 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 S1752195AbdHSSLG (ORCPT + 26 others); Sat, 19 Aug 2017 14:11:06 -0400 Received: from mail-wm0-f45.google.com ([74.125.82.45]:37031 "EHLO mail-wm0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751892AbdHSSLA (ORCPT ); Sat, 19 Aug 2017 14:11:00 -0400 Received: by mail-wm0-f45.google.com with SMTP id i66so20959564wmg.0 for ; Sat, 19 Aug 2017 11:10:59 -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=WpAB7m7CyEA9tmJ5QoBPu6wS8zGT6hzTjbT0E6oPCtY=; b=KHnpfLpe+li4wU6mAvuc/r7y1YnZBd+96XAd0CmIHenutW3hWYusSo52UYQ2kUfDTl cqgJNMn/vFGCTT+hgrwaCNWcv8m0ZJJG0QMNYLt6jM/fkMKVjsKuHYGTu09+zn+A/KGX nQbIj/hGcrTBSITSaJ89gdgOAx+U/fD80Gg60= 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=WpAB7m7CyEA9tmJ5QoBPu6wS8zGT6hzTjbT0E6oPCtY=; b=oU59gWJBtc/CKRre8APTBAXpZImpZ1Z4P5hLSdkNRUuy9fWYGsrk8LXPLMcJcZPIhy LgNDFoWeQ617QOa6YajQ1hdGLm8hbb2Ye22wR60yEeOidpU5ACdf6yZ7AbkyVmaFOWNw S77CwumMBB1cH04E/25ffpUK7vULO6wdTKZQAiRvRxmLM0swqbvpg3IcqP33Mcn7aeL0 BMelU9H5UNyYpaNcIld3n07l3FYU40OdWygFpp+OjKe15e4wAaxYlRShAH4RaXyBcl/q FkisLGdCyxY7eDwIx+ukVctxXRhhUrnqQpJldCsp4yekZ63Ab4VqsMgTUC3gT6aR0wIx 07HQ== X-Gm-Message-State: AHYfb5hbz9xcao8RvFI6cqgQvzhgMpdZfLHQRyJtNfeSomygQUkmh2fx c6uahkIYBU8Yy+f7xM4ayA== X-Received: by 10.28.18.66 with SMTP id 63mr4064584wms.20.1503166258405; Sat, 19 Aug 2017 11:10:58 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id m127sm4056261wmm.46.2017.08.19.11.10.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Aug 2017 11:10:57 -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 , Nicolas Pitre , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , x86@kernel.org Subject: [PATCH v3 1/5] arch: enable relative relocations for arm64, power, x86, s390 and x86 Date: Sat, 19 Aug 2017 19:10:31 +0100 Message-Id: <20170819181035.969-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170819181035.969-1-ard.biesheuvel@linaro.org> References: <20170819181035.969-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Before updating certain subsystems to use place relative 32-bit relocations in special sections, to save space and reduce the number of absolute relocations that need to be processed at runtime by relocatable kernels, introduce the Kconfig symbol and define it for some architectures that should be able to support and benefit from it. Cc: Catalin Marinas Cc: Will Deacon Cc: Benjamin Herrenschmidt Cc: Paul Mackerras Cc: Michael Ellerman Cc: Martin Schwidefsky Cc: Heiko Carstens Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: x86@kernel.org Signed-off-by: Ard Biesheuvel --- arch/Kconfig | 10 ++++++++++ arch/arm64/Kconfig | 1 + arch/arm64/kernel/vmlinux.lds.S | 2 +- arch/powerpc/Kconfig | 1 + arch/s390/Kconfig | 1 + arch/x86/Kconfig | 1 + 6 files changed, 15 insertions(+), 1 deletion(-) -- 2.11.0 diff --git a/arch/Kconfig b/arch/Kconfig index 21d0089117fe..4e77d8e8697e 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -940,4 +940,14 @@ config REFCOUNT_FULL against various use-after-free conditions that can be used in security flaw exploits. +config HAVE_ARCH_PREL32_RELOCATIONS + bool + help + May be selected by an architecture if it supports place-relative + 32-bit relocations, both in the toolchain and in the module loader, + in which case relative references can be used in special sections + for PCI fixup, initcalls etc which are only half the size on 64 bit + architectures, and don't require runtime relocation on relocatable + kernels. + source "kernel/gcov/Kconfig" diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index dfd908630631..ce05d2d8143e 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -72,6 +72,7 @@ config ARM64 select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT + select HAVE_ARCH_PREL32_RELOCATIONS select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_TRACEHOOK select HAVE_ARCH_TRANSPARENT_HUGEPAGE diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index 987a00ee446c..b6d8e6504896 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -171,7 +171,7 @@ SECTIONS CON_INITCALL SECURITY_INITCALL INIT_RAM_FS - *(.init.rodata.* .init.bss) /* from the EFI stub */ + *(.init.rodata.* .init.bss .init.discard.*) /* EFI stub */ } .exit.data : { ARM_EXIT_KEEP(EXIT_DATA) diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index 36f858c37ca7..f7e60ed5bdd0 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -163,6 +163,7 @@ config PPC select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT + select HAVE_ARCH_PREL32_RELOCATIONS select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_TRACEHOOK select ARCH_HAS_STRICT_KERNEL_RWX if (PPC_BOOK3S_64 && !RELOCATABLE && !HIBERNATION) diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig index 7eeb75d758c1..1206cc7daa0d 100644 --- a/arch/s390/Kconfig +++ b/arch/s390/Kconfig @@ -127,6 +127,7 @@ config S390 select HAVE_ARCH_AUDITSYSCALL select HAVE_ARCH_JUMP_LABEL select CPU_NO_EFFICIENT_FFS if !HAVE_MARCH_Z9_109_FEATURES + select HAVE_ARCH_PREL32_RELOCATIONS select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_SOFT_DIRTY select HAVE_ARCH_TRACEHOOK diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 781521b7cf9e..77246125883e 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -112,6 +112,7 @@ config X86 select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT select HAVE_ARCH_COMPAT_MMAP_BASES if MMU && COMPAT + select HAVE_ARCH_PREL32_RELOCATIONS select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_TRACEHOOK select HAVE_ARCH_TRANSPARENT_HUGEPAGE From patchwork Sat Aug 19 18:10:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 110458 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp2383782qge; Sat, 19 Aug 2017 11:11:19 -0700 (PDT) X-Received: by 10.84.168.132 with SMTP id f4mr13614662plb.170.1503166279258; Sat, 19 Aug 2017 11:11:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503166279; cv=none; d=google.com; s=arc-20160816; b=1D5HWOBxOLBuiFKBVupluUmPPLnWdQ4IJG9tpb8spZeyjdjUkNJ6jy3I7HMnrcu6fS 39ZRAhX0bVkb60wNF9VbG55dennqMTGykVCk+9mzp1wV8V6v3Hj/KiLGrhusn5cvmxTp sUlex+CcPTsL2YrftMAf17NRZAyfBQ1NYTgkHzfXO03KE4Jdh1JQ+2fcigs1ZIxqcgym nQkUQlJbfxr3ifOODchZiM0GP0uujH839nc4+yko2qvG0NMfeSkjxUfR2Ee2wCMrtr/u UpLJeigKmdFLWWl6lYBIAjcAPoQL4b0JYrb84N8er1zr/t+6IUIfTBfM0iLV4BwEmYY4 wJnA== 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=2eOEgVmpd0A5XeBpXWHpYxXFEptGR7gNJCJr3qKc3y8=; b=bl49tBk+PN609UeljcOOZCg+ePQ/8qCrieVuUe5zJY67Htwd/aBlckibRMHBwoRZ/2 TcEGq2h01APYVAoTKi58zjgKVltf9l/BrNsJlsFC3r/VBgosVotuweVU2Af6dk+pe/vI ssj5cC2VuQ3DvIAysYOy15HI6dEJjW+IayasGe3kE8PQgmQEp9J9OOwhytakC30H5+Ah wt3ol5y5Jat4jOrDOapD9OA0nA+DchiDQnkDrbH0vjsZu1Fq9uBp1DU2ze5MMwsWqWPq iHDjfVNW8LRARugTktIbxc6KO4qQH8lTCKpHYpgIR+1krxK+UJtrDV4Cb9mL/Fs/GAt7 BwjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iAqvUKtf; 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 d10si3063254pgn.553.2017.08.19.11.11.18; Sat, 19 Aug 2017 11:11:19 -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=iAqvUKtf; 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 S1752336AbdHSSLQ (ORCPT + 26 others); Sat, 19 Aug 2017 14:11:16 -0400 Received: from mail-wm0-f42.google.com ([74.125.82.42]:37959 "EHLO mail-wm0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751915AbdHSSLF (ORCPT ); Sat, 19 Aug 2017 14:11:05 -0400 Received: by mail-wm0-f42.google.com with SMTP id l19so20849191wmi.1 for ; Sat, 19 Aug 2017 11:11:04 -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=2eOEgVmpd0A5XeBpXWHpYxXFEptGR7gNJCJr3qKc3y8=; b=iAqvUKtfYm4qKYRSmJHJOAbdSXE6LzX1cuH4FSiwz7ryNLTFPwztxc4AWAWhfJWhNa Kb83ew6I/ssY2mgM5aMZjzZDEf4XnDWmydh/XNv37MQbmNc2UccsUldjGDJY3IjPbdru qB70pZAZ50LjgvdhlWXvdsbKiiocoAwbSXUeE= 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=2eOEgVmpd0A5XeBpXWHpYxXFEptGR7gNJCJr3qKc3y8=; b=kEWo0Yn320uIof0mCU8WZ8urF2i82uzsy6u37wmsM5c6vZwk7JMVOzDB60Vwza/sZM aNdOlLg09p1ZeEtj63vz42+imyIjHtEqiu45BQ0JmxbQibruplZtsildJAqn5ymEW5R2 4AjEu3hMLk1Hp4P3CB0nA0k5kWp0Ed53x0sg1It31Xn8yQaCtZF7KwbYXD6AXP6rjHyP VOw/5AdChfX/AlhzkcdEe+9Vf4hbPg8k8+R6xpv0+weLBqGbhBKn+HFiFgABRF76JeXh eT4YAXCPrbI3/UIoH4ZWWDTthTfEU7bbF9gbkRhzT1Eq2Q1tUxdnpSOZMEvd5zTEG8sv teGg== X-Gm-Message-State: AHYfb5hzni5KZY3J3Y9TrYWO7J5SQlkVWUHdUG49UIo3DCV6PAFYOl6w nLW2ansEEHMESQMQlCfyZQ== X-Received: by 10.28.129.205 with SMTP id c196mr3955440wmd.120.1503166264033; Sat, 19 Aug 2017 11:11:04 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id m127sm4056261wmm.46.2017.08.19.11.10.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Aug 2017 11:11:03 -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 , Nicolas Pitre , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , Ingo Molnar Subject: [PATCH v3 2/5] module: use relative references for __ksymtab entries Date: Sat, 19 Aug 2017 19:10:32 +0100 Message-Id: <20170819181035.969-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170819181035.969-1-ard.biesheuvel@linaro.org> References: <20170819181035.969-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org An ordinary arm64 defconfig build has ~64 KB worth of __ksymtab entries, each consisting of two 64-bit fields containing absolute references, to the symbol itself and to a char array containing its name, respectively. When we build the same configuration with KASLR enabled, we end up with an additional ~192 KB of relocations in the .init section, i.e., one 24 byte entry for each absolute reference, which all need to be processed at boot time. Given how the struct kernel_symbol that describes each entry is completely local to module.c (except for the references emitted by EXPORT_SYMBOL() itself), we can easily modify it to contain two 32-bit relative references instead. This reduces the size of the __ksymtab section by 50% for all 64-bit architectures, and gets rid of the runtime relocations entirely for architectures implementing KASLR, either via standard PIE linking (arm64) or using custom host tools (x86). Note that the binary search involving __ksymtab contents relies on each section being sorted by symbol name. This is implemented based on the input section names, not the names in the ksymtab entries, so this patch does not interfere with that. Given that the use of place-relative relocations requires support both in the toolchain and in the module loader, we cannot enable this feature for all architectures. So make it dependend on whether CONFIG_HAVE_ARCH_PREL32_RELOCATIONS is defined. Cc: Jessica Yu Cc: Arnd Bergmann Cc: Andrew Morton Cc: Ingo Molnar Cc: Kees Cook Cc: Thomas Garnier Cc: Nicolas Pitre Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/Kbuild | 1 + arch/x86/include/asm/export.h | 4 -- include/asm-generic/export.h | 12 +++++- include/linux/compiler.h | 11 +++++ include/linux/export.h | 45 +++++++++++++++----- kernel/module.c | 33 +++++++++++--- 6 files changed, 83 insertions(+), 23 deletions(-) -- 2.11.0 Acked-by: Jessica Yu (for module changes) diff --git a/arch/x86/include/asm/Kbuild b/arch/x86/include/asm/Kbuild index 5d6a53fd7521..3e8a88dcaa1d 100644 --- a/arch/x86/include/asm/Kbuild +++ b/arch/x86/include/asm/Kbuild @@ -9,5 +9,6 @@ generated-y += xen-hypercalls.h generic-y += clkdev.h generic-y += dma-contiguous.h generic-y += early_ioremap.h +generic-y += export.h generic-y += mcs_spinlock.h generic-y += mm-arch-hooks.h diff --git a/arch/x86/include/asm/export.h b/arch/x86/include/asm/export.h deleted file mode 100644 index 138de56b13eb..000000000000 --- a/arch/x86/include/asm/export.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifdef CONFIG_64BIT -#define KSYM_ALIGN 16 -#endif -#include diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h index 719db1968d81..97ce606459ae 100644 --- a/include/asm-generic/export.h +++ b/include/asm-generic/export.h @@ -5,12 +5,10 @@ #define KSYM_FUNC(x) x #endif #ifdef CONFIG_64BIT -#define __put .quad #ifndef KSYM_ALIGN #define KSYM_ALIGN 8 #endif #else -#define __put .long #ifndef KSYM_ALIGN #define KSYM_ALIGN 4 #endif @@ -25,6 +23,16 @@ #define KSYM(name) name #endif +.macro __put, val, name +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + .long \val - ., \name - . +#elif defined(CONFIG_64BIT) + .quad \val, \name +#else + .long \val, \name +#endif +.endm + /* * note on .section use: @progbits vs %progbits nastiness doesn't matter, * since we immediately emit into those sections anyway. diff --git a/include/linux/compiler.h b/include/linux/compiler.h index eca8ad75e28b..363fb3e734ec 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -590,4 +590,15 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s (_________p1); \ }) +/* + * Force the compiler to emit 'sym' as a symbol, so that we can reference + * it from inline assembler. Necessary in case 'sym' could be inlined + * otherwise, or eliminated entirely due to lack of references that are + * visibile to the compiler. + */ +#define __ADDRESSABLE(sym) \ + static void *__attribute__((section(".discard.text"), used)) \ + __PASTE(__discard_##sym, __LINE__)(void) \ + { return (void *)&sym; } \ + #endif /* __LINUX_COMPILER_H */ diff --git a/include/linux/export.h b/include/linux/export.h index 1a1dfdb2a5c6..156b974181a4 100644 --- a/include/linux/export.h +++ b/include/linux/export.h @@ -24,12 +24,6 @@ #define VMLINUX_SYMBOL_STR(x) __VMLINUX_SYMBOL_STR(x) #ifndef __ASSEMBLY__ -struct kernel_symbol -{ - unsigned long value; - const char *name; -}; - #ifdef MODULE extern struct module __this_module; #define THIS_MODULE (&__this_module) @@ -60,17 +54,46 @@ extern struct module __this_module; #define __CRC_SYMBOL(sym, sec) #endif +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +/* + * Emit the ksymtab entry as a pair of relative references: this reduces + * the size by half on 64-bit architectures, and eliminates the need for + * absolute relocations that require runtime processing on relocatable + * kernels. + */ +#define __KSYMTAB_ENTRY(sym, sec) \ + __ADDRESSABLE(sym) \ + asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \ + " .balign 8 \n" \ + VMLINUX_SYMBOL_STR(__ksymtab_##sym) ": \n" \ + " .long " VMLINUX_SYMBOL_STR(sym) "- . \n" \ + " .long " VMLINUX_SYMBOL_STR(__kstrtab_##sym) "- .\n" \ + " .previous \n") + +struct kernel_symbol { + signed int value_offset; + signed int name_offset; +}; +#else +#define __KSYMTAB_ENTRY(sym, sec) \ + static const struct kernel_symbol __ksymtab_##sym \ + __attribute__((section("___ksymtab" sec "+" #sym), used)) \ + = { (unsigned long)&sym, __kstrtab_##sym } + +struct kernel_symbol { + unsigned long value; + const char *name; +}; +#endif + /* For every exported symbol, place a struct in the __ksymtab section */ #define ___EXPORT_SYMBOL(sym, sec) \ extern typeof(sym) sym; \ __CRC_SYMBOL(sym, sec) \ static const char __kstrtab_##sym[] \ - __attribute__((section("__ksymtab_strings"), aligned(1))) \ + __attribute__((section("__ksymtab_strings"), used, aligned(1))) \ = VMLINUX_SYMBOL_STR(sym); \ - static const struct kernel_symbol __ksymtab_##sym \ - __used \ - __attribute__((section("___ksymtab" sec "+" #sym), used)) \ - = { (unsigned long)&sym, __kstrtab_##sym } + __KSYMTAB_ENTRY(sym, sec) #if defined(__KSYM_DEPS__) diff --git a/kernel/module.c b/kernel/module.c index 40f983cbea81..a45423dcc32d 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -539,12 +539,31 @@ static bool check_symbol(const struct symsearch *syms, return true; } +static unsigned long kernel_symbol_value(const struct kernel_symbol *sym) +{ +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + return (unsigned long)&sym->value_offset + sym->value_offset; +#else + return sym->value; +#endif +} + +static const char *kernel_symbol_name(const struct kernel_symbol *sym) +{ +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + return (const char *)((unsigned long)&sym->name_offset + + sym->name_offset); +#else + return sym->name; +#endif +} + static int cmp_name(const void *va, const void *vb) { const char *a; const struct kernel_symbol *b; a = va; b = vb; - return strcmp(a, b->name); + return strcmp(a, kernel_symbol_name(b)); } static bool find_symbol_in_section(const struct symsearch *syms, @@ -2190,7 +2209,7 @@ void *__symbol_get(const char *symbol) sym = NULL; preempt_enable(); - return sym ? (void *)sym->value : NULL; + return sym ? (void *)kernel_symbol_value(sym) : NULL; } EXPORT_SYMBOL_GPL(__symbol_get); @@ -2220,10 +2239,12 @@ static int verify_export_symbols(struct module *mod) for (i = 0; i < ARRAY_SIZE(arr); i++) { for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) { - if (find_symbol(s->name, &owner, NULL, true, false)) { + if (find_symbol(kernel_symbol_name(s), &owner, NULL, + true, false)) { pr_err("%s: exports duplicate symbol %s" " (owned by %s)\n", - mod->name, s->name, module_name(owner)); + mod->name, kernel_symbol_name(s), + module_name(owner)); return -ENOEXEC; } } @@ -2272,7 +2293,7 @@ static int simplify_symbols(struct module *mod, const struct load_info *info) ksym = resolve_symbol_wait(mod, info, name); /* Ok if resolved. */ if (ksym && !IS_ERR(ksym)) { - sym[i].st_value = ksym->value; + sym[i].st_value = kernel_symbol_value(ksym); break; } @@ -2532,7 +2553,7 @@ static int is_exported(const char *name, unsigned long value, ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab); else ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms); - return ks != NULL && ks->value == value; + return ks != NULL && kernel_symbol_value(ks) == value; } /* As per nm */ From patchwork Sat Aug 19 18:10:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 110457 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp2383758qge; Sat, 19 Aug 2017 11:11:17 -0700 (PDT) X-Received: by 10.84.175.67 with SMTP id s61mr13738908plb.296.1503166277261; Sat, 19 Aug 2017 11:11:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503166277; cv=none; d=google.com; s=arc-20160816; b=XRhqRAV8RpS4BdztZO9LWh2yeHsbFhesjAK0qbWxjmZGTjSpKsUNSjEcLbwY3qg9eL 463Qy4DWD0wqwJhiYE3lz3LUJMqAw6ZC9tpCTjCltExkXQbfllwY/usfBBSjLSd22m1t OIhFj5unDlJnpN/QHvdc3tQ1cuREHVJT3AHStiW5DpVmAt75IRy4EWjYAhnIz4MKZrty hNzC4sWm2ECuA8U99VQrYVOyboCIcWzTPMMmof6faTphrRJPhuhQI6C2AawRl11uQeL5 /GyQ1O/bS68e/EX0xRxqzr7iMGkKWSYBccdN5DwflZIVwfdvyb0wxR4mRHc+TwkAI72M eAtw== 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=FxmF2MLb+xevrPw41dj60xuQZQ7L8XgL7vrPD6eWzsI=; b=V+CGAvgnDCVAT7r42aQoZbbd0SYJcCoOIHFJodWe8HvxaZAE7jJ1WTWEUPgGoYyH3/ o76ty1FsfcbzZXVQ22+2xgNyLjy+MFRE7JF71XVp8lhcMYOqN7ry4Tj4CvwtfOq/iyXr fnPOo5kS6hxyBVXn2IpVs0Yywg2mT8zJm/ugUGAuMy4QR8UUJ7Em5tdGxobtmQnq05P9 sf5pTg7AX/wpRbDtqfOHFxw/2Gb2XFZFJZlt2x2CdW54XPOrzslyYilUyJUfkFcBYaaE PrCuDtTw6Gwc9RloImcxXvfgn4UwLN3GTcockceXWMSg7+g2eUk8VB395zgvpMr/oL/f E/RA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=gpfR9Rq1; 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 73si4273278pfs.652.2017.08.19.11.11.16; Sat, 19 Aug 2017 11:11:17 -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=gpfR9Rq1; 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 S1752255AbdHSSLO (ORCPT + 26 others); Sat, 19 Aug 2017 14:11:14 -0400 Received: from mail-wm0-f44.google.com ([74.125.82.44]:35932 "EHLO mail-wm0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751853AbdHSSLL (ORCPT ); Sat, 19 Aug 2017 14:11:11 -0400 Received: by mail-wm0-f44.google.com with SMTP id t201so21088597wmt.1 for ; Sat, 19 Aug 2017 11:11:10 -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=FxmF2MLb+xevrPw41dj60xuQZQ7L8XgL7vrPD6eWzsI=; b=gpfR9Rq16DlcbcAObiynW1/Llk6eIryWkUDVp2xhFLpXryKpOd7ShjoFncQYrG9Dyd SgYBE7GxarplDjeTQecSWKpj40Xjw4s3nZJk+WhP3WqTjeYHFJdW67nJxPFV5b4H9KoS K7h5XWPuSoSsisNxdF5XWyv6kXB0BaIK86uDQ= 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=FxmF2MLb+xevrPw41dj60xuQZQ7L8XgL7vrPD6eWzsI=; b=HGNV3FertRRR8OIEItZ7HHSUwzCpJh20Jr+v6X/Cf4cLa4+O5mMCCVggQdAad+g6bz 37ktcM10lcMJxZkXj2xX3sbzG37+9d8A0LWh0BGS/G104uOa47tZ+8QExWIbBuC6m487 MPsGwMBZQ+GyYSdK5Mj6kBmkOanyOeNcsIuwxpMNuXcvDuY8dnNtNv4fnzRj3IQTqXhf 2XO5qNALoZNd/tMGZ6BriIMV2KSIKhL60uPQZ8rTUcD+eLsb+rKyoLgbII3bRO1933mL Lik8hW1XiOCen4Zo2IS+LZo1UvfOSZIhSAk1qC83cHR3X8m3HZ04onGmIpHtinTlkWn6 iy1w== X-Gm-Message-State: AHYfb5h0+F1im8DMPRgbVLxPap0HdHVNLiMdQhPPfpF22lCiL70uorYa 8UMq1UqJ/q60lQNq2bt55w== X-Received: by 10.28.68.85 with SMTP id r82mr3612281wma.21.1503166268975; Sat, 19 Aug 2017 11:11:08 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id m127sm4056261wmm.46.2017.08.19.11.11.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Aug 2017 11:11:08 -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 , Nicolas Pitre , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu Subject: [PATCH v3 3/5] init: allow initcall tables to be emitted using relative references Date: Sat, 19 Aug 2017 19:10:33 +0100 Message-Id: <20170819181035.969-4-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170819181035.969-1-ard.biesheuvel@linaro.org> References: <20170819181035.969-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 | 44 +++++++++++++++----- init/main.c | 32 +++++++------- kernel/printk/printk.c | 4 +- security/security.c | 4 +- 4 files changed, 53 insertions(+), 31 deletions(-) -- 2.11.0 diff --git a/include/linux/init.h b/include/linux/init.h index 94769d687cf0..e816dfdbbd21 100644 --- a/include/linux/init.h +++ b/include/linux/init.h @@ -110,8 +110,24 @@ 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[]; +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +typedef signed int initcall_entry_t; + +static inline initcall_t initcall_from_entry(initcall_entry_t *entry) +{ + return (initcall_t)((unsigned long)entry + *entry); +} +#else +typedef initcall_t initcall_entry_t; + +static inline initcall_t initcall_from_entry(initcall_entry_t *entry) +{ + return *entry; +} +#endif + +extern initcall_entry_t __con_initcall_start[], __con_initcall_end[]; +extern initcall_entry_t __security_initcall_start[], __security_initcall_end[]; /* Used for contructor calls. */ typedef void (*ctor_fn_t)(void); @@ -161,9 +177,20 @@ 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"); +#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; +#endif + +#define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id) /* * Early initcalls run before initializing SMP. @@ -202,13 +229,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..ac2a91f71107 100644 --- a/init/main.c +++ b/init/main.c @@ -833,18 +833,18 @@ int __init_or_module do_one_initcall(initcall_t fn) } -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 = { +extern initcall_entry_t __initcall_start[]; +extern initcall_entry_t __initcall0_start[]; +extern initcall_entry_t __initcall1_start[]; +extern initcall_entry_t __initcall2_start[]; +extern initcall_entry_t __initcall3_start[]; +extern initcall_entry_t __initcall4_start[]; +extern initcall_entry_t __initcall5_start[]; +extern initcall_entry_t __initcall6_start[]; +extern initcall_entry_t __initcall7_start[]; +extern initcall_entry_t __initcall_end[]; + +static initcall_entry_t *initcall_levels[] __initdata = { __initcall0_start, __initcall1_start, __initcall2_start, @@ -870,7 +870,7 @@ static char *initcall_level_names[] __initdata = { static void __init do_initcall_level(int level) { - initcall_t *fn; + initcall_entry_t *fn; strcpy(initcall_command_line, saved_command_line); parse_args(initcall_level_names[level], @@ -880,7 +880,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 +911,10 @@ static void __init do_basic_setup(void) static void __init do_pre_smp_initcalls(void) { - initcall_t *fn; + 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..39d343c1c4b2 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; + 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..63b5f52bb098 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; + initcall_entry_t *call; call = __security_initcall_start; while (call < __security_initcall_end) { - (*call) (); + initcall_from_entry(call)(); call++; } } From patchwork Sat Aug 19 18:10:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 110460 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp2384170qge; Sat, 19 Aug 2017 11:11:46 -0700 (PDT) X-Received: by 10.98.36.93 with SMTP id r90mr6024251pfj.287.1503166306369; Sat, 19 Aug 2017 11:11:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503166306; cv=none; d=google.com; s=arc-20160816; b=p47Z+9815wBPn3X46Eu8EBnPh9Q5YdShs17gGvPQqywllFF2w3bJTKF1T6PhMQZCHE fqdov2zug7o/kkvCwPFM2/yoCR7u6juaqjS+y3WHZpryGOB4pcGJUC73BC2nrxit6ix7 Wrc2JfXZLZyCOJmW+icOcOloxa8gV93PZonSF6kUihe1uzkEjMHAxAALpxYLE7QFoCf+ 9TMcUoVYSncMi6CwsEstJLU5TBZGUnZffMJ+W8mrDo8l+Y2Y5KDhJ70l8M0G1uEBPOk6 jfkDM72acc7m3fgX+oI/hha5+7s4kCEPGyhZ4/9cNAhFYWD3x70NoxdEqdVMBbCAy8Ds 4BdA== 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=rYsb6llfpaqvuAGj2AHZ7qmMX4LU1ZUVfJ8j66knLjQ=; b=P/gy6Rj3Rf7/lxMMjfzCpBSDPUPKPmfRqIycsr8gQqNnVn7+iLc/f5JkEHbxsveA/H panubdvpjii6E7StoWuD7bxwYdTIm5PziMjcl84ai6HzxBJFiuXP+qCNJiXiUdG6sNfW lVcXGcU8eqYIanXOaaERq0gcaqCDBv00ciWns9e85HZfZQEgB3couCfH0miBQpnZOt+4 pSCUPR+KQ9XpSAn7WVTmqxQgIbyv7xjw4GImUKsQdSmsXFcL12+PL8MDs+Z2BQzoGxBz Q2E6bWvq53+umC2LVJJfsxOJXNC9QB+MjD749JzZABFpZh7YQQrCI6RKfb4COEFXbHsl 7X3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CJqicdWj; 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 g15si5626489pln.930.2017.08.19.11.11.46; Sat, 19 Aug 2017 11:11:46 -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=CJqicdWj; 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 S1752491AbdHSSLm (ORCPT + 26 others); Sat, 19 Aug 2017 14:11:42 -0400 Received: from mail-wm0-f44.google.com ([74.125.82.44]:34073 "EHLO mail-wm0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751853AbdHSSLP (ORCPT ); Sat, 19 Aug 2017 14:11:15 -0400 Received: by mail-wm0-f44.google.com with SMTP id c14so6538210wmh.1 for ; Sat, 19 Aug 2017 11:11:14 -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=rYsb6llfpaqvuAGj2AHZ7qmMX4LU1ZUVfJ8j66knLjQ=; b=CJqicdWjWDUAKxgiggFRpms2jK9xy42O7ZMZBszA7ykbw3mGwoPCu89xHmj44INc9G tjqbJkjwu0mrGfEUMlqVkvsEGGST5f+VsKqx8ZEbu+gt1/0mv5gVrimjdUeD6jT5/eGz RstgOCUKkr4f6ysNMFf8yDffYc1gn/NnEVSIE= 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=rYsb6llfpaqvuAGj2AHZ7qmMX4LU1ZUVfJ8j66knLjQ=; b=UroCwXOKYRWExHj4d0oUGUmvfQbsfqmuIzKoCbK8ghApFQf702IKt0p/6HPaS69JU+ eKi07ANofrZIbCDgt/2HAbRE1KS3BzLUiJOKFS2q3EE/PgWo7W6yUOhVHdNDTJJpqA0B zQg9ChrrusHx0IH9Aj3LRVVqGJfXJkvEDNU9hM7bvodEagjjg5Aqt5uq1ZXBpNze4V1V e9tQHjA7FUDJi/7qQP6tdTU0WjRXxQI1hbEZNyenkwNf1ZYPo3uU/sY0c4zVIxiZ8O/G 7VhVblgM8Z+ur4PEzIEXm1zown1HMJGy2WJgbxzO5xYYqtEXlsxC51phR+3sA/wjqSLp dUWQ== X-Gm-Message-State: AHYfb5jTdjm8TS4PUi8VIuai3OmmqLLF+QhyRt8irF+87Wqrs8Wc5ZY0 K/eGRwT2vtz8oLEhXtmGgw== X-Received: by 10.28.146.2 with SMTP id u2mr3255607wmd.76.1503166273754; Sat, 19 Aug 2017 11:11:13 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id m127sm4056261wmm.46.2017.08.19.11.11.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Aug 2017 11:11:13 -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 , Nicolas Pitre , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu Subject: [PATCH v3 4/5] drivers: pci: add support for relative addressing in quirk tables Date: Sat, 19 Aug 2017 19:10:34 +0100 Message-Id: <20170819181035.969-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170819181035.969-1-ard.biesheuvel@linaro.org> References: <20170819181035.969-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 PCI quirk tables to be emitted in a way that avoids absolute references to the hook functions. This reduces the size of the entries, and, more importantly, makes them invariant under runtime relocation (e.g., for KASLR) Cc: Bjorn Helgaas Signed-off-by: Ard Biesheuvel --- drivers/pci/quirks.c | 13 ++++++++++--- include/linux/pci.h | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) -- 2.11.0 diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c index 6967c6b4cf6b..10126612e342 100644 --- a/drivers/pci/quirks.c +++ b/drivers/pci/quirks.c @@ -3547,9 +3547,16 @@ static void pci_do_fixups(struct pci_dev *dev, struct pci_fixup *f, f->vendor == (u16) PCI_ANY_ID) && (f->device == dev->device || f->device == (u16) PCI_ANY_ID)) { - calltime = fixup_debug_start(dev, f->hook); - f->hook(dev); - fixup_debug_report(dev, calltime, f->hook); + void (*hook)(struct pci_dev *dev); +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + hook = (void *)((unsigned long)&f->hook_offset + + f->hook_offset); +#else + hook = f->hook; +#endif + calltime = fixup_debug_start(dev, hook); + hook(dev); + fixup_debug_report(dev, calltime, hook); } } diff --git a/include/linux/pci.h b/include/linux/pci.h index a75c13673852..dd3523c1afbb 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1735,7 +1735,11 @@ struct pci_fixup { u16 device; /* You can use PCI_ANY_ID here of course */ u32 class; /* You can use PCI_ANY_ID here too */ unsigned int class_shift; /* should be 0, 8, 16 */ +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + signed int hook_offset; +#else void (*hook)(struct pci_dev *dev); +#endif }; enum pci_fixup_pass { @@ -1749,12 +1753,28 @@ enum pci_fixup_pass { pci_fixup_suspend_late, /* pci_device_suspend_late() */ }; +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +#define __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \ + class_shift, hook) \ + __ADDRESSABLE(hook) \ + asm(".section " #sec ", \"a\" \n" \ + ".balign 16 \n" \ + ".short " #vendor ", " #device " \n" \ + ".long " #class ", " #class_shift " \n" \ + ".long " VMLINUX_SYMBOL_STR(hook) " - . \n" \ + ".previous \n"); +#define DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \ + class_shift, hook) \ + __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \ + class_shift, hook) +#else /* Anonymous variables would be nice... */ #define DECLARE_PCI_FIXUP_SECTION(section, name, vendor, device, class, \ class_shift, hook) \ static const struct pci_fixup __PASTE(__pci_fixup_##name,__LINE__) __used \ __attribute__((__section__(#section), aligned((sizeof(void *))))) \ = { vendor, device, class, class_shift, hook }; +#endif #define DECLARE_PCI_FIXUP_CLASS_EARLY(vendor, device, class, \ class_shift, hook) \ From patchwork Sat Aug 19 18:10:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 110459 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp2383880qge; Sat, 19 Aug 2017 11:11:26 -0700 (PDT) X-Received: by 10.99.182.67 with SMTP id v3mr12124885pgt.228.1503166286571; Sat, 19 Aug 2017 11:11:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503166286; cv=none; d=google.com; s=arc-20160816; b=fSlMS++5fH5WYOYiwnrhcJLf/Qo20FvFibYYOeba5UV69sFa07amqnaMZURTAyEQyu bLfHHaQ/rRt/Y6UOm+v/q2v40ngHttK1iiwsoRktvGJ0+FXDorAApmGtatqsmn0YrjLu 39MON2oiNQxfIbfhmFsxNk/2Ws6UkjqA4i3WZXSUpLC2RjD7Bk/GQ/IYQYtMUXdGvj5u ObhlamLVnVfzbq1iFbPOgo0LWGUK0MqgXm+iM+yLmF4zLJ2hhLlZidKE5Xrbsu3UxxoQ VjRu1BgjOHg/aDQQKQnYcvVmXy8EUru8k0Z/60uo3QcKT67lba1OEkcQOtD6IjTwfako ezLQ== 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=T0eIFkvXTGn2bVADgTPYmcybECRT8dELGRo+5YN0FJ4=; b=kaXx5/hy8CTi5DhtW8biwB43iSup8LdA+AtwYFkxrlwdILcywcKrEKHtXboTKhs5Uj hQfziuINs1JqV72s2qQiCBbbisra/DIPtm3JyAtLKxTeFpjWHVovTGeVf0g6J9aV5Qj4 jyuInwhJqCWxe5cSrooRJDskXr8epK1vHHiPvg0z1SJRVZCzCAgs6IMr8wHwSGVG8TZR Vb0+KyK3IRE7kv+NcVoa3Uwxi1bZq30rqrtK8B+TxmX9wBc5Dr6HqlFUcXdM9JRQrcgi SAm4c9ylCjMLnqClROvANmrK57CsxUg9U46DZ1ahvojGUloB458LR/tERT15FnYcmuOJ 1SOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Sp2cp7EW; 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 v13si5161206pgf.643.2017.08.19.11.11.26; Sat, 19 Aug 2017 11:11:26 -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=Sp2cp7EW; 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 S1752394AbdHSSLY (ORCPT + 26 others); Sat, 19 Aug 2017 14:11:24 -0400 Received: from mail-wm0-f44.google.com ([74.125.82.44]:37099 "EHLO mail-wm0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752349AbdHSSLU (ORCPT ); Sat, 19 Aug 2017 14:11:20 -0400 Received: by mail-wm0-f44.google.com with SMTP id i66so20962949wmg.0 for ; Sat, 19 Aug 2017 11:11:19 -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=T0eIFkvXTGn2bVADgTPYmcybECRT8dELGRo+5YN0FJ4=; b=Sp2cp7EWzZVVnXBAEnX/MPNZC36CajsgsXPGF9Ehn9gQy4WWMGbCorFF2WIsxFEsQa 6Byg8aoUstNuEySFiwmrniUodeRF3RR0kHLoNgH31YPekhb6coWH0rDvKXng+Bny4FWT lDsWjBXBQwducCOeUvKgw3gSHvZLIiAFziIEM= 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=T0eIFkvXTGn2bVADgTPYmcybECRT8dELGRo+5YN0FJ4=; b=dk/l7jM7OO/au6BOkATWZLAFB1l/Wrr1rlzNDpDyCEvd71iIoIYRxLYBSolf+mcop3 4Dc3NqO/qlgy2cFgVPCsCMMDKIDJdtv7NnyoO98OQqyUygL3A7YRvmdTHRe14aAvjNmM gGmx1uS+2mipUVDhZ5iqDKu0U0IZQaWRwLNkPnNjVTIdSizljVYuz8yHRHvJh2iHO0nQ X4YNdwB92JVmKawtoimTIzlU5itsICZwk+EPRcBxpz14ecwi8zfcPR4e37LXNAYcdfgj NHtYxPGAayJD11aVnfw0nix7B+4IY7TosPjr1cmMS+kzQKjgF2eRYbJTo5WmH6SowrEv OBiQ== X-Gm-Message-State: AHYfb5iC3l4EZbD2sENvMY5Z/GCV1xMeyB2EA+VrE4ORn07SwOTfyb7h 523waev0QaLcMTgsroD8NQ== X-Received: by 10.28.224.138 with SMTP id x132mr3003940wmg.90.1503166278921; Sat, 19 Aug 2017 11:11:18 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id m127sm4056261wmm.46.2017.08.19.11.11.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Aug 2017 11:11:18 -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 , Nicolas Pitre , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu Subject: [PATCH v3 5/5] kernel: tracepoints: add support for relative references Date: Sat, 19 Aug 2017 19:10:35 +0100 Message-Id: <20170819181035.969-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170819181035.969-1-ard.biesheuvel@linaro.org> References: <20170819181035.969-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To avoid the need for relocating absolute references to tracepoint structures at boot time when running relocatable kernels (which may take a disproportionate amount of space), add the option to emit these tables as relative references instead. Cc: Steven Rostedt Cc: Ingo Molnar Signed-off-by: Ard Biesheuvel --- include/linux/tracepoint.h | 19 +++++++++++++++---- kernel/tracepoint.c | 17 +++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) -- 2.11.0 Acked-by: Steven Rostedt (VMware) diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index a26ffbe09e71..d02bf1a695e8 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -228,6 +228,19 @@ extern void syscall_unregfunc(void); return static_key_false(&__tracepoint_##name.key); \ } +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +#define __TRACEPOINT_ENTRY(name) \ + asm(" .section \"__tracepoints_ptrs\", \"a\" \n" \ + " .balign 4 \n" \ + " .long " VMLINUX_SYMBOL_STR(__tracepoint_##name) " - .\n" \ + " .previous \n") +#else +#define __TRACEPOINT_ENTRY(name) \ + static struct tracepoint * const __tracepoint_ptr_##name __used \ + __attribute__((section("__tracepoints_ptrs"))) = \ + &__tracepoint_##name +#endif + /* * We have no guarantee that gcc and the linker won't up-align the tracepoint * structures, so we create an array of pointers that will be used for iteration @@ -237,11 +250,9 @@ extern void syscall_unregfunc(void); static const char __tpstrtab_##name[] \ __attribute__((section("__tracepoints_strings"))) = #name; \ struct tracepoint __tracepoint_##name \ - __attribute__((section("__tracepoints"))) = \ + __attribute__((section("__tracepoints"), used)) = \ { __tpstrtab_##name, STATIC_KEY_INIT_FALSE, reg, unreg, NULL };\ - static struct tracepoint * const __tracepoint_ptr_##name __used \ - __attribute__((section("__tracepoints_ptrs"))) = \ - &__tracepoint_##name; + __TRACEPOINT_ENTRY(name); #define DEFINE_TRACE(name) \ DEFINE_TRACE_FN(name, NULL, NULL); diff --git a/kernel/tracepoint.c b/kernel/tracepoint.c index 685c50ae6300..68b0218ef66b 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -508,12 +508,21 @@ static void for_each_tracepoint_range(struct tracepoint * const *begin, void (*fct)(struct tracepoint *tp, void *priv), void *priv) { - struct tracepoint * const *iter; - if (!begin) return; - for (iter = begin; iter < end; iter++) - fct(*iter, priv); + + if (IS_ENABLED(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)) { + const int *iter; + + for (iter = (const int *)begin; iter < (const int *)end; iter++) + fct((struct tracepoint *)((unsigned long)iter + *iter), + priv); + } else { + struct tracepoint * const *iter; + + for (iter = begin; iter < end; iter++) + fct(*iter, priv); + } } /**