From patchwork Mon Dec 25 20:54: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: 122701 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp225806qgn; Mon, 25 Dec 2017 12:55:46 -0800 (PST) X-Google-Smtp-Source: ACJfBouQ3HJ7SlTMZF4W4tW5ZpHGZFgd5yRnYJ1MOtFn3CMB3ixIGBM4A7ddN+1s7dbxdo6A2OpT X-Received: by 10.84.218.72 with SMTP id f8mr24053701plm.143.1514235346900; Mon, 25 Dec 2017 12:55:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514235346; cv=none; d=google.com; s=arc-20160816; b=Avk8vNZWhhtxlYN9xHNz2e9N6RbRsENMvwO3WivcbZ5O1CO5LFyd03Ci4UZ3fNRgdE 86OqvOvzwDmLQikek2hATTZLjDpbNMjoQWnt0Z+x/w1cHUeBZF8INMDs0yDY/e95AWFD M0w+ggE0dmJ09WSm5y3c3vMOZxy/9QipOixr3lO4QbCe5sFvP7dd4Z+bF5KO+q2ZaqDo 7lgNh4GAeMNtIm102b6WIASTS2OB6O+43QTqt2G8N5pdaqQ8UD6fatyHo1PxB6Jqszjz XyphacCHy/M/kvoHMOHH+TuCGcfsZUi2XMqrYb2/Vuq3/Q8BG1sxgx42uSlNG/5cQw4+ nzAg== 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=hET4AABNBeNsSpkOZUI8roSkdCikZPdlup845D/iO+8=; b=lO5+Oebkz5gswdaqKs6p/cE94jC2pITemsqBtPAuWmjAjKEvc3rpvfbackFQXVcGdG JhoBwULKbw6GxVqISK4R9WlMF8ZT3wklQNTR0SZalK6KKBa9xJYi/hsBuipmFUTifoXM dPCtHg86emuGbdrN6kXD13YBNpLaazUaBqDqT4C1tbduumVhD0ntpdagABxU9MiyVET2 XalhGnKa80C32ZoAzl3Kb/hnwZh2JAyJ2MI3jqCzoQAaxL3ADdIdLcQheMMb/LxP5IeO Bgw9pR75NrqQyYf2yS3GwXl8FL2608ESHYxbZ1xlJdxxcN/N4q+WX+sgCpZbVuhZ7tps 95xQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MUa6lfj1; 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 d12si21505668plo.388.2017.12.25.12.55.46; Mon, 25 Dec 2017 12:55:46 -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=MUa6lfj1; 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 S1753206AbdLYUz2 (ORCPT + 28 others); Mon, 25 Dec 2017 15:55:28 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:38452 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753144AbdLYUzS (ORCPT ); Mon, 25 Dec 2017 15:55:18 -0500 Received: by mail-wr0-f195.google.com with SMTP id o2so33421660wro.5 for ; Mon, 25 Dec 2017 12:55:18 -0800 (PST) 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=hET4AABNBeNsSpkOZUI8roSkdCikZPdlup845D/iO+8=; b=MUa6lfj1xmy9NUHTWfgLTx9dIhxXCvBfTiW6vaCxnjzQr9QQ4phDFsNkedXUMJ1IBb OF5hdF2K5BhlWykOs1Kln5hjij+9prS1y/WjcxJznYcJvtNPTi1PekFCuaFsCI9uWaC5 Fv+tC4vkFl4/kP7Uw/nGUMmDM7mVZWK7fTOz8= 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=hET4AABNBeNsSpkOZUI8roSkdCikZPdlup845D/iO+8=; b=jLtFGNkihW/oqq+n5/HiHLdFtV7ER+Jl3kYpD5RMQ5ElxaHVBNY/vGyvMXYBqPn20P /j2pSggvbZzjtyCx19zEw6uLKSugZfZw1z07L80s+2An5Nvast1alE12vH9bT8H7cOKv t/YPLeXDhuNroVajMA38xYqp4pYR9qUGlSsQ1aoqTrYBVnqpK6zwxY3eZIGGP3/SJp76 T/4zAnvEA54ky1HuGsbtPNTAhZdpSGZbC0Fe/xKncIIgQdx+ZWQKte3vBhspPumQNh6o 0ZHppwhCJx8KkHQ7vm1vV6mXr7eUW8j4PMn0aC3ZiL7OBc59m1OTfX4DrmtVpQnf5jOC U7RQ== X-Gm-Message-State: AKGB3mIxwoKnXYsQAeOjUfpNpM3q5xee7U09mnng/GVDh66FYQgn2Ggx OObLx102m0eJ/nt4Bk5pPaj0A2cGwZs= X-Received: by 10.223.175.50 with SMTP id z47mr23740531wrc.12.1514235317375; Mon, 25 Dec 2017 12:55:17 -0800 (PST) Received: from localhost.localdomain ([160.171.216.245]) by smtp.gmail.com with ESMTPSA id y42sm39552441wrc.96.2017.12.25.12.55.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Dec 2017 12:55:16 -0800 (PST) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Ralf Baechle , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , "David S. Miller" , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v5 1/8] arch: enable relative relocations for arm64, power, x86, s390 and x86 Date: Mon, 25 Dec 2017 20:54:33 +0000 Message-Id: <20171225205440.14575-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171225205440.14575-1-ard.biesheuvel@linaro.org> References: <20171225205440.14575-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 400b9e1b2f27..dbc036a7bd1b 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -959,4 +959,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 c9a7e9e1414f..66c7b9ab2a3d 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -89,6 +89,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 7da3e5c366a0..49ae5b43fe2b 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -156,7 +156,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 c51e6ce42e7a..e172478e2ae7 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -177,6 +177,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 || PPC32) && !RELOCATABLE && !HIBERNATION) diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig index 829c67986db7..ed29d1ebecd9 100644 --- a/arch/s390/Kconfig +++ b/arch/s390/Kconfig @@ -129,6 +129,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 d4fc98c50378..9f2bb853aedb 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -115,6 +115,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 Mon Dec 25 20:54: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: 122708 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp226810qgn; Mon, 25 Dec 2017 12:57:13 -0800 (PST) X-Google-Smtp-Source: ACJfBotIeXtk/Qm4L1u9cKuf8vs5LibS/6csFD1kETxM03L/PsktQQiv8goRelr+i84m5EB+TKAK X-Received: by 10.84.213.144 with SMTP id g16mr23687639pli.339.1514235433562; Mon, 25 Dec 2017 12:57:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514235433; cv=none; d=google.com; s=arc-20160816; b=x5o6T7/GKpMGETHnO9ubQ98SU0Qkz3WgP3sWYGoKaptmqDJbtoJLHIrgRN9aa+cG5l nKYDrfmWLjl0loRK7CAFOqEcQGLqxtoTeb1m/53UmVXT5sTsouVl6uRiXnSl628JHRMj 0kiVnZ0iwbUqLQX1UKiXcyOiUaqi+qoPVOkml9scqyoF/B+lB7iVSPH2TtYHypfPzj/x 1zWRnr0OpXj0g8ErKdM2LjOWXoF6Uq5jvV9eu2UBE/bXeMiwn3ec/4ptxlQrpQpgQilK rms+gJD3uNOzpNS4L1rmP1lFCq2vV168SYxWw1KBnNUagHBGDPMKDRxR2JdN5cF5nThe kn1Q== 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=hwNmpUBJll0CBM4WpZWcFTd/iFpXr+kE+wqybgwXamo=; b=sIWejZOslrOp3TznfbqrSM4aTtDPg/+Mv7OEmH1y2MqMUosfRXtuEqJe2/HvGZtQLE +8nClPINrQSz9wx9uEZV9z82qvuzju61LFgukre1MD++08g5XW7PIXw/YMaThccnpELE zpEfsZt/BB6JRhqp8kVr97f4NkZEnM2cuOwjpnW2iGT8eYr3u79yL1qa34AV3+xr6VgU jb3vQcbxcOWr9+x7c6S6ze77UR5rff/z5QrCV7qdoBd39qXVlVT2XStTLPvm+i2wCu99 hPHKsllgWg4jJ+kJrGD3fo2D7b7csz6dhJaKSzjC/3h5guAvlueRyc1fm5klwMl63+Um o/4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KeWdsxnj; 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 j12si14639289pgv.452.2017.12.25.12.57.13; Mon, 25 Dec 2017 12:57:13 -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=KeWdsxnj; 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 S1753468AbdLYU5K (ORCPT + 28 others); Mon, 25 Dec 2017 15:57:10 -0500 Received: from mail-wm0-f65.google.com ([74.125.82.65]:35936 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753159AbdLYUzZ (ORCPT ); Mon, 25 Dec 2017 15:55:25 -0500 Received: by mail-wm0-f65.google.com with SMTP id b76so32903069wmg.1 for ; Mon, 25 Dec 2017 12:55:24 -0800 (PST) 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=hwNmpUBJll0CBM4WpZWcFTd/iFpXr+kE+wqybgwXamo=; b=KeWdsxnjhacZ5ZoR7oCJAjv2UBOadogmnLCvH7IT/b9DE4W70x0B74dv/3blJz0sCG vmviHmuzds81AsM377XlCqAOuR9YAMu9Jhyq7c11K962gWAidCTXqHfodxPyFNP150d/ oMPPNKwiId0UECvE8aTDlJZ4uumRlnMvTu6sg= 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=hwNmpUBJll0CBM4WpZWcFTd/iFpXr+kE+wqybgwXamo=; b=p99CUVOmTxawCko6VDCvt+nRXzPKjeMdP3X5amA9yMF1NgkG23Mo333s/Scl0/H10R yZsVMOrhc650Vgk3A2JE3vBRxMhoSpXZrpH2Xp3A7Rv8Vo40kI0XpA3jN0lVdB+SyLNO PzRk+fHLmCpab1uK2l9XarkEq9rx5mTiFGUzQckkN3B6IV1qkPMkDcsY2obSP6fF/nRS Pk+BeqIdFIDEYknOZOaNiWRIrPmJKBhjb47fQFh5uGSxhBj9wj7jodBtd1S6ABBmZ/lU l3yrpwGyUNQAkJjlsYMWBKJBwP8xbmkx0DFHzM2WTPbHL5MmzUfAO7/65gTikljWXd6E /A8A== X-Gm-Message-State: AKGB3mKiiC+ohGnikiRUElVyb7tSHVBgJWf7RSCM1ONUwiCJJonimabM t0MZ99UsPY87CbnJCFghwUybX5Xpk30= X-Received: by 10.28.45.151 with SMTP id t145mr17907775wmt.108.1514235323280; Mon, 25 Dec 2017 12:55:23 -0800 (PST) Received: from localhost.localdomain ([160.171.216.245]) by smtp.gmail.com with ESMTPSA id y42sm39552441wrc.96.2017.12.25.12.55.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Dec 2017 12:55:22 -0800 (PST) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Ralf Baechle , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , "David S. Miller" , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org, Ingo Molnar Subject: [PATCH v5 2/8] module: use relative references for __ksymtab entries Date: Mon, 25 Dec 2017 20:54:34 +0000 Message-Id: <20171225205440.14575-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171225205440.14575-1-ard.biesheuvel@linaro.org> References: <20171225205440.14575-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 dependent on whether CONFIG_HAVE_ARCH_PREL32_RELOCATIONS is defined. Cc: Arnd Bergmann Cc: Andrew Morton Cc: Ingo Molnar Cc: Kees Cook Cc: Thomas Garnier Cc: Nicolas Pitre Acked-by: Jessica Yu Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/Kbuild | 1 + arch/x86/include/asm/export.h | 5 --- include/asm-generic/export.h | 12 ++++- include/linux/compiler.h | 11 +++++ include/linux/export.h | 46 +++++++++++++++----- kernel/module.c | 33 +++++++++++--- 6 files changed, 84 insertions(+), 24 deletions(-) -- 2.11.0 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 2a51d66689c5..000000000000 --- a/arch/x86/include/asm/export.h +++ /dev/null @@ -1,5 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.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 52e611ab9a6c..fe752d365334 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -327,4 +327,15 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s compiletime_assert(__native_word(t), \ "Need native word sized stores/loads for atomicity.") +/* + * 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..5112d0c41512 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,47 @@ extern struct module __this_module; #define __CRC_SYMBOL(sym, sec) #endif +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +#include +/* + * 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 dea01ac9cb74..d3a908ffc42c 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -549,12 +549,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, @@ -2198,7 +2217,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); @@ -2228,10 +2247,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; } } @@ -2280,7 +2301,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; } @@ -2540,7 +2561,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 Mon Dec 25 20:54: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: 122702 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp225814qgn; Mon, 25 Dec 2017 12:55:47 -0800 (PST) X-Google-Smtp-Source: ACJfBosxLI4sGSuo4wD//cydXuWYflXHQjbHRtrqqrMMXwUVBvxj9Rk9+HD2doSPAfNNht6wRjLt X-Received: by 10.99.111.206 with SMTP id k197mr20110729pgc.182.1514235347254; Mon, 25 Dec 2017 12:55:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514235347; cv=none; d=google.com; s=arc-20160816; b=VhF5w/XYynh59p6hGMYpLGhwaGQDDGsOT3rHx9veEoNOHJvhZ2SQDtqN6sLCdgaAs7 qCjbwyJJzofbL5RZZPyCbYmi1RnatVTvEo3l3d1OUE5XDEQ31XmE3OrgtLDBW+6fVOeC Gdeqt/00dSO3oJzmj85VmoBQ79+xehBAhFcg2s07yiQIcmmPDw3TdR4RO3TZFrQWGmAp l/E6ztQfBZx1cBi/SUMcfrRIJskRP0JuJTtGqg3iTncxR/+PqxPTpxL/WMfaIL8HPWg+ W/QGiezaDbATpZtQXc5UfWvA3EDC3pDLAUgc4PqRSjOFjHsGfWJ9ljX0UY5zQceWlqbK rjaQ== 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=lwLLrS7mueZ/zWj6YdhKL1lPIayLUTMzei7H77VCkQk=; b=hePPy8lEMguhFNRAx15UASV2uSqTWb4b0PCJ+T9ALKsnhzuyZCYhAMh6XOTJmRGulr 5vSBWCvJ1oX+4Il5ZV0dTu444sCs70gMu+cSAjZRVAmelWvLByLFeh6LgVG8WBxiPJUM ZQ/dOJN2aUpXU+Ql9HOUYBDPtMJndqjt+pPWfiaAB+htJvefel+L6dAaR3BvgELL0WCZ rhBciu2dlVTyumdZXRo58iY0ydwnaSjhEe745CPahjMUvKRjLG3wBdNBnCIdqrmw/WxX ZXwSjT8gmd4NQ4BdT0J06DygZYbt6pg+gdtr1FIrRDXgVu5Vh5AF2LlRT12HHJLghoO1 OesA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jcUuSxDj; 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 d12si21505668plo.388.2017.12.25.12.55.47; Mon, 25 Dec 2017 12:55:47 -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=jcUuSxDj; 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 S1753144AbdLYUzj (ORCPT + 28 others); Mon, 25 Dec 2017 15:55:39 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:37293 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753222AbdLYUza (ORCPT ); Mon, 25 Dec 2017 15:55:30 -0500 Received: by mail-wr0-f195.google.com with SMTP id f8so22633173wre.4 for ; Mon, 25 Dec 2017 12:55:29 -0800 (PST) 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=lwLLrS7mueZ/zWj6YdhKL1lPIayLUTMzei7H77VCkQk=; b=jcUuSxDjV6qUlFZNfjkwkooWeBRAhtuALdqXGpiYFN0qh/ik9DBdMQ/0X349oC23d0 3JjZFTNGenNghv+0eehfkXmrQl6pGeZ09YoXEr87h7h+7jtcTJyn0YJkqPrZ8ebqVGvB Bmo1HnZgSr2zEZjku2OZ+nZZWCzvV0gwm31pg= 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=lwLLrS7mueZ/zWj6YdhKL1lPIayLUTMzei7H77VCkQk=; b=JMHhhTPzS6Z7qGzDKpLxEE0gVjge0IgFGU9LVvbQQBRa2yrhxdcBV466Y1Rrw31a9H n9N6GYK5fJWu9yLDJ3UnCm5G9O9jd7MN1QcRMqeJ48fLlh2YCInbmDv0UkJbUQV5y36S 28UlqVOe1BUSRaRpn5EsDrKbvP/nvcDlaUuF7LjNwys5KTAIG6o0AYBj8my8SSEQ6W/A 1T2VorXqsEk5W+6mezPGZ9GabjW46S8QXts8v4LyYnc2hhZalIplhuOZn+MY523nNr4h dKssCu3tveWh+wFH3b5dlHp2i429i0/LbXvPcsDtySG09YFVqEEOfiUnWrzf6aPOZs3L wRag== X-Gm-Message-State: AKGB3mItodgGEb3B+/D01phIypGTmhlrWyI9YiZWAxMRxCYOaNAXpFK5 oKmLccYS2H/nLHMEucNRwiZaBp17+L8= X-Received: by 10.223.128.9 with SMTP id 9mr15934739wrk.70.1514235328837; Mon, 25 Dec 2017 12:55:28 -0800 (PST) Received: from localhost.localdomain ([160.171.216.245]) by smtp.gmail.com with ESMTPSA id y42sm39552441wrc.96.2017.12.25.12.55.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Dec 2017 12:55:28 -0800 (PST) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Ralf Baechle , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , "David S. Miller" , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v5 3/8] init: allow initcall tables to be emitted using relative references Date: Mon, 25 Dec 2017 20:54:35 +0000 Message-Id: <20171225205440.14575-4-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171225205440.14575-1-ard.biesheuvel@linaro.org> References: <20171225205440.14575-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 ea1b31101d9e..125bbea99c6b 100644 --- a/include/linux/init.h +++ b/include/linux/init.h @@ -109,8 +109,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); @@ -160,9 +176,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. @@ -201,13 +228,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 7b606fc48482..2cbe3c2804ab 100644 --- a/init/main.c +++ b/init/main.c @@ -845,18 +845,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, @@ -882,7 +882,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], @@ -892,7 +892,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) @@ -923,10 +923,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 b9006617710f..0516005261c7 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2611,7 +2611,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(); @@ -2622,7 +2622,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 1cd8526cb0b7..f648eeff06de 100644 --- a/security/security.c +++ b/security/security.c @@ -45,10 +45,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 Mon Dec 25 20:54:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 122706 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp226369qgn; Mon, 25 Dec 2017 12:56:34 -0800 (PST) X-Google-Smtp-Source: ACJfBovbJxYvNhWWW3+NteffkFv2ofv4HFvsJ6VZXYShYT4KmlQHvWQN6lBn8UD+NRg4sSDAS4/i X-Received: by 10.99.62.133 with SMTP id l127mr20480560pga.41.1514235394811; Mon, 25 Dec 2017 12:56:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514235394; cv=none; d=google.com; s=arc-20160816; b=Gg7gphC9RhoL4zyeo1/73ZdtVQDd3iy6kB5Y4KiHu0/trF8ElbpGPU6m0RFtMbWbC9 HTocXOVX6tPiibAgZ/4/cs7LsuYAURD3pArasPuPCqePqbRev/wAi0jyoHHDUAtqufEa f3xPVzbJFKePZ11xV8lQs2k1lRFCJqjttkBj/T4nriR/04Bbmn4lud6q4j4CoZkMhf2Y NBBpM7IFbHDnskBLP2LLjdn1qq2Ue3dKvUa0trUFClJE80V1oSlt2EFxM8BRMI7Jd92O o05OadXeJR+ukMncdrOQgbrsQMWtSQyMAfcHPk1K6+PLbHnKAU22GdzLuyATHFvEc/hQ X4gQ== 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=MNuvKMvZr3GVfTz4MvnzswRbek+xsbzeG112C9Cor2w=; b=ujNTlGUD9VvKQSTB2Zsgw1PVBGl5QUyzN7jdukhXRSanYH8WIz5jlhUSpE9l6MnHno 4kiVJ5oWBHOElA+mShzP5tXymqWNOnYWdIlkk5G4xmTJj6lkKWy8mW7d1CMxSdbSOqw5 L7WgnEVtrTpt82/BsLnbMI1dBOp+Hwrtg/MT6GUBTFKBvQlg7/5eZQRPYsbfOUgwONs6 knMub298X1AL0+dNnqSDqtlYeo10Bb4XWOOlFoc6QKArraObO4lmlKqnBSMc+pNy52cy HBUOsr2z/paAh9k1f9Uyl4M2GTheDG638y/dq/OXni+GFcvdfVsbu/hsjw3uQIflp4un KguA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BMyTz7wp; 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 t24si19536666pgv.106.2017.12.25.12.56.34; Mon, 25 Dec 2017 12:56:34 -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=BMyTz7wp; 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 S1753307AbdLYUzt (ORCPT + 28 others); Mon, 25 Dec 2017 15:55:49 -0500 Received: from mail-wm0-f65.google.com ([74.125.82.65]:46275 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753235AbdLYUzf (ORCPT ); Mon, 25 Dec 2017 15:55:35 -0500 Received: by mail-wm0-f65.google.com with SMTP id r78so33010112wme.5 for ; Mon, 25 Dec 2017 12:55:35 -0800 (PST) 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=MNuvKMvZr3GVfTz4MvnzswRbek+xsbzeG112C9Cor2w=; b=BMyTz7wp+Fxi1An3WUb91pRiOr+As7YcLQ8dArfHH7VtK2l4fc4Q3OOhL1nUxsLzhY KktWHlX+oWyjSJ3k1LQBpLpOIOsvMCPm+09fRDyD0xkV4StB6nNg1EUEWFpX6FmjYupi FamwgJg598YU7xHeL8gkuXiyPYyWKDrKfX6ro= 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=MNuvKMvZr3GVfTz4MvnzswRbek+xsbzeG112C9Cor2w=; b=iMSqFWd8n8kXxM30T9PzL9DODjhA5lVV3C0yQCJ3XdLdxwhxoVHN+LfWry3NKwgkyW mPQMu8fMOpsFxilext3C5+gceVccC91zvj8p/OFH9h6Y1RDh11x84o6Z0SDW2sKBTcbq VK9QaF9lHlBEolXInfjPGKoM5GQewgoGPgJ6463rpX6yNDQgZeJ+219GmzvDpJpOrTq5 C+naQq4vYa6i77brP043lvayEkSD0xEOYwSOxqW0eHtPi/WHktZgO0hHRRR3pIY+pxmG jHOGWjqPnaKJPqLLTLo6K3EmkpzdmgwoQ90QFfHhFnf9vlMrIT6dWiM3ib6mNOYjzITA Z7tA== X-Gm-Message-State: AKGB3mLythDjOwL4ZmhtbDfNhuGtny7Z1PaM8pMgCVaqdVYIgeNrWQWX +K2Bdb3m6cXvFpD1+q6dLYOTqdoJYYY= X-Received: by 10.28.9.146 with SMTP id 140mr22114484wmj.117.1514235334349; Mon, 25 Dec 2017 12:55:34 -0800 (PST) Received: from localhost.localdomain ([160.171.216.245]) by smtp.gmail.com with ESMTPSA id y42sm39552441wrc.96.2017.12.25.12.55.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Dec 2017 12:55:33 -0800 (PST) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Ralf Baechle , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , "David S. Miller" , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v5 4/8] PCI: Add support for relative addressing in quirk tables Date: Mon, 25 Dec 2017 20:54:36 +0000 Message-Id: <20171225205440.14575-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171225205440.14575-1-ard.biesheuvel@linaro.org> References: <20171225205440.14575-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) Acked-by: 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 10684b17d0bd..b6d51b4d5ce1 100644 --- a/drivers/pci/quirks.c +++ b/drivers/pci/quirks.c @@ -3556,9 +3556,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 c170c9250c8b..e8c34afb5d4a 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1792,7 +1792,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 { @@ -1806,12 +1810,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 Mon Dec 25 20:54:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 122703 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp225931qgn; Mon, 25 Dec 2017 12:55:57 -0800 (PST) X-Google-Smtp-Source: ACJfBot0nubwb4BnNIlZbH02IdzRpJHXyB+K7+pMFpLsWt0/4fnCR6YPL6JjoGYhroeKY3tqCkAz X-Received: by 10.84.172.195 with SMTP id n61mr22825945plb.321.1514235357400; Mon, 25 Dec 2017 12:55:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514235357; cv=none; d=google.com; s=arc-20160816; b=f8VgyQpObafWbqA/aABPVCbhrFkVbCxzujXI0UPZxyBflkMChKfGaR1owUUnkI4ynL iEYz64FG0vYZWTCbGRTT5NOK/jmj5nVTkhO1OyUBiCWFAzstH0+exYp8cRFihJhLD0Ww wwy6Hxb5JkWaRcnAOZNmxJLofNUwWgYZCRZ3QyZHxDEXT5oBBr30hkN1vopL34RKsYfM OyvNDQZXfwXYNcQ4pqAOP1W/9qQm4XwgS5J/leaav5SPHvylx7mcQ9IiaExhgMqtyvId xb42EsZS4fetGJCaONbGOCWodJ6XTRI6fQZ3I7rEiddpsCUSXqQZOe3rz766tjhAhXVy Hh8A== 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=FeRUkMrB+h9OzAG6tsXZHgHhFJKOYm9CVFNsKuS7D+M=; b=fSsNY6XM3rmAWSo4qn3IneFpQ2LYEu2GpbT74C/8JqyQFnYoZeV2imlsrahoe6GBWc /CKCbkV2CMf6gnko/65Rt4/SxsCr6NAhA4aTOZL0aaONQoFAxm2p55fYi9miAaBnPyRd HKV+G67MSu2WD0dOnit/m6MOXC7ABX18GsMDn/c9SW8NxjMyXa4pniljaNKt8u8cPoAD OQ62Irmwf2jLeSEHObhpHHhVrrnO4P2OzOoEEMVcfI6UYgWuL5GkiSpOGGcjHlsXMeBF FrMMS+zv7bhFMMc5KDpqsvMBvqIlQ6DNU0UYlPvNJrrynHHwJwJtaM9QCTOYdc6P5FM+ VgvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Q1HREJft; 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 y63si22079968pff.9.2017.12.25.12.55.57; Mon, 25 Dec 2017 12:55:57 -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=Q1HREJft; 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 S1753345AbdLYUzx (ORCPT + 28 others); Mon, 25 Dec 2017 15:55:53 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:35007 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753280AbdLYUzl (ORCPT ); Mon, 25 Dec 2017 15:55:41 -0500 Received: by mail-wr0-f195.google.com with SMTP id l19so21047462wrc.2 for ; Mon, 25 Dec 2017 12:55:41 -0800 (PST) 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=FeRUkMrB+h9OzAG6tsXZHgHhFJKOYm9CVFNsKuS7D+M=; b=Q1HREJftT2aZs/JqWZHOsaU2Z+TagXl6eJx5d8qWSHZLETi/xqSxljw9g7z/0zATGE duUGBNR37ud1cZgQ+9l7EbvqnYeqHzsK4SsA2kf1hhrWK+rl77iGjFYfxJozrsjDhnwN bXow+s9jmxFGkTsydCr38z9kopa8/WalpD6Z0= 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=FeRUkMrB+h9OzAG6tsXZHgHhFJKOYm9CVFNsKuS7D+M=; b=nTO2FoK1TrWeH+kSMkYkSLa2+c+yHOZSdhzahG60F13opX2/7fHPG7k4USWnYlRdn/ d3fz7pJzx5Mx7X3TWjQi5F66rVhO7lUSvv3PYF2yPRunLAlbLeiU2DPIhvirHgXDdxus efh1shgvN3C47flfzobBSoBYoe6rZRBfUrReqsg/MnaN2QelrV5Z9GS13jaTrXnPEX6R Sxyl9w613hcTx9VFzPSYymdfC1yCOLTTXFp8X8p33ZKxL+Q3YYaQQkpVAyFNfW24lfzB bJYfDaJVHw6UhBM86koeMiLGf3FJ085xmAkMH9fIGK1qgQxaQF9Q6Qvfcb3rUPIt7y8i 7Uyg== X-Gm-Message-State: AKGB3mLKd9i8Mj0UtEitkHQ5DdQB3fAOAJ0v5tHh9Ay+crIsAIZqG5Al aEHFhhzuU/eTG2umNESwBM1l2cezAoI= X-Received: by 10.223.145.230 with SMTP id 93mr21913104wri.190.1514235340172; Mon, 25 Dec 2017 12:55:40 -0800 (PST) Received: from localhost.localdomain ([160.171.216.245]) by smtp.gmail.com with ESMTPSA id y42sm39552441wrc.96.2017.12.25.12.55.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Dec 2017 12:55:39 -0800 (PST) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Ralf Baechle , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , "David S. Miller" , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v5 5/8] kernel: tracepoints: add support for relative references Date: Mon, 25 Dec 2017 20:54:37 +0000 Message-Id: <20171225205440.14575-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171225205440.14575-1-ard.biesheuvel@linaro.org> References: <20171225205440.14575-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 | 50 +++++++++++--------- 2 files changed, 42 insertions(+), 27 deletions(-) -- 2.11.0 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..05649fef106c 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -327,6 +327,28 @@ int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data) } EXPORT_SYMBOL_GPL(tracepoint_probe_unregister); +static void for_each_tracepoint_range(struct tracepoint * const *begin, + struct tracepoint * const *end, + void (*fct)(struct tracepoint *tp, void *priv), + void *priv) +{ + if (!begin) + return; + + 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); + } +} + #ifdef CONFIG_MODULES bool trace_module_has_bad_taint(struct module *mod) { @@ -391,15 +413,9 @@ EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier); * Ensure the tracer unregistered the module's probes before the module * teardown is performed. Prevents leaks of probe and data pointers. */ -static void tp_module_going_check_quiescent(struct tracepoint * const *begin, - struct tracepoint * const *end) +static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv) { - struct tracepoint * const *iter; - - if (!begin) - return; - for (iter = begin; iter < end; iter++) - WARN_ON_ONCE((*iter)->funcs); + WARN_ON_ONCE(tp->funcs); } static int tracepoint_module_coming(struct module *mod) @@ -450,8 +466,9 @@ static void tracepoint_module_going(struct module *mod) * Called the going notifier before checking for * quiescence. */ - tp_module_going_check_quiescent(mod->tracepoints_ptrs, - mod->tracepoints_ptrs + mod->num_tracepoints); + for_each_tracepoint_range(mod->tracepoints_ptrs, + mod->tracepoints_ptrs + mod->num_tracepoints, + tp_module_going_check_quiescent, NULL); break; } } @@ -503,19 +520,6 @@ static __init int init_tracepoints(void) __initcall(init_tracepoints); #endif /* CONFIG_MODULES */ -static void for_each_tracepoint_range(struct tracepoint * const *begin, - struct tracepoint * const *end, - 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); -} - /** * for_each_kernel_tracepoint - iteration on all kernel tracepoints * @fct: callback From patchwork Mon Dec 25 20:54:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 122707 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp226554qgn; Mon, 25 Dec 2017 12:56:51 -0800 (PST) X-Google-Smtp-Source: ACJfBovMSP0uCS0ea1hD/FL4JNQihNLwVzBiC6gqg+XE9dI5YxPxbkh92K1TqWh/BJr/zjD9Nqdh X-Received: by 10.99.116.1 with SMTP id p1mr7532874pgc.391.1514235411425; Mon, 25 Dec 2017 12:56:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514235411; cv=none; d=google.com; s=arc-20160816; b=orxmJ60mhQzbiTMzf/D6gN3gJz3IboyZT91wenMdB8cIlKiXoSeGxzV3z++mPvEUyB iNzAsBmI5dgXB242yrgk04OO8r3Zr9u0qPD9WNgZ4qgmm1wLAk/KwVYBgLVwo9sHxDZV GVi4AHLDHIN+T5WME2hE3xCa3CVpxKnLwpJMYR04AeuhoAbldvR65RA9iMg3xx1Fo5AH 51d3kJUULrIx6lW3BO/adoDbyu1KsmPuyQ1YN/Ja/6h03q3krpOvCkz1W2M9ZNEoVh7/ TEYLsuDW6S13BOZra34zbD3gP2GUf/bRKDkBTCbytjmLcO1YsDfQga1ocjFiM+MINxuO y1eg== 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=MRffbjiLeyjj2fnVRwnCqOYkBRlzupnaUjrp51GxkdQ=; b=Xx2sn27b2UDiGzQ5Fpp9BI6cYsAOARpvuNC8PXS8MGnaBgkZG94o4l7A2RcRlL4b0l kzFroRV4HTP/9Ccgu4kzTS2wnaMzcXOXnmnsLFPrz3e7XTMLvojq/x2P87osTcea6iBl 3DYOFBCvKKbyaoJBCLmWADsr3ytuu+UKEKsL2/PiiWNYc0t7pKo5R6AcXesWm2EcCbz+ A7LeIaW13oWjWMH7C+0/RctC2uWUg2gGA78+QU3EqgPOKzYLSDYTEWjQ1KiALec0IvMJ 0YS2Mou5WAxKiQ9BFYjj/Othc9A0+QbysfHhZRxfWK9zy4A7bldztbgXqDxkYbpmkOvg JliQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EqWAojXM; 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 y10si21650596pln.138.2017.12.25.12.56.51; Mon, 25 Dec 2017 12:56:51 -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=EqWAojXM; 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 S1753431AbdLYU4s (ORCPT + 28 others); Mon, 25 Dec 2017 15:56:48 -0500 Received: from mail-wr0-f193.google.com ([209.85.128.193]:39721 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753222AbdLYUzr (ORCPT ); Mon, 25 Dec 2017 15:55:47 -0500 Received: by mail-wr0-f193.google.com with SMTP id o101so4775885wrb.6 for ; Mon, 25 Dec 2017 12:55:46 -0800 (PST) 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=MRffbjiLeyjj2fnVRwnCqOYkBRlzupnaUjrp51GxkdQ=; b=EqWAojXMgPu+1baBQF6G0MLP29rwhAdUquUIQrK4g/IKvR6EYcBc4tdodAKD/loSas pZW1QDkvzEuohNpuWYK4kvHEe0y9kMm//i8B1ShacQJjXJc8tzpW+wEB+n9OZg/+meD0 24qWlrva+eHP8UG7Qty0sENHpbqujA8Gev8iI= 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=MRffbjiLeyjj2fnVRwnCqOYkBRlzupnaUjrp51GxkdQ=; b=X7rKAE7r54KP4gkjwUyCp3d6VhNH06lgIaeX0F7uliqhlhj5/UQ1K9PFtzlM+xL8Uh ALhkf97J1Yuec+0THlGqua2FJJeMLXdwhzsKok/Uc7LDa2nufO7iYimIs0vuH7zgBnOR e4YH9Rew5igXQYQlsnaNBO210daK5HSi76akc/p8bFii+hx2JAEBfl1P7YyrJlF+Vye9 xwyLlBeEjutu8QYZtLqDUENCsTXl2F4TDaw7l3x0VCzCxZHQfZg0kFToZoxj1T96jxxd LY6qtScsc5xXzXwvHVwdgMEjIyRJJufeLEukv4OqLnO2lOhWHzHVCGdMotJx6mUevCha A1Vw== X-Gm-Message-State: AKGB3mJg/BkGbuNTKjUa3jSMfwfNPzNSk90rCoEnzBSbEW6SKdeV/i5c l/FleulmACCZ3ecFBnVPvt9Z4XWRhKo= X-Received: by 10.223.154.19 with SMTP id z19mr22388200wrb.260.1514235345374; Mon, 25 Dec 2017 12:55:45 -0800 (PST) Received: from localhost.localdomain ([160.171.216.245]) by smtp.gmail.com with ESMTPSA id y42sm39552441wrc.96.2017.12.25.12.55.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Dec 2017 12:55:44 -0800 (PST) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Ralf Baechle , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , "David S. Miller" , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v5 6/8] kernel/jump_label: abstract jump_entry member accessors Date: Mon, 25 Dec 2017 20:54:38 +0000 Message-Id: <20171225205440.14575-7-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171225205440.14575-1-ard.biesheuvel@linaro.org> References: <20171225205440.14575-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In preparation of allowing architectures to use relative references in jump_label entries [which can dramatically reduce the memory footprint], introduce abstractions for references to the 'code' and 'key' members of struct jump_entry. Signed-off-by: Ard Biesheuvel --- arch/arm/include/asm/jump_label.h | 27 ++++++++++++++ arch/arm64/include/asm/jump_label.h | 27 ++++++++++++++ arch/mips/include/asm/jump_label.h | 27 ++++++++++++++ arch/powerpc/include/asm/jump_label.h | 27 ++++++++++++++ arch/s390/include/asm/jump_label.h | 20 +++++++++++ arch/sparc/include/asm/jump_label.h | 27 ++++++++++++++ arch/tile/include/asm/jump_label.h | 27 ++++++++++++++ arch/x86/include/asm/jump_label.h | 27 ++++++++++++++ kernel/jump_label.c | 38 +++++++++----------- 9 files changed, 225 insertions(+), 22 deletions(-) -- 2.11.0 diff --git a/arch/arm/include/asm/jump_label.h b/arch/arm/include/asm/jump_label.h index e12d7d096fc0..7b05b404063a 100644 --- a/arch/arm/include/asm/jump_label.h +++ b/arch/arm/include/asm/jump_label.h @@ -45,5 +45,32 @@ struct jump_entry { jump_label_t key; }; +static inline jump_label_t jump_entry_code(const struct jump_entry *entry) +{ + return entry->code; +} + +static inline struct static_key *jump_entry_key(const struct jump_entry *entry) +{ + return (struct static_key *)((unsigned long)entry->key & ~1UL); +} + +static inline bool jump_entry_is_branch(const struct jump_entry *entry) +{ + return (unsigned long)entry->key & 1UL; +} + +static inline bool jump_entry_is_module_init(const struct jump_entry *entry) +{ + return entry->code == 0; +} + +static inline void jump_entry_set_module_init(struct jump_entry *entry) +{ + entry->code = 0; +} + +#define jump_label_swap NULL + #endif /* __ASSEMBLY__ */ #endif diff --git a/arch/arm64/include/asm/jump_label.h b/arch/arm64/include/asm/jump_label.h index 1b5e0e843c3a..9d6e46355c89 100644 --- a/arch/arm64/include/asm/jump_label.h +++ b/arch/arm64/include/asm/jump_label.h @@ -62,5 +62,32 @@ struct jump_entry { jump_label_t key; }; +static inline jump_label_t jump_entry_code(const struct jump_entry *entry) +{ + return entry->code; +} + +static inline struct static_key *jump_entry_key(const struct jump_entry *entry) +{ + return (struct static_key *)((unsigned long)entry->key & ~1UL); +} + +static inline bool jump_entry_is_branch(const struct jump_entry *entry) +{ + return (unsigned long)entry->key & 1UL; +} + +static inline bool jump_entry_is_module_init(const struct jump_entry *entry) +{ + return entry->code == 0; +} + +static inline void jump_entry_set_module_init(struct jump_entry *entry) +{ + entry->code = 0; +} + +#define jump_label_swap NULL + #endif /* __ASSEMBLY__ */ #endif /* __ASM_JUMP_LABEL_H */ diff --git a/arch/mips/include/asm/jump_label.h b/arch/mips/include/asm/jump_label.h index e77672539e8e..70df9293dc49 100644 --- a/arch/mips/include/asm/jump_label.h +++ b/arch/mips/include/asm/jump_label.h @@ -66,5 +66,32 @@ struct jump_entry { jump_label_t key; }; +static inline jump_label_t jump_entry_code(const struct jump_entry *entry) +{ + return entry->code; +} + +static inline struct static_key *jump_entry_key(const struct jump_entry *entry) +{ + return (struct static_key *)((unsigned long)entry->key & ~1UL); +} + +static inline bool jump_entry_is_branch(const struct jump_entry *entry) +{ + return (unsigned long)entry->key & 1UL; +} + +static inline bool jump_entry_is_module_init(const struct jump_entry *entry) +{ + return entry->code == 0; +} + +static inline void jump_entry_set_module_init(struct jump_entry *entry) +{ + entry->code = 0; +} + +#define jump_label_swap NULL + #endif /* __ASSEMBLY__ */ #endif /* _ASM_MIPS_JUMP_LABEL_H */ diff --git a/arch/powerpc/include/asm/jump_label.h b/arch/powerpc/include/asm/jump_label.h index 9a287e0ac8b1..412b2699c9f6 100644 --- a/arch/powerpc/include/asm/jump_label.h +++ b/arch/powerpc/include/asm/jump_label.h @@ -59,6 +59,33 @@ struct jump_entry { jump_label_t key; }; +static inline jump_label_t jump_entry_code(const struct jump_entry *entry) +{ + return entry->code; +} + +static inline struct static_key *jump_entry_key(const struct jump_entry *entry) +{ + return (struct static_key *)((unsigned long)entry->key & ~1UL); +} + +static inline bool jump_entry_is_branch(const struct jump_entry *entry) +{ + return (unsigned long)entry->key & 1UL; +} + +static inline bool jump_entry_is_module_init(const struct jump_entry *entry) +{ + return entry->code == 0; +} + +static inline void jump_entry_set_module_init(struct jump_entry *entry) +{ + entry->code = 0; +} + +#define jump_label_swap NULL + #else #define ARCH_STATIC_BRANCH(LABEL, KEY) \ 1098: nop; \ diff --git a/arch/s390/include/asm/jump_label.h b/arch/s390/include/asm/jump_label.h index 40f651292aa7..3d4a08e9514b 100644 --- a/arch/s390/include/asm/jump_label.h +++ b/arch/s390/include/asm/jump_label.h @@ -50,5 +50,25 @@ struct jump_entry { jump_label_t key; }; +static inline jump_label_t jump_entry_code(const struct jump_entry *entry) +{ + return entry->code; +} + +static inline jump_label_t jump_entry_key(const struct jump_entry *entry) +{ + return entry->key; +} + +static inline bool jump_entry_is_module_init(const struct jump_entry *entry) +{ + return entry->code == 0; +} + +static inline void jump_entry_set_module_init(struct jump_entry *entry) +{ + entry->code = 0; +} + #endif /* __ASSEMBLY__ */ #endif diff --git a/arch/sparc/include/asm/jump_label.h b/arch/sparc/include/asm/jump_label.h index 94eb529dcb77..18e893687f7c 100644 --- a/arch/sparc/include/asm/jump_label.h +++ b/arch/sparc/include/asm/jump_label.h @@ -48,5 +48,32 @@ struct jump_entry { jump_label_t key; }; +static inline jump_label_t jump_entry_code(const struct jump_entry *entry) +{ + return entry->code; +} + +static inline struct static_key *jump_entry_key(const struct jump_entry *entry) +{ + return (struct static_key *)((unsigned long)entry->key & ~1UL); +} + +static inline bool jump_entry_is_branch(const struct jump_entry *entry) +{ + return (unsigned long)entry->key & 1UL; +} + +static inline bool jump_entry_is_module_init(const struct jump_entry *entry) +{ + return entry->code == 0; +} + +static inline void jump_entry_set_module_init(struct jump_entry *entry) +{ + entry->code = 0; +} + +#define jump_label_swap NULL + #endif /* __ASSEMBLY__ */ #endif diff --git a/arch/tile/include/asm/jump_label.h b/arch/tile/include/asm/jump_label.h index cde7573f397b..86acaa6ff33d 100644 --- a/arch/tile/include/asm/jump_label.h +++ b/arch/tile/include/asm/jump_label.h @@ -55,4 +55,31 @@ struct jump_entry { jump_label_t key; }; +static inline jump_label_t jump_entry_code(const struct jump_entry *entry) +{ + return entry->code; +} + +static inline struct static_key *jump_entry_key(const struct jump_entry *entry) +{ + return (struct static_key *)((unsigned long)entry->key & ~1UL); +} + +static inline bool jump_entry_is_branch(const struct jump_entry *entry) +{ + return (unsigned long)entry->key & 1UL; +} + +static inline bool jump_entry_is_module_init(const struct jump_entry *entry) +{ + return entry->code == 0; +} + +static inline void jump_entry_set_module_init(struct jump_entry *entry) +{ + entry->code = 0; +} + +#define jump_label_swap NULL + #endif /* _ASM_TILE_JUMP_LABEL_H */ diff --git a/arch/x86/include/asm/jump_label.h b/arch/x86/include/asm/jump_label.h index 8c0de4282659..009ff2699d07 100644 --- a/arch/x86/include/asm/jump_label.h +++ b/arch/x86/include/asm/jump_label.h @@ -74,6 +74,33 @@ struct jump_entry { jump_label_t key; }; +static inline jump_label_t jump_entry_code(const struct jump_entry *entry) +{ + return entry->code; +} + +static inline struct static_key *jump_entry_key(const struct jump_entry *entry) +{ + return (struct static_key *)((unsigned long)entry->key & ~1UL); +} + +static inline bool jump_entry_is_branch(const struct jump_entry *entry) +{ + return (unsigned long)entry->key & 1UL; +} + +static inline bool jump_entry_is_module_init(const struct jump_entry *entry) +{ + return entry->code == 0; +} + +static inline void jump_entry_set_module_init(struct jump_entry *entry) +{ + entry->code = 0; +} + +#define jump_label_swap NULL + #else /* __ASSEMBLY__ */ .macro STATIC_JUMP_IF_TRUE target, key, def diff --git a/kernel/jump_label.c b/kernel/jump_label.c index 8594d24e4adc..baed094ab8d1 100644 --- a/kernel/jump_label.c +++ b/kernel/jump_label.c @@ -37,10 +37,12 @@ static int jump_label_cmp(const void *a, const void *b) const struct jump_entry *jea = a; const struct jump_entry *jeb = b; - if (jea->key < jeb->key) + if ((unsigned long)jump_entry_key(jea) < + (unsigned long)jump_entry_key(jeb)) return -1; - if (jea->key > jeb->key) + if ((unsigned long)jump_entry_key(jea) > + (unsigned long)jump_entry_key(jeb)) return 1; return 0; @@ -53,7 +55,8 @@ jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop) size = (((unsigned long)stop - (unsigned long)start) / sizeof(struct jump_entry)); - sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL); + sort(start, size, sizeof(struct jump_entry), jump_label_cmp, + jump_label_swap); } static void jump_label_update(struct static_key *key); @@ -254,8 +257,8 @@ EXPORT_SYMBOL_GPL(jump_label_rate_limit); static int addr_conflict(struct jump_entry *entry, void *start, void *end) { - if (entry->code <= (unsigned long)end && - entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start) + if (jump_entry_code(entry) <= (unsigned long)end && + jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE > (unsigned long)start) return 1; return 0; @@ -314,16 +317,6 @@ static inline void static_key_set_linked(struct static_key *key) key->type |= JUMP_TYPE_LINKED; } -static inline struct static_key *jump_entry_key(struct jump_entry *entry) -{ - return (struct static_key *)((unsigned long)entry->key & ~1UL); -} - -static bool jump_entry_branch(struct jump_entry *entry) -{ - return (unsigned long)entry->key & 1UL; -} - /*** * A 'struct static_key' uses a union such that it either points directly * to a table of 'struct jump_entry' or to a linked list of modules which in @@ -348,7 +341,7 @@ static enum jump_label_type jump_label_type(struct jump_entry *entry) { struct static_key *key = jump_entry_key(entry); bool enabled = static_key_enabled(key); - bool branch = jump_entry_branch(entry); + bool branch = jump_entry_is_branch(entry); /* See the comment in linux/jump_label.h */ return enabled ^ branch; @@ -364,7 +357,8 @@ static void __jump_label_update(struct static_key *key, * kernel_text_address() verifies we are not in core kernel * init code, see jump_label_invalidate_module_init(). */ - if (entry->code && kernel_text_address(entry->code)) + if (jump_entry_is_module_init(entry) && + kernel_text_address(jump_entry_code(entry))) arch_jump_label_transform(entry, jump_label_type(entry)); } } @@ -417,7 +411,7 @@ static enum jump_label_type jump_label_init_type(struct jump_entry *entry) { struct static_key *key = jump_entry_key(entry); bool type = static_key_type(key); - bool branch = jump_entry_branch(entry); + bool branch = jump_entry_is_branch(entry); /* See the comment in linux/jump_label.h */ return type ^ branch; @@ -541,7 +535,7 @@ static int jump_label_add_module(struct module *mod) continue; key = iterk; - if (within_module(iter->key, mod)) { + if (within_module((unsigned long)key, mod)) { static_key_set_entries(key, iter); continue; } @@ -591,7 +585,7 @@ static void jump_label_del_module(struct module *mod) key = jump_entry_key(iter); - if (within_module(iter->key, mod)) + if (within_module((unsigned long)key, mod)) continue; /* No memory during module load */ @@ -634,8 +628,8 @@ static void jump_label_invalidate_module_init(struct module *mod) struct jump_entry *iter; for (iter = iter_start; iter < iter_stop; iter++) { - if (within_module_init(iter->code, mod)) - iter->code = 0; + if (within_module_init(jump_entry_code(iter), mod)) + jump_entry_set_module_init(iter); } } From patchwork Mon Dec 25 20:54:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 122704 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp226054qgn; Mon, 25 Dec 2017 12:56:08 -0800 (PST) X-Google-Smtp-Source: ACJfBosApFkH3j7oiOLew2hqG1w/iBBdLMaDmuGYpjpACwBENmwojV5QpzeTZ27ho1v1rlfg637/ X-Received: by 10.99.110.13 with SMTP id j13mr1232499pgc.446.1514235368514; Mon, 25 Dec 2017 12:56:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514235368; cv=none; d=google.com; s=arc-20160816; b=bQ2N2hOXqglCaEbnYUrlHlxAOnrevBcVYJCOKb+XzM1Kbj2yOEg2YSTgbw1P2V8/bg lfobeBFOzTF+7Ye4ERvwa13kPqS6b59/DH94ehLAlLWiKUgt4X6Xff88mBeA5qi/BLaT YZQxu7qdKfyhUFe7xagWgLuJMoD540wrSS1PuqqbhU1TP7Do8B9Y6BeaEOjSWr4CzV2M RatLjbEfkcwLU2pZdX5q4ZJBAUS5VvVRvGAP0TW8h+Hx6z6iBmGB77/viRgDovb3x0Iy MOWJOMNDzsqcB2IIm721zuFzrNYx453RGf+ANQO8AeZW5kxZu4VhVCUSB3vOW/Qzxh1F Y3fg== 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=nmovwwxZQQa2ugEDLnXxoU69CsqWUnhsDto5qVfQljI=; b=hODpArk2Mze22wu2+wndPd55S9aqYtSbgwI/5FWCCa5ethZcsdHAku7VMWbY94wqSw 3jJO2J6d1joU7HfCuUWde8QtoeKIZjSSmraImOuAGnsi6CN2ozloz5HAhn9hR/iOnGOc bBZhKWoR27sMPZW/3EHtq10IoPczpcPEiwnUC90TSTyLkufXgJc9Mf9kQtp/Buytak4e SrktJVmd4CuzMpTAugaa+m2WVNocoTL0lZEvN2xTtnULTgMPWPt2kSAAtGJOBIzc6xqL hxuvTIm31VevUTlSUXMNOy7xAaXZcK6dfMaT9FebrGn8YPvH89AMeFcQKSNuiV57yQrj pxJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FoybLdU3; 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 p26si21892532pfh.220.2017.12.25.12.56.08; Mon, 25 Dec 2017 12:56:08 -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=FoybLdU3; 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 S1753385AbdLYU4E (ORCPT + 28 others); Mon, 25 Dec 2017 15:56:04 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:43103 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753335AbdLYUzy (ORCPT ); Mon, 25 Dec 2017 15:55:54 -0500 Received: by mail-wm0-f68.google.com with SMTP id n138so32814618wmg.2 for ; Mon, 25 Dec 2017 12:55:53 -0800 (PST) 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=nmovwwxZQQa2ugEDLnXxoU69CsqWUnhsDto5qVfQljI=; b=FoybLdU34h7tD2YvNo1TOxrVEvcKxDgXeKMEmlTyHo14rK82VWAM7Gt4bG28T0C9j2 PlA5sqnwmPwYuhY3eMCsxXR2D9oaM0zNKDSw5VLk2RKm/LC0aaMgOXCdXgiHjieBY5D9 6dHlDOePmBPWtmBMMmFiCQS3T86vTRkDOcgI4= 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=nmovwwxZQQa2ugEDLnXxoU69CsqWUnhsDto5qVfQljI=; b=ETN7t4UTY0kMfYjofxifPRndFZXDnDi7eOg9wbqmp//m10wnu6n/4MTAC+gzgO6L20 BDF+LAQRV+76zh+7x/q9CgrAmjMGNWN+DBfYRQuXz24Fg101/P4sBcCK5z8OYhUsdryd b776cGqbidleDTqjtTeRNL4BGksnpTAcgTHuoeVZsKIy0PLDXBdftrTBWnza+uUd73NT 07e8vFcW4JLjxjqbtACOb9bn7HkWolAc26/6+t0PjbsjhZBl6m9nn3Y06q7qiukK9t5F GKTRFHYjsOwlgEqtT1oRq4HTPuecMZsIPqLWLkqYk3tzwpuh7QErjL6xl92Fwu/Y1aSg fsSQ== X-Gm-Message-State: AKGB3mKQIa/FrljYPv9PB3NjgWOwyoHkPp72ryAQVyzVdCU+/Zhwkpl6 kN5UUx4qjonJadcekxxDEc+hDbtUKww= X-Received: by 10.28.130.208 with SMTP id e199mr19345255wmd.75.1514235352290; Mon, 25 Dec 2017 12:55:52 -0800 (PST) Received: from localhost.localdomain ([160.171.216.245]) by smtp.gmail.com with ESMTPSA id y42sm39552441wrc.96.2017.12.25.12.55.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Dec 2017 12:55:51 -0800 (PST) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Ralf Baechle , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , "David S. Miller" , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v5 7/8] arm64/kernel: jump_label: use relative references Date: Mon, 25 Dec 2017 20:54:39 +0000 Message-Id: <20171225205440.14575-8-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171225205440.14575-1-ard.biesheuvel@linaro.org> References: <20171225205440.14575-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On a randomly chosen distro kernel build for arm64, vmlinux.o shows the following sections, containing jump label entries, and the associated RELA relocation records, respectively: ... [38088] __jump_table PROGBITS 0000000000000000 00e19f30 000000000002ea10 0000000000000000 WA 0 0 8 [38089] .rela__jump_table RELA 0000000000000000 01fd8bb0 000000000008be30 0000000000000018 I 38178 38088 8 ... In other words, we have 190 KB worth of 'struct jump_entry' instances, and 573 KB worth of RELA entries to relocate each entry's code, target and key members. This means the RELA section occupies 10% of the .init segment, and the two sections combined represent 5% of vmlinux's entire memory footprint. So let's switch from 64-bit absolute references to 32-bit relative references: this reduces the size of the __jump_table by 50%, and gets rid of the RELA section entirely. Note that this requires some extra care in the sorting routine, given that the offsets change when entries are moved around in the jump_entry table. Signed-off-by: Ard Biesheuvel --- arch/arm64/include/asm/jump_label.h | 27 ++++++++++++-------- arch/arm64/kernel/jump_label.c | 22 +++++++++++++--- 2 files changed, 36 insertions(+), 13 deletions(-) -- 2.11.0 diff --git a/arch/arm64/include/asm/jump_label.h b/arch/arm64/include/asm/jump_label.h index 9d6e46355c89..5cec68616125 100644 --- a/arch/arm64/include/asm/jump_label.h +++ b/arch/arm64/include/asm/jump_label.h @@ -30,8 +30,8 @@ static __always_inline bool arch_static_branch(struct static_key *key, bool bran { asm goto("1: nop\n\t" ".pushsection __jump_table, \"aw\"\n\t" - ".align 3\n\t" - ".quad 1b, %l[l_yes], %c0\n\t" + ".align 2\n\t" + ".long 1b - ., %l[l_yes] - ., %c0 - .\n\t" ".popsection\n\t" : : "i"(&((char *)key)[branch]) : : l_yes); @@ -44,8 +44,8 @@ static __always_inline bool arch_static_branch_jump(struct static_key *key, bool { asm goto("1: b %l[l_yes]\n\t" ".pushsection __jump_table, \"aw\"\n\t" - ".align 3\n\t" - ".quad 1b, %l[l_yes], %c0\n\t" + ".align 2\n\t" + ".long 1b - ., %l[l_yes] - ., %c0 - .\n\t" ".popsection\n\t" : : "i"(&((char *)key)[branch]) : : l_yes); @@ -57,19 +57,26 @@ static __always_inline bool arch_static_branch_jump(struct static_key *key, bool typedef u64 jump_label_t; struct jump_entry { - jump_label_t code; - jump_label_t target; - jump_label_t key; + s32 code; + s32 target; + s32 key; }; static inline jump_label_t jump_entry_code(const struct jump_entry *entry) { - return entry->code; + return (jump_label_t)&entry->code + entry->code; +} + +static inline jump_label_t jump_entry_target(const struct jump_entry *entry) +{ + return (jump_label_t)&entry->target + entry->target; } static inline struct static_key *jump_entry_key(const struct jump_entry *entry) { - return (struct static_key *)((unsigned long)entry->key & ~1UL); + unsigned long key = (unsigned long)&entry->key + entry->key; + + return (struct static_key *)(key & ~1UL); } static inline bool jump_entry_is_branch(const struct jump_entry *entry) @@ -87,7 +94,7 @@ static inline void jump_entry_set_module_init(struct jump_entry *entry) entry->code = 0; } -#define jump_label_swap NULL +void jump_label_swap(void *a, void *b, int size); #endif /* __ASSEMBLY__ */ #endif /* __ASM_JUMP_LABEL_H */ diff --git a/arch/arm64/kernel/jump_label.c b/arch/arm64/kernel/jump_label.c index c2dd1ad3e648..2b8e459e91f7 100644 --- a/arch/arm64/kernel/jump_label.c +++ b/arch/arm64/kernel/jump_label.c @@ -25,12 +25,12 @@ void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type) { - void *addr = (void *)entry->code; + void *addr = (void *)jump_entry_code(entry); u32 insn; if (type == JUMP_LABEL_JMP) { - insn = aarch64_insn_gen_branch_imm(entry->code, - entry->target, + insn = aarch64_insn_gen_branch_imm(jump_entry_code(entry), + jump_entry_target(entry), AARCH64_INSN_BRANCH_NOLINK); } else { insn = aarch64_insn_gen_nop(); @@ -50,4 +50,20 @@ void arch_jump_label_transform_static(struct jump_entry *entry, */ } +void jump_label_swap(void *a, void *b, int size) +{ + long delta = (unsigned long)a - (unsigned long)b; + struct jump_entry *jea = a; + struct jump_entry *jeb = b; + struct jump_entry tmp = *jea; + + jea->code = jeb->code - delta; + jea->target = jeb->target - delta; + jea->key = jeb->key - delta; + + jeb->code = tmp.code + delta; + jeb->target = tmp.target + delta; + jeb->key = tmp.key + delta; +} + #endif /* HAVE_JUMP_LABEL */ From patchwork Mon Dec 25 20:54:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 122705 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp226136qgn; Mon, 25 Dec 2017 12:56:18 -0800 (PST) X-Google-Smtp-Source: ACJfBosw5hLWa72gxAXM9m7JlI3TlME+26BprVBKyDqbeBMJoO5ezG61qKkJqsZsX58FkKbG4VFT X-Received: by 10.84.171.193 with SMTP id l59mr23708126plb.33.1514235378050; Mon, 25 Dec 2017 12:56:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514235378; cv=none; d=google.com; s=arc-20160816; b=f3QXRnxlwMatkfcaUx/4BAhwCA82uu8VyufTQSIHAMUbdhMbDcw/rh/CmuZQOQ6UFh TV1sCnWvIO0wylfWItDZexbiVAjBvTAgxSn4RVBtMZBBP1WW9Pgxwrc2VcP7DSdrMhz4 rPGMg1XCj8mkiu+euw1Cte5WMNf1zM2epuvuiJj4U86sJBwpaV75RCb9gV2LT5zYJtHq dr7llPLL/GeKp35ayIav6YNH9sqAiAUvduHAqfGAl4jpzwP5Fa1xFGnAqBCqg/vi+CCR xZoRLtnS7Fv2xrt+E/j1KVZSUG6DrUMeSXibOfXJASkIPFCmAIiiJX4w3S34aD27mQn9 l4vQ== 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=zBo/6EncMhQE+u7uWHtusHrsjwb/Xhu+WleYCGKp7Zw=; b=kbcSep6cgzGmlc4W2mckaTP7SWpY2miT9/EyqUBneuHNkBlRQjIgTcLfccpXwrkriW lWPYsg8vtLtCmlVdAG3qa07UJchNp26/D/hGKmZmUcYnXfzewXAYLsZ+R7aWbL57IW2m OCzkuFgoEvLfk+7L1EjJJzVzwh/bfh3clcE3U/nhIUXFlWcMbVmRn6Tkw6y66NqmKCEm ZCS6sPy/HWBg3944a0TtDH87ksw1iq+uyqXEg1heZGP89Rz9pIHqV5MKRqLqb+ql712b asxL00gLuJIz6DWYNSZkoH0AEfuCJ65aSFzOp/jV8dlWIfEFWme0+vrxQibLJL+K4+O/ GScQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AxFic8ow; 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 t24si19536666pgv.106.2017.12.25.12.56.17; Mon, 25 Dec 2017 12:56:18 -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=AxFic8ow; 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 S1753410AbdLYU4O (ORCPT + 28 others); Mon, 25 Dec 2017 15:56:14 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:37322 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753356AbdLYU4B (ORCPT ); Mon, 25 Dec 2017 15:56:01 -0500 Received: by mail-wr0-f195.google.com with SMTP id f8so22633689wre.4 for ; Mon, 25 Dec 2017 12:56:00 -0800 (PST) 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=zBo/6EncMhQE+u7uWHtusHrsjwb/Xhu+WleYCGKp7Zw=; b=AxFic8owS7Txq/CoSk0ELyr/8b7BGd7fSyXQ9DlBk9MPsWiEou3HkctdCK6fs9CWfj 9xFtvULW+K1SeXe3CEYaIajB4EIvNe7qVXxxbAL0bv4kdtmVfqu9eUh7UIVpWwuyN+KF Tm2A7kVu731KOVGTPGaB49PbIzknFoAdFSDVc= 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=zBo/6EncMhQE+u7uWHtusHrsjwb/Xhu+WleYCGKp7Zw=; b=lmKP0dfCySXbwoLGVh0YhBH52wFcYtHfabDM1nFBDhotkX8akdK383dprrCdmZCyC/ 2cMZHtfNQ9RtDqAG7EdzEJjeXIJyaEUJ+hWErP3GjOEhkv5iImZ4lemyrC5dWfxB4VLa F8c2s5zCgS36zVx17hpbmjL/2fe2J+bxij4xNm4Gg+KX1apa/3NGxXDck71OX9luBO6e N/O/On15Iun/1yulTuraTxOgOhhQJPcmUezWGy0i3KIW+vQSKi7a1B9hxARvyXMHoWxC wGSBgAeI8Rh86mQHC2IEvvEXad0NRFr3J0BTkfW2lnWQNi0GtwWdquwNJJu1bM1enO99 jZXA== X-Gm-Message-State: AKGB3mJ1nnDyemve2Ae9Nv1itmpfqwizh1C33BtIaSiKBsb8SmiJVdzD N8ucxHMkx4lSR6FvOTQaSwsm+43Pc3g= X-Received: by 10.223.200.133 with SMTP id k5mr12885530wrh.215.1514235359354; Mon, 25 Dec 2017 12:55:59 -0800 (PST) Received: from localhost.localdomain ([160.171.216.245]) by smtp.gmail.com with ESMTPSA id y42sm39552441wrc.96.2017.12.25.12.55.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Dec 2017 12:55:58 -0800 (PST) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Ralf Baechle , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , "David S. Miller" , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v5 8/8] x86/kernel: jump_table: use relative references Date: Mon, 25 Dec 2017 20:54:40 +0000 Message-Id: <20171225205440.14575-9-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171225205440.14575-1-ard.biesheuvel@linaro.org> References: <20171225205440.14575-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Similar to the arm64 case, 64-bit x86 can benefit from using 32-bit relative references rather than 64-bit absolute ones when emitting struct jump_entry instances. Not only does this reduce the memory footprint of the entries themselves by 50%, it also removes the need for carrying relocation metadata on relocatable builds (i.e., for KASLR) which saves a fair chunk of .init space as well (although the savings are not as dramatic as on arm64) Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/jump_label.h | 35 +++++++----- arch/x86/kernel/jump_label.c | 59 ++++++++++++++------ tools/objtool/special.c | 4 +- 3 files changed, 65 insertions(+), 33 deletions(-) -- 2.11.0 diff --git a/arch/x86/include/asm/jump_label.h b/arch/x86/include/asm/jump_label.h index 009ff2699d07..91c01af96907 100644 --- a/arch/x86/include/asm/jump_label.h +++ b/arch/x86/include/asm/jump_label.h @@ -36,8 +36,8 @@ static __always_inline bool arch_static_branch(struct static_key *key, bool bran asm_volatile_goto("1:" ".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t" ".pushsection __jump_table, \"aw\" \n\t" - _ASM_ALIGN "\n\t" - _ASM_PTR "1b, %l[l_yes], %c0 + %c1 \n\t" + ".balign 4\n\t" + ".long 1b - ., %l[l_yes] - ., %c0 + %c1 - .\n\t" ".popsection \n\t" : : "i" (key), "i" (branch) : : l_yes); @@ -52,8 +52,8 @@ static __always_inline bool arch_static_branch_jump(struct static_key *key, bool ".byte 0xe9\n\t .long %l[l_yes] - 2f\n\t" "2:\n\t" ".pushsection __jump_table, \"aw\" \n\t" - _ASM_ALIGN "\n\t" - _ASM_PTR "1b, %l[l_yes], %c0 + %c1 \n\t" + ".balign 4\n\t" + ".long 1b - ., %l[l_yes] - ., %c0 + %c1 - .\n\t" ".popsection \n\t" : : "i" (key), "i" (branch) : : l_yes); @@ -69,19 +69,26 @@ typedef u32 jump_label_t; #endif struct jump_entry { - jump_label_t code; - jump_label_t target; - jump_label_t key; + s32 code; + s32 target; + s32 key; }; static inline jump_label_t jump_entry_code(const struct jump_entry *entry) { - return entry->code; + return (jump_label_t)&entry->code + entry->code; +} + +static inline jump_label_t jump_entry_target(const struct jump_entry *entry) +{ + return (jump_label_t)&entry->target + entry->target; } static inline struct static_key *jump_entry_key(const struct jump_entry *entry) { - return (struct static_key *)((unsigned long)entry->key & ~1UL); + unsigned long key = (unsigned long)&entry->key + entry->key; + + return (struct static_key *)(key & ~1UL); } static inline bool jump_entry_is_branch(const struct jump_entry *entry) @@ -99,7 +106,7 @@ static inline void jump_entry_set_module_init(struct jump_entry *entry) entry->code = 0; } -#define jump_label_swap NULL +void jump_label_swap(void *a, void *b, int size); #else /* __ASSEMBLY__ */ @@ -114,8 +121,8 @@ static inline void jump_entry_set_module_init(struct jump_entry *entry) .byte STATIC_KEY_INIT_NOP .endif .pushsection __jump_table, "aw" - _ASM_ALIGN - _ASM_PTR .Lstatic_jump_\@, \target, \key + .balign 4 + .long .Lstatic_jump_\@ - ., \target - ., \key - . .popsection .endm @@ -130,8 +137,8 @@ static inline void jump_entry_set_module_init(struct jump_entry *entry) .Lstatic_jump_after_\@: .endif .pushsection __jump_table, "aw" - _ASM_ALIGN - _ASM_PTR .Lstatic_jump_\@, \target, \key + 1 + .balign 4 + .long .Lstatic_jump_\@ - ., \target - ., \key - . + 1 .popsection .endm diff --git a/arch/x86/kernel/jump_label.c b/arch/x86/kernel/jump_label.c index e56c95be2808..cc5034b42335 100644 --- a/arch/x86/kernel/jump_label.c +++ b/arch/x86/kernel/jump_label.c @@ -52,22 +52,24 @@ static void __jump_label_transform(struct jump_entry *entry, * Jump label is enabled for the first time. * So we expect a default_nop... */ - if (unlikely(memcmp((void *)entry->code, default_nop, 5) - != 0)) - bug_at((void *)entry->code, __LINE__); + if (unlikely(memcmp((void *)jump_entry_code(entry), + default_nop, 5) != 0)) + bug_at((void *)jump_entry_code(entry), + __LINE__); } else { /* * ...otherwise expect an ideal_nop. Otherwise * something went horribly wrong. */ - if (unlikely(memcmp((void *)entry->code, ideal_nop, 5) - != 0)) - bug_at((void *)entry->code, __LINE__); + if (unlikely(memcmp((void *)jump_entry_code(entry), + ideal_nop, 5) != 0)) + bug_at((void *)jump_entry_code(entry), + __LINE__); } code.jump = 0xe9; - code.offset = entry->target - - (entry->code + JUMP_LABEL_NOP_SIZE); + code.offset = jump_entry_target(entry) - + (jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE); } else { /* * We are disabling this jump label. If it is not what @@ -76,14 +78,18 @@ static void __jump_label_transform(struct jump_entry *entry, * are converting the default nop to the ideal nop. */ if (init) { - if (unlikely(memcmp((void *)entry->code, default_nop, 5) != 0)) - bug_at((void *)entry->code, __LINE__); + if (unlikely(memcmp((void *)jump_entry_code(entry), + default_nop, 5) != 0)) + bug_at((void *)jump_entry_code(entry), + __LINE__); } else { code.jump = 0xe9; - code.offset = entry->target - - (entry->code + JUMP_LABEL_NOP_SIZE); - if (unlikely(memcmp((void *)entry->code, &code, 5) != 0)) - bug_at((void *)entry->code, __LINE__); + code.offset = jump_entry_target(entry) - + (jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE); + if (unlikely(memcmp((void *)jump_entry_code(entry), + &code, 5) != 0)) + bug_at((void *)jump_entry_code(entry), + __LINE__); } memcpy(&code, ideal_nops[NOP_ATOMIC5], JUMP_LABEL_NOP_SIZE); } @@ -97,10 +103,13 @@ static void __jump_label_transform(struct jump_entry *entry, * */ if (poker) - (*poker)((void *)entry->code, &code, JUMP_LABEL_NOP_SIZE); + (*poker)((void *)jump_entry_code(entry), &code, + JUMP_LABEL_NOP_SIZE); else - text_poke_bp((void *)entry->code, &code, JUMP_LABEL_NOP_SIZE, - (void *)entry->code + JUMP_LABEL_NOP_SIZE); + text_poke_bp((void *)jump_entry_code(entry), &code, + JUMP_LABEL_NOP_SIZE, + (void *)jump_entry_code(entry) + + JUMP_LABEL_NOP_SIZE); } void arch_jump_label_transform(struct jump_entry *entry, @@ -140,4 +149,20 @@ __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry, __jump_label_transform(entry, type, text_poke_early, 1); } +void jump_label_swap(void *a, void *b, int size) +{ + long delta = (unsigned long)a - (unsigned long)b; + struct jump_entry *jea = a; + struct jump_entry *jeb = b; + struct jump_entry tmp = *jea; + + jea->code = jeb->code - delta; + jea->target = jeb->target - delta; + jea->key = jeb->key - delta; + + jeb->code = tmp.code + delta; + jeb->target = tmp.target + delta; + jeb->key = tmp.key + delta; +} + #endif diff --git a/tools/objtool/special.c b/tools/objtool/special.c index 84f001d52322..98ae55b39037 100644 --- a/tools/objtool/special.c +++ b/tools/objtool/special.c @@ -30,9 +30,9 @@ #define EX_ORIG_OFFSET 0 #define EX_NEW_OFFSET 4 -#define JUMP_ENTRY_SIZE 24 +#define JUMP_ENTRY_SIZE 12 #define JUMP_ORIG_OFFSET 0 -#define JUMP_NEW_OFFSET 8 +#define JUMP_NEW_OFFSET 4 #define ALT_ENTRY_SIZE 13 #define ALT_ORIG_OFFSET 0