From patchwork Tue Jun 26 18:27:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 140072 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp5572769lji; Tue, 26 Jun 2018 11:28:52 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe+8LRBrkXDRk/Arg/b5ylA/VP+kdOHscUIlaRvulnK3Yhd/hEctSnB1RX4RRMJZLOiFCHV X-Received: by 2002:a62:1fd6:: with SMTP id l83-v6mr2678311pfj.182.1530037732257; Tue, 26 Jun 2018 11:28:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530037732; cv=none; d=google.com; s=arc-20160816; b=wyQ0NJxbid1+2E1vYE4aS70HMPvmbW+VyICHBnEXfQn+whn4WR64YqskqwoMF6/hPi JF8QEKCVPthhN77M0ezi1X/b+ZVPau+9QKI4X4eFNfi4mm1HYVyLwWs6qAAtAwaVCk4T d3KRqOWI5uiP6eoQ2MqUmtQkfWbbrKvI+J/v+BXHal5BjQYTVLO2xqdhNkO8D5MKA1/8 sTU5WgGx8zsK/7BAWagvE5tAdU7cYzyD3kTxxcKt/egIXyo2RSgJNaOFUMYUjm8O0XJn PGWa4+WV/T/HiRfNrnFKLzV9c+HfzHMSd8b4CbV0QZwVbEdpoZByDgZF1cUX4AKQH8VB khGQ== 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=7Pl3hl9dlr/wJP2mnaTAtaT54uoquJiV2t4p4cdOAyg=; b=pOR/QRycSi/P69iITmGnyeZbCdkpjQpZyMj1/guPdtfZ5M0+5r5eMqFJIt1Vl3KxFq bJ0+uOQVLqMcJMLQMFg1er9z5A+mUobXJZ+xFQqEKVO4YRpcA9qjAEIPtT7Y/ffYQBSZ G52WIhvPzii5/A2SmumrALXKWFcS9pMavEqH4nSNgjEfy3kcairwWciKdMG4SHRV3NDH by9uk3MWBk8xP4Nnujok2u0mFOCmzh6bJmbj04eI1Sjv7vGyMf2ekHd3f6K3O0a46v/G URxVJLwMzK7KiYofkLrIoF+JhTGq1Adm+XA+6vzpxLz9OqPMWJb9O0N/L2ovCg/4rq8V ulvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Q2SuMHmp; 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 p5-v6si2103152pfb.171.2018.06.26.11.28.51; Tue, 26 Jun 2018 11:28:52 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Q2SuMHmp; 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 S1752913AbeFZS2r (ORCPT + 31 others); Tue, 26 Jun 2018 14:28:47 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:35862 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751708AbeFZS2o (ORCPT ); Tue, 26 Jun 2018 14:28:44 -0400 Received: by mail-wr0-f196.google.com with SMTP id f16-v6so18228090wrm.3 for ; Tue, 26 Jun 2018 11:28:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7Pl3hl9dlr/wJP2mnaTAtaT54uoquJiV2t4p4cdOAyg=; b=Q2SuMHmpWc0SFvLy6xXOjfHRMrRsAwFPYNhXwbVsiRG/mDtWiE26DFzdPw657vIUoe CI/gMks+q6ug0i6zfBiWFiiMjsSnnDHmWEh2IFpjvpHAMiLbAOPKwUpMP6oy5HYP6ohH NLRjX3Btm+gWIrQ3cUm9F7iD7Form2lxGBOCI= 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=7Pl3hl9dlr/wJP2mnaTAtaT54uoquJiV2t4p4cdOAyg=; b=g89OJer3EUj+zJ9F/kFw5OaQGilZ/vib8QsWpf//gX2efOOca+kLUYLFyxXj5RpJE0 aWjbr0JBDAhWO4J+KF6ShJrWyBh9MpbZCdFTAlJ6JQ1w1pkzuJcBJ8wsNvh6iHoufQVw gyV10aWCq3c4i+sWWEMJgcCubYcQ+9wJvi/e2DxJSw03vLdy2DMVVILEeJBY3RVZnKb4 VNEMFBQ++jxG9dekaFKMJ7PNf9EZ7JDxR4OpfEnmcGl6Uz3+9l321z4qTAjKhGtinzRx egxfho93qHy0pQ5FvkrJ9H9Xmaab0lqN4U35e6HES3z+ciaq5TmcgBBod0xcXapGHa4V CQwQ== X-Gm-Message-State: APt69E2CKPIH4eMxB7M7ba+oQwQEJsxDVaAjqx2A/IfWkFGOYM5Wvs5i lRThnNzMqJo9O0PCnZUn7tXUCjJcYag= X-Received: by 2002:adf:afd3:: with SMTP id y19-v6mr2601535wrd.176.1530037722557; Tue, 26 Jun 2018 11:28:42 -0700 (PDT) Received: from rev02.home ([2a01:cb1d:112:6f00:d037:e907:f0a2:a5ea]) by smtp.gmail.com with ESMTPSA id s17-v6sm1707664wmc.34.2018.06.26.11.28.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Jun 2018 11:28:41 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Arnd Bergmann , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: [PATCH v9 1/6] arch: enable relative relocations for arm64, power and x86 Date: Tue, 26 Jun 2018 20:27:56 +0200 Message-Id: <20180626182802.19932-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180626182802.19932-1-ard.biesheuvel@linaro.org> References: <20180626182802.19932-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: Thomas Gleixner Cc: Ingo Molnar Cc: x86@kernel.org Signed-off-by: Ard Biesheuvel --- arch/Kconfig | 10 ++++++++++ arch/arm64/Kconfig | 1 + arch/powerpc/Kconfig | 1 + arch/x86/Kconfig | 1 + 4 files changed, 13 insertions(+) -- 2.11.0 diff --git a/arch/Kconfig b/arch/Kconfig index 1aa59063f1fd..2b8b70820002 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -971,4 +971,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 42c090cf0292..1940c6405d04 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -95,6 +95,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_THREAD_STRUCT_WHITELIST select HAVE_ARCH_TRACEHOOK diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index 9f2b75fe2c2d..e4fe19789b8b 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 HAVE_CBPF_JIT if !PPC64 diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index f1dbb4ee19d7..e10a3542db7e 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -123,6 +123,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_THREAD_STRUCT_WHITELIST select HAVE_ARCH_TRACEHOOK From patchwork Tue Jun 26 18:27:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 140077 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp5574408lji; Tue, 26 Jun 2018 11:30:30 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKScWIx3957ZTUn4/n/zEvykloNbqp7hmk8tyM+lpgLoSmMv0cbNFMmbcrejWMQ+/NDd+QL X-Received: by 2002:a65:6455:: with SMTP id s21-v6mr2270577pgv.394.1530037830813; Tue, 26 Jun 2018 11:30:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530037830; cv=none; d=google.com; s=arc-20160816; b=K99GQAc1g5uMfG83MKKltAa4wI6aDwXMIqvBHfqLgaYq1zStFsa/T5xm2DnbXtcl/x +nHuKTDFgBvwbYbLFjJ7J6Qh3T4H48Dv7WN+dDrWYEURdiWoxQg698NlykhFx0MSH+vI /x9syZYgPLj8gpRfsiwJVO77Aqpnd3H2ZTOHQZKY31oYUhuUuLm4aiq/vSwvOrx5uCc3 s0hOpQApnslayKcTymDWAS2bXGwqTVLnNHpIYmnFjnsPJiHkpPsC7llIk5DCDQKf2PBs 96KTW32Yq3Uxn/B1+s/V7dX+Yb+px1LwsPMwXHMU0LFdZ06Be78OIPGr/K2Ih7f/sJS5 sY0A== 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=0rtlaTwu4CCqZ4xjhd2DpZMexFkKmZwXAJxYNg8GKrI=; b=X9fGz/phbald/EJ6HbobWbLAT0kWLUTlwKM/NdXbwdkwWFobhSkCXc5Wck9sXu2S6C a6xJgMZTzh4pgMUL7bdKUu6Dxr2sKmu6qVDIA1iB4KSfxMCruIhUan5/f5dNee5iBZqb GvapjiSnUTPcpdu0jvLbBZmwesTkAQPVm8PCmgp9Z+mQUmUzV5w9avNVQtRja2BahygJ Fq87VRaI+7ZBgYF58+5Wc3Zg1m1edAWicTzgno+dmqkmuAakwyRlGfmQfhot2pSomT7w mci5C1zJ7RvAvr3OWPcRFoFqO583Ig7LLi3qcEljjqt3v5sjTVLzMt6LBnUiFVKcbRy8 rH8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kWQofeSW; 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 l1-v6si1890773pgb.187.2018.06.26.11.30.30; Tue, 26 Jun 2018 11:30:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kWQofeSW; 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 S1754908AbeFZSa0 (ORCPT + 31 others); Tue, 26 Jun 2018 14:30:26 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:47062 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751121AbeFZS2q (ORCPT ); Tue, 26 Jun 2018 14:28:46 -0400 Received: by mail-wr0-f194.google.com with SMTP id l14-v6so13143197wrq.13 for ; Tue, 26 Jun 2018 11:28:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0rtlaTwu4CCqZ4xjhd2DpZMexFkKmZwXAJxYNg8GKrI=; b=kWQofeSWgXoFjz/j8asPkjzideKiaqYlr9d9vbu/r2+CWpyREDbBaoDmBUxiu0peFH HPq9AnjbD8tgNgl/vBzBFgNM5iwiBgZc2u4TDt0NhyvsxqwUZQNyAQ7opJ3P/nUOKwvH PTdbLXYvfUfdjZZ95APglwOGZ39x0iuVko2X4= 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=0rtlaTwu4CCqZ4xjhd2DpZMexFkKmZwXAJxYNg8GKrI=; b=PItl72HI+6VlGSrsXcHZbvp/U2BtT9zE3wP5ZRkgf368rq7VvOXap6XVVm8IJrrLSk 8w9SmDw6gOR+FSjhA8CgTF2a1JVO8yaN9j9RZAEIApYXsEFxoz7UvDb/qMARw5TRARzO 4Ff8Ii8Rr+hcW/9SJEk06kLKoj37rjeCb1ZA+5WXtwjykgB+SJB1iCx2goXL5gkR/5GQ kbI+WV0em52SgSW41WnJF5q1rCtUNMu1UXwH2it4t0gg+cARvrv666AcrMxnI/2cypvS ZJWxItKNWieCo5lxEINktgde0ORiYZ5sdQtbtwDxoOhmM7rX88FaQfPBU5/BnAh0BwFd XaEg== X-Gm-Message-State: APt69E1QGin5pzoTMf1jpEvBIvNIuOBII6lEK8d7kBSvqT8WdHFO4HHe q9AaIEpzu8SW6SwIydUHcuKzutw6K9I= X-Received: by 2002:adf:ebc3:: with SMTP id v3-v6mr2414460wrn.33.1530037724454; Tue, 26 Jun 2018 11:28:44 -0700 (PDT) Received: from rev02.home ([2a01:cb1d:112:6f00:d037:e907:f0a2:a5ea]) by smtp.gmail.com with ESMTPSA id s17-v6sm1707664wmc.34.2018.06.26.11.28.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Jun 2018 11:28:43 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Arnd Bergmann , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: [PATCH v9 2/6] module: allow symbol exports to be disabled Date: Tue, 26 Jun 2018 20:27:57 +0200 Message-Id: <20180626182802.19932-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180626182802.19932-1-ard.biesheuvel@linaro.org> References: <20180626182802.19932-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 allow existing C code to be incorporated into the decompressor or the UEFI stub, introduce a CPP macro that turns all EXPORT_SYMBOL_xxx declarations into nops, and #define it in places where such exports are undesirable. Note that this gets rid of a rather dodgy redefine of linux/export.h's header guard. Acked-by: Nicolas Pitre Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/kaslr.c | 5 +---- drivers/firmware/efi/libstub/Makefile | 3 ++- include/linux/export.h | 11 ++++++++++- 3 files changed, 13 insertions(+), 6 deletions(-) -- 2.11.0 diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index b87a7582853d..ed7a123bba42 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -23,11 +23,8 @@ * _ctype[] in lib/ctype.c is needed by isspace() of linux/ctype.h. * While both lib/ctype.c and lib/cmdline.c will bring EXPORT_SYMBOL * which is meaningless and will cause compiling error in some cases. - * So do not include linux/export.h and define EXPORT_SYMBOL(sym) - * as empty. */ -#define _LINUX_EXPORT_H -#define EXPORT_SYMBOL(sym) +#define __DISABLE_EXPORTS #include "misc.h" #include "error.h" diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile index a34e9290a699..0d0d3483241c 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -20,7 +20,8 @@ cflags-$(CONFIG_EFI_ARMSTUB) += -I$(srctree)/scripts/dtc/libfdt KBUILD_CFLAGS := $(cflags-y) -DDISABLE_BRANCH_PROFILING \ -D__NO_FORTIFY \ $(call cc-option,-ffreestanding) \ - $(call cc-option,-fno-stack-protector) + $(call cc-option,-fno-stack-protector) \ + -D__DISABLE_EXPORTS GCOV_PROFILE := n KASAN_SANITIZE := n diff --git a/include/linux/export.h b/include/linux/export.h index b768d6dd3c90..ea7df303d68d 100644 --- a/include/linux/export.h +++ b/include/linux/export.h @@ -66,7 +66,16 @@ extern struct module __this_module; __attribute__((section("___ksymtab" sec "+" #sym), used)) \ = { (unsigned long)&sym, __kstrtab_##sym } -#if defined(__KSYM_DEPS__) +#if defined(__DISABLE_EXPORTS) + +/* + * Allow symbol exports to be disabled completely so that C code may + * be reused in other execution contexts such as the UEFI stub or the + * decompressor. + */ +#define __EXPORT_SYMBOL(sym, sec) + +#elif defined(__KSYM_DEPS__) /* * For fine grained build dependencies, we want to tell the build system From patchwork Tue Jun 26 18:27:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 140073 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp5572840lji; Tue, 26 Jun 2018 11:28:56 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKQ0oIc+M46Mhl8o9fK0YHpyTwiQXPBaWsU0xpkJjUQcMOaOArnYAFVqzGUEgcj3Pn5Simy X-Received: by 2002:a17:902:b946:: with SMTP id h6-v6mr2673532pls.1.1530037736201; Tue, 26 Jun 2018 11:28:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530037736; cv=none; d=google.com; s=arc-20160816; b=NQUpVIrTEtjt5A7ENJm+kj+iACBHGbUMA1BCeJ7JdkhyKxbqjEejnEZzu3oOe/wvQT gR1Sc0x2QRZDHQQ1urNO8bED5K/3ocaW+4fvB/qUx7Eeta1inLAdCA8knS3VVKJ463w5 r8rShCvGy+ep8A0XooQjuUDGlFxULpNKjsmOaxo/OUgFM3MXYhk6oxDL9bYx3bcqIXsV WoQU8/zlu+XAshcF1qZXU81GnRxAfe1Q52lOpVKFTXRcKVpGHbQ6zCHtms1NDZJ9IUpS 9Z2RQu/zLsQuV1eM67+f520P+ilkqAoxeEWj1YIq8gZxyCsLOyF5pm87bewAKUhmQnIi o41A== 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=o8p9Iq0iPGdylp8Oth6Hq3Y5MrX11H4f3D1X+1TovYQ=; b=SKAPGMCzcRgbZi/9ZHjqWj9X/Ve/5cIytdUjHptoSIOHnL9p5UPxhCFtcsysP/AMKO mYXEUhqn+/AzmED3JTPsUIb+hiBIit54m6r8QF/n0bMbDuum41LnbZUNUBaXwI+B8Don R6RmPKRRBjRBypBMK3SxABQ0mvvB82dOMOPFuoYjGLUjTMMK6c7m5tH8CvdgS2sTeiLJ kcFOMrVAU+OwS/AE9OIKgbu2vM9r8C0iXbsJ0FnFBgGt+CkcniDYCOL/ms5qcgLwJuz/ lHoqndftNLSL2/1WS08S/3OeKwgrJ6RvGxovZJu80pGuwdF6WeQ2H42MbSbOYisypUCP w4bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CmfmsAA9; 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 m13-v6si1843539pgs.690.2018.06.26.11.28.55; Tue, 26 Jun 2018 11:28:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CmfmsAA9; 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 S1754128AbeFZS2w (ORCPT + 31 others); Tue, 26 Jun 2018 14:28:52 -0400 Received: from mail-wr0-f195.google.com ([209.85.128.195]:33697 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752886AbeFZS2r (ORCPT ); Tue, 26 Jun 2018 14:28:47 -0400 Received: by mail-wr0-f195.google.com with SMTP id k16-v6so18233904wro.0 for ; Tue, 26 Jun 2018 11:28:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=o8p9Iq0iPGdylp8Oth6Hq3Y5MrX11H4f3D1X+1TovYQ=; b=CmfmsAA9n5jGVS84/6q6+bWrR4dhFHwQR+xZ4QFiV0Pdh6A6snCMHLbydTlK4kaij8 ERS+hDNia2+xE0xv5ZZi1r4qFLJsBdrxfQVyL/TbjuLUv7CvaB+15uHzVYBESwYH8AXf RvdJipig0rHagy2VQx8eZskqbrkzMg5weH7eo= 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=o8p9Iq0iPGdylp8Oth6Hq3Y5MrX11H4f3D1X+1TovYQ=; b=OKgdByQE3rXT/kVEKaai7MrrxXE1utN335euJYoWsPAeFg1grx0ZBcfbxr8h7pjeRG /SMcH3MbFCxIR4LCBZE8iIWcBlYMzTv8/4hy0NSAIXxFqM8DBcplJjTY3KxsehS1Zezd 4i/LILSlXemWvzKFdWPyWT6dMVgq3F2DsMTmDMwtecKrSJNftr+RlzZD/eQ12430amSf /FIOqGEI8Z+8OrH4hoVjFvcwZkcnP8rWBDEL4WZFbGeXizHg+zsp9frBI8PXBMtFDotd 2T2VcMF4q989kEbMFNQrUNGfU6t6w07EaD9qcZnM7V4U3QVOTwqfSEyBWQJ5czEN74FU MgEA== X-Gm-Message-State: APt69E2Geoi0KGuH9MtA2K7s49sHwFXJcCMIcxFbYcIY+GPa9RPR1WWE E0fJn9ItVaDxYqJjq143Ujr+/Lf+bs8= X-Received: by 2002:adf:9063:: with SMTP id h90-v6mr2495896wrh.147.1530037726264; Tue, 26 Jun 2018 11:28:46 -0700 (PDT) Received: from rev02.home ([2a01:cb1d:112:6f00:d037:e907:f0a2:a5ea]) by smtp.gmail.com with ESMTPSA id s17-v6sm1707664wmc.34.2018.06.26.11.28.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Jun 2018 11:28:45 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Arnd Bergmann , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, Ingo Molnar Subject: [PATCH v9 3/6] module: use relative references for __ksymtab entries Date: Tue, 26 Jun 2018 20:27:58 +0200 Message-Id: <20180626182802.19932-4-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180626182802.19932-1-ard.biesheuvel@linaro.org> References: <20180626182802.19932-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 | 19 ++++++++ include/linux/export.h | 46 +++++++++++++++----- kernel/module.c | 32 +++++++++++--- 6 files changed, 91 insertions(+), 24 deletions(-) -- 2.11.0 diff --git a/arch/x86/include/asm/Kbuild b/arch/x86/include/asm/Kbuild index de690c2d2e33..a0ab9ab61c75 100644 --- a/arch/x86/include/asm/Kbuild +++ b/arch/x86/include/asm/Kbuild @@ -8,5 +8,6 @@ generated-y += xen-hypercalls.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 68efb950a918..4d73e6e3c66c 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 @@ -19,6 +17,16 @@ #define KCRC_ALIGN 4 #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 42506e4d1f53..2d9c63f41031 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -280,6 +280,25 @@ unsigned long read_word_at_a_time(const void *addr) #endif /* __KERNEL__ */ +/* + * 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 + * visible to the compiler. + */ +#define __ADDRESSABLE(sym) \ + static void * __attribute__((section(".discard"), used)) \ + __PASTE(__addressable_##sym, __LINE__) = (void *)&sym; + +/** + * offset_to_ptr - convert a relative memory offset to an absolute pointer + * @off: the address of the 32-bit offset value + */ +static inline void *offset_to_ptr(const int *off) +{ + return (void *)((unsigned long)off + *off); +} + #endif /* __ASSEMBLY__ */ #ifndef __optimize diff --git a/include/linux/export.h b/include/linux/export.h index ea7df303d68d..ae072bc5aacf 100644 --- a/include/linux/export.h +++ b/include/linux/export.h @@ -18,12 +18,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) @@ -54,17 +48,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" \ + "__ksymtab_" #sym ": \n" \ + " .long " #sym "- . \n" \ + " .long __kstrtab_" #sym "- . \n" \ + " .previous \n") + +struct kernel_symbol { + int value_offset; + 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))) \ = #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(__DISABLE_EXPORTS) diff --git a/kernel/module.c b/kernel/module.c index f475f30eed8c..7cb82e0fcac0 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -547,12 +547,30 @@ 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)offset_to_ptr(&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 offset_to_ptr(&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, @@ -2192,7 +2210,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); @@ -2222,10 +2240,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; } } @@ -2274,7 +2294,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; } @@ -2534,7 +2554,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 Tue Jun 26 18:27:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 140076 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp5573613lji; Tue, 26 Jun 2018 11:29:44 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLFJAAHMvbcW/me8z/lZm5+RyL32pRvfqgFJGsAEZyCVa4yX8gaPjpZdydYv+mkiGXqef67 X-Received: by 2002:a65:6409:: with SMTP id a9-v6mr2381582pgv.154.1530037784258; Tue, 26 Jun 2018 11:29:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530037784; cv=none; d=google.com; s=arc-20160816; b=JM+TYjDgR09k3ootJTOR26sQ4Wa9QgF7QHbI1DqhvvZDspFXlll03UnPp/XFLwKN6l IKr5XUCmHVreu6Ahvh3n+2XeqBOhooxC8SfaX77eOKlwTsceuHGJ5FHUop1K7e2vDsoQ XiWCzOg4iow5r32RjNW6Yu+HB1Fmq43HcnBHbeTWipcyKulct/6cuBpmhlNNxMAfYalX 20aMrV8Ue4kCjdJjAsPZP3tBM8y2mgBUf2SPVzYljsblfsOw2k8N4CogyeEYCrJQKsP0 E7pS9gV/ZFnFoWl9vig06dAHgOKyirczZeABxvrzzLlnDfFDwpuucOVxHqJRyqnBlBP4 IuGg== 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=e3jnCZ1uFoNVuf5Qep6Bc6KNPlh/C1aJMc4q0hY5MUE=; b=JBajE1fR9Xe1490zSh00QDG6XjQvT3AhmNibpXL45WdMpZ1zR64W90zUnZmlgBwqvq KWoLNOg7AsJxOQdN5YfXgRltEeTH45WcPhOTOQb0Jpv/+Ga5JZa7orwW768WH8UPfyFF NFrUaHqdrbmx1snrEX/qIy5VagWwIQkKFRBLKVzXLziz8cwQ60M683OZKmPOdPegQoRE AyhNvg7xHMKWYXqL8u5mJhL8Sqo2tzhgw/gvyBqDJ9ouZFd3E62Q8GAbY7hMj2sg0WMz BUtb9v9z6lMNej7NKYFz/GYS9yd8I4kNhE86ADmVVwm9CgEyLv1+3MwPZRdz+a5G0/nz 0nXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LK5muTqU; 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 n68-v6si2138142pfb.152.2018.06.26.11.29.43; Tue, 26 Jun 2018 11:29:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LK5muTqU; 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 S1754572AbeFZS3n (ORCPT + 31 others); Tue, 26 Jun 2018 14:29:43 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:47067 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750962AbeFZS2t (ORCPT ); Tue, 26 Jun 2018 14:28:49 -0400 Received: by mail-wr0-f193.google.com with SMTP id l14-v6so13143353wrq.13 for ; Tue, 26 Jun 2018 11:28:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=e3jnCZ1uFoNVuf5Qep6Bc6KNPlh/C1aJMc4q0hY5MUE=; b=LK5muTqU9KKzjP5cU+TC57wGqc+oEWRwIa/cBtdPV5ZGnhL8/xAvrfA/txCA+lbIMl z48mNDc5Sq5oiaKoviBG2gsH1ySZhDXd183Ff1qsFJauPtQzxEtCJbEobv2nYIUlWL+w d1YddENHiJLOVApc+XEX7k0swJFIVgx/2HjyA= 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=e3jnCZ1uFoNVuf5Qep6Bc6KNPlh/C1aJMc4q0hY5MUE=; b=NDfyE8sU1AxXf+CC05P8xMU2pZ4GOEitF1lXyQiR8JeEv1craiDMMbjoJtnIrfF/LH F0NvOHrkVn9pqRvm49ON0z16XAWIWFSKKgbcs65N0/anWbkPKPLtT9XE6h8tqetDPiFA zgspRUMOxaBolOW25kNZgT95rY6Xb8mtw+1RKWwZ6PTG0sC3uverw1AEZ8/kJzyqd9cj Cb8lSC9Ghh96lgMlBTEGlVmvqIfpAly1GH3SPoY2u91kZohbdVVkWa+vc5YqJkRO5Umi WJ8A1rqxeF15M1VZ2IMB5qXv7g859uESwLkxl7KP3dE81rIu60P6BBMi2J3FZrSzzwMg +qgA== X-Gm-Message-State: APt69E04dNc0CZs/Ofj6jNqTKsv/b7hhnbQc/6s1bibB1ushgHzwL2AO MWz/AonDKPeVuVztWoLqwwpFtpKtaJM= X-Received: by 2002:adf:9246:: with SMTP id 64-v6mr2481260wrj.109.1530037727883; Tue, 26 Jun 2018 11:28:47 -0700 (PDT) Received: from rev02.home ([2a01:cb1d:112:6f00:d037:e907:f0a2:a5ea]) by smtp.gmail.com with ESMTPSA id s17-v6sm1707664wmc.34.2018.06.26.11.28.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Jun 2018 11:28:47 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Arnd Bergmann , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: [PATCH v9 4/6] init: allow initcall tables to be emitted using relative references Date: Tue, 26 Jun 2018 20:27:59 +0200 Message-Id: <20180626182802.19932-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180626182802.19932-1-ard.biesheuvel@linaro.org> References: <20180626182802.19932-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 | 16 +++---- security/security.c | 17 ++++---- 4 files changed, 68 insertions(+), 41 deletions(-) -- 2.11.0 Acked-by: James Morris Acked-by: Sergey Senozhatsky Acked-by: Petr Mladek diff --git a/include/linux/init.h b/include/linux/init.h index bc27cf03c41e..2538d176dd1f 100644 --- a/include/linux/init.h +++ b/include/linux/init.h @@ -116,8 +116,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 int initcall_entry_t; + +static inline initcall_t initcall_from_entry(initcall_entry_t *entry) +{ + return offset_to_ptr(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); @@ -167,9 +183,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 " #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. @@ -208,13 +235,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 3b4ada11ed52..e59a01f163d6 100644 --- a/init/main.c +++ b/init/main.c @@ -901,18 +901,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, @@ -938,7 +938,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], @@ -949,7 +949,7 @@ static void __init do_initcall_level(int level) trace_initcall_level(initcall_level_names[level]); 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) @@ -980,11 +980,11 @@ static void __init do_basic_setup(void) static void __init do_pre_smp_initcalls(void) { - initcall_t *fn; + initcall_entry_t *fn; trace_initcall_level("early"); 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 247808333ba4..688a27b0888c 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2772,7 +2772,8 @@ EXPORT_SYMBOL(unregister_console); void __init console_init(void) { int ret; - initcall_t *call; + initcall_t call; + initcall_entry_t *ce; /* Setup the default TTY line discipline. */ n_tty_init(); @@ -2781,13 +2782,14 @@ void __init console_init(void) * set up the console device so that later boot sequences can * inform about problems etc.. */ - call = __con_initcall_start; + ce = __con_initcall_start; trace_initcall_level("console"); - while (call < __con_initcall_end) { - trace_initcall_start((*call)); - ret = (*call)(); - trace_initcall_finish((*call), ret); - call++; + while (ce < __con_initcall_end) { + call = initcall_from_entry(ce); + trace_initcall_start(call); + ret = call(); + trace_initcall_finish(call, ret); + ce++; } } diff --git a/security/security.c b/security/security.c index 68f46d849abe..1e7b1486d82a 100644 --- a/security/security.c +++ b/security/security.c @@ -48,14 +48,17 @@ static __initdata char chosen_lsm[SECURITY_NAME_MAX + 1] = static void __init do_security_initcalls(void) { int ret; - initcall_t *call; - call = __security_initcall_start; + initcall_t call; + initcall_entry_t *ce; + + ce = __security_initcall_start; trace_initcall_level("security"); - while (call < __security_initcall_end) { - trace_initcall_start((*call)); - ret = (*call) (); - trace_initcall_finish((*call), ret); - call++; + while (ce < __security_initcall_end) { + call = initcall_from_entry(ce); + trace_initcall_start(call); + ret = call(); + trace_initcall_finish(call, ret); + ce++; } } From patchwork Tue Jun 26 18:28:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 140075 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp5573165lji; Tue, 26 Jun 2018 11:29:16 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIcy4KjQcLh2A/ie+kDiMt+MKAp45L7iHXbDW9Owt48oGJCxAXqaaCPu91opEDvdVbYLX9p X-Received: by 2002:a17:902:650c:: with SMTP id b12-v6mr2747900plk.31.1530037756535; Tue, 26 Jun 2018 11:29:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530037756; cv=none; d=google.com; s=arc-20160816; b=ZgP0CX/lCqx2fzJmWNQK5guAPOmeHD3naywRrvqhI+eUwWSmg1sQA/9QMF3tPOisq5 b1uBWv1sGCjXJLaUXRq6l0Y24YmyRhzVB1Z7x5tBzbH1gYmjZJAH4MYN5pU8w/6/C9ms tAEvSNF71UrP1ysAUU+/Y/pJVIf/kiaT2M4j10k87dKhC39QSGelBa6PotGaud40eLAt 6mUHj5DxfH1bwtS1TgHFeigem+YDkPb6wqcXmPZ8LEpI962D+Nr+CAMSnsn152RpL3Vj mUPnnj3rBLvcg8p/IKWkXLjXrlllRwHYi6MKEpxXkOgh2UB4hLpe1AP70gq9AM1a5jtk FyFQ== 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=7rtDqZRn08p6IrU2qqSvkpaXuaNX+BLXDAxvrNGfhOo=; b=K71aV8KRYW4O+5IxBIGfRitdRDIh/4Q4hbyMg2FwLnYlHU5Q2ulsF1jNVdU96WyG2I PP1BV5Y32UMkwk5JOMLX0leAKyAYFAWX+GfZe+Lobkc1zZQYf5ZADVGx47w7nFD7vEtb BWrhbTW1jjSwkUSklcf06ypI89CBNFtWZTLR1KFuJdrnHAIBvwJ98GM2vrIjgOGqJ5g/ tpAFWlK8Rb3kh1OLfONQhDOmUziEVtssKdK1mdhf67FKeLbavVAyKnrOf4Ohz5NCoOSM qxcQo5I+BHVOd+CkdUEVcS8bYccY96f1I6o4boEVwk16S1QfUswHtZLU4Z5SPn5bOpni /ipg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EjRpK3hS; 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 d11-v6si1833023pgn.563.2018.06.26.11.29.16; Tue, 26 Jun 2018 11:29:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EjRpK3hS; 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 S934063AbeFZS3O (ORCPT + 31 others); Tue, 26 Jun 2018 14:29:14 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:43619 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752786AbeFZS2v (ORCPT ); Tue, 26 Jun 2018 14:28:51 -0400 Received: by mail-wr0-f193.google.com with SMTP id c5-v6so7056980wrs.10 for ; Tue, 26 Jun 2018 11:28:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7rtDqZRn08p6IrU2qqSvkpaXuaNX+BLXDAxvrNGfhOo=; b=EjRpK3hSgSPZ2IbhYyfsOubGNygDOsFKwCM0O9hlft6LOomb81jMgHZLG1WCI+dNvN vwGB/iCq21birz/AHBNXyBGsrgQ3lbnnHrZNJiAiEeBSCceRQxS55arQGPVFuWr7/Pbl NbboOjYq/dzf210TIurgGShTofSDL0QKmSEho= 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=7rtDqZRn08p6IrU2qqSvkpaXuaNX+BLXDAxvrNGfhOo=; b=am1TWE3DgI1tiLy7Bh18CSDaN8IIFJU9lBdnSks4lh2R9B1TnMkDn7nv9DX4gOxN4T Kni/Y52Jki6uZCF1hKmYwpfMTWSHVzRJL42JQBBxAHWDz5bXQiWSaTpCeZJKuy7HZAme V2XoyT9+Ruuf3gTPCeJO8Kv5Nl9E5s7/+TV0Dzrx35NpYxD8r5zQXLkZEfCFhECWVD29 Ddb7bf0PKUoJY+vRee3Fw8KF6ictIJCwbqHfKARt0Sip/HWuFvwke4/t5wAS5IUOKu2H iokZ45klytsFDKH4JLSBLYNJh47ceysHLNWqSzlWnGzroYW6VzgeHf5/0qvwYXGmRIcZ EyjA== X-Gm-Message-State: APt69E0gFlVc8yqpWGuQZDJtsWViSgyp2pBBIh7S6cM/ceCIG9mVAqY7 WOznHZIYUl/WHhuaq+RdmL72iEUCe/k= X-Received: by 2002:adf:8425:: with SMTP id 34-v6mr2402558wrf.72.1530037729483; Tue, 26 Jun 2018 11:28:49 -0700 (PDT) Received: from rev02.home ([2a01:cb1d:112:6f00:d037:e907:f0a2:a5ea]) by smtp.gmail.com with ESMTPSA id s17-v6sm1707664wmc.34.2018.06.26.11.28.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Jun 2018 11:28:48 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Arnd Bergmann , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: [PATCH v9 5/6] PCI: Add support for relative addressing in quirk tables Date: Tue, 26 Jun 2018 20:28:00 +0200 Message-Id: <20180626182802.19932-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180626182802.19932-1-ard.biesheuvel@linaro.org> References: <20180626182802.19932-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 | 12 +++++++++--- include/linux/pci.h | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) -- 2.11.0 diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c index f439de848658..0ba4e446e5db 100644 --- a/drivers/pci/quirks.c +++ b/drivers/pci/quirks.c @@ -64,9 +64,15 @@ 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 = offset_to_ptr(&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 340029b2fb38..51baa3ab5195 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1795,7 +1795,11 @@ struct pci_fixup { u16 device; /* Or PCI_ANY_ID */ u32 class; /* Or PCI_ANY_ID */ unsigned int class_shift; /* should be 0, 8, 16 */ +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + int hook_offset; +#else void (*hook)(struct pci_dev *dev); +#endif }; enum pci_fixup_pass { @@ -1809,12 +1813,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 " #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 Tue Jun 26 18:28:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 140074 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp5572900lji; Tue, 26 Jun 2018 11:29:00 -0700 (PDT) X-Google-Smtp-Source: AAOMgpclqnzNuwkQNsSnU4UzVrqBxDcYkM3PR3rTZ4h7W2vm/IADbFm2AYNB36Y+bJ2ILKguqn3n X-Received: by 2002:aa7:83d1:: with SMTP id j17-v6mr2666799pfn.236.1530037740546; Tue, 26 Jun 2018 11:29:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530037740; cv=none; d=google.com; s=arc-20160816; b=HdACt5i3POLdgU+XvRlqrKRBR14iKm+aESq9wB4UmTBUNfmDCWq7BbqKOn+7IZj2JY +RjTISP2T29XQS5+/kNgMso809ABHqqzVNOrYwq6VeXxQsD2+gpsii7tdWqE/HtUWkTQ SViPUSNAoNMoFSEz37GnumTk6IRt7NJR0M1pV1txZtfBLRoafaGT2Bysbzp+EvktiIGn GCvQram6VTGJ4JjQVj6tE8iC3ij4pgFyVnbSVAnnp42maFKVhqVGhyMUtn6nO6Hy9meO XGAkF11YfRuuLfKYqZmXP7kBDTPrglMALKe0ikyPLSUaUOWaTXYVtjh/hA/Eul1jqOL/ ALUw== 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=6EXfacxtBs2hWqAKIliQJWWXNn3sfRJsVjsG6USvlLk=; b=TnU6lnORGLAgXXDHlXNJ3Wm5pWWoU5AqT+nZoJ5zyVbpFEXtdMi3sATHSb/vF1CQ/L KzWOGPb4bgOc+wdvcZDmooX8qWtnjF71Gi1Y4NiQErd7EzLig+u+p6/99/1OlP/OCssG Ka14Kuwx2Y4OPK0BgQxHyJi0hN1cjyErxBlxSxiDJkEL9EI+b8SyNCTuiE7UJ5txeNWD RGg3Y4MZiLokis4PkipuRAIV87afLBmCKDmRderGCvU4lNvzWKuuuAdbEtBgvA66jezw VVcYwEVZTTo+Dk5QoIoxTQtvSQCg61smUwXKeVyjKtMruSYE8FFAASODT8HMxIOJ590N +zlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bQrVGqib; 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 34-v6si2012071plc.346.2018.06.26.11.29.00; Tue, 26 Jun 2018 11:29:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bQrVGqib; 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 S1754359AbeFZS26 (ORCPT + 31 others); Tue, 26 Jun 2018 14:28:58 -0400 Received: from mail-wr0-f195.google.com ([209.85.128.195]:41103 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753931AbeFZS2w (ORCPT ); Tue, 26 Jun 2018 14:28:52 -0400 Received: by mail-wr0-f195.google.com with SMTP id h10-v6so18200320wrq.8 for ; Tue, 26 Jun 2018 11:28:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6EXfacxtBs2hWqAKIliQJWWXNn3sfRJsVjsG6USvlLk=; b=bQrVGqibSpzm0lqYZfOm5M3p2Ku6H+gTjwAeyMNzYNZM2wBegZD3H2nSkXaYFw3GzF RALNyXp+ukRiRhRzRiQ7UULLBWgsF352CMLsqolMnKbhtIO8zu0YOG250DacHndwhwof Wk4yk38W3fkpJN+GqJixfAxrbDiIKD8xFX9w8= 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=6EXfacxtBs2hWqAKIliQJWWXNn3sfRJsVjsG6USvlLk=; b=bcyyvdiyJ0S91lCrI6JO2Ta17/UhgChvyLto5On6ZFa/z4fEx2jZ7f0vhqXokXTaQB gOmuCbZxDf17hdKzPmloguvHBsZ2AxX/0QfuwstffxtEbfCJM0pdoR3+J+3JJX0bcwvX rmfnWXmNBgmCy5xggNDFp+iDuAupsqwFH+JXFjZncvwjgkwe5a5vgopCqG78V9+84x47 kVOQEVwuuQP7FZ57pdsciEpX2neh4Q5LZRSZXK8dbMG5LRlgEDw2CySG/mHDX/TNfFnm faZXJl8oPraJQauai9VwSFwIgi6RMPxAw0R7TBbokOqjRFCY12hIZtkAqxkzedJNJysp VxAQ== X-Gm-Message-State: APt69E3tBACKl/Q3e2SRmL9pDMT6EZ3E03+/qi8NXnLAmt6I4/O89jSJ 7yVCAtJk0q6Ro2V9f4QyC4AHlXh48eE= X-Received: by 2002:adf:fb43:: with SMTP id c3-v6mr2534500wrs.32.1530037731293; Tue, 26 Jun 2018 11:28:51 -0700 (PDT) Received: from rev02.home ([2a01:cb1d:112:6f00:d037:e907:f0a2:a5ea]) by smtp.gmail.com with ESMTPSA id s17-v6sm1707664wmc.34.2018.06.26.11.28.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Jun 2018 11:28:50 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Arnd Bergmann , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Russell King , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Josh Poimboeuf , Steven Rostedt , Sergey Senozhatsky , Linus Torvalds , Jessica Yu , linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: [PATCH v9 6/6] kernel: tracepoints: add support for relative references Date: Tue, 26 Jun 2018 20:28:01 +0200 Message-Id: <20180626182802.19932-7-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180626182802.19932-1-ard.biesheuvel@linaro.org> References: <20180626182802.19932-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: Ingo Molnar Acked-by: Steven Rostedt (VMware) Signed-off-by: Ard Biesheuvel --- include/linux/tracepoint.h | 19 ++++++-- kernel/tracepoint.c | 49 +++++++++++--------- 2 files changed, 41 insertions(+), 27 deletions(-) -- 2.11.0 diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index 19a690b559ca..b130e40d82cb 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -225,6 +225,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 __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 @@ -234,11 +247,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 6dc6356c3327..451c8f5e8345 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -325,6 +325,27 @@ 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(offset_to_ptr(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) { @@ -389,15 +410,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) @@ -448,8 +463,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; } } @@ -501,19 +517,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